|
1 | 1 | import test from "node:test"; |
2 | 2 | import assert from "node:assert/strict"; |
3 | 3 | import { spawn } from "node:child_process"; |
| 4 | +import { randomUUID } from "node:crypto"; |
4 | 5 | import { once } from "node:events"; |
| 6 | +import { mkdtempSync, readdirSync, rmSync } from "node:fs"; |
| 7 | +import { tmpdir } from "node:os"; |
| 8 | +import { join, resolve as resolvePath } from "node:path"; |
5 | 9 | import { Readable, Writable } from "node:stream"; |
6 | | -import { resolve as resolvePath } from "node:path"; |
7 | 10 | import { |
8 | 11 | ClientSideConnection, |
9 | 12 | PROTOCOL_VERSION, |
@@ -73,6 +76,39 @@ async function withAdapter(run, extraEnv = {}) { |
73 | 76 | } |
74 | 77 | } |
75 | 78 |
|
| 79 | +/** Recursively collect every `<sessionId>.jsonl` Claude Code wrote under `dir`. */ |
| 80 | +function findPersistedSessionFiles(dir, sessionId) { |
| 81 | + let entries; |
| 82 | + try { |
| 83 | + entries = readdirSync(dir, { withFileTypes: true }); |
| 84 | + } catch (error) { |
| 85 | + if (error.code === "ENOENT") return []; |
| 86 | + throw error; |
| 87 | + } |
| 88 | + return entries.flatMap((entry) => { |
| 89 | + const path = join(dir, entry.name); |
| 90 | + if (entry.isDirectory()) return findPersistedSessionFiles(path, sessionId); |
| 91 | + return entry.name === `${sessionId}.jsonl` ? [path] : []; |
| 92 | + }); |
| 93 | +} |
| 94 | + |
| 95 | +/** Flatten an LLMock-normalized chat message into its text fragments. */ |
| 96 | +function messageTexts(message) { |
| 97 | + if (typeof message.content === "string") return [message.content]; |
| 98 | + if (!Array.isArray(message.content)) return []; |
| 99 | + return message.content.flatMap((part) => |
| 100 | + typeof part?.text === "string" ? [part.text] : [], |
| 101 | + ); |
| 102 | +} |
| 103 | + |
| 104 | +async function initialize(connection) { |
| 105 | + return await connection.initialize({ |
| 106 | + protocolVersion: PROTOCOL_VERSION, |
| 107 | + clientCapabilities: {}, |
| 108 | + clientInfo: { name: "agentos-test", version: "0.0.1" }, |
| 109 | + }); |
| 110 | +} |
| 111 | + |
76 | 112 | test("published Claude Agent ACP command initializes over stdio", async () => { |
77 | 113 | await withAdapter(async (connection) => { |
78 | 114 | const result = await connection.initialize({ |
@@ -239,3 +275,141 @@ test("published Claude Agent ACP prompts a second process while the first remain |
239 | 275 | await mock.stop(); |
240 | 276 | } |
241 | 277 | }); |
| 278 | + |
| 279 | +test("published Claude Agent ACP resumes a persisted session natively after its adapter process restarts", async () => { |
| 280 | + // The packaged agent keeps Claude Code's session directory on durable |
| 281 | + // storage (`CLAUDE_CONFIG_DIR`, `/home/agentos/.claude` by default). agentOS |
| 282 | + // restores a session after a VM restart with ACP `session/resume`, which the |
| 283 | + // adapter maps onto the SDK `resume` option: Claude Code reloads its own |
| 284 | + // persisted session file, so the resumed turn carries the prior context |
| 285 | + // without any transcript being re-sent by agentOS. |
| 286 | + const configDir = mkdtempSync(join(tmpdir(), "agentos-claude-config-")); |
| 287 | + const mock = new LLMock({ port: 0, logLevel: "silent" }); |
| 288 | + mock.addFixtures([ |
| 289 | + { |
| 290 | + match: { userMessage: "Reply with resume-first" }, |
| 291 | + response: { content: "resume-first" }, |
| 292 | + }, |
| 293 | + { |
| 294 | + match: { userMessage: "Reply with resume-second" }, |
| 295 | + response: { content: "resume-second" }, |
| 296 | + }, |
| 297 | + ]); |
| 298 | + const baseUrl = await mock.start(); |
| 299 | + const env = { ANTHROPIC_BASE_URL: baseUrl, CLAUDE_CONFIG_DIR: configDir }; |
| 300 | + try { |
| 301 | + const sessionId = await withAdapter(async (connection) => { |
| 302 | + await initialize(connection); |
| 303 | + const session = await connection.newSession({ |
| 304 | + cwd: packageDir, |
| 305 | + mcpServers: [], |
| 306 | + }); |
| 307 | + const first = await connection.prompt({ |
| 308 | + sessionId: session.sessionId, |
| 309 | + prompt: [{ type: "text", text: "Reply with resume-first" }], |
| 310 | + }); |
| 311 | + assert.equal(first.stopReason, "end_turn"); |
| 312 | + return session.sessionId; |
| 313 | + }, env); |
| 314 | + |
| 315 | + // Session persistence is on by default: Claude Code wrote the session |
| 316 | + // file under CLAUDE_CONFIG_DIR before its adapter process exited. |
| 317 | + const persisted = findPersistedSessionFiles( |
| 318 | + join(configDir, "projects"), |
| 319 | + sessionId, |
| 320 | + ); |
| 321 | + assert.equal( |
| 322 | + persisted.length, |
| 323 | + 1, |
| 324 | + `expected one persisted session file for ${sessionId} under ${configDir}`, |
| 325 | + ); |
| 326 | + |
| 327 | + const requestsBeforeResume = mock.getRequests().length; |
| 328 | + await withAdapter(async (connection) => { |
| 329 | + await initialize(connection); |
| 330 | + const resumed = await connection.resumeSession({ |
| 331 | + sessionId, |
| 332 | + cwd: packageDir, |
| 333 | + mcpServers: [], |
| 334 | + }); |
| 335 | + assert.ok( |
| 336 | + resumed.configOptions?.some((option) => option.id === "model"), |
| 337 | + "resumed session must expose the model config option", |
| 338 | + ); |
| 339 | + assert.ok( |
| 340 | + resumed.configOptions?.some((option) => option.id === "effort"), |
| 341 | + "resumed session must expose the agentOS effort config option", |
| 342 | + ); |
| 343 | + const second = await connection.prompt({ |
| 344 | + sessionId, |
| 345 | + prompt: [{ type: "text", text: "Reply with resume-second" }], |
| 346 | + }); |
| 347 | + assert.equal(second.stopReason, "end_turn"); |
| 348 | + }, env); |
| 349 | + |
| 350 | + const resumedTurn = mock |
| 351 | + .getRequests() |
| 352 | + .slice(requestsBeforeResume) |
| 353 | + .map((entry) => entry.body?.messages ?? []) |
| 354 | + .find((messages) => |
| 355 | + messages.some( |
| 356 | + (message) => |
| 357 | + message.role === "user" && |
| 358 | + messageTexts(message).some((text) => |
| 359 | + text.includes("Reply with resume-second"), |
| 360 | + ), |
| 361 | + ), |
| 362 | + ); |
| 363 | + assert.ok(resumedTurn, "the resumed prompt must reach the model"); |
| 364 | + const priorUser = resumedTurn.filter( |
| 365 | + (message) => |
| 366 | + message.role === "user" && |
| 367 | + messageTexts(message).some((text) => |
| 368 | + text.includes("Reply with resume-first"), |
| 369 | + ), |
| 370 | + ); |
| 371 | + const priorAssistant = resumedTurn.filter( |
| 372 | + (message) => |
| 373 | + message.role === "assistant" && |
| 374 | + messageTexts(message).some((text) => text.includes("resume-first")), |
| 375 | + ); |
| 376 | + assert.equal( |
| 377 | + priorUser.length, |
| 378 | + 1, |
| 379 | + "native resume must restore the prior user turn from the persisted session file", |
| 380 | + ); |
| 381 | + assert.equal( |
| 382 | + priorAssistant.length, |
| 383 | + 1, |
| 384 | + "native resume must restore the prior assistant turn from the persisted session file", |
| 385 | + ); |
| 386 | + } finally { |
| 387 | + await mock.stop(); |
| 388 | + rmSync(configDir, { recursive: true, force: true }); |
| 389 | + } |
| 390 | +}); |
| 391 | + |
| 392 | +test("published Claude Agent ACP reports a session missing from CLAUDE_CONFIG_DIR as resource not found", async () => { |
| 393 | + // A session directory that did not survive the restart must surface the ACP |
| 394 | + // resource-not-found error so agentOS can take its documented fallback path |
| 395 | + // instead of silently starting an unrelated session. |
| 396 | + const configDir = mkdtempSync(join(tmpdir(), "agentos-claude-config-")); |
| 397 | + try { |
| 398 | + await withAdapter(async (connection) => { |
| 399 | + await initialize(connection); |
| 400 | + await assert.rejects( |
| 401 | + connection.resumeSession({ |
| 402 | + sessionId: randomUUID(), |
| 403 | + cwd: packageDir, |
| 404 | + mcpServers: [], |
| 405 | + }), |
| 406 | + (error) => { |
| 407 | + assert.equal(error.code, -32002, `unexpected error: ${error.message}`); |
| 408 | + return true; |
| 409 | + }, |
| 410 | + ); |
| 411 | + }, { CLAUDE_CONFIG_DIR: configDir }); |
| 412 | + } finally { |
| 413 | + rmSync(configDir, { recursive: true, force: true }); |
| 414 | + } |
| 415 | +}); |
0 commit comments