@reinsjs/core 0.8.0 → 0.10.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/CHANGELOG.md +29 -0
- package/README.md +16 -1
- package/dist/{chunk-63AJKR7J.js → chunk-3QMUURRG.js} +2 -1
- package/dist/chunk-3QMUURRG.js.map +1 -0
- package/dist/index.d.ts +110 -6
- package/dist/index.js +247 -49
- package/dist/index.js.map +1 -1
- package/dist/testing/index.d.ts +1 -1
- package/dist/testing/index.js +1 -1
- package/dist/{types-DkOLu2fW.d.ts → types-CFO8uTd0.d.ts} +45 -3
- package/package.json +1 -1
- package/dist/chunk-63AJKR7J.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -8,7 +8,7 @@ import {
|
|
|
8
8
|
createEvent,
|
|
9
9
|
renderToolReference,
|
|
10
10
|
uuidv7
|
|
11
|
-
} from "./chunk-
|
|
11
|
+
} from "./chunk-3QMUURRG.js";
|
|
12
12
|
|
|
13
13
|
// src/events/core.ts
|
|
14
14
|
function contextTokensOf(usage) {
|
|
@@ -34,7 +34,8 @@ var DEFAULT_MODEL_INVISIBLE_TYPES = /* @__PURE__ */ new Set([
|
|
|
34
34
|
"core.memory_op",
|
|
35
35
|
"core.handoff",
|
|
36
36
|
"core.error",
|
|
37
|
-
"core.tools_bound"
|
|
37
|
+
"core.tools_bound",
|
|
38
|
+
"core.tool_started"
|
|
38
39
|
]);
|
|
39
40
|
function visibilityFilter(opts = {}) {
|
|
40
41
|
const invisible = opts.invisibleTypes ? new Set(opts.invisibleTypes) : DEFAULT_MODEL_INVISIBLE_TYPES;
|
|
@@ -50,6 +51,16 @@ function visibilityFilter(opts = {}) {
|
|
|
50
51
|
// src/projection/estimate.ts
|
|
51
52
|
var PER_EVENT_OVERHEAD = 4;
|
|
52
53
|
var IMAGE_TOKENS = 1600;
|
|
54
|
+
var VIDEO_TOKENS_PER_SEC = 100;
|
|
55
|
+
var AUDIO_TOKENS_PER_SEC = 32;
|
|
56
|
+
var VIDEO_BYTES_PER_SEC = 125e3;
|
|
57
|
+
var AUDIO_BYTES_PER_SEC = 8e3;
|
|
58
|
+
var MIN_GUESSED_SEC = 30;
|
|
59
|
+
function mediaSeconds(part, bytesPerSec) {
|
|
60
|
+
if (part.durationSec !== void 0) return part.durationSec;
|
|
61
|
+
const bytes = part.data.length * 3 / 4;
|
|
62
|
+
return Math.max(bytes / bytesPerSec, MIN_GUESSED_SEC);
|
|
63
|
+
}
|
|
53
64
|
function estimateTextTokens(text) {
|
|
54
65
|
let ascii = 0;
|
|
55
66
|
let other = 0;
|
|
@@ -62,9 +73,27 @@ function estimateTextTokens(text) {
|
|
|
62
73
|
function partsTokens(parts) {
|
|
63
74
|
let n = 0;
|
|
64
75
|
for (const p of parts) {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
76
|
+
switch (p.type) {
|
|
77
|
+
case "text":
|
|
78
|
+
n += estimateTextTokens(p.text);
|
|
79
|
+
break;
|
|
80
|
+
case "tool_reference":
|
|
81
|
+
n += estimateTextTokens(renderToolReference(p));
|
|
82
|
+
break;
|
|
83
|
+
case "image":
|
|
84
|
+
n += IMAGE_TOKENS;
|
|
85
|
+
break;
|
|
86
|
+
case "video":
|
|
87
|
+
n += Math.ceil(mediaSeconds(p, VIDEO_BYTES_PER_SEC) * VIDEO_TOKENS_PER_SEC);
|
|
88
|
+
break;
|
|
89
|
+
case "audio":
|
|
90
|
+
n += Math.ceil(mediaSeconds(p, AUDIO_BYTES_PER_SEC) * AUDIO_TOKENS_PER_SEC);
|
|
91
|
+
break;
|
|
92
|
+
default: {
|
|
93
|
+
const _exhaustive = p;
|
|
94
|
+
void _exhaustive;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
68
97
|
}
|
|
69
98
|
return n;
|
|
70
99
|
}
|
|
@@ -294,8 +323,24 @@ function splitTurns(events) {
|
|
|
294
323
|
return turns;
|
|
295
324
|
}
|
|
296
325
|
var DEFAULT_CUT_TO_RATIO = 0.6;
|
|
326
|
+
function partExcerpt(p) {
|
|
327
|
+
switch (p.type) {
|
|
328
|
+
case "text":
|
|
329
|
+
return p.text;
|
|
330
|
+
case "image":
|
|
331
|
+
case "video":
|
|
332
|
+
case "audio":
|
|
333
|
+
return `[${p.type}]`;
|
|
334
|
+
case "tool_reference":
|
|
335
|
+
return `[tool ${p.name}]`;
|
|
336
|
+
default: {
|
|
337
|
+
const _exhaustive = p;
|
|
338
|
+
return _exhaustive;
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
}
|
|
297
342
|
function excerpt(parts, max = 120) {
|
|
298
|
-
const text = parts.map(
|
|
343
|
+
const text = parts.map(partExcerpt).join(" ").replace(/\s+/g, " ").trim();
|
|
299
344
|
return text.length > max ? `${text.slice(0, max)}\u2026` : text;
|
|
300
345
|
}
|
|
301
346
|
var defaultThresholdSummary = (removed, ctx) => {
|
|
@@ -582,6 +627,48 @@ var LoweringError = class extends Error {
|
|
|
582
627
|
context;
|
|
583
628
|
};
|
|
584
629
|
|
|
630
|
+
// src/loop/stop.ts
|
|
631
|
+
var STOPPED_NOTE = "stopped by the user";
|
|
632
|
+
var STOPPED_NOT_EXECUTED_TEXT = "Not executed: the run was stopped by the user before this call started.";
|
|
633
|
+
var STOPPED_IN_PROGRESS_TEXT = "Interrupted: the run was stopped by the user while this call was in progress; it may have partly taken effect.";
|
|
634
|
+
var INTERRUPTED_NOT_EXECUTED_TEXT = "Not executed: an earlier run was interrupted before this call started.";
|
|
635
|
+
var INTERRUPTED_IN_PROGRESS_TEXT = "Interrupted: an earlier run was interrupted while this call was in progress; it may have partly taken effect.";
|
|
636
|
+
var OUTCOME_UNKNOWN_TEXT = "Execution was interrupted before a result was recorded; the outcome is unknown. Check the current state before retrying.";
|
|
637
|
+
var RunStop = class extends Error {
|
|
638
|
+
by;
|
|
639
|
+
constructor(by = "user") {
|
|
640
|
+
super(
|
|
641
|
+
by === "user" ? "the run was stopped by the user" : "the host closed calls left by an interrupted run"
|
|
642
|
+
);
|
|
643
|
+
this.name = "RunStop";
|
|
644
|
+
this.by = by;
|
|
645
|
+
}
|
|
646
|
+
};
|
|
647
|
+
function stopTextsOf(reason) {
|
|
648
|
+
return reason?.by === "host" ? {
|
|
649
|
+
notExecuted: INTERRUPTED_NOT_EXECUTED_TEXT,
|
|
650
|
+
inProgress: INTERRUPTED_IN_PROGRESS_TEXT,
|
|
651
|
+
note: "closed calls left by an interrupted run"
|
|
652
|
+
} : { notExecuted: STOPPED_NOT_EXECUTED_TEXT, inProgress: STOPPED_IN_PROGRESS_TEXT, note: STOPPED_NOTE };
|
|
653
|
+
}
|
|
654
|
+
function isStopReason(reason) {
|
|
655
|
+
return typeof reason === "object" && reason !== null && reason.name === "RunStop";
|
|
656
|
+
}
|
|
657
|
+
function isStopped(signal) {
|
|
658
|
+
return signal?.aborted === true && isStopReason(signal.reason);
|
|
659
|
+
}
|
|
660
|
+
function isReplayable(tool) {
|
|
661
|
+
return tool.replayable ?? tool.risk === "low";
|
|
662
|
+
}
|
|
663
|
+
function startedToolCallIds(timeline) {
|
|
664
|
+
const started = /* @__PURE__ */ new Set();
|
|
665
|
+
for (const raw of timeline) {
|
|
666
|
+
const e = raw;
|
|
667
|
+
if (e.type === "core.tool_started") started.add(e.payload.toolCallId);
|
|
668
|
+
}
|
|
669
|
+
return started;
|
|
670
|
+
}
|
|
671
|
+
|
|
585
672
|
// src/loop/retry.ts
|
|
586
673
|
var DEFAULT_RETRY_MAX_ATTEMPTS = 3;
|
|
587
674
|
var DEFAULT_RETRY_BASE_DELAY_MS = 1e3;
|
|
@@ -623,7 +710,7 @@ var CONNECTION_ERROR_NAMES = /* @__PURE__ */ new Set([
|
|
|
623
710
|
function isDefinitelyPermanent(err) {
|
|
624
711
|
if (err instanceof LoweringError) return true;
|
|
625
712
|
const name = err?.name;
|
|
626
|
-
return name === "AbortError" || name === "APIUserAbortError";
|
|
713
|
+
return name === "AbortError" || name === "APIUserAbortError" || isStopReason(err);
|
|
627
714
|
}
|
|
628
715
|
function statusFromMessage(message) {
|
|
629
716
|
const m = /^\s*(\d{3})\b/.exec(message) ?? /\b(?:status(?:\s+code)?|HTTP)[\s:=]+(\d{3})\b/i.exec(message);
|
|
@@ -743,6 +830,20 @@ async function computeConfigHash(input) {
|
|
|
743
830
|
})
|
|
744
831
|
);
|
|
745
832
|
}
|
|
833
|
+
async function computeModelDigest(model) {
|
|
834
|
+
return sha256(JSON.stringify(model));
|
|
835
|
+
}
|
|
836
|
+
async function computePendingToolsDigest(calls, tools) {
|
|
837
|
+
const names = [...new Set(calls.map((c) => c.payload.name))].sort();
|
|
838
|
+
return sha256(
|
|
839
|
+
JSON.stringify(
|
|
840
|
+
names.map((name) => {
|
|
841
|
+
const t = tools.find((x) => x.name === name);
|
|
842
|
+
return t ? [name, t.description, t.inputSchema] : [name, null];
|
|
843
|
+
})
|
|
844
|
+
)
|
|
845
|
+
);
|
|
846
|
+
}
|
|
746
847
|
async function computePendingDigest(calls) {
|
|
747
848
|
return sha256(
|
|
748
849
|
JSON.stringify(calls.map((c) => [c.payload.toolCallId, c.payload.name, c.payload.args ?? null]))
|
|
@@ -755,19 +856,26 @@ async function serializeRunState(input) {
|
|
|
755
856
|
lastSeq: input.lastSeq,
|
|
756
857
|
pendingToolCallIds: input.pending.map((c) => c.payload.toolCallId),
|
|
757
858
|
configHash: input.configHash,
|
|
758
|
-
pendingDigest: await computePendingDigest(input.pending)
|
|
859
|
+
pendingDigest: await computePendingDigest(input.pending),
|
|
860
|
+
...input.model !== void 0 && input.tools !== void 0 ? {
|
|
861
|
+
modelDigest: await computeModelDigest(input.model),
|
|
862
|
+
pendingToolsDigest: await computePendingToolsDigest(input.pending, input.tools)
|
|
863
|
+
} : {}
|
|
759
864
|
};
|
|
760
865
|
return input.secret === void 0 ? state : signRunState(state, input.secret);
|
|
761
866
|
}
|
|
762
867
|
function signingMaterial(state) {
|
|
763
|
-
|
|
868
|
+
const fields = [
|
|
764
869
|
state.v,
|
|
765
870
|
state.sessionId,
|
|
766
871
|
state.lastSeq,
|
|
767
872
|
state.pendingToolCallIds,
|
|
768
873
|
state.configHash,
|
|
769
874
|
state.pendingDigest
|
|
770
|
-
]
|
|
875
|
+
];
|
|
876
|
+
if (state.modelDigest !== void 0 || state.pendingToolsDigest !== void 0)
|
|
877
|
+
fields.push(state.modelDigest ?? null, state.pendingToolsDigest ?? null);
|
|
878
|
+
return JSON.stringify(fields);
|
|
771
879
|
}
|
|
772
880
|
async function hmac(secret, material) {
|
|
773
881
|
const key = await crypto.subtle.importKey(
|
|
@@ -806,7 +914,7 @@ var RunStateError = class extends Error {
|
|
|
806
914
|
};
|
|
807
915
|
function assertRunStateShape(raw) {
|
|
808
916
|
const s = raw;
|
|
809
|
-
const ok = typeof s === "object" && s !== null && s.v === 1 && typeof s.sessionId === "string" && Number.isInteger(s.lastSeq) && Array.isArray(s.pendingToolCallIds) && s.pendingToolCallIds.every((id) => typeof id === "string") && typeof s.configHash === "string" && typeof s.pendingDigest === "string" && (s.sig === void 0 || typeof s.sig === "string");
|
|
917
|
+
const ok = typeof s === "object" && s !== null && s.v === 1 && typeof s.sessionId === "string" && Number.isInteger(s.lastSeq) && Array.isArray(s.pendingToolCallIds) && s.pendingToolCallIds.every((id) => typeof id === "string") && typeof s.configHash === "string" && typeof s.pendingDigest === "string" && (s.modelDigest === void 0 || typeof s.modelDigest === "string") && (s.pendingToolsDigest === void 0 || typeof s.pendingToolsDigest === "string") && (s.sig === void 0 || typeof s.sig === "string");
|
|
810
918
|
if (!ok) throw new RunStateError("malformed", "run state is not in the v1 shape");
|
|
811
919
|
}
|
|
812
920
|
async function validateResume(input) {
|
|
@@ -835,7 +943,14 @@ async function validateResume(input) {
|
|
|
835
943
|
);
|
|
836
944
|
}
|
|
837
945
|
}
|
|
838
|
-
|
|
946
|
+
const narrowed = state.modelDigest !== void 0 && state.pendingToolsDigest !== void 0 && input.model !== void 0 && input.tools !== void 0;
|
|
947
|
+
if (narrowed && !input.allowConfigDrift) {
|
|
948
|
+
if (await computeModelDigest(input.model) !== state.modelDigest) {
|
|
949
|
+
throw new RunStateError("config_mismatch", "the model differs from when the run paused", {
|
|
950
|
+
part: "model"
|
|
951
|
+
});
|
|
952
|
+
}
|
|
953
|
+
} else if (state.configHash !== input.configHash && !input.allowConfigDrift) {
|
|
839
954
|
throw new RunStateError(
|
|
840
955
|
"config_mismatch",
|
|
841
956
|
"model, tool set or system prompt differs from when the run paused; pass allowConfigDrift if this is intended",
|
|
@@ -864,6 +979,14 @@ async function validateResume(input) {
|
|
|
864
979
|
got: ids
|
|
865
980
|
});
|
|
866
981
|
}
|
|
982
|
+
if (narrowed && !input.allowConfigDrift && await computePendingToolsDigest(pending, input.tools) !== state.pendingToolsDigest) {
|
|
983
|
+
const names = [...new Set(pending.map((c) => c.payload.name))].sort();
|
|
984
|
+
throw new RunStateError(
|
|
985
|
+
"config_mismatch",
|
|
986
|
+
`the definition of pending tool ${names.join(", ")} differs from when the run paused; pass allowConfigDrift if this is intended`,
|
|
987
|
+
{ part: "pendingTools", tools: names }
|
|
988
|
+
);
|
|
989
|
+
}
|
|
867
990
|
return pending;
|
|
868
991
|
}
|
|
869
992
|
|
|
@@ -925,15 +1048,25 @@ function toolSpecOf(tool) {
|
|
|
925
1048
|
function deferredToolSpecOf(tool, deferred) {
|
|
926
1049
|
return deferred ? { ...toolSpecOf(tool), deferLoading: true } : toolSpecOf(tool);
|
|
927
1050
|
}
|
|
1051
|
+
function isDuration(x) {
|
|
1052
|
+
return x === void 0 || typeof x === "number" && Number.isFinite(x) && x > 0;
|
|
1053
|
+
}
|
|
928
1054
|
function isContentPart(x) {
|
|
929
1055
|
if (typeof x !== "object" || x === null) return false;
|
|
930
1056
|
const p = x;
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
1057
|
+
switch (p.type) {
|
|
1058
|
+
case "text":
|
|
1059
|
+
return typeof p.text === "string";
|
|
1060
|
+
case "image":
|
|
1061
|
+
return typeof p.mime === "string" && typeof p.data === "string";
|
|
1062
|
+
case "video":
|
|
1063
|
+
case "audio":
|
|
1064
|
+
return typeof p.mime === "string" && p.mime.startsWith(`${p.type}/`) && typeof p.data === "string" && isDuration(p.durationSec);
|
|
1065
|
+
case "tool_reference":
|
|
1066
|
+
return typeof p.name === "string" && typeof p.description === "string" && typeof p.inputSchema === "object" && p.inputSchema !== null && !Array.isArray(p.inputSchema);
|
|
1067
|
+
default:
|
|
1068
|
+
return false;
|
|
935
1069
|
}
|
|
936
|
-
return false;
|
|
937
1070
|
}
|
|
938
1071
|
function isToolResult(x) {
|
|
939
1072
|
if (typeof x !== "object" || x === null) return false;
|
|
@@ -1002,7 +1135,7 @@ var INPUT_DRAFT_TYPES = /* @__PURE__ */ new Set([
|
|
|
1002
1135
|
"core.system_note"
|
|
1003
1136
|
]);
|
|
1004
1137
|
var INPUT_ACTORS = /* @__PURE__ */ new Set(["user", "model", "tool", "system", "host"]);
|
|
1005
|
-
var CONTENT_PART_TYPES = /* @__PURE__ */ new Set(["text", "image", "tool_reference"]);
|
|
1138
|
+
var CONTENT_PART_TYPES = /* @__PURE__ */ new Set(["text", "image", "video", "audio", "tool_reference"]);
|
|
1006
1139
|
function classifyInputItem(item) {
|
|
1007
1140
|
if (typeof item !== "object" || item === null) return void 0;
|
|
1008
1141
|
const { type, actor } = item;
|
|
@@ -1013,16 +1146,7 @@ function classifyInputItem(item) {
|
|
|
1013
1146
|
}
|
|
1014
1147
|
var NOTE_KINDS = /* @__PURE__ */ new Set(["perception", "pin", "budget", "host"]);
|
|
1015
1148
|
function isContentPartList(x) {
|
|
1016
|
-
|
|
1017
|
-
return x.every((p) => {
|
|
1018
|
-
if (typeof p !== "object" || p === null) return false;
|
|
1019
|
-
const part = p;
|
|
1020
|
-
if (part.type === "text") return typeof part.text === "string";
|
|
1021
|
-
if (part.type === "image") return typeof part.mime === "string" && typeof part.data === "string";
|
|
1022
|
-
if (part.type === "tool_reference")
|
|
1023
|
-
return typeof part.name === "string" && typeof part.description === "string" && typeof part.inputSchema === "object" && part.inputSchema !== null;
|
|
1024
|
-
return false;
|
|
1025
|
-
});
|
|
1149
|
+
return Array.isArray(x) && x.every(isContentPart);
|
|
1026
1150
|
}
|
|
1027
1151
|
function corePayloadProblem(draft) {
|
|
1028
1152
|
const p = typeof draft.payload === "object" && draft.payload !== null ? draft.payload : {};
|
|
@@ -1112,14 +1236,44 @@ async function* runLoop(cfg) {
|
|
|
1112
1236
|
lastSeq += events.length;
|
|
1113
1237
|
return events;
|
|
1114
1238
|
};
|
|
1115
|
-
const
|
|
1116
|
-
const
|
|
1239
|
+
const settleStopped = async (texts) => {
|
|
1240
|
+
const timeline = await readTimeline(log, sessionId, { registry });
|
|
1241
|
+
const pending = pendingToolCalls(timeline);
|
|
1242
|
+
if (pending.length === 0) return [];
|
|
1243
|
+
const started = startedToolCallIds(timeline);
|
|
1244
|
+
return append(
|
|
1245
|
+
pending.map((call) => {
|
|
1246
|
+
const { toolCallId, name } = call.payload;
|
|
1247
|
+
const tool = baseTools.find((t) => t.name === name);
|
|
1248
|
+
const text = !started.has(toolCallId) ? texts.notExecuted : tool !== void 0 && isReplayable(tool) ? texts.inProgress : OUTCOME_UNKNOWN_TEXT;
|
|
1249
|
+
return {
|
|
1250
|
+
type: "core.tool_result",
|
|
1251
|
+
actor: "tool",
|
|
1252
|
+
parentId: call.id,
|
|
1253
|
+
provenance: { source: name },
|
|
1254
|
+
payload: { toolCallId, name, content: [{ type: "text", text }], isError: true }
|
|
1255
|
+
};
|
|
1256
|
+
})
|
|
1257
|
+
);
|
|
1258
|
+
};
|
|
1259
|
+
const pause = async (requested, requestedInterruptions) => {
|
|
1260
|
+
const stopped = isStopped(cfg.signal);
|
|
1261
|
+
const texts = stopped ? stopTextsOf(cfg.signal?.reason) : void 0;
|
|
1262
|
+
const reason = texts ? "host" : requested;
|
|
1263
|
+
const interruptions = texts ? [{ kind: "host", note: texts.note }] : requestedInterruptions;
|
|
1264
|
+
const settled = texts ? await settleStopped(texts) : [];
|
|
1265
|
+
const events = [
|
|
1266
|
+
...settled,
|
|
1267
|
+
...await append([{ type: "core.run_paused", actor: "system", payload: { reason } }])
|
|
1268
|
+
];
|
|
1117
1269
|
const pending = pendingToolCalls(await readTimeline(log, sessionId, { registry }));
|
|
1118
1270
|
const state = await serializeRunState({
|
|
1119
1271
|
sessionId,
|
|
1120
1272
|
lastSeq,
|
|
1121
1273
|
pending,
|
|
1122
1274
|
configHash,
|
|
1275
|
+
model,
|
|
1276
|
+
tools: baseTools,
|
|
1123
1277
|
...cfg.secret !== void 0 ? { secret: cfg.secret } : {}
|
|
1124
1278
|
});
|
|
1125
1279
|
return { events, result: { status: "paused", sessionId, lastSeq, reason, interruptions, state } };
|
|
@@ -1147,6 +1301,8 @@ async function* runLoop(cfg) {
|
|
|
1147
1301
|
sessionId,
|
|
1148
1302
|
timeline,
|
|
1149
1303
|
configHash,
|
|
1304
|
+
model,
|
|
1305
|
+
tools: baseTools,
|
|
1150
1306
|
...cfg.secret !== void 0 ? { secret: cfg.secret } : {},
|
|
1151
1307
|
...cfg.allowConfigDrift !== void 0 ? { allowConfigDrift: cfg.allowConfigDrift } : {}
|
|
1152
1308
|
});
|
|
@@ -1183,16 +1339,25 @@ async function* runLoop(cfg) {
|
|
|
1183
1339
|
}))
|
|
1184
1340
|
);
|
|
1185
1341
|
}
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1342
|
+
if (!cfg.signal?.aborted) {
|
|
1343
|
+
yield* await append(
|
|
1344
|
+
toolsBoundDrafts({
|
|
1345
|
+
timeline: await readTimeline(log, sessionId, { registry }),
|
|
1346
|
+
toolNames: baseTools.map((t) => t.name),
|
|
1347
|
+
configHash,
|
|
1348
|
+
announce: cfg.announceToolChanges ?? true
|
|
1349
|
+
})
|
|
1350
|
+
);
|
|
1351
|
+
}
|
|
1194
1352
|
yield* await append(input.map((d) => withResultTrust(d, baseTools)));
|
|
1195
1353
|
while (true) {
|
|
1354
|
+
if (cfg.signal?.aborted) {
|
|
1355
|
+
const { events, result } = await pause("host", [
|
|
1356
|
+
{ kind: "host", note: "host aborted before this turn started" }
|
|
1357
|
+
]);
|
|
1358
|
+
yield* events;
|
|
1359
|
+
return result;
|
|
1360
|
+
}
|
|
1196
1361
|
const turnStartedAt = now();
|
|
1197
1362
|
const timeline = await readTimeline(log, sessionId, { registry });
|
|
1198
1363
|
const lastUsage = lastBudgetUsage(timeline)?.payload.tokens;
|
|
@@ -1245,13 +1410,6 @@ async function* runLoop(cfg) {
|
|
|
1245
1410
|
const events = await append(emitted.splice(0));
|
|
1246
1411
|
return events;
|
|
1247
1412
|
};
|
|
1248
|
-
if (cfg.signal?.aborted) {
|
|
1249
|
-
const { events, result } = await pause("host", [
|
|
1250
|
-
{ kind: "host", note: "host aborted before this turn started" }
|
|
1251
|
-
]);
|
|
1252
|
-
yield* events;
|
|
1253
|
-
return result;
|
|
1254
|
-
}
|
|
1255
1413
|
const pending = pendingToolCalls(timeline);
|
|
1256
1414
|
if (pending.length > 0) {
|
|
1257
1415
|
const located = toolBatchOf(timeline, pending);
|
|
@@ -1268,7 +1426,7 @@ async function* runLoop(cfg) {
|
|
|
1268
1426
|
batch
|
|
1269
1427
|
});
|
|
1270
1428
|
toolCallsTotal += settled.executed;
|
|
1271
|
-
if (settled.interruptions.length > 0) {
|
|
1429
|
+
if (settled.interruptions.length > 0 || isStopped(cfg.signal)) {
|
|
1272
1430
|
const reason = pauseReasonOf(settled.interruptions);
|
|
1273
1431
|
const { events, result } = await pause(reason, settled.interruptions);
|
|
1274
1432
|
yield* events;
|
|
@@ -1358,6 +1516,13 @@ async function* runLoop(cfg) {
|
|
|
1358
1516
|
message: failure.message,
|
|
1359
1517
|
detail: { usage: outcome?.usage }
|
|
1360
1518
|
};
|
|
1519
|
+
if (!canRetry && cfg.signal?.aborted) {
|
|
1520
|
+
const { events, result } = await pause("host", [
|
|
1521
|
+
{ kind: "host", note: "host aborted the model request" }
|
|
1522
|
+
]);
|
|
1523
|
+
yield* events;
|
|
1524
|
+
return result;
|
|
1525
|
+
}
|
|
1361
1526
|
if (!canRetry) {
|
|
1362
1527
|
const { events, result } = await fail({
|
|
1363
1528
|
...base,
|
|
@@ -1433,7 +1598,7 @@ async function* runLoop(cfg) {
|
|
|
1433
1598
|
}
|
|
1434
1599
|
}
|
|
1435
1600
|
]);
|
|
1436
|
-
if (interruptions.length > 0) {
|
|
1601
|
+
if (interruptions.length > 0 || calls.length > 0 && isStopped(cfg.signal)) {
|
|
1437
1602
|
const { events, result } = await pause(pauseReasonOf(interruptions), interruptions);
|
|
1438
1603
|
yield* events;
|
|
1439
1604
|
return result;
|
|
@@ -1528,6 +1693,7 @@ async function* executeToolCalls(ctx, calls, timeline, deps) {
|
|
|
1528
1693
|
};
|
|
1529
1694
|
const decisions = /* @__PURE__ */ new Map();
|
|
1530
1695
|
const requested = /* @__PURE__ */ new Set();
|
|
1696
|
+
const started = startedToolCallIds(timeline);
|
|
1531
1697
|
for (const raw of timeline) {
|
|
1532
1698
|
const e = raw;
|
|
1533
1699
|
if (e.type === "core.approval_decision") decisions.set(e.payload.toolCallId, e.payload);
|
|
@@ -1548,6 +1714,10 @@ async function* executeToolCalls(ctx, calls, timeline, deps) {
|
|
|
1548
1714
|
provenance: { source: name },
|
|
1549
1715
|
payload: { toolCallId, name, content: [{ type: "text", text }], isError: true }
|
|
1550
1716
|
});
|
|
1717
|
+
if (started.has(toolCallId) && (tool === void 0 || !isReplayable(tool))) {
|
|
1718
|
+
yield* await settle(errorResult(OUTCOME_UNKNOWN_TEXT));
|
|
1719
|
+
continue;
|
|
1720
|
+
}
|
|
1551
1721
|
const decided = decisions.get(toolCallId);
|
|
1552
1722
|
if (decided && !decided.approved) {
|
|
1553
1723
|
yield* await append([errorResult(`Approval denied${decided.reason ? `: ${decided.reason}` : ""}`)]);
|
|
@@ -1620,11 +1790,23 @@ async function* executeToolCalls(ctx, calls, timeline, deps) {
|
|
|
1620
1790
|
batch.held.push(toolCallId);
|
|
1621
1791
|
continue;
|
|
1622
1792
|
}
|
|
1793
|
+
if (cfg.signal?.aborted) break;
|
|
1794
|
+
const replayable = isReplayable(tool);
|
|
1795
|
+
const markStarted = async function* () {
|
|
1796
|
+
if (started.has(toolCallId)) return;
|
|
1797
|
+
started.add(toolCallId);
|
|
1798
|
+
yield* await flush();
|
|
1799
|
+
yield* await append([
|
|
1800
|
+
{ type: "core.tool_started", actor: "system", parentId: call.id, payload: { toolCallId } }
|
|
1801
|
+
]);
|
|
1802
|
+
};
|
|
1803
|
+
if (!replayable) yield* markStarted();
|
|
1623
1804
|
let result;
|
|
1624
1805
|
try {
|
|
1625
1806
|
summary.executed++;
|
|
1626
1807
|
const out = await tool.execute(args, toolCtx);
|
|
1627
1808
|
if (isSubagentPause(out)) {
|
|
1809
|
+
yield* markStarted();
|
|
1628
1810
|
summary.interruptions.push({ kind: "subagent", toolCallId, call: call.payload, ...out.detail });
|
|
1629
1811
|
batch.held.push(toolCallId);
|
|
1630
1812
|
if (cfg.signal?.aborted) break;
|
|
@@ -1641,7 +1823,9 @@ async function* executeToolCalls(ctx, calls, timeline, deps) {
|
|
|
1641
1823
|
payload: { toolCallId, name, content: normalized.content, isError: normalized.isError ?? false }
|
|
1642
1824
|
};
|
|
1643
1825
|
} catch (err) {
|
|
1644
|
-
result = errorResult(
|
|
1826
|
+
result = errorResult(
|
|
1827
|
+
!replayable && cfg.signal?.aborted ? OUTCOME_UNKNOWN_TEXT : `Tool execution failed: ${errorMessageOf(err)}`
|
|
1828
|
+
);
|
|
1645
1829
|
}
|
|
1646
1830
|
for (const s of sockets) {
|
|
1647
1831
|
const replaced = await s.afterTool?.(ctx, call, result);
|
|
@@ -1925,7 +2109,7 @@ function memoryStore() {
|
|
|
1925
2109
|
}
|
|
1926
2110
|
|
|
1927
2111
|
// src/index.ts
|
|
1928
|
-
var REINS_VERSION = "0.
|
|
2112
|
+
var REINS_VERSION = "0.10.0";
|
|
1929
2113
|
export {
|
|
1930
2114
|
BUILTIN_APPROVAL_POLICY,
|
|
1931
2115
|
CORE_SCHEMAS,
|
|
@@ -1939,13 +2123,20 @@ export {
|
|
|
1939
2123
|
DEFAULT_TRUST,
|
|
1940
2124
|
EventSchemaRegistry,
|
|
1941
2125
|
INPUT_DRAFT_TYPES,
|
|
2126
|
+
INTERRUPTED_IN_PROGRESS_TEXT,
|
|
2127
|
+
INTERRUPTED_NOT_EXECUTED_TEXT,
|
|
1942
2128
|
InMemoryBlobStore,
|
|
1943
2129
|
InMemoryEventLog,
|
|
1944
2130
|
InMemoryMemoryStore,
|
|
1945
2131
|
InMemoryRunLease,
|
|
1946
2132
|
LoweringError,
|
|
2133
|
+
OUTCOME_UNKNOWN_TEXT,
|
|
1947
2134
|
REINS_VERSION,
|
|
1948
2135
|
RunStateError,
|
|
2136
|
+
RunStop,
|
|
2137
|
+
STOPPED_IN_PROGRESS_TEXT,
|
|
2138
|
+
STOPPED_NOTE,
|
|
2139
|
+
STOPPED_NOT_EXECUTED_TEXT,
|
|
1949
2140
|
SchemaError,
|
|
1950
2141
|
StoreError,
|
|
1951
2142
|
UNTRUSTED_CLOSE_TAG,
|
|
@@ -1954,7 +2145,9 @@ export {
|
|
|
1954
2145
|
backoffDelayMs,
|
|
1955
2146
|
budgetTruncate,
|
|
1956
2147
|
computeConfigHash,
|
|
2148
|
+
computeModelDigest,
|
|
1957
2149
|
computePendingDigest,
|
|
2150
|
+
computePendingToolsDigest,
|
|
1958
2151
|
contextOverheadOf,
|
|
1959
2152
|
contextTokensOf,
|
|
1960
2153
|
covers,
|
|
@@ -1982,6 +2175,9 @@ export {
|
|
|
1982
2175
|
isContentPart,
|
|
1983
2176
|
isModelOutput,
|
|
1984
2177
|
isPinNote,
|
|
2178
|
+
isReplayable,
|
|
2179
|
+
isStopReason,
|
|
2180
|
+
isStopped,
|
|
1985
2181
|
isSubagentPause,
|
|
1986
2182
|
isTransientFailure,
|
|
1987
2183
|
keptBy,
|
|
@@ -2008,7 +2204,9 @@ export {
|
|
|
2008
2204
|
serializeRunState,
|
|
2009
2205
|
signRunState,
|
|
2010
2206
|
splitTurns,
|
|
2207
|
+
startedToolCallIds,
|
|
2011
2208
|
statusFromMessage,
|
|
2209
|
+
stopTextsOf,
|
|
2012
2210
|
subagentPause,
|
|
2013
2211
|
supersededIds,
|
|
2014
2212
|
toolBatchOf,
|