@noetic-tools/core 5.14.1 → 5.15.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/dist/builders/spawn-builder.d.ts +7 -0
- package/dist/builders/spawn-builder.js +2 -0
- package/dist/builders/workflow-hydrator.js +1 -0
- package/dist/harness/agent-harness.d.ts +2 -1
- package/dist/harness/agent-harness.js +32 -17
- package/dist/harness/deps/context.d.ts +1 -1
- package/dist/harness/deps/context.js +1 -1
- package/dist/harness/deps/runtime.d.ts +2 -1
- package/dist/harness/deps/runtime.js +1 -0
- package/dist/harness/deps/types.d.ts +1 -1
- package/dist/harness/model-call.d.ts +12 -12
- package/dist/harness/model-call.js +191 -116
- package/dist/harness/model-rounds.d.ts +1 -1
- package/dist/harness/model-rounds.js +1 -1
- package/dist/harness/steer-inbox.d.ts +51 -0
- package/dist/harness/steer-inbox.js +149 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/interpreter/action-deps.d.ts +2 -0
- package/dist/interpreter/action-deps.js +1 -0
- package/dist/interpreter/action-types.d.ts +1 -1
- package/dist/interpreter/execute-action.js +44 -1
- package/dist/runtime/durable/step-ledger.js +80 -15
- package/dist/runtime/message-queue.d.ts +15 -1
- package/dist/runtime/message-queue.js +54 -0
- package/dist/runtime/session-runner.d.ts +3 -0
- package/dist/runtime/session-runner.js +18 -29
- package/package.json +3 -3
- package/schema/noetic-workflow.schema.json +7 -0
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { frameworkCast, SteerAction, SteerPoint } from '@noetic-tools/types';
|
|
2
|
+
import { inputToItems, isSteer } from '../runtime/message-queue.js';
|
|
3
|
+
//#endregion
|
|
4
|
+
//#region Public API
|
|
5
|
+
/**
|
|
6
|
+
* @internal Open the steer inbox for a model call, or `undefined` when the call
|
|
7
|
+
* runs outside a session. A context inherits its parent's session only when it
|
|
8
|
+
* was spawned with `steers: 'forward'`; parallel paths and other spawns hold
|
|
9
|
+
* steers until control returns to the session's own context.
|
|
10
|
+
*/
|
|
11
|
+
export function openSteerInbox(opts) {
|
|
12
|
+
const { ctx } = opts;
|
|
13
|
+
if (!ctx) {
|
|
14
|
+
return undefined;
|
|
15
|
+
}
|
|
16
|
+
const queue = findSessionQueue(ctx);
|
|
17
|
+
if (!queue) {
|
|
18
|
+
return undefined;
|
|
19
|
+
}
|
|
20
|
+
return new SteerInbox({
|
|
21
|
+
...opts,
|
|
22
|
+
ctx,
|
|
23
|
+
queue,
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* @internal The steers a running model call can take. The call offers queued
|
|
28
|
+
* steers to the context layers at each steer point; those the layers apply are
|
|
29
|
+
* staged and handed over before the next model call.
|
|
30
|
+
*/
|
|
31
|
+
export class SteerInbox {
|
|
32
|
+
opts;
|
|
33
|
+
staged = {
|
|
34
|
+
messageIds: [],
|
|
35
|
+
items: [],
|
|
36
|
+
};
|
|
37
|
+
/** Steers already reported as waiting on tool calls, so each is reported once. */
|
|
38
|
+
reportedPending = new Set();
|
|
39
|
+
constructor(opts) {
|
|
40
|
+
this.opts = opts;
|
|
41
|
+
}
|
|
42
|
+
hasQueued() {
|
|
43
|
+
return this.opts.queue.peekAll().some(isSteer);
|
|
44
|
+
}
|
|
45
|
+
hasStaged() {
|
|
46
|
+
return this.staged.messageIds.length > 0;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Offer every queued steer at `point`. Returns `true` when at least one was
|
|
50
|
+
* staged by this offer.
|
|
51
|
+
*/
|
|
52
|
+
async offer(point, pendingCalls = []) {
|
|
53
|
+
let stagedAny = false;
|
|
54
|
+
for (const message of this.opts.queue.peekAll().filter(isSteer)) {
|
|
55
|
+
const verdict = await this.judge(message, point, pendingCalls);
|
|
56
|
+
if (verdict.action === SteerAction.Defer) {
|
|
57
|
+
this.reportPending(message, point, pendingCalls);
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
// A sibling call may have taken it while the layers were judging.
|
|
61
|
+
if (this.opts.queue.take((queued) => queued === message).length === 0) {
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
if (verdict.action === SteerAction.Drop) {
|
|
65
|
+
this.opts.emit('steer_dropped', {
|
|
66
|
+
messageId: message.id,
|
|
67
|
+
point,
|
|
68
|
+
});
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
await this.stage(message.id, verdict.items);
|
|
72
|
+
stagedAny = true;
|
|
73
|
+
}
|
|
74
|
+
return stagedAny;
|
|
75
|
+
}
|
|
76
|
+
/** Hand over the staged steers and clear them. */
|
|
77
|
+
takeStaged() {
|
|
78
|
+
const staged = this.staged;
|
|
79
|
+
this.staged = {
|
|
80
|
+
messageIds: [],
|
|
81
|
+
items: [],
|
|
82
|
+
};
|
|
83
|
+
return staged;
|
|
84
|
+
}
|
|
85
|
+
async judge(message, point, pendingCalls) {
|
|
86
|
+
const action = defaultAction(point, message.deliveryMode);
|
|
87
|
+
const items = inputToItems(message.input);
|
|
88
|
+
const { harness, layers } = this.opts;
|
|
89
|
+
if (!harness.onSteer || !layers.some((layer) => layer.hooks.onSteer)) {
|
|
90
|
+
return {
|
|
91
|
+
action,
|
|
92
|
+
items,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
return harness.onSteer(layers, {
|
|
96
|
+
steer: {
|
|
97
|
+
id: message.id,
|
|
98
|
+
items,
|
|
99
|
+
},
|
|
100
|
+
point,
|
|
101
|
+
action,
|
|
102
|
+
pendingCalls,
|
|
103
|
+
}, this.opts.ctx);
|
|
104
|
+
}
|
|
105
|
+
/** Steers are user input, so they pass through `onItemAppend` like any other.
|
|
106
|
+
* Re-render requests are ignored: recall does not run again mid-call. */
|
|
107
|
+
async stage(messageId, items) {
|
|
108
|
+
const piped = this.opts.layers.length > 0
|
|
109
|
+
? (await this.opts.harness.runAppendPipeline(this.opts.layers, items, this.opts.ctx)).items
|
|
110
|
+
: items;
|
|
111
|
+
this.staged.messageIds.push(messageId);
|
|
112
|
+
this.staged.items.push(...piped);
|
|
113
|
+
}
|
|
114
|
+
reportPending(message, point, pendingCalls) {
|
|
115
|
+
if (point !== SteerPoint.BeforeToolCall || this.reportedPending.has(message.id)) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
this.reportedPending.add(message.id);
|
|
119
|
+
this.opts.emit('steer_pending', {
|
|
120
|
+
messageId: message.id,
|
|
121
|
+
waitingOn: pendingCalls.map((call) => call.callId),
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
//#endregion
|
|
126
|
+
//#region Helpers
|
|
127
|
+
function findSessionQueue(ctx) {
|
|
128
|
+
for (let current = ctx; current; current = current.parent) {
|
|
129
|
+
const ext = frameworkCast(current);
|
|
130
|
+
if (ext._sessionQueue) {
|
|
131
|
+
return ext._sessionQueue;
|
|
132
|
+
}
|
|
133
|
+
if (!ext._forwardSteers) {
|
|
134
|
+
return undefined;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return undefined;
|
|
138
|
+
}
|
|
139
|
+
/** What each delivery mode does at each point before any layer has a say. */
|
|
140
|
+
function defaultAction(point, mode) {
|
|
141
|
+
if (point === SteerPoint.BeforeRound) {
|
|
142
|
+
return SteerAction.Apply;
|
|
143
|
+
}
|
|
144
|
+
if (point === SteerPoint.ItemDone && mode === 'next-item') {
|
|
145
|
+
return SteerAction.Apply;
|
|
146
|
+
}
|
|
147
|
+
return SteerAction.Defer;
|
|
148
|
+
}
|
|
149
|
+
//#endregion
|
package/dist/index.d.ts
CHANGED
|
@@ -162,9 +162,9 @@ export type { FrameworkStreamEvent, HarnessResponse, SdkStreamEvent, StreamEvent
|
|
|
162
162
|
/** @public */
|
|
163
163
|
export type { AgentConfig, AgentHarnessContract, AgentHooks, CallModelRequest, ContextCacheConfig, ContextCacheStore, ContextEpoch, DeliveryMode, ExecuteOptions, HarnessJudgmentContract, HarnessStatus, ItemSchemaConfig, ReanchorReason, RecallLayerOutput, SessionScope, SessionUsage, } from '@noetic-tools/types';
|
|
164
164
|
/** @public */
|
|
165
|
-
export type { AfterModelCallParams, AfterModelCallResult, ApproveToolCallParams, BeforeToolCallParams, BeforeToolCallResult, LedgerEntry, SteeringConfig, SteeringDecision, SteeringRule, SteeringState, ToolApprover, } from '@noetic-tools/types';
|
|
165
|
+
export type { AfterModelCallParams, AfterModelCallResult, ApproveToolCallParams, BeforeToolCallParams, BeforeToolCallResult, LedgerEntry, OnSteerParams, OnSteerResult, Steer, SteeringConfig, SteeringDecision, SteeringRule, SteeringState, SteerVerdict, ToolApprover, } from '@noetic-tools/types';
|
|
166
166
|
/** @public */
|
|
167
|
-
export { LedgerEntryKind, SteeringAction } from '@noetic-tools/types';
|
|
167
|
+
export { LedgerEntryKind, SteerAction, SteeringAction, SteerPoint } from '@noetic-tools/types';
|
|
168
168
|
/** @public */
|
|
169
169
|
export type { ExecuteStepFn, MergeFn, SettleResult, Snapshot, Step, StepAcpAgent, StepCallModel, StepConditional, StepEmitItems, StepEmitItemsFn, StepEmitItemsOption, StepEmitItemsResult, StepInParallel, StepInParallelAll, StepInParallelRace, StepInParallelSettle, StepInvokeTool, StepLoop, StepReplay, StepRunCode, StepSpawn, StepWithContext, Until, UntilRun, Verdict, } from '@noetic-tools/types';
|
|
170
170
|
/** @public */
|
package/dist/index.js
CHANGED
|
@@ -101,7 +101,7 @@ export { ChannelSnapshotSchema, CheckpointSchemaVersion, CheckpointSnapshotSchem
|
|
|
101
101
|
/** @public */
|
|
102
102
|
export { Slot } from '@noetic-tools/context';
|
|
103
103
|
/** @public */
|
|
104
|
-
export { LedgerEntryKind, SteeringAction } from '@noetic-tools/types';
|
|
104
|
+
export { LedgerEntryKind, SteerAction, SteeringAction, SteerPoint } from '@noetic-tools/types';
|
|
105
105
|
/** @public */
|
|
106
106
|
export { BooleanQuestionSchema, ChoiceQuestionSchema, choiceValues, DECIDE_LIMITS, DecideOutputError, DecideQuestionSchema, DecideQuestionsSchema, routeOutcomes, ScoreQuestionSchema, scoreLevels, } from '@noetic-tools/types';
|
|
107
107
|
/** @public */
|
|
@@ -5,4 +5,6 @@ export { defaultItemSchemaRegistry } from '@noetic-tools/types';
|
|
|
5
5
|
export { emitFrameworkEvent, getBroadcaster, shouldEmit } from '../runtime/broadcaster-utils.js';
|
|
6
6
|
export { ContextImpl, laneBase } from '../runtime/context-impl.js';
|
|
7
7
|
export { snapshotCwdState } from '../runtime/cwd-helpers.js';
|
|
8
|
+
export type { SessionCtxExtension } from '../runtime/message-queue.js';
|
|
9
|
+
export { INTERRUPT_REASON } from '../runtime/message-queue.js';
|
|
8
10
|
export { buildToolExecutionContext } from '../runtime/tool-context.js';
|
|
@@ -3,4 +3,5 @@ export { defaultItemSchemaRegistry } from '@noetic-tools/types';
|
|
|
3
3
|
export { emitFrameworkEvent, getBroadcaster, shouldEmit } from '../runtime/broadcaster-utils.js';
|
|
4
4
|
export { ContextImpl, laneBase } from '../runtime/context-impl.js';
|
|
5
5
|
export { snapshotCwdState } from '../runtime/cwd-helpers.js';
|
|
6
|
+
export { INTERRUPT_REASON } from '../runtime/message-queue.js';
|
|
6
7
|
export { buildToolExecutionContext } from '../runtime/tool-context.js';
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export type { ContextConfig, ContextData, ContextLayer, ExecutionContext, } from '@noetic-tools/context';
|
|
2
|
-
export type { AgentHarnessContract, CallModelRequest, Context, ContextRerenderRequest, ExecuteStepFn, FunctionCallItem, InputMessageItem, Item, LLMResponse, ProjectionPolicy, RecallLayerOutput, RetryPolicy, ServerToolSpec, StepCallModel, StepDecide, StepInvokeTool, StepMeta, StepRunCode, StepSpawn, StepWithContext, StorageAdapter, Tool, } from '@noetic-tools/types';
|
|
2
|
+
export type { AgentHarnessContract, CallModelRequest, Context, ContextRerenderRequest, ExecuteStepFn, FunctionCallItem, FunctionCallOutputItem, InputMessageItem, Item, LLMResponse, ProjectionPolicy, RecallLayerOutput, RetryPolicy, ServerToolSpec, StepCallModel, StepDecide, StepInvokeTool, StepMeta, StepRunCode, StepSpawn, StepWithContext, StorageAdapter, Tool, } from '@noetic-tools/types';
|
|
3
3
|
export { isServerToolSpec, SteeringAction } from '@noetic-tools/types';
|
|
@@ -5,7 +5,7 @@ import { createMessage, estimateTokens, extractAssistantText, frameworkCast, isN
|
|
|
5
5
|
import { ToolAttr } from '../observability/genai-attributes.js';
|
|
6
6
|
import { askApproval } from '../runtime/tool-approval.js';
|
|
7
7
|
import { emitToolUi } from '../runtime/tool-ui.js';
|
|
8
|
-
import { allocateBudgets, assembleView, buildToolExecutionContext, ContextImpl, commitLayerUsage, computeLayerUsage, contextToExecCtx, createContextCacheStore, DEFAULT_PROJECTION, defaultItemSchemaRegistry, disposeLayers, emitFrameworkEvent, getBroadcaster, initLayers, lineageKey, noteCacheOutcome, resolveCacheConfig, resolveLayerTools, returnLayers, shouldEmit, snapshotCwdState, spawnLayers, unpinLayers, } from './action-deps.js';
|
|
8
|
+
import { allocateBudgets, assembleView, buildToolExecutionContext, ContextImpl, commitLayerUsage, computeLayerUsage, contextToExecCtx, createContextCacheStore, DEFAULT_PROJECTION, defaultItemSchemaRegistry, disposeLayers, emitFrameworkEvent, getBroadcaster, INTERRUPT_REASON, initLayers, lineageKey, noteCacheOutcome, resolveCacheConfig, resolveLayerTools, returnLayers, shouldEmit, snapshotCwdState, spawnLayers, unpinLayers, } from './action-deps.js';
|
|
9
9
|
import { isServerToolSpec, SteeringAction } from './action-types.js';
|
|
10
10
|
import { openChildSpan, traceChild } from './child-span.js';
|
|
11
11
|
import { cloneWithGuard } from './clone-guard.js';
|
|
@@ -420,6 +420,42 @@ function buildModelRequest(params) {
|
|
|
420
420
|
}
|
|
421
421
|
return base;
|
|
422
422
|
}
|
|
423
|
+
/**
|
|
424
|
+
* Keep what an interrupted call finished, so the restarted turn knows what
|
|
425
|
+
* already ran. A call cut off mid-round leaves tool calls with no output,
|
|
426
|
+
* which no provider accepts in history, so each gets one.
|
|
427
|
+
*/
|
|
428
|
+
function keepFinishedItems(ctx, items) {
|
|
429
|
+
const answered = new Set(items
|
|
430
|
+
.filter((item) => item.type === 'function_call_output')
|
|
431
|
+
.map((item) => item.callId));
|
|
432
|
+
for (const item of items) {
|
|
433
|
+
ctx.itemLog.append(item);
|
|
434
|
+
}
|
|
435
|
+
for (const call of items.filter(isFunctionCall)) {
|
|
436
|
+
if (answered.has(call.callId)) {
|
|
437
|
+
continue;
|
|
438
|
+
}
|
|
439
|
+
ctx.itemLog.append({
|
|
440
|
+
id: `cancelled-${call.callId}`,
|
|
441
|
+
type: 'function_call_output',
|
|
442
|
+
status: 'incomplete',
|
|
443
|
+
callId: call.callId,
|
|
444
|
+
output: 'Cancelled before it ran.',
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
/**
|
|
449
|
+
* A guardrail retry throws the response away, but steers in it came from the
|
|
450
|
+
* user, not the model. Log them so the retry still sees them.
|
|
451
|
+
*/
|
|
452
|
+
function keepSteers(ctx, items) {
|
|
453
|
+
for (const item of items) {
|
|
454
|
+
if (item.type === 'message' && item.role !== 'assistant') {
|
|
455
|
+
ctx.itemLog.append(item);
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
}
|
|
423
459
|
/**
|
|
424
460
|
* Records a finished model response on the context: appends response items to
|
|
425
461
|
* the log, publishes `lastStepMeta`, and commits the per-layer usage breakdown
|
|
@@ -587,6 +623,9 @@ export async function executeCallModel(step, input, ctx, layers) {
|
|
|
587
623
|
// the signal fired. The spend is charged above, but a truncated generation
|
|
588
624
|
// is not this step's output — surface cancellation instead.
|
|
589
625
|
if (baseCtx.aborted) {
|
|
626
|
+
if (baseCtx.abortReason === INTERRUPT_REASON) {
|
|
627
|
+
keepFinishedItems(baseCtx, response.items);
|
|
628
|
+
}
|
|
590
629
|
throw new NoeticErrorImpl({
|
|
591
630
|
kind: 'cancelled',
|
|
592
631
|
reason: baseCtx.abortReason ?? 'context aborted',
|
|
@@ -606,6 +645,7 @@ export async function executeCallModel(step, input, ctx, layers) {
|
|
|
606
645
|
// so the retry's view ends on the correction rather than burying it
|
|
607
646
|
// ahead of the live band. Assembly drops it from history to avoid
|
|
608
647
|
// showing it twice.
|
|
648
|
+
keepSteers(baseCtx, response.items);
|
|
609
649
|
baseCtx.itemLog.append(guidance);
|
|
610
650
|
steeringTail = [
|
|
611
651
|
...steeringTail,
|
|
@@ -740,6 +780,9 @@ export async function executeSpawn(step, input, ctx, executeStep, opts) {
|
|
|
740
780
|
// The child's steps checkpoint the root, as an inParallel path's do.
|
|
741
781
|
checkpointFn: () => baseCtx.checkpoint(),
|
|
742
782
|
});
|
|
783
|
+
if (step.steers === 'forward') {
|
|
784
|
+
frameworkCast(childCtx)._forwardSteers = true;
|
|
785
|
+
}
|
|
743
786
|
// `childExecutionCtx` predates the spawn span; the child's hooks trace onto that span.
|
|
744
787
|
const childTracedCtx = {
|
|
745
788
|
...childExecutionCtx,
|
|
@@ -233,7 +233,7 @@ export class StepLedger {
|
|
|
233
233
|
/** Recovered entries, by path. */
|
|
234
234
|
replayable = new Map();
|
|
235
235
|
/** Sequence number holding each live entry, recovered or recorded, by path. */
|
|
236
|
-
live;
|
|
236
|
+
live = new LiveEntries();
|
|
237
237
|
/** Sequence number the next recorded entry takes. */
|
|
238
238
|
seq;
|
|
239
239
|
store;
|
|
@@ -250,7 +250,6 @@ export class StepLedger {
|
|
|
250
250
|
this.executionId = opts.executionId;
|
|
251
251
|
this.store = opts.store;
|
|
252
252
|
this.retention = resolveStepLedgerRetention(opts.retention);
|
|
253
|
-
this.live = new Map();
|
|
254
253
|
this.seq = 0;
|
|
255
254
|
if (opts.recovered) {
|
|
256
255
|
this.adopt(opts.recovered);
|
|
@@ -342,7 +341,7 @@ export class StepLedger {
|
|
|
342
341
|
await this.deleteSeq(replaced);
|
|
343
342
|
}
|
|
344
343
|
if (!entry.path.startsWith(REPLAY_KEY_PREFIX)) {
|
|
345
|
-
await this.prune((
|
|
344
|
+
await this.prune(this.live.beneath(entry.path));
|
|
346
345
|
}
|
|
347
346
|
}
|
|
348
347
|
/**
|
|
@@ -351,7 +350,7 @@ export class StepLedger {
|
|
|
351
350
|
* `key:` entries stay — they are keyed by content, and a later run may ask the same.
|
|
352
351
|
*/
|
|
353
352
|
async dropPaths() {
|
|
354
|
-
await this.prune((path) => !path.startsWith(REPLAY_KEY_PREFIX));
|
|
353
|
+
await this.prune(this.live.paths().filter((path) => !path.startsWith(REPLAY_KEY_PREFIX)));
|
|
355
354
|
for (const path of [
|
|
356
355
|
...this.replayable.keys(),
|
|
357
356
|
]) {
|
|
@@ -361,10 +360,9 @@ export class StepLedger {
|
|
|
361
360
|
}
|
|
362
361
|
}
|
|
363
362
|
async prune(dead) {
|
|
364
|
-
for (const
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
if (!dead(path)) {
|
|
363
|
+
for (const path of dead) {
|
|
364
|
+
const seq = this.live.get(path);
|
|
365
|
+
if (seq === undefined) {
|
|
368
366
|
continue;
|
|
369
367
|
}
|
|
370
368
|
this.live.delete(path);
|
|
@@ -414,13 +412,7 @@ export class StepLedger {
|
|
|
414
412
|
if (this.live.size < this.retention.maxEntries || path.startsWith(REPLAY_KEY_PREFIX)) {
|
|
415
413
|
return this.live.size;
|
|
416
414
|
}
|
|
417
|
-
|
|
418
|
-
for (const key of this.live.keys()) {
|
|
419
|
-
if (isBeneath(key, path)) {
|
|
420
|
-
beneath += 1;
|
|
421
|
-
}
|
|
422
|
-
}
|
|
423
|
-
return this.live.size - beneath;
|
|
415
|
+
return this.live.size - this.live.beneath(path).length;
|
|
424
416
|
}
|
|
425
417
|
/** One warning per reason per execution — a long run would otherwise flood the log. */
|
|
426
418
|
warnOnFirstDrop(count, message) {
|
|
@@ -440,4 +432,77 @@ function matches(entry, step) {
|
|
|
440
432
|
function isBeneath(path, parent) {
|
|
441
433
|
return path.startsWith(`${parent}/`) || path.startsWith(`${parent}[`);
|
|
442
434
|
}
|
|
435
|
+
/**
|
|
436
|
+
* Live entries by path, with the paths also kept sorted. Everything beneath a path
|
|
437
|
+
* starts with `<path>/` or `<path>[`, so in sorted order each is one contiguous run,
|
|
438
|
+
* found by binary search. Scanning every live entry instead made each record cost the
|
|
439
|
+
* whole live set — in deep recursion, where the live set and the paths both grow with
|
|
440
|
+
* depth, that was cubic.
|
|
441
|
+
*/
|
|
442
|
+
class LiveEntries {
|
|
443
|
+
seqs = new Map();
|
|
444
|
+
sorted = [];
|
|
445
|
+
get size() {
|
|
446
|
+
return this.seqs.size;
|
|
447
|
+
}
|
|
448
|
+
get(path) {
|
|
449
|
+
return this.seqs.get(path);
|
|
450
|
+
}
|
|
451
|
+
has(path) {
|
|
452
|
+
return this.seqs.has(path);
|
|
453
|
+
}
|
|
454
|
+
set(path, seq) {
|
|
455
|
+
if (!this.seqs.has(path)) {
|
|
456
|
+
this.sorted.splice(lowerBound(this.sorted, path), 0, path);
|
|
457
|
+
}
|
|
458
|
+
this.seqs.set(path, seq);
|
|
459
|
+
}
|
|
460
|
+
delete(path) {
|
|
461
|
+
if (!this.seqs.delete(path)) {
|
|
462
|
+
return;
|
|
463
|
+
}
|
|
464
|
+
this.sorted.splice(lowerBound(this.sorted, path), 1);
|
|
465
|
+
}
|
|
466
|
+
clear() {
|
|
467
|
+
this.seqs.clear();
|
|
468
|
+
this.sorted.length = 0;
|
|
469
|
+
}
|
|
470
|
+
paths() {
|
|
471
|
+
return [
|
|
472
|
+
...this.sorted,
|
|
473
|
+
];
|
|
474
|
+
}
|
|
475
|
+
/** Every live path beneath `parent`. */
|
|
476
|
+
beneath(parent) {
|
|
477
|
+
const found = [];
|
|
478
|
+
for (const prefix of [
|
|
479
|
+
`${parent}/`,
|
|
480
|
+
`${parent}[`,
|
|
481
|
+
]) {
|
|
482
|
+
for (let i = lowerBound(this.sorted, prefix); i < this.sorted.length; i++) {
|
|
483
|
+
const path = this.sorted[i];
|
|
484
|
+
if (!path?.startsWith(prefix)) {
|
|
485
|
+
break;
|
|
486
|
+
}
|
|
487
|
+
found.push(path);
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
return found;
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
/** Index of the first element of sorted `list` not less than `value`. */
|
|
494
|
+
function lowerBound(list, value) {
|
|
495
|
+
let low = 0;
|
|
496
|
+
let high = list.length;
|
|
497
|
+
while (low < high) {
|
|
498
|
+
const mid = (low + high) >>> 1;
|
|
499
|
+
if ((list[mid] ?? '') < value) {
|
|
500
|
+
low = mid + 1;
|
|
501
|
+
}
|
|
502
|
+
else {
|
|
503
|
+
high = mid;
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
return low;
|
|
507
|
+
}
|
|
443
508
|
//#endregion
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { DeliveryMode, ExecuteInput, ExecuteOptions } from '@noetic-tools/types';
|
|
1
|
+
import type { DeliveryMode, ExecuteInput, ExecuteOptions, Item } from '@noetic-tools/types';
|
|
2
2
|
/** @internal A message accepted by `AgentHarness.execute()` and held until the
|
|
3
3
|
* session runner processes it. The effective `DeliveryMode` is resolved at
|
|
4
4
|
* enqueue time so the runner never has to look at harness defaults again. */
|
|
@@ -9,8 +9,20 @@ export interface QueuedMessage {
|
|
|
9
9
|
readonly options: ExecuteOptions;
|
|
10
10
|
readonly enqueuedAt: number;
|
|
11
11
|
}
|
|
12
|
+
/** @internal Context fields that let a model call find its session's steers. */
|
|
13
|
+
export interface SessionCtxExtension {
|
|
14
|
+
_sessionQueue?: MessageQueue;
|
|
15
|
+
/** Set on a spawned child that takes its parent's steers. */
|
|
16
|
+
_forwardSteers?: boolean;
|
|
17
|
+
}
|
|
12
18
|
/** @internal Subscription callback fired after any mutation. */
|
|
13
19
|
export type QueueChangeListener = (size: number) => void;
|
|
20
|
+
/** @internal Convert `execute()` input to log items. An empty string is no input. */
|
|
21
|
+
export declare function inputToItems(input: ExecuteInput): Item[];
|
|
22
|
+
/** @internal The abort reason a `deliveryMode: 'interrupt'` message cancels its turn with. */
|
|
23
|
+
export declare const INTERRUPT_REASON = "interrupt";
|
|
24
|
+
/** @internal Whether a queued message is a steer: one meant for the running turn. */
|
|
25
|
+
export declare function isSteer(message: QueuedMessage): boolean;
|
|
14
26
|
/** @internal Per-thread FIFO of inbox messages with prepend + subscription.
|
|
15
27
|
*
|
|
16
28
|
* `prepend` is used when an in-flight turn is aborted — the aborting message
|
|
@@ -22,6 +34,8 @@ export declare class MessageQueue {
|
|
|
22
34
|
enqueue(message: QueuedMessage): void;
|
|
23
35
|
prepend(message: QueuedMessage): void;
|
|
24
36
|
drainAll(): QueuedMessage[];
|
|
37
|
+
/** Remove and return the messages `match` accepts, keeping the rest in order. */
|
|
38
|
+
take(match: (message: QueuedMessage) => boolean): QueuedMessage[];
|
|
25
39
|
peekAll(): ReadonlyArray<QueuedMessage>;
|
|
26
40
|
get size(): number;
|
|
27
41
|
subscribe(listener: QueueChangeListener): () => void;
|
|
@@ -1,4 +1,41 @@
|
|
|
1
1
|
//#endregion
|
|
2
|
+
//#region Input conversion
|
|
3
|
+
/** @internal Convert `execute()` input to log items. An empty string is no input. */
|
|
4
|
+
export function inputToItems(input) {
|
|
5
|
+
if (typeof input === 'string') {
|
|
6
|
+
if (input.length === 0) {
|
|
7
|
+
return [];
|
|
8
|
+
}
|
|
9
|
+
const item = {
|
|
10
|
+
id: `user-${crypto.randomUUID()}`,
|
|
11
|
+
type: 'message',
|
|
12
|
+
role: 'user',
|
|
13
|
+
status: 'completed',
|
|
14
|
+
content: [
|
|
15
|
+
{
|
|
16
|
+
type: 'input_text',
|
|
17
|
+
text: input,
|
|
18
|
+
},
|
|
19
|
+
],
|
|
20
|
+
};
|
|
21
|
+
return [
|
|
22
|
+
item,
|
|
23
|
+
];
|
|
24
|
+
}
|
|
25
|
+
if (Array.isArray(input)) {
|
|
26
|
+
return input;
|
|
27
|
+
}
|
|
28
|
+
return [
|
|
29
|
+
input,
|
|
30
|
+
];
|
|
31
|
+
}
|
|
32
|
+
/** @internal The abort reason a `deliveryMode: 'interrupt'` message cancels its turn with. */
|
|
33
|
+
export const INTERRUPT_REASON = 'interrupt';
|
|
34
|
+
/** @internal Whether a queued message is a steer: one meant for the running turn. */
|
|
35
|
+
export function isSteer(message) {
|
|
36
|
+
return message.deliveryMode === 'between-rounds' || message.deliveryMode === 'next-item';
|
|
37
|
+
}
|
|
38
|
+
//#endregion
|
|
2
39
|
//#region MessageQueue
|
|
3
40
|
/** @internal Per-thread FIFO of inbox messages with prepend + subscription.
|
|
4
41
|
*
|
|
@@ -24,6 +61,23 @@ export class MessageQueue {
|
|
|
24
61
|
this.notify();
|
|
25
62
|
return drained;
|
|
26
63
|
}
|
|
64
|
+
/** Remove and return the messages `match` accepts, keeping the rest in order. */
|
|
65
|
+
take(match) {
|
|
66
|
+
const taken = [];
|
|
67
|
+
for (let i = 0; i < this.items.length;) {
|
|
68
|
+
const message = this.items[i];
|
|
69
|
+
if (message && match(message)) {
|
|
70
|
+
taken.push(message);
|
|
71
|
+
this.items.splice(i, 1);
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
i++;
|
|
75
|
+
}
|
|
76
|
+
if (taken.length > 0) {
|
|
77
|
+
this.notify();
|
|
78
|
+
}
|
|
79
|
+
return taken;
|
|
80
|
+
}
|
|
27
81
|
peekAll() {
|
|
28
82
|
return this.items;
|
|
29
83
|
}
|
|
@@ -78,6 +78,9 @@ export declare class SessionRunner {
|
|
|
78
78
|
private readonly responseWaiters;
|
|
79
79
|
constructor(opts: SessionRunnerOpts);
|
|
80
80
|
getStatus(): HarnessStatus;
|
|
81
|
+
/** Queue a message. A steer that arrives mid-turn is announced first, so
|
|
82
|
+
* `steer_accepted` always precedes the `inbox_injected` that applies it. */
|
|
83
|
+
enqueue(message: QueuedMessage): void;
|
|
81
84
|
/** Cumulative token usage and cost across all turns this session has run. */
|
|
82
85
|
getUsage(): SessionUsage;
|
|
83
86
|
/** Replace the totals with ones a durable host read back from storage. */
|
|
@@ -1,37 +1,9 @@
|
|
|
1
1
|
import { isNoeticError } from '@noetic-tools/types';
|
|
2
2
|
import { emitFrameworkEvent } from './broadcaster-utils.js';
|
|
3
3
|
import { EventBroadcaster } from './event-broadcaster.js';
|
|
4
|
-
import { MessageQueue } from './message-queue.js';
|
|
4
|
+
import { inputToItems, isSteer, MessageQueue } from './message-queue.js';
|
|
5
5
|
//#endregion
|
|
6
6
|
//#region Helpers
|
|
7
|
-
function inputToItems(input) {
|
|
8
|
-
if (typeof input === 'string') {
|
|
9
|
-
if (input.length === 0) {
|
|
10
|
-
return [];
|
|
11
|
-
}
|
|
12
|
-
const item = {
|
|
13
|
-
id: `user-${crypto.randomUUID()}`,
|
|
14
|
-
type: 'message',
|
|
15
|
-
role: 'user',
|
|
16
|
-
status: 'completed',
|
|
17
|
-
content: [
|
|
18
|
-
{
|
|
19
|
-
type: 'input_text',
|
|
20
|
-
text: input,
|
|
21
|
-
},
|
|
22
|
-
],
|
|
23
|
-
};
|
|
24
|
-
return [
|
|
25
|
-
item,
|
|
26
|
-
];
|
|
27
|
-
}
|
|
28
|
-
if (Array.isArray(input)) {
|
|
29
|
-
return input;
|
|
30
|
-
}
|
|
31
|
-
return [
|
|
32
|
-
input,
|
|
33
|
-
];
|
|
34
|
-
}
|
|
35
7
|
function mergeInputsToItems(messages) {
|
|
36
8
|
const items = [];
|
|
37
9
|
for (const msg of messages) {
|
|
@@ -95,6 +67,23 @@ export class SessionRunner {
|
|
|
95
67
|
getStatus() {
|
|
96
68
|
return this.status;
|
|
97
69
|
}
|
|
70
|
+
/** Queue a message. A steer that arrives mid-turn is announced first, so
|
|
71
|
+
* `steer_accepted` always precedes the `inbox_injected` that applies it. */
|
|
72
|
+
enqueue(message) {
|
|
73
|
+
if (this.status.kind === 'generating' && isSteer(message)) {
|
|
74
|
+
emitFrameworkEvent({
|
|
75
|
+
broadcaster: this.broadcaster,
|
|
76
|
+
agentName: this.agentName,
|
|
77
|
+
eventType: 'steer_accepted',
|
|
78
|
+
data: {
|
|
79
|
+
messageId: message.id,
|
|
80
|
+
turnId: this.status.turnId,
|
|
81
|
+
deliveryMode: message.deliveryMode,
|
|
82
|
+
},
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
this.queue.enqueue(message);
|
|
86
|
+
}
|
|
98
87
|
/** Cumulative token usage and cost across all turns this session has run. */
|
|
99
88
|
getUsage() {
|
|
100
89
|
return {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@noetic-tools/core",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.15.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Step primitives, interpreter, runtime, and context layer contract for Noetic agents.",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -68,8 +68,8 @@
|
|
|
68
68
|
}
|
|
69
69
|
},
|
|
70
70
|
"dependencies": {
|
|
71
|
-
"@noetic-tools/context": "^1.
|
|
72
|
-
"@noetic-tools/types": "^3.
|
|
71
|
+
"@noetic-tools/context": "^1.10.0",
|
|
72
|
+
"@noetic-tools/types": "^3.13.0",
|
|
73
73
|
"@openrouter/agent": "^0.11.0",
|
|
74
74
|
"@openrouter/sdk": "1.4.25",
|
|
75
75
|
"jsonata": "2.2.2",
|