@agentvault/claude-bridge 0.8.34 → 0.8.36

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/dist/router.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import type { ToolConversation } from "@agentvault/agentvault";
1
2
  import type { ReplySink } from "./session.js";
2
3
  import type { WorkerTask } from "./worker-queue.js";
3
4
  /** The push interface wireBridge drives (matches PersistentClaudeSession.push). */
@@ -9,6 +10,8 @@ export interface PushLike {
9
10
  armed?: () => boolean;
10
11
  /** Held "earlier in this room" block — see room-context.ts. */
11
12
  roomContext?: string;
13
+ /** #1560: the conversation this message came from. */
14
+ conversation?: ToolConversation;
12
15
  }): unknown;
13
16
  }
14
17
  export interface RouterDeps {
package/dist/session.d.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  import { z } from "zod";
2
2
  import { type LlmUsage } from "./usage-report.js";
3
+ import type { ToolConversation, TurnFailureCode } from "@agentvault/agentvault";
4
+ import { type FailureDeps } from "./turn-failure.js";
3
5
  /**
4
6
  * The one tool the room session exposes: `room_say`. Speaking to the room is an
5
7
  * explicit choice — Claude calls this only when it has something to add, and
@@ -115,6 +117,13 @@ export interface SessionOpts {
115
117
  onPlanLimit?: (info: {
116
118
  resetsAt?: number;
117
119
  }) => void;
120
+ /**
121
+ * #1560: a failed turn is recorded (one `model_turn` span, one coded log
122
+ * line) and, when the turn owed a reply, answered with the owner notice.
123
+ * Absent: the failure is still logged by code and the bare notice is still
124
+ * posted, with no span.
125
+ */
126
+ failure?: FailureDeps;
118
127
  }
119
128
  export declare class PersistentClaudeSession {
120
129
  private opts;
@@ -144,6 +153,13 @@ export declare class PersistentClaudeSession {
144
153
  private roomServer;
145
154
  /** AbortController for the in-flight query(); abort() triggers it (queue timeout). */
146
155
  private _abort?;
156
+ /** #1560: every failure notice still being sent, so a worker's start()
157
+ * settles only after its notice did (the queue then moves on). */
158
+ private notices;
159
+ /** #1560: the code of the first failed turn this session recorded. */
160
+ private firstFailure?;
161
+ /** Review X1: set by abort(); the aborter owns the failure report from then on. */
162
+ private aborted;
147
163
  constructor(opts: SessionOpts);
148
164
  /**
149
165
  * Queue an inbound message for the model.
@@ -166,6 +182,8 @@ export declare class PersistentClaudeSession {
166
182
  toolEligibleSender?: boolean;
167
183
  armed?: () => boolean;
168
184
  roomContext?: string;
185
+ /** #1560: the conversation this message came from (span + notice wording). */
186
+ conversation?: ToolConversation;
169
187
  }): void;
170
188
  /** Route a say-tool message to the reply bound to the in-flight message, falling
171
189
  * back to opts.onSay. This is what closes the DM→room leak: the destination is
@@ -208,11 +226,31 @@ export declare class PersistentClaudeSession {
208
226
  */
209
227
  private buildSdkOptions;
210
228
  start(): Promise<void>;
229
+ /**
230
+ * #1560: the code of the first turn this session recorded as failed, or
231
+ * undefined. The worker queue reads it so a failure the session already
232
+ * reported (span, log, notice) is never reported a second time when the
233
+ * SDK then throws.
234
+ */
235
+ failureCode(): TurnFailureCode | undefined;
236
+ /**
237
+ * #1560: record one failed turn (once) and, if it owed a reply, start its
238
+ * notice. The record is synchronous; the notice is tracked in `notices`.
239
+ */
240
+ private failTurn;
241
+ /**
242
+ * #1560: the query() stopped with turns still unanswered (the listener's
243
+ * fatal path). Each one is recorded and, where owed, told — a message
244
+ * already handed to the model and one still waiting alike. Resolves once
245
+ * every notice has settled. Never throws.
246
+ */
247
+ failOpenTurns(err: unknown): Promise<void>;
211
248
  /**
212
249
  * #1467: at a turn's result, report it when Claude refused it with a rate
213
250
  * limit WHILE the plan's status is rejected. A refusal with the plan allowed
214
251
  * is a short-term rate limit, not the plan's cap; a rejected status with no
215
- * refusal (extra usage, model fallback) was served. Never throws.
252
+ * refusal (extra usage, model fallback) was served. Never throws. Returns
253
+ * whether it was the plan's limit (#1560: that span is the turn's one span).
216
254
  */
217
255
  private reportPlanLimitIfAny;
218
256
  }
@@ -0,0 +1,128 @@
1
+ /**
2
+ * #1560 slice 2 — a Claude turn that fails is shown to the owner, never
3
+ * swallowed, and never repeats the provider's words.
4
+ *
5
+ * The codes, the owner sentences and the span all come from the plugin's
6
+ * shared table (`packages/plugin/src/turn-failure.ts`), unchanged. This file
7
+ * only decides WHICH code a Claude Code failure is, and runs the one failure
8
+ * path both lanes use: record the span, log the code, then post the notice.
9
+ *
10
+ * What Claude Code tells us about a failed turn (SDK 0.2.141):
11
+ * - the made-up `assistant` message carries `error`, a closed enum
12
+ * (SDKAssistantMessageError). Its text block is the PROVIDER'S message,
13
+ * e.g. "Failed to authenticate. API Error: 401 OAuth access token has
14
+ * expired…". That text is never sent, logged or put in a span;
15
+ * - the `result` carries `is_error: true` and `api_error_status`;
16
+ * - `system/api_retry` carries `error_status: null` for a connection error
17
+ * with no HTTP response (the SDK's own type doc says so);
18
+ * - with no result at all, the SDK throws its own fixed messages for a
19
+ * missing or unspawnable CLI, and "Claude Code process exited with code N".
20
+ *
21
+ * Only those structured signals and the SDK's own fixed messages are read.
22
+ * The thrown "Claude Code returned an error result: <text>" carries provider
23
+ * text and is never matched for a code.
24
+ */
25
+ import { type ToolConversation, type ToolSpanSink, type TurnFailureCode } from "@agentvault/agentvault";
26
+ /** The provider named in a 1:1 notice. Claude Code signs in to Anthropic. */
27
+ export declare const PROVIDER = "Anthropic";
28
+ /** What the session saw on a failed turn. All fields optional; absent is absent. */
29
+ export interface ClaudeFailureSignal {
30
+ /** The made-up assistant message's `error` (SDKAssistantMessageError). */
31
+ assistantError?: unknown;
32
+ /** The result's `api_error_status`. */
33
+ apiErrorStatus?: unknown;
34
+ /** The turn saw `system/api_retry` with `error_status: null` (no HTTP response). */
35
+ connectionRetry?: boolean;
36
+ /** #1467: this refusal was the plan's own limit (already reported as its own span). */
37
+ planLimit?: boolean;
38
+ /** The result's `subtype` (e.g. "error_max_turns"). */
39
+ subtype?: unknown;
40
+ }
41
+ /**
42
+ * The code for a turn Claude Code reported as failed.
43
+ *
44
+ * Every branch is a literal `return "<code>"` on purpose: the plugin's
45
+ * tool-error-codes.test.ts scans this file for `return "<code>"` and checks
46
+ * each against KNOWN_TOOL_ERROR_CODES. A lookup table, ternary or variable
47
+ * would hide a code from that scan, so do not refactor this into one.
48
+ *
49
+ * The enum wins over the status: it is Claude Code's own reading of the
50
+ * response. The statuses follow the Anthropic API's documented error codes
51
+ * (401 authentication, 402 billing, 403 permission, 404 not found, 429 rate
52
+ * limit, 5xx/529 server or overloaded).
53
+ *
54
+ * `oauth_org_not_allowed` ("Your organization has disabled Claude
55
+ * subscription access for Claude Code", observed on loopita 09-25 and 10-02)
56
+ * is a SIGN-IN problem, not billing: the fix is to sign Claude Code in with an
57
+ * account or API key that is allowed. The billing sentence would send the
58
+ * owner to pay, which would not fix it.
59
+ */
60
+ export declare function classifyClaudeFailure(s: ClaudeFailureSignal): TurnFailureCode;
61
+ /**
62
+ * The code for a turn that ended in a THROW with no failed result to read
63
+ * (a missing CLI, a crash, a spawn error). Read in memory only; the message
64
+ * is never kept, logged or returned.
65
+ */
66
+ export declare function classifyClaudeThrow(err: unknown): TurnFailureCode;
67
+ /** The CLI's exit status when the SDK names one, else undefined. */
68
+ export declare function exitCodeOf(err: unknown): number | undefined;
69
+ /** What the failure path needs from the bridge. Every field optional (tests, legacy callers). */
70
+ export interface FailureDeps {
71
+ /** The name the owner knows this agent by (hub display name, else config). */
72
+ agentName?: () => string | undefined;
73
+ /** SecureChannel's TelemetryReporter. */
74
+ telemetry?: () => ToolSpanSink | null | undefined;
75
+ }
76
+ export interface FailedTurn {
77
+ code: TurnFailureCode;
78
+ exitCode?: number;
79
+ /** Only `"timeout"` today: the queue gave up on the turn. */
80
+ reason?: "timeout";
81
+ conversation?: ToolConversation;
82
+ durationMs: number;
83
+ /** The span is already recorded (#1467's plan-limit span). Exactly one per turn. */
84
+ spanRecorded?: boolean;
85
+ /** Where this turn's reply goes. */
86
+ reply?: (text: string) => void | Promise<void>;
87
+ /** Did this turn owe a reply? A room turn nobody addressed stays silent (#1107). */
88
+ owed: boolean;
89
+ /** The agent already said something this turn (re-review N1): the notice is
90
+ * then the BARE sentence. A partial "On it." must never be the last word. */
91
+ spoke?: boolean;
92
+ }
93
+ /**
94
+ * The one failure path. RECORD FIRST — the span and the log line, both
95
+ * synchronously — then post the notice, so a notice that stalls or fails
96
+ * cannot lose the record. Only the code and the exit status are logged.
97
+ *
98
+ * The notice names the cause only in a 1:1. Anywhere else (a room, or a turn
99
+ * whose conversation is unknown) it is the bare sentence: the coded ones
100
+ * describe the owner's own provider account. Never throws.
101
+ */
102
+ export declare function reportTurnFailed(deps: FailureDeps | undefined, t: FailedTurn, log: (m: string) => void): Promise<void>;
103
+ /**
104
+ * Review F1: the WHOLE fatal sequence (tell owed turns, then flush) is capped.
105
+ * A notice send that never settles (e.g. a stuck MLS catch-up) must not keep a
106
+ * deaf process alive: the supervisor can only restart a process that exits.
107
+ */
108
+ export declare const FATAL_DEADLINE_MS = 15000;
109
+ /** Resolves when `p` settles or after `ms`, whichever comes first. Never rejects. */
110
+ export declare function settleWithin(p: Promise<unknown>, ms: number): Promise<void>;
111
+ /**
112
+ * The always-on listener's query() rejected. Tell every owed turn it had not
113
+ * answered, flush telemetry so those spans are not lost with the process,
114
+ * then log the code (never the error text) and exit so the supervisor
115
+ * restarts the bridge. Never throws before `exit`, and always reaches it
116
+ * within `deadlineMs`.
117
+ */
118
+ export declare function stopAfterListenerFailure(err: unknown, deps: {
119
+ session: {
120
+ failOpenTurns(err: unknown): Promise<void>;
121
+ };
122
+ flush?: () => unknown;
123
+ log: (m: string) => void;
124
+ exit: (code: number) => void;
125
+ flushTimeoutMs?: number;
126
+ deadlineMs?: number;
127
+ }): Promise<void>;
128
+ //# sourceMappingURL=turn-failure.d.ts.map
@@ -1,4 +1,6 @@
1
+ import type { ToolConversation, TurnFailureCode } from "@agentvault/agentvault";
1
2
  import type { PersistentClaudeSession, ReplySink } from "./session.js";
3
+ import { type FailureDeps } from "./turn-failure.js";
2
4
  /** One tool-eligible turn to run in an isolated ephemeral worker. */
3
5
  export type WorkerTask = {
4
6
  /** The single instruction that seeds the worker's conversation. */
@@ -15,6 +17,8 @@ export type WorkerTask = {
15
17
  /** Armed-room turns: live getter the gate reads per tool decision (mid-turn
16
18
  * disarm denies the next call). Absent for owner DMs. */
17
19
  armed?: () => boolean;
20
+ /** #1560: the conversation this task answers (span + notice wording). */
21
+ conversation?: ToolConversation;
18
22
  };
19
23
  /**
20
24
  * Forensic record of EVERY task the queue runs — clean or not.
@@ -36,8 +40,13 @@ export type WorkerIncident = {
36
40
  /** ISO timestamp — the bridge's own logs are unstamped, so this is the anchor. */
37
41
  at: string;
38
42
  outcome: "ok" | "timeout" | "error";
39
- /** Absent on a clean turn — its presence is what marks a real incident. */
40
- error?: string;
43
+ /**
44
+ * #1560: the failure's code. Absent on a clean turn — its presence is what
45
+ * marks a real incident. Replaces the old `error` (the raw error message),
46
+ * which wrote the provider's text — "…OAuth access token has expired…" — to
47
+ * this file on every failed turn.
48
+ */
49
+ code?: TurnFailureCode;
41
50
  /**
42
51
  * enqueue -> start. THE load-bearing field: it separates "this task hung" from
43
52
  * "this task was stuck behind one that hung". Both look identical to the owner
@@ -102,13 +111,16 @@ export interface WorkerQueueDeps {
102
111
  * no trap.
103
112
  */
104
113
  onIncident?: (rec: WorkerIncident) => void;
114
+ /** #1560: span sink + agent name for the failure notice. */
115
+ failure?: FailureDeps;
105
116
  }
106
117
  /**
107
118
  * Serial FIFO of tool-eligible turns. One worker runs at a time (concurrent tool
108
119
  * calls against one device/workspace can interleave destructively; the owner is a
109
120
  * single actor). Each task runs in a fresh ephemeral worker seeded with only its
110
- * instruction; on error or timeout the worker is torn down, a generic message is
111
- * sent to the task's reply, and the queue advances.
121
+ * instruction; on error or timeout the worker is torn down, the failure is
122
+ * recorded by code and the owner notice is sent to the task's reply (#1560),
123
+ * and the queue advances.
112
124
  *
113
125
  * ⚠️ A task cannot wedge the queue PERMANENTLY, but it absolutely can wedge it for
114
126
  * up to `timeoutMs`. Serial + a 5-minute cap means ONE hung worker delays every
@@ -138,6 +150,8 @@ export declare class WorkerQueue {
138
150
  * indistinguishable from a turn that did nothing.
139
151
  */
140
152
  private snapshotOf;
153
+ /** The failure the session itself already recorded, if any. */
154
+ private failureOf;
141
155
  /** Best-effort trap emit. A broken sink must never break the queue. */
142
156
  private emitIncident;
143
157
  private runOne;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agentvault/claude-bridge",
3
- "version": "0.8.34",
3
+ "version": "0.8.36",
4
4
  "type": "module",
5
5
  "description": "AgentVault Claude Bridge — daemon for bridging a Claude agent into secure E2E-encrypted AgentVault 1:1 direct messages and rooms.",
6
6
  "main": "dist/index.js",