Files
AgentCompany/tests/integration/autonomous-chain/crash-recovery.spec.ts
T
ZacharyZhang-NY 14dc9a4897 refactor(cost): single writer for cost_entries, finishRun reads SUM
Cost was written through three independent paths: worker-runner shipped
`finalUsage.costUsd` in run-finished -> finishRun wrote `runs.cost_usd`,
`tasks.cost_usd += costUsd`, `agents.spent_monthly_usd += costUsd`, and
inserted a per-run aggregate row in cost_entries. CostWriter
additionally inserted per-message rows in cost_entries from main. Any
CostWriter failure created silent drift between `SUM(cost_entries)`
(read by budget enforcement) and `runs.cost_usd` (read by the UI).

Make CostWriter the single writer, finishRun the single reader.

- Add `db.recordCostEntry(...)` as the first-class insert path for
  per-message cost rows. CostWriter calls it; tests call it directly.
- Remove the aggregate INSERT from finishRun. finishRun now reads
  `SELECT COALESCE(SUM(amount_usd), 0) FROM cost_entries WHERE run_id=?
  AND unattributed=0` and writes that as the authoritative run total to
  runs.cost_usd, tasks.cost_usd, and agents.spent_monthly_usd.
- Drop `costUsd` from `finishRun` input, the worker `run-finished`
  ChildMessage shape, the automation `RunFinishedEvent` type, and every
  caller + test fixture. Cost is owned by the per-message path.
- worker-runner stops extracting `usage.cost.total` — token counts stay
  for session accumulation, cost comes from pi-ai via CostWriter.
- run-finished-helpers now gates budget incident checks on
  `event.status === "succeeded"` instead of a cost field that no longer
  exists on the event.
- automation-run-completion fires `budget_threshold` on every succeeded
  run (post-run spent_monthly_usd already reflects the SUM path).
- Update budget tests to write a `recordCostEntry` before `finishRun`
  to simulate the per-message cost path; drop `costUsd` from cross-
  subsystem, edge-case-budget, database, and related fixtures.

One writer, one reader, one source of truth. No drift.
2026-04-17 15:04:22 -04:00

93 lines
2.9 KiB
TypeScript

/**
* Step 7: Worker crash recovery (database layer).
* Physically extracted from autonomous-chain.spec.ts without behavior changes.
*/
import { describe, expect, it } from "vitest";
import { AppDatabase } from "../../../src/main/database";
import { installSharedSetup } from "./shared-setup";
const shared = installSharedSetup();
const { createAgent } = shared;
describe("Step 7: Worker crash recovery (database layer)", () => {
it("interruptStaleRuns clears running and queued runs on init", async () => {
const agentId = createAgent({ name: "Agent" });
const taskId = shared.db.saveTask({
companyId: shared.companyId,
title: "In-progress task",
description: "Running when crash happened",
assigneeAgentId: agentId,
workspaceId: shared.workspaceId,
priority: "medium",
status: "in_progress",
});
const run = shared.db.createRun({
companyId: shared.companyId,
taskId,
agentId,
workspaceId: shared.workspaceId,
connectorId: "openai-codex",
});
shared.db.markRunStatus(run.id, "running", "Started");
// Verify it's running
let snapshot = shared.db.listSnapshot();
expect(snapshot.runs.find((r) => r.id === run.id)?.status).toBe("running");
// Simulate app restart — close and re-init
shared.db.close();
const profileDir = shared.tempDirs[shared.tempDirs.length - 1]!;
shared.db = new AppDatabase(profileDir);
shared.db.init(); // This calls interruptStaleRuns()
snapshot = shared.db.listSnapshot();
const recoveredRun = snapshot.runs.find((r) => r.id === run.id);
expect(recoveredRun?.status).toBe("interrupted");
});
it("finishRun can mark interrupted runs from crash recovery", () => {
const agentId = createAgent({ name: "Agent" });
const taskId = shared.db.saveTask({
companyId: shared.companyId,
title: "Crash recovery task",
description: "Test",
assigneeAgentId: agentId,
workspaceId: shared.workspaceId,
priority: "medium",
status: "in_progress",
});
const run = shared.db.createRun({
companyId: shared.companyId,
taskId,
agentId,
workspaceId: shared.workspaceId,
connectorId: "openai-codex",
});
shared.db.markRunStatus(run.id, "running", "Started");
// Simulate crash recovery - mark as interrupted
shared.db.finishRun({
runId: run.id,
status: "interrupted",
summary: "Worker process crashed.",
errorMessage: "Worker process exited unexpectedly.",
exitCode: 1,
signal: null,
model: null,
sessionDisplayId: null,
unattributedCost: true,
});
const snapshot = shared.db.listSnapshot();
const recovered = snapshot.runs.find((r) => r.id === run.id);
expect(recovered?.status).toBe("interrupted");
expect(recovered?.errorMessage).toBe("Worker process exited unexpectedly.");
});
});