polygram 0.10.0-rc.9 → 0.11.0-rc.11
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/.claude-plugin/plugin.json +1 -1
- package/config.example.json +5 -2
- package/lib/autosteered-refs.js +20 -2
- package/lib/claude-bin.js +78 -0
- package/lib/db/auto-resume.js +7 -0
- package/lib/db/sessions.js +142 -1
- package/lib/error/classify.js +41 -0
- package/lib/handlers/abort.js +42 -4
- package/lib/handlers/slash-commands.js +22 -2
- package/lib/process/channels-bridge-protocol.js +168 -0
- package/lib/process/channels-bridge-server.js +264 -0
- package/lib/process/channels-bridge.mjs +320 -0
- package/lib/process/channels-process.js +1490 -0
- package/lib/process/channels-tool-dispatcher.js +266 -0
- package/lib/process/factory.js +63 -10
- package/lib/process/hook-event-tail.js +155 -0
- package/lib/process/hook-settings.js +144 -0
- package/lib/process/polygram-hook-append.js +71 -0
- package/lib/process/tmux-process.js +2475 -318
- package/lib/process/turn-phase.js +150 -0
- package/lib/process-manager.js +112 -2
- package/lib/sdk/callbacks.js +326 -4
- package/lib/telegram/display-hint.js +11 -0
- package/lib/telegram/heartbeat-reactor.js +254 -0
- package/lib/telegram/process-agent-reply.js +233 -0
- package/lib/telegram/sanitize-reply.js +101 -0
- package/lib/tmux/session-log-parser.js +329 -101
- package/lib/tmux/startup-gate.js +172 -0
- package/lib/tmux/tmux-runner.js +106 -11
- package/package.json +4 -2
- package/polygram.js +299 -29
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://anthropic.com/claude-code/plugin.schema.json",
|
|
3
3
|
"name": "polygram",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.11.0-rc.11",
|
|
5
5
|
"description": "Telegram integration for Claude Code that preserves the OpenClaw per-chat session model. Migration target for OpenClaw users. Multi-bot, multi-chat, per-topic isolation; SQLite transcripts; inline-keyboard approvals. Bundles /polygram:status|logs|pair-code|approvals admin commands plus history (transcript queries) and polygram-send (out-of-turn IPC sends with file-upload validation) skills.",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"telegram",
|
package/config.example.json
CHANGED
|
@@ -98,7 +98,9 @@
|
|
|
98
98
|
"cwd": "/Users/you/admin-agent",
|
|
99
99
|
"requireMention": true,
|
|
100
100
|
"isolateTopics": true,
|
|
101
|
-
"_comment_topics": "rc.48: each topic entry is EITHER a string (legacy: just a label) OR an object with optional fields {name, agent, cwd, model, effort, permissionMode}. Object form lets a topic override chat-level config. Per-topic permissionMode overrides chat-level — typical use: scope one topic to permissionMode:'default' (so settings.json gates apply) while the rest of the chat stays on bypassPermissions. Object form requires isolateTopics: true (each topic gets its own SDK Query); polygram emits a startup warning otherwise.",
|
|
101
|
+
"_comment_topics": "rc.48: each topic entry is EITHER a string (legacy: just a label) OR an object with optional fields {name, agent, cwd, model, effort, permissionMode, isolateUserConfig}. Object form lets a topic override chat-level config. Per-topic permissionMode overrides chat-level — typical use: scope one topic to permissionMode:'default' (so settings.json gates apply) while the rest of the chat stays on bypassPermissions. Object form requires isolateTopics: true (each topic gets its own SDK Query); polygram emits a startup warning otherwise.",
|
|
102
|
+
"_comment_isolateUserConfig": "0.10.0, tmux backend only: isolateUserConfig:true spawns the topic's claude TUI cut off from the user-level ~/.claude config — passes --strict-mcp-config (zero MCP servers load) and --setting-sources project,local (drops ~/.claude/settings.json; the spawn cwd's own .claude/settings.json still loads). Use it when a topic's agent would otherwise inherit slow user-global MCP servers whose cold-start (tens of seconds) wedges the TUI before it can accept a prompt. Settable at chat OR topic level (topic wins). Default false.",
|
|
103
|
+
"_comment_pm": "0.11.0: 'pm' selects the Process backend: 'sdk' (default; per-token Console API; full SDK features), 'tmux' (subscription-priced claude CLI in tmux; JSONL/pane parsing for IO), 'channels' (subscription-priced claude CLI in tmux; structured IO via the official Channels MCP protocol — see docs/0.11.0-channels-driver-plan.md). Settable at bot, chat, OR topic level (topic > chat > bot). Channels requires Pro/Max subscription, Claude Code v2.1.80+, and is in research preview — invokes --dangerously-load-development-channels.",
|
|
102
104
|
"topics": {
|
|
103
105
|
"100": "Customer A",
|
|
104
106
|
"200": {
|
|
@@ -107,7 +109,8 @@
|
|
|
107
109
|
"cwd": "/Users/you/customer-b-projects",
|
|
108
110
|
"model": "opus",
|
|
109
111
|
"effort": "high",
|
|
110
|
-
"permissionMode": "default"
|
|
112
|
+
"permissionMode": "default",
|
|
113
|
+
"isolateUserConfig": true
|
|
111
114
|
}
|
|
112
115
|
}
|
|
113
116
|
},
|
package/lib/autosteered-refs.js
CHANGED
|
@@ -46,12 +46,22 @@
|
|
|
46
46
|
* logged to opts.logger?.error — they never block clearing of
|
|
47
47
|
* subsequent refs.
|
|
48
48
|
* @param {{ error?: (msg: string) => void }} [opts.logger]
|
|
49
|
+
* @param {number} [opts.minIntervalMs=250]
|
|
50
|
+
* minimum gap (ms) between successive applyClear calls inside a
|
|
51
|
+
* single clear() loop. Telegram's setMessageReaction rate limit
|
|
52
|
+
* is ~5/sec/chat; 250ms (4/sec) stays under that. Pass 0 to
|
|
53
|
+
* disable pacing in tests / contexts where the underlying applyClear
|
|
54
|
+
* doesn't talk to a rate-limited API. Only the GAP between calls
|
|
55
|
+
* is paced — the first call fires immediately, single-ref clears
|
|
56
|
+
* incur no delay. L7 fix 2026-05-16: was unpaced, exceeded the
|
|
57
|
+
* Telegram cap under N≥6 autosteers per turn.
|
|
49
58
|
* @returns {AutosteeredRefs}
|
|
50
59
|
*/
|
|
51
|
-
function createAutosteeredRefs({ applyClear, logger = console } = {}) {
|
|
60
|
+
function createAutosteeredRefs({ applyClear, logger = console, minIntervalMs = 250 } = {}) {
|
|
52
61
|
if (typeof applyClear !== 'function') {
|
|
53
62
|
throw new TypeError('applyClear function required');
|
|
54
63
|
}
|
|
64
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
55
65
|
/** @type {Map<string, MsgRef[]>} */
|
|
56
66
|
const refs = new Map();
|
|
57
67
|
|
|
@@ -79,7 +89,15 @@ function createAutosteeredRefs({ applyClear, logger = console } = {}) {
|
|
|
79
89
|
if (!list || list.length === 0) return 0;
|
|
80
90
|
refs.delete(sessionKey);
|
|
81
91
|
let cleared = 0;
|
|
82
|
-
|
|
92
|
+
// L7: pace inter-call gaps to stay under Telegram's
|
|
93
|
+
// setMessageReaction rate limit (~5/sec/chat). The first call
|
|
94
|
+
// fires immediately — pacing applies only to the gap BEFORE the
|
|
95
|
+
// 2nd+ call. minIntervalMs=0 disables pacing entirely.
|
|
96
|
+
for (let i = 0; i < list.length; i += 1) {
|
|
97
|
+
const ref = list[i];
|
|
98
|
+
if (i > 0 && minIntervalMs > 0) {
|
|
99
|
+
await sleep(minIntervalMs);
|
|
100
|
+
}
|
|
83
101
|
try {
|
|
84
102
|
await applyClear(ref);
|
|
85
103
|
cleared += 1;
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const os = require('os');
|
|
4
|
+
const path = require('path');
|
|
5
|
+
const fs = require('fs');
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Resolve + verify the pinned claude CLI binary for the tmux backend.
|
|
9
|
+
*
|
|
10
|
+
* Why this exists: the tmux backend reads claude CLI INTERNAL
|
|
11
|
+
* artefacts (JSONL events, queue-operation semantics, TUI banner
|
|
12
|
+
* ASCII, READY hint strings, stop_reason values) — none a stable
|
|
13
|
+
* public contract. polygram pins ONE version
|
|
14
|
+
* (CLAUDE_CLI_PINNED_VERSION in lib/process/tmux-process.js) and
|
|
15
|
+
* must spawn THAT binary, never whatever `claude` on $PATH happens
|
|
16
|
+
* to resolve to.
|
|
17
|
+
*
|
|
18
|
+
* Before this module the tmux runner spawned the bare string
|
|
19
|
+
* `claude`, resolved through $PATH. The claude CLI installs each
|
|
20
|
+
* version as a standalone binary at
|
|
21
|
+
* ~/.local/share/claude/versions/<version>
|
|
22
|
+
* and points ~/.local/bin/claude (a symlink) at the active one.
|
|
23
|
+
* Its auto-updater re-points that symlink whenever a new version
|
|
24
|
+
* lands — so a $PATH spawn silently drifts (shumorobot 2026-05-16:
|
|
25
|
+
* CLI auto-updated 2.1.142 → 2.1.143 between deploys).
|
|
26
|
+
*
|
|
27
|
+
* Spawning the ABSOLUTE versioned path is immune to that: the
|
|
28
|
+
* updater only ADDS new version files, it never overwrites an
|
|
29
|
+
* existing one. `versions/2.1.142` stays byte-identical forever.
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Absolute path to the pinned claude binary.
|
|
34
|
+
*
|
|
35
|
+
* Resolution order:
|
|
36
|
+
* 1. POLYGRAM_CLAUDE_BIN env — explicit override (non-standard
|
|
37
|
+
* installs, CI, hosts where the layout differs).
|
|
38
|
+
* 2. ~/.local/share/claude/versions/<version> — the standard
|
|
39
|
+
* claude-CLI install location.
|
|
40
|
+
*
|
|
41
|
+
* The returned path is NOT guaranteed to exist — callers verify
|
|
42
|
+
* via verifyPinnedClaudeBin().
|
|
43
|
+
*
|
|
44
|
+
* @param {string} version — pinned version, e.g. '2.1.142'
|
|
45
|
+
* @returns {string} absolute path
|
|
46
|
+
*/
|
|
47
|
+
function resolvePinnedClaudeBin(version) {
|
|
48
|
+
const override = process.env.POLYGRAM_CLAUDE_BIN;
|
|
49
|
+
if (override) return override;
|
|
50
|
+
return path.join(os.homedir(), '.local', 'share', 'claude', 'versions', version);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Verify the pinned binary exists and is executable.
|
|
55
|
+
*
|
|
56
|
+
* @param {string} version — pinned version, e.g. '2.1.142'
|
|
57
|
+
* @returns {{ ok: boolean, path: string, reason?: string }}
|
|
58
|
+
* ok=true → path is a spawnable binary.
|
|
59
|
+
* ok=false → reason carries an operator-actionable message.
|
|
60
|
+
*/
|
|
61
|
+
function verifyPinnedClaudeBin(version) {
|
|
62
|
+
const binPath = resolvePinnedClaudeBin(version);
|
|
63
|
+
try {
|
|
64
|
+
fs.accessSync(binPath, fs.constants.X_OK);
|
|
65
|
+
return { ok: true, path: binPath };
|
|
66
|
+
} catch (err) {
|
|
67
|
+
const code = err && err.code ? err.code : (err && err.message) || 'unknown';
|
|
68
|
+
return {
|
|
69
|
+
ok: false,
|
|
70
|
+
path: binPath,
|
|
71
|
+
reason: `pinned claude CLI v${version} not found or not executable at `
|
|
72
|
+
+ `${binPath} (${code}). Install it with \`claude install ${version}\` `
|
|
73
|
+
+ 'or set POLYGRAM_CLAUDE_BIN to the correct binary path.',
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
module.exports = { resolvePinnedClaudeBin, verifyPinnedClaudeBin };
|
package/lib/db/auto-resume.js
CHANGED
|
@@ -90,6 +90,13 @@ function createAutoResumeTracker({ cooldownMs = DEFAULT_COOLDOWN_MS, now = Date.
|
|
|
90
90
|
*/
|
|
91
91
|
function isAutoResumable({ error, aborted, replay, shuttingDown }) {
|
|
92
92
|
if (aborted || replay || shuttingDown) return false;
|
|
93
|
+
// Review F#6: channels analog of the tmux 'idle with no Claude activity'
|
|
94
|
+
// pattern. The bridge socket dropped mid-turn (claude crashed, bridge
|
|
95
|
+
// process died) — that's a wedge, not a runaway. Same intent as the
|
|
96
|
+
// regex match below, just expressed via err.code because channels throws
|
|
97
|
+
// a different message string. TURN_TIMEOUT stays NON-resumable (it's
|
|
98
|
+
// the channels analog of the wall-clock ceiling — likely a runaway).
|
|
99
|
+
if (error?.code === 'BRIDGE_DISCONNECTED') return true;
|
|
93
100
|
const msg = String(error?.message || error || '');
|
|
94
101
|
return /idle with no Claude activity/i.test(msg);
|
|
95
102
|
}
|
package/lib/db/sessions.js
CHANGED
|
@@ -95,4 +95,145 @@ function getClaudeSessionId(db, sessionKey) {
|
|
|
95
95
|
return row?.claude_session_id || null;
|
|
96
96
|
}
|
|
97
97
|
|
|
98
|
-
|
|
98
|
+
// ─── S2: session-config drift ────────────────────────────────────────
|
|
99
|
+
//
|
|
100
|
+
// A stored `sessions` row records the config the claude session was
|
|
101
|
+
// SPAWNED under. Two of the recorded fields are spawn-identity:
|
|
102
|
+
// - agent — `--agent <name>` is baked into the spawned process;
|
|
103
|
+
// resuming a session spawned under agent X under agent Y forces
|
|
104
|
+
// claude to use Y's system prompt + tool whitelist against
|
|
105
|
+
// conversation history built under X's. Incoherent.
|
|
106
|
+
// - cwd — `--cwd <path>` (SDK) / tmux session cwd; claude resolves
|
|
107
|
+
// project-local config (.claude/settings.json, agent files,
|
|
108
|
+
// plugins) relative to it. Mid-conversation cwd drift means
|
|
109
|
+
// half the messages are answered with one project's allowlist
|
|
110
|
+
// and the other half with another's.
|
|
111
|
+
//
|
|
112
|
+
// pm_backend was REMOVED from spawn-identity (rc.32, 2026-05-21).
|
|
113
|
+
// Both backends spawn the same pinned claude binary and write the
|
|
114
|
+
// same on-disk JSONL (~/.claude/projects/<cwd-enc>/<sid>.jsonl) —
|
|
115
|
+
// claude itself doesn't know or care which Node-side wrapper invoked
|
|
116
|
+
// it. Treating a backend flip as drift was destructively dropping
|
|
117
|
+
// context across the SDK→tmux migration window, costing every chat
|
|
118
|
+
// its conversation history on its first turn under the new backend.
|
|
119
|
+
// shumorobot 2026-05-20 18:51 incident: the Music topic flipped
|
|
120
|
+
// tmux→sdk→tmux during runtime and lost its agent's prior context
|
|
121
|
+
// at each flip. The orphan-tmux problem that the flip ALSO triggered
|
|
122
|
+
// is solved by rc.31's spawn-time reconcile (TmuxProcess.start) —
|
|
123
|
+
// independently, so a backend flip is now a no-op for session-state.
|
|
124
|
+
//
|
|
125
|
+
// shumorobot 2026-05-17 22:03, topic :3 (the original drift incident)
|
|
126
|
+
// remains correctly handled: that row had agent+cwd drift in
|
|
127
|
+
// addition to backend, so the agent+cwd drift alone still drops it.
|
|
128
|
+
//
|
|
129
|
+
// model + effort are deliberately EXCLUDED from the invalidating set.
|
|
130
|
+
// They are NOT spawn-identity: a live `/model` or `/effort` change is
|
|
131
|
+
// pushed into the running session by `pm.setModel` /
|
|
132
|
+
// `pm.applyFlagSettings` with no respawn (lib/handlers/slash-commands.js,
|
|
133
|
+
// lib/handlers/config-callback.js). Including them here would
|
|
134
|
+
// destructively drop the whole session — discarding all context — on
|
|
135
|
+
// every model switch, double-handling what the live-apply path
|
|
136
|
+
// already covers cleanly. The stored model/effort columns are
|
|
137
|
+
// informational, not identity.
|
|
138
|
+
const SPAWN_IDENTITY_FIELDS = ['agent', 'cwd'];
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Decide whether a stored session can be resumed for the next spawn,
|
|
142
|
+
* or whether config drift means it must be dropped and re-spawned
|
|
143
|
+
* fresh.
|
|
144
|
+
*
|
|
145
|
+
* On drift the stale row is DELETED here — so the very next spawn
|
|
146
|
+
* mints a fresh claude_session_id under the correct config and the
|
|
147
|
+
* `onInit` callback re-upserts the row. This self-heals every
|
|
148
|
+
* pre-migration stale row across all chats with no manual SQL.
|
|
149
|
+
*
|
|
150
|
+
* @param {object|null} db — DB handle (null → fresh spawn)
|
|
151
|
+
* @param {string} sessionKey
|
|
152
|
+
* @param {object} resolved — freshly-resolved spawn config
|
|
153
|
+
* @param {string} [resolved.agent]
|
|
154
|
+
* @param {string} [resolved.cwd]
|
|
155
|
+
* @param {string} [resolved.backend] — 'sdk' | 'tmux' (resolved by
|
|
156
|
+
* process/factory.js pickBackend); compared to the row's pm_backend
|
|
157
|
+
* @returns {{ existingSessionId: string|null, drift: object|null }}
|
|
158
|
+
* existingSessionId — pass to start() for --resume, or null for a
|
|
159
|
+
* fresh spawn (no stored row, or drift dropped it)
|
|
160
|
+
* drift — null when no drift; otherwise { fields, before, after }
|
|
161
|
+
* for the `session-config-drift` telemetry event
|
|
162
|
+
*/
|
|
163
|
+
function resolveSessionForSpawn(db, sessionKey, resolved = {}) {
|
|
164
|
+
if (!db) return { existingSessionId: null, drift: null };
|
|
165
|
+
const row = db.getSession(sessionKey);
|
|
166
|
+
if (!row || !row.claude_session_id) {
|
|
167
|
+
return { existingSessionId: null, drift: null };
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Normalise: a missing field on either side is treated as equal to
|
|
171
|
+
// a missing field on the other (both null/undefined → no drift).
|
|
172
|
+
const after = {
|
|
173
|
+
agent: resolved.agent || null,
|
|
174
|
+
cwd: resolved.cwd || null,
|
|
175
|
+
pm_backend: resolved.backend || null,
|
|
176
|
+
};
|
|
177
|
+
const before = {
|
|
178
|
+
agent: row.agent || null,
|
|
179
|
+
cwd: row.cwd || null,
|
|
180
|
+
pm_backend: row.pm_backend || null,
|
|
181
|
+
};
|
|
182
|
+
const drifted = SPAWN_IDENTITY_FIELDS.filter((f) => {
|
|
183
|
+
// If the resolved config does not specify a field, do not treat
|
|
184
|
+
// it as drift — we have nothing to compare against.
|
|
185
|
+
if (after[f] == null) return false;
|
|
186
|
+
return before[f] !== after[f];
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
// 0.11.0 channels boundary (2026-05-25): SDK ↔ tmux flips remain
|
|
190
|
+
// non-invalidating per rc.32's reasoning. But channels mode is
|
|
191
|
+
// genuinely different on the claude side:
|
|
192
|
+
//
|
|
193
|
+
// - bridge MCP server is mounted (--mcp-config + --strict-mcp-config)
|
|
194
|
+
// - additional --append-system-prompt directive enforces the
|
|
195
|
+
// reply-tool contract (claude must call mcp__polygram-bridge__reply
|
|
196
|
+
// instead of writing inline). Sessions created without this
|
|
197
|
+
// directive don't have the contract baked into their context.
|
|
198
|
+
// - agent behavior expectations differ — same agent file, but
|
|
199
|
+
// conversation flow is "user msg arrives via channel, reply via
|
|
200
|
+
// tool" rather than "user msg arrives inline, reply inline."
|
|
201
|
+
//
|
|
202
|
+
// Live shumorobot 2026-05-25 incident: Music topic resumed a
|
|
203
|
+
// pre-channels session id (4837f61a-…) that had been mid-soulseek-
|
|
204
|
+
// curation. The user's "how are you" got interpreted as a continuation
|
|
205
|
+
// of THAT task; claude responded with music release info, inline,
|
|
206
|
+
// never calling the reply tool. Every turn timed out at 3min.
|
|
207
|
+
//
|
|
208
|
+
// Rule: any transition TO or FROM channels drops the prior session.
|
|
209
|
+
// XOR — flips between channels and {sdk,tmux} invalidate; sdk↔tmux
|
|
210
|
+
// flips remain free.
|
|
211
|
+
const wasChannels = before.pm_backend === 'channels';
|
|
212
|
+
const willBeChannels = after.pm_backend === 'channels';
|
|
213
|
+
if (after.pm_backend != null && wasChannels !== willBeChannels) {
|
|
214
|
+
drifted.push('pm_backend');
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
if (drifted.length === 0) {
|
|
218
|
+
return { existingSessionId: row.claude_session_id, drift: null };
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// Drift: drop the stale row so the next spawn is fresh + correct.
|
|
222
|
+
db.clearSessionId(sessionKey);
|
|
223
|
+
return {
|
|
224
|
+
existingSessionId: null,
|
|
225
|
+
drift: {
|
|
226
|
+
fields: drifted,
|
|
227
|
+
before: { ...before, claude_session_id: row.claude_session_id },
|
|
228
|
+
after,
|
|
229
|
+
},
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
module.exports = {
|
|
234
|
+
migrateJsonToDb,
|
|
235
|
+
getClaudeSessionId,
|
|
236
|
+
resolveSessionForSpawn,
|
|
237
|
+
countSessions,
|
|
238
|
+
SPAWN_IDENTITY_FIELDS,
|
|
239
|
+
};
|
package/lib/error/classify.js
CHANGED
|
@@ -163,6 +163,47 @@ const CODES = {
|
|
|
163
163
|
isTransient: false,
|
|
164
164
|
autoRecover: null,
|
|
165
165
|
},
|
|
166
|
+
// Review F#5: channels-specific error codes. Pre-fix these fell through
|
|
167
|
+
// to the generic 'unknown' kind (errorReplyText: "Hit a snag. Try
|
|
168
|
+
// resending.") which lies about what happened. Mirrors the rc.46→rc.47
|
|
169
|
+
// tmuxToolWedge fix where backend-specific codes needed their own kinds.
|
|
170
|
+
//
|
|
171
|
+
// BRIDGE_DISCONNECTED: thrown by ChannelsProcess when the mcp-bridge
|
|
172
|
+
// socket drops mid-turn (claude crashed, bridge process died, etc).
|
|
173
|
+
// isTransient: true because the daemon retries spawning the backend.
|
|
174
|
+
BRIDGE_DISCONNECTED: {
|
|
175
|
+
kind: 'bridgeDisconnected',
|
|
176
|
+
userMessage: '🔌 Lost the bridge to Claude mid-turn. Retrying — please resend if I don\'t reply in 30s.',
|
|
177
|
+
isTransient: true,
|
|
178
|
+
autoRecover: null,
|
|
179
|
+
},
|
|
180
|
+
// CHANNELS_HANDSHAKE_TIMEOUT: bridge process never sent session_init
|
|
181
|
+
// within the handshake window during start(). Usually means the bridge
|
|
182
|
+
// crashed pre-init or the socket file is stale.
|
|
183
|
+
CHANNELS_HANDSHAKE_TIMEOUT: {
|
|
184
|
+
kind: 'channelsHandshakeTimeout',
|
|
185
|
+
userMessage: '⏳ Couldn\'t start a Claude session — the bridge didn\'t respond in time. Try again in a moment.',
|
|
186
|
+
isTransient: true,
|
|
187
|
+
autoRecover: null,
|
|
188
|
+
},
|
|
189
|
+
// CHANNELS_DIALOG_TIMEOUT: a permission / usage-limit / context-overflow
|
|
190
|
+
// dialog opened mid-turn and we couldn't auto-respond within the dialog
|
|
191
|
+
// window. The turn is dead; user needs to retry.
|
|
192
|
+
CHANNELS_DIALOG_TIMEOUT: {
|
|
193
|
+
kind: 'channelsDialogTimeout',
|
|
194
|
+
userMessage: '🚧 Claude hit a dialog (permission/usage-limit) mid-turn and I couldn\'t auto-respond in time. Please resend.',
|
|
195
|
+
isTransient: false,
|
|
196
|
+
autoRecover: null,
|
|
197
|
+
},
|
|
198
|
+
// TURN_TIMEOUT: 10-min wall-clock cap on a single channels turn. Mirror
|
|
199
|
+
// of the tmux wall-clock ceiling — typically a runaway, not a wedge.
|
|
200
|
+
// Not transient (auto-retry would just runaway again).
|
|
201
|
+
TURN_TIMEOUT: {
|
|
202
|
+
kind: 'turnTimeout',
|
|
203
|
+
userMessage: '⏱ The turn ran past the 10-minute cap. Resend if the answer still matters.',
|
|
204
|
+
isTransient: false,
|
|
205
|
+
autoRecover: null,
|
|
206
|
+
},
|
|
166
207
|
};
|
|
167
208
|
|
|
168
209
|
/**
|
package/lib/handlers/abort.js
CHANGED
|
@@ -41,13 +41,37 @@ function createHandleAbort({
|
|
|
41
41
|
|
|
42
42
|
const threadId = msg.message_thread_id?.toString();
|
|
43
43
|
const sessionKey = getSessionKey(chatId, threadId, chatConfig);
|
|
44
|
-
const
|
|
44
|
+
const proc = pm.has(sessionKey) ? pm.get(sessionKey) : null;
|
|
45
|
+
const hadActive = !!proc?.inFlight;
|
|
45
46
|
|
|
46
47
|
// Mark BEFORE killing: the 'close' event fires almost immediately
|
|
47
48
|
// after interrupt, and the surrounding handleMessage's catch
|
|
48
49
|
// needs to see the flag to skip the generic error-reply.
|
|
49
50
|
if (hadActive) markSessionAborted(sessionKey);
|
|
50
51
|
|
|
52
|
+
// Bug 1 (incident 2026-05-18): "Stop" was turn-scoped — it only
|
|
53
|
+
// looked at an in-flight TURN. But the agent can leave a DETACHED
|
|
54
|
+
// background shell running (a `run_in_background:true` Bash) that
|
|
55
|
+
// outlives the turn; the tmux TUI shows an `N shell` indicator.
|
|
56
|
+
// When there is no live turn, check for such a shell and stop it
|
|
57
|
+
// so "Stop" acts truthfully instead of replying "Nothing to stop"
|
|
58
|
+
// while work is still churning. tmux-only — the SDK Process has no
|
|
59
|
+
// hasBackgroundShell()/killBackgroundShells(); the typeof guards
|
|
60
|
+
// make this a no-op there.
|
|
61
|
+
let killedBackgroundShell = false;
|
|
62
|
+
if (!hadActive && proc
|
|
63
|
+
&& typeof proc.hasBackgroundShell === 'function'
|
|
64
|
+
&& typeof proc.killBackgroundShells === 'function') {
|
|
65
|
+
try {
|
|
66
|
+
if (await proc.hasBackgroundShell()) {
|
|
67
|
+
markSessionAborted(sessionKey);
|
|
68
|
+
killedBackgroundShell = await proc.killBackgroundShells();
|
|
69
|
+
}
|
|
70
|
+
} catch (err) {
|
|
71
|
+
logger.error?.(`[${botName}] background-shell stop failed: ${err.message}`);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
51
75
|
// SDK abort: interrupt() + drainQueue(). interrupt() cancels
|
|
52
76
|
// the in-flight turn at SDK level WITHOUT tearing down the
|
|
53
77
|
// Query (cheap to reuse for the user's next message);
|
|
@@ -62,6 +86,7 @@ function createHandleAbort({
|
|
|
62
86
|
logEvent('abort-requested', {
|
|
63
87
|
chat_id: chatId, user_id: msg.from?.id || null,
|
|
64
88
|
had_active: hadActive,
|
|
89
|
+
killed_background_shell: killedBackgroundShell,
|
|
65
90
|
trigger: cleanText.slice(0, 40),
|
|
66
91
|
});
|
|
67
92
|
|
|
@@ -69,10 +94,23 @@ function createHandleAbort({
|
|
|
69
94
|
// detection is crude but reliable for ru/en.
|
|
70
95
|
const lang = /[а-яё]/i.test(cleanText) ? 'ru' : 'en';
|
|
71
96
|
const strs = {
|
|
72
|
-
en: {
|
|
73
|
-
|
|
97
|
+
en: {
|
|
98
|
+
stopped: 'Stopped.',
|
|
99
|
+
bgStopped: 'Stopped the background task.',
|
|
100
|
+
nothing: 'Nothing to stop.',
|
|
101
|
+
},
|
|
102
|
+
ru: {
|
|
103
|
+
stopped: 'Остановлено.',
|
|
104
|
+
bgStopped: 'Фоновая задача остановлена.',
|
|
105
|
+
nothing: 'Нечего останавливать.',
|
|
106
|
+
},
|
|
74
107
|
}[lang];
|
|
75
|
-
|
|
108
|
+
// Truthful ack: a stopped in-flight turn → "Stopped"; a stopped
|
|
109
|
+
// background shell → "Stopped the background task"; neither →
|
|
110
|
+
// "Nothing to stop".
|
|
111
|
+
const reply = hadActive ? strs.stopped
|
|
112
|
+
: killedBackgroundShell ? strs.bgStopped
|
|
113
|
+
: strs.nothing;
|
|
76
114
|
try {
|
|
77
115
|
await tg(bot, 'sendMessage', {
|
|
78
116
|
chat_id: chatId, text: reply,
|
|
@@ -199,7 +199,17 @@ function createSlashCommands({
|
|
|
199
199
|
}), 'log model change');
|
|
200
200
|
const { anyActive } = await applyConfigChange('model', newModel);
|
|
201
201
|
const ver = (modelVersionsDesc && modelVersionsDesc[newModel]) || newModel;
|
|
202
|
-
|
|
202
|
+
// Review F#10: channels backend can't apply model/effort changes
|
|
203
|
+
// live — its setModel/applyFlagSettings throw UNSUPPORTED_OPERATION,
|
|
204
|
+
// pm.setModel returns false → `anyActive` is true → user saw the
|
|
205
|
+
// misleading "I'll switch when I finish" message. Now we detect
|
|
206
|
+
// the channels backend explicitly and give an honest answer:
|
|
207
|
+
// settings are persisted to chatConfig and take effect on the next
|
|
208
|
+
// /reset or /new (channels lacks an in-place re-init path).
|
|
209
|
+
const backendName = typeof pm.getBackend === 'function' ? pm.getBackend(sessionKey) : null;
|
|
210
|
+
const suffix = backendName === 'channels'
|
|
211
|
+
? ` — applies on next /reset (channels)`
|
|
212
|
+
: (anyActive ? ` — I'll switch when I finish` : '');
|
|
203
213
|
await sendReply(`Model → ${newModel} (${ver})${suffix}`);
|
|
204
214
|
} else {
|
|
205
215
|
await sendReply(`Unknown model. Use: opus, sonnet, haiku`);
|
|
@@ -219,7 +229,17 @@ function createSlashCommands({
|
|
|
219
229
|
user: cmdUser, user_id: cmdUserId, source: 'command',
|
|
220
230
|
}), 'log effort change');
|
|
221
231
|
const { anyActive } = await applyConfigChange('effort', newEffort);
|
|
222
|
-
|
|
232
|
+
// Review F#10: channels backend can't apply model/effort changes
|
|
233
|
+
// live — its setModel/applyFlagSettings throw UNSUPPORTED_OPERATION,
|
|
234
|
+
// pm.setModel returns false → `anyActive` is true → user saw the
|
|
235
|
+
// misleading "I'll switch when I finish" message. Now we detect
|
|
236
|
+
// the channels backend explicitly and give an honest answer:
|
|
237
|
+
// settings are persisted to chatConfig and take effect on the next
|
|
238
|
+
// /reset or /new (channels lacks an in-place re-init path).
|
|
239
|
+
const backendName = typeof pm.getBackend === 'function' ? pm.getBackend(sessionKey) : null;
|
|
240
|
+
const suffix = backendName === 'channels'
|
|
241
|
+
? ` — applies on next /reset (channels)`
|
|
242
|
+
: (anyActive ? ` — I'll switch when I finish` : '');
|
|
223
243
|
await sendReply(`Effort → ${newEffort}${suffix}`);
|
|
224
244
|
} else {
|
|
225
245
|
await sendReply(`Unknown effort. Use: low, medium, high, xhigh, max`);
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bridge ↔ daemon socket protocol — typed schemas.
|
|
3
|
+
*
|
|
4
|
+
* Wire format: newline-delimited JSON over a unix socket per session.
|
|
5
|
+
* Both endpoints (ChannelsProcess and channels-bridge.mjs) speak the same
|
|
6
|
+
* message kinds. This module centralizes the shape so both sides safeParse
|
|
7
|
+
* inbound messages with the same constraints — protecting against malformed
|
|
8
|
+
* payloads silently corrupting pending-state Maps.
|
|
9
|
+
*
|
|
10
|
+
* Adding a new message kind:
|
|
11
|
+
* 1. Define its schema below as `<KindName>MessageSchema`
|
|
12
|
+
* 2. Add it to `AnyDaemonToBridgeMessage` or `AnyBridgeToDaemonMessage`
|
|
13
|
+
* 3. Handle it in the corresponding switch (channels-process.js
|
|
14
|
+
* _onBridgeMsg or channels-bridge.mjs handleDaemonMessage)
|
|
15
|
+
*
|
|
16
|
+
* Validation policy:
|
|
17
|
+
* - Daemon side uses `safeParse` and drops malformed messages with a warn
|
|
18
|
+
* (downgrades silent corruption into observable log)
|
|
19
|
+
* - Bridge side does the same on inbound from daemon
|
|
20
|
+
* - All validation happens AFTER hello-handshake auth (the auth gate is
|
|
21
|
+
* the first line of defense; schema is the second)
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
'use strict';
|
|
25
|
+
|
|
26
|
+
const { z } = require('zod');
|
|
27
|
+
|
|
28
|
+
// ─── shared primitives ─────────────────────────────────────────────
|
|
29
|
+
|
|
30
|
+
const NonEmptyString = z.string().min(1);
|
|
31
|
+
const OptionalString = z.string().optional();
|
|
32
|
+
const ToolCallId = z.string().min(1);
|
|
33
|
+
const RequestId = z.string().min(1);
|
|
34
|
+
const TurnId = z.string().min(1);
|
|
35
|
+
|
|
36
|
+
// ─── bridge → daemon ───────────────────────────────────────────────
|
|
37
|
+
|
|
38
|
+
const HelloSchema = z.object({
|
|
39
|
+
kind: z.literal('hello'),
|
|
40
|
+
session_key: NonEmptyString,
|
|
41
|
+
secret: NonEmptyString,
|
|
42
|
+
}).passthrough();
|
|
43
|
+
|
|
44
|
+
const SessionInitSchema = z.object({
|
|
45
|
+
kind: z.literal('session_init'),
|
|
46
|
+
claude_session_id: z.string(), // may be empty if claude generated one before bridge sees it
|
|
47
|
+
}).passthrough();
|
|
48
|
+
|
|
49
|
+
const ToolCallMessageSchema = z.object({
|
|
50
|
+
kind: z.literal('tool'),
|
|
51
|
+
session: NonEmptyString,
|
|
52
|
+
tool_call_id: ToolCallId,
|
|
53
|
+
name: z.enum(['reply', 'react', 'edit_message']),
|
|
54
|
+
args: z.object({}).passthrough(),
|
|
55
|
+
}).passthrough();
|
|
56
|
+
|
|
57
|
+
const PermRequestMessageSchema = z.object({
|
|
58
|
+
kind: z.literal('perm_req'),
|
|
59
|
+
session: NonEmptyString,
|
|
60
|
+
request_id: RequestId,
|
|
61
|
+
tool_name: NonEmptyString,
|
|
62
|
+
description: z.string(),
|
|
63
|
+
input_preview: z.string(),
|
|
64
|
+
}).passthrough();
|
|
65
|
+
|
|
66
|
+
const PongMessageSchema = z.object({
|
|
67
|
+
kind: z.literal('pong'),
|
|
68
|
+
}).passthrough();
|
|
69
|
+
|
|
70
|
+
const AnyBridgeToDaemonMessage = z.discriminatedUnion('kind', [
|
|
71
|
+
HelloSchema,
|
|
72
|
+
SessionInitSchema,
|
|
73
|
+
ToolCallMessageSchema,
|
|
74
|
+
PermRequestMessageSchema,
|
|
75
|
+
PongMessageSchema,
|
|
76
|
+
]);
|
|
77
|
+
|
|
78
|
+
// ─── daemon → bridge ───────────────────────────────────────────────
|
|
79
|
+
|
|
80
|
+
const HelloAckSchema = z.object({
|
|
81
|
+
kind: z.literal('hello_ack'),
|
|
82
|
+
}).passthrough();
|
|
83
|
+
|
|
84
|
+
const HelloRejectSchema = z.object({
|
|
85
|
+
kind: z.literal('hello_reject'),
|
|
86
|
+
reason: z.string().optional(),
|
|
87
|
+
}).passthrough();
|
|
88
|
+
|
|
89
|
+
const UserMessageSchema = z.object({
|
|
90
|
+
kind: z.literal('user_msg'),
|
|
91
|
+
text: z.string(),
|
|
92
|
+
chat_id: z.union([z.string(), z.number()]).optional(),
|
|
93
|
+
user: OptionalString,
|
|
94
|
+
msg_id: z.union([z.string(), z.number()]).optional(),
|
|
95
|
+
turn_id: OptionalString,
|
|
96
|
+
}).passthrough();
|
|
97
|
+
|
|
98
|
+
const PermVerdictMessageSchema = z.object({
|
|
99
|
+
kind: z.literal('perm_verdict'),
|
|
100
|
+
request_id: RequestId,
|
|
101
|
+
behavior: z.enum(['allow', 'deny']),
|
|
102
|
+
}).passthrough();
|
|
103
|
+
|
|
104
|
+
const ToolAckMessageSchema = z.object({
|
|
105
|
+
kind: z.literal('tool_ack'),
|
|
106
|
+
tool_call_id: ToolCallId,
|
|
107
|
+
ok: z.boolean(),
|
|
108
|
+
error: z.string().optional(),
|
|
109
|
+
}).passthrough();
|
|
110
|
+
|
|
111
|
+
const PingMessageSchema = z.object({
|
|
112
|
+
kind: z.literal('ping'),
|
|
113
|
+
}).passthrough();
|
|
114
|
+
|
|
115
|
+
const AnyDaemonToBridgeMessage = z.discriminatedUnion('kind', [
|
|
116
|
+
HelloAckSchema,
|
|
117
|
+
HelloRejectSchema,
|
|
118
|
+
UserMessageSchema,
|
|
119
|
+
PermVerdictMessageSchema,
|
|
120
|
+
ToolAckMessageSchema,
|
|
121
|
+
PingMessageSchema,
|
|
122
|
+
]);
|
|
123
|
+
|
|
124
|
+
// ─── helpers ──────────────────────────────────────────────────────
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Parse + validate a bridge → daemon message. Returns
|
|
128
|
+
* {ok:true, msg} on success or {ok:false, error} on failure.
|
|
129
|
+
*
|
|
130
|
+
* @param {unknown} raw — already JSON.parsed object
|
|
131
|
+
* @returns {{ok: true, msg: object}|{ok: false, error: string}}
|
|
132
|
+
*/
|
|
133
|
+
function parseBridgeToDaemonMessage(raw) {
|
|
134
|
+
const r = AnyBridgeToDaemonMessage.safeParse(raw);
|
|
135
|
+
if (r.success) return { ok: true, msg: r.data };
|
|
136
|
+
return { ok: false, error: zodErrorBrief(r.error, raw?.kind) };
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function parseDaemonToBridgeMessage(raw) {
|
|
140
|
+
const r = AnyDaemonToBridgeMessage.safeParse(raw);
|
|
141
|
+
if (r.success) return { ok: true, msg: r.data };
|
|
142
|
+
return { ok: false, error: zodErrorBrief(r.error, raw?.kind) };
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function zodErrorBrief(err, kindHint) {
|
|
146
|
+
const issues = (err?.issues || []).slice(0, 3).map(i => `${i.path.join('.')}: ${i.message}`);
|
|
147
|
+
return `kind=${kindHint || '?'} — ${issues.join('; ') || 'unknown'}`;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
module.exports = {
|
|
151
|
+
// schemas (exported for tests + downstream consumers)
|
|
152
|
+
HelloSchema,
|
|
153
|
+
SessionInitSchema,
|
|
154
|
+
ToolCallMessageSchema,
|
|
155
|
+
PermRequestMessageSchema,
|
|
156
|
+
PongMessageSchema,
|
|
157
|
+
AnyBridgeToDaemonMessage,
|
|
158
|
+
HelloAckSchema,
|
|
159
|
+
HelloRejectSchema,
|
|
160
|
+
UserMessageSchema,
|
|
161
|
+
PermVerdictMessageSchema,
|
|
162
|
+
ToolAckMessageSchema,
|
|
163
|
+
PingMessageSchema,
|
|
164
|
+
AnyDaemonToBridgeMessage,
|
|
165
|
+
// helpers
|
|
166
|
+
parseBridgeToDaemonMessage,
|
|
167
|
+
parseDaemonToBridgeMessage,
|
|
168
|
+
};
|