@skrr-ai/cli 0.1.178 → 0.1.180
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/commands/code/index.js +5 -0
- package/dist/commands/diagnostics/inference-smoke.d.ts +13 -0
- package/dist/commands/diagnostics/inference-smoke.js +50 -0
- package/dist/commands/harnesses/evaluations/evidence.d.ts +22 -0
- package/dist/commands/harnesses/evaluations/evidence.js +92 -0
- package/dist/commands/harnesses/evaluations/fixed-tool.d.ts +10 -0
- package/dist/commands/harnesses/evaluations/fixed-tool.js +189 -0
- package/dist/commands/harnesses/evaluations/grant.d.ts +30 -0
- package/dist/commands/harnesses/evaluations/grant.js +109 -0
- package/dist/commands/harnesses/evaluations/list.d.ts +18 -0
- package/dist/commands/harnesses/evaluations/list.js +73 -0
- package/dist/commands/harnesses/evaluations/platform-h9-turn.d.ts +13 -0
- package/dist/commands/harnesses/evaluations/platform-h9-turn.js +322 -0
- package/dist/commands/harnesses/evaluations/revoke.d.ts +19 -0
- package/dist/commands/harnesses/evaluations/revoke.js +45 -0
- package/dist/commands/harnesses/evaluations/show.d.ts +14 -0
- package/dist/commands/harnesses/evaluations/show.js +46 -0
- package/dist/commands/harnesses/list.d.ts +1 -0
- package/dist/commands/harnesses/list.js +11 -2
- package/dist/commands/harnesses/show.js +5 -0
- package/dist/commands/machines/hosted/identity-evaluations/issue.d.ts +12 -0
- package/dist/commands/machines/hosted/identity-evaluations/issue.js +40 -0
- package/dist/commands/machines/hosted/identity-evaluations/revoke.d.ts +11 -0
- package/dist/commands/machines/hosted/identity-evaluations/revoke.js +21 -0
- package/dist/commands/machines/hosted/identity-evaluations/show.d.ts +8 -0
- package/dist/commands/machines/hosted/identity-evaluations/show.js +20 -0
- package/dist/commands/machines/hosted/start.d.ts +1 -0
- package/dist/commands/machines/hosted/start.js +6 -0
- package/dist/lib/agentic-stream.d.ts +3 -0
- package/dist/lib/agentic-stream.js +63 -1
- package/dist/lib/copilot-fixed-probe-runner.d.ts +72 -0
- package/dist/lib/copilot-fixed-probe-runner.js +213 -0
- package/dist/lib/copilot-fixed-probe.d.ts +61 -0
- package/dist/lib/copilot-fixed-probe.js +223 -0
- package/dist/lib/first-party-harness-broker.d.ts +5 -0
- package/dist/lib/first-party-harness-broker.js +99 -0
- package/dist/lib/h9-platform-stream.d.ts +63 -0
- package/dist/lib/h9-platform-stream.js +317 -0
- package/dist/lib/h9-prior-stream.d.ts +44 -0
- package/dist/lib/h9-prior-stream.js +217 -0
- package/dist/lib/harness-evaluations.d.ts +251 -0
- package/dist/lib/harness-evaluations.js +66 -0
- package/dist/lib/harness-labels.d.ts +10 -0
- package/dist/lib/harness-labels.js +23 -0
- package/dist/lib/hosted-identity-evaluations.d.ts +26 -0
- package/dist/lib/hosted-identity-evaluations.js +18 -0
- package/dist/lib/hosted-machines.d.ts +1 -0
- package/dist/lib/inference-overload-smoke-contract.d.ts +53 -0
- package/dist/lib/inference-overload-smoke-contract.js +192 -0
- package/dist/lib/inference-overload-smoke.d.ts +78 -0
- package/dist/lib/inference-overload-smoke.js +350 -0
- package/dist/lib/operator-probe-files.d.ts +6 -0
- package/dist/lib/operator-probe-files.js +68 -0
- package/dist/lib/platform-h9-plan.d.ts +47 -0
- package/dist/lib/platform-h9-plan.js +78 -0
- package/dist/node_modules/@skrr-ai/auth-core/dist/cjs/codex-model-catalog.json +150 -0
- package/dist/node_modules/@skrr-ai/auth-core/dist/cjs/inferenceBrokerHarnesses.d.ts +12 -2
- package/dist/node_modules/@skrr-ai/auth-core/dist/cjs/inferenceBrokerHarnesses.js +12 -5
- package/dist/node_modules/@skrr-ai/auth-core/dist/esm/inferenceBrokerHarnesses.d.ts +12 -2
- package/dist/node_modules/@skrr-ai/auth-core/dist/esm/inferenceBrokerHarnesses.js +21 -7
- package/dist/node_modules/@skrr-ai/data-provider/index.js +6 -6
- package/dist/node_modules/@skrr-ai/inference-broker/dist/cjs/index.d.ts +3 -1
- package/dist/node_modules/@skrr-ai/inference-broker/dist/cjs/index.js +23 -1
- package/dist/node_modules/@skrr-ai/inference-broker/dist/cjs/managed-haiku-none.d.ts +8 -0
- package/dist/node_modules/@skrr-ai/inference-broker/dist/cjs/managed-haiku-none.js +40 -0
- package/dist/node_modules/@skrr-ai/inference-broker/dist/cjs/managed-inference-broker.d.ts +11 -20
- package/dist/node_modules/@skrr-ai/inference-broker/dist/cjs/managed-inference-broker.js +88 -9
- package/dist/node_modules/@skrr-ai/inference-broker/dist/cjs/overload-smoke.d.ts +56 -0
- package/dist/node_modules/@skrr-ai/inference-broker/dist/cjs/overload-smoke.js +277 -0
- package/dist/node_modules/@skrr-ai/inference-broker/dist/esm/index.d.ts +3 -1
- package/dist/node_modules/@skrr-ai/inference-broker/dist/esm/index.js +3 -1
- package/dist/node_modules/@skrr-ai/inference-broker/dist/esm/managed-haiku-none.d.ts +8 -0
- package/dist/node_modules/@skrr-ai/inference-broker/dist/esm/managed-haiku-none.js +20 -0
- package/dist/node_modules/@skrr-ai/inference-broker/dist/esm/managed-inference-broker.d.ts +11 -20
- package/dist/node_modules/@skrr-ai/inference-broker/dist/esm/managed-inference-broker.js +105 -12
- package/dist/node_modules/@skrr-ai/inference-broker/dist/esm/overload-smoke.d.ts +56 -0
- package/dist/node_modules/@skrr-ai/inference-broker/dist/esm/overload-smoke.js +261 -0
- package/oclif.manifest.json +1391 -443
- package/package.json +5 -2
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.H9_TURN_LABELS = void 0;
|
|
4
|
+
exports.h9FixedPrompt = h9FixedPrompt;
|
|
5
|
+
exports.validateH9Acceptance = validateH9Acceptance;
|
|
6
|
+
exports.streamH9PlatformTurn = streamH9PlatformTurn;
|
|
7
|
+
const node_stream_1 = require("node:stream");
|
|
8
|
+
const data_provider_1 = require("@skrr-ai/data-provider");
|
|
9
|
+
const agentic_stream_1 = require("./agentic-stream");
|
|
10
|
+
const copilot_fixed_probe_1 = require("./copilot-fixed-probe");
|
|
11
|
+
exports.H9_TURN_LABELS = [
|
|
12
|
+
'streaming',
|
|
13
|
+
'toolCalls',
|
|
14
|
+
'remember',
|
|
15
|
+
'interrupt',
|
|
16
|
+
'recall',
|
|
17
|
+
'cancel',
|
|
18
|
+
];
|
|
19
|
+
const need = (value, code) => {
|
|
20
|
+
if (!value)
|
|
21
|
+
throw new Error(code);
|
|
22
|
+
};
|
|
23
|
+
const id = (value) => typeof value === 'string' && /^[A-Za-z0-9][A-Za-z0-9_.:@-]{0,239}$/.test(value);
|
|
24
|
+
/** Fixed prompts for the two reviewed platform qualification pairs. */
|
|
25
|
+
function h9FixedPrompt(label, nonce, harness = 'pi') {
|
|
26
|
+
need(exports.H9_TURN_LABELS.includes(label), 'H9_LABEL_INVALID');
|
|
27
|
+
(0, copilot_fixed_probe_1.fixedCopilotCommand)(nonce); // Shared nonce validation and exact tool syntax.
|
|
28
|
+
switch (label) {
|
|
29
|
+
case 'streaming':
|
|
30
|
+
return 'Greet me in two short sentences. Do not use tools.';
|
|
31
|
+
case 'toolCalls':
|
|
32
|
+
if (harness === 'copilot-sdk')
|
|
33
|
+
return `Use Bash exactly once to run the exact command below, then reply with its printed value. Do not use any other tools.
|
|
34
|
+
|
|
35
|
+
${(0, copilot_fixed_probe_1.copilotPlatformScratchCommand)(nonce)}`;
|
|
36
|
+
return `Use Bash exactly once to run exactly: ${(0, copilot_fixed_probe_1.fixedCopilotCommand)(nonce)}. Use no other tools. Then reply with the printed value.`;
|
|
37
|
+
case 'remember':
|
|
38
|
+
return `Remember this marker: H9-MARKER-${nonce}. Reply OK only. Do not use tools.`;
|
|
39
|
+
case 'recall':
|
|
40
|
+
return 'What was the marker I asked you to remember? Reply with the marker only. Do not use tools.';
|
|
41
|
+
case 'interrupt':
|
|
42
|
+
case 'cancel':
|
|
43
|
+
// A counting prompt can trigger a shell command before the first text delta.
|
|
44
|
+
return 'Write a 1000-word story about a lighthouse, without using tools.';
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/** Actual normal HTTP acceptance, not a caller-provided session or permission context. */
|
|
48
|
+
function validateH9Acceptance(accepted, scope) {
|
|
49
|
+
need(accepted?.success === true &&
|
|
50
|
+
id(accepted.sessionId) &&
|
|
51
|
+
id(accepted.conversationId) &&
|
|
52
|
+
id(accepted.responseMessageId) &&
|
|
53
|
+
accepted.workspaceId === (scope.workspaceId ?? null) &&
|
|
54
|
+
(!scope.workspaceId || accepted.workspaceSource === 'request'), 'H9_EXACT_WORKSPACE_ACCEPTANCE_REQUIRED');
|
|
55
|
+
need(!scope.conversationId || accepted.conversationId === scope.conversationId, 'H9_CONVERSATION_CHANGED');
|
|
56
|
+
need(accepted.runtime?.mode === 'local' &&
|
|
57
|
+
accepted.runtime.provider === scope.harness &&
|
|
58
|
+
accepted.runtime.daemonId === scope.daemonId &&
|
|
59
|
+
accepted.runtime.agentRuntime === scope.harnessId &&
|
|
60
|
+
accepted.runtime.executionPlan?.selectedExecutionSelector === scope.harnessId &&
|
|
61
|
+
accepted.runtime.executionPlan?.fallbackUsed === false, 'H9_ACCEPTED_RUNTIME_MISMATCH');
|
|
62
|
+
const context = (0, copilot_fixed_probe_1.interactiveAdmission)(accepted.interactiveAdmission);
|
|
63
|
+
need(context.sessionId === accepted.sessionId &&
|
|
64
|
+
context.responseMessageId === accepted.responseMessageId, 'H9_ACCEPTED_CONTEXT_MISMATCH');
|
|
65
|
+
return context;
|
|
66
|
+
}
|
|
67
|
+
/** Pi 1.5.2 emits this structured Bash result and the daemon JSON-encodes it. */
|
|
68
|
+
function matchesPiBashOutput(actual, expected) {
|
|
69
|
+
if (typeof actual !== 'string' || actual.length > 1024)
|
|
70
|
+
return false;
|
|
71
|
+
try {
|
|
72
|
+
const result = JSON.parse(actual);
|
|
73
|
+
const content = result?.content;
|
|
74
|
+
const structured = result?.structuredContent;
|
|
75
|
+
return (Object.keys(result).sort().join(',') === 'content,structuredContent' &&
|
|
76
|
+
Array.isArray(content) &&
|
|
77
|
+
content.length === 1 &&
|
|
78
|
+
Object.keys(content[0]).sort().join(',') === 'text,type' &&
|
|
79
|
+
content[0].type === 'text' &&
|
|
80
|
+
content[0].text === expected &&
|
|
81
|
+
Object.keys(structured).sort().join(',') === 'exit_code,output,truncated,wall_time_seconds' &&
|
|
82
|
+
structured.output === expected &&
|
|
83
|
+
structured.truncated === false &&
|
|
84
|
+
structured.exit_code === 0 &&
|
|
85
|
+
Number.isFinite(structured.wall_time_seconds) &&
|
|
86
|
+
structured.wall_time_seconds >= 0);
|
|
87
|
+
}
|
|
88
|
+
catch {
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
/** Copilot 1.0.112 can append the runtime's shell completion trailer to stdout. */
|
|
93
|
+
function matchesCopilotBashOutput(actual, expected) {
|
|
94
|
+
if (actual === expected)
|
|
95
|
+
return true;
|
|
96
|
+
if (typeof actual !== 'string' || !actual.startsWith(expected))
|
|
97
|
+
return false;
|
|
98
|
+
const trailer = actual.slice(expected.length);
|
|
99
|
+
return (/^<shellId: (?:0|[1-9][0-9]{0,8}) completed with exit code 0>$/u.exec(trailer)?.[0] === trailer);
|
|
100
|
+
}
|
|
101
|
+
/** The normal, owner-scoped message route returns an array, even for one exact id. */
|
|
102
|
+
function recordedH9Response(rows, accepted, outcome) {
|
|
103
|
+
if (!Array.isArray(rows) || rows.length !== 1)
|
|
104
|
+
return null;
|
|
105
|
+
const only = rows[0];
|
|
106
|
+
if (only?.messageId !== accepted.responseMessageId ||
|
|
107
|
+
only.conversationId !== accepted.conversationId ||
|
|
108
|
+
only.isCreatedByUser !== false ||
|
|
109
|
+
only.metadata?.agentic?.sessionId !== accepted.sessionId ||
|
|
110
|
+
only.metadata.agentic.sessionOutcome?.status !== outcome ||
|
|
111
|
+
only.unfinished !== false ||
|
|
112
|
+
only.error !== false ||
|
|
113
|
+
typeof only.text !== 'string' ||
|
|
114
|
+
(0, data_provider_1.resolveMessageFailure)(only))
|
|
115
|
+
return null;
|
|
116
|
+
return { text: only.text, responseMessageId: only.messageId };
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Internal transport seam only. The closed operator runner must establish owner, unused
|
|
120
|
+
* grant, exact image/runtime/model and per-turn financial barriers before calling this.
|
|
121
|
+
* The shared client is the ONLY permission responder. No generic approval/input option.
|
|
122
|
+
*/
|
|
123
|
+
async function streamH9PlatformTurn(input) {
|
|
124
|
+
h9FixedPrompt(input.label, input.nonce, input.scope.harness);
|
|
125
|
+
const now = Date.now();
|
|
126
|
+
need(Number.isFinite(input.deadline) && input.deadline > now && input.deadline <= now + 60_000, 'H9_TURN_DEADLINE_INVALID');
|
|
127
|
+
const context = validateH9Acceptance(input.accepted, input.scope);
|
|
128
|
+
// Non-tool turns intentionally have no permission gate. Their first ask cancels the run.
|
|
129
|
+
const permission = input.label === 'toolCalls'
|
|
130
|
+
? new copilot_fixed_probe_1.FixedCopilotPermission(context, input.nonce, input.deadline, Date.now, input.scope.harness === 'pi' ? matchesPiBashOutput : matchesCopilotBashOutput, input.scope.harness === 'copilot-sdk')
|
|
131
|
+
: undefined;
|
|
132
|
+
const sink = new node_stream_1.Writable({
|
|
133
|
+
write(_chunk, _encoding, done) {
|
|
134
|
+
done();
|
|
135
|
+
},
|
|
136
|
+
});
|
|
137
|
+
let deltas = 0, bytes = 0, cancelRequestedAtMs = null;
|
|
138
|
+
let refusal = null;
|
|
139
|
+
let durableResponseId;
|
|
140
|
+
let expectedStopObserved = false;
|
|
141
|
+
const stopReason = 'H9 fixed cancellation case';
|
|
142
|
+
const expectedCancel = input.label === 'interrupt' || input.label === 'cancel';
|
|
143
|
+
const canReconcile = () => cancelRequestedAtMs === null &&
|
|
144
|
+
!refusal &&
|
|
145
|
+
!input.signal?.aborted &&
|
|
146
|
+
Date.now() < input.deadline;
|
|
147
|
+
const readResponseMessage = input.readResponseMessage;
|
|
148
|
+
const reconcileTerminal = !expectedCancel && readResponseMessage
|
|
149
|
+
? async () => {
|
|
150
|
+
if (!canReconcile())
|
|
151
|
+
return null;
|
|
152
|
+
const terminal = recordedH9Response(await readResponseMessage(), input.accepted, 'succeeded');
|
|
153
|
+
if (!terminal || !canReconcile())
|
|
154
|
+
return null;
|
|
155
|
+
durableResponseId = terminal.responseMessageId;
|
|
156
|
+
return { ...terminal, status: 'completed' };
|
|
157
|
+
}
|
|
158
|
+
: undefined;
|
|
159
|
+
const cancel = (reason) => {
|
|
160
|
+
permission?.stop(reason);
|
|
161
|
+
if (cancelRequestedAtMs !== null)
|
|
162
|
+
return;
|
|
163
|
+
cancelRequestedAtMs = Date.now();
|
|
164
|
+
client.cancel(reason); // Normal authenticated execution:interrupt, not a local detach.
|
|
165
|
+
};
|
|
166
|
+
const client = new agentic_stream_1.AgenticStreamClient({
|
|
167
|
+
baseURL: input.baseURL,
|
|
168
|
+
token: input.token,
|
|
169
|
+
sessionId: input.accepted.sessionId,
|
|
170
|
+
conversationId: input.accepted.conversationId,
|
|
171
|
+
// An empty selector is the legacy explicit Personal scope.
|
|
172
|
+
workspaceId: input.scope.workspaceId ?? '',
|
|
173
|
+
fixedCopilotProbe: permission,
|
|
174
|
+
quiet: true,
|
|
175
|
+
output: sink,
|
|
176
|
+
statusOutput: sink,
|
|
177
|
+
attachTimeoutMs: 10_000,
|
|
178
|
+
replayEnvelopes: input.replayEnvelopes,
|
|
179
|
+
reconcileTerminal,
|
|
180
|
+
idleTerminalProbeMs: 5_000,
|
|
181
|
+
onEvent: (event) => {
|
|
182
|
+
if (event.sessionId !== undefined && event.sessionId !== input.accepted.sessionId) {
|
|
183
|
+
refusal ??= 'H9_EVENT_SCOPE_MISMATCH';
|
|
184
|
+
cancel(refusal);
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
bytes += Buffer.byteLength(JSON.stringify(event));
|
|
188
|
+
if (bytes > 1_048_576) {
|
|
189
|
+
refusal ??= 'H9_EVENT_LIMIT';
|
|
190
|
+
cancel(refusal);
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
if ((!permission &&
|
|
194
|
+
['permission.requested', 'tool.started', 'tool.completed'].includes(event.type)) ||
|
|
195
|
+
[
|
|
196
|
+
'question.requested',
|
|
197
|
+
'permission.browser_required',
|
|
198
|
+
'run.handed_over',
|
|
199
|
+
'stream.gap',
|
|
200
|
+
'transport.error',
|
|
201
|
+
'text.reset',
|
|
202
|
+
].includes(event.type)) {
|
|
203
|
+
refusal ??= 'H9_UNEXPECTED_INTERACTION_OR_CONTEXT_LOSS';
|
|
204
|
+
cancel(refusal);
|
|
205
|
+
}
|
|
206
|
+
if (expectedCancel &&
|
|
207
|
+
event.type === 'run.cancel_requested' &&
|
|
208
|
+
event.sessionId === input.accepted.sessionId &&
|
|
209
|
+
event.reason === stopReason)
|
|
210
|
+
expectedStopObserved = true;
|
|
211
|
+
input.onEvent(event); // Actual canonical client event; never manufacture a tool witness.
|
|
212
|
+
if (expectedCancel &&
|
|
213
|
+
event.type === 'text.delta' &&
|
|
214
|
+
typeof event.text === 'string' &&
|
|
215
|
+
event.text.length > 0 &&
|
|
216
|
+
++deltas >= 3)
|
|
217
|
+
cancel(stopReason);
|
|
218
|
+
},
|
|
219
|
+
});
|
|
220
|
+
const abort = () => cancel('H9 operator interruption');
|
|
221
|
+
input.signal?.addEventListener('abort', abort, { once: true });
|
|
222
|
+
let hard;
|
|
223
|
+
const timer = setTimeout(() => cancel('H9 turn deadline'), Math.max(1, input.deadline - Date.now()));
|
|
224
|
+
try {
|
|
225
|
+
if (input.signal?.aborted)
|
|
226
|
+
throw new Error('H9_ABORTED_BEFORE_ATTACH');
|
|
227
|
+
const streamed = await Promise.race([
|
|
228
|
+
client.run(),
|
|
229
|
+
new Promise((_resolve, reject) => {
|
|
230
|
+
hard = setTimeout(() => {
|
|
231
|
+
cancel('H9 terminal unconfirmed');
|
|
232
|
+
reject(new Error('H9_TERMINAL_UNCONFIRMED'));
|
|
233
|
+
}, Math.max(1, input.deadline - Date.now()) + 15_000);
|
|
234
|
+
}),
|
|
235
|
+
]);
|
|
236
|
+
// The normal result gets its response id from legacy message:stream frames. Canonical
|
|
237
|
+
// replay can leave it unset, even when execution:complete carries a responseMessageId.
|
|
238
|
+
// A contradictory id on either surface is never repaired with a durable read.
|
|
239
|
+
const expectedResponseId = input.accepted.responseMessageId;
|
|
240
|
+
need(streamed.sessionId === input.accepted.sessionId &&
|
|
241
|
+
(streamed.lastEvent?.sessionId === undefined ||
|
|
242
|
+
streamed.lastEvent.sessionId === input.accepted.sessionId) &&
|
|
243
|
+
(streamed.lastEvent?.conversationId === undefined ||
|
|
244
|
+
streamed.lastEvent.conversationId === input.accepted.conversationId) &&
|
|
245
|
+
[
|
|
246
|
+
streamed.responseMessageId,
|
|
247
|
+
streamed.lastEvent?.responseMessageId,
|
|
248
|
+
streamed.lastEvent?.messageId,
|
|
249
|
+
].every((id) => id === undefined || id === expectedResponseId), 'H9_TERMINAL_SCOPE_MISMATCH');
|
|
250
|
+
let responseMessageId = streamed.responseMessageId;
|
|
251
|
+
const terminalEvent = streamed.lastEvent;
|
|
252
|
+
const observedAbort = streamed.status === 'aborted' &&
|
|
253
|
+
terminalEvent !== undefined &&
|
|
254
|
+
terminalEvent.sessionId === input.accepted.sessionId &&
|
|
255
|
+
terminalEvent.cancelled !== false;
|
|
256
|
+
const canBindCancellation = () => expectedStopObserved && !refusal && !input.signal?.aborted && Date.now() < input.deadline;
|
|
257
|
+
if (responseMessageId === undefined && streamed.status === 'completed' && reconcileTerminal) {
|
|
258
|
+
if (streamed.lastEvent?.type === 'run.reconciled' &&
|
|
259
|
+
streamed.lastEvent.reconciledFrom === 'durable_record') {
|
|
260
|
+
responseMessageId = durableResponseId;
|
|
261
|
+
}
|
|
262
|
+
else {
|
|
263
|
+
// The worker can emit completion before the message outcome is visible.
|
|
264
|
+
for (let attempt = 0; attempt < 3 && canReconcile(); attempt++) {
|
|
265
|
+
const recorded = await reconcileTerminal().catch(() => null);
|
|
266
|
+
if (recorded) {
|
|
267
|
+
responseMessageId = recorded.responseMessageId;
|
|
268
|
+
break;
|
|
269
|
+
}
|
|
270
|
+
if (attempt < 2 && canReconcile())
|
|
271
|
+
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
if (responseMessageId === undefined && observedAbort && readResponseMessage) {
|
|
276
|
+
// Daemon cancellation ACKs name the session but omit the response ID. Require the
|
|
277
|
+
// fixed Stop and an independently persisted cancellation of this exact response.
|
|
278
|
+
for (let attempt = 0; attempt < 3 && canBindCancellation(); attempt++) {
|
|
279
|
+
const rows = await readResponseMessage().catch(() => null);
|
|
280
|
+
if (!canBindCancellation())
|
|
281
|
+
break;
|
|
282
|
+
const recorded = recordedH9Response(rows, input.accepted, 'cancelled');
|
|
283
|
+
if (recorded) {
|
|
284
|
+
responseMessageId = recorded.responseMessageId;
|
|
285
|
+
break;
|
|
286
|
+
}
|
|
287
|
+
if (attempt < 2)
|
|
288
|
+
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
need(responseMessageId === expectedResponseId, 'H9_TERMINAL_SCOPE_MISMATCH');
|
|
292
|
+
const result = { ...streamed, responseMessageId };
|
|
293
|
+
input.onEvent({ type: 'run.result', ...result });
|
|
294
|
+
const permissionFacts = permission?.result() ?? null;
|
|
295
|
+
const successful = !refusal &&
|
|
296
|
+
(expectedCancel
|
|
297
|
+
? expectedStopObserved && observedAbort && !input.signal?.aborted
|
|
298
|
+
: cancelRequestedAtMs === null && result.status === 'completed') &&
|
|
299
|
+
(!permission || permissionFacts?.toolVerified === true);
|
|
300
|
+
return {
|
|
301
|
+
result,
|
|
302
|
+
permission: permissionFacts,
|
|
303
|
+
cancelRequestedAtMs,
|
|
304
|
+
successful,
|
|
305
|
+
refusal,
|
|
306
|
+
providerAbsenceClaimed: false,
|
|
307
|
+
financialSettlementClaimed: false,
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
finally {
|
|
311
|
+
clearTimeout(timer);
|
|
312
|
+
clearTimeout(hard);
|
|
313
|
+
input.signal?.removeEventListener('abort', abort);
|
|
314
|
+
permission?.close();
|
|
315
|
+
client.close(); // Local close is never proof of provider stop or settlement.
|
|
316
|
+
}
|
|
317
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { type H9Harness } from './h9-platform-stream';
|
|
2
|
+
import type { HarnessEvaluationEvidence, HarnessEvaluationFinancialEvidence } from './harness-evaluations';
|
|
3
|
+
import type { PlatformH9Plan } from './platform-h9-plan';
|
|
4
|
+
/**
|
|
5
|
+
* The reviewer seals the historical owner reads before the former lease and
|
|
6
|
+
* conversation are cleaned up. Revalidate their exact scope and stream here.
|
|
7
|
+
*/
|
|
8
|
+
export declare function capturedH9Stream(plan: PlatformH9Plan, harness: H9Harness, machineProduct: string | null): {
|
|
9
|
+
grantId: string;
|
|
10
|
+
machine: {
|
|
11
|
+
daemonId: string;
|
|
12
|
+
machineUuid: string;
|
|
13
|
+
machineProduct: string;
|
|
14
|
+
image: string;
|
|
15
|
+
};
|
|
16
|
+
harness: H9Harness;
|
|
17
|
+
version: "1.0.112" | "1.5.2";
|
|
18
|
+
model: "claude-sonnet-5";
|
|
19
|
+
sessionId: string;
|
|
20
|
+
conversationId: string;
|
|
21
|
+
responseMessageId: string;
|
|
22
|
+
requestId: string;
|
|
23
|
+
firstByteMs: number;
|
|
24
|
+
chunks: number;
|
|
25
|
+
};
|
|
26
|
+
export declare function bindH9PriorStreamSettlement(plan: PlatformH9Plan, stream: ReturnType<typeof capturedH9Stream>, readback: HarnessEvaluationFinancialEvidence, evidence: HarnessEvaluationEvidence, currentBinding: HarnessEvaluationEvidence['grant']['modelBindings'][number]): {
|
|
27
|
+
reservationId: string;
|
|
28
|
+
grantId: string;
|
|
29
|
+
machine: {
|
|
30
|
+
daemonId: string;
|
|
31
|
+
machineUuid: string;
|
|
32
|
+
machineProduct: string;
|
|
33
|
+
image: string;
|
|
34
|
+
};
|
|
35
|
+
harness: H9Harness;
|
|
36
|
+
version: "1.0.112" | "1.5.2";
|
|
37
|
+
model: "claude-sonnet-5";
|
|
38
|
+
sessionId: string;
|
|
39
|
+
conversationId: string;
|
|
40
|
+
responseMessageId: string;
|
|
41
|
+
requestId: string;
|
|
42
|
+
firstByteMs: number;
|
|
43
|
+
chunks: number;
|
|
44
|
+
};
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.capturedH9Stream = capturedH9Stream;
|
|
4
|
+
exports.bindH9PriorStreamSettlement = bindH9PriorStreamSettlement;
|
|
5
|
+
const node_util_1 = require("node:util");
|
|
6
|
+
const h9_platform_stream_1 = require("./h9-platform-stream");
|
|
7
|
+
const refuse = (reason) => {
|
|
8
|
+
throw new Error(`H9_PRIOR_STREAM_${reason}`);
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* The reviewer seals the historical owner reads before the former lease and
|
|
12
|
+
* conversation are cleaned up. Revalidate their exact scope and stream here.
|
|
13
|
+
*/
|
|
14
|
+
function capturedH9Stream(plan, harness, machineProduct) {
|
|
15
|
+
const prior = plan.priorStream;
|
|
16
|
+
if (!prior)
|
|
17
|
+
return refuse('REQUIRED');
|
|
18
|
+
const machine = prior.machine;
|
|
19
|
+
const { conversation, message, envelopes } = prior.capture ?? {};
|
|
20
|
+
const convo = conversation?.data;
|
|
21
|
+
const holder = convo?.agenticSession;
|
|
22
|
+
const accepted = holder?.idempotencyResponse;
|
|
23
|
+
const page = envelopes?.data;
|
|
24
|
+
const rows = message?.data;
|
|
25
|
+
const response = Array.isArray(rows) && rows.length === 1 ? rows[0] : null;
|
|
26
|
+
if (!accepted ||
|
|
27
|
+
!response ||
|
|
28
|
+
!page ||
|
|
29
|
+
conversation?.status !== 200 ||
|
|
30
|
+
message?.status !== 200 ||
|
|
31
|
+
envelopes?.status !== 200 ||
|
|
32
|
+
machine?.image !== plan.image ||
|
|
33
|
+
machine.machineProduct !== machineProduct ||
|
|
34
|
+
!machine.machineUuid ||
|
|
35
|
+
convo?.user !== plan.ownerUserId ||
|
|
36
|
+
(convo?.workspaceId ?? null) !== (plan.workspaceId ?? null) ||
|
|
37
|
+
typeof convo?.agent_id !== 'string' ||
|
|
38
|
+
convo.agent_id !== response.model ||
|
|
39
|
+
convo.conversationId !== accepted.conversationId ||
|
|
40
|
+
holder.sessionId !== accepted.sessionId ||
|
|
41
|
+
holder.responseMessageId !== accepted.responseMessageId ||
|
|
42
|
+
holder.clientRequestId !== accepted.clientRequestId ||
|
|
43
|
+
!/^h9-platform-[a-f0-9]{32}:streaming$/.test(accepted.clientRequestId ?? '') ||
|
|
44
|
+
accepted.clientRequestId === `h9-platform-${plan.operationId}:streaming` ||
|
|
45
|
+
accepted.isNewConversation !== true ||
|
|
46
|
+
response.conversationId !== accepted.conversationId ||
|
|
47
|
+
response.messageId !== accepted.responseMessageId ||
|
|
48
|
+
response.metadata?.agentic?.sessionId !== accepted.sessionId ||
|
|
49
|
+
response.metadata.agentic.sessionOutcome?.status !== 'succeeded' ||
|
|
50
|
+
response.isCreatedByUser !== false ||
|
|
51
|
+
response.error !== false ||
|
|
52
|
+
response.unfinished !== false ||
|
|
53
|
+
typeof response.text !== 'string' ||
|
|
54
|
+
!response.text.trim())
|
|
55
|
+
return refuse('IDENTITY_OR_MESSAGE');
|
|
56
|
+
(0, h9_platform_stream_1.validateH9Acceptance)(accepted, {
|
|
57
|
+
agentId: convo.agent_id,
|
|
58
|
+
harness,
|
|
59
|
+
harnessId: accepted.runtime?.agentRuntime ?? '',
|
|
60
|
+
daemonId: machine.daemonId,
|
|
61
|
+
workspaceId: plan.workspaceId,
|
|
62
|
+
});
|
|
63
|
+
const records = page.envelopes;
|
|
64
|
+
if (page.sessionId !== accepted.sessionId ||
|
|
65
|
+
page.from !== 0 ||
|
|
66
|
+
page.gap !== false ||
|
|
67
|
+
page.hasMore !== false ||
|
|
68
|
+
!Array.isArray(records) ||
|
|
69
|
+
records.length > 500 ||
|
|
70
|
+
records.length !== page.nextSeq ||
|
|
71
|
+
records.some((record, i) => !record ||
|
|
72
|
+
record.__seq !== i + 1 ||
|
|
73
|
+
!['service', 'turn-start', 'text', 'turn-end', 'turn-finalized'].includes(record.ev?.t)))
|
|
74
|
+
return refuse('REPLAY_GAP_OR_UNEXPECTED_EVENT');
|
|
75
|
+
const begin = records.filter((record) => record.ev.t === 'turn-start');
|
|
76
|
+
const end = records.filter((record) => record.ev.t === 'turn-end');
|
|
77
|
+
const finalized = records.filter((record) => record.ev.t === 'turn-finalized');
|
|
78
|
+
const chunks = records.filter((record) => record.ev.t === 'text' && record.ev.thinking !== true);
|
|
79
|
+
if (begin.length !== 1 ||
|
|
80
|
+
end.length !== 1 ||
|
|
81
|
+
finalized.length !== 1 ||
|
|
82
|
+
end[0].ev.status !== 'completed' ||
|
|
83
|
+
finalized[0].ev.subtype !== 'success' ||
|
|
84
|
+
end[0].__seq >= finalized[0].__seq ||
|
|
85
|
+
!chunks.length ||
|
|
86
|
+
typeof begin[0].turn !== 'string' ||
|
|
87
|
+
!begin[0].turn ||
|
|
88
|
+
[...begin, ...chunks, ...end, ...finalized].some((record) => record.turn !== begin[0].turn ||
|
|
89
|
+
record.messageId !== accepted.responseMessageId ||
|
|
90
|
+
record.agentId !== convo.agent_id) ||
|
|
91
|
+
chunks.some((record) => record.__seq <= begin[0].__seq ||
|
|
92
|
+
record.__seq >= end[0].__seq ||
|
|
93
|
+
record.origin?.provider !== harness ||
|
|
94
|
+
typeof record.ev.text !== 'string' ||
|
|
95
|
+
!record.ev.text) ||
|
|
96
|
+
chunks
|
|
97
|
+
.map((record) => record.ev.text)
|
|
98
|
+
.join('')
|
|
99
|
+
.trim() !== response.text.trim())
|
|
100
|
+
return refuse('BEHAVIOR');
|
|
101
|
+
const firstByteMs = chunks[0].time - Date.parse(response.createdAt);
|
|
102
|
+
if (!Number.isFinite(firstByteMs) || firstByteMs < 0)
|
|
103
|
+
return refuse('TIMING');
|
|
104
|
+
return {
|
|
105
|
+
grantId: prior.grantId,
|
|
106
|
+
machine: {
|
|
107
|
+
daemonId: machine.daemonId,
|
|
108
|
+
machineUuid: machine.machineUuid,
|
|
109
|
+
machineProduct: machine.machineProduct,
|
|
110
|
+
image: machine.image,
|
|
111
|
+
},
|
|
112
|
+
harness,
|
|
113
|
+
version: plan.version,
|
|
114
|
+
model: plan.model,
|
|
115
|
+
sessionId: accepted.sessionId,
|
|
116
|
+
conversationId: accepted.conversationId,
|
|
117
|
+
responseMessageId: accepted.responseMessageId,
|
|
118
|
+
requestId: accepted.clientRequestId,
|
|
119
|
+
firstByteMs,
|
|
120
|
+
chunks: chunks.length,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
function bindH9PriorStreamSettlement(plan, stream, readback, evidence, currentBinding) {
|
|
124
|
+
const { grant, financial } = readback;
|
|
125
|
+
const priorMachine = evidence.machine;
|
|
126
|
+
const admission = grant.admissions?.[0];
|
|
127
|
+
const ledger = financial.turns?.[0];
|
|
128
|
+
const customer = ledger?.customer;
|
|
129
|
+
const provider = ledger?.provider;
|
|
130
|
+
const row = evidence.turns?.find((turn) => turn.sessionId === stream.sessionId);
|
|
131
|
+
const reservation = row?.reservation;
|
|
132
|
+
if (grant.grantId !== stream.grantId ||
|
|
133
|
+
evidence.grant.grantId !== stream.grantId ||
|
|
134
|
+
grant.ownerUserId !== plan.ownerUserId ||
|
|
135
|
+
evidence.grant.ownerUserId !== plan.ownerUserId ||
|
|
136
|
+
grant.daemonId !== stream.machine.daemonId ||
|
|
137
|
+
grant.machineUuid !== stream.machine.machineUuid ||
|
|
138
|
+
priorMachine?.daemonId !== stream.machine.daemonId ||
|
|
139
|
+
priorMachine.machineUuid !== stream.machine.machineUuid ||
|
|
140
|
+
(priorMachine.image != null && priorMachine.image !== stream.machine.image) ||
|
|
141
|
+
(priorMachine.machineProduct != null &&
|
|
142
|
+
priorMachine.machineProduct !== stream.machine.machineProduct) ||
|
|
143
|
+
grant.harness !== stream.harness ||
|
|
144
|
+
grant.version !== plan.version ||
|
|
145
|
+
!(0, node_util_1.isDeepStrictEqual)(grant.models, [plan.model]) ||
|
|
146
|
+
!(0, node_util_1.isDeepStrictEqual)(grant.modelBindings, [currentBinding]) ||
|
|
147
|
+
!(0, node_util_1.isDeepStrictEqual)(evidence.grant.modelBindings, [currentBinding]) ||
|
|
148
|
+
financial.grantId !== stream.grantId ||
|
|
149
|
+
financial.workspaceId !== plan.workspaceId ||
|
|
150
|
+
financial.bindingDigest !== currentBinding.bindingDigest ||
|
|
151
|
+
financial.schemaVersion !== 1 ||
|
|
152
|
+
financial.state !== 'closed' ||
|
|
153
|
+
financial.outstandingReservations !== 0 ||
|
|
154
|
+
financial.providerAuthorizationUsd !== 1 ||
|
|
155
|
+
financial.customerQuoteUsageMicros !== 1_250_000 ||
|
|
156
|
+
!financial.priceBookVersion ||
|
|
157
|
+
evidence.gateway?.unavailable ||
|
|
158
|
+
admission?.sessionId !== stream.sessionId ||
|
|
159
|
+
ledger?.sessionId !== stream.sessionId ||
|
|
160
|
+
ledger.state !== 'closed' ||
|
|
161
|
+
ledger.closure !== 'provider_spend_reconciled' ||
|
|
162
|
+
customer?.status !== 'settled' ||
|
|
163
|
+
customer.priceBookVersion !== financial.priceBookVersion ||
|
|
164
|
+
customer.quoteUsageMicros !== financial.customerQuoteUsageMicros ||
|
|
165
|
+
[customer.settledUsageMicros, customer.settledBalanceMicros].some((amount) => !Number.isSafeInteger(amount) || amount < 0 || amount > customer.quoteUsageMicros) ||
|
|
166
|
+
!row ||
|
|
167
|
+
row.reservationId !== admission.reservationId ||
|
|
168
|
+
row.reservationId !== ledger.reservationId ||
|
|
169
|
+
row.requestedAlias !== plan.model ||
|
|
170
|
+
row.modelBindingDigest !== currentBinding.bindingDigest ||
|
|
171
|
+
!reservation ||
|
|
172
|
+
reservation.status !== 'settled' ||
|
|
173
|
+
reservation.finalizationReason !== 'provider_spend_reconciled' ||
|
|
174
|
+
reservation.provider !== stream.harness ||
|
|
175
|
+
reservation.upstreamProvider !== currentBinding.upstreamProvider ||
|
|
176
|
+
reservation.authorizedUsd !== financial.providerAuthorizationUsd ||
|
|
177
|
+
reservation.daemonId !== stream.machine.daemonId ||
|
|
178
|
+
reservation.harnessEvaluationGrantId !== stream.grantId ||
|
|
179
|
+
reservation.harnessVersion !== plan.version ||
|
|
180
|
+
reservation.harnessPlacement !== 'platform-machine' ||
|
|
181
|
+
reservation.managedAdmissionKind?.version !== 1 ||
|
|
182
|
+
reservation.managedAdmissionKind.kind !== 'evaluation' ||
|
|
183
|
+
!(0, node_util_1.isDeepStrictEqual)(reservation.harnessEvaluationModelBinding, currentBinding) ||
|
|
184
|
+
reservation.harnessEvaluationMintApproval?.bindingDigest !== currentBinding.bindingDigest ||
|
|
185
|
+
reservation.model !== currentBinding.policyModel ||
|
|
186
|
+
reservation.immutableGatewayModelAlias !== plan.model ||
|
|
187
|
+
!row.calls.length ||
|
|
188
|
+
provider?.leaseTerminal !== true ||
|
|
189
|
+
provider.authorizedUsd !== financial.providerAuthorizationUsd ||
|
|
190
|
+
row.calls.length !== provider.registeredRequests ||
|
|
191
|
+
row.calls.length !== provider.reconciledRequests ||
|
|
192
|
+
!Number.isFinite(provider.settledUsd) ||
|
|
193
|
+
provider.settledUsd < 0 ||
|
|
194
|
+
provider.settledUsd !== reservation.settledUsd)
|
|
195
|
+
return refuse('SETTLEMENT_OR_RUNTIME');
|
|
196
|
+
let providerSpend = 0;
|
|
197
|
+
for (const call of row.calls) {
|
|
198
|
+
const gateway = call.gateway;
|
|
199
|
+
if (!gateway ||
|
|
200
|
+
'readError' in gateway ||
|
|
201
|
+
call.observation !== 'reconciled' ||
|
|
202
|
+
gateway.callId !== call.providerRequestId ||
|
|
203
|
+
gateway.callType !== 'anthropic_messages' ||
|
|
204
|
+
gateway.status !== 'success' ||
|
|
205
|
+
!Number.isFinite(gateway.endMs) ||
|
|
206
|
+
typeof gateway.spend !== 'number' ||
|
|
207
|
+
!Number.isFinite(gateway.spend) ||
|
|
208
|
+
gateway.spend < 0 ||
|
|
209
|
+
!gateway.model ||
|
|
210
|
+
!currentBinding.gatewayDeploymentModels.includes(gateway.model))
|
|
211
|
+
return refuse('GATEWAY');
|
|
212
|
+
providerSpend += gateway.spend;
|
|
213
|
+
}
|
|
214
|
+
if (Math.abs(providerSpend - provider.settledUsd) > 1e-9)
|
|
215
|
+
return refuse('PROVIDER_SPEND');
|
|
216
|
+
return { ...stream, reservationId: row.reservationId };
|
|
217
|
+
}
|