Files
yuez 32292873f9 feat(daemon): serve a live-only prompt-history read
The web composer's ↑/↓ recall needs the plugin's prompt history, so the browser
and nvim walk one list instead of two. The daemon relays it and stores nothing:
it is a router, and the plugin stays the authority (AGENTS.md rule 2).

- plugin `hello` advertises the new `promptHistory` capability; the daemon never
  relays the read to a plugin that did not advertise it
- `workspace.query{kind:"promptHistory",limit}` is answered by the plugin from
  its own `prompt.history` archive (workspace cwd), oldest-first
- `GET /api/v1/workspaces/:id/prompt-history?limit=` → `{v, workspaceId, live,
  supported, entries}`; `limit` defaults to 200 and clamps to 500, an unknown
  workspace is a 404, and malformed entries are dropped
- the read deliberately does **not** call `instances.ensure()`: recall is a
  keystroke, not a lifecycle event, so a workspace with no connected plugin
  answers `{live:false, supported:false, entries:[]}` instead of booting an nvim
- `PluginConnection.capabilities` joins the interface the daemon sees, and the
  fake connection grows a writable one so the gate is testable

No event or reducer change: `fixtures/` are untouched.
2026-11-02 11:12:21 +08:00

291 lines
9.2 KiB
TypeScript

/**
* Deterministic fakes for the daemon surface tests.
*
* W2/W3 will provide the real `InstanceManager` / `WorkspaceStore`; the daemon
* only depends on `src/core/interfaces.ts`, so these fakes mirror those shapes.
*/
import type {
Bus,
InstanceManager,
InstanceResourcesSnapshot,
PluginConnection,
WorkspaceStore,
} from "../../src/core/interfaces.js";
import type { InstanceState, RecentSessionInfo, SessionState, WorkspaceInfo } from "../../src/protocol/types.js";
export interface CommandCall {
id: string;
sessionId?: string;
method: string;
params?: Record<string, unknown>;
clientRequestId?: string;
}
let deviceCounter = 0;
export class FakeConnection implements PluginConnection {
readonly instanceId: string;
readonly workspaceId: string;
readonly cwd: string;
readonly pid = process.pid;
readonly hasUI = true;
readonly listenAddr: string | null = null;
readonly pluginVersion = "test-1.0.0";
/** Writable so a test can model a plugin that predates a capability. */
capabilities: string[] = ["promptHistory"];
subscribed = new Set<string>();
snapshots = new Map<string, SessionState>();
seqs = new Map<string, number>();
commands: CommandCall[] = [];
sendRecords: Record<string, unknown>[] = [];
closedReason: string | undefined;
commandHandler: ((call: CommandCall) => Promise<{ seq?: number; disposition?: string }>) | undefined;
private commandCounter = 0;
constructor(workspaceId: string, instanceId = `i-${++deviceCounter}`) {
this.workspaceId = workspaceId;
this.instanceId = instanceId;
this.cwd = `/repo/${workspaceId}`;
}
send(record: Record<string, unknown>): void {
this.sendRecords.push(record);
}
async command(call: CommandCall): Promise<{ seq?: number; disposition?: string }> {
this.commands.push(call);
if (this.commandHandler) return this.commandHandler(call);
return { seq: ++this.commandCounter, disposition: "started" };
}
subscribe(sessionId: string): void {
this.subscribed.add(sessionId);
}
unsubscribe(sessionId: string): void {
this.subscribed.delete(sessionId);
}
sessionIds(): string[] {
return [...this.snapshots.keys()];
}
snapshot(sessionId: string): SessionState | undefined {
return this.snapshots.get(sessionId);
}
lastSeq(sessionId: string): number {
return this.seqs.get(sessionId) ?? 0;
}
close(reason: string): void {
this.closedReason = reason;
}
}
export function makeSessionState(sessionId: string, workspaceId: string): SessionState {
return {
sessionId,
workspaceId,
cwd: `/repo/${workspaceId}`,
name: `Session ${sessionId}`,
status: "idle",
flags: { attention: false, done: true, error: false },
model: { provider: "anthropic", id: "claude-sonnet-4-5" },
thinking: "medium",
usage: { input: 1, output: 2, cacheRead: 0, cacheWrite: 0, cost: 0.01 },
contextUsage: null,
queue: [],
todos: [],
tasks: [],
batches: [],
subagents: [],
attention: [],
entries: [],
};
}
export function makeInstanceState(workspaceId: string, instanceId: string): InstanceState {
return {
workspaceId,
instanceId,
status: "ready",
nvimPid: 1234,
pluginVersion: "test-1.0.0",
hasUI: true,
attachedClients: 0,
listenAddr: null,
startedAt: Date.now(),
};
}
export class FakeInstanceManager implements InstanceManager {
readonly instances = new Map<string, FakeConnection>();
readonly states = new Map<string, InstanceState>();
readonly sessionOwners = new Map<string, string>();
ensureCalls: string[] = [];
shutdownCalls: { workspaceId: string; mode: "drain" | "stop" }[] = [];
maybeUnsubscribeCalls: { workspaceId: string; sessionId: string }[] = [];
resourcesValue: InstanceResourcesSnapshot = {
instances: [],
totalRssBytes: 0,
physicalMemoryBytes: 16 * 1024 ** 3,
warn: false,
};
private changeHandlers: ((state: InstanceState) => void)[] = [];
addWorkspace(workspaceId: string, instanceId = `i-${workspaceId}`): FakeConnection {
const conn = new FakeConnection(workspaceId, instanceId);
this.instances.set(workspaceId, conn);
this.states.set(workspaceId, makeInstanceState(workspaceId, instanceId));
return conn;
}
async ensure(workspaceId: string): Promise<InstanceState> {
this.ensureCalls.push(workspaceId);
let state = this.states.get(workspaceId);
if (!state) {
const conn = this.addWorkspace(workspaceId);
state = this.states.get(workspaceId)!;
for (const handler of this.changeHandlers) handler(state);
void conn;
}
return state;
}
async shutdown(workspaceId: string, mode: "drain" | "stop"): Promise<void> {
this.shutdownCalls.push({ workspaceId, mode });
this.instances.delete(workspaceId);
this.states.delete(workspaceId);
}
get(workspaceId: string): PluginConnection | undefined {
return this.instances.get(workspaceId);
}
list(): InstanceState[] {
return [...this.states.values()];
}
instanceStates(): InstanceState[] {
return this.list();
}
findWorkspaceBySession(sessionId: string): string | undefined {
for (const conn of this.instances.values()) {
if (conn.subscribed.has(sessionId)) return conn.workspaceId;
}
return this.sessionOwners.get(sessionId);
}
async resources(): Promise<InstanceResourcesSnapshot> {
return this.resourcesValue;
}
maybeUnsubscribe(workspaceId: string, sessionId: string): void {
this.maybeUnsubscribeCalls.push({ workspaceId, sessionId });
}
onInstanceChange(handler: (state: InstanceState) => void): void {
this.changeHandlers.push(handler);
}
}
export function makeWorkspaceInfo(workspaceId: string, overrides: Partial<WorkspaceInfo> = {}): WorkspaceInfo {
return {
workspaceId,
cwd: `/repo/${workspaceId}`,
name: `Workspace ${workspaceId}`,
trusted: true,
exists: true,
sessionCount: 0,
liveCount: 0,
lastActiveAt: null,
instance: null,
...overrides,
};
}
export class FakeWorkspaceStore implements WorkspaceStore {
readonly workspaces = new Map<string, WorkspaceInfo>();
addCalls: { cwd: string; alias?: string; trust?: boolean }[] = [];
updateCalls: { workspaceId: string; patch: Record<string, unknown> }[] = [];
removeCalls: string[] = [];
refreshCalls: (string | undefined)[] = [];
/** Seeded answer for `recentSessions()`; the fake never touches disk. */
recentSessionRows: RecentSessionInfo[] = [];
constructor(seed: WorkspaceInfo[] = []) {
for (const workspace of seed) this.workspaces.set(workspace.workspaceId, workspace);
}
list(): WorkspaceInfo[] {
return [...this.workspaces.values()];
}
get(workspaceId: string): WorkspaceInfo | undefined {
return this.workspaces.get(workspaceId);
}
byCwd(cwd: string): WorkspaceInfo | undefined {
return this.list().find((workspace) => workspace.cwd === cwd);
}
async add(input: { cwd: string; alias?: string; trust?: boolean }): Promise<WorkspaceInfo> {
this.addCalls.push(input);
const workspace = makeWorkspaceInfo(`ws-${this.workspaces.size + 1}`, {
cwd: input.cwd,
alias: input.alias ?? null,
trusted: input.trust ?? false,
});
this.workspaces.set(workspace.workspaceId, workspace);
return workspace;
}
async update(
workspaceId: string,
patch: Partial<{ alias: string | null; pinned: boolean; hidden: boolean; trusted: boolean }>,
): Promise<WorkspaceInfo> {
this.updateCalls.push({ workspaceId, patch });
const current = this.workspaces.get(workspaceId) ?? makeWorkspaceInfo(workspaceId);
const next = { ...current, ...patch };
this.workspaces.set(workspaceId, next);
return next;
}
async remove(workspaceId: string): Promise<void> {
this.removeCalls.push(workspaceId);
this.workspaces.delete(workspaceId);
}
async refresh(workspaceId?: string): Promise<void> {
this.refreshCalls.push(workspaceId);
}
async ensureForCwd(cwd: string): Promise<WorkspaceInfo> {
return this.byCwd(cwd) ?? this.add({ cwd });
}
recentSessions(limit = 20): RecentSessionInfo[] {
return this.recentSessionRows.slice(0, limit);
}
}
export class FakeBus implements Bus {
readonly published: Record<string, unknown>[] = [];
readonly handlers = new Set<(record: Record<string, unknown>) => void>();
publish(record: Record<string, unknown>): void {
this.published.push(record);
for (const handler of this.handlers) handler(record);
}
subscribe(handler: (record: Record<string, unknown>) => void): () => void {
this.handlers.add(handler);
return () => {
this.handlers.delete(handler);
};
}
}