@vellumai/assistant 0.12.2-dev.202609182015.188de8a → 0.12.2-dev.202609182114.335fa56
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.
- package/ARCHITECTURE.md +8 -6
- package/docs/architecture/turn-actor.md +2 -0
- package/package.json +1 -1
- package/src/__tests__/assistant-event-targeting.test.ts +186 -0
- package/src/__tests__/assistant-stream-state.test.ts +7 -9
- package/src/__tests__/channel-retry-sweep.test.ts +9 -0
- package/src/__tests__/dm-backfill.test.ts +83 -1
- package/src/__tests__/helpers/verification-sessions-ipc-sim.ts +5 -0
- package/src/__tests__/message-provenance-contact-id.test.ts +69 -0
- package/src/__tests__/process-message-display-content.test.ts +56 -0
- package/src/__tests__/reaction-persistence.test.ts +5 -0
- package/src/__tests__/runtime-events-sse-reconnect.test.ts +1 -1
- package/src/__tests__/scripted-turn-metadata-persistence.test.ts +73 -0
- package/src/__tests__/thread-backfill.test.ts +53 -0
- package/src/approvals/AGENTS.md +1 -1
- package/src/cli/commands/roadmap.help.ts +2 -3
- package/src/daemon/conversation-messaging.ts +19 -1
- package/src/daemon/message-provenance.ts +23 -0
- package/src/daemon/process-message.ts +13 -4
- package/src/persistence/conversation-crud.ts +8 -0
- package/src/plugins/defaults/memory/__tests__/archive-removal-framing.test.ts +64 -0
- package/src/plugins/defaults/memory/substrate/prompts/consolidation.ts +2 -2
- package/src/plugins/defaults/memory/tools.test.ts +24 -1
- package/src/plugins/defaults/memory/tools.ts +12 -1
- package/src/runtime/assistant-event-hub.ts +16 -86
- package/src/runtime/assistant-event-targeting.ts +79 -0
- package/src/runtime/assistant-stream-state.ts +13 -112
- package/src/runtime/channel-retry-sweep.ts +1 -0
- package/src/runtime/http-types.ts +6 -0
- package/src/runtime/routes/events-routes.ts +2 -2
- package/src/runtime/routes/inbound-message-handler.ts +35 -38
- package/src/runtime/routes/inbound-stages/backfill-sender-provenance.test.ts +165 -0
- package/src/runtime/routes/inbound-stages/backfill-sender-provenance.ts +92 -0
- package/src/runtime/routes/inbound-stages/background-dispatch.test.ts +29 -0
- package/src/runtime/routes/inbound-stages/background-dispatch.ts +1 -0
- package/src/runtime/routes/inbound-stages/reaction-intercept.ts +2 -0
- package/src/util/canonicalize-identity.test.ts +21 -0
- package/src/util/canonicalize-identity.ts +16 -0
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Who receives a targeted assistant event. Live fanout
|
|
3
|
+
* (`AssistantEventHub.publish`) and replay (`getReplayWindow`, reached from
|
|
4
|
+
* the SSE reconnect and the `events/tail` route) both decide delivery with
|
|
5
|
+
* {@link matchesTargeting}, so a targeting rule added here reaches every
|
|
6
|
+
* path at once.
|
|
7
|
+
*
|
|
8
|
+
* Conversation scoping is deliberately not part of this check. Live fanout
|
|
9
|
+
* applies a subscriber's conversation filter only to events that do not name
|
|
10
|
+
* a client; replay scopes to the conversation its caller asked for. Each path
|
|
11
|
+
* applies its own scoping after this check.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import type { HostProxyCapability, InterfaceId } from "../channels/types.js";
|
|
15
|
+
import type { AssistantEventPublishOptions } from "./assistant-event-publish-options.js";
|
|
16
|
+
|
|
17
|
+
/** The connection an event is being delivered to, live or on replay. */
|
|
18
|
+
export interface SubscriberIdentity {
|
|
19
|
+
type: "client" | "process";
|
|
20
|
+
clientId?: string;
|
|
21
|
+
interfaceId?: InterfaceId;
|
|
22
|
+
capabilities?: readonly HostProxyCapability[];
|
|
23
|
+
actorPrincipalId?: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Whether `subscriber` receives an event carrying `targeting`. An event with
|
|
28
|
+
* no targeting reaches every subscriber. Every rule a targeted event sets must
|
|
29
|
+
* hold, and a process subscriber never matches a rule that names a client
|
|
30
|
+
* property.
|
|
31
|
+
*/
|
|
32
|
+
export function matchesTargeting(
|
|
33
|
+
targeting: AssistantEventPublishOptions | undefined,
|
|
34
|
+
subscriber: SubscriberIdentity,
|
|
35
|
+
): boolean {
|
|
36
|
+
if (!targeting) {
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
const isClient = subscriber.type === "client";
|
|
40
|
+
|
|
41
|
+
// Self-echo suppression: the client that originated a mutation does not
|
|
42
|
+
// receive its own invalidation back.
|
|
43
|
+
if (
|
|
44
|
+
targeting.excludeClientId != null &&
|
|
45
|
+
isClient &&
|
|
46
|
+
subscriber.clientId === targeting.excludeClientId
|
|
47
|
+
) {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
if (
|
|
51
|
+
targeting.targetInterfaceId != null &&
|
|
52
|
+
(!isClient || subscriber.interfaceId !== targeting.targetInterfaceId)
|
|
53
|
+
) {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
// An event scoped to one person reaches only that person's connections; a
|
|
57
|
+
// connection with no verified principal never matches.
|
|
58
|
+
if (
|
|
59
|
+
targeting.targetActorPrincipalId != null &&
|
|
60
|
+
(!isClient ||
|
|
61
|
+
subscriber.actorPrincipalId !== targeting.targetActorPrincipalId)
|
|
62
|
+
) {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
if (
|
|
66
|
+
targeting.targetClientId != null &&
|
|
67
|
+
(!isClient || subscriber.clientId !== targeting.targetClientId)
|
|
68
|
+
) {
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
if (
|
|
72
|
+
targeting.targetCapability != null &&
|
|
73
|
+
(!isClient ||
|
|
74
|
+
!subscriber.capabilities?.includes(targeting.targetCapability))
|
|
75
|
+
) {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
@@ -70,6 +70,11 @@ import {
|
|
|
70
70
|
import type { AssistantEventEnvelope } from "../api/index.js";
|
|
71
71
|
import { getLogger } from "../util/logger.js";
|
|
72
72
|
import { getWorkspaceDir } from "../util/platform.js";
|
|
73
|
+
import type { AssistantEventPublishOptions } from "./assistant-event-publish-options.js";
|
|
74
|
+
import {
|
|
75
|
+
matchesTargeting,
|
|
76
|
+
type SubscriberIdentity,
|
|
77
|
+
} from "./assistant-event-targeting.js";
|
|
73
78
|
|
|
74
79
|
const log = getLogger("assistant-stream-state");
|
|
75
80
|
|
|
@@ -93,41 +98,13 @@ const SEQ_RESERVATION_BLOCK = 1024;
|
|
|
93
98
|
|
|
94
99
|
// ── Types ────────────────────────────────────────────────────────────
|
|
95
100
|
|
|
96
|
-
/**
|
|
97
|
-
* Targeting / exclusion modifiers attached to an event at publish time.
|
|
98
|
-
* Stored on ring entries so replay can re-apply the same delivery
|
|
99
|
-
* filter that the live `publish()` path used.
|
|
100
|
-
*
|
|
101
|
-
* Fields use plain `string` rather than branded channel types so
|
|
102
|
-
* this module stays independent of the `channels/` package.
|
|
103
|
-
*/
|
|
104
|
-
export interface EventTargeting {
|
|
105
|
-
targetCapability?: string;
|
|
106
|
-
targetClientId?: string;
|
|
107
|
-
targetInterfaceId?: string;
|
|
108
|
-
targetActorPrincipalId?: string;
|
|
109
|
-
excludeClientId?: string;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* Identity of the subscriber requesting a replay window. Replay
|
|
114
|
-
* filtering mirrors the live `publish()` logic in `AssistantEventHub`:
|
|
115
|
-
* targeted entries are only delivered when the subscriber matches.
|
|
116
|
-
*/
|
|
117
|
-
export interface ReplaySubscriber {
|
|
118
|
-
type: "client" | "process";
|
|
119
|
-
clientId?: string;
|
|
120
|
-
interfaceId?: string;
|
|
121
|
-
capabilities?: readonly string[];
|
|
122
|
-
actorPrincipalId?: string;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
101
|
interface RingEntry {
|
|
126
102
|
seq: number;
|
|
127
103
|
event: AssistantEventEnvelope;
|
|
128
104
|
emittedAt: number;
|
|
129
105
|
sizeBytes: number;
|
|
130
|
-
|
|
106
|
+
/** The publish options the event went out with, re-applied on replay. */
|
|
107
|
+
targeting?: AssistantEventPublishOptions;
|
|
131
108
|
}
|
|
132
109
|
|
|
133
110
|
interface AssistantStreamState {
|
|
@@ -217,7 +194,7 @@ export function isStreamSeqStampingDisabled(): boolean {
|
|
|
217
194
|
*/
|
|
218
195
|
export function stampAndBuffer(
|
|
219
196
|
event: AssistantEventEnvelope,
|
|
220
|
-
options?: { targeting?:
|
|
197
|
+
options?: { targeting?: AssistantEventPublishOptions },
|
|
221
198
|
): void {
|
|
222
199
|
if (stampingDisabled) {
|
|
223
200
|
return;
|
|
@@ -274,9 +251,9 @@ export function stampAndBuffer(
|
|
|
274
251
|
* buffered entry -- callers should fall back to a snapshot resync.
|
|
275
252
|
*
|
|
276
253
|
* When `subscriber` is provided, entries carrying targeting metadata
|
|
277
|
-
* are filtered
|
|
278
|
-
* `AssistantEventHub
|
|
279
|
-
* outside their intended delivery set on reconnect.
|
|
254
|
+
* are filtered with {@link matchesTargeting}, the check the live
|
|
255
|
+
* `publish()` path in `AssistantEventHub` applies, so targeted events do
|
|
256
|
+
* not leak to subscribers outside their intended delivery set on reconnect.
|
|
280
257
|
*
|
|
281
258
|
* When `conversationId` is provided, only that conversation's events are
|
|
282
259
|
* returned -- a conversation-scoped subscription only delivers its own
|
|
@@ -289,7 +266,7 @@ export function stampAndBuffer(
|
|
|
289
266
|
*/
|
|
290
267
|
export function getReplayWindow(
|
|
291
268
|
lastSeenSeq: number,
|
|
292
|
-
subscriber?:
|
|
269
|
+
subscriber?: SubscriberIdentity,
|
|
293
270
|
conversationId?: string,
|
|
294
271
|
): readonly AssistantEventEnvelope[] | null {
|
|
295
272
|
evict();
|
|
@@ -317,7 +294,7 @@ export function getReplayWindow(
|
|
|
317
294
|
entry.seq > lastSeenSeq &&
|
|
318
295
|
(conversationId == null ||
|
|
319
296
|
entry.event.conversationId === conversationId) &&
|
|
320
|
-
(subscriber == null ||
|
|
297
|
+
(subscriber == null || matchesTargeting(entry.targeting, subscriber)),
|
|
321
298
|
)
|
|
322
299
|
.map((entry) => entry.event);
|
|
323
300
|
}
|
|
@@ -520,82 +497,6 @@ function readReservedCeiling(): number {
|
|
|
520
497
|
return 0;
|
|
521
498
|
}
|
|
522
499
|
|
|
523
|
-
/**
|
|
524
|
-
* Mirrors the delivery logic in `AssistantEventHub.publish()`. Returns
|
|
525
|
-
* `true` when `subscriber` would have received the entry during live
|
|
526
|
-
* fanout.
|
|
527
|
-
*/
|
|
528
|
-
function matchesSubscriber(
|
|
529
|
-
entry: RingEntry,
|
|
530
|
-
subscriber: ReplaySubscriber,
|
|
531
|
-
): boolean {
|
|
532
|
-
const t = entry.targeting;
|
|
533
|
-
if (!t) {
|
|
534
|
-
return true;
|
|
535
|
-
}
|
|
536
|
-
|
|
537
|
-
// Self-echo suppression: the originating client never receives the
|
|
538
|
-
// event back.
|
|
539
|
-
if (
|
|
540
|
-
t.excludeClientId != null &&
|
|
541
|
-
subscriber.type === "client" &&
|
|
542
|
-
subscriber.clientId === t.excludeClientId
|
|
543
|
-
) {
|
|
544
|
-
return false;
|
|
545
|
-
}
|
|
546
|
-
|
|
547
|
-
// Interface targeting: only clients of the requested interface.
|
|
548
|
-
if (t.targetInterfaceId != null) {
|
|
549
|
-
if (
|
|
550
|
-
subscriber.type !== "client" ||
|
|
551
|
-
subscriber.interfaceId !== t.targetInterfaceId
|
|
552
|
-
) {
|
|
553
|
-
return false;
|
|
554
|
-
}
|
|
555
|
-
}
|
|
556
|
-
|
|
557
|
-
// Principal targeting: only the named person's own connections.
|
|
558
|
-
if (t.targetActorPrincipalId != null) {
|
|
559
|
-
if (
|
|
560
|
-
subscriber.type !== "client" ||
|
|
561
|
-
subscriber.actorPrincipalId !== t.targetActorPrincipalId
|
|
562
|
-
) {
|
|
563
|
-
return false;
|
|
564
|
-
}
|
|
565
|
-
}
|
|
566
|
-
|
|
567
|
-
if (t.targetClientId != null) {
|
|
568
|
-
// Client targeting: bypass conversation filter, deliver only to the
|
|
569
|
-
// named client.
|
|
570
|
-
if (
|
|
571
|
-
subscriber.type !== "client" ||
|
|
572
|
-
subscriber.clientId !== t.targetClientId
|
|
573
|
-
) {
|
|
574
|
-
return false;
|
|
575
|
-
}
|
|
576
|
-
if (
|
|
577
|
-
t.targetCapability != null &&
|
|
578
|
-
!subscriber.capabilities?.includes(t.targetCapability)
|
|
579
|
-
) {
|
|
580
|
-
return false;
|
|
581
|
-
}
|
|
582
|
-
return true;
|
|
583
|
-
}
|
|
584
|
-
|
|
585
|
-
// Capability targeting (without client targeting): only subscribers
|
|
586
|
-
// that declare the required capability.
|
|
587
|
-
if (t.targetCapability != null) {
|
|
588
|
-
if (
|
|
589
|
-
subscriber.type !== "client" ||
|
|
590
|
-
!subscriber.capabilities?.includes(t.targetCapability)
|
|
591
|
-
) {
|
|
592
|
-
return false;
|
|
593
|
-
}
|
|
594
|
-
}
|
|
595
|
-
|
|
596
|
-
return true;
|
|
597
|
-
}
|
|
598
|
-
|
|
599
500
|
function evict(): void {
|
|
600
501
|
const now = Date.now();
|
|
601
502
|
while (state.ring.length > 0) {
|
|
@@ -70,6 +70,12 @@ export interface RuntimeMessageConversationOptions {
|
|
|
70
70
|
};
|
|
71
71
|
assistantId?: string;
|
|
72
72
|
trustContext?: TrustContext;
|
|
73
|
+
/**
|
|
74
|
+
* The person whose own inbound message this turn persists. Channel ingress
|
|
75
|
+
* and its retry replay pass the sender's trust; it names the persisted user
|
|
76
|
+
* row's author (`PersistMessageOptions.author`).
|
|
77
|
+
*/
|
|
78
|
+
author?: TrustContext;
|
|
73
79
|
/**
|
|
74
80
|
* Whether this turn should permit interactive approval prompts.
|
|
75
81
|
* Channel ingress sets this true so confirmations can be resolved
|
|
@@ -38,7 +38,7 @@ import {
|
|
|
38
38
|
AssistantEventHub,
|
|
39
39
|
assistantEventHub,
|
|
40
40
|
} from "../assistant-event-hub.js";
|
|
41
|
-
import type {
|
|
41
|
+
import type { SubscriberIdentity } from "../assistant-event-targeting.js";
|
|
42
42
|
import { getReplayWindow } from "../assistant-stream-state.js";
|
|
43
43
|
import { ACTOR_PRINCIPALS, GATEWAY_PRINCIPALS } from "../auth/route-policy.js";
|
|
44
44
|
import { DEFAULT_HEARTBEAT_INTERVAL_MS } from "../client-health.js";
|
|
@@ -600,7 +600,7 @@ function replaySubscriberFor(
|
|
|
600
600
|
clientId: string | null,
|
|
601
601
|
interfaceId: InterfaceId | null,
|
|
602
602
|
actorPrincipalId: string | undefined,
|
|
603
|
-
):
|
|
603
|
+
): SubscriberIdentity {
|
|
604
604
|
if (!clientId || !interfaceId) {
|
|
605
605
|
return { type: "process" };
|
|
606
606
|
}
|
|
@@ -106,7 +106,10 @@ import { markProcessed } from "../../persistence/delivery-status.js";
|
|
|
106
106
|
import { upsertBinding } from "../../persistence/external-conversation-store.js";
|
|
107
107
|
import type { ContentBlock } from "../../providers/types.js";
|
|
108
108
|
import { checkIngressForSecrets } from "../../security/secret-ingress.js";
|
|
109
|
-
import {
|
|
109
|
+
import {
|
|
110
|
+
canonicalizeInboundIdentity,
|
|
111
|
+
inboundIdentitiesMatch,
|
|
112
|
+
} from "../../util/canonicalize-identity.js";
|
|
110
113
|
import { safeParseRecord } from "../../util/json.js";
|
|
111
114
|
import { getLogger } from "../../util/logger.js";
|
|
112
115
|
import { truncate } from "../../util/truncate.js";
|
|
@@ -126,6 +129,11 @@ import {
|
|
|
126
129
|
enforceIngressAcl,
|
|
127
130
|
} from "./inbound-stages/acl-enforcement.js";
|
|
128
131
|
import { enforceAdmissionPolicy } from "./inbound-stages/admission-policy.js";
|
|
132
|
+
import {
|
|
133
|
+
type BackfilledSenderProvenance,
|
|
134
|
+
type BackfilledSenderProvenanceResolver,
|
|
135
|
+
createBackfilledSenderProvenanceResolver,
|
|
136
|
+
} from "./inbound-stages/backfill-sender-provenance.js";
|
|
129
137
|
import { processChannelMessageInBackground } from "./inbound-stages/background-dispatch.js";
|
|
130
138
|
import { handleBootstrapIntercept } from "./inbound-stages/bootstrap-intercept.js";
|
|
131
139
|
import { handleEditIntercept } from "./inbound-stages/edit-intercept.js";
|
|
@@ -1835,6 +1843,7 @@ async function persistBackfilledSlackMessage(params: {
|
|
|
1835
1843
|
message: ProviderMessage;
|
|
1836
1844
|
account?: string;
|
|
1837
1845
|
guardianExternalUserId?: string;
|
|
1846
|
+
resolveSenderProvenance: BackfilledSenderProvenanceResolver;
|
|
1838
1847
|
}): Promise<boolean> {
|
|
1839
1848
|
const { message } = params;
|
|
1840
1849
|
|
|
@@ -1872,10 +1881,19 @@ async function persistBackfilledSlackMessage(params: {
|
|
|
1872
1881
|
message.metadata,
|
|
1873
1882
|
"actorTimezoneLabel",
|
|
1874
1883
|
);
|
|
1875
|
-
const
|
|
1884
|
+
const role = (await isBackfilledSlackAssistantMessage(
|
|
1876
1885
|
message,
|
|
1877
|
-
params.
|
|
1878
|
-
)
|
|
1886
|
+
params.account,
|
|
1887
|
+
))
|
|
1888
|
+
? "assistant"
|
|
1889
|
+
: "user";
|
|
1890
|
+
// The assistant's own posts are replayed as assistant history, not as any
|
|
1891
|
+
// person's words, so only a person's row asks who its sender is.
|
|
1892
|
+
const provenance: BackfilledSenderProvenance =
|
|
1893
|
+
role === "user"
|
|
1894
|
+
? await params.resolveSenderProvenance(actorExternalUserId)
|
|
1895
|
+
: { provenanceTrustClass: "unknown" };
|
|
1896
|
+
const isGuardian = provenance.provenanceTrustClass === "guardian";
|
|
1879
1897
|
const slackTranscriptTimestampTimezone =
|
|
1880
1898
|
resolveSlackTranscriptTimestampTimezone();
|
|
1881
1899
|
const slackTimezoneFields = buildSlackTimezoneMetadata({
|
|
@@ -1899,13 +1917,6 @@ async function persistBackfilledSlackMessage(params: {
|
|
|
1899
1917
|
...(slackFiles.length > 0 ? { slackFiles } : {}),
|
|
1900
1918
|
};
|
|
1901
1919
|
|
|
1902
|
-
const role = (await isBackfilledSlackAssistantMessage(
|
|
1903
|
-
message,
|
|
1904
|
-
params.account,
|
|
1905
|
-
))
|
|
1906
|
-
? "assistant"
|
|
1907
|
-
: "user";
|
|
1908
|
-
|
|
1909
1920
|
const rawText = message.text ?? "";
|
|
1910
1921
|
|
|
1911
1922
|
// `sentAt` is when the message was sent; the row's `createdAt` is when the
|
|
@@ -1944,7 +1955,7 @@ async function persistBackfilledSlackMessage(params: {
|
|
|
1944
1955
|
metadata: {
|
|
1945
1956
|
...envelope,
|
|
1946
1957
|
...(sentAt !== undefined ? { sentAt } : {}),
|
|
1947
|
-
|
|
1958
|
+
...provenance,
|
|
1948
1959
|
provenanceSourceChannel: "slack",
|
|
1949
1960
|
...(params.guardianExternalUserId
|
|
1950
1961
|
? { provenanceGuardianExternalUserId: params.guardianExternalUserId }
|
|
@@ -2063,22 +2074,6 @@ async function buildBackfilledSlackContentBlocks(
|
|
|
2063
2074
|
return blocks;
|
|
2064
2075
|
}
|
|
2065
2076
|
|
|
2066
|
-
function isBackfilledSlackGuardianMessage(
|
|
2067
|
-
message: ProviderMessage,
|
|
2068
|
-
guardianExternalUserId: string | undefined,
|
|
2069
|
-
): boolean {
|
|
2070
|
-
const rawSenderId = message.sender?.id?.trim();
|
|
2071
|
-
if (!rawSenderId || !guardianExternalUserId) {
|
|
2072
|
-
return false;
|
|
2073
|
-
}
|
|
2074
|
-
const normalizedSender =
|
|
2075
|
-
canonicalizeInboundIdentity("slack", rawSenderId) ?? rawSenderId;
|
|
2076
|
-
const normalizedGuardian =
|
|
2077
|
-
canonicalizeInboundIdentity("slack", guardianExternalUserId) ??
|
|
2078
|
-
guardianExternalUserId.trim();
|
|
2079
|
-
return normalizedSender === normalizedGuardian;
|
|
2080
|
-
}
|
|
2081
|
-
|
|
2082
2077
|
const SLACK_ASSISTANT_THREAD_PLACEHOLDER_TEXT = "New Assistant Thread";
|
|
2083
2078
|
|
|
2084
2079
|
async function isSlackAssistantThreadPlaceholder(
|
|
@@ -2115,7 +2110,7 @@ async function isBackfilledSlackAssistantMessage(
|
|
|
2115
2110
|
return false;
|
|
2116
2111
|
}
|
|
2117
2112
|
|
|
2118
|
-
if (rawSenderId &&
|
|
2113
|
+
if (rawSenderId && inboundIdentitiesMatch("slack", rawSenderId, botUserId)) {
|
|
2119
2114
|
return true;
|
|
2120
2115
|
}
|
|
2121
2116
|
|
|
@@ -2131,7 +2126,7 @@ async function isBackfilledSlackAssistantMessage(
|
|
|
2131
2126
|
const resolvedBotUserId = await resolveSlackBotUserId(account, rawBotId);
|
|
2132
2127
|
return (
|
|
2133
2128
|
typeof resolvedBotUserId === "string" &&
|
|
2134
|
-
|
|
2129
|
+
inboundIdentitiesMatch("slack", resolvedBotUserId, botUserId)
|
|
2135
2130
|
);
|
|
2136
2131
|
} catch (err) {
|
|
2137
2132
|
log.warn(
|
|
@@ -2142,14 +2137,6 @@ async function isBackfilledSlackAssistantMessage(
|
|
|
2142
2137
|
}
|
|
2143
2138
|
}
|
|
2144
2139
|
|
|
2145
|
-
function slackIdentityMatches(left: string, right: string): boolean {
|
|
2146
|
-
const canonicalSender =
|
|
2147
|
-
canonicalizeInboundIdentity("slack", left) ?? left.trim();
|
|
2148
|
-
const canonicalBot =
|
|
2149
|
-
canonicalizeInboundIdentity("slack", right) ?? right.trim();
|
|
2150
|
-
return canonicalSender === canonicalBot;
|
|
2151
|
-
}
|
|
2152
|
-
|
|
2153
2140
|
/**
|
|
2154
2141
|
* Transient view of `slackFiles` that preserves the download URLs added by
|
|
2155
2142
|
* `mapSlackFiles` on the in-flight `ProviderMessage`. These URLs never reach
|
|
@@ -2290,6 +2277,10 @@ async function runBackfillSlackDmIfCold(params: {
|
|
|
2290
2277
|
// and a transcript that reads correctly when the renderer joins on
|
|
2291
2278
|
// monotonic createdAt.
|
|
2292
2279
|
const ordered = [...fetched].reverse();
|
|
2280
|
+
const resolveSenderProvenance = createBackfilledSenderProvenanceResolver(
|
|
2281
|
+
params.guardianExternalUserId,
|
|
2282
|
+
ordered,
|
|
2283
|
+
);
|
|
2293
2284
|
for (const message of ordered) {
|
|
2294
2285
|
if (seen.has(message.id)) {
|
|
2295
2286
|
continue;
|
|
@@ -2309,6 +2300,7 @@ async function runBackfillSlackDmIfCold(params: {
|
|
|
2309
2300
|
...(params.guardianExternalUserId
|
|
2310
2301
|
? { guardianExternalUserId: params.guardianExternalUserId }
|
|
2311
2302
|
: {}),
|
|
2303
|
+
resolveSenderProvenance,
|
|
2312
2304
|
});
|
|
2313
2305
|
seen.add(message.id);
|
|
2314
2306
|
if (stored) {
|
|
@@ -2890,6 +2882,10 @@ export async function triggerSlackThreadBackfillIfNeeded(params: {
|
|
|
2890
2882
|
}
|
|
2891
2883
|
|
|
2892
2884
|
let persisted = 0;
|
|
2885
|
+
const resolveSenderProvenance = createBackfilledSenderProvenanceResolver(
|
|
2886
|
+
guardianExternalUserId,
|
|
2887
|
+
fetched,
|
|
2888
|
+
);
|
|
2893
2889
|
for (const message of fetched) {
|
|
2894
2890
|
if (!message.id) {
|
|
2895
2891
|
continue;
|
|
@@ -2907,6 +2903,7 @@ export async function triggerSlackThreadBackfillIfNeeded(params: {
|
|
|
2907
2903
|
message,
|
|
2908
2904
|
...(account ? { account } : {}),
|
|
2909
2905
|
...(guardianExternalUserId ? { guardianExternalUserId } : {}),
|
|
2906
|
+
resolveSenderProvenance,
|
|
2910
2907
|
});
|
|
2911
2908
|
threadState.storedChannelTs.add(message.id);
|
|
2912
2909
|
if (stored) {
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { afterAll, beforeEach, describe, expect, spyOn, test } from "bun:test";
|
|
2
|
+
|
|
3
|
+
import type { TrustVerdict } from "@vellumai/gateway-client";
|
|
4
|
+
|
|
5
|
+
import type { InboundTrustReadResult } from "../../../calls/inbound-trust-reader.js";
|
|
6
|
+
import * as inboundTrustReader from "../../../calls/inbound-trust-reader.js";
|
|
7
|
+
import type { Message as ProviderMessage } from "../../../messaging/provider-types.js";
|
|
8
|
+
import { createBackfilledSenderProvenanceResolver } from "./backfill-sender-provenance.js";
|
|
9
|
+
|
|
10
|
+
const readsBySender = new Map<string, InboundTrustReadResult>();
|
|
11
|
+
const readInboundTrustMock = spyOn(
|
|
12
|
+
inboundTrustReader,
|
|
13
|
+
"readInboundTrust",
|
|
14
|
+
).mockImplementation(
|
|
15
|
+
async (input) =>
|
|
16
|
+
readsBySender.get(input.actorExternalId ?? "") ?? { ok: false },
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
afterAll(() => {
|
|
20
|
+
readInboundTrustMock.mockRestore();
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
const GUARDIAN_ID = "U_GUARDIAN";
|
|
24
|
+
|
|
25
|
+
function memberVerdict(
|
|
26
|
+
trustClass: TrustVerdict["trustClass"],
|
|
27
|
+
contactId: string,
|
|
28
|
+
status: string,
|
|
29
|
+
): TrustVerdict {
|
|
30
|
+
return {
|
|
31
|
+
trustClass,
|
|
32
|
+
canonicalSenderId: `U_${contactId}`,
|
|
33
|
+
contactId,
|
|
34
|
+
channelId: `ch-${contactId}`,
|
|
35
|
+
status,
|
|
36
|
+
policy: "allow",
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function readOf(verdict: TrustVerdict): InboundTrustReadResult {
|
|
41
|
+
return { ok: true, verdict, admissionPolicy: null };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
beforeEach(() => {
|
|
45
|
+
readsBySender.clear();
|
|
46
|
+
readInboundTrustMock.mockClear();
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
describe("createBackfilledSenderProvenanceResolver", () => {
|
|
50
|
+
test("stamps the gateway verdict's trust class and contact id", async () => {
|
|
51
|
+
readsBySender.set(
|
|
52
|
+
"U_ALICE",
|
|
53
|
+
readOf(memberVerdict("trusted_contact", "c-alice", "active")),
|
|
54
|
+
);
|
|
55
|
+
readsBySender.set(
|
|
56
|
+
"U_BOB",
|
|
57
|
+
readOf(memberVerdict("unverified_contact", "c-bob", "unverified")),
|
|
58
|
+
);
|
|
59
|
+
const resolve = createBackfilledSenderProvenanceResolver(GUARDIAN_ID, []);
|
|
60
|
+
|
|
61
|
+
expect(await resolve("U_ALICE")).toEqual({
|
|
62
|
+
provenanceTrustClass: "trusted_contact",
|
|
63
|
+
provenanceContactId: "c-alice",
|
|
64
|
+
});
|
|
65
|
+
expect(await resolve("U_BOB")).toEqual({
|
|
66
|
+
provenanceTrustClass: "unverified_contact",
|
|
67
|
+
provenanceContactId: "c-bob",
|
|
68
|
+
});
|
|
69
|
+
expect(readInboundTrustMock).toHaveBeenCalledWith({
|
|
70
|
+
channelType: "slack",
|
|
71
|
+
actorExternalId: "U_ALICE",
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
test("a stranger verdict carries no contact id", async () => {
|
|
76
|
+
readsBySender.set(
|
|
77
|
+
"U_STRANGER",
|
|
78
|
+
readOf({ trustClass: "unknown", canonicalSenderId: "U_STRANGER" }),
|
|
79
|
+
);
|
|
80
|
+
const resolve = createBackfilledSenderProvenanceResolver(GUARDIAN_ID, []);
|
|
81
|
+
|
|
82
|
+
expect(await resolve("U_STRANGER")).toEqual({
|
|
83
|
+
provenanceTrustClass: "unknown",
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test("an unreadable verdict falls back to the guardian address comparison", async () => {
|
|
88
|
+
const resolve = createBackfilledSenderProvenanceResolver(GUARDIAN_ID, []);
|
|
89
|
+
|
|
90
|
+
expect(await resolve(GUARDIAN_ID)).toEqual({
|
|
91
|
+
provenanceTrustClass: "guardian",
|
|
92
|
+
});
|
|
93
|
+
expect(await resolve("U_ALICE")).toEqual({
|
|
94
|
+
provenanceTrustClass: "unknown",
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
test("a resolver-failure verdict is not trusted for class or contact id", async () => {
|
|
99
|
+
readsBySender.set(
|
|
100
|
+
"U_ALICE",
|
|
101
|
+
readOf({
|
|
102
|
+
...memberVerdict("trusted_contact", "c-alice", "active"),
|
|
103
|
+
resolutionFailed: true,
|
|
104
|
+
}),
|
|
105
|
+
);
|
|
106
|
+
const resolve = createBackfilledSenderProvenanceResolver(GUARDIAN_ID, []);
|
|
107
|
+
|
|
108
|
+
expect(await resolve("U_ALICE")).toEqual({
|
|
109
|
+
provenanceTrustClass: "unknown",
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
test("reads the gateway once per distinct sender", async () => {
|
|
114
|
+
readsBySender.set(
|
|
115
|
+
"U_ALICE",
|
|
116
|
+
readOf(memberVerdict("trusted_contact", "c-alice", "active")),
|
|
117
|
+
);
|
|
118
|
+
const resolve = createBackfilledSenderProvenanceResolver(GUARDIAN_ID, []);
|
|
119
|
+
|
|
120
|
+
await Promise.all([
|
|
121
|
+
resolve("U_ALICE"),
|
|
122
|
+
resolve(" U_ALICE "),
|
|
123
|
+
resolve("U_BOB"),
|
|
124
|
+
]);
|
|
125
|
+
await resolve("U_ALICE");
|
|
126
|
+
|
|
127
|
+
expect(readInboundTrustMock).toHaveBeenCalledTimes(2);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
test("a row with no sender id is unknown without a gateway read", async () => {
|
|
131
|
+
const resolve = createBackfilledSenderProvenanceResolver(GUARDIAN_ID, []);
|
|
132
|
+
|
|
133
|
+
expect(await resolve(undefined)).toEqual({
|
|
134
|
+
provenanceTrustClass: "unknown",
|
|
135
|
+
});
|
|
136
|
+
expect(readInboundTrustMock).not.toHaveBeenCalled();
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
test("starts every sender's read up front and together, bots included", () => {
|
|
140
|
+
const message = (id: string, senderId: string, isBot = false) =>
|
|
141
|
+
({
|
|
142
|
+
id,
|
|
143
|
+
conversationId: "C1",
|
|
144
|
+
sender: { id: senderId, name: senderId },
|
|
145
|
+
text: "hi",
|
|
146
|
+
timestamp: 0,
|
|
147
|
+
platform: "slack",
|
|
148
|
+
...(isBot ? { metadata: { isBot: true } } : {}),
|
|
149
|
+
}) satisfies ProviderMessage;
|
|
150
|
+
|
|
151
|
+
createBackfilledSenderProvenanceResolver(GUARDIAN_ID, [
|
|
152
|
+
message("1", "U_ALICE"),
|
|
153
|
+
message("2", "B_THIRD_PARTY", true),
|
|
154
|
+
message("3", "U_ALICE"),
|
|
155
|
+
message("4", "U_BOB"),
|
|
156
|
+
]);
|
|
157
|
+
|
|
158
|
+
// Every read is in flight before any of them has settled.
|
|
159
|
+
expect(readInboundTrustMock.mock.calls.map(([input]) => input)).toEqual([
|
|
160
|
+
{ channelType: "slack", actorExternalId: "U_ALICE" },
|
|
161
|
+
{ channelType: "slack", actorExternalId: "B_THIRD_PARTY" },
|
|
162
|
+
{ channelType: "slack", actorExternalId: "U_BOB" },
|
|
163
|
+
]);
|
|
164
|
+
});
|
|
165
|
+
});
|