@vellumai/assistant 0.10.11-dev.202607221933.baa2119 → 0.10.11-dev.202607222129.31c06e5

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 (59) hide show
  1. package/node_modules/@vellumai/gateway-client/src/__tests__/binding-strength-contract.test.ts +122 -0
  2. package/node_modules/@vellumai/gateway-client/src/binding-strength-contract.ts +155 -0
  3. package/node_modules/@vellumai/gateway-client/src/index.ts +14 -0
  4. package/package.json +1 -1
  5. package/src/__tests__/approval-source-link.test.ts +208 -0
  6. package/src/__tests__/btw-routes.test.ts +13 -2
  7. package/src/__tests__/conversation-store-ephemeral.test.ts +182 -0
  8. package/src/__tests__/plugin-import-boundary-guard.test.ts +1 -1
  9. package/src/__tests__/slack-notification-approval-card.test.ts +103 -20
  10. package/src/__tests__/tool-approval-seed-content-blocks.test.ts +179 -96
  11. package/src/__tests__/ui-choice-copy-surfaces.test.ts +144 -3
  12. package/src/api/events/memory-recalled.ts +59 -0
  13. package/src/api/events/memory-status.ts +29 -0
  14. package/src/api/events/service-group-update-complete.ts +29 -0
  15. package/src/api/events/service-group-update-progress.ts +23 -0
  16. package/src/api/events/service-group-update-starting.ts +26 -0
  17. package/src/api/index.ts +36 -1
  18. package/src/api/surfaces.ts +77 -21
  19. package/src/calls/__tests__/voice-triage-escalate.test.ts +58 -3
  20. package/src/calls/voice-session-bridge.ts +6 -2
  21. package/src/calls/voice-triage-escalate.ts +30 -1
  22. package/src/config/feature-flag-registry.json +9 -0
  23. package/src/daemon/conversation-store.ts +14 -2
  24. package/src/daemon/conversation-surfaces.ts +78 -168
  25. package/src/daemon/conversation-tool-setup.ts +7 -3
  26. package/src/daemon/handlers/shared.ts +11 -1
  27. package/src/daemon/message-types/memory.ts +8 -46
  28. package/src/daemon/message-types/surfaces.ts +1 -1
  29. package/src/daemon/message-types/upgrades.ts +11 -28
  30. package/src/daemon/trust-context-types.ts +8 -0
  31. package/src/messaging/channel-binding-schema.ts +6 -2
  32. package/src/messaging/providers/slack/approval-source.ts +87 -0
  33. package/src/messaging/providers/slack/deep-link.ts +45 -8
  34. package/src/messaging/providers/slack/message-metadata.ts +16 -0
  35. package/src/notifications/AGENTS.md +2 -0
  36. package/src/notifications/access-request-copy.ts +10 -21
  37. package/src/notifications/adapters/slack.ts +67 -13
  38. package/src/notifications/approval-card-data.ts +58 -16
  39. package/src/notifications/broadcaster.ts +41 -19
  40. package/src/notifications/guardian-question-mode.ts +81 -0
  41. package/src/notifications/types.ts +5 -0
  42. package/src/persistence/delivery-crud.ts +48 -1
  43. package/src/persistence/migrations/347-delete-stray-greeting-conversation.ts +46 -0
  44. package/src/persistence/migrations/__tests__/347-delete-stray-greeting-conversation.test.ts +100 -0
  45. package/src/persistence/steps.ts +2 -0
  46. package/src/plugins/defaults/memory/hooks/user-prompt-submit.ts +2 -2
  47. package/src/plugins/mtime-cache.ts +21 -10
  48. package/src/runtime/approval-source-link.ts +107 -0
  49. package/src/runtime/confirmation-request-guardian-bridge.ts +9 -0
  50. package/src/runtime/introduction-policy.ts +23 -54
  51. package/src/runtime/routes/btw-routes.ts +9 -2
  52. package/src/runtime/routes/inbound-message-handler.ts +5 -13
  53. package/src/runtime/routes/upgrade-broadcast-routes.ts +6 -8
  54. package/src/runtime/tool-grant-request-helper.ts +14 -0
  55. package/src/tools/registry.ts +26 -17
  56. package/src/tools/tool-approval-handler.ts +2 -0
  57. package/src/tools/types.ts +8 -0
  58. package/src/tools/ui-surface/definitions.ts +20 -16
  59. package/src/tools/ui-surface/surface-shape-docs.ts +21 -6
@@ -0,0 +1,122 @@
1
+ /**
2
+ * Tests for the shared binding-strength vocabulary: the display ladder and the
3
+ * enforcement order (demotion guard — LUM-2505).
4
+ */
5
+
6
+ import { describe, expect, test } from "bun:test";
7
+
8
+ import {
9
+ bindingStrengthForVerifiedVia,
10
+ isBindingDemotion,
11
+ knownStrengthRank,
12
+ VERIFIED_VIA_CHALLENGE,
13
+ VERIFIED_VIA_CHANNEL_CLAIM,
14
+ VERIFIED_VIA_MANUAL,
15
+ } from "../binding-strength-contract.js";
16
+
17
+ describe("knownStrengthRank", () => {
18
+ test("unverified (null / undefined / empty) ranks 0", () => {
19
+ expect(knownStrengthRank(null)).toBe(0);
20
+ expect(knownStrengthRank(undefined)).toBe(0);
21
+ expect(knownStrengthRank("")).toBe(0);
22
+ });
23
+
24
+ test("inbound channel claim ranks 1", () => {
25
+ expect(knownStrengthRank(VERIFIED_VIA_CHANNEL_CLAIM)).toBe(1);
26
+ });
27
+
28
+ test("workspace-vouched (manual) ranks 2", () => {
29
+ expect(knownStrengthRank(VERIFIED_VIA_MANUAL)).toBe(2);
30
+ });
31
+
32
+ test("proven / possession values all rank 3", () => {
33
+ expect(knownStrengthRank(VERIFIED_VIA_CHALLENGE)).toBe(3);
34
+ expect(knownStrengthRank("invite")).toBe(3);
35
+ expect(knownStrengthRank("voice")).toBe(3);
36
+ expect(knownStrengthRank("bootstrap")).toBe(3);
37
+ // Guardian auto-registration bindings (createGuardianBinding writers).
38
+ expect(knownStrengthRank("platform_auto_register")).toBe(3);
39
+ expect(knownStrengthRank("webhook_registration")).toBe(3);
40
+ });
41
+
42
+ test("unrecognized non-empty provenance is unknown (null)", () => {
43
+ expect(knownStrengthRank("guardian_approval")).toBeNull();
44
+ expect(knownStrengthRank("something_new")).toBeNull();
45
+ });
46
+ });
47
+
48
+ describe("bindingStrengthForVerifiedVia (display ladder)", () => {
49
+ test("maps the three introduction-card provenances", () => {
50
+ expect(bindingStrengthForVerifiedVia(VERIFIED_VIA_CHALLENGE)).toBe(
51
+ "verified_handshake",
52
+ );
53
+ expect(bindingStrengthForVerifiedVia(VERIFIED_VIA_MANUAL)).toBe(
54
+ "internal_workspace_match",
55
+ );
56
+ expect(bindingStrengthForVerifiedVia(VERIFIED_VIA_CHANNEL_CLAIM)).toBe(
57
+ "inbound_channel_claim",
58
+ );
59
+ });
60
+
61
+ test("returns null for provenance outside the ladder (card UI relies on this)", () => {
62
+ expect(bindingStrengthForVerifiedVia("invite")).toBeNull();
63
+ expect(bindingStrengthForVerifiedVia("bootstrap")).toBeNull();
64
+ expect(bindingStrengthForVerifiedVia("voice")).toBeNull();
65
+ expect(bindingStrengthForVerifiedVia(null)).toBeNull();
66
+ expect(bindingStrengthForVerifiedVia(undefined)).toBeNull();
67
+ });
68
+ });
69
+
70
+ describe("isBindingDemotion", () => {
71
+ test("a lower-strength source demoting a higher-strength binding is refused", () => {
72
+ // challenge (3) is the strongest — nothing weaker may overwrite it.
73
+ expect(isBindingDemotion(VERIFIED_VIA_CHALLENGE, VERIFIED_VIA_MANUAL)).toBe(
74
+ true,
75
+ );
76
+ expect(
77
+ isBindingDemotion(VERIFIED_VIA_CHALLENGE, VERIFIED_VIA_CHANNEL_CLAIM),
78
+ ).toBe(true);
79
+ // manual (2) may not be demoted to an inbound claim (1).
80
+ expect(
81
+ isBindingDemotion(VERIFIED_VIA_MANUAL, VERIFIED_VIA_CHANNEL_CLAIM),
82
+ ).toBe(true);
83
+ // A proven value demoted to null/unverified is a demotion too.
84
+ expect(isBindingDemotion(VERIFIED_VIA_CHALLENGE, null)).toBe(true);
85
+ // Guardian auto-registration bindings are proven — a later manual attest
86
+ // or channel claim can never demote them.
87
+ expect(
88
+ isBindingDemotion("platform_auto_register", VERIFIED_VIA_MANUAL),
89
+ ).toBe(true);
90
+ expect(
91
+ isBindingDemotion("webhook_registration", VERIFIED_VIA_CHANNEL_CLAIM),
92
+ ).toBe(true);
93
+ });
94
+
95
+ test("equal-tier writes and lateral proven swaps are not demotions", () => {
96
+ expect(isBindingDemotion(VERIFIED_VIA_CHALLENGE, "invite")).toBe(false);
97
+ expect(isBindingDemotion("invite", VERIFIED_VIA_CHALLENGE)).toBe(false);
98
+ expect(isBindingDemotion(VERIFIED_VIA_MANUAL, VERIFIED_VIA_MANUAL)).toBe(
99
+ false,
100
+ );
101
+ });
102
+
103
+ test("upgrades are not demotions", () => {
104
+ expect(
105
+ isBindingDemotion(VERIFIED_VIA_CHANNEL_CLAIM, VERIFIED_VIA_CHALLENGE),
106
+ ).toBe(false);
107
+ expect(isBindingDemotion(null, VERIFIED_VIA_CHANNEL_CLAIM)).toBe(false);
108
+ expect(isBindingDemotion(VERIFIED_VIA_MANUAL, VERIFIED_VIA_CHALLENGE)).toBe(
109
+ false,
110
+ );
111
+ });
112
+
113
+ test("unknown provenance on either side is never a demotion (fail-open)", () => {
114
+ expect(isBindingDemotion(VERIFIED_VIA_CHALLENGE, "guardian_approval")).toBe(
115
+ false,
116
+ );
117
+ expect(
118
+ isBindingDemotion("guardian_approval", VERIFIED_VIA_CHANNEL_CLAIM),
119
+ ).toBe(false);
120
+ expect(isBindingDemotion("unknown_a", "unknown_b")).toBe(false);
121
+ });
122
+ });
@@ -0,0 +1,155 @@
1
+ /**
2
+ * Shared binding-strength vocabulary for the contact channel `verified_via`
3
+ * provenance column.
4
+ *
5
+ * Two distinct notions live here, both keyed on the free-text `verified_via`
6
+ * audit value:
7
+ *
8
+ * 1. The 3-tier *display* ladder (`BindingStrength` +
9
+ * `bindingStrengthForVerifiedVia`) the introduction card renders from. It
10
+ * covers only the three introduction-card provenances and returns `null`
11
+ * for everything else — the card UI depends on that exact shape.
12
+ *
13
+ * 2. The *enforcement* order (`knownStrengthRank` + `isBindingDemotion`) the
14
+ * gateway ACL write path uses to refuse demotions (LUM-2505): a total,
15
+ * conservative ranking over every real `verified_via` value, so a
16
+ * lower-strength source can never silently overwrite a higher-strength
17
+ * active binding.
18
+ *
19
+ * This lives in the shared package — not `assistant/` — because the gateway
20
+ * owns the contact ACL and is where the demotion guard runs. The assistant
21
+ * re-exports the display helpers from `runtime/introduction-policy.ts`, so its
22
+ * card/provenance call sites are unchanged.
23
+ *
24
+ * No import from `assistant/` or `gateway/`: pure vocabulary.
25
+ */
26
+
27
+ // ---------------------------------------------------------------------------
28
+ // verified_via constants
29
+ // ---------------------------------------------------------------------------
30
+
31
+ /** `verified_via` written by the gateway when a verification code is redeemed. */
32
+ export const VERIFIED_VIA_CHALLENGE = "challenge";
33
+
34
+ /** `verified_via` written when the guardian direct-trusts a workspace-vouched identity. */
35
+ export const VERIFIED_VIA_MANUAL = "manual";
36
+
37
+ /** `verified_via` written when the guardian trusts an identity the platform is not vouching for. */
38
+ export const VERIFIED_VIA_CHANNEL_CLAIM = "manual_channel_claim";
39
+
40
+ // ---------------------------------------------------------------------------
41
+ // Display ladder (introduction card)
42
+ // ---------------------------------------------------------------------------
43
+
44
+ /**
45
+ * Binding-strength ladder the introduction card renders:
46
+ *
47
+ * verified_handshake > internal_workspace_match > inbound_channel_claim
48
+ *
49
+ * - `verified_handshake` — the contact proved control of the channel by
50
+ * returning a verification code (`verified_via: "challenge"`).
51
+ * - `internal_workspace_match` — the platform authenticated the identity inside
52
+ * the guardian's own workspace; the guardian vouches directly
53
+ * (`verified_via: "manual"`).
54
+ * - `inbound_channel_claim` — the guardian trusts an identity the platform is
55
+ * NOT vouching for (`verified_via: "manual_channel_claim"`).
56
+ */
57
+ export type BindingStrength =
58
+ | "verified_handshake"
59
+ | "internal_workspace_match"
60
+ | "inbound_channel_claim";
61
+
62
+ /**
63
+ * Derive the display binding strength for a `verified_via`. Returns `null` for
64
+ * provenance outside the introduction ladder (e.g. `invite`, `bootstrap`),
65
+ * which predate it and keep their own audit meaning — the introduction card UI
66
+ * relies on this exact shape.
67
+ *
68
+ * This is the DISPLAY ladder, not the enforcement order: use
69
+ * {@link knownStrengthRank} / {@link isBindingDemotion} for the demotion guard.
70
+ */
71
+ export function bindingStrengthForVerifiedVia(
72
+ verifiedVia: string | null | undefined,
73
+ ): BindingStrength | null {
74
+ switch (verifiedVia) {
75
+ case VERIFIED_VIA_CHALLENGE:
76
+ return "verified_handshake";
77
+ case VERIFIED_VIA_MANUAL:
78
+ return "internal_workspace_match";
79
+ case VERIFIED_VIA_CHANNEL_CLAIM:
80
+ return "inbound_channel_claim";
81
+ default:
82
+ return null;
83
+ }
84
+ }
85
+
86
+ // ---------------------------------------------------------------------------
87
+ // Enforcement order (demotion guard — LUM-2505)
88
+ // ---------------------------------------------------------------------------
89
+
90
+ /**
91
+ * `verified_via` values where the actor demonstrably controls the channel (a
92
+ * code/token/possession proof) or the binding is a guardian binding (bootstrap
93
+ * or platform/webhook auto-registration). They share the top enforcement rank:
94
+ * none may be silently demoted, and a lateral swap between them is not a
95
+ * demotion. Every real production writer of `verified_via` that outranks
96
+ * `manual` must appear here, or the demotion guard would treat it as unknown
97
+ * (incomparable) and let a `manual` re-attest quietly demote it.
98
+ */
99
+ const PROVEN_VERIFIED_VIA: ReadonlySet<string> = new Set([
100
+ VERIFIED_VIA_CHALLENGE, // returned a verification code
101
+ "invite", // redeemed an invite code / token
102
+ "voice", // returned a voice (DTMF) code
103
+ "bootstrap", // guardian binding (also protected by the guardian-downgrade guard)
104
+ "platform_auto_register", // guardian binding auto-registered by the platform
105
+ "webhook_registration", // guardian binding registered via a provider webhook
106
+ ]);
107
+
108
+ /**
109
+ * Total, conservative strength rank for a `verified_via`, used by the gateway
110
+ * ACL write path to refuse demotions. Higher = stronger. Grouped into coarse
111
+ * tiers that hold under ANY intra-tier ordering the pending Slack-permissions
112
+ * PRD (D4) may land on:
113
+ *
114
+ * 3 proven / possession — {@link PROVEN_VERIFIED_VIA}.
115
+ * 2 workspace-vouched — the platform authenticated the identity but the
116
+ * actor did not actively prove control here (`manual`).
117
+ * 1 inbound channel claim — nobody vouches; the guardian trusted an
118
+ * unproven claim (`manual_channel_claim`).
119
+ * 0 unverified — no verification at all (`null` / empty).
120
+ *
121
+ * Returns `null` for an unrecognized non-empty provenance ("unknown"). The
122
+ * demotion guard treats unknown as incomparable and never refuses on it, so a
123
+ * forward-compatible new `verified_via` value is never mistaken for a demotion.
124
+ */
125
+ export function knownStrengthRank(
126
+ verifiedVia: string | null | undefined,
127
+ ): number | null {
128
+ if (verifiedVia == null || verifiedVia === "") return 0;
129
+ if (PROVEN_VERIFIED_VIA.has(verifiedVia)) return 3;
130
+ if (verifiedVia === VERIFIED_VIA_MANUAL) return 2;
131
+ if (verifiedVia === VERIFIED_VIA_CHANNEL_CLAIM) return 1;
132
+ return null;
133
+ }
134
+
135
+ /**
136
+ * Whether writing `incomingVia` over an existing `existingVia` binding would
137
+ * LOWER its strength — the demotion the gateway ACL write path refuses
138
+ * (LUM-2505).
139
+ *
140
+ * True only when BOTH provenances are known and the incoming rank is strictly
141
+ * lower than the existing. Unknown provenance on either side is incomparable
142
+ * and never counts as a demotion (fail-open), so the guard blocks only provable
143
+ * downgrades and never breaks a new/unrecognized `verified_via`. Equal ranks
144
+ * (including lateral swaps within the proven tier) and upgrades are not
145
+ * demotions.
146
+ */
147
+ export function isBindingDemotion(
148
+ existingVia: string | null | undefined,
149
+ incomingVia: string | null | undefined,
150
+ ): boolean {
151
+ const existing = knownStrengthRank(existingVia);
152
+ const incoming = knownStrengthRank(incomingVia);
153
+ if (existing == null || incoming == null) return false;
154
+ return incoming < existing;
155
+ }
@@ -321,3 +321,17 @@ export type {
321
321
  ResolvedChannelPermission,
322
322
  RiskThreshold,
323
323
  } from "./channel-permission-contract.js";
324
+
325
+ // Binding-strength vocabulary for the contact `verified_via` provenance —
326
+ // display ladder (introduction card) + the enforcement order the gateway ACL
327
+ // write path uses to refuse demotions (LUM-2505).
328
+ export {
329
+ bindingStrengthForVerifiedVia,
330
+ isBindingDemotion,
331
+ knownStrengthRank,
332
+ VERIFIED_VIA_CHALLENGE,
333
+ VERIFIED_VIA_CHANNEL_CLAIM,
334
+ VERIFIED_VIA_MANUAL,
335
+ } from "./binding-strength-contract.js";
336
+
337
+ export type { BindingStrength } from "./binding-strength-contract.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vellumai/assistant",
3
- "version": "0.10.11-dev.202607221933.baa2119",
3
+ "version": "0.10.11-dev.202607222129.31c06e5",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "exports": {
@@ -0,0 +1,208 @@
1
+ import { beforeEach, describe, expect, test } from "bun:test";
2
+
3
+ import { getDb } from "../persistence/db-connection.js";
4
+ import { initializeDb } from "../persistence/db-init.js";
5
+ import { recordInbound } from "../persistence/delivery-crud.js";
6
+ import { upsertBinding } from "../persistence/external-conversation-store.js";
7
+ import { resolveApprovalSourceReference } from "../runtime/approval-source-link.js";
8
+
9
+ await initializeDb();
10
+
11
+ function resetTables() {
12
+ const db = getDb();
13
+ db.run("DELETE FROM channel_inbound_events");
14
+ db.run("DELETE FROM conversation_keys");
15
+ db.run("DELETE FROM external_conversation_bindings");
16
+ db.run("DELETE FROM conversations");
17
+ }
18
+
19
+ describe("resolveApprovalSourceReference", () => {
20
+ beforeEach(resetTables);
21
+
22
+ test("returns null for channels without a registered resolver", () => {
23
+ const result = recordInbound("telegram", "12345", "evt-1", {
24
+ sourceMessageId: "678",
25
+ });
26
+ expect(
27
+ resolveApprovalSourceReference("telegram", result.conversationId),
28
+ ).toBeNull();
29
+ });
30
+
31
+ test("returns null for a conversation with no Slack provenance", () => {
32
+ expect(resolveApprovalSourceReference("slack", "conv-none")).toBeNull();
33
+ });
34
+
35
+ test("resolves chat id and message permalink from the latest inbound event (DM)", () => {
36
+ const result = recordInbound("slack", "D01XYZ", "evt-1", {
37
+ sourceMessageId: "1700000000.000100",
38
+ });
39
+
40
+ expect(
41
+ resolveApprovalSourceReference("slack", result.conversationId),
42
+ ).toEqual({
43
+ sourceChatId: "D01XYZ",
44
+ sourceLink: {
45
+ webUrl: "https://slack.com/archives/D01XYZ/p1700000000000100",
46
+ },
47
+ });
48
+ });
49
+
50
+ test("uses the most recent inbound event when several exist", () => {
51
+ const first = recordInbound("slack", "D01XYZ", "evt-1", {
52
+ sourceMessageId: "1699999900.000001",
53
+ });
54
+ recordInbound("slack", "D01XYZ", "evt-2", {
55
+ sourceMessageId: "1700000000.000100",
56
+ });
57
+
58
+ expect(
59
+ resolveApprovalSourceReference("slack", first.conversationId),
60
+ ).toEqual({
61
+ sourceChatId: "D01XYZ",
62
+ sourceLink: {
63
+ webUrl: "https://slack.com/archives/D01XYZ/p1700000000000100",
64
+ },
65
+ });
66
+ });
67
+
68
+ test("threaded conversations link the message inside its thread", () => {
69
+ const result = recordInbound("slack", "C01ABC", "evt-1", {
70
+ sourceMessageId: "1700000001.000200",
71
+ sourceThreadId: "1700000000.000100",
72
+ });
73
+ upsertBinding({
74
+ conversationId: result.conversationId,
75
+ sourceChannel: "slack",
76
+ externalChatId: "C01ABC",
77
+ externalThreadId: "1700000000.000100",
78
+ });
79
+
80
+ expect(
81
+ resolveApprovalSourceReference("slack", result.conversationId),
82
+ ).toEqual({
83
+ sourceChatId: "C01ABC",
84
+ sourceLink: {
85
+ webUrl:
86
+ "https://slack.com/archives/C01ABC/p1700000001000200?thread_ts=1700000000.000100&cid=C01ABC",
87
+ },
88
+ });
89
+ });
90
+
91
+ test("a thread-root message carries no thread params", () => {
92
+ const result = recordInbound("slack", "C01ABC", "evt-1", {
93
+ sourceMessageId: "1700000000.000100",
94
+ sourceThreadId: "1700000000.000100",
95
+ });
96
+ upsertBinding({
97
+ conversationId: result.conversationId,
98
+ sourceChannel: "slack",
99
+ externalChatId: "C01ABC",
100
+ externalThreadId: "1700000000.000100",
101
+ });
102
+
103
+ expect(
104
+ resolveApprovalSourceReference("slack", result.conversationId),
105
+ ).toEqual({
106
+ sourceChatId: "C01ABC",
107
+ sourceLink: {
108
+ webUrl: "https://slack.com/archives/C01ABC/p1700000000000100",
109
+ },
110
+ });
111
+ });
112
+
113
+ test("falls back to a ts-shaped externalMessageId when sourceMessageId is absent", () => {
114
+ const result = recordInbound("slack", "D01XYZ", "1700000000.000100");
115
+
116
+ expect(
117
+ resolveApprovalSourceReference("slack", result.conversationId),
118
+ ).toEqual({
119
+ sourceChatId: "D01XYZ",
120
+ sourceLink: {
121
+ webUrl: "https://slack.com/archives/D01XYZ/p1700000000000100",
122
+ },
123
+ });
124
+ });
125
+
126
+ test("omits the link when no id is ts-shaped", () => {
127
+ const result = recordInbound("slack", "D01XYZ", "evt-opaque");
128
+
129
+ expect(
130
+ resolveApprovalSourceReference("slack", result.conversationId),
131
+ ).toEqual({
132
+ sourceChatId: "D01XYZ",
133
+ });
134
+ });
135
+
136
+ test("an ingress-stamped hint identifies the exact message, immune to later inbound rows", () => {
137
+ // A second message lands while the first message's turn is still
138
+ // escalating — the hint pins the card to the actual triggering message.
139
+ const result = recordInbound("slack", "D01XYZ", "evt-1", {
140
+ sourceMessageId: "1699999900.000001",
141
+ });
142
+ recordInbound("slack", "D01XYZ", "evt-2", {
143
+ sourceMessageId: "1700000000.000100",
144
+ });
145
+
146
+ expect(
147
+ resolveApprovalSourceReference("slack", result.conversationId, {
148
+ requesterChatId: "D01XYZ",
149
+ sourceMessageId: "1699999900.000001",
150
+ }),
151
+ ).toEqual({
152
+ sourceChatId: "D01XYZ",
153
+ sourceLink: {
154
+ webUrl: "https://slack.com/archives/D01XYZ/p1699999900000001",
155
+ },
156
+ });
157
+ });
158
+
159
+ test("a threaded hint links the message inside its thread without a DB row", () => {
160
+ expect(
161
+ resolveApprovalSourceReference("slack", "conv-unpersisted", {
162
+ requesterChatId: "C01ABC",
163
+ sourceMessageId: "1700000001.000200",
164
+ sourceThreadId: "1700000000.000100",
165
+ }),
166
+ ).toEqual({
167
+ sourceChatId: "C01ABC",
168
+ sourceLink: {
169
+ webUrl:
170
+ "https://slack.com/archives/C01ABC/p1700000001000200?thread_ts=1700000000.000100&cid=C01ABC",
171
+ },
172
+ });
173
+ });
174
+
175
+ test("a hint without a ts-shaped message id falls back to persisted reconstruction", () => {
176
+ const result = recordInbound("slack", "D01XYZ", "evt-1", {
177
+ sourceMessageId: "1700000000.000100",
178
+ });
179
+
180
+ expect(
181
+ resolveApprovalSourceReference("slack", result.conversationId, {
182
+ requesterChatId: "D01XYZ",
183
+ sourceMessageId: "not-a-ts",
184
+ }),
185
+ ).toEqual({
186
+ sourceChatId: "D01XYZ",
187
+ sourceLink: {
188
+ webUrl: "https://slack.com/archives/D01XYZ/p1700000000000100",
189
+ },
190
+ });
191
+ });
192
+
193
+ test("ignores bindings from other channels", () => {
194
+ const result = recordInbound("slack", "D01XYZ", "evt-opaque");
195
+ upsertBinding({
196
+ conversationId: result.conversationId,
197
+ sourceChannel: "telegram",
198
+ externalChatId: "12345",
199
+ externalThreadId: "1700000000.000100",
200
+ });
201
+
202
+ expect(
203
+ resolveApprovalSourceReference("slack", result.conversationId),
204
+ ).toEqual({
205
+ sourceChatId: "D01XYZ",
206
+ });
207
+ });
208
+ });
@@ -463,10 +463,18 @@ describe("POST /v1/btw", () => {
463
463
  await readStream(result as ReadableStream<Uint8Array>);
464
464
 
465
465
  expect(mockGetConversationByKey).toHaveBeenCalledWith("greeting-abc123");
466
- expect(mockGetOrCreateConversation).toHaveBeenCalledWith("greeting-abc123");
466
+ // An unmapped key has no real conversation, so the side-chain runs against
467
+ // an ephemeral in-memory conversation with no persisted (sidebar-visible)
468
+ // row.
469
+ expect(mockGetOrCreateConversation).toHaveBeenCalledWith(
470
+ "greeting-abc123",
471
+ {
472
+ ephemeral: true,
473
+ },
474
+ );
467
475
  });
468
476
 
469
- test("known conversationKey resolves to existing conversation ID", async () => {
477
+ test("known conversationKey resolves to existing conversation ID without the ephemeral flag", async () => {
470
478
  mockGetConversationByKey.mockReturnValueOnce({
471
479
  conversationId: "existing-conv-id",
472
480
  });
@@ -479,8 +487,11 @@ describe("POST /v1/btw", () => {
479
487
  });
480
488
  await readStream(result as ReadableStream<Uint8Array>);
481
489
 
490
+ // A mapped key targets a real conversation, so its persisted row is reused
491
+ // — no ephemeral options.
482
492
  expect(mockGetOrCreateConversation).toHaveBeenCalledWith(
483
493
  "existing-conv-id",
494
+ undefined,
484
495
  );
485
496
  });
486
497
  });