@noetic-tools/core 5.19.1-next.1 → 5.19.2
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/adapters/openrouter.js +5 -0
- package/dist/builders/control-flow-builders.d.ts +15 -6
- package/dist/builders/control-flow-builders.js +8 -0
- package/dist/builders/decide-builders.d.ts +2 -5
- package/dist/builders/dynamic-workflow.d.ts +16 -1
- package/dist/builders/dynamic-workflow.js +222 -11
- package/dist/builders/every.d.ts +3 -3
- package/dist/builders/every.js +2 -1
- package/dist/builders/provide-builder.d.ts +3 -2
- package/dist/builders/provide-builder.js +2 -1
- package/dist/builders/sequence.d.ts +14 -0
- package/dist/builders/sequence.js +103 -0
- package/dist/builders/spawn-builder.d.ts +3 -2
- package/dist/builders/spawn-builder.js +3 -2
- package/dist/builders/step-builders.d.ts +80 -1
- package/dist/builders/step-builders.js +115 -1
- package/dist/builders/verify-builder.d.ts +79 -0
- package/dist/builders/verify-builder.js +69 -0
- package/dist/builders/workflow-child-nodes.d.ts +21 -0
- package/dist/builders/workflow-child-nodes.js +335 -0
- package/dist/builders/workflow-connection-nodes.d.ts +17 -0
- package/dist/builders/workflow-connection-nodes.js +160 -0
- package/dist/builders/workflow-connections.d.ts +90 -0
- package/dist/builders/workflow-connections.js +560 -0
- package/dist/builders/workflow-control-nodes.d.ts +13 -0
- package/dist/builders/workflow-control-nodes.js +213 -0
- package/dist/builders/workflow-hydrator.d.ts +44 -3
- package/dist/builders/workflow-hydrator.js +53 -495
- package/dist/builders/workflow-verify.d.ts +19 -0
- package/dist/builders/workflow-verify.js +194 -0
- package/dist/conditions/conditions.d.ts +7 -5
- package/dist/conditions/conditions.js +7 -4
- package/dist/connections/blobs.d.ts +33 -0
- package/dist/connections/blobs.js +77 -0
- package/dist/connections/cron-schedule.d.ts +32 -0
- package/dist/connections/cron-schedule.js +213 -0
- package/dist/connections/durability.d.ts +65 -0
- package/dist/connections/durability.js +54 -0
- package/dist/connections/errors.d.ts +30 -0
- package/dist/connections/errors.js +100 -0
- package/dist/connections/event-stream.d.ts +24 -0
- package/dist/connections/event-stream.js +176 -0
- package/dist/connections/gates.d.ts +94 -0
- package/dist/connections/gates.js +271 -0
- package/dist/connections/glob.d.ts +10 -0
- package/dist/connections/glob.js +46 -0
- package/dist/connections/handle.d.ts +22 -0
- package/dist/connections/handle.js +84 -0
- package/dist/connections/harness-wiring.d.ts +52 -0
- package/dist/connections/harness-wiring.js +143 -0
- package/dist/connections/in-memory-connection.d.ts +147 -0
- package/dist/connections/in-memory-connection.js +593 -0
- package/dist/connections/index.d.ts +34 -0
- package/dist/connections/index.js +23 -0
- package/dist/connections/json-schema.d.ts +28 -0
- package/dist/connections/json-schema.js +338 -0
- package/dist/connections/model-tools.d.ts +46 -0
- package/dist/connections/model-tools.js +466 -0
- package/dist/connections/negotiation.d.ts +30 -0
- package/dist/connections/negotiation.js +159 -0
- package/dist/connections/policy.d.ts +21 -0
- package/dist/connections/policy.js +34 -0
- package/dist/connections/pool.d.ts +111 -0
- package/dist/connections/pool.js +339 -0
- package/dist/connections/refs.d.ts +74 -0
- package/dist/connections/refs.js +316 -0
- package/dist/connections/registry-types.d.ts +141 -0
- package/dist/connections/registry-types.js +14 -0
- package/dist/connections/registry.d.ts +125 -0
- package/dist/connections/registry.js +775 -0
- package/dist/connections/secrets.d.ts +59 -0
- package/dist/connections/secrets.js +153 -0
- package/dist/connections/settle.d.ts +9 -0
- package/dist/connections/settle.js +26 -0
- package/dist/connections/sources.d.ts +27 -0
- package/dist/connections/sources.js +300 -0
- package/dist/connections/triggers.d.ts +78 -0
- package/dist/connections/triggers.js +310 -0
- package/dist/connections/values.d.ts +26 -0
- package/dist/connections/values.js +93 -0
- package/dist/connections/views.d.ts +31 -0
- package/dist/connections/views.js +176 -0
- package/dist/expression/check.js +80 -1
- package/dist/expression/jsonata.d.ts +14 -4
- package/dist/expression/jsonata.js +27 -9
- package/dist/expression/step-outputs.d.ts +25 -0
- package/dist/expression/step-outputs.js +78 -0
- package/dist/harness/agent-harness.d.ts +69 -166
- package/dist/harness/agent-harness.js +170 -262
- package/dist/harness/call-span-collector.d.ts +33 -0
- package/dist/harness/call-span-collector.js +83 -0
- package/dist/harness/call-spend.d.ts +2 -0
- package/dist/harness/call-spend.js +10 -14
- package/dist/harness/connection-gate.d.ts +78 -0
- package/dist/harness/connection-gate.js +169 -0
- package/dist/harness/deps/interpreter.d.ts +1 -0
- package/dist/harness/deps/interpreter.js +1 -0
- package/dist/harness/deps/runtime.d.ts +1 -1
- package/dist/harness/deps/types.d.ts +1 -1
- package/dist/harness/harness-options.d.ts +153 -0
- package/dist/harness/harness-options.js +36 -0
- package/dist/harness/harness-tracing.d.ts +16 -0
- package/dist/harness/harness-tracing.js +35 -0
- package/dist/harness/llm-client.d.ts +71 -0
- package/dist/harness/llm-client.js +218 -0
- package/dist/harness/model-call.d.ts +2 -7
- package/dist/harness/model-call.js +141 -558
- package/dist/harness/model-request.d.ts +42 -0
- package/dist/harness/model-request.js +159 -0
- package/dist/harness/model-rounds.d.ts +18 -2
- package/dist/harness/model-rounds.js +39 -8
- package/dist/harness/model-stream.d.ts +39 -0
- package/dist/harness/model-stream.js +131 -0
- package/dist/harness/model-tool-round.d.ts +37 -0
- package/dist/harness/model-tool-round.js +197 -0
- package/dist/harness/spend.d.ts +2 -0
- package/dist/harness/spend.js +5 -1
- package/dist/index.d.ts +31 -6
- package/dist/index.js +18 -1
- package/dist/interpreter/connection-steps.d.ts +194 -0
- package/dist/interpreter/connection-steps.js +537 -0
- package/dist/interpreter/execute-action.d.ts +2 -8
- package/dist/interpreter/execute-action.js +3 -178
- package/dist/interpreter/execute-call.d.ts +19 -0
- package/dist/interpreter/execute-call.js +99 -0
- package/dist/interpreter/execute-control.d.ts +6 -1
- package/dist/interpreter/execute-control.js +14 -0
- package/dist/interpreter/execute-invoke-tool.d.ts +11 -0
- package/dist/interpreter/execute-invoke-tool.js +183 -0
- package/dist/interpreter/execute-verify.d.ts +21 -0
- package/dist/interpreter/execute-verify.js +391 -0
- package/dist/interpreter/execute-wait.d.ts +21 -0
- package/dist/interpreter/execute-wait.js +233 -0
- package/dist/interpreter/execute.js +24 -133
- package/dist/interpreter/step-kind-dispatch.d.ts +24 -0
- package/dist/interpreter/step-kind-dispatch.js +157 -0
- package/dist/interpreter/step-walk.d.ts +2 -0
- package/dist/interpreter/step-walk.js +24 -0
- package/dist/interpreter/verify-host.d.ts +40 -0
- package/dist/interpreter/verify-host.js +245 -0
- package/dist/runtime/durable/step-ledger.d.ts +34 -2
- package/dist/runtime/durable/step-ledger.js +97 -3
- package/dist/runtime/in-memory-artifact-store.d.ts +24 -0
- package/dist/runtime/in-memory-artifact-store.js +240 -0
- package/dist/schemas/workflow.d.ts +2 -2
- package/dist/schemas/workflow.js +1 -1
- package/dist/util/harness-internals.d.ts +23 -0
- package/dist/util/harness-internals.js +35 -0
- package/package.json +3 -3
- package/schema/noetic-workflow.schema.json +1170 -92
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Durability (spec 35 § Durability): idempotency keys, the pending ledger
|
|
3
|
+
* entry a `call` or `wait` writes before it reaches a server, and what a
|
|
4
|
+
* resumed run does with a connection that was replaced.
|
|
5
|
+
*
|
|
6
|
+
* If a connection's identity instance changed, results recorded against it
|
|
7
|
+
* describe a world that is gone. Its determinism decides, following OVP's
|
|
8
|
+
* resume strategy:
|
|
9
|
+
*
|
|
10
|
+
* | `replayPrefix` | Resume |
|
|
11
|
+
* |---|---|
|
|
12
|
+
* | `'free'` | Discard the ledger from the first step that used it; run live. |
|
|
13
|
+
* | `'costly'`, with `reset` | Reset the connection, then the same. |
|
|
14
|
+
* | `'unsafe'` | Refuse until `harness.restore(id, { rerun: [name] })`. |
|
|
15
|
+
*
|
|
16
|
+
* An epoch change alone (a hot reload) keeps refs and results. A connection
|
|
17
|
+
* that declares no determinism is treated as `'unsafe'`.
|
|
18
|
+
*/
|
|
19
|
+
//#endregion
|
|
20
|
+
//#region Public API
|
|
21
|
+
/** The idempotency key of a step's call: the run and the step's ledger key. */
|
|
22
|
+
export function stepIdempotencyKey(executionId, ledgerKey) {
|
|
23
|
+
return `${executionId}:${ledgerKey}`;
|
|
24
|
+
}
|
|
25
|
+
/** The idempotency key of a model tool call: the turn's execution and the tool call id. */
|
|
26
|
+
export function toolIdempotencyKey(executionId, toolCallId) {
|
|
27
|
+
return `${executionId}:tool:${toolCallId}`;
|
|
28
|
+
}
|
|
29
|
+
/** Whether `current` is the same thing `recorded` described (an epoch change alone keeps it). */
|
|
30
|
+
export function sameInstance(recorded, current) {
|
|
31
|
+
return recorded.surface === current.surface && recorded.instance === current.instance;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Decides what a resumed run does about a connection whose results were
|
|
35
|
+
* recorded against `recorded`.
|
|
36
|
+
*/
|
|
37
|
+
export function resumeDecision(params) {
|
|
38
|
+
if (sameInstance(params.recorded, params.current)) {
|
|
39
|
+
return 'keep';
|
|
40
|
+
}
|
|
41
|
+
if (params.rerunAllowed) {
|
|
42
|
+
return 'discard';
|
|
43
|
+
}
|
|
44
|
+
const replay = params.determinism?.replayPrefix ?? 'unsafe';
|
|
45
|
+
if (replay === 'free') {
|
|
46
|
+
return 'discard';
|
|
47
|
+
}
|
|
48
|
+
const resettable = params.canReset && (params.determinism?.reset ?? 'none') !== 'none';
|
|
49
|
+
if (replay === 'costly' && resettable) {
|
|
50
|
+
return 'reset';
|
|
51
|
+
}
|
|
52
|
+
return 'refuse';
|
|
53
|
+
}
|
|
54
|
+
//#endregion
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error constructors for the connection runtime (spec 35 § Errors). Setup and
|
|
3
|
+
* validation problems are `NoeticConfigError`s with a `CONNECTION_*` code; run
|
|
4
|
+
* time problems are `NoeticErrorImpl`s with a `connection_*` kind, or
|
|
5
|
+
* `steering_denied` when a gate refused.
|
|
6
|
+
*/
|
|
7
|
+
import type { ConnectionErrorPayload, ConnectionInputRequest } from '@noetic-tools/types';
|
|
8
|
+
import { NoeticConfigError, NoeticErrorImpl } from '@noetic-tools/types';
|
|
9
|
+
/** `CONNECTION_UNKNOWN`: a handle, node, trigger or allowlist names a connection the harness does not have. */
|
|
10
|
+
export declare function unknownConnectionError(name: string, known: ReadonlyArray<string>): NoeticConfigError;
|
|
11
|
+
/** `CONNECTION_INVALID`: a manifest entry, handle or harness entry is malformed. */
|
|
12
|
+
export declare function invalidConnectionError(message: string, hint: string): NoeticConfigError;
|
|
13
|
+
/** `CONNECTION_NOT_ALLOWED`: a request asks for more than it is granted, or sets a host-only setup field. */
|
|
14
|
+
export declare function notAllowedConnectionError(message: string, hint: string): NoeticConfigError;
|
|
15
|
+
/** `CONNECTION_TOOL_COLLISION`: two operations would reach the model under one tool name. */
|
|
16
|
+
export declare function toolCollisionError(toolName: string, first: string, second: string): NoeticConfigError;
|
|
17
|
+
/** `connection_unavailable`. */
|
|
18
|
+
export declare function unavailableError(connection: string, reason: string): NoeticErrorImpl;
|
|
19
|
+
/** `connection_failed`. */
|
|
20
|
+
export declare function failedError(connection: string, op: string, error: ConnectionErrorPayload): NoeticErrorImpl;
|
|
21
|
+
/** `connection_ref_invalid`. */
|
|
22
|
+
export declare function refInvalidError(connection: string, reason: string): NoeticErrorImpl;
|
|
23
|
+
/** `connection_secret_refused`. */
|
|
24
|
+
export declare function secretRefusedError(connection: string, op: string): NoeticErrorImpl;
|
|
25
|
+
/** `connection_input_required`. */
|
|
26
|
+
export declare function inputRequiredError(connection: string, request: ConnectionInputRequest): NoeticErrorImpl;
|
|
27
|
+
/** `connection_resume_refused`. */
|
|
28
|
+
export declare function resumeRefusedError(connection: string, reason: string): NoeticErrorImpl;
|
|
29
|
+
/** `steering_denied`: a gate refused the call. */
|
|
30
|
+
export declare function deniedError(guidance: string): NoeticErrorImpl;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error constructors for the connection runtime (spec 35 § Errors). Setup and
|
|
3
|
+
* validation problems are `NoeticConfigError`s with a `CONNECTION_*` code; run
|
|
4
|
+
* time problems are `NoeticErrorImpl`s with a `connection_*` kind, or
|
|
5
|
+
* `steering_denied` when a gate refused.
|
|
6
|
+
*/
|
|
7
|
+
import { ConnectionConfigErrorCode, NoeticConfigError, NoeticErrorImpl } from '@noetic-tools/types';
|
|
8
|
+
//#region Config errors
|
|
9
|
+
/** `CONNECTION_UNKNOWN`: a handle, node, trigger or allowlist names a connection the harness does not have. */
|
|
10
|
+
export function unknownConnectionError(name, known) {
|
|
11
|
+
const list = known.length > 0 ? known.join(', ') : '(none)';
|
|
12
|
+
return new NoeticConfigError({
|
|
13
|
+
code: ConnectionConfigErrorCode.Unknown,
|
|
14
|
+
message: `The harness has no connection named '${name}'. Known: ${list}.`,
|
|
15
|
+
hint: `Add '${name}' to the harness \`connections\`, or point the handle at an existing one with \`from\`.`,
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
/** `CONNECTION_INVALID`: a manifest entry, handle or harness entry is malformed. */
|
|
19
|
+
export function invalidConnectionError(message, hint) {
|
|
20
|
+
return new NoeticConfigError({
|
|
21
|
+
code: ConnectionConfigErrorCode.Invalid,
|
|
22
|
+
message,
|
|
23
|
+
hint,
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
/** `CONNECTION_NOT_ALLOWED`: a request asks for more than it is granted, or sets a host-only setup field. */
|
|
27
|
+
export function notAllowedConnectionError(message, hint) {
|
|
28
|
+
return new NoeticConfigError({
|
|
29
|
+
code: ConnectionConfigErrorCode.NotAllowed,
|
|
30
|
+
message,
|
|
31
|
+
hint,
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
/** `CONNECTION_TOOL_COLLISION`: two operations would reach the model under one tool name. */
|
|
35
|
+
export function toolCollisionError(toolName, first, second) {
|
|
36
|
+
return new NoeticConfigError({
|
|
37
|
+
code: ConnectionConfigErrorCode.ToolCollision,
|
|
38
|
+
message: `Tool name '${toolName}' would be used by both ${first} and ${second}.`,
|
|
39
|
+
hint: "Rename one connection, or set `expose: 'none'` (or `'eager'`) on one of them.",
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
//#endregion
|
|
43
|
+
//#region Run-time errors
|
|
44
|
+
/** `connection_unavailable`. */
|
|
45
|
+
export function unavailableError(connection, reason) {
|
|
46
|
+
return new NoeticErrorImpl({
|
|
47
|
+
kind: 'connection_unavailable',
|
|
48
|
+
connection,
|
|
49
|
+
reason,
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
/** `connection_failed`. */
|
|
53
|
+
export function failedError(connection, op, error) {
|
|
54
|
+
return new NoeticErrorImpl({
|
|
55
|
+
kind: 'connection_failed',
|
|
56
|
+
connection,
|
|
57
|
+
op,
|
|
58
|
+
error,
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
/** `connection_ref_invalid`. */
|
|
62
|
+
export function refInvalidError(connection, reason) {
|
|
63
|
+
return new NoeticErrorImpl({
|
|
64
|
+
kind: 'connection_ref_invalid',
|
|
65
|
+
connection,
|
|
66
|
+
reason,
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
/** `connection_secret_refused`. */
|
|
70
|
+
export function secretRefusedError(connection, op) {
|
|
71
|
+
return new NoeticErrorImpl({
|
|
72
|
+
kind: 'connection_secret_refused',
|
|
73
|
+
connection,
|
|
74
|
+
op,
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
/** `connection_input_required`. */
|
|
78
|
+
export function inputRequiredError(connection, request) {
|
|
79
|
+
return new NoeticErrorImpl({
|
|
80
|
+
kind: 'connection_input_required',
|
|
81
|
+
connection,
|
|
82
|
+
request,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
/** `connection_resume_refused`. */
|
|
86
|
+
export function resumeRefusedError(connection, reason) {
|
|
87
|
+
return new NoeticErrorImpl({
|
|
88
|
+
kind: 'connection_resume_refused',
|
|
89
|
+
connection,
|
|
90
|
+
reason,
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
/** `steering_denied`: a gate refused the call. */
|
|
94
|
+
export function deniedError(guidance) {
|
|
95
|
+
return new NoeticErrorImpl({
|
|
96
|
+
kind: 'steering_denied',
|
|
97
|
+
guidance,
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
//#endregion
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Waits listen in memory (spec 35 § Events): from the moment the server
|
|
3
|
+
* confirms the subscription, events are pumped into a buffer of up to
|
|
4
|
+
* `WAIT_BUFFER_LIMIT`. Overflow drops the oldest event and records a gap,
|
|
5
|
+
* which verification reports as a blind spot. Each event is transformed on the
|
|
6
|
+
* way in (stamped with the connection, refs sealed, secrets scrubbed).
|
|
7
|
+
*/
|
|
8
|
+
import type { ConnectionEvent, ConnectionEventInit, EventStream } from '@noetic-tools/types';
|
|
9
|
+
/** How a buffered stream treats what comes through it. */
|
|
10
|
+
export interface BufferedStreamOptions {
|
|
11
|
+
/** Default `WAIT_BUFFER_LIMIT`. */
|
|
12
|
+
readonly limit?: number;
|
|
13
|
+
/** Turns a provider event into the event the harness hands on. */
|
|
14
|
+
readonly transform: (event: ConnectionEventInit) => Promise<ConnectionEvent>;
|
|
15
|
+
/** Called for every gap (including overflow) and for the stream's end. */
|
|
16
|
+
readonly onChange?: (change: {
|
|
17
|
+
readonly change: 'gap' | 'ended';
|
|
18
|
+
readonly reason: string;
|
|
19
|
+
}) => void;
|
|
20
|
+
/** Called for every event delivered into the buffer. */
|
|
21
|
+
readonly onEvent?: (event: ConnectionEvent) => void;
|
|
22
|
+
}
|
|
23
|
+
/** Wraps a provider stream in a bounded, transforming buffer. */
|
|
24
|
+
export declare function bufferedEventStream(source: EventStream, options: BufferedStreamOptions): EventStream<ConnectionEvent>;
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Waits listen in memory (spec 35 § Events): from the moment the server
|
|
3
|
+
* confirms the subscription, events are pumped into a buffer of up to
|
|
4
|
+
* `WAIT_BUFFER_LIMIT`. Overflow drops the oldest event and records a gap,
|
|
5
|
+
* which verification reports as a blind spot. Each event is transformed on the
|
|
6
|
+
* way in (stamped with the connection, refs sealed, secrets scrubbed).
|
|
7
|
+
*/
|
|
8
|
+
import { WAIT_BUFFER_LIMIT } from '@noetic-tools/types';
|
|
9
|
+
//#endregion
|
|
10
|
+
//#region Buffer
|
|
11
|
+
function done() {
|
|
12
|
+
return {
|
|
13
|
+
value: undefined,
|
|
14
|
+
done: true,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
function dropOldestEvents(state) {
|
|
18
|
+
let dropped = 0;
|
|
19
|
+
while (state.buffered > state.limit) {
|
|
20
|
+
const index = state.queue.findIndex((m) => m.type === 'event');
|
|
21
|
+
state.queue.splice(index, 1);
|
|
22
|
+
state.buffered -= 1;
|
|
23
|
+
dropped += 1;
|
|
24
|
+
}
|
|
25
|
+
return dropped;
|
|
26
|
+
}
|
|
27
|
+
/** Records `dropped` overflowed events as one gap at the head, merging with an unread one. */
|
|
28
|
+
function recordOverflow(state, dropped) {
|
|
29
|
+
const merging = state.overflowGap !== undefined && state.queue[0] === state.overflowGap;
|
|
30
|
+
state.droppedTotal = merging ? state.droppedTotal + dropped : dropped;
|
|
31
|
+
const reason = `wait buffer overflowed: ${state.droppedTotal} oldest event(s) dropped`;
|
|
32
|
+
const gap = {
|
|
33
|
+
type: 'gap',
|
|
34
|
+
reason,
|
|
35
|
+
};
|
|
36
|
+
if (merging) {
|
|
37
|
+
state.queue[0] = gap;
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
state.queue.unshift(gap);
|
|
41
|
+
}
|
|
42
|
+
state.overflowGap = gap;
|
|
43
|
+
return reason;
|
|
44
|
+
}
|
|
45
|
+
function trim(state, onChange) {
|
|
46
|
+
const dropped = dropOldestEvents(state);
|
|
47
|
+
if (dropped === 0) {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
const reason = recordOverflow(state, dropped);
|
|
51
|
+
onChange?.({
|
|
52
|
+
change: 'gap',
|
|
53
|
+
reason,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
function deliver(state, message, onChange) {
|
|
57
|
+
const waiter = state.waiters.shift();
|
|
58
|
+
if (waiter) {
|
|
59
|
+
waiter({
|
|
60
|
+
value: message,
|
|
61
|
+
done: false,
|
|
62
|
+
});
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
state.queue.push(message);
|
|
66
|
+
if (message.type === 'event') {
|
|
67
|
+
state.buffered += 1;
|
|
68
|
+
}
|
|
69
|
+
trim(state, onChange);
|
|
70
|
+
}
|
|
71
|
+
function finish(state) {
|
|
72
|
+
state.ended = true;
|
|
73
|
+
for (const waiter of state.waiters.splice(0)) {
|
|
74
|
+
waiter(done());
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
function takeNext(state) {
|
|
78
|
+
const message = state.queue.shift();
|
|
79
|
+
if (message?.type === 'event') {
|
|
80
|
+
state.buffered -= 1;
|
|
81
|
+
}
|
|
82
|
+
if (message) {
|
|
83
|
+
return Promise.resolve({
|
|
84
|
+
value: message,
|
|
85
|
+
done: false,
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
if (state.ended) {
|
|
89
|
+
return Promise.resolve(done());
|
|
90
|
+
}
|
|
91
|
+
return new Promise((resolve) => {
|
|
92
|
+
state.waiters.push(resolve);
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
//#endregion
|
|
96
|
+
//#region Pump
|
|
97
|
+
/** Hands one source message on; returns whether the stream ended with it. */
|
|
98
|
+
async function forwardMessage(state, message, options) {
|
|
99
|
+
if (message.type === 'event') {
|
|
100
|
+
const event = await options.transform(message.event);
|
|
101
|
+
options.onEvent?.(event);
|
|
102
|
+
deliver(state, {
|
|
103
|
+
type: 'event',
|
|
104
|
+
event,
|
|
105
|
+
}, options.onChange);
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
const change = message.type === 'gap' ? 'gap' : 'ended';
|
|
109
|
+
options.onChange?.({
|
|
110
|
+
change,
|
|
111
|
+
reason: message.reason,
|
|
112
|
+
});
|
|
113
|
+
deliver(state, message, options.onChange);
|
|
114
|
+
return change === 'ended';
|
|
115
|
+
}
|
|
116
|
+
async function pump(state, source, options) {
|
|
117
|
+
try {
|
|
118
|
+
for await (const message of source) {
|
|
119
|
+
if (state.closed || (await forwardMessage(state, message, options))) {
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
catch (e) {
|
|
125
|
+
if (state.closed) {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
const reason = e instanceof Error ? e.message : String(e);
|
|
129
|
+
options.onChange?.({
|
|
130
|
+
change: 'ended',
|
|
131
|
+
reason,
|
|
132
|
+
});
|
|
133
|
+
deliver(state, {
|
|
134
|
+
type: 'terminated',
|
|
135
|
+
reason,
|
|
136
|
+
}, options.onChange);
|
|
137
|
+
}
|
|
138
|
+
finally {
|
|
139
|
+
finish(state);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
//#endregion
|
|
143
|
+
//#region Public API
|
|
144
|
+
/** Wraps a provider stream in a bounded, transforming buffer. */
|
|
145
|
+
export function bufferedEventStream(source, options) {
|
|
146
|
+
const state = {
|
|
147
|
+
limit: options.limit ?? WAIT_BUFFER_LIMIT,
|
|
148
|
+
queue: [],
|
|
149
|
+
waiters: [],
|
|
150
|
+
ended: false,
|
|
151
|
+
closed: false,
|
|
152
|
+
buffered: 0,
|
|
153
|
+
droppedTotal: 0,
|
|
154
|
+
overflowGap: undefined,
|
|
155
|
+
};
|
|
156
|
+
const closeSource = async () => {
|
|
157
|
+
state.closed = true;
|
|
158
|
+
finish(state);
|
|
159
|
+
await source.close();
|
|
160
|
+
};
|
|
161
|
+
void source.ready.then(() => pump(state, source, options), () => finish(state));
|
|
162
|
+
return {
|
|
163
|
+
ready: source.ready,
|
|
164
|
+
close: closeSource,
|
|
165
|
+
[Symbol.asyncIterator]() {
|
|
166
|
+
return {
|
|
167
|
+
next: () => takeNext(state),
|
|
168
|
+
async return() {
|
|
169
|
+
await closeSource();
|
|
170
|
+
return done();
|
|
171
|
+
},
|
|
172
|
+
};
|
|
173
|
+
},
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
//#endregion
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The gates (spec 35 § Gates). Every connection call — from the model, a
|
|
3
|
+
* step, or a document — passes the same gates, in order:
|
|
4
|
+
*
|
|
5
|
+
* 1. The operation exists on the connection, and for steps and documents is
|
|
6
|
+
* declared by the manifest or handle.
|
|
7
|
+
* 2. Its arguments match the operation's schema. Nothing is defaulted.
|
|
8
|
+
* 3. Its refs were issued by this harness to this thread, for this connection.
|
|
9
|
+
* 4. The connection's `policy`; `guarded` operations need approval unless the
|
|
10
|
+
* policy allows them.
|
|
11
|
+
* 5. `beforeToolCall` layers and the harness approver, as for any tool.
|
|
12
|
+
* 6. Secrets are filled in, and the call is sent.
|
|
13
|
+
*
|
|
14
|
+
* A refusal throws `steering_denied` (a step fails with it; a model tool
|
|
15
|
+
* returns it as a tool error). A server that refuses with `guarded` is asked
|
|
16
|
+
* for approval and retried with `_meta["tools.noetic/confirmed"]`. Long
|
|
17
|
+
* operations are waited on, with progress reported as `connection_task`.
|
|
18
|
+
*/
|
|
19
|
+
import type { ArtifactStore, Connection, ConnectionPolicy, ConnectionTaskStatus, Context, SecretResolver, SteeringDecision } from '@noetic-tools/types';
|
|
20
|
+
import type { RefScope, RefSealer } from './refs.js';
|
|
21
|
+
import type { ConnectionCaller, GatedCallOptions } from './registry-types.js';
|
|
22
|
+
import type { SecretScrubber } from './secrets.js';
|
|
23
|
+
/** A call as the harness's own gates (layers, approver) see it. */
|
|
24
|
+
export interface GateRequest {
|
|
25
|
+
readonly connection: string;
|
|
26
|
+
readonly op: string;
|
|
27
|
+
/** The arguments with secrets still unfilled. */
|
|
28
|
+
readonly args: unknown;
|
|
29
|
+
readonly caller: ConnectionCaller;
|
|
30
|
+
/** The name a tool pipeline would know the call by: `<connection>_<op>`. */
|
|
31
|
+
readonly toolName: string;
|
|
32
|
+
readonly callId?: string;
|
|
33
|
+
readonly ctx?: Context;
|
|
34
|
+
/** Why approval is being asked: a policy or profile flag, or a server's `guarded` refusal. */
|
|
35
|
+
readonly reason?: 'policy' | 'guarded';
|
|
36
|
+
}
|
|
37
|
+
/** The harness hooks gate 5 runs, and who answers long operations' questions. */
|
|
38
|
+
export interface GateHooks {
|
|
39
|
+
/** `beforeToolCall` layers. Skipped for model calls, whose tool pipeline already ran them. */
|
|
40
|
+
beforeCall?(request: GateRequest): Promise<SteeringDecision>;
|
|
41
|
+
/** The harness approver. Without one, calls that need approval are refused. */
|
|
42
|
+
approve?(request: GateRequest): Promise<SteeringDecision>;
|
|
43
|
+
/** Answers an `input_required` question; `undefined` when nobody can. */
|
|
44
|
+
answerInput?(request: GateRequest & {
|
|
45
|
+
readonly status: ConnectionTaskStatus;
|
|
46
|
+
}): Promise<{
|
|
47
|
+
readonly answer: unknown;
|
|
48
|
+
} | undefined>;
|
|
49
|
+
}
|
|
50
|
+
/** Everything one gated call runs against. */
|
|
51
|
+
export interface GateContext {
|
|
52
|
+
readonly name: string;
|
|
53
|
+
readonly connection: Connection;
|
|
54
|
+
readonly policy?: ConnectionPolicy;
|
|
55
|
+
readonly executionId: string;
|
|
56
|
+
readonly refScope: RefScope;
|
|
57
|
+
readonly refs: RefSealer;
|
|
58
|
+
readonly scrubber: SecretScrubber;
|
|
59
|
+
readonly secrets?: SecretResolver;
|
|
60
|
+
readonly artifacts: ArtifactStore;
|
|
61
|
+
readonly hooks: GateHooks;
|
|
62
|
+
/** Progress of a long operation (the registry emits `connection_task`). */
|
|
63
|
+
readonly onTask?: (op: string, status: ConnectionTaskStatus, stepId?: string) => void;
|
|
64
|
+
/** An operation with effects ran (connection views re-read `'effect'` reads). */
|
|
65
|
+
readonly onEffect?: () => void;
|
|
66
|
+
}
|
|
67
|
+
/** Scrubs secrets, seals refs and links blobs in a value coming in from a connection. */
|
|
68
|
+
export declare function admitIncoming(gc: Pick<GateContext, 'scrubber' | 'refs' | 'refScope' | 'artifacts' | 'executionId' | 'name'>, value: unknown, stepPath?: string): Promise<unknown>;
|
|
69
|
+
/**
|
|
70
|
+
* Waits on a long operation a previous attempt started, instead of sending the
|
|
71
|
+
* operation again (spec 35 § Durability: "waiting on the same task"). Its
|
|
72
|
+
* outcome is handled exactly as `gatedCall`'s.
|
|
73
|
+
*
|
|
74
|
+
* @throws NoeticErrorImpl `connection_failed` or `connection_input_required`.
|
|
75
|
+
*/
|
|
76
|
+
export declare function gatedAwaitTask(gc: GateContext, task: {
|
|
77
|
+
readonly op: string;
|
|
78
|
+
readonly taskId: string;
|
|
79
|
+
readonly opts: GatedCallOptions;
|
|
80
|
+
}): Promise<unknown>;
|
|
81
|
+
/**
|
|
82
|
+
* Runs one call through gates 1–6 and returns its output.
|
|
83
|
+
*
|
|
84
|
+
* @throws NoeticErrorImpl `steering_denied` when a gate refuses,
|
|
85
|
+
* `connection_ref_invalid`, `connection_secret_refused`, `connection_failed`
|
|
86
|
+
* or `connection_input_required`.
|
|
87
|
+
*/
|
|
88
|
+
export declare function gatedCall(gc: GateContext, call: {
|
|
89
|
+
readonly op: string;
|
|
90
|
+
readonly args: unknown;
|
|
91
|
+
readonly opts: GatedCallOptions & {
|
|
92
|
+
readonly ctx?: Context;
|
|
93
|
+
};
|
|
94
|
+
}): Promise<unknown>;
|