@markus-global/cli 0.12.3 → 0.12.5-rc.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (159) hide show
  1. package/dist/background-maintenance.d.ts +25 -0
  2. package/dist/background-maintenance.d.ts.map +1 -1
  3. package/dist/background-maintenance.js +61 -0
  4. package/dist/background-maintenance.js.map +1 -1
  5. package/dist/commands/im-inbound-origin.d.ts +38 -0
  6. package/dist/commands/im-inbound-origin.d.ts.map +1 -0
  7. package/dist/commands/im-inbound-origin.js +48 -0
  8. package/dist/commands/im-inbound-origin.js.map +1 -0
  9. package/dist/commands/start.d.ts.map +1 -1
  10. package/dist/commands/start.js +211 -46
  11. package/dist/commands/start.js.map +1 -1
  12. package/dist/markus.mjs +18523 -9866
  13. package/dist/web-ui/assets/{AsyncState-_UC7xcxJ.js → AsyncState-ByYn1wrM.js} +1 -1
  14. package/dist/web-ui/assets/ChatComponents-Bwh8SlUV.js +14 -0
  15. package/dist/web-ui/assets/{ContentRenderer-C-V5k_3K.js → ContentRenderer-GLgoGp2a.js} +6 -6
  16. package/dist/web-ui/assets/DeliverableShareModal-CRfnRuR3.js +1 -0
  17. package/dist/web-ui/assets/{Deliverables-CQnWqbTb.js → Deliverables-EFsnf6kU.js} +2 -2
  18. package/dist/web-ui/assets/MarkdownMessage-CaLBeMto.js +4 -0
  19. package/dist/web-ui/assets/{Search-CVYQIy_D.js → Search-COiaXUNr.js} +1 -1
  20. package/dist/web-ui/assets/{SendToChat-IOAvuta-.js → SendToChat-DleryIlc.js} +1 -1
  21. package/dist/web-ui/assets/Settings-CzY8z2Yh.js +8 -0
  22. package/dist/web-ui/assets/{Store-BEsLd_RN.js → Store-C4VpQCM_.js} +1 -1
  23. package/dist/web-ui/assets/Switch-ByogYoGH.js +1 -0
  24. package/dist/web-ui/assets/Team-Cutq2J24.js +93 -0
  25. package/dist/web-ui/assets/Work-BO-mz-na.js +142 -0
  26. package/dist/web-ui/assets/{arc-DP1Vf7Sx.js → arc-mYv67jdD.js} +1 -1
  27. package/dist/web-ui/assets/{architectureDiagram-3BPJPVTR-Buq8JYN8.js → architectureDiagram-3BPJPVTR-Bzpz_1r6.js} +1 -1
  28. package/dist/web-ui/assets/{blockDiagram-GPEHLZMM-BCa2p2_X.js → blockDiagram-GPEHLZMM-BBrLiivl.js} +1 -1
  29. package/dist/web-ui/assets/{c4Diagram-AAUBKEIU-BxsgggQY.js → c4Diagram-AAUBKEIU-BwDUoXA_.js} +1 -1
  30. package/dist/web-ui/assets/channel-Bx09lbhA.js +1 -0
  31. package/dist/web-ui/assets/{chunk-2J33WTMH-BJZZ60c9.js → chunk-2J33WTMH-DCSfJ-S9.js} +1 -1
  32. package/dist/web-ui/assets/{chunk-4BX2VUAB-C5hc4E6J.js → chunk-4BX2VUAB-CI0n1b2H.js} +1 -1
  33. package/dist/web-ui/assets/{chunk-55IACEB6-CbyvZ6Mk.js → chunk-55IACEB6-DqlSWLrm.js} +1 -1
  34. package/dist/web-ui/assets/{chunk-727SXJPM-JmNt9TVA.js → chunk-727SXJPM-BtmAfGYV.js} +1 -1
  35. package/dist/web-ui/assets/{chunk-AQP2D5EJ-BTk0v5ba.js → chunk-AQP2D5EJ-BgkA4lpQ.js} +1 -1
  36. package/dist/web-ui/assets/{chunk-FMBD7UC4-BO-dhB1U.js → chunk-FMBD7UC4-DCSr1JH4.js} +1 -1
  37. package/dist/web-ui/assets/{chunk-ND2GUHAM-BdjCcLdb.js → chunk-ND2GUHAM-CETYBRRm.js} +1 -1
  38. package/dist/web-ui/assets/{chunk-QZHKN3VN-BAzRC82n.js → chunk-QZHKN3VN-C58yD7lH.js} +1 -1
  39. package/dist/web-ui/assets/classDiagram-4FO5ZUOK-DGZqU3iA.js +1 -0
  40. package/dist/web-ui/assets/classDiagram-v2-Q7XG4LA2-DGZqU3iA.js +1 -0
  41. package/dist/web-ui/assets/{cose-bilkent-S5V4N54A-CVffmErT.js → cose-bilkent-S5V4N54A-BpvdK03m.js} +1 -1
  42. package/dist/web-ui/assets/{dagre-BM42HDAG-Dd9pB2fX.js → dagre-BM42HDAG-CZ1K-KD5.js} +1 -1
  43. package/dist/web-ui/assets/{diagram-2AECGRRQ-BG_AUqzM.js → diagram-2AECGRRQ-D136Ok1V.js} +1 -1
  44. package/dist/web-ui/assets/{diagram-5GNKFQAL-D5qFMRFf.js → diagram-5GNKFQAL-Ji6qPYYV.js} +1 -1
  45. package/dist/web-ui/assets/{diagram-KO2AKTUF-B3qTxSWl.js → diagram-KO2AKTUF-B-s2RC-f.js} +1 -1
  46. package/dist/web-ui/assets/{diagram-LMA3HP47-BrxC1636.js → diagram-LMA3HP47-COWaps70.js} +1 -1
  47. package/dist/web-ui/assets/{diagram-OG6HWLK6-BvXyEQ9-.js → diagram-OG6HWLK6-bh6MvwWn.js} +1 -1
  48. package/dist/web-ui/assets/{docx-preview-DBAZFeEb.js → docx-preview-BRynUqJd.js} +1 -1
  49. package/dist/web-ui/assets/{erDiagram-TEJ5UH35-CMap9oy_.js → erDiagram-TEJ5UH35-DOPh2LlL.js} +1 -1
  50. package/dist/web-ui/assets/{flowDiagram-I6XJVG4X-BZerJtvk.js → flowDiagram-I6XJVG4X-B_kxDHIp.js} +1 -1
  51. package/dist/web-ui/assets/{ganttDiagram-6RSMTGT7-CagPMHMO.js → ganttDiagram-6RSMTGT7-DYBKHOkK.js} +1 -1
  52. package/dist/web-ui/assets/{gitGraphDiagram-PVQCEYII-DZ5dDKRu.js → gitGraphDiagram-PVQCEYII-D8_0BxLC.js} +1 -1
  53. package/dist/web-ui/assets/index-9SXWNIl0.css +1 -0
  54. package/dist/web-ui/assets/index-TAHyKznL.js +867 -0
  55. package/dist/web-ui/assets/{infoDiagram-5YYISTIA-CHLdsDUZ.js → infoDiagram-5YYISTIA-DayJP7Ws.js} +1 -1
  56. package/dist/web-ui/assets/{ishikawaDiagram-YF4QCWOH-SdeinQaS.js → ishikawaDiagram-YF4QCWOH-vzxCYVSq.js} +1 -1
  57. package/dist/web-ui/assets/{journeyDiagram-JHISSGLW-BtlL2fi0.js → journeyDiagram-JHISSGLW-B-Fud82o.js} +1 -1
  58. package/dist/web-ui/assets/{kanban-definition-UN3LZRKU-BKPQYXkB.js → kanban-definition-UN3LZRKU-DzuKdB65.js} +1 -1
  59. package/dist/web-ui/assets/{mermaid.core-MkJR6w4B.js → mermaid.core-D4ql87kI.js} +5 -5
  60. package/dist/web-ui/assets/{mindmap-definition-RKZ34NQL-CbOmHX_u.js → mindmap-definition-RKZ34NQL-DosvXt2g.js} +1 -1
  61. package/dist/web-ui/assets/{pieDiagram-4H26LBE5-CypGjJP1.js → pieDiagram-4H26LBE5-DhXtMWhr.js} +1 -1
  62. package/dist/web-ui/assets/{quadrantDiagram-W4KKPZXB-CGtemfUz.js → quadrantDiagram-W4KKPZXB-D33pLLOq.js} +1 -1
  63. package/dist/web-ui/assets/{requirementDiagram-4Y6WPE33-N7YfpCvE.js → requirementDiagram-4Y6WPE33-B-iIz-XM.js} +1 -1
  64. package/dist/web-ui/assets/{sankeyDiagram-5OEKKPKP-CYnSrei-.js → sankeyDiagram-5OEKKPKP-BMd9AFEW.js} +1 -1
  65. package/dist/web-ui/assets/{sequenceDiagram-3UESZ5HK-CsB7dCPF.js → sequenceDiagram-3UESZ5HK-CI1MIdwo.js} +1 -1
  66. package/dist/web-ui/assets/{stateDiagram-AJRCARHV-DTDFUXFP.js → stateDiagram-AJRCARHV-Bq6FKPW2.js} +1 -1
  67. package/dist/web-ui/assets/stateDiagram-v2-BHNVJYJU-8nMhmWY5.js +1 -0
  68. package/dist/web-ui/assets/{timeline-definition-PNZ67QCA-C_bWAN9B.js → timeline-definition-PNZ67QCA-BDdmmSRo.js} +1 -1
  69. package/dist/web-ui/assets/{vennDiagram-CIIHVFJN-CX_qAuUT.js → vennDiagram-CIIHVFJN-DP2q5vY2.js} +1 -1
  70. package/dist/web-ui/assets/{wardley-L42UT6IY-q53jQ293.js → wardley-L42UT6IY-Bw3fjszD.js} +1 -1
  71. package/dist/web-ui/assets/{wardleyDiagram-YWT4CUSO-D34dKCJU.js → wardleyDiagram-YWT4CUSO-37bQUocJ.js} +1 -1
  72. package/dist/web-ui/assets/{xychartDiagram-2RQKCTM6-BFAngp66.js → xychartDiagram-2RQKCTM6-DnLDZyNC.js} +1 -1
  73. package/dist/web-ui/index.html +2 -2
  74. package/docs/README.md +50 -16
  75. package/docs/api/guides/websocket.md +43 -0
  76. package/docs/api/reference.md +657 -0
  77. package/docs/architecture/a2a-communication.md +303 -0
  78. package/docs/architecture/agent-runtime.md +145 -28
  79. package/docs/architecture/architecture.md +30 -11
  80. package/docs/architecture/cognitive-architecture.md +189 -15
  81. package/docs/architecture/concurrent-processing.md +190 -31
  82. package/docs/architecture/context-assembly.md +264 -0
  83. package/docs/architecture/derived-views.md +463 -0
  84. package/docs/architecture/desktop-auto-update.md +185 -0
  85. package/docs/architecture/facts.md +406 -0
  86. package/docs/architecture/learning-loop.md +11 -0
  87. package/docs/architecture/limits.md +79 -0
  88. package/docs/architecture/mailbox-system.md +786 -668
  89. package/docs/architecture/memory-system.md +1 -1
  90. package/docs/architecture/messaging-gateway.md +7 -0
  91. package/docs/architecture/notification-delivery.md +221 -0
  92. package/docs/architecture/notification-sounds.md +177 -0
  93. package/docs/architecture/prompt-engineering.md +171 -700
  94. package/docs/{design/remote-access-contract.md → architecture/remote-access.md} +4 -0
  95. package/docs/architecture/state-machines.md +288 -5
  96. package/docs/architecture/tool-loop-harness.md +362 -0
  97. package/docs/architecture/tool-system.md +40 -16
  98. package/docs/design/background-callback-in-turn-delivery-2026-11-22.md +95 -0
  99. package/docs/design/c10-0b-pick-identity-leak-2026-11-22.md +233 -0
  100. package/docs/design/c10-a-identity-columns-2026-11-22.md +182 -0
  101. package/docs/design/c10-c-trust-boundary-2026-11-23.md +222 -0
  102. package/docs/design/c10-external-adapters-plan-2026-11-19.md +448 -0
  103. package/docs/design/c8-residual-mechanisms-2026-11-15.md +984 -0
  104. package/docs/design/external-signal-decision-loop-2026-11-25.md +579 -0
  105. package/docs/guides/development.md +1 -1
  106. package/docs/{architecture → guides}/mailbox-audit-checklist.md +7 -0
  107. package/docs/guides/remote-access.md +1 -1
  108. package/docs/{design/agent-stop-intent-audit.md → records/agent-stop-intent-audit-2026-11-08.md} +4 -0
  109. package/docs/records/c10-l5-gate-verification-2026-11-26.md +72 -0
  110. package/docs/records/c10-l5-live-verification-2026-11-25.md +117 -0
  111. package/docs/records/cut6-c4-verification-2026-11-12.md +268 -0
  112. package/docs/records/cut6-mailbox-address-envelope-design-2026-11-12.md +871 -0
  113. package/docs/records/cut7-c7a-red-green-evidence-2026-11-13.md +105 -0
  114. package/docs/records/cut7-c7a-workunit-implementation-2026-11-13.md +116 -0
  115. package/docs/records/cut7-c7b-verification-2026-11-14.md +90 -0
  116. package/docs/records/cut7-live-verification-2026-11-14.md +33 -0
  117. package/docs/records/cut7-wiring-negative-cache-2026-11-14.md +64 -0
  118. package/docs/records/cut7-workunit-execution-plan-2026-11-13.md +206 -0
  119. package/docs/records/cut8-a-entity-keys-design-2026-11-14.md +377 -0
  120. package/docs/records/cut8-b-turn-delta-injection-2026-11-14.md +362 -0
  121. package/docs/records/cut8-c-single-clock-2026-11-14.md +119 -0
  122. package/docs/records/cut8-c-single-clock-design-2026-11-14.md +150 -0
  123. package/docs/records/cut8-d-2026-11-14.md +231 -0
  124. package/docs/records/cut8-live-verification-2026-11-14.md +1018 -0
  125. package/docs/records/cut9-adjacent-identity-paths-2026-11-15.md +200 -0
  126. package/docs/records/cut9-identity-declaration-2026-11-15.md +163 -0
  127. package/docs/records/cut9-inbox-dual-run-diff-2026-11-15.md +117 -0
  128. package/docs/records/cut9-live-verification-2026-11-15.md +222 -0
  129. package/docs/records/cut9-runtime-probe-2026-11-19.md +520 -0
  130. package/docs/{design/deliverable-sharing.md → records/deliverable-sharing-2026-08-12.md} +10 -3
  131. package/docs/records/identity-backlog-disposition-2026-11-22.md +143 -0
  132. package/docs/records/marketplace-orphan-freeze-2026-11-22.md +239 -0
  133. package/docs/{architecture/notify-user-context-truth.md → records/notify-user-context-truth-2026-10-28.md} +6 -0
  134. package/docs/{handoff → records}/remote-access-handoff.md +1 -1
  135. package/package.json +1 -1
  136. package/templates/roles/HANDBOOK.md +1 -1
  137. package/templates/skills/agent-building/SKILL.md +1 -1
  138. package/dist/web-ui/assets/ChatComponents-Ca9tWI9W.js +0 -12
  139. package/dist/web-ui/assets/DeliverableShareModal-BvpyowYJ.js +0 -1
  140. package/dist/web-ui/assets/MarkdownMessage-DedA8Zob.js +0 -4
  141. package/dist/web-ui/assets/Settings-BQDqf4q5.js +0 -8
  142. package/dist/web-ui/assets/Switch-C6InTNUi.js +0 -1
  143. package/dist/web-ui/assets/Team-nfNyWj-s.js +0 -85
  144. package/dist/web-ui/assets/Work-BQvgkynj.js +0 -142
  145. package/dist/web-ui/assets/channel-BTSrOo6c.js +0 -1
  146. package/dist/web-ui/assets/classDiagram-4FO5ZUOK-BSYlcnF9.js +0 -1
  147. package/dist/web-ui/assets/classDiagram-v2-Q7XG4LA2-BSYlcnF9.js +0 -1
  148. package/dist/web-ui/assets/index-CKPQYzzR.js +0 -867
  149. package/dist/web-ui/assets/index-CzHSs0Vd.css +0 -1
  150. package/dist/web-ui/assets/stateDiagram-v2-BHNVJYJU-AVnDjlzF.js +0 -1
  151. package/docs/api/api.md +0 -353
  152. package/docs/architecture/llm-provider-timeouts.md +0 -60
  153. package/docs/architecture/state-ownership.md +0 -83
  154. /package/docs/architecture/{frontend/team-chat.md → team-chat.md} +0 -0
  155. /package/docs/{analysis → records}/2026-10-29-a2a-cache-hit-rate.md +0 -0
  156. /package/docs/{fixes → records}/2026-11-12-session-expiry-and-mention-thinking.md +0 -0
  157. /package/docs/{handoff → records}/approval-tri-state-handoff.md +0 -0
  158. /package/docs/{design → records}/overview-attention-redesign.md +0 -0
  159. /package/docs/{remote-access/fast-path-plan.md → records/remote-access-fast-path-plan-2026-10-17.md} +0 -0
@@ -0,0 +1,377 @@
1
+ # Cut 8 · C8-A — Entity identity converges on the WorkUnit; destinations split on three axes
2
+
3
+ > Branch `task/c8-a-entity-keys` → PR into `feat/refactor-plan`.
4
+ > Base: `0425bd48` (merge of PR #444, cut 7).
5
+ > Design + test plan were written before the first red run; §7 records what the
6
+ > final implementation actually decided where the plan was wrong.
7
+
8
+ ---
9
+
10
+ ## 1. Symptom
11
+
12
+ Send three A2A messages about the **same** piece of work. The recipient's number
13
+ of memory sessions grows by three instead of staying flat, and the three
14
+ `mailbox_items` rows carry three different `work_unit_ref` values.
15
+
16
+ Live reading (read-only, `~/.markus/data.db`, 2026-11-14 17:0x):
17
+
18
+ | reading | value |
19
+ |---|---|
20
+ | `mailbox_items` where `source_type='a2a_message'` | 34 739 |
21
+ | … of those with `work_unit_ref IS NOT NULL` | 11 |
22
+ | … **without** a `[conversation:x]` tag in `payload.content` | **29 993 (86 %)** |
23
+ | messages on the busiest single `(agent, channel)` | 612 |
24
+
25
+ The 86 % is the mechanism: a message without a conversation tag fell through to
26
+ `a2a_${agentId}_${timestamp}` — a timestamp in an identity slot, i.e. one new
27
+ session per message by construction. The other 14 % were pinned to a stable
28
+ session id but still minted one WorkUnit per delivery.
29
+
30
+ ## 2. Root cause (R1 — one fact, two writers)
31
+
32
+ "Which thing is this message about" had two writers, and the delivery channel won:
33
+
34
+ | writer | where | what it claimed |
35
+ |---|---|---|
36
+ | transport route | `resolveEntityKeys()` → `channel:<channelKey>` | the item belongs to a route |
37
+ | transport route (again) | `sessionHint {role:'channel', key: channelKey}` → `channelMemorySessionId()` | the turn's memory session |
38
+ | transport route (again, per hop) | `WORK_UNIT_REF.a2a(channelKey, deliveryId)` | **one WU per delivery** |
39
+ | the thing itself | `mailbox_items.work_unit_ref` | the item belongs to a WorkUnit |
40
+
41
+ Because the ref encoded `deliveryId`, three messages about one task necessarily
42
+ became three WUs; because the session id was derived from the transport (or a
43
+ timestamp), they became three sessions.
44
+
45
+ **Root-cause test — could this recur under a different name?** Yes: any new
46
+ transport (email, webhook, a new IM gateway) could mint its own `<transport>:`
47
+ key and split sessions again. So the fix cannot be an `a2a` special case; the fix
48
+ is that **the entity is derivable from the WorkUnit ref and nowhere else**.
49
+
50
+ ## 3. Design (as implemented)
51
+
52
+ ### 3.1 One pure derivation, three consumers
53
+
54
+ `packages/shared/src/work-unit.ts`:
55
+
56
+ ```
57
+ entityKeysOfWorkUnitRef(ref): string[]
58
+ task_<id>_r<n> → ['task:<id>']
59
+ a2a:task:<id> → ['task:<id>']
60
+ a2a:channel:<key> → ['wu:<ref>']
61
+ session:<cs>:r<n> → ['conv:<cs>']
62
+ delegation:… → ['wu:<ref>']
63
+ anything else → ['wu:<ref>'] (never empty)
64
+
65
+ sessionScopeKeyOfWorkUnitRef(ref): string | undefined
66
+ a2a:<rest> → 'a2a_' + sanitise(rest)
67
+ everything else → undefined (this layer declares no session)
68
+
69
+ taskIdOfWorkUnitRef(ref): string | undefined
70
+ task_<id>_r<n> / a2a:task:<id> → <id>
71
+ ```
72
+
73
+ `undefined` means "this ref carries no session identity, use the legacy path" —
74
+ **not** "there is no session". Same discipline as `sessionLockKeyOfContextRef()`.
75
+ It deliberately yields nothing for `task_<id>_r<n>`: the consumer wraps the value
76
+ in `channelMemorySessionId()`, so returning the task ref there would mint
77
+ `channel_task_x_r1_<agent>` beside the task's real execution session — the same
78
+ R1 in a new place (§7.5).
79
+
80
+ ### 3.2 `WORK_UNIT_REF.a2a` names the thing, not the delivery
81
+
82
+ ```diff
83
+ - a2a(channelKey, deliveryId) { return `a2a:${channelKey}:${deliveryId}`; }
84
+ + a2a(entityKey) { return `a2a:${entityKey}`; }
85
+ ```
86
+
87
+ The caller supplies the entity: `task:<taskId>` when the sender's current turn is
88
+ a WorkUnit that belongs to a task, else `channel:<channelKey>` (a conversation is
89
+ a legitimate entity; a delivery is not). The parent turn ref is read from
90
+ in-memory turn state (`turnWorkUnitRef`), never queried from the DB.
91
+
92
+ ### 3.3 The registry decides which dimensions lock — not the call site
93
+
94
+ The plan (§7.1) originally proposed filtering `conversation`/`channel` inside
95
+ `resolveEntityKeys()` when a WorkUnit was present. That would have been a second
96
+ derivation source, and `mailbox-route.test.ts` ("keys derive only from
97
+ `route().affinity`") exists precisely to catch that. The implemented shape is a
98
+ registry change instead:
99
+
100
+ - `MailboxEntityScope`: **`'channel'` deleted**, **`'workunit'` added**.
101
+ - `a2a_message.affinity`: `['task','requirement','channel','system']` →
102
+ `['task','requirement','workunit','system']`.
103
+ - `resolveEntityKeys()` gains one `case 'workunit'` that calls
104
+ `entityKeysOfWorkUnitRef(item.workUnitRef)` — a declared dimension, resolved in
105
+ the one place dimensions are resolved.
106
+
107
+ `channel:` is gone rather than kept as a non-locking dimension: locks live only in
108
+ memory (`entityLocks`), so there is no historical key to migrate, and leaving a
109
+ dead namespace is the suspended-reference antipattern. A legacy A2A row with no
110
+ WorkUnit now falls back to `system:<agentId>` — the documented "unknown =
111
+ conservatively serial" rule (slower, never wrong). New A2A rows always carry a
112
+ WorkUnit, so this is a drain-the-queue path only.
113
+
114
+ **`conv:` needed no change**: for chat, `WORK_UNIT_REF.chat` is
115
+ `session:<cs>:r<n>`, whose entity key is `conv:<cs>` — exactly what the
116
+ `conversation` dimension already derived from `metadata.dbSessionId`. The
117
+ convergence is true by construction, and §3.1's table is the proof.
118
+
119
+ ### 3.4 `dedupeKey` derives from the entity
120
+
121
+ `roundDedupKeyOf()` now reads the task id from the WorkUnit ref first
122
+ (`taskIdOfWorkUnitRef`), falling back to `payload.taskId` / `metadata.taskId` for
123
+ legacy rows. Shape `(sourceType, taskId, round)` is unchanged: two review requests
124
+ for the same task in two different rounds are two facts, not one.
125
+
126
+ ### 3.5 One producer-side change in core
127
+
128
+ `agent.ts` was deriving the A2A channel session a **second** time from
129
+ `extra.channelKey`, overriding the hint `resolveTurnSession()` had just applied.
130
+ It now reads the session key from the hint (single source); `extra.channelKey`
131
+ still falls back for rows without a hint.
132
+
133
+ ### 3.6 Three destinations, one decision point
134
+
135
+ ```
136
+ resolveMailboxDestination(entityKeys, activeTurnEntityKeys): 'inject' | 'queue' | 'overview'
137
+ no concrete entity (only `system:` fallback) → 'overview'
138
+ intersects the active turn's entities → 'inject' (never queue behind itself)
139
+ otherwise / turn unknown → 'queue'
140
+ ```
141
+
142
+ The one pre-existing in-turn injection point (`task_comment` while its task is
143
+ executing) now goes through this function instead of its own
144
+ `activeTasks.has(taskId)` check — one predicate answered one question. `'overview'`
145
+ is the existing `catch_all` surface where entity-less signals already land.
146
+
147
+ Delta-injection *content* (what injected text looks like, cache behaviour when
148
+ empty) is **C8-B** and deliberately not in this card.
149
+
150
+ ### 3.7 All fifteen types: affinity → destination
151
+
152
+ The card's objective is "re-sort the **15** mailbox item types onto three axes",
153
+ so listing one consumer would read as "the other fourteen were missed". They were
154
+ not missed — fourteen of them are already carried by an existing path, and this
155
+ section says which one, per type. Column *who decides* has exactly three
156
+ possible answers and no fourth:
157
+
158
+ - **`resolveMailboxDestination`** — the entity-intersection predicate
159
+ (§3.6). Today it has **one** consumer, `task_comment` (`agent.ts:2798`); the
160
+ other types have no in-turn injection to decide.
161
+ - **default queue** — the item is dequeued and its own `case` in the dispatch
162
+ switch (`agent.ts:2481`–`2990`) handles it. Whether that costs an LLM turn is
163
+ a *type-level* fact (`invokesLLM`), not a destination decision.
164
+ - **overview** — the `catch_all` surface (`bounceSignal(..., 'catch_all', ...)`
165
+ at `mailbox.ts:626`, read-only `catchAll()` at `:654`). It is an **exit path**
166
+ for items nobody handled, not a per-type destination; a type never "goes to
167
+ overview" by design, it lands there by failing to be handled.
168
+
169
+ | # | type | affinity (lock dimensions) | `invokesLLM` | destination — who decides |
170
+ |---|---|---|---|---|
171
+ | 1 | `human_chat` | task, requirement, conversation, system | yes | default queue → own case (`agent.ts:2481`) |
172
+ | 2 | `a2a_message` | task, requirement, **workunit**, system | yes | default queue → shares the `human_chat` case |
173
+ | 3 | `mention` | task, requirement, user, conversation, system | yes | default queue → own case (`:2718`) |
174
+ | 4 | `session_reply` | task, requirement, conversation, system | yes | default queue → own case (`:2951`) |
175
+ | 5 | `task_comment` | task, requirement, system | no | **`resolveMailboxDestination`** → `inject` when the entity is the running task, else `queue` (one `comment_response` turn) |
176
+ | 6 | `review_request` | task, requirement, system | yes | default queue; `strictState` ⇒ may not be merged/deferred/dropped (`:2823`) |
177
+ | 7 | `requirement_comment` | task, requirement, system | no | default queue → records, no turn (`:2754`) |
178
+ | 8 | `requirement_update` | requirement, system | no | default queue; only `intent='action_required'` is authoritative (`:2731`) |
179
+ | 9 | `workflow_update` | task, requirement, system | yes | default queue; informational branch closes without a turn (`:2770`) |
180
+ | 10 | `task_status_update` | task, system | no | default queue; `intent='trigger_execution'` is the execution instruction (`:2614`) |
181
+ | 11 | `callback_result` | conversation, system | yes | default queue → own case (`:2972`); never age-purged (`isNeverPurgeable`) |
182
+ | 12 | `heartbeat` | system | yes | default queue → own case (`:2901`) |
183
+ | 13 | `daily_report` | system | yes | default queue → grouped with `system_event` (`:2918`) |
184
+ | 14 | `memory_consolidation` | system | yes | default queue → own case (`:2938`); **not** age-droppable any more (§3.8) |
185
+ | 15 | `system_event` | system | yes | default queue → own case (`:2918`) |
186
+
187
+ The three axes therefore hold for all fifteen: every type resolves entity keys
188
+ through one function (`resolveEntityKeys`, driven by the registry), every type's
189
+ destination is answered either by the one predicate or by the default queue, and
190
+ nothing reaches "overview" except by the existing catch-all exit. The gap this
191
+ card actually had was *not* in the other fourteen — it was that `a2a_message`
192
+ was the one type whose identity came from the transport (§2).
193
+
194
+ ### 3.8 The disposal table: dream is not age-droppable
195
+
196
+ The card owns the whole `type → disposal` dispatch table, and one entry was
197
+ wrong: `memory_consolidation` (dream) was listed as "stale information, drop by
198
+ age". A dream run is background work on the sleep path, not attention noise; age
199
+ expiry silently discards the run and leaves no trace.
200
+
201
+ The table also had **two writers**: a hand-maintained literal array in
202
+ `limits.ts` beside the registry's per-type `triageStaleDrop` flag. Only the flag
203
+ was ever read (`routeMailboxItem(item).triageStaleDrop` at `mailbox.ts:682` and
204
+ `:2145`); the literal had no readers at all and still looked like the source of
205
+ truth. That is the same R1 as the rest of the card, one layer down, so it is
206
+ fixed the same way — **delete the second writer**: `TRIAGE_STALE_DROP_TYPES` is
207
+ now a projection of `MAILBOX_TYPE_REGISTRY`, and `memory_consolidation`'s flag is
208
+ `false`.
209
+
210
+ Scope note: this is the *disposal* fact only. `memory_consolidation`'s **affinity**
211
+ (`['system']`, the global single key) is C8-C's battlefield and is untouched here.
212
+
213
+ ## 4. Test plan — each test observed red before implementation
214
+
215
+ `packages/shared/test/work-unit-ref.test.ts` (new, 17 assertions)
216
+ 1. `a2a` ref carries no per-delivery discriminator (same entity twice → same ref).
217
+ 2. `entityKeysOfWorkUnitRef` table, all six shapes.
218
+ 3. `a2a:channel:*` yields **no** `channel:` key (would re-introduce the transport writer).
219
+ 4. Never empty (an empty key set silently disables mutual exclusion).
220
+ 5. `sessionScopeKeyOfWorkUnitRef` table; `session:*` → `undefined`; `task_*` →
221
+ `undefined` (see §7.5); safe charset for the values it does return.
222
+
223
+ `packages/shared/test/mailbox-route.test.ts` (extended, +10)
224
+ 6. Same task + different `channelKey` + same WU → **identical** entity keys.
225
+ 7. A WU-carrying item emits no `channel:` / `conv:` key.
226
+ 8. A WU-less item falls back to `system:` (not `channel:`).
227
+ 9. `dedupeKey` takes the task id from the WU ref when the payload is silent.
228
+ 10. `resolveMailboxDestination`: inject / queue / overview.
229
+
230
+ `packages/org-manager/test/agent-delivery.test.ts` (extended, +6)
231
+ 11. Three deliveries in one task turn → one ref (`a2a:task:tsk_1`).
232
+ 12. … → one session key (`a2a_task_tsk_1`).
233
+ 13. Identity lands on the in-memory item at enqueue, not only the DB column.
234
+ 14. No turn WU → `a2a:channel:<channelKey>`, still not per-delivery.
235
+ 15. Different tasks → different refs.
236
+ 16. `linkMailboxItem` remains the only writer of the DB column.
237
+
238
+ `packages/core/test/mailbox-concurrent.test.ts` (rewritten)
239
+ 17. Same entity serial even across different channels; different entities parallel.
240
+
241
+ `packages/core/test/mailbox-entity-consolidation.test.ts` (new)
242
+ 18. One thing over two channels → **one** queued item (guards pre-triage merge).
243
+ 19. Two different things in one channel → **two** items (guards enqueue merge).
244
+ 20. Legacy rows (no WorkUnit) still merge by `channelKey` — behaviour preserved.
245
+
246
+ 18 and 19 each bypass the *other* merge layer on purpose (18 pulls the first item
247
+ out of the queue with `dequeue()` / `putBack()`, 19 relies on the enqueue path),
248
+ so neither can be satisfied by a layer other than the one it names.
249
+
250
+ `packages/core/test/mailbox-batch-and-purge.test.ts` (extended, +2 and one
251
+ updated expectation)
252
+ 21. An expired `memory_consolidation` item **survives** `purgeStaleItems()` —
253
+ behavioural, not a source-text assertion.
254
+ 22. `TRIAGE_STALE_DROP_TYPES` equals the registry projection **and** does not
255
+ contain `memory_consolidation`, with `heartbeat` still present as the control
256
+ (otherwise "dream is absent" could just mean the list went empty).
257
+ 23. The mixed-purge test now expects dream **and** the `action_required` item to
258
+ survive while only the plain informational item is reclaimed (its old
259
+ expectation counted dream as droppable).
260
+
261
+ ## 5. Verification
262
+
263
+ | gate | result |
264
+ |---|---|
265
+ | `tsc -b --force` | clean |
266
+ | `npm run lint` | 0 errors (1001 pre-existing warnings) |
267
+ | `node scripts/architecture-guard.mjs` | 34 hits, all whitelisted; contracts 18/18 |
268
+ | `node scripts/security-guard.mjs` | ok |
269
+ | `npx vitest run` (whole repo, serial) | 8536 passed / 1 failed / 13 skipped (8550) |
270
+ | `npm run test:web-ui` | 1476 passed / 3 skipped, 0 failed |
271
+ | the 1 failure | `cli: auto-runs quickInit when config is missing` — **also fails at base `0425bd48`** (60 s timeout; reproduced in a clean baseline worktree, see §6) |
272
+
273
+ Live reading of the rows the **old** code produced (read-only `~/.markus/data.db`):
274
+
275
+ ```
276
+ last 2h, source_type='a2a_message':
277
+ a2a_rows = 17 distinct_refs = 17 ref_with_delivery_id = 17 null_ref = 0
278
+ ```
279
+
280
+ That is the "before" baseline in one line: one WorkUnit per delivery, and every
281
+ ref embeds the delivery id. Feeding the same entity through the **shipped**
282
+ derivation (`packages/shared/dist/work-unit.js` — no re-implementation of the
283
+ rule in the probe):
284
+
285
+ ```
286
+ three messages on one task → ref: 1, entity: 1, session scope key: 1, ref contains dlv_: false
287
+ three messages on one channel → ref: 1, entity: 1, session scope key: 1
288
+ ```
289
+
290
+ ## 6. Baseline red-proof
291
+
292
+ A detached worktree at base `0425bd48` was installed offline (`pnpm install
293
+ --frozen-lockfile`) and built (`tsc -b`). Two things were proven there.
294
+
295
+ **The CLI failure is pre-existing.** The identical test fails identically in the
296
+ baseline worktree — 1 failed / 15 passed there, 1 failed / 15 passed here, same
297
+ 60 s timeout. This card touches neither the CLI nor `quickInit`.
298
+
299
+ **The new merge assertions are not hollow.** Copied into the baseline worktree
300
+ and run there:
301
+
302
+ | assertion | base | head |
303
+ |---|---|---|
304
+ | one thing over two channels → one item | ✗ `expected length 1, got 2` | ✓ |
305
+ | two things in one channel → two items | ✗ `expected length 2, got 1` | ✓ |
306
+ | legacy rows (no WorkUnit) still merge by channel | ✓ | ✓ |
307
+
308
+ The third row is the control: it proves the fallback path was preserved rather
309
+ than replaced, so the two red rows above are attributable to the entity rule.
310
+
311
+ The dream/disposal assertions were proven the same way — same file copied into
312
+ the built baseline worktree, where the registry still says "drop dream by age":
313
+
314
+ | assertion | base | head |
315
+ |---|---|---|
316
+ | an expired `memory_consolidation` survives `purgeStaleItems()` | ✗ `expected 1 to be +0` (it was purged) | ✓ |
317
+ | the stale-drop list does not contain `memory_consolidation` | ✗ `expected [ 'task_status_update', …(4) ] to not include 'memory_consolidation'` | ✓ |
318
+ | mixed purge: dream **and** the `action_required` item survive | ✗ `expected 2 to be 1` | ✓ |
319
+
320
+ The control stays green on both sides: `heartbeat` (real informational noise) is
321
+ still reclaimed by age, so "dream survives" cannot be a side effect of the purge
322
+ having stopped working altogether.
323
+
324
+ One trap worth recording: a fresh baseline worktree fails **every** test in a file
325
+ within single-digit ms until the workspace dependencies are built
326
+ (`Failed to resolve entry for package "@markus/shared"`). That is an install
327
+ artefact and must not be read as "red at base" — the tell is `AssertionError`
328
+ versus module resolution, and per-test duration of ms versus seconds.
329
+
330
+ ## 7. Where the implementation corrected the plan
331
+
332
+ 1. **§3.3 rewritten**: the plan filtered scopes inside `resolveEntityKeys`; that
333
+ is a second derivation source, so it became a registry dimension change
334
+ instead. `channel` was deleted outright rather than demoted.
335
+ 2. **`conv:` convergence needed no code**: it was already true by construction
336
+ (§3.3) — asserting a "fix" there would have been theatre.
337
+ 3. **A third writer was found and removed** (§3.5): core re-derived the A2A
338
+ session from `extra.channelKey` after `resolveTurnSession` had already applied
339
+ the hint. That was a small-scale copy of the same R1.
340
+ 4. **The merge key had to move too** (review requirement, §5.1 below): merging
341
+ a2a items by `channelKey` reproduces the same defect in a new shape — one
342
+ thing over two channels is *not* merged (read twice, in two contexts), and two
343
+ things inside one channel *are* merged (the second one's reason to execute
344
+ disappears). Both merge layers now key on the entity; the dead
345
+ `_groupType: string` parameter went with it, which is why the `'channel'`
346
+ literal at the call site is gone rather than renamed.
347
+ 5. **`turnSessionIdOfWorkUnitRef` → `sessionScopeKeyOfWorkUnitRef`** (§3.1):
348
+ the old name promised a session id but the value gets wrapped by
349
+ `channelMemorySessionId()`, and the `task_*` branch returned the ref itself —
350
+ which would have produced `channel_task_x_r1_<agent>` beside the task's real
351
+ execution session. Same R1, re-introduced by a name. No production caller
352
+ passed a task ref, so behaviour is unchanged; the trap is what is gone. A name
353
+ is a constraint for the next reader, so it now says what the function answers.
354
+ 6. **dream was taken out of the age-based purge** (review requirement, §3.8):
355
+ `memory_consolidation.triageStaleDrop` → `false`, and
356
+ `TRIAGE_STALE_DROP_TYPES` stopped being a hand-maintained literal. Worth
357
+ recording *why* this was a defect rather than a preference: the literal had no
358
+ runtime readers at all (the only reader is
359
+ `routeMailboxItem(item).triageStaleDrop`), so it pretended to be the source of
360
+ truth while the registry actually decided — and the entry it carried said
361
+ "drop a dream run after four hours". Deleting the second writer is what makes
362
+ the correction stick; editing both copies would have left the drift trap in
363
+ place.
364
+
365
+ ## 8. Boundaries
366
+
367
+ - No `cs_*` / `sess_*` session contract change; no LLM provider change.
368
+ - No back-fill or migration of historical rows; locks are in-memory only.
369
+ - `mailbox_items.work_unit_ref` keeps exactly one writer (`linkMailboxItem`).
370
+ - `work_units` schema untouched.
371
+ - `heartbeat` / `daily_report` / `memory_consolidation` **affinity**
372
+ (`['system']`, the global single key) untouched — that is C8-C's battlefield.
373
+ - **dream's disposal is this card, and it is done** (§3.8): `memory_consolidation`
374
+ is out of the age-based purge and the stale-drop list is now derived, not
375
+ hand-written. The `type → disposal` dispatch table belongs to C8-A; the
376
+ affinity table does not.
377
+ - The batch-assembly entry point is untouched — that is C8-D.