mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-02 02:30:43 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - Google Antigravity agents can run Google's native `/swarm` (a coordinator, helpers and a verifier) only on the Antigravity CLI > - #22731 adds a verified CLI route to the Runner, but nothing selects it yet > - A swarm must start only when the signed-in user asks for it, only when an admin turned the experimental feature on, and never silently run as an ordinary request when it cannot run > - This pull request adds the trusted `/swarm` request in the task composer, the server checks and run selection behind an experimental flag, removes Paperclip delegation tools from a swarm turn, and adds the attended catalog cells, docs and ledger > - The benefit is that a user can start a native swarm from a task, and gets one plain message when it is not available ## Linked Issues or Issue Description Refs #22731, #22727, #22728, #22701 **Subsystem affected** Task composer (UI), issue comments API, run selection (server), Paperclip Runner tool bridge, runner-e2e catalog, docs. **Problem or motivation** A user who writes `/swarm <request>` to a Google Antigravity agent should get a real native swarm in the task folder. Today `/swarm` is neutralised text, and the task runs as an ordinary request. **Proposed solution** Read a leading `/swarm` with the shared rule from #22727 in the composer, send it as a structured request, check it on the server, and select the CLI route from #22731 for that run only. ## What Changed - **Trusted start (UI).** `parseSwarmCommand` in `composer-commands.ts` uses `parseLeadingUserCommand` (the one rule from #22727). A leading `/swarm` in the task composer sends the request text with `nativeCommand: "swarm"`; a bare `/swarm` asks for a request; `/swarm` with an assignee change is refused; `/swarm` anywhere else stays text. A resent draft keeps the structured request. - **Contract.** `addIssueCommentSchema` gains `nativeCommand: "swarm"`. `instance.experimental.enableAntigravitySwarm` (feature catalog, settings page, server defaults), off by default. No Cloud policy in Core. - **Server (`services/antigravity-swarm.ts`, comments route, runtime selection).** - The comments route accepts `nativeCommand` only from a signed-in board user. When a swarm cannot run it answers 422 with one plain message and posts nothing: flag off, not a Google Antigravity assignee, a chat, Plan mode. - An accepted request is recorded in the activity log (`issue.antigravity_swarm_requested`, server-written) and the assignee's wake carries the comment id. Comment text is never read as a command. - Run selection makes the run a swarm only when that activity record exists. It refuses a remote or cloud environment and Plan mode with a plain run failure, so a swarm is never run as an ordinary request. It logs `issue.antigravity_swarm_started` in plain language: helper actions are auto-approved, Paperclip does not sandbox the helpers and cannot see their shell commands, and usage is one total. - The native execution input sets the Antigravity provider mode `swarm` for that run only, and carries the verified request in its own field, `provider.swarmRequest` (the Runner contract accepts it only with that mode and requires it there, 1 to 16,000 characters). The Runner copies it into the model envelope as `swarmRequest`; the swarm route sends exactly `/swarm <swarmRequest>` and never reads `task.prompt`, which holds rendered wake comments, task context and text the Runner adds (handoff, attachments, queued messages). - The swarm turn contract (mode `swarm`, envelope `paperclip.native-model-envelope.v3`, `swarmRequest` of 1 to 16,000 characters) is declared as `antigravitySwarmTurn` in `protocol/provider-schemas/acpx-sidecar.schema.json` and generated into TypeScript, so the pack `contractDigest` covers it (it changes; older image packs are rebuilt rather than reused). Provider profile `commandDigest`s and versions are unchanged, so no saved session is reset. - What the swarm sees of the task: only the request. The title, the description (which other people or agents may have written) and other comments are not part of the prompt; the coordinator can read them through Paperclip's task tools as data if it needs context. - An empty `/swarm` or one over 16,000 characters is refused in the composer, by the comments route (422, nothing posted) and at run selection (plain run failure), so it never runs as ordinary work. - A swarm run never takes queued messages, and Interrupt treats it as already started: it stops the swarm and delivers the queued message as the next ordinary request, before or after the provider accepted the turn. - Retry after Stop, and past the three automatic attempts, counts only when the signed-in user asked for it (their own Retry request on the task after the last attempt). Wake reasons and copied run context are not proof: a recovery that copies the request marker, an automatic retry on the user's behalf, or an agent wake that names `task_retry` stay bounded. - **Runner.** For a swarm turn the Paperclip bridge drops delegation tools (`paperclip_create_task`, `paperclip_create_issue`, hire/assign tools); the instructions also tell the coordinator not to delegate. - **runner-e2e.** `antigravity-swarm` suite, 5 local cells (`two-helpers-and-verifier`, `stop-mid-swarm`, `process-loss-retry`, `follow-up-after-swarm`, `flag-off-refusal`), explicit-only, never in `--all` or CI matrix jobs. There is no automated executor yet: the launcher refuses the cells and points at the live recipe. Catalog counts updated (1392 cells). - **Docs.** `tests/runner-e2e/SUITES.md`, `doc/evals.md`, and the `/swarm` section, ledger row and live recipe in `doc/plans/2026-11-22-antigravity-qualification.md`. ## Verification - `pnpm --filter @paperclipai/server exec vitest run src/services/antigravity-swarm.test.ts`: 2 passed (red first: module missing; Plan mode red then green). - `pnpm --filter @paperclipai/server exec vitest run src/__tests__/issue-comment-reopen-routes.test.ts -t swarm`: 3 passed; 2 red against the route without this change. - `pnpm --filter @paperclipai/ui exec vitest run src/components/task-chat/TaskChatComposer.test.tsx`: 164 passed, including 4 new `/swarm` tests (red first). - Runner: `vitest run src/drivers/acpx/antigravity-swarm-*.test.ts`: 69 passed (route tool filter red first). - runner-e2e: `antigravity-swarm.test.ts` (red first) plus the catalog, selector and Antigravity tests: 28 files passed. - Typecheck: server, UI (except generated Storybook files absent from a worktree) and runner: no errors. - Live, real CLI 1.5.15 with the owner's sign-in, through the driver: the receipt verified (pinned identity, task folder, `auto` read back, `gemini-3.8-flash` + `low`). Google then refused the prompt with its 429 quota text; the driver reported it as a typed "out of quota" failure, retired the CLI and nothing was written. - **Not run live yet:** the full Paperclip proof (swarm to Done, Stop, kill and Retry, follow-up, flag off). This PR is merged only after it passes. Recipe: `doc/plans/2026-11-22-antigravity-qualification.md#swarm-live-proof`. ## Risks ### Security notes: the trust boundary Helpers are auto-approved and Paperclip does not sandbox the CLI on a local computer, so the boundary is who asked and what text is sent. Every input that can influence it: **(a) Whether a run is a swarm** - `nativeCommand: "swarm"` on the comment request: accepted only from a signed-in board user; the request record (`issue.antigravity_swarm_requested`, actor = that user, `commentId`) commits in the same transaction as the comment. Paperclip-written. - `antigravitySwarmCommentId` on the wake/run context: set only by the comments route and by run selection. It is a pointer, never trusted alone: selection re-verifies comment id + task + company + human author (no agent author) + a matching record by the same user. - The pending request for marker-less wakes: the task's latest human comment, verified the same way. - Retry inheritance: the run's `retryOfRunId` chain (server-written), bounded to 8 hops on the same task. - Manual vs automatic: a request's first run, or a user's own Retry request (`task_retry` / `retry_failed_run`, requested by a user, not `initiatedBy` automation) after the last attempt. Not the wake reason string. - Instance flag `enableAntigravitySwarm` (admin), the assignee's adapter config (Google Antigravity), execution target (local only), work mode (Standard only: Plan, Ask and any other mode are refused, at comment time and at run time), chat vs task: all Paperclip-owned settings. - Prior attempts on the same request (run status rows): Paperclip-written. **(b) What text and tools the swarm gets** - The prompt: `/swarm ` + `provider.swarmRequest`, which is the recorded comment body minus a leading `/swarm`, trimmed, byte for byte (tested through the production input builder and envelope with a merged wake that contains an agent comment). The verified request. - The system prompt append: only Paperclip-authored text: Paperclip's fixed execution prompt, the run's hand-in values (the server-written completion revision and criterion IDs that `paperclip_finish`/`paperclip_block` require; never a criterion's requirement text), and the fixed swarm rules. **The swarm runs without the agent's own instructions**: the instruction bundle and connection instructions are left out, because an agent with an editable instructions folder can rewrite its instructions in an earlier ordinary run, the helpers act under auto mode, and Paperclip keeps no user-approved copy of those instructions to trust instead. - Tools: only Paperclip's own MCP bridge (any other assigned tool server refuses the swarm), with delegation, hiring, routing and generic API operations removed. Paperclip's permission handler approves a CLI permission request only when its tool name is `mcp_paperclip_<tool>` and `<tool>` is one the run's bridge admits; everything else is refused, and the CLI then ends the turn. Tool descriptions are Paperclip-authored; tool results (for example other comments) are data the coordinator reads, not the request. - Model and mode: the agent's configured model, Antigravity's `auto` mode, both read back and pinned by the receipt. - Not sent: task title, description, wake comments, continuation events, attachments, queued messages, handoff text. Known residual: a Retry or recovery run that has not yet been selected (still preparing) does not carry the request marker, so an Interrupt in that short window keeps the message for the turn boundary instead of stopping the run; the message is delivered after the run ends. - Off by default and local only. With the flag on, helpers act under Antigravity's `auto` mode. **Paperclip does not sandbox them on a local computer**, and their tool calls never reach Paperclip, so helper shell commands can write outside the task folder where the CLI's own sandbox allows (workspace plus temp directories). The settings text, the activity log and the plan doc say so. - On macOS the CLI uses its own Google sign-in from the login keychain, not Paperclip's stored connection. - Google's consumer terms have a clause about third-party tools that access the service. A legal read is pending; this PR does not claim compliance. - `/swarm` as the first word of a composer message is now a swarm request for every agent; for agents other than Google Antigravity the server refuses it with a plain message. To send the literal text, put other text first. - The new runner-e2e cells are attended only and have no automated executor. > For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and discuss it in `#dev` before opening the PR. Feature PRs that overlap with planned core work may need to be redirected — check the roadmap first. See `CONTRIBUTING.md`. Review round 2 (heada2decc65f3): Greptile's "swarms must guess completion fields" fixed as above (runner `nativeSystemInstructions`, red first 1 in the fake-CLI swarm runtime test; request text to the CLI still exactly `/swarm <request>`); CI typecheck failure was a TS18046 in `acpx-runtime-sidecar.test.ts` (test-program typecheck), fixed; master merged, its pruned `heartbeat-provider-limit-wait` and `provider-failure-classification` suites dropped, with this branch's swarm cases kept in `antigravity-swarm-provider-limit.test.ts` and `antigravity-swarm.test.ts`. No protocol schema change. Round 3 (head1444503e84, from live run6): CLI 1.5.15 names a client MCP call `mcp_<server>_<tool>` with no `_meta` (payload recorded from the pinned CLI, now the fake CLI's fixture), so Paperclip's permission handler refused the coordinator's own `get_task_context` and the CLI ended the turn on the denial. The handler now approves only tools the run's Paperclip bridge admits, by that name, and fails closed otherwise; a refused Paperclip tool is reported as such, not as a helper blocked outside the task folder. A swarm turn without a hand-in fails plainly (`ANTIGRAVITY_SWARM_NO_RESULT`: "The swarm ended without doing any work"), every swarm-route failure code settles as `antigravity_swarm_unavailable` (no automatic retry; Retry offered), and selection never starts another swarm automatically after one; a lost CLI process still recovers on its own. The run history always records whether the CLI's sign-in matches the connection account (with the reason when unknown). Usage-limit evidence is scoped by the executed provider mode, so limits from pre-scope swarms hold only swarms. Red first: runtime 5, server 3. Then (head74454a17f3): an empty `/swarm` body now gets the documented 422 `antigravity_swarm_unavailable` instead of a generic 400 (`addIssueSwarmCommentSchema`, route red first 1); an empty ordinary comment keeps its 400. Review round (earlier head): - `pnpm --filter @paperclipai/server exec vitest run` on the swarm, request-path, native-execution-input, first-turn, queued-comments and comment-route tests: 475 passed. Red first: request path 1, execution input 3, empty/over-long DB 3 and route 2, Interrupt during a swarm 2, Retry proof 2. - Runner `src/drivers/acpx src/contracts`: passed; swarm runtime red first 6 (request field, verbatim text, empty, over-long, no field, request only in the rendered prompt). - UI `TaskChatComposer.test.tsx`: over-long `/swarm` refused (red first 1). - Typecheck: server (layers), UI and runner: no errors. ## Model Used - Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, with tool use (shell, file edit) in Claude Code. ## 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 - [ ] 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>
1002 lines
78 KiB
TypeScript
1002 lines
78 KiB
TypeScript
import { ENVIRONMENT_PROVIDER_WAIT_KEY, ENVIRONMENT_PROVIDER_WAIT_RETRY_REASON, environmentProviderWaitMessage } from "./environment-provider-wait.js";
|
||
import { ANTIGRAVITY_SWARM_UNAVAILABLE } from "./antigravity-swarm-refusal.js";
|
||
import { STORAGE_UNAVAILABLE_WAIT_RETRY_REASON } from "../storage/storage-unavailable.js";
|
||
import { AGENT_DIRECTORY_WAIT_RETRY_REASON, INTERRUPTED_PREPARATION_RETRY_REASON } from "./execution-recovery-attempt.js";
|
||
import { boundSetupCommandFailure, readSetupCommandFailure } from "./workspace-setup-command-failure.js";
|
||
import { and, desc, eq, inArray, isNull, not, notInArray, or, sql, type SQL } from "drizzle-orm";
|
||
import { unidentifiedLocalLaunchRefusals } from "./native-local-process-stop.js";
|
||
import { nativeSessionCleanupBlock } from "../vendor/paperclip-runner/index.js";
|
||
import { agents, agentWakeupRequests, approvals, chatActions, companies, environmentLeases, heartbeatRuns, issueApprovals, issueRecoveryActions,
|
||
issues, issueThreadInteractions, issueTreeHoldMembers, issueTreeHolds, nativeRunFinalizations, projects, statusDecisions, type Db } from "@paperclipai/db";
|
||
import { NATIVE_AGENT_BLOCKER_HANDOFF_CODE } from "./native-runtime/native-agent-blocker.js";
|
||
import { unionAll } from "drizzle-orm/pg-core";
|
||
import { EXECUTION_RECONCILIATION_CAUSES, HEARTBEAT_RUN_STATUSES, readProviderFailure, type TaskInterruptionPresentation } from "@paperclipai/shared";
|
||
import { captureTaskInterruptionFailure } from "../sentry.js";
|
||
import { isAiAuthenticationBlocked } from "./ai-auth-failure.js";
|
||
import { isProviderQuotaActionRequired } from "./provider-quota-reset.js";
|
||
import { readSystemRecovery } from "./system-recovery-state.js";
|
||
import { PROJECT_ACCESS_DENIED } from "./project-access-denial.js";
|
||
import { authorizationService } from "./authorization.js";
|
||
import { activeIssueInteractionCondition } from "./issue-question-context.js";
|
||
import { taskWorkspaceProjectRequirements } from "./task-workspace-access.js";
|
||
import { isAcknowledgedNativeStop } from "./acknowledged-native-stop.js";
|
||
import { createRunSecretRedactionRegistry } from "./run-secret-redaction.js";
|
||
import { instanceSettingsService } from "./instance-settings.js";
|
||
import { redactCurrentUserText } from "../log-redaction.js";
|
||
import { logger } from "../middleware/logger.js";
|
||
import { canonicalExecutionUuid } from "./execution-identity.js";
|
||
import { chatActionDeadline } from "./chat-action-work.js";
|
||
import { NATIVE_CLEANUP_SETTLING_MESSAGE, NATIVE_SESSION_CLEANUP_QUARANTINED } from "./native-runtime/completed-turn-cleanup.js";
|
||
import { isAcknowledgedLegacyUserStop, isQueuedDependencyWait, runStopPresentationJson } from "./run-cancellation.js";
|
||
import { heldRetryOffersRetry, liveExecutionOwnerCondition, taskAttemptCondition } from "./task-retry-eligibility.js";
|
||
import { exhaustedNativeFinalizationCondition, savedResultRetryPendingCondition } from "./native-runtime/exhausted-finalization-retry.js";
|
||
import { AI_CONNECTION_BUSY_RETRY_REASON, AI_CONNECTION_POOL_WAIT_RETRY_REASON, RUNNER_REVISION_WAIT_RETRY_REASON,
|
||
WORKSPACE_BUSY_RETRY_REASON } from "../modules/run-dispatch/index.js";
|
||
|
||
// These scheduled runs wait for a shared resource; none records a failed
|
||
// attempt. Their dispatcher re-checks the resource, so the run can stay
|
||
// scheduled past its retry time. Retry cannot free the resource either.
|
||
const resourceWaitRetryReasons = new Set<string>([WORKSPACE_BUSY_RETRY_REASON, AI_CONNECTION_BUSY_RETRY_REASON,
|
||
AI_CONNECTION_POOL_WAIT_RETRY_REASON, RUNNER_REVISION_WAIT_RETRY_REASON, STORAGE_UNAVAILABLE_WAIT_RETRY_REASON,
|
||
AGENT_DIRECTORY_WAIT_RETRY_REASON]);
|
||
|
||
// A due same-run resume dispatches within seconds; past this, offer Retry.
|
||
const NATIVE_RESUME_DISPATCH_GRACE_MS = 2 * 60_000;
|
||
// A queued-message Interrupt waits for the stopped run's cleanup before its
|
||
// successor starts. Past this, a hand-off that never started offers Retry.
|
||
const QUEUED_INTERRUPT_HANDOFF_GRACE_MS = 5 * 60_000;
|
||
const QUEUED_INPUT_DISPATCH_GRACE_MS = 2 * 60_000;
|
||
const AUTOMATIC_DISPOSITION_GRACE_MS = 2 * 60_000;
|
||
const WAITING_CONTINUATION_MOVE_FORWARD_MS = 15 * 60_000;
|
||
const NO_WORK_CONTINUATION_HANDOFF_GRACE_MS = 30_000;
|
||
const NO_WORK_CONTINUATION_CAUSES = new Set(["native_predecessor_retirement_unproven", "native_failed_before_provider_turn"]);
|
||
|
||
function awaitsExactRemoteResume(issue: Pick<typeof issues.$inferSelect, "status" | "executionRunId">,
|
||
run: Pick<typeof heartbeatRuns.$inferSelect, "id" | "processPid" | "processGroupId">,
|
||
coordinator: Pick<typeof nativeRunFinalizations.$inferSelect, "failureDetail">): boolean {
|
||
const detail = coordinator.failureDetail as Record<string, unknown> | null;
|
||
return issue.status === "blocked" && issue.executionRunId === run.id
|
||
&& (run.processPid !== null || run.processGroupId !== null) && detail?.recoveryMode === "exact_checkpoint_resume";
|
||
}
|
||
|
||
type ProjectionIssue = Pick<typeof issues.$inferSelect, "id" | "companyId" | "status" | "assigneeAgentId" | "projectId">;
|
||
type ProjectionRun = Pick<typeof heartbeatRuns.$inferSelect,
|
||
"id" | "status" | "scheduledRetryAt" | "errorCode" | "livenessState" | "resultJson"> &
|
||
Partial<Pick<typeof heartbeatRuns.$inferSelect, "agentId" | "error" | "companyId" | "nativeIssueId" | "runtimeMode" | "startedAt"
|
||
| "scheduledRetryReason" | "createdAt" | "finishedAt" | "processPid" | "processGroupId">> & {
|
||
environmentProviderWaitName?: string | null;
|
||
/** Due time of the native coordinator's own same-run resume, if one is pending. */
|
||
nativeResumeAt?: Date | null;
|
||
/** When the native coordinator saved this run's terminal failure, if it did. */
|
||
nativeFailedAt?: Date | null;
|
||
/** When native recovery of this run's provider failure last moved, while that recovery is still undecided. */
|
||
providerRecoveryAt?: Date | null;
|
||
/** A succeeded native run whose accepted result ran out of finalization retries; Retry applies it again. */
|
||
nativeFinalizationRetry?: boolean;
|
||
/** A saved-result Retry of this succeeded run is still being applied. */
|
||
nativeFinalizationRetryPending?: boolean;
|
||
/** The configured model id the provider rejected (native_provider_model_rejected only). */
|
||
rejectedModel?: string | null;
|
||
/** A cleanup-gate refusal after this run's own provider turn completed; its work is saved. */
|
||
completedTurnCleanup?: boolean;
|
||
};
|
||
type ProjectionWait = { id: string; requestedAt: Date; sourceRunId?: string | null; executionWaitReason?: string | null;
|
||
preparationStatus?: string | null; preparationRetryAt?: number | string | null; preparationUpdatedAt?: Date | null;
|
||
/** When the user pressed Interrupt to deliver this queued message now. */
|
||
queuedInterruptRequestedAt?: string | null;
|
||
queuedComment?: boolean };
|
||
type ProjectionState = {
|
||
run?: ProjectionRun;
|
||
action?: typeof issueRecoveryActions.$inferSelect;
|
||
wait?: ProjectionWait;
|
||
retryWait?: ProjectionWait;
|
||
/** The pending queued message this run was interrupted for, if any. */
|
||
interruptWait?: ProjectionWait;
|
||
agent?: Pick<typeof agents.$inferSelect, "status" | "pauseReason">;
|
||
projectAccessAllowed?: boolean;
|
||
projectAccessProjectId?: string | null;
|
||
workspaceSelectionAvailable?: boolean;
|
||
project?: Pick<typeof projects.$inferSelect, "pausedAt" | "pauseReason">;
|
||
company?: Pick<typeof companies.$inferSelect, "status" | "pauseReason">;
|
||
paused: boolean;
|
||
approval: boolean;
|
||
question?: boolean;
|
||
/** A board-owned workspace-owner fence on this run that no automatic proof has released. */
|
||
ownerStopRequired?: boolean;
|
||
unidentifiedLaunchConfirmable?: boolean;
|
||
/** The latest wake for a task with no run yet, when admission refused it. */
|
||
admissionWait?: { id: string; status: string; reason: string | null; agentId: string };
|
||
retryDependencies?: RetryDependencies;
|
||
/** The task waits on unresolved blocker tasks, a wait that does not depend on its last run. */
|
||
waitingOnBlockers?: boolean;
|
||
waitingOnOpenSubtasks?: boolean;
|
||
/** A live owner is settling this run's session cleanup gate: an unreleased
|
||
* remote environment, or a scheduled close in this process. Without one,
|
||
* nothing settles the gate by itself, so Retry is the way forward. */
|
||
cleanupSettling?: boolean;
|
||
/** The fresh-continuation fallback is continuing this run's task (recorded,
|
||
* not yet started) or will check it again at `nextCheckAt`. */
|
||
freshContinuation?: { state: "continuing"; pendingSince?: number } | { state: "scheduled"; nextCheckAt: number };
|
||
automaticDispositionAt?: number;
|
||
waitingContinuation?: { queuedAt: number; personRequested: boolean };
|
||
recoveryHandedToPerson?: boolean;
|
||
agentBlockerNeedsPerson?: boolean;
|
||
};
|
||
|
||
// A task read reports the change that made a task fail, not every later read of
|
||
// retained history. The delivery cache is process-local, so a restart or another
|
||
// replica would otherwise resend each old failure whenever a list shows it.
|
||
const TASK_INTERRUPTION_REPORT_WINDOW_MS = 60 * 60_000;
|
||
|
||
/** Latest saved fact that can move a task into the failed state, if known. The
|
||
* recovery row's update time covers recovery that newly fails on an old run:
|
||
* each saved recovery transition stamps it, while activity heartbeats do not.
|
||
* The native resume due time and terminal finalization cover a same-run resume
|
||
* that lapses or is exhausted on an old run. */
|
||
export function taskInterruptionChangedAt(input: Pick<ProjectionState, "run" | "action" | "wait" | "retryWait">): number | null {
|
||
const { run, wait, action } = input;
|
||
const retryWait = input.retryWait ?? wait;
|
||
const preparationFailedAt = retryWait?.preparationStatus === "failed" || retryWait?.preparationStatus === "cancelled"
|
||
? retryWait.preparationUpdatedAt : null;
|
||
const times: unknown[] = [run?.finishedAt ?? run?.createdAt, run?.scheduledRetryAt, run?.nativeResumeAt, run?.nativeFailedAt,
|
||
wait?.requestedAt, retryWait?.requestedAt, action?.updatedAt, preparationFailedAt].map(value => value?.getTime());
|
||
times.push(readSystemRecovery(action?.evidence)?.nextAttemptAt,
|
||
retryWait?.preparationRetryAt == null ? null : Number(retryWait.preparationRetryAt));
|
||
const known = times.filter((time): time is number => typeof time === "number" && Number.isFinite(time));
|
||
return known.length ? Math.max(...known) : null;
|
||
}
|
||
|
||
function boundedJsonString(value: SQL, limit: number) {
|
||
return sql<string | null>`case when jsonb_typeof(${value}) = 'string' then left(${value} #>> '{}', ${limit}) else null end`;
|
||
}
|
||
|
||
/** Projected reason for a budget.blocked refusal caused by unpriced usage. */
|
||
export const UNPRICED_USAGE_WAIT = "accounting.unpriced_usage";
|
||
/** Admission's unpriced-usage refusal text (budgets.ts). Only this fixed
|
||
* phrase is matched; the stored message itself is never displayed. */
|
||
const unpricedUsageWait = (reason: SQL, message: SQL) =>
|
||
sql<string | null>`case when ${reason} = 'budget.blocked' and ${message} like '%recorded usage has no reliable price%'
|
||
then ${UNPRICED_USAGE_WAIT} else ${reason} end`;
|
||
|
||
// Persisted wait messages can contain diagnostics. Ordinary task views expose
|
||
// only these stable categories, never arbitrary payloads or source identifiers.
|
||
const retryWaitMessages: Record<string, string> = {
|
||
workspace_repair_required: "Retry is waiting for Paperclip to recover the saved files. Your message is saved.",
|
||
remote_cleanup: "Retry is waiting for the previous environment to stop. Your message is saved.",
|
||
local_cleanup: "Retry is waiting for the previous execution to finish cleanup. Your message is saved.",
|
||
cleanup_pending: "Retry is waiting for the previous execution to finish cleanup. Your message is saved.",
|
||
execution_settling: "Retry is waiting for the previous execution to stop. Your message is saved.",
|
||
controller_settling: "Retry is waiting for the previous execution to finish recovery. Your message is saved.",
|
||
process_identity_missing: "Retry is waiting for Paperclip to verify that the previous execution stopped. Your message is saved.",
|
||
process_running: "Retry is waiting for the previous execution to stop. Your message is saved.",
|
||
execution_active: "Retry is waiting for the current run to finish. Your message is saved.",
|
||
decision_pending: "Retry is waiting for a pending approval or question to be resolved. Your message is saved.",
|
||
"budget.blocked": "Retry is waiting for the budget to allow new work. Check Costs for unrecorded or unpriced usage.",
|
||
// Admission records an unpriced-usage refusal as budget.blocked; the
|
||
// projection shows it as the accounting matter it is.
|
||
[UNPRICED_USAGE_WAIT]: "Retry is waiting because recorded usage has no price. Check Costs, or allow unpriced usage in the budget policy. Your message is saved.",
|
||
finalizing_saved_result: "Paperclip is finishing the saved result before Retry can continue.",
|
||
// Another click cannot release a copyback owner that stopped without reporting.
|
||
// Retry starts a new recovery generation; the agent does not repeat its work.
|
||
saved_work_recovery_failed: "Saving the agent's files failed. Retry to try again; the agent will not repeat its work.",
|
||
saved_work_recovery_refused: "Paperclip refused to recover the saved files because a safety check on them cannot pass. "
|
||
+ "Retry checks the saved files again. The agent's result is saved, and the agent will not repeat its work.",
|
||
workspace_owner_stop_required: "Retry is waiting until Paperclip confirms that the previous server stopped saving files. The agent's result and your Retry are saved.",
|
||
cleanup_quarantined: NATIVE_CLEANUP_SETTLING_MESSAGE,
|
||
cancelled_by_operator: "This attempt was stopped. Send a new message to continue from saved work.",
|
||
// A new click replaces a Retry declined for its requester's access with a
|
||
// fresh attempt under the clicker's access, so Retry stays available.
|
||
retry_authority_denied: "Retry could not start with the requesting user's access. Press Retry to start it with yours.",
|
||
previous_execution_unverified: "Retry can't continue because Paperclip can't confirm that the previous attempt stopped before it started work. Your message is saved.",
|
||
// An edited saved message can never be replayed; only new input continues.
|
||
retry_source_changed: "Retry cannot continue because its saved message changed. Send a new message to continue from saved work.",
|
||
};
|
||
|
||
/** The stable text for a Retry admission refusal, as the task view shows it. */
|
||
export function taskRetryWaitMessage(reason: string): string {
|
||
return retryWaitMessages[reason] ?? "Retry is waiting for execution checks. Your message is saved.";
|
||
}
|
||
|
||
const UNIDENTIFIED_RUNNER_STOP_MESSAGE = "Paperclip can't confirm that the previous run stopped. The server restarted after it started the agent but before it recorded the agent's process. "
|
||
+ "If no agent is still working on this task, a board operator can confirm that it stopped, and your Retry starts right away. Your message is saved.";
|
||
|
||
type RetryDependencies = { count: number; blocked: boolean; cancelled: boolean; savingFilesOnly: boolean };
|
||
|
||
function dependencyRetryWaitMessage(dependencies: RetryDependencies): string {
|
||
if (dependencies.cancelled) return "Retry is waiting on a cancelled task this one depends on. Remove or replace that dependency to continue. Your Retry is saved.";
|
||
if (dependencies.blocked) return "Retry is waiting on a blocked task this one depends on. Retry or unblock it; this task continues when it is done. Your Retry is saved.";
|
||
if (dependencies.savingFilesOnly) return "Retry is waiting for a finished task this one depends on to save its files. Your Retry is saved.";
|
||
return "Retry is waiting for the tasks this one depends on to finish. Your Retry is saved.";
|
||
}
|
||
|
||
// A task that never started can wait for a temporary admission hold. The
|
||
// assigned-task liveness sweep admits it once the hold clears; no Retry needed.
|
||
const admissionWaitMessages: Record<string, string> = {
|
||
"budget.blocked": "Waiting for the budget to allow new work. The task starts automatically when it does.",
|
||
[UNPRICED_USAGE_WAIT]: "Waiting because recorded usage has no price. Check Costs, or allow unpriced usage in the budget policy.",
|
||
};
|
||
|
||
/** Recognize old persisted failures without displaying arbitrary run error text. */
|
||
function deniedProjectId(issue: ProjectionIssue, run?: ProjectionRun): string | null | undefined {
|
||
const historical = run?.errorCode === "setup_failed" && run.error === "Project is outside this low-trust boundary.";
|
||
if (!run || run.status !== "failed" || run.agentId !== issue.assigneeAgentId
|
||
|| (run.errorCode !== PROJECT_ACCESS_DENIED && !historical)) return undefined;
|
||
const detail = run.resultJson?.projectAccessDenied;
|
||
return detail && typeof detail === "object" && "projectId" in detail && typeof detail.projectId === "string"
|
||
? detail.projectId : null;
|
||
}
|
||
|
||
/** Presentation never grants authority. Dispatch repeats all gates under its
|
||
* locks. The same projection is used in detail, lists and attention surfaces.
|
||
* A native run refused by the cleanup gate offers no Retry only while a live
|
||
* owner settles that gate. Without one, the task Retry (explicit continuation)
|
||
* proves the old session stopped and continues in a fresh session. */
|
||
export function projectTaskInterruption(issue: ProjectionIssue, input: ProjectionState, now = Date.now()): TaskInterruptionPresentation | null {
|
||
const presentation = projectTaskInterruptionState(issue, input, now);
|
||
if (input.question && !["done", "cancelled"].includes(issue.status) && !["running", "queued"].includes(input.run?.status ?? "")
|
||
&& presentation?.responsibility !== "user")
|
||
return { intentId: input.wait?.id ?? null, responsibility: "user", state: "waiting_for_user", userAction: "question",
|
||
canRetry: presentation?.canRetry === true && presentation.retryAction !== "move_forward" && !input.wait && !input.retryWait,
|
||
nextRetryAt: null, sourceRunIds: input.run ? [input.run.id] : [], observedRunId: input.run?.id ?? null };
|
||
if (!presentation?.canRetry || input.run?.errorCode !== NATIVE_SESSION_CLEANUP_QUARANTINED || !input.cleanupSettling)
|
||
return presentation;
|
||
return { ...presentation, canRetry: false };
|
||
}
|
||
|
||
function projectTaskInterruptionState(issue: ProjectionIssue, input: ProjectionState, now: number): TaskInterruptionPresentation | null {
|
||
const { run, wait, action, agent, project, company } = input;
|
||
const retryWait = input.retryWait ?? wait;
|
||
if (!run) {
|
||
const admission = input.admissionWait;
|
||
if (!admission || admission.status !== "skipped" || !Object.hasOwn(admissionWaitMessages, admission.reason ?? "")
|
||
|| issue.status !== "todo" || admission.agentId !== issue.assigneeAgentId) return null;
|
||
const waiting: TaskInterruptionPresentation = { intentId: admission.id, responsibility: "system", state: "working",
|
||
nextRetryAt: null, canRetry: false, userAction: null, sourceRunIds: [], observedRunId: null };
|
||
// Budget pauses, ordinary pauses and approvals wait on a person; only an
|
||
// accounting hold settles by itself. Admission records all of them as budget.blocked.
|
||
const userAction = !issue.assigneeAgentId ? "assign_agent" : input.approval ? "approval"
|
||
: [agent?.pauseReason, project?.pauseReason, company?.pauseReason].some(reason => reason === "budget" || reason === "budget_exceeded")
|
||
? "budget" : input.paused || project?.pausedAt || agent?.status === "paused" || company?.status === "paused" ? "resume" : null;
|
||
if (userAction) return { ...waiting, responsibility: "user", state: "waiting_for_user", userAction };
|
||
return { ...waiting, retryWait: { reason: admission.reason!, message: admissionWaitMessages[admission.reason!] } };
|
||
}
|
||
const system = readSystemRecovery(action?.evidence);
|
||
const base: TaskInterruptionPresentation = { intentId: wait?.id ?? null, responsibility: "system",
|
||
state: "resolved", nextRetryAt: null, canRetry: false, userAction: null,
|
||
sourceRunIds: [run.id], observedRunId: run.id };
|
||
if (["done", "cancelled"].includes(issue.status)) return base;
|
||
// Retry took effect: the saved result is being applied again.
|
||
if (run.status === "succeeded" && run.nativeFinalizationRetryPending)
|
||
return { ...base, state: "working", recoveryStep: "finishing_recovery" };
|
||
if (run.status === "succeeded" && !wait && !run.nativeFinalizationRetry) return issue.status === "blocked" && input.agentBlockerNeedsPerson
|
||
? { ...base, responsibility: "user", state: "waiting_for_user", canRetry: true, userAction: "unblock" } : base;
|
||
const providerFailure = run.status === "failed" || run.status === "scheduled_retry"
|
||
? readProviderFailure(run.resultJson?.providerFailure) : null;
|
||
if (providerFailure) base.providerFailure = providerFailure;
|
||
if (["failed", "scheduled_retry"].includes(run.status) && run.errorCode === "workspace_validation_failed"
|
||
&& typeof run.error === "string" && run.error.trim()) base.workspaceFailure = { message: run.error };
|
||
// A scheduled retry carries its setup_failed predecessor's cause; it has no error code of its own.
|
||
const setupFailure = run.status === "failed" && run.errorCode === "setup_failed" ? readSetupCommandFailure(run.resultJson, run.error)
|
||
: run.status === "scheduled_retry" ? readSetupCommandFailure(run.resultJson, null) : null;
|
||
if (setupFailure) base.setupFailure = setupFailure;
|
||
const userAction = !issue.assigneeAgentId ? "assign_agent" : input.approval || run.errorCode === "native_provider_approval_required" ? "approval"
|
||
: isAiAuthenticationBlocked(run) ? "reconnect"
|
||
: run.errorCode === "native_provider_model_rejected" ? "choose_model"
|
||
: [agent?.pauseReason, project?.pauseReason, company?.pauseReason].some(reason => reason === "budget" || reason === "budget_exceeded")
|
||
? "budget" : input.paused || project?.pausedAt || agent?.status === "paused" || company?.status === "paused" ? "resume" : null;
|
||
if (userAction) return { ...base, responsibility: "user", state: "waiting_for_user", userAction,
|
||
...(userAction === "choose_model" && run.rejectedModel ? { rejectedModel: run.rejectedModel } : {}) };
|
||
// The previous controller cannot confirm its own stop. Name the one board
|
||
// action instead of showing the fenced run as working (RC-47).
|
||
if (input.ownerStopRequired) return { ...base, responsibility: "user", state: "waiting_for_user", userAction: "confirm_owner_stop" };
|
||
// The task's existing dependency UI owns this wait. Keep actual recovery and
|
||
// Retry preparation failures authoritative; this does not change admission.
|
||
if (!action && !retryWait?.preparationStatus && isQueuedDependencyWait(run)) return base;
|
||
if (!wait && !action && isAcknowledgedLegacyUserStop(run)) return base;
|
||
// Intentional Stop is not an unsuccessful attempt. This is presentation only;
|
||
// physical containment and pending cleanup keep their execution-side gates.
|
||
if (!wait && run.runtimeMode === "native" && run.companyId === issue.companyId && run.nativeIssueId === issue.id
|
||
&& isAcknowledgedNativeStop({ ...run, companyId: run.companyId, nativeIssueId: run.nativeIssueId })) return base;
|
||
// A queued-message Interrupt stops this run to deliver that exact message.
|
||
// The stop is the user's intent and the message starts next, so show the
|
||
// hand-off as working, not as a failed attempt.
|
||
// Newer queued input can follow it, so match the recorded queue, not the latest wait.
|
||
const interruptWait = input.interruptWait ?? wait;
|
||
const interruptAt = Date.parse(interruptWait?.queuedInterruptRequestedAt ?? "");
|
||
if (interruptWait && run.status === "cancelled" && run.resultJson?.queuedCommentInterruptQueueId === interruptWait.id
|
||
&& Number.isFinite(interruptAt) && now - interruptAt < QUEUED_INTERRUPT_HANDOFF_GRACE_MS
|
||
&& !(retryWait?.sourceRunId === run.id && retryWait.preparationStatus) && system?.state !== "exhausted")
|
||
return { ...base, intentId: interruptWait.id, state: "working" };
|
||
const waiting = input.waitingContinuation;
|
||
if (run.status === "queued" && waiting && !waiting.personRequested && run.agentId === issue.assigneeAgentId
|
||
&& now - waiting.queuedAt >= WAITING_CONTINUATION_MOVE_FORWARD_MS)
|
||
return { ...base, state: "working", canRetry: true, retryAction: "move_forward" };
|
||
if (["running", "queued"].includes(run.status)) return { ...base, state: "working" };
|
||
// Paperclip is continuing the task by itself: no failed attempt and no Retry.
|
||
if (input.freshContinuation?.state === "continuing") {
|
||
const pendingSince = input.freshContinuation.pendingSince;
|
||
if (pendingSince !== undefined && !input.retryWait && run.agentId === issue.assigneeAgentId
|
||
&& now - pendingSince >= WAITING_CONTINUATION_MOVE_FORWARD_MS)
|
||
return { ...base, state: "working", canRetry: true, retryAction: "move_forward" };
|
||
return { ...base, state: "working", canRetry: false };
|
||
}
|
||
if (input.freshContinuation?.state === "scheduled" && input.freshContinuation.nextCheckAt > now)
|
||
return { ...base, state: "retry_scheduled", nextRetryAt: new Date(input.freshContinuation.nextCheckAt).toISOString(), canRetry: false };
|
||
if (input.automaticDispositionAt !== undefined && ["failed", "timed_out", "interrupted", "cancelled"].includes(run.status)
|
||
&& now - Math.max(input.automaticDispositionAt, run.finishedAt?.getTime() ?? 0) < AUTOMATIC_DISPOSITION_GRACE_MS)
|
||
return { ...base, state: "working", canRetry: false };
|
||
if (run.status === "failed" && NO_WORK_CONTINUATION_CAUSES.has(String(run.resultJson?.failureCause))
|
||
&& run.resultJson?.continuationScheduling !== "refused" && run.finishedAt
|
||
&& now - run.finishedAt.getTime() < NO_WORK_CONTINUATION_HANDOFF_GRACE_MS)
|
||
return { ...base, state: "working", canRetry: false };
|
||
const deniedProject = deniedProjectId(issue, run);
|
||
let projectAccess: TaskInterruptionPresentation["projectAccess"];
|
||
if (deniedProject !== undefined) {
|
||
// Permissions cannot repair a stale or unavailable workspace selection.
|
||
if (input.workspaceSelectionAvailable === false) return { ...base, state: "failed" };
|
||
const ready = input.projectAccessAllowed === true;
|
||
const projectId = input.projectAccessProjectId === undefined ? deniedProject : input.projectAccessProjectId;
|
||
projectAccess = { agentId: run.agentId!, projectId: projectId === issue.projectId ? projectId : null, ready };
|
||
if (!ready) return { ...base, responsibility: "user", state: "waiting_for_user",
|
||
userAction: "project_access", projectAccess };
|
||
}
|
||
const recoveryStep: TaskInterruptionPresentation["recoveryStep"] = !system ? undefined
|
||
: system.phase === "verify_execution" ? "checking_previous_run"
|
||
: system.phase === "preserve_files" ? "saving_previous_files" : "finishing_recovery";
|
||
const activeObservation = action?.evidence.systemRecoveryActiveAt;
|
||
const recoveryRunning = system?.state === "running" && typeof activeObservation === "number"
|
||
&& activeObservation > now - 45_000;
|
||
const recoveryScheduled = Boolean(system?.nextAttemptAt && system.nextAttemptAt > now);
|
||
const preparationDeadline = retryWait?.preparationRetryAt == null ? NaN : Number(retryWait.preparationRetryAt);
|
||
// A pending Retry preparation is already a Retry; never offer another one.
|
||
const preparationPending = retryWait?.sourceRunId === run.id && Boolean(retryWait.preparationStatus)
|
||
&& ["issued", "processing"].includes(retryWait.preparationStatus!) && Number.isFinite(preparationDeadline);
|
||
if (retryWait?.sourceRunId === run.id && retryWait.preparationStatus) {
|
||
const deadline = preparationDeadline;
|
||
const pending = preparationPending;
|
||
// Saved-work recovery gates a pending preparation, so it names the wait.
|
||
// Failed or cancelled preparation stays authoritative.
|
||
if (pending && !recoveryRunning && !recoveryScheduled)
|
||
return { ...base, state: "retry_scheduled", nextRetryAt: new Date(deadline).toISOString() };
|
||
if (!pending) return { ...base, state: "failed", canRetry: retryWait.preparationStatus !== "cancelled" };
|
||
}
|
||
if (run.status === "scheduled_retry" && run.scheduledRetryReason === ENVIRONMENT_PROVIDER_WAIT_RETRY_REASON && !system)
|
||
return { ...base, state: "working", canRetry: false, retryWait: { reason: ENVIRONMENT_PROVIDER_WAIT_RETRY_REASON,
|
||
message: environmentProviderWaitMessage(run.environmentProviderWaitName ?? null) } };
|
||
if (run.status === "scheduled_retry" && resourceWaitRetryReasons.has(run.scheduledRetryReason ?? "") && !system)
|
||
return { ...base, state: "working" };
|
||
if (run.status === "scheduled_retry" && run.scheduledRetryReason === INTERRUPTED_PREPARATION_RETRY_REASON && !system
|
||
&& (!run.scheduledRetryAt || now - run.scheduledRetryAt.getTime() < NATIVE_RESUME_DISPATCH_GRACE_MS))
|
||
return { ...base, state: "working" };
|
||
// The native coordinator resumes this same run by itself. A manual Retry
|
||
// would race it, so show the resume until it is due and briefly while its
|
||
// dispatch runs. A resume that never ran falls through to one Retry.
|
||
const nativeResumeAt = run.nativeResumeAt?.getTime();
|
||
const retryAt = system?.nextAttemptAt ?? nativeResumeAt ?? run.scheduledRetryAt?.getTime();
|
||
if (run.runtimeMode === "native" && run.errorCode === "native_governed_wait_settlement_pending") {
|
||
const settling = recoveryRunning || Boolean(retryAt && now - retryAt < NATIVE_RESUME_DISPATCH_GRACE_MS);
|
||
return { ...base, state: settling ? "working" : "failed", canRetry: false,
|
||
retryWait: { reason: "governed_wait_settlement", message: settling
|
||
? "Your work is saved. Paperclip is finishing shutdown."
|
||
: "Paperclip couldn’t confirm shutdown. Your work is saved; continuation is paused." } };
|
||
}
|
||
if (retryAt && retryAt > now) return { ...base, state: "retry_scheduled",
|
||
nextRetryAt: new Date(retryAt).toISOString(), canRetry: !preparationPending && !nativeResumeAt && run.status !== "scheduled_retry",
|
||
...(system?.nextAttemptAt && recoveryStep ? { recoveryStep } : {}) };
|
||
const dispatchDueAt = nativeResumeAt ?? (run.status === "scheduled_retry" ? run.scheduledRetryAt?.getTime() : undefined);
|
||
if (dispatchDueAt && !system && now - dispatchDueAt < NATIVE_RESUME_DISPATCH_GRACE_MS)
|
||
return { ...base, state: "working" };
|
||
if (recoveryRunning) return { ...base, state: "working", ...(recoveryStep ? { recoveryStep } : {}) };
|
||
const providerRecoveryAt = run.providerRecoveryAt?.getTime();
|
||
if (providerRecoveryAt !== undefined && run.status === "failed" && !input.recoveryHandedToPerson && system?.state !== "exhausted"
|
||
&& now - Math.max(providerRecoveryAt, run.finishedAt?.getTime() ?? 0, action?.updatedAt.getTime() ?? 0) < AUTOMATIC_DISPOSITION_GRACE_MS)
|
||
return { ...base, state: "working", ...(recoveryStep ? { recoveryStep } : {}) };
|
||
const dependencies = input.retryDependencies;
|
||
if (retryWait?.sourceRunId === run.id && !retryWait.executionWaitReason && !retryWait.preparationStatus && dependencies?.count)
|
||
return { ...base, intentId: retryWait.id, state: "working",
|
||
retryWait: { reason: "dependency_blocked", message: dependencyRetryWaitMessage(dependencies) } };
|
||
if (retryWait?.sourceRunId === run.id && retryWait.executionWaitReason === "process_identity_missing"
|
||
&& input.unidentifiedLaunchConfirmable)
|
||
return { ...base, responsibility: "user", state: "waiting_for_user", canRetry: false, userAction: "confirm_run_stopped",
|
||
retryWait: { reason: "process_identity_missing", message: UNIDENTIFIED_RUNNER_STOP_MESSAGE } };
|
||
if (retryWait?.sourceRunId === run.id && retryWait.executionWaitReason) {
|
||
const reason = Object.hasOwn(retryWaitMessages, retryWait.executionWaitReason) ? retryWait.executionWaitReason : "execution_recovery";
|
||
// Keep the latest input receipt so message submission can settle independently.
|
||
return { ...base, state: "failed",
|
||
canRetry: heldRetryOffersRetry(reason, input.cleanupSettling), retryWait: { reason,
|
||
message: retryWaitMessages[reason] ?? "Retry is waiting for execution checks. Your message is saved." } };
|
||
}
|
||
const queuedDuringRun = Boolean(wait && run.status === "succeeded" && (["todo", "in_progress", "in_review"].includes(issue.status)
|
||
|| (issue.status === "blocked" && input.waitingOnOpenSubtasks && !wait.sourceRunId && wait.queuedComment)) && run.finishedAt && wait.requestedAt.getTime() <= run.finishedAt.getTime()
|
||
&& now - run.finishedAt.getTime() < QUEUED_INPUT_DISPATCH_GRACE_MS);
|
||
if (wait && (wait.requestedAt.getTime() > now - 10_000 || queuedDuringRun) && system?.state !== "exhausted")
|
||
return { ...base, state: "working" };
|
||
if (run.errorCode === NATIVE_SESSION_CLEANUP_QUARANTINED) {
|
||
// The provider turn completed and its work is saved; only shutdown of the
|
||
// old session is unproven, and that settles by itself. A task that waits on
|
||
// its blockers shows that wait, not a failed attempt. Without such a wait a
|
||
// missing task disposition stays visible.
|
||
if (run.completedTurnCleanup && input.waitingOnBlockers) return base;
|
||
// Waiting is honest only while something settles the gate. Otherwise
|
||
// Retry proves the old session stopped and continues from saved work.
|
||
if (!input.cleanupSettling) return { ...base, state: "failed", canRetry: true };
|
||
return { ...base, state: "failed", canRetry: false,
|
||
retryWait: { reason: "cleanup_quarantined", message: NATIVE_CLEANUP_SETTLING_MESSAGE } };
|
||
}
|
||
if (run.status === "failed" && isProviderQuotaActionRequired(run, new Date(now)))
|
||
return { ...base, responsibility: "user", state: "waiting_for_user", userAction: "provider_billing", canRetry: true };
|
||
// A /swarm the swarm route refused for a reason the person must change (the
|
||
// model, a planning run, extra tool servers): Retry would meet the same
|
||
// refusal, so the plain message says what to do and no Retry is offered.
|
||
if (run.status === "failed" && run.errorCode === ANTIGRAVITY_SWARM_UNAVAILABLE
|
||
&& providerFailure?.provider === "antigravity" && providerFailure.retryability === "requires_action")
|
||
return { ...base, state: "failed", canRetry: false };
|
||
return { ...base, state: "failed", canRetry: true, ...(projectAccess ? { projectAccess } : {}) };
|
||
}
|
||
|
||
/** Tasks whose cleanup-gated run still has a live owner settling the gate: a
|
||
* remote environment not yet released (its termination receipt retires the
|
||
* gate), or a scheduled close in this process. An operator-required gate or a
|
||
* gate lost with a restarted process has no owner. */
|
||
async function quarantineCleanupSettling(db: Db, companyId: string,
|
||
entries: Array<{ issueId: string; run: { id: string; nativeIssueId?: string | null } }>): Promise<Set<string>> {
|
||
const settling = new Set<string>();
|
||
if (!entries.length) return settling;
|
||
const unreleased = new Set((await db.select({ runId: environmentLeases.heartbeatRunId }).from(environmentLeases).where(and(
|
||
eq(environmentLeases.companyId, companyId), inArray(environmentLeases.heartbeatRunId, entries.map(entry => entry.run.id)),
|
||
isNull(environmentLeases.releasedAt), sql`${environmentLeases.provider} is distinct from 'local'`,
|
||
))).map(row => row.runId));
|
||
for (const { issueId, run } of entries) {
|
||
if (unreleased.has(run.id) || nativeSessionCleanupBlock({ companyId, issueId: run.nativeIssueId ?? issueId }) === "pending")
|
||
settling.add(issueId);
|
||
}
|
||
return settling;
|
||
}
|
||
|
||
/** A scheduled retry has no outcome yet, so its provider failure is read from
|
||
* the predecessor it retries: the countdown keeps its reason.
|
||
* Batch list reads rather than doing seven queries per row. Keep run logs and
|
||
* large result bodies out of this projection. */
|
||
export async function taskInterruptionPresentations(db: Db, companyId: string, input: ProjectionIssue[]) {
|
||
const selected = input.filter(issue => issue.companyId === companyId);
|
||
const result = new Map<string, TaskInterruptionPresentation | null>();
|
||
if (!selected.length) return result;
|
||
const ids = selected.map(issue => issue.id);
|
||
const requested = sql`unnest(${sql.param([...new Set(ids)])}::text[]) as requested_interruption_issues(requested_issue_id)`;
|
||
const requestedIssueId = sql<string>`"requested_interruption_issues"."requested_issue_id"`;
|
||
const requestedUuid = sql<string>`case when ${requestedIssueId} ~ '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$'
|
||
then ${requestedIssueId}::uuid else null end`;
|
||
const legacyIssue = sql<string>`${heartbeatRuns.contextSnapshot}->>'issueId'`;
|
||
const candidate = (condition: SQL | undefined, executionOwner = false) => db.select({ id: heartbeatRuns.id,
|
||
createdAt: heartbeatRuns.createdAt, executionOwner: sql<number>`${executionOwner ? 1 : 0}::int`.as("execution_owner") })
|
||
.from(heartbeatRuns).where(and(eq(heartbeatRuns.companyId, companyId), condition, taskAttemptCondition()))
|
||
.orderBy(desc(heartbeatRuns.createdAt), desc(heartbeatRuns.id)).limit(1);
|
||
const nativeRun = eq(heartbeatRuns.nativeIssueId, requestedUuid);
|
||
const firstClassRun = and(isNull(heartbeatRuns.nativeIssueId), eq(heartbeatRuns.issueId, requestedUuid));
|
||
const legacyRun = and(isNull(heartbeatRuns.nativeIssueId), isNull(heartbeatRuns.issueId), eq(legacyIssue, requestedIssueId));
|
||
const candidates = unionAll(
|
||
candidate(and(liveExecutionOwnerCondition(companyId, requestedUuid), or(nativeRun, firstClassRun, legacyRun)), true),
|
||
candidate(nativeRun),
|
||
candidate(firstClassRun),
|
||
candidate(legacyRun),
|
||
).as("interruption_run_candidates");
|
||
const latestRuns = db.select({ id: candidates.id }).from(candidates)
|
||
.orderBy(desc(candidates.executionOwner), desc(candidates.createdAt), desc(candidates.id)).limit(1).as("latest_interruption_runs");
|
||
const wakeIssue = sql<string>`${agentWakeupRequests.payload}->>'issueId'`;
|
||
const isRetryWait = sql<boolean>`coalesce(jsonb_typeof(${agentWakeupRequests.payload}#>'{taskRetry,sourceRunId}') = 'string', false)`;
|
||
const agentIds = selected.flatMap(issue => issue.assigneeAgentId ? [issue.assigneeAgentId] : []);
|
||
const projectIds = selected.flatMap(issue => issue.projectId ? [issue.projectId] : []);
|
||
const startingIssueIds = selected.filter(issue => issue.status === "todo" && issue.assigneeAgentId).map(issue => issue.id);
|
||
const [runs, waits, agentRows, projectRows, pauseRows, gateRows, companyRows, admissionWaits] = await Promise.all([
|
||
db.select({ issueId: requestedIssueId, id: heartbeatRuns.id, status: heartbeatRuns.status,
|
||
createdAt: heartbeatRuns.createdAt, finishedAt: heartbeatRuns.finishedAt, agentId: heartbeatRuns.agentId, companyId: heartbeatRuns.companyId,
|
||
nativeIssueId: heartbeatRuns.nativeIssueId, runtimeMode: heartbeatRuns.runtimeMode, startedAt: heartbeatRuns.startedAt,
|
||
processPid: heartbeatRuns.processPid, processGroupId: heartbeatRuns.processGroupId,
|
||
agentAdapter: agents.adapterType,
|
||
error: sql<string | null>`case when ${heartbeatRuns.errorCode} = 'setup_failed'
|
||
and ${heartbeatRuns.error} = 'Project is outside this low-trust boundary.'
|
||
or ${heartbeatRuns.errorCode} = 'workspace_validation_failed'
|
||
-- A setup command failure recorded before setupCommandFailure existed: only its fixed message shape.
|
||
or ${heartbeatRuns.errorCode} = 'setup_failed'
|
||
and ${heartbeatRuns.error} ~ '^(Execution workspace provision|Workspace seed|Runtime provision) command "'
|
||
then ${heartbeatRuns.error} else null end`,
|
||
errorCode: heartbeatRuns.errorCode, livenessState: heartbeatRuns.livenessState, scheduledRetryAt: heartbeatRuns.scheduledRetryAt,
|
||
// The model this run asked the provider for; configuration, not provider text.
|
||
// Saved fields only, so quarantines written by earlier releases read the same.
|
||
completedTurnCleanup: sql<boolean>`coalesce(${heartbeatRuns.runtimeMode} = 'native' and ${heartbeatRuns.status} = 'failed'
|
||
and ${heartbeatRuns.errorCode} = ${NATIVE_SESSION_CLEANUP_QUARANTINED}
|
||
and ${heartbeatRuns.runnerProfileJson}#>>'{sessionCheckpoint,identity,runId}' = ${heartbeatRuns.id}::text
|
||
and ${heartbeatRuns.runnerProfileJson}#>>'{sessionCheckpoint,terminal,turnTerminalState}' = 'completed'
|
||
and coalesce(jsonb_typeof(${heartbeatRuns.runnerProfileJson}#>'{sessionCheckpoint,activeTurnId}'), 'null') = 'null', false)`,
|
||
rejectedModel: sql<string | null>`case when ${heartbeatRuns.errorCode} = 'native_provider_model_rejected'
|
||
then ${boundedJsonString(sql`${heartbeatRuns.runnerProfileJson}->'nativeExecutionInput'->'provider'->'model'`, 200)} end`,
|
||
scheduledRetryReason: heartbeatRuns.scheduledRetryReason,
|
||
environmentProviderWaitName: sql<string | null>`case when ${heartbeatRuns.scheduledRetryReason} = ${ENVIRONMENT_PROVIDER_WAIT_RETRY_REASON}
|
||
then ${boundedJsonString(sql`${heartbeatRuns.contextSnapshot}->${ENVIRONMENT_PROVIDER_WAIT_KEY}->'environmentName'`, 200)} end`,
|
||
// Candidate only: the coordinator also pushes nextAttemptAt forward while
|
||
// it defers an ineligible run. The scheduler's own admission check below
|
||
// decides whether this resume is real.
|
||
nativeResumeAt: sql<Date | null>`(select ${nativeRunFinalizations.nextAttemptAt} from ${nativeRunFinalizations}
|
||
where ${nativeRunFinalizations.companyId} = ${heartbeatRuns.companyId} and ${nativeRunFinalizations.runId} = ${heartbeatRuns.id}
|
||
and ${heartbeatRuns.runtimeMode} = 'native' and ${heartbeatRuns.status} = 'failed'
|
||
and ${nativeRunFinalizations.resultId} is null and ${nativeRunFinalizations.phase} = 'retryable_failure'
|
||
and ${heartbeatRuns.contextSnapshot}->'nativeQuestionCancellation' is null
|
||
and ${heartbeatRuns.runnerProfileJson}->'legacyWorkspaceRecoveryMigration' is null)`.mapWith(value => value == null ? null : new Date(value)),
|
||
nativeFailedAt: sql<Date | null>`(select ${nativeRunFinalizations.updatedAt} from ${nativeRunFinalizations}
|
||
where ${nativeRunFinalizations.companyId} = ${heartbeatRuns.companyId} and ${nativeRunFinalizations.runId} = ${heartbeatRuns.id}
|
||
and ${nativeRunFinalizations.phase} = 'terminal_failure')`.mapWith(value => value == null ? null : new Date(value)),
|
||
providerRecoveryAt: sql<Date | null>`(select ${nativeRunFinalizations.updatedAt} from ${nativeRunFinalizations}
|
||
where ${nativeRunFinalizations.companyId} = ${heartbeatRuns.companyId} and ${nativeRunFinalizations.runId} = ${heartbeatRuns.id}
|
||
and ${heartbeatRuns.runtimeMode} = 'native' and ${heartbeatRuns.status} = 'failed'
|
||
and ${nativeRunFinalizations.phase} = 'terminal_failure' and ${nativeRunFinalizations.failureCode} = 'native_provider_terminal_failed'
|
||
and ${nativeRunFinalizations.resultId} is null and ${nativeRunFinalizations.leaseOwner} is null
|
||
and coalesce(${nativeRunFinalizations.failureDetail}->>'successorRunId', '') = '')`.mapWith(value => value == null ? null : new Date(value)),
|
||
nativeFinalizationRetry: exhaustedNativeFinalizationCondition(),
|
||
nativeFinalizationRetryPending: savedResultRetryPendingCondition(),
|
||
// The run whose resultJson supplied providerFailure. A scheduled retry
|
||
// shows its predecessor's reason, so redaction must use that run's registry.
|
||
providerFailureRunId: sql<string>`case when ${heartbeatRuns.resultJson}->'providerFailure' is null
|
||
and ${heartbeatRuns.status} = 'scheduled_retry' and ${heartbeatRuns.retryOfRunId} is not null
|
||
then ${heartbeatRuns.retryOfRunId} else ${heartbeatRuns.id} end`,
|
||
// The run whose resultJson supplied setupCommandFailure (a scheduled retry shows its predecessor's).
|
||
setupFailureRunId: sql<string>`case when ${heartbeatRuns.resultJson}->'setupCommandFailure' is null
|
||
and ${heartbeatRuns.status} = 'scheduled_retry' and ${heartbeatRuns.retryOfRunId} is not null
|
||
then ${heartbeatRuns.retryOfRunId} else ${heartbeatRuns.id} end`,
|
||
resultJson: sql<Record<string, unknown>>`${runStopPresentationJson(heartbeatRuns.resultJson)} || jsonb_build_object('configurationIncomplete', ${heartbeatRuns.resultJson}->'configurationIncomplete',
|
||
'stopReason', ${boundedJsonString(sql`${heartbeatRuns.resultJson}->'stopReason'`, 80)},
|
||
'timeoutSource', ${boundedJsonString(sql`${heartbeatRuns.resultJson}->'timeoutSource'`, 80)},
|
||
'timeoutFired', coalesce(${heartbeatRuns.resultJson}->'timeoutFired' = 'true'::jsonb, false),
|
||
'projectAccessDenied', ${heartbeatRuns.resultJson}->'projectAccessDenied',
|
||
'cancelledByActorType', ${boundedJsonString(sql`${heartbeatRuns.resultJson}->'cancelledByActorType'`, 32)},
|
||
'cancelledByUserId', ${boundedJsonString(sql`${heartbeatRuns.resultJson}->'cancelledByUserId'`, 160)},
|
||
'queuedCommentInterruptQueueId', ${boundedJsonString(sql`${heartbeatRuns.resultJson}->'queuedCommentInterruptQueueId'`, 160)},
|
||
'nativeCancellation', jsonb_build_object(
|
||
'schema', ${boundedJsonString(sql`${heartbeatRuns.resultJson}->'nativeCancellation'->'schema'`, 64)},
|
||
'runId', ${boundedJsonString(sql`${heartbeatRuns.resultJson}->'nativeCancellation'->'runId'`, 160)},
|
||
'companyId', ${boundedJsonString(sql`${heartbeatRuns.resultJson}->'nativeCancellation'->'companyId'`, 160)},
|
||
'issueId', ${boundedJsonString(sql`${heartbeatRuns.resultJson}->'nativeCancellation'->'issueId'`, 160)},
|
||
'scope', ${boundedJsonString(sql`${heartbeatRuns.resultJson}->'nativeCancellation'->'scope'`, 32)},
|
||
'reasonCode', ${boundedJsonString(sql`${heartbeatRuns.resultJson}->'nativeCancellation'->'reasonCode'`, 64)},
|
||
'dispatchState', ${boundedJsonString(sql`${heartbeatRuns.resultJson}->'nativeCancellation'->'dispatchState'`, 32)},
|
||
'dispatched', coalesce(${heartbeatRuns.resultJson}->'nativeCancellation'->'dispatched' = 'true'::jsonb, false),
|
||
'intentAuditId', ${boundedJsonString(sql`${heartbeatRuns.resultJson}->'nativeCancellation'->'intentAuditId'`, 160)},
|
||
'acknowledgementAuditId', ${boundedJsonString(sql`${heartbeatRuns.resultJson}->'nativeCancellation'->'acknowledgementAuditId'`, 160)}),
|
||
'providerFailure', coalesce(${heartbeatRuns.resultJson}->'providerFailure', case when ${heartbeatRuns.status} = 'scheduled_retry'
|
||
then (select predecessor.result_json->'providerFailure' from ${heartbeatRuns} predecessor
|
||
where predecessor.company_id = ${heartbeatRuns.companyId} and predecessor.id = ${heartbeatRuns.retryOfRunId}) end),
|
||
'setupCommandFailure', coalesce(${heartbeatRuns.resultJson}->'setupCommandFailure', case when ${heartbeatRuns.status} = 'scheduled_retry'
|
||
then (select predecessor.result_json->'setupCommandFailure' from ${heartbeatRuns} predecessor
|
||
where predecessor.company_id = ${heartbeatRuns.companyId} and predecessor.id = ${heartbeatRuns.retryOfRunId}
|
||
and predecessor.error_code = 'setup_failed') end))`.as("result_json"),
|
||
}).from(requested).innerJoinLateral(latestRuns, sql`true`).innerJoin(heartbeatRuns, and(eq(heartbeatRuns.companyId, companyId), eq(heartbeatRuns.id, latestRuns.id)))
|
||
.leftJoin(agents, and(eq(agents.companyId, heartbeatRuns.companyId), eq(agents.id, heartbeatRuns.agentId)))
|
||
.orderBy(requestedIssueId),
|
||
// Keep at most two candidates per task: latest Retry and latest other input.
|
||
// A new message must not hide an existing Retry's admission explanation.
|
||
db.selectDistinctOn([wakeIssue, isRetryWait], { issueId: wakeIssue, id: agentWakeupRequests.id, requestedAt: agentWakeupRequests.requestedAt,
|
||
requestedByActorId: agentWakeupRequests.requestedByActorId,
|
||
sourceRunId: boundedJsonString(sql`${agentWakeupRequests.payload}#>'{taskRetry,sourceRunId}'`, 160),
|
||
executionWaitReason: boundedJsonString(sql`to_jsonb(${unpricedUsageWait(sql`${agentWakeupRequests.payload}#>>'{executionWait,reason}'`,
|
||
sql`${agentWakeupRequests.payload}#>>'{executionWait,message}'`)})`, 80),
|
||
preparationStatus: chatActions.status, preparationRetryAt: chatActionDeadline("failed_run_retry"),
|
||
preparationUpdatedAt: chatActions.updatedAt,
|
||
queuedComment: sql<boolean>`coalesce(jsonb_typeof(${agentWakeupRequests.payload}->'commentId') = 'string'
|
||
or jsonb_array_length(case when jsonb_typeof(${agentWakeupRequests.payload}#>'{_paperclipWakeContext,wakeCommentIds}') = 'array'
|
||
then ${agentWakeupRequests.payload}#>'{_paperclipWakeContext,wakeCommentIds}' end) > 0, false)` })
|
||
.from(agentWakeupRequests).leftJoin(chatActions, and(eq(chatActions.companyId, agentWakeupRequests.companyId),
|
||
eq(chatActions.id, canonicalExecutionUuid(sql`${agentWakeupRequests.payload}#>>'{taskRetry,chatActionId}'`)),
|
||
eq(chatActions.kind, "failed_run_retry"), sql`${chatActions.payload}->>'taskRetryIntentId' = ${agentWakeupRequests.id}::text`,
|
||
sql`${chatActions.payload}->>'issueId' = ${wakeIssue}`,
|
||
sql`${chatActions.payload}->>'agentId' = ${agentWakeupRequests.agentId}::text`,
|
||
sql`${chatActions.payload}->>'failedRunId' = ${agentWakeupRequests.payload}#>>'{taskRetry,sourceRunId}'`,
|
||
inArray(chatActions.status, ["issued", "processing", "failed", "cancelled"])))
|
||
.where(and(eq(agentWakeupRequests.companyId, companyId), inArray(wakeIssue, ids),
|
||
eq(agentWakeupRequests.status, "deferred_issue_execution"), eq(agentWakeupRequests.requestedByActorType, "user")))
|
||
.orderBy(wakeIssue, isRetryWait, desc(agentWakeupRequests.requestedAt), desc(agentWakeupRequests.id)),
|
||
agentIds.length ? db.select({ id: agents.id, status: agents.status, pauseReason: agents.pauseReason }).from(agents)
|
||
.where(and(eq(agents.companyId, companyId), inArray(agents.id, agentIds))) : [],
|
||
projectIds.length ? db.select({ id: projects.id, pausedAt: projects.pausedAt, pauseReason: projects.pauseReason }).from(projects)
|
||
.where(and(eq(projects.companyId, companyId), inArray(projects.id, projectIds))) : [],
|
||
db.select({ issueId: issueTreeHoldMembers.issueId }).from(issueTreeHoldMembers).innerJoin(issueTreeHolds, and(
|
||
eq(issueTreeHolds.companyId, issueTreeHoldMembers.companyId), eq(issueTreeHolds.id, issueTreeHoldMembers.holdId)))
|
||
.where(and(eq(issueTreeHoldMembers.companyId, companyId), inArray(issueTreeHoldMembers.issueId, ids),
|
||
eq(issueTreeHolds.status, "active"), eq(issueTreeHolds.mode, "pause"))),
|
||
unionAll(
|
||
db.select({ issueId: issueApprovals.issueId, kind: sql<"approval" | "question">`'approval'` }).from(issueApprovals).innerJoin(approvals, and(
|
||
eq(approvals.companyId, issueApprovals.companyId), eq(approvals.id, issueApprovals.approvalId)))
|
||
.where(and(eq(issueApprovals.companyId, companyId), inArray(issueApprovals.issueId, ids),
|
||
inArray(approvals.status, ["pending", "revision_requested"]))),
|
||
db.selectDistinct({ issueId: issueThreadInteractions.issueId, kind: sql<"approval" | "question">`'question'` }).from(issueThreadInteractions)
|
||
.where(and(eq(issueThreadInteractions.companyId, companyId), inArray(issueThreadInteractions.issueId, ids),
|
||
eq(issueThreadInteractions.kind, "ask_user_questions"), eq(issueThreadInteractions.status, "pending"), activeIssueInteractionCondition())),
|
||
),
|
||
db.select({ status: companies.status, pauseReason: companies.pauseReason }).from(companies).where(eq(companies.id, companyId)),
|
||
// Only consulted for tasks without a run: the latest wake says whether admission refused it.
|
||
startingIssueIds.length ? db.selectDistinctOn([wakeIssue], { issueId: wakeIssue, id: agentWakeupRequests.id,
|
||
status: agentWakeupRequests.status, reason: unpricedUsageWait(sql`${agentWakeupRequests.reason}`, sql`${agentWakeupRequests.error}`),
|
||
agentId: agentWakeupRequests.agentId })
|
||
.from(agentWakeupRequests).where(and(eq(agentWakeupRequests.companyId, companyId), inArray(wakeIssue, startingIssueIds)))
|
||
.orderBy(wakeIssue, desc(agentWakeupRequests.requestedAt), desc(agentWakeupRequests.id)) : [],
|
||
]);
|
||
// Advertise a pending resume only when the scheduler would admit it, using
|
||
// its own check, so the task never hides Retry behind a deferred candidate
|
||
// and never offers Retry beside a resume that will run. Rare rows only.
|
||
// Candidates are failed native runs inside an active coordinator retry, so
|
||
// there are few. Their rows load in three batched reads; admission itself
|
||
// runs a few bounded reads per candidate, four at a time.
|
||
const resumeCandidates = runs.filter(run => run.nativeResumeAt);
|
||
if (resumeCandidates.length) {
|
||
const { readNativeSessionResumptionAgent } = await import("./native-runtime/native-session-resumption-admission.js");
|
||
const candidateIds = resumeCandidates.map(run => run.id);
|
||
const [fullRuns, coordinators] = await Promise.all([
|
||
db.select().from(heartbeatRuns).where(and(eq(heartbeatRuns.companyId, companyId), inArray(heartbeatRuns.id, candidateIds))),
|
||
db.select().from(nativeRunFinalizations).where(and(eq(nativeRunFinalizations.companyId, companyId),
|
||
inArray(nativeRunFinalizations.runId, candidateIds))),
|
||
]);
|
||
const issueIds = [...new Set(fullRuns.flatMap(run => run.nativeIssueId ? [run.nativeIssueId] : []))];
|
||
const issueRows = issueIds.length ? await db.select().from(issues).where(and(eq(issues.companyId, companyId),
|
||
inArray(issues.id, issueIds))) : [];
|
||
const runById = new Map(fullRuns.map(run => [run.id, run]));
|
||
const coordinatorByRun = new Map(coordinators.map(row => [row.runId, row]));
|
||
const issueById = new Map(issueRows.map(row => [row.id, row]));
|
||
for (let start = 0; start < resumeCandidates.length; start += 4) {
|
||
await Promise.all(resumeCandidates.slice(start, start + 4).map(async candidate => {
|
||
const run = runById.get(candidate.id);
|
||
const issue = run?.nativeIssueId ? issueById.get(run.nativeIssueId) : undefined;
|
||
const coordinator = coordinatorByRun.get(candidate.id);
|
||
const admitted = run && issue && coordinator
|
||
&& (awaitsExactRemoteResume(issue, run, coordinator)
|
||
|| await readNativeSessionResumptionAgent(db, { issue, run, coordinator }).catch(() => null));
|
||
if (!admitted) candidate.nativeResumeAt = null;
|
||
}));
|
||
}
|
||
}
|
||
// A queued-message Interrupt records its queue on the stopped run. Newer
|
||
// input can hide that wake from the latest-wait read, so load it by id.
|
||
// The click's recorded intent is the authority, not the wake's requester:
|
||
// a user's message can join an earlier automatic wake.
|
||
const interruptQueueId = (run: typeof runs[number]) => {
|
||
const id = run.status === "cancelled" ? run.resultJson?.queuedCommentInterruptQueueId : null;
|
||
return typeof id === "string" && /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/.test(id) ? id : null;
|
||
};
|
||
const interruptQueueIds = runs.flatMap(run => interruptQueueId(run) ?? []);
|
||
const interruptWaits = interruptQueueIds.length ? await db.select({ issueId: wakeIssue, id: agentWakeupRequests.id,
|
||
requestedAt: agentWakeupRequests.requestedAt,
|
||
queuedInterruptRequestedAt: boundedJsonString(sql`${agentWakeupRequests.payload}#>'{queuedCommentInterrupt,requestedAt}'`, 40) })
|
||
.from(agentWakeupRequests).where(and(eq(agentWakeupRequests.companyId, companyId), inArray(agentWakeupRequests.id, interruptQueueIds),
|
||
eq(agentWakeupRequests.status, "deferred_issue_execution"),
|
||
sql`${agentWakeupRequests.payload}#>>'{queuedCommentInterrupt,actorId}' is not null`)) : [];
|
||
const interruptWaitById = new Map(interruptWaits.map(wait => [wait.id, wait]));
|
||
const runIds = runs.map(run => run.id);
|
||
const actions = runIds.length ? await db.selectDistinctOn([issueRecoveryActions.sourceIssueId]).from(issueRecoveryActions).where(and(
|
||
eq(issueRecoveryActions.companyId, companyId), inArray(issueRecoveryActions.sourceIssueId, ids),
|
||
inArray(sql`${issueRecoveryActions.evidence}->>'runId'`, runIds),
|
||
sql`${issueRecoveryActions.evidence} ? 'systemRecovery'`,
|
||
)).orderBy(issueRecoveryActions.sourceIssueId, desc(issueRecoveryActions.createdAt)) : [];
|
||
// The fresh-continuation fallback's records for these runs.
|
||
const freshRows = runIds.length ? await db.select({ issueId: issueRecoveryActions.sourceIssueId,
|
||
runId: sql<string>`${issueRecoveryActions.evidence}->>'runId'`,
|
||
delivery: sql<string | null>`${issueRecoveryActions.evidence}->>'continuationDelivery'`,
|
||
recorded: sql<boolean>`${issueRecoveryActions.evidence} ? 'freshContinuation'`,
|
||
status: issueRecoveryActions.status,
|
||
schema: sql<string | null>`${issueRecoveryActions.evidence}#>>'{freshContinuation,schema}'`,
|
||
recordedAt: sql<string | null>`${issueRecoveryActions.evidence}#>>'{freshContinuation,recordedAt}'`,
|
||
reconciledRunId: sql<string | null>`${issueRecoveryActions.evidence}#>>'{executionReconciliation,runId}'`,
|
||
checkKind: sql<string | null>`${issueRecoveryActions.evidence}#>>'{freshContinuationCheck,kind}'`,
|
||
nextCheckAt: sql<string | null>`${issueRecoveryActions.evidence}#>>'{freshContinuationCheck,nextCheckAt}'` })
|
||
.from(issueRecoveryActions).where(and(eq(issueRecoveryActions.companyId, companyId), inArray(issueRecoveryActions.sourceIssueId, ids),
|
||
inArray(sql`${issueRecoveryActions.evidence}->>'runId'`, runIds),
|
||
sql`(${issueRecoveryActions.evidence} ? 'freshContinuationSettlement' or ${issueRecoveryActions.evidence} ? 'freshContinuationCheck')`)) : [];
|
||
const pendingSchema = freshRows.some(row => row.delivery === "pending")
|
||
? (await import("./fresh-continuation-fallback.js")).FRESH_CONTINUATION_SCHEMA : null;
|
||
const unfinished = runs.filter(run => ["failed", "timed_out", "interrupted", "cancelled"].includes(run.status)
|
||
&& selected.some(issue => issue.id === run.issueId && !["done", "cancelled"].includes(issue.status)));
|
||
const awaitingSystemContinuation = unfinished.length
|
||
? await (await import("./fresh-continuation-fallback.js")).runsAwaitingSystemContinuation(db, companyId,
|
||
unfinished.map(run => run.issueId), unfinished.map(run => run.id))
|
||
: new Set<string>();
|
||
const freshContinuationFor = (issueId: string, runId: string | undefined): ProjectionState["freshContinuation"] => {
|
||
const rows = freshRows.filter(row => row.issueId === issueId && row.runId === runId);
|
||
// Only the primary record (it carries `freshContinuation`) says whether a
|
||
// continuation is still on its way; holds coalesced into it never do.
|
||
if (rows.some(row => row.recorded && ["not_requested", "pending"].includes(row.delivery ?? ""))) {
|
||
const pendingSince = Math.max(...rows.filter(row => row.recorded && row.delivery === "pending" && row.status === "resolved"
|
||
&& row.schema === pendingSchema && row.reconciledRunId === runId).map(row => Date.parse(row.recordedAt ?? "")));
|
||
return Number.isFinite(pendingSince) ? { state: "continuing", pendingSince } : { state: "continuing" };
|
||
}
|
||
const scheduled = rows.filter(row => ["exact_recovery_pending", "backoff"].includes(row.checkKind ?? "") && row.nextCheckAt)
|
||
.map(row => Number(row.nextCheckAt)).sort((a, b) => a - b)[0];
|
||
if (awaitingSystemContinuation.has(`${issueId}:${runId}`) && retryWaitMap.get(issueId)?.sourceRunId !== runId)
|
||
return { state: "continuing" };
|
||
return scheduled ? { state: "scheduled", nextCheckAt: scheduled } : undefined;
|
||
};
|
||
const dispositionRows = runIds.length ? await db.select({ issueId: issueRecoveryActions.sourceIssueId,
|
||
runId: sql<string>`${issueRecoveryActions.evidence}->>'runId'`, updatedAt: issueRecoveryActions.updatedAt })
|
||
.from(issueRecoveryActions).where(and(eq(issueRecoveryActions.companyId, companyId),
|
||
inArray(issueRecoveryActions.sourceIssueId, ids), inArray(sql`${issueRecoveryActions.evidence}->>'runId'`, runIds),
|
||
inArray(issueRecoveryActions.status, ["active", "escalated"]), eq(issueRecoveryActions.ownerType, "system"),
|
||
inArray(issueRecoveryActions.cause, [...EXECUTION_RECONCILIATION_CAUSES]), isNull(issueRecoveryActions.ownerUserId),
|
||
sql`not (${issueRecoveryActions.evidence} ?| array['systemRecovery', 'executionReconciliation', 'freshContinuation',
|
||
'freshContinuationCheck', 'ownerGoneAttestation', 'explicitUserContinuation'])`)) : [];
|
||
const recoveringRunIds = runs.flatMap(run => run.providerRecoveryAt ? [run.id] : []);
|
||
const handedToPerson = new Set((recoveringRunIds.length ? await db.select({ issueId: issueRecoveryActions.sourceIssueId })
|
||
.from(issueRecoveryActions).where(and(eq(issueRecoveryActions.companyId, companyId), inArray(issueRecoveryActions.sourceIssueId, ids),
|
||
inArray(sql`${issueRecoveryActions.evidence}->>'runId'`, recoveringRunIds),
|
||
sql`(${issueRecoveryActions.evidence} ?| array['ownerGoneAttestation', 'explicitUserContinuation']
|
||
or coalesce(${issueRecoveryActions.evidence}#>>'{freshContinuationCheck,kind}', 'exact_recovery_pending') not in ('exact_recovery_pending', 'backoff'))`)) : [])
|
||
.map(row => row.issueId));
|
||
const automaticDispositionAt = new Map<string, number>();
|
||
if (dispositionRows.length) {
|
||
const { isPersonOwnedFailureCode } = await import("./fresh-continuation-fallback.js");
|
||
for (const row of dispositionRows) {
|
||
const run = runs.find(candidate => candidate.issueId === row.issueId && candidate.id === row.runId);
|
||
if (!run || isPersonOwnedFailureCode(run.errorCode)) continue;
|
||
automaticDispositionAt.set(row.issueId, Math.max(automaticDispositionAt.get(row.issueId) ?? 0, row.updatedAt.getTime()));
|
||
}
|
||
}
|
||
const queuedContinuationIds = runs.filter(run => run.status === "queued" && run.createdAt
|
||
&& run.createdAt.getTime() <= Date.now() - WAITING_CONTINUATION_MOVE_FORWARD_MS).map(run => run.id);
|
||
const [continuationRows, personRows] = queuedContinuationIds.length ? await Promise.all([
|
||
db.select({ runId: sql<string>`${issueRecoveryActions.evidence}->>'continuationRunId'` }).from(issueRecoveryActions).where(and(
|
||
eq(issueRecoveryActions.companyId, companyId), inArray(issueRecoveryActions.sourceIssueId, ids),
|
||
sql`${issueRecoveryActions.evidence} ? 'freshContinuation'`,
|
||
sql`${issueRecoveryActions.evidence}->>'continuationDelivery' = 'delivered'`,
|
||
inArray(sql`${issueRecoveryActions.evidence}->>'continuationRunId'`, queuedContinuationIds))),
|
||
db.select({ runId: agentWakeupRequests.runId }).from(agentWakeupRequests).where(and(
|
||
eq(agentWakeupRequests.companyId, companyId), inArray(agentWakeupRequests.runId, queuedContinuationIds),
|
||
eq(agentWakeupRequests.requestedByActorType, "user"))),
|
||
]) : [[], []];
|
||
const waitingContinuationRunIds = new Set(continuationRows.map(row => row.runId));
|
||
const personRequestedRunIds = new Set(personRows.flatMap(row => row.runId ? [row.runId] : []));
|
||
const waitingContinuationFor = (run: typeof runs[number] | undefined): ProjectionState["waitingContinuation"] =>
|
||
run?.createdAt && waitingContinuationRunIds.has(run.id)
|
||
? { queuedAt: run.createdAt.getTime(), personRequested: personRequestedRunIds.has(run.id) } : undefined;
|
||
const ownerStops = runIds.length ? await db.select({ issueId: issueRecoveryActions.sourceIssueId,
|
||
runId: sql<string>`${issueRecoveryActions.evidence}->>'runId'` }).from(issueRecoveryActions).where(and(
|
||
eq(issueRecoveryActions.companyId, companyId), inArray(issueRecoveryActions.sourceIssueId, ids),
|
||
eq(issueRecoveryActions.cause, "native_workspace_finalization_owner_unverified"), eq(issueRecoveryActions.ownerType, "board"),
|
||
inArray(issueRecoveryActions.status, ["active", "escalated"]),
|
||
inArray(sql`${issueRecoveryActions.evidence}->>'runId'`, runIds),
|
||
sql`${issueRecoveryActions.evidence}->'workspaceOwnerStop' is null`,
|
||
sql`coalesce(${issueRecoveryActions.evidence}#>>'{workspaceOwnerRecovery,state}', '') <> 'resuming'`,
|
||
)) : [];
|
||
const ownerStopRequired = new Set(ownerStops.map(row => `${row.issueId}:${row.runId}`));
|
||
const blockedSucceededRunIds = runs.filter(run => run.status === "succeeded"
|
||
&& selected.some(issue => issue.id === run.issueId && issue.status === "blocked")).map(run => run.id);
|
||
const agentBlockerHandoffRunIds = new Set(blockedSucceededRunIds.length ? (await db.select({ runId: statusDecisions.runId })
|
||
.from(statusDecisions).where(and(eq(statusDecisions.companyId, companyId), inArray(statusDecisions.runId, blockedSucceededRunIds),
|
||
eq(statusDecisions.reasonCode, NATIVE_AGENT_BLOCKER_HANDOFF_CODE), eq(statusDecisions.applicationState, "applied"))))
|
||
.map(row => row.runId) : []);
|
||
const byIssue = <T extends { issueId: string }>(rows: T[]) => new Map(rows.map(row => [row.issueId, row]));
|
||
const runMap = byIssue(runs), waitMap = new Map<string, typeof waits[number]>();
|
||
// A Retry belongs to the run it observed. New conversation work supersedes
|
||
// that authority, even before dispatch retires the old receipt. Retained
|
||
// ordinary input is independent and must still be shown as waiting.
|
||
const currentWaits = waits.filter(wait => !wait.sourceRunId || wait.sourceRunId === runMap.get(wait.issueId)?.id);
|
||
const decisionWaits = currentWaits.filter(wait => wait.executionWaitReason === "decision_pending");
|
||
if (decisionWaits.length) {
|
||
const { pendingContinuationDecisionWaits } = await import("./explicit-native-continuation.js");
|
||
const held = await pendingContinuationDecisionWaits(db, companyId, decisionWaits.flatMap(wait => wait.requestedByActorId
|
||
? [{ id: wait.id, issueId: wait.issueId, actorId: wait.requestedByActorId, taskRetrySourceRunId: wait.sourceRunId }] : []));
|
||
for (const wait of decisionWaits) if (!held.has(wait.id)) wait.executionWaitReason = null;
|
||
}
|
||
const retryWaitMap = byIssue(currentWaits.filter(wait => wait.sourceRunId));
|
||
const retryIssueIds = [...retryWaitMap.keys()];
|
||
const retryDependencyMap = new Map<string, RetryDependencies>();
|
||
if (retryIssueIds.length) {
|
||
const { issueService } = await import("./issues.js");
|
||
const readiness = await issueService(db).listDependencyReadiness(companyId, retryIssueIds);
|
||
const blockerIds = [...new Set([...readiness.values()].flatMap(row => row.unresolvedBlockerIssueIds))];
|
||
const statusRows = blockerIds.length ? await db.select({ id: issues.id, status: issues.status }).from(issues)
|
||
.where(and(eq(issues.companyId, companyId), inArray(issues.id, blockerIds))) : [];
|
||
const statusById = new Map(statusRows.map(row => [row.id, row.status]));
|
||
for (const [waitingIssueId, row] of readiness) {
|
||
const ids = [...new Set(row.unresolvedBlockerIssueIds)];
|
||
if (!ids.length) continue;
|
||
const statuses = ids.map(id => statusById.get(id));
|
||
const saving = new Set(row.pendingFinalizeBlockerIssueIds);
|
||
retryDependencyMap.set(waitingIssueId, { count: ids.length,
|
||
blocked: statuses.includes("blocked"), cancelled: statuses.includes("cancelled"),
|
||
savingFilesOnly: ids.every(id => saving.has(id)) });
|
||
}
|
||
}
|
||
// A completed turn refused only by the cleanup gate shows the task's own
|
||
// blocker wait. Read readiness for those tasks only.
|
||
const settledIssueIds = [...runMap.values()].filter(run => run.completedTurnCleanup).map(run => run.issueId);
|
||
const waitingOnBlockers = new Set<string>();
|
||
if (settledIssueIds.length) {
|
||
const { issueService } = await import("./issues.js");
|
||
const readiness = await issueService(db).listDependencyReadiness(companyId, settledIssueIds);
|
||
for (const [waitingIssueId, row] of readiness)
|
||
if (row.unresolvedBlockerIssueIds.length) waitingOnBlockers.add(waitingIssueId);
|
||
}
|
||
const parentIds = selected.filter(issue => issue.status === "blocked" && runMap.get(issue.id)?.status === "succeeded"
|
||
&& currentWaits.some(wait => wait.issueId === issue.id && wait.queuedComment)).map(issue => issue.id);
|
||
const waitingOnOpenSubtasks = new Set(parentIds.length ? (await db.selectDistinct({ parentId: issues.parentId }).from(issues)
|
||
.where(and(eq(issues.companyId, companyId), inArray(issues.parentId, parentIds), notInArray(issues.status, ["done", "cancelled"]))))
|
||
.flatMap(row => row.parentId ? [row.parentId] : []) : []);
|
||
for (const wait of currentWaits) {
|
||
const current = waitMap.get(wait.issueId);
|
||
if (!current || wait.requestedAt > current.requestedAt ||
|
||
(wait.requestedAt.getTime() === current.requestedAt.getTime() && wait.id > current.id)) waitMap.set(wait.issueId, wait);
|
||
}
|
||
// Recheck current authority, including standard trust, root-issue grants and
|
||
// project policy. Do not reuse the failed run's frozen policy for a new retry.
|
||
// Only denied tasks need these reads; deduplicate projects and batch per agent.
|
||
// A task's organizational project can differ from its retained source. Old
|
||
// failures did not record the denied project, and even typed failures can
|
||
// become obsolete after a workspace selection changes.
|
||
const deniedIssues = selected.filter(issue => issue.assigneeAgentId && !["done", "cancelled"].includes(issue.status)
|
||
&& deniedProjectId(issue, runMap.get(issue.id)) !== undefined);
|
||
const requirements = await taskWorkspaceProjectRequirements(db, companyId, deniedIssues.map(issue => issue.id));
|
||
const deniedByAgent = new Map<string, Set<string>>();
|
||
for (const issue of deniedIssues) {
|
||
const ids = deniedByAgent.get(issue.assigneeAgentId!) ?? new Set<string>();
|
||
for (const projectId of requirements.get(issue.id)?.projectIds ?? []) ids.add(projectId);
|
||
if (ids.size) deniedByAgent.set(issue.assigneeAgentId!, ids);
|
||
}
|
||
const currentAccess = new Map<string, boolean>();
|
||
const authorization = authorizationService(db);
|
||
for (const [agentId, ids] of deniedByAgent) {
|
||
const projectIds = [...ids];
|
||
const decisions = await authorization.decideReads({ type: "agent", agentId, companyId, source: "agent_key" },
|
||
projectIds.map(projectId => ({ type: "project" as const, companyId, projectId })));
|
||
projectIds.forEach((projectId, index) => currentAccess.set(`${agentId}:${projectId}`, decisions[index]?.allowed === true));
|
||
}
|
||
const actionMap = new Map(actions.map(action => [action.sourceIssueId, action]));
|
||
const interruptWaitFor = (issueId: string) => {
|
||
const run = runMap.get(issueId);
|
||
const wait = run ? interruptWaitById.get(interruptQueueId(run) ?? "") : undefined;
|
||
return wait?.issueId === issueId ? wait : undefined;
|
||
};
|
||
const agentMap = new Map(agentRows.map(agent => [agent.id, agent]));
|
||
const projectMap = new Map(projectRows.map(project => [project.id, project]));
|
||
const pauses = new Set(pauseRows.map(row => row.issueId));
|
||
const approvalsSet = new Set(gateRows.filter(row => row.kind === "approval").map(row => row.issueId));
|
||
const admissionWaitMap = byIssue(admissionWaits);
|
||
const questions = new Set(gateRows.filter(row => row.kind === "question").map(row => row.issueId));
|
||
const launchWaits = [...retryWaitMap].flatMap(([issueId, wait]) => {
|
||
const run = runMap.get(issueId);
|
||
return run && wait.sourceRunId === run.id && wait.executionWaitReason === "process_identity_missing" ? [{ issueId, run }] : [];
|
||
});
|
||
const launchRefusals = await unidentifiedLocalLaunchRefusals(db, launchWaits.map(entry => entry.run));
|
||
const cleanupSettling = await quarantineCleanupSettling(db, companyId,
|
||
[...runMap].flatMap(([issueId, run]) => run.errorCode === NATIVE_SESSION_CLEANUP_QUARANTINED ? [{ issueId, run }] : []));
|
||
const confirmableLaunches = new Set(launchWaits.filter(entry => launchRefusals.get(entry.run.id) === null).map(entry => entry.issueId));
|
||
const now = Date.now();
|
||
for (const issue of selected) {
|
||
const required = requirements.get(issue.id);
|
||
const denied = required?.projectIds.filter(projectId => currentAccess.get(`${issue.assigneeAgentId}:${projectId}`) !== true);
|
||
const facts = { run: runMap.get(issue.id), action: actionMap.get(issue.id), wait: waitMap.get(issue.id),
|
||
retryWait: retryWaitMap.get(issue.id) };
|
||
const presentation = projectTaskInterruption(issue, { ...facts,
|
||
freshContinuation: freshContinuationFor(issue.id, facts.run?.id),
|
||
automaticDispositionAt: automaticDispositionAt.get(issue.id),
|
||
waitingContinuation: waitingContinuationFor(facts.run),
|
||
recoveryHandedToPerson: handedToPerson.has(issue.id),
|
||
agentBlockerNeedsPerson: agentBlockerHandoffRunIds.has(facts.run?.id ?? ""),
|
||
interruptWait: interruptWaitFor(issue.id),
|
||
projectAccessAllowed: denied?.length === 0,
|
||
projectAccessProjectId: denied?.includes(issue.projectId ?? "") ? issue.projectId : null,
|
||
workspaceSelectionAvailable: required?.available ?? false,
|
||
agent: agentMap.get(issue.assigneeAgentId ?? ""), project: projectMap.get(issue.projectId ?? ""),
|
||
company: companyRows[0], paused: pauses.has(issue.id), approval: approvalsSet.has(issue.id), question: questions.has(issue.id),
|
||
ownerStopRequired: ownerStopRequired.has(`${issue.id}:${facts.run?.id}`),
|
||
unidentifiedLaunchConfirmable: confirmableLaunches.has(issue.id),
|
||
admissionWait: admissionWaitMap.get(issue.id),
|
||
retryDependencies: retryDependencyMap.get(issue.id),
|
||
waitingOnBlockers: waitingOnBlockers.has(issue.id),
|
||
waitingOnOpenSubtasks: waitingOnOpenSubtasks.has(issue.id),
|
||
cleanupSettling: cleanupSettling.has(issue.id),
|
||
}, now);
|
||
result.set(issue.id, presentation);
|
||
const run = facts.run;
|
||
const runStatus = HEARTBEAT_RUN_STATUSES.find(status => status === run?.status);
|
||
const changedAt = taskInterruptionChangedAt(facts);
|
||
if (presentation?.responsibility === "system" && presentation.state === "failed" && run && runStatus
|
||
&& (changedAt === null || changedAt > now - TASK_INTERRUPTION_REPORT_WINDOW_MS)) {
|
||
captureTaskInterruptionFailure({ companyId, taskId: issue.id, agentId: run.agentId,
|
||
runId: run.id, runStatus, intentId: presentation.intentId, runErrorCode: run.errorCode ?? null,
|
||
agentAdapter: run.agentAdapter ?? "unknown" });
|
||
}
|
||
}
|
||
const offered = [...result].flatMap(([issueId, presentation]) => presentation?.canRetry ? [issueId] : []);
|
||
if (offered.length) {
|
||
const { executionBlockerPredicate, getExecutionBlocker, restoreHoldPredicate, restoreHoldReleasedForCurrentOwnerPredicate } =
|
||
await import("./execution-blocker.js");
|
||
const held = await db.selectDistinct({ issueId: issueRecoveryActions.sourceIssueId }).from(issueRecoveryActions).where(and(
|
||
eq(issueRecoveryActions.companyId, companyId), inArray(issueRecoveryActions.sourceIssueId, offered),
|
||
executionBlockerPredicate(), not(restoreHoldReleasedForCurrentOwnerPredicate()), restoreHoldPredicate()));
|
||
for (const { issueId } of held) {
|
||
const blocker = await getExecutionBlocker(db, companyId, issueId);
|
||
const presentation = result.get(issueId);
|
||
if (!presentation || !blocker || blocker.canRetry) continue;
|
||
presentation.canRetry = false;
|
||
delete presentation.retryAction;
|
||
if (!presentation.retryWait && blocker.workspaceRecovery && blocker.workspaceRecovery.status !== "recovered")
|
||
presentation.retryWait = { reason: "workspace_repair_required", message: retryWaitMessages.workspace_repair_required! };
|
||
}
|
||
}
|
||
// Redact each explanation with the registry of the run that recorded it: the
|
||
// workspace failure is the observed run's, while a scheduled retry's provider
|
||
// failure may be its predecessor's.
|
||
const disclosures = [...result.entries()].flatMap(([issueId, presentation]) =>
|
||
(presentation?.providerFailure || presentation?.workspaceFailure || presentation?.setupFailure) && presentation.observedRunId
|
||
? [{ id: presentation.observedRunId, providerFailureRunId: runMap.get(issueId)?.providerFailureRunId ?? presentation.observedRunId,
|
||
setupFailureRunId: runMap.get(issueId)?.setupFailureRunId ?? presentation.observedRunId,
|
||
providerFailure: presentation.providerFailure, workspaceFailure: presentation.workspaceFailure,
|
||
setupFailure: presentation.setupFailure, presentation }] : []);
|
||
if (disclosures.length) {
|
||
try {
|
||
const [redacted, settings] = await Promise.all([
|
||
createRunSecretRedactionRegistry(db).redactForRuns(companyId, disclosures.flatMap(({ id, providerFailureRunId, setupFailureRunId, providerFailure, workspaceFailure, setupFailure }) => [
|
||
{ id: providerFailureRunId, providerFailure, workspaceFailure: undefined, setupFailure: undefined },
|
||
{ id, providerFailure: undefined, workspaceFailure, setupFailure: undefined },
|
||
{ id: setupFailureRunId, providerFailure: undefined, workspaceFailure: undefined, setupFailure },
|
||
])),
|
||
instanceSettingsService(db).getGeneral(),
|
||
]);
|
||
const safe = disclosures.map((_, index) => ({ providerFailure: redacted[3 * index]?.providerFailure,
|
||
workspaceFailure: redacted[3 * index + 1]?.workspaceFailure, setupFailure: redacted[3 * index + 2]?.setupFailure }));
|
||
const safeMessage = (value: unknown) => typeof value === "string"
|
||
? [...redactCurrentUserText(value, { enabled: settings.censorUsernameInLogs })].slice(0, 4000).join("") : "";
|
||
disclosures.forEach(({ presentation }, index) => {
|
||
if (presentation.providerFailure) {
|
||
const value = safe[index]?.providerFailure;
|
||
const failure = readProviderFailure({ ...value, message: safeMessage(value?.message) });
|
||
if (failure) presentation.providerFailure = failure;
|
||
else delete presentation.providerFailure;
|
||
}
|
||
if (presentation.workspaceFailure) {
|
||
const message = safeMessage(safe[index]?.workspaceFailure?.message);
|
||
if (message.trim()) presentation.workspaceFailure = { message };
|
||
else delete presentation.workspaceFailure;
|
||
}
|
||
if (presentation.setupFailure) {
|
||
const value = safe[index]?.setupFailure;
|
||
// Redact the whole text, then shorten: the output keeps its final lines, not a head slice.
|
||
const whole = (text: unknown) => typeof text === "string" ? redactCurrentUserText(text, { enabled: settings.censorUsernameInLogs }) : "";
|
||
const command = whole(value?.command), message = whole(value?.message);
|
||
const failure = value && command.trim() && message.trim()
|
||
? boundSetupCommandFailure({ ...value, command, output: value.output == null ? null : whole(value.output) || null, message })
|
||
: null;
|
||
if (failure) presentation.setupFailure = failure;
|
||
else delete presentation.setupFailure;
|
||
}
|
||
});
|
||
} catch {
|
||
for (const { presentation } of disclosures) {
|
||
delete presentation.providerFailure;
|
||
delete presentation.workspaceFailure;
|
||
delete presentation.setupFailure;
|
||
}
|
||
logger.warn({ companyId, runCount: disclosures.length }, "Could not redact run failure display; explanations omitted");
|
||
}
|
||
}
|
||
return result;
|
||
}
|
||
|
||
export async function taskInterruptionPresentation(db: Db, issue: ProjectionIssue) {
|
||
return (await taskInterruptionPresentations(db, issue.companyId, [issue])).get(issue.id) ?? null;
|
||
}
|
||
|
||
/**
|
||
* The refusal that admission recorded on this Retry request, when another
|
||
* click cannot change it. A Retry answer must name it instead of reporting
|
||
* the request as accepted: such a request starts no run by itself. The task
|
||
* view's projection says whether a quarantined session is still cleaning up.
|
||
*/
|
||
export async function taskRetryRefusal(db: Pick<Db, "select">, companyId: string, requestId: string,
|
||
presentation?: Pick<TaskInterruptionPresentation, "canRetry" | "retryWait"> | null):
|
||
Promise<{ reason: string; message: string } | null> {
|
||
const [wake] = await db.select({ status: agentWakeupRequests.status, runId: agentWakeupRequests.runId,
|
||
reason: sql<string | null>`${agentWakeupRequests.payload}#>>'{executionWait,reason}'` })
|
||
.from(agentWakeupRequests).where(and(eq(agentWakeupRequests.companyId, companyId), eq(agentWakeupRequests.id, requestId)));
|
||
if (!wake || wake.status !== "deferred_issue_execution" || wake.runId || !wake.reason) return null;
|
||
const shown = presentation?.retryWait?.reason === wake.reason ? presentation : null;
|
||
// A quarantined session that is still cleaning up is a refusal only while
|
||
// the task view hides Retry for it; every other reason has a fixed verdict.
|
||
if (heldRetryOffersRetry(wake.reason, wake.reason === "cleanup_quarantined" && shown?.canRetry === false)) return null;
|
||
return { reason: wake.reason, message: shown?.retryWait?.message ?? taskRetryWaitMessage(wake.reason) };
|
||
}
|