mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-03 17:51:58 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work. > - Native runs can retain a local session after an interrupted durable suspend. > - The orphan reaper repeatedly scans completed history for that cleanup duty. > - Idle scans keep the database active. Removing them requires complete wake and ownership paths. > - This pull request moves retained-session cleanup to committed signals, local release events and saved deadlines. > - Existing physical proof checks still decide whether cleanup is safe. The shared scheduler does not grant execution authority. > - The benefit is quiet retained history with recoverable cleanup and owned shutdown. ## Linked Issues or Issue Description Refs #17709 and #18135. This is slice 51 of the existing recurring-server sleep stack. Both referenced dependencies have merged. The target is `master`. **What happened?** The periodic orphan pass scans retained native sessions even after their cleanup is complete or no automatic action is safe. Some eligible sessions also depend on a local session, environment lease or original maintenance callback releasing ownership. An earlier decision notification can be consumed before the run's later terminal projection commits. **Expected behavior** Reconstruct existing work at startup. Wake from committed writes and actual owner release. Register the saved claim expiry or a probe for a specific live process. Stop recurring discovery when no obligation remains. Preserve original callback ownership and all existing cleanup proof checks. **Steps to reproduce** 1. Retain completed native runs with a durable-suspend close failure. 2. Let the periodic orphan pass run with no new work. 3. Observe repeated cleanup discovery. Separately, delay terminal projection after its decision notification is consumed and observe the missing cleanup wake. **Paperclip version or commit** Parent #17709 at reviewed head `3b75f4bc1da1370304f069fc287af3d693575716`, landed as `df6156c2e37f542a69f393dde31e357b82b7cac1`. Current integrated master: `31b927e3013a8a4b3266854b740cf40a24753788`. ## What Changed - Move scoped, paged discovery to the retained cleanup owner. Preserve explicit manual recovery. - Register the duty with the shared scheduler and remove it from the periodic orphan pass. Keep concrete lease expiry and live-process probe obligations. - Use typed outcomes for settled/refused work and process, session, environment, maintenance or lease waits. Keep physical proof and quarantine decisions in the executor. - Wake from committed finalization and terminal projection writes, durable process/lease release, and local session or callback settlement. Routine company-only finalization hints leave dormant cleanup quiet. - Join duplicate cleanup and serialize inspections on one database lane. Keep original Runner callbacks owned until settlement and shutdown. - Preserve all master profile fixtures and the branch's historical identities. Save the colliding sleep profile 79 separately. Generate combined Copilot profile 80 from its source and retain master's other provider profiles. - Add the owner audit and regression coverage. No new table, migration or independent maintenance timer is added. ## Verification - Server acceptance: **2,342 distinct passing cases in 18 complete files**. The 14-file unaffected group contributes 1,532 passes. Full startup contributes 76, process recovery 410, environment service 51, and external-chat response 273. The 29-case cleanup worker suite and 977-case executor suite run in full. No case in these files is skipped. - Controlled scope regression: removing only the ordinary finalization issue/run guard wakes dormant retained cleanup; the same assertion passes after restoring it. A real local owner release still resumes the durable candidate. - Preserve the first broad failure: 1,607 passed and one startup shutdown case failed because the coordinator mock lacked `stop()`. Add only that mock method. All 76 unchanged startup cases then pass. Do not treat the first broad command as green. - Dedicated PostgreSQL 17 databases use the existing migration 413 schema. Embedded-only fixtures use the existing recovery helper with fresh per-call databases. All temporary imports are restored byte-exact. No assertion or timeout is relaxed. - Current-master integration preserves both complete source histories. The server, UI and database trees are identical to the server-tested source. All master historical profile fixtures remain byte-exact. The prepared sleep profile 79 remains independently inspectable. - Current runtime validation at `d08f764e3efe75e96929173afe7ee8739121ad9f` passes all 12 commands (22 installed ACPX/Grok contract cases and 26 native-provider Rust cases): `pnpm install --frozen-lockfile --ignore-scripts`; Copilot/Grok identity and generated profile checks; Runner and server typechecking; server build; Copilot/Pi installation, profile compatibility and transport settlement files (130 passed; one opt-in closed Pi snapshot case skipped because its distribution root is not configured); installed ACPX model/reconnect package contract and Grok identity tests; Rust native-provider backend tests; module boundaries and migration order. - Earlier 94-case installation/settlement acceptance applies to the prepared profile 79 source. It is retained as history and does not substitute for the current profile 80 validation above. - Failure history and duty-specific evidence remain in `doc/plans/2026-10-26-retained-native-session-cleanup-sleep-audit.md`. Exact-current-head acceptance: all 60 checks are clean, including protected CI and security; Greptile gives 5/5 for `281d579de0d08e907194b8b790a0597b07d16b7b` with no actionable findings. No Ponytail review is present under the authorized missing-review policy. - These checks do not claim a full local repository suite, live Copilot or Daytona qualification, deployed host/database suspension, or measured cost savings. ## Risks - Missing a supported producer could strand cleanup. Tests exercise real finalizer, heartbeat, environment and process writers. Process-local signals cover in-process writers; no multi-replica or external SQL notification transport is added. - A timeout can leave an original callback active. Cleanup and shutdown must keep waiting for it. The existing global maintenance drain is conservative across roots. - A retained live process still has a scoped 30-second absence probe. A saved lease keeps its actual deadline. Operational errors retain retry work. Immutable proof refusals and operator-required histories do not gain automatic retry authority. - Cleanup keeps all existing artifact, identity, claim, no-launch and quarantine checks. Terminal status alone does not authorize physical cleanup. - This PR does not expand Cloud alarm coverage or relax host sleep eligibility. It proves worker behavior, not deployed app/database suspension. - The Copilot source identity has changed; these checks do not claim new live provider qualification. - The parent has merged. This PR is merged only after its current-head protected CI and review gates pass under the existing campaign authorization. ## Model Used OpenAI Codex, GPT-6 family, with code execution and repository tools. The exact serving variant and context-window size are not exposed in this session. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [x] All Paperclip CI gates are green - [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge --------- Co-authored-by: Paperclip <noreply@paperclip.ing> Co-authored-by: Codex <noreply@openai.com>
374 lines
22 KiB
TypeScript
374 lines
22 KiB
TypeScript
import { resolveExternalTestDatabaseUrl } from "../../__tests__/helpers/external-test-database.js";
|
|
import { randomBytes } from "node:crypto";
|
|
import type { Db } from "@paperclipai/db";
|
|
import { agents, agentWakeupRequests, companies, createDb, EMBEDDED_POSTGRES_TEST_TIMEOUT_MS, heartbeatRunEvents, heartbeatRuns, issues, subscribeDatabaseWork } from "@paperclipai/db";
|
|
import { eq, sql } from "drizzle-orm";
|
|
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
|
import { getEmbeddedPostgresTestSupport, startEmbeddedPostgresTestDatabase } from "../../__tests__/helpers/embedded-postgres.js";
|
|
import type { CurrentUserRedactionOptions } from "../../log-redaction.js";
|
|
import { publishLiveEvent } from "../live-events.js";
|
|
import { getHeartbeatRunRuntimeStatus } from "../heartbeat-run-runtime-status.js";
|
|
import { createHeartbeatRunState } from "./run-state.js";
|
|
import { heartbeatService } from "../heartbeat.js";
|
|
import { createHeartbeatRunPreparation } from "./run-preparation.js";
|
|
import { createHeartbeatLifecycle, persistHeartbeatRunProcessMetadata, type HeartbeatLifecycleDependencies } from "./run-lifecycle.js";
|
|
import { DELIVERY_QUEUES, subscribeDeliveryWork } from "../delivery-work-notifications.js";
|
|
|
|
vi.mock("../live-events.js", () => ({ publishLiveEvent: vi.fn() }));
|
|
|
|
type Run = typeof heartbeatRuns.$inferSelect;
|
|
function callbacks(db: Db) {
|
|
const state = createHeartbeatRunState(db);
|
|
return {
|
|
...state,
|
|
...createHeartbeatRunPreparation(db),
|
|
getRunStatus: vi.fn(state.getRunStatus),
|
|
issuesSvc: {
|
|
addComment: vi.fn<HeartbeatLifecycleDependencies["issuesSvc"]["addComment"]>(),
|
|
listReviewAttention: vi.fn<HeartbeatLifecycleDependencies["issuesSvc"]["listReviewAttention"]>(),
|
|
},
|
|
recovery: { escalateStrandedAssignedIssue: vi.fn<HeartbeatLifecycleDependencies["recovery"]["escalateStrandedAssignedIssue"]>() },
|
|
companySkills: { completeTestRunForIssue: vi.fn<HeartbeatLifecycleDependencies["companySkills"]["completeTestRunForIssue"]>() },
|
|
budgets: { getInvocationBlock: vi.fn(async () => null) },
|
|
treeControlSvc: { getActivePauseHoldGate: vi.fn(async () => null) },
|
|
enqueueWakeup: vi.fn<HeartbeatLifecycleDependencies["enqueueWakeup"]>(),
|
|
getCurrentUserRedactionOptions: vi.fn(async (): Promise<CurrentUserRedactionOptions> => ({})),
|
|
getAgentInvokability: vi.fn<HeartbeatLifecycleDependencies["getAgentInvokability"]>(async () => ({ invokable: true })),
|
|
emitTerminalAgentTaskRun: vi.fn<HeartbeatLifecycleDependencies["emitTerminalAgentTaskRun"]>(),
|
|
budgetHooks: {},
|
|
} satisfies HeartbeatLifecycleDependencies;
|
|
}
|
|
function guardedDatabase() {
|
|
const access = vi.fn(() => { throw new Error("Unexpected lifecycle database access"); });
|
|
return { db: new Proxy({}, { get: access }) as Db, access };
|
|
}
|
|
|
|
describe("heartbeat lifecycle module boundary", () => {
|
|
it("constructs without database work or reporting effects", () => {
|
|
const database = guardedDatabase();
|
|
const deps = callbacks(database.db);
|
|
createHeartbeatLifecycle(database.db, deps);
|
|
expect(database.access).not.toHaveBeenCalled();
|
|
for (const callback of Object.values(deps)) if (vi.isMockFunction(callback)) expect(callback).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("ignores absent wake requests without accessing the database", async () => {
|
|
const database = guardedDatabase();
|
|
const lifecycle = createHeartbeatLifecycle(database.db, callbacks(database.db));
|
|
await lifecycle.setWakeupStatus(null, "completed");
|
|
await lifecycle.setWakeupStatus(undefined, "completed");
|
|
expect(database.access).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it.each([
|
|
{ livenessState: "empty_response", contextSnapshot: { goalControlRequestId: "goal" } },
|
|
{ livenessState: "plan_only", contextSnapshot: { resumeSessionGoalHeartbeat: true } },
|
|
{ livenessState: "productive", contextSnapshot: { issueId: "issue" } },
|
|
{ livenessState: "empty_response", contextSnapshot: {} },
|
|
])("keeps ineligible continuations out of wake admission (%j)", async (input) => {
|
|
const database = guardedDatabase();
|
|
const deps = callbacks(database.db);
|
|
await createHeartbeatLifecycle(database.db, deps).handleRunLivenessContinuation(input as unknown as Run);
|
|
expect(database.access).not.toHaveBeenCalled();
|
|
expect(deps.enqueueWakeup).not.toHaveBeenCalled();
|
|
expect(deps.budgets.getInvocationBlock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("preserves retained native ownership before lease-release teardown reads or writes", async () => {
|
|
const database = guardedDatabase();
|
|
const deps = callbacks(database.db);
|
|
const run = { runtimeMode: "native", status: "running", nativePhase: "terminal_failure", errorCode: "native_execution_ownership_unverified" } as Run;
|
|
expect(await createHeartbeatLifecycle(database.db, deps).terminalizeRunOnLeaseRelease(run)).toBe(run);
|
|
expect(database.access).not.toHaveBeenCalled();
|
|
expect(deps.emitTerminalAgentTaskRun).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("rejects late progress after re-reading a terminal run through the service callback", async () => {
|
|
const database = guardedDatabase();
|
|
const deps = callbacks(database.db);
|
|
const run = { id: "late-progress", companyId: "company", agentId: "agent", status: "running", contextSnapshot: {} } as Run;
|
|
deps.getRunStatus.mockResolvedValue({ ...run, status: "succeeded" });
|
|
expect(await createHeartbeatLifecycle(database.db, deps).recordCurrentHeartbeatRunRuntimeProgress(run, { phase: "finalize", message: "late update" }, null)).toBeNull();
|
|
expect(deps.getRunStatus).toHaveBeenCalledWith(run.id);
|
|
expect(getHeartbeatRunRuntimeStatus(run.id)).toBeNull();
|
|
expect(database.access).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("does not publish progress when the current status read fails", async () => {
|
|
const database = guardedDatabase();
|
|
const deps = callbacks(database.db);
|
|
deps.getRunStatus.mockRejectedValue(new Error("status read failed"));
|
|
const run = { id: "unavailable-progress", companyId: "company", agentId: "agent", status: "running" };
|
|
vi.mocked(publishLiveEvent).mockClear();
|
|
await expect(createHeartbeatLifecycle(database.db, deps)
|
|
.recordCurrentHeartbeatRunRuntimeProgress(run, { phase: "restore", message: "restoring" }, null))
|
|
.rejects.toThrow("status read failed");
|
|
expect(publishLiveEvent).not.toHaveBeenCalled();
|
|
expect(database.access).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
const external = resolveExternalTestDatabaseUrl("PAPERCLIP_LIFECYCLE_TEST_DATABASE_URL");
|
|
const support = external ? { supported: true as const } : await getEmbeddedPostgresTestSupport();
|
|
if (!support.supported) console.warn(`Skipping lifecycle module database tests: ${support.reason}`);
|
|
describe.skipIf(!support.supported)("heartbeat lifecycle database wiring", () => {
|
|
let database: Awaited<ReturnType<typeof startEmbeddedPostgresTestDatabase>>;
|
|
let db: Db;
|
|
beforeAll(async () => {
|
|
if (!external) database = await startEmbeddedPostgresTestDatabase("paperclip-heartbeat-lifecycle-module");
|
|
db = createDb(external ?? database.connectionString);
|
|
}, EMBEDDED_POSTGRES_TEST_TIMEOUT_MS);
|
|
afterAll(async () => {
|
|
await db?.$client.end();
|
|
await database?.cleanup();
|
|
});
|
|
beforeEach(async () => {
|
|
vi.mocked(publishLiveEvent).mockClear();
|
|
await db.execute(sql`truncate table companies restart identity cascade`);
|
|
});
|
|
|
|
async function fixture(patch: Partial<typeof heartbeatRuns.$inferInsert> = {}) {
|
|
const [company] = await db.insert(companies).values({ name: "Lifecycle module", issuePrefix: "LM" }).returning();
|
|
const [agent] = await db.insert(agents).values({ companyId: company.id, name: "Lifecycle agent" }).returning();
|
|
const [wake] = await db.insert(agentWakeupRequests).values({ companyId: company.id, agentId: agent.id, source: "assignment", status: "claimed" }).returning();
|
|
const [run] = await db.insert(heartbeatRuns).values({ companyId: company.id, agentId: agent.id, wakeupRequestId: wake.id, status: "running", runtimeMode: "native", ...patch }).returning();
|
|
return { company, agent, wake, run };
|
|
}
|
|
|
|
it.each(["succeeded", "failed", "cancelled", "timed_out", "interrupted", "deleted"])(
|
|
"rechecks progress after a run becomes %s without reading its large payloads",
|
|
async (status) => {
|
|
const { run } = await fixture({
|
|
contextSnapshot: { prompt: randomBytes(128 * 1024).toString("hex") },
|
|
runnerProfileJson: { configuration: randomBytes(128 * 1024).toString("hex") },
|
|
resultJson: { providerData: randomBytes(128 * 1024).toString("hex") },
|
|
});
|
|
const deps = callbacks(db);
|
|
const lifecycle = createHeartbeatLifecycle(db, deps);
|
|
const update = { phase: "restore" as const, message: "Restoring workspace", currentToolName: "sync" };
|
|
const progress = await lifecycle.recordCurrentHeartbeatRunRuntimeProgress(run, update, null);
|
|
expect(progress).toMatchObject({
|
|
companyId: run.companyId, agentId: run.agentId, runId: run.id,
|
|
phase: update.phase, message: update.message, currentToolName: "sync",
|
|
});
|
|
expect(await deps.getRunStatus.mock.results[0].value).toEqual({
|
|
id: run.id, companyId: run.companyId, agentId: run.agentId, status: "running",
|
|
});
|
|
expect(publishLiveEvent).toHaveBeenCalledTimes(1);
|
|
expect(publishLiveEvent).toHaveBeenCalledWith(expect.objectContaining({
|
|
companyId: run.companyId,
|
|
type: "heartbeat.run.progress",
|
|
payload: expect.objectContaining({ runId: run.id, agentId: run.agentId, phase: update.phase, message: update.message }),
|
|
}));
|
|
|
|
if (status === "deleted") await db.delete(heartbeatRuns).where(eq(heartbeatRuns.id, run.id));
|
|
else await db.update(heartbeatRuns).set({ status }).where(eq(heartbeatRuns.id, run.id));
|
|
|
|
expect(await lifecycle.recordCurrentHeartbeatRunRuntimeProgress(run, update, null)).toBeNull();
|
|
expect(deps.getRunStatus).toHaveBeenCalledTimes(2);
|
|
expect(getHeartbeatRunRuntimeStatus(run.id)).toBeNull();
|
|
expect(publishLiveEvent).toHaveBeenCalledTimes(1);
|
|
},
|
|
);
|
|
|
|
it("wires fresh progress status reads through the public heartbeat service", async () => {
|
|
const { run } = await fixture();
|
|
const service = heartbeatService(db);
|
|
const update = { phase: "config_sync" as const, message: "Syncing workspace" };
|
|
expect(await service.recordRuntimeProgress(run, update, null)).toMatchObject({ runId: run.id, message: update.message });
|
|
await db.update(heartbeatRuns).set({ status: "cancelled" }).where(eq(heartbeatRuns.id, run.id));
|
|
expect(await service.recordRuntimeProgress(run, update, null)).toBeNull();
|
|
expect(getHeartbeatRunRuntimeStatus(run.id)).toBeNull();
|
|
expect(publishLiveEvent).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("preserves sandbox process birth instead of inspecting a colliding controller PID", async () => {
|
|
const { run } = await fixture();
|
|
const remoteBirth = "2026-10-09T00:00:00.000Z";
|
|
const persisted = await persistHeartbeatRunProcessMetadata(db, run.id, {
|
|
pid: process.pid, processGroupId: null, startedAt: remoteBirth, targetKind: "remote",
|
|
});
|
|
expect(persisted?.processStartedAt?.toISOString()).toBe(remoteBirth);
|
|
expect(persisted?.processPid).toBe(process.pid);
|
|
});
|
|
|
|
it("keeps one terminal outcome and reports only the winner of competing writes", async () => {
|
|
const { run } = await fixture();
|
|
const deps = callbacks(db);
|
|
const lifecycle = createHeartbeatLifecycle(db, deps);
|
|
const results = await Promise.all([
|
|
lifecycle.setRunStatusIfRunning(run.id, "succeeded", { finishedAt: new Date() }),
|
|
lifecycle.setRunStatusIfRunning(run.id, "failed", { errorCode: "late_failure", finishedAt: new Date() }),
|
|
]);
|
|
const winner = results.find(result => result.updated)!;
|
|
expect(results.filter(result => result.updated)).toHaveLength(1);
|
|
const [persisted] = await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.id, run.id));
|
|
expect(persisted.status).toBe(winner.run?.status);
|
|
expect(results.every(result => result.run?.status === persisted.status)).toBe(true);
|
|
expect(deps.emitTerminalAgentTaskRun).toHaveBeenCalledTimes(1);
|
|
expect(deps.emitTerminalAgentTaskRun).toHaveBeenCalledWith(winner.run, "running", undefined);
|
|
expect(vi.mocked(publishLiveEvent).mock.calls.filter(([event]) => event.type === "heartbeat.run.status")).toHaveLength(1);
|
|
});
|
|
|
|
it("rechecks native ownership atomically before a terminal write", async () => {
|
|
const { run } = await fixture({ errorCode: "native_execution_ownership_unverified", nativePhase: "terminal_failure" });
|
|
const deps = callbacks(db);
|
|
const result = await createHeartbeatLifecycle(db, deps).setRunStatusIfRunning(run.id, "failed");
|
|
expect(result).toMatchObject({ updated: false, run: { status: "running", errorCode: run.errorCode } });
|
|
expect(deps.emitTerminalAgentTaskRun).not.toHaveBeenCalled();
|
|
expect(publishLiveEvent).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it.each(["setRunStatus", "setRunStatusIfRunning"] as const)("preserves accounting metadata while %s enriches usage", async (operation) => {
|
|
const { run } = await fixture({ usageJson: { accountingReceiptReady: false, usageSource: "per_run", inputTokens: 5 } });
|
|
await createHeartbeatLifecycle(db, callbacks(db))[operation](run.id, "succeeded", { usageJson: { inputTokens: 8, outputTokens: 3 } });
|
|
const [persisted] = await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.id, run.id));
|
|
expect(persisted.usageJson).toEqual({ accountingReceiptReady: false, usageSource: "per_run", inputTokens: 8, outputTokens: 3 });
|
|
});
|
|
|
|
it.each(["setRunStatus", "setRunStatusIfRunning"] as const)("signals committed terminal accounting before %s publishes a failed live event", async operation => {
|
|
const { run } = await fixture({ costAccountingPending: true });
|
|
const settled = vi.fn();
|
|
const unsubscribe = subscribeDeliveryWork(db, DELIVERY_QUEUES.accounting, settled);
|
|
vi.mocked(publishLiveEvent).mockImplementationOnce(() => { throw new Error("Live publication unavailable"); });
|
|
try {
|
|
await expect(createHeartbeatLifecycle(db, callbacks(db))[operation](run.id, "succeeded", { finishedAt: new Date() }))
|
|
.rejects.toThrow("Live publication unavailable");
|
|
const [persisted] = await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.id, run.id));
|
|
expect(persisted.status).toBe("succeeded");
|
|
expect(persisted.costAccountingPending).toBe(true);
|
|
expect(settled).toHaveBeenCalledOnce();
|
|
} finally { unsubscribe(); }
|
|
});
|
|
|
|
it.each(["native", "legacy", "unbound"] as const)("keeps %s run-status response recovery scoped to its task or run", async binding => {
|
|
const { run } = await fixture();
|
|
const [issue] = await db.insert(issues).values({ companyId: run.companyId, title: "Scope owner" }).returning();
|
|
await db.update(heartbeatRuns).set(binding === "native" ? { nativeIssueId: issue.id }
|
|
: binding === "legacy" ? { contextSnapshot: { issueId: issue.id } } : { contextSnapshot: {} }).where(eq(heartbeatRuns.id, run.id));
|
|
const hints: unknown[] = [];
|
|
const unsubscribe = subscribeDatabaseWork(db, DELIVERY_QUEUES.nativeResponsePresentation, (event, scopes) => {
|
|
if (event === "settled") for (const hint of scopes ?? []) hints.push(JSON.parse(hint));
|
|
});
|
|
try {
|
|
await createHeartbeatLifecycle(db, callbacks(db)).setRunStatusIfRunning(run.id, "succeeded", { finishedAt: new Date() });
|
|
expect(hints).toEqual([binding === "unbound" ? { companyId: run.companyId, runId: run.id }
|
|
: { companyId: run.companyId, issueId: issue.id }]);
|
|
expect((await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.id, run.id)))[0].status).toBe("succeeded");
|
|
} finally { unsubscribe(); }
|
|
});
|
|
|
|
it.each(["setRunStatus", "setRunStatusIfRunning"] as const)("wakes retained cleanup only after the outer %s terminal write commits", async operation => {
|
|
const { run } = await fixture();
|
|
const hints: unknown[] = [];
|
|
const unsubscribe = subscribeDatabaseWork(db, DELIVERY_QUEUES.retainedNativeSessionCleanup, (event, scopes) => {
|
|
if (event === "settled") for (const scope of scopes ?? []) hints.push(JSON.parse(scope));
|
|
});
|
|
try {
|
|
await db.transaction(async transaction => {
|
|
const tx = transaction as unknown as Db;
|
|
await createHeartbeatLifecycle(tx, callbacks(tx))[operation](run.id, "succeeded", { finishedAt: new Date() });
|
|
expect(hints).toEqual([]);
|
|
expect((await tx.select().from(heartbeatRuns).where(eq(heartbeatRuns.id, run.id)))[0].status).toBe("succeeded");
|
|
});
|
|
expect(hints).toEqual([{ companyId: run.companyId, runId: run.id }]);
|
|
expect((await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.id, run.id)))[0].finishedAt).not.toBeNull();
|
|
hints.length = 0;
|
|
|
|
await createHeartbeatLifecycle(db, callbacks(db)).setRunStatus(run.id, "succeeded", { error: "repaired metadata" });
|
|
expect(hints).toEqual([{ companyId: run.companyId, runId: run.id }]);
|
|
} finally { unsubscribe(); }
|
|
});
|
|
|
|
it("does not wake retained cleanup for running progress", async () => {
|
|
const { run } = await fixture();
|
|
const settled = vi.fn();
|
|
const unsubscribe = subscribeDeliveryWork(db, DELIVERY_QUEUES.retainedNativeSessionCleanup, settled);
|
|
try {
|
|
await createHeartbeatLifecycle(db, callbacks(db)).setRunStatus(run.id, "running");
|
|
expect(settled).not.toHaveBeenCalled();
|
|
} finally { unsubscribe(); }
|
|
});
|
|
|
|
it("does not request accounting for an unchanged running status", async () => {
|
|
const { run } = await fixture();
|
|
const settled = vi.fn();
|
|
const unsubscribe = subscribeDeliveryWork(db, DELIVERY_QUEUES.accounting, settled);
|
|
try {
|
|
await createHeartbeatLifecycle(db, callbacks(db)).setRunStatus(run.id, "running");
|
|
expect(settled).not.toHaveBeenCalled();
|
|
} finally { unsubscribe(); }
|
|
});
|
|
|
|
it("signals legacy terminal accounting after the nested terminalizer transaction commits", async () => {
|
|
const { run } = await fixture({ runtimeMode: "legacy", costAccountingPending: true });
|
|
const settled = vi.fn();
|
|
const unsubscribe = subscribeDeliveryWork(db, DELIVERY_QUEUES.accounting, settled);
|
|
try {
|
|
const result = await createHeartbeatLifecycle(db, callbacks(db)).setRunStatusIfRunning(run.id, "failed", { finishedAt: new Date() });
|
|
expect(result.updated).toBe(true);
|
|
expect(result.run?.status).toBe("failed");
|
|
expect(settled).toHaveBeenCalledOnce();
|
|
} finally { unsubscribe(); }
|
|
});
|
|
|
|
it.each(["claimed", "cancelled"] as const)("settles a %s wake without reviving a revoked request", async (status) => {
|
|
const { wake } = await fixture();
|
|
await db.update(agentWakeupRequests).set({ status }).where(eq(agentWakeupRequests.id, wake.id));
|
|
await createHeartbeatLifecycle(db, callbacks(db)).setWakeupStatus(wake.id, "completed", { reason: "settled" });
|
|
const [persisted] = await db.select().from(agentWakeupRequests).where(eq(agentWakeupRequests.id, wake.id));
|
|
expect(persisted.status).toBe(status === "cancelled" ? "cancelled" : "completed");
|
|
expect(persisted.reason).toBe(status === "cancelled" ? wake.reason : "settled");
|
|
});
|
|
|
|
it("publishes the persisted sequence for concurrent event writes", async () => {
|
|
const { run } = await fixture();
|
|
const lifecycle = createHeartbeatLifecycle(db, callbacks(db));
|
|
await Promise.all(["first", "second"].map(message => lifecycle.appendRunEvent(run, { eventType: "lifecycle", message })));
|
|
const rows = await db.select().from(heartbeatRunEvents).where(eq(heartbeatRunEvents.runId, run.id)).orderBy(heartbeatRunEvents.seq);
|
|
expect(rows.map(row => row.seq)).toEqual([1, 2]);
|
|
const published = vi.mocked(publishLiveEvent).mock.calls.map(([event]) => event).filter(event => event.type === "heartbeat.run.event");
|
|
expect(published).toHaveLength(2);
|
|
for (const row of rows) expect(published.find(event => event.payload?.seq === row.seq)?.payload).toMatchObject({ runId: run.id, message: row.message });
|
|
});
|
|
|
|
it.each(["legacy.case_handoff_cleanup_intent", "legacy.case_handoff_cleanup_verified"])(
|
|
"does not count controller cleanup bookkeeping as agent progress (%s)", async (eventType) => {
|
|
const { run } = await fixture({ status: "succeeded", runtimeMode: "legacy" });
|
|
const lifecycle = createHeartbeatLifecycle(db, callbacks(db));
|
|
await lifecycle.appendRunEvent(run, { eventType, stream: "system", message: "Controller cleanup receipt" });
|
|
const result = { summary: "I will inspect the repo next and then implement the fix." };
|
|
const planning = await lifecycle.classifyAndPersistRunLiveness(run, result);
|
|
expect(planning).toMatchObject({ livenessState: "plan_only", lastUsefulActionAt: null });
|
|
|
|
await lifecycle.appendRunEvent(run, { eventType: "item.completed", stream: "stdout", message: "Read the source file" });
|
|
const progressed = await lifecycle.classifyAndPersistRunLiveness(run, result);
|
|
expect(progressed?.livenessState).toBe("advanced");
|
|
expect(progressed?.lastUsefulActionAt).toBeInstanceOf(Date);
|
|
},
|
|
);
|
|
|
|
it("reads current redaction settings for each event and persists the published sanitized payload", async () => {
|
|
const { run } = await fixture();
|
|
const deps = callbacks(db);
|
|
deps.getCurrentUserRedactionOptions.mockResolvedValueOnce({ enabled: true, userNames: ["alice"], homeDirs: ["/Users/alice"] });
|
|
deps.getCurrentUserRedactionOptions.mockResolvedValueOnce({ enabled: true, userNames: ["bob"], homeDirs: ["/Users/bob"] });
|
|
const lifecycle = createHeartbeatLifecycle(db, deps);
|
|
for (const name of ["alice", "bob"]) await lifecycle.appendRunEvent(run, {
|
|
eventType: "lifecycle", message: `Opening /Users/${name}/project`, payload: { path: `/Users/${name}/project`, token: "test-credential-material" },
|
|
});
|
|
const rows = await db.select().from(heartbeatRunEvents).where(eq(heartbeatRunEvents.runId, run.id)).orderBy(heartbeatRunEvents.seq);
|
|
expect(deps.getCurrentUserRedactionOptions).toHaveBeenCalledTimes(2);
|
|
expect(rows).toHaveLength(2);
|
|
const published = vi.mocked(publishLiveEvent).mock.calls.map(([event]) => event).filter(event => event.type === "heartbeat.run.event");
|
|
expect(published).toHaveLength(2);
|
|
for (const row of rows) {
|
|
const text = JSON.stringify({ message: row.message, payload: row.payload });
|
|
expect(text).not.toContain("alice");
|
|
expect(text).not.toContain("bob");
|
|
expect(text).not.toContain("test-credential-material");
|
|
expect(published.find(event => event.payload?.seq === row.seq)?.payload).toMatchObject({ message: row.message, payload: row.payload });
|
|
}
|
|
});
|
|
});
|