@evident-ai/cli 3.0.1-dev.51bb855 → 3.0.1-dev.5391f43
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/index.js +885 -80
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
// src/index.ts
|
|
4
|
+
import { createRequire } from "module";
|
|
4
5
|
import { Command } from "commander";
|
|
5
6
|
|
|
6
7
|
// src/commands/login.ts
|
|
@@ -470,12 +471,6 @@ import chalk6 from "chalk";
|
|
|
470
471
|
import ora3 from "ora";
|
|
471
472
|
import { select as select3 } from "@inquirer/prompts";
|
|
472
473
|
|
|
473
|
-
// ../../packages/types/src/opencode/index.ts
|
|
474
|
-
function opencodeMessageIdFor(queuedMessageId) {
|
|
475
|
-
const sanitized = queuedMessageId.replace(/[^a-zA-Z0-9]/g, "_");
|
|
476
|
-
return `msg_${sanitized}`;
|
|
477
|
-
}
|
|
478
|
-
|
|
479
474
|
// ../../packages/types/src/telemetry/index.ts
|
|
480
475
|
var TelemetryEventTypes = {
|
|
481
476
|
// Agent activity events (shown in web UI activity log)
|
|
@@ -490,8 +485,34 @@ var TelemetryEventTypes = {
|
|
|
490
485
|
var MAX_FRAME_BYTES = 256 * 1024;
|
|
491
486
|
var TUNNEL_DRAIN_PING_PATH = "/__evident/drain";
|
|
492
487
|
|
|
488
|
+
// ../../packages/types/src/logging/index.ts
|
|
489
|
+
var CORRELATION_ID_HEADER = "x-evident-correlation-id";
|
|
490
|
+
function log(level, event, fields) {
|
|
491
|
+
const method = level === "debug" ? "log" : level;
|
|
492
|
+
try {
|
|
493
|
+
console[method]("[evident]", JSON.stringify({ level, event, ...fields }));
|
|
494
|
+
} catch (err) {
|
|
495
|
+
console.error(
|
|
496
|
+
"[evident] log_serialize_failed",
|
|
497
|
+
event,
|
|
498
|
+
err instanceof Error ? err.message : String(err)
|
|
499
|
+
);
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
function stripQuery(url) {
|
|
503
|
+
try {
|
|
504
|
+
return new URL(url).pathname;
|
|
505
|
+
} catch {
|
|
506
|
+
const q = url.indexOf("?");
|
|
507
|
+
return q === -1 ? url : url.slice(0, q);
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
|
|
493
511
|
// src/lib/telemetry.ts
|
|
494
|
-
var CLI_VERSION = process.env.npm_package_version
|
|
512
|
+
var CLI_VERSION = (true ? "3.0.0" : void 0) ?? process.env.npm_package_version ?? "unknown";
|
|
513
|
+
function getCliVersion() {
|
|
514
|
+
return CLI_VERSION;
|
|
515
|
+
}
|
|
495
516
|
var eventBuffer = [];
|
|
496
517
|
var flushTimeout = null;
|
|
497
518
|
var isShuttingDown = false;
|
|
@@ -691,14 +712,14 @@ async function waitForOpenCodeHealth(port, timeoutMs = 3e4) {
|
|
|
691
712
|
}
|
|
692
713
|
|
|
693
714
|
// src/lib/opencode/opencode-version-gate.ts
|
|
694
|
-
var QUEUE_VALIDATED_OPENCODE_VERSIONS = ["1.17.11"];
|
|
695
|
-
function isQueueValidatedVersion(
|
|
696
|
-
if (!
|
|
697
|
-
return QUEUE_VALIDATED_OPENCODE_VERSIONS.includes(
|
|
698
|
-
}
|
|
699
|
-
function buildOpenCodeVersionWarning(
|
|
700
|
-
if (isQueueValidatedVersion(
|
|
701
|
-
const detected =
|
|
715
|
+
var QUEUE_VALIDATED_OPENCODE_VERSIONS = ["1.17.11", "1.18.3"];
|
|
716
|
+
function isQueueValidatedVersion(version2) {
|
|
717
|
+
if (!version2) return false;
|
|
718
|
+
return QUEUE_VALIDATED_OPENCODE_VERSIONS.includes(version2);
|
|
719
|
+
}
|
|
720
|
+
function buildOpenCodeVersionWarning(version2) {
|
|
721
|
+
if (isQueueValidatedVersion(version2)) return null;
|
|
722
|
+
const detected = version2 ? `v${version2}` : "unknown";
|
|
702
723
|
const validated = QUEUE_VALIDATED_OPENCODE_VERSIONS.map((v) => `v${v}`).join(", ");
|
|
703
724
|
return `Warning: opencode ${detected} is not a queue-validated version (validated: ${validated}). Native message queuing \u2014 which channel (Slack/WhatsApp) message handling relies on \u2014 is unverified on this version; queued/follow-up messages may behave unexpectedly. Continuing anyway. Bumping the validated set requires re-running the queue validation.`;
|
|
704
725
|
}
|
|
@@ -1015,7 +1036,11 @@ function roleOf(m) {
|
|
|
1015
1036
|
}
|
|
1016
1037
|
function completedOf(m) {
|
|
1017
1038
|
if (!m || typeof m !== "object") return void 0;
|
|
1018
|
-
return m.info?.time?.completed;
|
|
1039
|
+
return m.info?.time?.completed ?? m.time?.completed;
|
|
1040
|
+
}
|
|
1041
|
+
function createdOf(m) {
|
|
1042
|
+
if (!m || typeof m !== "object") return void 0;
|
|
1043
|
+
return m.info?.time?.created ?? m.time?.created;
|
|
1019
1044
|
}
|
|
1020
1045
|
function idOf(m) {
|
|
1021
1046
|
if (!m || typeof m !== "object") return void 0;
|
|
@@ -1035,6 +1060,24 @@ function finishOf(m) {
|
|
|
1035
1060
|
const infoFinish = m.info?.finish;
|
|
1036
1061
|
return typeof infoFinish === "string" ? infoFinish : void 0;
|
|
1037
1062
|
}
|
|
1063
|
+
function errorOf(m) {
|
|
1064
|
+
if (!m || typeof m !== "object") return void 0;
|
|
1065
|
+
return m.info?.error ?? m.error;
|
|
1066
|
+
}
|
|
1067
|
+
function isAssistantInFlight(m) {
|
|
1068
|
+
if (completedOf(m) == null) return true;
|
|
1069
|
+
return finishOf(m) === "tool-calls";
|
|
1070
|
+
}
|
|
1071
|
+
async function getSessionMessages(port, sessionId) {
|
|
1072
|
+
try {
|
|
1073
|
+
const res = await fetch(`${opencodeBase(port)}/session/${sessionId}/message`);
|
|
1074
|
+
if (!res.ok) return null;
|
|
1075
|
+
const body = await res.json();
|
|
1076
|
+
return Array.isArray(body) ? body : null;
|
|
1077
|
+
} catch {
|
|
1078
|
+
return null;
|
|
1079
|
+
}
|
|
1080
|
+
}
|
|
1038
1081
|
async function createOpenCodeSession(port, directory) {
|
|
1039
1082
|
const url = new URL(`${opencodeBase(port)}/session`);
|
|
1040
1083
|
if (directory && directory.trim()) {
|
|
@@ -1052,9 +1095,16 @@ async function createOpenCodeSession(port, directory) {
|
|
|
1052
1095
|
const data = await response.json();
|
|
1053
1096
|
return data.id;
|
|
1054
1097
|
}
|
|
1055
|
-
|
|
1098
|
+
function messageText(m) {
|
|
1099
|
+
if (!m || !Array.isArray(m.parts)) return "";
|
|
1100
|
+
return m.parts.filter((p) => p.type === "text" && typeof p.text === "string").map((p) => p.text).join("");
|
|
1101
|
+
}
|
|
1102
|
+
async function sendPromptAsync(port, sessionId, content, options) {
|
|
1103
|
+
const before = await getSessionMessages(port, sessionId);
|
|
1104
|
+
const knownUserIds = new Set(
|
|
1105
|
+
(before ?? []).filter((m) => roleOf(m) === "user").map((m) => idOf(m)).filter((id) => typeof id === "string")
|
|
1106
|
+
);
|
|
1056
1107
|
const body = {
|
|
1057
|
-
messageID: messageId,
|
|
1058
1108
|
parts: [{ type: "text", text: content }]
|
|
1059
1109
|
};
|
|
1060
1110
|
if (options?.agent) {
|
|
@@ -1078,6 +1128,29 @@ async function sendPromptAsync(port, sessionId, content, options, messageId) {
|
|
|
1078
1128
|
const text = await res.text().catch(() => "");
|
|
1079
1129
|
throw new Error(`OpenCode prompt_async failed: HTTP ${res.status}${text ? `: ${text}` : ""}`);
|
|
1080
1130
|
}
|
|
1131
|
+
const READ_BACK_ATTEMPTS = 5;
|
|
1132
|
+
const READ_BACK_DELAY_MS = 150;
|
|
1133
|
+
for (let attempt = 0; attempt < READ_BACK_ATTEMPTS; attempt++) {
|
|
1134
|
+
const after = await getSessionMessages(port, sessionId);
|
|
1135
|
+
if (after) {
|
|
1136
|
+
let best = null;
|
|
1137
|
+
for (const m of after) {
|
|
1138
|
+
if (roleOf(m) !== "user") continue;
|
|
1139
|
+
const id = idOf(m);
|
|
1140
|
+
if (typeof id !== "string" || knownUserIds.has(id)) continue;
|
|
1141
|
+
if (messageText(m) !== content) continue;
|
|
1142
|
+
const created = createdOf(m) ?? 0;
|
|
1143
|
+
if (best === null || created > best.created) {
|
|
1144
|
+
best = { id, created };
|
|
1145
|
+
}
|
|
1146
|
+
}
|
|
1147
|
+
if (best) return best.id;
|
|
1148
|
+
}
|
|
1149
|
+
if (attempt < READ_BACK_ATTEMPTS - 1) {
|
|
1150
|
+
await new Promise((resolve) => setTimeout(resolve, READ_BACK_DELAY_MS));
|
|
1151
|
+
}
|
|
1152
|
+
}
|
|
1153
|
+
return null;
|
|
1081
1154
|
}
|
|
1082
1155
|
function findAssistantReplyAfter(messages, userMessageId) {
|
|
1083
1156
|
if (!messages || messages.length === 0) return null;
|
|
@@ -1094,19 +1167,31 @@ function findAssistantReplyAfter(messages, userMessageId) {
|
|
|
1094
1167
|
}
|
|
1095
1168
|
function findLastAssistantReplyFor(messages, userMessageId) {
|
|
1096
1169
|
if (!messages || messages.length === 0) return null;
|
|
1170
|
+
let lastCorrelated = null;
|
|
1171
|
+
let lastNonErrored = null;
|
|
1097
1172
|
for (let i = messages.length - 1; i >= 0; i--) {
|
|
1098
1173
|
const m = messages[i];
|
|
1099
|
-
if (roleOf(m)
|
|
1174
|
+
if (roleOf(m) !== "assistant" || parentIdOf(m) !== userMessageId) continue;
|
|
1175
|
+
if (lastCorrelated === null) lastCorrelated = m;
|
|
1176
|
+
if (errorOf(m) == null) {
|
|
1177
|
+
lastNonErrored = m;
|
|
1178
|
+
break;
|
|
1179
|
+
}
|
|
1100
1180
|
}
|
|
1181
|
+
if (lastCorrelated) return lastNonErrored ?? lastCorrelated;
|
|
1101
1182
|
const userIndex = messages.findIndex((m) => idOf(m) === userMessageId);
|
|
1102
1183
|
if (userIndex === -1) return null;
|
|
1103
1184
|
let last = null;
|
|
1185
|
+
let lastOk = null;
|
|
1104
1186
|
for (let i = userIndex + 1; i < messages.length; i++) {
|
|
1105
1187
|
const role = roleOf(messages[i]);
|
|
1106
1188
|
if (role === "user") break;
|
|
1107
|
-
if (role === "assistant")
|
|
1189
|
+
if (role === "assistant") {
|
|
1190
|
+
last = messages[i];
|
|
1191
|
+
if (errorOf(messages[i]) == null) lastOk = messages[i];
|
|
1192
|
+
}
|
|
1108
1193
|
}
|
|
1109
|
-
return last;
|
|
1194
|
+
return lastOk ?? last;
|
|
1110
1195
|
}
|
|
1111
1196
|
function messageRunState(messages, userMessageId) {
|
|
1112
1197
|
if (!messages || messages.length === 0) return "unknown";
|
|
@@ -1116,12 +1201,27 @@ function messageRunState(messages, userMessageId) {
|
|
|
1116
1201
|
if (!reply) return "unknown";
|
|
1117
1202
|
}
|
|
1118
1203
|
if (!reply) return "queued";
|
|
1119
|
-
if (
|
|
1120
|
-
|
|
1121
|
-
return "done";
|
|
1204
|
+
if (isAssistantInFlight(reply)) return "running";
|
|
1205
|
+
return errorOf(reply) != null ? "failed" : "done";
|
|
1122
1206
|
}
|
|
1123
|
-
function
|
|
1124
|
-
|
|
1207
|
+
function messageError(messages, userMessageId) {
|
|
1208
|
+
const reply = findLastAssistantReplyFor(messages, userMessageId);
|
|
1209
|
+
const error2 = errorOf(reply);
|
|
1210
|
+
if (error2 == null) return null;
|
|
1211
|
+
if (typeof error2 === "string") return error2;
|
|
1212
|
+
if (typeof error2 === "object") {
|
|
1213
|
+
const e = error2;
|
|
1214
|
+
const dataMessage = e.data?.message;
|
|
1215
|
+
if (typeof dataMessage === "string") return dataMessage;
|
|
1216
|
+
if (typeof e.message === "string") return e.message;
|
|
1217
|
+
}
|
|
1218
|
+
return "The agent run failed.";
|
|
1219
|
+
}
|
|
1220
|
+
function hasRunningAssistantExcept(messages, exceptUserMessageId) {
|
|
1221
|
+
if (!messages || messages.length === 0) return false;
|
|
1222
|
+
return messages.some(
|
|
1223
|
+
(m) => roleOf(m) === "assistant" && parentIdOf(m) !== exceptUserMessageId && isAssistantInFlight(m)
|
|
1224
|
+
);
|
|
1125
1225
|
}
|
|
1126
1226
|
|
|
1127
1227
|
// src/lib/tunnel/connection.ts
|
|
@@ -1192,12 +1292,22 @@ var StreamForwarder = class {
|
|
|
1192
1292
|
}
|
|
1193
1293
|
async handleOpen(frame) {
|
|
1194
1294
|
const { sid, method, path, headers, has_body } = frame;
|
|
1295
|
+
const correlationId = headers?.[CORRELATION_ID_HEADER];
|
|
1296
|
+
const startedAt = Date.now();
|
|
1195
1297
|
if (path === TUNNEL_DRAIN_PING_PATH) {
|
|
1196
1298
|
this.callbacks.onDrainPing?.();
|
|
1197
1299
|
this.send({ type: "head", sid, status: 204, headers: {} });
|
|
1198
1300
|
this.send({ type: "res_end", sid });
|
|
1199
1301
|
return;
|
|
1200
1302
|
}
|
|
1303
|
+
if (process.env.DEBUG) {
|
|
1304
|
+
log("debug", "agent_request", {
|
|
1305
|
+
correlation_id: correlationId,
|
|
1306
|
+
sid,
|
|
1307
|
+
method,
|
|
1308
|
+
path: stripQuery(path)
|
|
1309
|
+
});
|
|
1310
|
+
}
|
|
1201
1311
|
const ac = new AbortController();
|
|
1202
1312
|
let bodyPromise;
|
|
1203
1313
|
let pushBody;
|
|
@@ -1244,6 +1354,14 @@ var StreamForwarder = class {
|
|
|
1244
1354
|
if (!STRIP_RES.has(key.toLowerCase())) resHeaders[key] = value;
|
|
1245
1355
|
});
|
|
1246
1356
|
this.send({ type: "head", sid, status: upstream.status, headers: resHeaders });
|
|
1357
|
+
if (process.env.DEBUG) {
|
|
1358
|
+
log("debug", "agent_response", {
|
|
1359
|
+
correlation_id: correlationId,
|
|
1360
|
+
sid,
|
|
1361
|
+
status: upstream.status,
|
|
1362
|
+
duration_ms: Date.now() - startedAt
|
|
1363
|
+
});
|
|
1364
|
+
}
|
|
1247
1365
|
this.callbacks.onHead?.(sid, upstream.status);
|
|
1248
1366
|
try {
|
|
1249
1367
|
if (upstream.body) {
|
|
@@ -1513,7 +1631,7 @@ var DEFAULT_RETRY_POLICY = {
|
|
|
1513
1631
|
};
|
|
1514
1632
|
var DEFAULT_PAUSED_POLL_INTERVAL_MS = 2e3;
|
|
1515
1633
|
var DEFAULT_PAUSED_MAX_WAIT_MS = 10 * 60 * 1e3;
|
|
1516
|
-
var
|
|
1634
|
+
var DEFAULT_STUCK_QUEUED_MS = 6e4;
|
|
1517
1635
|
var ChannelAuthError = class extends Error {
|
|
1518
1636
|
constructor(message) {
|
|
1519
1637
|
super(message);
|
|
@@ -1548,10 +1666,18 @@ var ChannelDriver = class {
|
|
|
1548
1666
|
sleep;
|
|
1549
1667
|
pausedPollIntervalMs;
|
|
1550
1668
|
pausedMaxWaitMs;
|
|
1551
|
-
|
|
1669
|
+
stuckQueuedMs;
|
|
1552
1670
|
now;
|
|
1553
1671
|
/** Cache of conversationId → opencode sessionId. */
|
|
1554
1672
|
sessions = /* @__PURE__ */ new Map();
|
|
1673
|
+
/**
|
|
1674
|
+
* Per-opencode-session dispatch lock (Task 2.1a). `sendPromptAsync` is no
|
|
1675
|
+
* longer idempotent (no caller-supplied `messageID`), and its read-back picks
|
|
1676
|
+
* "the one new user row" — which is only unambiguous if no OTHER dispatch into
|
|
1677
|
+
* the SAME session interleaves its snapshot→POST→read-back. This map chains each
|
|
1678
|
+
* session's dispatches so they run serially; distinct sessions stay concurrent.
|
|
1679
|
+
*/
|
|
1680
|
+
sessionDispatchLocks = /* @__PURE__ */ new Map();
|
|
1555
1681
|
/**
|
|
1556
1682
|
* Per-SESSION watchers (WI-3), keyed by opencode sessionId. Single-flight per
|
|
1557
1683
|
* session: one polling loop services all of that session's in-flight messages.
|
|
@@ -1568,6 +1694,54 @@ var ChannelDriver = class {
|
|
|
1568
1694
|
* a steady-state-poll re-dispatch will not double-run the message.
|
|
1569
1695
|
*/
|
|
1570
1696
|
dispatched = /* @__PURE__ */ new Set();
|
|
1697
|
+
/**
|
|
1698
|
+
* Re-adopted (ADR-0046) Evident message ids currently tracked by a watcher.
|
|
1699
|
+
* Used only to distinguish a RE-ADOPTED give-up from a normal-dispatch give-up
|
|
1700
|
+
* so the former can be parked in `dontRedispatch` (Bug 2). A row is added when
|
|
1701
|
+
* it is re-adopted and removed when its watcher settles or it is observed off
|
|
1702
|
+
* the processing list.
|
|
1703
|
+
*/
|
|
1704
|
+
readopted = /* @__PURE__ */ new Set();
|
|
1705
|
+
/**
|
|
1706
|
+
* "Don't re-DISPATCH / re-attach this orphan again" (Bug 2/5). Set when a
|
|
1707
|
+
* re-adopted running/orphan row's watcher hit its `processed_at`-anchored
|
|
1708
|
+
* deadline (or an orphan whose window already elapsed): the still-`processing`
|
|
1709
|
+
* server row would otherwise be re-adopted (and re-dispatched) on EVERY ~2s
|
|
1710
|
+
* drain until the 15-min cron resets it — spamming new turns.
|
|
1711
|
+
*
|
|
1712
|
+
* CRITICAL (Bugbot #202): this suppresses ONLY the dispatch/re-attach paths, it
|
|
1713
|
+
* does NOT suppress DONE delivery. A row parked here whose reply later COMPLETES
|
|
1714
|
+
* in opencode must still be delivered via `markDone` on the next drain — so
|
|
1715
|
+
* `readoptOne` computes `state` FIRST and this set is checked only on the
|
|
1716
|
+
* non-done path. It is cleared once the row leaves the processing list (cron
|
|
1717
|
+
* reset → it drains normally as `pending`), so it can never leak.
|
|
1718
|
+
*/
|
|
1719
|
+
dontRedispatch = /* @__PURE__ */ new Set();
|
|
1720
|
+
/**
|
|
1721
|
+
* "markDone for this row is TERMINALLY undeliverable" (Bug 4). Set ONLY when a
|
|
1722
|
+
* re-adopted DONE row's `markDone` returned a terminal 4xx (a status that will
|
|
1723
|
+
* never succeed). Checked at the TOP of the `done` branch so we do NOT re-attempt
|
|
1724
|
+
* that markDone every ~2s drain while the row stays `processing`. A TRANSIENT
|
|
1725
|
+
* markDone failure must NOT land here (it must still retry next drain). Separate
|
|
1726
|
+
* from `dontRedispatch` because the two concerns are independent: a row can need
|
|
1727
|
+
* "stop re-dispatching" without "stop delivering", and vice versa. Cleared once
|
|
1728
|
+
* the row leaves the processing list, exactly like `dontRedispatch`.
|
|
1729
|
+
*/
|
|
1730
|
+
doneUndeliverable = /* @__PURE__ */ new Set();
|
|
1731
|
+
/**
|
|
1732
|
+
* "A null-id re-adopt re-dispatch is in flight, awaiting its read-back" (WI-5
|
|
1733
|
+
* Task 5.4, High-2). Since we no longer send a caller-supplied id, a re-dispatch
|
|
1734
|
+
* is NOT idempotent: if `forceReadoptRun` dispatches on tick N but the read-back +
|
|
1735
|
+
* persist hasn't landed before tick N+1 re-reads the still-null
|
|
1736
|
+
* `row.opencode_message_id`, tick N+1 would dispatch AGAIN → duplicate user turns.
|
|
1737
|
+
* A row is added here right before its `sendPromptAsync` and `forceReadoptRun`
|
|
1738
|
+
* short-circuits while it is present, so a null-id row is re-dispatched AT MOST
|
|
1739
|
+
* ONCE per outstanding read-back. Cleared on a SUCCESSFUL dispatch+read-back (the
|
|
1740
|
+
* row is then tracked in `dispatched`, so `readoptOne`'s early skip prevents
|
|
1741
|
+
* re-entry) OR on a failed/unresolved dispatch (the message is genuinely un-sent,
|
|
1742
|
+
* so the NEXT tick may retry exactly once more).
|
|
1743
|
+
*/
|
|
1744
|
+
awaitingReadopt = /* @__PURE__ */ new Set();
|
|
1571
1745
|
/**
|
|
1572
1746
|
* Cache of the opencode root directory (from `GET /path`). Resolved lazily on
|
|
1573
1747
|
* first session creation so drain-created sessions are rooted at the project
|
|
@@ -1575,6 +1749,16 @@ var ChannelDriver = class {
|
|
|
1575
1749
|
* not yet resolved; `null` = resolved-but-unavailable (don't keep retrying).
|
|
1576
1750
|
*/
|
|
1577
1751
|
opencodeDirectory = void 0;
|
|
1752
|
+
/**
|
|
1753
|
+
* Cache of opencode `sessionId → parentID` (its parent session, or `null` when
|
|
1754
|
+
* the session is a root with no parent). Sub-agents spawned via the `task` tool
|
|
1755
|
+
* run in CHILD sessions whose `parentID` chains up to the Evident-created
|
|
1756
|
+
* (watched) session; we resolve this once per session so a child-session
|
|
1757
|
+
* question/permission can be attributed to the watched session's subtree
|
|
1758
|
+
* (`sessionBelongsTo`) instead of being dropped by an exact-id filter. A missing
|
|
1759
|
+
* entry = not yet resolved; `null` = resolved root (stop walking).
|
|
1760
|
+
*/
|
|
1761
|
+
sessionParents = /* @__PURE__ */ new Map();
|
|
1578
1762
|
/** Serialises drains so a reconnect during a drain doesn't double-process. */
|
|
1579
1763
|
draining = false;
|
|
1580
1764
|
constructor(config2) {
|
|
@@ -1590,7 +1774,7 @@ var ChannelDriver = class {
|
|
|
1590
1774
|
this.sleep = config2.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
|
|
1591
1775
|
this.pausedPollIntervalMs = config2.pausedPollIntervalMs ?? DEFAULT_PAUSED_POLL_INTERVAL_MS;
|
|
1592
1776
|
this.pausedMaxWaitMs = config2.pausedMaxWaitMs ?? DEFAULT_PAUSED_MAX_WAIT_MS;
|
|
1593
|
-
this.
|
|
1777
|
+
this.stuckQueuedMs = config2.stuckQueuedMs ?? DEFAULT_STUCK_QUEUED_MS;
|
|
1594
1778
|
this.now = config2.now ?? (() => Date.now());
|
|
1595
1779
|
}
|
|
1596
1780
|
/** The IPv4-loopback base URL for the local `opencode serve`. */
|
|
@@ -1623,6 +1807,7 @@ var ChannelDriver = class {
|
|
|
1623
1807
|
for (const conv of conversations) {
|
|
1624
1808
|
dispatched += await this.processConversation(conv);
|
|
1625
1809
|
}
|
|
1810
|
+
await this.readoptProcessing();
|
|
1626
1811
|
} finally {
|
|
1627
1812
|
this.draining = false;
|
|
1628
1813
|
}
|
|
@@ -1680,11 +1865,11 @@ var ChannelDriver = class {
|
|
|
1680
1865
|
skippedAlreadyDispatched += 1;
|
|
1681
1866
|
continue;
|
|
1682
1867
|
}
|
|
1683
|
-
const opencodeMessageId = opencodeMessageIdFor2(message.id);
|
|
1684
1868
|
const options = {
|
|
1685
1869
|
agent: message.opencode_agent ?? void 0,
|
|
1686
1870
|
model: message.opencode_model ?? void 0
|
|
1687
1871
|
};
|
|
1872
|
+
let opencodeMessageId;
|
|
1688
1873
|
try {
|
|
1689
1874
|
this.log({
|
|
1690
1875
|
level: "info",
|
|
@@ -1692,7 +1877,10 @@ var ChannelDriver = class {
|
|
|
1692
1877
|
conversation_id: conv.id,
|
|
1693
1878
|
message_id: message.id
|
|
1694
1879
|
});
|
|
1695
|
-
await
|
|
1880
|
+
opencodeMessageId = await this.dispatchLocked(
|
|
1881
|
+
sessionId,
|
|
1882
|
+
() => sendPromptAsync(this.port, sessionId, message.content, options)
|
|
1883
|
+
);
|
|
1696
1884
|
} catch (err) {
|
|
1697
1885
|
if (err instanceof ChannelAuthError) throw err;
|
|
1698
1886
|
this.dispatched.delete(message.id);
|
|
@@ -1706,9 +1894,19 @@ var ChannelDriver = class {
|
|
|
1706
1894
|
});
|
|
1707
1895
|
continue;
|
|
1708
1896
|
}
|
|
1897
|
+
if (opencodeMessageId === null) {
|
|
1898
|
+
this.log({
|
|
1899
|
+
level: "error",
|
|
1900
|
+
message: `Message ${message.id.slice(0, 8)} dispatched but its opencode id could not be read back \u2014 leaving un-tracked to retry next tick`,
|
|
1901
|
+
conversation_id: conv.id,
|
|
1902
|
+
message_id: message.id
|
|
1903
|
+
});
|
|
1904
|
+
continue;
|
|
1905
|
+
}
|
|
1709
1906
|
this.dispatched.add(message.id);
|
|
1710
1907
|
this.registerInFlight(conv, sessionId, message, opencodeMessageId);
|
|
1711
1908
|
dispatched += 1;
|
|
1909
|
+
void this.postSignal(conv.id, message.id, "dispatched");
|
|
1712
1910
|
}
|
|
1713
1911
|
if (messages.length > 0 && dispatched === 0 && skippedAlreadyDispatched === messages.length) {
|
|
1714
1912
|
this.log({
|
|
@@ -1753,6 +1951,25 @@ var ChannelDriver = class {
|
|
|
1753
1951
|
// -------------------------------------------------------------------------
|
|
1754
1952
|
// Per-session watcher (WI-3)
|
|
1755
1953
|
// -------------------------------------------------------------------------
|
|
1954
|
+
/**
|
|
1955
|
+
* Run one dispatch (`sendPromptAsync` snapshot→POST→read-back) serialized per
|
|
1956
|
+
* opencode session (Task 2.1a), so two dispatches into the SAME session can
|
|
1957
|
+
* never interleave and mis-correlate their read-backs. Distinct sessions run
|
|
1958
|
+
* concurrently. The chained tail intentionally ignores the prior result/error
|
|
1959
|
+
* (each dispatch reports its own outcome to its caller).
|
|
1960
|
+
*/
|
|
1961
|
+
dispatchLocked(sessionId, fn) {
|
|
1962
|
+
const prior = this.sessionDispatchLocks.get(sessionId) ?? Promise.resolve();
|
|
1963
|
+
const run2 = prior.then(fn, fn);
|
|
1964
|
+
this.sessionDispatchLocks.set(
|
|
1965
|
+
sessionId,
|
|
1966
|
+
run2.then(
|
|
1967
|
+
() => void 0,
|
|
1968
|
+
() => void 0
|
|
1969
|
+
)
|
|
1970
|
+
);
|
|
1971
|
+
return run2;
|
|
1972
|
+
}
|
|
1756
1973
|
/** Register a freshly-dispatched message with its session's watcher state. */
|
|
1757
1974
|
registerInFlight(conv, sessionId, message, opencodeMessageId) {
|
|
1758
1975
|
let watcher = this.watchers.get(sessionId);
|
|
@@ -1774,7 +1991,54 @@ var ChannelDriver = class {
|
|
|
1774
1991
|
dispatchedAt: now,
|
|
1775
1992
|
deadline: now + this.pausedMaxWaitMs,
|
|
1776
1993
|
started: false,
|
|
1777
|
-
done: false
|
|
1994
|
+
done: false,
|
|
1995
|
+
stuckReported: false
|
|
1996
|
+
});
|
|
1997
|
+
}
|
|
1998
|
+
/**
|
|
1999
|
+
* Register a RE-ADOPTED `processing` message with its session watcher
|
|
2000
|
+
* (ADR-0046, WI-4). Mirrors `registerInFlight` but anchors the give-up
|
|
2001
|
+
* `deadline` to the row's SERVER-SIDE `processed_at` (Invariant 1), NEVER to
|
|
2002
|
+
* `now`: a row already `processing` for e.g. 5 min must give up ~5 min from now
|
|
2003
|
+
* (10 min after `processed_at`), not 10 min from now — otherwise its deadline
|
|
2004
|
+
* lands ~15 min after `processed_at`, coinciding with the cron reset →
|
|
2005
|
+
* double-drive race. `dispatchedAt` stays `now` (only the appear-guard uses it).
|
|
2006
|
+
*
|
|
2007
|
+
* `evidentMessageId` addresses the SERVER row (for markProcessing/markDone);
|
|
2008
|
+
* `opencodeMessageId` is the id the watcher polls for a reply — for the orphan
|
|
2009
|
+
* fresh-run path these differ (a fresh opencode id under the same server row).
|
|
2010
|
+
*
|
|
2011
|
+
* `started` is set true so the watcher does NOT re-`markProcessing` a row the
|
|
2012
|
+
* server already flipped to `processing`; the running/done transitions still
|
|
2013
|
+
* fire from the watcher's normal branches.
|
|
2014
|
+
*/
|
|
2015
|
+
registerReadopted(conv, sessionId, message, opencodeMessageId, processedAtMs) {
|
|
2016
|
+
let watcher = this.watchers.get(sessionId);
|
|
2017
|
+
if (!watcher) {
|
|
2018
|
+
watcher = {
|
|
2019
|
+
conv,
|
|
2020
|
+
inFlight: /* @__PURE__ */ new Map(),
|
|
2021
|
+
loop: null,
|
|
2022
|
+
reportedQuestions: /* @__PURE__ */ new Set(),
|
|
2023
|
+
reportedPermissions: /* @__PURE__ */ new Set()
|
|
2024
|
+
};
|
|
2025
|
+
this.watchers.set(sessionId, watcher);
|
|
2026
|
+
}
|
|
2027
|
+
watcher.inFlight.set(message.id, {
|
|
2028
|
+
evidentMessageId: message.id,
|
|
2029
|
+
opencodeMessageId,
|
|
2030
|
+
message,
|
|
2031
|
+
dispatchedAt: this.now(),
|
|
2032
|
+
deadline: processedAtMs + this.pausedMaxWaitMs,
|
|
2033
|
+
// The server row is ALREADY `processing`; do not re-fire markProcessing.
|
|
2034
|
+
started: true,
|
|
2035
|
+
done: false,
|
|
2036
|
+
// Not yet reported stuck-queued. The once-guard (`stuckReported`) applies,
|
|
2037
|
+
// AND the stuck-queued observer INCLUDES re-adopted queued wedges: it gates
|
|
2038
|
+
// on `state === 'queued'` (turn produced no reply), not on `started`, so a
|
|
2039
|
+
// re-adopted row left wedged in `queued` still emits the signal once
|
|
2040
|
+
// (#210/#220 observability).
|
|
2041
|
+
stuckReported: false
|
|
1778
2042
|
});
|
|
1779
2043
|
}
|
|
1780
2044
|
/**
|
|
@@ -1840,6 +2104,7 @@ var ChannelDriver = class {
|
|
|
1840
2104
|
conversation_id: watcher.conv.id
|
|
1841
2105
|
});
|
|
1842
2106
|
for (const evidentMessageId of [...watcher.inFlight.keys()]) {
|
|
2107
|
+
this.readopted.delete(evidentMessageId);
|
|
1843
2108
|
this.removeInFlight(watcher, evidentMessageId);
|
|
1844
2109
|
}
|
|
1845
2110
|
return;
|
|
@@ -1860,10 +2125,15 @@ var ChannelDriver = class {
|
|
|
1860
2125
|
async serviceInFlightMessage(sessionId, watcher, inFlight, messages) {
|
|
1861
2126
|
const conv = watcher.conv;
|
|
1862
2127
|
const state = messageRunState(messages, inFlight.opencodeMessageId);
|
|
1863
|
-
if ((state === "running" || state === "done") && !inFlight.started) {
|
|
2128
|
+
if ((state === "running" || state === "done" || state === "failed") && !inFlight.started) {
|
|
1864
2129
|
let claimed;
|
|
1865
2130
|
try {
|
|
1866
|
-
claimed = await this.markProcessing(
|
|
2131
|
+
claimed = await this.markProcessing(
|
|
2132
|
+
conv.id,
|
|
2133
|
+
inFlight.evidentMessageId,
|
|
2134
|
+
sessionId,
|
|
2135
|
+
inFlight.opencodeMessageId
|
|
2136
|
+
);
|
|
1867
2137
|
} catch (err) {
|
|
1868
2138
|
if (err instanceof ChannelAuthError) throw err;
|
|
1869
2139
|
this.log({
|
|
@@ -1893,7 +2163,12 @@ var ChannelDriver = class {
|
|
|
1893
2163
|
message_id: inFlight.evidentMessageId
|
|
1894
2164
|
});
|
|
1895
2165
|
try {
|
|
1896
|
-
await this.markDone(
|
|
2166
|
+
await this.markDone(
|
|
2167
|
+
conv.id,
|
|
2168
|
+
inFlight.evidentMessageId,
|
|
2169
|
+
sessionId,
|
|
2170
|
+
inFlight.opencodeMessageId
|
|
2171
|
+
);
|
|
1897
2172
|
} catch (err) {
|
|
1898
2173
|
if (err instanceof ChannelAuthError) throw err;
|
|
1899
2174
|
if (err instanceof ChannelTerminalError) {
|
|
@@ -1929,10 +2204,59 @@ var ChannelDriver = class {
|
|
|
1929
2204
|
this.removeInFlight(watcher, inFlight.evidentMessageId);
|
|
1930
2205
|
return;
|
|
1931
2206
|
}
|
|
1932
|
-
if (state === "
|
|
1933
|
-
if (
|
|
1934
|
-
|
|
2207
|
+
if (state === "failed") {
|
|
2208
|
+
if (!inFlight.done) {
|
|
2209
|
+
const error2 = messageError(messages, inFlight.opencodeMessageId) ?? void 0;
|
|
2210
|
+
this.log({
|
|
2211
|
+
level: "error",
|
|
2212
|
+
message: `Message ${inFlight.evidentMessageId.slice(0, 8)} errored \u2014 marking failed: ${error2 ?? "(no error text)"}`,
|
|
2213
|
+
conversation_id: conv.id,
|
|
2214
|
+
message_id: inFlight.evidentMessageId
|
|
2215
|
+
});
|
|
2216
|
+
try {
|
|
2217
|
+
await this.markFailed(conv.id, inFlight.evidentMessageId, sessionId, error2);
|
|
2218
|
+
} catch (err) {
|
|
2219
|
+
if (err instanceof ChannelAuthError) throw err;
|
|
2220
|
+
if (err instanceof ChannelTerminalError) {
|
|
2221
|
+
this.log({
|
|
2222
|
+
level: "error",
|
|
2223
|
+
message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} failed (terminal HTTP ${err.status}) \u2014 leaving for the cron safety net: ${err.message}`,
|
|
2224
|
+
conversation_id: conv.id,
|
|
2225
|
+
message_id: inFlight.evidentMessageId
|
|
2226
|
+
});
|
|
2227
|
+
this.removeInFlight(watcher, inFlight.evidentMessageId);
|
|
2228
|
+
return;
|
|
2229
|
+
}
|
|
2230
|
+
if (this.now() >= inFlight.deadline) {
|
|
2231
|
+
this.log({
|
|
2232
|
+
level: "error",
|
|
2233
|
+
message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} failed within the watch window \u2014 leaving for the cron safety net: ${err instanceof Error ? err.message : String(err)}`,
|
|
2234
|
+
conversation_id: conv.id,
|
|
2235
|
+
message_id: inFlight.evidentMessageId
|
|
2236
|
+
});
|
|
2237
|
+
this.removeInFlight(watcher, inFlight.evidentMessageId);
|
|
2238
|
+
return;
|
|
2239
|
+
}
|
|
2240
|
+
this.log({
|
|
2241
|
+
level: "error",
|
|
2242
|
+
message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} failed (will retry next tick): ${err instanceof Error ? err.message : String(err)}`,
|
|
2243
|
+
conversation_id: conv.id,
|
|
2244
|
+
message_id: inFlight.evidentMessageId
|
|
2245
|
+
});
|
|
2246
|
+
return;
|
|
2247
|
+
}
|
|
2248
|
+
inFlight.done = true;
|
|
1935
2249
|
}
|
|
2250
|
+
this.removeInFlight(watcher, inFlight.evidentMessageId);
|
|
2251
|
+
return;
|
|
2252
|
+
}
|
|
2253
|
+
const pastStuckBound = this.now() - inFlight.dispatchedAt >= this.stuckQueuedMs;
|
|
2254
|
+
const sessionIdle = state === "queued" && !hasRunningAssistantExcept(messages, inFlight.opencodeMessageId);
|
|
2255
|
+
if (state === "queued" && pastStuckBound && sessionIdle && !inFlight.stuckReported) {
|
|
2256
|
+
inFlight.stuckReported = true;
|
|
2257
|
+
void this.postSignal(conv.id, inFlight.evidentMessageId, "stuck_queued", {
|
|
2258
|
+
stuck_for_ms: this.now() - inFlight.dispatchedAt
|
|
2259
|
+
});
|
|
1936
2260
|
}
|
|
1937
2261
|
if (this.now() >= inFlight.deadline) {
|
|
1938
2262
|
this.log({
|
|
@@ -1941,48 +2265,386 @@ var ChannelDriver = class {
|
|
|
1941
2265
|
conversation_id: conv.id,
|
|
1942
2266
|
message_id: inFlight.evidentMessageId
|
|
1943
2267
|
});
|
|
2268
|
+
void this.postSignal(conv.id, inFlight.evidentMessageId, "gave_up", {
|
|
2269
|
+
watched_for_ms: this.now() - inFlight.dispatchedAt
|
|
2270
|
+
});
|
|
1944
2271
|
this.removeInFlight(watcher, inFlight.evidentMessageId);
|
|
1945
2272
|
}
|
|
1946
2273
|
}
|
|
2274
|
+
// -------------------------------------------------------------------------
|
|
2275
|
+
// Restart recovery: re-adopt `processing` messages (ADR-0046, WI-3/4/5)
|
|
2276
|
+
// -------------------------------------------------------------------------
|
|
1947
2277
|
/**
|
|
1948
|
-
* Re-
|
|
1949
|
-
*
|
|
1950
|
-
*
|
|
1951
|
-
* the
|
|
2278
|
+
* Re-adopt this agent's `processing` messages on drain (ADR-0046 Decision §1).
|
|
2279
|
+
*
|
|
2280
|
+
* The pending drain only re-drives `pending` rows; a message already flipped to
|
|
2281
|
+
* `processing` before the runner died is watched by nobody until the 15-min
|
|
2282
|
+
* cron resets it. Here we fetch those rows, and per row resolve its correlated
|
|
2283
|
+
* reply against opencode's OWN session store — completing, re-attaching, or
|
|
2284
|
+
* (for an orphan) forcing a genuine fresh run. Runs on EVERY drain tick, so it
|
|
2285
|
+
* is idempotent per message (Invariant 2): a row a watcher already tracks is
|
|
2286
|
+
* skipped in `readoptOne` — one driver, no double-drive.
|
|
2287
|
+
*
|
|
2288
|
+
* Only `ChannelAuthError` propagates (to `drainPending`, like the pending
|
|
2289
|
+
* path); every other early return LOGS a reason with context — no silent drop.
|
|
1952
2290
|
*/
|
|
1953
|
-
async
|
|
2291
|
+
async readoptProcessing() {
|
|
2292
|
+
const rows = await this.getProcessingMessages();
|
|
2293
|
+
if (this.dontRedispatch.size > 0 || this.doneUndeliverable.size > 0) {
|
|
2294
|
+
const stillProcessing = new Set(rows.map((r) => r.id));
|
|
2295
|
+
for (const id of [...this.dontRedispatch, ...this.doneUndeliverable]) {
|
|
2296
|
+
if (!stillProcessing.has(id)) {
|
|
2297
|
+
const cleared = this.dontRedispatch.delete(id);
|
|
2298
|
+
const clearedUndeliverable = this.doneUndeliverable.delete(id);
|
|
2299
|
+
if (cleared || clearedUndeliverable) {
|
|
2300
|
+
this.log({
|
|
2301
|
+
level: "info",
|
|
2302
|
+
message: `Re-adopt: message ${id.slice(0, 8)} left the processing list (cron reset) \u2014 cleared gave-up marker`,
|
|
2303
|
+
message_id: id
|
|
2304
|
+
});
|
|
2305
|
+
}
|
|
2306
|
+
}
|
|
2307
|
+
}
|
|
2308
|
+
}
|
|
2309
|
+
if (rows.length === 0) return;
|
|
2310
|
+
const bySession = /* @__PURE__ */ new Map();
|
|
2311
|
+
for (const row of rows) {
|
|
2312
|
+
if (!row.opencode_session_id) {
|
|
2313
|
+
this.log({
|
|
2314
|
+
level: "error",
|
|
2315
|
+
message: `Cannot re-adopt processing message ${row.id.slice(0, 8)} \u2014 no opencode session id; leaving for the cron safety net`,
|
|
2316
|
+
conversation_id: row.conversation_id,
|
|
2317
|
+
message_id: row.id
|
|
2318
|
+
});
|
|
2319
|
+
continue;
|
|
2320
|
+
}
|
|
2321
|
+
const list = bySession.get(row.opencode_session_id) ?? [];
|
|
2322
|
+
list.push(row);
|
|
2323
|
+
bySession.set(row.opencode_session_id, list);
|
|
2324
|
+
}
|
|
2325
|
+
for (const [sessionId, sessionRows] of bySession) {
|
|
2326
|
+
let messages;
|
|
2327
|
+
try {
|
|
2328
|
+
const res = await this.fetchImpl(`${this.opencodeBase}/session/${sessionId}/message`);
|
|
2329
|
+
if (!res.ok) {
|
|
2330
|
+
this.log({
|
|
2331
|
+
level: "error",
|
|
2332
|
+
message: `Re-adopt: polling session ${sessionId.slice(0, 8)} returned HTTP ${res.status} \u2014 skipping this session this tick`
|
|
2333
|
+
});
|
|
2334
|
+
continue;
|
|
2335
|
+
}
|
|
2336
|
+
const body = await res.json();
|
|
2337
|
+
if (!Array.isArray(body)) {
|
|
2338
|
+
this.log({
|
|
2339
|
+
level: "error",
|
|
2340
|
+
message: `Re-adopt: polling session ${sessionId.slice(0, 8)} returned a non-array message body \u2014 skipping this session this tick`
|
|
2341
|
+
});
|
|
2342
|
+
continue;
|
|
2343
|
+
}
|
|
2344
|
+
messages = body;
|
|
2345
|
+
} catch (err) {
|
|
2346
|
+
this.log({
|
|
2347
|
+
level: "error",
|
|
2348
|
+
message: `Re-adopt: failed to poll session ${sessionId.slice(0, 8)} (will retry next drain): ${err instanceof Error ? err.message : String(err)}`
|
|
2349
|
+
});
|
|
2350
|
+
continue;
|
|
2351
|
+
}
|
|
2352
|
+
for (const row of sessionRows) {
|
|
2353
|
+
await this.readoptOne(sessionId, row, messages);
|
|
2354
|
+
}
|
|
2355
|
+
}
|
|
2356
|
+
}
|
|
2357
|
+
/**
|
|
2358
|
+
* Re-adopt ONE `processing` row against the tick's session message snapshot
|
|
2359
|
+
* (ADR-0046 Decision §1/§2). Idempotent: skips a row already being driven.
|
|
2360
|
+
*
|
|
2361
|
+
* Branches on `messageRunState(messages, row.opencode_message_id)` — the
|
|
2362
|
+
* opencode-assigned user-message id persisted on the first `processing` PATCH
|
|
2363
|
+
* (#218). A row with a NULL stored id (dispatched but the read-back never landed
|
|
2364
|
+
* before the restart) has no id to correlate → treated as an orphan and
|
|
2365
|
+
* re-dispatched (at most once, see `forceReadoptRun`):
|
|
2366
|
+
* - `done` → `markDone` now (guarded like the watcher's done branch);
|
|
2367
|
+
* - `failed` → `markFailed` with the surfaced error (issue #182), so an
|
|
2368
|
+
* errored turn is reported failed on restart, NOT re-dispatched;
|
|
2369
|
+
* - `running`/`queued` → re-attach a watcher via `registerReadopted` (no re-dispatch),
|
|
2370
|
+
* tracking the stored id so the reply correlates by it;
|
|
2371
|
+
* - `unknown`/null id → re-dispatch (opencode assigns a fresh id) + attach a watcher.
|
|
2372
|
+
*
|
|
2373
|
+
* Only `ChannelAuthError` propagates.
|
|
2374
|
+
*/
|
|
2375
|
+
async readoptOne(sessionId, row, messages) {
|
|
2376
|
+
if (this.isTracked(sessionId, row.id)) {
|
|
2377
|
+
this.log({
|
|
2378
|
+
level: "info",
|
|
2379
|
+
message: `Re-adopt: message ${row.id.slice(0, 8)} already tracked in-flight \u2014 skipping (owned by the watcher loop)`,
|
|
2380
|
+
conversation_id: row.conversation_id,
|
|
2381
|
+
message_id: row.id
|
|
2382
|
+
});
|
|
2383
|
+
return;
|
|
2384
|
+
}
|
|
2385
|
+
const ocId = row.opencode_message_id;
|
|
2386
|
+
const state = messageRunState(messages, ocId ?? "");
|
|
2387
|
+
if (state === "done") {
|
|
2388
|
+
if (this.doneUndeliverable.has(row.id)) {
|
|
2389
|
+
this.log({
|
|
2390
|
+
level: "info",
|
|
2391
|
+
message: `Re-adopt: message ${row.id.slice(0, 8)} markDone is terminally undeliverable \u2014 left to the cron; skipping until it leaves processing`,
|
|
2392
|
+
conversation_id: row.conversation_id,
|
|
2393
|
+
message_id: row.id
|
|
2394
|
+
});
|
|
2395
|
+
return;
|
|
2396
|
+
}
|
|
2397
|
+
this.log({
|
|
2398
|
+
level: "info",
|
|
2399
|
+
message: `Re-adopt: message ${row.id.slice(0, 8)} completed while unwatched \u2014 marking done`,
|
|
2400
|
+
conversation_id: row.conversation_id,
|
|
2401
|
+
message_id: row.id
|
|
2402
|
+
});
|
|
2403
|
+
try {
|
|
2404
|
+
await this.markDone(row.conversation_id, row.id, sessionId, ocId);
|
|
2405
|
+
} catch (err) {
|
|
2406
|
+
if (err instanceof ChannelAuthError) throw err;
|
|
2407
|
+
if (err instanceof ChannelTerminalError) {
|
|
2408
|
+
this.doneUndeliverable.add(row.id);
|
|
2409
|
+
this.log({
|
|
2410
|
+
level: "error",
|
|
2411
|
+
message: `Re-adopt: failed to mark message ${row.id.slice(0, 8)} done (terminal HTTP ${err.status}) \u2014 parking until it leaves processing; leaving for the cron safety net: ${err.message}`,
|
|
2412
|
+
conversation_id: row.conversation_id,
|
|
2413
|
+
message_id: row.id
|
|
2414
|
+
});
|
|
2415
|
+
return;
|
|
2416
|
+
}
|
|
2417
|
+
this.log({
|
|
2418
|
+
level: "error",
|
|
2419
|
+
message: `Re-adopt: failed to mark message ${row.id.slice(0, 8)} done (will retry next drain): ${err instanceof Error ? err.message : String(err)}`,
|
|
2420
|
+
conversation_id: row.conversation_id,
|
|
2421
|
+
message_id: row.id
|
|
2422
|
+
});
|
|
2423
|
+
return;
|
|
2424
|
+
}
|
|
2425
|
+
this.dontRedispatch.delete(row.id);
|
|
2426
|
+
return;
|
|
2427
|
+
}
|
|
2428
|
+
if (state === "failed") {
|
|
2429
|
+
const error2 = messageError(messages, ocId ?? "") ?? void 0;
|
|
2430
|
+
this.log({
|
|
2431
|
+
level: "error",
|
|
2432
|
+
message: `Re-adopt: message ${row.id.slice(0, 8)} errored while unwatched \u2014 marking failed: ${error2 ?? "(no error text)"}`,
|
|
2433
|
+
conversation_id: row.conversation_id,
|
|
2434
|
+
message_id: row.id
|
|
2435
|
+
});
|
|
2436
|
+
try {
|
|
2437
|
+
await this.markFailed(row.conversation_id, row.id, sessionId, error2);
|
|
2438
|
+
} catch (err) {
|
|
2439
|
+
if (err instanceof ChannelAuthError) throw err;
|
|
2440
|
+
if (err instanceof ChannelTerminalError) {
|
|
2441
|
+
this.doneUndeliverable.add(row.id);
|
|
2442
|
+
this.log({
|
|
2443
|
+
level: "error",
|
|
2444
|
+
message: `Re-adopt: failed to mark message ${row.id.slice(0, 8)} failed (terminal HTTP ${err.status}) \u2014 parking until it leaves processing; leaving for the cron safety net: ${err.message}`,
|
|
2445
|
+
conversation_id: row.conversation_id,
|
|
2446
|
+
message_id: row.id
|
|
2447
|
+
});
|
|
2448
|
+
return;
|
|
2449
|
+
}
|
|
2450
|
+
this.log({
|
|
2451
|
+
level: "error",
|
|
2452
|
+
message: `Re-adopt: failed to mark message ${row.id.slice(0, 8)} failed (will retry next drain): ${err instanceof Error ? err.message : String(err)}`,
|
|
2453
|
+
conversation_id: row.conversation_id,
|
|
2454
|
+
message_id: row.id
|
|
2455
|
+
});
|
|
2456
|
+
return;
|
|
2457
|
+
}
|
|
2458
|
+
this.dontRedispatch.delete(row.id);
|
|
2459
|
+
return;
|
|
2460
|
+
}
|
|
2461
|
+
if (this.dontRedispatch.has(row.id)) {
|
|
2462
|
+
this.log({
|
|
2463
|
+
level: "info",
|
|
2464
|
+
message: `Re-adopt: message ${row.id.slice(0, 8)} already gave up \u2014 left to the cron; skipping until it leaves processing`,
|
|
2465
|
+
conversation_id: row.conversation_id,
|
|
2466
|
+
message_id: row.id
|
|
2467
|
+
});
|
|
2468
|
+
return;
|
|
2469
|
+
}
|
|
2470
|
+
if ((state === "running" || state === "queued") && ocId) {
|
|
2471
|
+
const conv = this.convForRow(sessionId, row);
|
|
2472
|
+
const message = this.queuedMessageForRow(row);
|
|
2473
|
+
this.registerReadopted(conv, sessionId, message, ocId, this.processedAtMs(row));
|
|
2474
|
+
this.dispatched.add(row.id);
|
|
2475
|
+
this.readopted.add(row.id);
|
|
2476
|
+
this.ensureWatcherRunning(sessionId);
|
|
2477
|
+
this.log({
|
|
2478
|
+
level: "info",
|
|
2479
|
+
message: `Re-adopt: message ${row.id.slice(0, 8)} ${state} \u2014 re-attached watcher (stored id, no re-dispatch)`,
|
|
2480
|
+
conversation_id: row.conversation_id,
|
|
2481
|
+
message_id: row.id
|
|
2482
|
+
});
|
|
2483
|
+
return;
|
|
2484
|
+
}
|
|
2485
|
+
await this.forceReadoptRun(sessionId, row);
|
|
2486
|
+
}
|
|
2487
|
+
/**
|
|
2488
|
+
* Re-dispatch an orphaned (`unknown`/null-id) `processing` row (ADR-0046 §2).
|
|
2489
|
+
*
|
|
2490
|
+
* #218/WI-5: the row's user message is absent (never kept, or a null stored id),
|
|
2491
|
+
* so we re-`prompt_async` WITHOUT a caller id (opencode assigns a monotonic one),
|
|
2492
|
+
* read it back, and register the watcher under the assigned id so the reply
|
|
2493
|
+
* correlates server-side.
|
|
2494
|
+
*
|
|
2495
|
+
* ⚠️ AT-MOST-ONCE (High-2): dispatch is no longer idempotent (no caller-supplied
|
|
2496
|
+
* id). Without a guard, if this dispatches on tick N but the read-back+persist
|
|
2497
|
+
* hasn't landed before tick N+1 re-reads the still-null `opencode_message_id`,
|
|
2498
|
+
* tick N+1 would dispatch AGAIN → duplicate user turns. The `awaitingReadopt`
|
|
2499
|
+
* latch makes a null-id row re-dispatched AT MOST ONCE per outstanding read-back:
|
|
2500
|
+
* short-circuit while the row is latched; clear it on a successful dispatch (the
|
|
2501
|
+
* row is then tracked in `dispatched`, so `readoptOne`'s early skip prevents
|
|
2502
|
+
* re-entry) OR on a failed/unresolved dispatch (genuinely un-sent → the next tick
|
|
2503
|
+
* may retry exactly once more).
|
|
2504
|
+
*
|
|
2505
|
+
* `evidentMessageId = row.id` addresses the SERVER row. Deadline anchored to
|
|
2506
|
+
* `processed_at` (Invariant 1).
|
|
2507
|
+
*/
|
|
2508
|
+
async forceReadoptRun(sessionId, row) {
|
|
2509
|
+
if (this.awaitingReadopt.has(row.id)) {
|
|
2510
|
+
this.log({
|
|
2511
|
+
level: "info",
|
|
2512
|
+
message: `Re-adopt: message ${row.id.slice(0, 8)} already has a re-dispatch awaiting read-back \u2014 skipping (at most once)`,
|
|
2513
|
+
conversation_id: row.conversation_id,
|
|
2514
|
+
message_id: row.id
|
|
2515
|
+
});
|
|
2516
|
+
return;
|
|
2517
|
+
}
|
|
2518
|
+
if (this.processedAtMs(row) + this.pausedMaxWaitMs <= this.now()) {
|
|
2519
|
+
this.dontRedispatch.add(row.id);
|
|
2520
|
+
this.log({
|
|
2521
|
+
level: "info",
|
|
2522
|
+
message: `Re-adopt: message ${row.id.slice(0, 8)} orphaned but its re-adopt window has already elapsed \u2014 not dispatching an unwatchable turn; parking until it leaves processing (cron will reset it)`,
|
|
2523
|
+
conversation_id: row.conversation_id,
|
|
2524
|
+
message_id: row.id
|
|
2525
|
+
});
|
|
2526
|
+
return;
|
|
2527
|
+
}
|
|
1954
2528
|
const options = {
|
|
1955
|
-
agent:
|
|
1956
|
-
model:
|
|
2529
|
+
agent: row.opencode_agent ?? void 0,
|
|
2530
|
+
model: row.opencode_model ?? void 0
|
|
1957
2531
|
};
|
|
1958
2532
|
this.log({
|
|
1959
2533
|
level: "info",
|
|
1960
|
-
message: `
|
|
1961
|
-
|
|
2534
|
+
message: `Re-adopt: message ${row.id.slice(0, 8)} orphaned (user message absent) \u2014 re-dispatching (opencode assigns a fresh id)`,
|
|
2535
|
+
conversation_id: row.conversation_id,
|
|
2536
|
+
message_id: row.id
|
|
1962
2537
|
});
|
|
2538
|
+
this.awaitingReadopt.add(row.id);
|
|
2539
|
+
let ocId;
|
|
1963
2540
|
try {
|
|
1964
|
-
await
|
|
1965
|
-
this.port,
|
|
2541
|
+
ocId = await this.dispatchLocked(
|
|
1966
2542
|
sessionId,
|
|
1967
|
-
|
|
1968
|
-
options,
|
|
1969
|
-
inFlight.opencodeMessageId
|
|
2543
|
+
() => sendPromptAsync(this.port, sessionId, row.content, options)
|
|
1970
2544
|
);
|
|
1971
2545
|
} catch (err) {
|
|
2546
|
+
this.awaitingReadopt.delete(row.id);
|
|
2547
|
+
if (err instanceof ChannelAuthError) throw err;
|
|
1972
2548
|
this.log({
|
|
1973
2549
|
level: "error",
|
|
1974
|
-
message: `Re-dispatch failed for message ${
|
|
1975
|
-
|
|
2550
|
+
message: `Re-adopt: re-dispatch failed for message ${row.id.slice(0, 8)} (will retry next drain): ${err instanceof Error ? err.message : String(err)}`,
|
|
2551
|
+
conversation_id: row.conversation_id,
|
|
2552
|
+
message_id: row.id
|
|
2553
|
+
});
|
|
2554
|
+
return;
|
|
2555
|
+
}
|
|
2556
|
+
if (ocId === null) {
|
|
2557
|
+
this.awaitingReadopt.delete(row.id);
|
|
2558
|
+
this.log({
|
|
2559
|
+
level: "error",
|
|
2560
|
+
message: `Re-adopt: message ${row.id.slice(0, 8)} re-dispatched but its opencode id could not be read back \u2014 leaving un-tracked to retry next drain`,
|
|
2561
|
+
conversation_id: row.conversation_id,
|
|
2562
|
+
message_id: row.id
|
|
1976
2563
|
});
|
|
2564
|
+
return;
|
|
1977
2565
|
}
|
|
1978
|
-
|
|
2566
|
+
const conv = this.convForRow(sessionId, row);
|
|
2567
|
+
const message = this.queuedMessageForRow(row);
|
|
2568
|
+
this.registerReadopted(conv, sessionId, message, ocId, this.processedAtMs(row));
|
|
2569
|
+
this.dispatched.add(row.id);
|
|
2570
|
+
this.readopted.add(row.id);
|
|
2571
|
+
this.awaitingReadopt.delete(row.id);
|
|
2572
|
+
this.ensureWatcherRunning(sessionId);
|
|
2573
|
+
}
|
|
2574
|
+
/**
|
|
2575
|
+
* True if `evidentMessageId` is already being driven — either in the
|
|
2576
|
+
* authoritative `dispatched` set or a live watcher's in-flight set for this
|
|
2577
|
+
* session (Invariant 2, WI-5). Either signal means a watcher owns the row.
|
|
2578
|
+
*/
|
|
2579
|
+
isTracked(sessionId, evidentMessageId) {
|
|
2580
|
+
if (this.dispatched.has(evidentMessageId)) return true;
|
|
2581
|
+
const watcher = this.watchers.get(sessionId);
|
|
2582
|
+
return watcher?.inFlight.has(evidentMessageId) ?? false;
|
|
2583
|
+
}
|
|
2584
|
+
/**
|
|
2585
|
+
* Parse a re-adopt row's `processed_at` (ISO string) to epoch ms for the
|
|
2586
|
+
* deadline anchor (Invariant 1). The endpoint guarantees `processed_at` is set
|
|
2587
|
+
* for `processing` rows, but if it is somehow null/unparseable fall back to
|
|
2588
|
+
* `now` (defensive) AND log — a fallback means the anchor is weaker than
|
|
2589
|
+
* intended, which is worth surfacing.
|
|
2590
|
+
*/
|
|
2591
|
+
processedAtMs(row) {
|
|
2592
|
+
const parsed = row.processed_at ? Date.parse(row.processed_at) : NaN;
|
|
2593
|
+
if (!Number.isNaN(parsed)) return parsed;
|
|
2594
|
+
this.log({
|
|
2595
|
+
level: "error",
|
|
2596
|
+
message: `Re-adopt: message ${row.id.slice(0, 8)} has null/unparseable processed_at (${String(row.processed_at)}) \u2014 anchoring deadline to now (defensive)`,
|
|
2597
|
+
conversation_id: row.conversation_id,
|
|
2598
|
+
message_id: row.id
|
|
2599
|
+
});
|
|
2600
|
+
return this.now();
|
|
2601
|
+
}
|
|
2602
|
+
/** Build the `PendingConversation` shape the watcher needs from a re-adopt row. */
|
|
2603
|
+
convForRow(sessionId, row) {
|
|
2604
|
+
return {
|
|
2605
|
+
id: row.conversation_id,
|
|
2606
|
+
agent_id: this.agentId,
|
|
2607
|
+
opencode_session_id: sessionId,
|
|
2608
|
+
pending_message_count: 0,
|
|
2609
|
+
oldest_pending_at: row.processed_at
|
|
2610
|
+
};
|
|
2611
|
+
}
|
|
2612
|
+
/** Build the `QueuedMessage` shape the watcher/re-dispatch needs from a re-adopt row. */
|
|
2613
|
+
queuedMessageForRow(row) {
|
|
2614
|
+
return {
|
|
2615
|
+
id: row.id,
|
|
2616
|
+
content: row.content,
|
|
2617
|
+
status: "processing",
|
|
2618
|
+
opencode_agent: row.opencode_agent,
|
|
2619
|
+
opencode_model: row.opencode_model,
|
|
2620
|
+
source_message_id: row.source_message_id,
|
|
2621
|
+
slack_user_id: row.slack_user_id
|
|
2622
|
+
};
|
|
1979
2623
|
}
|
|
1980
2624
|
/**
|
|
1981
2625
|
* Remove a message from the in-flight set AND the authoritative dispatched
|
|
1982
2626
|
* set. Once the in-flight set empties, the watcher loop's `while` guard exits
|
|
1983
2627
|
* and its `.finally` removes the session entry from `this.watchers`.
|
|
2628
|
+
*
|
|
2629
|
+
* Bug 2: if a RE-ADOPTED message is removed WITHOUT having completed
|
|
2630
|
+
* (`!inFlight.done` — i.e. a give-up: deadline reached, or markDone left to the
|
|
2631
|
+
* cron), park it in `dontRedispatch` so the next drain does NOT re-adopt (and
|
|
2632
|
+
* re-dispatch) the still-`processing` row every ~2s until the 15-min cron. A
|
|
2633
|
+
* re-adopted message that completed (`done`) needs no marker — it's leaving
|
|
2634
|
+
* `processing`. This suppresses only re-dispatch: if its reply later completes,
|
|
2635
|
+
* the done branch still delivers it (Bugbot #202).
|
|
1984
2636
|
*/
|
|
1985
2637
|
removeInFlight(watcher, evidentMessageId) {
|
|
2638
|
+
const inFlight = watcher.inFlight.get(evidentMessageId);
|
|
2639
|
+
if (this.readopted.delete(evidentMessageId) && inFlight && !inFlight.done) {
|
|
2640
|
+
this.dontRedispatch.add(evidentMessageId);
|
|
2641
|
+
this.log({
|
|
2642
|
+
level: "info",
|
|
2643
|
+
message: `Re-adopt: message ${evidentMessageId.slice(0, 8)} gave up \u2014 parking until it leaves the processing list (cron reset)`,
|
|
2644
|
+
conversation_id: watcher.conv.id,
|
|
2645
|
+
message_id: evidentMessageId
|
|
2646
|
+
});
|
|
2647
|
+
}
|
|
1986
2648
|
watcher.inFlight.delete(evidentMessageId);
|
|
1987
2649
|
this.dispatched.delete(evidentMessageId);
|
|
1988
2650
|
}
|
|
@@ -2011,8 +2673,8 @@ var ChannelDriver = class {
|
|
|
2011
2673
|
} catch {
|
|
2012
2674
|
}
|
|
2013
2675
|
for (const q of questions) {
|
|
2014
|
-
if (q.sessionID !== sessionId) continue;
|
|
2015
2676
|
if (watcher.reportedQuestions.has(q.id)) continue;
|
|
2677
|
+
if (!await this.sessionBelongsTo(q.sessionID, sessionId)) continue;
|
|
2016
2678
|
const paused = this.attributeInteraction(watcher, q.tool?.messageID, messages);
|
|
2017
2679
|
const reported = await this.reportInteraction(
|
|
2018
2680
|
watcher.conv.id,
|
|
@@ -2032,8 +2694,8 @@ var ChannelDriver = class {
|
|
|
2032
2694
|
} catch {
|
|
2033
2695
|
}
|
|
2034
2696
|
for (const p of permissions) {
|
|
2035
|
-
if (p.sessionID !== sessionId) continue;
|
|
2036
2697
|
if (watcher.reportedPermissions.has(p.id)) continue;
|
|
2698
|
+
if (!await this.sessionBelongsTo(p.sessionID, sessionId)) continue;
|
|
2037
2699
|
const paused = this.attributeInteraction(watcher, p.messageID, messages);
|
|
2038
2700
|
const reported = await this.reportInteraction(
|
|
2039
2701
|
watcher.conv.id,
|
|
@@ -2044,6 +2706,50 @@ var ChannelDriver = class {
|
|
|
2044
2706
|
if (reported) watcher.reportedPermissions.add(p.id);
|
|
2045
2707
|
}
|
|
2046
2708
|
}
|
|
2709
|
+
/**
|
|
2710
|
+
* True when `sessionId` is the `rootSessionId` itself OR a descendant of it —
|
|
2711
|
+
* i.e. its `parentID` chain (resolved via `GET /session/:id`) reaches the
|
|
2712
|
+
* watched root. Sub-agents spawned via the `task` tool run in child sessions,
|
|
2713
|
+
* so their questions/permissions live under a different `sessionID` that must
|
|
2714
|
+
* still be attributed to the root conversation the watcher owns.
|
|
2715
|
+
*
|
|
2716
|
+
* Parents are cached in `sessionParents` so we walk each session at most once;
|
|
2717
|
+
* a bounded depth cap guards against a cycle or a pathological chain, and any
|
|
2718
|
+
* fetch failure is treated as "not a descendant" (best-effort — the interaction
|
|
2719
|
+
* simply isn't surfaced this tick and is retried next tick once resolvable).
|
|
2720
|
+
*/
|
|
2721
|
+
async sessionBelongsTo(sessionId, rootSessionId) {
|
|
2722
|
+
let current = sessionId;
|
|
2723
|
+
for (let depth = 0; current && depth < 32; depth++) {
|
|
2724
|
+
if (current === rootSessionId) return true;
|
|
2725
|
+
const parent = await this.resolveSessionParent(current);
|
|
2726
|
+
if (parent === null || parent === void 0) return false;
|
|
2727
|
+
current = parent;
|
|
2728
|
+
}
|
|
2729
|
+
return false;
|
|
2730
|
+
}
|
|
2731
|
+
/**
|
|
2732
|
+
* Resolve (and cache) a session's `parentID` via `GET /session/:id`. Returns
|
|
2733
|
+
* `null` for a root session (no parent) and `undefined` when opencode is
|
|
2734
|
+
* unreachable / the session can't be read (so the caller stops walking without
|
|
2735
|
+
* caching a wrong answer — the next tick retries).
|
|
2736
|
+
*/
|
|
2737
|
+
async resolveSessionParent(sessionId) {
|
|
2738
|
+
const cached = this.sessionParents.get(sessionId);
|
|
2739
|
+
if (cached !== void 0) return cached;
|
|
2740
|
+
let parent = void 0;
|
|
2741
|
+
try {
|
|
2742
|
+
const res = await this.fetchImpl(`${this.opencodeBase}/session/${sessionId}`);
|
|
2743
|
+
if (res.ok) {
|
|
2744
|
+
const body = await res.json();
|
|
2745
|
+
parent = body && typeof body.parentID === "string" ? body.parentID : null;
|
|
2746
|
+
}
|
|
2747
|
+
} catch {
|
|
2748
|
+
parent = void 0;
|
|
2749
|
+
}
|
|
2750
|
+
if (parent !== void 0) this.sessionParents.set(sessionId, parent);
|
|
2751
|
+
return parent;
|
|
2752
|
+
}
|
|
2047
2753
|
/**
|
|
2048
2754
|
* Attribute a surfaced interaction to the in-flight message it paused on (M-1).
|
|
2049
2755
|
*
|
|
@@ -2126,6 +2832,35 @@ var ChannelDriver = class {
|
|
|
2126
2832
|
}
|
|
2127
2833
|
return await res.json();
|
|
2128
2834
|
}
|
|
2835
|
+
/**
|
|
2836
|
+
* Fetch this agent's `processing` messages for re-adoption (ADR-0046, WI-1).
|
|
2837
|
+
* The pending path (`getPendingConversations`/`getPendingMessages`) only
|
|
2838
|
+
* surfaces `pending` rows, so a message already `processing` when the runner
|
|
2839
|
+
* died is invisible to it — this dedicated endpoint returns exactly those rows
|
|
2840
|
+
* with the fields the re-adopt path needs (`processed_at`,
|
|
2841
|
+
* `opencode_session_id`, routing).
|
|
2842
|
+
*
|
|
2843
|
+
* Response is an OBJECT WRAPPER `{ messages: [...] }` (snake_case) — NOT a bare
|
|
2844
|
+
* array. Propagates `ChannelAuthError` on 401/403; throws a plain `Error` on
|
|
2845
|
+
* other non-ok so `drainPending`'s try/finally leaves `draining` false and the
|
|
2846
|
+
* next tick retries.
|
|
2847
|
+
*/
|
|
2848
|
+
async getProcessingMessages() {
|
|
2849
|
+
const res = await this.fetchImpl(
|
|
2850
|
+
`${this.apiUrl}/agents/${this.agentId}/conversations/processing`,
|
|
2851
|
+
{ headers: { Authorization: this.getAuthHeader() } }
|
|
2852
|
+
);
|
|
2853
|
+
this.assertAuth(res, "fetching processing messages");
|
|
2854
|
+
if (!res.ok) {
|
|
2855
|
+
throw new Error(`Failed to get processing messages: HTTP ${res.status}`);
|
|
2856
|
+
}
|
|
2857
|
+
const data = await res.json();
|
|
2858
|
+
let messages = data.messages ?? [];
|
|
2859
|
+
if (this.conversationFilter) {
|
|
2860
|
+
messages = messages.filter((m) => m.conversation_id === this.conversationFilter);
|
|
2861
|
+
}
|
|
2862
|
+
return messages;
|
|
2863
|
+
}
|
|
2129
2864
|
/**
|
|
2130
2865
|
* EXISTING combinedAuth route — now fired by the watcher on queued→running
|
|
2131
2866
|
* (Task 3.3), NOT at dispatch/claim time. `{status:'processing',
|
|
@@ -2147,13 +2882,17 @@ var ChannelDriver = class {
|
|
|
2147
2882
|
* A single attempt (no internal retry): the watcher's per-tick loop is the
|
|
2148
2883
|
* retry vehicle for the swap-to-running.
|
|
2149
2884
|
*/
|
|
2150
|
-
async markProcessing(conversationId, messageId, sessionId) {
|
|
2885
|
+
async markProcessing(conversationId, messageId, sessionId, opencodeMessageId) {
|
|
2151
2886
|
const res = await this.fetchImpl(
|
|
2152
2887
|
`${this.apiUrl}/agents/${this.agentId}/threads/${conversationId}/messages/${messageId}`,
|
|
2153
2888
|
{
|
|
2154
2889
|
method: "PATCH",
|
|
2155
2890
|
headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
|
|
2156
|
-
body: JSON.stringify({
|
|
2891
|
+
body: JSON.stringify({
|
|
2892
|
+
status: "processing",
|
|
2893
|
+
opencode_session_id: sessionId,
|
|
2894
|
+
...opencodeMessageId ? { opencode_message_id: opencodeMessageId } : {}
|
|
2895
|
+
})
|
|
2157
2896
|
}
|
|
2158
2897
|
);
|
|
2159
2898
|
this.assertAuth(res, "marking message as processing");
|
|
@@ -2191,13 +2930,17 @@ var ChannelDriver = class {
|
|
|
2191
2930
|
* watcher retries next tick within the
|
|
2192
2931
|
* deadline, Finding 4).
|
|
2193
2932
|
*/
|
|
2194
|
-
async markDone(conversationId, messageId, sessionId) {
|
|
2933
|
+
async markDone(conversationId, messageId, sessionId, opencodeMessageId) {
|
|
2195
2934
|
const res = await this.fetchImpl(
|
|
2196
2935
|
`${this.apiUrl}/agents/${this.agentId}/threads/${conversationId}/messages/${messageId}`,
|
|
2197
2936
|
{
|
|
2198
2937
|
method: "PATCH",
|
|
2199
2938
|
headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
|
|
2200
|
-
body: JSON.stringify({
|
|
2939
|
+
body: JSON.stringify({
|
|
2940
|
+
status: "done",
|
|
2941
|
+
opencode_session_id: sessionId,
|
|
2942
|
+
...opencodeMessageId ? { opencode_message_id: opencodeMessageId } : {}
|
|
2943
|
+
})
|
|
2201
2944
|
}
|
|
2202
2945
|
);
|
|
2203
2946
|
this.assertAuth(res, "marking message as done");
|
|
@@ -2207,7 +2950,17 @@ var ChannelDriver = class {
|
|
|
2207
2950
|
}
|
|
2208
2951
|
throw new ChannelTerminalError(`marking message as done: HTTP ${res.status}`, res.status);
|
|
2209
2952
|
}
|
|
2210
|
-
|
|
2953
|
+
/**
|
|
2954
|
+
* Mark a message `failed`. `sessionId` / `error` are threaded to the API ONLY
|
|
2955
|
+
* when provided (issue #182): a bare `markFailed(conv, msg)` sends
|
|
2956
|
+
* `{status:'failed'}` unchanged (the dispatch-failure path), while an errored
|
|
2957
|
+
* OpenCode turn sends `{status:'failed', opencode_session_id, error}` so the
|
|
2958
|
+
* failure reason reaches the channel.
|
|
2959
|
+
*/
|
|
2960
|
+
async markFailed(conversationId, messageId, sessionId, error2) {
|
|
2961
|
+
const body = { status: "failed" };
|
|
2962
|
+
if (sessionId !== void 0) body.opencode_session_id = sessionId;
|
|
2963
|
+
if (error2 !== void 0) body.error = error2;
|
|
2211
2964
|
await this.callWithRetry(
|
|
2212
2965
|
"marking message as failed",
|
|
2213
2966
|
() => this.fetchImpl(
|
|
@@ -2215,11 +2968,47 @@ var ChannelDriver = class {
|
|
|
2215
2968
|
{
|
|
2216
2969
|
method: "PATCH",
|
|
2217
2970
|
headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
|
|
2218
|
-
body: JSON.stringify(
|
|
2971
|
+
body: JSON.stringify(body)
|
|
2219
2972
|
}
|
|
2220
2973
|
)
|
|
2221
2974
|
);
|
|
2222
2975
|
}
|
|
2976
|
+
/**
|
|
2977
|
+
* Best-effort, SINGLE-ATTEMPT runner→server telemetry ping
|
|
2978
|
+
* (queued-followup-redrive). `POST .../messages/:id/signal {signal, ...extra}`
|
|
2979
|
+
* — the server records it via `log()` (no DB write, no notification). This is
|
|
2980
|
+
* fire-and-forget: it MUST NEVER throw into the drain or the watcher tick, and
|
|
2981
|
+
* MUST NOT use `callWithRetry` (a telemetry ping must not block the sequential
|
|
2982
|
+
* watcher tick — one attempt is enough). A failure is SWALLOWED but LOGGED with
|
|
2983
|
+
* context (no silent catch, per development-workflow).
|
|
2984
|
+
*/
|
|
2985
|
+
async postSignal(conversationId, messageId, signal, extra) {
|
|
2986
|
+
try {
|
|
2987
|
+
const res = await this.fetchImpl(
|
|
2988
|
+
`${this.apiUrl}/agents/${this.agentId}/threads/${conversationId}/messages/${messageId}/signal`,
|
|
2989
|
+
{
|
|
2990
|
+
method: "POST",
|
|
2991
|
+
headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
|
|
2992
|
+
body: JSON.stringify({ signal, ...extra })
|
|
2993
|
+
}
|
|
2994
|
+
);
|
|
2995
|
+
if (!res.ok) {
|
|
2996
|
+
this.log({
|
|
2997
|
+
level: "error",
|
|
2998
|
+
message: `Signal '${signal}' for message ${messageId.slice(0, 8)} returned HTTP ${res.status} (telemetry-only, ignored)`,
|
|
2999
|
+
conversation_id: conversationId,
|
|
3000
|
+
message_id: messageId
|
|
3001
|
+
});
|
|
3002
|
+
}
|
|
3003
|
+
} catch (err) {
|
|
3004
|
+
this.log({
|
|
3005
|
+
level: "error",
|
|
3006
|
+
message: `Signal '${signal}' for message ${messageId.slice(0, 8)} failed (telemetry-only, ignored): ${err instanceof Error ? err.message : String(err)}`,
|
|
3007
|
+
conversation_id: conversationId,
|
|
3008
|
+
message_id: messageId
|
|
3009
|
+
});
|
|
3010
|
+
}
|
|
3011
|
+
}
|
|
2223
3012
|
async persistSession(conversationId, sessionId) {
|
|
2224
3013
|
const res = await this.fetchImpl(
|
|
2225
3014
|
`${this.apiUrl}/agents/${this.agentId}/threads/${conversationId}`,
|
|
@@ -2535,7 +3324,8 @@ async function getAgentInfo(agentId, authHeader) {
|
|
|
2535
3324
|
// src/commands/run.ts
|
|
2536
3325
|
var MAX_ACTIVITY_LOG_ENTRIES = 10;
|
|
2537
3326
|
var CHANNEL_POLL_INTERVAL_MS = Number(process.env.EVIDENT_CHANNEL_POLL_INTERVAL_MS) || 2e3;
|
|
2538
|
-
|
|
3327
|
+
var CHANNEL_STUCK_QUEUED_MS = Number(process.env.EVIDENT_STUCK_QUEUED_MS) || void 0;
|
|
3328
|
+
function log2(state, message, isError = false) {
|
|
2539
3329
|
if (state.json) {
|
|
2540
3330
|
console.log(
|
|
2541
3331
|
JSON.stringify({
|
|
@@ -2560,9 +3350,9 @@ function logActivity(state, entry) {
|
|
|
2560
3350
|
}
|
|
2561
3351
|
if (!state.interactive) {
|
|
2562
3352
|
if (entry.type === "error") {
|
|
2563
|
-
|
|
3353
|
+
log2(state, entry.error ?? "Unknown error", true);
|
|
2564
3354
|
} else if (entry.type === "info" && entry.message) {
|
|
2565
|
-
|
|
3355
|
+
log2(state, entry.message);
|
|
2566
3356
|
}
|
|
2567
3357
|
}
|
|
2568
3358
|
}
|
|
@@ -2648,6 +3438,7 @@ async function handleAuthError(state, error2) {
|
|
|
2648
3438
|
}
|
|
2649
3439
|
async function driveChannels(state, driver) {
|
|
2650
3440
|
let idlePolls = 0;
|
|
3441
|
+
let lastSeenProxiedActivityAt = state.lastProxiedActivityAt;
|
|
2651
3442
|
while (state.running) {
|
|
2652
3443
|
if (state.connection?.reconnecting && state.connection.reconnectPromise) {
|
|
2653
3444
|
logActivity(state, { type: "info", message: "Waiting for tunnel reconnection..." });
|
|
@@ -2657,7 +3448,9 @@ async function driveChannels(state, driver) {
|
|
|
2657
3448
|
try {
|
|
2658
3449
|
const processed = await driver.drainPending();
|
|
2659
3450
|
state.messageCount += processed;
|
|
2660
|
-
|
|
3451
|
+
const proxiedActivity = state.lastProxiedActivityAt !== lastSeenProxiedActivityAt;
|
|
3452
|
+
lastSeenProxiedActivityAt = state.lastProxiedActivityAt;
|
|
3453
|
+
if (processed > 0 || driver.hasInFlightWatchers() || proxiedActivity) {
|
|
2661
3454
|
idlePolls = 0;
|
|
2662
3455
|
if (processed > 0 && state.interactive) displayStatus(state);
|
|
2663
3456
|
} else if (state.idleTimeout !== null) {
|
|
@@ -2709,7 +3502,7 @@ async function cleanup(state) {
|
|
|
2709
3502
|
logActivity(state, { type: "info", message: "Stopped OpenCode process" });
|
|
2710
3503
|
displayStatus(state);
|
|
2711
3504
|
} else {
|
|
2712
|
-
|
|
3505
|
+
log2(state, "Stopped OpenCode process");
|
|
2713
3506
|
}
|
|
2714
3507
|
state.opencodeProcess = null;
|
|
2715
3508
|
}
|
|
@@ -2732,10 +3525,11 @@ async function run(options) {
|
|
|
2732
3525
|
running: true,
|
|
2733
3526
|
activityLog: [],
|
|
2734
3527
|
messageCount: 0,
|
|
3528
|
+
lastProxiedActivityAt: null,
|
|
2735
3529
|
authHeader: ""
|
|
2736
3530
|
};
|
|
2737
3531
|
if (state.idleTimeout === null && (process.env.GITHUB_ACTIONS || process.env.CI)) {
|
|
2738
|
-
|
|
3532
|
+
log2(
|
|
2739
3533
|
state,
|
|
2740
3534
|
"Warning: No --idle-timeout set in CI environment. The runner will poll indefinitely until the job times out. Consider adding --idle-timeout 30 to avoid wasting runner minutes.",
|
|
2741
3535
|
false
|
|
@@ -2746,7 +3540,7 @@ async function run(options) {
|
|
|
2746
3540
|
logActivity(state, { type: "info", message: "Shutting down..." });
|
|
2747
3541
|
displayStatus(state);
|
|
2748
3542
|
} else {
|
|
2749
|
-
|
|
3543
|
+
log2(state, "Shutting down...");
|
|
2750
3544
|
}
|
|
2751
3545
|
await cleanup(state);
|
|
2752
3546
|
await shutdownTelemetry();
|
|
@@ -2779,7 +3573,7 @@ async function run(options) {
|
|
|
2779
3573
|
const resolved = await resolveAgentIdFromKey(state.authHeader);
|
|
2780
3574
|
if (resolved.agent_id) {
|
|
2781
3575
|
state.agentId = resolved.agent_id;
|
|
2782
|
-
|
|
3576
|
+
log2(state, `Resolved agent ID from key: ${state.agentId}`);
|
|
2783
3577
|
if (state.interactive && !state.json) {
|
|
2784
3578
|
logActivity(state, {
|
|
2785
3579
|
type: "info",
|
|
@@ -2842,17 +3636,17 @@ async function run(options) {
|
|
|
2842
3636
|
port: state.port,
|
|
2843
3637
|
interactive: state.interactive,
|
|
2844
3638
|
agentId: state.agentId,
|
|
2845
|
-
log: (message) =>
|
|
3639
|
+
log: (message) => log2(state, message)
|
|
2846
3640
|
});
|
|
2847
3641
|
state.port = oc.port;
|
|
2848
3642
|
state.opencodeProcess = oc.process;
|
|
2849
3643
|
state.opencodeVersion = oc.version;
|
|
2850
3644
|
state.opencodeConnected = oc.process !== null || oc.version !== null;
|
|
2851
|
-
const
|
|
2852
|
-
ocSpinner?.succeed(`OpenCode running on port ${state.port}${
|
|
3645
|
+
const version2 = state.opencodeVersion ? ` (v${state.opencodeVersion})` : "";
|
|
3646
|
+
ocSpinner?.succeed(`OpenCode running on port ${state.port}${version2}`);
|
|
2853
3647
|
const versionWarning = buildOpenCodeVersionWarning(state.opencodeVersion);
|
|
2854
3648
|
if (versionWarning) {
|
|
2855
|
-
|
|
3649
|
+
log2(state, versionWarning, false);
|
|
2856
3650
|
if (state.interactive && !state.json) {
|
|
2857
3651
|
logActivity(state, { type: "info", message: versionWarning });
|
|
2858
3652
|
}
|
|
@@ -2868,6 +3662,7 @@ async function run(options) {
|
|
|
2868
3662
|
apiUrl: getApiUrlConfig(),
|
|
2869
3663
|
getAuthHeader: () => state.authHeader,
|
|
2870
3664
|
conversationFilter: state.conversationFilter,
|
|
3665
|
+
stuckQueuedMs: CHANNEL_STUCK_QUEUED_MS,
|
|
2871
3666
|
log: (entry) => logActivity(state, {
|
|
2872
3667
|
type: entry.level === "error" ? "error" : "info",
|
|
2873
3668
|
message: entry.message,
|
|
@@ -2887,7 +3682,11 @@ async function run(options) {
|
|
|
2887
3682
|
type: "info",
|
|
2888
3683
|
message: `Tunnel ${isReconnect ? "reconnected" : "connected"} (agent: ${agentId})`
|
|
2889
3684
|
});
|
|
2890
|
-
emitAgentConnected(state.agentId, {
|
|
3685
|
+
emitAgentConnected(state.agentId, {
|
|
3686
|
+
port: state.port,
|
|
3687
|
+
cli_version: getCliVersion(),
|
|
3688
|
+
opencode_version: state.opencodeVersion
|
|
3689
|
+
});
|
|
2891
3690
|
if (!isReconnect) tunnelSpinner?.succeed("Tunnel connected");
|
|
2892
3691
|
if (state.interactive) displayStatus(state);
|
|
2893
3692
|
channelDriver.drainPending().then((processed) => {
|
|
@@ -2921,9 +3720,14 @@ async function run(options) {
|
|
|
2921
3720
|
logActivity(state, { type: "error", error: error2 });
|
|
2922
3721
|
if (state.interactive) displayStatus(state);
|
|
2923
3722
|
},
|
|
2924
|
-
// Web traffic is proxied transparently;
|
|
3723
|
+
// Web traffic is proxied transparently; note opencode is live and stamp
|
|
3724
|
+
// proxied activity so the idle loop treats interactive proxy use as work.
|
|
3725
|
+
// Fires per forwarded response head (incl. every SSE open) and excludes
|
|
3726
|
+
// the internal drain-ping, so an actively-used proxy keeps the timer
|
|
3727
|
+
// fresh while a lone idle SSE with no follow-up requests still ages out.
|
|
2925
3728
|
onResponse: () => {
|
|
2926
3729
|
state.opencodeConnected = true;
|
|
3730
|
+
state.lastProxiedActivityAt = Date.now();
|
|
2927
3731
|
},
|
|
2928
3732
|
// A channel message was queued and the api-worker pinged us over the
|
|
2929
3733
|
// tunnel to drain immediately instead of waiting for the next poll tick.
|
|
@@ -2962,7 +3766,7 @@ async function run(options) {
|
|
|
2962
3766
|
throw error2;
|
|
2963
3767
|
}
|
|
2964
3768
|
if (!interactive || state.json) {
|
|
2965
|
-
|
|
3769
|
+
log2(state, "Driving channel messages...");
|
|
2966
3770
|
}
|
|
2967
3771
|
await driveChannels(state, channelDriver);
|
|
2968
3772
|
await cleanup(state);
|
|
@@ -2974,7 +3778,7 @@ async function run(options) {
|
|
|
2974
3778
|
})
|
|
2975
3779
|
);
|
|
2976
3780
|
} else if (!interactive) {
|
|
2977
|
-
|
|
3781
|
+
log2(state, `Completed. Processed ${state.messageCount} message(s).`);
|
|
2978
3782
|
}
|
|
2979
3783
|
await shutdownTelemetry();
|
|
2980
3784
|
process.exit(0);
|
|
@@ -2996,8 +3800,9 @@ async function run(options) {
|
|
|
2996
3800
|
}
|
|
2997
3801
|
|
|
2998
3802
|
// src/index.ts
|
|
3803
|
+
var { version } = createRequire(import.meta.url)("../package.json");
|
|
2999
3804
|
var program = new Command();
|
|
3000
|
-
program.name("evident").description("Run OpenCode locally and connect it to Evident").version(
|
|
3805
|
+
program.name("evident").description("Run OpenCode locally and connect it to Evident").version(version).option(
|
|
3001
3806
|
"--endpoint <url>",
|
|
3002
3807
|
"Evident API base URL (default: production; e.g. http://localhost:3001)"
|
|
3003
3808
|
).option("--tunnel <url>", "Tunnel WebSocket URL (default: production; e.g. ws://localhost:8787)").hook("preAction", (thisCommand) => {
|