@vellumai/assistant 0.11.2-dev.202608062308.5442cf1 → 0.11.2-dev.202608070306.b97cdd6
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/docs/trusted-contact-access.md +9 -5
- package/package.json +1 -1
- package/src/__tests__/channel-guardian.test.ts +4 -4
- package/src/__tests__/conversation-initial-prompt.test.ts +51 -22
- package/src/__tests__/conversation-origin-channel-filter.test.ts +95 -0
- package/src/__tests__/discord-access-request-privacy.test.ts +366 -0
- package/src/__tests__/discord-requester-notice-privacy.test.ts +87 -0
- package/src/__tests__/guardian-outbound-http.test.ts +8 -8
- package/src/__tests__/notification-platform-adapter.test.ts +104 -0
- package/src/__tests__/persona-resolver.test.ts +154 -0
- package/src/__tests__/plugin-import-boundary-guard.test.ts +6 -0
- package/src/__tests__/resolve-trust-class.test.ts +20 -0
- package/src/__tests__/tool-side-effects-documents.test.ts +0 -6
- package/src/__tests__/tool-side-effects-hook-failures.test.ts +129 -0
- package/src/__tests__/verification-outbound-delivery.test.ts +176 -0
- package/src/approvals/guardian-channel-delivery.ts +83 -0
- package/src/approvals/guardian-expiry-notifier.ts +13 -11
- package/src/approvals/guardian-request-resolvers.ts +197 -131
- package/src/config/feature-flag-registry.json +8 -0
- package/src/contacts/__tests__/guardian-delivery-reader.test.ts +16 -0
- package/src/contacts/guardian-delivery-reader.ts +27 -0
- package/src/daemon/conversation-initial-prompt.ts +16 -38
- package/src/daemon/handlers/config-channels.ts +44 -4
- package/src/daemon/identity-helpers.ts +38 -10
- package/src/daemon/tool-side-effects.ts +0 -90
- package/src/daemon/trust-context.ts +14 -6
- package/src/heartbeat/heartbeat-service.ts +6 -5
- package/src/messaging/providers/discord/api.ts +90 -0
- package/src/messaging/providers/discord/dm-delivery.test.ts +204 -0
- package/src/messaging/providers/discord/transport.ts +15 -2
- package/src/notifications/__tests__/assistant-reply-producer.test.ts +205 -2
- package/src/notifications/__tests__/notification-utils.test.ts +249 -0
- package/src/notifications/adapters/platform.ts +31 -2
- package/src/notifications/assistant-reply-producer.ts +44 -28
- package/src/notifications/home-feed-side-effect.ts +6 -3
- package/src/notifications/notification-utils.ts +167 -0
- package/src/persistence/conversation-queries.ts +27 -1
- package/src/persistence/conversation-types.ts +11 -0
- package/src/plugins/defaults/memory/__tests__/memory-retrospective-job.test.ts +1 -1
- package/src/plugins/defaults/memory/memory-retrospective-job.ts +6 -5
- package/src/plugins/defaults/memory/substrate/sweep-job.ts +7 -0
- package/src/plugins/defaults/memory/v2/router.ts +8 -0
- package/src/prompts/persona-resolver.ts +6 -2
- package/src/runtime/migrations/__tests__/vbundle-symlink-streaming-importer.test.ts +0 -0
- package/src/runtime/routes/__tests__/channel-verification-routes.test.ts +1 -1
- package/src/runtime/routes/channel-verification-routes.ts +2 -27
- package/src/runtime/routes/inbound-stages/bootstrap-intercept.ts +2 -2
- package/src/runtime/verification-outbound-actions.ts +234 -213
- package/src/runtime/verification-templates.ts +41 -30
- package/src/__tests__/tool-side-effects-slack-dm.test.ts +0 -357
|
@@ -43,6 +43,9 @@ Design doc defining how unknown users gain access to a Vellum assistant via chan
|
|
|
43
43
|
5. **On approval the gateway mints a verification session.** The decide op carries a `mint_outbound_session` outcome; inside the decide transaction the gateway (`gateway/src/verification/session-service.ts`) generates a 6-digit code, persists only its SHA-256 hash in `channel_verification_sessions` (identity-bound to the requester, `verificationPurpose: 'trusted_contact'`), and returns the raw secret to the daemon in the decide response for delivery.
|
|
44
44
|
6. **The code is delivered.** The daemon delivers the code to the guardian's verified channel (ephemeral + DM on Slack shared channels so other members never see it). On Slack the code is also DM'd straight to the requester; on other channels the guardian relays it out-of-band (in person, text message, phone call). That out-of-band transfer is the trust anchor: it proves the requester has a real-world relationship with the guardian.
|
|
45
45
|
7. **Requester enters the code** back to the assistant on the same channel. The **gateway** intercepts bare verification codes at ingress (`gateway/src/verification/text-verification.ts`) whenever an interceptable session exists for that channel — the daemon never sees verification code messages.
|
|
46
|
+
|
|
47
|
+
On Discord the code is delivered to, and answered in, a DM (`gateway/src/discord/admit.ts` admits DMs on their own lane). A bare code typed into a guild channel is dropped by the mention gate before it reaches the intercept, so it is never consumed and never reaches the agent.
|
|
48
|
+
|
|
46
49
|
8. **Gateway verifies the code and activates the user.** `validateAndConsumeSession()` checks the rate limit, hashes the code, matches it against a live session, verifies identity binding, atomically consumes the session, then applies the trusted-contact side effects: `applyTrustedContactSideEffects()` upserts the verified channel into the gateway ACL with `status: 'active'`, `policy: 'allow'` and mirrors the contact identity to the assistant DB. A blocked/revoked authoritative gateway row rejects the verification even when the code matched. The gateway delivers the deterministic success reply.
|
|
47
50
|
9. **All subsequent messages are accepted normally.** The gateway stamps an allow verdict and the daemon processes the message.
|
|
48
51
|
|
|
@@ -65,11 +68,12 @@ requested -> pending_guardian -> verification_pending -> active | denied | expir
|
|
|
65
68
|
|
|
66
69
|
Identity binding ensures the verification code can only be consumed by the intended recipient on the intended channel. The binding fields are set on the gateway `channel_verification_sessions` record when the session is created; the check itself is `checkIdentityMatch` (`gateway/src/verification/identity-match.ts`).
|
|
67
70
|
|
|
68
|
-
| Channel | Identity fields | Binding behavior
|
|
69
|
-
| --------- | -------------------------------------------------------------------------------- |
|
|
70
|
-
| Telegram | `expectedExternalUserId` = Telegram user ID, `expectedChatId` = Telegram chat ID | Both are taken from the original denied message's metadata. The `identityBindingStatus` is `'bound'`. Verification requires the actor's user ID or chat ID to match (user ID required when both are set on a shared chat).
|
|
71
|
-
| Voice | `expectedPhoneE164` = phone number in E.164 format | Phone-based identity binding. Verification requires the caller's number to match the expected phone.
|
|
72
|
-
|
|
|
71
|
+
| Channel | Identity fields | Binding behavior |
|
|
72
|
+
| --------- | -------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
73
|
+
| Telegram | `expectedExternalUserId` = Telegram user ID, `expectedChatId` = Telegram chat ID | Both are taken from the original denied message's metadata. The `identityBindingStatus` is `'bound'`. Verification requires the actor's user ID or chat ID to match (user ID required when both are set on a shared chat). |
|
|
74
|
+
| Voice | `expectedPhoneE164` = phone number in E.164 format | Phone-based identity binding. Verification requires the caller's number to match the expected phone. |
|
|
75
|
+
| Discord | `expectedExternalUserId` = Discord user snowflake. `expectedChatId` is unset on the guardian-initiated and trusted-contact paths, and is the originating guild channel on the access-request approval path (`mint_outbound_session`), which sets it for every channel alike. | The code is delivered to a DM and answered there, but the DM channel does not exist until it is opened, so those two paths have no chat id to record at mint time. Where the approval path does set one, it is a room rather than an identity and never becomes a sufficient credential on its own: `checkIdentityMatch` requires the `expectedExternalUserId` match whenever both fields are set. That path also writes the room into `destinationAddress`, where it describes nothing the code is ever sent to (see LUM-3110). |
|
|
76
|
+
| Bootstrap | none yet (`identityBindingStatus: 'pending_bootstrap'`) | Unbound deep-link sessions use a 32-byte hex token instead of a numeric code; the identity is bound when the token is redeemed (`verification_sessions_bind_identity`). |
|
|
73
77
|
|
|
74
78
|
**Anti-oracle invariant:** When identity verification fails, the error message is identical to the "invalid or expired code" message. This prevents attackers from distinguishing between a wrong code and a wrong identity, which would leak information about which identities have pending sessions.
|
|
75
79
|
|
package/package.json
CHANGED
|
@@ -137,7 +137,7 @@ import {
|
|
|
137
137
|
startOutbound,
|
|
138
138
|
} from "../runtime/verification-outbound-actions.js";
|
|
139
139
|
import {
|
|
140
|
-
|
|
140
|
+
composeVerificationText,
|
|
141
141
|
GUARDIAN_VERIFY_TEMPLATE_KEYS,
|
|
142
142
|
} from "../runtime/verification-templates.js";
|
|
143
143
|
import { resetDbForTesting } from "./db-test-helpers.js";
|
|
@@ -2344,7 +2344,7 @@ describe("outbound Telegram verification", () => {
|
|
|
2344
2344
|
});
|
|
2345
2345
|
|
|
2346
2346
|
test("telegram template does not include verification code in message", async () => {
|
|
2347
|
-
const msg =
|
|
2347
|
+
const msg = composeVerificationText(
|
|
2348
2348
|
GUARDIAN_VERIFY_TEMPLATE_KEYS.TELEGRAM_CHALLENGE_REQUEST,
|
|
2349
2349
|
{ code: "abc123", expiresInMinutes: 10 },
|
|
2350
2350
|
);
|
|
@@ -2353,7 +2353,7 @@ describe("outbound Telegram verification", () => {
|
|
|
2353
2353
|
});
|
|
2354
2354
|
|
|
2355
2355
|
test("telegram resend template does not include code and includes (resent) suffix", async () => {
|
|
2356
|
-
const msg =
|
|
2356
|
+
const msg = composeVerificationText(
|
|
2357
2357
|
GUARDIAN_VERIFY_TEMPLATE_KEYS.TELEGRAM_RESEND,
|
|
2358
2358
|
{ code: "xyz789", expiresInMinutes: 5 },
|
|
2359
2359
|
);
|
|
@@ -2363,7 +2363,7 @@ describe("outbound Telegram verification", () => {
|
|
|
2363
2363
|
});
|
|
2364
2364
|
|
|
2365
2365
|
test("telegram template includes Vellum assistant prefix", async () => {
|
|
2366
|
-
const msg =
|
|
2366
|
+
const msg = composeVerificationText(
|
|
2367
2367
|
GUARDIAN_VERIFY_TEMPLATE_KEYS.TELEGRAM_CHALLENGE_REQUEST,
|
|
2368
2368
|
{ code: "999999", expiresInMinutes: 10, assistantName: "MyBot" },
|
|
2369
2369
|
);
|
|
@@ -3,11 +3,12 @@
|
|
|
3
3
|
*
|
|
4
4
|
* A conversation's system prompt is built once at construction and frozen for
|
|
5
5
|
* every turn (the agent loop never re-resolves it), so the persona slot must
|
|
6
|
-
* resolve correctly here. With no construction-time identity
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
6
|
+
* resolve correctly here. With no construction-time identity OR a
|
|
7
|
+
* guardian-class trust context (background/scheduled turns, memory
|
|
8
|
+
* consolidation, managed desktop) the guardian binding must be warmed BEFORE
|
|
9
|
+
* building (else a cold cache pins `users/default.md`); a channel-routed
|
|
10
|
+
* conversation carrying a non-guardian requester trust context must build with
|
|
11
|
+
* it directly so the requester's profile resolves via the DB contact lookup.
|
|
11
12
|
*
|
|
12
13
|
* Dependency seams are injected so this exercises the sequencing without mocking
|
|
13
14
|
* the widely-imported guardian-delivery / system-prompt modules (those global
|
|
@@ -15,10 +16,7 @@
|
|
|
15
16
|
*/
|
|
16
17
|
import { describe, expect, test } from "bun:test";
|
|
17
18
|
|
|
18
|
-
import {
|
|
19
|
-
resolveInitialSystemPrompt,
|
|
20
|
-
warmGuardianBindings,
|
|
21
|
-
} from "../daemon/conversation-initial-prompt.js";
|
|
19
|
+
import { resolveInitialSystemPrompt } from "../daemon/conversation-initial-prompt.js";
|
|
22
20
|
import type { ConversationCreateOptions } from "../daemon/handlers/shared.js";
|
|
23
21
|
import type { TrustContext } from "../daemon/trust-context-types.js";
|
|
24
22
|
|
|
@@ -47,18 +45,22 @@ const REQUESTER_TRUST = {
|
|
|
47
45
|
requesterExternalUserId: "user-123",
|
|
48
46
|
} as unknown as TrustContext;
|
|
49
47
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
48
|
+
// The shape scheduler execute/reuse runs, memory consolidation, and other
|
|
49
|
+
// runBackgroundJob callers pass at construction: guardian class, no
|
|
50
|
+
// per-actor identity.
|
|
51
|
+
const GUARDIAN_BACKGROUND_TRUST = {
|
|
52
|
+
sourceChannel: "vellum",
|
|
53
|
+
trustClass: "guardian",
|
|
54
|
+
} as unknown as TrustContext;
|
|
55
|
+
|
|
56
|
+
// Managed desktop: guardian class WITH a requester principal id. The persona
|
|
57
|
+
// resolution may still need the guardian-delivery cache (contact lookup miss
|
|
58
|
+
// or null userFile falls through to the guardian file).
|
|
59
|
+
const GUARDIAN_REQUESTER_TRUST = {
|
|
60
|
+
sourceChannel: "vellum",
|
|
61
|
+
trustClass: "guardian",
|
|
62
|
+
requesterExternalUserId: "principal-123",
|
|
63
|
+
} as unknown as TrustContext;
|
|
62
64
|
|
|
63
65
|
describe("resolveInitialSystemPrompt", () => {
|
|
64
66
|
test("no construction-time identity: warms the guardian binding, then builds with no trust context", async () => {
|
|
@@ -72,7 +74,7 @@ describe("resolveInitialSystemPrompt", () => {
|
|
|
72
74
|
expect(builtWith).toEqual([undefined]);
|
|
73
75
|
});
|
|
74
76
|
|
|
75
|
-
test("channel-routed: threads the requester trust context and skips the guardian warm", async () => {
|
|
77
|
+
test("non-guardian channel-routed: threads the requester trust context and skips the guardian warm", async () => {
|
|
76
78
|
const { calls, builtWith, deps } = spyDeps();
|
|
77
79
|
const result = await resolveInitialSystemPrompt(
|
|
78
80
|
{ trustContext: REQUESTER_TRUST } as ConversationCreateOptions,
|
|
@@ -86,6 +88,33 @@ describe("resolveInitialSystemPrompt", () => {
|
|
|
86
88
|
expect(builtWith).toEqual([REQUESTER_TRUST]);
|
|
87
89
|
});
|
|
88
90
|
|
|
91
|
+
test("guardian-class background shape: warms the guardian binding, then builds with the trust context", async () => {
|
|
92
|
+
const { calls, builtWith, deps } = spyDeps();
|
|
93
|
+
const result = await resolveInitialSystemPrompt(
|
|
94
|
+
{ trustContext: GUARDIAN_BACKGROUND_TRUST } as ConversationCreateOptions,
|
|
95
|
+
deps,
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
expect(result).toBe("BUILD");
|
|
99
|
+
// Scheduled tasks and memory consolidation construct conversations in
|
|
100
|
+
// worker processes whose guardian-delivery cache starts cold; the warm
|
|
101
|
+
// MUST precede the build or the persona slot pins users/default.md.
|
|
102
|
+
expect(calls).toEqual(["warm", "build"]);
|
|
103
|
+
expect(builtWith).toEqual([GUARDIAN_BACKGROUND_TRUST]);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
test("guardian-class with requester identity (managed desktop): still warms before building", async () => {
|
|
107
|
+
const { calls, builtWith, deps } = spyDeps();
|
|
108
|
+
const result = await resolveInitialSystemPrompt(
|
|
109
|
+
{ trustContext: GUARDIAN_REQUESTER_TRUST } as ConversationCreateOptions,
|
|
110
|
+
deps,
|
|
111
|
+
);
|
|
112
|
+
|
|
113
|
+
expect(result).toBe("BUILD");
|
|
114
|
+
expect(calls).toEqual(["warm", "build"]);
|
|
115
|
+
expect(builtWith).toEqual([GUARDIAN_REQUESTER_TRUST]);
|
|
116
|
+
});
|
|
117
|
+
|
|
89
118
|
test("an explicit override is used verbatim and skips the warm and build", async () => {
|
|
90
119
|
const { calls, deps } = spyDeps();
|
|
91
120
|
const result = await resolveInitialSystemPrompt(
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import type { Database } from "bun:sqlite";
|
|
2
|
+
import { beforeEach, describe, expect, test } from "bun:test";
|
|
3
|
+
|
|
4
|
+
import { createConversation } from "../persistence/conversation-crud.js";
|
|
5
|
+
import { ensureGroupMigration } from "../persistence/conversation-group-migration.js";
|
|
6
|
+
import { listConversations } from "../persistence/conversation-queries.js";
|
|
7
|
+
import { getDb } from "../persistence/db-connection.js";
|
|
8
|
+
import { initializeDb } from "../persistence/db-init.js";
|
|
9
|
+
|
|
10
|
+
await initializeDb();
|
|
11
|
+
ensureGroupMigration();
|
|
12
|
+
|
|
13
|
+
function getRawDb(): Database {
|
|
14
|
+
return (getDb() as unknown as { $client: Database }).$client;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* `originChannel` is what lets each sidebar channel card fetch its own rows,
|
|
19
|
+
* and what lets the Chats card ask for the ones no channel claimed.
|
|
20
|
+
*
|
|
21
|
+
* The asymmetry under test: `origin_channel` is deliberately NULL at insert so
|
|
22
|
+
* an inbound message can claim the conversation for its channel, and migration
|
|
23
|
+
* 288 settles the unclaimed ones to 'vellum' at daemon startup. So NULL means
|
|
24
|
+
* "not yet attributed", and between one boot and the next it is what most rows
|
|
25
|
+
* carry. Reading `vellum` strictly would leave every one of them in no section
|
|
26
|
+
* at all: not native, not any channel.
|
|
27
|
+
*/
|
|
28
|
+
describe("listConversations originChannel filter", () => {
|
|
29
|
+
beforeEach(() => {
|
|
30
|
+
const raw = getRawDb();
|
|
31
|
+
raw.run("DELETE FROM conversations");
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
/** Set `origin_channel` directly; `createConversation` leaves it unset. */
|
|
35
|
+
function stamp(id: string, originChannel: string | null): void {
|
|
36
|
+
getRawDb().run("UPDATE conversations SET origin_channel = ? WHERE id = ?", [
|
|
37
|
+
originChannel,
|
|
38
|
+
id,
|
|
39
|
+
]);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function listedIds(originChannel: string): string[] {
|
|
43
|
+
return listConversations({ originChannel } as Parameters<
|
|
44
|
+
typeof listConversations
|
|
45
|
+
>[0]).map((c) => c.id);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
test("vellum matches rows that were never attributed", () => {
|
|
49
|
+
createConversation({ id: "conv-unattributed", source: "user" });
|
|
50
|
+
stamp("conv-unattributed", null);
|
|
51
|
+
|
|
52
|
+
expect(listedIds("vellum")).toEqual(["conv-unattributed"]);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test("vellum matches rows explicitly stamped vellum", () => {
|
|
56
|
+
createConversation({ id: "conv-native", source: "user" });
|
|
57
|
+
stamp("conv-native", "vellum");
|
|
58
|
+
|
|
59
|
+
expect(listedIds("vellum")).toEqual(["conv-native"]);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
/* The whole point: an unattributed row and a stamped native row are the same
|
|
63
|
+
section. Asserting both are returned together, rather than each alone,
|
|
64
|
+
is what fails if the predicate goes back to a strict equality. */
|
|
65
|
+
test("vellum returns unattributed and stamped rows together", () => {
|
|
66
|
+
createConversation({ id: "conv-native", source: "user" });
|
|
67
|
+
stamp("conv-native", "vellum");
|
|
68
|
+
createConversation({ id: "conv-unattributed", source: "user" });
|
|
69
|
+
stamp("conv-unattributed", null);
|
|
70
|
+
|
|
71
|
+
expect(listedIds("vellum").sort()).toEqual([
|
|
72
|
+
"conv-native",
|
|
73
|
+
"conv-unattributed",
|
|
74
|
+
]);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test("vellum does not claim a row belonging to a channel", () => {
|
|
78
|
+
createConversation({ id: "conv-slack", source: "user" });
|
|
79
|
+
stamp("conv-slack", "slack");
|
|
80
|
+
|
|
81
|
+
expect(listedIds("vellum")).toEqual([]);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
/* Only `vellum` is tolerant. A row is claimed for a channel by that channel
|
|
85
|
+
alone, so there is no ambiguity for the others to absorb - and a tolerant
|
|
86
|
+
`slack` would pull every unattributed row into the Slack card. */
|
|
87
|
+
test("a channel matches exactly and never absorbs unattributed rows", () => {
|
|
88
|
+
createConversation({ id: "conv-slack", source: "user" });
|
|
89
|
+
stamp("conv-slack", "slack");
|
|
90
|
+
createConversation({ id: "conv-unattributed", source: "user" });
|
|
91
|
+
stamp("conv-unattributed", null);
|
|
92
|
+
|
|
93
|
+
expect(listedIds("slack")).toEqual(["conv-slack"]);
|
|
94
|
+
});
|
|
95
|
+
});
|
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Nothing a Discord access-request decision sends may land in the guild
|
|
3
|
+
* channel the request came from.
|
|
4
|
+
*
|
|
5
|
+
* Two things travel on this path and both are meant for exactly one person: a
|
|
6
|
+
* decision notice, and a 6-digit verification code. Discord has no ephemeral
|
|
7
|
+
* message outside an interaction response, so anything posted back into the
|
|
8
|
+
* room is readable by every member of the server. That makes the code the
|
|
9
|
+
* sharper case: it is a live secret, and a `mint_outbound_session` is bound to
|
|
10
|
+
* the requester's account, so publishing it hands anyone watching a ten-minute
|
|
11
|
+
* window to spend it before the requester does.
|
|
12
|
+
*
|
|
13
|
+
* The assertions below are written as the failure shape rather than as
|
|
14
|
+
* expected addresses. A test that checks "the notice went to the user id"
|
|
15
|
+
* keeps passing when a *second* delivery is added that also posts to the
|
|
16
|
+
* channel; a test that checks no delivery anywhere carries the guild channel
|
|
17
|
+
* or the secret on a non-DM route does not.
|
|
18
|
+
*/
|
|
19
|
+
import { beforeEach, describe, expect, mock, test } from "bun:test";
|
|
20
|
+
|
|
21
|
+
mock.module("../config/env.js", () => ({
|
|
22
|
+
isHttpAuthDisabled: () => true,
|
|
23
|
+
getGatewayInternalBaseUrl: () => "http://127.0.0.1:7830",
|
|
24
|
+
}));
|
|
25
|
+
|
|
26
|
+
const emitSignalCalls: Array<Record<string, unknown>> = [];
|
|
27
|
+
mock.module("../notifications/emit-signal.js", () => ({
|
|
28
|
+
emitNotificationSignal: async (params: Record<string, unknown>) => {
|
|
29
|
+
emitSignalCalls.push(params);
|
|
30
|
+
return {
|
|
31
|
+
signalId: "mock-signal-id",
|
|
32
|
+
deduplicated: false,
|
|
33
|
+
dispatched: true,
|
|
34
|
+
reason: "mock",
|
|
35
|
+
deliveryResults: [],
|
|
36
|
+
};
|
|
37
|
+
},
|
|
38
|
+
}));
|
|
39
|
+
|
|
40
|
+
const deliverReplyCalls: Array<{
|
|
41
|
+
url: string;
|
|
42
|
+
payload: Record<string, unknown>;
|
|
43
|
+
}> = [];
|
|
44
|
+
mock.module("../runtime/gateway-client.js", () => ({
|
|
45
|
+
deliverChannelReply: async (
|
|
46
|
+
url: string,
|
|
47
|
+
payload: Record<string, unknown>,
|
|
48
|
+
) => {
|
|
49
|
+
deliverReplyCalls.push({ url, payload });
|
|
50
|
+
},
|
|
51
|
+
}));
|
|
52
|
+
|
|
53
|
+
const withdrawCalls: Array<Record<string, unknown>> = [];
|
|
54
|
+
mock.module("../approvals/guardian-card-withdrawal.js", () => ({
|
|
55
|
+
withdrawGuardianRequestCards: async (params: Record<string, unknown>) => {
|
|
56
|
+
withdrawCalls.push(params);
|
|
57
|
+
},
|
|
58
|
+
}));
|
|
59
|
+
|
|
60
|
+
import { createGuardianGatewaySim } from "./guardian-gateway-sim.js";
|
|
61
|
+
|
|
62
|
+
const sim = createGuardianGatewaySim();
|
|
63
|
+
mock.module("../channels/gateway-guardian-requests.js", () => sim.module);
|
|
64
|
+
|
|
65
|
+
import { applyGuardianDecision } from "../approvals/guardian-decision-primitive.js";
|
|
66
|
+
import type { ActorContext } from "../approvals/guardian-request-resolvers.js";
|
|
67
|
+
import { initializeDb } from "../persistence/db-init.js";
|
|
68
|
+
import { serializeRequesterSignals } from "../runtime/introduction-policy.js";
|
|
69
|
+
|
|
70
|
+
await initializeDb();
|
|
71
|
+
|
|
72
|
+
const TEST_PRINCIPAL_ID = "guardian-principal";
|
|
73
|
+
|
|
74
|
+
/** The public guild channel the request came from. */
|
|
75
|
+
const GUILD_CHANNEL = "700000000000000001";
|
|
76
|
+
/** The requester's own user snowflake. */
|
|
77
|
+
const REQUESTER = "900000000000000042";
|
|
78
|
+
/** The guardian's own user snowflake, when they decide from Discord. */
|
|
79
|
+
const GUARDIAN = "900000000000000007";
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* The reply callback a Discord inbound carries: it addresses the guild
|
|
83
|
+
* channel, which is precisely why a decision must not use it.
|
|
84
|
+
*/
|
|
85
|
+
const GUILD_REPLY_CALLBACK =
|
|
86
|
+
"http://127.0.0.1:7830/deliver/discord?threadId=700000000000000055";
|
|
87
|
+
|
|
88
|
+
function resetState(): void {
|
|
89
|
+
sim.reset();
|
|
90
|
+
emitSignalCalls.length = 0;
|
|
91
|
+
deliverReplyCalls.length = 0;
|
|
92
|
+
withdrawCalls.length = 0;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function desktopGuardian(): ActorContext {
|
|
96
|
+
return {
|
|
97
|
+
actorPrincipalId: TEST_PRINCIPAL_ID,
|
|
98
|
+
actorExternalUserId: undefined,
|
|
99
|
+
channel: "vellum",
|
|
100
|
+
guardianPrincipalId: TEST_PRINCIPAL_ID,
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** A guardian who decided from Telegram about a Discord requester. */
|
|
105
|
+
function telegramGuardian(): ActorContext {
|
|
106
|
+
return {
|
|
107
|
+
actorPrincipalId: TEST_PRINCIPAL_ID,
|
|
108
|
+
actorExternalUserId: "telegram-guardian-1",
|
|
109
|
+
channel: "telegram",
|
|
110
|
+
guardianPrincipalId: TEST_PRINCIPAL_ID,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** A guardian who decided by replying in the Discord guild channel. */
|
|
115
|
+
function discordGuardian(): ActorContext {
|
|
116
|
+
return {
|
|
117
|
+
actorPrincipalId: TEST_PRINCIPAL_ID,
|
|
118
|
+
actorExternalUserId: GUARDIAN,
|
|
119
|
+
channel: "discord",
|
|
120
|
+
guardianPrincipalId: TEST_PRINCIPAL_ID,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function makeDiscordAccessRequest() {
|
|
125
|
+
return sim.seedRequest({
|
|
126
|
+
kind: "access_request",
|
|
127
|
+
sourceChannel: "discord",
|
|
128
|
+
sourceConversationId: "access-req-conv",
|
|
129
|
+
requesterExternalUserId: REQUESTER,
|
|
130
|
+
requesterChatId: GUILD_CHANNEL,
|
|
131
|
+
guardianPrincipalId: TEST_PRINCIPAL_ID,
|
|
132
|
+
toolName: "ingress_access_request",
|
|
133
|
+
requesterSignals: serializeRequesterSignals({ isStranger: true }),
|
|
134
|
+
expiresAt: Date.now() + 60_000,
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** Every chatId any delivery was addressed to. */
|
|
139
|
+
function deliveredChatIds(): string[] {
|
|
140
|
+
return deliverReplyCalls.map((c) => String(c.payload.chatId ?? ""));
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Whether a delivery went out on a route that resolves to a DM.
|
|
145
|
+
*
|
|
146
|
+
* `dm=1` is the marker the Discord transport reads to treat `chatId` as a
|
|
147
|
+
* recipient to open a DM with rather than a channel to post in, so a delivery
|
|
148
|
+
* without it is a delivery into a room.
|
|
149
|
+
*/
|
|
150
|
+
function isDmRoute(url: string): boolean {
|
|
151
|
+
return url.includes("dm=1");
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
describe("Discord access-request decisions stay out of the guild channel", () => {
|
|
155
|
+
beforeEach(() => resetState());
|
|
156
|
+
|
|
157
|
+
test("an on-channel approval never posts the code or the notice to the room", async () => {
|
|
158
|
+
const req = makeDiscordAccessRequest();
|
|
159
|
+
|
|
160
|
+
const result = await applyGuardianDecision({
|
|
161
|
+
requestId: req.id,
|
|
162
|
+
action: "verify_code",
|
|
163
|
+
actorContext: discordGuardian(),
|
|
164
|
+
channelDeliveryContext: {
|
|
165
|
+
replyCallbackUrl: GUILD_REPLY_CALLBACK,
|
|
166
|
+
guardianChatId: GUILD_CHANNEL,
|
|
167
|
+
assistantId: "asst-test",
|
|
168
|
+
},
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
expect(result.applied).toBe(true);
|
|
172
|
+
const secret = sim.state.mintedSecret;
|
|
173
|
+
expect(secret).toBeTruthy();
|
|
174
|
+
|
|
175
|
+
// Nothing was addressed to the room.
|
|
176
|
+
expect(deliveredChatIds()).not.toContain(GUILD_CHANNEL);
|
|
177
|
+
|
|
178
|
+
// Nothing carrying the secret travelled on a non-DM route. This is the
|
|
179
|
+
// assertion that fails if the in-band reply context is ever used again:
|
|
180
|
+
// the guild callback carries no `dm=1`.
|
|
181
|
+
for (const call of deliverReplyCalls) {
|
|
182
|
+
const text = String(call.payload.text ?? "");
|
|
183
|
+
if (text.includes(secret)) {
|
|
184
|
+
expect(isDmRoute(call.url)).toBe(true);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// The guardian who approved still gets their copy of the code. Without
|
|
189
|
+
// this they would approve and never receive the thing they approved.
|
|
190
|
+
const guardianCode = deliverReplyCalls.find(
|
|
191
|
+
(c) =>
|
|
192
|
+
c.payload.chatId === GUARDIAN &&
|
|
193
|
+
String(c.payload.text ?? "").includes(secret),
|
|
194
|
+
);
|
|
195
|
+
expect(guardianCode).toBeDefined();
|
|
196
|
+
expect(isDmRoute(guardianCode?.url ?? "")).toBe(true);
|
|
197
|
+
|
|
198
|
+
// And the requester is told, on a DM route.
|
|
199
|
+
const requesterNotice = deliverReplyCalls.find(
|
|
200
|
+
(c) => c.payload.chatId === REQUESTER,
|
|
201
|
+
);
|
|
202
|
+
expect(requesterNotice).toBeDefined();
|
|
203
|
+
expect(isDmRoute(requesterNotice?.url ?? "")).toBe(true);
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
test("a desktop approval reaches the requester rather than failing silently", async () => {
|
|
207
|
+
// The regression the DM route introduced: the deliver URL says `dm=1`, so
|
|
208
|
+
// passing the guild channel as chatId asks Discord to open a DM with a
|
|
209
|
+
// channel. That call fails and the requester is never told.
|
|
210
|
+
const req = makeDiscordAccessRequest();
|
|
211
|
+
|
|
212
|
+
const result = await applyGuardianDecision({
|
|
213
|
+
requestId: req.id,
|
|
214
|
+
action: "verify_code",
|
|
215
|
+
actorContext: desktopGuardian(),
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
expect(result.applied).toBe(true);
|
|
219
|
+
expect(deliveredChatIds()).not.toContain(GUILD_CHANNEL);
|
|
220
|
+
|
|
221
|
+
const requesterDelivery = deliverReplyCalls.find(
|
|
222
|
+
(c) => c.payload.chatId === REQUESTER,
|
|
223
|
+
);
|
|
224
|
+
expect(requesterDelivery).toBeDefined();
|
|
225
|
+
expect(isDmRoute(requesterDelivery?.url ?? "")).toBe(true);
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
test("an on-channel denial never posts to the room", async () => {
|
|
229
|
+
const req = makeDiscordAccessRequest();
|
|
230
|
+
|
|
231
|
+
const result = await applyGuardianDecision({
|
|
232
|
+
requestId: req.id,
|
|
233
|
+
action: "block",
|
|
234
|
+
actorContext: discordGuardian(),
|
|
235
|
+
channelDeliveryContext: {
|
|
236
|
+
replyCallbackUrl: GUILD_REPLY_CALLBACK,
|
|
237
|
+
guardianChatId: GUILD_CHANNEL,
|
|
238
|
+
assistantId: "asst-test",
|
|
239
|
+
},
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
expect(result.applied).toBe(true);
|
|
243
|
+
expect(deliveredChatIds()).not.toContain(GUILD_CHANNEL);
|
|
244
|
+
|
|
245
|
+
const requesterNotice = deliverReplyCalls.find(
|
|
246
|
+
(c) => c.payload.chatId === REQUESTER,
|
|
247
|
+
);
|
|
248
|
+
expect(requesterNotice).toBeDefined();
|
|
249
|
+
expect(isDmRoute(requesterNotice?.url ?? "")).toBe(true);
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
test("a guardian on another channel still gets their copy of the code", async () => {
|
|
253
|
+
// The requester's route is suppressed because a Discord guild channel has
|
|
254
|
+
// no one reader; the guardian's is not, because theirs is a private
|
|
255
|
+
// Telegram chat. Suppressing both would leave the guardian with no
|
|
256
|
+
// confirmation and no code, so the approval would read as a no-op.
|
|
257
|
+
const req = makeDiscordAccessRequest();
|
|
258
|
+
const TELEGRAM_CALLBACK = "http://127.0.0.1:7830/deliver/telegram";
|
|
259
|
+
|
|
260
|
+
const result = await applyGuardianDecision({
|
|
261
|
+
requestId: req.id,
|
|
262
|
+
action: "verify_code",
|
|
263
|
+
actorContext: telegramGuardian(),
|
|
264
|
+
channelDeliveryContext: {
|
|
265
|
+
replyCallbackUrl: TELEGRAM_CALLBACK,
|
|
266
|
+
guardianChatId: "telegram-chat-1",
|
|
267
|
+
assistantId: "asst-test",
|
|
268
|
+
},
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
expect(result.applied).toBe(true);
|
|
272
|
+
const secret = sim.state.mintedSecret;
|
|
273
|
+
|
|
274
|
+
const guardianCode = deliverReplyCalls.find(
|
|
275
|
+
(c) =>
|
|
276
|
+
c.payload.chatId === "telegram-chat-1" &&
|
|
277
|
+
String(c.payload.text ?? "").includes(secret),
|
|
278
|
+
);
|
|
279
|
+
expect(guardianCode).toBeDefined();
|
|
280
|
+
// Their code goes to their own channel, never the requester's transport.
|
|
281
|
+
expect(guardianCode?.url).toBe(TELEGRAM_CALLBACK);
|
|
282
|
+
|
|
283
|
+
// The requester is still reached privately, on Discord's own route.
|
|
284
|
+
const requesterDelivery = deliverReplyCalls.find(
|
|
285
|
+
(c) => c.payload.chatId === REQUESTER,
|
|
286
|
+
);
|
|
287
|
+
expect(requesterDelivery).toBeDefined();
|
|
288
|
+
expect(isDmRoute(requesterDelivery?.url ?? "")).toBe(true);
|
|
289
|
+
expect(deliveredChatIds()).not.toContain(GUILD_CHANNEL);
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
test("the requester gets the code itself, now that a DM reply is heard", async () => {
|
|
293
|
+
// The DM ingress lane lands with this change, so a code sent with "reply
|
|
294
|
+
// with it here" can actually be answered. Before the lane existed the
|
|
295
|
+
// requester got a courier notice instead, because a code they could not
|
|
296
|
+
// spend is worse than one the guardian relays.
|
|
297
|
+
const req = makeDiscordAccessRequest();
|
|
298
|
+
|
|
299
|
+
const result = await applyGuardianDecision({
|
|
300
|
+
requestId: req.id,
|
|
301
|
+
action: "verify_code",
|
|
302
|
+
actorContext: desktopGuardian(),
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
expect(result.applied).toBe(true);
|
|
306
|
+
const secret = sim.state.mintedSecret;
|
|
307
|
+
|
|
308
|
+
const codeToRequester = deliverReplyCalls.find(
|
|
309
|
+
(c) =>
|
|
310
|
+
c.payload.chatId === REQUESTER &&
|
|
311
|
+
String(c.payload.text ?? "").includes(secret),
|
|
312
|
+
);
|
|
313
|
+
expect(codeToRequester).toBeDefined();
|
|
314
|
+
// Still only ever on the DM route.
|
|
315
|
+
expect(isDmRoute(codeToRequester?.url ?? "")).toBe(true);
|
|
316
|
+
expect(deliveredChatIds()).not.toContain(GUILD_CHANNEL);
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
test("a denial still records the guardian-facing lifecycle signal", async () => {
|
|
320
|
+
// The signal used to be gated on the in-band reply context, which the
|
|
321
|
+
// per-reader suppression removes for Discord. Losing it would mean a
|
|
322
|
+
// Discord denial left no record for the guardian at all.
|
|
323
|
+
const req = makeDiscordAccessRequest();
|
|
324
|
+
|
|
325
|
+
await applyGuardianDecision({
|
|
326
|
+
requestId: req.id,
|
|
327
|
+
action: "block",
|
|
328
|
+
actorContext: discordGuardian(),
|
|
329
|
+
channelDeliveryContext: {
|
|
330
|
+
replyCallbackUrl: GUILD_REPLY_CALLBACK,
|
|
331
|
+
guardianChatId: GUILD_CHANNEL,
|
|
332
|
+
assistantId: "asst-test",
|
|
333
|
+
},
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
const decisionSignals = emitSignalCalls.filter(
|
|
337
|
+
(c) => c.sourceEventName === "ingress.trusted_contact.guardian_decision",
|
|
338
|
+
);
|
|
339
|
+
expect(decisionSignals).toHaveLength(1);
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
test("no Discord delivery is ever addressed to the originating conversation", async () => {
|
|
343
|
+
// The umbrella invariant, swept across every decision this resolver takes,
|
|
344
|
+
// so a newly added action cannot quietly reintroduce the leak.
|
|
345
|
+
for (const action of ["verify_code", "block", "leave_unverified"] as const) {
|
|
346
|
+
resetState();
|
|
347
|
+
const req = makeDiscordAccessRequest();
|
|
348
|
+
|
|
349
|
+
await applyGuardianDecision({
|
|
350
|
+
requestId: req.id,
|
|
351
|
+
action,
|
|
352
|
+
actorContext: discordGuardian(),
|
|
353
|
+
channelDeliveryContext: {
|
|
354
|
+
replyCallbackUrl: GUILD_REPLY_CALLBACK,
|
|
355
|
+
guardianChatId: GUILD_CHANNEL,
|
|
356
|
+
assistantId: "asst-test",
|
|
357
|
+
},
|
|
358
|
+
});
|
|
359
|
+
|
|
360
|
+
expect(deliveredChatIds()).not.toContain(GUILD_CHANNEL);
|
|
361
|
+
for (const call of deliverReplyCalls) {
|
|
362
|
+
expect(isDmRoute(call.url)).toBe(true);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
});
|
|
366
|
+
});
|