Compare commits

...

33 Commits

Author SHA1 Message Date
Ralph Chang 041115c173 chore(code-health): prepare v1.6.5 2026-05-19 15:05:48 +08:00
Ralph Chang a480b734b2 feat(memory): add rolling reinforcement window 2026-05-15 11:16:34 +08:00
Ralph Chang 5163ea3b8f docs(release): prepare v1.6.3 2026-05-14 17:06:03 +08:00
Ralph Chang 9591f85dca fix(memory-diag): remove duplicate active memory candidates 2026-05-14 16:53:17 +08:00
Ralph Chang 93550b2e41 feat(memory-diag): add memory command detail 2026-05-14 09:29:55 +08:00
Ralph Chang 3c4282b241 feat(memory-diag): clarify diagnostic provenance 2026-05-13 21:18:24 +08:00
Ralph Chang 5bca3432b0 feat(memory-diag): add quality review board command
Add memory-diag quality command for objective review of memory-system
mechanisms and active memory content. The command is read-only and
non-authoritative, providing evidence, heuristic flags, and review
questions without making quality judgments or suggesting mutations.

Key components:
- quality-review-model.ts: builds ReviewBoardReport with provenance,
  re-absorption detection, mechanism facts (rejection, reinforcement,
  eviction/caps, identity/dedup), and memory content facts
- formatters/quality.ts: human and JSON output with separate
  system-mechanism and memory-content sections
- commands/quality.ts: command entry point with --json, --verbose,
  --no-emoji, --raw options
- cli.ts: parser whitelist for quality accepting --workspace, --json,
  rejecting mutation/filter flags

co-author: code-execute-agent, comprehensive-code-reviewer,
systems-architect, creative-disruptor

Closes docs/plans/2026-05-11-memory-diag-quality-review-board.md
2026-05-12 14:13:03 +08:00
Ralph Chang e4dfe81d89 fix: package memory-diag compiled runtime 2026-05-11 15:43:56 +08:00
Ralph Chang 9b6955f490 docs(readme): remove roadmap section 2026-05-08 22:13:40 +08:00
Ralph Chang e708e77e61 docs(release): prepare v1.6.1 2026-05-08 22:09:08 +08:00
Ralph Chang 9114b57dc1 feat(tui): consolidate memory dialog menu 2026-05-08 21:41:57 +08:00
Ralph Chang 2ff17ea1b3 fix(tui): keep memory commands out of suggestions 2026-05-08 20:55:03 +08:00
Ralph Chang 65b3b2f2c3 fix(tui): clarify memory command surface 2026-05-08 19:49:56 +08:00
Ralph Chang 49bf866de2 feat(tui): add native memory visibility commands 2026-05-08 19:26:17 +08:00
Ralph Chang 8b46150fab docs(release): trim v1.6.0 release notes 2026-05-08 12:26:37 +08:00
Ralph Chang 79320cb21d chore(release): merge v1.6.0 2026-05-08 12:22:23 +08:00
Ralph Chang 09880c1840 feat(memory): add numbered compaction refs 2026-05-08 12:18:39 +08:00
Ralph Chang c538381969 chore(release): prepare v1.5.5 2026-05-05 12:23:37 +08:00
Ralph Chang 06dcf61711 feat(hot-state): section-aware greedy renderer, accounting types, fix docs drift
Replace blunt .slice(0, maxRenderedChars) truncation with a greedy line
accumulator that fits whole lines and suppresses header-only sections.
Add accountHotSessionStateRender() returning prompt + omitted[] accounting.
Export HotStateSection, HotStateOmissionReason, HotStateOmittedItem,
HotSessionStateRenderAccounting types for v2 evidence extension.
Fix configuration.md ranking formula (ACTION_WEIGHT + count*3, weights
50/45/30/20). Clarify README injection order. Add 9 unit tests.
2026-05-05 12:22:09 +08:00
Ralph Chang 2918645d8a refactor(memory-diag): remove legacy aliases, centralize command metadata, prepare v1.5.4
- Remove legacy CLI aliases (health, quality, rejections, disappearances, trace)
- Centralize command metadata in command-metadata.ts
- Move trace lifecycle into explain command
- Move disappearance helpers into missing formatter
- Remove cleanup:workspaces from package scripts (dev tool preserved)
- Bump version to 1.5.4
2026-05-02 21:57:13 +08:00
Ralph Chang 84aa020774 chore(release): prepare v1.5.3 2026-05-02 20:49:23 +08:00
Ralph Chang 3c13773231 feat(memory-diag): publish diagnostics CLI 2026-05-02 20:36:58 +08:00
Ralph Chang e0357c572a feat(reinforcement): compaction prompt wording reuse, migration evidence, and validation baseline
Wave 1 — Compaction prompt improvement:
- Add three wording-reuse bullets to buildCompactionPrompt() under
  CRITICAL MEMORY RULES: do not create rephrased duplicates, reuse
  existing wording exactly when re-emitting, only emit new memories
  when the fact is new, materially corrected, or more specific.
- This attacks the root cause of zero reinforcement: compaction
  generating variant text for the same durable fact.

Wave 2 — Bug fixes:
- Bug #2: Add placeholder comment to superseded_existing branch in
  decision dedupe (unreachable until v1.5.4 numbered refs). Preserve
  as const type assertions.
- Bug #3: Add memory_migration_superseded evidence event type. Both
  P0 and quality cleanup migrations now produce evidence events for
  superseded entries. loadWorkspaceMemory appends migration evidence
  on first-load migrations only (idempotent via migration IDs). No
  historical backfill.
- Bug #4: Add documentation comment explaining that feedback identity
  key returns exact key (absorbed_identity currently impossible for
  feedback). Add test verifying this behavior.

Wave 3 — Validation baseline script:
- Add scripts/dev/validate-identity-keys.ts: read-only script that
  scans workspace memory stores, computes exact/identity key
  collisions, and reports reinforcement statistics. Baseline matches
  audit: 0 exact collisions, 0 identity collisions, 0 reinforcement
  events across 123 active memories.

Identity extension is gated on measurement: if the prompt change
produces measurable reinforcement (reinforcementCount > 0), identity
extension may be unnecessary. Decision dedupe stays exact-only
(Wave 4 deferred).
2026-05-02 15:03:34 +08:00
Ralph Chang f19614565a chore(release): prepare v1.5.2 2026-05-01 16:00:44 +08:00
Ralph Chang 36593b512e Merge branch 'feat/deprecate-safety-critical' 2026-04-30 19:54:50 +08:00
Ralph Chang ab872ef2c6 chore(release): prepare v1.5.1 2026-04-30 19:54:24 +08:00
Ralph Chang f25a235b93 fix(retention): add UTC calendar-day diversity gate to reinforceMemory
Implement OQ-2 decision: allow at most one reinforcement per memory
identity per UTC calendar day. Same-day reinforcement is blocked
regardless of session or interval. This prevents repetitive-task
gaming where a daily recurring task could reach MAX_COUNT=6 in hours.

Guard order: same-session → calendar-day → 1-hour → max-count
(existing guards kept as defense-in-depth)

1 hour guard is redundant within same day but preserved for
sub-hour edge cases.
2026-04-30 18:38:29 +08:00
Ralph Chang 84245c783d feat(explainability): add diagnostics JSON, per-memory explain, lifecycle trace
Phase 4 Tasks 4.1-4.3:
- memory-diag health --json: machine-readable MemoryDiagJSON output
- memory-diag explain: per-memory render status with strength, reasons,
  evidence event IDs
- memory-diag trace --memory <id>: lifecycle history from evidence events
  and relations (superseded_by, reinforced_by)
- MemoryRenderStatus type with 9 statuses
- All diagnostics are read-only, no storage mutations
- Privacy-safe: redacted text previews, no raw secrets
- 270 tests pass, typecheck pass
2026-04-30 18:06:28 +08:00
Ralph Chang 617b3646d8 feat(evidence): wire evidence events into extraction, promotion, reinforcement, render, storage, and hook lifecycle
Phase 3 Tasks 3.2-3.6:
- Extraction evidence: accepted/rejected/explicit_detected/explicit_ignored
- Promotion evidence with relation edges (superseded/superseded_by, absorbed/retained)
- Reinforcement evidence with reinforced/reinforced_by relations
- Render accounting helper with render_selected/render_omitted evidence
- Storage evidence: corrupt_json_quarantined, stale_lock_recovered, lock_timeout
- Hook failure evidence in plugin
- All evidence failures swallowed, never throw into memory behavior
- Privacy-safe textPreview (redacted + truncated)
- 266 tests pass, typecheck pass
2026-04-30 17:54:13 +08:00
Ralph Chang 27e9d7ce92 feat(evidence): add evidence infrastructure - types, append, query, retention
Phase 3 Task 3.1:
- Create src/evidence-log.ts with EvidenceEventType, EvidencePhase,
  EvidenceOutcome, MemoryEvidenceRef, EvidenceRelation, EvidenceEventV1,
  EvidenceEventInput types
- Add appendEvidenceEvent/appendEvidenceEvents with safe write, privacy
  hashing (SHA-256 truncated), textPreview redaction, bounded retention
- Add queryEvidenceEvents, summarizeMemoryEvidence, traceMemoryLifecycle
- Add workspaceEvidenceLogPath to src/paths.ts
- Add 8 evidence-log tests: round-trip, privacy, query, resilience, retention
- Relations limited to wiring roles only (no kind/derived_from/validates)
- 253 tests pass
2026-04-30 17:33:40 +08:00
Ralph Chang aa7cc6c60e refactor(retention): extract retention module from workspace-memory
Move retention constants and math to a focused src/retention.ts module:
- All half-life, reinforcement, dormancy constants
- TYPE_FACTOR, SOURCE_FACTOR, USER_IMPORTANCE_FACTOR
- RETENTION_TYPE_MAX (renamed from TYPE_MAX)
- calculateInitialStrength, calculateEffectiveHalfLife,
  calculateRetentionStrength, calculateDormantDays,
  calculateEffectiveAgeDays, reinforceMemory

No behavior changes. retention.ts imports only types from types.ts.
Workspace-memory.ts still owns storage, consolidation, and rendering.
2026-04-30 17:28:31 +08:00
Ralph Chang 36f00147ca feat(deprecation): remove safetyCritical retention multiplier and type-cap bypass
- Remove SAFETY_CRITICAL_FACTOR = 6.0 from workspace-memory.ts
- Remove safetyFactor from calculateInitialStrength() - all memories now
  fade according to the same rules
- Remove safetyCritical bypass from applyTypeMaxCaps() - safetyCritical
  entries compete normally under TYPE_MAX caps
- Preserve safetyCritical?: boolean in LongTermMemoryEntry type for
  backward compatibility (no producer sets it to true)
- Update memory-diag to show deprecation warning instead of capacity alert
- Update tests: add backward-compatibility fixture test, deprecation
  strength test, normal cap competition test
- Update docs/architecture.md, RELEASE_NOTES.md, CHANGELOG.md,
  docs/configuration.md

Phase 1.5 complete: safetyCritical is now a deprecated field with no
active behavior. Safety rules belong in user-controlled agent.md files.
2026-04-30 17:23:01 +08:00
Ralph Chang 830d97c6c6 fix(security): harden hooks, quarantine corrupt JSON, test locks, fix promotion dedupe
- Wrap hooks with try/catch to prevent OpenCode disruption
- Add warnMemoryHook() for safe error logging
- Quarantine corrupt JSON files before fallback
- Add cross-process lock safety tests
- Fix pending promotion same-batch dedupe
- Update docs/architecture.md with lock semantics
- 242 tests passing
2026-04-30 11:52:01 +08:00
85 changed files with 17556 additions and 1286 deletions
+2
View File
@@ -51,6 +51,8 @@ pnpm-lock.yaml
# Superpowers local planning artifacts
docs/superpowers/plans/
docs/plans/
docs/disruptions/
# Local dev/admin script inputs
scripts/dev/run-migration-roots.local.txt
+2 -2
View File
@@ -208,8 +208,8 @@ const typedData = data as WorkspaceMemoryStore; // Explicit cast after validati
// ============================================================================
// ✅ REQUIRED: Block comments for complex logic
// Quality gate: Reject candidates that are git hashes, errors, or path-heavy
function shouldAcceptWorkspaceMemoryCandidate(candidate: string): boolean {
// Quality gate: return accepted/reasons so rejection evidence stays explainable
function evaluateWorkspaceMemoryCandidate(candidate: WorkspaceMemoryCandidate): CandidateEvaluation {
// ...
}
+200 -1
View File
@@ -5,13 +5,211 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [1.6.5] - 2026-05-19
### Added
- Added `check:package-integrity` to verify `package.json` and on-disk `package-lock.json` root versions stay aligned even though the lockfile remains ignored by git.
- Added `tsconfig.unused.json` as a strict unused-symbol audit gate for development and release checks.
- Added package-integrity tests covering matching versions, mismatch reporting, and missing-lockfile guidance.
- Added storage/evidence contract tests for full-state JSON overwrites and concurrent evidence JSONL appends.
- Added workspace-memory render-order characterization and memory-visibility order coverage for the shared memory type order.
### Changed
- Centralized the current memory type ordering (`feedback`, `project`, `decision`, `reference`) in a narrow `memory-kind-policy` seam used by workspace rendering, TUI grouping, and memory visibility.
- Extracted diagnostics producer-version grouping and inference helpers from `memory-diag quality` into a pure diagnostics-only module while preserving the existing JSON and human output contracts.
- Documented storage write-path contracts in code: `updateJSON` is the locked read-modify-write path, `atomicWriteJSON` is the full-state overwrite primitive, and evidence logs remain append-only JSONL with bounded pruning.
- Marked legacy parser fixtures and retention caps as intentional compatibility/policy-contract test coverage.
- Updated developer docs to reference `evaluateWorkspaceMemoryCandidate` instead of the removed private acceptance wrapper.
### Deprecated
- Marked `REINFORCEMENT_MIN_INTERVAL_MS` with JSDoc `@deprecated`; the rolling reinforcement policy uses `REINFORCEMENT_MIN_ELAPSED_MS`.
### Removed
- Removed unused imports and private unused helpers discovered by the new unused-symbol audit, including the private `shouldAcceptWorkspaceMemoryCandidate` wrapper.
### Fixed
- Fixed release hygiene drift detection for the ignored lockfile by adding an explicit package integrity check.
- Reduced future diagnostics and memory-kind change risk by extracting small behavior-preserving seams without changing runtime memory behavior.
## [1.6.4] - 2026-05-15
### Changed
- Replaced same-session reinforcement blocking with a rolling 7-day elapsed reinforcement window so long-lived OpenCode sessions can reinforce durable memories after meaningful weekly recurrence.
- Kept the 45-day base half-life while changing the max reinforcement count to a growth saturation point: memories at count 6 can refresh retention timestamps weekly without increasing count or effective half-life.
- Bumped memory evidence instrumentation to version 3 for the new elapsed-window and refresh-only reinforcement semantics.
- Updated `memory-diag commands --memory` to show elapsed-window details, `sameSession` evidence, `reinforcementMode`, and legacy missing timestamp markers without exposing raw session IDs.
- Updated `memory-diag quality` to keep historical `same_session` block accounting while preventing new `sameSession` evidence from triggering old same-session diagnostic questions.
### Fixed
- Prevented long-lived sessions from indefinitely blocking reinforcement solely because the session ID stayed the same across days.
- Prevented saturated memories from growing stronger beyond the max reinforcement count while still allowing continued weekly use to keep them fresh.
- Preserved historical block reason compatibility for `same_session`, `same_utc_day`, `min_interval`, and `max_count` without producing those reasons from the new policy path.
## [1.6.3] - 2026-05-14
### Added
- Added `memory-diag quality`, a read-only review board for memory-system mechanism evidence, answerability levels, provenance classification, active-memory review surfaces, and JSON review output.
- Added producer/version-aware diagnostic facts so current instrumentation can be separated from historical or unversioned evidence when reviewing reinforcement, rejection, and eviction patterns.
- Added `memory-diag commands --memory <memory-id>` for focused reinforcement command detail, including current memory status, recorded block reasons, missing block detail counts, UTC-day evidence, and privacy-safe JSON.
### Changed
- Made `activeMemoryDisplay` the canonical active-memory review surface in `memory-diag quality` JSON and removed duplicate active-memory `reviewCandidates` entries.
- Clarified diagnostic provenance and answerability wording so `memory-diag quality` separates facts, heuristic flags, review questions, and human judgment requirements.
### Fixed
- Removed duplicate active-memory candidate construction from `memory-diag quality` to prevent drift between human output and JSON surfaces.
- Kept reinforcement detail diagnostics evidence-only so blocked reinforcement attempts are shown as recorded evidence without claiming policy failure or memory loss.
## [1.6.2] - 2026-05-11
### Fixed
- Fixed the published `memory-diag` npm bin by compiling the diagnostics CLI before packing and launching the compiled JavaScript runtime instead of type-stripping TypeScript under `node_modules`.
### Added
- Added a pack/npx smoke test that runs `memory-diag --help` from a packed tarball outside the repository.
## [1.6.1] - 2026-05-08
### Added
- Native OpenCode TUI `/memory` submenu for local memory statistics, searchable current workspace memory refs, and help.
- Package `./tui` export for OpenCode TUI plugin loading.
### Changed
- README documents separate server and TUI plugin configuration.
- Recent activity/last TUI commands were removed before release because duplicate-looking slash menu entries were not useful.
- Pre-release hyphenated TUI commands were consolidated into `/memory` because native submenu/list dialogs provide better bounded navigation with less slash-menu clutter.
### Fixed
- Replaced a literal NUL byte in `workspace-memory.ts` regex source with a `\0` escape so source search tools treat the file as text.
### Notes / Known UX
- TUI memory command output opens in transcript-free native TUI dialogs and does not call the LLM.
## [1.6.0] - 2026-05-08
### Added
- Numbered compaction memory references (`[M1]`, `[M2]`, ...) for existing rendered workspace memories.
- Compaction memory commands: `REINFORCE [M#]` for retention reinforcement and `REPLACE [M#] [type] text` for protected replacement.
- `CompactionMemoryRef` session-state snapshots with optional compaction IDs for overlap detection.
- Evidence events for numbered memory command outcomes: `memory_reinforced`, `memory_replaced_numbered_ref`, and `memory_reverted_numbered_ref`.
- Public `memory-diag commands` report for command counts, outcomes, rejection reasons, protected replacement blocks, malformed commands, and latest command events.
- Public dry-run-first `memory-diag revert` command for manually reverting successful numbered replacements by replacement memory ID or evidence event ID.
- Hard quality rejection reasons for unresolved questions, transient bug/debug state, and deployment snapshots.
- Soft `terse_label` diagnostic for very short label-like candidates.
- Regression tests for command parsing, REINFORCE, protected REPLACE, revert behavior, compaction ref validation, overlap protection, and fallback behavior when the model omits the compaction snapshot ID.
### Changed
- Compaction prompts now include numbered memory refs and concise memory-operation rules instead of asking the model to reuse existing wording exactly.
- Compaction no longer duplicates hot session state inside the compaction prompt; hot state remains available in normal prompt context.
- Duplicate maintenance now prefers explicit REINFORCE or protected REPLACE evidence over silent duplicate restatement.
- Rendered decision memory cap increased from 10 to 12 while keeping the global rendered cap at 28.
- Rejected memory command evidence now uses neutral `target` relations instead of lifecycle-mutating `reinforced` or `superseded` relation roles.
- `memory-diag` public command metadata now includes `commands` and `revert` alongside `status`, `rejected`, `missing`, and `explain`.
### Fixed
- Overlapping same-session compactions can no longer silently apply numbered commands against the wrong snapshot when the snapshot ID is present.
- Numbered command resolution now rejects stale refs whose memory ID, status, or exact key no longer match the current workspace memory entry.
- Protected replacements are surfaced as first-class diagnostics instead of being buried in generic rejection counts.
### Recovery note
Successful numbered replacements supersede the original memory and add a replacement. To inspect or recover one, run `memory-diag commands --verbose`, then dry-run `memory-diag revert --memory <replacement-memory-id>` or `memory-diag revert --event <event-id>` before adding `--apply`.
## [1.5.5] - 2026-05-05
### Added
- Section-aware greedy line accumulator for hot session state rendering that fits whole lines instead of truncating mid-line.
- `accountHotSessionStateRender()` accounting function returning prompt text, omitted items, and char budget for v2 evidence extension.
- Exported types for render accounting diagnostics: `HotStateSection`, `HotStateOmissionReason`, `HotStateOmittedItem`, `HotSessionStateRenderAccounting`.
- 9 direct unit tests for hot session state rendering covering empty state, ranking, section caps, char budget, boundary conditions, header suppression, wrapper parity, and newline counting.
- Two-layer omission model: section caps first (`section_cap`), then char budget (`char_budget`), with clear per-item omission reasons.
### Changed
- Hot session state rendering no longer uses blunt `.slice(0, maxRenderedChars)` prompt truncation.
- Header-only sections are suppressed: a section is only rendered if at least one entry line fits the char budget.
- `renderHotSessionState()` is now a delegation wrapper to `accountHotSessionStateRender().prompt`, preserving backward compatibility.
- Fixed `docs/configuration.md` active-file ranking formula: was `count * action_weight * recency_decay` with weights 4/3/2/1, now correctly `ACTION_WEIGHT[action] + count * 3` with weights edit 50, write 45, grep 30, read 20, tie-break by `lastSeen` descending.
- Clarified `README.md` system prompt injection order: workspace memory and hot state index positions depend on whether workspace memory state is available.
### Fixed
- Hot session state prompt could be truncated mid-line under the old `.slice()` budget enforcement.
## [1.5.4] - 2026-05-02
### Changed
- Centralized the supported `memory-diag` command metadata for the official public commands: `status`, `rejected`, `missing`, and `explain`.
- Removed pre-public legacy aliases so unsupported spellings now use the standard unknown-subcommand path instead of carrying v2.0 compatibility debt.
- Kept `coverage` and `audit` as hidden maintainer diagnostics with neutral maintainer-only notices.
- Removed public npm script exposure for the internal workspace cleanup tool while keeping the development tool in the repository.
### Fixed
- Moved missing-memory disappearance detail formatting into the official `missing` formatter before deleting legacy disappearance formatter code.
## [1.5.3] - 2026-05-02
### Added
- Published the read-only `memory-diag` package binary for package-qualified `npx --package opencode-working-memory memory-diag` usage.
- Added user-facing diagnostics commands for concise status, rejected memory review, missing memory review, and per-memory explanation.
### Changed
- Reworked the diagnostics CLI into focused command, model, formatter, and utility modules while preserving legacy aliases and maintainer diagnostics.
- Updated README and configuration docs to document the supported `memory-diag` CLI workflow and package-qualified `npx` usage.
- Raised the Node.js requirement to `>=22.6.0` for the TypeScript diagnostics CLI runtime.
### Fixed
- Avoided repeated workspace snapshot construction in status diagnostics.
- Replaced per-memory evidence summary lookups with grouped evidence processing to avoid N+1 diagnostics work.
- Added top-level CLI error handling for cleaner user-facing failures.
- Removed a diagnostics model circular dependency by extracting evidence grouping helpers to a neutral module.
## [1.5.1] - 2026-04-30
### Added
- Per-workspace evidence log for extraction, promotion, reinforcement, render, storage, and hook lifecycle events.
- `memory-diag health --json` for machine-readable diagnostics.
- `memory-diag explain` for per-memory render status, strength, reasons, and evidence event IDs.
- `memory-diag trace --memory <id>` for memory lifecycle history.
- UTC calendar-day reinforcement gate so repeated matches cannot inflate a memory multiple times in the same day.
### Changed
- Retention constants and calculations moved to `src/retention.ts`.
- `safetyCritical` is now fully inert: no retention multiplier and no type-cap bypass, while remaining JSON-compatible.
## [1.5.0] - 2026-04-29
### Added
- Strength-based workspace memory retention using exponential decay instead of additive priority scoring.
- Per-type rendered caps for workspace memory candidates: feedback 10, decision 10, project 8, and reference 6.
- Safety-critical memory weighting and type-cap exemption so important entries survive type floods while still competing under the global rendered cap.
- Dormant-workspace effective age: after 14 days without activity, additional dormant time counts at 0.25x for retention decay.
- Reinforcement tracking for repeated memories, with same-session and one-hour guards to prevent accidental reinforcement spam.
- Memory health diagnostics for stored vs rendered counts, type caps, global cap overflow, dormancy, retention monitoring, and strength-ranked top/weakest entries.
@@ -21,6 +219,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Workspace memory rendering now ranks entries by retention strength, not the previous priority/penalty model.
- Confidence is retained for compatibility but no longer affects retention scoring.
- Deprecated `safetyCritical` is retained for JSON compatibility but no longer affects retention strength or type-cap behavior.
- Old or stale-marked memories are no longer hard-pruned; they remain stored and only fall out of rendered context through strength and cap competition.
- Existing duplicate promotion and dedupe paths now reinforce the surviving memory instead of only absorbing the duplicate.
- Health output now separates stored active memories from rendered candidates to make cap behavior easier to understand.
+98 -72
View File
@@ -23,27 +23,73 @@ Use it when you want your agent to remember things like:
- Important file paths or references
- Current active files and unresolved errors
## Features
## What You Get
- **Workspace memory** — durable project facts, preferences, decisions, and references across sessions.
- **Hot session state** — active files, open errors, and current working context for the current session.
- **Explicit memory triggers** — users can say “remember this”, “記住”, “覚えて”, or “기억해” to save durable facts.
- **Compaction-based extraction** — memory extraction piggybacks on OpenCodes existing compaction flow.
- **No manual tools** — memory is injected automatically into the system prompt.
- **Quality guards** — filters noisy memories, temporary progress snapshots, stack traces, raw errors, and credentials.
- **Retention decay** — keeps the strongest memories in prompt context while older or weaker memories fade out naturally; important and reinforced memories decay more slowly.
| Need | Feature |
|---|---|
| Remember durable context | Workspace memory keeps project facts, preferences, decisions, and references across sessions. |
| Capture what matters | Say `remember this` or `記住` to explicitly save important rules and preferences. |
| Inspect memory locally | Use `/memory` in the OpenCode TUI to browse status, help, and searchable current `[M#]` memories. |
| Stay out of the way | Memory is injected automatically and piggybacks on OpenCode compaction — no manual tools, no extra LLM/API calls. |
| Keep memory clean | Quality guards filter noise, redact credentials, dedupe repeats, and let weak memories fade. |
```text
remember this ──► workspace memory ──► /memory
▲ │ searchable [M#] refs
│ ▼
compaction ─────► reinforce / replace ──► selective prompt context
```
## Installation
Add OpenCode Working Memory to your OpenCode config:
New users: add OpenCode Working Memory to both OpenCode plugin configs.
`.opencode/opencode.json`:
```json
{
"$schema": "https://opencode.ai/config.json",
"plugin": ["opencode-working-memory"]
}
```
Then restart OpenCode. It activates automatically.
`.opencode/tui.json`:
```json
{
"$schema": "https://opencode.ai/tui.json",
"plugin": ["opencode-working-memory"]
}
```
Existing users: keep your current `.opencode/opencode.json` config and add only the `.opencode/tui.json` block above to enable the native `/memory` TUI menu.
Then restart OpenCode. Memory activates automatically, and `/memory` appears in the TUI slash command menu.
## Native TUI Memory Menu
The TUI plugin adds one display-only local memory command:
- `/memory` — open a native memory submenu.
Submenu entries:
- Status — show status counts for workspace memory, rendered memories, pending memory, open errors, and recent decisions.
- Current memories — browse a searchable grouped list of current active workspace memories with display-local `[M1]` refs.
- Help — show command help.
This menu is read-only and local-only. It reads local memory files and opens native TUI dialogs, so it does not create conversation history entries and does not make an LLM/API call.
```text
/memory
├─ Status
├─ Current memories ← searchable, grouped [M#] refs
└─ Help
```
Use `/memory` when you want to inspect what the agent currently remembers without asking the model or polluting the transcript.
Compaction output already appears through OpenCode's built-in conversation flow. This plugin does not add duplicate compaction notices.
## How It Works
@@ -85,11 +131,13 @@ OpenCode Working Memory adds durable memory without making extra LLM/API calls.
┌──────────────────────────────────────┐
│ ⚡ Prompt Context │
│ system[1]: frozen workspace memory
│ system[2+]: hot session state
│ system[1]*: frozen workspace memory │
│ system[2+]*: hot session state │
└──────────────────────────────────────┘
```
\* Conceptually, workspace memory is pushed first when it is non-empty, and hot session state is pushed after workspace memory. If workspace memory is empty, hot state may be the first plugin-added system message. Actual `system[]` indices also depend on OpenCode and other plugins, so `system[1]` / `system[2+]` is a simplified model.
**Zero extra API calls:** OpenCode Working Memory does not call the model on its own. Memory extraction is folded into OpenCode's built-in compaction request.
**Cache-friendly layout:** durable workspace memory is rendered as a stable frozen snapshot for the session, while fast-changing hot session state is appended separately. Compaction starts a new cache epoch, refreshing the workspace snapshot after pending memories are promoted.
@@ -145,69 +193,65 @@ Memories decay over time. The strongest stay visible in the prompt; weaker ones
## Explicit Memory Triggers
You can explicitly ask the agent to remember durable facts.
Examples:
Most memory is extracted automatically during compaction. When something is especially important, tell the agent directly:
```md
Remember this: we prefer Vitest for new frontend tests.
記住:這個 repo 發 release 前要先跑 npm test。
覚えておいて: API clients should use the shared retry helper.
기억해줘: this project uses pnpm, not npm.
```
Supported trigger languages include:
Use explicit triggers for stable preferences, project rules, architecture decisions, or important references. Then inspect active workspace memory with:
| Language | Examples |
|---|---|
| English | `remember this`, `save to memory`, `from now on`, `my preference` |
| Chinese | `記住`, `记住`, `記得`, `请帮我记住` |
| Japanese | `覚えて`, `覚えておいて`, `メモして` |
| Korean | `기억해`, `기억해줘`, `메모해줘` |
Negative requests are respected too:
```md
Don't remember this.
不要記住這個。
覚えないで。
기억하지 마.
```text
/memory → Current memories
```
Avoid saving:
Trigger phrases include `remember this`, `save to memory`, `from now on`, `my preference`, `記住`, `記得`, `覚えて`, and `기억해`.
- Secrets, passwords, tokens, or credentials
- Temporary progress updates
- Raw command output
- Short-lived session details
Negative requests are respected too: `Don't remember this`, `不要記住這個`, `覚えないで`, `기억하지 마`.
Avoid asking memory to save secrets, temporary progress, raw command output, or short-lived session details.
## Quality Guards
OpenCode Working Memory tries to keep memory useful and low-noise.
**Good memory is selective memory.**
It includes guards for:
OpenCode Working Memory is designed to be selective. Its strength is not storing more; it is keeping the prompt focused on durable facts that still help.
- Credential redaction
- Duplicate memory cleanup
- Accounting for promoted, absorbed, superseded, and rejected memories
- Strength-based retention so useful memories stay visible without hard age pruning
- Filtering stack traces, git hashes, raw errors, and noisy path-heavy facts
- Rejecting temporary project progress snapshots
It protects memory quality in three ways:
- **Selective** — filters temporary progress, raw errors, stack traces, git hashes, noisy debug fragments, and duplicate restatements.
- **Safe** — redacts credentials and protects manual or explicit memories from unsafe automatic replacement.
- **Diagnosable** — tracks promoted, absorbed, superseded, rejected, reinforced, and replaced memory outcomes.
The goal is to remember durable facts, not every detail.
**Good memory is selective memory.**
Historical cleanup is intentionally conservative: extraction-time filtering may reject more aggressively, but one-time migration cleanup only supersedes high-confidence garbage patterns. This protects existing durable memories written in declarative style, such as "API endpoint is X" or "Product branding is Y".
For local development cleanup, use:
### Numbered Memory Refs
```bash
npm run cleanup:workspaces -- --dry-run
npm run cleanup:workspaces -- --quarantine
During compaction, existing workspace memories may be shown as numbered refs such as `[M1]` or `[M2]`. The model can reinforce a still-useful memory or propose a protected replacement instead of copying the same fact again.
```md
REINFORCE [M1]
REPLACE [M2] project Updated durable project fact.
```
The cleanup command only quarantines definite temp/test workspace residues by default. It does not delete unknown missing-root workspaces.
Protected memories and stale refs are rejected rather than mutated. Use `memory-diag commands` for detailed command outcomes and recovery guidance.
### Memory Diagnostics CLI
For deeper troubleshooting, use the read-only `memory-diag` CLI:
```bash
npx --package opencode-working-memory memory-diag status
npx --package opencode-working-memory memory-diag rejected
npx --package opencode-working-memory memory-diag missing
npx --package opencode-working-memory memory-diag explain <memory-id>
npx --package opencode-working-memory memory-diag quality
```
See [Diagnostics](docs/diagnostics.md) for the full command reference, numbered memory command reports, and dry-run recovery workflow.
## Configuration
@@ -223,35 +267,17 @@ Default behavior:
See [Configuration](docs/configuration.md) for customization options.
## Roadmap
Current focus:
- Add explicit delete tombstones so removed memories do not get re-extracted.
- Enforce explicit `supersedes` chains for safer replacement of obsolete memories.
- Explore tiered hot/warm/cold storage after the retention model has more real-world data.
## Documentation
- [Architecture Overview](docs/architecture.md)
- [Configuration](docs/configuration.md)
- [Diagnostics](docs/diagnostics.md)
- [Installation Guide](docs/installation.md)
## Development
```bash
git clone https://github.com/sdwolf4103/opencode-working-memory.git
cd opencode-working-memory
npm install
npm test
npm run typecheck
npm run cleanup:workspaces -- --dry-run
```
## Requirements
- OpenCode plugin API `>=1.2.0 <2.0.0`
- Node.js >= 18.0.0
- Node.js >= 22.6.0 (the published `memory-diag` CLI runs compiled JavaScript)
## Limitations
+428 -4
View File
@@ -1,12 +1,436 @@
# Release Notes
## 1.6.5 (2026-05-19)
### Code Health and Release Hygiene
This patch release is an internal health release before the next feature wave. It does not change memory extraction, reinforcement policy, TUI behavior, or the `memory-diag` CLI contract. Instead, it makes the codebase easier to audit and safer to modify.
The release adds package-version integrity checks, a clean unused-symbol audit, focused characterization tests, storage/evidence contract coverage, a narrow shared memory-type ordering seam, and a small diagnostics versioning extraction.
### What Changed
- **Package integrity check**: added `npm run check:package-integrity` to verify `package.json` and the on-disk `package-lock.json` root versions match, with a clear `run npm install first` message when the ignored lockfile is missing.
- **Unused-symbol audit**: added `tsconfig.unused.json` and cleaned the existing unused imports/private helpers so the audit now passes cleanly.
- **Memory type order seam**: centralized the current order (`feedback`, `project`, `decision`, `reference`) for workspace rendering, memory visibility, and TUI grouping without creating a broader policy registry.
- **Storage/evidence contracts**: documented write-path semantics and added tests for full-state JSON overwrite behavior and concurrent evidence JSONL appends.
- **Diagnostics containment**: extracted producer-version grouping and inference helpers from `memory-diag quality` into a pure diagnostics-only module while preserving existing output shape and wording.
- **Characterization coverage**: added render-order coverage and labeled compatibility/policy-contract tests so future refactors can distinguish intentional legacy behavior from brittle fixtures.
### Upgrade Notes
- No configuration changes are required.
- Existing workspace memory files and evidence logs remain compatible.
- The `memory-diag` CLI JSON shape and human output wording are intended to be unchanged.
- `package-lock.json` remains ignored by git in this repository; run `npm install` before `npm run check:package-integrity` if the lockfile is missing locally.
- `REINFORCEMENT_MIN_INTERVAL_MS` remains exported for compatibility but is now marked `@deprecated`; use `REINFORCEMENT_MIN_ELAPSED_MS` for the rolling reinforcement policy.
### Validation
- `npm run check:package-integrity``PACKAGE_INTEGRITY_PASS version=1.6.5`
- `node --import ./tests/setup-xdg-data-home.ts --test --experimental-strip-types tests/package-integrity.test.ts` — 3 tests passing
- `./node_modules/.bin/tsc -p tsconfig.unused.json` — no unused-symbol errors
- `node --test --experimental-strip-types tests/memory-diag-quality.test.ts tests/memory-diag.test.ts` — 93 tests passing
- `node --import ./tests/setup-xdg-data-home.ts --test --experimental-strip-types tests/storage.test.ts tests/evidence-log.test.ts` — 22 tests passing
- `npm run typecheck``TYPECHECK_PASS`
- `npm test` — 504 tests passing, `TEST_PASS`
- `npm run build``BUILD_PASS`
---
## 1.6.4 (2026-05-15)
### Rolling Weekly Reinforcement
This patch release fixes the reinforcement policy for users who work in long-lived OpenCode sessions. Reinforcement no longer treats `same_session` as a hard block. Instead, each memory uses a rolling 7-day elapsed window, so recurring preferences can be reinforced after meaningful weekly use even when the session stays open.
The base retention half-life remains 45 days. The max reinforcement count remains 6, but it now acts as a growth saturation point rather than a lifetime hard stop.
### What Changed
- **7-day rolling window**: repeated reinforcement is allowed once 7 rolling days have elapsed since the memory's last reinforcement; 7 days minus 1ms still blocks.
- **Same-session as evidence**: `sameSession` is recorded for diagnostics but no longer blocks reinforcement by itself.
- **Refresh-only saturation**: memories at reinforcement count 6 can refresh `retentionClock`, `lastReinforcedAt`, and session evidence after the weekly window without increasing count or effective half-life.
- **Instrumentation v3**: new reinforcement evidence records elapsed-window fields, `sameSession`, `reinforcementMode`, and legacy missing timestamp markers.
- **Diagnostics updated**: `memory-diag commands --memory` exposes the new fields, while `memory-diag quality` keeps historical `same_session` block analysis separate from new same-session evidence.
### Upgrade Notes
- No configuration changes are required.
- Existing workspace memory files and evidence logs remain compatible.
- Historical diagnostics may still show older block reasons such as `same_session`, `same_utc_day`, `min_interval`, or `max_count`; new instrumentation-version-3 events use the rolling elapsed-window semantics.
- Consumers of `memory-diag commands --memory --json` should use `reinforcementMode` to distinguish count-increment reinforcement from refresh-only saturation.
### Validation
- `node --test --experimental-strip-types tests/retention.test.ts` — 10 tests passing
- `node --test --experimental-strip-types tests/workspace-memory.test.ts tests/plugin.test.ts` — 181 tests passing
- `node --test --experimental-strip-types tests/memory-diag.test.ts tests/memory-diag-quality.test.ts` — 93 tests passing
- `npm run typecheck``TYPECHECK_PASS`
- `npm test` — 498 tests passing, `TEST_PASS`
- `npm run build``BUILD_PASS`
---
## 1.6.3 (2026-05-14)
### Diagnostic Quality Review Board
This patch release focuses on safer memory diagnostics. It adds a read-only quality review board and finer reinforcement drill-downs so reviewers can inspect memory-system evidence without treating historical artifacts as current failures or turning heuristic flags into automatic cleanup decisions.
The goal is better observability before policy changes: diagnostics show facts, provenance, answerability, and review questions, while leaving judgment to the operator.
### What Changed
- **Quality review board**: `memory-diag quality` now reports system-mechanism facts for rejection filters, reinforcement rules, eviction/caps, identity/dedup, and active memory content review.
- **Version/provenance context**: diagnostics distinguish current producer-instrumented events from historical or unversioned evidence where possible, and label ambiguous evidence conservatively.
- **Focused reinforcement drill-down**: `memory-diag commands --memory <memory-id>` shows one memory's reinforcement command evidence, current status, recorded block reasons, missing details, and UTC-day evidence.
- **Canonical active-memory JSON surface**: `memory-diag quality --json` now uses `activeMemoryDisplay` as the single active-memory review surface instead of duplicating active memories under `reviewCandidates`.
- **Attribution-safe wording**: quality and reinforcement diagnostics avoid claiming that a block is a bug, policy failure, or cause of memory loss; they present recorded evidence and review prompts instead.
### Upgrade Notes
- No configuration changes are required.
- Existing workspace memory files and evidence logs remain compatible.
- If you consume `memory-diag quality --json` from unreleased builds after v1.6.2, read active-memory review data from `activeMemoryDisplay`; `reviewCandidates` is now reserved for system-mechanism candidates.
### Useful Commands
```bash
npx --package opencode-working-memory@1.6.3 memory-diag quality
npx --package opencode-working-memory@1.6.3 memory-diag quality --json
npx --package opencode-working-memory@1.6.3 memory-diag commands --memory <memory-id>
```
### Validation
- `node --test --experimental-strip-types tests/memory-diag-quality.test.ts` — 53 tests passing
- `node --test --experimental-strip-types tests/memory-diag.test.ts` — 39 tests passing
- `npm run typecheck``TYPECHECK_PASS`
- `npm test` — 486 tests passing, `TEST_PASS`
- `npm run build``BUILD_PASS`
- `npm run test:pack:memory-diag` — packed tarball smoke test passed
---
## 1.6.2 (2026-05-11)
### Published `memory-diag` Bin Fix
This patch release fixes the published npm package path for `memory-diag`. In v1.6.1 the source-tree CLI tests passed, but the installed package could fail under `npx --package opencode-working-memory memory-diag` because Node refuses TypeScript type stripping for files inside `node_modules`.
v1.6.2 compiles the diagnostics CLI during packing and makes the npm bin launch the compiled JavaScript runtime.
### What Changed
- **Compiled diagnostics runtime**: `prepack` now builds `dist/scripts/memory-diag.js` before the package is packed or published.
- **Safer npm bin wrapper**: `memory-diag` no longer runs published `.ts` files through `--experimental-strip-types`; it launches the compiled JS artifact and reports a clear reinstall/build message if the artifact is missing.
- **Packaged-bin smoke test**: release verification now includes a pack/npx smoke test from a temp consumer project outside the repository.
### Upgrade Notes
- No config changes are required.
- Existing OpenCode server and TUI plugin entry points are unchanged.
- If you hit the v1.6.1 bin failure, upgrade and rerun:
```bash
npx --package opencode-working-memory@1.6.2 memory-diag --help
```
### Validation
- `npm run build``BUILD_PASS`
- `node ./scripts/memory-diag-bin.cjs --help`
- `npm run test:pack:memory-diag` — packed tarball smoke test passed
- `npm run typecheck``TYPECHECK_PASS`
- `npm test` — 421 tests passing, `TEST_PASS`
- `npm pack --dry-run` — includes compiled `dist/` diagnostics artifacts
---
## 1.6.1 (2026-05-08)
### Native TUI Memory Menu
This release adds a native OpenCode TUI memory menu so users can inspect local working memory without asking the model and without adding command output to the conversation transcript.
Open `/memory` in the TUI to browse memory status, current workspace memories, and help from native dialogs.
> Memory should stay visible when you need it — and stay out of the transcript when you are only inspecting it.
```text
/memory
├─ Status
│ local counts and memory health
├─ Current memories
│ searchable grouped [M#] refs
└─ Help
local usage notes
```
### What Changed
- **Single TUI entry point**: `/memory` opens a native submenu instead of exposing multiple memory slash commands.
- **Searchable current memory list**: `Current memories` uses OpenCode's native select dialog for bounded scrolling, filtering, and grouping.
- **Transcript-free inspection**: memory status, list, help, empty states, and errors render in native dialogs instead of user-style session messages.
- **Server and TUI plugin exports**: the package exposes `./server` and `./tui` entry points for OpenCode plugin loading.
- **User docs refreshed**: README highlights the `/memory` workflow and moves the full diagnostics CLI reference to `docs/diagnostics.md`.
### Upgrade Notes
- Add `.opencode/tui.json` if you want the native `/memory` TUI menu. Existing server-only configuration continues to work.
- Restart OpenCode after adding the TUI plugin config.
- The TUI menu is read-only and local-only. It does not call the LLM.
- Individual memory row selection is intentionally a no-op in this release; use the list for inspection and search.
### Validation
- `npm run typecheck``TYPECHECK_PASS`
- `npm test` — 421 tests passing, `TEST_PASS`
- `npm pack --dry-run`
- Real OpenCode TUI smoke test for `/memory` menu, searchable current memories, and transcript-free output.
---
## 1.6.0 (2026-05-08)
### Numbered Memory Refs
This release turns compaction from a one-way memory extractor into a memory maintenance loop. The model now sees numbered references for existing workspace memories and can explicitly reinforce a still-useful memory or propose a protected replacement when compaction reveals that old memory is obsolete.
The goal is not to make memory more aggressive. It is to make memory more accountable: old facts should be strengthened when they keep proving useful, replaced only when the target is safe, and diagnosable when the model tries something risky.
> **Good memory is selective memory.**
> v1.6 lets memory say “this still matters” without copying it again — and lets obsolete compaction memories fade behind a safer replacement trail.
```text
compaction summary
Memory candidates:
Memory ref snapshot id: <uuid>
[M1] decision · reinforced=2 · source=explicit
[M2] project · reinforced=0 · source=compaction
├─ REINFORCE [M1]
│ ↑ slows decay, no text mutation
└─ REPLACE [M2] project Updated durable fact
↑ only allowed for safe compaction targets
```
### What Changed
- **Numbered memory refs**: compaction prompts now render existing workspace memories as `[M1]`, `[M2]`, ... references so the model can target a known memory instead of restating it as a duplicate candidate.
- **REINFORCE commands**: `REINFORCE [M#]` increments the target memory's reinforcement count and updates its retention clock without changing its text.
- **Protected REPLACE commands**: `REPLACE [M#] [type] text` supersedes the old memory and appends a replacement only when the target is safe: active, compaction-sourced, and not already reinforced.
- **Reinforce + append workflow**: when a memory is mostly right but needs more context, compaction can reinforce the old memory and emit a new candidate for the new durable fact instead of mutating history.
- **Compaction prompt restructure**: verbose type definitions and the old “reuse existing wording exactly” instruction were replaced with shorter command rules, categorization guidance, and concrete memory-operation examples.
- **Hot state removed from compaction context**: active files, current errors, and pending session state remain in normal prompt context but are no longer duplicated inside the compaction prompt, saving budget and reducing accidental promotion of transient state.
- **New hard quality gates**: unresolved questions, transient bug/debug state, and deployment snapshots are rejected as durable memory candidates.
- **Soft terse-label diagnostic**: very short label-like candidates are reported for tuning without being hard-rejected in v1.6.
- **Decision cap raised**: rendered decision memories now have a per-type cap of 12 instead of 10, while the global rendered cap remains 28.
- **Overlap guard for compaction refs**: memory ref snapshots are tagged with a compaction ID when available, so overlapping same-session compactions cannot silently apply commands against the wrong numbered snapshot.
- **Safer evidence semantics**: rejected memory command events use a neutral `target` relation role instead of lifecycle roles such as `reinforced` or `superseded`.
### Why This Helps
- Useful memories can become stronger through real reuse instead of duplicate extraction.
- Obsolete compaction-sourced memories can be replaced with an explicit evidence trail rather than left to drift.
- Manual, explicit, and already-reinforced memories are protected from automatic replacement.
- Compaction prompt budget is spent on durable memory maintenance, not on duplicated hot session state.
- Command outcomes are visible enough to tune the feature after release instead of guessing from reinforcement counts alone.
### Diagnostics
Inspect command behavior with:
```bash
npx --package opencode-working-memory memory-diag commands
npx --package opencode-working-memory memory-diag commands --verbose
npx --package opencode-working-memory memory-diag commands --json
```
The command report includes:
- compactions with command evidence
- REINFORCE and REPLACE counts
- reinforced, superseded, rejected, and blocked outcomes
- invalid or malformed command counts
- same-type vs cross-type replacements
- protected REPLACE blocks, split by reinforced target and protected source
- latest command events in verbose mode
If a numbered replacement needs manual recovery, use the dry-run-first revert command:
```bash
npx --package opencode-working-memory memory-diag revert --memory <replacement-memory-id>
npx --package opencode-working-memory memory-diag revert --memory <replacement-memory-id> --apply
```
You can also target a replacement evidence event directly:
```bash
npx --package opencode-working-memory memory-diag revert --event <event-id>
```
### Safety Model
- REINFORCE never edits memory text.
- REPLACE is rejected for manual or explicit memories.
- REPLACE is rejected for already reinforced targets.
- REPLACE is rejected if the numbered ref no longer matches the current memory ID, status, and exact key.
- If a compaction snapshot ID is present and mismatched, all numbered commands from that summary are rejected with `missing_memory_ref_snapshot`.
- If the model omits the snapshot ID, v1.6 falls back to exact memory ref validation for compatibility and command effectiveness.
### Upgrade Notes
- No configuration changes required.
- Existing workspace memory files remain compatible.
- Existing session state remains compatible; old sessions without compaction ref snapshots fall back safely.
- Existing evidence logs remain compatible; new command events are appended only after v1.6 runs.
- `memory-diag` now exposes two additional public commands: `commands` and `revert`.
### Validation
- `npm run typecheck``TYPECHECK_PASS`
- `npm test` — 405 tests passing, `TEST_PASS`
---
## 1.5.5 (2026-05-05)
### Hot State Rendering Health
This release replaces blunt prompt truncation with a section-aware greedy renderer that fits whole lines and suppresses empty sections, plus render accounting types for future diagnostics.
> **Good rendering is selective too.** If a section doesn't fit, omit it — don't cut it in half.
### What Changed
- **Whole-line rendering**: the hot session state prompt no longer truncates mid-line with `.slice(0, maxRenderedChars)`. A greedy line accumulator fits complete lines and stops when the next line would exceed the 700-char budget.
- **Header-only sections suppressed**: a section heading is only rendered if at least one entry fits alongside it. No more orphaned `active_files:` headers with no content underneath.
- **Render accounting**: `accountHotSessionStateRender()` now returns `prompt`, `omitted[]`, and `maxRenderedChars` so future v2 diagnostics can surface which items fell out and why.
- **Two-layer omission model**: section caps (`section_cap`) trim per-type overflow first, then the char budget (`char_budget`) trims anything that doesn't fit. Each omitted item carries its reason and section.
- **Backward-compatible wrapper**: `renderHotSessionState()` delegates to the accounting function and returns just the prompt string, preserving existing plugin behavior.
### Docs Fixes
- **Active-file ranking formula corrected**: `docs/configuration.md` now matches the actual implementation — `ACTION_WEIGHT[action] + count * 3` with weights edit 50, write 45, grep 30, read 20, tie-break by `lastSeen` descending. The old docs claimed `count * action_weight * recency_decay` with weights 4/3/2/1.
- **Injection order clarified**: README now explains that workspace memory and hot state system-prompt positions depend on whether workspace memory state is available.
### Not Included Yet
- Evidence events for omitted items are deferred to v2, pending a `memory-diag` consumer to avoid unused storage litter.
### Upgrade Notes
- No configuration changes required.
- Existing workspace memory files remain compatible.
- Hot session state rendering behavior is intentionally different (cleaner output, no mid-line cuts), but the same information is presented when it fits within the budget.
### Validation
- `npm run typecheck``TYPECHECK_PASS`
- `npm test` — 356 tests passing, `TEST_PASS`
---
## 1.5.4 (2026-05-02)
### Memory Diagnostics Surface Cleanup
This cleanup release keeps the newly published `memory-diag` CLI small before legacy spellings become compatibility debt.
### What Changed
- **Official commands only**: the public CLI surface is `status`, `rejected`, `missing`, and `explain`.
- **Pre-public aliases removed**: old spellings such as `health`, `quality`, `rejections`, `disappearances`, and `trace` are no longer recognized.
- **Maintainer diagnostics clarified**: hidden `coverage` and `audit` commands remain available as maintainer-only diagnostics and stay out of public usage output.
- **Cleaner internals**: current command metadata now has one source of truth, and legacy command/formatter wrappers were removed.
- **Internal cleanup script hidden**: the workspace cleanup helper is no longer exposed as an npm script; the underlying development tool remains in `scripts/dev/`.
### Validation
- `npm run typecheck``TYPECHECK_PASS`
- `npm test` — 347 tests passing, `TEST_PASS`
---
## 1.5.3 (2026-05-02)
### Published Memory Diagnostics CLI
This release makes the read-only workspace memory diagnostics CLI available as the package binary `memory-diag`, with user-facing commands for checking memory health and understanding why memories are rejected, missing, shown, or hidden.
> Good memory is selective memory — and diagnostics should explain the selection.
### What Changed
- **Published CLI bin**: run diagnostics with `npx --package opencode-working-memory memory-diag` or `memory-diag status`.
- **User-facing commands**: `status`, `rejected`, `missing`, and `explain <memory-id>` are documented as the supported public workflow.
- **Legacy compatibility**: existing maintainer and legacy commands remain accepted with deprecation notices where applicable.
- **Cleaner CLI architecture**: the former monolithic diagnostics script is now split into focused command, model, formatter, and utility modules.
- **Faster diagnostics**: evidence grouping avoids repeated per-memory evidence queries in snapshot diagnostics.
- **Cleaner failures**: top-level CLI error handling now reports usage and unexpected command errors without noisy stack traces.
- **Docs alignment**: README and configuration docs now use package-qualified `npx` commands to avoid resolving an unrelated package named `memory-diag`.
### Requirements
- Node.js `>=22.6.0` is now required because the published diagnostics binary runs TypeScript through Node's `--experimental-strip-types` support.
### Validation
- `npm run typecheck``TYPECHECK_PASS`
- `npm test` — 358 tests passing, `TEST_PASS`
---
## 1.5.1 (2026-04-30)
### Evidence Loop and Explainability
This release adds an evidence-based audit trail for memory lifecycle events and user-facing diagnostics for understanding why memories are rendered, promoted, or rejected.
> **Evidence before sublimation.** Every memory decision can be traced.
### What Changed
- **Evidence log**: extraction, promotion, reinforcement, render, and storage events are now recorded in a per-workspace `events.jsonl` with 90-day retention and 5000-event cap.
- **User explainability**: `memory-diag explain` shows per-memory render status with strength, reasons, and evidence. `memory-diag trace --memory <id>` shows the full lifecycle history.
- **Machine-readable diagnostics**: `memory-diag health --json` outputs structured `MemoryDiagJSON` for scripting.
- **Calendar-day reinforcement gate**: reinforcement now requires distinct UTC calendar days, preventing repetitive-task gaming that could inflate a memory's strength within a single day.
- **SafetyCritical deprecation complete**: the `safetyCritical` field no longer affects retention strength or type-cap bypass. All memories fade by the same rules.
- **Retention module extraction**: retention constants and calculations moved to `src/retention.ts` for cleaner separation.
### Privacy
- Evidence text previews are credential-redacted. Memory content is stored as truncated hashes, never in full.
- Diagnostics default to redacted output. `--raw` is available for maintainers.
### Upgrade Notes
- No configuration changes required.
- Existing workspace memory files remain compatible.
- Evidence logs are created automatically; no migration needed.
### Validation
- `npm run typecheck`
- `npm test` — 271 tests passing
---
## 1.5.0 (2026-04-29)
### Retention Decay Model
This release changes workspace memory retention from hard stale pruning and additive priority scoring to a strength-based decay model.
Think of it like a forgetting curve: memories fade over time, but important, reinforced, and safety-critical memories decay slower. Weak entries fall out of rendered prompt context by cap competition, not hard deletion.
Think of it like a forgetting curve: memories fade over time, but important and reinforced memories decay slower. Weak entries fall out of rendered prompt context by cap competition, not hard deletion.
> **Memory should fade, so the agent can keep learning.**
> Important memories decay slower, but every memory must leave room for newer project reality and avoid long-term memory pollution.
@@ -27,10 +451,10 @@ Think of it like a forgetting curve: memories fade over time, but important, rei
### What Changed
- **Strength-based retention**: workspace memory now uses exponential decay: initial strength × age decay.
- **Better initial strength**: type, source, user importance, and safety-critical status now determine how strong a memory starts.
- **Better initial strength**: type, source, and user importance now determine how strong a memory starts.
- **No confidence scoring**: confidence remains in stored data for compatibility, but it no longer affects retention ranking.
- **Type caps**: rendered workspace memory now caps feedback, decisions, project facts, and references separately so one type cannot monopolize all 28 slots.
- **Safety-critical protection**: safety-critical entries get stronger retention and are exempt from per-type caps, while still competing under the global rendered cap.
- **Deprecation:** `safetyCritical` field no longer affects retention strength or type-cap bypass. All system memories now fade according to the same rules. Safety rules belong in user-controlled `agent.md` files, not in system memory.
- **Dormant-aware age**: after 14 inactive days, additional dormant workspace time counts at 0.25x so paused projects do not forget too aggressively.
- **Reinforcement**: repeated matching memories reinforce the survivor and slow future decay, with same-session and one-hour guards to avoid accidental spam.
- **No hard stale pruning**: old or stale-marked memories are no longer automatically dropped by age; they lose rendered space only through cap competition.
@@ -96,7 +520,7 @@ Retention monitoring:
### Validation
- `npm run typecheck`
- `npm test` — 237 tests passing
- `npm test` — 242 tests passing
- `bun scripts/memory-diag.ts health`
---
+35 -3
View File
@@ -55,6 +55,30 @@ Long-term memory that persists across sessions within the same workspace. Perfec
}
```
### Evidence Log Schema
Workspace diagnostics also read the append-only evidence log for the current workspace. New evidence records are additive and keep historical records valid:
```typescript
{
version: 1,
eventId: string,
createdAt: string,
workspaceKey: string,
workspaceRootHash: string,
producerName?: string,
producerVersion?: string,
instrumentationVersion?: number,
type: EvidenceEventType,
phase: EvidencePhase,
outcome: EvidenceOutcome,
reasonCodes: string[],
details?: Record<string, string | number | boolean | null | string[] | number[]>
}
```
Instrumentation version 2 added optional causal block details for diagnostics without backfilling old JSONL records. Instrumentation version 3 adds elapsed-window reinforcement details such as `details.elapsedMs`, `details.requiredElapsedMs`, `details.sameSession`, `details.reinforcementMode`, and `details.legacyMissingTimestamp`. Historical reinforcement-block events may still include older `details.blockReason` values such as `same_session`, `same_utc_day`, `min_interval`, `max_count`, or may have missing block details. Capacity-removal events may include `details.strengthAtRemoval`, `details.rankAtRemoval`, `details.typeRankAtRemoval`, and `details.ageDaysAtRemoval`. `memory-diag quality` treats missing producer/instrumentation fields as historical or ambiguous rather than proof of current behavior.
### Entry Types
| Type | Purpose | Example |
@@ -109,14 +133,14 @@ Retention then decides which active memories are rendered into prompt context. I
strength = initialStrength * 2 ** (-effectiveAgeDays / effectiveHalfLifeDays)
```
Initial strength is based on memory type, source, optional user importance, and safety-critical status. Confidence remains stored for compatibility but is not part of retention scoring.
Initial strength is based on memory type, source, and optional user importance. Confidence remains stored for compatibility but is not part of retention scoring.
Rendered candidates are selected in this order:
1. Exclude `status: "superseded"` entries.
2. Compute current retention strength.
3. Sort by strength descending.
4. Apply per-type caps, with safety-critical entries exempt from type caps.
4. Apply per-type caps.
5. Keep the top 28 rendered entries under the workspace memory character budget.
Default type caps:
@@ -130,7 +154,11 @@ Default type caps:
The type-cap total is 34, intentionally above the global 28-entry cap. These are maximums, not quotas.
Dormant workspaces age more slowly: after 14 inactive days, additional dormant time counts at 0.25x for retention decay. Repeated duplicate memories reinforce the surviving entry and slow future decay, but same-session and under-one-hour repeats do not stack reinforcement.
Dormant workspaces age more slowly: after 14 inactive days, additional dormant time counts at 0.25x for retention decay. Repeated duplicate memories reinforce the surviving entry only after a rolling 7-day elapsed window. Below reinforcement count 6, an allowed recurrence increments the reinforcement count and refreshes retention timestamps; at count 6 or higher, an allowed recurrence refreshes retention timestamps without increasing the count. Same-session status is recorded as diagnostic evidence, not as a new-policy block reason.
### Safety-Critical Deprecation
The `safetyCritical` field on `LongTermMemoryEntry` is deprecated as of the retention v1.5.1 model update. It no longer affects retention strength or type-cap bypass. The field is preserved in the type definition for backward compatibility with existing workspace memory JSON files, but has no active behavior. Safety rules should be maintained in user-controlled files such as `agent.md` rather than in system memory.
### System Prompt Injection
@@ -343,6 +371,10 @@ canonical("Use npm cache for plugins") === "use npm cache for plugins"
const workspaceKey = sha256(realpath(workspaceRoot)).slice(0, 16)
```
### Storage Safety
All read-modify-write updates go through `updateJSON()`, which combines an in-process promise queue with an on-disk `.lock` file. The lock file uses exclusive creation, heartbeat refreshes while held, stale-lock recovery after 30 seconds, and a 5 second wait timeout for live contention. Direct `readJSON()` is fallback-oriented and does not mutate data except when corrupt JSON is quarantined.
## Performance Considerations
### Memory Budgets
+36 -11
View File
@@ -34,7 +34,7 @@ const WORKSPACE_DORMANT_AFTER_DAYS = 14;
const DORMANT_DECAY_MULTIPLIER = 0.25;
```
Initial strength uses type, source, user importance, and safety-critical factors. Confidence is stored for compatibility but is not used for retention scoring.
Initial strength uses type, source, and user importance factors. Confidence is stored for compatibility but is not used for retention scoring.
Rendered type caps prevent one type from filling all workspace memory slots:
@@ -45,7 +45,7 @@ Rendered type caps prevent one type from filling all workspace memory slots:
| `project` | 8 |
| `reference` | 6 |
Safety-critical memories are exempt from type caps but still compete under the global `maxEntries` limit. Old or stale-marked memories are not hard-pruned by age; they lose rendered space through strength and cap competition.
Old or stale-marked memories are not hard-pruned by age; they lose rendered space through strength and cap competition. The deprecated `safetyCritical` field is preserved for compatibility but no longer affects strength or type caps.
## Hot Session State Limits
@@ -87,16 +87,33 @@ const HOT_STATE_LIMITS = {
## Active File Scoring
Files are ranked by action type:
Files are ranked by action type plus repeated access count:
| Action | Weight | Description |
|--------|--------|-------------|
| `write` | 4 | File created/overwritten |
| `edit` | 3 | File modified |
| `read` | 2 | File read |
| `grep` | 1 | Grep searched in file |
| `edit` | 50 | File modified |
| `write` | 45 | File created/overwritten |
| `grep` | 30 | Grep searched in file |
| `read` | 20 | File read |
Score formula: `count * action_weight * recency_decay`
Score formula: `ACTION_WEIGHT[action] + count * 3`
When scores tie, the most recent `lastSeen` timestamp sorts first. There is no recency decay factor in the score itself.
## Hot Session State Truncation
Hot state rendering applies section caps before the character budget:
| Section | Rendered cap |
|---------|--------------|
| `active_files` | 8 |
| `open_errors` | 3 |
| `recent_decisions` | 8 |
| `pending_memories` | 6 |
After section caps, the renderer applies the 700-character budget with whole-line inclusion only. The prefix line is counted against the budget, and a section heading is emitted only when at least one entry from that section fits; header-only sections are suppressed.
`accountHotSessionStateRender()` returns prompt and omission accounting for future diagnostics. Evidence event integration for hot-state render omissions is planned for v2.
## Error Categories
@@ -204,13 +221,21 @@ cat ~/.local/share/opencode-working-memory/workspaces/*/sessions/*.json | jq
### Inspect Retention Health
From a source checkout, maintainers can inspect stored vs rendered memory behavior:
Use the diagnostics CLI to check memory health for the current workspace:
```bash
bun scripts/memory-diag.ts health
npx --package opencode-working-memory memory-diag status
# or from a source checkout:
npm run diag -- status
```
The health output includes stored active memories, rendered candidates, type caps, global cap overflow, dormancy status, retention monitoring alerts, and strength-ranked top/weakest entries.
For detailed diagnostics, use `--verbose`:
```bash
npx --package opencode-working-memory memory-diag status --verbose
```
The status output includes overall health (OK/WARNING/DEGRADED), key metrics, and suggested next steps when attention is needed.
### Clear Workspace Memory
+100
View File
@@ -0,0 +1,100 @@
# Memory Diagnostics
Use the read-only diagnostics CLI when you want to understand what OpenCode Working Memory is doing for the current workspace.
The npm package is `opencode-working-memory`; the installed bin is `memory-diag`, so package-qualified `npx` avoids resolving a different package named `memory-diag`.
## Commands
| Question | Command |
|---|---|
| Is memory healthy? | `npx --package opencode-working-memory memory-diag` or `npx --package opencode-working-memory memory-diag status` |
| Why was something rejected? | `npx --package opencode-working-memory memory-diag rejected` |
| Where did my memory go? | `npx --package opencode-working-memory memory-diag missing` |
| Why is this memory shown or hidden? | `npx --package opencode-working-memory memory-diag explain <memory-id>` |
| How are numbered memory commands behaving? | `npx --package opencode-working-memory memory-diag commands` |
| What reinforcement evidence exists for one memory? | `npx --package opencode-working-memory memory-diag commands --memory <memory-id>` |
| How do I review memory quality without automatic cleanup? | `npx --package opencode-working-memory memory-diag quality` |
| Revert a numbered replacement? | `npx --package opencode-working-memory memory-diag revert --memory <replacement-memory-id>` |
## Global Options
- `--workspace <path>` — inspect another workspace; defaults to the current directory.
- `--verbose` — show detailed diagnostics.
- `--json` — print machine-readable output where supported.
## Diagnostic Answerability Contract
Every diagnostic section must document:
1. **Question:** What does the reviewer want to know?
2. **Decision:** What action could the answer inform?
3. **Competing explanations:** At least two interpretations of the same metric.
4. **Required signals:** What fields/events distinguish those explanations?
5. **Current signals:** What currently exists?
6. **Answerability level:** `supported` | `partial` | `inventory_only` | `not_instrumented`
7. **Output permission:** What the tool may say without overclaiming.
For `memory-diag quality`:
- `reinforcementRules`: `inventory_only` (cannot distinguish spam from legitimate blocks)
- `evictionAndCaps`: `inventory_only` (cannot distinguish healthy turnover from premature eviction)
- Old evidence remains ambiguous. Answerability improves for producer-instrumented events, including instrumentation version 2 block details and instrumentation version 3 elapsed-window details. Mixed old/new logs will show a mix of `inventory_only` and `partial` sections.
- Producer-instrumented reinforcement blocks can upgrade `reinforcementRules` to `partial` by showing exact block reasons and, when available, rolling elapsed-window fields; they still require human content judgment.
- Producer-instrumented capacity removals with rank/strength snapshots can upgrade `evictionAndCaps` to `partial`; fullness alone remains occupancy inventory, not proof of a capacity problem.
## Examples
```bash
npx --package opencode-working-memory memory-diag status
npx --package opencode-working-memory memory-diag rejected --verbose
npx --package opencode-working-memory memory-diag missing --workspace /path/to/project
npx --package opencode-working-memory memory-diag status --json
npx --package opencode-working-memory memory-diag commands --verbose
npx --package opencode-working-memory memory-diag commands --memory <memory-id>
npx --package opencode-working-memory memory-diag quality
npx --package opencode-working-memory memory-diag revert --memory <replacement-memory-id>
```
## Quality Review Board
Use `memory-diag quality` for a read-only, answerability-scoped evidence inventory without automatic cleanup.
- Primarily provides memory-system mechanism observations for human/agent interpretation.
- Secondarily helps review active memory content quality.
- Prints answerability labels and output permissions so inventory facts are not presented as conclusions.
- Separates system-mechanism facts, memory-content facts, heuristic flags, and review questions.
- Includes inferred evidence provenance because historical records do not record producer package version.
- Labels uncertain provenance as `unversioned_ambiguous` so old artifacts are not treated as current mechanism failures.
- Does not decide what to delete or mutate.
- Use `--json` for agent/objective review.
## Numbered Memory Command Reports
Use `memory-diag commands` to inspect `REINFORCE [M#]` and `REPLACE [M#]` outcomes from compaction.
```bash
npx --package opencode-working-memory memory-diag commands
npx --package opencode-working-memory memory-diag commands --verbose
npx --package opencode-working-memory memory-diag commands --memory <memory-id>
```
The report includes successful reinforcements, refresh-only reinforcements, successful replacements, malformed commands, stale refs, protected replacement blocks, and latest command events in verbose mode.
Use `commands --memory <memory-id>` when you need a focused, evidence-only reinforcement view for one memory. It reports current memory status separately from recorded reinforcement attempts, block reasons, missing block details, elapsed-window fields (`elapsedMs`, `requiredElapsedMs`), `sameSession` evidence, `reinforcementMode` (`increment` or `refresh_only`), `legacyMissingTimestamp` when true, and historical UTC-day evidence without judging whether the policy is correct.
Current reinforcement policy uses a rolling 7-day elapsed window. Below reinforcement count 6, allowed attempts increment the count and refresh retention timestamps; at count 6 or higher, allowed attempts refresh retention timestamps without increasing the count. Historical evidence can still show older block reasons such as `same_session`, `same_utc_day`, `min_interval`, `max_count`, or missing block details because evidence logs are append-only and are not backfilled.
## Dry-run Recovery
`memory-diag revert` is dry-run by default. Add `--apply` only after reviewing the planned original/replacement status changes.
```bash
npx --package opencode-working-memory memory-diag revert --memory <replacement-memory-id>
npx --package opencode-working-memory memory-diag revert --memory <replacement-memory-id> --apply
```
You can also target a replacement evidence event directly:
```bash
npx --package opencode-working-memory memory-diag revert --event <event-id>
```
@@ -0,0 +1,491 @@
# Native TUI Memory Command UX Implementation Plan
> **For agentic workers:** Use `agenthub-writing-plans-skill` to create this plan and `agenthub-executing-plans-skill` to execute it task-by-task. Steps use checkbox (`- [ ]`) syntax. Wave checkpoints are gates.
**Goal:** Replace the current ambiguous native OpenCode TUI memory slash-command surface with three visibly distinct hyphenated commands before commit/push.
**User outcome:** OpenCode users see only `/memory-status`, `/memory-list`, and `/memory-help` in slash autocomplete; status shows memory statistics, list shows current active workspace memories as display-local `[M1]` refs, and duplicate recent-activity commands are no longer user-facing.
**Architecture:** Keep the existing TUI plugin and no-reply session-message injection path. Change only the command registration/routing layer (`src/tui-plugin.ts`) and the local read/format core (`src/memory-visibility.ts`), then update focused tests and docs. Do not add storage, background jobs, LLM calls, command mutation, or a parallel UI surface.
**Tech stack:** TypeScript ESM on Node >=22.6, OpenCode TUI plugin API, local JSON stores, Node built-in test runner with `--experimental-strip-types`.
**Scope mode:** COMPLETE for the approved UX correction; no implementation code is changed by this plan.
---
## Scope Challenge
- Existing leverage: Reuse `src/tui-plugin.ts` command registration and `api.client.session.prompt({ noReply: true })`; reuse `src/memory-visibility.ts` local read snapshots, redaction helper, and `accountWorkspaceMemoryRender()`/`accountWorkspaceMemoryCompactionRefs()` accounting instead of creating a separate diagnostic subsystem.
- Minimum complete change: Register three unique top-level slash names, add/route a list formatter, remove visible activity/last commands, make status stats-only, and update README/CHANGELOG/tests to match the new public surface.
- Scope smell check: Expected code/docs/test touch set is 6 files: `src/tui-plugin.ts`, `src/memory-visibility.ts`, `tests/tui-plugin.test.ts`, `tests/memory-visibility.test.ts`, `README.md`, and `CHANGELOG.md`. `RELEASE_NOTES.md`, `docs/installation.md`, and `docs/configuration.md` do not currently mention the TUI memory commands and should stay unchanged unless implementation reveals new command mentions.
- Lake vs ocean: The lake is display-only status/list/help for existing local data. Ocean-sized extras remain out of scope: `/memory delete`, `/memory edit`, stable memory IDs in the TUI, interactive list selection, evidence activity dashboards, assistant-style output APIs, and upstream OpenCode TUI changes.
- Out of scope: No activity/last user-facing commands, no `/memory` space-subcommand autocomplete entries, no new aliases unless OpenCode proves they do not create duplicate menu rows, no persistence schema changes, no LLM/API calls, and no server plugin behavior changes.
## Search and Prior Art
- Layer 1 choices:
- `src/tui-plugin.ts:113-148` currently registers four commands with the same `slash.name: "memory"`, causing OpenCode to show multiple identical `/memory` rows.
- `src/tui-plugin.ts:58-63` maps internal values `memory.activity` and `memory.last` to the same `"activity"` command, confirming duplication.
- `src/memory-visibility.ts:12` currently exposes `MemoryVisibilityCommand = "status" | "activity" | "help"`; `formatMemoryHelp()` at lines 273-288 documents `/memory activity` and `/memory last`.
- `src/memory-visibility.ts:213-238` already formats status counts but also includes preview lines; the approved UX wants status focused on statistics and delegates memory content to `/memory-list`.
- `src/workspace-memory.ts:937-997` already has numbered ref accounting (`accountWorkspaceMemoryCompactionRefs`) that returns rendered entries, omitted entries, and `refs` with `M1`, `M2`, ... display labels. This is the best existing source for list refs because it respects the same selection/cap logic as compaction refs.
- `tests/tui-plugin.test.ts:98-153` covers TUI registration, no-reply injection, routing, no-session warning, dialog clearing, and injection failure.
- `tests/memory-visibility.test.ts:53-195` covers status/activity/help formatting and read-only redaction behavior.
- `README.md:64-76` and `CHANGELOG.md:8-25` document the current `/memory` status/activity/help UX and must be updated before commit/push.
- Layer 2 choices: None required. Do not add dependencies.
- Layer 3 choices: A small `MemoryListModel`/`formatMemoryList()` in `src/memory-visibility.ts` is justified because the TUI needs a user-facing grouped list shape that differs from compaction prompt text.
- Eureka findings: OpenCode's current slash menu does not visibly distinguish trailing subcommand text, so the technically elegant `/memory status` model is worse UX than three hyphenated top-level commands for this release.
## Architecture
### Components and responsibilities
- `src/tui-plugin.ts`: Owns visible TUI command names and active-session/no-reply injection. It should register exactly three commands with `slash.name` values `memory-status`, `memory-list`, and `memory-help`; internal `value` strings may remain dot-form (`memory.status`, `memory.list`, `memory.help`) because they are not displayed to users.
- `src/memory-visibility.ts`: Owns read-only local models and markdown/plain-text formatting. It should expose command variants `status`, `list`, and `help`; status should report stats only; list should show active rendered workspace memories with display-local `[M#]` refs; help should list only the three public commands. `MemoryVisibilityCommand` is currently consumed by `src/tui-plugin.ts`; verify no other consumers exist before changing/removing exported command variants.
- `src/workspace-memory.ts`: No planned edit. Reuse `accountWorkspaceMemoryRender()` for stats and `accountWorkspaceMemoryCompactionRefs()` for capped/ref-capable list selection. If imports need updating, import only existing exported functions.
- `tests/tui-plugin.test.ts`: Assert unique slash names and removal of user-facing activity/last registrations.
- `tests/memory-visibility.test.ts`: Assert status/list/help output contracts, redaction/truncation, caps, and fallback routing.
- `README.md` and `CHANGELOG.md`: Align public docs with the new three-command UX. `RELEASE_NOTES.md` has no 1.6.1 TUI command section today; leave it unchanged unless a later release-notes pass adds one.
### Data flow
```text
User selects /memory-status, /memory-list, or /memory-help in OpenCode TUI
-> src/tui-plugin.ts registered command onSelect
-> determine active sessionID from api.route/current session route
-> commandFromValue(value) returns "status" | "list" | "help"
-> src/memory-visibility.ts renderMemoryCommand(root, sessionID, command)
status: read workspace/session/pending snapshots + render accounting counts
list: read workspace snapshot + accountWorkspaceMemoryCompactionRefs() + safePreview()
help: static command help
-> api.client.session.prompt({ sessionID, noReply: true, parts: [{ type: "text", text }] })
-> OpenCode renders the report as local no-reply session text; no LLM call is made
```
### Output contracts
#### `/memory-status`
Required shape:
```md
## Memory status
Workspace:
- Active memories: <n>
- Rendered in prompt: <n>
- Omitted active memories: <n>
- Superseded memories: <n>
Pending:
- Pending in this session: <n>
- Pending journal memories: <n>
Session:
- Open errors: <n>
- Recent decisions: <n>
Use /memory-list to view current [M1]-[M28] memory refs.
Local only: no LLM request was made.
```
Notes:
- Remove preview lines from status.
- Keep zero/empty counts visible.
- Keep the local-only footer.
#### `/memory-list`
Required shape:
```md
## Current workspace memories
Display refs are local to this output and may change after memory updates.
feedback:
- [M1] <redacted/truncated text>
project:
- [M2] <redacted/truncated text>
decision:
- [M3] <redacted/truncated text>
reference:
- [M4] <redacted/truncated text>
Shown: <rendered> of <active> active memories.
Omitted active memories: <omitted-active>.
Local only: no LLM request was made.
```
Notes:
- Use refs that are explicitly display-local, not stable IDs.
- Group by memory type/kind in the existing order: `feedback`, `project`, `decision`, `reference`.
- Show only active, non-superseded memories selected by the same caps/budget used for rendered memory refs. The default global cap is 28 (`src/types.ts` via `LONG_TERM_LIMITS.maxEntries`).
- Apply `safePreview()` or equivalent credential redaction/truncation to every displayed memory text. Do not dump raw JSON or full unbounded memory text.
- Empty state: `No active workspace memories are stored yet.` plus the local-only footer.
#### `/memory-help`
Required shape:
```md
## Memory help
Commands:
- /memory-status — show local memory statistics.
- /memory-list — show current workspace memories as display-local [M1]-[M28] refs.
- /memory-help — show this help.
These commands are read-only, local-only, and do not call the LLM.
```
Notes:
- Do not mention `/memory`, `/memory status`, `/memory activity`, or `/memory last` as available commands.
- It is acceptable to keep a short note that mutation commands such as delete/edit are not available, but do not expand scope.
### Error flow
- No active session route: preserve existing warning toast behavior and do not write a message.
- Local read/format error with a session: preserve existing `## Memory error` stream-visible report.
- `api.client.session.prompt()` failure: preserve existing error toast and no retry.
- Unknown internal command value: route to help. Do not register unknown/legacy values in the visible command list.
### Security and permissions
- Commands are read-only over local memory/session/pending files and write only the user-invoked no-reply session output.
- Display memory text only after redaction and truncation.
- Do not introduce shell execution, network calls, LLM calls, or file writes to memory stores.
- Do not treat display-local `[M#]` refs as authorization or stable identity; they are only labels in the printed list.
### Performance
- Status remains O(number of workspace/session/pending entries), using existing bounded stores.
- List should format at most the rendered/ref-selected memories and must respect existing caps/budgets; avoid full evidence lifecycle joins.
- Removing activity from the visible UX avoids querying/formatting evidence logs during normal command use.
### Production failure scenarios
- OpenCode still displays aliases or duplicate slash names unexpectedly: keep only three primary `slash.name` values and avoid aliases until verified.
- User expects `/memory` from an unreleased local build: because this is before commit/push, prefer clean UX over compatibility debt; docs should clearly advertise the three hyphenated commands.
- Very long memories or credentials appear in stored data: list formatter must redacted/truncate via `safePreview()` and tests should assert credential-like fixture text is absent.
- More than 28 active memories exist: list reports shown vs active and omitted count; it must not imply refs cover hidden memories.
## Backwards Compatibility Stance
- Treat the current `/memory` space-subcommand surface as pre-public/unshipped for this commit because it produces duplicate-looking menu entries in OpenCode.
- Remove visible registrations for `/memory`, `/memory status`, `/memory activity`, `/memory last`, and `/memory help`.
- Do not document old spellings.
- Internal fallback may continue routing unknown values to help, but do not preserve hidden legacy command entries if OpenCode would show them in autocomplete.
- If a reviewer requests aliases, add only after confirming aliases do not create extra duplicate menu rows; otherwise defer aliases to a later OpenCode API capability discussion.
## File Plan
- Modify: `src/tui-plugin.ts:58-63` — route `memory.status`, `memory.list`, and `memory.help`; remove activity/last mapping from the public path.
- Modify: `src/tui-plugin.ts:113-148` — register exactly three commands with `slash.name` values `memory-status`, `memory-list`, `memory-help`; remove `Memory activity` and `Memory last` command objects.
- Modify: `src/memory-visibility.ts:12-40` — change command/model types from status/activity/help to status/list/help; add `MemoryListModel`; remove or unexport activity-only types/functions if no longer used.
- Modify: `src/memory-visibility.ts:190-238` — keep stats model but format status as grouped statistics with no previews.
- Modify: `src/memory-visibility.ts:240-271` — replace activity reader/formatter with list reader/formatter or remove activity code and add list code nearby.
- Modify: `src/memory-visibility.ts:273-288` — update help to list only `/memory-status`, `/memory-list`, `/memory-help`.
- Modify: `src/memory-visibility.ts:291-302` — route `"list"` to the new list formatter and remove `"activity"` routing.
- Modify: `tests/tui-plugin.test.ts` — assert three unique visible commands and route status/list/help.
- Modify: `tests/memory-visibility.test.ts` — replace activity tests with list tests; update status/help assertions.
- Modify: `README.md:33,51-76` — update feature copy and Native TUI command docs.
- Modify: `CHANGELOG.md:8-25` — amend unreleased/current 1.6.1 entry from status/activity/help to status/list/help and note hyphenated names.
- No planned change: `RELEASE_NOTES.md` — no 1.6.1 TUI command mention exists in current evidence.
- No planned change: `docs/installation.md`, `docs/configuration.md` — current grep found no TUI command mentions.
## Test Strategy
- Framework: Node built-in test runner via `npm test`; TypeScript via `npm run typecheck`.
- Unit coverage:
- `memory-visibility.ts` status counts with active/superseded/rendered/omitted entries, pending memories, pending journal entries, open errors, and recent decisions; assert no preview section remains.
- `memory-visibility.ts` list output with active memories grouped by type, display-local `[M#]` labels, shown/active/omitted summary, redacted credential-like text, empty state, and local-only footer.
- `memory-visibility.ts` help text lists only three hyphenated commands and omits `/memory activity` and `/memory last`.
- `renderMemoryCommand()` routes `status`, `list`, and `help`; unknown values fall back to help.
- TUI integration-style unit coverage:
- Registers exactly three command values.
- Slash names are exactly `memory-status`, `memory-list`, `memory-help` and unique.
- No registered command value is `memory.activity` or `memory.last`.
- Selecting `memory.status`, `memory.list`, and `memory.help` injects no-reply text with the expected heading.
- Existing no-session warning, dialog clearing, and prompt-injection failure behavior still passes.
- Docs verification:
- Grep for old public spellings in markdown and source tests after implementation; old spellings should remain only in this plan or intentionally in negative assertions.
## Implementation Tasks
## Wave 1: Failing Tests for the New Public Contract
### Task 1.1: Update TUI command registration/routing tests first
**Purpose:** Prove the slash command menu no longer contains duplicate-looking `/memory` entries.
**Files:**
- Modify: `tests/tui-plugin.test.ts`
**Behavior:**
- Given the TUI plugin registers commands, there are exactly three memory commands.
- Given autocomplete displays slash names, the names are unique hyphenated top-level commands.
- Given a command is selected, status/list/help route to distinct headings.
- [ ] **Step 1: Write failing tests**
Add or update assertions equivalent to:
```ts
test("registers three unique hyphenated memory slash commands", async () => {
const api = makeMockTuiApi({ route: { name: "session", params: { sessionID: "ses_1" } } });
await MemoryTuiPlugin(api as any, undefined, mockMeta);
const slashNames = api.commands.map(command => command.slash?.name).filter(Boolean);
assert.deepEqual(slashNames, ["memory-status", "memory-list", "memory-help"]);
assert.equal(new Set(slashNames).size, slashNames.length);
assert.deepEqual(api.commands.map(command => command.value), ["memory.status", "memory.list", "memory.help"]);
assert.equal(api.commands.some(command => command.value === "memory.activity"), false);
assert.equal(api.commands.some(command => command.value === "memory.last"), false);
});
```
Update the routing test to select `memory.list` and expect `## Current workspace memories`.
- [ ] **Step 2: Run expected failure**
Run: `node --import ./tests/setup-xdg-data-home.ts --test --experimental-strip-types tests/tui-plugin.test.ts`
Expected: FAIL because current `src/tui-plugin.ts` registers repeated `slash.name: "memory"` and does not register `memory.list`.
### Task 1.2: Update memory visibility formatter tests first
**Purpose:** Lock the approved status/list/help output shape before implementation.
**Files:**
- Modify: `tests/memory-visibility.test.ts`
**Behavior:**
- Status is stats-only and points to `/memory-list`.
- List prints current active memories grouped by type with display-local refs and redaction.
- Help lists only three hyphenated commands.
- [ ] **Step 1: Write failing tests**
Required assertions:
```ts
assert.match(output, /^## Memory status/);
assert.match(output, /Workspace:/);
assert.match(output, /Pending:/);
assert.match(output, /Session:/);
assert.match(output, /Use \/memory-list to view current \[M1\]-\[M28\] memory refs\./);
assert.equal(output.includes("Recent active memory previews"), false);
```
Replace activity tests with list tests that create at least one memory for each type and one superseded memory. The redaction fixture must include at least one short credential-like active memory that is guaranteed to render, such as `Remember password: sushi for the fake test.`, so `output.includes("sushi") === false` proves redaction rather than omission by caps/budget. Assert:
```ts
assert.match(output, /^## Current workspace memories/);
assert.match(output, /Display refs are local to this output/);
assert.match(output, /feedback:\n- \[M\d+\]/);
assert.match(output, /project:\n- \[M\d+\]/);
assert.match(output, /decision:\n- \[M\d+\]/);
assert.match(output, /reference:\n- \[M\d+\]/);
assert.match(output, /Shown: \d+ of \d+ active memories\./);
assert.equal(output.includes("sushi"), false);
assert.equal(output.includes("Superseded memory should not be active"), false);
```
Update help assertions:
```ts
assert.match(output, /\/memory-status/);
assert.match(output, /\/memory-list/);
assert.match(output, /\/memory-help/);
assert.equal(output.includes("/memory activity"), false);
assert.equal(output.includes("/memory last"), false);
```
- [ ] **Step 2: Run expected failure**
Run: `node --import ./tests/setup-xdg-data-home.ts --test --experimental-strip-types tests/memory-visibility.test.ts`
Expected: FAIL because current implementation still exposes activity/last and lacks list output.
### Wave 1 Checkpoint
- [ ] Confirm both focused test files fail for the expected missing behavior, not unrelated setup errors.
- [ ] Do not proceed if failures indicate fixture/storage regressions unrelated to command UX.
## Wave 2: Implement the Visibility Core
### Task 2.1: Add list model/formatter and simplify status/help
**Purpose:** Make the local rendering core match the approved command set independently of TUI registration.
**Files:**
- Modify: `src/memory-visibility.ts`
**Implementation instructions:**
- Change `MemoryVisibilityCommand` to `"status" | "list" | "help"`.
- Add a `MemoryListModel` that contains:
- `activeMemories: number`
- `renderedMemories: number`
- `omittedActiveMemories: number`
- `groups: Record<LongTermMemoryEntry["type"], Array<{ ref: string; text: string }>>` or equivalent typed structure preserving `feedback`, `project`, `decision`, `reference` order.
- Implement `getMemoryList(root: string)` using `readWorkspaceMemorySnapshot(root)` and `accountWorkspaceMemoryCompactionRefs(store)`.
- Count active memories from the raw/snapshot store by `status !== "superseded"`.
- Use accounting `refs` plus `rendered` entries to build display-local refs.
- Only use the `refs`, `rendered`, and `omitted` fields from `accountWorkspaceMemoryCompactionRefs()` for the list formatter; discard its `evidence` and `prompt` fields and do not call `appendEvidenceEvents()` from `/memory-list`.
- Display text must pass through `safePreview(ref.textPreview)`.
- `omittedActiveMemories` should count only `accounting.omitted` entries whose memory is not superseded; `accounting.omitted` can include superseded entries from selection accounting and those must not inflate active omissions.
- Implement `formatMemoryList(model)` with the required output contract.
- Update `formatMemoryStatus()` to remove preview output and use grouped stat sections.
- Update `formatMemoryHelp()` to list only `/memory-status`, `/memory-list`, `/memory-help`.
- Update `renderMemoryCommand()` switch to route `"list"`.
- Remove `MemoryActivityModel`, `DEFAULT_ACTIVITY_LIMIT`, `MAX_ACTIVITY_LIMIT`, `clampLimit`, `getMemoryActivity()`, `formatMemoryActivity()`, `formatActivityEvent()`, and `summarizeReasons()` if they become unused. Also remove unused `EvidenceEventV1`/`queryEvidenceEvents` imports.
- Before deleting activity-only exports/helpers, grep `src/` for `MemoryActivityModel`, `getMemoryActivity`, `formatMemoryActivity`, `formatActivityEvent`, and `summarizeReasons()`; remove them only after confirming there are no cross-module consumers outside `memory-visibility.ts`.
- [ ] **Step 1: Implement minimal code**
Do not modify `src/workspace-memory.ts` unless TypeScript proves an export is missing. Current evidence shows `accountWorkspaceMemoryCompactionRefs` is exported.
- [ ] **Step 2: Run focused verification**
Run: `node --import ./tests/setup-xdg-data-home.ts --test --experimental-strip-types tests/memory-visibility.test.ts`
Expected: PASS.
- [ ] **Step 3: Run typecheck for dead imports/types**
Run: `npm run typecheck`
Expected: PASS and output includes `TYPECHECK_PASS`.
### Wave 2 Checkpoint
- [ ] Status output contains no memory preview content.
- [ ] List output includes display-local `[M#]` refs, grouped by type, with redacted/truncated text.
- [ ] Activity/last formatter exports are either removed or no longer referenced by user-facing code.
## Wave 3: Implement the TUI Command Surface
### Task 3.1: Register only three hyphenated slash commands
**Purpose:** Fix OpenCode autocomplete by ensuring visible slash names are unique top-level commands.
**Files:**
- Modify: `src/tui-plugin.ts`
**Implementation instructions:**
- Update `commandFromValue(value)`:
- `memory.status` -> `"status"`
- `memory.list` -> `"list"`
- `memory.help` -> `"help"`
- default -> `"help"`
- Update `memoryCommands(api)` to return exactly three objects:
- title `Memory status`, value `memory.status`, description `Show working memory statistics in the current session.`, category `Memory`, suggested `true`, `slash: { name: "memory-status" }`
- title `Memory list`, value `memory.list`, description `Show current workspace memories with display-local refs.`, category `Memory`, `slash: { name: "memory-list" }`
- title `Memory help`, value `memory.help`, description `Show working memory help.`, category `Memory`, `slash: { name: "memory-help" }`
- Remove `Memory activity` and `Memory last` command objects.
- Do not include `aliases: ["mem"]` in this wave; aliases can be reconsidered only after verifying they do not create duplicate menu entries.
- [ ] **Step 1: Implement minimal code**
Keep existing active-session guard, no-reply injection, dialog clearing, and prompt failure toast logic unchanged.
- [ ] **Step 2: Run focused verification**
Run: `node --import ./tests/setup-xdg-data-home.ts --test --experimental-strip-types tests/tui-plugin.test.ts`
Expected: PASS.
- [ ] **Step 3: Run adjacent focused verification**
Run: `node --import ./tests/setup-xdg-data-home.ts --test --experimental-strip-types tests/tui-plugin.test.ts tests/memory-visibility.test.ts`
Expected: PASS.
### Wave 3 Checkpoint
- [ ] TUI registration tests prove slash names are unique.
- [ ] No test expects or selects `memory.activity` or `memory.last`.
- [ ] No implementation path requires OpenCode to render trailing subcommand text.
## Wave 4: Documentation and Release Metadata Alignment
### Task 4.1: Update user-facing docs
**Purpose:** Ensure install/usage docs no longer advertise broken or removed command spellings.
**Files:**
- Modify: `README.md`
- Modify: `CHANGELOG.md`
- Verify/no change unless needed: `RELEASE_NOTES.md`, `docs/installation.md`, `docs/configuration.md`
**Implementation instructions:**
- In `README.md` feature bullets, replace “status, recent activity, and help” with “status, current memory list, and help”.
- In the Native TUI Memory Command section, document:
- `/memory-status` — status counts/statistics
- `/memory-list` — current active workspace memories with display-local `[M1]` refs
- `/memory-help` — help
- Keep the existing local-only/no LLM/no-reply transcript caveat.
- Remove docs for `/memory`, `/memory status`, `/memory activity`, `/memory last`, and `/memory help` as available user commands.
- In `CHANGELOG.md` 1.6.1 entry, amend the current TUI command bullet to say hyphenated `/memory-status`, `/memory-list`, `/memory-help`, and note recent activity/last were removed before release because duplicate entries were not useful.
- `RELEASE_NOTES.md` currently has no 1.6.1 TUI command mention in the evidence read; do not add a release note unless the release process requires a 1.6.1 section.
- [ ] **Step 1: Update markdown docs**
Use exact command names consistently.
- [ ] **Step 2: Run docs/source grep**
Run equivalent local search:
```bash
rg "/memory activity|/memory last|/memory status|/memory help|slash: \{ name: \"memory\"|memory\.activity|memory\.last" README.md CHANGELOG.md src tests
```
Expected: no matches except this plan file if searching the whole repo, or negative test assertions that intentionally verify old commands are absent. The space-separated forms in this grep are obsolete spellings; the correct hyphenated commands `/memory-status`, `/memory-list`, and `/memory-help` should remain present.
### Wave 4 Checkpoint
- [ ] Docs advertise only `/memory-status`, `/memory-list`, `/memory-help`.
- [ ] Changelog matches the pre-release UX correction.
- [ ] No release docs mention stale activity/last commands.
## Final Verification
- [ ] Run: `npm run typecheck`
Expected: PASS and output includes `TYPECHECK_PASS`.
- [ ] Run: `npm test`
Expected: PASS and output includes `TEST_PASS`.
- [ ] Run: `npm pack --dry-run`
Expected: package contains `index.ts`, `src/tui-plugin.ts`, `src/memory-visibility.ts`, README, LICENSE, and no unexpected generated artifacts.
- [ ] Manual OpenCode TUI smoke before commit/push:
- Configure `.opencode/tui.json` to load the local plugin target.
- Open slash command menu and confirm exactly three visible memory commands: `/memory-status`, `/memory-list`, `/memory-help`.
- Select `/memory-status`; expected no-reply session text headed `## Memory status`, no assistant response, no LLM/provider activity.
- Select `/memory-list`; expected no-reply session text headed `## Current workspace memories`, display-local `[M#]` refs, grouped memory types, redacted/truncated text.
- Select `/memory-help`; expected help lists only the three hyphenated commands.
- [ ] Review changed files for placeholders, dead code, unused activity imports, debug logging, stale docs, raw secret output, and accidental storage writes.
## Review Readiness
- [ ] Scope challenge resolved: this is a focused UX correction, not a memory subsystem rewrite.
- [ ] Architecture and data flow are explicit.
- [ ] Every changed behavior has a focused test or manual TUI smoke check.
- [ ] Failure paths and user-visible states are covered.
- [ ] Commands are exact and runnable.
- [ ] Backwards compatibility stance is explicit and pre-release-safe.
- [ ] Plan has no placeholders.
## Risks and Mitigations
- Risk: Hyphenated names are less elegant than `/memory status` subcommands. Mitigation: current OpenCode menu behavior makes hyphenated top-level names the only visible unambiguous option.
- Risk: Users may interpret `[M#]` as stable memory IDs. Mitigation: list output must explicitly say refs are display-local and may change after memory updates.
- Risk: Activity formatter code may be left as unused dead code. Mitigation: typecheck plus source grep should catch unused imports/references; remove activity-only exports unless a maintainer-only consumer is introduced later.
- Risk: List output may leak long or sensitive memory text. Mitigation: use redaction/truncation for each line and add regression assertions that credential-like fixture text is absent.
- Risk: Docs drift with the just-added 1.6.1 changelog. Mitigation: amend the same 1.6.1 entry before commit/push rather than adding contradictory release notes.
+21 -7
View File
@@ -1,23 +1,37 @@
{
"name": "opencode-working-memory",
"version": "1.5.0",
"version": "1.6.5",
"description": "Three-layer memory architecture for OpenCode with workspace memory and hot session state",
"type": "module",
"main": "index.ts",
"exports": {
".": "./index.ts"
".": "./index.ts",
"./server": "./index.ts",
"./tui": "./src/tui-plugin.ts"
},
"bin": {
"memory-diag": "./scripts/memory-diag-bin.cjs"
},
"files": [
"index.ts",
"src/",
"scripts/memory-diag.ts",
"scripts/memory-diag/",
"scripts/memory-diag-bin.cjs",
"dist/",
"README.md",
"LICENSE"
],
"scripts": {
"build": "node -e \"console.log('No build step required: OpenCode loads index.ts directly')\"",
"typecheck": "tsc --noEmit",
"test": "node --import ./tests/setup-xdg-data-home.ts --test --experimental-strip-types tests/*.test.ts",
"cleanup:workspaces": "node --experimental-strip-types scripts/dev/cleanup-workspaces.ts",
"clean:dist": "node -e \"require('node:fs').rmSync('dist',{recursive:true,force:true})\"",
"build:memory-diag": "npm run clean:dist && tsc -p tsconfig.memory-diag.json",
"build": "npm run build:memory-diag && node -e \"console.log('BUILD_PASS')\"",
"prepack": "npm run build",
"diag": "npm run --silent build:memory-diag && node ./scripts/memory-diag-bin.cjs",
"test:pack:memory-diag": "node --test --experimental-strip-types tests/smoke/memory-diag-packaging.test.ts",
"check:package-integrity": "node --experimental-strip-types scripts/dev/check-package-integrity.ts",
"typecheck": "tsc --noEmit && node -e \"console.log('TYPECHECK_PASS')\"",
"test": "node --import ./tests/setup-xdg-data-home.ts --test --experimental-strip-types tests/*.test.ts && node -e \"console.log('TEST_PASS')\"",
"check:compat": "npm install --no-save @opencode-ai/plugin@latest && npm run typecheck && npm test"
},
"keywords": [
@@ -47,6 +61,6 @@
"typescript": "^5.9.2"
},
"engines": {
"node": ">=18.0.0"
"node": ">=22.6.0"
}
}
+91
View File
@@ -0,0 +1,91 @@
#!/usr/bin/env node
import { readFile } from "node:fs/promises";
import { dirname, join, resolve } from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
type PackageManifest = {
version?: unknown;
};
type PackageLock = {
version?: unknown;
packages?: Record<string, { version?: unknown } | undefined>;
};
export type PackageVersionMismatch = {
field: "package-lock.json version" | "package-lock.json packages[\"\"].version";
expected: string;
actual: unknown;
};
export function packageVersionMismatches(
packageJson: PackageManifest,
packageLock: PackageLock,
): PackageVersionMismatch[] {
if (typeof packageJson.version !== "string" || packageJson.version.length === 0) {
throw new Error("package.json version must be a non-empty string");
}
const expected = packageJson.version;
const rootLockVersion = packageLock.version;
const rootPackageVersion = packageLock.packages?.[""]?.version;
const candidates = [
{ field: "package-lock.json version" as const, actual: rootLockVersion },
{ field: "package-lock.json packages[\"\"].version" as const, actual: rootPackageVersion },
];
return candidates
.filter(candidate => candidate.actual !== expected)
.map(candidate => ({ ...candidate, expected }));
}
export function formatPackageVersionMismatch(mismatch: PackageVersionMismatch): string {
return `${mismatch.field} (${String(mismatch.actual)}) does not match package.json version (${mismatch.expected})`;
}
export function packageLockReadErrorMessage(error: unknown): string {
const code = error && typeof error === "object" && "code" in error ? String(error.code) : "";
if (code === "ENOENT") return "package-lock.json not found; run npm install first";
const message = error instanceof Error ? error.message : String(error);
return `Unable to read package-lock.json; run npm install first. ${message}`;
}
async function readJsonFile<T>(path: string): Promise<T> {
return JSON.parse(await readFile(path, "utf8")) as T;
}
async function main(): Promise<void> {
const repoRoot = join(dirname(fileURLToPath(import.meta.url)), "../..");
const packageJson = await readJsonFile<PackageManifest>(join(repoRoot, "package.json"));
let packageLock: PackageLock;
try {
packageLock = await readJsonFile<PackageLock>(join(repoRoot, "package-lock.json"));
} catch (error) {
console.error(packageLockReadErrorMessage(error));
process.exit(1);
}
const mismatches = packageVersionMismatches(packageJson, packageLock);
if (mismatches.length > 0) {
console.error("Package integrity check failed:");
for (const mismatch of mismatches) {
console.error(`- ${formatPackageVersionMismatch(mismatch)}`);
}
process.exit(1);
}
console.log(`PACKAGE_INTEGRITY_PASS version=${packageJson.version}`);
}
function isMainModule(): boolean {
const invokedPath = process.argv[1];
return invokedPath ? import.meta.url === pathToFileURL(resolve(invokedPath)).href : false;
}
if (isMainModule()) {
await main();
}
+368
View File
@@ -0,0 +1,368 @@
#!/usr/bin/env node
/**
* Read-only baseline report for workspace-memory exact/identity key collisions.
*
* This script reads workspace-memory.json files from the local workspace memory
* data directory. It never writes to workspace memory stores; the only optional
* write is the explicit --json report path.
*/
import { existsSync } from "node:fs";
import { mkdir, readdir, readFile, writeFile } from "node:fs/promises";
import { homedir } from "node:os";
import { basename, dirname, join, resolve } from "node:path";
import { workspaceMemoryExactKey, workspaceMemoryIdentityKey } from "../../src/workspace-memory.ts";
import type { LongTermMemoryEntry, LongTermSource, LongTermType } from "../../src/types.ts";
type CliOptions = {
workspacesDir: string;
jsonPath?: string;
};
type ReinforcementCountStatistics = {
min: number;
max: number;
mean: number;
gtZero: number;
};
type MemorySummary = {
id: string;
type: LongTermType;
source: LongTermSource;
text: string;
reinforcementCount: number;
};
type WorkspaceSummary = {
dirName: string;
dirPath: string;
key: string;
root: string;
};
type CollisionGroup = {
kind: "exact" | "identity";
workspace: WorkspaceSummary;
key: string;
classification: "unclassified";
memories: MemorySummary[];
};
type ValidationReport = {
summary: {
workspaces: number;
activeMemories: number;
exactCollisionGroups: number;
identityCollisionGroups: number;
reinforcementCountStatistics: ReinforcementCountStatistics;
};
groups: CollisionGroup[];
};
type ScannedMemory = {
entry: LongTermMemoryEntry;
exactKey: string;
identityKey: string;
reinforcementCount: number;
};
const DEFAULT_WORKSPACES_DIR = join(homedir(), ".local", "share", "opencode-working-memory", "workspaces");
const WORKSPACES_DIR_ENV_KEYS = [
"OPENCODE_WORKING_MEMORY_WORKSPACES_DIR",
"WORKSPACE_MEMORY_WORKSPACES_DIR",
];
function usage(): string {
return `Usage:
node --experimental-strip-types scripts/dev/validate-identity-keys.ts [workspaces-dir] [--json <path>]
node --experimental-strip-types scripts/dev/validate-identity-keys.ts --data-path <workspaces-dir> [--json <path>]
Defaults:
workspaces-dir defaults to ${DEFAULT_WORKSPACES_DIR}
env override: ${WORKSPACES_DIR_ENV_KEYS.join(" or ")}
`;
}
function die(message: string): never {
console.error(message);
console.error(usage());
process.exit(1);
}
function envWorkspacesDir(): string | undefined {
for (const key of WORKSPACES_DIR_ENV_KEYS) {
const value = process.env[key];
if (value) return value;
}
return undefined;
}
function expandHome(path: string): string {
if (path === "~") return homedir();
if (path.startsWith("~/")) return join(homedir(), path.slice(2));
return path;
}
function parseArgs(argv: string[]): CliOptions {
let workspacesDir: string | undefined;
let jsonPath: string | undefined;
for (let i = 0; i < argv.length; i += 1) {
const arg = argv[i];
if (arg === "--help" || arg === "-h") {
console.log(usage());
process.exit(0);
}
if (arg === "--json") {
const value = argv[++i];
if (!value) die("--json requires a path");
jsonPath = expandHome(value);
continue;
}
if (arg === "--data-path" || arg === "--workspaces-dir") {
const value = argv[++i];
if (!value) die(`${arg} requires a path`);
if (workspacesDir) die("Only one workspaces directory may be provided");
workspacesDir = expandHome(value);
continue;
}
if (arg.startsWith("--")) die(`Unknown option: ${arg}`);
if (workspacesDir) die("Only one workspaces directory may be provided");
workspacesDir = expandHome(arg);
}
return {
workspacesDir: resolve(workspacesDir ?? expandHome(envWorkspacesDir() ?? DEFAULT_WORKSPACES_DIR)),
jsonPath: jsonPath ? resolve(jsonPath) : undefined,
};
}
function isLongTermType(value: unknown): value is LongTermType {
return value === "feedback" || value === "project" || value === "decision" || value === "reference";
}
function isLongTermSource(value: unknown): value is LongTermSource {
return value === "explicit" || value === "compaction" || value === "manual";
}
function isMemoryEntry(value: unknown): value is LongTermMemoryEntry {
if (!value || typeof value !== "object") return false;
const entry = value as Partial<LongTermMemoryEntry>;
return typeof entry.id === "string"
&& isLongTermType(entry.type)
&& typeof entry.text === "string"
&& isLongTermSource(entry.source);
}
function reinforcementCount(entry: LongTermMemoryEntry): number {
const count = entry.reinforcementCount ?? 0;
return Number.isFinite(count) && count > 0 ? count : 0;
}
function summarizeMemory(memory: ScannedMemory): MemorySummary {
return {
id: memory.entry.id,
type: memory.entry.type,
source: memory.entry.source,
text: memory.entry.text,
reinforcementCount: memory.reinforcementCount,
};
}
function addToGroupMap(map: Map<string, ScannedMemory[]>, key: string, memory: ScannedMemory): void {
const group = map.get(key);
if (group) {
group.push(memory);
} else {
map.set(key, [memory]);
}
}
function computeReinforcementStatistics(counts: number[]): ReinforcementCountStatistics {
if (counts.length === 0) {
return { min: 0, max: 0, mean: 0, gtZero: 0 };
}
const sum = counts.reduce((total, count) => total + count, 0);
return {
min: Math.min(...counts),
max: Math.max(...counts),
mean: sum / counts.length,
gtZero: counts.filter(count => count > 0).length,
};
}
async function readWorkspaceMemory(workspaceDir: string): Promise<unknown | undefined> {
const path = join(workspaceDir, "workspace-memory.json");
if (!existsSync(path)) return undefined;
try {
const raw = await readFile(path, "utf8");
return JSON.parse(raw);
} catch (error) {
const code = error && typeof error === "object" && "code" in error ? String(error.code) : "";
if (code === "ENOENT" || code === "EISDIR") return undefined;
throw error;
}
}
async function scanWorkspaces(workspacesDir: string): Promise<ValidationReport> {
const dirents = await readdir(workspacesDir, { withFileTypes: true });
const workspaceDirs = dirents
.filter(dirent => dirent.isDirectory())
.map(dirent => dirent.name)
.sort((a, b) => a.localeCompare(b));
const collisionGroups: CollisionGroup[] = [];
const reinforcementCounts: number[] = [];
let workspaces = 0;
let activeMemories = 0;
for (const dirName of workspaceDirs) {
const dirPath = join(workspacesDir, dirName);
const store = await readWorkspaceMemory(dirPath);
if (!store || typeof store !== "object") continue;
const storeObject = store as {
workspace?: { key?: unknown; root?: unknown };
entries?: unknown;
};
const rawEntries = Array.isArray(storeObject.entries) ? storeObject.entries : [];
const workspace: WorkspaceSummary = {
dirName,
dirPath,
key: typeof storeObject.workspace?.key === "string" ? storeObject.workspace.key : dirName,
root: typeof storeObject.workspace?.root === "string" ? storeObject.workspace.root : "<unknown>",
};
workspaces += 1;
const exactGroups = new Map<string, ScannedMemory[]>();
const identityGroups = new Map<string, ScannedMemory[]>();
for (const rawEntry of rawEntries) {
if (!isMemoryEntry(rawEntry)) continue;
if (rawEntry.status === "superseded") continue;
const scanned: ScannedMemory = {
entry: rawEntry,
exactKey: workspaceMemoryExactKey(rawEntry),
identityKey: workspaceMemoryIdentityKey(rawEntry),
reinforcementCount: reinforcementCount(rawEntry),
};
activeMemories += 1;
reinforcementCounts.push(scanned.reinforcementCount);
addToGroupMap(exactGroups, scanned.exactKey, scanned);
addToGroupMap(identityGroups, scanned.identityKey, scanned);
}
for (const [key, memories] of exactGroups.entries()) {
if (memories.length < 2) continue;
collisionGroups.push({
kind: "exact",
workspace,
key,
classification: "unclassified",
memories: memories.map(summarizeMemory),
});
}
for (const [key, memories] of identityGroups.entries()) {
if (memories.length < 2) continue;
collisionGroups.push({
kind: "identity",
workspace,
key,
classification: "unclassified",
memories: memories.map(summarizeMemory),
});
}
}
const exactCollisionGroups = collisionGroups.filter(group => group.kind === "exact").length;
const identityCollisionGroups = collisionGroups.filter(group => group.kind === "identity").length;
return {
summary: {
workspaces,
activeMemories,
exactCollisionGroups,
identityCollisionGroups,
reinforcementCountStatistics: computeReinforcementStatistics(reinforcementCounts),
},
groups: collisionGroups,
};
}
function oneLine(text: string): string {
return text.replace(/\s+/g, " ").trim();
}
function renderHumanReport(report: ValidationReport, workspacesDir: string): string {
const stats = report.summary.reinforcementCountStatistics;
const lines: string[] = [];
lines.push("Workspace memory identity key validation baseline");
lines.push(`Data path: ${workspacesDir}`);
lines.push("");
lines.push("Summary:");
lines.push(`- Workspaces scanned: ${report.summary.workspaces}`);
lines.push(`- Active memories: ${report.summary.activeMemories}`);
lines.push(`- Exact collision groups: ${report.summary.exactCollisionGroups}`);
lines.push(`- Identity collision groups: ${report.summary.identityCollisionGroups}`);
lines.push(`- Reinforcement counts: min=${stats.min} max=${stats.max} mean=${stats.mean.toFixed(2)} gtZero=${stats.gtZero}`);
if (report.groups.length === 0) {
lines.push("");
lines.push("Collision breakdown: none");
return lines.join("\n");
}
const groupsByWorkspace = new Map<string, CollisionGroup[]>();
for (const group of report.groups) {
const key = group.workspace.key;
const existing = groupsByWorkspace.get(key);
if (existing) existing.push(group);
else groupsByWorkspace.set(key, [group]);
}
lines.push("");
lines.push("Collision breakdown:");
for (const [workspaceKey, groups] of [...groupsByWorkspace.entries()].sort((a, b) => a[0].localeCompare(b[0]))) {
const workspace = groups[0].workspace;
const exactCount = groups.filter(group => group.kind === "exact").length;
const identityCount = groups.filter(group => group.kind === "identity").length;
lines.push(`- Workspace ${workspaceKey}`);
lines.push(` root: ${workspace.root}`);
lines.push(` dir: ${workspace.dirPath}`);
lines.push(` exactCollisionGroups=${exactCount} identityCollisionGroups=${identityCount}`);
for (const group of groups.sort((a, b) => a.kind.localeCompare(b.kind) || a.key.localeCompare(b.key))) {
lines.push(` - ${group.kind} key: ${group.key}`);
lines.push(` classification: ${group.classification}`);
for (const memory of group.memories) {
lines.push(` - id=${memory.id} type=${memory.type} source=${memory.source} reinforcementCount=${memory.reinforcementCount}`);
lines.push(` text: ${oneLine(memory.text)}`);
}
}
}
return lines.join("\n");
}
async function writeJsonReport(path: string, report: ValidationReport): Promise<void> {
if (basename(path) === "workspace-memory.json") {
die("Refusing to write JSON output to a workspace-memory.json file");
}
await mkdir(dirname(path), { recursive: true });
await writeFile(path, `${JSON.stringify(report, null, 2)}\n`, "utf8");
}
const options = parseArgs(process.argv.slice(2));
const report = await scanWorkspaces(options.workspacesDir);
console.log(renderHumanReport(report, options.workspacesDir));
if (options.jsonPath) {
await writeJsonReport(options.jsonPath, report);
console.log(`\nJSON report written to: ${options.jsonPath}`);
}
+31
View File
@@ -0,0 +1,31 @@
#!/usr/bin/env node
const { execFileSync } = require("child_process");
const { existsSync } = require("fs");
const path = require("path");
function isSupportedNodeVersion(version) {
const [major = 0, minor = 0, patch = 0] = version.split(".").map(part => Number(part));
void patch;
return major > 22 || (major === 22 && minor >= 6);
}
if (!isSupportedNodeVersion(process.versions.node)) {
process.stderr.write(`memory-diag requires Node >=22.6.0 per opencode-working-memory package engines. Current Node: v${process.versions.node}.\n`);
process.exit(1);
}
const binDir = __dirname;
const compiledScript = path.join(binDir, "..", "dist", "scripts", "memory-diag.js");
if (!existsSync(compiledScript)) {
process.stderr.write("memory-diag package is missing dist/scripts/memory-diag.js. Reinstall opencode-working-memory or run npm run build before using the local package.\n");
process.exit(1);
}
const args = [compiledScript, ...process.argv.slice(2)];
try {
execFileSync(process.execPath, args, { stdio: "inherit" });
process.exit(0);
} catch (e) {
process.exit(e.status || 1);
}
+33 -697
View File
@@ -4,715 +4,51 @@
* Does not send telemetry, make API calls, or affect plugin runtime behavior.
*/
import { createHash } from "node:crypto";
import { existsSync } from "node:fs";
import { readFile, readdir } from "node:fs/promises";
import { join } from "node:path";
import { dataHome, extractionRejectionLogPath, migrationLogPath, workspaceKey, workspaceMemoryPath, workspacePendingJournalPath } from "../src/paths.ts";
import { assessMemoryQuality, HARD_QUALITY_REASONS } from "../src/memory-quality.ts";
import { redactCredentials } from "../src/redaction.ts";
import { scanWorkspaceResidues } from "../src/workspace-cleanup.ts";
import { calculateRetentionStrength, renderWorkspaceMemory } from "../src/workspace-memory.ts";
import type { LongTermMemoryEntry, LongTermSource, LongTermType, PendingMemoryJournalStore, WorkspaceMemoryStore } from "../src/types.ts";
import { LONG_TERM_LIMITS, PROMOTION_RETRY_LIMITS } from "../src/types.ts";
import { parseArgs, type ParsedArgs } from "./memory-diag/cli.ts";
import { dispatch } from "./memory-diag/command-registry.ts";
import { CliInputError } from "./memory-diag/types.ts";
type Command = "health" | "rejections" | "audit";
type Origin = "explicit_trigger" | "compaction_candidate" | "manual" | "migration_check" | "unknown";
type ParsedError = Extract<ParsedArgs, { ok: false }>;
type ParsedHelp = Extract<ParsedArgs, { ok: true; help: true }>;
type CliOptions = {
raw: boolean;
workspace?: string;
all?: boolean;
softOnly?: boolean;
triggerOnly?: boolean;
since?: string;
migration?: string;
};
type RejectionLogRecord = {
timestamp?: string;
workspaceKey?: string;
workspaceRoot?: string;
type?: LongTermType;
source?: LongTermSource | string;
origin?: string;
fromTrigger?: boolean;
text?: string;
reasons?: string[];
};
type NormalizedRejection = Required<Pick<RejectionLogRecord, "timestamp" | "type" | "text" | "reasons">> & {
workspaceKey?: string;
workspaceRoot?: string;
source?: string;
origin: Origin;
fromTrigger: boolean;
};
type MigrationLogRecord = {
migrationId?: string;
timestamp?: string;
workspaceKey?: string;
workspaceRoot?: string;
entryId?: string;
type?: LongTermType;
source?: LongTermSource | string;
text?: string;
reasons?: string[];
hardReasons?: string[];
beforeStatus?: string;
afterStatus?: string;
};
const TYPES: LongTermType[] = ["feedback", "decision", "project", "reference"];
const TYPE_MAX_FOR_DIAG: Record<LongTermType, number> = {
feedback: 10,
decision: 10,
project: 8,
reference: 6,
};
const WORKSPACE_DORMANT_AFTER_DAYS_FOR_DIAG = 14;
const DORMANT_DECAY_MULTIPLIER_FOR_DIAG = 0.25;
const SUSPICIOUS_REASONS = [
"progress_snapshot",
"active_file_snapshot",
"commit_or_ci_snapshot",
"temporary_status",
"raw_error",
"code_or_api_signature",
] as const;
const ALLOWED_ORIGINS = new Set<Origin>([
"explicit_trigger",
"compaction_candidate",
"manual",
"migration_check",
"unknown",
]);
function usage(): string {
return `Usage:
bun scripts/memory-diag.ts health [--workspace <path>] [--all] [--raw]
bun scripts/memory-diag.ts rejections [--soft-only] [--trigger-only] [--since 14d] [--raw]
bun scripts/memory-diag.ts audit [--migration <id>] [--raw]
`;
function isParsedError(parsed: ParsedArgs): parsed is ParsedError {
return parsed.ok === false;
}
function die(message: string): never {
console.error(message);
console.error(usage());
process.exit(1);
function isParsedHelp(parsed: ParsedArgs): parsed is ParsedHelp {
return parsed.ok === true && "help" in parsed;
}
function parseArgs(argv: string[]): { command: Command; options: CliOptions } {
const [command, ...rest] = argv;
if (!command || command === "--help" || command === "-h") {
console.log(usage());
process.exit(0);
async function main(): Promise<number> {
const parsed = parseArgs(process.argv.slice(2));
if (isParsedError(parsed)) {
console.error(parsed.message);
console.error(parsed.usage);
return parsed.exitCode;
}
if (command !== "health" && command !== "rejections" && command !== "audit") {
die(`Unknown subcommand: ${command}`);
if (isParsedHelp(parsed)) {
console.log(parsed.usage);
return 0;
}
const options: CliOptions = { raw: false };
for (let i = 0; i < rest.length; i += 1) {
const arg = rest[i];
if (arg === "--raw") options.raw = true;
else if (arg === "--all") options.all = true;
else if (arg === "--soft-only") options.softOnly = true;
else if (arg === "--trigger-only") options.triggerOnly = true;
else if (arg === "--workspace") {
const value = rest[++i];
if (!value) die("--workspace requires a path");
options.workspace = value;
} else if (arg === "--since") {
const value = rest[++i];
if (!value) die("--since requires a duration or ISO timestamp");
options.since = value;
} else if (arg === "--migration") {
const value = rest[++i];
if (!value) die("--migration requires an id");
options.migration = value;
} else {
die(`Unknown option: ${arg}`);
}
}
if (command === "health") {
if (options.all && options.workspace) die("Use either --all or --workspace, not both");
} else {
if (options.all || options.workspace) die(`${command} does not accept --all or --workspace`);
}
if (command !== "rejections" && (options.softOnly || options.triggerOnly || options.since)) {
die(`${command} does not accept rejection filters`);
}
if (command !== "audit" && options.migration) {
die(`${command} does not accept --migration`);
}
return { command, options };
}
function countBy<T extends string>(items: T[]): Map<T, number> {
const counts = new Map<T, number>();
for (const item of items) counts.set(item, (counts.get(item) ?? 0) + 1);
return counts;
}
function sortedCounts<T extends string>(counts: Map<T, number>): Array<[T, number]> {
return [...counts.entries()].sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0]));
}
function workspaceRootHash(root: string): string {
return createHash("sha256").update(root).digest("hex").slice(0, 16);
}
function redactAbsolutePaths(text: string): string {
return text.replace(/(?:^|[\s"'`(=:\[])(\/(?:Users|home|private|tmp|var|opt|Volumes|[^\s"'`)\],;:]+)\/[^\s"'`)\],;:]*)/g, (match, path) => match.replace(path, "<path>"));
}
function cleanText(text: string, raw: boolean): string {
if (raw) return text;
return redactAbsolutePaths(redactCredentials(text));
}
function cleanPath(path: string, raw: boolean): string {
return raw ? path : "<path>";
}
function formatWorkspaceIdentity(workspaceKeyValue: string | undefined, workspaceRoot: string | undefined, raw: boolean): string {
const parts: string[] = [];
if (workspaceKeyValue) parts.push(`workspaceKey=${workspaceKeyValue}`);
if (workspaceRoot) {
parts.push(raw ? `workspaceRoot=${workspaceRoot}` : `workspaceRootHash=${workspaceRootHash(workspaceRoot)}`);
}
return parts.join(" ");
}
function truncate(text: string, max = 120): string {
const collapsed = text.replace(/\s+/g, " ").trim();
return collapsed.length <= max ? collapsed : `${collapsed.slice(0, max - 1)}`;
}
async function readJSONFile<T>(path: string): Promise<T | null> {
try {
return JSON.parse(await readFile(path, "utf8")) as T;
} catch {
return null;
}
}
async function readJSONLFile<T>(path: string): Promise<{ records: T[]; invalidLines: number }> {
let content = "";
try {
content = await readFile(path, "utf8");
} catch {
return { records: [], invalidLines: 0 };
}
const records: T[] = [];
let invalidLines = 0;
for (const line of content.split("\n")) {
const trimmed = line.trim();
if (!trimmed) continue;
try {
records.push(JSON.parse(trimmed) as T);
} catch {
invalidLines += 1;
const result = await dispatch(parsed.command, parsed.options);
const stderr = [parsed.deprecationNotice, result.stderr].filter(Boolean).join("\n");
if (stderr) process.stderr.write(stderr.endsWith("\n") ? stderr : `${stderr}\n`);
if (result.stdout) process.stdout.write(result.stdout.endsWith("\n") ? result.stdout : `${result.stdout}\n`);
return result.exitCode ?? 0;
} catch (error) {
if (error instanceof CliInputError) {
process.stderr.write(`${error.message}\n`);
return 1;
}
}
return { records, invalidLines };
}
function canonicalMemoryText(text: string): string {
return text
.normalize("NFKC")
.toLowerCase()
.replace(/[\s\p{P}]+/gu, " ")
.trim();
}
function ageDays(entry: LongTermMemoryEntry): number | null {
const time = new Date(entry.createdAt).getTime();
if (Number.isNaN(time)) return null;
return Math.floor((Date.now() - time) / 86_400_000);
}
function formatStrength(value: number): string {
return Number.isFinite(value) ? value.toFixed(3) : "0.000";
}
function daysSinceIso(value: string | undefined, now = Date.now()): number | null {
if (!value) return null;
const ms = new Date(value).getTime();
if (!Number.isFinite(ms)) return null;
return Math.max(0, (now - ms) / 86_400_000);
}
function formatPercent(ratio: number): string {
return `${(ratio * 100).toFixed(1)}%`;
}
type RetentionDiagItem = {
entry: LongTermMemoryEntry;
strength: number;
};
function isSafetyCriticalForDiag(entry: LongTermMemoryEntry): boolean {
return entry.safetyCritical === true;
}
function retentionCandidatesForDiag(store: WorkspaceMemoryStore): {
sorted: RetentionDiagItem[];
rendered: RetentionDiagItem[];
typeCapped: RetentionDiagItem[];
globalCapped: RetentionDiagItem[];
} {
const now = Date.now();
const active = store.entries.filter(entry => entry.status !== "superseded");
const sorted = active
.map(entry => ({ entry, strength: calculateRetentionStrength(entry, now, store.lastActivityAt) }))
.sort((a, b) => b.strength - a.strength || a.entry.id.localeCompare(b.entry.id));
const rendered: RetentionDiagItem[] = [];
const typeCapped: RetentionDiagItem[] = [];
const globalCapped: RetentionDiagItem[] = [];
const typeCounts: Partial<Record<LongTermType, number>> = {};
for (const item of sorted) {
if (!isSafetyCriticalForDiag(item.entry)) {
const count = typeCounts[item.entry.type] ?? 0;
const max = TYPE_MAX_FOR_DIAG[item.entry.type] ?? Infinity;
if (count >= max) {
typeCapped.push(item);
continue;
}
typeCounts[item.entry.type] = count + 1;
}
if (rendered.length < LONG_TERM_LIMITS.maxEntries) {
rendered.push(item);
} else {
globalCapped.push(item);
}
}
return { sorted, rendered, typeCapped, globalCapped };
}
function promotionLimit(source: LongTermSource): number {
if (source === "manual") return PROMOTION_RETRY_LIMITS.maxManualAttempts;
return PROMOTION_RETRY_LIMITS.maxExplicitAttempts;
}
function emptyStore(root: string, key: string): WorkspaceMemoryStore {
return {
version: 1,
workspace: { root, key },
limits: { maxRenderedChars: LONG_TERM_LIMITS.maxRenderedChars, maxEntries: LONG_TERM_LIMITS.maxEntries },
entries: [],
migrations: [],
updatedAt: new Date(0).toISOString(),
};
}
function normalizedStore(store: WorkspaceMemoryStore | null, root: string, key: string): WorkspaceMemoryStore {
const fallback = emptyStore(root, key);
return {
...fallback,
...(store ?? {}),
workspace: store?.workspace ?? fallback.workspace,
limits: {
maxRenderedChars: store?.limits?.maxRenderedChars ?? fallback.limits.maxRenderedChars,
maxEntries: store?.limits?.maxEntries ?? fallback.limits.maxEntries,
},
entries: Array.isArray(store?.entries) ? store.entries : [],
migrations: Array.isArray(store?.migrations) ? store.migrations : [],
};
}
function normalizedJournal(journal: PendingMemoryJournalStore | null): PendingMemoryJournalStore {
return {
version: 1,
workspace: journal?.workspace ?? { root: "", key: "" },
entries: Array.isArray(journal?.entries) ? journal.entries : [],
updatedAt: journal?.updatedAt ?? new Date(0).toISOString(),
};
}
async function runHealth(options: CliOptions): Promise<void> {
if (options.all) {
const scan = await scanWorkspaceResidues({ includeOrphans: true, minAgeMs: 0 });
console.log("Workspace memory health");
console.log("");
if (scan.results.length === 0) {
console.log("No workspace stores found.");
return;
}
for (let i = 0; i < scan.results.length; i += 1) {
const result = scan.results[i];
if (i > 0) console.log("");
await printWorkspaceHealth({
root: result.root,
key: result.workspaceKey,
memoryPath: join(result.workspaceDir, "workspace-memory.json"),
pendingPath: join(result.workspaceDir, "workspace-pending-journal.json"),
raw: options.raw,
});
}
return;
}
const root = options.workspace ?? process.cwd();
const key = await workspaceKey(root);
await printWorkspaceHealth({
root,
key,
memoryPath: await workspaceMemoryPath(root),
pendingPath: await workspacePendingJournalPath(root),
raw: options.raw,
includeTitle: true,
});
}
async function printWorkspaceHealth(input: {
root?: string;
key: string;
memoryPath: string;
pendingPath: string;
raw: boolean;
includeTitle?: boolean;
}): Promise<void> {
if (input.includeTitle) {
console.log("Workspace memory health");
console.log("");
}
const rawStore = await readJSONFile<WorkspaceMemoryStore>(input.memoryPath);
const storeRoot = rawStore?.workspace?.root ?? input.root ?? "";
const storeKey = rawStore?.workspace?.key ?? input.key;
const store = normalizedStore(rawStore, storeRoot, storeKey);
const journal = normalizedJournal(await readJSONFile<PendingMemoryJournalStore>(input.pendingPath));
const identity = formatWorkspaceIdentity(storeKey, storeRoot || undefined, input.raw);
if (identity) console.log(identity);
console.log(`memoryPath=${cleanPath(input.memoryPath, input.raw)}`);
console.log(`pendingPath=${cleanPath(input.pendingPath, input.raw)}`);
if (!rawStore) console.log("memory store: missing or unreadable (treated as empty)");
if (!existsSync(input.pendingPath)) console.log("pending journal: missing (treated as empty)");
console.log("");
const active = store.entries.filter(entry => entry.status !== "superseded");
const superseded = store.entries.filter(entry => entry.status === "superseded");
const retention = retentionCandidatesForDiag(store);
const renderedEntries = retention.rendered.map(item => item.entry);
const renderedEstimate = renderWorkspaceMemory(store).length;
console.log(`Stored active memories: ${active.length}`);
console.log(`Superseded memories: ${superseded.length}`);
console.log(`Rendered candidates: ${renderedEntries.length}`);
console.log(`Rendered estimate: ${renderedEstimate.toLocaleString()} chars`);
console.log("");
const pendingEntries = journal.entries;
const retryable = pendingEntries.filter(entry => (entry.promotionAttempts ?? 0) < promotionLimit(entry.source)).length;
const nearRetryLimit = pendingEntries.filter(entry => (entry.promotionAttempts ?? 0) >= promotionLimit(entry.source) - 1).length;
const pendingBySource = countBy(pendingEntries.map(entry => entry.source));
console.log("Pending journal:");
console.log(` total: ${pendingEntries.length}`);
console.log(` retryable: ${retryable}`);
console.log(` near retry limit: ${nearRetryLimit}`);
console.log(" by source:");
for (const source of ["explicit", "manual", "compaction"] as LongTermSource[]) {
console.log(` ${source}: ${pendingBySource.get(source) ?? 0}`);
}
console.log("");
console.log("By type:");
for (const type of TYPES) {
const storedCount = active.filter(entry => entry.type === type).length;
const renderedCount = renderedEntries.filter(entry => entry.type === type).length;
const supersededCount = superseded.filter(entry => entry.type === type).length;
console.log(` ${type.padEnd(9)} stored=${String(storedCount).padEnd(3)} rendered=${String(renderedCount).padEnd(3)} typeCap=${TYPE_MAX_FOR_DIAG[type]} superseded=${supersededCount}`);
}
console.log("");
console.log("Retention caps:");
console.log(` type-capped entries: ${retention.typeCapped.length}`);
console.log(` global-cap overflow: ${retention.globalCapped.length}`);
console.log("");
const olderThan30 = active.filter(entry => (ageDays(entry) ?? 0) > 30).length;
const olderThan90 = active.filter(entry => (ageDays(entry) ?? 0) > 90).length;
const staleMarked = active.filter(entry => {
const days = ageDays(entry);
return Boolean(entry.staleAfterDays && days !== null && days > entry.staleAfterDays);
}).length;
console.log("Age:");
console.log(` stale-marked: ${staleMarked}`);
console.log(` older than 30d: ${olderThan30}`);
console.log(` older than 90d: ${olderThan90}`);
console.log("");
const wallDaysSinceActivity = daysSinceIso(store.lastActivityAt);
const dormantDiscountActive = wallDaysSinceActivity !== null && wallDaysSinceActivity > WORKSPACE_DORMANT_AFTER_DAYS_FOR_DIAG;
const dormantDaysPastGrace = wallDaysSinceActivity === null
? 0
: Math.max(0, wallDaysSinceActivity - WORKSPACE_DORMANT_AFTER_DAYS_FOR_DIAG);
console.log("Dormancy:");
console.log(` lastActivityAt: ${store.lastActivityAt ?? "(missing)"}`);
console.log(` wall days since activity: ${wallDaysSinceActivity === null ? "unknown" : wallDaysSinceActivity.toFixed(1)}`);
console.log(` dormant discount active: ${dormantDiscountActive ? "yes" : "no"}`);
console.log(` dormant days past grace: ${dormantDaysPastGrace.toFixed(1)}`);
console.log(` dormant multiplier: ${DORMANT_DECAY_MULTIPLIER_FOR_DIAG}`);
console.log("");
const highImportanceCount = active.filter(entry => entry.userImportance === "high").length;
const safetyCriticalCount = active.filter(isSafetyCriticalForDiag).length;
const maxReinforcedCount = active.filter(entry => (entry.reinforcementCount ?? 0) >= 6).length;
const highImportanceRatio = active.length === 0 ? 0 : highImportanceCount / active.length;
const maxReinforcedRatio = active.length === 0 ? 0 : maxReinforcedCount / active.length;
const highImportanceAlert = highImportanceRatio > 0.3;
const safetyCriticalAlert = safetyCriticalCount > 5;
const maxReinforcedAlert = maxReinforcedRatio > 0.1;
console.log("Retention monitoring:");
console.log(` high_importance_ratio: ${formatPercent(highImportanceRatio)} (alert > 30%)${highImportanceAlert ? " ALERT" : ""}`);
console.log(` safety_critical_count: ${safetyCriticalCount} (alert > 5)${safetyCriticalAlert ? " ALERT" : ""}`);
console.log(` max_reinforced_count: ${maxReinforcedAlert ? `${maxReinforcedCount} (${formatPercent(maxReinforcedRatio)}, alert > 10%) ALERT` : `${maxReinforcedCount} (alert > 10% active)`}`);
console.log("");
const qualityByEntry = active.map(entry => ({ entry, quality: assessMemoryQuality(entry) }));
const duplicateCounts = countBy(active.map(entry => `${entry.type}:${canonicalMemoryText(entry.text)}`));
const duplicateExtras = [...duplicateCounts.values()].reduce((sum, count) => sum + Math.max(0, count - 1), 0);
console.log("Quality warnings:");
console.log(` progress-like active memories: ${qualityByEntry.filter(item => item.quality.reasons.includes("progress_snapshot")).length}`);
console.log(` path-heavy active memories: ${qualityByEntry.filter(item => item.quality.reasons.includes("path_heavy")).length}`);
console.log(` duplicate-ish exact canonical text: ${duplicateExtras}`);
console.log(` very long entries: ${active.filter(entry => entry.text.length > LONG_TERM_LIMITS.maxEntryTextChars).length}`);
console.log("");
console.log("Suspicious active memories:");
for (const reason of SUSPICIOUS_REASONS) {
console.log(` ${reason}-like: ${qualityByEntry.filter(item => item.quality.reasons.includes(reason)).length}`);
}
const failingQuality = qualityByEntry.filter(item => !item.quality.accepted);
if (failingQuality.length > 0) {
console.log("");
console.log("Active memories failing offline quality checks:");
for (const item of failingQuality.slice(0, 8)) {
console.log(` - [${item.entry.type}] reasons=${item.quality.reasons.join(",")} ${JSON.stringify(truncate(cleanText(item.entry.text, input.raw)))}`);
}
}
console.log("");
console.log("Top rendered candidates:");
const top = retention.rendered.slice(0, 5);
if (top.length === 0) {
console.log(" (none)");
} else {
for (const item of top) {
console.log(` - strength=${formatStrength(item.strength)} [${item.entry.type}] ${truncate(cleanText(item.entry.text, input.raw))}`);
}
}
console.log("");
console.log("Weakest active memories:");
const weakest = retention.sorted.slice(-5).reverse();
if (weakest.length === 0) {
console.log(" (none)");
} else {
for (const item of weakest) {
console.log(` - strength=${formatStrength(item.strength)} [${item.entry.type}] ${truncate(cleanText(item.entry.text, input.raw))}`);
if (error instanceof Error) {
process.stderr.write(`memory-diag failed: ${error.message}\n`);
return 1;
}
process.stderr.write(`memory-diag failed: ${String(error)}\n`);
return 1;
}
}
function inferOrigin(record: RejectionLogRecord): Origin {
if (record.origin && ALLOWED_ORIGINS.has(record.origin as Origin)) return record.origin as Origin;
if (record.source === "compaction") return "compaction_candidate";
if (record.source === "explicit") return "explicit_trigger";
if (record.source === "manual") return "manual";
return "unknown";
}
function normalizeRejection(record: RejectionLogRecord): NormalizedRejection | null {
if (!record.text || !Array.isArray(record.reasons)) return null;
const origin = inferOrigin(record);
return {
timestamp: record.timestamp ?? "",
workspaceKey: record.workspaceKey,
workspaceRoot: record.workspaceRoot,
type: record.type ?? "project",
source: record.source,
origin,
fromTrigger: typeof record.fromTrigger === "boolean" ? record.fromTrigger : origin === "explicit_trigger",
text: record.text,
reasons: record.reasons,
};
}
function sinceCutoff(rawSince: string | undefined): number | null {
if (!rawSince) return null;
const relative = rawSince.match(/^(\d+)([dhm])$/i);
if (relative) {
const amount = Number(relative[1]);
const unit = relative[2].toLowerCase();
const multiplier = unit === "d" ? 86_400_000 : unit === "h" ? 3_600_000 : 60_000;
return Date.now() - amount * multiplier;
}
const timestamp = new Date(rawSince).getTime();
if (Number.isNaN(timestamp)) die(`Invalid --since value: ${rawSince}`);
return timestamp;
}
function hasSoftReason(record: NormalizedRejection): boolean {
return record.reasons.some(reason => !HARD_QUALITY_REASONS.has(reason));
}
async function runRejections(options: CliOptions): Promise<void> {
const path = extractionRejectionLogPath();
const { records, invalidLines } = await readJSONLFile<RejectionLogRecord>(path);
const cutoff = sinceCutoff(options.since);
let normalized = records.map(normalizeRejection).filter((record): record is NormalizedRejection => record !== null);
if (cutoff !== null) {
normalized = normalized.filter(record => {
const timestamp = new Date(record.timestamp).getTime();
return !Number.isNaN(timestamp) && timestamp >= cutoff;
});
}
if (options.softOnly) normalized = normalized.filter(hasSoftReason);
if (options.triggerOnly) normalized = normalized.filter(record => record.fromTrigger || record.origin === "explicit_trigger");
console.log("Extraction rejection summary");
console.log("");
console.log(`logPath=${cleanPath(path, options.raw)}`);
if (invalidLines > 0) console.log(`Invalid JSONL lines skipped: ${invalidLines}`);
console.log("");
console.log(`Total rejected: ${normalized.length}`);
console.log("");
console.log("By reason:");
const byReason = sortedCounts(countBy(normalized.flatMap(record => record.reasons)));
if (byReason.length === 0) console.log(" (none)");
else for (const [reason, count] of byReason) console.log(` ${reason.padEnd(24)} ${count}`);
console.log("");
console.log("By origin:");
const byOrigin = sortedCounts(countBy(normalized.map(record => record.origin)));
if (byOrigin.length === 0) console.log(" (none)");
else for (const [origin, count] of byOrigin) console.log(` ${origin.padEnd(24)} ${count}`);
console.log("");
console.log("Trigger-origin rejections (high priority for v1.5):");
const triggerReasons = sortedCounts(countBy(normalized.filter(record => record.fromTrigger || record.origin === "explicit_trigger").flatMap(record => record.reasons)));
if (triggerReasons.length === 0) console.log(" (none)");
else for (const [reason, count] of triggerReasons) console.log(` ${reason.padEnd(24)} ${count}`);
console.log("");
console.log("Recent suspicious soft rejects:");
const suspicious = normalized
.filter(hasSoftReason)
.sort((a, b) => (new Date(b.timestamp).getTime() || 0) - (new Date(a.timestamp).getTime() || 0))
.slice(0, 8);
if (suspicious.length === 0) {
console.log(" (none)");
} else {
for (const record of suspicious) {
const identity = formatWorkspaceIdentity(record.workspaceKey, record.workspaceRoot, options.raw);
console.log(` - [${record.type}] ${JSON.stringify(truncate(cleanText(record.text, options.raw)))}`);
console.log(` reasons: ${record.reasons.join(",")}`);
console.log(` origin: ${record.origin}${identity ? ` (${identity})` : ""}`);
}
}
}
function migrationLogsRoot(): string {
return join(dataHome(), "opencode-working-memory", "migration-logs");
}
async function migrationLogPaths(options: CliOptions): Promise<string[]> {
if (options.migration) return [migrationLogPath(options.migration)];
const root = migrationLogsRoot();
let entries: string[] = [];
try {
entries = await readdir(root);
} catch {
return [];
}
return entries.filter(entry => entry.endsWith(".jsonl")).sort().map(entry => join(root, entry));
}
function migrationIdFromPath(path: string): string {
return path.split("/").pop()?.replace(/\.jsonl$/, "") ?? "unknown";
}
function riskySupersedeReasons(record: MigrationLogRecord): string[] {
const reasons: string[] = [];
const hardReasonsMissing = !Array.isArray(record.hardReasons);
const hardReasons = Array.isArray(record.hardReasons) ? record.hardReasons : [];
const qualityReasons = Array.isArray(record.reasons) ? record.reasons : [];
const text = record.text ?? "";
if (hardReasonsMissing || hardReasons.length === 0) reasons.push("missing_or_empty_hardReasons");
if (qualityReasons.length > 0 && hardReasons.length === 0) reasons.push("soft_reasons_without_hardReasons");
if (/\b(?:User|user|prefers|requires|wants|insists)\b||使||||/u.test(text)) reasons.push("user_preference_marker");
if (/\b(?:must|should|do not|never|is|are|follows)\b|||||/iu.test(text)) reasons.push("durable_rule_marker");
if ((record.type === "feedback" || record.type === "decision") && hardReasons.length === 1 && hardReasons[0] === "path_heavy") {
reasons.push("feedback_or_decision_path_heavy_only");
}
return reasons;
}
async function runAudit(options: CliOptions): Promise<void> {
const paths = await migrationLogPaths(options);
console.log("Migration audit report");
console.log("");
if (paths.length === 0) {
console.log("No migration logs found.");
return;
}
for (let i = 0; i < paths.length; i += 1) {
const path = paths[i];
const migrationId = options.migration ?? migrationIdFromPath(path);
const { records, invalidLines } = await readJSONLFile<MigrationLogRecord>(path);
const superseded = records.filter(record => !record.afterStatus || record.afterStatus === "superseded");
const hardReasons = superseded.flatMap(record => {
if (Array.isArray(record.hardReasons)) return record.hardReasons;
return Array.isArray(record.reasons) ? record.reasons.filter(reason => HARD_QUALITY_REASONS.has(reason)) : [];
});
const risky = superseded
.map(record => ({ record, reasons: riskySupersedeReasons(record) }))
.filter(item => item.reasons.length > 0);
if (i > 0) console.log("");
console.log(`Migration: ${migrationId}`);
console.log(`logPath=${cleanPath(path, options.raw)}`);
if (invalidLines > 0) console.log(`Invalid JSONL lines skipped: ${invalidLines}`);
console.log(`Superseded entries: ${superseded.length}`);
console.log("");
console.log("By hard reason:");
const byHardReason = sortedCounts(countBy(hardReasons));
if (byHardReason.length === 0) console.log(" (none)");
else for (const [reason, count] of byHardReason) console.log(` ${reason.padEnd(24)} ${count}`);
console.log("");
console.log("Potentially risky supersedes:");
console.log(` ${risky.length}`);
for (const item of risky.slice(0, 10)) {
const record = item.record;
const hard = Array.isArray(record.hardReasons) ? record.hardReasons : [];
const identity = formatWorkspaceIdentity(record.workspaceKey, record.workspaceRoot, options.raw);
console.log(` - [${record.type ?? "unknown"}] hardReasons=${JSON.stringify(hard)} risk=${item.reasons.join(",")} ${JSON.stringify(truncate(cleanText(record.text ?? "", options.raw)))}`);
if (identity) console.log(` ${identity}`);
}
}
}
const { command, options } = parseArgs(process.argv.slice(2));
if (command === "health") await runHealth(options);
else if (command === "rejections") await runRejections(options);
else await runAudit(options);
process.exitCode = await main();
+138
View File
@@ -0,0 +1,138 @@
import { HIDDEN_COMMAND_NOTICES, isCommand } from "./command-metadata.ts";
import type { CliOptions, Command, ParsedArgs } from "./types.ts";
export type { ParsedArgs } from "./types.ts";
export function usage(): string {
return `Usage:
memory-diag [status] [--workspace <path>] [--verbose] [--json]
memory-diag rejected [--workspace <path>] [--verbose] [--json]
memory-diag missing [--workspace <path>] [--verbose] [--json]
memory-diag explain [memory-id] [--workspace <path>] [--raw]
memory-diag commands [--workspace <path>] [--verbose] [--json] [--memory <id>]
memory-diag quality [--workspace <path>] [--verbose] [--json] [--raw] [--no-emoji]
memory-diag revert (--memory <replacement-id> | --event <event-id>) [--workspace <path>] [--apply]
Global options:
--workspace <path> Workspace path (default: current directory)
--verbose Show detailed diagnostics
--json Print machine-readable JSON where supported
--no-emoji Disable emoji in human output
`;
}
function error(message: string): ParsedArgs {
return { ok: false, message, usage: usage(), exitCode: 1 };
}
function isValidSince(rawSince: string): boolean {
if (/^(\d+)([dhm])$/i.test(rawSince)) return true;
return !Number.isNaN(new Date(rawSince).getTime());
}
export function parseArgs(argv: string[]): ParsedArgs {
const [first, ...tail] = argv;
if (first === "--help" || first === "-h") {
return { ok: true, help: true, usage: usage() };
}
let command: Command = "status";
let deprecationNotice: string | undefined;
let rest = argv;
if (first && !first.startsWith("--")) {
rest = tail;
if (isCommand(first)) {
command = first;
deprecationNotice = HIDDEN_COMMAND_NOTICES[first];
} else {
return error(`Unknown subcommand: ${first}`);
}
}
const options: CliOptions = { raw: false, positional: [] };
for (let i = 0; i < rest.length; i += 1) {
const arg = rest[i];
if (arg === "--raw") options.raw = true;
else if (arg === "--json") options.json = true;
else if (arg === "--verbose") options.verbose = true;
else if (arg === "--no-emoji") options.noEmoji = true;
else if (arg === "--all") options.all = true;
else if (arg === "--soft-only") options.softOnly = true;
else if (arg === "--trigger-only") options.triggerOnly = true;
else if (arg === "--include-historical") options.includeHistorical = true;
else if (arg === "--explain") options.explain = true;
else if (arg === "--apply") options.apply = true;
else if (arg === "--workspace") {
const value = rest[++i];
if (!value) return error("--workspace requires a path");
options.workspace = value;
} else if (arg === "--since") {
const value = rest[++i];
if (!value) return error("--since requires a duration or ISO timestamp");
options.since = value;
} else if (arg === "--reason") {
const value = rest[++i];
if (!value) return error("--reason requires a reason code");
options.reason = value;
} else if (arg === "--migration") {
const value = rest[++i];
if (!value) return error("--migration requires an id");
options.migration = value;
} else if (arg === "--memory") {
const value = rest[++i];
if (!value) return error("--memory requires an id");
options.memory = value;
} else if (arg === "--event") {
const value = rest[++i];
if (!value) return error("--event requires an id");
options.event = value;
} else if (!arg.startsWith("--") && command === "explain") {
options.positional?.push(arg);
} else {
return error(`Unknown option: ${arg}`);
}
}
if (command === "explain") {
const positional = options.positional ?? [];
if (positional.length > 1) return error("explain accepts at most one memory id");
if (positional.length === 1 && options.memory) return error("Use either explain <memory-id> or --memory, not both");
if (positional.length === 1) options.memory = positional[0];
} else if ((options.positional ?? []).length > 0) {
return error(`Unknown option: ${options.positional?.[0]}`);
}
if (command === "status") {
if (options.all) return error(`${command} does not accept --all`);
} else if (command === "rejected" || command === "missing" || command === "coverage" || command === "explain" || command === "commands" || command === "quality" || command === "revert") {
if (options.all) return error(`${command} does not accept --all`);
} else {
if (options.all || options.workspace) return error(`${command} does not accept --all or --workspace`);
}
if (options.json && command !== "status" && command !== "rejected" && command !== "missing" && command !== "coverage" && command !== "commands" && command !== "quality") {
return error(`${command} does not accept --json`);
}
if (command !== "rejected" && (options.softOnly || options.triggerOnly || options.since)) {
return error(`${command} does not accept rejection filters`);
}
if (command !== "coverage" && options.includeHistorical) return error(`${command} does not accept --include-historical`);
if (command !== "rejected" && options.reason) return error(`${command} does not accept rejection filters`);
if (command !== "missing" && options.explain) return error(`${command} does not accept --explain`);
if (command !== "audit" && options.migration) {
return error(`${command} does not accept --migration`);
}
if (command !== "explain" && command !== "revert" && command !== "commands" && options.memory) {
return error(`${command} does not accept --memory`);
}
if (command !== "revert" && options.event) return error(`${command} does not accept --event`);
if (command !== "revert" && options.apply) return error(`${command} does not accept --apply`);
if (command === "revert") {
if (!options.memory && !options.event) return error("revert requires --memory or --event");
if (options.memory && options.event) return error("Use either --memory or --event, not both");
}
if (command === "rejected" && options.since && !isValidSince(options.since)) {
return error(`Invalid --since value: ${options.since}`);
}
return { ok: true, command, options, deprecationNotice };
}
+16
View File
@@ -0,0 +1,16 @@
export const VISIBLE_COMMANDS = ["status", "rejected", "missing", "explain", "commands", "quality", "revert"] as const;
export const HIDDEN_COMMANDS = ["coverage", "audit"] as const;
export type VisibleCommand = typeof VISIBLE_COMMANDS[number];
export type HiddenCommand = typeof HIDDEN_COMMANDS[number];
export type Command = VisibleCommand | HiddenCommand;
export const HIDDEN_COMMAND_NOTICES: Partial<Record<Command, string>> = {
coverage: "Note: 'coverage' is a maintainer-only diagnostic and is not part of the public CLI surface.",
audit: "Note: 'audit' is a maintainer-only diagnostic and is not part of the public CLI surface.",
};
export function isCommand(value: string | undefined): value is Command {
return value !== undefined
&& ([...VISIBLE_COMMANDS, ...HIDDEN_COMMANDS] as readonly string[]).includes(value);
}
+24
View File
@@ -0,0 +1,24 @@
import { runAudit } from "./commands/audit.ts";
import { runCommands } from "./commands/commands.ts";
import { runCoverage } from "./commands/coverage.ts";
import { runExplain } from "./commands/explain.ts";
import { runMissing } from "./commands/missing.ts";
import { runQuality } from "./commands/quality.ts";
import { runRejected } from "./commands/rejected.ts";
import { runRevert } from "./commands/revert.ts";
import { runStatus } from "./commands/status.ts";
import type { CliOptions, Command, CommandResult } from "./types.ts";
export async function dispatch(command: Command, options: CliOptions): Promise<CommandResult> {
switch (command) {
case "status": return runStatus(options);
case "rejected": return runRejected(options);
case "missing": return runMissing(options);
case "coverage": return runCoverage(options);
case "audit": return runAudit(options);
case "explain": return runExplain(options);
case "commands": return runCommands(options);
case "quality": return runQuality(options);
case "revert": return runRevert(options);
}
}
+24
View File
@@ -0,0 +1,24 @@
import { HARD_QUALITY_REASONS } from "../../../src/memory-quality.ts";
import { formatMigrationAudit, type MigrationAuditReport } from "../formatters/audit.ts";
import { readJSONLFile } from "../io.ts";
import { migrationIdFromPath, migrationLogPaths, riskySupersedeReasons } from "../migrations-model.ts";
import type { CliOptions, CommandResult, MigrationLogRecord } from "../types.ts";
export async function runAudit(options: CliOptions): Promise<CommandResult> {
const paths = await migrationLogPaths(options);
const reports: MigrationAuditReport[] = [];
for (const path of paths) {
const migrationId = options.migration ?? migrationIdFromPath(path);
const { records, invalidLines } = await readJSONLFile<MigrationLogRecord>(path);
const superseded = records.filter(record => !record.afterStatus || record.afterStatus === "superseded");
const hardReasons = superseded.flatMap(record => {
if (Array.isArray(record.hardReasons)) return record.hardReasons;
return Array.isArray(record.reasons) ? record.reasons.filter(reason => HARD_QUALITY_REASONS.has(reason)) : [];
});
const risky = superseded
.map(record => ({ record, reasons: riskySupersedeReasons(record) }))
.filter(item => item.reasons.length > 0);
reports.push({ migrationId, path, invalidLines, superseded, hardReasons, risky });
}
return { stdout: formatMigrationAudit(reports, { raw: options.raw }) };
}
+453
View File
@@ -0,0 +1,453 @@
import { queryEvidenceEvents, type EvidenceEventV1, type EvidenceOutcome } from "../../../src/evidence-log.ts";
import { workspaceKey, workspaceMemoryPath } from "../../../src/paths.ts";
import type { WorkspaceMemoryStore } from "../../../src/types.ts";
import { accountWorkspaceMemoryRender } from "../../../src/workspace-memory.ts";
import { readJSONFile } from "../io.ts";
import { objectFromCounts, sortedCounts } from "../text.ts";
import type { CliOptions, CommandResult } from "../types.ts";
import { normalizedStore } from "../workspace-snapshot.ts";
type CommandKind = "reinforce" | "replace";
type MemoryCommandSummary = {
version: 1;
generatedAt: string;
compactionsWithCommandEvidence: number;
commands: Record<CommandKind, number>;
outcomes: Record<"reinforced" | "superseded" | "rejected" | "blocked", number>;
invalidMalformedCommands: number;
replacements: {
sameType: number;
crossType: number;
};
protectedReplacements: {
total: number;
protectedReinforcedTarget: number;
protectedMemorySource: number;
};
rejectionReasons: Record<string, number>;
latestEvents: Array<{
eventId: string;
createdAt: string;
type: string;
outcome: EvidenceOutcome;
ref?: string;
memoryId?: string;
reasonCodes: string[];
textPreview?: string;
}>;
};
type MemoryCommandDetail = {
version: 1;
generatedAt: string;
memoryId: string;
current: {
present: boolean;
status?: string;
renderStatus?: "rendered" | "not_rendered" | "unknown";
type?: string;
source?: string;
};
summary: {
attempts: number;
reinforced: number;
rejectedOrBlocked: number;
windowBlocked: number;
blocksByReason: Record<string, number>;
blockDetailsMissing: number;
refs: string[];
sameSessionCrossUtcDayBlocks: number;
};
events: Array<{
eventId: string;
createdAt: string;
outcome: EvidenceOutcome;
ref?: string;
blockReason?: string;
reasonCodes: string[];
attemptedAtIso?: string;
lastReinforcedAtIso?: string;
elapsedMs?: number;
requiredElapsedMs?: number;
sameSession?: boolean;
legacyMissingTimestamp?: boolean;
reinforcementMode?: string;
crossUtcDay?: boolean | "unknown";
producerVersion?: string;
instrumentationVersion?: number;
}>;
};
const INVALID_COMMAND_REASONS = new Set([
"invalid_memory_command",
"invalid_memory_ref",
"invalid_memory_type",
"empty_replacement_text",
]);
function hasReason(event: EvidenceEventV1, reason: string): boolean {
return event.reasonCodes.includes(reason);
}
function isInvalidMalformedCommandEvent(event: EvidenceEventV1): boolean {
return event.type === "extraction_candidate_rejected"
&& event.reasonCodes.some(reason => INVALID_COMMAND_REASONS.has(reason));
}
function isParsedCommandEvent(event: EvidenceEventV1): boolean {
return event.type === "memory_reinforced" || event.type === "memory_replaced_numbered_ref";
}
function isManualRevertEvent(event: EvidenceEventV1): boolean {
return event.type === "memory_reverted_numbered_ref";
}
function isCommandEvidenceEvent(event: EvidenceEventV1): boolean {
return isParsedCommandEvent(event) || isInvalidMalformedCommandEvent(event) || isManualRevertEvent(event);
}
function refFromEvent(event: EvidenceEventV1): string | undefined {
const ref = event.details?.ref;
return typeof ref === "string" ? ref : undefined;
}
function isReinforcementEvent(event: EvidenceEventV1): boolean {
return event.type === "memory_reinforced";
}
function stringDetail(event: EvidenceEventV1, key: string): string | undefined {
const value = event.details?.[key];
return typeof value === "string" && value.length > 0 ? value : undefined;
}
function numberDetail(event: EvidenceEventV1, key: string): number | undefined {
const value = event.details?.[key];
return typeof value === "number" && Number.isFinite(value) ? value : undefined;
}
function booleanDetail(event: EvidenceEventV1, key: string): boolean | undefined {
const value = event.details?.[key];
return typeof value === "boolean" ? value : undefined;
}
function isRejectedOrBlocked(event: EvidenceEventV1): boolean {
return event.outcome === "rejected" || hasReason(event, "reinforcement_window_blocked");
}
function blockReasonFor(event: EvidenceEventV1): string | undefined {
if (!isRejectedOrBlocked(event)) return undefined;
return stringDetail(event, "blockReason") ?? "unknown";
}
function isCrossUtcDay(attemptedAtIso: string | undefined, lastReinforcedAtIso: string | undefined): boolean | "unknown" {
if (!attemptedAtIso || !lastReinforcedAtIso) return "unknown";
const attempted = new Date(attemptedAtIso);
const lastReinforced = new Date(lastReinforcedAtIso);
if (Number.isNaN(attempted.getTime()) || Number.isNaN(lastReinforced.getTime())) return "unknown";
return attempted.toISOString().slice(0, 10) !== lastReinforced.toISOString().slice(0, 10);
}
async function currentMemoryStatus(root: string, memoryId: string): Promise<MemoryCommandDetail["current"]> {
const rawStore = await readJSONFile<WorkspaceMemoryStore>(await workspaceMemoryPath(root));
const storeRoot = rawStore?.workspace?.root ?? root;
const storeKey = rawStore?.workspace?.key ?? await workspaceKey(root);
const store = normalizedStore(rawStore, storeRoot, storeKey);
const activeEntry = store.entries.find(entry => entry.id === memoryId && entry.status !== "superseded");
const renderAccounting = accountWorkspaceMemoryRender(store);
const renderedIds = new Set(renderAccounting.rendered.map(memory => memory.id));
const omittedIds = new Set(renderAccounting.omitted.map(item => item.memory.id));
const renderStatus = renderedIds.has(memoryId)
? "rendered"
: omittedIds.has(memoryId)
? "not_rendered"
: "unknown";
if (!activeEntry) {
return { present: false, renderStatus };
}
return {
present: true,
status: activeEntry.status,
renderStatus,
type: activeEntry.type,
source: activeEntry.source,
};
}
function detailEventJSON(event: EvidenceEventV1): MemoryCommandDetail["events"][number] {
const attemptedAtIso = stringDetail(event, "attemptedAtIso");
const lastReinforcedAtIso = stringDetail(event, "lastReinforcedAtIso");
const blocked = isRejectedOrBlocked(event);
const blockReason = blockReasonFor(event);
return {
eventId: event.eventId,
createdAt: event.createdAt,
outcome: event.outcome,
ref: refFromEvent(event),
blockReason,
reasonCodes: event.reasonCodes,
attemptedAtIso,
lastReinforcedAtIso,
elapsedMs: numberDetail(event, "elapsedMs"),
requiredElapsedMs: numberDetail(event, "requiredElapsedMs"),
sameSession: booleanDetail(event, "sameSession"),
legacyMissingTimestamp: booleanDetail(event, "legacyMissingTimestamp") === true ? true : undefined,
reinforcementMode: stringDetail(event, "reinforcementMode"),
crossUtcDay: blocked ? isCrossUtcDay(attemptedAtIso, lastReinforcedAtIso) : undefined,
producerVersion: event.producerVersion,
instrumentationVersion: event.instrumentationVersion,
};
}
export async function buildMemoryCommandDetail(
root: string,
memoryId: string,
events: EvidenceEventV1[],
generatedAt = new Date().toISOString(),
): Promise<MemoryCommandDetail> {
const reinforcementEvents = events.filter(isReinforcementEvent);
const blockReasonCounts = new Map<string, number>();
const refs = new Set<string>();
let blockDetailsMissing = 0;
let sameSessionCrossUtcDayBlocks = 0;
for (const event of reinforcementEvents) {
const ref = refFromEvent(event);
if (ref) refs.add(ref);
if (!isRejectedOrBlocked(event)) continue;
const blockReason = blockReasonFor(event) ?? "unknown";
blockReasonCounts.set(blockReason, (blockReasonCounts.get(blockReason) ?? 0) + 1);
if (!stringDetail(event, "blockReason")) blockDetailsMissing += 1;
if (blockReason === "same_session" && isCrossUtcDay(stringDetail(event, "attemptedAtIso"), stringDetail(event, "lastReinforcedAtIso")) === true) {
sameSessionCrossUtcDayBlocks += 1;
}
}
return {
version: 1,
generatedAt,
memoryId,
current: await currentMemoryStatus(root, memoryId),
summary: {
attempts: reinforcementEvents.length,
reinforced: reinforcementEvents.filter(event => event.outcome === "reinforced").length,
rejectedOrBlocked: reinforcementEvents.filter(isRejectedOrBlocked).length,
windowBlocked: reinforcementEvents.filter(event => hasReason(event, "reinforcement_window_blocked")).length,
blocksByReason: objectFromCounts(blockReasonCounts),
blockDetailsMissing,
refs: [...refs].sort(),
sameSessionCrossUtcDayBlocks,
},
events: reinforcementEvents.map(detailEventJSON),
};
}
function latestEventJSON(event: EvidenceEventV1): MemoryCommandSummary["latestEvents"][number] {
return {
eventId: event.eventId,
createdAt: event.createdAt,
type: event.type,
outcome: event.outcome,
ref: refFromEvent(event),
memoryId: event.memory?.memoryId,
reasonCodes: event.reasonCodes,
textPreview: event.textPreview,
};
}
export function buildMemoryCommandSummary(events: EvidenceEventV1[], generatedAt = new Date().toISOString()): MemoryCommandSummary {
const commandEvents = events.filter(isCommandEvidenceEvent);
const compactionCommandEvents = commandEvents.filter(event => !isManualRevertEvent(event));
const parsedEvents = compactionCommandEvents.filter(isParsedCommandEvent);
const invalidEvents = compactionCommandEvents.filter(isInvalidMalformedCommandEvent);
const sessions = new Set(compactionCommandEvents.map(event => event.sessionHash).filter((value): value is string => typeof value === "string" && value.length > 0));
const replacementSuccesses = parsedEvents.filter(event => event.type === "memory_replaced_numbered_ref" && event.outcome === "superseded");
const rejectedCommandEvents = commandEvents.filter(event => event.outcome === "rejected");
const rejectionReasonCounts = new Map<string, number>();
for (const event of rejectedCommandEvents) {
for (const reason of event.reasonCodes) {
rejectionReasonCounts.set(reason, (rejectionReasonCounts.get(reason) ?? 0) + 1);
}
}
const protectedReinforcedTarget = parsedEvents.filter(event => event.type === "memory_replaced_numbered_ref" && hasReason(event, "protected_reinforced_target")).length;
const protectedMemorySource = parsedEvents.filter(event => event.type === "memory_replaced_numbered_ref" && hasReason(event, "protected_memory_source")).length;
const parsedRejected = parsedEvents.filter(event => event.outcome === "rejected").length;
return {
version: 1,
generatedAt,
compactionsWithCommandEvidence: sessions.size > 0 ? sessions.size : compactionCommandEvents.length > 0 ? 1 : 0,
commands: {
reinforce: parsedEvents.filter(event => event.type === "memory_reinforced").length,
replace: parsedEvents.filter(event => event.type === "memory_replaced_numbered_ref").length,
},
outcomes: {
reinforced: parsedEvents.filter(event => event.outcome === "reinforced").length,
superseded: parsedEvents.filter(event => event.outcome === "superseded").length,
rejected: parsedRejected,
blocked: parsedRejected,
},
invalidMalformedCommands: invalidEvents.length,
replacements: {
sameType: replacementSuccesses.filter(event => hasReason(event, "same_type_replace")).length,
crossType: replacementSuccesses.filter(event => hasReason(event, "cross_type_replace")).length,
},
protectedReplacements: {
total: parsedEvents.filter(event => event.type === "memory_replaced_numbered_ref" && (hasReason(event, "protected_reinforced_target") || hasReason(event, "protected_memory_source"))).length,
protectedReinforcedTarget,
protectedMemorySource,
},
rejectionReasons: objectFromCounts(rejectionReasonCounts),
latestEvents: commandEvents.slice(-10).reverse().map(latestEventJSON),
};
}
function formatReasonCounts(rejectionReasons: Record<string, number>): string[] {
const counts = new Map(Object.entries(rejectionReasons));
const rows = sortedCounts(counts);
if (rows.length === 0) return [" (none)"];
return rows.map(([reason, count]) => ` - ${reason}: ${count}`);
}
function formatLatestEvents(events: MemoryCommandSummary["latestEvents"]): string[] {
if (events.length === 0) return [" (none)"];
return events.map(event => {
const ref = event.ref ? ` ref=${event.ref}` : "";
const memoryId = event.memoryId ? ` memory=${event.memoryId}` : "";
const textPreview = event.textPreview ? ` text=${JSON.stringify(event.textPreview)}` : "";
return ` - ${event.createdAt} ${event.type} ${event.outcome}${ref}${memoryId} reasons=${event.reasonCodes.join(",") || "none"}${textPreview}`;
});
}
function formatInlineCounts(counts: Record<string, number>): string {
const rows = sortedCounts(new Map(Object.entries(counts)));
return rows.length > 0 ? rows.map(([reason, count]) => `${reason}=${count}`).join(", ") : "(none)";
}
function formatCrossUtcDay(value: boolean | "unknown" | undefined): string {
if (value === true) return "yes";
if (value === false) return "no";
return "unknown";
}
function formatBoolean(value: boolean): string {
return value ? "yes" : "no";
}
function formatMemoryCommandDetailEvents(events: MemoryCommandDetail["events"]): string[] {
if (events.length === 0) return [" (none)"];
return events.map(event => {
const ref = event.ref ? ` ref=${event.ref}` : "";
const blockReason = event.blockReason ? ` blockReason=${event.blockReason}` : "";
const reinforcementMode = event.reinforcementMode ? ` reinforcementMode=${event.reinforcementMode}` : "";
const attemptedAt = event.attemptedAtIso ? ` attemptedAt=${event.attemptedAtIso}` : "";
const lastReinforcedAt = event.lastReinforcedAtIso ? ` lastReinforcedAt=${event.lastReinforcedAtIso}` : "";
const elapsedMs = event.elapsedMs !== undefined ? ` elapsedMs=${event.elapsedMs}` : "";
const requiredElapsedMs = event.requiredElapsedMs !== undefined ? ` requiredElapsedMs=${event.requiredElapsedMs}` : "";
const sameSession = event.sameSession !== undefined ? ` sameSession=${formatBoolean(event.sameSession)}` : "";
const legacyMissingTimestamp = event.legacyMissingTimestamp === true ? " legacyMissingTimestamp=yes" : "";
const crossUtcDay = event.crossUtcDay !== undefined ? ` crossUtcDay=${formatCrossUtcDay(event.crossUtcDay)}` : "";
return ` - ${event.createdAt} outcome=${event.outcome}${ref}${blockReason}${reinforcementMode}${attemptedAt}${lastReinforcedAt}${elapsedMs}${requiredElapsedMs}${sameSession}${legacyMissingTimestamp}${crossUtcDay} reasons=${event.reasonCodes.join(",") || "none"}`;
});
}
export function formatMemoryCommandDetail(detail: MemoryCommandDetail, options: Pick<CliOptions, "verbose"> = {}): string {
const current = detail.current;
const lines = [
`Memory command diagnostics for ${detail.memoryId}`,
"",
"Current memory:",
` - present: ${current.present ? "yes" : "no"}`,
` - status: ${current.status ?? "unknown"}`,
` - render: ${current.renderStatus ?? "unknown"}`,
];
if (current.type) lines.push(` - type: ${current.type}`);
if (current.source) lines.push(` - source: ${current.source}`);
lines.push("");
if (detail.summary.attempts === 0) {
lines.push(`No reinforcement command evidence found for ${detail.memoryId}.`);
return lines.join("\n");
}
lines.push(
"Reinforcement summary:",
` - attempts: ${detail.summary.attempts}`,
` - reinforced: ${detail.summary.reinforced}`,
` - rejected/blocked: ${detail.summary.rejectedOrBlocked}`,
` - window blocked: ${detail.summary.windowBlocked}`,
` - block reasons: ${formatInlineCounts(detail.summary.blocksByReason)}`,
` - block details missing: ${detail.summary.blockDetailsMissing}`,
` - same-session cross UTC day blocks: ${detail.summary.sameSessionCrossUtcDayBlocks}`,
` - refs: ${detail.summary.refs.length > 0 ? detail.summary.refs.join(", ") : "(none)"}`,
"",
);
const eventRows = options.verbose ? detail.events : detail.events.slice(-10).reverse();
if (!options.verbose && detail.events.length > eventRows.length) {
lines.push(`Latest reinforcement events (showing ${eventRows.length} of ${detail.events.length}):`);
} else {
lines.push("Latest reinforcement events:");
}
lines.push(...formatMemoryCommandDetailEvents(eventRows));
return lines.join("\n");
}
export function formatMemoryCommandSummary(summary: MemoryCommandSummary, options: Pick<CliOptions, "verbose" | "noEmoji"> = {}): string {
const warning = options.noEmoji ? "!" : "⚠";
const lines = [
"Memory command diagnostics",
"",
"Key metrics:",
` - compactions with command evidence: ${summary.compactionsWithCommandEvidence}`,
` - reinforce: ${summary.commands.reinforce}`,
` - replace: ${summary.commands.replace}`,
` - reinforced: ${summary.outcomes.reinforced}`,
` - superseded: ${summary.outcomes.superseded}`,
` - rejected: ${summary.outcomes.rejected}`,
` - blocked: ${summary.outcomes.blocked}`,
` - invalid/malformed commands: ${summary.invalidMalformedCommands}`,
` - same-type replacements: ${summary.replacements.sameType}`,
` - cross-type replacements: ${summary.replacements.crossType}`,
` - ${warning} Protected REPLACE blocked: ${summary.protectedReplacements.total} (reinforced: ${summary.protectedReplacements.protectedReinforcedTarget}, source: ${summary.protectedReplacements.protectedMemorySource})`,
"",
"Rejection reasons:",
...formatReasonCounts(summary.rejectionReasons),
];
if (options.verbose) {
lines.push("", "Latest command events:", ...formatLatestEvents(summary.latestEvents));
}
return lines.join("\n");
}
export async function runCommands(options: CliOptions): Promise<CommandResult> {
const root = options.workspace ?? process.cwd();
if (options.memory) {
const events = await queryEvidenceEvents(root, { memoryId: options.memory });
const detail = await buildMemoryCommandDetail(root, options.memory, events);
if (options.json) {
return { stdout: JSON.stringify(detail, null, 2) };
}
return { stdout: formatMemoryCommandDetail(detail, options) };
}
const events = await queryEvidenceEvents(root);
const summary = buildMemoryCommandSummary(events);
if (options.json) {
return { stdout: JSON.stringify(summary, null, 2) };
}
return { stdout: formatMemoryCommandSummary(summary, options) };
}
+14
View File
@@ -0,0 +1,14 @@
import { buildInspectionReadModel, coverageRows } from "../inspection-model.ts";
import { buildCoverageJSON, formatCoverage } from "../formatters/coverage.ts";
import type { CliOptions, CommandResult } from "../types.ts";
export async function runCoverage(options: CliOptions): Promise<CommandResult> {
const model = await buildInspectionReadModel(options);
const rows = coverageRows(model, options.includeHistorical === true);
if (options.json) {
return { stdout: JSON.stringify(buildCoverageJSON(rows), null, 2) };
}
return { stdout: formatCoverage(rows) };
}
+19
View File
@@ -0,0 +1,19 @@
import { traceMemoryLifecycle } from "../../../src/evidence-log.ts";
import { formatExplain } from "../formatters/explain.ts";
import { formatTrace } from "../formatters/trace.ts";
import { snapshotForOptions } from "../workspace-snapshot.ts";
import type { CliOptions, CommandResult } from "../types.ts";
export async function runExplain(options: CliOptions): Promise<CommandResult> {
if (options.memory) {
const root = options.workspace ?? process.cwd();
const [snapshot, trace] = await Promise.all([
snapshotForOptions(options),
traceMemoryLifecycle(root, { memoryId: options.memory }),
]);
return { stdout: formatTrace(options.memory, snapshot, trace) };
}
const snapshot = await snapshotForOptions(options);
return { stdout: formatExplain(snapshot) };
}
+14
View File
@@ -0,0 +1,14 @@
import { buildInspectionReadModel, disappearanceRows } from "../inspection-model.ts";
import { buildMissingJSON, formatMissing } from "../formatters/missing.ts";
import type { CliOptions, CommandResult } from "../types.ts";
export async function runMissing(options: CliOptions): Promise<CommandResult> {
const model = await buildInspectionReadModel(options);
const rows = disappearanceRows(model);
if (options.json) {
return { stdout: JSON.stringify(buildMissingJSON(rows, { explain: options.explain }), null, 2) };
}
return { stdout: formatMissing(rows, { verbose: options.verbose, explain: options.explain }) };
}
+19
View File
@@ -0,0 +1,19 @@
import { buildQualityJSON, formatQualityReviewBoard } from "../formatters/quality.ts";
import { buildInspectionReadModel } from "../inspection-model.ts";
import { buildQualityReviewBoard } from "../quality-review-model.ts";
import type { CliOptions, CommandResult } from "../types.ts";
export async function runQuality(options: CliOptions): Promise<CommandResult> {
const model = await buildInspectionReadModel(options);
const report = buildQualityReviewBoard(model, {
verbose: options.verbose,
raw: options.raw,
json: options.json,
});
if (options.json) {
return { stdout: JSON.stringify(buildQualityJSON(report, options.raw), null, 2) };
}
return { stdout: formatQualityReviewBoard(report, { verbose: options.verbose }) };
}
+25
View File
@@ -0,0 +1,25 @@
import { buildRejectedJSON, formatRejected } from "../formatters/rejected.ts";
import { loadRejectionRecords, rejectionFalsePositiveRisk, rejectionQualitySummary } from "../rejections-model.ts";
import type { CliOptions, CommandResult } from "../types.ts";
export async function runRejected(options: CliOptions): Promise<CommandResult> {
const { path, invalidLines, records } = await loadRejectionRecords(options);
const summary = rejectionQualitySummary(records);
const falsePositiveRisk = rejectionFalsePositiveRisk(summary);
if (options.json) {
return { stdout: JSON.stringify(buildRejectedJSON({ summary, falsePositiveRisk }), null, 2) };
}
return {
stdout: formatRejected({
path,
invalidLines,
records,
summary,
falsePositiveRisk,
raw: options.raw,
verbose: options.verbose,
}),
};
}
+193
View File
@@ -0,0 +1,193 @@
import { appendEvidenceEvents, queryEvidenceEvents, type EvidenceEventInput, type EvidenceEventV1, type MemoryEvidenceRef } from "../../../src/evidence-log.ts";
import { workspaceMemoryPath } from "../../../src/paths.ts";
import type { LongTermMemoryEntry, WorkspaceMemoryStore } from "../../../src/types.ts";
import { updateWorkspaceMemoryWithAccounting } from "../../../src/workspace-memory.ts";
import { readJSONFile } from "../io.ts";
import { cleanText, truncate } from "../text.ts";
import { CliInputError, type CliOptions, type CommandResult } from "../types.ts";
type ReplacementLink = {
event: EvidenceEventV1;
originalId: string;
replacementId: string;
};
type RevertPlan = ReplacementLink & {
original: LongTermMemoryEntry;
replacement: LongTermMemoryEntry;
};
function reject(message: string): never {
throw new CliInputError(`revert rejected: ${message}`);
}
function memoryRef(memory: LongTermMemoryEntry, status: LongTermMemoryEntry["status"] = memory.status): MemoryEvidenceRef {
return {
memoryId: memory.id,
type: memory.type,
source: memory.source,
status,
};
}
function replacementIdFromEvent(event: EvidenceEventV1): string | undefined {
return event.relations?.find(relation => relation.role === "superseded_by")?.memory?.memoryId;
}
function originalIdFromEvent(event: EvidenceEventV1): string | undefined {
return event.memory?.memoryId
?? event.relations?.find(relation => relation.role === "superseded")?.memory?.memoryId;
}
function replacementLinkFromEvent(event: EvidenceEventV1): ReplacementLink {
if (event.type !== "memory_replaced_numbered_ref") {
reject(`event ${event.eventId} is not a memory_replaced_numbered_ref event`);
}
if (event.outcome !== "superseded") {
reject(`event ${event.eventId} is not a successful numbered replacement`);
}
if (!event.reasonCodes.includes("numbered_ref_replace")) {
reject(`event ${event.eventId} is not a numbered replacement`);
}
const originalId = originalIdFromEvent(event);
const replacementId = replacementIdFromEvent(event);
if (!originalId || !replacementId) {
reject(`event ${event.eventId} does not identify original and replacement memories`);
}
return { event, originalId, replacementId };
}
function selectReplacementLink(events: EvidenceEventV1[], options: CliOptions): ReplacementLink {
if (options.event) {
const event = events.find(item => item.eventId === options.event);
if (!event) reject(`event ${options.event} was not found`);
return replacementLinkFromEvent(event);
}
const memoryId = options.memory;
if (!memoryId) reject("missing --memory or --event selector");
const matches = events
.filter(event => event.type === "memory_replaced_numbered_ref")
.filter(event => replacementIdFromEvent(event) === memoryId);
if (matches.length === 0) {
reject(`replacement memory ${memoryId} was not created by memory_replaced_numbered_ref`);
}
if (matches.length > 1) {
reject(`replacement memory ${memoryId} has ${matches.length} replacement events; use --event`);
}
return replacementLinkFromEvent(matches[0]);
}
function validatePlan(link: ReplacementLink, store: WorkspaceMemoryStore): RevertPlan {
const byId = new Map(store.entries.map(entry => [entry.id, entry]));
const original = byId.get(link.originalId);
const replacement = byId.get(link.replacementId);
if (!original) reject(`original memory ${link.originalId} is missing`);
if (!replacement) reject(`replacement memory ${link.replacementId} is missing`);
if (original.status !== "superseded") reject(`original memory ${original.id} is not superseded`);
if (replacement.status !== "active") reject(`replacement memory ${replacement.id} is not active`);
const laterSuperseder = store.entries.find(entry =>
entry.status === "active"
&& entry.id !== original.id
&& entry.id !== replacement.id
&& (entry.supersedes ?? []).includes(replacement.id)
);
if (laterSuperseder) {
reject(`replacement memory ${replacement.id} is superseded by active memory ${laterSuperseder.id}`);
}
return { ...link, original, replacement };
}
async function dryRunPlan(root: string, link: ReplacementLink): Promise<RevertPlan> {
const rawStore = await readJSONFile<WorkspaceMemoryStore>(await workspaceMemoryPath(root));
const store: WorkspaceMemoryStore = rawStore ?? {
version: 1,
workspace: { root, key: "" },
limits: { maxRenderedChars: 0, maxEntries: 0 },
entries: [],
migrations: [],
updatedAt: new Date(0).toISOString(),
};
return validatePlan(link, store);
}
function revertEvidence(plan: RevertPlan): EvidenceEventInput {
const replacement = { ...plan.replacement, status: "superseded" as const };
const original = { ...plan.original, status: "active" as const };
return {
type: "memory_reverted_numbered_ref",
phase: "storage",
outcome: "recovered",
memory: memoryRef(replacement, "superseded"),
relations: [
{ role: "superseded", memory: memoryRef(replacement, "superseded") },
{ role: "recovered", memory: memoryRef(original, "active") },
],
reasonCodes: ["manual_revert_numbered_ref"],
details: {
replacementEventId: plan.event.eventId,
replacementMemoryId: plan.replacementId,
restoredMemoryId: plan.originalId,
},
textPreview: original.text,
};
}
async function applyPlan(root: string, link: ReplacementLink): Promise<RevertPlan> {
let applied: RevertPlan | undefined;
const updateResult = await updateWorkspaceMemoryWithAccounting(root, store => {
const plan = validatePlan(link, store);
const nowIso = new Date().toISOString();
applied = {
...plan,
original: { ...plan.original, status: "active", updatedAt: nowIso },
replacement: { ...plan.replacement, status: "superseded", updatedAt: nowIso },
};
return {
...store,
entries: store.entries.map(entry => {
if (entry.id === plan.originalId) return applied!.original;
if (entry.id === plan.replacementId) return applied!.replacement;
return entry;
}),
updatedAt: nowIso,
lastActivityAt: nowIso,
};
});
if (!applied) reject("unable to apply revert");
await appendEvidenceEvents(root, [...updateResult.evidence, revertEvidence(applied)]);
return applied;
}
function formatPlan(plan: RevertPlan, applied: boolean): string {
const heading = applied ? "Memory revert applied" : "Memory revert dry run";
const nextStep = applied ? "Changes applied." : "No changes applied. Re-run with --apply to mutate workspace memory.";
return [
heading,
"",
"Planned changes:",
` - replacement: ${plan.replacementId} active -> superseded`,
` - original: ${plan.originalId} superseded -> active`,
` - replacement event: ${plan.event.eventId}`,
` - restored text: ${truncate(cleanText(plan.original.text, false), 100)}`,
"",
nextStep,
].join("\n");
}
export async function runRevert(options: CliOptions): Promise<CommandResult> {
const root = options.workspace ?? process.cwd();
const events = await queryEvidenceEvents(root);
const link = selectReplacementLink(events, options);
const plan = options.apply ? await applyPlan(root, link) : await dryRunPlan(root, link);
return { stdout: formatPlan(plan, options.apply === true) };
}
+112
View File
@@ -0,0 +1,112 @@
import { RETENTION_TYPE_MAX, WORKSPACE_DORMANT_AFTER_DAYS } from "../../../src/retention.ts";
import { TYPES } from "../constants.ts";
import { formatStatus, type StatusReadout } from "../formatters/status.ts";
import { buildInspectionReadModel, disappearanceRows } from "../inspection-model.ts";
import { retentionCandidatesForDiag, retentionClockSummary, daysSinceIso } from "../retention-model.ts";
import { rejectionFalsePositiveRisk, rejectionQualitySummary } from "../rejections-model.ts";
import { isWithinDays, memoryDiagJSONFromSnapshot } from "../workspace-snapshot.ts";
import type { CliOptions, CommandResult, MemoryInspectionReadModel } from "../types.ts";
export function buildStatusReadout(model: MemoryInspectionReadModel, now = Date.now()): StatusReadout {
const active = model.store.entries.filter(entry => entry.status !== "superseded");
const retention = retentionCandidatesForDiag(model.store, now);
const clocks = retentionClockSummary(active);
const disappearances = disappearanceRows(model);
const evidenceCovered = active.filter(entry => (model.evidenceByMemoryId.get(entry.id) ?? []).length > 0).length;
const rejectionSummary = rejectionQualitySummary(model.rejectionRecords);
const falsePositiveRisk = rejectionFalsePositiveRisk(rejectionSummary);
const typeCounts = Object.fromEntries(TYPES.map(type => [type, active.filter(entry => entry.type === type).length]));
const capsFull = active.length >= model.store.limits.maxEntries || TYPES.some(type => (typeCounts[type] ?? 0) >= RETENTION_TYPE_MAX[type]);
const unknownDisappearances = disappearances.filter(row => row.classification === "historical_absent_unknown_reason").length;
const status = unknownDisappearances > 0 || clocks.invalid > 0
? "degraded"
: capsFull || rejectionSummary.legacyUnscopedCount > 0 || falsePositiveRisk === "high"
? "warning"
: "ok";
const rejectedLast7Days = model.evidenceEvents.filter(event => event.outcome === "rejected" && isWithinDays(event.createdAt, 7, now)).length;
const evidenceCoveragePercent = active.length === 0 ? 100 : Math.round((evidenceCovered / active.length) * 100);
const needsAttention: string[] = [];
if (clocks.invalid > 0) needsAttention.push(`${clocks.invalid} invalid retention clocks`);
if (unknownDisappearances > 0) needsAttention.push(`${unknownDisappearances} memories disappeared without terminal evidence`);
if (capsFull) needsAttention.push("memory store is at or over retention caps");
if (rejectionSummary.legacyUnscopedCount > 0) needsAttention.push(`${rejectionSummary.legacyUnscopedCount} legacy unscoped rejection records`);
if (falsePositiveRisk === "high") needsAttention.push("possible rejection false-positive risk is high");
const suggestedNextSteps: string[] = [];
if (clocks.invalid > 0 || unknownDisappearances > 0) suggestedNextSteps.push("Run memory-diag missing --verbose to inspect missing-memory evidence.");
if (capsFull) suggestedNextSteps.push("Run memory-diag explain to see which memories are hidden by caps.");
if (rejectionSummary.legacyUnscopedCount > 0 || falsePositiveRisk === "high") suggestedNextSteps.push("Run memory-diag rejected --verbose to inspect rejection quality.");
const wallDaysSinceActivity = daysSinceIso(model.store.lastActivityAt, now);
const dormantDiscountActive = wallDaysSinceActivity !== null && wallDaysSinceActivity > WORKSPACE_DORMANT_AFTER_DAYS;
const dormantDaysPastGrace = wallDaysSinceActivity === null ? 0 : Math.max(0, wallDaysSinceActivity - WORKSPACE_DORMANT_AFTER_DAYS);
return {
status,
summaryText: `Summary: Workspace memory quality is ${status}: ${active.length} active memories, ${evidenceCovered}/${active.length} with evidence, ${disappearances.length} evidence-only disappearances (${unknownDisappearances} unknown), ${clocks.invalid} invalid retention clocks, and ${rejectionSummary.legacyUnscopedCount} legacy unscoped rejection records.`,
active: active.length,
rendered: retention.rendered.length,
pending: model.pending.entries.length,
rejectedLast7Days,
evidenceCoveragePercent,
needsAttention,
suggestedNextSteps,
caps: {
active: active.length,
maxEntries: model.store.limits.maxEntries,
rendered: retention.rendered.length,
typeCapped: retention.typeCapped.length,
globalCapped: retention.globalCapped.length,
typeCounts,
capsFull,
},
retention: clocks,
evidence: {
currentWithEvidence: evidenceCovered,
currentWithoutEvidence: active.length - evidenceCovered,
evidenceMemoryIds: model.evidenceByMemoryId.size,
disappearances: disappearances.length,
unknownDisappearances,
withTerminalReason: disappearances.length - unknownDisappearances,
},
rejections: { ...rejectionSummary, falsePositiveRisk },
dormant: {
lastActivityAt: model.store.lastActivityAt,
wallDaysSinceActivity,
dormantDiscountActive,
dormantDaysPastGrace,
},
topRendered: retention.rendered.slice(0, 5),
weakestActive: retention.sorted.slice(-5).reverse(),
};
}
export async function runStatus(options: CliOptions): Promise<CommandResult> {
const now = Date.now();
const model = await buildInspectionReadModel(options);
const readout = buildStatusReadout(model, now);
if (options.json) {
const root = options.workspace ?? process.cwd();
const diag = memoryDiagJSONFromSnapshot(root, model.snapshot);
return {
stdout: JSON.stringify({
...diag,
summary: {
...diag.summary,
status: readout.status,
evidenceCoveragePercent: readout.evidenceCoveragePercent,
needsAttention: readout.needsAttention,
suggestedNextSteps: readout.suggestedNextSteps,
},
}, null, 2),
};
}
return {
stdout: formatStatus(readout, model, {
verbose: options.verbose,
noEmoji: options.noEmoji,
isTty: process.stdout.isTTY === true,
raw: options.raw,
}),
};
}
+21
View File
@@ -0,0 +1,21 @@
import type { LongTermType } from "../../src/types.ts";
import type { Origin } from "./types.ts";
export const TYPES: LongTermType[] = ["feedback", "decision", "project", "reference"];
export const SUSPICIOUS_REASONS = [
"progress_snapshot",
"active_file_snapshot",
"commit_or_ci_snapshot",
"temporary_status",
"raw_error",
"code_or_api_signature",
] as const;
export const ALLOWED_ORIGINS = new Set<Origin>([
"explicit_trigger",
"compaction_candidate",
"manual",
"migration_check",
"unknown",
]);
+21
View File
@@ -0,0 +1,21 @@
import type { EvidenceEventV1 } from "../../src/evidence-log.ts";
import { uniqueStrings } from "./text.ts";
export function eventMemoryIds(event: EvidenceEventV1): string[] {
return uniqueStrings([
event.memory?.memoryId ?? "",
...(event.relations ?? []).map(relation => relation.memory?.memoryId ?? ""),
]);
}
export function groupEvidenceByMemoryId(events: EvidenceEventV1[]): Map<string, EvidenceEventV1[]> {
const grouped = new Map<string, EvidenceEventV1[]>();
for (const event of events) {
for (const id of eventMemoryIds(event)) {
const bucket = grouped.get(id) ?? [];
bucket.push(event);
grouped.set(id, bucket);
}
}
return grouped;
}
+48
View File
@@ -0,0 +1,48 @@
import { cleanPath, cleanText, countBy, formatWorkspaceIdentity, sortedCounts, truncate } from "../text.ts";
import type { MigrationLogRecord } from "../types.ts";
export type MigrationAuditReport = {
migrationId: string;
path: string;
invalidLines: number;
superseded: MigrationLogRecord[];
hardReasons: string[];
risky: Array<{ record: MigrationLogRecord; reasons: string[] }>;
};
export function formatMigrationAudit(reports: MigrationAuditReport[], options: { raw: boolean }): string {
const lines: string[] = [];
lines.push("Migration audit report");
lines.push("");
if (reports.length === 0) {
lines.push("No migration logs found.");
return lines.join("\n");
}
for (let i = 0; i < reports.length; i += 1) {
const report = reports[i];
if (i > 0) lines.push("");
lines.push(`Migration: ${report.migrationId}`);
lines.push(`logPath=${cleanPath(report.path, options.raw)}`);
if (report.invalidLines > 0) lines.push(`Invalid JSONL lines skipped: ${report.invalidLines}`);
lines.push(`Superseded entries: ${report.superseded.length}`);
lines.push("");
lines.push("By hard reason:");
const byHardReason = sortedCounts(countBy(report.hardReasons));
if (byHardReason.length === 0) lines.push(" (none)");
else for (const [reason, count] of byHardReason) lines.push(` ${reason.padEnd(24)} ${count}`);
lines.push("");
lines.push("Potentially risky supersedes:");
lines.push(` ${report.risky.length}`);
for (const item of report.risky.slice(0, 10)) {
const record = item.record;
const hard = Array.isArray(record.hardReasons) ? record.hardReasons : [];
const identity = formatWorkspaceIdentity(record.workspaceKey, record.workspaceRoot, options.raw);
lines.push(` - [${record.type ?? "unknown"}] hardReasons=${JSON.stringify(hard)} risk=${item.reasons.join(",")} ${JSON.stringify(truncate(cleanText(record.text ?? "", options.raw)))}`);
if (identity) lines.push(` ${identity}`);
}
}
return lines.join("\n");
}
@@ -0,0 +1,34 @@
import type { coverageRows } from "../inspection-model.ts";
import { countBy, objectFromCounts } from "../text.ts";
import type { CoverageClass } from "../types.ts";
export type CoverageRows = ReturnType<typeof coverageRows>;
export function buildCoverageJSON(rows: CoverageRows, generatedAt = new Date().toISOString()): Record<string, unknown> {
const classCounts = objectFromCounts(countBy(rows.map(row => row.class)));
return {
version: 1,
generatedAt,
classCounts,
memories: rows,
};
}
export function formatCoverage(rows: CoverageRows): string {
const classCounts = objectFromCounts(countBy(rows.map(row => row.class)));
const lines: string[] = [];
lines.push("Memory evidence coverage");
lines.push("");
lines.push("Class counts:");
for (const cls of ["full_lifecycle", "render_only", "no_evidence", "historical_absent_with_reason", "historical_absent_unknown_reason"] as CoverageClass[]) {
lines.push(` ${cls}: ${classCounts[cls] ?? 0}`);
}
lines.push("");
lines.push("Per-memory rows:");
if (rows.length === 0) lines.push(" (none)");
for (const row of rows) {
const phases = row.eventCounts.byPhase;
lines.push(` ${row.id} ${row.class} current=${row.current ? "yes" : "no"} total=${row.eventCounts.total} extraction=${phases.extraction ?? 0} promotion=${phases.promotion ?? 0} render=${phases.render ?? 0} storage=${phases.storage ?? 0}`);
}
return lines.join("\n");
}
+42
View File
@@ -0,0 +1,42 @@
import type { EvidenceEventV1 } from "../../../src/evidence-log.ts";
import { formatStrength } from "../retention-model.ts";
import type { WorkspaceDiagSnapshot } from "../types.ts";
export function formatEvidenceRefs(eventIds: string[], allEvents: EvidenceEventV1[]): string {
if (eventIds.length === 0) return "(none)";
const byId = new Map(allEvents.map(event => [event.eventId, event]));
return eventIds
.map(id => {
const event = byId.get(id);
return event ? `${event.eventId} ${event.type}` : id;
})
.join(", ");
}
export function formatExplain(snapshot: WorkspaceDiagSnapshot): string {
const lines: string[] = [];
lines.push("Workspace memory explainability");
lines.push("");
if (snapshot.memories.length === 0) {
lines.push("No memories found.");
}
for (const memory of snapshot.memories) {
lines.push(`Memory ${memory.id}: ${memory.status}`);
const strength = typeof memory.strength === "number" ? formatStrength(memory.strength) : "n/a";
lines.push(`- strength=${strength}, type=${memory.type}, source=${memory.source}`);
lines.push(`- reasons: ${memory.reasonCodes.length > 0 ? memory.reasonCodes.join(", ") : "(none)"}`);
lines.push(`- evidence: ${formatEvidenceRefs(memory.evidenceEventIds, snapshot.allEvents)}`);
lines.push("");
}
const quarantines = snapshot.recentEvents.filter(event => event.type === "storage_corrupt_json_quarantined");
if (quarantines.length > 0) {
lines.push("Quarantined stores:");
for (const event of quarantines) {
lines.push(`- quarantined_corrupt_store: ${event.eventId} ${event.type}; reasons=${event.reasonCodes.join(",")}`);
}
}
return lines.join("\n");
}
+102
View File
@@ -0,0 +1,102 @@
import type { disappearanceRows } from "../inspection-model.ts";
import { eventCounts } from "../inspection-model.ts";
import { formatDetails } from "../text.ts";
export type DisappearanceRows = ReturnType<typeof disappearanceRows>;
export type MissingSummary = {
total: number;
explained: number;
needsReview: number;
};
export function missingSummary(rows: DisappearanceRows): MissingSummary {
return {
total: rows.length,
explained: rows.filter(row => row.classification === "historical_absent_with_reason").length,
needsReview: rows.filter(row => row.classification === "historical_absent_unknown_reason").length,
};
}
export function buildMissingJSON(rows: DisappearanceRows, options: { explain?: boolean; generatedAt?: string } = {}): Record<string, unknown> {
return {
...buildDisappearancesJSON(rows, options),
summary: missingSummary(rows),
};
}
function buildDisappearancesJSON(rows: DisappearanceRows, options: { explain?: boolean; generatedAt?: string } = {}): Record<string, unknown> {
return {
version: 1,
generatedAt: options.generatedAt ?? new Date().toISOString(),
disappearances: rows.map(row => ({
id: row.id,
classification: row.classification,
terminalType: row.terminalType,
reasonCodes: row.reasonCodes,
eventCounts: eventCounts(row.events),
details: options.explain ? row.event?.details : undefined,
})),
};
}
function formatDisappearances(rows: DisappearanceRows, options: { explain?: boolean } = {}): string {
const lines: string[] = [];
lines.push("Memory disappearances");
lines.push("");
if (rows.length === 0) {
lines.push("No evidence-only memories found.");
return lines.join("\n");
}
for (const row of rows) {
const reasons = row.reasonCodes.length > 0 ? row.reasonCodes.join(",") : "none";
lines.push(`Memory ${row.id}: ${row.classification} terminal=${row.terminalType} reasons=${reasons}`);
if (options.explain) {
lines.push(` events: ${row.events.map(event => event.type).join(", ")}`);
if (row.event?.type === "memory_removed_capacity") {
lines.push(` memory_removed_capacity details: ${formatDetails(row.event.details)}`);
}
const renderTypeCap = row.events.find(event => event.type === "render_omitted" && event.reasonCodes.includes("type_cap"));
if (renderTypeCap) {
lines.push(` render_omitted type-cap observation: reasons=${renderTypeCap.reasonCodes.join(",")} details=${formatDetails(renderTypeCap.details)}`);
}
}
}
return lines.join("\n");
}
export function formatMissing(rows: DisappearanceRows, options: { verbose?: boolean; explain?: boolean } = {}): string {
const summary = missingSummary(rows);
const lines: string[] = [];
lines.push("Missing memory summary");
lines.push("");
if (rows.length === 0) {
lines.push("No missing memories found.");
return lines.join("\n");
}
lines.push(`Total missing: ${summary.total}`);
lines.push(`Explained: ${summary.explained}`);
lines.push(`Needs review: ${summary.needsReview}`);
lines.push("");
const unknownRows = rows
.filter(row => row.classification === "historical_absent_unknown_reason")
.slice(0, 5);
lines.push("Unknown disappearance samples:");
if (unknownRows.length === 0) {
lines.push(" (none)");
} else {
for (const row of unknownRows) {
const reasons = row.reasonCodes.length > 0 ? row.reasonCodes.join(",") : "none";
lines.push(` - ${row.id} terminal=${row.terminalType} reasons=${reasons}`);
}
}
if (options.verbose || options.explain) {
lines.push("");
lines.push(formatDisappearances(rows, { explain: true }));
}
return lines.join("\n");
}
+467
View File
@@ -0,0 +1,467 @@
import { cleanText, formatDetails } from "../text.ts";
import type {
CandidateProvenance,
HeuristicFlag,
ProvenanceClassification,
RejectionVersionFacts,
ReinforcementVersionFacts,
ReviewBoardActiveMemory,
ReviewBoardCandidate,
ReviewBoardReport,
EvictionVersionFacts,
VersionAvailability,
VersionCoverage,
VersionedMechanismFacts,
} from "../quality-review-model.ts";
const PROVENANCE_ORDER: ProvenanceClassification[] = [
"explicit_migration_evidence",
"legacy_unversioned_format",
"reabsorbed_post_rejection",
"suspected_pre_migration_legacy",
"likely_current_behavior",
"unversioned_ambiguous",
];
const REVIEW_FLAG_CAVEAT = "This flag is a prompt for review, not a conclusion.";
export function buildQualityJSON(report: ReviewBoardReport, raw = false): unknown {
if (raw) return report;
return redactUnknown(report);
}
export function formatQualityReviewBoard(
report: ReviewBoardReport,
options: { verbose?: boolean },
): string {
const bullet = "-";
const lines: string[] = [];
lines.push("Memory quality review board");
lines.push("Purpose: evidence for human/agent review only; no automatic judgment or cleanup.");
lines.push("Producer version note: historical records do not include package/plugin version; provenance below is inferred.");
lines.push("Primary review purpose: SYSTEM MECHANISM observations (filters, reinforcement, eviction/caps, identity/dedup).");
lines.push("Secondary review purpose: MEMORY CONTENT quality (staleness, durability, redundancy, specificity).");
lines.push("");
pushEvidenceProvenance(lines, report, bullet);
lines.push("");
pushSystemMechanismFacts(lines, report, bullet);
lines.push("");
pushMemoryContentFacts(lines, report, bullet);
lines.push("");
pushSystemMechanismCandidates(lines, report, bullet);
lines.push("");
pushMemoryContentCandidates(lines, report, bullet, options);
lines.push("");
pushReviewQuestions(lines, report, bullet);
lines.push("");
pushNextCommands(lines, report, bullet);
return lines.join("\n");
}
function pushEvidenceProvenance(lines: string[], report: ReviewBoardReport, bullet: string): void {
const context = report.provenanceContext;
const instrumentation = report.facts.systemMechanisms.instrumentation;
lines.push("Evidence provenance");
lines.push(` ${bullet} method: migration/timestamp/format inference`);
lines.push(` ${bullet} confidence: ${context.confidenceDisclaimer}`);
lines.push(` ${bullet} Producer coverage: ${instrumentation.evidenceEventsWithProducer} of ${instrumentation.evidenceEventsTotal} evidence events instrumented`);
lines.push(` ${bullet} Rejection producer coverage: ${instrumentation.rejectionRecordsWithProducer} of ${instrumentation.rejectionRecordsTotal} rejection records instrumented`);
lines.push(` ${bullet} instrumentation versions: ${formatCounts(instrumentation.instrumentationVersions)}`);
lines.push(` ${bullet} migration timeline: ${formatMigrationTimeline(context.migrationTimeline)}`);
if (context.lastActivityAt) lines.push(` ${bullet} last activity: ${context.lastActivityAt}`);
}
function pushSystemMechanismFacts(lines: string[], report: ReviewBoardReport, bullet: string): void {
const facts = report.facts.systemMechanisms;
lines.push("Facts - system mechanisms");
lines.push(" Provenance counts for mechanism evidence");
lines.push(` ${bullet} ${formatProvenanceCounts(report.provenanceContext.countsByClassification)}`);
lines.push(" Rejection filters");
pushAnswerability(lines, report.answerability?.rejectionFilters, " ");
lines.push(` ${bullet} rejected records: ${facts.rejectionFilters.totalRecords} (unique: ${facts.rejectionFilters.uniqueTexts})`);
lines.push(` ${bullet} raw reason-code distribution: ${formatCounts(facts.rejectionFilters.byRawReasonCode)}`);
lines.push(` ${bullet} type distribution: ${formatCounts(facts.rejectionFilters.byType)}`);
lines.push(` ${bullet} ambiguous/architecture-like rejected candidates: ${facts.rejectionFilters.ambiguousOrArchitectureLike}`);
lines.push(` ${bullet} status-or-hard-reason evidence: ${facts.rejectionFilters.hardReasonOrNoiseHeuristic}`);
lines.push(` ${bullet} re-absorbed rejected texts: ${facts.rejectionFilters.reabsorbedRejectedTexts}`);
if (facts.versionedFacts) pushVersionAnalysis(lines, facts.versionedFacts.rejectionFilters, facts.versionedFacts.versionCoverage, formatRejectionVersionFacts, bullet);
lines.push(" Reinforcement rules");
pushAnswerability(lines, report.answerability?.reinforcementRules, " ");
lines.push(` ${bullet} reinforce attempts: ${facts.reinforcementRules.reinforceEvents}, reinforced: ${facts.reinforcementRules.reinforcedEvents}, rejected/blocked: ${facts.reinforcementRules.rejectedOrBlockedEvents}`);
lines.push(` ${bullet} reinforcement-window blocked: ${facts.reinforcementRules.windowBlockedEvents} (rate: ${formatPercent(facts.reinforcementRules.windowBlockRate)})`);
lines.push(` ${bullet} Exact block reasons: ${formatCounts(facts.reinforcementRules.blocksByExactReason)}`);
lines.push(` ${bullet} window blocks by UTC day: ${formatCounts(facts.reinforcementRules.windowBlocksByUtcDay)}`);
lines.push(` ${bullet} block details missing: ${facts.reinforcementRules.blockDetailsMissing}`);
lines.push(` ${bullet} repeated blocks by memory: ${formatRepeatedBlocks(facts.reinforcementRules.repeatedBlocksByMemory)}`);
lines.push(` ${bullet} malformed command events: ${facts.reinforcementRules.malformedCommandEvents}`);
if (facts.versionedFacts) pushVersionAnalysis(lines, facts.versionedFacts.reinforcementRules, facts.versionedFacts.versionCoverage, formatReinforcementVersionFacts, bullet);
lines.push(" Eviction and caps");
pushAnswerability(lines, report.answerability?.evictionAndCaps, " ");
lines.push(` ${bullet} active memories: ${facts.evictionAndCaps.activeMemories} / ${facts.evictionAndCaps.maxEntries}`);
lines.push(` ${bullet} rendered memories: ${facts.evictionAndCaps.renderedMemories}`);
lines.push(` ${bullet} cap occupancy: ${formatFullCaps(facts.evictionAndCaps.fullCaps, facts.evictionAndCaps.typeCounts, facts.evictionAndCaps.typeCaps, facts.evictionAndCaps.activeMemories, facts.evictionAndCaps.maxEntries)}`);
lines.push(` ${bullet} capacity removals: total=${facts.evictionAndCaps.removedByCapacity}, global=${facts.evictionAndCaps.removedByGlobalCap}, type=${facts.evictionAndCaps.removedByTypeCap}`);
lines.push(` ${bullet} Removals with snapshot: ${facts.evictionAndCaps.recentCapacityRemovalsWithSnapshot}`);
lines.push(` ${bullet} Removals without snapshot: ${facts.evictionAndCaps.capacitySnapshotsMissing} (historical)`);
if (facts.evictionAndCaps.highestRankRemoved) lines.push(` ${bullet} highest-rank removed snapshot: ${formatHighestRankRemoved(facts.evictionAndCaps.highestRankRemoved)}`);
lines.push(` ${bullet} recent evictions by type: ${formatCounts(facts.evictionAndCaps.recentEvictionsByType)}`);
lines.push(` ${bullet} recent evicted content shown: ${facts.evictionAndCaps.recentEvictedContentShown}`);
if (facts.versionedFacts) pushVersionAnalysis(lines, facts.versionedFacts.evictionAndCaps, facts.versionedFacts.versionCoverage, formatEvictionVersionFacts, bullet);
lines.push(" Unknown disappearances");
pushAnswerability(lines, report.answerability?.unknownDisappearances, " ");
lines.push(` ${bullet} unversioned disappearance inventory: evidence-only=${facts.evictionAndCaps.missingEvidenceOnly}, unknown=${facts.evictionAndCaps.unknownDisappearances}`);
lines.push(" Identity and dedup");
pushAnswerability(lines, report.answerability?.identityAndDedup, " ");
lines.push(` ${bullet} replacements: total=${facts.identityAndDedup.replacementEvents}, same-type=${facts.identityAndDedup.sameTypeReplacementEvents}, cross-type=${facts.identityAndDedup.crossTypeReplacementEvents}`);
lines.push(` ${bullet} superseded entries: ${facts.identityAndDedup.supersededEntries}`);
lines.push(` ${bullet} exact duplicate/identity groups identified: ${facts.identityAndDedup.duplicateTextOrIdentityGroups}`);
}
function pushMemoryContentFacts(lines: string[], report: ReviewBoardReport, bullet: string): void {
const facts = report.facts.memoryContent;
lines.push("Facts - memory content");
pushAnswerability(lines, report.answerability?.memoryContent, " ");
lines.push(` ${bullet} rendered memories: ${facts.renderedMemories}`);
lines.push(` ${bullet} evidence coverage: ${facts.evidenceCoverage.covered} / ${facts.evidenceCoverage.total}`);
lines.push(` ${bullet} type counts: ${formatTypeCountsWithCaps(facts.typeCounts, facts.typeCaps)}`);
lines.push(` ${bullet} weakest/strongest active memory previews: weakest=${formatMemoryPreviews(facts.weakestActiveMemories)}; strongest=${formatMemoryPreviews(facts.strongestActiveMemories)}`);
}
function pushAnswerability(
lines: string[],
assessment: NonNullable<ReviewBoardReport["answerability"]>[keyof NonNullable<ReviewBoardReport["answerability"]>] | undefined,
indent: string,
): void {
if (!assessment) return;
const suffix = assessment.level === "partial" ? " — causal fields exist, but human content judgment is still required" : "";
lines.push(`${indent}(Answerability: ${assessment.level}${suffix})`);
lines.push(`${indent}Output permission: ${assessment.outputPermission}`);
}
function pushVersionAnalysis<TFacts>(
lines: string[],
mechanism: VersionedMechanismFacts<TFacts>,
coverage: VersionCoverage,
formatFacts: (facts: TFacts) => string,
bullet: string,
): void {
lines.push(" Version analysis by producer version");
lines.push(` Version-stamp coverage (all evidence/rejection records, not mechanism problem counts): Coverage: ${formatCoveragePercent(coverage.coveragePercent)} of ${formatInteger(coverage.totalEvents)} records carry a version stamp (${formatInteger(coverage.currentVersionEvents)} current, ${formatInteger(coverage.previousVersionEvents)} previous, ${formatInteger(coverage.unknownVersionEvents)} unknown/unversioned). Comparison will become meaningful as new events accumulate.`);
if (coverage.isTransitional) {
lines.push(" NOTE: Version coverage is below 50%. Current-version comparisons may not be representative.");
}
lines.push(` ${mechanismOpportunityDescription(mechanism)}`);
for (const group of ["current", "previous", "unknown_unversioned"] as const) {
const bucket = mechanism.buckets[group];
lines.push(` ${bullet} ${bucket.label}: opportunities=${bucket.opportunityCount}, observed=${bucket.observedPatternCount}, sample=${bucket.sampleAssessment}, answerability=${bucket.answerabilityLevel}`);
if (Object.keys(bucket.producerVersions).length > 0) lines.push(` producer versions: ${formatCounts(bucket.producerVersions)}`);
lines.push(` composition: ${formatVersionAvailability(bucket.versionAvailability)}`);
lines.push(` facts: ${formatFacts(bucket.facts)}`);
}
lines.push(` ${bullet} inference: ${mechanism.inference.message}`);
lines.push(` diagnostic strength: ${diagnosticStrengthLabel(mechanism)}`);
const diagnosticLine = currentMechanismDiagnosticLine(mechanism);
if (diagnosticLine) lines.push(` ${diagnosticLine}`);
if (mechanism.diagnosticQuestions) {
for (const question of mechanism.diagnosticQuestions) {
lines.push(` diagnostic question: ${question.question} Evidence: ${question.evidence.join(", ")}`);
}
}
lines.push(` caveat: ${mechanism.inference.caveat}`);
}
function diagnosticStrengthLabel<TFacts>(mechanism: VersionedMechanismFacts<TFacts>): string {
const current = mechanism.buckets.current;
const strength = mechanism.inference.status === "no_current_version_opportunities" || current.opportunityCount === 0
? "unavailable"
: current.opportunityCount < mechanism.sampleThreshold
? "weak"
: "moderate";
return hasCurrentCausalDetail(mechanism) ? `${strength}; causal detail available` : strength;
}
function hasCurrentCausalDetail<TFacts>(mechanism: VersionedMechanismFacts<TFacts>): boolean {
const currentFacts = mechanism.buckets.current.facts;
return isReinforcementVersionFacts(currentFacts) && Object.keys(currentFacts.blocksByExactReason).length > 0;
}
function currentMechanismDiagnosticLine<TFacts>(mechanism: VersionedMechanismFacts<TFacts>): string | undefined {
const current = mechanism.buckets.current;
if (!isReinforcementVersionFacts(current.facts)) return undefined;
const facts = current.facts;
const parts: string[] = [];
if (Object.keys(facts.blocksByExactReason).length > 0) parts.push(`current block reasons=${formatCounts(facts.blocksByExactReason)}`);
if (facts.blockDetailsMissing > 0) parts.push(`current block details missing=${facts.blockDetailsMissing}`);
if (parts.length === 0) return undefined;
parts.push(`sample=${current.opportunityCount} attempts`);
return `diagnostic: ${parts.join("; ")}`;
}
function isReinforcementVersionFacts(facts: unknown): facts is ReinforcementVersionFacts {
return typeof facts === "object"
&& facts !== null
&& "blocksByExactReason" in facts
&& "blockDetailsMissing" in facts
&& "windowBlockedEvents" in facts;
}
function mechanismOpportunityDescription<TFacts>(mechanism: VersionedMechanismFacts<TFacts>): string {
if (mechanism.opportunityName === "rejection candidates") return "Mechanism opportunities below are reviewable rejection candidates only; render/accounting events are excluded.";
if (mechanism.opportunityName === "attempts") return "Mechanism opportunities below are reinforcement attempts only; render/accounting events are excluded.";
if (mechanism.opportunityName === "capacity removals") return "Mechanism opportunities below are capacity removals only; render/accounting events are excluded.";
return `Mechanism opportunities below are ${mechanism.opportunityName} only; render/accounting events are excluded.`;
}
function formatVersionAvailability(availability: VersionAvailability): string {
const parts = [
availability.noProducerFields > 0 ? `no producer fields=${availability.noProducerFields}` : undefined,
availability.unknownProducerVersion > 0 ? `unknown version=${availability.unknownProducerVersion}` : undefined,
availability.emptyProducerVersion > 0 ? `empty version=${availability.emptyProducerVersion}` : undefined,
availability.knownProducerVersion > 0 ? `known version=${availability.knownProducerVersion}` : undefined,
].filter((part): part is string => Boolean(part));
return parts.length === 0 ? "(empty bucket)" : parts.join(", ");
}
function formatRejectionVersionFacts(facts: RejectionVersionFacts): string {
return `records=${facts.totalRecords}, candidates=${facts.candidateRecords}, raw reason codes=${formatCounts(facts.byRawReasonCode)}, types=${formatCounts(facts.byType)}`;
}
function formatReinforcementVersionFacts(facts: ReinforcementVersionFacts): string {
return `reinforce attempts=${facts.reinforceEvents}, window blocked=${facts.windowBlockedEvents}, exact reasons=${formatCounts(facts.blocksByExactReason)}, block details missing=${facts.blockDetailsMissing}`;
}
function formatEvictionVersionFacts(facts: EvictionVersionFacts): string {
return `capacity removals=${facts.removedByCapacity}, with snapshot=${facts.recentCapacityRemovalsWithSnapshot}, missing snapshot=${facts.capacitySnapshotsMissing}`;
}
function pushSystemMechanismCandidates(lines: string[], report: ReviewBoardReport, bullet: string): void {
const display = report.provenanceContext.candidateDisplay;
if (report.provenanceContext.candidateLimit && display && display.shown < display.total) {
lines.push(`System mechanism review candidates (representative; ${display.shown} shown of ${display.total} total; limit ${report.provenanceContext.candidateLimit} per mechanism category)`);
} else {
lines.push("System mechanism review candidates");
}
pushCandidateGroup(lines, "Rejection filter evidence", candidatesFor(report, ["rejection_rule_evidence"]), bullet);
pushCandidateGroup(lines, "Re-absorption evidence", candidatesFor(report, ["reabsorption_evidence"]), bullet);
pushCandidateGroup(lines, "Reinforcement rule evidence", candidatesFor(report, ["numbered_command_evidence"]), bullet);
pushCandidateGroup(lines, "Eviction/cap evidence", candidatesFor(report, ["eviction_cap_evidence", "missing_evidence"]), bullet);
pushCandidateGroup(lines, "Identity/dedup evidence", candidatesFor(report, ["identity_dedup_evidence"]), bullet);
}
function pushCandidateGroup(lines: string[], title: string, candidates: ReviewBoardCandidate[], bullet: string): void {
lines.push(` ${title}`);
if (candidates.length === 0) {
lines.push(" (none)");
return;
}
const shared = sharedProvenance(candidates);
if (shared) lines.push(` shared provenance for displayed candidates in this group: ${formatProvenance(shared)}`);
for (const candidate of candidates) pushCandidate(lines, candidate, bullet, shared);
}
function pushCandidate(lines: string[], candidate: ReviewBoardCandidate, bullet: string, groupProvenance?: CandidateProvenance): void {
const rawReasonCodes = candidate.evidence.rawReasonCodes && candidate.evidence.rawReasonCodes.length > 0
? candidate.evidence.rawReasonCodes.join(", ")
: "none";
const question = candidate.reviewQuestions[0] ?? "What should a reviewer infer from this evidence?";
lines.push(` ${bullet} concern=${formatConcern(candidate.concernKind)} id=${candidate.id} source=${candidate.source} mechanism=${candidate.mechanism ?? "unspecified"} raw reason codes=${rawReasonCodes} question=${question}`);
if (candidate.provenance && (!groupProvenance || formatProvenance(candidate.provenance) !== formatProvenance(groupProvenance))) {
lines.push(` provenance: ${formatProvenance(candidate.provenance)}`);
}
if (candidate.evidence.eventIds && candidate.evidence.eventIds.length > 0) lines.push(` event ids: ${candidate.evidence.eventIds.join(", ")}`);
if (candidate.evidence.textAvailable) {
lines.push(` text preview: ${candidate.evidence.textPreview ?? "available but empty after redaction"}`);
} else {
lines.push(" text preview: unavailable in historical evidence");
}
lines.push(` facts: ${formatCandidateFacts(candidate.facts)}`);
pushHeuristicFlags(lines, candidate.heuristicFlags, " ", bullet);
}
function pushMemoryContentCandidates(lines: string[], report: ReviewBoardReport, bullet: string, options: { verbose?: boolean }): void {
const display = report.activeMemoryDisplay;
lines.push("Memory content review candidates");
if (report.reviewQuestions.memoryContent.length > 0) {
lines.push(" Standard review questions (applicable to all active memories below):");
for (const question of report.reviewQuestions.memoryContent) lines.push(` ${bullet} ${question}`);
}
if (display.total === 0) {
lines.push(" Active memories (none)");
return;
}
if (display.total <= display.threshold) {
lines.push(` Active memories (showing all ${display.total} because <= ${display.threshold})`);
} else if (display.mode === "all" || options.verbose) {
lines.push(` Active memories (showing all ${display.total} because --verbose)`);
} else {
lines.push(` Active memories (showing ${display.shown} of ${display.total})`);
lines.push(` Showing ${display.shown} of ${display.total} active memories. Use --verbose or --json for all active memory text.`);
}
display.items.forEach((item, index) => pushActiveMemory(lines, item, index + 1, bullet, report.reviewQuestions.memoryContent));
}
function pushActiveMemory(lines: string[], item: ReviewBoardActiveMemory, index: number, bullet: string, standardQuestions: string[]): void {
const strength = typeof item.strength === "number" ? item.strength.toFixed(3) : "unknown";
lines.push(` [${index}] id=${item.id} type=${item.type} source=${item.source} status=${item.status} strength=${strength}`);
lines.push(" text: " + indentContinuation(item.text, " "));
const rawReasonCodes = item.evidence.rawReasonCodes.length > 0 ? item.evidence.rawReasonCodes.join(", ") : "none";
lines.push(` evidence: events=${item.evidence.eventCount} raw reason codes=${rawReasonCodes}`);
if (item.provenance) lines.push(` provenance: ${formatProvenance(item.provenance)}`);
pushHeuristicFlags(lines, item.heuristicFlags, " ", bullet);
if (questionsEqual(item.reviewQuestions, standardQuestions)) return;
const additionalQuestions = item.reviewQuestions.filter(question => !standardQuestions.includes(question));
if (additionalQuestions.length > 0 && additionalQuestions.length < item.reviewQuestions.length) {
lines.push(" additional review questions:");
for (const question of additionalQuestions) lines.push(` ${bullet} ${question}`);
return;
}
lines.push(" review questions:");
for (const question of item.reviewQuestions) lines.push(` ${bullet} ${question}`);
}
function pushHeuristicFlags(lines: string[], flags: HeuristicFlag[], indent: string, bullet: string): void {
if (flags.length === 0) return;
lines.push(`${indent}heuristic flags:`);
for (const flag of flags) {
const caveat = flag.caveat || REVIEW_FLAG_CAVEAT;
lines.push(`${indent} ${bullet} ${flag.label}: ${flag.evidence}. ${caveat}`);
}
}
function pushReviewQuestions(lines: string[], report: ReviewBoardReport, bullet: string): void {
lines.push("Review questions");
lines.push(" SYSTEM MECHANISM");
for (const question of report.reviewQuestions.systemMechanism) lines.push(` ${bullet} ${question}`);
lines.push(" MEMORY CONTENT");
for (const question of report.reviewQuestions.memoryContent) lines.push(` ${bullet} ${question}`);
}
function pushNextCommands(lines: string[], report: ReviewBoardReport, bullet: string): void {
lines.push("Next commands");
for (const command of report.nextCommands) lines.push(` ${bullet} ${command}`);
}
function candidatesFor(report: ReviewBoardReport, sources: ReviewBoardCandidate["source"][]): ReviewBoardCandidate[] {
const sourceSet = new Set(sources);
return report.reviewCandidates.filter(candidate => candidate.concernKind === "system_mechanism" && sourceSet.has(candidate.source));
}
function sharedProvenance(candidates: ReviewBoardCandidate[]): CandidateProvenance | undefined {
if (candidates.length <= 1) return undefined;
const first = candidates[0]?.provenance;
if (!first) return undefined;
const key = formatProvenance(first);
return candidates.every(candidate => candidate.provenance && formatProvenance(candidate.provenance) === key) ? first : undefined;
}
function formatConcern(concern: ReviewBoardCandidate["concernKind"]): string {
return concern === "system_mechanism" ? "SYSTEM MECHANISM" : "MEMORY CONTENT";
}
function formatMigrationTimeline(timeline: ReviewBoardReport["provenanceContext"]["migrationTimeline"]): string {
if (timeline.length === 0) return "(none)";
return timeline.map(row => `${row.migrationId}=${row.presentInStore ? "present" : "absent"}${row.firstEvidenceAt ? ` firstEvidenceAt=${row.firstEvidenceAt}` : ""}`).join(", ");
}
function formatProvenanceCounts(counts: Record<ProvenanceClassification, number>): string {
return PROVENANCE_ORDER.map(classification => `${classification}=${counts[classification] ?? 0}`).join(", ");
}
function formatCounts(counts: Record<string, number>): string {
const entries = Object.entries(counts).sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0]));
return entries.length === 0 ? "(none)" : entries.map(([key, count]) => `${key}=${count}`).join(", ");
}
function formatTypeCountsWithCaps(counts: Record<string, number>, caps: Record<string, number>): string {
const keys = uniqueSorted([...Object.keys(caps), ...Object.keys(counts)]);
return keys.length === 0 ? "(none)" : keys.map(key => `${key} ${counts[key] ?? 0}/${caps[key] ?? "?"}`).join(", ");
}
function formatFullCaps(fullCaps: string[], typeCounts: Record<string, number>, typeCaps: Record<string, number>, active: number, maxEntries: number): string {
if (fullCaps.length === 0) return "(none)";
return fullCaps.map(cap => cap === "global" ? `global ${active}/${maxEntries}` : `${cap} ${typeCounts[cap] ?? 0}/${typeCaps[cap] ?? "?"}`).join(", ");
}
function formatRepeatedBlocks(blocks: ReviewBoardReport["facts"]["systemMechanisms"]["reinforcementRules"]["repeatedBlocksByMemory"]): string {
if (blocks.length === 0) return "(none)";
return blocks.map(block => `${block.memoryId} count=${block.count} refs=${block.refs.join("|") || "none"} raw reason codes=${block.rawReasonCodes.join("|") || "none"}`).join(", ");
}
function formatHighestRankRemoved(snapshot: NonNullable<ReviewBoardReport["facts"]["systemMechanisms"]["evictionAndCaps"]["highestRankRemoved"]>): string {
const parts = [
`eventId=${snapshot.eventId}`,
snapshot.memoryId ? `memoryId=${snapshot.memoryId}` : undefined,
snapshot.type ? `type=${snapshot.type}` : undefined,
`rankAtRemoval=${snapshot.rankAtRemoval}`,
typeof snapshot.strengthAtRemoval === "number" ? `strengthAtRemoval=${snapshot.strengthAtRemoval}` : undefined,
].filter((part): part is string => Boolean(part));
return parts.join(" ");
}
function formatMemoryPreviews(items: ReviewBoardReport["facts"]["memoryContent"]["weakestActiveMemories"]): string {
if (items.length === 0) return "(none)";
return items.map(item => `${item.id} type=${item.type} strength=${typeof item.strength === "number" ? item.strength.toFixed(3) : "unknown"} text=${JSON.stringify(item.textPreview)}`).join(" | ");
}
function formatPercent(value: number): string {
return `${(Number.isFinite(value) ? value * 100 : 0).toFixed(1)}%`;
}
function formatCoveragePercent(value: number): string {
if (!Number.isFinite(value)) return "0%";
return Number.isInteger(value) ? `${value}%` : `${value.toFixed(1)}%`;
}
function formatInteger(value: number): string {
return new Intl.NumberFormat("en-US", { maximumFractionDigits: 0 }).format(value);
}
function formatProvenance(provenance: CandidateProvenance): string {
return `${provenance.classification} confidence=${provenance.confidence}; basis=${provenance.basis.join("; ") || "unavailable"}; caveat=${provenance.interpretationCaveat}`;
}
function formatCandidateFacts(facts: Record<string, unknown>): string {
if (Object.keys(facts).length === 0) return "(none)";
return formatDetails(Object.fromEntries(
Object.entries(facts).map(([key, value]) => [key, formatFactValue(value)]),
));
}
function formatFactValue(value: unknown): string | number | boolean | string[] | undefined {
if (value === undefined) return undefined;
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") return value;
if (value === null) return "null";
if (Array.isArray(value)) return value.map(item => typeof item === "string" || typeof item === "number" || typeof item === "boolean" || item === null ? String(item) : stringifyUnknown(item));
return stringifyUnknown(value);
}
function stringifyUnknown(value: unknown): string {
try {
const serialized = JSON.stringify(value);
return serialized === undefined ? String(value) : serialized;
} catch {
return String(value);
}
}
function questionsEqual(a: string[], b: string[]): boolean {
return a.length === b.length && a.every((value, index) => value === b[index]);
}
function indentContinuation(text: string, indent: string): string {
return text.split("\n").map((line, index) => index === 0 ? line : `${indent}${line}`).join("\n");
}
function uniqueSorted(values: string[]): string[] {
return [...new Set(values)].sort();
}
function redactUnknown(value: unknown): unknown {
if (typeof value === "string") return cleanText(value, false);
if (Array.isArray(value)) return value.map(item => redactUnknown(item));
if (!value || typeof value !== "object") return value;
return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, redactUnknown(item)]));
}
@@ -0,0 +1,93 @@
import { rejectionFalsePositiveRisk, rejectionQualitySummary } from "../rejections-model.ts";
import { cleanPath, cleanText, countBy, sortedCounts, truncate } from "../text.ts";
import type { NormalizedRejection } from "../types.ts";
export type RejectedSummary = ReturnType<typeof rejectionQualitySummary>;
export type RejectedFalsePositiveRisk = ReturnType<typeof rejectionFalsePositiveRisk>;
export function buildRejectedJSON(input: {
summary: RejectedSummary;
falsePositiveRisk: RejectedFalsePositiveRisk;
generatedAt?: string;
}): Record<string, unknown> {
return {
version: 1,
generatedAt: input.generatedAt ?? new Date().toISOString(),
...input.summary,
falsePositiveRisk: input.falsePositiveRisk,
};
}
export function formatRejected(input: {
path: string;
invalidLines: number;
records: NormalizedRejection[];
summary: RejectedSummary;
falsePositiveRisk: RejectedFalsePositiveRisk;
raw: boolean;
verbose?: boolean;
}): string {
const lines: string[] = [];
lines.push("Rejected memory summary");
lines.push("");
lines.push(`Total rejected: ${input.summary.totalRecords}`);
lines.push(`Unique texts: ${input.summary.uniqueTexts}`);
lines.push("");
lines.push("Top reasons:");
const byReason = sortedCounts(countBy(input.records.flatMap(record => record.reasons))).slice(0, 5);
if (byReason.length === 0) lines.push(" (none)");
else for (const [reason, count] of byReason) lines.push(` ${reason.padEnd(36)} ${count}`);
lines.push("");
lines.push(`False-positive risk: ${input.falsePositiveRisk}`);
if (input.falsePositiveRisk === "high") {
lines.push("⚠️ Possible false positives detected. Use --verbose to inspect samples.");
}
lines.push("");
lines.push("Recent samples:");
const samples = [...input.records]
.sort((a, b) => (new Date(b.timestamp).getTime() || 0) - (new Date(a.timestamp).getTime() || 0))
.slice(0, 5);
if (samples.length === 0) {
lines.push(" (none)");
} else {
for (const record of samples) {
lines.push(` - [${record.type}] ${truncate(cleanText(record.text, input.raw))}`);
lines.push(` reasons: ${record.reasons.length > 0 ? record.reasons.join(",") : "none"}`);
}
}
if (!input.verbose) return lines.join("\n");
lines.push("");
lines.push("Possible false-positive grouping is heuristic, not deterministic truth.");
lines.push(`logPath=${cleanPath(input.path, input.raw)}`);
if (input.invalidLines > 0) lines.push(`Invalid JSONL lines skipped: ${input.invalidLines}`);
lines.push("");
lines.push("Reason distribution (raw records):");
for (const [reason, count] of Object.entries(input.summary.reasonDistribution)) lines.push(` ${reason.padEnd(36)} ${count}`);
if (Object.keys(input.summary.reasonDistribution).length === 0) lines.push(" (none)");
lines.push("");
lines.push("Reason distribution (unique text):");
for (const [reason, count] of Object.entries(input.summary.uniqueReasonDistribution)) lines.push(` ${reason.padEnd(36)} ${count}`);
if (Object.keys(input.summary.uniqueReasonDistribution).length === 0) lines.push(" (none)");
lines.push("");
lines.push("By origin:");
const byOrigin = sortedCounts(countBy(input.records.map(record => record.origin)));
if (byOrigin.length === 0) lines.push(" (none)");
else for (const [origin, count] of byOrigin) lines.push(` ${origin.padEnd(36)} ${count}`);
lines.push("");
lines.push("Possible false-positive groups (heuristic, not deterministic):");
for (const [group, data] of Object.entries(input.summary.possibleFalsePositiveGroups)) {
lines.push(` ${group}: ${data.count}`);
for (const sample of data.samples) lines.push(` - ${JSON.stringify(cleanText(sample, input.raw))}`);
}
return lines.join("\n");
}
+155
View File
@@ -0,0 +1,155 @@
import {
DORMANT_DECAY_MULTIPLIER,
RETENTION_TYPE_MAX,
} from "../../../src/retention.ts";
import { TYPES } from "../constants.ts";
import { formatStrength } from "../retention-model.ts";
import { cleanText, truncate } from "../text.ts";
import type { MemoryInspectionReadModel, RetentionDiagItem } from "../types.ts";
export type MemoryStatusLevel = "ok" | "warning" | "degraded";
export type StatusReadout = {
status: MemoryStatusLevel;
summaryText: string;
active: number;
rendered: number;
pending: number;
rejectedLast7Days: number;
evidenceCoveragePercent: number;
needsAttention: string[];
suggestedNextSteps: string[];
caps: {
active: number;
maxEntries: number;
rendered: number;
typeCapped: number;
globalCapped: number;
typeCounts: Record<string, number>;
capsFull: boolean;
};
retention: { present: number; missing: number; invalid: number };
evidence: {
currentWithEvidence: number;
currentWithoutEvidence: number;
evidenceMemoryIds: number;
disappearances: number;
unknownDisappearances: number;
withTerminalReason: number;
};
rejections: {
totalRecords: number;
workspaceScopedCount: number;
legacyUnscopedCount: number;
falsePositiveRisk: string;
};
dormant: {
lastActivityAt?: string;
wallDaysSinceActivity: number | null;
dormantDiscountActive: boolean;
dormantDaysPastGrace: number;
};
topRendered: RetentionDiagItem[];
weakestActive: RetentionDiagItem[];
};
export function formatStatus(readout: StatusReadout, model: MemoryInspectionReadModel, options: { verbose?: boolean; noEmoji?: boolean; isTty?: boolean; raw?: boolean } = {}): string {
if (options.verbose) return formatStatusVerbose(readout, model, options);
const lines: string[] = [];
const label = statusLabel(readout.status, options);
lines.push(`${label} Memory status`);
lines.push("");
lines.push("Key metrics:");
lines.push(` active memories: ${readout.active}`);
lines.push(` rendered: ${readout.rendered}`);
lines.push(` pending: ${readout.pending}`);
lines.push(` rejected 7d: ${readout.rejectedLast7Days}`);
lines.push(` evidence coverage: ${readout.evidenceCoveragePercent}%`);
if (readout.needsAttention.length > 0) {
lines.push("");
lines.push("Needs attention:");
for (const item of readout.needsAttention) lines.push(` - ${item}`);
}
if (readout.status !== "ok" && readout.suggestedNextSteps.length > 0) {
lines.push("");
lines.push("Suggested next steps:");
for (const item of readout.suggestedNextSteps) lines.push(` - ${item}`);
}
return lines.join("\n");
}
function statusLabel(status: MemoryStatusLevel, options: { noEmoji?: boolean; isTty?: boolean }): string {
const text = status === "ok" ? "OK" : status === "warning" ? "WARNING" : "DEGRADED";
if (options.noEmoji || !options.isTty) return text;
const emoji = status === "ok" ? "🧠" : status === "warning" ? "⚠️" : "✖️";
return `${emoji} ${text}`;
}
function formatStatusVerbose(readout: StatusReadout, model: MemoryInspectionReadModel, options: { raw?: boolean } = {}): string {
const lines: string[] = [];
lines.push("Memory status inspection");
lines.push("");
lines.push(readout.summaryText);
lines.push("");
lines.push("Caps:");
lines.push(` active: ${readout.caps.active} / ${readout.caps.maxEntries}`);
for (const type of TYPES) {
const count = readout.caps.typeCounts[type] ?? 0;
const limit = RETENTION_TYPE_MAX[type];
const marker = count >= limit ? " FULL" : "";
lines.push(` ${type}: ${count} / ${limit}${marker}`);
}
lines.push(` rendered: ${readout.caps.rendered}`);
lines.push(` type-capped entries: ${readout.caps.typeCapped}`);
lines.push(` global-cap overflow: ${readout.caps.globalCapped}`);
lines.push(` caps full: ${readout.caps.capsFull ? "yes" : "no"}`);
lines.push("");
lines.push("Retention clocks:");
lines.push(` present: ${readout.retention.present}`);
lines.push(` missing: ${readout.retention.missing}`);
lines.push(` invalid: ${readout.retention.invalid}`);
lines.push("");
lines.push("Evidence:");
lines.push(` current with evidence: ${readout.evidence.currentWithEvidence}`);
lines.push(` current without evidence: ${readout.evidence.currentWithoutEvidence}`);
lines.push(` evidence memory ids: ${readout.evidence.evidenceMemoryIds}`);
lines.push(` disappearances: ${readout.evidence.disappearances}`);
lines.push(` unknown disappearances: ${readout.evidence.unknownDisappearances}`);
lines.push("");
lines.push("Rejection scoping:");
lines.push(` total records: ${readout.rejections.totalRecords}`);
lines.push(` workspace scoped: ${readout.rejections.workspaceScopedCount}`);
lines.push(` legacy unscoped: ${readout.rejections.legacyUnscopedCount}`);
lines.push(` false-positive risk: ${readout.rejections.falsePositiveRisk}`);
lines.push("");
lines.push("Dormancy:");
lines.push(` lastActivityAt: ${readout.dormant.lastActivityAt ?? "(missing)"}`);
lines.push(` wall days since activity: ${readout.dormant.wallDaysSinceActivity === null ? "unknown" : readout.dormant.wallDaysSinceActivity.toFixed(1)}`);
lines.push(` dormant discount active: ${readout.dormant.dormantDiscountActive ? "yes" : "no"}`);
lines.push(` dormant days past grace: ${readout.dormant.dormantDaysPastGrace.toFixed(1)}`);
lines.push(` dormant multiplier: ${DORMANT_DECAY_MULTIPLIER}`);
lines.push("");
lines.push("Top rendered candidates:");
pushMemoryItems(lines, readout.topRendered, options.raw === true);
lines.push("");
lines.push("Weakest active memories:");
pushMemoryItems(lines, readout.weakestActive, options.raw === true);
lines.push("");
lines.push("Store:");
lines.push(` superseded: ${model.store.entries.length - readout.active}`);
return lines.join("\n");
}
function pushMemoryItems(lines: string[], items: RetentionDiagItem[], raw: boolean): void {
if (items.length === 0) {
lines.push(" (none)");
return;
}
for (const item of items) {
lines.push(` - strength=${formatStrength(item.strength)} [${item.entry.type}] ${truncate(cleanText(item.entry.text, raw))}`);
}
}
+36
View File
@@ -0,0 +1,36 @@
import type { EvidenceEventV1 } from "../../../src/evidence-log.ts";
import { formatTraceEvent, relationMemoryIds, statusFromTraceEvent } from "../trace-model.ts";
import type { WorkspaceDiagSnapshot } from "../types.ts";
export function formatTrace(memoryId: string, snapshot: WorkspaceDiagSnapshot, trace: { events: EvidenceEventV1[] }): string {
const lines: string[] = [];
const memoryRow = snapshot.memories.find(memory => memory.id === memoryId);
const status = memoryRow?.status ?? statusFromTraceEvent(trace.events.at(-1));
lines.push(`Memory ${memoryId}: ${status}`);
lines.push("");
lines.push("Lifecycle:");
if (trace.events.length === 0) {
lines.push("(none)");
return lines.join("\n");
}
for (const event of trace.events) {
lines.push(formatTraceEvent(event));
}
const supersededBy = relationMemoryIds(trace.events, "superseded_by");
if (supersededBy.length > 0) {
lines.push("");
lines.push("Superseded by:");
for (const id of supersededBy) lines.push(`- ${id}`);
}
const reinforcedBy = relationMemoryIds(trace.events, "reinforced_by");
if (reinforcedBy.length > 0) {
lines.push("");
lines.push("Reinforced by:");
for (const id of reinforcedBy) lines.push(`- ${id}`);
}
return lines.join("\n");
}
+98
View File
@@ -0,0 +1,98 @@
import type { EvidenceEventV1 } from "../../src/evidence-log.ts";
import type { LongTermType } from "../../src/types.ts";
import { countBy, objectFromCounts } from "./text.ts";
import { groupEvidenceByMemoryId } from "./evidence-model.ts";
import { loadRejectionRecords } from "./rejections-model.ts";
import { snapshotForOptions } from "./workspace-snapshot.ts";
import type { CliOptions, CoverageClass, DisappearanceReason, MemoryInspectionReadModel } from "./types.ts";
export async function buildInspectionReadModel(options: CliOptions): Promise<MemoryInspectionReadModel> {
const snapshot = await snapshotForOptions(options);
const rejections = await loadRejectionRecords(options);
return {
snapshot,
store: snapshot.store,
pending: snapshot.journal,
evidenceEvents: snapshot.allEvents,
rejectionRecords: rejections.records,
currentById: new Map(snapshot.store.entries.map(entry => [entry.id, entry])),
evidenceByMemoryId: groupEvidenceByMemoryId(snapshot.allEvents),
};
}
export function terminalDisappearanceReason(events: EvidenceEventV1[]): DisappearanceReason {
const terminal = [...events].reverse().find(event =>
event.type === "memory_removed_capacity"
|| event.type === "promotion_absorbed_exact"
|| event.type === "promotion_absorbed_identity"
|| event.type === "promotion_superseded"
);
const renderOmission = [...events].reverse().find(event => event.type === "render_omitted");
const event = terminal ?? renderOmission;
if (!event) {
return { classification: "historical_absent_unknown_reason", terminalType: "unknown", reasonCodes: [] };
}
return {
classification: "historical_absent_with_reason",
terminalType: event.type,
reasonCodes: event.reasonCodes,
event,
};
}
export function coverageClassForMemory(id: string, model: MemoryInspectionReadModel): CoverageClass {
const events = model.evidenceByMemoryId.get(id) ?? [];
if (!model.currentById.has(id)) return terminalDisappearanceReason(events).classification;
if (events.length === 0) return "no_evidence";
if (events.every(event => event.phase === "render")) return "render_only";
return "full_lifecycle";
}
export function eventCounts(events: EvidenceEventV1[]): { total: number; byType: Record<string, number>; byPhase: Record<string, number> } {
return {
total: events.length,
byType: objectFromCounts(countBy(events.map(event => event.type))),
byPhase: objectFromCounts(countBy(events.map(event => event.phase))),
};
}
export function coverageRows(model: MemoryInspectionReadModel, includeHistorical: boolean): Array<{
id: string;
class: CoverageClass;
current: boolean;
type?: LongTermType;
eventCounts: ReturnType<typeof eventCounts>;
}> {
const ids = new Set<string>(model.currentById.keys());
if (includeHistorical) {
for (const id of model.evidenceByMemoryId.keys()) ids.add(id);
}
return [...ids].sort().map(id => {
const entry = model.currentById.get(id);
const events = model.evidenceByMemoryId.get(id) ?? [];
return {
id,
class: coverageClassForMemory(id, model),
current: Boolean(entry),
type: entry?.type ?? events.find(event => event.memory?.memoryId === id)?.memory?.type,
eventCounts: eventCounts(events),
};
});
}
export function disappearanceRows(model: MemoryInspectionReadModel): Array<{
id: string;
classification: DisappearanceReason["classification"];
terminalType: DisappearanceReason["terminalType"];
reasonCodes: string[];
event?: EvidenceEventV1;
events: EvidenceEventV1[];
}> {
const rows = [...model.evidenceByMemoryId.entries()]
.filter(([id]) => !model.currentById.has(id))
.map(([id, events]) => {
const reason = terminalDisappearanceReason(events);
return { id, ...reason, events };
});
return rows.sort((a, b) => a.classification.localeCompare(b.classification) || a.id.localeCompare(b.id));
}
+36
View File
@@ -0,0 +1,36 @@
import { existsSync } from "node:fs";
import { readFile } from "node:fs/promises";
export async function readJSONFile<T>(path: string): Promise<T | null> {
try {
return JSON.parse(await readFile(path, "utf8")) as T;
} catch {
return null;
}
}
export async function readJSONLFile<T>(path: string): Promise<{ records: T[]; invalidLines: number }> {
let content = "";
try {
content = await readFile(path, "utf8");
} catch {
return { records: [], invalidLines: 0 };
}
const records: T[] = [];
let invalidLines = 0;
for (const line of content.split("\n")) {
const trimmed = line.trim();
if (!trimmed) continue;
try {
records.push(JSON.parse(trimmed) as T);
} catch {
invalidLines += 1;
}
}
return { records, invalidLines };
}
export function pathExists(path: string): boolean {
return existsSync(path);
}
+42
View File
@@ -0,0 +1,42 @@
import { readdir } from "node:fs/promises";
import { join } from "node:path";
import { dataHome, migrationLogPath } from "../../src/paths.ts";
import type { CliOptions, MigrationLogRecord } from "./types.ts";
export function migrationLogsRoot(): string {
return join(dataHome(), "opencode-working-memory", "migration-logs");
}
export async function migrationLogPaths(options: CliOptions): Promise<string[]> {
if (options.migration) return [migrationLogPath(options.migration)];
const root = migrationLogsRoot();
let entries: string[] = [];
try {
entries = await readdir(root);
} catch {
return [];
}
return entries.filter(entry => entry.endsWith(".jsonl")).sort().map(entry => join(root, entry));
}
export function migrationIdFromPath(path: string): string {
return path.split("/").pop()?.replace(/\.jsonl$/, "") ?? "unknown";
}
export function riskySupersedeReasons(record: MigrationLogRecord): string[] {
const reasons: string[] = [];
const hardReasonsMissing = !Array.isArray(record.hardReasons);
const hardReasons = Array.isArray(record.hardReasons) ? record.hardReasons : [];
const qualityReasons = Array.isArray(record.reasons) ? record.reasons : [];
const text = record.text ?? "";
if (hardReasonsMissing || hardReasons.length === 0) reasons.push("missing_or_empty_hardReasons");
if (qualityReasons.length > 0 && hardReasons.length === 0) reasons.push("soft_reasons_without_hardReasons");
if (/\b(?:User|user|prefers|requires|wants|insists)\b||使||||/u.test(text)) reasons.push("user_preference_marker");
if (/\b(?:must|should|do not|never|is|are|follows)\b|||||/iu.test(text)) reasons.push("durable_rule_marker");
if ((record.type === "feedback" || record.type === "decision") && hardReasons.length === 1 && hardReasons[0] === "path_heavy") {
reasons.push("feedback_or_decision_path_heavy_only");
}
return reasons;
}
File diff suppressed because it is too large Load Diff
+236
View File
@@ -0,0 +1,236 @@
export type AnswerabilityLevel = "supported" | "partial" | "inventory_only" | "not_instrumented";
export type ProducerVersionGroup = "current" | "previous" | "unknown_unversioned";
export type ProducerBearingRecord = {
producerName?: string;
producerVersion?: string;
instrumentationVersion?: number;
};
export type VersionSampleAssessment =
| "observed"
| "not_observed_but_sample_small"
| "not_observed_with_sufficient_sample"
| "no_current_version_opportunities";
export type VersionAvailability = {
noProducerFields: number;
unknownProducerVersion: number;
emptyProducerVersion: number;
knownProducerVersion: number;
};
export type VersionCoverage = {
totalEvents: number;
currentVersionEvents: number;
previousVersionEvents: number;
unknownVersionEvents: number;
coveragePercent: number;
isTransitional: boolean;
};
export const VERSION_ANALYSIS_SAMPLE_THRESHOLD = 5;
export const VERSION_GROUPS: ProducerVersionGroup[] = ["current", "previous", "unknown_unversioned"];
export const VERSION_GROUPING_CAVEAT = "Version grouping is based only on producerVersion strings in evidence" as const;
export type VersionedMechanismInference = {
status:
| "current_recurrence_detected"
| "pattern_persists_across_versions"
| "no_current_evidence_observed"
| "no_current_evidence_sample_small"
| "no_current_version_opportunities"
| "no_previous_pattern_observed";
message: string;
caveat: typeof VERSION_GROUPING_CAVEAT;
};
export type VersionBucketFacts<TFacts> = {
group: ProducerVersionGroup;
label: string;
opportunityCount: number;
observedPatternCount: number;
producerVersions: Record<string, number>;
versionAvailability: VersionAvailability;
answerabilityLevel: AnswerabilityLevel;
sampleAssessment: VersionSampleAssessment;
facts: TFacts;
};
export type VersionedMechanismDiagnosticQuestion = {
mechanism: "reinforcement_rule";
group: ProducerVersionGroup;
question: string;
evidence: string[];
};
export type VersionedMechanismFacts<TFacts> = {
currentPackageVersion: string;
opportunityName: string;
sampleThreshold: number;
buckets: Record<ProducerVersionGroup, VersionBucketFacts<TFacts>>;
inference: VersionedMechanismInference;
diagnosticQuestions?: VersionedMechanismDiagnosticQuestion[];
};
export function buildVersionBuckets<TRecord extends ProducerBearingRecord, TFacts>(
records: TRecord[],
currentPackageVersion: string,
summarize: (records: TRecord[]) => { facts: TFacts; opportunityCount: number; observedPatternCount: number },
): Record<ProducerVersionGroup, VersionBucketFacts<TFacts>> {
const grouped = Object.fromEntries(VERSION_GROUPS.map(group => [group, []])) as Record<ProducerVersionGroup, TRecord[]>;
for (const record of records) grouped[producerVersionGroupFor(record, currentPackageVersion)].push(record);
return Object.fromEntries(VERSION_GROUPS.map(group => {
const bucketRecords = grouped[group];
const summary = summarize(bucketRecords);
return [group, {
group,
label: versionGroupLabel(group, currentPackageVersion),
opportunityCount: summary.opportunityCount,
observedPatternCount: summary.observedPatternCount,
producerVersions: producerVersionCounts(bucketRecords),
versionAvailability: buildVersionAvailability(bucketRecords),
answerabilityLevel: group === "current" && summary.opportunityCount > 0 ? "partial" : "inventory_only",
sampleAssessment: sampleAssessmentFor(group, summary.opportunityCount, summary.observedPatternCount, currentPackageVersion),
facts: summary.facts,
} satisfies VersionBucketFacts<TFacts>];
})) as Record<ProducerVersionGroup, VersionBucketFacts<TFacts>>;
}
export function computeVersionedInference<TFacts>(
mechanism: Omit<VersionedMechanismFacts<TFacts>, "inference">,
text: { observedPattern: string; patternName: string },
): VersionedMechanismInference {
const current = mechanism.buckets.current;
const previous = mechanism.buckets.previous;
const currentFact = `Current version: ${current.observedPatternCount} ${text.observedPattern} in ${current.opportunityCount} ${mechanism.opportunityName}.`;
const previousFact = `Previous versions: ${previous.observedPatternCount} ${text.observedPattern} in ${previous.opportunityCount} ${mechanism.opportunityName}.`;
const unknownUnversioned = mechanism.buckets.unknown_unversioned;
if (!isAssessableCurrentPackageVersion(mechanism.currentPackageVersion) || current.opportunityCount === 0) {
return inference("no_current_version_opportunities", "Current package version is unknown or has no events; cannot assess recurrence.");
}
if (current.observedPatternCount > 0 && previous.observedPatternCount === 0 && unknownUnversioned.observedPatternCount === 0) {
return inference("no_previous_pattern_observed", `${currentFact} No previous pattern observed — this is a new pattern, not a recurrence.`);
}
if (current.observedPatternCount > 0) {
if (previous.observedPatternCount > 0) {
return inference("pattern_persists_across_versions", `${currentFact} ${previousFact} Current recurrence detected — ${text.patternName} observed in current version. Pattern persists across versions.`);
}
// Current has signal, previous has none, but unknown/unversioned has signal
return inference("current_recurrence_detected", `${currentFact} No known previous-version pattern observed, but unknown/unversioned evidence shows ${unknownUnversioned.observedPatternCount} ${text.observedPattern}. Pattern may persist — version grouping cannot confirm or deny.`);
}
if (current.opportunityCount < mechanism.sampleThreshold) {
return inference("no_current_evidence_sample_small", `${currentFact} ${previousFact} No current evidence observed, but current-version opportunity count is ${current.opportunityCount} (<${mechanism.sampleThreshold}); do not infer absence.`);
}
return inference("no_current_evidence_observed", `${currentFact} ${previousFact} No recurrence observed with sufficient current-version sample.`);
}
export function hasProducerFields(record: ProducerBearingRecord): boolean {
return typeof record.producerName === "string"
&& record.producerName.length > 0
&& typeof record.producerVersion === "string"
&& record.producerVersion.length > 0
&& typeof record.instrumentationVersion === "number";
}
export function hasKnownProducerVersion(record: ProducerBearingRecord): boolean {
if (typeof record.producerVersion !== "string") return false;
const producerVersion = record.producerVersion.trim();
return producerVersion.length > 0 && producerVersion !== "unknown";
}
export function producerVersionGroupFor(record: ProducerBearingRecord, currentPackageVersion: string): ProducerVersionGroup {
if (!hasKnownProducerVersion(record)) return "unknown_unversioned";
const producerVersion = String(record.producerVersion).trim();
const currentVersion = currentPackageVersion.trim();
if (currentVersion.length > 0 && currentVersion !== "unknown" && producerVersion === currentVersion) return "current";
return "previous";
}
export function buildVersionCoverage(records: ProducerBearingRecord[], currentPackageVersion: string): VersionCoverage {
const coverage: VersionCoverage = {
totalEvents: records.length,
currentVersionEvents: 0,
previousVersionEvents: 0,
unknownVersionEvents: 0,
coveragePercent: 0,
isTransitional: true,
};
for (const record of records) {
const group = producerVersionGroupFor(record, currentPackageVersion);
if (group === "current") coverage.currentVersionEvents += 1;
if (group === "previous") coverage.previousVersionEvents += 1;
if (group === "unknown_unversioned") coverage.unknownVersionEvents += 1;
}
coverage.coveragePercent = coverage.totalEvents === 0
? 0
: Math.round(((coverage.currentVersionEvents + coverage.previousVersionEvents) / coverage.totalEvents) * 1000) / 10;
coverage.isTransitional = coverage.coveragePercent < 50;
return coverage;
}
function producerVersionCounts(records: ProducerBearingRecord[]): Record<string, number> {
const counts: Record<string, number> = {};
for (const record of records) {
if (!hasKnownProducerVersion(record)) continue;
const version = String(record.producerVersion).trim();
counts[version] = (counts[version] ?? 0) + 1;
}
return counts;
}
function versionGroupLabel(group: ProducerVersionGroup, currentPackageVersion: string): string {
if (group === "current") return `current version ${currentPackageVersion}`;
if (group === "previous") return "previous versions";
return "unknown/unversioned";
}
function sampleAssessmentFor(
group: ProducerVersionGroup,
opportunityCount: number,
observedPatternCount: number,
currentPackageVersion: string,
): VersionSampleAssessment {
if (observedPatternCount > 0) return "observed";
if (group === "current" && (!isAssessableCurrentPackageVersion(currentPackageVersion) || opportunityCount === 0)) return "no_current_version_opportunities";
if (opportunityCount < VERSION_ANALYSIS_SAMPLE_THRESHOLD) return "not_observed_but_sample_small";
return "not_observed_with_sufficient_sample";
}
function isAssessableCurrentPackageVersion(currentPackageVersion: string): boolean {
const trimmed = currentPackageVersion.trim();
return trimmed.length > 0 && trimmed !== "unknown";
}
function inference(status: VersionedMechanismInference["status"], message: string): VersionedMechanismInference {
return { status, message, caveat: VERSION_GROUPING_CAVEAT };
}
function buildVersionAvailability(records: ProducerBearingRecord[]): VersionAvailability {
const availability: VersionAvailability = {
noProducerFields: 0,
unknownProducerVersion: 0,
emptyProducerVersion: 0,
knownProducerVersion: 0,
};
for (const record of records) {
const hasAnyProducerField = typeof record.producerName === "string"
|| typeof record.producerVersion === "string"
|| typeof record.instrumentationVersion === "number";
if (!hasAnyProducerField) {
availability.noProducerFields += 1;
continue;
}
if (typeof record.producerVersion !== "string" || record.producerVersion.trim().length === 0) {
availability.emptyProducerVersion += 1;
continue;
}
if (record.producerVersion.trim() === "unknown") {
availability.unknownProducerVersion += 1;
continue;
}
availability.knownProducerVersion += 1;
}
return availability;
}
+141
View File
@@ -0,0 +1,141 @@
import { extractionRejectionLogPath, workspaceKey } from "../../src/paths.ts";
import { HARD_QUALITY_REASONS, isArchitectureLikeDecision } from "../../src/memory-quality.ts";
import { ALLOWED_ORIGINS } from "./constants.ts";
import { readJSONLFile } from "./io.ts";
import { canonicalMemoryText, cleanText, countBy, objectFromCounts, truncate } from "./text.ts";
import { CliInputError, type CliOptions, type NormalizedRejection, type Origin, type RejectionLogRecord } from "./types.ts";
export { CliInputError } from "./types.ts";
export function inferOrigin(record: RejectionLogRecord): Origin {
if (record.origin && ALLOWED_ORIGINS.has(record.origin as Origin)) return record.origin as Origin;
if (record.source === "compaction") return "compaction_candidate";
if (record.source === "explicit") return "explicit_trigger";
if (record.source === "manual") return "manual";
return "unknown";
}
export function normalizeRejection(record: RejectionLogRecord): NormalizedRejection | null {
if (!record.text || !Array.isArray(record.reasons)) return null;
const origin = inferOrigin(record);
return {
timestamp: record.timestamp ?? "",
workspaceKey: record.workspaceKey,
workspaceRoot: record.workspaceRoot,
workspaceRootHash: record.workspaceRootHash,
type: record.type ?? "project",
source: record.source,
origin,
fromTrigger: typeof record.fromTrigger === "boolean" ? record.fromTrigger : origin === "explicit_trigger",
producerName: typeof record.producerName === "string" ? record.producerName : undefined,
producerVersion: typeof record.producerVersion === "string" ? record.producerVersion : undefined,
instrumentationVersion: typeof record.instrumentationVersion === "number" ? record.instrumentationVersion : undefined,
decisionLogicName: typeof record.decisionLogicName === "string" ? record.decisionLogicName : undefined,
decisionLogicVersion: typeof record.decisionLogicVersion === "number" ? record.decisionLogicVersion : undefined,
text: record.text,
reasons: record.reasons,
};
}
export function sinceCutoff(rawSince: string | undefined, now = Date.now()): number | null {
if (!rawSince) return null;
const relative = rawSince.match(/^(\d+)([dhm])$/i);
if (relative) {
const amount = Number(relative[1]);
const unit = relative[2].toLowerCase();
const multiplier = unit === "d" ? 86_400_000 : unit === "h" ? 3_600_000 : 60_000;
return now - amount * multiplier;
}
const timestamp = new Date(rawSince).getTime();
if (Number.isNaN(timestamp)) throw new CliInputError(`Invalid --since value: ${rawSince}`);
return timestamp;
}
export function hasSoftReason(record: NormalizedRejection): boolean {
return record.reasons.some(reason => !HARD_QUALITY_REASONS.has(reason));
}
export function hasWorkspaceScope(record: NormalizedRejection): boolean {
return Boolean(record.workspaceKey || record.workspaceRoot || record.workspaceRootHash);
}
export async function workspaceKeyForOption(options: CliOptions): Promise<string | undefined> {
return options.workspace ? workspaceKey(options.workspace) : undefined;
}
export async function loadRejectionRecords(options: CliOptions): Promise<{ path: string; invalidLines: number; records: NormalizedRejection[] }> {
const path = extractionRejectionLogPath();
const { records, invalidLines } = await readJSONLFile<RejectionLogRecord>(path);
const cutoff = sinceCutoff(options.since);
const requestedWorkspaceKey = await workspaceKeyForOption(options);
let normalized = records.map(normalizeRejection).filter((record): record is NormalizedRejection => record !== null);
if (requestedWorkspaceKey) {
normalized = normalized.filter(record => !record.workspaceKey || record.workspaceKey === requestedWorkspaceKey);
}
if (cutoff !== null) {
normalized = normalized.filter(record => {
const timestamp = new Date(record.timestamp).getTime();
return !Number.isNaN(timestamp) && timestamp >= cutoff;
});
}
if (options.softOnly) normalized = normalized.filter(hasSoftReason);
if (options.triggerOnly) normalized = normalized.filter(record => record.fromTrigger || record.origin === "explicit_trigger");
if (options.reason) normalized = normalized.filter(record => record.reasons.includes(options.reason ?? ""));
return { path, invalidLines, records: normalized };
}
export function uniqueByCanonicalText(records: NormalizedRejection[]): NormalizedRejection[] {
const byText = new Map<string, NormalizedRejection>();
for (const record of records) {
const key = `${record.type}:${canonicalMemoryText(record.text)}`;
if (!byText.has(key)) byText.set(key, record);
}
return [...byText.values()];
}
export function rejectionQualitySummary(records: NormalizedRejection[]): {
totalRecords: number;
uniqueTexts: number;
workspaceScopedCount: number;
legacyUnscopedCount: number;
reasonDistribution: Record<string, number>;
uniqueReasonDistribution: Record<string, number>;
possibleFalsePositiveGroups: Record<string, { count: number; samples: string[] }>;
} {
const uniqueRecords = uniqueByCanonicalText(records);
const badDecisionUnique = uniqueRecords.filter(record => record.reasons.includes("bad_decision"));
const groups: Record<string, { count: number; samples: string[] }> = {
architecture_like_possible_false_positive: { count: 0, samples: [] },
clearly_garbage: { count: 0, samples: [] },
ambiguous: { count: 0, samples: [] },
};
for (const record of badDecisionUnique) {
const hardReasons = record.reasons.filter(reason => HARD_QUALITY_REASONS.has(reason));
const statusLike = /\b(?:implemented|added|updated|fixed|completed|reviewed|tests?|CI|commit|wave|phase|task|session)\b/i.test(record.text);
const group = isArchitectureLikeDecision(record.text) && hardReasons.length === 0 && !statusLike
? "architecture_like_possible_false_positive"
: hardReasons.length > 0 || statusLike
? "clearly_garbage"
: "ambiguous";
groups[group].count += 1;
if (groups[group].samples.length < 5) groups[group].samples.push(truncate(cleanText(record.text, false), 120));
}
return {
totalRecords: records.length,
uniqueTexts: uniqueRecords.length,
workspaceScopedCount: records.filter(hasWorkspaceScope).length,
legacyUnscopedCount: records.filter(record => !hasWorkspaceScope(record)).length,
reasonDistribution: objectFromCounts(countBy(records.flatMap(record => record.reasons))),
uniqueReasonDistribution: objectFromCounts(countBy(uniqueRecords.flatMap(record => record.reasons))),
possibleFalsePositiveGroups: groups,
};
}
export function rejectionFalsePositiveRisk(summary: ReturnType<typeof rejectionQualitySummary>): "low" | "high" {
const possible = summary.possibleFalsePositiveGroups.architecture_like_possible_false_positive.count;
return possible >= 3 || (summary.uniqueTexts > 0 && possible / summary.uniqueTexts >= 0.5) ? "high" : "low";
}
+84
View File
@@ -0,0 +1,84 @@
import {
RETENTION_TYPE_MAX,
calculateRetentionStrength,
} from "../../src/retention.ts";
import type { LongTermMemoryEntry, LongTermSource, LongTermType, WorkspaceMemoryStore } from "../../src/types.ts";
import { LONG_TERM_LIMITS, PROMOTION_RETRY_LIMITS } from "../../src/types.ts";
import type { RetentionDiagItem } from "./types.ts";
export function ageDays(entry: LongTermMemoryEntry, now = Date.now()): number | null {
const time = new Date(entry.createdAt).getTime();
if (Number.isNaN(time)) return null;
return Math.floor((now - time) / 86_400_000);
}
export function formatStrength(value: number): string {
return Number.isFinite(value) ? value.toFixed(3) : "0.000";
}
export function daysSinceIso(value: string | undefined, now = Date.now()): number | null {
if (!value) return null;
const ms = new Date(value).getTime();
if (!Number.isFinite(ms)) return null;
return Math.max(0, (now - ms) / 86_400_000);
}
export function isSafetyCriticalForDiag(entry: LongTermMemoryEntry): boolean {
return entry.safetyCritical === true;
}
export function retentionCandidatesForDiag(store: WorkspaceMemoryStore, now = Date.now()): {
sorted: RetentionDiagItem[];
rendered: RetentionDiagItem[];
typeCapped: RetentionDiagItem[];
globalCapped: RetentionDiagItem[];
} {
const active = store.entries.filter(entry => entry.status !== "superseded");
const sorted = active
.map(entry => ({ entry, strength: calculateRetentionStrength(entry, now, store.lastActivityAt) }))
.sort((a, b) => b.strength - a.strength || a.entry.id.localeCompare(b.entry.id));
const rendered: RetentionDiagItem[] = [];
const typeCapped: RetentionDiagItem[] = [];
const globalCapped: RetentionDiagItem[] = [];
const typeCounts: Partial<Record<LongTermType, number>> = {};
for (const item of sorted) {
const count = typeCounts[item.entry.type] ?? 0;
const max = RETENTION_TYPE_MAX[item.entry.type] ?? Infinity;
if (count >= max) {
typeCapped.push(item);
continue;
}
typeCounts[item.entry.type] = count + 1;
if (rendered.length < LONG_TERM_LIMITS.maxEntries) {
rendered.push(item);
} else {
globalCapped.push(item);
}
}
return { sorted, rendered, typeCapped, globalCapped };
}
export function promotionLimit(source: LongTermSource): number {
if (source === "manual") return PROMOTION_RETRY_LIMITS.maxManualAttempts;
return PROMOTION_RETRY_LIMITS.maxExplicitAttempts;
}
export function retentionClockSummary(entries: LongTermMemoryEntry[]): { present: number; missing: number; invalid: number } {
let present = 0;
let missing = 0;
let invalid = 0;
for (const entry of entries) {
if (entry.retentionClock === undefined) {
missing += 1;
} else if (!Number.isFinite(entry.retentionClock) || entry.retentionClock <= 0) {
invalid += 1;
} else {
present += 1;
}
}
return { present, missing, invalid };
}
+69
View File
@@ -0,0 +1,69 @@
import { createHash } from "node:crypto";
import type { EvidenceEventV1 } from "../../src/evidence-log.ts";
import { redactCredentials } from "../../src/redaction.ts";
export function countBy<T extends string>(items: T[]): Map<T, number> {
const counts = new Map<T, number>();
for (const item of items) counts.set(item, (counts.get(item) ?? 0) + 1);
return counts;
}
export function sortedCounts<T extends string>(counts: Map<T, number>): Array<[T, number]> {
return [...counts.entries()].sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0]));
}
export function workspaceRootHash(root: string): string {
return createHash("sha256").update(root).digest("hex").slice(0, 16);
}
export function redactAbsolutePaths(text: string): string {
return text.replace(/(?:^|[\s"'`(=:\[])(\/(?:Users|home|private|tmp|var|opt|Volumes|[^\s"'`)\],;:]+)\/[^\s"'`)\],;:]*)/g, (match, path) => match.replace(path, "<path>"));
}
export function cleanText(text: string, raw: boolean): string {
if (raw) return text;
return redactAbsolutePaths(redactCredentials(text));
}
export function cleanPath(path: string, raw: boolean): string {
return raw ? path : "<path>";
}
export function formatWorkspaceIdentity(workspaceKeyValue: string | undefined, workspaceRoot: string | undefined, raw: boolean): string {
const parts: string[] = [];
if (workspaceKeyValue) parts.push(`workspaceKey=${workspaceKeyValue}`);
if (workspaceRoot) {
parts.push(raw ? `workspaceRoot=${workspaceRoot}` : `workspaceRootHash=${workspaceRootHash(workspaceRoot)}`);
}
return parts.join(" ");
}
export function truncate(text: string, max = 120): string {
const collapsed = text.replace(/\s+/g, " ").trim();
return collapsed.length <= max ? collapsed : `${collapsed.slice(0, max - 1)}`;
}
export function canonicalMemoryText(text: string): string {
return text
.normalize("NFKC")
.toLowerCase()
.replace(/[\s\p{P}]+/gu, " ")
.trim();
}
export function formatPercent(ratio: number): string {
return `${(ratio * 100).toFixed(1)}%`;
}
export function uniqueStrings(values: string[]): string[] {
return [...new Set(values.filter(Boolean))];
}
export function objectFromCounts<T extends string>(counts: Map<T, number>): Record<string, number> {
return Object.fromEntries(sortedCounts(counts));
}
export function formatDetails(details: EvidenceEventV1["details"]): string {
if (!details || Object.keys(details).length === 0) return "none";
return Object.entries(details).map(([key, value]) => `${key}=${Array.isArray(value) ? value.join("|") : String(value)}`).join(" ");
}
+30
View File
@@ -0,0 +1,30 @@
import type { EvidenceEventV1 } from "../../src/evidence-log.ts";
import { uniqueStrings } from "./text.ts";
import { statusFromOmissionReason } from "./workspace-snapshot.ts";
export function statusFromTraceEvent(event: EvidenceEventV1 | undefined): string {
if (!event) return "unknown";
if (event.type === "render_selected") return "rendered";
if (event.type === "render_omitted") return statusFromOmissionReason(event.reasonCodes[0]);
if (event.type === "promotion_absorbed_exact" || event.type === "promotion_absorbed_identity") return "omitted_absorbed_duplicate";
if (event.type === "promotion_retry_scheduled") return "pending_retry";
if (event.type === "promotion_rejected_capacity" || event.type === "promotion_retry_exhausted") return "pending_rejected_capacity";
if (event.type === "storage_corrupt_json_quarantined") return "quarantined_corrupt_store";
if (event.outcome === "superseded") return "omitted_superseded";
return event.outcome;
}
export function formatTraceEvent(event: EvidenceEventV1): string {
const reasons = event.reasonCodes.length > 0 ? event.reasonCodes.join(",") : "none";
const relations = (event.relations ?? [])
.map(relation => relation.memory?.memoryId ? `${relation.role}=${relation.memory.memoryId}` : undefined)
.filter((value): value is string => Boolean(value));
const relationText = relations.length > 0 ? `; ${relations.join(", ")}` : "";
return `- ${event.eventId} ${event.type}: ${event.outcome}; reasons=${reasons}${relationText}`;
}
export function relationMemoryIds(events: EvidenceEventV1[], role: string): string[] {
return uniqueStrings(events.flatMap(event => (event.relations ?? [])
.filter(relation => relation.role === role)
.map(relation => relation.memory?.memoryId ?? "")));
}
+169
View File
@@ -0,0 +1,169 @@
import type { EvidenceEventType, EvidenceEventV1, EvidenceOutcome } from "../../src/evidence-log.ts";
import type { LongTermMemoryEntry, LongTermSource, LongTermType, PendingMemoryJournalStore, WorkspaceMemoryStore } from "../../src/types.ts";
import type { Command } from "./command-metadata.ts";
export type { Command, HiddenCommand, VisibleCommand } from "./command-metadata.ts";
export type MemoryRenderStatus =
| "rendered"
| "omitted_superseded"
| "omitted_type_cap"
| "omitted_global_cap"
| "omitted_char_budget"
| "omitted_absorbed_duplicate"
| "pending_retry"
| "pending_rejected_capacity"
| "quarantined_corrupt_store";
export type MemoryDiagJSON = {
version: 1;
workspace: { rootHash: string; key: string };
generatedAt: string;
summary: {
storedActive: number;
rendered: number;
pending: number;
rejectedLast7Days: number;
corruptStoresQuarantinedLast30Days: number;
status?: "ok" | "warning" | "degraded";
evidenceCoveragePercent?: number;
needsAttention?: string[];
suggestedNextSteps?: string[];
};
memories: Array<{
id: string;
type: "feedback" | "project" | "decision" | "reference";
source: "explicit" | "compaction" | "manual";
status: MemoryRenderStatus;
strength?: number;
reasonCodes: string[];
textPreview?: string;
evidenceEventIds: string[];
}>;
recentEvents: Array<{
eventId: string;
type: EvidenceEventType;
outcome: EvidenceOutcome;
createdAt: string;
memoryId?: string;
reasonCodes: string[];
}>;
};
export type Origin = "explicit_trigger" | "compaction_candidate" | "manual" | "migration_check" | "unknown";
export type CliOptions = {
raw: boolean;
json?: boolean;
verbose?: boolean;
noEmoji?: boolean;
workspace?: string;
all?: boolean;
softOnly?: boolean;
triggerOnly?: boolean;
includeHistorical?: boolean;
reason?: string;
explain?: boolean;
since?: string;
migration?: string;
memory?: string;
event?: string;
apply?: boolean;
positional?: string[];
auditMode?: "coverage" | "migrations";
};
export type ParsedArgs =
| { ok: true; command: Command; options: CliOptions; deprecationNotice?: string }
| { ok: true; help: true; usage: string }
| { ok: false; message: string; usage: string; exitCode: number };
export type CommandResult = { stdout: string; stderr?: string; exitCode?: number };
export class CliInputError extends Error {}
export type RejectionLogRecord = {
timestamp?: string;
workspaceKey?: string;
workspaceRoot?: string;
workspaceRootHash?: string;
type?: LongTermType;
source?: LongTermSource | string;
origin?: string;
fromTrigger?: boolean;
text?: string;
reasons?: string[];
producerName?: string;
producerVersion?: string;
instrumentationVersion?: number;
decisionLogicName?: string;
decisionLogicVersion?: number;
};
export type NormalizedRejection = Required<Pick<RejectionLogRecord, "timestamp" | "type" | "text" | "reasons">> & {
workspaceKey?: string;
workspaceRoot?: string;
workspaceRootHash?: string;
source?: string;
origin: Origin;
fromTrigger: boolean;
producerName?: string;
producerVersion?: string;
instrumentationVersion?: number;
decisionLogicName?: string;
decisionLogicVersion?: number;
};
export type MigrationLogRecord = {
migrationId?: string;
timestamp?: string;
workspaceKey?: string;
workspaceRoot?: string;
entryId?: string;
type?: LongTermType;
source?: LongTermSource | string;
text?: string;
reasons?: string[];
hardReasons?: string[];
beforeStatus?: string;
afterStatus?: string;
};
export type RetentionDiagItem = {
entry: LongTermMemoryEntry;
strength: number;
};
export type WorkspaceDiagSnapshot = {
store: WorkspaceMemoryStore;
journal: PendingMemoryJournalStore;
retention: {
sorted: RetentionDiagItem[];
rendered: RetentionDiagItem[];
typeCapped: RetentionDiagItem[];
globalCapped: RetentionDiagItem[];
};
memories: MemoryDiagJSON["memories"];
recentEvents: MemoryDiagJSON["recentEvents"];
allEvents: EvidenceEventV1[];
summary: MemoryDiagJSON["summary"];
};
export type MemoryInspectionReadModel = {
snapshot: WorkspaceDiagSnapshot;
store: WorkspaceMemoryStore;
pending: PendingMemoryJournalStore;
evidenceEvents: EvidenceEventV1[];
rejectionRecords: NormalizedRejection[];
currentById: Map<string, LongTermMemoryEntry>;
evidenceByMemoryId: Map<string, EvidenceEventV1[]>;
};
export type CoverageClass = "full_lifecycle" | "render_only" | "no_evidence" | "historical_absent_with_reason" | "historical_absent_unknown_reason";
export type DisappearanceReason = {
classification: "historical_absent_with_reason" | "historical_absent_unknown_reason";
terminalType: EvidenceEventType | "unknown";
reasonCodes: string[];
event?: EvidenceEventV1;
};
+236
View File
@@ -0,0 +1,236 @@
import { calculateRetentionStrength } from "../../src/retention.ts";
import {
queryEvidenceEvents,
type EvidenceEventV1,
} from "../../src/evidence-log.ts";
import { workspaceKey, workspaceMemoryPath, workspacePendingJournalPath } from "../../src/paths.ts";
import { accountWorkspaceMemoryRender } from "../../src/workspace-memory.ts";
import type { LongTermMemoryEntry, PendingMemoryJournalStore, WorkspaceMemoryStore } from "../../src/types.ts";
import { LONG_TERM_LIMITS } from "../../src/types.ts";
import { readJSONFile } from "./io.ts";
import { groupEvidenceByMemoryId } from "./evidence-model.ts";
import { promotionLimit, retentionCandidatesForDiag } from "./retention-model.ts";
import { cleanText, truncate, uniqueStrings, workspaceRootHash } from "./text.ts";
import type { CliOptions, MemoryDiagJSON, MemoryRenderStatus, WorkspaceDiagSnapshot } from "./types.ts";
export function emptyStore(root: string, key: string): WorkspaceMemoryStore {
return {
version: 1,
workspace: { root, key },
limits: { maxRenderedChars: LONG_TERM_LIMITS.maxRenderedChars, maxEntries: LONG_TERM_LIMITS.maxEntries },
entries: [],
migrations: [],
updatedAt: new Date(0).toISOString(),
};
}
export function normalizedStore(store: WorkspaceMemoryStore | null, root: string, key: string): WorkspaceMemoryStore {
const fallback = emptyStore(root, key);
return {
...fallback,
...(store ?? {}),
workspace: store?.workspace ?? fallback.workspace,
limits: {
maxRenderedChars: store?.limits?.maxRenderedChars ?? fallback.limits.maxRenderedChars,
maxEntries: store?.limits?.maxEntries ?? fallback.limits.maxEntries,
},
entries: Array.isArray(store?.entries) ? store.entries : [],
migrations: Array.isArray(store?.migrations) ? store.migrations : [],
};
}
export function normalizedJournal(journal: PendingMemoryJournalStore | null): PendingMemoryJournalStore {
return {
version: 1,
workspace: journal?.workspace ?? { root: "", key: "" },
entries: Array.isArray(journal?.entries) ? journal.entries : [],
updatedAt: journal?.updatedAt ?? new Date(0).toISOString(),
};
}
export function eventMemoryId(event: EvidenceEventV1): string | undefined {
return event.memory?.memoryId
?? event.relations?.map(relation => relation.memory?.memoryId).find((id): id is string => Boolean(id));
}
export function isWithinDays(iso: string, days: number, now = Date.now()): boolean {
const ms = new Date(iso).getTime();
return Number.isFinite(ms) && ms >= now - days * 86_400_000;
}
export function renderStatusReason(status: MemoryRenderStatus, fallback?: string): string[] {
switch (status) {
case "rendered": return ["within_caps", "within_char_budget"];
case "omitted_superseded": return ["superseded"];
case "omitted_type_cap": return ["type_cap"];
case "omitted_global_cap": return ["global_cap"];
case "omitted_char_budget": return [fallback === "empty_render_budget" ? "empty_render_budget" : "char_budget"];
case "omitted_absorbed_duplicate": return ["absorbed_duplicate"];
case "pending_retry": return ["retryable_capacity_rejection"];
case "pending_rejected_capacity": return ["capacity_rejected", "max_attempts_reached"];
case "quarantined_corrupt_store": return ["invalid_json"];
}
}
export function statusFromOmissionReason(reason: string | undefined): MemoryRenderStatus {
if (reason === "superseded") return "omitted_superseded";
if (reason === "type_cap") return "omitted_type_cap";
if (reason === "global_cap") return "omitted_global_cap";
return "omitted_char_budget";
}
export function pendingStatus(entry: LongTermMemoryEntry): MemoryRenderStatus {
const attempts = entry.promotionAttempts ?? 0;
return attempts >= promotionLimit(entry.source) ? "pending_rejected_capacity" : "pending_retry";
}
export function safeTextPreview(text: string): string {
return truncate(cleanText(text, false), 120);
}
function evidenceSummaryForMemory(grouped: Map<string, EvidenceEventV1[]>, memoryId: string): { eventIds: string[]; reasonCodes: string[] } {
const events = grouped.get(memoryId) ?? [];
const reasonCodes = new Set<string>();
for (const event of events) {
for (const reason of event.reasonCodes) reasonCodes.add(reason);
}
return {
eventIds: events.map(event => event.eventId),
reasonCodes: [...reasonCodes],
};
}
export async function buildWorkspaceDiagSnapshot(input: {
root: string;
key: string;
memoryPath: string;
pendingPath: string;
}, now = Date.now()): Promise<WorkspaceDiagSnapshot> {
const rawStore = await readJSONFile<WorkspaceMemoryStore>(input.memoryPath);
const storeRoot = rawStore?.workspace?.root ?? input.root;
const storeKey = rawStore?.workspace?.key ?? input.key;
const store = normalizedStore(rawStore, storeRoot, storeKey);
const journal = normalizedJournal(await readJSONFile<PendingMemoryJournalStore>(input.pendingPath));
const retention = retentionCandidatesForDiag(store, now);
const renderAccounting = accountWorkspaceMemoryRender(store);
const renderedIds = new Set(renderAccounting.rendered.map(memory => memory.id));
const omittedById = new Map(renderAccounting.omitted.map(item => [item.memory.id, item.reason]));
const allEvents = await queryEvidenceEvents(input.root);
const evidenceByMemoryId = groupEvidenceByMemoryId(allEvents);
const recentEvidence = await queryEvidenceEvents(input.root, { newestFirst: true, limit: 50 });
const memoryRows: MemoryDiagJSON["memories"] = [];
const seenIds = new Set<string>();
for (const entry of store.entries) {
const omissionReason = omittedById.get(entry.id);
const status: MemoryRenderStatus = renderedIds.has(entry.id)
? "rendered"
: statusFromOmissionReason(omissionReason ?? (entry.status === "superseded" ? "superseded" : undefined));
const summary = evidenceSummaryForMemory(evidenceByMemoryId, entry.id);
memoryRows.push({
id: entry.id,
type: entry.type,
source: entry.source,
status,
strength: calculateRetentionStrength(entry, now, store.lastActivityAt),
reasonCodes: uniqueStrings([...renderStatusReason(status, omissionReason), ...summary.reasonCodes]),
textPreview: safeTextPreview(entry.text),
evidenceEventIds: summary.eventIds,
});
seenIds.add(entry.id);
}
for (const entry of journal.entries) {
const status = pendingStatus(entry);
const summary = evidenceSummaryForMemory(evidenceByMemoryId, entry.id);
memoryRows.push({
id: entry.id,
type: entry.type,
source: entry.source,
status,
strength: calculateRetentionStrength(entry, now, store.lastActivityAt),
reasonCodes: uniqueStrings([...renderStatusReason(status), entry.lastPromotionFailureReason ?? "", ...summary.reasonCodes]),
textPreview: safeTextPreview(entry.text),
evidenceEventIds: summary.eventIds,
});
seenIds.add(entry.id);
}
for (const event of allEvents) {
if (event.outcome !== "absorbed") continue;
const memory = event.memory;
if (!memory?.memoryId || !memory.type || !memory.source || seenIds.has(memory.memoryId)) continue;
const summary = evidenceSummaryForMemory(evidenceByMemoryId, memory.memoryId);
memoryRows.push({
id: memory.memoryId,
type: memory.type,
source: memory.source,
status: "omitted_absorbed_duplicate",
reasonCodes: uniqueStrings([...renderStatusReason("omitted_absorbed_duplicate"), ...summary.reasonCodes]),
evidenceEventIds: summary.eventIds.length > 0 ? summary.eventIds : [event.eventId],
});
seenIds.add(memory.memoryId);
}
const recentEvents = recentEvidence.map(event => ({
eventId: event.eventId,
type: event.type,
outcome: event.outcome,
createdAt: event.createdAt,
memoryId: eventMemoryId(event),
reasonCodes: uniqueStrings([
...event.reasonCodes,
...(event.type === "storage_corrupt_json_quarantined" ? ["quarantined_corrupt_store"] : []),
]),
}));
return {
store,
journal,
retention,
memories: memoryRows,
recentEvents,
allEvents,
summary: {
storedActive: store.entries.filter(entry => entry.status !== "superseded").length,
rendered: retention.rendered.length,
pending: journal.entries.length,
rejectedLast7Days: allEvents.filter(event => event.outcome === "rejected" && isWithinDays(event.createdAt, 7, now)).length,
corruptStoresQuarantinedLast30Days: allEvents.filter(event => event.type === "storage_corrupt_json_quarantined" && isWithinDays(event.createdAt, 30, now)).length,
},
};
}
export async function buildMemoryDiagJSON(root: string): Promise<MemoryDiagJSON> {
const key = await workspaceKey(root);
const snapshot = await buildWorkspaceDiagSnapshot({
root,
key,
memoryPath: await workspaceMemoryPath(root),
pendingPath: await workspacePendingJournalPath(root),
});
return memoryDiagJSONFromSnapshot(root, snapshot);
}
export function memoryDiagJSONFromSnapshot(root: string, snapshot: WorkspaceDiagSnapshot, generatedAt = new Date().toISOString()): MemoryDiagJSON {
return {
version: 1,
workspace: { rootHash: workspaceRootHash(snapshot.store.workspace.root || root), key: snapshot.store.workspace.key },
generatedAt,
summary: snapshot.summary,
memories: snapshot.memories,
recentEvents: snapshot.recentEvents,
};
}
export async function snapshotForOptions(options: CliOptions): Promise<WorkspaceDiagSnapshot> {
const root = options.workspace ?? process.cwd();
const key = await workspaceKey(root);
return buildWorkspaceDiagSnapshot({
root,
key,
memoryPath: await workspaceMemoryPath(root),
pendingPath: await workspacePendingJournalPath(root),
});
}
+547
View File
@@ -0,0 +1,547 @@
import { createHash } from "node:crypto";
import { existsSync } from "node:fs";
import { appendFile, mkdir, readFile, realpath, rename, rm, stat, writeFile } from "node:fs/promises";
import { dirname, join } from "node:path";
import { dataHome, workspaceEvidenceLogPath, workspaceKey } from "./paths.ts";
import { redactCredentials } from "./redaction.ts";
import { producerFields } from "./instrumentation.ts";
export type EvidenceEventType =
| "extraction_candidate_accepted"
| "extraction_candidate_rejected"
| "explicit_memory_detected"
| "explicit_memory_ignored"
| "pending_memory_appended"
| "pending_memory_cleared"
| "promotion_promoted"
| "promotion_absorbed_exact"
| "promotion_absorbed_identity"
| "promotion_superseded"
| "promotion_rejected_capacity"
| "promotion_retry_scheduled"
| "promotion_retry_exhausted"
| "memory_reinforced"
| "memory_replaced_numbered_ref"
| "memory_reverted_numbered_ref"
| "memory_migration_superseded"
| "render_selected"
| "render_omitted"
| "memory_removed_capacity"
| "storage_corrupt_json_quarantined"
| "storage_stale_lock_recovered"
| "storage_lock_timeout"
| "hook_failed";
export type EvidencePhase =
| "extraction"
| "explicit"
| "pending_journal"
| "promotion"
| "reinforcement"
| "render"
| "storage"
| "hook";
export type EvidenceOutcome =
| "accepted"
| "rejected"
| "promoted"
| "absorbed"
| "superseded"
| "rendered"
| "omitted"
| "removed"
| "retried"
| "exhausted"
| "reinforced"
| "quarantined"
| "failed"
| "recovered";
export type MemoryEvidenceRef = {
memoryId?: string;
memoryKeyHash?: string;
identityKeyHash?: string;
type?: "feedback" | "project" | "decision" | "reference";
source?: "explicit" | "compaction" | "manual";
status?: "active" | "superseded";
};
export type EvidenceRelation = {
role:
| "candidate"
| "pending"
| "promoted"
| "retained"
| "absorbed"
| "target"
| "superseded"
| "superseded_by"
| "reinforced"
| "reinforced_by"
| "recovered"
| "rendered"
| "omitted"
| "removed";
memory?: MemoryEvidenceRef;
};
export type EvidenceDetailValue = string | number | boolean | null | string[] | number[];
export type EvidenceEventV1 = {
version: 1;
eventId: string;
createdAt: string;
workspaceKey: string;
workspaceRootHash: string;
sessionHash?: string;
messageHash?: string;
producerName?: string;
producerVersion?: string;
instrumentationVersion?: number;
type: EvidenceEventType;
phase: EvidencePhase;
outcome: EvidenceOutcome;
memory?: MemoryEvidenceRef;
relations?: EvidenceRelation[];
reasonCodes: string[];
details?: Record<string, EvidenceDetailValue>;
textPreview?: string;
};
export type EvidenceEventInput = Omit<
EvidenceEventV1,
"version" | "eventId" | "createdAt" | "workspaceKey" | "workspaceRootHash"
>;
export type EvidenceQuery = {
since?: string;
until?: string;
types?: EvidenceEventType[];
phases?: EvidencePhase[];
outcomes?: EvidenceOutcome[];
memoryId?: string;
memoryKeyHash?: string;
identityKeyHash?: string;
sessionHash?: string;
limit?: number;
newestFirst?: boolean;
};
export type MemoryEvidenceSummary = {
memoryId?: string;
memoryKeyHash?: string;
latestOutcome?: EvidenceOutcome;
latestRenderStatus?: "rendered" | "omitted";
reasonCodes: string[];
eventIds: string[];
lastEventAt?: string;
};
export type MemoryLifecycleTrace = {
memoryId?: string;
memoryKeyHash?: string;
identityKeyHash?: string;
events: EvidenceEventV1[];
createdBy?: EvidenceEventV1;
acceptedBy?: EvidenceEventV1;
promotedBy?: EvidenceEventV1;
absorbedBy?: EvidenceEventV1;
supersededBy?: EvidenceEventV1;
reinforcedBy: EvidenceEventV1[];
latestRender?: EvidenceEventV1;
currentStatus:
| "accepted"
| "pending"
| "promoted"
| "absorbed"
| "superseded"
| "rendered"
| "omitted"
| "rejected"
| "unknown";
};
export const EVIDENCE_LOG_LIMITS = {
maxAgeDays: 90,
maxEventsPerWorkspace: 5000,
maxBytesPerWorkspace: 2 * 1024 * 1024,
pruneEveryAppendCount: 100,
} as const;
const appendCounts = new Map<string, number>();
const HASH_PATTERN = /^[a-f0-9]{16}$/i;
const DAY_MS = 24 * 60 * 60 * 1000;
const MAX_DETAIL_STRING_CHARS = 240;
const MAX_DETAIL_ARRAY_ITEMS = 25;
function evidenceHash(value: string): string {
return createHash("sha256").update(value).digest("hex").slice(0, 16);
}
function normalizeHashValue(value: string | undefined): string | undefined {
if (!value) return undefined;
return HASH_PATTERN.test(value) ? value.toLowerCase() : evidenceHash(value);
}
async function resolvedRoot(root: string): Promise<string> {
return realpath(root).catch(() => root);
}
function evidenceTextPreview(text: string, maxChars = 120): string {
return redactCredentials(text).replace(/\s+/g, " ").trim().slice(0, maxChars);
}
function sanitizeReasonCode(reason: string): string {
return reason.replace(/[^a-zA-Z0-9_.:-]/g, "_").slice(0, 120);
}
function sanitizeMemoryRef(memory: MemoryEvidenceRef | undefined): MemoryEvidenceRef | undefined {
if (!memory) return undefined;
const sanitized: MemoryEvidenceRef = {};
if (typeof memory.memoryId === "string" && memory.memoryId) sanitized.memoryId = memory.memoryId.slice(0, 160);
if (memory.memoryKeyHash) sanitized.memoryKeyHash = normalizeHashValue(memory.memoryKeyHash);
if (memory.identityKeyHash) sanitized.identityKeyHash = normalizeHashValue(memory.identityKeyHash);
if (memory.type) sanitized.type = memory.type;
if (memory.source) sanitized.source = memory.source;
if (memory.status) sanitized.status = memory.status;
return Object.keys(sanitized).length > 0 ? sanitized : undefined;
}
function sanitizeRelations(relations: EvidenceRelation[] | undefined): EvidenceRelation[] | undefined {
if (!relations) return undefined;
const sanitized = relations
.map(relation => ({
role: relation.role,
memory: sanitizeMemoryRef(relation.memory),
}))
.slice(0, 25);
return sanitized.length > 0 ? sanitized : undefined;
}
function sanitizeDetailString(value: string): string {
return evidenceTextPreview(value, MAX_DETAIL_STRING_CHARS);
}
function sanitizeDetails(details: EvidenceEventInput["details"]): EvidenceEventV1["details"] {
if (!details) return undefined;
const sanitized: Record<string, EvidenceDetailValue> = {};
for (const [rawKey, rawValue] of Object.entries(details).slice(0, 50)) {
const key = rawKey.replace(/[^a-zA-Z0-9_.:-]/g, "_").slice(0, 80);
if (!key) continue;
if (typeof rawValue === "string") {
sanitized[key] = sanitizeDetailString(rawValue);
} else if (typeof rawValue === "number") {
if (Number.isFinite(rawValue)) sanitized[key] = rawValue;
} else if (typeof rawValue === "boolean" || rawValue === null) {
sanitized[key] = rawValue;
} else if (Array.isArray(rawValue)) {
if (rawValue.every(item => typeof item === "string")) {
sanitized[key] = rawValue.slice(0, MAX_DETAIL_ARRAY_ITEMS).map(item => sanitizeDetailString(item));
} else if (rawValue.every(item => typeof item === "number" && Number.isFinite(item))) {
sanitized[key] = rawValue.slice(0, MAX_DETAIL_ARRAY_ITEMS) as number[];
}
}
}
return Object.keys(sanitized).length > 0 ? sanitized : undefined;
}
function buildEvidenceEvent(
input: EvidenceEventInput,
workspaceKeyValue: string,
workspaceRootHash: string,
): EvidenceEventV1 {
const textPreviewMax = input.type === "extraction_candidate_rejected" ? 80 : 120;
const event: EvidenceEventV1 = {
version: 1,
eventId: `evt_${Date.now()}_${Math.random().toString(36).slice(2, 10).padEnd(8, "0")}`,
createdAt: new Date().toISOString(),
workspaceKey: workspaceKeyValue,
workspaceRootHash,
type: input.type,
phase: input.phase,
outcome: input.outcome,
reasonCodes: input.reasonCodes.map(sanitizeReasonCode).filter(Boolean).slice(0, 25),
};
const memory = sanitizeMemoryRef(input.memory);
const relations = sanitizeRelations(input.relations);
const details = sanitizeDetails(input.details);
if (input.sessionHash) event.sessionHash = normalizeHashValue(input.sessionHash);
if (input.messageHash) event.messageHash = normalizeHashValue(input.messageHash);
if (memory) event.memory = memory;
if (relations) event.relations = relations;
if (details) event.details = details;
if (input.textPreview) event.textPreview = evidenceTextPreview(input.textPreview, textPreviewMax);
return {
...event,
...producerFields(),
};
}
async function safeAppendEvidenceLine(path: string, line: string): Promise<void> {
// Evidence logs are JSONL append streams, not JSON store read-modify-write
// documents. Appends intentionally use appendFile so independent evidence
// writers do not need to share the JSON store lock path.
try {
await mkdir(dirname(path), { recursive: true });
await appendFile(path, `${line}\n`, "utf8");
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
console.error(`[memory] failed to write evidence event: ${message}`);
}
}
async function maybePruneEvidenceLog(path: string): Promise<void> {
// Bounded pruning is a separate best-effort compaction of the append-only log.
// It rewrites the JSONL file only at configured append intervals and never
// routes through updateJSON because evidence is not a single JSON document.
const nextCount = (appendCounts.get(path) ?? 0) + 1;
appendCounts.set(path, nextCount);
if (nextCount % EVIDENCE_LOG_LIMITS.pruneEveryAppendCount !== 0) return;
try {
await pruneEvidenceLogPath(path);
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
console.error(`[memory] failed to prune evidence log: ${message}`);
}
}
export async function appendEvidenceEvent(root: string, event: EvidenceEventInput): Promise<EvidenceEventV1> {
const records = await appendEvidenceEvents(root, [event]);
return records[0];
}
export async function appendEvidenceEvents(root: string, events: EvidenceEventInput[]): Promise<EvidenceEventV1[]> {
const path = await workspaceEvidenceLogPath(root);
const rootPath = await resolvedRoot(root);
const workspaceRootHash = evidenceHash(rootPath);
const workspaceKeyValue = await workspaceKey(root);
const records = events.map(event => buildEvidenceEvent(event, workspaceKeyValue, workspaceRootHash));
for (const record of records) {
await safeAppendEvidenceLine(path, JSON.stringify(record));
await maybePruneEvidenceLog(path);
}
return records;
}
export async function appendEvidenceEventForWorkspaceKey(
workspaceKeyValue: string,
event: EvidenceEventInput,
): Promise<EvidenceEventV1> {
const path = join(dataHome(), "opencode-working-memory", "workspaces", workspaceKeyValue, "evidence", "events.jsonl");
const record = buildEvidenceEvent(event, workspaceKeyValue, workspaceKeyValue);
await safeAppendEvidenceLine(path, JSON.stringify(record));
await maybePruneEvidenceLog(path);
return record;
}
type ParsedEvidenceLine = {
event: EvidenceEventV1;
index: number;
};
function parseEvidenceLine(line: string): EvidenceEventV1 | null {
try {
const parsed = JSON.parse(line) as Partial<EvidenceEventV1>;
if (parsed.version !== 1 || !parsed.eventId || !parsed.createdAt || !parsed.type) return null;
return parsed as EvidenceEventV1;
} catch {
return null;
}
}
async function readEvidenceLines(path: string, warnInvalid: boolean): Promise<{ valid: ParsedEvidenceLine[]; invalid: string[] }> {
if (!existsSync(path)) return { valid: [], invalid: [] };
const raw = await readFile(path, "utf8");
const valid: ParsedEvidenceLine[] = [];
const invalid: string[] = [];
raw.split(/\n/).forEach((line, index) => {
if (!line.trim()) return;
const event = parseEvidenceLine(line);
if (event) {
valid.push({ event, index });
} else {
invalid.push(line);
if (warnInvalid) console.warn(`[memory] skipped invalid evidence log line ${index + 1}`);
}
});
return { valid, invalid };
}
function eventTimeMs(event: EvidenceEventV1): number {
const ms = new Date(event.createdAt).getTime();
return Number.isFinite(ms) ? ms : 0;
}
async function atomicWriteText(path: string, text: string): Promise<void> {
await mkdir(dirname(path), { recursive: true });
const tmp = `${path}.${process.pid}.${Date.now()}.${Math.random().toString(36).slice(2, 10)}.tmp`;
try {
await writeFile(tmp, text, { encoding: "utf8", mode: 0o600 });
await rename(tmp, path);
} catch (error) {
await rm(tmp, { force: true }).catch(() => undefined);
throw error;
}
}
function serializeEvents(events: EvidenceEventV1[]): string {
return events.map(event => JSON.stringify(event)).join("\n") + (events.length > 0 ? "\n" : "");
}
function trimEventsToByteLimit(events: EvidenceEventV1[]): EvidenceEventV1[] {
let kept = [...events];
while (kept.length > 0 && Buffer.byteLength(serializeEvents(kept), "utf8") > EVIDENCE_LOG_LIMITS.maxBytesPerWorkspace) {
kept = kept.slice(1);
}
return kept;
}
async function pruneEvidenceLogPath(path: string): Promise<void> {
if (!existsSync(path)) return;
const stats = await stat(path);
if (stats.isDirectory()) return;
const { valid, invalid } = await readEvidenceLines(path, false);
if (invalid.length > 0) {
const corruptPath = `${path}.corrupt-lines-${Date.now()}.jsonl`;
await writeFile(corruptPath, invalid.join("\n") + "\n", { encoding: "utf8", mode: 0o600 }).catch(error => {
const message = error instanceof Error ? error.message : String(error);
console.error(`[memory] failed to quarantine invalid evidence lines: ${message}`);
});
}
const cutoff = Date.now() - EVIDENCE_LOG_LIMITS.maxAgeDays * DAY_MS;
let events = valid
.filter(item => eventTimeMs(item.event) >= cutoff)
.sort((a, b) => eventTimeMs(a.event) - eventTimeMs(b.event) || a.index - b.index)
.map(item => item.event);
if (events.length > EVIDENCE_LOG_LIMITS.maxEventsPerWorkspace) {
events = events.slice(events.length - EVIDENCE_LOG_LIMITS.maxEventsPerWorkspace);
}
events = trimEventsToByteLimit(events);
await atomicWriteText(path, serializeEvents(events));
}
function memoryRefMatches(memory: MemoryEvidenceRef | undefined, query: Pick<EvidenceQuery, "memoryId" | "memoryKeyHash" | "identityKeyHash">): boolean {
if (!memory) return false;
const memoryKeyHash = normalizeHashValue(query.memoryKeyHash);
const identityKeyHash = normalizeHashValue(query.identityKeyHash);
if (query.memoryId && memory.memoryId === query.memoryId) return true;
if (memoryKeyHash && memory.memoryKeyHash === memoryKeyHash) return true;
if (identityKeyHash && memory.identityKeyHash === identityKeyHash) return true;
return false;
}
function eventMatchesMemory(event: EvidenceEventV1, query: Pick<EvidenceQuery, "memoryId" | "memoryKeyHash" | "identityKeyHash">): boolean {
if (!query.memoryId && !query.memoryKeyHash && !query.identityKeyHash) return true;
if (memoryRefMatches(event.memory, query)) return true;
return (event.relations ?? []).some(relation => memoryRefMatches(relation.memory, query));
}
export async function queryEvidenceEvents(
root: string,
query: EvidenceQuery = {},
): Promise<EvidenceEventV1[]> {
const path = await workspaceEvidenceLogPath(root);
const { valid } = await readEvidenceLines(path, true);
const sinceMs = query.since ? new Date(query.since).getTime() : undefined;
const untilMs = query.until ? new Date(query.until).getTime() : undefined;
const sessionHash = normalizeHashValue(query.sessionHash);
let events = valid.map(item => item.event).filter(event => {
const createdAtMs = eventTimeMs(event);
if (Number.isFinite(sinceMs) && createdAtMs < sinceMs) return false;
if (Number.isFinite(untilMs) && createdAtMs > untilMs) return false;
if (query.types && !query.types.includes(event.type)) return false;
if (query.phases && !query.phases.includes(event.phase)) return false;
if (query.outcomes && !query.outcomes.includes(event.outcome)) return false;
if (sessionHash && event.sessionHash !== sessionHash) return false;
if (!eventMatchesMemory(event, query)) return false;
return true;
});
if (query.newestFirst) events = events.slice().reverse();
if (typeof query.limit === "number" && query.limit >= 0) events = events.slice(0, query.limit);
return events;
}
export async function summarizeMemoryEvidence(
root: string,
input: { memoryId?: string; memoryKeyHash?: string },
): Promise<MemoryEvidenceSummary> {
const events = await queryEvidenceEvents(root, input);
const latest = events.at(-1);
const latestRender = events.filter(event => event.phase === "render").at(-1);
const reasonCodes = new Set<string>();
for (const event of events) {
for (const reason of event.reasonCodes) reasonCodes.add(reason);
}
return {
memoryId: input.memoryId,
memoryKeyHash: normalizeHashValue(input.memoryKeyHash),
latestOutcome: latest?.outcome,
latestRenderStatus: latestRender?.outcome === "rendered" || latestRender?.outcome === "omitted"
? latestRender.outcome
: undefined,
reasonCodes: [...reasonCodes],
eventIds: events.map(event => event.eventId),
lastEventAt: latest?.createdAt,
};
}
function currentStatusFromEvent(event: EvidenceEventV1 | undefined): MemoryLifecycleTrace["currentStatus"] {
if (!event) return "unknown";
if (event.outcome === "accepted") return "accepted";
if (event.outcome === "promoted") return "promoted";
if (event.outcome === "absorbed") return "absorbed";
if (event.outcome === "superseded") return "superseded";
if (event.outcome === "rendered") return "rendered";
if (event.outcome === "omitted") return "omitted";
if (event.outcome === "rejected") return "rejected";
if (event.type === "pending_memory_appended") return "pending";
return "unknown";
}
export async function traceMemoryLifecycle(
root: string,
input: { memoryId?: string; memoryKeyHash?: string; identityKeyHash?: string },
): Promise<MemoryLifecycleTrace> {
const events = await queryEvidenceEvents(root, input);
const createdBy = events.find(event => event.type === "explicit_memory_detected" || event.type === "pending_memory_appended");
const acceptedBy = events.find(event => event.type === "extraction_candidate_accepted" || event.type === "explicit_memory_detected");
const promotedBy = events.find(event => event.type === "promotion_promoted");
const absorbedBy = events.find(event => event.outcome === "absorbed");
const supersededBy = events.find(event => event.outcome === "superseded");
const reinforcedBy = events.filter(event => event.type === "memory_reinforced" || event.outcome === "reinforced");
const latestRender = events.filter(event => event.phase === "render").at(-1);
const latest = events.at(-1);
return {
memoryId: input.memoryId,
memoryKeyHash: normalizeHashValue(input.memoryKeyHash),
identityKeyHash: normalizeHashValue(input.identityKeyHash),
events,
createdBy,
acceptedBy,
promotedBy,
absorbedBy,
supersededBy,
reinforcedBy,
latestRender,
currentStatus: currentStatusFromEvent(latest),
};
}
+282 -33
View File
@@ -6,6 +6,8 @@ import { LONG_TERM_LIMITS } from "./types.ts";
import { assessMemoryQuality } from "./memory-quality.ts";
import { extractionRejectionLogPath } from "./paths.ts";
import { redactCredentials } from "./redaction.ts";
import type { EvidenceEventInput } from "./evidence-log.ts";
import { producerFields } from "./instrumentation.ts";
function id(prefix: string): string {
return `${prefix}_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
@@ -46,6 +48,39 @@ function isNegatedMemoryRequest(text: string, matchIndex: number): boolean {
}
export function extractExplicitMemories(text: string): LongTermMemoryEntry[] {
return extractExplicitMemoriesWithEvidence(text).entries;
}
export type WorkspaceMemoryParseResult = {
entries: LongTermMemoryEntry[];
commands: WorkspaceMemoryCommand[];
evidence: EvidenceEventInput[];
};
export type WorkspaceMemoryCommand =
| { kind: "REINFORCE"; ref: string }
| { kind: "REPLACE"; ref: string; type: LongTermType; text: string };
function evidenceTextPreview(text: string, maxChars = 120): string {
return redactCredentials(text).replace(/\s+/g, " ").trim().slice(0, maxChars);
}
function memoryEvidence(memory: LongTermMemoryEntry): EvidenceEventInput["memory"] {
return {
memoryId: memory.id,
type: memory.type,
source: memory.source,
status: memory.status,
};
}
function extractionEvidence(
input: Pick<EvidenceEventInput, "type" | "phase" | "outcome" | "reasonCodes" | "textPreview" | "memory" | "details">,
): EvidenceEventInput {
return input;
}
export function extractExplicitMemoriesWithEvidence(text: string): WorkspaceMemoryParseResult {
// 注意:所有pattern必須有 g flag,因為使用 matchAll()
// Pattern 必須在行首匹配,避免匹配到句子中間的非指令式用法
const patterns = [
@@ -71,29 +106,74 @@ export function extractExplicitMemories(text: string): LongTermMemoryEntry[] {
const nowMs = Date.now();
const now = new Date(nowMs).toISOString();
const entries: LongTermMemoryEntry[] = [];
const evidence: EvidenceEventInput[] = [];
const seen = new Set<string>();
const negatedLinePattern = /(?:^|\n)\s*(?:(?:please\s+)?(?:do\s+not|don't|dont|never)\s+remember(?:\s+(?:this|that))?|不要\s*(?:記住|记住)|別\s*(?:記住|记住)|别\s*(?:記住|记住))[:,]?\s*(.+)$/gim;
for (const match of text.matchAll(negatedLinePattern)) {
evidence.push(extractionEvidence({
type: "explicit_memory_ignored",
phase: "explicit",
outcome: "rejected",
reasonCodes: ["negated_request"],
textPreview: evidenceTextPreview(match[1] ?? match[0], 80),
}));
}
for (const pattern of patterns) {
for (const match of text.matchAll(pattern)) {
const body = match[1]?.trim();
if (!body || body.length < 8) continue;
if (body && /^(再说|再說|later|next time)$/i.test(body)) {
evidence.push(extractionEvidence({
type: "explicit_memory_ignored",
phase: "explicit",
outcome: "rejected",
reasonCodes: ["deferral"],
textPreview: evidenceTextPreview(body, 80),
}));
continue;
}
if (!body || body.length < 8) {
evidence.push(extractionEvidence({
type: "explicit_memory_ignored",
phase: "explicit",
outcome: "rejected",
reasonCodes: ["too_short"],
textPreview: evidenceTextPreview(body ?? match[0], 80),
}));
continue;
}
// Calculate actual trigger position (after possible newline)
const triggerIndex = match.index! + (match[0].match(/^[\s\n]*/)?.[0]?.length || 0);
// Check if this is a negated request (e.g., "不要記住")
if (isNegatedMemoryRequest(text, triggerIndex)) continue;
if (isNegatedMemoryRequest(text, triggerIndex)) {
evidence.push(extractionEvidence({
type: "explicit_memory_ignored",
phase: "explicit",
outcome: "rejected",
reasonCodes: ["negated_request"],
textPreview: evidenceTextPreview(body, 80),
}));
continue;
}
// Check if it's a deferral (e.g., "later", "next time")
if (/^(再说|再說|later|next time)$/i.test(body)) continue;
// Dedupe by canonical body
const key = body.toLowerCase().replace(/\s+/g, " ").trim();
if (seen.has(key)) continue;
if (seen.has(key)) {
evidence.push(extractionEvidence({
type: "explicit_memory_ignored",
phase: "explicit",
outcome: "rejected",
reasonCodes: ["duplicate_in_message"],
textPreview: evidenceTextPreview(body, 80),
}));
continue;
}
seen.add(key);
const type = classifyExplicitMemory(body);
entries.push({
const memory: LongTermMemoryEntry = {
id: id("mem"),
type,
text: body.slice(0, LONG_TERM_LIMITS.maxEntryTextChars),
@@ -104,11 +184,20 @@ export function extractExplicitMemories(text: string): LongTermMemoryEntry[] {
updatedAt: now,
retentionClock: nowMs,
staleAfterDays: staleAfterDaysFor(type),
});
};
entries.push(memory);
evidence.push(extractionEvidence({
type: "explicit_memory_detected",
phase: "explicit",
outcome: "accepted",
reasonCodes: ["explicit_trigger_matched"],
memory: memoryEvidence(memory),
textPreview: evidenceTextPreview(memory.text),
}));
}
}
return entries;
return { entries, commands: [], evidence };
}
function classifyExplicitMemory(text: string): LongTermType {
@@ -239,6 +328,18 @@ type ExtractionRejectionLogEntry = {
text: string;
reasons: string[];
source: "compaction";
workspaceKey?: string;
workspaceRootHash?: string;
producerName?: string;
producerVersion?: string;
instrumentationVersion?: number;
decisionLogicName?: string;
decisionLogicVersion?: number;
};
type WorkspaceMemoryCandidateParseOptions = {
workspaceKey?: string;
workspaceRootHash?: string;
};
async function logExtractionRejection(entry: ExtractionRejectionLogEntry): Promise<void> {
@@ -251,15 +352,15 @@ async function logExtractionRejection(entry: ExtractionRejectionLogEntry): Promi
}
}
function shouldAcceptWorkspaceMemoryCandidate(
function evaluateWorkspaceMemoryCandidate(
entry: {
type: LongTermType;
text: string;
},
options: {
fromMemoryTrigger?: boolean;
} = {},
): boolean {
} & WorkspaceMemoryCandidateParseOptions = {},
): { accepted: boolean; reasons: string[] } {
const text = entry.text.trim();
const minLength = options.fromMemoryTrigger ? 6 : 20;
@@ -267,14 +368,14 @@ function shouldAcceptWorkspaceMemoryCandidate(
if (entry.type === "reference" && /\b(?:admin\s+)?pin\s|scrypt|n=\d+|r=\d+|p=\d+/i.test(text)) {
// Stable config values can be short — allow below generic min length
} else if (text.length < minLength) {
return false;
return { accepted: false, reasons: ["too_short"] };
}
// Indirect Prompt Injection / Adversarial Instructions
// Rejects attempts to overwrite system behavior or "ignore" rules.
// comparative "instead of" is allowed.
if (/\b(ignore\s+all|ignore\s+previous|ignore\s+instruction|overwrite\s+system|overwrite\s+rules|forget\s+all|delete\s+root)\b/i.test(text)) return false;
if (/\b(ignore|instruction|overwrite)\b/i.test(text) && /\b(previous|all|rules|behavior|prompt|system)\b/i.test(text)) return false;
if (/\b(ignore\s+all|ignore\s+previous|ignore\s+instruction|overwrite\s+system|overwrite\s+rules|forget\s+all|delete\s+root)\b/i.test(text)) return { accepted: false, reasons: ["prompt_injection"] };
if (/\b(ignore|instruction|overwrite)\b/i.test(text) && /\b(previous|all|rules|behavior|prompt|system)\b/i.test(text)) return { accepted: false, reasons: ["prompt_injection"] };
const quality = assessMemoryQuality({ type: entry.type, text, source: "compaction" });
if (!quality.accepted) {
@@ -284,11 +385,86 @@ function shouldAcceptWorkspaceMemoryCandidate(
text: redactCredentials(text),
reasons: quality.reasons,
source: "compaction",
workspaceKey: options.workspaceKey,
workspaceRootHash: options.workspaceRootHash,
...producerFields(),
decisionLogicName: "assessMemoryQuality",
decisionLogicVersion: 1,
});
return false;
return { accepted: false, reasons: quality.reasons };
}
return true;
return { accepted: true, reasons: ["quality_gate_passed"] };
}
function commandAttemptReason(line: string): string {
const normalized = line.replace(/^\s*-\s*/, "").trim();
const reinforceMatch = normalized.match(/^REINFORCE\s+(.+)$/i);
if (reinforceMatch) {
return /^\[M[1-9]\d*\]$/i.test(reinforceMatch[1]?.trim() ?? "")
? "invalid_memory_command"
: "invalid_memory_ref";
}
const replaceMatch = normalized.match(/^REPLACE\s+(.*)$/i);
if (!replaceMatch) return "invalid_memory_command";
const rest = replaceMatch[1]?.trim() ?? "";
const refMatch = rest.match(/^(\[[^\]]+\]|\S+)(?:\s+(.*))?$/);
const ref = refMatch?.[1] ?? "";
if (!/^\[M[1-9]\d*\]$/i.test(ref)) return "invalid_memory_ref";
const afterRef = refMatch?.[2]?.trim() ?? "";
const typeMatch = afterRef.match(/^(\[[^\]]+\]|\S+)(?:\s+(.*))?$/);
const typeToken = typeMatch?.[1] ?? "";
if (!/^\[(feedback|project|decision|reference)\]$/i.test(typeToken)) {
return "invalid_memory_type";
}
const replacementText = typeMatch?.[2]?.trim() ?? "";
return replacementText ? "invalid_memory_command" : "empty_replacement_text";
}
function isCommandAttempt(line: string): boolean {
const normalized = line.replace(/^\s*-\s*/, "").trim();
return /^(REINFORCE|REPLACE)\b/i.test(normalized)
|| /\b(REINFORCE|REPLACE)\b.*\[?\w+\]?/i.test(normalized);
}
function parseWorkspaceMemoryCommand(line: string): WorkspaceMemoryCommand | null {
const normalized = line.replace(/^\s*-\s*/, "").trim();
const reinforce = normalized.match(/^REINFORCE\s+\[(M[1-9]\d*)\]\s*$/i);
if (reinforce) {
return { kind: "REINFORCE", ref: reinforce[1].toUpperCase() };
}
const replace = normalized.match(/^REPLACE\s+\[(M[1-9]\d*)\]\s+\[(feedback|project|decision|reference)\]\s+(.+)$/i);
if (replace) {
const text = replace[3].trim();
if (!text) return null;
return {
kind: "REPLACE",
ref: replace[1].toUpperCase(),
type: replace[2].toLowerCase() as LongTermType,
text,
};
}
return null;
}
function parseCandidateLine(line: string): { type: LongTermType; body: string } | null {
const bracketed = line.trim().match(/^\s*-?\s*\[(feedback|project|decision|reference)\]\s+(.+)$/i);
if (bracketed) {
return { type: bracketed[1].toLowerCase() as LongTermType, body: bracketed[2] };
}
const bracketless = line.trim().match(/^-\s*(feedback|project|decision|reference)\b\s+(.+)$/i);
if (bracketless) {
return { type: bracketless[1].toLowerCase() as LongTermType, body: bracketless[2] };
}
return null;
}
/**
@@ -315,34 +491,98 @@ function extractCandidateBlock(summary: string): string | null {
return null;
}
export function parseWorkspaceMemoryCandidates(summary: string): LongTermMemoryEntry[] {
export function parseWorkspaceMemoryCandidates(
summary: string,
options?: WorkspaceMemoryCandidateParseOptions,
): LongTermMemoryEntry[] {
return parseWorkspaceMemoryCandidatesWithEvidence(summary, options).entries;
}
export function parseWorkspaceMemoryCandidatesWithEvidence(
summary: string,
options: WorkspaceMemoryCandidateParseOptions = {},
): WorkspaceMemoryParseResult {
const block = extractCandidateBlock(summary);
if (!block) return [];
if (!block) return { entries: [], commands: [], evidence: [] };
const nowMs = Date.now();
const now = new Date(nowMs).toISOString();
const entries: LongTermMemoryEntry[] = [];
const commands: WorkspaceMemoryCommand[] = [];
const evidence: EvidenceEventInput[] = [];
for (const line of block.split("\n")) {
if (!line.trim() || /^\s*\(?none\)?\s*$/i.test(line)) continue;
const command = parseWorkspaceMemoryCommand(line);
if (command) {
commands.push(command);
continue;
}
// Accept both "- [type] text" (bracketed) and "- type text" (bracketless)
const item = line.trim().match(
/^-\s*(?:\[(feedback|project|decision|reference)\]|(feedback|project|decision|reference)\b)\s+(.+)$/i,
);
if (!item) continue;
const type = (item[1] ?? item[2]).toLowerCase() as LongTermType;
const normalizedBody = normalizeCandidateBody(item[3]);
if (!normalizedBody) continue;
const item = parseCandidateLine(line);
if (!item) {
if (isCommandAttempt(line)) {
evidence.push(extractionEvidence({
type: "extraction_candidate_rejected",
phase: "extraction",
outcome: "rejected",
reasonCodes: [commandAttemptReason(line)],
textPreview: evidenceTextPreview(line, 80),
}));
}
continue;
}
const type = item.type;
const normalizedBody = normalizeCandidateBody(item.body);
if (!normalizedBody) {
evidence.push(extractionEvidence({
type: "extraction_candidate_rejected",
phase: "extraction",
outcome: "rejected",
reasonCodes: ["negated_request"],
memory: { type, source: "compaction" },
textPreview: evidenceTextPreview(item.body, 80),
}));
continue;
}
const minLength = normalizedBody.hadTrigger ? 6 : 12;
if (normalizedBody.text.length < minLength) continue;
if (normalizedBody.text.length < minLength) {
evidence.push(extractionEvidence({
type: "extraction_candidate_rejected",
phase: "extraction",
outcome: "rejected",
reasonCodes: ["too_short"],
memory: { type, source: "compaction" },
textPreview: evidenceTextPreview(normalizedBody.text, 80),
}));
continue;
}
// Apply quality gate
if (!shouldAcceptWorkspaceMemoryCandidate(
const quality = evaluateWorkspaceMemoryCandidate(
{ type, text: normalizedBody.text },
{ fromMemoryTrigger: normalizedBody.hadTrigger },
)) continue;
{
fromMemoryTrigger: normalizedBody.hadTrigger,
workspaceKey: options.workspaceKey,
workspaceRootHash: options.workspaceRootHash,
},
);
if (!quality.accepted) {
evidence.push(extractionEvidence({
type: "extraction_candidate_rejected",
phase: "extraction",
outcome: "rejected",
reasonCodes: quality.reasons,
memory: { type, source: "compaction" },
textPreview: evidenceTextPreview(normalizedBody.text, 80),
}));
continue;
}
entries.push({
const memory: LongTermMemoryEntry = {
id: id("mem"),
type,
text: normalizedBody.text.slice(0, LONG_TERM_LIMITS.maxEntryTextChars),
@@ -353,8 +593,17 @@ export function parseWorkspaceMemoryCandidates(summary: string): LongTermMemoryE
updatedAt: now,
retentionClock: nowMs,
staleAfterDays: staleAfterDaysFor(type),
});
};
entries.push(memory);
evidence.push(extractionEvidence({
type: "extraction_candidate_accepted",
phase: "extraction",
outcome: "accepted",
reasonCodes: ["quality_gate_passed", "valid_candidate_format"],
memory: memoryEvidence(memory),
textPreview: evidenceTextPreview(memory.text),
}));
}
return entries;
return { entries, commands, evidence };
}
+41
View File
@@ -0,0 +1,41 @@
import { readFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
const __dirname = dirname(fileURLToPath(import.meta.url));
let cachedVersion: string | undefined;
const MEMORY_PRODUCER_NAME = "opencode-working-memory";
const MEMORY_INSTRUMENTATION_VERSION = 3;
function producerVersion(): string {
if (cachedVersion) return cachedVersion;
try {
const candidates = [
join(__dirname, "..", "package.json"),
join(__dirname, "..", "..", "package.json"),
// resolve from compiled dist/src/ -> repo root
];
for (const path of candidates) {
try {
const pkg = JSON.parse(readFileSync(path, "utf8"));
cachedVersion = pkg.version as string;
break;
} catch {
// try next
}
}
if (!cachedVersion) cachedVersion = "unknown";
} catch {
cachedVersion = "unknown";
}
return cachedVersion;
}
export function producerFields(): { producerName: string; producerVersion: string; instrumentationVersion: number } {
return {
producerName: MEMORY_PRODUCER_NAME,
producerVersion: producerVersion(),
instrumentationVersion: MEMORY_INSTRUMENTATION_VERSION,
};
}
+14
View File
@@ -0,0 +1,14 @@
import type { LongTermType } from "./types.ts";
// Current workspace-memory display/render order. This is intentionally a narrow
// shared constant, not a broader memory-kind policy registry.
export const MEMORY_TYPE_ORDER = ["feedback", "project", "decision", "reference"] as const satisfies readonly LongTermType[];
export function emptyMemoryTypeGroups<T>(): Record<LongTermType, T[]> {
return {
feedback: [],
project: [],
decision: [],
reference: [],
};
}
+96 -4
View File
@@ -7,6 +7,7 @@ export type MemoryQualityInput = Pick<LongTermMemoryEntry, "type" | "text"> & {
export type MemoryQualityResult = {
accepted: boolean;
reasons: string[];
diagnostics?: string[];
};
export const HARD_QUALITY_REASONS: ReadonlySet<string> = new Set([
@@ -18,6 +19,9 @@ export const HARD_QUALITY_REASONS: ReadonlySet<string> = new Set([
"active_file_snapshot",
"code_or_api_signature",
"path_heavy",
"unresolved_question",
"transient_bug_state",
"deployment_snapshot",
]);
export function isHardQualityReason(reason: string): boolean {
@@ -36,10 +40,18 @@ export function assessMemoryQuality(entry: MemoryQualityInput): MemoryQualityRes
if (isTemporaryStatusViolation(text)) reasons.push("temporary_status");
if (isActiveFileSnapshotViolation(text)) reasons.push("active_file_snapshot");
if (isCodeOrApiSignatureViolation(text)) reasons.push("code_or_api_signature");
if (isUnresolvedQuestionViolation(text)) reasons.push("unresolved_question");
if (isTransientBugStateViolation(text)) reasons.push("transient_bug_state");
if (isDeploymentSnapshotViolation(text)) reasons.push("deployment_snapshot");
if (entry.type === "feedback" && isFeedbackQualityViolation(text)) reasons.push("bad_feedback");
if (entry.type === "decision" && isDecisionQualityViolation(text)) reasons.push("bad_decision");
return { accepted: reasons.length === 0, reasons };
const diagnostics = isTerseLabelDiagnostic(text) ? ["terse_label"] : [];
return {
accepted: reasons.length === 0,
reasons,
...(diagnostics.length > 0 ? { diagnostics } : {}),
};
}
export function isProgressSnapshotViolation(text: string): boolean {
@@ -78,15 +90,67 @@ export function isFeedbackQualityViolation(text: string): boolean {
return true;
}
export function isDecisionQualityViolation(text: string): boolean {
const futureRule = /\b(?:use|keep|prefer|avoid|do not|don't|must|should|never|always|require|choose|reject)\b/i.test(text)
export function hasFutureRule(text: string): boolean {
return /\b(?:use|keep|prefer|avoid|do not|don't|must|should|never|always|require|choose|reject)\b/i.test(text)
|| /(?:使|||||||||)/u.test(text);
if (!futureRule) return true;
}
function textWithoutUrls(text: string): string {
return text.replace(/https?:\/\/[^\s`"'<>]+/gi, "");
}
function hasDurableRuleMarker(text: string): boolean {
return /\b(?:must|always|never|use|do\s+not|don't)\b/i.test(text)
|| /\bshould\b(?!\s+we\b)/i.test(text)
|| /(?:|||||使|)/u.test(text);
}
function isUnresolvedQuestionViolation(text: string): boolean {
if (hasDurableRuleMarker(text)) return false;
const withoutUrls = textWithoutUrls(text).trim();
const startsUnresolved = /^(?:question:|open question\b|unresolved\b|pending question\b|todo:\s*decide\b|TBD\b|TODO\b|||)/iu.test(withoutUrls);
if (startsUnresolved) return true;
if (/\b(?:need to decide|needs decision|not decided|whether to|should we|do we need)\b/i.test(withoutUrls)) return true;
if (/(?:|||)/u.test(withoutUrls)) return true;
if (/[?]\s*$/.test(withoutUrls)) return true;
const hasQuestion = /[?]/.test(withoutUrls);
const hasPlanningPhrase = /\b(?:we need|need to|next|later|follow up)\b/i.test(withoutUrls)
|| /(?:||)/u.test(withoutUrls);
return hasQuestion && hasPlanningPhrase;
}
export function isArchitectureLikeDecision(text: string): boolean {
if (/\b(?:[A-Z][A-Z0-9]*_[A-Z0-9_]*|[A-Z][A-Z0-9]{3,})\b/.test(text)) return true;
if (/\b(?:schema|model|scoring|retention|cap|evidence|normalization|root cause|architecture(?!\s+keywords?)|boundary|rule|memory system)\b/i.test(text)) return true;
if (/(?:|||||||||||||)/u.test(text)) return true;
return false;
}
export function isImplementationStatusDecision(text: string): boolean {
if (/\b(?:implemented|added|updated|fixed|completed|reviewed)\b/i.test(text)) return true;
if (/\b(?:was|were|has been|had been)\b/i.test(text) && /\b(?:previous|last|latest|this session|this wave|already)\b/i.test(text)) return true;
return false;
}
export function isDecisionQualityViolation(text: string): boolean {
if (hasFutureRule(text)) {
if (isImplementationStatusDecision(text)) return true;
return false;
}
if (isArchitectureLikeDecision(text)) {
if (isImplementationStatusDecision(text)) return true;
if (/\b(?:session|wave|task|test|CI|compatibility|commit)\b/i.test(text)) return true;
return false;
}
return true;
}
function isRawErrorViolation(text: string): boolean {
if (/^\s*(Error|TypeError|ReferenceError|SyntaxError|Exception):/i.test(text)) return true;
if (/at \S+ \([^)]+:\d+:\d+\)/.test(text)) return true;
@@ -113,6 +177,34 @@ function isTemporaryStatusViolation(text: string): boolean {
return false;
}
function isTransientBugStateViolation(text: string): boolean {
return /\b(?:currently debugging|still failing|unresolved bug|temporary workaround|next step is to fix|tests are failing)\b/i.test(text)
|| /(?:|||)/u.test(text);
}
function isDeploymentSnapshotViolation(text: string): boolean {
const hasDeploymentContext = /\b(?:deployed|current|latest|active|revision|build|release)\b/i.test(text)
|| /(?:|||)/u.test(text);
if (!hasDeploymentContext) return false;
const highEntropyId = /\b(?:rev|build|release|revision)[-_]?[A-Za-z0-9]{10,}\b/i.test(text)
|| /\b[A-Za-z0-9]*[A-Z][A-Za-z0-9]*\d[A-Za-z0-9]*[A-Za-z0-9_-]{8,}\b/.test(text)
|| /\b[A-Za-z0-9]*\d[A-Za-z0-9]*[A-Z][A-Za-z0-9]*[A-Za-z0-9_-]{8,}\b/.test(text);
return highEntropyId;
}
function isTerseLabelDiagnostic(text: string): boolean {
if (/[:]/u.test(text)) return false;
const codePoints = [...text].length;
const tokens = text.split(/\s+/u).filter(Boolean);
if (codePoints >= 18 && tokens.length >= 4) return false;
const hasMarker = /\b(?:is|are|was|were|has|have|uses?|keeps?|requires?|prefers?|wants?|supports?|must|should|always|never|do\s+not|don't|because|for|with|when|after|before)\b/i.test(text)
|| /(?:使|||||||||||||)/u.test(text);
return !hasMarker;
}
function isActiveFileSnapshotViolation(text: string): boolean {
return /^(modified|created|deleted|renamed)\s+\S+\.\S+$/i.test(text);
}
+301
View File
@@ -0,0 +1,301 @@
// No OpenCode SDK or TUI imports. Uses only local file-system reads from workspace memory, session state, and pending journal.
import { readFile } from "node:fs/promises";
import { sessionStatePath, workspaceKey, workspaceMemoryPath, workspacePendingJournalPath } from "./paths.ts";
import { redactCredentials } from "./redaction.ts";
import type { LongTermMemoryEntry, PendingMemoryJournalStore, SessionState, WorkspaceMemoryStore } from "./types.ts";
import { LONG_TERM_LIMITS } from "./types.ts";
import { accountWorkspaceMemoryCompactionRefs, accountWorkspaceMemoryRender } from "./workspace-memory.ts";
import { MEMORY_TYPE_ORDER, emptyMemoryTypeGroups } from "./memory-kind-policy.ts";
export type MemoryVisibilityCommand = "status" | "list" | "help";
type MemoryListItem = {
ref: string;
text: string;
};
export type MemoryStatusModel = {
activeMemories: number;
supersededMemories: number;
renderedInPrompt: number;
omittedActiveMemories: number;
pendingInSession: number;
pendingJournalMemories: number;
openErrors: number;
recentDecisions: number;
};
export type MemoryListModel = {
activeMemories: number;
renderedMemories: number;
omittedActiveMemories: number;
groups: Record<LongTermMemoryEntry["type"], MemoryListItem[]>;
};
const MAX_PREVIEW_CHARS = 120;
function safePreview(text: string | undefined, maxChars = MAX_PREVIEW_CHARS): string {
const clean = redactCredentials(text ?? "").replace(/\s+/g, " ").trim();
if (clean.length <= maxChars) return clean;
return `${clean.slice(0, Math.max(0, maxChars - 1)).trimEnd()}`;
}
async function readJSONSnapshot(path: string): Promise<unknown | undefined> {
try {
return JSON.parse(await readFile(path, "utf8"));
} catch {
return undefined;
}
}
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
function isLongTermType(value: unknown): value is LongTermMemoryEntry["type"] {
return value === "feedback" || value === "project" || value === "decision" || value === "reference";
}
function isLongTermSource(value: unknown): value is LongTermMemoryEntry["source"] {
return value === "explicit" || value === "compaction" || value === "manual";
}
function isLongTermMemoryEntry(value: unknown): value is LongTermMemoryEntry {
if (!isRecord(value)) return false;
if (typeof value.id !== "string") return false;
if (!isLongTermType(value.type)) return false;
if (typeof value.text !== "string") return false;
if (!isLongTermSource(value.source)) return false;
if (typeof value.confidence !== "number") return false;
if (value.status !== "active" && value.status !== "superseded") return false;
if (typeof value.createdAt !== "string") return false;
return typeof value.updatedAt === "string";
}
function memoryEntries(value: unknown): LongTermMemoryEntry[] {
return Array.isArray(value) ? value.filter(isLongTermMemoryEntry) : [];
}
async function emptyWorkspaceMemorySnapshot(root: string): Promise<WorkspaceMemoryStore> {
const nowIso = new Date().toISOString();
return {
version: 1,
workspace: { root, key: await workspaceKey(root) },
limits: {
maxRenderedChars: LONG_TERM_LIMITS.maxRenderedChars,
maxEntries: LONG_TERM_LIMITS.maxEntries,
},
entries: [],
migrations: [],
updatedAt: nowIso,
lastActivityAt: nowIso,
};
}
async function readWorkspaceMemorySnapshot(root: string): Promise<WorkspaceMemoryStore> {
const fallback = await emptyWorkspaceMemorySnapshot(root);
const loaded = await readJSONSnapshot(await workspaceMemoryPath(root));
if (!isRecord(loaded)) return fallback;
const limits = isRecord(loaded.limits) ? loaded.limits : {};
return {
version: 1,
workspace: fallback.workspace,
limits: {
maxRenderedChars: typeof limits.maxRenderedChars === "number" ? limits.maxRenderedChars : LONG_TERM_LIMITS.maxRenderedChars,
maxEntries: typeof limits.maxEntries === "number" ? limits.maxEntries : LONG_TERM_LIMITS.maxEntries,
},
entries: memoryEntries(loaded.entries),
migrations: Array.isArray(loaded.migrations) ? loaded.migrations.filter(item => typeof item === "string") : [],
updatedAt: typeof loaded.updatedAt === "string" ? loaded.updatedAt : fallback.updatedAt,
lastActivityAt: typeof loaded.lastActivityAt === "string" ? loaded.lastActivityAt : undefined,
};
}
async function emptyPendingJournalSnapshot(root: string): Promise<PendingMemoryJournalStore> {
return {
version: 1,
workspace: { root, key: await workspaceKey(root) },
entries: [],
updatedAt: new Date().toISOString(),
};
}
async function readPendingJournalSnapshot(root: string): Promise<PendingMemoryJournalStore> {
const fallback = await emptyPendingJournalSnapshot(root);
const loaded = await readJSONSnapshot(await workspacePendingJournalPath(root));
if (!isRecord(loaded)) return fallback;
return {
version: 1,
workspace: fallback.workspace,
entries: memoryEntries(loaded.entries),
updatedAt: typeof loaded.updatedAt === "string" ? loaded.updatedAt : fallback.updatedAt,
};
}
function emptySessionStateSnapshot(sessionID: string): SessionState {
return {
version: 1,
sessionID,
turn: 0,
updatedAt: new Date().toISOString(),
activeFiles: [],
openErrors: [],
recentDecisions: [],
pendingMemories: [],
compactionMemoryRefs: [],
};
}
async function readSessionStateSnapshot(root: string, sessionID: string): Promise<SessionState> {
const fallback = emptySessionStateSnapshot(sessionID);
const loaded = await readJSONSnapshot(await sessionStatePath(root, sessionID));
if (!isRecord(loaded)) return fallback;
return {
...fallback,
turn: typeof loaded.turn === "number" ? loaded.turn : fallback.turn,
updatedAt: typeof loaded.updatedAt === "string" ? loaded.updatedAt : fallback.updatedAt,
activeFiles: Array.isArray(loaded.activeFiles) ? loaded.activeFiles as SessionState["activeFiles"] : [],
openErrors: Array.isArray(loaded.openErrors) ? loaded.openErrors as SessionState["openErrors"] : [],
recentDecisions: Array.isArray(loaded.recentDecisions) ? loaded.recentDecisions as SessionState["recentDecisions"] : [],
pendingMemories: memoryEntries(loaded.pendingMemories),
compactionMemoryRefs: Array.isArray(loaded.compactionMemoryRefs) ? loaded.compactionMemoryRefs as SessionState["compactionMemoryRefs"] : [],
};
}
export async function getMemoryStatus(root: string, sessionID: string): Promise<MemoryStatusModel> {
const [store, sessionState, pendingJournal] = await Promise.all([
readWorkspaceMemorySnapshot(root),
readSessionStateSnapshot(root, sessionID),
readPendingJournalSnapshot(root),
]);
const renderAccounting = accountWorkspaceMemoryRender(store);
const activeEntries = store.entries.filter(entry => entry.status !== "superseded");
const supersededEntries = store.entries.filter(entry => entry.status === "superseded");
return {
activeMemories: activeEntries.length,
supersededMemories: supersededEntries.length,
renderedInPrompt: renderAccounting.rendered.length,
omittedActiveMemories: renderAccounting.omitted.filter(item => item.memory.status !== "superseded").length,
pendingInSession: sessionState.pendingMemories.length,
pendingJournalMemories: pendingJournal.entries.length,
openErrors: sessionState.openErrors.filter(error => error.status === "open").length,
recentDecisions: sessionState.recentDecisions.length,
};
}
export function formatMemoryStatus(model: MemoryStatusModel): string {
return [
"## Memory status",
"",
"Workspace:",
`- Active memories: ${model.activeMemories}`,
`- Rendered in prompt: ${model.renderedInPrompt}`,
`- Omitted active memories: ${model.omittedActiveMemories}`,
`- Superseded memories: ${model.supersededMemories}`,
"",
"Pending:",
`- Pending in this session: ${model.pendingInSession}`,
`- Pending journal memories: ${model.pendingJournalMemories}`,
"",
"Session:",
`- Open errors: ${model.openErrors}`,
`- Recent decisions: ${model.recentDecisions}`,
"",
`Use /memory → Current memories to browse current [M1]-[M${LONG_TERM_LIMITS.maxEntries}] memory refs.`,
"",
"Local only: no LLM request was made.",
].join("\n");
}
function emptyMemoryListGroups(): MemoryListModel["groups"] {
return emptyMemoryTypeGroups<MemoryListItem>();
}
export async function getMemoryList(root: string): Promise<MemoryListModel> {
const store = await readWorkspaceMemorySnapshot(root);
const accounting = accountWorkspaceMemoryCompactionRefs(store);
const groups = emptyMemoryListGroups();
const renderedMemoryIds = new Set(accounting.rendered.map(memory => memory.id));
for (const ref of accounting.refs) {
if (!renderedMemoryIds.has(ref.memoryId)) continue;
groups[ref.type].push({
ref: ref.ref,
text: safePreview(ref.textPreview),
});
}
const renderedMemories = MEMORY_TYPE_ORDER.reduce((total, type) => total + groups[type].length, 0);
return {
activeMemories: store.entries.filter(entry => entry.status !== "superseded").length,
renderedMemories,
omittedActiveMemories: accounting.omitted.filter(item => item.memory.status !== "superseded").length,
groups,
};
}
export function formatMemoryList(model: MemoryListModel): string {
const lines = [
"## Current workspace memories",
"",
];
if (model.renderedMemories === 0) {
lines.push("No active workspace memories are stored yet.", "", "Local only: no LLM request was made.");
return lines.join("\n");
}
lines.push("Display refs are local to this output and may change after memory updates.", "");
for (const type of MEMORY_TYPE_ORDER) {
const group = model.groups[type];
if (group.length === 0) continue;
lines.push(`${type}:`);
for (const item of group) {
lines.push(`- [${item.ref}] ${item.text}`);
}
lines.push("");
}
lines.push(
`Shown: ${model.renderedMemories} of ${model.activeMemories} active memories.`,
`Omitted active memories: ${model.omittedActiveMemories}.`,
"",
"Local only: no LLM request was made.",
);
return lines.join("\n");
}
export function formatMemoryHelp(): string {
return [
"## Memory help",
"",
"Command:",
"- /memory — open the local memory menu.",
"",
"Menu entries:",
"- Status — show local memory statistics.",
`- Current memories — browse active workspace memories as display-local [M1]-[M${LONG_TERM_LIMITS.maxEntries}] refs.`,
"- Help — show this help.",
"",
"These commands are read-only, local-only, and do not call the LLM.",
].join("\n");
}
export async function renderMemoryCommand(root: string, sessionID: string, command: MemoryVisibilityCommand): Promise<string> {
switch (command) {
case "status":
return formatMemoryStatus(await getMemoryStatus(root, sessionID));
case "list":
return formatMemoryList(await getMemoryList(root));
case "help":
return formatMemoryHelp();
default:
return formatMemoryHelp();
}
}
+4
View File
@@ -24,6 +24,10 @@ export async function workspacePendingJournalPath(root: string): Promise<string>
return join(await memoryRoot(root), "workspace-pending-journal.json");
}
export async function workspaceEvidenceLogPath(root: string): Promise<string> {
return join(await memoryRoot(root), "evidence", "events.jsonl");
}
export async function sessionStatePath(root: string, sessionID: string): Promise<string> {
const safeSessionID = createHash("sha256").update(sessionID).digest("hex").slice(0, 32);
return join(await memoryRoot(root), "sessions", `${safeSessionID}.json`);
+580 -150
View File
@@ -19,25 +19,31 @@
* - Processes explicit memory from latest user text once per message id
* - Injects frozen workspace memory and dynamic hot session state into system prompt
* - Updates session state after tool execution
* - Augments compaction context with memory, hot state, todos, and instruction
* - Augments compaction context with numbered memory refs, todos, and instruction
* - Parses compaction summaries for memory candidates and merges them
*/
import { rm } from "fs/promises";
import { createHash, randomUUID } from "node:crypto";
import { realpath, rm } from "fs/promises";
import type { Plugin } from "@opencode-ai/plugin";
import {
extractExplicitMemories,
extractExplicitMemoriesWithEvidence,
extractActiveFiles,
extractErrorsFromBash,
parseWorkspaceMemoryCandidates,
parseWorkspaceMemoryCandidatesWithEvidence,
staleAfterDaysFor,
type WorkspaceMemoryCommand,
} from "./extractors.ts";
import { assessMemoryQuality } from "./memory-quality.ts";
import {
loadWorkspaceMemory,
updateWorkspaceMemory,
updateWorkspaceMemoryWithAccounting,
renderWorkspaceMemory,
reinforceMemory,
accountWorkspaceMemoryRender,
accountWorkspaceMemoryCompactionRefs,
workspaceMemoryExactKey,
workspaceMemoryIdentityKey,
} from "./workspace-memory.ts";
import { REINFORCEMENT_MAX_COUNT, tryReinforceMemory, type ReinforcementDecision } from "./retention.ts";
import {
appendPendingMemories,
clearPendingMemories,
@@ -56,14 +62,15 @@ import {
addRecentDecision,
renderHotSessionState,
} from "./session-state.ts";
import { sessionStatePath } from "./paths.ts";
import { sessionStatePath, workspaceKey } from "./paths.ts";
import {
latestUserText,
latestCompactionSummary,
pendingTodos,
} from "./opencode.ts";
import { accountPendingPromotions } from "./promotion-accounting.ts";
import { WORKSPACE_MEMORY_CACHE_LIMITS } from "./types.ts";
import { accountPendingPromotions, promotionAccountingEvidenceEvents } from "./promotion-accounting.ts";
import { appendEvidenceEvent, appendEvidenceEvents, type EvidenceEventInput, type MemoryEvidenceRef } from "./evidence-log.ts";
import { type CompactionMemoryRef, type LongTermMemoryEntry, LONG_TERM_LIMITS, WORKSPACE_MEMORY_CACHE_LIMITS } from "./types.ts";
/**
* Build the complete compaction prompt.
@@ -73,11 +80,14 @@ import { WORKSPACE_MEMORY_CACHE_LIMITS } from "./types.ts";
* Our template uses only ## Markdown headings and explicitly forbids YAML frontmatter,
* horizontal rules, and delimiter lines.
*
* @param privateContext - Background context (workspace memory, hot session state,
* @param privateContext - Background context (numbered workspace memory refs,
* pending todos) from our plugin and any other plugins. Shown to the model to
* inform the summary but not copied verbatim.
*/
function buildCompactionPrompt(privateContext: string): string {
function buildCompactionPrompt(privateContext: string, compactionId?: string): string {
const snapshotInstruction = compactionId
? `- If you emit any REINFORCE or REPLACE command, include \`Memory ref snapshot id: ${compactionId}\` as the first line under \"Memory candidates:\" so numbered refs match the correct compaction snapshot.`
: "";
return [
"Provide a detailed summary for continuing our conversation above.",
"Focus on information that would help another agent continue the work: the goal, user instructions, completed work, current state, decisions, relevant files, and next steps.",
@@ -92,6 +102,7 @@ function buildCompactionPrompt(privateContext: string): string {
"- Do not output horizontal rules.",
"- Do not wrap the summary in delimiter lines such as ---.",
"- Do not use code fences around the summary.",
...(snapshotInstruction ? [snapshotInstruction] : []),
"",
"Use this structure:",
"",
@@ -113,16 +124,19 @@ function buildCompactionPrompt(privateContext: string): string {
"",
"CRITICAL MEMORY RULES:",
"- Most compactions should produce ZERO memories. Empty is correct when nothing durable changed.",
"- Existing memories are numbered [M#]. If an existing memory is still accurate, emit at most 3 lines like `REINFORCE [M#]`; do not rephrase it.",
"- Use `REPLACE [M#] [type] text` only for eligible unreinforced compaction-sourced memories where the old text itself needs correction; this is rarely the right choice.",
"- To supplement or correct a memory, REINFORCE the existing [M#] if it is still accurate, and also emit a new complete [type] candidate with the addition or correction. Do not use REPLACE for additions; do not reinforce a memory that is now inaccurate.",
"- NO completion or progress statements: do not extract completed work, passing tests, commits, PR status, wave/task/phase completion, or current state.",
"- NO session-internal implementation notes: do not extract what files were edited, what bug was just fixed, what command just ran, or what the assistant reviewed.",
"- feedback ONLY means stable user preferences or user instructions, written in imperative/future-facing form.",
"- decision ONLY means rules that apply to FUTURE work, not decisions already implemented in this session.",
"- project/reference ONLY when the fact is stable across sessions and hard to rediscover from the repository.",
"- decision = future rule/architecture choice; reference = stable lookup fact; project = stable project fact; feedback = stable user preference.",
"- Do not use decision for service names, IDs, URLs, file paths, or one-off session status; use reference/project or skip.",
"- If unsure, skip it.",
"",
"Good memory examples:",
"- REINFORCE [M1]",
"- [feedback] User prefers architecture reviews in Traditional Chinese.",
"- [decision] Do not add semantic merge to memory dedupe.",
"- [decision] Keep memory dedupe exact-only for decisions.",
"- [project] This repository is an OpenCode plugin using local JSON stores.",
"- [reference] Workspace memory is rendered as frozen system[1]; pending memories remain in hot state until compaction.",
"",
@@ -133,11 +147,13 @@ function buildCompactionPrompt(privateContext: string): string {
"- The assistant reviewed code reviewer feedback and updated the plan.",
"- Commit a762e86 contains the owner scope fix.",
"",
"Format when there ARE durable memories:",
"Format when there ARE REINFORCE/REPLACE commands or durable new candidates:",
"Memory candidates:",
"- [feedback|decision|project|reference] future-facing durable fact",
"REINFORCE [M#]",
"REPLACE [M#] [feedback|decision|project|reference] corrected durable fact",
"- [feedback|decision|project|reference] new future-facing durable fact",
"",
"Format when there are NO durable memories:",
"Format when there are NO REINFORCE/REPLACE commands or durable candidates:",
"Memory candidates:",
"(none)",
"",
@@ -162,6 +178,56 @@ function renderTodosForCompaction(todos: Array<{ content: string; status: string
return lines.join("\n");
}
function safeErrorMessage(error: unknown): string {
const message = error instanceof Error ? error.message : String(error);
return message.replace(/\s+/g, " ").slice(0, 240);
}
type CompactionRefResolution =
| {
ok: true;
refSnapshot: CompactionMemoryRef;
target: LongTermMemoryEntry;
targetIndex: number;
}
| {
ok: false;
reason: "missing_memory_ref_snapshot" | "invalid_memory_ref" | "memory_ref_target_unavailable" | "memory_ref_target_changed";
refSnapshot?: CompactionMemoryRef;
target?: LongTermMemoryEntry;
};
async function warnMemoryHook(scope: string, error: unknown, root?: string): Promise<void> {
const message = safeErrorMessage(error);
console.error(`[memory] ${scope} failed: ${message}`);
if (root) {
await appendEvidenceEvent(root, {
type: "hook_failed",
phase: "hook",
outcome: "failed",
reasonCodes: [scope],
details: { message },
}).catch(() => undefined);
}
}
async function workspaceRootHash(root: string): Promise<string> {
const resolved = await realpath(root).catch(() => root);
return createHash("sha256").update(resolved).digest("hex").slice(0, 16);
}
async function workspaceIdentity(root: string): Promise<{ workspaceKey: string; workspaceRootHash: string }> {
const [workspaceKeyValue, workspaceRootHashValue] = await Promise.all([
workspaceKey(root),
workspaceRootHash(root),
]);
return { workspaceKey: workspaceKeyValue, workspaceRootHash: workspaceRootHashValue };
}
function compactionIdFromSummary(summary: string): string | undefined {
return summary.match(/Memory ref snapshot id:\s*([a-zA-Z0-9_-]+)/i)?.[1];
}
export const MemoryV2Plugin: Plugin = async (input) => {
const { directory, client } = input;
@@ -198,6 +264,281 @@ export const MemoryV2Plugin: Plugin = async (input) => {
// Cache for processed user message IDs (to avoid duplicate processing)
const processedUserMessages = new Map<string, Set<string>>();
function memoryEvidenceRef(memory: LongTermMemoryEntry): MemoryEvidenceRef {
return {
memoryId: memory.id,
memoryKeyHash: memoryKey(memory),
identityKeyHash: workspaceMemoryIdentityKey(memory),
type: memory.type,
source: memory.source,
status: memory.status,
};
}
function pendingAppendedEvidence(memory: LongTermMemoryEntry): EvidenceEventInput {
return {
type: "pending_memory_appended",
phase: "pending_journal",
outcome: "accepted",
memory: memoryEvidenceRef(memory),
relations: [{ role: "pending", memory: memoryEvidenceRef(memory) }],
reasonCodes: ["pending_journal_append"],
textPreview: memory.text,
};
}
function memoryReinforcedEvidence(
memory: LongTermMemoryEntry | undefined,
ref: string,
outcome: "reinforced" | "rejected",
reasonCodes: string[],
details: EvidenceEventInput["details"] = {},
): EvidenceEventInput {
const relationRole = outcome === "rejected" ? "target" : "reinforced";
return {
type: "memory_reinforced",
phase: "reinforcement",
outcome,
memory: memory ? memoryEvidenceRef(memory) : undefined,
relations: memory ? [{ role: relationRole, memory: memoryEvidenceRef(memory) }] : undefined,
reasonCodes,
details: {
ref,
...details,
},
textPreview: memory?.text,
};
}
function reinforcementDecisionTimingDetails(decision: ReinforcementDecision): EvidenceEventInput["details"] {
return {
attemptedAtMs: decision.attemptedAt,
attemptedAtIso: new Date(decision.attemptedAt).toISOString(),
...(decision.lastReinforcedAt !== undefined ? {
lastReinforcedAtMs: decision.lastReinforcedAt,
lastReinforcedAtIso: new Date(decision.lastReinforcedAt).toISOString(),
} : {}),
...(decision.elapsedMs !== undefined ? { elapsedMs: decision.elapsedMs } : {}),
requiredElapsedMs: decision.requiredElapsedMs,
sameSession: decision.sameSession,
...(decision.legacyMissingTimestamp ? { legacyMissingTimestamp: true } : {}),
};
}
function replacementMemoryId(): string {
return `mem_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
}
function memoryReplacedEvidence(
oldMemory: LongTermMemoryEntry | undefined,
newMemory: LongTermMemoryEntry | undefined,
ref: string,
outcome: "superseded" | "rejected",
reasonCodes: string[],
details: EvidenceEventInput["details"] = {},
): EvidenceEventInput {
const relations = outcome === "rejected"
? [
...(oldMemory ? [{ role: "target" as const, memory: memoryEvidenceRef(oldMemory) }] : []),
]
: [
...(oldMemory ? [{ role: "superseded" as const, memory: memoryEvidenceRef(oldMemory) }] : []),
...(newMemory ? [{ role: "superseded_by" as const, memory: memoryEvidenceRef(newMemory) }] : []),
];
return {
type: "memory_replaced_numbered_ref",
phase: "storage",
outcome,
memory: oldMemory ? memoryEvidenceRef(oldMemory) : undefined,
relations: relations.length > 0 ? relations : undefined,
reasonCodes,
details: {
ref,
...details,
},
textPreview: newMemory?.text ?? oldMemory?.text,
};
}
function compactionRefByLabel(refs: CompactionMemoryRef[]): Map<string, CompactionMemoryRef> {
return new Map(refs.map(ref => [ref.ref.toUpperCase(), ref]));
}
function compactionSnapshotStatus(
refs: CompactionMemoryRef[],
expectedCompactionId: string | undefined,
): { ok: true } | { ok: false; storedCompactionId: string } {
if (refs.length === 0) return { ok: false, storedCompactionId: "none" };
const ids = new Set(refs.map(ref => ref.compactionId).filter((id): id is string => typeof id === "string" && id.length > 0));
if (!expectedCompactionId) return { ok: true };
if (ids.size === 1 && ids.has(expectedCompactionId)) return { ok: true };
if (ids.size === 0) return { ok: false, storedCompactionId: "none" };
if (ids.size === 1) return { ok: false, storedCompactionId: [...ids][0] };
return { ok: false, storedCompactionId: "mixed" };
}
function resolveCompactionMemoryRef(
refs: CompactionMemoryRef[],
refsByLabel: Map<string, CompactionMemoryRef>,
entries: LongTermMemoryEntry[],
ref: string,
): CompactionRefResolution {
if (refs.length === 0) {
return { ok: false, reason: "missing_memory_ref_snapshot" };
}
const refSnapshot = refsByLabel.get(ref.toUpperCase());
if (!refSnapshot) {
return { ok: false, reason: "invalid_memory_ref" };
}
const targetIndex = entries.findIndex(entry => entry.id === refSnapshot.memoryId);
const target = targetIndex >= 0 ? entries[targetIndex] : undefined;
if (!target || target.status !== "active") {
return { ok: false, reason: "memory_ref_target_unavailable", refSnapshot, target };
}
if (workspaceMemoryExactKey(target) !== refSnapshot.exactKey) {
return { ok: false, reason: "memory_ref_target_changed", refSnapshot, target };
}
return { ok: true, refSnapshot, target, targetIndex };
}
async function applyCompactionMemoryCommands(
sessionID: string,
commands: WorkspaceMemoryCommand[],
compactionId: string | undefined,
): Promise<void> {
if (commands.length === 0) return;
const sessionState = await loadSessionState(directory, sessionID);
const snapshotStatus = compactionSnapshotStatus(sessionState.compactionMemoryRefs, compactionId);
const refs = snapshotStatus.ok ? sessionState.compactionMemoryRefs : [];
const refsByLabel = compactionRefByLabel(refs);
const evidence: EvidenceEventInput[] = [];
const now = Date.now();
let snapshotMismatchDetails: EvidenceEventInput["details"] = {};
if ("storedCompactionId" in snapshotStatus) {
snapshotMismatchDetails = {
...(compactionId ? { compactionId } : {}),
storedCompactionId: snapshotStatus.storedCompactionId,
};
}
const updateResult = await updateWorkspaceMemoryWithAccounting(directory, workspaceMemory => {
for (const command of commands) {
const resolution = resolveCompactionMemoryRef(refs, refsByLabel, workspaceMemory.entries, command.ref);
if (resolution.ok === false) {
const memoryId = resolution.refSnapshot?.memoryId;
const details = memoryId ? { ...snapshotMismatchDetails, memoryId } : snapshotMismatchDetails;
if (command.kind === "REINFORCE") {
evidence.push(memoryReinforcedEvidence(resolution.target, command.ref, "rejected", [resolution.reason], details));
} else {
evidence.push(memoryReplacedEvidence(resolution.target, undefined, command.ref, "rejected", [resolution.reason], details));
}
continue;
}
const { refSnapshot, target, targetIndex } = resolution;
if (command.kind === "REINFORCE") {
const decision = tryReinforceMemory(target, sessionID, now);
if (decision.outcome === "blocked") {
evidence.push(memoryReinforcedEvidence(target, command.ref, "rejected", ["numbered_ref_reinforce", "reinforcement_window_blocked", `reinforcement_block_${decision.blockReason}`], {
memoryId: refSnapshot.memoryId,
blockReason: decision.blockReason,
...reinforcementDecisionTimingDetails(decision),
reinforcementCount: decision.reinforcementCount,
maxReinforcementCount: decision.maxReinforcementCount,
}));
continue;
}
const reinforced = decision.memory;
workspaceMemory.entries[targetIndex] = reinforced;
const reasonCodes = ["numbered_ref_reinforce", "reinforcement_window_allowed"];
if (decision.reinforcementMode === "refresh_only") {
reasonCodes.push("reinforcement_saturation_refresh");
}
evidence.push(memoryReinforcedEvidence(reinforced, command.ref, "reinforced", reasonCodes, {
memoryId: refSnapshot.memoryId,
reinforcementOutcome: decision.reinforcementMode === "refresh_only" ? "refreshed" : "reinforced",
reinforcementMode: decision.reinforcementMode,
...reinforcementDecisionTimingDetails(decision),
previousReinforcementCount: decision.previousReinforcementCount,
newReinforcementCount: decision.newReinforcementCount,
reinforcementCount: decision.newReinforcementCount,
maxReinforcementCount: REINFORCEMENT_MAX_COUNT,
}));
continue;
}
if (target.source !== "compaction") {
evidence.push(memoryReplacedEvidence(target, undefined, command.ref, "rejected", ["protected_memory_source"], {
memoryId: refSnapshot.memoryId,
source: target.source,
}));
continue;
}
if ((target.reinforcementCount ?? 0) > 0) {
evidence.push(memoryReplacedEvidence(target, undefined, command.ref, "rejected", ["protected_reinforced_target"], {
memoryId: refSnapshot.memoryId,
reinforcementCount: target.reinforcementCount ?? 0,
}));
continue;
}
const quality = assessMemoryQuality({ type: command.type, text: command.text, source: "compaction" });
if (!quality.accepted) {
evidence.push(memoryReplacedEvidence(target, undefined, command.ref, "rejected", quality.reasons, {
memoryId: refSnapshot.memoryId,
}));
continue;
}
const supersededTarget: LongTermMemoryEntry = {
...target,
status: "superseded",
updatedAt: new Date(now).toISOString(),
};
const replacement: LongTermMemoryEntry = {
id: replacementMemoryId(),
type: command.type,
text: command.text.slice(0, LONG_TERM_LIMITS.maxEntryTextChars),
source: "compaction",
confidence: 0.75,
status: "active",
createdAt: new Date(now).toISOString(),
updatedAt: new Date(now).toISOString(),
retentionClock: now,
staleAfterDays: staleAfterDaysFor(command.type),
supersedes: [target.id],
};
workspaceMemory.entries[targetIndex] = supersededTarget;
workspaceMemory.entries.push(replacement);
evidence.push(memoryReplacedEvidence(supersededTarget, replacement, command.ref, "superseded", [
"numbered_ref_replace",
command.type === target.type ? "same_type_replace" : "cross_type_replace",
], {
oldMemoryId: target.id,
newMemoryId: replacement.id,
oldType: target.type,
newType: command.type,
}));
}
return workspaceMemory;
});
await appendEvidenceEvents(directory, [...updateResult.evidence, ...evidence].map(event => ({
...event,
sessionHash: sessionID,
})));
}
function pruneFrozenWorkspaceMemoryCache(now = Date.now()): void {
for (const [sessionID, cached] of frozenWorkspaceMemoryCache) {
if (now - cached.loadedAt > WORKSPACE_MEMORY_CACHE_LIMITS.frozenTtlMs) {
@@ -254,7 +595,14 @@ export const MemoryV2Plugin: Plugin = async (input) => {
if (!latestMessage?.id || processedForSession.has(latestMessage.id)) return;
const memories = extractExplicitMemories(latestMessage.text).map(memory => ({
const extraction = extractExplicitMemoriesWithEvidence(latestMessage.text);
await appendEvidenceEvents(directory, extraction.evidence.map(event => ({
...event,
sessionHash: sessionID,
messageHash: latestMessage.id,
})));
const memories = extraction.entries.map(memory => ({
...memory,
pendingOwnerSessionID: sessionID,
pendingMessageID: latestMessage.id,
@@ -267,6 +615,11 @@ export const MemoryV2Plugin: Plugin = async (input) => {
return state;
});
await appendPendingMemories(directory, memories);
await appendEvidenceEvents(directory, memories.map(memory => ({
...pendingAppendedEvidence(memory),
sessionHash: sessionID,
messageHash: latestMessage.id,
})));
}
if (decisions.length > 0) {
@@ -285,6 +638,22 @@ export const MemoryV2Plugin: Plugin = async (input) => {
rememberProcessedUserMessage(sessionID, latestMessage.id, processedForSession);
}
function dedupePendingPromotionMemories(
memories: LongTermMemoryEntry[],
): LongTermMemoryEntry[] {
const seen = new Set<string>();
const deduped: LongTermMemoryEntry[] = [];
for (const memory of memories) {
const key = memoryKey(memory);
if (seen.has(key)) continue;
seen.add(key);
deduped.push(memory);
}
return deduped;
}
async function promotePendingMemories(
sessionID?: string,
options: { includeUnownedJournal?: boolean; includeOwnedJournal?: boolean } = {},
@@ -302,10 +671,10 @@ export const MemoryV2Plugin: Plugin = async (input) => {
return false;
});
const pending = [
const pending = dedupePendingPromotionMemories([
...(sessionState?.pendingMemories ?? []),
...journalPending,
];
]);
if (pending.length === 0) return;
let beforeEntries: Awaited<ReturnType<typeof loadWorkspaceMemory>>["entries"] = [];
@@ -323,11 +692,12 @@ export const MemoryV2Plugin: Plugin = async (input) => {
const key = memoryKey(memory);
const existing = existingByKey.get(key);
if (existing) {
const reinforced = reinforceMemory(
const decision = tryReinforceMemory(
existing.memory,
sessionID ?? memory.pendingOwnerSessionID ?? "workspace-promotion",
promotedAt,
);
const reinforced = decision.memory;
if (reinforced !== existing.memory) {
workspaceMemory.entries[existing.index] = reinforced;
existingByKey.set(key, { memory: reinforced, index: existing.index });
@@ -364,6 +734,20 @@ export const MemoryV2Plugin: Plugin = async (input) => {
},
);
await appendEvidenceEvents(directory, [
...updateResult.evidence,
...promotionAccountingEvidenceEvents({
pending,
after: updateResult.store.entries,
events: updateResult.events,
accounting,
exhaustedRejectedKeys,
}),
].map(event => ({
...event,
sessionHash: sessionID,
})));
const sessionRemovalKeys = new Set([
...accounting.clearableKeys,
...exhaustedRejectedKeys,
@@ -438,7 +822,12 @@ export const MemoryV2Plugin: Plugin = async (input) => {
}
const store = await loadWorkspaceMemory(root);
const renderedPrompt = renderWorkspaceMemory(store);
const renderAccounting = accountWorkspaceMemoryRender(store);
const renderedPrompt = renderAccounting.prompt;
await appendEvidenceEvents(root, renderAccounting.evidence.map(event => ({
...event,
sessionHash: sessionID,
})));
frozenWorkspaceMemoryCache.set(sessionID, { store, renderedPrompt, loadedAt: now });
pruneFrozenWorkspaceMemoryCache(now);
return { store, renderedPrompt };
@@ -456,44 +845,55 @@ export const MemoryV2Plugin: Plugin = async (input) => {
return props?.sessionID ?? props?.info?.id;
}
async function clearCompactionMemoryRefs(sessionID: string): Promise<void> {
await updateSessionState(directory, sessionID, state => {
state.compactionMemoryRefs = [];
return state;
});
}
return {
// Inject workspace memory and hot session state into system prompt
"experimental.chat.system.transform": async (hookInput, output) => {
const { sessionID } = hookInput;
if (!sessionID) return;
pruneFrozenWorkspaceMemoryCache();
pruneProcessedUserMessagesCache();
try {
pruneFrozenWorkspaceMemoryCache();
pruneProcessedUserMessagesCache();
// Sub-agents are short-lived - skip memory system
if (await isSubAgent(sessionID)) return;
// Sub-agents are short-lived - skip memory system
if (await isSubAgent(sessionID)) return;
// Process explicit user memory even on no-tool turns. Keep this after the
// sub-agent guard so child sessions never append to the parent journal.
await processLatestUserMessage(sessionID);
// Process explicit user memory even on no-tool turns. Keep this after the
// sub-agent guard so child sessions never append to the parent journal.
await processLatestUserMessage(sessionID);
// Before first snapshot in this session, promote durable unowned backlog from
// prior sessions. Current-turn owned explicit memory remains pending and only
// appears in hot state for this transform.
if (!frozenWorkspaceMemoryCache.has(sessionID) && await hasPendingJournalEntries(directory)) {
await promotePendingMemories(undefined, { includeUnownedJournal: true, includeOwnedJournal: false });
}
// Before first snapshot in this session, promote durable unowned backlog from
// prior sessions. Current-turn owned explicit memory remains pending and only
// appears in hot state for this transform.
if (!frozenWorkspaceMemoryCache.has(sessionID) && await hasPendingJournalEntries(directory)) {
await promotePendingMemories(undefined, { includeUnownedJournal: true, includeOwnedJournal: false });
}
// Get frozen workspace memory snapshot (loaded and rendered once per session)
const workspaceSnapshot = await getFrozenWorkspaceMemorySnapshot(directory, sessionID);
// Get frozen workspace memory snapshot (loaded and rendered once per session)
const workspaceSnapshot = await getFrozenWorkspaceMemorySnapshot(directory, sessionID);
// Get current hot session state
const sessionState = await loadSessionState(directory, sessionID);
// Get current hot session state
const sessionState = await loadSessionState(directory, sessionID);
// Inject frozen workspace memory snapshot
if (workspaceSnapshot.renderedPrompt) {
output.system.push(workspaceSnapshot.renderedPrompt);
}
// Inject frozen workspace memory snapshot
if (workspaceSnapshot.renderedPrompt) {
output.system.push(workspaceSnapshot.renderedPrompt);
}
// Render and inject hot session state
const hotPrompt = renderHotSessionState(sessionState, directory);
if (hotPrompt) {
output.system.push(hotPrompt);
// Render and inject hot session state
const hotPrompt = renderHotSessionState(sessionState, directory);
if (hotPrompt) {
output.system.push(hotPrompt);
}
} catch (error) {
await warnMemoryHook("chat.system.transform", error, directory);
}
},
@@ -506,50 +906,54 @@ export const MemoryV2Plugin: Plugin = async (input) => {
// Sub-agents don't need memory tracking
if (await isSubAgent(sessionID)) return;
await updateSessionState(directory, sessionID, state => {
// Track active files from tool usage
if (toolName === "read" || toolName === "edit" || toolName === "write" || toolName === "grep") {
const files = extractActiveFiles(
toolName,
args as Record<string, unknown>,
toolOutput ?? ""
);
for (const { path, action } of files) {
touchActiveFile(state, path, action);
if (action === "edit" || action === "write") {
markErrorsMaybeFixedForFile(state, path, directory);
try {
await updateSessionState(directory, sessionID, state => {
// Track active files from tool usage
if (toolName === "read" || toolName === "edit" || toolName === "write" || toolName === "grep") {
const files = extractActiveFiles(
toolName,
args as Record<string, unknown>,
toolOutput ?? ""
);
for (const { path, action } of files) {
touchActiveFile(state, path, action);
if (action === "edit" || action === "write") {
markErrorsMaybeFixedForFile(state, path, directory);
}
}
}
}
// Track errors from failed bash commands
if (toolName === "bash") {
const argsRecord = args as Record<string, unknown>;
const command: string = typeof argsRecord?.command === "string"
? argsRecord.command
: "";
const outputText: string = toolOutput ?? "";
// Track errors from failed bash commands
if (toolName === "bash") {
const argsRecord = args as Record<string, unknown>;
const command: string = typeof argsRecord?.command === "string"
? argsRecord.command
: "";
const outputText: string = toolOutput ?? "";
// Check if command succeeded - clear errors for that category
const exitCode = bashExitCode(hookOutput);
if (typeof exitCode !== "number") {
// Unknown exit status: do not extract and do not clear
} else if (exitCode === 0 && command) {
clearErrorsForSuccessfulCommand(state, command);
} else if (command) {
// Only extract errors for commands with explicit non-zero exit
const errors = extractErrorsFromBash(command, outputText);
for (const error of errors) {
upsertOpenError(state, error);
// Check if command succeeded - clear errors for that category
const exitCode = bashExitCode(hookOutput);
if (typeof exitCode !== "number") {
// Unknown exit status: do not extract and do not clear
} else if (exitCode === 0 && command) {
clearErrorsForSuccessfulCommand(state, command);
} else if (command) {
// Only extract errors for commands with explicit non-zero exit
const errors = extractErrorsFromBash(command, outputText);
for (const error of errors) {
upsertOpenError(state, error);
}
}
}
}
return state;
});
return state;
});
// Process explicit memory from latest user message
// Only process once per message ID
await processLatestUserMessage(sessionID);
// Process explicit memory from latest user message
// Only process once per message ID
await processLatestUserMessage(sessionID);
} catch (error) {
await warnMemoryHook("tool.execute.after", error, directory);
}
},
/**
@@ -567,95 +971,121 @@ export const MemoryV2Plugin: Plugin = async (input) => {
const { sessionID } = hookInput;
if (!sessionID) return;
// Sub-agents don't need compaction support
if (await isSubAgent(sessionID)) return;
try {
// Sub-agents don't need compaction support
if (await isSubAgent(sessionID)) return;
// Preserve context injected by other plugins that ran before us.
// Setting output.prompt bypasses the default prompt + context join,
// so we must explicitly carry forward any existing output.context.
const otherContext = output.context.filter(Boolean).join("\n\n");
// Preserve context injected by other plugins that ran before us.
// Setting output.prompt bypasses the default prompt + context join,
// so we must explicitly carry forward any existing output.context.
const otherContext = output.context.filter(Boolean).join("\n\n");
// Build our private context (workspace memory, hot state, todos)
const contextParts: string[] = [];
// Build our private context (numbered workspace memory refs, todos)
const contextParts: string[] = [];
// 1. Frozen workspace memory snapshot
const workspaceSnapshot = await getFrozenWorkspaceMemorySnapshot(directory, sessionID);
if (workspaceSnapshot.renderedPrompt) {
contextParts.push(workspaceSnapshot.renderedPrompt);
// 1. Compaction-only numbered workspace memory snapshot
const compactionId = randomUUID();
const workspaceStore = await loadWorkspaceMemory(directory);
const compactionRefs = accountWorkspaceMemoryCompactionRefs(workspaceStore);
const refsWithCompactionId = compactionRefs.refs.map(ref => ({ ...ref, compactionId }));
await updateSessionState(directory, sessionID, state => {
state.compactionMemoryRefs = refsWithCompactionId;
return state;
});
await appendEvidenceEvents(directory, compactionRefs.evidence.map(event => ({
...event,
sessionHash: sessionID,
})));
if (compactionRefs.prompt) {
contextParts.push(compactionRefs.prompt);
}
// 2. Pending todos from OpenCode
const todos = await pendingTodos(client, sessionID);
const todosPrompt = renderTodosForCompaction(todos);
if (todosPrompt) {
contextParts.push(todosPrompt);
}
// Combine: other plugins' context first, then our private context
const privateContext = [otherContext, ...contextParts]
.filter(Boolean)
.join("\n\n");
// Replace the default prompt entirely with our ---free template
output.prompt = buildCompactionPrompt(privateContext, compactionId);
// Clear context array since we consumed it into output.prompt.
// Subsequent plugins that set output.prompt will also need to check
// output.context if they want to preserve other plugin contributions.
output.context.length = 0;
} catch (error) {
await warnMemoryHook("session.compacting", error, directory);
}
// 2. Hot session state
const sessionState = await loadSessionState(directory, sessionID);
const hotPrompt = renderHotSessionState(sessionState, directory);
if (hotPrompt) {
contextParts.push(hotPrompt);
}
// 3. Pending todos from OpenCode
const todos = await pendingTodos(client, sessionID);
const todosPrompt = renderTodosForCompaction(todos);
if (todosPrompt) {
contextParts.push(todosPrompt);
}
// Combine: other plugins' context first, then our private context
const privateContext = [otherContext, ...contextParts]
.filter(Boolean)
.join("\n\n");
// Replace the default prompt entirely with our ---free template
output.prompt = buildCompactionPrompt(privateContext);
// Clear context array since we consumed it into output.prompt.
// Subsequent plugins that set output.prompt will also need to check
// output.context if they want to preserve other plugin contributions.
output.context.length = 0;
},
// Handle session events
event: async ({ event }) => {
if (event.type === "session.compacted") {
const sessionID = sessionIDFromEventProperties(event.properties);
if (!sessionID) return;
// Sub-agents don't need post-compaction processing
if (await isSubAgent(sessionID)) return;
// Parse latest compaction summary for memory candidates, stage them into
// durable pending journal, then promote pending memories.
const summary = await latestCompactionSummary(client, sessionID);
const candidates = summary ? parseWorkspaceMemoryCandidates(summary) : [];
if (candidates.length > 0) {
await appendPendingMemories(directory, candidates);
}
let sessionID: string | undefined;
try {
await promotePendingMemories(sessionID, { includeUnownedJournal: true });
} catch {
sessionID = sessionIDFromEventProperties(event.properties);
if (!sessionID) return;
// Sub-agents don't need post-compaction processing
if (await isSubAgent(sessionID)) return;
try {
// Parse latest compaction summary for memory candidates, stage them into
// durable pending journal, then promote pending memories.
const summary = await latestCompactionSummary(client, sessionID);
const compactionId = summary ? compactionIdFromSummary(summary) : undefined;
const parseResult = summary
? parseWorkspaceMemoryCandidatesWithEvidence(summary, await workspaceIdentity(directory))
: { entries: [], commands: [], evidence: [] };
await appendEvidenceEvents(directory, parseResult.evidence.map(event => ({
...event,
sessionHash: sessionID,
})));
await applyCompactionMemoryCommands(sessionID, parseResult.commands, compactionId);
const candidates = parseResult.entries;
if (candidates.length > 0) {
await appendPendingMemories(directory, candidates);
await appendEvidenceEvents(directory, candidates.map(memory => ({
...pendingAppendedEvidence(memory),
sessionHash: sessionID,
})));
}
await promotePendingMemories(sessionID, { includeUnownedJournal: true });
} finally {
await clearCompactionMemoryRefs(sessionID);
}
} catch (error) {
// Keep pending memories in session/journal for retry on next event/session.
await warnMemoryHook("event.session.compacted", error, directory);
}
}
if (event.type === "session.deleted") {
const sessionID = sessionIDFromEventProperties(event.properties);
if (sessionID) {
try {
const sessionID = sessionIDFromEventProperties(event.properties);
if (sessionID) {
// Promote pending memories before deleting per-session state.
// If promotion fails, leave session state and journal intact.
let promoted = false;
try {
let promoted = false;
await promotePendingMemories(sessionID, { includeOwnedJournal: true, includeUnownedJournal: false });
promoted = true;
} catch {
return;
} finally {
if (promoted) {
frozenWorkspaceMemoryCache.delete(sessionID);
processedUserMessages.delete(sessionID);
sessionParentCache.delete(sessionID);
}
}
await rm(await sessionStatePath(directory, sessionID), { force: true });
await rm(await sessionStatePath(directory, sessionID), { force: true });
}
} catch (error) {
await warnMemoryHook("event.session.deleted", error, directory);
}
}
},
+116
View File
@@ -2,6 +2,7 @@ import type { LongTermMemoryEntry } from "./types.ts";
import { memoryKey } from "./pending-journal.ts";
import type { MemoryConsolidationEvent } from "./workspace-memory.ts";
import { workspaceMemoryIdentityKey } from "./workspace-memory.ts";
import type { EvidenceEventInput, MemoryEvidenceRef } from "./evidence-log.ts";
export type PendingPromotionAccounting = {
promotedKeys: Set<string>;
@@ -105,3 +106,118 @@ export function accountPendingPromotions(input: {
clearableKeys,
};
}
function memoryRef(memory: LongTermMemoryEntry | undefined): MemoryEvidenceRef | undefined {
if (!memory) return undefined;
return {
memoryId: memory.id,
memoryKeyHash: memoryKey(memory),
identityKeyHash: workspaceMemoryIdentityKey(memory),
type: memory.type,
source: memory.source,
status: memory.status,
};
}
function retainedMemoryFor(
pending: LongTermMemoryEntry,
event: MemoryConsolidationEvent | undefined,
after: LongTermMemoryEntry[],
): LongTermMemoryEntry | undefined {
if (event?.retainedId) {
const byId = after.find(memory => memory.id === event.retainedId);
if (byId) return byId;
}
const exactKey = memoryKey(pending);
const identityKey = workspaceMemoryIdentityKey(pending);
return after.find(memory => memory.status !== "superseded" && (
memoryKey(memory) === exactKey || workspaceMemoryIdentityKey(memory) === identityKey
));
}
function promotionEventBase(
type: EvidenceEventInput["type"],
outcome: EvidenceEventInput["outcome"],
memory: LongTermMemoryEntry,
reasonCodes: string[],
): EvidenceEventInput {
return {
type,
phase: "promotion",
outcome,
memory: memoryRef(memory),
reasonCodes,
textPreview: memory.text,
};
}
export function promotionAccountingEvidenceEvents(input: {
pending: LongTermMemoryEntry[];
after: LongTermMemoryEntry[];
events?: MemoryConsolidationEvent[];
accounting: PendingPromotionAccounting;
exhaustedRejectedKeys?: Set<string>;
}): EvidenceEventInput[] {
const terminalByKey = new Map((input.events ?? []).map(event => [event.memoryKey, event]));
const exhaustedRejectedKeys = input.exhaustedRejectedKeys ?? new Set<string>();
const evidence: EvidenceEventInput[] = [];
for (const pending of input.pending) {
const key = memoryKey(pending);
const terminal = terminalByKey.get(key);
const retained = retainedMemoryFor(pending, terminal, input.after);
if (input.accounting.promotedKeys.has(key)) {
evidence.push({
...promotionEventBase("promotion_promoted", "promoted", pending, ["new_workspace_entry"]),
relations: [
{ role: "promoted", memory: memoryRef(retained ?? pending) },
],
});
continue;
}
if (input.accounting.absorbedKeys.has(key)) {
const exact = terminal?.reason !== "absorbed_identity";
evidence.push({
...promotionEventBase(
exact ? "promotion_absorbed_exact" : "promotion_absorbed_identity",
"absorbed",
pending,
[exact ? "same_exact_key" : "same_identity_key"],
),
relations: [
{ role: "absorbed" as const, memory: memoryRef(pending) },
{ role: "retained" as const, memory: memoryRef(retained) },
].filter(relation => relation.memory),
});
continue;
}
if (input.accounting.supersededKeys.has(key)) {
evidence.push({
...promotionEventBase("promotion_superseded", "superseded", pending, ["superseded_existing"]),
relations: [
{ role: "superseded" as const, memory: memoryRef(pending) },
{ role: "superseded_by" as const, memory: memoryRef(retained) },
].filter(relation => relation.memory),
});
continue;
}
if (input.accounting.rejectedKeys.has(key)) {
evidence.push(promotionEventBase("promotion_rejected_capacity", "rejected", pending, ["capacity_rejected"]));
if (input.accounting.retryableRejectedKeys.has(key)) {
evidence.push(promotionEventBase(
exhaustedRejectedKeys.has(key) ? "promotion_retry_exhausted" : "promotion_retry_scheduled",
exhaustedRejectedKeys.has(key) ? "exhausted" : "retried",
pending,
[exhaustedRejectedKeys.has(key) ? "max_attempts_reached" : "retryable_capacity_rejection"],
));
}
}
}
return evidence;
}
+216
View File
@@ -0,0 +1,216 @@
import type { LongTermMemoryEntry, WorkspaceMemoryStore } from "./types.ts";
export type ReinforcementBlockReason =
| "min_elapsed_window"
/** @deprecated Historical diagnostic literal; no longer produced by new policy. */
| "same_session"
/** @deprecated Historical diagnostic literal; no longer produced by new policy. */
| "same_utc_day"
/** @deprecated Historical diagnostic literal; no longer produced by new policy. */
| "min_interval"
/** @deprecated Historical diagnostic literal; no longer produced by new policy. */
| "max_count";
export type ReinforcementMode = "increment" | "refresh_only";
type ReinforcementDecisionMetadata = {
attemptedAt: number;
lastReinforcedAt?: number;
elapsedMs?: number;
requiredElapsedMs: number;
sameSession: boolean;
legacyMissingTimestamp?: boolean;
};
export type ReinforcementDecision =
| ({
outcome: "reinforced";
memory: LongTermMemoryEntry;
previousReinforcementCount: number;
newReinforcementCount: number;
reinforcementMode: ReinforcementMode;
} & ReinforcementDecisionMetadata)
| ({
outcome: "blocked";
memory: LongTermMemoryEntry;
blockReason: ReinforcementBlockReason;
reinforcementCount: number;
maxReinforcementCount: number;
} & ReinforcementDecisionMetadata);
// Retention decay model constants (v1.5)
export const BASE_HALF_LIFE_DAYS = 45;
export const REINFORCEMENT_HALFLIFE_FACTOR = 0.85;
export const REINFORCEMENT_MAX_COUNT = 6;
export const DAY_MS = 24 * 60 * 60 * 1000;
export const REINFORCEMENT_MIN_ELAPSED_MS = 7 * DAY_MS;
/** @deprecated Compatibility constant; new policy uses REINFORCEMENT_MIN_ELAPSED_MS. */
export const REINFORCEMENT_MIN_INTERVAL_MS = 60 * 60 * 1000;
export const WORKSPACE_DORMANT_AFTER_DAYS = 14;
export const DORMANT_DECAY_MULTIPLIER = 0.25;
export const TYPE_FACTOR = {
reference: 1.0,
project: 1.25,
feedback: 2.25,
decision: 2.5,
} as const;
export const SOURCE_FACTOR = {
compaction: 1.0,
manual: 1.4,
explicit: 2.0,
} as const;
export const USER_IMPORTANCE_FACTOR = {
low: 0.7,
normal: 1.0,
high: 1.5,
} as const;
export const RETENTION_TYPE_MAX = {
feedback: 10,
decision: 12,
project: 8,
reference: 6,
} as const;
export function calculateInitialStrength(memory: LongTermMemoryEntry): number {
const typeFactor = TYPE_FACTOR[memory.type] ?? 1.0;
const sourceFactor = SOURCE_FACTOR[memory.source] ?? 1.0;
const importanceFactor = USER_IMPORTANCE_FACTOR[memory.userImportance ?? "normal"] ?? 1.0;
return typeFactor * sourceFactor * importanceFactor;
}
export function calculateEffectiveHalfLife(memory: LongTermMemoryEntry): number {
const reinforcementCount = Math.min(
memory.reinforcementCount ?? 0,
REINFORCEMENT_MAX_COUNT,
);
const factor = Math.pow(REINFORCEMENT_HALFLIFE_FACTOR, reinforcementCount);
return BASE_HALF_LIFE_DAYS / factor;
}
function timestampMs(value: unknown, fallback: number): number {
const ms = typeof value === "number" ? value : new Date(String(value)).getTime();
return Number.isFinite(ms) ? ms : fallback;
}
export function calculateRetentionStrength(
memory: LongTermMemoryEntry,
now: number,
lastActivityAt?: string,
): number {
const initialStrength = calculateInitialStrength(memory);
const effectiveHalfLife = calculateEffectiveHalfLife(memory);
// Use retentionClock if available, fallback to updatedAt.
const retentionStart = Number.isFinite(memory.retentionClock)
? memory.retentionClock
: memory.updatedAt ?? memory.createdAt;
const createdAtMs = timestampMs(retentionStart, now);
const effectiveAgeDays = calculateEffectiveAgeDays(createdAtMs, now, lastActivityAt);
// Calculate strength using exponential decay.
const strength = initialStrength * Math.pow(2, -effectiveAgeDays / effectiveHalfLife);
return Number.isFinite(strength) ? Math.max(0, strength) : 0;
}
export function calculateDormantDays(store: WorkspaceMemoryStore, now: number): number {
const lastActivity = store.lastActivityAt
? new Date(store.lastActivityAt).getTime()
: now;
if (!Number.isFinite(lastActivity)) return 0;
const daysSinceActivity = (now - lastActivity) / DAY_MS;
return Math.max(0, daysSinceActivity);
}
export function calculateEffectiveAgeDays(
entryStartMs: number,
now: number,
lastActivityAt?: string,
): number {
const wallAgeDays = Math.max(0, (now - entryStartMs) / DAY_MS);
if (!lastActivityAt) return wallAgeDays;
const lastActivityMs = new Date(lastActivityAt).getTime();
if (!Number.isFinite(lastActivityMs)) return wallAgeDays;
const dormantStartMs = lastActivityMs + WORKSPACE_DORMANT_AFTER_DAYS * DAY_MS;
const overlapStartMs = Math.max(entryStartMs, dormantStartMs);
const dormantOverlapDays = Math.max(0, (now - overlapStartMs) / DAY_MS);
const activeDays = wallAgeDays - dormantOverlapDays;
return activeDays + dormantOverlapDays * DORMANT_DECAY_MULTIPLIER;
}
export function tryReinforceMemory(
memory: LongTermMemoryEntry,
sessionId: string,
now: number,
): ReinforcementDecision {
const count = memory.reinforcementCount ?? 0;
const lastAt = validLastReinforcedAt(memory.lastReinforcedAt);
const lastSession = memory.lastReinforcedSessionID;
const sameSession = lastSession === sessionId;
const legacyMissingTimestamp = count > 0 && lastAt === undefined;
const metadata: ReinforcementDecisionMetadata = {
attemptedAt: now,
...(lastAt !== undefined ? {
lastReinforcedAt: lastAt,
elapsedMs: now - lastAt,
} : {}),
requiredElapsedMs: REINFORCEMENT_MIN_ELAPSED_MS,
sameSession,
...(legacyMissingTimestamp ? { legacyMissingTimestamp: true } : {}),
};
if (lastAt !== undefined && now - lastAt < REINFORCEMENT_MIN_ELAPSED_MS) {
return blockedDecision(memory, "min_elapsed_window", count, metadata);
}
const reinforcementMode: ReinforcementMode = count >= REINFORCEMENT_MAX_COUNT
? "refresh_only"
: "increment";
const newReinforcementCount = reinforcementMode === "refresh_only" ? count : count + 1;
const reinforced: LongTermMemoryEntry = {
...memory,
reinforcementCount: newReinforcementCount,
lastReinforcedAt: now,
lastReinforcedSessionID: sessionId,
retentionClock: now,
};
return {
outcome: "reinforced",
memory: reinforced,
previousReinforcementCount: count,
newReinforcementCount,
reinforcementMode,
...metadata,
};
}
function validLastReinforcedAt(value: unknown): number | undefined {
if (typeof value !== "number") return undefined;
return Number.isFinite(value) && value > 0 ? value : undefined;
}
function blockedDecision(
memory: LongTermMemoryEntry,
blockReason: ReinforcementBlockReason,
reinforcementCount: number,
metadata: ReinforcementDecisionMetadata,
): ReinforcementDecision {
return {
outcome: "blocked",
memory,
blockReason,
reinforcementCount,
maxReinforcementCount: REINFORCEMENT_MAX_COUNT,
...metadata,
};
}
+181 -39
View File
@@ -1,10 +1,14 @@
import { relative } from "path";
import { sessionStatePath } from "./paths.ts";
import { atomicWriteJSON, readJSON, updateJSON } from "./storage.ts";
import type { ActiveFile, LongTermMemoryEntry, OpenError, SessionDecision, SessionState } from "./types.ts";
import { HOT_STATE_LIMITS } from "./types.ts";
import type { ActiveFile, CompactionMemoryRef, LongTermMemoryEntry, OpenError, SessionDecision, SessionState } from "./types.ts";
import { HOT_STATE_LIMITS, LONG_TERM_LIMITS } from "./types.ts";
import { memoryKey } from "./pending-journal.ts";
type SessionStateInput = Omit<SessionState, "compactionMemoryRefs"> & {
compactionMemoryRefs?: unknown;
};
const ACTION_WEIGHT: Record<ActiveFile["action"], number> = {
edit: 50,
write: 45,
@@ -22,6 +26,7 @@ export function createEmptySessionState(sessionID: string): SessionState {
openErrors: [],
recentDecisions: [],
pendingMemories: [],
compactionMemoryRefs: [],
};
}
@@ -33,10 +38,11 @@ export async function loadSessionState(root: string, sessionID: string): Promise
loaded.openErrors = Array.isArray(loaded.openErrors) ? loaded.openErrors : [];
loaded.recentDecisions = Array.isArray(loaded.recentDecisions) ? loaded.recentDecisions : [];
loaded.pendingMemories = Array.isArray(loaded.pendingMemories) ? loaded.pendingMemories : [];
loaded.compactionMemoryRefs = normalizeCompactionMemoryRefs((loaded as SessionStateInput).compactionMemoryRefs);
return loaded;
}
export async function saveSessionState(root: string, state: SessionState): Promise<void> {
export async function saveSessionState(root: string, state: SessionState | SessionStateInput): Promise<void> {
await atomicWriteJSON(await sessionStatePath(root, state.sessionID), normalizeSessionState(state));
}
@@ -52,18 +58,53 @@ export async function updateSessionState(
current.openErrors = Array.isArray(current.openErrors) ? current.openErrors : [];
current.recentDecisions = Array.isArray(current.recentDecisions) ? current.recentDecisions : [];
current.pendingMemories = Array.isArray(current.pendingMemories) ? current.pendingMemories : [];
current.compactionMemoryRefs = normalizeCompactionMemoryRefs((current as SessionStateInput).compactionMemoryRefs);
return normalizeSessionState(await updater(current));
});
}
function normalizeSessionState(state: SessionState): SessionState {
function normalizeSessionState(state: SessionState | SessionStateInput): SessionState {
state.updatedAt = new Date().toISOString();
state.activeFiles = state.activeFiles.slice(0, HOT_STATE_LIMITS.maxActiveFilesStored);
state.openErrors = state.openErrors.slice(0, HOT_STATE_LIMITS.maxOpenErrorsStored);
state.recentDecisions = state.recentDecisions.slice(0, HOT_STATE_LIMITS.maxRecentDecisionsStored);
state.pendingMemories = dedupePendingMemories(Array.isArray(state.pendingMemories) ? state.pendingMemories : [])
.slice(-HOT_STATE_LIMITS.maxPendingMemoriesStored);
return state;
return {
...state,
compactionMemoryRefs: normalizeCompactionMemoryRefs(state.compactionMemoryRefs),
};
}
function normalizeCompactionMemoryRefs(value: unknown): CompactionMemoryRef[] {
if (!Array.isArray(value)) return [];
if (value.some(item => !isCompactionMemoryRef(item))) return [];
return value.slice(0, LONG_TERM_LIMITS.maxEntries);
}
function isCompactionMemoryRef(value: unknown): value is CompactionMemoryRef {
if (!isRecord(value)) return false;
if (typeof value.ref !== "string" || !/^M[1-9]\d*$/.test(value.ref)) return false;
if (typeof value.memoryId !== "string" || value.memoryId.trim() === "") return false;
if (value.compactionId !== undefined && typeof value.compactionId !== "string") return false;
if (!isLongTermType(value.type)) return false;
if (!isLongTermSource(value.source)) return false;
if (typeof value.exactKey !== "string" || value.exactKey.trim() === "") return false;
if (typeof value.identityKey !== "string" || value.identityKey.trim() === "") return false;
if (typeof value.textPreview !== "string") return false;
return typeof value.capturedAt === "number" && Number.isFinite(value.capturedAt);
}
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
function isLongTermType(value: unknown): value is CompactionMemoryRef["type"] {
return value === "feedback" || value === "project" || value === "decision" || value === "reference";
}
function isLongTermSource(value: unknown): value is CompactionMemoryRef["source"] {
return value === "explicit" || value === "compaction" || value === "manual";
}
function dedupePendingMemories(memories: LongTermMemoryEntry[]): LongTermMemoryEntry[] {
@@ -189,49 +230,150 @@ export function clearErrorsForSuccessfulCommand(state: SessionState, command: st
state.updatedAt = new Date().toISOString();
}
export type HotStateSection = "active_files" | "open_errors" | "recent_decisions" | "pending_memories";
export type HotStateOmissionReason = "section_cap" | "char_budget";
export type HotStateOmittedItem = {
section: HotStateSection;
reason: HotStateOmissionReason;
text: string;
memoryId?: string;
};
export type HotSessionStateRenderAccounting = {
prompt: string;
omitted: HotStateOmittedItem[];
maxRenderedChars: number;
};
type HotStateRenderItem = {
section: HotStateSection;
line: string;
memoryId?: string;
};
type HotStateRenderSection = {
heading: `${HotStateSection}:`;
items: HotStateRenderItem[];
};
const HOT_STATE_PREFIX = "Hot session state (current session):";
export function accountHotSessionStateRender(state: SessionState, workspaceRoot: string): HotSessionStateRenderAccounting {
const maxRenderedChars = HOT_STATE_LIMITS.maxRenderedChars;
const omitted: HotStateOmittedItem[] = [];
const sections = buildHotStateRenderSections(state, workspaceRoot, omitted);
if (sections.every(section => section.items.length === 0)) {
return { prompt: "", omitted, maxRenderedChars };
}
const lines: string[] = [HOT_STATE_PREFIX];
let renderedEntryCount = 0;
for (const section of sections) {
let headingRendered = false;
for (const item of section.items) {
if (headingRendered) {
if (wouldFit(lines, item.line, maxRenderedChars)) {
lines.push(item.line);
renderedEntryCount += 1;
} else {
omitted.push(omitHotStateItem(item, "char_budget"));
}
continue;
}
if (wouldFit(lines, section.heading, maxRenderedChars)
&& wouldFit([...lines, section.heading], item.line, maxRenderedChars)) {
lines.push(section.heading, item.line);
headingRendered = true;
renderedEntryCount += 1;
} else {
omitted.push(omitHotStateItem(item, "char_budget"));
}
}
}
if (renderedEntryCount === 0) return { prompt: "", omitted, maxRenderedChars };
return { prompt: lines.join("\n"), omitted, maxRenderedChars };
}
export function renderHotSessionState(state: SessionState, workspaceRoot: string): string {
const activeFiles = rankActiveFiles(state.activeFiles).slice(0, HOT_STATE_LIMITS.maxActiveFilesRendered);
return accountHotSessionStateRender(state, workspaceRoot).prompt;
}
function buildHotStateRenderSections(
state: SessionState,
workspaceRoot: string,
omitted: HotStateOmittedItem[],
): HotStateRenderSection[] {
const activeFiles = rankActiveFiles(state.activeFiles).map(item => ({
section: "active_files" as const,
line: `- ${displayPath(workspaceRoot, item.path)} (${item.action}, ${item.count}x)`,
}));
const openErrors = [...state.openErrors]
.sort((a, b) => b.lastSeen - a.lastSeen)
.slice(0, HOT_STATE_LIMITS.maxOpenErrorsRendered);
const decisions = state.recentDecisions.slice(-HOT_STATE_LIMITS.maxRecentDecisionsStored);
const pendingMemories = dedupePendingMemories(state.pendingMemories)
.slice(-HOT_STATE_LIMITS.maxPendingMemoriesRendered);
.map(err => ({
section: "open_errors" as const,
line: `- [${err.category}] ${err.summary}`,
}));
const decisions = state.recentDecisions.map(item => ({
section: "recent_decisions" as const,
line: `- ${item.text}`,
}));
const pendingMemories = dedupePendingMemories(state.pendingMemories).map(item => ({
section: "pending_memories" as const,
line: `- [${item.type}] ${item.text}`,
memoryId: item.id,
}));
if (activeFiles.length === 0 && openErrors.length === 0 && decisions.length === 0 && pendingMemories.length === 0) return "";
const renderedActiveFiles = capHotStateItems(activeFiles, HOT_STATE_LIMITS.maxActiveFilesRendered, "start", omitted);
const renderedOpenErrors = capHotStateItems(openErrors, HOT_STATE_LIMITS.maxOpenErrorsRendered, "start", omitted);
const renderedDecisions = capHotStateItems(decisions, HOT_STATE_LIMITS.maxRecentDecisionsStored, "end", omitted);
const renderedPendingMemories = capHotStateItems(
pendingMemories,
HOT_STATE_LIMITS.maxPendingMemoriesRendered,
"end",
omitted,
);
const lines: string[] = ["Hot session state (current session):"];
return [
{ heading: "active_files:", items: renderedActiveFiles },
{ heading: "open_errors:", items: renderedOpenErrors },
{ heading: "recent_decisions:", items: renderedDecisions },
{ heading: "pending_memories:", items: renderedPendingMemories },
];
}
if (activeFiles.length > 0) {
lines.push("active_files:");
for (const item of activeFiles) {
const viewPath = displayPath(workspaceRoot, item.path);
lines.push(`- ${viewPath} (${item.action}, ${item.count}x)`);
}
}
function capHotStateItems(
items: HotStateRenderItem[],
cap: number,
keep: "start" | "end",
omitted: HotStateOmittedItem[],
): HotStateRenderItem[] {
if (items.length <= cap) return items;
if (openErrors.length > 0) {
lines.push("open_errors:");
for (const err of openErrors) {
lines.push(`- [${err.category}] ${err.summary}`);
}
}
const renderedItems = keep === "start" ? items.slice(0, cap) : items.slice(-cap);
const omittedItems = keep === "start" ? items.slice(cap) : items.slice(0, items.length - cap);
omitted.push(...omittedItems.map(item => omitHotStateItem(item, "section_cap")));
return renderedItems;
}
if (decisions.length > 0) {
lines.push("recent_decisions:");
for (const decision of decisions) {
lines.push(`- ${decision.text}`);
}
}
function omitHotStateItem(item: HotStateRenderItem, reason: HotStateOmissionReason): HotStateOmittedItem {
return {
section: item.section,
reason,
text: item.line,
...(item.memoryId ? { memoryId: item.memoryId } : {}),
};
}
if (pendingMemories.length > 0) {
lines.push("pending_memories:");
for (const memory of pendingMemories) {
lines.push(`- [${memory.type}] ${memory.text}`);
}
}
return lines.join("\n").slice(0, HOT_STATE_LIMITS.maxRenderedChars);
function wouldFit(lines: string[], nextLine: string, maxRenderedChars: number): boolean {
return [...lines, nextLine].join("\n").length <= maxRenderedChars;
}
function rankActiveFiles(activeFiles: ActiveFile[]): ActiveFile[] {
+76 -1
View File
@@ -3,17 +3,66 @@ import { randomUUID } from "crypto";
import { mkdir, open, readFile, rename, rm, stat, writeFile } from "fs/promises";
import type { FileHandle } from "fs/promises";
import { dirname } from "path";
import { appendEvidenceEventForWorkspaceKey } from "./evidence-log.ts";
const fileLocks = new Map<string, Promise<unknown>>();
const LOCK_WAIT_TIMEOUT_MS = 5000;
const LOCK_STALE_MS = 30_000;
const LOCK_HEARTBEAT_MS = 1_000;
function workspaceKeyFromStorePath(path: string): string | undefined {
return path.match(/[\\/]opencode-working-memory[\\/]workspaces[\\/]([a-f0-9]{16})[\\/]/i)?.[1];
}
function storeKindFromPath(path: string): string {
if (path.endsWith("workspace-memory.json")) return "workspace_memory";
if (path.endsWith("workspace-pending-journal.json")) return "pending_journal";
if (path.includes(`${"/"}sessions${"/"}`) || path.includes(`${"\\"}sessions${"\\"}`)) return "session_state";
return "unknown";
}
async function emitStorageEvidence(path: string, event: Parameters<typeof appendEvidenceEventForWorkspaceKey>[1]): Promise<void> {
const key = workspaceKeyFromStorePath(path);
if (!key) return;
await appendEvidenceEventForWorkspaceKey(key, event).catch(() => undefined);
}
async function quarantineCorruptJSON(path: string): Promise<string | null> {
const quarantinePath = `${path}.corrupt-${Date.now()}-${process.pid}-${randomUUID()}`;
try {
await rename(path, quarantinePath);
return quarantinePath;
} catch {
return null;
}
}
export async function readJSON<T>(path: string, fallback: () => T): Promise<T> {
if (!existsSync(path)) return fallback();
try {
return JSON.parse(await readFile(path, "utf8")) as T;
} catch {
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
const quarantinePath = await quarantineCorruptJSON(path);
if (quarantinePath) {
console.error(`[memory] invalid JSON in ${path}; quarantined to ${quarantinePath}: ${message}`);
await emitStorageEvidence(path, {
type: "storage_corrupt_json_quarantined",
phase: "storage",
outcome: "quarantined",
reasonCodes: ["invalid_json"],
details: {
storeKind: storeKindFromPath(path),
quarantined: true,
},
});
} else {
console.error(`[memory] invalid JSON in ${path}; using fallback without quarantine: ${message}`);
}
return fallback();
}
}
@@ -82,10 +131,30 @@ async function withFileLock<T>(path: string, fn: () => Promise<T>): Promise<T> {
if (await isLockStale(lockPath)) {
await rm(lockPath, { force: true });
await emitStorageEvidence(path, {
type: "storage_stale_lock_recovered",
phase: "storage",
outcome: "recovered",
reasonCodes: ["stale_lock"],
details: {
storeKind: storeKindFromPath(path),
waitMs: Date.now() - started,
},
});
continue;
}
if (Date.now() - started > LOCK_WAIT_TIMEOUT_MS) {
await emitStorageEvidence(path, {
type: "storage_lock_timeout",
phase: "storage",
outcome: "failed",
reasonCodes: ["lock_wait_timeout"],
details: {
storeKind: storeKindFromPath(path),
waitMs: LOCK_WAIT_TIMEOUT_MS,
},
});
throw new Error(`Timed out waiting for lock ${lockPath}`);
}
@@ -95,6 +164,9 @@ async function withFileLock<T>(path: string, fn: () => Promise<T>): Promise<T> {
}
export async function atomicWriteJSON(path: string, data: unknown): Promise<void> {
// Full-state overwrite primitive: callers must already own the complete next
// JSON document. Do not use this for read-modify-write updates that must
// preserve concurrent changes; use updateJSON for that contract instead.
await mkdir(dirname(path), { recursive: true });
const tmp = `${path}.${process.pid}.${Date.now()}.${randomUUID()}.tmp`;
await writeFile(tmp, JSON.stringify(data, null, 2), { encoding: "utf8", mode: 0o600 });
@@ -106,6 +178,9 @@ export async function updateJSON<T>(
fallback: () => T,
updater: (current: T) => T | Promise<T>,
): Promise<T> {
// Locked read-modify-write path: serializes in-process callers and uses a
// filesystem lock for cross-process callers before reading, updating, and
// atomically replacing the JSON document.
const previous = fileLocks.get(path) ?? Promise.resolve();
let release: () => void = () => {};
const currentLock = new Promise<void>(resolve => {
+303
View File
@@ -0,0 +1,303 @@
import {
formatMemoryHelp,
formatMemoryList,
getMemoryList,
renderMemoryCommand,
type MemoryVisibilityCommand,
} from "./memory-visibility.ts";
import { MEMORY_TYPE_ORDER } from "./memory-kind-policy.ts";
type DialogContext = {
clear?: () => void;
};
type DialogSize = "medium" | "large" | "xlarge";
type DialogElement = unknown;
type DialogStackContext = {
clear?: () => void;
replace?: (render: () => DialogElement, onClose?: () => void) => void;
setSize?: (size: DialogSize) => void;
};
type DialogAlertComponent = (props: { title: string; message: string; onConfirm?: () => void }) => DialogElement;
type DialogSelectOption<Value = string> = {
title: string;
value: Value;
description?: string;
footer?: string;
category?: string;
disabled?: boolean;
onSelect?: () => void | Promise<void>;
};
type DialogSelectProps<Value = string> = {
title: string;
placeholder?: string;
options: DialogSelectOption<Value>[];
onSelect?: (option: DialogSelectOption<Value>) => void | Promise<void>;
skipFilter?: boolean;
};
type DialogSelectComponent = <Value = string>(props: DialogSelectProps<Value>) => DialogElement;
type TuiCommand = {
title: string;
value: string;
description?: string;
category?: string;
suggested?: boolean;
slash?: {
name: string;
aliases?: string[];
};
onSelect?: (dialog?: DialogContext) => void | Promise<void>;
};
type TuiRouteCurrent =
| { name: "home" }
| { name: "session"; params: { sessionID: string; prompt?: unknown } }
| { name: string; params?: Record<string, unknown> };
type TuiPluginApi = {
command: {
register: (cb: () => TuiCommand[]) => () => void;
};
route: ({ readonly current: TuiRouteCurrent } | TuiRouteCurrent);
ui: {
DialogAlert?: DialogAlertComponent;
DialogSelect?: DialogSelectComponent;
toast: (input: { variant?: "info" | "success" | "warning" | "error"; message: string }) => void;
dialog?: DialogStackContext;
};
state: {
path: {
directory: string;
};
};
client?: unknown;
};
type TuiPlugin = (api: TuiPluginApi, options: unknown, meta: unknown) => Promise<void>;
function currentRoute(api: TuiPluginApi): TuiRouteCurrent {
const route = api.route as ({ readonly current?: TuiRouteCurrent } & Partial<TuiRouteCurrent>);
return route.current ?? (route as TuiRouteCurrent);
}
function renderErrorReport(error: unknown): string {
const detail = error instanceof Error ? error.message : String(error);
return [
"## Memory error",
"",
"Unable to render local memory visibility output.",
`Error: ${detail}`,
"",
"Local only: no LLM request was made.",
].join("\n");
}
function dialogSizeForCommand(command: MemoryVisibilityCommand): DialogSize {
if (command === "list") return "xlarge";
if (command === "status") return "large";
return "medium";
}
function fallbackTitleForCommand(command: MemoryVisibilityCommand): string {
if (command === "list") return "Current workspace memories";
if (command === "status") return "Memory status";
return "Memory help";
}
function dialogCopyFromMarkdown(text: string, fallbackTitle: string): { title: string; message: string } {
const match = /^##\s+(.+)$/m.exec(text);
if (!match) return { title: fallbackTitle, message: text };
const headingStart = match.index;
const headingEnd = text.indexOf("\n", headingStart);
const before = text.slice(0, headingStart);
const after = headingEnd === -1 ? "" : text.slice(headingEnd + 1);
return {
title: match[1].trim(),
message: `${before}${after}`.replace(/^\s+/, ""),
};
}
function getDialogApi(api: TuiPluginApi): {
DialogAlert: DialogAlertComponent;
DialogSelect: DialogSelectComponent;
dialog: Required<Pick<DialogStackContext, "replace" | "setSize">>;
} | undefined {
if (
typeof api.ui.DialogAlert !== "function" ||
typeof api.ui.DialogSelect !== "function" ||
typeof api.ui.dialog?.replace !== "function" ||
typeof api.ui.dialog?.setSize !== "function"
) {
api.ui.toast({
variant: "error",
message: "Memory dialog UI is unavailable in this OpenCode runtime.",
});
return undefined;
}
return {
DialogAlert: api.ui.DialogAlert,
DialogSelect: api.ui.DialogSelect,
dialog: {
replace: api.ui.dialog.replace,
setSize: api.ui.dialog.setSize,
},
};
}
function showDialogError(api: TuiPluginApi, error: unknown): void {
const detail = error instanceof Error ? error.message : String(error);
api.ui.toast({
variant: "error",
message: `Unable to show memory dialog: ${detail}`,
});
}
function showAlertFromMarkdown(api: TuiPluginApi, text: string, fallbackTitle: string, size: DialogSize): void {
const dialogApi = getDialogApi(api);
if (!dialogApi) return;
const { title, message } = dialogCopyFromMarkdown(text, fallbackTitle);
try {
dialogApi.dialog.replace(() => dialogApi.DialogAlert({ title, message }));
dialogApi.dialog.setSize(size);
} catch (error) {
showDialogError(api, error);
}
}
function showMemoryMenu(api: TuiPluginApi, dialog?: DialogContext): void {
const dialogApi = getDialogApi(api);
if (!dialogApi) return;
const options: DialogSelectOption[] = [
{
title: "Status",
value: "memory.status",
description: "Show local memory statistics",
onSelect: () => showMemoryStatus(api),
},
{
title: "Current memories",
value: "memory.list",
description: "Browse active workspace memories with display-local refs",
onSelect: () => showMemoryList(api),
},
{
title: "Help",
value: "memory.help",
description: "Show memory command help",
onSelect: () => showMemoryHelp(api),
},
];
try {
dialog?.clear?.();
dialogApi.dialog.replace(() => dialogApi.DialogSelect({
title: "Memory",
placeholder: "Search memory actions",
options,
}));
dialogApi.dialog.setSize("large");
} catch (error) {
showDialogError(api, error);
}
}
async function showMemoryStatus(api: TuiPluginApi): Promise<void> {
const route = currentRoute(api);
if (route.name !== "session" || typeof route.params?.sessionID !== "string") {
api.ui.toast({
variant: "warning",
message: "Open a session to use memory commands.",
});
return;
}
const sessionID = route.params.sessionID;
const dialogApi = getDialogApi(api);
if (!dialogApi) return;
let text: string;
let fallbackTitle = fallbackTitleForCommand("status");
try {
text = await renderMemoryCommand(api.state.path.directory, sessionID, "status");
} catch (error) {
text = renderErrorReport(error);
fallbackTitle = "Memory error";
}
const { title, message } = dialogCopyFromMarkdown(text, fallbackTitle);
try {
dialogApi.dialog.replace(() => dialogApi.DialogAlert({ title, message }));
dialogApi.dialog.setSize(dialogSizeForCommand("status"));
} catch (error) {
showDialogError(api, error);
}
}
function showMemoryHelp(api: TuiPluginApi): void {
showAlertFromMarkdown(api, formatMemoryHelp(), "Memory help", "medium");
}
async function showMemoryList(api: TuiPluginApi): Promise<void> {
const dialogApi = getDialogApi(api);
if (!dialogApi) return;
try {
const model = await getMemoryList(api.state.path.directory);
if (model.renderedMemories === 0) {
showAlertFromMarkdown(api, formatMemoryList(model), "Current workspace memories", "medium");
return;
}
const options: DialogSelectOption[] = [];
for (const type of MEMORY_TYPE_ORDER) {
for (const item of model.groups[type]) {
options.push({
title: `[${item.ref}] ${item.text}`,
value: item.ref,
category: type,
footer: "display-local",
});
}
}
dialogApi.dialog.replace(() => dialogApi.DialogSelect({
title: "Current workspace memories",
placeholder: "Search memory refs",
options,
}));
dialogApi.dialog.setSize("xlarge");
} catch (error) {
showAlertFromMarkdown(api, renderErrorReport(error), "Memory error", "medium");
}
}
function memoryCommands(api: TuiPluginApi): TuiCommand[] {
return [
{
title: "Memory",
value: "memory.menu",
description: "Browse local working memory.",
category: "Memory",
slash: { name: "memory" },
onSelect: (dialog?: DialogContext) => showMemoryMenu(api, dialog),
},
];
}
export const MemoryTuiPlugin: TuiPlugin = async (api) => {
api.command.register(() => memoryCommands(api));
};
export default {
id: "working-memory-tui",
tui: MemoryTuiPlugin,
};
+13
View File
@@ -28,6 +28,18 @@ export type LongTermMemoryEntry = {
safetyCritical?: boolean;
};
export type CompactionMemoryRef = {
ref: string;
compactionId?: string;
memoryId: string;
type: LongTermType;
source: LongTermSource;
exactKey: string;
identityKey: string;
textPreview: string;
capturedAt: number;
};
export type WorkspaceMemoryStore = {
version: 1;
workspace: {
@@ -92,6 +104,7 @@ export type SessionState = {
openErrors: OpenError[];
recentDecisions: SessionDecision[];
pendingMemories: LongTermMemoryEntry[];
compactionMemoryRefs: CompactionMemoryRef[];
};
export const LONG_TERM_LIMITS = {
+446 -177
View File
@@ -1,153 +1,27 @@
import { appendFile, mkdir } from "node:fs/promises";
import { dirname } from "node:path";
import type { LongTermMemoryEntry, WorkspaceMemoryStore } from "./types.ts";
import type { CompactionMemoryRef, LongTermMemoryEntry, WorkspaceMemoryStore } from "./types.ts";
import { LONG_TERM_LIMITS } from "./types.ts";
import { migrationLogPath, workspaceKey, workspaceMemoryPath } from "./paths.ts";
import { atomicWriteJSON, readJSON, updateJSON } from "./storage.ts";
import { assessMemoryQuality, isHardQualityReason, isProgressSnapshotViolation } from "./memory-quality.ts";
import { redactCredentials } from "./redaction.ts";
import {
REINFORCEMENT_MAX_COUNT,
RETENTION_TYPE_MAX,
calculateRetentionStrength,
tryReinforceMemory,
type ReinforcementDecision,
} from "./retention.ts";
import type { EvidenceEventInput, MemoryEvidenceRef } from "./evidence-log.ts";
import { appendEvidenceEvents } from "./evidence-log.ts";
import { MEMORY_TYPE_ORDER } from "./memory-kind-policy.ts";
// Minimum length for workspace_memory envelope: <workspace_memory>\n...\n</workspace_memory>
const MIN_ENVELOPE_LENGTH = 80;
const MIGRATION_ID = "2026-04-26-p0-cleanup";
const QUALITY_CLEANUP_MIGRATION_ID = "2026-04-28-quality-cleanup";
// Retention decay model constants (v1.5)
const BASE_HALF_LIFE_DAYS = 45;
const REINFORCEMENT_HALFLIFE_FACTOR = 0.85;
const REINFORCEMENT_MAX_COUNT = 6;
const REINFORCEMENT_MIN_INTERVAL_MS = 60 * 60 * 1000; // 1 hour
const WORKSPACE_DORMANT_AFTER_DAYS = 14;
const DORMANT_DECAY_MULTIPLIER = 0.25;
const DAY_MS = 24 * 60 * 60 * 1000;
const TYPE_FACTOR = {
reference: 1.0,
project: 1.25,
feedback: 2.25,
decision: 2.5,
} as const;
const SOURCE_FACTOR = {
compaction: 1.0,
manual: 1.4,
explicit: 2.0,
} as const;
const USER_IMPORTANCE_FACTOR = {
low: 0.7,
normal: 1.0,
high: 1.5,
} as const;
const SAFETY_CRITICAL_FACTOR = 6.0;
const TYPE_MAX = {
feedback: 10,
decision: 10,
project: 8,
reference: 6,
} as const;
export function calculateInitialStrength(memory: LongTermMemoryEntry): number {
const typeFactor = TYPE_FACTOR[memory.type] ?? 1.0;
const sourceFactor = SOURCE_FACTOR[memory.source] ?? 1.0;
const importanceFactor = USER_IMPORTANCE_FACTOR[memory.userImportance ?? "normal"] ?? 1.0;
const safetyFactor = memory.safetyCritical ? SAFETY_CRITICAL_FACTOR : 1.0;
return typeFactor * sourceFactor * importanceFactor * safetyFactor;
}
export function calculateEffectiveHalfLife(memory: LongTermMemoryEntry): number {
const reinforcementCount = Math.min(
memory.reinforcementCount ?? 0,
REINFORCEMENT_MAX_COUNT,
);
const factor = Math.pow(REINFORCEMENT_HALFLIFE_FACTOR, reinforcementCount);
return BASE_HALF_LIFE_DAYS / factor;
}
function timestampMs(value: unknown, fallback: number): number {
const ms = typeof value === "number" ? value : new Date(String(value)).getTime();
return Number.isFinite(ms) ? ms : fallback;
}
export function calculateRetentionStrength(
memory: LongTermMemoryEntry,
now: number,
lastActivityAt?: string,
): number {
const initialStrength = calculateInitialStrength(memory);
const effectiveHalfLife = calculateEffectiveHalfLife(memory);
// Use retentionClock if available, fallback to updatedAt.
const retentionStart = Number.isFinite(memory.retentionClock)
? memory.retentionClock
: memory.updatedAt ?? memory.createdAt;
const createdAtMs = timestampMs(retentionStart, now);
const effectiveAgeDays = calculateEffectiveAgeDays(createdAtMs, now, lastActivityAt);
// Calculate strength using exponential decay.
const strength = initialStrength * Math.pow(2, -effectiveAgeDays / effectiveHalfLife);
return Number.isFinite(strength) ? Math.max(0, strength) : 0;
}
export function calculateDormantDays(store: WorkspaceMemoryStore, now: number): number {
const lastActivity = store.lastActivityAt
? new Date(store.lastActivityAt).getTime()
: now;
if (!Number.isFinite(lastActivity)) return 0;
const daysSinceActivity = (now - lastActivity) / DAY_MS;
return Math.max(0, daysSinceActivity);
}
export function calculateEffectiveAgeDays(
entryStartMs: number,
now: number,
lastActivityAt?: string,
): number {
const wallAgeDays = Math.max(0, (now - entryStartMs) / DAY_MS);
if (!lastActivityAt) return wallAgeDays;
const lastActivityMs = new Date(lastActivityAt).getTime();
if (!Number.isFinite(lastActivityMs)) return wallAgeDays;
const dormantStartMs = lastActivityMs + WORKSPACE_DORMANT_AFTER_DAYS * DAY_MS;
const overlapStartMs = Math.max(entryStartMs, dormantStartMs);
const dormantOverlapDays = Math.max(0, (now - overlapStartMs) / DAY_MS);
const activeDays = wallAgeDays - dormantOverlapDays;
return activeDays + dormantOverlapDays * DORMANT_DECAY_MULTIPLIER;
}
export function reinforceMemory(
memory: LongTermMemoryEntry,
sessionId: string,
now: number,
): LongTermMemoryEntry {
if (memory.lastReinforcedSessionID === sessionId) {
return memory;
}
if (memory.lastReinforcedAt && now - memory.lastReinforcedAt < REINFORCEMENT_MIN_INTERVAL_MS) {
return memory;
}
if ((memory.reinforcementCount ?? 0) >= REINFORCEMENT_MAX_COUNT) {
return memory;
}
return {
...memory,
reinforcementCount: (memory.reinforcementCount ?? 0) + 1,
lastReinforcedAt: now,
lastReinforcedSessionID: sessionId,
retentionClock: now,
};
}
const RETENTION_CLOCK_BACKFILL_MIGRATION_ID = "2026-05-01-retention-clock-backfill";
export type MemoryConsolidationReason =
| "promoted"
@@ -170,6 +44,7 @@ export type LongTermLimitResult = {
dropped: MemoryConsolidationEvent[];
absorbed: MemoryConsolidationEvent[];
superseded: MemoryConsolidationEvent[];
evidence: EvidenceEventInput[];
};
export type WorkspaceMemoryNormalizationResult = LongTermLimitResult & {
@@ -177,6 +52,26 @@ export type WorkspaceMemoryNormalizationResult = LongTermLimitResult & {
events: MemoryConsolidationEvent[];
};
export type WorkspaceMemoryRenderAccounting = {
rendered: LongTermMemoryEntry[];
omitted: Array<{
memory: LongTermMemoryEntry;
reason: "superseded" | "type_cap" | "global_cap" | "char_budget" | "empty_render_budget";
}>;
evidence: EvidenceEventInput[];
prompt: string;
};
export type WorkspaceMemoryCompactionRefsAccounting = WorkspaceMemoryRenderAccounting & {
refs: CompactionMemoryRef[];
};
type WorkspaceMemoryRenderSelection = {
active: LongTermMemoryEntry[];
omitted: WorkspaceMemoryRenderAccounting["omitted"];
maxChars: number;
};
export type QualityCleanupMigrationLogEntry = {
migrationId: string;
timestamp: string;
@@ -192,6 +87,17 @@ export type QualityCleanupMigrationLogEntry = {
afterStatus: "superseded";
};
export type P0CleanupMigrationResult = {
store: WorkspaceMemoryStore;
events: EvidenceEventInput[];
};
export type QualityCleanupMigrationResult = {
store: WorkspaceMemoryStore;
events: QualityCleanupMigrationLogEntry[];
evidence: EvidenceEventInput[];
};
export async function emptyWorkspaceMemory(root: string): Promise<WorkspaceMemoryStore> {
const nowIso = new Date().toISOString();
return {
@@ -233,6 +139,14 @@ export async function loadWorkspaceMemory(root: string): Promise<WorkspaceMemory
// writes for ordering/capacity/timestamp-only normalization.
if (hasSecurityOrMigrationChange(store, normalized.store)) {
await atomicWriteJSON(path, normalized.store);
// loadWorkspaceMemory has a narrow migration side effect: when a first-load
// migration actually supersedes entries, append evidence for those storage
// changes. Migration IDs keep this idempotent on repeated loads.
const migrationEvidence = normalized.evidence.filter(event => event.type === "memory_migration_superseded");
if (migrationEvidence.length > 0) {
await appendEvidenceEvents(root, migrationEvidence);
}
}
return normalized.store;
@@ -249,6 +163,7 @@ function hasSecurityOrMigrationChange(
if (beforeEntry.text !== afterEntry.text) return true;
if ((beforeEntry.rationale ?? "") !== (afterEntry.rationale ?? "")) return true;
if (beforeEntry.status !== afterEntry.status) return true;
if ((beforeEntry.retentionClock ?? null) !== (afterEntry.retentionClock ?? null)) return true;
}
const beforeMigrations = JSON.stringify(before.migrations ?? []);
@@ -277,8 +192,15 @@ export async function updateWorkspaceMemoryWithAccounting(
const fallback = await emptyWorkspaceMemory(root);
let finalResult: WorkspaceMemoryNormalizationResult | undefined;
const store = await updateJSON(path, () => fallback, async current => {
const normalized = await normalizeWorkspaceMemory(root, current);
finalResult = await normalizeWorkspaceMemoryWithAccounting(root, await updater(normalized));
const currentNormalization = await normalizeWorkspaceMemoryWithAccounting(root, current);
const currentMigrationEvidence = currentNormalization.evidence.filter(event => event.type === "memory_migration_superseded");
finalResult = await normalizeWorkspaceMemoryWithAccounting(root, await updater(currentNormalization.store));
if (currentMigrationEvidence.length > 0) {
finalResult = {
...finalResult,
evidence: [...currentMigrationEvidence, ...finalResult.evidence],
};
}
return finalResult.store;
});
@@ -288,6 +210,7 @@ export async function updateWorkspaceMemoryWithAccounting(
dropped: [],
absorbed: [],
superseded: [],
evidence: [],
events: [],
};
}
@@ -303,7 +226,8 @@ export async function normalizeWorkspaceMemoryWithAccounting(
root: string,
store: WorkspaceMemoryStore,
): Promise<WorkspaceMemoryNormalizationResult> {
const nowIso = new Date().toISOString();
const nowMs = Date.now();
const nowIso = new Date(nowMs).toISOString();
let result: WorkspaceMemoryStore = {
...store,
@@ -340,10 +264,12 @@ export async function normalizeWorkspaceMemoryWithAccounting(
const beforeQualityCleanup = result;
const qualityCleanup = runMigrationQualityCleanup(result, nowIso);
result = qualityCleanup.store;
let migrationEvidence: EvidenceEventInput[] = [];
let skipRemainingMigrations = false;
if (qualityCleanup.events.length > 0) {
try {
await appendQualityCleanupMigrationLog(qualityCleanup.events);
migrationEvidence = [...migrationEvidence, ...qualityCleanup.evidence];
} catch (error) {
console.error("[memory] failed to write quality cleanup migration log:", error);
console.error("[memory] aborting migration to maintain audit trail integrity");
@@ -352,7 +278,18 @@ export async function normalizeWorkspaceMemoryWithAccounting(
}
}
if (!skipRemainingMigrations) {
result = runMigrationP0Cleanup(result, nowIso);
const p0Cleanup = runMigrationP0Cleanup(result, nowIso);
result = p0Cleanup.store;
migrationEvidence = [...migrationEvidence, ...p0Cleanup.events];
}
result.entries = result.entries.map(entry => backfillRetentionClock(entry, nowMs));
if (!result.migrations.includes(RETENTION_CLOCK_BACKFILL_MIGRATION_ID)) {
result = {
...result,
migrations: [...result.migrations, RETENTION_CLOCK_BACKFILL_MIGRATION_ID],
updatedAt: nowIso,
};
}
// P0 accounting only considers active entries. Entries that were already
@@ -376,38 +313,64 @@ export async function normalizeWorkspaceMemoryWithAccounting(
dropped: accounting.dropped,
absorbed: accounting.absorbed,
superseded: accounting.superseded,
evidence: [...migrationEvidence, ...accounting.evidence],
events: [...accounting.dropped, ...accounting.absorbed, ...accounting.superseded],
};
}
function backfillRetentionClock(entry: LongTermMemoryEntry, nowMs: number): LongTermMemoryEntry {
if (Number.isFinite(entry.retentionClock)) {
return entry;
}
const createdAtMs = new Date(entry.createdAt).getTime();
if (Number.isFinite(createdAtMs)) {
return { ...entry, retentionClock: createdAtMs };
}
const updatedAtMs = new Date(entry.updatedAt).getTime();
if (Number.isFinite(updatedAtMs)) {
return { ...entry, retentionClock: updatedAtMs };
}
return { ...entry, retentionClock: nowMs };
}
export function runMigrationP0Cleanup(
store: WorkspaceMemoryStore,
nowIso: string,
): WorkspaceMemoryStore {
): P0CleanupMigrationResult {
if (store.migrations?.includes(MIGRATION_ID)) {
return store;
return { store, events: [] };
}
const events: EvidenceEventInput[] = [];
const entries = store.entries.map(entry => {
if (entry.source !== "compaction") return entry;
if (entry.type !== "project") return entry;
if (entry.status === "superseded") return entry;
if (isProgressSnapshotViolation(entry.text)) {
return {
const superseded = {
...entry,
status: "superseded" as const,
updatedAt: nowIso,
};
events.push(migrationSupersededEvidence(superseded, ["migration:p0_cleanup"], MIGRATION_ID));
return superseded;
}
return entry;
});
return {
...store,
entries,
migrations: [...(store.migrations || []), MIGRATION_ID],
updatedAt: nowIso,
store: {
...store,
entries,
migrations: [...(store.migrations || []), MIGRATION_ID],
updatedAt: nowIso,
},
events,
};
}
@@ -421,12 +384,13 @@ async function appendQualityCleanupMigrationLog(events: QualityCleanupMigrationL
export function runMigrationQualityCleanup(
store: WorkspaceMemoryStore,
nowIso: string,
): { store: WorkspaceMemoryStore; events: QualityCleanupMigrationLogEntry[] } {
): QualityCleanupMigrationResult {
if (store.migrations?.includes(QUALITY_CLEANUP_MIGRATION_ID)) {
return { store, events: [] };
return { store, events: [], evidence: [] };
}
const events: QualityCleanupMigrationLogEntry[] = [];
const evidence: EvidenceEventInput[] = [];
let changed = false;
const entries = store.entries.map(entry => {
if (entry.source !== "compaction") return entry;
@@ -460,12 +424,19 @@ export function runMigrationQualityCleanup(
...hardReasons.map(reason => `quality:${reason}`),
]);
return {
const superseded = {
...entry,
status: "superseded" as const,
updatedAt: nowIso,
tags: [...tags],
};
evidence.push(migrationSupersededEvidence(
superseded,
["migration:quality_cleanup", ...hardReasons.map(reason => `quality:${reason}`)],
QUALITY_CLEANUP_MIGRATION_ID,
{ hardReasons },
));
return superseded;
});
return {
@@ -476,6 +447,7 @@ export function runMigrationQualityCleanup(
updatedAt: changed ? nowIso : store.updatedAt,
},
events,
evidence,
};
}
@@ -559,7 +531,7 @@ function extractConcreteIdentityKey(text: string): string | null {
if (pathIdentity) return pathIdentity;
}
const pathMatch = text.match(/(?:\/[^\s`"'<>]+|(?:\.{1,2}[\\/]|[A-Za-z0-9_.-]+[\\/])[^\s`"'<>]+|[A-Za-z0-9_.-]+\.(?:json|jsonc|ts|tsx|js|jsx|mjs|cjs|md|yaml|yml|toml|lock|config))(?:\b|$)/);
const pathMatch = text.match(/(?:\/[^\0\s`"'<>]+|(?:\.{1,2}[\\/]|[A-Za-z0-9_.-]+[\\/])[^\s`"'<>]+|[A-Za-z0-9_.-]+\.(?:json|jsonc|ts|tsx|js|jsx|mjs|cjs|md|yaml|yml|toml|lock|config))(?:\b|$)/);
if (!pathMatch) return null;
return normalizeConcretePathIdentity(pathMatch[0]);
@@ -588,6 +560,61 @@ function consolidationEvent(
};
}
function capacityRemovalEvidence(
memory: LongTermMemoryEntry,
reason: "type_cap" | "global_cap",
details: {
strengthAtRemoval: number;
rankAtRemoval: number;
typeRankAtRemoval: number;
ageDaysAtRemoval: number;
},
): EvidenceEventInput {
return {
type: "memory_removed_capacity",
phase: "storage",
outcome: "removed",
reasonCodes: [reason],
memory: memoryEvidenceRef(memory),
relations: [{
role: "removed",
memory: memoryEvidenceRef(memory),
}],
details: {
type: memory.type,
globalCap: LONG_TERM_LIMITS.maxEntries,
...(reason === "type_cap" ? { typeCap: RETENTION_TYPE_MAX[memory.type] } : {}),
...(typeof memory.retentionClock === "number" && Number.isFinite(memory.retentionClock) ? { retentionClock: memory.retentionClock } : {}),
...(memory.createdAt ? { createdAt: memory.createdAt } : {}),
...(memory.source ? { source: memory.source } : {}),
...details,
},
};
}
function migrationSupersededEvidence(
memory: LongTermMemoryEntry,
reasonCodes: string[],
migrationId: string,
details: EvidenceEventInput["details"] = {},
): EvidenceEventInput {
return {
type: "memory_migration_superseded",
phase: "storage",
outcome: "superseded",
memory: memoryEvidenceRef(memory),
relations: [{ role: "superseded", memory: memoryEvidenceRef(memory) }],
reasonCodes,
details: {
migrationId,
type: memory.type,
source: memory.source,
...details,
},
textPreview: memory.text,
};
}
/** Choose better memory when identity/topic keys conflict */
function chooseBetterMemory(
a: LongTermMemoryEntry,
@@ -643,6 +670,13 @@ export function enforceLongTermLimitsWithAccounting(
const capped = applyTypeMaxCaps(sorted);
const kept = capped.slice(0, LONG_TERM_LIMITS.maxEntries);
const keptIds = new Set(kept.map(entry => entry.id));
const cappedIds = new Set(capped.map(entry => entry.id));
const typeCapLosers = sorted.filter(entry => !cappedIds.has(entry.id));
const globalCapLosers = capped.filter(entry => !keptIds.has(entry.id));
const capacityEvidence: EvidenceEventInput[] = [
...typeCapLosers.map(entry => capacityRemovalEvidence(entry, "type_cap", capacityRemovalSnapshot(entry, sorted, now, lastActivityAt))),
...globalCapLosers.map(entry => capacityRemovalEvidence(entry, "global_cap", capacityRemovalSnapshot(entry, sorted, now, lastActivityAt))),
];
const capacityDropped = sorted
.filter(entry => !keptIds.has(entry.id))
.map(entry => consolidationEvent(entry, "rejected_capacity"));
@@ -652,36 +686,68 @@ export function enforceLongTermLimitsWithAccounting(
dropped: [...dedupeResult.dropped, ...capacityDropped],
absorbed: dedupeResult.absorbed,
superseded: dedupeResult.superseded,
evidence: [...dedupeResult.evidence, ...capacityEvidence],
};
}
function capacityRemovalSnapshot(
memory: LongTermMemoryEntry,
sorted: LongTermMemoryEntry[],
now: number,
lastActivityAt?: string,
): {
strengthAtRemoval: number;
rankAtRemoval: number;
typeRankAtRemoval: number;
ageDaysAtRemoval: number;
} {
const createdAtMs = new Date(memory.createdAt).getTime();
const rank = sorted.findIndex(entry => entry.id === memory.id);
const typeRank = sorted.filter(entry => entry.type === memory.type).findIndex(entry => entry.id === memory.id);
return {
strengthAtRemoval: calculateRetentionStrength(memory, now, lastActivityAt),
rankAtRemoval: rank >= 0 ? rank + 1 : -1,
typeRankAtRemoval: typeRank >= 0 ? typeRank + 1 : -1,
ageDaysAtRemoval: Number.isFinite(createdAtMs) ? Math.floor(Math.max(0, now - createdAtMs) / 86_400_000) : 0,
};
}
function applyTypeMaxCaps(entries: LongTermMemoryEntry[]): LongTermMemoryEntry[] {
return applyTypeMaxCapsWithOmissions(entries).kept;
}
function applyTypeMaxCapsWithOmissions(entries: LongTermMemoryEntry[]): { kept: LongTermMemoryEntry[]; omitted: LongTermMemoryEntry[] } {
const capped: LongTermMemoryEntry[] = [];
const omitted: LongTermMemoryEntry[] = [];
const typeCounts: Partial<Record<LongTermMemoryEntry["type"], number>> = {};
for (const entry of entries) {
if (entry.safetyCritical) {
capped.push(entry);
const count = typeCounts[entry.type] ?? 0;
const max = RETENTION_TYPE_MAX[entry.type] ?? Infinity;
if (count >= max) {
omitted.push(entry);
continue;
}
const count = typeCounts[entry.type] ?? 0;
const max = TYPE_MAX[entry.type] ?? Infinity;
if (count >= max) continue;
capped.push(entry);
typeCounts[entry.type] = count + 1;
}
return capped;
return { kept: capped, omitted };
}
export function dedupeLongTermEntriesWithAccounting(entries: LongTermMemoryEntry[]): LongTermLimitResult {
const now = Date.now();
const absorbed: MemoryConsolidationEvent[] = [];
const superseded: MemoryConsolidationEvent[] = [];
const evidence: EvidenceEventInput[] = [];
// For project/reference/feedback: dedupe by concrete identity or exact canonical text.
// Feedback is grouped with project/reference for entity dedupe, but
// workspaceMemoryIdentityKey() returns exact key for feedback (no concrete
// identity extraction). This means feedback absorbed_identity is currently
// impossible. When identity key extraction is extended to all types, feedback
// with matching concrete identifiers will correctly produce absorbed_identity.
const projectRefEntries = entries.filter(e => e.type === "project" || e.type === "reference" || e.type === "feedback");
// Build identity key dedup for project/reference/feedback.
@@ -698,11 +764,14 @@ export function dedupeLongTermEntriesWithAccounting(entries: LongTermMemoryEntry
const reason = workspaceMemoryExactKey(entry) === workspaceMemoryExactKey(existing)
? "absorbed_exact" as const
: "absorbed_identity" as const;
const reinforced = reinforceMemory(
const decision = tryReinforceMemory(
retained,
reinforcementSessionId(retained, dropped),
now,
);
const reinforced = decision.memory;
const reinforcedEvent = reinforcementEvidence(retained, dropped, decision, reason);
if (reinforcedEvent) evidence.push(reinforcedEvent);
absorbed.push(consolidationEvent(dropped, reason, reinforced));
entityDeduped.set(key, reinforced);
@@ -723,12 +792,15 @@ export function dedupeLongTermEntriesWithAccounting(entries: LongTermMemoryEntry
const dropped = retained === entry ? existing : entry;
const reason = workspaceMemoryExactKey(entry) === workspaceMemoryExactKey(existing)
? "absorbed_exact" as const
: "superseded_existing" as const;
const reinforced = reinforceMemory(
: "superseded_existing" as const; // v1.5.4 placeholder: unreachable until numbered refs
const decision = tryReinforceMemory(
retained,
reinforcementSessionId(retained, dropped),
now,
);
const reinforced = decision.memory;
const reinforcedEvent = reinforcementEvidence(retained, dropped, decision, reason);
if (reinforcedEvent) evidence.push(reinforcedEvent);
if (reason === "superseded_existing") {
superseded.push(consolidationEvent(dropped, reason, reinforced));
@@ -751,6 +823,93 @@ export function dedupeLongTermEntriesWithAccounting(entries: LongTermMemoryEntry
dropped: [],
absorbed,
superseded,
evidence,
};
}
function memoryEvidenceRef(memory: LongTermMemoryEntry): MemoryEvidenceRef {
return {
memoryId: memory.id,
memoryKeyHash: workspaceMemoryExactKey(memory),
identityKeyHash: workspaceMemoryIdentityKey(memory),
type: memory.type,
source: memory.source,
status: memory.status,
};
}
function reinforcementEvidence(
retained: LongTermMemoryEntry,
dropped: LongTermMemoryEntry,
decision: ReinforcementDecision,
reason: "absorbed_exact" | "absorbed_identity" | "superseded_existing",
): EvidenceEventInput | undefined {
const duplicateReason = reason === "absorbed_identity" ? "duplicate_identity" : "duplicate_exact";
if (decision.outcome === "blocked") {
return {
type: "memory_reinforced",
phase: "reinforcement",
outcome: "rejected",
memory: memoryEvidenceRef(retained),
relations: [
{ role: "target", memory: memoryEvidenceRef(retained) },
{ role: "reinforced_by", memory: memoryEvidenceRef(dropped) },
],
reasonCodes: [duplicateReason, "reinforcement_window_blocked", `reinforcement_block_${decision.blockReason}`],
details: {
memoryId: retained.id,
droppedMemoryId: dropped.id,
blockReason: decision.blockReason,
...reinforcementDecisionTimingDetails(decision),
reinforcementCount: decision.reinforcementCount,
maxReinforcementCount: decision.maxReinforcementCount,
},
textPreview: retained.text,
};
}
const reinforced = decision.memory;
const reasonCodes = [duplicateReason, "reinforcement_window_allowed"];
if (decision.reinforcementMode === "refresh_only") {
reasonCodes.push("reinforcement_saturation_refresh");
}
return {
type: "memory_reinforced",
phase: "reinforcement",
outcome: "reinforced",
memory: memoryEvidenceRef(reinforced),
relations: [
{ role: "reinforced", memory: memoryEvidenceRef(reinforced) },
{ role: "reinforced_by", memory: memoryEvidenceRef(dropped) },
],
reasonCodes,
details: {
memoryId: reinforced.id,
droppedMemoryId: dropped.id,
reinforcementOutcome: decision.reinforcementMode === "refresh_only" ? "refreshed" : "reinforced",
reinforcementMode: decision.reinforcementMode,
...reinforcementDecisionTimingDetails(decision),
previousReinforcementCount: decision.previousReinforcementCount,
newReinforcementCount: decision.newReinforcementCount,
reinforcementCount: decision.newReinforcementCount,
maxReinforcementCount: REINFORCEMENT_MAX_COUNT,
},
textPreview: reinforced.text,
};
}
function reinforcementDecisionTimingDetails(decision: ReinforcementDecision): EvidenceEventInput["details"] {
return {
attemptedAtMs: decision.attemptedAt,
attemptedAtIso: new Date(decision.attemptedAt).toISOString(),
...(decision.lastReinforcedAt !== undefined ? {
lastReinforcedAtMs: decision.lastReinforcedAt,
lastReinforcedAtIso: new Date(decision.lastReinforcedAt).toISOString(),
} : {}),
...(decision.elapsedMs !== undefined ? { elapsedMs: decision.elapsedMs } : {}),
requiredElapsedMs: decision.requiredElapsedMs,
sameSession: decision.sameSession,
...(decision.legacyMissingTimestamp ? { legacyMissingTimestamp: true } : {}),
};
}
@@ -775,32 +934,58 @@ function compareLongTermMemoryForRetention(
return a.id.localeCompare(b.id);
}
function wouldFit(
lines: string[],
nextLine: string,
closingLine: string,
maxChars: number
): boolean {
return [...lines, nextLine, closingLine].join("\n").length <= maxChars;
export function renderWorkspaceMemory(store: WorkspaceMemoryStore): string {
return accountWorkspaceMemoryRender(store).prompt;
}
export function renderWorkspaceMemory(store: WorkspaceMemoryStore): string {
const active = enforceLongTermLimitsWithAccounting(store.entries, store).kept;
if (active.length === 0) return "";
function selectWorkspaceMemoryForRender(store: WorkspaceMemoryStore): WorkspaceMemoryRenderSelection {
const now = Date.now();
const maxChars = Math.min(
store.limits.maxRenderedChars,
LONG_TERM_LIMITS.maxRenderedChars
);
const omitted: WorkspaceMemoryRenderAccounting["omitted"] = [];
for (const entry of store.entries) {
if (entry.status === "superseded") {
omitted.push({ memory: entry, reason: "superseded" });
}
}
const activeEntries = store.entries.filter(entry => entry.status !== "superseded");
const phase1 = activeEntries.map(entry => ({ ...entry, text: entry.text.slice(0, LONG_TERM_LIMITS.maxEntryTextChars) }));
const dedupeResult = dedupeLongTermEntriesWithAccounting(phase1);
const sorted = [...dedupeResult.kept].sort((a, b) => compareLongTermMemoryForRetention(a, b, now, store.lastActivityAt));
const typeCapResult = applyTypeMaxCapsWithOmissions(sorted);
for (const memory of typeCapResult.omitted) omitted.push({ memory, reason: "type_cap" });
const active = typeCapResult.kept.slice(0, LONG_TERM_LIMITS.maxEntries);
for (const memory of typeCapResult.kept.slice(LONG_TERM_LIMITS.maxEntries)) omitted.push({ memory, reason: "global_cap" });
return { active, omitted, maxChars };
}
export function accountWorkspaceMemoryRender(store: WorkspaceMemoryStore): WorkspaceMemoryRenderAccounting {
const { active, omitted, maxChars } = selectWorkspaceMemoryForRender(store);
const evidence: EvidenceEventInput[] = [];
if (active.length === 0) {
for (const item of omitted) evidence.push(renderEvidence(item.memory, "omitted", item.reason));
return { rendered: [], omitted, evidence, prompt: "" };
}
// If maxChars smaller than minimum envelope, return empty string
if (maxChars < MIN_ENVELOPE_LENGTH) return "";
if (maxChars < MIN_ENVELOPE_LENGTH) {
for (const memory of active) omitted.push({ memory, reason: "empty_render_budget" });
for (const item of omitted) evidence.push(renderEvidence(item.memory, "omitted", item.reason));
return { rendered: [], omitted, evidence, prompt: "" };
}
const lines: string[] = [
"Workspace memory (cross-session, verify if stale):",
];
const rendered: LongTermMemoryEntry[] = [];
for (const type of ["feedback", "project", "decision", "reference"] as const) {
for (const type of MEMORY_TYPE_ORDER) {
const items = active.filter(entry => entry.type === type);
if (items.length === 0) continue;
@@ -810,6 +995,9 @@ export function renderWorkspaceMemory(store: WorkspaceMemoryStore): string {
const line = `- ${renderEntry(item)}`;
if ([...lines, ...sectionLines, line].join("\n").length <= maxChars) {
sectionLines.push(line);
rendered.push(item);
} else {
omitted.push({ memory: item, reason: "char_budget" });
}
}
@@ -818,7 +1006,88 @@ export function renderWorkspaceMemory(store: WorkspaceMemoryStore): string {
}
}
return lines.join("\n");
for (const memory of rendered) evidence.push(renderEvidence(memory, "rendered"));
for (const item of omitted) evidence.push(renderEvidence(item.memory, "omitted", item.reason));
return { rendered, omitted, evidence, prompt: lines.join("\n") };
}
export function accountWorkspaceMemoryCompactionRefs(store: WorkspaceMemoryStore): WorkspaceMemoryCompactionRefsAccounting {
const { active, omitted, maxChars } = selectWorkspaceMemoryForRender(store);
const evidence: EvidenceEventInput[] = [];
const originalById = new Map(store.entries.map(entry => [entry.id, entry]));
if (active.length === 0) {
for (const item of omitted) evidence.push(renderEvidence(item.memory, "omitted", item.reason));
return { rendered: [], omitted, evidence, prompt: "", refs: [] };
}
const lines: string[] = [
"Existing workspace memories available for consolidation:",
];
const rendered: LongTermMemoryEntry[] = [];
const refs: CompactionMemoryRef[] = [];
const capturedAt = Date.now();
for (const type of MEMORY_TYPE_ORDER) {
const items = active.filter(entry => entry.type === type);
if (items.length === 0) continue;
const sectionLines: string[] = [`${type}:`];
for (const item of items) {
const ref = `M${refs.length + 1}`;
const line = `[${ref}] ${item.text}`;
if ([...lines, ...sectionLines, line].join("\n").length <= maxChars) {
const original = originalById.get(item.id) ?? item;
sectionLines.push(line);
rendered.push(item);
refs.push({
ref,
memoryId: item.id,
type: original.type,
source: original.source,
exactKey: workspaceMemoryExactKey(original),
identityKey: workspaceMemoryIdentityKey(original),
textPreview: item.text,
capturedAt,
});
} else {
omitted.push({ memory: item, reason: "char_budget" });
}
}
if (sectionLines.length > 1) {
lines.push(...sectionLines);
}
}
for (const memory of rendered) evidence.push(renderEvidence(memory, "rendered"));
for (const item of omitted) evidence.push(renderEvidence(item.memory, "omitted", item.reason));
return {
rendered,
omitted,
evidence,
prompt: rendered.length > 0 ? lines.join("\n") : "",
refs,
};
}
function renderEvidence(
memory: LongTermMemoryEntry,
outcome: "rendered" | "omitted",
reason?: WorkspaceMemoryRenderAccounting["omitted"][number]["reason"],
): EvidenceEventInput {
return {
type: outcome === "rendered" ? "render_selected" : "render_omitted",
phase: "render",
outcome,
memory: memoryEvidenceRef(memory),
relations: [{ role: outcome === "rendered" ? "rendered" : "omitted", memory: memoryEvidenceRef(memory) }],
reasonCodes: outcome === "rendered" ? ["within_caps", "within_char_budget"] : [reason ?? "char_budget"],
textPreview: memory.text,
};
}
function renderEntry(entry: LongTermMemoryEntry): string {
+380
View File
@@ -0,0 +1,380 @@
import test from "node:test";
import assert from "node:assert/strict";
import { createHash } from "node:crypto";
import { existsSync } from "node:fs";
import { mkdir, mkdtemp, readFile, readdir, rm, writeFile } from "node:fs/promises";
import { dirname, join } from "node:path";
import { realpath } from "node:fs/promises";
import { tmpdir } from "node:os";
import {
EVIDENCE_LOG_LIMITS,
appendEvidenceEvent,
appendEvidenceEvents,
queryEvidenceEvents,
summarizeMemoryEvidence,
traceMemoryLifecycle,
type EvidenceEventInput,
type EvidenceEventV1,
} from "../src/evidence-log.ts";
import { workspaceEvidenceLogPath, workspaceKey } from "../src/paths.ts";
async function tempRoot(): Promise<string> {
return mkdtemp(join(tmpdir(), "opencode-evidence-log-"));
}
function eventInput(overrides: Partial<EvidenceEventInput> = {}): EvidenceEventInput {
return {
type: "promotion_promoted",
phase: "promotion",
outcome: "promoted",
reasonCodes: ["new_workspace_entry"],
memory: { memoryId: "mem-a", type: "decision", source: "explicit", status: "active" },
textPreview: "Use npm test before release",
...overrides,
};
}
async function readLog(root: string): Promise<string> {
return readFile(await workspaceEvidenceLogPath(root), "utf8");
}
function privacyHash(value: string): string {
return createHash("sha256").update(value).digest("hex").slice(0, 16);
}
async function workspaceRootHash(root: string): Promise<string> {
const resolved = await realpath(root).catch(() => root);
return privacyHash(resolved);
}
function manualEvent(rootKey: string, rootHash: string, id: string, createdAt: string): EvidenceEventV1 {
return {
version: 1,
eventId: id,
createdAt,
workspaceKey: rootKey,
workspaceRootHash: rootHash,
type: "render_selected",
phase: "render",
outcome: "rendered",
memory: { memoryId: id, type: "decision" },
reasonCodes: ["within_caps", "within_char_budget"],
};
}
test("appendEvidenceEvent writes versioned JSONL with workspace hashes", async () => {
const root = await tempRoot();
try {
const event = await appendEvidenceEvent(root, eventInput({ sessionHash: "session-1", messageHash: "message-1" }));
const raw = await readLog(root);
const stored = JSON.parse(raw.trim()) as EvidenceEventV1;
assert.equal(stored.version, 1);
assert.match(stored.eventId, /^evt_\d+_[a-z0-9]{8}$/);
assert.equal(stored.eventId, event.eventId);
assert.equal(stored.workspaceKey, await workspaceKey(root));
assert.equal(stored.workspaceRootHash, await workspaceRootHash(root));
assert.equal(stored.sessionHash, privacyHash("session-1"));
assert.equal(stored.messageHash, privacyHash("message-1"));
assert.equal(stored.type, "promotion_promoted");
assert.equal(stored.outcome, "promoted");
} finally {
await rm(root, { recursive: true, force: true });
}
});
test("appendEvidenceEvent redacts text previews before writing", async () => {
const root = await tempRoot();
try {
await appendEvidenceEvent(root, eventInput({
type: "extraction_candidate_rejected",
phase: "extraction",
outcome: "rejected",
reasonCodes: ["raw_error"],
textPreview: "password: sushi\nAdmin PIN 是 456123\nBearer abc.def.ghi\nThis candidate is rejected and should be short",
details: {
note: "password: sushi Admin PIN 是 456123 Bearer abc.def.ghi",
},
}));
const raw = await readLog(root);
const stored = JSON.parse(raw.trim()) as EvidenceEventV1;
assert.ok(!raw.includes("sushi"));
assert.ok(!raw.includes("456123"));
assert.ok(!raw.includes("abc.def.ghi"));
assert.ok(stored.textPreview?.includes("[REDACTED]"));
assert.ok((stored.textPreview?.length ?? 0) <= 80);
} finally {
await rm(root, { recursive: true, force: true });
}
});
test("concurrent evidence appends preserve independent JSONL records", async () => {
const root = await tempRoot();
try {
const count = 40;
await Promise.all(Array.from({ length: count }, (_, index) =>
appendEvidenceEvent(root, eventInput({ memory: { memoryId: `concurrent-${index}` } }))
));
const raw = await readLog(root);
const lines = raw.trim().split("\n");
const events = await queryEvidenceEvents(root);
const memoryIds = new Set(events.map(event => event.memory?.memoryId));
assert.equal(lines.length, count);
assert.equal(events.length, count);
for (let index = 0; index < count; index += 1) {
assert.equal(memoryIds.has(`concurrent-${index}`), true);
}
} finally {
await rm(root, { recursive: true, force: true });
}
});
test("queryEvidenceEvents filters by type outcome and memory id", async () => {
const root = await tempRoot();
try {
await appendEvidenceEvents(root, [
eventInput({ type: "promotion_promoted", outcome: "promoted", memory: { memoryId: "mem-a" } }),
eventInput({ type: "render_omitted", phase: "render", outcome: "omitted", reasonCodes: ["type_cap"], memory: { memoryId: "mem-a" } }),
eventInput({ type: "render_omitted", phase: "render", outcome: "omitted", reasonCodes: ["global_cap"], memory: { memoryId: "mem-b" } }),
]);
const result = await queryEvidenceEvents(root, {
types: ["render_omitted"],
outcomes: ["omitted"],
memoryId: "mem-a",
});
assert.equal(result.length, 1);
assert.equal(result[0].type, "render_omitted");
assert.equal(result[0].memory?.memoryId, "mem-a");
} finally {
await rm(root, { recursive: true, force: true });
}
});
test("memory_removed_capacity event round-trips through append and query", async () => {
const root = await tempRoot();
try {
await appendEvidenceEvent(root, eventInput({
type: "memory_removed_capacity",
phase: "storage",
outcome: "removed",
reasonCodes: ["global_cap"],
memory: { memoryId: "removed-memory", type: "reference", source: "compaction", status: "active" },
relations: [{ role: "removed", memory: { memoryId: "removed-memory", type: "reference", source: "compaction", status: "active" } }],
details: {
type: "reference",
globalCap: 28,
retentionClock: Date.UTC(2026, 4, 1),
createdAt: "2026-05-01T00:00:00.000Z",
source: "compaction",
},
}));
const result = await queryEvidenceEvents(root, {
types: ["memory_removed_capacity"],
phases: ["storage"],
outcomes: ["removed"],
memoryId: "removed-memory",
});
assert.equal(result.length, 1);
assert.equal(result[0].type, "memory_removed_capacity");
assert.equal(result[0].phase, "storage");
assert.equal(result[0].outcome, "removed");
assert.deepEqual(result[0].details, {
type: "reference",
globalCap: 28,
retentionClock: Date.UTC(2026, 4, 1),
createdAt: "2026-05-01T00:00:00.000Z",
source: "compaction",
});
} finally {
await rm(root, { recursive: true, force: true });
}
});
test("memory_migration_superseded event round-trips through append and query", async () => {
const root = await tempRoot();
try {
await appendEvidenceEvent(root, eventInput({
type: "memory_migration_superseded",
phase: "storage",
outcome: "superseded",
reasonCodes: ["migration:quality_cleanup", "quality:progress_snapshot"],
memory: { memoryId: "migrated-memory", type: "project", source: "compaction", status: "superseded" },
relations: [{ role: "superseded", memory: { memoryId: "migrated-memory", type: "project", source: "compaction", status: "superseded" } }],
details: {
migrationId: "2026-04-28-quality-cleanup",
type: "project",
source: "compaction",
},
}));
const result = await queryEvidenceEvents(root, {
types: ["memory_migration_superseded"],
phases: ["storage"],
outcomes: ["superseded"],
memoryId: "migrated-memory",
});
assert.equal(result.length, 1);
assert.equal(result[0].type, "memory_migration_superseded");
assert.equal(result[0].phase, "storage");
assert.equal(result[0].outcome, "superseded");
assert.ok(result[0].reasonCodes.includes("migration:quality_cleanup"));
assert.equal(result[0].memory?.status, "superseded");
} finally {
await rm(root, { recursive: true, force: true });
}
});
test("memory_reverted_numbered_ref event round-trips through append and query", async () => {
const root = await tempRoot();
try {
await appendEvidenceEvent(root, eventInput({
type: "memory_reverted_numbered_ref",
phase: "storage",
outcome: "recovered",
reasonCodes: ["manual_revert_numbered_ref"],
memory: { memoryId: "replacement-memory", type: "decision", source: "compaction", status: "superseded" },
relations: [
{ role: "superseded", memory: { memoryId: "replacement-memory", type: "decision", source: "compaction", status: "superseded" } },
{ role: "recovered", memory: { memoryId: "restored-memory", type: "decision", source: "compaction", status: "active" } },
],
}));
const result = await queryEvidenceEvents(root, {
types: ["memory_reverted_numbered_ref"],
phases: ["storage"],
outcomes: ["recovered"],
memoryId: "restored-memory",
});
assert.equal(result.length, 1);
assert.equal(result[0].type, "memory_reverted_numbered_ref");
assert.equal(result[0].phase, "storage");
assert.equal(result[0].outcome, "recovered");
assert.ok(result[0].reasonCodes.includes("manual_revert_numbered_ref"));
assert.ok(result[0].relations?.some(relation => relation.role === "recovered" && relation.memory?.memoryId === "restored-memory"));
} finally {
await rm(root, { recursive: true, force: true });
}
});
test("queryEvidenceEvents supports newestFirst and limit", async () => {
const root = await tempRoot();
try {
const events = await appendEvidenceEvents(root, [
eventInput({ memory: { memoryId: "oldest" } }),
eventInput({ memory: { memoryId: "middle" } }),
eventInput({ memory: { memoryId: "newest" } }),
]);
const result = await queryEvidenceEvents(root, { newestFirst: true, limit: 2 });
assert.deepEqual(result.map(event => event.eventId), [events[2].eventId, events[1].eventId]);
} finally {
await rm(root, { recursive: true, force: true });
}
});
test("appendEvidenceEvent returns a record when appendFile fails", async () => {
const root = await tempRoot();
try {
const path = await workspaceEvidenceLogPath(root);
await mkdir(path, { recursive: true });
const event = await appendEvidenceEvent(root, eventInput());
assert.equal(event.version, 1);
assert.match(event.eventId, /^evt_\d+_[a-z0-9]{8}$/);
assert.equal(event.workspaceKey, await workspaceKey(root));
} finally {
await rm(root, { recursive: true, force: true });
}
});
test("evidence log pruning drops old events, caps count, and quarantines invalid lines", async () => {
const root = await tempRoot();
try {
const path = await workspaceEvidenceLogPath(root);
await mkdir(dirname(path), { recursive: true });
const rootKey = await workspaceKey(root);
const rootHash = await workspaceRootHash(root);
const old = manualEvent(rootKey, rootHash, "old-event", new Date(Date.now() - 91 * 24 * 60 * 60 * 1000).toISOString());
const recentEvents = Array.from({ length: EVIDENCE_LOG_LIMITS.maxEventsPerWorkspace + 1 }, (_, i) =>
manualEvent(rootKey, rootHash, `recent-${i}`, new Date(Date.now() - (EVIDENCE_LOG_LIMITS.maxEventsPerWorkspace - i) * 1000).toISOString())
);
await writeFile(path, [
JSON.stringify(old),
"{not valid json",
...recentEvents.map(event => JSON.stringify(event)),
].join("\n") + "\n", "utf8");
const appended = await appendEvidenceEvents(root, Array.from({ length: EVIDENCE_LOG_LIMITS.pruneEveryAppendCount }, (_, i) =>
eventInput({ memory: { memoryId: `appended-${i}` }, reasonCodes: ["new_workspace_entry"] })
));
const events = await queryEvidenceEvents(root);
const memoryIds = new Set(events.map(event => event.memory?.memoryId));
const files = await readdir(dirname(path));
assert.ok(events.length <= EVIDENCE_LOG_LIMITS.maxEventsPerWorkspace);
assert.equal(memoryIds.has("old-event"), false);
assert.equal(memoryIds.has("recent-0"), false, "oldest events over the count cap should be pruned");
assert.equal(memoryIds.has(appended.at(-1)?.memory?.memoryId), true);
assert.ok(files.some(file => file.startsWith("events.jsonl.corrupt-lines-")));
} finally {
await rm(root, { recursive: true, force: true });
}
});
test("memory evidence summary and lifecycle trace derive latest status", async () => {
const root = await tempRoot();
try {
await appendEvidenceEvents(root, [
eventInput({ type: "extraction_candidate_accepted", phase: "extraction", outcome: "accepted", reasonCodes: ["quality_gate_passed"], memory: { memoryId: "mem-life", memoryKeyHash: "raw-key" } }),
eventInput({ type: "promotion_promoted", phase: "promotion", outcome: "promoted", reasonCodes: ["new_workspace_entry"], memory: { memoryId: "mem-life", memoryKeyHash: "raw-key" } }),
eventInput({ type: "memory_reinforced", phase: "reinforcement", outcome: "reinforced", reasonCodes: ["duplicate_exact"], relations: [{ role: "reinforced", memory: { memoryId: "mem-life", memoryKeyHash: "raw-key" } }] }),
eventInput({ type: "render_selected", phase: "render", outcome: "rendered", reasonCodes: ["within_caps", "within_char_budget"], memory: { memoryId: "mem-life", memoryKeyHash: "raw-key" } }),
]);
const summary = await summarizeMemoryEvidence(root, { memoryId: "mem-life" });
const trace = await traceMemoryLifecycle(root, { memoryId: "mem-life" });
assert.equal(summary.latestOutcome, "rendered");
assert.equal(summary.latestRenderStatus, "rendered");
assert.ok(summary.reasonCodes.includes("duplicate_exact"));
assert.equal(trace.currentStatus, "rendered");
assert.ok(trace.acceptedBy);
assert.ok(trace.promotedBy);
assert.equal(trace.reinforcedBy.length, 1);
assert.equal(trace.latestRender?.type, "render_selected");
} finally {
await rm(root, { recursive: true, force: true });
}
});
test("evidence relation roles reject sublimation placeholders at compile-time surface", () => {
const allowedRoles: Array<NonNullable<EvidenceEventInput["relations"]>[number]["role"]> = [
"candidate",
"pending",
"promoted",
"retained",
"absorbed",
"superseded",
"superseded_by",
"reinforced",
"reinforced_by",
"rendered",
"omitted",
"removed",
];
assert.equal(allowedRoles.includes("candidate"), true);
});
+145 -1
View File
@@ -3,7 +3,13 @@ import assert from "node:assert/strict";
import { mkdtemp, readFile, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { extractErrorsFromBash, extractExplicitMemories, parseWorkspaceMemoryCandidates } from "../src/extractors.ts";
import {
extractErrorsFromBash,
extractExplicitMemories,
extractExplicitMemoriesWithEvidence,
parseWorkspaceMemoryCandidates,
parseWorkspaceMemoryCandidatesWithEvidence,
} from "../src/extractors.ts";
async function waitForFile(path: string, attempts = 20): Promise<string> {
let lastError: unknown;
@@ -145,6 +151,20 @@ test("extractExplicitMemories captures multiple memories in same message", () =>
assert.equal(items.length, 2);
});
test("explicit memory extraction returns detected and ignored evidence", () => {
const result = extractExplicitMemoriesWithEvidence([
"remember this: Prefer deterministic tests.",
"don't remember this: temporary password: sushi",
"remember this: later",
].join("\n"));
assert.equal(result.entries.length, 1);
assert.ok(result.evidence.some(event => event.type === "explicit_memory_detected"));
assert.ok(result.evidence.some(event => event.type === "explicit_memory_ignored" && event.reasonCodes.includes("negated_request")));
assert.ok(result.evidence.some(event => event.type === "explicit_memory_ignored" && event.reasonCodes.includes("deferral")));
assert.equal(JSON.stringify(result.evidence).includes("sushi"), false);
});
// ============================================
// Task 7: Compaction quality gate tests
// ============================================
@@ -176,6 +196,100 @@ test("parseWorkspaceMemoryCandidates rejects raw error", () => {
assert.equal(items.length, 0);
});
test("compaction accepted candidate returns privacy-safe extraction evidence", () => {
const summary = `
Memory candidates:
- [decision] Use accounting evidence events to explain promoted memories in diagnostics.
`;
const result = parseWorkspaceMemoryCandidatesWithEvidence(summary);
assert.equal(result.entries.length, 1);
assert.equal(result.evidence.length, 1);
assert.equal(result.evidence[0].type, "extraction_candidate_accepted");
assert.ok(result.evidence[0].reasonCodes.includes("quality_gate_passed"));
assert.ok(result.evidence[0].reasonCodes.includes("valid_candidate_format"));
assert.match(result.evidence[0].textPreview ?? "", /accounting evidence events/);
});
test("parseWorkspaceMemoryCandidatesWithEvidence returns mixed valid commands and entries", () => {
const summary = `
Memory candidates:
REINFORCE [M3]
REPLACE [M4] [decision] Use numbered refs for safe memory consolidation.
- [feedback] User prefers concise implementation handoffs.
`;
const result = parseWorkspaceMemoryCandidatesWithEvidence(summary);
assert.deepEqual(result.commands, [
{ kind: "REINFORCE", ref: "M3" },
{ kind: "REPLACE", ref: "M4", type: "decision", text: "Use numbered refs for safe memory consolidation." },
]);
assert.equal(result.entries.length, 1);
assert.equal(result.entries[0].type, "feedback");
assert.equal(result.entries[0].text, "User prefers concise implementation handoffs.");
});
test("parseWorkspaceMemoryCandidatesWithEvidence rejects malformed command attempts", () => {
const summary = `
Memory candidates:
REINFORCE M3
REINFORCE [X3]
REPLACE [M3] text without type
REPLACE [M3] [invalid] text
REPLACE [M3] [decision]
`;
const result = parseWorkspaceMemoryCandidatesWithEvidence(summary);
const rejections = result.evidence.filter(event => event.type === "extraction_candidate_rejected");
assert.equal(result.entries.length, 0);
assert.deepEqual(result.commands, []);
assert.equal(rejections.length, 5);
assert.ok(rejections.every(event => event.phase === "extraction"));
assert.ok(rejections.every(event => event.outcome === "rejected"));
assert.deepEqual(rejections.map(event => event.reasonCodes[0]), [
"invalid_memory_ref",
"invalid_memory_ref",
"invalid_memory_type",
"invalid_memory_type",
"empty_replacement_text",
]);
});
test("parseWorkspaceMemoryCandidates accepts bracketed candidates without bullets", () => {
const summary = `
Memory candidates:
[project] This repository uses local JSON stores for workspace memory.
`;
const items = parseWorkspaceMemoryCandidates(summary);
assert.equal(items.length, 1);
assert.equal(items[0].type, "project");
assert.equal(items[0].text, "This repository uses local JSON stores for workspace memory.");
});
test("compaction rejected candidate returns rejection evidence without secrets", () => {
const summary = `
Memory candidates:
- [feedback] password: sushi Admin PIN 456123 Bearer abc.def.ghi TypeError: Cannot read property x
`;
const result = parseWorkspaceMemoryCandidatesWithEvidence(summary);
const raw = JSON.stringify(result.evidence);
assert.equal(result.entries.length, 0);
assert.equal(result.evidence.length, 1);
assert.equal(result.evidence[0].type, "extraction_candidate_rejected");
assert.ok(result.evidence[0].reasonCodes.length > 0);
assert.equal(raw.includes("sushi"), false);
assert.equal(raw.includes("456123"), false);
assert.equal(raw.includes("abc.def.ghi"), false);
assert.ok((result.evidence[0].textPreview?.length ?? 0) <= 80);
});
test("parseWorkspaceMemoryCandidates rejects stack trace", () => {
const summary = `
## Memory Candidates
@@ -332,6 +446,36 @@ Memory candidates:
}
});
test("new rejection records include workspaceKey and workspaceRootHash when provided", async () => {
const dataHome = await mkdtemp(join(tmpdir(), "wm-extraction-scope-data-"));
const previousXdgDataHome = process.env.XDG_DATA_HOME;
process.env.XDG_DATA_HOME = dataHome;
try {
const summary = `
Memory candidates:
- feedback Wave 1 completed successfully and all tests passed
`;
const result = parseWorkspaceMemoryCandidatesWithEvidence(summary, {
workspaceKey: "testkey1234567",
workspaceRootHash: "abcdef123456",
});
assert.equal(result.entries.length, 0);
const logPath = join(dataHome, "opencode-working-memory", "extraction-rejections.jsonl");
const lines = (await waitForFile(logPath)).trim().split("\n");
assert.equal(lines.length, 1);
const event = JSON.parse(lines[0]);
assert.equal(event.workspaceKey, "testkey1234567");
assert.equal(event.workspaceRootHash, "abcdef123456");
} finally {
if (previousXdgDataHome === undefined) delete process.env.XDG_DATA_HOME;
else process.env.XDG_DATA_HOME = previousXdgDataHome;
await rm(dataHome, { recursive: true, force: true });
}
});
test("parseWorkspaceMemoryCandidates redacts secrets in extraction rejection log", async () => {
const dataHome = await mkdtemp(join(tmpdir(), "wm-extraction-redact-data-"));
const previousXdgDataHome = process.env.XDG_DATA_HOME;
+135
View File
@@ -0,0 +1,135 @@
import test from "node:test";
import assert from "node:assert/strict";
import type { EvidenceEventType, EvidenceEventV1, EvidenceOutcome, EvidencePhase } from "../src/evidence-log.ts";
import type { LongTermMemoryEntry } from "../src/types.ts";
import type { MemoryInspectionReadModel } from "../scripts/memory-diag/types.ts";
import { CliInputError, normalizeRejection, rejectionQualitySummary, sinceCutoff } from "../scripts/memory-diag/rejections-model.ts";
import { coverageRows, disappearanceRows } from "../scripts/memory-diag/inspection-model.ts";
import { groupEvidenceByMemoryId } from "../scripts/memory-diag/evidence-model.ts";
import { statusFromTraceEvent } from "../scripts/memory-diag/trace-model.ts";
function entry(id: string, type: LongTermMemoryEntry["type"]): LongTermMemoryEntry {
const now = new Date("2026-01-01T00:00:00.000Z").toISOString();
return {
id,
type,
text: `${id} text`,
source: "compaction",
confidence: 0.75,
status: "active",
createdAt: now,
updatedAt: now,
};
}
function event(overrides: Partial<EvidenceEventV1> & { type: EvidenceEventType; phase: EvidencePhase; outcome: EvidenceOutcome }): EvidenceEventV1 {
return {
version: 1,
eventId: `evt-${overrides.type}-${Math.random()}`,
createdAt: new Date("2026-01-01T00:00:00.000Z").toISOString(),
workspaceKey: "workspace-key",
workspaceRootHash: "workspace-root-hash",
reasonCodes: [],
...overrides,
};
}
function model(entries: LongTermMemoryEntry[], events: EvidenceEventV1[]): MemoryInspectionReadModel {
return {
store: {
version: 1,
workspace: { root: "/tmp/workspace", key: "workspace-key" },
limits: { maxRenderedChars: 24_000, maxEntries: 28 },
entries,
migrations: [],
updatedAt: new Date("2026-01-01T00:00:00.000Z").toISOString(),
},
pending: { version: 1, workspace: { root: "", key: "" }, entries: [], updatedAt: new Date(0).toISOString() },
evidenceEvents: events,
rejectionRecords: [],
currentById: new Map(entries.map(memory => [memory.id, memory])),
evidenceByMemoryId: groupEvidenceByMemoryId(events),
};
}
test("normalizeRejection infers origins from source", () => {
assert.equal(normalizeRejection({ source: "compaction", text: "a", reasons: ["bad_decision"] })?.origin, "compaction_candidate");
assert.equal(normalizeRejection({ source: "explicit", text: "a", reasons: ["bad_feedback"] })?.origin, "explicit_trigger");
assert.equal(normalizeRejection({ source: "manual", text: "a", reasons: ["bad_feedback"] })?.origin, "manual");
assert.equal(normalizeRejection({ source: "unknown-source", text: "a", reasons: ["bad_feedback"] })?.origin, "unknown");
});
test("sinceCutoff accepts relative durations and ISO timestamps", () => {
const now = new Date("2026-01-15T12:00:00.000Z").getTime();
assert.equal(sinceCutoff("14d", now), now - 14 * 86_400_000);
assert.equal(sinceCutoff("3h", now), now - 3 * 3_600_000);
assert.equal(sinceCutoff("30m", now), now - 30 * 60_000);
assert.equal(sinceCutoff("2026-01-01T00:00:00.000Z", now), new Date("2026-01-01T00:00:00.000Z").getTime());
assert.throws(() => sinceCutoff("forever", now), (error: unknown) => {
assert.ok(error instanceof CliInputError);
assert.equal((error as Error).message, "Invalid --since value: forever");
return true;
});
});
test("rejectionQualitySummary keeps architecture-like false-positive grouping", () => {
const records = [
normalizeRejection({ type: "decision", source: "compaction", text: "Retention scoring model uses evidence caps to avoid normalization drift", reasons: ["bad_decision"] }),
normalizeRejection({ type: "decision", source: "compaction", text: "Implemented phase 2 and updated tests", reasons: ["bad_decision"] }),
normalizeRejection({ type: "decision", source: "compaction", text: "Maybe useful", reasons: ["bad_decision"] }),
].filter(record => record !== null);
const summary = rejectionQualitySummary(records);
assert.equal(summary.totalRecords, 3);
assert.equal(summary.possibleFalsePositiveGroups.architecture_like_possible_false_positive.count, 1);
assert.equal(summary.possibleFalsePositiveGroups.clearly_garbage.count, 1);
assert.equal(summary.possibleFalsePositiveGroups.ambiguous.count, 1);
});
test("coverageRows classifies current and historical memory evidence", () => {
const entries = [entry("mem-full", "feedback"), entry("mem-render-only", "decision"), entry("mem-no-evidence", "project")];
const events = [
event({ type: "extraction_candidate_accepted", phase: "extraction", outcome: "accepted", memory: { memoryId: "mem-full", type: "feedback", source: "compaction" } }),
event({ type: "promotion_promoted", phase: "promotion", outcome: "promoted", memory: { memoryId: "mem-full", type: "feedback", source: "compaction" } }),
event({ type: "render_selected", phase: "render", outcome: "rendered", memory: { memoryId: "mem-render-only", type: "decision", source: "compaction" } }),
event({ type: "memory_removed_capacity", phase: "storage", outcome: "removed", memory: { memoryId: "historical-cap", type: "project", source: "compaction" }, reasonCodes: ["global_cap"] }),
event({ type: "promotion_promoted", phase: "promotion", outcome: "promoted", memory: { memoryId: "historical-unknown", type: "reference", source: "compaction" } }),
];
const rows = coverageRows(model(entries, events), true);
const byId = new Map(rows.map(row => [row.id, row.class]));
assert.equal(byId.get("mem-full"), "full_lifecycle");
assert.equal(byId.get("mem-render-only"), "render_only");
assert.equal(byId.get("mem-no-evidence"), "no_evidence");
assert.equal(byId.get("historical-cap"), "historical_absent_with_reason");
assert.equal(byId.get("historical-unknown"), "historical_absent_unknown_reason");
});
test("disappearanceRows surfaces terminal capacity evidence", () => {
const events = [
event({ type: "promotion_promoted", phase: "promotion", outcome: "promoted", memory: { memoryId: "capacity-loser", type: "decision", source: "compaction" } }),
event({ type: "memory_removed_capacity", phase: "storage", outcome: "removed", memory: { memoryId: "capacity-loser", type: "decision", source: "compaction" }, reasonCodes: ["type_cap"] }),
];
const rows = disappearanceRows(model([], events));
assert.equal(rows.length, 1);
assert.equal(rows[0].id, "capacity-loser");
assert.equal(rows[0].classification, "historical_absent_with_reason");
assert.equal(rows[0].terminalType, "memory_removed_capacity");
assert.deepEqual(rows[0].reasonCodes, ["type_cap"]);
});
test("statusFromTraceEvent maps lifecycle events", () => {
assert.equal(statusFromTraceEvent(undefined), "unknown");
assert.equal(statusFromTraceEvent(event({ type: "render_selected", phase: "render", outcome: "rendered" })), "rendered");
assert.equal(statusFromTraceEvent(event({ type: "render_omitted", phase: "render", outcome: "omitted", reasonCodes: ["type_cap"] })), "omitted_type_cap");
assert.equal(statusFromTraceEvent(event({ type: "promotion_absorbed_exact", phase: "promotion", outcome: "absorbed" })), "omitted_absorbed_duplicate");
assert.equal(statusFromTraceEvent(event({ type: "promotion_retry_scheduled", phase: "promotion", outcome: "retried" })), "pending_retry");
assert.equal(statusFromTraceEvent(event({ type: "promotion_retry_exhausted", phase: "promotion", outcome: "exhausted" })), "pending_rejected_capacity");
assert.equal(statusFromTraceEvent(event({ type: "storage_corrupt_json_quarantined", phase: "storage", outcome: "quarantined" })), "quarantined_corrupt_store");
assert.equal(statusFromTraceEvent(event({ type: "promotion_superseded", phase: "promotion", outcome: "superseded" })), "omitted_superseded");
});
+188
View File
@@ -0,0 +1,188 @@
import test from "node:test";
import assert from "node:assert/strict";
import { parseArgs } from "../scripts/memory-diag/cli.ts";
test("help returns usage without exposing hidden or removed commands", () => {
const parsed = parseArgs(["--help"]);
assert.equal(parsed.ok, true);
assert.equal("help" in parsed && parsed.help, true);
assert.match(parsed.usage, /Usage:/);
assert.match(parsed.usage, /memory-diag \[status\]/);
assert.match(parsed.usage, /memory-diag quality/);
assert.match(parsed.usage, /memory-diag commands/);
assert.match(parsed.usage, /memory-diag revert/);
for (const command of ["health", "rejections", "disappearances", "trace", "coverage", "audit"]) {
assert.doesNotMatch(parsed.usage, new RegExp(command));
}
});
test("status defaults when no subcommand", () => {
const parsed = parseArgs([]);
assert.equal(parsed.ok, true);
assert.equal("command" in parsed && parsed.command, "status");
assert.deepEqual("options" in parsed && parsed.options, { raw: false, positional: [] });
});
test("unknown command returns usage error", () => {
const parsed = parseArgs(["unknown"]);
assert.equal(parsed.ok, false);
if (parsed.ok) return;
assert.equal(parsed.message, "Unknown subcommand: unknown");
assert.equal(parsed.exitCode, 1);
assert.match(parsed.usage, /Usage:/);
});
test("removed legacy aliases are ordinary unknown subcommands", () => {
for (const command of ["health", "rejections", "disappearances", "trace"]) {
const parsed = parseArgs([command]);
assert.equal(parsed.ok, false, command);
if (parsed.ok) continue;
assert.equal(parsed.message, `Unknown subcommand: ${command}`);
assert.match(parsed.usage, /Usage:/);
}
});
test("quality accepts read-only workspace json and display flags", () => {
const parsed = parseArgs(["quality", "--workspace", "/tmp/workspace", "--json", "--verbose", "--raw", "--no-emoji"]);
assert.equal(parsed.ok, true);
assert.equal("command" in parsed && parsed.command, "quality");
assert.equal("options" in parsed && parsed.options.workspace, "/tmp/workspace");
assert.equal("options" in parsed && parsed.options.json, true);
assert.equal("options" in parsed && parsed.options.verbose, true);
assert.equal("options" in parsed && parsed.options.raw, true);
assert.equal("options" in parsed && parsed.options.noEmoji, true);
});
test("quality rejects mutation filter and drill-down flags", () => {
const cases: Array<{ args: string[]; message: string }> = [
{ args: ["quality", "--all"], message: "quality does not accept --all" },
{ args: ["quality", "--apply"], message: "quality does not accept --apply" },
{ args: ["quality", "--memory", "mem-1"], message: "quality does not accept --memory" },
{ args: ["quality", "--event", "evt-1"], message: "quality does not accept --event" },
{ args: ["quality", "--reason", "bad_decision"], message: "quality does not accept rejection filters" },
{ args: ["quality", "--since", "7d"], message: "quality does not accept rejection filters" },
{ args: ["quality", "--soft-only"], message: "quality does not accept rejection filters" },
{ args: ["quality", "--trigger-only"], message: "quality does not accept rejection filters" },
{ args: ["quality", "--include-historical"], message: "quality does not accept --include-historical" },
{ args: ["quality", "--explain"], message: "quality does not accept --explain" },
];
for (const item of cases) {
const parsed = parseArgs(item.args);
assert.equal(parsed.ok, false, item.args.join(" "));
if (parsed.ok) continue;
assert.equal(parsed.message, item.message);
assert.match(parsed.usage, /Usage:/);
}
});
test("hidden maintainer commands are accepted with neutral notices", () => {
const coverage = parseArgs(["coverage"]);
assert.equal(coverage.ok, true);
assert.equal("command" in coverage && coverage.command, "coverage");
assert.equal("deprecationNotice" in coverage && coverage.deprecationNotice, "Note: 'coverage' is a maintainer-only diagnostic and is not part of the public CLI surface.");
const audit = parseArgs(["audit"]);
assert.equal(audit.ok, true);
assert.equal("command" in audit && audit.command, "audit");
assert.equal("deprecationNotice" in audit && audit.deprecationNotice, "Note: 'audit' is a maintainer-only diagnostic and is not part of the public CLI surface.");
});
test("current command flag validation messages are preserved", () => {
const cases: Array<{ args: string[]; message: string }> = [
{ args: ["status", "--all"], message: "status does not accept --all" },
{ args: ["coverage", "--all"], message: "coverage does not accept --all" },
{ args: ["missing", "--all"], message: "missing does not accept --all" },
{ args: ["rejected", "--all"], message: "rejected does not accept --all" },
{ args: ["audit", "--workspace", "/tmp/workspace"], message: "audit does not accept --all or --workspace" },
{ args: ["explain", "--all"], message: "explain does not accept --all" },
{ args: ["status", "--since", "forever"], message: "status does not accept rejection filters" },
{ args: ["status", "--reason", "bad_decision"], message: "status does not accept rejection filters" },
{ args: ["status", "--quality"], message: "Unknown option: --quality" },
{ args: ["rejected", "--unique"], message: "Unknown option: --unique" },
];
for (const item of cases) {
const parsed = parseArgs(item.args);
assert.equal(parsed.ok, false, item.args.join(" "));
if (parsed.ok) continue;
assert.equal(parsed.message, item.message);
assert.match(parsed.usage, /Usage:/);
}
});
test("commands accepts workspace json and verbose flags", () => {
const parsed = parseArgs(["commands", "--workspace", "/tmp/workspace", "--json", "--verbose"]);
assert.equal(parsed.ok, true);
assert.equal("command" in parsed && parsed.command, "commands");
assert.equal("options" in parsed && parsed.options.workspace, "/tmp/workspace");
assert.equal("options" in parsed && parsed.options.json, true);
assert.equal("options" in parsed && parsed.options.verbose, true);
});
test("commands accepts memory drill-down selector", () => {
const parsed = parseArgs(["commands", "--workspace", "/tmp/workspace", "--memory", "mem-1", "--json", "--verbose"]);
assert.equal(parsed.ok, true);
assert.equal("command" in parsed && parsed.command, "commands");
assert.equal("options" in parsed && parsed.options.memory, "mem-1");
assert.equal("options" in parsed && parsed.options.json, true);
assert.equal("options" in parsed && parsed.options.verbose, true);
});
test("revert accepts memory or event selectors and apply flag", () => {
const byMemory = parseArgs(["revert", "--memory", "mem-new", "--workspace", "/tmp/workspace", "--apply"]);
assert.equal(byMemory.ok, true);
assert.equal("command" in byMemory && byMemory.command, "revert");
assert.equal("options" in byMemory && byMemory.options.memory, "mem-new");
assert.equal("options" in byMemory && byMemory.options.workspace, "/tmp/workspace");
assert.equal("options" in byMemory && byMemory.options.apply, true);
const byEvent = parseArgs(["revert", "--event", "evt_1"]);
assert.equal(byEvent.ok, true);
assert.equal("command" in byEvent && byEvent.command, "revert");
assert.equal("options" in byEvent && byEvent.options.event, "evt_1");
assert.equal("options" in byEvent && byEvent.options.apply, undefined);
});
test("revert requires exactly one selector", () => {
const missing = parseArgs(["revert"]);
assert.equal(missing.ok, false);
if (!missing.ok) assert.equal(missing.message, "revert requires --memory or --event");
const both = parseArgs(["revert", "--memory", "mem-new", "--event", "evt_1"]);
assert.equal(both.ok, false);
if (!both.ok) assert.equal(both.message, "Use either --memory or --event, not both");
});
test("rejected invalid since value returns current error", () => {
const parsed = parseArgs(["rejected", "--since", "forever"]);
assert.equal(parsed.ok, false);
if (parsed.ok) return;
assert.equal(parsed.message, "Invalid --since value: forever");
assert.match(parsed.usage, /Usage:/);
});
test("explain accepts positional memory id", () => {
const parsed = parseArgs(["explain", "mem-1", "--workspace", "/tmp/workspace"]);
assert.equal(parsed.ok, true);
assert.equal("command" in parsed && parsed.command, "explain");
assert.equal("options" in parsed && parsed.options.memory, "mem-1");
assert.deepEqual("options" in parsed && parsed.options.positional, ["mem-1"]);
});
test("explain with both positional and memory flag errors", () => {
const parsed = parseArgs(["explain", "mem-1", "--memory", "mem-2"]);
assert.equal(parsed.ok, false);
if (parsed.ok) return;
assert.equal(parsed.message, "Use either explain <memory-id> or --memory, not both");
});
+110
View File
@@ -0,0 +1,110 @@
import test from "node:test";
import assert from "node:assert/strict";
import type { EvidenceEventType, EvidenceEventV1, EvidenceOutcome, EvidencePhase } from "../src/evidence-log.ts";
import type { MemoryInspectionReadModel, WorkspaceDiagSnapshot } from "../scripts/memory-diag/types.ts";
import { formatCoverage } from "../scripts/memory-diag/formatters/coverage.ts";
import { formatMigrationAudit } from "../scripts/memory-diag/formatters/audit.ts";
import { formatExplain } from "../scripts/memory-diag/formatters/explain.ts";
import { buildMissingJSON, formatMissing } from "../scripts/memory-diag/formatters/missing.ts";
import { formatTrace } from "../scripts/memory-diag/formatters/trace.ts";
function emptyInspectionModel(): MemoryInspectionReadModel {
return {
store: {
version: 1,
workspace: { root: "/tmp/workspace", key: "workspace-key" },
limits: { maxRenderedChars: 24_000, maxEntries: 28 },
entries: [],
migrations: [],
updatedAt: new Date("2026-01-01T00:00:00.000Z").toISOString(),
},
pending: { version: 1, workspace: { root: "", key: "" }, entries: [], updatedAt: new Date(0).toISOString() },
evidenceEvents: [],
rejectionRecords: [],
currentById: new Map(),
evidenceByMemoryId: new Map(),
};
}
function emptySnapshot(): WorkspaceDiagSnapshot {
return {
store: emptyInspectionModel().store,
journal: emptyInspectionModel().pending,
retention: { sorted: [], rendered: [], typeCapped: [], globalCapped: [] },
memories: [],
recentEvents: [],
allEvents: [],
summary: {
storedActive: 0,
rendered: 0,
pending: 0,
rejectedLast7Days: 0,
corruptStoresQuarantinedLast30Days: 0,
},
};
}
function event(overrides: Partial<EvidenceEventV1> & { type: EvidenceEventType; phase: EvidencePhase; outcome: EvidenceOutcome }): EvidenceEventV1 {
return {
version: 1,
eventId: `evt-${overrides.type}`,
createdAt: new Date("2026-01-01T00:00:00.000Z").toISOString(),
workspaceKey: "workspace-key",
workspaceRootHash: "workspace-root-hash",
reasonCodes: [],
...overrides,
};
}
test("coverage formatter includes class counts section", () => {
const output = formatCoverage([]);
assert.match(output, /Class counts:/);
assert.match(output, /Per-memory rows:\n \(none\)/);
});
test("missing formatter verbose output preserves disappearance details", () => {
const output = formatMissing([], { verbose: true });
assert.match(output, /No missing memories found\./);
});
test("missing JSON includes disappearance rows and summary", () => {
const rows = [{
id: "historical-1",
classification: "historical_absent_unknown_reason" as const,
terminalType: "unknown" as const,
reasonCodes: [],
events: [event({ type: "extraction_candidate_accepted", phase: "extraction", outcome: "accepted" })],
event: undefined,
}];
const output = buildMissingJSON(rows, { generatedAt: "2026-01-01T00:00:00.000Z" });
assert.equal(output.version, 1);
assert.deepEqual(output.summary, { total: 1, explained: 0, needsReview: 1 });
assert.deepEqual((output.disappearances as Array<{ id: string }>)[0]?.id, "historical-1");
});
test("trace formatter includes lifecycle section", () => {
const output = formatTrace("mem-1", emptySnapshot(), {
events: [event({ type: "render_selected", phase: "render", outcome: "rendered", memory: { memoryId: "mem-1", type: "feedback", source: "explicit" } })],
});
assert.match(output, /Lifecycle:/);
assert.match(output, /evt-render_selected render_selected/);
});
test("audit formatter preserves no-log output", () => {
const output = formatMigrationAudit([], { raw: false });
assert.match(output, /Migration audit report/);
assert.match(output, /No migration logs found\./);
});
test("explain formatter preserves no-memory output", () => {
const output = formatExplain(emptySnapshot());
assert.match(output, /Workspace memory explainability/);
assert.match(output, /No memories found\./);
});
File diff suppressed because it is too large Load Diff
+54
View File
@@ -0,0 +1,54 @@
import test from "node:test";
import assert from "node:assert/strict";
import { mkdtemp, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { canonicalMemoryText, cleanText, countBy, sortedCounts, truncate } from "../scripts/memory-diag/text.ts";
import { readJSONLFile } from "../scripts/memory-diag/io.ts";
test("cleanText redacts credentials and absolute paths unless raw", () => {
const text = "Use password: sushi and api_key=abc123 in /Users/alice/project/config.json";
const cleaned = cleanText(text, false);
assert.match(cleaned, /password: \[REDACTED\]/);
assert.match(cleaned, /api_key=\[REDACTED\]/);
assert.match(cleaned, /<path>/);
assert.doesNotMatch(cleaned, /sushi/);
assert.doesNotMatch(cleaned, /\/Users\/alice/);
assert.equal(cleanText(text, true), text);
});
test("truncate collapses whitespace and applies ellipsis at max length", () => {
assert.equal(truncate(" hello\n\tworld "), "hello world");
assert.equal(truncate("abcdef", 5), "abcd…");
});
test("canonicalMemoryText normalizes punctuation and case", () => {
assert.equal(canonicalMemoryText("Hello, WORLD!!! Path?"), "hello world path");
});
test("sortedCounts sorts by count descending then key ascending", () => {
const counts = new Map<string, number>([["b", 2], ["a", 2], ["c", 3]]);
assert.deepEqual(sortedCounts(counts), [["c", 3], ["a", 2], ["b", 2]]);
});
test("countBy counts string items", () => {
assert.deepEqual([...countBy(["beta", "alpha", "beta"]).entries()], [["beta", 2], ["alpha", 1]]);
});
test("readJSONLFile returns valid records and invalid line count", async () => {
const root = await mkdtemp(join(tmpdir(), "opencode-memory-diag-utils-"));
try {
const path = join(root, "records.jsonl");
await writeFile(path, '{"id":"one"}\nnot-json\n\n{"id":"two"}\n', "utf8");
const result = await readJSONLFile<{ id: string }>(path);
assert.deepEqual(result.records, [{ id: "one" }, { id: "two" }]);
assert.equal(result.invalidLines, 1);
} finally {
await rm(root, { recursive: true, force: true });
}
});
+224
View File
@@ -0,0 +1,224 @@
import test from "node:test";
import assert from "node:assert/strict";
import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { dirname, join } from "node:path";
import { appendEvidenceEvents, queryEvidenceEvents, summarizeMemoryEvidence, type EvidenceEventInput, type EvidenceEventV1 } from "../src/evidence-log.ts";
import { groupEvidenceByMemoryId } from "../scripts/memory-diag/evidence-model.ts";
import { retentionCandidatesForDiag } from "../scripts/memory-diag/retention-model.ts";
import { buildMemoryDiagJSON, memoryDiagJSONFromSnapshot, normalizedJournal, normalizedStore, snapshotForOptions } from "../scripts/memory-diag/workspace-snapshot.ts";
import { workspaceKey, workspaceMemoryPath, workspacePendingJournalPath } from "../src/paths.ts";
import { LONG_TERM_LIMITS, type LongTermMemoryEntry, type PendingMemoryJournalStore, type WorkspaceMemoryStore } from "../src/types.ts";
function entry(id: string, text: string, type: LongTermMemoryEntry["type"]): LongTermMemoryEntry {
const now = new Date("2026-01-01T00:00:00.000Z").toISOString();
return {
id,
type,
text,
source: "compaction",
confidence: 0.75,
status: "active",
createdAt: now,
updatedAt: now,
retentionClock: new Date(now).getTime(),
};
}
function evidence(overrides: Partial<EvidenceEventInput>): EvidenceEventInput {
return {
type: "promotion_promoted",
phase: "promotion",
outcome: "promoted",
memory: { memoryId: "mem-active", type: "decision", source: "compaction", status: "active" },
reasonCodes: ["new_workspace_entry"],
...overrides,
};
}
function groupedEvidenceSummary(grouped: Map<string, EvidenceEventV1[]>, memoryId: string): { eventIds: string[]; reasonCodes: string[] } {
const events = grouped.get(memoryId) ?? [];
const reasonCodes = new Set<string>();
for (const event of events) {
for (const reason of event.reasonCodes) reasonCodes.add(reason);
}
return {
eventIds: events.map(event => event.eventId),
reasonCodes: [...reasonCodes],
};
}
async function writeWorkspaceStore(root: string, entries: LongTermMemoryEntry[]): Promise<void> {
const key = await workspaceKey(root);
const path = await workspaceMemoryPath(root);
const store: WorkspaceMemoryStore = {
version: 1,
workspace: { root, key },
limits: { maxRenderedChars: LONG_TERM_LIMITS.maxRenderedChars, maxEntries: LONG_TERM_LIMITS.maxEntries },
entries,
migrations: [],
updatedAt: new Date("2026-01-01T00:00:00.000Z").toISOString(),
};
await mkdir(dirname(path), { recursive: true });
await writeFile(path, JSON.stringify(store, null, 2), "utf8");
}
async function writePendingJournal(root: string, entries: LongTermMemoryEntry[]): Promise<void> {
const key = await workspaceKey(root);
const path = await workspacePendingJournalPath(root);
const store: PendingMemoryJournalStore = {
version: 1,
workspace: { root, key },
entries,
updatedAt: new Date("2026-01-01T00:00:00.000Z").toISOString(),
};
await mkdir(dirname(path), { recursive: true });
await writeFile(path, JSON.stringify(store, null, 2), "utf8");
}
test("normalizedStore returns an empty store with limits and empty arrays", () => {
const store = normalizedStore(null, "/tmp/example-workspace", "workspace-key");
assert.equal(store.version, 1);
assert.deepEqual(store.workspace, { root: "/tmp/example-workspace", key: "workspace-key" });
assert.deepEqual(store.limits, { maxRenderedChars: LONG_TERM_LIMITS.maxRenderedChars, maxEntries: LONG_TERM_LIMITS.maxEntries });
assert.deepEqual(store.entries, []);
assert.deepEqual(store.migrations, []);
});
test("normalizedJournal returns an empty journal", () => {
const journal = normalizedJournal(null);
assert.equal(journal.version, 1);
assert.deepEqual(journal.workspace, { root: "", key: "" });
assert.deepEqual(journal.entries, []);
assert.equal(journal.updatedAt, new Date(0).toISOString());
});
test("retentionCandidatesForDiag separates rendered, type-capped, and global-capped entries", () => {
const entries: LongTermMemoryEntry[] = [
...Array.from({ length: 11 }, (_, i) => entry(`feedback-${String(i).padStart(2, "0")}`, `Feedback memory ${i}`, "feedback")),
...Array.from({ length: 10 }, (_, i) => entry(`decision-${String(i).padStart(2, "0")}`, `Decision memory ${i}`, "decision")),
...Array.from({ length: 8 }, (_, i) => entry(`project-${String(i).padStart(2, "0")}`, `Project memory ${i}`, "project")),
...Array.from({ length: 6 }, (_, i) => entry(`reference-${String(i).padStart(2, "0")}`, `Reference memory ${i}`, "reference")),
];
const store = normalizedStore({ entries, workspace: { root: "/tmp/root", key: "key" } } as WorkspaceMemoryStore, "/tmp/root", "key");
const candidates = retentionCandidatesForDiag(store, new Date("2026-01-02T00:00:00.000Z").getTime());
assert.equal(candidates.rendered.length, LONG_TERM_LIMITS.maxEntries);
assert.equal(candidates.typeCapped.length, 1);
assert.equal(candidates.globalCapped.length, 6);
assert.equal(candidates.typeCapped[0].entry.type, "feedback");
});
test("buildMemoryDiagJSON redacts previews, includes pending entries, and preserves summary fields", async () => {
const root = await mkdtemp(join(tmpdir(), "opencode-memory-diag-workspace-model-"));
try {
const active = entry("mem-active", "Remember password: sushi and file /Users/alice/private.txt", "decision");
const pending = { ...entry("mem-pending", "Pending api_key=secret-value", "project"), promotionAttempts: 1 };
await writeWorkspaceStore(root, [active]);
await writePendingJournal(root, [pending]);
const diag = await buildMemoryDiagJSON(root);
assert.equal(diag.version, 1);
assert.equal(diag.summary.storedActive, 1);
assert.equal(diag.summary.rendered, 1);
assert.equal(diag.summary.pending, 1);
assert.equal(diag.summary.rejectedLast7Days, 0);
assert.equal(diag.summary.corruptStoresQuarantinedLast30Days, 0);
assert.equal(diag.memories.length, 2);
assert.equal(diag.memories.find(memory => memory.id === "mem-pending")?.status, "pending_retry");
assert.ok(diag.memories.some(memory => memory.textPreview?.includes("[REDACTED]")));
assert.ok(diag.memories.some(memory => memory.textPreview?.includes("<path>")));
assert.ok(!JSON.stringify(diag).includes("sushi"));
assert.ok(!JSON.stringify(diag).includes("secret-value"));
assert.equal(diag.workspace.key, await workspaceKey(root));
} finally {
await rm(root, { recursive: true, force: true });
}
});
test("memoryDiagJSONFromSnapshot serializes an existing snapshot with fixed generatedAt", async () => {
const root = await mkdtemp(join(tmpdir(), "opencode-memory-diag-snapshot-json-"));
try {
const active = entry("mem-active", "Stable decision memory", "decision");
const pending = { ...entry("mem-pending", "Pending project memory", "project"), promotionAttempts: 1 };
await writeWorkspaceStore(root, [active]);
await writePendingJournal(root, [pending]);
const snapshot = await snapshotForOptions({ raw: false, workspace: root });
const generatedAt = "2026-05-02T00:00:00.000Z";
const diag = memoryDiagJSONFromSnapshot(root, snapshot, generatedAt);
assert.equal(diag.version, 1);
assert.equal(diag.generatedAt, generatedAt);
assert.equal(diag.memories, snapshot.memories);
assert.equal(diag.recentEvents, snapshot.recentEvents);
assert.equal(diag.summary, snapshot.summary);
} finally {
await rm(root, { recursive: true, force: true });
}
});
test("grouped evidence summaries match per-memory summaries for stored pending and absorbed memories", async () => {
const root = await mkdtemp(join(tmpdir(), "opencode-memory-diag-evidence-equivalence-"));
try {
const active = entry("mem-active", "Stable decision memory", "decision");
const pending = { ...entry("mem-pending", "Pending project memory", "project"), promotionAttempts: 1 };
await writeWorkspaceStore(root, [active]);
await writePendingJournal(root, [pending]);
await appendEvidenceEvents(root, [
evidence({ memory: { memoryId: "mem-active", type: "decision", source: "compaction", status: "active" }, reasonCodes: ["stored_reason"] }),
evidence({ type: "pending_memory_appended", phase: "pending_journal", outcome: "accepted", memory: { memoryId: "mem-pending", type: "project", source: "compaction" }, reasonCodes: ["pending_reason"] }),
evidence({ type: "promotion_absorbed_exact", phase: "promotion", outcome: "absorbed", memory: { memoryId: "mem-absorbed", type: "feedback", source: "compaction" }, reasonCodes: ["same_exact_key"] }),
]);
const grouped = groupEvidenceByMemoryId(await queryEvidenceEvents(root));
for (const id of ["mem-active", "mem-pending", "mem-absorbed"]) {
const oldSummary = await summarizeMemoryEvidence(root, { memoryId: id });
const groupedSummary = groupedEvidenceSummary(grouped, id);
assert.deepEqual(groupedSummary.eventIds, oldSummary.eventIds);
assert.deepEqual(groupedSummary.reasonCodes, oldSummary.reasonCodes);
}
} finally {
await rm(root, { recursive: true, force: true });
}
});
test("buildMemoryDiagJSON preserves evidence ids and reason codes for stored pending and absorbed memories", async () => {
const root = await mkdtemp(join(tmpdir(), "opencode-memory-diag-evidence-rows-"));
try {
const active = entry("mem-active", "Stable decision memory", "decision");
const pending = { ...entry("mem-pending", "Pending project memory", "project"), promotionAttempts: 1 };
await writeWorkspaceStore(root, [active]);
await writePendingJournal(root, [pending]);
const events = await appendEvidenceEvents(root, [
evidence({ memory: { memoryId: "mem-active", type: "decision", source: "compaction", status: "active" }, reasonCodes: ["stored_reason"] }),
evidence({ type: "pending_memory_appended", phase: "pending_journal", outcome: "accepted", memory: { memoryId: "mem-pending", type: "project", source: "compaction" }, reasonCodes: ["pending_reason"] }),
evidence({ type: "promotion_absorbed_exact", phase: "promotion", outcome: "absorbed", memory: { memoryId: "mem-absorbed", type: "feedback", source: "compaction" }, reasonCodes: ["same_exact_key"] }),
]);
const diag = await buildMemoryDiagJSON(root);
const activeRow = diag.memories.find(memory => memory.id === "mem-active");
const pendingRow = diag.memories.find(memory => memory.id === "mem-pending");
const absorbedRow = diag.memories.find(memory => memory.id === "mem-absorbed");
assert.ok(activeRow);
assert.ok(pendingRow);
assert.ok(absorbedRow);
assert.deepEqual(activeRow.evidenceEventIds, [events[0].eventId]);
assert.ok(activeRow.reasonCodes.includes("stored_reason"));
assert.deepEqual(pendingRow.evidenceEventIds, [events[1].eventId]);
assert.ok(pendingRow.reasonCodes.includes("pending_reason"));
assert.deepEqual(absorbedRow.evidenceEventIds, [events[2].eventId]);
assert.ok(absorbedRow.reasonCodes.includes("same_exact_key"));
assert.ok(absorbedRow.reasonCodes.includes("absorbed_duplicate"));
} finally {
await rm(root, { recursive: true, force: true });
}
});
+1216 -26
View File
File diff suppressed because it is too large Load Diff
+122
View File
@@ -35,6 +35,18 @@ const acceptedCases = [
expectedType: "reference",
expectedText: /bracketless/,
},
{
name: "stable URL query reference",
line: "- [reference] Memory diagnostics dashboard URL is https://example.test/search?q=memory&view=summary",
expectedType: "reference",
expectedText: /search\?q=memory/,
},
{
name: "bilingual stable rule",
line: "- [decision] 使用 durable evidence records 保持 memory command auditability",
expectedType: "decision",
expectedText: /保持 memory command auditability/,
},
] as const;
const rejectedCases = [
@@ -78,6 +90,34 @@ const rejectedCases = [
name: "session internal review note",
line: "- [feedback] The assistant reviewed the code reviewer feedback and updated the plan",
},
{
name: "unresolved question suffix",
line: "- [project] Should we add semantic merge to workspace memory?",
},
{
name: "unresolved question prefix",
line: "- [reference] TODO: decide whether to keep this migration path",
},
{
name: "unresolved Chinese question",
line: "- [decision] 需要決定是否要增加新的記憶壓縮策略",
},
{
name: "transient bug state",
line: "- [project] Tests are failing and the next step is to fix the retry path",
},
{
name: "Chinese transient bug state",
line: "- [reference] 目前正在 debug storage lock failure,暫時 workaround 待修",
},
{
name: "deployment snapshot",
line: "- [reference] Latest deployed revision is rev-a8F3kL9pQ2xZ7bN4",
},
{
name: "Chinese deployment snapshot",
line: "- [project] 目前部署版本 build-9f8A7c6D5e4F3g2H 是 active release",
},
] as const;
for (const item of acceptedCases) {
@@ -125,6 +165,57 @@ test("progress snapshot rejection is type independent", () => {
}
});
test("new v1.6 hard quality reasons are emitted by concrete heuristics", () => {
const cases = [
{
reason: "unresolved_question",
entry: { type: "reference" as const, text: "Open question: whether to keep legacy prompt rendering", source: "compaction" as const },
},
{
reason: "unresolved_question",
entry: { type: "project" as const, text: "We need to decide storage migration order?", source: "compaction" as const },
},
{
reason: "transient_bug_state",
entry: { type: "project" as const, text: "Currently debugging memory replacement and tests are failing", source: "compaction" as const },
},
{
reason: "deployment_snapshot",
entry: { type: "reference" as const, text: "Current active release build is build-X9kLmN42pQ7rT6z", source: "compaction" as const },
},
];
for (const { reason, entry } of cases) {
const result = assessMemoryQuality(entry);
assert.equal(result.accepted, false, `${entry.text} should reject`);
assert.ok(result.reasons.includes(reason), `${entry.text} -> ${result.reasons.join(",")}`);
}
});
test("unresolved question guardrails preserve stable URL queries and durable rules", () => {
const urlResult = assessMemoryQuality({
type: "reference",
text: "Memory dashboard URL is https://example.test/search?q=memory&view=summary",
source: "compaction",
});
assert.equal(urlResult.reasons.includes("unresolved_question"), false, urlResult.reasons.join(","));
assert.equal(urlResult.accepted, true);
const durableRule = assessMemoryQuality({
type: "decision",
text: "Use verifier questions only when acceptance evidence is missing?",
source: "compaction",
});
assert.equal(durableRule.reasons.includes("unresolved_question"), false, durableRule.reasons.join(","));
});
test("terse_label is diagnostic only and does not block quality acceptance", () => {
const result = assessMemoryQuality({ type: "reference", text: "Cache key", source: "compaction" });
assert.equal(result.accepted, true);
assert.deepEqual(result.reasons, []);
assert.ok(result.diagnostics?.includes("terse_label"));
});
test("feedback must be stable user preference or instruction", () => {
assert.equal(assessMemoryQuality({ type: "feedback", text: "User prefers concise architecture reviews", source: "compaction" }).accepted, true);
assert.equal(assessMemoryQuality({ type: "feedback", text: "Implemented owner-aware cleanup in plugin.ts", source: "compaction" }).accepted, false);
@@ -136,6 +227,33 @@ test("decision must be future-facing rule, not completed implementation note", (
assert.equal(assessMemoryQuality({ type: "decision", text: "Added semantic merge tests in the previous wave", source: "compaction" }).accepted, false);
});
test("bad_decision 3-tier gate: architecture-like decisions accepted without future-rule imperative", () => {
const architectureLikeCases = [
{ text: "Rule 不在記憶系統 schema 內,歸用戶(agent.md / claude.md),系統最多到 Preference + Suggestion", type: "decision" as const },
{ text: "Ghost memory root cause: normalization 把 capacity losers 從 store 移除時沒有 emit terminal evidence", type: "decision" as const },
{ text: "BASE_HALF_LIFE_DAYS 應從 60 降低到 45", type: "decision" as const },
{ text: "採用 decay-rate 模型取代 priority+penalty 模型", type: "decision" as const },
{ text: "從 scoring 移除 confidence,目前是固定值無意義", type: "decision" as const },
];
const stillRejectedCases = [
{ text: "Implemented phase 2 and updated tests", type: "decision" as const },
{ text: "Implemented CI_SCHEMA_UPDATE for compatibility run 42", type: "decision" as const },
{ text: "Session reviewed the architecture model changes", type: "decision" as const },
{ text: "Some random text with no architecture keywords or future rules", type: "decision" as const },
];
for (const entry of architectureLikeCases) {
const result = assessMemoryQuality({ ...entry, source: "compaction" });
assert.equal(result.reasons.includes("bad_decision"), false, `${entry.text} -> ${result.reasons.join(",")}`);
}
for (const entry of stillRejectedCases) {
const result = assessMemoryQuality({ ...entry, source: "compaction" });
assert.equal(result.reasons.includes("bad_decision"), true, `${entry.text} -> ${result.reasons.join(",")}`);
}
});
test("shared quality gate owns extractor low-quality syntax rejections", () => {
const rejected = [
{ type: "project" as const, text: "fix: add new feature" },
@@ -169,7 +287,11 @@ test("hard quality reasons exclude soft whitelist failures", () => {
assert.equal(isHardQualityReason("code_or_api_signature"), true);
assert.equal(isHardQualityReason("path_heavy"), true);
assert.equal(isHardQualityReason("empty"), true);
assert.equal(isHardQualityReason("unresolved_question"), true);
assert.equal(isHardQualityReason("transient_bug_state"), true);
assert.equal(isHardQualityReason("deployment_snapshot"), true);
assert.equal(isHardQualityReason("bad_feedback"), false);
assert.equal(isHardQualityReason("bad_decision"), false);
assert.equal(isHardQualityReason("terse_label"), false);
});
+220
View File
@@ -0,0 +1,220 @@
import test from "node:test";
import assert from "node:assert/strict";
import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
import { dirname, join } from "node:path";
import { tmpdir } from "node:os";
import { appendPendingMemories } from "../src/pending-journal.ts";
import { saveSessionState } from "../src/session-state.ts";
import { MEMORY_TYPE_ORDER } from "../src/memory-kind-policy.ts";
import type { LongTermMemoryEntry, WorkspaceMemoryStore } from "../src/types.ts";
import { workspaceMemoryPath } from "../src/paths.ts";
import { saveWorkspaceMemory } from "../src/workspace-memory.ts";
import {
formatMemoryHelp,
formatMemoryList,
formatMemoryStatus,
getMemoryList,
getMemoryStatus,
renderMemoryCommand,
} from "../src/memory-visibility.ts";
async function tempRoot(): Promise<string> {
return mkdtemp(join(tmpdir(), "memory-visibility-test-"));
}
function memory(id: string, text: string, overrides: Partial<LongTermMemoryEntry> = {}): LongTermMemoryEntry {
const now = new Date().toISOString();
return {
id,
type: "decision",
text,
source: "compaction",
confidence: 0.8,
status: "active",
createdAt: now,
updatedAt: now,
...overrides,
};
}
test("formats status counts from workspace, session, and pending journal stores", async () => {
const root = await tempRoot();
try {
const now = new Date().toISOString();
const store: WorkspaceMemoryStore = {
version: 1,
workspace: { root, key: "test" },
limits: { maxRenderedChars: 115, maxEntries: 28 },
entries: [
memory("mem-short", "Keep tests focused."),
memory("mem-long", "Long memory with password: sushi ".repeat(20), { type: "reference" }),
memory("mem-old", "Superseded memory should not be active.", { status: "superseded" }),
],
migrations: [],
updatedAt: now,
};
await saveWorkspaceMemory(root, store);
await saveSessionState(root, {
version: 1,
sessionID: "ses_status",
turn: 1,
updatedAt: now,
activeFiles: [],
openErrors: [{
id: "err-1",
category: "typecheck",
summary: "Typecheck failed",
fingerprint: "typecheck",
status: "open",
firstSeen: Date.now(),
lastSeen: Date.now(),
seenCount: 1,
}],
recentDecisions: [{ id: "dec-1", text: "Prefer local rendering", source: "user", createdAt: Date.now() }],
pendingMemories: [memory("pending-session", "Pending for this session", { source: "explicit", pendingOwnerSessionID: "ses_status" })],
compactionMemoryRefs: [],
});
await appendPendingMemories(root, [memory("pending-journal", "Pending in durable journal", { source: "explicit", pendingOwnerSessionID: "ses_status" })]);
const output = formatMemoryStatus(await getMemoryStatus(root, "ses_status"));
assert.match(output, /^## Memory status/);
assert.match(output, /Workspace:/);
assert.match(output, /- Active memories: 2/);
assert.match(output, /- Rendered in prompt: 1/);
assert.match(output, /- Omitted active memories: 1/);
assert.match(output, /- Superseded memories: 1/);
assert.match(output, /Pending:/);
assert.match(output, /- Pending in this session: 1/);
assert.match(output, /- Pending journal memories: 1/);
assert.match(output, /Session:/);
assert.match(output, /- Open errors: 1/);
assert.match(output, /- Recent decisions: 1/);
assert.match(output, /Use \/memory → Current memories to browse current \[M1\]-\[M28\] memory refs\./);
assert.match(output, /Local only: no LLM request was made\./);
assert.equal(output.includes("Recent active memory previews"), false);
assert.equal(output.includes("sushi"), false, "status output should not include memory previews");
} finally {
await rm(root, { recursive: true, force: true });
}
});
test("getMemoryStatus redacts previews without rewriting workspace memory", async () => {
const root = await tempRoot();
try {
const now = new Date().toISOString();
const path = await workspaceMemoryPath(root);
await mkdir(dirname(path), { recursive: true });
const store: WorkspaceMemoryStore = {
version: 1,
workspace: { root, key: "test" },
limits: { maxRenderedChars: 3600, maxEntries: 28 },
entries: [memory("mem-secret", "Remember password: sushi for the fake test fixture.", { createdAt: now, updatedAt: now })],
migrations: [],
updatedAt: now,
};
const before = JSON.stringify(store, null, 2);
await writeFile(path, before, "utf8");
const output = formatMemoryStatus(await getMemoryStatus(root, "ses_readonly"));
const after = await readFile(path, "utf8");
assert.match(output, /- Active memories: 1/);
assert.equal(output.includes("Recent active memory previews"), false);
assert.equal(output.includes("sushi"), false, "status output should not include memory previews");
assert.equal(after, before, "status display must not persist normalization, migration, or redaction changes");
} finally {
await rm(root, { recursive: true, force: true });
}
});
test("formats current workspace memories grouped by type with display-local refs", async () => {
const root = await tempRoot();
try {
const now = new Date().toISOString();
await saveWorkspaceMemory(root, {
version: 1,
workspace: { root, key: "test" },
limits: { maxRenderedChars: 3600, maxEntries: 28 },
entries: [
memory("mem-feedback", "Remember password: sushi for the fake test.", { type: "feedback" }),
memory("mem-project", "Project memory should render in its own group.", { type: "project" }),
memory("mem-decision", "Decision memory should render in its own group.", { type: "decision" }),
memory("mem-reference", "Reference memory should render in its own group.", { type: "reference" }),
memory("mem-superseded", "Superseded memory should not be active", { type: "reference", status: "superseded" }),
],
migrations: [],
updatedAt: now,
});
const output = formatMemoryList(await getMemoryList(root));
assert.match(output, /^## Current workspace memories/);
assert.match(output, /Display refs are local to this output/);
assert.match(output, /feedback:\n- \[M\d+\]/);
assert.match(output, /project:\n- \[M\d+\]/);
assert.match(output, /decision:\n- \[M\d+\]/);
assert.match(output, /reference:\n- \[M\d+\]/);
const groupIndexes = MEMORY_TYPE_ORDER.map(type => output.indexOf(`${type}:`));
assert.equal(groupIndexes.every(index => index >= 0), true, "all memory type groups should render");
assert.deepEqual(groupIndexes, [...groupIndexes].sort((a, b) => a - b), "memory list groups should follow shared memory type order");
assert.match(output, /Shown: \d+ of \d+ active memories\./);
assert.match(output, /Shown: 4 of 4 active memories\./);
assert.match(output, /Omitted active memories: 0\./);
assert.equal(output.includes("[M1]"), true, "at least one display-local ref should render");
assert.equal(output.includes("sushi"), false, "list previews should redact credential-like text");
assert.equal(output.includes("Superseded memory should not be active"), false);
} finally {
await rm(root, { recursive: true, force: true });
}
});
test("formats empty memory list state", () => {
const output = formatMemoryList({
activeMemories: 0,
renderedMemories: 0,
omittedActiveMemories: 0,
groups: { feedback: [], project: [], decision: [], reference: [] },
});
assert.match(output, /^## Current workspace memories/);
assert.match(output, /No active workspace memories are stored yet\./);
assert.match(output, /Local only: no LLM request was made\./);
assert.equal(output.includes("feedback:"), false);
});
test("formats help text for available display commands", () => {
const output = formatMemoryHelp();
assert.match(output, /^## Memory help/);
assert.match(output, /\/memory — open the local memory menu\./);
assert.match(output, /Status — show local memory statistics\./);
assert.match(output, /Current memories — browse active workspace memories as display-local \[M1\]-\[M28\] refs\./);
assert.match(output, /Help — show this help\./);
for (const removedCommand of ["/memory-" + "status", "/memory-" + "list", "/memory-" + "help"]) {
assert.equal(output.includes(removedCommand), false);
}
assert.equal(output.includes("/memory activity"), false);
assert.equal(output.includes("/memory last"), false);
assert.equal(output.includes("/memory status"), false);
assert.equal(output.includes("/memory help"), false);
assert.match(output, /do not call the LLM/);
});
test("renderMemoryCommand routes list output", async () => {
const root = await tempRoot();
try {
const output = await renderMemoryCommand(root, "ses_list", "list");
assert.match(output, /^## Current workspace memories/);
} finally {
await rm(root, { recursive: true, force: true });
}
});
test("renderMemoryCommand falls back to help for unknown command values", async () => {
const root = await tempRoot();
try {
const output = await renderMemoryCommand(root, "ses_unknown", "unknown" as never);
assert.match(output, /^## Memory help/);
} finally {
await rm(root, { recursive: true, force: true });
}
});
+38
View File
@@ -0,0 +1,38 @@
import test from "node:test";
import assert from "node:assert/strict";
import {
formatPackageVersionMismatch,
packageLockReadErrorMessage,
packageVersionMismatches,
} from "../scripts/dev/check-package-integrity.ts";
test("package integrity accepts matching package and lockfile versions", () => {
const mismatches = packageVersionMismatches(
{ version: "1.6.4" },
{ version: "1.6.4", packages: { "": { version: "1.6.4" } } },
);
assert.deepEqual(mismatches, []);
});
test("package integrity reports both lockfile version mismatches", () => {
const mismatches = packageVersionMismatches(
{ version: "1.6.4" },
{ version: "1.6.3", packages: { "": { version: "1.6.2" } } },
);
assert.deepEqual(
mismatches.map(formatPackageVersionMismatch),
[
"package-lock.json version (1.6.3) does not match package.json version (1.6.4)",
"package-lock.json packages[\"\"].version (1.6.2) does not match package.json version (1.6.4)",
],
);
});
test("package integrity explains missing package-lock.json", () => {
assert.equal(
packageLockReadErrorMessage(Object.assign(new Error("missing"), { code: "ENOENT" })),
"package-lock.json not found; run npm install first",
);
});
+1412 -8
View File
File diff suppressed because it is too large Load Diff
+55 -1
View File
@@ -1,7 +1,7 @@
import test from "node:test";
import assert from "node:assert/strict";
import type { LongTermMemoryEntry } from "../src/types.ts";
import { accountPendingPromotions } from "../src/promotion-accounting.ts";
import { accountPendingPromotions, promotionAccountingEvidenceEvents } from "../src/promotion-accounting.ts";
import { memoryKey } from "../src/pending-journal.ts";
import type { MemoryConsolidationEvent } from "../src/workspace-memory.ts";
import { workspaceMemoryExactKey, workspaceMemoryIdentityKey } from "../src/workspace-memory.ts";
@@ -229,3 +229,57 @@ test("accountPendingPromotions marks manual capacity rejection as retryable", ()
assert.equal(result.clearableKeys.size, 0);
assert.deepEqual([...result.retryableRejectedKeys], [memoryKey(pending[0])]);
});
test("promotionAccountingEvidenceEvents maps every promotion outcome with relations", () => {
const promoted = mem("promoted", "Promoted memory should produce evidence.", { source: "explicit" });
const absorbed = mem("absorbed", "Absorbed memory should produce evidence.", { source: "explicit" });
const retained = mem("retained", "absorbed memory should produce evidence.", { source: "explicit" });
const identityAbsorbed = mem("identity-absorbed", "Project config lives in `src/config.ts`", { type: "reference" });
const identityRetained = mem("identity-retained", "Project config lives in `./src/config.ts`", { type: "reference" });
const superseded = mem("superseded", "Parser supports 3 formats.", { source: "compaction" });
const replacement = mem("replacement", "Parser supports 4 formats.", { source: "compaction" });
const capacity = mem("capacity", "Capacity rejected explicit memory should retry.", { source: "explicit", type: "reference" });
const exhausted = mem("exhausted", "Exhausted explicit memory should stop retrying.", { source: "explicit", type: "reference" });
const pending = [promoted, absorbed, identityAbsorbed, superseded, capacity, exhausted];
const accounting = {
promotedKeys: new Set([memoryKey(promoted)]),
absorbedKeys: new Set([memoryKey(absorbed), memoryKey(identityAbsorbed)]),
supersededKeys: new Set([memoryKey(superseded)]),
rejectedKeys: new Set([memoryKey(capacity), memoryKey(exhausted)]),
retryableRejectedKeys: new Set([memoryKey(capacity), memoryKey(exhausted)]),
clearableKeys: new Set([memoryKey(promoted), memoryKey(absorbed), memoryKey(identityAbsorbed), memoryKey(superseded), memoryKey(exhausted)]),
};
const events = [
{ ...event(absorbed, "absorbed_exact"), retainedId: retained.id },
{ ...event(identityAbsorbed, "absorbed_identity"), retainedId: identityRetained.id },
{ ...event(superseded, "superseded_existing"), retainedId: replacement.id, supersededId: superseded.id },
event(capacity, "rejected_capacity"),
event(exhausted, "rejected_capacity"),
];
const evidence = promotionAccountingEvidenceEvents({
pending,
after: [promoted, retained, identityRetained, replacement],
events,
accounting,
exhaustedRejectedKeys: new Set([memoryKey(exhausted)]),
});
const expectedPromotionEventTypes = new Set([
"promotion_promoted",
"promotion_absorbed_exact",
"promotion_absorbed_identity",
"promotion_superseded",
"promotion_rejected_capacity",
"promotion_retry_scheduled",
"promotion_retry_exhausted",
]);
assert.deepEqual(new Set(evidence.map(event => event.type)), expectedPromotionEventTypes);
const absorbedEvent = evidence.find(event => event.type === "promotion_absorbed_exact");
assert.ok(absorbedEvent?.relations?.some(relation => relation.role === "absorbed" && relation.memory?.memoryId === absorbed.id));
assert.ok(absorbedEvent?.relations?.some(relation => relation.role === "retained" && relation.memory?.memoryId === retained.id));
const supersededEvent = evidence.find(event => event.type === "promotion_superseded");
assert.ok(supersededEvent?.relations?.some(relation => relation.role === "superseded" && relation.memory?.memoryId === superseded.id));
assert.ok(supersededEvent?.relations?.some(relation => relation.role === "superseded_by" && relation.memory?.memoryId === replacement.id));
});
+211
View File
@@ -0,0 +1,211 @@
import test from "node:test";
import assert from "node:assert/strict";
import {
BASE_HALF_LIFE_DAYS,
REINFORCEMENT_MAX_COUNT,
tryReinforceMemory,
} from "../src/retention.ts";
import type { LongTermMemoryEntry } from "../src/types.ts";
const DAY_MS = 24 * 60 * 60 * 1000;
const ROLLING_WINDOW_MS = 7 * DAY_MS;
const baseMemory = (overrides: Partial<LongTermMemoryEntry> = {}): LongTermMemoryEntry => ({
id: "mem-retention",
type: "decision",
text: "Durable decision for reinforcement",
source: "compaction",
confidence: 0.75,
status: "active",
createdAt: "2026-05-10T00:00:00.000Z",
updatedAt: "2026-05-10T00:00:00.000Z",
...overrides,
});
test("tryReinforceMemory allows same session after rolling 8 days", () => {
const lastAt = Date.UTC(2026, 4, 8, 12, 0, 0);
const now = lastAt + 8 * DAY_MS;
const memory = baseMemory({
reinforcementCount: 1,
lastReinforcedAt: lastAt,
lastReinforcedSessionID: "session-a",
retentionClock: lastAt,
});
const decision = tryReinforceMemory(memory, "session-a", now);
assert.equal(decision.outcome, "reinforced");
assert.equal(decision.reinforcementMode, "increment");
assert.equal(decision.previousReinforcementCount, 1);
assert.equal(decision.newReinforcementCount, 2);
assert.equal(decision.memory.reinforcementCount, 2);
assert.equal(decision.memory.retentionClock, now);
assert.equal(decision.memory.lastReinforcedAt, now);
assert.equal(decision.memory.lastReinforcedSessionID, "session-a");
assert.equal(decision.sameSession, true);
assert.equal(decision.elapsedMs, 8 * DAY_MS);
assert.equal(decision.requiredElapsedMs, ROLLING_WINDOW_MS);
});
test("tryReinforceMemory allows exactly 7 rolling days after last reinforcement", () => {
const lastAt = Date.UTC(2026, 4, 8, 12, 0, 0);
const now = lastAt + ROLLING_WINDOW_MS;
const memory = baseMemory({
reinforcementCount: 5,
lastReinforcedAt: lastAt,
lastReinforcedSessionID: "session-a",
retentionClock: lastAt,
});
const decision = tryReinforceMemory(memory, "session-b", now);
assert.equal(decision.outcome, "reinforced");
assert.equal(decision.reinforcementMode, "increment");
assert.equal(decision.memory.reinforcementCount, 6);
assert.equal(decision.memory.retentionClock, now);
assert.equal(decision.memory.lastReinforcedAt, now);
assert.equal(decision.memory.lastReinforcedSessionID, "session-b");
assert.equal(decision.sameSession, false);
assert.equal(decision.elapsedMs, ROLLING_WINDOW_MS);
assert.equal(decision.requiredElapsedMs, ROLLING_WINDOW_MS);
});
test("tryReinforceMemory blocks 7 rolling days minus 1ms as min_elapsed_window", () => {
const lastAt = Date.UTC(2026, 4, 8, 12, 0, 0);
const now = lastAt + ROLLING_WINDOW_MS - 1;
const memory = baseMemory({
reinforcementCount: 1,
lastReinforcedAt: lastAt,
lastReinforcedSessionID: "session-a",
});
const decision = tryReinforceMemory(memory, "session-a", now);
assert.equal(decision.outcome, "blocked");
assert.equal(decision.blockReason, "min_elapsed_window");
assert.equal(decision.memory, memory);
assert.equal(decision.sameSession, true);
assert.equal(decision.lastReinforcedAt, lastAt);
assert.equal(decision.elapsedMs, ROLLING_WINDOW_MS - 1);
assert.equal(decision.requiredElapsedMs, ROLLING_WINDOW_MS);
});
test("tryReinforceMemory blocks different session below rolling window", () => {
const lastAt = Date.UTC(2026, 4, 8, 12, 0, 0);
const now = lastAt + 3 * DAY_MS;
const memory = baseMemory({
reinforcementCount: 1,
lastReinforcedAt: lastAt,
lastReinforcedSessionID: "session-a",
});
const decision = tryReinforceMemory(memory, "session-b", now);
assert.equal(decision.outcome, "blocked");
assert.equal(decision.blockReason, "min_elapsed_window");
assert.equal(decision.sameSession, false);
assert.equal(decision.elapsedMs, 3 * DAY_MS);
assert.equal(decision.requiredElapsedMs, ROLLING_WINDOW_MS);
});
test("tryReinforceMemory blocks UTC midnight crossing below rolling window", () => {
const lastAt = Date.UTC(2026, 4, 12, 23, 45, 0);
const now = Date.UTC(2026, 4, 13, 2, 15, 0);
const memory = baseMemory({
reinforcementCount: 1,
lastReinforcedAt: lastAt,
lastReinforcedSessionID: "session-a",
});
const decision = tryReinforceMemory(memory, "session-b", now);
assert.equal(decision.outcome, "blocked");
assert.equal(decision.blockReason, "min_elapsed_window");
assert.notEqual(decision.blockReason, "same_utc_day");
assert.equal(decision.elapsedMs, now - lastAt);
assert.equal(decision.requiredElapsedMs, ROLLING_WINDOW_MS);
});
test("tryReinforceMemory refreshes saturated count after rolling window", () => {
const lastAt = Date.UTC(2026, 4, 8, 12, 0, 0);
const now = lastAt + ROLLING_WINDOW_MS;
const memory = baseMemory({
reinforcementCount: REINFORCEMENT_MAX_COUNT,
lastReinforcedAt: lastAt,
lastReinforcedSessionID: "session-a",
retentionClock: lastAt,
});
const decision = tryReinforceMemory(memory, "session-b", now);
assert.equal(decision.outcome, "reinforced");
assert.equal(decision.reinforcementMode, "refresh_only");
assert.equal(decision.previousReinforcementCount, REINFORCEMENT_MAX_COUNT);
assert.equal(decision.newReinforcementCount, REINFORCEMENT_MAX_COUNT);
assert.notEqual(decision.memory, memory);
assert.equal(decision.memory.reinforcementCount, REINFORCEMENT_MAX_COUNT);
assert.equal(decision.memory.lastReinforcedAt, now);
assert.equal(decision.memory.lastReinforcedSessionID, "session-b");
assert.equal(decision.memory.retentionClock, now);
assert.equal(decision.elapsedMs, ROLLING_WINDOW_MS);
assert.equal(decision.requiredElapsedMs, ROLLING_WINDOW_MS);
});
test("tryReinforceMemory blocks saturated count below rolling window as min_elapsed_window", () => {
const lastAt = Date.UTC(2026, 4, 8, 12, 0, 0);
const now = lastAt + ROLLING_WINDOW_MS - 1;
const memory = baseMemory({
reinforcementCount: REINFORCEMENT_MAX_COUNT,
lastReinforcedAt: lastAt,
lastReinforcedSessionID: "session-a",
});
const decision = tryReinforceMemory(memory, "session-b", now);
assert.equal(decision.outcome, "blocked");
assert.equal(decision.blockReason, "min_elapsed_window");
assert.notEqual(decision.blockReason, "max_count");
assert.equal(decision.reinforcementCount, REINFORCEMENT_MAX_COUNT);
assert.equal(decision.maxReinforcementCount, REINFORCEMENT_MAX_COUNT);
assert.equal(decision.requiredElapsedMs, ROLLING_WINDOW_MS);
});
test("tryReinforceMemory normalizes missing legacy timestamp while incrementing", () => {
const now = Date.UTC(2026, 4, 12, 12, 0, 0);
const memory = baseMemory({
reinforcementCount: 2,
lastReinforcedSessionID: "session-a",
});
const decision = tryReinforceMemory(memory, "session-b", now);
assert.equal(decision.outcome, "reinforced");
assert.equal(decision.reinforcementMode, "increment");
assert.equal(decision.legacyMissingTimestamp, true);
assert.equal(decision.memory.reinforcementCount, 3);
assert.equal(decision.memory.lastReinforcedAt, now);
assert.equal(decision.memory.retentionClock, now);
});
test("tryReinforceMemory normalizes invalid legacy timestamp while refresh-only saturated", () => {
const now = Date.UTC(2026, 4, 12, 12, 0, 0);
const memory = baseMemory({
reinforcementCount: REINFORCEMENT_MAX_COUNT,
lastReinforcedAt: Number.NaN,
lastReinforcedSessionID: "session-a",
retentionClock: Date.UTC(2026, 4, 8, 12, 0, 0),
});
const decision = tryReinforceMemory(memory, "session-b", now);
assert.equal(decision.outcome, "reinforced");
assert.equal(decision.reinforcementMode, "refresh_only");
assert.equal(decision.legacyMissingTimestamp, true);
assert.equal(decision.memory.reinforcementCount, REINFORCEMENT_MAX_COUNT);
assert.equal(decision.memory.lastReinforcedAt, now);
assert.equal(decision.memory.retentionClock, now);
});
test("BASE_HALF_LIFE_DAYS remains 45", () => {
assert.equal(BASE_HALF_LIFE_DAYS, 45);
});
+363
View File
@@ -0,0 +1,363 @@
import test from "node:test";
import assert from "node:assert/strict";
import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { dirname, join } from "node:path";
import { MemoryV2Plugin } from "../src/plugin.ts";
import * as sessionStateModule from "../src/session-state.ts";
import type { HotSessionStateRenderAccounting } from "../src/session-state.ts";
import { sessionStatePath, workspaceMemoryPath } from "../src/paths.ts";
import type { ActiveFile, CompactionMemoryRef, LongTermMemoryEntry, OpenError, SessionDecision, SessionState } from "../src/types.ts";
import { HOT_STATE_LIMITS, LONG_TERM_LIMITS } from "../src/types.ts";
type AccountHotSessionStateRender = (state: SessionState, workspaceRoot: string) => HotSessionStateRenderAccounting;
const accountHotSessionStateRender = (
sessionStateModule as typeof sessionStateModule & { accountHotSessionStateRender: AccountHotSessionStateRender }
).accountHotSessionStateRender;
const { createEmptySessionState, loadSessionState, renderHotSessionState, saveSessionState } = sessionStateModule;
const root = "/repo";
function state(overrides: Partial<SessionState> = {}): SessionState {
return {
version: 1,
sessionID: "session-state-test",
turn: 0,
updatedAt: "2026-05-05T00:00:00.000Z",
activeFiles: [],
openErrors: [],
recentDecisions: [],
pendingMemories: [],
compactionMemoryRefs: [],
...overrides,
};
}
function compactionRef(index: number, overrides: Partial<CompactionMemoryRef> = {}): CompactionMemoryRef {
return {
ref: `M${index}`,
memoryId: `memory-${index}`,
type: "decision",
source: "compaction",
exactKey: `decision:durable fact ${index}`,
identityKey: `decision:durable fact ${index}`,
textPreview: `Durable fact ${index}`,
capturedAt: 1_777_000_000_000 + index,
...overrides,
};
}
function mockRootClient(summary = "") {
return {
session: {
get: async () => ({ data: { parentID: null } }),
messages: async () => ({
data: summary
? [{ info: { role: "assistant", summary: true }, parts: [{ type: "text", text: summary }] }]
: [],
}),
todo: async () => ({ data: [] }),
},
};
}
function activeFile(path: string, action: ActiveFile["action"], count: number, lastSeen: number): ActiveFile {
return { path, action, count, lastSeen };
}
function openError(id: string, summary: string, lastSeen: number): OpenError {
return {
id,
category: "test",
summary,
fingerprint: `fingerprint-${id}`,
status: "open",
firstSeen: lastSeen - 1,
lastSeen,
seenCount: 1,
};
}
function decision(id: string, text: string, createdAt: number): SessionDecision {
return { id, text, source: "assistant", createdAt };
}
function memory(id: string, text: string, type: LongTermMemoryEntry["type"] = "decision"): LongTermMemoryEntry {
return {
id,
type,
text,
source: "compaction",
confidence: 0.75,
status: "active",
createdAt: "2026-05-05T00:00:00.000Z",
updatedAt: "2026-05-05T00:00:00.000Z",
};
}
test("accountHotSessionStateRender returns empty prompt and no omissions for empty state", () => {
const accounting = accountHotSessionStateRender(createEmptySessionState("empty-session"), root);
assert.equal(accounting.prompt, "");
assert.deepEqual(accounting.omitted, []);
assert.equal(accounting.maxRenderedChars, HOT_STATE_LIMITS.maxRenderedChars);
});
test("accountHotSessionStateRender renders hot-state sections in stable order", () => {
const accounting = accountHotSessionStateRender(state({
activeFiles: [activeFile("/repo/src/a.ts", "read", 1, 1)],
openErrors: [openError("err-1", "one failing test", 2)],
recentDecisions: [decision("dec-1", "Keep renderer simple", 3)],
pendingMemories: [memory("mem-1", "Promote useful fact")],
}), root);
assert.ok(accounting.prompt.startsWith("Hot session state (current session):"));
assert.ok(accounting.prompt.indexOf("active_files:") < accounting.prompt.indexOf("open_errors:"));
assert.ok(accounting.prompt.indexOf("open_errors:") < accounting.prompt.indexOf("recent_decisions:"));
assert.ok(accounting.prompt.indexOf("recent_decisions:") < accounting.prompt.indexOf("pending_memories:"));
});
test("accountHotSessionStateRender ranks active files by score then lastSeen descending", () => {
const accounting = accountHotSessionStateRender(state({
activeFiles: [
activeFile("/repo/src/edit.ts", "edit", 1, 400),
activeFile("/repo/src/write.ts", "write", 5, 200),
activeFile("/repo/src/grep.ts", "grep", 10, 300),
activeFile("/repo/src/read.ts", "read", 12, 100),
],
}), root);
const lines = accounting.prompt.split("\n");
const activeLines = lines.filter(line => line.startsWith("- src/"));
assert.deepEqual(activeLines, [
"- src/grep.ts (grep, 10x)",
"- src/write.ts (write, 5x)",
"- src/read.ts (read, 12x)",
"- src/edit.ts (edit, 1x)",
]);
});
test("accountHotSessionStateRender reports section-cap omissions for every capped section", () => {
const accounting = accountHotSessionStateRender(state({
activeFiles: Array.from({ length: 9 }, (_, index) => activeFile(`/repo/a${index}.ts`, "read", 1, 100 - index)),
openErrors: Array.from({ length: 4 }, (_, index) => openError(`err-${index}`, `e${index}`, 100 - index)),
recentDecisions: Array.from({ length: 9 }, (_, index) => decision(`dec-${index}`, `d${index}`, index)),
pendingMemories: Array.from({ length: 7 }, (_, index) => memory(`mem-${index}`, `m${index}`)),
}), root);
const sectionCapOmissions = accounting.omitted.filter(item => item.reason === "section_cap");
assert.deepEqual(
sectionCapOmissions.map(item => item.section).sort(),
["active_files", "open_errors", "pending_memories", "recent_decisions"].sort(),
);
assert.equal(sectionCapOmissions.find(item => item.section === "pending_memories")?.memoryId, "mem-0");
});
test("accountHotSessionStateRender omits over-budget entries without cutting rendered lines", () => {
const longPath = `/repo/${"x".repeat(650)}.ts`;
const accounting = accountHotSessionStateRender(state({
activeFiles: [
activeFile("/repo/src/short.ts", "read", 1, 20),
activeFile(longPath, "read", 1, 10),
],
}), root);
assert.equal(accounting.prompt, [
"Hot session state (current session):",
"active_files:",
"- src/short.ts (read, 1x)",
].join("\n"));
assert.equal(accounting.omitted.length, 1);
assert.equal(accounting.omitted[0]?.reason, "char_budget");
assert.equal(accounting.omitted[0]?.section, "active_files");
assert.ok(!accounting.prompt.includes("x".repeat(20)));
});
test("accountHotSessionStateRender includes exact 700-char prompt but omits one additional character", () => {
const fixedPrompt = [
"Hot session state (current session):",
"pending_memories:",
"- [decision] ",
].join("\n");
const exactText = "x".repeat(HOT_STATE_LIMITS.maxRenderedChars - fixedPrompt.length);
const exactAccounting = accountHotSessionStateRender(state({
pendingMemories: [memory("mem-exact", exactText)],
}), root);
assert.equal(exactAccounting.prompt.length, HOT_STATE_LIMITS.maxRenderedChars);
assert.equal(exactAccounting.omitted.length, 0);
const overAccounting = accountHotSessionStateRender(state({
pendingMemories: [memory("mem-over", `${exactText}y`)],
}), root);
assert.equal(overAccounting.prompt, "");
assert.equal(overAccounting.omitted.length, 1);
assert.equal(overAccounting.omitted[0]?.reason, "char_budget");
assert.equal(overAccounting.omitted[0]?.memoryId, "mem-over");
});
test("accountHotSessionStateRender suppresses header-only sections when no entries fit", () => {
const accounting = accountHotSessionStateRender(state({
activeFiles: [activeFile(`/repo/${"z".repeat(720)}.ts`, "read", 1, 1)],
}), root);
assert.equal(accounting.prompt, "");
assert.equal(accounting.omitted.length, 1);
assert.equal(accounting.omitted[0]?.reason, "char_budget");
assert.ok(!accounting.prompt.includes("active_files:"));
});
test("renderHotSessionState delegates to accounted renderer prompt for empty and seeded states", () => {
const empty = createEmptySessionState("compat-empty");
const seeded = state({
activeFiles: [activeFile("/repo/src/a.ts", "edit", 2, 1)],
pendingMemories: [memory("mem-compat", "Compatibility prompt")],
});
assert.equal(renderHotSessionState(empty, root), accountHotSessionStateRender(empty, root).prompt);
assert.equal(renderHotSessionState(seeded, root), accountHotSessionStateRender(seeded, root).prompt);
});
test("accountHotSessionStateRender counts newline separators in the 700-char budget", () => {
const fixedPrompt = [
"Hot session state (current session):",
"recent_decisions:",
"- ",
].join("\n");
const exactText = "n".repeat(HOT_STATE_LIMITS.maxRenderedChars - fixedPrompt.length);
const exactAccounting = accountHotSessionStateRender(state({
recentDecisions: [decision("dec-exact", exactText, 1)],
}), root);
assert.equal(exactAccounting.prompt.length, HOT_STATE_LIMITS.maxRenderedChars);
assert.equal(exactAccounting.omitted.length, 0);
const overAccounting = accountHotSessionStateRender(state({
recentDecisions: [decision("dec-over", `${exactText}!`, 1)],
}), root);
assert.equal(overAccounting.prompt, "");
assert.equal(overAccounting.omitted.length, 1);
assert.equal(overAccounting.omitted[0]?.reason, "char_budget");
});
test("compaction memory refs round-trip through session state and are capped", async () => {
const tmpDir = await mkdtemp(join(tmpdir(), "memory-session-state-"));
try {
const refs = Array.from({ length: LONG_TERM_LIMITS.maxEntries + 2 }, (_, index) => compactionRef(
index + 1,
index === 0 ? { compactionId: "compaction-snapshot-1" } : {},
));
await saveSessionState(tmpDir, state({
sessionID: "compaction-ref-roundtrip",
compactionMemoryRefs: refs,
}));
const loaded = await loadSessionState(tmpDir, "compaction-ref-roundtrip");
assert.equal(loaded.compactionMemoryRefs.length, LONG_TERM_LIMITS.maxEntries);
assert.deepEqual(loaded.compactionMemoryRefs, refs.slice(0, LONG_TERM_LIMITS.maxEntries));
} finally {
await rm(tmpDir, { recursive: true, force: true });
}
});
test("invalid stored compaction memory refs normalize to empty without crashing", async () => {
const tmpDir = await mkdtemp(join(tmpdir(), "memory-session-state-"));
try {
const path = await sessionStatePath(tmpDir, "invalid-compaction-ref-session");
await mkdir(dirname(path), { recursive: true });
await writeFile(path, JSON.stringify({
version: 1,
sessionID: "stale-id",
turn: 0,
updatedAt: "2026-05-05T00:00:00.000Z",
activeFiles: [],
openErrors: [],
recentDecisions: [],
pendingMemories: [],
compactionMemoryRefs: [
compactionRef(1),
{ ...compactionRef(2), ref: "not-a-numbered-ref" },
],
}), "utf8");
const loaded = await loadSessionState(tmpDir, "invalid-compaction-ref-session");
assert.equal(loaded.sessionID, "invalid-compaction-ref-session");
assert.deepEqual(loaded.compactionMemoryRefs, []);
} finally {
await rm(tmpDir, { recursive: true, force: true });
}
});
test("compaction memory refs never render in hot session state", () => {
const rendered = renderHotSessionState(state({
activeFiles: [activeFile("/repo/src/a.ts", "read", 1, 1)],
compactionMemoryRefs: [compactionRef(1, {
memoryId: "secret-memory-id",
exactKey: "decision:secret exact key",
identityKey: "decision:secret identity key",
textPreview: "Secret compaction ref preview must not render",
})],
}), root);
assert.match(rendered, /active_files:/);
assert.doesNotMatch(rendered, /Secret compaction ref preview/);
assert.doesNotMatch(rendered, /secret-memory-id/);
assert.doesNotMatch(rendered, /secret exact key/);
assert.doesNotMatch(rendered, /secret identity key/);
assert.doesNotMatch(rendered, /\bM1\b/);
});
test("session.compacted clears compaction memory refs after processing", async () => {
const tmpDir = await mkdtemp(join(tmpdir(), "memory-session-state-"));
try {
await saveSessionState(tmpDir, state({
sessionID: "clear-compaction-refs-session",
compactionMemoryRefs: [compactionRef(1)],
}));
const plugin = await MemoryV2Plugin({ directory: tmpDir, client: mockRootClient() });
await (plugin as Record<string, Function>)["event"]({
event: { type: "session.compacted", properties: { sessionID: "clear-compaction-refs-session" } },
});
const loaded = await loadSessionState(tmpDir, "clear-compaction-refs-session");
assert.deepEqual(loaded.compactionMemoryRefs, []);
} finally {
await rm(tmpDir, { recursive: true, force: true });
}
});
test("session.compacted clears compaction memory refs even when promotion fails", async () => {
const tmpDir = await mkdtemp(join(tmpdir(), "memory-session-state-"));
try {
await saveSessionState(tmpDir, state({
sessionID: "clear-compaction-refs-failure-session",
pendingMemories: [memory("mem-pending-failure", "Keep pending memory when promotion fails")],
compactionMemoryRefs: [compactionRef(1)],
}));
const workspacePath = await workspaceMemoryPath(tmpDir);
await rm(workspacePath, { force: true }).catch(() => undefined);
await mkdir(workspacePath, { recursive: true });
const plugin = await MemoryV2Plugin({ directory: tmpDir, client: mockRootClient() });
await (plugin as Record<string, Function>)["event"]({
event: { type: "session.compacted", properties: { sessionID: "clear-compaction-refs-failure-session" } },
});
const loaded = await loadSessionState(tmpDir, "clear-compaction-refs-failure-session");
assert.deepEqual(loaded.compactionMemoryRefs, []);
assert.equal(loaded.pendingMemories.length, 1,
"unrelated retryable pending memory should remain on promotion failure");
assert.equal(loaded.pendingMemories[0].id, "mem-pending-failure");
} finally {
await rm(tmpDir, { recursive: true, force: true });
}
});
+51
View File
@@ -0,0 +1,51 @@
import test from "node:test";
import assert from "node:assert/strict";
import { execFile } from "node:child_process";
import { mkdir, mkdtemp, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { promisify } from "node:util";
const execFileAsync = promisify(execFile);
const repoRoot = join(dirname(fileURLToPath(import.meta.url)), "../..");
const maxBuffer = 10 * 1024 * 1024;
function executable(name: "npm" | "npx"): string {
return process.platform === "win32" ? `${name}.cmd` : name;
}
test("packed memory-diag bin runs from a temp consumer project", async () => {
const tempRoot = await mkdtemp(join(tmpdir(), "opencode-memory-diag-packaging-"));
const packDir = join(tempRoot, "pack");
const consumerDir = join(tempRoot, "consumer");
try {
await mkdir(packDir, { recursive: true });
await mkdir(consumerDir, { recursive: true });
const packResult = await execFileAsync(executable("npm"), [
"pack",
repoRoot,
"--pack-destination",
packDir,
"--silent",
], { cwd: tempRoot, maxBuffer });
const tarballName = packResult.stdout.trim().split(/\r?\n/).filter(Boolean).at(-1);
assert.ok(tarballName, `npm pack did not report a tarball name. stdout:\n${packResult.stdout}\nstderr:\n${packResult.stderr}`);
const tarballPath = join(packDir, tarballName);
const runResult = await execFileAsync(executable("npx"), [
"--yes",
"--package",
tarballPath,
"memory-diag",
"--help",
], { cwd: consumerDir, maxBuffer });
assert.match(runResult.stdout, /Usage:/);
assert.match(runResult.stdout, /memory-diag \[status\]/);
} finally {
await rm(tempRoot, { recursive: true, force: true });
}
});
+141 -3
View File
@@ -1,11 +1,13 @@
import test from "node:test";
import assert from "node:assert/strict";
import { existsSync } from "node:fs";
import { mkdtemp, rm, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { mkdir, mkdtemp, readFile, readdir, rm, writeFile } from "node:fs/promises";
import { dirname, join } from "node:path";
import { tmpdir } from "node:os";
import { spawn } from "node:child_process";
import { updateJSON } from "../src/storage.ts";
import { atomicWriteJSON, readJSON, updateJSON } from "../src/storage.ts";
import { queryEvidenceEvents } from "../src/evidence-log.ts";
import { workspaceMemoryPath } from "../src/paths.ts";
test("updateJSON serializes concurrent increments", async () => {
const root = await mkdtemp(join(tmpdir(), "wm-storage-"));
@@ -22,6 +24,23 @@ test("updateJSON serializes concurrent increments", async () => {
}
});
test("atomicWriteJSON is a full-state overwrite primitive", async () => {
const root = await mkdtemp(join(tmpdir(), "wm-storage-atomic-overwrite-"));
try {
const path = join(root, "store.json");
await atomicWriteJSON(path, { retained: true, removed: true });
await atomicWriteJSON(path, { retained: true });
const raw = await readFile(path, "utf8");
assert.deepEqual(JSON.parse(raw), { retained: true });
assert.equal(raw.includes("removed"), false, "atomic overwrite should not merge with previous state");
assert.equal(existsSync(`${path}.lock`), false, "atomic overwrite should not create the RMW lock file");
} finally {
await rm(root, { recursive: true, force: true });
}
});
test("updateJSON does not replace corrupt JSON with fallback", async () => {
const root = await mkdtemp(join(tmpdir(), "wm-storage-corrupt-"));
try {
@@ -37,6 +56,44 @@ test("updateJSON does not replace corrupt JSON with fallback", async () => {
}
});
test("readJSON quarantines corrupt JSON and returns fallback", async () => {
const dir = await mkdtemp(join(tmpdir(), "wm-storage-corrupt-"));
const path = join(dir, "store.json");
try {
await writeFile(path, "{ invalid json", "utf8");
const loaded = await readJSON(path, () => ({ ok: true }));
assert.deepEqual(loaded, { ok: true });
const files = await readdir(dir);
assert.equal(files.includes("store.json"), false);
assert.equal(files.some(file => file.startsWith("store.json.corrupt-")), true);
} finally {
await rm(dir, { recursive: true, force: true });
}
});
test("readJSON emits corrupt JSON quarantine evidence for workspace stores", async () => {
const root = await mkdtemp(join(tmpdir(), "wm-storage-evidence-corrupt-"));
try {
const path = await workspaceMemoryPath(root);
await mkdir(dirname(path), { recursive: true });
await writeFile(path, "{ invalid json", "utf8");
const loaded = await readJSON(path, () => ({ ok: true }));
const events = await queryEvidenceEvents(root, { types: ["storage_corrupt_json_quarantined"] });
assert.deepEqual(loaded, { ok: true });
assert.equal(events.length, 1);
assert.equal(events[0].reasonCodes.includes("invalid_json"), true);
assert.equal(JSON.stringify(events).includes("invalid json"), false);
} finally {
await rm(root, { recursive: true, force: true });
}
});
test("updateJSON recovers stale lock files left by crashed process", async () => {
const root = await mkdtemp(join(tmpdir(), "wm-storage-stale-lock-"));
try {
@@ -52,6 +109,47 @@ test("updateJSON recovers stale lock files left by crashed process", async () =>
}
});
test("updateJSON emits stale lock recovery evidence for workspace stores", async () => {
const root = await mkdtemp(join(tmpdir(), "wm-storage-evidence-stale-lock-"));
try {
const path = await workspaceMemoryPath(root);
const lockPath = `${path}.lock`;
await mkdir(dirname(path), { recursive: true });
await writeFile(lockPath, `999999\n0\n`, "utf8");
await updateJSON(path, () => ({ count: 0 }), current => ({ count: current.count + 1 }));
const events = await queryEvidenceEvents(root, { types: ["storage_stale_lock_recovered"] });
assert.equal(events.length, 1);
assert.equal(events[0].reasonCodes.includes("stale_lock"), true);
assert.equal(JSON.stringify(events).includes("999999"), false);
} finally {
await rm(root, { recursive: true, force: true });
}
});
test("updateJSON emits lock timeout evidence and still throws", async () => {
const root = await mkdtemp(join(tmpdir(), "wm-storage-evidence-timeout-"));
try {
const path = await workspaceMemoryPath(root);
const lockPath = `${path}.lock`;
await mkdir(dirname(path), { recursive: true });
await writeFile(lockPath, `${process.pid}\n${Date.now()}\n`, "utf8");
await assert.rejects(
updateJSON(path, () => ({ count: 0 }), current => current),
/Timed out waiting for lock/,
);
const events = await queryEvidenceEvents(root, { types: ["storage_lock_timeout"] });
assert.equal(events.length, 1);
assert.equal(events[0].reasonCodes.includes("lock_wait_timeout"), true);
assert.equal(JSON.stringify(events).includes(String(process.pid)), false);
} finally {
await rm(root, { recursive: true, force: true });
}
});
test("updateJSON serializes writes across separate node processes", async () => {
const root = await mkdtemp(join(tmpdir(), "wm-storage-xproc-"));
try {
@@ -81,3 +179,43 @@ test("updateJSON serializes writes across separate node processes", async () =>
await rm(root, { recursive: true, force: true });
}
});
test("updateJSON waits for a live cross-process lock and preserves both updates", async () => {
const root = await mkdtemp(join(tmpdir(), "wm-storage-live-lock-"));
try {
const path = join(root, "counter.json");
const worker = `
import { updateJSON } from ${JSON.stringify(new URL("../src/storage.ts", import.meta.url).href)};
const path = process.argv[1];
await updateJSON(path, () => ({ count: 0, order: [] }), async current => {
await new Promise(resolve => setTimeout(resolve, 250));
return { count: current.count + 1, order: [...current.order, "child"] };
});
`;
const child = spawn(
process.execPath,
["--experimental-strip-types", "--input-type=module", "-e", worker, path],
{ stdio: "inherit" },
);
await new Promise(resolve => setTimeout(resolve, 50));
await updateJSON(path, () => ({ count: 0, order: [] as string[] }), current => ({
count: current.count + 1,
order: [...current.order, "parent"],
}));
await new Promise<void>((resolve, reject) => {
child.on("exit", code => code === 0 ? resolve() : reject(new Error(`child exited ${code}`)));
child.on("error", reject);
});
const final = await updateJSON(path, () => ({ count: 0, order: [] as string[] }), current => current);
assert.equal(final.count, 2);
assert.deepEqual(new Set(final.order), new Set(["child", "parent"]));
assert.equal(existsSync(`${path}.lock`), false);
} finally {
await rm(root, { recursive: true, force: true });
}
});
+365
View File
@@ -0,0 +1,365 @@
import test from "node:test";
import assert from "node:assert/strict";
import { mkdtemp, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import type { TuiCommand } from "@opencode-ai/plugin/tui";
import type { LongTermMemoryEntry, WorkspaceMemoryStore } from "../src/types.ts";
import { saveWorkspaceMemory } from "../src/workspace-memory.ts";
// ---------------------------------------------------------------------------
// Mock infrastructure
// ---------------------------------------------------------------------------
type MockDialogSize = "medium" | "large" | "xlarge";
type MockDialogAlertProps = { title: string; message: string; onConfirm?: () => void };
type MockDialogSelectOption<Value = string> = {
title: string;
value: Value;
description?: string;
footer?: string;
category?: string;
disabled?: boolean;
onSelect?: () => void | Promise<void>;
};
type MockDialogSelectProps<Value = string> = {
title: string;
placeholder?: string;
options: MockDialogSelectOption<Value>[];
onSelect?: (option: MockDialogSelectOption<Value>) => void | Promise<void>;
skipFilter?: boolean;
};
type MockDialogElement =
| { type: "DialogAlert"; props: MockDialogAlertProps }
| { type: "DialogSelect"; props: MockDialogSelectProps };
type MockDialogRender = () => MockDialogElement;
type MockDialogContext = {
clear: () => void;
replace?: (render: MockDialogRender, onClose?: () => void) => void;
setSize?: (size: MockDialogSize) => void;
renders: MockDialogElement[];
sizes: MockDialogSize[];
events: string[];
};
type RuntimeCommand = { value: string; suggested?: boolean; slash?: { name: string; aliases?: string[] }; onSelect?: (dialog: MockDialogContext) => void | Promise<void> };
type MockPromptCall = Record<string, unknown>;
interface MockTuiApi {
commands: RuntimeCommand[];
prompts: MockPromptCall[];
toasts: Array<{ variant?: string; message: string }>;
dialog: MockDialogContext;
route: { name: string; params?: Record<string, unknown> };
state: { path: { directory: string } };
command: { register: (cb: () => TuiCommand[]) => () => void };
ui: {
toast: (input: { variant?: string; message: string }) => void;
dialog?: Partial<MockDialogContext>;
DialogAlert?: (props: MockDialogAlertProps) => MockDialogElement;
DialogSelect?: (props: MockDialogSelectProps) => MockDialogElement;
};
client: { session: { prompt: (input: MockPromptCall) => Promise<void> } };
}
function makeMockTuiApi(options: {
route: { name: string; params?: Record<string, unknown> };
directory?: string;
missingDialogAlert?: boolean;
missingDialogSelect?: boolean;
missingDialogReplace?: boolean;
missingDialogSetSize?: boolean;
dialogReplaceThrows?: boolean;
}): MockTuiApi {
const commands: RuntimeCommand[] = [];
const prompts: MockPromptCall[] = [];
const toasts: Array<{ variant?: string; message: string }> = [];
const dialog: MockDialogContext = {
clear: () => { dialog.events.push("clear"); },
replace: (render: MockDialogRender) => {
dialog.events.push("replace");
if (options.dialogReplaceThrows) throw new Error("dialog failure");
dialog.renders.push(render());
},
setSize: (size: MockDialogSize) => {
dialog.events.push(`setSize:${size}`);
dialog.sizes.push(size);
},
renders: [],
sizes: [],
events: [],
};
const uiDialog: Partial<MockDialogContext> = {
clear: dialog.clear,
replace: options.missingDialogReplace ? undefined : dialog.replace,
setSize: options.missingDialogSetSize ? undefined : dialog.setSize,
};
return {
commands,
prompts,
toasts,
dialog,
route: options.route,
state: { path: { directory: options.directory ?? "/mock/workspace" } },
command: {
register: (cb: () => TuiCommand[]) => {
const items = cb();
for (const item of items) {
commands.push({
value: item.value,
suggested: item.suggested,
slash: item.slash,
onSelect: item.onSelect
? (dialogContext: MockDialogContext = dialog) => (item.onSelect as (dialog: MockDialogContext) => void | Promise<void>)(dialogContext)
: undefined,
});
}
return () => {};
},
},
ui: {
toast: (input: { variant?: string; message: string }) => { toasts.push(input); },
dialog: uiDialog,
DialogAlert: options.missingDialogAlert ? undefined : (props: MockDialogAlertProps): MockDialogElement => ({ type: "DialogAlert", props }),
DialogSelect: options.missingDialogSelect ? undefined : (props: MockDialogSelectProps): MockDialogElement => ({ type: "DialogSelect", props }),
},
client: {
session: {
prompt: async (input: MockPromptCall) => { prompts.push(input); },
},
},
};
}
async function selectCommand(api: MockTuiApi, value: string): Promise<void> {
const command = api.commands.find((item): item is RuntimeCommand => item.value === value);
assert.ok(command, `registered command ${value}`);
await command.onSelect?.(api.dialog);
}
function lastDialog(api: MockTuiApi): MockDialogElement {
const hit = api.dialog.renders.at(-1);
assert.ok(hit, "expected a rendered dialog");
return hit;
}
async function chooseSelectOption(api: MockTuiApi, value: string): Promise<void> {
const dialog = lastDialog(api);
assert.equal(dialog.type, "DialogSelect");
const option = dialog.props.options.find(item => item.value === value);
assert.ok(option, `expected select option ${value}`);
// Source evidence: OpenCode's plugin API maps option.onSelect to a zero-arg
// callback and DialogSelect invokes option.onSelect before top-level onSelect.
await option.onSelect?.();
}
async function tempRoot(): Promise<string> {
return mkdtemp(join(tmpdir(), "memory-tui-test-"));
}
function memory(id: string, text: string, overrides: Partial<LongTermMemoryEntry> = {}): LongTermMemoryEntry {
const now = new Date().toISOString();
return {
id,
type: "decision",
text,
source: "compaction",
confidence: 0.8,
status: "active",
createdAt: now,
updatedAt: now,
...overrides,
};
}
async function seedWorkspaceMemories(root: string): Promise<void> {
const now = new Date().toISOString();
const store: WorkspaceMemoryStore = {
version: 1,
workspace: { root, key: "test" },
limits: { maxRenderedChars: 3600, maxEntries: 28 },
entries: [
memory("mem-feedback", "Remember password: sushi for the fake test.", { type: "feedback" }),
memory("mem-project", "Project memory should render in its group.", { type: "project" }),
memory("mem-decision", "Decision memory should render in its group.", { type: "decision" }),
memory("mem-reference", "Reference memory should render in its group.", { type: "reference" }),
memory("mem-superseded", "Superseded memory should not be active", { type: "reference", status: "superseded" }),
],
migrations: [],
updatedAt: now,
};
await saveWorkspaceMemory(root, store);
}
// Dynamic import to allow module-level mocking
const { MemoryTuiPlugin } = await import("../src/tui-plugin.ts");
test("registers one unsuggested /memory slash command", async () => {
const api = makeMockTuiApi({ route: { name: "session", params: { sessionID: "ses_1" } } });
await MemoryTuiPlugin(api as any, undefined, { id: "test", source: "file", spec: "test", target: "./tui", first_time: Date.now(), last_time: Date.now(), time_changed: Date.now(), load_count: 1, fingerprint: "test", state: "first" });
assert.deepEqual(api.commands.map(command => command.value), ["memory.menu"]);
assert.deepEqual(api.commands.map(command => command.slash?.name).filter(Boolean), ["memory"]);
assert.deepEqual(api.commands.map(command => command.suggested), [undefined]);
for (const removedName of ["memory-" + "status", "memory-" + "list", "memory-" + "help"]) {
assert.equal(api.commands.some(command => command.slash?.name === removedName), false);
}
});
test("opens the memory submenu without prompt injection", async () => {
const api = makeMockTuiApi({ route: { name: "session", params: { sessionID: "ses_1" } } });
await MemoryTuiPlugin(api as any, undefined, { id: "test", source: "file", spec: "test", target: "./tui", first_time: Date.now(), last_time: Date.now(), time_changed: Date.now(), load_count: 1, fingerprint: "test", state: "first" });
await selectCommand(api, "memory.menu");
assert.equal(api.prompts.length, 0);
const dialog = lastDialog(api);
assert.equal(dialog.type, "DialogSelect");
assert.equal(dialog.props.title, "Memory");
assert.equal(dialog.props.placeholder, "Search memory actions");
assert.deepEqual(dialog.props.options.map(item => item.title), ["Status", "Current memories", "Help"]);
assert.deepEqual(dialog.props.options.map(item => item.value), ["memory.status", "memory.list", "memory.help"]);
assert.ok(api.dialog.events.indexOf("clear") < api.dialog.events.indexOf("replace"));
assert.ok(api.dialog.events.indexOf("replace") < api.dialog.events.indexOf("setSize:large"));
});
test("supports home-route menu, list, and help while status warns", async () => {
const root = await tempRoot();
try {
const api = makeMockTuiApi({ route: { name: "home" }, directory: root });
await MemoryTuiPlugin(api as any, undefined, { id: "test", source: "file", spec: "test", target: "./tui", first_time: Date.now(), last_time: Date.now(), time_changed: Date.now(), load_count: 1, fingerprint: "test", state: "first" });
await selectCommand(api, "memory.menu");
assert.equal(lastDialog(api).type, "DialogSelect");
assert.equal(lastDialog(api).props.title, "Memory");
await chooseSelectOption(api, "memory.list");
assert.equal(lastDialog(api).type, "DialogAlert");
assert.equal(lastDialog(api).props.title, "Current workspace memories");
await selectCommand(api, "memory.menu");
await chooseSelectOption(api, "memory.help");
assert.equal(lastDialog(api).type, "DialogAlert");
assert.equal(lastDialog(api).props.title, "Memory help");
await selectCommand(api, "memory.menu");
const beforeStatusRenders = api.dialog.renders.length;
await chooseSelectOption(api, "memory.status");
assert.equal(api.dialog.renders.length, beforeStatusRenders, "status should not render without an active session");
assert.ok(api.toasts.some(t => t.variant === "warning" && t.message === "Open a session to use memory commands."));
assert.equal(api.prompts.length, 0);
} finally {
await rm(root, { recursive: true, force: true });
}
});
test("shows status and help alerts from the submenu", async () => {
const root = await tempRoot();
try {
const api = makeMockTuiApi({ route: { name: "session", params: { sessionID: "ses_1" } }, directory: root });
await MemoryTuiPlugin(api as any, undefined, { id: "test", source: "file", spec: "test", target: "./tui", first_time: Date.now(), last_time: Date.now(), time_changed: Date.now(), load_count: 1, fingerprint: "test", state: "first" });
await selectCommand(api, "memory.menu");
await chooseSelectOption(api, "memory.status");
assert.equal(lastDialog(api).type, "DialogAlert");
assert.equal(lastDialog(api).props.title, "Memory status");
assert.match(lastDialog(api).props.message, /Workspace:/);
await selectCommand(api, "memory.menu");
await chooseSelectOption(api, "memory.help");
assert.equal(lastDialog(api).type, "DialogAlert");
assert.equal(lastDialog(api).props.title, "Memory help");
assert.match(lastDialog(api).props.message, /Status/);
assert.equal(api.prompts.length, 0);
} finally {
await rm(root, { recursive: true, force: true });
}
});
test("shows current memories in a grouped DialogSelect with no-op row selection", async () => {
const root = await tempRoot();
try {
await seedWorkspaceMemories(root);
const api = makeMockTuiApi({ route: { name: "session", params: { sessionID: "ses_1" } }, directory: root });
await MemoryTuiPlugin(api as any, undefined, { id: "test", source: "file", spec: "test", target: "./tui", first_time: Date.now(), last_time: Date.now(), time_changed: Date.now(), load_count: 1, fingerprint: "test", state: "first" });
await selectCommand(api, "memory.menu");
await chooseSelectOption(api, "memory.list");
const dialog = lastDialog(api);
assert.equal(dialog.type, "DialogSelect");
assert.equal(dialog.props.title, "Current workspace memories");
assert.equal(dialog.props.placeholder, "Search memory refs");
assert.deepEqual([...new Set(dialog.props.options.map(item => item.category))], ["feedback", "project", "decision", "reference"]);
assert.ok(dialog.props.options.every(item => /^\[M\d+\] /.test(item.title)));
assert.ok(dialog.props.options.every(item => typeof item.footer === "string"));
assert.equal(dialog.props.options.some(item => item.title.includes("sushi")), false);
assert.equal(dialog.props.options.some(item => item.title.includes("Superseded memory should not be active")), false);
assert.equal(api.dialog.sizes.at(-1), "xlarge");
const beforeRenders = api.dialog.renders.length;
const beforeToasts = api.toasts.length;
await chooseSelectOption(api, dialog.props.options[0].value);
assert.equal(api.dialog.renders.length, beforeRenders, "memory row selection should not replace dialog in this wave");
assert.equal(api.toasts.length, beforeToasts, "memory row selection should not expose mutation/action toast");
assert.equal(api.prompts.length, 0);
} finally {
await rm(root, { recursive: true, force: true });
}
});
test("shows empty current memories as an alert", async () => {
const root = await tempRoot();
try {
const api = makeMockTuiApi({ route: { name: "session", params: { sessionID: "ses_1" } }, directory: root });
await MemoryTuiPlugin(api as any, undefined, { id: "test", source: "file", spec: "test", target: "./tui", first_time: Date.now(), last_time: Date.now(), time_changed: Date.now(), load_count: 1, fingerprint: "test", state: "first" });
await selectCommand(api, "memory.menu");
await chooseSelectOption(api, "memory.list");
assert.equal(lastDialog(api).type, "DialogAlert");
assert.equal(lastDialog(api).props.title, "Current workspace memories");
assert.match(lastDialog(api).props.message, /No active workspace memories are stored yet\./);
} finally {
await rm(root, { recursive: true, force: true });
}
});
test("shows local read failures in a memory error alert", async () => {
const api = makeMockTuiApi({ route: { name: "session", params: { sessionID: "ses_1" } } });
api.state.path.directory = undefined as never;
await MemoryTuiPlugin(api as any, undefined, { id: "test", source: "file", spec: "test", target: "./tui", first_time: Date.now(), last_time: Date.now(), time_changed: Date.now(), load_count: 1, fingerprint: "test", state: "first" });
await selectCommand(api, "memory.menu");
await chooseSelectOption(api, "memory.status");
assert.equal(api.prompts.length, 0);
assert.equal(lastDialog(api).type, "DialogAlert");
assert.equal(lastDialog(api).props.title, "Memory error");
assert.match(lastDialog(api).props.message, /Unable to render local memory visibility output\./);
});
test("shows error toast when dialog runtime API is unavailable", async () => {
for (const options of [
{ missingDialogAlert: true },
{ missingDialogSelect: true },
{ missingDialogReplace: true },
{ missingDialogSetSize: true },
]) {
const api = makeMockTuiApi({ route: { name: "session", params: { sessionID: "ses_1" } }, ...options });
await MemoryTuiPlugin(api as any, undefined, { id: "test", source: "file", spec: "test", target: "./tui", first_time: Date.now(), last_time: Date.now(), time_changed: Date.now(), load_count: 1, fingerprint: "test", state: "first" });
await selectCommand(api, "memory.menu");
assert.equal(api.prompts.length, 0, "should not fall back to prompt injection");
assert.equal(api.dialog.renders.length, 0, "should not partially open a dialog when API guard fails");
assert.ok(api.toasts.some(t => t.variant === "error" && t.message === "Memory dialog UI is unavailable in this OpenCode runtime."));
}
});
test("shows error toast when dialog replacement fails without prompt fallback", async () => {
const api = makeMockTuiApi({ route: { name: "session", params: { sessionID: "ses_1" } }, dialogReplaceThrows: true });
await MemoryTuiPlugin(api as any, undefined, { id: "test", source: "file", spec: "test", target: "./tui", first_time: Date.now(), last_time: Date.now(), time_changed: Date.now(), load_count: 1, fingerprint: "test", state: "first" });
await selectCommand(api, "memory.menu");
assert.equal(api.prompts.length, 0);
assert.equal(api.dialog.renders.length, 0);
assert.equal(api.dialog.sizes.length, 0);
assert.ok(api.toasts.some(t => t.variant === "error" && /^Unable to show memory dialog: dialog failure$/.test(t.message)), "should show error toast on dialog failure");
});
File diff suppressed because it is too large Load Diff
+1 -1
View File
@@ -25,6 +25,6 @@
"allowImportingTsExtensions": true,
"noEmit": true
},
"include": ["index.ts", "src/**/*.ts"],
"include": ["index.ts", "src/**/*.ts", "scripts/memory-diag.ts", "scripts/memory-diag/**/*.ts"],
"exclude": ["node_modules", "dist"]
}
+27
View File
@@ -0,0 +1,27 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"allowImportingTsExtensions": true,
"noEmit": false,
"outDir": "dist",
"rootDir": ".",
"declaration": false,
"sourceMap": false,
"rewriteRelativeImportExtensions": true,
"tsBuildInfoFile": "dist/.tsbuildinfo"
},
"include": ["scripts/memory-diag.ts", "scripts/memory-diag/**/*.ts", "src/**/*.ts"],
"exclude": [
"node_modules",
"dist",
"tests",
"src/plugin.ts",
"src/tui-plugin.ts",
"src/opencode.ts",
"src/session-state.ts",
"src/extractors.ts",
"src/pending-journal.ts",
"src/promotion-accounting.ts",
"src/memory-visibility.ts"
]
}
+7
View File
@@ -0,0 +1,7 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"noUnusedLocals": true,
"noUnusedParameters": true
}
}