AI-powered virtual executive team — a single coherent executive persona backed by 8 specialist agents (FastAPI + Next.js).
fix(memory): retry a bad-quote drop once and declare the extraction event type
Rufus4ffc138fc4a4
diff --git a/packages/core/openexecutive/architecture/architecture-facts.yaml b/packages/core/openexecutive/architecture/architecture-facts.yamlindex bc4fa53b..6b448aeb 100644--- a/packages/core/openexecutive/architecture/architecture-facts.yaml+++ b/packages/core/openexecutive/architecture/architecture-facts.yaml@@ -951,6 +951,27 @@ memory: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.+ A `bad_quote` drop earns exactly one corrective call: the model is shown+ the rejected items and the quotes it gave, told to copy the span+ character-for-character (typos included) or return empty arrays, with the+ tool forced where the provider supports tool choice. Every retry item is+ validated first (a still-invalid one adds no second drop; counted+ `still_invalid`), then classified: label matches a pending bad-quote drop+ record → recovery, stored and record released; else its quote (terminal+ punctuation ignored) already supports a stored item of that kind →+ re-send, ignored and counted `repeated`; else a new proposal, stored and+ counted — a reworded label cannot be told from a different item, so its+ original drop stands. Recovery is checked before re-send because two+ items can rest on one sentence. Malformed shapes at any level (a kind+ that is not a list, an item or payload that is not a dict, a response+ with no tool call) are counted `malformed`. The row's `retry` block+ (`rejected`, `recovered`, `repeated`, `still_invalid`, `malformed`,+ `failure`) is always present; `rejected` is set from the complete drop+ list when the row is written, so neither a store failure before the+ retry nor a cancellation during it can zero it. `proposed == stored ++ item drops` still holds after the retry. A retry exception is the+ retry's own `failure`; the first pass's results stand. `memory_extraction`+ is a declared `EVENT_TYPES` member so the audit UI filter can select it.peer_memory:what: |diff --git a/packages/core/openexecutive/architecture/prebuilt/memory.json b/packages/core/openexecutive/architecture/prebuilt/memory.jsonindex dfd69cbf..b8efe5e5 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 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.",+ "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\nThe validator rejects a paraphrase, and the model paraphrases: on one install two of three real commitments were lost that way, the quote beginning \"SVN's engagement covers only …\" where the person had typed something else. So a `bad_quote` drop earns exactly one corrective call. The model is shown the rejected items and the quotes it gave, told to copy the span character-for-character (typos and casing included) or return empty arrays, and the tool is forced where the provider supports tool choice. Every retry item is validated first; a still-invalid one adds no second drop and is counted `still_invalid`. A valid item is then classified in order: its label matches a pending drop record → a recovery, stored and the record released; else its quote (terminal punctuation ignored) already supports a stored item of that kind → a re-send, ignored and counted `repeated`, so a model that re-sends everything cannot double-count `stored`; else a new proposal, stored and counted — a reworded label cannot be told from a different item, and crediting it would erase the very `bad_quote` census the row exists to show, so its original drop stands. Recovery is checked before re-send because two commitments can rest on one sentence. Malformed shapes at any level — a kind that is not a list, an item or payload that is not a dict, a response with no tool call — are counted `malformed`, so a retry that returned garbage does not read like one that obediently returned nothing. The row's `retry` block (`rejected`, `recovered`, `repeated`, `still_invalid`, `malformed`, `failure`) is always present, zeros when nothing was rejected; `rejected` is set from the complete drop list when the row is written, so neither a store failure before the retry nor a cancellation during it can zero it. The arithmetic above still holds after the retry. An exception on the retry is recorded as the retry's own `failure` and leaves the first pass's results untouched; the cost is one utility-model call, only on turns that dropped something. `memory_extraction` is a declared event type, so the audit page's filter can select it.\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"+ "generated_at": "2026-09-21T17:33:02Z"}diff --git a/packages/core/openexecutive/audit/logger.py b/packages/core/openexecutive/audit/logger.pyindex 6281566d..befea731 100644--- a/packages/core/openexecutive/audit/logger.py+++ b/packages/core/openexecutive/audit/logger.py@@ -59,6 +59,7 @@ EVENT_TYPES: tuple[str, ...] = ("memory_snapshot", # episodic context + company profile at turn entry"committee_review", # committee-reviewed draft + critiques (pre-existing emit, now declared)"peer_memory", # Honcho per-person memory — prefetch + sync_turn outcomes+ "memory_extraction", # episodic extractor — proposed / stored / dropped per pass)diff --git a/packages/core/openexecutive/memory/episodic.py b/packages/core/openexecutive/memory/episodic.pyindex 1de09d1d..d907954e 100644--- a/packages/core/openexecutive/memory/episodic.py+++ b/packages/core/openexecutive/memory/episodic.py@@ -1730,7 +1730,8 @@ _EXTRACTION_SYSTEM = ("Required for every item: a `user_commitment_quote` field with verbatim text copied from ""the USER QUESTION block. The quote must be a declarative commitment — NOT a question, ""NOT a hypothetical, NOT a paraphrase. If you cannot find such a quote in the USER QUESTION "- "text, do not include the item at all. A deterministic post-extraction validator will "+ "text, do not include the item at all. Copy it exactly as the user typed it — typos, "+ "casing and punctuation included; never correct or tidy it. A deterministic post-extraction validator will ""drop any item whose quote does not appear verbatim in the USER QUESTION or whose quote ""ends in a question mark — fabricating quotes wastes a tool call.\n""\n"@@ -1976,9 +1977,14 @@ def _accept(spec: _ItemSpec,user_message: str,dropped: list[dict[str, str]],+ rejected: list[tuple[_ItemSpec, dict[str, Any]]] | None = None,) -> bool:"""True when ``item`` is complete and genuinely the user's own commitment.+ ``rejected`` collects the full item on the bad-quote path so the retry can+ show the model exactly what it wrote; the audit record keeps only the+ truncated label.+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 isthe bug this centralises away: it broke `proposed == stored + dropped`,@@ -1999,12 +2005,14 @@ def _accept()return False- quote = str(item.get("user_commitment_quote", ""))+ quote = _quote_of(item)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]}+ {"kind": spec.kind, "reason": "bad_quote", "label": label[:_LABEL_CHARS]})+ if rejected is not None:+ rejected.append((spec, item))logger.debug("Dropping %s — invalid user_commitment_quote %r (%s=%r)",spec.kind,@@ -2016,6 +2024,296 @@ def _accept(return True+_ITEM_SPECS = (_DECISIONS, _INITIATIVES, _ADVICE)++# How much of an item's label rides in its drop record, in the retry prompt and+# in the identity the two passes use to recognise the same item. One number,+# because a drop record and a re-submission must cut the label identically to+# match up.+_LABEL_CHARS = 60++# How much of a rejected quote is echoed back to the model on the retry. The+# quote is model output and uncapped, so this bounds the retry prompt.+_RETRY_QUOTE_ECHO_CHARS = 200++# Output budget for both extraction calls. Three short arrays of items; a+# model that needs more is padding, not extracting.+_EXTRACTION_MAX_TOKENS = 1024+++def _store_item(+ spec: _ItemSpec, item: dict[str, Any], *, session_id: str, db_path: Path+) -> None:+ """Persist one accepted item. The three store functions take different+ fields, so this is the one place the mapping lives; both extraction passes+ go through it. Raises on a failed write — a locked database must surface+ as the pass's `failure`, not as a healthy-looking `stored=0`."""+ if spec is _DECISIONS:+ store_decision(+ domain=item.get("domain", "general"),+ summary=item["summary"],+ rationale=item.get("rationale", ""),+ session_id=session_id,+ db_path=db_path,+ )+ elif spec is _INITIATIVES:+ store_initiative(+ title=item["title"],+ status=item.get("status", "active"),+ summary=item["summary"],+ db_path=db_path,+ )+ else:+ store_advice(+ domain=item.get("domain", "general"),+ query_summary=item["query_summary"],+ advice_summary=item["advice_summary"],+ session_id=session_id,+ db_path=db_path,+ )+++def _run_items(+ payload: dict[str, Any],+ user_message: str,+ *,+ proposed: dict[str, int],+ stored: dict[str, int],+ dropped: list[dict[str, str]],+ rejected: list[tuple[_ItemSpec, dict[str, Any]]],+ accepted: set[tuple[str, str]],+ session_id: str,+ db_path: Path,+) -> None:+ """First pass over one tool payload: read → count → validate → store.++ ``accepted`` receives the `_stored_quote_key` of every stored item so the retry+ can tell a re-send of something already stored from a recovered or new+ item.+ """+ for spec in _ITEM_SPECS:+ for item in _iter_items(payload, spec, dropped):+ proposed[spec.key] += 1+ _validate_and_store(+ item, spec, user_message,+ dropped=dropped, rejected=rejected, stored=stored,+ accepted=accepted, session_id=session_id, db_path=db_path,+ )+++def _validate_and_store(+ item: dict[str, Any],+ spec: _ItemSpec,+ user_message: str,+ *,+ dropped: list[dict[str, str]],+ rejected: list[tuple[_ItemSpec, dict[str, Any]]] | None,+ stored: dict[str, int],+ accepted: set[tuple[str, str]],+ session_id: str,+ db_path: Path,+) -> bool:+ """First-pass item: validate and, if it passes, store and count it. The+ retry classifies an accepted item first (recovery / re-send / new), so it+ calls `_accept` and `_store_and_count` itself."""+ if not _accept(item, spec, user_message, dropped, rejected):+ return False+ _store_and_count(+ spec, item, stored=stored, accepted=accepted, session_id=session_id, db_path=db_path+ )+ return True+++def _store_and_count(+ spec: _ItemSpec,+ item: dict[str, Any],+ *,+ stored: dict[str, int],+ accepted: set[tuple[str, str]],+ session_id: str,+ db_path: Path,+) -> None:+ """Store an accepted item and count it — after the write, never before."""+ _store_item(spec, item, session_id=session_id, db_path=db_path)+ stored[spec.key] += 1+ accepted.add(_stored_quote_key(spec, item))+++def _quote_of(item: dict[str, Any]) -> str:+ """The item's quote as text. A JSON `null` is no quote, not the word+ "None" — which `str()` would make it, and which then matches any user+ message containing that word."""+ quote = item.get("user_commitment_quote")+ return "" if quote is None else str(quote)+++def _drop_label_key(spec: _ItemSpec, item: dict[str, Any]) -> tuple[str, str]:+ """How a re-submission is matched to its drop record: kind and label, cut+ the way the record cuts it. A reworded label does not match — the item is+ then stored as a new proposal and the original drop stands, because+ "reworded" and "different" cannot be told apart."""+ return spec.kind, str(item.get(spec.label, ""))[:_LABEL_CHARS]+++def _stored_quote_key(spec: _ItemSpec, item: dict[str, Any]) -> tuple[str, str]:+ """How a re-sent, already-stored item is recognised: kind and the quote it+ rests on, ignoring terminal punctuation. The model rewords labels freely+ but keeps a quote that passed, so the quote is the stable identity; asked+ to re-copy character-for-character it may gain or lose the full stop, so+ that must not make a new identity. Checked only after the drop records:+ a correctly re-quoted item whose label matches a pending drop is a+ recovery even when its sentence already supports a stored item."""+ return spec.kind, _normalize_for_quote_match(_quote_of(item)).strip(" .!")+++def _find_drop(dropped: list[dict[str, str]], key: tuple[str, str]) -> int | None:+ """Index of the bad-quote drop record a re-submitted item was filed under.++ Only an exact `(kind, label)` match counts. A looser rule — "any bad-quote+ drop of this kind" — would let an unrelated valid item consume the record+ and report a recovery that never happened, erasing the very `bad_quote`+ census this row exists to show. None means the item is a new proposal,+ whether or not the model meant it as a reworded re-send.+ """+ kind, label = key+ for i, d in enumerate(dropped):+ if d["kind"] == kind and d["reason"] == "bad_quote" and d.get("label") == label:+ return i+ return None+++def _retry_prompt(+ turn_block: str, rejected: list[tuple[_ItemSpec, dict[str, Any]]]+) -> str:+ lines = []+ for spec, item in rejected:+ _, label = _drop_label_key(spec, item)+ quote = _quote_of(item)[:_RETRY_QUOTE_ECHO_CHARS]+ lines.append(f'- {spec.kind} "{label}": quote given "{quote}"')+ return (+ f"{turn_block}\n\n"+ "REJECTED ITEMS — the user_commitment_quote you gave was NOT found "+ "verbatim in USER QUESTION:\n"+ + "\n".join(lines)+ + "\n\nRe-submit ONLY these items. Copy user_commitment_quote "+ "character-for-character from USER QUESTION, including typos and casing. "+ "If USER QUESTION has no declarative sentence that supports an item, omit "+ "it. Do not add new items. Return empty arrays for anything you cannot "+ "re-quote."+ )+++async def _retry_rejected(+ rejected: list[tuple[_ItemSpec, dict[str, Any]]],+ *,+ turn_block: str,+ user_message: str,+ routing_model: str,+ proposed: dict[str, int],+ stored: dict[str, int],+ dropped: list[dict[str, str]],+ accepted: set[tuple[str, str]],+ result: dict[str, Any],+ session_id: str,+ db_path: Path,+) -> None:+ """One corrective pass for items the first pass dropped as `bad_quote`.++ The model has repeatedly paraphrased the user's words instead of copying+ them, and the validator (rightly) rejects a paraphrase — so a real+ commitment was lost on the first slip with no second chance. This shows+ the model exactly which quotes failed and asks for the verbatim span, with+ the tool forced so a text-only reply cannot happen. Cost: one utility-model+ call, only on turns that dropped something.++ Accounting keeps `proposed == stored + item drops` true. Every retry item+ is validated first; a still-invalid one adds no second drop and is counted+ under `still_invalid`. A valid item is then classified in this order: its+ label matches a pending bad-quote drop record → a recovery, stored and the+ record released; else its quote (same kind, terminal punctuation ignored)+ already supports a stored item → a re-send, ignored and counted under+ `repeated`; else a new proposal, stored and counted — including a+ re-submission whose label the model reworded, since that cannot be told+ from a different item, and its original drop then stands. Recovery is+ checked before re-send because two items can rest on one sentence.+ Malformed shapes at any level — a kind that is not a list, an item that is+ not a dict, a tool payload that is not a dict, or a response with no+ `store_memories` call at all — are counted under `malformed`, so a retry+ that returned garbage does not read as one that obediently returned+ nothing. The tool is forced where the provider supports tool choice.++ ``result`` is the caller's `retry` block, mutated in place: work done+ before a cancellation stays counted. An `Exception` here is recorded as+ the retry's own `failure` and leaves the first pass's results untouched;+ cancellation propagates so the whole pass is marked FAILED, as it is+ today.+ """+ from openexecutive.audit.usage import log_model_usage+ from openexecutive.providers import get_provider++ scratch: list[dict[str, str]] = []+ tool_calls: int | None = None # None until a response arrives+ try:+ response = await get_provider(routing_model).messages_create(+ model=routing_model,+ max_tokens=_EXTRACTION_MAX_TOKENS,+ system=_EXTRACTION_SYSTEM,+ tools=[_EXTRACTION_TOOL],+ tool_choice={"type": "tool", "name": "store_memories"},+ messages=[{"role": "user", "content": _retry_prompt(turn_block, rejected)}],+ )+ log_model_usage(response, model=routing_model, actor="memory_extractor")++ tool_calls = 0+ for block in response.content:+ if block.type != "tool_use" or block.name != "store_memories":+ continue+ tool_calls += 1+ inp = block.input+ if not isinstance(inp, dict):+ scratch.append({"kind": "pass", "reason": "payload_not_a_dict"})+ continue+ for spec in _ITEM_SPECS:+ for item in _iter_items(inp, spec, scratch):+ if not _accept(item, spec, user_message, scratch):+ continue+ drop_at = _find_drop(dropped, _drop_label_key(spec, item))+ if drop_at is None and _stored_quote_key(spec, item) in accepted:+ result["repeated"] += 1+ continue+ _store_and_count(+ spec, item, stored=stored, accepted=accepted,+ session_id=session_id, db_path=db_path,+ )+ if drop_at is None:+ proposed[spec.key] += 1+ else:+ del dropped[drop_at]+ result["recovered"] += 1+ except Exception as exc:+ result["failure"] = type(exc).__name__+ logger.exception("Episodic memory extraction retry failed — keeping first pass")+ finally:+ # Counted in `finally` so a retry that died mid-loop still reports the+ # drops it had already seen, not zeros beside its `failure`. A response+ # with no tool call is one malformed shape; no response at all is not.+ malformed = sum(1 for d in scratch if d["reason"] in _SHAPE_REASONS)+ result["malformed"] = malformed + (1 if tool_calls == 0 else 0)+ result["still_invalid"] = len(scratch) - malformed+++def _no_retry() -> dict[str, Any]:+ """The `retry` audit shape when no corrective pass ran (or before it has)."""+ return {+ "rejected": 0,+ "recovered": 0,+ "repeated": 0,+ "still_invalid": 0,+ "malformed": 0,+ "failure": "",+ }++def _audit_extraction(proposed: dict[str, int],stored: dict[str, int],@@ -2023,9 +2321,14 @@ def _audit_extraction(*,session_id: str,failure: str = "",+ retry: dict[str, Any] | None = None,) -> None:"""One `memory_extraction` audit row per extraction pass.+ `retry` is always present in the details (zeros when nothing was rejected)+ so a query never has to branch on key presence; it says how many bad-quote+ drops a corrective pass was owed and how many it recovered.+The point is that "the model proposed nothing" and "the model proposedthings and every one was rejected" are different failures with differentfixes, and until this row existed they were indistinguishable outside a@@ -2051,13 +2354,17 @@ def _audit_extraction(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 ""+ retry = retry if retry is not None else _no_retry()+ retry_note = (+ f" retry={retry['recovered']}/{retry['rejected']}" if retry["rejected"] else ""+ )try:from openexecutive.audit import log_eventlog_event("memory_extraction",f"{prefix}proposed={total_proposed} stored={total_stored} "- f"dropped={len(dropped)} malformed={malformed}",+ f"dropped={len(dropped)} malformed={malformed}{retry_note}",session_id=session_id or None,actor="memory_extractor",details={@@ -2070,6 +2377,7 @@ def _audit_extraction(# drops on decisions this week" is a query, not a string split# over a field that can itself contain colons."dropped": dropped[:_MAX_DROPPED_IN_AUDIT],+ "retry": retry,},)except Exception:@@ -2129,6 +2437,9 @@ async def _extract_and_store(proposed = {"decisions": 0, "initiatives": 0, "advice": 0}stored = {"decisions": 0, "initiatives": 0, "advice": 0}dropped: list[dict[str, str]] = []+ rejected: list[tuple[_ItemSpec, dict[str, Any]]] = []+ accepted: set[tuple[str, str]] = set()+ retry = _no_retry()failure = ""try:from openexecutive.audit.usage import log_model_usage@@ -2155,22 +2466,18 @@ async def _extract_and_store(else:existing_block = ""+ turn_block = (+ f"{existing_block}"+ f"USER QUESTION:\n{user_message[:_MAX_INPUT_CHARS]}\n\n"+ f"EXECUTIVE RESPONSE:\n{assistant_response[:_MAX_INPUT_CHARS]}"+ )response = await get_provider(routing_model).messages_create(model=routing_model,- max_tokens=1024,+ max_tokens=_EXTRACTION_MAX_TOKENS,system=_EXTRACTION_SYSTEM,tools=[_EXTRACTION_TOOL],tool_choice={"type": "auto"},- messages=[- {- "role": "user",- "content": (- f"{existing_block}"- f"USER QUESTION:\n{user_message[:_MAX_INPUT_CHARS]}\n\n"- f"EXECUTIVE RESPONSE:\n{assistant_response[:_MAX_INPUT_CHARS]}"- ),- }- ],+ messages=[{"role": "user", "content": turn_block}],)log_model_usage(response, model=routing_model, actor="memory_extractor")@@ -2190,45 +2497,32 @@ async def _extract_and_store(if not isinstance(inp, dict):dropped.append({"kind": "pass", "reason": "payload_not_a_dict"})continue- # 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- if not _accept(d, _DECISIONS, user_message, dropped):- continue- store_decision(- domain=d.get("domain", "general"),- summary=d["summary"],- rationale=d.get("rationale", ""),- session_id=session_id,- db_path=db_path,- )- stored["decisions"] += 1- for i in _iter_items(inp, _INITIATIVES, dropped):- proposed["initiatives"] += 1- 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- for a in _iter_items(inp, _ADVICE, dropped):- proposed["advice"] += 1- if not _accept(a, _ADVICE, user_message, dropped):- continue- store_advice(- 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"] += 1+ _run_items(+ inp,+ user_message,+ proposed=proposed,+ stored=stored,+ dropped=dropped,+ rejected=rejected,+ accepted=accepted,+ session_id=session_id,+ db_path=db_path,+ )++ if rejected:+ await _retry_rejected(+ rejected,+ turn_block=turn_block,+ user_message=user_message,+ routing_model=routing_model,+ proposed=proposed,+ stored=stored,+ dropped=dropped,+ accepted=accepted,+ result=retry,+ session_id=session_id,+ db_path=db_path,+ )except Exception as exc:# Recorded so the audit row can say the pass FAILED. Without it a@@ -2248,9 +2542,13 @@ async def _extract_and_store(raisefinally:# In `finally`, not the happy path: a pass that crashed is exactly the- # one an operator needs to see.+ # one an operator needs to see. `rejected` is the count of bad-quote+ # drops a corrective pass was owed, set here from the complete list so+ # neither a store failure before the retry nor a cancellation during+ # it can leave a block that denies the drop records beside it.+ retry["rejected"] = len(rejected)_audit_extraction(- proposed, stored, dropped, session_id=session_id, failure=failure+ proposed, stored, dropped, session_id=session_id, failure=failure, retry=retry)diff --git a/packages/core/tests/unit/test_audit_logger.py b/packages/core/tests/unit/test_audit_logger.pyindex 5754cf19..6817f65e 100644--- a/packages/core/tests/unit/test_audit_logger.py+++ b/packages/core/tests/unit/test_audit_logger.py@@ -177,6 +177,9 @@ def test_event_types_constant_includes_known_types() -> None:assert "scheduled_action" in EVENT_TYPESassert "alert" in EVENT_TYPESassert "integration_inbound" in EVENT_TYPES+ assert "memory_extraction" in EVENT_TYPES, (+ "every extraction pass writes this row; it must be selectable in the audit UI filter"+ )def test_q_escapes_like_wildcards(audit: AuditLogger) -> None:diff --git a/packages/core/tests/unit/test_episodic.py b/packages/core/tests/unit/test_episodic.pyindex e9725a56..fdca148a 100644--- a/packages/core/tests/unit/test_episodic.py+++ b/packages/core/tests/unit/test_episodic.py@@ -795,6 +795,9 @@ def test_extract_and_store_drops_decision_with_question_quote(assert ep.list_decisions(db_path=db) == [], ("decision with question-mark quote must be dropped by the validator")+ assert create_mock.await_count == 2, (+ "a bad quote earns one corrective call; the replayed quote is rejected again"+ )def test_extract_and_store_stores_decision_with_valid_quote(@@ -888,3 +891,6 @@ def test_extract_and_store_drops_initiative_with_hallucinated_quote(assert ep.list_initiatives(db_path=db) == [], ("initiative with hallucinated user_commitment_quote must be dropped")+ assert create_mock.await_count == 2, (+ "a bad quote earns one corrective call; the replayed quote is rejected again"+ )diff --git a/packages/core/tests/unit/test_extraction_gate.py b/packages/core/tests/unit/test_extraction_gate.pyindex 1be193a1..02c05092 100644--- a/packages/core/tests/unit/test_extraction_gate.py+++ b/packages/core/tests/unit/test_extraction_gate.py@@ -156,31 +156,72 @@ def test_a_missing_person_row_fails_closed() -> None:)+class _Pass(NamedTuple):+ stores: _Stores+ audit: list[dict[str, Any]]+ create: mock.AsyncMock+ """The provider's `messages_create` — call count and prompts of both passes."""+++def _tool_response(payload: Any) -> Any:+ class _Block:+ type = "tool_use"+ name = "store_memories"+ input = payload++ class _Response:+ content = [_Block()]++ return _Response()+++# Sentinel: the retry (if any) replays the first payload, as a single+# `return_value` mock does. Tests that exercise the retry pass a payload.+_REPLAY = object()++# The `retry` block of a pass that owed no corrective call — the code's own+# shape, so a renamed key fails these tests at the cause rather than drifting.+_NO_RETRY = episodic._no_retry()++async def _run_pass(payload: Any,db_path: Path,*,user_message: str = "Cut burn to 400k. Tell the team.",+ retry_payload: Any = _REPLAY,) -> tuple[_Stores, list[dict[str, Any]]]:+ """`_run_pass_full` for tests that only read the stores and the audit row."""+ result = await _run_pass_full(+ payload, db_path, user_message=user_message, retry_payload=retry_payload+ )+ return result.stores, result.audit+++async def _run_pass_full(+ payload: Any,+ db_path: Path,+ *,+ user_message: str = "Cut burn to 400k. Tell the team.",+ retry_payload: Any = _REPLAY,+) -> _Pass:"""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.+ `retry_payload` is what the model answers on the corrective pass that a+ `bad_quote` drop triggers: a payload, a whole response object (anything+ with `.content`), or an exception instance to raise.+ Left alone, the mock replays `payload`, so a test with a bad quote and no+ interest in the retry sees the item rejected a second time.+All three store functions are mocked, so every kind is asserted the sameway — 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 thedefault `./episodic_memory.db` if a code path stops going through them."""- class _Block:- type = "tool_use"- name = "store_memories"- input = payload-- class _Response:- content = [_Block()]-rows: list[dict[str, Any]] = []def _capture(event_type: str, summary: str, **kw: Any) -> None:@@ -196,16 +237,23 @@ async def _run_pass(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()- )+ if retry_payload is _REPLAY:+ create = mock.AsyncMock(return_value=_tool_response(payload))+ else:+ if isinstance(retry_payload, BaseException) or hasattr(retry_payload, "content"):+ second = retry_payload+ else:+ second = _tool_response(retry_payload)+ create = mock.AsyncMock(side_effect=[_tool_response(payload), second])+ provider.return_value.messages_create = createawait episodic.extract_and_store(user_message, "Understood.", db_path=db_path, session_id="s-1")- return (+ return _Pass(_Stores(store_decision, store_initiative, store_advice),[r for r in rows if r["event_type"] == "memory_extraction"],+ create,)@@ -226,7 +274,7 @@ async def test_extraction_audits_proposed_stored_and_dropped(db: Path) -> None:{"domain": "finance", "summary": "Sell the building","user_commitment_quote": "sell the building"},],- })+ }, retry_payload={}) # the corrective pass finds nothing to re-quoteassert stores.decision.call_count == 1assert stores.decision.call_args.kwargs["summary"] == "Cut burn to 400k", (@@ -240,6 +288,9 @@ async def test_extraction_audits_proposed_stored_and_dropped(db: Path) -> None:{"kind": "decision", "reason": "bad_quote", "label": "Sell the building"}]assert audit[0]["details"]["failure"] == ""+ assert audit[0]["details"]["retry"] == {+ **_NO_RETRY, "rejected": 1,+ }# --------------------------------------------------------------------- #@@ -362,7 +413,7 @@ async def test_proposed_equals_stored_plus_dropped(db: Path) -> None:{"summary": "", "user_commitment_quote": "Cut burn to 400k"},"not a dict",],- })+ }, retry_payload={}) # the bad quote triggers a retry; it re-quotes nothingdetails = audit[0]["details"]assert details["proposed"]["decisions"] == 3, "the non-dict never becomes an item"@@ -445,6 +496,7 @@ async def test_non_string_fields_do_not_abort_the_pass(stores, audit = await _run_pass(db_path=db,payload={"decisions": [decision], "initiatives": [_VALID_INITIATIVE]},+ retry_payload={}, # a rejected quote triggers a retry; it re-quotes nothing)assert audit[0]["details"]["failure"] == "", "the pass must not have raised"@@ -565,3 +617,438 @@ async def test_a_garbage_pass_does_not_read_as_an_empty_one(db: Path) -> None:_, quiet = await _run_pass(db_path=db, payload={"decisions": []})assert quiet[0]["details"]["malformed_count"] == 0+++# --------------------------------------------------------------------- #+# The corrective retry+#+# The validator rejects a paraphrase, and the model paraphrases: on a live+# install two of three real commitments were lost that way — the quote it+# gave began "SVN's engagement covers only ..." where the user had typed+# something else. One more call, only on turns that dropped a bad quote,+# shows the model exactly what failed and asks for the verbatim span.+# --------------------------------------------------------------------- #++_USER = "therea re only 2 properties in SVN scope. Tell the team."+_BAD = {+ "domain": "operations",+ "summary": "SVN scope is two properties",+ "user_commitment_quote": "There are only two properties in SVN's scope",+}+_GOOD = {**_BAD, "user_commitment_quote": "therea re only 2 properties in SVN scope"}+++async def test_a_bad_quote_is_retried_once_and_recovered(db: Path) -> None:+ result = await _run_pass_full(+ {"decisions": [_BAD]}, db, user_message=_USER,+ retry_payload={"decisions": [_GOOD]},+ )++ assert result.create.await_count == 2, "exactly one corrective call"+ assert result.stores.decision.call_count == 1+ assert result.stores.decision.call_args.kwargs["summary"] == _BAD["summary"]+ details = result.audit[0]["details"]+ assert details["proposed"]["decisions"] == 1, "a recovery is not a new proposal"+ assert details["stored"]["decisions"] == 1+ assert details["dropped"] == [], "the recovered item's drop record is released"+ assert details["retry"] == {**_NO_RETRY, "rejected": 1, "recovered": 1}+ assert result.audit[0]["summary"].endswith("retry=1/1")+++async def test_the_retry_prompt_names_the_rejected_item_and_forces_the_tool(+ db: Path,+) -> None:+ result = await _run_pass_full(+ {"decisions": [_BAD]}, db, user_message=_USER, retry_payload={},+ )++ first, second = result.create.call_args_list+ content = second.kwargs["messages"][0]["content"]+ assert content.startswith(first.kwargs["messages"][0]["content"]), (+ "the retry sees the same turn the first pass saw"+ )+ assert "REJECTED ITEMS" in content+ assert _BAD["summary"] in content and _BAD["user_commitment_quote"] in content+ assert "character-for-character" in content+ assert second.kwargs["tool_choice"] == {"type": "tool", "name": "store_memories"}+ assert second.kwargs["system"] == first.kwargs["system"]+++async def test_no_retry_without_a_bad_quote_drop(db: Path) -> None:+ """A missing field is not something a second call can fix."""+ result = await _run_pass_full(+ {"decisions": [{"summary": "", "user_commitment_quote": "Cut burn to 400k"}]},+ db,+ )++ assert result.create.await_count == 1+ assert result.audit[0]["details"]["retry"] == _NO_RETRY+ assert "retry=" not in result.audit[0]["summary"]+++async def test_a_retry_that_still_fails_keeps_the_original_drop(db: Path) -> None:+ result = await _run_pass_full(+ {"decisions": [_BAD]}, db, user_message=_USER,+ retry_payload={"decisions": [_BAD]},+ )++ details = result.audit[0]["details"]+ assert details["dropped"] == [+ {"kind": "decision", "reason": "bad_quote", "label": _BAD["summary"]}+ ], "still invalid: one record, not two"+ assert details["stored"]["decisions"] == 0+ assert details["retry"] == {**_NO_RETRY, "rejected": 1, "still_invalid": 1}, (+ "the second bad quote is counted, not filed as a second drop"+ )+ assert sum(details["proposed"].values()) == (+ sum(details["stored"].values()) + len(details["dropped"])+ )+++async def test_extra_retry_items_count_as_new_proposals(db: Path) -> None:+ """Told to add nothing, the model may still add; a valid extra item is+ real, so it is stored and counted as proposed — never as a recovery, in+ whichever order it arrives."""+ extra = {"summary": "Tell the team", "user_commitment_quote": "Tell the team"}+ result = await _run_pass_full(+ {"decisions": [_BAD]}, db, user_message=_USER,+ retry_payload={"decisions": [extra, _GOOD]},+ )++ details = result.audit[0]["details"]+ assert details["proposed"]["decisions"] == 2+ assert details["stored"]["decisions"] == 2+ assert details["dropped"] == []+ assert details["retry"]["recovered"] == 1+++async def test_a_re_submitted_stored_item_is_not_stored_twice(db: Path) -> None:+ """The model re-sending everything must not double-count `stored`."""+ valid = {"summary": "Tell the team", "user_commitment_quote": "Tell the team"}+ result = await _run_pass_full(+ {"decisions": [valid, _BAD]}, db, user_message=_USER,+ retry_payload={"decisions": [valid, _GOOD]},+ )++ details = result.audit[0]["details"]+ assert result.stores.decision.call_count == 2, "valid once, recovered once"+ assert details["proposed"]["decisions"] == 2+ assert details["stored"]["decisions"] == 2+ assert details["retry"] == {**_NO_RETRY, "rejected": 1, "recovered": 1, "repeated": 1}+++async def test_a_recovery_of_one_kind_does_not_release_another(db: Path) -> None:+ result = await _run_pass_full(+ {"decisions": [_BAD]}, db, user_message=_USER,+ retry_payload={"initiatives": [{+ "title": "SVN scope", "summary": "Two properties",+ "user_commitment_quote": "therea re only 2 properties in SVN scope",+ }]},+ )++ details = result.audit[0]["details"]+ assert details["dropped"] == [+ {"kind": "decision", "reason": "bad_quote", "label": _BAD["summary"]}+ ]+ assert details["proposed"] == {"decisions": 1, "initiatives": 1, "advice": 0}+ assert details["stored"] == {"decisions": 0, "initiatives": 1, "advice": 0}+ assert details["retry"]["recovered"] == 0+++async def test_a_retry_failure_keeps_the_first_pass_results(db: Path) -> None:+ valid = {"summary": "Tell the team", "user_commitment_quote": "Tell the team"}+ result = await _run_pass_full(+ {"decisions": [valid, _BAD]}, db, user_message=_USER,+ retry_payload=RuntimeError("upstream 529"),+ )++ details = result.audit[0]["details"]+ assert details["failure"] == "", "the pass itself did not fail"+ assert not result.audit[0]["summary"].startswith("FAILED")+ assert details["stored"]["decisions"] == 1+ assert details["dropped"] == [+ {"kind": "decision", "reason": "bad_quote", "label": _BAD["summary"]}+ ]+ assert details["retry"] == {**_NO_RETRY, "rejected": 1, "failure": "RuntimeError"}+++async def test_a_cancelled_retry_is_logged_as_failed(db: Path) -> None:+ """Cancellation is not the retry's to swallow: the row must say FAILED,+ and its `retry` block must still say a corrective pass was owed."""+ rows: list[dict[str, Any]] = []++ def _capture(event_type: str, summary: str, **kw: Any) -> None:+ rows.append({"event_type": event_type, "summary": summary, **kw})++ with (+ mock.patch.object(episodic, "store_decision"),+ mock.patch("openexecutive.audit.log_event", _capture),+ 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(+ side_effect=[_tool_response({"decisions": [_BAD]}), asyncio.CancelledError()]+ )+ with pytest.raises(asyncio.CancelledError):+ await episodic.extract_and_store(_USER, "ok", db_path=db, session_id="s-1")++ audit = [r for r in rows if r["event_type"] == "memory_extraction"]+ assert audit[0]["summary"].startswith("FAILED(CancelledError)")+ assert audit[0]["details"]["dropped"] == [+ {"kind": "decision", "reason": "bad_quote", "label": _BAD["summary"]}+ ]+ assert audit[0]["details"]["retry"] == {**_NO_RETRY, "rejected": 1}, (+ "a zeroed block would deny the retry that the drop record proves was owed"+ )+++async def test_no_retry_when_the_first_pass_failed(db: Path) -> None:+ rows: list[dict[str, Any]] = []++ def _capture(event_type: str, summary: str, **kw: Any) -> None:+ rows.append({"event_type": event_type, "summary": summary, **kw})++ with (+ mock.patch("openexecutive.audit.log_event", _capture),+ mock.patch("openexecutive.config.get_settings"),+ mock.patch.object(episodic, "get_active_initiatives", return_value=[]),+ mock.patch("openexecutive.providers.get_provider") as provider,+ ):+ create = mock.AsyncMock(side_effect=RuntimeError("upstream 529"))+ provider.return_value.messages_create = create+ await episodic.extract_and_store(_USER, "ok", db_path=db, session_id="s-1")++ assert create.await_count == 1+ audit = [r for r in rows if r["event_type"] == "memory_extraction"]+ assert audit[0]["details"]["failure"] == "RuntimeError"+ assert audit[0]["details"]["retry"] == _NO_RETRY+++async def test_a_reworded_re_submission_is_a_new_proposal_and_the_drop_stands(+ db: Path,+) -> None:+ """A valid item that matches no drop record by label is not a recovery,+ even if the model meant it as one — "reworded" and "different" cannot be+ told apart, and crediting it would erase the bad-quote census."""+ reworded = {**_GOOD, "summary": "Only two properties are in SVN's scope"}+ result = await _run_pass_full(+ {"decisions": [_BAD]}, db, user_message=_USER,+ retry_payload={"decisions": [reworded]},+ )++ details = result.audit[0]["details"]+ assert result.stores.decision.call_count == 1, "the valid item is still kept"+ assert details["proposed"]["decisions"] == 2+ assert details["stored"]["decisions"] == 1+ assert details["dropped"] == [+ {"kind": "decision", "reason": "bad_quote", "label": _BAD["summary"]}+ ]+ assert details["retry"]["recovered"] == 0+++async def test_a_re_sent_stored_item_with_a_reworded_label_is_repeated(+ db: Path,+) -> None:+ """The model rewords labels but keeps a quote that passed, so the quote+ is what identifies a re-send. Same quote, new label: stored once."""+ valid = {"summary": "Tell the team about it", "user_commitment_quote": "Tell the team"}+ reworded = {**valid, "summary": "Tell the whole team about it"}+ result = await _run_pass_full(+ {"decisions": [valid, _BAD]}, db, user_message=_USER,+ retry_payload={"decisions": [reworded, _GOOD]},+ )++ details = result.audit[0]["details"]+ assert result.stores.decision.call_count == 2, "valid once, recovered once"+ assert details["stored"]["decisions"] == 2+ assert details["retry"] == {**_NO_RETRY, "rejected": 1, "recovered": 1, "repeated": 1}+++async def test_a_corrected_item_sharing_a_stored_label_is_still_recovered(+ db: Path,+) -> None:+ """Two items with the same summary, one stored and one rejected: the+ corrected one carries a different quote, so it is not a re-send."""+ stored_twin = {**_BAD, "user_commitment_quote": "Tell the team"}+ result = await _run_pass_full(+ {"decisions": [stored_twin, _BAD]}, db, user_message=_USER,+ retry_payload={"decisions": [_GOOD]},+ )++ details = result.audit[0]["details"]+ assert result.stores.decision.call_count == 2+ assert details["dropped"] == []+ assert details["retry"] == {**_NO_RETRY, "rejected": 1, "recovered": 1}+++async def test_retry_garbage_is_counted_not_hidden(db: Path) -> None:+ """A retry that returned only garbage must not read like one that+ obediently returned empty arrays — the ambiguity the row exists to remove."""+ result = await _run_pass_full(+ {"decisions": [_BAD]}, db, user_message=_USER,+ retry_payload={"decisions": "nope", "advice": ["x"], "initiatives": [_BAD]},+ )++ details = result.audit[0]["details"]+ assert details["malformed_count"] == 0, "the first pass was clean"+ assert details["dropped"] == [+ {"kind": "decision", "reason": "bad_quote", "label": _BAD["summary"]}+ ], "retry drops never become first-pass drop records"+ assert details["retry"] == {+ **_NO_RETRY, "rejected": 1, "malformed": 2, "still_invalid": 1,+ }+++async def test_a_correctly_requoted_item_sharing_a_stored_sentence_is_recovered(+ db: Path,+) -> None:+ """Two commitments can rest on one sentence. A re-submission whose label+ matches a pending drop is a recovery even though its (now correct) quote+ already supports a stored item — the drop record is checked first."""+ first = {"summary": "Cut burn to 400k", "user_commitment_quote": "Cut burn to 400k"}+ freeze_bad = {"summary": "Freeze hiring", "user_commitment_quote": "We will freeze hiring"}+ freeze_good = {**freeze_bad, "user_commitment_quote": "Cut burn to 400k"}+ result = await _run_pass_full(+ {"decisions": [first, freeze_bad]}, db, user_message="Cut burn to 400k.",+ retry_payload={"decisions": [freeze_good]},+ )++ details = result.audit[0]["details"]+ assert [c.kwargs["summary"] for c in result.stores.decision.call_args_list] == [+ "Cut burn to 400k", "Freeze hiring",+ ]+ assert details["dropped"] == []+ assert details["retry"] == {**_NO_RETRY, "rejected": 1, "recovered": 1}+++async def test_a_re_sent_item_whose_quote_gained_a_full_stop_is_repeated(+ db: Path,+) -> None:+ """Asked to re-copy character-for-character, the model may add or drop the+ terminator; that must not mint a new identity and a second `stored`."""+ valid = {"summary": "Tell the team", "user_commitment_quote": "Tell the team"}+ result = await _run_pass_full(+ {"decisions": [valid, _BAD]}, db, user_message=_USER,+ retry_payload={"decisions": [+ {**valid, "user_commitment_quote": "Tell the team."}, _GOOD,+ ]},+ )++ details = result.audit[0]["details"]+ assert result.stores.decision.call_count == 2, "valid once, recovered once"+ assert details["proposed"]["decisions"] == 2+ assert details["retry"] == {**_NO_RETRY, "rejected": 1, "recovered": 1, "repeated": 1}+++async def test_an_invalid_re_send_is_still_invalid_not_repeated(db: Path) -> None:+ """Validation comes first: an item with a stored quote but no summary was+ never a re-send of anything."""+ valid = {"summary": "Tell the team", "user_commitment_quote": "Tell the team"}+ result = await _run_pass_full(+ {"decisions": [valid, _BAD]}, db, user_message=_USER,+ retry_payload={"decisions": [{"summary": "", "user_commitment_quote": "Tell the team"}]},+ )++ assert result.audit[0]["details"]["retry"] == {+ **_NO_RETRY, "rejected": 1, "still_invalid": 1,+ }+++class _NoToolCall:+ """A retry response with no `store_memories` block at all."""+ content: list[Any] = []+++ "retry_payload",+ [["decisions"], _NoToolCall()],+ ids=["payload-not-a-dict", "no-tool-call"],+)+async def test_retry_garbage_above_item_level_is_counted(+ retry_payload: Any, db: Path+) -> None:+ """A retry that returned a non-dict payload, or no `store_memories` call at+ all, must not read like one that obediently returned empty arrays."""+ result = await _run_pass_full(+ {"decisions": [_BAD]}, db, user_message=_USER, retry_payload=retry_payload,+ )++ assert result.audit[0]["details"]["retry"] == {+ **_NO_RETRY, "rejected": 1, "malformed": 1,+ }+++async def test_a_store_failure_before_the_retry_still_reports_what_was_owed(+ db: Path,+) -> None:+ """`rejected` is the count of drops a corrective pass was owed, whether or+ not the pass lived long enough to run it."""+ rows: list[dict[str, Any]] = []++ def _capture(event_type: str, summary: str, **kw: Any) -> None:+ rows.append({"event_type": event_type, "summary": summary, **kw})++ valid = {"summary": "Tell the team", "user_commitment_quote": "Tell the team"}+ with (+ mock.patch.object(+ episodic, "store_decision", side_effect=sqlite3.OperationalError("locked")+ ),+ mock.patch("openexecutive.audit.log_event", _capture),+ 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,+ ):+ create = mock.AsyncMock(return_value=_tool_response({"decisions": [_BAD, valid]}))+ provider.return_value.messages_create = create+ await episodic.extract_and_store(_USER, "ok", db_path=db, session_id="s-1")++ assert create.await_count == 1, "no retry after the pass failed"+ audit = [r for r in rows if r["event_type"] == "memory_extraction"]+ assert audit[0]["details"]["failure"] == "OperationalError"+ assert audit[0]["details"]["retry"] == {**_NO_RETRY, "rejected": 1}+++async def test_a_null_quote_is_not_the_word_none(db: Path) -> None:+ """`str(None)` is "None", which a user message can contain."""+ _, audit = await _run_pass(+ {"decisions": [{"summary": "Nothing is sold", "user_commitment_quote": None}]},+ db, user_message="None of them are sold.", retry_payload={},+ )++ assert audit[0]["details"]["stored"]["decisions"] == 0+ assert audit[0]["details"]["dropped"] == [+ {"kind": "decision", "reason": "bad_quote", "label": "Nothing is sold"}+ ]+++def test_the_retry_prompt_shows_a_null_quote_as_empty() -> None:+ """The model wrote JSON null, not the word "None"; the prompt must not+ tell it otherwise."""+ prompt = episodic._retry_prompt(+ "USER QUESTION:\nx", [(episodic._DECISIONS, {"summary": "X", "user_commitment_quote": None})]+ )+ assert '- decision "X": quote given ""' in prompt+ assert "None" not in promptdiff --git a/packages/core/tests/unit/test_usage_recording.py b/packages/core/tests/unit/test_usage_recording.pyindex 446363cc..9d5c714a 100644--- a/packages/core/tests/unit/test_usage_recording.py+++ b/packages/core/tests/unit/test_usage_recording.py@@ -185,3 +185,32 @@ def test_memory_extractor_records_usage(rows = audit.query(event_type="cache_event")assert len(rows) == 1 and rows[0].actor == "memory_extractor"assert rows[0].details["model"] == "claude-test"+++def test_memory_extractor_retry_records_its_own_usage(+ audit: AuditLogger, tmp_path: Path, monkeypatch: pytest.MonkeyPatch,+) -> None:+ """The corrective pass is a second model call and must show up as one."""+ from openexecutive.memory import episodic as ep++ db = tmp_path / "episodic.db"+ ep.initialize_db(db)+ bad_quote = SimpleNamespace(+ type="tool_use", name="store_memories",+ input={"decisions": [{+ "summary": "Cut burn", "user_commitment_quote": "we will cut the burn",+ }]},+ )+ first = _response(input_tokens=12, output_tokens=2)+ first.content = [bad_quote]+ provider = SimpleNamespace(messages_create=AsyncMock(side_effect=[+ first, _response(input_tokens=30, output_tokens=1),+ ]))+ monkeypatch.setattr("openexecutive.providers.get_provider", lambda _m: provider)+ monkeypatch.setattr(+ "openexecutive.config.get_settings", lambda: SimpleNamespace(routing_model="claude-test"),+ )+ asyncio.run(ep.extract_and_store("Cut burn to 400k.", "ok", db_path=db))+ rows = audit.query(event_type="cache_event")+ assert len(rows) == 2 and {r.actor for r in rows} == {"memory_extractor"}+ assert sorted(r.details["input_tokens"] for r in rows) == [12, 30]