@noetic-tools/core 5.22.0-next.5 → 5.22.0-next.6
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/harness/agent-harness.d.ts +15 -1
- package/dist/harness/agent-harness.js +117 -35
- package/dist/harness/deps/types.d.ts +1 -1
- package/dist/harness/model-rounds.d.ts +3 -0
- package/dist/harness/model-rounds.js +22 -0
- package/dist/harness/steer-inbox.d.ts +8 -2
- package/dist/harness/steer-inbox.js +7 -6
- package/dist/harness/thread-record.d.ts +6 -2
- package/dist/harness/thread-record.js +18 -8
- package/dist/index.d.ts +1 -1
- package/dist/runtime/durable/step-ledger.d.ts +2 -0
- package/dist/runtime/durable/step-ledger.js +4 -0
- package/package.json +2 -2
|
@@ -7,7 +7,7 @@ import type { LayerStateStore, RecallCache } from './deps/context.js';
|
|
|
7
7
|
import type { DetachedSpawnOverrides } from './deps/interpreter.js';
|
|
8
8
|
import type { CheckpointStore, EventBroadcaster, RestoreCheckpointOptions, StepLedgerRetention, StepLedgerStore } from './deps/runtime.js';
|
|
9
9
|
import { ChannelStore, Drains } from './deps/runtime.js';
|
|
10
|
-
import type { AcpLiveSession, AcpSessionInfo, AgentConfig, AgentHarnessContract, CallModelRequest, CancelOptions, Channel, ChannelHandle, Context, ContextCacheStore, ContextData, ContextLayer, CustomItem, CustomItemOf, CustomItemStreamEvent, CustomStreamingItem, CwdState, DecideRequest, DecideResponse, DecisionRecord, DeclaredItems, DetachedHandle, DrainReport, ExecuteOptions, ExecutionScope, ExecutionStatus, ExternalChannel, FsAdapter, HarnessResponse, HarnessStatus, Item, LLMResponse, OnSteerParams, PendingSpend, RecallLayerOutput, RecallOptions, ReconcileSpendOptions, SessionScope, SessionUsage, ShellAdapter, Span, SpendSettlement, SteeringDecision, SteerVerdict, Step, StepLike, StepsInput, StepsOutput, StorageAdapter, StreamEvent, StreamingItem, SubprocessAdapter, TraceExporter, ValidStepChain, WriteOffSpendOptions } from './deps/types.js';
|
|
10
|
+
import type { AcpLiveSession, AcpSessionInfo, AgentConfig, AgentHarnessContract, CallModelRequest, CancelOptions, Channel, ChannelHandle, Context, ContextCacheStore, ContextData, ContextLayer, CustomItem, CustomItemOf, CustomItemStreamEvent, CustomStreamingItem, CwdState, DecideRequest, DecideResponse, DecisionRecord, DeclaredItems, DetachedHandle, DrainReport, ExecuteOptions, ExecutionScope, ExecutionStatus, ExternalChannel, FsAdapter, HarnessResponse, HarnessStatus, Item, LLMResponse, OnSteerParams, PendingSpend, RecallLayerOutput, RecallOptions, ReconcileSpendOptions, SessionScope, SessionUsage, ShellAdapter, Span, SpendSettlement, SteeringDecision, SteerVerdict, Step, StepLike, StepsInput, StepsOutput, StorageAdapter, StoredThread, StreamEvent, StreamingItem, SubprocessAdapter, TraceExporter, ValidStepChain, WriteOffSpendOptions } from './deps/types.js';
|
|
11
11
|
import type { AgentHarnessOpts, WakeTimeListener } from './harness-options.js';
|
|
12
12
|
import { Spend } from './spend.js';
|
|
13
13
|
/**
|
|
@@ -371,6 +371,20 @@ export declare class AgentHarness<TParams extends Record<string, unknown> = Reco
|
|
|
371
371
|
* refused and the turn was cut instead: a wake replays it to here.
|
|
372
372
|
*/
|
|
373
373
|
private keepOrCut;
|
|
374
|
+
/** Start the stored turn before putting kept messages back or reporting drops. */
|
|
375
|
+
private beginStoredTurn;
|
|
376
|
+
/**
|
|
377
|
+
* Split stored messages between the queue and the steers the start write clears.
|
|
378
|
+
*/
|
|
379
|
+
private splitKeptMessages;
|
|
380
|
+
/**
|
|
381
|
+
* What the thread's stored record holds (spec 23a § Sessions): whether it has
|
|
382
|
+
* history and the turn it has in flight, cut off or parked. Undefined when the
|
|
383
|
+
* thread has no record, or the harness keeps none. A host that keeps its own
|
|
384
|
+
* copy of the conversation seeds history only when none is stored.
|
|
385
|
+
* @public
|
|
386
|
+
*/
|
|
387
|
+
getStoredThread(threadId: string): Promise<StoredThread | undefined>;
|
|
374
388
|
/** The options a stored turn is woken with when the wake names none: `turnOptions`, or none. */
|
|
375
389
|
private storedTurnOptions;
|
|
376
390
|
/**
|
|
@@ -4,6 +4,7 @@ import { runEvery } from '../connections/settle.js';
|
|
|
4
4
|
import { pendingTasksOf, resumableWaitsOf } from '../interpreter/connection-steps.js';
|
|
5
5
|
import { turnEndingIn } from '../interpreter/turn-ending.js';
|
|
6
6
|
import { AcpSessionStore } from '../runtime/acp-session-store.js';
|
|
7
|
+
import { emitFrameworkEvent } from '../runtime/broadcaster-utils.js';
|
|
7
8
|
import { stubOf } from '../runtime/durable/step-ledger.js';
|
|
8
9
|
import { heldCallsOf, toHeldCall } from '../runtime/held-calls.js';
|
|
9
10
|
import { OutputStore } from '../runtime/output-store.js';
|
|
@@ -18,11 +19,13 @@ import { forgetExecution, forgetThread, listStoredExecutions, listStoredThreads
|
|
|
18
19
|
import { resolveCheckpointStore, resolveEnvironmentAdapters } from './harness-options.js';
|
|
19
20
|
import { createHarnessSpan, recordChannelWarning, resolveTraceExporter } from './harness-tracing.js';
|
|
20
21
|
import { LlmGateway } from './llm-client.js';
|
|
22
|
+
import { savedItemIds } from './model-rounds.js';
|
|
21
23
|
import { buildItemSchemaRegistry } from './model-schema.js';
|
|
22
24
|
import { DEFAULT_TASK_POLL_MS, DEFAULT_WAKE_DEBOUNCE_MS, ParkWatchers } from './park-watchers.js';
|
|
23
25
|
import { readDecision } from './read-decision.js';
|
|
24
26
|
import { recordedToolCalls } from './recorded-tool-calls.js';
|
|
25
27
|
import { Spend } from './spend.js';
|
|
28
|
+
import { steerInConversation, steerItems } from './steer-inbox.js';
|
|
26
29
|
import { ThreadRecord } from './thread-record.js';
|
|
27
30
|
import { TurnCuts } from './turn-cuts.js';
|
|
28
31
|
/** Keep `message` in storage for after the thread's parked turn, with its options. */
|
|
@@ -30,6 +33,29 @@ function keepForLater(session, record, message) {
|
|
|
30
33
|
session.keptOptions.set(message.id, message.options);
|
|
31
34
|
return record.keepQueued(message);
|
|
32
35
|
}
|
|
36
|
+
/** A stored turn as a host sees it (`turnOptions`, `getStoredThread`). */
|
|
37
|
+
function storedTurnOf(threadId, turn, cut) {
|
|
38
|
+
return {
|
|
39
|
+
threadId,
|
|
40
|
+
executionId: turn.executionId,
|
|
41
|
+
messageIds: turn.messageIds,
|
|
42
|
+
...(turn.version === undefined
|
|
43
|
+
? {}
|
|
44
|
+
: {
|
|
45
|
+
version: turn.version,
|
|
46
|
+
}),
|
|
47
|
+
...(turn.parked
|
|
48
|
+
? {
|
|
49
|
+
parked: turn.parked,
|
|
50
|
+
}
|
|
51
|
+
: {}),
|
|
52
|
+
...(cut
|
|
53
|
+
? {
|
|
54
|
+
cut,
|
|
55
|
+
}
|
|
56
|
+
: {}),
|
|
57
|
+
};
|
|
58
|
+
}
|
|
33
59
|
//#endregion
|
|
34
60
|
const DEFAULT_THREAD_ID = '__default__';
|
|
35
61
|
/**
|
|
@@ -1047,26 +1073,14 @@ export class AgentHarness {
|
|
|
1047
1073
|
await this.start();
|
|
1048
1074
|
const connectionTools = await this.#connections.openTurn(ctx, perTurnOptions.connections, graph);
|
|
1049
1075
|
this.setTurnTools(ctx, perTurnOptions.tools ?? [], connectionTools);
|
|
1050
|
-
// Steers the last harness accepted but never applied go back in the queue;
|
|
1051
|
-
// ones already in the conversation are skipped when offered.
|
|
1052
|
-
for (const steer of (await record?.steers()) ?? []) {
|
|
1053
|
-
const queued = session.runner.queue.peekAll().some((m) => m.id === steer.id);
|
|
1054
|
-
if (!queued && !messageIds.includes(steer.id)) {
|
|
1055
|
-
// With the options it was sent with, when this process is the one it was sent to.
|
|
1056
|
-
const options = session.keptOptions.get(steer.id) ?? steer.options;
|
|
1057
|
-
session.keptOptions.delete(steer.id);
|
|
1058
|
-
session.runner.queue.enqueue({
|
|
1059
|
-
...steer,
|
|
1060
|
-
options,
|
|
1061
|
-
});
|
|
1062
|
-
}
|
|
1063
|
-
}
|
|
1064
1076
|
// A stuck turn the host chose to go on with may discard what no longer matches.
|
|
1065
1077
|
const accept = (await record?.turn())?.accept;
|
|
1066
1078
|
if (accept && ctx instanceof ContextImpl) {
|
|
1067
1079
|
ctx.ledger?.acceptVersionLoss(typeof accept === 'string' ? accept : undefined);
|
|
1068
1080
|
}
|
|
1069
|
-
|
|
1081
|
+
if (record) {
|
|
1082
|
+
await this.beginStoredTurn(session, record, ctx, messageIds, perTurnOptions.keptSteers);
|
|
1083
|
+
}
|
|
1070
1084
|
// A turn on record can be cut off from here on. A host that sleeps between
|
|
1071
1085
|
// requests has to know when to come back and look (see `turnDueAt`).
|
|
1072
1086
|
if (record && this.leaseOptions) {
|
|
@@ -1366,31 +1380,99 @@ export class AgentHarness {
|
|
|
1366
1380
|
throw error;
|
|
1367
1381
|
}
|
|
1368
1382
|
}
|
|
1369
|
-
/**
|
|
1370
|
-
async
|
|
1371
|
-
|
|
1372
|
-
|
|
1383
|
+
/** Start the stored turn before putting kept messages back or reporting drops. */
|
|
1384
|
+
async beginStoredTurn(session, record, ctx, messageIds, keptSteers) {
|
|
1385
|
+
const { removed, kept } = await this.splitKeptMessages(session, record, messageIds, keptSteers);
|
|
1386
|
+
try {
|
|
1387
|
+
await record.begin(ctx, messageIds, removed.map((steer) => steer.id));
|
|
1388
|
+
}
|
|
1389
|
+
catch (error) {
|
|
1390
|
+
await this.#connections.release(ctx.id);
|
|
1391
|
+
throw error;
|
|
1392
|
+
}
|
|
1393
|
+
for (const message of kept) {
|
|
1394
|
+
session.keptOptions.delete(message.id);
|
|
1395
|
+
if (!session.runner.queue.peekAll().some((queued) => queued.id === message.id)) {
|
|
1396
|
+
session.runner.queue.enqueue(message);
|
|
1397
|
+
}
|
|
1398
|
+
}
|
|
1399
|
+
const saved = removed.length > 0 && ctx instanceof ContextImpl ? savedItemIds(ctx) : undefined;
|
|
1400
|
+
// Told only once the record no longer holds them.
|
|
1401
|
+
for (const steer of removed) {
|
|
1402
|
+
if (saved && steerInConversation(steerItems(steer), (id) => saved.has(id))) {
|
|
1403
|
+
continue;
|
|
1404
|
+
}
|
|
1405
|
+
emitFrameworkEvent({
|
|
1406
|
+
broadcaster: session.runner.broadcaster,
|
|
1407
|
+
agentName: this.config.name,
|
|
1408
|
+
eventType: 'steer_dropped',
|
|
1409
|
+
data: {
|
|
1410
|
+
messageId: steer.id,
|
|
1411
|
+
reason: 'kept_steers',
|
|
1412
|
+
},
|
|
1413
|
+
});
|
|
1373
1414
|
}
|
|
1374
|
-
|
|
1415
|
+
}
|
|
1416
|
+
/**
|
|
1417
|
+
* Split stored messages between the queue and the steers the start write clears.
|
|
1418
|
+
*/
|
|
1419
|
+
async splitKeptMessages(session, record, messageIds, keptSteers) {
|
|
1420
|
+
const removed = [];
|
|
1421
|
+
const kept = [];
|
|
1422
|
+
for (const steer of await record.steers()) {
|
|
1423
|
+
const queued = session.runner.queue.peekAll().some((m) => m.id === steer.id);
|
|
1424
|
+
if (queued || messageIds.includes(steer.id)) {
|
|
1425
|
+
continue;
|
|
1426
|
+
}
|
|
1427
|
+
// With the options it was sent with, when this process is the one it was sent to.
|
|
1428
|
+
const options = session.keptOptions.get(steer.id);
|
|
1429
|
+
// A message this process kept for a parked turn still has its sender here.
|
|
1430
|
+
if (keptSteers === 'drop' && options === undefined && isSteer(steer)) {
|
|
1431
|
+
removed.push(steer);
|
|
1432
|
+
continue;
|
|
1433
|
+
}
|
|
1434
|
+
kept.push({
|
|
1435
|
+
...steer,
|
|
1436
|
+
options: options ?? steer.options,
|
|
1437
|
+
});
|
|
1438
|
+
}
|
|
1439
|
+
return {
|
|
1440
|
+
removed,
|
|
1441
|
+
kept,
|
|
1442
|
+
};
|
|
1443
|
+
}
|
|
1444
|
+
/**
|
|
1445
|
+
* What the thread's stored record holds (spec 23a § Sessions): whether it has
|
|
1446
|
+
* history and the turn it has in flight, cut off or parked. Undefined when the
|
|
1447
|
+
* thread has no record, or the harness keeps none. A host that keeps its own
|
|
1448
|
+
* copy of the conversation seeds history only when none is stored.
|
|
1449
|
+
* @public
|
|
1450
|
+
*/
|
|
1451
|
+
async getStoredThread(threadId) {
|
|
1452
|
+
await this.forgetting.get(threadId);
|
|
1453
|
+
// Through the session's own record, when there is one: a seed it queued lands first.
|
|
1454
|
+
const record = await (this.sessions.get(threadId)?.record ?? this.threadRecord(threadId))?.stored();
|
|
1455
|
+
if (!record) {
|
|
1456
|
+
return undefined;
|
|
1457
|
+
}
|
|
1458
|
+
const turn = record.turn;
|
|
1459
|
+
const cut = turn && !turn.parked ? this.cuts?.markOf(threadId, turn) : undefined;
|
|
1460
|
+
return {
|
|
1375
1461
|
threadId,
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
...(turn.version === undefined
|
|
1379
|
-
? {}
|
|
1380
|
-
: {
|
|
1381
|
-
version: turn.version,
|
|
1382
|
-
}),
|
|
1383
|
-
...(turn.parked
|
|
1462
|
+
hasHistory: record.items.length > 0,
|
|
1463
|
+
...(turn
|
|
1384
1464
|
? {
|
|
1385
|
-
|
|
1465
|
+
turn: storedTurnOf(threadId, turn, cut),
|
|
1386
1466
|
}
|
|
1387
1467
|
: {}),
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1468
|
+
};
|
|
1469
|
+
}
|
|
1470
|
+
/** The options a stored turn is woken with when the wake names none: `turnOptions`, or none. */
|
|
1471
|
+
async storedTurnOptions(threadId, turn, cut) {
|
|
1472
|
+
if (!this.turnOptionsFor) {
|
|
1473
|
+
return {};
|
|
1474
|
+
}
|
|
1475
|
+
return await this.turnOptionsFor(storedTurnOf(threadId, turn, cut));
|
|
1394
1476
|
}
|
|
1395
1477
|
/**
|
|
1396
1478
|
* The earliest time the harness has something to do, in epoch ms: a parked turn
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export type { ContextCacheConfig, ContextCacheStore, ContextData, ContextLayer, ExecutionContext, ProjectionPolicy, StorageAdapter, } from '@noetic-tools/context';
|
|
2
|
-
export type { AcpKeepAlive, AcpLiveSession, AcpSessionInfo, AgentConfig, AgentHarnessContract, AgentHooks, CallModelRequest, CancelOptions, Channel, ChannelHandle, Context, CustomItem, CustomItemOf, CustomItemStreamEvent, CustomItemsBrand, CustomStreamingItem, CwdState, DecideRequest, DecideResponse, DecisionRecord, DeclaredItems, DeliveryMode, DetachedHandle, DrainReport, ExecuteInput, ExecuteOptions, ExecutionScope, ExecutionStatus, ExternalChannel, FsAdapter, HarnessResponse, HarnessStatus, HeldCallHandler, HeldSteerPolicy, Item, ItemSchemaConfig, ItemSchemaExtensions, LLMResponse, LlmProviderConfig, OnSteerParams, PendingSpend, RecallLayerOutput, RecallOptions, ReconcileSpendOptions, SessionScope, SessionUsage, ShellAdapter, Span, SpendSettlement, SteeringDecision, SteerVerdict, Step, StepLike, Steps, StepsInput, StepsOutput, StoredTurn, StreamEvent, StreamingItem, SubprocessAdapter, Tool, ToolApprover, TraceExporter, TurnCut, TurnCutReason, UsageCharge, ValidStepChain, WriteOffSpendOptions, } from '@noetic-tools/types';
|
|
2
|
+
export type { AcpKeepAlive, AcpLiveSession, AcpSessionInfo, AgentConfig, AgentHarnessContract, AgentHooks, CallModelRequest, CancelOptions, Channel, ChannelHandle, Context, CustomItem, CustomItemOf, CustomItemStreamEvent, CustomItemsBrand, CustomStreamingItem, CwdState, DecideRequest, DecideResponse, DecisionRecord, DeclaredItems, DeliveryMode, DetachedHandle, DrainReport, ExecuteInput, ExecuteOptions, ExecutionScope, ExecutionStatus, ExternalChannel, FsAdapter, HarnessResponse, HarnessStatus, HeldCallHandler, HeldSteerPolicy, Item, ItemSchemaConfig, ItemSchemaExtensions, KeptSteers, LLMResponse, LlmProviderConfig, OnSteerParams, PendingSpend, RecallLayerOutput, RecallOptions, ReconcileSpendOptions, SessionScope, SessionUsage, ShellAdapter, Span, SpendSettlement, SteeringDecision, SteerVerdict, Step, StepLike, Steps, StepsInput, StepsOutput, StoredThread, StoredTurn, StreamEvent, StreamingItem, SubprocessAdapter, Tool, ToolApprover, TraceExporter, TurnCut, TurnCutReason, UsageCharge, ValidStepChain, WriteOffSpendOptions, } from '@noetic-tools/types';
|
|
3
3
|
export { SteeringAction } from '@noetic-tools/types';
|
|
4
4
|
export type { ZodType } from 'zod';
|
|
@@ -26,6 +26,7 @@
|
|
|
26
26
|
*/
|
|
27
27
|
import type { Item, ItemSchemaRegistry, RoundUsage } from '@noetic-tools/types';
|
|
28
28
|
import type { ToolCallResult } from '../runtime/tool-call.js';
|
|
29
|
+
import { ContextImpl } from './deps/runtime.js';
|
|
29
30
|
/** Ledger kind of a model-loop round's record. */
|
|
30
31
|
export declare const ROUND_KIND = "callModel:round";
|
|
31
32
|
/** Ledger kind of one tool call's record within a round. */
|
|
@@ -72,6 +73,8 @@ export interface ReplayedRound extends RoundRecord {
|
|
|
72
73
|
/** A failed call already charged this round's bill to the context. */
|
|
73
74
|
charged: boolean;
|
|
74
75
|
}
|
|
76
|
+
/** Item ids saved in the context, completed steps and model rounds. */
|
|
77
|
+
export declare function savedItemIds(ctx: ContextImpl): Set<string>;
|
|
75
78
|
/**
|
|
76
79
|
* The round ledger for one model loop. `open` gives none when the call is not running
|
|
77
80
|
* as a step on a context with a ledger.
|
|
@@ -83,6 +83,28 @@ const RoundOutputSchema = z.object({
|
|
|
83
83
|
const chargedRounds = new WeakMap();
|
|
84
84
|
//#endregion
|
|
85
85
|
//#region Public API
|
|
86
|
+
/** Item ids saved in the context, completed steps and model rounds. */
|
|
87
|
+
export function savedItemIds(ctx) {
|
|
88
|
+
const ids = new Set();
|
|
89
|
+
const add = (items) => {
|
|
90
|
+
for (const item of items) {
|
|
91
|
+
if (item && typeof item === 'object' && 'id' in item && typeof item.id === 'string') {
|
|
92
|
+
ids.add(item.id);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
add(ctx.itemLog.items);
|
|
97
|
+
for (const entry of ctx.ledger?.savedEntries() ?? []) {
|
|
98
|
+
add(entry.items ?? []);
|
|
99
|
+
if (entry.kind === ROUND_KIND) {
|
|
100
|
+
const round = RoundOutputSchema.safeParse(entry.output);
|
|
101
|
+
if (round.success) {
|
|
102
|
+
add(round.data.items);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return ids;
|
|
107
|
+
}
|
|
86
108
|
/**
|
|
87
109
|
* The round ledger for one model loop. `open` gives none when the call is not running
|
|
88
110
|
* as a step on a context with a ledger.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { AgentHarnessContract, Context, ContextLayer, FunctionCallItem, Item } from '@noetic-tools/types';
|
|
2
2
|
import { SteerPoint } from '@noetic-tools/types';
|
|
3
|
-
import type { MessageQueue } from '../runtime/message-queue.js';
|
|
3
|
+
import type { MessageQueue, QueuedMessage } from '../runtime/message-queue.js';
|
|
4
4
|
/** @internal Steers taken off the queue, waiting for the next model call. */
|
|
5
5
|
export interface StagedSteers {
|
|
6
6
|
messageIds: string[];
|
|
@@ -46,11 +46,17 @@ export declare class SteerInbox {
|
|
|
46
46
|
offer(point: SteerPoint, pendingCalls?: ReadonlyArray<FunctionCallItem>): Promise<boolean>;
|
|
47
47
|
/** Hand over the staged steers and clear them. */
|
|
48
48
|
takeStaged(): StagedSteers;
|
|
49
|
-
private alreadyApplied;
|
|
50
49
|
private judge;
|
|
51
50
|
/** Steers are user input, so they pass through `onItemAppend` like any other.
|
|
52
51
|
* Re-render requests are ignored: recall does not run again mid-call. */
|
|
53
52
|
private stage;
|
|
54
53
|
private reportPending;
|
|
55
54
|
}
|
|
55
|
+
/** @internal Whether every item of a steer is already in the conversation. */
|
|
56
|
+
export declare function steerInConversation(items: ReadonlyArray<Item>, hasItem: (id: string) => boolean): boolean;
|
|
57
|
+
/**
|
|
58
|
+
* A steer's items. Text gets an id from the message id rather than a random one,
|
|
59
|
+
* so a resumed turn can tell the steer is already in the conversation.
|
|
60
|
+
*/
|
|
61
|
+
export declare function steerItems(message: QueuedMessage): Item[];
|
|
56
62
|
export {};
|
|
@@ -56,7 +56,7 @@ export class SteerInbox {
|
|
|
56
56
|
const items = steerItems(message);
|
|
57
57
|
// A resumed turn re-queues the steers its last run kept; the ones that run
|
|
58
58
|
// already applied come back with the replayed rounds and steps.
|
|
59
|
-
if (this.
|
|
59
|
+
if (steerInConversation(items, this.opts.inConversation)) {
|
|
60
60
|
this.opts.queue.take((queued) => queued === message);
|
|
61
61
|
continue;
|
|
62
62
|
}
|
|
@@ -91,10 +91,6 @@ export class SteerInbox {
|
|
|
91
91
|
};
|
|
92
92
|
return staged;
|
|
93
93
|
}
|
|
94
|
-
alreadyApplied(items) {
|
|
95
|
-
return (items.length > 0 &&
|
|
96
|
-
items.every((item) => 'id' in item && item.id !== undefined && this.opts.inConversation(item.id)));
|
|
97
|
-
}
|
|
98
94
|
async judge(message, items, point, pendingCalls) {
|
|
99
95
|
const action = defaultAction(point, message.deliveryMode, this.opts.harness.config.steerWhileHeld);
|
|
100
96
|
const { harness, layers } = this.opts;
|
|
@@ -136,12 +132,17 @@ export class SteerInbox {
|
|
|
136
132
|
}
|
|
137
133
|
}
|
|
138
134
|
//#endregion
|
|
135
|
+
/** @internal Whether every item of a steer is already in the conversation. */
|
|
136
|
+
export function steerInConversation(items, hasItem) {
|
|
137
|
+
return (items.length > 0 &&
|
|
138
|
+
items.every((item) => 'id' in item && item.id !== undefined && hasItem(item.id)));
|
|
139
|
+
}
|
|
139
140
|
//#region Helpers
|
|
140
141
|
/**
|
|
141
142
|
* A steer's items. Text gets an id from the message id rather than a random one,
|
|
142
143
|
* so a resumed turn can tell the steer is already in the conversation.
|
|
143
144
|
*/
|
|
144
|
-
function steerItems(message) {
|
|
145
|
+
export function steerItems(message) {
|
|
145
146
|
if (typeof message.input !== 'string') {
|
|
146
147
|
return inputToItems(message.input);
|
|
147
148
|
}
|
|
@@ -70,8 +70,9 @@ export declare class ThreadRecord {
|
|
|
70
70
|
/**
|
|
71
71
|
* Record the turn about to run on `ctx`, before any of it runs. Kept steers it
|
|
72
72
|
* delivers as input stop being steers: re-delivering the turn re-sends them.
|
|
73
|
+
* Kept steers named in `removed` are removed in the same write (`keptSteers: 'drop'`).
|
|
73
74
|
*/
|
|
74
|
-
begin(ctx: Context, messageIds: ReadonlyArray<string>): Promise<void>;
|
|
75
|
+
begin(ctx: Context, messageIds: ReadonlyArray<string>, removed?: ReadonlyArray<string>): Promise<void>;
|
|
75
76
|
/**
|
|
76
77
|
* The turn on `ctx` stopped rather than redo a recorded effect under a newer flow
|
|
77
78
|
* version. It stays, marked, until the host settles it.
|
|
@@ -122,6 +123,8 @@ export declare class ThreadRecord {
|
|
|
122
123
|
answerFor(messageId: string): Promise<string | undefined>;
|
|
123
124
|
/** The executions of every turn the thread began, oldest first, the one in flight last. */
|
|
124
125
|
executions(): Promise<string[]>;
|
|
126
|
+
/** The stored record, read after every write issued before it. */
|
|
127
|
+
stored(): Promise<SessionRecord | undefined>;
|
|
125
128
|
/** The turn in flight, cut off or parked, when one is recorded. */
|
|
126
129
|
turn(): Promise<SessionRecord['turn']>;
|
|
127
130
|
/**
|
|
@@ -138,7 +141,8 @@ export declare class ThreadRecord {
|
|
|
138
141
|
* A write the turn itself makes. Under a lease it carries the lease's term and is
|
|
139
142
|
* refused when the record already holds a higher one — another harness took the
|
|
140
143
|
* turn, whatever this one's clock says — and a write that fails throws instead of
|
|
141
|
-
* being logged (spec 23 § One writer per turn, § Fail-stop).
|
|
144
|
+
* being logged (spec 23 § One writer per turn, § Fail-stop). A caller that
|
|
145
|
+
* reports a removal also needs the write to succeed without a lease.
|
|
142
146
|
*/
|
|
143
147
|
private updateHeld;
|
|
144
148
|
/** Read-modify-write the stored record, best-effort. Call only from inside `run`. */
|
|
@@ -146,11 +146,12 @@ export class ThreadRecord {
|
|
|
146
146
|
/**
|
|
147
147
|
* Record the turn about to run on `ctx`, before any of it runs. Kept steers it
|
|
148
148
|
* delivers as input stop being steers: re-delivering the turn re-sends them.
|
|
149
|
+
* Kept steers named in `removed` are removed in the same write (`keptSteers: 'drop'`).
|
|
149
150
|
*/
|
|
150
|
-
begin(ctx, messageIds) {
|
|
151
|
-
|
|
151
|
+
begin(ctx, messageIds, removed = []) {
|
|
152
|
+
const start = (record) => ({
|
|
152
153
|
...record,
|
|
153
|
-
steers: record.steers?.filter((steer) => !messageIds.includes(steer.id)),
|
|
154
|
+
steers: record.steers?.filter((steer) => !messageIds.includes(steer.id) && !removed.includes(steer.id)),
|
|
154
155
|
executions: withExecution(record.executions, ctx.id),
|
|
155
156
|
...this.withLayers(record),
|
|
156
157
|
turn: {
|
|
@@ -179,7 +180,8 @@ export class ThreadRecord {
|
|
|
179
180
|
}
|
|
180
181
|
: {}),
|
|
181
182
|
},
|
|
182
|
-
})
|
|
183
|
+
});
|
|
184
|
+
return this.updateHeld(ctx, start, removed.length > 0);
|
|
183
185
|
}
|
|
184
186
|
/**
|
|
185
187
|
* The turn on `ctx` stopped rather than redo a recorded effect under a newer flow
|
|
@@ -317,6 +319,10 @@ export class ThreadRecord {
|
|
|
317
319
|
executions() {
|
|
318
320
|
return this.run(async () => executionsOf(await this.load()));
|
|
319
321
|
}
|
|
322
|
+
/** The stored record, read after every write issued before it. */
|
|
323
|
+
stored() {
|
|
324
|
+
return this.run(() => this.load());
|
|
325
|
+
}
|
|
320
326
|
/** The turn in flight, cut off or parked, when one is recorded. */
|
|
321
327
|
turn() {
|
|
322
328
|
return this.run(async () => (await this.load())?.turn);
|
|
@@ -361,17 +367,21 @@ export class ThreadRecord {
|
|
|
361
367
|
* A write the turn itself makes. Under a lease it carries the lease's term and is
|
|
362
368
|
* refused when the record already holds a higher one — another harness took the
|
|
363
369
|
* turn, whatever this one's clock says — and a write that fails throws instead of
|
|
364
|
-
* being logged (spec 23 § One writer per turn, § Fail-stop).
|
|
370
|
+
* being logged (spec 23 § One writer per turn, § Fail-stop). A caller that
|
|
371
|
+
* reports a removal also needs the write to succeed without a lease.
|
|
365
372
|
*/
|
|
366
|
-
updateHeld(ctx, change) {
|
|
373
|
+
updateHeld(ctx, change, mustWrite = false) {
|
|
367
374
|
const fence = this.fence;
|
|
368
|
-
if (!fence) {
|
|
375
|
+
if (!fence && !mustWrite) {
|
|
369
376
|
return this.update(change);
|
|
370
377
|
}
|
|
371
378
|
return this.run(async () => {
|
|
372
|
-
fence
|
|
379
|
+
fence?.assert();
|
|
373
380
|
try {
|
|
374
381
|
await this.commit((record) => {
|
|
382
|
+
if (!fence) {
|
|
383
|
+
return change(record);
|
|
384
|
+
}
|
|
375
385
|
if ((record.term ?? 0) > fence.term) {
|
|
376
386
|
throw fence.lose(`the thread record was written at term ${record.term}`);
|
|
377
387
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -181,7 +181,7 @@ export type { LayerTraceSpan, Span, TraceExporter } from '@noetic-tools/types';
|
|
|
181
181
|
/** @public */
|
|
182
182
|
export type { FrameworkStreamEvent, HarnessResponse, SdkStreamEvent, StreamEvent, StreamingItem, TurnCut, TurnCutReason, } from '@noetic-tools/types';
|
|
183
183
|
/** @public */
|
|
184
|
-
export type { AgentConfig, AgentHarnessContract, AgentHooks, CallModelRequest, CancelOptions, ContextCacheConfig, ContextCacheStore, ContextEpoch, DeliveryMode, DrainCall, DrainReport, ExecuteOptions, ExecutionScope, ExecutionStatus, HarnessJudgmentContract, HarnessStatus, HeldCall, HeldCallContext, HeldCallEvent, HeldCallHandler, HeldCallSettled, HeldResolution, HeldSteerPolicy, ItemSchemaConfig, ReanchorReason, RecallLayerOutput, RecordedToolCall, SessionScope, SessionUsage, StoredTurn, StuckTurn, ToolHold, TrailMark, } from '@noetic-tools/types';
|
|
184
|
+
export type { AgentConfig, AgentHarnessContract, AgentHooks, CallModelRequest, CancelOptions, ContextCacheConfig, ContextCacheStore, ContextEpoch, DeliveryMode, DrainCall, DrainReport, ExecuteOptions, ExecutionScope, ExecutionStatus, HarnessJudgmentContract, HarnessStatus, HeldCall, HeldCallContext, HeldCallEvent, HeldCallHandler, HeldCallSettled, HeldResolution, HeldSteerPolicy, ItemSchemaConfig, KeptSteers, ReanchorReason, RecallLayerOutput, RecordedToolCall, SessionScope, SessionUsage, StoredThread, StoredTurn, StuckTurn, ToolHold, TrailMark, } from '@noetic-tools/types';
|
|
185
185
|
/** @public */
|
|
186
186
|
export type { ForgetOptions, ForgetReport, ListStoredOptions, PendingSpend, ReconcileSpendOptions, SpendSettlement, StoredExecution, WriteOffSpendOptions, } from '@noetic-tools/types';
|
|
187
187
|
/** @public */
|
|
@@ -305,6 +305,8 @@ export declare class StepLedger {
|
|
|
305
305
|
get isEmpty(): boolean;
|
|
306
306
|
/** What retention has done so far. A snapshot — safe to hold. */
|
|
307
307
|
get stats(): StepLedgerStats;
|
|
308
|
+
/** Entries available to replay when a turn resumes. */
|
|
309
|
+
savedEntries(): Iterable<StepLedgerEntry>;
|
|
308
310
|
/**
|
|
309
311
|
* The recorded entry for `path`, when a previous run completed the same step there on
|
|
310
312
|
* the same input. A divergence — a different step id, kind, or input — returns
|
|
@@ -489,6 +489,10 @@ export class StepLedger {
|
|
|
489
489
|
...this.counts,
|
|
490
490
|
};
|
|
491
491
|
}
|
|
492
|
+
/** Entries available to replay when a turn resumes. */
|
|
493
|
+
savedEntries() {
|
|
494
|
+
return this.replayable.values();
|
|
495
|
+
}
|
|
492
496
|
/**
|
|
493
497
|
* The recorded entry for `path`, when a previous run completed the same step there on
|
|
494
498
|
* the same input. A divergence — a different step id, kind, or input — returns
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@noetic-tools/core",
|
|
3
|
-
"version": "5.22.0-next.
|
|
3
|
+
"version": "5.22.0-next.6",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Step primitives, interpreter, runtime, and context layer contract for Noetic agents.",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -69,7 +69,7 @@
|
|
|
69
69
|
},
|
|
70
70
|
"dependencies": {
|
|
71
71
|
"@noetic-tools/context": "^2.0.0-next.1",
|
|
72
|
-
"@noetic-tools/types": "^3.19.0-next.
|
|
72
|
+
"@noetic-tools/types": "^3.19.0-next.5",
|
|
73
73
|
"@openrouter/agent": "^0.11.0",
|
|
74
74
|
"@openrouter/sdk": "1.4.25",
|
|
75
75
|
"jsonata": "2.2.2",
|