Q60: step limits end sessions as checkpoints; only sessions without progress spend the budget

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ZacharyZhang-NYandClaude Opus 5.5 committed 2026-10-08 10:33:57 -04:00
1 parent 8775c34650
commit 88463e4396
6 files changed
+166 -35

No files matched your search

+67
View File
@@ -0,0 +1,67 @@
import { randomUUID } from "node:crypto";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { describe, expect, test } from "vitest";
import { DatabaseConnection } from "../../src/main/database/connection";
import { OnboardingService } from "../../src/main/services/onboarding-service";
import { ProviderService } from "../../src/main/services/provider-service";
import { providerAdapters } from "../../src/main/services/provider-adapters";
import { TaskExecutionService } from "../../src/main/services/task-execution-service";
import { ChatService } from "../../src/main/services/chat-service";
import { ensureCeoDirectSpace } from "../../src/main/services/ceo-direct-space";
import { claimNextQueuedRun } from "../../src/main/app/task-queue-worker";
import type { AgentTurnResult } from "../../src/main/services/provider-tool-turn";
const testVault = {
encryptSecret: (secret: string) => Buffer.from(secret, "utf8"),
decryptSecret: (ciphertext: Buffer) => ciphertext.toString("utf8"),
};
describe("session checkpoints", () => {
test("a chat reply still working at the step limit stays open and posts no stand-in reply", async () => {
let finishing = false;
const turn = async (): Promise<AgentTurnResult> => finishing
? { text: "Audit done: all clean.", toolCalls: [], rawResponseJson: "{}" }
: { text: "Looking into it.", toolCalls: [{ id: `c_${randomUUID()}`, name: "read_messages", arguments: {} }], rawResponseJson: "{}" };
const connection = new DatabaseConnection(join(tmpdir(), `ac-checkpoint-${randomUUID()}.db`));
const providers = new ProviderService(connection.db, providerAdapters, testVault, undefined, turn);
const onboarding = new OnboardingService({ db: connection.db, saveProviderCredential: (input) => providers.saveCredential(input) });
const execution = new TaskExecutionService(connection.db, providers);
let orgId = "";
let ceoId = "";
connection.transaction(() => {
const summary = onboarding.bootstrap({
companyName: "FlowPilot", companyDescription: "CSM analytics.", employeeName: "Yang", workspacePath: "",
executionMode: "ai_ceo", autoApproveHires: true, reviewDeliverables: false,
goalTitle: "Ship the MVP.", goalDescription: "A working product.",
providerKey: "openai", providerSecret: "sk-test-key", providerModel: "gpt-5.4",
});
orgId = summary.orgId;
ceoId = summary.ceoAgentId;
});
connection.db.prepare("update task_runs set state = 'completed' where state = 'queued'").run();
const founder = (connection.db.prepare("select id from employees where org_id = ?").get(orgId) as { id: string }).id;
new ChatService(connection.db).sendMessage({
orgId, spaceId: ensureCeoDirectSpace(connection.db, orgId)!, threadId: null,
senderType: "employee", senderId: founder, bodyMd: "Audit every repo, please.", attachmentFileIds: [],
});
const wake = claimNextQueuedRun(connection)!;
await execution.executeRun(orgId, wake.id);
const run = connection.db.prepare("select r.state, t.status from task_runs r join tasks t on t.id = r.task_id where r.id = ?").get(wake.id);
expect(run).toEqual({ state: "completed", status: "backlog" });
expect(connection.db.prepare("select count(*) as n from chat_messages where sender_type = 'agent' and sender_id = ?").get(ceoId)).toEqual({ n: 0 });
// The heartbeat continuation answers and closes the conversation's task.
finishing = true;
const wakeTask = (connection.db.prepare("select task_id from task_runs where id = ?").get(wake.id) as { task_id: string }).task_id;
connection.db.prepare("update task_runs set created_at = '2000-01-01T00:00:00.000Z' where agent_id = ?").run(ceoId);
connection.db.prepare("update tasks set status = 'done' where origin is null").run();
const queued = connection.transaction(() => execution.enqueueHeartbeatRuns());
const continuation = queued.find((id) => (connection.db.prepare("select task_id from task_runs where id = ?").get(id) as { task_id: string }).task_id === wakeTask)!;
connection.db.prepare("update task_runs set state = 'running' where id = ?").run(continuation);
await execution.executeRun(orgId, continuation);
expect(connection.db.prepare("select status from tasks where id = ?").get(wakeTask)).toEqual({ status: "done" });
expect(connection.db.prepare("select count(*) as n from chat_messages where sender_type = 'agent' and sender_id = ? and body_md = 'Audit done: all clean.'").get(ceoId)).toEqual({ n: 1 });
connection.close();
});
});
+24
View File
@@ -234,4 +234,28 @@ describe("agent run loop (real tool dispatch)", () => {
)).rejects.toThrow(/empty turn/);
connection.close();
});
test("a session still working at the step limit ends as a checkpoint instead of failing", async () => {
const connection = new DatabaseConnection(join(tmpdir(), `ac-loop4-${randomUUID()}.db`));
const organization = new OrganizationService(connection.db);
let orgId = "";
let ceoId = "";
let providerId = "";
connection.transaction(() => {
orgId = organization.createOrganization({ organizationName: "OrbitOps", employeeName: "Yang", mission: "m", goal: "g" });
providerId = (connection.db.prepare("select id from providers where org_id = ? and provider_key = 'openai'").get(orgId) as { id: string }).id;
ceoId = organization.createAgent({ orgId, name: "CEO", role: "CEO", department: "Executive", modelProviderId: providerId, modelId: "gpt-5.4", budgetCents: 25000, managerAgentId: null });
});
const ctx: AgentToolContext = {
orgId, agent: { id: ceoId, modelProviderId: providerId, modelId: "gpt-5.4" },
taskProjectId: null, taskGoalId: null, taskId: null, sourceThreadId: null, workspace: null,
};
const busy = Array.from({ length: 40 }, (_, index) => ({ text: index === 11 ? "Still building." : "", toolCalls: [call("read_messages", {})] }));
const result = await runAgentLoop(
{ providers: fakeProvider(busy, connection.db), tools: { db: connection.db, organization, chat: new ChatService(connection.db) } },
{ orgId, providerId, inputBudgetTokens: 112_000, workspaceId: null, instructions: "i", initialUserTurn: "u", toolContext: ctx },
);
expect(result).toMatchObject({ iterations: 12, toolCallCount: 12, hitStepLimit: true, finalText: "Still building." });
connection.close();
});
});
+37 -1
View File
@@ -727,7 +727,7 @@ describe("task execution service", () => {
spendRun(); spendRun(); spendRun();
expect(connection.transaction(() => execution.enqueueHeartbeatRuns())).toEqual([]);
const stalled = connection.db.prepare("select status, stalled_reason from tasks where id = ?").get(taskId) as { status: string; stalled_reason: string };
expect(stalled).toEqual({ status: "in_progress", stalled_reason: "Stalled: 3 sessions ran without reaching done. Edit, reassign or reopen the task to give it a fresh budget." });
expect(stalled).toEqual({ status: "in_progress", stalled_reason: "Stalled: 3 sessions made no progress. Edit, reassign or reopen the task to give it a fresh budget." });
expect(connection.db.prepare("select count(*) as n from audit_events where entity_id = ? and action = 'stalled'").get(taskId)).toEqual({ n: 1 });
expect(notified).toBe(1);
@@ -743,6 +743,42 @@ describe("task execution service", () => {
connection.close();
});
test("sessions that make progress do not spend the run budget, up to a ceiling of sessions", () => {
const connection = createConnection();
const organization = new OrganizationService(connection.db);
const providers = new ProviderService(connection.db, providerAdapters, testVault);
const execution = new TaskExecutionService(connection.db, providers);
let orgId = "";
let taskId = "";
connection.transaction(() => {
orgId = organization.createOrganization({ organizationName: "Acme", employeeName: "Dana Lee", mission: "m", goal: "g" });
const providerId = openAiProviderId(connection, orgId);
providers.saveCredential({ orgId, providerId, secret: "sk-live-provider", model: "gpt-5.4" });
const agentId = organization.createAgent({
orgId, name: "Ada", role: "Engineer", department: "Engineering", modelProviderId: providerId, modelId: "gpt-5.4",
budgetCents: 10000, managerAgentId: null, heartbeatEnabled: true,
});
taskId = organization.createTask({
orgId, title: "Big build", description: "d", status: TASK_STATUS.IN_PROGRESS, assigneeAgentId: agentId,
risk: RISK_LEVEL.LOW, projectId: null, goalId: null, workspaceId: null, priority: TASK_PRIORITY.NORMAL, requiresFilesystem: false,
});
});
const productiveSession = (): boolean => {
const [runId] = connection.transaction(() => execution.enqueueHeartbeatRuns());
if (!runId) return false;
connection.db.prepare("update task_runs set state = 'completed' where id = ?").run(runId);
connection.db.prepare("insert into run_actions(id, org_id, run_id, task_id, tool_name, arguments_excerpt, result_excerpt, created_at) values (?, ?, ?, ?, 'write_deliverable', '{}', 'Wrote src/app.ts', ?)")
.run(`rac_${runId}`, orgId, runId, taskId, new Date().toISOString());
return true;
};
let sessions = 0;
while (productiveSession()) sessions += 1;
expect(sessions).toBe(12);
expect(connection.db.prepare("select stalled_reason from tasks where id = ?").get(taskId))
.toEqual({ stalled_reason: "Stalled: 12 sessions ran without reaching done. Edit, reassign or reopen the task to give it a fresh budget." });
connection.close();
});
test("an open task with a completed run keeps getting sessions until done or the budget is spent", () => {
const connection = createConnection();
const organization = new OrganizationService(connection.db);