diff --git a/config-baseline.json b/config-baseline.json index 139b65b24351..a176ca173d40 100644 --- a/config-baseline.json +++ b/config-baseline.json @@ -1846,6 +1846,9 @@ "history_max_days": 365, "backup_enabled": true, "backup_keep": 7, + "persistence_enabled": true, + "inject_memory": true, + "inject_lessons": true, "migrated": false } }, @@ -2204,6 +2207,48 @@ "enumValues": null, "defaultValue": 7 }, + { + "path": "memory.persistence_enabled", + "kind": "core", + "type": "boolean", + "required": false, + "deprecated": false, + "sensitive": false, + "tags": [], + "label": "Persistence Enabled", + "help": "Global switch for persistent memory. Off: no automatic memory writes (lessons, consolidation extraction, task-runner lessons) and no stored memory/lessons injected into new sessions; within-conversation context is unaffected. Explicit dashboard edits and deletions stay available. An installed app's own ingestion sweep is out of scope and still writes app-scoped rows.", + "hasChildren": false, + "enumValues": null, + "defaultValue": true + }, + { + "path": "memory.inject_memory", + "kind": "core", + "type": "boolean", + "required": false, + "deprecated": false, + "sensitive": false, + "tags": [], + "label": "Inject Memory Context", + "help": "Inject the stored memory block (preferences, the memory activity index and recent-session snippets) into new-session context. On-demand memory_recall is unaffected.", + "hasChildren": false, + "enumValues": null, + "defaultValue": true + }, + { + "path": "memory.inject_lessons", + "kind": "core", + "type": "boolean", + "required": false, + "deprecated": false, + "sensitive": false, + "tags": [], + "label": "Inject Lessons Context", + "help": "Inject the learned-corrections and user-profile blocks into new-session context.", + "hasChildren": false, + "enumValues": null, + "defaultValue": true + }, { "path": "memory.migrated", "kind": "core", diff --git a/docs/system-specs/modules/config.md b/docs/system-specs/modules/config.md index 221609e717fc..9ecefeaef9c2 100644 --- a/docs/system-specs/modules/config.md +++ b/docs/system-specs/modules/config.md @@ -1894,6 +1894,9 @@ class MemoryConfig: embed_model_legacy_ids: list[str] = field(default_factory=list) # managed compatibility labels retained across restarts; explicit model apply clears them and rebuilds inherited vectors history_idle_hours: float = 3.0 # consolidate history after N hours idle history_max_days: int = 365 # prune daily history files older than this + persistence_enabled: bool = True # global switch: off = no automatic memory writes (lessons, consolidation, task-runner) AND no stored memory/lessons injected + inject_memory: bool = True # inject the stored memory block (preferences, activity index, recent-session snippets) into new-session context + inject_lessons: bool = True # inject the [Learned corrections] + [USER PROFILE] blocks into new-session context @dataclass class KnowledgeConfig: @@ -2662,7 +2665,10 @@ Returns the effective config for a channel: }, "memory": { "history_idle_hours": 3.0, - "history_max_days": 365 + "history_max_days": 365, + "persistence_enabled": true, + "inject_memory": true, + "inject_lessons": true }, "knowledge": { "auto_add_documents": false, diff --git a/docs/system-specs/modules/learn-cron-dashboard.md b/docs/system-specs/modules/learn-cron-dashboard.md index 8bba2ea6705f..319f8a8a8f42 100644 --- a/docs/system-specs/modules/learn-cron-dashboard.md +++ b/docs/system-specs/modules/learn-cron-dashboard.md @@ -71,6 +71,8 @@ Named-store lessons require a middleware-verified internal request or the dashbo The `learn_add` MCP tool (backed by `POST /api/lessons`) is subject to session-scope checks in `dashboard/handlers/cron.py:api_lessons_create`: +0. Before any write proceeds, the global persistence switch is checked: `memory.persistence_enabled: false` refuses every create with HTTP 403 and an SEL `persistence_disabled` audit record, regardless of session mode. Enforced on the route so every transport that posts here (MCP tool, dashboard, direct HTTP) is covered by the one check; `GET /api/lessons` and `DELETE /api/lessons` are unaffected — reads and the right to forget survive the switch. + 1. `X-Session-Key` header is required; missing → HTTP 400 `missing X-Session-Key`. 2. `dashboard:ui` (browser UI's static key) is always allowed. 3. Otherwise the slot name (portion after the `:` prefix, or the whole key) must satisfy at least one of: diff --git a/docs/system-specs/modules/memory-skills-hooks.md b/docs/system-specs/modules/memory-skills-hooks.md index c704727b3d93..502e6370ee34 100644 --- a/docs/system-specs/modules/memory-skills-hooks.md +++ b/docs/system-specs/modules/memory-skills-hooks.md @@ -267,6 +267,20 @@ larger than 8 MiB is refused without truncating or deleting it; editable-history GET and PUT retain the existing `store_unavailable` (503) error response. Ordinary V2 message context does not scan learned history. +Whether a group is in scope is the intersection of the caller-passed +`context_groups` (subagent narrowing) with the operator's config toggles — +`memory.inject_memory` / `memory.inject_lessons`, with +`memory.persistence_enabled` as the global switch — computed inside +`build_session_context()` so every surface (dashboard, channels, cron, +heartbeat, task runner, eval, subagents) obeys the config without passing +anything. 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: reading the caller scope alone there would +hand back withheld memory for the rest of the session. The `[CONTEXT SCOPE]` +withheld-groups block stays keyed to the caller-passed value only: "your parent +withheld" describes per-spawn narrowing, not the operator's standing config +choice. + `MemoryStore.get_context()` retains `history_cap=25_000` as its default for programmatic readers. V1 `ContextBuilder` calls it with the scaled history cap when building fresh session context. V2 reads preference/project anchors without diff --git a/src/kiro_crew/cli_commands.py b/src/kiro_crew/cli_commands.py index 068d0db289e0..1e150da4042b 100644 --- a/src/kiro_crew/cli_commands.py +++ b/src/kiro_crew/cli_commands.py @@ -2457,11 +2457,23 @@ def _learn(args: argparse.Namespace) -> None: jsonl_store = LessonStore() cfg = KiroCrewConfig.load() + action = getattr(args, "learn_action", None) + # Global persistence switch (memory.persistence_enabled): the CLI writes to + # the store directly (no HTTP), so it carries its own check mirroring the + # POST /api/lessons refusal. Refused ahead of the vector store because + # constructing and init-ing it creates or migrates memory.db, which a + # refused write must not do; list and remove still need that store, so the + # check is scoped to the add action. + if action == "add" and not cfg.memory.persistence_enabled: + print( + "Lesson NOT saved: persistent memory is disabled " + "(memory.persistence_enabled is false). Re-enable with: " + "kirocrew config set memory.persistence_enabled true" + ) + return vs = VectorMemoryStore(embedding_dim=cfg.memory.embedding_dim) vs.init() try: - action = getattr(args, "learn_action", None) - if action == "add": rule = args.rule category = args.category diff --git a/src/kiro_crew/config/loader.py b/src/kiro_crew/config/loader.py index 8b744d4326b8..52ed1e2a6c55 100644 --- a/src/kiro_crew/config/loader.py +++ b/src/kiro_crew/config/loader.py @@ -2945,6 +2945,9 @@ def _build_memory_config(memory_data: dict) -> MemoryConfig: history_max_days=_safe_nonnegative_int(memory_data.get("history_max_days", 365), 365), backup_enabled=_safe_bool(memory_data.get("backup_enabled", True), True), backup_keep=_safe_int(memory_data.get("backup_keep", 7), 7, 1, None), + persistence_enabled=_safe_bool(memory_data.get("persistence_enabled", True), True), + inject_memory=_safe_bool(memory_data.get("inject_memory", True), True), + inject_lessons=_safe_bool(memory_data.get("inject_lessons", True), True), migrated=memory_data.get("migrated", False), ) diff --git a/src/kiro_crew/config/sections.py b/src/kiro_crew/config/sections.py index f0dc51e2f546..7af6fcf7359c 100644 --- a/src/kiro_crew/config/sections.py +++ b/src/kiro_crew/config/sections.py @@ -2199,6 +2199,34 @@ class MemoryConfig: "Values below 1 are treated as 1 so retention cannot empty the directory.", ), ) + persistence_enabled: bool = field( + default=True, + metadata=_meta( + "Persistence Enabled", + "Global switch for persistent memory. Off: no automatic memory " + "writes (lessons, consolidation extraction, task-runner lessons) " + "and no stored memory/lessons injected into new sessions; " + "within-conversation context is unaffected. Explicit dashboard " + "edits and deletions stay available. An installed app's own " + "ingestion sweep is out of scope and still writes app-scoped rows.", + ), + ) + inject_memory: bool = field( + default=True, + metadata=_meta( + "Inject Memory Context", + "Inject the stored memory block (preferences, the memory activity " + "index and recent-session snippets) into new-session context. " + "On-demand memory_recall is unaffected.", + ), + ) + inject_lessons: bool = field( + default=True, + metadata=_meta( + "Inject Lessons Context", + "Inject the learned-corrections and user-profile blocks into " "new-session context.", + ), + ) migrated: bool = field( default=False, metadata=_meta("Migrated", "Whether memory has been migrated to vector store."), diff --git a/src/kiro_crew/context.py b/src/kiro_crew/context.py index ed44f9c74023..1afaf8e694d8 100644 --- a/src/kiro_crew/context.py +++ b/src/kiro_crew/context.py @@ -1450,6 +1450,37 @@ def _group_included(groups: frozenset[str] | None, group: str) -> bool: return groups is None or group in groups +def _config_scoped_groups( + context_groups: frozenset[str] | None, cfg: "KiroCrewConfig | None" = None +) -> frozenset[str] | None: + """The caller-passed scope intersected with the operator's config toggles. + + ``memory.inject_memory`` / ``memory.inject_lessons`` (with + ``memory.persistence_enabled`` as the global switch) withhold a group on + EVERY surface. Intersecting here — instead of at each call site — keeps a + new context entry point from silently escaping the config; + ``build_session_context``, the v2 essentials builder and the + post-compaction re-injection all route through this. + Subagent narrowing is preserved: config can only remove groups from the + caller-passed scope, never add one back. Only the memory and lessons groups + are ever subtracted, so a project-group gate reads the caller scope + directly. The ``[CONTEXT SCOPE]`` block + stays keyed to the caller-passed value, because "your parent withheld" + describes per-spawn narrowing, not the operator's standing choice. + """ + if cfg is None: + cfg = KiroCrewConfig.load() + withheld: set[str] = set() + if not (cfg.memory.persistence_enabled and cfg.memory.inject_memory): + withheld.add(CONTEXT_GROUP_MEMORY) + if not (cfg.memory.persistence_enabled and cfg.memory.inject_lessons): + withheld.add(CONTEXT_GROUP_LESSONS) + if not withheld: + return context_groups + base = SWITCHABLE_CONTEXT_GROUPS if context_groups is None else context_groups + return frozenset(base) - withheld + + def _build_context_scope_section(groups: frozenset[str] | None) -> str: """Name the groups a parent withheld, or ``""`` when nothing was withheld. @@ -3346,6 +3377,21 @@ def _build_v2_essentials( template = member_template or template if not owner: return "" + # Same config intersection as build_session_context: this builder is a + # second context entry point, so a group the operator disabled must be + # withheld here too rather than only on the main path. + # + # EXCEPT when profile_overrides is supplied. That argument makes this a + # VALIDATOR (_validate_private_profile_update), not a context build: the + # candidate profile is appended only under the memory-group gate below, + # and render_essentials' combined-budget refusal is what rejects a + # profile that fits per-file but overflows the combined cap. Scoping the + # groups here would drop that gate whenever injection is disabled, so an + # oversized profile would save and then break every later member build. + # A validation pass must see the complete candidate set regardless of + # what the operator currently injects. + if profile_overrides is None: + context_groups = _config_scoped_groups(context_groups) reads = not blocks_reads and _group_included(context_groups, CONTEXT_GROUP_MEMORY) identity = self._build_member_section(owner, strict=True, include_briefing=reads) documents = documents_for_member( @@ -3650,6 +3696,19 @@ def _mark(label: str) -> None: # member capability gate below — one read per context build. _cfg = KiroCrewConfig.load() + # Config-driven injection toggles (memory.inject_memory / + # memory.inject_lessons, with memory.persistence_enabled as the global + # switch): a group the operator disabled is withheld on EVERY surface — + # dashboard, channels, cron, heartbeat, task runner, eval, subagents — + # by intersecting here, the one method all context builds pass through, + # rather than at the eleven call sites that would each have to remember + # to pass ``context_groups``. The caller-passed ``context_groups`` keeps + # driving the [CONTEXT SCOPE] block below: its "Your parent withheld" + # prose describes subagent narrowing, and a config withholding is the + # operator's standing choice, not the parent's per-spawn one, so it is + # deliberately silent there. + effective_groups = _config_scoped_groups(context_groups, _cfg) + if mode == _member_mode and _member_backend_can_dispatch(_cfg): append_required( f"[CREW MEMBER OPERATING MODE]\n" @@ -3715,7 +3774,7 @@ def _mark(label: str) -> None: # the sub-agent reads the scope as framing rather than discovering a gap. append_required(_build_context_scope_section(context_groups)) - if _group_included(context_groups, CONTEXT_GROUP_LESSONS): + if _group_included(effective_groups, CONTEXT_GROUP_LESSONS): profile_ctx = _build_user_profile_section(_cfg) if profile_ctx: parts.append(profile_ctx) @@ -3895,7 +3954,7 @@ def _mark(label: str) -> None: memory = None member_vectors = None if not blocks_reads and any( - _group_included(context_groups, group) + _group_included(effective_groups, group) for group in (CONTEXT_GROUP_MEMORY, CONTEXT_GROUP_LESSONS) ): if private: @@ -3912,7 +3971,7 @@ def _mark(label: str) -> None: ) else: memory = self.get_memory_for(workspace, memory_store) - if not blocks_reads and _group_included(context_groups, CONTEXT_GROUP_MEMORY): + if not blocks_reads and _group_included(effective_groups, CONTEXT_GROUP_MEMORY): if private: append_required( "[Memory tools]\n" @@ -4026,7 +4085,7 @@ def _mark(label: str) -> None: lessons_renderer: Callable[[int], str] | None = None lessons_part_index: int | None = None if (memory is not None or member_vectors is not None) and _group_included( - context_groups, CONTEXT_GROUP_LESSONS + effective_groups, CONTEXT_GROUP_LESSONS ): # V1 only: the JSONL store answers when the vector store is absent OR not yet # populated, and stays silent once it holds lessons. @@ -4128,7 +4187,7 @@ def _render_default_jsonl_lessons(hard_cap: int) -> str: session_key and self.conversation_log and not blocks_reads - and _group_included(context_groups, CONTEXT_GROUP_MEMORY) + and _group_included(effective_groups, CONTEXT_GROUP_MEMORY) ): provenance = self.conversation_log.recent_with_provenance( session_key, exclude_last_n=exclude_last_n @@ -4634,7 +4693,13 @@ def build_message( # mapping excludes and an unmapped custom agent cannot receive a block # its session-start context never contained. if not is_new_session and needs_reinjection: - if not blocks_reads and _group_included(context_groups, CONTEXT_GROUP_MEMORY): + # The stored-memory half routes through the same config intersection + # as the session-start build: this path restores a block that build + # withheld, so reading the caller scope alone would hand back the + # activity index the operator's inject_memory setting excluded. + if not blocks_reads and _group_included( + _config_scoped_groups(context_groups), CONTEXT_GROUP_MEMORY + ): memory = self.get_memory_for(workspace, memory_store) parts.append(_neutralize_structural_markers(memory.activity_index())) parts.append( diff --git a/src/kiro_crew/dashboard/handlers/cron.py b/src/kiro_crew/dashboard/handlers/cron.py index 4e6d8841a2d6..77728b3e23af 100644 --- a/src/kiro_crew/dashboard/handlers/cron.py +++ b/src/kiro_crew/dashboard/handlers/cron.py @@ -2507,6 +2507,30 @@ async def api_lessons_create(request: web.Request) -> web.Response: {"error": "Memory writes are not allowed in this session mode."}, status=403, ) + # Global persistence switch (memory.persistence_enabled). + # Enforced on the route rather than in the learn_add MCP handler so every + # transport that posts here (MCP tool, dashboard, direct HTTP) is covered + # by the one check. Reads and deletions stay available — the right to + # forget survives the switch. + if not KiroCrewConfig.load().memory.persistence_enabled: + _sel().log_api_access( + caller=sk, + operation="learn_add", + outcome="denied", + source="dashboard", + resources="persistence_disabled", + error="Persistent memory is disabled (memory.persistence_enabled).", + ) + return web.json_response( + { + "error": "Lesson was NOT saved: persistent memory is disabled " + "(memory.persistence_enabled is false). Re-enable it with " + "`kirocrew config set memory.persistence_enabled true` to save " + "lessons again.", + "code": "persistence_disabled", + }, + status=403, + ) # Validate body fields against the SAME schema the learn_add MCP tool uses # (LEARN_ADD_SCHEMA), so REST and tool paths share one source of truth: # rule must be a string (bounded to MAX_SHORT_STRING), category/scope are diff --git a/src/kiro_crew/dashboard/handlers/memory.py b/src/kiro_crew/dashboard/handlers/memory.py index 74ad8552ba6f..d5c30f6a0a3c 100644 --- a/src/kiro_crew/dashboard/handlers/memory.py +++ b/src/kiro_crew/dashboard/handlers/memory.py @@ -2110,6 +2110,29 @@ async def api_memory_consolidate(request: web.Request) -> web.Response: return gate if not state.consolidator: return web.json_response({"error": "consolidator not available"}, status=503) + # Global persistence switch (memory.persistence_enabled). The + # inner _consolidate gate would refuse anyway; refusing here tells the + # dashboard caller WHY instead of returning a generic refusal, and spends + # no transcript read on a request that cannot proceed. The denial is + # SEL-recorded: the request passed identity and the write gate, so the + # refusal is a config-state decision an audit trail has to show rather than + # an unauthenticated caller being turned away upstream. + if not KiroCrewConfig.load().memory.persistence_enabled: + _sel().log_api_access( + caller=request.headers.get("X-Session-Key", ""), + operation="memory.consolidate", + outcome="denied", + source="dashboard", + resources="persistence_disabled", + ) + return web.json_response( + { + "error": "Consolidation is paused: persistent memory is disabled " + "(memory.persistence_enabled is false).", + "code": "persistence_disabled", + }, + status=403, + ) body, body_err = await read_bounded_json(request, max_bytes=None) if body_err is not None: return body_err diff --git a/src/kiro_crew/docs/configuration.md b/src/kiro_crew/docs/configuration.md index 9bbcc403928a..1ab3c5b42d70 100644 --- a/src/kiro_crew/docs/configuration.md +++ b/src/kiro_crew/docs/configuration.md @@ -201,7 +201,10 @@ Set via `kirocrew config set agent.acp_backend kas`. "embedding_provider": "llama_cpp", "embedding_dim": 1024, "history_idle_hours": 3.0, - "history_max_days": 365 + "history_max_days": 365, + "persistence_enabled": true, + "inject_memory": true, + "inject_lessons": true }, "skills": { "max_triggered": 0 @@ -439,6 +442,9 @@ them, so there is no enable switch here: only knobs for *which* model runs. | `memory.history_max_days` | Days of history to retain before pruning | `365` | | `memory.backup_enabled` | Periodic rotating backups of every active memory store (the default store, named V1 stores and member V2 stores); retention does not delete active memories | `true` | | `memory.backup_keep` | Backup copies retained per store, with a minimum of one | `7` | +| `memory.persistence_enabled` | Global switch for persistent memory. Off: no automatic memory writes anywhere — `learn_add` and `kirocrew learn add` refuse, history consolidation pauses entirely (no LLM turn spent), task-runner lesson extraction skips — and stored memory/lessons are not injected into new sessions. Within-conversation context is unaffected, and explicit dashboard edits/deletions (the right to forget) stay available. One documented exception: an installed app's own ingestion sweep (Ops Mission Control's ledger import) still writes app-scoped episodic rows, because it is reached only through that app's trigger | `true` | +| `memory.inject_memory` | Inject the stored memory block (preferences, the memory activity index, recent-session snippets) into new-session context, including the re-injection after a compaction. On-demand `memory_recall` and writes are unaffected | `true` | +| `memory.inject_lessons` | Inject the learned-corrections and user-profile blocks into new-session context. Writes are unaffected | `true` | Decay, episodic capacity eviction and history age pruning apply to V1 only. V2 keeps memory until explicit correction, replacement, forgetting or restoration. diff --git a/src/kiro_crew/history_consolidation.py b/src/kiro_crew/history_consolidation.py index 808bda68a9c2..8d9b1fbfe451 100644 --- a/src/kiro_crew/history_consolidation.py +++ b/src/kiro_crew/history_consolidation.py @@ -176,6 +176,24 @@ class _ConsolidationNotDispatched(Exception): """A consolidation prompt never reached the provider.""" +def _persistence_disabled() -> bool: + """True when the operator turned persistent memory off. + + ``memory.persistence_enabled`` is the global persistence switch: consolidation + is the largest automatic writer (lessons, semantic, episodic, preferences, + projects, history, auto-skills all flow from one pass), so a disabled + system must not schedule it — pausing entirely rather than run-and-discard, + so no LLM turn is ever billed for output that would be thrown away. + Read through ``KiroCrewConfig.load()`` (fingerprint-cached, so per-turn + checks cost a stat) rather than a constructor flag, so flipping the key + takes effect without a gateway restart. Imported lazily to keep this + module's import graph light (same rationale as the facade seams above). + """ + from kiro_crew.config.loader import KiroCrewConfig + + return not KiroCrewConfig.load().memory.persistence_enabled + + def _fmt_message(message: dict) -> str: """Render one transcript message for a consolidation prompt.""" tools = f" [tools: {', '.join(message['tools'])}]" if message.get("tools") else "" @@ -696,6 +714,8 @@ async def _note_environment_failure(self, key: str, reason: str) -> None: def maybe_consolidate(self, key: str) -> None: """Fire preferences/projects consolidation if message threshold exceeded.""" self._last_activity[key] = _time.time() + if _persistence_disabled(): + return if key in self._running: return total = len(self._log._read_messages(key)) @@ -733,6 +753,8 @@ def _on_done(fut: asyncio.Task, k: str = key, off: int = total) -> None: # type def check_idle_sessions(self) -> None: """Check all tracked sessions for idle-based history consolidation.""" + if _persistence_disabled(): + return now = _time.time() for key, last in list(self._last_activity.items()): if now - last < self._history_idle_secs: @@ -787,6 +809,8 @@ def consolidate_session(self, key: str) -> None: """ if key in self._running: return + if _persistence_disabled(): + return total, unconsolidated = self._log.consolidation_counts(key) if unconsolidated < 1: return @@ -875,6 +899,25 @@ async def _consolidate( # itself raised, and that path is not billed. attempted = AttemptedSpan(0, 0, 0) try: + # Persistence global switch, checked here as well as in the automatic + # entry points so the manual triggers (POST /api/memory/consolidate, + # ``kirocrew consolidate``) are covered too. The REFUSED sentinel + # gives the entry-point done-callbacks the right semantics for free: + # no pass ran, so offsets must not advance and throttles must not be + # set. + # + # INSIDE the try, so the finally below clears self._running. The + # entry points add the key before scheduling this task and their + # done-callbacks never discard it, so returning ahead of the try + # would strand the key and refuse every later consolidation for that + # session — reachable when the switch is flipped off in the gap + # between create_task and the task's first line. + if _persistence_disabled(): + self._logger.info( + "consolidation skipped for %s: memory.persistence_enabled is false", key + ) + return _CONSOLIDATION_REFUSED + from kiro_crew.execution_context import read_session_execution from kiro_crew.history import is_incognito_transcript diff --git a/src/kiro_crew/taskrunner.py b/src/kiro_crew/taskrunner.py index 8082d5f2c9f5..3f36508a62a7 100644 --- a/src/kiro_crew/taskrunner.py +++ b/src/kiro_crew/taskrunner.py @@ -2617,6 +2617,12 @@ def _report(fut: "asyncio.Future[None]") -> None: # ── Learn from Failures ── async def _extract_lesson(self, task: Task, run: Project | None = None) -> None: + # Global persistence switch (memory.persistence_enabled): + # skip BEFORE the LLM call, so a disabled system spends no turn + # distilling a lesson it is not allowed to store. Placed ahead of store + # resolution so member-private lesson stores are covered too. + if not KiroCrewConfig.load().memory.persistence_enabled: + return try: from kiro_crew.memory_stores import UnknownMemoryStore diff --git a/test/test_config_loader.py b/test/test_config_loader.py index cdde43ff17d5..07d033878914 100644 --- a/test/test_config_loader.py +++ b/test/test_config_loader.py @@ -4642,6 +4642,39 @@ def test_new_keys_are_dashboard_editable(self) -> None: assert key in _EDITABLE_CONFIG, key +class TestMemoryPersistenceAndInjectionToggles: + """``memory.persistence_enabled`` / ``inject_memory`` / ``inject_lessons``: + default on, override reads, junk falls back to the default (``_safe_bool`` + accepts only real booleans).""" + + def test_defaults_are_on(self) -> None: + mc = _load_from_dict({}).memory + assert (mc.persistence_enabled, mc.inject_memory, mc.inject_lessons) == ( + True, + True, + True, + ) + + def test_an_empty_memory_section_leaves_every_toggle_on(self) -> None: + mc = _load_from_dict({"memory": {}}).memory + assert (mc.persistence_enabled, mc.inject_memory, mc.inject_lessons) == ( + True, + True, + True, + ) + + @pytest.mark.parametrize("key", ["persistence_enabled", "inject_memory", "inject_lessons"]) + def test_false_reads_false(self, key: str) -> None: + mc = _load_from_dict({"memory": {key: False}}).memory + assert getattr(mc, key) is False + + @pytest.mark.parametrize("key", ["persistence_enabled", "inject_memory", "inject_lessons"]) + @pytest.mark.parametrize("bad", ["false", 0, 1, None, [], {}]) + def test_junk_falls_back_to_on(self, key: str, bad: object) -> None: + mc = _load_from_dict({"memory": {key: bad}}).memory + assert getattr(mc, key) is True + + class TestKnowledgePoolIdleTtl: """``knowledge.pool_idle_ttl_secs`` parsing: default, override, explicit 0, and rejection of negative / bool / typed-wrong values back to the default.""" diff --git a/test/test_memory_context_toggles.py b/test/test_memory_context_toggles.py new file mode 100644 index 000000000000..b79051841501 --- /dev/null +++ b/test/test_memory_context_toggles.py @@ -0,0 +1,473 @@ +"""Config-driven memory/lessons injection toggles and the global persistence switch. + +Two features share one mechanism: + +* ``memory.inject_memory`` / ``memory.inject_lessons`` withhold the stored-memory + and lessons blocks on the MAIN path — ``context_groups=None``, which every + non-subagent surface passes — where the spawn-time ``include_*`` flags never + reach. The intersection happens inside ``build_session_context``, so every + surface obeys the config without passing anything. +* ``memory.persistence_enabled`` is the global switch: off withholds BOTH groups + regardless of the ``inject_*`` values, and stops the automatic writers. The + writer gates tested here are consolidation (all three automatic entry points + plus ``_consolidate`` itself, which the manual REST/CLI triggers call), the + ``POST /api/lessons`` route (the enforcement point behind the ``learn_add`` + MCP tool), and the ``kirocrew learn add`` CLI's direct store write. + +The builder fixture mirrors ``test_subagent_context_groups._builder``: every +group's content is populated, because an "absent" assertion against an empty +store passes with the gate deleted. +""" + +from __future__ import annotations + +import argparse +import asyncio +import contextlib +import json +from types import SimpleNamespace +from typing import Any +from unittest.mock import AsyncMock, MagicMock, patch + +import pytest + +from kiro_crew import history as history_mod +from kiro_crew.config.loader import config_dir, config_path +from kiro_crew.context import ( + CONTEXT_GROUP_MEMORY, + CONTEXT_GROUP_PROJECT, + SWITCHABLE_CONTEXT_GROUPS, + ContextBuilder, + _config_scoped_groups, +) +from kiro_crew.history import ConversationLog, HistoryConsolidator +from kiro_crew.history_consolidation import _CONSOLIDATION_REFUSED +from kiro_crew.learn import Lesson, LessonStore +from kiro_crew.memory import MemoryStore +from kiro_crew.skills import SkillsLoader + +ALL_GROUPS = frozenset(SWITCHABLE_CONTEXT_GROUPS) + +KEY = "dashboard:chat-toggles" + + +def _write_config(memory_overrides: dict[str, Any] | None = None) -> None: + """Write the isolated home's config.json. + + Always carries the onboarding answer that puts a [USER PROFILE] block in + the lessons group, so the lessons-absent assertions have real content to + be absent. + """ + data: dict[str, Any] = {"dashboard": {"user_role": "developer"}} + if memory_overrides: + data["memory"] = memory_overrides + p = config_path() + p.parent.mkdir(parents=True, exist_ok=True) + p.write_text(json.dumps(data), encoding="utf-8") + + +def _builder(tmp_path) -> ContextBuilder: + """A builder with the memory and lessons groups' content populated.""" + memory = MemoryStore(workspace=tmp_path / "ws") + memory.write_preferences("# User Preferences\n\n- Prefers tabs over spaces\n") + memory.write_projects("# Active Projects\n\n- Ship the widget rewrite\n") + lessons = LessonStore(base_dir=tmp_path) + lessons.save( + Lesson(ts="2026-01-01T00:00:00Z", rule="Always pass encoding=utf-8", category="tool") + ) + return ContextBuilder( + memory=memory, + skills=SkillsLoader(skills_path=tmp_path / "skills", install_builtins=False), + lessons=lessons, + ) + + +class TestInjectMemoryToggle: + def test_present_by_default(self, tmp_path): + _write_config() + ctx = _builder(tmp_path).build_session_context() + assert "Prefers tabs over spaces" in ctx + assert "[Memory tools]" in ctx + + def test_absent_when_disabled(self, tmp_path): + _write_config({"inject_memory": False}) + ctx = _builder(tmp_path).build_session_context() + assert "Prefers tabs over spaces" not in ctx + assert "[Memory tools]" not in ctx + + def test_lessons_unaffected(self, tmp_path): + _write_config({"inject_memory": False}) + ctx = _builder(tmp_path).build_session_context() + assert "Always pass encoding=utf-8" in ctx + + def test_config_withholding_is_silent(self, tmp_path): + """No [CONTEXT SCOPE] marker: "your parent withheld" describes subagent + narrowing, and a config withholding is the operator's standing choice.""" + _write_config({"inject_memory": False, "inject_lessons": False}) + ctx = _builder(tmp_path).build_session_context() + assert "[CONTEXT SCOPE]" not in ctx + + +class TestPostCompactionReinjection: + """The re-injection after a compaction restores what session start withheld. + + ``build_message`` re-injects the stored-memory activity index once a + compaction drops the session-start context. That is the same block + ``build_session_context`` gates, so it routes through the same config + intersection — reading the caller scope alone would hand back the memory an + operator's ``inject_memory: false`` excluded, for the rest of the session. + """ + + MARKER = "Call memory_recall with specific keywords" + + def _reinjected(self, tmp_path) -> str: + message, _ = _builder(tmp_path).build_message( + "hi", is_new_session=False, needs_reinjection=True + ) + return message + + def test_present_by_default(self, tmp_path): + _write_config() + assert self.MARKER in self._reinjected(tmp_path) + + def test_absent_when_memory_injection_is_disabled(self, tmp_path): + _write_config({"inject_memory": False}) + assert self.MARKER not in self._reinjected(tmp_path) + + def test_absent_when_the_global_switch_is_off(self, tmp_path): + _write_config({"persistence_enabled": False}) + assert self.MARKER not in self._reinjected(tmp_path) + + +class TestInjectLessonsToggle: + def test_absent_when_disabled(self, tmp_path): + _write_config({"inject_lessons": False}) + ctx = _builder(tmp_path).build_session_context() + assert "Always pass encoding=utf-8" not in ctx + assert "[USER PROFILE]" not in ctx + + def test_memory_unaffected(self, tmp_path): + _write_config({"inject_lessons": False}) + ctx = _builder(tmp_path).build_session_context() + assert "Prefers tabs over spaces" in ctx + + +class TestPersistenceGlobalSwitchInjection: + def test_withholds_both_groups(self, tmp_path): + _write_config({"persistence_enabled": False, "inject_memory": True, "inject_lessons": True}) + ctx = _builder(tmp_path).build_session_context() + assert "Prefers tabs over spaces" not in ctx + assert "Always pass encoding=utf-8" not in ctx + assert "[USER PROFILE]" not in ctx + + def test_conduct_and_workspace_survive(self, tmp_path): + """Within-conversation context is out of scope; only stored blocks go.""" + _write_config({"persistence_enabled": False}) + ctx = _builder(tmp_path).build_session_context() + assert "[CRITICAL RULES" in ctx + assert "[WORKSPACE IDENTITY]" in ctx + + +class TestConfigIntersectsSubagentScope: + def test_config_wins_over_an_explicit_full_scope(self, tmp_path): + """A parent granting every group cannot re-enable a config-disabled one.""" + _write_config({"inject_memory": False}) + ctx = _builder(tmp_path).build_session_context(context_groups=ALL_GROUPS) + assert "Prefers tabs over spaces" not in ctx + + def test_subagent_narrowing_survives_config_all_on(self, tmp_path): + _write_config() + ctx = _builder(tmp_path).build_session_context( + context_groups=ALL_GROUPS - {CONTEXT_GROUP_MEMORY} + ) + assert "Prefers tabs over spaces" not in ctx + # The parent's withholding is still announced. + assert "[CONTEXT SCOPE]" in ctx + + +class TestConfigScopedGroupsHelper: + """The shared intersection every context entry point routes through. + + Asserted directly because it now has two callers (``build_session_context`` + and the v2 essentials builder): a regression in one caller is caught by the + section tests above, but a regression in the helper itself would surface as + two unrelated failures without naming the cause. + """ + + def test_all_on_passes_the_caller_scope_through_unchanged(self, tmp_path): + _write_config() + assert _config_scoped_groups(None) is None + assert _config_scoped_groups(ALL_GROUPS) == ALL_GROUPS + + def test_a_disabled_group_is_removed_from_an_implicit_full_scope(self, tmp_path): + _write_config({"inject_memory": False}) + assert _config_scoped_groups(None) == ALL_GROUPS - {CONTEXT_GROUP_MEMORY} + + def test_the_global_switch_removes_both_memory_and_lessons(self, tmp_path): + _write_config({"persistence_enabled": False}) + assert _config_scoped_groups(None) == frozenset({CONTEXT_GROUP_PROJECT}) + + def test_config_can_only_subtract_never_restore(self, tmp_path): + """A group the parent withheld stays withheld however config reads.""" + _write_config() + parent = ALL_GROUPS - {CONTEXT_GROUP_MEMORY} + assert CONTEXT_GROUP_MEMORY not in (_config_scoped_groups(parent) or ALL_GROUPS) + + +class TestValidationPathIgnoresInjectionConfig: + """A profile-validation pass must see the complete candidate set. + + ``_build_v2_essentials`` doubles as the validator behind a private-profile + update (``profile_overrides`` supplied): the candidate is appended only + under the memory-group gate, and ``render_essentials`` raising on the + combined budget is what rejects a profile that fits per-file but overflows + combined. If config scoping applied there, disabling injection would skip + that refusal, save an oversized profile, and break every later member + context build for that member. + + The builder is driven past ``member_context_identity`` with a stubbed owner, + since an unresolvable store returns before the scoping decision is ever made + — which is what makes the assertion about that decision meaningful. + """ + + def _scoping_calls(self, tmp_path, monkeypatch, **kwargs) -> list[object]: + """Drive the builder and report what the scoping helper was asked.""" + builder = _builder(tmp_path) + calls: list[object] = [] + monkeypatch.setattr( + "kiro_crew.member_essential_context.member_context_identity", + lambda member, *, member_is_id=True: ("alice", "template"), + ) + monkeypatch.setattr( + "kiro_crew.context._config_scoped_groups", + lambda groups, cfg=None: calls.append(groups) or groups, + ) + # Downstream rendering needs a fully provisioned V2 member; this test is + # about the scoping decision, which is made before any of it. + with contextlib.suppress(Exception): + builder._build_v2_essentials("store-alice", **kwargs) + return calls + + def test_scoping_is_skipped_when_profile_overrides_is_supplied(self, tmp_path, monkeypatch): + _write_config({"persistence_enabled": False}) + calls = self._scoping_calls( + tmp_path, monkeypatch, profile_overrides={"USER.md": "candidate"} + ) + assert calls == [], "config scoping must not run on a validation pass" + + def test_a_context_build_still_consults_config(self, tmp_path, monkeypatch): + """The control: without profile_overrides the scoping still applies.""" + _write_config({"persistence_enabled": False}) + calls = self._scoping_calls(tmp_path, monkeypatch) + assert calls == [None] + + +# --------------------------------------------------------------------------- +# Writer gates +# --------------------------------------------------------------------------- + + +def _seed_log(tmp_path, key: str = KEY, count: int = 40) -> ConversationLog: + """A real transcript with enough messages to clear the 30-message threshold.""" + log = ConversationLog(base_dir=tmp_path / "sessions") + log.init() + with history_mod.allow_on_loop_persist(): + for i in range(count): + log.append(key, "user", f"m{i}") + return log + + +def _make_consolidator(log: ConversationLog, **kw: Any) -> HistoryConsolidator: + memory = MagicMock() + memory.read_preferences.return_value = "" + memory.read_projects.return_value = "" + kw.setdefault("history_idle_secs", 0) + kw.setdefault("sessions", None) + return HistoryConsolidator(log=log, memory=memory, migrated=True, **kw) + + +class TestConsolidationGate: + @pytest.mark.asyncio + async def test_maybe_consolidate_schedules_nothing_when_disabled(self, tmp_path): + _write_config({"persistence_enabled": False}) + cons = _make_consolidator(_seed_log(tmp_path)) + with patch.object(HistoryConsolidator, "_consolidate", new=AsyncMock()) as run: + cons.maybe_consolidate(KEY) + await asyncio.sleep(0) + run.assert_not_awaited() + + @pytest.mark.asyncio + async def test_maybe_consolidate_fires_when_enabled(self, tmp_path): + """The threshold fixture really clears the gate — the disabled assertion + above is about the switch, not about an under-seeded transcript.""" + _write_config() + cons = _make_consolidator(_seed_log(tmp_path)) + with patch.object( + HistoryConsolidator, "_consolidate", new=AsyncMock(return_value=None) + ) as run: + cons.maybe_consolidate(KEY) + await asyncio.gather(*cons._tasks) + run.assert_awaited() + + @pytest.mark.asyncio + async def test_idle_sweep_schedules_nothing_when_disabled(self, tmp_path): + _write_config({"persistence_enabled": False}) + cons = _make_consolidator(_seed_log(tmp_path)) + cons._last_activity[KEY] = 0.0 # long idle + with patch.object(HistoryConsolidator, "_consolidate", new=AsyncMock()) as run: + cons.check_idle_sessions() + await asyncio.sleep(0) + run.assert_not_awaited() + + @pytest.mark.asyncio + async def test_session_end_schedules_nothing_when_disabled(self, tmp_path): + _write_config({"persistence_enabled": False}) + cons = _make_consolidator(_seed_log(tmp_path)) + with patch.object(HistoryConsolidator, "_consolidate", new=AsyncMock()) as run: + cons.consolidate_session(KEY) + await asyncio.sleep(0) + run.assert_not_awaited() + + @pytest.mark.asyncio + async def test_consolidate_itself_refuses_covering_manual_triggers(self, tmp_path): + """POST /api/memory/consolidate and the CLI call _consolidate directly; + the REFUSED sentinel keeps offsets unadvanced and throttles unset.""" + _write_config({"persistence_enabled": False}) + cons = _make_consolidator(_seed_log(tmp_path)) + assert await cons._consolidate(KEY) is _CONSOLIDATION_REFUSED + + @pytest.mark.asyncio + async def test_a_refusal_releases_the_running_claim(self, tmp_path): + """The refusal must not strand the key in ``_running``. + + The automatic entry points claim the key BEFORE scheduling the task and + their done-callbacks never discard it — ``_consolidate``'s own ``finally`` + is the only clearer. A refusal that returned ahead of that ``try`` would + leave the key claimed forever, so every later consolidation for the + session is refused by the ``key in self._running`` guard even after + persistence is switched back on. Reachable whenever the switch is + flipped off between ``create_task`` and the task's first line. + """ + _write_config({"persistence_enabled": False}) + cons = _make_consolidator(_seed_log(tmp_path)) + cons._running.add(KEY) # what the entry points do before scheduling + assert await cons._consolidate(KEY) is _CONSOLIDATION_REFUSED + assert KEY not in cons._running, "a refused pass left the session claimed" + + +class TestLessonsRouteGate: + @pytest.mark.asyncio + async def test_post_api_lessons_refuses_when_disabled(self, tmp_path): + _write_config({"persistence_enabled": False}) + from kiro_crew.dashboard.handlers import cron as cron_mod + + req = MagicMock() + req.app = {"state": MagicMock()} + req.headers = {"X-Session-Key": "dashboard:ui"} + + async def _body(request, **_kw): + return {"rule": "r", "category": "tool"}, None + + with ( + patch.object(cron_mod, "read_bounded_json", side_effect=_body), + patch.object(cron_mod, "_recognize_session", new=AsyncMock(return_value=None)), + patch.object(cron_mod, "_is_restricted_session", return_value=False), + patch.object(cron_mod, "_sel", return_value=MagicMock()), + ): + resp = await cron_mod.api_lessons_create(req) + assert resp.status == 403 + assert "persistence_enabled" in resp.text + + +class TestConsolidateRouteGate: + @pytest.mark.asyncio + async def test_consolidate_route_refuses_and_audits_when_disabled(self, tmp_path): + """The refusal is SEL-recorded. + + The request has already passed identity and the write gate by this + point, so the refusal is a config-state decision about an authorized + caller — exactly what an audit trail has to show. An unrecorded denial + leaves no trace that the switch turned work away. + """ + _write_config({"persistence_enabled": False}) + from kiro_crew.dashboard.handlers import memory as memory_mod + + req = MagicMock() + req.app = {"state": MagicMock()} + req.headers = {"X-Session-Key": "dashboard:ui"} + sel = MagicMock() + with ( + patch.object( + memory_mod, + "resolve_lesson_memory_store", + new=AsyncMock(return_value=(None, None)), + ), + patch.object(memory_mod, "_memory_write_gate", new=AsyncMock(return_value=None)), + patch.object(memory_mod, "_sel", return_value=sel), + ): + resp = await memory_mod.api_memory_consolidate(req) + assert resp.status == 403 + assert "persistence_enabled" in resp.text + assert sel.log_api_access.called, "a persistence denial must be SEL-recorded" + kwargs = sel.log_api_access.call_args.kwargs + assert kwargs["outcome"] == "denied" + assert kwargs["resources"] == "persistence_disabled" + + +class TestLearnCliGate: + def test_learn_add_refuses_when_disabled(self, tmp_path, capsys): + _write_config({"persistence_enabled": False}) + from kiro_crew import cli_commands + + args = argparse.Namespace( + learn_action="add", rule="Always frobnicate", category="tool", negative=None + ) + cli_commands._learn(args) + out = capsys.readouterr().out + assert "NOT saved" in out + assert "persistence_enabled" in out + # Nothing reached either store. + assert LessonStore().load_all() == [] + # And no store was opened on the way to the refusal: constructing the + # vector store creates or migrates memory.db, which a write the switch + # refuses must not leave behind. + assert not (config_dir() / "memory.db").exists() + + +class TestTaskrunnerLessonGate: + """The task runner's lesson extractor stops before it spends an LLM turn. + + Asserted behaviourally, because the inventory ratchet cannot carry this one: + its check is that the module mentions ``persistence_enabled`` at all, and the + guard's own comment names the key, so a deleted guard leaves that token in + place and the cheap check stays green. Reaching ``_capture_execution`` — the + first thing the extractor touches past the switch — is the witness. + """ + + async def _capture_reached(self) -> bool: + """Drive the extractor on a stub and report whether it ran past the gate.""" + from kiro_crew.taskrunner import TaskRunner + + reached = False + + def _capture() -> SimpleNamespace: + nonlocal reached + reached = True + # Ephemeral, so the enabled path returns at its own mode check + # instead of resolving a store or calling the model. + return SimpleNamespace(memory_mode="ephemeral", member_id=None) + + stub = SimpleNamespace(_capture_execution=_capture, _lesson_store=None, _ctx=None) + await TaskRunner._extract_lesson(stub, SimpleNamespace(title="t", error="boom")) + return reached + + @pytest.mark.asyncio + async def test_extraction_is_skipped_when_disabled(self, tmp_path): + _write_config({"persistence_enabled": False}) + assert await self._capture_reached() is False + + @pytest.mark.asyncio + async def test_extraction_runs_when_enabled(self, tmp_path): + """The control: the gate is what stopped it above, not the stub.""" + _write_config() + assert await self._capture_reached() is True diff --git a/test/test_persistence_writer_inventory.py b/test/test_persistence_writer_inventory.py new file mode 100644 index 000000000000..3a1c4e6c89ee --- /dev/null +++ b/test/test_persistence_writer_inventory.py @@ -0,0 +1,181 @@ +"""Every automatic persistent-memory writer is gated by the persistence switch. + +``memory.persistence_enabled: false`` promises "no automatic memory writes". +The read side has a chokepoint (``_config_scoped_groups`` inside +``build_session_context``), but the write side cannot have one: the gate must +sit at the *service boundary* rather than in the storage layer, because +explicit dashboard edits and deletions have to keep working while the switch is +off — a store-layer refusal would take away the right to forget along with the +automatic capture. + +The cost of that shape is drift: a future automatic writer can call +``write_lesson()`` / ``write_episodic()`` / ``write_preferences()`` directly and +persist while the operator's config says memory is off, silently breaking the +promise. This module is the ratchet that makes such a writer go red instead of +shipping: it walks the source for calls to the persistent-write primitives and +requires every enclosing function to be classified below. A new call site lands +in no bucket and fails, forcing an explicit decision about whether it needs the +gate. + +It pins the INVENTORY, not the gate mechanism — that each gate actually refuses +is asserted in ``test_memory_context_toggles.py``. +""" + +from __future__ import annotations + +import ast +from pathlib import Path + +import pytest + +SRC = Path(__file__).resolve().parents[1] / "src" / "kiro_crew" + +#: Methods that commit something to a persistent memory store. +_WRITE_PRIMITIVES = frozenset( + { + "write_lesson", + "write_episodic", + "set_semantic", + "set_semantic_if_absent", + "write_preferences", + "write_projects", + "append_history", + "save_or_enrich", + } +) + +#: Automatic writers that MUST be behind the persistence switch, as +#: ``::``. Each is +#: gated at its entry point; see test_memory_context_toggles.py for the +#: behavioural assertions. +GATED_AUTOMATIC = { + # One consolidation pass writes lessons, semantic, episodic, preferences and + # projects. Gated in _consolidate plus all three automatic entry points. + "history_consolidation.py::_save_lessons", + "history_consolidation.py::_write_structured_memory", + "history_consolidation.py::_consolidate", + # Distils a lesson from a repeatedly failing task; gated before the LLM call. + "taskrunner.py::_extract_lesson", + # The enforcement point behind the learn_add MCP tool: every transport that + # posts a lesson (tool, dashboard, direct HTTP) is refused by this one check. + "dashboard/handlers/cron.py::api_lessons_create", +} + +#: Writers that are deliberately NOT gated, each with the reason it is exempt. +#: An entry here is a decision, not an oversight — changing one means changing +#: what the switch promises. +EXEMPT = { + # The user's own explicit edits and the right to forget survive the switch. + "dashboard/handlers/memory.py::api_memory_preferences": "explicit user edit", + "dashboard/handlers/memory.py::api_memory_projects": "explicit user edit", + "dashboard/handlers/memory.py::write_preferences": "explicit user edit", + "dashboard/handlers/memory.py::write_projects": "explicit user edit", + # An explicit semantic-memory write from the dashboard editor, like the + # preference/project edits above. + "dashboard/handlers/memory.py::api_memory_semantic_write": "explicit user edit", + # Internal helpers: they hold no policy and are reached through the callers + # above, which are classified. + "memory.py::add_preference": "internal helper, caller decides", + "memory.py::append_history": "storage facade, caller decides", + "memory.py::write": "internal helper, caller decides", + # Store-internal writes. The storage layer is deliberately NOT the gate (see + # the module docstring): these are reached from the classified entry points + # or from an explicit migration/import the user asked for. + "vector_memory.py::write_lesson": "storage primitive", + "vector_memory.py::seed_item_if_absent": "storage primitive", + "vector_memory.py::import_memory": "explicit user-initiated import", + "vector_memory.py::migrate_from_markdown": "explicit one-shot migration", + "vector_memory.py::promote_episodic_patterns": "explicit dashboard action", + # A one-shot, user-initiated import (merge-only, never tombstones). + "onboarding_import.py::_write_instruction": "user-initiated one-shot import", + "onboarding_import.py::_write_memory": "user-initiated one-shot import", + # Harnesses that seed their own isolated workspace, never the live store. + "eval/runner.py::_seed_profile": "eval harness, isolated workspace", + "eval/bench/ingest.py::ingest_instance": "bench harness, isolated workspace", + "eval/bench/member_v2.py::_insert": "bench harness, isolated workspace", + "eval/bench/member_v2.py::_edge_report": "bench harness, isolated workspace", + # The CLI's own explicit `kirocrew learn add`, which carries its own check + # (a refusal message rather than a silent skip) — see cli_commands. + "cli_commands.py::_learn": "carries its own refusal", + # KNOWN GAP, flagged for a maintainer ruling rather than silently widened: + # an installed app's sweep projects its ops ledger into the shared episodic + # store, so it writes while the switch is off. It is app-scoped and reached + # only through that app's own trigger, but "no automatic memory writes" does + # not currently hold for it. + "apps/builtins/ops_mission_control/backend/ledger_index.py::import_pending": ( + "app-scoped sweep; ungated pending a ruling" + ), +} + + +def _writer_sites() -> set[str]: + """Every ``::`` in the package that reaches a write primitive. + + 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 a scanner that only looked at call targets would miss exactly + the paths most likely to skip a gate. + """ + found: set[str] = set() + for path in SRC.rglob("*.py"): + rel = path.relative_to(SRC).as_posix() + if rel.startswith("_vendor/"): + continue + try: + tree = ast.parse(path.read_text(encoding="utf-8")) + except (OSError, SyntaxError): # pragma: no cover - unreadable source + continue + for node in ast.walk(tree): + if not isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)): + continue + for inner in ast.walk(node): + if isinstance(inner, ast.Attribute) and inner.attr in _WRITE_PRIMITIVES: + found.add(f"{rel}::{node.name}") + return found + + +def test_every_persistent_writer_is_classified() -> None: + """A new write site must be gated or explicitly exempted, never neither.""" + classified = GATED_AUTOMATIC | set(EXEMPT) + unclassified = sorted(site for site in _writer_sites() if site not in classified) + assert not unclassified, ( + "New persistent-memory write site(s) that are neither gated by " + "memory.persistence_enabled nor listed as exempt:\n " + + "\n ".join(unclassified) + + "\n\nAn automatic writer must check the switch (see " + "history_consolidation._persistence_disabled or " + "taskrunner._extract_lesson) and be added to GATED_AUTOMATIC. A writer " + "that legitimately runs while persistence is off (an explicit user " + "edit, a storage primitive, a test harness) goes in EXEMPT with its " + "reason. Do NOT add an entry to silence this without deciding which " + "one it is." + ) + + +def test_the_register_has_no_stale_entries() -> None: + """Every classified site must still exist in the source. + + Without this the register accumulates dead names, and a genuinely new writer + can then collide with one and be waved through. + """ + sites = _writer_sites() + stale = sorted(site for site in (GATED_AUTOMATIC | set(EXEMPT)) if site not in sites) + assert ( + not stale + ), "Register entries with no matching write site (renamed or deleted):\n " + "\n ".join(stale) + + +@pytest.mark.parametrize("site", sorted(GATED_AUTOMATIC)) +def test_a_gated_writer_names_the_switch(site: str) -> None: + """Each gated writer's module reads the config key, so the gate is real. + + A module-level check keeps this cheap and stable: proving the refusal + behaviour is test_memory_context_toggles.py's job, but a module that stopped + mentioning the key at all has lost its gate. + """ + module = SRC / site.split("::", 1)[0] + assert "persistence_enabled" in module.read_text(encoding="utf-8"), ( + f"{site} is registered as gated, but {module.name} never reads " + "memory.persistence_enabled" + )