feat: context optimization pipeline with capability routing#3466
Merged
feat: context optimization pipeline with capability routing#3466
Conversation
…pi-layer research - Spec: 6-change design for GSD extension context optimization - Plan: 9-task TDD implementation plan with exact file paths and code - Pi-layer doc: 10 infrastructure opportunities (research only, not planned) Part of #3171, #3406, #3452, #3433. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Introduces PhaseAnchor read/write utilities so downstream agents can inherit decisions, blockers, and intent written at phase boundaries without re-inferring from conversation history. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…ent preferences Implement ADR-004 Phase 2 capability scoring with 7-dimension model profiles, task requirement vectors, and weighted scoring. Add ContextManagementConfig preferences for observation masking thresholds. Wire capability scoring into auto-model-selection dispatch path. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…cation Register observation masker in before_provider_request hook to replace old tool results with placeholders during auto-mode. Add tool result truncation (configurable via context_management.tool_result_max_chars). Inject phase handoff anchors into prompt builders so downstream phases inherit decisions from research/planning. Write anchors after successful phase completion. Update ADR-004 status to Implemented. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Contributor
🔴 PR Risk Report — CRITICAL
Affected Systems
File Breakdown
|
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…ment Update dynamic-model-routing.md with capability-aware scoring section. Update token-optimization.md with observation masking, tool truncation, and phase handoff anchor documentation. Update configuration.md with context_management preference block and capability_routing flag. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Collaborator
|
Follow-up audit (validated against PR branch + current runtime contracts): I reviewed the diff and validated each point against source and docs. I found 5 meaningful issues:
|
trek-e
pushed a commit
that referenced
this pull request
Apr 3, 2026
- Fix slice anchor collisions: key anchors by (phase, sliceId) so research-slice/plan-slice anchors from different slices no longer overwrite each other within the same milestone. - Fix payload shape mismatch: context-masker and tool result truncation now handle both internal message format (type field) and provider API formats (role=tool, content arrays with tool_result blocks). - Plumb context_management into KNOWN_PREFERENCE_KEYS and mergePreferences() so the config is properly recognized and merged. - Remove dead compaction_threshold_percent config that was validated and documented but never read at runtime. - Populate structured handoff data in phase anchors by extracting decisions, blockers, and next steps from the artifact files produced by each phase. https://claude.ai/code/session_012ysgpj3kKCNcZdEL7W5eRe
… state corruption - Add missing 'context_management' to KNOWN_PREFERENCE_KEYS set so users don't get spurious unknown-key warnings when configuring it. - Replace in-place mutation of tool result content with immutable spread to prevent corrupting shared conversation message objects. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The Classification type gained stop and backtrack variants from main but triage-ui.ts was not updated, causing a TypeScript build failure. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…age format
The observation masker and tool result truncation in before_provider_request
were checking m.type === "toolResult" but the actual pi-ai payload uses
m.role === "toolResult" with content as TextContent[] arrays (not strings).
bashExecution messages are converted to {role:"user"} by convertToLlm before
the hook fires, so checking m.type === "bashExecution" was a no-op.
- Fix context-masker to match on role, handle array content, detect bash
results by their "Ran `" prefix
- Fix register-hooks truncation to operate on role:"toolResult" with
array content blocks
- Update tests to use correct pi-ai LLM payload format
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implements a comprehensive context optimization pipeline for GSD auto-mode, targeting both token cost reduction and context drift prevention:
context_management.observation_maskingandobservation_mask_turnspreferences.context_management.tool_result_max_chars, default 800) during auto-mode sessions.Changes by area
model-router.ts,auto-model-selection.ts,complexity-classifier.tscontext-masker.ts,register-hooks.tsbefore_provider_requestphase-anchor.ts,auto-prompts.ts,auto/phases.ts,prompts/execute-task.mdpreferences-types.ts,preferences-validation.ts,docs/preferences-reference.mdADR-004-capability-aware-model-routing.mdpi-context-optimization-opportunities.mdCloses #3171, #3406, #3452, #3433
Test plan
🤖 Generated with Claude Code