AI-powered virtual executive team — a single coherent executive persona backed by 8 specialist agents (FastAPI + Next.js).
Extract only the principal's turns; fix three audit-row lies
Claude4e85a41863a9
diff --git a/packages/core/openexecutive/architecture/architecture-facts.yaml b/packages/core/openexecutive/architecture/architecture-facts.yamlindex daaca830..347b4a86 100644--- a/packages/core/openexecutive/architecture/architecture-facts.yaml+++ b/packages/core/openexecutive/architecture/architecture-facts.yaml@@ -927,13 +927,29 @@ memory:also skips those. "Do B." and "Done" differ by one character and meanopposite things, so length cannot separate a decision from anacknowledgement. `_is_valid_user_commitment` — the verbatim-quote check- against the user's own words — is the only gate that can, and it is the- one that runs. The cost of admitting acknowledgements is one utility-fast+ against the user's own words — is the gate that can, and it is the one+ that runs. The cost of admitting acknowledgements is one utility-fastcall per turn that proposes nothing.+ That quote check presumes `user_message` holds the PRINCIPAL's words, and+ on a chat channel it does not: `Executive.chat()` is reached from Slack,+ Discord, Telegram, Google Chat and the email poller, so a teammate's line+ would be stored in `decisions` with no speaker attached, and an inbound+ email body is text the sender chose (a self-quote is free). Removing the+ length floor is what makes this bite — short channel traffic used to fall+ under it incidentally — so `should_extract` also takes `origin_channel`+ and `person_id`. Empty channel = web / CLI / API, the principal's own+ authenticated surfaces, where person_id is legitimately None in a+ single-user install. A named channel must resolve to `is_principal`;+ an unresolvable speaker fails closed.Every pass writes a `memory_extraction` audit row (proposed / stored /- dropped-with-reason, plus a `failure` reason when the pass raises) —- before it, an extractor storing nothing looked identical to one with- nothing to store. A `proposed>0, stored=0` streak is the signal to watch.+ dropped-with-reason / malformed, plus a `failure` reason when the pass+ raises — the handler catches BaseException too, so a CancelledError at+ shutdown is not logged as a clean empty pass). Before it, an extractor+ storing nothing looked identical to one with nothing to store. A+ `proposed>0, stored=0` streak is the signal to watch; `malformed` keeps a+ pass that got only garbage from reading as one that found nothing.+ `stored` is incremented AFTER the write, so a locked database reports+ `stored=0` rather than a healthy-looking pass that wrote no rows.peer_memory:what: |diff --git a/packages/core/openexecutive/architecture/prebuilt/audit.json b/packages/core/openexecutive/architecture/prebuilt/audit.jsonindex 256f2e56..2da7d241 100644--- a/packages/core/openexecutive/architecture/prebuilt/audit.json+++ b/packages/core/openexecutive/architecture/prebuilt/audit.json@@ -1,7 +1,7 @@{"section_id": "audit","title": "Audit Log",- "markdown": "The audit log is a persistent, searchable, append-only record of what the system did — distinct from episodic memory (which biases future LLM behavior). Every event flows through `audit/logger.log_event`, a synchronous SQLite writer (WAL + 5s busy_timeout) that appends one row to the `audit_log` table in `episodic_memory.db`.\n\n### Event types\nRows are tagged by `event_type`, including `chat_turn`, `specialist_consult`, `tool_invocation`, `scheduled_action`, `alert`, `integration_inbound` (Slack / Discord / email / Telegram / Google Chat), plus `knowledge_retrieval`, `memory_extraction` (one row per episodic-extraction pass, written in a `finally` so a crashed pass is recorded too: how many items the model proposed, how many were stored, a `{kind, reason}` record per drop, and a `failure` naming the exception type when the pass raised — a `proposed>0, stored=0` streak means the quote validator is rejecting everything, which is otherwise invisible), `cache_event` (one row per model call from every call site — Executive turns, specialist consults (chat turns and workflow steps), research specialists, the research routing and watchlist passes, triage, memory extraction — carrying tokens, cache hits, cost, server-side search count and, inside a research run, its `run_id`), `memory_snapshot`, `committee_review`, `peer_memory`, and the external-signal/monitor events.\n\n### Session & turn context\nEach row carries `session_id`, `turn_id`, `actor`, `summary`, `details_json`, `full_json`, and `department`. A `turn_id` (`f\"t-{uuid12}\"`) is set per Executive turn and propagated automatically via ContextVars to retriever / specialist / tool / cache emits, grouping one user-assistant exchange. `summary` is the searchable column (LIKE, ~300 char cap); `details_json` is structured and not searched.\n\n### Views\n- `GET /audit` — filterable list with a \"Group by session\" toggle and session pills.\n- `GET /audit/session/{id}` — full ordered timeline plus a server-derived node/edge graph rendered as a three-pane React Flow chart.\n\n### Invariant\nAudit writes never break a chat turn: all writes swallow exceptions and log a warning, and instrumentation observes only — it never touches request building or the cached prompt blocks. Audit is append-only: no UPDATE or DELETE paths exist in product code.",+ "markdown": "The audit log is a persistent, searchable, append-only record of what the system did — distinct from episodic memory (which biases future LLM behavior). Every event flows through `audit/logger.log_event`, a synchronous SQLite writer (WAL + 5s busy_timeout) that appends one row to the `audit_log` table in `episodic_memory.db`.\n\n### Event types\nRows are tagged by `event_type`, including `chat_turn`, `specialist_consult`, `tool_invocation`, `scheduled_action`, `alert`, `integration_inbound` (Slack / Discord / email / Telegram / Google Chat), plus `knowledge_retrieval`, `memory_extraction` (one row per episodic-extraction pass, written in a `finally` so a crashed pass is recorded too: how many items the model proposed, how many were stored, a `{kind, reason}` record per drop, a `malformed` count for payload shapes that were never usable items, and a `failure` naming the exception type when the pass raised — including `CancelledError`, which is a `BaseException` and so would otherwise be logged as a clean empty pass — a `proposed>0, stored=0` streak means the quote validator is rejecting everything, which is otherwise invisible), `cache_event` (one row per model call from every call site — Executive turns, specialist consults (chat turns and workflow steps), research specialists, the research routing and watchlist passes, triage, memory extraction — carrying tokens, cache hits, cost, server-side search count and, inside a research run, its `run_id`), `memory_snapshot`, `committee_review`, `peer_memory`, and the external-signal/monitor events.\n\n### Session & turn context\nEach row carries `session_id`, `turn_id`, `actor`, `summary`, `details_json`, `full_json`, and `department`. A `turn_id` (`f\"t-{uuid12}\"`) is set per Executive turn and propagated automatically via ContextVars to retriever / specialist / tool / cache emits, grouping one user-assistant exchange. `summary` is the searchable column (LIKE, ~300 char cap); `details_json` is structured and not searched.\n\n### Views\n- `GET /audit` — filterable list with a \"Group by session\" toggle and session pills.\n- `GET /audit/session/{id}` — full ordered timeline plus a server-derived node/edge graph rendered as a three-pane React Flow chart.\n\n### Invariant\nAudit writes never break a chat turn: all writes swallow exceptions and log a warning, and instrumentation observes only — it never touches request building or the cached prompt blocks. Audit is append-only: no UPDATE or DELETE paths exist in product code.","mermaid": "flowchart TD\n Chat[\"Chat turn / consult / tool call\"]\n Sched[\"Scheduled action\"]\n Inbound[\"Inbound integration\"]\n Log[\"audit/logger.log_event\"]\n DB[\"audit_log table (append-only)\"]\n ListView[\"/audit list view\"]\n SessView[\"/audit/session/{id} flow chart\"]\n Chat -->|event + turn_id| Log\n Sched --> Log\n Inbound --> Log\n Log -->|swallow errors, never block| DB\n DB --> ListView\n DB --> SessView\n class Chat entry\n class Sched entry\n class Inbound entry\n class Log compute\n class DB storage\n class ListView hot\n class SessView hot","generated_at": "2026-06-01T00:00:00+00:00"}diff --git a/packages/core/openexecutive/architecture/prebuilt/lifecycle.json b/packages/core/openexecutive/architecture/prebuilt/lifecycle.jsonindex 2bd3bb47..5204879f 100644--- a/packages/core/openexecutive/architecture/prebuilt/lifecycle.json+++ b/packages/core/openexecutive/architecture/prebuilt/lifecycle.json@@ -1,7 +1,7 @@{"section_id": "lifecycle","title": "Request Lifecycle",- "markdown": "A single chat message makes a full round-trip through the API, the orchestrator's tool-use loop, a parallel specialist fan-out, and synthesis — all streamed back as server-sent events.\n\n### Steps\n\n1. **Arrival** — the client posts to `POST /chat` (`openexecutive.api.routes.chat`). The turn carries a `<current_speaker>` hint resolved from the caller's `person_id`, plus a `<briefing>` digest of current open alerts (built in parallel with RAG/episodic/peer-memory fetches) so the Executive can discuss any item shown on the `/today` briefing page A turn arriving from a chat adapter instead of the web app also carries a `<channel>` block naming the surface and stating which approvals can be completed there; on Slack the `<briefing>` digest is gated to the principal's DMs. All three ride in the **user turn**, never a cached system block.\n2. **Tool-use loop** — the `Executive` calls Claude. For a cross-domain question the assistant turn emits multiple `consult_specialist` tool-use blocks at once.\n3. **Parallel fan-out** — `openexecutive.orchestrator.router.route_parallel` dispatches those calls concurrently, bounded by `MAX_PARALLEL_SPECIALISTS`; calls past the cap get a skip tool_result.\n4. **Per-specialist RAG** — each specialist runs its own domain-filtered ChromaDB query, then reasons over the retrieved chunks and returns analysis as a tool_result.\n5. **Synthesis** — the Executive feeds the tool_results back to Claude and streams one coherent reply; a `thinking` keepalive covers the specialist round.\n6. **Optional committee review** — when enabled, `Executive.stream_chat_with_committee()` runs a silent draft, parallel reviewer critiques, then a streamed revision pass.\n7. **Side effects** — any side-effecting tool call surfaces an `action_taken` chip; the stream closes with a `done` event. One side-effecting tool is gated server-side rather than by its description: on a turn that arrived from a chat channel, `ack_alert` will only accept an `alert_id` the session recorded as having been shown to the model this turn — and it fails closed, so an id it cannot even parse is refused rather than waved through to the handler's own parse. The unattended passes (the nightly reflection, the research synthesis) do not carry the tool at all, because they set no origin channel and so would not be checked. After the stream closes, the turn is also handed to the episodic memory extractor (`memory.episodic.schedule_extraction`, fire-and-forget) — on every turn where the principal said anything at all. Both this path and the committee path call the same `should_extract` predicate, and it applies no length floor: the gate used to sum in the assistant's reply, which let a verbose answer drag a trivial turn in and discarded the short, decisive instructions that are the whole point of extracting. A user-side floor would fare no better — \"Do B.\" and \"Done\" are one character apart. See **Memory** for what that cost on a live tenant.\n8. **Tool failures** — a handler that raises instead of returning its JSON error is isolated: both tool gathers run with `return_exceptions=True`, the model gets an error `tool_result` naming only the exception's TYPE — a message can carry a filesystem path, a validation error echoing the input, or a third-party HTTP body, and anything in model context can be quoted back to the user, so the full repr stays in the log and the audit row — and the failure is audited with `ok=false`. Only `CancelledError` is re-raised, so turn-timeout and client-disconnect handling still work. Before this one crashing tool aborted the whole turn, which reached the user as an unexplained apology with no audit row (#136).\n\nWeb search, when invoked on the Anthropic path, executes inside the Anthropic API itself (`web_search_20250305`) and returns within the same generation; for any model routed via OpenRouter the same tool becomes OpenRouter's `openrouter:web_search` server tool, which also runs inside the generation with the same search cap and domain lists.",+ "markdown": "A single chat message makes a full round-trip through the API, the orchestrator's tool-use loop, a parallel specialist fan-out, and synthesis — all streamed back as server-sent events.\n\n### Steps\n\n1. **Arrival** — the client posts to `POST /chat` (`openexecutive.api.routes.chat`). The turn carries a `<current_speaker>` hint resolved from the caller's `person_id`, plus a `<briefing>` digest of current open alerts (built in parallel with RAG/episodic/peer-memory fetches) so the Executive can discuss any item shown on the `/today` briefing page A turn arriving from a chat adapter instead of the web app also carries a `<channel>` block naming the surface and stating which approvals can be completed there; on Slack the `<briefing>` digest is gated to the principal's DMs. All three ride in the **user turn**, never a cached system block.\n2. **Tool-use loop** — the `Executive` calls Claude. For a cross-domain question the assistant turn emits multiple `consult_specialist` tool-use blocks at once.\n3. **Parallel fan-out** — `openexecutive.orchestrator.router.route_parallel` dispatches those calls concurrently, bounded by `MAX_PARALLEL_SPECIALISTS`; calls past the cap get a skip tool_result.\n4. **Per-specialist RAG** — each specialist runs its own domain-filtered ChromaDB query, then reasons over the retrieved chunks and returns analysis as a tool_result.\n5. **Synthesis** — the Executive feeds the tool_results back to Claude and streams one coherent reply; a `thinking` keepalive covers the specialist round.\n6. **Optional committee review** — when enabled, `Executive.stream_chat_with_committee()` runs a silent draft, parallel reviewer critiques, then a streamed revision pass.\n7. **Side effects** — any side-effecting tool call surfaces an `action_taken` chip; the stream closes with a `done` event. One side-effecting tool is gated server-side rather than by its description: on a turn that arrived from a chat channel, `ack_alert` will only accept an `alert_id` the session recorded as having been shown to the model this turn — and it fails closed, so an id it cannot even parse is refused rather than waved through to the handler's own parse. The unattended passes (the nightly reflection, the research synthesis) do not carry the tool at all, because they set no origin channel and so would not be checked. After the stream closes, the turn is also handed to the episodic memory extractor (`memory.episodic.schedule_extraction`, fire-and-forget) — on every turn where the principal said anything at all, and only the principal: a turn naming an `origin_channel` must resolve to a person marked `is_principal`, because the quote validator downstream cannot tell whose words it is checking. Both this path and the committee path call the same `should_extract` predicate, and it applies no length floor: the gate used to sum in the assistant's reply, which let a verbose answer drag a trivial turn in and discarded the short, decisive instructions that are the whole point of extracting. A user-side floor would fare no better — \"Do B.\" and \"Done\" are one character apart. See **Memory** for what that cost on a live tenant.\n8. **Tool failures** — a handler that raises instead of returning its JSON error is isolated: both tool gathers run with `return_exceptions=True`, the model gets an error `tool_result` naming only the exception's TYPE — a message can carry a filesystem path, a validation error echoing the input, or a third-party HTTP body, and anything in model context can be quoted back to the user, so the full repr stays in the log and the audit row — and the failure is audited with `ok=false`. Only `CancelledError` is re-raised, so turn-timeout and client-disconnect handling still work. Before this one crashing tool aborted the whole turn, which reached the user as an unexplained apology with no audit row (#136).\n\nWeb search, when invoked on the Anthropic path, executes inside the Anthropic API itself (`web_search_20250305`) and returns within the same generation; for any model routed via OpenRouter the same tool becomes OpenRouter's `openrouter:web_search` server tool, which also runs inside the generation with the same search cap and domain lists.","mermaid": "sequenceDiagram\n participant C as Client\n participant API as FastAPI /chat\n participant E as Executive\n participant CL as Claude\n participant S as Specialists\n participant KB as ChromaDB\n C->>API: POST /chat message\n API->>E: start turn\n E->>CL: messages (tool-use loop)\n CL-->>E: consult_specialist calls\n E->>S: route_parallel fan-out\n S->>KB: domain-filtered RAG\n KB-->>S: chunks\n S-->>E: analysis tool_results\n E->>CL: synthesize\n CL-->>E: streamed reply\n E-->>API: SSE chunks then done\n API-->>C: streamed response","generated_at": "2026-09-18T20:54:42.334412+00:00"}diff --git a/packages/core/openexecutive/architecture/prebuilt/memory.json b/packages/core/openexecutive/architecture/prebuilt/memory.jsonindex 3f0fb90c..dfd69cbf 100644--- a/packages/core/openexecutive/architecture/prebuilt/memory.json+++ b/packages/core/openexecutive/architecture/prebuilt/memory.json@@ -1,7 +1,7 @@{"section_id": "memory","title": "Memory System",- "markdown": "The Executive's durable memory is an episodic SQLite store at `./episodic_memory.db`, managed by `openexecutive.memory.episodic`. It holds four tables:\n\n- `decisions` — choices made, with `domain`, `summary`, `rationale`, `outcome`, and `tags`.\n- `initiatives` — tracked efforts with a `status` and `summary` (idempotent upserts).\n- `advice_given` — query/advice pairs per domain.\n- `scheduled_actions` — a dispatch queue, treated separately from recall memory.\n\n### What gets extracted, and when\n\n`episodic.extract_and_store` runs after a turn and asks a utility-fast model for decisions, initiatives and advice. Every candidate must carry a `user_commitment_quote` that appears verbatim in the principal's own message (`_is_valid_user_commitment`) — the model has repeatedly tried to log the Executive's *recommendations* as the user's commitments, and that quote is the hard gate against it.\n\nA turn reaches the extractor if `should_extract(user_message)` is true, which means only that the principal said something — there is deliberately **no length floor**. There used to be one, on the combined user+assistant length, and on a live tenant it selected almost exactly the wrong turns: 9 of 16 exchanges were blocked, and the 9 held every instruction the principal gave, while the 7 admitted were long analytical exchanges with no commitment in them. The extractor ran 13 times and stored nothing.\n\nMoving that floor to the user's side would relocate the bug, not fix it. The canonical executive decision is a long analysis answered with \"Approve option B.\" (17 chars) or \"Do B.\" (5), and any floor high enough to skip \"Done\" (4) also skips those — \"Do B.\" and \"Done\" differ by one character and mean opposite things. Length cannot separate a decision from an acknowledgement; the verbatim-quote check can, so `_is_valid_user_commitment` is the only gate. The price is one utility-fast call on turns that propose nothing.\n\nEach pass writes a `memory_extraction` audit row — in a `finally`, so a pass that crashed is recorded too, with the exception type as its `failure` reason: how many items the model proposed, how many were stored, and a `{kind, reason}` record per drop (`bad_quote`, `missing_summary`, `not_a_list`, `item_not_a_dict`, `payload_not_a_dict`). The tool payload is not schema-checked anywhere, so a malformed shape is counted as a drop rather than raised — it used to abort the whole pass, discarding the other two kinds with it and suppressing the row, which read exactly like \"extraction never ran\". Without it, \"the model found nothing\" and \"the model found things and every one was rejected\" are indistinguishable from outside the database — which is why the failure above survived undetected and took a SQLite session on the tenant to find. A `proposed>0, stored=0` streak is the signal to watch.\n\n### Session scoping\n\n`decisions` and `advice_given` rows carry a `session_id`, so per-thread channels (Discord, Telegram) can pull just-this-thread history without leaking cross-conversation context. A null `session_id` falls back to a global view.\n\n### Surfacing\n\n`openexecutive.memory.episodic.format_for_prompt` renders recent decisions and initiatives into a compact `<past_decisions>` block injected into the user turn — alongside `<retrieved_context>` and peer memory — never into a cached system block.\n\nThe chat route also injects a `<briefing>` block into the same user turn: a compact digest of the current open (unread) alerts, built by `openexecutive.briefing.context.format_open_alerts_for_prompt` (company-wide, `status=\"unread\"`, mirroring `/today`). This is what lets the Executive discuss a briefing item the principal clicked or named on `/today` (e.g. \"Gulf Coast Port Cyberattack\"). It is resolved in parallel with the other context fetches, omitted when there are no open alerts, and never placed in a cached system block.\n\n### System of record\n\nSQL is the system of record; the peer-memory service is a recall layer mirrored from it, not a replacement. When `store_decision` / `store_initiative` / `store_advice` insert a row whose `department` is non-empty and not `\"general\"`, the row is mirrored as a kind-tagged note into that department's peer via `append_department_note`. The mirror fires only after a successful INSERT, is fire-and-forget, and never rolls back the SQL row on peer-memory failure. `scheduled_actions` is never mirrored — it is a queue, not memory.",+ "markdown": "The Executive's durable memory is an episodic SQLite store at `./episodic_memory.db`, managed by `openexecutive.memory.episodic`. It holds four tables:\n\n- `decisions` — choices made, with `domain`, `summary`, `rationale`, `outcome`, and `tags`.\n- `initiatives` — tracked efforts with a `status` and `summary` (idempotent upserts).\n- `advice_given` — query/advice pairs per domain.\n- `scheduled_actions` — a dispatch queue, treated separately from recall memory.\n\n### What gets extracted, and when\n\n`episodic.extract_and_store` runs after a turn and asks a utility-fast model for decisions, initiatives and advice. Every candidate must carry a `user_commitment_quote` that appears verbatim in the principal's own message (`_is_valid_user_commitment`) — the model has repeatedly tried to log the Executive's *recommendations* as the user's commitments, and that quote is the hard gate against it.\n\nA turn reaches the extractor if `should_extract(user_message)` is true, which means only that the principal said something — there is deliberately **no length floor**. There used to be one, on the combined user+assistant length, and on a live tenant it selected almost exactly the wrong turns: 9 of 16 exchanges were blocked, and the 9 held every instruction the principal gave, while the 7 admitted were long analytical exchanges with no commitment in them. The extractor ran 13 times and stored nothing.\n\nMoving that floor to the user's side would relocate the bug, not fix it. The canonical executive decision is a long analysis answered with \"Approve option B.\" (17 chars) or \"Do B.\" (5), and any floor high enough to skip \"Done\" (4) also skips those — \"Do B.\" and \"Done\" differ by one character and mean opposite things. Length cannot separate a decision from an acknowledgement; the verbatim-quote check can, so `_is_valid_user_commitment` is the gate that decides. The price is one utility-fast call on turns that propose nothing.\n\nThat quote check is only meaningful when `user_message` holds the **principal's** words, and on a chat channel it does not — `Executive.chat()` is reached from Slack, Discord, Telegram, Google Chat and the email poller. A teammate's line would be written to `decisions` with no speaker attached, indistinguishable from the principal's own; an inbound email body is text the sender chose, so a self-quote is free. Removing the length floor is what makes this bite — short channel traffic used to fall under it incidentally — so `should_extract` also takes the turn's `origin_channel` and `person_id`. No channel means the web app, the CLI or the API, all of them the principal's own authenticated surfaces, where `person_id` is legitimately null in a single-user install. A turn that names a channel must resolve to a person marked `is_principal`, and an unresolvable speaker fails closed.\n\nEach pass writes a `memory_extraction` audit row — in a `finally`, so a pass that crashed is recorded too, with the exception type as its `failure` reason: how many items the model proposed, how many were stored, a `malformed` count so a pass where the model returned only garbage does not read as one that found nothing, and a `{kind, reason}` record per drop — `bad_quote` and `missing_field` for an item the model proposed, `not_a_list`, `item_not_a_dict` and `payload_not_a_dict` for a shape that was never a usable item. Every proposed item is either stored or dropped, so `proposed == stored + item drops` is the arithmetic that says whether anything was lost silently. The tool payload is not schema-checked anywhere, so a malformed shape is counted as a drop rather than raised — it used to abort the whole pass, discarding the other two kinds with it and suppressing the row, which read exactly like \"extraction never ran\". Without it, \"the model found nothing\" and \"the model found things and every one was rejected\" are indistinguishable from outside the database — which is why the failure above survived undetected and took a SQLite session on the tenant to find. A `proposed>0, stored=0` streak is the signal to watch.\n\n### Session scoping\n\n`decisions` and `advice_given` rows carry a `session_id`, so per-thread channels (Discord, Telegram) can pull just-this-thread history without leaking cross-conversation context. A null `session_id` falls back to a global view.\n\n### Surfacing\n\n`openexecutive.memory.episodic.format_for_prompt` renders recent decisions and initiatives into a compact `<past_decisions>` block injected into the user turn — alongside `<retrieved_context>` and peer memory — never into a cached system block.\n\nThe chat route also injects a `<briefing>` block into the same user turn: a compact digest of the current open (unread) alerts, built by `openexecutive.briefing.context.format_open_alerts_for_prompt` (company-wide, `status=\"unread\"`, mirroring `/today`). This is what lets the Executive discuss a briefing item the principal clicked or named on `/today` (e.g. \"Gulf Coast Port Cyberattack\"). It is resolved in parallel with the other context fetches, omitted when there are no open alerts, and never placed in a cached system block.\n\n### System of record\n\nSQL is the system of record; the peer-memory service is a recall layer mirrored from it, not a replacement. When `store_decision` / `store_initiative` / `store_advice` insert a row whose `department` is non-empty and not `\"general\"`, the row is mirrored as a kind-tagged note into that department's peer via `append_department_note`. The mirror fires only after a successful INSERT, is fire-and-forget, and never rolls back the SQL row on peer-memory failure. `scheduled_actions` is never mirrored — it is a queue, not memory.","mermaid": "flowchart TD\n Store[\"store_decision / store_initiative / store_advice\"]\n DB[\"episodic_memory.db<br/>decisions / initiatives<br/>advice_given / scheduled_actions\"]\n Fmt[\"format_for_prompt\"]\n Turn[\"User turn<br/>past_decisions block\"]\n Mirror[\"append_department_note\"]\n Peer[\"Department peer<br/>(Honcho)\"]\n Store --> DB\n DB --> Fmt\n Fmt --> Turn\n Store -->|\"dept set & not general\"| Mirror\n Mirror -->|\"fire-and-forget\"| Peer\n class Store entry\n class DB storage\n class Fmt compute\n class Turn hot\n class Peer external\n","generated_at": "2026-06-01T00:00:00+00:00"}diff --git a/packages/core/openexecutive/memory/episodic.py b/packages/core/openexecutive/memory/episodic.pyindex 2ad9b800..1de09d1d 100644--- a/packages/core/openexecutive/memory/episodic.py+++ b/packages/core/openexecutive/memory/episodic.py@@ -9,7 +9,7 @@ from collections.abc import Generatorfrom contextlib import contextmanagerfrom datetime import UTC, datetime, timedeltafrom pathlib import Path-from typing import Any+from typing import Any, NamedTuplefrom pydantic import BaseModel@@ -1833,46 +1833,119 @@ def _is_valid_user_commitment(quote: str, user_message: str) -> bool:_MAX_INPUT_CHARS = 20_000 # cap each side to avoid runaway cost-def should_extract(user_message: str) -> bool:+# How many individual drop records ride along in the audit row's `details`.+# `dropped_count` is always exact; this caps only the itemised list, because a+# model that returns fifty malformed items would otherwise put fifty records+# in one audit row. Ten is enough to see the pattern — and the pattern is what+# an operator reads this field for, since the counts above it already say how+# bad it is.+_MAX_DROPPED_IN_AUDIT = 10++def should_extract(+ user_message: str,+ *,+ origin_channel: str = "",+ person_id: int | None = None,+) -> bool:"""True when this turn is worth an extraction pass.- There is deliberately no length floor. There used to be one on the- combined user+assistant length, which discarded short instructions- answered at length — on a live tenant it blocked every commitment the- principal made while admitting only the long analytical exchanges that- had none, and the extractor ran 13 times storing nothing.+ **No length floor.** There used to be one on the combined user+assistant+ length, which discarded short instructions answered at length — on a live+ tenant it blocked every commitment the principal made while admitting only+ the long analytical exchanges that had none, and the extractor ran 13+ times storing nothing.Moving that floor to the user's side does not fix it, it relocates it: thecanonical executive decision is a long analysis answered with "Approveoption B." (17 chars) or "Do B." (5), and any floor high enough to skip"Done" (4) also skips those. Length cannot separate a decision from anacknowledgement — "Do B." and "Done" differ by one character and mean- opposite things.-- `_is_valid_user_commitment` is the gate that actually tests for a- commitment: it requires a verbatim quote from the user's own words. A pass- over an acknowledgement costs one utility-fast call (~$0.005 observed) and- stores nothing, which is the right trade against losing approvals. If- per-turn cost ever becomes the binding constraint, the lever is a semantic- prefilter or a per-session cap — not a length proxy for a property it- cannot measure.-- The single decision point for both call sites in- `orchestrator.executive`, so the rule is testable directly and the two- paths cannot drift apart.+ opposite things. A pass over an acknowledgement costs one utility-fast+ call and stores nothing, which is the right trade against losing+ approvals. If per-turn cost ever becomes the binding constraint, the lever+ is a semantic prefilter or a per-session cap — not a length proxy for a+ property it cannot measure.++ **Only the principal's own words.** `_is_valid_user_commitment` is the+ gate that tests for a commitment, and it does so by requiring a verbatim+ quote from `user_message`. That is only meaningful when `user_message`+ actually holds the principal's words. On a chat channel it does not: a+ teammate's Slack line would be stored in `decisions` with no speaker+ attached, indistinguishable from the principal's own; an inbound email+ body is text the sender chose, and a self-quote is free.++ Removing the length floor is what makes this matter — short channel+ traffic used to fall under it incidentally — so the speaker check lands+ with it. A turn with no `origin_channel` came from the web app, the CLI or+ the API, all of which are the principal's own authenticated surfaces, and+ `person_id` is legitimately None there in a single-user install. A turn+ that names a channel must resolve to a person marked `is_principal`.++ The single decision point for both call sites in `orchestrator.executive`,+ so the rule is testable directly and the two paths cannot drift apart."""- return bool(user_message.strip())+ if not user_message.strip():+ return False+ if not origin_channel:+ return True+ if person_id is None:+ return False+ try:+ from openexecutive.people.store import get_person++ person = get_person(person_id)+ except Exception:+ # Fail closed: an unresolvable speaker is not the principal.+ logger.warning(+ "extraction_speaker_lookup_failed person_id=%s channel=%s",+ person_id,+ origin_channel,+ exc_info=True,+ )+ return False+ return bool(person is not None and person.is_principal)++# Drop reasons for a payload SHAPE the model got wrong, as opposed to an item+# it proposed and that was then rejected. Kept apart in the audit row so the+# `proposed == stored + item drops` arithmetic stays true.+_SHAPE_REASONS = frozenset({"not_a_list", "item_not_a_dict", "payload_not_a_dict"})-# Payload key -> the `kind` a drop is recorded under. The per-item drops below-# are singular, so grouping the audit rows by `kind` has to see one spelling.-_ITEM_KINDS = {"decisions": "decision", "initiatives": "initiative", "advice": "advice"}++class _ItemSpec(NamedTuple):+ """How one kind of extracted item is read out of the model's payload.++ The three kinds differ only in their field names, so this is the one place+ those names live. Spelling them once keeps a drop recorded by+ `_iter_items` and a drop recorded by `_accept` under the same `kind`,+ which is what makes "bad-quote drops on decisions this week" a single+ query rather than a union over spellings that have drifted apart.+ """++ key: str+ """The payload key the model writes, e.g. `decisions`."""++ kind: str+ """Singular name every drop record for this kind is filed under."""++ required: tuple[str, ...]+ """Fields that must be non-empty for the item to be worth storing."""++ label: str+ """The field echoed (truncated) into a drop so a human can identify it."""+++_DECISIONS = _ItemSpec("decisions", "decision", ("summary",), "summary")+_INITIATIVES = _ItemSpec("initiatives", "initiative", ("title", "summary"), "title")+_ADVICE = _ItemSpec(+ "advice", "advice", ("query_summary", "advice_summary"), "query_summary"+)def _iter_items(- payload: dict[str, Any], key: str, dropped: list[dict[str, str]]+ payload: dict[str, Any], spec: _ItemSpec, dropped: list[dict[str, str]]) -> list[dict[str, Any]]:- """The dict-shaped items under ``key``, skipping anything malformed.+ """The dict-shaped items for ``spec``, skipping anything malformed.The model's tool payload is not schema-checked, so `{"decisions": "..."}`or `{"decisions": ["text"]}` used to raise out of the whole pass — taking@@ -1883,22 +1956,66 @@ def _iter_items(A missing key or an explicit `null` is not a drop: the model omitting akind it found nothing for is the normal case."""- kind = _ITEM_KINDS[key]- raw = payload.get(key)+ raw = payload.get(spec.key)if raw is None:return []if not isinstance(raw, list):- dropped.append({"kind": kind, "reason": "not_a_list"})+ dropped.append({"kind": spec.kind, "reason": "not_a_list"})return []out: list[dict[str, Any]] = []for item in raw:if isinstance(item, dict):out.append(item)else:- dropped.append({"kind": kind, "reason": "item_not_a_dict"})+ dropped.append({"kind": spec.kind, "reason": "item_not_a_dict"})return out+def _accept(+ item: dict[str, Any],+ spec: _ItemSpec,+ user_message: str,+ dropped: list[dict[str, str]],+) -> bool:+ """True when ``item`` is complete and genuinely the user's own commitment.++ Shared by all three kinds so a drop is recorded on every rejecting path.+ Counting an item as proposed and then returning without a drop record is+ the bug this centralises away: it broke `proposed == stored + dropped`,+ which is the arithmetic an operator uses to tell "the model found nothing"+ from "the model found things and every one was rejected".++ The quote check is the hard gate. The model has repeatedly proven willing+ to log the Executive's *recommendations* as if the user had committed to+ them — the May 27 incident turned the question "Should we scope as fixed+ POC or hourly?" into the decision "First $30K deal will be structured as+ fixed POC". Requiring a verbatim quote from the user's own message is what+ catches that; the prompt is only the soft instruction layer.+ """+ missing = [field for field in spec.required if not item.get(field)]+ if missing:+ dropped.append(+ {"kind": spec.kind, "reason": "missing_field", "field": missing[0]}+ )+ return False++ quote = str(item.get("user_commitment_quote", ""))+ label = str(item.get(spec.label, ""))+ if not _is_valid_user_commitment(quote, user_message):+ dropped.append(+ {"kind": spec.kind, "reason": "bad_quote", "label": label[:60]}+ )+ logger.debug(+ "Dropping %s — invalid user_commitment_quote %r (%s=%r)",+ spec.kind,+ quote[:120],+ spec.label,+ label[:80],+ )+ return False+ return True++def _audit_extraction(proposed: dict[str, int],stored: dict[str, int],@@ -1917,11 +2034,22 @@ def _audit_extraction(is the RATIO over time, which a `proposed>0, stored=0` streak makesobvious.+ `proposed` counts items the model actually emitted as objects, so every+ proposed item is either stored or dropped and+ `proposed == stored + (dropped - malformed)` holds. A malformed SHAPE was+ never a usable item, so it is counted separately rather than folded into+ `proposed` — otherwise `dropped > proposed` on a payload that was nothing+ but garbage. `malformed` is broken out for the same reason the rest of+ this row exists: without it a pass where the model returned only garbage+ reads `proposed=0 stored=0`, which is what "the model found nothing"+ looks like.+Never raises: auditing an extraction must not be able to break the turnthat produced it."""total_proposed = sum(proposed.values())total_stored = sum(stored.values())+ malformed = sum(1 for d in dropped if d["reason"] in _SHAPE_REASONS)prefix = f"FAILED({failure}) " if failure else ""try:from openexecutive.audit import log_event@@ -1929,18 +2057,19 @@ def _audit_extraction(log_event("memory_extraction",f"{prefix}proposed={total_proposed} stored={total_stored} "- f"dropped={len(dropped)}",+ f"dropped={len(dropped)} malformed={malformed}",session_id=session_id or None,actor="memory_extractor",details={"proposed": proposed,"stored": stored,"dropped_count": len(dropped),+ "malformed_count": malformed,"failure": failure,# Structured like `proposed`/`stored` so "how many bad-quote# drops on decisions this week" is a query, not a string split# over a field that can itself contain colons.- "dropped": dropped[:10],+ "dropped": dropped[:_MAX_DROPPED_IN_AUDIT],},)except Exception:@@ -2061,82 +2190,45 @@ async def _extract_and_store(if not isinstance(inp, dict):dropped.append({"kind": "pass", "reason": "payload_not_a_dict"})continue- # Validate every item against the user's actual text. The LLM has- # repeatedly proven willing to log the executive's recommendations- # as if the user had committed to them — see the May 27 incident- # where "Should we scope as fixed POC or hourly?" produced a- # decision "First $30K deal will be structured as fixed POC".- # The user_commitment_quote field + this validator are the- # hard gate that catches that pattern; the prompt is now just- # the soft instruction layer.- for d in _iter_items(inp, "decisions", dropped):+ # Each loop is read → count → validate → store. The counting and+ # validation are identical across the three kinds and live in+ # `_accept`; only the store call differs, because the three store+ # functions take different fields.+ for d in _iter_items(inp, _DECISIONS, dropped):proposed["decisions"] += 1- domain = d.get("domain", "general")- summary = d.get("summary", "")- quote = d.get("user_commitment_quote", "")- if not summary:- dropped.append({"kind": "decision", "reason": "missing_summary"})- continue- if not _is_valid_user_commitment(quote, user_message):- dropped.append({"kind": "decision", "reason": "bad_quote", "summary": summary[:60]})- logger.debug(- "Dropping decision — invalid user_commitment_quote %r (summary=%r)",- quote[:120],- summary[:80],- )+ if not _accept(d, _DECISIONS, user_message, dropped):continue- stored["decisions"] += 1store_decision(- domain=domain,- summary=summary,+ domain=d.get("domain", "general"),+ summary=d["summary"],rationale=d.get("rationale", ""),session_id=session_id,db_path=db_path,)- for i in _iter_items(inp, "initiatives", dropped):+ stored["decisions"] += 1+ for i in _iter_items(inp, _INITIATIVES, dropped):proposed["initiatives"] += 1- title = i.get("title", "")- status = i.get("status", "active")- summary = i.get("summary", "")- quote = i.get("user_commitment_quote", "")- if not (title and summary):- dropped.append({"kind": "initiative", "reason": "missing_field"})- continue- if not _is_valid_user_commitment(quote, user_message):- dropped.append({"kind": "initiative", "reason": "bad_quote", "title": title[:60]})- logger.debug(- "Dropping initiative — invalid user_commitment_quote %r (title=%r)",- quote[:120],- title[:80],- )+ if not _accept(i, _INITIATIVES, user_message, dropped):continue+ store_initiative(+ title=i["title"],+ status=i.get("status", "active"),+ summary=i["summary"],+ db_path=db_path,+ )stored["initiatives"] += 1- store_initiative(title=title, status=status, summary=summary, db_path=db_path)- for a in _iter_items(inp, "advice", dropped):+ for a in _iter_items(inp, _ADVICE, dropped):proposed["advice"] += 1- domain = a.get("domain", "general")- query_summary = a.get("query_summary", "")- advice_summary = a.get("advice_summary", "")- quote = a.get("user_commitment_quote", "")- if not (query_summary and advice_summary):- dropped.append({"kind": "advice", "reason": "missing_field"})- continue- if not _is_valid_user_commitment(quote, user_message):- dropped.append({"kind": "advice", "reason": "bad_quote", "query_summary": query_summary[:60]})- logger.debug(- "Dropping advice — invalid user_commitment_quote %r (query=%r)",- quote[:120],- query_summary[:80],- )+ if not _accept(a, _ADVICE, user_message, dropped):continue- stored["advice"] += 1store_advice(- domain=domain,- query_summary=query_summary,- advice_summary=advice_summary,+ domain=a.get("domain", "general"),+ query_summary=a["query_summary"],+ advice_summary=a["advice_summary"],session_id=session_id,db_path=db_path,)+ stored["advice"] += 1except Exception as exc:# Recorded so the audit row can say the pass FAILED. Without it a@@ -2145,6 +2237,15 @@ async def _extract_and_store(# state this row exists to eliminate.failure = type(exc).__name__logger.exception("Episodic memory extraction failed — skipping silently")+ except BaseException as exc:+ # `CancelledError` is a BaseException, so the clause above misses it+ # while the `finally` still writes a row. A pass cancelled at shutdown+ # would then be logged as `proposed=0 stored=0 failure=''` — byte for+ # byte what "the model proposed nothing" looks like, which is the one+ # ambiguity this row exists to remove. Labelled and re-raised, never+ # swallowed: cancellation still has to propagate.+ failure = type(exc).__name__+ raisefinally:# In `finally`, not the happy path: a pass that crashed is exactly the# one an operator needs to see.diff --git a/packages/core/openexecutive/orchestrator/executive.py b/packages/core/openexecutive/orchestrator/executive.pyindex 2a15a7d8..e7e37e09 100644--- a/packages/core/openexecutive/orchestrator/executive.py+++ b/packages/core/openexecutive/orchestrator/executive.py@@ -808,7 +808,11 @@ class Executive:# call time, so scheduling it out here would snapshot (None,# None) and the memory_extractor's model call would record# unattributed however correct the snapshot itself was.- if should_extract(user_message):+ if should_extract(+ user_message,+ origin_channel=session.origin_channel,+ person_id=person_id,+ ):schedule_extraction(user_message, full_response, session_id=session.session_id)@@ -1223,8 +1227,14 @@ class Executive:schedule_extraction,should_extract,)- if should_extract(user_message):- schedule_extraction(user_message, final_response, session_id=session.session_id)+ if should_extract(+ user_message,+ origin_channel=session.origin_channel,+ person_id=person_id,+ ):+ schedule_extraction(+ user_message, final_response, session_id=session.session_id+ )# Mirror the completed exchange into Honcho (see stream_chat for# rationale). Fire-and-forget; no-ops when person_id is None.diff --git a/packages/core/tests/unit/test_committee_debug_events.py b/packages/core/tests/unit/test_committee_debug_events.pyindex 997ac3f8..03c588dd 100644--- a/packages/core/tests/unit/test_committee_debug_events.py+++ b/packages/core/tests/unit/test_committee_debug_events.py@@ -11,6 +11,8 @@ import asynciofrom typing import Anyfrom unittest.mock import patch+import pytest+from openexecutive.memory.company_profile import CompanyProfilefrom openexecutive.orchestrator.committee_reviewers import Critiquefrom openexecutive.orchestrator.debug_events import DebugCollector@@ -18,6 +20,21 @@ from openexecutive.orchestrator.executive import Executivefrom openexecutive.orchestrator.session import Session[email protected](autouse=True)+def _no_audit_writes(monkeypatch: pytest.MonkeyPatch) -> None:+ """Keep this module's audit rows out of the default `./episodic_memory.db`.++ A committee turn schedules episodic extraction fire-and-forget, and the+ resulting `memory_extraction` row goes through `audit.log_event`, which+ targets the default DB whatever `db_path` the extraction was handed. The+ rows then surface as real data in other modules' assertions during a full+ run only — the audit-log pollution trap in CLAUDE.md.+ """+ monkeypatch.setattr(+ "openexecutive.audit.log_event", lambda *a, **kw: None, raising=False+ )++class _NoopAsyncStream:"""Async context manager yielding zero text deltas — enough for therevision call to complete without making real Anthropic requests."""diff --git a/packages/core/tests/unit/test_episodic.py b/packages/core/tests/unit/test_episodic.pyindex 3ed21722..e9725a56 100644--- a/packages/core/tests/unit/test_episodic.py+++ b/packages/core/tests/unit/test_episodic.py@@ -35,6 +35,21 @@ def db(tmp_path: Path) -> Path:return db_path[email protected](autouse=True)+def _no_audit_writes(monkeypatch: pytest.MonkeyPatch) -> None:+ """Keep `memory_extraction` rows out of the default `./episodic_memory.db`.++ `_audit_extraction` calls `openexecutive.audit.log_event`, which targets+ the default DB regardless of the `db_path` the extraction was given. The+ tests below that drive `extract_and_store` therefore leak rows that other+ modules read back as real data — visible only in a full-suite run, which+ is the audit-log pollution trap in CLAUDE.md.+ """+ monkeypatch.setattr(+ "openexecutive.audit.log_event", lambda *a, **kw: None, raising=False+ )++def test_decision_update_and_delete(db: Path) -> None:store_decision("strategy", "Expand to EU", rationale="Larger market", db_path=db)items = list_decisions(db_path=db)@@ -464,33 +479,71 @@ def test_extraction_gate_has_no_length_floor() -> None:def test_executive_call_sites_use_the_shared_gate() -> None:- """Both extraction trigger sites in executive.py must call the shared- predicate, not inline a length comparison. Previously they were 300- (committee) and 800 (stream) — asymmetric, low, and easy to drift further.- Pin via source inspection because the call sites live inside long async- streaming methods that aren't unit-testable end-to-end.+ """Every `schedule_extraction` in executive.py must be guarded by a bare+ `should_extract(...)` call and nothing else.++ Checked against the parsed syntax tree rather than the source text. A+ string search pins yesterday's spelling: `if should_extract(user_message)+ and len(user_message) >= 200:` re-introduces the exact defect this work+ removed — a length floor at the call site — while still containing the+ substring a `count(...)` assertion looks for. The AST sees the extra+ condition.++ The call sites live inside long async streaming methods that are not+ unit-testable end-to-end, which is why the guard is pinned structurally+ rather than by driving it."""+ import astimport inspectfrom openexecutive.orchestrator import executive- src = inspect.getsource(executive)- # Each call site contributes an import AND a call, so >= 2 would be- # satisfied by a single surviving site — a partial revert would pass.- assert src.count("should_extract(user_message)") == 2, (- "both the stream_chat and stream_chat_with_committee extraction "- "guards must call should_extract(user_message)"+ tree = ast.parse(inspect.getsource(executive))++ def _calls_schedule(node: ast.AST) -> bool:+ return any(+ isinstance(n, ast.Call)+ and isinstance(n.func, ast.Name)+ and n.func.id == "schedule_extraction"+ for n in ast.walk(node)+ )++ guards = [+ node+ for node in ast.walk(tree)+ if isinstance(node, ast.If) and _calls_schedule(node)+ ]+ assert len(guards) == 2, (+ "expected exactly two guarded extraction sites (stream_chat and "+ f"stream_chat_with_committee), found {len(guards)}")- # And they must measure the USER's message. Summing in the assistant's- # length is the original defect: it let a verbose reply drag a trivial turn- # in, and discarded a one-line instruction answered concisely.- assert "len(full_response) + len(user_message)" not in src- assert "len(final_response) + len(user_message)" not in src- # And the old hard-coded thresholds must be gone — guard against a- # partial revert that wires the constant in one place but leaves the- # other path on the old number.- assert "len(user_message) >= 800" not in src- assert "len(user_message) >= 300" not in src++ for guard in guards:+ test = guard.test+ assert isinstance(test, ast.Call), (+ f"line {guard.lineno}: the guard must be a bare should_extract(...) "+ f"call, not {type(test).__name__} — an `and len(...) >= N` clause "+ "is the length floor coming back"+ )+ assert isinstance(test.func, ast.Name) and test.func.id == "should_extract", (+ f"line {guard.lineno}: guard must call should_extract"+ )+ assert [a.id for a in test.args if isinstance(a, ast.Name)] == ["user_message"]+ # The speaker check is not optional: a channel turn carries someone+ # else's words, and the quote validator cannot tell whose they are.+ assert {kw.arg for kw in test.keywords} == {"origin_channel", "person_id"}, (+ f"line {guard.lineno}: guard must pass the speaker's channel and id"+ )++ # No extraction may be scheduled outside a guard.+ guarded = {id(n) for g in guards for n in ast.walk(g)}+ for node in ast.walk(tree):+ if (+ isinstance(node, ast.Call)+ and isinstance(node.func, ast.Name)+ and node.func.id == "schedule_extraction"+ ):+ assert id(node) in guarded, f"line {node.lineno}: unguarded extraction"# ---------------------------------------------------------------------------diff --git a/packages/core/tests/unit/test_executive_peer_memory_kwarg.py b/packages/core/tests/unit/test_executive_peer_memory_kwarg.pyindex 51811def..bba5757d 100644--- a/packages/core/tests/unit/test_executive_peer_memory_kwarg.py+++ b/packages/core/tests/unit/test_executive_peer_memory_kwarg.py@@ -10,6 +10,25 @@ import osfrom types import SimpleNamespacefrom unittest.mock import AsyncMock, MagicMock, patch+import pytest++[email protected](autouse=True)+def _no_audit_writes(monkeypatch: pytest.MonkeyPatch) -> None:+ """Keep this module's `memory_extraction` rows out of the default+ `./episodic_memory.db`.++ Driving a turn schedules episodic extraction fire-and-forget, and its+ audit row goes through `audit.log_event`, which targets the default DB+ whatever `db_path` the extraction was handed. Those rows surface as real+ data in other modules' assertions during a full run only — the audit-log+ pollution trap in CLAUDE.md.+ """+ monkeypatch.setattr(+ "openexecutive.audit.log_event", lambda *a, **kw: None, raising=False+ )++os.environ.setdefault("ANTHROPIC_API_KEY", "sk-test-not-used")diff --git a/packages/core/tests/unit/test_extraction_gate.py b/packages/core/tests/unit/test_extraction_gate.pyindex df03c821..ff11945e 100644--- a/packages/core/tests/unit/test_extraction_gate.py+++ b/packages/core/tests/unit/test_extraction_gate.py@@ -11,8 +11,10 @@ The turns below are the real ones, with their real lengths."""from __future__ import annotations+import asyncio+import sqlite3from pathlib import Path-from typing import Any+from typing import Any, NamedTuplefrom unittest import mockimport pytest@@ -74,17 +76,102 @@ def test_empty_turns_are_skipped() -> None:assert not episodic.should_extract(blank)+class _Stores(NamedTuple):+ """The three persistence calls, mocked so a test can assert on arguments."""++ decision: mock.MagicMock+ initiative: mock.MagicMock+ advice: mock.MagicMock+++# --------------------------------------------------------------------- #+# Who is speaking+#+# `_is_valid_user_commitment` tests a quote against `user_message`, which is+# only meaningful when that text is the principal's own words. On a chat+# channel it is not: `Executive.chat()` is reached from Slack, Discord,+# Telegram, Google Chat and the email poller, and the message there belongs to+# whoever sent it. Removing the length floor is what makes this bite — short+# channel traffic used to fall under it incidentally.+# --------------------------------------------------------------------- #+++def _person(*, is_principal: bool) -> mock.MagicMock:+ person = mock.MagicMock()+ person.is_principal = is_principal+ return person+++def test_a_web_turn_needs_no_person_row() -> None:+ """No origin channel means the web app, the CLI or the API — the+ principal's own authenticated surfaces. `person_id` is legitimately None+ there in a single-user install, which is the tenant this fix is for."""+ assert episodic.should_extract("Do B.", origin_channel="", person_id=None)++[email protected]("channel", ["slack", "discord", "telegram", "email"])+def test_a_channel_turn_from_the_principal_is_extracted(channel: str) -> None:+ with mock.patch(+ "openexecutive.people.store.get_person", return_value=_person(is_principal=True)+ ):+ assert episodic.should_extract(+ "Do B.", origin_channel=channel, person_id=7+ )++[email protected]("channel", ["slack", "discord", "telegram", "email"])+def test_a_channel_turn_from_anyone_else_is_skipped(channel: str) -> None:+ """A teammate's line would land in `decisions` with no speaker attached,+ indistinguishable from the principal's own commitment."""+ with mock.patch(+ "openexecutive.people.store.get_person",+ return_value=_person(is_principal=False),+ ):+ assert not episodic.should_extract(+ "Let's move the deadline to Friday.", origin_channel=channel, person_id=9+ )+++def test_an_unidentified_channel_speaker_is_skipped() -> None:+ """An inbound email is text the sender chose, so a verbatim self-quote is+ free. With no resolved person there is nothing to check it against."""+ assert not episodic.should_extract(+ "I approve the wire transfer.", origin_channel="email", person_id=None+ )+++def test_a_failed_person_lookup_fails_closed() -> None:+ with mock.patch(+ "openexecutive.people.store.get_person", side_effect=RuntimeError("no table")+ ):+ assert not episodic.should_extract(+ "Do B.", origin_channel="slack", person_id=7+ )+++def test_a_missing_person_row_fails_closed() -> None:+ with mock.patch("openexecutive.people.store.get_person", return_value=None):+ assert not episodic.should_extract(+ "Do B.", origin_channel="slack", person_id=7+ )++async def _run_pass(payload: Any,db_path: Path,*,user_message: str = "Cut burn to 400k. Tell the team.",-) -> tuple[mock.MagicMock, list[dict[str, Any]]]:+) -> tuple[_Stores, list[dict[str, Any]]]:"""Drive one real extraction pass over a model payload.The payload is whatever the model put in its `store_memories` tool block —it is NOT schema-checked anywhere, so tests hand it the shapes a modelactually emits, including the malformed ones.++ All three store functions are mocked, so every kind is asserted the same+ way — on the arguments it was called with, not just on the audit counters.+ `db_path` still points at an isolated DB so nothing here can reach the+ default `./episodic_memory.db` if a code path stops going through them."""class _Block:type = "tool_use"@@ -100,7 +187,9 @@ async def _run_pass(rows.append({"event_type": event_type, "summary": summary, **kw})with (- mock.patch.object(episodic, "store_decision") as store,+ mock.patch.object(episodic, "store_decision") as store_decision,+ mock.patch.object(episodic, "store_initiative") as store_initiative,+ mock.patch.object(episodic, "store_advice") as store_advice,mock.patch("openexecutive.audit.log_event", _capture),mock.patch("openexecutive.audit.usage.log_model_usage"),mock.patch("openexecutive.config.get_settings"),@@ -114,7 +203,10 @@ async def _run_pass(user_message, "Understood.", db_path=db_path, session_id="s-1")- return store, [r for r in rows if r["event_type"] == "memory_extraction"]+ return (+ _Stores(store_decision, store_initiative, store_advice),+ [r for r in rows if r["event_type"] == "memory_extraction"],+ )@pytest.mark.asyncio@@ -125,7 +217,7 @@ async def test_extraction_audits_proposed_stored_and_dropped(db: Path) -> None:from "the model found nothing" because drops were logger.debug and storeswrote no row."""- store, audit = await _run_pass(db_path=db, payload={+ stores, audit = await _run_pass(db_path=db, payload={"decisions": [# Valid: the quote is the user's own words, terminated.{"domain": "finance", "summary": "Cut burn to 400k",@@ -136,13 +228,16 @@ async def test_extraction_audits_proposed_stored_and_dropped(db: Path) -> None:],})- assert store.call_count == 1+ assert stores.decision.call_count == 1+ assert stores.decision.call_args.kwargs["summary"] == "Cut burn to 400k", (+ "the item that survived must be the one with a valid quote"+ )assert len(audit) == 1assert audit[0]["details"]["proposed"]["decisions"] == 2assert audit[0]["details"]["stored"]["decisions"] == 1assert audit[0]["details"]["dropped_count"] == 1assert audit[0]["details"]["dropped"] == [- {"kind": "decision", "reason": "bad_quote", "summary": "Sell the building"}+ {"kind": "decision", "reason": "bad_quote", "label": "Sell the building"}]assert audit[0]["details"]["failure"] == ""@@ -173,9 +268,9 @@ async def test_extraction_audits_proposed_stored_and_dropped(db: Path) -> None:async def test_malformed_items_are_dropped_not_raised(payload: dict[str, Any], reason: str, db: Path) -> None:- store, audit = await _run_pass(payload, db)+ stores, audit = await _run_pass(payload, db)- assert store.call_count == 0+ assert [s.call_count for s in stores] == [0, 0, 0]assert len(audit) == 1, "the pass must still audit itself"assert audit[0]["details"]["failure"] == "", "must not have raised"assert audit[0]["details"]["dropped"] == [@@ -200,7 +295,7 @@ async def test_a_null_kind_is_not_a_drop(db: Path) -> None:async def test_one_malformed_kind_does_not_lose_the_others(db: Path) -> None:"""A bad `decisions` value used to abort the pass before `initiatives`was ever read, silently discarding good items alongside the bad one."""- _, audit = await _run_pass(db_path=db, payload={+ stores, audit = await _run_pass(db_path=db, payload={"decisions": "oops","initiatives": [{"title": "Cut burn", "summary": "Down to 400k",@@ -208,6 +303,12 @@ async def test_one_malformed_kind_does_not_lose_the_others(db: Path) -> None:],})+ assert stores.initiative.call_args.kwargs == {+ "title": "Cut burn",+ "status": "active",+ "summary": "Down to 400k",+ "db_path": db,+ }assert audit[0]["details"]["stored"]["initiatives"] == 1assert audit[0]["details"]["dropped"] == [{"kind": "decision", "reason": "not_a_list"}@@ -216,14 +317,71 @@ async def test_one_malformed_kind_does_not_lose_the_others(db: Path) -> None:@pytest.mark.asyncioasync def test_a_non_dict_payload_is_dropped(db: Path) -> None:- store, audit = await _run_pass(["decisions"], db)+ stores, audit = await _run_pass(["decisions"], db)- assert store.call_count == 0+ assert [s.call_count for s in stores] == [0, 0, 0]assert audit[0]["details"]["dropped"] == [{"kind": "pass", "reason": "payload_not_a_dict"}]+ "payload,expected",+ [+ ({"decisions": [{"summary": "", "user_commitment_quote": "Cut burn to 400k"}]},+ {"kind": "decision", "reason": "missing_field", "field": "summary"}),+ ({"initiatives": [{"summary": "Down to 400k",+ "user_commitment_quote": "Cut burn to 400k"}]},+ {"kind": "initiative", "reason": "missing_field", "field": "title"}),+ ({"advice": [{"query_summary": "How much runway?",+ "user_commitment_quote": "Cut burn to 400k"}]},+ {"kind": "advice", "reason": "missing_field", "field": "advice_summary"}),+ ],+ ids=["decision-summary", "initiative-title", "advice-summary"],+)+async def test_an_incomplete_item_is_recorded_as_a_drop(+ payload: dict[str, Any], expected: dict[str, str], db: Path+) -> None:+ """These paths counted the item as proposed and then `continue`d without a+ drop record, so `proposed == stored + dropped` did not hold and the audit+ row understated how much the model had thrown away."""+ _, audit = await _run_pass(payload, db)++ assert audit[0]["details"]["dropped"] == [expected]+++async def test_proposed_equals_stored_plus_dropped(db: Path) -> None:+ """The arithmetic an operator reads the row for. One item of every+ outcome: stored, bad quote, incomplete, and a malformed non-dict."""+ _, audit = await _run_pass(db_path=db, payload={+ "decisions": [+ {"summary": "Cut burn to 400k", "user_commitment_quote": "Cut burn to 400k"},+ {"summary": "Sell the building", "user_commitment_quote": "sell the building"},+ {"summary": "", "user_commitment_quote": "Cut burn to 400k"},+ "not a dict",+ ],+ })++ details = audit[0]["details"]+ assert details["proposed"]["decisions"] == 3, "the non-dict never becomes an item"+ assert details["stored"]["decisions"] == 1+ assert details["dropped_count"] == 3, "two rejected items plus the non-dict"++ # A malformed shape is a drop that was never a proposed item, so it is+ # excluded here. Every item that WAS proposed must be stored or dropped —+ # a path that counts one and then falls through silently is the defect.+ shape_reasons = {"not_a_list", "item_not_a_dict", "payload_not_a_dict"}+ item_drops = [d for d in details["dropped"] if d["reason"] not in shape_reasons]+ assert sum(details["proposed"].values()) == (+ sum(details["stored"].values()) + len(item_drops)+ )+ assert sorted(d["reason"] for d in details["dropped"]) == [+ "bad_quote", "item_not_a_dict", "missing_field",+ ]++@pytest.mark.asyncioasync def test_a_crashed_pass_still_audits_with_a_failure_reason(db: Path) -> None:"""A provider outage that writes no row at all reads exactly like@@ -251,3 +409,155 @@ async def test_a_crashed_pass_still_audits_with_a_failure_reason(db: Path) -> Noassert len(audit) == 1assert audit[0]["details"]["failure"] == "RuntimeError"assert audit[0]["summary"].startswith("FAILED(RuntimeError)")+++# --------------------------------------------------------------------- #+# Field types+#+# `_iter_items` guards the container shapes; it says nothing about what is+# inside an item. A non-string `user_commitment_quote` or label used to raise+# out of the whole pass from inside the validator, which lost the OTHER kinds+# in the same payload — the regression the malformed-container tests above+# claim to pin, arriving through a door they do not cover.+# --------------------------------------------------------------------- #+++_VALID_INITIATIVE = {+ "title": "Cut burn",+ "summary": "Down to 400k",+ "user_commitment_quote": "Cut burn to 400k",+}+++ "decision",+ [+ {"summary": "S", "user_commitment_quote": 5},+ {"summary": 123, "user_commitment_quote": "nope"},+ {"summary": ["a"], "user_commitment_quote": None},+ ],+ ids=["int-quote", "int-summary", "list-summary"],+)+async def test_non_string_fields_do_not_abort_the_pass(+ decision: dict[str, Any], db: Path+) -> None:+ stores, audit = await _run_pass(+ db_path=db,+ payload={"decisions": [decision], "initiatives": [_VALID_INITIATIVE]},+ )++ assert audit[0]["details"]["failure"] == "", "the pass must not have raised"+ assert stores.initiative.call_count == 1, (+ "a bad decision must not cost the valid initiative beside it"+ )+ assert stores.decision.call_count == 0+++async def test_a_failed_write_is_not_counted_as_stored(db: Path) -> None:+ """`stored` counted the item before the INSERT, so a locked database+ reported `stored>0` while writing nothing.++ That inverts the one alarm this row exists for: a total store-layer+ failure would surface as a healthy-looking pass instead of the+ `proposed>0, stored=0` streak the operator is told to watch for.+ """+ class _Block:+ type = "tool_use"+ name = "store_memories"+ input = {"decisions": [+ {"summary": "Cut burn to 400k", "user_commitment_quote": "Cut burn to 400k"},+ ]}++ class _Response:+ content = [_Block()]++ rows: list[dict[str, Any]] = []++ with (+ mock.patch.object(+ episodic, "store_decision",+ side_effect=sqlite3.OperationalError("database is locked"),+ ),+ mock.patch(+ "openexecutive.audit.log_event",+ lambda event_type, summary, **kw: rows.append(+ {"event_type": event_type, "summary": summary, **kw}+ ),+ ),+ mock.patch("openexecutive.audit.usage.log_model_usage"),+ mock.patch("openexecutive.config.get_settings"),+ mock.patch.object(episodic, "get_active_initiatives", return_value=[]),+ mock.patch("openexecutive.providers.get_provider") as provider,+ ):+ provider.return_value.messages_create = mock.AsyncMock(+ return_value=_Response()+ )+ await episodic.extract_and_store(+ "Cut burn to 400k.", "ok", db_path=db, session_id="s-1"+ )++ audit = [r for r in rows if r["event_type"] == "memory_extraction"]+ assert audit[0]["details"]["stored"]["decisions"] == 0, (+ "nothing was written, so nothing may be reported as stored"+ )+ assert audit[0]["details"]["failure"] == "OperationalError"+++async def test_a_cancelled_pass_is_not_logged_as_an_empty_one(db: Path) -> None:+ """`CancelledError` is a `BaseException`, so `except Exception` misses it+ while the `finally` still writes a row.++ The row then read `proposed=0 stored=0 failure=''` — byte for byte what+ "the model proposed nothing" looks like, which is the exact ambiguity the+ row was added to remove. Cancellation must still propagate.+ """+ rows: list[dict[str, Any]] = []++ async def _hang(*args: Any, **kwargs: Any) -> Any:+ await asyncio.sleep(3600)++ with (+ mock.patch(+ "openexecutive.audit.log_event",+ lambda event_type, summary, **kw: rows.append(+ {"event_type": event_type, "summary": summary, **kw}+ ),+ ),+ mock.patch("openexecutive.config.get_settings"),+ mock.patch.object(episodic, "get_active_initiatives", return_value=[]),+ mock.patch("openexecutive.providers.get_provider") as provider,+ ):+ provider.return_value.messages_create = _hang+ task = asyncio.create_task(+ episodic.extract_and_store(+ "Cut burn to 400k.", "ok", db_path=db, session_id="s-1"+ )+ )+ await asyncio.sleep(0.05)+ task.cancel()+ with pytest.raises(asyncio.CancelledError):+ await task++ audit = [r for r in rows if r["event_type"] == "memory_extraction"]+ assert len(audit) == 1+ assert audit[0]["details"]["failure"] == "CancelledError"+ assert audit[0]["summary"].startswith("FAILED(CancelledError)")+++async def test_a_garbage_pass_does_not_read_as_an_empty_one(db: Path) -> None:+ """`proposed` counts only items the model emitted as objects, so a payload+ that was nothing but garbage would otherwise read `proposed=0 stored=0` —+ identical to a turn with nothing to extract. `malformed` separates them."""+ _, audit = await _run_pass(db_path=db, payload={"decisions": ["a", "b"]})++ details = audit[0]["details"]+ assert details["proposed"]["decisions"] == 0+ assert details["malformed_count"] == 2+ assert "malformed=2" in audit[0]["summary"]++ _, quiet = await _run_pass(db_path=db, payload={"decisions": []})+ assert quiet[0]["details"]["malformed_count"] == 0diff --git a/packages/core/tests/unit/test_web_search_tool.py b/packages/core/tests/unit/test_web_search_tool.pyindex 44df440a..9882a467 100644--- a/packages/core/tests/unit/test_web_search_tool.py+++ b/packages/core/tests/unit/test_web_search_tool.py@@ -19,6 +19,23 @@ import pytestfrom openexecutive.config import get_settings[email protected](autouse=True)+def _no_audit_writes(monkeypatch: pytest.MonkeyPatch) -> None:+ """Keep this module's `memory_extraction` rows out of the default+ `./episodic_memory.db`.++ Driving a turn schedules episodic extraction fire-and-forget, and its+ audit row goes through `audit.log_event`, which targets the default DB+ whatever `db_path` the extraction was handed. Those rows surface as real+ data in other modules' assertions during a full run only — the audit-log+ pollution trap in CLAUDE.md.+ """+ monkeypatch.setattr(+ "openexecutive.audit.log_event", lambda *a, **kw: None, raising=False+ )+++def _reset_settings_cache() -> None:# get_settings constructs a fresh Settings() each call, so just clear envs.# ENABLE_WEB_SEARCH defaults to True in config.py; pin it false here so