@cohortapp/agent-sdk 2.3.2 → 2.4.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 (148) hide show
  1. package/framework-features.json +30 -0
  2. package/lib/backlog.mjs +136 -0
  3. package/lib/cadences.mjs +63 -2
  4. package/lib/cadences.test.mjs +105 -0
  5. package/lib/capability/inventory.mjs +542 -0
  6. package/lib/capability/inventory.test.mjs +232 -0
  7. package/lib/capability/probe.mjs +255 -0
  8. package/lib/channels/contract.mjs +37 -1
  9. package/lib/channels/contract.test.mjs +25 -1
  10. package/lib/claude-bin.mjs +37 -3
  11. package/lib/claude-bin.test.mjs +42 -8
  12. package/lib/execution/disposition.mjs +501 -0
  13. package/lib/execution/disposition.test.mjs +482 -0
  14. package/lib/execution/drive.mjs +352 -0
  15. package/lib/execution/drive.test.mjs +270 -0
  16. package/lib/execution/effects.mjs +340 -0
  17. package/lib/execution/effects.test.mjs +193 -0
  18. package/lib/execution/index.mjs +152 -0
  19. package/lib/execution/intake.mjs +581 -0
  20. package/lib/execution/intake.test.mjs +343 -0
  21. package/lib/execution/journal.mjs +374 -0
  22. package/lib/execution/journal.test.mjs +261 -0
  23. package/lib/execution/match.mjs +331 -0
  24. package/lib/execution/match.test.mjs +235 -0
  25. package/lib/execution/pipeline.mjs +341 -0
  26. package/lib/execution/pipeline.test.mjs +389 -0
  27. package/lib/execution/route.mjs +332 -0
  28. package/lib/execution/route.test.mjs +186 -0
  29. package/lib/execution/surface-policy.mjs +446 -0
  30. package/lib/execution/surface-policy.test.mjs +162 -0
  31. package/lib/goals/admission.mjs +209 -0
  32. package/lib/goals/admission.test.mjs +139 -0
  33. package/lib/goals/classify.mjs +206 -0
  34. package/lib/goals/classify.test.mjs +109 -0
  35. package/lib/goals/collaborate.mjs +415 -0
  36. package/lib/goals/collaborate.test.mjs +324 -0
  37. package/lib/goals/gaps.mjs +111 -0
  38. package/lib/goals/gaps.test.mjs +284 -0
  39. package/lib/goals/loop.mjs +537 -0
  40. package/lib/goals/loop.test.mjs +719 -0
  41. package/lib/identity/persona.mjs +247 -0
  42. package/lib/identity/persona.test.mjs +117 -0
  43. package/lib/kpi.mjs +469 -0
  44. package/lib/kpi.test.mjs +244 -0
  45. package/lib/mandate/audit.mjs +168 -0
  46. package/lib/mandate/audit.test.mjs +195 -0
  47. package/lib/mandate/cache.mjs +162 -0
  48. package/lib/mandate/derive.mjs +317 -0
  49. package/lib/mandate/derive.test.mjs +224 -0
  50. package/lib/mandate/model.mjs +352 -0
  51. package/lib/mandate/model.test.mjs +145 -0
  52. package/lib/mandate/refresh.mjs +187 -0
  53. package/lib/mandate/refresh.test.mjs +293 -0
  54. package/lib/mcp/server.test.mjs +4 -4
  55. package/lib/org/approvals.mjs +14 -2
  56. package/lib/org/client.mjs +58 -22
  57. package/lib/org/client.test.mjs +3 -1
  58. package/lib/org/inbound/directedness.mjs +720 -0
  59. package/lib/org/inbound/directedness.test.mjs +543 -0
  60. package/lib/org/inbound/facts.mjs +501 -0
  61. package/lib/org/inbound/facts.test.mjs +375 -0
  62. package/lib/org/inbound/hydrate.mjs +535 -0
  63. package/lib/org/inbound/hydrate.test.mjs +326 -0
  64. package/lib/org/inbound/index.mjs +233 -0
  65. package/lib/org/inbound/index.test.mjs +324 -0
  66. package/lib/org/inbound/io.mjs +141 -0
  67. package/lib/org/inbound/project.mjs +201 -0
  68. package/lib/org/inbound/project.test.mjs +287 -0
  69. package/lib/org/inbound/surfaces.mjs +257 -0
  70. package/lib/org/knowledge.mjs +10 -1
  71. package/lib/org/knowledge.test.mjs +8 -1
  72. package/lib/org/leases.mjs +5 -0
  73. package/lib/org/mesh.mjs +17 -2
  74. package/lib/org/messaging.mjs +40 -4
  75. package/lib/org/messaging.test.mjs +40 -0
  76. package/lib/org/param-contract.mjs +694 -0
  77. package/lib/org/param-contract.test.mjs +451 -0
  78. package/lib/org/protocol.checksum +1 -1
  79. package/lib/org/protocol.mjs +8 -0
  80. package/lib/org/protocol.test.mjs +5 -1
  81. package/lib/org/push.mjs +1025 -0
  82. package/lib/org/push.test.mjs +690 -0
  83. package/lib/org/tool-surface.mjs +138 -38
  84. package/lib/org/tool-surface.test.mjs +13 -8
  85. package/lib/org/typing.mjs +341 -0
  86. package/lib/org/typing.test.mjs +291 -0
  87. package/lib/plan/compile.mjs +510 -0
  88. package/lib/plan/compile.test.mjs +286 -0
  89. package/lib/plan/emit.mjs +256 -0
  90. package/lib/plan/emit.test.mjs +246 -0
  91. package/lib/plan/explain.mjs +226 -0
  92. package/lib/plan/explain.test.mjs +188 -0
  93. package/lib/plan/schema.mjs +140 -0
  94. package/lib/resource-governor.mjs +47 -1
  95. package/lib/resource-governor.test.mjs +21 -1
  96. package/lib/setup/enroll-from-cohort.mjs +84 -16
  97. package/lib/setup/enroll-from-cohort.test.mjs +43 -1
  98. package/lib/setup/sections/identity.mjs +15 -4
  99. package/lib/setup/sections/identity.test.mjs +94 -0
  100. package/lib/setup/sections/inventory.mjs +178 -0
  101. package/lib/setup/sections/inventory.test.mjs +198 -0
  102. package/lib/setup/sections/mandate.mjs +392 -0
  103. package/lib/setup/sections/mandate.test.mjs +373 -0
  104. package/lib/setup/sections/subagents.mjs +427 -0
  105. package/lib/setup/sections/subagents.test.mjs +429 -0
  106. package/lib/setup/sections/verify.mjs +121 -0
  107. package/lib/setup/sections/verify.test.mjs +175 -0
  108. package/lib/setup/sot.mjs +2 -0
  109. package/lib/subagents/cli.mjs +463 -0
  110. package/lib/subagents/cli.test.mjs +389 -0
  111. package/lib/subagents/client.mjs +373 -0
  112. package/lib/subagents/client.test.mjs +309 -0
  113. package/lib/subagents/gap.mjs +268 -0
  114. package/lib/subagents/gap.test.mjs +234 -0
  115. package/lib/subagents/lock.mjs +296 -0
  116. package/lib/subagents/lock.test.mjs +248 -0
  117. package/lib/subagents/manifest.mjs +224 -0
  118. package/lib/subagents/manifest.test.mjs +175 -0
  119. package/lib/subagents/refs.mjs +274 -0
  120. package/lib/subagents/refs.test.mjs +204 -0
  121. package/lib/subagents/resolve.mjs +455 -0
  122. package/lib/subagents/resolve.test.mjs +422 -0
  123. package/lib/subagents/schema.mjs +467 -0
  124. package/lib/subagents/schema.test.mjs +306 -0
  125. package/package.json +8 -3
  126. package/plugins/maestro-skills/.claude-plugin/marketplace.json +16 -0
  127. package/policies/ai-disclosure.yaml +42 -2
  128. package/scaffold/CLAUDE.md +16 -2
  129. package/schedules/triggers/goal-steward.md +79 -0
  130. package/scripts/ci/conformance-org-api.mjs +792 -0
  131. package/scripts/ci/conformance-org-api.test.mjs +417 -0
  132. package/scripts/daemon/agent-daemon.mjs +36 -4
  133. package/scripts/daemon/cadence-handlers.mjs +145 -1
  134. package/scripts/daemon/goal-steward-cadence.test.mjs +243 -0
  135. package/scripts/daemon/inbox-deferral.mjs +45 -2
  136. package/scripts/daemon/inbox-deferral.test.mjs +56 -0
  137. package/scripts/daemon/inbox-wake.mjs +282 -0
  138. package/scripts/daemon/inbox-wake.test.mjs +199 -0
  139. package/scripts/daemon/prompt-builder.mjs +41 -1
  140. package/scripts/daemon/typing-registry.mjs +55 -2
  141. package/scripts/daemon/typing-registry.test.mjs +25 -0
  142. package/scripts/local-triggers/generate-plists.test.mjs +5 -5
  143. package/scripts/setup/gen-subagent-manifest.mjs +95 -0
  144. package/scripts/setup/gen-subagent-manifest.test.mjs +124 -0
  145. package/scripts/setup/generate-plan.mjs +108 -0
  146. package/scripts/setup/init-capability-manifest.mjs +70 -0
  147. package/scripts/setup/init-skill-marketplace.mjs +155 -0
  148. package/scripts/setup/init-skill-marketplace.test.mjs +193 -0
@@ -0,0 +1,543 @@
1
+ /**
2
+ * directedness.test.mjs — the pure half of wide org inbound.
3
+ *
4
+ * Every family gets a POSITIVE case (this is mine, and here is the typed reason)
5
+ * and a NEGATIVE case. The negatives are the point: the org event feed is
6
+ * org-wide, so a resolver that guesses "yes" makes an agent barge into other
7
+ * people's private conversations. The load-bearing one —
8
+ * "an event in someone else's private room must NEVER be directed" — is asserted
9
+ * three ways: not a member, not visible, and roster unknown.
10
+ *
11
+ * Run: node --test lib/org/inbound/directedness.test.mjs
12
+ */
13
+
14
+ "use strict";
15
+
16
+ import { test } from "node:test";
17
+ import assert from "node:assert/strict";
18
+
19
+ import {
20
+ classifyEvent,
21
+ resolveDirected,
22
+ coordsOf,
23
+ matchesMyName,
24
+ taskIdFromFileKey,
25
+ needsSpaceProbe,
26
+ } from "./directedness.mjs";
27
+
28
+ const ME = "M-me";
29
+ const THEM = "M-them";
30
+
31
+ /** A minimal Facts object; every field opt-in so a test states only what it means. */
32
+ function facts(over = {}) {
33
+ return {
34
+ me: ME,
35
+ myNames: [],
36
+ channels: new Map(),
37
+ visibleChannelIds: new Set(),
38
+ memberChannelIds: new Set(),
39
+ channelPages: new Map(),
40
+ myThreadRootIds: new Set(),
41
+ taskRole: null,
42
+ tasks: null,
43
+ decisions: null,
44
+ decisionVoices: new Map(),
45
+ escalations: null,
46
+ approvals: null,
47
+ fileAcl: null,
48
+ degraded: [],
49
+ ...over,
50
+ };
51
+ }
52
+
53
+ /** Register a channel as visible (+ a member when non-PUBLIC). */
54
+ function withChannel(f, id, kind) {
55
+ f.channels.set(id, { id, kind, name: id, slug: id });
56
+ f.visibleChannelIds.add(id);
57
+ if (kind !== "PUBLIC") f.memberChannelIds.add(id);
58
+ return f;
59
+ }
60
+
61
+ /** Put a message on the hydrated page for a channel. */
62
+ function withMessage(f, channelId, msg) {
63
+ const page = f.channelPages.get(channelId) || { byId: new Map(), messages: [] };
64
+ page.byId.set(String(msg.id), msg);
65
+ page.messages.push(msg);
66
+ f.channelPages.set(channelId, page);
67
+ return f;
68
+ }
69
+
70
+ function ev(over) {
71
+ return { seq: 1, at: "2026-08-11T00:00:00.000Z", actor: THEM, ...over };
72
+ }
73
+
74
+ // ---------------------------------------------------------------------------
75
+ // coordsOf — modern + legacy event shapes
76
+ // ---------------------------------------------------------------------------
77
+
78
+ test("coordsOf reads {family,kind} and the legacy dotted kind", () => {
79
+ assert.deepEqual(coordsOf({ family: "board", kind: "item.assigned" }), {
80
+ family: "board",
81
+ kind: "item.assigned",
82
+ });
83
+ assert.deepEqual(coordsOf({ kind: "messaging.send" }), {
84
+ family: "messaging",
85
+ kind: "send",
86
+ });
87
+ });
88
+
89
+ // ---------------------------------------------------------------------------
90
+ // THE SAFETY INVARIANT — someone else's private room
91
+ // ---------------------------------------------------------------------------
92
+
93
+ test("NEGATIVE: a DM between two other people is never directed (not a member)", () => {
94
+ const f = facts();
95
+ // The room is not in my roster at all — hq only returns a DM to its members.
96
+ const c = classifyEvent(
97
+ ev({ family: "messaging", kind: "send", entity_id: "msg-1", payload: { actor: THEM, channelId: "C-theirs", channelKind: "DM" } }),
98
+ );
99
+ const v = resolveDirected(c, ME, f);
100
+ assert.equal(v.directed, false);
101
+ assert.equal(v.reason, "channel_not_visible");
102
+ });
103
+
104
+ test("NEGATIVE: a DM visible-but-not-member is refused (belt and braces)", () => {
105
+ const f = facts();
106
+ // Deliberately inconsistent state: visible without membership. Must still refuse.
107
+ f.channels.set("C-theirs", { id: "C-theirs", kind: "DM" });
108
+ f.visibleChannelIds.add("C-theirs");
109
+ const c = classifyEvent(
110
+ ev({ family: "messaging", kind: "send", entity_id: "m1", payload: { actor: THEM, channelId: "C-theirs", channelKind: "DM" } }),
111
+ );
112
+ const v = resolveDirected(c, ME, f);
113
+ assert.equal(v.directed, false);
114
+ assert.equal(v.reason, "not_a_member");
115
+ });
116
+
117
+ test("NEGATIVE: roster unreadable → private rooms stay closed, not open", () => {
118
+ const f = facts({ visibleChannelIds: null, memberChannelIds: null });
119
+ const c = classifyEvent(
120
+ ev({ family: "messaging", kind: "send", entity_id: "m1", payload: { actor: THEM, channelId: "C-x", channelKind: "DM" } }),
121
+ );
122
+ const v = resolveDirected(c, ME, f);
123
+ assert.equal(v.directed, false);
124
+ assert.equal(v.reason, "membership_unknown");
125
+ });
126
+
127
+ test("NEGATIVE: a PRIVATE space I am not in is refused before any mention probe", () => {
128
+ const f = facts();
129
+ f.channels.set("C-priv", { id: "C-priv", kind: "PRIVATE" });
130
+ f.visibleChannelIds.add("C-priv"); // visible but NOT a member
131
+ withMessage(f, "C-priv", { id: "m9", authorId: THEM, body: "hey", mentions: [ME] });
132
+ const c = classifyEvent(
133
+ ev({ family: "messaging", kind: "send", entity_id: "m9", payload: { actor: THEM, channelId: "C-priv", channelKind: "PRIVATE", mentionCount: 1 } }),
134
+ );
135
+ const v = resolveDirected(c, ME, f);
136
+ assert.equal(v.directed, false, "a mention in a private space I am not in must not reach me");
137
+ assert.equal(v.reason, "not_a_member");
138
+ });
139
+
140
+ test("NEGATIVE: my own echo is never inbound", () => {
141
+ const f = withChannel(facts(), "C-dm", "DM");
142
+ const c = classifyEvent(
143
+ ev({ family: "messaging", kind: "send", actor: ME, entity_id: "m1", payload: { actor: ME, channelId: "C-dm", channelKind: "DM" } }),
144
+ );
145
+ assert.equal(resolveDirected(c, ME, f).reason, "own_echo");
146
+ });
147
+
148
+ // ---------------------------------------------------------------------------
149
+ // messaging — DM / mention / thread / ambient
150
+ // ---------------------------------------------------------------------------
151
+
152
+ test("POSITIVE: a message in a DM I belong to is directed (reason dm)", () => {
153
+ const f = withChannel(facts(), "C-dm", "DM");
154
+ withMessage(f, "C-dm", { id: "m1", authorId: THEM, body: "ping" });
155
+ const c = classifyEvent(
156
+ ev({ family: "messaging", kind: "send", entity_id: "m1", payload: { actor: THEM, channelId: "C-dm", channelKind: "DM", mentionCount: 0, threaded: false } }),
157
+ );
158
+ const v = resolveDirected(c, ME, f);
159
+ assert.deepEqual({ d: v.directed, s: v.surface, r: v.reason }, { d: true, s: "dm", r: "dm" });
160
+ });
161
+
162
+ test("POSITIVE: an @mention in a PUBLIC space is directed (THE headline gap)", () => {
163
+ const f = withChannel(facts(), "C-pub", "PUBLIC");
164
+ withMessage(f, "C-pub", { id: "m2", authorId: THEM, body: "@Isla can you look?", mentions: [ME] });
165
+ const c = classifyEvent(
166
+ ev({ family: "messaging", kind: "send", entity_id: "m2", payload: { actor: THEM, channelId: "C-pub", channelKind: "PUBLIC", mentionCount: 1, threaded: false } }),
167
+ );
168
+ const v = resolveDirected(c, ME, f);
169
+ assert.deepEqual({ d: v.directed, s: v.surface, r: v.reason }, { d: true, s: "mention", r: "mention" });
170
+ });
171
+
172
+ test("POSITIVE: an @mention in a PRIVATE space I AM in is directed", () => {
173
+ const f = withChannel(facts(), "C-priv", "PRIVATE");
174
+ withMessage(f, "C-priv", { id: "m3", authorId: THEM, body: "over to you", mentions: [ME] });
175
+ const c = classifyEvent(
176
+ ev({ family: "messaging", kind: "send", entity_id: "m3", payload: { actor: THEM, channelId: "C-priv", channelKind: "PRIVATE", mentionCount: 1, threaded: false } }),
177
+ );
178
+ assert.equal(resolveDirected(c, ME, f).surface, "mention");
179
+ });
180
+
181
+ test("POSITIVE: a reply in a thread I have spoken in is directed (reason thread)", () => {
182
+ const f = withChannel(facts(), "C-pub", "PUBLIC");
183
+ f.myThreadRootIds.add("root-1");
184
+ withMessage(f, "C-pub", { id: "m4", authorId: THEM, body: "and one more thing", threadRootId: "root-1", mentions: [] });
185
+ const c = classifyEvent(
186
+ ev({ family: "messaging", kind: "send", entity_id: "m4", payload: { actor: THEM, channelId: "C-pub", channelKind: "PUBLIC", mentionCount: 0, threaded: true } }),
187
+ );
188
+ const v = resolveDirected(c, ME, f);
189
+ assert.deepEqual({ s: v.surface, r: v.reason }, { s: "thread_reply", r: "thread" });
190
+ });
191
+
192
+ test("NEGATIVE: ambient public chatter that names nobody is not directed", () => {
193
+ const f = withChannel(facts(), "C-pub", "PUBLIC");
194
+ withMessage(f, "C-pub", { id: "m5", authorId: THEM, body: "standup at 10", mentions: [] });
195
+ const c = classifyEvent(
196
+ ev({ family: "messaging", kind: "send", entity_id: "m5", payload: { actor: THEM, channelId: "C-pub", channelKind: "PUBLIC", mentionCount: 0, threaded: false } }),
197
+ );
198
+ const v = resolveDirected(c, ME, f);
199
+ assert.equal(v.directed, false);
200
+ // The redacted counts alone prove it — no page was needed.
201
+ assert.equal(v.reason, "no_mentions_not_threaded");
202
+ });
203
+
204
+ test("NEGATIVE: a thread reply in a thread I am NOT in is not directed", () => {
205
+ const f = withChannel(facts(), "C-pub", "PUBLIC");
206
+ withMessage(f, "C-pub", { id: "m6", authorId: THEM, body: "sure", threadRootId: "root-other", mentions: [] });
207
+ const c = classifyEvent(
208
+ ev({ family: "messaging", kind: "send", entity_id: "m6", payload: { actor: THEM, channelId: "C-pub", channelKind: "PUBLIC", mentionCount: 0, threaded: true } }),
209
+ );
210
+ assert.equal(resolveDirected(c, ME, f).directed, false);
211
+ });
212
+
213
+ test("needsSpaceProbe: counts prove it, unless prose-name matching is on", () => {
214
+ const cheap = { hints: { mentionCount: 0, threaded: false } };
215
+ assert.equal(needsSpaceProbe(cheap, {}), false);
216
+ assert.equal(needsSpaceProbe(cheap, { myNames: ["Isla"] }), null, "name matching needs the body");
217
+ assert.equal(needsSpaceProbe({ hints: { mentionCount: 2, threaded: false } }, {}), true);
218
+ assert.equal(needsSpaceProbe({ hints: {} }, {}), null, "absent counts → probe to be safe");
219
+ });
220
+
221
+ test("POSITIVE: prose-name mention in a public space when myNames is supplied", () => {
222
+ const f = withChannel(facts({ myNames: ["Isla"] }), "C-pub", "PUBLIC");
223
+ withMessage(f, "C-pub", { id: "m7", authorId: THEM, body: "Isla, can you take this?", mentions: [] });
224
+ const c = classifyEvent(
225
+ ev({ family: "messaging", kind: "send", entity_id: "m7", payload: { actor: THEM, channelId: "C-pub", channelKind: "PUBLIC", mentionCount: 0, threaded: false } }),
226
+ );
227
+ const v = resolveDirected(c, ME, f);
228
+ assert.deepEqual({ s: v.surface, r: v.reason }, { s: "mention", r: "named" });
229
+ });
230
+
231
+ // ---------------------------------------------------------------------------
232
+ // calling
233
+ // ---------------------------------------------------------------------------
234
+
235
+ test("POSITIVE/NEGATIVE: a call in my room is directed; in a room I am not in it is not", () => {
236
+ const f = withChannel(facts(), "C-dm", "DM");
237
+ const mine = classifyEvent(
238
+ ev({ family: "calling", kind: "call.started", entity_id: "call-1", payload: { actor: THEM, callId: "call-1", channelId: "C-dm", topic: "sync" } }),
239
+ );
240
+ assert.equal(resolveDirected(mine, ME, f).surface, "call");
241
+
242
+ const theirs = classifyEvent(
243
+ ev({ family: "calling", kind: "call.started", entity_id: "call-2", payload: { actor: THEM, callId: "call-2", channelId: "C-other" } }),
244
+ );
245
+ assert.equal(resolveDirected(theirs, ME, f).directed, false);
246
+ });
247
+
248
+ test("POSITIVE: a call whose payload names me as an invitee is directed", () => {
249
+ const c = classifyEvent(
250
+ ev({ family: "calling", kind: "call.invited", entity_id: "call-3", payload: { actor: THEM, callId: "call-3", invitees: [ME] } }),
251
+ );
252
+ const v = resolveDirected(c, ME, facts());
253
+ assert.deepEqual({ s: v.surface, r: v.reason }, { s: "call", r: "participant" });
254
+ });
255
+
256
+ // ---------------------------------------------------------------------------
257
+ // board
258
+ // ---------------------------------------------------------------------------
259
+
260
+ test("POSITIVE: board.item.assigned naming me needs no join at all", () => {
261
+ const c = classifyEvent(
262
+ ev({ family: "board", kind: "item.assigned", entity_id: "T-1", payload: { actor: THEM, assignee: ME } }),
263
+ );
264
+ const v = resolveDirected(c, ME, facts());
265
+ assert.deepEqual({ s: v.surface, r: v.reason }, { s: "task_assigned", r: "assignee" });
266
+ });
267
+
268
+ test("POSITIVE: a comment on a task I own is directed (reason assignee)", () => {
269
+ const f = facts({ taskRole: new Map([["T-1", "assignee"]]) });
270
+ const c = classifyEvent(
271
+ ev({ family: "board", kind: "item.commented", entity_id: "T-1", payload: { actor: THEM, kind: "human" } }),
272
+ );
273
+ const v = resolveDirected(c, ME, f);
274
+ assert.deepEqual({ s: v.surface, r: v.reason }, { s: "task_comment", r: "assignee" });
275
+ });
276
+
277
+ test("POSITIVE: a comment on a task I REVIEW is directed (reason reviewer)", () => {
278
+ const f = facts({ taskRole: new Map([["T-2", "reviewer"]]) });
279
+ const c = classifyEvent(ev({ family: "board", kind: "board.blocked", entity_id: "T-2", payload: { actor: THEM, reason: "waiting" } }));
280
+ assert.equal(resolveDirected(c, ME, f).reason, "reviewer");
281
+ });
282
+
283
+ test("NEGATIVE: a comment on somebody else's task is not directed", () => {
284
+ const f = facts({ taskRole: new Map([["T-1", "assignee"]]) });
285
+ const c = classifyEvent(ev({ family: "board", kind: "item.commented", entity_id: "T-99", payload: { actor: THEM } }));
286
+ const v = resolveDirected(c, ME, f);
287
+ assert.equal(v.directed, false);
288
+ assert.equal(v.reason, "not_my_task");
289
+ });
290
+
291
+ test("NEGATIVE: board facts unreadable → not directed, and the reason says so", () => {
292
+ const c = classifyEvent(ev({ family: "board", kind: "item.commented", entity_id: "T-1", payload: { actor: THEM } }));
293
+ assert.equal(resolveDirected(c, ME, facts()).reason, "board_unknown");
294
+ });
295
+
296
+ test("NEGATIVE: an assignment to SOMEONE ELSE is not directed at me", () => {
297
+ const c = classifyEvent(
298
+ ev({ family: "board", kind: "item.assigned", entity_id: "T-7", payload: { actor: ME, assignee: THEM } }),
299
+ );
300
+ // (actor is me here, so it is also my own echo — assert with a third party.)
301
+ const c2 = classifyEvent(
302
+ ev({ family: "board", kind: "item.assigned", entity_id: "T-7", actor: "M-boss", payload: { actor: "M-boss", assignee: THEM } }),
303
+ );
304
+ assert.equal(resolveDirected(c, ME, facts()).directed, false);
305
+ assert.equal(resolveDirected(c2, ME, facts()).directed, false);
306
+ });
307
+
308
+ // ---------------------------------------------------------------------------
309
+ // file (chat attachment) + files (workspace doc)
310
+ // ---------------------------------------------------------------------------
311
+
312
+ test("POSITIVE: a file comment in a room I belong to is directed", () => {
313
+ const f = withChannel(facts(), "C-team", "PRIVATE");
314
+ const c = classifyEvent(
315
+ ev({ family: "file", kind: "file.commented", entity_id: "fc-1", payload: { fileKey: "message-attachment:x", channelId: "C-team", authorId: THEM } }),
316
+ );
317
+ const v = resolveDirected(c, ME, f);
318
+ assert.deepEqual({ s: v.surface, r: v.reason }, { s: "file_comment", r: "channel" });
319
+ });
320
+
321
+ test("NEGATIVE: a file comment in a room I am not in is not directed", () => {
322
+ const f = facts();
323
+ const c = classifyEvent(
324
+ ev({ family: "file", kind: "file.commented", entity_id: "fc-2", payload: { fileKey: "message-attachment:y", channelId: "C-theirs", authorId: THEM } }),
325
+ );
326
+ assert.equal(resolveDirected(c, ME, f).directed, false);
327
+ });
328
+
329
+ test("POSITIVE: a comment on MY task's attachment survives a missing channelId", () => {
330
+ const f = facts({ taskRole: new Map([["T-5", "assignee"]]) });
331
+ const c = classifyEvent(
332
+ ev({ family: "file", kind: "file.commented", entity_id: "fc-3", payload: { fileKey: "task-attachment:T-5:doc.pdf", channelId: null, authorId: THEM } }),
333
+ );
334
+ assert.equal(resolveDirected(c, ME, f).reason, "assignee");
335
+ });
336
+
337
+ test("taskIdFromFileKey recovers the task, and only for task attachments", () => {
338
+ assert.equal(taskIdFromFileKey("task-attachment:T-5:doc.pdf"), "T-5");
339
+ assert.equal(taskIdFromFileKey("message-attachment:abc"), "");
340
+ assert.equal(taskIdFromFileKey(undefined), "");
341
+ });
342
+
343
+ test("POSITIVE/NEGATIVE: doc comments follow ownership and shares", () => {
344
+ const owned = facts({ fileAcl: new Map([["F-1", { ownerId: ME, shared: false }]]) });
345
+ const shared = facts({ fileAcl: new Map([["F-2", { ownerId: THEM, shared: true }]]) });
346
+ const named = facts({ fileAcl: new Map([["F-3", { ownerId: THEM, shared: false, mentionsMe: true }]]) });
347
+ const foreign = facts({ fileAcl: new Map([["F-4", { ownerId: THEM, shared: false }]]) });
348
+ const mk = (id) => classifyEvent(ev({ family: "files", kind: "doc.comment", entity_id: id, payload: { name: "spec", commentId: "dc1" } }));
349
+
350
+ assert.equal(resolveDirected(mk("F-1"), ME, owned).reason, "owner");
351
+ assert.equal(resolveDirected(mk("F-2"), ME, shared).reason, "shared");
352
+ assert.equal(resolveDirected(mk("F-3"), ME, named).reason, "named");
353
+ assert.equal(resolveDirected(mk("F-4"), ME, foreign).directed, false);
354
+ // A doc that files.get could not see at all is NOT_FOUND → never mine.
355
+ assert.equal(resolveDirected(mk("F-9"), ME, foreign).reason, "file_not_visible");
356
+ });
357
+
358
+ // ---------------------------------------------------------------------------
359
+ // decision
360
+ // ---------------------------------------------------------------------------
361
+
362
+ test("POSITIVE: a comment on a decision I proposed is directed (reason proposer)", () => {
363
+ const f = facts({ decisions: new Map([["D-1", { id: "D-1", title: "Ship it", proposedBy: ME }]]) });
364
+ const c = classifyEvent(
365
+ ev({ family: "decision", kind: "decision.comment", entity_id: "D-1", payload: { commentId: "dc1", authorId: THEM, length: 10 } }),
366
+ );
367
+ const v = resolveDirected(c, ME, f);
368
+ assert.deepEqual({ s: v.surface, r: v.reason }, { s: "decision", r: "proposer" });
369
+ });
370
+
371
+ test("POSITIVE: an adjustment on a decision I have commented on is directed", () => {
372
+ const f = facts({
373
+ decisions: new Map([["D-2", { id: "D-2", proposedBy: THEM }]]),
374
+ decisionVoices: new Map([["D-2", new Set([ME, THEM])]]),
375
+ });
376
+ const c = classifyEvent(
377
+ ev({ family: "decision", kind: "decision.adjustment_requested", entity_id: "D-2", payload: { commentId: "dc2", authorId: THEM } }),
378
+ );
379
+ assert.equal(resolveDirected(c, ME, f).reason, "commenter");
380
+ });
381
+
382
+ test("NEGATIVE: a decision I have nothing to do with is not directed", () => {
383
+ const f = facts({
384
+ decisions: new Map([["D-3", { id: "D-3", proposedBy: THEM }]]),
385
+ decisionVoices: new Map([["D-3", new Set([THEM])]]),
386
+ });
387
+ const c = classifyEvent(ev({ family: "decision", kind: "decision.comment", entity_id: "D-3", payload: { authorId: THEM } }));
388
+ const v = resolveDirected(c, ME, f);
389
+ assert.equal(v.directed, false);
390
+ assert.equal(v.reason, "not_my_decision");
391
+ });
392
+
393
+ // ---------------------------------------------------------------------------
394
+ // escalation
395
+ // ---------------------------------------------------------------------------
396
+
397
+ test("POSITIVE: an escalation waiting on me is directed", () => {
398
+ const f = facts({ escalations: new Map([["E-1", { id: "E-1", title: "prod down", waitingOn: ME }]]) });
399
+ const c = classifyEvent(
400
+ ev({ family: "escalation", kind: "escalation.raised", entity_id: "E-1", payload: { actor: THEM, severity: "high", taskId: null, channelId: null } }),
401
+ );
402
+ const v = resolveDirected(c, ME, f);
403
+ assert.deepEqual({ s: v.surface, r: v.reason }, { s: "escalation", r: "waiting_on" });
404
+ });
405
+
406
+ test("POSITIVE: an escalation on my task is directed", () => {
407
+ const f = facts({
408
+ escalations: new Map([["E-2", { id: "E-2", taskId: "T-1" }]]),
409
+ taskRole: new Map([["T-1", "assignee"]]),
410
+ });
411
+ const c = classifyEvent(
412
+ ev({ family: "escalation", kind: "escalation.raised", entity_id: "E-2", payload: { actor: THEM, taskId: "T-1" } }),
413
+ );
414
+ assert.equal(resolveDirected(c, ME, f).reason, "assignee");
415
+ });
416
+
417
+ test("NEGATIVE: an escalation in a room I am not in is not directed", () => {
418
+ const f = facts({ escalations: new Map([["E-3", { id: "E-3", channelId: "C-theirs" }]]), taskRole: new Map() });
419
+ const c = classifyEvent(
420
+ ev({ family: "escalation", kind: "escalation.raised", entity_id: "E-3", payload: { actor: THEM, channelId: "C-theirs" } }),
421
+ );
422
+ assert.equal(resolveDirected(c, ME, f).directed, false);
423
+ });
424
+
425
+ // ---------------------------------------------------------------------------
426
+ // approval
427
+ // ---------------------------------------------------------------------------
428
+
429
+ test("POSITIVE: an approval whose row names me as approver is directed", () => {
430
+ const f = facts({ approvals: new Map([["A-1", { id: "A-1", requester: THEM, approver: ME, status: "pending", actionClass: "spend" }]]) });
431
+ const c = classifyEvent(
432
+ ev({ family: "approval", kind: "approval.requested", entity_id: "A-1", payload: { requester: THEM, actionClass: "spend", payloadHash: "h" } }),
433
+ );
434
+ const v = resolveDirected(c, ME, f);
435
+ assert.deepEqual({ s: v.surface, r: v.reason }, { s: "approval", r: "approver" });
436
+ });
437
+
438
+ test("POSITIVE: the resolution of an approval I requested reaches me (payload-named)", () => {
439
+ const c = classifyEvent(
440
+ ev({ family: "approval", kind: "approval.approved", entity_id: "A-2", payload: { requester: ME, approver: THEM } }),
441
+ );
442
+ const v = resolveDirected(c, ME, facts());
443
+ assert.deepEqual({ s: v.surface, r: v.reason }, { s: "approval", r: "requester" });
444
+ });
445
+
446
+ test("NEGATIVE: an approval on someone else's desk is not directed", () => {
447
+ const f = facts({ approvals: new Map([["A-3", { id: "A-3", requester: THEM, approver: "M-boss", status: "pending" }]]) });
448
+ const c = classifyEvent(ev({ family: "approval", kind: "approval.requested", entity_id: "A-3", payload: { requester: THEM } }));
449
+ const v = resolveDirected(c, ME, f);
450
+ assert.equal(v.directed, false);
451
+ assert.equal(v.reason, "not_my_approval");
452
+ });
453
+
454
+ // ---------------------------------------------------------------------------
455
+ // handoff
456
+ // ---------------------------------------------------------------------------
457
+
458
+ test("POSITIVE: a handoff offered to me is directed; one between others is not", () => {
459
+ const mine = classifyEvent(
460
+ ev({ family: "handoff", kind: "handoff.offered", entity_id: "R-1", payload: { itemId: "T-1", from: THEM, to: ME, intent: "review" } }),
461
+ );
462
+ const v = resolveDirected(mine, ME, facts());
463
+ assert.deepEqual({ s: v.surface, r: v.reason }, { s: "handoff", r: "direct" });
464
+
465
+ const theirs = classifyEvent(
466
+ ev({ family: "handoff", kind: "handoff.offered", entity_id: "R-2", payload: { itemId: "T-2", from: THEM, to: "M-boss" } }),
467
+ );
468
+ assert.equal(resolveDirected(theirs, ME, facts()).directed, false);
469
+ });
470
+
471
+ test("POSITIVE: my own offer being accepted comes back as reason requester", () => {
472
+ const c = classifyEvent(
473
+ ev({ family: "handoff", kind: "handoff.accepted", actor: THEM, entity_id: "R-3", payload: { itemId: "T-3", from: ME, to: THEM } }),
474
+ );
475
+ assert.equal(resolveDirected(c, ME, facts()).reason, "requester");
476
+ });
477
+
478
+ // ---------------------------------------------------------------------------
479
+ // email
480
+ // ---------------------------------------------------------------------------
481
+
482
+ test("POSITIVE: inbound email routed to my mailbox is directed (payload names the seat)", () => {
483
+ const c = classifyEvent(
484
+ ev({ family: "email", kind: "received", actor: "system", entity_id: "EM-1", payload: { emailMessageId: "EM-1", threadId: "ET-1", mailboxId: "MB-1", memberId: ME, autoSubmitted: false } }),
485
+ );
486
+ const v = resolveDirected(c, ME, facts(), { enabled: { email: true } });
487
+ assert.deepEqual({ s: v.surface, r: v.reason }, { s: "email", r: "direct" });
488
+ });
489
+
490
+ test("NEGATIVE: another seat's mail, unrouted mail, and auto-submitted mail are all refused", () => {
491
+ const other = classifyEvent(
492
+ ev({ family: "email", kind: "received", entity_id: "EM-2", payload: { memberId: THEM, threadId: "ET-2" } }),
493
+ );
494
+ assert.equal(resolveDirected(other, ME, facts(), { enabled: { email: true } }).directed, false);
495
+
496
+ const unrouted = classifyEvent(
497
+ ev({ family: "email", kind: "received", entity_id: "EM-3", payload: { memberId: null, unrouted: true } }),
498
+ );
499
+ assert.equal(unrouted, null, "unrouted mail belongs to nobody");
500
+
501
+ const auto = classifyEvent(
502
+ ev({ family: "email", kind: "received", entity_id: "EM-4", payload: { memberId: ME, autoSubmitted: true } }),
503
+ );
504
+ assert.equal(auto, null, "vacation autoreplies are not inbound work");
505
+ });
506
+
507
+ test("email is OFF by default, so the default enable map refuses it", () => {
508
+ const c = classifyEvent(
509
+ ev({ family: "email", kind: "received", entity_id: "EM-5", payload: { memberId: ME, threadId: "ET-5" } }),
510
+ );
511
+ const v = resolveDirected(c, ME, facts(), { enabled: { email: false } });
512
+ assert.equal(v.directed, false);
513
+ assert.equal(v.reason, "surface_disabled");
514
+ });
515
+
516
+ // ---------------------------------------------------------------------------
517
+ // families a daemon must NOT be woken for
518
+ // ---------------------------------------------------------------------------
519
+
520
+ test("noise families produce no candidate at all", () => {
521
+ for (const family of ["presence", "cost", "registry", "member", "branding", "subagent"]) {
522
+ assert.equal(classifyEvent(ev({ family, kind: "beat", payload: { actor: THEM } })), null, family);
523
+ }
524
+ });
525
+
526
+ test("classifyEvent never throws on malformed rows", () => {
527
+ for (const bad of [null, undefined, 42, "x", {}, { family: "board" }, { family: "messaging", kind: "send", payload: null }]) {
528
+ assert.doesNotThrow(() => classifyEvent(bad));
529
+ }
530
+ });
531
+
532
+ // ---------------------------------------------------------------------------
533
+ // matchesMyName
534
+ // ---------------------------------------------------------------------------
535
+
536
+ test("matchesMyName respects word boundaries and the @ prefix", () => {
537
+ const f = { myNames: ["Isla", "Isla Roselli"] };
538
+ assert.equal(matchesMyName(f, "Isla, can you look?"), true);
539
+ assert.equal(matchesMyName(f, "@isla please"), true);
540
+ assert.equal(matchesMyName(f, "islands are nice"), false);
541
+ assert.equal(matchesMyName(f, "the island"), false);
542
+ assert.equal(matchesMyName({ myNames: [] }, "Isla"), false, "no names configured → never matches");
543
+ });