@evident-ai/cli 3.0.1-dev.b14e3dc → 3.0.1-dev.b58bd29
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 +973 -71
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -471,12 +471,6 @@ import chalk6 from "chalk";
|
|
|
471
471
|
import ora3 from "ora";
|
|
472
472
|
import { select as select3 } from "@inquirer/prompts";
|
|
473
473
|
|
|
474
|
-
// ../../packages/types/src/opencode/index.ts
|
|
475
|
-
function opencodeMessageIdFor(queuedMessageId) {
|
|
476
|
-
const sanitized = queuedMessageId.replace(/[^a-zA-Z0-9]/g, "_");
|
|
477
|
-
return `msg_${sanitized}`;
|
|
478
|
-
}
|
|
479
|
-
|
|
480
474
|
// ../../packages/types/src/telemetry/index.ts
|
|
481
475
|
var TelemetryEventTypes = {
|
|
482
476
|
// Agent activity events (shown in web UI activity log)
|
|
@@ -515,7 +509,10 @@ function stripQuery(url) {
|
|
|
515
509
|
}
|
|
516
510
|
|
|
517
511
|
// src/lib/telemetry.ts
|
|
518
|
-
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
|
+
}
|
|
519
516
|
var eventBuffer = [];
|
|
520
517
|
var flushTimeout = null;
|
|
521
518
|
var isShuttingDown = false;
|
|
@@ -715,7 +712,7 @@ async function waitForOpenCodeHealth(port, timeoutMs = 3e4) {
|
|
|
715
712
|
}
|
|
716
713
|
|
|
717
714
|
// src/lib/opencode/opencode-version-gate.ts
|
|
718
|
-
var QUEUE_VALIDATED_OPENCODE_VERSIONS = ["1.17.11"];
|
|
715
|
+
var QUEUE_VALIDATED_OPENCODE_VERSIONS = ["1.17.11", "1.18.3"];
|
|
719
716
|
function isQueueValidatedVersion(version2) {
|
|
720
717
|
if (!version2) return false;
|
|
721
718
|
return QUEUE_VALIDATED_OPENCODE_VERSIONS.includes(version2);
|
|
@@ -1039,7 +1036,11 @@ function roleOf(m) {
|
|
|
1039
1036
|
}
|
|
1040
1037
|
function completedOf(m) {
|
|
1041
1038
|
if (!m || typeof m !== "object") return void 0;
|
|
1042
|
-
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;
|
|
1043
1044
|
}
|
|
1044
1045
|
function idOf(m) {
|
|
1045
1046
|
if (!m || typeof m !== "object") return void 0;
|
|
@@ -1059,6 +1060,24 @@ function finishOf(m) {
|
|
|
1059
1060
|
const infoFinish = m.info?.finish;
|
|
1060
1061
|
return typeof infoFinish === "string" ? infoFinish : void 0;
|
|
1061
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
|
+
}
|
|
1062
1081
|
async function createOpenCodeSession(port, directory) {
|
|
1063
1082
|
const url = new URL(`${opencodeBase(port)}/session`);
|
|
1064
1083
|
if (directory && directory.trim()) {
|
|
@@ -1076,9 +1095,16 @@ async function createOpenCodeSession(port, directory) {
|
|
|
1076
1095
|
const data = await response.json();
|
|
1077
1096
|
return data.id;
|
|
1078
1097
|
}
|
|
1079
|
-
|
|
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
|
+
);
|
|
1080
1107
|
const body = {
|
|
1081
|
-
messageID: messageId,
|
|
1082
1108
|
parts: [{ type: "text", text: content }]
|
|
1083
1109
|
};
|
|
1084
1110
|
if (options?.agent) {
|
|
@@ -1102,6 +1128,29 @@ async function sendPromptAsync(port, sessionId, content, options, messageId) {
|
|
|
1102
1128
|
const text = await res.text().catch(() => "");
|
|
1103
1129
|
throw new Error(`OpenCode prompt_async failed: HTTP ${res.status}${text ? `: ${text}` : ""}`);
|
|
1104
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;
|
|
1105
1154
|
}
|
|
1106
1155
|
function findAssistantReplyAfter(messages, userMessageId) {
|
|
1107
1156
|
if (!messages || messages.length === 0) return null;
|
|
@@ -1118,19 +1167,31 @@ function findAssistantReplyAfter(messages, userMessageId) {
|
|
|
1118
1167
|
}
|
|
1119
1168
|
function findLastAssistantReplyFor(messages, userMessageId) {
|
|
1120
1169
|
if (!messages || messages.length === 0) return null;
|
|
1170
|
+
let lastCorrelated = null;
|
|
1171
|
+
let lastNonErrored = null;
|
|
1121
1172
|
for (let i = messages.length - 1; i >= 0; i--) {
|
|
1122
1173
|
const m = messages[i];
|
|
1123
|
-
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
|
+
}
|
|
1124
1180
|
}
|
|
1181
|
+
if (lastCorrelated) return lastNonErrored ?? lastCorrelated;
|
|
1125
1182
|
const userIndex = messages.findIndex((m) => idOf(m) === userMessageId);
|
|
1126
1183
|
if (userIndex === -1) return null;
|
|
1127
1184
|
let last = null;
|
|
1185
|
+
let lastOk = null;
|
|
1128
1186
|
for (let i = userIndex + 1; i < messages.length; i++) {
|
|
1129
1187
|
const role = roleOf(messages[i]);
|
|
1130
1188
|
if (role === "user") break;
|
|
1131
|
-
if (role === "assistant")
|
|
1189
|
+
if (role === "assistant") {
|
|
1190
|
+
last = messages[i];
|
|
1191
|
+
if (errorOf(messages[i]) == null) lastOk = messages[i];
|
|
1192
|
+
}
|
|
1132
1193
|
}
|
|
1133
|
-
return last;
|
|
1194
|
+
return lastOk ?? last;
|
|
1134
1195
|
}
|
|
1135
1196
|
function messageRunState(messages, userMessageId) {
|
|
1136
1197
|
if (!messages || messages.length === 0) return "unknown";
|
|
@@ -1140,12 +1201,27 @@ function messageRunState(messages, userMessageId) {
|
|
|
1140
1201
|
if (!reply) return "unknown";
|
|
1141
1202
|
}
|
|
1142
1203
|
if (!reply) return "queued";
|
|
1143
|
-
if (
|
|
1144
|
-
|
|
1145
|
-
return "done";
|
|
1204
|
+
if (isAssistantInFlight(reply)) return "running";
|
|
1205
|
+
return errorOf(reply) != null ? "failed" : "done";
|
|
1146
1206
|
}
|
|
1147
|
-
function
|
|
1148
|
-
|
|
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
|
+
);
|
|
1149
1225
|
}
|
|
1150
1226
|
|
|
1151
1227
|
// src/lib/tunnel/connection.ts
|
|
@@ -1224,12 +1300,14 @@ var StreamForwarder = class {
|
|
|
1224
1300
|
this.send({ type: "res_end", sid });
|
|
1225
1301
|
return;
|
|
1226
1302
|
}
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1303
|
+
if (process.env.DEBUG) {
|
|
1304
|
+
log("debug", "agent_request", {
|
|
1305
|
+
correlation_id: correlationId,
|
|
1306
|
+
sid,
|
|
1307
|
+
method,
|
|
1308
|
+
path: stripQuery(path)
|
|
1309
|
+
});
|
|
1310
|
+
}
|
|
1233
1311
|
const ac = new AbortController();
|
|
1234
1312
|
let bodyPromise;
|
|
1235
1313
|
let pushBody;
|
|
@@ -1276,12 +1354,14 @@ var StreamForwarder = class {
|
|
|
1276
1354
|
if (!STRIP_RES.has(key.toLowerCase())) resHeaders[key] = value;
|
|
1277
1355
|
});
|
|
1278
1356
|
this.send({ type: "head", sid, status: upstream.status, headers: resHeaders });
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
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
|
+
}
|
|
1285
1365
|
this.callbacks.onHead?.(sid, upstream.status);
|
|
1286
1366
|
try {
|
|
1287
1367
|
if (upstream.body) {
|
|
@@ -1551,7 +1631,7 @@ var DEFAULT_RETRY_POLICY = {
|
|
|
1551
1631
|
};
|
|
1552
1632
|
var DEFAULT_PAUSED_POLL_INTERVAL_MS = 2e3;
|
|
1553
1633
|
var DEFAULT_PAUSED_MAX_WAIT_MS = 10 * 60 * 1e3;
|
|
1554
|
-
var
|
|
1634
|
+
var DEFAULT_STUCK_QUEUED_MS = 6e4;
|
|
1555
1635
|
var ChannelAuthError = class extends Error {
|
|
1556
1636
|
constructor(message) {
|
|
1557
1637
|
super(message);
|
|
@@ -1586,10 +1666,18 @@ var ChannelDriver = class {
|
|
|
1586
1666
|
sleep;
|
|
1587
1667
|
pausedPollIntervalMs;
|
|
1588
1668
|
pausedMaxWaitMs;
|
|
1589
|
-
|
|
1669
|
+
stuckQueuedMs;
|
|
1590
1670
|
now;
|
|
1591
1671
|
/** Cache of conversationId → opencode sessionId. */
|
|
1592
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();
|
|
1593
1681
|
/**
|
|
1594
1682
|
* Per-SESSION watchers (WI-3), keyed by opencode sessionId. Single-flight per
|
|
1595
1683
|
* session: one polling loop services all of that session's in-flight messages.
|
|
@@ -1606,6 +1694,54 @@ var ChannelDriver = class {
|
|
|
1606
1694
|
* a steady-state-poll re-dispatch will not double-run the message.
|
|
1607
1695
|
*/
|
|
1608
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();
|
|
1609
1745
|
/**
|
|
1610
1746
|
* Cache of the opencode root directory (from `GET /path`). Resolved lazily on
|
|
1611
1747
|
* first session creation so drain-created sessions are rooted at the project
|
|
@@ -1613,8 +1749,33 @@ var ChannelDriver = class {
|
|
|
1613
1749
|
* not yet resolved; `null` = resolved-but-unavailable (don't keep retrying).
|
|
1614
1750
|
*/
|
|
1615
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();
|
|
1616
1762
|
/** Serialises drains so a reconnect during a drain doesn't double-process. */
|
|
1617
1763
|
draining = false;
|
|
1764
|
+
/**
|
|
1765
|
+
* The currently-executing `drainPending()` promise, or null when idle. Lets a
|
|
1766
|
+
* graceful shutdown (`waitForInFlight`) await an in-progress drain so a turn it
|
|
1767
|
+
* is about to dispatch is not missed by the `hasInFlightWatchers()` check (a
|
|
1768
|
+
* drain that entered before `stop()` still registers its watcher).
|
|
1769
|
+
*/
|
|
1770
|
+
activeDrain = null;
|
|
1771
|
+
/**
|
|
1772
|
+
* Set by `stop()` on graceful shutdown. Once stopped, `drainPending` no longer
|
|
1773
|
+
* dispatches NEW work (it returns 0 immediately) — but the per-session watcher
|
|
1774
|
+
* loops already running keep going so in-flight turns can finish and deliver
|
|
1775
|
+
* their reply. `run.ts` awaits `waitForInFlight()` before it closes the tunnel
|
|
1776
|
+
* and stops opencode.
|
|
1777
|
+
*/
|
|
1778
|
+
stopped = false;
|
|
1618
1779
|
constructor(config2) {
|
|
1619
1780
|
this.agentId = config2.agentId;
|
|
1620
1781
|
this.port = config2.port;
|
|
@@ -1628,7 +1789,7 @@ var ChannelDriver = class {
|
|
|
1628
1789
|
this.sleep = config2.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
|
|
1629
1790
|
this.pausedPollIntervalMs = config2.pausedPollIntervalMs ?? DEFAULT_PAUSED_POLL_INTERVAL_MS;
|
|
1630
1791
|
this.pausedMaxWaitMs = config2.pausedMaxWaitMs ?? DEFAULT_PAUSED_MAX_WAIT_MS;
|
|
1631
|
-
this.
|
|
1792
|
+
this.stuckQueuedMs = config2.stuckQueuedMs ?? DEFAULT_STUCK_QUEUED_MS;
|
|
1632
1793
|
this.now = config2.now ?? (() => Date.now());
|
|
1633
1794
|
}
|
|
1634
1795
|
/** The IPv4-loopback base URL for the local `opencode serve`. */
|
|
@@ -1646,8 +1807,21 @@ var ChannelDriver = class {
|
|
|
1646
1807
|
* @returns the number of messages NEWLY dispatched to opencode's native queue.
|
|
1647
1808
|
*/
|
|
1648
1809
|
async drainPending() {
|
|
1810
|
+
if (this.stopped) return 0;
|
|
1649
1811
|
if (this.draining) return 0;
|
|
1650
1812
|
this.draining = true;
|
|
1813
|
+
const run2 = this.runDrain();
|
|
1814
|
+
this.activeDrain = run2.then(
|
|
1815
|
+
() => {
|
|
1816
|
+
this.activeDrain = null;
|
|
1817
|
+
},
|
|
1818
|
+
() => {
|
|
1819
|
+
this.activeDrain = null;
|
|
1820
|
+
}
|
|
1821
|
+
);
|
|
1822
|
+
return run2;
|
|
1823
|
+
}
|
|
1824
|
+
async runDrain() {
|
|
1651
1825
|
let dispatched = 0;
|
|
1652
1826
|
try {
|
|
1653
1827
|
const conversations = await this.getPendingConversations();
|
|
@@ -1659,8 +1833,10 @@ var ChannelDriver = class {
|
|
|
1659
1833
|
});
|
|
1660
1834
|
}
|
|
1661
1835
|
for (const conv of conversations) {
|
|
1836
|
+
if (this.stopped) break;
|
|
1662
1837
|
dispatched += await this.processConversation(conv);
|
|
1663
1838
|
}
|
|
1839
|
+
await this.readoptProcessing();
|
|
1664
1840
|
} finally {
|
|
1665
1841
|
this.draining = false;
|
|
1666
1842
|
}
|
|
@@ -1678,6 +1854,51 @@ var ChannelDriver = class {
|
|
|
1678
1854
|
}
|
|
1679
1855
|
return false;
|
|
1680
1856
|
}
|
|
1857
|
+
/**
|
|
1858
|
+
* Begin a graceful stop: stop accepting NEW channel work. Idempotent. After
|
|
1859
|
+
* this, `drainPending()` is a no-op (returns 0), so no new message is dispatched
|
|
1860
|
+
* — but the watcher loops already tracking in-flight turns keep running, so a
|
|
1861
|
+
* turn that has finished (or is about to) still fires `markDone` and delivers
|
|
1862
|
+
* its reply. Pair with `waitForInFlight()` to bound how long shutdown waits.
|
|
1863
|
+
*/
|
|
1864
|
+
stop() {
|
|
1865
|
+
this.stopped = true;
|
|
1866
|
+
}
|
|
1867
|
+
/**
|
|
1868
|
+
* Wait (up to `timeoutMs`) for in-flight watcher work to settle during a
|
|
1869
|
+
* graceful shutdown, so a turn whose reply is ready — or completes within the
|
|
1870
|
+
* window — is delivered before the process exits, instead of being cut off and
|
|
1871
|
+
* left for the ADR-0046 restart-recovery path.
|
|
1872
|
+
*
|
|
1873
|
+
* Bounded on purpose: the watcher's own give-up deadline is up to 10 minutes,
|
|
1874
|
+
* far longer than a shutdown grace period (e.g. Fargate's SIGTERM→SIGKILL
|
|
1875
|
+
* window). We poll `hasInFlightWatchers()` and return as soon as the in-flight
|
|
1876
|
+
* set empties OR the timeout elapses. Anything still in flight at the timeout is
|
|
1877
|
+
* safe to abandon — it stays `processing` server-side and is re-adopted on the
|
|
1878
|
+
* next runner start (ADR-0046).
|
|
1879
|
+
*
|
|
1880
|
+
* @returns true if all in-flight work settled within the window; false if the
|
|
1881
|
+
* timeout elapsed with work still in flight.
|
|
1882
|
+
*/
|
|
1883
|
+
async waitForInFlight(timeoutMs) {
|
|
1884
|
+
const deadline = this.now() + timeoutMs;
|
|
1885
|
+
const step = Math.min(this.pausedPollIntervalMs, 250);
|
|
1886
|
+
if (this.activeDrain) {
|
|
1887
|
+
let drainSettled = false;
|
|
1888
|
+
void this.activeDrain.then(() => {
|
|
1889
|
+
drainSettled = true;
|
|
1890
|
+
});
|
|
1891
|
+
while (!drainSettled) {
|
|
1892
|
+
if (this.now() >= deadline) return false;
|
|
1893
|
+
await this.sleep(step);
|
|
1894
|
+
}
|
|
1895
|
+
}
|
|
1896
|
+
while (this.hasInFlightWatchers()) {
|
|
1897
|
+
if (this.now() >= deadline) return false;
|
|
1898
|
+
await this.sleep(step);
|
|
1899
|
+
}
|
|
1900
|
+
return true;
|
|
1901
|
+
}
|
|
1681
1902
|
/**
|
|
1682
1903
|
* Await all outstanding per-session watchers (WI-3).
|
|
1683
1904
|
*
|
|
@@ -1714,15 +1935,16 @@ var ChannelDriver = class {
|
|
|
1714
1935
|
let dispatched = 0;
|
|
1715
1936
|
let skippedAlreadyDispatched = 0;
|
|
1716
1937
|
for (const message of messages) {
|
|
1938
|
+
if (this.stopped) break;
|
|
1717
1939
|
if (this.dispatched.has(message.id)) {
|
|
1718
1940
|
skippedAlreadyDispatched += 1;
|
|
1719
1941
|
continue;
|
|
1720
1942
|
}
|
|
1721
|
-
const opencodeMessageId = opencodeMessageIdFor2(message.id);
|
|
1722
1943
|
const options = {
|
|
1723
1944
|
agent: message.opencode_agent ?? void 0,
|
|
1724
1945
|
model: message.opencode_model ?? void 0
|
|
1725
1946
|
};
|
|
1947
|
+
let opencodeMessageId;
|
|
1726
1948
|
try {
|
|
1727
1949
|
this.log({
|
|
1728
1950
|
level: "info",
|
|
@@ -1730,7 +1952,10 @@ var ChannelDriver = class {
|
|
|
1730
1952
|
conversation_id: conv.id,
|
|
1731
1953
|
message_id: message.id
|
|
1732
1954
|
});
|
|
1733
|
-
await
|
|
1955
|
+
opencodeMessageId = await this.dispatchLocked(
|
|
1956
|
+
sessionId,
|
|
1957
|
+
() => sendPromptAsync(this.port, sessionId, message.content, options)
|
|
1958
|
+
);
|
|
1734
1959
|
} catch (err) {
|
|
1735
1960
|
if (err instanceof ChannelAuthError) throw err;
|
|
1736
1961
|
this.dispatched.delete(message.id);
|
|
@@ -1744,9 +1969,19 @@ var ChannelDriver = class {
|
|
|
1744
1969
|
});
|
|
1745
1970
|
continue;
|
|
1746
1971
|
}
|
|
1972
|
+
if (opencodeMessageId === null) {
|
|
1973
|
+
this.log({
|
|
1974
|
+
level: "error",
|
|
1975
|
+
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`,
|
|
1976
|
+
conversation_id: conv.id,
|
|
1977
|
+
message_id: message.id
|
|
1978
|
+
});
|
|
1979
|
+
continue;
|
|
1980
|
+
}
|
|
1747
1981
|
this.dispatched.add(message.id);
|
|
1748
1982
|
this.registerInFlight(conv, sessionId, message, opencodeMessageId);
|
|
1749
1983
|
dispatched += 1;
|
|
1984
|
+
void this.postSignal(conv.id, message.id, "dispatched");
|
|
1750
1985
|
}
|
|
1751
1986
|
if (messages.length > 0 && dispatched === 0 && skippedAlreadyDispatched === messages.length) {
|
|
1752
1987
|
this.log({
|
|
@@ -1791,6 +2026,25 @@ var ChannelDriver = class {
|
|
|
1791
2026
|
// -------------------------------------------------------------------------
|
|
1792
2027
|
// Per-session watcher (WI-3)
|
|
1793
2028
|
// -------------------------------------------------------------------------
|
|
2029
|
+
/**
|
|
2030
|
+
* Run one dispatch (`sendPromptAsync` snapshot→POST→read-back) serialized per
|
|
2031
|
+
* opencode session (Task 2.1a), so two dispatches into the SAME session can
|
|
2032
|
+
* never interleave and mis-correlate their read-backs. Distinct sessions run
|
|
2033
|
+
* concurrently. The chained tail intentionally ignores the prior result/error
|
|
2034
|
+
* (each dispatch reports its own outcome to its caller).
|
|
2035
|
+
*/
|
|
2036
|
+
dispatchLocked(sessionId, fn) {
|
|
2037
|
+
const prior = this.sessionDispatchLocks.get(sessionId) ?? Promise.resolve();
|
|
2038
|
+
const run2 = prior.then(fn, fn);
|
|
2039
|
+
this.sessionDispatchLocks.set(
|
|
2040
|
+
sessionId,
|
|
2041
|
+
run2.then(
|
|
2042
|
+
() => void 0,
|
|
2043
|
+
() => void 0
|
|
2044
|
+
)
|
|
2045
|
+
);
|
|
2046
|
+
return run2;
|
|
2047
|
+
}
|
|
1794
2048
|
/** Register a freshly-dispatched message with its session's watcher state. */
|
|
1795
2049
|
registerInFlight(conv, sessionId, message, opencodeMessageId) {
|
|
1796
2050
|
let watcher = this.watchers.get(sessionId);
|
|
@@ -1812,7 +2066,54 @@ var ChannelDriver = class {
|
|
|
1812
2066
|
dispatchedAt: now,
|
|
1813
2067
|
deadline: now + this.pausedMaxWaitMs,
|
|
1814
2068
|
started: false,
|
|
1815
|
-
done: false
|
|
2069
|
+
done: false,
|
|
2070
|
+
stuckReported: false
|
|
2071
|
+
});
|
|
2072
|
+
}
|
|
2073
|
+
/**
|
|
2074
|
+
* Register a RE-ADOPTED `processing` message with its session watcher
|
|
2075
|
+
* (ADR-0046, WI-4). Mirrors `registerInFlight` but anchors the give-up
|
|
2076
|
+
* `deadline` to the row's SERVER-SIDE `processed_at` (Invariant 1), NEVER to
|
|
2077
|
+
* `now`: a row already `processing` for e.g. 5 min must give up ~5 min from now
|
|
2078
|
+
* (10 min after `processed_at`), not 10 min from now — otherwise its deadline
|
|
2079
|
+
* lands ~15 min after `processed_at`, coinciding with the cron reset →
|
|
2080
|
+
* double-drive race. `dispatchedAt` stays `now` (only the appear-guard uses it).
|
|
2081
|
+
*
|
|
2082
|
+
* `evidentMessageId` addresses the SERVER row (for markProcessing/markDone);
|
|
2083
|
+
* `opencodeMessageId` is the id the watcher polls for a reply — for the orphan
|
|
2084
|
+
* fresh-run path these differ (a fresh opencode id under the same server row).
|
|
2085
|
+
*
|
|
2086
|
+
* `started` is set true so the watcher does NOT re-`markProcessing` a row the
|
|
2087
|
+
* server already flipped to `processing`; the running/done transitions still
|
|
2088
|
+
* fire from the watcher's normal branches.
|
|
2089
|
+
*/
|
|
2090
|
+
registerReadopted(conv, sessionId, message, opencodeMessageId, processedAtMs) {
|
|
2091
|
+
let watcher = this.watchers.get(sessionId);
|
|
2092
|
+
if (!watcher) {
|
|
2093
|
+
watcher = {
|
|
2094
|
+
conv,
|
|
2095
|
+
inFlight: /* @__PURE__ */ new Map(),
|
|
2096
|
+
loop: null,
|
|
2097
|
+
reportedQuestions: /* @__PURE__ */ new Set(),
|
|
2098
|
+
reportedPermissions: /* @__PURE__ */ new Set()
|
|
2099
|
+
};
|
|
2100
|
+
this.watchers.set(sessionId, watcher);
|
|
2101
|
+
}
|
|
2102
|
+
watcher.inFlight.set(message.id, {
|
|
2103
|
+
evidentMessageId: message.id,
|
|
2104
|
+
opencodeMessageId,
|
|
2105
|
+
message,
|
|
2106
|
+
dispatchedAt: this.now(),
|
|
2107
|
+
deadline: processedAtMs + this.pausedMaxWaitMs,
|
|
2108
|
+
// The server row is ALREADY `processing`; do not re-fire markProcessing.
|
|
2109
|
+
started: true,
|
|
2110
|
+
done: false,
|
|
2111
|
+
// Not yet reported stuck-queued. The once-guard (`stuckReported`) applies,
|
|
2112
|
+
// AND the stuck-queued observer INCLUDES re-adopted queued wedges: it gates
|
|
2113
|
+
// on `state === 'queued'` (turn produced no reply), not on `started`, so a
|
|
2114
|
+
// re-adopted row left wedged in `queued` still emits the signal once
|
|
2115
|
+
// (#210/#220 observability).
|
|
2116
|
+
stuckReported: false
|
|
1816
2117
|
});
|
|
1817
2118
|
}
|
|
1818
2119
|
/**
|
|
@@ -1878,6 +2179,7 @@ var ChannelDriver = class {
|
|
|
1878
2179
|
conversation_id: watcher.conv.id
|
|
1879
2180
|
});
|
|
1880
2181
|
for (const evidentMessageId of [...watcher.inFlight.keys()]) {
|
|
2182
|
+
this.readopted.delete(evidentMessageId);
|
|
1881
2183
|
this.removeInFlight(watcher, evidentMessageId);
|
|
1882
2184
|
}
|
|
1883
2185
|
return;
|
|
@@ -1898,10 +2200,15 @@ var ChannelDriver = class {
|
|
|
1898
2200
|
async serviceInFlightMessage(sessionId, watcher, inFlight, messages) {
|
|
1899
2201
|
const conv = watcher.conv;
|
|
1900
2202
|
const state = messageRunState(messages, inFlight.opencodeMessageId);
|
|
1901
|
-
if ((state === "running" || state === "done") && !inFlight.started) {
|
|
2203
|
+
if ((state === "running" || state === "done" || state === "failed") && !inFlight.started) {
|
|
1902
2204
|
let claimed;
|
|
1903
2205
|
try {
|
|
1904
|
-
claimed = await this.markProcessing(
|
|
2206
|
+
claimed = await this.markProcessing(
|
|
2207
|
+
conv.id,
|
|
2208
|
+
inFlight.evidentMessageId,
|
|
2209
|
+
sessionId,
|
|
2210
|
+
inFlight.opencodeMessageId
|
|
2211
|
+
);
|
|
1905
2212
|
} catch (err) {
|
|
1906
2213
|
if (err instanceof ChannelAuthError) throw err;
|
|
1907
2214
|
this.log({
|
|
@@ -1931,7 +2238,12 @@ var ChannelDriver = class {
|
|
|
1931
2238
|
message_id: inFlight.evidentMessageId
|
|
1932
2239
|
});
|
|
1933
2240
|
try {
|
|
1934
|
-
await this.markDone(
|
|
2241
|
+
await this.markDone(
|
|
2242
|
+
conv.id,
|
|
2243
|
+
inFlight.evidentMessageId,
|
|
2244
|
+
sessionId,
|
|
2245
|
+
inFlight.opencodeMessageId
|
|
2246
|
+
);
|
|
1935
2247
|
} catch (err) {
|
|
1936
2248
|
if (err instanceof ChannelAuthError) throw err;
|
|
1937
2249
|
if (err instanceof ChannelTerminalError) {
|
|
@@ -1967,10 +2279,59 @@ var ChannelDriver = class {
|
|
|
1967
2279
|
this.removeInFlight(watcher, inFlight.evidentMessageId);
|
|
1968
2280
|
return;
|
|
1969
2281
|
}
|
|
1970
|
-
if (state === "
|
|
1971
|
-
if (
|
|
1972
|
-
|
|
2282
|
+
if (state === "failed") {
|
|
2283
|
+
if (!inFlight.done) {
|
|
2284
|
+
const error2 = messageError(messages, inFlight.opencodeMessageId) ?? void 0;
|
|
2285
|
+
this.log({
|
|
2286
|
+
level: "error",
|
|
2287
|
+
message: `Message ${inFlight.evidentMessageId.slice(0, 8)} errored \u2014 marking failed: ${error2 ?? "(no error text)"}`,
|
|
2288
|
+
conversation_id: conv.id,
|
|
2289
|
+
message_id: inFlight.evidentMessageId
|
|
2290
|
+
});
|
|
2291
|
+
try {
|
|
2292
|
+
await this.markFailed(conv.id, inFlight.evidentMessageId, sessionId, error2);
|
|
2293
|
+
} catch (err) {
|
|
2294
|
+
if (err instanceof ChannelAuthError) throw err;
|
|
2295
|
+
if (err instanceof ChannelTerminalError) {
|
|
2296
|
+
this.log({
|
|
2297
|
+
level: "error",
|
|
2298
|
+
message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} failed (terminal HTTP ${err.status}) \u2014 leaving for the cron safety net: ${err.message}`,
|
|
2299
|
+
conversation_id: conv.id,
|
|
2300
|
+
message_id: inFlight.evidentMessageId
|
|
2301
|
+
});
|
|
2302
|
+
this.removeInFlight(watcher, inFlight.evidentMessageId);
|
|
2303
|
+
return;
|
|
2304
|
+
}
|
|
2305
|
+
if (this.now() >= inFlight.deadline) {
|
|
2306
|
+
this.log({
|
|
2307
|
+
level: "error",
|
|
2308
|
+
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)}`,
|
|
2309
|
+
conversation_id: conv.id,
|
|
2310
|
+
message_id: inFlight.evidentMessageId
|
|
2311
|
+
});
|
|
2312
|
+
this.removeInFlight(watcher, inFlight.evidentMessageId);
|
|
2313
|
+
return;
|
|
2314
|
+
}
|
|
2315
|
+
this.log({
|
|
2316
|
+
level: "error",
|
|
2317
|
+
message: `Failed to mark message ${inFlight.evidentMessageId.slice(0, 8)} failed (will retry next tick): ${err instanceof Error ? err.message : String(err)}`,
|
|
2318
|
+
conversation_id: conv.id,
|
|
2319
|
+
message_id: inFlight.evidentMessageId
|
|
2320
|
+
});
|
|
2321
|
+
return;
|
|
2322
|
+
}
|
|
2323
|
+
inFlight.done = true;
|
|
1973
2324
|
}
|
|
2325
|
+
this.removeInFlight(watcher, inFlight.evidentMessageId);
|
|
2326
|
+
return;
|
|
2327
|
+
}
|
|
2328
|
+
const pastStuckBound = this.now() - inFlight.dispatchedAt >= this.stuckQueuedMs;
|
|
2329
|
+
const sessionIdle = state === "queued" && !hasRunningAssistantExcept(messages, inFlight.opencodeMessageId);
|
|
2330
|
+
if (state === "queued" && pastStuckBound && sessionIdle && !inFlight.stuckReported) {
|
|
2331
|
+
inFlight.stuckReported = true;
|
|
2332
|
+
void this.postSignal(conv.id, inFlight.evidentMessageId, "stuck_queued", {
|
|
2333
|
+
stuck_for_ms: this.now() - inFlight.dispatchedAt
|
|
2334
|
+
});
|
|
1974
2335
|
}
|
|
1975
2336
|
if (this.now() >= inFlight.deadline) {
|
|
1976
2337
|
this.log({
|
|
@@ -1979,48 +2340,395 @@ var ChannelDriver = class {
|
|
|
1979
2340
|
conversation_id: conv.id,
|
|
1980
2341
|
message_id: inFlight.evidentMessageId
|
|
1981
2342
|
});
|
|
2343
|
+
void this.postSignal(conv.id, inFlight.evidentMessageId, "gave_up", {
|
|
2344
|
+
watched_for_ms: this.now() - inFlight.dispatchedAt
|
|
2345
|
+
});
|
|
1982
2346
|
this.removeInFlight(watcher, inFlight.evidentMessageId);
|
|
1983
2347
|
}
|
|
1984
2348
|
}
|
|
2349
|
+
// -------------------------------------------------------------------------
|
|
2350
|
+
// Restart recovery: re-adopt `processing` messages (ADR-0046, WI-3/4/5)
|
|
2351
|
+
// -------------------------------------------------------------------------
|
|
1985
2352
|
/**
|
|
1986
|
-
* Re-
|
|
1987
|
-
*
|
|
1988
|
-
*
|
|
1989
|
-
* the
|
|
2353
|
+
* Re-adopt this agent's `processing` messages on drain (ADR-0046 Decision §1).
|
|
2354
|
+
*
|
|
2355
|
+
* The pending drain only re-drives `pending` rows; a message already flipped to
|
|
2356
|
+
* `processing` before the runner died is watched by nobody until the 15-min
|
|
2357
|
+
* cron resets it. Here we fetch those rows, and per row resolve its correlated
|
|
2358
|
+
* reply against opencode's OWN session store — completing, re-attaching, or
|
|
2359
|
+
* (for an orphan) forcing a genuine fresh run. Runs on EVERY drain tick, so it
|
|
2360
|
+
* is idempotent per message (Invariant 2): a row a watcher already tracks is
|
|
2361
|
+
* skipped in `readoptOne` — one driver, no double-drive.
|
|
2362
|
+
*
|
|
2363
|
+
* Only `ChannelAuthError` propagates (to `drainPending`, like the pending
|
|
2364
|
+
* path); every other early return LOGS a reason with context — no silent drop.
|
|
1990
2365
|
*/
|
|
1991
|
-
async
|
|
2366
|
+
async readoptProcessing() {
|
|
2367
|
+
const rows = await this.getProcessingMessages();
|
|
2368
|
+
if (this.dontRedispatch.size > 0 || this.doneUndeliverable.size > 0) {
|
|
2369
|
+
const stillProcessing = new Set(rows.map((r) => r.id));
|
|
2370
|
+
for (const id of [...this.dontRedispatch, ...this.doneUndeliverable]) {
|
|
2371
|
+
if (!stillProcessing.has(id)) {
|
|
2372
|
+
const cleared = this.dontRedispatch.delete(id);
|
|
2373
|
+
const clearedUndeliverable = this.doneUndeliverable.delete(id);
|
|
2374
|
+
if (cleared || clearedUndeliverable) {
|
|
2375
|
+
this.log({
|
|
2376
|
+
level: "info",
|
|
2377
|
+
message: `Re-adopt: message ${id.slice(0, 8)} left the processing list (cron reset) \u2014 cleared gave-up marker`,
|
|
2378
|
+
message_id: id
|
|
2379
|
+
});
|
|
2380
|
+
}
|
|
2381
|
+
}
|
|
2382
|
+
}
|
|
2383
|
+
}
|
|
2384
|
+
if (rows.length === 0) return;
|
|
2385
|
+
const bySession = /* @__PURE__ */ new Map();
|
|
2386
|
+
for (const row of rows) {
|
|
2387
|
+
if (!row.opencode_session_id) {
|
|
2388
|
+
this.log({
|
|
2389
|
+
level: "error",
|
|
2390
|
+
message: `Cannot re-adopt processing message ${row.id.slice(0, 8)} \u2014 no opencode session id; leaving for the cron safety net`,
|
|
2391
|
+
conversation_id: row.conversation_id,
|
|
2392
|
+
message_id: row.id
|
|
2393
|
+
});
|
|
2394
|
+
continue;
|
|
2395
|
+
}
|
|
2396
|
+
const list = bySession.get(row.opencode_session_id) ?? [];
|
|
2397
|
+
list.push(row);
|
|
2398
|
+
bySession.set(row.opencode_session_id, list);
|
|
2399
|
+
}
|
|
2400
|
+
for (const [sessionId, sessionRows] of bySession) {
|
|
2401
|
+
let messages;
|
|
2402
|
+
try {
|
|
2403
|
+
const res = await this.fetchImpl(`${this.opencodeBase}/session/${sessionId}/message`);
|
|
2404
|
+
if (!res.ok) {
|
|
2405
|
+
this.log({
|
|
2406
|
+
level: "error",
|
|
2407
|
+
message: `Re-adopt: polling session ${sessionId.slice(0, 8)} returned HTTP ${res.status} \u2014 skipping this session this tick`
|
|
2408
|
+
});
|
|
2409
|
+
continue;
|
|
2410
|
+
}
|
|
2411
|
+
const body = await res.json();
|
|
2412
|
+
if (!Array.isArray(body)) {
|
|
2413
|
+
this.log({
|
|
2414
|
+
level: "error",
|
|
2415
|
+
message: `Re-adopt: polling session ${sessionId.slice(0, 8)} returned a non-array message body \u2014 skipping this session this tick`
|
|
2416
|
+
});
|
|
2417
|
+
continue;
|
|
2418
|
+
}
|
|
2419
|
+
messages = body;
|
|
2420
|
+
} catch (err) {
|
|
2421
|
+
this.log({
|
|
2422
|
+
level: "error",
|
|
2423
|
+
message: `Re-adopt: failed to poll session ${sessionId.slice(0, 8)} (will retry next drain): ${err instanceof Error ? err.message : String(err)}`
|
|
2424
|
+
});
|
|
2425
|
+
continue;
|
|
2426
|
+
}
|
|
2427
|
+
for (const row of sessionRows) {
|
|
2428
|
+
await this.readoptOne(sessionId, row, messages);
|
|
2429
|
+
}
|
|
2430
|
+
}
|
|
2431
|
+
}
|
|
2432
|
+
/**
|
|
2433
|
+
* Re-adopt ONE `processing` row against the tick's session message snapshot
|
|
2434
|
+
* (ADR-0046 Decision §1/§2). Idempotent: skips a row already being driven.
|
|
2435
|
+
*
|
|
2436
|
+
* Branches on `messageRunState(messages, row.opencode_message_id)` — the
|
|
2437
|
+
* opencode-assigned user-message id persisted on the first `processing` PATCH
|
|
2438
|
+
* (#218). A row with a NULL stored id (dispatched but the read-back never landed
|
|
2439
|
+
* before the restart) has no id to correlate → treated as an orphan and
|
|
2440
|
+
* re-dispatched (at most once, see `forceReadoptRun`):
|
|
2441
|
+
* - `done` → `markDone` now (guarded like the watcher's done branch);
|
|
2442
|
+
* - `failed` → `markFailed` with the surfaced error (issue #182), so an
|
|
2443
|
+
* errored turn is reported failed on restart, NOT re-dispatched;
|
|
2444
|
+
* - `running`/`queued` → re-attach a watcher via `registerReadopted` (no re-dispatch),
|
|
2445
|
+
* tracking the stored id so the reply correlates by it;
|
|
2446
|
+
* - `unknown`/null id → re-dispatch (opencode assigns a fresh id) + attach a watcher.
|
|
2447
|
+
*
|
|
2448
|
+
* Only `ChannelAuthError` propagates.
|
|
2449
|
+
*/
|
|
2450
|
+
async readoptOne(sessionId, row, messages) {
|
|
2451
|
+
if (this.isTracked(sessionId, row.id)) {
|
|
2452
|
+
this.log({
|
|
2453
|
+
level: "info",
|
|
2454
|
+
message: `Re-adopt: message ${row.id.slice(0, 8)} already tracked in-flight \u2014 skipping (owned by the watcher loop)`,
|
|
2455
|
+
conversation_id: row.conversation_id,
|
|
2456
|
+
message_id: row.id
|
|
2457
|
+
});
|
|
2458
|
+
return;
|
|
2459
|
+
}
|
|
2460
|
+
const ocId = row.opencode_message_id;
|
|
2461
|
+
const state = messageRunState(messages, ocId ?? "");
|
|
2462
|
+
if (state === "done") {
|
|
2463
|
+
if (this.doneUndeliverable.has(row.id)) {
|
|
2464
|
+
this.log({
|
|
2465
|
+
level: "info",
|
|
2466
|
+
message: `Re-adopt: message ${row.id.slice(0, 8)} markDone is terminally undeliverable \u2014 left to the cron; skipping until it leaves processing`,
|
|
2467
|
+
conversation_id: row.conversation_id,
|
|
2468
|
+
message_id: row.id
|
|
2469
|
+
});
|
|
2470
|
+
return;
|
|
2471
|
+
}
|
|
2472
|
+
this.log({
|
|
2473
|
+
level: "info",
|
|
2474
|
+
message: `Re-adopt: message ${row.id.slice(0, 8)} completed while unwatched \u2014 marking done`,
|
|
2475
|
+
conversation_id: row.conversation_id,
|
|
2476
|
+
message_id: row.id
|
|
2477
|
+
});
|
|
2478
|
+
try {
|
|
2479
|
+
await this.markDone(row.conversation_id, row.id, sessionId, ocId);
|
|
2480
|
+
} catch (err) {
|
|
2481
|
+
if (err instanceof ChannelAuthError) throw err;
|
|
2482
|
+
if (err instanceof ChannelTerminalError) {
|
|
2483
|
+
this.doneUndeliverable.add(row.id);
|
|
2484
|
+
this.log({
|
|
2485
|
+
level: "error",
|
|
2486
|
+
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}`,
|
|
2487
|
+
conversation_id: row.conversation_id,
|
|
2488
|
+
message_id: row.id
|
|
2489
|
+
});
|
|
2490
|
+
return;
|
|
2491
|
+
}
|
|
2492
|
+
this.log({
|
|
2493
|
+
level: "error",
|
|
2494
|
+
message: `Re-adopt: failed to mark message ${row.id.slice(0, 8)} done (will retry next drain): ${err instanceof Error ? err.message : String(err)}`,
|
|
2495
|
+
conversation_id: row.conversation_id,
|
|
2496
|
+
message_id: row.id
|
|
2497
|
+
});
|
|
2498
|
+
return;
|
|
2499
|
+
}
|
|
2500
|
+
this.dontRedispatch.delete(row.id);
|
|
2501
|
+
return;
|
|
2502
|
+
}
|
|
2503
|
+
if (state === "failed") {
|
|
2504
|
+
const error2 = messageError(messages, ocId ?? "") ?? void 0;
|
|
2505
|
+
this.log({
|
|
2506
|
+
level: "error",
|
|
2507
|
+
message: `Re-adopt: message ${row.id.slice(0, 8)} errored while unwatched \u2014 marking failed: ${error2 ?? "(no error text)"}`,
|
|
2508
|
+
conversation_id: row.conversation_id,
|
|
2509
|
+
message_id: row.id
|
|
2510
|
+
});
|
|
2511
|
+
try {
|
|
2512
|
+
await this.markFailed(row.conversation_id, row.id, sessionId, error2);
|
|
2513
|
+
} catch (err) {
|
|
2514
|
+
if (err instanceof ChannelAuthError) throw err;
|
|
2515
|
+
if (err instanceof ChannelTerminalError) {
|
|
2516
|
+
this.doneUndeliverable.add(row.id);
|
|
2517
|
+
this.log({
|
|
2518
|
+
level: "error",
|
|
2519
|
+
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}`,
|
|
2520
|
+
conversation_id: row.conversation_id,
|
|
2521
|
+
message_id: row.id
|
|
2522
|
+
});
|
|
2523
|
+
return;
|
|
2524
|
+
}
|
|
2525
|
+
this.log({
|
|
2526
|
+
level: "error",
|
|
2527
|
+
message: `Re-adopt: failed to mark message ${row.id.slice(0, 8)} failed (will retry next drain): ${err instanceof Error ? err.message : String(err)}`,
|
|
2528
|
+
conversation_id: row.conversation_id,
|
|
2529
|
+
message_id: row.id
|
|
2530
|
+
});
|
|
2531
|
+
return;
|
|
2532
|
+
}
|
|
2533
|
+
this.dontRedispatch.delete(row.id);
|
|
2534
|
+
return;
|
|
2535
|
+
}
|
|
2536
|
+
if (this.dontRedispatch.has(row.id)) {
|
|
2537
|
+
this.log({
|
|
2538
|
+
level: "info",
|
|
2539
|
+
message: `Re-adopt: message ${row.id.slice(0, 8)} already gave up \u2014 left to the cron; skipping until it leaves processing`,
|
|
2540
|
+
conversation_id: row.conversation_id,
|
|
2541
|
+
message_id: row.id
|
|
2542
|
+
});
|
|
2543
|
+
return;
|
|
2544
|
+
}
|
|
2545
|
+
if ((state === "running" || state === "queued") && ocId) {
|
|
2546
|
+
const conv = this.convForRow(sessionId, row);
|
|
2547
|
+
const message = this.queuedMessageForRow(row);
|
|
2548
|
+
this.registerReadopted(conv, sessionId, message, ocId, this.processedAtMs(row));
|
|
2549
|
+
this.dispatched.add(row.id);
|
|
2550
|
+
this.readopted.add(row.id);
|
|
2551
|
+
this.ensureWatcherRunning(sessionId);
|
|
2552
|
+
this.log({
|
|
2553
|
+
level: "info",
|
|
2554
|
+
message: `Re-adopt: message ${row.id.slice(0, 8)} ${state} \u2014 re-attached watcher (stored id, no re-dispatch)`,
|
|
2555
|
+
conversation_id: row.conversation_id,
|
|
2556
|
+
message_id: row.id
|
|
2557
|
+
});
|
|
2558
|
+
return;
|
|
2559
|
+
}
|
|
2560
|
+
await this.forceReadoptRun(sessionId, row);
|
|
2561
|
+
}
|
|
2562
|
+
/**
|
|
2563
|
+
* Re-dispatch an orphaned (`unknown`/null-id) `processing` row (ADR-0046 §2).
|
|
2564
|
+
*
|
|
2565
|
+
* #218/WI-5: the row's user message is absent (never kept, or a null stored id),
|
|
2566
|
+
* so we re-`prompt_async` WITHOUT a caller id (opencode assigns a monotonic one),
|
|
2567
|
+
* read it back, and register the watcher under the assigned id so the reply
|
|
2568
|
+
* correlates server-side.
|
|
2569
|
+
*
|
|
2570
|
+
* ⚠️ AT-MOST-ONCE (High-2): dispatch is no longer idempotent (no caller-supplied
|
|
2571
|
+
* id). Without a guard, if this dispatches on tick N but the read-back+persist
|
|
2572
|
+
* hasn't landed before tick N+1 re-reads the still-null `opencode_message_id`,
|
|
2573
|
+
* tick N+1 would dispatch AGAIN → duplicate user turns. The `awaitingReadopt`
|
|
2574
|
+
* latch makes a null-id row re-dispatched AT MOST ONCE per outstanding read-back:
|
|
2575
|
+
* short-circuit while the row is latched; clear it on a successful dispatch (the
|
|
2576
|
+
* row is then tracked in `dispatched`, so `readoptOne`'s early skip prevents
|
|
2577
|
+
* re-entry) OR on a failed/unresolved dispatch (genuinely un-sent → the next tick
|
|
2578
|
+
* may retry exactly once more).
|
|
2579
|
+
*
|
|
2580
|
+
* `evidentMessageId = row.id` addresses the SERVER row. Deadline anchored to
|
|
2581
|
+
* `processed_at` (Invariant 1).
|
|
2582
|
+
*/
|
|
2583
|
+
async forceReadoptRun(sessionId, row) {
|
|
2584
|
+
if (this.stopped) {
|
|
2585
|
+
this.log({
|
|
2586
|
+
level: "info",
|
|
2587
|
+
message: `Re-adopt: message ${row.id.slice(0, 8)} orphaned but the runner is stopping \u2014 not starting a fresh turn; leaving for restart recovery`,
|
|
2588
|
+
conversation_id: row.conversation_id,
|
|
2589
|
+
message_id: row.id
|
|
2590
|
+
});
|
|
2591
|
+
return;
|
|
2592
|
+
}
|
|
2593
|
+
if (this.awaitingReadopt.has(row.id)) {
|
|
2594
|
+
this.log({
|
|
2595
|
+
level: "info",
|
|
2596
|
+
message: `Re-adopt: message ${row.id.slice(0, 8)} already has a re-dispatch awaiting read-back \u2014 skipping (at most once)`,
|
|
2597
|
+
conversation_id: row.conversation_id,
|
|
2598
|
+
message_id: row.id
|
|
2599
|
+
});
|
|
2600
|
+
return;
|
|
2601
|
+
}
|
|
2602
|
+
if (this.processedAtMs(row) + this.pausedMaxWaitMs <= this.now()) {
|
|
2603
|
+
this.dontRedispatch.add(row.id);
|
|
2604
|
+
this.log({
|
|
2605
|
+
level: "info",
|
|
2606
|
+
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)`,
|
|
2607
|
+
conversation_id: row.conversation_id,
|
|
2608
|
+
message_id: row.id
|
|
2609
|
+
});
|
|
2610
|
+
return;
|
|
2611
|
+
}
|
|
1992
2612
|
const options = {
|
|
1993
|
-
agent:
|
|
1994
|
-
model:
|
|
2613
|
+
agent: row.opencode_agent ?? void 0,
|
|
2614
|
+
model: row.opencode_model ?? void 0
|
|
1995
2615
|
};
|
|
1996
2616
|
this.log({
|
|
1997
2617
|
level: "info",
|
|
1998
|
-
message: `
|
|
1999
|
-
|
|
2618
|
+
message: `Re-adopt: message ${row.id.slice(0, 8)} orphaned (user message absent) \u2014 re-dispatching (opencode assigns a fresh id)`,
|
|
2619
|
+
conversation_id: row.conversation_id,
|
|
2620
|
+
message_id: row.id
|
|
2000
2621
|
});
|
|
2622
|
+
this.awaitingReadopt.add(row.id);
|
|
2623
|
+
let ocId;
|
|
2001
2624
|
try {
|
|
2002
|
-
await
|
|
2003
|
-
this.port,
|
|
2625
|
+
ocId = await this.dispatchLocked(
|
|
2004
2626
|
sessionId,
|
|
2005
|
-
|
|
2006
|
-
options,
|
|
2007
|
-
inFlight.opencodeMessageId
|
|
2627
|
+
() => sendPromptAsync(this.port, sessionId, row.content, options)
|
|
2008
2628
|
);
|
|
2009
2629
|
} catch (err) {
|
|
2630
|
+
this.awaitingReadopt.delete(row.id);
|
|
2631
|
+
if (err instanceof ChannelAuthError) throw err;
|
|
2010
2632
|
this.log({
|
|
2011
2633
|
level: "error",
|
|
2012
|
-
message: `Re-dispatch failed for message ${
|
|
2013
|
-
|
|
2634
|
+
message: `Re-adopt: re-dispatch failed for message ${row.id.slice(0, 8)} (will retry next drain): ${err instanceof Error ? err.message : String(err)}`,
|
|
2635
|
+
conversation_id: row.conversation_id,
|
|
2636
|
+
message_id: row.id
|
|
2014
2637
|
});
|
|
2638
|
+
return;
|
|
2015
2639
|
}
|
|
2016
|
-
|
|
2640
|
+
if (ocId === null) {
|
|
2641
|
+
this.awaitingReadopt.delete(row.id);
|
|
2642
|
+
this.log({
|
|
2643
|
+
level: "error",
|
|
2644
|
+
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`,
|
|
2645
|
+
conversation_id: row.conversation_id,
|
|
2646
|
+
message_id: row.id
|
|
2647
|
+
});
|
|
2648
|
+
return;
|
|
2649
|
+
}
|
|
2650
|
+
const conv = this.convForRow(sessionId, row);
|
|
2651
|
+
const message = this.queuedMessageForRow(row);
|
|
2652
|
+
this.registerReadopted(conv, sessionId, message, ocId, this.processedAtMs(row));
|
|
2653
|
+
this.dispatched.add(row.id);
|
|
2654
|
+
this.readopted.add(row.id);
|
|
2655
|
+
this.awaitingReadopt.delete(row.id);
|
|
2656
|
+
this.ensureWatcherRunning(sessionId);
|
|
2657
|
+
}
|
|
2658
|
+
/**
|
|
2659
|
+
* True if `evidentMessageId` is already being driven — either in the
|
|
2660
|
+
* authoritative `dispatched` set or a live watcher's in-flight set for this
|
|
2661
|
+
* session (Invariant 2, WI-5). Either signal means a watcher owns the row.
|
|
2662
|
+
*/
|
|
2663
|
+
isTracked(sessionId, evidentMessageId) {
|
|
2664
|
+
if (this.dispatched.has(evidentMessageId)) return true;
|
|
2665
|
+
const watcher = this.watchers.get(sessionId);
|
|
2666
|
+
return watcher?.inFlight.has(evidentMessageId) ?? false;
|
|
2667
|
+
}
|
|
2668
|
+
/**
|
|
2669
|
+
* Parse a re-adopt row's `processed_at` (ISO string) to epoch ms for the
|
|
2670
|
+
* deadline anchor (Invariant 1). The endpoint guarantees `processed_at` is set
|
|
2671
|
+
* for `processing` rows, but if it is somehow null/unparseable fall back to
|
|
2672
|
+
* `now` (defensive) AND log — a fallback means the anchor is weaker than
|
|
2673
|
+
* intended, which is worth surfacing.
|
|
2674
|
+
*/
|
|
2675
|
+
processedAtMs(row) {
|
|
2676
|
+
const parsed = row.processed_at ? Date.parse(row.processed_at) : NaN;
|
|
2677
|
+
if (!Number.isNaN(parsed)) return parsed;
|
|
2678
|
+
this.log({
|
|
2679
|
+
level: "error",
|
|
2680
|
+
message: `Re-adopt: message ${row.id.slice(0, 8)} has null/unparseable processed_at (${String(row.processed_at)}) \u2014 anchoring deadline to now (defensive)`,
|
|
2681
|
+
conversation_id: row.conversation_id,
|
|
2682
|
+
message_id: row.id
|
|
2683
|
+
});
|
|
2684
|
+
return this.now();
|
|
2685
|
+
}
|
|
2686
|
+
/** Build the `PendingConversation` shape the watcher needs from a re-adopt row. */
|
|
2687
|
+
convForRow(sessionId, row) {
|
|
2688
|
+
return {
|
|
2689
|
+
id: row.conversation_id,
|
|
2690
|
+
agent_id: this.agentId,
|
|
2691
|
+
opencode_session_id: sessionId,
|
|
2692
|
+
pending_message_count: 0,
|
|
2693
|
+
oldest_pending_at: row.processed_at
|
|
2694
|
+
};
|
|
2695
|
+
}
|
|
2696
|
+
/** Build the `QueuedMessage` shape the watcher/re-dispatch needs from a re-adopt row. */
|
|
2697
|
+
queuedMessageForRow(row) {
|
|
2698
|
+
return {
|
|
2699
|
+
id: row.id,
|
|
2700
|
+
content: row.content,
|
|
2701
|
+
status: "processing",
|
|
2702
|
+
opencode_agent: row.opencode_agent,
|
|
2703
|
+
opencode_model: row.opencode_model,
|
|
2704
|
+
source_message_id: row.source_message_id,
|
|
2705
|
+
slack_user_id: row.slack_user_id
|
|
2706
|
+
};
|
|
2017
2707
|
}
|
|
2018
2708
|
/**
|
|
2019
2709
|
* Remove a message from the in-flight set AND the authoritative dispatched
|
|
2020
2710
|
* set. Once the in-flight set empties, the watcher loop's `while` guard exits
|
|
2021
2711
|
* and its `.finally` removes the session entry from `this.watchers`.
|
|
2712
|
+
*
|
|
2713
|
+
* Bug 2: if a RE-ADOPTED message is removed WITHOUT having completed
|
|
2714
|
+
* (`!inFlight.done` — i.e. a give-up: deadline reached, or markDone left to the
|
|
2715
|
+
* cron), park it in `dontRedispatch` so the next drain does NOT re-adopt (and
|
|
2716
|
+
* re-dispatch) the still-`processing` row every ~2s until the 15-min cron. A
|
|
2717
|
+
* re-adopted message that completed (`done`) needs no marker — it's leaving
|
|
2718
|
+
* `processing`. This suppresses only re-dispatch: if its reply later completes,
|
|
2719
|
+
* the done branch still delivers it (Bugbot #202).
|
|
2022
2720
|
*/
|
|
2023
2721
|
removeInFlight(watcher, evidentMessageId) {
|
|
2722
|
+
const inFlight = watcher.inFlight.get(evidentMessageId);
|
|
2723
|
+
if (this.readopted.delete(evidentMessageId) && inFlight && !inFlight.done) {
|
|
2724
|
+
this.dontRedispatch.add(evidentMessageId);
|
|
2725
|
+
this.log({
|
|
2726
|
+
level: "info",
|
|
2727
|
+
message: `Re-adopt: message ${evidentMessageId.slice(0, 8)} gave up \u2014 parking until it leaves the processing list (cron reset)`,
|
|
2728
|
+
conversation_id: watcher.conv.id,
|
|
2729
|
+
message_id: evidentMessageId
|
|
2730
|
+
});
|
|
2731
|
+
}
|
|
2024
2732
|
watcher.inFlight.delete(evidentMessageId);
|
|
2025
2733
|
this.dispatched.delete(evidentMessageId);
|
|
2026
2734
|
}
|
|
@@ -2049,8 +2757,8 @@ var ChannelDriver = class {
|
|
|
2049
2757
|
} catch {
|
|
2050
2758
|
}
|
|
2051
2759
|
for (const q of questions) {
|
|
2052
|
-
if (q.sessionID !== sessionId) continue;
|
|
2053
2760
|
if (watcher.reportedQuestions.has(q.id)) continue;
|
|
2761
|
+
if (!await this.sessionBelongsTo(q.sessionID, sessionId)) continue;
|
|
2054
2762
|
const paused = this.attributeInteraction(watcher, q.tool?.messageID, messages);
|
|
2055
2763
|
const reported = await this.reportInteraction(
|
|
2056
2764
|
watcher.conv.id,
|
|
@@ -2070,8 +2778,8 @@ var ChannelDriver = class {
|
|
|
2070
2778
|
} catch {
|
|
2071
2779
|
}
|
|
2072
2780
|
for (const p of permissions) {
|
|
2073
|
-
if (p.sessionID !== sessionId) continue;
|
|
2074
2781
|
if (watcher.reportedPermissions.has(p.id)) continue;
|
|
2782
|
+
if (!await this.sessionBelongsTo(p.sessionID, sessionId)) continue;
|
|
2075
2783
|
const paused = this.attributeInteraction(watcher, p.messageID, messages);
|
|
2076
2784
|
const reported = await this.reportInteraction(
|
|
2077
2785
|
watcher.conv.id,
|
|
@@ -2082,6 +2790,50 @@ var ChannelDriver = class {
|
|
|
2082
2790
|
if (reported) watcher.reportedPermissions.add(p.id);
|
|
2083
2791
|
}
|
|
2084
2792
|
}
|
|
2793
|
+
/**
|
|
2794
|
+
* True when `sessionId` is the `rootSessionId` itself OR a descendant of it —
|
|
2795
|
+
* i.e. its `parentID` chain (resolved via `GET /session/:id`) reaches the
|
|
2796
|
+
* watched root. Sub-agents spawned via the `task` tool run in child sessions,
|
|
2797
|
+
* so their questions/permissions live under a different `sessionID` that must
|
|
2798
|
+
* still be attributed to the root conversation the watcher owns.
|
|
2799
|
+
*
|
|
2800
|
+
* Parents are cached in `sessionParents` so we walk each session at most once;
|
|
2801
|
+
* a bounded depth cap guards against a cycle or a pathological chain, and any
|
|
2802
|
+
* fetch failure is treated as "not a descendant" (best-effort — the interaction
|
|
2803
|
+
* simply isn't surfaced this tick and is retried next tick once resolvable).
|
|
2804
|
+
*/
|
|
2805
|
+
async sessionBelongsTo(sessionId, rootSessionId) {
|
|
2806
|
+
let current = sessionId;
|
|
2807
|
+
for (let depth = 0; current && depth < 32; depth++) {
|
|
2808
|
+
if (current === rootSessionId) return true;
|
|
2809
|
+
const parent = await this.resolveSessionParent(current);
|
|
2810
|
+
if (parent === null || parent === void 0) return false;
|
|
2811
|
+
current = parent;
|
|
2812
|
+
}
|
|
2813
|
+
return false;
|
|
2814
|
+
}
|
|
2815
|
+
/**
|
|
2816
|
+
* Resolve (and cache) a session's `parentID` via `GET /session/:id`. Returns
|
|
2817
|
+
* `null` for a root session (no parent) and `undefined` when opencode is
|
|
2818
|
+
* unreachable / the session can't be read (so the caller stops walking without
|
|
2819
|
+
* caching a wrong answer — the next tick retries).
|
|
2820
|
+
*/
|
|
2821
|
+
async resolveSessionParent(sessionId) {
|
|
2822
|
+
const cached = this.sessionParents.get(sessionId);
|
|
2823
|
+
if (cached !== void 0) return cached;
|
|
2824
|
+
let parent = void 0;
|
|
2825
|
+
try {
|
|
2826
|
+
const res = await this.fetchImpl(`${this.opencodeBase}/session/${sessionId}`);
|
|
2827
|
+
if (res.ok) {
|
|
2828
|
+
const body = await res.json();
|
|
2829
|
+
parent = body && typeof body.parentID === "string" ? body.parentID : null;
|
|
2830
|
+
}
|
|
2831
|
+
} catch {
|
|
2832
|
+
parent = void 0;
|
|
2833
|
+
}
|
|
2834
|
+
if (parent !== void 0) this.sessionParents.set(sessionId, parent);
|
|
2835
|
+
return parent;
|
|
2836
|
+
}
|
|
2085
2837
|
/**
|
|
2086
2838
|
* Attribute a surfaced interaction to the in-flight message it paused on (M-1).
|
|
2087
2839
|
*
|
|
@@ -2164,6 +2916,35 @@ var ChannelDriver = class {
|
|
|
2164
2916
|
}
|
|
2165
2917
|
return await res.json();
|
|
2166
2918
|
}
|
|
2919
|
+
/**
|
|
2920
|
+
* Fetch this agent's `processing` messages for re-adoption (ADR-0046, WI-1).
|
|
2921
|
+
* The pending path (`getPendingConversations`/`getPendingMessages`) only
|
|
2922
|
+
* surfaces `pending` rows, so a message already `processing` when the runner
|
|
2923
|
+
* died is invisible to it — this dedicated endpoint returns exactly those rows
|
|
2924
|
+
* with the fields the re-adopt path needs (`processed_at`,
|
|
2925
|
+
* `opencode_session_id`, routing).
|
|
2926
|
+
*
|
|
2927
|
+
* Response is an OBJECT WRAPPER `{ messages: [...] }` (snake_case) — NOT a bare
|
|
2928
|
+
* array. Propagates `ChannelAuthError` on 401/403; throws a plain `Error` on
|
|
2929
|
+
* other non-ok so `drainPending`'s try/finally leaves `draining` false and the
|
|
2930
|
+
* next tick retries.
|
|
2931
|
+
*/
|
|
2932
|
+
async getProcessingMessages() {
|
|
2933
|
+
const res = await this.fetchImpl(
|
|
2934
|
+
`${this.apiUrl}/agents/${this.agentId}/conversations/processing`,
|
|
2935
|
+
{ headers: { Authorization: this.getAuthHeader() } }
|
|
2936
|
+
);
|
|
2937
|
+
this.assertAuth(res, "fetching processing messages");
|
|
2938
|
+
if (!res.ok) {
|
|
2939
|
+
throw new Error(`Failed to get processing messages: HTTP ${res.status}`);
|
|
2940
|
+
}
|
|
2941
|
+
const data = await res.json();
|
|
2942
|
+
let messages = data.messages ?? [];
|
|
2943
|
+
if (this.conversationFilter) {
|
|
2944
|
+
messages = messages.filter((m) => m.conversation_id === this.conversationFilter);
|
|
2945
|
+
}
|
|
2946
|
+
return messages;
|
|
2947
|
+
}
|
|
2167
2948
|
/**
|
|
2168
2949
|
* EXISTING combinedAuth route — now fired by the watcher on queued→running
|
|
2169
2950
|
* (Task 3.3), NOT at dispatch/claim time. `{status:'processing',
|
|
@@ -2185,13 +2966,17 @@ var ChannelDriver = class {
|
|
|
2185
2966
|
* A single attempt (no internal retry): the watcher's per-tick loop is the
|
|
2186
2967
|
* retry vehicle for the swap-to-running.
|
|
2187
2968
|
*/
|
|
2188
|
-
async markProcessing(conversationId, messageId, sessionId) {
|
|
2969
|
+
async markProcessing(conversationId, messageId, sessionId, opencodeMessageId) {
|
|
2189
2970
|
const res = await this.fetchImpl(
|
|
2190
2971
|
`${this.apiUrl}/agents/${this.agentId}/threads/${conversationId}/messages/${messageId}`,
|
|
2191
2972
|
{
|
|
2192
2973
|
method: "PATCH",
|
|
2193
2974
|
headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
|
|
2194
|
-
body: JSON.stringify({
|
|
2975
|
+
body: JSON.stringify({
|
|
2976
|
+
status: "processing",
|
|
2977
|
+
opencode_session_id: sessionId,
|
|
2978
|
+
...opencodeMessageId ? { opencode_message_id: opencodeMessageId } : {}
|
|
2979
|
+
})
|
|
2195
2980
|
}
|
|
2196
2981
|
);
|
|
2197
2982
|
this.assertAuth(res, "marking message as processing");
|
|
@@ -2229,13 +3014,17 @@ var ChannelDriver = class {
|
|
|
2229
3014
|
* watcher retries next tick within the
|
|
2230
3015
|
* deadline, Finding 4).
|
|
2231
3016
|
*/
|
|
2232
|
-
async markDone(conversationId, messageId, sessionId) {
|
|
3017
|
+
async markDone(conversationId, messageId, sessionId, opencodeMessageId) {
|
|
2233
3018
|
const res = await this.fetchImpl(
|
|
2234
3019
|
`${this.apiUrl}/agents/${this.agentId}/threads/${conversationId}/messages/${messageId}`,
|
|
2235
3020
|
{
|
|
2236
3021
|
method: "PATCH",
|
|
2237
3022
|
headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
|
|
2238
|
-
body: JSON.stringify({
|
|
3023
|
+
body: JSON.stringify({
|
|
3024
|
+
status: "done",
|
|
3025
|
+
opencode_session_id: sessionId,
|
|
3026
|
+
...opencodeMessageId ? { opencode_message_id: opencodeMessageId } : {}
|
|
3027
|
+
})
|
|
2239
3028
|
}
|
|
2240
3029
|
);
|
|
2241
3030
|
this.assertAuth(res, "marking message as done");
|
|
@@ -2245,7 +3034,17 @@ var ChannelDriver = class {
|
|
|
2245
3034
|
}
|
|
2246
3035
|
throw new ChannelTerminalError(`marking message as done: HTTP ${res.status}`, res.status);
|
|
2247
3036
|
}
|
|
2248
|
-
|
|
3037
|
+
/**
|
|
3038
|
+
* Mark a message `failed`. `sessionId` / `error` are threaded to the API ONLY
|
|
3039
|
+
* when provided (issue #182): a bare `markFailed(conv, msg)` sends
|
|
3040
|
+
* `{status:'failed'}` unchanged (the dispatch-failure path), while an errored
|
|
3041
|
+
* OpenCode turn sends `{status:'failed', opencode_session_id, error}` so the
|
|
3042
|
+
* failure reason reaches the channel.
|
|
3043
|
+
*/
|
|
3044
|
+
async markFailed(conversationId, messageId, sessionId, error2) {
|
|
3045
|
+
const body = { status: "failed" };
|
|
3046
|
+
if (sessionId !== void 0) body.opencode_session_id = sessionId;
|
|
3047
|
+
if (error2 !== void 0) body.error = error2;
|
|
2249
3048
|
await this.callWithRetry(
|
|
2250
3049
|
"marking message as failed",
|
|
2251
3050
|
() => this.fetchImpl(
|
|
@@ -2253,11 +3052,47 @@ var ChannelDriver = class {
|
|
|
2253
3052
|
{
|
|
2254
3053
|
method: "PATCH",
|
|
2255
3054
|
headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
|
|
2256
|
-
body: JSON.stringify(
|
|
3055
|
+
body: JSON.stringify(body)
|
|
2257
3056
|
}
|
|
2258
3057
|
)
|
|
2259
3058
|
);
|
|
2260
3059
|
}
|
|
3060
|
+
/**
|
|
3061
|
+
* Best-effort, SINGLE-ATTEMPT runner→server telemetry ping
|
|
3062
|
+
* (queued-followup-redrive). `POST .../messages/:id/signal {signal, ...extra}`
|
|
3063
|
+
* — the server records it via `log()` (no DB write, no notification). This is
|
|
3064
|
+
* fire-and-forget: it MUST NEVER throw into the drain or the watcher tick, and
|
|
3065
|
+
* MUST NOT use `callWithRetry` (a telemetry ping must not block the sequential
|
|
3066
|
+
* watcher tick — one attempt is enough). A failure is SWALLOWED but LOGGED with
|
|
3067
|
+
* context (no silent catch, per development-workflow).
|
|
3068
|
+
*/
|
|
3069
|
+
async postSignal(conversationId, messageId, signal, extra) {
|
|
3070
|
+
try {
|
|
3071
|
+
const res = await this.fetchImpl(
|
|
3072
|
+
`${this.apiUrl}/agents/${this.agentId}/threads/${conversationId}/messages/${messageId}/signal`,
|
|
3073
|
+
{
|
|
3074
|
+
method: "POST",
|
|
3075
|
+
headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
|
|
3076
|
+
body: JSON.stringify({ signal, ...extra })
|
|
3077
|
+
}
|
|
3078
|
+
);
|
|
3079
|
+
if (!res.ok) {
|
|
3080
|
+
this.log({
|
|
3081
|
+
level: "error",
|
|
3082
|
+
message: `Signal '${signal}' for message ${messageId.slice(0, 8)} returned HTTP ${res.status} (telemetry-only, ignored)`,
|
|
3083
|
+
conversation_id: conversationId,
|
|
3084
|
+
message_id: messageId
|
|
3085
|
+
});
|
|
3086
|
+
}
|
|
3087
|
+
} catch (err) {
|
|
3088
|
+
this.log({
|
|
3089
|
+
level: "error",
|
|
3090
|
+
message: `Signal '${signal}' for message ${messageId.slice(0, 8)} failed (telemetry-only, ignored): ${err instanceof Error ? err.message : String(err)}`,
|
|
3091
|
+
conversation_id: conversationId,
|
|
3092
|
+
message_id: messageId
|
|
3093
|
+
});
|
|
3094
|
+
}
|
|
3095
|
+
}
|
|
2261
3096
|
async persistSession(conversationId, sessionId) {
|
|
2262
3097
|
const res = await this.fetchImpl(
|
|
2263
3098
|
`${this.apiUrl}/agents/${this.agentId}/threads/${conversationId}`,
|
|
@@ -2528,6 +3363,25 @@ async function resolveAgentIdFromKey(authHeader) {
|
|
|
2528
3363
|
return { error: `Failed to resolve agent from key: ${message}` };
|
|
2529
3364
|
}
|
|
2530
3365
|
}
|
|
3366
|
+
async function notifyAgentDisconnected(agentId, authHeader) {
|
|
3367
|
+
const apiUrl = getApiUrlConfig();
|
|
3368
|
+
try {
|
|
3369
|
+
const response = await fetch(`${apiUrl}/agents/${agentId}/disconnect`, {
|
|
3370
|
+
method: "POST",
|
|
3371
|
+
headers: { Authorization: authHeader }
|
|
3372
|
+
});
|
|
3373
|
+
if (!response.ok) {
|
|
3374
|
+
const serverMessage = await readErrorMessage(response);
|
|
3375
|
+
return {
|
|
3376
|
+
ok: false,
|
|
3377
|
+
error: `HTTP ${response.status}${serverMessage ? `: ${serverMessage}` : ""}`
|
|
3378
|
+
};
|
|
3379
|
+
}
|
|
3380
|
+
return { ok: true };
|
|
3381
|
+
} catch (error2) {
|
|
3382
|
+
return { ok: false, error: error2 instanceof Error ? error2.message : String(error2) };
|
|
3383
|
+
}
|
|
3384
|
+
}
|
|
2531
3385
|
async function getAgentInfo(agentId, authHeader) {
|
|
2532
3386
|
const apiUrl = getApiUrlConfig();
|
|
2533
3387
|
try {
|
|
@@ -2573,6 +3427,8 @@ async function getAgentInfo(agentId, authHeader) {
|
|
|
2573
3427
|
// src/commands/run.ts
|
|
2574
3428
|
var MAX_ACTIVITY_LOG_ENTRIES = 10;
|
|
2575
3429
|
var CHANNEL_POLL_INTERVAL_MS = Number(process.env.EVIDENT_CHANNEL_POLL_INTERVAL_MS) || 2e3;
|
|
3430
|
+
var CHANNEL_STUCK_QUEUED_MS = Number(process.env.EVIDENT_STUCK_QUEUED_MS) || void 0;
|
|
3431
|
+
var SHUTDOWN_DRAIN_TIMEOUT_MS = Number(process.env.EVIDENT_SHUTDOWN_DRAIN_MS) || 25e3;
|
|
2576
3432
|
function log2(state, message, isError = false) {
|
|
2577
3433
|
if (state.json) {
|
|
2578
3434
|
console.log(
|
|
@@ -2738,8 +3594,42 @@ async function driveChannels(state, driver) {
|
|
|
2738
3594
|
}
|
|
2739
3595
|
}
|
|
2740
3596
|
}
|
|
2741
|
-
async function
|
|
3597
|
+
async function notifyOffline(state) {
|
|
3598
|
+
if (!state.agentId || !state.authHeader) return;
|
|
3599
|
+
if (!state.connected) {
|
|
3600
|
+
log2(state, "Skipping offline signal \u2014 this runner does not hold the live tunnel");
|
|
3601
|
+
return;
|
|
3602
|
+
}
|
|
3603
|
+
const result = await notifyAgentDisconnected(state.agentId, state.authHeader);
|
|
3604
|
+
if (result.ok) {
|
|
3605
|
+
log2(state, "Notified Evident the agent is going offline");
|
|
3606
|
+
} else {
|
|
3607
|
+
logActivity(state, {
|
|
3608
|
+
type: "error",
|
|
3609
|
+
error: `Could not notify Evident of offline status (relay will still report it): ${result.error}`
|
|
3610
|
+
});
|
|
3611
|
+
if (state.interactive) displayStatus(state);
|
|
3612
|
+
}
|
|
3613
|
+
}
|
|
3614
|
+
async function cleanup(state, opts = {}) {
|
|
2742
3615
|
state.running = false;
|
|
3616
|
+
if (opts.graceful && state.channelDriver) {
|
|
3617
|
+
state.channelDriver.stop();
|
|
3618
|
+
log2(state, "Draining in-flight channel work before shutdown...");
|
|
3619
|
+
if (state.interactive) {
|
|
3620
|
+
logActivity(state, { type: "info", message: "Draining in-flight work before shutdown..." });
|
|
3621
|
+
displayStatus(state);
|
|
3622
|
+
}
|
|
3623
|
+
const settled = await state.channelDriver.waitForInFlight(SHUTDOWN_DRAIN_TIMEOUT_MS);
|
|
3624
|
+
if (!settled) {
|
|
3625
|
+
logActivity(state, {
|
|
3626
|
+
type: "info",
|
|
3627
|
+
message: "Shutdown drain timed out with work still in flight \u2014 leaving it for restart recovery"
|
|
3628
|
+
});
|
|
3629
|
+
if (state.interactive) displayStatus(state);
|
|
3630
|
+
}
|
|
3631
|
+
}
|
|
3632
|
+
await notifyOffline(state);
|
|
2743
3633
|
if (state.connection) {
|
|
2744
3634
|
state.connection.close();
|
|
2745
3635
|
state.connection = null;
|
|
@@ -2770,7 +3660,9 @@ async function run(options) {
|
|
|
2770
3660
|
opencodeVersion: null,
|
|
2771
3661
|
opencodeProcess: null,
|
|
2772
3662
|
connection: null,
|
|
3663
|
+
channelDriver: null,
|
|
2773
3664
|
running: true,
|
|
3665
|
+
shuttingDown: false,
|
|
2774
3666
|
activityLog: [],
|
|
2775
3667
|
messageCount: 0,
|
|
2776
3668
|
lastProxiedActivityAt: null,
|
|
@@ -2784,13 +3676,15 @@ async function run(options) {
|
|
|
2784
3676
|
);
|
|
2785
3677
|
}
|
|
2786
3678
|
const handleSignal = async () => {
|
|
3679
|
+
if (state.shuttingDown) return;
|
|
3680
|
+
state.shuttingDown = true;
|
|
2787
3681
|
if (state.interactive) {
|
|
2788
3682
|
logActivity(state, { type: "info", message: "Shutting down..." });
|
|
2789
3683
|
displayStatus(state);
|
|
2790
3684
|
} else {
|
|
2791
3685
|
log2(state, "Shutting down...");
|
|
2792
3686
|
}
|
|
2793
|
-
await cleanup(state);
|
|
3687
|
+
await cleanup(state, { graceful: true });
|
|
2794
3688
|
await shutdownTelemetry();
|
|
2795
3689
|
process.exit(0);
|
|
2796
3690
|
};
|
|
@@ -2910,12 +3804,14 @@ async function run(options) {
|
|
|
2910
3804
|
apiUrl: getApiUrlConfig(),
|
|
2911
3805
|
getAuthHeader: () => state.authHeader,
|
|
2912
3806
|
conversationFilter: state.conversationFilter,
|
|
3807
|
+
stuckQueuedMs: CHANNEL_STUCK_QUEUED_MS,
|
|
2913
3808
|
log: (entry) => logActivity(state, {
|
|
2914
3809
|
type: entry.level === "error" ? "error" : "info",
|
|
2915
3810
|
message: entry.message,
|
|
2916
3811
|
error: entry.level === "error" ? entry.message : void 0
|
|
2917
3812
|
})
|
|
2918
3813
|
});
|
|
3814
|
+
state.channelDriver = channelDriver;
|
|
2919
3815
|
const connection = new RunnerConnection({
|
|
2920
3816
|
agentId: state.agentId,
|
|
2921
3817
|
getAuthHeader: () => state.authHeader,
|
|
@@ -2929,7 +3825,11 @@ async function run(options) {
|
|
|
2929
3825
|
type: "info",
|
|
2930
3826
|
message: `Tunnel ${isReconnect ? "reconnected" : "connected"} (agent: ${agentId})`
|
|
2931
3827
|
});
|
|
2932
|
-
emitAgentConnected(state.agentId, {
|
|
3828
|
+
emitAgentConnected(state.agentId, {
|
|
3829
|
+
port: state.port,
|
|
3830
|
+
cli_version: getCliVersion(),
|
|
3831
|
+
opencode_version: state.opencodeVersion
|
|
3832
|
+
});
|
|
2933
3833
|
if (!isReconnect) tunnelSpinner?.succeed("Tunnel connected");
|
|
2934
3834
|
if (state.interactive) displayStatus(state);
|
|
2935
3835
|
channelDriver.drainPending().then((processed) => {
|
|
@@ -3012,6 +3912,7 @@ async function run(options) {
|
|
|
3012
3912
|
log2(state, "Driving channel messages...");
|
|
3013
3913
|
}
|
|
3014
3914
|
await driveChannels(state, channelDriver);
|
|
3915
|
+
if (state.shuttingDown) return;
|
|
3015
3916
|
await cleanup(state);
|
|
3016
3917
|
if (state.json) {
|
|
3017
3918
|
console.log(
|
|
@@ -3026,6 +3927,7 @@ async function run(options) {
|
|
|
3026
3927
|
await shutdownTelemetry();
|
|
3027
3928
|
process.exit(0);
|
|
3028
3929
|
} catch (error2) {
|
|
3930
|
+
if (state.shuttingDown) return;
|
|
3029
3931
|
await cleanup(state);
|
|
3030
3932
|
const message = error2 instanceof Error ? error2.message : String(error2);
|
|
3031
3933
|
if (state.json) {
|