agents-relay 2.0.198 → 2.0.200
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.
|
@@ -1,11 +1,23 @@
|
|
|
1
|
-
import { randomUUID } from 'node:crypto';
|
|
2
|
-
import { writeFile, unlink } from 'node:fs/promises';
|
|
3
1
|
import { join } from 'node:path';
|
|
4
2
|
import { homedir } from 'node:os';
|
|
5
3
|
import { spawnOwnedProcess } from './owned-process.js';
|
|
6
4
|
import { BrowserWorkerPoolManager, BrowserPoolAdmissionError } from './browser-worker-pool.js';
|
|
7
5
|
import { monitorPoolDecision, progressPoolDecision } from './browser-worker-pool-graph.js';
|
|
6
|
+
import { webHelmDaemonRequest } from './webhelm-daemon-client.js';
|
|
8
7
|
const WORKSPACE = 'Neo';
|
|
8
|
+
const WORKSPACE_CAPACITY = 6;
|
|
9
|
+
let pendingWorkspaceProvision;
|
|
10
|
+
async function ensureNeoWorkspace() {
|
|
11
|
+
if (!pendingWorkspaceProvision) {
|
|
12
|
+
pendingWorkspaceProvision = (async () => {
|
|
13
|
+
// Shared setup must outlive cancellation by one waiting Worker.
|
|
14
|
+
await cliCommand(['ensure']);
|
|
15
|
+
await webHelmDaemonRequest({ op: 'workspace-create', name: WORKSPACE, pool_size: WORKSPACE_CAPACITY });
|
|
16
|
+
})();
|
|
17
|
+
void pendingWorkspaceProvision.finally(() => { pendingWorkspaceProvision = undefined; }).catch(() => { });
|
|
18
|
+
}
|
|
19
|
+
await pendingWorkspaceProvision;
|
|
20
|
+
}
|
|
9
21
|
const OUTPUT_LIMIT = 2_000_000;
|
|
10
22
|
let manager;
|
|
11
23
|
export const browserWorkerPool = () => manager ??= new BrowserWorkerPoolManager();
|
|
@@ -63,14 +75,39 @@ function parseEnvelope(value) {
|
|
|
63
75
|
throw Error('WebHelm returned no structured result');
|
|
64
76
|
}
|
|
65
77
|
async function configRun(provider, action, sessionId, config, signal) {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
78
|
+
return parseEnvelope(JSON.stringify(await webHelmDaemonRequest({
|
|
79
|
+
op: 'platform-run', platform: provider, action, session_id: sessionId, config,
|
|
80
|
+
auto_session: false, workspace: WORKSPACE,
|
|
81
|
+
}, { signal, timeoutMs: 150_000 })));
|
|
82
|
+
}
|
|
83
|
+
/** Verify release by exact Session ID; a transient navigation error may
|
|
84
|
+
* already have returned the Tab to helm.html but left daemon ownership active.
|
|
85
|
+
* Never free the Relay lease without stop/read-back evidence. */
|
|
86
|
+
async function stopWebHelmSessionVerified(sessionId) {
|
|
87
|
+
let lastError = new Error('WebHelm Session release was not verified');
|
|
88
|
+
for (let attempt = 0; attempt < 2; attempt++) {
|
|
89
|
+
try {
|
|
90
|
+
const result = await webHelmDaemonRequest({ op: 'stop', session_id: sessionId }, { timeoutMs: 45_000 });
|
|
91
|
+
if (result.returned_to_idle === true || result.already_closed === true)
|
|
92
|
+
return;
|
|
93
|
+
lastError = new Error('WebHelm stop returned no verified idle tab receipt');
|
|
94
|
+
}
|
|
95
|
+
catch (error) {
|
|
96
|
+
lastError = error instanceof Error ? error : new Error(String(error));
|
|
97
|
+
}
|
|
98
|
+
// Idempotent recovery: a lost response is not a failed stop if the
|
|
99
|
+
// daemon independently confirms this exact Session is no longer owned.
|
|
100
|
+
try {
|
|
101
|
+
const state = await webHelmDaemonRequest({ op: 'list' }, { timeoutMs: 15_000 });
|
|
102
|
+
const sessions = state.sessions;
|
|
103
|
+
if (Array.isArray(sessions) && !sessions.some(row => typeof row === 'object' && row !== null && row.session_id === sessionId))
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
catch { /* no proof: keep the original lease */ }
|
|
107
|
+
if (attempt === 0)
|
|
108
|
+
await new Promise(resolve => setTimeout(resolve, 250));
|
|
73
109
|
}
|
|
110
|
+
throw lastError;
|
|
74
111
|
}
|
|
75
112
|
/** Poll only real assistant output, not page uptime or the browser keepalive. */
|
|
76
113
|
const OBSERVE = `const url=(await page_info()).url;
|
|
@@ -84,10 +121,12 @@ export function pooledWebHelmRun(request, signal) {
|
|
|
84
121
|
let released = false;
|
|
85
122
|
let stopped = false;
|
|
86
123
|
let monitor;
|
|
124
|
+
let heartbeatTimer;
|
|
125
|
+
let pendingHeartbeat;
|
|
87
126
|
let completionRecoveryTimer;
|
|
88
127
|
let pendingProbe, pendingCompletionRecovery;
|
|
89
128
|
let stopping;
|
|
90
|
-
let
|
|
129
|
+
let observedAt = Date.now(), lastFingerprint = '', sampled = false;
|
|
91
130
|
let recoveries = 0, monitoring = false, threadId;
|
|
92
131
|
let submissionAttempted = false, verifiedSubmitted = false, uncertain = false, recoveryOutcomeUncertain = false;
|
|
93
132
|
let providerPreflightFailed = false;
|
|
@@ -97,12 +136,14 @@ export function pooledWebHelmRun(request, signal) {
|
|
|
97
136
|
stopped = true;
|
|
98
137
|
if (monitor)
|
|
99
138
|
clearInterval(monitor);
|
|
139
|
+
if (heartbeatTimer)
|
|
140
|
+
clearInterval(heartbeatTimer);
|
|
100
141
|
if (completionRecoveryTimer)
|
|
101
142
|
clearTimeout(completionRecoveryTimer);
|
|
102
143
|
stopping = (async () => {
|
|
103
144
|
// Clearing the interval does not cancel an already running WebHelm
|
|
104
145
|
// command. Drain it before closing the session or releasing the slot.
|
|
105
|
-
await Promise.allSettled([pendingProbe, pendingCompletionRecovery]);
|
|
146
|
+
await Promise.allSettled([pendingProbe, pendingCompletionRecovery, pendingHeartbeat]);
|
|
106
147
|
if (!lease)
|
|
107
148
|
return;
|
|
108
149
|
if (uncertain) {
|
|
@@ -114,7 +155,7 @@ export function pooledWebHelmRun(request, signal) {
|
|
|
114
155
|
return;
|
|
115
156
|
}
|
|
116
157
|
try {
|
|
117
|
-
await
|
|
158
|
+
await stopWebHelmSessionVerified(sessionId);
|
|
118
159
|
pool.release(lease);
|
|
119
160
|
}
|
|
120
161
|
catch {
|
|
@@ -125,13 +166,19 @@ export function pooledWebHelmRun(request, signal) {
|
|
|
125
166
|
};
|
|
126
167
|
const release = async () => { released = true; if (lease)
|
|
127
168
|
await stop(); };
|
|
128
|
-
const cancel = () => {
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
169
|
+
const cancel = () => {
|
|
170
|
+
released = true;
|
|
171
|
+
if (!lease)
|
|
172
|
+
return;
|
|
173
|
+
if (verifiedSubmitted || submissionAttempted) {
|
|
174
|
+
uncertain = true;
|
|
175
|
+
pool.quarantine(lease);
|
|
176
|
+
}
|
|
177
|
+
// Cancel must halt keepalives and probes even when delivery is uncertain.
|
|
178
|
+
// Leave the quarantined Session open; WebHelm can reclaim it on demand
|
|
179
|
+
// after its last_used_at expires without falsely freeing the Relay slot.
|
|
180
|
+
void stop().catch(() => { });
|
|
181
|
+
};
|
|
135
182
|
const sendRecovery = async () => {
|
|
136
183
|
if (stopped || released || uncertain || recoveryOutcomeUncertain || !sessionId || !threadId || !request.recover || recoveries >= 2)
|
|
137
184
|
return;
|
|
@@ -158,12 +205,9 @@ export function pooledWebHelmRun(request, signal) {
|
|
|
158
205
|
monitoring = true;
|
|
159
206
|
try {
|
|
160
207
|
const now = Date.now();
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
browserKeepAliveAt = now;
|
|
165
|
-
}
|
|
166
|
-
const observation = parseEnvelope(await cliCommand(['session', 'exec', sessionId], OBSERVE));
|
|
208
|
+
const observation = parseEnvelope(JSON.stringify(await webHelmDaemonRequest({
|
|
209
|
+
op: 'exec', session_id: sessionId, code: OBSERVE,
|
|
210
|
+
}, { timeoutMs: 45_000 })));
|
|
167
211
|
if (observation.url && observation.url.split('?')[0].replace(/\/$/, '') !== lease.threadUrl.replace(/\/$/, ''))
|
|
168
212
|
return;
|
|
169
213
|
const current = fingerprint(observation);
|
|
@@ -214,7 +258,8 @@ export function pooledWebHelmRun(request, signal) {
|
|
|
214
258
|
throw Error(`WebHelm platform ${request.provider} missing`);
|
|
215
259
|
let session;
|
|
216
260
|
try {
|
|
217
|
-
|
|
261
|
+
await ensureNeoWorkspace();
|
|
262
|
+
session = await webHelmDaemonRequest({ op: 'start', workspace: WORKSPACE }, { signal, timeoutMs: 60_000 });
|
|
218
263
|
}
|
|
219
264
|
catch (error) {
|
|
220
265
|
providerPreflightFailed = true;
|
|
@@ -224,8 +269,23 @@ export function pooledWebHelmRun(request, signal) {
|
|
|
224
269
|
throw Error('No WebHelm session ID');
|
|
225
270
|
sessionId = session.session_id;
|
|
226
271
|
pool.bindSession(lease, sessionId);
|
|
227
|
-
|
|
228
|
-
|
|
272
|
+
if (session.workspace !== WORKSPACE || session.workspace_supported !== true) {
|
|
273
|
+
throw Error('WebHelm returned a Session outside the provisioned Neo Workspace');
|
|
274
|
+
}
|
|
275
|
+
// Keep session last_used_at fresh without mutating the worker's model progress.
|
|
276
|
+
const heartbeatPeriod = Math.max(1000, Math.min(request.heartbeatIntervalMs ?? 45_000, 60_000));
|
|
277
|
+
heartbeatTimer = setInterval(() => {
|
|
278
|
+
if (stopped || !sessionId || pendingHeartbeat)
|
|
279
|
+
return;
|
|
280
|
+
const active = webHelmDaemonRequest({ op: 'heartbeat', session_id: sessionId }, { timeoutMs: 15_000 })
|
|
281
|
+
.then(reply => { if (reply.alive !== true)
|
|
282
|
+
throw Error('WebHelm rejected Session heartbeat'); })
|
|
283
|
+
.catch(() => { })
|
|
284
|
+
.finally(() => { if (pendingHeartbeat === active)
|
|
285
|
+
pendingHeartbeat = undefined; });
|
|
286
|
+
pendingHeartbeat = active;
|
|
287
|
+
}, heartbeatPeriod);
|
|
288
|
+
heartbeatTimer.unref();
|
|
229
289
|
if (released || signal.aborted)
|
|
230
290
|
throw Error('Browser worker cancelled before submission');
|
|
231
291
|
const cfg = await request.build();
|
|
@@ -280,8 +340,7 @@ export function pooledWebHelmRun(request, signal) {
|
|
|
280
340
|
uncertain = true;
|
|
281
341
|
pool.quarantine(lease);
|
|
282
342
|
}
|
|
283
|
-
|
|
284
|
-
await stop();
|
|
343
|
+
await stop();
|
|
285
344
|
}
|
|
286
345
|
throw rejection;
|
|
287
346
|
}
|
package/dist/dashboard-html.js
CHANGED
|
@@ -324,7 +324,9 @@ function sanitizeGraphSvg(markup){const parsed=new DOMParser().parseFromString(m
|
|
|
324
324
|
function applyGraphPalette(svg){const ns='http://www.w3.org/2000/svg';for(const path of svg.querySelectorAll('path')){if(path.closest('marker')){if(!path.hasAttribute('fill'))path.setAttribute('fill','#65768a');continue;}if(!path.hasAttribute('fill'))path.setAttribute('fill','none');if(!path.hasAttribute('stroke'))path.setAttribute('stroke','#65768a');}for(const node of svg.querySelectorAll('g.node')){const error=node.classList.contains('failure')||node.classList.contains('exception');for(const shape of node.querySelectorAll('rect,polygon,circle,ellipse')){shape.setAttribute('fill',error?'#ffe4e4':'#f9fbff');shape.setAttribute('stroke',error?'#be3030':'#657f9e');shape.setAttribute('stroke-width','1.3');}for(const label of node.querySelectorAll('text'))label.setAttribute('fill',node.classList.contains('drillable')?'#2367b1':error?'#7c1818':'#182536');}for(const rect of svg.querySelectorAll('rect')){if(!rect.closest('g.node')&&!rect.hasAttribute('fill'))rect.setAttribute('fill','#ffffff');}for(const label of svg.querySelectorAll('text'))if(!label.hasAttribute('fill'))label.setAttribute('fill','#182536');}
|
|
325
325
|
async function renderSelectedGraph(){const graph=selectedWorkflowGraph();renderGraphNavigation();document.querySelector('#graph-detail').hidden=true;if(!graph){graphTitle.textContent='Graph not found';graphPath.textContent='';graphDescription.textContent='';graphCanvas.replaceChildren(element('p','error-state',text('Graph not found: '+selectedGraphId+'. Select Entry or another graph.')));return;}graphTitle.textContent=graph.name;graphDescription.textContent=graph.description||'';graphPath.textContent=graph.path;graphCanvas.replaceChildren(element('p','subtle',text('Rendering…')));try{if(!window.mermaid)throw new Error('Mermaid renderer unavailable');if(!window.__relayMermaidReady){window.mermaid.initialize({startOnLoad:false,securityLevel:'strict',flowchart:{htmlLabels:false}});window.__relayMermaidReady=true;}const rendered=await window.mermaid.render('relay-graph-'+graph.id.replace(/[^A-Za-z0-9_-]/g,'-'),graph.source.replace(/^\\s*click\\s+[^\\n]*$/gm,''));if(graph.id!==selectedGraphId)return;const svg=sanitizeGraphSvg(rendered.svg);graphCanvas.replaceChildren(svg);const definitions=graphNodeDefinitions(graph.source);for(const node of svg.querySelectorAll('g.node')){const entry=Array.from(definitions.entries()).find(([key])=>node.id.includes('-flowchart-'+key+'-'));const label=entry?.[1]||'';if(label)addNodeLabel(node,label);const target=entry?.[0];const next=target?graphLinks(graph.source).get(target):null;if(next){interactiveNode(node,()=>navigateGraph(next),'Explore '+target);}else{const match=/^(state|guard|action):\\s*([A-Za-z0-9_.-]+)/.exec(label);if(match)interactiveNode(node,()=>graphDetail(match[1]==='guard'?'Guard':match[1]==='action'?'Action':'State',match[2]),'Inspect '+match[1]+' '+match[2]);}}applyGraphPalette(svg);}catch(error){graphCanvas.replaceChildren(element('p','error-state',text('Visual preview unavailable: '+error.message+'.')));}}
|
|
326
326
|
async function drawGraphs(){try{const graphs=await loadJSON('/api/graphs');workflowGraphs=Array.isArray(graphs)?graphs:[];const requested=graphFromHash();selectedGraphId=requested||'main';graphsMeta.textContent=(workflowGraphs.length-1)+' executable workflow graphs · main is conceptual';renderGraphList();await renderSelectedGraph();}catch(error){graphsMeta.textContent='Unavailable';workflowGraphs=[];selectedGraphId='main';renderGraphList();await renderSelectedGraph();graphPath.textContent='Executable graphs unavailable: '+error.message;}}
|
|
327
|
-
function
|
|
327
|
+
function auditWorker(record){return value(record?.selected?.worker?.adapter??record?.worker?.adapter??record?.selected?.worker?.id??record?.worker?.id);}
|
|
328
|
+
function auditModel(record){const model=record?.selected?.model;return value(typeof model==='string'?model:model?.model);}
|
|
329
|
+
function renderAudit(records){auditLog.replaceChildren();if(!Array.isArray(records)||!records.length){auditLog.append(element('p','empty',text('No schema v5 routing decisions yet.')));return;}for(const record of records){const card=element('article','audit-entry');const head=element('div','audit-entry-head');head.append(element('strong','',text(auditWorker(record)+' · '+auditModel(record))),element('span','subtle',text(new Date(record.timestamp).toLocaleString())));card.append(head);const correlation=record.correlation||{};card.append(element('div','subtle',text([correlation.job_id,correlation.task_id].filter(Boolean).join(' / ')||'uncorrelated')));const probabilities=record.decision_evidence?.probabilities||{};const gates=(record.gate_results||[]).filter(item=>!item.eligible).map(item=>item.candidate_id+': '+item.rejection_reasons.join(', '));const nl=String.fromCharCode(10);const details=document.createElement('pre');details.textContent='fallback: '+String(Boolean(record.decision_evidence?.fallback))+(Object.keys(probabilities).length?nl+'probabilities: '+JSON.stringify(probabilities,null,2):'')+(gates.length?nl+'rejected: '+gates.join(nl):'');card.append(details);auditLog.append(card);}}
|
|
328
330
|
async function drawAudit(){auditRefresh.disabled=true;try{renderAudit(await loadJSON('/api/decision-audit'));}catch(error){auditLog.replaceChildren(element('p','error-state',text('Audit unavailable: '+error.message)));}finally{auditRefresh.disabled=false;}}
|
|
329
331
|
async function drawRules(){rulesStatus.textContent='';try{const rules=await loadJSON('/api/execution-router-rules');rulesText.value=typeof rules.text==='string'?rules.text:'';rulesMeta.textContent=rules.hash&&rules.hash!=='none'?'Loaded · '+rules.hash.slice(0,12):'No custom rules saved';}catch(error){rulesMeta.textContent='Unavailable';rulesStatus.textContent='Could not load rules: '+error.message;}void drawAudit();}
|
|
330
332
|
async function testRouter(){rulesTest.disabled=true;routerTestResults.replaceChildren(element('p','subtle',text('Calling router with current status…')));try{if(!selectedJob||!selectedTaskId)throw new Error('Select a Job and Task first');const response=await fetch('/api/router-test',{method:'POST',headers:{'content-type':'application/json'},body:JSON.stringify({jobId:selected()?.id,taskId:selectedTaskId})});if(!response.ok)throw new Error(response.status+' '+await response.text());const result=await response.json();const routeSelection=result.selected||{};const payload={selected:routeSelection,proposed_fields:result.proposedFields||{},probabilities:result.probabilities||{},fallback:Boolean(result.fallback),effective_choices:result.effectiveChoices||[],routing_instruction:result.routingInstruction||'',merged_choices:result.mergedChoices||[],hard_gates:result.gates||[],laya_input:result.decisionInput||null};routerTestResults.replaceChildren(element('strong','',text((routeSelection.worker||'—')+' · '+(routeSelection.model||'—'))));routerTestResults.append(element('div','subtle',text('route: '+(routeSelection.route||'—'))),element('pre','',text(yamlDump(payload))));}catch(error){routerTestResults.replaceChildren(element('p','error-state',text('Router test failed: '+error.message)));}finally{rulesTest.disabled=false;}}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { createConnection } from 'node:net';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { homedir } from 'node:os';
|
|
4
|
+
const daemonSocket = () => process.env.WEBHELM_SOCKET ||
|
|
5
|
+
join(process.env.WEBHELM_RUNTIME_DIR ||
|
|
6
|
+
join(process.env.WEBHELM_CONFIG_ROOT || join(homedir(), '.config', 'webhelm'), 'runtime'), 'session-node.sock');
|
|
7
|
+
/** Application-owned WebHelm IPC: never provision custom Workspaces via the agent CLI. */
|
|
8
|
+
export function webHelmDaemonRequest(payload, options = {}) {
|
|
9
|
+
return new Promise((resolve, reject) => {
|
|
10
|
+
const socket = createConnection({ path: daemonSocket() });
|
|
11
|
+
const timeoutMs = options.timeoutMs ?? 45_000;
|
|
12
|
+
let buffer = '';
|
|
13
|
+
let settled = false;
|
|
14
|
+
const settle = (error, value) => {
|
|
15
|
+
if (settled)
|
|
16
|
+
return;
|
|
17
|
+
settled = true;
|
|
18
|
+
options.signal?.removeEventListener('abort', onAbort);
|
|
19
|
+
socket.destroy();
|
|
20
|
+
if (error)
|
|
21
|
+
reject(error);
|
|
22
|
+
else
|
|
23
|
+
resolve(value ?? {});
|
|
24
|
+
};
|
|
25
|
+
const onAbort = () => settle(new Error('WebHelm daemon request aborted'));
|
|
26
|
+
if (options.signal?.aborted) {
|
|
27
|
+
onAbort();
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
options.signal?.addEventListener('abort', onAbort, { once: true });
|
|
31
|
+
socket.setEncoding('utf8');
|
|
32
|
+
socket.setTimeout(timeoutMs, () => settle(new Error('WebHelm daemon ' + payload.op + ' timed out')));
|
|
33
|
+
socket.once('connect', () => socket.write(JSON.stringify(payload) + '\n'));
|
|
34
|
+
socket.on('data', (chunk) => {
|
|
35
|
+
buffer += chunk;
|
|
36
|
+
if (buffer.length > 2_000_000)
|
|
37
|
+
return settle(new Error('WebHelm daemon response exceeds limit'));
|
|
38
|
+
const delimiter = buffer.indexOf('\n');
|
|
39
|
+
if (delimiter < 0)
|
|
40
|
+
return;
|
|
41
|
+
try {
|
|
42
|
+
const result = JSON.parse(buffer.slice(0, delimiter));
|
|
43
|
+
if (result.error || result.ok === false)
|
|
44
|
+
return settle(new Error(String(result.error || 'WebHelm daemon rejected request')));
|
|
45
|
+
settle(undefined, result);
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
settle(error instanceof Error ? error : new Error(String(error)));
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
socket.once('error', (error) => settle(error));
|
|
52
|
+
socket.once('end', () => {
|
|
53
|
+
if (!settled)
|
|
54
|
+
settle(new Error('WebHelm daemon closed without a response'));
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
}
|