fix(adapter-utils): extend the duplex fail-closed run disposition to the CLI lane (#11966)

## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - Paperclip runs agents through adapter execution lanes
> - Duplex adapters can lose their control channel before a process
completes
> - The ACP lane already fails closed, but the CLI lane can report false
success
> - This pull request applies the same completion rule to the CLI lane
and shares the loss code
> - The benefit is consistent failure reporting when a duplex channel
closes during a run

## Linked Issues or Issue Description

**What happened?**

A CLI-lane duplex run can lose its control channel before clean process
completion. The run can then report `succeeded` with exit code 0 and no
error code.

**Expected behavior**

The execution target must fail closed when the channel dies before clean
completion. It must return exit code 1, the typed `duplex_channel_lost`
error code, and a short stderr note.

**Steps to reproduce**

1. Start a duplex adapter run through the CLI execution lane.
2. Close the duplex control channel before the process completes
cleanly.
3. Inspect the run result and error code.

**Paperclip version or commit**

Commit `5e01523d4eb6df4a20a0bddd05374c9c42225203`.

**Deployment mode**

Built from source.

**Installation method**

Built from source with pnpm.

**Agent adapter(s) involved**

Claude Code, Codex, Cursor, Gemini, Kimi, OpenCode, and Pi local
adapters.

**Database mode**

Not database-related.

## What Changed

- Add an optional `errorCode` field to `RunProcessResult`.
- Add a one-read completion seam to the execution target process
options.
- Fail closed when a duplex channel dies before clean process
completion.
- Add `settleRunDisposition()` to atomically read and mark orderly
completion.
- Share the typed duplex loss error code across the ACP and CLI lanes.
- Mark non-success terminal results as orderly completion before
teardown.
- Wire the seam through the seven duplex adapters.
- Add regression tests for channel loss, clean completion, and non-clean
terminal results.

## Verification

- `npx vitest run
packages/adapter-utils/src/execution-target-sandbox.test.ts` — 118
passed.
- `npx vitest run packages/adapter-utils/src/acpx-engine/execute.test.ts
-t "sandbox duplex run-disposition seam"` — 4 passed.
- The author confirmed a clean type-check for
`@paperclipai/adapter-utils` and the seven duplex adapter packages.
- Pre-existing environment failures remain outside this change. They
include `EACCES mkdir '/srv/paperclip'` and remote file-size setup
failures.

## Risks

The change alters terminal status for CLI duplex runs that lose control
before clean completion. The typed error code and stderr note keep the
failure visible. The broker marks failed, cancelled, and timed-out
results as orderly completion to prevent false loss events during
teardown.

## Model Used

OpenAI Codex, GPT-5, tool use and code execution, with the standard
GPT-5 context window. The model assisted with the implementation and
test work.

## 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
- [x] All Paperclip CI gates are green
- [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

---------

Co-authored-by: Paperclip <noreply@paperclip.ing>
This commit is contained in:
Nicky LeachandPaperclip authored and GitHub committed 2026-08-22 11:12:51 -07:00
1 parent f572e08678
commit cc42a67e7e
14 files changed
+503 -22

No files matched your search

+63 -1
View File
@@ -45,6 +45,7 @@ import {
import {
createDuplexBridgeBroker,
DEFAULT_DUPLEX_BROKER_BUDGETS,
DUPLEX_CHANNEL_LOST_ERROR_CODE,
isSafeBridgeMethod,
typedDuplexLossReason,
type DuplexBridgeBroker,
@@ -235,6 +236,18 @@ export interface AdapterExecutionTargetProcessOptions {
* onLog is suppressed and incremental chunks flow through `onLog` instead.
*/
runLogTail?: SandboxRunLogTailFactory | null;
/**
* Sandbox-only: the atomic run-disposition settle from the Paperclip bridge
* handle. When provided, `runAdapterExecutionTargetProcess` calls it once at
* the clean-completion boundary of the process, synchronously and before the
* run-log tail finishes. The call reads the disposition and marks the
* host-observed orderly completion in one broker step, so a gateway exit
* after the clean process completion cannot latch a false mid-run loss. A
* control channel that died before the clean completion still fails the run
* closed with the typed `duplex_channel_lost` code. The file bridge path
* never sets it.
*/
settleRunDisposition?: (() => DuplexBrokerRunDisposition) | null;
localProcessSandbox?: LocalProcessSandboxOptions | null;
}
@@ -263,6 +276,15 @@ export interface AdapterExecutionTargetPaperclipBridgeHandle {
* whose control channel died mid-turn.
*/
readRunDisposition?(): DuplexBrokerRunDisposition;
/**
* Atomically read the run disposition and mark the host-observed orderly
* completion in one broker step. The ACP lane calls it at the terminal
* finalization boundary for a success-eligible completion, so no `await` can
* separate the read from the mark and a teardown loss cannot slip in between.
* A loss that already latched keeps the failure, because the broker no-ops the
* mark after a latched loss. The file bridge path never sets it.
*/
settleRunDisposition?(): DuplexBrokerRunDisposition;
/**
* Mark the host-observed orderly completion of the agent turn on the broker's
* ordered lifecycle. The caller marks it at the ACP terminal-finalization
@@ -720,6 +742,35 @@ export async function resolveAdapterExecutionTargetCommandForLogs(
});
}
// Apply the run-disposition seam to one clean process result. Only a clean
// completion is success-eligible: a timed-out, signalled, or non-zero-exit
// result is already a failure, so the seam leaves it unchanged. This is the same
// success-eligibility rule the ACP lane applies. For a success-eligible result
// the seam settles the disposition in one atomic broker step: it reads the
// disposition and marks the host-observed orderly completion together, so a
// gateway exit after the clean completion cannot latch a false mid-run loss. A
// duplex control channel that died before the clean completion fails the run
// closed: the seam sets a non-zero exit code, the typed `duplex_channel_lost`
// error code, and a stderr note that names only the typed loss reason.
function applyRunDispositionSeam(
result: RunProcessResult,
settleRunDisposition: (() => DuplexBrokerRunDisposition) | null | undefined,
): RunProcessResult {
const successEligible = result.exitCode === 0 && !result.timedOut && result.signal === null;
if (!successEligible || !settleRunDisposition) return result;
const disposition = settleRunDisposition();
if (!disposition.failed) return result;
const lossReason = disposition.lossReason ?? "other";
const note = `[paperclip] The sandbox duplex control channel was lost (${lossReason}) before the run completed.\n`;
const separator = result.stderr.length > 0 && !result.stderr.endsWith("\n") ? "\n" : "";
return {
...result,
exitCode: 1,
errorCode: DUPLEX_CHANNEL_LOST_ERROR_CODE,
stderr: `${result.stderr}${separator}${note}`,
};
}
export async function runAdapterExecutionTargetProcess(
runId: string,
target: AdapterExecutionTarget | null | undefined,
@@ -756,10 +807,17 @@ export async function runAdapterExecutionTargetProcess(
? async (meta) => options.onSpawn?.({ ...meta, processGroupId: null })
: undefined,
});
// Settle the duplex run disposition synchronously at the clean-completion
// boundary, before the run-log tail finishes. The atomic settle marks the
// host-observed orderly completion in one broker step, so a gateway exit
// after the clean process completion cannot latch a false mid-run loss. A
// control channel that died before this clean completion still fails the
// run closed.
const settled = applyRunDispositionSeam(result, options.settleRunDisposition);
if (runLogTail) {
await runLogTail.finish({ stdout: result.stdout, stderr: result.stderr });
}
return result;
return settled;
} catch (error) {
if (runLogTail) {
await runLogTail.abort();
@@ -3144,6 +3202,10 @@ export async function startAdapterExecutionTargetPaperclipBridge(input: {
// seam. A loss ordered before an orderly completion reports a failure
// with the typed loss reason; every other state reports a success.
readRunDisposition: (): DuplexBrokerRunDisposition => activeBroker.runDisposition,
// Atomically read the latch and mark the orderly completion for the
// ACP success-eligible terminal, so no await separates the read from
// the mark and a teardown loss cannot slip in between.
settleRunDisposition: (): DuplexBrokerRunDisposition => activeBroker.settleRunDisposition(),
// Surface the broker's orderly-completion mark to the run-disposition
// seam. The seam marks the completion for a success-eligible terminal,
// so a teardown loss after the completion stays a normal teardown.