mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-25 16:14:26 +01:00
## Thinking Path
> - Paperclip is the open source app people use to manage AI agents for
work
> - When a person terminates an agent, Paperclip stops the agent's
running native turn
> - Two paths can record that Stop: the terminate route ("Cancelled
because the agent was terminated ...") and the lifecycle worker ("Agent
lifecycle stop requested"). Both are system-initiated and expected
> - When the stopped turn ends without a semantic result, the native
executor keeps the run cancelled only for a person's Stop, a
reassignment, or a governance pause. A termination Stop fell through, so
the run ended `failed` with `native_session_interrupted`
> - Whether the run ended cancelled or failed depended on which path won
the race
> - This pull request treats an acknowledged termination Stop like a
person's Stop in the executor and in the lease-release terminalization
> - The benefit is that a terminated agent's interrupted run ends
cancelled, whichever path records the Stop first
## Linked Issues or Issue Description
Refs #20186 (the same treatment for a governance pause Stop)
**What happened?**
A person chose Terminate for an agent while its native turn ran. The
lifecycle Stop recorded an acknowledged native cancellation (initiator
system). The turn ended without a semantic result. The executor then
wrote the run as `failed` with `native_finalization_missing: session
returned no semantic result`.
**Expected behavior**
The run ends `cancelled`. A termination is an intentional Stop, not a
failure to retry.
**Steps to reproduce**
Terminate an agent while its native turn runs, so that the Stop
interrupts the turn before it records a result. Before this change, the
run became `failed` when the lifecycle worker recorded the Stop first.
## What Changed
- `governance-pause-stop.ts`: adds `AGENT_TERMINATED_STOP_REASON`,
`COMPANY_ARCHIVED_STOP_REASON` and `isAgentLifecycleStopRecord`. The
predicate accepts a system-initiated, expected control-plane
cancellation whose reason is a lifecycle, termination or company-archive
Stop.
- `acknowledged-native-stop.ts`: adds
`hasAcknowledgedNativeLifecycleStopIntent`, which requires that record
and an acknowledged, dispatched native Stop receipt.
- `native-session-executor.ts`: the no-semantic-result branch accepts
that intent, so it settles the coordinator as `native_retry_cancelled`
and raises `NativeCancellationPendingRecoveryError`, as for a person's
Stop.
- `heartbeat/run-lifecycle.ts`: lease-release terminalization keeps such
a run `cancelled`.
- The terminate route and the lifecycle driver use the shared reason
constants. The reason text does not change.
## Verification
- `native-session-executor.test.ts`: a new case for each reason
(lifecycle Stop, terminate route, company archive). Each was red before
the change and is green now. The earlier control case now uses a system
Stop that is neither a pause nor a termination, and it still takes the
retry path. Full file: 1414 passed.
- `heartbeat-run-lease-release-terminalization.test.ts`: a new case for
a lifecycle Stop when teardown wins the finalizer race. It was red
before the change (`interrupted`) and is green now.
- Product evidence: a paid chat-eval run of `agents.retire-agent` on a
branch that combines this change with three related fixes. The result is
in a PR comment.
## Risks
- A termination or company-archive Stop that interrupts a turn no longer
schedules an exact resume of that turn. The agent is leaving service, so
no resume can run.
- Other system Stops keep their current behavior.
## Model Used
- Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, via Claude Code with
tool use (shell, file edits, test execution). Extended reasoning
enabled.
## Checklist
- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [ ] All Paperclip CI gates are green
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
43 lines
2.6 KiB
TypeScript
43 lines
2.6 KiB
TypeScript
/**
|
|
* A governance pause stop: the control plane cancelled the run because its
|
|
* agent was paused (the direct pause route, or the agent lifecycle stop that a
|
|
* budget, company or manual pause issues) or because a budget hard stop fenced it. The stop is
|
|
* intentional, not a failure: the task keeps its status, and resuming
|
|
* continues the work (budget-resume-continuation.ts). It never becomes a
|
|
* reconciliation incident that has no control.
|
|
*/
|
|
export const AGENT_LIFECYCLE_STOP_REASON = "Agent lifecycle stop requested";
|
|
export const BUDGET_PAUSE_STOP_REASON = "Cancelled due to budget pause";
|
|
/** The direct pause route stops the agent's runs with this reason before the lifecycle worker runs. */
|
|
export const AGENT_PAUSE_STOP_REASON = "Cancelled due to agent pause";
|
|
export const AGENT_TERMINATED_STOP_REASON =
|
|
"Cancelled because the agent was terminated or became invalid-org-chain under a terminated manager";
|
|
export const COMPANY_ARCHIVED_STOP_REASON = "Cancelled because the company was archived";
|
|
|
|
const GOVERNANCE_PAUSE_STOP_REASONS: ReadonlySet<string> = new Set([
|
|
AGENT_LIFECYCLE_STOP_REASON, BUDGET_PAUSE_STOP_REASON, AGENT_PAUSE_STOP_REASON]);
|
|
|
|
const record = (value: unknown): Record<string, unknown> =>
|
|
value !== null && typeof value === "object" && !Array.isArray(value) ? value as Record<string, unknown> : {};
|
|
|
|
export function isGovernancePauseStop(run: { status: string; resultJson?: Record<string, unknown> | null }): boolean {
|
|
if (run.status !== "cancelled") return false;
|
|
const cancellation = record(run.resultJson?.cancellation);
|
|
if (record(cancellation.initiator).type !== "system" || cancellation.expected !== true
|
|
|| typeof cancellation.reason !== "string" || !GOVERNANCE_PAUSE_STOP_REASONS.has(cancellation.reason)) return false;
|
|
// Termination and cleanup issue the same lifecycle stop reason. Only the
|
|
// pausing path records pauseStop, so require it for that reason.
|
|
return cancellation.reason !== AGENT_LIFECYCLE_STOP_REASON
|
|
|| Object.keys(record(run.resultJson?.pauseStop)).length > 0;
|
|
}
|
|
|
|
const AGENT_TERMINATION_STOP_REASONS: ReadonlySet<string> = new Set([
|
|
AGENT_LIFECYCLE_STOP_REASON, AGENT_TERMINATED_STOP_REASON, COMPANY_ARCHIVED_STOP_REASON]);
|
|
|
|
export function isAgentLifecycleStopRecord(resultJson: Record<string, unknown> | null | undefined): boolean {
|
|
const cancellation = record(resultJson?.cancellation);
|
|
return record(cancellation.initiator).type === "system" && cancellation.source === "control_plane"
|
|
&& cancellation.expected === true && typeof cancellation.reason === "string"
|
|
&& AGENT_TERMINATION_STOP_REASONS.has(cancellation.reason);
|
|
}
|