@agentvault/grok-bridge 0.4.31 → 0.4.33

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/README.md CHANGED
@@ -60,6 +60,28 @@ baked at the original install.
60
60
  Logs go to `<data-dir>/logs/grok-bridge.error.log` — the bridge writes to stderr,
61
61
  so the `.error.log` file is the one with content in it.
62
62
 
63
+ ## When a reply fails
64
+
65
+ When a Grok turn fails, the bridge records it first, then tells the chat, and only then restarts itself. It never drops a failed turn without a trace.
66
+
67
+ - **In a 1:1 with the owner**, the owner sees one plain sentence. It names the cause when the bridge can tell what it is. For example: "Ohtani couldn't answer this message: its xAI sign-in expired. Sign in again on the agent's computer." The bridge can tell an expired sign-in, a billing problem, rate limiting, a model the account can't use, xAI being unreachable, and a Grok CLI too old to answer. Only the expired sign-in has been seen live so far. When the cause is unclear, the sentence just says the agent couldn't answer this message.
68
+ - **In a room**, the sentence never names the cause, because other people and agents can read it. The room just sees that the agent couldn't answer this message.
69
+ - **No notice** is posted for a turn the agent didn't owe a reply to: a room message that didn't @mention it, or a 1:1 the owner's device didn't vouch for.
70
+ - **In every case**, the cause is recorded privately for the owner under **Health, Recent problems**.
71
+
72
+ Every failed turn ends this process, and the service manager starts a fresh one. That is on purpose: the Grok CLI does not confirm a cancel, so the old session could still speak. The fresh process starts a new Grok session. The bridge saves no Grok session to resume, so there is no saved session to lose or repair.
73
+
74
+ The log holds one line per event, with codes and exit numbers only. It never holds message text or provider error text:
75
+
76
+ | Log line | What it means |
77
+ |----------|---------------|
78
+ | `turn_failed code=<code> exit=<n or none>` | The turn failed. `exit` is the Grok CLI's exit status when it died mid-turn, otherwise `none`. A notice follows when the turn owed a reply. |
79
+ | `notice sent to <chat>` | The failure notice was delivered. |
80
+ | `notice_failed code=<code>` or `reason=timeout` | The failure notice could not be delivered. The failure is still recorded under Health. |
81
+ | `turn_cancelled reason=shutdown` | The bridge was already shutting down, so the turn was cut short on purpose. Nothing is recorded or posted. |
82
+
83
+ The Grok CLI's own error output can repeat the provider's error text, so the log records only its size and level (`grok: stderr lines=<n> bytes=<n> level=ERROR`). A failed start is logged the same way, by cause only: `fatal: grok did not start: code=<code> exit=<n or none> ...`. To see the CLI's own words, run `grok` by hand.
84
+
63
85
  ## Copied-from ledger — REVIEW PERIODICALLY
64
86
 
65
87
  These files were copied once from the Claude bridge and are now owned here. A fix
@@ -23,6 +23,23 @@ export declare class AcpTimeout extends Error {
23
23
  readonly ms: number;
24
24
  constructor(method: string, ms: number);
25
25
  }
26
+ /**
27
+ * The agent answered a request with a JSON-RPC `error` (#1560). The message is
28
+ * unchanged from before this class existed (`<method>: <error JSON>`); the
29
+ * fields let the turn-failure classifier read the code and `data` without
30
+ * re-parsing it. Both may carry provider text: never log or send them.
31
+ */
32
+ export declare class AcpRpcError extends Error {
33
+ readonly method: string;
34
+ readonly rpcCode: unknown;
35
+ readonly rpcMessage: unknown;
36
+ readonly rpcData: unknown;
37
+ constructor(method: string, error: {
38
+ code?: unknown;
39
+ message?: unknown;
40
+ data?: unknown;
41
+ });
42
+ }
26
43
  /**
27
44
  * JSON-RPC over a child's stdio. Transport only — it knows nothing about ACP
28
45
  * methods, sessions or Grok.
package/dist/bridge.d.ts CHANGED
@@ -13,7 +13,7 @@
13
13
  * bridge declares its own in its bridge.ts; that declaration was NOT copied.
14
14
  * Type-only, so this cannot create a runtime import cycle. */
15
15
  import { type ToolSpanSink } from "@agentvault/agentvault";
16
- import type { RoomSession } from "./grok-session.js";
16
+ import type { ReplySink, RoomSession } from "./grok-session.js";
17
17
  import { type RoomContextLimits } from "./room-context.js";
18
18
  import { type TerminalVerdict } from "./service/self-uninstall.js";
19
19
  export interface RoomMessage {
@@ -30,6 +30,10 @@ export interface RoomMessage {
30
30
  * payloads / tests → treated as "not an agent" is NOT assumed: replyExpected is
31
31
  * set only when we affirmatively know the sender is a human (=== false). */
32
32
  senderIsAgent?: boolean;
33
+ /** #1574 — SecureChannel: the room's audience, "private_pair" or "group".
34
+ * Passed through to owesRoomReply untyped; anything but "private_pair"
35
+ * (absent on emitters before #1574) reads as a group room. */
36
+ roomAudience?: unknown;
33
37
  /** ISO send time from the emit (`data.created_at`). Used to stamp a message
34
38
  * held as room context; receipt time stands in when absent. */
35
39
  timestamp?: string;
@@ -141,7 +145,7 @@ export declare class ActiveTarget {
141
145
  * these per inbound (at push time) and hands it to the session, so a room message
142
146
  * arriving mid-compose can never redirect a private 1:1 reply into the room.
143
147
  */
144
- snapshotReply(channel: ReplyChannel, log?: (msg: string) => void): (text: string) => Promise<void>;
148
+ snapshotReply(channel: ReplyChannel, log?: (msg: string) => void): ReplySink;
145
149
  }
146
150
  export interface LifecycleChannel {
147
151
  on(ev: "auth_failed", cb: (e: {
@@ -1,11 +1,22 @@
1
+ import { type ToolConversation, type ToolSpanSink } from "@agentvault/agentvault";
1
2
  import type { TurnResult } from "./agent-driver.js";
3
+ /** A reply sink. `{ notice: true }` marks the #1560 failure notice: delivered
4
+ * to the same chat, but not recorded as the agent's own `say`. */
5
+ export type ReplySink = (text: string, opts?: {
6
+ notice?: boolean;
7
+ }) => Promise<void>;
8
+ /** What a pushed turn knows about where it came from. */
9
+ export interface PushOpts {
10
+ autoReplyOnText?: boolean;
11
+ replyExpected?: boolean;
12
+ /** #1560: the chat this turn answers. It names the failure span and picks the
13
+ * notice wording: only a 1:1 hears the cause, anything else the bare line. */
14
+ conversation?: ToolConversation;
15
+ }
2
16
  /** Grok's OWN copy of the room-session contract. The Claude bridge has its own;
3
17
  * the two are deliberately unrelated and must not be unified. */
4
18
  export interface RoomSession {
5
- push(text: string, reply?: (text: string) => Promise<void>, opts?: {
6
- autoReplyOnText?: boolean;
7
- replyExpected?: boolean;
8
- }): void;
19
+ push(text: string, reply?: ReplySink, opts?: PushOpts): void;
9
20
  }
10
21
  export interface DriverLike {
11
22
  deliver(text: string): Promise<TurnResult>;
@@ -27,6 +38,8 @@ export declare class GrokSession implements RoomSession {
27
38
  private queue;
28
39
  private running;
29
40
  private invalidated;
41
+ /** Review F3: the error that failed this session, once one has. */
42
+ private failure;
30
43
  /** The message whose turn is in flight, or null between turns. Null is a
31
44
  * DISTINCT state from "a message that carries no reply sink" — conflating
32
45
  * them is what let a post-turn say fall through to the session-level sink. */
@@ -53,13 +66,18 @@ export declare class GrokSession implements RoomSession {
53
66
  * it. Revisit once `_meta.agentProfile` is probed properly.
54
67
  */
55
68
  identity?: string;
56
- /** Fatal delivery failure: tear down this process for supervised restart. */
69
+ /** Fatal delivery failure: tear down this process for supervised restart.
70
+ * #1560: runs only AFTER the failure is recorded and the notice is out. */
57
71
  onFatal?: (error: unknown) => void | Promise<void>;
72
+ /** #1560: the name the owner reads in a failure notice. */
73
+ agentName?: string | (() => string | undefined);
74
+ /** #1560: where a failed turn's `model_turn` span goes (channel.telemetry). */
75
+ telemetry?: () => ToolSpanSink | null | undefined;
76
+ /** Test seam: the bound on the notice's send. */
77
+ noticeTimeoutMs?: number;
58
78
  });
59
- push(text: string, reply?: (text: string) => Promise<void>, opts?: {
60
- autoReplyOnText?: boolean;
61
- replyExpected?: boolean;
62
- }): void;
79
+ push(text: string, reply?: ReplySink, opts?: PushOpts): void;
80
+ private inbound;
63
81
  /** Hand this to startRoomMcpServer({ onSay }). The behaviour is stable for
64
82
  * the life of the session — the per-message routing happens HERE, not in
65
83
  * the listener — but each call returns a NEW closure; do not rely on
@@ -67,10 +85,26 @@ export declare class GrokSession implements RoomSession {
67
85
  sayHandler(): (text: string) => void;
68
86
  /** Revoke output and future work BEFORE asynchronous process cleanup. */
69
87
  invalidate(): void;
88
+ /** True while a turn is in flight. #1560: if the CLI dies now, THAT turn
89
+ * fails, and it must be recorded and noticed before the process exits. */
90
+ turnInFlight(): boolean;
70
91
  /** Resolves when the queue is empty. For tests and shutdown. */
71
92
  idle(): Promise<void>;
72
93
  private log;
73
94
  private run;
95
+ /**
96
+ * #1560: a failed turn is never silent. In this order:
97
+ * 1. RECORD one `model_turn` span with the code (Health, Recent problems),
98
+ * 2. LOG the code and exit status only (`turn_failed code=<c> exit=<n|none>`),
99
+ * 3. NOTICE the chat with the owner sentence, only where the turn owed a
100
+ * reply (`autoReplyOnText`: an un-addressed room turn stays silent, #1107;
101
+ * an unattested 1:1 gets no plain text, #621). Only a 1:1 hears the
102
+ * cause; a room or an unknown chat gets the bare sentence, because the
103
+ * coded ones describe the owner's own provider account.
104
+ * The notice is bounded so the fatal teardown that follows always runs.
105
+ * Never throws.
106
+ */
107
+ private reportFailure;
74
108
  /**
75
109
  * #416: when the owner expects an answer and the model produced prose but
76
110
  * never called the say tool, send the prose rather than dropping it.
@@ -5,6 +5,11 @@
5
5
  * messages stopped running turns: the agent is shown only what @-mentions it,
6
6
  * with what it missed arriving as a quoted context block. Telling the model it
7
7
  * sees everything would have it treat that block as live conversation.
8
+ *
9
+ * #1574: a private room (one person + this agent) is the exception — every
10
+ * person's message there owes a reply with no name, and the turn itself carries
11
+ * the plugin's PRIVATE_ROOM_TURN_NOTE. Without this sentence the rules above
12
+ * would tell the model it was only shown a message because it was mentioned.
8
13
  */
9
14
  export declare function roomRules(sayTool: string): string;
10
15
  export declare function identityPrompt(agentName: string, sayTool: string): string;