diff --git a/.release-please-manifest.json b/.release-please-manifest.json index dd32f2b9..8d16b2cf 100644 --- a/.release-please-manifest.json +++ b/.release-please-manifest.json @@ -1,3 +1,3 @@ { - ".": "0.1.32" + ".": "0.1.34" } diff --git a/AGENTS.md b/AGENTS.md index 22c14a5a..b123f53c 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -35,7 +35,7 @@ Each owning file's comments and tests hold the detail; read them before touching `src/proposal.js`; usage comes from acpx's stderr line or a harness store (`src/acpx.js`), printed only by `src/commands/usage.js`. - **Evidence floors live in `buildProposal`** (`src/proposal.js`): every non-`extract`/`move` edit needs - `minGapEvidence` distinct sessions from `summary.sources`; deletions need `harm`-class sessions; a pure + `minGapEvidence` distinct observers from fold-issued sources; deletions need `harm`-class observers; a pure deletion inside a skill file is refused. Extraction and deletion never share one decision. Never add a lexical or text-shape classifier to these gates, and non-compliance never counts as harm. The >= 20% relevance placement table (`src/prompts/synthesis.md`) stays prompt guidance, never a `buildProposal` gate. @@ -47,7 +47,7 @@ Each owning file's comments and tests hold the detail; read them before touching - **Corpus mix is interactive vs non-interactive, never an unknown bucket** (`src/interaction.js`). - **backpass must never analyze itself**: keep `SELF_SESSION_SENTINEL` (`src/prompts.js`) at the start of every model-facing prompt (`src/discovery/self.js` drops those sessions). -- **Gap evidence**: one ledger sighting per (gap, transcript) (`src/gap-ledger.js`); record before pruning; +- **Gap evidence**: native ledger contributions retain their own provenance (`src/gap-ledger.js`); record before pruning; never surface uncorroborated singletons; gap identity is judged by the consolidation call, with bigram similarity only as fallback; there is no orchestrator-memory write path. - **Fold uses only selected, fresh evidence** (`foldForRun` in `src/commands/propose.js`, `isEvidenceFresh`). @@ -80,7 +80,7 @@ Each owning file's comments and tests hold the detail; read them before touching - **The live TUI is an enhancement layer** (`src/tui/`): output must be identical with it inactive (non-TTY, CI, `NO_COLOR`, `--quiet`, `--json`), with clean stdout. - **Rejection identity** (`src/state.js`): a refused add/rewrite/remove stays suppressed on the same kind and file - when measured `gapIds` or `instructionIds` overlap, until a strictly higher session count; extract/move stay + when measured `gapIds` or `instructionIds` overlap, until a strictly higher observer count; extract/move stay hunk-key only. Optional reject reasons never revive an edit and are not evidence floors. - Cursor IDE support is deferred; see the header of `src/discovery/adapters/cursor-ide.js`. diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c0a5918..f39e0e00 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,19 @@ # Changelog +## [0.1.34](https://github.com/kunchenguid/backpass/compare/backpass-v0.1.33...backpass-v0.1.34) (2026-10-13) + + +### Bug Fixes + +* **discovery:** associate sessions in repos with separate git directories ([#206](https://github.com/kunchenguid/backpass/issues/206)) ([29c46e7](https://github.com/kunchenguid/backpass/commit/29c46e7209fbfd5a8739d343163b8250d67b14c6)) + +## [0.1.33](https://github.com/kunchenguid/backpass/compare/backpass-v0.1.32...backpass-v0.1.33) (2026-10-12) + + +### Features + +* **discovery:** add opt-in OMP session discovery to the Pi adapter ([#151](https://github.com/kunchenguid/backpass/issues/151)) ([9a4a773](https://github.com/kunchenguid/backpass/commit/9a4a773c0eaebfdb3ea4950a8a0f53d447ad8b60)) + ## [0.1.32](https://github.com/kunchenguid/backpass/compare/backpass-v0.1.31...backpass-v0.1.32) (2026-09-30) diff --git a/README.md b/README.md index b43fca31..e45973de 100644 --- a/README.md +++ b/README.md @@ -36,8 +36,8 @@ memory file and project skills - under a token budget, gated by you. machines except into an agent you already authenticated, and obvious secrets are redacted before they do. - **Evidence-gated** - Every proposed edit carries verbatim quotes from real sessions, - and every `add`, `rewrite`, or `remove` edit needs evidence from at least two distinct - sessions. Small, noisy, bounded steps - not a rewrite. + subject to the [corroboration and harm floors](#5-gradient-descent---native-edits). + Small, noisy, bounded steps - not a rewrite. - **Human in the loop** - Analysis never writes. See [Apply - the human gate](#8-apply---the-human-gate) for review and explicit scripted decisions. @@ -238,15 +238,15 @@ It cannot be combined with By default, backpass reads the local transcript stores of seven harnesses directly. No API, no upload. -| Harness | Store | Repo tie | -| -------------- | ---------------------------------------------- | --------------------------------------------------- | -| **claude** | `~/.claude/projects//.jsonl` | per-line `cwd` | -| **codex** | `~/.codex/sessions/YYYY/MM/DD/rollout-*.jsonl` | `cwd` + recorded `git.repository_url` | -| **pi** | standalone and BB-managed Pi JSONL stores | session-header `cwd` | -| **opencode** | `~/.local/share/opencode/opencode.db` (sqlite) | `session.directory` / `session_v2.directory` | -| **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 | +| Harness | Store | Repo tie | +| -------------- | ----------------------------------------------- | --------------------------------------------------- | +| **claude** | `~/.claude/projects//.jsonl` | per-line `cwd` | +| **codex** | `~/.codex/sessions/YYYY/MM/DD/rollout-*.jsonl` | `cwd` + recorded `git.repository_url` | +| **pi** | standalone, OMP, and BB-managed Pi JSONL stores | session-header `cwd` | +| **opencode** | `~/.local/share/opencode/opencode.db` (sqlite) | `session.directory` / `session_v2.directory` | +| **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 | 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 @@ -259,6 +259,33 @@ sessions under `~/.bb/pi-bridge-sessions/`. It also honors `PI_CODING_AGENT_DIR` set in backpass's environment. When roots overlap, backpass scans every applicable layout and reads each JSONL file once. +OMP (Oh My Pi) collection is **off by default**. Set `discovery.includeOmp` to `true` in +`.backpassrc.json` or your [personal config](#configuration) to also read +`~/.omp/agent/sessions/` through the Pi adapter: + +```json +{ "discovery": { "includeOmp": true } } +``` + +Then run `backpass scan --harness pi` (or a normal `backpass` run). The setting also +applies to configured SSH hosts. For `--scope user`, set it in `user.discovery` and +include `"pi"` in `user.discovery.harnesses`; user scope otherwise collects only Claude +and Codex. Explicit Pi store environment overrides still work without this setting. +It enables the additional default OMP store and its title/nested handling, not the +harness backpass invokes. +Without it, nothing OMP-specific is read: Pi stores (including `PI_CODING_AGENT_DIR` +and bridge directories) are listed one level deep and need a `session` header on line 1, +exactly as before. + +OMP nests subagent JSONL files below each parent session, and a subagent's own subagents +one level further down. Only the OMP store, with the setting on, is walked that deep and +may open with a `title` record; a `PI_CODING_AGENT_DIR` that points at the same directory +is treated as that store. Backpass analyzes each file separately. With a readable root +header, the session and its descendants share one corroborating observer, including across +synced SSH stores. If the root is unreadable, the highest readable ancestor is used; without one, the file retains +its own observer identity. Matching descendant copies link their families without merging +distinct roots on the same host that share a native ID. + OpenCode collection reads both store layouts: OpenCode 1.x (`session`, `message`, `part`) and OpenCode 2.x (`session_v2`, `session_message`). For 2.x, session activity uses the later of the session's update time and its newest message's update time. An upgraded store keeps its 1.x tables beside the copies in `session_v2`, so a session found in both is read from `session_v2`. @@ -325,8 +352,8 @@ whitespace is ignored; spans still point into the rendered event field. A quote overlaps text the distiller added (truncation markers, output-size notes, redaction placeholders) is rejected, and a reported raw-transcript read fails the analysis. -Collection is incremental. Codex alone can hold 10,000+ rollouts, so verdicts are cached in -`.backpass/scan-cache.json` by path, mtime and size - re-scans cost only the new files. +Collection is incremental: cached file-backed descriptors are reused when their inputs +are unchanged. The cache rules live in [`src/discovery/index.js`](src/discovery/index.js). A harness whose store is missing or has drifted into an unrecognised shape produces a warning and is skipped; the run continues. backpass's own loss and gradient-descent calls land in these same stores under the repo's cwd; every prompt it sends is tagged, and tagged sessions are excluded from the corpus (the `SELF` column in `backpass scan`). @@ -335,12 +362,15 @@ OpenCode sessions with no recorded messages, such as unused agent probes, are no Every remaining session is labelled **interactive** or **non-interactive** (`src/interaction.js`). Codex `codex exec` / `originator: codex_exec`, Claude SDK, GitHub, action, and CI -entrypoints, OpenCode child sessions (`parent_id`), and a cwd with a `.no-mistakes` path -segment are non-interactive. Hermes gateway, cron, and WhatsApp sessions are classified the -same way if they leak past collection's source filter. A no-mistakes pipeline run is just one -kind of non-interactive session, not its own category. Missing harness metadata defaults to -interactive. `backpass scan`, the proposal, and apply all print the mix so relevance is never -silently computed against a robot-skewed pool. +entrypoints, OpenCode child sessions (`parent_id`), OMP subagent transcripts, and a cwd +with a `.no-mistakes` path segment are non-interactive. Hermes gateway, cron, and WhatsApp +sessions are classified the same way if they leak past collection's source filter. A +no-mistakes pipeline run is just one kind of non-interactive session, not its own category. +Missing harness metadata defaults to interactive. `backpass scan`, the proposal, and apply +all print the mix so relevance is never silently computed against a robot-skewed pool. +Standalone `backpass propose` uses current discovery's interaction signals and category +for admitted cached evidence. Records without a stored category remain excluded until +analysis backfills them. ```sh backpass scan --since 7d --strict @@ -437,16 +467,16 @@ Per-transcript failures, such as timeouts or unparseable output, still let the r ### 4. Aggregate gradients - and one judged consolidation call Evidence is grouped by instruction, giving each one a positive/negative count, a count of -distinct sessions with harm-class negatives, and a **relevance** figure: the share of +distinct observers with harm-class negatives, and a **relevance** figure: the share of analyzed sessions in which it mattered at all. Relevance is reported both overall and separately for interactive and non-interactive sessions. The fold also reports memory-file units that substantially overlap a project skill. An overlap with a skill description duplicates always-loaded tokens and points the shrink at the memory-file copy; an overlap with a triggered skill body is placement evidence only, since the memory copy may be the only always-loaded coverage. Neither kind is deleted automatically. Duplicate gaps across -sessions are clustered, and only clusters seen in at least `minGapEvidence` sessions -(default 2) are eligible for synthesis. Mixed-domain clusters below that floor remain -visible as report-only diagnostics. One bad session never rewrites the weights. +sessions are clustered; only clusters meeting the [evidence floor](#5-gradient-descent---native-edits) +are eligible for synthesis. Mixed-domain clusters below that floor remain visible as +report-only diagnostics. One bad session never rewrites the weights. Whether two sightings are one gap is a judgment call, not a word-overlap score - models paraphrase, and a paraphrase that fails a lexical match would hide real recurrence. @@ -456,8 +486,9 @@ consolidation call sees the full open gap set and merges entries that describe t mistake. That second judgment is what lets two sightings of a brand-new gap in the same run's parallel fan-out corroborate. A failed consolidation call degrades the run to lexical identity and says so; it never aborts. All sightings cluster before their domain -is decided. Each sighting votes `project` or `orchestration`; only a majority-orchestration -cluster is excluded from synthesis, while a tie stays eligible. Mixed clusters are always +is decided. Each corroborating observer contributes one `project` or `orchestration` vote; +related sightings share a vote, with any project sighting making it project. Only a +majority-orchestration cluster is excluded from synthesis, while a tie stays eligible. Mixed clusters are always reported with their orchestration count, even below the evidence floor. A corroborated majority-excluded cluster, including a pure-orchestration cluster, remains clearly labeled as a report-only diagnostic rather than becoming an instruction in the project's memory @@ -471,11 +502,11 @@ on disk. That file is left untouched, but it does not count toward this run's se or instruction scores, or add a gap observation, until ordinary discovery and analysis select and refresh it. -Gap sightings persist across runs in `.backpass/gap-ledger.json` by gap and session, but a -run only counts observations whose sessions belong to its selected sample. This prevents -older observations outside the cap from undoing the sample mix while still allowing -corroboration across runs when those sessions remain selected. The same session never counts -twice, a sighting retires once the memory file or a project skill covers it, and a session's +Gap sightings persist across runs in `.backpass/gap-ledger.json`; identity and provenance +rules live in [`src/gap-ledger.js`](src/gap-ledger.js). A run only counts observations +admitted under observers in its selected sample. Observers outside the cap cannot undo +the sample mix, while selected observers can corroborate across runs. Each observer +counts once per gap, a sighting retires once the memory file or a project skill covers it, and a session's sightings expire after `gapLedgerMaxAge` (default 90d). Until a gap corroborates it stays out of the proposal entirely. @@ -500,15 +531,17 @@ Then mechanical gates run, and they are not negotiable: - every measured change belongs to exactly one annotated edit - an unexplained change is a violation, so is an edit that names no change - every edit that changes the always-loaded surface - adding, rewriting or removing text - - needs quotes from `minGapEvidence` distinct sessions. The count is measured from the - edit's own quote sources, and only source labels issued by this run's fold count. A - mistyped or invented label does not create another session, and a session count the model - reports is ignored. Rewrites are not + needs quotes from `minGapEvidence` distinct corroborating observers (default 2). The + count is measured from the edit's own quote sources: only labels issued by this run's + fold count, and related transcripts share the observer described under + [collection](#1-collect-samples---which-sessions-belong-to-this-repo). + A mistyped or invented label does not create another observer; model-reported counts + are ignored. Rewrites are not classified by shape: a one-session tightening is refused along with a one-session append, because deciding which is which is a question about meaning that a line diff cannot answer. `extract` and `move` are exempt - they keep every always-loaded line. - removing a memory-file instruction outright needs harm-class negatives from - `minGapEvidence` distinct sessions - non-compliance never counts, because a rule that + `minGapEvidence` distinct observers - non-compliance never counts, because a rule that was skipped needs reinforcement, not deletion. A pure deletion in a skill file is also a removal, but no evidence can attribute harm to skill-file text, so it is refused. - an extraction preserves every line it removes in the skills it creates or extends, and @@ -599,7 +632,7 @@ Skill descriptions are weights too. If the evidence shows an agent lacked knowle skill already contains, that is a _failed trigger_ - backpass proposes a description edit, not duplicate content. Failed triggers are counted, not guessed: the analysis stage sees every skill's name and trigger line, stamps a gap it judges covered by an existing skill -with `coveredBySkill`, and the gap ledger corroborates those citations across sessions +with `coveredBySkill`, and the gap ledger corroborates those citations across observers before synthesis is told to fix the trigger. The same coverage check retires a ledger gap once a skill's content answers it, so a gap resolved by extraction stops resurfacing. @@ -627,8 +660,8 @@ a headless box or `--no-open` just hands you the link. There is no DEFER button, and it isn't missing: **rejections are remembered.** A rejected add, rewrite, or remove stays rejected while a later proposal of the same kind against the same file cites the same gap or touches the same instruction, even if the wording changed, -until a later run has strictly more sessions behind it. Extract and move still match on the -hunk bytes. The browser surface can attach an optional reason (`wrong-evidence`, +until a later run has strictly more corroborating observers behind it. Extract and move +still match on the hunk bytes. The browser surface can attach an optional reason (`wrong-evidence`, `already-covered`, `too-narrow`, `too-broad`, `disagree`); `--no-ui` records the rejection without asking for one. @@ -737,8 +770,9 @@ weight of its own; a missing file is reported, never created: into one. - **One review.** The proposal and `backpass apply` cover every file, each edit labeled with its file and each nested file with its own budget. Apply refuses a saved nested - edit if that file is no longer named in the current config. The two-session evidence - floor, remembered rejections, and `apply` as the only writer are unchanged. + edit if that file is no longer named in the current config. The + [evidence floors](#5-gradient-descent---native-edits), remembered rejections, and + `apply` as the only writer are unchanged. Each nested directory keeps the pointer model above: name its canonical file, and a sibling `AGENTS.md` or `CLAUDE.md` that only imports it needs nothing, while a sibling @@ -866,6 +900,7 @@ CLI flags on top: "since": "30d", "worktreeGlobs": [], "cloneRoots": [], + "includeOmp": false, "minUserTurns": 2 }, "jobs": 4 @@ -938,7 +973,7 @@ exclude (`.git/info/exclude`, written by `backpass init`) rather than the tracke ``` .backpass/ - scan-cache.json collect-samples verdicts by path + mtime + size + scan-cache.json cached collect-samples descriptors (see Collect samples) evidence/.json per-transcript loss evidence-summary.json aggregated gradients proposal.json the latest parseable gradient-descent step (absent if none was produced) @@ -946,7 +981,7 @@ exclude (`.git/info/exclude`, written by `backpass init`) rather than the tracke prompts/ the exact prompts of the last run agent-probe-cache.json which harnesses were available and logged in, and when rejections.json edits you turned down, and the evidence behind them - gap-ledger.json gap sightings by gap and session, accumulated across runs + gap-ledger.json durable gap sightings (see Aggregate gradients) hosts/ transcripts fetched from SSH hosts (mode 0700), pruned after 30 days unused raw/ temporary session events (see Distill) apply/apply.html the rendered review surface @@ -959,7 +994,7 @@ For the user-scope state location and isolation contract, see - **Causal attribution is genuinely hard.** A model can confabulate influence. The mitigations are structural - mandatory verbatim quotes checked against the trace they - claim to come from, the two-session rule, negative + claim to come from, the [corroboration floor](#5-gradient-descent---native-edits), negative evidence weighted highest, and a human gate - but read the evidence, not just the title. - **Transcript formats are undocumented** and can change without notice. Each adapter is pinned by a golden fixture and fails soft. diff --git a/docs/upstream-sync-2026-10-15.md b/docs/upstream-sync-2026-10-15.md new file mode 100644 index 00000000..8efe74af --- /dev/null +++ b/docs/upstream-sync-2026-10-15.md @@ -0,0 +1,57 @@ +# Backpass upstream sync: integration addendum + +The sync retains merge commit `731d6808517bbfee64d7606b72f1ff64ed2f2c67` and upstream +`e6a030a550be438e5b81a1ba91e01abc90faad07` as ancestry. Incoming changes add opt-in OMP +collection and observer deduplication, separate-git-directory discovery, and upstream +release metadata. House external-source, trace-only, private-run, credential-route, +SSH, and nested-memory capabilities remain required. + +The five content-conflict resolutions combine the following behavior: + +| File | Integration verdict | +| ------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `README.md` | Document upstream OMP collection and shared observers alongside the house evidence and scope rules. | +| `src/commands/propose.js` | Use current discovery's interaction metadata after house freshness admission; resolve current observers in the filtered view before consolidation, preserving durable history. | +| `src/discovery/index.js` | Retain house remote and external discovery paths while adding upstream OMP metadata, ancestor cache dependencies, and observer aliases across synced stores. | +| `src/fold.js` | Accept upstream observer deduplication for corroboration, harm, domain votes, and skill citations; retain eligible native projects, maximum risk, and nested ownership. | +| `src/gap-ledger.js` | Retain durable house provenance and freshness isolation while resolving current upstream shared observers in filtered views. Distinct native contributions and incompatible freshness stamps stay separately stored. | + +Native transcript identities drive direct and persisted ownership, representative +quotes, root ownership, and proposal routing. A child working only in `apps/api` can +support that file even when its observer parent worked at the root. Related native +contributions retain their individual projects and coverage status: coverage in one +project cannot suppress an uncovered contribution in another. Counter definitions and +coverage eligibility are owned by [`src/fold.js`](../src/fold.js). Representative quotes carry +both observer and project witnesses through the proposal floors. Instruction and gap +quotes use the same bounded selection rule, scaled to the configured evidence and +project floors. Synthesis and report rendering emit that selected set without a second +truncation, so an independent witness cannot disappear between storage and the prompt. + +Ledger migration and recording had also combined a child's historical wording with +its newly readable parent's source and content stamps. Recording, normalization, +and consolidation now combine only compatible contributions and retain their maximum +recurrence risk with the same reducer used by direct folding. Filtering checks the +original native source and stamps; stale wording stays durable without entering the +current model view. Source, external revision, and evidence-route boundaries remain +in force. The unsafe combined storage representation is replaced; no house capability +is retired. + +The filtered ledger view resolves current observer identities before consolidation, +including when a parent header disappears and later returns while a child's analysis +is stale. This changes only the view's observer attribution; durable source identities, +content digests, revisions, evidence keys, and first-seen times remain intact. Analysis, +consolidation, and fold share the filtered-view boundary, while direct folds still +resolve supplied observations against current records. + +The clean proposal overlap keeps observer-based evidence floors and native-based +ownership as separate maps. No new proposal gate or text classifier is introduced. +Remote-discovery, analysis-reuse, and interaction regressions assert individual native +provenance and a single related observer vote instead of one storage row per observer. +Behavioral regressions also cover project eligibility in both coverage directions, +contribution order, maximum risk, project-floor proposal acceptance, quote limits, +root ownership across nested passes, and parent-header restoration before consolidation. +Emitted instruction and gap evidence is fed into the real proposal gates at default and +higher evidence/project floors. Persisted recording, normalization, and consolidation +regressions cover high/low ordering and exclude stale high-risk contributions. +These are offline synthetic checks, with no account-integration claim. The outer +executor owns the full check, final PR conflict report, and house CI. diff --git a/package.json b/package.json index cc80e2cd..136a47c1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "backpass", - "version": "0.1.32", + "version": "0.1.34", "packageManager": "pnpm@11.5.0", "description": "Gradient descent for your agent memory - analyzes past agent session transcripts and proposes evidence-backed edits to AGENTS.md / CLAUDE.md", "type": "module", diff --git a/src/analyze.js b/src/analyze.js index 28ae862c..b1adfdc4 100644 --- a/src/analyze.js +++ b/src/analyze.js @@ -14,7 +14,7 @@ import { assertPrivatePath, evidenceKey, isEvidenceFresh, safeFileName } from ". import { analysisRoute, routeForPick } from "./provenance.js"; import { emitProgress } from "./progress.js"; import { UserError, color, info, warn } from "./logger.js"; -import { transcriptIdentity } from "./transcript.js"; +import { corroborationIdentityOf, transcriptIdentity } from "./transcript.js"; /** * Stage 1 of the pipeline (design section 3): one cheap model call per transcript, @@ -511,6 +511,10 @@ export async function analyzeTranscripts({ harness: transcript.harness, id: transcript.id, identity: transcriptIdentity(transcript), + parentSessionId: transcript.parentSessionId || null, + corroborationIdentity: corroborationIdentityOf(transcript), + corroborationNativeId: transcript.corroborationNativeId || null, + corroborationStartedAt: transcript.corroborationStartedAt ?? null, path: transcript.path, mtimeMs: transcript.mtimeMs, bytes: transcript.bytes, diff --git a/src/commands/propose.js b/src/commands/propose.js index 29b4c6a7..3e4b1174 100644 --- a/src/commands/propose.js +++ b/src/commands/propose.js @@ -4,12 +4,13 @@ import { filterGapLedger, ledgerGapObservations, mergeGapEntries, + normalizeGapLedgerSessions, pruneGapLedger, recordGapObservations, } from "../gap-ledger.js"; import { synthesizeProposal } from "../synthesize.js"; import { ProposalViolation } from "../proposal.js"; -import { formatCorpusMix, INTERACTIVE, NON_INTERACTIVE } from "../interaction.js"; +import { classifyInteraction, formatCorpusMix, INTERACTIVE, NON_INTERACTIVE } from "../interaction.js"; import { UserError, color, info, json, out, terminalSafe } from "../logger.js"; import { budgetBar, formatTokens } from "../tokens.js"; import { emitProgress } from "../progress.js"; @@ -19,7 +20,7 @@ import { printUsage } from "./usage.js"; import { closeRemoteDiscovery, discoverForRun } from "./scan.js"; import { capTranscripts } from "../sample.js"; import { isEvidenceFresh } from "../state.js"; -import { transcriptIdentity } from "../transcript.js"; +import { corroborationIdentityOf, transcriptIdentity } from "../transcript.js"; import { pruneHostCache } from "../discovery/cache.js"; import { analysisRoute, assertSourceCurrent, proposalProvenance, routeForPick } from "../provenance.js"; import { checkRunContext } from "../run-context.js"; @@ -37,6 +38,9 @@ import { checkRunContext } from "../run-context.js"; * cap remain on disk. Folding those records would inflate `analyzedSessions` beyond the * sampled corpus or score positional instruction aliases against an index they never saw. * Legacy records stay excluded until ordinary discovery and analysis backfill them. + * Current discovery's interaction and corroboration fields are overlaid only after + * admission, so stale stamps and observer identities from missing ancestors do not + * survive in the fold. */ export async function foldForRun(ctx, memoryFile, memoryHash, skills = [], transcripts = [], { route = null } = {}) { const { state, minGapEvidence, gapLedgerMaxAge } = ctx.config; @@ -45,7 +49,6 @@ export async function foldForRun(ctx, memoryFile, memoryHash, skills = [], trans ctx.sessionSource || transcripts.some((transcript) => transcript.sourceKind === "external") ? await analysisRoute(ctx.config) : null; - const selected = new Set(selectedByIdentity.keys()); const evidence = state.listEvidence(); const identitiesByLegacyId = new Map(); for (const record of evidence) { @@ -54,30 +57,53 @@ export async function foldForRun(ctx, memoryFile, memoryHash, skills = [], trans if (!identitiesByLegacyId.has(legacyId)) identitiesByLegacyId.set(legacyId, new Set()); identitiesByLegacyId.get(legacyId).add(transcriptIdentity(record.transcript)); } - const selectedGapSessions = new Set(selected); + const selectedGapSessions = new Set(); + const selectedObserversByNative = new Map(); + const legacyIds = new Set(); for (const transcript of transcripts) { + selectedGapSessions.add(corroborationIdentityOf(transcript)); + const nativeIdentity = transcript.identity || transcript.id; + if (!selectedObserversByNative.has(nativeIdentity)) selectedObserversByNative.set(nativeIdentity, new Set()); + selectedObserversByNative.get(nativeIdentity).add(corroborationIdentityOf(transcript)); const identities = identitiesByLegacyId.get(transcript.id); if (identities?.size === 1 && identities.has(transcriptIdentity(transcript))) { - selectedGapSessions.add(transcript.id); + legacyIds.add(transcript.id); + if (!selectedObserversByNative.has(transcript.id)) selectedObserversByNative.set(transcript.id, new Set()); + selectedObserversByNative.get(transcript.id).add(corroborationIdentityOf(transcript)); } } - const relevant = evidence.filter((e) => { - const currentTranscript = selectedByIdentity.get(transcriptIdentity(e.transcript)); - return ( - e.memoryPath === memoryFile.path && - e.memoryHash === memoryHash && - (e.transcript?.interaction === INTERACTIVE || e.transcript?.interaction === NON_INTERACTIVE) && - currentTranscript && - isEvidenceFresh(e, currentTranscript, memoryHash, evidenceRoute) - ); - }); + const relevant = []; + for (const record of evidence) { + const currentTranscript = selectedByIdentity.get(transcriptIdentity(record.transcript)); + if ( + record.memoryPath !== memoryFile.path || + record.memoryHash !== memoryHash || + (record.transcript?.interaction !== INTERACTIVE && record.transcript?.interaction !== NON_INTERACTIVE) || + !currentTranscript || + !isEvidenceFresh(record, currentTranscript, memoryHash, evidenceRoute) + ) { + continue; + } + const transcript = { + ...record.transcript, + interactionSignals: currentTranscript.interactionSignals, + interaction: classifyInteraction(currentTranscript), + parentSessionId: currentTranscript.parentSessionId || null, + corroborationIdentity: corroborationIdentityOf(currentTranscript), + corroborationNativeId: currentTranscript.corroborationNativeId || null, + corroborationStartedAt: currentTranscript.corroborationStartedAt ?? null, + }; + relevant.push({ ...record, transcript }); + } const traceOnly = Boolean(ctx.sessionSource) || transcripts.some((transcript) => transcript.sourceKind === "external"); const ledger = state.readGapLedger(); - recordGapObservations(ledger, relevant, { skills }); + normalizeGapLedgerSessions(ledger, transcripts, { legacyIds }); + recordGapObservations(ledger, relevant, { skills, legacyIds, transcripts }); const gapView = filterGapLedger(structuredClone(ledger), relevant, transcripts, { sourceKind: traceOnly ? "external" : "native", + legacyIds, }); // Consolidate after recording, so the pass sees this run's sightings too: two // sessions coining the same brand-new gap in one parallel fan-out can only line up @@ -99,9 +125,11 @@ export async function foldForRun(ctx, memoryFile, memoryHash, skills = [], trans } state.writeGapLedger(ledger); - const gapObservations = ledgerGapObservations(gapView, memoryFile.path, skills).filter((observation) => - selectedGapSessions.has(observation.sessionId), - ); + const gapObservations = ledgerGapObservations(gapView, memoryFile.path, skills).filter((observation) => { + if (!selectedGapSessions.has(observation.sessionId)) return false; + const observers = selectedObserversByNative.get(observation.sourceSessionId); + return !observers || (observers.size === 1 && observers.has(observation.sessionId)); + }); const summary = foldEvidence(relevant, { minGapEvidence, minGapProjects: ctx.scope?.kind === "user" ? ctx.config.minGapProjects || 1 : 0, diff --git a/src/commands/scan.js b/src/commands/scan.js index 44eed9a5..b56a100d 100644 --- a/src/commands/scan.js +++ b/src/commands/scan.js @@ -58,8 +58,16 @@ async function cmdScanCore(ctx) { mix, ...(runContext ? { runContext } : {}), transcripts: transcripts.map((transcript) => { + /** @type {Record} */ const serialized = { ...transcript }; delete serialized.remote; + // Observer fields appear only when related transcripts share one (OMP subagents), + // so a plain store serializes exactly as before. + if (serialized.corroborationIdentity === serialized.identity) { + delete serialized.corroborationIdentity; + delete serialized.corroborationNativeId; + delete serialized.corroborationStartedAt; + } return serialized; }), }); diff --git a/src/config.js b/src/config.js index a507217c..269bc2a5 100644 --- a/src/config.js +++ b/src/config.js @@ -69,8 +69,8 @@ export const DEFAULT_CONFIG = { maxEditsPerRun: null, minGapEvidence: 2, /** - * Gap observations persist across runs in `.backpass/gap-ledger.json`, but only - * observations from the current selected sample count toward `minGapEvidence`. + * Retirement age for durable gap sightings; identity and admission rules live in + * `src/gap-ledger.js`. * A session's observations retire past this age (a duration like 90d, `all` to never expire). */ gapLedgerMaxAge: "90d", @@ -119,6 +119,8 @@ export const DEFAULT_CONFIG = { * is refused by name. Absolute paths; `~` is expanded. */ opencodeStores: [], + /** Include Oh My Pi's default session store in Pi discovery; opt-in only. */ + includeOmp: false, minUserTurns: 2, includeCursorIde: false, }, @@ -473,6 +475,9 @@ function validate(config, { kind = "project", repoRoot = null } = {}) { } config.discovery.harnesses = config.discovery.harnesses.filter((h) => known.has(h)); parseSince(config.discovery.since); + if (typeof config.discovery.includeOmp !== "boolean") { + throw new UserError("config.discovery.includeOmp must be a boolean"); + } if (!Array.isArray(config.discovery.cloneRoots) || config.discovery.cloneRoots.some((p) => typeof p !== "string")) { throw new UserError("config.discovery.cloneRoots must be an array of paths"); } diff --git a/src/consolidate.js b/src/consolidate.js index 8a8b1f52..036bd08b 100644 --- a/src/consolidate.js +++ b/src/consolidate.js @@ -21,9 +21,9 @@ import { warn } from "./logger.js"; * paraphrase (measured on a production ledger: 89 entries containing at least six * multi-session gaps, highest cross-session score 0.34 against the 0.45 bar). The model * is asked only "same gap or not", the merge itself is mechanical (`mergeGapEntries`), - * unknown ids and malformed groups are dropped, and a session is never double-counted + * unknown ids and malformed groups are dropped, and an observer is never double-counted * however the entries merge. Strict admission is untouched: merging changes how - * sightings line up, never how many distinct sessions a proposal needs. + * sightings line up, never how many distinct observers a proposal needs. * * Exactly one call per run, and only when at least two entries exist. Failure is * fail-soft with a warning: the run degrades to lexical identity (the pre-consolidation @@ -38,7 +38,9 @@ let callCounter = 0; function renderEntries(entries) { return entries .map((entry) => { - const sessions = Object.keys(entry.sessions).length; + const sessions = new Set( + Object.entries(entry.sessions).map(([identity, observation]) => observation.observerId || identity), + ).size; const mistake = firstMistake(entry); return ( `[${entry.id}] (sessions=${sessions}) ${entry.proposedInstruction}` + diff --git a/src/discovery/adapters/pi.js b/src/discovery/adapters/pi.js index 82e5e1fc..68e2b733 100644 --- a/src/discovery/adapters/pi.js +++ b/src/discovery/adapters/pi.js @@ -16,16 +16,28 @@ import { /** * Pi writes standalone sessions under - * `~/.pi/agent/sessions//_.jsonl`. BB's Pi bridge writes the + * `~/.pi/agent/sessions//_.jsonl`. omp (Oh My Pi) uses the + * same JSONL shape under `~/.omp/agent/sessions/` (opt-in via `discovery.includeOmp`) + * and honors `PI_CODING_AGENT_DIR`, but + * prepends a fixed-width `{type:"title"}` record, so the `{type:"session", cwd, id}` + * entry is line 2 there. Only the opt-in OMP store gets the title and nested handling + * below; every other store is read one level deep with a line-1 header, as before. + * omp also writes subagent transcripts one level deeper, at + * `//.jsonl`, and their own subagents at + * `///..jsonl`. Ancestor headers establish + * observer provenance; nested layout alone marks automation. BB's Pi bridge writes the * same JSONL shape directly under `/pi-bridge-sessions/`. * - * Line 1 is `{type:"session", cwd, id}`. Entries form a parent/child tree but arrive in - * order, so a linear read is faithful. `model_change` / `thinking_level_change` records - * give the model actually used. No remote is recorded - dead worktrees reach tier 3 only. + * Entries form a parent/child tree but arrive in order, so a linear read is faithful. + * `model_change` / `thinking_level_change` records give the model actually used + * (`modelId` on pi, `model` on omp). No remote is recorded - dead worktrees reach tier 3 + * only. */ export const name = "pi"; +const SUBAGENT_DEPTH = 2; + export function storeRoot() { return home(".pi", "agent", "sessions"); } @@ -46,11 +58,14 @@ function realpathOrResolve(value) { } } -function storeSpecs() { +function storeSpecs(config) { const specs = [ { path: storeRoot(), direct: false, nested: true }, { path: home(".bb", "pi-bridge-sessions"), direct: true, nested: false }, ]; + if (config?.discovery?.includeOmp === true) { + specs.push({ path: home(".omp", "agent", "sessions"), direct: false, nested: true, omp: true }); + } const piAgentDir = expandEnvPath(process.env.PI_CODING_AGENT_DIR); if (piAgentDir) specs.push({ path: path.join(piAgentDir, "sessions"), direct: false, nested: true }); const piSessionDir = expandEnvPath(process.env.PI_CODING_AGENT_SESSION_DIR); @@ -67,6 +82,7 @@ function storeSpecs() { if (existing) { existing.direct ||= spec.direct; existing.nested ||= spec.nested; + existing.omp ||= spec.omp; } else { unique.set(key, spec); } @@ -74,17 +90,19 @@ function storeSpecs() { return [...unique.values()]; } -export function storeRoots() { - return storeSpecs().map((spec) => spec.path); +/** @param {{ discovery?: { includeOmp?: boolean } }} [config] */ +export function storeRoots(config) { + return storeSpecs(config).map((spec) => spec.path); } -export function enumerate() { +/** @param {{ config?: { discovery?: { includeOmp?: boolean } }, cutoffMs?: number | null, repo?: object }} [options] */ +export function enumerate({ config } = {}) { const out = []; const seen = new Set(); - for (const spec of storeSpecs()) { + for (const spec of storeSpecs(config)) { const files = [ ...(spec.direct ? listFiles(spec.path, ".jsonl") : []), - ...(spec.nested ? listDirs(spec.path).flatMap((dir) => listFiles(dir, ".jsonl")) : []), + ...(spec.nested ? listDirs(spec.path).flatMap((dir) => sessionFiles(dir, spec.omp ? SUBAGENT_DEPTH : 0)) : []), ]; for (const file of files) { const key = realpathOrResolve(file); @@ -92,35 +110,130 @@ export function enumerate() { seen.add(key); const stat = statOrNull(file); if (!stat) continue; - out.push({ key, path: file, mtimeMs: stat.mtimeMs, bytes: stat.size }); + out.push({ + key, + path: file, + mtimeMs: stat.mtimeMs, + bytes: stat.size, + omp: Boolean(spec.omp), + subagent: Boolean(spec.omp) && path.relative(spec.path, file).split(path.sep).length > 2, + }); } } return out; } -export function classify(candidate) { - const [first] = readHeadLines(candidate.path, 1); - const entry = first && parseJsonLine(first); +function sessionFiles(dir, depth) { + return [ + ...listFiles(dir, ".jsonl"), + ...(depth > 0 ? listDirs(dir).flatMap((sub) => sessionFiles(sub, depth - 1)) : []), + ]; +} + +export function createScanContext() { + return { parentHeaders: new Map() }; +} + +/** A leading `{type:"title"}` record is an omp trait; plain Pi stores read exactly as on main. */ +function readSessionHeader(file, { omp = false } = {}) { + const [firstLine, secondLine] = readHeadLines(file, omp ? 2 : 1); + const first = parseJsonLine(firstLine); + return first?.type === "session" ? first : omp && first?.type === "title" ? parseJsonLine(secondLine) : null; +} + +function readParentSession(parentPath, scanContext) { + const cache = scanContext?.parentHeaders; + if (cache?.has(parentPath)) return cache.get(parentPath); + const stat = statOrNull(parentPath); + const entry = stat?.isFile() ? readSessionHeader(parentPath, { omp: true }) : null; + const result = + entry?.type === "session" + ? { + entry, + stat, + fingerprint: JSON.stringify([ + stat.dev, + stat.ino, + stat.mtimeMs, + stat.ctimeMs, + stat.size, + entry.id ?? null, + entry.cwd ?? null, + entry.timestamp ?? null, + ]), + } + : null; + cache?.set(parentPath, result); + return result; +} + +function parentSessionPathFor(candidatePath) { + const sessionDir = path.dirname(candidatePath); + return path.join(path.dirname(sessionDir), `${path.basename(sessionDir)}.jsonl`); +} + +function ancestorSessionPaths(candidatePath) { + const out = []; + let current = candidatePath; + for (let depth = 0; depth < SUBAGENT_DEPTH; depth += 1) { + current = parentSessionPathFor(current); + out.push(current); + } + return out; +} + +/** @param {{ scanContext?: { parentHeaders: Map } }} [options] */ +export function cacheDependency(candidate, options = {}) { + // Plain Pi files depend on nothing but themselves. An omp file also depends on its + // ancestors, and carries a marker so a store first scanned without omp parsing + // (a title-led header reads as null there) is reclassified once omp is switched on. + if (!candidate.omp) return undefined; + return JSON.stringify( + candidate.subagent + ? ancestorSessionPaths(candidate.path).map( + (ancestorPath) => readParentSession(ancestorPath, options.scanContext)?.fingerprint ?? null, + ) + : [], + ); +} + +/** @param {{ scanContext?: { parentHeaders: Map } }} [options] */ +export function classify(candidate, options = {}) { + const { scanContext } = options; + const entry = readSessionHeader(candidate.path, { omp: candidate.omp }); if (!entry || entry.type !== "session" || !entry.cwd) return null; - return { + + const descriptor = { id: entry.id || path.basename(candidate.path, ".jsonl"), cwd: entry.cwd, gitBranch: null, remotes: [], startedAt: entry.timestamp ? Date.parse(entry.timestamp) : candidate.mtimeMs, model: null, - interactionSignals: emptyInteractionSignals(), + interactionSignals: candidate.subagent ? { source: "subagent" } : emptyInteractionSignals(), }; + + for (const ancestorPath of candidate.subagent ? ancestorSessionPaths(candidate.path) : []) { + const ancestorInfo = readParentSession(ancestorPath, scanContext); + const ancestor = ancestorInfo?.entry; + if (!ancestor) continue; + descriptor.parentSessionId = ancestor.id || path.basename(ancestorPath, ".jsonl"); + descriptor.parentSessionPath = ancestorPath; + descriptor.parentSessionStartedAt = ancestor.timestamp ? Date.parse(ancestor.timestamp) : ancestorInfo.stat.mtimeMs; + } + + return descriptor; } export function read(ref) { const entries = readJsonl(ref.path); const events = []; let model = null; + const omp = entries[0]?.type === "title"; for (const entry of entries) { if (entry.type === "model_change") { - model = entry.modelId || model; + model = entry.modelId || (omp && entry.model) || model; continue; } if (entry.type !== "message" || !entry.message) continue; diff --git a/src/discovery/hosts.js b/src/discovery/hosts.js index 7322a524..8fd53786 100644 --- a/src/discovery/hosts.js +++ b/src/discovery/hosts.js @@ -216,10 +216,10 @@ async function locate(entry, controlPath) { /** * Discover on every configured host, fail-soft per host. * - * @param {{ hosts: object[], harnesses: string[], cutoffMs: number | null, controlPath: string }} options + * @param {{ hosts: object[], harnesses: string[], cutoffMs: number | null, controlPath: string, includeOmp?: boolean }} options * @returns {Promise} one result per host, in configured order */ -export async function collectHosts({ hosts, harnesses, cutoffMs, controlPath }) { +export async function collectHosts({ hosts, harnesses, cutoffMs, controlPath, includeOmp = false }) { const results = []; for (const entry of hosts) { const result = emptyHostResult(entry); @@ -243,7 +243,7 @@ export async function collectHosts({ hosts, harnesses, cutoffMs, controlPath }) result.error = `failed to start ssh control master: ${masterFailure.message}`; } else { result.master = masterCall.master; - await collectOneHost(entry, { harnesses, cutoffMs }, result); + await collectOneHost(entry, { harnesses, cutoffMs, includeOmp }, result); } } catch (err) { if (err instanceof UserError) { @@ -272,7 +272,7 @@ export async function collectHosts({ hosts, harnesses, cutoffMs, controlPath }) return results; } -async function collectOneHost(entry, { harnesses, cutoffMs }, result) { +async function collectOneHost(entry, { harnesses, cutoffMs, includeOmp }, result) { const located = await locate(entry, result.master.controlPath); if (located.failure) { result.error = located.failure.message; @@ -318,7 +318,7 @@ async function collectOneHost(entry, { harnesses, cutoffMs }, result) { } const program = buildProbeProgram( - { protocol: PROTOCOL, op: "discover", harnesses: selected, cutoffMs }, + { protocol: PROTOCOL, op: "discover", harnesses: selected, cutoffMs, includeOmp }, { env: entry.env, }, diff --git a/src/discovery/index.js b/src/discovery/index.js index 6be82864..b8283a6c 100644 --- a/src/discovery/index.js +++ b/src/discovery/index.js @@ -41,9 +41,10 @@ export function getAdapter(harness) { * Discovery (design section 2). * * For file-backed stores the expensive step is reading each transcript's header, so - * results are memoised in `.backpass/scan-cache.json` keyed by path + mtime + size. - * Re-scans are then O(new files) - which matters: codex alone had 10,317 rollouts on - * the machine this was designed against. + * results are memoised in `.backpass/scan-cache.json` keyed by path + mtime + size. An + * adapter may also export `cacheDependency` (a fingerprint of other files a descriptor + * reads, such as OMP ancestor sessions); a mismatch reclassifies the entry. Unchanged + * entries avoid reclassification, but dependency fingerprints are checked per candidate. * * SQLite-backed stores (opencode, hermes, cursor IDE) query session metadata directly, * so they skip the file-header cache entirely. @@ -157,6 +158,7 @@ async function discoverNativeTranscripts({ hosts, harnesses: selected.filter((h) => getAdapter(h)), cutoffMs, + includeOmp: config.discovery.includeOmp, controlPath: createControlPath(), }); remoteMasters.push(...collected.map((result) => result.master).filter(Boolean)); @@ -362,6 +364,19 @@ function remoteTranscripts(result, entry, { scope, repo, config, strict, identit function dropCrossHostDuplicates(transcripts) { const seen = new Map(); const drops = new Map(); + const observerAliases = new Map(); + const observerCorrespondences = new Map(); + const canonicalObserver = (identity) => { + while (observerAliases.has(identity)) identity = observerAliases.get(identity); + return identity; + }; + const aliasObserver = (observer, retainedObserver) => { + const canonical = canonicalObserver(observer); + const retained = canonicalObserver(retainedObserver); + if (canonical !== retained) { + observerAliases.set(canonical, retained); + } + }; let write = 0; for (const transcript of transcripts) { const key = `${transcript.harness}\n${transcript.nativeId}`; @@ -369,13 +384,49 @@ function dropCrossHostDuplicates(transcripts) { if (previous && previous.host !== (transcript.host || null)) { const host = transcript.host || "local"; drops.set(host, (drops.get(host) || 0) + 1); + if ( + (transcript.parentSessionId || + (transcript.harness === "pi" && transcript.interactionSignals.source === "subagent")) && + (previous.parentSessionId || (previous.harness === "pi" && previous.interactionSignals.source === "subagent")) + ) { + aliasObserver(transcript.corroborationIdentity, previous.corroborationIdentity); + const observerKey = `${transcript.harness}\n${transcript.corroborationNativeId}`; + const retainedObserverKey = `${previous.harness}\n${previous.corroborationNativeId}`; + if (!observerCorrespondences.has(observerKey)) observerCorrespondences.set(observerKey, previous); + if (!observerCorrespondences.has(retainedObserverKey)) + observerCorrespondences.set(retainedObserverKey, previous); + } continue; } - if (!previous) seen.set(key, { host: transcript.host || null }); + if (!previous) seen.set(key, transcript); transcripts[write] = transcript; write += 1; } transcripts.length = write; + const observers = new Map(observerCorrespondences); + for (const transcript of transcripts) { + const key = `${transcript.harness}\n${transcript.corroborationNativeId}`; + const previous = observers.get(key); + if ( + !previous || + (!observerCorrespondences.has(key) && + previous.host === (transcript.host || null) && + !previous.parentSessionId && + transcript.parentSessionId) + ) { + observers.set(key, transcript); + } + } + for (const transcript of transcripts) { + const key = `${transcript.harness}\n${transcript.corroborationNativeId}`; + const previous = observers.get(key); + if (previous.host !== (transcript.host || null) && !observerAliases.has(transcript.corroborationIdentity)) { + aliasObserver(transcript.corroborationIdentity, previous.corroborationIdentity); + } + } + for (const transcript of transcripts) { + transcript.corroborationIdentity = canonicalObserver(transcript.corroborationIdentity); + } return drops; } @@ -425,8 +476,10 @@ function discoverFiles( const candidates = enumerateNative ? enumerateNative(adapter, { cutoffMs, repo, config }) : adapter.enumerate({ cutoffMs, repo, config }); + const scanContext = adapter.createScanContext?.(); const out = []; + const hasCacheDependency = typeof adapter.cacheDependency === "function"; for (const candidate of candidates) { if (cutoffMs && candidate.mtimeMs < cutoffMs) continue; stats.scanned += 1; @@ -443,19 +496,25 @@ function discoverFiles( const cacheKey = `${adapter.name}:${candidate.key}`; const cached = cache.entries[cacheKey]; + const cacheDependency = hasCacheDependency + ? adapter.cacheDependency(candidate, { repo, config, scanContext }) + : undefined; let descriptor; if ( cached && cached.mtimeMs === candidate.mtimeMs && cached.bytes === candidate.bytes && + (!hasCacheDependency || cached.cacheDependency === cacheDependency) && hasInteractionSignals(cached.descriptor) ) { stats.cached += 1; descriptor = cached.descriptor; } else { - descriptor = adapter.classify(candidate, { repo, config }) || null; - cache.entries[cacheKey] = { mtimeMs: candidate.mtimeMs, bytes: candidate.bytes, descriptor }; + descriptor = adapter.classify(candidate, { repo, config, scanContext }) || null; + const cacheEntry = { mtimeMs: candidate.mtimeMs, bytes: candidate.bytes, descriptor }; + if (hasCacheDependency) cacheEntry.cacheDependency = cacheDependency; + cache.entries[cacheKey] = cacheEntry; markDirty(); } @@ -517,6 +576,21 @@ function toTranscript(adapter, row, association, id, { host = null, remote = nul remote, }; transcript.identity = transcriptIdentity(transcript); + if (row.parentSessionId && row.parentSessionPath) { + transcript.parentSessionId = row.parentSessionId; + transcript.corroborationIdentity = transcriptIdentity({ + ...transcript, + identity: null, + nativeId: row.parentSessionId, + path: row.parentSessionPath, + }); + transcript.corroborationNativeId = row.parentSessionId; + transcript.corroborationStartedAt = row.parentSessionStartedAt ?? transcript.startedAt; + } else { + transcript.corroborationIdentity = transcript.identity; + transcript.corroborationNativeId = id; + transcript.corroborationStartedAt = transcript.startedAt; + } transcript.interaction = classifyInteraction(transcript); return transcript; } diff --git a/src/discovery/remote/probe.js b/src/discovery/remote/probe.js index 1d55f98f..6aa41c51 100644 --- a/src/discovery/remote/probe.js +++ b/src/discovery/remote/probe.js @@ -89,6 +89,9 @@ async function descriptorFrom(adapter, row, id) { remotes: Array.isArray(row.remotes) ? row.remotes : [], title: row.title || null, startedAt: row.startedAt || null, + parentSessionId: row.parentSessionId, + parentSessionPath: row.parentSessionPath, + parentSessionStartedAt: row.parentSessionStartedAt, mtimeMs: row.mtimeMs || 0, bytes: row.bytes || 0, contentSignature, @@ -101,7 +104,7 @@ async function descriptorFrom(adapter, row, id) { }; } -async function discoverHarness(adapter, { cutoffMs }) { +async function discoverHarness(adapter, { cutoffMs, includeOmp }) { const stats = { scanned: 0, classified: 0, self: 0, error: null }; const out = []; const warnings = []; @@ -124,10 +127,12 @@ async function discoverHarness(adapter, { cutoffMs }) { return { stats, descriptors: out, warnings }; } - for (const candidate of adapter.enumerate({ cutoffMs })) { + const candidates = adapter.enumerate({ cutoffMs, config: { discovery: { includeOmp } } }); + const scanContext = adapter.createScanContext?.(); + for (const candidate of candidates) { if (cutoffMs && candidate.mtimeMs < cutoffMs) continue; stats.scanned += 1; - const classified = adapter.classify(candidate); + const classified = adapter.classify(candidate, { scanContext }); if (!classified) continue; stats.classified += 1; const merged = { ...candidate, ...classified }; @@ -157,8 +162,8 @@ async function discoverHarness(adapter, { cutoffMs }) { return { stats, descriptors: out, warnings }; } -/** @param {{ harnesses?: string[], cutoffMs?: number | null }} request */ -export async function discover({ harnesses = [], cutoffMs = null } = {}) { +/** @param {{ harnesses?: string[], cutoffMs?: number | null, includeOmp?: boolean }} request */ +export async function discover({ harnesses = [], cutoffMs = null, includeOmp = false } = {}) { const harnessStats = Object.create(null); const descriptors = []; const warnings = []; @@ -176,7 +181,7 @@ export async function discover({ harnesses = [], cutoffMs = null } = {}) { continue; } try { - const result = await discoverHarness(adapter, { cutoffMs }); + const result = await discoverHarness(adapter, { cutoffMs, includeOmp }); harnessStats[harness] = result.stats; descriptors.push(...result.descriptors); warnings.push(...result.warnings); diff --git a/src/fold.js b/src/fold.js index d2c5557c..593bbb0b 100644 --- a/src/fold.js +++ b/src/fold.js @@ -9,9 +9,12 @@ import { GAP_SIMILARITY_THRESHOLD, disambiguateSourceLabels, gapSource, + highestRisk, normalizeSourceLabel, + resolveGapObservationObserver, } from "./gap-ledger.js"; import { crossSurfaceDuplicates } from "./overlap.js"; +import { corroborationIdentityOf } from "./transcript.js"; /** * Stage 2 of the pipeline (design section 3): fold per-transcript evidence into one @@ -25,15 +28,14 @@ import { crossSurfaceDuplicates } from "./overlap.js"; * non-interactive so a robot-heavy corpus cannot hide that an instruction only * fired in one category. That ratio is what decides memory-file vs skill * placement in section 7. - * 2. Near-duplicate gaps from different sessions are clustered, so "three sessions - * re-derived the db schema" arrives as one item with three quotes. Judged identity + * 2. Near-duplicate gaps are clustered with observer counts for corroboration, while + * native transcript identities retain ownership and project coverage. Judged identity * happens upstream (analysis citations and the consolidation pass reshape the - * ledger); the clustering here stays deterministic. Per-sighting `domain` votes - * travel with the cluster; the fold decides the cluster domain after grouping - * (majority orchestration is excluded from proposals; mixed clusters stay visible). + * ledger); the clustering here stays deterministic. Cluster domain is decided after + * grouping by `clusterDomainVote`. * 3. Gap clusters below `minGapEvidence` are ineligible for synthesis. Mixed-domain * clusters remain visible as report-only diagnostics; other under-floor clusters are - * dropped. Batch size > 1 means one bad session never rewrites the weights. Sessions + * dropped. Batch size > 1 means one bad session never rewrites the weights. Observers * are counted across runs, not per run: when the caller passes `gapObservations` (the * pruned gap ledger, `src/gap-ledger.js`) the clusters are built from those instead of * this run's records, so a gap seen once now and once on a later run graduates. Without @@ -103,7 +105,21 @@ export function foldEvidence( // without the project requirement, so a project-scoped run still has an allowlist. const sources = new Set(); const sourceProjects = {}; - const persistedObservations = gapObservations ?? []; + const observersByNative = new Map(); + for (const record of usable) { + const nativeIdentity = record.transcript.identity || record.transcript.id; + if (!observersByNative.has(nativeIdentity)) observersByNative.set(nativeIdentity, new Set()); + observersByNative.get(nativeIdentity).add(corroborationIdentityOf(record.transcript)); + } + const persistedObservations = (gapObservations ?? []) + .map((observation) => { + const sessionId = resolveGapObservationObserver(observation, observersByNative); + return sessionId && sessionId !== observation.sessionId ? { ...observation, sessionId } : observation; + }) + .filter((observation) => { + const observers = observersByNative.get(observation?.sourceSessionId); + return !observers || (observers.size === 1 && observers.has(observation?.sessionId)); + }); const issuedSources = disambiguateSourceLabels([ ...usable.map((record) => ({ source: gapSource(record.transcript), @@ -111,12 +127,14 @@ export function foldEvidence( })), ...persistedObservations.map((observation) => ({ source: observation?.source, - identity: observation?.sessionId, + identity: observation?.sourceSessionId || observation?.sessionId, })), ]); const recordSources = issuedSources.slice(0, usable.length); const observationSources = issuedSources.slice(usable.length); for (const [index, record] of usable.entries()) { + const sessionIdentity = record.transcript.identity || record.transcript.id; + const corroborationIdentity = corroborationIdentityOf(record.transcript); if (record.usedRawTranscript) usedRawCount += 1; const source = recordSources[index]; sources.add(source); @@ -126,7 +144,6 @@ export function foldEvidence( for (const item of record[polarity] || []) { const entry = touch(item.instruction); entry[polarity] += 1; - const sessionIdentity = record.transcript.identity || record.transcript.id; const category = classifyInteraction(record.transcript); entry.sessions.add(sessionIdentity); entry.sessionsByInteraction[category].add(sessionIdentity); @@ -138,9 +155,9 @@ export function foldEvidence( // `class` is what a negative means (harm vs non-compliance vs irrelevant); // `harmSessions` is what the removal-evidence floor counts. A record from // before the class existed carries none and never counts as harm. - if (polarity === "negative" && item.class === "harm") entry.harmSessions.add(sessionIdentity); + if (polarity === "negative" && item.class === "harm") entry.harmSessions.add(corroborationIdentity); if (polarity === "negative" && item.class === "non-compliance") { - entry.nonComplianceSessions.add(sessionIdentity); + entry.nonComplianceSessions.add(corroborationIdentity); } entry.quotes.push({ polarity, @@ -162,7 +179,9 @@ export function foldEvidence( quote: gap.quote, recurrenceRisk: gap.recurrenceRisk, source, - sessionId: record.transcript.identity || record.transcript.id, + sessionId: corroborationIdentity, + sourceSessionId: sessionIdentity, + sightingIds: [sessionIdentity], domain: gap.domain === "orchestration" ? "orchestration" : "project", project: record.transcript.project || null, projectRoot: record.transcript.projectRoot || null, @@ -171,11 +190,8 @@ export function foldEvidence( } } - // Orchestration-domain votes are mistakes caused not by this repository but by the - // external agent harness or tooling that orchestrated the session. They still cluster - // with project sightings of the same gap; a cluster is withheld from proposals only - // when a majority of its sightings vote orchestration. Mixed clusters stay visible - // so one inconsistent classifier call cannot drop a real recurrence below the floor. + // Domain voting is owned by `clusterDomainVote`; observations are never pre-filtered + // by domain, so one inconsistent classifier call cannot hide a real recurrence. const labeledObservations = gapObservations ? persistedObservations.map((observation, index) => ({ ...observation, @@ -205,6 +221,11 @@ export function foldEvidence( const parentHarmSessions = parentSessionCounts(memoryFile, instructions, "harmSessions"); const parentNonComplianceSessions = parentSessionCounts(memoryFile, instructions, "nonComplianceSessions"); + const sourceObservers = sourceIdentitiesOf(issuedSources, [ + ...usable.map((record) => [corroborationIdentityOf(record.transcript)]), + ...persistedObservations.map((observation) => [observation?.sessionId]), + ]); + const instructionRows = [...instructions.values()] .map((entry) => { const unit = findInstructionUnit(memoryFile, entry.instruction); @@ -233,7 +254,15 @@ export function foldEvidence( parentId: unit?.parentId ?? null, nonCompliance: entry.quotes.filter((q) => q.polarity === "negative" && q.class === "non-compliance").length, nonComplianceSessions: entry.nonComplianceSessions.size, - quotes: entry.quotes.slice(0, 6), + quotes: representativeItems( + entry.quotes.map((quote) => ({ + quote, + sessionId: sourceObservers[quote.source], + project: sourceProjects[quote.source], + })), + minGapEvidence, + minGapProjects, + ).map((item) => item.quote), ...(skillOverlap ? { skillOverlap } : {}), }; }) @@ -249,7 +278,7 @@ export function foldEvidence( projects: cluster.projects.size, projectCoveredSessions: cluster.projectCoveredSessions.size, recurrenceRisk: highestRisk(eligibleItems), - quotes: representativeGapItems(eligibleItems, route).map((i) => ({ + quotes: representativeGapItems(eligibleItems, route, minGapEvidence, minGapProjects).map((i) => ({ text: i.quote, effect: i.mistake, source: i.source, @@ -266,13 +295,17 @@ export function foldEvidence( const owners = route ? gapClusters.map((cluster, index) => - allDecided[index].failedTriggerSkill ? null : route.ownerOf([...cluster.sessions]), + allDecided[index].failedTriggerSkill + ? null + : route.ownerOf(nativeGapSightings(cluster.items).map((item) => item.sessionId)), ) : null; const routedGaps = []; const decided = route ? allDecided.filter((cluster, index) => { - const owner = rootOwnsGap(gapClusters[index].items, route.rootOwnedGaps) ? null : owners[index]; + const owner = rootOwnsGap(nativeGapSightings(gapClusters[index].items), route.rootOwnedGaps) + ? null + : owners[index]; if (owner === route.weight) return true; if (cluster.sessions >= minGapEvidence && !cluster.majorityOrchestration) { routedGaps.push({ @@ -322,7 +355,7 @@ export function foldEvidence( negative: negativeCount, // The missing-instruction lane's findings count: every sighting this fold clustered // over (the pruned ledger when one is passed, this run's records otherwise). - // Orchestration sightings are per-sighting votes; cluster domain is decided after grouping. + // Raw sighting totals are report-only; `clusterDomainVote` counts observers after grouping. gapSightings: allObservations.length, gapClusters: gaps.length, reportOnlyGapClusters: reportOnlyGaps.length, @@ -338,6 +371,7 @@ export function foldEvidence( instructions: instructionRows, sources: [...sources], sourceProjects, + sourceObservers, parentHarmSessions, gaps, crossSurfaceDuplicates: duplicates, @@ -348,24 +382,25 @@ export function foldEvidence( if (route.weight === null) { summary.rootOwnedGaps = gapClusters .filter((cluster, index) => owners[index] === null && cluster.sessions.size >= minGapEvidence) - .map((cluster) => cluster.items.map((item) => ({ sessionId: item.sessionId, quote: item.quote }))); + .map((cluster) => nativeGapSightings(cluster.items)); } summary.routedGaps = routedGaps; - summary.sourceSessions = sourceSessionsOf(issuedSources, usable, persistedObservations); + summary.sourceSessions = sourceIdentitiesOf(issuedSources, [ + ...usable.map((record) => [record.transcript.identity || record.transcript.id]), + ...persistedObservations.map((observation) => nativeSightingIds(observation)), + ]); } return summary; } -/** Which session each issued source label names, for the routing gate in `buildProposal`. */ -function sourceSessionsOf(issuedSources, usable, observations) { - const identities = [ - ...usable.map((record) => record.transcript.identity || record.transcript.id), - ...observations.map((observation) => observation?.sessionId), - ]; +function sourceIdentitiesOf(issuedSources, identities) { const out = {}; issuedSources.forEach((source, index) => { const label = normalizeSourceLabel(source); - if (label && identities[index] && !out[label]) out[label] = identities[index]; + if (!label) return; + const prior = out[label] ? [out[label]].flat() : []; + const combined = [...new Set([...prior, ...identities[index]].filter(Boolean))]; + if (combined.length) out[label] = combined.length === 1 ? combined[0] : combined; }); return out; } @@ -405,10 +440,10 @@ function oversizedRestructureTargets(memoryFile, nonComplianceSessions, minGapEv } /** - * Greedy similarity clustering over gap observations. A cluster counts each session once - * no matter how many observations it contributed (re-analysis, duplicate reports). - * A user-scope sighting the session's own project memory already covers is recorded but - * does not count toward `sessions` or `projects`. + * Greedy similarity clustering over gap observations. Eligible native contributions retain + * their projects and maximum risk, but each corroborating observer counts once in `sessions`. + * A user-scope sighting its own project memory already covers is recorded but does not count + * toward `sessions` or `projects`; uncovered relatives remain eligible. */ export function clusterGapObservations(observations, { checkProjectCoverage = false } = {}) { const clusters = []; @@ -424,6 +459,9 @@ export function clusterGapObservations(observations, { checkProjectCoverage = fa recurrenceRisk: obs.recurrenceRisk, source: obs.source, sessionId: obs.sessionId, + sourceSessionId: nativeSightingIds(obs)[0], + projectRoot: obs.projectRoot || null, + sightings: nativeSightingIds(obs).map((sessionId) => ({ sessionId, quote: obs.quote })), domain: observationDomain(obs), project: obs.project || null, projectCovered, @@ -431,8 +469,16 @@ export function clusterGapObservations(observations, { checkProjectCoverage = fa gapIds: new Set(observationGapIds(obs)), }; if (cluster) { - const sessionItem = cluster.items.find((candidate) => candidate.sessionId === obs.sessionId); + const sessionItem = cluster.items.find( + (candidate) => + candidate.sessionId === item.sessionId && + candidate.sourceSessionId === item.sourceSessionId && + candidate.project === item.project && + candidate.projectRoot === item.projectRoot, + ); if (sessionItem) { + sessionItem.sightings.push(...item.sightings); + sessionItem.recurrenceRisk = highestRisk([sessionItem, item]); if (obs.coveredBySkill) sessionItem.coveredBySkills.add(obs.coveredBySkill); if (observationDomain(obs) !== "orchestration") sessionItem.domain = "project"; if (projectCovered) sessionItem.projectCovered = true; @@ -537,25 +583,60 @@ function observationDomain(obs) { return obs?.domain === "orchestration" ? "orchestration" : "project"; } -function representativeGapItems(items, route) { - const selected = items.slice(0, 6); - if (!route || items.length <= 6) return selected; - const owner = route.ownerOf(items.map((item) => item.sessionId)); - if (route.ownerOf(selected.map((item) => item.sessionId)) === owner) return selected; - for (const item of items.slice(6)) { +function nativeSightingIds(observation) { + const identities = [...new Set([observation.sourceSessionId, ...(observation.sightingIds || [])].filter(Boolean))]; + return identities.length ? identities : [observation.sessionId]; +} + +function nativeGapSightings(items) { + return items.flatMap((item) => item.sightings); +} + +function representativeItems(items, minGapEvidence, minGapProjects) { + const observers = new Map(); + const projects = new Map(); + for (const item of items) { + if (!observers.has(item.sessionId)) observers.set(item.sessionId, item); + if (item.project && !projects.has(item.project)) projects.set(item.project, item); + } + const candidates = [ + ...new Set([...projects.values()].slice(0, minGapProjects).concat([...observers.values()], items)), + ]; + return candidates.slice(0, Math.max(6, minGapEvidence + Math.max(0, minGapProjects - 1))); +} + +function representativeGapItems(items, route, minGapEvidence, minGapProjects) { + const selected = representativeItems(items, minGapEvidence, minGapProjects); + if (!route || items.length <= selected.length) return selected; + const owner = route.ownerOf(nativeGapSightings(items).map((item) => item.sessionId)); + if (route.ownerOf(nativeGapSightings(selected).map((item) => item.sessionId)) === owner) return selected; + for (const item of items.filter((candidate) => !selected.includes(candidate))) { const candidate = [...selected.slice(0, -1), item]; - if (route.ownerOf(candidate.map((entry) => entry.sessionId)) === owner) return candidate; + if (route.ownerOf(nativeGapSightings(candidate).map((entry) => entry.sessionId)) === owner) { + const losesObserver = + new Set(candidate.map((entry) => entry.sessionId)).size < + new Set(selected.map((entry) => entry.sessionId)).size; + const losesProject = + new Set(candidate.map((entry) => entry.project).filter(Boolean)).size < + new Set(selected.map((entry) => entry.project).filter(Boolean)).size; + return losesObserver || losesProject ? [...selected, item] : candidate; + } } return selected; } /** - * Cluster domain is a majority of per-sighting votes, not a pre-filter. Ties (including - * 1 of 2) stay project so one inconsistent analysis call cannot kill a real recurrence. + * Cluster domain is a majority of observer votes, not a pre-filter. Related sightings + * share a vote with project precedence; ties stay project so one inconsistent analysis + * call cannot kill a real recurrence. */ function clusterDomainVote(items) { - const orchestrationSightings = items.filter((item) => item.domain === "orchestration").length; - const sightings = items.length; + const domains = new Map(); + for (const item of items) { + if (!domains.has(item.sessionId) || item.domain !== "orchestration") domains.set(item.sessionId, item.domain); + } + const orchestrationSightings = [...domains.values()].filter((domain) => domain === "orchestration").length; + const sightings = domains.size; return { orchestrationSightings, mixed: orchestrationSightings > 0 && orchestrationSightings < sightings, @@ -563,27 +644,22 @@ function clusterDomainVote(items) { }; } -function highestRisk(items) { - const order = { high: 3, medium: 2, low: 1 }; - return items.reduce((best, i) => (order[i.recurrenceRisk] > order[best] ? i.recurrenceRisk : best), "low"); -} - /** * The failed-trigger reading of a cluster: which existing skill the analyses judged to - * already cover this mistake, and in how many of the cluster's sessions. Items are one - * per session, so counting items counts sessions. The most-cited skill wins; a cluster - * nobody tied to a skill contributes nothing. + * already cover this mistake, and in how many of the cluster's observers. The most-cited + * skill wins; a cluster nobody tied to a skill contributes nothing. */ function failedTriggerOf(items, minGapEvidence) { const counts = new Map(); for (const item of items) { for (const skill of item.coveredBySkills) { - counts.set(skill, (counts.get(skill) || 0) + 1); + if (!counts.has(skill)) counts.set(skill, new Set()); + counts.get(skill).add(item.sessionId); } } let best = null; for (const [skill, sessions] of counts) { - if (!best || sessions > best.sessions) best = { skill, sessions }; + if (!best || sessions.size > best.sessions) best = { skill, sessions: sessions.size }; } return best && best.sessions >= minGapEvidence ? { failedTriggerSkill: best.skill, failedTriggerSessions: best.sessions } @@ -649,7 +725,7 @@ function renderEvidence(summary, { includeReportOnly }) { (row.known ? "" : " (id not found in current file - stale reference)"), ); if (row.skillOverlap) renderSkillOverlap(lines, row.skillOverlap); - for (const quote of row.quotes.slice(0, 3)) { + for (const quote of row.quotes) { const sign = quote.polarity === "negative" ? "-" : "+"; const cls = quote.polarity === "negative" ? ` [${quote.class ?? "unclassified"}]` : ""; const effect = quote.effect ? ` :: ${oneLine(quote.effect, 200)}` : ""; @@ -728,7 +804,7 @@ function renderEvidence(summary, { includeReportOnly }) { `line instead of duplicating its content in the memory file`, ); } - for (const quote of gap.quotes.slice(0, 3)) { + for (const quote of gap.quotes) { lines.push(` "${oneLine(quote.text)}" (${quote.source})`); } } diff --git a/src/gap-ledger.js b/src/gap-ledger.js index b1f45259..65885fad 100644 --- a/src/gap-ledger.js +++ b/src/gap-ledger.js @@ -1,18 +1,18 @@ import { parseMemoryUnits, similarity } from "./memory.js"; import { parseSince } from "./config.js"; import { sha256 } from "./state.js"; +import { corroborationIdentityOf } from "./transcript.js"; /** * Durable gap corroboration across runs (`.backpass/gap-ledger.json`). * - * The fold stage only promotes a gap once `minGapEvidence` distinct sessions report it. - * Counting those sessions from the evidence that happens to be on disk is lossy: a + * The fold stage only promotes a gap once `minGapEvidence` distinct observers report it. + * Counting those observers from the evidence that happens to be on disk is lossy: a * transcript's evidence file is rewritten every time it is re-analyzed against a changed * memory file (every apply changes the hash), and the analysis model rephrases gaps * between runs, so two observations of one gap rarely line up in a single fold. This - * ledger keeps every gap observation, keyed by gap identity and session, so observations - * collected on different runs can still reach the bar when their sessions are together in - * a later run's selected sample. + * ledger preserves gap history across runs, so observations collected separately can + * still reach the bar when their observers are together in a later run's selected sample. * * Identity and freshness rules: * @@ -30,17 +30,21 @@ import { sha256 } from "./state.js"; * - Every observation carries the `domain` the analysis judged: `orchestration` when the * mistake was not caused by this repository but by an external agent harness or tooling * that orchestrated the task, `project` for every other mistake. - * Each observation stores that vote. The fold clusters first and decides domain - * afterward (majority orchestration withholds a cluster from proposals; a mixed - * cluster stays visible). A missing domain counts as project, so evidence from - * before the field existed keeps its old behavior. - * - Sessions are keyed by canonical transcript identity (with the legacy id as a fallback), - * so re-analyzing or re-sampling the same source session overwrites its observation and - * never adds a count. Persisted observations only contribute when that identity belongs - * to the current selected sample, so sessions outside the window or cap cannot skew fold. + * Domain voting is owned by `clusterDomainVote` in `src/fold.js`. A missing domain + * counts as project, so evidence from before the field existed keeps its old behavior. + * - Observations normally use discovery's shared observer identity for linked OMP + * descendants, the transcript's own otherwise (`corroborationIdentityOf`). Linkage + * rules live in `src/discovery/adapters/pi.js`. + * Older per-file keys migrate only when selected native provenance and any occupied + * destination are compatible; legacy id keys additionally require unambiguous + * `legacyIds`. Incompatible history stays separately keyed, and recording uses a + * native or source-qualified key rather than overwriting it. Storage keys do not + * grant extra observer votes. Persisted observations only contribute when their + * admitted observer belongs to the current selected sample, so observers outside the + * window or cap cannot skew fold. * - A gap is a fact about its session: re-analysis that no longer mentions it is model - * noise, not the session changing, so observations are only ever replaced, not removed - * by absence. They retire in exactly two ways: the memory surface gains content + * noise, not the session changing, so absence never removes history. Sightings retire + * in exactly two ways: the memory surface gains content * that covers the gap - a memory-file instruction or a skill's description/body * (`GAP_COVERED_THRESHOLD`, the `reanchor` bar) - or the sighting * has waited longer than `gapLedgerMaxAge` for a partner, counted from when backpass @@ -59,13 +63,18 @@ export const GAP_SIMILARITY_THRESHOLD = 0.45; /** A memory-file instruction this similar to a gap's proposal covers it. */ export const GAP_COVERED_THRESHOLD = 0.6; +export function highestRisk(items) { + const order = { high: 3, medium: 2, low: 1 }; + return items.reduce((best, item) => (order[item.recurrenceRisk] > order[best] ? item.recurrenceRisk : best), "low"); +} + export function emptyGapLedger() { return { version: 1, entries: {} }; } /** - * Fold-issued source label for one session. Evidence floors, `summary.sources`, and - * `sourceProjects` all key off this string, so it must not collapse two sessions. + * Fold-issued source label for one native transcript. `summary.sources` and + * `sourceProjects` key off this string; observer identities carry evidence floors. * Time-prefixed Codex ULIDs share an 8-character prefix when they start in the same * minute; keep the native id whole. * @@ -166,21 +175,169 @@ export function findGapEntry(ledger, memoryPath, proposedInstruction) { return best; } +function sessionIdentityAliases(transcript, sessionIdentity, legacyIds) { + return [...new Set([transcript.identity, transcript.id])].filter( + (identity) => identity && identity !== sessionIdentity && (identity !== transcript.id || legacyIds.has(identity)), + ); +} + +function selectedNativeObservers(transcripts, legacyIds) { + const observers = new Map(); + for (const transcript of transcripts) { + if (!(transcript?.corroborationIdentity || transcript?.identity || transcript?.id)) continue; + const observer = corroborationIdentityOf(transcript); + for (const native of new Set([ + transcript.identity || transcript.id, + ...(legacyIds.has(transcript.id) ? [transcript.id] : []), + ])) { + if (!observers.has(native)) observers.set(native, new Set()); + observers.get(native).add(observer); + } + } + return observers; +} + +export function resolveGapObservationObserver(observation, observersByNative) { + if (!observation || hasUnattributedSightings(observation)) return null; + let observer = null; + for (const native of new Set([observation.sourceSessionId, ...(observation.sightingIds || [])])) { + const observers = observersByNative.get(native); + if (observers?.size !== 1) return null; + const [selected] = observers; + if (observer !== null && observer !== selected) return null; + observer = selected; + } + return observer; +} + +function isOrdinaryNative(transcript, sessionIdentity) { + return ( + !transcript.parentSessionId && + !(transcript.harness === "pi" && transcript.interactionSignals?.source === "subagent") && + (transcript.identity || transcript.id) === sessionIdentity + ); +} + +function sameContribution(left, right) { + if ((left.sourceSessionId || null) !== (right.sourceSessionId || null)) return false; + const leftNatives = [...new Set([left.sourceSessionId, ...(left.sightingIds || [])].filter(Boolean))].sort(); + const rightNatives = [...new Set([right.sourceSessionId, ...(right.sightingIds || [])].filter(Boolean))].sort(); + if (leftNatives.length && rightNatives.length && JSON.stringify(leftNatives) !== JSON.stringify(rightNatives)) + return false; + if ((left.sourceKind || "native") !== (right.sourceKind || "native")) return false; + if ((left.sourceId || null) !== (right.sourceId || null)) return false; + if (left.contentDigest && right.contentDigest && left.contentDigest !== right.contentDigest) return false; + return left.sourceKind !== "external" || (left.revision === right.revision && left.evidenceKey === right.evidenceKey); +} + +function contributionKey(observation) { + return sha256( + JSON.stringify([ + observation.sourceSessionId, + observation.sightingIds, + observation.sourceKind, + observation.sourceId, + observation.revision, + observation.evidenceKey, + observation.contentDigest, + ]), + ); +} + +function destinationAcceptsObservation(observation, sessionIdentity, transcript, observersByNative, provenance) { + if (!observation) return true; + const prior = + !observation.sourceSessionId && isOrdinaryNative(transcript, sessionIdentity) + ? { ...observation, sourceSessionId: transcript.identity || transcript.id } + : observation; + if (!sameContribution(prior, provenance)) return false; + for (const native of new Set([observation.sourceSessionId, ...(observation.sightingIds || [])].filter(Boolean))) { + const observers = observersByNative.get(native); + if (observers && (observers.size !== 1 || !observers.has(sessionIdentity))) return false; + } + if (resolveGapObservationObserver(observation, observersByNative) === sessionIdentity) return true; + const nativeIdentity = transcript.identity || transcript.id; + return ( + (observation.unattributedSightings && + (observation.sourceSessionId === nativeIdentity || observation.sightingIds?.includes(nativeIdentity)) && + observersByNative.get(nativeIdentity)?.size === 1) || + (!observation.sourceSessionId && + isOrdinaryNative(transcript, sessionIdentity) && + (observation.sightingIds || []).every((native) => { + const observers = observersByNative.get(native); + return observers?.size === 1 && observers.has(sessionIdentity); + })) + ); +} + +function takePriorObservations( + entry, + sessionIdentity, + aliases, + transcript, + destinationIdentity = sessionIdentity, + acceptsPrior = (_observation) => true, +) { + const nativeIdentity = transcript.identity || transcript.id; + const current = entry.sessions[destinationIdentity]; + if (current && !current.sourceSessionId && isOrdinaryNative(transcript, sessionIdentity)) { + current.sourceSessionId = nativeIdentity; + current.sightingIds = [...new Set([...(current.sightingIds || []), nativeIdentity])]; + delete current.unattributedSightings; + } + for (const alias of aliases) { + const observation = entry.sessions[alias]; + if (!observation || observation.sourceSessionId) continue; + observation.sourceSessionId = nativeIdentity; + observation.sightingIds = [...new Set([...(observation.sightingIds || []), nativeIdentity])]; + observation.source = gapSource(transcript); + } + const priors = [entry.sessions[destinationIdentity], ...aliases.map((identity) => entry.sessions[identity])].filter( + (observation) => observation && acceptsPrior(observation), + ); + const firstObservedAt = priors + .map((observation) => observation.firstObservedAt || observation.observedAt) + .filter((value) => Number.isFinite(Date.parse(value))) + .sort((a, b) => Date.parse(a) - Date.parse(b))[0]; + const coveredBySkill = priors.find((observation) => observation.coveredBySkill)?.coveredBySkill; + for (const alias of aliases) { + if (priors.includes(entry.sessions[alias])) delete entry.sessions[alias]; + } + return { priors, firstObservedAt, coveredBySkill }; +} + +function hasUnattributedSightings(observation) { + return Boolean(observation.unattributedSightings || !observation.sourceSessionId); +} + /** - * Fold this run's evidence into the ledger. One observation per (gap, session); a - * session seen again replaces its own observation and keeps its first-seen timestamp. + * Record this run's sightings under the identity and provenance rules above, retaining + * the first-seen timestamp when replacing compatible history. * - * @param {{ now?: Date, skills?: unknown[] }} [options] + * @param {{ now?: Date, skills?: unknown[], legacyIds?: Set, transcripts?: object[] }} [options] */ export function recordGapObservations(ledger, evidenceRecords, options = {}) { - const { now = new Date() } = options; + const { now = new Date(), legacyIds = new Set() } = options; const observedAt = new Date(now).toISOString(); + const observersByNative = selectedNativeObservers( + options.transcripts || evidenceRecords.map((record) => record?.transcript), + legacyIds, + ); let recorded = 0; for (const record of evidenceRecords) { if (!record || record.status !== "ok" || !record.memoryPath) continue; const transcript = record.transcript || {}; - const sessionIdentity = transcript.identity || transcript.id; - if (!sessionIdentity) continue; + const sessionIdentity = corroborationIdentityOf(transcript); + if (!(transcript.corroborationIdentity || transcript.identity || transcript.id)) continue; + const provenance = { + sourceSessionId: transcript.identity || transcript.id, + sightingIds: [transcript.identity || transcript.id], + sourceKind: transcript.sourceKind || "native", + sourceId: transcript.sourceId || null, + revision: transcript.revision || null, + evidenceKey: record.key || null, + contentDigest: transcript.contentSignature || `${transcript.mtimeMs}:${transcript.bytes}`, + }; for (const gap of record.gaps || []) { if (!gap || !gap.proposedInstruction) continue; // A citation from the analysis turn wins over word overlap: the model saw both @@ -210,22 +367,65 @@ export function recordGapObservations(ledger, evidenceRecords, options = {}) { entry.proposedInstruction = gap.proposedInstruction; } } - const identityPrior = entry.sessions[sessionIdentity]; - const aliasPrior = transcript.id && transcript.id !== sessionIdentity ? entry.sessions[transcript.id] : null; - const sameTrace = (observation) => - (transcript.sourceKind !== "external" && observation.sourceKind !== "external") || - (observation.sourceKind === (transcript.sourceKind || "native") && - observation.sourceId === (transcript.sourceId || null) && - observation.revision === (transcript.revision || null) && - observation.evidenceKey === (record.key || null)); - const priors = [identityPrior, aliasPrior].filter((observation) => observation && sameTrace(observation)); - const firstObservedAt = priors - .map((observation) => observation.firstObservedAt || observation.observedAt) - .filter((value) => Number.isFinite(Date.parse(value))) - .sort((a, b) => Date.parse(a) - Date.parse(b))[0]; - if (aliasPrior) delete entry.sessions[transcript.id]; - const coveredBySkill = - gap.coveredBySkill || priors.find((observation) => observation.coveredBySkill)?.coveredBySkill; + let destinationIdentity = sessionIdentity; + let sourceQualified = false; + if ( + !destinationAcceptsObservation( + entry.sessions[destinationIdentity], + sessionIdentity, + transcript, + observersByNative, + provenance, + ) + ) { + destinationIdentity = transcript.identity || transcript.id; + } + if ( + !destinationAcceptsObservation( + entry.sessions[destinationIdentity], + sessionIdentity, + transcript, + observersByNative, + provenance, + ) + ) { + destinationIdentity = JSON.stringify([sessionIdentity, transcript.identity || transcript.id]); + sourceQualified = true; + } + if ( + !destinationAcceptsObservation( + entry.sessions[destinationIdentity], + sessionIdentity, + transcript, + observersByNative, + provenance, + ) + ) { + destinationIdentity = JSON.stringify([ + sessionIdentity, + transcript.identity || transcript.id, + contributionKey(provenance), + ]); + sourceQualified = true; + } + const prior = takePriorObservations( + entry, + sessionIdentity, + (sourceQualified ? [] : sessionIdentityAliases(transcript, sessionIdentity, legacyIds)).filter( + (identity) => + identity !== destinationIdentity && + !entry.sessions[identity]?.sourceSessionId && + (entry.sessions[identity]?.sightingIds || []).every((native) => { + const observers = observersByNative.get(native); + return observers?.size === 1 && observers.has(sessionIdentity); + }), + ), + transcript, + destinationIdentity, + (observation) => sameContribution(observation, provenance), + ); + const { priors, firstObservedAt } = prior; + const coveredBySkill = gap.coveredBySkill || prior.coveredBySkill; const phrasings = [ ...new Set([ ...priors.flatMap( @@ -234,10 +434,11 @@ export function recordGapObservations(ledger, evidenceRecords, options = {}) { gap.proposedInstruction, ]), ]; - entry.sessions[sessionIdentity] = { + entry.sessions[destinationIdentity] = { firstObservedAt: firstObservedAt || observedAt, observedAt, sessionStartedAt: + transcript.corroborationStartedAt ?? transcript.startedAt ?? priors.find((observation) => observation.sessionStartedAt != null)?.sessionStartedAt ?? null, @@ -249,11 +450,26 @@ export function recordGapObservations(ledger, evidenceRecords, options = {}) { contentSignature: transcript.contentSignature || null, contentDigest: transcript.contentSignature || `${transcript.mtimeMs}:${transcript.bytes}`, source: gapSource(transcript), + sourceSessionId: transcript.identity || transcript.id, + observerId: sessionIdentity, + sightingIds: [ + ...new Set( + [ + ...priors.flatMap((observation) => observation.sightingIds || []), + transcript.identity || transcript.id, + ].filter(Boolean), + ), + ], + ...(priors.some(hasUnattributedSightings) ? { unattributedSightings: true } : {}), mistake: gap.mistake, quote: gap.quote, - recurrenceRisk: gap.recurrenceRisk, + recurrenceRisk: highestRisk([gap, ...priors]), phrasings, - domain: gap.domain === "orchestration" ? "orchestration" : "project", + domain: + gap.domain === "orchestration" && + !priors.some((observation) => observation.observedAt === observedAt && observation.domain !== "orchestration") + ? "orchestration" + : "project", // A failed trigger: the analysis judged an existing skill's content to cover // this mistake. Absent when no skill covers it (including all pre-existing // observations), and absence never counts as a citation. @@ -266,6 +482,120 @@ export function recordGapObservations(ledger, evidenceRecords, options = {}) { } return recorded; } +/** + * Re-key selected observations in old ledgers under the identity and provenance rules + * above; native analysis identities remain separate from corroborating observers. + */ +export function normalizeGapLedgerSessions(ledger, transcripts, { legacyIds = new Set() } = {}) { + const selections = []; + const observersByNative = selectedNativeObservers(transcripts, legacyIds); + for (const transcript of transcripts) { + if (!(transcript?.corroborationIdentity || transcript?.identity || transcript?.id)) continue; + const sessionIdentity = corroborationIdentityOf(transcript); + const aliases = sessionIdentityAliases(transcript, sessionIdentity, legacyIds); + const ordinaryNative = isOrdinaryNative(transcript, sessionIdentity); + selections.push({ transcript, sessionIdentity, aliases, ordinaryNative }); + } + + const resolvesTo = (observation, sessionIdentity) => + resolveGapObservationObserver(observation, observersByNative) === sessionIdentity; + for (const entry of Object.values(ledger.entries)) { + for (const { transcript, sessionIdentity, aliases: nativeAliases, ordinaryNative } of selections) { + if ( + legacyIds.has(transcript.id) && + transcript.identity && + transcript.identity !== transcript.id && + observersByNative.get(transcript.id)?.size === 1 && + observersByNative.get(transcript.id).has(sessionIdentity) + ) { + for (const observation of Object.values(entry.sessions)) { + if (observation.sourceSessionId === transcript.id) observation.sourceSessionId = transcript.identity; + if (observation.sightingIds?.includes(transcript.id)) { + observation.sightingIds = [ + ...new Set( + observation.sightingIds.map((identity) => + identity === transcript.id ? transcript.identity : identity, + ), + ), + ]; + } + } + } + const destination = entry.sessions[sessionIdentity]; + if ( + destination && + !resolvesTo(destination, sessionIdentity) && + !( + ordinaryNative && + !destination.sourceSessionId && + (destination.sightingIds || []).every((native) => { + const observers = observersByNative.get(native); + return observers?.size === 1 && observers.has(sessionIdentity); + }) + ) + ) + continue; + const aliases = nativeAliases.filter((identity) => { + const prior = entry.sessions[identity]; + return !prior?.sourceSessionId + ? (prior?.sightingIds || []).every((native) => { + const observers = observersByNative.get(native); + return observers?.size === 1 && observers.has(sessionIdentity); + }) + : resolvesTo(prior, sessionIdentity); + }); + for (const [identity, prior] of Object.entries(entry.sessions)) { + if ( + identity !== sessionIdentity && + prior.sourceSessionId === (transcript.identity || transcript.id) && + resolvesTo(prior, sessionIdentity) && + !aliases.includes(identity) + ) + aliases.push(identity); + } + const observation = entry.sessions[sessionIdentity]; + if (ordinaryNative && observation && !observation.sourceSessionId) { + observation.sourceSessionId = transcript.identity || transcript.id; + observation.sightingIds = [...new Set([...(observation.sightingIds || []), observation.sourceSessionId])]; + delete observation.unattributedSightings; + } + if (!aliases.some((identity) => entry.sessions[identity])) continue; + const original = + entry.sessions[sessionIdentity] || aliases.map((identity) => entry.sessions[identity]).find(Boolean); + const { priors, firstObservedAt, coveredBySkill } = takePriorObservations( + entry, + sessionIdentity, + aliases, + transcript, + sessionIdentity, + (observation) => sameContribution(observation, original), + ); + const current = priors[0]; + const project = current.project || priors.find((observation) => observation.project)?.project; + const projectRoot = current.projectRoot || priors.find((observation) => observation.projectRoot)?.projectRoot; + + entry.sessions[sessionIdentity] = { + ...current, + observerId: sessionIdentity, + ...(priors.length > 1 ? { recurrenceRisk: highestRisk(priors) } : {}), + ...(firstObservedAt ? { firstObservedAt } : {}), + sightingIds: [...new Set(priors.flatMap((observation) => observation.sightingIds || []))], + ...(priors.some(hasUnattributedSightings) ? { unattributedSightings: true } : {}), + phrasings: [ + ...new Set([ + ...priors.flatMap( + (observation) => observation.phrasings || [observation.proposedInstruction].filter(Boolean), + ), + ]), + ].filter(Boolean), + domain: priors.some((observation) => observation.domain !== "orchestration") ? "project" : "orchestration", + ...(coveredBySkill ? { coveredBySkill } : {}), + ...(project ? { project } : {}), + ...(projectRoot ? { projectRoot } : {}), + }; + } + } +} /** * Narrow a ledger copy to the sightings this run may show a model or fold: selected trace @@ -280,14 +610,16 @@ export function filterGapLedger(ledger, evidenceRecords, selectedTranscripts = [ options.sourceKind || (selectedTranscripts.some((transcript) => transcript.sourceKind === "external") ? "external" : "native"); const selected = new Map(); + const observersByNative = selectedNativeObservers(selectedTranscripts, options.legacyIds || new Set()); for (const transcript of selectedTranscripts) { selected.set(transcript.identity || transcript.id, transcript); if (transcript.sourceKind !== "external" && transcript.id) selected.set(transcript.id, transcript); } for (const [id, entry] of Object.entries(ledger.entries)) { for (const [sessionId, observation] of Object.entries(entry.sessions)) { - const record = current.get(sessionId); - const transcript = selected.get(sessionId); + const nativeIdentity = observation.sourceSessionId || sessionId; + const record = current.get(nativeIdentity); + const transcript = selected.get(nativeIdentity); const traceOnly = transcript?.sourceKind === "external"; const valid = !transcript && sourceKind === "native" && observation.sourceKind !== "external" @@ -305,6 +637,10 @@ export function filterGapLedger(ledger, evidenceRecords, selectedTranscripts = [ observation.contentDigest === (transcript.contentSignature || `${transcript.mtimeMs}:${transcript.bytes}`)); if (!valid) delete entry.sessions[sessionId]; + else { + const observer = resolveGapObservationObserver(observation, observersByNative); + if (observer) observation.observerId = observer; + } } if (!Object.keys(entry.sessions).length) { delete ledger.entries[id]; @@ -392,9 +728,12 @@ export function ledgerGapObservations(ledger, memoryPath, skills = null) { observations.push({ proposedInstruction: entry.proposedInstruction, phrasings: obs.phrasings?.length ? obs.phrasings : [entry.proposedInstruction], - sessionId, + sessionId: obs.observerId || sessionId, gapIds: [entry.id, ...(entry.aliases || [])], source: obs.source, + ...(obs.sourceSessionId ? { sourceSessionId: obs.sourceSessionId } : {}), + sightingIds: obs.sightingIds || [], + unattributedSightings: hasUnattributedSightings(obs), mistake: obs.mistake, quote: obs.quote, recurrenceRisk: obs.recurrenceRisk, @@ -451,17 +790,28 @@ export function mergeGapEntries(ledger, groups) { for (const entry of entries) { if (entry === target) continue; for (const [sessionId, obs] of Object.entries(entry.sessions)) { - const prior = target.sessions[sessionId]; + const destination = target.sessions[sessionId]; + const storageKey = + destination && !sameContribution(destination, obs) + ? JSON.stringify([sessionId, contributionKey(obs)]) + : sessionId; + const prior = target.sessions[storageKey]; if (!prior) { - target.sessions[sessionId] = obs; + target.sessions[storageKey] = { + ...obs, + phrasings: obs.phrasings || [entry.proposedInstruction], + }; } else { const earlier = Date.parse(obs.firstObservedAt || obs.observedAt) < Date.parse(prior.firstObservedAt || prior.observedAt); if (earlier) prior.firstObservedAt = obs.firstObservedAt || obs.observedAt; + prior.recurrenceRisk = highestRisk([prior, obs]); if (prior.domain === "orchestration" && obs.domain !== "orchestration") prior.domain = "project"; if (!prior.coveredBySkill && obs.coveredBySkill) prior.coveredBySkill = obs.coveredBySkill; if (!prior.project && obs.project) prior.project = obs.project; if (!prior.projectRoot && obs.projectRoot) prior.projectRoot = obs.projectRoot; + prior.sightingIds = [...new Set([...(prior.sightingIds || []), ...(obs.sightingIds || [])])]; + if (hasUnattributedSightings(prior) || hasUnattributedSightings(obs)) prior.unattributedSightings = true; prior.phrasings = [ ...new Set([ ...(prior.phrasings || [target.proposedInstruction]), diff --git a/src/interaction.js b/src/interaction.js index 020f1fa8..0e04a894 100644 --- a/src/interaction.js +++ b/src/interaction.js @@ -6,9 +6,10 @@ * * Non-interactive is detected best-effort from per-harness metadata (codex * `originator: codex_exec` / `source: exec`, claude `entrypoint` values that start - * with `sdk`, an OpenCode child `parent_id`, Hermes cron/gateway/whatsapp if they - * ever leak past discovery) and from cwd (a `.no-mistakes` path segment - pipeline - * worktrees are one kind of non-interactive run, not their own category). + * with `sdk`, an OpenCode child `parent_id`, an OMP subagent's nested layout, + * Hermes cron/gateway/whatsapp if they ever leak past discovery) and from cwd (a + * `.no-mistakes` path segment - pipeline worktrees are one kind of non-interactive run, + * not their own category). */ export const INTERACTIVE = "interactive"; @@ -57,9 +58,13 @@ function claudeEntrypointIsNonInteractive(entrypoint) { /** * Map a discovered transcript (or adapter descriptor) onto the two public categories. - * Explicit `transcript.interaction` is trusted when it is already one of the two labels. + * An OMP subagent is always non-interactive, even over a stamped label; otherwise an + * explicit `transcript.interaction` is trusted when it is already one of the two labels. */ export function classifyInteraction(transcript) { + if (transcript?.parentSessionId || (transcript?.harness === "pi" && signalsOf(transcript).source === "subagent")) { + return NON_INTERACTIVE; + } const stamped = transcript?.interaction; if (stamped === INTERACTIVE || stamped === NON_INTERACTIVE) return stamped; diff --git a/src/proposal.js b/src/proposal.js index 96d24b06..b93fc6af 100644 --- a/src/proposal.js +++ b/src/proposal.js @@ -105,7 +105,7 @@ export class ProposalViolation extends Error { } } -function normalizeEdit(raw, index, knownSources = null) { +function normalizeEdit(raw, index, knownSources = null, sourceObservers = null) { const kind = String(raw?.kind || "").toLowerCase(); const refs = Array.isArray(raw?.changes) ? raw.changes : Array.isArray(raw?.hunks) ? raw.hunks : []; const normalizedEvidence = normalizeEvidence(raw?.evidence); @@ -122,9 +122,9 @@ function normalizeEdit(raw, index, knownSources = null) { instructions: Array.isArray(raw?.instructions) ? raw.instructions.map(String) : [], evidence, // Corroboration is measured from the edit's normalized quotes, never from a - // model-reported count. When the fold handed over this run's source labels, - // only those labels count - a typed-but-never-issued source is not a session. - transcripts: countSources(normalizedEvidence, knownSources), + // model-reported count. Fold-issued labels admit citations; `sourceObservers` + // collapses related native transcripts before counting independent observers. + transcripts: countSources(normalizedEvidence, knownSources, sourceObservers), }; } @@ -139,11 +139,12 @@ function normalizeEvidence(evidence) { })); } -function countSources(evidence, known = null) { +function countSources(evidence, known = null, observers = null) { if (!Array.isArray(evidence)) return 0; - const labels = new Set(evidence.map((e) => normalizeSourceLabel(e?.source)).filter(Boolean)); - if (!known) return labels.size; - return [...labels].filter((label) => known.has(label)).length; + const labels = [...new Set(evidence.map((e) => normalizeSourceLabel(e?.source)).filter(Boolean))]; + const admitted = known ? labels.filter((label) => known.has(label)) : labels; + if (!observers) return admitted.length; + return new Set(admitted.flatMap((label) => [observers[label]].flat().filter(Boolean))).size; } /** The del-line texts of a hunk that are not carried by `lineCounts` (blank lines ignored). */ @@ -388,7 +389,7 @@ export function buildProposal(rawResult, context) { const knownSources = Array.isArray(summary?.sources) ? new Set(summary.sources.map(normalizeSourceLabel).filter(Boolean)) : null; - const edits = rawEdits.map((raw, i) => normalizeEdit(raw, i, knownSources)); + const edits = rawEdits.map((raw, i) => normalizeEdit(raw, i, knownSources, summary?.sourceObservers)); const changesById = new Map(measured.changes.map((c) => [c.id, c])); // Skill description lines are always loaded, so they sit under the same cap as the @@ -602,9 +603,9 @@ export function buildProposal(rawResult, context) { } // Corroboration is measured, not declared, and it covers the whole always-loaded - // surface: adding, rewriting and removing text all clear the same session floor. + // surface: adding, rewriting and removing text all clear the same observer floor. // Asking instead whether a rewrite "really" adds an instruction requires a semantic - // text classifier. Counting the distinct sessions behind an edit's own quotes asks + // text classifier. Counting the distinct observers behind an edit's own quotes asks // nothing of the text, so there is no shape to game. Extract and move keep every line // on the always-loaded surface, so they stay exempt; a removal keeps the harm floor // below on top of this one. @@ -634,14 +635,18 @@ export function buildProposal(rawResult, context) { if (routing && !preservesAlwaysLoaded(edit.kind) && hunks.some((h) => h.added > 0 && h.removed === 0)) { const sessions = [ ...new Set( - edit.evidence.map((item) => summary?.sourceSessions?.[normalizeSourceLabel(item.source)]).filter(Boolean), + edit.evidence + .flatMap((item) => [summary?.sourceSessions?.[normalizeSourceLabel(item.source)]].flat()) + .filter(Boolean), ), ]; - const sightings = edit.evidence.map((item) => ({ - sessionId: summary?.sourceSessions?.[normalizeSourceLabel(item.source)], - quote: item.text, - })); - const owner = rootOwnsGap(sightings, routing.rootOwnedGaps) + const sightings = edit.evidence.flatMap((item) => + [summary?.sourceSessions?.[normalizeSourceLabel(item.source)]] + .flat() + .filter(Boolean) + .map((sessionId) => ({ sessionId, quote: item.text })), + ); + const owner = rootOwnsGap(sightings, routing.rootOwnedGaps ?? summary?.rootOwnedGaps) ? routing.rootPath : (routing.ownerOf(sessions) ?? routing.rootPath); const here = routing.weight ?? routing.rootPath; diff --git a/src/repo.js b/src/repo.js index 4c5789c0..c3d7e279 100644 --- a/src/repo.js +++ b/src/repo.js @@ -58,11 +58,20 @@ export function listWorktrees(root) { } catch { return [realpathOrSelf(root)]; } - const paths = []; + // With `git init --separate-git-dir`, git reports the external git dir as the + // main `worktree` entry; it is not a checkout, so it must not be counted as one. + let commonDir = null; + try { + commonDir = realpathOrSelf(path.resolve(root, git(["rev-parse", "--git-common-dir"], root))); + } catch { + // keep every reported entry + } + const paths = [realpathOrSelf(root)]; for (const line of raw.split("\n")) { - if (line.startsWith("worktree ")) paths.push(realpathOrSelf(line.slice("worktree ".length))); + if (!line.startsWith("worktree ")) continue; + const worktree = realpathOrSelf(line.slice("worktree ".length)); + if (worktree !== commonDir) paths.push(worktree); } - if (!paths.length) paths.push(realpathOrSelf(root)); return [...new Set(paths)]; } diff --git a/src/transcript.js b/src/transcript.js index 29dc0c1d..7b12c827 100644 --- a/src/transcript.js +++ b/src/transcript.js @@ -43,3 +43,8 @@ export function transcriptIdentity(transcript) { ) .digest("hex"); } + +/** Shared observer identity for related transcript files such as OMP subagents. */ +export function corroborationIdentityOf(transcript) { + return transcript?.corroborationIdentity || transcriptIdentity(transcript); +} diff --git a/test/adapters.test.js b/test/adapters.test.js index d95bacd9..7b2103b1 100644 --- a/test/adapters.test.js +++ b/test/adapters.test.js @@ -27,6 +27,11 @@ function candidateFor(file) { return { key: file, path: file, mtimeMs: stat.mtimeMs, bytes: stat.size }; } +/** What `pi.enumerate` yields for a file in the opt-in OMP store (`subagent` for nested files). */ +function ompCandidateFor(file, { subagent = false } = {}) { + return { ...candidateFor(file), omp: true, subagent }; +} + function messages(events) { return events.filter((e) => e.kind === "message"); } @@ -151,6 +156,142 @@ test("pi adapter reads the session header and drops thinking blocks", () => { assert.equal(toolCall.result, "nothing to commit"); }); +test("pi adapter ignores an omp-style model field outside a title-led transcript", () => { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), "backpass-pi-model-")); + const file = path.join(dir, "plain.jsonl"); + fs.writeFileSync( + file, + [ + { type: "session", version: 3, id: "plain", timestamp: "2026-08-27T00:00:00.000Z", cwd: "/repo/demo" }, + { type: "model_change", id: "m1", parentId: null, model: "cursor/composer-2.5" }, + ] + .map((entry) => JSON.stringify(entry)) + .join("\n") + "\n", + ); + assert.equal(pi.read({ path: file }).model, null); +}); + +test("pi adapter classifies omp sessions past the title record and reads model", () => { + const file = path.join(FIXTURES, "omp-session.jsonl"); + const descriptor = pi.classify(ompCandidateFor(file)); + assert.equal(descriptor.id, "omp-5678"); + assert.equal(pi.classify(candidateFor(file)), null, "a plain Pi store never reads a title-led header"); + assert.equal(descriptor.cwd, "/repo/demo"); + + const { events, model } = pi.read({ path: file }); + assert.equal(model, "cursor/composer-2.5", "omp model_change carries model, not modelId"); + const [toolCall] = tools(events); + assert.equal(toolCall.name, "bash"); + assert.equal(toolCall.result, "nothing to commit"); +}); +test("pi adapter accepts only line one or line two after a title header", () => { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), "backpass-omp-header-")); + const title = JSON.stringify({ type: "title", v: 1, title: "" }); + const session = JSON.stringify({ type: "session", version: 3, id: "late", cwd: "/repo/demo" }); + const other = JSON.stringify({ type: "message", message: { role: "user", content: "hello" } }); + const afterTitle = path.join(dir, "after-title.jsonl"); + const afterOther = path.join(dir, "after-other.jsonl"); + + fs.writeFileSync(afterTitle, `${title}\n${other}\n${session}\n`); + fs.writeFileSync(afterOther, `${other}\n${session}\n`); + + assert.equal(pi.classify(ompCandidateFor(afterTitle)), null, "line three is outside the header"); + assert.equal(pi.classify(ompCandidateFor(afterOther)), null, "line two is a header only after a title record"); +}); + +test("pi adapter links an OMP subagent to its sibling parent session", () => { + const root = fs.mkdtempSync(path.join(os.tmpdir(), "backpass-omp-parent-")); + const sessionDir = path.join(root, "-repo-demo"); + const parentName = "2026-08-27T00-00-00.000Z_parent-folder"; + const parentPath = path.join(sessionDir, `${parentName}.jsonl`); + const childPath = path.join(sessionDir, parentName, "Subagent.jsonl"); + writeOmpSession(parentPath, { id: "parent-native", cwd: "/repo/demo" }); + writeOmpSession(childPath, { id: "child-native", cwd: "/repo/demo" }); + + const child = pi.classify(ompCandidateFor(childPath, { subagent: true })); + assert.equal(child.parentSessionId, "parent-native"); + assert.equal(child.parentSessionPath, parentPath); + assert.equal(child.parentSessionStartedAt, Date.parse("2026-08-27T00:00:00.000Z")); + assert.equal(pi.classify(ompCandidateFor(parentPath)).parentSessionId, undefined); +}); + +test("pi adapter links a second-level OMP subagent to the root session", () => { + const root = fs.mkdtempSync(path.join(os.tmpdir(), "backpass-omp-nested-")); + const sessionDir = path.join(root, "-repo-demo"); + const parentName = "2026-08-27T00-00-00.000Z_parent-folder"; + const parentPath = path.join(sessionDir, `${parentName}.jsonl`); + const childPath = path.join(sessionDir, parentName, "Subagent.jsonl"); + const grandchildPath = path.join(sessionDir, parentName, "Subagent", "Subagent.Child.jsonl"); + writeOmpSession(parentPath, { id: "parent-native", cwd: "/repo/demo" }); + writeOmpSession(childPath, { id: "child-native", cwd: "/repo/demo" }); + writeOmpSession(grandchildPath, { id: "grandchild-native", cwd: "/repo/demo" }); + + const grandchild = pi.classify(ompCandidateFor(grandchildPath, { subagent: true })); + assert.equal(grandchild.id, "grandchild-native"); + assert.equal(grandchild.parentSessionId, "parent-native"); + assert.equal(grandchild.parentSessionPath, parentPath); + assert.equal(grandchild.parentSessionStartedAt, Date.parse("2026-08-27T00:00:00.000Z")); +}); + +test("pi adapter links OMP subagents by nested path even when their cwd differs", () => { + const root = fs.mkdtempSync(path.join(os.tmpdir(), "backpass-omp-cwd-")); + const sessionDir = path.join(root, "-repo-demo"); + const parentName = "2026-08-27T00-00-00.000Z_parent-folder"; + const parentPath = path.join(sessionDir, `${parentName}.jsonl`); + const childPath = path.join(sessionDir, parentName, "Subagent.jsonl"); + const grandchildPath = path.join(sessionDir, parentName, "Subagent", "Subagent.Child.jsonl"); + writeOmpSession(parentPath, { id: "parent-native", cwd: "/repo/demo" }); + writeOmpSession(childPath, { id: "child-native", cwd: "/repo/demo/packages/api" }); + writeOmpSession(grandchildPath, { id: "grandchild-native", cwd: "/worktrees/demo-isolated" }); + + const child = pi.classify(ompCandidateFor(childPath, { subagent: true })); + assert.equal(child.cwd, "/repo/demo/packages/api", "association still uses the subagent's own cwd"); + assert.equal(child.parentSessionId, "parent-native"); + assert.equal(child.parentSessionPath, parentPath); + + const grandchild = pi.classify(ompCandidateFor(grandchildPath, { subagent: true })); + assert.equal(grandchild.cwd, "/worktrees/demo-isolated"); + assert.equal(grandchild.parentSessionId, "parent-native"); + assert.equal(grandchild.parentSessionPath, parentPath); +}); + +test("pi discovery checks a missing parent path once per scan", () => { + const root = fs.mkdtempSync(path.join(os.tmpdir(), "backpass-pi-parent-cache-")); + const sessionDir = path.join(root, "sessions", "-repo-demo"); + const firstPath = path.join(sessionDir, "missing-parent", "First.jsonl"); + const secondPath = path.join(sessionDir, "missing-parent", "Second.jsonl"); + writeOmpSession(firstPath, { id: "first", cwd: "/repo/demo" }); + writeOmpSession(secondPath, { id: "second", cwd: "/repo/demo" }); + const candidates = [ompCandidateFor(firstPath, { subagent: true }), ompCandidateFor(secondPath, { subagent: true })]; + const missingParentPath = path.join(sessionDir, "missing-parent.jsonl"); + const scanContext = pi.createScanContext(); + const originalStatSync = fs.statSync; + let parentProbes = 0; + + fs.statSync = function (file, ...args) { + if (file === missingParentPath) parentProbes += 1; + return originalStatSync.call(this, file, ...args); + }; + try { + for (const candidate of candidates) { + const descriptor = pi.classify(candidate, { scanContext }); + assert.equal(descriptor.parentSessionId, undefined); + } + } finally { + fs.statSync = originalStatSync; + } + + assert.equal(parentProbes, 1, "subagents sharing a missing parent should look it up once per scan"); +}); +function writeOmpSession(file, { id, cwd }) { + fs.mkdirSync(path.dirname(file), { recursive: true }); + fs.writeFileSync( + file, + `${JSON.stringify({ type: "title", v: 1, title: "", updatedAt: "2026-08-27T00:00:00.000Z", pad: " " })}\n` + + `${JSON.stringify({ type: "session", version: 3, id, timestamp: "2026-08-27T00:00:00.000Z", cwd })}\n`, + ); +} + function writePiSession(file, { id, cwd }) { fs.mkdirSync(path.dirname(file), { recursive: true }); fs.writeFileSync( @@ -190,6 +331,119 @@ function withPiStoreEnv( } } +test("pi nested layout signals subagents without readable ancestor headers", () => { + const homeDir = fs.mkdtempSync(path.join(os.tmpdir(), "backpass-omp-missing-headers-")); + const sessionDir = path.join(homeDir, ".omp", "agent", "sessions", "-repo-demo"); + const childPath = path.join(sessionDir, "root", "Subagent.jsonl"); + const grandchildPath = path.join(sessionDir, "other-root", "Missing", "Missing.Child.jsonl"); + writeOmpSession(childPath, { id: "child-native", cwd: "/repo/demo" }); + writeOmpSession(grandchildPath, { id: "grandchild-native", cwd: "/repo/demo" }); + fs.writeFileSync(path.join(sessionDir, "root.jsonl"), "not a session header\n"); + const piPath = path.join(homeDir, ".pi", "agent", "sessions", "-repo-demo", "ordinary.jsonl"); + const bridgeDir = path.join(homeDir, "bridge"); + const bridgePath = path.join(bridgeDir, "ordinary.jsonl"); + writePiSession(piPath, { id: "pi-native", cwd: "/repo/demo" }); + writePiSession(bridgePath, { id: "bridge-native", cwd: "/repo/demo" }); + + withPiStoreEnv({ homeDir, bridgeDir }, () => { + const candidates = pi.enumerate({ config: { discovery: { includeOmp: true } } }); + for (const file of [childPath, grandchildPath]) { + const descriptor = pi.classify(candidates.find((candidate) => candidate.path === file)); + assert.equal(descriptor.interactionSignals.source, "subagent"); + assert.equal(descriptor.parentSessionId, undefined); + assert.equal(descriptor.parentSessionPath, undefined); + assert.equal(descriptor.parentSessionStartedAt, undefined); + } + for (const file of [piPath, bridgePath]) { + const descriptor = pi.classify(candidates.find((candidate) => candidate.path === file)); + assert.deepEqual(descriptor.interactionSignals, {}); + assert.equal(descriptor.parentSessionId, undefined); + } + }); +}); + +test("pi adapter keeps plain Pi stores one level deep and reads nested layout only in the opt-in OMP store", () => { + const homeDir = fs.mkdtempSync(path.join(os.tmpdir(), "backpass-pi-depth-")); + const piAgentDir = path.join(homeDir, "pi-agent"); + const stores = { + default: path.join(homeDir, ".pi", "agent", "sessions", "-repo-demo"), + custom: path.join(piAgentDir, "sessions", "-repo-demo"), + omp: path.join(homeDir, ".omp", "agent", "sessions", "-repo-demo"), + }; + for (const [store, dir] of Object.entries(stores)) { + const writer = store === "omp" ? writeOmpSession : writePiSession; + writer(path.join(dir, "root.jsonl"), { id: `${store}-root`, cwd: "/repo/demo" }); + writer(path.join(dir, "root", "Helper.jsonl"), { id: `${store}-helper`, cwd: "/repo/demo" }); + } + writeOmpSession(path.join(stores.default, "titled.jsonl"), { id: "titled", cwd: "/repo/demo" }); + + withPiStoreEnv({ homeDir, piAgentDir }, () => { + for (const config of [undefined, { discovery: { includeOmp: false } }, { discovery: { includeOmp: true } }]) { + const candidates = pi.enumerate({ config }); + for (const store of ["default", "custom"]) { + const files = candidates.filter((candidate) => candidate.path.startsWith(stores[store])).map((c) => c.path); + assert.ok(files.includes(path.join(stores[store], "root.jsonl"))); + assert.ok(!files.includes(path.join(stores[store], "root", "Helper.jsonl")), `${store} store stays one level`); + for (const candidate of candidates.filter((c) => c.path.startsWith(stores[store]))) { + assert.equal(candidate.omp, false); + assert.equal(candidate.subagent, false); + const descriptor = pi.classify(candidate); + if (descriptor) { + assert.deepEqual(descriptor.interactionSignals, {}); + assert.equal(descriptor.parentSessionId, undefined); + assert.equal(pi.cacheDependency(candidate), undefined); + } + } + } + const titled = candidates.find((candidate) => candidate.path === path.join(stores.default, "titled.jsonl")); + assert.equal(pi.classify(titled), null, "a title-led header is not read outside the OMP store"); + } + + const candidates = pi.enumerate({ config: { discovery: { includeOmp: true } } }); + const helper = candidates.find((candidate) => candidate.path === path.join(stores.omp, "root", "Helper.jsonl")); + assert.ok(helper, "the OMP store is walked into subagent layout when enabled"); + assert.equal(helper.omp, true); + assert.equal(helper.subagent, true); + const root = candidates.find((candidate) => candidate.path === path.join(stores.omp, "root.jsonl")); + assert.equal(root.omp, true); + assert.equal(root.subagent, false); + assert.equal(pi.classify(helper).parentSessionId, "omp-root"); + assert.equal( + pi.enumerate({ config: { discovery: { includeOmp: false } } }).some((c) => c.path.startsWith(stores.omp)), + false, + ); + }); +}); + +test("pi adapter reads an OMP store reached through PI_CODING_AGENT_DIR only when includeOmp is on", () => { + const homeDir = fs.mkdtempSync(path.join(os.tmpdir(), "backpass-pi-omp-env-")); + const piAgentDir = path.join(homeDir, ".omp", "agent"); + const dir = path.join(piAgentDir, "sessions", "-repo-demo"); + writeOmpSession(path.join(dir, "root.jsonl"), { id: "root", cwd: "/repo/demo" }); + writeOmpSession(path.join(dir, "root", "Helper.jsonl"), { id: "helper", cwd: "/repo/demo" }); + + withPiStoreEnv({ homeDir, piAgentDir }, () => { + const off = pi.enumerate({ config: { discovery: { includeOmp: false } } }); + assert.deepEqual( + off.map((candidate) => path.basename(candidate.path)), + ["root.jsonl"], + "without the setting only the top level is listed", + ); + assert.equal(pi.classify(off[0]), null, "and its title-led header is not read"); + + const on = pi.enumerate({ config: { discovery: { includeOmp: true } } }); + assert.deepEqual(on.map((candidate) => path.basename(candidate.path)).sort(), ["Helper.jsonl", "root.jsonl"]); + assert.equal(pi.classify(on.find((candidate) => candidate.subagent)).parentSessionId, "root"); + const rootOn = on.find((candidate) => !candidate.subagent); + assert.equal(pi.classify(rootOn).id, "root"); + assert.notEqual( + pi.cacheDependency(rootOn), + pi.cacheDependency(off[0]), + "switching the setting reclassifies a file cached while it read as null", + ); + }); +}); + test("pi adapter enumerates standalone and BB-managed session roots without duplicates", () => { const fakeHome = fs.mkdtempSync(path.join(os.tmpdir(), "backpass-pi-home-")); const piAgentDir = path.join(fakeHome, "pi-agent"); @@ -200,6 +454,27 @@ test("pi adapter enumerates standalone and BB-managed session roots without dupl id: "standalone", cwd: "/repo/demo", }); + writeOmpSession(path.join(fakeHome, ".omp", "agent", "sessions", "-repo-demo", "omp-standalone.jsonl"), { + id: "omp-standalone", + cwd: "/repo/demo", + }); + writeOmpSession(path.join(fakeHome, ".omp", "agent", "sessions", "-repo-demo", "omp-standalone", "Subagent.jsonl"), { + id: "omp-subagent", + cwd: "/repo/demo", + }); + writeOmpSession( + path.join( + fakeHome, + ".omp", + "agent", + "sessions", + "-repo-demo", + "omp-standalone", + "Subagent", + "Subagent.Child.jsonl", + ), + { id: "omp-nested-subagent", cwd: "/repo/demo" }, + ); writePiSession(path.join(piAgentDir, "sessions", "-repo-demo", "custom-agent.jsonl"), { id: "custom-agent", cwd: "/repo/demo", @@ -222,9 +497,26 @@ test("pi adapter enumerates standalone and BB-managed session roots without dupl }); withPiStoreEnv({ homeDir: fakeHome, piAgentDir, piSessionDir, bbDataDir, bridgeDir }, () => { + for (const config of [undefined, { discovery: { includeOmp: false } }]) { + assert.deepEqual( + pi + .enumerate({ config }) + .map((candidate) => path.basename(candidate.path)) + .sort(), + [ + "custom-agent.jsonl", + "custom-data.jsonl", + "custom-session.jsonl", + "default-bb.jsonl", + "direct-override.jsonl", + "standalone.jsonl", + ].sort(), + "OMP's default store is not read unless explicitly enabled", + ); + } assert.deepEqual( pi - .enumerate() + .enumerate({ config: { discovery: { includeOmp: true } } }) .map((candidate) => path.basename(candidate.path)) .sort(), [ @@ -233,8 +525,11 @@ test("pi adapter enumerates standalone and BB-managed session roots without dupl "custom-session.jsonl", "default-bb.jsonl", "direct-override.jsonl", + "omp-standalone.jsonl", "standalone.jsonl", - ], + "Subagent.Child.jsonl", + "Subagent.jsonl", + ].sort(), ); }); diff --git a/test/analyze-reuse.test.js b/test/analyze-reuse.test.js index 96695143..fd52e702 100644 --- a/test/analyze-reuse.test.js +++ b/test/analyze-reuse.test.js @@ -5,7 +5,11 @@ import os from "node:os"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { spawnSync } from "node:child_process"; +import { assertNativeGapContributions } from "./helpers/gap-observers.js"; +import { resolveRepo } from "../src/repo.js"; +import { loadConfig } from "../src/config.js"; +import { discoverForRun } from "../src/commands/scan.js"; import { State } from "../src/state.js"; import { resolveMemoryFiles } from "../src/memory.js"; import { foldForRun } from "../src/commands/propose.js"; @@ -308,3 +312,153 @@ test("old-hash leftover evidence cannot change the current fold's session count, }); }); }); + +test("OMP analysis persists parent observer identity and fold restores it for legacy evidence", async () => { + const home = fs.mkdtempSync(path.join(os.tmpdir(), "backpass-omp-fold-home-")); + const dir = initRepo(MEMORY); + const sessionRoot = path.join(home, ".omp", "agent", "sessions", "-repo-demo"); + const parentName = "2026-08-27T00-00-00.000Z_parent-folder"; + const childPath = path.join(sessionRoot, parentName, "Subagent.jsonl"); + + const writeOmpTranscript = (file, id) => { + fs.mkdirSync(path.dirname(file), { recursive: true }); + const entries = [ + { type: "title", v: 1, title: "" }, + { type: "session", version: 3, id, timestamp: "2026-08-27T00:00:00.000Z", cwd: dir }, + { type: "message", message: { role: "user", content: "Please build the project." } }, + { type: "message", message: { role: "assistant", content: "Ran make build as instructed." } }, + { type: "message", message: { role: "user", content: "Now run the tests too." } }, + { type: "message", message: { role: "assistant", content: "Tests pass." } }, + ]; + fs.writeFileSync(file, `${entries.map((entry) => JSON.stringify(entry)).join("\n")}\n`); + }; + + writeOmpTranscript(path.join(sessionRoot, `${parentName}.jsonl`), "parent-native"); + writeOmpTranscript(path.join(sessionRoot, parentName, "Subagent.jsonl"), "child-native"); + const disabled = runAnalyze(dir, home); + assert.equal(disabled.status, 0, disabled.output); + assert.equal(JSON.parse(disabled.stdout).transcripts, 0, "the default CLI run does not read OMP sessions"); + fs.writeFileSync(path.join(dir, ".backpassrc.json"), JSON.stringify({ discovery: { includeOmp: true } })); + + const analyzed = runAnalyze(dir, home); + assert.equal(analyzed.status, 0, analyzed.output); + assert.equal(analyzed.summary.analyzed, 2, "the real analyzer writes evidence for parent and child"); + + const previousHome = process.env.HOME; + const previousUserProfile = process.env.USERPROFILE; + process.env.HOME = home; + process.env.USERPROFILE = home; + try { + const repo = resolveRepo(dir); + const config = loadConfig(dir, { discovery: { harnesses: ["pi"], since: "all", includeOmp: true } }); + const state = new State(dir).ensure(); + config.state = state; + const ctx = { repo, config, scope: null, strict: false, limit: null }; + const { transcripts } = await discoverForRun(ctx); + assert.equal(transcripts.length, 2, "discovery returns both OMP transcripts"); + assert.equal(new Set(transcripts.map((transcript) => transcript.corroborationIdentity)).size, 1); + + const evidence = state.listEvidence(); + assert.equal(evidence.length, 2); + + const memoryFile = resolveMemoryFiles(dir, ["AGENTS.md", "CLAUDE.md"]).primary; + const memoryHash = evidence[0].memoryHash; + const foldCtx = { repo, config: { ...config, minGapEvidence: 2, gapLedgerMaxAge: "90d" }, scope: null }; + + const current = await foldForRun(foldCtx, memoryFile, memoryHash, [], transcripts); + assert.equal(current.gaps.length, 0, "parent and child count as one observer, below the two-session threshold"); + assertNativeGapContributions(state.readGapLedger(), transcripts); + assert.ok( + evidence.every((record) => + transcripts.some( + (transcript) => + transcript.path === record.transcript.path && + transcript.corroborationIdentity === record.transcript.corroborationIdentity, + ), + ), + "analysis persists the observer identity needed by later folds", + ); + + const childEvidence = evidence.find((record) => record.transcript.path === childPath); + assert.ok(childEvidence); + for (const field of [ + "parentSessionId", + "corroborationIdentity", + "corroborationNativeId", + "corroborationStartedAt", + ]) { + delete childEvidence.transcript[field]; + } + state.writeEvidence(childEvidence.transcript, childEvidence); + + const reused = runAnalyze(dir, home); + assert.equal(reused.status, 0, reused.output); + assert.deepEqual([reused.summary.analyzed, reused.summary.cached], [0, 2]); + const reusedChild = state.listEvidence().find((record) => record.transcript.path === childPath); + assert.equal(reusedChild.transcript.parentSessionId, "parent-native"); + assert.equal(reusedChild.transcript.corroborationIdentity, transcripts[0].corroborationIdentity); + assert.equal(reusedChild.transcript.corroborationNativeId, "parent-native"); + + state.writeGapLedger({ version: 1, entries: {} }); + for (const record of evidence) { + const transcript = { ...record.transcript }; + delete transcript.parentSessionId; + delete transcript.corroborationIdentity; + delete transcript.corroborationNativeId; + delete transcript.corroborationStartedAt; + state.writeEvidence(transcript, { ...record, transcript }); + } + + const legacy = await foldForRun(foldCtx, memoryFile, memoryHash, [], transcripts); + assert.equal(legacy.gaps.length, 0, "selected discovery metadata restores identity for older evidence"); + assertNativeGapContributions(state.readGapLedger(), transcripts); + } finally { + if (previousHome === undefined) delete process.env.HOME; + else process.env.HOME = previousHome; + if (previousUserProfile === undefined) delete process.env.USERPROFILE; + else process.env.USERPROFILE = previousUserProfile; + } +}); + +test("OMP analysis labels missing-ancestor child and grandchild as automation without invented root provenance", () => { + const home = fs.mkdtempSync(path.join(os.tmpdir(), "backpass-omp-missing-analysis-")); + const dir = initRepo(MEMORY); + const sessionRoot = path.join(home, ".omp", "agent", "sessions", "-repo-demo"); + const files = [ + [path.join(sessionRoot, "root", "Child.jsonl"), "child-native"], + [path.join(sessionRoot, "other-root", "Missing", "Missing.Grandchild.jsonl"), "grandchild-native"], + ]; + for (const [file, id] of files) { + fs.mkdirSync(path.dirname(file), { recursive: true }); + const entries = [ + { type: "title", v: 1, title: "" }, + { type: "session", version: 3, id, timestamp: "2026-08-27T00:00:00.000Z", cwd: dir }, + { type: "message", message: { role: "user", content: "Please build the project." } }, + { type: "message", message: { role: "assistant", content: "Ran make build as instructed." } }, + { type: "message", message: { role: "user", content: "Now run the tests too." } }, + { type: "message", message: { role: "assistant", content: "Tests pass." } }, + ]; + fs.writeFileSync(file, `${entries.map((entry) => JSON.stringify(entry)).join("\n")}\n`); + } + fs.writeFileSync(path.join(sessionRoot, "root.jsonl"), "not a session header\n"); + fs.writeFileSync(path.join(dir, ".backpassrc.json"), JSON.stringify({ discovery: { includeOmp: true } })); + const analyzed = runAnalyze(dir, home); + assert.equal(analyzed.status, 0, analyzed.output); + assert.equal(analyzed.summary.analyzed, 2); + const state = new State(dir).ensure(); + const evidence = state.listEvidence(); + assert.equal(evidence.length, 2); + for (const record of evidence) { + assert.equal(record.transcript.interaction, "non-interactive"); + assert.equal(record.transcript.parentSessionId, null); + assert.equal(record.transcript.corroborationIdentity, record.transcript.identity); + assert.equal(record.transcript.corroborationNativeId, record.transcript.id.slice("pi-".length)); + assert.equal(record.transcript.corroborationStartedAt, record.transcript.startedAt); + record.transcript.interaction = "interactive"; + state.writeEvidence(record.transcript, record); + } + const reused = runAnalyze(dir, home); + assert.equal(reused.status, 0, reused.output); + assert.deepEqual([reused.summary.analyzed, reused.summary.cached], [0, 2]); + assert.ok(state.listEvidence().every((record) => record.transcript.interaction === "non-interactive")); +}); diff --git a/test/config.test.js b/test/config.test.js index d687d3cd..f7802a2c 100644 --- a/test/config.test.js +++ b/test/config.test.js @@ -178,6 +178,15 @@ test("--include-cursor-ide is the only way the deferred store is scanned", () => assert.ok(config.discovery.harnesses.includes("cursor-ide")); }); +test("OMP discovery rejects non-boolean opt-ins rather than treating them as enabled", () => { + for (const includeOmp of ["true", 1, null, []]) { + assert.throws( + () => loadConfig(tempRepo({ discovery: { includeOmp } })), + /config\.discovery\.includeOmp must be a boolean/, + ); + } +}); + test("unknown harness names are dropped rather than failing the run", () => { const config = loadConfig(tempRepo({ discovery: { harnesses: ["claude", "not-a-harness"] } })); assert.deepEqual(config.discovery.harnesses, ["claude"]); diff --git a/test/consolidate.test.js b/test/consolidate.test.js index d76dcb28..5b406ef7 100644 --- a/test/consolidate.test.js +++ b/test/consolidate.test.js @@ -150,3 +150,78 @@ test("fewer than two open gaps never spends a model call", async () => { const summary = await foldForRun(h.ctx, memoryFile, "h1", [], h.transcripts); assert.match(String(summary.consolidation.skipped), /fewer than two/); }); + +test("consolidation sees fresh native contributions and counts related observers once", async () => { + const oldWording = "Read docs/db.md before writing queries."; + const freshWording = "Read docs/db.md before writing any production queries."; + const oldChild = record("C", oldWording); + Object.assign(oldChild.transcript, { contentSignature: "child-v1" }); + oldChild.key = evidenceKey(oldChild.transcript, "h1"); + const h = harness([oldChild], { merges: [] }); + await foldForRun(h.ctx, memoryFile, "h1", [], [oldChild.transcript]); + + const parent = record("P", freshWording); + const child = record("C", freshWording); + Object.assign(child.transcript, { contentSignature: "child-v2", corroborationIdentity: "P" }); + child.key = evidenceKey(child.transcript, "h1"); + const independent = record("X", freshWording); + const unrelated = record("Y", UNRELATED); + const records = [parent, child, independent, unrelated]; + for (const evidence of records) h.state.writeEvidence(evidence.transcript.id, evidence); + await foldForRun( + h.ctx, + memoryFile, + "h1", + [], + records.map((evidence) => evidence.transcript), + ); + + const prompt = fs.readFileSync(path.join(h.state.root, "prompts", "consolidate-gaps.md"), "utf8"); + assert.ok(prompt.includes(`(sessions=2) ${freshWording}`)); + assert.ok(!prompt.includes(oldWording)); +}); + +test("restored ancestry resolves consolidation observers without refreshing stale analysis history", async () => { + const parent = record("P", SIGHTING_A); + const child = record("C", SIGHTING_A); + Object.assign(child.transcript, { corroborationIdentity: "P", contentSignature: "unchanged-child" }); + child.key = evidenceKey(child.transcript, "h1"); + const h = harness([parent, child], { merges: [] }); + await foldForRun(h.ctx, memoryFile, "h1", [], [parent.transcript, child.transcript]); + + const lostParent = { ...child, transcript: { ...child.transcript, corroborationIdentity: "C" } }; + lostParent.key = evidenceKey(lostParent.transcript, "h1"); + h.state.writeEvidence(lostParent.transcript.id, lostParent); + await foldForRun(h.ctx, memoryFile, "h1", [], [lostParent.transcript]); + const history = structuredClone( + Object.values(h.state.readGapLedger().entries) + .flatMap((entry) => Object.values(entry.sessions)) + .find((observation) => observation.sourceSessionId === "C"), + ); + assert.equal(history.observerId, "C"); + + const fresh = [record("P", SIGHTING_A), record("X", SIGHTING_A), record("Y", UNRELATED)]; + for (const evidence of fresh) { + evidence.memoryHash = "h2"; + evidence.key = evidenceKey(evidence.transcript, "h2"); + h.state.writeEvidence(evidence.transcript.id, evidence); + } + const currentChild = { ...lostParent.transcript, corroborationIdentity: "P" }; + const summary = await foldForRun( + h.ctx, + memoryFile, + "h2", + [], + [...fresh.map((evidence) => evidence.transcript), currentChild], + ); + assert.equal(summary.analyzedSessions, 3, "the child's h1 analysis remains stale"); + assert.equal(summary.gaps[0].sessions, 2); + assert.equal(summary.sourceObservers[history.source], "P"); + const prompt = fs.readFileSync(path.join(h.state.root, "prompts", "consolidate-gaps.md"), "utf8"); + assert.ok(prompt.includes(`(sessions=2) ${SIGHTING_A}`)); + assert.ok(!prompt.includes(`(sessions=3) ${SIGHTING_A}`)); + const durable = Object.values(h.state.readGapLedger().entries) + .flatMap((entry) => Object.values(entry.sessions)) + .find((observation) => observation.sourceSessionId === "C"); + assert.deepEqual(durable, history, "resolving the view must preserve every durable contribution stamp"); +}); diff --git a/test/fixtures/omp-session.jsonl b/test/fixtures/omp-session.jsonl new file mode 100644 index 00000000..7ee32648 --- /dev/null +++ b/test/fixtures/omp-session.jsonl @@ -0,0 +1,6 @@ +{"type":"title","v":1,"title":"","updatedAt":"2026-08-03T08:00:00.000Z","pad":" "} +{"type":"session","version":3,"id":"omp-5678","timestamp":"2026-08-03T08:00:00.000Z","cwd":"/repo/demo"} +{"type":"model_change","id":"m1","parentId":null,"timestamp":"2026-08-03T08:00:00.100Z","model":"cursor/composer-2.5","resolvedModelIsFallback":false} +{"type":"message","id":"e1","parentId":"m1","timestamp":"2026-08-03T08:00:01.000Z","message":{"role":"user","content":[{"type":"text","text":"Add the changelog entry."}]}} +{"type":"message","id":"e2","parentId":"e1","timestamp":"2026-08-03T08:00:02.000Z","message":{"role":"assistant","content":[{"type":"thinking","thinking":"internal reasoning that must be dropped"},{"type":"text","text":"Editing CHANGELOG.md."},{"type":"toolCall","id":"tc1","name":"bash","arguments":{"command":"git status"}}]}} +{"type":"message","id":"e3","parentId":"e2","timestamp":"2026-08-03T08:00:03.000Z","message":{"role":"toolResult","toolCallId":"tc1","content":[{"type":"text","text":"nothing to commit"}]}} diff --git a/test/fold.test.js b/test/fold.test.js index 39147622..bdb67661 100644 --- a/test/fold.test.js +++ b/test/fold.test.js @@ -145,6 +145,183 @@ test("current records and legacy gap observations share one label per session", assert.deepEqual(new Set(Object.values(summary.sourceProjects)), new Set(["repo-a", "repo-b"])); }); +test("direct fold rejects a selected representative's inconsistent historical observer before clustering", () => { + const gap = "Read database documentation before writing queries."; + const current = (id, observer) => + record(id, { + transcript: { + id, + identity: id, + nativeId: id, + harness: "pi", + corroborationIdentity: observer, + startedAt: Date.parse("2026-08-01T00:00:00Z"), + }, + negative: [{ instruction: "AG-001", quote: `current mistake ${id}`, class: "non-compliance" }], + }); + const historical = { + proposedInstruction: gap, + sessionId: "P", + sourceSessionId: "G", + sightingIds: ["P", "G"], + source: "pi · G · 2026-08-01", + quote: "historical G quote", + }; + const independent = { + proposedInstruction: gap, + sessionId: "X", + sourceSessionId: "X", + sightingIds: ["X"], + source: "pi · X · 2026-08-01", + quote: "independent X quote", + }; + const route = { weight: null, rootPath: "AGENTS.md", ownerOf: () => null }; + for (const matchingGap of [false, true]) { + const fresh = { + proposedInstruction: gap, + sessionId: "L", + sourceSessionId: "G", + sightingIds: ["G"], + source: historical.source, + quote: "fresh G quote", + }; + const summary = foldEvidence([current("P", "P"), current("G", "L"), current("X", "X")], { + minGapEvidence: 2, + memoryFile, + route, + gapObservations: [historical, independent, ...(matchingGap ? [fresh] : [])], + }); + assert.equal(summary.sourceObservers[historical.source], "L"); + assert.equal(summary.totals.gapSightings, matchingGap ? 2 : 1); + assert.equal(summary.gaps.length, matchingGap ? 1 : 0); + if (matchingGap) { + assert.equal(summary.gaps[0].sessions, 2); + assert.deepEqual(summary.gaps[0].quotes.map((quote) => quote.text).sort(), [ + "fresh G quote", + "independent X quote", + ]); + } + assert.ok(!summary.rootOwnedGaps.flat().some((item) => item.sessionId === "P")); + } + const ambiguous = foldEvidence([current("G", "L"), current("G", "M")], { + gapObservations: [{ ...historical, sessionId: "L" }, independent], + minGapEvidence: 2, + }); + assert.equal(ambiguous.totals.gapSightings, 1); + assert.deepEqual(ambiguous.gaps, []); + + for (const representativeSelected of [false, true]) { + const summary = foldEvidence( + [current("P", "P"), current("X", "X"), ...(representativeSelected ? [current("G", "L")] : [])], + { + gapObservations: [{ ...historical, sessionId: representativeSelected ? "L" : "P" }, independent], + minGapEvidence: 2, + route, + }, + ); + assert.equal(summary.gaps[0].sessions, 2); + assert.equal(summary.sourceObservers[historical.source], representativeSelected ? "L" : "P"); + } +}); + +test("direct fold resolves retained native aliases without admitting incompatible occupied observers", () => { + const gap = "Read database documentation before writing queries."; + const current = (id, observer) => + record(id, { + transcript: { + id, + identity: id, + nativeId: id, + harness: "pi", + corroborationIdentity: observer, + startedAt: Date.parse("2026-08-01T00:00:00Z"), + }, + }); + const historical = { + proposedInstruction: gap, + sessionId: "P", + sourceSessionId: "G", + sightingIds: ["P", "G"], + source: "pi · G · 2026-08-01", + quote: "historical G quote", + }; + const native = { + proposedInstruction: gap, + sessionId: "H", + sourceSessionId: "H", + sightingIds: ["H"], + source: "pi · H · 2026-08-01", + quote: "valid H quote", + domain: "orchestration", + }; + const independent = { + proposedInstruction: gap, + sessionId: "X", + sourceSessionId: "X", + sightingIds: ["X"], + source: "pi · X · 2026-08-01", + quote: "valid X quote", + }; + const duplicate = { ...native, quote: "second valid H quote", domain: "project" }; + const summary = foldEvidence([current("P", "P"), current("G", "L"), current("H", "P"), current("X", "X")], { + minGapEvidence: 2, + gapObservations: [historical, native, duplicate, independent], + route: { weight: null, rootPath: "AGENTS.md", ownerOf: () => null }, + }); + assert.equal(summary.gaps[0].sessions, 2); + assert.deepEqual(summary.gaps[0].quotes.map((quote) => quote.text).sort(), ["valid H quote", "valid X quote"]); + assert.equal(summary.sourceObservers[native.source], "P"); + const singleton = foldEvidence([current("H", "P")], { + minGapEvidence: 2, + gapObservations: [native, duplicate], + }); + assert.deepEqual(singleton.gaps, []); + assert.equal(singleton.totals.droppedGapSingletons, 1); +}); + +test("direct fold resolves source-qualified collision records to one validated observer", () => { + const gap = "Read database documentation before writing queries."; + const current = (id, observer) => + record(id, { + transcript: { + id, + identity: id, + nativeId: id, + harness: "pi", + corroborationIdentity: observer, + startedAt: Date.parse("2026-08-01T00:00:00Z"), + }, + }); + const fresh = { + proposedInstruction: gap, + sessionId: JSON.stringify(["P", "P"]), + sourceSessionId: "P", + sightingIds: ["P"], + source: "pi · P · 2026-08-01", + quote: "fresh P quote", + }; + const independent = { + proposedInstruction: gap, + sessionId: "X", + sourceSessionId: "X", + sightingIds: ["X"], + source: "pi · X · 2026-08-01", + quote: "fresh X quote", + }; + const summary = foldEvidence([current("P", "P"), current("X", "X")], { + gapObservations: [fresh, independent], + minGapEvidence: 2, + }); + assert.equal(summary.gaps[0].sessions, 2); + assert.equal(summary.sourceObservers[fresh.source], "P"); + const singleton = foldEvidence([current("P", "P")], { + gapObservations: [fresh, { ...fresh, sessionId: "P" }], + minGapEvidence: 2, + }); + assert.deepEqual(singleton.gaps, []); + assert.equal(singleton.totals.droppedGapSingletons, 1); +}); + test("instructions with no evidence still appear - they are the removal candidates", () => { const summary = foldEvidence([record("s1", { positive: [{ instruction: "AG-001", quote: "q" }] })], { memoryFile }); @@ -532,6 +709,75 @@ test("harm-class negatives are counted per distinct session, and only explicit h assert.equal(rows.get("AG-001").negative, 4); assert.equal(rows.get("AG-002").harmSessions, 0, "non-compliance and unclassified never count as harm"); }); +test("OMP subagents share corroboration while relevance remains per file", () => { + const startedAt = Date.parse("2026-08-01T00:00:00Z"); + const parent = { + id: "pi-parent", + nativeId: "parent-native", + identity: "pi-file-parent", + corroborationIdentity: "pi-parent-session", + corroborationNativeId: "parent-native", + corroborationStartedAt: startedAt, + harness: "pi", + startedAt, + interaction: "interactive", + }; + const child = { + id: "pi-child", + nativeId: "child-native", + identity: "pi-file-child", + parentSessionId: "parent-native", + corroborationIdentity: "pi-parent-session", + corroborationNativeId: "parent-native", + corroborationStartedAt: startedAt, + harness: "pi", + startedAt: startedAt + 86_400_000, + interaction: "non-interactive", + }; + const independent = { + id: "pi-independent", + nativeId: "independent-native", + identity: "pi-file-independent", + corroborationIdentity: "pi-independent-session", + corroborationNativeId: "independent-native", + corroborationStartedAt: startedAt + 2_000, + harness: "pi", + startedAt: startedAt + 2_000, + interaction: "interactive", + }; + const observed = (transcript) => + record(transcript.id, { + transcript, + negative: [ + { instruction: "AG-001", quote: `harm ${transcript.id}`, class: "harm" }, + { instruction: "AG-002", quote: `ignored ${transcript.id}`, class: "non-compliance" }, + ], + gaps: [{ proposedInstruction: "Read the deployment runbook first.", quote: `gap ${transcript.id}` }], + }); + + const parentAndChild = foldEvidence([observed(parent), observed(child)], { minGapEvidence: 2, memoryFile }); + const oneObserver = new Map(parentAndChild.instructions.map((row) => [row.instruction, row])); + assert.equal(parentAndChild.gaps.length, 0, "parent and subagent cannot clear the two-session floor"); + assert.equal(parentAndChild.totals.droppedGapSingletons, 1); + assert.deepEqual(parentAndChild.sources, ["pi · parent-native · 2026-08-01", "pi · child-native · 2026-08-02"]); + assert.deepEqual(Object.values(parentAndChild.sourceObservers), ["pi-parent-session", "pi-parent-session"]); + assert.equal(oneObserver.get("AG-001").harmSessions, 1); + assert.equal(oneObserver.get("AG-002").nonComplianceSessions, 1); + assert.equal(oneObserver.get("AG-001").sessions, 2, "relevance still measures both analyzed files"); + assert.equal(oneObserver.get("AG-001").relevance, 1); + + const independentlyCorroborated = foldEvidence([observed(parent), observed(child), observed(independent)], { + minGapEvidence: 2, + memoryFile, + }); + const twoObservers = new Map(independentlyCorroborated.instructions.map((row) => [row.instruction, row])); + assert.equal(independentlyCorroborated.gaps.length, 1); + assert.equal(independentlyCorroborated.gaps[0].sessions, 2); + assert.equal(independentlyCorroborated.sources.length, 3); + assert.equal(twoObservers.get("AG-001").harmSessions, 2); + assert.equal(twoObservers.get("AG-002").nonComplianceSessions, 2); + assert.equal(twoObservers.get("AG-001").sessions, 3); +}); test("failed-trigger citations count per skill and reach the synthesis prompt with the cluster", () => { const covered = (id, phrasing) => diff --git a/test/gap-ledger.test.js b/test/gap-ledger.test.js index 417ed8c7..5d1ccdb7 100644 --- a/test/gap-ledger.test.js +++ b/test/gap-ledger.test.js @@ -14,7 +14,11 @@ import { mergeGapEntries, pruneGapLedger, recordGapObservations, + normalizeGapLedgerSessions, } from "../src/gap-ledger.js"; +import { routingFor, workedPaths } from "../src/nested.js"; +import { buildProposal } from "../src/proposal.js"; +import { makeRepo, stageAndMeasure, writeIn } from "./helpers/staging.js"; const MEMORY_PATH = "AGENTS.md"; const DAY = 86_400_000; @@ -45,12 +49,26 @@ function record(id, gaps, { startedAt = Date.parse("2026-08-01T00:00:00Z"), memo /** A throwaway `.backpass/` and a ctx shaped like the one `foldForRun` reads. */ function harness(overrides = {}) { - const root = fs.mkdtempSync(path.join(os.tmpdir(), "backpass-ledger-")); + const root = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), "backpass-ledger-"))); const state = new State(root).ensure(); const ctx = { config: { state, minGapEvidence: 2, gapLedgerMaxAge: "90d", ...overrides } }; return { root, state, ctx }; } +function combineLegacyObserver(ledger, observer, representative) { + for (const entry of Object.values(ledger.entries)) { + const prior = entry.sessions[observer]; + const contribution = entry.sessions[representative]; + entry.sessions[observer] = { + ...contribution, + firstObservedAt: prior.firstObservedAt, + sightingIds: [...new Set([...prior.sightingIds, ...contribution.sightingIds])], + phrasings: [...new Set([...prior.phrasings, ...contribution.phrasings])], + }; + delete entry.sessions[representative]; + } +} + /** One backpass run: the evidence files on disk at the time, then the fold. */ async function run(h, records, file = memoryFile(), memoryHash = "h1") { for (const r of records) h.state.writeEvidence(r.transcript.id, r); @@ -113,6 +131,554 @@ test("persisted selected gap sources remain fold-issued without fresh evidence", } }); +test("selected native provenance migrates durable observers without a fresh matching gap", async () => { + for (const [oldObserver, currentObserver] of [ + ["child-observer", "ancestor-observer"], + ["old-host:observer", "canonical-host:observer"], + ]) { + for (const reanalyzed of [false, true]) { + const h = harness({ gapLedgerMaxAge: "all" }); + const native = (id, observer, gaps, memoryHash) => { + const evidence = record(id, gaps, { memoryHash }); + evidence.transcript = { + ...evidence.transcript, + harness: "pi", + nativeId: id, + corroborationIdentity: observer, + }; + evidence.key = evidenceKey(evidence.transcript, memoryHash); + return evidence; + }; + const old = native("G", oldObserver, [{ proposedInstruction: GAP, domain: "orchestration" }], "h1"); + await run(h, [old]); + const entryId = gapEntryId(MEMORY_PATH, GAP); + const before = h.state.readGapLedger().entries[entryId].sessions[oldObserver]; + const current = native("G", currentObserver, [], "h2"); + const independent = native("X", "independent-observer", [GAP_REPHRASED], "h2"); + if (reanalyzed) h.state.writeEvidence(current.transcript.id, current); + h.state.writeEvidence(independent.transcript.id, independent); + const summary = await foldForRun(h.ctx, memoryFile(), "h2", [], [current.transcript, independent.transcript]); + assert.equal(summary.analyzedSessions, reanalyzed ? 2 : 1); + assert.equal(summary.gaps.length, 1); + assert.equal(summary.gaps[0].sessions, 2); + assert.deepEqual(summary.gaps[0].quotes.map((quote) => quote.text).sort(), ["quote from G", "quote from X"]); + const sessions = h.state.readGapLedger().entries[entryId].sessions; + assert.equal(sessions[oldObserver], undefined); + assert.equal(sessions[currentObserver].firstObservedAt, before.firstObservedAt); + assert.equal(sessions[currentObserver].observedAt, before.observedAt); + assert.equal(sessions[currentObserver].memoryHash, "h1"); + assert.equal(sessions[currentObserver].sourceSessionId, "G"); + assert.deepEqual(sessions[currentObserver].sightingIds, ["G"]); + assert.equal(sessions[currentObserver].domain, "orchestration"); + } + } +}); + +test("observer migration requires complete selected unambiguous native provenance", () => { + const selected = (identity, observer) => ({ + id: `pi-${identity}`, + identity, + harness: "pi", + corroborationIdentity: observer, + }); + const cases = [ + { name: "convergent", transcripts: [selected("G", "R"), selected("H", "R")], move: true }, + { name: "unselected sighting", transcripts: [selected("G", "R")], move: false }, + { name: "unselected representative", transcripts: [selected("H", "R")], move: false }, + { name: "divergent", transcripts: [selected("G", "R"), selected("H", "S")], move: false }, + { + name: "ambiguous native", + transcripts: [selected("G", "R"), selected("G", "S"), selected("H", "R")], + move: false, + }, + { + name: "unattributed", + transcripts: [selected("G", "R"), selected("H", "R")], + move: false, + unattributedSightings: true, + }, + ]; + for (const scenario of cases) { + for (const oldKey of ["C", "G"]) { + const entryId = gapEntryId(MEMORY_PATH, GAP); + const observation = { + firstObservedAt: "2026-08-01T00:00:00Z", + observedAt: "2026-08-03T00:00:00Z", + source: "pi · G · 2026-08-01", + sourceSessionId: "G", + sightingIds: ["H"], + quote: "representative quote", + domain: "project", + ...(scenario.unattributedSightings ? { unattributedSightings: true } : {}), + }; + const ledger = { + version: 1, + entries: { + [entryId]: { + id: entryId, + memoryPath: MEMORY_PATH, + proposedInstruction: GAP, + sessions: { [oldKey]: structuredClone(observation) }, + }, + }, + }; + normalizeGapLedgerSessions(ledger, scenario.transcripts); + const observations = ledgerGapObservations(ledger, MEMORY_PATH); + assert.equal(observations.length, 1, scenario.name); + assert.equal(observations[0].sessionId, scenario.move ? "R" : oldKey, scenario.name); + const summary = foldEvidence([], { + minGapEvidence: 1, + gapObservations: observations.filter((observation) => observation.sessionId === "R"), + }); + assert.equal(summary.gaps.length, scenario.move ? 1 : 0, scenario.name); + if (!scenario.move) assert.deepEqual(ledger.entries[entryId].sessions[oldKey], observation); + else { + assert.equal(observations[0].sourceSessionId, "G"); + assert.deepEqual(observations[0].sightingIds, ["H"]); + } + } + } +}); + +test("foldForRun leaves ambiguous and incompletely selected old observers outside corroboration", async () => { + const native = (identity, observer) => ({ + id: identity, + identity, + harness: "pi", + corroborationIdentity: observer, + startedAt: Date.parse("2026-08-01T00:00:00Z"), + interaction: "interactive", + }); + const scenarios = [ + { selected: [native("G", "R")], sightingIds: ["G", "H"] }, + { selected: [native("G", "R"), native("H", "S")], sightingIds: ["G", "H"] }, + { selected: [native("G", "R"), native("G", "S")], sightingIds: ["G"] }, + { selected: [native("G", "R")], sightingIds: ["G"], unattributedSightings: true }, + ]; + for (const scenario of scenarios) { + const h = harness({ gapLedgerMaxAge: "all" }); + const entryId = gapEntryId(MEMORY_PATH, GAP); + const old = { + observedAt: "2026-08-01T00:00:00Z", + source: "pi · G · 2026-08-01", + sourceSessionId: "G", + sightingIds: scenario.sightingIds, + quote: "old G quote", + ...(scenario.unattributedSightings ? { unattributedSightings: true } : {}), + }; + h.state.writeGapLedger({ + version: 1, + entries: { + [entryId]: { id: entryId, memoryPath: MEMORY_PATH, proposedInstruction: GAP, sessions: { C: old } }, + }, + }); + const independent = record("X", [GAP], { memoryHash: "h2" }); + h.state.writeEvidence(independent.transcript.id, independent); + const summary = await foldForRun(h.ctx, memoryFile(), "h2", [], [...scenario.selected, independent.transcript]); + assert.equal(summary.analyzedSessions, 1); + assert.deepEqual(summary.gaps, []); + assert.deepEqual(h.state.readGapLedger().entries[entryId].sessions.C, old); + assert.equal(h.state.readGapLedger().entries[entryId].sessions.R, undefined); + } +}); + +test("a selected representative cannot grant its former selected observer to one quote", async () => { + const native = (id, observer, gaps, memoryHash) => { + const evidence = record(id, gaps, { memoryHash }); + evidence.transcript = { + ...evidence.transcript, + harness: "pi", + nativeId: id, + corroborationIdentity: observer, + }; + evidence.key = evidenceKey(evidence.transcript, memoryHash); + return evidence; + }; + for (const parentFresh of [false, true]) { + for (const matchingGap of [false, true]) { + const h = harness({ gapLedgerMaxAge: "all" }); + const ledger = { version: 1, entries: {} }; + recordGapObservations(ledger, [native("P", "P", [GAP], "h1"), native("G", "P", [GAP], "h1")]); + combineLegacyObserver(ledger, "P", "G"); + const entryId = gapEntryId(MEMORY_PATH, GAP); + const historical = structuredClone(ledger.entries[entryId].sessions.P); + assert.equal(historical.sourceSessionId, "G"); + assert.deepEqual(historical.sightingIds, ["P", "G"]); + h.state.writeGapLedger(ledger); + + const parent = native("P", "P", [], parentFresh ? "h2" : "h1"); + const representative = native("G", "L", matchingGap ? [GAP] : [], "h2"); + representative.negative = [ + { + instruction: "AG-001", + quote: "G skipped the database documentation", + class: "non-compliance", + }, + ]; + const independent = native("X", "X", [GAP], "h2"); + for (const evidence of [parent, representative, independent]) { + h.state.writeEvidence(evidence.transcript.id, evidence); + } + const route = { weight: null, rootPath: MEMORY_PATH, ownerOf: () => null }; + const summary = await foldForRun( + h.ctx, + memoryFile(), + "h2", + [], + [parent.transcript, representative.transcript, independent.transcript], + { route }, + ); + + assert.equal(summary.analyzedSessions, parentFresh ? 3 : 2); + assert.equal(summary.totals.gapSightings, matchingGap ? 2 : 1); + assert.equal(summary.gaps.length, matchingGap ? 1 : 0); + if (matchingGap) { + assert.equal(summary.gaps[0].sessions, 2); + assert.deepEqual(summary.gaps[0].quotes.map((quote) => quote.text).sort(), ["quote from G", "quote from X"]); + } + const quote = summary.instructions.find((row) => row.instruction === "AG-001").quotes[0]; + assert.equal(summary.sourceObservers[quote.source], "L"); + assert.ok(!summary.rootOwnedGaps.flat().some((item) => item.sessionId === "P")); + assert.deepEqual(h.state.readGapLedger().entries[entryId].sessions.P, historical); + + const repo = makeRepo({ [MEMORY_PATH]: "# T\n\n- Run pnpm test before pushing.\n- Keep the README current.\n" }); + const staged = stageAndMeasure({ + repo, + edit: (root) => writeIn(root, MEMORY_PATH, (text) => `${text}- ${GAP}\n`), + }); + const built = buildProposal( + { + edits: [ + { + changes: staged.measured.changes.map((change) => change.id), + kind: "add", + title: "Read the database documentation", + rationale: "Avoid re-deriving query behavior", + evidence: [{ polarity: "negative", text: quote.text, source: quote.source }], + }, + ], + }, + { + repo, + memoryFile: staged.memoryFile, + measured: staged.measured, + summary, + config: { ...h.ctx.config, budgetTokens: 5000, maxEditsPerRun: 5, skillsDir: ".agents/skills" }, + }, + ); + assert.equal(built.proposal.edits.length, 0); + assert.ok(built.violations.some((violation) => /backed by 1 session\(s\); 2 are required/.test(violation))); + } + } +}); + +test("an occupied incompatible observer preserves its native alias through normalization and recording", () => { + const native = (id, observer, gaps = []) => { + const evidence = record(id, gaps); + evidence.transcript = { ...evidence.transcript, harness: "pi", nativeId: id, corroborationIdentity: observer }; + evidence.key = evidenceKey(evidence.transcript, evidence.memoryHash); + return evidence; + }; + const selected = [native("P", "P"), native("G", "L"), native("H", "P"), native("X", "X")]; + const ledger = { version: 1, entries: {} }; + recordGapObservations(ledger, [native("P", "P", [GAP]), native("G", "P", [GAP]), native("H", "H", [GAP])], { + now: new Date("2026-08-02T00:00:00Z"), + }); + combineLegacyObserver(ledger, "P", "G"); + const entryId = gapEntryId(MEMORY_PATH, GAP); + const historical = structuredClone(ledger.entries[entryId].sessions.P); + const valid = structuredClone(ledger.entries[entryId].sessions.H); + normalizeGapLedgerSessions( + ledger, + selected.map((evidence) => evidence.transcript), + ); + assert.deepEqual(ledger.entries[entryId].sessions.P, historical); + assert.deepEqual(ledger.entries[entryId].sessions.H, valid); + recordGapObservations(ledger, [native("H", "P", [GAP])], { + now: new Date("2026-08-03T00:00:00Z"), + }); + assert.deepEqual( + ledger.entries[entryId].sessions.P, + historical, + "standalone recording also rejects unresolved destinations", + ); + assert.deepEqual(ledger.entries[entryId].sessions.H.sightingIds, ["H"]); + recordGapObservations(ledger, [native("H", "P", [GAP])], { + transcripts: selected.map((evidence) => evidence.transcript), + now: new Date("2026-08-03T00:00:00Z"), + }); + assert.deepEqual(ledger.entries[entryId].sessions.P, historical); + assert.equal(ledger.entries[entryId].sessions.H.firstObservedAt, valid.firstObservedAt); + assert.deepEqual(ledger.entries[entryId].sessions.H.sightingIds, ["H"]); +}); + +test("unresolved occupied destinations do not absorb valid selected native aliases", () => { + const native = (id, observer) => ({ + id, + identity: id, + harness: "pi", + corroborationIdentity: observer, + startedAt: Date.parse("2026-08-01T00:00:00Z"), + interaction: "interactive", + }); + for (const scenario of [ + { selected: [native("H", "P")] }, + { selected: [native("H", "P"), native("G", "P"), native("G", "L")] }, + { selected: [native("H", "P"), native("G", "P")], unattributedSightings: true }, + ]) { + const entryId = gapEntryId(MEMORY_PATH, GAP); + const unresolved = { + observedAt: "2026-08-01T00:00:00Z", + sourceSessionId: "G", + sightingIds: ["G"], + source: "pi · G · 2026-08-01", + quote: "unresolved G quote", + ...(scenario.unattributedSightings ? { unattributedSightings: true } : {}), + }; + const valid = { + observedAt: "2026-08-02T00:00:00Z", + sourceSessionId: "H", + sightingIds: ["H"], + source: "pi · H · 2026-08-01", + quote: "valid H quote", + }; + const ledger = { + version: 1, + entries: { + [entryId]: { + id: entryId, + memoryPath: MEMORY_PATH, + proposedInstruction: GAP, + sessions: { P: structuredClone(unresolved), H: structuredClone(valid) }, + }, + }, + }; + normalizeGapLedgerSessions(ledger, scenario.selected); + assert.deepEqual(ledger.entries[entryId].sessions.P, unresolved); + assert.deepEqual(ledger.entries[entryId].sessions.H, valid); + const fresh = { ...record("H", [GAP]), transcript: scenario.selected[0] }; + recordGapObservations(ledger, [fresh], { transcripts: scenario.selected }); + assert.deepEqual(ledger.entries[entryId].sessions.P, unresolved); + assert.deepEqual(ledger.entries[entryId].sessions.H.sightingIds, ["H"]); + } +}); + +test("selected native history corroborates separately from an incompatible occupied observer on disk", async () => { + for (const reanalyzed of [false, true]) { + for (const matchingGap of [false, true]) { + const h = harness({ gapLedgerMaxAge: "all" }); + const native = (id, observer, gaps, memoryHash) => { + const evidence = record(id, gaps, { memoryHash }); + evidence.transcript = { + ...evidence.transcript, + harness: "pi", + nativeId: id, + corroborationIdentity: observer, + }; + evidence.key = evidenceKey(evidence.transcript, memoryHash); + return evidence; + }; + const ledger = { version: 1, entries: {} }; + recordGapObservations( + ledger, + [native("P", "P", [GAP], "h1"), native("G", "P", [GAP], "h1"), native("H", "H", [GAP], "h1")], + { now: new Date("2026-08-02T00:00:00Z") }, + ); + combineLegacyObserver(ledger, "P", "G"); + const entryId = gapEntryId(MEMORY_PATH, GAP); + const historical = structuredClone(ledger.entries[entryId].sessions.P); + const valid = structuredClone(ledger.entries[entryId].sessions.H); + h.state.writeGapLedger(ledger); + const current = [ + native("P", "P", [], reanalyzed ? "h2" : "h1"), + native("G", "L", [], reanalyzed ? "h2" : "h1"), + native("H", "P", matchingGap ? [GAP] : [], reanalyzed || matchingGap ? "h2" : "h1"), + native("X", "X", [GAP], "h2"), + ]; + for (const evidence of current) h.state.writeEvidence(evidence.transcript.id, evidence); + const summary = await foldForRun( + h.ctx, + memoryFile(), + "h2", + [], + current.map((evidence) => evidence.transcript), + { + route: { weight: null, rootPath: MEMORY_PATH, ownerOf: () => null }, + }, + ); + assert.equal(summary.analyzedSessions, reanalyzed ? 4 : matchingGap ? 2 : 1); + assert.equal(summary.totals.gapSightings, 2); + assert.equal(summary.gaps[0].sessions, 2); + assert.deepEqual(summary.gaps[0].quotes.map((quote) => quote.text).sort(), ["quote from H", "quote from X"]); + assert.equal(summary.sourceObservers["pi · H · 2026-08-01"], "P"); + assert.ok(!summary.rootOwnedGaps.flat().some((item) => /quote from [PG]$/.test(item.quote))); + const sessions = h.state.readGapLedger().entries[entryId].sessions; + assert.deepEqual(sessions.P, historical); + if (!matchingGap) assert.deepEqual(sessions.H, valid); + else { + assert.equal(sessions.H.firstObservedAt, valid.firstObservedAt); + assert.deepEqual(sessions.H.sightingIds, ["H"]); + } + const repo = makeRepo({ [MEMORY_PATH]: "# T\n\n- Run pnpm test before pushing.\n- Keep the README current.\n" }); + const staged = stageAndMeasure({ + repo, + edit: (root) => writeIn(root, MEMORY_PATH, (text) => `${text}- ${GAP}\n`), + }); + const build = (quotes) => + buildProposal( + { + edits: [ + { + changes: staged.measured.changes.map((change) => change.id), + kind: "add", + title: "Read the database documentation", + rationale: "Avoid re-deriving query behavior", + evidence: quotes.map((quote) => ({ polarity: "negative", ...quote })), + }, + ], + }, + { + repo, + memoryFile: staged.memoryFile, + measured: staged.measured, + summary, + config: { ...h.ctx.config, budgetTokens: 5000, maxEditsPerRun: 5, skillsDir: ".agents/skills" }, + }, + ); + assert.equal(build(summary.gaps[0].quotes).proposal.edits.length, 1); + const hQuote = summary.gaps[0].quotes.find((quote) => quote.text === "quote from H"); + const duplicate = build([hQuote, hQuote]); + assert.equal(duplicate.proposal.edits.length, 0); + assert.ok(duplicate.violations.some((violation) => /backed by 1 session\(s\); 2 are required/.test(violation))); + } + } +}); + +test("fresh native evidence retains a deterministic sibling when its own observer slot is incompatible", async () => { + const h = harness({ gapLedgerMaxAge: "all" }); + const native = (id, observer, gaps, memoryHash) => { + const evidence = record(id, gaps, { memoryHash }); + evidence.transcript = { ...evidence.transcript, harness: "pi", nativeId: id, corroborationIdentity: observer }; + evidence.key = evidenceKey(evidence.transcript, memoryHash); + return evidence; + }; + const ledger = { version: 1, entries: {} }; + recordGapObservations(ledger, [native("P", "P", [GAP], "h1"), native("G", "P", [GAP], "h1")]); + combineLegacyObserver(ledger, "P", "G"); + const entryId = gapEntryId(MEMORY_PATH, GAP); + const historical = structuredClone(ledger.entries[entryId].sessions.P); + h.state.writeGapLedger(ledger); + const current = [native("P", "P", [GAP], "h2"), native("G", "L", [], "h2"), native("X", "X", [GAP], "h2")]; + for (const evidence of current) h.state.writeEvidence(evidence.transcript.id, evidence); + for (let run = 0; run < 2; run += 1) { + const summary = await foldForRun( + h.ctx, + memoryFile(), + "h2", + [], + current.map((evidence) => evidence.transcript), + ); + assert.equal(summary.gaps[0].sessions, 2); + assert.equal(summary.totals.gapSightings, 2); + assert.deepEqual(summary.gaps[0].quotes.map((quote) => quote.text).sort(), ["quote from P", "quote from X"]); + const sessions = h.state.readGapLedger().entries[entryId].sessions; + assert.deepEqual(sessions.P, historical); + assert.equal(Object.keys(sessions).length, 3); + assert.deepEqual(sessions[JSON.stringify(["P", "P"])].sightingIds, ["P"]); + } +}); + +test("selected historical sightings remain admitted when their representative is not selected", async () => { + const h = harness({ gapLedgerMaxAge: "all" }); + const entryId = gapEntryId(MEMORY_PATH, GAP); + const historical = { + observedAt: "2026-08-01T00:00:00Z", + source: "pi · G · 2026-08-01", + sourceSessionId: "G", + sightingIds: ["P", "G"], + quote: "historical G quote", + }; + h.state.writeGapLedger({ + version: 1, + entries: { + [entryId]: { id: entryId, memoryPath: MEMORY_PATH, proposedInstruction: GAP, sessions: { P: historical } }, + }, + }); + const parent = record("P", [], { memoryHash: "h2" }); + parent.transcript = { ...parent.transcript, harness: "pi", corroborationIdentity: "P" }; + parent.key = evidenceKey(parent.transcript, "h2"); + const independent = record("X", [GAP], { memoryHash: "h2" }); + for (const evidence of [parent, independent]) h.state.writeEvidence(evidence.transcript.id, evidence); + const route = { weight: null, rootPath: MEMORY_PATH, ownerOf: () => null }; + const summary = await foldForRun(h.ctx, memoryFile(), "h2", [], [parent.transcript, independent.transcript], { + route, + }); + assert.equal(summary.gaps[0].sessions, 2); + assert.equal(summary.sourceObservers[historical.source], "P"); + assert.deepEqual(h.state.readGapLedger().entries[entryId].sessions.P, historical); +}); + +test("fresh matching evidence cannot bypass rejected attributed alias migration", async () => { + for (const destination of [null, "other-observer"]) { + const h = harness({ gapLedgerMaxAge: "all" }); + const entryId = gapEntryId(MEMORY_PATH, GAP); + const old = { + observedAt: "2026-08-01T00:00:00Z", + source: "old native source", + sourceSessionId: "G", + sightingIds: ["G", "H"], + quote: "old mixed quote", + }; + h.state.writeGapLedger({ + version: 1, + entries: { + [entryId]: { id: entryId, memoryPath: MEMORY_PATH, proposedInstruction: GAP, sessions: { G: old } }, + }, + }); + const fresh = record("G", [GAP], { memoryHash: "h2" }); + fresh.transcript = { ...fresh.transcript, harness: "pi", corroborationIdentity: "R" }; + fresh.key = evidenceKey(fresh.transcript, "h2"); + h.state.writeEvidence(fresh.transcript.id, fresh); + const selected = [fresh.transcript]; + if (destination) + selected.push({ + ...record("H", [], { memoryHash: "h2" }).transcript, + harness: "pi", + corroborationIdentity: destination, + }); + const summary = await foldForRun(h.ctx, memoryFile(), "h2", [], selected); + assert.equal(summary.analyzedSessions, 1); + assert.deepEqual(summary.gaps, []); + assert.equal(summary.totals.gapSightings, 1); + const sessions = h.state.readGapLedger().entries[entryId].sessions; + assert.deepEqual(sessions.G, old); + assert.equal(sessions.R.sourceSessionId, "G"); + assert.deepEqual(sessions.R.sightingIds, ["G"]); + } +}); + +test("direct recording still migrates provenance-free legacy aliases", () => { + const entryId = gapEntryId(MEMORY_PATH, GAP); + const ledger = { + version: 1, + entries: { + [entryId]: { + id: entryId, + memoryPath: MEMORY_PATH, + proposedInstruction: GAP, + sessions: { legacy: { observedAt: "2026-08-01T00:00:00Z", quote: "legacy quote" } }, + }, + }, + }; + const fresh = record("legacy", [GAP]); + fresh.transcript.identity = "current-native"; + recordGapObservations(ledger, [fresh], { legacyIds: new Set(["legacy"]) }); + assert.equal(ledger.entries[entryId].sessions.legacy, undefined); + const observation = ledgerGapObservations(ledger, MEMORY_PATH)[0]; + assert.equal(observation.sessionId, "current-native"); + assert.equal(observation.sourceSessionId, "current-native"); +}); + test("the same session is never double-counted across runs", async () => { const h = harness(); await run(h, [record("claude-s1", [GAP])]); @@ -126,6 +692,298 @@ test("the same session is never double-counted across runs", async () => { assert.equal(entries.length, 1, "rephrasings of one gap share one ledger entry"); assert.deepEqual(Object.keys(entries[0].sessions), ["claude-s1"]); }); +test("OMP parent and subagent persist native sightings with one vote in the child sample", async () => { + const h = harness(); + const startedAt = Date.parse("2026-08-01T00:00:00Z"); + const parentPath = "/omp/sessions/-repo-demo/parent-session.jsonl"; + const parentIdentity = "pi-parent-session"; + const parent = { + id: "pi-parent", + nativeId: "parent-native", + identity: "pi-file-parent", + corroborationIdentity: parentIdentity, + corroborationNativeId: "parent-native", + corroborationStartedAt: startedAt, + harness: "pi", + path: parentPath, + startedAt, + interaction: "interactive", + }; + const child = { + id: "pi-child", + nativeId: "child-native", + identity: "pi-file-child", + parentSessionId: "parent-native", + corroborationIdentity: parentIdentity, + corroborationNativeId: "parent-native", + corroborationStartedAt: startedAt, + harness: "pi", + path: "/omp/sessions/-repo-demo/parent-session/Subagent.jsonl", + startedAt: startedAt + 1_000, + interaction: "non-interactive", + }; + const independent = { + id: "claude-independent", + nativeId: "independent", + identity: "independent-session", + corroborationIdentity: "independent-session", + corroborationNativeId: "independent", + corroborationStartedAt: startedAt + 2_000, + harness: "claude", + path: "/claude/independent.jsonl", + startedAt: startedAt + 2_000, + interaction: "interactive", + }; + const evidence = (transcript) => { + const result = record(transcript.id, [GAP]); + result.transcript = transcript; + result.key = evidenceKey(transcript, result.memoryHash); + return result; + }; + const parentEvidence = evidence(parent); + const childEvidence = evidence(child); + + const first = await run(h, [parentEvidence, childEvidence]); + assert.equal(first.gaps.length, 0, "a parent plus its subagent remains one observer"); + assert.deepEqual( + Object.values(Object.values(h.state.readGapLedger().entries)[0].sessions) + .map((observation) => observation.sourceSessionId) + .sort(), + [parent.identity, child.identity].sort(), + ); + + const childOnly = await foldForRun(h.ctx, memoryFile(), "h1", [], [child]); + assert.equal( + childOnly.totals.gapSightings, + 2, + "unresolved history and fresh native evidence remain separate sightings", + ); + assert.equal(childOnly.gaps.length, 0); + + const independentEvidence = evidence(independent); + h.state.writeEvidence(independent.id, independentEvidence); + const withIndependent = await foldForRun(h.ctx, memoryFile(), "h1", [], [child, independent]); + assert.equal(withIndependent.gaps.length, 1); + assert.equal(withIndependent.gaps[0].sessions, 2); +}); +test("a selected OMP child keeps unresolved legacy parent history separate with one observer vote", async () => { + const h = harness({ gapLedgerMaxAge: "all" }); + const startedAt = Date.parse("2026-08-01T00:00:00Z"); + const parentIdentity = "pi-parent-session"; + const parent = { + id: "pi-parent", + nativeId: "parent-native", + identity: parentIdentity, + harness: "pi", + path: "/omp/sessions/-repo-demo/parent-session.jsonl", + startedAt, + interaction: "interactive", + }; + const child = { + id: "pi-child", + nativeId: "child-native", + identity: "pi-file-child", + parentSessionId: "parent-native", + corroborationIdentity: parentIdentity, + corroborationNativeId: "parent-native", + corroborationStartedAt: startedAt, + harness: "pi", + path: "/omp/sessions/-repo-demo/parent-session/Subagent.jsonl", + startedAt: startedAt + 1_000, + interaction: "non-interactive", + }; + const asEvidence = (transcript) => { + const result = record(transcript.id, [GAP]); + result.transcript = transcript; + result.key = evidenceKey(transcript, result.memoryHash); + return result; + }; + const legacyChild = { ...child }; + delete legacyChild.parentSessionId; + delete legacyChild.corroborationIdentity; + delete legacyChild.corroborationNativeId; + delete legacyChild.corroborationStartedAt; + const ledger = { version: 1, entries: {} }; + recordGapObservations(ledger, [asEvidence(parent), asEvidence(legacyChild)], { + now: new Date(startedAt + 2_000), + }); + const before = structuredClone(Object.values(ledger.entries)[0].sessions); + h.state.writeGapLedger(ledger); + + const currentChildEvidence = asEvidence(child); + currentChildEvidence.gaps = []; + h.state.writeEvidence(child.id, currentChildEvidence); + const summary = await foldForRun(h.ctx, memoryFile(), "h1", [], [child]); + + assert.equal(summary.gaps.length, 0, "old parent and child keys still represent one observer"); + assert.equal(summary.totals.droppedGapSingletons, 1); + const sessions = Object.values(h.state.readGapLedger().entries)[0].sessions; + assert.deepEqual(sessions, before, "the unresolved occupied parent and valid child stay intact on disk"); + assert.deepEqual(Object.keys(sessions).sort(), [parentIdentity, child.identity].sort()); +}); + +test("native sightings union across recording, migration and consolidation with one root vote", () => { + const h = harness({ gapLedgerMaxAge: "all" }); + const gapA = "Always verify the API schema before adding a handler."; + const gapB = "Check deployment permissions when releasing services."; + const native = (id, root, gap, relative) => { + const transcript = { + id: `pi-${id}`, + identity: id, + nativeId: id, + harness: "pi", + corroborationIdentity: root, + startedAt: Date.parse("2026-08-02T00:00:00Z"), + interaction: "non-interactive", + cwd: h.root, + }; + return { + ...record(id, [gap]), + transcript, + relative, + }; + }; + const records = [ + native("child-api", "observer-a", gapA, "apps/api/a.ts"), + native("child-web", "observer-a", gapB, "apps/web/a.ts"), + native("independent-api", "observer-b", gapA, "apps/api/b.ts"), + ]; + const ledger = { version: 1, entries: {} }; + recordGapObservations(ledger, records); + const apiEntry = Object.values(ledger.entries).find((entry) => entry.proposedInstruction === gapA); + const webEntry = Object.values(ledger.entries).find((entry) => entry.proposedInstruction === gapB); + assert.equal(mergeGapEntries(ledger, [[apiEntry.id, webEntry.id]]), 1); + normalizeGapLedgerSessions( + ledger, + records.map((record) => record.transcript), + ); + const observations = ledgerGapObservations(ledger, MEMORY_PATH); + const shared = observations.find((observation) => observation.sessionId === "observer-a"); + assert.equal(shared.sourceSessionId, "child-api"); + assert.deepEqual(shared.sightingIds, ["child-api"]); + assert.deepEqual(observations.find((observation) => observation.sourceSessionId === "child-web").sightingIds, [ + "child-web", + ]); + assert.equal(shared.source, "pi · child-api · 2026-08-02"); + const attribution = new Map( + records.map((record) => [ + record.transcript.identity, + workedPaths(record.transcript, [{ kind: "tool", input: { path: record.relative } }], [h.root]), + ]), + ); + const api = { path: "apps/api/AGENTS.md", dir: "apps/api" }; + const route = routingFor([api], attribution, MEMORY_PATH, null); + const summary = foldEvidence(records, { minGapEvidence: 2, gapObservations: observations, route }); + assert.equal(summary.gaps.length, 1); + assert.equal(summary.gaps[0].sessions, 2); + assert.equal(summary.sourceObservers[shared.source], "observer-a"); + assert.equal( + foldEvidence(records, { + minGapEvidence: 2, + gapObservations: observations, + route: { ...route, weight: api.path }, + }).gaps.length, + 0, + ); + + recordGapObservations(ledger, [records[1]]); + const preserved = ledgerGapObservations(ledger, MEMORY_PATH); + const prior = preserved.find((observation) => observation.sessionId === "observer-a"); + assert.deepEqual(prior, shared); + const fresh = preserved.find((observation) => observation.sourceSessionId === "child-web"); + assert.equal(fresh.sourceSessionId, "child-web"); + assert.deepEqual(fresh.sightingIds, ["child-web"]); + const refolded = foldEvidence(records, { minGapEvidence: 2, gapObservations: preserved, route }); + assert.equal(refolded.gaps[0].sessions, 2); +}); + +test("ordinary non-OMP selected legacy observations keep their native routing identity", async () => { + const h = harness({ gapLedgerMaxAge: "all" }); + const selected = [record("ordinary-a", [GAP]), record("ordinary-b", [GAP])].map((record) => ({ + ...record.transcript, + cwd: h.root, + })); + const entryId = gapEntryId(MEMORY_PATH, GAP); + h.state.writeGapLedger({ + version: 1, + entries: { + [entryId]: { + id: entryId, + memoryPath: MEMORY_PATH, + proposedInstruction: GAP, + sessions: Object.fromEntries( + selected.map((transcript) => [ + transcript.identity, + { + observedAt: "2026-08-03T00:00:00Z", + source: `claude · ${transcript.id} · 2026-08-01`, + quote: `quote ${transcript.id}`, + }, + ]), + ), + }, + }, + }); + const api = { path: "apps/api/AGENTS.md", dir: "apps/api" }; + const attribution = new Map( + selected.map((transcript) => [ + transcript.identity, + workedPaths(transcript, [{ kind: "tool", input: { path: "apps/api/handler.ts" } }], [h.root]), + ]), + ); + const summary = await foldForRun(h.ctx, memoryFile(), "h1", [], selected, { + route: routingFor([api], attribution, MEMORY_PATH, api.path), + }); + assert.equal(summary.gaps[0].sessions, 2); + for (const observation of ledgerGapObservations(h.state.readGapLedger(), MEMORY_PATH)) { + assert.equal(observation.sourceSessionId, observation.sessionId); + assert.deepEqual(observation.sightingIds, [observation.sessionId]); + assert.equal(summary.sourceSessions[observation.source], observation.sessionId); + } +}); + +test("an OMP parent and subagent sharing a root vote project in one run whatever the order", () => { + const startedAt = Date.parse("2026-08-01T00:00:00Z"); + const rootIdentity = "pi-parent-session"; + const observe = (id, domain) => { + const transcript = { + id, + nativeId: `${id}-native`, + identity: `pi-file-${id}`, + corroborationIdentity: rootIdentity, + corroborationNativeId: "parent-native", + corroborationStartedAt: startedAt, + harness: "pi", + startedAt, + interaction: "interactive", + }; + const result = record(id, [{ proposedInstruction: GAP, domain }]); + result.transcript = transcript; + result.key = evidenceKey(transcript, result.memoryHash); + return result; + }; + const parent = observe("pi-parent", "project"); + const child = observe("pi-child", "orchestration"); + const domainAfter = (records, ledger = { version: 1, entries: {} }, now = new Date(startedAt + DAY)) => { + recordGapObservations(ledger, records, { now, transcripts: [parent.transcript, child.transcript] }); + const summary = foldEvidence(records, { + gapObservations: ledgerGapObservations(ledger, MEMORY_PATH), + minGapEvidence: 1, + }); + assert.equal(summary.gaps[0].sessions, 1); + return { ledger, domain: summary.gaps[0].orchestrationSightings ? "orchestration" : "project" }; + }; + + assert.equal(domainAfter([parent, child]).domain, "project"); + assert.equal(domainAfter([child, parent]).domain, "project"); + + const { ledger } = domainAfter([parent]); + assert.equal( + domainAfter([child], ledger, new Date(startedAt + 2 * DAY)).domain, + "project", + "a later child observation cannot overwrite the parent's native project evidence", + ); +}); test("a legacy session-id observation migrates without counting the identity as a second session", async () => { const h = harness(); @@ -145,6 +1003,71 @@ test("a legacy session-id observation migrates without counting the identity as assert.equal(entry.sessions["stable-identity-s1"].firstObservedAt, firstObservedAt); }); +test("an ambiguous legacy session id never migrates onto a selected session", async () => { + const h = harness({ gapLedgerMaxAge: "all" }); + const evidenceFor = (transcript, gaps) => { + const result = record(transcript.id, gaps); + result.transcript = transcript; + result.key = evidenceKey(transcript, result.memoryHash); + return result; + }; + const shared = (identity) => ({ + ...record("claude-shared", []).transcript, + nativeId: "shared", + identity, + path: `/claude/${identity}.jsonl`, + }); + const a = shared("session-a"); + const b = shared("session-b"); + const c = record("claude-c", []).transcript; + const ledger = { version: 1, entries: {} }; + recordGapObservations(ledger, [record("claude-shared", [GAP]), record("claude-c", [GAP])]); + h.state.writeGapLedger(ledger); + h.state.writeEvidence(a, evidenceFor(a, [])); + h.state.writeEvidence(b, evidenceFor(b, [GAP])); + + const summary = await foldForRun(h.ctx, memoryFile(), "h1", [], [a, c]); + + assert.equal(summary.gaps.length, 0, "a sighting that may be B's must not corroborate A"); + const [entry] = Object.values(h.state.readGapLedger().entries); + assert.deepEqual(Object.keys(entry.sessions).sort(), ["claude-c", "claude-shared"]); +}); + +test("a selected session recording a gap never inherits an ambiguous legacy id's sighting", async () => { + const h = harness(); + const shared = (identity) => ({ + ...record("claude-shared", []).transcript, + nativeId: "shared", + identity, + path: `/claude/${identity}.jsonl`, + }); + const a = shared("session-a"); + const b = shared("session-b"); + const evidenceFor = (transcript, gaps) => { + const result = record(transcript.id, gaps); + result.transcript = transcript; + result.key = evidenceKey(transcript, result.memoryHash); + return result; + }; + const ledger = { version: 1, entries: {} }; + const bFirstObservedAt = new Date(Date.now() - 80 * DAY); + recordGapObservations(ledger, [record("claude-shared", [GAP])], { now: bFirstObservedAt }); + h.state.writeGapLedger(ledger); + h.state.writeEvidence(a, evidenceFor(a, [GAP])); + h.state.writeEvidence(b, evidenceFor(b, [GAP])); + + const summary = await foldForRun(h.ctx, memoryFile(), "h1", [], [a]); + + assert.equal(summary.gaps.length, 0, "B's sighting must not corroborate A"); + const [entry] = Object.values(h.state.readGapLedger().entries); + assert.deepEqual(Object.keys(entry.sessions).sort(), ["claude-shared", "session-a"]); + assert.equal(entry.sessions["claude-shared"].firstObservedAt, bFirstObservedAt.toISOString()); + assert.ok( + Date.parse(entry.sessions["session-a"].firstObservedAt) > bFirstObservedAt.getTime(), + "A's fresh sighting keeps its own first-seen time", + ); +}); + test("a genuine one-off never graduates, however many runs see it", async () => { const h = harness(); for (let i = 0; i < 5; i += 1) { @@ -261,6 +1184,44 @@ test("re-analysis of a session does not restart its expiry clock", async () => { assert.equal(summary.gaps.length, 0, "the re-seen old sighting keeps its original first-seen time"); }); +test("a plain Pi session re-reporting a provenance-free sighting keeps its slot and first-seen time", async () => { + const h = harness({ gapLedgerMaxAge: "90d" }); + const entryId = gapEntryId(MEMORY_PATH, GAP); + const firstObservedAt = new Date(Date.now() - 50 * DAY).toISOString(); + h.state.writeGapLedger({ + version: 1, + entries: { + [entryId]: { + id: entryId, + memoryPath: MEMORY_PATH, + proposedInstruction: GAP, + phrasings: [GAP], + sessions: { + "pi-s1": { + firstObservedAt, + observedAt: firstObservedAt, + memoryHash: "h1", + source: "pi · pi-s1 · 2026-08-01", + quote: "quote from pi-s1", + phrasings: [GAP_REPHRASED], + coveredBySkill: "db-docs", + }, + }, + }, + }, + }); + const again = record("pi-s1", [GAP], { memoryHash: "h2" }); + again.transcript = { ...again.transcript, harness: "pi", nativeId: "pi-s1" }; + again.key = evidenceKey(again.transcript, "h2"); + await run(h, [again], memoryFile(), "h2"); + const sessions = h.state.readGapLedger().entries[entryId].sessions; + assert.deepEqual(Object.keys(sessions), ["pi-s1"], "the main-era sighting is replaced, not duplicated"); + assert.equal(sessions["pi-s1"].firstObservedAt, firstObservedAt); + assert.equal(sessions["pi-s1"].sourceSessionId, "pi-s1"); + assert.deepEqual(sessions["pi-s1"].phrasings, [GAP_REPHRASED, GAP]); + assert.equal(sessions["pi-s1"].coveredBySkill, "db-docs"); +}); + test("a gap the memory file now covers is retired from the ledger", async () => { const h = harness(); await run(h, [record("claude-s1", [GAP])]); diff --git a/test/helpers/gap-observers.js b/test/helpers/gap-observers.js new file mode 100644 index 00000000..afcabd7b --- /dev/null +++ b/test/helpers/gap-observers.js @@ -0,0 +1,31 @@ +import assert from "node:assert/strict"; + +import { foldEvidence } from "../../src/fold.js"; +import { gapSource, ledgerGapObservations } from "../../src/gap-ledger.js"; +import { corroborationIdentityOf } from "../../src/transcript.js"; + +export function assertNativeGapContributions(ledger, transcripts, memoryPath = "AGENTS.md") { + const observations = Object.values(ledger.entries) + .filter((entry) => entry.memoryPath === memoryPath) + .flatMap((entry) => Object.values(entry.sessions)); + assert.deepEqual( + observations.map((observation) => observation.sourceSessionId).sort(), + transcripts.map((transcript) => transcript.identity || transcript.id).sort(), + ); + for (const transcript of transcripts) { + const identity = transcript.identity || transcript.id; + const observation = observations.find((candidate) => candidate.sourceSessionId === identity); + assert.deepEqual(observation.sightingIds, [identity]); + assert.equal(observation.source, gapSource(transcript)); + assert.equal(observation.contentDigest, transcript.contentSignature || `${transcript.mtimeMs}:${transcript.bytes}`); + } + const summary = foldEvidence( + transcripts.map((transcript) => ({ status: "ok", transcript })), + { + minGapEvidence: 1, + gapObservations: ledgerGapObservations(ledger, memoryPath), + }, + ); + assert.equal(summary.gaps.length, 1); + assert.equal(summary.gaps[0].sessions, new Set(transcripts.map(corroborationIdentityOf)).size); +} diff --git a/test/interaction.test.js b/test/interaction.test.js index 2ede9b16..55f1c9bb 100644 --- a/test/interaction.test.js +++ b/test/interaction.test.js @@ -7,6 +7,7 @@ import { spawnSync } from "node:child_process"; import { fileURLToPath } from "node:url"; import { DatabaseSync } from "node:sqlite"; import vm from "node:vm"; +import { assertNativeGapContributions } from "./helpers/gap-observers.js"; import * as claude from "../src/discovery/adapters/claude.js"; import * as codex from "../src/discovery/adapters/codex.js"; @@ -25,6 +26,7 @@ import { foldForRun, printProposal } from "../src/commands/propose.js"; import { cmdScan } from "../src/commands/scan.js"; import { renderApplySurface } from "../src/apply/lavish.js"; import { evidenceKey, State } from "../src/state.js"; +import { recordGapObservations } from "../src/gap-ledger.js"; import { transcriptIdentity } from "../src/transcript.js"; import { sampleTranscripts, capTranscripts } from "../src/sample.js"; import { setLoggerSink } from "../src/logger.js"; @@ -268,6 +270,65 @@ test("scan prints the interactive/non-interactive mix through the CLI", () => { assert.match(human.stdout, /interactive 1 · non-interactive 2/); }); +test("scan --json on a plain Pi store ignores deeper files and carries no observer fields until includeOmp is set", () => { + const repo = initRepo(); + const home = fs.mkdtempSync(path.join(os.tmpdir(), "backpass-pi-plain-home-")); + const session = (id, title = false) => + [ + ...(title ? [{ type: "title", v: 1, title: "" }] : []), + { type: "session", version: 3, id, timestamp: "2026-08-20T10:00:00.000Z", cwd: repo }, + { + type: "message", + id: "e1", + timestamp: "2026-08-20T10:00:01.000Z", + message: { role: "user", content: [{ type: "text", text: `Do the work ${id}` }] }, + }, + ].map((entry) => JSON.stringify(entry)); + const write = (file, lines) => { + fs.mkdirSync(path.dirname(file), { recursive: true }); + fs.writeFileSync(file, `${lines.join("\n")}\n`); + }; + const piDir = path.join(home, ".pi", "agent", "sessions", "-repo-demo"); + write(path.join(piDir, "plain-root.jsonl"), session("plain-root")); + write(path.join(piDir, "plain-root", "Deeper.jsonl"), session("plain-deeper")); + write(path.join(piDir, "titled.jsonl"), session("plain-titled", true)); + const ompDir = path.join(home, ".omp", "agent", "sessions", "-repo-demo"); + write(path.join(ompDir, "omp-root.jsonl"), session("omp-root", true)); + write(path.join(ompDir, "omp-root", "Helper.jsonl"), session("omp-helper", true)); + + const scan = () => { + const result = spawnSync(process.execPath, [CLI, "scan", "--harness", "pi", "--since", "all", "--json"], { + cwd: repo, + env: { ...process.env, HOME: home, USERPROFILE: home, NO_COLOR: "1" }, + encoding: "utf8", + }); + assert.equal(result.status, 0, result.stderr); + return JSON.parse(result.stdout); + }; + + const plain = scan(); + assert.deepEqual( + plain.transcripts.map((t) => [t.nativeId, t.interaction]), + [["plain-root", INTERACTIVE]], + ); + assert.deepEqual(plain.mix, { interactive: 1, nonInteractive: 0, total: 1 }); + for (const t of plain.transcripts) { + assert.deepEqual(t.interactionSignals, {}); + for (const key of ["corroborationIdentity", "corroborationNativeId", "corroborationStartedAt"]) { + assert.equal(key in t, false, key); + } + } + + fs.writeFileSync(path.join(repo, ".backpassrc.json"), JSON.stringify({ discovery: { includeOmp: true } })); + const opted = scan(); + const byId = new Map(opted.transcripts.map((t) => [t.nativeId, t])); + assert.deepEqual([...byId.keys()].sort(), ["omp-helper", "omp-root", "plain-root"]); + assert.equal(byId.get("plain-root").interaction, INTERACTIVE); + assert.equal(byId.get("omp-root").interaction, INTERACTIVE); + assert.equal(byId.get("omp-helper").interaction, NON_INTERACTIVE); + assert.equal(byId.get("omp-helper").corroborationNativeId, "omp-root"); +}); + test("cmdScan includes the mix on the human table", async () => { const repo = initRepo(); const home = fs.mkdtempSync(path.join(os.tmpdir(), "backpass-mix-scan-")); @@ -478,6 +539,44 @@ test("fold excludes legacy evidence without an interaction category", async () = assert.equal(state.readEvidence(transcript).transcript.interaction, undefined); }); +test("fold keeps legacy evidence excluded when current discovery stamps an interaction", async () => { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), "backpass-mix-fold-current-")); + const state = new State(dir).ensure(); + const transcript = { + harness: "codex", + id: "codex-legacy-current", + nativeId: "legacy-current", + path: "/sessions/legacy-current.jsonl", + mtimeMs: 100, + bytes: 200, + interaction: INTERACTIVE, + }; + const memoryHash = "sha256:memory"; + state.writeEvidence(transcript, { + status: "ok", + transcript: { harness: "codex", id: transcript.id, path: transcript.path }, + memoryHash, + memoryPath: "AGENTS.md", + key: evidenceKey(transcript, memoryHash), + positive: [{ instruction: "AG-001", quote: "followed the repository rule exactly" }], + negative: [], + gaps: [], + }); + + const summary = await foldForRun( + { + repo: { root: dir }, + config: { state, minGapEvidence: 2, gapLedgerMaxAge: "90d" }, + }, + { path: "AGENTS.md", text: "", units: [] }, + memoryHash, + [], + [transcript], + ); + + assert.equal(summary.analyzedSessions, 0, "only analysis may backfill a stored interaction stamp"); +}); + test("fold selection distinguishes colliding native IDs by source", async () => { const dir = fs.mkdtempSync(path.join(os.tmpdir(), "backpass-mix-identity-fold-")); const state = new State(dir).ensure(); @@ -749,6 +848,389 @@ test("evidence records carry the category and fold reports relevance per categor process.env.HOME = prevHome; } }); +test("OMP subagents share their parent identity and refresh cached relations", async () => { + const repo = initRepo(); + const home = fs.mkdtempSync(path.join(os.tmpdir(), "backpass-omp-discovery-")); + const sessionRoot = path.join(home, ".omp", "agent", "sessions", "-repo-demo"); + const parentName = "2026-08-27T00-00-00.000Z_parent-folder"; + const parentPath = path.join(sessionRoot, `${parentName}.jsonl`); + const childPath = path.join(sessionRoot, parentName, "Subagent.jsonl"); + const header = (id) => [ + { type: "title", v: 1, title: "" }, + { type: "session", version: 3, id, timestamp: "2026-08-27T00:00:00.000Z", cwd: repo }, + ]; + writeJsonl(parentPath, header("parent-native")); + writeJsonl(childPath, header("child-native")); + + const prevHome = process.env.HOME; + process.env.HOME = home; + try { + const config = loadConfig(repo, { discovery: { harnesses: ["pi"], since: "all", includeOmp: true } }); + config.state = new State(repo).ensure(); + const cache = config.state.readScanCache(); + for (const candidate of pi.enumerate({ config })) { + const descriptor = pi.classify(candidate); + delete descriptor.parentSessionId; + delete descriptor.parentSessionPath; + delete descriptor.parentSessionStartedAt; + cache.entries[`pi:${candidate.key}`] = { + mtimeMs: candidate.mtimeMs, + bytes: candidate.bytes, + descriptor, + }; + } + config.state.writeScanCache(cache); + + const repository = { name: "demo", root: repo, worktrees: [repo], remotes: [] }; + const first = await discoverTranscripts({ repo: repository, config }); + assert.equal(first.perHarness.pi.cached, 0, "the pre-relation cache must be reclassified"); + const parent = first.transcripts.find((transcript) => transcript.nativeId === "parent-native"); + const child = first.transcripts.find((transcript) => transcript.nativeId === "child-native"); + assert.ok(parent && child); + assert.notEqual(parent.identity, child.identity, "the two files remain separately analyzable"); + assert.equal(parent.corroborationIdentity, parent.identity); + assert.equal(child.corroborationIdentity, parent.identity); + assert.equal(parent.interaction, INTERACTIVE); + assert.equal(child.interaction, NON_INTERACTIVE); + + const second = await discoverTranscripts({ repo: repository, config }); + assert.equal(second.perHarness.pi.cached, 2, "the refreshed relation is safe to reuse"); + assert.equal( + second.transcripts.find((transcript) => transcript.nativeId === "child-native").corroborationIdentity, + parent.identity, + ); + config.discovery.includeOmp = false; + const disabled = await discoverTranscripts({ repo: repository, config }); + assert.deepEqual(disabled.transcripts, [], "opting out must not replay cached OMP descriptors"); + } finally { + if (prevHome === undefined) delete process.env.HOME; + else process.env.HOME = prevHome; + } +}); + +test("Pi child cache is invalidated when its parent session appears", async () => { + const repo = initRepo(); + const home = fs.mkdtempSync(path.join(os.tmpdir(), "backpass-omp-parent-cache-")); + const sessionRoot = path.join(home, ".omp", "agent", "sessions", "-repo-demo"); + const parentName = "2026-08-27T00-00-00.000Z_parent-folder"; + const parentPath = path.join(sessionRoot, `${parentName}.jsonl`); + const childPath = path.join(sessionRoot, parentName, "Subagent.jsonl"); + const header = (id) => [ + { type: "title", v: 1, title: "" }, + { type: "session", version: 3, id, timestamp: "2026-08-27T00:00:00.000Z", cwd: repo }, + ]; + writeJsonl(childPath, header("child-native")); + + const prevHome = process.env.HOME; + process.env.HOME = home; + try { + const config = loadConfig(repo, { discovery: { harnesses: ["pi"], since: "all", includeOmp: true } }); + config.state = new State(repo).ensure(); + const repository = { name: "demo", root: repo, worktrees: [repo], remotes: [] }; + + const first = await discoverTranscripts({ repo: repository, config }); + const childBeforeParent = first.transcripts.find((transcript) => transcript.nativeId === "child-native"); + assert.ok(childBeforeParent); + assert.equal(childBeforeParent.parentSessionId, undefined); + assert.equal(childBeforeParent.interaction, NON_INTERACTIVE); + assert.equal(childBeforeParent.corroborationIdentity, childBeforeParent.identity); + const cachedMissingParent = await discoverTranscripts({ repo: repository, config }); + assert.equal(cachedMissingParent.perHarness.pi.cached, 1); + assert.equal(cachedMissingParent.transcripts[0].interaction, NON_INTERACTIVE); + + writeJsonl(parentPath, header("parent-native")); + const second = await discoverTranscripts({ repo: repository, config }); + const parent = second.transcripts.find((transcript) => transcript.nativeId === "parent-native"); + const child = second.transcripts.find((transcript) => transcript.nativeId === "child-native"); + + assert.ok(parent && child); + assert.equal(child.parentSessionId, "parent-native"); + assert.equal(child.corroborationIdentity, parent.identity); + assert.equal(second.perHarness.pi.cached, 0, "the child descriptor must be reclassified after its parent appears"); + + const third = await discoverTranscripts({ repo: repository, config }); + assert.equal(third.perHarness.pi.cached, 2, "the refreshed parent relation is safe to reuse"); + assert.equal( + third.transcripts.find((transcript) => transcript.nativeId === "child-native").corroborationIdentity, + parent.identity, + ); + } finally { + if (prevHome === undefined) delete process.env.HOME; + else process.env.HOME = prevHome; + } +}); + +test("nested OMP automation stays non-interactive with unreadable ancestors and refreshes legacy caches", async () => { + const repo = initRepo(); + const home = fs.mkdtempSync(path.join(os.tmpdir(), "backpass-omp-unreadable-")); + const sessionRoot = path.join(home, ".omp", "agent", "sessions", "-repo-demo"); + const childPath = path.join(sessionRoot, "root", "Subagent.jsonl"); + const grandchildPath = path.join(sessionRoot, "other-root", "Missing", "Missing.Child.jsonl"); + const header = (id) => [ + { type: "title", v: 1, title: "" }, + { type: "session", version: 3, id, timestamp: "2026-08-27T00:00:00.000Z", cwd: repo }, + ]; + writeJsonl(childPath, header("child-native")); + writeJsonl(grandchildPath, header("grandchild-native")); + writeJsonl(path.join(sessionRoot, "root.jsonl"), [{ type: "title", title: "unreadable header" }]); + const prevHome = process.env.HOME; + process.env.HOME = home; + try { + const config = loadConfig(repo, { discovery: { harnesses: ["pi"], since: "all", includeOmp: true } }); + config.state = new State(repo).ensure(); + const cache = config.state.readScanCache(); + for (const candidate of pi.enumerate({ config })) { + const descriptor = pi.classify(candidate); + if (descriptor) descriptor.interactionSignals = {}; + cache.entries[`pi:${candidate.key}`] = { mtimeMs: candidate.mtimeMs, bytes: candidate.bytes, descriptor }; + } + config.state.writeScanCache(cache); + const repository = { name: "demo", root: repo, worktrees: [repo], remotes: [] }; + const first = await discoverTranscripts({ repo: repository, config }); + assert.equal(first.perHarness.pi.cached, 0); + assert.equal(first.transcripts.length, 2); + for (const transcript of first.transcripts) { + assert.equal(transcript.interaction, NON_INTERACTIVE); + assert.equal(classifyInteraction({ ...transcript, interaction: INTERACTIVE }), NON_INTERACTIVE); + assert.equal(transcript.parentSessionId, undefined); + assert.equal(transcript.corroborationIdentity, transcript.identity); + assert.equal(transcript.corroborationNativeId, transcript.nativeId); + assert.equal(transcript.corroborationStartedAt, transcript.startedAt); + } + const second = await discoverTranscripts({ repo: repository, config }); + assert.equal(second.perHarness.pi.cached, 3); + assert.ok(second.transcripts.every((transcript) => transcript.interaction === NON_INTERACTIVE)); + + const memoryHash = "sha256:memory"; + for (const transcript of second.transcripts) { + config.state.writeEvidence(transcript, { + status: "ok", + transcript: { ...transcript, interaction: INTERACTIVE }, + memoryHash, + memoryPath: "AGENTS.md", + key: evidenceKey(transcript, memoryHash), + positive: [], + negative: [], + gaps: [], + }); + } + const summary = await analyzeTranscripts({ + transcripts: second.transcripts, + memoryFile: { path: "AGENTS.md" }, + config: { ...config, agents: { resolve: () => assert.fail("fresh evidence must not invoke an agent") } }, + repo: repository, + memoryHash, + }); + assert.deepEqual([summary.cached, summary.analyzed], [2, 0]); + const evidence = config.state.listEvidence(); + assert.ok(evidence.every((record) => record.transcript.interaction === NON_INTERACTIVE)); + assert.ok(evidence.every((record) => record.transcript.parentSessionId === null)); + assert.deepEqual(foldEvidence(evidence).analyzedByInteraction, { [INTERACTIVE]: 0, [NON_INTERACTIVE]: 2 }); + } finally { + if (prevHome === undefined) delete process.env.HOME; + else process.env.HOME = prevHome; + } +}); + +test("standalone fold refreshes stale OMP interaction categories with unreadable ancestors", async () => { + const repo = initRepo(); + const home = fs.mkdtempSync(path.join(os.tmpdir(), "backpass-omp-fold-interaction-")); + const sessionRoot = path.join(home, ".omp", "agent", "sessions", "-repo-demo"); + const header = (id) => [ + { type: "title", v: 1, title: "" }, + { type: "session", version: 3, id, timestamp: "2026-08-27T00:00:00.000Z", cwd: repo }, + ]; + writeJsonl(path.join(sessionRoot, "root.jsonl"), [{ type: "title", title: "unreadable header" }]); + writeJsonl(path.join(sessionRoot, "root", "Subagent.jsonl"), header("child-native")); + writeJsonl(path.join(sessionRoot, "other-root", "Missing", "Missing.Child.jsonl"), header("grandchild-native")); + writeJsonl(path.join(sessionRoot, "human.jsonl"), header("human-native")); + const prevHome = process.env.HOME; + process.env.HOME = home; + try { + const config = loadConfig(repo, { discovery: { harnesses: ["pi"], since: "all", includeOmp: true } }); + config.state = new State(repo).ensure(); + const repository = { name: "demo", root: repo, worktrees: [repo], remotes: [] }; + const { transcripts } = await discoverTranscripts({ repo: repository, config }); + assert.equal(transcripts.length, 3); + for (const transcript of transcripts) { + assert.equal(transcript.parentSessionId, undefined); + const human = transcript.nativeId === "human-native"; + assert.equal(transcript.interaction, human ? INTERACTIVE : NON_INTERACTIVE); + config.state.writeEvidence(transcript, { + status: "ok", + transcript: { ...transcript, interactionSignals: {}, interaction: human ? NON_INTERACTIVE : INTERACTIVE }, + memoryHash: "sha256:memory", + memoryPath: "AGENTS.md", + key: evidenceKey(transcript, "sha256:memory"), + positive: human ? [{ instruction: "AG-001", quote: "kept the PR small" }] : [], + negative: human ? [] : [{ instruction: "AG-002", quote: "made the PR too large", class: "harm" }], + gaps: [], + }); + } + + for (const staleDiscoveryStamp of [false, true]) { + const selected = transcripts.map((transcript) => ({ + ...transcript, + interaction: staleDiscoveryStamp ? INTERACTIVE : transcript.interaction, + })); + const summary = await foldForRun( + { repo: repository, config }, + { path: "AGENTS.md", text: "", units: [] }, + "sha256:memory", + [], + selected, + ); + assert.equal(summary.analyzedSessions, 3); + assert.deepEqual(summary.analyzedByInteraction, { [INTERACTIVE]: 1, [NON_INTERACTIVE]: 2 }); + assert.deepEqual(summary.instructions.find((row) => row.instruction === "AG-001").relevanceByInteraction, { + [INTERACTIVE]: 1, + [NON_INTERACTIVE]: 0, + }); + assert.deepEqual(summary.instructions.find((row) => row.instruction === "AG-002").relevanceByInteraction, { + [INTERACTIVE]: 0, + [NON_INTERACTIVE]: 1, + }); + } + assert.equal( + config.state.listEvidence().find((record) => record.transcript.nativeId === "child-native").transcript + .interaction, + INTERACTIVE, + ); + } finally { + if (prevHome === undefined) delete process.env.HOME; + else process.env.HOME = prevHome; + fs.rmSync(home, { recursive: true, force: true }); + fs.rmSync(repo, { recursive: true, force: true }); + } +}); + +test("nested OMP subagents share the root identity once the root session appears", async () => { + const repo = initRepo(); + const home = fs.mkdtempSync(path.join(os.tmpdir(), "backpass-omp-nested-discovery-")); + const sessionRoot = path.join(home, ".omp", "agent", "sessions", "-repo-demo"); + const rootName = "2026-08-27T00-00-00.000Z_root-folder"; + const rootPath = path.join(sessionRoot, `${rootName}.jsonl`); + const childPath = path.join(sessionRoot, rootName, "Subagent.jsonl"); + const grandchildPath = path.join(sessionRoot, rootName, "Subagent", "Subagent.Child.jsonl"); + const header = (id, timestamp) => [ + { type: "title", v: 1, title: "" }, + { type: "session", version: 3, id, timestamp, cwd: repo }, + ]; + writeJsonl(childPath, header("child-native", "2026-08-27T00:01:00.000Z")); + writeJsonl(grandchildPath, header("grandchild-native", "2026-08-27T00:02:00.000Z")); + + const prevHome = process.env.HOME; + process.env.HOME = home; + try { + const config = loadConfig(repo, { discovery: { harnesses: ["pi"], since: "all", includeOmp: true } }); + config.state = new State(repo).ensure(); + config.gapLedgerMaxAge = "all"; + const repository = { name: "demo", root: repo, worktrees: [repo], remotes: [] }; + const byNativeId = (result, nativeId) => result.transcripts.find((transcript) => transcript.nativeId === nativeId); + + const first = await discoverTranscripts({ repo: repository, config }); + assert.equal(byNativeId(first, "child-native").parentSessionId, undefined); + assert.equal(byNativeId(first, "grandchild-native").parentSessionId, "child-native"); + + writeJsonl(rootPath, header("root-native", "2026-08-27T00:00:00.000Z")); + const second = await discoverTranscripts({ repo: repository, config }); + assert.equal(second.perHarness.pi.cached, 0, "both descendants must be reclassified when the root appears"); + const root = byNativeId(second, "root-native"); + const child = byNativeId(second, "child-native"); + const grandchild = byNativeId(second, "grandchild-native"); + assert.ok(root && child && grandchild); + assert.equal(new Set([root.identity, child.identity, grandchild.identity]).size, 3, "each file stays analyzable"); + for (const descendant of [child, grandchild]) { + assert.equal(descendant.parentSessionId, "root-native"); + assert.equal(descendant.corroborationIdentity, root.identity); + assert.equal(descendant.corroborationNativeId, "root-native"); + assert.equal(descendant.corroborationStartedAt, root.startedAt); + assert.equal(descendant.interaction, NON_INTERACTIVE); + } + assert.equal(root.corroborationIdentity, root.identity); + assert.equal(root.interaction, INTERACTIVE); + + const third = await discoverTranscripts({ repo: repository, config }); + assert.equal(third.perHarness.pi.cached, 3, "the refreshed relations are safe to reuse"); + assert.equal(byNativeId(third, "grandchild-native").corroborationIdentity, root.identity); + + const perFile = (transcript) => ({ + status: "ok", + memoryPath: "AGENTS.md", + memoryHash: "sha256:memory", + transcript: { ...transcript, parentSessionId: null, corroborationIdentity: null, corroborationNativeId: null }, + gaps: [ + { + proposedInstruction: "Read docs/db.md before writing queries.", + mistake: "re-derived it", + quote: "quote", + recurrenceRisk: "high", + }, + ], + }); + const ledger = { version: 1, entries: {} }; + recordGapObservations(ledger, [perFile(child), perFile(grandchild)]); + config.state.writeGapLedger(ledger); + + const summary = await foldForRun( + { repo: { root: repo }, config }, + { path: "AGENTS.md", text: "", units: [] }, + "sha256:memory", + [], + [child, grandchild], + ); + assert.equal(summary.gaps.length, 0, "per-file sightings of one root session are one observer"); + assertNativeGapContributions(config.state.readGapLedger(), [child, grandchild]); + } finally { + if (prevHome === undefined) delete process.env.HOME; + else process.env.HOME = prevHome; + } +}); + +test("OMP subagents running in another repo cwd still share the root identity", async () => { + const repo = initRepo(); + const subdir = path.join(repo, "packages", "api"); + fs.mkdirSync(subdir, { recursive: true }); + const home = fs.mkdtempSync(path.join(os.tmpdir(), "backpass-omp-cwd-discovery-")); + const sessionRoot = path.join(home, ".omp", "agent", "sessions", "-repo-demo"); + const rootName = "2026-08-27T00-00-00.000Z_root-folder"; + const rootPath = path.join(sessionRoot, `${rootName}.jsonl`); + const childPath = path.join(sessionRoot, rootName, "Subagent.jsonl"); + const grandchildPath = path.join(sessionRoot, rootName, "Subagent", "Subagent.Child.jsonl"); + const header = (id, cwd) => [ + { type: "title", v: 1, title: "" }, + { type: "session", version: 3, id, timestamp: "2026-08-27T00:00:00.000Z", cwd }, + ]; + writeJsonl(rootPath, header("root-native", repo)); + writeJsonl(childPath, header("child-native", subdir)); + writeJsonl(grandchildPath, header("grandchild-native", subdir)); + + const prevHome = process.env.HOME; + process.env.HOME = home; + try { + const config = loadConfig(repo, { discovery: { harnesses: ["pi"], since: "all", includeOmp: true } }); + config.state = new State(repo).ensure(); + const repository = { name: "demo", root: repo, worktrees: [repo], remotes: [] }; + const result = await discoverTranscripts({ repo: repository, config }); + const byNativeId = (nativeId) => result.transcripts.find((transcript) => transcript.nativeId === nativeId); + const root = byNativeId("root-native"); + const child = byNativeId("child-native"); + const grandchild = byNativeId("grandchild-native"); + + assert.ok(root && child && grandchild, "a subagent in a repo subdirectory still maps to this repository"); + assert.equal(child.cwd, subdir); + for (const descendant of [child, grandchild]) { + assert.equal(descendant.parentSessionId, "root-native"); + assert.equal(descendant.corroborationIdentity, root.identity); + assert.equal(descendant.corroborationNativeId, "root-native"); + assert.equal(descendant.interaction, NON_INTERACTIVE); + } + assert.equal(root.interaction, INTERACTIVE); + } finally { + if (prevHome === undefined) delete process.env.HOME; + else process.env.HOME = prevHome; + } +}); test("the sampler keeps both categories when a 98% non-interactive corpus exceeds the cap", () => { const interactive = Array.from({ length: 2 }, (_, i) => ({ diff --git a/test/isolation.test.js b/test/isolation.test.js index c207eb4e..1c200645 100644 --- a/test/isolation.test.js +++ b/test/isolation.test.js @@ -377,7 +377,7 @@ test("a later revision citing a revoked gap never carries the revoked wording in }, ], }); - await foldForRun( + const summary = await foldForRun( { config: { ...h.config, minGapEvidence: 2, gapLedgerMaxAge: "90d" }, repo: { root: h.root }, @@ -391,65 +391,81 @@ test("a later revision citing a revoked gap never carries the revoked wording in const prompt = recordedCalls().at(-1).prompt; assert.match(prompt, /Bind publication attestation/); assert.doesNotMatch(prompt, /Revoked old wording|revoked-skill/); - const observation = h.state.readGapLedger().entries.revoked.sessions[revised.identity]; + const observations = Object.values(h.state.readGapLedger().entries.revoked.sessions); + const historical = observations.find((observation) => observation.revision === "rev-1"); + assert.deepEqual(historical.phrasings, ["Revoked old wording"]); + assert.equal(historical.evidenceKey, "old"); + assert.equal(historical.coveredBySkill, "revoked-skill"); + const observation = observations.find((candidate) => candidate.revision === "rev-2"); + assert.equal(observation.sourceSessionId, revised.identity); + assert.equal(observation.evidenceKey, evidenceKey(revised, "memory-a")); assert.deepEqual(observation.phrasings, ["Bind publication attestation to its exact commit."]); assert.equal(observation.coveredBySkill, undefined); + assert.equal(summary.analyzedSessions, 1); + assert.deepEqual(summary.gaps, [], "revoked history cannot supply a second corroborating session"); }); -test("a grown native session keeps its unmentioned sighting on disk but out of prompts", async () => { - const h = setup({ agent: "claude", model: null, effort: null }); - const grown = { - harness: "claude", - id: "native-a", - nativeId: "native-a", - interaction: "interactive", - mtimeMs: 2, - bytes: 200, - }; - const identity = (grown.identity = transcriptIdentity(grown)); - const staleSighting = { - sourceKind: "native", - contentDigest: "1:100", - firstObservedAt: new Date().toISOString(), - phrasings: ["Stale grown gap"], - }; - h.state.writeGapLedger({ - version: 1, - entries: { - stale: { - id: "stale", - memoryPath: "AGENTS.md", - proposedInstruction: "Stale grown gap", - sessions: { [identity]: staleSighting }, +for (const related of [false, true]) + test(`a grown ${related ? "OMP child" : "native session"} keeps its unmentioned sighting on disk but out of prompts`, async () => { + const h = setup({ agent: "claude", model: null, effort: null }); + const grown = { + harness: related ? "pi" : "claude", + ...(related ? { parentSessionId: "parent", corroborationIdentity: "omp-parent" } : {}), + id: "native-a", + nativeId: "native-a", + identity: "", + interaction: "interactive", + mtimeMs: 2, + bytes: 200, + }; + const identity = (grown.identity = transcriptIdentity(grown)); + const observer = grown.corroborationIdentity || identity; + const staleSighting = { + ...(related ? { sourceSessionId: identity, sightingIds: [identity] } : {}), + sourceKind: "native", + contentDigest: "1:100", + firstObservedAt: new Date().toISOString(), + phrasings: ["Stale grown gap"], + }; + h.state.writeGapLedger({ + version: 1, + entries: { + stale: { + id: "stale", + memoryPath: "AGENTS.md", + proposedInstruction: "Stale grown gap", + sessions: { [observer]: staleSighting }, + }, }, - }, - }); - h.state.writeEvidence(grown, { - status: "ok", - transcript: grown, - memoryPath: "AGENTS.md", - memoryHash: "memory-a", - key: evidenceKey(grown, "memory-a"), - positive: [], - negative: [], - gaps: ["Bind publication attestation to its exact commit.", "Keep credential seats isolated per run."].map( - (proposedInstruction) => ({ - proposedInstruction, - mistake: "current mistake", - quote: "I checked the sample.", - recurrenceRisk: "high", - }), - ), + }); + h.state.writeEvidence(grown, { + status: "ok", + transcript: grown, + memoryPath: "AGENTS.md", + memoryHash: "memory-a", + key: evidenceKey(grown, "memory-a"), + positive: [], + negative: [], + gaps: ["Bind publication attestation to its exact commit.", "Keep credential seats isolated per run."].map( + (proposedInstruction) => ({ + proposedInstruction, + mistake: "current mistake", + quote: "I checked the sample.", + recurrenceRisk: "high", + }), + ), + }); + await foldForRun( + { config: { ...h.config, minGapEvidence: 2, gapLedgerMaxAge: "90d" }, repo: { root: h.root } }, + h.memoryFile, + "memory-a", + [], + [grown], + ); + const prompt = recordedCalls().at(-1).prompt; + assert.match(prompt, /Bind publication attestation/); + assert.doesNotMatch(prompt, /Stale grown gap/); + assert.deepEqual(h.state.readGapLedger().entries.stale.sessions, { + [observer]: { ...staleSighting, sourceSessionId: identity, sightingIds: [identity] }, + }); }); - await foldForRun( - { config: { ...h.config, minGapEvidence: 2, gapLedgerMaxAge: "90d" }, repo: { root: h.root } }, - h.memoryFile, - "memory-a", - [], - [grown], - ); - const prompt = recordedCalls().at(-1).prompt; - assert.match(prompt, /Bind publication attestation/); - assert.doesNotMatch(prompt, /Stale grown gap/); - assert.deepEqual(h.state.readGapLedger().entries.stale.sessions, { [identity]: staleSighting }); -}); diff --git a/test/nested.test.js b/test/nested.test.js index 27a9fcf2..8dab24a8 100644 --- a/test/nested.test.js +++ b/test/nested.test.js @@ -465,6 +465,115 @@ test("the fold keeps only the gap clusters this file owns, and names where the o assert.ok(!("routedGaps" in unrouted) && !("sourceSessions" in unrouted), "no routing, no new summary fields"); }); +test("an unknown native observer beside an API child keeps both the fold and proposal root-owned", () => { + const repo = makeRepo({ + "AGENTS.md": "# Root\n\n- Use pnpm.\n", + "apps/api/AGENTS.md": "# API\n\n- Keep handlers small.\n", + }); + const child = { identity: "native-api-child", cwd: repo.root }; + const attribution = new Map([ + [ + child.identity, + workedPaths(child, [{ kind: "tool", input: { path: "apps/api/handler.ts" } }], checkoutRoots(repo)), + ], + ]); + const observations = [ + { + proposedInstruction: API_GAP, + sessionId: "observer-api", + sourceSessionId: child.identity, + sightingIds: [child.identity], + source: "pi · api-child · 2026-08-02", + quote: "API child quote", + }, + { + proposedInstruction: API_GAP, + sessionId: "legacy-observer", + source: "pi · legacy-root · 2026-08-01", + quote: "unknown native quote", + }, + ]; + const rootRoute = routingFor([API], attribution, "AGENTS.md", null); + const rootSummary = foldEvidence([], { + minGapEvidence: 2, + gapObservations: observations, + route: rootRoute, + }); + assert.equal(rootSummary.gaps[0].sessions, 2); + for (const weight of [null, API.path]) { + const route = { ...rootRoute, weight, rootOwnedGaps: rootSummary.rootOwnedGaps }; + const summary = foldEvidence([], { minGapEvidence: 2, gapObservations: observations, route }); + if (weight === null) assert.equal(summary.gaps.length, 1); + else { + assert.deepEqual(summary.gaps, []); + assert.equal(summary.routedGaps[0].owner, "AGENTS.md"); + } + const staged = stageAndMeasure({ + repo, + memoryPath: weight ?? "AGENTS.md", + edit: (root) => writeIn(root, weight ?? "AGENTS.md", (text) => `${text}- ${API_GAP}\n`), + }); + const result = buildProposal( + { + edits: [ + { + changes: staged.measured.changes.map((change) => change.id), + kind: "add", + title: "API contract", + evidence: observations.map((observation) => ({ + polarity: "negative", + text: observation.quote, + source: observation.source, + })), + }, + ], + }, + { + memoryFile: staged.memoryFile, + measured: staged.measured, + config: { budgetTokens: 5000, maxEditsPerRun: 5, minGapEvidence: 2, skillsDir: ".agents/skills" }, + repo, + summary, + routing: route, + }, + ); + if (weight === null) { + assert.deepEqual(result.violations, []); + assert.equal(result.proposal.edits[0].transcripts, 2); + } else { + assert.equal(result.proposal.edits.length, 0); + assert.ok( + result.violations.some((violation) => violation.includes("cross-cutting evidence belongs in AGENTS.md")), + ); + } + const withoutRootOverride = buildProposal( + { + edits: [ + { + changes: staged.measured.changes.map((change) => change.id), + kind: "add", + title: "API contract", + evidence: observations.map((observation) => ({ + polarity: "negative", + text: observation.quote, + source: observation.source, + })), + }, + ], + }, + { + memoryFile: staged.memoryFile, + measured: staged.measured, + config: { budgetTokens: 5000, maxEditsPerRun: 5, minGapEvidence: 2, skillsDir: ".agents/skills" }, + repo, + summary: { ...summary, rootOwnedGaps: [] }, + routing: { ...route, rootOwnedGaps: [] }, + }, + ); + assert.equal(withoutRootOverride.proposal.edits.length, weight === null ? 1 : 0); + } +}); + test("mixed-directory sessions never enter a nested analysis corpus or own its gap", async () => { const transcripts = ["api-1", "both", "root-file-and-api"].map((identity) => ({ identity })); const { corpora } = await nestedCorpora({}, [API], transcripts, ATTRIBUTION); diff --git a/test/observer-provenance.test.js b/test/observer-provenance.test.js new file mode 100644 index 00000000..c3caa956 --- /dev/null +++ b/test/observer-provenance.test.js @@ -0,0 +1,512 @@ +import test from "node:test"; +import assert from "node:assert/strict"; + +import { foldForRun } from "../src/commands/propose.js"; +import { foldEvidence, renderEvidenceForPrompt } from "../src/fold.js"; +import { + emptyGapLedger, + filterGapLedger, + gapEntryId, + ledgerGapObservations, + mergeGapEntries, + normalizeGapLedgerSessions, + recordGapObservations, +} from "../src/gap-ledger.js"; +import { routingFor } from "../src/nested.js"; +import { buildProposal } from "../src/proposal.js"; +import { evidenceKey, State } from "../src/state.js"; +import { makeRepo, stageAndMeasure, writeIn } from "./helpers/staging.js"; + +const API = { path: "apps/api/AGENTS.md", dir: "apps/api" }; +const GAP = "Read docs/db.md before writing queries."; +const CURRENT_GAP = "Read docs/db.md before writing any production queries."; + +function evidence(id, observer, gaps = [GAP], content = id, memoryPath = "AGENTS.md") { + const transcript = { + id, + identity: id, + nativeId: id, + harness: "pi", + interaction: "interactive", + corroborationIdentity: observer, + contentSignature: content, + startedAt: Date.parse("2026-08-01T00:00:00Z"), + }; + return { + status: "ok", + transcript, + memoryPath, + memoryHash: "memory", + key: evidenceKey(transcript, "memory"), + positive: [], + negative: [], + gaps: gaps.map((proposedInstruction) => ({ + proposedInstruction, + mistake: "Re-derived query behavior", + quote: `${id} read the wrong schema`, + recurrenceRisk: "high", + })), + }; +} + +function context(repo) { + const state = new State(repo.root).ensure(); + return { config: { state, minGapEvidence: 2, gapLedgerMaxAge: "all" }, repo }; +} + +function propose(repo, summary, route) { + const memoryPath = route.weight ?? route.rootPath; + const staged = stageAndMeasure({ + repo, + memoryPath, + edit: (root) => writeIn(root, memoryPath, (text) => `${text}- ${GAP}\n`), + }); + return buildProposal( + { + edits: [ + { + changes: staged.measured.changes.map((change) => change.id), + kind: "add", + title: "Read the schema", + evidence: summary.gaps[0].quotes.map((quote) => ({ polarity: "negative", ...quote })), + }, + ], + }, + { + repo, + memoryFile: staged.memoryFile, + measured: staged.measured, + summary, + routing: route, + config: { budgetTokens: 5000, maxEditsPerRun: 5, minGapEvidence: 2, skillsDir: ".agents/skills" }, + }, + ); +} + +for (const persisted of [false, true]) { + test(`native child ownership survives ${persisted ? "persisted" : "direct"} fold and proposal`, async () => { + const repo = makeRepo({ "AGENTS.md": "# Root\n", [API.path]: "# API\n" }); + const ctx = context(repo); + const records = [evidence("P", "P", []), evidence("C", "P"), evidence("X", "X")]; + const attribution = new Map([ + ["P", ["README.md"]], + ["C", ["apps/api/c.js"]], + ["X", ["apps/api/x.js"]], + ]); + const rootRoute = routingFor([API], attribution, "AGENTS.md", null); + const fold = async (route) => { + if (!persisted) return foldEvidence(records, { minGapEvidence: 2, route }); + for (const record of records) ctx.config.state.writeEvidence(record.transcript.id, record); + return foldForRun( + ctx, + { path: "AGENTS.md", units: [] }, + "memory", + [], + records.map((record) => record.transcript), + { route }, + ); + }; + const root = await fold(rootRoute); + assert.deepEqual(root.gaps, [], "only C and X reported the gap, and both worked in the API"); + assert.equal(root.routedGaps[0].owner, API.path); + const apiRoute = { ...rootRoute, weight: API.path, rootOwnedGaps: root.rootOwnedGaps }; + const api = await fold(apiRoute); + assert.equal(api.gaps[0].sessions, 2); + const childQuote = api.gaps[0].quotes.find((quote) => quote.text.startsWith("C ")); + assert.equal(api.sourceObservers[childQuote.source], "P"); + assert.equal(api.sourceSessions[childQuote.source], "C"); + assert.deepEqual(propose(repo, api, apiRoute).violations, []); + const misplaced = propose(repo, api, rootRoute); + assert.equal(misplaced.proposal.edits.length, 0); + assert.ok(misplaced.violations.some((violation) => violation.includes(`it belongs in ${API.path}`))); + }); +} + +test("related native sightings retain cross-directory ownership and one observer vote", () => { + const repo = makeRepo({ "AGENTS.md": "# Root\n", [API.path]: "# API\n" }); + const records = [evidence("C", "P"), evidence("P", "P"), evidence("X", "X")]; + const attribution = new Map([ + ["P", ["README.md"]], + ["C", ["apps/api/c.js"]], + ["X", ["apps/api/x.js"]], + ]); + const route = routingFor([API], attribution, "AGENTS.md", null); + const root = foldEvidence(records, { route }); + assert.equal(root.gaps[0].sessions, 2, "P and C share one vote"); + assert.deepEqual(propose(repo, root, route).violations, []); + const nested = foldEvidence( + records.filter((record) => record.transcript.id !== "P"), + { + route: { ...route, weight: API.path, rootOwnedGaps: root.rootOwnedGaps }, + }, + ); + assert.deepEqual(nested.gaps, [], "the native child's real quote keeps the root ownership override"); +}); + +test("representative quote selection preserves native ownership beyond six observers", () => { + const repo = makeRepo({ "AGENTS.md": "# Root\n", [API.path]: "# API\n" }); + const records = Array.from({ length: 7 }, (_, index) => evidence(`C${index}`, `P${index}`)); + const attribution = new Map( + records.map((record, index) => [record.transcript.id, [index === 6 ? "README.md" : "apps/api/c.js"]]), + ); + const route = routingFor([API], attribution, "AGENTS.md", null); + const root = foldEvidence(records, { route }); + assert.equal(root.gaps[0].sessions, 7); + assert.equal(root.gaps[0].quotes.length, 6); + assert.ok(root.gaps[0].quotes.some((quote) => quote.text.startsWith("C6 "))); + assert.deepEqual(propose(repo, root, route).violations, []); +}); + +test("parent appearance cannot refresh a stale child's wording in the persisted ledger", async () => { + for (const currentChildGap of [false, true]) { + const repo = makeRepo({ "AGENTS.md": "# Root\n" }); + const ctx = context(repo); + const oldChild = evidence("C", "C", [GAP], "child-v1"); + ctx.config.state.writeEvidence("C", oldChild); + await foldForRun(ctx, { path: "AGENTS.md", units: [] }, "memory", [], [oldChild.transcript]); + const before = Object.values(ctx.config.state.readGapLedger().entries)[0].sessions.C; + const records = [ + evidence("P", "P", [CURRENT_GAP]), + evidence("C", "P", currentChildGap ? [CURRENT_GAP] : [], "child-v2"), + evidence("X", "X", [CURRENT_GAP]), + ]; + for (const record of records) ctx.config.state.writeEvidence(record.transcript.id, record); + for (let run = 0; run < 2; run += 1) { + const summary = await foldForRun( + ctx, + { path: "AGENTS.md", units: [] }, + "memory", + [], + records.map((record) => record.transcript), + ); + assert.equal(summary.gaps[0].sessions, 2); + assert.equal( + summary.gaps[0].proposedInstruction, + CURRENT_GAP, + "stale wording A cannot reach consolidation or fold", + ); + const sessions = Object.values(ctx.config.state.readGapLedger().entries)[0].sessions; + const historical = Object.values(sessions).find((observation) => observation.contentDigest === "child-v1"); + assert.equal(historical.sourceSessionId, "C"); + assert.deepEqual(historical.phrasings, [GAP]); + assert.equal(historical.firstObservedAt, before.firstObservedAt); + assert.equal(Object.values(sessions).filter((observation) => observation.sourceSessionId === "P").length, 1); + } + } +}); + +test("normalization and consolidation preserve original native, revision and route provenance", () => { + for (const axis of ["native", "content", "source", "revision", "route"]) { + const first = evidence("C", "P", [GAP], "v1"); + const second = evidence(axis === "native" ? "P" : "C", "P", [CURRENT_GAP], axis === "content" ? "v2" : "v1"); + if (["source", "revision", "route"].includes(axis)) { + for (const record of [first, second]) { + Object.assign(record.transcript, { sourceKind: "external", sourceId: "source-a", revision: "r1" }); + record.key = "route-a"; + } + if (axis === "source") second.transcript.sourceId = "source-b"; + if (axis === "revision") second.transcript.revision = "r2"; + if (axis === "route") second.key = "route-b"; + } + const ledger = emptyGapLedger(); + recordGapObservations(ledger, [first]); + const originalId = gapEntryId("AGENTS.md", GAP); + const original = structuredClone(ledger.entries[originalId].sessions.P); + const other = emptyGapLedger(); + recordGapObservations(other, [second]); + const secondId = gapEntryId("AGENTS.md", CURRENT_GAP); + ledger.entries[secondId] = other.entries[secondId]; + mergeGapEntries(ledger, [[originalId, secondId]]); + normalizeGapLedgerSessions(ledger, [second.transcript]); + const sessions = Object.values(ledger.entries)[0].sessions; + assert.equal(Object.keys(sessions).length, 2, axis); + assert.deepEqual( + Object.values(sessions).find( + (observation) => observation.quote === first.gaps[0].quote && observation.phrasings.includes(GAP), + ), + original, + axis, + ); + const filtered = filterGapLedger(structuredClone(ledger), [second], [second.transcript]); + const observations = ledgerGapObservations(filtered, "AGENTS.md"); + assert.ok( + observations.some((observation) => observation.phrasings.includes(CURRENT_GAP)), + axis, + ); + if (axis !== "native") + assert.ok( + observations.every((observation) => !observation.phrasings.includes(GAP)), + axis, + ); + } +}); + +for (const persisted of [false, true]) { + for (const coveredNative of [null, "P", "C"]) { + test(`eligible native projects: ${persisted ? "persisted" : "direct"}, ${coveredNative || "no"} coverage`, async () => { + for (const reverse of [false, true]) { + const projectA = makeRepo({ "AGENTS.md": coveredNative === "P" ? `# A\n\n- ${GAP}\n` : "# A\n" }); + const projectB = makeRepo({ "AGENTS.md": coveredNative === "C" ? `# B\n\n- ${GAP}\n` : "# B\n" }); + const projectD = makeRepo({ "AGENTS.md": "# D\n" }); + const userRepo = makeRepo({ "AGENTS.md": "# User\n" }); + const ctx = context(userRepo); + Object.assign(ctx.config, { minGapProjects: 2 }); + Object.assign(ctx, { scope: { kind: "user" } }); + const records = [evidence("P", "P"), evidence("C", "P"), evidence("X", "X")]; + for (const [index, record] of records.entries()) { + const project = index === 0 ? projectA : index === 1 ? projectB : coveredNative ? projectD : projectA; + Object.assign(record.transcript, { project: project.root, projectRoot: project.root }); + Object.assign(record.gaps[0], { + recurrenceRisk: index === 1 ? "high" : "low", + domain: index === 0 ? "orchestration" : "project", + ...(index < 2 ? { coveredBySkill: "db-docs" } : {}), + }); + } + if (reverse) records.reverse(); + let summary; + if (persisted) { + for (const record of records) ctx.config.state.writeEvidence(record.transcript.id, record); + summary = await foldForRun( + ctx, + { path: "AGENTS.md", units: [] }, + "memory", + [{ name: "db-docs", description: "", body: "" }], + records.map((record) => record.transcript), + ); + } else { + summary = foldEvidence(records, { minGapEvidence: 2, minGapProjects: 2, checkProjectCoverage: true }); + } + assert.equal(summary.gaps.length, 1, "two uncovered observers in two projects clear both floors"); + const gap = summary.gaps[0]; + assert.equal(gap.sessions, 2); + assert.equal(gap.projects, 2); + assert.equal(gap.recurrenceRisk, coveredNative === "C" ? "low" : "high"); + assert.equal(gap.orchestrationSightings, coveredNative === "C" ? 1 : 0); + assert.equal(gap.failedTriggerSkill, undefined, "two related skill citations remain one observer vote"); + assert.equal(gap.projectCoveredSessions, coveredNative ? 1 : 0); + assert.ok(!gap.quotes.some((quote) => coveredNative && quote.text.startsWith(`${coveredNative} `))); + if (coveredNative !== "C") assert.ok(gap.quotes.some((quote) => quote.text.startsWith("C "))); + + const staged = stageAndMeasure({ + repo: userRepo, + edit: (root) => writeIn(root, "AGENTS.md", (text) => `${text}- ${GAP}\n`), + }); + const built = buildProposal( + { + edits: [ + { + changes: staged.measured.changes.map((change) => change.id), + kind: "add", + title: "Read the schema", + evidence: gap.quotes.map((quote) => ({ polarity: "negative", ...quote })), + }, + ], + }, + { + repo: userRepo, + memoryFile: staged.memoryFile, + measured: staged.measured, + summary, + scope: { kind: "user" }, + config: { + budgetTokens: 5000, + maxEditsPerRun: 5, + minGapEvidence: 2, + minGapProjects: 2, + skillsDir: ".agents/skills", + }, + }, + ); + assert.deepEqual(built.violations, []); + assert.equal(built.proposal.edits[0].transcripts, 2); + assert.equal(built.proposal.edits[0].projects, 2); + } + }); + } +} + +test("native project witnesses survive quote limits without multiplying observer or skill votes", () => { + const records = Array.from({ length: 7 }, (_, index) => evidence(`C${index}`, "P")); + records.push(evidence("X", "X")); + for (const [index, record] of records.entries()) { + Object.assign(record.transcript, { project: index === 6 ? "/project-b" : "/project-a" }); + Object.assign(record.gaps[0], { coveredBySkill: index < 7 ? "db-docs" : null }); + } + const summary = foldEvidence(records, { minGapEvidence: 2, minGapProjects: 2 }); + assert.equal(summary.gaps[0].sessions, 2); + assert.equal(summary.gaps[0].projects, 2); + const quotes = summary.gaps[0].quotes; + assert.ok(quotes.some((quote) => quote.text.startsWith("C6 "))); + assert.ok(quotes.some((quote) => quote.text.startsWith("X "))); + assert.equal(summary.gaps[0].failedTriggerSkill, undefined); +}); + +test("repeated native evidence retains the highest eligible recurrence risk", async () => { + for (const persisted of [false, true]) { + for (const risks of [ + ["low", "high"], + ["high", "low"], + ]) { + const repeated = evidence("C", "P"); + repeated.gaps = risks.map((recurrenceRisk, index) => ({ + ...repeated.gaps[0], + quote: `C repeated the mistake ${index}`, + recurrenceRisk, + })); + const independent = evidence("X", "X"); + independent.gaps[0].recurrenceRisk = "low"; + const records = [repeated, independent]; + let summary; + if (persisted) { + const ctx = context(makeRepo({ "AGENTS.md": "# Memory\n" })); + for (const record of records) ctx.config.state.writeEvidence(record.transcript.id, record); + summary = await foldForRun( + ctx, + { path: "AGENTS.md", units: [] }, + "memory", + [], + records.map((record) => record.transcript), + ); + const native = Object.values(ctx.config.state.readGapLedger().entries)[0].sessions.P; + assert.equal(native.recurrenceRisk, "high"); + assert.equal(native.sourceSessionId, "C"); + } else { + summary = foldEvidence(records); + } + assert.equal(summary.gaps[0].sessions, 2); + assert.equal(summary.gaps[0].recurrenceRisk, "high"); + } + } +}); + +function emittedEvidence(summary) { + const prompt = renderEvidenceForPrompt(summary); + return [...prompt.matchAll(/^ {4}(?:([-+])(?: \[[^\]]+\])? )?"([^"]+)"(?: :: .*?)? \((.+)\)$/gm)].map((match) => ({ + polarity: match[1] === "+" ? "positive" : "negative", + text: match[2], + source: match[3], + })); +} + +for (const lane of ["gap", "positive", "negative"]) { + for (const [minGapEvidence, minGapProjects, maximumQuotes] of [ + [2, 0, 6], + [2, 3, 6], + [7, 3, 9], + [2, 8, 9], + ]) { + test(`emitted ${lane} witnesses satisfy observer=${minGapEvidence} and project=${minGapProjects} floors`, () => { + const repo = makeRepo({ "AGENTS.md": "# Memory\n\n- Existing rule.\n" }); + const related = Array.from({ length: 8 }, (_, index) => evidence(`F${index}`, "P")); + const independent = Array.from({ length: minGapEvidence - 1 }, (_, index) => evidence(`X${index}`, `X${index}`)); + const records = [...related, ...independent]; + for (const [index, record] of records.entries()) { + Object.assign(record.transcript, { project: `/project-${index < minGapProjects ? index : 0}` }); + if (lane !== "gap") { + record[lane] = [{ instruction: "AG-001", quote: `witness ${record.transcript.id}`, class: "harm" }]; + record.gaps = []; + } + } + const staged = stageAndMeasure({ + repo, + edit: (root) => writeIn(root, "AGENTS.md", (text) => `${text}- ${GAP}\n`), + }); + const summary = foldEvidence(records, { minGapEvidence, minGapProjects, memoryFile: staged.memoryFile }); + const citations = emittedEvidence(summary); + assert.ok(citations.length <= maximumQuotes); + const built = buildProposal( + { + edits: [ + { + changes: staged.measured.changes.map((change) => change.id), + kind: "add", + title: "Use the emitted witnesses", + evidence: citations, + }, + ], + }, + { + repo, + memoryFile: staged.memoryFile, + measured: staged.measured, + summary, + scope: minGapProjects ? { kind: "user" } : null, + config: { + budgetTokens: 5000, + maxEditsPerRun: 5, + minGapEvidence, + minGapProjects, + skillsDir: ".agents/skills", + }, + }, + ); + assert.deepEqual(built.violations, [], "the final emitted evidence must carry the witnesses needed by the gates"); + assert.equal(built.proposal.edits[0].transcripts, minGapEvidence); + if (minGapProjects) assert.equal(built.proposal.edits[0].projects, minGapProjects); + }); + } +} + +for (const boundary of ["recording", "normalization", "consolidation"]) { + for (const stale of [false, true]) { + test(`${boundary} preserves compatible risk maxima without refreshing ${stale ? "stale" : "current"} provenance`, async () => { + for (const highFirst of [false, true]) { + const ctx = context(makeRepo({ "AGENTS.md": "# Memory\n" })); + const first = evidence("C", "P"); + first.gaps[0].recurrenceRisk = highFirst || stale ? "high" : "low"; + const second = evidence("C", "P"); + second.gaps[0].recurrenceRisk = highFirst || stale ? "low" : "high"; + if (stale) first.transcript.contentSignature = "old-child"; + first.key = evidenceKey(first.transcript, "memory"); + const ledger = emptyGapLedger(); + recordGapObservations(ledger, [first]); + const entry = Object.values(ledger.entries)[0]; + const original = structuredClone(entry.sessions.P); + if (boundary === "recording") { + recordGapObservations(ledger, [second]); + } else if (boundary === "normalization") { + const alias = emptyGapLedger(); + recordGapObservations(alias, [second]); + entry.sessions.C = Object.values(alias.entries)[0].sessions.P; + normalizeGapLedgerSessions(ledger, [second.transcript]); + } else { + const other = emptyGapLedger(); + second.gaps[0].proposedInstruction = "Inspect database contracts before editing SQL."; + recordGapObservations(other, [second]); + const otherEntry = Object.values(other.entries)[0]; + ledger.entries[otherEntry.id] = otherEntry; + mergeGapEntries(ledger, [[entry.id, otherEntry.id]]); + } + ctx.config.state.writeGapLedger(ledger); + second.gaps = []; + const independent = evidence("X", "X"); + independent.gaps[0].recurrenceRisk = "low"; + for (const record of [second, independent]) ctx.config.state.writeEvidence(record.transcript.id, record); + const summary = await foldForRun( + ctx, + { path: "AGENTS.md", units: [] }, + "memory", + [], + [second.transcript, independent.transcript], + ); + assert.equal(summary.gaps[0].sessions, 2); + assert.equal(summary.gaps[0].recurrenceRisk, stale ? "low" : "high"); + const observations = Object.values(ctx.config.state.readGapLedger().entries).flatMap((candidate) => + Object.values(candidate.sessions), + ); + if (stale) { + const history = observations.find((observation) => observation.contentDigest === "old-child"); + assert.equal(history.sourceSessionId, "C"); + assert.equal(history.recurrenceRisk, "high"); + assert.equal(history.firstObservedAt, original.firstObservedAt); + assert.equal(history.evidenceKey, original.evidenceKey); + } else { + const native = observations.find((observation) => observation.sourceSessionId === "C"); + assert.equal(native.recurrenceRisk, "high"); + assert.equal(native.contentDigest, "C"); + assert.equal(native.evidenceKey, original.evidenceKey); + } + } + }); + } +} diff --git a/test/proposal.test.js b/test/proposal.test.js index fad59e9d..4f784b78 100644 --- a/test/proposal.test.js +++ b/test/proposal.test.js @@ -595,6 +595,58 @@ test("a quote counts as a session only when the fold issued its source label", ( ); }); +test("native OMP quote labels still measure one proposal observer per root", () => { + const observed = (nativeId, observer) => ({ + status: "ok", + transcript: { + id: `pi-${nativeId}`, + identity: `pi-file-${nativeId}`, + nativeId, + harness: "pi", + startedAt: Date.parse("2026-08-02T00:00:00Z"), + corroborationIdentity: observer, + corroborationNativeId: observer, + corroborationStartedAt: Date.parse("2026-08-01T00:00:00Z"), + interaction: "non-interactive", + }, + negative: [{ instruction: "AG-001", quote: `harm ${nativeId}`, class: "harm" }], + gaps: [{ proposedInstruction: "Read the API contract first.", quote: `gap ${nativeId}` }], + }); + for (const independent of [false, true]) { + const summary = foldEvidence([ + observed("child-a", "root-a"), + observed("child-b", independent ? "root-b" : "root-a"), + ]); + assert.equal(summary.analyzedSessions, 2); + assert.equal(summary.sources.length, 2); + assert.equal(summary.instructions[0].harmSessions, independent ? 2 : 1); + assert.equal(summary.gaps.length, independent ? 1 : 0); + const result = gate({ + edit: memoryEdit(REWRITE_SHAPES["append a sentence"]), + annotation: { + edits: [ + claim(["H1"], { + kind: "rewrite", + evidence: summary.instructions[0].quotes.map((quote) => ({ + polarity: "negative", + text: quote.text, + source: quote.source, + })), + }), + ], + }, + context: { summary }, + }); + if (independent) { + assert.deepEqual(result.violations, []); + assert.equal(result.proposal.edits[0].transcripts, 2); + } else { + assert.equal(result.proposal.edits.length, 0); + assert.ok(result.violations.some((violation) => /backed by 1 session\(s\); 2 are required/.test(violation))); + } + } +}); + test("on the r1 dry-run corpus, only the edit whose second source was never issued is refused", () => { const foldRecord = (id, day, quote) => ({ status: "ok", diff --git a/test/remote-discovery.test.js b/test/remote-discovery.test.js index 9658a6d6..f66319de 100644 --- a/test/remote-discovery.test.js +++ b/test/remote-discovery.test.js @@ -4,8 +4,21 @@ import fs from "node:fs"; import path from "node:path"; import { applyHostFlag, loadConfig } from "../src/config.js"; -import { discoverProject, initRepo, sshCalls, tmpdir, withRemoteEnv, writeClaudeSession } from "./helpers/remote.js"; -import { disambiguateSourceLabels, gapSource } from "../src/gap-ledger.js"; +import { + discoverProject, + initRepo, + projectRun, + sshCalls, + tmpdir, + withRemoteEnv, + writeClaudeSession, +} from "./helpers/remote.js"; +import { + disambiguateSourceLabels, + gapSource, + ledgerGapObservations, + recordGapObservations, +} from "../src/gap-ledger.js"; import { classifySshFailure, closeSshMasters } from "../src/discovery/remote/ssh.js"; import { discoverTranscripts } from "../src/discovery/index.js"; import { resolveHostList } from "../src/discovery/hosts.js"; @@ -14,6 +27,9 @@ import { State } from "../src/state.js"; import { SELF_SESSION_SENTINEL } from "../src/sentinel.js"; import { UserError, setLoggerSink } from "../src/logger.js"; import { clearProgressSink, setProgressSink } from "../src/progress.js"; +import { foldEvidence } from "../src/fold.js"; +import { assertNativeGapContributions } from "./helpers/gap-observers.js"; +import { INTERACTIVE, NON_INTERACTIVE } from "../src/interaction.js"; const REMOTE = "github.com/acme/demo"; @@ -40,6 +56,692 @@ function scenario({ variant = {}, cwdOverride = null, sessionText = null } = {}) }; } +function writeOmpSession(file, id, cwd, timestamp = "2026-08-27T00:00:00.000Z") { + fs.mkdirSync(path.dirname(file), { recursive: true }); + fs.writeFileSync( + file, + `${JSON.stringify({ type: "title", v: 1, title: "" })}\n` + + `${JSON.stringify({ type: "session", version: 3, id, timestamp, cwd })}\n`, + ); +} + +async function withRemotePiEnv(scenario, fn) { + const keys = ["PI_CODING_AGENT_DIR", "PI_CODING_AGENT_SESSION_DIR", "BB_DATA_DIR", "BB_PI_BRIDGE_SESSION_DIR"]; + const previous = Object.fromEntries(keys.map((key) => [key, process.env[key]])); + for (const key of keys) delete process.env[key]; + try { + return await withRemoteEnv({ localHome: scenario.localHome, hosts: scenario.hosts }, fn); + } finally { + for (const key of keys) { + if (previous[key] === undefined) delete process.env[key]; + else process.env[key] = previous[key]; + } + } +} + +function observerRecords(transcripts) { + return transcripts.map((transcript) => ({ + status: "ok", + memoryPath: "AGENTS.md", + transcript, + negative: [{ instruction: "db-rule", class: "harm", quote: "following the rule caused damage" }], + gaps: [ + { + proposedInstruction: "Read docs/db.md before writing queries.", + mistake: "re-derived the schema", + quote: "read the schema", + recurrenceRisk: "high", + domain: "project", + }, + ], + })); +} + +function assertSingletonObserver(transcripts, identity) { + const records = observerRecords(transcripts); + const folded = foldEvidence(records, { minGapEvidence: 2 }); + assert.equal(folded.gaps.length, 0); + assert.equal(folded.totals.droppedGapSingletons, 1); + assert.equal(folded.instructions[0].harmSessions, 1); + const ledger = { version: 1, entries: {} }; + recordGapObservations(ledger, records); + assert.ok(transcripts.every((transcript) => transcript.corroborationIdentity === identity)); + assertNativeGapContributions(ledger, transcripts); + const persisted = foldEvidence(records, { + minGapEvidence: 2, + gapObservations: ledgerGapObservations(ledger, "AGENTS.md"), + }); + assert.equal(persisted.gaps.length, 0); + assert.equal(persisted.instructions[0].harmSessions, 1); +} + +test("remote OMP subagents keep their parent's corroboration identity", async () => { + const s = scenario(); + const sessionDir = path.join(s.remoteHome, ".omp", "agent", "sessions", "-repo-demo"); + const parentName = "2026-08-27T00-00-00.000Z_parent-folder"; + const parentPath = path.join(sessionDir, `${parentName}.jsonl`); + const childPath = path.join(sessionDir, parentName, "Subagent.jsonl"); + writeOmpSession(parentPath, "parent-native", s.remoteClone); + writeOmpSession(childPath, "child-native", s.remoteClone, "2026-08-27T00:01:00.000Z"); + + const piEnv = ["PI_CODING_AGENT_DIR", "PI_CODING_AGENT_SESSION_DIR", "BB_DATA_DIR", "BB_PI_BRIDGE_SESSION_DIR"]; + const previous = Object.fromEntries(piEnv.map((key) => [key, process.env[key]])); + for (const key of piEnv) delete process.env[key]; + let result; + try { + const disabled = await withRemoteEnv({ localHome: s.localHome, hosts: s.hosts }, () => + discoverProject(s.repoRoot, { + discovery: { hosts: ["mac-home"], harnesses: ["pi"], since: "all" }, + }), + ); + assert.deepEqual(disabled.transcripts, [], "the remote OMP store is opt-in too"); + result = await withRemoteEnv({ localHome: s.localHome, hosts: s.hosts }, () => + discoverProject(s.repoRoot, { + discovery: { hosts: ["mac-home"], harnesses: ["pi"], since: "all", includeOmp: true }, + }), + ); + } finally { + for (const key of piEnv) { + if (previous[key] === undefined) delete process.env[key]; + else process.env[key] = previous[key]; + } + } + + const parent = result.transcripts.find((transcript) => transcript.nativeId === "parent-native"); + const child = result.transcripts.find((transcript) => transcript.nativeId === "child-native"); + assert.ok(parent && child); + assert.notEqual(parent.identity, child.identity); + assert.equal(child.parentSessionId, "parent-native"); + assert.equal(child.corroborationIdentity, parent.identity); + assert.equal(child.corroborationNativeId, "parent-native"); + assert.equal(child.corroborationStartedAt, parent.startedAt); +}); + +test("a copied OMP root and retained remote descendants count as one observer without merging local split roots", async () => { + const s = scenario(); + const rootName = "2026-08-27T00-00-00.000Z_root"; + const localRoot = path.join(s.localHome, ".omp", "agent", "sessions", "-repo-demo"); + const remoteRoot = path.join(s.remoteHome, ".omp", "agent", "sessions", "-repo-demo"); + writeOmpSession(path.join(localRoot, `${rootName}.jsonl`), "root-native", s.repoRoot); + writeOmpSession(path.join(remoteRoot, `${rootName}.jsonl`), "root-native", s.remoteClone); + writeOmpSession(path.join(remoteRoot, rootName, "Child.jsonl"), "child-native", s.remoteClone); + writeOmpSession( + path.join(remoteRoot, rootName, "Child", "Child.Grandchild.jsonl"), + "grandchild-native", + s.remoteClone, + ); + const discover = () => + withRemotePiEnv(s, () => + discoverProject(s.repoRoot, { + discovery: { hosts: ["mac-home"], harnesses: ["pi"], since: "all", includeOmp: true }, + }), + ); + const result = await discover(); + assert.equal(result.transcripts.length, 3); + assert.equal(result.perHost[0].duplicates, 1); + const root = result.transcripts.find((transcript) => transcript.nativeId === "root-native"); + assert.equal(root.host, null); + assert.equal(new Set(result.transcripts.map((transcript) => transcript.identity)).size, 3); + assert.equal(new Set(result.transcripts.map((transcript) => transcript.nativeId)).size, 3); + for (const descendant of result.transcripts.filter((transcript) => transcript !== root)) { + assert.equal(descendant.host, "mac-home"); + assert.ok(descendant.path.startsWith(remoteRoot)); + assert.equal(descendant.parentSessionId, "root-native"); + assert.equal(descendant.corroborationIdentity, root.identity); + assert.equal(descendant.corroborationNativeId, "root-native"); + assert.equal(descendant.interaction, NON_INTERACTIVE); + } + const records = result.transcripts.map((transcript) => ({ + status: "ok", + memoryPath: "AGENTS.md", + transcript, + gaps: [ + { + proposedInstruction: "Read docs/db.md before writing queries.", + mistake: "re-derived the schema", + quote: "read the schema", + recurrenceRisk: "high", + domain: "project", + }, + ], + })); + const folded = foldEvidence(records, { minGapEvidence: 2 }); + assert.equal(folded.gaps.length, 0); + assert.equal(folded.totals.droppedGapSingletons, 1); + const ledger = { version: 1, entries: {} }; + recordGapObservations(ledger, records); + assertNativeGapContributions(ledger, result.transcripts); + + const splitPath = path.join(s.localHome, ".omp", "agent", "sessions", "-repo-demo", "split-root.jsonl"); + writeOmpSession(splitPath, "root-native", s.repoRoot); + writeOmpSession(path.join(localRoot, rootName, "Child.jsonl"), "child-native", s.repoRoot); + writeOmpSession(path.join(remoteRoot, "independent.jsonl"), "independent-native", s.remoteClone); + const split = await discover(); + const roots = split.transcripts.filter((transcript) => transcript.nativeId === "root-native"); + assert.equal(roots.length, 2); + assert.ok(roots.every((transcript) => transcript.host === null)); + assert.equal(new Set(roots.map((transcript) => transcript.corroborationIdentity)).size, 2); + const localChild = split.transcripts.find((transcript) => transcript.nativeId === "child-native"); + assert.equal(localChild.host, null); + assert.equal(localChild.corroborationIdentity, root.identity); + const grandchild = split.transcripts.find((transcript) => transcript.nativeId === "grandchild-native"); + const splitRoot = roots.find((transcript) => transcript.path === splitPath); + assert.equal(grandchild.corroborationIdentity, localChild.corroborationIdentity); + assert.notEqual(grandchild.corroborationIdentity, splitRoot.identity); + assertSingletonObserver([localChild, grandchild], root.identity); + const independentRootEvidence = foldEvidence(observerRecords([localChild, grandchild, splitRoot]), { + minGapEvidence: 2, + }); + assert.equal(independentRootEvidence.gaps.length, 1); + assert.equal(independentRootEvidence.instructions[0].harmSessions, 2); + const independent = split.transcripts.find((transcript) => transcript.nativeId === "independent-native"); + assert.ok(independent); + assert.equal(independent.corroborationIdentity, independent.identity); + assert.ok(roots.every((transcript) => transcript.corroborationIdentity !== independent.identity)); +}); + +for (const remoteOnly of [false, true]) { + for (const filtered of [false, true]) { + test(`copied orphan siblings reconcile every readable-root correspondence (${remoteOnly ? "remote-only" : "local"}, ${filtered ? "filtered" : "all"})`, async () => { + const s = scenario(); + const now = Date.parse("2026-08-28T12:00:00.000Z"); + const old = "2026-08-27T00:00:00.000Z"; + const recent = "2026-08-28T11:00:00.000Z"; + const firstHome = remoteOnly ? s.remoteHome : s.localHome; + const firstCwd = remoteOnly ? s.remoteClone : s.repoRoot; + const secondHome = remoteOnly ? tmpdir("remote-other") : s.remoteHome; + const secondCwd = remoteOnly + ? initRepo(path.join(secondHome, "code", "demo"), `https://${REMOTE}.git`) + : s.remoteClone; + if (remoteOnly) s.hosts["mac-other"] = { home: secondHome }; + const rootName = "2026-08-27T00-00-00.000Z_root"; + const firstDirectory = path.join(firstHome, ".omp", "agent", "sessions", "-repo-demo"); + const secondDirectory = path.join(secondHome, ".omp", "agent", "sessions", "-repo-demo"); + const write = (file, id, cwd, timestamp) => { + writeOmpSession(file, id, cwd, timestamp); + fs.utimesSync(file, Date.parse(timestamp) / 1000, Date.parse(timestamp) / 1000); + }; + write(path.join(firstDirectory, `${rootName}.jsonl`), "root-native", firstCwd, old); + fs.writeFileSync(path.join(firstDirectory, `${rootName}.jsonl`), "not a session header\n"); + write(path.join(secondDirectory, `${rootName}.jsonl`), "root-native", secondCwd, old); + for (const [name, id] of [ + ["A", "a-native"], + ["B", "b-native"], + ]) { + write(path.join(firstDirectory, rootName, `${name}.jsonl`), id, firstCwd, recent); + write(path.join(secondDirectory, rootName, `${name}.jsonl`), id, secondCwd, recent); + } + write(path.join(secondDirectory, rootName, "C.jsonl"), "c-native", secondCwd, recent); + const result = await withRemotePiEnv(s, async () => { + const options = projectRun(s.repoRoot, { + discovery: { + hosts: remoteOnly ? ["mac-home", "mac-other"] : ["mac-home"], + harnesses: ["pi"], + since: filtered ? "1d" : "all", + includeOmp: true, + }, + }); + const discovered = await discoverTranscripts({ ...options, now }); + await closeSshMasters(discovered.remoteMasters); + return discovered; + }); + assert.equal(result.transcripts.length, filtered ? 3 : 4); + assert.equal(result.perHost.at(-1).duplicates, 2); + const a = result.transcripts.find((transcript) => transcript.nativeId === "a-native"); + const b = result.transcripts.find((transcript) => transcript.nativeId === "b-native"); + const c = result.transcripts.find((transcript) => transcript.nativeId === "c-native"); + assert.ok(a && b && c); + assert.notEqual(a.identity, b.identity); + for (const transcript of [a, b]) { + assert.equal(transcript.host, remoteOnly ? "mac-home" : null); + assert.equal(transcript.parentSessionId, undefined); + assert.equal(transcript.corroborationNativeId, transcript.nativeId); + assert.equal(transcript.interactionSignals.source, "subagent"); + assert.equal(transcript.interaction, NON_INTERACTIVE); + } + assert.equal(c.host, remoteOnly ? "mac-other" : "mac-home"); + assert.equal(c.parentSessionId, "root-native"); + assert.equal(c.corroborationNativeId, "root-native"); + assert.equal(c.corroborationStartedAt, Date.parse(old)); + assert.equal(new Set(result.transcripts.map((transcript) => transcript.corroborationIdentity)).size, 1); + assertSingletonObserver(result.transcripts, a.corroborationIdentity); + }); + } +} + +for (const filtered of [false, true]) { + test(`established orphan correspondence wins native-root fallback on later hosts (${filtered ? "filtered" : "all"})`, async () => { + const s = scenario(); + const otherHome = tmpdir("remote-other"); + const otherClone = initRepo(path.join(otherHome, "code", "demo"), `https://${REMOTE}.git`); + s.hosts["mac-other"] = { home: otherHome }; + const now = Date.parse("2026-08-28T12:00:00.000Z"); + const old = "2026-08-27T00:00:00.000Z"; + const recent = "2026-08-28T11:00:00.000Z"; + const rootName = "2026-08-27T00-00-00.000Z_root"; + const localDirectory = path.join(s.localHome, ".omp", "agent", "sessions", "-repo-demo"); + const remoteDirectory = path.join(s.remoteHome, ".omp", "agent", "sessions", "-repo-demo"); + const otherDirectory = path.join(otherHome, ".omp", "agent", "sessions", "-repo-demo"); + const independentPath = path.join(s.localHome, ".omp", "agent", "sessions", "-repo-demo", "split-root.jsonl"); + const write = (file, id, cwd, timestamp) => { + writeOmpSession(file, id, cwd, timestamp); + fs.utimesSync(file, Date.parse(timestamp) / 1000, Date.parse(timestamp) / 1000); + }; + write(independentPath, "root-native", s.repoRoot, recent); + write(path.join(localDirectory, `${rootName}.jsonl`), "root-native", s.repoRoot, old); + fs.writeFileSync(path.join(localDirectory, `${rootName}.jsonl`), "not a session header\n"); + write(path.join(localDirectory, rootName, "A.jsonl"), "a-native", s.repoRoot, recent); + write(path.join(remoteDirectory, `${rootName}.jsonl`), "root-native", s.remoteClone, old); + write(path.join(remoteDirectory, rootName, "A.jsonl"), "a-native", s.remoteClone, recent); + write(path.join(remoteDirectory, rootName, "B.jsonl"), "b-native", s.remoteClone, recent); + write(path.join(otherDirectory, `${rootName}.jsonl`), "root-native", otherClone, old); + write(path.join(otherDirectory, rootName, "C.jsonl"), "c-native", otherClone, recent); + const result = await withRemotePiEnv(s, async () => { + const options = projectRun(s.repoRoot, { + discovery: { + hosts: ["mac-home", "mac-other"], + harnesses: ["pi"], + since: filtered ? "1d" : "all", + includeOmp: true, + }, + }); + const discovered = await discoverTranscripts({ ...options, now }); + await closeSshMasters(discovered.remoteMasters); + return discovered; + }); + assert.equal(result.transcripts.length, 4); + assert.deepEqual( + result.perHost.map((host) => host.duplicates), + filtered ? [1, 0] : [2, 1], + ); + const independent = result.transcripts.find((transcript) => transcript.path === independentPath); + const a = result.transcripts.find((transcript) => transcript.nativeId === "a-native"); + const b = result.transcripts.find((transcript) => transcript.nativeId === "b-native"); + const c = result.transcripts.find((transcript) => transcript.nativeId === "c-native"); + assert.ok(independent && a && b && c); + assert.equal(independent.host, null); + assert.equal(independent.interaction, INTERACTIVE); + assert.equal(independent.parentSessionId, undefined); + assert.equal(independent.corroborationIdentity, independent.identity); + assert.equal(a.host, null); + assert.equal(a.parentSessionId, undefined); + assert.equal(a.interactionSignals.source, "subagent"); + assert.equal(a.corroborationNativeId, "a-native"); + assert.equal(a.corroborationIdentity, a.identity); + assert.equal(b.host, "mac-home"); + assert.equal(c.host, "mac-other"); + for (const transcript of [b, c]) { + assert.equal(transcript.parentSessionId, "root-native"); + assert.equal(transcript.corroborationNativeId, "root-native"); + assert.equal(transcript.corroborationStartedAt, Date.parse(old)); + assert.equal(transcript.corroborationIdentity, a.identity); + assert.equal(transcript.interaction, NON_INTERACTIVE); + assert.notEqual(transcript.identity, a.identity); + } + assert.notEqual(independent.corroborationIdentity, a.corroborationIdentity); + assertSingletonObserver([b, c], a.identity); + assertSingletonObserver([a, b, c], a.identity); + const records = observerRecords([independent, b, c]); + const folded = foldEvidence(records, { minGapEvidence: 2 }); + assert.equal(folded.gaps.length, 1); + assert.equal(folded.instructions[0].harmSessions, 2); + const ledger = { version: 1, entries: {} }; + recordGapObservations(ledger, records); + assertNativeGapContributions(ledger, [independent, b, c]); + const persisted = foldEvidence(records, { + minGapEvidence: 2, + gapObservations: ledgerGapObservations(ledger, "AGENTS.md"), + }); + assert.equal(persisted.gaps.length, 1); + assert.equal(persisted.instructions[0].harmSessions, 2); + }); +} + +for (const remoteOnly of [false, true]) { + for (const filtered of [false, true]) { + test(`copied descendants retain their split-root family (${remoteOnly ? "remote-only" : "local"}, ${filtered ? "filtered" : "all"})`, async () => { + const s = scenario(); + const now = Date.parse("2026-08-28T12:00:00.000Z"); + const old = "2026-08-27T00:00:00.000Z"; + const recent = "2026-08-28T11:00:00.000Z"; + const firstHome = remoteOnly ? s.remoteHome : s.localHome; + const firstCwd = remoteOnly ? s.remoteClone : s.repoRoot; + const secondHome = remoteOnly ? tmpdir("remote-other") : s.remoteHome; + const secondCwd = remoteOnly + ? initRepo(path.join(secondHome, "code", "demo"), `https://${REMOTE}.git`) + : s.remoteClone; + if (remoteOnly) s.hosts["mac-other"] = { home: secondHome }; + const rootName = "2026-08-27T00-00-00.000Z_root"; + const familyDirectory = path.join(firstHome, ".omp", "agent", "sessions", "-repo-demo"); + const independentPath = path.join(familyDirectory, "split-root.jsonl"); + const familyPath = path.join(familyDirectory, `${rootName}.jsonl`); + const childPath = path.join(familyDirectory, rootName, "Child.jsonl"); + const copiedDirectory = path.join(secondHome, ".omp", "agent", "sessions", "-repo-demo"); + const grandchildPath = path.join(copiedDirectory, rootName, "Child", "Child.Grandchild.jsonl"); + const write = (file, id, cwd, timestamp) => { + writeOmpSession(file, id, cwd, timestamp); + fs.utimesSync(file, Date.parse(timestamp) / 1000, Date.parse(timestamp) / 1000); + }; + write(independentPath, "root-native", firstCwd, recent); + write(familyPath, "root-native", firstCwd, old); + write(childPath, "child-native", firstCwd, recent); + write(path.join(copiedDirectory, `${rootName}.jsonl`), "root-native", secondCwd, old); + write(path.join(copiedDirectory, rootName, "Child.jsonl"), "child-native", secondCwd, recent); + write(grandchildPath, "grandchild-native", secondCwd, recent); + const result = await withRemotePiEnv(s, async () => { + const options = projectRun(s.repoRoot, { + discovery: { + hosts: remoteOnly ? ["mac-home", "mac-other"] : ["mac-home"], + harnesses: ["pi"], + since: filtered ? "1d" : "all", + includeOmp: true, + }, + }); + const discovered = await discoverTranscripts({ ...options, now }); + await closeSshMasters(discovered.remoteMasters); + return discovered; + }); + assert.equal(result.transcripts.length, filtered ? 3 : 4); + assert.equal(result.perHost.at(-1).duplicates, filtered ? 1 : 2); + const independent = result.transcripts.find((transcript) => transcript.path === independentPath); + const family = result.transcripts.find((transcript) => transcript.path === familyPath); + const child = result.transcripts.find((transcript) => transcript.nativeId === "child-native"); + const grandchild = result.transcripts.find((transcript) => transcript.nativeId === "grandchild-native"); + assert.ok(independent && child && grandchild); + assert.equal(child.path, childPath); + assert.equal(child.host, remoteOnly ? "mac-home" : null); + assert.equal(grandchild.path, grandchildPath); + assert.equal(grandchild.host, remoteOnly ? "mac-other" : "mac-home"); + assert.notEqual(child.identity, grandchild.identity); + assert.equal(independent.nativeId, "root-native"); + assert.equal(independent.corroborationIdentity, independent.identity); + assert.notEqual(independent.corroborationIdentity, child.corroborationIdentity); + assert.equal(grandchild.corroborationIdentity, child.corroborationIdentity); + for (const transcript of [child, grandchild]) { + assert.equal(transcript.parentSessionId, "root-native"); + assert.equal(transcript.corroborationNativeId, "root-native"); + assert.equal(transcript.corroborationStartedAt, Date.parse(old)); + assert.equal(transcript.interaction, NON_INTERACTIVE); + } + if (filtered) { + assert.equal(family, undefined); + } else { + assert.ok(family); + assert.equal(family.nativeId, independent.nativeId); + assert.notEqual(family.identity, independent.identity); + assert.equal(family.corroborationIdentity, family.identity); + assert.equal(child.corroborationIdentity, family.identity); + } + assertSingletonObserver([child, grandchild], child.corroborationIdentity); + const records = observerRecords([child, grandchild, independent]); + const folded = foldEvidence(records, { minGapEvidence: 2 }); + assert.equal(folded.gaps.length, 1); + assert.equal(folded.instructions[0].harmSessions, 2); + const ledger = { version: 1, entries: {} }; + recordGapObservations(ledger, records); + assertNativeGapContributions(ledger, [child, grandchild, independent]); + const persisted = foldEvidence(records, { + minGapEvidence: 2, + gapObservations: ledgerGapObservations(ledger, "AGENTS.md"), + }); + assert.equal(persisted.gaps.length, 1); + assert.equal(persisted.instructions[0].harmSessions, 2); + }); + } +} + +for (const remoteOnly of [false, true]) { + for (const missingFirstRoot of [false, true]) { + for (const filtered of [false, true]) { + test(`copied descendants share an observer with a missing ${missingFirstRoot ? "retained" : "dropped"} ancestor (${remoteOnly ? "remote-only" : "local"}, ${filtered ? "filtered" : "all"})`, async () => { + const s = scenario(); + const now = Date.parse("2026-08-28T12:00:00.000Z"); + const old = "2026-08-27T00:00:00.000Z"; + const recent = "2026-08-28T11:00:00.000Z"; + const firstHome = remoteOnly ? s.remoteHome : s.localHome; + const firstCwd = remoteOnly ? s.remoteClone : s.repoRoot; + const secondHome = remoteOnly ? tmpdir("remote-other") : s.remoteHome; + const secondCwd = remoteOnly + ? initRepo(path.join(secondHome, "code", "demo"), `https://${REMOTE}.git`) + : s.remoteClone; + if (remoteOnly) s.hosts["mac-other"] = { home: secondHome }; + const rootName = "2026-08-27T00-00-00.000Z_root"; + const firstDirectory = path.join(firstHome, ".omp", "agent", "sessions", "-repo-demo"); + const secondDirectory = path.join(secondHome, ".omp", "agent", "sessions", "-repo-demo"); + const firstRootPath = path.join(firstDirectory, `${rootName}.jsonl`); + const secondRootPath = path.join(secondDirectory, `${rootName}.jsonl`); + const childPath = path.join(firstDirectory, rootName, "Child.jsonl"); + const copiedChildPath = path.join(secondDirectory, rootName, "Child.jsonl"); + const grandchildPath = path.join(secondDirectory, rootName, "Child", "Child.Grandchild.jsonl"); + const independentPath = path.join(firstHome, ".omp", "agent", "sessions", "-repo-demo", "split-root.jsonl"); + const write = (file, id, cwd, timestamp) => { + writeOmpSession(file, id, cwd, timestamp); + fs.utimesSync(file, Date.parse(timestamp) / 1000, Date.parse(timestamp) / 1000); + }; + write(independentPath, "root-native", firstCwd, recent); + write(firstRootPath, "root-native", firstCwd, old); + write(secondRootPath, "root-native", secondCwd, old); + fs.writeFileSync(missingFirstRoot ? firstRootPath : secondRootPath, "not a session header\n"); + write(childPath, "child-native", firstCwd, recent); + write(copiedChildPath, "child-native", secondCwd, recent); + write(grandchildPath, "grandchild-native", secondCwd, recent); + const result = await withRemotePiEnv(s, async () => { + const options = projectRun(s.repoRoot, { + discovery: { + hosts: remoteOnly ? ["mac-home", "mac-other"] : ["mac-home"], + harnesses: ["pi"], + since: filtered ? "1d" : "all", + includeOmp: true, + }, + }); + const discovered = await discoverTranscripts({ ...options, now }); + await closeSshMasters(discovered.remoteMasters); + return discovered; + }); + assert.equal(result.transcripts.length, !filtered && !missingFirstRoot ? 4 : 3); + assert.equal(result.perHost.at(-1).duplicates, !filtered && missingFirstRoot ? 2 : 1); + const independent = result.transcripts.find((transcript) => transcript.path === independentPath); + const child = result.transcripts.find((transcript) => transcript.nativeId === "child-native"); + const grandchild = result.transcripts.find((transcript) => transcript.nativeId === "grandchild-native"); + assert.ok(independent && child && grandchild); + assert.equal(child.path, childPath); + assert.equal(child.host, remoteOnly ? "mac-home" : null); + assert.equal(grandchild.path, grandchildPath); + assert.equal(grandchild.host, remoteOnly ? "mac-other" : "mac-home"); + assert.notEqual(child.identity, grandchild.identity); + assert.equal(child.parentSessionId, missingFirstRoot ? undefined : "root-native"); + assert.equal(grandchild.parentSessionId, missingFirstRoot ? "root-native" : "child-native"); + assert.equal(child.interactionSignals.source, "subagent"); + assert.equal(child.interaction, NON_INTERACTIVE); + assert.equal(grandchild.interaction, NON_INTERACTIVE); + const familyIdentity = missingFirstRoot ? child.identity : child.corroborationIdentity; + assert.equal(grandchild.corroborationIdentity, familyIdentity); + assert.equal(independent.corroborationIdentity, independent.identity); + assert.notEqual(independent.identity, familyIdentity); + if (missingFirstRoot) { + assert.equal(child.corroborationIdentity, child.identity); + assert.equal(child.corroborationNativeId, "child-native"); + } else { + assert.equal(child.corroborationNativeId, "root-native"); + const family = result.transcripts.find((transcript) => transcript.path === firstRootPath); + if (filtered) assert.equal(family, undefined); + else assert.equal(family.identity, familyIdentity); + } + assertSingletonObserver([child, grandchild], familyIdentity); + const records = observerRecords([child, grandchild, independent]); + const folded = foldEvidence(records, { minGapEvidence: 2 }); + assert.equal(folded.gaps.length, 1); + assert.equal(folded.instructions[0].harmSessions, 2); + const ledger = { version: 1, entries: {} }; + recordGapObservations(ledger, records); + assertNativeGapContributions(ledger, [child, grandchild, independent]); + const persisted = foldEvidence(records, { + minGapEvidence: 2, + gapObservations: ledgerGapObservations(ledger, "AGENTS.md"), + }); + assert.equal(persisted.gaps.length, 1); + assert.equal(persisted.instructions[0].harmSessions, 2); + }); + } + } +} + +test("filtered OMP roots still share one cross-host gap and harm observer", async () => { + const now = Date.parse("2026-08-28T12:00:00.000Z"); + const old = Date.parse("2026-08-27T00:00:00.000Z"); + const recent = "2026-08-28T11:00:00.000Z"; + for (const includeLocal of [true, false]) { + const s = scenario(); + const otherHome = tmpdir("remote-other"); + const otherClone = initRepo(path.join(otherHome, "code", "demo"), `https://${REMOTE}.git`); + s.hosts["mac-other"] = { home: otherHome }; + const rootName = "2026-08-27T00-00-00.000Z_root"; + const stores = [ + [s.localHome, s.repoRoot], + [s.remoteHome, s.remoteClone], + [otherHome, otherClone], + ].map(([home, cwd]) => ({ + directory: path.join(home, ".omp", "agent", "sessions", "-repo-demo"), + cwd, + })); + for (const store of stores) { + const rootPath = path.join(store.directory, `${rootName}.jsonl`); + writeOmpSession(rootPath, "root-native", store.cwd); + fs.utimesSync(rootPath, old / 1000, old / 1000); + } + const writeRecent = (file, id, cwd) => { + writeOmpSession(file, id, cwd, recent); + fs.utimesSync(file, Date.parse(recent) / 1000, Date.parse(recent) / 1000); + }; + if (includeLocal) { + writeRecent(path.join(stores[0].directory, rootName, "Local.jsonl"), "local-child", s.repoRoot); + } + writeRecent(path.join(stores[1].directory, rootName, "Remote.jsonl"), "remote-child", s.remoteClone); + writeRecent(path.join(stores[2].directory, rootName, "Other.jsonl"), "other-child", otherClone); + writeRecent( + path.join(stores[2].directory, rootName, "Other", "Other.Grandchild.jsonl"), + "other-grandchild", + otherClone, + ); + const discover = () => + withRemotePiEnv(s, async () => { + const options = projectRun(s.repoRoot, { + discovery: { hosts: ["mac-home", "mac-other"], harnesses: ["pi"], since: "1d", includeOmp: true }, + }); + const result = await discoverTranscripts({ ...options, now }); + await closeSshMasters(result.remoteMasters); + return result; + }); + const result = await discover(); + assert.equal(result.cutoffMs, now - 24 * 60 * 60 * 1000); + assert.deepEqual( + result.transcripts.map((transcript) => transcript.nativeId).sort(), + [...(includeLocal ? ["local-child"] : []), "other-child", "other-grandchild", "remote-child"].sort(), + "old roots are excluded while every distinct recent descendant is retained", + ); + assert.ok(result.perHost.every((host) => host.duplicates === 0)); + assert.equal(new Set(result.transcripts.map((transcript) => transcript.identity)).size, result.transcripts.length); + assert.equal(new Set(result.transcripts.map((transcript) => transcript.corroborationIdentity)).size, 1); + for (const transcript of result.transcripts) { + assert.equal(transcript.parentSessionId, "root-native"); + assert.equal(transcript.corroborationNativeId, "root-native"); + assert.equal(transcript.corroborationStartedAt, old); + assert.equal(transcript.interaction, NON_INTERACTIVE); + } + const recordsFor = (transcripts) => + transcripts.map((transcript) => ({ + status: "ok", + memoryPath: "AGENTS.md", + transcript, + negative: [{ instruction: "db-rule", class: "harm", quote: "following the rule caused damage" }], + gaps: [ + { + proposedInstruction: "Read docs/db.md before writing queries.", + mistake: "re-derived the schema", + quote: "read the schema", + recurrenceRisk: "high", + domain: "project", + }, + ], + })); + const records = recordsFor(result.transcripts); + const folded = foldEvidence(records, { minGapEvidence: 2 }); + assert.equal(folded.gaps.length, 0); + assert.equal(folded.totals.droppedGapSingletons, 1); + assert.equal(folded.instructions[0].harmSessions, 1); + const ledger = { version: 1, entries: {} }; + recordGapObservations(ledger, records, { now: new Date(now) }); + assertNativeGapContributions(ledger, result.transcripts); + const persisted = foldEvidence(records, { + minGapEvidence: 2, + gapObservations: ledgerGapObservations(ledger, "AGENTS.md"), + }); + assert.equal(persisted.gaps.length, 0); + assert.equal(persisted.instructions[0].harmSessions, 1); + + if (includeLocal) { + const splitPath = path.join(s.localHome, ".omp", "agent", "sessions", "-repo-demo", "split-root.jsonl"); + writeRecent(splitPath, "root-native", s.repoRoot); + const split = await discover(); + const ordinary = split.transcripts.find((transcript) => transcript.path === splitPath); + const localChild = split.transcripts.find((transcript) => transcript.nativeId === "local-child"); + assert.equal(ordinary.interaction, INTERACTIVE); + assert.equal(ordinary.parentSessionId, undefined); + assert.equal(ordinary.corroborationIdentity, ordinary.identity); + assert.notEqual(ordinary.corroborationIdentity, localChild.corroborationIdentity); + assert.equal(new Set(split.transcripts.map((transcript) => transcript.corroborationIdentity)).size, 2); + const splitFolded = foldEvidence(recordsFor(split.transcripts), { minGapEvidence: 2 }); + assert.equal(splitFolded.gaps.length, 1); + assert.equal(splitFolded.instructions[0].harmSessions, 2); + } + } +}); + +test("remote nested OMP automation is non-interactive without readable ancestor provenance", async () => { + const s = scenario(); + const sessionRoot = path.join(s.remoteHome, ".omp", "agent", "sessions", "-repo-demo"); + writeOmpSession(path.join(sessionRoot, "root", "Child.jsonl"), "child-native", s.remoteClone); + writeOmpSession( + path.join(sessionRoot, "other-root", "Missing", "Missing.Grandchild.jsonl"), + "grandchild-native", + s.remoteClone, + ); + fs.writeFileSync(path.join(sessionRoot, "root.jsonl"), "not a session header\n"); + const result = await withRemotePiEnv(s, () => + discoverProject(s.repoRoot, { + discovery: { hosts: ["mac-home"], harnesses: ["pi"], since: "all", includeOmp: true }, + }), + ); + assert.equal(result.transcripts.length, 2); + for (const transcript of result.transcripts) { + assert.equal(transcript.host, "mac-home"); + assert.equal(transcript.interactionSignals.source, "subagent"); + assert.equal(transcript.interaction, NON_INTERACTIVE); + assert.equal(transcript.parentSessionId, undefined); + assert.equal(transcript.corroborationIdentity, transcript.identity); + assert.equal(transcript.corroborationNativeId, transcript.nativeId); + assert.equal(transcript.corroborationStartedAt, transcript.startedAt); + } + writeOmpSession(path.join(sessionRoot, "root.jsonl"), "root-native", s.remoteClone); + writeOmpSession(path.join(sessionRoot, "other-root.jsonl"), "other-root-native", s.remoteClone); + const refreshed = await withRemotePiEnv(s, () => + discoverProject(s.repoRoot, { + discovery: { hosts: ["mac-home"], harnesses: ["pi"], since: "all", includeOmp: true }, + }), + ); + for (const [childId, rootId] of [ + ["child-native", "root-native"], + ["grandchild-native", "other-root-native"], + ]) { + const child = refreshed.transcripts.find((transcript) => transcript.nativeId === childId); + const root = refreshed.transcripts.find((transcript) => transcript.nativeId === rootId); + assert.equal(child.parentSessionId, rootId); + assert.equal(child.corroborationIdentity, root.identity); + assert.equal(child.interaction, NON_INTERACTIVE); + assert.equal(root.interaction, INTERACTIVE); + } +}); + test("host collection has plain progress without duplicating live progress", async () => { const plain = scenario(); const lines = []; diff --git a/test/repo.test.js b/test/repo.test.js index fe2f46af..eadb30d6 100644 --- a/test/repo.test.js +++ b/test/repo.test.js @@ -5,8 +5,8 @@ import os from "node:os"; import path from "node:path"; import { execFileSync } from "node:child_process"; -import { associate } from "../src/discovery/association.js"; -import { attachSiblingClones, ensureLocalExclude, resolveRepo } from "../src/repo.js"; +import { associate, passesStrict } from "../src/discovery/association.js"; +import { attachSiblingClones, ensureLocalExclude, listWorktrees, resolveRepo } from "../src/repo.js"; function git(args, cwd) { execFileSync("git", args, { cwd, stdio: "ignore" }); @@ -26,6 +26,51 @@ function initRepo() { return dir; } +test("separate git directories retain tier-1 association with the checkout", (t) => { + const parent = tempDir("backpass-separate-git-"); + t.after(() => fs.rmSync(parent, { recursive: true, force: true })); + const root = path.join(parent, "checkout with spaces"); + const nested = path.join(root, "nested"); + const unrelated = path.join(parent, "unrelated"); + fs.mkdirSync(nested, { recursive: true }); + fs.mkdirSync(unrelated); + git(["init", "-q", "--separate-git-dir", path.join(parent, "git metadata")], root); + + assert.ok(fs.statSync(path.join(root, ".git")).isFile()); + assert.deepEqual(listWorktrees(root), [fs.realpathSync(root)]); + const repo = resolveRepo(root); + for (const [cwd, confidence] of [ + [root, "exact"], + [nested, "nested"], + ]) { + const association = associate({ cwd }, repo); + assert.equal(association?.tier, 1); + assert.equal(association?.confidence, confidence); + assert.equal(passesStrict(association, true), true); + } + assert.equal(associate({ cwd: unrelated }, repo), null); +}); + +test("listWorktrees retains ordinary and linked checkout paths without duplicates", (t) => { + const root = initRepo(); + t.after(() => fs.rmSync(root, { recursive: true, force: true })); + assert.deepEqual(listWorktrees(root), [fs.realpathSync(root)]); + const parent = tempDir("backpass-linked-"); + t.after(() => fs.rmSync(parent, { recursive: true, force: true })); + const linked = path.join(parent, "linked"); + git(["worktree", "add", "-q", "-b", "linked", linked], root); + + const worktrees = listWorktrees(root); + assert.equal(worktrees.length, 2); + assert.deepEqual(new Set(worktrees), new Set([fs.realpathSync(root), fs.realpathSync(linked)])); +}); + +test("listWorktrees falls back to the normalized root outside a Git repository", (t) => { + const root = tempDir("backpass-no-git-"); + t.after(() => fs.rmSync(root, { recursive: true, force: true })); + assert.deepEqual(listWorktrees(root), [fs.realpathSync(root)]); +}); + test("writes the line to .git/info/exclude, not a tracked .gitignore", () => { const dir = initRepo();