Skip to content

fix: preserve retrieval receipts and Codex installs - #568

Merged
mohanagy merged 2 commits into
mainfrom
agent/activation-followup-fixes
Jul 18, 2026
Merged

mohanagy merged 2 commits into
mainfrom
agent/activation-followup-fixes

Conversation

@mohanagy

@mohanagy mohanagy commented Jul 18, 2026 •

Copy link
Copy Markdown
Owner

Summary

  • Reconcile retrieval-plan obligation receipts after recovery, output shaping, compaction, context resolution, and delta filtering.
  • Harden managed Claude/Codex installation lifecycle checks so doctor detects divergent hooks, custom hook scripts are preserved, and empty configuration is cleaned safely.
  • Add regression coverage for normal, verbose, retrieve, delta, recovery, doctor, install, coexistence, and uninstall paths.

Why

A retrieval receipt could describe evidence that had been removed from the final visible result. Installer lifecycle behavior could also report a Claude hook as healthy when its managed script had diverged, or risk replacing a user-owned hook.

Impact

Agents receive evidence receipts that match the snippets they can actually inspect. Install, doctor, and uninstall operations remain workspace-scoped while protecting user-owned hook scripts.

Validation

  • npm run typecheck
  • npx vitest run --maxWorkers=1 --no-file-parallelism tests/unit/doctor.test.ts tests/unit/install.test.ts tests/unit/context-pack-recovery.test.ts tests/unit/retrieve-conceptual-fallback.test.ts tests/unit/retrieve-cross-layer-flow.test.ts tests/unit/context-pack-command.test.ts tests/unit/stdio-slice-surface.test.ts (187 passed)
  • git diff --check

Follow-up

Refs #565
Refs #567

This PR is ready for review. Neither issue is being closed, and no release claim is being made until current-head CI, CodeRabbit, a refreshed exact-candidate receipt, and successful Codex client trials are available.

Summary by CodeRabbit

  • Improvements
    • Retrieval plans now consistently reconcile query obligations with the actual evidence/snippets included across full, compact, fallback, recovery, and streaming context responses.
    • Conceptual fallback and recovery now use snippet-grounded evidence coverage to guide decision-making and final obligation coverage.
  • Bug Fixes
    • Improved detection/validation of Claude prompt-hook configuration, including compatibility with legacy setups.
    • Install now prevents overwriting user-managed Claude hook scripts; uninstall/cleanup now reliably removes only the managed Claude/Gemini/Codex hook entries while pruning empty sections (and cleaning up MCP server entries).

@coderabbitai

coderabbitai Bot commented Jul 18, 2026 •

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: c3a48d63-cb33-467d-bcbe-2f992012d832

📥 Commits

Reviewing files that changed from the base of the PR and between a876ce8 and dc68d68.

📒 Files selected for processing (6)
  • src/infrastructure/context-pack-command.ts
  • src/infrastructure/install.ts
  • src/runtime/retrieve/conceptual-fallback.ts
  • tests/unit/context-pack-command.test.ts
  • tests/unit/install.test.ts
  • tests/unit/retrieve-conceptual-fallback.test.ts
🚧 Files skipped from review as they are similar to previous changes (5)
  • src/infrastructure/context-pack-command.ts
  • tests/unit/retrieve-conceptual-fallback.test.ts
  • src/runtime/retrieve/conceptual-fallback.ts
  • tests/unit/context-pack-command.test.ts
  • src/infrastructure/install.ts

📝 Walkthrough

Walkthrough

Retrieval outputs now reconcile query-obligation coverage with returned snippets across recovery, compaction, serialization, and stdio flows. Claude hook management detects current and legacy managed hooks, protects user scripts, and removes empty configuration structures.

Changes

Retrieval evidence reconciliation

Layer / File(s) Summary
Snippet-grounded coverage contract
src/runtime/retrieve/conceptual-fallback.ts
Conceptual fallback plans and recovery decisions use snippet-grounded query coverage, with a helper that updates retrieval-plan obligations.
Runtime recovery and shaping reconciliation
src/runtime/context-pack-recovery.ts, src/runtime/retrieve.ts
Recovery, snippet budgeting, compaction, and stdio shaping reconcile retrieval plans against their resulting matched nodes.
Serialized and stdio payload reconciliation
src/infrastructure/context-pack-command.ts, src/runtime/stdio/tools.ts
Serialized, delta, and non-delta context-pack payloads update query-obligation totals and final coverage from emitted nodes.
Retrieval reconciliation tests
tests/unit/context-pack-command.test.ts, tests/unit/context-pack-recovery.test.ts, tests/unit/retrieve-conceptual-fallback.test.ts, tests/unit/retrieve-cross-layer-flow.test.ts, tests/unit/stdio-slice-surface.test.ts
Tests validate coverage reconciliation across fallback, recovery, compact, serialized, cross-layer, and stdio representations.

Claude hook management

Layer / File(s) Summary
Managed Claude hook identity and detection
src/infrastructure/install.ts, src/infrastructure/doctor.ts
Claude hook commands, managed script checks, current UserPromptSubmit detection, and legacy hook detection are centralized and used by doctor reporting.
Hook installation and configuration cleanup
src/infrastructure/install.ts, tests/unit/install.test.ts
Installation protects user-managed scripts, uninstall removes empty hook and MCP containers, and tests cover Claude, Gemini, and Codex cleanup behavior.
Claude doctor detection tests
tests/unit/doctor.test.ts
Doctor tests cover current and legacy managed hooks, partial configurations, incorrect commands, and unrelated PreToolUse entries.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant RetrievalRuntime
  participant Compaction
  participant StdioTools
  participant ContextPackSerializer
  participant QueryCoverage
  RetrievalRuntime->>Compaction: shape matched_nodes
  Compaction->>QueryCoverage: evaluate snippet coverage
  QueryCoverage-->>Compaction: query_obligations
  Compaction->>StdioTools: build compact payload
  StdioTools->>QueryCoverage: reconcile payload nodes
  QueryCoverage-->>StdioTools: updated retrieval_plan
  StdioTools->>ContextPackSerializer: serialize context pack
  ContextPackSerializer->>QueryCoverage: reconcile emitted snippets
  QueryCoverage-->>ContextPackSerializer: final query_obligations
Loading

Possibly related PRs

  • mohanagy/madar#345: Introduces the snippet-budget shaping used by the retrieval-plan reconciliation flow.
  • mohanagy/madar#493: Introduces the generated Claude prompt-hook script identity used by these hook lifecycle changes.
🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning It covers summary, why, impact, and validation, but it does not follow the template’s Testing, Checklist, or Related issues sections. Add the missing Testing, Checklist, and Related issues sections using the template headings, and keep the validation commands under Testing.
Docstring Coverage ⚠️ Warning Docstring coverage is 18.18% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately reflects the two main changes: retrieval receipt reconciliation and safer Codex install behavior.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch agent/activation-followup-fixes

Comment @coderabbitai help to get the list of available commands.

@mohanagy
mohanagy marked this pull request as ready for review July 18, 2026 09:46

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/infrastructure/context-pack-command.ts (1)

1890-1893: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Preserve the reconciliation result when no new obligation was lost.

The retrieval plan may already have been mutated at Line 1886. Returning false here skips the caller’s post-reconciliation budget enforcement despite that mutation.

Proposed fix
   const lostDuringSerialization = queryCoverage.missing_obligations.some((obligation) => !baselineMissing.has(obligation))
   if (!lostDuringSerialization) {
-    return false
+    return retrievalPlanReconciled
   }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/infrastructure/context-pack-command.ts` around lines 1890 - 1893, Update
the reconciliation logic around queryCoverage and lostDuringSerialization so it
preserves the caller’s post-reconciliation budget-enforcement path when the
retrieval plan was already mutated, rather than unconditionally returning false.
Retain the no-new-obligation check, but return or propagate the existing
reconciliation result that reflects the mutation performed near line 1886.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/infrastructure/install.ts`:
- Around line 793-803: Update hasManagedClaudePromptHookScript to verify
scriptPath is a regular file with lstatSync before reading it, returning false
for directories and other non-file paths. Wrap the filesystem checks and
readFileSync call in error handling so broken symlinks, inaccessible paths, and
other filesystem failures are treated as unmanaged rather than thrown.

In `@src/runtime/retrieve/conceptual-fallback.ts`:
- Around line 623-637: Update the reconciliation logic around
evaluateQueryEvidenceCoverage to clamp plan.query_obligations.initially_covered
to the reconciled coverage.total, preserve that clamped value in the returned
query_obligations, and include it in the early equality check alongside total
and finally_covered.

---

Outside diff comments:
In `@src/infrastructure/context-pack-command.ts`:
- Around line 1890-1893: Update the reconciliation logic around queryCoverage
and lostDuringSerialization so it preserves the caller’s post-reconciliation
budget-enforcement path when the retrieval plan was already mutated, rather than
unconditionally returning false. Retain the no-new-obligation check, but return
or propagate the existing reconciliation result that reflects the mutation
performed near line 1886.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 72d2806d-1612-46e8-b90e-85ca222ba72b

📥 Commits

Reviewing files that changed from the base of the PR and between e8f7d1d and a876ce8.

📒 Files selected for processing (14)
  • src/infrastructure/context-pack-command.ts
  • src/infrastructure/doctor.ts
  • src/infrastructure/install.ts
  • src/runtime/context-pack-recovery.ts
  • src/runtime/retrieve.ts
  • src/runtime/retrieve/conceptual-fallback.ts
  • src/runtime/stdio/tools.ts
  • tests/unit/context-pack-command.test.ts
  • tests/unit/context-pack-recovery.test.ts
  • tests/unit/doctor.test.ts
  • tests/unit/install.test.ts
  • tests/unit/retrieve-conceptual-fallback.test.ts
  • tests/unit/retrieve-cross-layer-flow.test.ts
  • tests/unit/stdio-slice-surface.test.ts

Comment thread src/infrastructure/install.ts
Comment thread src/runtime/retrieve/conceptual-fallback.ts
@mohanagy
mohanagy merged commit 3fe8b5f into main Jul 18, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant