@vellumai/assistant 0.10.2-dev.202606242234.c9e9e1d → 0.10.2-dev.202606250106.466483e

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 (34) hide show
  1. package/bun.lock +17 -0
  2. package/node_modules/@vellumai/service-contracts/package.json +1 -0
  3. package/node_modules/@vellumai/service-contracts/src/__tests__/channels.test.ts +28 -0
  4. package/node_modules/@vellumai/service-contracts/src/channels.ts +41 -0
  5. package/node_modules/@vellumai/service-contracts/src/index.ts +1 -0
  6. package/package.json +3 -1
  7. package/src/__tests__/conversation-agent-loop.test.ts +129 -2
  8. package/src/__tests__/conversation-surfaces-task-progress.test.ts +29 -0
  9. package/src/__tests__/guardian-expiry-notifier.test.ts +282 -0
  10. package/src/__tests__/plugin-pipeline.test.ts +96 -0
  11. package/src/__tests__/ui-file-upload-surface.test.ts +86 -0
  12. package/src/api/events/conversation-notice.ts +26 -0
  13. package/src/api/index.ts +13 -1
  14. package/src/api/surfaces.ts +39 -3
  15. package/src/approvals/guardian-channel-delivery.ts +30 -0
  16. package/src/approvals/guardian-expiry-notifier.ts +148 -0
  17. package/src/approvals/guardian-request-resolvers.ts +1 -11
  18. package/src/channels/types.ts +10 -20
  19. package/src/config/feature-flag-registry.json +4 -4
  20. package/src/daemon/conversation-agent-loop.ts +19 -0
  21. package/src/daemon/conversation-notices.ts +60 -0
  22. package/src/daemon/conversation-surfaces.ts +26 -2
  23. package/src/daemon/message-types/conversations.ts +2 -0
  24. package/src/daemon/message-types/surfaces.ts +12 -12
  25. package/src/plugins/defaults/memory-v3-shadow/__tests__/injection.test.ts +33 -3
  26. package/src/plugins/defaults/memory-v3-shadow/__tests__/pool-select.test.ts +48 -4
  27. package/src/plugins/defaults/memory-v3-shadow/__tests__/shadow-plugin.test.ts +4 -8
  28. package/src/plugins/defaults/memory-v3-shadow/injector.ts +43 -15
  29. package/src/plugins/defaults/memory-v3-shadow/pool-select.ts +48 -12
  30. package/src/plugins/defaults/memory-v3-shadow/shadow-plugin.ts +4 -10
  31. package/src/plugins/pipeline.ts +111 -13
  32. package/src/runtime/routes/canonical-guardian-expiry-sweep.ts +13 -5
  33. package/src/workflows/library.test.ts +140 -0
  34. package/src/workflows/library.ts +82 -28
package/bun.lock CHANGED
@@ -55,6 +55,7 @@
55
55
  "@types/node": "25.5.0",
56
56
  "@types/semver": "7.5.8",
57
57
  "@types/uuid": "10.0.0",
58
+ "@typescript/native-preview": "7.0.0-dev.20260624.1",
58
59
  "ajv": "8.18.0",
59
60
  "drizzle-kit": "0.31.10",
60
61
  "eslint": "10.0.3",
@@ -455,6 +456,22 @@
455
456
 
456
457
  "@typescript-eslint/visitor-keys": ["@typescript-eslint/visitor-keys@8.57.0", "", { "dependencies": { "@typescript-eslint/types": "8.57.0", "eslint-visitor-keys": "^5.0.0" } }, "sha512-zm6xx8UT/Xy2oSr2ZXD0pZo7Jx2XsCoID2IUh9YSTFRu7z+WdwYTRk6LhUftm1crwqbuoF6I8zAFeCMw0YjwDg=="],
457
458
 
459
+ "@typescript/native-preview": ["@typescript/native-preview@7.0.0-dev.20260624.1", "", { "optionalDependencies": { "@typescript/native-preview-darwin-arm64": "7.0.0-dev.20260624.1", "@typescript/native-preview-darwin-x64": "7.0.0-dev.20260624.1", "@typescript/native-preview-linux-arm": "7.0.0-dev.20260624.1", "@typescript/native-preview-linux-arm64": "7.0.0-dev.20260624.1", "@typescript/native-preview-linux-x64": "7.0.0-dev.20260624.1", "@typescript/native-preview-win32-arm64": "7.0.0-dev.20260624.1", "@typescript/native-preview-win32-x64": "7.0.0-dev.20260624.1" }, "bin": { "tsgo": "bin/tsgo.js" } }, "sha512-ogwfNo1xuAutOF8RbTCo3Ut0q/65u2ucOeHizi6O14q+3vnelNS+u8qVC2QWXubMcwtuN5E9cbfPslvGC4kdwA=="],
460
+
461
+ "@typescript/native-preview-darwin-arm64": ["@typescript/native-preview-darwin-arm64@7.0.0-dev.20260624.1", "", { "os": "darwin", "cpu": "arm64" }, "sha512-g8CqDkYCHTCYdhBHXs5cMraBurOS+KrcMFxE0SsaKZoI6Tnp+le1aWvxUBbzNKJYyThHJqb/1mLopzEJxJCuKA=="],
462
+
463
+ "@typescript/native-preview-darwin-x64": ["@typescript/native-preview-darwin-x64@7.0.0-dev.20260624.1", "", { "os": "darwin", "cpu": "x64" }, "sha512-P00JVvSV90eioYDuINAKmOSA8yhFTWLq6RvS5lrCfUuDlcgr2kSOgZAfFHIksHBVz6ZXpAXpa0dHPmc5SJ3Ymw=="],
464
+
465
+ "@typescript/native-preview-linux-arm": ["@typescript/native-preview-linux-arm@7.0.0-dev.20260624.1", "", { "os": "linux", "cpu": "arm" }, "sha512-eWHELvfQMkVRjafMd+3ATgM9p9yAergJaM4AOY8AekCNWnHFwUrp/ohh+ryyMUIqque5jjb/kuTiOiGj728I2Q=="],
466
+
467
+ "@typescript/native-preview-linux-arm64": ["@typescript/native-preview-linux-arm64@7.0.0-dev.20260624.1", "", { "os": "linux", "cpu": "arm64" }, "sha512-cppM2yTZ/Gd1hOXy8NEJcUBxJ0O0zl9CU3OU1ZWZ/OHWWX/ukEzCCr94SUwJhjIWOylBCpIYkrvYoTwxNa94XQ=="],
468
+
469
+ "@typescript/native-preview-linux-x64": ["@typescript/native-preview-linux-x64@7.0.0-dev.20260624.1", "", { "os": "linux", "cpu": "x64" }, "sha512-FaB8rS+rKYz4nDrEsHsF3b4cn7eCKCYroMJReA375OuQ6PHcmCNQ6QlVetA0dfFBxTTgejmoKyfw9xgAA5P4Yw=="],
470
+
471
+ "@typescript/native-preview-win32-arm64": ["@typescript/native-preview-win32-arm64@7.0.0-dev.20260624.1", "", { "os": "win32", "cpu": "arm64" }, "sha512-BgkqbCmSHDb5UxqWaFlFFJ/DHNT3lEUO4W8627ap6+QthJZuXk2imiHAX3PgYXC6en9fLLyR6jjcseAa4CCshg=="],
472
+
473
+ "@typescript/native-preview-win32-x64": ["@typescript/native-preview-win32-x64@7.0.0-dev.20260624.1", "", { "os": "win32", "cpu": "x64" }, "sha512-WaZ+ue63NgB2j/lqjirfevh/TqcsCxSqnKhGGiRnlxHyYIBcoq+x7KngyEnyGIaywJE1PcFeXA+2EMSIPlSEiQ=="],
474
+
458
475
  "@vellumai/ces-client": ["@vellumai/ces-client@file:../packages/ces-client", { "dependencies": { "@vellumai/service-contracts": "file:../service-contracts" }, "devDependencies": { "@types/bun": "1.2.4", "typescript": "5.7.3" } }],
459
476
 
460
477
  "@vellumai/credential-storage": ["@vellumai/credential-storage@file:../packages/credential-storage", { "devDependencies": { "@types/bun": "1.3.10", "typescript": "5.9.3" } }],
@@ -6,6 +6,7 @@
6
6
  "type": "module",
7
7
  "exports": {
8
8
  ".": "./src/index.ts",
9
+ "./channels": "./src/channels.ts",
9
10
  "./credential-rpc": "./src/credential-rpc.ts",
10
11
  "./ingress": "./src/ingress.ts",
11
12
  "./twilio-ingress": "./src/twilio-ingress.ts",
@@ -0,0 +1,28 @@
1
+ import { describe, expect, test } from "bun:test";
2
+
3
+ import { CHANNEL_IDS, isChannelId } from "../channels.js";
4
+
5
+ describe("isChannelId", () => {
6
+ test("accepts every canonical channel id", () => {
7
+ for (const id of CHANNEL_IDS) {
8
+ expect(isChannelId(id)).toBe(true);
9
+ }
10
+ });
11
+
12
+ test("includes the internal channels no external surface ingresses", () => {
13
+ // `platform` (control plane) and `vellum` (native app) are part of the
14
+ // canonical vocabulary even though the gateway never ingresses them. The
15
+ // gateway's narrower list is a compile-time-asserted subset of this set,
16
+ // so these must remain canonical for that assertion to mean anything.
17
+ expect(isChannelId("platform")).toBe(true);
18
+ expect(isChannelId("vellum")).toBe(true);
19
+ });
20
+
21
+ test("rejects unknown strings and non-string values", () => {
22
+ expect(isChannelId("discord")).toBe(false);
23
+ expect(isChannelId("")).toBe(false);
24
+ expect(isChannelId(undefined)).toBe(false);
25
+ expect(isChannelId(null)).toBe(false);
26
+ expect(isChannelId(42)).toBe(false);
27
+ });
28
+ });
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Canonical channel-id vocabulary shared between the assistant daemon and the
3
+ * gateway.
4
+ *
5
+ * A "channel" is an external messaging surface an actor can reach the
6
+ * assistant through (Slack, Telegram, WhatsApp, phone, …) plus a couple of
7
+ * internal ids (`vellum` for native app conversations, `platform` for the
8
+ * internal control plane). This is the single source of truth for that set:
9
+ * the assistant adopts it wholesale as its `ChannelId`, and the gateway
10
+ * asserts its own (narrower) inbound list is a subset of it so the two sides
11
+ * cannot silently drift.
12
+ *
13
+ * Both packages depend on `@vellumai/service-contracts`, so hoisting the set
14
+ * here (rather than maintaining a copy on each side) means adding or renaming
15
+ * a channel happens in exactly one place.
16
+ *
17
+ * Note that a consumer may legitimately handle only a *subset* of these — the
18
+ * gateway, for example, never ingresses `platform`. Use a local list guarded
19
+ * by `satisfies readonly ChannelId[]` for those cases rather than redefining
20
+ * the union.
21
+ */
22
+
23
+ export const CHANNEL_IDS = [
24
+ "telegram",
25
+ "phone",
26
+ "vellum",
27
+ "whatsapp",
28
+ "slack",
29
+ "email",
30
+ "platform",
31
+ "a2a",
32
+ ] as const;
33
+
34
+ export type ChannelId = (typeof CHANNEL_IDS)[number];
35
+
36
+ export function isChannelId(value: unknown): value is ChannelId {
37
+ return (
38
+ typeof value === "string" &&
39
+ (CHANNEL_IDS as readonly string[]).includes(value)
40
+ );
41
+ }
@@ -18,6 +18,7 @@
18
18
  * module so that both sides can depend on it without circular references.
19
19
  */
20
20
 
21
+ export * from "./channels.js";
21
22
  export * from "./transport.js";
22
23
  export * from "./error.js";
23
24
  export * from "./handles.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vellumai/assistant",
3
- "version": "0.10.2-dev.202606242234.c9e9e1d",
3
+ "version": "0.10.2-dev.202606250106.466483e",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "exports": {
@@ -24,6 +24,7 @@
24
24
  "lint:circular": "bun run scripts/check-circular-deps.ts",
25
25
  "lint:unused:production": "knip --production --include exports",
26
26
  "typecheck": "bunx tsc --noEmit",
27
+ "typecheck:fast": "bunx tsgo --noEmit",
27
28
  "test": "bash scripts/test.sh",
28
29
  "test:coverage": "COVERAGE=true bash scripts/test.sh",
29
30
  "test:stable": "EXCLUDE_EXPERIMENTAL=true bash scripts/test.sh",
@@ -106,6 +107,7 @@
106
107
  "@types/node": "25.5.0",
107
108
  "@types/semver": "7.5.8",
108
109
  "@types/uuid": "10.0.0",
110
+ "@typescript/native-preview": "7.0.0-dev.20260624.1",
109
111
  "ajv": "8.18.0",
110
112
  "drizzle-kit": "0.31.10",
111
113
  "eslint": "10.0.3",
@@ -10,8 +10,14 @@ import {
10
10
  } from "bun:test";
11
11
 
12
12
  import type { LoopToolExecutor } from "../agent/loop.js";
13
+ import {
14
+ queueConversationNotice,
15
+ resetConversationNoticesForTests,
16
+ } from "../daemon/conversation-notices.js";
13
17
  import type { ServerMessage } from "../daemon/message-protocol.js";
18
+ import type { UserPromptSubmitContext } from "../plugin-api/types.js";
14
19
  import { resetPluginRegistryAndRegisterDefaults } from "../plugins/defaults/index.js";
20
+ import { registerPlugin } from "../plugins/registry.js";
15
21
  import type { Message, Provider, ToolDefinition } from "../providers/types.js";
16
22
  import { ContextOverflowError } from "../providers/types.js";
17
23
 
@@ -273,8 +279,8 @@ const deleteMessageByIdMock = mock(() => ({
273
279
  const reserveMessageMock = mock(async () => ({ id: "msg-reserve" }));
274
280
  const updateMessageContentMock = mock(() => {});
275
281
  mock.module("../memory/conversation-crud.js", () => ({
276
- setConversationProcessingStartedAt: () => {},
277
- isConversationProcessing: () => false,
282
+ setConversationProcessingStartedAt: () => {},
283
+ isConversationProcessing: () => false,
278
284
  setConversationOriginChannelIfUnset: () => {},
279
285
  updateConversationUsage: () => {},
280
286
  updateMessageMetadata: updateMessageMetadataMock,
@@ -867,7 +873,14 @@ beforeEach(() => {
867
873
  indexMessageNowMock.mockClear();
868
874
  projectAssistantMessageMock.mockClear();
869
875
  publishSyncInvalidationMock.mockClear();
876
+ resolveAssistantAttachmentsMock.mockClear();
877
+ resolveAssistantAttachmentsMock.mockImplementation(async () => ({
878
+ assistantAttachments: [],
879
+ emittedAttachments: [],
880
+ directiveWarnings: [],
881
+ }));
870
882
  mockMessageById = null;
883
+ resetConversationNoticesForTests();
871
884
  // The compaction pipeline runs through the plugin registry; reset and
872
885
  // re-register every default so it dispatches to middleware backed by the
873
886
  // mocked collaborators these tests install (`syncMessageToDisk`, etc.)
@@ -876,6 +889,120 @@ beforeEach(() => {
876
889
  });
877
890
 
878
891
  describe("session-agent-loop", () => {
892
+ describe("user-prompt-submit hook failures", () => {
893
+ test("logs and continues with prior hook mutations", async () => {
894
+ registerPlugin({
895
+ manifest: {
896
+ name: "test-user-prompt-rewrite",
897
+ version: "1.0.0",
898
+ },
899
+ hooks: {
900
+ "user-prompt-submit": async (_ctx: UserPromptSubmitContext) => ({
901
+ latestMessages: [
902
+ {
903
+ role: "user" as const,
904
+ content: [{ type: "text" as const, text: "rewritten prompt" }],
905
+ },
906
+ ],
907
+ }),
908
+ },
909
+ });
910
+ registerPlugin({
911
+ manifest: {
912
+ name: "test-user-prompt-throw",
913
+ version: "1.0.0",
914
+ },
915
+ hooks: {
916
+ "user-prompt-submit": async () => {
917
+ throw new Error("simulated hook failure");
918
+ },
919
+ },
920
+ });
921
+
922
+ const events: ServerMessage[] = [];
923
+ const ctx = makeCtx({ providerResponses: [textResponse("ok")] });
924
+ const runSpy = spyOn(ctx.agentLoop, "run");
925
+
926
+ await runAgentLoopImpl(ctx, "hello", "msg-1", (msg) => events.push(msg));
927
+
928
+ expect(runSpy).toHaveBeenCalledTimes(1);
929
+ const call = runSpy.mock.calls[0]?.[0] as
930
+ | { messages: Message[] }
931
+ | undefined;
932
+ expect(call?.messages[0]?.content).toEqual([
933
+ { type: "text", text: "rewritten prompt" },
934
+ ]);
935
+ expect(
936
+ events.find((event) => event.type === "conversation_error"),
937
+ ).toBeUndefined();
938
+ expect(
939
+ events.find((event) => event.type === "message_complete"),
940
+ ).toBeDefined();
941
+ });
942
+ });
943
+
944
+ describe("conversation notices", () => {
945
+ test("emits queued billing notices after a successful turn", async () => {
946
+ const events: ServerMessage[] = [];
947
+ const ctx = makeCtx({ providerResponses: [textResponse("ok")] });
948
+ queueConversationNotice(ctx.conversationId, "memory-v3-test", {
949
+ source: "memory_v3",
950
+ code: "PROVIDER_BILLING",
951
+ userMessage: "You've run out of credits.",
952
+ errorCategory: "credits_exhausted",
953
+ });
954
+
955
+ await runAgentLoopImpl(ctx, "hello", "msg-1", (msg) => events.push(msg));
956
+
957
+ expect(
958
+ events.find((event) => event.type === "conversation_error"),
959
+ ).toBeUndefined();
960
+ const messageCompleteIndex = events.findIndex(
961
+ (event) => event.type === "message_complete",
962
+ );
963
+ const conversationNoticeIndex = events.findIndex(
964
+ (event) => event.type === "conversation_notice",
965
+ );
966
+
967
+ expect(messageCompleteIndex).toBeGreaterThanOrEqual(0);
968
+ expect(conversationNoticeIndex).toBeGreaterThan(messageCompleteIndex);
969
+ expect(events[conversationNoticeIndex]).toEqual({
970
+ type: "conversation_notice",
971
+ conversationId: "test-conv",
972
+ source: "memory_v3",
973
+ code: "PROVIDER_BILLING",
974
+ userMessage: "You've run out of credits.",
975
+ errorCategory: "credits_exhausted",
976
+ });
977
+ });
978
+
979
+ test("clears queued notices when post-loop success work fails", async () => {
980
+ resolveAssistantAttachmentsMock.mockImplementation(async () => {
981
+ throw new Error("attachment resolution failed");
982
+ });
983
+ const events: ServerMessage[] = [];
984
+ const ctx = makeCtx({ providerResponses: [textResponse("ok")] });
985
+ queueConversationNotice(ctx.conversationId, "memory-v3-test", {
986
+ source: "memory_v3",
987
+ code: "PROVIDER_BILLING",
988
+ userMessage: "You've run out of credits.",
989
+ errorCategory: "credits_exhausted",
990
+ });
991
+
992
+ await runAgentLoopImpl(ctx, "hello", "msg-1", (msg) => events.push(msg));
993
+
994
+ expect(
995
+ events.find((event) => event.type === "conversation_notice"),
996
+ ).toBeUndefined();
997
+ expect(
998
+ events.find((event) => event.type === "message_complete"),
999
+ ).toBeUndefined();
1000
+ expect(
1001
+ events.find((event) => event.type === "conversation_error"),
1002
+ ).toBeDefined();
1003
+ });
1004
+ });
1005
+
879
1006
  describe("timezone turn context", () => {
880
1007
  test("passes ctx.clientTimezone and ui.detectedTimezone into timezone resolution", async () => {
881
1008
  mockUiConfig = {
@@ -336,6 +336,35 @@ describe("task_progress surface compatibility", () => {
336
336
  });
337
337
  });
338
338
 
339
+ test("ui_show file_upload normalizes a comma-joined acceptedTypes string", async () => {
340
+ const sent: ServerMessage[] = [];
341
+ const ctx = makeContext(sent);
342
+
343
+ // The model may emit acceptedTypes as a comma-joined string; the renderer
344
+ // calls `.join`/`.some` on it, so the daemon hands the client a clean array.
345
+ const result = await surfaceProxyResolver(ctx, "ui_show", {
346
+ surface_type: "file_upload",
347
+ title: "Upload a receipt",
348
+ data: {
349
+ prompt: "Share the receipt",
350
+ acceptedTypes: "image/*, application/pdf",
351
+ },
352
+ });
353
+
354
+ expect(result.isError).toBe(false);
355
+
356
+ const showMessage = sent.find(
357
+ (msg): msg is UiSurfaceShow => msg.type === "ui_surface_show",
358
+ );
359
+ expect(showMessage).toBeDefined();
360
+ if (!showMessage || showMessage.surfaceType !== "file_upload") return;
361
+
362
+ expect(showMessage.data.acceptedTypes).toEqual([
363
+ "image/*",
364
+ "application/pdf",
365
+ ]);
366
+ });
367
+
339
368
  test("ui_show dynamic_page uses data.html when properly nested", async () => {
340
369
  const sent: ServerMessage[] = [];
341
370
  const ctx = makeContext(sent);
@@ -0,0 +1,282 @@
1
+ /**
2
+ * Tests for guardian request expiry side effects:
3
+ *
4
+ * 1. notifyExpiredGuardianRequest — per-kind behavior (requester notice for
5
+ * access_request / tool_grant_request, interaction release for tool_approval,
6
+ * no-op for pending_question), Slack DM routing, non-deliverable channels,
7
+ * and best-effort (non-throwing) delivery.
8
+ * 2. Sweep integration — an expired request is transitioned to `expired` and the
9
+ * requester is notified through the wired-in notifier.
10
+ */
11
+
12
+ import { beforeEach, describe, expect, mock, test } from "bun:test";
13
+
14
+ // Silence logging.
15
+ mock.module("../util/logger.js", () => ({
16
+ getLogger: () =>
17
+ new Proxy({} as Record<string, unknown>, {
18
+ get: () => () => {},
19
+ }),
20
+ truncateForLog: (value: string) => value,
21
+ }));
22
+
23
+ // Capture requester channel deliveries; optionally fail to exercise the
24
+ // best-effort path.
25
+ const deliveredReplies: Array<{
26
+ url: string;
27
+ payload: { chatId: string; text: string; assistantId?: string };
28
+ }> = [];
29
+ let deliveryError: Error | null = null;
30
+ mock.module("../runtime/gateway-client.js", () => ({
31
+ deliverChannelReply: async (
32
+ url: string,
33
+ payload: { chatId: string; text: string; assistantId?: string },
34
+ ) => {
35
+ if (deliveryError) throw deliveryError;
36
+ deliveredReplies.push({ url, payload });
37
+ },
38
+ }));
39
+
40
+ // Capture hub broadcasts (interaction_resolved) emitted by pendingInteractions.
41
+ const broadcasts: Array<Record<string, unknown>> = [];
42
+ mock.module("../runtime/assistant-event-hub.js", () => ({
43
+ broadcastMessage: (msg: Record<string, unknown>) => {
44
+ broadcasts.push(msg);
45
+ },
46
+ }));
47
+
48
+ // The sweep withdraws cards via this module; we only assert it is invoked, and
49
+ // mocking it keeps the sweep import light (no surface/slack transitive deps).
50
+ let withdrawCalls = 0;
51
+ mock.module("../approvals/guardian-card-withdrawal.js", () => ({
52
+ withdrawGuardianRequestCards: async () => {
53
+ withdrawCalls++;
54
+ },
55
+ }));
56
+
57
+ import { notifyExpiredGuardianRequest } from "../approvals/guardian-expiry-notifier.js";
58
+ import type { CanonicalGuardianRequest } from "../memory/canonical-guardian-store.js";
59
+ import {
60
+ createCanonicalGuardianRequest,
61
+ getCanonicalGuardianRequest,
62
+ } from "../memory/canonical-guardian-store.js";
63
+ import { getDb } from "../memory/db-connection.js";
64
+ import { initializeDb } from "../memory/db-init.js";
65
+ import * as pendingInteractions from "../runtime/pending-interactions.js";
66
+ import { sweepExpiredCanonicalGuardianRequests } from "../runtime/routes/canonical-guardian-expiry-sweep.js";
67
+
68
+ await initializeDb();
69
+
70
+ /** Build a fully-populated canonical request, overriding the interesting bits. */
71
+ function makeRequest(
72
+ overrides: Partial<CanonicalGuardianRequest> & { kind: string },
73
+ ): CanonicalGuardianRequest {
74
+ return {
75
+ id: "req-1",
76
+ sourceType: "channel",
77
+ sourceChannel: "telegram",
78
+ conversationId: "conv-1",
79
+ requesterExternalUserId: "req-user",
80
+ requesterChatId: "req-chat",
81
+ guardianExternalUserId: "guardian-user",
82
+ guardianPrincipalId: "guardian-principal",
83
+ callSessionId: null,
84
+ pendingQuestionId: null,
85
+ questionText: null,
86
+ requestCode: "ABC123",
87
+ toolName: null,
88
+ inputDigest: null,
89
+ commandPreview: null,
90
+ riskLevel: null,
91
+ activityText: null,
92
+ executionTarget: null,
93
+ status: "expired",
94
+ answerText: null,
95
+ decidedByExternalUserId: null,
96
+ decidedByPrincipalId: null,
97
+ followupState: null,
98
+ expiresAt: 1000,
99
+ createdAt: 1000,
100
+ updatedAt: 2000,
101
+ ...overrides,
102
+ };
103
+ }
104
+
105
+ beforeEach(() => {
106
+ deliveredReplies.length = 0;
107
+ broadcasts.length = 0;
108
+ deliveryError = null;
109
+ withdrawCalls = 0;
110
+ pendingInteractions.clear();
111
+ });
112
+
113
+ describe("notifyExpiredGuardianRequest", () => {
114
+ test("access_request: notifies the requester on their channel", async () => {
115
+ await notifyExpiredGuardianRequest(
116
+ makeRequest({
117
+ kind: "access_request",
118
+ sourceChannel: "telegram",
119
+ requesterChatId: "tg-chat",
120
+ requesterExternalUserId: "tg-user",
121
+ }),
122
+ );
123
+
124
+ expect(deliveredReplies).toHaveLength(1);
125
+ expect(deliveredReplies[0].url).toBe("/deliver/telegram");
126
+ expect(deliveredReplies[0].payload.chatId).toBe("tg-chat");
127
+ expect(deliveredReplies[0].payload.text).toContain(
128
+ "access request expired",
129
+ );
130
+ });
131
+
132
+ test("access_request on Slack: routes to the requester DM, not the channel", async () => {
133
+ await notifyExpiredGuardianRequest(
134
+ makeRequest({
135
+ kind: "access_request",
136
+ sourceChannel: "slack",
137
+ requesterChatId: "C0SHARED",
138
+ requesterExternalUserId: "U123",
139
+ }),
140
+ );
141
+
142
+ expect(deliveredReplies).toHaveLength(1);
143
+ expect(deliveredReplies[0].url).toBe("/deliver/slack");
144
+ // DM via the user id, never the shared channel id.
145
+ expect(deliveredReplies[0].payload.chatId).toBe("U123");
146
+ });
147
+
148
+ test("tool_grant_request: notice names the tool", async () => {
149
+ await notifyExpiredGuardianRequest(
150
+ makeRequest({
151
+ kind: "tool_grant_request",
152
+ sourceChannel: "telegram",
153
+ requesterChatId: "tg-chat",
154
+ toolName: "bash",
155
+ }),
156
+ );
157
+
158
+ expect(deliveredReplies).toHaveLength(1);
159
+ expect(deliveredReplies[0].payload.text).toContain('"bash"');
160
+ expect(deliveredReplies[0].payload.text).toContain("expired");
161
+ });
162
+
163
+ test("tool_approval: releases the pending interaction, sends no channel notice", async () => {
164
+ pendingInteractions.register("req-ta", {
165
+ conversationId: "conv-1",
166
+ kind: "confirmation",
167
+ });
168
+
169
+ await notifyExpiredGuardianRequest(
170
+ makeRequest({ id: "req-ta", kind: "tool_approval" }),
171
+ );
172
+
173
+ expect(pendingInteractions.get("req-ta")).toBeUndefined();
174
+ const resolvedEvent = broadcasts.find(
175
+ (b) => b.type === "interaction_resolved",
176
+ );
177
+ expect(resolvedEvent).toMatchObject({
178
+ requestId: "req-ta",
179
+ state: "cancelled",
180
+ });
181
+ expect(deliveredReplies).toHaveLength(0);
182
+ });
183
+
184
+ test("tool_approval: no registered interaction is a safe no-op", async () => {
185
+ await notifyExpiredGuardianRequest(
186
+ makeRequest({ id: "req-none", kind: "tool_approval" }),
187
+ );
188
+
189
+ expect(deliveredReplies).toHaveLength(0);
190
+ expect(broadcasts).toHaveLength(0);
191
+ });
192
+
193
+ test("pending_question: no notice (voice owns its lifecycle)", async () => {
194
+ await notifyExpiredGuardianRequest(
195
+ makeRequest({ kind: "pending_question", sourceChannel: "phone" }),
196
+ );
197
+
198
+ expect(deliveredReplies).toHaveLength(0);
199
+ });
200
+
201
+ test("non-deliverable channel: no notice", async () => {
202
+ await notifyExpiredGuardianRequest(
203
+ makeRequest({ kind: "access_request", sourceChannel: "vellum" }),
204
+ );
205
+
206
+ expect(deliveredReplies).toHaveLength(0);
207
+ });
208
+
209
+ test("missing requester chat: no notice", async () => {
210
+ await notifyExpiredGuardianRequest(
211
+ makeRequest({
212
+ kind: "access_request",
213
+ sourceChannel: "telegram",
214
+ requesterChatId: null,
215
+ requesterExternalUserId: null,
216
+ }),
217
+ );
218
+
219
+ expect(deliveredReplies).toHaveLength(0);
220
+ });
221
+
222
+ test("delivery failure is swallowed (best-effort)", async () => {
223
+ deliveryError = new Error("gateway down");
224
+
225
+ await expect(
226
+ notifyExpiredGuardianRequest(
227
+ makeRequest({
228
+ kind: "access_request",
229
+ sourceChannel: "telegram",
230
+ requesterChatId: "tg-chat",
231
+ }),
232
+ ),
233
+ ).resolves.toBeUndefined();
234
+ });
235
+ });
236
+
237
+ describe("sweep integration", () => {
238
+ beforeEach(() => {
239
+ const db = getDb();
240
+ db.run("DELETE FROM canonical_guardian_requests");
241
+ db.run("DELETE FROM canonical_guardian_deliveries");
242
+ });
243
+
244
+ test("expired access_request is transitioned and the requester is notified", async () => {
245
+ const request = createCanonicalGuardianRequest({
246
+ kind: "access_request",
247
+ sourceType: "channel",
248
+ sourceChannel: "telegram",
249
+ requesterChatId: "tg-chat",
250
+ requesterExternalUserId: "tg-user",
251
+ guardianPrincipalId: "guardian-principal",
252
+ expiresAt: Date.now() - 1000, // already past
253
+ });
254
+
255
+ const expiredCount = await sweepExpiredCanonicalGuardianRequests();
256
+
257
+ expect(expiredCount).toBe(1);
258
+ expect(getCanonicalGuardianRequest(request.id)?.status).toBe("expired");
259
+ expect(withdrawCalls).toBe(1);
260
+ expect(deliveredReplies).toHaveLength(1);
261
+ expect(deliveredReplies[0].payload.text).toContain(
262
+ "access request expired",
263
+ );
264
+ });
265
+
266
+ test("not-yet-expired requests are left pending and unnotified", async () => {
267
+ const request = createCanonicalGuardianRequest({
268
+ kind: "access_request",
269
+ sourceType: "channel",
270
+ sourceChannel: "telegram",
271
+ requesterChatId: "tg-chat",
272
+ guardianPrincipalId: "guardian-principal",
273
+ expiresAt: Date.now() + 60_000, // still in the future
274
+ });
275
+
276
+ const expiredCount = await sweepExpiredCanonicalGuardianRequests();
277
+
278
+ expect(expiredCount).toBe(0);
279
+ expect(getCanonicalGuardianRequest(request.id)?.status).toBe("pending");
280
+ expect(deliveredReplies).toHaveLength(0);
281
+ });
282
+ });