@leavewhite9088/harness-bus 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ARCHITECTURE.md +81 -81
- package/LICENSE +21 -21
- package/README.md +86 -86
- package/SETUP_FOR_AI.md +79 -79
- package/package.json +4 -2
- package/packages/agentd/agentd.config.json +3 -1
- package/packages/agentd/cli/commands.mjs +854 -810
- package/packages/agentd/cli/format.mjs +294 -238
- package/packages/agentd/cli/hbus.mjs +14 -14
- package/packages/agentd/cli/index.mjs +4 -4
- package/packages/agentd/cli/skill/SKILL.md +66 -61
- package/packages/agentd/cli/skill/references/custom.md +12 -0
- package/packages/agentd/cli/skill/references/playbooks.md +89 -86
- package/packages/agentd/cli/skill/references/routing.md +43 -35
- package/packages/agentd/cli/skill-install.mjs +157 -140
- package/packages/agentd/cli/update.mjs +221 -221
- package/packages/agentd/client/index.mjs +3 -3
- package/packages/agentd/contexts/executor.md +13 -12
- package/packages/agentd/contexts/templates/architect.md +12 -12
- package/packages/agentd/contexts/templates/coder.md +12 -12
- package/packages/agentd/contexts/templates/debugger.md +12 -12
- package/packages/agentd/contexts/templates/researcher.md +12 -12
- package/packages/agentd/contexts/templates/reviewer.md +12 -12
- package/packages/agentd/contexts/templates/tester.md +12 -12
- package/packages/agentd/core/base/data-dir.mjs +39 -39
- package/packages/agentd/core/base/index.mjs +5 -5
- package/packages/agentd/core/base/protocol.mjs +13 -13
- package/packages/agentd/core/env/config.mjs +52 -1
- package/packages/agentd/core/env/index.mjs +6 -1
- package/packages/agentd/core/env/install.mjs +82 -4
- package/packages/agentd/core/pi/index.mjs +1 -1
- package/packages/agentd/core/pi/rpc.mjs +206 -206
- package/packages/agentd/core/pool/acp/client.mjs +7 -9
- package/packages/agentd/core/pool/acp/process.mjs +27 -18
- package/packages/agentd/core/pool/acp/run-tracker.mjs +293 -293
- package/packages/agentd/core/pool/acp/sessions.mjs +465 -465
- package/packages/agentd/core/pool/acp/turn-runner.mjs +5 -3
- package/packages/agentd/core/pool/index.mjs +2 -2
- package/packages/agentd/core/pool/instances.mjs +3 -5
- package/packages/agentd/core/pool/probe.mjs +91 -85
- package/packages/agentd/core/pool/registry.mjs +45 -1
- package/packages/agentd/core/pool/support/compaction.mjs +2 -43
- package/packages/agentd/core/pool/support/index.mjs +1 -1
- package/packages/agentd/core/prompts/index.mjs +12 -12
- package/packages/agentd/core/prompts/rules.mjs +22 -22
- package/packages/agentd/core/prompts/templates.mjs +172 -172
- package/packages/agentd/core/providers/index.mjs +14 -14
- package/packages/agentd/core/providers/store.mjs +206 -206
- package/packages/agentd/core/records/cleanup.mjs +51 -51
- package/packages/agentd/core/records/index.mjs +8 -8
- package/packages/agentd/core/records/metrics.mjs +73 -73
- package/packages/agentd/core/records/recovery.mjs +39 -39
- package/packages/agentd/core/records/transcripts.mjs +122 -122
- package/packages/agentd/core/threads/index.mjs +18 -17
- package/packages/agentd/core/threads/manager.mjs +498 -476
- package/packages/agentd/core/threads/paths.mjs +78 -78
- package/packages/agentd/core/threads/store.mjs +146 -125
- package/packages/agentd/daemon/agentd.mjs +58 -58
- package/packages/agentd/daemon/events.mjs +126 -126
- package/packages/agentd/daemon/guard.mjs +127 -127
- package/packages/agentd/daemon/lifecycle.mjs +61 -61
- package/packages/agentd/daemon/ops/index.mjs +15 -15
- package/packages/agentd/daemon/ops/providers.mjs +46 -46
- package/packages/agentd/daemon/ops/run.mjs +413 -400
- package/packages/agentd/daemon/ops/system.mjs +76 -69
- package/packages/agentd/daemon/ops/threads.mjs +156 -153
- package/packages/agentd/daemon/server.mjs +130 -130
- package/packages/agentd/package.json +1 -1
- package/packages/agentd/pi-adapter/acp-adapter.mjs +501 -501
- package/packages/agentd/scripts/restart-daemon.mjs +74 -74
- package/packages/agentd/scripts/run-isolated.mjs +42 -42
- package/packages/agentd/scripts/run-unit.mjs +28 -28
- package/packages/backend/src/daemon.mjs +69 -69
- package/packages/backend/src/server.mjs +270 -270
- package/packages/drivers/{agy.mjs → agy/index.mjs} +2 -1
- package/packages/drivers/{claude.mjs → claude/index.mjs} +23 -1
- package/packages/drivers/{codex.mjs → codex/index.mjs} +2 -1
- package/packages/drivers/{dsh.mjs → dsh/index.mjs} +3 -2
- package/packages/drivers/grok/index.mjs +96 -0
- package/packages/drivers/index.mjs +25 -12
- package/packages/drivers/{kimi.mjs → kimi/index.mjs} +2 -1
- package/packages/drivers/{mcode.mjs → mcode/index.mjs} +2 -1
- package/packages/drivers/{opencode.mjs → opencode/index.mjs} +2 -1
- package/packages/drivers/{pi.mjs → pi/index.mjs} +3 -2
- package/packages/drivers/probes/acp.mjs +109 -109
- package/packages/drivers/probes/index.mjs +131 -129
- package/packages/drivers/probes/provider.mjs +222 -222
- package/packages/drivers/qoder/index.mjs +32 -0
- package/packages/drivers/{bin.mjs → shared/bin.mjs} +2 -0
- package/packages/drivers/shared/index.mjs +2 -0
- package/packages/preflight/bin/preflight.mjs +116 -116
- package/packages/preflight/src/index.mjs +5 -5
- package/packages/preflight/src/report.mjs +18 -1
- package/packages/web/dist/apple-touch-icon.png +0 -0
- package/packages/web/dist/assets/index-CT8dIwav.css +2 -0
- package/packages/web/dist/assets/index-CypTUMFB.js +79 -0
- package/packages/web/dist/favicon-32.png +0 -0
- package/packages/web/dist/favicon.svg +19 -15
- package/packages/web/dist/index.html +5 -5
- package/packages/web/dist/assets/index-B2kdkHNw.js +0 -79
- package/packages/web/dist/assets/index-DQTyjC4x.css +0 -2
- /package/packages/drivers/{dsh-sessions.mjs → dsh/sessions.mjs} +0 -0
- /package/packages/drivers/{pi-catalog.mjs → pi/catalog.mjs} +0 -0
- /package/packages/drivers/{lib.mjs → shared/lib.mjs} +0 -0
|
@@ -1,126 +1,126 @@
|
|
|
1
|
-
import { randomUUID } from 'node:crypto';
|
|
2
|
-
|
|
3
|
-
export const EPOCH = randomUUID().slice(0, 8);
|
|
4
|
-
export const RING_MAX = 2000;
|
|
5
|
-
const ring = [];
|
|
6
|
-
let seq = 0;
|
|
7
|
-
|
|
8
|
-
const subscribers = new Map();
|
|
9
|
-
export const allSubscribers = new Set();
|
|
10
|
-
const clients = new Set();
|
|
11
|
-
const listeners = new Set();
|
|
12
|
-
const MAX_BACKLOG = 32 * 1024 * 1024;
|
|
13
|
-
|
|
14
|
-
function writeAll(socks, payload) {
|
|
15
|
-
const line = JSON.stringify(payload) + '\n';
|
|
16
|
-
for (const sock of socks) {
|
|
17
|
-
try {
|
|
18
|
-
if (sock.destroyed) continue;
|
|
19
|
-
if (sock.writableLength > MAX_BACKLOG) {
|
|
20
|
-
sock.destroy();
|
|
21
|
-
continue;
|
|
22
|
-
}
|
|
23
|
-
sock.write(line);
|
|
24
|
-
} catch {
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export function allocEvent(type, payload = {}) {
|
|
30
|
-
seq += 1;
|
|
31
|
-
return { id: `${EPOCH}-${seq}`, seq, epoch: EPOCH, event: type, ts: new Date().toISOString(), ...payload };
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
function deliverTargets(jobId) {
|
|
35
|
-
const jobSet = subscribers.get(jobId);
|
|
36
|
-
if (!jobSet || jobSet.size === 0) return allSubscribers;
|
|
37
|
-
if (allSubscribers.size === 0) return jobSet;
|
|
38
|
-
const merged = new Set(jobSet);
|
|
39
|
-
for (const sock of allSubscribers) merged.add(sock);
|
|
40
|
-
return merged;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
function deliver(ev) {
|
|
44
|
-
if (!ev.jobId) return writeAll(clients, ev);
|
|
45
|
-
const targets = deliverTargets(ev.jobId);
|
|
46
|
-
if (targets.size) writeAll(targets, ev);
|
|
47
|
-
if (ev.event === 'job.done' || ev.event === 'job.interrupted') {
|
|
48
|
-
subscribers.delete(ev.jobId);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export function publish(ev) {
|
|
53
|
-
ring.push(ev);
|
|
54
|
-
while (ring.length > RING_MAX) ring.shift();
|
|
55
|
-
deliver(ev);
|
|
56
|
-
for (const fn of [...listeners]) {
|
|
57
|
-
try {
|
|
58
|
-
fn(ev);
|
|
59
|
-
} catch {
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
return ev;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export function onEvent(fn) {
|
|
66
|
-
listeners.add(fn);
|
|
67
|
-
return () => listeners.delete(fn);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export function publishEphemeral(jobId, payload) {
|
|
71
|
-
const ev = { event: 'progress', jobId, ts: new Date().toISOString(), ...payload };
|
|
72
|
-
const targets = deliverTargets(jobId);
|
|
73
|
-
if (targets.size) writeAll(targets, ev);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export function publishLive(event, payload) {
|
|
77
|
-
if (!allSubscribers.size) return;
|
|
78
|
-
writeAll(allSubscribers, { event, ts: new Date().toISOString(), ...payload });
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
export function subscribe(sock, jobId) {
|
|
82
|
-
if (jobId === '*') {
|
|
83
|
-
allSubscribers.add(sock);
|
|
84
|
-
return;
|
|
85
|
-
}
|
|
86
|
-
if (!subscribers.has(jobId)) subscribers.set(jobId, new Set());
|
|
87
|
-
subscribers.get(jobId).add(sock);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
export function unsubscribe(sock, jobId) {
|
|
91
|
-
if (jobId === '*') {
|
|
92
|
-
allSubscribers.delete(sock);
|
|
93
|
-
return;
|
|
94
|
-
}
|
|
95
|
-
const set = subscribers.get(jobId);
|
|
96
|
-
if (!set) return;
|
|
97
|
-
set.delete(sock);
|
|
98
|
-
if (set.size === 0) subscribers.delete(jobId);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
export function eventsSince(afterId, limit = 500) {
|
|
102
|
-
if (!afterId) return { reset: false, epoch: EPOCH, events: ring.slice(-limit), hasMore: ring.length > limit };
|
|
103
|
-
|
|
104
|
-
const dash = String(afterId).lastIndexOf('-');
|
|
105
|
-
const epoch = dash > 0 ? String(afterId).slice(0, dash) : null;
|
|
106
|
-
const fromSeq = dash > 0 ? Number(String(afterId).slice(dash + 1)) : NaN;
|
|
107
|
-
|
|
108
|
-
if (epoch !== EPOCH || !Number.isFinite(fromSeq)) {
|
|
109
|
-
// Different daemon lifetime: the caller must reload history from receipts.
|
|
110
|
-
return { reset: true, epoch: EPOCH, events: [], reason: 'epoch-mismatch' };
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
const events = ring.filter((e) => e.seq > fromSeq).slice(0, limit);
|
|
114
|
-
return { reset: false, epoch: EPOCH, events, hasMore: events.length === limit };
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
export const addClient = (sock) => clients.add(sock);
|
|
118
|
-
|
|
119
|
-
export function dropClient(sock) {
|
|
120
|
-
clients.delete(sock);
|
|
121
|
-
allSubscribers.delete(sock);
|
|
122
|
-
for (const [jobId, set] of subscribers) {
|
|
123
|
-
set.delete(sock);
|
|
124
|
-
if (set.size === 0) subscribers.delete(jobId);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
|
|
3
|
+
export const EPOCH = randomUUID().slice(0, 8);
|
|
4
|
+
export const RING_MAX = 2000;
|
|
5
|
+
const ring = [];
|
|
6
|
+
let seq = 0;
|
|
7
|
+
|
|
8
|
+
const subscribers = new Map();
|
|
9
|
+
export const allSubscribers = new Set();
|
|
10
|
+
const clients = new Set();
|
|
11
|
+
const listeners = new Set();
|
|
12
|
+
const MAX_BACKLOG = 32 * 1024 * 1024;
|
|
13
|
+
|
|
14
|
+
function writeAll(socks, payload) {
|
|
15
|
+
const line = JSON.stringify(payload) + '\n';
|
|
16
|
+
for (const sock of socks) {
|
|
17
|
+
try {
|
|
18
|
+
if (sock.destroyed) continue;
|
|
19
|
+
if (sock.writableLength > MAX_BACKLOG) {
|
|
20
|
+
sock.destroy();
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
sock.write(line);
|
|
24
|
+
} catch {
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function allocEvent(type, payload = {}) {
|
|
30
|
+
seq += 1;
|
|
31
|
+
return { id: `${EPOCH}-${seq}`, seq, epoch: EPOCH, event: type, ts: new Date().toISOString(), ...payload };
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function deliverTargets(jobId) {
|
|
35
|
+
const jobSet = subscribers.get(jobId);
|
|
36
|
+
if (!jobSet || jobSet.size === 0) return allSubscribers;
|
|
37
|
+
if (allSubscribers.size === 0) return jobSet;
|
|
38
|
+
const merged = new Set(jobSet);
|
|
39
|
+
for (const sock of allSubscribers) merged.add(sock);
|
|
40
|
+
return merged;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function deliver(ev) {
|
|
44
|
+
if (!ev.jobId) return writeAll(clients, ev);
|
|
45
|
+
const targets = deliverTargets(ev.jobId);
|
|
46
|
+
if (targets.size) writeAll(targets, ev);
|
|
47
|
+
if (ev.event === 'job.done' || ev.event === 'job.interrupted') {
|
|
48
|
+
subscribers.delete(ev.jobId);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function publish(ev) {
|
|
53
|
+
ring.push(ev);
|
|
54
|
+
while (ring.length > RING_MAX) ring.shift();
|
|
55
|
+
deliver(ev);
|
|
56
|
+
for (const fn of [...listeners]) {
|
|
57
|
+
try {
|
|
58
|
+
fn(ev);
|
|
59
|
+
} catch {
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return ev;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function onEvent(fn) {
|
|
66
|
+
listeners.add(fn);
|
|
67
|
+
return () => listeners.delete(fn);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function publishEphemeral(jobId, payload) {
|
|
71
|
+
const ev = { event: 'progress', jobId, ts: new Date().toISOString(), ...payload };
|
|
72
|
+
const targets = deliverTargets(jobId);
|
|
73
|
+
if (targets.size) writeAll(targets, ev);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function publishLive(event, payload) {
|
|
77
|
+
if (!allSubscribers.size) return;
|
|
78
|
+
writeAll(allSubscribers, { event, ts: new Date().toISOString(), ...payload });
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function subscribe(sock, jobId) {
|
|
82
|
+
if (jobId === '*') {
|
|
83
|
+
allSubscribers.add(sock);
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
if (!subscribers.has(jobId)) subscribers.set(jobId, new Set());
|
|
87
|
+
subscribers.get(jobId).add(sock);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function unsubscribe(sock, jobId) {
|
|
91
|
+
if (jobId === '*') {
|
|
92
|
+
allSubscribers.delete(sock);
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
const set = subscribers.get(jobId);
|
|
96
|
+
if (!set) return;
|
|
97
|
+
set.delete(sock);
|
|
98
|
+
if (set.size === 0) subscribers.delete(jobId);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export function eventsSince(afterId, limit = 500) {
|
|
102
|
+
if (!afterId) return { reset: false, epoch: EPOCH, events: ring.slice(-limit), hasMore: ring.length > limit };
|
|
103
|
+
|
|
104
|
+
const dash = String(afterId).lastIndexOf('-');
|
|
105
|
+
const epoch = dash > 0 ? String(afterId).slice(0, dash) : null;
|
|
106
|
+
const fromSeq = dash > 0 ? Number(String(afterId).slice(dash + 1)) : NaN;
|
|
107
|
+
|
|
108
|
+
if (epoch !== EPOCH || !Number.isFinite(fromSeq)) {
|
|
109
|
+
// Different daemon lifetime: the caller must reload history from receipts.
|
|
110
|
+
return { reset: true, epoch: EPOCH, events: [], reason: 'epoch-mismatch' };
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const events = ring.filter((e) => e.seq > fromSeq).slice(0, limit);
|
|
114
|
+
return { reset: false, epoch: EPOCH, events, hasMore: events.length === limit };
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export const addClient = (sock) => clients.add(sock);
|
|
118
|
+
|
|
119
|
+
export function dropClient(sock) {
|
|
120
|
+
clients.delete(sock);
|
|
121
|
+
allSubscribers.delete(sock);
|
|
122
|
+
for (const [jobId, set] of subscribers) {
|
|
123
|
+
set.delete(sock);
|
|
124
|
+
if (set.size === 0) subscribers.delete(jobId);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
@@ -1,127 +1,127 @@
|
|
|
1
|
-
import { appendAudit } from '../core/records/index.mjs';
|
|
2
|
-
import * as threads from '../core/threads/index.mjs';
|
|
3
|
-
|
|
4
|
-
export const THREAD_ARG_OPS = new Set([
|
|
5
|
-
'thread.get',
|
|
6
|
-
'thread.update',
|
|
7
|
-
'thread.remove',
|
|
8
|
-
'thread.start',
|
|
9
|
-
'thread.probe',
|
|
10
|
-
'thread.sessions',
|
|
11
|
-
'thread.use',
|
|
12
|
-
'thread.new',
|
|
13
|
-
'thread.deleteSession',
|
|
14
|
-
'thread.models',
|
|
15
|
-
'thread.setModel',
|
|
16
|
-
'thread.transcript',
|
|
17
|
-
'thread.queue',
|
|
18
|
-
'thread.queue.clear',
|
|
19
|
-
'thread.promote',
|
|
20
|
-
'thread.cancel',
|
|
21
|
-
'run',
|
|
22
|
-
'tail',
|
|
23
|
-
]);
|
|
24
|
-
|
|
25
|
-
// Fail-closed allowlist for managed child agents (`callerAgent` present via HARNESS_BUS_MANAGED_AGENT).
|
|
26
|
-
// Child executors cannot touch provider secrets, delete/mutate threads, edit roles, inspect audits, or shut down the daemon.
|
|
27
|
-
export const MANAGED_AGENT_ALLOWED = new Set([
|
|
28
|
-
'hello',
|
|
29
|
-
'subscribe',
|
|
30
|
-
'unsubscribe',
|
|
31
|
-
'thread.list',
|
|
32
|
-
'thread.get',
|
|
33
|
-
'thread.resolve',
|
|
34
|
-
'thread.transcript',
|
|
35
|
-
'thread.queue',
|
|
36
|
-
'thread.cancel',
|
|
37
|
-
'run',
|
|
38
|
-
'fanout',
|
|
39
|
-
'tail',
|
|
40
|
-
'wait',
|
|
41
|
-
'role.list',
|
|
42
|
-
'role.get',
|
|
43
|
-
'provider.list',
|
|
44
|
-
'cli.models',
|
|
45
|
-
'cli.effort',
|
|
46
|
-
'health',
|
|
47
|
-
'receipts',
|
|
48
|
-
]);
|
|
49
|
-
|
|
50
|
-
function deny(actor, project, op, target, detail) {
|
|
51
|
-
appendAudit({ actor, project: project ?? null, op, target: target ?? null, status: 'denied', detail });
|
|
52
|
-
throw new Error(detail);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
function resolveCallerWorkspace(callerAgent) {
|
|
56
|
-
if (!callerAgent) return null;
|
|
57
|
-
const found = threads.findThreadByKey(callerAgent);
|
|
58
|
-
if (found) return found.ws;
|
|
59
|
-
const sepAt = callerAgent.indexOf(threads.SEP);
|
|
60
|
-
if (sepAt > 0) return threads.getWorkspace(callerAgent.slice(0, sepAt));
|
|
61
|
-
return null;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
function checkManagedCaller(op, args) {
|
|
65
|
-
const callerAgent = args?.callerAgent ? String(args.callerAgent).trim() : '';
|
|
66
|
-
const isManaged = Boolean(callerAgent) || args?.caller === 'managed';
|
|
67
|
-
if (!isManaged) return { isManaged: false, actor: args?.caller ?? 'user', callerWs: null };
|
|
68
|
-
|
|
69
|
-
const actor = callerAgent ? `agent:${callerAgent}` : 'managed';
|
|
70
|
-
if (!MANAGED_AGENT_ALLOWED.has(op)) {
|
|
71
|
-
deny(actor, null, op, args?.thread ?? args?.agent ?? args?.id ?? null, `managed child agent "${actor}" is not allowed to call "${op}"`);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
const callerWs = resolveCallerWorkspace(callerAgent);
|
|
75
|
-
const needsWorkspaceScope = op === 'thread.resolve' || op === 'run' || op === 'fanout' || op === 'thread.cancel';
|
|
76
|
-
|
|
77
|
-
if (needsWorkspaceScope) {
|
|
78
|
-
const targetKey = args?.thread ?? args?.agent ?? null;
|
|
79
|
-
if (targetKey) {
|
|
80
|
-
const targetFound = threads.findThreadByKey(targetKey);
|
|
81
|
-
if (targetFound && (!callerWs || targetFound.ws.id !== callerWs.id)) {
|
|
82
|
-
deny(actor, callerWs?.id ?? null, op, targetKey, `managed child agent "${actor}" cannot access thread outside its workspace`);
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
const checkDirInsideCaller = (rawPath, label) => {
|
|
86
|
-
if (!rawPath) return null;
|
|
87
|
-
if (!callerWs) {
|
|
88
|
-
deny(actor, null, op, String(rawPath), `managed child agent "${actor}" has no registered workspace scope for ${label}`);
|
|
89
|
-
}
|
|
90
|
-
try {
|
|
91
|
-
return threads.resolveInside(callerWs.root, rawPath);
|
|
92
|
-
} catch (err) {
|
|
93
|
-
deny(actor, callerWs.id, op, String(rawPath), err?.message ?? String(err));
|
|
94
|
-
}
|
|
95
|
-
};
|
|
96
|
-
if (args?.cwd) {
|
|
97
|
-
const safeCwd = checkDirInsideCaller(args.cwd, 'cwd');
|
|
98
|
-
if (args?.dir && safeCwd) checkDirInsideCaller(threads.resolveInside(safeCwd, args.dir), 'dir');
|
|
99
|
-
}
|
|
100
|
-
if (op === 'fanout' && Array.isArray(args?.targets)) {
|
|
101
|
-
for (const item of args.targets) {
|
|
102
|
-
if (item && typeof item === 'object' && item.cwd) {
|
|
103
|
-
checkDirInsideCaller(item.cwd, 'fanout target cwd');
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
return { isManaged: true, actor, callerWs };
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
export function scopeArgs(op, args) {
|
|
113
|
-
const { actor } = checkManagedCaller(op, args);
|
|
114
|
-
const threadKey = args?.thread ?? args?.agent ?? null;
|
|
115
|
-
if (!threadKey || !THREAD_ARG_OPS.has(op)) return args;
|
|
116
|
-
|
|
117
|
-
const found = threads.findThreadByKey(threadKey);
|
|
118
|
-
if (found && args?.cwd) {
|
|
119
|
-
try {
|
|
120
|
-
const safeCwd = threads.resolveInside(found.ws.root, args.cwd);
|
|
121
|
-
return { ...args, thread: found.key, agent: found.key, cwd: safeCwd };
|
|
122
|
-
} catch (err) {
|
|
123
|
-
deny(actor, found.ws.id, op, String(args.cwd), err?.message ?? String(err));
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
return args;
|
|
127
|
-
}
|
|
1
|
+
import { appendAudit } from '../core/records/index.mjs';
|
|
2
|
+
import * as threads from '../core/threads/index.mjs';
|
|
3
|
+
|
|
4
|
+
export const THREAD_ARG_OPS = new Set([
|
|
5
|
+
'thread.get',
|
|
6
|
+
'thread.update',
|
|
7
|
+
'thread.remove',
|
|
8
|
+
'thread.start',
|
|
9
|
+
'thread.probe',
|
|
10
|
+
'thread.sessions',
|
|
11
|
+
'thread.use',
|
|
12
|
+
'thread.new',
|
|
13
|
+
'thread.deleteSession',
|
|
14
|
+
'thread.models',
|
|
15
|
+
'thread.setModel',
|
|
16
|
+
'thread.transcript',
|
|
17
|
+
'thread.queue',
|
|
18
|
+
'thread.queue.clear',
|
|
19
|
+
'thread.promote',
|
|
20
|
+
'thread.cancel',
|
|
21
|
+
'run',
|
|
22
|
+
'tail',
|
|
23
|
+
]);
|
|
24
|
+
|
|
25
|
+
// Fail-closed allowlist for managed child agents (`callerAgent` present via HARNESS_BUS_MANAGED_AGENT).
|
|
26
|
+
// Child executors cannot touch provider secrets, delete/mutate threads, edit roles, inspect audits, or shut down the daemon.
|
|
27
|
+
export const MANAGED_AGENT_ALLOWED = new Set([
|
|
28
|
+
'hello',
|
|
29
|
+
'subscribe',
|
|
30
|
+
'unsubscribe',
|
|
31
|
+
'thread.list',
|
|
32
|
+
'thread.get',
|
|
33
|
+
'thread.resolve',
|
|
34
|
+
'thread.transcript',
|
|
35
|
+
'thread.queue',
|
|
36
|
+
'thread.cancel',
|
|
37
|
+
'run',
|
|
38
|
+
'fanout',
|
|
39
|
+
'tail',
|
|
40
|
+
'wait',
|
|
41
|
+
'role.list',
|
|
42
|
+
'role.get',
|
|
43
|
+
'provider.list',
|
|
44
|
+
'cli.models',
|
|
45
|
+
'cli.effort',
|
|
46
|
+
'health',
|
|
47
|
+
'receipts',
|
|
48
|
+
]);
|
|
49
|
+
|
|
50
|
+
function deny(actor, project, op, target, detail) {
|
|
51
|
+
appendAudit({ actor, project: project ?? null, op, target: target ?? null, status: 'denied', detail });
|
|
52
|
+
throw new Error(detail);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function resolveCallerWorkspace(callerAgent) {
|
|
56
|
+
if (!callerAgent) return null;
|
|
57
|
+
const found = threads.findThreadByKey(callerAgent);
|
|
58
|
+
if (found) return found.ws;
|
|
59
|
+
const sepAt = callerAgent.indexOf(threads.SEP);
|
|
60
|
+
if (sepAt > 0) return threads.getWorkspace(callerAgent.slice(0, sepAt));
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function checkManagedCaller(op, args) {
|
|
65
|
+
const callerAgent = args?.callerAgent ? String(args.callerAgent).trim() : '';
|
|
66
|
+
const isManaged = Boolean(callerAgent) || args?.caller === 'managed';
|
|
67
|
+
if (!isManaged) return { isManaged: false, actor: args?.caller ?? 'user', callerWs: null };
|
|
68
|
+
|
|
69
|
+
const actor = callerAgent ? `agent:${callerAgent}` : 'managed';
|
|
70
|
+
if (!MANAGED_AGENT_ALLOWED.has(op)) {
|
|
71
|
+
deny(actor, null, op, args?.thread ?? args?.agent ?? args?.id ?? null, `managed child agent "${actor}" is not allowed to call "${op}"`);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const callerWs = resolveCallerWorkspace(callerAgent);
|
|
75
|
+
const needsWorkspaceScope = op === 'thread.resolve' || op === 'run' || op === 'fanout' || op === 'thread.cancel';
|
|
76
|
+
|
|
77
|
+
if (needsWorkspaceScope) {
|
|
78
|
+
const targetKey = args?.thread ?? args?.agent ?? null;
|
|
79
|
+
if (targetKey) {
|
|
80
|
+
const targetFound = threads.findThreadByKey(targetKey);
|
|
81
|
+
if (targetFound && (!callerWs || targetFound.ws.id !== callerWs.id)) {
|
|
82
|
+
deny(actor, callerWs?.id ?? null, op, targetKey, `managed child agent "${actor}" cannot access thread outside its workspace`);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
const checkDirInsideCaller = (rawPath, label) => {
|
|
86
|
+
if (!rawPath) return null;
|
|
87
|
+
if (!callerWs) {
|
|
88
|
+
deny(actor, null, op, String(rawPath), `managed child agent "${actor}" has no registered workspace scope for ${label}`);
|
|
89
|
+
}
|
|
90
|
+
try {
|
|
91
|
+
return threads.resolveInside(callerWs.root, rawPath);
|
|
92
|
+
} catch (err) {
|
|
93
|
+
deny(actor, callerWs.id, op, String(rawPath), err?.message ?? String(err));
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
if (args?.cwd) {
|
|
97
|
+
const safeCwd = checkDirInsideCaller(args.cwd, 'cwd');
|
|
98
|
+
if (args?.dir && safeCwd) checkDirInsideCaller(threads.resolveInside(safeCwd, args.dir), 'dir');
|
|
99
|
+
}
|
|
100
|
+
if (op === 'fanout' && Array.isArray(args?.targets)) {
|
|
101
|
+
for (const item of args.targets) {
|
|
102
|
+
if (item && typeof item === 'object' && item.cwd) {
|
|
103
|
+
checkDirInsideCaller(item.cwd, 'fanout target cwd');
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return { isManaged: true, actor, callerWs };
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export function scopeArgs(op, args) {
|
|
113
|
+
const { actor } = checkManagedCaller(op, args);
|
|
114
|
+
const threadKey = args?.thread ?? args?.agent ?? null;
|
|
115
|
+
if (!threadKey || !THREAD_ARG_OPS.has(op)) return args;
|
|
116
|
+
|
|
117
|
+
const found = threads.findThreadByKey(threadKey);
|
|
118
|
+
if (found && args?.cwd) {
|
|
119
|
+
try {
|
|
120
|
+
const safeCwd = threads.resolveInside(found.ws.root, args.cwd);
|
|
121
|
+
return { ...args, thread: found.key, agent: found.key, cwd: safeCwd };
|
|
122
|
+
} catch (err) {
|
|
123
|
+
deny(actor, found.ws.id, op, String(args.cwd), err?.message ?? String(err));
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return args;
|
|
127
|
+
}
|
|
@@ -1,61 +1,61 @@
|
|
|
1
|
-
import { rmSync } from 'node:fs';
|
|
2
|
-
|
|
3
|
-
const SHUTDOWN_MS = 10_000;
|
|
4
|
-
|
|
5
|
-
export function startLifecycle({ endpointFile, socketPath = null, pool, sweepIdle, stats, idleMs, idleInstanceMs, log }) {
|
|
6
|
-
let ending = null;
|
|
7
|
-
function shutdown(code) {
|
|
8
|
-
if (ending) return ending;
|
|
9
|
-
try {
|
|
10
|
-
rmSync(endpointFile, { force: true });
|
|
11
|
-
} catch {
|
|
12
|
-
}
|
|
13
|
-
if (socketPath && process.platform !== 'win32') {
|
|
14
|
-
try {
|
|
15
|
-
rmSync(socketPath, { force: true });
|
|
16
|
-
} catch {
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
const force = setTimeout(() => {
|
|
20
|
-
log(`some processes did not stop within ${SHUTDOWN_MS / 1000}s; exiting anyway`);
|
|
21
|
-
process.exit(code);
|
|
22
|
-
}, SHUTDOWN_MS);
|
|
23
|
-
ending = Promise.all([...pool.values()].map((a) => a.shutdown().catch(() => {}))).then(() => {
|
|
24
|
-
clearTimeout(force);
|
|
25
|
-
process.exit(code);
|
|
26
|
-
});
|
|
27
|
-
return ending;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
let sweeping = false;
|
|
31
|
-
setInterval(async () => {
|
|
32
|
-
if (sweeping || ending) return;
|
|
33
|
-
sweeping = true;
|
|
34
|
-
try {
|
|
35
|
-
const stopped = await sweepIdle(idleInstanceMs);
|
|
36
|
-
if (stopped.length) log(`idle, stopped: ${stopped.join(', ')}`);
|
|
37
|
-
} catch (err) {
|
|
38
|
-
log('idle sweep failed:', err.message);
|
|
39
|
-
} finally {
|
|
40
|
-
sweeping = false;
|
|
41
|
-
}
|
|
42
|
-
}, 60_000).unref();
|
|
43
|
-
|
|
44
|
-
setInterval(() => {
|
|
45
|
-
const busy = [...pool.values()].some((a) => a.busy);
|
|
46
|
-
if (!ending && stats.connections === 0 && !busy && Date.now() - stats.lastActivity > idleMs) {
|
|
47
|
-
log('idle; shutting down');
|
|
48
|
-
shutdown(0);
|
|
49
|
-
}
|
|
50
|
-
}, 30_000).unref();
|
|
51
|
-
|
|
52
|
-
for (const sig of ['SIGINT', 'SIGTERM']) process.on(sig, () => shutdown(0));
|
|
53
|
-
process.on('uncaughtException', (e) => {
|
|
54
|
-
log('uncaught:', e?.stack ?? String(e));
|
|
55
|
-
});
|
|
56
|
-
process.on('unhandledRejection', (e) => {
|
|
57
|
-
log('unhandledRejection:', e?.stack ?? String(e));
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
return { shutdown };
|
|
61
|
-
}
|
|
1
|
+
import { rmSync } from 'node:fs';
|
|
2
|
+
|
|
3
|
+
const SHUTDOWN_MS = 10_000;
|
|
4
|
+
|
|
5
|
+
export function startLifecycle({ endpointFile, socketPath = null, pool, sweepIdle, stats, idleMs, idleInstanceMs, log }) {
|
|
6
|
+
let ending = null;
|
|
7
|
+
function shutdown(code) {
|
|
8
|
+
if (ending) return ending;
|
|
9
|
+
try {
|
|
10
|
+
rmSync(endpointFile, { force: true });
|
|
11
|
+
} catch {
|
|
12
|
+
}
|
|
13
|
+
if (socketPath && process.platform !== 'win32') {
|
|
14
|
+
try {
|
|
15
|
+
rmSync(socketPath, { force: true });
|
|
16
|
+
} catch {
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
const force = setTimeout(() => {
|
|
20
|
+
log(`some processes did not stop within ${SHUTDOWN_MS / 1000}s; exiting anyway`);
|
|
21
|
+
process.exit(code);
|
|
22
|
+
}, SHUTDOWN_MS);
|
|
23
|
+
ending = Promise.all([...pool.values()].map((a) => a.shutdown().catch(() => {}))).then(() => {
|
|
24
|
+
clearTimeout(force);
|
|
25
|
+
process.exit(code);
|
|
26
|
+
});
|
|
27
|
+
return ending;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
let sweeping = false;
|
|
31
|
+
setInterval(async () => {
|
|
32
|
+
if (sweeping || ending) return;
|
|
33
|
+
sweeping = true;
|
|
34
|
+
try {
|
|
35
|
+
const stopped = await sweepIdle(idleInstanceMs);
|
|
36
|
+
if (stopped.length) log(`idle, stopped: ${stopped.join(', ')}`);
|
|
37
|
+
} catch (err) {
|
|
38
|
+
log('idle sweep failed:', err.message);
|
|
39
|
+
} finally {
|
|
40
|
+
sweeping = false;
|
|
41
|
+
}
|
|
42
|
+
}, 60_000).unref();
|
|
43
|
+
|
|
44
|
+
setInterval(() => {
|
|
45
|
+
const busy = [...pool.values()].some((a) => a.busy);
|
|
46
|
+
if (!ending && stats.connections === 0 && !busy && Date.now() - stats.lastActivity > idleMs) {
|
|
47
|
+
log('idle; shutting down');
|
|
48
|
+
shutdown(0);
|
|
49
|
+
}
|
|
50
|
+
}, 30_000).unref();
|
|
51
|
+
|
|
52
|
+
for (const sig of ['SIGINT', 'SIGTERM']) process.on(sig, () => shutdown(0));
|
|
53
|
+
process.on('uncaughtException', (e) => {
|
|
54
|
+
log('uncaught:', e?.stack ?? String(e));
|
|
55
|
+
});
|
|
56
|
+
process.on('unhandledRejection', (e) => {
|
|
57
|
+
log('unhandledRejection:', e?.stack ?? String(e));
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
return { shutdown };
|
|
61
|
+
}
|
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import { providerOps } from './providers.mjs';
|
|
2
|
-
import { runOps } from './run.mjs';
|
|
3
|
-
import { systemOps } from './system.mjs';
|
|
4
|
-
import { threadOps } from './threads.mjs';
|
|
5
|
-
|
|
6
|
-
export function buildOps(ctx) {
|
|
7
|
-
const ops = {};
|
|
8
|
-
for (const part of [threadOps(ctx), runOps(ctx), providerOps(ctx), systemOps(ctx)]) {
|
|
9
|
-
for (const [name, fn] of Object.entries(part)) {
|
|
10
|
-
if (name in ops) throw new Error(`operation "${name}" is defined twice`);
|
|
11
|
-
ops[name] = fn;
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
return ops;
|
|
15
|
-
}
|
|
1
|
+
import { providerOps } from './providers.mjs';
|
|
2
|
+
import { runOps } from './run.mjs';
|
|
3
|
+
import { systemOps } from './system.mjs';
|
|
4
|
+
import { threadOps } from './threads.mjs';
|
|
5
|
+
|
|
6
|
+
export function buildOps(ctx) {
|
|
7
|
+
const ops = {};
|
|
8
|
+
for (const part of [threadOps(ctx), runOps(ctx), providerOps(ctx), systemOps(ctx)]) {
|
|
9
|
+
for (const [name, fn] of Object.entries(part)) {
|
|
10
|
+
if (name in ops) throw new Error(`operation "${name}" is defined twice`);
|
|
11
|
+
ops[name] = fn;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
return ops;
|
|
15
|
+
}
|