Files
PaperClipAI/server/src/services/native-runtime/status-arbiter.ts
T
DottaandPaperclip c9021c6721 fix: require explicit native completion reviews (#13314)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - Native runs report their outcome through paperclip_finish.
> - The server previously turned incomplete reports into human approval
requests.
> - Those requests could block a later successful run, even when no
person had requested review.
> - This pull request creates review cards only for explicit attention
requests and withdraws proven old fallback cards.
> - Agents receive useful completion feedback, while explicit approval
gates and task state protections remain in force.

## Linked Issues or Issue Description

Related: #13266 removed reviews caused by policy upgrades. This change
removes the separate completion fallback.

**What happened?**

An agent reported needs_review while waiting for checks without
requesting a human decision. Paperclip created a generic Native
completion review. A later successful report could not complete the task
because that old card remained pending.

**Expected behavior**

Ordinary low-risk work completes after a valid done report, a successful
run, and workspace finalization. Incomplete work stays with the agent.
Explicit approval requests remain visible and must be resolved.

**Steps to reproduce**

1. Complete a native run with needs_review and no attention requests.
2. Continue the task and submit a successful done report.
3. Observe that the old implementation leaves the task in review behind
a generic confirmation card.

## What Changed

- Require explicit attention requests to create native review cards.
Route each request independently and preserve pending or declined
decisions.
- Withdraw only pending system cards with matching old decision,
assessment, effect, contract, and prompt provenance. Preserve history
and explicit or answered requests.
- Reassess an affected current result without overwriting later task
edits, runs, contracts, or workspace failures.
- Return pending approval links and required actions through the
completion tool. Reject contradictory done reports and empty review
requests before accepting a result.
- Allow one corrective continuation for incomplete results, then expose
a recovery action.
- Update status fixtures, database regressions, runner tests, and the
completion contract documentation.

## Verification

- `pnpm -r typecheck` passed after merging current master.
- `pnpm build` passed after merging current master.
- The combined branch passed 89 completion and Agent Chat tests. Other
targeted tests passed: 170 external-chat and reconciliation tests; 50
runner-resume and control-plane tests; 13 arbiter tests; 7 chat delivery
tests; 21 runner completion and runtime-context tests.
- The full local test attempt exposed old review fixtures and a missing
fake-provider binary. The fixtures are fixed and the helper is built.
All affected suites pass in fresh reruns. The timing-sensitive Discord
test also passed on rerun.
- All latest-head CI checks passed, including build, typecheck, general
and serialized tests, runner verification, browser tests, and canary dry
run. Greptile is 5/5 with zero unresolved comments.

## Risks

- Cleanup changes existing pending cards. It requires exact system
provenance and only applies to low-risk agent-claim contracts. It does
not delete history or dismiss explicit requests.
- Status still commits after the turn and workspace finalization.
Completion feedback reports current constraints and does not claim an
early status commit.
- Incomplete reports now request a bounded corrective run instead of an
automatic approval. Repeated failures expose recovery.

## Model Used

OpenAI Codex, GPT-6, with repository inspection, code execution, and
test tools. The exact deployment identifier and context window size are
not exposed in this session.

## 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>
2026-09-12 13:00:04 -05:00

476 lines
16 KiB
TypeScript

import type { NativeEvidenceAssessment } from "./evidence-classifier.js";
export const NATIVE_STATUS_ARBITER_POLICY_VERSION = "phase6-v5";
export type NativeAuthoritativeIssueStatus =
| "backlog"
| "todo"
| "in_progress"
| "in_review"
| "blocked"
| "done"
| "cancelled";
export type NativeGovernanceGate = {
kind: "approval" | "interaction" | "execution_stage";
id: string;
};
export type NativeStatusEffect =
| { kind: "create_interaction"; gate?: NativeGovernanceGate; prompt?: string }
| {
kind: "bind_reviewer";
requestKey?: string;
prompt: string;
detailsMarkdown?: string | null;
ownerUserId?: string | null;
ownerAgentId?: string | null;
}
| { kind: "notify_owner"; agentId: string; reason: string }
| {
kind: "enqueue_continuation";
continuationKind:
| "same_agent"
| "retry"
| "delegated_issue"
| "response_wake"
| "monitor";
summary: string;
idempotencyKey: string;
agentId: string;
}
| {
kind: "bind_blocker";
owner: { agentId: string } | "board";
action: string;
}
| { kind: "schedule_retry"; cause: string; summary: string; agentId: string }
| {
kind: "record_finalization_error";
cause: string;
nextAction: string;
agentId: string;
}
| { kind: "release_run_resources" }
| { kind: "create_delegated_issue"; agentId: string; summary: string }
| { kind: "accept_replacement_turn" }
| { kind: "cancel_continuations" }
| { kind: "append_superseding_assessment" }
| { kind: "dispatch_pending_effect" }
| { kind: "increment_status_version" }
| { kind: "schedule_reconciliation" }
| { kind: "record_shadow_decision" }
| { kind: "render_four_layers" }
| { kind: "materialize_contract" }
| { kind: "record_mode_labeled_divergence" }
| { kind: "record_mode_native" }
| { kind: "record_policy_version" }
| { kind: "finish_as_native" }
| { kind: "resume_workspace_operation" }
| { kind: "record_expiry" }
| { kind: "record_stale_response" }
| { kind: "link_canonical_request" }
| {
kind: "record_recovery";
cause: string;
nextAction: string;
agentId: string;
}
| { kind: "release_checkout" };
export interface NativeStatusDecision {
policyVersion: typeof NATIVE_STATUS_ARBITER_POLICY_VERSION;
statusAction: NativeAuthoritativeIssueStatus | "preserve";
toStatus: NativeAuthoritativeIssueStatus;
reasonCode: string | null;
unblockDescriptor: {
owner: { agentId: string } | "board";
action: string;
} | null;
effects: NativeStatusEffect[];
}
/** Pure authority boundary: model prose is evidence, never a status command. */
export function arbitrateNativeStatus(input: {
assessment: NativeEvidenceAssessment;
terminalState: "succeeded" | "failed" | "cancelled";
workspaceFinalizeStatus: "succeeded" | "failed";
governanceGate?: NativeGovernanceGate | null;
completionClaimPolicyAccepted?: boolean;
allowIncompleteContinuation?: boolean;
/**
* True when the issue already has a durable unresolved dependency edge.
* A "current_track" result cannot remain in_progress in that state because
* checkout is intentionally gated until the dependency resolves.
*/
hasUnresolvedIssueBlockers?: boolean;
/** A governance interaction created by this run was accepted before the run settled. */
governanceResolvedForRun?: boolean;
externalChatResponseWaitAuthorization?:
"authorized" | "revoked" | "not_applicable";
boardResponseWaitAuthorized?: boolean;
boardResponseWaitOrigin?: boolean;
reviewOwnerUserId?: string | null;
agentId: string;
priorIssueStatus: NativeAuthoritativeIssueStatus;
}): NativeStatusDecision {
if (["done", "cancelled"].includes(input.priorIssueStatus)) {
return {
policyVersion: NATIVE_STATUS_ARBITER_POLICY_VERSION,
statusAction: "preserve",
toStatus: input.priorIssueStatus,
reasonCode: "terminal_status_preserved",
unblockDescriptor: null,
effects: [],
};
}
if (input.workspaceFinalizeStatus !== "succeeded") {
return {
policyVersion: NATIVE_STATUS_ARBITER_POLICY_VERSION,
statusAction: "preserve",
toStatus: input.priorIssueStatus,
reasonCode: "finalization_failed_claim_preserved",
unblockDescriptor: null,
effects: [
{
kind: "record_finalization_error",
cause: "workspace_finalization_failed",
nextAction:
"Repair and re-run workspace finalization for the persisted native result.",
agentId: input.agentId,
},
],
};
}
if (input.terminalState !== "succeeded") {
if (input.terminalState === "cancelled") {
return {
policyVersion: NATIVE_STATUS_ARBITER_POLICY_VERSION,
statusAction: "preserve",
toStatus: input.priorIssueStatus,
reasonCode: "cancellation_run_only",
unblockDescriptor: null,
effects: [{ kind: "release_run_resources" }],
};
}
return {
policyVersion: NATIVE_STATUS_ARBITER_POLICY_VERSION,
statusAction: "preserve",
toStatus: input.priorIssueStatus,
reasonCode: "run_failed_partial_evidence_preserved",
unblockDescriptor: null,
effects: [
{
kind: "schedule_retry",
cause: "native_run_failed",
summary:
"Resume the persisted native run without opening a second provider session.",
agentId: input.agentId,
},
],
};
}
if (input.governanceGate) {
if (
input.assessment.reportedDisposition === "yielded" &&
input.assessment.continuation?.kind === "response_wake"
) {
return {
policyVersion: NATIVE_STATUS_ARBITER_POLICY_VERSION,
statusAction: "in_review",
toStatus: "in_review",
reasonCode: "governed_response_waiting",
unblockDescriptor: null,
// The existing interaction is the liveness path. Its authoritative
// resolution owns the response wake; dispatching one now would start a
// duplicate provider turn before the human has answered.
effects: [{ kind: "create_interaction", gate: input.governanceGate }],
};
}
return {
policyVersion: NATIVE_STATUS_ARBITER_POLICY_VERSION,
statusAction: "in_review",
toStatus: "in_review",
reasonCode: "governed_gate_pending",
unblockDescriptor: null,
effects: [
{ kind: "create_interaction", gate: input.governanceGate },
{
kind: "notify_owner",
agentId: input.agentId,
reason: "governed_gate_pending",
},
],
};
}
if (
input.governanceResolvedForRun === true &&
["needs_review", "blocked"].includes(
input.assessment.reportedDisposition,
) &&
input.assessment.attentionRequests.length > 0 &&
input.assessment.attentionRequests.every(
(request) =>
request.ownerClass === "human" &&
["review", "approval"].includes(request.kind),
)
) {
return {
policyVersion: NATIVE_STATUS_ARBITER_POLICY_VERSION,
statusAction: "in_review",
toStatus: "in_review",
reasonCode: "governance_response_continuation_queued",
unblockDescriptor: null,
// The accepted interaction already queued the response wake. Creating a
// second completion-review interaction here would strand that continuation.
// Planning turns commonly report `blocked` while awaiting their own
// confirmation; once accepted, that response wake is authoritative too.
effects: [],
};
}
if (
input.hasUnresolvedIssueBlockers === true &&
["done", "blocked"].includes(input.assessment.reportedDisposition)
) {
const owner = input.assessment.blocker?.boardOwned
? ("board" as const)
: { agentId: input.agentId };
return {
policyVersion: NATIVE_STATUS_ARBITER_POLICY_VERSION,
statusAction: "blocked",
toStatus: "blocked",
reasonCode: "durable_dependency_blocker_bound",
unblockDescriptor: {
owner,
action:
input.assessment.blocker?.unblockAction ??
"Wait for the issue's durable dependency blockers to complete.",
},
// Dependency completion owns the wake. Do not immediately rerun the
// blocked issue merely because the provider mislabeled its scope/status.
effects: [],
};
}
const evidenceComplete =
input.assessment.reportedDisposition === "done" &&
input.assessment.objectiveSatisfied &&
input.assessment.allCriteriaSatisfied &&
input.assessment.verificationPassed &&
!input.assessment.hasFailedVerification &&
input.assessment.attentionRequests.length === 0 &&
!input.assessment.hasBlockingRemainingWork;
const policyClaimComplete =
input.completionClaimPolicyAccepted === true &&
input.assessment.reportedDisposition === "done" &&
input.assessment.contractRevisionMatches &&
input.assessment.objectiveClaimSatisfied &&
input.assessment.criterionAssessments.length > 0 &&
input.assessment.criterionAssessments.every(
(entry) => entry.claimStatus === "satisfied",
) &&
!input.assessment.hasFailedVerification &&
input.assessment.attentionRequests.length === 0 &&
!input.assessment.hasBlockingRemainingWork;
const complete = evidenceComplete || policyClaimComplete;
if (complete) {
return {
policyVersion: NATIVE_STATUS_ARBITER_POLICY_VERSION,
statusAction: "done",
toStatus: "done",
reasonCode: input.completionClaimPolicyAccepted
? "completion_claim_policy_accepted"
: "completion_contract_satisfied",
unblockDescriptor: null,
effects: [{ kind: "release_checkout" }],
};
}
// A completion claim is not a request for human approval. Only a concrete,
// explicitly reported attention request may create a review interaction.
if (input.assessment.attentionRequests.length > 0) {
return {
policyVersion: NATIVE_STATUS_ARBITER_POLICY_VERSION,
statusAction: "in_review",
toStatus: "in_review",
reasonCode: "actionable_attention_pending",
unblockDescriptor: null,
effects: input.assessment.attentionRequests.map((request, index) => ({
kind: "bind_reviewer",
requestKey: `attention-${index}`,
prompt: request.summary.slice(0, 1_000),
detailsMarkdown: input.assessment.summary,
ownerUserId: request.ownerClass === "agent" ? null : (input.reviewOwnerUserId ?? null),
ownerAgentId: request.ownerClass === "agent" ? request.targetAgentId : null,
})),
};
}
if (
input.assessment.reportedDisposition === "blocked" &&
input.assessment.blocker
) {
const owner = input.assessment.blocker.boardOwned
? ("board" as const)
: { agentId: input.agentId };
if (input.assessment.blocker.scope === "task_wide") {
return {
policyVersion: NATIVE_STATUS_ARBITER_POLICY_VERSION,
statusAction: "blocked",
toStatus: "blocked",
reasonCode: "task_wide_blocker_bound",
unblockDescriptor: {
owner,
action: input.assessment.blocker.unblockAction,
},
effects: [
{
kind: "bind_blocker",
owner,
action: input.assessment.blocker.unblockAction,
},
{
kind: "notify_owner",
agentId: input.agentId,
reason: "task_wide_blocker_bound",
},
],
};
}
return {
policyVersion: NATIVE_STATUS_ARBITER_POLICY_VERSION,
statusAction: "blocked",
toStatus: "blocked",
reasonCode: "current_track_blocker_waiting",
// A provider's current_track label is not evidence that another
// authorized, productive track exists. Bind the actual unblock request
// without waking the same agent to repeat the blocked work or old title.
unblockDescriptor: {
owner: "board",
action: input.assessment.blocker.unblockAction,
},
effects: [
{
kind: "bind_blocker",
owner: "board",
action: input.assessment.blocker.unblockAction,
},
],
};
}
if (
input.assessment.reportedDisposition === "yielded" &&
input.assessment.continuation?.kind === "response_wake" &&
input.externalChatResponseWaitAuthorization === "authorized"
) {
return {
policyVersion: NATIVE_STATUS_ARBITER_POLICY_VERSION,
statusAction: "in_progress",
toStatus: "in_progress",
reasonCode: "external_chat_response_waiting",
unblockDescriptor: null,
// The active, authorized provider conversation is the durable liveness
// path. Only its next admitted message may enqueue the response wake.
effects: [],
};
}
if (
input.assessment.reportedDisposition === "yielded" &&
input.assessment.continuation?.kind === "response_wake" &&
input.externalChatResponseWaitAuthorization === "revoked"
) {
return {
policyVersion: NATIVE_STATUS_ARBITER_POLICY_VERSION,
statusAction: "preserve",
toStatus: input.priorIssueStatus,
reasonCode: "external_chat_response_wait_authorization_lost",
unblockDescriptor: null,
// Revocation cannot be converted into an unrequested background run.
effects: [],
};
}
if (
input.assessment.reportedDisposition === "yielded" &&
input.assessment.continuation?.kind === "response_wake" &&
input.boardResponseWaitAuthorized === true
) {
return {
policyVersion: NATIVE_STATUS_ARBITER_POLICY_VERSION,
statusAction: "in_progress",
toStatus: "in_progress",
reasonCode: "board_response_waiting",
unblockDescriptor: null,
// The committer proves the current Board cause again before persisting
// the wait. Only a new user cause may wake this completed response.
effects: [],
};
}
if (
input.assessment.reportedDisposition === "yielded" &&
input.assessment.continuation?.kind === "response_wake" &&
input.boardResponseWaitOrigin
) {
return {
policyVersion: NATIVE_STATUS_ARBITER_POLICY_VERSION,
statusAction: "preserve",
toStatus: input.priorIssueStatus,
reasonCode: "board_response_wait_superseded",
unblockDescriptor: null,
effects: [],
};
}
if (
input.assessment.reportedDisposition === "yielded" &&
input.assessment.continuation
) {
return {
policyVersion: NATIVE_STATUS_ARBITER_POLICY_VERSION,
statusAction: "in_progress",
toStatus: "in_progress",
reasonCode: "live_continuation_registered",
unblockDescriptor: null,
effects: [
{
kind: "enqueue_continuation",
continuationKind: input.assessment.continuation.kind,
summary: input.assessment.continuation.summary,
idempotencyKey: input.assessment.continuation.idempotencyKey,
agentId: input.agentId,
},
],
};
}
if (input.allowIncompleteContinuation === false) {
return {
policyVersion: NATIVE_STATUS_ARBITER_POLICY_VERSION,
statusAction: "preserve",
toStatus: input.priorIssueStatus,
reasonCode: "prior_status_preserved_no_live_path",
unblockDescriptor: null,
effects: [
{
kind: "record_finalization_error",
cause: "completion_evidence_incomplete",
nextAction:
"Bind a durable continuation or a named recovery owner before changing issue status.",
agentId: input.agentId,
},
],
};
}
return {
policyVersion: NATIVE_STATUS_ARBITER_POLICY_VERSION,
statusAction: "in_progress",
toStatus: "in_progress",
reasonCode: "completion_evidence_incomplete",
unblockDescriptor: null,
effects: [
{
kind: "enqueue_continuation",
continuationKind: "same_agent",
summary:
"Finish the remaining work and report done, or explicitly request a named reviewer decision. Waiting for checks or an incomplete completion report does not require human approval.",
idempotencyKey: "native-completion-incomplete",
agentId: input.agentId,
},
],
};
}