@phren/cli 0.3.54 → 0.3.56
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/bridge/agent-hook-callback.js +1 -0
- package/dist/bridge/agent-hooks.js +1 -0
- package/dist/bridge/dispatch-returns.d.ts +4 -0
- package/dist/bridge/dispatch-returns.js +10 -3
- package/dist/bridge/git.js +82 -8
- package/dist/bridge/hand-off-queue.d.ts +21 -0
- package/dist/bridge/hand-off-queue.js +31 -2
- package/dist/bridge/projects.js +13 -8
- package/dist/bridge/prompt-deliveries.d.ts +12 -0
- package/dist/bridge/prompt-deliveries.js +74 -21
- package/dist/bridge/server-pane-routes.d.ts +4 -0
- package/dist/bridge/server-pane-routes.js +20 -4
- package/dist/bridge/server.js +14 -13
- package/dist/bridge/session-activity.d.ts +28 -0
- package/dist/bridge/session-activity.js +30 -0
- package/dist/bridge/socket-budget.d.ts +29 -0
- package/dist/bridge/socket-budget.js +62 -0
- package/dist/bridge/turn-records.d.ts +2 -0
- package/dist/bridge/turn-records.js +6 -1
- package/dist/bridge-hook.mjs +2363 -2150
- package/package.json +1 -1
|
@@ -15,6 +15,7 @@ import { CodexServerUnavailable, codexServers } from "./codex-servers.js";
|
|
|
15
15
|
import { refuseWorkingSlash } from "./model-switch.js";
|
|
16
16
|
import { PERMISSION_MODE_VALUES, refuseAgentOrigin } from "./permission-mode.js";
|
|
17
17
|
import { sessionWebServers } from "./session-servers.js";
|
|
18
|
+
import { takesPrompt } from "./session-activity.js";
|
|
18
19
|
import { gitRoot, repositoryDiff, withGitReadDeadline } from "./projects.js";
|
|
19
20
|
import { BridgeError, MAX_FRAME, object, objects, startingTargetSchema, targetSchema } from "./protocol.js";
|
|
20
21
|
import { childAgent, childAgentTree, conversationNamedPaths, targetTranscriptPath } from "./transcripts.js";
|
|
@@ -119,6 +120,14 @@ function claudeQuestionAnswer(data) {
|
|
|
119
120
|
}
|
|
120
121
|
/** A secret typed into a terminal prompt: printable, bounded, never logged. */
|
|
121
122
|
const secretText = z.string().min(1).max(256).refine(t => !/[\x00-\x1f\x7f]/.test(t));
|
|
123
|
+
/** What `takesPrompt` checks before typing into a Claude pane Herdr reads working: nothing structured
|
|
124
|
+
* pending there and no dialog on its screen. */
|
|
125
|
+
export function promptGuards(agentHooks, target) {
|
|
126
|
+
return { held: () => !!(agentHooks.approval(target) || agentHooks.terminalPrompt(target) || agentHooks.servedQuestion(target)),
|
|
127
|
+
screen: () => terminalProvider().readScreen(target.server, target.pane, { scope: "pane", source: "visible", lines: 40, format: "ansi", timeoutMs: 2_000 }) };
|
|
128
|
+
}
|
|
129
|
+
/** Herdr answered the request with a refusal (a 409 carrying its own error code): it performed nothing. */
|
|
130
|
+
const herdrRefused = (error) => error instanceof BridgeError && error.status === 409 && typeof error.details?.herdrCode === "string";
|
|
122
131
|
/** An idle agent that has not taken typed text within the first wait
|
|
123
132
|
* likely lost the Enter while it redrew (an update notice, a resize): the
|
|
124
133
|
* text sits in its input line. Press Enter once more and wait again. A
|
|
@@ -131,8 +140,10 @@ async function resubmitIfIdle(agentHooks, target, text) {
|
|
|
131
140
|
catch {
|
|
132
141
|
return "unknown";
|
|
133
142
|
} };
|
|
143
|
+
// A pane that turned working started a turn: its UserPromptSubmit is on its way (the agent runs its
|
|
144
|
+
// other hooks too). Wait for it rather than answering "queued" for a message the agent took.
|
|
134
145
|
if (await status() === "working")
|
|
135
|
-
return
|
|
146
|
+
return await agentHooks.deliveries.awaitLate(target, text);
|
|
136
147
|
const late = agentHooks.deliveries.awaitLate(target, text);
|
|
137
148
|
await terminalProvider().sendKeys(target.server, target.pane, ["enter"]);
|
|
138
149
|
const outcome = await late;
|
|
@@ -388,7 +399,8 @@ export async function paneRouteOnce(ctx, url, data, response, typing) {
|
|
|
388
399
|
// is pending there: an approval the Hook holds or saw, or a
|
|
389
400
|
// status Herdr cannot read. Otherwise the answer keys are the way.
|
|
390
401
|
const status = String(pane.agent_status);
|
|
391
|
-
|
|
402
|
+
// A Claude lead waiting on its background agents takes a prompt though Herdr reads it working (`takesPrompt`).
|
|
403
|
+
if (data.hookQueued === true && !await takesPrompt(target.server, pane, target.session, promptGuards(agentHooks, target)))
|
|
392
404
|
throw new BridgeError(409, "The worker is busy; the hand-off remains queued.", { code: "hand-off-busy" });
|
|
393
405
|
if (status === "unknown" || (["blocked", "waiting"].includes(status) && (agentHooks.approval(target) || agentHooks.terminalPrompt(target) || agentHooks.servedQuestion(target)))) {
|
|
394
406
|
throw new BridgeError(409, "This agent needs input in the terminal first.");
|
|
@@ -462,12 +474,16 @@ export async function paneRouteOnce(ctx, url, data, response, typing) {
|
|
|
462
474
|
let outcome = await promptWithStartupRetry(async () => {
|
|
463
475
|
const refused = new AbortController();
|
|
464
476
|
const expected = agentHooks.deliveries.expect(target, text, busy && target.source !== "claude" ? 300 : 1_500, refused.signal, deliveryId, typeof pane.terminal_id === "string" ? pane.terminal_id : undefined);
|
|
465
|
-
|
|
477
|
+
let typed = false;
|
|
466
478
|
try {
|
|
479
|
+
await typing();
|
|
480
|
+
typed = true;
|
|
467
481
|
await terminalProvider().prompt(target.server, target.pane, text);
|
|
468
482
|
}
|
|
469
483
|
catch (error) {
|
|
470
|
-
|
|
484
|
+
// Nothing reached the pane: the reservation failed first, the agent was not ready, or Herdr refused the
|
|
485
|
+
// request (its `agent.prompt` is one call). The record goes, or it would hold the pane's hand-offs.
|
|
486
|
+
if (!typed || agentNotReady(error) || herdrRefused(error))
|
|
471
487
|
refused.abort();
|
|
472
488
|
throw error;
|
|
473
489
|
}
|
package/dist/bridge/server.js
CHANGED
|
@@ -12,7 +12,7 @@ import { hookPeers, peerRequest } from "./peers.js";
|
|
|
12
12
|
import { handOff } from "./hand-off.js";
|
|
13
13
|
import { sessionStalls } from "./stalls.js";
|
|
14
14
|
import { HandOffQueue } from "./hand-off-queue.js";
|
|
15
|
-
import { paneRouteOnce } from "./server-pane-routes.js";
|
|
15
|
+
import { paneRouteOnce, promptGuards } from "./server-pane-routes.js";
|
|
16
16
|
import { FanoutMessages } from "./fanout-messages.js";
|
|
17
17
|
import { activateModules as moduleSnapshot } from "../modules/runtime.js";
|
|
18
18
|
import { disabledHint } from "../modules/registry.js";
|
|
@@ -60,6 +60,7 @@ import { SessionMover, transcriptTail } from "./session-move.js";
|
|
|
60
60
|
import { harnessInventoryWithin, launchCheckOff } from "./harnesses.js";
|
|
61
61
|
import { paneAccount, paneAccountKey } from "./pane-accounts.js";
|
|
62
62
|
import { readTurn } from "./turn-records.js";
|
|
63
|
+
import { takesPrompt } from "./session-activity.js";
|
|
63
64
|
import { readLaunchBrief } from "./launch-brief.js";
|
|
64
65
|
import { conductorPane, readRoleState } from "./conductor-role.js";
|
|
65
66
|
import { terminalKind, terminalProvider } from "./terminal.js";
|
|
@@ -68,6 +69,7 @@ import { codexAppServerEnabled, codexServers } from "./codex-servers.js";
|
|
|
68
69
|
import { CodexAuthKeeper, codexAuthRefreshEnabled } from "./codex-auth-refresh.js";
|
|
69
70
|
import { localNames } from "./computer-names.js";
|
|
70
71
|
import { startStoreSyncLoop } from "../sync/store-sync.js";
|
|
72
|
+
import { SocketBudget, socketIdentity, socketKind } from "./socket-budget.js";
|
|
71
73
|
export { capabilities, capabilitiesForModules, requireRoute } from "./server-routes.js";
|
|
72
74
|
export { streamCloseReason } from "./server-stream.js";
|
|
73
75
|
export { herdrAgentName, launchSession } from "./server-launch.js";
|
|
@@ -107,6 +109,7 @@ export async function serve(version, options = {}) {
|
|
|
107
109
|
// Follows what dispatched workers do and tells the dispatching agent.
|
|
108
110
|
let succession;
|
|
109
111
|
const returns = dispatches ? new DispatchReturns({
|
|
112
|
+
takesPrompt: (target, pane) => takesPrompt(target.server, pane, target.session, promptGuards(agentHooks, target)),
|
|
110
113
|
localComputer: { id: computerID, name: hostname() },
|
|
111
114
|
forward: (origin, text, deliveryId) => succession?.forward(origin, text, deliveryId) ?? Promise.resolve(undefined),
|
|
112
115
|
// Continue on another signed-in account when a Claude worker stops at its usage limit: opt-in, `phren config account-failover on`.
|
|
@@ -154,6 +157,10 @@ export async function serve(version, options = {}) {
|
|
|
154
157
|
},
|
|
155
158
|
notify: (target, text, deliveryId, computer) => handOff({ computer, target, text, deliveryId }, { notifySender: false, bypassConductor: true }),
|
|
156
159
|
send: sendPrompt,
|
|
160
|
+
delivery: (target, deliveryId) => agentHooks.deliveries.status(deliveryId, target),
|
|
161
|
+
inFlight: target => agentHooks.deliveries.holding(target),
|
|
162
|
+
held: (target, deliveryId) => agentHooks.deliveries.held(deliveryId, target),
|
|
163
|
+
ready: (target, pane) => takesPrompt(target.server, pane, target.session, promptGuards(agentHooks, target)),
|
|
157
164
|
}) : undefined;
|
|
158
165
|
// Moving a session to another agent (session-move.ts): its hand-off request
|
|
159
166
|
// takes the hand_off path, and the new agent starts through the launch path.
|
|
@@ -268,7 +275,7 @@ export async function serve(version, options = {}) {
|
|
|
268
275
|
http.headersTimeout = 10_000;
|
|
269
276
|
http.maxHeadersCount = 32;
|
|
270
277
|
const ws = new WebSocketServer({ noServer: true, maxPayload: 65_536, perMessageDeflate: false });
|
|
271
|
-
const socketPools = new
|
|
278
|
+
const socketPools = new SocketBudget();
|
|
272
279
|
http.on("upgrade", (request, socket, head) => {
|
|
273
280
|
try {
|
|
274
281
|
const url = new URL(request.url || "/", "http://phren.local");
|
|
@@ -279,20 +286,14 @@ export async function serve(version, options = {}) {
|
|
|
279
286
|
requireRoute(modules, "WS", url.pathname);
|
|
280
287
|
// Parsed before the upgrade: an invalid server name is refused as the other routes refuse it.
|
|
281
288
|
const overviewServer = url.pathname === "/v1/overview" ? selectedServer(url) : undefined;
|
|
282
|
-
//
|
|
283
|
-
//
|
|
284
|
-
|
|
285
|
-
const pool = request.headers["x-phren-client"] === "desktop" ? "desktop" : "default";
|
|
289
|
+
// Sockets are budgeted per authenticated identity (socket-budget.ts); the
|
|
290
|
+
// x-phren-client header only picks a sub-pool inside that budget.
|
|
291
|
+
const identity = socketIdentity(request), kind = socketKind(request);
|
|
286
292
|
ws.handleUpgrade(request, socket, head, client => {
|
|
287
|
-
|
|
288
|
-
socketPools.
|
|
289
|
-
members.add(client);
|
|
290
|
-
client.once("close", () => members.delete(client));
|
|
291
|
-
while (members.size > 16) {
|
|
292
|
-
const oldest = members.values().next().value;
|
|
293
|
+
client.once("close", () => socketPools.remove(client));
|
|
294
|
+
for (const oldest of socketPools.add(client, identity, kind)) {
|
|
293
295
|
oldest.close(1008, "Too many connections; reconnect");
|
|
294
296
|
oldest.terminate();
|
|
295
|
-
members.delete(oldest);
|
|
296
297
|
ws.clients.delete(oldest);
|
|
297
298
|
}
|
|
298
299
|
if (overviewServer !== undefined) {
|
|
@@ -36,6 +36,34 @@ export declare const BACKGROUND_STALE_MS: number;
|
|
|
36
36
|
* the Stop's count less those `finishedTasks` (FinalTurn's) says finished
|
|
37
37
|
* after it. Undefined once none are left or the Stop is BACKGROUND_STALE_MS old. */
|
|
38
38
|
export declare function recordedBackground(record: TurnRecord | undefined, finishedTasks?: readonly string[], now?: number): number | undefined;
|
|
39
|
+
/** What may still be open in a pane whose turn record says it ended: Claude
|
|
40
|
+
* starts a turn on its own when a background agent finishes (its task
|
|
41
|
+
* notification, with no UserPromptSubmit), and that turn can open a dialog
|
|
42
|
+
* Herdr still reads as working (plan approval, an MCP elicitation, a
|
|
43
|
+
* permission request the Hook holds). Pasted text plus Enter would answer it. */
|
|
44
|
+
export interface PromptGuards {
|
|
45
|
+
/** An approval the Hook holds, a terminal prompt or a question it serves for this pane. */
|
|
46
|
+
held?: () => boolean;
|
|
47
|
+
/** The pane's visible screen; a dialog there (pane-status.ts `screenDialog`) or a failed read refuses. */
|
|
48
|
+
screen?: () => Promise<string>;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Whether the agent in a pane takes a typed prompt now: Herdr reads it idle or
|
|
52
|
+
* done, or it is a Claude session Herdr reads `working` whose own turn record
|
|
53
|
+
* ended with background tasks still running (`recordedBackground`). That is
|
|
54
|
+
* Claude's "Waiting for N background agents to finish": its turn is over and
|
|
55
|
+
* pasted text plus Enter submits at once and starts a turn (seen live, Claude
|
|
56
|
+
* Code 2.1.302), but Herdr keeps it `working` until the last background agent
|
|
57
|
+
* ends. Waiting for idle there held a hand-off or a dispatch-return notice for
|
|
58
|
+
* as long as a lead kept sub-agents running, over an hour. Another working
|
|
59
|
+
* pane, or one blocked, waiting or unknown, does not take one, nor one with a
|
|
60
|
+
* dialog open (`guards`). The record is read only for a working Claude pane,
|
|
61
|
+
* through the same guard the session rows use (`paneRecord`), so a leftover
|
|
62
|
+
* record of an earlier conversation never speaks for this one; once the prompt
|
|
63
|
+
* is submitted its UserPromptSubmit makes the turn working again. Past
|
|
64
|
+
* BACKGROUND_STALE_MS after the Stop the pane waits for idle again.
|
|
65
|
+
*/
|
|
66
|
+
export declare function takesPrompt(server: string, pane: Json, session: string | undefined, guards?: PromptGuards, now?: number): Promise<boolean>;
|
|
39
67
|
/** The background shells and monitors a turn still awaits, from its transcript
|
|
40
68
|
* final turn. A recorded ended turn bounds `FinalTurn.awaited` by the Stop's
|
|
41
69
|
* count less the tasks that finished since, and by the 2-hour cap; a Stop that
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { briefLabel } from "./launch-brief.js";
|
|
2
|
+
import { screenDialog } from "./pane-status.js";
|
|
2
3
|
import { provider } from "./protocol.js";
|
|
3
4
|
import { backgroundLeft, readTurn, turnPhase } from "./turn-records.js";
|
|
4
5
|
/**
|
|
@@ -49,6 +50,35 @@ export function recordedBackground(record, finishedTasks, now = Date.now()) {
|
|
|
49
50
|
return undefined;
|
|
50
51
|
return backgroundLeft(phase.background, phase.at, finishedTasks) || undefined;
|
|
51
52
|
}
|
|
53
|
+
/**
|
|
54
|
+
* Whether the agent in a pane takes a typed prompt now: Herdr reads it idle or
|
|
55
|
+
* done, or it is a Claude session Herdr reads `working` whose own turn record
|
|
56
|
+
* ended with background tasks still running (`recordedBackground`). That is
|
|
57
|
+
* Claude's "Waiting for N background agents to finish": its turn is over and
|
|
58
|
+
* pasted text plus Enter submits at once and starts a turn (seen live, Claude
|
|
59
|
+
* Code 2.1.302), but Herdr keeps it `working` until the last background agent
|
|
60
|
+
* ends. Waiting for idle there held a hand-off or a dispatch-return notice for
|
|
61
|
+
* as long as a lead kept sub-agents running, over an hour. Another working
|
|
62
|
+
* pane, or one blocked, waiting or unknown, does not take one, nor one with a
|
|
63
|
+
* dialog open (`guards`). The record is read only for a working Claude pane,
|
|
64
|
+
* through the same guard the session rows use (`paneRecord`), so a leftover
|
|
65
|
+
* record of an earlier conversation never speaks for this one; once the prompt
|
|
66
|
+
* is submitted its UserPromptSubmit makes the turn working again. Past
|
|
67
|
+
* BACKGROUND_STALE_MS after the Stop the pane waits for idle again.
|
|
68
|
+
*/
|
|
69
|
+
export async function takesPrompt(server, pane, session, guards = {}, now = Date.now()) {
|
|
70
|
+
const status = String(pane.agent_status);
|
|
71
|
+
if (status === "idle" || status === "done")
|
|
72
|
+
return true;
|
|
73
|
+
if (status !== "working" || pane.agent !== "claude")
|
|
74
|
+
return false;
|
|
75
|
+
if (!recordedBackground(await paneRecord(server, pane, session), [], now) || guards.held?.())
|
|
76
|
+
return false;
|
|
77
|
+
if (!guards.screen)
|
|
78
|
+
return true;
|
|
79
|
+
const screen = await guards.screen().catch(() => undefined);
|
|
80
|
+
return screen !== undefined && !screenDialog("claude", screen);
|
|
81
|
+
}
|
|
52
82
|
/** The background shells and monitors a turn still awaits, from its transcript
|
|
53
83
|
* final turn. A recorded ended turn bounds `FinalTurn.awaited` by the Stop's
|
|
54
84
|
* count less the tasks that finished since, and by the 2-hour cap; a Stop that
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { IncomingMessage } from "node:http";
|
|
2
|
+
/** Sockets one identity may hold per client kind, and in all. */
|
|
3
|
+
export declare const SOCKETS_PER_KIND = 16;
|
|
4
|
+
export declare const SOCKETS_PER_IDENTITY = 32;
|
|
5
|
+
/** Every identity together; the backstop if identities themselves multiply. */
|
|
6
|
+
export declare const SOCKETS_TOTAL = 128;
|
|
7
|
+
/**
|
|
8
|
+
* Who a WebSocket belongs to, from what the Hook itself trusts rather than a
|
|
9
|
+
* client hint: a peer key's gateway rebuilds its request with `x-phren-origin:
|
|
10
|
+
* peer` and the peer's fingerprint, so that is its own identity; everything
|
|
11
|
+
* else arrives on the owner's local socket or the owner's SSH pipe and is the
|
|
12
|
+
* owner. `x-phren-client` only picks a sub-pool inside the identity.
|
|
13
|
+
*/
|
|
14
|
+
export declare function socketIdentity(request: Pick<IncomingMessage, "headers">): string;
|
|
15
|
+
export declare const socketKind: (request: Pick<IncomingMessage, "headers">) => string;
|
|
16
|
+
/** Admits sockets oldest-first under the per-kind, per-identity and total caps. */
|
|
17
|
+
export declare class SocketBudget<S> {
|
|
18
|
+
private readonly limits;
|
|
19
|
+
private readonly entries;
|
|
20
|
+
constructor(limits?: {
|
|
21
|
+
kind: number;
|
|
22
|
+
identity: number;
|
|
23
|
+
total: number;
|
|
24
|
+
});
|
|
25
|
+
/** Register a socket; returns the sockets to close to stay inside the caps. */
|
|
26
|
+
add(socket: S, identity: string, kind: string): S[];
|
|
27
|
+
remove(socket: S): void;
|
|
28
|
+
count(identity?: string): number;
|
|
29
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { requestPeer } from "./request-origin.js";
|
|
2
|
+
/** Sockets one identity may hold per client kind, and in all. */
|
|
3
|
+
export const SOCKETS_PER_KIND = 16;
|
|
4
|
+
export const SOCKETS_PER_IDENTITY = 32;
|
|
5
|
+
/** Every identity together; the backstop if identities themselves multiply. */
|
|
6
|
+
export const SOCKETS_TOTAL = 128;
|
|
7
|
+
/**
|
|
8
|
+
* Who a WebSocket belongs to, from what the Hook itself trusts rather than a
|
|
9
|
+
* client hint: a peer key's gateway rebuilds its request with `x-phren-origin:
|
|
10
|
+
* peer` and the peer's fingerprint, so that is its own identity; everything
|
|
11
|
+
* else arrives on the owner's local socket or the owner's SSH pipe and is the
|
|
12
|
+
* owner. `x-phren-client` only picks a sub-pool inside the identity.
|
|
13
|
+
*/
|
|
14
|
+
export function socketIdentity(request) {
|
|
15
|
+
const peer = requestPeer(request);
|
|
16
|
+
return peer ? `peer:${peer.fingerprint}` : "owner";
|
|
17
|
+
}
|
|
18
|
+
export const socketKind = (request) => request.headers["x-phren-client"] === "desktop" ? "desktop" : "default";
|
|
19
|
+
/** Admits sockets oldest-first under the per-kind, per-identity and total caps. */
|
|
20
|
+
export class SocketBudget {
|
|
21
|
+
limits;
|
|
22
|
+
entries = new Set();
|
|
23
|
+
constructor(limits = { kind: SOCKETS_PER_KIND, identity: SOCKETS_PER_IDENTITY, total: SOCKETS_TOTAL }) {
|
|
24
|
+
this.limits = limits;
|
|
25
|
+
}
|
|
26
|
+
/** Register a socket; returns the sockets to close to stay inside the caps. */
|
|
27
|
+
add(socket, identity, kind) {
|
|
28
|
+
const entry = { socket, identity, kind };
|
|
29
|
+
this.entries.add(entry);
|
|
30
|
+
const evicted = [];
|
|
31
|
+
const drop = (match) => {
|
|
32
|
+
const oldest = [...this.entries].find(match);
|
|
33
|
+
if (!oldest)
|
|
34
|
+
return;
|
|
35
|
+
this.entries.delete(oldest);
|
|
36
|
+
evicted.push(oldest.socket);
|
|
37
|
+
};
|
|
38
|
+
while ([...this.entries].filter(e => e.identity === identity && e.kind === kind).length > this.limits.kind)
|
|
39
|
+
drop(e => e.identity === identity && e.kind === kind);
|
|
40
|
+
while ([...this.entries].filter(e => e.identity === identity).length > this.limits.identity)
|
|
41
|
+
drop(e => e.identity === identity);
|
|
42
|
+
while (this.entries.size > this.limits.total) {
|
|
43
|
+
const counts = new Map();
|
|
44
|
+
for (const e of this.entries)
|
|
45
|
+
counts.set(e.identity, (counts.get(e.identity) ?? 0) + 1);
|
|
46
|
+
// Ties go against a peer identity, so forged peer budgets never push out the owner's own devices first.
|
|
47
|
+
const [heaviest] = [...counts].sort((a, b) => b[1] - a[1] || Number(a[0] === "owner") - Number(b[0] === "owner"))[0];
|
|
48
|
+
drop(e => e.identity === heaviest);
|
|
49
|
+
}
|
|
50
|
+
return evicted;
|
|
51
|
+
}
|
|
52
|
+
remove(socket) {
|
|
53
|
+
for (const e of this.entries)
|
|
54
|
+
if (e.socket === socket) {
|
|
55
|
+
this.entries.delete(e);
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
count(identity) {
|
|
60
|
+
return identity === undefined ? this.entries.size : [...this.entries].filter(e => e.identity === identity).length;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
@@ -37,6 +37,8 @@ export interface TurnEvent {
|
|
|
37
37
|
session: string;
|
|
38
38
|
/** SessionStart / UserPromptSubmit: the dispatch id the worker's hook named. */
|
|
39
39
|
dispatch?: string;
|
|
40
|
+
/** SessionStart only: how the session started, as the harness names it (Claude: startup, resume, clear, compact). */
|
|
41
|
+
start?: string;
|
|
40
42
|
/** Stop only: the harness's count of in-flight background tasks, when it reports one. */
|
|
41
43
|
background?: number;
|
|
42
44
|
/** Stop only: the harness's last assistant message, when it reports one. */
|
|
@@ -67,8 +67,13 @@ export function nextTurn(previous, event) {
|
|
|
67
67
|
record.at = at;
|
|
68
68
|
if (event.dispatch && briefId.safeParse(event.dispatch).success)
|
|
69
69
|
record.dispatch ??= event.dispatch;
|
|
70
|
-
if (event.event === "SessionStart")
|
|
70
|
+
if (event.event === "SessionStart") {
|
|
71
71
|
record.startedAt ??= at;
|
|
72
|
+
// A resumed or cleared conversation in the same terminal no longer runs the background agents its
|
|
73
|
+
// last Stop counted; a compaction keeps them.
|
|
74
|
+
if ((event.start === "resume" || event.start === "clear") && record.stop?.background !== undefined)
|
|
75
|
+
delete record.stop.background;
|
|
76
|
+
}
|
|
72
77
|
else if (event.event === "UserPromptSubmit")
|
|
73
78
|
record.prompt = { seq: record.seq, at };
|
|
74
79
|
else {
|