# Phase 3 PR #1 Checklist: Adaptive Memory Injection and Compaction Weighting

Created: 2026-02-12
Owner: Flynn core
Status: ready to implement

## Goal

Improve memory usefulness and compaction quality by injecting relevant memory context adaptively and preserving high-value turns during compaction.

## Scope

In scope:
- adaptive memory relevance scoring
- configurable memory injection strategy
- compaction weighting for important turns (tool outcomes, corrections, preferences)
- tests and perf guardrails

Out of scope:
- new vector backend work
- UI controls for weighting

## Files

New:
- `src/memory/adaptive.ts`
- `src/memory/adaptive.test.ts`
- `src/context/weighting.ts`
- `src/context/weighting.test.ts`

Modified:
- `src/backends/native/orchestrator.ts`
- `src/context/compaction.ts`
- `src/memory/store.ts`
- `src/config/schema.ts`

## Implementation Steps

1. Add adaptive memory scorer:
   - keyword overlap with recent turns
   - recency weighting
   - token budget clipping
2. Add config flags:
   - `memory.injection_strategy` (`all|recent|adaptive`)
   - `memory.max_injection_tokens`
   - `compaction.importance_threshold`
3. Integrate adaptive injector in orchestrator memory injection path.
4. Add compaction message weighting and selection algorithm.
5. Ensure fallback to existing behavior on errors/timeouts.
6. Add tests for relevance selection and weighted compaction ordering.

## Validation Commands

```bash
pnpm typecheck
pnpm test:run src/memory/adaptive.test.ts
pnpm test:run src/context/weighting.test.ts
pnpm test:run src/context/compaction.test.ts
pnpm test:run src/backends/native/orchestrator.test.ts
pnpm test:run
pnpm lint
pnpm build
```

## Acceptance Criteria

- Adaptive mode injects fewer but more relevant memory snippets.
- Compaction preserves high-importance turns over low-value chatter.
- Token budgets are respected consistently.
- Existing behavior preserved when adaptive features disabled.
- No measurable latency regression beyond agreed budget.

## Risks

- Relevance scoring false positives/negatives.
  - Mitigation: conservative scoring + extensive fixtures.
- Added latency in hot path.
  - Mitigation: bounded scoring time and fallback mode.

## Commit Message

`feat(memory): add adaptive injection and weighted compaction`
