@vellumai/assistant 0.4.15 → 0.4.17

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 (73) hide show
  1. package/Dockerfile +6 -6
  2. package/README.md +1 -2
  3. package/package.json +1 -1
  4. package/src/__tests__/approval-routes-http.test.ts +383 -254
  5. package/src/__tests__/call-controller.test.ts +1074 -751
  6. package/src/__tests__/call-routes-http.test.ts +329 -279
  7. package/src/__tests__/channel-approval-routes.test.ts +2 -13
  8. package/src/__tests__/channel-approvals.test.ts +227 -182
  9. package/src/__tests__/channel-guardian.test.ts +1 -0
  10. package/src/__tests__/conversation-attention-telegram.test.ts +157 -114
  11. package/src/__tests__/conversation-routes-guardian-reply.test.ts +164 -104
  12. package/src/__tests__/conversation-routes.test.ts +71 -41
  13. package/src/__tests__/daemon-server-session-init.test.ts +258 -191
  14. package/src/__tests__/deterministic-verification-control-plane.test.ts +183 -134
  15. package/src/__tests__/extract-email.test.ts +42 -0
  16. package/src/__tests__/gateway-only-enforcement.test.ts +467 -368
  17. package/src/__tests__/gateway-only-guard.test.ts +54 -55
  18. package/src/__tests__/gmail-integration.test.ts +48 -46
  19. package/src/__tests__/guardian-action-followup-executor.test.ts +215 -150
  20. package/src/__tests__/guardian-outbound-http.test.ts +334 -208
  21. package/src/__tests__/guardian-routing-invariants.test.ts +680 -613
  22. package/src/__tests__/guardian-routing-state.test.ts +257 -209
  23. package/src/__tests__/guardian-verification-voice-binding.test.ts +47 -40
  24. package/src/__tests__/handle-user-message-secret-resume.test.ts +44 -21
  25. package/src/__tests__/handlers-user-message-approval-consumption.test.ts +269 -195
  26. package/src/__tests__/inbound-invite-redemption.test.ts +194 -151
  27. package/src/__tests__/ingress-reconcile.test.ts +184 -142
  28. package/src/__tests__/non-member-access-request.test.ts +291 -247
  29. package/src/__tests__/notification-telegram-adapter.test.ts +60 -46
  30. package/src/__tests__/pairing-concurrent.test.ts +78 -0
  31. package/src/__tests__/recording-intent-handler.test.ts +422 -291
  32. package/src/__tests__/runtime-attachment-metadata.test.ts +107 -69
  33. package/src/__tests__/runtime-events-sse.test.ts +67 -50
  34. package/src/__tests__/send-endpoint-busy.test.ts +314 -232
  35. package/src/__tests__/session-approval-overrides.test.ts +93 -91
  36. package/src/__tests__/sms-messaging-provider.test.ts +74 -47
  37. package/src/__tests__/trusted-contact-approval-notifier.test.ts +339 -274
  38. package/src/__tests__/trusted-contact-inline-approval-integration.test.ts +484 -372
  39. package/src/__tests__/trusted-contact-lifecycle-notifications.test.ts +261 -239
  40. package/src/__tests__/trusted-contact-multichannel.test.ts +179 -140
  41. package/src/__tests__/twilio-config.test.ts +49 -41
  42. package/src/__tests__/twilio-routes-elevenlabs.test.ts +189 -162
  43. package/src/__tests__/twilio-routes.test.ts +389 -280
  44. package/src/calls/call-controller.ts +1 -1
  45. package/src/calls/guardian-action-sweep.ts +6 -6
  46. package/src/calls/twilio-routes.ts +2 -4
  47. package/src/config/bundled-skills/chatgpt-import/tools/chatgpt-import.ts +29 -4
  48. package/src/config/bundled-skills/messaging/SKILL.md +5 -4
  49. package/src/config/bundled-skills/messaging/tools/messaging-reply.ts +69 -4
  50. package/src/config/env.ts +39 -29
  51. package/src/daemon/handlers/config-inbox.ts +5 -5
  52. package/src/daemon/handlers/skills.ts +18 -10
  53. package/src/daemon/ipc-contract/messages.ts +1 -0
  54. package/src/daemon/ipc-contract/surfaces.ts +7 -1
  55. package/src/daemon/pairing-store.ts +15 -2
  56. package/src/daemon/session-agent-loop-handlers.ts +5 -0
  57. package/src/daemon/session-agent-loop.ts +1 -1
  58. package/src/daemon/session-process.ts +1 -1
  59. package/src/daemon/session-slash.ts +4 -4
  60. package/src/daemon/session-surfaces.ts +42 -2
  61. package/src/runtime/auth/token-service.ts +95 -45
  62. package/src/runtime/channel-retry-sweep.ts +2 -2
  63. package/src/runtime/http-server.ts +8 -7
  64. package/src/runtime/http-types.ts +1 -1
  65. package/src/runtime/routes/conversation-routes.ts +1 -1
  66. package/src/runtime/routes/guardian-bootstrap-routes.ts +3 -2
  67. package/src/runtime/routes/guardian-expiry-sweep.ts +5 -5
  68. package/src/runtime/routes/pairing-routes.ts +4 -1
  69. package/src/sequence/reply-matcher.ts +14 -4
  70. package/src/skills/frontmatter.ts +9 -6
  71. package/src/tools/ui-surface/definitions.ts +3 -1
  72. package/src/util/platform.ts +0 -12
  73. package/docs/architecture/http-token-refresh.md +0 -274
@@ -267,18 +267,6 @@ export function readSessionToken(): string | null {
267
267
  }
268
268
  }
269
269
 
270
- /**
271
- * Read the runtime HTTP bearer token from disk. Returns null if the
272
- * file doesn't exist or can't be read (HTTP server not running).
273
- */
274
- export function readHttpToken(): string | null {
275
- try {
276
- return readFileSync(getHttpTokenPath(), 'utf-8').trim();
277
- } catch {
278
- return null;
279
- }
280
- }
281
-
282
270
  function expandHomePath(p: string): string {
283
271
  if (p === '~') return homedir();
284
272
  if (p.startsWith('~/')) return join(homedir(), p.slice(2));
@@ -1,274 +0,0 @@
1
- # HTTP Token Refresh Protocol
2
-
3
- Design for how the daemon notifies clients of bearer token rotation and how clients recover from stale tokens.
4
-
5
- ## Current State
6
-
7
- The daemon's HTTP bearer token is resolved at startup and persisted to `~/.vellum/http-token` (mode 0600). The startup token resolution order is: (1) the `RUNTIME_PROXY_BEARER_TOKEN` env var if set, (2) the existing token read from `~/.vellum/http-token` if the file is readable and non-empty, (3) a newly generated random token as a last resort. Clients read this file at connection time:
8
-
9
- - **macOS (local)**: Reads `~/.vellum/http-token` from disk via `resolveHttpTokenPath()` / `readHttpToken()`. Has direct filesystem access to the token file.
10
- - **iOS (remote)**: Receives the bearer token during the QR-code pairing flow. The token is stored in the iOS Keychain and used for all subsequent HTTP/SSE requests.
11
- - **Chrome extension**: User manually pastes the token from `~/.vellum/http-token` into the extension popup.
12
-
13
- Token regeneration today (macOS Settings > Connect > Regenerate Bearer Token):
14
- 1. macOS client writes a new random token to `~/.vellum/http-token`.
15
- 2. macOS client kills the daemon process.
16
- 3. The health monitor restarts the daemon, which reads the new token from disk.
17
- 4. macOS client re-reads the token from disk on next health check.
18
- 5. **iOS clients are broken** -- they still hold the old token and get 401s. The only recovery is to re-pair via QR code.
19
-
20
- ## Problem
21
-
22
- When the bearer token is rotated (manually or programmatically), remote clients (iOS, Chrome extension) have no way to learn about the new token. They receive 401 responses and cannot recover without manual re-configuration.
23
-
24
- ## Design
25
-
26
- ### 1. SSE Token Rotation Event
27
-
28
- When the daemon detects that its bearer token has changed, it emits a `token_rotated` SSE event to all connected clients **before** the old token is invalidated. This gives clients a window to capture the new token and seamlessly reconnect.
29
-
30
- **Event format** (delivered as an `AssistantEvent` envelope wrapping a new `ServerMessage` type):
31
-
32
- ```typescript
33
- // New ServerMessage variant
34
- interface TokenRotatedMessage {
35
- type: 'token_rotated';
36
- newToken: string; // The replacement bearer token
37
- expiresOldAt: number; // Unix timestamp (ms) -- old token stops working after this
38
- }
39
- ```
40
-
41
- **Grace period**: The daemon accepts both old and new tokens for a configurable grace window (default: 30 seconds) after emitting the event. This gives slow clients time to process the event and switch tokens. After the grace period, only the new token is valid.
42
-
43
- **Sequence diagram (routine rotation)**:
44
-
45
- ```mermaid
46
- sequenceDiagram
47
- participant Trigger as Rotation Trigger
48
- participant Daemon as Daemon
49
- participant SSE as SSE Stream
50
- participant Client as iOS / Chrome Client
51
-
52
- Trigger->>Daemon: Rotate token (manual, API, periodic)
53
- Daemon->>Daemon: Generate new token, write to ~/.vellum/http-token
54
- Daemon->>Daemon: Enter grace period (accept old + new)
55
- Daemon->>SSE: Emit token_rotated {newToken, expiresOldAt}
56
- SSE->>Client: token_rotated event
57
- Client->>Client: Store new token (Keychain / localStorage)
58
- Client->>Client: Update Authorization header
59
- Client->>Daemon: Reconnect SSE with new token
60
- Note over Daemon: Grace period expires
61
- Daemon->>Daemon: Reject old token (401)
62
- ```
63
-
64
- **Sequence diagram (revocation rotation)**:
65
-
66
- ```mermaid
67
- sequenceDiagram
68
- participant Trigger as Security Event
69
- participant Daemon as Daemon
70
- participant SSE as SSE Stream
71
- participant Client as iOS / Chrome Client
72
-
73
- Trigger->>Daemon: Rotate token (revoke: true)
74
- Daemon->>Daemon: Generate new token, immediately invalidate old token
75
- Daemon->>SSE: Terminate all old-token connections
76
- Daemon->>Daemon: Write new token to ~/.vellum/http-token
77
- Note over SSE: No token_rotated event emitted
78
- Client->>Daemon: Next request with old token → 401
79
- Client->>Client: Trigger fallback recovery (re-pair / re-read / re-paste)
80
- ```
81
-
82
- ### 2. Client 401 Recovery (Stale Token Detection)
83
-
84
- If a client misses the SSE event (network partition, app backgrounded, SSE disconnected at rotation time), it needs a fallback recovery mechanism.
85
-
86
- **401 response handling**:
87
-
88
- When a client receives a `401 Unauthorized` response:
89
-
90
- 1. **macOS (local)**: Re-reads `~/.vellum/http-token` from disk. If the token differs from the in-memory token, updates and retries. This already works implicitly since macOS re-reads the token on most HTTP calls via `resolveLocalDaemonHTTPEndpoint()`.
91
-
92
- 2. **iOS (remote)**: Cannot read the token file. Must re-pair via QR code. The 401 response triggers the client to surface a "Session expired -- re-pair required" UI prompt. This is the expected behavior when the SSE notification is missed.
93
-
94
- 3. **Chrome extension**: Surfaces an error message directing the user to paste the new token.
95
-
96
- **Retry logic**: Clients should retry at most once after a 401 before surfacing the error UI. This prevents retry storms during legitimate auth failures (wrong token, not just stale).
97
-
98
- ### 3. Token Rotation Triggers
99
-
100
- The token can be rotated via:
101
-
102
- | Trigger | Description | Current | Proposed | Mode |
103
- |---------|-------------|---------|----------|------|
104
- | Manual (macOS Settings) | User clicks "Regenerate Bearer Token" | Yes (kills daemon) | Graceful rotation via daemon API | Routine |
105
- | API endpoint | `POST /v1/auth/rotate-token` | No | New endpoint | Routine (default) or Revocation (`revoke: true`) |
106
- | Periodic rotation | Automatic rotation on a configurable schedule | No | Future consideration | Routine |
107
- | Security event | Forced rotation after suspicious activity | No | Future consideration | Revocation |
108
-
109
- **`POST /v1/auth/rotate-token`** (new endpoint):
110
-
111
- Allows programmatic token rotation without restarting the daemon.
112
-
113
- Request body (optional): `{ "revoke": boolean }` (default: `false`).
114
-
115
- **Routine mode** (`revoke: false`, default):
116
- 1. Generates a new random token.
117
- 2. Writes it to `~/.vellum/http-token`.
118
- 3. Emits the `token_rotated` SSE event with grace period.
119
- 4. Starts accepting both tokens during the grace period.
120
- 5. After grace period, rejects the old token.
121
-
122
- **Revocation mode** (`revoke: true`):
123
- 1. Generates a new random token.
124
- 2. Immediately invalidates the old token in memory (no grace period).
125
- 3. Terminates all SSE connections authenticated with the old token.
126
- 4. Writes the new token to `~/.vellum/http-token`.
127
- 5. Does **not** emit `token_rotated` -- the new token is never sent to old-token sessions.
128
- 6. Clients must recover via their platform-specific fallback (re-read from disk, re-pair, or re-paste).
129
-
130
- This eliminates the current "kill and restart" approach for token rotation.
131
-
132
- ### 4. Daemon-Side Implementation
133
-
134
- **Grace period token validation**: During routine rotation, `verifyBearerToken()` accepts either the old or new token. The `RuntimeHttpServer` holds both tokens:
135
-
136
- ```typescript
137
- // Conceptual extension to RuntimeHttpServer
138
- private currentToken: string;
139
- private previousToken: string | null = null;
140
- private graceDeadline: number | null = null;
141
-
142
- // Modified auth check
143
- private isValidToken(provided: string): boolean {
144
- if (verifyBearerToken(provided, this.currentToken)) return true;
145
- if (this.previousToken && this.graceDeadline && Date.now() < this.graceDeadline) {
146
- return verifyBearerToken(provided, this.previousToken);
147
- }
148
- return false;
149
- }
150
-
151
- // Rotation has two ordering strategies depending on revoke mode:
152
- // - Revocation: invalidate in-memory FIRST (security-critical), then persist.
153
- // A disk write failure must never leave a compromised token valid.
154
- // - Routine: persist to disk FIRST, then update in-memory state.
155
- // A disk write failure aborts rotation — clients keep the old token
156
- // rather than being locked out by an in-memory-only switch.
157
- private rotateToken(revoke: boolean): string {
158
- const newToken = generateToken();
159
-
160
- if (revoke) {
161
- // Revocation: invalidate the compromised token immediately.
162
- // Even if the disk write below fails, the old token is gone from memory.
163
- this.currentToken = newToken;
164
- this.previousToken = null;
165
- this.graceDeadline = null;
166
- this.terminateOldTokenSSEConnections();
167
- writeTokenToDisk(newToken);
168
- } else {
169
- // Routine: persist to disk first — if this throws, auth state is untouched
170
- writeTokenToDisk(newToken);
171
- this.previousToken = this.currentToken;
172
- this.currentToken = newToken;
173
- this.graceDeadline = Date.now() + GRACE_PERIOD_MS;
174
- this.emitTokenRotatedEvent(newToken, this.graceDeadline);
175
- }
176
- return newToken;
177
- }
178
- ```
179
-
180
- **Failure semantics — routine vs. revocation**:
181
-
182
- The two rotation modes have deliberately different failure ordering to match their security requirements:
183
-
184
- | | Routine (`revoke: false`) | Revocation (`revoke: true`) |
185
- |---|---|---|
186
- | **Order** | Persist to disk first, then update in-memory state | Update in-memory state first, then persist to disk |
187
- | **Disk write failure** | Rotation aborts cleanly — in-memory auth state is untouched, clients keep working with the old token | Old token is already invalidated in memory; the API endpoint returns an error to the caller |
188
- | **Rationale** | Availability: don't lock out clients if persistence fails | Security: a potentially compromised token must never remain valid, even briefly |
189
-
190
- **Revocation disk-write failure in detail**: If `writeTokenToDisk` throws after the in-memory switch during revocation, the system enters a degraded state:
191
-
192
- 1. **In-memory state**: `currentToken` holds the new (unpersisted) token. The old token is rejected. All old-token SSE connections have been terminated.
193
- 2. **Disk state**: `~/.vellum/http-token` still contains the old (now-invalid) token.
194
- 3. **API response**: The `POST /v1/auth/rotate-token` endpoint returns an error indicating the persistence failure. The response body includes the new token so the caller can manually persist or distribute it if needed.
195
- 4. **Client impact by platform**:
196
- - **macOS**: Re-reading the token file yields the stale old token, which is rejected (401). Recovery requires a daemon restart (which generates a fresh token and persists it) or a successful retry of the rotation API call.
197
- - **iOS**: Already disconnected (old-token SSE terminated). Cannot recover until the daemon restarts or the rotation is retried successfully, at which point re-pairing is required.
198
- - **Chrome extension**: Same as iOS — the pasted token is stale and rejected.
199
- 5. **Daemon restart recovery**: A daemon restart does **not** automatically heal this state. At startup, the daemon first checks for the `RUNTIME_PROXY_BEARER_TOKEN` env var, then tries to read the existing token from `~/.vellum/http-token`, and only generates a new random token if both are unavailable (see `assistant/src/daemon/lifecycle.ts`, lines 110-124). In the degraded state described here — where the disk still holds the old (now-invalid) token — a restart would reload that stale token, making it the active bearer token again. To actually recover, the operator must either: (a) manually delete or overwrite `~/.vellum/http-token` before restarting the daemon, (b) set `RUNTIME_PROXY_BEARER_TOKEN` to a known-good value, or (c) successfully retry the `POST /v1/auth/rotate-token` endpoint while the daemon is still running with the new in-memory token.
200
- 6. **Why this is acceptable**: Revocation is a security-critical operation triggered when the old token is suspected compromised. The invariant — "a compromised token must not remain valid" — takes precedence over client convenience. The degraded state requires manual intervention but disk write failures are rare in practice (permissions, disk full), and the API response includes the new token so the caller can retry or manually persist it.
201
-
202
- **SSE event emission** (routine rotation only): The `token_rotated` event is published to `assistantEventHub` as a `ServerMessage`, reaching all connected SSE subscribers across all conversations. This event is never emitted during revocation rotations.
203
-
204
- ### 5. iOS Client Implementation
205
-
206
- **SSE event handler** (in `HTTPTransport`):
207
-
208
- ```swift
209
- // In parseSSEData, handle the new message type
210
- case .tokenRotated(let msg):
211
- // Persist the new token to Keychain
212
- DaemonConfigStore.shared.updateBearerToken(msg.newToken)
213
- // Update in-memory token
214
- self.bearerToken = msg.newToken
215
- // Reconnect SSE with the new token
216
- self.stopSSE()
217
- self.startSSE()
218
- ```
219
-
220
- **401 response handler**:
221
-
222
- ```swift
223
- // In any HTTP request that receives 401
224
- if http.statusCode == 401 {
225
- // Token is stale and we missed the rotation event
226
- // Surface re-pairing UI
227
- onMessage?(.sessionError(SessionErrorMessage(
228
- sessionId: sessionId,
229
- code: .authenticationRequired,
230
- userMessage: "Session expired. Please re-pair your device.",
231
- retryable: false
232
- )))
233
- }
234
- ```
235
-
236
- ### 6. Security Considerations
237
-
238
- - **Rotation modes**: Token rotation has two distinct modes with different security requirements:
239
-
240
- 1. **Routine rotation** (manual refresh, periodic schedule): The old token is not compromised -- the goal is seamless credential rollover. The SSE `token_rotated` event delivers the new token to connected clients, and the grace period allows them to transition. This is safe because the SSE channel is authenticated, and any session holding the old token is a legitimate client.
241
-
242
- 2. **Revocation rotation** (security event, suspected compromise): The old token may be in the hands of an attacker. In this mode, the daemon **must not** push the replacement token to old-token SSE sessions -- doing so would hand the new credential to the very sessions being revoked. Instead:
243
- - The daemon immediately invalidates the old token (no grace period).
244
- - All SSE connections authenticated with the old token are terminated.
245
- - The `POST /v1/auth/rotate-token` endpoint accepts an optional `revoke: true` flag to select this mode.
246
- - Legitimate clients recover via their fallback path: macOS re-reads `~/.vellum/http-token` from disk; iOS prompts for re-pairing; Chrome extension prompts for a new token.
247
-
248
- The `token_rotated` SSE event is only emitted during routine rotations. The rotation trigger determines the mode.
249
-
250
- - **Grace period length**: 30 seconds is long enough for clients to process the event but short enough to limit the window where both tokens are valid. Only applies to routine rotations.
251
- - **No token in logs**: The `token_rotated` event payload must be excluded from any server-side event logging. Use the existing log-redaction patterns.
252
- - **Constant-time comparison**: The existing `verifyBearerToken()` using `timingSafeEqual` continues to be used for both old and new token checks during the grace period.
253
-
254
- ### 7. Migration Path
255
-
256
- This design is additive and backward-compatible:
257
-
258
- 1. **Phase 1**: Add `POST /v1/auth/rotate-token` endpoint and `token_rotated` SSE event to the daemon. Update macOS Settings to call the API endpoint instead of kill-and-restart.
259
- 2. **Phase 2**: Add `token_rotated` handler to `HTTPTransport.swift` (shared between macOS and iOS). Add 401 retry-once logic.
260
- 3. **Phase 3** (future): Add periodic rotation and security-event-triggered rotation.
261
-
262
- Clients that do not understand the `token_rotated` event will simply ignore it (SSE events with unknown types are safe to skip). They will eventually get 401s after the grace period and fall back to their existing recovery path (re-read from disk for macOS, re-pair for iOS).
263
-
264
- ## Key Files
265
-
266
- | File | Role |
267
- |------|------|
268
- | `assistant/src/runtime/http-server.ts` | Auth check, grace period logic, rotation endpoint |
269
- | `assistant/src/runtime/middleware/auth.ts` | `verifyBearerToken()` -- constant-time token comparison |
270
- | `assistant/src/runtime/assistant-event.ts` | `AssistantEvent` envelope, SSE framing |
271
- | `assistant/src/daemon/lifecycle.ts` | Token generation and persistence at startup |
272
- | `clients/shared/IPC/HTTPDaemonClient.swift` | `HTTPTransport` -- SSE stream, 401 handling |
273
- | `clients/shared/IPC/DaemonClient.swift` | `readHttpToken()`, `resolveHttpTokenPath()` |
274
- | `clients/macos/.../SettingsConnectTab.swift` | Manual token regeneration UI |