|
| 1 | +import { spawnSync } from "node:child_process"; |
| 2 | +import { runCommandGuard } from "./command-guard.js"; |
| 3 | +import { createSidecarRecorder, type OpenCodeSidecarRuntimeContext } from "./events.js"; |
| 4 | +import { exitCodeFromOutput, outputText, toolKey } from "./evidence.js"; |
| 5 | +import { createIdleVerifier } from "./idle-verify.js"; |
| 6 | +import { commandFromTool, pathsFromTool } from "./paths.js"; |
| 7 | +import { currentSidecarWorkingTreeHash } from "./worktree-hash.js"; |
| 8 | + |
| 9 | +export async function OpenCodePlusplusSidecar(context: OpenCodeSidecarRuntimeContext): Promise<Record<string, unknown>> { |
| 10 | + return createOpenCodePlusplusSidecar(context); |
| 11 | +} |
| 12 | + |
| 13 | +export async function createOpenCodePlusplusSidecar(context: OpenCodeSidecarRuntimeContext): Promise<Record<string, unknown>> { |
| 14 | + const recorder = createSidecarRecorder(context); |
| 15 | + const idle = createIdleVerifier(context.directory, recorder); |
| 16 | + const toolStarts = new Map<string, { startedAt: string; workingTreeHashBefore: string }>(); |
| 17 | + |
| 18 | + function rememberToolStart(tool: unknown, args: unknown): void { |
| 19 | + toolStarts.set(toolKey(tool, args), { |
| 20 | + startedAt: new Date().toISOString(), |
| 21 | + workingTreeHashBefore: currentSidecarWorkingTreeHash(context.directory) |
| 22 | + }); |
| 23 | + } |
| 24 | + |
| 25 | + function recordToolAfter(input: unknown, output: unknown): void { |
| 26 | + try { |
| 27 | + const inputRecord = input && typeof input === "object" ? (input as Record<string, unknown>) : {}; |
| 28 | + const outputRecord = output && typeof output === "object" ? (output as Record<string, unknown>) : {}; |
| 29 | + const tool = inputRecord.tool ?? inputRecord.name ?? "unknown"; |
| 30 | + const args = inputRecord.args ?? inputRecord.arguments ?? {}; |
| 31 | + const command = commandFromTool(tool, args); |
| 32 | + const paths = pathsFromTool(args); |
| 33 | + const key = toolKey(tool, args); |
| 34 | + const started = toolStarts.get(key) ?? { |
| 35 | + startedAt: new Date().toISOString(), |
| 36 | + workingTreeHashBefore: currentSidecarWorkingTreeHash(context.directory) |
| 37 | + }; |
| 38 | + toolStarts.delete(key); |
| 39 | + |
| 40 | + const cliArgs = [ |
| 41 | + "sidecar", |
| 42 | + "record-tool", |
| 43 | + context.directory, |
| 44 | + "--json", |
| 45 | + "--tool", |
| 46 | + String(tool), |
| 47 | + "--exit-code", |
| 48 | + String(exitCodeFromOutput(output) ?? 0), |
| 49 | + "--started-at", |
| 50 | + started.startedAt, |
| 51 | + "--finished-at", |
| 52 | + new Date().toISOString(), |
| 53 | + "--working-tree-hash-before", |
| 54 | + started.workingTreeHashBefore, |
| 55 | + "--working-tree-hash-after", |
| 56 | + currentSidecarWorkingTreeHash(context.directory) |
| 57 | + ]; |
| 58 | + if (command) cliArgs.push("--command", command); |
| 59 | + const sessionId = inputRecord.sessionID ?? inputRecord.sessionId ?? outputRecord.sessionID ?? outputRecord.sessionId; |
| 60 | + if (sessionId) cliArgs.push("--session-id", String(sessionId)); |
| 61 | + const stdout = outputText(output, ["stdout", "output", "text"]); |
| 62 | + const stderr = outputText(output, ["stderr", "error"]); |
| 63 | + if (stdout) cliArgs.push("--stdout", stdout); |
| 64 | + if (stderr) cliArgs.push("--stderr", stderr); |
| 65 | + for (const file of paths) cliArgs.push("--path", file); |
| 66 | + |
| 67 | + const recordResult = spawnSync("opencode-plusplus", cliArgs, { |
| 68 | + cwd: context.directory, |
| 69 | + encoding: "utf8", |
| 70 | + shell: process.platform === "win32", |
| 71 | + maxBuffer: 10 * 1024 * 1024 |
| 72 | + }); |
| 73 | + recorder.record("sidecar.record-tool", { tool, command, paths, exitCode: recordResult.status ?? 1 }); |
| 74 | + if ((recordResult.status ?? 1) !== 0) { |
| 75 | + recorder.log("debug", "tool evidence record failed", { tool, command, status: recordResult.status ?? 1 }); |
| 76 | + } |
| 77 | + } catch (error) { |
| 78 | + recorder.log("debug", "tool evidence record failed", { message: error instanceof Error ? error.message : String(error) }); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + return { |
| 83 | + name: "opencode-plusplus-sidecar", |
| 84 | + "tool.execute.before": async ({ tool, args }: { tool: unknown; args: unknown }) => { |
| 85 | + rememberToolStart(tool, args); |
| 86 | + runCommandGuard(context.directory, recorder, tool, args); |
| 87 | + }, |
| 88 | + "tool.execute.after": async (input: unknown, output: unknown) => { |
| 89 | + recordToolAfter(input, output); |
| 90 | + }, |
| 91 | + event: async ({ event }: { event?: Record<string, unknown> }) => { |
| 92 | + const eventRecord = event ?? {}; |
| 93 | + const type = eventRecord.type; |
| 94 | + if (type === "session.created") { |
| 95 | + recorder.record("session.created"); |
| 96 | + recorder.log("debug", "sidecar active", { directory: context.directory, worktree: context.worktree }); |
| 97 | + } |
| 98 | + |
| 99 | + if (type === "file.edited") { |
| 100 | + const properties = eventRecord.properties && typeof eventRecord.properties === "object" ? (eventRecord.properties as Record<string, unknown>) : {}; |
| 101 | + const file = properties.file ?? properties.path ?? eventRecord.file ?? eventRecord.path ?? "unknown"; |
| 102 | + idle.markDirty("file.edited", { file }); |
| 103 | + } |
| 104 | + |
| 105 | + if (type === "file.watcher.updated") { |
| 106 | + const properties = eventRecord.properties && typeof eventRecord.properties === "object" ? (eventRecord.properties as Record<string, unknown>) : {}; |
| 107 | + const file = properties.file ?? properties.path ?? eventRecord.file ?? eventRecord.path ?? "unknown"; |
| 108 | + idle.markDirty("file.watcher.updated", { file }); |
| 109 | + } |
| 110 | + |
| 111 | + if (type === "session.idle") { |
| 112 | + recorder.record("session.idle"); |
| 113 | + idle.maybeVerifyOnIdle(); |
| 114 | + } |
| 115 | + } |
| 116 | + }; |
| 117 | +} |
0 commit comments