Skip to content

feat(memory): persistence master switch + context injection toggles - #10213

Merged
iamwhatever merged 1 commit into
kirodotdev:mainfrom
Yangxinyee:feat/memory-context-toggles
Sep 23, 2026
Merged

iamwhatever merged 1 commit into
kirodotdev:mainfrom
Yangxinyee:feat/memory-context-toggles

Conversation

@Yangxinyee

@Yangxinyee Yangxinyee commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Motivation

Two open asks against the memory subsystem:

  • Add a global setting to disable persistent memory #9959: there is no way to turn persistent memory off. Security policy can block writes, but nothing stops already-stored memories from being injected into every future session; incorrect memories degrade responses and the accumulated store grows past practical human review.
  • Expose memory and lessons context blocks to config: disable toggles and size caps #9909 (the disable half): the memory and lessons context blocks dominate the pre-prompt budget (~13k tokens measured on a long-lived install, paid on every turn) and cannot be turned off on the main path. The gate mechanism (SWITCHABLE_CONTEXT_GROUPS / _group_included) already exists but is only reachable from subagent spawns — the dashboard/channel/cron paths never pass context_groups.

Why it matters

Users who cannot trust or audit accumulated memory today have no off switch short of deleting their store and hoping nothing re-writes it. Power users tuning context budgets have no lever at all — the blocks are unconditionally injected on every surface. Both issues were routed needs-human by the repo's triage pipeline with the note that the mechanism is nearly pure exposure.

What changed (motivation → approach → change)

Goal: one coherent config surface covering both asks, rather than two competing switches.

Approach: intersect the operator's config with the caller-passed context_groups inside build_session_context — the one method every surface (dashboard, hooks, Slack, Discord, generic channels, cron, heartbeat, monitor, task runner, eval, subagents) passes through — instead of threading a new parameter through the ten call sites that don't pass one. Subagent narrowing is preserved (intersection); the [CONTEXT SCOPE] block stays keyed to the caller-passed scope only, because "your parent withheld" describes per-spawn narrowing, not the operator's standing choice. Write-side enforcement sits at service boundaries rather than inside the storage layer, so the existing capabilities.memory_writes governance gate (a per-surface policy mechanism on one MCP call site) is left intact and the two compose.

Built:

  • memory.persistence_enabled (master, default true): off stops every automatic writer — POST /api/lessons refuses with 403 / code: persistence_disabled (the enforcement point behind the learn_add MCP tool), kirocrew learn add refuses with an actionable message, task-runner lesson extraction skips before its LLM call, and history consolidation pauses entirely (all three automatic entry points plus _consolidate itself, so the manual REST/CLI triggers are covered and no LLM turn is billed; the existing REFUSED sentinel keeps offsets unadvanced). It also withholds the stored memory/lessons blocks from new-session context. Untouched by design: within-conversation context, reads, explicit deletions (the right to forget), and explicit dashboard edits.
  • memory.inject_memory / memory.inject_lessons (default true): withhold the corresponding context block, durably via config.

Out of scope, deliberately: the size-caps half of #9909 (two mutually exclusive config shapes proposed; awaiting a maintainer ruling), the "queue memories for review" mode from #9959 (overlaps the candidate-tier discussion in #9983 / #2149), and a Settings UI toggle (config.json + kirocrew config set work today).

Tests

  • New test/test_memory_context_toggles.py: presence/absence of each block with seeded content; config∩subagent intersection in both directions (config off beats an explicit full-scope parent; subagent narrowing survives config all-on); config withholding emits no [CONTEXT SCOPE] marker; consolidation gates (all three entry points schedule nothing when disabled, an enabled-path control test, and _consolidate itself returns the REFUSED sentinel — covering the manual triggers); POST /api/lessons 403 with the machine-readable code; CLI refusal with nothing written to either store.
  • test/test_config_loader.py::TestMemoryPersistenceAndInjectionToggles: house-style default / override / junk-rejection tests for all three keys (_safe_bool semantics).
  • config-baseline.json regenerated; baseline drift and error-code ratchet tests green.

Manual verification

Ran the gateway with an isolated data home; flipped each key in config.json (picked up live via the fingerprint cache, no restart): with inject_lessons: false a new session's context carries no [Learned corrections]/[USER PROFILE] block; with persistence_enabled: false, learn_add returns the refusal text, kirocrew learn add prints it, and no consolidation fires past the 30-message threshold ("consolidation skipped … persistence_enabled" in the log).

Full local gate on this host: backend suite 94,494 passed with 21 failures, all of which reproduce identically on a clean origin/main checkout (host-environmental: test_symbols_manifest_contract.py ×14, PTY terminal ×2, ksh installer, pptx provision, spawn-exec OverflowError, apps deps lock); isort / flake8 / mypy clean; frontend cross-surface guard specs 6606/6607 (the one failure is a pre-existing flake that passes in isolation on both this branch and main).

Related Issues

Closes #9959. Refs #9909 (size caps left for a follow-up pending a config-shape ruling — see my comment on the issue).

Checklist

  • At most two commits (one is the norm), with a Conventional Commits title (feat|fix|docs|refactor|perf|test|chore|ci|build|revert: ...)
  • Existing tests pass and new tests added for new functionality
  • Self-review completed; code follows project style guidelines
  • Documentation updated (if applicable)
  • No secrets, credentials, or internal references in the diff

Contribution License Agreement

N/A — CLA text pending per the template placeholder; I agree to license this contribution under the repository's license (Apache-2.0).

@Yangxinyee
Yangxinyee requested review from a team and Zedmor September 11, 2026 21:20
@github-actions github-actions Bot added merge conflict Branch has merge conflicts with its base — author must resolve before merge fork Pull request from a fork (external contributor) readiness: checking Automated validation is still running labels Sep 11, 2026
@Yangxinyee
Yangxinyee force-pushed the feat/memory-context-toggles branch from 4306431 to a09f3f1 Compare September 14, 2026 18:02
@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention merge conflict Branch has merge conflicts with its base — author must resolve before merge and removed readiness: checking Automated validation is still running merge conflict Branch has merge conflicts with its base — author must resolve before merge labels Sep 14, 2026
@Yangxinyee
Yangxinyee force-pushed the feat/memory-context-toggles branch from a09f3f1 to 33d9c8e Compare September 14, 2026 21:55
@github-actions github-actions Bot added readiness: checking Automated validation is still running and removed readiness: action required A blocking check or review needs attention labels Sep 14, 2026
@github-actions

github-actions Bot commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Fable 5, fork) — 🟡 CONCERNS

Design-level review of 5249a96f3966977c0a32a8d6f306c2037ca13c90 via the fork AI-review pipeline — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

Design-Verdict: CONCERNS

The switch's "no automatic memory writes" promise ships with a hole: the ops app's ledger sweep still writes episodic rows while persistence is off.

Watch

The PR's own inventory register marks ops_mission_control/backend/ledger_index.py::import_pending "ungated pending a ruling" — so an operator who flips persistence_enabled: false for the trust/audit reasons #9959 names still accumulates automatic app-scoped episodic writes, exactly the store growth the switch exists to stop. It is disclosed in the user docs and the register, but the exemption is an open maintainer decision, not a settled one.
Clears when: a maintainer rules on the exemption in the PR thread — either the sweep gains the persistence gate or the exemption is affirmed as intended semantics.

[DESIGN-REVIEWED] 5249a96

@github-actions

github-actions Bot commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Fable 5, fork) — ✅ PASS

Premise-level review of 5249a96f3966977c0a32a8d6f306c2037ca13c90 via the fork AI-review pipeline — why this exists and whether the shipped surface is the smallest honest version. Updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

First-Principles-Verdict: PASS

Verify the admitted hole: with persistence off, ops_mission_control's ledger sweep still writes episodic rows — the diff itself defers that to a maintainer ruling.

Not justified as shipped

  • Item 8 — undeclared: api_memory_consolidate gains its own early 403 + SEL denial the description never mentions; its code comment concedes "the inner _consolidate gate would refuse anyway", justified only by the earlier, machine-readable refusal and the transcript read it saves.
  • Item 9 — undeclared: test_persistence_writer_inventory.py — a repo-wide AST ratchet whose GATED_AUTOMATIC/EXEMPT register every future write-primitive caller must edit — appears nowhere in the description's test list, though it matches the repo's pinned-inventory house pattern.

What this change ships

Inventory (10 items) — 8 justified

Intent: give operators a standing off switch for persistent memory and its context injection (#9959, #9909's disable half) — an ADDITION, defaults preserving current behavior.

  1. New config key memory.persistence_enabled, default on — justified
  2. New keys memory.inject_memory / memory.inject_lessons withhold the stored-memory / lessons blocks on every surface — justified
  3. Persistence off: lesson writes refuse on all three transports (POST /api/lessons 403, MCP, CLI) — justified
  4. Persistence off: history consolidation pauses at all entry points, no LLM turn billed — justified
  5. Persistence off: task-runner lesson extraction skips before its LLM call — justified
  6. Injection toggles also govern the post-compaction memory re-injection — justified
  7. Config intersects with subagent narrowing; [CONTEXT SCOPE] stays parent-only — justified
  8. POST /api/memory/consolidate gets its own early 403 + SEL denial — undeclared
  9. New writer-inventory ratchet test with a gated/exempt register — undeclared
  10. Installed-app ingestion sweep documented as still writing while off, deferred for a ruling — justified

[FIRST-PRINCIPLES-REVIEWED] 5249a96

@github-actions

github-actions Bot commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

GPT 5.6 Review (fork) — ✅ no blocking findings

Reviewed 5249a96f3966977c0a32a8d6f306c2037ca13c90 via the fork AI-review pipeline; updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] 5249a96

@github-actions

github-actions Bot commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Opus 4.8 Review (fork) — ✅ no blocking findings

Reviewed 5249a96f3966977c0a32a8d6f306c2037ca13c90 via the fork AI-review pipeline; updated in place on each push.

Review details

No findings.

[OPUS-REVIEWED] 5249a96

@Yangxinyee
Yangxinyee force-pushed the feat/memory-context-toggles branch from 33d9c8e to eac9737 Compare September 14, 2026 22:22
@github-actions github-actions Bot removed the merge conflict Branch has merge conflicts with its base — author must resolve before merge label Sep 14, 2026
@Yangxinyee
Yangxinyee force-pushed the feat/memory-context-toggles branch from eac9737 to f741f1b Compare September 14, 2026 22:31
@Yangxinyee

Copy link
Copy Markdown
Contributor Author

Thanks — the Watch is legitimate and is now addressed by the second option you named. Pushed in f741f1bf9.

Watch (write-side has no chokepoint) — fixed via the inventory ratchet.

I did not take the store-layer backstop, deliberately: the storage layer cannot be the gate without taking away the right to forget, since explicit dashboard edits and deletions must keep working while the switch is off. An "explicit-write flag" would push that policy decision into every call site anyway, which is the same drift surface one layer down.

So test/test_persistence_writer_inventory.py pins the inventory instead. It AST-walks src/kiro_crew for every function that reaches a persistent-write primitive (write_lesson, write_episodic, set_semantic, set_semantic_if_absent, write_preferences, write_projects, append_history, save_or_enrich) and requires each one to be classified as either GATED_AUTOMATIC or EXEMPT with a stated reason. A new writer lands in neither and goes red with a message naming both options. A second test rejects stale register entries, so a rename cannot leave a dead name a future writer could collide with. I verified the ratchet bites by adding a throwaway ungated writer — it failed as intended — then reverted it.

One detail worth flagging: the scan matches an attribute reference, not only a call. The offloading writers hand the bound method to a pool (run_in_embed_pool(store.write_lesson, ...), asyncio.to_thread(store.save, ...)) rather than calling it inline, and my first call-target-only version silently missed taskrunner._extract_lesson — the very shape most likely to skip a gate.

A gap the inventory surfaced, which I'd like a ruling on. Building the register turned up an automatic writer neither of us had counted: apps/builtins/ops_mission_control/backend/ledger_index.py::import_pending projects that app's ops ledger into the shared episodic store on a sweep, so it writes while persistence_enabled is false. I registered it in EXEMPT with the reason "app-scoped sweep; ungated pending a ruling" rather than either silently widening this PR into an app backend or hiding it — the entry makes the decision visible and reviewable. If you want the switch to cover installed apps' automatic capture too, I'm happy to gate it here or in a follow-up; it is a four-line check plus its own test.

Suggestion (the api_memory_consolidate check duplicates the REFUSED sentinel). Keeping both, and I'd argue the drift risk is lower than it looks: the two read the same config key through the same loader, so they cannot disagree about the answer — divergence would mean one was deleted, not that they drifted apart. What the route check buys is a specific 403 + code: persistence_disabled for the dashboard caller instead of a generic refusal, and it spends no transcript read on a request that cannot proceed. Threading a typed reason back out of the sentinel would change a contract three done-callbacks and their tests depend on (REFUSED currently means exactly "no pass ran, do not advance offsets"), which is a wider change than the duplication costs. Happy to revisit if you'd rather have the single gate.

@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention and removed readiness: checking Automated validation is still running labels Sep 14, 2026
@Yangxinyee
Yangxinyee force-pushed the feat/memory-context-toggles branch from f741f1b to 42137f4 Compare September 14, 2026 22:47
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Sep 14, 2026
@Yangxinyee
Yangxinyee force-pushed the feat/memory-context-toggles branch from 42137f4 to 0528612 Compare September 15, 2026 18:26
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: checking Automated validation is still running labels Sep 21, 2026
@Yangxinyee

Copy link
Copy Markdown
Contributor Author

Backend is green on ca144d898 (backend-test=success, 81 checks passing). The one real failure from the previous push was mine and is fixed: test_agent_spec_fields_doc::test_steering_is_still_the_backend_gated_half pins the steering gate's source text, and I had substituted effective_groups there. Reverted — _config_scoped_groups only ever subtracts the memory and lessons groups, so on a project-group gate the substitution was a no-op.

The three remaining reds are not from this diff:

  • Frontend Tests (2) — aws-control/DrivePage.test.tsx, the share-dialog focus-return case (expected <div> to be null). This commit touches 0 files under website/, so the frontend tree is byte-identical to main. That file already carries two flake-hygiene commits in the same family (test(aws-control): wait for the focus-return tick in DrivePage focus assertions #11050 waited for the focus-return tick; f9f3fccca stopped two specs asserting transient states).
  • Coverage Gate — fails closed on frontend-test=failure; the same line reports backend-test=success coverage-combine=success.
  • Opus 4.8 Review — "review incomplete" — no verdict reached; the earlier push hit API rate limit exceeded for installation across the fork lanes.

Could a maintainer re-run those two lanes? I have no re-run permission on a fork PR.

@Yangxinyee
Yangxinyee force-pushed the feat/memory-context-toggles branch from ca144d8 to e98684f Compare September 22, 2026 05:27
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Sep 22, 2026
@Yangxinyee

Copy link
Copy Markdown
Contributor Author

Status on e98684fe5 (same tree as ca144d898, re-pushed to re-trigger the lanes that reached no verdict):

  • Frontend Tests (2) now passes — all four frontend shards green, confirming the DrivePage focus-return failure was the flake and not this diff.
  • Backend Tests (3.12, 4) and (Windows) (4) are red from main, not from here. main's own CI on 0a8005093 fails the identical test on the identical two shards: test_pending_boundary_consumer_semantics.py::test_running_and_turn_running_slot_readers_are_enumerated (run 35690360470). It arrives here through the merge ref; the other seven shards pass. Coverage Gate fails closed on it.
  • GPT / Opus / Internal Content Scan / Security Scope Review still reach no verdict: API rate limit exceeded for installation, intermittent across fork PRs (could not list open PRs; the query itself failed on all 3 attempts).

Nothing left on this PR is mine. Both remaining blockers clear on their own — the shards once main is green, the fork lanes on a re-run with API budget.

@Yangxinyee
Yangxinyee force-pushed the feat/memory-context-toggles branch from e98684f to 63fb62a Compare September 22, 2026 17:58
@github-actions github-actions Bot added readiness: checking Automated validation is still running and removed readiness: action required A blocking check or review needs attention labels Sep 22, 2026
@Yangxinyee
Yangxinyee force-pushed the feat/memory-context-toggles branch from 63fb62a to 58dd849 Compare September 22, 2026 20:03
@Yangxinyee

Copy link
Copy Markdown
Contributor Author

Green on 63fb62ab3 (81 checks) once main shipped its two test fixes (#12718, #12865) and the fork lanes got API budget — the earlier shard-4 red was main's, as above.

Taking the GPT finding in 58dd849d9; it is right, and the hole is worse than a weak assertion. test_a_gated_writer_names_the_switch checks that the module mentions persistence_enabled, and _extract_lesson's guard carries that token in its own comment — so deleting the guard leaves the ratchet green, and _extract_lesson was the one gated writer with no behavioural test.

Added TestTaskrunnerLessonGate: it drives _extract_lesson with a stub whose _capture_execution records being called — the first thing the extractor touches past the switch — and asserts it is not reached with persistence off, plus the enabled control so the stub cannot be what stopped it. No LLM call is involved either way. The module check stays as the cheap tripwire for the other three writers, which do have behavioural refusals.

Opus 4.8 Review had been in progress for two hours on the previous SHA, so this push also gives it a fresh run.

@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running and removed readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention labels Sep 22, 2026
Add three memory.* config keys wired end to end:

- persistence_enabled (the global switch, kirodotdev#9959): off stops every
  automatic persistent-memory writer -- POST /api/lessons (the
  learn_add MCP tool's enforcement point), kirocrew learn add,
  task-runner lesson extraction, and history consolidation (all three
  automatic entry points plus _consolidate itself, so the manual
  REST/CLI triggers are covered and no LLM turn is billed) -- and
  withholds the stored memory/lessons blocks from new-session context.
  Within-conversation context is untouched; reads and deletions stay
  available.
- inject_memory / inject_lessons (kirodotdev#9909, injection half): withhold
  the corresponding context group on the main path.

Enforcement intersects the caller-passed context_groups with the
config-enabled set inside build_session_context, the one method all
surfaces pass through, so no call site changes. The member-essentials
builder and the post-compaction re-injection in build_message route
through the same intersection, because each restores a block the
session-start build gates. The [CONTEXT SCOPE] block stays keyed to the
caller-passed scope: config withholding is the operator's standing
choice, not the parent's.

The CLI refusal sits ahead of the vector store, whose construction
creates or migrates memory.db, so a refused write leaves nothing behind.

Closes kirodotdev#9959
Refs kirodotdev#9909 (size caps await a maintainer ruling on config shape)
@Yangxinyee
Yangxinyee force-pushed the feat/memory-context-toggles branch from 58dd849 to 5249a96 Compare September 22, 2026 20:46
@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running and removed readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention labels Sep 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fork Pull request from a fork (external contributor)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a global setting to disable persistent memory

3 participants