Skip to content

feat: add bounded compaction semantic index - #476

Merged
danny-avila merged 14 commits into
mainfrom
danny-avila/compaction-semantic-index
Aug 29, 2026
Merged

feat: add bounded compaction semantic index#476
danny-avila merged 14 commits into
mainfrom
danny-avila/compaction-semantic-index

Conversation

@danny-avila

@danny-avila danny-avila commented Aug 27, 2026

Copy link
Copy Markdown
Owner

Summary

Adds the Agents SDK Compaction Semantic Index in guidance-only mode and derives it during formatAgentMessages' existing persisted-content analysis.

The formatter can return bounded, source-addressed semantic guidance beside provider messages, summary, and token metadata. Hosts forward that result into AgentInputs; no separate payload extraction pass is needed. Raw conversation messages remain complete and authoritative, and generated labels never replace evidence.

One-pass Model Context Reconstruction

  • Derives settled tool outcomes, visible reasoning labels, and activity-phase labels inside formatter branches that already own their exact source coordinates.
  • Reuses the formatter's parsed tool input; string-backed arguments are parsed once, not once per projection.
  • Admits a tool intent only when the host supplies that tool's name in compactionSemanticIndex.intentToolNames; arbitrary business intent arguments remain ordinary tool input.
  • Respects tool filtering and positional summary slicing; missing stable source identity fails closed.
  • Bounds pending and oversized derived text before the index leaves the formatter.
  • The option is disabled by default and allocates no semantic-index array when absent.
  • Provider messages are structurally identical with derivation enabled.

Request and cache shape

  • The appendix is rendered only after splitAtRecencyBoundary selects messagesToRefine.
  • It is inserted inside the existing one-off final HumanMessage before the checkpoint instruction.
  • Cached raw history and provider cache markers are identical with and without the index.
  • Primary and fallback summarizers receive the same appendix.
  • An absent index leaves the compaction prompt unchanged.

Trust, lifecycle, and privacy boundaries

  • Requires explicit, valid typed provenance for the exact persisted content part; absent, invalid, or partially omitted provenance fails closed.
  • Source/content/tool/reasoning identities are used only for validation, dedupe, and ordering; they are not sent to the model.
  • Highest revision wins; conflicting equal revisions fail closed.
  • Pending, redacted, or oversized newest revisions remain bounded textless tombstones that suppress stale committed guidance.
  • Caller data is copied and frozen before graph awaits. _sourceInputs retains only the bounded snapshot, never a rejected caller-owned index.
  • Input scan (256 entries), output count (64), per-entry (512 chars), source-content index, identity length, input text, and total appendix (4096 chars) are bounded.
  • Host text is normalized and XML-escaped, with an explicit data-not-instruction boundary.
  • Langfuse metadata records scalar included/omitted/character counts. Export shaping redacts only the generated appendix while preserving identical literals in raw history, custom prompts, and previous summaries.
  • Self-spawned children do not inherit the parent's run-scoped index. Explicit child configs may provide their own.

Timing scope

This interface captures semantic state present during Model Context Reconstruction, before AgentContext construction. Same-run label ingestion would require a separate late-bound resolver or append seam with explicit timing and failure semantics.

Not included:

  • LibreChat lifecycle, enablement, forwarding, or rollout changes
  • hidden reasoning or raw chain-of-thought
  • semantic range selection or tool-result replacement
  • treating generated labels as authoritative
  • compaction pins

Performance

npm run bench:compaction-semantic-index measures rendering and formatter projection with rotating samples.

Standalone median run, 9 × 300 formatter projections over 32 tool-rich persisted messages producing 192 entries:

  • current pre-strip path without derivation: ~258.2 µs/projection
  • equivalent bounded separate extraction plus pre-strip: ~261.4 µs/projection
  • one-pass formatter derivation: ~264.4 µs/projection

The enabled one-pass path is within ~1.2% (about 3 µs at 192 entries) of the equivalent separate projection while removing its extra payload pass and duplicate string-argument parse. The normal disabled formatter remains allocation-free for the semantic index and measured at parity with the current pre-strip path. Appendix rendering and source validation occur only when compaction fires (~23 µs for 48 exact entries in the same run).

Coverage-balanced bounds

  • Producer admission stays on the original array-push path through 256 entries.
  • On overflow, fixed-memory retention keeps the early prefix, recent suffix, and bounded first/latest representatives of every semantic type.
  • Renderer budgeting prioritizes temporal endpoints, latest/earliest type representatives, and recursively bisected history, then restores exact provenance order.
  • Producer-side omissions remain observable without retaining discarded entries.
  • Regression coverage proves initial goals, rare middle activity/reasoning, and latest outcomes survive both caps.

Performance follow-up:

  • 192-entry common formatter projection: ~274.7 µs after versus ~277.6 µs before in the recorded median runs.
  • 48-entry appendix render: ~23.2 µs after versus ~22.9 µs before.
  • 384-entry overflow projection: ~748.1 µs after versus ~715.1 µs before (~4.6%). This cost occurs only after the 256-entry cap overflows and preserves revision suppression with bounded, collision-safe identity tracking.
  • Disabled behavior remains unchanged.

Verification

  • npx tsc --noEmit
  • focused ESLint with zero warnings
  • 491 focused formatter/AgentContext/compaction tests passing, one pre-existing skip
  • npm run check:circular-deps
  • npm run build
  • npm run bench:compaction-semantic-index
  • git diff --check

New derivation coverage includes provider-message parity, single-parse stringified tool intents, host intent classification, exact source coordinates, pending lifecycle, missing identity, summary-slice exclusion, fixed entry bounds, and bounded oversized tombstones.

Architecture

ADR 0007 and CONTEXT.md now place extraction inside Model Context Reconstruction, where exact provenance already exists. LibreChat retains lifecycle, enablement, tool-intent classification, forwarding, and rollout ownership. The separate host extraction module is intentionally rejected as a shallow seam that duplicates traversal and coordinate logic.

@danny-avila

Copy link
Copy Markdown
Owner Author

@codex review

Please review the current PR head 6e2fcef. Confirm that this exact commit is the reviewed commit and ignore findings that apply only to earlier heads.

@danny-avila

Copy link
Copy Markdown
Owner Author

@codex review

Please review the current PR head 1a2ca3b. Confirm that this exact commit is the reviewed commit and ignore findings that apply only to earlier heads.

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1a2ca3b020

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/summarization/semanticIndex.ts
Comment thread src/summarization/semanticIndex.ts Outdated
Comment thread src/langfuseTraceShaping.ts Outdated
@danny-avila

Copy link
Copy Markdown
Owner Author

@codex review

Please review the current PR head dc71ba5. Confirm that this exact commit is the reviewed commit and ignore findings that apply only to earlier heads.

@danny-avila

Copy link
Copy Markdown
Owner Author

@codex review

Please review the current PR head 1fd308b. Confirm that this exact commit is the reviewed commit and ignore findings that apply only to earlier heads.

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1fd308bd89

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/langfuseTraceShaping.ts Outdated
Comment thread src/summarization/semanticIndex.ts Outdated
Comment thread src/summarization/semanticIndex.ts Outdated
Comment thread src/agents/AgentContext.ts
Comment thread src/summarization/semanticIndex.ts Outdated
@danny-avila

Copy link
Copy Markdown
Owner Author

@codex review

Please review the current PR head e34de5c. Confirm that this exact commit is the reviewed commit and ignore findings that apply only to earlier heads.

After two actionable rounds I completed a whole-diff invariant audit covering responsibility boundaries, exact provenance, revision tombstones, bounded memory and telemetry, cache-prefix stability, trace locality, subagent scoping, and the disabled path.

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e34de5cb8b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/summarization/semanticIndex.ts Outdated
Comment thread src/summarization/semanticIndex.ts
@danny-avila

Copy link
Copy Markdown
Owner Author

@codex review

Please review the current PR head 1b565c0. Confirm that this exact commit is the reviewed commit and ignore findings that apply only to earlier heads.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: 1b565c00fb

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danny-avila

Copy link
Copy Markdown
Owner Author

@codex review

Please review the current PR head f1b038e. Confirm that this exact commit is the reviewed commit and ignore findings that apply only to earlier heads.

Architecture changed after the prior review: formatAgentMessages now derives the bounded semantic index during its existing persisted-content analysis, tool intents require a host-approved tool-name set, and the separate LibreChat extraction pass is no longer the intended design. Please examine disabled-path allocations, exact provenance after summary slicing, bounds, provider-message parity, and the updated ADR ownership seam.

@danny-avila

Copy link
Copy Markdown
Owner Author

@codex review

Please review the current PR head 373d326. Confirm that this exact commit is the reviewed commit and ignore findings that apply only to earlier heads.

The invariant closeout moved semantic extraction into the formatter branches that already parse tool inputs, eliminating duplicate JSON parsing; UI-only activity labels now short-circuit provider analysis, derived pending/oversized text is bounded before return, and the benchmark compares equivalent bounded separate versus one-pass projection. Please focus on provider parity, source-index mapping after tool filtering/summary slicing, and disabled-path cost.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🚀

Reviewed commit: 373d3262c1

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danny-avila

Copy link
Copy Markdown
Owner Author

@codex review

Please review the current PR head 6119a4b. Confirm that this exact commit is the reviewed commit and ignore findings that apply only to earlier heads.

This follow-up replaces oldest-only cap behavior with fixed-memory early/recent/type-balanced producer retention and compaction-only temporal/type-balanced character-budget selection. The common <=256-entry formatter path remains the original array-push path; balanced state initializes only after overflow. Please focus on bounded CPU/memory, revision and omission-count semantics, exact chronological output ordering, and disabled/common-path latency.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Aug 28, 2026

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review Completed 2026-08-29T00:40:38.389280Z ce1e5bb Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6119a4bd31

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/messages/format.ts
@danny-avila

Copy link
Copy Markdown
Owner Author

@codex review

Please review exact current PR head af4b0a7.

This follow-up addresses the prior suppression finding with bounded, collision-safe identity tracking across all semantic entry types. Please audit revision precedence, equal-revision conflict handling, bounded memory, retention/ring eviction, output ordering, and common-path performance. Ignore superseded heads.

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: af4b0a7361

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/messages/format.ts
Comment thread src/messages/format.ts
@danny-avila

Copy link
Copy Markdown
Owner Author

@codex review

Please review exact current PR head fdda1b8.

This follow-up renews advanced/conflicting revisions in the bounded global and per-type recent rings, and aligns producer identity hashing/equality with renderer whitespace normalization. Please audit ring cursor/order behavior, reference counts and eviction, normalized collision handling, revision suppression, bounded memory, and performance. Ignore superseded heads.

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: fdda1b865e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/messages/format.ts Outdated
Comment thread src/messages/format.ts Outdated
@danny-avila

Copy link
Copy Markdown
Owner Author

@codex review

Please review exact current PR head fe9c705.

This follow-up normalizes equal-revision text exactly like the renderer before conflict detection and rejects present malformed activity revisions while preserving the absent legacy default. Please audit overflow/non-overflow semantic parity, fail-closed validation, tombstone behavior, regression coverage, and performance. Ignore superseded heads.

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: fe9c705243

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/messages/format.ts Outdated
Comment thread src/summarization/semanticIndex.ts Outdated
@danny-avila

Copy link
Copy Markdown
Owner Author

@codex review

Please review exact current PR head ce1e5bb.

This follow-up converts malformed non-identity semantic state into bounded suppressing tombstones whenever identity and revision remain trustworthy, covering producer pending/status/text and snapshot/direct-render redaction/status/text paths. Please audit redaction safety, stale-revision suppression, malformed input behavior, legacy defaults, bounded data retention, and performance. Ignore superseded heads.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: ce1e5bb807

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danny-avila

Copy link
Copy Markdown
Owner Author

@codex review

Retrying because the prior exact-head request cleared its in-progress reaction without publishing a terminal review or clean reaction after 15 minutes. Please review exact current PR head ce1e5bb. Ignore all superseded heads.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: ce1e5bb807

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danny-avila
danny-avila merged commit 2f4e9db into main Aug 29, 2026
13 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