diff --git a/.github/workflows/native-hosts.yml b/.github/workflows/native-hosts.yml index 7ca402c..fde044e 100644 --- a/.github/workflows/native-hosts.yml +++ b/.github/workflows/native-hosts.yml @@ -95,7 +95,9 @@ jobs: if: always() with: name: host-plugin-macos-results - path: ${{ runner.temp }}/crosshead-plugin-smoke/ + path: | + ${{ runner.temp }}/crosshead-plugin-smoke/host-plugins-result.json + ${{ runner.temp }}/crosshead-plugin-smoke/logs/ include-hidden-files: true if-no-files-found: ignore retention-days: 7 diff --git a/package-lock.json b/package-lock.json index daffce2..5bb1469 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@crosshead/runtime", - "version": "0.5.0-alpha.12", + "version": "0.5.0-alpha.13", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@crosshead/runtime", - "version": "0.5.0-alpha.12", + "version": "0.5.0-alpha.13", "license": "Apache-2.0", "dependencies": { "@noble/curves": "^1.9.7", diff --git a/package.json b/package.json index fb4629d..2cc8b61 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@crosshead/runtime", - "version": "0.5.0-alpha.12", + "version": "0.5.0-alpha.13", "description": "Crosshead local runtime: persistent memory for Claude Code, Codex, Pi, Hermes and OpenCode. CLI, daemon, host adapters and hooks.", "license": "Apache-2.0", "private": true, diff --git a/src/runtime/adapter-utils.ts b/src/runtime/adapter-utils.ts index 975e171..1e9829e 100644 --- a/src/runtime/adapter-utils.ts +++ b/src/runtime/adapter-utils.ts @@ -32,16 +32,23 @@ export const summarizeHotMemory = (value: unknown): string | null => { } try { const parsed = JSON.parse(value) as Record; + const checkpoints = Array.isArray(parsed.continuityCheckpoints) + ? parsed.continuityCheckpoints.filter((entry): entry is Record => Boolean(entry) && typeof entry === "object" && !Array.isArray(entry)) + : null; + const latestCheckpoint = checkpoints?.sort((a, b) => + (Date.parse(String(b.occurredAt)) || 0) - (Date.parse(String(a.occurredAt)) || 0))[0]; const fields = [ sanitizeString(parsed.currentGoal), parsed.latestTurn && typeof parsed.latestTurn === "object" && !Array.isArray(parsed.latestTurn) ? sanitizeString((parsed.latestTurn as Record).summary) : null, sanitizeString(parsed.summary), + sanitizeString(latestCheckpoint?.summary) ?? sanitizeString(latestCheckpoint?.goal), ].filter((entry): entry is string => entry !== null); if (fields.length > 0) { return fields.join(" | ").slice(0, 280); } + if (checkpoints) return null; } catch { // Raw string hot-memory content is still usable. } @@ -49,6 +56,19 @@ export const summarizeHotMemory = (value: unknown): string | null => { return normalized.length <= 280 ? normalized : `${normalized.slice(0, 277).trimEnd()}...`; }; +export const summarizeHotMemorySnapshot = (snapshot: Record | null, preferredRuntime: string): string | null => { + if (!snapshot) return null; + const entries = (Array.isArray(snapshot.scopes) ? snapshot.scopes : [snapshot]) + .filter((entry): entry is Record => Boolean(entry) && typeof entry === "object" && !Array.isArray(entry)); + const preferred = (entry: Record): boolean => + entry.scope === preferredRuntime || String(entry.scope).startsWith(`ws-${preferredRuntime}-`); + for (const entry of [...entries.filter(preferred), ...entries.filter((candidate) => !preferred(candidate))]) { + const summary = summarizeHotMemory(entry.content); + if (summary) return summary; + } + return null; +}; + export const summarizeJudgmentEntries = (value: unknown): string[] => Array.isArray(value) ? value diff --git a/src/runtime/claude-code-adapter.ts b/src/runtime/claude-code-adapter.ts index d549c7a..cf97e94 100644 --- a/src/runtime/claude-code-adapter.ts +++ b/src/runtime/claude-code-adapter.ts @@ -42,11 +42,12 @@ import { parseArgs, sanitizeString, shellQuote, - summarizeHotMemory, + summarizeHotMemorySnapshot, summarizeJudgmentEntries, writeManagedFile, } from "./adapter-utils"; import { + filterHotMemorySnapshotForWorkspace, HostRuntimeAdapterBase, type HostAdapterCompactEventRequest, type HostAdapterIdentity, @@ -959,28 +960,31 @@ export class ClaudeCodeRuntimeAdapter extends HostRuntimeAdapterBase { async createHandoff(input: ClaudeCodeAdapterHandoffCreateRequest): Promise { const profileName = input.profileName ?? this.config.profileName; + const workspaceId = input.workspaceId ?? `repo:${path.resolve(input.workspaceRoot)}`; const needsHotSummary = !input.memory?.hotSummary; - const needsWarmIds = !input.memory?.warmIds || input.memory.warmIds.length === 0; - const needsJudgment = + // Hosted warm and judgment views are profile-wide and have no trustworthy project filter. + const needsWarmIds = !this.config.workspaceOnly && (!input.memory?.warmIds || input.memory.warmIds.length === 0); + const needsJudgment = !this.config.workspaceOnly && ( !input.memory?.selfModelIds || input.memory.selfModelIds.length === 0 || !input.reasoning?.currentTheory || !input.reasoning?.openQuestions || input.reasoning.openQuestions.length === 0 || !input.reasoning?.rejectedPaths || - !input.reasoning.unresolvedRisks; + !input.reasoning.unresolvedRisks); const needsIdentity = !input.identity?.npub; const needsPolicyRef = !input.policy?.policyRef; const targetRuntime = input.targetRuntime ?? "codex"; const targetLabel = targetRuntime === "codex" ? "Codex" : "Claude Code"; const warnings: string[] = []; - const [hot, warm, judgment, status] = await Promise.all([ - needsHotSummary ? this.client.readHotMemory(profileName) : Promise.resolve(null), + const [rawHot, warm, judgment, status] = await Promise.all([ + needsHotSummary ? this.client.readHotMemory(profileName, "all") : Promise.resolve(null), optionalHostedRead(needsWarmIds, () => this.client.listWarmMemory(profileName, { status: "active" }), "Warm memory", warnings), optionalHostedRead(needsJudgment, () => this.client.inspectContinuityJudgment(profileName), "Continuity judgment", warnings), needsIdentity || needsPolicyRef ? this.client.status(profileName) : Promise.resolve(null), ]); + const hot = filterHotMemorySnapshotForWorkspace(rawHot, workspaceId, this.config.workspaceOnly, this.config.legacyUnscoped, this.config.legacyBefore); const snapshot = judgment?.snapshot && typeof judgment.snapshot === "object" && !Array.isArray(judgment.snapshot) @@ -1044,7 +1048,7 @@ export class ClaudeCodeRuntimeAdapter extends HostRuntimeAdapterBase { nextAction: input.reasoning?.nextAction ?? null, }, memory: { - hotSummary: input.memory?.hotSummary ?? summarizeHotMemory(hot?.content), + hotSummary: input.memory?.hotSummary ?? summarizeHotMemorySnapshot(hot, this.config.workspaceOnly ? "claude-code" : "shared"), warmIds: input.memory?.warmIds ?? activeWarmIds, coldChunkIds: input.memory?.coldChunkIds ?? [], projectModelIds: input.memory?.projectModelIds ?? [], @@ -1085,6 +1089,7 @@ export class ClaudeCodeRuntimeAdapter extends HostRuntimeAdapterBase { const plainTextSummary = buildPlainTextContinuationSummary(translation, targetRuntime); const restatementComparison = compareContinuityRestatement(materialization.startupContext, translation, targetRuntime); const created = (await this.client.createHandoff({ + profileName, translation, maxBytes: input.maxBytes, })) as { handoff?: HandoffStoredRecord }; diff --git a/src/runtime/cli/handoff.ts b/src/runtime/cli/handoff.ts index 35fa9c9..2e8a492 100644 --- a/src/runtime/cli/handoff.ts +++ b/src/runtime/cli/handoff.ts @@ -1,8 +1,93 @@ import path from "path"; import { OnlyBotsLocalRuntimeDaemon, formatHandoffExportResult, formatHandoffImportResult, formatHandoffInspection, formatHandoffOutcomeResult, formatHandoffVerification, parseArgs, parseJsonFlag } from "../local-runtime"; -import { chmodSync, existsSync, readFileSync, writeFileSync } from "fs"; +import { chmodSync, closeSync, constants, existsSync, fstatSync, linkSync, openSync, readSync, renameSync, unlinkSync, writeFileSync } from "fs"; +import { randomUUID } from "crypto"; import { parseAllowedSigners } from "../handoff-import"; +import { HANDOFF_CAPSULE_DEFAULT_MAX_BYTES } from "../handoff-capsules"; import { workspaceProjectRoot } from "../workspace-identity"; +import { resolveRuntimePaths } from "../runtime-persistence"; +import { readWorkspaceHookPolicy, workspaceHookForInvocation } from "../workspace-hooks"; + +type HandoffHost = "codex" | "claude-code"; + +// Pretty-printed JSON is larger than the signed capsule's 16 KiB canonical budget. +const HANDOFF_CAPSULE_FILE_MAX_BYTES = HANDOFF_CAPSULE_DEFAULT_MAX_BYTES * 8; +const HANDOFF_CREATE_INPUT_MAX_BYTES = 1024 * 1024; + +const readJsonFileBounded = (filePath: string, maxBytes: number): Record => { + const fd = openSync(path.resolve(filePath), constants.O_RDONLY | constants.O_NONBLOCK); + try { + const stat = fstatSync(fd); + if (!stat.isFile() || stat.size > maxBytes) throw new Error(`Handoff file must be a regular file no larger than ${maxBytes} bytes`); + const buffer = Buffer.alloc(maxBytes + 1); + let length = 0; + while (length < buffer.length) { + const count = readSync(fd, buffer, length, buffer.length - length, null); + if (count === 0) break; + length += count; + } + if (length > maxBytes) throw new Error(`Handoff file must be no larger than ${maxBytes} bytes`); + return JSON.parse(buffer.toString("utf8", 0, length)) as Record; + } finally { + closeSync(fd); + } +}; + +const writeCapsuleFile = (outputFile: string, capsule: unknown, force: boolean): void => { + const rendered = `${JSON.stringify(capsule, null, 2)}\n`; + if (Buffer.byteLength(rendered) > HANDOFF_CAPSULE_FILE_MAX_BYTES) { + throw new Error(`Handoff capsule file exceeds ${HANDOFF_CAPSULE_FILE_MAX_BYTES} bytes`); + } + const temporary = path.join(path.dirname(outputFile), `.${path.basename(outputFile)}.${randomUUID()}.tmp`); + try { + writeFileSync(temporary, rendered, { mode: 0o600, flag: "wx" }); + chmodSync(temporary, 0o600); + if (force) renameSync(temporary, outputFile); + else linkSync(temporary, outputFile); + } finally { + try { unlinkSync(temporary); } catch (error) { + if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error; + } + } +}; + +const scopedWorkspaceId = (root: string, requested: string | undefined): string => { + const canonical = `repo:${root}`; + if (requested !== undefined && requested !== canonical) { + throw new Error(`This scoped workspace uses ${canonical}. Remove the workspace-id override or use the bound project.`); + } + return canonical; +}; + +const scopedHandoffBinding = (home: string | undefined, host: HandoffHost, directory: string, profileName: string | undefined, hostHome: string | undefined) => { + const paths = resolveRuntimePaths(home); + const policy = readWorkspaceHookPolicy(paths); + if (!policy) return null; + let selected: ReturnType = null; + for (const binding of policy.bindings) { + if (binding.host !== host) continue; + const match = workspaceHookForInvocation(policy, host, binding.id, directory); + if (!match) continue; + if (selected) throw new Error(`Multiple ${host} bindings apply to this workspace. Run crosshead workspace check to repair the configuration.`); + selected = match; + } + if (!selected) return null; + if (profileName !== undefined && profileName !== selected.binding.profile) { + throw new Error(`This workspace uses profile ${selected.binding.profile} for ${host}. Choose that profile or reconfigure the workspace.`); + } + if (hostHome && path.resolve(hostHome) !== selected.binding.hostHome) { + throw new Error(`This workspace uses ${selected.binding.hostHome} as its ${host} home. Remove the host-home override or reconfigure the workspace.`); + } + return { + home: paths.home, + hostHome: selected.binding.hostHome, + root: selected.root, + profileName: selected.binding.profile, + sourceExcerpts: selected.binding.sourceExcerpts, + legacyUnscoped: selected.binding.legacyUnscoped ?? false, + legacyBefore: selected.binding.legacyBefore, + }; +}; export const handoffFromCli = async (argv: string[]): Promise => { const { positionals, flags } = parseArgs(argv); @@ -116,23 +201,31 @@ export const handoffFromCli = async (argv: string[]): Promise => { let result: Record; if (sourceRuntime === "codex") { const { CodexRuntimeAdapter } = await import("../codex-adapter"); - const adapter = CodexRuntimeAdapter.fromInstalled({ + const scoped = scopedHandoffBinding(typeof flags.home === "string" ? flags.home : undefined, "codex", workspaceRoot, profileName, typeof flags["codex-home"] === "string" ? flags["codex-home"] : undefined); + const adapter = scoped ? CodexRuntimeAdapter.forWorkspace(scoped.home, scoped.hostHome, scoped.profileName, scoped.sourceExcerpts, scoped.legacyUnscoped, scoped.legacyBefore) : CodexRuntimeAdapter.fromInstalled({ home: typeof flags.home === "string" ? flags.home : undefined, hostHome: typeof flags["codex-home"] === "string" ? flags["codex-home"] : undefined, }); result = (await adapter.createHandoff({ ...sharedRequest, - hostHome: typeof flags["codex-home"] === "string" ? flags["codex-home"] : undefined, + profileName: scoped?.profileName ?? sharedRequest.profileName, + workspaceRoot: scoped?.root ?? sharedRequest.workspaceRoot, + workspaceId: scoped ? scopedWorkspaceId(scoped.root, sharedRequest.workspaceId) : sharedRequest.workspaceId, + hostHome: scoped?.hostHome ?? (typeof flags["codex-home"] === "string" ? flags["codex-home"] : undefined), })) as unknown as Record; } else if (sourceRuntime === "claude-code") { const { ClaudeCodeRuntimeAdapter } = await import("../claude-code-adapter"); - const adapter = ClaudeCodeRuntimeAdapter.fromInstalled({ + const scoped = scopedHandoffBinding(typeof flags.home === "string" ? flags.home : undefined, "claude-code", workspaceRoot, profileName, typeof flags["claude-home"] === "string" ? flags["claude-home"] : undefined); + const adapter = scoped ? ClaudeCodeRuntimeAdapter.forWorkspace(scoped.home, scoped.hostHome, scoped.root, scoped.profileName, scoped.sourceExcerpts, scoped.legacyUnscoped, scoped.legacyBefore) : ClaudeCodeRuntimeAdapter.fromInstalled({ home: typeof flags.home === "string" ? flags.home : undefined, hostHome: typeof flags["claude-home"] === "string" ? flags["claude-home"] : undefined, }); result = (await adapter.createHandoff({ ...sharedRequest, - hostHome: typeof flags["claude-home"] === "string" ? flags["claude-home"] : undefined, + profileName: scoped?.profileName ?? sharedRequest.profileName, + workspaceRoot: scoped?.root ?? sharedRequest.workspaceRoot, + workspaceId: scoped ? scopedWorkspaceId(scoped.root, sharedRequest.workspaceId) : sharedRequest.workspaceId, + hostHome: scoped?.hostHome ?? (typeof flags["claude-home"] === "string" ? flags["claude-home"] : undefined), })) as unknown as Record; } else { throw new Error("handoff export requires --source codex or --source claude-code"); @@ -141,11 +234,7 @@ export const handoffFromCli = async (argv: string[]): Promise => { if (outputFile) { const record = result.record as { capsule?: unknown } | undefined; if (!record?.capsule) throw new Error("handoff export produced no capsule to write"); - // mode applies only when the file is created, so a replaced file is narrowed explicitly too. - // Narrow an existing file before writing, so the capsule is never readable by others even briefly. - if (flags.force === true && existsSync(outputFile)) chmodSync(outputFile, 0o600); - writeFileSync(outputFile, `${JSON.stringify(record.capsule, null, 2)}\n`, { mode: 0o600, flag: flags.force === true ? "w" : "wx" }); - chmodSync(outputFile, 0o600); + writeCapsuleFile(outputFile, record.capsule, flags.force === true); result = { ...result, capsuleFile: outputFile }; } return flags.json || flags.debug ? JSON.stringify(result, null, 2) : formatHandoffExportResult(result as unknown as Record); @@ -162,7 +251,7 @@ export const handoffFromCli = async (argv: string[]): Promise => { : ""; const capsule = filePath.trim().length > 0 - ? (JSON.parse(readFileSync(path.resolve(filePath), "utf8")) as Record) + ? readJsonFileBounded(filePath, HANDOFF_CAPSULE_FILE_MAX_BYTES) : undefined; if (!capsule && handoffId.trim().length === 0) { throw new Error("handoff import requires --file or --id "); @@ -214,23 +303,31 @@ export const handoffFromCli = async (argv: string[]): Promise => { let result: Record; if (targetRuntime === "claude-code") { const { ClaudeCodeRuntimeAdapter } = await import("../claude-code-adapter"); - const adapter = ClaudeCodeRuntimeAdapter.fromInstalled({ + const scoped = scopedHandoffBinding(typeof flags.home === "string" ? flags.home : undefined, "claude-code", sharedRequest.workspaceRoot, profileName, typeof flags["claude-home"] === "string" ? flags["claude-home"] : undefined); + const adapter = scoped ? ClaudeCodeRuntimeAdapter.forWorkspace(scoped.home, scoped.hostHome, scoped.root, scoped.profileName, scoped.sourceExcerpts, scoped.legacyUnscoped, scoped.legacyBefore) : ClaudeCodeRuntimeAdapter.fromInstalled({ home: typeof flags.home === "string" ? flags.home : undefined, hostHome: typeof flags["claude-home"] === "string" ? flags["claude-home"] : undefined, }); result = (await adapter.importHandoff({ ...sharedRequest, - hostHome: typeof flags["claude-home"] === "string" ? flags["claude-home"] : undefined, + profileName: scoped?.profileName ?? sharedRequest.profileName, + workspaceRoot: scoped?.root ?? sharedRequest.workspaceRoot, + workspaceId: scoped ? scopedWorkspaceId(scoped.root, sharedRequest.workspaceId) : sharedRequest.workspaceId, + hostHome: scoped?.hostHome ?? (typeof flags["claude-home"] === "string" ? flags["claude-home"] : undefined), })) as unknown as Record; } else if (targetRuntime === "codex") { const { CodexRuntimeAdapter } = await import("../codex-adapter"); - const adapter = CodexRuntimeAdapter.fromInstalled({ + const scoped = scopedHandoffBinding(typeof flags.home === "string" ? flags.home : undefined, "codex", sharedRequest.workspaceRoot, profileName, typeof flags["codex-home"] === "string" ? flags["codex-home"] : undefined); + const adapter = scoped ? CodexRuntimeAdapter.forWorkspace(scoped.home, scoped.hostHome, scoped.profileName, scoped.sourceExcerpts, scoped.legacyUnscoped, scoped.legacyBefore) : CodexRuntimeAdapter.fromInstalled({ home: typeof flags.home === "string" ? flags.home : undefined, hostHome: typeof flags["codex-home"] === "string" ? flags["codex-home"] : undefined, }); result = (await adapter.importHandoff({ ...sharedRequest, - hostHome: typeof flags["codex-home"] === "string" ? flags["codex-home"] : undefined, + profileName: scoped?.profileName ?? sharedRequest.profileName, + workspaceRoot: scoped?.root ?? sharedRequest.workspaceRoot, + workspaceId: scoped ? scopedWorkspaceId(scoped.root, sharedRequest.workspaceId) : sharedRequest.workspaceId, + hostHome: scoped?.hostHome ?? (typeof flags["codex-home"] === "string" ? flags["codex-home"] : undefined), })) as unknown as Record; } else { throw new Error("handoff import currently supports only --target claude-code or --target codex"); @@ -356,7 +453,7 @@ export const handoffFromCli = async (argv: string[]): Promise => { if (inputPath.trim().length === 0) { throw new Error("handoff create requires --input "); } - const payload = JSON.parse(readFileSync(path.resolve(inputPath), "utf8")) as Record; + const payload = readJsonFileBounded(inputPath, HANDOFF_CREATE_INPUT_MAX_BYTES); const result = await daemon.createHandoff( { ...payload, @@ -384,7 +481,7 @@ export const handoffFromCli = async (argv: string[]): Promise => { if (filePath.trim().length === 0) { throw new Error("handoff verify requires --file "); } - const capsule = JSON.parse(readFileSync(path.resolve(filePath), "utf8")) as Record; + const capsule = readJsonFileBounded(filePath, HANDOFF_CAPSULE_FILE_MAX_BYTES); const result = await daemon.verifyHandoff({ capsule }, profileName); return flags.json || flags.debug ? JSON.stringify(result, null, 2) : formatHandoffVerification(result); } @@ -398,7 +495,7 @@ export const handoffFromCli = async (argv: string[]): Promise => { ? flags["handoff-id"] : ""; if (filePath.trim().length > 0) { - const capsule = JSON.parse(readFileSync(path.resolve(filePath), "utf8")) as Record; + const capsule = readJsonFileBounded(filePath, HANDOFF_CAPSULE_FILE_MAX_BYTES); const result = await daemon.inspectHandoff({ capsule }, profileName); return flags.json || flags.debug ? JSON.stringify(result, null, 2) : formatHandoffInspection(result); } diff --git a/src/runtime/codex-adapter.ts b/src/runtime/codex-adapter.ts index 37fd843..a04836c 100644 --- a/src/runtime/codex-adapter.ts +++ b/src/runtime/codex-adapter.ts @@ -41,11 +41,12 @@ import { parseArgs, sanitizeString, shellQuote, - summarizeHotMemory, + summarizeHotMemorySnapshot, summarizeJudgmentEntries, writeManagedFile, } from "./adapter-utils"; import { + filterHotMemorySnapshotForWorkspace, HostRuntimeAdapterBase, type HostAdapterCompactEventRequest, type HostAdapterIdentity, @@ -988,28 +989,31 @@ export class CodexRuntimeAdapter extends HostRuntimeAdapterBase { async createHandoff(input: CodexAdapterHandoffCreateRequest): Promise { const profileName = input.profileName ?? this.config.profileName; + const workspaceId = input.workspaceId ?? `repo:${path.resolve(input.workspaceRoot)}`; const needsHotSummary = !input.memory?.hotSummary; - const needsWarmIds = !input.memory?.warmIds || input.memory.warmIds.length === 0; - const needsJudgment = + // Hosted warm and judgment views are profile-wide and have no trustworthy project filter. + const needsWarmIds = !this.config.workspaceOnly && (!input.memory?.warmIds || input.memory.warmIds.length === 0); + const needsJudgment = !this.config.workspaceOnly && ( !input.memory?.selfModelIds || input.memory.selfModelIds.length === 0 || !input.reasoning?.currentTheory || !input.reasoning?.openQuestions || input.reasoning.openQuestions.length === 0 || !input.reasoning?.rejectedPaths || - !input.reasoning.unresolvedRisks; + !input.reasoning.unresolvedRisks); const needsIdentity = !input.identity?.npub; const needsPolicyRef = !input.policy?.policyRef; const targetRuntime = input.targetRuntime ?? "claude-code"; const targetLabel = targetRuntime === "codex" ? "Codex" : "Claude Code"; const warnings: string[] = []; - const [hot, warm, judgment, status] = await Promise.all([ - needsHotSummary ? this.client.readHotMemory(profileName) : Promise.resolve(null), + const [rawHot, warm, judgment, status] = await Promise.all([ + needsHotSummary ? this.client.readHotMemory(profileName, "all") : Promise.resolve(null), optionalHostedRead(needsWarmIds, () => this.client.listWarmMemory(profileName, { status: "active" }), "Warm memory", warnings), optionalHostedRead(needsJudgment, () => this.client.inspectContinuityJudgment(profileName), "Continuity judgment", warnings), needsIdentity || needsPolicyRef ? this.client.status(profileName) : Promise.resolve(null), ]); + const hot = filterHotMemorySnapshotForWorkspace(rawHot, workspaceId, this.config.workspaceOnly, this.config.legacyUnscoped, this.config.legacyBefore); const snapshot = judgment?.snapshot && typeof judgment.snapshot === "object" && !Array.isArray(judgment.snapshot) @@ -1064,7 +1068,7 @@ export class CodexRuntimeAdapter extends HostRuntimeAdapterBase { nextAction: input.reasoning?.nextAction ?? null, }, memory: { - hotSummary: input.memory?.hotSummary ?? summarizeHotMemory(hot?.content), + hotSummary: input.memory?.hotSummary ?? summarizeHotMemorySnapshot(hot, this.config.workspaceOnly ? "codex" : "shared"), warmIds: input.memory?.warmIds ?? activeWarmIds, coldChunkIds: input.memory?.coldChunkIds ?? [], projectModelIds: input.memory?.projectModelIds ?? [], @@ -1105,6 +1109,7 @@ export class CodexRuntimeAdapter extends HostRuntimeAdapterBase { const plainTextSummary = buildPlainTextContinuationSummary(translation, targetRuntime); const restatementComparison = compareContinuityRestatement(materialization.startupContext, translation, targetRuntime); const created = (await this.client.createHandoff({ + profileName, translation, maxBytes: input.maxBytes, })) as { handoff?: HandoffStoredRecord }; diff --git a/src/runtime/hosted-client.ts b/src/runtime/hosted-client.ts index 049ccc6..ac5d407 100644 --- a/src/runtime/hosted-client.ts +++ b/src/runtime/hosted-client.ts @@ -12,6 +12,7 @@ import type { ContinuityClaimVerificationResult, } from "../protocol/continuity-claims"; import { L402_IDEMPOTENCY_HEADER } from "../protocol/hosted-api"; +import { SHARED_HOT_SCOPE } from "../protocol/memory"; export type HostedSession = { sessionToken: string; @@ -383,7 +384,7 @@ export class HostedOnlyBotsClient { private async signMemory(content: string, scope?: string): Promise { const unsigned: Omit = { version: SIGNED_MEMORY_VERSION, - scope: scope ?? null, + scope: scope ?? SHARED_HOT_SCOPE, content, signedBy: this.config.npub, createdAt: new Date().toISOString(), @@ -394,14 +395,17 @@ export class HostedOnlyBotsClient { return JSON.stringify({ ...unsigned, signature } satisfies SignedMemoryEnvelope); } - private async verifyMemory(content: unknown, expectedScope?: string): Promise { + private async verifyMemory(content: unknown, expectedScope?: string): Promise<{ content: string; signedAt: string } | null> { if (typeof content !== "string" || content.length > MAX_JSON_RESPONSE_BYTES) return null; let value: unknown; try { value = JSON.parse(content); } catch { return null; } if (!value || typeof value !== "object" || Array.isArray(value)) return null; const envelope = value as Partial; + const resolvedScope = expectedScope ?? SHARED_HOT_SCOPE; + const validScope = envelope.scope === resolvedScope || + (resolvedScope === SHARED_HOT_SCOPE && envelope.scope === null); if (envelope.version !== SIGNED_MEMORY_VERSION || envelope.signedBy !== this.config.npub || - envelope.scope !== (expectedScope ?? null) || typeof envelope.content !== "string" || + !validScope || (envelope.scope !== null && typeof envelope.scope !== "string") || typeof envelope.content !== "string" || typeof envelope.createdAt !== "string" || !Number.isFinite(Date.parse(envelope.createdAt)) || typeof envelope.signature !== "string" || !/^[0-9a-f]{128}$/i.test(envelope.signature)) return null; const decoded = nip19.decode(envelope.signedBy); @@ -418,7 +422,7 @@ export class HostedOnlyBotsClient { new TextEncoder().encode(this.memorySignaturePayload(unsigned)), decoded.data ); - return valid ? envelope.content : null; + return valid ? { content: envelope.content, signedAt: envelope.createdAt } : null; } private async verifiedHotMemoryResponse( @@ -430,15 +434,15 @@ export class HostedOnlyBotsClient { if (!raw || typeof raw !== "object" || Array.isArray(raw)) continue; const entry = raw as Record; const scope = typeof entry.scope === "string" ? entry.scope : undefined; - const content = await this.verifyMemory(entry.content, scope); - if (content !== null) scopes.push({ ...entry, content, integrity: "verified" }); + const verified = await this.verifyMemory(entry.content, scope); + if (verified !== null) scopes.push({ ...entry, content: verified.content, signed_at: verified.signedAt, integrity: "verified" }); } return { ...data, scopes, integrity: "verified-client-signatures" }; } - const content = await this.verifyMemory(data.content, expectedScope); - return content === null + const verified = await this.verifyMemory(data.content, expectedScope); + return verified === null ? { ...data, content: "", integrity: "rejected-unverified" } - : { ...data, content, integrity: "verified" }; + : { ...data, content: verified.content, signed_at: verified.signedAt, integrity: "verified" }; } // Header bag for L402 paid operations. The hosted API rejects paid POSTs without a valid diff --git a/src/runtime/local-runtime.ts b/src/runtime/local-runtime.ts index 71a92b4..70e27e0 100644 --- a/src/runtime/local-runtime.ts +++ b/src/runtime/local-runtime.ts @@ -3343,8 +3343,8 @@ export class OnlyBotsLocalRuntimeDaemon { return manager.read(scope ?? "shared"); } // Pass the scope through unchanged (undefined => the server's shared default), so internal - // callers like adapter handoff summaries keep the single-doc shape. The operator CLI opts - // into the cross-body union explicitly by passing scope=all. + // legacy callers keep the single-doc shape. Handoff and operator flows request + // the cross-body union explicitly with scope=all and filter it by workspace. const result = await client.readHotMemory(scope); await this.persistSession(resolvedProfileName, secret, client); return result; diff --git a/src/runtime/workspace-memory.ts b/src/runtime/workspace-memory.ts index b3dad3b..c8d0173 100644 --- a/src/runtime/workspace-memory.ts +++ b/src/runtime/workspace-memory.ts @@ -58,9 +58,12 @@ export const filterHotMemorySnapshotForWorkspace = ( }; const normalize = (entry: Record): Record | null => { const content = parseHotMemoryEntryContent(entry); - if (!content || !equivalent(getWorkspaceId(content), entry.updated_at ?? content.updatedAt ?? content.occurredAt)) return null; + // A hosted server can change updated_at. Legacy adoption must use the timestamp + // inside the verified client signature; local files may use their local mtime. + const observedAt = entry.integrity === "verified" ? entry.signed_at : entry.updated_at ?? content?.updatedAt ?? content?.occurredAt; + if (!content || !equivalent(getWorkspaceId(content), observedAt)) return null; const checkpoints = Array.isArray(content.continuityCheckpoints) - ? content.continuityCheckpoints.filter(isRecord).filter(checkpoint => equivalent(getWorkspaceId(checkpoint), checkpoint.occurredAt ?? entry.updated_at ?? content.updatedAt)) + ? content.continuityCheckpoints.filter(isRecord).filter(checkpoint => equivalent(getWorkspaceId(checkpoint), checkpoint.occurredAt ?? observedAt)) .map(checkpoint => ({ ...checkpoint, workspaceId })) : undefined; return { ...entry, content: JSON.stringify({ ...content, workspaceId, ...(checkpoints ? { continuityCheckpoints: checkpoints } : {}) }) }; @@ -78,7 +81,7 @@ export const filterHotMemorySnapshotForWorkspace = ( const entryWorkspaceId = getWorkspaceId(parseHotMemoryEntryContent(hotMemory)); return entryWorkspaceId === workspaceId || (!workspaceOnly && !entryWorkspaceId) ? hotMemory : null; } - const scopes = scopedEntries.filter((entry) => { + let scopes = scopedEntries.filter((entry) => { if (!hasHotMemoryEntryContent(entry)) { return false; } @@ -88,6 +91,12 @@ export const filterHotMemorySnapshotForWorkspace = ( if (scopes.length === 0) { return null; } + // Once this project has its own hot state, a legacy shared doc may describe + // another project. Keep the shared fallback only until project state exists. + if (!workspaceOnly) { + const owned = scopes.filter((entry) => getWorkspaceId(parseHotMemoryEntryContent(entry)) === workspaceId); + if (owned.length > 0) scopes = owned; + } // The legacy runtime alias and its retained workspace partition describe one // document. Inject the newest version once, under the readable runtime name. const selected = new Map>(); diff --git a/tests/runtime/handoff-cli-scoped.test.ts b/tests/runtime/handoff-cli-scoped.test.ts new file mode 100644 index 0000000..4d13cf0 --- /dev/null +++ b/tests/runtime/handoff-cli-scoped.test.ts @@ -0,0 +1,122 @@ +import { execFileSync, spawn } from "node:child_process"; +import { lstatSync, mkdirSync, mkdtempSync, readFileSync, rmSync, statSync, symlinkSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import path from "node:path"; + +import { expect, it, vi } from "vitest"; + +import { generateRuntimeIdentity, OnlyBotsLocalRuntimeDaemon, setupRuntimeProfile } from "../../src/runtime/local-runtime"; +import { writeLocalHotMemory } from "../../src/runtime/local-hot-memory"; +import { resolveRuntimePaths } from "../../src/runtime/runtime-persistence"; +import { workspaceHotScope } from "../../src/runtime/workspace-hot-memory"; + +const cliFile = path.resolve(__dirname, "../../dist/runtime/cli.js"); + +it("exports and imports a signed handoff after normal scoped project setup", async () => { + const root = mkdtempSync(path.join(tmpdir(), "crosshead-scoped-handoff-")); + const home = path.join(root, "runtime"); + const project = path.join(root, "project"); + const otherProject = path.join(root, "other-project"); + const codexHome = path.join(root, "codex"); + const claudeHome = path.join(root, "claude"); + const capsuleFile = path.join(root, "capsule.json"); + for (const directory of [project, otherProject, codexHome, claudeHome]) mkdirSync(directory, { recursive: true }); + execFileSync("git", ["init", "-q", project]); + const env = { ...process.env, CROSSHEAD_HOME: home, CODEX_HOME: codexHome, CLAUDE_CONFIG_DIR: claudeHome }; + for (const key of ["CROSSHEAD_PROFILE", "ONLYBOTS_PROFILE", "ONLYBOTS_HOME", "ONLYBOTS_RUNTIME_HOME", "ONLYBOTS_CODEX_WRAPPER_ACTIVE"]) delete env[key]; + const cli = async (...args: string[]): Promise<{ status: number | null; stdout: string; stderr: string }> => await new Promise((resolve, reject) => { + const child = spawn(process.execPath, [cliFile, ...args, "--json"], { cwd: project, env }); + let stdout = "", stderr = ""; + child.stdout.on("data", (chunk) => { stdout += String(chunk); }); + child.stderr.on("data", (chunk) => { stderr += String(chunk); }); + child.on("error", reject); + child.on("close", (status) => resolve({ status, stdout, stderr })); + }); + let daemon: OnlyBotsLocalRuntimeDaemon | null = null; + try { + await setupRuntimeProfile({ home, profileName: "default", apiBase: "http://127.0.0.1:0", ...generateRuntimeIdentity(), makeDefault: true }); + const scopedIdentity = generateRuntimeIdentity(); + const scopedProfile = await setupRuntimeProfile({ home, profileName: "work", apiBase: "http://127.0.0.1:0", ...scopedIdentity, makeDefault: false }); + const setup = await cli("quickstart", "--scope", "project", "--adapter", "both", "--workspace-root", project, + "--profile", "work", "--home", home, "--codex-home", codexHome, "--claude-home", claudeHome, "--no-start"); + expect(setup.status, setup.stdout + setup.stderr).toBe(0); + expect(JSON.parse(setup.stdout).bindings).toHaveLength(2); + daemon = new OnlyBotsLocalRuntimeDaemon(home); + await daemon.start(); + const projectWideJudgment = vi.spyOn(daemon, "inspectContinuityJudgment").mockResolvedValue({ + snapshot: { projectModel: { currentDirection: [{ summary: "OTHER PROJECT PRIVATE DIRECTION" }] } }, + }); + const projectWideWarm = vi.spyOn(daemon, "listWarmMemory").mockResolvedValue({ + entries: [{ id: "other-project-warm-id" }], + }); + const state = resolveRuntimePaths(home).state; + const workspaceId = `repo:${project}`; + const otherWorkspaceId = `repo:${otherProject}`; + const checkpoint = (id: string, summary: string) => JSON.stringify({ runtime: "codex", workspaceId: id, continuityCheckpoints: [{ + runtime: "codex", workspaceId: id, sessionId: "prior", turnId: summary, goal: "Finish the task", summary, + occurredAt: new Date().toISOString(), + }] }); + writeLocalHotMemory(state, checkpoint(workspaceId, "This project's checksum still fails on empty rows"), workspaceHotScope("codex", workspaceId), scopedProfile); + writeLocalHotMemory(state, checkpoint(otherWorkspaceId, "OTHER WORKSPACE SECRET"), workspaceHotScope("codex", otherWorkspaceId), scopedProfile); + + const exported = await cli("handoff", "export", "--source", "codex", "--target", "claude-code", + "--workspace-root", project, "--session-id", "source-session", "--prompt", "Finish the scoped handoff", + "--home", home, "--output", capsuleFile); + expect(exported.status, exported.stdout + exported.stderr).toBe(0); + expect(JSON.parse(exported.stdout).record.capsule.sourceRuntime).toBe("codex"); + expect(statSync(capsuleFile).mode & 0o777).toBe(0o600); + const capsule = JSON.parse(readFileSync(capsuleFile, "utf8")); + expect(capsule.targetRuntime).toBe("claude-code"); + expect(capsule.signature.signedBy).toBe(scopedIdentity.npub); + expect(capsule.continuityIr.identity.npub).toBe(scopedIdentity.npub); + expect(capsule.continuityIr.memoryReferences.hotMemory.summary).toContain("checksum still fails on empty rows"); + expect(JSON.stringify(capsule)).not.toContain("OTHER WORKSPACE SECRET"); + expect(JSON.stringify(capsule)).not.toContain("OTHER PROJECT PRIVATE DIRECTION"); + expect(JSON.stringify(capsule)).not.toContain("other-project-warm-id"); + expect(projectWideJudgment).not.toHaveBeenCalled(); + expect(projectWideWarm).not.toHaveBeenCalled(); + const oversizedFile = path.join(root, "oversized.json"); + writeFileSync(oversizedFile, " ".repeat(128 * 1024 + 1)); + for (const subcommand of ["import", "verify", "explain"]) { + const oversized = await cli("handoff", subcommand, "--file", oversizedFile, "--home", home); + expect(oversized.status).not.toBe(0); + expect(JSON.parse(oversized.stdout).error.message).toContain("no larger than 131072 bytes"); + } + const preservedFile = path.join(root, "preserved.txt"); + const forcedFile = path.join(root, "forced-capsule.json"); + writeFileSync(preservedFile, "keep this file"); + symlinkSync(preservedFile, forcedFile); + const forced = await cli("handoff", "export", "--source", "codex", "--target", "claude-code", + "--workspace-root", project, "--session-id", "source-session", "--prompt", "Export safely", + "--home", home, "--output", forcedFile, "--force"); + expect(forced.status, forced.stdout + forced.stderr).toBe(0); + expect(readFileSync(preservedFile, "utf8")).toBe("keep this file"); + expect(lstatSync(forcedFile).isSymbolicLink()).toBe(false); + expect(statSync(forcedFile).mode & 0o777).toBe(0o600); + const wrongWorkspace = await cli("handoff", "export", "--source", "codex", "--target", "claude-code", + "--workspace-root", project, "--workspace-id", otherWorkspaceId, "--session-id", "source-session", + "--prompt", "Export the other project", "--home", home); + expect(wrongWorkspace.status).not.toBe(0); + expect(JSON.parse(wrongWorkspace.stdout).error.message).toContain("workspace-id override"); + expect(wrongWorkspace.stdout).not.toContain("OTHER WORKSPACE SECRET"); + + const imported = await cli("handoff", "import", "--target", "claude-code", "--file", capsuleFile, + "--workspace-root", project, "--home", home); + expect(imported.status, imported.stdout + imported.stderr).toBe(0); + expect(JSON.parse(imported.stdout)).toMatchObject({ accepted: true, savedToHotMemory: true, verification: { valid: true } }); + const wrongImport = await cli("handoff", "import", "--target", "claude-code", "--file", capsuleFile, + "--workspace-root", project, "--workspace-id", otherWorkspaceId, "--home", home); + expect(wrongImport.status).not.toBe(0); + expect(JSON.parse(wrongImport.stdout).error.message).toContain("workspace-id override"); + + const wrongProfile = await cli("handoff", "export", "--source", "codex", "--target", "claude-code", + "--workspace-root", project, "--session-id", "source-session", "--prompt", "Wrong profile", + "--profile", "default", "--home", home); + expect(wrongProfile.status).not.toBe(0); + expect(JSON.parse(wrongProfile.stdout).error.message).toContain("This workspace uses profile work"); + } finally { + vi.restoreAllMocks(); + if (daemon) await daemon.stop(); + rmSync(root, { recursive: true, force: true }); + } +}, 60_000); diff --git a/tests/runtime/host-adapter-base.test.ts b/tests/runtime/host-adapter-base.test.ts index 8554f07..3529ae4 100644 --- a/tests/runtime/host-adapter-base.test.ts +++ b/tests/runtime/host-adapter-base.test.ts @@ -32,6 +32,12 @@ describe("host adapter hot-memory filtering", () => { expect(filterHotMemorySnapshotForWorkspace(snapshot, "repo:/fixture", true)?.scopes).toEqual([snapshot.scopes[1]]); expect(filterHotMemorySnapshotForWorkspace(snapshot, "repo:/other", true)).toBeNull(); }); + it("uses legacy shared memory only before the current project has its own state", () => { + const shared = { scope: "shared", content: JSON.stringify({ currentGoal: "other project work" }) }; + const owned = { scope: "codex", content: JSON.stringify({ workspaceId: "repo:/fixture", currentGoal: "this project work" }) }; + expect(filterHotMemorySnapshotForWorkspace({ scope: "all", scopes: [shared] }, "repo:/fixture")?.scopes).toEqual([shared]); + expect(filterHotMemorySnapshotForWorkspace({ scope: "all", scopes: [shared, owned] }, "repo:/fixture")?.scopes).toEqual([owned]); + }); it("delivers the newer workspace copy once and accepts newer legacy-client updates", () => { const workspaceId = "repo:/tmp/project", runtime = "codex"; const older = { workspaceId, runtime, updatedAt: "2026-10-20T00:00:00Z", lastTurn: { summary: "old finding" } }; @@ -122,6 +128,22 @@ describe("host adapter hot-memory filtering", () => { { scope: "claude-code", workspaceId: "repo:/tmp/foreign", content: currentContent }, ]); }); + + it("uses the signed time when adopting legacy hosted memory", () => { + const workspaceId = "repo:/tmp/current/workspace"; + const entry = { + scope: "shared", + content: JSON.stringify({ summary: "legacy note" }), + updated_at: "2025-01-01T00:00:00Z", + signed_at: "2027-01-01T00:00:00Z", + integrity: "verified", + }; + const snapshot = { scope: "all", scopes: [entry] }; + expect(filterHotMemorySnapshotForWorkspace(snapshot, workspaceId, true, true, "2026-01-01T00:00:00Z")).toBeNull(); + expect(filterHotMemorySnapshotForWorkspace({ scope: "all", scopes: [{ ...entry, signed_at: undefined }] }, workspaceId, true, true, "2026-01-01T00:00:00Z")).toBeNull(); + const old = { ...entry, signed_at: "2025-01-01T00:00:00Z", updated_at: "2027-01-01T00:00:00Z" }; + expect(filterHotMemorySnapshotForWorkspace({ scope: "all", scopes: [old] }, workspaceId, true, true, "2026-01-01T00:00:00Z")?.scopes).toHaveLength(1); + }); }); describe("host adapter prompt trust boundary", () => { diff --git a/tests/runtime/hosted-client-security.test.ts b/tests/runtime/hosted-client-security.test.ts index e575404..4125240 100644 --- a/tests/runtime/hosted-client-security.test.ts +++ b/tests/runtime/hosted-client-security.test.ts @@ -149,7 +149,10 @@ describe("hosted client security boundaries", () => { stored = String((await readJson(request)).content); return json(response, 200, { ok: true }); } - return json(response, 200, { content: stored }); + if (request.url === "/v1/memory/hot?scope=all") { + return json(response, 200, { scope: "all", scopes: [{ scope: "shared", content: stored }] }); + } + return json(response, 200, { scope: "shared", content: stored }); }); const client = new HostedOnlyBotsClient({ apiBase: base, npub: signer.npub, nsec: signer.nsec, @@ -157,10 +160,43 @@ describe("hosted client security boundaries", () => { }); await client.writeHotMemory('{"goal":"safe"}'); expect(stored).not.toContain(signer.nsec); + expect((JSON.parse(stored) as Record).scope).toBe("shared"); expect(await client.readHotMemory()).toMatchObject({ content: '{"goal":"safe"}', integrity: "verified" }); + expect(await client.readHotMemory("all")).toMatchObject({ + scopes: [{ scope: "shared", content: '{"goal":"safe"}', signed_at: expect.any(String), integrity: "verified" }], + }); const envelope = JSON.parse(stored) as Record; stored = JSON.stringify({ ...envelope, content: '{"goal":"server injected"}' }); expect(await client.readHotMemory()).toMatchObject({ content: "", integrity: "rejected-unverified" }); + expect(await client.readHotMemory("all")).toMatchObject({ scopes: [] }); + }); + + it("reads legacy default-scope signatures as shared only", async () => { + const signer = identity(); + const unsigned = { + version: "crosshead_signed_memory_v1", + scope: null, + content: '{"goal":"legacy"}', + signedBy: signer.npub, + createdAt: new Date().toISOString(), + }; + const canonical = JSON.stringify(Object.fromEntries(Object.entries(unsigned).sort(([a], [b]) => a.localeCompare(b)))); + const signature = Buffer.from(await schnorr.sign(new TextEncoder().encode(`crosshead-memory-v1\n${canonical}`), signer.secret)).toString("hex"); + const stored = JSON.stringify({ ...unsigned, signature }); + const base = await listen((request, response) => { + if (request.url === "/v1/memory/hot?scope=all") { + return json(response, 200, { scope: "all", scopes: [{ scope: "shared", content: stored }, { scope: "codex", content: stored }] }); + } + return json(response, 200, { scope: "shared", content: stored }); + }); + const client = new HostedOnlyBotsClient({ + apiBase: base, npub: signer.npub, nsec: signer.nsec, + session: { sessionToken: "session-test", expiresAt: new Date(Date.now() + 60_000).toISOString(), scopes: [] }, + }); + expect(await client.readHotMemory()).toMatchObject({ content: unsigned.content, integrity: "verified" }); + expect(await client.readHotMemory("all")).toMatchObject({ + scopes: [{ scope: "shared", content: unsigned.content, integrity: "verified" }], + }); }); it("drops unauthenticated hosted context text and bounds JSON responses", async () => {