# Context Compaction Context-Limit Sourcing

## Accepted Source And Fallback

Spec source: `context-compaction-remediation.spec-clarify-context-limit-source`.

For compaction thresholding (`token_usage.total_tokens >= context_limit - reserved`),
the context window size MUST resolve with this precedence order:

1. Runtime model metadata limit (source of truth) when the active model exposes a positive integer context window.
2. Global config override (`defaults.context_window_tokens`) when set to a positive integer.
3. Hard default `200000` tokens.

Rationale:
- Runtime metadata best matches the model actually executing requests.
- Config override exists as deterministic fallback when metadata is unavailable.
- Hard default preserves current behavior and keeps compaction operational.

## Backward Compatibility

Existing config files without `defaults.context_window_tokens` remain valid with no migration.
When the field is absent, resolution skips step (2) and falls back to runtime metadata or hard default.

## Implementation Acceptance Criteria

- Compaction threshold wiring uses a single resolver implementing the exact precedence above.
- The resolver treats missing/invalid non-positive values as "not set" and continues fallback.
- Agent loop stores the resolved integer limit before running threshold checks.
