pi-mono/packages/coding-agent/test/experimental-session-support.ts
Mario Zechner 48dd1e2f0f feat(coding-agent): port the experimental client/server to pi-durable
Session workers now host a durable Harness over a per-session sqlite storage.
The server keeps each session in <sessionDir>/<id>/ with meta.json and
session.sqlite and never opens the storage itself. Workers hold retirement
while the Harness task graph has live tasks and resume interrupted work on
open.

Transcript serves the root conversation's durable view directly, Models
follows the conversation's agent document, and AgentController maps prompt,
steer, follow-up, queue cancellation, abort, and compaction onto the root
conversation, plus waitForPrompt. navigate, nextRun, and resume are dropped
(see experimental/services/README.md TODO). The durable TUI and the worker
share harness-setup.ts.

pi-server exports its own SessionMetadata and no longer depends on
pi-agent-core. Chord accepts readonly JSON types in service contracts without
walking them, which recursive JSON types like pi-ai's made too deep.
2026-10-01 14:45:01 +02:00

44 lines
1.7 KiB
TypeScript

import { mkdir, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { BACKGROUND_CONTEXT } from "@earendil-works/chord/context";
import { AgentDoc, createSession, type ModelRef, ROOT_CONVERSATION_ID } from "@earendil-works/pi-durable";
import { openNodeSqliteStorage } from "@earendil-works/pi-durable/storage/sqlite/node";
import {
createSession as createCatalogSession,
readSession,
type SessionCatalogMetadata,
sessionStoragePath,
} from "../src/experimental/session-catalog.ts";
export async function createExperimentalSessions(
sessionsRoot: string,
ids: readonly string[],
cwd = process.cwd(),
): Promise<SessionCatalogMetadata[]> {
const metadata: SessionCatalogMetadata[] = [];
for (const id of ids) metadata.push(await createCatalogSession(sessionsRoot, { id, cwd }));
return metadata;
}
export async function configureExperimentalWorkerModel(agentDir: string): Promise<void> {
await mkdir(agentDir, { recursive: true });
await writeFile(join(agentDir, "auth.json"), JSON.stringify({ anthropic: { type: "api_key", key: "test-key" } }), {
mode: 0o600,
});
}
/** The root conversation's model, read from the Session's durable storage while no worker owns it. */
export async function readExperimentalSessionState(
sessionsRoot: string,
sessionId: string,
): Promise<{ model: ModelRef | undefined }> {
const metadata = await readSession(sessionsRoot, sessionId);
if (metadata === undefined) throw new Error(`Expected Session ${sessionId}`);
const session = createSession(await openNodeSqliteStorage(sessionStoragePath(metadata)));
try {
const agent = await session.snapshot(AgentDoc, ROOT_CONVERSATION_ID, BACKGROUND_CONTEXT);
return { model: agent?.model };
} finally {
await session.close(BACKGROUND_CONTEXT);
}
}