Files
PaperClipAI/server/src/services/native-runtime/native-agent-blocker.integration.test.ts
T
Devin FoleyandPaperclip 18978bdc1d fix(native): preserve named human unblock owners (#22438)
## Thinking Path

> - Paperclip manages work carried out by agents and people.
> - Native finalization converts a runner result into a durable task
status and follow-up effects.
> - A blocked result can name the person who must decide what happens
next.
> - The classifier discarded that identity, and the arbiter saved a
generic board owner.
> - This could replace an explicit user owner saved through the task API
and leave the decision outside that person's inbox.
> - This pull request resolves human owners against active company
membership and preserves them through commit.
> - The named person receives the existing blocker card, and the blocked
worker does not get another wake.

## Linked Issues or Issue Description

**What happened?**

Native finalization replaced a named human unblock owner with `board`.
It also overwrote an explicit user owner saved through the task API
during the turn.

**Expected behavior**

Keep a valid human unblock owner and route the blocker card to that
person's inbox. Use the board when the reference is unknown, inactive,
ambiguous, or outside the company.

**Steps to reproduce**

1. Create an active company member with a distinct display name.
2. Have a native worker report a human blocker using that name.
3. Finalize the run and read the task's unblock owner and the member's
inbox.

Related: #21265 covers named agent handoffs and retains board ownership
for people. This change covers human member routing and does not add
agent handoffs.

## What Changed

- Resolve a human owner from an explicit user ID, an API-bound user, or
one matching company member by ID, full name, or email.
- Preserve the user owner for both blocker scopes and durable dependency
waits.
- Recheck the accepted result and active membership under the status
commit lock, including board fallbacks. Rebuild the assessment on every
status retry so a newly saved owner or revoked membership cannot be
overwritten by stale evidence.
- Enforce `canAssignTasksToHumans` before binding a human owner,
including dependency waits that do not emit a `bind_blocker` effect.
- Validate the result before retiring automatic completion reviews.
Invalid results leave existing reviews pending; valid results retain the
normal cleanup path.
- Keep human blocker routing out of the agent wake queue.
- Add embedded Postgres regressions for finalization, replay, personal
attention, invalid owners, late API ownership changes, membership
revocation, permission denial, and answer continuation for human-owned
blockers.
- Make the answer-continuation fixture persist the same result it
classifies, so commit-time evidence validation sees realistic saved
evidence. Assert the resolved user owner in both blocker scopes.
- Let the recovery census clock advance at real-time pace during
delivery I/O waits. This handles callbacks and nested timers without
jumping to maintenance deadlines. Preserve the existing real-clock
timeout and recovery assertions, and name pending queues in failures.
- Document human blocker resolution and compatibility.

## Verification

- Red-green reproduction: three ownership regressions failed on the
original implementation and passed with the fix.
- Red-green race reproduction: late API ownership changes and membership
revocation failed before the commit recheck and per-retry reassessment
fixes. Each finalization invocation now has to commit; a later replay
cannot hide an exhausted retry.
- Red-green review-retirement reproduction: a new invalid result
cancelled a proven automatic review before throwing
`native_result_invalid_disposition`. The new invalid/valid regression
pair verifies that invalid results preserve the pending review and task
decision, while valid completion still retires the card.
- Red-green answer-continuation reproduction: both human-owner cases
failed with `native_status_race` while saved evidence was empty.
Persisting the classified result and asserting `{ userId }` restored all
13 answer-continuation cases, including worker resumption.
- Red-green permission reproduction: an agent with
`canAssignTasksToHumans: false` could bind a human blocker before the
permission check. Direct and dependency cases now refuse finalization
without creating a personal blocker card.
- On refreshed head `a6d37916546`, `pnpm --filter @paperclipai/server
exec node scripts/typecheck-layers.mjs` passed on macOS with Node 24.
- On refreshed head `a6d37916546`, from the repository root, `CI=true
node node_modules/vitest/vitest.mjs run
server/src/services/native-runtime/native-agent-blocker.integration.test.ts
server/src/services/native-runtime/status-arbiter.test.ts
server/src/services/native-runtime/evidence-classifier.test.ts
server/src/__tests__/native-status-reassessment-work.test.ts
server/src/__tests__/routable-blocked.test.ts
server/src/__tests__/native-status-arbiter-corpus.test.ts
server/src/services/native-runtime/native-environment-blocker.integration.test.ts
server/src/__tests__/heartbeat-blocked-question-answer.test.ts
server/src/__tests__/issue-recovery-live-run-agent-census.test.ts`
passed: 292 tests in nine files on macOS with Node 24.
- Red-green census reproduction: both post-I/O callbacks and nested
timers must settle without jumping to a future maintenance deadline. The
nested case timed out under a zero-only clock pump; the shared
automatically advancing census clock handles both.
- Final CI at `a6d37916546`: [run 44848053016, attempt
2](https://github.com/paperclipai/paperclip/actions/runs/44848053016/attempts/2)
completed successfully. Build, Canary Dry Run, policy, every server and
workspace test shard, Runner checks, serialized suites, e2e, and verify
are green. Greptile review 7 is 5/5 on the same head, Superagent and
other security checks pass, and no review threads remain unresolved. A
local merge-tree check against master `621c5577cdf` also passed with no
conflicts.
- Current CI at `a6d37916546`, [run
44848053016](https://github.com/paperclipai/paperclip/actions/runs/44848053016):
attempt 1 finished with 13 interrupted nonaggregate jobs, each with an
explicit runner shutdown signal in its log. Nine signals occurred within
three seconds on separate m6a.xlarge hosts in us-east-1a; Build and
Canary also shut down later. Aggregate e2e and verify failed on those
dependency results. The underlying termination cause remains unknown.
Shared CI settings are unchanged.
- The [automatic recovery
run](https://github.com/paperclipai/paperclip/actions/runs/44849704877)
skipped retry because Build had only the annotation `Process completed
with exit code 143.` The classifier reads logs for cancellation
annotations, but did not read this job's log. That log explicitly
records a runner shutdown at 01:10:49 UTC. The other 12 failed jobs were
recognized as runner loss. After verifying all 13 logs, manually
requested failed-job attempt 2; it completed successfully without source
or configuration changes between attempts. The shared recovery
classifier is unchanged; this false negative remains a documented
follow-up.
- Historical CI at `cf601ba`: all server shards (including the census),
Runner checks, typecheck/release registry, serialized suites, e2e, and
canary dry run passed. Build received runner shutdown signals on
attempts 1–7; aggregate verify failed because Build failed. Attempt 5
diagnostics show shutdown while the job lease was valid. Attempt 7
stopped after server dependency preparation, without a compiler error.
The termination cause remains unknown. Three recent PRs completed Build
on the same AWS fleet; this is not evidence of a fleet-wide outage.
Shared CI settings are unchanged.
- Diagnostic comparison at `cf601ba`: the server compiler passed locally
on macOS with four checkers, full typechecking, and
declaration/JavaScript emission (`node ../scripts/run-tsc.mjs --checkers
4 --extendedDiagnostics` under Node 24). The monitored process group
peaked at about 10.5 GiB RSS and finished in 25.8 seconds. This does not
establish the cause of the Linux runner shutdowns.
- Refreshed the branch onto master `611dc68581c` without conflicts; the
PR source diff is unchanged. Greptile review 7 scored head `a6d37916546`
5/5 with no new actionable findings and no unresolved review threads.
All applicable CI gates passed on attempt 2.
- Full local workspace build at `cf601ba` passed on macOS using Node 24
and the documented single-thread compiler mode:
`PAPERCLIP_TYPECHECK_LOW_MEMORY=1 npx --yes --package=node@24
--package=pnpm@9.15.4 pnpm build`. This includes typechecking and
artifact emission. The tracked working tree stayed clean.
- `pnpm verify --explain` returned exit 2 because this documentation
path requires broad CI coverage. Fresh CI will supply that coverage; the
full local test suite was not run.
- Earlier CI build and canary jobs received runner shutdown signals. The
server shard also exposed an answer-continuation fixture that saved an
empty result while classifying a named human. That mismatch was
reproduced locally, and the fixture now persists the classified result
and checks the user owner. Fresh CI is required for this revision.
- `git diff --check` and the staged gitleaks scan passed. The diff was
also checked for private issue references, identifiers, URLs, and
personal data.

## Risks

- Name and email matching ignore case. Several matches fall back to the
board. Invalid explicit IDs never fall through to another person.
- Resolution adds bounded reads only when a native result reports a
human blocker. It adds no worker, timer, or idle scan.
- No database or result-schema migration is required. Existing board and
agent decisions remain readable. Committed historical decisions are not
rewritten; new finalizations use current membership.
- This changes human routing only. It grants no access and changes no
merge, approval, or dependency policy.

## Model Used

- OpenAI GPT-6.1 Sol (`gpt-6.1-sol`) through Codex in T3 Code, with
xhigh reasoning effort, code editing, shell tools, and test execution.

## 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 and contains no internal
Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [x] All Paperclip CI gates are green
- [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

---------

Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-11-20 17:45:45 -08:00

326 lines
23 KiB
TypeScript

import { randomUUID } from "node:crypto";
import { and, eq } from "drizzle-orm";
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
import { agents, agentWakeupRequests, authUsers, companies, companyMemberships, completionContracts, createDb, heartbeatRuns, issueRelations, issues,
nativeRunFinalizations, statusDecisions } from "@paperclipai/db";
import type { ControlPlanePort, NativeExecutionInputV1, PrpStructuredRunResult, PrpTerminalState } from "@paperclipai/paperclip-runner";
import { startEmbeddedPostgresTestDatabase } from "../../__tests__/helpers/embedded-postgres.js";
import { CONTROL_PLANE_CONFORMANCE_RESULT } from "../../vendor/paperclip-runner/testing.js";
const provider = vi.hoisted(() => ({ execute: vi.fn() }));
vi.mock("../../vendor/paperclip-runner/index.js", async (importOriginal) => ({
...(await importOriginal<typeof import("../../vendor/paperclip-runner/index.js")>()), executeNativeSession: provider.execute,
}));
import { executePaperclipNativeSession } from "./native-session-executor.js";
import { buildNativeCompletionContract } from "./completion-contracts.js";
import { prepareNativeHeartbeatRun } from "./prepare-native-run.js";
import { finalizeNativeRun } from "./native-run-finalizer.js";
import { NATIVE_ENVIRONMENT_BLOCKER_CODE, NATIVE_ENVIRONMENT_BLOCKER_HINT, NATIVE_ENVIRONMENT_BLOCKER_MESSAGE } from "./native-environment-blocker.js";
import { NATIVE_AGENT_BLOCKER_CONTINUATION_CODE, NATIVE_AGENT_BLOCKER_HANDOFF_CODE, NATIVE_AGENT_BLOCKER_HINT } from "./native-agent-blocker.js";
import { taskInterruptionPresentation } from "../task-interruption-presentation.js";
import { requestTaskRetry } from "../task-retry.js";
import { heartbeatService } from "../heartbeat.js";
import { attentionService } from "../attention.js";
import * as statusCommitter from "./status-decision-committer.js";
import * as evidenceClassifier from "./evidence-classifier.js";
type Blocker = NonNullable<PrpStructuredRunResult["blocker"]>;
const toolOutputBlocker: Blocker = {
owner: { kind: "system", name: "execution environment" }, scope: "task_wide", reasonCode: "tool_output_failure",
unblockAction: "Resolve the persistent tool-output failure ('cryptic' returned for every tool call) and/or provide the current contents of the ledger file.",
};
const selfBlocker: Blocker = {
owner: { kind: "agent", name: "ledger worker" }, scope: "task_wide", reasonCode: "work_incomplete",
unblockAction: "Re-read the ledger file and save the ledger document.",
};
describe("native run that reports a task-wide blocker it owns", () => {
let temporary: Awaited<ReturnType<typeof startEmbeddedPostgresTestDatabase>>;
let db: ReturnType<typeof createDb>;
const companyId = randomUUID(), agentId = randomUUID(), boardUserId = "ledger-board-user";
const namedUserId = "named-dependency-reviewer";
const foreignCompanyId = randomUUID();
beforeAll(async () => {
temporary = await startEmbeddedPostgresTestDatabase("native-agent-blocker-");
db = createDb(temporary.connectionString);
await db.insert(companies).values({ id: companyId, name: "Agent blocker regression", issuePrefix: "AGB" });
await db.insert(agents).values({ id: agentId, companyId, name: "Ledger worker", status: "active", adapterType: "paperclip_runner",
adapterConfig: { provider: "opencode", model: "test-model" } });
await db.insert(companyMemberships).values({ companyId, principalType: "user", principalId: boardUserId, status: "active", membershipRole: "member" });
await db.insert(authUsers).values({ id: namedUserId, name: "Dependency Reviewer", email: "reviewer@example.test",
createdAt: new Date(), updatedAt: new Date() });
await db.insert(companyMemberships).values({ companyId, principalType: "user", principalId: namedUserId, status: "active", membershipRole: "member" });
await db.insert(companies).values({ id: foreignCompanyId, name: "Other company", issuePrefix: "OTHER" });
for (const [id, name, membershipCompany, status] of [
["foreign-reviewer", "Foreign Reviewer", foreignCompanyId, "active"],
["inactive-reviewer", "Inactive Reviewer", companyId, "inactive"],
["ambiguous-reviewer-one", "Shared Reviewer", companyId, "active"],
["ambiguous-reviewer-two", "Shared Reviewer", companyId, "active"],
]) {
await db.insert(authUsers).values({ id, name, email: `${id}@example.test`, createdAt: new Date(), updatedAt: new Date() });
await db.insert(companyMemberships).values({ companyId: membershipCompany, principalType: "user", principalId: id, status, membershipRole: "member" });
}
});
afterAll(async () => { await temporary?.cleanup(); });
async function createTask() {
const issueId = randomUUID();
await db.insert(issues).values({ id: issueId, companyId, title: "Ledger task", status: "in_progress", workMode: "standard",
assigneeAgentId: agentId, createdByUserId: boardUserId, responsibleUserId: boardUserId });
return issueId;
}
async function runTurn(issueId: string, report: { blocker: Blocker } | { done: true }, runId: string = randomUUID(),
beforeFinalize?: () => Promise<unknown>, expectedPhase: "committed" | "terminal_failure" = "committed") {
const [existing] = await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.id, runId));
if (!existing) await db.insert(heartbeatRuns).values({ id: runId, companyId, agentId, responsibleUserId: boardUserId, contextSnapshot: { issueId } });
const [issue] = await db.select().from(issues).where(eq(issues.id, issueId));
const [run] = await db.update(heartbeatRuns).set({ status: "running", runtimeMode: "native", nativeIssueId: issueId, startedAt: new Date() })
.where(eq(heartbeatRuns.id, runId)).returning();
await db.update(issues).set({ executionRunId: runId }).where(eq(issues.id, issueId));
const native = await prepareNativeHeartbeatRun({ db, run, issue, environmentLeaseId: randomUUID() });
const [contract] = await db.select().from(completionContracts).where(eq(completionContracts.issueId, issueId));
const contractInput = buildNativeCompletionContract(issue, { revision: contract.revision });
await db.insert(nativeRunFinalizations).values({ runId, companyId, issueId, phase: "observed" });
const execution: NativeExecutionInputV1 = {
schema: "paperclip.native-execution-input.v1", binding: { companyId, runId, issueId, agentId, executionWorkspaceId: runId },
provider: { kind: "codex", model: "test-model" },
task: { identifier: issueId, title: issue.title, description: null, prompt: "Continue the task using its history.", workMode: "standard" },
workspace: { cwd: "/tmp", repoUrl: null, repoRef: null, branchName: null },
session: { normalizedSessionId: native.normalizedSessionId, driverKind: "codex_app_server", protocolVersion: 1, lifecyclePolicy: { mode: "per_turn", idleTimeoutMs: null } },
completionContract: { id: contract.id, sha256: contract.canonicalSha256, schemaVersion: "paperclip.completion-contract.v1", contract: contractInput },
interactionResponses: [], credentialBindings: [],
};
await db.update(heartbeatRuns).set({ runnerProfileJson: { nativeExecutionInput: execution } }).where(eq(heartbeatRuns.id, runId));
const disposition = "blocker" in report ? "blocked" as const : "done" as const;
const terminal: PrpTerminalState = { schema: "paperclip.prp.terminal.v1", turnTerminalState: "completed", runTerminalState: "succeeded", reportedWorkDisposition: disposition };
const result: PrpStructuredRunResult = "blocker" in report
? { ...CONTROL_PLANE_CONFORMANCE_RESULT, reportedWorkDisposition: "blocked",
summary: "All rounds ran, but every tool call returned 'cryptic', so the ledger could not be read.",
completionClaim: { contractRevision: contractInput.revision, objectiveSatisfied: false,
criteria: contractInput.criteria.map(criterion => ({ criterionId: criterion.id, status: "not_satisfied" as const, evidenceRefs: [] })),
remainingWork: [{ description: "Save the ledger document", blocksCompletion: true }] },
evidence: [], verification: [], attentionRequests: [], blocker: report.blocker }
: { ...CONTROL_PLANE_CONFORMANCE_RESULT, reportedWorkDisposition: "done", summary: "Saved the ledger and posted the comment.",
completionClaim: { contractRevision: contractInput.revision, objectiveSatisfied: true,
criteria: contractInput.criteria.map(criterion => ({ criterionId: criterion.id, status: "satisfied" as const, evidenceRefs: [] })),
remainingWork: [] },
evidence: [], verification: [], attentionRequests: [] };
provider.execute.mockReset().mockImplementation(async (options: { controlPlane: ControlPlanePort }) => {
await options.controlPlane.openRun({ identity: { companyId, issueId, runId, agentId, sessionId: native.normalizedSessionId }, backendKind: "mock", sourceInstanceId: native.runnerInstanceId });
await options.controlPlane.completeRun({ result, terminal, turnId: native.turnId });
return { result, terminal, turnId: native.turnId, normalizedSessionId: native.normalizedSessionId, providerSessionId: "fixture-session", driverKind: "codex_app_server", driverVersion: "fixture", nativeEventCount: 0, highestContiguousSourceSeq: 0, usage: null };
});
const unexpected = async () => { throw new Error("Unexpected provider call"); };
await executePaperclipNativeSession({ db, execution, runnerInstanceId: native.runnerInstanceId, backend: { descriptor: unexpected, openSession: unexpected } });
await beforeFinalize?.();
for (let replay = 0; replay < (expectedPhase === "committed" ? 2 : 1); replay++) {
const finalized = await finalizeNativeRun({ db, runId, workspaceFinalizeStatus: "succeeded", projectRunStatus: true });
expect(finalized?.phase).toBe(expectedPhase);
}
if (expectedPhase === "terminal_failure") await expect(finalizeNativeRun({ db, runId, workspaceFinalizeStatus: "succeeded" }))
.rejects.toThrow("native_finalization_terminal_failure");
const [decision] = await db.select().from(statusDecisions).where(eq(statusDecisions.runId, runId));
const retries = await db.select().from(heartbeatRuns).where(and(eq(heartbeatRuns.companyId, companyId), eq(heartbeatRuns.retryOfRunId, runId)));
const [settled] = await db.select().from(issues).where(eq(issues.id, issueId));
return { runId, decision: decision!, retries, issue: settled! };
}
const ownerWakes = async (issueId: string) => (await db.select().from(agentWakeupRequests).where(eq(agentWakeupRequests.companyId, companyId)))
.filter(wake => wake.payload?.issueId === issueId && wake.requestedByActorId === "native-status-committer");
it.each(["task_wide", "current_track"] as const)("routes a %s human blocker to the named company member through finalization and replay", async scope => {
const issueId = await createTask();
const blocked = await runTurn(issueId, { blocker: { ...selfBlocker, scope,
owner: { kind: "user", name: "Dependency Reviewer" }, unblockAction: "Decide whether to defer this dependency upgrade." } });
expect(blocked.issue).toMatchObject({ status: "blocked", unblockDescriptor: {
owner: { userId: namedUserId }, action: "Decide whether to defer this dependency upgrade." } });
expect(blocked.retries).toEqual([]);
expect(await ownerWakes(issueId)).toEqual([]);
const mine = await attentionService(db).list(companyId, { userId: namedUserId, view: "mine" });
expect(mine.items).toEqual(expect.arrayContaining([expect.objectContaining({
sourceKind: "blocker_attention", subject: expect.objectContaining({ id: issueId }),
audience: expect.objectContaining({ kind: "user", userId: namedUserId }),
})]));
const others = await attentionService(db).list(companyId, { userId: boardUserId, view: "mine" });
expect(others.items.some(item => item.dedupKey.startsWith("blocked-owner:") && item.subject.id === issueId)).toBe(false);
}, 120_000);
it("preserves a typed human unblock owner saved by an API call during the turn", async () => {
const issueId = await createTask();
const blocked = await runTurn(issueId, { blocker: { ...selfBlocker,
owner: { kind: "user", name: "the decision owner" } } }, randomUUID(), async () => {
await db.update(issues).set({ status: "blocked", unblockDescriptor: {
owner: { userId: namedUserId }, action: selfBlocker.unblockAction } }).where(eq(issues.id, issueId));
});
expect(blocked.issue.unblockDescriptor?.owner).toEqual({ userId: namedUserId });
expect(await ownerWakes(issueId)).toEqual([]);
}, 120_000);
it("rebuilds a board assessment when a human owner is bound before the authoritative issue read", async () => {
const issueId = await createTask();
const classify = evidenceClassifier.classifyNativeEvidence;
let bound = false;
const spy = vi.spyOn(evidenceClassifier, "classifyNativeEvidence").mockImplementation(async input => {
const assessment = await classify(input);
if (!bound && input.issueId === issueId) {
bound = true;
await db.update(issues).set({ status: "blocked", statusVersion: 1, unblockDescriptor: {
owner: { userId: namedUserId }, action: selfBlocker.unblockAction } }).where(eq(issues.id, issueId));
}
return assessment;
});
try {
const blocked = await runTurn(issueId, { blocker: { ...selfBlocker, owner: { kind: "user", name: "the decision owner" } } });
expect(bound).toBe(true);
expect(blocked.issue.unblockDescriptor?.owner).toEqual({ userId: namedUserId });
expect(await ownerWakes(issueId)).toEqual([]);
} finally { spy.mockRestore(); }
}, 120_000);
it("rechecks a board fallback when its human owner changes without changing the status version", async () => {
const issueId = await createTask();
await db.update(issues).set({ status: "blocked", unblockDescriptor: {
owner: "board", action: selfBlocker.unblockAction } }).where(eq(issues.id, issueId));
const commit = statusCommitter.commitNativeStatusDecision;
let rebound = false;
const spy = vi.spyOn(statusCommitter, "commitNativeStatusDecision").mockImplementation(async input => {
if (!rebound && input.issueId === issueId && input.decision.unblockDescriptor?.owner === "board") {
rebound = true;
await db.update(issues).set({ unblockDescriptor: {
owner: { userId: namedUserId }, action: selfBlocker.unblockAction } }).where(eq(issues.id, issueId));
}
return commit(input);
});
try {
const blocked = await runTurn(issueId, { blocker: { ...selfBlocker, owner: { kind: "user", name: "the decision owner" } } });
expect(rebound).toBe(true);
expect(blocked.issue.unblockDescriptor?.owner).toEqual({ userId: namedUserId });
expect(await ownerWakes(issueId)).toEqual([]);
} finally { spy.mockRestore(); }
}, 120_000);
it("keeps an unknown human blocker with the board instead of assigning the task creator", async () => {
const blocked = await runTurn(await createTask(), { blocker: { ...selfBlocker,
owner: { kind: "user", name: "Unknown Reviewer" } } });
expect(blocked.issue.unblockDescriptor?.owner).toBe("board");
expect(blocked.retries).toEqual([]);
}, 120_000);
it.each([false, true])("refuses human blocker routing when the agent cannot assign tasks to humans (dependency: %s)", async dependency => {
const issueId = await createTask();
if (dependency) await db.insert(issueRelations).values({ companyId, issueId: await createTask(), relatedIssueId: issueId, type: "blocks" });
await db.update(agents).set({ permissions: { interactions: { canAssignTasksToHumans: false } } }).where(eq(agents.id, agentId));
try {
const blocked = await runTurn(issueId, { blocker: { ...selfBlocker,
owner: { kind: "user", name: "Dependency Reviewer" } } }, undefined, undefined, "terminal_failure");
expect(blocked.issue.unblockDescriptor).toBeNull();
expect(blocked.decision).toBeUndefined();
const [finalized] = await db.select().from(nativeRunFinalizations).where(eq(nativeRunFinalizations.runId, blocked.runId));
expect(finalized).toMatchObject({ phase: "terminal_failure", failureCode: "agent_interaction_denied", nextAttemptAt: null });
const feed = await attentionService(db).list(companyId, { userId: namedUserId, view: "mine" });
expect(feed.items.some(item => item.dedupKey.startsWith("blocked-owner:") && item.subject.id === issueId)).toBe(false);
} finally { await db.update(agents).set({ permissions: {} }).where(eq(agents.id, agentId)); }
}, 120_000);
it.each(["Foreign Reviewer", "Inactive Reviewer", "Shared Reviewer"])("does not route a human blocker through an unsafe name: %s", async name => {
const blocked = await runTurn(await createTask(), { blocker: { ...selfBlocker, owner: { kind: "user", name } } });
expect(blocked.issue.unblockDescriptor?.owner).toBe("board");
}, 120_000);
it("uses an explicit opaque member ID and does not guess a replacement for an invalid ID", async () => {
for (const userId of [namedUserId, "foreign-reviewer", "inactive-reviewer", "", 42]) {
const blocked = await runTurn(await createTask(), { blocker: { ...selfBlocker,
owner: { kind: "user", name: "Dependency Reviewer", ...{ userId } } } });
expect(blocked.issue.unblockDescriptor?.owner).toEqual(userId === namedUserId ? { userId: namedUserId } : "board");
}
}, 120_000);
it("reclassifies a human owner whose membership is revoked between assessment and commit", async () => {
const commit = statusCommitter.commitNativeStatusDecision;
let revoked = false;
const spy = vi.spyOn(statusCommitter, "commitNativeStatusDecision").mockImplementation(async input => {
if (!revoked && typeof input.decision.unblockDescriptor?.owner === "object"
&& "userId" in input.decision.unblockDescriptor.owner) {
revoked = true;
await db.update(companyMemberships).set({ status: "inactive" }).where(and(
eq(companyMemberships.companyId, companyId), eq(companyMemberships.principalId, namedUserId)));
}
return commit(input);
});
try {
const blocked = await runTurn(await createTask(), { blocker: { ...selfBlocker,
owner: { kind: "user", name: "Dependency Reviewer" } } });
expect(revoked).toBe(true);
expect(blocked.issue.unblockDescriptor?.owner).toBe("board");
} finally {
spy.mockRestore();
await db.update(companyMemberships).set({ status: "active" }).where(and(
eq(companyMemberships.companyId, companyId), eq(companyMemberships.principalId, namedUserId)));
}
}, 120_000);
it("continues a tool-output blocker in a fresh session, and the continuation finishes the task", async () => {
const issueId = await createTask();
const first = await runTurn(issueId, { blocker: toolOutputBlocker });
expect(first.decision.reasonCode).toBe(NATIVE_ENVIRONMENT_BLOCKER_CODE);
expect(first.issue).toMatchObject({ status: "in_progress", unblockDescriptor: null });
expect(first.retries).toHaveLength(1);
expect(first.retries[0]).toMatchObject({ status: "scheduled_retry", scheduledRetryReason: NATIVE_ENVIRONMENT_BLOCKER_CODE,
error: NATIVE_ENVIRONMENT_BLOCKER_MESSAGE,
contextSnapshot: { forceFreshSession: true, livenessContinuationInstruction: NATIVE_ENVIRONMENT_BLOCKER_HINT } });
expect(NATIVE_ENVIRONMENT_BLOCKER_MESSAGE).not.toContain("unavailable");
expect(NATIVE_ENVIRONMENT_BLOCKER_HINT).toContain("Check whether that blocker still applies");
expect(await ownerWakes(issueId)).toEqual([]);
const second = await runTurn(issueId, { done: true }, first.retries[0]!.id);
expect(second.issue.status).toBe("done");
}, 120_000);
it("continues a blocker the agent owns once, then hands a repeated one to a person instead of retrying again", async () => {
const issueId = await createTask();
const first = await runTurn(issueId, { blocker: selfBlocker });
expect(first.decision.reasonCode).toBe(NATIVE_AGENT_BLOCKER_CONTINUATION_CODE);
expect(first.issue).toMatchObject({ status: "in_progress", unblockDescriptor: null });
expect(first.retries).toHaveLength(1);
expect(first.retries[0]).toMatchObject({ status: "scheduled_retry", scheduledRetryReason: NATIVE_AGENT_BLOCKER_CONTINUATION_CODE,
scheduledRetryAttempt: 1, contextSnapshot: { forceFreshSession: true, livenessContinuationInstruction: NATIVE_AGENT_BLOCKER_HINT } });
expect(await taskInterruptionPresentation(db, first.issue)).toMatchObject({ canRetry: false });
const second = await runTurn(issueId, { blocker: selfBlocker }, first.retries[0]!.id);
expect(second.decision.reasonCode).toBe(NATIVE_AGENT_BLOCKER_HANDOFF_CODE);
expect(second.retries).toHaveLength(0);
expect(second.issue).toMatchObject({ status: "blocked", unblockDescriptor: { owner: "board", action: selfBlocker.unblockAction } });
expect(await ownerWakes(issueId)).toEqual([]);
}, 120_000);
it("hands a system blocker that needs a credential to a person without automatic retries", async () => {
const issueId = await createTask();
const blocked = await runTurn(issueId, { blocker: { ...toolOutputBlocker, reasonCode: "credential_missing",
unblockAction: "Connect the deploy account." } });
expect(blocked.decision.reasonCode).toBe(NATIVE_AGENT_BLOCKER_HANDOFF_CODE);
expect(blocked.retries).toHaveLength(0);
expect(blocked.issue).toMatchObject({ status: "blocked", unblockDescriptor: { owner: "board", action: "Connect the deploy account." } });
}, 120_000);
it("hands a blocker that only a person can resolve to the board with a Retry that starts the task again", async () => {
const issueId = await createTask();
const blocked = await runTurn(issueId, { blocker: { ...selfBlocker, owner: { kind: "external", name: "deploy key holder" },
reasonCode: "credential_missing", unblockAction: "Add the deploy key to the project." } });
expect(blocked.decision.reasonCode).toBe(NATIVE_AGENT_BLOCKER_HANDOFF_CODE);
expect(blocked.retries).toHaveLength(0);
expect(blocked.issue).toMatchObject({ status: "blocked", unblockDescriptor: { owner: "board", action: "Add the deploy key to the project." } });
expect(await ownerWakes(issueId)).toEqual([]);
expect(await taskInterruptionPresentation(db, blocked.issue)).toMatchObject({ responsibility: "user", state: "waiting_for_user",
userAction: "unblock", canRetry: true, observedRunId: blocked.runId });
const receipt = await requestTaskRetry({ db, companyId, issueId, actorId: boardUserId, actorSource: "session",
idempotencyKey: randomUUID(), observedRunId: blocked.runId });
expect(receipt).toMatchObject({ accepted: true, stale: false });
const heartbeat = heartbeatService(db);
await heartbeat.resumeTaskRetry(companyId, receipt.requestId!);
const retried = (await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.agentId, agentId)))
.filter(run => run.contextSnapshot?.issueId === issueId && run.contextSnapshot?.taskRetryRequestId === receipt.requestId);
expect(retried).toHaveLength(1);
await heartbeat.drainActiveRunExecutions();
}, 120_000);
});