@inhuman.tools/turn 0.0.0-stage → 0.1.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/LICENSE.md +7 -0
- package/dist/agent-calls/acceptance.d.ts +12 -0
- package/dist/agent-calls/acceptance.d.ts.map +1 -0
- package/dist/agent-calls/acceptance.js +64 -0
- package/dist/agent-calls/acceptance.js.map +1 -0
- package/dist/agent-calls/callbacks.d.ts +14 -0
- package/dist/agent-calls/callbacks.d.ts.map +1 -0
- package/dist/agent-calls/callbacks.js +59 -0
- package/dist/agent-calls/callbacks.js.map +1 -0
- package/dist/agent-calls/delivery.d.ts +7 -0
- package/dist/agent-calls/delivery.d.ts.map +1 -0
- package/dist/agent-calls/delivery.js +16 -0
- package/dist/agent-calls/delivery.js.map +1 -0
- package/dist/agent-calls/index.d.ts +7 -0
- package/dist/agent-calls/index.d.ts.map +1 -0
- package/dist/agent-calls/index.js +6 -0
- package/dist/agent-calls/index.js.map +1 -0
- package/dist/agent-calls/operator.d.ts +21 -0
- package/dist/agent-calls/operator.d.ts.map +1 -0
- package/dist/agent-calls/operator.js +78 -0
- package/dist/agent-calls/operator.js.map +1 -0
- package/dist/agent-calls/runtime.d.ts +99 -0
- package/dist/agent-calls/runtime.d.ts.map +1 -0
- package/dist/agent-calls/runtime.js +245 -0
- package/dist/agent-calls/runtime.js.map +1 -0
- package/dist/agent-calls/types.d.ts +31 -0
- package/dist/agent-calls/types.d.ts.map +1 -0
- package/dist/agent-calls/types.js +2 -0
- package/dist/agent-calls/types.js.map +1 -0
- package/dist/context/compact-floors.d.ts +43 -0
- package/dist/context/compact-floors.d.ts.map +1 -0
- package/dist/context/compact-floors.js +52 -0
- package/dist/context/compact-floors.js.map +1 -0
- package/dist/context/compact.d.ts +42 -0
- package/dist/context/compact.d.ts.map +1 -0
- package/dist/context/compact.js +155 -0
- package/dist/context/compact.js.map +1 -0
- package/dist/context/delivery.d.ts +13 -0
- package/dist/context/delivery.d.ts.map +1 -0
- package/dist/context/delivery.js +6 -0
- package/dist/context/delivery.js.map +1 -0
- package/dist/context/history.d.ts +66 -0
- package/dist/context/history.d.ts.map +1 -0
- package/dist/context/history.js +573 -0
- package/dist/context/history.js.map +1 -0
- package/dist/context/index.d.ts +10 -0
- package/dist/context/index.d.ts.map +1 -0
- package/dist/context/index.js +10 -0
- package/dist/context/index.js.map +1 -0
- package/dist/context/observations.d.ts +7 -0
- package/dist/context/observations.d.ts.map +1 -0
- package/dist/context/observations.js +11 -0
- package/dist/context/observations.js.map +1 -0
- package/dist/context/retention/defaults.d.ts +10 -0
- package/dist/context/retention/defaults.d.ts.map +1 -0
- package/dist/context/retention/defaults.js +47 -0
- package/dist/context/retention/defaults.js.map +1 -0
- package/dist/context/retention/index.d.ts +14 -0
- package/dist/context/retention/index.d.ts.map +1 -0
- package/dist/context/retention/index.js +21 -0
- package/dist/context/retention/index.js.map +1 -0
- package/dist/context/retention/narrow.d.ts +22 -0
- package/dist/context/retention/narrow.d.ts.map +1 -0
- package/dist/context/retention/narrow.js +140 -0
- package/dist/context/retention/narrow.js.map +1 -0
- package/dist/context/retention/render.d.ts +37 -0
- package/dist/context/retention/render.d.ts.map +1 -0
- package/dist/context/retention/render.js +149 -0
- package/dist/context/retention/render.js.map +1 -0
- package/dist/context/retention/resolve.d.ts +51 -0
- package/dist/context/retention/resolve.d.ts.map +1 -0
- package/dist/context/retention/resolve.js +254 -0
- package/dist/context/retention/resolve.js.map +1 -0
- package/dist/context/retention/schema.d.ts +345 -0
- package/dist/context/retention/schema.d.ts.map +1 -0
- package/dist/context/retention/schema.js +149 -0
- package/dist/context/retention/schema.js.map +1 -0
- package/dist/context/retention/stamp.d.ts +25 -0
- package/dist/context/retention/stamp.d.ts.map +1 -0
- package/dist/context/retention/stamp.js +103 -0
- package/dist/context/retention/stamp.js.map +1 -0
- package/dist/context/retention/step.d.ts +33 -0
- package/dist/context/retention/step.d.ts.map +1 -0
- package/dist/context/retention/step.js +74 -0
- package/dist/context/retention/step.js.map +1 -0
- package/dist/context/retention/view.d.ts +57 -0
- package/dist/context/retention/view.d.ts.map +1 -0
- package/dist/context/retention/view.js +121 -0
- package/dist/context/retention/view.js.map +1 -0
- package/dist/context/squeeze.d.ts +45 -0
- package/dist/context/squeeze.d.ts.map +1 -0
- package/dist/context/squeeze.js +182 -0
- package/dist/context/squeeze.js.map +1 -0
- package/dist/context/tools.d.ts +16 -0
- package/dist/context/tools.d.ts.map +1 -0
- package/dist/context/tools.js +73 -0
- package/dist/context/tools.js.map +1 -0
- package/dist/context/transcript.d.ts +151 -0
- package/dist/context/transcript.d.ts.map +1 -0
- package/dist/context/transcript.js +659 -0
- package/dist/context/transcript.js.map +1 -0
- package/dist/policy-processor/bypass.d.ts +95 -0
- package/dist/policy-processor/bypass.d.ts.map +1 -0
- package/dist/policy-processor/bypass.js +188 -0
- package/dist/policy-processor/bypass.js.map +1 -0
- package/dist/policy-processor/classifier.d.ts +47 -0
- package/dist/policy-processor/classifier.d.ts.map +1 -0
- package/dist/policy-processor/classifier.js +671 -0
- package/dist/policy-processor/classifier.js.map +1 -0
- package/dist/policy-processor/cluster.d.ts +7 -0
- package/dist/policy-processor/cluster.d.ts.map +1 -0
- package/dist/policy-processor/cluster.js +32 -0
- package/dist/policy-processor/cluster.js.map +1 -0
- package/dist/policy-processor/evidence.d.ts +26 -0
- package/dist/policy-processor/evidence.d.ts.map +1 -0
- package/dist/policy-processor/evidence.js +204 -0
- package/dist/policy-processor/evidence.js.map +1 -0
- package/dist/policy-processor/explain.d.ts +59 -0
- package/dist/policy-processor/explain.d.ts.map +1 -0
- package/dist/policy-processor/explain.js +224 -0
- package/dist/policy-processor/explain.js.map +1 -0
- package/dist/policy-processor/index.d.ts +6 -0
- package/dist/policy-processor/index.d.ts.map +1 -0
- package/dist/policy-processor/index.js +6 -0
- package/dist/policy-processor/index.js.map +1 -0
- package/dist/policy-processor/license.d.ts +23 -0
- package/dist/policy-processor/license.d.ts.map +1 -0
- package/dist/policy-processor/license.js +47 -0
- package/dist/policy-processor/license.js.map +1 -0
- package/dist/policy-processor/peel-cwd.d.ts +2 -0
- package/dist/policy-processor/peel-cwd.d.ts.map +1 -0
- package/dist/policy-processor/peel-cwd.js +2 -0
- package/dist/policy-processor/peel-cwd.js.map +1 -0
- package/dist/policy-processor/real-path.d.ts +7 -0
- package/dist/policy-processor/real-path.d.ts.map +1 -0
- package/dist/policy-processor/real-path.js +42 -0
- package/dist/policy-processor/real-path.js.map +1 -0
- package/dist/policy-processor/scope.d.ts +111 -0
- package/dist/policy-processor/scope.d.ts.map +1 -0
- package/dist/policy-processor/scope.js +629 -0
- package/dist/policy-processor/scope.js.map +1 -0
- package/dist/policy-processor/self-protect.d.ts +10 -0
- package/dist/policy-processor/self-protect.d.ts.map +1 -0
- package/dist/policy-processor/self-protect.js +48 -0
- package/dist/policy-processor/self-protect.js.map +1 -0
- package/dist/policy-processor/soft-deny.d.ts +75 -0
- package/dist/policy-processor/soft-deny.d.ts.map +1 -0
- package/dist/policy-processor/soft-deny.js +185 -0
- package/dist/policy-processor/soft-deny.js.map +1 -0
- package/dist/policy-processor/unattended-guard.d.ts +28 -0
- package/dist/policy-processor/unattended-guard.d.ts.map +1 -0
- package/dist/policy-processor/unattended-guard.js +145 -0
- package/dist/policy-processor/unattended-guard.js.map +1 -0
- package/dist/session/cancel.d.ts +14 -0
- package/dist/session/cancel.d.ts.map +1 -0
- package/dist/session/cancel.js +32 -0
- package/dist/session/cancel.js.map +1 -0
- package/dist/session/commands.d.ts +41 -0
- package/dist/session/commands.d.ts.map +1 -0
- package/dist/session/commands.js +241 -0
- package/dist/session/commands.js.map +1 -0
- package/dist/session/compact-actor.d.ts +23 -0
- package/dist/session/compact-actor.d.ts.map +1 -0
- package/dist/session/compact-actor.js +91 -0
- package/dist/session/compact-actor.js.map +1 -0
- package/dist/session/compact-env.d.ts +54 -0
- package/dist/session/compact-env.d.ts.map +1 -0
- package/dist/session/compact-env.js +345 -0
- package/dist/session/compact-env.js.map +1 -0
- package/dist/session/compact.d.ts +42 -0
- package/dist/session/compact.d.ts.map +1 -0
- package/dist/session/compact.js +68 -0
- package/dist/session/compact.js.map +1 -0
- package/dist/session/index.d.ts +9 -0
- package/dist/session/index.d.ts.map +1 -0
- package/dist/session/index.js +8 -0
- package/dist/session/index.js.map +1 -0
- package/dist/session/park.d.ts +16 -0
- package/dist/session/park.d.ts.map +1 -0
- package/dist/session/park.js +73 -0
- package/dist/session/park.js.map +1 -0
- package/dist/session/side-channel-reminder.d.ts +41 -0
- package/dist/session/side-channel-reminder.d.ts.map +1 -0
- package/dist/session/side-channel-reminder.js +132 -0
- package/dist/session/side-channel-reminder.js.map +1 -0
- package/dist/session/types.d.ts +44 -0
- package/dist/session/types.d.ts.map +1 -0
- package/dist/session/types.js +2 -0
- package/dist/session/types.js.map +1 -0
- package/dist/tool-catalog/catalog.d.ts +77 -0
- package/dist/tool-catalog/catalog.d.ts.map +1 -0
- package/dist/tool-catalog/catalog.js +380 -0
- package/dist/tool-catalog/catalog.js.map +1 -0
- package/dist/tool-catalog/index.d.ts +4 -0
- package/dist/tool-catalog/index.d.ts.map +1 -0
- package/dist/tool-catalog/index.js +3 -0
- package/dist/tool-catalog/index.js.map +1 -0
- package/dist/tool-catalog/recipes.d.ts +36 -0
- package/dist/tool-catalog/recipes.d.ts.map +1 -0
- package/dist/tool-catalog/recipes.js +121 -0
- package/dist/tool-catalog/recipes.js.map +1 -0
- package/dist/tool-catalog/types.d.ts +103 -0
- package/dist/tool-catalog/types.d.ts.map +1 -0
- package/dist/tool-catalog/types.js +5 -0
- package/dist/tool-catalog/types.js.map +1 -0
- package/dist/tool-runtime/approval/approved.d.ts +13 -0
- package/dist/tool-runtime/approval/approved.d.ts.map +1 -0
- package/dist/tool-runtime/approval/approved.js +20 -0
- package/dist/tool-runtime/approval/approved.js.map +1 -0
- package/dist/tool-runtime/approval/dist.d.ts +6 -0
- package/dist/tool-runtime/approval/dist.d.ts.map +1 -0
- package/dist/tool-runtime/approval/dist.js +28 -0
- package/dist/tool-runtime/approval/dist.js.map +1 -0
- package/dist/tool-runtime/approval/inbound.d.ts +7 -0
- package/dist/tool-runtime/approval/inbound.d.ts.map +1 -0
- package/dist/tool-runtime/approval/inbound.js +48 -0
- package/dist/tool-runtime/approval/inbound.js.map +1 -0
- package/dist/tool-runtime/approval/unattended.d.ts +8 -0
- package/dist/tool-runtime/approval/unattended.d.ts.map +1 -0
- package/dist/tool-runtime/approval/unattended.js +23 -0
- package/dist/tool-runtime/approval/unattended.js.map +1 -0
- package/dist/tool-runtime/blob-port.d.ts +46 -0
- package/dist/tool-runtime/blob-port.d.ts.map +1 -0
- package/dist/tool-runtime/blob-port.js +47 -0
- package/dist/tool-runtime/blob-port.js.map +1 -0
- package/dist/tool-runtime/extra-root.d.ts +3 -0
- package/dist/tool-runtime/extra-root.d.ts.map +1 -0
- package/dist/tool-runtime/extra-root.js +14 -0
- package/dist/tool-runtime/extra-root.js.map +1 -0
- package/dist/tool-runtime/handler.d.ts +29 -0
- package/dist/tool-runtime/handler.d.ts.map +1 -0
- package/dist/tool-runtime/handler.js +460 -0
- package/dist/tool-runtime/handler.js.map +1 -0
- package/dist/tool-runtime/index.d.ts +14 -0
- package/dist/tool-runtime/index.d.ts.map +1 -0
- package/dist/tool-runtime/index.js +14 -0
- package/dist/tool-runtime/index.js.map +1 -0
- package/dist/tool-runtime/peel.d.ts +19 -0
- package/dist/tool-runtime/peel.d.ts.map +1 -0
- package/dist/tool-runtime/peel.js +74 -0
- package/dist/tool-runtime/peel.js.map +1 -0
- package/dist/tool-runtime/plugin-context.d.ts +13 -0
- package/dist/tool-runtime/plugin-context.d.ts.map +1 -0
- package/dist/tool-runtime/plugin-context.js +55 -0
- package/dist/tool-runtime/plugin-context.js.map +1 -0
- package/dist/tool-runtime/recall.d.ts +18 -0
- package/dist/tool-runtime/recall.d.ts.map +1 -0
- package/dist/tool-runtime/recall.js +100 -0
- package/dist/tool-runtime/recall.js.map +1 -0
- package/dist/tool-runtime/run-system-tool.d.ts +13 -0
- package/dist/tool-runtime/run-system-tool.d.ts.map +1 -0
- package/dist/tool-runtime/run-system-tool.js +230 -0
- package/dist/tool-runtime/run-system-tool.js.map +1 -0
- package/dist/tool-runtime/tool-error.d.ts +6 -0
- package/dist/tool-runtime/tool-error.d.ts.map +1 -0
- package/dist/tool-runtime/tool-error.js +6 -0
- package/dist/tool-runtime/tool-error.js.map +1 -0
- package/dist/tool-runtime/worker-tools.d.ts +8 -0
- package/dist/tool-runtime/worker-tools.d.ts.map +1 -0
- package/dist/tool-runtime/worker-tools.js +65 -0
- package/dist/tool-runtime/worker-tools.js.map +1 -0
- package/dist/tool-runtime/write-admit.d.ts +21 -0
- package/dist/tool-runtime/write-admit.d.ts.map +1 -0
- package/dist/tool-runtime/write-admit.js +89 -0
- package/dist/tool-runtime/write-admit.js.map +1 -0
- package/package.json +114 -3
- package/policy/auto-mode-ask.scm +201 -0
- package/policy/bypass-detect.prompt +55 -0
- package/policy/classifier-bash-incomplete.prompt +46 -0
- package/policy/classifier-bash.prompt +45 -0
- package/policy/classifier-mcp.prompt +45 -0
- package/policy/classifier-read.prompt +44 -0
- package/policy/classifier-write-outside.prompt +46 -0
- package/policy/classifier-write-risky.prompt +45 -0
- package/policy/classifier-write-workspace.prompt +46 -0
- package/policy/classifier-write.prompt +45 -0
- package/policy/soft-deny-round1.prompt +51 -0
- package/policy/soft-deny-round2.prompt +60 -0
- package/src/agent-calls/acceptance.ts +71 -0
- package/src/agent-calls/callbacks.ts +75 -0
- package/src/agent-calls/delivery.ts +15 -0
- package/src/agent-calls/index.ts +16 -0
- package/src/agent-calls/operator.ts +99 -0
- package/src/agent-calls/runtime.ts +310 -0
- package/src/agent-calls/types.ts +33 -0
- package/src/context/compact-floors.ts +76 -0
- package/src/context/compact.ts +176 -0
- package/src/context/delivery.ts +15 -0
- package/src/context/history.ts +725 -0
- package/src/context/index.ts +35 -0
- package/src/context/observations.ts +13 -0
- package/src/context/retention/defaults.ts +58 -0
- package/src/context/retention/index.ts +45 -0
- package/src/context/retention/narrow.ts +123 -0
- package/src/context/retention/render.ts +177 -0
- package/src/context/retention/resolve.ts +306 -0
- package/src/context/retention/schema.ts +173 -0
- package/src/context/retention/stamp.ts +121 -0
- package/src/context/retention/step.ts +85 -0
- package/src/context/retention/view.ts +161 -0
- package/src/context/squeeze.ts +218 -0
- package/src/context/tools.ts +92 -0
- package/src/context/transcript.ts +705 -0
- package/src/policy-processor/README.md +43 -0
- package/src/policy-processor/bypass.ts +246 -0
- package/src/policy-processor/classifier.ts +711 -0
- package/src/policy-processor/cluster.ts +31 -0
- package/src/policy-processor/evidence.ts +214 -0
- package/src/policy-processor/explain.ts +310 -0
- package/src/policy-processor/index.ts +6 -0
- package/src/policy-processor/license.ts +45 -0
- package/src/policy-processor/peel-cwd.ts +1 -0
- package/src/policy-processor/real-path.ts +41 -0
- package/src/policy-processor/scope.ts +655 -0
- package/src/policy-processor/self-protect.ts +46 -0
- package/src/policy-processor/soft-deny.ts +225 -0
- package/src/policy-processor/unattended-guard.ts +158 -0
- package/src/session/cancel.ts +34 -0
- package/src/session/commands.ts +292 -0
- package/src/session/compact-actor.ts +132 -0
- package/src/session/compact-env.ts +539 -0
- package/src/session/compact.ts +95 -0
- package/src/session/index.ts +18 -0
- package/src/session/park.ts +73 -0
- package/src/session/side-channel-reminder.ts +148 -0
- package/src/session/types.ts +50 -0
- package/src/tool-catalog/catalog.ts +282 -0
- package/src/tool-catalog/index.ts +26 -0
- package/src/tool-catalog/recipes.ts +154 -0
- package/src/tool-catalog/types.ts +110 -0
- package/src/tool-runtime/approval/approved.ts +20 -0
- package/src/tool-runtime/approval/dist.ts +29 -0
- package/src/tool-runtime/approval/inbound.ts +50 -0
- package/src/tool-runtime/approval/unattended.ts +24 -0
- package/src/tool-runtime/blob-port.ts +64 -0
- package/src/tool-runtime/extra-root.ts +16 -0
- package/src/tool-runtime/handler.ts +576 -0
- package/src/tool-runtime/index.ts +21 -0
- package/src/tool-runtime/peel.ts +91 -0
- package/src/tool-runtime/plugin-context.ts +68 -0
- package/src/tool-runtime/recall.ts +104 -0
- package/src/tool-runtime/run-system-tool.ts +273 -0
- package/src/tool-runtime/tool-error.ts +6 -0
- package/src/tool-runtime/worker-tools.ts +76 -0
- package/src/tool-runtime/write-admit.ts +90 -0
- package/README.md +0 -3
|
@@ -0,0 +1,659 @@
|
|
|
1
|
+
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
|
|
2
|
+
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
|
|
3
|
+
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
|
|
4
|
+
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
|
|
5
|
+
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
|
|
6
|
+
var _, done = false;
|
|
7
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
8
|
+
var context = {};
|
|
9
|
+
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
|
|
10
|
+
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
|
|
11
|
+
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
|
|
12
|
+
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
|
|
13
|
+
if (kind === "accessor") {
|
|
14
|
+
if (result === void 0) continue;
|
|
15
|
+
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
|
|
16
|
+
if (_ = accept(result.get)) descriptor.get = _;
|
|
17
|
+
if (_ = accept(result.set)) descriptor.set = _;
|
|
18
|
+
if (_ = accept(result.init)) initializers.unshift(_);
|
|
19
|
+
}
|
|
20
|
+
else if (_ = accept(result)) {
|
|
21
|
+
if (kind === "field") initializers.unshift(_);
|
|
22
|
+
else descriptor[key] = _;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
if (target) Object.defineProperty(target, contextIn.name, descriptor);
|
|
26
|
+
done = true;
|
|
27
|
+
};
|
|
28
|
+
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
|
|
29
|
+
var useValue = arguments.length > 2;
|
|
30
|
+
for (var i = 0; i < initializers.length; i++) {
|
|
31
|
+
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
|
|
32
|
+
}
|
|
33
|
+
return useValue ? value : void 0;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Compact-strategy transcript: an ordered eat slice plus the cap that rides it.
|
|
37
|
+
*
|
|
38
|
+
* Cells are completions, tool calls, card/ack message chunks, or text cells.
|
|
39
|
+
*
|
|
40
|
+
* THE WALK: `walkTranscript` is the only eat iteration. A cutoff
|
|
41
|
+
* (`CompactJob`) projects the retained context; a completion anchors a round; tools and scheme
|
|
42
|
+
* programs attach to the open completion. Notification cells, approval notes,
|
|
43
|
+
* and steers collect parallel to tools — they never anchor or break a round.
|
|
44
|
+
* Inner scheme invocations
|
|
45
|
+
* live on `ProgrammaticJob` children, not this walk. `messagesFrom` and
|
|
46
|
+
* `Transcript.fromWork` walk it. Remaining eat cells
|
|
47
|
+
* (`MessageChunk`, text cell) yield `cell` so `fromWork` stays a walk —
|
|
48
|
+
* the last-eat and rounds views ignore them. A new chunk kind is a union
|
|
49
|
+
* member plus per-view handling. An uncommitted compact, prompt, ReACT, and
|
|
50
|
+
* non-eat values are silent.
|
|
51
|
+
*
|
|
52
|
+
* Rounds here group the materialized cell list so cutoff card cells
|
|
53
|
+
* can themselves be rounds. A successful `CompactJob` replaces the prefix and
|
|
54
|
+
* marks its `card` cells as prior cards
|
|
55
|
+
* (`transcript/compaction-results`). Cells before the first round anchor
|
|
56
|
+
* (prior cards) belong to the oldest slice: `sliceRounds(0, …)` starts at
|
|
57
|
+
* cell 0, so prefix ∪ keep = full.
|
|
58
|
+
*
|
|
59
|
+
* Occupancy: a stamped `totalTokens` is `usage.total_tokens` of that call —
|
|
60
|
+
* prompt + response, where the prompt carries the whole history. It is a
|
|
61
|
+
* monotone window GAUGE, not a per-cell weight; summing it across completions
|
|
62
|
+
* over-counts quadratically. `tokensUsed()` therefore diffs the gauge at the
|
|
63
|
+
* slice boundary (`baseGauge` = last reading before the slice) and falls back
|
|
64
|
+
* to `contentTokensOf` (ceil chars/4) for cells past the last reading.
|
|
65
|
+
* `contentTokensOf` is the raw content meter (`tokens/count`).
|
|
66
|
+
*
|
|
67
|
+
* There is no card-kind field on `CompletionExpectation`; card identity is the
|
|
68
|
+
* card set (plus `MessageChunk.card`). That is the hook the models
|
|
69
|
+
* give us today.
|
|
70
|
+
*/
|
|
71
|
+
import { arrival } from "@here.build/arrival/host-internals";
|
|
72
|
+
import { CompactJob, CompletionExpectation, contextNeedFromChunks, DEFAULT_COMPACT_KEEP_TURNS as COMPACT_KEEP_DIAL, jobTriggersReactLoop, ProgrammaticJob, SessionRoot, SessionTurn, tapeCompletion, ToolCallJob, TurnApprovalNoteChunk, TurnNotificationChunk, TurnSteerChunk, } from "@inhuman.tools/harness-model";
|
|
73
|
+
import { keepFloorsOfJob } from "./compact-floors.js";
|
|
74
|
+
import { compactToolLine, toolCallRepresentation } from "./squeeze.js";
|
|
75
|
+
export class MessageChunk {
|
|
76
|
+
role;
|
|
77
|
+
text;
|
|
78
|
+
ack;
|
|
79
|
+
checkIn;
|
|
80
|
+
constructor(role, text, flags) {
|
|
81
|
+
this.role = role;
|
|
82
|
+
this.text = text;
|
|
83
|
+
this.ack = flags?.ack === true;
|
|
84
|
+
this.checkIn = flags?.checkIn === true;
|
|
85
|
+
}
|
|
86
|
+
asCheckIn() {
|
|
87
|
+
return this.checkIn ? this : new MessageChunk(this.role, this.text, { ack: this.ack, checkIn: true });
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
export function turnSpine(turn) {
|
|
91
|
+
return [...turn.doneWork, ...turn.queue];
|
|
92
|
+
}
|
|
93
|
+
/** Compact-dropped `read_file` jobs are not in this projection (compact policy). */
|
|
94
|
+
export function historyBehind(turn, read) {
|
|
95
|
+
const ctx = historyRead(turn, read);
|
|
96
|
+
const own = messagesFrom(turnSpine(turn), ctx, () => turn.turnTail instanceof SessionTurn ? historyBehind(turn.turnTail, read) : []);
|
|
97
|
+
return finishHistory(own, () => (turn.turnTail instanceof SessionTurn ? historyBehind(turn.turnTail, read) : []));
|
|
98
|
+
}
|
|
99
|
+
/** Eat a successful compact saw: history strictly before the job, tail included. */
|
|
100
|
+
export function eatBeforeCompact(turn, job, read) {
|
|
101
|
+
const ctx = historyRead(turn, read);
|
|
102
|
+
const own = messagesFrom(turnSpine(turn), ctx, () => (turn.turnTail instanceof SessionTurn ? historyBehind(turn.turnTail, read) : []), job);
|
|
103
|
+
return finishHistory(own, () => (turn.turnTail instanceof SessionTurn ? historyBehind(turn.turnTail, read) : []));
|
|
104
|
+
}
|
|
105
|
+
function finishHistory(own, tail) {
|
|
106
|
+
if (own.anchored)
|
|
107
|
+
return own.messages;
|
|
108
|
+
if (own.replaced || own.messages.length > 0)
|
|
109
|
+
return [...own.messages, ...own.pending];
|
|
110
|
+
return [...tail(), ...own.pending];
|
|
111
|
+
}
|
|
112
|
+
function historyRead(turn, read) {
|
|
113
|
+
return {
|
|
114
|
+
tailNeed: turn.turnTail instanceof SessionTurn ? turn.turnTail.contextNeed : null,
|
|
115
|
+
kv: read?.kv ?? sessionKv(turn),
|
|
116
|
+
configFloor: read?.configFloor != null && read.configFloor >= 1 ? read.configFloor : COMPACT_KEEP_DIAL,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
function sessionKv(turn) {
|
|
120
|
+
const parent = turn.parent;
|
|
121
|
+
if (parent instanceof SessionRoot)
|
|
122
|
+
return parent.kvs;
|
|
123
|
+
return {};
|
|
124
|
+
}
|
|
125
|
+
function messagesFrom(chunks, read, seed, stopBefore) {
|
|
126
|
+
let eaten = null;
|
|
127
|
+
let last = null;
|
|
128
|
+
const tools = [];
|
|
129
|
+
const notifies = [];
|
|
130
|
+
const notes = [];
|
|
131
|
+
const steers = [];
|
|
132
|
+
const resetRound = () => {
|
|
133
|
+
last = null;
|
|
134
|
+
tools.length = 0;
|
|
135
|
+
notifies.length = 0;
|
|
136
|
+
notes.length = 0;
|
|
137
|
+
steers.length = 0;
|
|
138
|
+
};
|
|
139
|
+
for (const event of walkTranscript(chunks)) {
|
|
140
|
+
switch (event.kind) {
|
|
141
|
+
case "compact": {
|
|
142
|
+
const eat = last != null
|
|
143
|
+
? [...last.messages, last, ...tools, ...notifies, ...notes, ...steers]
|
|
144
|
+
: [...(eaten ?? seed()), ...notifies, ...notes, ...steers];
|
|
145
|
+
if (stopBefore != null && event.chunk === stopBefore) {
|
|
146
|
+
return {
|
|
147
|
+
messages: eat,
|
|
148
|
+
anchored: last != null,
|
|
149
|
+
replaced: true,
|
|
150
|
+
pending: [],
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
eaten = projectCompactMessages(eat, event.chunk, chunks, read);
|
|
154
|
+
resetRound();
|
|
155
|
+
break;
|
|
156
|
+
}
|
|
157
|
+
case "completion": {
|
|
158
|
+
last = event.chunk;
|
|
159
|
+
tools.length = 0;
|
|
160
|
+
notifies.length = 0;
|
|
161
|
+
notes.length = 0;
|
|
162
|
+
steers.length = 0;
|
|
163
|
+
break;
|
|
164
|
+
}
|
|
165
|
+
case "tool": {
|
|
166
|
+
if (jobTriggersReactLoop(event.chunk))
|
|
167
|
+
tools.push(event.chunk);
|
|
168
|
+
break;
|
|
169
|
+
}
|
|
170
|
+
case "scheme": {
|
|
171
|
+
tools.push(event.chunk);
|
|
172
|
+
break;
|
|
173
|
+
}
|
|
174
|
+
case "notify": {
|
|
175
|
+
notifies.push(event.chunk);
|
|
176
|
+
break;
|
|
177
|
+
}
|
|
178
|
+
case "approval-note": {
|
|
179
|
+
notes.push(event.chunk);
|
|
180
|
+
break;
|
|
181
|
+
}
|
|
182
|
+
case "steer": {
|
|
183
|
+
steers.push(event.chunk);
|
|
184
|
+
break;
|
|
185
|
+
}
|
|
186
|
+
default:
|
|
187
|
+
break;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
if (last != null) {
|
|
191
|
+
return {
|
|
192
|
+
messages: [...last.messages, last, ...tools, ...notifies, ...notes, ...steers],
|
|
193
|
+
anchored: true,
|
|
194
|
+
replaced: false,
|
|
195
|
+
pending: [],
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
return {
|
|
199
|
+
messages: eaten ?? [],
|
|
200
|
+
anchored: false,
|
|
201
|
+
replaced: eaten != null,
|
|
202
|
+
pending: [...notifies, ...notes, ...steers],
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
export function* walkTranscript(chunks) {
|
|
206
|
+
for (const chunk of chunks) {
|
|
207
|
+
if (chunk instanceof CompactJob && chunk.cutoff) {
|
|
208
|
+
yield { kind: "compact", chunk };
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
const pew = tapeCompletion(chunk);
|
|
212
|
+
if (pew != null) {
|
|
213
|
+
yield { kind: "completion", chunk: pew };
|
|
214
|
+
continue;
|
|
215
|
+
}
|
|
216
|
+
if (chunk instanceof CompletionExpectation) {
|
|
217
|
+
yield { kind: "completion", chunk };
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
if (chunk instanceof ToolCallJob) {
|
|
221
|
+
yield { kind: "tool", chunk };
|
|
222
|
+
}
|
|
223
|
+
else if (chunk instanceof ProgrammaticJob) {
|
|
224
|
+
yield { kind: "scheme", chunk };
|
|
225
|
+
}
|
|
226
|
+
else if (chunk instanceof TurnNotificationChunk) {
|
|
227
|
+
yield { kind: "notify", chunk };
|
|
228
|
+
}
|
|
229
|
+
else if (chunk instanceof TurnApprovalNoteChunk) {
|
|
230
|
+
yield { kind: "approval-note", chunk };
|
|
231
|
+
}
|
|
232
|
+
else if (chunk instanceof TurnSteerChunk) {
|
|
233
|
+
yield { kind: "steer", chunk };
|
|
234
|
+
}
|
|
235
|
+
else if (typeof chunk === "string" || chunk instanceof MessageChunk) {
|
|
236
|
+
yield { kind: "cell", chunk };
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
let Transcript = (() => {
|
|
241
|
+
var _a;
|
|
242
|
+
let _classDecorators = [(_a = arrival).private.bind(_a)];
|
|
243
|
+
let _classDescriptor;
|
|
244
|
+
let _classExtraInitializers = [];
|
|
245
|
+
let _classThis;
|
|
246
|
+
var Transcript = class {
|
|
247
|
+
static { _classThis = this; }
|
|
248
|
+
static {
|
|
249
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
|
|
250
|
+
__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
|
|
251
|
+
Transcript = _classThis = _classDescriptor.value;
|
|
252
|
+
if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
253
|
+
__runInitializers(_classThis, _classExtraInitializers);
|
|
254
|
+
}
|
|
255
|
+
cells;
|
|
256
|
+
cap;
|
|
257
|
+
static empty(cap = 0) {
|
|
258
|
+
return new Transcript([], cap);
|
|
259
|
+
}
|
|
260
|
+
static fromCells(cells, cap, cards) {
|
|
261
|
+
return new Transcript(cells, cap ?? inferCap(cells), cards);
|
|
262
|
+
}
|
|
263
|
+
static fromWork(work, cap) {
|
|
264
|
+
const cells = [];
|
|
265
|
+
const cards = new Set();
|
|
266
|
+
for (const event of walkTranscript(work)) {
|
|
267
|
+
if (event.kind === "compact") {
|
|
268
|
+
const projected = projectCompactMessages(cells, event.chunk, work, {
|
|
269
|
+
tailNeed: null,
|
|
270
|
+
kv: {},
|
|
271
|
+
configFloor: COMPACT_KEEP_DIAL,
|
|
272
|
+
});
|
|
273
|
+
cells.length = 0;
|
|
274
|
+
cards.clear();
|
|
275
|
+
for (const cell of projected) {
|
|
276
|
+
cells.push(cell);
|
|
277
|
+
if (cell instanceof CompactJob)
|
|
278
|
+
cards.add(cell);
|
|
279
|
+
}
|
|
280
|
+
continue;
|
|
281
|
+
}
|
|
282
|
+
cells.push(event.chunk);
|
|
283
|
+
}
|
|
284
|
+
return new Transcript(cells, cap ?? inferCap(cells), cards);
|
|
285
|
+
}
|
|
286
|
+
/** Occupancy window start — see preamble. */
|
|
287
|
+
baseGauge;
|
|
288
|
+
checkIns;
|
|
289
|
+
constructor(cells, cap, checkIns, baseGauge = 0) {
|
|
290
|
+
this.cells = cells;
|
|
291
|
+
this.cap = cap;
|
|
292
|
+
this.baseGauge = baseGauge;
|
|
293
|
+
this.checkIns = checkIns instanceof Set ? checkIns : new Set(checkIns);
|
|
294
|
+
}
|
|
295
|
+
isCheckIn(cell) {
|
|
296
|
+
return this.checkIns.has(cell) || cell instanceof CompactJob || (cell instanceof MessageChunk && cell.checkIn);
|
|
297
|
+
}
|
|
298
|
+
checkInCells() {
|
|
299
|
+
return this.cells.filter((cell) => this.isCheckIn(cell));
|
|
300
|
+
}
|
|
301
|
+
/** The transcript without its prior-card cells: what happened since, not what was already folded. */
|
|
302
|
+
withoutCheckIns() {
|
|
303
|
+
return new Transcript(this.cells.filter((cell) => !this.isCheckIn(cell)), this.cap, [], this.baseGauge);
|
|
304
|
+
}
|
|
305
|
+
sliceCells(from, to = this.cells.length) {
|
|
306
|
+
const lo = Math.max(0, from);
|
|
307
|
+
const hi = Math.min(this.cells.length, to);
|
|
308
|
+
const sliced = this.cells.slice(lo, hi);
|
|
309
|
+
let baseGauge = this.baseGauge;
|
|
310
|
+
for (let i = Math.min(lo, this.cells.length) - 1; i >= 0; i--) {
|
|
311
|
+
const cell = this.cells[i];
|
|
312
|
+
if (cell instanceof CompletionExpectation && cell.totalTokens != null && cell.totalTokens > 0) {
|
|
313
|
+
baseGauge = cell.totalTokens;
|
|
314
|
+
break;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
return new Transcript(sliced, this.cap, this.checkIns, baseGauge);
|
|
318
|
+
}
|
|
319
|
+
/** `sliceRounds(0, …)` includes prior cards — see preamble. */
|
|
320
|
+
sliceRounds(from, to) {
|
|
321
|
+
const rounds = this.rounds();
|
|
322
|
+
if (from >= to || rounds.length === 0) {
|
|
323
|
+
return new Transcript([], this.cap, this.checkIns, this.baseGauge);
|
|
324
|
+
}
|
|
325
|
+
const start = from === 0 ? 0 : (rounds[from]?.start ?? this.cells.length);
|
|
326
|
+
const end = rounds[to - 1]?.end ?? start;
|
|
327
|
+
return this.sliceCells(start, end);
|
|
328
|
+
}
|
|
329
|
+
rounds() {
|
|
330
|
+
const out = [];
|
|
331
|
+
let response = null;
|
|
332
|
+
let toolCalls = [];
|
|
333
|
+
let start = 0;
|
|
334
|
+
const flush = (end) => {
|
|
335
|
+
if (response == null)
|
|
336
|
+
return;
|
|
337
|
+
out.push({
|
|
338
|
+
start,
|
|
339
|
+
end,
|
|
340
|
+
prompt: response.userPrompt,
|
|
341
|
+
response,
|
|
342
|
+
toolCalls,
|
|
343
|
+
});
|
|
344
|
+
response = null;
|
|
345
|
+
toolCalls = [];
|
|
346
|
+
};
|
|
347
|
+
for (const [i, cell_] of this.cells.entries()) {
|
|
348
|
+
const cell = cell_;
|
|
349
|
+
if (cell instanceof CompletionExpectation) {
|
|
350
|
+
flush(i);
|
|
351
|
+
response = cell;
|
|
352
|
+
start = i;
|
|
353
|
+
}
|
|
354
|
+
else if ((cell instanceof ToolCallJob || cell instanceof ProgrammaticJob) && response != null) {
|
|
355
|
+
toolCalls.push(cell);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
flush(this.cells.length);
|
|
359
|
+
return out;
|
|
360
|
+
}
|
|
361
|
+
/** Occupancy — see preamble. */
|
|
362
|
+
tokensUsed() {
|
|
363
|
+
let lastGauge = -1;
|
|
364
|
+
let lastIndex = -1;
|
|
365
|
+
for (let i = 0; i < this.cells.length; i++) {
|
|
366
|
+
const cell = this.cells[i];
|
|
367
|
+
if (cell instanceof CompletionExpectation && cell.totalTokens != null && cell.totalTokens > 0) {
|
|
368
|
+
lastGauge = cell.totalTokens;
|
|
369
|
+
lastIndex = i;
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
if (lastIndex === -1)
|
|
373
|
+
return contentTokensOf(this.cells);
|
|
374
|
+
const gauged = Math.max(0, lastGauge - this.baseGauge);
|
|
375
|
+
return gauged + contentTokensOf(this.cells.slice(lastIndex + 1));
|
|
376
|
+
}
|
|
377
|
+
/**
|
|
378
|
+
* The frontier is the OLDEST round of this transcript. During the classify
|
|
379
|
+
* walk `preserve-transcript` runs frontier → newest (chronological), so the
|
|
380
|
+
* round being decided is the first one.
|
|
381
|
+
*/
|
|
382
|
+
frontierRound() {
|
|
383
|
+
const rounds = this.rounds();
|
|
384
|
+
if (rounds.length === 0)
|
|
385
|
+
return new Transcript([], this.cap, this.checkIns, this.baseGauge);
|
|
386
|
+
return this.sliceRounds(0, 1);
|
|
387
|
+
}
|
|
388
|
+
/** `transcript/newer`: preserve without the frontier — drop the oldest round. */
|
|
389
|
+
withoutFrontier() {
|
|
390
|
+
const rounds = this.rounds();
|
|
391
|
+
if (rounds.length <= 1)
|
|
392
|
+
return new Transcript([], this.cap, this.checkIns, this.baseGauge);
|
|
393
|
+
return this.sliceRounds(1, rounds.length);
|
|
394
|
+
}
|
|
395
|
+
/** `transcript/older`: compact without the frontier — drop the newest round. */
|
|
396
|
+
withoutNewest() {
|
|
397
|
+
const rounds = this.rounds();
|
|
398
|
+
if (rounds.length <= 1)
|
|
399
|
+
return new Transcript([], this.cap, this.checkIns, this.baseGauge);
|
|
400
|
+
return this.sliceRounds(0, rounds.length - 1);
|
|
401
|
+
}
|
|
402
|
+
completeRounds() {
|
|
403
|
+
const rounds = this.rounds();
|
|
404
|
+
if (rounds.length === 0)
|
|
405
|
+
return new Transcript([], this.cap, this.checkIns, this.baseGauge);
|
|
406
|
+
const last = rounds.at(-1);
|
|
407
|
+
if (last && isRoundComplete(last))
|
|
408
|
+
return this.sliceRounds(0, rounds.length);
|
|
409
|
+
if (rounds.length === 1)
|
|
410
|
+
return new Transcript([], this.cap, this.checkIns, this.baseGauge);
|
|
411
|
+
return this.sliceRounds(0, rounds.length - 1);
|
|
412
|
+
}
|
|
413
|
+
partialRound() {
|
|
414
|
+
const rounds = this.rounds();
|
|
415
|
+
if (rounds.length === 0)
|
|
416
|
+
return null;
|
|
417
|
+
const last = rounds.at(-1);
|
|
418
|
+
if (last && isRoundComplete(last))
|
|
419
|
+
return null;
|
|
420
|
+
return this.sliceRounds(rounds.length - 1, rounds.length);
|
|
421
|
+
}
|
|
422
|
+
concat(other) {
|
|
423
|
+
const checkIns = new Set(this.checkIns);
|
|
424
|
+
for (const cell of other.checkIns)
|
|
425
|
+
checkIns.add(cell);
|
|
426
|
+
for (const cell of other.cells) {
|
|
427
|
+
if (other.isCheckIn(cell))
|
|
428
|
+
checkIns.add(cell);
|
|
429
|
+
}
|
|
430
|
+
return new Transcript([...this.cells, ...other.cells], this.cap || other.cap, checkIns, this.baseGauge);
|
|
431
|
+
}
|
|
432
|
+
};
|
|
433
|
+
return Transcript = _classThis;
|
|
434
|
+
})();
|
|
435
|
+
export { Transcript };
|
|
436
|
+
/**
|
|
437
|
+
* Content-token stand-in: `ceil(chars / 4)`. Replace this one function when
|
|
438
|
+
* per-cell prompt/response tokens exist on the model.
|
|
439
|
+
*/
|
|
440
|
+
export function estimateTokens(text) {
|
|
441
|
+
if (text.length === 0)
|
|
442
|
+
return 0;
|
|
443
|
+
return Math.ceil(text.length / 4);
|
|
444
|
+
}
|
|
445
|
+
/** Content meter — see preamble. */
|
|
446
|
+
export function contentTokensOf(cells) {
|
|
447
|
+
let used = 0;
|
|
448
|
+
for (const cell of cells)
|
|
449
|
+
used += estimateTokens(textOfCell(cell));
|
|
450
|
+
return used;
|
|
451
|
+
}
|
|
452
|
+
export function charsOf(cells) {
|
|
453
|
+
let n = 0;
|
|
454
|
+
for (const cell of cells)
|
|
455
|
+
n += textOfCell(cell).length;
|
|
456
|
+
return n;
|
|
457
|
+
}
|
|
458
|
+
export function inferCap(cells, fallback = 0) {
|
|
459
|
+
for (let i = cells.length - 1; i >= 0; i--) {
|
|
460
|
+
const cell = cells[i];
|
|
461
|
+
if (cell instanceof CompletionExpectation && (cell.contextLimit ?? 0) > 0) {
|
|
462
|
+
return cell.contextLimit ?? 0;
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
return fallback;
|
|
466
|
+
}
|
|
467
|
+
export function payloadSuffix(transcript, n) {
|
|
468
|
+
const cells = transcript.cells.filter((cell) => !transcript.isCheckIn(cell));
|
|
469
|
+
if (n <= 0 || cells.length === 0)
|
|
470
|
+
return [];
|
|
471
|
+
const start = Math.max(0, cells.length - Math.floor(n));
|
|
472
|
+
let owner = start;
|
|
473
|
+
while (owner > 0 &&
|
|
474
|
+
(cells[owner] instanceof ToolCallJob ||
|
|
475
|
+
cells[owner] instanceof ProgrammaticJob ||
|
|
476
|
+
cells[owner] instanceof TurnNotificationChunk ||
|
|
477
|
+
cells[owner] instanceof TurnApprovalNoteChunk ||
|
|
478
|
+
cells[owner] instanceof TurnSteerChunk)) {
|
|
479
|
+
owner--;
|
|
480
|
+
}
|
|
481
|
+
if (!(cells[owner] instanceof CompletionExpectation))
|
|
482
|
+
owner = start;
|
|
483
|
+
return cells.slice(owner);
|
|
484
|
+
}
|
|
485
|
+
function isRoundComplete(round) {
|
|
486
|
+
const requested = round.response.responseCalls.length;
|
|
487
|
+
const hasResponse = round.response.responseText !== "" || (round.response.totalTokens ?? 0) > 0;
|
|
488
|
+
if (requested === 0)
|
|
489
|
+
return hasResponse;
|
|
490
|
+
if (round.toolCalls.length < requested)
|
|
491
|
+
return false;
|
|
492
|
+
return round.toolCalls.every((tool) => {
|
|
493
|
+
const json = tool instanceof ToolCallJob ? tool.sealedOutcomeJson() : tool.receiptJson;
|
|
494
|
+
return json != null && json !== "";
|
|
495
|
+
});
|
|
496
|
+
}
|
|
497
|
+
/** Floors are non-zero by default; a single-round eat legitimately keeps all. */
|
|
498
|
+
export function sliceCompactEat(eat, floors) {
|
|
499
|
+
const rounds = eat.rounds();
|
|
500
|
+
const emptyKeep = eat.sliceCells(eat.cells.length);
|
|
501
|
+
if (rounds.length === 0)
|
|
502
|
+
return { prefix: eat, keep: emptyKeep };
|
|
503
|
+
let keepRounds = floors.rounds;
|
|
504
|
+
const userTurns = floors.userTurns;
|
|
505
|
+
if (keepRounds > 0 || userTurns > 0)
|
|
506
|
+
keepRounds = Math.max(keepRounds, 1);
|
|
507
|
+
let keepFrom = rounds.length;
|
|
508
|
+
if (keepRounds > 0)
|
|
509
|
+
keepFrom = Math.min(keepFrom, Math.max(0, rounds.length - keepRounds));
|
|
510
|
+
if (userTurns > 0) {
|
|
511
|
+
let counted = 0;
|
|
512
|
+
let idx = rounds.length;
|
|
513
|
+
for (let i = rounds.length - 1; i >= 0; i -= 1) {
|
|
514
|
+
if (rounds[i].prompt !== "")
|
|
515
|
+
counted += 1;
|
|
516
|
+
idx = i;
|
|
517
|
+
if (counted >= userTurns)
|
|
518
|
+
break;
|
|
519
|
+
}
|
|
520
|
+
if (counted > 0)
|
|
521
|
+
keepFrom = Math.min(keepFrom, idx);
|
|
522
|
+
}
|
|
523
|
+
// Preserve floors are a preference, not a right to keep everything. Clamp so
|
|
524
|
+
// >= 2 rounds always leaves one round in the prefix. One round still keeps all.
|
|
525
|
+
if (rounds.length >= 2 && keepFrom < 1)
|
|
526
|
+
keepFrom = 1;
|
|
527
|
+
if (keepFrom >= rounds.length)
|
|
528
|
+
return { prefix: eat, keep: emptyKeep };
|
|
529
|
+
if (keepFrom <= 0)
|
|
530
|
+
return { prefix: eat.sliceRounds(0, 0), keep: eat };
|
|
531
|
+
return {
|
|
532
|
+
prefix: eat.sliceRounds(0, keepFrom),
|
|
533
|
+
keep: eat.sliceRounds(keepFrom, rounds.length),
|
|
534
|
+
};
|
|
535
|
+
}
|
|
536
|
+
/**
|
|
537
|
+
* Keep is {@link sliceCompactEat}'s suffix. Squeeze is up to `floors.rounds`
|
|
538
|
+
* rounds immediately before that keep, taken from the prefix. If the squeeze
|
|
539
|
+
* would empty the prefix and at least two rounds exist, shrink the squeeze
|
|
540
|
+
* (to zero when the prefix is a single round) so one round stays in it.
|
|
541
|
+
*/
|
|
542
|
+
export function sliceCompactZones(eat, floors) {
|
|
543
|
+
const sliced = sliceCompactEat(eat, floors);
|
|
544
|
+
const empty = eat.sliceRounds(0, 0);
|
|
545
|
+
const prefixRounds = sliced.prefix.rounds();
|
|
546
|
+
let squeezeCount = Math.min(Math.max(0, floors.rounds), prefixRounds.length);
|
|
547
|
+
if (eat.rounds().length >= 2 && prefixRounds.length - squeezeCount < 1) {
|
|
548
|
+
squeezeCount = Math.max(0, prefixRounds.length - 1);
|
|
549
|
+
}
|
|
550
|
+
if (squeezeCount <= 0)
|
|
551
|
+
return { prefix: sliced.prefix, squeeze: empty, keep: sliced.keep };
|
|
552
|
+
const prefixEnd = prefixRounds.length - squeezeCount;
|
|
553
|
+
return {
|
|
554
|
+
prefix: sliced.prefix.sliceRounds(0, prefixEnd),
|
|
555
|
+
squeeze: sliced.prefix.sliceRounds(prefixEnd, prefixRounds.length),
|
|
556
|
+
keep: sliced.keep,
|
|
557
|
+
};
|
|
558
|
+
}
|
|
559
|
+
/** Squeeze band as role rows, then keep cells verbatim. The card is separate. */
|
|
560
|
+
export function projectCompactRows(eat, floors) {
|
|
561
|
+
const zones = sliceCompactZones(eat, floors);
|
|
562
|
+
const rows = [];
|
|
563
|
+
for (const round of zones.squeeze.rounds()) {
|
|
564
|
+
const promptText = round.prompt.trim();
|
|
565
|
+
const response = round.response.responseText.trim();
|
|
566
|
+
if (promptText !== "")
|
|
567
|
+
rows.push({ kind: "user", text: promptText });
|
|
568
|
+
if (response !== "")
|
|
569
|
+
rows.push({ kind: "assistant", text: response });
|
|
570
|
+
for (const tool of round.toolCalls) {
|
|
571
|
+
if (tool instanceof ToolCallJob && !jobTriggersReactLoop(tool))
|
|
572
|
+
continue;
|
|
573
|
+
const line = compactToolLine(tool).trim();
|
|
574
|
+
if (line === "")
|
|
575
|
+
continue;
|
|
576
|
+
const name = (tool instanceof ToolCallJob ? tool.name : tool.toolName).trim();
|
|
577
|
+
rows.push({
|
|
578
|
+
kind: "user",
|
|
579
|
+
text: `[${name === "" ? "tool" : name}] ${line}`,
|
|
580
|
+
});
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
for (const cell of zones.keep.cells) {
|
|
584
|
+
if (cell instanceof MessageChunk)
|
|
585
|
+
continue;
|
|
586
|
+
if (isCompletionMessage(cell))
|
|
587
|
+
rows.push({ kind: "keep", cell });
|
|
588
|
+
}
|
|
589
|
+
return { zones, rows };
|
|
590
|
+
}
|
|
591
|
+
/**
|
|
592
|
+
* The card is the `CompactJob` itself, a typed cell (F10): `Transcript` then
|
|
593
|
+
* knows it is a prior card, and `transcript/compaction-results` is no longer
|
|
594
|
+
* always empty. Its text is `standIn`.
|
|
595
|
+
*/
|
|
596
|
+
export function projectionMessages(card, rows) {
|
|
597
|
+
const out = [];
|
|
598
|
+
if (card.standIn.trim() !== "")
|
|
599
|
+
out.push(card);
|
|
600
|
+
for (const row of rows) {
|
|
601
|
+
if (row.kind === "keep")
|
|
602
|
+
out.push(row.cell);
|
|
603
|
+
else if (row.text !== "")
|
|
604
|
+
out.push(row.text);
|
|
605
|
+
}
|
|
606
|
+
return out;
|
|
607
|
+
}
|
|
608
|
+
function floorsFor(job, chunks, read) {
|
|
609
|
+
const index = chunks.indexOf(job);
|
|
610
|
+
const before = index === -1 ? chunks : chunks.slice(0, index);
|
|
611
|
+
const need = contextNeedFromChunks(before, read.tailNeed);
|
|
612
|
+
return keepFloorsOfJob(job, need, read);
|
|
613
|
+
}
|
|
614
|
+
/** Card, squeeze lines, and verbatim keep cells for the wire and the next eat. */
|
|
615
|
+
export function compactLeadRows(turn, job, read) {
|
|
616
|
+
const ctx = historyRead(turn, read);
|
|
617
|
+
const eat = eatBeforeCompact(turn, job, read);
|
|
618
|
+
const { rows } = projectCompactRows(Transcript.fromCells(eat), floorsFor(job, turnSpine(turn), ctx));
|
|
619
|
+
const card = job.standIn.trim();
|
|
620
|
+
return card === "" ? rows : [{ kind: "user", text: card }, ...rows];
|
|
621
|
+
}
|
|
622
|
+
function projectCompactMessages(eat, job, chunks, read) {
|
|
623
|
+
const { rows } = projectCompactRows(Transcript.fromCells(eat), floorsFor(job, chunks, read));
|
|
624
|
+
return projectionMessages(job, rows);
|
|
625
|
+
}
|
|
626
|
+
function isCompletionMessage(cell) {
|
|
627
|
+
return !(cell instanceof MessageChunk);
|
|
628
|
+
}
|
|
629
|
+
export function textOfCell(cell) {
|
|
630
|
+
if (typeof cell === "string")
|
|
631
|
+
return cell;
|
|
632
|
+
if (cell instanceof CompactJob)
|
|
633
|
+
return cell.standIn.trim();
|
|
634
|
+
if (cell instanceof MessageChunk)
|
|
635
|
+
return cell.text;
|
|
636
|
+
if (cell instanceof TurnNotificationChunk)
|
|
637
|
+
return cell.notificationText();
|
|
638
|
+
if (cell instanceof TurnApprovalNoteChunk)
|
|
639
|
+
return cell.approvalNoteText();
|
|
640
|
+
if (cell instanceof TurnSteerChunk)
|
|
641
|
+
return cell.completionMessage;
|
|
642
|
+
if (cell instanceof ToolCallJob) {
|
|
643
|
+
const scheme = cell.children.find((child) => child instanceof ProgrammaticJob);
|
|
644
|
+
if (scheme !== undefined) {
|
|
645
|
+
return `scheme ${scheme.expr} ${scheme.resultText()}`.trim();
|
|
646
|
+
}
|
|
647
|
+
const painted = toolCallRepresentation(cell);
|
|
648
|
+
return `${painted.name} ${painted.args} ${painted.result}`.trim();
|
|
649
|
+
}
|
|
650
|
+
if (cell instanceof ProgrammaticJob) {
|
|
651
|
+
return `scheme ${cell.expr} ${cell.resultText()}`.trim();
|
|
652
|
+
}
|
|
653
|
+
const pew = tapeCompletion(cell) ?? (cell instanceof CompletionExpectation ? cell : null);
|
|
654
|
+
if (pew != null) {
|
|
655
|
+
return `${pew.userPrompt}\n${pew.responseText}`.trim();
|
|
656
|
+
}
|
|
657
|
+
return "";
|
|
658
|
+
}
|
|
659
|
+
//# sourceMappingURL=transcript.js.map
|