mirror of
https://github.com/agentclientprotocol/claude-agent-acp.git
synced 2026-11-24 02:02:23 +00:00
Implements the unstable [Session status RFD](https://agentclientprotocol.com/rfds/session-status) on both agents, and advertises `status: {}` in the v1 `sessionCapabilities` and the v2 `capabilities.session`. ## What it answers | Session | v1 | v2 | | --- | --- | --- | | Held by this connection | `open` | `open` with `state` | | Not held, non-empty transcript stored | `saved` | `saved` | | Not held, no transcript | `-32002` with `data.sessionId` | same | | Subagent session | `-32002` | `-32602`, as resume, close, fork and delete of one | - The request starts no query, sends no update, and waits for no resume, restart or close. - On v2, `state` is the foreground state as a `state_update` snapshot holds it: with the session's `background` list and without a stop reason. `V2Background.snapshot` builds it without marking the list as reported, so a status request does not change which later `state_update`s go out. - `saved` or unknown is decided in the new shared `src/session-status.ts`: a non-empty `<sessionId>.jsonl` in any project directory. The request names no `cwd`, so a resume with another `cwd` can still fail with `-32002`. A directory that cannot be read for another reason than being absent fails the request and does not answer `-32002`. ## Behaviour to review - **A v1 session whose query ended reads `open`.** The connection still holds it, and a `session/load` with the same params returns it as it is, so `saved` would point the client at a resume that revives nothing. Its prompts fail with the "session ended" error. v2 drops such a session and answers from its transcript. - **A session that was never prompted is unknown once it is closed.** Claude Code writes the transcript at the first prompt, and a resume of it fails with `-32002` too. - **A subagent session does not advertise `status`,** so v2 refuses the request like its other requests, and v1 does not know the id. - **`open` is per connection.** Each connection has its own agent and its own Claude Code processes. ## Not done - The lookup does one `readdir` and one `stat` per project directory for each probe of a session that is not held. Sharing the v1 session index's cached scan is a follow-up. - Not run against the real CLI (`npm run test:integration`). ## Other changes - `isSessionId` and `projectsRoot` moved from `src/v1/session-index/project-dirs.ts` to `src/paths.ts`, so the shared module can use them. - The 40 v2 and 2 AIR scenario golden files gain the capability in `initialize`, and the origin/main comparison allows it, next to `systemPrompt` and `auth.status`. - The README lists `session/status` in the v2 surface.
69 lines
2.5 KiB
TypeScript
69 lines
2.5 KiB
TypeScript
/**
|
|
* `session/status` (the unstable "Session status" RFD) of a session that the
|
|
* connection does not hold: `saved` or unknown. Each core answers `open`
|
|
* itself for the sessions it holds.
|
|
*/
|
|
import * as fs from "node:fs/promises";
|
|
import * as path from "node:path";
|
|
import { RequestError } from "@agentclientprotocol/sdk/unstable";
|
|
import { isSessionId, projectsRoot } from "./paths.js";
|
|
|
|
/** What a core advertises as `status` in its session capabilities. */
|
|
export const SESSION_STATUS_CAPABILITY = {};
|
|
|
|
/**
|
|
* `saved` when a project directory holds a transcript file of `sessionId`
|
|
* that is not empty. Its content is not read, so a transcript without a
|
|
* conversation is `saved` too, and its resume fails with `-32002`. Fails with
|
|
* `-32002` and `data.sessionId` otherwise:
|
|
*
|
|
* - an id that is not a UUID, such as a subagent's session id;
|
|
* - an id without a transcript: one never used, a session never prompted
|
|
* (Claude Code writes the transcript at the first prompt), and a session
|
|
* whose transcripts were removed.
|
|
*
|
|
* The request names no `cwd`, so a transcript of any project counts: a
|
|
* resume with another `cwd` can still fail with `-32002`.
|
|
*
|
|
* It lists the project directories and stats the one file name in each. No
|
|
* query starts and no session is loaded. A directory or file that cannot be
|
|
* read for another reason than being absent fails the request: it does not
|
|
* make the session unknown.
|
|
*/
|
|
export async function savedSessionStatus(sessionId: string): Promise<{ status: "saved" }> {
|
|
if (!isSessionId(sessionId) || !(await hasTranscript(sessionId))) {
|
|
throw RequestError.sessionNotFound(sessionId);
|
|
}
|
|
return { status: "saved" };
|
|
}
|
|
|
|
async function hasTranscript(sessionId: string): Promise<boolean> {
|
|
const root = projectsRoot();
|
|
let projects: string[];
|
|
try {
|
|
projects = await fs.readdir(root);
|
|
} catch (error) {
|
|
// No session was ever stored.
|
|
if (isAbsent(error)) return false;
|
|
throw error;
|
|
}
|
|
const found = await Promise.all(
|
|
projects.map((project) =>
|
|
fs.stat(path.join(root, project, `${sessionId}.jsonl`)).then(
|
|
(stats) => stats.isFile() && stats.size > 0,
|
|
(error: unknown) => {
|
|
// No such transcript, or `project` is a file.
|
|
if (isAbsent(error)) return false;
|
|
throw error;
|
|
},
|
|
),
|
|
),
|
|
);
|
|
return found.includes(true);
|
|
}
|
|
|
|
function isAbsent(error: unknown): boolean {
|
|
const code = (error as { code?: unknown }).code;
|
|
return code === "ENOENT" || code === "ENOTDIR";
|
|
}
|