@noetic-tools/core 5.14.0 → 5.15.0
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 +45 -2
- package/dist/interpreter/execute-control.js +4 -3
- package/dist/observability/span-impl.d.ts +9 -0
- package/dist/observability/span-impl.js +13 -0
- package/dist/runtime/context-impl.d.ts +50 -10
- package/dist/runtime/context-impl.js +115 -27
- 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,
|
|
@@ -735,11 +775,14 @@ export async function executeSpawn(step, input, ctx, executeStep, opts) {
|
|
|
735
775
|
id: childId,
|
|
736
776
|
// The spawn's own path prefixes the child's, so a resumed run lines its steps up
|
|
737
777
|
// with the ones recorded inside the same spawn rather than at the root.
|
|
738
|
-
|
|
778
|
+
path: baseCtx instanceof ContextImpl ? baseCtx.branchPath('') : undefined,
|
|
739
779
|
ledger: baseCtx instanceof ContextImpl ? baseCtx.ledger : undefined,
|
|
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,
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*/
|
|
4
4
|
import { createMessage, frameworkCast, isNoeticConfigError, isNoeticError, NoeticErrorImpl, } from '@noetic-tools/types';
|
|
5
5
|
import { NoeticAttr, NoeticEvent } from '../observability/genai-attributes.js';
|
|
6
|
-
import { ContextImpl } from '../runtime/context-impl.js';
|
|
6
|
+
import { ContextImpl, PathNode } from '../runtime/context-impl.js';
|
|
7
7
|
import { snapshotCwdState } from '../runtime/cwd-helpers.js';
|
|
8
8
|
import { toUntil } from '../until/decider.js';
|
|
9
9
|
import { contextToExecCtx, returnLayers, spawnLayers } from './action-deps.js';
|
|
@@ -27,7 +27,6 @@ function createChildContexts(ctx, paths, stepId) {
|
|
|
27
27
|
const threadId = isContextImpl(ctx) ? ctx.threadId : crypto.randomUUID();
|
|
28
28
|
const resourceId = isContextImpl(ctx) ? ctx.resourceId : undefined;
|
|
29
29
|
const channelStore = getContextChannelStore(ctx);
|
|
30
|
-
const parentPath = isContextImpl(ctx) ? ctx.currentPath() : '';
|
|
31
30
|
const parentLedger = isContextImpl(ctx) ? ctx.ledger : undefined;
|
|
32
31
|
return paths.map((path, index) => new ContextImpl({
|
|
33
32
|
harness: ctx.harness,
|
|
@@ -40,7 +39,9 @@ function createChildContexts(ctx, paths, stepId) {
|
|
|
40
39
|
/* Ledger paths must stay unique across sibling paths: a child ContextImpl
|
|
41
40
|
* starts with an empty frontier, so without a prefix every path would
|
|
42
41
|
* restart at the root and collide with its siblings. */
|
|
43
|
-
|
|
42
|
+
path: isContextImpl(ctx)
|
|
43
|
+
? ctx.branchPath(`[${index}]`)
|
|
44
|
+
: new PathNode(undefined, `[${index}]`),
|
|
44
45
|
ledger: parentLedger,
|
|
45
46
|
// A path's steps checkpoint the root: its usage, state, and layer folds roll
|
|
46
47
|
// into what a resumed root restores.
|
|
@@ -1,4 +1,11 @@
|
|
|
1
1
|
import type { Span } from '@noetic-tools/types';
|
|
2
|
+
/**
|
|
3
|
+
* Most events one span keeps. An uncapped loop adds a `loop.verdict` event to its
|
|
4
|
+
* enclosing span every iteration, and a long-lived span would hold them all, so a
|
|
5
|
+
* span keeps the latest events and counts the rest. 128 matches OpenTelemetry's
|
|
6
|
+
* default event count limit.
|
|
7
|
+
*/
|
|
8
|
+
export declare const MAX_SPAN_EVENTS = 128;
|
|
2
9
|
export declare class SpanImpl implements Span {
|
|
3
10
|
readonly traceId: string;
|
|
4
11
|
readonly spanId: string;
|
|
@@ -12,6 +19,8 @@ export declare class SpanImpl implements Span {
|
|
|
12
19
|
timestamp: number;
|
|
13
20
|
attributes?: Record<string, string | number | boolean>;
|
|
14
21
|
}>;
|
|
22
|
+
/** Events dropped, oldest first, once the span held `MAX_SPAN_EVENTS`. */
|
|
23
|
+
droppedEvents: number;
|
|
15
24
|
constructor(name: string, parent: Span | null, traceId?: string);
|
|
16
25
|
setAttribute(key: string, value: string | number | boolean): void;
|
|
17
26
|
addEvent(name: string, attributes?: Record<string, string | number | boolean>): void;
|
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Most events one span keeps. An uncapped loop adds a `loop.verdict` event to its
|
|
3
|
+
* enclosing span every iteration, and a long-lived span would hold them all, so a
|
|
4
|
+
* span keeps the latest events and counts the rest. 128 matches OpenTelemetry's
|
|
5
|
+
* default event count limit.
|
|
6
|
+
*/
|
|
7
|
+
export const MAX_SPAN_EVENTS = 128;
|
|
1
8
|
export class SpanImpl {
|
|
2
9
|
traceId;
|
|
3
10
|
spanId;
|
|
@@ -7,6 +14,8 @@ export class SpanImpl {
|
|
|
7
14
|
endTime;
|
|
8
15
|
attributes = new Map();
|
|
9
16
|
events = [];
|
|
17
|
+
/** Events dropped, oldest first, once the span held `MAX_SPAN_EVENTS`. */
|
|
18
|
+
droppedEvents = 0;
|
|
10
19
|
constructor(name, parent, traceId) {
|
|
11
20
|
this.name = name;
|
|
12
21
|
this.traceId = traceId ?? parent?.traceId ?? crypto.randomUUID();
|
|
@@ -23,6 +32,10 @@ export class SpanImpl {
|
|
|
23
32
|
timestamp: Date.now(),
|
|
24
33
|
attributes,
|
|
25
34
|
});
|
|
35
|
+
if (this.events.length > MAX_SPAN_EVENTS) {
|
|
36
|
+
this.events.shift();
|
|
37
|
+
this.droppedEvents += 1;
|
|
38
|
+
}
|
|
26
39
|
}
|
|
27
40
|
end() {
|
|
28
41
|
this.endTime = Date.now();
|
|
@@ -80,19 +80,28 @@ export declare class ContextImpl implements Context<ContextData> {
|
|
|
80
80
|
*/
|
|
81
81
|
private readonly _inFlight;
|
|
82
82
|
/**
|
|
83
|
-
* Execution path of the step whose lane this is. On the context itself it is
|
|
84
|
-
* prefix its steps hang under: empty on a root context, the parent's path plus
|
|
85
|
-
* discriminator on an inParallel/spawn child, so a path stays unique across the
|
|
86
|
-
* step tree. A lane (see `enterStep`) reads its own step's
|
|
83
|
+
* Execution path node of the step whose lane this is. On the context itself it is
|
|
84
|
+
* the prefix its steps hang under: empty on a root context, the parent's path plus
|
|
85
|
+
* a discriminator on an inParallel/spawn child, so a path stays unique across the
|
|
86
|
+
* whole step tree. A lane (see `enterStep`) reads its own step's node here instead.
|
|
87
|
+
* Each node counts the dispatches beneath it, so a loop body re-entered on each
|
|
88
|
+
* iteration gets `#0`, `#1`, … instead of colliding on one path.
|
|
87
89
|
* @internal
|
|
88
90
|
*/
|
|
89
|
-
private readonly
|
|
91
|
+
private readonly _node;
|
|
90
92
|
/**
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
+
* Nodes on this context holding dispatch counts, so a run baseline can list every
|
|
94
|
+
* live count. Shared by every lane.
|
|
93
95
|
* @internal
|
|
94
96
|
*/
|
|
95
|
-
private readonly
|
|
97
|
+
private readonly _counted;
|
|
98
|
+
/**
|
|
99
|
+
* Dispatch counts restored from a checkpoint, keyed `<parent path>/<step id>`, not
|
|
100
|
+
* yet taken by a dispatch. A dispatch moves its count into its parent node; until
|
|
101
|
+
* this empties, each dispatch builds its parent's path to look its count up.
|
|
102
|
+
* @internal
|
|
103
|
+
*/
|
|
104
|
+
private readonly _restored;
|
|
96
105
|
/**
|
|
97
106
|
* Completion ledger for the execution this context belongs to. Shared by reference
|
|
98
107
|
* with inParallel/spawn children — one execution, one ledger.
|
|
@@ -126,8 +135,8 @@ export declare class ContextImpl implements Context<ContextData> {
|
|
|
126
135
|
span?: Span;
|
|
127
136
|
channelStore?: ChannelStore;
|
|
128
137
|
checkpointFn?: (ctx: Context) => Promise<void>;
|
|
129
|
-
/** Path this context's steps hang under (inParallel/spawn children inherit one). */
|
|
130
|
-
|
|
138
|
+
/** Path node this context's steps hang under (inParallel/spawn children inherit one; see `branchPath`). */
|
|
139
|
+
path?: PathNode;
|
|
131
140
|
/** The execution's completion ledger, shared with children by reference. */
|
|
132
141
|
ledger?: StepLedger;
|
|
133
142
|
layers?: ContextLayer[];
|
|
@@ -222,6 +231,12 @@ export declare class ContextImpl implements Context<ContextData> {
|
|
|
222
231
|
* what lets a resumed run line recorded outputs up with the steps that produced them.
|
|
223
232
|
*/
|
|
224
233
|
currentPath(): string;
|
|
234
|
+
/**
|
|
235
|
+
* @internal
|
|
236
|
+
* A path node under this lane's path, for an inParallel/spawn child context to hang
|
|
237
|
+
* its steps under. The child's path string is built only if something reads it.
|
|
238
|
+
*/
|
|
239
|
+
branchPath(segment: string): PathNode;
|
|
225
240
|
/**
|
|
226
241
|
* @internal
|
|
227
242
|
* Items appended to the item log through this lane, including by steps it
|
|
@@ -290,6 +305,13 @@ export declare class ContextImpl implements Context<ContextData> {
|
|
|
290
305
|
* its dispatches where the recorded run did rather than from zero.
|
|
291
306
|
*/
|
|
292
307
|
restoreOccurrences(occurrences: Record<string, number>): void;
|
|
308
|
+
/**
|
|
309
|
+
* Every live dispatch count, keyed `<parent path>/<step id>` — the form checkpoints
|
|
310
|
+
* persist and `restoreOccurrences` reads back.
|
|
311
|
+
*/
|
|
312
|
+
private liveCounts;
|
|
313
|
+
/** The restored count for `stepId` under `parent`, consumed on first use; 0 when none. */
|
|
314
|
+
private takeRestored;
|
|
293
315
|
/**
|
|
294
316
|
* @internal
|
|
295
317
|
* Return a defensive copy of the current frontier. Consumed by the
|
|
@@ -302,6 +324,24 @@ export interface RunBaseline {
|
|
|
302
324
|
items: Item[];
|
|
303
325
|
occurrences: Record<string, number>;
|
|
304
326
|
}
|
|
327
|
+
/**
|
|
328
|
+
* @internal
|
|
329
|
+
* One point in the execution path tree: a context's root, or one dispatch. It
|
|
330
|
+
* holds the dispatch counts for steps directly beneath it, keyed by step id, and
|
|
331
|
+
* builds its path string only when something reads it. Deep recursion makes
|
|
332
|
+
* paths long — each level adds a segment and its ids grow — so building every
|
|
333
|
+
* dispatch's path eagerly cost time cubic in depth even when no ledger read it.
|
|
334
|
+
*/
|
|
335
|
+
export declare class PathNode {
|
|
336
|
+
private readonly parent;
|
|
337
|
+
private readonly segment;
|
|
338
|
+
/** Dispatches beneath this path, by step id; the count is the next ordinal. */
|
|
339
|
+
readonly counts: Map<string, number>;
|
|
340
|
+
private text?;
|
|
341
|
+
constructor(parent: PathNode | undefined, segment: string);
|
|
342
|
+
/** The full path: the parent's path plus this node's segment. Built once, on first read. */
|
|
343
|
+
get path(): string;
|
|
344
|
+
}
|
|
305
345
|
/**
|
|
306
346
|
* @internal
|
|
307
347
|
* The context a lane views, or `ctx` itself when it is not a lane. For per-execution
|