Skip to content

Comments

Use metadata flag for OpenCode compaction prompts#60

Merged
riatzukiza merged 4 commits intodevfrom
feat/compaction-heuristics-22
Nov 20, 2025
Merged

Use metadata flag for OpenCode compaction prompts#60
riatzukiza merged 4 commits intodevfrom
feat/compaction-heuristics-22

Conversation

@riatzukiza
Copy link
Collaborator

Summary

  • add explicit metadata tagging for compaction prompts and keep metadata available through filtering
  • prefer metadata flags when stripping OpenCode compaction instructions, falling back to regex heuristics; remove metadata afterward
  • expand tests and spec to cover metadata-aware compaction filtering

Testing

  • npm test -- request-transformer.test.ts

Closes #22

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 20, 2025

Warning

Rate limit exceeded

@riatzukiza has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 12 minutes and 51 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 6f77333 and 2f88ad0.

📒 Files selected for processing (1)
  • test/session-manager.test.ts (3 hunks)

Summary by CodeRabbit

  • New Features

    • Parent conversation references now supported for improved conversation branching and better context preservation across forked conversations.
  • Improvements

    • Enhanced metadata handling throughout request processing to better preserve context during optimization operations.
    • Refined filtering logic for more accurate and effective prompt management.

✏️ Tip: You can customize this high-level summary in your review settings.

Walkthrough

This pull request implements explicit metadata tagging for OpenCode compaction prompts and threads metadata preservation throughout the input filtering and transformation pipeline. The changes add compaction metadata to prompt items, extend filtering options to preserve metadata, and update consumers to handle the new metadata signals.

Changes

Cohort / File(s) Summary
Compaction metadata tagging
lib/compaction/codex-compaction.ts, lib/request/compaction-helpers.ts
Attaches compaction metadata (source: "opencode-compaction", opencodeCompaction: true) to developer and user InputItem objects; passes preserveMetadata: true to filterInput during compaction processing.
Input filtering and metadata handling
lib/request/input-filters.ts
Adds preserveMetadata option to filterInput; introduces hasCompactionMetadataFlag helper to detect compaction signals via item metadata (checking source, opencodeCompaction, opencode_compaction keys); updates pruning logic to sanitize compaction-related prompts when either the existing condition or metadata flag is present.
Request transformation pipeline
lib/request/request-transformer.ts
Introduces workingInput staging variable to prevent direct mutations; calls filterInput with preserveMetadata: true; updates filterOpenCodeSystemPrompts and conditional filtering to operate on staged input; plumbs metadata preservation through codex and non-codex branches.
Prompt cache constants
lib/request/prompt-cache.ts
Exports previously internal constants PROMPT_CACHE_METADATA_KEYS and PROMPT_CACHE_FORK_KEYS to enable external consumption.
Session fork detection
lib/session/session-manager.ts
Imports PROMPT_CACHE_FORK_KEYS and replaces hardcoded fork key list with iteration over the constant in extractForkIdentifier.
Test coverage
test/request-transformer.test.ts, test/session-manager.test.ts
Adds tests for metadata preservation in filterInput, compaction metadata detection in filterOpenCodeSystemPrompts, metadata stripping in transformRequestBody; adds tests for parent conversation fork derivation via parentConversationId and parent_conversation_id body options.

Sequence Diagram

sequenceDiagram
    participant Consumer as Consumer
    participant Compaction as Compaction
    participant Transformer as Request Transformer
    participant Filters as Input Filters
    
    Consumer->>Compaction: buildCompactionPromptItems()
    activate Compaction
    Compaction->>Compaction: Create InputItems with<br/>metadata tags
    Note over Compaction: source: "opencode-compaction"<br/>opencodeCompaction: true
    Compaction-->>Consumer: Return tagged items
    deactivate Compaction
    
    Consumer->>Transformer: transformRequestBody()
    activate Transformer
    Transformer->>Filters: filterInput(input,<br/>preserveMetadata: true)
    activate Filters
    Note over Filters: Preserves metadata during<br/>initial filtering
    Filters-->>Transformer: Filtered input with metadata
    deactivate Filters
    
    Transformer->>Filters: filterOpenCodeSystemPrompts()
    activate Filters
    Note over Filters: Detects compaction via<br/>hasCompactionMetadataFlag()
    rect rgb(200, 220, 255)
        Note over Filters: New: Check metadata flag first
        Filters->>Filters: Item has compaction metadata?
    end
    Filters-->>Transformer: Sanitized output
    deactivate Filters
    
    Transformer->>Transformer: Conditional re-filtering<br/>with preserveIds check
    Transformer-->>Consumer: Transformed body
    deactivate Transformer
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Areas requiring extra attention:

  • lib/request/input-filters.ts: New hasCompactionMetadataFlag helper function and updated pruning logic introduce new detection heuristics; verify that the metadata key checks (source, opencodeCompaction, opencode_compaction) are exhaustive and correctly prioritize metadata over regex fallback.
  • lib/request/request-transformer.ts: Staged input (workingInput) refactoring changes mutation patterns; trace through both codex and non-codex branches to ensure body.input assignments are correct and no state is lost.
  • Export visibility of prompt-cache constants: Verify that exporting PROMPT_CACHE_METADATA_KEYS and PROMPT_CACHE_FORK_KEYS does not create unintended external dependencies or API fragility.
  • Test coverage in session-manager.test.ts: Duplicated test block noted; check if this is intentional or a merge artifact that should be consolidated.

Possibly related issues

  • Compaction heuristics: prefer explicit metadata flag for OpenCode prompts #22 — Compaction heuristics: prefer explicit metadata flag for OpenCode prompts: This PR directly implements the follow-up suggestion from the #20 review, adding explicit metadata tagging to OpenCode compaction prompts and hardening detection via the new hasCompactionMetadataFlag helper with fallback to regex heuristics.

Possibly related PRs

  • Device/stealth #20: Implements the Codex compaction flow and modifies the same compaction and filtering files; this PR extends that work by adding metadata signaling and preservation mechanisms.
  • Release #29: Touches metadata-preservation and filterInput changes in the same compaction input handling and request-transformer/session-manager wiring.
  • address code rabbit comments #58: Also modifies lib/request/compaction-helpers.ts and related compaction behavior, so changes may interact at this file level.

Poem

🏷️ Metadata whispers softly now,
Tagged and treasured through the flow,
From compaction's touch to filter's test,
Each prompt wears its badge with zest.
Heuristics fade when signs align—
Explicit beats the guesswork line. ✨

Pre-merge checks and finishing touches

❌ Failed checks (2 warnings)
Check name Status Explanation Resolution
Out of Scope Changes check ⚠️ Warning One notable change appears out of scope: exporting PROMPT_CACHE_METADATA_KEYS and PROMPT_CACHE_FORK_KEYS in prompt-cache.ts, and related updates to session-manager.ts. These changes address fork key handling but are not directly required by issue #22. Clarify whether fork key constant exports are required by issue #22 or if they should be moved to a separate PR for better change isolation and review clarity.
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: using metadata flags for OpenCode compaction prompts instead of relying solely on regex heuristics.
Description check ✅ Passed The description clearly relates to the changeset, explaining the addition of metadata tagging for compaction prompts and metadata-aware filtering logic.
Linked Issues check ✅ Passed All core requirements from issue #22 are met: metadata markers added to compaction prompts, filterOpenCodeSystemPrompts updated to prefer metadata flags with regex fallback, and tests added for metadata-aware filtering.

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

riatzukiza and others added 2 commits November 20, 2025 17:14
…ariable'

Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
@riatzukiza riatzukiza enabled auto-merge November 20, 2025 23:14
auto-merge was automatically disabled November 20, 2025 23:18

Pull Request is not mergeable

@riatzukiza riatzukiza enabled auto-merge November 20, 2025 23:19
@riatzukiza riatzukiza merged commit 1dbfe18 into dev Nov 20, 2025
14 of 15 checks passed
@riatzukiza riatzukiza deleted the feat/compaction-heuristics-22 branch November 20, 2025 23:20
github-actions bot added a commit that referenced this pull request Nov 20, 2025
@riatzukiza riatzukiza mentioned this pull request Nov 20, 2025
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.

Compaction heuristics: prefer explicit metadata flag for OpenCode prompts

1 participant