Skip to content

fix(pi): archive current Pi compaction shape and avoid duplicate save - #673

Open
noxos-os wants to merge 1 commit into
Gentleman-Programming:mainfrom
noxos-os:fix/compaction-recovery-event-shape
Open

fix(pi): archive current Pi compaction shape and avoid duplicate save#673
noxos-os wants to merge 1 commit into
Gentleman-Programming:mainfrom
noxos-os:fix/compaction-recovery-event-shape

Conversation

@noxos-os

@noxos-os noxos-os commented Jul 27, 2026

Copy link
Copy Markdown

Problem

gentle-engram's session_compact handler never auto-archived the compaction summary to Engram on current Pi, because extractCompactedSummary only inspected legacy top-level / nested paths and missed Pi's documented event shape:

// Pi session_compact event (docs: docs/compaction.md)
{ compactionEntry: CompactionEntry, fromExtension, reason, willRetry }
// CompactionEntry.summary holds the summary text

As a result, summary was undefined, the /observations POST was skipped, and the agent was always asked to save the summary manually via the FIRST ACTION REQUIRED: mem_session_summary notice on the next turn — even though that was meant to be a fallback.

Separately, buildRecoveryNotice always appended the manual-save instruction, so once extraction is fixed the agent would duplicate the save it just completed.

Fix

plugin/pi/compaction-recovery.js

  • Add ["compactionEntry", "summary"] as the first SUMMARY_FIELD_PATH so the documented event shape is extracted (legacy fallbacks are preserved).
  • buildRecoveryNotice(project, context, persisted = false): when persisted, emit a continuity-only acknowledgement (still surfaces retrieved Engram context) and omit the manual FIRST ACTION REQUIRED: mem_session_summary instruction. Default false keeps existing behavior for any caller.

plugin/pi/index.ts

  • In session_compact, treat the archive POST's confirmed response as persisted and pass it to buildRecoveryNotice. bestEffortEngramFetch returns null on a failed/unreachable write, so a failed archive still falls back to the manual save instruction — only a confirmed success suppresses it.

Behavior matrix

summary extracted archive succeeded notice on next turn
yes yes context + "already saved" (no manual save)
yes no (server down) context + manual FIRST ACTION REQUIRED (retry)
no n/a context + manual FIRST ACTION REQUIRED (unchanged)

Tests

  • extractCompactedSummary now covers { compactionEntry: { summary } } (native + extension-provided, plus whitespace-only ignored).
  • buildRecoveryNotice(..., true) asserts no FIRST ACTION REQUIRED while still surfacing context.

Full local run: node --test plugin/pi/test/*.test.mjs44 pass / 0 fail.

Backward compatible: buildRecoveryNotice third arg defaults to false, and the new extraction path is strictly additive.

Summary by CodeRabbit

  • Bug Fixes

    • Improved recovery after session compaction by correctly recognizing current summary formats.
    • Recovery notices now accurately indicate whether the summary was saved successfully.
    • Empty summaries are handled without generating misleading recovery instructions.
  • Tests

    • Added coverage for current compaction events, empty summaries, and saved-summary notifications.

Pi's session_compact event carries the summary at
event.compactionEntry.summary, but extractCompactedSummary only inspected
legacy top-level/nested paths, so the auto-archive to Engram never fired on
current Pi and the agent was always asked to save the summary manually.

- Add ['compactionEntry','summary'] as the first SUMMARY_FIELD_PATH so the
  documented event shape is extracted.
- Track whether the archive POST actually succeeded and pass a persisted flag
  to buildRecoveryNotice. When persisted, inject a continuity-only notice and
  drop the manual FIRST ACTION REQUIRED: mem_session_summary instruction so
  the next turn does not duplicate the save it just completed. A failed or
  unreachable write still falls back to the manual instruction.
- Cover both the new event shape and the persisted flag in tests.
@coderabbitai

coderabbitai Bot commented Jul 27, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Compaction recovery now reads the current session_compact summary shape, tracks whether the summary was persisted to Engram, and generates either an acknowledgement or manual recovery instructions. Tests cover both extraction and persisted notice behavior.

Changes

Compaction recovery flow

Layer / File(s) Summary
Summary extraction and notice modes
plugin/pi/compaction-recovery.js, plugin/pi/test/compaction-recovery.test.mjs
Prioritizes compactionEntry.summary, documents manual fallback behavior, and adds persisted acknowledgement messaging with coverage for both notice modes and summary extraction.
Persistence status wiring
plugin/pi/index.ts
Tracks successful observation persistence using the non-null Engram response and passes the result to buildRecoveryNotice.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant SessionCompactHandler
  participant EngramObservations
  participant buildRecoveryNotice
  SessionCompactHandler->>EngramObservations: Persist compaction summary
  EngramObservations-->>SessionCompactHandler: Return response or null
  SessionCompactHandler->>buildRecoveryNotice: Pass persistedSummary
  buildRecoveryNotice-->>SessionCompactHandler: Build acknowledgement or recovery notice
Loading

Suggested reviewers: gentleman-programming, alan-thegentleman

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly reflects the main change: supporting the current Pi compaction shape and avoiding duplicate save instructions.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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 unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

@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

🤖 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 `@plugin/pi/compaction-recovery.js`:
- Around line 70-93: Move persisted-versus-manual recovery policy out of
plugin/pi/compaction-recovery.js: remove persistedAcknowledgement and the
persisted branch in buildRecoveryNotice, and consume the status or notice
produced by the core layer instead. In plugin/pi/index.ts, forward the archive
result from the compaction flow without interpreting saved !== null or selecting
user-facing recovery behavior; both affected sites should remain thin adapters
that parse input, call the core API/tool, and return results.

In `@plugin/pi/test/compaction-recovery.test.mjs`:
- Around line 18-36: Update the extractCompactedSummary tests to include a
conflicting legacy summary alongside compactionEntry.summary, and assert that
the modern compactionEntry.summary value is returned. Keep the existing native,
extension, and empty-summary coverage unchanged.
🪄 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: Repository UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 178120ad-0224-411e-9a22-4d01e81c9cb1

📥 Commits

Reviewing files that changed from the base of the PR and between 763a6ba and 1e7d50e.

📒 Files selected for processing (3)
  • plugin/pi/compaction-recovery.js
  • plugin/pi/index.ts
  • plugin/pi/test/compaction-recovery.test.mjs

Comment on lines +70 to +93
/**
* Used when the compaction summary was already archived to Engram by
* gentle-engram. Still surfaces retrieved project context for continuity, but
* omits the manual `FIRST ACTION REQUIRED: mem_session_summary` instruction so
* the agent does not duplicate the save it just completed.
*/
function persistedAcknowledgement(project) {
return (
`Compaction recovery summary was already saved to Engram (${project}) by gentle-engram. ` +
`No manual mem_session_summary call is needed for this compaction. ` +
`Call mem_context if you need additional recent project memory.`
);
}

/**
* Build the recovery notice injected before the next agent turn.
*
* @param {string} project Engram project name.
* @param {string|undefined} context Retrieved Engram project context, if any.
* @param {boolean} [persisted=false] True when the compaction summary was
* already archived to Engram; suppresses the manual save instruction.
*/
export function buildRecoveryNotice(project, context, persisted = false) {
const instruction = persisted ? persistedAcknowledgement(project) : recoveryInstruction(project);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

📐 Maintainability & Code Quality | 🟠 Major | 🏗️ Heavy lift

Move compaction persistence policy into the core layer.

The PR adds the persisted/manual recovery decision across two plugin/** modules. Keep the plugin adapter limited to parsing input, invoking the core Go API/tool, and returning results.

  • plugin/pi/compaction-recovery.js#L70-L93: remove the plugin-owned persisted acknowledgement and recovery-mode branch; consume a core-produced status or notice.
  • plugin/pi/index.ts#L890-L911: forward the archive result instead of interpreting saved !== null and selecting the user-facing recovery behavior.

As per path instructions, adapters under plugin/** must stay thin: “parse input, call the core Go API/tool, return. No business logic, no external runtime deps.”

📍 Affects 2 files
  • plugin/pi/compaction-recovery.js#L70-L93 (this comment)
  • plugin/pi/index.ts#L890-L911
🤖 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 `@plugin/pi/compaction-recovery.js` around lines 70 - 93, Move
persisted-versus-manual recovery policy out of plugin/pi/compaction-recovery.js:
remove persistedAcknowledgement and the persisted branch in buildRecoveryNotice,
and consume the status or notice produced by the core layer instead. In
plugin/pi/index.ts, forward the archive result from the compaction flow without
interpreting saved !== null or selecting user-facing recovery behavior; both
affected sites should remain thin adapters that parse input, call the core
API/tool, and return results.

Source: Path instructions

Comment on lines +18 to +36
test("extractCompactedSummary reads the current Pi session_compact event shape", () => {
// Documented Pi event: { compactionEntry: CompactionEntry, ... } where
// CompactionEntry.summary holds the summary text.
assert.equal(
extractCompactedSummary({ compactionEntry: { summary: "native pi summary" } }),
"native pi summary",
);
assert.equal(
extractCompactedSummary({
compactionEntry: { summary: " extension summary " },
fromExtension: true,
reason: "threshold",
willRetry: false,
}),
"extension summary",
);
// Empty compactionEntry.summary is ignored (falls through / returns undefined).
assert.equal(extractCompactedSummary({ compactionEntry: { summary: " " } }), undefined);
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Test modern-field precedence explicitly.

These cases prove that compactionEntry.summary is readable, but not that it takes precedence over legacy fields. Add a conflicting legacy summary and assert that the modern value wins.

Suggested test
   assert.equal(
     extractCompactedSummary({ compactionEntry: { summary: "native pi summary" } }),
     "native pi summary",
   );
+  assert.equal(
+    extractCompactedSummary({
+      compactionEntry: { summary: "modern summary" },
+      payload: { summary: "legacy summary" },
+    }),
+    "modern summary",
+  );
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
test("extractCompactedSummary reads the current Pi session_compact event shape", () => {
// Documented Pi event: { compactionEntry: CompactionEntry, ... } where
// CompactionEntry.summary holds the summary text.
assert.equal(
extractCompactedSummary({ compactionEntry: { summary: "native pi summary" } }),
"native pi summary",
);
assert.equal(
extractCompactedSummary({
compactionEntry: { summary: " extension summary " },
fromExtension: true,
reason: "threshold",
willRetry: false,
}),
"extension summary",
);
// Empty compactionEntry.summary is ignored (falls through / returns undefined).
assert.equal(extractCompactedSummary({ compactionEntry: { summary: " " } }), undefined);
});
test("extractCompactedSummary reads the current Pi session_compact event shape", () => {
// Documented Pi event: { compactionEntry: CompactionEntry, ... } where
// CompactionEntry.summary holds the summary text.
assert.equal(
extractCompactedSummary({ compactionEntry: { summary: "native pi summary" } }),
"native pi summary",
);
assert.equal(
extractCompactedSummary({
compactionEntry: { summary: "modern summary" },
payload: { summary: "legacy summary" },
}),
"modern summary",
);
assert.equal(
extractCompactedSummary({
compactionEntry: { summary: " extension summary " },
fromExtension: true,
reason: "threshold",
willRetry: false,
}),
"extension summary",
);
// Empty compactionEntry.summary is ignored (falls through / returns undefined).
assert.equal(extractCompactedSummary({ compactionEntry: { summary: " " } }), undefined);
});
🤖 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 `@plugin/pi/test/compaction-recovery.test.mjs` around lines 18 - 36, Update the
extractCompactedSummary tests to include a conflicting legacy summary alongside
compactionEntry.summary, and assert that the modern compactionEntry.summary
value is returned. Keep the existing native, extension, and empty-summary
coverage unchanged.

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