@blackbelt-technology/pi-agent-dashboard 0.6.0 → 0.7.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/README.md +40 -38
- package/docs/architecture.md +148 -2
- package/package.json +6 -4
- package/packages/extension/package.json +4 -3
- package/packages/extension/src/AGENTS.md +1 -0
- package/packages/extension/src/__tests__/bridge-resume-disconnect.test.ts +333 -0
- package/packages/extension/src/__tests__/openspec-cli-shim.test.ts +145 -0
- package/packages/extension/src/__tests__/provider-register-native-models.test.ts +182 -0
- package/packages/extension/src/__tests__/provider-register-thinking-levels.test.ts +54 -1
- package/packages/extension/src/__tests__/single-derivation-guard.test.ts +39 -0
- package/packages/extension/src/bridge-context.ts +21 -0
- package/packages/extension/src/bridge.ts +41 -6
- package/packages/extension/src/openspec-cli-shim.ts +173 -0
- package/packages/extension/src/openspec-cli-shim.ts.AGENTS.md +7 -0
- package/packages/extension/src/provider-register.ts +180 -17
- package/packages/extension/src/provider-register.ts.AGENTS.md +1 -1
- package/packages/extension/src/session-sync.ts +9 -4
- package/packages/server/package.json +8 -8
- package/packages/server/src/__tests__/decorate-goals-spend.test.ts +79 -0
- package/packages/server/src/__tests__/embed-lifecycle-boot-smoke.test.ts +44 -0
- package/packages/server/src/__tests__/goal-routes.test.ts +62 -7
- package/packages/server/src/__tests__/goal-supervisor.test.ts +4 -4
- package/packages/server/src/__tests__/headless-pid-registry.test.ts +7 -3
- package/packages/server/src/__tests__/memory-event-store.test.ts +369 -13
- package/packages/server/src/__tests__/models-introspection-thinking-map.test.ts +67 -0
- package/packages/server/src/__tests__/pi-changelog-routes.test.ts +9 -2
- package/packages/server/src/__tests__/recovery-offer.test.ts +42 -7
- package/packages/server/src/__tests__/recovery-reattach-retraction.test.ts +188 -0
- package/packages/server/src/__tests__/session-tags-persistence.test.ts +29 -0
- package/packages/server/src/browser-handlers/AGENTS.md +1 -1
- package/packages/server/src/browser-handlers/__tests__/session-meta-handler.test.ts +98 -1
- package/packages/server/src/browser-handlers/handler-context.ts +24 -13
- package/packages/server/src/browser-handlers/session-action-handler.ts +12 -1
- package/packages/server/src/browser-handlers/session-meta-handler.ts +29 -0
- package/packages/server/src/embed-lifecycle/AGENTS.md +17 -0
- package/packages/server/src/embed-lifecycle/__tests__/caps.test.ts +129 -0
- package/packages/server/src/embed-lifecycle/__tests__/embed-lifecycle-controller.test.ts +84 -0
- package/packages/server/src/embed-lifecycle/__tests__/identity-key.test.ts +73 -0
- package/packages/server/src/embed-lifecycle/__tests__/idle-reaper.test.ts +173 -0
- package/packages/server/src/embed-lifecycle/__tests__/lifecycle-config.test.ts +26 -0
- package/packages/server/src/embed-lifecycle/__tests__/lifecycle-event-capture.test.ts +32 -0
- package/packages/server/src/embed-lifecycle/__tests__/lifecycle-marker-persistence.test.ts +106 -0
- package/packages/server/src/embed-lifecycle/__tests__/lifecycle-metrics.test.ts +39 -0
- package/packages/server/src/embed-lifecycle/__tests__/liveness-probe.test.ts +77 -0
- package/packages/server/src/embed-lifecycle/__tests__/quiescence.test.ts +175 -0
- package/packages/server/src/embed-lifecycle/__tests__/reclamation-soak.test.ts +77 -0
- package/packages/server/src/embed-lifecycle/__tests__/session-lifecycle-policy.test.ts +38 -0
- package/packages/server/src/embed-lifecycle/__tests__/visitor-session-registry.test.ts +140 -0
- package/packages/server/src/embed-lifecycle/caps.ts +88 -0
- package/packages/server/src/embed-lifecycle/cwd-allowlist.ts +50 -0
- package/packages/server/src/embed-lifecycle/embed-lifecycle-controller.ts +90 -0
- package/packages/server/src/embed-lifecycle/identity-key.ts +73 -0
- package/packages/server/src/embed-lifecycle/idle-reaper.ts +161 -0
- package/packages/server/src/embed-lifecycle/lifecycle-event-capture.ts +30 -0
- package/packages/server/src/embed-lifecycle/lifecycle-metrics.ts +70 -0
- package/packages/server/src/embed-lifecycle/liveness-probe.ts +123 -0
- package/packages/server/src/embed-lifecycle/quiescence.ts +159 -0
- package/packages/server/src/embed-lifecycle/session-lifecycle-policy.ts +31 -0
- package/packages/server/src/embed-lifecycle/visitor-session-registry.ts +178 -0
- package/packages/server/src/event-wiring.ts +20 -0
- package/packages/server/src/event-wiring.ts.AGENTS.md +1 -0
- package/packages/server/src/goal/AGENTS.md +1 -0
- package/packages/server/src/goal/decorate-goals-spend.ts +47 -0
- package/packages/server/src/goal/goal-supervisor.ts +5 -3
- package/packages/server/src/model-proxy/AGENTS.md +2 -2
- package/packages/server/src/model-proxy/__tests__/internal-registry-native-merge.test.ts +155 -0
- package/packages/server/src/model-proxy/__tests__/read-native-models.test.ts +92 -0
- package/packages/server/src/model-proxy/internal-registry.ts +49 -23
- package/packages/server/src/model-proxy/registry-singleton.ts +15 -9
- package/packages/server/src/pairing/browser-gateway.ts +40 -11
- package/packages/server/src/pairing/browser-gateway.ts.AGENTS.md +1 -1
- package/packages/server/src/persistence/memory-event-store.ts +369 -23
- package/packages/server/src/persistence/memory-event-store.ts.AGENTS.md +1 -1
- package/packages/server/src/routes/AGENTS.md +2 -2
- package/packages/server/src/routes/goal-routes.ts +14 -9
- package/packages/server/src/routes/models-introspection-routes.ts +10 -0
- package/packages/server/src/routes/system-routes.ts +16 -1
- package/packages/server/src/server.ts +216 -65
- package/packages/server/src/server.ts.AGENTS.md +1 -1
- package/packages/server/src/session/memory-session-manager.ts +4 -0
- package/packages/server/src/session/session-scanner.ts +9 -0
- package/packages/server/src/session/session-to-meta.ts +6 -0
- package/packages/server/src/spawn-process/headless-pid-registry.ts +14 -4
- package/packages/shared/package.json +1 -1
- package/packages/shared/src/AGENTS.md +5 -3
- package/packages/shared/src/__tests__/models-json-reader.test.ts +100 -0
- package/packages/shared/src/__tests__/nightly-workflow-contract.test.ts +125 -0
- package/packages/shared/src/__tests__/openspec-poller-1.6.0-compat.test.ts +112 -0
- package/packages/shared/src/__tests__/pnpm-migration-contract.test.ts +177 -0
- package/packages/shared/src/__tests__/publish-workflow-contract.test.ts +6 -3
- package/packages/shared/src/browser-protocol.ts +22 -0
- package/packages/shared/src/config.ts +70 -0
- package/packages/shared/src/dashboard-plugin/AGENTS.md +1 -1
- package/packages/shared/src/dashboard-plugin/slot-props.ts +9 -0
- package/packages/shared/src/dashboard-plugin/ui-primitives.ts +19 -0
- package/packages/shared/src/models-json-reader.ts +98 -0
- package/packages/shared/src/platform/AGENTS.md +1 -1
- package/packages/shared/src/platform/paths.ts +12 -0
- package/packages/shared/src/session-meta.ts +9 -0
- package/packages/shared/src/types.ts +52 -1
- package/docs/AGENTS.md +0 -47
- package/packages/extension/AGENTS.md +0 -7
- package/packages/server/AGENTS.md +0 -7
- package/packages/shared/AGENTS.md +0 -7
- package/scripts/AGENTS.md +0 -38
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reap-decision core (test-plan #E1, #E12, #E15, #F1, #F2, #F3, #X4, #X5).
|
|
3
|
+
* `decideReap` is the pure heart of the reaper; this suite is the decision
|
|
4
|
+
* table + one-signal-veto matrix + BVA boundary that proves gear selection and
|
|
5
|
+
* every veto without a server instance.
|
|
6
|
+
* See change: add-embed-session-lifecycle.
|
|
7
|
+
*/
|
|
8
|
+
import { describe, expect, it } from "vitest";
|
|
9
|
+
import {
|
|
10
|
+
decideReap,
|
|
11
|
+
isAtRest,
|
|
12
|
+
type LifecycleSignals,
|
|
13
|
+
type LifecycleThresholds,
|
|
14
|
+
} from "../quiescence.js";
|
|
15
|
+
|
|
16
|
+
const T: LifecycleThresholds = {
|
|
17
|
+
idleTimeoutMs: 60_000,
|
|
18
|
+
graceWindowMs: 10_000,
|
|
19
|
+
hardCeilingMs: 600_000,
|
|
20
|
+
};
|
|
21
|
+
const NOW = 10_000_000;
|
|
22
|
+
|
|
23
|
+
/** A fully quiescent, idle, past-grace ephemeral session (the reap baseline). */
|
|
24
|
+
function quiescent(overrides: Partial<LifecycleSignals> = {}): LifecycleSignals {
|
|
25
|
+
return {
|
|
26
|
+
lifecyclePolicy: "ephemeral",
|
|
27
|
+
lastRunStartedAt: NOW - 500_000,
|
|
28
|
+
lastSettledAt: NOW - 400_000, // settled after start ⇒ at rest
|
|
29
|
+
currentTool: null,
|
|
30
|
+
hasPendingAsk: false,
|
|
31
|
+
followUpCount: 0,
|
|
32
|
+
steeringCount: 0,
|
|
33
|
+
hasLiveChild: false,
|
|
34
|
+
hasTerminalInCwd: false,
|
|
35
|
+
hasSubscriber: false,
|
|
36
|
+
activatedAt: NOW - 500_000, // well past grace
|
|
37
|
+
lastActivityAt: NOW - 120_000, // idle age 120s > 60s timeout
|
|
38
|
+
cpuIdle: true,
|
|
39
|
+
...overrides,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** A streaming (mid-run, unsettled) session. */
|
|
44
|
+
function streaming(overrides: Partial<LifecycleSignals> = {}): LifecycleSignals {
|
|
45
|
+
return quiescent({
|
|
46
|
+
lastRunStartedAt: NOW - 700_000, // started, no later settle
|
|
47
|
+
lastSettledAt: NOW - 800_000, // older than start ⇒ mid-run
|
|
48
|
+
...overrides,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
describe("isAtRest", () => {
|
|
53
|
+
it("is at rest when a settle is the latest run signal", () => {
|
|
54
|
+
expect(isAtRest({ lastRunStartedAt: 100, lastSettledAt: 200 })).toBe(true);
|
|
55
|
+
});
|
|
56
|
+
it("is mid-run when a start is newer than the last settle", () => {
|
|
57
|
+
expect(isAtRest({ lastRunStartedAt: 300, lastSettledAt: 200 })).toBe(false);
|
|
58
|
+
});
|
|
59
|
+
it("is at rest when a run never started", () => {
|
|
60
|
+
expect(isAtRest({})).toBe(true);
|
|
61
|
+
});
|
|
62
|
+
it("is mid-run when a run started but never settled", () => {
|
|
63
|
+
expect(isAtRest({ lastRunStartedAt: 300 })).toBe(false);
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
describe("decideReap — gear 1 (idle)", () => {
|
|
68
|
+
// E1 / E12 — eligible idle ephemeral is reaped.
|
|
69
|
+
it("reaps a fully quiescent idle ephemeral session", () => {
|
|
70
|
+
expect(decideReap(quiescent(), T, NOW)).toEqual({ action: "reap", reason: "idle" });
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
// E1 — durable/absent is never reaped.
|
|
74
|
+
it("skips a durable session", () => {
|
|
75
|
+
expect(decideReap(quiescent({ lifecyclePolicy: "durable" }), T, NOW).action).toBe("skip");
|
|
76
|
+
expect(decideReap(quiescent({ lifecyclePolicy: undefined }), T, NOW).action).toBe("skip");
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
// E12 — BVA boundary: age == timeout is NOT reaped; timeout+1 IS.
|
|
80
|
+
it("does not reap at exactly the idle timeout, reaps just past it", () => {
|
|
81
|
+
const atBoundary = quiescent({ lastActivityAt: NOW - T.idleTimeoutMs });
|
|
82
|
+
expect(decideReap(atBoundary, T, NOW).action).toBe("skip");
|
|
83
|
+
const pastBoundary = quiescent({ lastActivityAt: NOW - T.idleTimeoutMs - 1 });
|
|
84
|
+
expect(decideReap(pastBoundary, T, NOW)).toEqual({ action: "reap", reason: "idle" });
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
describe("decideReap — X4 one-signal-veto matrix", () => {
|
|
89
|
+
// Flipping active generation on the idle baseline makes it a stalled stream;
|
|
90
|
+
// the quiescence gate must veto the LOSSY gear-1 idle reap (it may instead be
|
|
91
|
+
// gracefully stopped after its turn — never idle-hard-killed).
|
|
92
|
+
it("does NOT idle-reap an actively-generating session", () => {
|
|
93
|
+
const verdict = decideReap(quiescent({ lastRunStartedAt: NOW - 100 }), T, NOW);
|
|
94
|
+
expect(verdict).not.toEqual({ action: "reap", reason: "idle" });
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
// Each flip of a single busy signal on the otherwise-reapable (at-rest)
|
|
98
|
+
// baseline must block the reap outright.
|
|
99
|
+
const cases: Array<[string, Partial<LifecycleSignals>, string]> = [
|
|
100
|
+
["current tool executing", { currentTool: "bash" }, "current-tool"],
|
|
101
|
+
["pending ask_user", { hasPendingAsk: true }, "pending-ask"],
|
|
102
|
+
["queued follow-up", { followUpCount: 1 }, "followup-queued"],
|
|
103
|
+
["queued steering", { steeringCount: 1 }, "steering-queued"],
|
|
104
|
+
["live terminal in cwd", { hasTerminalInCwd: true }, "terminal-in-cwd"],
|
|
105
|
+
["live child process", { hasLiveChild: true }, "live-child"],
|
|
106
|
+
["active watcher", { hasSubscriber: true }, "active-watcher"],
|
|
107
|
+
["within grace window", { activatedAt: NOW - 1 }, "grace-window"],
|
|
108
|
+
];
|
|
109
|
+
for (const [label, flip, reason] of cases) {
|
|
110
|
+
it(`does NOT reap: ${label}`, () => {
|
|
111
|
+
const verdict = decideReap(quiescent(flip), T, NOW);
|
|
112
|
+
expect(verdict.action).toBe("skip");
|
|
113
|
+
expect((verdict as { reason: string }).reason).toBe(reason);
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
describe("decideReap — gear 3 (phantom)", () => {
|
|
119
|
+
// E15 — wedged streaming past the hard ceiling, ~0 CPU, no child, no watcher,
|
|
120
|
+
// no ask, empty queues ⇒ phantom.
|
|
121
|
+
it("force-reaps a wedged streaming session as phantom", () => {
|
|
122
|
+
const phantom = streaming({ lastRunStartedAt: NOW - (T.hardCeilingMs + 1) });
|
|
123
|
+
expect(decideReap(phantom, T, NOW)).toEqual({ action: "reap", reason: "phantom" });
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
// X5 — pending ask blocks phantom even past the ceiling.
|
|
127
|
+
it("does NOT phantom-reap a session blocked on ask_user", () => {
|
|
128
|
+
const blocked = streaming({
|
|
129
|
+
lastRunStartedAt: NOW - (T.hardCeilingMs + 1),
|
|
130
|
+
hasPendingAsk: true,
|
|
131
|
+
});
|
|
132
|
+
expect(decideReap(blocked, T, NOW).action).toBe("skip");
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
// X5 — non-empty queue blocks phantom.
|
|
136
|
+
it("does NOT phantom-reap a session with queued work", () => {
|
|
137
|
+
const queued = streaming({
|
|
138
|
+
lastRunStartedAt: NOW - (T.hardCeilingMs + 1),
|
|
139
|
+
followUpCount: 2,
|
|
140
|
+
});
|
|
141
|
+
expect(decideReap(queued, T, NOW).action).toBe("skip");
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
// Phantom requires ~0 CPU: a busy tree past the ceiling is never force-reaped
|
|
145
|
+
// as phantom (it may still be gracefully stopped after its turn).
|
|
146
|
+
it("does NOT phantom-reap a CPU-busy streaming session", () => {
|
|
147
|
+
const busy = streaming({ lastRunStartedAt: NOW - (T.hardCeilingMs + 1), cpuIdle: false });
|
|
148
|
+
const verdict = decideReap(busy, T, NOW);
|
|
149
|
+
expect(verdict).not.toEqual({ action: "reap", reason: "phantom" });
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
describe("decideReap — gear 2 (stop-after-turn)", () => {
|
|
154
|
+
// F1 — idle-trending streaming, empty queues, no watcher, past timeout ⇒ stop.
|
|
155
|
+
it("sends stop-after-turn for an idle-trending streaming session", () => {
|
|
156
|
+
const idleStream = streaming({ lastRunStartedAt: NOW - 100_000 }); // past idle, below ceiling
|
|
157
|
+
expect(decideReap(idleStream, T, NOW)).toEqual({
|
|
158
|
+
action: "reap",
|
|
159
|
+
reason: "stop-after-turn",
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
// F2 — streaming with a non-empty queue is NOT stopped (drains first).
|
|
164
|
+
it("does NOT stop a streaming session with queued follow-up", () => {
|
|
165
|
+
const queued = streaming({ lastRunStartedAt: NOW - 100_000, followUpCount: 1 });
|
|
166
|
+
expect(decideReap(queued, T, NOW).action).toBe("skip");
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
// F3 — a streaming session below the idle timeout is left alone (disconnect
|
|
170
|
+
// alone never reclaims; only quiescence + timeout does).
|
|
171
|
+
it("does NOT stop a recently-active streaming session", () => {
|
|
172
|
+
const recent = streaming({ lastRunStartedAt: NOW - 5_000, lastActivityAt: NOW - 1_000 });
|
|
173
|
+
expect(decideReap(recent, T, NOW).action).toBe("skip");
|
|
174
|
+
});
|
|
175
|
+
});
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reclamation soak — the #383 goal (test-plan #P1).
|
|
3
|
+
*
|
|
4
|
+
* The real L2 soak (qa VM matrix) spawns N ephemeral `pi` sessions, leaves them
|
|
5
|
+
* quiescent past the idle timeout, and asserts the aggregate `pi` process count
|
|
6
|
+
* → 0 and aggregate RSS drops below a floor after one sweep. That real-RSS
|
|
7
|
+
* measurement needs a live multi-pi runtime; here we prove the deterministic
|
|
8
|
+
* INVARIANT that drives it: given N quiescent ephemeral sessions, one reaper
|
|
9
|
+
* sweep reclaims ALL of them (killBySessionId fired N times), so the live pi
|
|
10
|
+
* process count collapses to zero — which is what makes the aggregate RSS fall.
|
|
11
|
+
* See change: add-embed-session-lifecycle.
|
|
12
|
+
*/
|
|
13
|
+
import { DEFAULT_EMBED_LIFECYCLE } from "@blackbelt-technology/pi-dashboard-shared/config.js";
|
|
14
|
+
import type { DashboardSession } from "@blackbelt-technology/pi-dashboard-shared/types.js";
|
|
15
|
+
import { describe, expect, it } from "vitest";
|
|
16
|
+
import {
|
|
17
|
+
createEmbedLifecycleController,
|
|
18
|
+
type EmbedLifecycleControllerDeps,
|
|
19
|
+
} from "../embed-lifecycle-controller.js";
|
|
20
|
+
|
|
21
|
+
const NOW = Date.now();
|
|
22
|
+
const N = 35; // the reported regression: 35 accumulated sessions
|
|
23
|
+
|
|
24
|
+
function quiescentEphemeral(i: number): DashboardSession {
|
|
25
|
+
return {
|
|
26
|
+
id: `embed-${i}`,
|
|
27
|
+
cwd: `/work/${i}`,
|
|
28
|
+
source: "embed",
|
|
29
|
+
lifecyclePolicy: "ephemeral",
|
|
30
|
+
status: "idle",
|
|
31
|
+
startedAt: NOW - 10_000_000,
|
|
32
|
+
pid: 1000 + i,
|
|
33
|
+
lastRunStartedAt: NOW - 9_000_000,
|
|
34
|
+
lastSettledAt: NOW - 8_000_000, // settled ⇒ at rest
|
|
35
|
+
currentTool: null,
|
|
36
|
+
lastActivityAt: NOW - 8_000_000, // far past idle timeout
|
|
37
|
+
} as DashboardSession;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
describe("reclamation soak (P1 invariant)", () => {
|
|
41
|
+
it("reaps ALL quiescent ephemeral sessions in one sweep → live count collapses to 0", async () => {
|
|
42
|
+
// N ephemeral sessions + a few durable coding sessions that must survive.
|
|
43
|
+
const sessions: DashboardSession[] = [
|
|
44
|
+
...Array.from({ length: N }, (_, i) => quiescentEphemeral(i)),
|
|
45
|
+
{ ...quiescentEphemeral(999), id: "human", lifecyclePolicy: "durable" } as DashboardSession,
|
|
46
|
+
];
|
|
47
|
+
const killed = new Set<string>();
|
|
48
|
+
|
|
49
|
+
const deps: EmbedLifecycleControllerDeps = {
|
|
50
|
+
config: () => ({ ...DEFAULT_EMBED_LIFECYCLE, enabled: true, idleTimeoutSeconds: 1 }),
|
|
51
|
+
// Live view: a killed session drops out of the active list (its runtime
|
|
52
|
+
// ended), mirroring the real process exiting.
|
|
53
|
+
listSessions: () => sessions.filter((s) => !killed.has(s.id)),
|
|
54
|
+
getSubscriberCount: () => 0,
|
|
55
|
+
listTerminalCwds: () => [],
|
|
56
|
+
hasPendingUiRequest: () => false,
|
|
57
|
+
killBySessionId: async (id) => {
|
|
58
|
+
killed.add(id);
|
|
59
|
+
return true;
|
|
60
|
+
},
|
|
61
|
+
sendStopAfterTurn: () => {},
|
|
62
|
+
probe: async () => ({ ok: true, childCount: 0, cpuPercent: 0 }),
|
|
63
|
+
};
|
|
64
|
+
const ctrl = createEmbedLifecycleController(deps);
|
|
65
|
+
|
|
66
|
+
expect(ctrl.snapshot().activeEphemeral).toBe(N);
|
|
67
|
+
|
|
68
|
+
await ctrl.reaper.sweepOnce();
|
|
69
|
+
|
|
70
|
+
// Every ephemeral session reclaimed; the durable coding session survives.
|
|
71
|
+
expect(killed.size).toBe(N);
|
|
72
|
+
expect(killed.has("human")).toBe(false);
|
|
73
|
+
// Live ephemeral process count collapses to zero (the RSS-drop driver).
|
|
74
|
+
expect(ctrl.snapshot().activeEphemeral).toBe(0);
|
|
75
|
+
expect(ctrl.snapshot().reaped.idle).toBe(N);
|
|
76
|
+
});
|
|
77
|
+
});
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provenance accessor contract (test-plan #E2, #E5): an absent `lifecyclePolicy`
|
|
3
|
+
* is treated as `"durable"` (legacy safety), interactive sessions stay durable,
|
|
4
|
+
* and only an explicit `"ephemeral"` marker flips `isEphemeral`. Every downstream
|
|
5
|
+
* gate (reaper, caps) reads through these accessors, so this is the single point
|
|
6
|
+
* where absent-⇒-durable is guaranteed.
|
|
7
|
+
* See change: add-embed-session-lifecycle.
|
|
8
|
+
*/
|
|
9
|
+
import { describe, expect, it } from "vitest";
|
|
10
|
+
import {
|
|
11
|
+
effectiveLifecyclePolicy,
|
|
12
|
+
isEphemeral,
|
|
13
|
+
} from "../session-lifecycle-policy.js";
|
|
14
|
+
|
|
15
|
+
describe("session lifecycle policy accessor", () => {
|
|
16
|
+
// E2 — absent policy defaults to durable.
|
|
17
|
+
it("treats an absent lifecyclePolicy as durable", () => {
|
|
18
|
+
expect(effectiveLifecyclePolicy({})).toBe("durable");
|
|
19
|
+
expect(isEphemeral({})).toBe(false);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
// E5 — interactive spawns (no marker) stay durable and ungoverned.
|
|
23
|
+
it("classifies an interactive session (no marker) as durable", () => {
|
|
24
|
+
const interactive = { lifecyclePolicy: undefined };
|
|
25
|
+
expect(effectiveLifecyclePolicy(interactive)).toBe("durable");
|
|
26
|
+
expect(isEphemeral(interactive)).toBe(false);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("classifies an explicitly durable session as durable", () => {
|
|
30
|
+
expect(effectiveLifecyclePolicy({ lifecyclePolicy: "durable" })).toBe("durable");
|
|
31
|
+
expect(isEphemeral({ lifecyclePolicy: "durable" })).toBe(false);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("classifies an ephemeral session as ephemeral", () => {
|
|
35
|
+
expect(effectiveLifecyclePolicy({ lifecyclePolicy: "ephemeral" })).toBe("ephemeral");
|
|
36
|
+
expect(isEphemeral({ lifecyclePolicy: "ephemeral" })).toBe(true);
|
|
37
|
+
});
|
|
38
|
+
});
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Idempotent acquire registry (test-plan #E6, #E7, #E8, #E9, #E10, #X1, #X2).
|
|
3
|
+
* Drives the coalescing ladder with injected deps: reuse-live, resume-renumber,
|
|
4
|
+
* concurrent convergence, register-window join, allowlist reject, register
|
|
5
|
+
* timeout.
|
|
6
|
+
* See change: add-embed-session-lifecycle.
|
|
7
|
+
*/
|
|
8
|
+
import { DEFAULT_EMBED_LIFECYCLE } from "@blackbelt-technology/pi-dashboard-shared/config.js";
|
|
9
|
+
import { describe, expect, it, vi } from "vitest";
|
|
10
|
+
import { composeIdentityKey } from "../identity-key.js";
|
|
11
|
+
import {
|
|
12
|
+
createVisitorSessionRegistry,
|
|
13
|
+
type RegistryDeps,
|
|
14
|
+
} from "../visitor-session-registry.js";
|
|
15
|
+
|
|
16
|
+
const REQ = { visitorId: "v1", cwd: "/srv/e/proj", agentIdentity: "a" };
|
|
17
|
+
const CANON = "/srv/e/proj";
|
|
18
|
+
const KEY = composeIdentityKey("v1", CANON, "a");
|
|
19
|
+
|
|
20
|
+
function makeDeps(over: Partial<RegistryDeps> = {}): RegistryDeps {
|
|
21
|
+
return {
|
|
22
|
+
config: () => ({ ...DEFAULT_EMBED_LIFECYCLE, enabled: true, registerTimeoutSeconds: 1 }),
|
|
23
|
+
canonicalizeCwd: (p) => p,
|
|
24
|
+
isCwdAllowed: () => true,
|
|
25
|
+
isSessionLive: () => false,
|
|
26
|
+
isSessionResumable: () => false,
|
|
27
|
+
spawn: vi.fn(),
|
|
28
|
+
resume: vi.fn(),
|
|
29
|
+
...over,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
describe("visitor session registry acquire", () => {
|
|
34
|
+
// E8 / E9 — concurrent acquires converge on ONE spawn and both resolve to it.
|
|
35
|
+
it("coalesces concurrent acquires onto a single spawn", async () => {
|
|
36
|
+
const spawn = vi.fn();
|
|
37
|
+
const reg = createVisitorSessionRegistry(makeDeps({ spawn }));
|
|
38
|
+
const p1 = reg.acquire(REQ);
|
|
39
|
+
const p2 = reg.acquire(REQ);
|
|
40
|
+
await Promise.resolve(); // let the admit→fire microtask run
|
|
41
|
+
expect(spawn).toHaveBeenCalledTimes(1); // one spawn for two acquires
|
|
42
|
+
reg.resolveByCwd(CANON, "sess-1");
|
|
43
|
+
const [r1, r2] = await Promise.all([p1, p2]);
|
|
44
|
+
expect(r1.sessionId).toBe("sess-1");
|
|
45
|
+
expect(r2.sessionId).toBe("sess-1");
|
|
46
|
+
expect(r1.reason).toBe("spawned");
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
// E6 / E7 — once a live session is mapped, a later acquire reuses it (no spawn).
|
|
50
|
+
it("reuses a live session on a subsequent acquire", async () => {
|
|
51
|
+
const spawn = vi.fn();
|
|
52
|
+
let live = false;
|
|
53
|
+
const reg = createVisitorSessionRegistry(
|
|
54
|
+
makeDeps({ spawn, isSessionLive: () => live }),
|
|
55
|
+
);
|
|
56
|
+
const p1 = reg.acquire(REQ);
|
|
57
|
+
reg.resolveByCwd(CANON, "sess-1");
|
|
58
|
+
await p1;
|
|
59
|
+
live = true; // the spawned session is now live
|
|
60
|
+
|
|
61
|
+
const r2 = await reg.acquire(REQ);
|
|
62
|
+
expect(r2).toEqual({ sessionId: "sess-1", reason: "live" });
|
|
63
|
+
expect(spawn).toHaveBeenCalledTimes(1); // no second spawn
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
// E10 — a reaped (ended, resumable) session is resumed under a NEW id and the
|
|
67
|
+
// key re-points to it.
|
|
68
|
+
it("resumes a resumable session under a new id and re-points the key", async () => {
|
|
69
|
+
const resume = vi.fn();
|
|
70
|
+
let live = true;
|
|
71
|
+
let resumable = false;
|
|
72
|
+
const reg = createVisitorSessionRegistry(
|
|
73
|
+
makeDeps({
|
|
74
|
+
resume,
|
|
75
|
+
isSessionLive: () => live,
|
|
76
|
+
isSessionResumable: () => resumable,
|
|
77
|
+
spawn: vi.fn(),
|
|
78
|
+
}),
|
|
79
|
+
);
|
|
80
|
+
// First acquire spawns sess-1.
|
|
81
|
+
const p1 = reg.acquire(REQ);
|
|
82
|
+
reg.resolveByCwd(CANON, "sess-1");
|
|
83
|
+
await p1;
|
|
84
|
+
|
|
85
|
+
// sess-1 gets reaped: no longer live, now resumable.
|
|
86
|
+
live = false;
|
|
87
|
+
resumable = true;
|
|
88
|
+
const p2 = reg.acquire(REQ);
|
|
89
|
+
await Promise.resolve(); // let the admit→resume microtask run
|
|
90
|
+
expect(resume).toHaveBeenCalledWith("sess-1", REQ, KEY);
|
|
91
|
+
reg.resolveByCwd(CANON, "sess-2"); // resume mints a fresh id
|
|
92
|
+
const r2 = await p2;
|
|
93
|
+
expect(r2.sessionId).toBe("sess-2");
|
|
94
|
+
expect(reg.mappedSessionId(KEY)).toBe("sess-2"); // re-pointed
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
// X1 — an out-of-allowlist cwd is rejected, no spawn.
|
|
98
|
+
it("rejects an out-of-allowlist cwd without spawning", async () => {
|
|
99
|
+
const spawn = vi.fn();
|
|
100
|
+
const reg = createVisitorSessionRegistry(makeDeps({ spawn, isCwdAllowed: () => false }));
|
|
101
|
+
await expect(reg.acquire(REQ)).rejects.toThrow(/cwd not allowed/);
|
|
102
|
+
expect(spawn).not.toHaveBeenCalled();
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
// X2 — a spawn that never registers rejects the coalesced result and clears
|
|
106
|
+
// the entry so waiters do not hang (and a later acquire can retry).
|
|
107
|
+
it("rejects on register timeout and clears the in-flight entry", async () => {
|
|
108
|
+
vi.useFakeTimers();
|
|
109
|
+
try {
|
|
110
|
+
const spawn = vi.fn();
|
|
111
|
+
const reg = createVisitorSessionRegistry(makeDeps({ spawn }));
|
|
112
|
+
const p = reg.acquire(REQ);
|
|
113
|
+
const assertion = expect(p).rejects.toThrow(/register timeout/);
|
|
114
|
+
await vi.advanceTimersByTimeAsync(1000); // registerTimeoutSeconds: 1
|
|
115
|
+
await assertion;
|
|
116
|
+
// Entry cleared → a fresh acquire spawns again (does not coalesce onto the
|
|
117
|
+
// dead promise).
|
|
118
|
+
reg.acquire(REQ);
|
|
119
|
+
await vi.advanceTimersByTimeAsync(0); // flush the admit→fire microtask
|
|
120
|
+
expect(spawn).toHaveBeenCalledTimes(2);
|
|
121
|
+
} finally {
|
|
122
|
+
vi.useRealTimers();
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
// Caps admission: a CapacityError from `admit` propagates and blocks the spawn.
|
|
127
|
+
it("propagates a capacity error from the admission gate", async () => {
|
|
128
|
+
const spawn = vi.fn();
|
|
129
|
+
const reg = createVisitorSessionRegistry(
|
|
130
|
+
makeDeps({
|
|
131
|
+
spawn,
|
|
132
|
+
admit: async () => {
|
|
133
|
+
throw new Error("capacity");
|
|
134
|
+
},
|
|
135
|
+
}),
|
|
136
|
+
);
|
|
137
|
+
await expect(reg.acquire(REQ)).rejects.toThrow(/capacity/);
|
|
138
|
+
expect(spawn).not.toHaveBeenCalled();
|
|
139
|
+
});
|
|
140
|
+
});
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Active-session caps with graceful reclaim (D9, D11).
|
|
3
|
+
*
|
|
4
|
+
* `createCapsAdmission` returns an `admit(key)` gate for the acquire path. It
|
|
5
|
+
* counts ONLY `ephemeral` active sessions. When admitting would exceed a cap it
|
|
6
|
+
* reclaims the OLDEST safely-quiescent candidate first; if every candidate is
|
|
7
|
+
* busy it throws a structured `CapacityError` and terminates nothing. The
|
|
8
|
+
* GLOBAL cap is the hard security bound against a caller that spoofs
|
|
9
|
+
* `visitorId`s (an over-global breach reclaims across ALL visitors); the
|
|
10
|
+
* per-visitor cap is fairness for trusted identities (reclaims within the
|
|
11
|
+
* visitor). Reaper and caps share a `reclaimGuard` set so they never
|
|
12
|
+
* double-select the same victim.
|
|
13
|
+
*
|
|
14
|
+
* See OpenSpec change: add-embed-session-lifecycle.
|
|
15
|
+
*/
|
|
16
|
+
import type { EmbedLifecycleConfig } from "@blackbelt-technology/pi-dashboard-shared/config.js";
|
|
17
|
+
import { visitorIdOf } from "./identity-key.js";
|
|
18
|
+
|
|
19
|
+
/** Structured capacity error — no active session was terminated (X3). */
|
|
20
|
+
export class CapacityError extends Error {
|
|
21
|
+
readonly code = "EMBED_CAPACITY";
|
|
22
|
+
constructor(message = "embedded-session capacity reached") {
|
|
23
|
+
super(message);
|
|
24
|
+
this.name = "CapacityError";
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** A candidate the caps gate may count and (if quiescent) reclaim. */
|
|
29
|
+
export interface CapSessionInfo {
|
|
30
|
+
sessionId: string;
|
|
31
|
+
visitorId: string;
|
|
32
|
+
quiescent: boolean;
|
|
33
|
+
lastActivityAt?: number;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface CapsDeps {
|
|
37
|
+
config: () => EmbedLifecycleConfig;
|
|
38
|
+
/** Currently-active EPHEMERAL sessions (durable sessions are never listed). */
|
|
39
|
+
listEphemeralActive: () => readonly CapSessionInfo[];
|
|
40
|
+
/** Graceful reclaim (killBySessionId) of the chosen victim. */
|
|
41
|
+
reclaim: (sessionId: string) => Promise<void>;
|
|
42
|
+
/** Shared with the reaper so both never double-select a victim. */
|
|
43
|
+
reclaimGuard?: Set<string>;
|
|
44
|
+
/** Fires when an acquire is rejected for capacity (diagnostics). */
|
|
45
|
+
onCapacityReject?: () => void;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface CapsAdmission {
|
|
49
|
+
admit: (key: string) => Promise<void>;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function createCapsAdmission(deps: CapsDeps): CapsAdmission {
|
|
53
|
+
const guard = deps.reclaimGuard ?? new Set<string>();
|
|
54
|
+
|
|
55
|
+
async function admit(key: string): Promise<void> {
|
|
56
|
+
const cfg = deps.config();
|
|
57
|
+
const visitorId = visitorIdOf(key);
|
|
58
|
+
// Exclude sessions already being reclaimed by the reaper or a prior admit.
|
|
59
|
+
const active = deps.listEphemeralActive().filter((s) => !guard.has(s.sessionId));
|
|
60
|
+
const globalCount = active.length;
|
|
61
|
+
const perVisitorCount = active.filter((s) => s.visitorId === visitorId).length;
|
|
62
|
+
|
|
63
|
+
const overGlobal = globalCount >= cfg.maxActiveEmbedSessionsGlobal;
|
|
64
|
+
const overVisitor = perVisitorCount >= cfg.maxActiveEmbedSessionsPerVisitor;
|
|
65
|
+
if (!overGlobal && !overVisitor) return; // slot available
|
|
66
|
+
|
|
67
|
+
// Reclaim pool: the GLOBAL breach is adversarial → reclaim across ALL
|
|
68
|
+
// visitors; a per-visitor breach reclaims only within that visitor.
|
|
69
|
+
const pool = overGlobal ? active : active.filter((s) => s.visitorId === visitorId);
|
|
70
|
+
const victim = pool
|
|
71
|
+
.filter((s) => s.quiescent)
|
|
72
|
+
.sort((a, b) => (a.lastActivityAt ?? 0) - (b.lastActivityAt ?? 0))[0];
|
|
73
|
+
|
|
74
|
+
if (!victim) {
|
|
75
|
+
deps.onCapacityReject?.();
|
|
76
|
+
throw new CapacityError();
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
guard.add(victim.sessionId);
|
|
80
|
+
try {
|
|
81
|
+
await deps.reclaim(victim.sessionId);
|
|
82
|
+
} finally {
|
|
83
|
+
guard.delete(victim.sessionId);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return { admit };
|
|
88
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Server-side cwd allowlist for the acquire path (D11).
|
|
3
|
+
*
|
|
4
|
+
* An untrusted embed visitor controls the requested `cwd`, so — before ever
|
|
5
|
+
* spawning a `pi` there — acquire validates the realpath-resolved cwd against a
|
|
6
|
+
* configured set of allowed roots (the same containment idiom as the localhost
|
|
7
|
+
* file routes). An empty allowlist means "unconfigured" and, for safety, denies
|
|
8
|
+
* every request (the caller enables the feature by configuring roots).
|
|
9
|
+
*
|
|
10
|
+
* See OpenSpec change: add-embed-session-lifecycle.
|
|
11
|
+
*/
|
|
12
|
+
import { realpathSync } from "node:fs";
|
|
13
|
+
import { caseInsensitiveFilesystem } from "@blackbelt-technology/pi-dashboard-shared/platform/paths.js";
|
|
14
|
+
import { within } from "../lib/path-containment.js";
|
|
15
|
+
|
|
16
|
+
export interface AllowlistOptions {
|
|
17
|
+
/** Test seam for `realpathSync`. */
|
|
18
|
+
realpath?: (p: string) => string;
|
|
19
|
+
/** Force case-insensitivity (defaults to darwin/win32 detection). */
|
|
20
|
+
caseInsensitive?: boolean;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* True when `cwd` (realpath-resolved) is contained by at least one allowed root
|
|
25
|
+
* (also realpath-resolved). A realpath failure on the cwd falls back to the raw
|
|
26
|
+
* path; a failure on a root drops that root. An empty `allowedRoots` denies.
|
|
27
|
+
*/
|
|
28
|
+
export function isCwdAllowed(
|
|
29
|
+
cwd: string,
|
|
30
|
+
allowedRoots: readonly string[],
|
|
31
|
+
opts: AllowlistOptions = {},
|
|
32
|
+
): boolean {
|
|
33
|
+
if (allowedRoots.length === 0) return false;
|
|
34
|
+
const realpath = opts.realpath ?? realpathSync;
|
|
35
|
+
const caseInsensitive = opts.caseInsensitive ?? caseInsensitiveFilesystem();
|
|
36
|
+
const resolve = (p: string): string => {
|
|
37
|
+
let r: string;
|
|
38
|
+
try {
|
|
39
|
+
r = realpath(p);
|
|
40
|
+
} catch {
|
|
41
|
+
r = p;
|
|
42
|
+
}
|
|
43
|
+
// Case-normalize on case-insensitive filesystems so a realpath'd cwd and an
|
|
44
|
+
// allowed root differing only by casing still contain — matching how
|
|
45
|
+
// `identity-key.ts` canonicalizes the cwd (else a valid cwd is rejected).
|
|
46
|
+
return caseInsensitive ? r.toLowerCase() : r;
|
|
47
|
+
};
|
|
48
|
+
const realCwd = resolve(cwd);
|
|
49
|
+
return allowedRoots.some((root) => within(realCwd, resolve(root)));
|
|
50
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Server-integration facade for the embed-session-lifecycle layer.
|
|
3
|
+
*
|
|
4
|
+
* Constructs the live reaper + observability metrics and wires them to the real
|
|
5
|
+
* server components (session manager, browser gateway, terminal manager,
|
|
6
|
+
* headless pid registry, pi gateway). Dormant when `config().enabled` is false
|
|
7
|
+
* (D8) — constructing and starting it is byte-for-byte behavior-preserving on
|
|
8
|
+
* upgrade. The acquire registry + caps are shared-layer modules consumed by the
|
|
9
|
+
* embed/chat-gateway front when it lands (Non-Goal here), so they are NOT
|
|
10
|
+
* constructed live; only the reaper (which reclaims the automation-produced
|
|
11
|
+
* ephemeral sessions #383 is about) runs.
|
|
12
|
+
*
|
|
13
|
+
* See OpenSpec change: add-embed-session-lifecycle.
|
|
14
|
+
*/
|
|
15
|
+
import type { EmbedLifecycleConfig } from "@blackbelt-technology/pi-dashboard-shared/config.js";
|
|
16
|
+
import type { DashboardSession } from "@blackbelt-technology/pi-dashboard-shared/types.js";
|
|
17
|
+
import { createIdleReaper, type IdleReaper } from "./idle-reaper.js";
|
|
18
|
+
import {
|
|
19
|
+
createLifecycleMetrics,
|
|
20
|
+
type LifecycleMetrics,
|
|
21
|
+
type LifecycleMetricsSnapshot,
|
|
22
|
+
} from "./lifecycle-metrics.js";
|
|
23
|
+
import { createLivenessProbe, type LivenessProbe } from "./liveness-probe.js";
|
|
24
|
+
import { isEphemeral } from "./session-lifecycle-policy.js";
|
|
25
|
+
|
|
26
|
+
export interface EmbedLifecycleControllerDeps {
|
|
27
|
+
config: () => EmbedLifecycleConfig;
|
|
28
|
+
listSessions: () => readonly DashboardSession[];
|
|
29
|
+
getSubscriberCount: (sessionId: string) => number;
|
|
30
|
+
listTerminalCwds: () => readonly string[];
|
|
31
|
+
hasPendingUiRequest: (sessionId: string) => boolean;
|
|
32
|
+
killBySessionId: (sessionId: string) => Promise<boolean>;
|
|
33
|
+
sendStopAfterTurn: (sessionId: string) => void;
|
|
34
|
+
/** Test seam for the liveness probe (defaults to the real `ps` probe). */
|
|
35
|
+
probe?: LivenessProbe;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface EmbedLifecycleController {
|
|
39
|
+
metrics: LifecycleMetrics;
|
|
40
|
+
reaper: IdleReaper;
|
|
41
|
+
start: () => void;
|
|
42
|
+
stop: () => void;
|
|
43
|
+
snapshot: () => LifecycleMetricsSnapshot;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** An ephemeral session that is not ended is "active". */
|
|
47
|
+
function isActiveEphemeral(s: DashboardSession): boolean {
|
|
48
|
+
return isEphemeral(s) && s.status !== "ended";
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function createEmbedLifecycleController(
|
|
52
|
+
deps: EmbedLifecycleControllerDeps,
|
|
53
|
+
): EmbedLifecycleController {
|
|
54
|
+
const probe = deps.probe ?? createLivenessProbe();
|
|
55
|
+
|
|
56
|
+
const metrics = createLifecycleMetrics({
|
|
57
|
+
countActiveEphemeral: () => deps.listSessions().filter(isActiveEphemeral).length,
|
|
58
|
+
// A cheap idle approximation for diagnostics: active ephemeral, not
|
|
59
|
+
// streaming, no tool running. The reaper itself uses the full predicate.
|
|
60
|
+
countIdleEphemeral: () =>
|
|
61
|
+
deps
|
|
62
|
+
.listSessions()
|
|
63
|
+
.filter((s) => isActiveEphemeral(s) && s.status !== "streaming" && s.currentTool == null)
|
|
64
|
+
.length,
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
const reaper = createIdleReaper({
|
|
68
|
+
config: deps.config,
|
|
69
|
+
listSessions: deps.listSessions,
|
|
70
|
+
probe,
|
|
71
|
+
hasSubscriber: (id) => deps.getSubscriberCount(id) > 0,
|
|
72
|
+
hasTerminalInCwd: (cwd) => deps.listTerminalCwds().some((c) => c === cwd),
|
|
73
|
+
hasPendingAsk: deps.hasPendingUiRequest,
|
|
74
|
+
queueCounts: (id) => {
|
|
75
|
+
const q = deps.listSessions().find((s) => s.id === id)?.pendingQueues;
|
|
76
|
+
return { followUp: q?.followUp.length ?? 0, steering: q?.steering.length ?? 0 };
|
|
77
|
+
},
|
|
78
|
+
killBySessionId: deps.killBySessionId,
|
|
79
|
+
sendStopAfterTurn: deps.sendStopAfterTurn,
|
|
80
|
+
onReap: (_id, reason) => metrics.recordReap(reason),
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
return {
|
|
84
|
+
metrics,
|
|
85
|
+
reaper,
|
|
86
|
+
start: () => reaper.start(),
|
|
87
|
+
stop: () => reaper.stop(),
|
|
88
|
+
snapshot: () => metrics.snapshot(),
|
|
89
|
+
};
|
|
90
|
+
}
|