mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-04 12:50:37 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work. > - An agent can ask a person a question on a task. The saved answer wakes the agent. > - Dependencies stop task work until the blocker tasks finish. #20363 lets a verified human answer reach the assignee in a bounded turn while blockers stay open. That turn does not check out the task. > - Run preparation used the bounded path only when the task status was `blocked`. A task can wait in review (`in_review`) and still have open blockers. > - For such a task, dispatch admitted the answer turn. Then preparation tried the normal resolved-interaction checkout. The checkout refused the run with "Issue is blocked by unresolved blockers" (HTTP 422). > - The run failed `setup_failed`. The answer did not reach the agent, and Sentry received the refusal as an unexpected run failure. > - This pull request keeps a verified answer in the bounded turn whenever the task still has open blockers. > - The benefit is that the person's answer reaches the agent, and the task and its blockers do not change. ## Linked Issues or Issue Description Refs #20363 (deliver saved answers to dependency-blocked tasks). This change covers the same answer on a task that is not in `blocked` status. **What happened?** A parent task waited in review. Some of its child blocker tasks were still blocked. The assignee had asked a question on the parent. A person answered it. Dispatch admitted the answer turn with `dependencyBlockedInteraction: true`. Run preparation then called the resolved-interaction checkout, because the task status was `in_review`, not `blocked`. The checkout dependency check threw "Issue is blocked by unresolved blockers". The run failed `setup_failed` about one second after it started. The adapter did not run. Sentry received the error. **Expected behavior** The saved answer reaches the assignee as a bounded answer turn. The task stays `in_review`. Its blockers and dependency edges do not change. When the last blocker has already finished, the task checks out normally, as before. **Steps to reproduce** 1. Assign a task to an agent and set its status to `in_review`. 2. Add a `backlog` task that blocks it. 3. Save an agent question on the task and answer it as a person. 4. Dispatch the resolved-interaction wake. 5. Observe that the run fails `setup_failed` with "Issue is blocked by unresolved blockers". **Paperclip version or commit** Seen on a hosted staging server. The new regression case fails on `master` ata722935dbcand passes with this change. ## What Changed - `server/src/services/heartbeat.ts`: run preparation treats a verified dependency-blocked answer as a bounded turn without checkout when the task is `blocked` (unchanged) or when its dependencies are still not ready. Before this change, only the `blocked` status qualified. - The answer still needs the same proof as before: the stored interaction is human-resolved, belongs to this task and agent, and matches the wake's status and kind. - `server/src/__tests__/heartbeat-dependency-scheduling.test.ts`: the existing saved-response matrix gets two cases: - `in_review_answered`: a reviewed task with an open blocker receives the answer once and stays `in_review`. - `in_review_blocker_finishes`: when the blocker finishes before preparation, the reviewed task checks out normally and becomes `in_progress`. The answer is delivered once. ## Verification - Red before the fix, green after: `delivers only a saved human response while dependencies remain blocked: in_review_answered`. Before the fix the run ended `{ status: "failed", errorCode: "setup_failed", error: "Issue is blocked by unresolved blockers" }`, which is the staging failure. After the fix it succeeds. - Safety case `in_review_blocker_finishes` passes before and after the fix. A reviewed task with no open blockers keeps its normal checkout. - The existing cases still pass. They cover a pending, missing, wrong-task, or agent-resolved response that stays blocked, a response that changes before dispatch, workspace waits, and a blocked task whose blocker finishes. - Commands (local, Node 25, embedded Postgres, branch at `master`a722935dbcplus this change): - `pnpm --filter @paperclipai/server exec vitest run src/__tests__/heartbeat-dependency-scheduling.test.ts src/__tests__/heartbeat-agent-directory-busy.test.ts src/modules/run-dispatch/domain/wake-context.test.ts src/modules/run-dispatch/domain/policy.test.ts src/__tests__/deferred-execution-receipt-work.test.ts src/services/explicit-native-continuation.test.ts` (613 passed) - `pnpm --filter @paperclipai/server exec vitest run src/__tests__/heartbeat-clean-continuation-dispatch.test.ts src/__tests__/heartbeat-isolated-continuation-hold.test.ts src/__tests__/issue-thread-interactions-service.test.ts src/__tests__/agent-interaction-acceptance.test.ts` (148 passed) - `pnpm --filter @paperclipai/server typecheck` (passed) ## Risks - Low risk. The change applies only to a wake that dispatch already admitted as a verified dependency-blocked answer, and only while the task still has open blockers. Before this change, that run always failed at checkout. - The bounded turn does not check out the task, change its status, or resolve a blocker. Pause, ownership, approval, budget, and execution gates are unchanged. - No migration. No change to the API or to stored data. ## Model Used - Claude Opus 5.5 (`claude-opus-5-5`), through Claude Code with tool use (shell, file edit, 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 (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 - [ ] I have updated relevant documentation to reflect my changes (no doc change: the code comment and tests record the contract) - [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: paperclipbugbot <356415228+paperclipbugbot@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1808 lines
79 KiB
TypeScript
1808 lines
79 KiB
TypeScript
import { randomUUID } from "node:crypto";
|
|
import { and, eq, inArray, sql } from "drizzle-orm";
|
|
import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from "vitest";
|
|
import {
|
|
costEvents,
|
|
activityLog,
|
|
agents,
|
|
agentRuntimeState,
|
|
agentWakeupRequests,
|
|
companySkills,
|
|
completionContracts,
|
|
nativeRunResults,
|
|
nativeRunFinalizations,
|
|
workAssessments,
|
|
statusDecisions,
|
|
statusDecisionEffects,
|
|
companies,
|
|
authUsers,
|
|
companyMemberships,
|
|
createDb,
|
|
documentRevisions,
|
|
documents,
|
|
environmentLeases,
|
|
environments,
|
|
executionWorkspaces,
|
|
heartbeatRunEvents,
|
|
heartbeatRuns,
|
|
issueComments,
|
|
issueDocuments,
|
|
issueRelations,
|
|
issueRecoveryActions,
|
|
issueThreadInteractions,
|
|
issueTreeHolds,
|
|
issues,
|
|
workspaceOperations,
|
|
} from "@paperclipai/db";
|
|
import {
|
|
getEmbeddedPostgresTestSupport,
|
|
startEmbeddedPostgresTestDatabase,
|
|
} from "./helpers/embedded-postgres.js";
|
|
import { resolveExternalTestDatabaseUrl } from "./helpers/external-test-database.js";
|
|
import { heartbeatService } from "../services/heartbeat.ts";
|
|
import { runningProcesses } from "../adapters/index.ts";
|
|
import { createPostgresRunDispatchAdapter } from "../modules/run-dispatch/adapters/postgres.js";
|
|
import { taskInterruptionPresentation } from "../services/task-interruption-presentation.js";
|
|
import * as workspacePreparation from "../services/heartbeat/workspace-preparation.js";
|
|
import { WorkspaceBusyDeferral } from "../services/heartbeat/retries.js";
|
|
|
|
const mockAdapterExecute = vi.hoisted(() =>
|
|
vi.fn(async () => ({
|
|
exitCode: 0,
|
|
signal: null,
|
|
timedOut: false,
|
|
errorMessage: null,
|
|
summary: "Dependency-aware heartbeat test run.",
|
|
provider: "test",
|
|
model: "test-model",
|
|
})),
|
|
);
|
|
|
|
vi.mock("../adapters/index.ts", async () => {
|
|
const actual = await vi.importActual<typeof import("../adapters/index.ts")>("../adapters/index.ts");
|
|
return {
|
|
...actual,
|
|
getServerAdapter: vi.fn(() => ({
|
|
supportsLocalAgentJwt: false,
|
|
execute: mockAdapterExecute,
|
|
})),
|
|
};
|
|
});
|
|
|
|
const external = resolveExternalTestDatabaseUrl("PAPERCLIP_DEPENDENCY_TEST_DATABASE_URL");
|
|
const embeddedPostgresSupport = external ? { supported: true, reason: undefined } : await getEmbeddedPostgresTestSupport();
|
|
const describeEmbeddedPostgres = embeddedPostgresSupport.supported ? describe : describe.skip;
|
|
|
|
if (!embeddedPostgresSupport.supported) {
|
|
console.warn(
|
|
`Skipping embedded Postgres heartbeat dependency scheduling tests on this host: ${embeddedPostgresSupport.reason ?? "unsupported environment"}`,
|
|
);
|
|
}
|
|
|
|
async function ensureIssueRelationsTable(db: ReturnType<typeof createDb>) {
|
|
await db.execute(sql.raw(`
|
|
CREATE TABLE IF NOT EXISTS "issue_relations" (
|
|
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
"company_id" uuid NOT NULL,
|
|
"issue_id" uuid NOT NULL,
|
|
"related_issue_id" uuid NOT NULL,
|
|
"type" text NOT NULL,
|
|
"created_by_agent_id" uuid,
|
|
"created_by_user_id" text,
|
|
"created_at" timestamptz NOT NULL DEFAULT now(),
|
|
"updated_at" timestamptz NOT NULL DEFAULT now()
|
|
);
|
|
`));
|
|
}
|
|
|
|
async function waitForCondition(fn: () => Promise<boolean>, timeoutMs = 3_000) {
|
|
const deadline = Date.now() + timeoutMs;
|
|
while (Date.now() < deadline) {
|
|
if (await fn()) return true;
|
|
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
}
|
|
return fn();
|
|
}
|
|
|
|
describeEmbeddedPostgres("heartbeat dependency-aware queued run selection", () => {
|
|
let db!: ReturnType<typeof createDb>;
|
|
let heartbeat!: ReturnType<typeof heartbeatService>;
|
|
let tempDb: Awaited<ReturnType<typeof startEmbeddedPostgresTestDatabase>> | null = null;
|
|
|
|
beforeAll(async () => {
|
|
if (!external) tempDb = await startEmbeddedPostgresTestDatabase("paperclip-heartbeat-dependency-scheduling-");
|
|
db = createDb(external ?? tempDb!.connectionString);
|
|
await db.insert(authUsers).values({ id: "responsible-user", name: "Fixture owner", email: "owner@example.test", createdAt: new Date(), updatedAt: new Date() });
|
|
heartbeat = heartbeatService(db);
|
|
await ensureIssueRelationsTable(db);
|
|
}, 20_000);
|
|
|
|
afterEach(async () => {
|
|
let idlePolls = 0;
|
|
for (let attempt = 0; attempt < 100; attempt += 1) {
|
|
const runs = await db
|
|
.select({ status: heartbeatRuns.status })
|
|
.from(heartbeatRuns);
|
|
const hasActiveRun = runs.some((run) => run.status === "queued" || run.status === "running");
|
|
if (!hasActiveRun) {
|
|
idlePolls += 1;
|
|
if (idlePolls >= 3) break;
|
|
} else {
|
|
idlePolls = 0;
|
|
}
|
|
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
}
|
|
await heartbeat.drainActiveRunExecutions();
|
|
vi.restoreAllMocks();
|
|
mockAdapterExecute.mockReset();
|
|
mockAdapterExecute.mockImplementation(async () => ({
|
|
exitCode: 0,
|
|
signal: null,
|
|
timedOut: false,
|
|
errorMessage: null,
|
|
summary: "Dependency-aware heartbeat test run.",
|
|
provider: "test",
|
|
model: "test-model",
|
|
}));
|
|
runningProcesses.clear();
|
|
await db.delete(environmentLeases);
|
|
await db.delete(activityLog);
|
|
await db.delete(companySkills);
|
|
await db.delete(issueComments);
|
|
await db.delete(issueDocuments);
|
|
await db.delete(documentRevisions);
|
|
await db.delete(documents);
|
|
await db.delete(issueRelations);
|
|
await db.delete(issueTreeHolds);
|
|
await db.delete(statusDecisionEffects);
|
|
await db.delete(statusDecisions);
|
|
await db.delete(workAssessments);
|
|
await db.delete(nativeRunFinalizations);
|
|
await db.delete(nativeRunResults);
|
|
await db.delete(completionContracts);
|
|
await db.delete(issues);
|
|
await db.delete(heartbeatRunEvents);
|
|
await db.delete(activityLog);
|
|
await db.delete(costEvents);
|
|
await db.delete(heartbeatRuns);
|
|
await db.delete(agentWakeupRequests);
|
|
await db.delete(agentRuntimeState);
|
|
await db.delete(agents);
|
|
await db.delete(companySkills);
|
|
await db.delete(environments);
|
|
await db.delete(workspaceOperations);
|
|
await db.delete(executionWorkspaces);
|
|
await db.delete(environmentLeases);
|
|
for (let attempt = 0; attempt < 5; attempt += 1) {
|
|
try {
|
|
await db.transaction(async (tx) => {
|
|
await tx.delete(companySkills);
|
|
await tx.delete(companyMemberships);
|
|
await tx.delete(companies);
|
|
});
|
|
break;
|
|
} catch (error) {
|
|
if (attempt === 4) throw error;
|
|
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
}
|
|
}
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await db?.delete(authUsers).where(eq(authUsers.id, "responsible-user"));
|
|
await db?.$client.end();
|
|
await tempDb?.cleanup();
|
|
});
|
|
|
|
it("delivers a revised native child completion after the running parent blocks", async () => {
|
|
const companyId = randomUUID();
|
|
const agentId = randomUUID();
|
|
const issueId = randomUUID();
|
|
const childId = randomUUID();
|
|
const decisionId = randomUUID();
|
|
let finishParent!: () => void;
|
|
const parentFinished = new Promise<void>((resolve) => { finishParent = resolve; });
|
|
mockAdapterExecute.mockImplementationOnce(async () => {
|
|
await parentFinished;
|
|
await db.update(issues).set({ status: "blocked" }).where(eq(issues.id, issueId));
|
|
return { exitCode: 0, signal: null, timedOut: false, errorMessage: null,
|
|
summary: "Waiting for the child revision.", provider: "test", model: "test-model" };
|
|
});
|
|
mockAdapterExecute.mockImplementationOnce(async () => {
|
|
const [run] = await db.select().from(heartbeatRuns).where(and(
|
|
eq(heartbeatRuns.companyId, companyId), eq(heartbeatRuns.status, "running"),
|
|
));
|
|
await db.insert(issueComments).values({ companyId, issueId, authorAgentId: agentId,
|
|
authorType: "agent", createdByRunId: run!.id, body: "Reviewed and delivered the revised ZIP." });
|
|
await db.update(issues).set({ status: "done" }).where(eq(issues.id, issueId));
|
|
return { exitCode: 0, signal: null, timedOut: false, errorMessage: null,
|
|
summary: "Reviewed and delivered the revised ZIP.", provider: "test", model: "test-model" };
|
|
});
|
|
await db.insert(companies).values({ id: companyId, name: "Native handoff",
|
|
issuePrefix: `N${companyId.replace(/-/g, "").slice(0, 6).toUpperCase()}`,
|
|
requireBoardApprovalForNewAgents: false, defaultResponsibleUserId: "responsible-user" });
|
|
await db.insert(companyMemberships).values({ companyId, principalType: "user", principalId: "responsible-user", status: "active", membershipRole: "member" });
|
|
await db.insert(agents).values({ id: agentId, companyId, name: "Parent", role: "engineer",
|
|
status: "active", adapterType: "codex_local", adapterConfig: {}, permissions: {},
|
|
runtimeConfig: { heartbeat: { wakeOnDemand: true, maxConcurrentRuns: 1 } } });
|
|
await db.insert(issues).values([
|
|
{ id: issueId, companyId, title: "Deliver the revised ZIP", status: "todo",
|
|
assigneeAgentId: agentId, responsibleUserId: "responsible-user" },
|
|
{ id: childId, companyId, parentId: issueId, title: "Revise the ZIP", status: "done",
|
|
responsibleUserId: "responsible-user" },
|
|
]);
|
|
try {
|
|
const parentRun = await heartbeat.wakeup(agentId, { source: "assignment", triggerDetail: "system",
|
|
reason: "issue_assigned", payload: { issueId }, contextSnapshot: { issueId, wakeReason: "issue_assigned" } });
|
|
expect(parentRun).not.toBeNull();
|
|
await db.insert(issueComments).values({ companyId, issueId, authorAgentId: agentId,
|
|
authorType: "agent", createdByRunId: parentRun!.id, body: "Waiting for the child revision." });
|
|
expect(await waitForCondition(async () => mockAdapterExecute.mock.calls.length === 1, 30_000)).toBe(true);
|
|
const [intent] = await db.insert(agentWakeupRequests).values({ companyId, agentId,
|
|
source: "automation", triggerDetail: "system", reason: "issue_children_completed",
|
|
requestedByActorType: "system", requestedByActorId: "native-status-committer",
|
|
idempotencyKey: `issue_children_completed:${issueId}:${childId}:${decisionId}`,
|
|
payload: { issueId, taskId: issueId, _paperclipWakeContext: {
|
|
issueId, taskId: issueId, source: "native_status_decision",
|
|
nativeChildCompletionDecisionId: decisionId, completedChildIssueId: childId,
|
|
childIssueIds: [childId], childIssueSummaries: [{ id: childId, summary: "Revised ZIP: 11 tests." }],
|
|
} },
|
|
}).returning();
|
|
await heartbeat.dispatchPendingNativeStatusWakeups({ companyId });
|
|
await heartbeat.dispatchPendingNativeStatusWakeups({ companyId });
|
|
const deferred = await db.select().from(agentWakeupRequests).where(and(
|
|
eq(agentWakeupRequests.companyId, companyId), eq(agentWakeupRequests.status, "deferred_issue_execution"),
|
|
));
|
|
expect(deferred).toHaveLength(1);
|
|
expect(deferred[0]!.runId).toBeNull();
|
|
expect(mockAdapterExecute).toHaveBeenCalledTimes(1);
|
|
finishParent();
|
|
expect(await waitForCondition(async () => mockAdapterExecute.mock.calls.length === 2, 10_000)).toBe(true);
|
|
await heartbeat.drainActiveRunExecutions();
|
|
const runs = await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.companyId, companyId));
|
|
expect(runs.map((run) => ({ id: run.id, status: run.status, context: run.contextSnapshot, error: run.error }))).toHaveLength(2);
|
|
const continuation = runs.find((run) => run.id !== parentRun!.id)!;
|
|
expect(continuation.contextSnapshot).toMatchObject({
|
|
wakeReason: "issue_children_completed", nativeChildCompletionDecisionId: decisionId,
|
|
nativeStatusWakeIntentId: intent!.id, completedChildIssueId: childId,
|
|
childIssueSummaries: [{ id: childId, summary: "Revised ZIP: 11 tests." }],
|
|
});
|
|
expect(continuation.status).toBe("succeeded");
|
|
const first = runs.find((run) => run.id === parentRun!.id)!;
|
|
expect(continuation.startedAt!.getTime()).toBeGreaterThanOrEqual(first.finishedAt!.getTime());
|
|
await heartbeat.dispatchPendingNativeStatusWakeups({ companyId });
|
|
expect(await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.companyId, companyId))).toHaveLength(2);
|
|
} finally {
|
|
finishParent();
|
|
}
|
|
}, 45_000);
|
|
|
|
it("runs the parent's children-completed wake when a child it bound after completion saved after its finalize", async () => {
|
|
const companyId = randomUUID();
|
|
const agentId = randomUUID();
|
|
const parentId = randomUUID();
|
|
const firstChildId = randomUUID();
|
|
const secondChildId = randomUUID();
|
|
const firstWorkspaceId = randomUUID();
|
|
const secondWorkspaceId = randomUUID();
|
|
const firstRunId = randomUUID();
|
|
const secondRunId = randomUUID();
|
|
const decisionId = randomUUID();
|
|
await db.insert(companies).values({ id: companyId, name: "Child completion",
|
|
issuePrefix: `C${companyId.replace(/-/g, "").slice(0, 6).toUpperCase()}`,
|
|
requireBoardApprovalForNewAgents: false, defaultResponsibleUserId: "responsible-user" });
|
|
await db.insert(companyMemberships).values({ companyId, principalType: "user", principalId: "responsible-user", status: "active", membershipRole: "member" });
|
|
await db.insert(agents).values({ id: agentId, companyId, name: "Parent", role: "engineer",
|
|
status: "active", adapterType: "codex_local", adapterConfig: {}, permissions: {},
|
|
runtimeConfig: { heartbeat: { wakeOnDemand: true, maxConcurrentRuns: 1 } } });
|
|
await db.insert(executionWorkspaces).values([firstWorkspaceId, secondWorkspaceId].map((id) => ({
|
|
id, companyId, mode: "shared_workspace", strategyType: "project_primary", name: "Child workspace", status: "active",
|
|
})));
|
|
await db.insert(issues).values([
|
|
{ id: parentId, companyId, title: "Parent", status: "in_progress",
|
|
assigneeAgentId: agentId, responsibleUserId: "responsible-user" },
|
|
{ id: firstChildId, companyId, parentId, title: "First child", status: "done",
|
|
assigneeAgentId: agentId, executionWorkspaceId: firstWorkspaceId, responsibleUserId: "responsible-user",
|
|
completedAt: new Date("2026-11-16T23:33:00.000Z") },
|
|
{ id: secondChildId, companyId, parentId, title: "Second child", status: "in_progress",
|
|
assigneeAgentId: agentId, executionWorkspaceId: secondWorkspaceId, responsibleUserId: "responsible-user" },
|
|
]);
|
|
await db.insert(heartbeatRuns).values([
|
|
{ id: firstRunId, companyId, agentId, issueId: firstChildId, status: "succeeded",
|
|
startedAt: new Date("2026-11-16T23:28:28.000Z"), finishedAt: new Date("2026-11-16T23:32:59.430Z") },
|
|
{ id: secondRunId, companyId, agentId, issueId: secondChildId, status: "running",
|
|
startedAt: new Date("2026-11-16T23:31:17.000Z") },
|
|
]);
|
|
await db.insert(workspaceOperations).values([
|
|
{ companyId, executionWorkspaceId: firstWorkspaceId, issueId: firstChildId, heartbeatRunId: firstRunId,
|
|
phase: "workspace_finalize", status: "succeeded",
|
|
startedAt: new Date("2026-11-16T23:32:57.000Z"), finishedAt: new Date("2026-11-16T23:32:59.000Z") },
|
|
{ companyId, executionWorkspaceId: firstWorkspaceId, issueId: firstChildId, heartbeatRunId: firstRunId,
|
|
phase: "remote_workspace_snapshot", status: "succeeded",
|
|
startedAt: new Date("2026-11-16T23:32:58.000Z"), finishedAt: new Date("2026-11-16T23:32:59.200Z") },
|
|
]);
|
|
await db.insert(issueRelations).values([firstChildId, secondChildId].map((blockerId) => ({
|
|
companyId, issueId: blockerId, relatedIssueId: parentId, type: "blocks" as const, createdByAgentId: agentId,
|
|
})));
|
|
await db.update(issues).set({ status: "blocked", blockedTransitionAt: new Date("2026-11-16T23:34:52.374Z") })
|
|
.where(eq(issues.id, parentId));
|
|
await db.insert(workspaceOperations).values({ companyId, executionWorkspaceId: secondWorkspaceId,
|
|
issueId: secondChildId, heartbeatRunId: secondRunId, phase: "workspace_finalize", status: "succeeded",
|
|
startedAt: new Date("2026-11-16T23:35:20.000Z"), finishedAt: new Date("2026-11-16T23:35:22.000Z") });
|
|
await db.update(heartbeatRuns).set({ status: "succeeded", finishedAt: new Date("2026-11-16T23:35:23.250Z") })
|
|
.where(eq(heartbeatRuns.id, secondRunId));
|
|
await db.update(issues).set({ status: "done", completedAt: new Date("2026-11-16T23:35:23.218Z") })
|
|
.where(eq(issues.id, secondChildId));
|
|
const [intent] = await db.insert(agentWakeupRequests).values({ companyId, agentId,
|
|
source: "automation", triggerDetail: "system", reason: "issue_children_completed",
|
|
requestedByActorType: "system", requestedByActorId: "native-status-committer",
|
|
idempotencyKey: `issue_children_completed:${parentId}:${secondChildId}:${decisionId}`,
|
|
payload: { issueId: parentId, taskId: parentId, _paperclipWakeContext: {
|
|
issueId: parentId, taskId: parentId, source: "native_status_decision",
|
|
nativeChildCompletionDecisionId: decisionId, completedChildIssueId: secondChildId,
|
|
childIssueIds: [firstChildId, secondChildId],
|
|
} },
|
|
}).returning();
|
|
|
|
await heartbeat.dispatchPendingNativeStatusWakeups({ companyId });
|
|
expect(await waitForCondition(async () => mockAdapterExecute.mock.calls.length === 1, 10_000)).toBe(true);
|
|
await heartbeat.drainActiveRunExecutions();
|
|
|
|
const dependencyWaits = await db.select().from(agentWakeupRequests).where(and(
|
|
eq(agentWakeupRequests.companyId, companyId), eq(agentWakeupRequests.reason, "issue_dependencies_blocked"),
|
|
));
|
|
expect(dependencyWaits).toEqual([]);
|
|
const parentRuns = await db.select().from(heartbeatRuns).where(and(
|
|
eq(heartbeatRuns.companyId, companyId), sql`${heartbeatRuns.contextSnapshot}->>'issueId' = ${parentId}`,
|
|
));
|
|
const childrenCompletedRun = parentRuns.find((run) => run.contextSnapshot?.nativeStatusWakeIntentId === intent!.id);
|
|
expect(childrenCompletedRun?.contextSnapshot).toMatchObject({
|
|
wakeReason: "issue_children_completed", nativeStatusWakeIntentId: intent!.id,
|
|
completedChildIssueId: secondChildId,
|
|
});
|
|
}, 45_000);
|
|
|
|
it("keeps a saved native continuation behind its active predecessor without retagging that run", async () => {
|
|
const companyId = randomUUID(), agentId = randomUUID(), issueId = randomUUID();
|
|
let finishPredecessor!: () => void;
|
|
const predecessorFinished = new Promise<void>(resolve => { finishPredecessor = resolve; });
|
|
mockAdapterExecute.mockImplementationOnce(async () => {
|
|
await predecessorFinished;
|
|
return { exitCode: 0, signal: null, timedOut: false, errorMessage: null,
|
|
summary: "The repository is staged; continue on the saved admission.", provider: "test", model: "test-model" };
|
|
});
|
|
mockAdapterExecute.mockImplementationOnce(async () => {
|
|
await db.update(issues).set({ status: "done" }).where(eq(issues.id, issueId));
|
|
return { exitCode: 0, signal: null, timedOut: false, errorMessage: null,
|
|
summary: "Verified and delivered the prepared repository.", provider: "test", model: "test-model" };
|
|
});
|
|
await db.insert(companies).values({ id: companyId, name: "Saved repository continuation",
|
|
issuePrefix: `N${companyId.replace(/-/g, "").slice(0, 6).toUpperCase()}`,
|
|
requireBoardApprovalForNewAgents: false, defaultResponsibleUserId: "responsible-user" });
|
|
await db.insert(companyMemberships).values({ companyId, principalType: "user", principalId: "responsible-user", status: "active", membershipRole: "member" });
|
|
await db.insert(agents).values({ id: agentId, companyId, name: "Repository worker", role: "engineer", status: "active",
|
|
adapterType: "codex_local", adapterConfig: {}, permissions: {}, runtimeConfig: { heartbeat: { wakeOnDemand: true, maxConcurrentRuns: 1 } } });
|
|
await db.insert(issues).values({ id: issueId, companyId, title: "Continue repository verification", status: "todo",
|
|
assigneeAgentId: agentId, responsibleUserId: "responsible-user" });
|
|
try {
|
|
const predecessor = await heartbeat.wakeup(agentId, { source: "assignment", triggerDetail: "system", reason: "issue_assigned",
|
|
payload: { issueId }, contextSnapshot: { issueId, source: "initial-repository-request" } });
|
|
expect(predecessor).not.toBeNull();
|
|
expect(await waitForCondition(async () => mockAdapterExecute.mock.calls.length === 1, 30_000)).toBe(true);
|
|
const contractId = randomUUID(), resultId = randomUUID(), assessmentId = randomUUID(), decisionId = randomUUID();
|
|
await db.insert(completionContracts).values({ id: contractId, companyId, issueId, revision: 1,
|
|
schemaVersion: "paperclip.completion-contract.v1", policyVersion: "test", risk: "standard",
|
|
completionAuthority: "server_arbiter", incompleteCriteriaPolicy: "preserve_non_terminal",
|
|
contractJson: {}, canonicalSha256: contractId, createdByActorType: "system", createdByActorId: "test" });
|
|
await db.update(heartbeatRuns).set({ nativeIssueId: issueId, completionContractId: contractId }).where(eq(heartbeatRuns.id, predecessor!.id));
|
|
await db.insert(nativeRunResults).values({ id: resultId, companyId, issueId, runId: predecessor!.id,
|
|
completionContractId: contractId, serverFingerprint: resultId, schemaStatus: "accepted", resultJson: {}, canonicalSha256: resultId });
|
|
await db.insert(workAssessments).values({ id: assessmentId, companyId, issueId, runId: predecessor!.id,
|
|
contractId, resultId, triggerKind: "turn_finished", triggerActorCompanyId: companyId,
|
|
priorIssueStatus: "in_progress", priorStatusVersion: 0, policyVersion: "test", assessmentJson: {}, inputDigest: assessmentId });
|
|
await db.insert(statusDecisions).values({ id: decisionId, companyId, issueId, runId: predecessor!.id,
|
|
assessmentId, decisionVersion: 1, policyVersion: "test", fromStatus: "in_progress", toStatus: "in_progress",
|
|
reasonCode: "live_continuation_registered", decisionJson: {}, decisionDigest: decisionId, applicationState: "applied", appliedAt: new Date() });
|
|
const [intent] = await db.insert(agentWakeupRequests).values({ companyId, agentId, source: "automation", triggerDetail: "system",
|
|
reason: "issue_status_changed", requestedByActorType: "system", requestedByActorId: "native-status-committer",
|
|
idempotencyKey: `native-status:${decisionId}:continuation`, payload: { issueId, taskId: issueId, nativeDecisionId: decisionId, continuationKind: "same_agent",
|
|
_paperclipWakeContext: { issueId, taskId: issueId, source: "native_status_decision", continuationKind: "same_agent" } } }).returning();
|
|
await db.insert(statusDecisionEffects).values({ companyId, issueId, decisionId, ordinal: 0,
|
|
effectKind: "enqueue_continuation", targetType: "agent_wakeup_request", targetId: intent!.id,
|
|
idempotencyKey: `native-status:${decisionId}:continuation`, payload: { continuationKind: "same_agent" }, deliveryState: "delivered", deliveredAt: new Date() });
|
|
await heartbeat.dispatchPendingNativeStatusWakeups({ companyId });
|
|
await heartbeat.dispatchPendingNativeStatusWakeups({ companyId });
|
|
const queued = await db.select().from(agentWakeupRequests).where(eq(agentWakeupRequests.companyId, companyId));
|
|
expect(queued.filter(row => row.status === "deferred_issue_execution"), JSON.stringify(queued.map(row => ({ status: row.status, reason: row.reason, runId: row.runId, error: row.error })))).toHaveLength(1);
|
|
expect(queued.find(row => row.id === intent!.id)).toMatchObject({ status: "coalesced", runId: null });
|
|
const [unchanged] = await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.id, predecessor!.id));
|
|
expect(unchanged!.contextSnapshot).toMatchObject({ source: "initial-repository-request" });
|
|
expect(unchanged!.contextSnapshot?.nativeStatusWakeIntentId).toBeUndefined();
|
|
expect(mockAdapterExecute).toHaveBeenCalledTimes(1);
|
|
finishPredecessor();
|
|
expect(await waitForCondition(async () => mockAdapterExecute.mock.calls.length === 2, 10_000)).toBe(true);
|
|
await heartbeat.drainActiveRunExecutions();
|
|
const completed = await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.companyId, companyId));
|
|
expect(completed).toHaveLength(2);
|
|
const successor = completed.find(run => run.id !== predecessor!.id)!;
|
|
expect(successor).toMatchObject({ status: "succeeded", contextSnapshot: { source: "native_status_decision", nativeStatusWakeIntentId: intent!.id } });
|
|
expect(successor.startedAt!.getTime()).toBeGreaterThanOrEqual(completed.find(run => run.id === predecessor!.id)!.finishedAt!.getTime());
|
|
await heartbeat.dispatchPendingNativeStatusWakeups({ companyId });
|
|
expect(await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.companyId, companyId))).toHaveLength(2);
|
|
} finally { finishPredecessor(); }
|
|
}, 45_000);
|
|
|
|
it("dispatches and coalesces durable native status wake intents into one heartbeat run", async () => {
|
|
const companyId = randomUUID();
|
|
const agentId = randomUUID();
|
|
const issueId = randomUUID();
|
|
|
|
await db.insert(companies).values({
|
|
id: companyId,
|
|
name: "Paperclip",
|
|
issuePrefix: `N${companyId.replace(/-/g, "").slice(0, 6).toUpperCase()}`,
|
|
requireBoardApprovalForNewAgents: false,
|
|
});
|
|
await db.insert(companyMemberships).values({ companyId, principalType: "user", principalId: "responsible-user", status: "active", membershipRole: "member" });
|
|
await db.insert(agents).values({
|
|
id: agentId,
|
|
companyId,
|
|
name: "NativeWakeRunner",
|
|
role: "engineer",
|
|
status: "active",
|
|
adapterType: "codex_local",
|
|
adapterConfig: {},
|
|
runtimeConfig: { heartbeat: { wakeOnDemand: true, maxConcurrentRuns: 1 } },
|
|
permissions: {},
|
|
});
|
|
await db.insert(issues).values({
|
|
id: issueId,
|
|
companyId,
|
|
title: "Resume after native child completion",
|
|
status: "blocked",
|
|
priority: "medium",
|
|
assigneeAgentId: agentId,
|
|
responsibleUserId: "responsible-user",
|
|
});
|
|
const nativeWakePayload = {
|
|
issueId,
|
|
taskId: issueId,
|
|
_paperclipWakeContext: {
|
|
issueId,
|
|
taskId: issueId,
|
|
source: "native_status_decision",
|
|
},
|
|
};
|
|
const intents = await db.insert(agentWakeupRequests).values([
|
|
{
|
|
companyId,
|
|
agentId,
|
|
source: "automation",
|
|
triggerDetail: "system",
|
|
reason: "issue_children_completed",
|
|
payload: nativeWakePayload,
|
|
requestedByActorType: "system",
|
|
requestedByActorId: "native-status-committer",
|
|
idempotencyKey: `native-parent:${issueId}`,
|
|
},
|
|
{
|
|
companyId,
|
|
agentId,
|
|
source: "automation",
|
|
triggerDetail: "system",
|
|
reason: "issue_blockers_resolved",
|
|
payload: nativeWakePayload,
|
|
requestedByActorType: "system",
|
|
requestedByActorId: "native-status-committer",
|
|
idempotencyKey: `native-dependency:${issueId}`,
|
|
},
|
|
]).returning({ id: agentWakeupRequests.id });
|
|
|
|
const result = await heartbeat.dispatchPendingNativeStatusWakeups({ companyId });
|
|
expect(result).toMatchObject({ scanned: 2, dispatched: 1, recovered: 1 });
|
|
|
|
await heartbeat.drainActiveRunExecutions();
|
|
const runs = await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.companyId, companyId));
|
|
const dispatchedRequests = await db.select().from(agentWakeupRequests).where(
|
|
sql`${agentWakeupRequests.requestedByActorId} like 'native-status-wake-dispatch:%'`,
|
|
);
|
|
expect(dispatchedRequests).toHaveLength(1);
|
|
const dispatchedRun = runs.find((run) => run.id === dispatchedRequests[0]!.runId);
|
|
expect(dispatchedRun).toMatchObject({
|
|
status: "succeeded",
|
|
agentId,
|
|
contextSnapshot: {
|
|
source: "native_status_decision",
|
|
statusDecisionSource: "native_status_decision",
|
|
},
|
|
});
|
|
|
|
const persistedIntents = await db.select({
|
|
id: agentWakeupRequests.id,
|
|
status: agentWakeupRequests.status,
|
|
runId: agentWakeupRequests.runId,
|
|
}).from(agentWakeupRequests).where(inArray(agentWakeupRequests.id, intents.map((intent) => intent.id)));
|
|
expect(persistedIntents).toHaveLength(2);
|
|
expect(persistedIntents).toEqual(expect.arrayContaining([
|
|
expect.objectContaining({ status: "coalesced", runId: dispatchedRun!.id }),
|
|
expect.objectContaining({ status: "coalesced", runId: dispatchedRun!.id }),
|
|
]));
|
|
expect(dispatchedRequests[0]).toMatchObject({ runId: dispatchedRun!.id });
|
|
});
|
|
|
|
it.each(["onboarding", "ordinary", "cancelled"])("dispatches only a verified completed onboarding report: %s", async kind => {
|
|
const companyId = randomUUID(), agentId = randomUUID(), issueId = randomUUID(), childId = randomUUID();
|
|
await db.insert(companies).values({ id: companyId, name: "Completion", issuePrefix: `R${companyId.replace(/-/g, "").slice(0, 6).toUpperCase()}` });
|
|
await db.insert(companyMemberships).values({ companyId, principalType: "user", principalId: "responsible-user", status: "active", membershipRole: "member" });
|
|
await db.insert(agents).values({ id: agentId, companyId, name: "Lead", role: "engineer", status: "active", adapterType: "codex_local",
|
|
adapterConfig: {}, runtimeConfig: { heartbeat: { wakeOnDemand: true, maxConcurrentRuns: 1 } }, permissions: {} });
|
|
await db.insert(issues).values({ id: issueId, companyId, title: "Parent", status: kind === "cancelled" ? "cancelled" : "done",
|
|
originKind: kind === "ordinary" ? "manual" : "onboarding_first_task", assigneeAgentId: agentId, responsibleUserId: "responsible-user" });
|
|
await db.insert(issues).values({ id: childId, companyId, title: "Saved result", parentId: issueId, status: "done", assigneeAgentId: agentId });
|
|
await db.insert(agentWakeupRequests).values({ companyId, agentId, source: "automation", triggerDetail: "system", reason: "issue_children_completed",
|
|
requestedByActorType: "system", requestedByActorId: "native-status-committer", idempotencyKey: `onboarding-completed:${issueId}`,
|
|
payload: { issueId, _paperclipWakeContext: { issueId, completedChildIssueId: childId, onboardingCompletion: true } } });
|
|
const result = await heartbeat.dispatchPendingNativeStatusWakeups({ companyId });
|
|
expect(result.dispatched).toBe(kind === "onboarding" ? 1 : 0);
|
|
await heartbeat.drainActiveRunExecutions();
|
|
const runs = await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.companyId, companyId));
|
|
expect(runs).toHaveLength(kind === "onboarding" ? 1 : 0);
|
|
if (kind === "onboarding") {
|
|
expect({ status: runs[0].status, errorCode: runs[0].errorCode, error: runs[0].error })
|
|
.toEqual({ status: "succeeded", errorCode: null, error: null });
|
|
expect(runs[0].contextSnapshot).toMatchObject({
|
|
onboardingCompletion: true, chatCompletionUpdates: [expect.objectContaining({ id: childId, status: "done" })],
|
|
});
|
|
expect(mockAdapterExecute).toHaveBeenCalledOnce();
|
|
}
|
|
expect((await db.select().from(issues).where(eq(issues.id, issueId)))[0].status).toBe(kind === "cancelled" ? "cancelled" : "done");
|
|
});
|
|
|
|
it("coalesces the native intent when dispatch admission defers behind an active issue run", async () => {
|
|
const companyId = randomUUID();
|
|
const agentId = randomUUID();
|
|
const issueId = randomUUID();
|
|
const activeRunId = randomUUID();
|
|
|
|
await db.insert(companies).values({
|
|
id: companyId,
|
|
name: "Paperclip",
|
|
issuePrefix: `R${companyId.replace(/-/g, "").slice(0, 6).toUpperCase()}`,
|
|
requireBoardApprovalForNewAgents: false,
|
|
});
|
|
await db.insert(companyMemberships).values({ companyId, principalType: "user", principalId: "responsible-user", status: "active", membershipRole: "member" });
|
|
await db.insert(agents).values({
|
|
id: agentId,
|
|
companyId,
|
|
name: "NativeWakeRunner",
|
|
role: "engineer",
|
|
status: "active",
|
|
adapterType: "codex_local",
|
|
adapterConfig: {},
|
|
runtimeConfig: { heartbeat: { wakeOnDemand: true, maxConcurrentRuns: 1 } },
|
|
permissions: {},
|
|
});
|
|
await db.insert(heartbeatRuns).values({
|
|
id: activeRunId,
|
|
companyId,
|
|
agentId,
|
|
status: "running",
|
|
invocationSource: "automation",
|
|
triggerDetail: "system",
|
|
contextSnapshot: { issueId },
|
|
});
|
|
await db.insert(issues).values({
|
|
id: issueId,
|
|
companyId,
|
|
title: "Native wake behind active run",
|
|
status: "in_progress",
|
|
priority: "medium",
|
|
assigneeAgentId: agentId,
|
|
responsibleUserId: "responsible-user",
|
|
executionRunId: activeRunId,
|
|
executionLockedAt: new Date(),
|
|
});
|
|
await db.insert(issueRecoveryActions).values({
|
|
companyId,
|
|
sourceIssueId: issueId,
|
|
kind: "active_run_watchdog",
|
|
status: "active",
|
|
ownerType: "board",
|
|
cause: "legacy_execution_requires_reconciliation",
|
|
fingerprint: activeRunId,
|
|
evidence: { runId: activeRunId },
|
|
nextAction: "Wait for the active execution to stop.",
|
|
});
|
|
const [intent] = await db.insert(agentWakeupRequests).values({
|
|
companyId,
|
|
agentId,
|
|
source: "automation",
|
|
triggerDetail: "system",
|
|
reason: "issue_children_completed",
|
|
payload: { issueId, taskId: issueId, _paperclipWakeContext: { issueId, taskId: issueId } },
|
|
requestedByActorType: "system",
|
|
requestedByActorId: "native-status-committer",
|
|
idempotencyKey: `native-race:${issueId}`,
|
|
}).returning();
|
|
|
|
const result = await heartbeat.dispatchPendingNativeStatusWakeups({ companyId });
|
|
expect(result).toMatchObject({ scanned: 1, dispatched: 0, deferred: 1 });
|
|
|
|
const rows = await db.select({
|
|
id: agentWakeupRequests.id,
|
|
status: agentWakeupRequests.status,
|
|
runId: agentWakeupRequests.runId,
|
|
requestedByActorId: agentWakeupRequests.requestedByActorId,
|
|
}).from(agentWakeupRequests).where(eq(agentWakeupRequests.companyId, companyId));
|
|
expect(rows.find((row) => row.id === intent!.id)).toMatchObject({
|
|
status: "coalesced",
|
|
runId: null,
|
|
});
|
|
expect(rows.filter((row) => row.status === "deferred_issue_execution")).toHaveLength(1);
|
|
expect(rows.filter((row) => row.requestedByActorId?.startsWith("native-status-wake-dispatch:"))).toHaveLength(1);
|
|
|
|
// Simulate the process crash window after the dispatch receipt was written
|
|
// but before the committer intent was reconciled.
|
|
await db.update(agentWakeupRequests).set({ status: "queued", updatedAt: new Date() })
|
|
.where(eq(agentWakeupRequests.id, intent!.id));
|
|
const recovered = await heartbeat.dispatchPendingNativeStatusWakeups({ companyId });
|
|
expect(recovered).toMatchObject({ scanned: 1, recovered: 1 });
|
|
const recoveredIntent = await db.select({ status: agentWakeupRequests.status })
|
|
.from(agentWakeupRequests).where(eq(agentWakeupRequests.id, intent!.id))
|
|
.then((result) => result[0]);
|
|
expect(recoveredIntent).toMatchObject({ status: "coalesced" });
|
|
});
|
|
|
|
it.each(["answered", "accepted", "rejected", "pending", "missing", "wrong_issue", "agent_resolved", "changed_before_dispatch",
|
|
"workspace_wait", "workspace_wait_changed", "blocker_finishes", "in_review_answered", "in_review_blocker_finishes"] as const)(
|
|
"delivers only a saved human response while dependencies remain blocked: %s", async response => {
|
|
const companyId = randomUUID(), agentId = randomUUID(), blockerId = randomUUID(), issueId = randomUUID();
|
|
// A task waiting in review can still have open blockers. The answer must
|
|
// reach its assignee without a checkout that the blockers would refuse.
|
|
// Once its last blocker finishes, the reviewed task resumes through its
|
|
// normal checkout instead of staying in a bounded turn.
|
|
const reviewed = response === "in_review_answered" || response === "in_review_blocker_finishes";
|
|
const blockerFinishes = response === "blocker_finishes" || response === "in_review_blocker_finishes";
|
|
const parentStatus = reviewed ? "in_review" : "blocked";
|
|
await db.insert(companies).values({ id: companyId, name: "Response fixture",
|
|
issuePrefix: `T${companyId.replace(/-/g, "").slice(0, 6).toUpperCase()}`,
|
|
requireBoardApprovalForNewAgents: false, defaultResponsibleUserId: "responsible-user" });
|
|
await db.insert(companyMemberships).values({ companyId, principalType: "user", principalId: "responsible-user",
|
|
status: "active", membershipRole: "member" });
|
|
await db.insert(agents).values({ id: agentId, companyId, name: "Parent assignee", role: "engineer", status: "active",
|
|
adapterType: "codex_local", adapterConfig: {}, runtimeConfig: { heartbeat: { wakeOnDemand: true, maxConcurrentRuns: 1 } }, permissions: {} });
|
|
await db.insert(issues).values([
|
|
{ id: blockerId, companyId, title: "Parked trial", status: "backlog", responsibleUserId: "responsible-user" },
|
|
{ id: issueId, companyId, title: "Finish trial prerequisites", status: parentStatus, assigneeAgentId: agentId,
|
|
responsibleUserId: "responsible-user" },
|
|
]);
|
|
await db.insert(issueRelations).values({ companyId, issueId: blockerId, relatedIssueId: issueId, type: "blocks" });
|
|
const confirmation = response === "accepted" || response === "rejected";
|
|
const status = confirmation ? response : response === "pending" ? "pending" : "answered";
|
|
const [interaction] = await db.insert(issueThreadInteractions).values({ companyId,
|
|
issueId: response === "wrong_issue" ? blockerId : issueId, createdByAgentId: agentId,
|
|
kind: confirmation ? "request_confirmation" : "ask_user_questions", status,
|
|
title: "Setup access", resolvedByUserId: response === "pending" || response === "agent_resolved" ? null : "responsible-user",
|
|
resolvedByAgentId: response === "agent_resolved" ? agentId : null,
|
|
resolvedAt: response === "pending" ? null : new Date(),
|
|
payload: confirmation ? { version: 1, prompt: "Continue setup?" }
|
|
: { version: 1, questions: [{ id: "access", prompt: "Can setup continue?", selectionMode: "single",
|
|
options: [{ id: "ready", label: "Access is ready" }] }] },
|
|
result: response === "pending" ? null : confirmation ? { version: 1, outcome: response }
|
|
: { version: 1, answers: [{ questionId: "access", optionIds: ["ready"] }] },
|
|
}).returning();
|
|
const context = { issueId, mutation: "interaction", interactionId: response === "missing" ? randomUUID() : interaction.id,
|
|
interactionKind: interaction.kind, interactionStatus: status, wakeReason: "issue_commented" };
|
|
const resourceWait = response === "workspace_wait" || response === "workspace_wait_changed";
|
|
let deferredSourceId: string | null = null;
|
|
const workspace = resourceWait ? vi.spyOn(workspacePreparation, "prepareHeartbeatWorkspace")
|
|
.mockRejectedValueOnce(new WorkspaceBusyDeferral({ holder: { runId: randomUUID(), agentId,
|
|
issueId: blockerId, issueIdentifier: null }, executionWorkspaceId: randomUUID(),
|
|
wasIssueAssignee: true, deferralAttempt: 0 })) : null;
|
|
const service = response === "changed_before_dispatch" || resourceWait || blockerFinishes ? heartbeatService(db, {
|
|
beforeChatControlRecoveryCheck: async ({ stage }) => {
|
|
if (blockerFinishes && stage === "claim")
|
|
await db.update(issues).set({ status: "done" }).where(eq(issues.id, blockerId));
|
|
},
|
|
beforeResolvedInteractionContinuationDispatchCheck: async ({ runId }) => {
|
|
if (response === "changed_before_dispatch" || response === "workspace_wait_changed" && deferredSourceId && runId !== deferredSourceId)
|
|
await db.update(issueThreadInteractions).set({ resolvedByUserId: null, resolvedByAgentId: agentId })
|
|
.where(eq(issueThreadInteractions.id, interaction.id));
|
|
},
|
|
}) : heartbeat;
|
|
const wake = await service.wakeup(agentId, { source: "automation", triggerDetail: "system", reason: "issue_commented",
|
|
payload: context, contextSnapshot: context, requestedByActorType: "user", requestedByActorId: "responsible-user" });
|
|
let dispatchedRunId = wake?.id;
|
|
if (resourceWait) {
|
|
expect(wake).not.toBeNull();
|
|
await service.drainActiveRunExecutions();
|
|
expect((await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.id, wake!.id)))[0])
|
|
.toMatchObject({ status: "cancelled", errorCode: "workspace_busy" });
|
|
expect(mockAdapterExecute).not.toHaveBeenCalled();
|
|
const [retry] = await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.retryOfRunId, wake!.id));
|
|
expect(retry).toMatchObject({ status: "scheduled_retry", scheduledRetryReason: "workspace_busy" });
|
|
deferredSourceId = wake!.id;
|
|
dispatchedRunId = retry.id;
|
|
workspace!.mockRestore();
|
|
expect((await service.promoteDueScheduledRetries(new Date(Date.now() + 5 * 60_000))).runIds).toContain(retry.id);
|
|
await service.resumeQueuedRuns();
|
|
}
|
|
const shouldDeliver = ["answered", "accepted", "rejected", "workspace_wait", "blocker_finishes", "in_review_answered",
|
|
"in_review_blocker_finishes"].includes(response);
|
|
if (shouldDeliver) {
|
|
expect(wake).not.toBeNull();
|
|
await service.drainActiveRunExecutions();
|
|
const [run] = await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.id, dispatchedRunId!));
|
|
expect({ status: run.status, error: run.error, errorCode: run.errorCode })
|
|
.toEqual({ status: "succeeded", error: null, errorCode: null });
|
|
expect(run).toMatchObject({ status: "succeeded", contextSnapshot: {
|
|
interactionId: interaction.id, interactionStatus: status, dependencyBlockedInteraction: true,
|
|
unresolvedBlockerIssueIds: [blockerId],
|
|
} });
|
|
const answerRuns = (await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.agentId, agentId)))
|
|
.filter((row) => row.status === "succeeded"
|
|
&& (row.contextSnapshot as Record<string, unknown> | null)?.interactionId === interaction.id);
|
|
expect(answerRuns.map((row) => row.id)).toEqual([run.id]);
|
|
// The checked-out task's ordinary disposition repair may follow; the
|
|
// saved answer itself is delivered once.
|
|
if (response !== "in_review_blocker_finishes") expect(mockAdapterExecute).toHaveBeenCalledTimes(1);
|
|
if (response === "answered") {
|
|
// A resource wait keeps the same bounded interaction and must not
|
|
// strand the answer again at scheduled promotion.
|
|
const [retry] = await db.insert(heartbeatRuns).values({ companyId, agentId,
|
|
invocationSource: "automation", status: "scheduled_retry", scheduledRetryAt: new Date(),
|
|
scheduledRetryReason: "interaction_continuation_infra_retry", retryOfRunId: run.id,
|
|
contextSnapshot: { ...run.contextSnapshot, wakeReason: "interaction_continuation_infra_retry",
|
|
retryReason: "interaction_continuation_infra_retry" },
|
|
}).returning();
|
|
const retryGate = () => createPostgresRunDispatchAdapter(db).evaluateScheduledRetryGate({
|
|
companyId, runId: retry.id, retryReasonOverride: "interaction_continuation_infra_retry", now: new Date(),
|
|
});
|
|
expect(await retryGate()).toMatchObject({ allowed: true });
|
|
await db.update(issueThreadInteractions).set({ resolvedByUserId: null, resolvedByAgentId: agentId })
|
|
.where(eq(issueThreadInteractions.id, interaction.id));
|
|
expect(await retryGate()).toMatchObject({ allowed: false, errorCode: "issue_dependencies_blocked" });
|
|
}
|
|
} else if (response === "changed_before_dispatch" || response === "workspace_wait_changed") {
|
|
expect(wake).not.toBeNull();
|
|
await service.drainActiveRunExecutions();
|
|
expect((await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.id, dispatchedRunId!)))[0])
|
|
.toMatchObject({ status: "cancelled", errorCode: "issue_not_in_progress" });
|
|
expect(mockAdapterExecute).not.toHaveBeenCalled();
|
|
} else {
|
|
expect(wake).toBeNull();
|
|
expect(mockAdapterExecute).not.toHaveBeenCalled();
|
|
}
|
|
expect(await db.select().from(issueRelations).where(eq(issueRelations.relatedIssueId, issueId))).toHaveLength(1);
|
|
expect((await db.select().from(issues).where(eq(issues.id, blockerId)))[0].status)
|
|
.toBe(blockerFinishes ? "done" : "backlog");
|
|
expect((await db.select().from(issues).where(eq(issues.id, issueId)))[0].status)
|
|
.toBe(response === "in_review_blocker_finishes" ? "in_progress" : parentStatus);
|
|
expect(await db.select().from(heartbeatRuns).where(sql`${heartbeatRuns.contextSnapshot}->>'issueId' = ${blockerId}`)).toHaveLength(0);
|
|
},
|
|
);
|
|
|
|
it("keeps blocked descendants idle until their blockers resolve", async () => {
|
|
const companyId = randomUUID();
|
|
const agentId = randomUUID();
|
|
const blockerId = randomUUID();
|
|
const blockedIssueId = randomUUID();
|
|
const readyIssueId = randomUUID();
|
|
|
|
await db.insert(companies).values({
|
|
id: companyId,
|
|
name: "Paperclip",
|
|
issuePrefix: `T${companyId.replace(/-/g, "").slice(0, 6).toUpperCase()}`,
|
|
requireBoardApprovalForNewAgents: false,
|
|
defaultResponsibleUserId: "responsible-user",
|
|
});
|
|
await db.insert(companyMemberships).values({ companyId, principalType: "user", principalId: "responsible-user", status: "active", membershipRole: "member" });
|
|
await db.insert(agents).values({
|
|
id: agentId,
|
|
companyId,
|
|
name: "CodexCoder",
|
|
role: "engineer",
|
|
status: "active",
|
|
adapterType: "codex_local",
|
|
adapterConfig: {},
|
|
runtimeConfig: {
|
|
heartbeat: {
|
|
wakeOnDemand: true,
|
|
maxConcurrentRuns: 1,
|
|
},
|
|
},
|
|
permissions: {},
|
|
});
|
|
await db.insert(issues).values([
|
|
{
|
|
id: blockerId,
|
|
companyId,
|
|
title: "Mission 0",
|
|
status: "todo",
|
|
priority: "high",
|
|
responsibleUserId: "responsible-user",
|
|
},
|
|
{
|
|
id: blockedIssueId,
|
|
companyId,
|
|
title: "Mission 2",
|
|
status: "todo",
|
|
priority: "medium",
|
|
assigneeAgentId: agentId,
|
|
responsibleUserId: "responsible-user",
|
|
},
|
|
{
|
|
id: readyIssueId,
|
|
companyId,
|
|
title: "Mission 1",
|
|
status: "todo",
|
|
priority: "critical",
|
|
assigneeAgentId: agentId,
|
|
responsibleUserId: "responsible-user",
|
|
},
|
|
]);
|
|
await db.insert(issueRelations).values({
|
|
companyId,
|
|
issueId: blockerId,
|
|
relatedIssueId: blockedIssueId,
|
|
type: "blocks",
|
|
});
|
|
|
|
const blockedWake = await heartbeat.wakeup(agentId, {
|
|
source: "assignment",
|
|
triggerDetail: "system",
|
|
reason: "issue_assigned",
|
|
payload: { issueId: blockedIssueId },
|
|
contextSnapshot: { issueId: blockedIssueId, wakeReason: "issue_assigned" },
|
|
});
|
|
expect(blockedWake).toBeNull();
|
|
|
|
const blockedWakeRequest = await waitForCondition(async () => {
|
|
const wakeup = await db
|
|
.select({
|
|
status: agentWakeupRequests.status,
|
|
reason: agentWakeupRequests.reason,
|
|
})
|
|
.from(agentWakeupRequests)
|
|
.where(
|
|
and(
|
|
eq(agentWakeupRequests.agentId, agentId),
|
|
sql`${agentWakeupRequests.payload} ->> 'issueId' = ${blockedIssueId}`,
|
|
),
|
|
)
|
|
.orderBy(agentWakeupRequests.requestedAt)
|
|
.then((rows) => rows[0] ?? null);
|
|
return Boolean(
|
|
wakeup &&
|
|
wakeup.status === "skipped" &&
|
|
wakeup.reason === "issue_dependencies_blocked",
|
|
);
|
|
});
|
|
expect(blockedWakeRequest).toBe(true);
|
|
|
|
const blockedRunsBeforeResolution = await db
|
|
.select({ count: sql<number>`count(*)::int` })
|
|
.from(heartbeatRuns)
|
|
.where(sql`${heartbeatRuns.contextSnapshot} ->> 'issueId' = ${blockedIssueId}`)
|
|
.then((rows) => rows[0]?.count ?? 0);
|
|
expect(blockedRunsBeforeResolution).toBe(0);
|
|
|
|
const commentId = randomUUID();
|
|
await db.insert(issueComments).values({
|
|
id: commentId, companyId, issueId: blockedIssueId,
|
|
authorType: "user", authorUserId: "responsible-user",
|
|
body: "Explain the current dependency without starting blocked work.",
|
|
});
|
|
const interactionWake = await heartbeat.wakeup(agentId, {
|
|
source: "automation",
|
|
triggerDetail: "system",
|
|
reason: "issue_commented",
|
|
payload: { issueId: blockedIssueId, commentId },
|
|
contextSnapshot: {
|
|
issueId: blockedIssueId,
|
|
wakeReason: "issue_commented",
|
|
},
|
|
});
|
|
expect(interactionWake).not.toBeNull();
|
|
|
|
// This checks dependency interaction semantics, not provider startup speed.
|
|
// Join the admitted execution before inspecting its persisted outcome.
|
|
await heartbeat.drainActiveRunExecutions();
|
|
|
|
const interactionRun = await db
|
|
.select({
|
|
status: heartbeatRuns.status,
|
|
contextSnapshot: heartbeatRuns.contextSnapshot,
|
|
})
|
|
.from(heartbeatRuns)
|
|
.where(eq(heartbeatRuns.id, interactionWake!.id))
|
|
.then((rows) => rows[0] ?? null);
|
|
|
|
expect(interactionRun?.status).toBe("succeeded");
|
|
expect(interactionRun?.contextSnapshot).toMatchObject({
|
|
dependencyBlockedInteraction: true,
|
|
unresolvedBlockerIssueIds: [blockerId],
|
|
});
|
|
|
|
let finishReadyRun!: () => void;
|
|
const readyRunCanFinish = new Promise<void>((resolve) => {
|
|
finishReadyRun = resolve;
|
|
});
|
|
mockAdapterExecute.mockImplementationOnce(async () => {
|
|
await readyRunCanFinish;
|
|
return {
|
|
exitCode: 0,
|
|
signal: null,
|
|
timedOut: false,
|
|
errorMessage: null,
|
|
summary: "Ready dependency scheduling run complete.",
|
|
provider: "test",
|
|
model: "test-model",
|
|
};
|
|
});
|
|
|
|
const readyWake = await heartbeat.wakeup(agentId, {
|
|
source: "assignment",
|
|
triggerDetail: "system",
|
|
reason: "issue_assigned",
|
|
payload: { issueId: readyIssueId },
|
|
contextSnapshot: { issueId: readyIssueId, wakeReason: "issue_assigned" },
|
|
});
|
|
expect(readyWake).not.toBeNull();
|
|
await db.insert(issueComments).values({
|
|
companyId,
|
|
issueId: readyIssueId,
|
|
authorAgentId: agentId,
|
|
authorType: "agent",
|
|
createdByRunId: readyWake!.id,
|
|
body: "Ready dependency scheduling run complete.",
|
|
});
|
|
finishReadyRun();
|
|
|
|
await waitForCondition(async () => {
|
|
const run = await db
|
|
.select({ status: heartbeatRuns.status })
|
|
.from(heartbeatRuns)
|
|
.where(eq(heartbeatRuns.id, readyWake!.id))
|
|
.then((rows) => rows[0] ?? null);
|
|
return run?.status === "succeeded";
|
|
});
|
|
|
|
const readyRun = await db
|
|
.select()
|
|
.from(heartbeatRuns)
|
|
.where(eq(heartbeatRuns.id, readyWake!.id))
|
|
.then((rows) => rows[0] ?? null);
|
|
|
|
expect(readyRun?.status).toBe("succeeded");
|
|
|
|
await db
|
|
.update(issues)
|
|
.set({ status: "done", updatedAt: new Date() })
|
|
.where(eq(issues.id, blockerId));
|
|
|
|
const promotedWake = await heartbeat.wakeup(agentId, {
|
|
source: "automation",
|
|
triggerDetail: "system",
|
|
reason: "issue_blockers_resolved",
|
|
payload: { issueId: blockedIssueId, resolvedBlockerIssueId: blockerId },
|
|
contextSnapshot: {
|
|
issueId: blockedIssueId,
|
|
wakeReason: "issue_blockers_resolved",
|
|
resolvedBlockerIssueId: blockerId,
|
|
},
|
|
});
|
|
expect(promotedWake).not.toBeNull();
|
|
|
|
await waitForCondition(async () => {
|
|
const run = await db
|
|
.select({ status: heartbeatRuns.status })
|
|
.from(heartbeatRuns)
|
|
.where(eq(heartbeatRuns.id, promotedWake!.id))
|
|
.then((rows) => rows[0] ?? null);
|
|
return run?.status === "succeeded";
|
|
});
|
|
|
|
const promotedBlockedRun = await db
|
|
.select({
|
|
id: heartbeatRuns.id,
|
|
status: heartbeatRuns.status,
|
|
})
|
|
.from(heartbeatRuns)
|
|
.where(eq(heartbeatRuns.id, promotedWake!.id))
|
|
.then((rows) => rows[0] ?? null);
|
|
const blockedWakeRequestCount = await db
|
|
.select({ count: sql<number>`count(*)::int` })
|
|
.from(agentWakeupRequests)
|
|
.where(
|
|
and(
|
|
eq(agentWakeupRequests.agentId, agentId),
|
|
sql`${agentWakeupRequests.payload} ->> 'issueId' = ${blockedIssueId}`,
|
|
),
|
|
)
|
|
.then((rows) => rows[0]?.count ?? 0);
|
|
|
|
expect(promotedBlockedRun?.status).toBe("succeeded");
|
|
expect(blockedWakeRequestCount).toBeGreaterThanOrEqual(2);
|
|
|
|
const noActiveRuns = await waitForCondition(async () => {
|
|
const rows = await db
|
|
.select({ status: heartbeatRuns.status })
|
|
.from(heartbeatRuns);
|
|
return rows.every((run) => run.status !== "queued" && run.status !== "running");
|
|
}, 10_000);
|
|
expect(noActiveRuns).toBe(true);
|
|
});
|
|
|
|
it("admits an issue_blocker_stalled wake while a blocker is unresolved", async () => {
|
|
// The waiter's blocker stalled on a board-owned recovery action. Parking
|
|
// this wake until the blocker resolves would hide the stall again.
|
|
const companyId = randomUUID();
|
|
const agentId = randomUUID();
|
|
const blockerId = randomUUID();
|
|
const dependentId = randomUUID();
|
|
await db.insert(companies).values({
|
|
id: companyId, name: "Paperclip", issuePrefix: `T${companyId.replace(/-/g, "").slice(0, 6).toUpperCase()}`,
|
|
requireBoardApprovalForNewAgents: false, defaultResponsibleUserId: "responsible-user",
|
|
});
|
|
await db.insert(companyMemberships).values({ companyId, principalType: "user", principalId: "responsible-user", status: "active", membershipRole: "member" });
|
|
await db.insert(agents).values({
|
|
id: agentId, companyId, name: "CodexCoder", role: "engineer", status: "active", adapterType: "codex_local",
|
|
adapterConfig: {}, runtimeConfig: { heartbeat: { wakeOnDemand: true, maxConcurrentRuns: 1 } }, permissions: {},
|
|
});
|
|
await db.insert(issues).values([
|
|
{ id: blockerId, companyId, title: "Stalled blocker", status: "blocked", priority: "high", responsibleUserId: "responsible-user" },
|
|
{ id: dependentId, companyId, title: "Waiting dependent", status: "blocked", priority: "medium", assigneeAgentId: agentId,
|
|
responsibleUserId: "responsible-user" },
|
|
]);
|
|
await db.insert(issueRelations).values({ companyId, issueId: blockerId, relatedIssueId: dependentId, type: "blocks" });
|
|
const [action] = await db.insert(issueRecoveryActions).values({ companyId, sourceIssueId: blockerId, kind: "run_recovery",
|
|
status: "resolved", outcome: "blocked", ownerType: "board", cause: "native_session_retry_exhausted",
|
|
fingerprint: "stall", nextAction: "Retry", resolvedAt: new Date() }).returning();
|
|
const stallWake = (requestedByActorType: "system" | "agent") => heartbeat.wakeup(agentId, {
|
|
source: "automation",
|
|
triggerDetail: "system",
|
|
reason: "issue_blocker_stalled",
|
|
payload: { issueId: dependentId, stalledIssueId: blockerId },
|
|
idempotencyKey: `issue_blocker_stalled:${dependentId}:${blockerId}:${action.id}:${requestedByActorType}`,
|
|
requestedByActorType,
|
|
requestedByActorId: requestedByActorType === "system" ? "issue-blocker-stall" : agentId,
|
|
contextSnapshot: { issueId: dependentId, wakeReason: "issue_blocker_stalled",
|
|
blockerStall: { stalledIssueId: blockerId, relation: "blocker", recoveryAction: { id: action.id } } },
|
|
});
|
|
|
|
// The reason string alone is not trusted: an agent cannot use it to skip the blocker gate.
|
|
expect(await stallWake("agent")).toBeNull();
|
|
|
|
const stalledWake = await stallWake("system");
|
|
expect(stalledWake).not.toBeNull();
|
|
await heartbeat.drainActiveRunExecutions();
|
|
const [run] = await db.select({ status: heartbeatRuns.status, contextSnapshot: heartbeatRuns.contextSnapshot })
|
|
.from(heartbeatRuns).where(eq(heartbeatRuns.id, stalledWake!.id));
|
|
expect(run?.status).toBe("succeeded");
|
|
expect(run?.contextSnapshot).toMatchObject({ dependencyBlockedInteraction: true, unresolvedBlockerIssueIds: [blockerId] });
|
|
|
|
// A resource wait replaces the run with a server-created retry that keeps
|
|
// the stall context but not the wake reason. The retry gate still admits it.
|
|
const retryGate = async (contextSnapshot: Record<string, unknown>) => {
|
|
const [retry] = await db.insert(heartbeatRuns).values({ companyId, agentId, invocationSource: "automation",
|
|
status: "scheduled_retry", scheduledRetryAt: new Date(), scheduledRetryReason: "transient_failure",
|
|
retryOfRunId: stalledWake!.id, contextSnapshot }).returning();
|
|
return createPostgresRunDispatchAdapter(db).evaluateScheduledRetryGate({ companyId, runId: retry.id,
|
|
retryReasonOverride: "transient_failure", now: new Date() });
|
|
};
|
|
const replacement: Record<string, unknown> = { ...(run!.contextSnapshot as Record<string, unknown>),
|
|
wakeReason: "bounded_transient_heartbeat_retry", retryReason: "transient_failure", retryOfRunId: stalledWake!.id };
|
|
expect(await retryGate(replacement)).toMatchObject({ allowed: true });
|
|
const { blockerStall: _stall, ...withoutStall } = replacement;
|
|
expect(await retryGate(withoutStall)).toMatchObject({ allowed: false, errorCode: "issue_dependencies_blocked" });
|
|
// Once the stall's action is no longer current, the retry waits for the blocker like any other.
|
|
await db.update(issueRecoveryActions).set({ outcome: "restored" }).where(eq(issueRecoveryActions.id, action.id));
|
|
expect(await retryGate(replacement)).toMatchObject({ allowed: false, errorCode: "issue_dependencies_blocked" });
|
|
});
|
|
|
|
it("defers issue_blockers_resolved as a follow-up when the same issue is already running", async () => {
|
|
const companyId = randomUUID();
|
|
const agentId = randomUUID();
|
|
const blockerId = randomUUID();
|
|
const blockedIssueId = randomUUID();
|
|
const activeRunId = randomUUID();
|
|
|
|
await db.insert(companies).values({
|
|
id: companyId,
|
|
name: "Paperclip",
|
|
issuePrefix: `D${companyId.replace(/-/g, "").slice(0, 6).toUpperCase()}`,
|
|
requireBoardApprovalForNewAgents: false,
|
|
});
|
|
await db.insert(companyMemberships).values({ companyId, principalType: "user", principalId: "responsible-user", status: "active", membershipRole: "member" });
|
|
await db.insert(agents).values({
|
|
id: agentId,
|
|
companyId,
|
|
name: "CodexCoder",
|
|
role: "engineer",
|
|
status: "active",
|
|
adapterType: "codex_local",
|
|
adapterConfig: {},
|
|
runtimeConfig: {
|
|
heartbeat: {
|
|
wakeOnDemand: true,
|
|
maxConcurrentRuns: 1,
|
|
},
|
|
},
|
|
permissions: {},
|
|
});
|
|
await db.insert(heartbeatRuns).values({
|
|
id: activeRunId,
|
|
companyId,
|
|
agentId,
|
|
status: "running",
|
|
invocationSource: "on_demand",
|
|
contextSnapshot: {
|
|
issueId: blockedIssueId,
|
|
wakeReason: "manual_test_active_run",
|
|
},
|
|
});
|
|
await db.insert(issues).values([
|
|
{
|
|
id: blockerId,
|
|
companyId,
|
|
title: "Completed prerequisite",
|
|
status: "done",
|
|
priority: "medium",
|
|
},
|
|
{
|
|
id: blockedIssueId,
|
|
companyId,
|
|
title: "Blocked dependent",
|
|
status: "blocked",
|
|
priority: "medium",
|
|
assigneeAgentId: agentId,
|
|
executionRunId: activeRunId,
|
|
executionLockedAt: new Date(),
|
|
},
|
|
]);
|
|
await db.insert(issueRelations).values({
|
|
companyId,
|
|
issueId: blockerId,
|
|
relatedIssueId: blockedIssueId,
|
|
type: "blocks",
|
|
});
|
|
runningProcesses.set(activeRunId, {
|
|
child: {} as import("node:child_process").ChildProcess,
|
|
graceSec: 1,
|
|
processGroupId: null,
|
|
});
|
|
|
|
const idempotencyKey = `issue_blockers_resolved:${blockedIssueId}:${blockerId}`;
|
|
const wake = await heartbeat.wakeup(agentId, {
|
|
source: "automation",
|
|
triggerDetail: "system",
|
|
reason: "issue_blockers_resolved",
|
|
payload: {
|
|
issueId: blockedIssueId,
|
|
resolvedBlockerIssueId: blockerId,
|
|
},
|
|
idempotencyKey,
|
|
contextSnapshot: {
|
|
issueId: blockedIssueId,
|
|
wakeReason: "issue_blockers_resolved",
|
|
resolvedBlockerIssueId: blockerId,
|
|
},
|
|
});
|
|
|
|
expect(wake).toBeNull();
|
|
|
|
const wakeRequests = await db
|
|
.select({
|
|
status: agentWakeupRequests.status,
|
|
reason: agentWakeupRequests.reason,
|
|
idempotencyKey: agentWakeupRequests.idempotencyKey,
|
|
runId: agentWakeupRequests.runId,
|
|
})
|
|
.from(agentWakeupRequests)
|
|
.where(eq(agentWakeupRequests.idempotencyKey, idempotencyKey));
|
|
|
|
expect(wakeRequests).toEqual([
|
|
expect.objectContaining({
|
|
status: "deferred_issue_execution",
|
|
reason: "issue_execution_deferred",
|
|
idempotencyKey,
|
|
runId: null,
|
|
}),
|
|
]);
|
|
|
|
runningProcesses.delete(activeRunId);
|
|
await db
|
|
.update(heartbeatRuns)
|
|
.set({ status: "succeeded", finishedAt: new Date(), updatedAt: new Date() })
|
|
.where(eq(heartbeatRuns.id, activeRunId));
|
|
});
|
|
|
|
it("honors maxConcurrentRuns 1 by leaving a second assignment wake queued", async () => {
|
|
const companyId = randomUUID();
|
|
const agentId = randomUUID();
|
|
const firstIssueId = randomUUID();
|
|
const secondIssueId = randomUUID();
|
|
let finishFirstRun!: () => void;
|
|
const firstRunFinished = new Promise<void>((resolve) => {
|
|
finishFirstRun = resolve;
|
|
});
|
|
|
|
mockAdapterExecute.mockImplementationOnce(async () => {
|
|
await firstRunFinished;
|
|
return {
|
|
exitCode: 0,
|
|
signal: null,
|
|
timedOut: false,
|
|
errorMessage: null,
|
|
summary: "First assignment run completed.",
|
|
provider: "test",
|
|
model: "test-model",
|
|
};
|
|
});
|
|
|
|
await db.insert(companies).values({
|
|
id: companyId,
|
|
name: "Paperclip",
|
|
issuePrefix: `T${companyId.replace(/-/g, "").slice(0, 6).toUpperCase()}`,
|
|
requireBoardApprovalForNewAgents: false,
|
|
defaultResponsibleUserId: "responsible-user",
|
|
});
|
|
await db.insert(companyMemberships).values({ companyId, principalType: "user", principalId: "responsible-user", status: "active", membershipRole: "member" });
|
|
await db.insert(agents).values({
|
|
id: agentId,
|
|
companyId,
|
|
name: "CodexCoder",
|
|
role: "engineer",
|
|
status: "active",
|
|
adapterType: "codex_local",
|
|
adapterConfig: {},
|
|
runtimeConfig: {
|
|
heartbeat: {
|
|
wakeOnDemand: true,
|
|
maxConcurrentRuns: 1,
|
|
},
|
|
},
|
|
permissions: {},
|
|
});
|
|
await db.insert(issues).values([
|
|
{
|
|
id: firstIssueId,
|
|
companyId,
|
|
title: "First assignment",
|
|
status: "todo",
|
|
priority: "high",
|
|
assigneeAgentId: agentId,
|
|
responsibleUserId: "responsible-user",
|
|
},
|
|
{
|
|
id: secondIssueId,
|
|
companyId,
|
|
title: "Second assignment",
|
|
status: "todo",
|
|
priority: "high",
|
|
assigneeAgentId: agentId,
|
|
responsibleUserId: "responsible-user",
|
|
},
|
|
]);
|
|
|
|
try {
|
|
const firstWake = await heartbeat.wakeup(agentId, {
|
|
source: "assignment",
|
|
triggerDetail: "system",
|
|
reason: "issue_assigned",
|
|
payload: { issueId: firstIssueId },
|
|
contextSnapshot: { issueId: firstIssueId, wakeReason: "issue_assigned" },
|
|
});
|
|
expect(firstWake).not.toBeNull();
|
|
await db.insert(issueComments).values({
|
|
companyId,
|
|
issueId: firstIssueId,
|
|
authorAgentId: agentId,
|
|
authorType: "agent",
|
|
createdByRunId: firstWake!.id,
|
|
body: "First assignment run completed.",
|
|
});
|
|
|
|
const firstRunStarted = await waitForCondition(async () => {
|
|
const run = await db
|
|
.select({ status: heartbeatRuns.status })
|
|
.from(heartbeatRuns)
|
|
.where(eq(heartbeatRuns.id, firstWake!.id))
|
|
.then((rows) => rows[0] ?? null);
|
|
return run?.status === "running";
|
|
});
|
|
expect(firstRunStarted).toBe(true);
|
|
const firstAdapterStarted = await waitForCondition(async () => mockAdapterExecute.mock.calls.length === 1, 30_000);
|
|
expect(firstAdapterStarted).toBe(true);
|
|
|
|
const secondWake = await heartbeat.wakeup(agentId, {
|
|
source: "assignment",
|
|
triggerDetail: "system",
|
|
reason: "issue_assigned",
|
|
payload: { issueId: secondIssueId },
|
|
contextSnapshot: { issueId: secondIssueId, wakeReason: "issue_assigned" },
|
|
});
|
|
expect(secondWake).not.toBeNull();
|
|
await db.insert(issueComments).values({
|
|
companyId,
|
|
issueId: secondIssueId,
|
|
authorAgentId: agentId,
|
|
authorType: "agent",
|
|
createdByRunId: secondWake!.id,
|
|
body: "Second assignment run completed.",
|
|
});
|
|
|
|
const secondRunWhileFirstRunning = await db
|
|
.select({ status: heartbeatRuns.status })
|
|
.from(heartbeatRuns)
|
|
.where(eq(heartbeatRuns.id, secondWake!.id))
|
|
.then((rows) => rows[0] ?? null);
|
|
expect(secondRunWhileFirstRunning?.status).toBe("queued");
|
|
expect(mockAdapterExecute).toHaveBeenCalledTimes(1);
|
|
|
|
finishFirstRun();
|
|
|
|
const firstRunSucceeded = await waitForCondition(async () => {
|
|
const run = await db
|
|
.select({ status: heartbeatRuns.status })
|
|
.from(heartbeatRuns)
|
|
.where(eq(heartbeatRuns.id, firstWake!.id))
|
|
.then((rows) => rows[0] ?? null);
|
|
return run?.status === "succeeded";
|
|
});
|
|
expect(firstRunSucceeded).toBe(true);
|
|
|
|
const secondRunSucceeded = await waitForCondition(async () => {
|
|
const run = await db
|
|
.select({ status: heartbeatRuns.status })
|
|
.from(heartbeatRuns)
|
|
.where(eq(heartbeatRuns.id, secondWake!.id))
|
|
.then((rows) => rows[0] ?? null);
|
|
return run?.status === "succeeded";
|
|
}, 10_000);
|
|
expect(secondRunSucceeded).toBe(true);
|
|
expect(mockAdapterExecute.mock.calls.length).toBeGreaterThanOrEqual(2);
|
|
} finally {
|
|
finishFirstRun();
|
|
}
|
|
}, 40_000);
|
|
|
|
it("cancels stale queued runs when issue blockers are still unresolved", async () => {
|
|
const companyId = randomUUID();
|
|
const agentId = randomUUID();
|
|
const blockerId = randomUUID();
|
|
const blockedIssueId = randomUUID();
|
|
const readyIssueId = randomUUID();
|
|
const blockedWakeupRequestId = randomUUID();
|
|
const readyWakeupRequestId = randomUUID();
|
|
const blockedRunId = randomUUID();
|
|
const readyRunId = randomUUID();
|
|
|
|
await db.insert(companies).values({
|
|
id: companyId,
|
|
name: "Paperclip",
|
|
issuePrefix: `T${companyId.replace(/-/g, "").slice(0, 6).toUpperCase()}`,
|
|
requireBoardApprovalForNewAgents: false,
|
|
defaultResponsibleUserId: "responsible-user",
|
|
});
|
|
await db.insert(companyMemberships).values({ companyId, principalType: "user", principalId: "responsible-user", status: "active", membershipRole: "member" });
|
|
await db.insert(agents).values({
|
|
id: agentId,
|
|
companyId,
|
|
name: "QAChecker",
|
|
role: "qa",
|
|
status: "active",
|
|
adapterType: "codex_local",
|
|
adapterConfig: {},
|
|
runtimeConfig: {
|
|
heartbeat: {
|
|
wakeOnDemand: true,
|
|
maxConcurrentRuns: 2,
|
|
},
|
|
},
|
|
permissions: {},
|
|
});
|
|
await db.insert(issues).values([
|
|
{
|
|
id: blockerId,
|
|
companyId,
|
|
title: "Security review",
|
|
status: "blocked",
|
|
priority: "high",
|
|
responsibleUserId: "responsible-user",
|
|
},
|
|
{
|
|
id: blockedIssueId,
|
|
companyId,
|
|
title: "QA validation",
|
|
status: "blocked",
|
|
priority: "medium",
|
|
assigneeAgentId: agentId,
|
|
responsibleUserId: "responsible-user",
|
|
},
|
|
{
|
|
id: readyIssueId,
|
|
companyId,
|
|
title: "Ready QA task",
|
|
status: "todo",
|
|
priority: "low",
|
|
assigneeAgentId: agentId,
|
|
responsibleUserId: "responsible-user",
|
|
},
|
|
]);
|
|
await db.insert(issueRelations).values({
|
|
companyId,
|
|
issueId: blockerId,
|
|
relatedIssueId: blockedIssueId,
|
|
type: "blocks",
|
|
});
|
|
await db.insert(agentWakeupRequests).values([
|
|
{
|
|
id: blockedWakeupRequestId,
|
|
companyId,
|
|
agentId,
|
|
source: "automation",
|
|
triggerDetail: "system",
|
|
reason: "transient_failure_retry",
|
|
payload: { issueId: blockedIssueId },
|
|
status: "queued",
|
|
},
|
|
{
|
|
id: readyWakeupRequestId,
|
|
companyId,
|
|
agentId,
|
|
source: "assignment",
|
|
triggerDetail: "system",
|
|
reason: "issue_assigned",
|
|
payload: { issueId: readyIssueId },
|
|
status: "queued",
|
|
},
|
|
]);
|
|
await db.insert(heartbeatRuns).values([
|
|
{
|
|
id: blockedRunId,
|
|
companyId,
|
|
agentId,
|
|
invocationSource: "automation",
|
|
triggerDetail: "system",
|
|
status: "queued",
|
|
wakeupRequestId: blockedWakeupRequestId,
|
|
contextSnapshot: {
|
|
issueId: blockedIssueId,
|
|
wakeReason: "transient_failure_retry",
|
|
},
|
|
},
|
|
{
|
|
id: readyRunId,
|
|
companyId,
|
|
agentId,
|
|
invocationSource: "assignment",
|
|
triggerDetail: "system",
|
|
status: "queued",
|
|
wakeupRequestId: readyWakeupRequestId,
|
|
contextSnapshot: {
|
|
issueId: readyIssueId,
|
|
wakeReason: "issue_assigned",
|
|
},
|
|
},
|
|
]);
|
|
await db
|
|
.update(agentWakeupRequests)
|
|
.set({ runId: blockedRunId })
|
|
.where(eq(agentWakeupRequests.id, blockedWakeupRequestId));
|
|
await db
|
|
.update(agentWakeupRequests)
|
|
.set({ runId: readyRunId })
|
|
.where(eq(agentWakeupRequests.id, readyWakeupRequestId));
|
|
await db.insert(issueComments).values({
|
|
companyId,
|
|
issueId: readyIssueId,
|
|
authorAgentId: agentId,
|
|
authorType: "agent",
|
|
createdByRunId: readyRunId,
|
|
body: "Ready queued run completed.",
|
|
});
|
|
await db
|
|
.update(issues)
|
|
.set({
|
|
executionRunId: blockedRunId,
|
|
executionAgentNameKey: "qa-checker",
|
|
executionLockedAt: new Date(),
|
|
})
|
|
.where(eq(issues.id, blockedIssueId));
|
|
|
|
mockAdapterExecute.mockImplementationOnce(async () => {
|
|
await db.update(issues).set({ status: "done" }).where(eq(issues.id, readyIssueId));
|
|
return { exitCode: 0, signal: null, timedOut: false, errorMessage: null,
|
|
summary: "Ready queued run completed.", provider: "test", model: "test-model" };
|
|
});
|
|
await heartbeat.resumeQueuedRuns();
|
|
|
|
expect(await waitForCondition(async () => {
|
|
const [ready] = await db.select({ status: heartbeatRuns.status }).from(heartbeatRuns).where(eq(heartbeatRuns.id, readyRunId));
|
|
return ready?.status === "succeeded";
|
|
}, 30_000)).toBe(true);
|
|
await heartbeat.drainActiveRunExecutions();
|
|
|
|
const [blockedRun, blockedWakeup, blockedIssue, readyRun] = await Promise.all([
|
|
db
|
|
.select({
|
|
status: heartbeatRuns.status,
|
|
errorCode: heartbeatRuns.errorCode,
|
|
finishedAt: heartbeatRuns.finishedAt,
|
|
resultJson: heartbeatRuns.resultJson,
|
|
})
|
|
.from(heartbeatRuns)
|
|
.where(eq(heartbeatRuns.id, blockedRunId))
|
|
.then((rows) => rows[0] ?? null),
|
|
db
|
|
.select({
|
|
status: agentWakeupRequests.status,
|
|
error: agentWakeupRequests.error,
|
|
})
|
|
.from(agentWakeupRequests)
|
|
.where(eq(agentWakeupRequests.id, blockedWakeupRequestId))
|
|
.then((rows) => rows[0] ?? null),
|
|
db
|
|
.select({
|
|
executionRunId: issues.executionRunId,
|
|
executionAgentNameKey: issues.executionAgentNameKey,
|
|
executionLockedAt: issues.executionLockedAt,
|
|
})
|
|
.from(issues)
|
|
.where(eq(issues.id, blockedIssueId))
|
|
.then((rows) => rows[0] ?? null),
|
|
db
|
|
.select({ status: heartbeatRuns.status })
|
|
.from(heartbeatRuns)
|
|
.where(eq(heartbeatRuns.id, readyRunId))
|
|
.then((rows) => rows[0] ?? null),
|
|
]);
|
|
|
|
expect(blockedRun?.status).toBe("cancelled");
|
|
expect(blockedRun?.errorCode).toBe("issue_dependencies_blocked");
|
|
expect(blockedRun?.finishedAt).toBeTruthy();
|
|
expect(blockedRun?.resultJson).toMatchObject({ stopReason: "issue_dependencies_blocked" });
|
|
expect(blockedRun?.resultJson).toMatchObject({ cancellation: { source: "control_plane", expected: true } });
|
|
const [waitingIssue] = await db.select().from(issues).where(eq(issues.id, blockedIssueId));
|
|
expect(await taskInterruptionPresentation(db, waitingIssue!)).toMatchObject({ state: "resolved", canRetry: false });
|
|
await db.update(heartbeatRuns).set({ resultJson: { ...blockedRun!.resultJson, timeoutFired: true } })
|
|
.where(eq(heartbeatRuns.id, blockedRunId));
|
|
expect(await taskInterruptionPresentation(db, waitingIssue!)).toMatchObject({ state: "failed", canRetry: true });
|
|
await db.update(heartbeatRuns).set({ resultJson: blockedRun!.resultJson }).where(eq(heartbeatRuns.id, blockedRunId));
|
|
expect(blockedWakeup?.status).toBe("skipped");
|
|
expect(blockedWakeup?.error).toContain("dependencies are still blocked");
|
|
expect(blockedIssue).toMatchObject({
|
|
executionRunId: null,
|
|
executionAgentNameKey: null,
|
|
executionLockedAt: null,
|
|
});
|
|
expect(readyRun?.status).toBe("succeeded");
|
|
expect(mockAdapterExecute).toHaveBeenCalledTimes(1);
|
|
}, 40_000);
|
|
|
|
it("suppresses normal wakeups while allowing comment interaction wakes under a pause hold", async () => {
|
|
const companyId = randomUUID();
|
|
const agentId = randomUUID();
|
|
const rootIssueId = randomUUID();
|
|
const issueChain = Array.from({ length: 17 }, () => randomUUID());
|
|
const deepDescendantIssueId = issueChain.at(-1)!;
|
|
|
|
await db.insert(companies).values({
|
|
id: companyId,
|
|
name: "Paperclip",
|
|
issuePrefix: `T${companyId.replace(/-/g, "").slice(0, 6).toUpperCase()}`,
|
|
requireBoardApprovalForNewAgents: false,
|
|
defaultResponsibleUserId: "responsible-user",
|
|
});
|
|
await db.insert(companyMemberships).values({ companyId, principalType: "user", principalId: "responsible-user", status: "active", membershipRole: "member" });
|
|
await db.insert(agents).values({
|
|
id: agentId,
|
|
companyId,
|
|
name: "SecurityEngineer",
|
|
role: "engineer",
|
|
status: "active",
|
|
adapterType: "codex_local",
|
|
adapterConfig: {},
|
|
runtimeConfig: {
|
|
heartbeat: {
|
|
wakeOnDemand: true,
|
|
maxConcurrentRuns: 1,
|
|
},
|
|
},
|
|
permissions: {},
|
|
});
|
|
await db.insert(issues).values([
|
|
{
|
|
id: rootIssueId,
|
|
companyId,
|
|
title: "Paused root",
|
|
status: "todo",
|
|
priority: "medium",
|
|
assigneeAgentId: agentId,
|
|
responsibleUserId: "responsible-user",
|
|
},
|
|
...issueChain.map((issueId, index) => ({
|
|
id: issueId,
|
|
companyId,
|
|
parentId: index === 0 ? rootIssueId : issueChain[index - 1],
|
|
title: `Paused desc ${index + 1}`,
|
|
status: "todo",
|
|
priority: "medium",
|
|
assigneeAgentId: agentId,
|
|
responsibleUserId: "responsible-user",
|
|
})),
|
|
]);
|
|
const [hold] = await db
|
|
.insert(issueTreeHolds)
|
|
.values({
|
|
companyId,
|
|
rootIssueId,
|
|
mode: "pause",
|
|
status: "active",
|
|
reason: "security test hold",
|
|
releasePolicy: { strategy: "manual" },
|
|
})
|
|
.returning();
|
|
|
|
const blockedWake = await heartbeat.wakeup(agentId, {
|
|
source: "automation",
|
|
triggerDetail: "system",
|
|
reason: "issue_blockers_resolved",
|
|
payload: { issueId: deepDescendantIssueId },
|
|
contextSnapshot: { issueId: deepDescendantIssueId, wakeReason: "issue_blockers_resolved" },
|
|
});
|
|
|
|
expect(blockedWake).toBeNull();
|
|
const skippedWake = await db
|
|
.select({
|
|
status: agentWakeupRequests.status,
|
|
reason: agentWakeupRequests.reason,
|
|
})
|
|
.from(agentWakeupRequests)
|
|
.where(sql`${agentWakeupRequests.payload} ->> 'issueId' = ${deepDescendantIssueId}`)
|
|
.then((rows) => rows[0] ?? null);
|
|
expect(skippedWake).toMatchObject({ status: "skipped", reason: "issue_tree_hold_active" });
|
|
|
|
const childCommentId = randomUUID();
|
|
await db.insert(issueComments).values({
|
|
id: childCommentId,
|
|
companyId,
|
|
issueId: deepDescendantIssueId,
|
|
authorUserId: "board-user",
|
|
body: "Please respond while this hold is active.",
|
|
});
|
|
|
|
const forgedChildCommentWake = await heartbeat.wakeup(agentId, {
|
|
source: "on_demand",
|
|
triggerDetail: "manual",
|
|
reason: "issue_commented",
|
|
payload: { issueId: deepDescendantIssueId, commentId: childCommentId },
|
|
requestedByActorType: "agent",
|
|
requestedByActorId: agentId,
|
|
});
|
|
expect(forgedChildCommentWake).toBeNull();
|
|
|
|
const childCommentWake = await heartbeat.wakeup(agentId, {
|
|
source: "automation",
|
|
triggerDetail: "system",
|
|
reason: "issue_commented",
|
|
payload: { issueId: deepDescendantIssueId, commentId: childCommentId },
|
|
requestedByActorType: "user",
|
|
requestedByActorId: "board-user",
|
|
contextSnapshot: {
|
|
issueId: deepDescendantIssueId,
|
|
commentId: childCommentId,
|
|
wakeCommentId: childCommentId,
|
|
wakeReason: "issue_commented",
|
|
source: "issue.comment",
|
|
},
|
|
});
|
|
|
|
expect(childCommentWake).not.toBeNull();
|
|
const childRun = await db
|
|
.select({ contextSnapshot: heartbeatRuns.contextSnapshot })
|
|
.from(heartbeatRuns)
|
|
.where(eq(heartbeatRuns.id, childCommentWake!.id))
|
|
.then((rows) => rows[0] ?? null);
|
|
expect(childRun?.contextSnapshot).toMatchObject({
|
|
treeHoldInteraction: true,
|
|
activeTreeHold: {
|
|
holdId: hold.id,
|
|
rootIssueId,
|
|
mode: "pause",
|
|
interaction: true,
|
|
},
|
|
});
|
|
});
|
|
|
|
it("allows comment interaction wakes when a legacy hold has a full_pause note", async () => {
|
|
const companyId = randomUUID();
|
|
const agentId = randomUUID();
|
|
const rootIssueId = randomUUID();
|
|
|
|
await db.insert(companies).values({
|
|
id: companyId,
|
|
name: "Paperclip",
|
|
issuePrefix: `T${companyId.replace(/-/g, "").slice(0, 6).toUpperCase()}`,
|
|
requireBoardApprovalForNewAgents: false,
|
|
defaultResponsibleUserId: "responsible-user",
|
|
});
|
|
await db.insert(companyMemberships).values({ companyId, principalType: "user", principalId: "responsible-user", status: "active", membershipRole: "member" });
|
|
await db.insert(agents).values({
|
|
id: agentId,
|
|
companyId,
|
|
name: "SecurityEngineer",
|
|
role: "engineer",
|
|
status: "active",
|
|
adapterType: "codex_local",
|
|
adapterConfig: {},
|
|
runtimeConfig: {
|
|
heartbeat: {
|
|
wakeOnDemand: true,
|
|
maxConcurrentRuns: 1,
|
|
},
|
|
},
|
|
permissions: {},
|
|
});
|
|
await db.insert(issues).values({
|
|
id: rootIssueId,
|
|
companyId,
|
|
title: "Paused root",
|
|
status: "todo",
|
|
priority: "medium",
|
|
assigneeAgentId: agentId,
|
|
responsibleUserId: "responsible-user",
|
|
});
|
|
await db.insert(issueTreeHolds).values({
|
|
companyId,
|
|
rootIssueId,
|
|
mode: "pause",
|
|
status: "active",
|
|
reason: "full pause",
|
|
releasePolicy: { strategy: "manual", note: "full_pause" },
|
|
});
|
|
|
|
const rootCommentId = randomUUID();
|
|
await db.insert(issueComments).values({
|
|
id: rootCommentId,
|
|
companyId,
|
|
issueId: rootIssueId,
|
|
authorUserId: "board-user",
|
|
body: "Please respond while this hold is active.",
|
|
});
|
|
|
|
const rootCommentWake = await heartbeat.wakeup(agentId, {
|
|
source: "automation",
|
|
triggerDetail: "system",
|
|
reason: "issue_commented",
|
|
payload: { issueId: rootIssueId, commentId: rootCommentId },
|
|
requestedByActorType: "user",
|
|
requestedByActorId: "board-user",
|
|
contextSnapshot: {
|
|
issueId: rootIssueId,
|
|
commentId: rootCommentId,
|
|
wakeCommentId: rootCommentId,
|
|
wakeReason: "issue_commented",
|
|
source: "issue.comment",
|
|
},
|
|
});
|
|
|
|
expect(rootCommentWake).not.toBeNull();
|
|
const rootRun = await db
|
|
.select({ contextSnapshot: heartbeatRuns.contextSnapshot })
|
|
.from(heartbeatRuns)
|
|
.where(eq(heartbeatRuns.id, rootCommentWake!.id))
|
|
.then((rows) => rows[0] ?? null);
|
|
expect(rootRun?.contextSnapshot).toMatchObject({
|
|
treeHoldInteraction: true,
|
|
activeTreeHold: {
|
|
rootIssueId,
|
|
mode: "pause",
|
|
interaction: true,
|
|
},
|
|
});
|
|
});
|
|
});
|