@linxiraos/pi-agent-core 1.5.1 → 1.5.11
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 +17 -2
- package/THIRD-PARTY-NOTICES.txt +22 -11
- package/dist/types/agent-loop.d.ts +7 -1
- package/dist/types/compaction/compaction.d.ts +4 -2
- package/dist/types/compaction/entries.d.ts +35 -2
- package/dist/types/index.d.ts +1 -0
- package/dist/types/message-identity.d.ts +8 -0
- package/dist/types/speculative-execution.d.ts +1 -1
- package/dist/types/telemetry.d.ts +1 -1
- package/dist/types/types.d.ts +9 -0
- package/package.json +8 -8
- package/src/agent-loop.ts +53 -18
- package/src/agent.ts +16 -1
- package/src/compaction/compaction.ts +24 -4
- package/src/compaction/entries.ts +46 -1
- package/src/index.ts +1 -0
- package/src/message-identity.ts +14 -0
- package/src/speculative-execution.ts +7 -0
- package/src/telemetry.ts +1 -1
- package/src/tokenizer.ts +98 -9
- package/src/types.ts +9 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
## [Unreleased]
|
|
2
2
|
|
|
3
|
-
## [1.5.
|
|
3
|
+
## [1.5.11] - 2026-11-20
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
### Changed
|
|
6
|
+
|
|
7
|
+
- Released as part of Zeta 1.5.11; no package-specific user-visible changes.
|
|
6
8
|
|
|
7
9
|
## [9.2.2] - 2026-01-31
|
|
8
10
|
|
|
@@ -130,6 +132,19 @@
|
|
|
130
132
|
|
|
131
133
|
Initial release under @oh-my-pi scope. See previous releases at [badlogic/pi-mono](https://github.com/badlogic/pi-mono).
|
|
132
134
|
|
|
135
|
+
## [1.5.10] - 2026-11-19
|
|
136
|
+
|
|
137
|
+
### Fixed
|
|
138
|
+
|
|
139
|
+
- Corrected context cost estimates for requests that omit reasoning history, so billing estimates now include thinking text only when it is sent or reconstructed for a tool call.
|
|
140
|
+
|
|
141
|
+
## [1.5.2] - 2026-11-13
|
|
142
|
+
|
|
143
|
+
### Changed
|
|
144
|
+
|
|
145
|
+
- Released as part of Zeta 1.5.2 asset review; no package-specific user-visible changes.
|
|
146
|
+
- `prepareCompaction()` runs as the `compaction.prepare` loop phase, so a `ui.loop-blocked` stall from a cold token walk over a long branch names compaction instead of "unknown" ([#15027](https://github.com/can1357/oh-my-pi/issues/15027), [#18867](https://github.com/can1357/oh-my-pi/pull/18867) by [@wolfiesch](https://github.com/wolfiesch))
|
|
147
|
+
|
|
133
148
|
## [1.5.0] - 2026-11-09
|
|
134
149
|
|
|
135
150
|
- Released as part of Zeta 1.5.0; no package-specific user-visible changes.
|
package/THIRD-PARTY-NOTICES.txt
CHANGED
|
@@ -19,20 +19,19 @@ Forked application trees:
|
|
|
19
19
|
|
|
20
20
|
| Path | Upstream | Snapshot | License | What Zeta changed |
|
|
21
21
|
|---|---|---|---|---|
|
|
22
|
-
| `packages/ main/ crates/ python/ lsp/ sdk/ types/ skills/` | oh-my-pi (OMP), https://github.com/can1357/oh-my-pi | integrated through tag `v18.
|
|
22
|
+
| `packages/ main/ crates/ python/ lsp/ sdk/ types/ skills/` | oh-my-pi (OMP), https://github.com/can1357/oh-my-pi | integrated through tag `v18.22.0` (`document/upstream-sync.md`) | see upstream files | product name, paths, command names and env prefixes; branded assets; internal identifiers renamed to `__zeta*` |
|
|
23
23
|
| `web-ui/` | OMP Web, https://github.com/17380936778/omp-web | frozen snapshot, **abandoned 2026-11-05** (`document/upstream-sync.md`) | see upstream files | third-party UI, not maintained by Zeta: frozen as received, no builds, no fixes; the product renderer is `web-app/` (`Differences from Upstream` in README) |
|
|
24
|
-
| `web-app/` (`src/studio`, `src/vendor/*`) | OMP Studio
|
|
25
|
-
| `termide/` | TermIDE, https://github.com/termide/termide | tag `0.
|
|
24
|
+
| `web-app/` (`src/studio`, `src/vendor/*`) | OMP Studio, https://github.com/the-snowpear/omp-studio | official `v0.2.1` (`6d0b764bb9eac88c1671c0380229823c5630cc74`) integrated 2026-11-16; `web-ui/` is a separate frozen OMP Web snapshot | MIT | maintained by Zeta now: the host contract is reimplemented in `studio-host` (pair/CSRF/query/command/events), the renderer was rebranded (`zeta-studio` wire name, Zeta links/fixtures), and Zeta adds windowed transcripts, precompressed static serving, the plugin registry, and the usage dashboard |
|
|
25
|
+
| `termide/` | TermIDE, https://github.com/termide/termide | tag `0.49.0` = `057eb55e0a7c4ee969982fab62dc83bde028e052` | MIT | deviations listed in `termide/VENDOR.md` |
|
|
26
26
|
| `editor/npm/*` (`@linxiraos/editor*`) | TermIDE `panel-editor` | same tree as `termide/` | MIT | built as the editor-only bin `termide-editor` (`editor/npm/editor/VENDOR.md`) |
|
|
27
27
|
| `plugins/official/*` | per plugin | per plugin | per plugin | each directory carries `plugin.json`, README with source, and its license |
|
|
28
28
|
|
|
29
|
-
Why the fork renames things
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
existing installation does not silently break.
|
|
29
|
+
Why the fork renames things: Zeta keeps its product identity distinct from its
|
|
30
|
+
upstreams. Renames cover the product name, command names (`zetawork` /
|
|
31
|
+
`zetacode` / `zetaide`), the `zeta://` scheme, the npm scope `@linxiraos/*`, and
|
|
32
|
+
release asset names (`zeta-cli-*`). User-visible configuration uses Zeta names;
|
|
33
|
+
upstream status directories and retired configuration aliases are not loaded.
|
|
34
|
+
The component licenses below retain their original grants and notices.
|
|
36
35
|
|
|
37
36
|
Brand and assets:
|
|
38
37
|
|
|
@@ -48,6 +47,18 @@ Frozen counterpart constants (interoperability, not branding):
|
|
|
48
47
|
These are wire-compatible identifiers Zeta must keep, listed here so that a
|
|
49
48
|
brand sweep does not break them.
|
|
50
49
|
|
|
50
|
+
SHEETJS COMMUNITY EDITION 0.20.3
|
|
51
|
+
================================
|
|
52
|
+
|
|
53
|
+
Copyright (C) 2012-present SheetJS LLC. Licensed under Apache-2.0; the full
|
|
54
|
+
Apache License 2.0 is reproduced in this aggregate below. The unchanged official
|
|
55
|
+
distribution, including its LICENSE and source notices, is retained at
|
|
56
|
+
web-app/vendor/xlsx-0.20.3.tgz. Source: https://git.sheetjs.com/sheetjs/sheetjs
|
|
57
|
+
Official archive: https://cdn.sheetjs.com/xlsx-0.20.3/xlsx-0.20.3.tgz
|
|
58
|
+
SHA-512: oLDq3jw7AcLqKWH2AhCpVTZl8mf6X2YReP+Neh0SJUzV/BdZYjth94tG5toiMB1PPrYtxOCfaoUCkvtuH+3AJA==
|
|
59
|
+
Zeta changes dependency resolution to this local archive only; parser bytes
|
|
60
|
+
and the upstream license are unchanged (ADR-0059).
|
|
61
|
+
|
|
51
62
|
PYTHON DEPENDENCY DISTRIBUTION ROLE
|
|
52
63
|
===================================
|
|
53
64
|
|
|
@@ -432,7 +443,7 @@ SOFTWARE.
|
|
|
432
443
|
web-app/src/studio + web-app/src/vendor/* + web-ui/components/studio +
|
|
433
444
|
web-ui/styles/studio + web-ui/lib/studio-protocol
|
|
434
445
|
|
|
435
|
-
Ported UI surface and protocol types from OMP Studio 0.1.17
|
|
446
|
+
Ported UI surface and protocol types from OMP Studio (MIT): the original port was `0.1.17`; the current product renderer follows official `v0.2.1`, integrated 2026-11-16.
|
|
436
447
|
|
|
437
448
|
- `web-ui/components/studio/` and `web-ui/styles/studio/` are derived from the
|
|
438
449
|
OMP Studio renderer (`apps/renderer/src`).
|
|
@@ -5,7 +5,13 @@
|
|
|
5
5
|
import { type AssistantMessage, type Context, EventStream, type ToolResultMessage } from "@linxiraos/pi-ai";
|
|
6
6
|
import { type Dialect } from "@linxiraos/pi-ai/dialect";
|
|
7
7
|
import { type AgentRunCoverage, type AgentRunSummary } from "./run-collector.js";
|
|
8
|
-
import type { AgentContext, AgentEvent, AgentLoopConfig, AgentMessage, SteeringQueueState, StreamFn } from "./types.js";
|
|
8
|
+
import type { AgentContext, AgentEvent, AgentLoopConfig, AgentMessage, AgentTool, SteeringQueueState, StreamFn } from "./types.js";
|
|
9
|
+
/**
|
|
10
|
+
* A tool's batch concurrency for one call: an unset mode is shared, and a
|
|
11
|
+
* throwing per-call resolver is exclusive — the ordering that cannot race —
|
|
12
|
+
* rather than taking down the whole batch.
|
|
13
|
+
*/
|
|
14
|
+
export declare function resolveToolConcurrency(tool: AgentTool<any, any, any> | undefined, args: Record<string, unknown>): "shared" | "exclusive";
|
|
9
15
|
/** Stop-details marker for a provider error after assistant content/tool args already streamed. */
|
|
10
16
|
export declare const STREAM_INTERRUPTED_AFTER_CONTENT_STOP_DETAIL = "stream_interrupted_after_content";
|
|
11
17
|
/**
|
|
@@ -104,7 +104,7 @@ export declare function shouldUseProviderNativeCompaction(model: Model, settings
|
|
|
104
104
|
* components. Provider-side orchestration tokens are billable but never replay
|
|
105
105
|
* into the conversation prefix, so they are excluded from context sizing.
|
|
106
106
|
*/
|
|
107
|
-
export declare function calculateContextTokens(usage: Usage): number;
|
|
107
|
+
export declare function calculateContextTokens(usage: Pick<Usage, "input" | "output" | "cacheRead" | "cacheWrite" | "totalTokens" | "contextTokens" | "orchestration">): number;
|
|
108
108
|
export declare function calculatePromptTokens(usage: Usage): number;
|
|
109
109
|
export declare function hasContextTokenUsage(usage: Usage): boolean;
|
|
110
110
|
/**
|
|
@@ -416,7 +416,9 @@ export declare function findReadableCompactionIndex(pathEntries: SessionEntry[],
|
|
|
416
416
|
/**
|
|
417
417
|
* Pass the caller's warm `tokenizer` (the Agent's for the active model) so the
|
|
418
418
|
* full-branch estimate walk hits its memo; the cold default is for one-shot
|
|
419
|
-
* callers that have no live agent.
|
|
419
|
+
* callers that have no live agent. A cold walk over a long branch blocks the
|
|
420
|
+
* event loop for hundreds of milliseconds, so it runs as the
|
|
421
|
+
* `compaction.prepare` loop phase.
|
|
420
422
|
*/
|
|
421
423
|
export declare function prepareCompaction(pathEntries: SessionEntry[], settings: CompactionSettings, activeModel?: Model, tokenizer?: Tokenizer): CompactionPreparation | undefined;
|
|
422
424
|
/**
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ImageContent, MessageAttribution, ServiceTierByFamily, TextContent } from "@linxiraos/pi-ai";
|
|
1
|
+
import type { ImageContent, MessageAttribution, ServiceTierByFamily, StopReason, TextContent, Usage } from "@linxiraos/pi-ai";
|
|
2
2
|
import type { AgentMessage } from "../types.js";
|
|
3
3
|
export interface SessionEntryBase {
|
|
4
4
|
type: string;
|
|
@@ -115,7 +115,40 @@ export interface ResetBoundaryEntry extends SessionEntryBase {
|
|
|
115
115
|
}
|
|
116
116
|
export interface CustomCompactionSessionEntries {
|
|
117
117
|
}
|
|
118
|
-
|
|
118
|
+
/**
|
|
119
|
+
* Entries a coding-agent session can append that compaction already ignores.
|
|
120
|
+
* Declared here (rather than imported from the coding agent) so both packages
|
|
121
|
+
* agree on the durable transcript shape without a dependency cycle.
|
|
122
|
+
*/
|
|
123
|
+
export interface ImControlEntry extends SessionEntryBase {
|
|
124
|
+
type: "im_control";
|
|
125
|
+
metadata: unknown;
|
|
126
|
+
}
|
|
127
|
+
export interface ModelUsageEntry extends SessionEntryBase {
|
|
128
|
+
type: "model_usage";
|
|
129
|
+
purpose: string;
|
|
130
|
+
role?: string;
|
|
131
|
+
api: string;
|
|
132
|
+
provider: string;
|
|
133
|
+
model: string;
|
|
134
|
+
usage: Usage;
|
|
135
|
+
stopReason: StopReason;
|
|
136
|
+
errorMessage?: string;
|
|
137
|
+
cacheRefresh?: {
|
|
138
|
+
sentAt: number;
|
|
139
|
+
hit: boolean;
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
export interface SessionRunInitEntry extends SessionEntryBase {
|
|
143
|
+
type: "run_init";
|
|
144
|
+
client?: "cli" | "web" | "desktop" | "automation";
|
|
145
|
+
}
|
|
146
|
+
export interface CredentialPinEntry extends SessionEntryBase {
|
|
147
|
+
type: "credential_pin";
|
|
148
|
+
provider: string;
|
|
149
|
+
hash: string;
|
|
150
|
+
}
|
|
151
|
+
export type SessionEntry = SessionMessageEntry | ImControlEntry | ModelUsageEntry | ThinkingLevelChangeEntry | ModelChangeEntry | ServiceTierChangeEntry | CompactionEntry | BranchSummaryEntry | CustomEntry | CustomMessageEntry | LabelEntry | TitleChangeEntry | TtsrInjectionEntry | SessionInitEntry | SessionRunInitEntry | ModeChangeEntry | CredentialPinEntry | ResetBoundaryEntry | CustomCompactionSessionEntries[keyof CustomCompactionSessionEntries];
|
|
119
152
|
export interface ReadonlySessionManager {
|
|
120
153
|
getBranch(leafId?: string | null): SessionEntry[];
|
|
121
154
|
getEntry(id: string): SessionEntry | undefined;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { AgentMessage } from "./types.js";
|
|
2
|
+
/** Enumerable symbol survives immutable message spreads, never JSON/provider wire. */
|
|
3
|
+
export declare const AGENT_MESSAGE_IDENTITY: unique symbol;
|
|
4
|
+
export type IdentifiedAgentMessage = AgentMessage & {
|
|
5
|
+
[AGENT_MESSAGE_IDENTITY]?: object;
|
|
6
|
+
};
|
|
7
|
+
export declare function agentMessageIdentity(message: object): object | undefined;
|
|
8
|
+
export declare function identifyAgentMessage(message: object, identity?: object): object;
|
|
@@ -60,7 +60,7 @@ export declare class SpeculativeOperationCoordinator {
|
|
|
60
60
|
reconcileFinalCalls(calls: ReadonlyMap<string, AgentToolCall>): Promise<void>;
|
|
61
61
|
authorizeLaunch(context: SpeculativeLaunchContext): Promise<SpeculativeAuthorization>;
|
|
62
62
|
discardChildren(parentToolCallId: string, reason: string): Promise<void>;
|
|
63
|
-
claim(tool: AgentTool | undefined, toolCall: AgentToolCall, args: Record<string, unknown
|
|
63
|
+
claim(tool: AgentTool | undefined, toolCall: AgentToolCall, args: Record<string, unknown>, earlierEffectsSettled?: Promise<void>, signal?: AbortSignal): Promise<SpeculativeRawOutcome | undefined>;
|
|
64
64
|
admitFinalized(context: AgentContext, toolCall: AgentToolCall, loopConfig: AgentLoopConfig, signal: AbortSignal | undefined): void;
|
|
65
65
|
admit(definition: SpeculativeChildDefinition): Promise<SpeculativeChildHandle | undefined>;
|
|
66
66
|
}
|
|
@@ -482,7 +482,7 @@ export interface JudgmentTelemetryOptions {
|
|
|
482
482
|
readonly provider: string;
|
|
483
483
|
/** Requested model id. */
|
|
484
484
|
readonly model: string;
|
|
485
|
-
/** Model that answered, when the provider resolves an alias (`jev-latest` → `jev-1.
|
|
485
|
+
/** Model that answered, when the provider resolves an alias (`jev-latest` → `jev-1.14.0`). */
|
|
486
486
|
readonly responseModel?: string;
|
|
487
487
|
/** Caller-level reason the judgment ran; stamped as {@link OmpGenAIAttr.JudgmentPurpose}. */
|
|
488
488
|
readonly purpose: string;
|
package/dist/types/types.d.ts
CHANGED
|
@@ -605,6 +605,15 @@ export interface ToolCallContext {
|
|
|
605
605
|
}>;
|
|
606
606
|
/** Provider-native metadata for the current call, when present. */
|
|
607
607
|
providerMetadata?: ToolCallProviderMetadata;
|
|
608
|
+
/**
|
|
609
|
+
* Settles once every earlier call of this batch that runs alongside this one
|
|
610
|
+
* and may change files (its approval, asked with its arguments and no context,
|
|
611
|
+
* is not "read", e.g. `bash`) has
|
|
612
|
+
* finished; absent when there is none. The model sees the batch's results only
|
|
613
|
+
* after the whole batch, so a read-only tool can check its target again here
|
|
614
|
+
* and redo a read those calls changed instead of returning stale content.
|
|
615
|
+
*/
|
|
616
|
+
earlierEffectsSettled?: Promise<void>;
|
|
608
617
|
/**
|
|
609
618
|
* Cooperative steering signal: aborted when a queued user/steering message
|
|
610
619
|
* (or an interrupting peer IRC) is detected while this tool batch runs. Its
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"type": "module",
|
|
3
3
|
"name": "@linxiraos/pi-agent-core",
|
|
4
|
-
"version": "1.5.
|
|
4
|
+
"version": "1.5.11",
|
|
5
5
|
"description": "General-purpose agent with transport abstraction, state management, and attachment support",
|
|
6
6
|
"homepage": "https://linxira-os.github.io/zeta/",
|
|
7
7
|
"author": "Linxira-OS",
|
|
@@ -35,16 +35,16 @@
|
|
|
35
35
|
"fmt": "oxfmt --no-error-on-unmatched-pattern 'src/**/*.{ts,tsx}' '{test,bench,examples,scripts}/**/*.ts' '*.ts'"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@linxiraos/pi-ai": "1.5.
|
|
39
|
-
"@linxiraos/pi-catalog": "1.5.
|
|
40
|
-
"@linxiraos/pi-natives": "1.5.
|
|
41
|
-
"@linxiraos/pi-utils": "1.5.
|
|
42
|
-
"@linxiraos/pi-wire": "1.5.
|
|
43
|
-
"@linxiraos/zeta-compact": "1.5.
|
|
38
|
+
"@linxiraos/pi-ai": "1.5.11",
|
|
39
|
+
"@linxiraos/pi-catalog": "1.5.11",
|
|
40
|
+
"@linxiraos/pi-natives": "1.5.11",
|
|
41
|
+
"@linxiraos/pi-utils": "1.5.11",
|
|
42
|
+
"@linxiraos/pi-wire": "1.5.11",
|
|
43
|
+
"@linxiraos/zeta-compact": "1.5.11",
|
|
44
44
|
"@opentelemetry/api": "^1.9.1"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
|
-
"@linxiraos/zeta-schema": "1.5.
|
|
47
|
+
"@linxiraos/zeta-schema": "1.5.11",
|
|
48
48
|
"@opentelemetry/context-async-hooks": "2.9.0",
|
|
49
49
|
"@opentelemetry/sdk-trace-base": "2.9.0",
|
|
50
50
|
"@types/bun": "1.4.4"
|
package/src/agent-loop.ts
CHANGED
|
@@ -102,6 +102,7 @@ import type {
|
|
|
102
102
|
SteeringInterruptSource,
|
|
103
103
|
SteeringQueueState,
|
|
104
104
|
StreamFn,
|
|
105
|
+
ToolApprovalDecision,
|
|
105
106
|
} from "./types";
|
|
106
107
|
import {
|
|
107
108
|
ASIDE_MESSAGE_COMMIT,
|
|
@@ -110,6 +111,24 @@ import {
|
|
|
110
111
|
SPECULATIVE_STREAM_SESSION,
|
|
111
112
|
} from "./types";
|
|
112
113
|
|
|
114
|
+
/**
|
|
115
|
+
* A tool's batch concurrency for one call: an unset mode is shared, and a
|
|
116
|
+
* throwing per-call resolver is exclusive — the ordering that cannot race —
|
|
117
|
+
* rather than taking down the whole batch.
|
|
118
|
+
*/
|
|
119
|
+
export function resolveToolConcurrency(
|
|
120
|
+
tool: AgentTool<any, any, any> | undefined,
|
|
121
|
+
args: Record<string, unknown>,
|
|
122
|
+
): "shared" | "exclusive" {
|
|
123
|
+
const mode = tool?.concurrency;
|
|
124
|
+
if (typeof mode !== "function") return mode ?? "shared";
|
|
125
|
+
try {
|
|
126
|
+
return mode(args);
|
|
127
|
+
} catch {
|
|
128
|
+
return "exclusive";
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
113
132
|
async function waitForProcessPause(config: AgentLoopConfig, signal?: AbortSignal): Promise<void> {
|
|
114
133
|
try {
|
|
115
134
|
while (agentPauseGate.paused && !signal?.aborted) {
|
|
@@ -3550,7 +3569,11 @@ async function executeToolCalls(
|
|
|
3550
3569
|
flushResultMessages();
|
|
3551
3570
|
};
|
|
3552
3571
|
|
|
3553
|
-
const runTool = async (
|
|
3572
|
+
const runTool = async (
|
|
3573
|
+
record: (typeof records)[number],
|
|
3574
|
+
index: number,
|
|
3575
|
+
earlierEffectsSettled: Promise<void> | undefined,
|
|
3576
|
+
): Promise<void> => {
|
|
3554
3577
|
// A pending interrupt preempts not-yet-started interruptible tools,
|
|
3555
3578
|
// except receivers that drain the wake with an already-aborted signal.
|
|
3556
3579
|
// Non-interruptible work is never skipped, whatever the source: the
|
|
@@ -3659,7 +3682,7 @@ async function executeToolCalls(
|
|
|
3659
3682
|
if (abortBeforeToolExecution()) return;
|
|
3660
3683
|
|
|
3661
3684
|
const speculativeOutcome = speculationCoordinator
|
|
3662
|
-
? await speculationCoordinator.claim(tool, toolCall, executionArgs)
|
|
3685
|
+
? await speculationCoordinator.claim(tool, toolCall, executionArgs, earlierEffectsSettled, record.signal)
|
|
3663
3686
|
: undefined;
|
|
3664
3687
|
if (speculativeOutcome) {
|
|
3665
3688
|
// Normalize exactly like the ordinary execute path below: third-party
|
|
@@ -3688,6 +3711,7 @@ async function executeToolCalls(
|
|
|
3688
3711
|
toolCalls: toolCallInfos,
|
|
3689
3712
|
steeringSignal: steeringSoftController.signal,
|
|
3690
3713
|
providerMetadata: toolCall.providerMetadata,
|
|
3714
|
+
...(earlierEffectsSettled ? { earlierEffectsSettled } : {}),
|
|
3691
3715
|
addAdditionalContext: context => {
|
|
3692
3716
|
if (isNonBlankContext(context)) record.reportedContext.push(context);
|
|
3693
3717
|
},
|
|
@@ -3828,6 +3852,10 @@ async function executeToolCalls(
|
|
|
3828
3852
|
|
|
3829
3853
|
let lastExclusive: Promise<void> = Promise.resolve();
|
|
3830
3854
|
let sharedTasks: Promise<void>[] = [];
|
|
3855
|
+
// The shared calls among `sharedTasks` whose approval, asked with the call's
|
|
3856
|
+
// arguments, is not "read": they may change files that a later call running
|
|
3857
|
+
// beside them reads.
|
|
3858
|
+
let sharedEffects: Promise<void>[] = [];
|
|
3831
3859
|
const tasks: Promise<void>[] = [];
|
|
3832
3860
|
|
|
3833
3861
|
// While tool calls are in flight, queued steering or interrupting IRC would
|
|
@@ -3905,25 +3933,17 @@ async function executeToolCalls(
|
|
|
3905
3933
|
: undefined;
|
|
3906
3934
|
for (let index = 0; index < records.length; index++) {
|
|
3907
3935
|
const record = records[index];
|
|
3908
|
-
|
|
3909
|
-
|
|
3910
|
-
|
|
3911
|
-
// Resolved from the prepared (possibly hook-revised) args — raw args
|
|
3912
|
-
// only when validation failed, and those records error out before
|
|
3913
|
-
// executing. A throwing resolver must not take down the whole batch,
|
|
3914
|
-
// so fall back to the safe (serial) mode.
|
|
3915
|
-
try {
|
|
3916
|
-
concurrency = concurrencyMode(record.args);
|
|
3917
|
-
} catch {
|
|
3918
|
-
concurrency = "exclusive";
|
|
3919
|
-
}
|
|
3920
|
-
} else {
|
|
3921
|
-
concurrency = concurrencyMode ?? "shared";
|
|
3922
|
-
}
|
|
3936
|
+
// Resolved from the prepared (possibly hook-revised) args — raw args
|
|
3937
|
+
// only when validation failed, and those records error out before executing.
|
|
3938
|
+
const concurrency = resolveToolConcurrency(record.tool, record.args);
|
|
3923
3939
|
const start = concurrency === "exclusive" ? Promise.all([lastExclusive, ...sharedTasks]) : lastExclusive;
|
|
3940
|
+
const earlierEffectsSettled =
|
|
3941
|
+
concurrency === "shared" && sharedEffects.length > 0
|
|
3942
|
+
? Promise.allSettled(sharedEffects).then(() => undefined)
|
|
3943
|
+
: undefined;
|
|
3924
3944
|
const task = start.then(async () => {
|
|
3925
3945
|
try {
|
|
3926
|
-
await runTool(record, index);
|
|
3946
|
+
await runTool(record, index, earlierEffectsSettled);
|
|
3927
3947
|
} finally {
|
|
3928
3948
|
refreshPauseState();
|
|
3929
3949
|
}
|
|
@@ -3932,8 +3952,23 @@ async function executeToolCalls(
|
|
|
3932
3952
|
if (concurrency === "exclusive") {
|
|
3933
3953
|
lastExclusive = task;
|
|
3934
3954
|
sharedTasks = [];
|
|
3955
|
+
sharedEffects = [];
|
|
3935
3956
|
} else {
|
|
3936
3957
|
sharedTasks.push(task);
|
|
3958
|
+
// A dynamic approval is asked with the prepared arguments, as concurrency
|
|
3959
|
+
// is above, and no context; one that throws, or that says anything but
|
|
3960
|
+
// "read", counts as a call that may write. A resolver that says "write"
|
|
3961
|
+
// only with a context or only for transformed arguments is not counted, so
|
|
3962
|
+
// a read after it returns what it read first.
|
|
3963
|
+
const approval = record.tool?.approval;
|
|
3964
|
+
let decision: ToolApprovalDecision | undefined;
|
|
3965
|
+
try {
|
|
3966
|
+
decision = typeof approval === "function" ? approval(record.args) : approval;
|
|
3967
|
+
} catch {
|
|
3968
|
+
decision = undefined;
|
|
3969
|
+
}
|
|
3970
|
+
const tier = typeof decision === "string" ? decision : (decision?.tier ?? "exec");
|
|
3971
|
+
if (tier !== "read") sharedEffects.push(task);
|
|
3937
3972
|
}
|
|
3938
3973
|
}
|
|
3939
3974
|
try {
|
package/src/agent.ts
CHANGED
|
@@ -42,6 +42,7 @@ import type { AppendOnlyContextManager } from "./append-only-context";
|
|
|
42
42
|
import { agentPauseGate } from "./pause";
|
|
43
43
|
import { isProviderRefusalMessage } from "./replay-policy";
|
|
44
44
|
import { SentToolDefinitions } from "./sent-tool-definitions";
|
|
45
|
+
import { identifyAgentMessage } from "./message-identity";
|
|
45
46
|
import { Tokenizer } from "./tokenizer";
|
|
46
47
|
import {
|
|
47
48
|
createAdditionalContextMessage,
|
|
@@ -423,6 +424,7 @@ export class Agent {
|
|
|
423
424
|
#tokenizer = new Tokenizer(this.#state.model);
|
|
424
425
|
#listeners = new Set<(e: AgentEvent) => void>();
|
|
425
426
|
#queueListeners = new Set<() => void>();
|
|
427
|
+
#streamingAssistantIdentity: object | undefined;
|
|
426
428
|
#pauseListeners = new Set<(paused: boolean) => void>();
|
|
427
429
|
#abortController?: AbortController;
|
|
428
430
|
#convertToLlm: (messages: AgentMessage[]) => Message[] | Promise<Message[]>;
|
|
@@ -1985,7 +1987,11 @@ export class Agent {
|
|
|
1985
1987
|
try {
|
|
1986
1988
|
const updated = await transform(message);
|
|
1987
1989
|
if (updated) entry.toolResult = updated;
|
|
1988
|
-
} catch {
|
|
1990
|
+
} catch (error) {
|
|
1991
|
+
logger.warn("agent: cursor tool result transform failed", {
|
|
1992
|
+
error: error instanceof Error ? error.message : String(error),
|
|
1993
|
+
});
|
|
1994
|
+
}
|
|
1989
1995
|
})();
|
|
1990
1996
|
entry.pending = pending;
|
|
1991
1997
|
await pending;
|
|
@@ -2368,6 +2374,15 @@ export class Agent {
|
|
|
2368
2374
|
}
|
|
2369
2375
|
|
|
2370
2376
|
#emit(e: AgentEvent) {
|
|
2377
|
+
if (e.type === "message_start" || e.type === "message_update" || e.type === "message_end") {
|
|
2378
|
+
if (e.message.role === "assistant") {
|
|
2379
|
+
if (e.type === "message_start") this.#streamingAssistantIdentity = identifyAgentMessage(e.message);
|
|
2380
|
+
else identifyAgentMessage(e.message, this.#streamingAssistantIdentity);
|
|
2381
|
+
if (e.type === "message_end") this.#streamingAssistantIdentity = undefined;
|
|
2382
|
+
} else {
|
|
2383
|
+
identifyAgentMessage(e.message);
|
|
2384
|
+
}
|
|
2385
|
+
}
|
|
2371
2386
|
for (const listener of this.#listeners) {
|
|
2372
2387
|
try {
|
|
2373
2388
|
const result = listener(e) as unknown;
|
|
@@ -35,7 +35,7 @@ import { stripOpenAIResponsesOutputOnlyStatusesForReplay } from "@linxiraos/pi-a
|
|
|
35
35
|
import { resolveCatalogPolicy } from "@linxiraos/pi-catalog/compat/catalog-policy";
|
|
36
36
|
import { preferredDialect } from "@linxiraos/pi-catalog/identity";
|
|
37
37
|
import { clampThinkingLevelForModel } from "@linxiraos/pi-catalog/model-thinking";
|
|
38
|
-
import { isRecord, logger, prompt } from "@linxiraos/pi-utils";
|
|
38
|
+
import { isRecord, logger, prompt, withLoopPhase } from "@linxiraos/pi-utils";
|
|
39
39
|
import * as snapcompact from "@linxiraos/zeta-compact";
|
|
40
40
|
import { type AgentTelemetry, instrumentedCompleteSimple } from "../telemetry";
|
|
41
41
|
import { ThinkingLevel } from "../thinking";
|
|
@@ -262,7 +262,12 @@ export function shouldUseProviderNativeCompaction(
|
|
|
262
262
|
* components. Provider-side orchestration tokens are billable but never replay
|
|
263
263
|
* into the conversation prefix, so they are excluded from context sizing.
|
|
264
264
|
*/
|
|
265
|
-
export function calculateContextTokens(
|
|
265
|
+
export function calculateContextTokens(
|
|
266
|
+
usage: Pick<
|
|
267
|
+
Usage,
|
|
268
|
+
"input" | "output" | "cacheRead" | "cacheWrite" | "totalTokens" | "contextTokens" | "orchestration"
|
|
269
|
+
>,
|
|
270
|
+
): number {
|
|
266
271
|
if (usage.contextTokens !== undefined) {
|
|
267
272
|
return Math.max(0, usage.contextTokens);
|
|
268
273
|
}
|
|
@@ -1289,7 +1294,9 @@ async function generateShortSummary(
|
|
|
1289
1294
|
maxTokens,
|
|
1290
1295
|
signal,
|
|
1291
1296
|
apiKey,
|
|
1292
|
-
|
|
1297
|
+
// Display-only summary under a 512-token cap: reasoning would consume the
|
|
1298
|
+
// whole budget. The full summary keeps the session's effort.
|
|
1299
|
+
disableReasoning: true,
|
|
1293
1300
|
initiatorOverride: options?.initiatorOverride,
|
|
1294
1301
|
metadata: options?.metadata,
|
|
1295
1302
|
fetch: options?.fetch,
|
|
@@ -1468,13 +1475,26 @@ export function findReadableCompactionIndex(
|
|
|
1468
1475
|
/**
|
|
1469
1476
|
* Pass the caller's warm `tokenizer` (the Agent's for the active model) so the
|
|
1470
1477
|
* full-branch estimate walk hits its memo; the cold default is for one-shot
|
|
1471
|
-
* callers that have no live agent.
|
|
1478
|
+
* callers that have no live agent. A cold walk over a long branch blocks the
|
|
1479
|
+
* event loop for hundreds of milliseconds, so it runs as the
|
|
1480
|
+
* `compaction.prepare` loop phase.
|
|
1472
1481
|
*/
|
|
1473
1482
|
export function prepareCompaction(
|
|
1474
1483
|
pathEntries: SessionEntry[],
|
|
1475
1484
|
settings: CompactionSettings,
|
|
1476
1485
|
activeModel?: Model,
|
|
1477
1486
|
tokenizer: Tokenizer = new Tokenizer(activeModel),
|
|
1487
|
+
): CompactionPreparation | undefined {
|
|
1488
|
+
return withLoopPhase("compaction.prepare", () =>
|
|
1489
|
+
prepareCompactionUnlabeled(pathEntries, settings, activeModel, tokenizer),
|
|
1490
|
+
);
|
|
1491
|
+
}
|
|
1492
|
+
|
|
1493
|
+
function prepareCompactionUnlabeled(
|
|
1494
|
+
pathEntries: SessionEntry[],
|
|
1495
|
+
settings: CompactionSettings,
|
|
1496
|
+
activeModel: Model | undefined,
|
|
1497
|
+
tokenizer: Tokenizer,
|
|
1478
1498
|
): CompactionPreparation | undefined {
|
|
1479
1499
|
const lastEntry = pathEntries[pathEntries.length - 1];
|
|
1480
1500
|
// A speculative native record may leave uncovered messages before the record.
|
|
@@ -1,4 +1,11 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
ImageContent,
|
|
3
|
+
MessageAttribution,
|
|
4
|
+
ServiceTierByFamily,
|
|
5
|
+
StopReason,
|
|
6
|
+
TextContent,
|
|
7
|
+
Usage,
|
|
8
|
+
} from "@linxiraos/pi-ai";
|
|
2
9
|
import type { AgentMessage } from "../types";
|
|
3
10
|
|
|
4
11
|
export interface SessionEntryBase {
|
|
@@ -131,8 +138,44 @@ export interface ResetBoundaryEntry extends SessionEntryBase {
|
|
|
131
138
|
|
|
132
139
|
export interface CustomCompactionSessionEntries {}
|
|
133
140
|
|
|
141
|
+
/**
|
|
142
|
+
* Entries a coding-agent session can append that compaction already ignores.
|
|
143
|
+
* Declared here (rather than imported from the coding agent) so both packages
|
|
144
|
+
* agree on the durable transcript shape without a dependency cycle.
|
|
145
|
+
*/
|
|
146
|
+
export interface ImControlEntry extends SessionEntryBase {
|
|
147
|
+
type: "im_control";
|
|
148
|
+
metadata: unknown;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export interface ModelUsageEntry extends SessionEntryBase {
|
|
152
|
+
type: "model_usage";
|
|
153
|
+
purpose: string;
|
|
154
|
+
role?: string;
|
|
155
|
+
api: string;
|
|
156
|
+
provider: string;
|
|
157
|
+
model: string;
|
|
158
|
+
usage: Usage;
|
|
159
|
+
stopReason: StopReason;
|
|
160
|
+
errorMessage?: string;
|
|
161
|
+
cacheRefresh?: { sentAt: number; hit: boolean };
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export interface SessionRunInitEntry extends SessionEntryBase {
|
|
165
|
+
type: "run_init";
|
|
166
|
+
client?: "cli" | "web" | "desktop" | "automation";
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export interface CredentialPinEntry extends SessionEntryBase {
|
|
170
|
+
type: "credential_pin";
|
|
171
|
+
provider: string;
|
|
172
|
+
hash: string;
|
|
173
|
+
}
|
|
174
|
+
|
|
134
175
|
export type SessionEntry =
|
|
135
176
|
| SessionMessageEntry
|
|
177
|
+
| ImControlEntry
|
|
178
|
+
| ModelUsageEntry
|
|
136
179
|
| ThinkingLevelChangeEntry
|
|
137
180
|
| ModelChangeEntry
|
|
138
181
|
| ServiceTierChangeEntry
|
|
@@ -144,7 +187,9 @@ export type SessionEntry =
|
|
|
144
187
|
| TitleChangeEntry
|
|
145
188
|
| TtsrInjectionEntry
|
|
146
189
|
| SessionInitEntry
|
|
190
|
+
| SessionRunInitEntry
|
|
147
191
|
| ModeChangeEntry
|
|
192
|
+
| CredentialPinEntry
|
|
148
193
|
| ResetBoundaryEntry
|
|
149
194
|
| CustomCompactionSessionEntries[keyof CustomCompactionSessionEntries];
|
|
150
195
|
|
package/src/index.ts
CHANGED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { AgentMessage } from "./types";
|
|
2
|
+
|
|
3
|
+
/** Enumerable symbol survives immutable message spreads, never JSON/provider wire. */
|
|
4
|
+
export const AGENT_MESSAGE_IDENTITY = Symbol("agent-message-identity");
|
|
5
|
+
export type IdentifiedAgentMessage = AgentMessage & { [AGENT_MESSAGE_IDENTITY]?: object };
|
|
6
|
+
|
|
7
|
+
export function agentMessageIdentity(message: object): object | undefined {
|
|
8
|
+
return (message as IdentifiedAgentMessage)[AGENT_MESSAGE_IDENTITY];
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function identifyAgentMessage(message: object, identity?: object): object {
|
|
12
|
+
const identified = message as IdentifiedAgentMessage;
|
|
13
|
+
return (identified[AGENT_MESSAGE_IDENTITY] ??= identity ?? {});
|
|
14
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import * as path from "node:path";
|
|
2
2
|
import type { AssistantMessage } from "@linxiraos/pi-ai";
|
|
3
|
+
import { untilAborted } from "@linxiraos/pi-utils";
|
|
3
4
|
import { validateAgentToolArguments } from "./tool-arguments";
|
|
4
5
|
import type {
|
|
5
6
|
AgentContext,
|
|
@@ -617,9 +618,15 @@ export class SpeculativeOperationCoordinator {
|
|
|
617
618
|
tool: AgentTool | undefined,
|
|
618
619
|
toolCall: AgentToolCall,
|
|
619
620
|
args: Record<string, unknown>,
|
|
621
|
+
earlierEffectsSettled?: Promise<void>,
|
|
622
|
+
signal?: AbortSignal,
|
|
620
623
|
): Promise<SpeculativeRawOutcome | undefined> {
|
|
621
624
|
const candidate = this.#candidates.get(toolCall.id);
|
|
622
625
|
if (candidate?.source !== "direct") return undefined;
|
|
626
|
+
// Final assistant order can differ from admission order. Validate only
|
|
627
|
+
// after the earlier shared writers have exposed their final effects.
|
|
628
|
+
if (earlierEffectsSettled) await untilAborted(signal, earlierEffectsSettled);
|
|
629
|
+
signal?.throwIfAborted();
|
|
623
630
|
return this.#commitCandidate(candidate, tool, toolCall, args);
|
|
624
631
|
}
|
|
625
632
|
|
package/src/telemetry.ts
CHANGED
|
@@ -1687,7 +1687,7 @@ export interface JudgmentTelemetryOptions {
|
|
|
1687
1687
|
readonly provider: string;
|
|
1688
1688
|
/** Requested model id. */
|
|
1689
1689
|
readonly model: string;
|
|
1690
|
-
/** Model that answered, when the provider resolves an alias (`jev-latest` → `jev-1.
|
|
1690
|
+
/** Model that answered, when the provider resolves an alias (`jev-latest` → `jev-1.14.0`). */
|
|
1691
1691
|
readonly responseModel?: string;
|
|
1692
1692
|
/** Caller-level reason the judgment ran; stamped as {@link OmpGenAIAttr.JudgmentPurpose}. */
|
|
1693
1693
|
readonly purpose: string;
|
package/src/tokenizer.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { AssistantMessage, Model } from "@linxiraos/pi-ai";
|
|
1
|
+
import type { Api, AssistantMessage, Model } from "@linxiraos/pi-ai";
|
|
2
2
|
import { renderDemotedThinking } from "@linxiraos/pi-ai/dialect/demotion";
|
|
3
3
|
import { isFactoryDroidGeminiWire, keepsFactoryGeminiReasoning } from "@linxiraos/pi-ai/providers/factory-droid/gemini";
|
|
4
4
|
import { ollamaChatDropsOwnThinking } from "@linxiraos/pi-ai/providers/ollama";
|
|
@@ -51,21 +51,71 @@ type TokenizerModel = Pick<Model, "tokenizer"> &
|
|
|
51
51
|
requestModelId?: string;
|
|
52
52
|
};
|
|
53
53
|
|
|
54
|
-
/** The model
|
|
55
|
-
interface
|
|
56
|
-
api:
|
|
54
|
+
/** The api/provider/model a {@link Tokenizer} counts for. */
|
|
55
|
+
interface ModelIdentity {
|
|
56
|
+
api: Api;
|
|
57
57
|
provider: string;
|
|
58
58
|
id: string;
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
-
function
|
|
61
|
+
function modelIdentity(model: TokenizerModel | null | undefined): ModelIdentity | undefined {
|
|
62
62
|
if (!model?.api || !model.provider || !model.id) return undefined;
|
|
63
|
-
// Filtering targets strip native reasoning items before the request is encoded.
|
|
64
|
-
const compat = model.compat;
|
|
65
|
-
if (compat && "filterReasoningHistory" in compat && compat.filterReasoningHistory) return undefined;
|
|
66
63
|
return { api: model.api, provider: model.provider, id: model.id };
|
|
67
64
|
}
|
|
68
65
|
|
|
66
|
+
/**
|
|
67
|
+
* Whether the reader's resolved compat strips native `type: "reasoning"` items
|
|
68
|
+
* from replayed Responses history (`Model.compat.filterReasoningHistory`, a
|
|
69
|
+
* resolved boolean the request builders read too).
|
|
70
|
+
*/
|
|
71
|
+
function filtersReasoningHistory(model: TokenizerModel | null | undefined): boolean {
|
|
72
|
+
const compat = model?.compat;
|
|
73
|
+
return compat !== undefined && "filterReasoningHistory" in compat && compat.filterReasoningHistory === true;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Which of a filtered reader's turns its encoder rebuilds thinking items for; `null` for none. */
|
|
77
|
+
type RebuiltThinkingTurns = "all-turns" | "tool-calls";
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Whether the reader's Responses encoder rebuilds a filtered turn's own thinking
|
|
81
|
+
* into a synthetic reasoning item. `filterReasoningHistory` drops the native
|
|
82
|
+
* item, but the `requiresReasoningContent*` safety net puts the turn's thinking
|
|
83
|
+
* text back on the wire as the rebuilt item's `reasoning_text` — unless the
|
|
84
|
+
* target rejects synthetic items (`allowsSyntheticReasoningContentForToolCalls`),
|
|
85
|
+
* in which case reconstruction is off and the text is dropped with the item.
|
|
86
|
+
* Mirrors `buildParams`' `canReconstructReasoningReplay` and
|
|
87
|
+
* `convertResponsesAssistantMessage`' `carriedReasoningTexts`.
|
|
88
|
+
*/
|
|
89
|
+
function rebuiltThinkingTurns(model: TokenizerModel | null | undefined): RebuiltThinkingTurns | null {
|
|
90
|
+
const compat = model?.compat;
|
|
91
|
+
// `buildParams` gates both reconstructions on a requested reasoning effort,
|
|
92
|
+
// which a per-message count cannot see; a model that cannot reason never
|
|
93
|
+
// reconstructs, and one that can counts the text it may send.
|
|
94
|
+
if (model?.reasoning !== true || !filtersReasoningHistory(model) || compat === undefined) return null;
|
|
95
|
+
if (
|
|
96
|
+
!("allowsSyntheticReasoningContentForToolCalls" in compat) ||
|
|
97
|
+
compat.allowsSyntheticReasoningContentForToolCalls !== true
|
|
98
|
+
) {
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
if (
|
|
102
|
+
"requiresReasoningContentForAllAssistantTurns" in compat &&
|
|
103
|
+
compat.requiresReasoningContentForAllAssistantTurns === true
|
|
104
|
+
) {
|
|
105
|
+
return "all-turns";
|
|
106
|
+
}
|
|
107
|
+
if ("requiresReasoningContentForToolCalls" in compat && compat.requiresReasoningContentForToolCalls === true) {
|
|
108
|
+
return "tool-calls";
|
|
109
|
+
}
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** The model an assistant turn's native reasoning (signed thinking, Responses history) is replayed to. */
|
|
114
|
+
function reasoningReplayReader(model: TokenizerModel | null | undefined): ModelIdentity | undefined {
|
|
115
|
+
// Filtering targets strip native reasoning items before the request is encoded.
|
|
116
|
+
return filtersReasoningHistory(model) ? undefined : modelIdentity(model);
|
|
117
|
+
}
|
|
118
|
+
|
|
69
119
|
/** Identity of the reader whose replayed reasoning a {@link Tokenizer} counts; equal keys count it alike. */
|
|
70
120
|
export function reasoningReplayKey(model: TokenizerModel | null | undefined): string {
|
|
71
121
|
const reader = reasoningReplayReader(model);
|
|
@@ -218,7 +268,13 @@ export class Tokenizer {
|
|
|
218
268
|
readonly #encoding: natives.Encoding | null;
|
|
219
269
|
readonly #frameBilling: snapcompact.FrameBilling;
|
|
220
270
|
readonly frameBillingKey: string;
|
|
221
|
-
readonly #reasoningReader:
|
|
271
|
+
readonly #reasoningReader: ModelIdentity | undefined;
|
|
272
|
+
/** The counted model's identity, set even when filtering leaves no reasoning reader. */
|
|
273
|
+
readonly #model: ModelIdentity | undefined;
|
|
274
|
+
/** Whether the reader strips native reasoning items out of its Responses requests. */
|
|
275
|
+
readonly #filtersReasoningHistory: boolean;
|
|
276
|
+
/** Which turns the reader's encoder rebuilds a stripped reasoning item for, carrying the thinking text. */
|
|
277
|
+
readonly #rebuiltThinkingTurns: RebuiltThinkingTurns | null;
|
|
222
278
|
readonly #dropsOllamaThinking: boolean;
|
|
223
279
|
readonly #dropsCompletionsThinking: boolean;
|
|
224
280
|
/** Claude's own API and prefix-bound hosts keep another model's signature from their deployment. */
|
|
@@ -260,6 +316,9 @@ export class Tokenizer {
|
|
|
260
316
|
this.#frameBilling = snapcompact.frameBilling(model ?? undefined);
|
|
261
317
|
this.frameBillingKey = snapcompact.frameBillingKey(this.#frameBilling);
|
|
262
318
|
this.#reasoningReader = reasoningReplayReader(model);
|
|
319
|
+
this.#model = modelIdentity(model);
|
|
320
|
+
this.#filtersReasoningHistory = filtersReasoningHistory(model);
|
|
321
|
+
this.#rebuiltThinkingTurns = rebuiltThinkingTurns(model);
|
|
263
322
|
this.#dropsOllamaThinking = ollamaChatDropsOwnThinking(model);
|
|
264
323
|
this.#dropsCompletionsThinking = completionsDropsOwnThinking(model);
|
|
265
324
|
const compat = model?.compat;
|
|
@@ -474,12 +533,35 @@ export class Tokenizer {
|
|
|
474
533
|
message.stopReason !== "toolUse" &&
|
|
475
534
|
content.some(block => block.type === "toolCall");
|
|
476
535
|
const untrustedIndex = invalidStop ? content.length - 1 : -1;
|
|
536
|
+
// A filtering Responses reader replays no reasoning item and its encoder
|
|
537
|
+
// drops the turn's own thinking blocks, so usually neither their text nor
|
|
538
|
+
// their signature reaches the wire (the reconstruction below is the one
|
|
539
|
+
// exception). Another model's thinking has no native slot there either;
|
|
540
|
+
// the transform demotes it to visible text, which is sent.
|
|
541
|
+
const ownThinkingFiltered =
|
|
542
|
+
this.#filtersReasoningHistory && this.#model !== undefined && isSameModelTurn(message, this.#model);
|
|
543
|
+
// A filtering reader drops the turn's own reasoning item, but its
|
|
544
|
+
// encoder rebuilds one — carrying this turn's thinking text — for
|
|
545
|
+
// targets that require reasoning content and accept a synthetic item.
|
|
546
|
+
// The item is only attached to a turn that emits another item.
|
|
547
|
+
const ownThinkingRebuilt =
|
|
548
|
+
ownThinkingFiltered &&
|
|
549
|
+
this.#rebuiltThinkingTurns !== null &&
|
|
550
|
+
message.stopReason !== "error" &&
|
|
551
|
+
(this.#rebuiltThinkingTurns === "all-turns" || content.some(block => block.type === "toolCall")) &&
|
|
552
|
+
content.some(block => block.type === "text" || block.type === "toolCall");
|
|
477
553
|
let signedReasoning = false;
|
|
478
554
|
for (let index = 0; index < content.length; index++) {
|
|
479
555
|
const block = content[index];
|
|
480
556
|
if (block.type === "text") {
|
|
481
557
|
fragments.push(block.text);
|
|
482
558
|
} else if (block.type === "thinking") {
|
|
559
|
+
if (ownThinkingFiltered) {
|
|
560
|
+
// The rebuilt item carries the thinking text verbatim; the
|
|
561
|
+
// signature stays off the wire either way.
|
|
562
|
+
if (ownThinkingRebuilt) fragments.push(block.thinking);
|
|
563
|
+
continue;
|
|
564
|
+
}
|
|
483
565
|
// `transformMessages` drops another model's empty thinking and demotes the
|
|
484
566
|
// rest to visible text; only the reader's own thinking stays native.
|
|
485
567
|
if (ollamaReader && !ownOllamaTurn) {
|
|
@@ -489,6 +571,11 @@ export class Tokenizer {
|
|
|
489
571
|
if (this.#dropsOllamaThinking && ownOllamaTurn) continue;
|
|
490
572
|
// Chat-completions drops it unless LiteLLM replays it in `thinking_blocks`.
|
|
491
573
|
if (ownDroppedCompletionsTurn && !isLiteLLMThinkingSignature(block.thinkingSignature)) continue;
|
|
574
|
+
if (this.#filtersReasoningHistory && this.#model !== undefined) {
|
|
575
|
+
// The demoted rendering is the visible text the request carries.
|
|
576
|
+
if (block.thinking.trim()) fragments.push(renderDemotedThinking(this.#model.id, block.thinking));
|
|
577
|
+
continue;
|
|
578
|
+
}
|
|
492
579
|
fragments.push(block.thinking);
|
|
493
580
|
// Providers charge for the opaque signature/reasoning payload that
|
|
494
581
|
// rides alongside the thinking text (OpenAI Responses encrypted
|
|
@@ -617,6 +704,8 @@ export class Tokenizer {
|
|
|
617
704
|
tokenizerEncodingForModel(model) === this.#encoding &&
|
|
618
705
|
snapcompact.frameBillingKey(snapcompact.frameBilling(model ?? undefined)) === this.frameBillingKey &&
|
|
619
706
|
reasoningReplayKey(model) === this.reasoningReplayKey &&
|
|
707
|
+
filtersReasoningHistory(model) === this.#filtersReasoningHistory &&
|
|
708
|
+
rebuiltThinkingTurns(model) === this.#rebuiltThinkingTurns &&
|
|
620
709
|
resolveThinkingReplayTokens(model ?? {}) === this.thinkingReplayTokens &&
|
|
621
710
|
ollamaChatDropsOwnThinking(model) === this.#dropsOllamaThinking &&
|
|
622
711
|
completionsDropsOwnThinking(model) === this.#dropsCompletionsThinking &&
|
package/src/types.ts
CHANGED
|
@@ -691,6 +691,15 @@ export interface ToolCallContext {
|
|
|
691
691
|
toolCalls: Array<{ id: string; name: string }>;
|
|
692
692
|
/** Provider-native metadata for the current call, when present. */
|
|
693
693
|
providerMetadata?: ToolCallProviderMetadata;
|
|
694
|
+
/**
|
|
695
|
+
* Settles once every earlier call of this batch that runs alongside this one
|
|
696
|
+
* and may change files (its approval, asked with its arguments and no context,
|
|
697
|
+
* is not "read", e.g. `bash`) has
|
|
698
|
+
* finished; absent when there is none. The model sees the batch's results only
|
|
699
|
+
* after the whole batch, so a read-only tool can check its target again here
|
|
700
|
+
* and redo a read those calls changed instead of returning stale content.
|
|
701
|
+
*/
|
|
702
|
+
earlierEffectsSettled?: Promise<void>;
|
|
694
703
|
/**
|
|
695
704
|
* Cooperative steering signal: aborted when a queued user/steering message
|
|
696
705
|
* (or an interrupting peer IRC) is detected while this tool batch runs. Its
|