@oyasmi/pipiclaw 0.9.5 → 0.9.6-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +220 -126
- package/README.md +217 -123
- package/dist/agent/channel-runner.d.ts +1 -1
- package/dist/agent/channel-runner.js +1 -1
- package/dist/agent/prompt/sections.js +1 -1
- package/dist/agent/prompt/types.d.ts +2 -1
- package/dist/agent/session/compaction.d.ts +2 -0
- package/dist/agent/session/compaction.js +22 -14
- package/dist/agent/session/session-host.d.ts +1 -1
- package/dist/agent/session/session-host.js +3 -1
- package/dist/agent/stop-watchdog.js +5 -1
- package/dist/agent/turn/turn-state.d.ts +2 -0
- package/dist/agent/turn/turn-state.js +2 -1
- package/dist/agent/types.d.ts +3 -1
- package/dist/channel/attachments.d.ts +4 -0
- package/dist/channel/attachments.js +4 -0
- package/dist/channel/store.d.ts +1 -1
- package/dist/commands/catalog.js +25 -9
- package/dist/commands/router.js +5 -1
- package/dist/desk/commands.d.ts +14 -2
- package/dist/desk/commands.js +68 -20
- package/dist/desk/registry.d.ts +6 -2
- package/dist/desk/registry.js +70 -4
- package/dist/desk/types.d.ts +17 -0
- package/dist/dingtalk/bot.d.ts +1 -1
- package/dist/dingtalk/bot.js +4 -3
- package/dist/dingtalk/delivery.js +13 -8
- package/dist/dingtalk/inbound-media.d.ts +36 -14
- package/dist/dingtalk/inbound-media.js +71 -12
- package/dist/dingtalk/inbound.d.ts +9 -2
- package/dist/dingtalk/inbound.js +18 -2
- package/dist/dingtalk/media.d.ts +4 -10
- package/dist/dingtalk/media.js +58 -32
- package/dist/events/commands.d.ts +2 -0
- package/dist/events/commands.js +13 -8
- package/dist/events/history.d.ts +9 -0
- package/dist/events/history.js +66 -0
- package/dist/events/parse.js +7 -3
- package/dist/events/types.d.ts +3 -0
- package/dist/events/watcher.d.ts +3 -7
- package/dist/events/watcher.js +51 -56
- package/dist/jobs/kill.d.ts +23 -0
- package/dist/jobs/kill.js +65 -0
- package/dist/jobs/manager.d.ts +19 -18
- package/dist/jobs/manager.js +36 -103
- package/dist/memory/reflect.js +23 -3
- package/dist/memory/session-corpus.d.ts +1 -1
- package/dist/memory/session-corpus.js +70 -1
- package/dist/memory/session-search.js +13 -11
- package/dist/memory/store.d.ts +2 -0
- package/dist/memory/store.js +19 -9
- package/dist/playbooks/agent-delegation.md +5 -4
- package/dist/playbooks/memory-and-learning.md +1 -1
- package/dist/playbooks/task-lead.md +4 -4
- package/dist/runtime/bootstrap.d.ts +6 -0
- package/dist/runtime/bootstrap.js +11 -7
- package/dist/runtime/desk-runtime.d.ts +8 -2
- package/dist/runtime/desk-runtime.js +35 -4
- package/dist/runtime/durable-dispatch.d.ts +14 -2
- package/dist/runtime/durable-dispatch.js +207 -65
- package/dist/runtime/ingress-busy.js +17 -8
- package/dist/runtime/ingress-deps.d.ts +4 -1
- package/dist/runtime/ingress.js +6 -1
- package/dist/runtime/request-execution.js +7 -6
- package/dist/runtime/shutdown.d.ts +1 -0
- package/dist/runtime/shutdown.js +87 -35
- package/dist/runtime/task-control.d.ts +1 -0
- package/dist/runtime/task-control.js +25 -2
- package/dist/runtime/task-lane.d.ts +1 -0
- package/dist/runtime/task-lane.js +21 -6
- package/dist/runtime/task-step.js +11 -2
- package/dist/runtime/turn-run.js +103 -66
- package/dist/security/command-guard.d.ts +1 -1
- package/dist/security/command-guard.js +237 -314
- package/dist/security/path-guard.js +48 -7
- package/dist/security/shell-parse.d.ts +10 -0
- package/dist/security/shell-parse.js +224 -0
- package/dist/security/types.d.ts +2 -0
- package/dist/subagents/commands.d.ts +1 -1
- package/dist/subagents/detached-discovery.d.ts +1 -1
- package/dist/subagents/discovery.d.ts +18 -199
- package/dist/subagents/discovery.js +35 -289
- package/dist/subagents/dispatch.d.ts +1 -2
- package/dist/subagents/dispatch.js +39 -20
- package/dist/subagents/external/antigravity-cli.d.ts +2 -0
- package/dist/subagents/external/antigravity-cli.js +517 -0
- package/dist/subagents/external/claude-code.js +8 -17
- package/dist/subagents/external/codex-cli.js +8 -18
- package/dist/subagents/external/cursor-cli.js +34 -31
- package/dist/subagents/external/dispatch.d.ts +21 -7
- package/dist/subagents/external/dispatch.js +139 -11
- package/dist/subagents/external/exec.js +3 -0
- package/dist/subagents/external/harness.d.ts +57 -33
- package/dist/subagents/external/harness.js +69 -40
- package/dist/subagents/external/reconciliation.js +1 -0
- package/dist/subagents/external/registry.js +4 -1
- package/dist/subagents/external/role-warnings.d.ts +4 -13
- package/dist/subagents/external/role-warnings.js +4 -28
- package/dist/subagents/external/run.d.ts +1 -2
- package/dist/subagents/external/run.js +11 -11
- package/dist/subagents/external/settlement.d.ts +2 -0
- package/dist/subagents/external/settlement.js +8 -2
- package/dist/subagents/format.d.ts +1 -1
- package/dist/subagents/format.js +1 -1
- package/dist/subagents/internal/outcome.d.ts +1 -2
- package/dist/subagents/internal/provider-retry.d.ts +1 -2
- package/dist/subagents/internal/provider-retry.js +1 -1
- package/dist/subagents/internal/run.d.ts +1 -2
- package/dist/subagents/internal/state.d.ts +1 -2
- package/dist/subagents/internal/worker.d.ts +1 -2
- package/dist/subagents/internal/worker.js +3 -1
- package/dist/subagents/resolve.d.ts +56 -0
- package/dist/subagents/resolve.js +202 -0
- package/dist/subagents/result.d.ts +1 -2
- package/dist/subagents/result.js +7 -3
- package/dist/subagents/run-record.d.ts +33 -2
- package/dist/subagents/run-record.js +2 -0
- package/dist/subagents/runs.d.ts +2 -24
- package/dist/subagents/runs.js +6 -0
- package/dist/subagents/task-envelope.d.ts +1 -2
- package/dist/subagents/task-envelope.js +8 -4
- package/dist/subagents/types.d.ts +93 -4
- package/dist/subagents/types.js +16 -1
- package/dist/subagents/workspace-lease.d.ts +11 -21
- package/dist/subagents/workspace-lease.js +48 -21
- package/dist/subagents/worktree.d.ts +10 -0
- package/dist/subagents/worktree.js +77 -0
- package/dist/tasks/archive.js +4 -0
- package/dist/tasks/commands.d.ts +3 -0
- package/dist/tasks/commands.js +16 -2
- package/dist/tasks/control.d.ts +2 -0
- package/dist/tasks/delivery.d.ts +2 -0
- package/dist/tasks/delivery.js +141 -21
- package/dist/tasks/loop-log.d.ts +2 -0
- package/dist/tasks/loop-log.js +9 -0
- package/dist/tools/bash.js +2 -0
- package/dist/tools/contract/presentation.js +3 -4
- package/dist/tools/event-manage.d.ts +2 -0
- package/dist/tools/event-manage.js +20 -2
- package/dist/tools/index.d.ts +2 -1
- package/dist/tools/index.js +2 -0
- package/dist/tools/registry.js +3 -1
- package/dist/tools/send-media.js +9 -5
- package/dist/tools/session-search.js +1 -1
- package/dist/tools/subagent-manage.d.ts +1 -1
- package/dist/tools/subagent-manage.js +15 -149
- package/dist/tools/subagent.d.ts +2 -0
- package/dist/tools/subagent.js +6 -1
- package/dist/tools/task-manage/factory.js +2 -2
- package/dist/tools/task-manage/lifecycle.d.ts +3 -3
- package/dist/tools/task-manage/lifecycle.js +49 -9
- package/dist/tools/task-manage/schema.d.ts +4 -1
- package/dist/tools/task-manage/schema.js +4 -1
- package/dist/tools/task-manage/types.d.ts +4 -1
- package/dist/tui/cli.d.ts +1 -6
- package/dist/tui/cli.js +29 -3
- package/dist/tui/client.d.ts +2 -0
- package/dist/tui/client.js +13 -4
- package/dist/usage/ledger.d.ts +1 -1
- package/dist/usage/render.js +1 -1
- package/examples/sub-agents/README.md +39 -73
- package/examples/sub-agents/agents/agy-main.md +23 -0
- package/examples/sub-agents/agents/claude-high.md +9 -9
- package/examples/sub-agents/agents/claude-main.md +9 -9
- package/examples/sub-agents/agents/codex-flash.md +9 -9
- package/examples/sub-agents/agents/codex-high.md +9 -9
- package/examples/sub-agents/agents/codex-main.md +9 -9
- package/examples/sub-agents/agents/codex-review.md +9 -9
- package/examples/sub-agents/agents/cursor-high.md +9 -9
- package/examples/sub-agents/agents/cursor-main.md +9 -9
- package/examples/sub-agents/agents/glm-flash.md +9 -9
- package/examples/sub-agents/agents/glm-high.md +9 -9
- package/package.json +8 -8
- package/examples/sub-agents/agents/scout.md +0 -30
- package/examples/sub-agents/roles/builder-hard.md +0 -34
- package/examples/sub-agents/roles/builder.md +0 -34
- package/examples/sub-agents/roles/explorer.md +0 -39
- package/examples/sub-agents/roles/git-committer.md +0 -43
- package/examples/sub-agents/roles/planner.md +0 -33
- package/examples/sub-agents/roles/reviewer.md +0 -33
- package/examples/sub-agents/roles/verifier.md +0 -35
- package/examples/sub-agents/roles/worker.md +0 -34
|
@@ -10,9 +10,10 @@ import * as log from "../log.js";
|
|
|
10
10
|
import { writeFileAtomically } from "../shared/atomic-file.js";
|
|
11
11
|
import { createSerialQueue } from "../shared/serial-queue.js";
|
|
12
12
|
import { errorMessage } from "../shared/text-utils.js";
|
|
13
|
-
import {
|
|
13
|
+
import { isPlainObject } from "../shared/type-guards.js";
|
|
14
14
|
const DEFAULT_LEASE_MS = 15 * 60_000;
|
|
15
15
|
const DEFAULT_INTERVAL_MS = 30_000;
|
|
16
|
+
const DEFAULT_EXHAUSTED_RETENTION_MS = 7 * 24 * 60 * 60_000;
|
|
16
17
|
/** Total delivery attempts (crash-lease redelivery and `markRetryable` failures combined) before
|
|
17
18
|
* a dispatch id is given up on as a poison pill rather than retried forever. */
|
|
18
19
|
const MAX_DELIVERIES = 8;
|
|
@@ -96,31 +97,52 @@ function dispatchId(event) {
|
|
|
96
97
|
.update(JSON.stringify([event.channelId, event.producer, event.ts, event.text]))
|
|
97
98
|
.digest("hex");
|
|
98
99
|
}
|
|
99
|
-
|
|
100
|
+
class CorruptDispatchError extends Error {
|
|
101
|
+
}
|
|
102
|
+
function parseRecord(raw, id) {
|
|
103
|
+
let value;
|
|
100
104
|
try {
|
|
101
|
-
|
|
102
|
-
if (!isRecord(value) ||
|
|
103
|
-
value.version !== 1 ||
|
|
104
|
-
typeof value.id !== "string" ||
|
|
105
|
-
typeof value.createdAt !== "string" ||
|
|
106
|
-
(value.status !== "pending" &&
|
|
107
|
-
value.status !== "queued" &&
|
|
108
|
-
value.status !== "running" &&
|
|
109
|
-
value.status !== "exhausted") ||
|
|
110
|
-
!isRecord(value.event) ||
|
|
111
|
-
value.event.source !== "wake" ||
|
|
112
|
-
typeof value.event.channelId !== "string" ||
|
|
113
|
-
typeof value.event.producer !== "string" ||
|
|
114
|
-
typeof value.event.text !== "string" ||
|
|
115
|
-
typeof value.deliveries !== "number" ||
|
|
116
|
-
(value.nextAttemptAt !== undefined && typeof value.nextAttemptAt !== "string")) {
|
|
117
|
-
return undefined;
|
|
118
|
-
}
|
|
119
|
-
return value;
|
|
105
|
+
value = JSON.parse(raw);
|
|
120
106
|
}
|
|
121
|
-
catch {
|
|
122
|
-
|
|
107
|
+
catch (cause) {
|
|
108
|
+
throw new CorruptDispatchError("Invalid dispatch JSON; have the deployer inspect the record.", { cause });
|
|
109
|
+
}
|
|
110
|
+
const date = (v) => typeof v === "string" && Number.isFinite(Date.parse(v));
|
|
111
|
+
if (!isPlainObject(value) ||
|
|
112
|
+
value.version !== 1 ||
|
|
113
|
+
value.id !== id ||
|
|
114
|
+
!date(value.createdAt) ||
|
|
115
|
+
typeof value.status !== "string" ||
|
|
116
|
+
!["pending", "queued", "running", "exhausted"].includes(value.status) ||
|
|
117
|
+
!isPlainObject(value.event) ||
|
|
118
|
+
value.event.source !== "wake" ||
|
|
119
|
+
typeof value.event.channelId !== "string" ||
|
|
120
|
+
!value.event.channelId ||
|
|
121
|
+
typeof value.event.desk !== "string" ||
|
|
122
|
+
!value.event.desk ||
|
|
123
|
+
typeof value.event.ts !== "string" ||
|
|
124
|
+
!value.event.ts ||
|
|
125
|
+
typeof value.event.producer !== "string" ||
|
|
126
|
+
!value.event.producer ||
|
|
127
|
+
(value.event.presentation !== undefined && value.event.presentation !== "awaited") ||
|
|
128
|
+
(value.event.images !== undefined &&
|
|
129
|
+
(!Array.isArray(value.event.images) ||
|
|
130
|
+
value.event.images.some((image) => !isPlainObject(image) ||
|
|
131
|
+
typeof image.path !== "string" ||
|
|
132
|
+
typeof image.mimeType !== "string" ||
|
|
133
|
+
!Number.isSafeInteger(image.byteSize) ||
|
|
134
|
+
image.byteSize < 0 ||
|
|
135
|
+
(image.unsupportedFormat !== undefined && typeof image.unsupportedFormat !== "boolean")))) ||
|
|
136
|
+
typeof value.event.text !== "string" ||
|
|
137
|
+
value.event.dispatchId !== id ||
|
|
138
|
+
!Number.isSafeInteger(value.deliveries) ||
|
|
139
|
+
value.deliveries < 0 ||
|
|
140
|
+
(value.nextAttemptAt !== undefined && !date(value.nextAttemptAt)) ||
|
|
141
|
+
(value.leaseExpiresAt !== undefined && !date(value.leaseExpiresAt)) ||
|
|
142
|
+
(value.exhaustedAt !== undefined && !date(value.exhaustedAt))) {
|
|
143
|
+
throw new CorruptDispatchError("Invalid dispatch structure; have the deployer inspect the record.");
|
|
123
144
|
}
|
|
145
|
+
return value;
|
|
124
146
|
}
|
|
125
147
|
/**
|
|
126
148
|
* Tiny file-backed outbox for synthetic work. It intentionally provides
|
|
@@ -129,12 +151,24 @@ function parseRecord(raw) {
|
|
|
129
151
|
* channel queue.
|
|
130
152
|
*/
|
|
131
153
|
export class DurableDispatchService {
|
|
154
|
+
run(id, work) {
|
|
155
|
+
const pending = this.queue.run(id, work);
|
|
156
|
+
this.pendingWrites.add(pending);
|
|
157
|
+
void pending.finally(() => this.pendingWrites.delete(pending)).catch(() => undefined);
|
|
158
|
+
return pending;
|
|
159
|
+
}
|
|
160
|
+
/** stop() closes background admission; after active turns settle, wait for started writes
|
|
161
|
+
* before another process can acquire the daemon lock. A hung write keeps that lock retained. */
|
|
162
|
+
async flush() {
|
|
163
|
+
await Promise.allSettled([...this.pendingWrites]);
|
|
164
|
+
}
|
|
132
165
|
constructor(options) {
|
|
133
166
|
this.options = options;
|
|
134
167
|
this.stopped = false;
|
|
135
168
|
this.capacityRejected = new Map();
|
|
136
169
|
this.capacityDrains = new Map();
|
|
137
170
|
this.queue = createSerialQueue();
|
|
171
|
+
this.pendingWrites = new Set();
|
|
138
172
|
this.timer = null;
|
|
139
173
|
/**
|
|
140
174
|
* Dispatch ids whose turn this process is still running (spec 031, D2).
|
|
@@ -155,6 +189,7 @@ export class DurableDispatchService {
|
|
|
155
189
|
/** Bumped by `discardQueuedClaims`; lets an in-flight `absorbPending` notice that its closures were dropped. */
|
|
156
190
|
this.discardEpoch = new Map();
|
|
157
191
|
this.leaseMs = options.leaseMs ?? DEFAULT_LEASE_MS;
|
|
192
|
+
this.exhaustedRetentionMs = options.exhaustedRetentionMs ?? DEFAULT_EXHAUSTED_RETENTION_MS;
|
|
158
193
|
}
|
|
159
194
|
start() {
|
|
160
195
|
if (this.timer)
|
|
@@ -199,7 +234,12 @@ export class DurableDispatchService {
|
|
|
199
234
|
for (const id of [...(this.capacityRejected.get(channelId) ?? [])]) {
|
|
200
235
|
if (this.stopped)
|
|
201
236
|
return;
|
|
202
|
-
|
|
237
|
+
try {
|
|
238
|
+
await this.drainRecord(id, Date.now());
|
|
239
|
+
}
|
|
240
|
+
catch (error) {
|
|
241
|
+
this.reportRecordFault(id, error);
|
|
242
|
+
}
|
|
203
243
|
}
|
|
204
244
|
} while (drain.dirty && !this.stopped);
|
|
205
245
|
})()
|
|
@@ -226,6 +266,8 @@ export class DurableDispatchService {
|
|
|
226
266
|
}
|
|
227
267
|
}
|
|
228
268
|
async dispatch(event) {
|
|
269
|
+
if (this.stopped)
|
|
270
|
+
return false;
|
|
229
271
|
// Spec 058, D7: every in-process producer stamps its desk. An event without one cannot be
|
|
230
272
|
// checked against the channel's binding at admission, so it is refused here, at the door.
|
|
231
273
|
if (!event.desk) {
|
|
@@ -236,7 +278,7 @@ export class DurableDispatchService {
|
|
|
236
278
|
return false;
|
|
237
279
|
}
|
|
238
280
|
const id = event.dispatchId ?? dispatchId(event);
|
|
239
|
-
await this.
|
|
281
|
+
await this.run(id, async () => {
|
|
240
282
|
const existing = await this.read(id);
|
|
241
283
|
if (existing)
|
|
242
284
|
return;
|
|
@@ -268,27 +310,38 @@ export class DurableDispatchService {
|
|
|
268
310
|
try {
|
|
269
311
|
filenames = (await readdir(this.options.stateDir)).filter((name) => name.endsWith(".json"));
|
|
270
312
|
}
|
|
271
|
-
catch {
|
|
272
|
-
|
|
313
|
+
catch (error) {
|
|
314
|
+
if (error.code === "ENOENT")
|
|
315
|
+
return 0;
|
|
316
|
+
throw error;
|
|
273
317
|
}
|
|
274
318
|
let canceled = 0;
|
|
275
319
|
for (const filename of filenames) {
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
320
|
+
if (this.stopped)
|
|
321
|
+
break;
|
|
322
|
+
const id = this.idFromFilename(filename);
|
|
323
|
+
if (id === undefined)
|
|
324
|
+
continue;
|
|
325
|
+
try {
|
|
326
|
+
await this.run(id, async () => {
|
|
327
|
+
const record = await this.read(id);
|
|
328
|
+
if (!record || record.event.channelId !== channelId)
|
|
329
|
+
return;
|
|
330
|
+
if (record.status !== "queued" && record.status !== "running")
|
|
331
|
+
return;
|
|
332
|
+
// Drop the liveness claim too, or the renew branch above would keep this record
|
|
333
|
+
// alive forever and the cancelled turn would never be redelivered.
|
|
334
|
+
this.running.delete(id);
|
|
335
|
+
// `/stop` already dropped the queued closures; the redelivery must not be turned away.
|
|
336
|
+
this.absorbed.delete(id);
|
|
337
|
+
record.leaseExpiresAt = undefined;
|
|
338
|
+
await this.write(record);
|
|
339
|
+
canceled++;
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
catch (error) {
|
|
343
|
+
this.reportRecordFault(id, error);
|
|
344
|
+
}
|
|
292
345
|
}
|
|
293
346
|
return canceled;
|
|
294
347
|
}
|
|
@@ -318,14 +371,14 @@ export class DurableDispatchService {
|
|
|
318
371
|
return true;
|
|
319
372
|
if (this.absorbed.delete(id))
|
|
320
373
|
return false;
|
|
321
|
-
this.
|
|
322
|
-
await this.queue.run(id, async () => {
|
|
374
|
+
await this.run(id, async () => {
|
|
323
375
|
const record = await this.read(id);
|
|
324
376
|
if (!record)
|
|
325
377
|
return;
|
|
326
378
|
record.status = "running";
|
|
327
379
|
record.leaseExpiresAt = new Date(Date.now() + this.leaseMs).toISOString();
|
|
328
380
|
await this.write(record);
|
|
381
|
+
this.running.add(id);
|
|
329
382
|
});
|
|
330
383
|
return true;
|
|
331
384
|
}
|
|
@@ -346,23 +399,32 @@ export class DurableDispatchService {
|
|
|
346
399
|
try {
|
|
347
400
|
filenames = (await readdir(this.options.stateDir)).filter((name) => name.endsWith(".json"));
|
|
348
401
|
}
|
|
349
|
-
catch {
|
|
350
|
-
|
|
402
|
+
catch (error) {
|
|
403
|
+
if (error.code === "ENOENT")
|
|
404
|
+
return [];
|
|
405
|
+
throw error;
|
|
351
406
|
}
|
|
352
407
|
const candidates = [];
|
|
353
408
|
for (const filename of filenames) {
|
|
354
|
-
const id =
|
|
409
|
+
const id = this.idFromFilename(filename);
|
|
410
|
+
if (id === undefined)
|
|
411
|
+
continue;
|
|
355
412
|
if (id === primaryId || this.running.has(id))
|
|
356
413
|
continue;
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
414
|
+
try {
|
|
415
|
+
const record = await this.read(id);
|
|
416
|
+
if (record && this.isAbsorbable(record, channelId, accept))
|
|
417
|
+
candidates.push({ id, createdAt: record.createdAt });
|
|
418
|
+
}
|
|
419
|
+
catch (error) {
|
|
420
|
+
this.reportRecordFault(id, error);
|
|
421
|
+
}
|
|
360
422
|
}
|
|
361
423
|
candidates.sort((a, b) => a.createdAt.localeCompare(b.createdAt));
|
|
362
424
|
const events = [];
|
|
363
425
|
for (const { id } of candidates.slice(0, limit)) {
|
|
364
426
|
try {
|
|
365
|
-
await this.
|
|
427
|
+
await this.run(id, async () => {
|
|
366
428
|
// Re-checked under the record's own lock: a drain or its own delivery may have moved it.
|
|
367
429
|
const record = await this.read(id);
|
|
368
430
|
if (!record || this.running.has(id) || !this.isAbsorbable(record, channelId, accept))
|
|
@@ -409,8 +471,16 @@ export class DurableDispatchService {
|
|
|
409
471
|
return;
|
|
410
472
|
this.running.delete(id);
|
|
411
473
|
this.untrack(id);
|
|
412
|
-
await this.
|
|
413
|
-
|
|
474
|
+
await this.run(id, async () => {
|
|
475
|
+
if (!(await this.read(id)))
|
|
476
|
+
return;
|
|
477
|
+
try {
|
|
478
|
+
await unlink(recordPath(this.options.stateDir, id));
|
|
479
|
+
}
|
|
480
|
+
catch (error) {
|
|
481
|
+
if (error.code !== "ENOENT")
|
|
482
|
+
throw error;
|
|
483
|
+
}
|
|
414
484
|
});
|
|
415
485
|
}
|
|
416
486
|
/** Release a failed handler's in-process claim without deleting its durable record. Structured
|
|
@@ -426,7 +496,7 @@ export class DurableDispatchService {
|
|
|
426
496
|
if (!id)
|
|
427
497
|
return;
|
|
428
498
|
this.running.delete(id);
|
|
429
|
-
await this.
|
|
499
|
+
await this.run(id, async () => {
|
|
430
500
|
const record = await this.read(id);
|
|
431
501
|
if (!record)
|
|
432
502
|
return;
|
|
@@ -442,27 +512,73 @@ export class DurableDispatchService {
|
|
|
442
512
|
try {
|
|
443
513
|
filenames = (await readdir(this.options.stateDir)).filter((name) => name.endsWith(".json")).sort();
|
|
444
514
|
}
|
|
445
|
-
catch {
|
|
446
|
-
|
|
515
|
+
catch (error) {
|
|
516
|
+
if (error.code === "ENOENT")
|
|
517
|
+
return;
|
|
518
|
+
throw error;
|
|
447
519
|
}
|
|
520
|
+
const candidates = [];
|
|
448
521
|
for (const filename of filenames) {
|
|
449
|
-
|
|
450
|
-
|
|
522
|
+
if (this.stopped)
|
|
523
|
+
break;
|
|
524
|
+
const id = this.idFromFilename(filename);
|
|
525
|
+
if (id === undefined)
|
|
526
|
+
continue;
|
|
527
|
+
try {
|
|
528
|
+
const record = await this.read(id);
|
|
529
|
+
if (record) {
|
|
530
|
+
candidates.push({ id, createdAtMs: Date.parse(record.createdAt), filename });
|
|
531
|
+
}
|
|
532
|
+
else {
|
|
533
|
+
this.untrack(id);
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
catch (error) {
|
|
537
|
+
this.reportRecordFault(id, error);
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
candidates.sort((a, b) => a.createdAtMs - b.createdAtMs || a.filename.localeCompare(b.filename));
|
|
541
|
+
for (const { id } of candidates) {
|
|
542
|
+
if (this.stopped)
|
|
543
|
+
break;
|
|
544
|
+
try {
|
|
545
|
+
await this.drainRecord(id, now);
|
|
546
|
+
}
|
|
547
|
+
catch (error) {
|
|
548
|
+
this.reportRecordFault(id, error);
|
|
549
|
+
}
|
|
451
550
|
}
|
|
452
551
|
}
|
|
453
552
|
async drainRecord(id, now) {
|
|
454
|
-
|
|
553
|
+
if (this.stopped)
|
|
554
|
+
return;
|
|
555
|
+
await this.run(id, async () => {
|
|
556
|
+
if (this.stopped)
|
|
557
|
+
return;
|
|
455
558
|
const record = await this.read(id);
|
|
559
|
+
if (this.stopped)
|
|
560
|
+
return;
|
|
456
561
|
if (!record) {
|
|
457
562
|
this.untrack(id);
|
|
458
563
|
return;
|
|
459
564
|
}
|
|
460
565
|
if (record.status === "pending")
|
|
461
566
|
this.trackRejected(record.event.channelId, id);
|
|
462
|
-
// A poison pill: give up on redelivery for good, once.
|
|
463
|
-
// `onExhausted`'s notice stays inspectable
|
|
567
|
+
// A poison pill: give up on redelivery for good, once. Kept on disk during the retention
|
|
568
|
+
// window so `onExhausted`'s notice stays inspectable, then unlinked so dead-letter files
|
|
569
|
+
// do not accumulate across every future drain tick.
|
|
464
570
|
if (record.status === "exhausted") {
|
|
465
571
|
this.untrack(id);
|
|
572
|
+
const exhaustedSinceMs = Date.parse(record.exhaustedAt ?? record.createdAt);
|
|
573
|
+
if (now - exhaustedSinceMs >= this.exhaustedRetentionMs) {
|
|
574
|
+
try {
|
|
575
|
+
await unlink(recordPath(this.options.stateDir, id));
|
|
576
|
+
}
|
|
577
|
+
catch (error) {
|
|
578
|
+
if (error.code !== "ENOENT")
|
|
579
|
+
throw error;
|
|
580
|
+
}
|
|
581
|
+
}
|
|
466
582
|
return;
|
|
467
583
|
}
|
|
468
584
|
// The turn is still running in this process: renew its lease rather than treating a
|
|
@@ -490,6 +606,7 @@ export class DurableDispatchService {
|
|
|
490
606
|
return;
|
|
491
607
|
if (record.deliveries >= MAX_DELIVERIES) {
|
|
492
608
|
record.status = "exhausted";
|
|
609
|
+
record.exhaustedAt = new Date(now).toISOString();
|
|
493
610
|
this.untrack(id);
|
|
494
611
|
record.leaseExpiresAt = undefined;
|
|
495
612
|
record.nextAttemptAt = undefined;
|
|
@@ -528,13 +645,38 @@ export class DurableDispatchService {
|
|
|
528
645
|
await this.write(record);
|
|
529
646
|
});
|
|
530
647
|
}
|
|
648
|
+
idFromFilename(filename) {
|
|
649
|
+
const id = decodeDispatchId(filename.slice(0, -".json".length));
|
|
650
|
+
if (`${encodeDispatchId(id)}.json` === filename)
|
|
651
|
+
return id;
|
|
652
|
+
log.logWarn("dispatch.record.failed", "Dispatch record failed", {
|
|
653
|
+
reason: "Invalid dispatch filename; have the deployer inspect the record.",
|
|
654
|
+
fields: { path: join(this.options.stateDir, filename), classification: "corrupt" },
|
|
655
|
+
});
|
|
656
|
+
return undefined;
|
|
657
|
+
}
|
|
658
|
+
reportRecordFault(id, error) {
|
|
659
|
+
log.logWarn("dispatch.record.failed", "Dispatch record failed", {
|
|
660
|
+
reason: errorMessage(error),
|
|
661
|
+
fields: {
|
|
662
|
+
dispatchId: id,
|
|
663
|
+
path: recordPath(this.options.stateDir, id),
|
|
664
|
+
classification: error instanceof CorruptDispatchError ? "corrupt" : "io",
|
|
665
|
+
code: error.code,
|
|
666
|
+
},
|
|
667
|
+
});
|
|
668
|
+
}
|
|
531
669
|
async read(id) {
|
|
670
|
+
let raw;
|
|
532
671
|
try {
|
|
533
|
-
|
|
672
|
+
raw = await readFile(recordPath(this.options.stateDir, id), "utf-8");
|
|
534
673
|
}
|
|
535
|
-
catch {
|
|
536
|
-
|
|
674
|
+
catch (error) {
|
|
675
|
+
if (error.code === "ENOENT")
|
|
676
|
+
return undefined;
|
|
677
|
+
throw error;
|
|
537
678
|
}
|
|
679
|
+
return parseRecord(raw, id);
|
|
538
680
|
}
|
|
539
681
|
async write(record) {
|
|
540
682
|
await writeFileAtomically(recordPath(this.options.stateDir, record.id), `${JSON.stringify(record)}\n`);
|
|
@@ -96,21 +96,29 @@ export function createBusyHandlers(deps, queues) {
|
|
|
96
96
|
* output is ended at once (the card is closed, late events dropped).
|
|
97
97
|
*/
|
|
98
98
|
function abortTurn(channelId, runner) {
|
|
99
|
+
if (isShuttingDown())
|
|
100
|
+
return;
|
|
99
101
|
runner.requestStop();
|
|
100
102
|
void runner.abort().catch((err) => {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
103
|
+
if (!isShuttingDown())
|
|
104
|
+
log.logWarn("agent.turn.abort_failed", "Turn abort failed", {
|
|
105
|
+
channelId: channelId,
|
|
106
|
+
reason: errorMessage(err),
|
|
107
|
+
});
|
|
105
108
|
});
|
|
106
|
-
|
|
109
|
+
const generation = runner.getTurnStatus().generation;
|
|
110
|
+
const existing = stopWatchdogs.get(channelId);
|
|
111
|
+
if (existing?.runner === runner && existing.generation === generation)
|
|
107
112
|
return;
|
|
108
|
-
|
|
113
|
+
const registration = { runner, generation };
|
|
114
|
+
stopWatchdogs.set(channelId, registration);
|
|
109
115
|
void forceEndStuckTurnAfterStop({
|
|
110
116
|
channelId,
|
|
111
117
|
runner,
|
|
112
118
|
graceMs: options.stopForceEndGraceMs,
|
|
113
|
-
isCurrent: () =>
|
|
119
|
+
isCurrent: () => !isShuttingDown() &&
|
|
120
|
+
stopWatchdogs.get(channelId) === registration &&
|
|
121
|
+
channelRunners.get(channelId) === runner,
|
|
114
122
|
recover: () => {
|
|
115
123
|
runner.retireForRecovery();
|
|
116
124
|
channelRunners.delete(channelId);
|
|
@@ -126,7 +134,8 @@ export function createBusyHandlers(deps, queues) {
|
|
|
126
134
|
});
|
|
127
135
|
})
|
|
128
136
|
.finally(() => {
|
|
129
|
-
stopWatchdogs.
|
|
137
|
+
if (!isShuttingDown() && stopWatchdogs.get(channelId) === registration)
|
|
138
|
+
stopWatchdogs.delete(channelId);
|
|
130
139
|
});
|
|
131
140
|
}
|
|
132
141
|
/** One message's turn is no longer wanted (its caller cancelled or left): abort only that turn. */
|
|
@@ -32,7 +32,10 @@ export interface IngressDeps {
|
|
|
32
32
|
channelRunners: Map<string, AgentRunner>;
|
|
33
33
|
sessionResetChains: Map<string, Promise<void>>;
|
|
34
34
|
activeTasks: Set<Promise<void>>;
|
|
35
|
-
stopWatchdogs:
|
|
35
|
+
stopWatchdogs: Map<string, {
|
|
36
|
+
runner: AgentRunner;
|
|
37
|
+
generation?: number;
|
|
38
|
+
}>;
|
|
36
39
|
channelIndex: ChannelIndex;
|
|
37
40
|
archiveIncomingMessage: (channelId: string, message: ArchivedMessage, contextLabel: string) => Promise<void>;
|
|
38
41
|
/** The channel's runner; `undefined` when it is not bound to an available desk — none is ever built then. */
|
package/dist/runtime/ingress.js
CHANGED
|
@@ -27,7 +27,12 @@ export function createIngress(deps) {
|
|
|
27
27
|
let queue = queues.get(channelId);
|
|
28
28
|
if (!queue) {
|
|
29
29
|
queue = new ChannelQueue((current) => {
|
|
30
|
-
|
|
30
|
+
const watchdog = deps.stopWatchdogs.get(channelId);
|
|
31
|
+
const runner = channelRunners.get(channelId);
|
|
32
|
+
if (stopped ||
|
|
33
|
+
isShuttingDown() ||
|
|
34
|
+
(watchdog && watchdog.runner === runner && watchdog.generation === runner?.getTurnStatus().generation) ||
|
|
35
|
+
queues.get(channelId) !== current)
|
|
31
36
|
return;
|
|
32
37
|
deps.getDurableDispatch()?.notifyCapacity(channelId);
|
|
33
38
|
});
|
|
@@ -56,14 +56,15 @@ export function createRequestExecution(deps) {
|
|
|
56
56
|
});
|
|
57
57
|
}
|
|
58
58
|
finally {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
running.delete(message.channelId);
|
|
59
|
+
// Detach from shared maps before `endTurn()` flips `isBusy()` to false and before
|
|
60
|
+
// `dispose()` awaits, so `evictIdleRunnersOverCap` / `shutdown` can never double-dispose
|
|
61
|
+
// a request-scoped runner or count an ended request against chat's idle LRU cap.
|
|
62
|
+
running.delete(message.channelId);
|
|
63
|
+
if (deps.runners.get(message.channelId) === runner) {
|
|
65
64
|
deps.runners.delete(message.channelId);
|
|
66
65
|
}
|
|
66
|
+
runner.endTurn();
|
|
67
|
+
await runner.dispose();
|
|
67
68
|
}
|
|
68
69
|
};
|
|
69
70
|
const task = run();
|