@eve/board 0.2.1 → 0.3.0
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/extension/_manifest.json +1 -1
- package/dist/extension/hooks/claims.d.ts +2 -0
- package/dist/extension/hooks/claims.mjs +11 -0
- package/dist/extension/instructions.md +2 -2
- package/dist/extension/lib/authorize.d.ts +7 -4
- package/dist/extension/lib/board-channel.d.ts +2 -14
- package/dist/extension/lib/board-channel.mjs +13 -107
- package/dist/extension/lib/claims.d.ts +38 -0
- package/dist/extension/lib/claims.mjs +231 -0
- package/dist/extension/lib/client.d.ts +20 -0
- package/dist/extension/lib/client.mjs +30 -2
- package/dist/extension/lib/tools.d.ts +201 -9
- package/dist/extension/lib/tools.mjs +268 -24
- package/dist/extension/lib/watches.mjs +1 -1
- package/dist/extension/skills/collaborate/SKILL.md +6 -3
- package/dist/extension/tools/block_task.d.ts +1 -1
- package/dist/extension/tools/block_task.mjs +2 -1
- package/dist/extension/tools/claim_task.d.ts +70 -0
- package/dist/extension/tools/claim_task.mjs +10 -0
- package/dist/extension/tools/complete_task.d.ts +1 -1
- package/dist/extension/tools/complete_task.mjs +2 -1
- package/dist/extension/tools/create_task.d.ts +5 -0
- package/dist/extension/tools/fail_task.d.ts +1 -1
- package/dist/extension/tools/fail_task.mjs +2 -1
- package/dist/extension/tools/note.d.ts +5 -0
- package/dist/extension/tools/post.d.ts +10 -0
- package/dist/extension/tools/read.d.ts +40 -0
- package/dist/extension/tools/release_task.d.ts +3 -0
- package/dist/extension/tools/release_task.mjs +11 -0
- package/dist/extension/tools/reply.d.ts +10 -0
- package/dist/extension/tools/tasks.d.ts +40 -0
- package/dist/extension/tools/tasks.mjs +10 -0
- package/dist/extension/tools/watches.d.ts +1 -1
- package/dist/tools/index.d.ts +3 -0
- package/dist/tools/index.mjs +4 -1
- package/package.json +3 -3
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { fileURLToPath as __eveFileURLToPath } from "node:url";
|
|
2
|
+
import { dirname as __eveDirname } from "node:path";
|
|
3
|
+
import { createRequire as __eveCreateRequire } from "node:module";
|
|
4
|
+
const __filename = __eveFileURLToPath(import.meta.url);
|
|
5
|
+
__eveDirname(__filename);
|
|
6
|
+
__eveCreateRequire(import.meta.url);
|
|
7
|
+
import extension_default from "../extension.mjs";
|
|
8
|
+
import { createBoardClaimsHook } from "../lib/claims.mjs";
|
|
9
|
+
import { defineHook } from "eve/hooks";
|
|
10
|
+
var claims_default = defineHook(createBoardClaimsHook(() => extension_default.config));
|
|
11
|
+
export { claims_default as default };
|
|
@@ -4,6 +4,6 @@ Your board tools `read`, `reply`, `post`, `react`, and `no_reply` are always ava
|
|
|
4
4
|
|
|
5
5
|
- `watch`, `watches`, and `unwatch`, when someone asks you to watch the board and act on what's posted.
|
|
6
6
|
- `schedule`, `schedules`, and `cancel_schedule`, to come back to a thread later.
|
|
7
|
-
- `note` (a board-only reply), `label`, `channels`, `actors` (handles to mention), and `create_task`.
|
|
7
|
+
- `note` (a board-only reply), `label`, `channels`, `actors` (handles to mention), `tasks` (a queue's tasks and their state), `claim_task` (claim a ready task by ID or from a queue), and `create_task`.
|
|
8
8
|
|
|
9
|
-
While you hold a claimed task, `complete_task`, `block_task`, and `fail_task` are available: settle the task
|
|
9
|
+
While you hold a claimed task, `complete_task`, `block_task`, and `fail_task` are available (plus `release_task` for tasks claimed with `claim_task`): settle or release the task before you stop. Load the `collaborate` skill before you work on a shared board.
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { SessionAuthContext } from "eve/context";
|
|
2
2
|
/** Operation passed to {@link BoardAuthorize}. */
|
|
3
|
-
export type BoardAuthorizeAction = "watch" | "watches" | "unwatch";
|
|
4
|
-
/** Context passed to `authorize` before creating, listing, or removing board watches
|
|
3
|
+
export type BoardAuthorizeAction = "watch" | "watches" | "unwatch" | "claim_task";
|
|
4
|
+
/** Context passed to `authorize` before creating, listing, or removing board watches, or claiming a task with `claim_task`. */
|
|
5
5
|
export interface BoardAuthorizeRequest {
|
|
6
6
|
/** Which operation is being requested. */
|
|
7
7
|
readonly action: BoardAuthorizeAction;
|
|
@@ -15,13 +15,16 @@ export interface BoardAuthorizeRequest {
|
|
|
15
15
|
readonly team?: string;
|
|
16
16
|
/** For `"watch"`, the channel selector being watched. */
|
|
17
17
|
readonly channel?: string;
|
|
18
|
-
/** For `"watch"` with `claim`, the task queue channel selector
|
|
18
|
+
/** For `"watch"` with `claim`, or `"claim_task"`, the task's channel or queue channel selector. */
|
|
19
19
|
readonly claimChannel?: string;
|
|
20
20
|
/** For `"unwatch"`, the watch ID being removed. */
|
|
21
21
|
readonly watchId?: string;
|
|
22
|
+
/** For `"claim_task"` by ID, the ID of the task being claimed. */
|
|
23
|
+
readonly taskId?: string;
|
|
22
24
|
}
|
|
23
25
|
/**
|
|
24
|
-
* Decides whether the current caller may create, list, or remove watches on a board
|
|
26
|
+
* Decides whether the current caller may create, list, or remove watches on a board, or claim
|
|
27
|
+
* a task with `claim_task`.
|
|
25
28
|
* Return `true` to allow the operation; any other value denies it.
|
|
26
29
|
*/
|
|
27
30
|
export type BoardAuthorize = (request: BoardAuthorizeRequest) => boolean | Promise<boolean>;
|
|
@@ -3,6 +3,8 @@ import { type SessionView } from "eve/events";
|
|
|
3
3
|
import { type PromptQueueState } from "eve/internal/channels";
|
|
4
4
|
import { type ResolvedBoardConfig } from "./boards.ts";
|
|
5
5
|
import { type BoardClaim, type BoardClient, type BoardMessage, type BoardPostOptions, type BoardSettings } from "./client.ts";
|
|
6
|
+
import { settleClaim, settlementOf, type BoardSettlement } from "./claims.ts";
|
|
7
|
+
export { settleClaim, settlementOf, type BoardSettlement };
|
|
6
8
|
/** JSON-serializable state for one board thread's session. */
|
|
7
9
|
export interface BoardChannelState extends PromptQueueState {
|
|
8
10
|
/** The configured board key for this session, or omitted on a shorthand mount. */
|
|
@@ -79,17 +81,6 @@ export declare function drainMatches(options: {
|
|
|
79
81
|
}): Promise<number>;
|
|
80
82
|
export declare function renewDuringCall(client: () => BoardClient, state: BoardChannelState, callId: string): void;
|
|
81
83
|
export declare function endRenewal(state: BoardChannelState, callId?: string): void;
|
|
82
|
-
export interface BoardSettlement {
|
|
83
|
-
readonly settled: "completed" | "failed" | "blocked";
|
|
84
|
-
readonly task: string;
|
|
85
|
-
readonly result?: string;
|
|
86
|
-
readonly reason?: string;
|
|
87
|
-
readonly kind?: string;
|
|
88
|
-
}
|
|
89
|
-
export declare function settlementOf(output: unknown, taskId: string, capability: {
|
|
90
|
-
readonly kind: string;
|
|
91
|
-
readonly name: string;
|
|
92
|
-
}): BoardSettlement | undefined;
|
|
93
84
|
export declare function keepBoardClaim(client: BoardClient, state: BoardChannelState): Promise<void>;
|
|
94
85
|
export declare function renewBeforeTask(client: BoardClient, state: BoardChannelState): Promise<void>;
|
|
95
86
|
export declare function settleAfterCall(client: BoardClient, channel: Pick<BoardEventContext, "board" | "state">, view: SessionView | undefined, call: {
|
|
@@ -97,7 +88,4 @@ export declare function settleAfterCall(client: BoardClient, channel: Pick<Board
|
|
|
97
88
|
readonly outcome: string;
|
|
98
89
|
readonly output?: unknown;
|
|
99
90
|
}): Promise<void>;
|
|
100
|
-
export declare function settleClaim(client: BoardClient, claim: BoardClaim, settlement: BoardSettlement): Promise<{
|
|
101
|
-
readonly problem?: string;
|
|
102
|
-
}>;
|
|
103
91
|
export declare function splitBoardText(text: string, limit?: number): string[];
|
|
@@ -8,25 +8,15 @@ import { lookupBoard, resolveBoards, resolveSessionBoard, selectBoard } from "./
|
|
|
8
8
|
import { BOARD_TEXT_LIMIT, boardAllows, boardErrorCode, boardPostId } from "./client.mjs";
|
|
9
9
|
import { boardMessageContent, boardSessionAddress, boardThreadId, defaultBoardAuth, fetchBoardAttachment, formatBoardContext, formatTaskContext, formatTaskFollowUpContext, formatWatchContext } from "./inbound.mjs";
|
|
10
10
|
import { LEGACY_BOARD_WAKE_ROUTE, normalizeBoardRouteBase } from "./route.mjs";
|
|
11
|
+
import { CLAIM_LEASE_SECONDS, CLAIM_WATCH_LABEL, endClaimRenewal, keepClaimInSlot, renewClaimBeforeTask, renewClaimDuringCall, settleClaim, settlementOf, stopClaimRenewal } from "./claims.mjs";
|
|
11
12
|
import { BoardWatchConfigError, boardClients, checkQueueScope, coversChannel, findWatchRecord } from "./watches.mjs";
|
|
12
13
|
import { POST, SessionStrandedError, defineChannel } from "eve/channels";
|
|
13
14
|
import { call } from "eve/events";
|
|
14
15
|
import { createLogger, postedIn, renderTextInteraction, surfaceEvents } from "eve/internal/channels";
|
|
15
16
|
const log = createLogger("board.channel");
|
|
16
|
-
const CLAIM_LEASE_SECONDS = 3600;
|
|
17
|
-
const RENEW_EVERY_MS = 3e4;
|
|
18
|
-
const RECENT_RENEWAL_MS = 6e4;
|
|
19
|
-
const SETTLE_ATTEMPTS = 3;
|
|
20
|
-
const SETTLE_BACKOFF_MS = 500;
|
|
21
17
|
const MAX_PULLS_PER_WAKE = 20;
|
|
22
18
|
const MATCHES_PER_PULL = 10;
|
|
23
19
|
const DEFAULT_MAX_UPLOAD_BYTES = 26214400;
|
|
24
|
-
const SETTLEMENTS = new Set([
|
|
25
|
-
"completed",
|
|
26
|
-
"failed",
|
|
27
|
-
"blocked"
|
|
28
|
-
]);
|
|
29
|
-
const CLAIM_WATCH_LABEL = "eve-claim-watch";
|
|
30
20
|
const CLAIM_EVENTS = new Set([
|
|
31
21
|
"task.assigned",
|
|
32
22
|
"task.created",
|
|
@@ -309,7 +299,7 @@ async function dispatch(wake, match, record) {
|
|
|
309
299
|
const root = (await deployment.thread(posted.re)).message;
|
|
310
300
|
const held = root.task?.claim;
|
|
311
301
|
if (held?.actor !== self.actor || Date.parse(held.until) <= Date.now()) return false;
|
|
312
|
-
if (root.labels[
|
|
302
|
+
if (root.labels["eve-claim-watch"] !== record.id) return false;
|
|
313
303
|
await start(posted, formatTaskFollowUpContext(posted, boardKey), `${idempotencyPrefix}:${match.id}`);
|
|
314
304
|
return true;
|
|
315
305
|
}
|
|
@@ -348,89 +338,25 @@ async function sendRecovering(source, message, options) {
|
|
|
348
338
|
return await source.send(message, options);
|
|
349
339
|
}
|
|
350
340
|
}
|
|
351
|
-
function
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
return Date.parse(claim.until) - now < CLAIM_LEASE_SECONDS * 1e3 / 3;
|
|
357
|
-
}
|
|
358
|
-
function renewedRecently(claim, now = Date.now()) {
|
|
359
|
-
return Date.parse(claim.until) - now > CLAIM_LEASE_SECONDS * 1e3 - RECENT_RENEWAL_MS;
|
|
360
|
-
}
|
|
361
|
-
async function renewClaim(client, claim) {
|
|
362
|
-
try {
|
|
363
|
-
return {
|
|
364
|
-
...claim,
|
|
365
|
-
until: await client.held(claim).renew(CLAIM_LEASE_SECONDS)
|
|
366
|
-
};
|
|
367
|
-
} catch (error) {
|
|
368
|
-
if (boardErrorCode(error) === "claim_lost") {
|
|
369
|
-
log.warn("board task claim was lost; the agent no longer holds the task", { task: claim.taskId });
|
|
370
|
-
return null;
|
|
341
|
+
function stateSlot(state) {
|
|
342
|
+
return {
|
|
343
|
+
get: () => state.task ?? null,
|
|
344
|
+
set: (claim) => {
|
|
345
|
+
state.task = claim;
|
|
371
346
|
}
|
|
372
|
-
logError("board task renewal failed", error, { task: claim.taskId });
|
|
373
|
-
return claim;
|
|
374
|
-
}
|
|
375
|
-
}
|
|
376
|
-
const renewals = new Map();
|
|
377
|
-
function renewDuringCall(client, state, callId) {
|
|
378
|
-
const claim = state.task;
|
|
379
|
-
if (claim == null) return;
|
|
380
|
-
const running = renewals.get(claim.token);
|
|
381
|
-
if (running !== void 0) {
|
|
382
|
-
running.calls.add(callId);
|
|
383
|
-
return;
|
|
384
|
-
}
|
|
385
|
-
const renewal = {
|
|
386
|
-
calls: new Set([callId]),
|
|
387
|
-
claim,
|
|
388
|
-
renewing: false
|
|
389
347
|
};
|
|
390
|
-
renewal.timer = setInterval(async () => {
|
|
391
|
-
if (renewal.renewing || renewal.claim === null || !claimDue(renewal.claim)) return;
|
|
392
|
-
renewal.renewing = true;
|
|
393
|
-
try {
|
|
394
|
-
renewal.claim = await renewClaim(client(), renewal.claim);
|
|
395
|
-
} finally {
|
|
396
|
-
renewal.renewing = false;
|
|
397
|
-
}
|
|
398
|
-
if (renewal.claim === null) clearInterval(renewal.timer);
|
|
399
|
-
}, RENEW_EVERY_MS);
|
|
400
|
-
renewal.timer.unref?.();
|
|
401
|
-
renewals.set(claim.token, renewal);
|
|
402
348
|
}
|
|
403
|
-
function
|
|
404
|
-
|
|
405
|
-
renewals.delete(token);
|
|
349
|
+
function renewDuringCall(client, state, callId) {
|
|
350
|
+
renewClaimDuringCall(client, state.task ?? null, callId);
|
|
406
351
|
}
|
|
407
352
|
function endRenewal(state, callId) {
|
|
408
|
-
|
|
409
|
-
const renewal = token === void 0 ? void 0 : renewals.get(token);
|
|
410
|
-
if (token === void 0 || renewal === void 0) return;
|
|
411
|
-
if (callId !== void 0) renewal.calls.delete(callId);
|
|
412
|
-
state.task = renewal.claim;
|
|
413
|
-
if (callId === void 0 || renewal.calls.size === 0 || renewal.claim === null) stopRenewal(token);
|
|
414
|
-
}
|
|
415
|
-
const SETTLE_TOOL = /^(?:.+__)?(?:complete|fail|block)_task$/u;
|
|
416
|
-
function settlementOf(output, taskId, capability) {
|
|
417
|
-
if (capability.kind !== "tool" || !SETTLE_TOOL.test(capability.name)) return void 0;
|
|
418
|
-
if (typeof output !== "object" || output === null) return void 0;
|
|
419
|
-
const settlement = output;
|
|
420
|
-
if (settlement.task !== taskId || !SETTLEMENTS.has(settlement.settled ?? "")) return void 0;
|
|
421
|
-
return settlement;
|
|
353
|
+
endClaimRenewal(stateSlot(state), callId);
|
|
422
354
|
}
|
|
423
355
|
async function keepBoardClaim(client, state) {
|
|
424
|
-
|
|
356
|
+
await keepClaimInSlot(client, stateSlot(state));
|
|
425
357
|
}
|
|
426
358
|
async function renewBeforeTask(client, state) {
|
|
427
|
-
|
|
428
|
-
}
|
|
429
|
-
async function renewBoardClaim(client, state) {
|
|
430
|
-
const claim = state.task;
|
|
431
|
-
if (claim == null) return;
|
|
432
|
-
state.task = await renewClaim(client, claim);
|
|
433
|
-
if (state.task === null) stopRenewal(claim.token);
|
|
359
|
+
await renewClaimBeforeTask(client, stateSlot(state));
|
|
434
360
|
}
|
|
435
361
|
async function settleAfterCall(client, channel, view, call$1) {
|
|
436
362
|
const { state } = channel;
|
|
@@ -443,32 +369,12 @@ async function settleAfterCall(client, channel, view, call$1) {
|
|
|
443
369
|
}
|
|
444
370
|
const { problem } = await settleClaim(client, claim, settlement);
|
|
445
371
|
state.task = null;
|
|
446
|
-
|
|
372
|
+
stopClaimRenewal(claim.token);
|
|
447
373
|
if (problem !== void 0) await bestEffort("note", async () => void await channel.board.reply({
|
|
448
374
|
noSync: true,
|
|
449
375
|
text: problem
|
|
450
376
|
}));
|
|
451
377
|
}
|
|
452
|
-
async function settleClaim(client, claim, settlement) {
|
|
453
|
-
const held = client.held(claim);
|
|
454
|
-
for (let attempt = 1;; attempt += 1) try {
|
|
455
|
-
if (settlement.settled === "completed") await held.complete(settlement.result);
|
|
456
|
-
else if (settlement.settled === "failed") await held.fail(settlement.reason ?? "Failed.");
|
|
457
|
-
else await held.block(settlement.reason ?? "Needs a person.", { kind: settlement.kind });
|
|
458
|
-
return {};
|
|
459
|
-
} catch (error) {
|
|
460
|
-
if (boardErrorCode(error) === "claim_lost") return { problem: `eve couldn't mark task #${claim.taskId} ${settlement.settled}: its claim ended before the agent settled it.` };
|
|
461
|
-
const id = logError("board task settlement failed", error, {
|
|
462
|
-
task: claim.taskId,
|
|
463
|
-
attempt
|
|
464
|
-
});
|
|
465
|
-
if (temporaryBoardFailure(error) && attempt < SETTLE_ATTEMPTS) {
|
|
466
|
-
await new Promise((resolve) => setTimeout(resolve, attempt * SETTLE_BACKOFF_MS));
|
|
467
|
-
continue;
|
|
468
|
-
}
|
|
469
|
-
return { problem: `eve couldn't mark task #${claim.taskId} ${settlement.settled} on the board (error id ${id}). Its claim runs out within the hour, and the task goes back to the queue.` };
|
|
470
|
-
}
|
|
471
|
-
}
|
|
472
378
|
function threadHandle(client, state) {
|
|
473
379
|
const threadId = state.threadId ?? "";
|
|
474
380
|
return {
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { HookDefinition } from "eve/hooks";
|
|
2
|
+
import { type BoardClaim, type BoardClient, type BoardSettings } from "./client.ts";
|
|
3
|
+
export declare const CLAIM_LEASE_SECONDS = 3600;
|
|
4
|
+
export declare const CLAIM_WATCH_LABEL = "eve-claim-watch";
|
|
5
|
+
export interface SessionBoardClaim {
|
|
6
|
+
readonly board: string;
|
|
7
|
+
readonly channel: string;
|
|
8
|
+
readonly claim: BoardClaim;
|
|
9
|
+
}
|
|
10
|
+
export declare function readSessionClaim(): SessionBoardClaim | null;
|
|
11
|
+
export declare function writeSessionClaim(claim: SessionBoardClaim): void;
|
|
12
|
+
export declare function clearSessionClaim(): void;
|
|
13
|
+
export interface ClaimSlot {
|
|
14
|
+
get(): BoardClaim | null;
|
|
15
|
+
set(claim: BoardClaim | null): void;
|
|
16
|
+
}
|
|
17
|
+
export interface BoardSettlement {
|
|
18
|
+
readonly settled: "completed" | "failed" | "blocked" | "released";
|
|
19
|
+
readonly task: string;
|
|
20
|
+
readonly result?: string;
|
|
21
|
+
readonly reason?: string;
|
|
22
|
+
readonly kind?: string;
|
|
23
|
+
}
|
|
24
|
+
export declare function settlementOf(output: unknown, taskId: string, capability: {
|
|
25
|
+
readonly kind: string;
|
|
26
|
+
readonly name: string;
|
|
27
|
+
}): BoardSettlement | undefined;
|
|
28
|
+
export declare function renewClaimDuringCall(client: () => BoardClient, claim: BoardClaim | null, callId: string): void;
|
|
29
|
+
export declare function stopClaimRenewal(token: string): void;
|
|
30
|
+
export declare function endClaimRenewal(slot: ClaimSlot, callId?: string): void;
|
|
31
|
+
export declare function keepClaimInSlot(client: BoardClient, slot: ClaimSlot): Promise<void>;
|
|
32
|
+
export declare function renewClaimBeforeTask(client: BoardClient, slot: ClaimSlot): Promise<void>;
|
|
33
|
+
export declare function settleClaim(client: BoardClient, claim: BoardClaim, settlement: BoardSettlement): Promise<{
|
|
34
|
+
readonly claimLost?: boolean;
|
|
35
|
+
readonly errorId?: string;
|
|
36
|
+
readonly problem?: string;
|
|
37
|
+
}>;
|
|
38
|
+
export declare function createBoardClaimsHook(settings: () => BoardSettings): HookDefinition;
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import { fileURLToPath as __eveFileURLToPath } from "node:url";
|
|
2
|
+
import { dirname as __eveDirname } from "node:path";
|
|
3
|
+
import { createRequire as __eveCreateRequire } from "node:module";
|
|
4
|
+
const __filename = __eveFileURLToPath(import.meta.url);
|
|
5
|
+
__eveDirname(__filename);
|
|
6
|
+
__eveCreateRequire(import.meta.url);
|
|
7
|
+
import { resolveSessionBoard } from "./boards.mjs";
|
|
8
|
+
import { boardErrorCode } from "./client.mjs";
|
|
9
|
+
import { boardClients } from "./watches.mjs";
|
|
10
|
+
import { createLogger } from "eve/internal/channels";
|
|
11
|
+
import { defineState } from "eve/context";
|
|
12
|
+
const log = createLogger("board.claims");
|
|
13
|
+
const CLAIM_LEASE_SECONDS = 3600;
|
|
14
|
+
const CLAIM_WATCH_LABEL = "eve-claim-watch";
|
|
15
|
+
const RENEW_EVERY_MS = 3e4;
|
|
16
|
+
const RECENT_RENEWAL_MS = 6e4;
|
|
17
|
+
const SETTLE_ATTEMPTS = 3;
|
|
18
|
+
const SETTLE_BACKOFF_MS = 500;
|
|
19
|
+
const SETTLEMENTS = new Set([
|
|
20
|
+
"completed",
|
|
21
|
+
"failed",
|
|
22
|
+
"blocked"
|
|
23
|
+
]);
|
|
24
|
+
const SETTLE_TOOL = /^(?:.+__)?(?:complete|fail|block)_task$/u;
|
|
25
|
+
const sessionClaimState = defineState("board.claim", () => null);
|
|
26
|
+
function readSessionClaim() {
|
|
27
|
+
return sessionClaimState.get();
|
|
28
|
+
}
|
|
29
|
+
function writeSessionClaim(claim) {
|
|
30
|
+
sessionClaimState.update(() => claim);
|
|
31
|
+
}
|
|
32
|
+
function clearSessionClaim() {
|
|
33
|
+
const existing = sessionClaimState.get();
|
|
34
|
+
if (existing !== null) stopClaimRenewal(existing.claim.token);
|
|
35
|
+
sessionClaimState.update(() => null);
|
|
36
|
+
}
|
|
37
|
+
function settlementOf(output, taskId, capability) {
|
|
38
|
+
if (capability.kind !== "tool" || !SETTLE_TOOL.test(capability.name)) return void 0;
|
|
39
|
+
if (typeof output !== "object" || output === null) return void 0;
|
|
40
|
+
const settlement = output;
|
|
41
|
+
if (settlement.task !== taskId || !SETTLEMENTS.has(settlement.settled ?? "")) return void 0;
|
|
42
|
+
return settlement;
|
|
43
|
+
}
|
|
44
|
+
function temporaryBoardFailure(error) {
|
|
45
|
+
const status = error?.status;
|
|
46
|
+
return typeof status !== "number" || status === 429 || status >= 500;
|
|
47
|
+
}
|
|
48
|
+
function claimDue(claim, now = Date.now()) {
|
|
49
|
+
return Date.parse(claim.until) - now < CLAIM_LEASE_SECONDS * 1e3 / 3;
|
|
50
|
+
}
|
|
51
|
+
function renewedRecently(claim, now = Date.now()) {
|
|
52
|
+
return Date.parse(claim.until) - now > CLAIM_LEASE_SECONDS * 1e3 - RECENT_RENEWAL_MS;
|
|
53
|
+
}
|
|
54
|
+
async function renewClaim(client, claim) {
|
|
55
|
+
try {
|
|
56
|
+
return {
|
|
57
|
+
...claim,
|
|
58
|
+
until: await client.held(claim).renew(CLAIM_LEASE_SECONDS)
|
|
59
|
+
};
|
|
60
|
+
} catch (error) {
|
|
61
|
+
if (boardErrorCode(error) === "claim_lost") {
|
|
62
|
+
log.warn("board task claim was lost; the agent no longer holds the task", { task: claim.taskId });
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
logError("board task renewal failed", error, { task: claim.taskId });
|
|
66
|
+
return claim;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
const renewals = new Map();
|
|
70
|
+
function renewClaimDuringCall(client, claim, callId) {
|
|
71
|
+
if (claim == null) return;
|
|
72
|
+
const running = renewals.get(claim.token);
|
|
73
|
+
if (running !== void 0) {
|
|
74
|
+
running.calls.add(callId);
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
const renewal = {
|
|
78
|
+
calls: new Set([callId]),
|
|
79
|
+
claim,
|
|
80
|
+
renewing: false
|
|
81
|
+
};
|
|
82
|
+
renewal.timer = setInterval(async () => {
|
|
83
|
+
if (renewal.renewing || renewal.claim === null || !claimDue(renewal.claim)) return;
|
|
84
|
+
renewal.renewing = true;
|
|
85
|
+
try {
|
|
86
|
+
let resolved;
|
|
87
|
+
try {
|
|
88
|
+
resolved = client();
|
|
89
|
+
} catch (error) {
|
|
90
|
+
log.warn("board task claim's board is no longer configured; dropping renewal", {
|
|
91
|
+
error,
|
|
92
|
+
task: renewal.claim.taskId
|
|
93
|
+
});
|
|
94
|
+
renewal.claim = null;
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
renewal.claim = await renewClaim(resolved, renewal.claim);
|
|
98
|
+
} finally {
|
|
99
|
+
renewal.renewing = false;
|
|
100
|
+
}
|
|
101
|
+
if (renewal.claim === null) clearInterval(renewal.timer);
|
|
102
|
+
}, RENEW_EVERY_MS);
|
|
103
|
+
renewal.timer.unref?.();
|
|
104
|
+
renewals.set(claim.token, renewal);
|
|
105
|
+
}
|
|
106
|
+
function stopClaimRenewal(token) {
|
|
107
|
+
clearInterval(renewals.get(token)?.timer);
|
|
108
|
+
renewals.delete(token);
|
|
109
|
+
}
|
|
110
|
+
function endClaimRenewal(slot, callId) {
|
|
111
|
+
const token = slot.get()?.token;
|
|
112
|
+
const renewal = token === void 0 ? void 0 : renewals.get(token);
|
|
113
|
+
if (token === void 0 || renewal === void 0) return;
|
|
114
|
+
if (callId !== void 0) renewal.calls.delete(callId);
|
|
115
|
+
slot.set(renewal.claim);
|
|
116
|
+
if (callId === void 0 || renewal.calls.size === 0 || renewal.claim === null) stopClaimRenewal(token);
|
|
117
|
+
}
|
|
118
|
+
async function keepClaimInSlot(client, slot) {
|
|
119
|
+
const claim = slot.get();
|
|
120
|
+
if (claim != null && claimDue(claim)) await renewSlot(client, slot);
|
|
121
|
+
}
|
|
122
|
+
async function renewClaimBeforeTask(client, slot) {
|
|
123
|
+
const claim = slot.get();
|
|
124
|
+
if (claim != null && !renewedRecently(claim)) await renewSlot(client, slot);
|
|
125
|
+
}
|
|
126
|
+
async function renewSlot(client, slot) {
|
|
127
|
+
const claim = slot.get();
|
|
128
|
+
if (claim == null) return;
|
|
129
|
+
const renewed = await renewClaim(client, claim);
|
|
130
|
+
slot.set(renewed);
|
|
131
|
+
if (renewed === null) stopClaimRenewal(claim.token);
|
|
132
|
+
}
|
|
133
|
+
async function settleClaim(client, claim, settlement) {
|
|
134
|
+
const held = client.held(claim);
|
|
135
|
+
const verb = settlement.settled === "released" ? `release task #${claim.taskId}` : `mark task #${claim.taskId} ${settlement.settled}`;
|
|
136
|
+
for (let attempt = 1;; attempt += 1) try {
|
|
137
|
+
if (settlement.settled === "completed") await held.complete(settlement.result);
|
|
138
|
+
else if (settlement.settled === "failed") await held.fail(settlement.reason ?? "Failed.");
|
|
139
|
+
else if (settlement.settled === "released") await held.release();
|
|
140
|
+
else await held.block(settlement.reason ?? "Needs a person.", { kind: settlement.kind });
|
|
141
|
+
return {};
|
|
142
|
+
} catch (error) {
|
|
143
|
+
if (boardErrorCode(error) === "claim_lost") return {
|
|
144
|
+
claimLost: true,
|
|
145
|
+
problem: `eve couldn't ${verb}: its claim ended before the agent settled it.`
|
|
146
|
+
};
|
|
147
|
+
const id = logError("board task settlement failed", error, {
|
|
148
|
+
attempt,
|
|
149
|
+
task: claim.taskId
|
|
150
|
+
});
|
|
151
|
+
if (temporaryBoardFailure(error) && attempt < SETTLE_ATTEMPTS) {
|
|
152
|
+
await new Promise((resolve) => setTimeout(resolve, attempt * SETTLE_BACKOFF_MS));
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
return {
|
|
156
|
+
errorId: id,
|
|
157
|
+
problem: `eve couldn't ${verb} on the board (error id ${id}). Its claim runs out within the hour, and the task goes back to the queue.`
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
function sessionSlot() {
|
|
162
|
+
return {
|
|
163
|
+
get: () => sessionClaimState.get()?.claim ?? null,
|
|
164
|
+
set: (claim) => {
|
|
165
|
+
sessionClaimState.update((current) => current === null || claim === null ? null : {
|
|
166
|
+
...current,
|
|
167
|
+
claim
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
function createBoardClaimsHook(settings) {
|
|
173
|
+
const resolveClient = (boardKey) => {
|
|
174
|
+
try {
|
|
175
|
+
return boardClients(resolveSessionBoard(settings(), boardKey)).deployment;
|
|
176
|
+
} catch (error) {
|
|
177
|
+
log.warn("board task claim's board is no longer configured; clearing the session claim", {
|
|
178
|
+
board: boardKey,
|
|
179
|
+
error
|
|
180
|
+
});
|
|
181
|
+
clearSessionClaim();
|
|
182
|
+
return null;
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
return { events: {
|
|
186
|
+
async "turn.started"() {
|
|
187
|
+
const held = readSessionClaim();
|
|
188
|
+
if (held === null) return;
|
|
189
|
+
const client = resolveClient(held.board);
|
|
190
|
+
if (client !== null) await keepClaimInSlot(client, sessionSlot());
|
|
191
|
+
},
|
|
192
|
+
async "call.requested"() {
|
|
193
|
+
const held = readSessionClaim();
|
|
194
|
+
if (held === null) return;
|
|
195
|
+
const client = resolveClient(held.board);
|
|
196
|
+
if (client !== null) await keepClaimInSlot(client, sessionSlot());
|
|
197
|
+
},
|
|
198
|
+
async "call.started"(event) {
|
|
199
|
+
const held = readSessionClaim();
|
|
200
|
+
if (held === null) return;
|
|
201
|
+
const boardKey = held.board;
|
|
202
|
+
if (event.data.taskId === void 0) {
|
|
203
|
+
if (resolveClient(boardKey) === null) return;
|
|
204
|
+
renewClaimDuringCall(() => boardClients(resolveSessionBoard(settings(), boardKey)).deployment, held.claim, event.data.callId);
|
|
205
|
+
} else {
|
|
206
|
+
const client = resolveClient(boardKey);
|
|
207
|
+
if (client !== null) await renewClaimBeforeTask(client, sessionSlot());
|
|
208
|
+
}
|
|
209
|
+
},
|
|
210
|
+
async "call.settled"(event) {
|
|
211
|
+
endClaimRenewal(sessionSlot(), event.data.callId);
|
|
212
|
+
const held = readSessionClaim();
|
|
213
|
+
if (held === null) return;
|
|
214
|
+
const client = resolveClient(held.board);
|
|
215
|
+
if (client !== null) await keepClaimInSlot(client, sessionSlot());
|
|
216
|
+
},
|
|
217
|
+
"turn.settled"() {
|
|
218
|
+
endClaimRenewal(sessionSlot());
|
|
219
|
+
}
|
|
220
|
+
} };
|
|
221
|
+
}
|
|
222
|
+
function logError(message, error, fields = {}) {
|
|
223
|
+
const errorId = crypto.randomUUID();
|
|
224
|
+
log.error(message, {
|
|
225
|
+
...fields,
|
|
226
|
+
error,
|
|
227
|
+
errorId
|
|
228
|
+
});
|
|
229
|
+
return errorId;
|
|
230
|
+
}
|
|
231
|
+
export { CLAIM_LEASE_SECONDS, CLAIM_WATCH_LABEL, clearSessionClaim, createBoardClaimsHook, endClaimRenewal, keepClaimInSlot, readSessionClaim, renewClaimBeforeTask, renewClaimDuringCall, settleClaim, settlementOf, stopClaimRenewal, writeSessionClaim };
|
|
@@ -18,6 +18,8 @@ export interface BoardAttachment {
|
|
|
18
18
|
export interface BoardTaskState {
|
|
19
19
|
readonly state: string;
|
|
20
20
|
readonly owner: string;
|
|
21
|
+
readonly for: string | null;
|
|
22
|
+
readonly needs: readonly string[];
|
|
21
23
|
readonly claim: {
|
|
22
24
|
readonly actor: string;
|
|
23
25
|
readonly until: string;
|
|
@@ -133,6 +135,22 @@ export interface BoardReadyTaskQuery {
|
|
|
133
135
|
readonly from?: readonly string[];
|
|
134
136
|
readonly lease?: number;
|
|
135
137
|
}
|
|
138
|
+
export declare const BOARD_TASK_STATES: readonly ["waiting", "ready", "claimed", "blocked", "completed", "failed", "canceled"];
|
|
139
|
+
export type BoardTaskStateName = (typeof BOARD_TASK_STATES)[number];
|
|
140
|
+
export interface BoardTasksQuery {
|
|
141
|
+
readonly channel?: string;
|
|
142
|
+
readonly state?: "open" | "all" | BoardTaskStateName;
|
|
143
|
+
readonly label?: readonly string[];
|
|
144
|
+
readonly owner?: string;
|
|
145
|
+
readonly for?: string;
|
|
146
|
+
readonly needs?: string;
|
|
147
|
+
readonly after?: string;
|
|
148
|
+
readonly limit?: number;
|
|
149
|
+
}
|
|
150
|
+
export interface BoardTasksPage {
|
|
151
|
+
readonly tasks: readonly BoardThreadSummary[];
|
|
152
|
+
readonly cursor: string;
|
|
153
|
+
}
|
|
136
154
|
export interface BoardNewTask {
|
|
137
155
|
readonly text: string;
|
|
138
156
|
readonly labels?: Readonly<Record<string, string>>;
|
|
@@ -212,6 +230,7 @@ export interface BoardClient {
|
|
|
212
230
|
typing(id: string, status?: string): Promise<void>;
|
|
213
231
|
thread(id: string, range?: {
|
|
214
232
|
readonly after?: string;
|
|
233
|
+
readonly limit?: number;
|
|
215
234
|
}): Promise<BoardThread>;
|
|
216
235
|
posted(id: string): Promise<BoardMessage>;
|
|
217
236
|
linked(url: string): Promise<BoardThread>;
|
|
@@ -233,6 +252,7 @@ export interface BoardClient {
|
|
|
233
252
|
cancelSchedule(id: string): Promise<{
|
|
234
253
|
readonly canceled: boolean;
|
|
235
254
|
}>;
|
|
255
|
+
tasks(query?: BoardTasksQuery): Promise<BoardTasksPage>;
|
|
236
256
|
claimReady(query: BoardReadyTaskQuery): Promise<BoardClaimedTask | null>;
|
|
237
257
|
claim(taskId: string, lease?: number): Promise<BoardClaimedTask>;
|
|
238
258
|
held(claim: BoardClaim): BoardHeldTask;
|
|
@@ -7,6 +7,15 @@ __eveCreateRequire(import.meta.url);
|
|
|
7
7
|
import { createHash } from "node:crypto";
|
|
8
8
|
import { board } from "@vercel/board";
|
|
9
9
|
import { compileAccess } from "@vercel/board/access";
|
|
10
|
+
const BOARD_TASK_STATES = [
|
|
11
|
+
"waiting",
|
|
12
|
+
"ready",
|
|
13
|
+
"claimed",
|
|
14
|
+
"blocked",
|
|
15
|
+
"completed",
|
|
16
|
+
"failed",
|
|
17
|
+
"canceled"
|
|
18
|
+
];
|
|
10
19
|
const BOARD_PERMISSIONS = [
|
|
11
20
|
"read",
|
|
12
21
|
"post",
|
|
@@ -168,7 +177,10 @@ function createBoardClient(board, can) {
|
|
|
168
177
|
async typing(id, status) {
|
|
169
178
|
await handle.typing(id, status === void 0 ? void 0 : status.slice(0, 50));
|
|
170
179
|
},
|
|
171
|
-
thread: async (id, range = {}) => threadOf(await handle.thread(id,
|
|
180
|
+
thread: async (id, range = {}) => threadOf(await handle.thread(id, {
|
|
181
|
+
...range.after !== void 0 && { after: range.after },
|
|
182
|
+
...range.limit !== void 0 && { limit: range.limit }
|
|
183
|
+
})),
|
|
172
184
|
posted: async (id) => messageOf(await handle.posted(id)),
|
|
173
185
|
linked: async (url) => threadOf(await handle.linked(url)),
|
|
174
186
|
async threads(query = {}) {
|
|
@@ -203,6 +215,20 @@ function createBoardClient(board, can) {
|
|
|
203
215
|
const { canceled } = await handle.cancelSchedule(id);
|
|
204
216
|
return { canceled };
|
|
205
217
|
},
|
|
218
|
+
async tasks(query = {}) {
|
|
219
|
+
const page = await handle.tasks.list({
|
|
220
|
+
...query,
|
|
221
|
+
...query.label !== void 0 && { label: [...query.label] }
|
|
222
|
+
});
|
|
223
|
+
return {
|
|
224
|
+
cursor: page.cursor,
|
|
225
|
+
tasks: page.tasks.map((summary) => ({
|
|
226
|
+
last: summary.last === null ? null : messageOf(summary.last),
|
|
227
|
+
message: messageOf(summary.message),
|
|
228
|
+
replies: summary.replies
|
|
229
|
+
}))
|
|
230
|
+
};
|
|
231
|
+
},
|
|
206
232
|
async claimReady(query) {
|
|
207
233
|
const claim = await handle.tasks.claim({
|
|
208
234
|
channel: query.channel,
|
|
@@ -303,6 +329,8 @@ function messageOf(view) {
|
|
|
303
329
|
actor: task.claim.actor,
|
|
304
330
|
until: task.claim.until
|
|
305
331
|
},
|
|
332
|
+
for: task.for,
|
|
333
|
+
needs: [...task.needs],
|
|
306
334
|
owner: task.owner,
|
|
307
335
|
reason: task.reason,
|
|
308
336
|
result: task.result,
|
|
@@ -361,4 +389,4 @@ function scheduleOf(schedule) {
|
|
|
361
389
|
text: schedule.text
|
|
362
390
|
};
|
|
363
391
|
}
|
|
364
|
-
export { BOARD_CHANNEL_SELECTOR, BOARD_HOLD_KIND_PATTERN, BOARD_NAME_PATTERN, BOARD_PERMISSIONS, BOARD_TAIL_LIMIT, BOARD_TASK_NOTE_LIMIT, BOARD_TEXT_LIMIT, BOARD_UPLOAD_LIMIT, DEFAULT_BOARD_PROFILE, boardAllows, boardErrorCode, boardPostId, createBoardClient, withHints };
|
|
392
|
+
export { BOARD_CHANNEL_SELECTOR, BOARD_HOLD_KIND_PATTERN, BOARD_NAME_PATTERN, BOARD_PERMISSIONS, BOARD_TAIL_LIMIT, BOARD_TASK_NOTE_LIMIT, BOARD_TASK_STATES, BOARD_TEXT_LIMIT, BOARD_UPLOAD_LIMIT, DEFAULT_BOARD_PROFILE, boardAllows, boardErrorCode, boardPostId, createBoardClient, withHints };
|