diff --git a/README.md b/README.md index b10bf381..c3d4737f 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ It finds the agent sessions that actually ran in your repo, reads what happened in them, and proposes evidence-backed edits to your memory surface - the memory file and project skills - under a token budget, gated by you. -- **Local-first** - Reads the transcript stores of seven agent harnesses directly from disk, +- **Local-first** - Reads the transcript stores of eight agent harnesses directly from disk, locally or over SSH to your own machines. No API, no upload; transcripts never leave your machines except into an agent you already authenticated, and obvious secrets are redacted before they do. @@ -220,7 +220,7 @@ It cannot be combined with ### 1. Collect samples - which sessions belong to this repo -backpass reads the local transcript stores of seven harnesses directly. No API, no upload. +backpass reads the local transcript stores of eight harnesses directly. No API, no upload. | Harness | Store | Repo tie | | -------------- | ----------------------------------------------- | --------------------------------------------------- | @@ -231,6 +231,7 @@ backpass reads the local transcript stores of seven harnesses directly. No API, | **grok** | `~/.grok/sessions///` | `summary.json` `cwd` + `git_remotes` | | **cursor CLI** | `~/.cursor/chats///` | `meta.json` `cwd` | | **hermes** | `~/.hermes/state.db` (sqlite) | session cwd, with CLI prompt / ACP config fallbacks | +| **copilot** | `~/.copilot/session-state//events.jsonl` | `session.start` `cwd` + `gitRoot` (no remote) | Claude collection covers `$CLAUDE_CONFIG_DIR/projects` alongside the default store, so a relocated config dir does not hide its sessions. The variable is read from backpass's own @@ -888,7 +889,7 @@ CLI flags on top: ] }, "discovery": { - "harnesses": ["claude", "codex", "pi", "opencode", "grok", "cursor", "hermes"], + "harnesses": ["claude", "codex", "pi", "opencode", "grok", "cursor", "hermes", "copilot"], "since": "30d", "worktreeGlobs": [], "cloneRoots": [], diff --git a/src/agents.js b/src/agents.js index fb063dec..91ff1711 100644 --- a/src/agents.js +++ b/src/agents.js @@ -58,6 +58,7 @@ export const VERDICT_LABELS = { const LOGIN_HINTS = { codex: "codex login", + copilot: "copilot login", claude: "claude auth login", grok: "grok login", opencode: "opencode auth login", diff --git a/src/cli.js b/src/cli.js index 4793f780..397295d0 100644 --- a/src/cli.js +++ b/src/cli.js @@ -93,7 +93,7 @@ COMMANDS COLLECT SAMPLES --since only sessions newer than this (30d, 12h, 2w, all) [30d] --harness limit to these harnesses - (claude, codex, pi, opencode, grok, cursor, hermes) + (claude, codex, pi, opencode, grok, cursor, hermes, copilot) --strict deterministic associations only (tiers 1, 1.5, and 2) --host also collect from this SSH host this run (repeatable; "none" collects locally only). Configure hosts once in diff --git a/src/config.js b/src/config.js index 28af46ba..50ee1f3e 100644 --- a/src/config.js +++ b/src/config.js @@ -8,7 +8,7 @@ import { normalizeSkillsDir } from "./skills.js"; export const CONFIG_FILENAME = ".backpassrc.json"; export const STATE_DIRNAME = ".backpass"; -export const ALL_HARNESSES = ["claude", "codex", "pi", "opencode", "grok", "cursor", "hermes"]; +export const ALL_HARNESSES = ["claude", "codex", "pi", "opencode", "grok", "cursor", "hermes", "copilot"]; /** Cursor IDE is deferred to v1.1 and only ever runs behind --include-cursor-ide. */ export const OPT_IN_HARNESSES = ["cursor-ide"]; diff --git a/src/discovery/adapters/copilot.js b/src/discovery/adapters/copilot.js new file mode 100644 index 00000000..e6e13f75 --- /dev/null +++ b/src/discovery/adapters/copilot.js @@ -0,0 +1,116 @@ +import path from "node:path"; + +import { emptyInteractionSignals } from "../../interaction.js"; +import { attachToolResults, home, listDirs, parseJsonLine, readHeadLines, readJsonl, statOrNull } from "./shared.js"; + +/** + * GitHub Copilot CLI: ~/.copilot/session-state//events.jsonl + * + * Every line is an event `{ type, data, id, timestamp, parentId }`. The first is + * `session.start`, whose `data.context` carries `cwd`, `gitRoot` and `branch`. The recorded + * `repository` is a host-specific label (`owner/name`, or `org/project/repo` on Azure + * DevOps), not a git remote URL, so no remote is reported and association relies on the + * cwd tiers, as for Claude Code. + * + * `COPILOT_HOME` relocates the whole config dir. + * + * Sub-agent work (`parentToolCallId`) is a sidechain and is skipped. `user.message` events + * the harness injected itself (instruction discovery, skills, system notices, prompts + * delegated to sub-agents) carry a non-user `source` and are skipped too. + */ + +const HEADER_LINES = 5; +const EVENTS_FILE = "events.jsonl"; +const USER_SOURCES = new Set(["user", "autopilot-objective"]); + +export const name = "copilot"; + +export function storeRoot() { + return process.env.COPILOT_HOME + ? path.join(process.env.COPILOT_HOME, "session-state") + : home(".copilot", "session-state"); +} + +export function enumerate() { + const out = []; + for (const dir of listDirs(storeRoot())) { + const file = path.join(dir, EVENTS_FILE); + const stat = statOrNull(file); + if (!stat) continue; + out.push({ key: file, path: file, mtimeMs: stat.mtimeMs, bytes: stat.size }); + } + return out; +} + +export function classify(candidate) { + for (const line of readHeadLines(candidate.path, HEADER_LINES)) { + const entry = parseJsonLine(line); + if (!entry || entry.type !== "session.start") continue; + const data = entry.data || {}; + const context = data.context || {}; + if (!context.cwd) return null; + return { + id: data.sessionId || path.basename(path.dirname(candidate.path)), + cwd: context.cwd, + gitRoot: context.gitRoot || null, + gitBranch: context.branch || null, + remotes: [], + startedAt: data.startTime ? Date.parse(data.startTime) : candidate.mtimeMs, + model: data.selectedModel || null, + interactionSignals: emptyInteractionSignals(), + }; + } + return null; +} + +export function read(ref) { + const entries = readJsonl(ref.path); + const events = []; + let model = null; + + for (const entry of entries) { + const data = entry.data || {}; + switch (entry.type) { + case "session.start": + model = model || data.selectedModel || null; + break; + case "user.message": { + if (data.source && !USER_SOURCES.has(data.source)) break; + if (typeof data.content === "string" && data.content.trim()) { + events.push({ kind: "message", role: "user", text: data.content }); + } + break; + } + case "assistant.message": { + if (data.parentToolCallId) break; + model = model || data.model || null; + if (typeof data.content === "string" && data.content.trim()) { + events.push({ kind: "message", role: "assistant", text: data.content }); + } + for (const request of data.toolRequests || []) { + events.push({ + kind: "tool", + name: request.name, + input: request.arguments, + pendingId: request.toolCallId, + }); + } + break; + } + case "tool.execution_complete": { + if (data.parentToolCallId) break; + events.push({ + kind: "tool-result", + id: data.toolCallId, + result: data.result?.content ?? data.error?.message ?? data.error, + status: data.success === false ? "error" : "completed", + }); + break; + } + default: + break; + } + } + + return { events: attachToolResults(events), model }; +} diff --git a/src/discovery/index.js b/src/discovery/index.js index 82a116ed..ea9bc3cd 100644 --- a/src/discovery/index.js +++ b/src/discovery/index.js @@ -6,6 +6,7 @@ import * as pi from "./adapters/pi.js"; import * as grok from "./adapters/grok.js"; import * as opencode from "./adapters/opencode.js"; import * as hermes from "./adapters/hermes.js"; +import * as copilot from "./adapters/copilot.js"; import * as cursorCli from "./adapters/cursor-cli.js"; import * as cursorIde from "./adapters/cursor-ide.js"; @@ -27,6 +28,7 @@ export const ADAPTERS = Object.assign(Object.create(null), { grok, opencode, hermes, + copilot, cursor: cursorCli, "cursor-ide": cursorIde, }); diff --git a/src/discovery/remote/bundle.js b/src/discovery/remote/bundle.js index e04a7305..35c1ef82 100644 --- a/src/discovery/remote/bundle.js +++ b/src/discovery/remote/bundle.js @@ -32,6 +32,7 @@ export const PROBE_MANIFEST = [ "discovery/adapters/grok.js", "discovery/adapters/opencode.js", "discovery/adapters/hermes.js", + "discovery/adapters/copilot.js", "discovery/adapters/cursor-cli.js", "discovery/adapters/cursor-ide.js", "discovery/remote/frames.js", @@ -51,6 +52,7 @@ export const REMOTE_ENV_ALLOWLIST = [ "CLAUDE_CONFIG_DIR", "CODEX_HOME", "HERMES_HOME", + "COPILOT_HOME", "PI_CODING_AGENT_DIR", "PI_CODING_AGENT_SESSION_DIR", "BB_DATA_DIR", diff --git a/src/discovery/remote/probe.js b/src/discovery/remote/probe.js index 6aa41c51..bff7f576 100644 --- a/src/discovery/remote/probe.js +++ b/src/discovery/remote/probe.js @@ -9,6 +9,7 @@ import * as pi from "../adapters/pi.js"; import * as grok from "../adapters/grok.js"; import * as opencode from "../adapters/opencode.js"; import * as hermes from "../adapters/hermes.js"; +import * as copilot from "../adapters/copilot.js"; import * as cursorCli from "../adapters/cursor-cli.js"; import * as cursorIde from "../adapters/cursor-ide.js"; @@ -49,6 +50,7 @@ const ADAPTERS = Object.assign(Object.create(null), { grok, opencode, hermes, + copilot, cursor: cursorCli, "cursor-ide": cursorIde, }); diff --git a/src/harness-invoke.js b/src/harness-invoke.js index fd34fd92..3b4a8e12 100644 --- a/src/harness-invoke.js +++ b/src/harness-invoke.js @@ -84,6 +84,10 @@ export function prepareHarnessInvocation({ agent, model = null, effort = null, w : baseInvocation({ notes, dispose }); } else if (agent === "grok") { invocation = grokInvocation({ requestedModel, requestedEffort, writeAccess, notes, cleanups, dispose }); + } else if (agent === "copilot") { + invocation = overlay + ? copilotInvocation({ requestedModel, requestedEffort, notes, cleanups, dispose }) + : baseInvocation({ notes, dispose }); } else if (agent === "claude" || agent === "codex" || agent === "opencode") { invocation = { ...baseInvocation({ notes, dispose }), @@ -94,7 +98,7 @@ export function prepareHarnessInvocation({ agent, model = null, effort = null, w } else if (overlay) { throw new UserError( `${agent} has no proven invocation-scoped way to apply ${describeOverride(requestedModel, requestedEffort)} without writing persistent harness defaults`, - "pin pi, claude, codex, grok, or opencode, or omit the model and effort override", + "pin pi, claude, codex, copilot, grok, or opencode, or omit the model and effort override", ); } else { invocation = baseInvocation({ notes, dispose }); @@ -160,6 +164,32 @@ function piInvocation({ requestedModel, requestedEffort, notes, cleanups, dispos }; } +function copilotInvocation({ requestedModel, requestedEffort, notes, cleanups, dispose }) { + if (process.platform === "win32") { + throw new UserError( + `cannot apply ${describeOverride(requestedModel, requestedEffort)} through acpx --agent on Windows`, + "pin pi, claude, codex, or opencode, or omit the model and effort override", + ); + } + const extra = []; + if (requestedModel) extra.push("--model", requestedModel); + if (requestedEffort) extra.push("--reasoning-effort", requestedEffort); + extra.push("--acp", "--stdio"); + const real = resolveOnPath("copilot"); + if (!real) { + throw new UserError( + `cannot apply ${describeOverride(requestedModel, requestedEffort)} as copilot process flags because copilot was not found on PATH`, + "install the Copilot CLI, or pin a different agent / omit the model and effort override", + ); + } + const { nodeCommand, dir } = writeArgvWrapper({ realCommand: real, extraArgs: extra, binName: "copilot" }); + cleanups.push(() => fs.rmSync(dir, { recursive: true, force: true })); + return { + ...baseInvocation({ notes, dispose }), + acpxAgentCommand: nodeCommand, + }; +} + function grokInvocation({ requestedModel, requestedEffort, writeAccess = false, notes, cleanups, dispose }) { if (process.platform === "win32") { throw new UserError( diff --git a/src/tui/render.js b/src/tui/render.js index d1813b21..b9ce496f 100644 --- a/src/tui/render.js +++ b/src/tui/render.js @@ -34,6 +34,7 @@ const HARNESS_HUES = { cursor: "blue", "cursor-ide": "blue", hermes: "yellow", + copilot: "blue", }; /** diff --git a/test/adapters.test.js b/test/adapters.test.js index 7b2103b1..27e1aecc 100644 --- a/test/adapters.test.js +++ b/test/adapters.test.js @@ -9,6 +9,7 @@ import { execFileSync } from "node:child_process"; import * as claude from "../src/discovery/adapters/claude.js"; import * as codex from "../src/discovery/adapters/codex.js"; +import * as copilot from "../src/discovery/adapters/copilot.js"; import * as pi from "../src/discovery/adapters/pi.js"; import * as grok from "../src/discovery/adapters/grok.js"; import * as cursorCli from "../src/discovery/adapters/cursor-cli.js"; @@ -103,6 +104,60 @@ test("claude adapter enumerates the default store and CLAUDE_CONFIG_DIR, without assert.deepEqual(enumerateClaude(fakeHome, defaultRoot), ["personal.jsonl"]); }); +test("copilot adapter classifies a session from its session.start context", () => { + const file = path.join(FIXTURES, "copilot-session.jsonl"); + const descriptor = copilot.classify(candidateFor(file)); + + assert.equal(descriptor.id, "11111111-2222-4333-8444-555555555555"); + assert.equal(descriptor.cwd, "/work/demo"); + assert.equal(descriptor.gitRoot, "/work/demo"); + assert.equal(descriptor.gitBranch, "main"); + assert.equal(descriptor.model, "claude-sonnet-5.5"); + // The recorded repository is a host label, not a remote URL. + assert.deepEqual(descriptor.remotes, []); +}); + +test("copilot adapter keeps user turns, folds tool results, and drops injected and sub-agent events", () => { + const file = path.join(FIXTURES, "copilot-session.jsonl"); + const { events, model } = copilot.read({ path: file }); + + assert.equal(model, "claude-sonnet-5.5"); + assert.deepEqual( + messages(events).map((m) => `${m.role}: ${m.text}`), + [ + "user: Fix the failing build", + "assistant: Looking at the build output.", + "user: Use pnpm, not npm", + "assistant: Switching to pnpm.", + ], + ); + + const [toolCall] = tools(events); + assert.equal(toolCall.name, "bash"); + assert.equal(toolCall.input.command, "npm test"); + assert.equal(toolCall.result, "1 failing"); + assert.equal(toolCall.status, "error"); +}); + +test("copilot adapter enumerates events.jsonl per session and honors COPILOT_HOME", () => { + const previous = process.env.COPILOT_HOME; + const relocated = fs.mkdtempSync(path.join(os.tmpdir(), "backpass-copilot-home-")); + const sessionDir = path.join(relocated, "session-state", "abc"); + fs.mkdirSync(sessionDir, { recursive: true }); + fs.mkdirSync(path.join(relocated, "session-state", "empty"), { recursive: true }); + fs.copyFileSync(path.join(FIXTURES, "copilot-session.jsonl"), path.join(sessionDir, "events.jsonl")); + process.env.COPILOT_HOME = relocated; + try { + assert.deepEqual( + copilot.enumerate().map((c) => path.relative(relocated, c.path)), + [path.join("session-state", "abc", "events.jsonl")], + ); + } finally { + if (previous === undefined) delete process.env.COPILOT_HOME; + else process.env.COPILOT_HOME = previous; + } +}); + test("codex adapter honors CODEX_HOME", () => { const previous = process.env.CODEX_HOME; const relocated = fs.mkdtempSync(path.join(os.tmpdir(), "backpass-codex-home-")); diff --git a/test/config.test.js b/test/config.test.js index f7802a2c..eb524a74 100644 --- a/test/config.test.js +++ b/test/config.test.js @@ -30,7 +30,16 @@ test("the defaults match the approved design", () => { assert.equal(config.minGapEvidence, 2); assert.equal(config.jobs, 4); assert.equal(config.discovery.since, "30d"); - assert.deepEqual(config.discovery.harnesses, ["claude", "codex", "pi", "opencode", "grok", "cursor", "hermes"]); + assert.deepEqual(config.discovery.harnesses, [ + "claude", + "codex", + "pi", + "opencode", + "grok", + "cursor", + "hermes", + "copilot", + ]); assert.ok(!config.discovery.harnesses.includes("cursor-ide"), "Cursor IDE is deferred to v1.1"); assert.deepEqual(config.analysis, { agent: null, model: null, effort: null }, "agents are auto-picked by default"); assert.deepEqual(config.synthesis, { agent: null, model: null, effort: null }); diff --git a/test/fixtures/copilot-session.jsonl b/test/fixtures/copilot-session.jsonl new file mode 100644 index 00000000..c580f85f --- /dev/null +++ b/test/fixtures/copilot-session.jsonl @@ -0,0 +1,10 @@ +{"type":"session.start","data":{"sessionId":"11111111-2222-4333-8444-555555555555","version":1,"producer":"copilot-agent","copilotVersion":"1.0.106","startTime":"2026-11-01T09:00:00.000Z","selectedModel":"claude-sonnet-5.5","context":{"cwd":"/work/demo","gitRoot":"/work/demo","repository":"acme/demo","hostType":"github","branch":"main"}},"id":"e1","timestamp":"2026-11-01T09:00:00.100Z","parentId":null} +{"type":"user.message","data":{"content":"Instructions loaded from AGENTS.md","source":"instruction-discovery","turnId":"0"},"id":"e2","timestamp":"2026-11-01T09:00:01.000Z","parentId":"e1"} +{"type":"user.message","data":{"content":"Fix the failing build","transformedContent":"xFix the failing build","turnId":"0"},"id":"e3","timestamp":"2026-11-01T09:00:02.000Z","parentId":"e2"} +{"type":"assistant.message","data":{"messageId":"m1","model":"claude-sonnet-5.5","content":"Looking at the build output.","toolRequests":[{"toolCallId":"call_1","name":"bash","arguments":{"command":"npm test"}}],"turnId":"0"},"id":"e4","timestamp":"2026-11-01T09:00:03.000Z","parentId":"e3"} +{"type":"tool.execution_start","data":{"toolCallId":"call_1","toolName":"bash","arguments":{"command":"npm test"},"turnId":"0"},"id":"e5","timestamp":"2026-11-01T09:00:04.000Z","parentId":"e4"} +{"type":"tool.execution_complete","data":{"toolCallId":"call_1","success":false,"result":{"content":"1 failing"},"turnId":"0"},"id":"e6","timestamp":"2026-11-01T09:00:05.000Z","parentId":"e5"} +{"type":"user.message","data":{"content":"Delegated prompt for a sub-agent","source":"agent-abc","turnId":"1"},"id":"e7","timestamp":"2026-11-01T09:00:06.000Z","parentId":"e6"} +{"type":"assistant.message","data":{"messageId":"m2","model":"claude-haiku-4.5","content":"Sub-agent chatter","parentToolCallId":"call_task","toolRequests":[],"turnId":"1"},"id":"e8","timestamp":"2026-11-01T09:00:07.000Z","parentId":"e7"} +{"type":"user.message","data":{"content":"Use pnpm, not npm","source":"user","turnId":"2"},"id":"e9","timestamp":"2026-11-01T09:00:08.000Z","parentId":"e8"} +{"type":"assistant.message","data":{"messageId":"m3","model":"claude-sonnet-5.5","content":"Switching to pnpm.","toolRequests":[],"turnId":"2"},"id":"e10","timestamp":"2026-11-01T09:00:09.000Z","parentId":"e9"} diff --git a/test/harness-invoke-copilot.test.js b/test/harness-invoke-copilot.test.js new file mode 100644 index 00000000..b1f8f9ee --- /dev/null +++ b/test/harness-invoke-copilot.test.js @@ -0,0 +1,44 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import { spawnSync } from "node:child_process"; +import { prepareHarnessInvocation } from "../src/harness-invoke.js"; + +test( + "copilot model and effort reach the real binary as flags, ahead of the ACP stdio flags", + { skip: process.platform === "win32" }, + () => { + const bin = fs.mkdtempSync(path.join(os.tmpdir(), "bp-copilot-bin-")); + const log = path.join(bin, "args.log"); + fs.writeFileSync(path.join(bin, "copilot"), `#!/bin/sh\nfor a in "$@"; do echo "$a"; done > "${log}"\n`, { + mode: 0o755, + }); + const prevPath = process.env.PATH; + process.env.PATH = `${bin}${path.delimiter}${prevPath}`; + const inv = prepareHarnessInvocation({ agent: "copilot", model: "some-model", effort: "high" }); + try { + assert.ok(inv.acpxAgentCommand); + const run = spawnSync("/bin/sh", ["-c", inv.acpxAgentCommand], { encoding: "utf8" }); + assert.equal(run.status, 0, run.stderr); + assert.deepEqual(fs.readFileSync(log, "utf8").trim().split("\n"), [ + "--model", + "some-model", + "--reasoning-effort", + "high", + "--acp", + "--stdio", + ]); + } finally { + inv.dispose(); + process.env.PATH = prevPath; + fs.rmSync(bin, { recursive: true, force: true }); + } + }, +); + +test("copilot without overrides needs no wrapper", () => { + const inv = prepareHarnessInvocation({ agent: "copilot" }); + assert.equal(inv.acpxAgentCommand, null); +});