@oh-my-pi/pi-agent-core 15.12.4 → 15.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +61 -140
- package/package.json +6 -6
- package/src/agent-loop.ts +180 -2
- package/src/agent.ts +7 -2
package/CHANGELOG.md
CHANGED
|
@@ -2,111 +2,68 @@
|
|
|
2
2
|
|
|
3
3
|
## [Unreleased]
|
|
4
4
|
|
|
5
|
-
## [15.12.4] - 2026-06-13
|
|
6
|
-
### Fixed
|
|
7
|
-
|
|
8
|
-
- Fixed remote compaction input trimming to use unlimited context when `model.contextWindow` is unset
|
|
9
|
-
|
|
10
|
-
## [15.12.1] - 2026-06-12
|
|
11
5
|
### Breaking Changes
|
|
12
6
|
|
|
13
7
|
- Changed `pruneSupersededToolResults` to allow `supersedeKey` to be omitted so useless-result pruning can run without read-style supersede grouping
|
|
8
|
+
- Removed `compaction/index.ts` re-export of snapcompact helpers, so snapcompact utilities are no longer available from the agent compaction barrel and should be imported from `@oh-my-pi/snapcompact`
|
|
9
|
+
- Removed the `convertToLlm` alias export from `compaction/messages` — it duplicated `defaultConvertToLlm` under a second name. Import `defaultConvertToLlm` (array form) or the new `convertMessageToLlm` (single-message form) instead
|
|
14
10
|
|
|
15
11
|
### Added
|
|
16
12
|
|
|
13
|
+
- Added repetition-loop detection to the streaming agent loop for Gemini-family providers. A runaway run of a repeated text or thinking unit is detected mid-stream from a bounded rolling tail (O(1) per delta), the provider request is aborted, the repeated tail is collapsed to a single representative copy, and the turn ends gracefully with an `error` stop reason. Legitimate all-numeric/whitespace/punctuation runs (hexdumps, zero-fills, numeric tables) are not misclassified as loops ([#2549](https://github.com/can1357/oh-my-pi/pull/2549) by [@usr-bin-roygbiv](https://github.com/usr-bin-roygbiv)).
|
|
17
14
|
- Added `pruneUseless` controls to `PruneConfig` and `SupersedePruneConfig` so callers can toggle compaction of `toolResult` entries marked `useless`
|
|
18
15
|
- Added the ability to disable useless-result pruning by setting `pruneUseless` to false
|
|
19
16
|
- Tools can flag a result contextually useless (`AgentToolResult.useless`; overridable via `AfterToolCallResult.useless`): the agent loop copies the flag onto the persisted `ToolResultMessage` (errors always win), and compaction consumes it — the cache-aware supersede pass and the threshold prune blank flagged results to the exact `USELESS_NOTICE` placeholder (bypassing the protect window, skipping results smaller than the notice), shake collects them inside the protect-recent window, and `serializeConversation` drops the whole tool call/result pair from summarizer input
|
|
20
|
-
|
|
21
|
-
### Changed
|
|
22
|
-
|
|
23
|
-
- Changed `pruneSupersededToolResults` to allow omitted `supersedeKey` when `pruneUseless` is enabled, so useless-result pruning can run without read-style supersede grouping
|
|
24
|
-
|
|
25
|
-
## [15.11.4] - 2026-06-12
|
|
26
|
-
### Added
|
|
27
|
-
|
|
28
17
|
- Added `hasSteeringMessages` to `AgentLoopConfig` (wired by `Agent` to its steering queue): a peek used by the immediate-interrupt poll during tool execution, so the loop can detect queued steering without dequeuing and the queue keeps owning its messages until the injection boundary
|
|
29
18
|
- The agent loop now re-samples after a non-terminal stop (`stopReason: "stop"` with `stopDetails: { type: "pause_turn" }`, emitted by the Codex providers for `end_turn: false` commentary-only responses): the assistant message is committed to history and the model is called again without ending the turn. Consecutive pause continuations without an intervening tool call are capped at 8 to bound a backend that never stops pausing.
|
|
30
|
-
|
|
31
|
-
### Changed
|
|
32
|
-
|
|
33
|
-
- Changed steering handling so queued steering messages are now dequeued only at injection boundaries, with immediate mid-batch interrupt polling using `hasSteeringMessages`. Consumers constructing `AgentLoopConfig` directly with only `getSteeringMessages` no longer get mid-batch interrupts — steering degrades to boundary-only delivery until they also supply `hasSteeringMessages`
|
|
34
|
-
- Compaction, handoff, short-summary, and branch-summarization helpers now accept an `ApiKey` (static string or resolver) instead of a pre-resolved string, so a 401 mid-compaction force-refreshes and rotates the credential through the central auth-retry policy before any model-level fallback. The remote OpenAI compaction request is wrapped in `withAuth` and its HTTP failures now carry `.status`, so the retry classifier actually fires on remote-compaction 401s.
|
|
35
|
-
- `transformProviderContext` now receives the dispatch model as a second argument (`(context, model) => Context`), so per-request transforms can gate on model capabilities (vision input, provider, API family). Existing single-argument implementations keep working unchanged.
|
|
36
|
-
- Remote-compaction and summarization failures now throw pi-ai's typed `ProviderHttpError` instead of mutating plain `Error`s with a `.status` property; the generic `requestRemoteCompaction` error now carries `.status` (and response headers) too.
|
|
37
|
-
|
|
38
|
-
### Fixed
|
|
39
|
-
|
|
40
|
-
- Fixed a regression where steering messages could be injected into history during an aborted in-flight tool batch, leaving them hidden from queue consumers for post-abort continue
|
|
41
|
-
|
|
42
|
-
## [15.11.2] - 2026-06-11
|
|
43
|
-
|
|
44
|
-
### Added
|
|
45
|
-
|
|
46
19
|
- `AgentTool.concurrency` now also accepts a per-call resolver function `(args) => "shared" | "exclusive"`, letting tools pick the scheduling mode from the call's arguments (a throwing resolver falls back to `"exclusive"`)
|
|
47
|
-
|
|
48
|
-
### Fixed
|
|
49
|
-
|
|
50
|
-
- Fixed whitespace-only error tool results so Anthropic requests no longer 400 with `tool_result: content cannot be empty if is_error is true` and wedge the session on every subsequent turn
|
|
51
|
-
## [15.11.0] - 2026-06-10
|
|
52
|
-
### Breaking Changes
|
|
53
|
-
|
|
54
|
-
- Removed `compaction/index.ts` re-export of snapcompact helpers, so snapcompact utilities are no longer available from the agent compaction barrel and should be imported from `@oh-my-pi/snapcompact`
|
|
55
|
-
- Removed the `convertToLlm` alias export from `compaction/messages` — it duplicated `defaultConvertToLlm` under a second name. Import `defaultConvertToLlm` (array form) or the new `convertMessageToLlm` (single-message form) instead
|
|
56
|
-
|
|
57
|
-
### Added
|
|
58
|
-
|
|
59
20
|
- Added `convertMessageToLlm()`: the single-message core transformer behind `defaultConvertToLlm()`. Embedders with app-specific message roles should handle their own roles and delegate every core role (`user`/`developer`/`assistant`/`toolResult`/`custom`/`hookMessage`/`branchSummary`/`compactionSummary`) to it instead of duplicating the conversion — a duplicated `compactionSummary` case is how snapcompact frames once silently dropped off provider requests
|
|
60
21
|
- Added `pruneSupersededToolResults()` and the opt-in `PruneConfig.supersedeKey` hook so harnesses can prune stale tool results superseded by a newer read of the same file; superseded results are pruned ahead of age-based victims during overflow pruning and replaced with a `[Superseded by a newer read of this file]` placeholder. Without the new config, `pruneToolOutputs()` behavior is unchanged.
|
|
61
22
|
- Added `readToolSupersedeKey()` implementing the read-tool path/selector grammar (selector-free reads supersede range reads of the same file; URL-scheme paths exempt). Pruning honors prompt-cache economics: per-turn prunes only fire when the post-candidate suffix is small or the cache is cold (idle gap).
|
|
62
23
|
- Added the `snapcompact` compaction strategy via `@oh-my-pi/snapcompact`: instead of an LLM summary, discarded history is printed onto dense bitmap frames and re-attached to the compaction summary message as image blocks. `CompactionSummaryMessage` gains an optional `images` field, `estimateTokens()` charges per attached frame, and frames persist under `preserveData.snapcompact` with an 8-frame middle-out eviction budget.
|
|
63
24
|
- Snapcompact frames are now rendered in a provider-aware shape (`SNAPCOMPACT_SHAPES` + `resolveSnapcompactShape(api)`), following the snapcompact 200k-token monolithic evals: Anthropic-family and unknown APIs get `8x8r-bw` (unscii-8 square cells, black ink, every line printed twice with the copy on a pale highlight band — read at F1 parity with raw text at ~2x lower cost and the most refusal-robust), Google gets `8x8r-sent` (sentence-hue ink, ~2.9x cheaper), and OpenAI gets `6x6u-sent` (unscii Lanczos-stretched to 6x6 cells — OpenAI bills a flat ~2.9k tokens per image, so frame count is the only cost lever) with `detail: "original"` on the frame images. `snapcompactCompact()` accepts `model`/`shape` options, frames persist their shape metadata, mixed-shape archives (provider switches, legacy 5x8 frames) are flagged in the reading instructions, and `snapcompactGeometry()`/`renderSnapcompactFrame()` now take a shape
|
|
64
|
-
|
|
65
|
-
### Changed
|
|
66
|
-
|
|
67
|
-
- Compaction and branch-summary file lists are now a single `<files>` tag instead of `<read-files>`/`<modified-files>`: paths render as the grouped, prefix-folded directory tree the find/search tools emit (`# dir/` headers, bare basenames), each annotated `(Read)`, `(Write)`, or `(RW)` — modified files that were also read get `(RW)`. Legacy tags in summaries written by earlier versions are still stripped and self-heal on the next compaction
|
|
68
|
-
|
|
69
|
-
### Fixed
|
|
70
|
-
|
|
71
|
-
- Fixed queued steering messages being drained into an externally aborted run: interrupting mid-tool execution (e.g. Enter with a pending steer) dequeued the steer into the dying run — it landed in history without a response and the post-abort resume saw an empty queue, so the agent stopped instead of continuing. Steering/follow-up/aside queue polls are now skipped once the run's abort signal fires, leaving the queue intact for `Agent.continue()`.
|
|
72
|
-
- Fixed `<read-files>` compaction lists recording the same file once per line-range/raw selector (`src/foo.ts:50-200`, `:raw`, `:1-50:raw`, …): read-tool selectors are now stripped before tracking, so reads dedupe to the base path and match their write/edit path when splitting read-only vs modified lists. Selector-polluted lists stored by earlier compactions self-heal on the next compaction. `readToolSupersedeKey()` now shares the same splitter (`splitReadSelector()`), gaining the `..` range alias and `L`-prefix forms it previously missed.
|
|
73
|
-
- Fixed `estimateTokens()` undercounting thinking-heavy assistant messages on replay: `thinkingSignature` payloads (OpenAI Responses encrypted reasoning items, Anthropic signed thinking blocks, etc.) and `redactedThinking.data` are now charged alongside the visible thinking text, so the local estimate tracks provider-reported usage instead of straddling the threshold on every turn ([#2275](https://github.com/can1357/oh-my-pi/issues/2275)).
|
|
74
|
-
|
|
75
|
-
## [15.10.12] - 2026-06-10
|
|
76
|
-
|
|
77
|
-
### Added
|
|
78
|
-
|
|
79
25
|
- Added `AgentLoopConfig.getDisableReasoning` so callers can override `disableReasoning` per LLM call, mirroring `getReasoning`.
|
|
80
26
|
- Added `transformProviderContext` to `AgentOptions`/`AgentLoopConfig`: an optional hook applied to the assembled provider context after conversion, normalization, and append-only handling, but before telemetry capture and provider send.
|
|
81
|
-
|
|
82
|
-
### Fixed
|
|
83
|
-
|
|
84
|
-
- Fixed `Agent` runs so explicit reasoning disablement is forwarded to provider stream options and re-resolved per continuation, keeping mid-run thinking-off changes in sync with the next provider request.
|
|
85
|
-
|
|
86
|
-
## [15.10.11] - 2026-06-10
|
|
87
|
-
|
|
88
|
-
### Changed
|
|
89
|
-
|
|
90
|
-
- Editorial pass over the compaction prompts: fixed garbled grammar and missing articles, RFC-keyed prohibitions, deduped restated instructions; parsed markers (`<read-files>`/`<modified-files>`/`<previous-summary>`) and all output-format headings left byte-identical
|
|
91
|
-
- Catalog imports moved to the new `@oh-my-pi/pi-catalog` package: subpath imports (`calculateCost`, Codex wire constants) plus catalog values previously taken from the `@oh-my-pi/pi-ai` root (`getBundledModel`, `clampThinkingLevelForModel`), which pi-ai no longer re-exports; type-only `Model`/`Api`/`Effort` imports from pi-ai are unchanged
|
|
92
|
-
|
|
93
|
-
## [15.10.8] - 2026-06-09
|
|
94
|
-
|
|
95
|
-
### Added
|
|
96
|
-
|
|
97
27
|
- Added optional `fetch` overrides to `SummaryOptions` and `compact`/`generateSummary` so remote compaction can use custom HTTP clients
|
|
98
28
|
- Added optional `fetch` option to `ProxyStreamOptions` to control the HTTP request used by `streamProxy`
|
|
99
29
|
- Added optional `fetch` overrides to `requestOpenAiRemoteCompaction` and `requestRemoteCompaction` for injectable HTTP transport
|
|
100
30
|
- Added the upstream provider that served a request (`AssistantMessage.upstreamProvider`, e.g. OpenRouter's routed provider) as a `pi.gen_ai.response.upstream_provider` chat-span telemetry attribute, alongside the existing response id and time-to-first-chunk.
|
|
31
|
+
- Added a non-interrupting "aside" message channel to the agent loop (`AgentLoopConfig.getAsideMessages` / `Agent.setAsideMessageProvider`). Asides are drained at each step boundary (after a tool batch, before the next model call) and at the yield check, so passive notifications (e.g. background-job completions, late LSP diagnostics) reach the model *between requests* without waiting for the agent to stop and without aborting in-flight tools the way steering does.
|
|
32
|
+
- Added optional `promptCacheKey` support to `AgentOptions` and `Agent` via a new `promptCacheKey` property so providers can receive a caller-provided prompt cache key
|
|
33
|
+
- Added optional `ApiKeyResolveContext` parameter to `getApiKey` in `AgentOptions` and `AgentLoopConfig` so key resolvers can receive retry context
|
|
34
|
+
- Added `getReadToolPath(context)` to `@oh-my-pi/pi-agent-core/compaction/tool-protection` to extract a paired `read` tool call's `path` for embedders building read-targeted protection matchers
|
|
35
|
+
- Added `getReadToolPath(context)` to `@oh-my-pi/pi-agent-core/compaction/tool-protection`: the shared primitive that extracts a paired `read` tool call's `path` argument, so embedders can build their own read-targeted compaction protection matchers (e.g. plan-file reads) the same way `isSkillReadToolResult` does.
|
|
36
|
+
- Added optional `AgentTool.matcherDigest(args)` hook: tools whose streamed arguments encode content in a wire grammar (patch formats, escaped strings) can expose the real content they introduce, so stream-content matchers (e.g. TTSR rules) run against plain source text instead of the wire format.
|
|
37
|
+
- Added `shake` compaction primitives (`collectShakeRegions`, `applyShakeRegion`, `applyShakeRegions`, `summarizeShakeRegions`, `DEFAULT_SHAKE_CONFIG`, `AGGRESSIVE_SHAKE_CONFIG`, plus the `ShakeRegion`/`ShakeConfig`/`ShakeSummaryItem`/`ShakeSummaryComplete`/`ProtectedToolMatcher` types) under `@oh-my-pi/pi-agent-core/compaction`. These detect heavy context regions — whole tool-call results plus large fenced/XML blocks — and either elide them with placeholders or extractively compress them through an injected completion backend (no LLM summary cut-point). The compressor is provider-agnostic: callers wire it to a local on-device model. Pure detection/mutation; no I/O.
|
|
101
38
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
### Removed
|
|
39
|
+
### Changed
|
|
105
40
|
|
|
106
|
-
-
|
|
41
|
+
- Changed `pruneSupersededToolResults` to allow omitted `supersedeKey` when `pruneUseless` is enabled, so useless-result pruning can run without read-style supersede grouping
|
|
42
|
+
- Changed steering handling so queued steering messages are now dequeued only at injection boundaries, with immediate mid-batch interrupt polling using `hasSteeringMessages`. Consumers constructing `AgentLoopConfig` directly with only `getSteeringMessages` no longer get mid-batch interrupts — steering degrades to boundary-only delivery until they also supply `hasSteeringMessages`
|
|
43
|
+
- Compaction, handoff, short-summary, and branch-summarization helpers now accept an `ApiKey` (static string or resolver) instead of a pre-resolved string, so a 401 mid-compaction force-refreshes and rotates the credential through the central auth-retry policy before any model-level fallback. The remote OpenAI compaction request is wrapped in `withAuth` and its HTTP failures now carry `.status`, so the retry classifier actually fires on remote-compaction 401s.
|
|
44
|
+
- `transformProviderContext` now receives the dispatch model as a second argument (`(context, model) => Context`), so per-request transforms can gate on model capabilities (vision input, provider, API family). Existing single-argument implementations keep working unchanged.
|
|
45
|
+
- Remote-compaction and summarization failures now throw pi-ai's typed `ProviderHttpError` instead of mutating plain `Error`s with a `.status` property; the generic `requestRemoteCompaction` error now carries `.status` (and response headers) too.
|
|
46
|
+
- Compaction and branch-summary file lists are now a single `<files>` tag instead of `<read-files>`/`<modified-files>`: paths render as the grouped, prefix-folded directory tree the find/search tools emit (`# dir/` headers, bare basenames), each annotated `(Read)`, `(Write)`, or `(RW)` — modified files that were also read get `(RW)`. Legacy tags in summaries written by earlier versions are still stripped and self-heal on the next compaction
|
|
47
|
+
- Editorial pass over the compaction prompts: fixed garbled grammar and missing articles, RFC-keyed prohibitions, deduped restated instructions; parsed markers (`<read-files>`/`<modified-files>`/`<previous-summary>`) and all output-format headings left byte-identical
|
|
48
|
+
- Catalog imports moved to the new `@oh-my-pi/pi-catalog` package: subpath imports (`calculateCost`, Codex wire constants) plus catalog values previously taken from the `@oh-my-pi/pi-ai` root (`getBundledModel`, `clampThinkingLevelForModel`), which pi-ai no longer re-exports; type-only `Model`/`Api`/`Effort` imports from pi-ai are unchanged
|
|
49
|
+
- Changed core custom and hook messages to convert to `developer` messages for provider context.
|
|
50
|
+
- Enabled streaming API calls to re-resolve credentials through the `getApiKey` callback when retries occur after authentication-related errors
|
|
51
|
+
- `Agent.abort(reason?)` now forwards `reason` to the underlying `AbortController`, and the synthesized aborted assistant message carries that reason on `errorMessage` (string or non-`AbortError` `Error` message) instead of always defaulting to `"Request was aborted"`. Bare `abort()` is unchanged.
|
|
52
|
+
- Changed `Agent.appendMessage`, `popMessage`, `clearMessages`, and `reset` to mutate `state.messages` and `state.pendingToolCalls` in place instead of allocating a fresh array/Set on every transition. Subscribers that capture `state.messages` by reference now observe updates without needing to re-read `state` after each event. The public type signature is unchanged (always `AgentMessage[]` / `Set<string>`).
|
|
107
53
|
|
|
108
54
|
### Fixed
|
|
109
55
|
|
|
56
|
+
- Fixed repetition loop handling to collapse repeated `thinking` blocks to a single representative copy when a loop is detected
|
|
57
|
+
- Fixed repetition-loop detection to ignore repeats that contain only digits, whitespace, or punctuation so legitimate numeric outputs no longer stop with a repetition-loop error
|
|
58
|
+
- Fixed false-positive repetition-loop checks across `text` and `thinking` stream boundaries by tracking loop detection per block type
|
|
59
|
+
- Fixed dynamic forced tool choices from queue hooks being filtered against the active per-turn tool set before provider dispatch. ([#1701](https://github.com/can1357/oh-my-pi/issues/1701))
|
|
60
|
+
- Fixed remote compaction input trimming to use unlimited context when `model.contextWindow` is unset
|
|
61
|
+
- Fixed a regression where steering messages could be injected into history during an aborted in-flight tool batch, leaving them hidden from queue consumers for post-abort continue
|
|
62
|
+
- Fixed whitespace-only error tool results so Anthropic requests no longer 400 with `tool_result: content cannot be empty if is_error is true` and wedge the session on every subsequent turn
|
|
63
|
+
- Fixed queued steering messages being drained into an externally aborted run: interrupting mid-tool execution (e.g. Enter with a pending steer) dequeued the steer into the dying run — it landed in history without a response and the post-abort resume saw an empty queue, so the agent stopped instead of continuing. Steering/follow-up/aside queue polls are now skipped once the run's abort signal fires, leaving the queue intact for `Agent.continue()`.
|
|
64
|
+
- Fixed `<read-files>` compaction lists recording the same file once per line-range/raw selector (`src/foo.ts:50-200`, `:raw`, `:1-50:raw`, …): read-tool selectors are now stripped before tracking, so reads dedupe to the base path and match their write/edit path when splitting read-only vs modified lists. Selector-polluted lists stored by earlier compactions self-heal on the next compaction. `readToolSupersedeKey()` now shares the same splitter (`splitReadSelector()`), gaining the `..` range alias and `L`-prefix forms it previously missed.
|
|
65
|
+
- Fixed `estimateTokens()` undercounting thinking-heavy assistant messages on replay: `thinkingSignature` payloads (OpenAI Responses encrypted reasoning items, Anthropic signed thinking blocks, etc.) and `redactedThinking.data` are now charged alongside the visible thinking text, so the local estimate tracks provider-reported usage instead of straddling the threshold on every turn ([#2275](https://github.com/can1357/oh-my-pi/issues/2275)).
|
|
66
|
+
- Fixed `Agent` runs so explicit reasoning disablement is forwarded to provider stream options and re-resolved per continuation, keeping mid-run thinking-off changes in sync with the next provider request.
|
|
110
67
|
- Fixed stalled aborted assistant responses so the run now stops without waiting for provider iterator cleanup and returns the aborted message promptly
|
|
111
68
|
- Fixed `afterToolCall` handling so it now runs for completed tool executions even after a run is aborted so tool post-processing still applies
|
|
112
69
|
- Fixed `agentLoopDetailed().detailed()` so run telemetry and coverage are captured before `stream.result()` resolves.
|
|
@@ -117,96 +74,64 @@
|
|
|
117
74
|
- Fixed tool-call completion so assistant messages on abort keep only completed tool-call blocks and continue processing tool calls when a length stop still included results
|
|
118
75
|
- Fixed deliberate aborts (TTSR rule matches, user-interrupt labels) so a mid-stream tool-call block that never reached `toolcall_end` is retained on the aborted assistant message and paired with a placeholder result labeled by the abort reason, instead of being dropped; anonymous aborts (bare `abort()`) still drop incomplete tool calls whose partial arguments are unsafe to replay
|
|
119
76
|
- Fixed runs that stopped with reason `length` after returning tool results so execution continues to handle additional tool calls
|
|
120
|
-
|
|
121
|
-
## [15.10.3] - 2026-06-08
|
|
122
|
-
|
|
123
|
-
### Added
|
|
124
|
-
|
|
125
|
-
- Added a non-interrupting "aside" message channel to the agent loop (`AgentLoopConfig.getAsideMessages` / `Agent.setAsideMessageProvider`). Asides are drained at each step boundary (after a tool batch, before the next model call) and at the yield check, so passive notifications (e.g. background-job completions, late LSP diagnostics) reach the model *between requests* without waiting for the agent to stop and without aborting in-flight tools the way steering does.
|
|
126
|
-
|
|
127
|
-
### Changed
|
|
128
|
-
|
|
129
|
-
- Changed core custom and hook messages to convert to `developer` messages for provider context.
|
|
130
|
-
|
|
131
|
-
### Fixed
|
|
132
|
-
|
|
133
77
|
- Fixed the compaction spinner freezing (only repainting on a terminal resize) when compacting very large codex/OpenAI contexts. `buildOpenAiNativeHistory` re-collected the full known/custom tool-call id sets on every history-bearing message, rescanning the entire growing native history each time — O(N²) in history items — which blocked the event loop for seconds and starved the loader's animation timer and render scheduler. The sets are now maintained incrementally (linear), so building the compaction request no longer monopolizes the main thread.
|
|
78
|
+
- Fixed proxy stream silently returning a zero-token success response when the server disconnects without sending a `done` or `error` terminal SSE event. The stream now throws an error, surfacing the disconnect as an `error` event with `stopReason: "error"` and resolving `finalResultPromise`, instead of defaulting to `stopReason: "stop"` with empty content and leaving `stream.result()` callers hanging indefinitely.
|
|
79
|
+
- Fixed handling of short-lived API keys so that expired tokens are retried with a refreshed value during 401/usage-limit failures
|
|
80
|
+
- Ensured fallback API key resolution uses the initially configured static `apiKey` when `getApiKey` is present
|
|
81
|
+
- Wrapped oneshot LLM completions (`instrumentedCompleteSimple`: handoff, compaction/branch summaries) in an `EventLoopKeepalive`. These run outside the agent `#runLoop`, so without the keepalive Bun's event loop stopped servicing timers while parked on the completion promise — freezing host spinners (e.g. the `/handoff` loader) until an unrelated terminal resize poked the loop into rendering again.
|
|
82
|
+
- Surfaced Anthropic stream failures whose message starts with `Output blocked by conten` as normal assistant error lifecycle events, so interactive clients render content-filter blocks instead of silently dropping the streaming bubble at `agent_end`.
|
|
83
|
+
- Fixed the agent loop wedging the model when a `write`/`edit` tool call is truncated by `stop_reason: length` (e.g. an OpenCode Zen / Claude-3.5-Haiku turn that emits >~1000 lines of code, blowing past the 8K `max_tokens` output cap). The skipped tool result now surfaces an actionable hint — naming `stop_reason: length` and telling the model to split the payload into multiple smaller calls — instead of the generic "Tool call was not executed because the assistant ended its turn" placeholder, which left the auto-continue loop re-emitting the same oversized payload until the user gave up. Tools are still NOT executed when the arguments are truncated. ([#1785](https://github.com/can1357/oh-my-pi/issues/1785))
|
|
84
|
+
- Engaged GPT-5 Harmony leak detection on the committed assistant message (openai-codex only). `detectHarmonyLeakInAssistantMessage` now runs on the streamed `done`/`error` result and the trailing fallback, so a leaked final response is aborted-and-retried by the existing mitigation instead of being committed as-is. Tool-argument (`tool_arg`) scanning is gated on the trailing-garbage `T` co-signal and only fires when a caller supplies a parse boundary via `detectHarmonyLeakInAssistantMessage`'s new optional `toolArgParseEnd` resolver. The agent loop passes none — it cannot bound a streamed tool DSL — so that surface stays inert and a legitimate codex tool call whose content legitimately carries `to=functions.*` next to a channel word or non-Latin script (e.g. editing the harmony fixtures) is never hard-aborted.
|
|
85
|
+
- Fixed tool-output pruning and shake protection for `read`: ordinary file/URL reads are now eligible for compaction, while `read` calls whose `path` starts with `skill://` remain protected like native `skill` results.
|
|
134
86
|
|
|
135
87
|
### Removed
|
|
136
88
|
|
|
89
|
+
- Removed the `maxToolCallsPerTurn` option from `AgentOptions` and `AgentLoopConfig`, so assistant turns are no longer capped after a configured number of completed tool calls
|
|
137
90
|
- Removed the now-dead `<turn-aborted>` marker from the OpenAI compaction output user-message filter, since `transformMessages` no longer emits that note.
|
|
138
91
|
- Removed stale synthetic user-message tag filters from OpenAI remote compaction output preservation; developer messages are now dropped by role instead.
|
|
139
92
|
- Tool executions now receive the active turn `AbortSignal` unconditionally.
|
|
93
|
+
- Removed the local-model `summarizeShakeRegions` compressor and related shake-summary prompt/types; shake now only provides mechanical artifact-backed elision primitives.
|
|
140
94
|
|
|
141
|
-
## [15.
|
|
95
|
+
## [15.13.0] - 2026-06-14
|
|
142
96
|
|
|
143
|
-
|
|
97
|
+
## [15.12.6] - 2026-06-14
|
|
144
98
|
|
|
145
|
-
|
|
99
|
+
## [15.12.4] - 2026-06-13
|
|
146
100
|
|
|
147
|
-
## [15.
|
|
101
|
+
## [15.12.1] - 2026-06-12
|
|
148
102
|
|
|
149
|
-
|
|
103
|
+
## [15.11.4] - 2026-06-12
|
|
150
104
|
|
|
151
|
-
|
|
152
|
-
- Added optional `ApiKeyResolveContext` parameter to `getApiKey` in `AgentOptions` and `AgentLoopConfig` so key resolvers can receive retry context
|
|
105
|
+
## [15.11.2] - 2026-06-11
|
|
153
106
|
|
|
154
|
-
|
|
107
|
+
## [15.11.0] - 2026-06-10
|
|
155
108
|
|
|
156
|
-
|
|
157
|
-
- `Agent.abort(reason?)` now forwards `reason` to the underlying `AbortController`, and the synthesized aborted assistant message carries that reason on `errorMessage` (string or non-`AbortError` `Error` message) instead of always defaulting to `"Request was aborted"`. Bare `abort()` is unchanged.
|
|
109
|
+
## [15.10.12] - 2026-06-10
|
|
158
110
|
|
|
159
|
-
|
|
111
|
+
## [15.10.11] - 2026-06-10
|
|
160
112
|
|
|
161
|
-
|
|
162
|
-
- Ensured fallback API key resolution uses the initially configured static `apiKey` when `getApiKey` is present
|
|
163
|
-
- Wrapped oneshot LLM completions (`instrumentedCompleteSimple`: handoff, compaction/branch summaries) in an `EventLoopKeepalive`. These run outside the agent `#runLoop`, so without the keepalive Bun's event loop stopped servicing timers while parked on the completion promise — freezing host spinners (e.g. the `/handoff` loader) until an unrelated terminal resize poked the loop into rendering again.
|
|
113
|
+
## [15.10.8] - 2026-06-09
|
|
164
114
|
|
|
165
|
-
## [15.
|
|
115
|
+
## [15.10.5] - 2026-06-08
|
|
166
116
|
|
|
167
|
-
|
|
117
|
+
## [15.10.3] - 2026-06-08
|
|
168
118
|
|
|
169
|
-
|
|
119
|
+
## [15.10.2] - 2026-06-08
|
|
170
120
|
|
|
171
|
-
## [15.
|
|
121
|
+
## [15.10.1] - 2026-06-07
|
|
172
122
|
|
|
173
|
-
|
|
123
|
+
## [15.9.5] - 2026-06-05
|
|
174
124
|
|
|
175
|
-
|
|
176
|
-
- Added `getReadToolPath(context)` to `@oh-my-pi/pi-agent-core/compaction/tool-protection`: the shared primitive that extracts a paired `read` tool call's `path` argument, so embedders can build their own read-targeted compaction protection matchers (e.g. plan-file reads) the same way `isSkillReadToolResult` does.
|
|
125
|
+
## [15.8.3] - 2026-06-03
|
|
177
126
|
|
|
178
127
|
## [15.8.2] - 2026-06-03
|
|
179
128
|
|
|
180
|
-
### Added
|
|
181
|
-
|
|
182
|
-
- Added optional `AgentTool.matcherDigest(args)` hook: tools whose streamed arguments encode content in a wire grammar (patch formats, escaped strings) can expose the real content they introduce, so stream-content matchers (e.g. TTSR rules) run against plain source text instead of the wire format.
|
|
183
|
-
|
|
184
|
-
### Fixed
|
|
185
|
-
|
|
186
|
-
- Fixed the agent loop wedging the model when a `write`/`edit` tool call is truncated by `stop_reason: length` (e.g. an OpenCode Zen / Claude-3.5-Haiku turn that emits >~1000 lines of code, blowing past the 8K `max_tokens` output cap). The skipped tool result now surfaces an actionable hint — naming `stop_reason: length` and telling the model to split the payload into multiple smaller calls — instead of the generic "Tool call was not executed because the assistant ended its turn" placeholder, which left the auto-continue loop re-emitting the same oversized payload until the user gave up. Tools are still NOT executed when the arguments are truncated. ([#1785](https://github.com/can1357/oh-my-pi/issues/1785))
|
|
187
|
-
|
|
188
129
|
## [15.8.0] - 2026-06-02
|
|
189
130
|
|
|
190
|
-
### Fixed
|
|
191
|
-
|
|
192
|
-
- Engaged GPT-5 Harmony leak detection on the committed assistant message (openai-codex only). `detectHarmonyLeakInAssistantMessage` now runs on the streamed `done`/`error` result and the trailing fallback, so a leaked final response is aborted-and-retried by the existing mitigation instead of being committed as-is. Tool-argument (`tool_arg`) scanning is gated on the trailing-garbage `T` co-signal and only fires when a caller supplies a parse boundary via `detectHarmonyLeakInAssistantMessage`'s new optional `toolArgParseEnd` resolver. The agent loop passes none — it cannot bound a streamed tool DSL — so that surface stays inert and a legitimate codex tool call whose content legitimately carries `to=functions.*` next to a channel word or non-Latin script (e.g. editing the harmony fixtures) is never hard-aborted.
|
|
193
|
-
|
|
194
131
|
## [15.7.4] - 2026-05-31
|
|
195
132
|
|
|
196
|
-
### Removed
|
|
197
|
-
|
|
198
|
-
- Removed the local-model `summarizeShakeRegions` compressor and related shake-summary prompt/types; shake now only provides mechanical artifact-backed elision primitives.
|
|
199
|
-
|
|
200
133
|
## [15.7.3] - 2026-05-31
|
|
201
134
|
|
|
202
|
-
### Added
|
|
203
|
-
|
|
204
|
-
- Added `shake` compaction primitives (`collectShakeRegions`, `applyShakeRegion`, `applyShakeRegions`, `summarizeShakeRegions`, `DEFAULT_SHAKE_CONFIG`, `AGGRESSIVE_SHAKE_CONFIG`, plus the `ShakeRegion`/`ShakeConfig`/`ShakeSummaryItem`/`ShakeSummaryComplete`/`ProtectedToolMatcher` types) under `@oh-my-pi/pi-agent-core/compaction`. These detect heavy context regions — whole tool-call results plus large fenced/XML blocks — and either elide them with placeholders or extractively compress them through an injected completion backend (no LLM summary cut-point). The compressor is provider-agnostic: callers wire it to a local on-device model. Pure detection/mutation; no I/O.
|
|
205
|
-
|
|
206
|
-
### Fixed
|
|
207
|
-
|
|
208
|
-
- Fixed tool-output pruning and shake protection for `read`: ordinary file/URL reads are now eligible for compaction, while `read` calls whose `path` starts with `skill://` remain protected like native `skill` results.
|
|
209
|
-
|
|
210
135
|
## [15.5.15] - 2026-05-30
|
|
211
136
|
|
|
212
137
|
### Added
|
|
@@ -229,10 +154,6 @@
|
|
|
229
154
|
|
|
230
155
|
- Fixed compaction summarizer throws losing the provider's HTTP status. `generateSummary`, `generateHandoff`, `generateShortSummary`, and `generateTurnPrefixSummary` now route their `stopReason === "error"` throws through a `createSummarizationError` helper that copies `AssistantMessage.errorStatus` onto the thrown `Error` as `.status`, letting downstream consumers (e.g. `AgentSession.#isCompactionAuthFailure` in `@oh-my-pi/pi-coding-agent`) branch on real provider 401/403s without regex-scraping the message body.
|
|
231
156
|
|
|
232
|
-
### Changed
|
|
233
|
-
|
|
234
|
-
- Changed `Agent.appendMessage`, `popMessage`, `clearMessages`, and `reset` to mutate `state.messages` and `state.pendingToolCalls` in place instead of allocating a fresh array/Set on every transition. Subscribers that capture `state.messages` by reference now observe updates without needing to re-read `state` after each event. The public type signature is unchanged (always `AgentMessage[]` / `Set<string>`).
|
|
235
|
-
|
|
236
157
|
## [15.5.0] - 2026-05-26
|
|
237
158
|
|
|
238
159
|
### Added
|
|
@@ -734,4 +655,4 @@ Initial release under @oh-my-pi scope. See previous releases at [badlogic/pi-mon
|
|
|
734
655
|
|
|
735
656
|
- `Agent` constructor now has all options optional (empty options use defaults).
|
|
736
657
|
|
|
737
|
-
- `queueMessage()` is now synchronous (no longer returns a Promise).
|
|
658
|
+
- `queueMessage()` is now synchronous (no longer returns a Promise).
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"type": "module",
|
|
3
3
|
"name": "@oh-my-pi/pi-agent-core",
|
|
4
|
-
"version": "15.
|
|
4
|
+
"version": "15.13.0",
|
|
5
5
|
"description": "General-purpose agent with transport abstraction, state management, and attachment support",
|
|
6
6
|
"homepage": "https://omp.sh",
|
|
7
7
|
"author": "Can Boluk",
|
|
@@ -35,11 +35,11 @@
|
|
|
35
35
|
"fmt": "biome format --write ."
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@oh-my-pi/pi-ai": "15.
|
|
39
|
-
"@oh-my-pi/pi-catalog": "15.
|
|
40
|
-
"@oh-my-pi/pi-natives": "15.
|
|
41
|
-
"@oh-my-pi/pi-utils": "15.
|
|
42
|
-
"@oh-my-pi/snapcompact": "15.
|
|
38
|
+
"@oh-my-pi/pi-ai": "15.13.0",
|
|
39
|
+
"@oh-my-pi/pi-catalog": "15.13.0",
|
|
40
|
+
"@oh-my-pi/pi-natives": "15.13.0",
|
|
41
|
+
"@oh-my-pi/pi-utils": "15.13.0",
|
|
42
|
+
"@oh-my-pi/snapcompact": "15.13.0",
|
|
43
43
|
"@opentelemetry/api": "^1.9.1"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
package/src/agent-loop.ts
CHANGED
|
@@ -15,7 +15,7 @@ import {
|
|
|
15
15
|
validateToolArguments,
|
|
16
16
|
zodToWireSchema,
|
|
17
17
|
} from "@oh-my-pi/pi-ai";
|
|
18
|
-
import { sanitizeText } from "@oh-my-pi/pi-utils";
|
|
18
|
+
import { logger, sanitizeText } from "@oh-my-pi/pi-utils";
|
|
19
19
|
import {
|
|
20
20
|
createHarmonyAuditEvent,
|
|
21
21
|
detectHarmonyLeakInAssistantMessage,
|
|
@@ -708,6 +708,7 @@ async function runLoopBody(
|
|
|
708
708
|
});
|
|
709
709
|
}
|
|
710
710
|
stream.push({ type: "turn_end", message, toolResults });
|
|
711
|
+
|
|
711
712
|
stream.push(buildAgentEndEvent(newMessages, telemetry, stepCounter.count));
|
|
712
713
|
stream.end(newMessages);
|
|
713
714
|
return;
|
|
@@ -917,6 +918,10 @@ async function streamAssistantResponse(
|
|
|
917
918
|
? AbortSignal.any([signal, harmonyAbortController.signal])
|
|
918
919
|
: harmonyAbortController.signal
|
|
919
920
|
: signal;
|
|
921
|
+
const repetitionAbortController = new AbortController();
|
|
922
|
+
const finalRequestSignal = requestSignal
|
|
923
|
+
? AbortSignal.any([requestSignal, repetitionAbortController.signal])
|
|
924
|
+
: repetitionAbortController.signal;
|
|
920
925
|
const effectiveTemperature =
|
|
921
926
|
harmonyRetryAttempt > 0 && config.temperature !== undefined ? config.temperature + 0.05 : config.temperature;
|
|
922
927
|
const effectiveToolChoice = dynamicToolChoice ?? config.toolChoice;
|
|
@@ -984,7 +989,7 @@ async function streamAssistantResponse(
|
|
|
984
989
|
reasoning: effectiveReasoning,
|
|
985
990
|
disableReasoning: effectiveDisableReasoning,
|
|
986
991
|
temperature: effectiveTemperature,
|
|
987
|
-
signal:
|
|
992
|
+
signal: finalRequestSignal,
|
|
988
993
|
onResponse: captureOnResponse,
|
|
989
994
|
});
|
|
990
995
|
|
|
@@ -1013,6 +1018,56 @@ async function streamAssistantResponse(
|
|
|
1013
1018
|
return aborted;
|
|
1014
1019
|
};
|
|
1015
1020
|
|
|
1021
|
+
const finishRepetitionStream = async (
|
|
1022
|
+
kind: "text" | "thinking",
|
|
1023
|
+
pattern: string,
|
|
1024
|
+
count: number,
|
|
1025
|
+
): Promise<AssistantMessage> => {
|
|
1026
|
+
repetitionAbortController.abort();
|
|
1027
|
+
try {
|
|
1028
|
+
const cleanup = responseIterator.return?.();
|
|
1029
|
+
if (cleanup) void cleanup.catch(() => {});
|
|
1030
|
+
} catch {
|
|
1031
|
+
// ignore
|
|
1032
|
+
}
|
|
1033
|
+
if (partialMessage) {
|
|
1034
|
+
truncateRepetition(partialMessage, kind, pattern);
|
|
1035
|
+
partialMessage.stopReason = "error";
|
|
1036
|
+
partialMessage.errorMessage = `Repetition loop detected: assistant repeated "${pattern.trim()}" ${count} times consecutively.`;
|
|
1037
|
+
}
|
|
1038
|
+
const finalMsg = snapshotAssistantMessage(
|
|
1039
|
+
partialMessage ?? {
|
|
1040
|
+
role: "assistant",
|
|
1041
|
+
content: [],
|
|
1042
|
+
api: config.model.api,
|
|
1043
|
+
provider: config.model.provider,
|
|
1044
|
+
model: config.model.id,
|
|
1045
|
+
usage: {
|
|
1046
|
+
input: 0,
|
|
1047
|
+
output: 0,
|
|
1048
|
+
cacheRead: 0,
|
|
1049
|
+
cacheWrite: 0,
|
|
1050
|
+
totalTokens: 0,
|
|
1051
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
1052
|
+
},
|
|
1053
|
+
stopReason: "error",
|
|
1054
|
+
errorMessage: `Repetition loop detected.`,
|
|
1055
|
+
timestamp: Date.now(),
|
|
1056
|
+
},
|
|
1057
|
+
);
|
|
1058
|
+
if (addedPartial) {
|
|
1059
|
+
context.messages[context.messages.length - 1] = finalMsg;
|
|
1060
|
+
} else {
|
|
1061
|
+
context.messages.push(finalMsg);
|
|
1062
|
+
}
|
|
1063
|
+
if (!addedPartial) {
|
|
1064
|
+
stream.push({ type: "message_start", message: snapshotAssistantMessage(finalMsg) });
|
|
1065
|
+
}
|
|
1066
|
+
stream.push({ type: "message_end", message: snapshotAssistantMessage(finalMsg) });
|
|
1067
|
+
await finishChat(finalMsg);
|
|
1068
|
+
return finalMsg;
|
|
1069
|
+
};
|
|
1070
|
+
|
|
1016
1071
|
// Set up a single abort race: register the abort listener once for the whole
|
|
1017
1072
|
// stream and reuse the same race promise for every iterator.next() instead of
|
|
1018
1073
|
// allocating Promise.withResolvers and add/removeEventListener per event.
|
|
@@ -1029,6 +1084,14 @@ async function streamAssistantResponse(
|
|
|
1029
1084
|
detachAbortListener = () => requestSignal.removeEventListener("abort", onAbort);
|
|
1030
1085
|
}
|
|
1031
1086
|
|
|
1087
|
+
// Rolling tail of streamed text/thinking used for repetition-loop detection.
|
|
1088
|
+
// Bounded to REPETITION_WINDOW chars and reset when the active block kind
|
|
1089
|
+
// switches (text <-> thinking) so detection stays O(1) per delta and never
|
|
1090
|
+
// miscounts a repeated unit across a thinking/answer boundary.
|
|
1091
|
+
let repetitionTail = "";
|
|
1092
|
+
let repetitionKind: "text" | "thinking" | undefined;
|
|
1093
|
+
const isGeminiModel = config.model.provider.includes("google") || config.model.provider.includes("gemini");
|
|
1094
|
+
|
|
1032
1095
|
try {
|
|
1033
1096
|
while (true) {
|
|
1034
1097
|
let next: IteratorResult<AssistantMessageEvent>;
|
|
@@ -1113,6 +1176,27 @@ async function streamAssistantResponse(
|
|
|
1113
1176
|
assistantMessageEvent: snapshotAssistantMessageEvent(event),
|
|
1114
1177
|
message: snapshotAssistantMessage(partialMessage),
|
|
1115
1178
|
});
|
|
1179
|
+
|
|
1180
|
+
if (isGeminiModel && (event.type === "text_delta" || event.type === "thinking_delta")) {
|
|
1181
|
+
const kind = event.type === "text_delta" ? "text" : "thinking";
|
|
1182
|
+
if (repetitionKind !== kind) {
|
|
1183
|
+
repetitionKind = kind;
|
|
1184
|
+
repetitionTail = "";
|
|
1185
|
+
}
|
|
1186
|
+
repetitionTail += event.delta;
|
|
1187
|
+
if (repetitionTail.length > REPETITION_WINDOW) {
|
|
1188
|
+
repetitionTail = repetitionTail.slice(-REPETITION_WINDOW);
|
|
1189
|
+
}
|
|
1190
|
+
const repetition = detectRepetition(repetitionTail);
|
|
1191
|
+
if (repetition) {
|
|
1192
|
+
const [pattern, count] = repetition;
|
|
1193
|
+
logger.warn("Repetition loop detected during assistant stream, aborting.", {
|
|
1194
|
+
pattern,
|
|
1195
|
+
count,
|
|
1196
|
+
});
|
|
1197
|
+
return await finishRepetitionStream(kind, pattern, count);
|
|
1198
|
+
}
|
|
1199
|
+
}
|
|
1116
1200
|
}
|
|
1117
1201
|
break;
|
|
1118
1202
|
}
|
|
@@ -1719,3 +1803,97 @@ function createSkippedToolResult(): AgentToolResult<any> {
|
|
|
1719
1803
|
details: {},
|
|
1720
1804
|
};
|
|
1721
1805
|
}
|
|
1806
|
+
|
|
1807
|
+
const REPETITION_WINDOW = 250;
|
|
1808
|
+
const REPETITION_MIN_REPEATED_CHARS = 180;
|
|
1809
|
+
|
|
1810
|
+
function detectRepetition(text: string): [pattern: string, count: number] | null {
|
|
1811
|
+
if (text.length < REPETITION_MIN_REPEATED_CHARS) return null;
|
|
1812
|
+
|
|
1813
|
+
const windowSize = Math.min(text.length, REPETITION_WINDOW);
|
|
1814
|
+
const searchSpace = text.slice(-windowSize);
|
|
1815
|
+
|
|
1816
|
+
for (let len = 2; len <= 60; len++) {
|
|
1817
|
+
if (searchSpace.length < len * 4) continue;
|
|
1818
|
+
|
|
1819
|
+
const pattern = searchSpace.slice(-len);
|
|
1820
|
+
// Only treat a repeated unit as a pathological loop when it carries real
|
|
1821
|
+
// linguistic content (a letter or a pictographic emoji). Runs made purely of
|
|
1822
|
+
// digits, whitespace or punctuation are legitimate in tabular / hex / numeric
|
|
1823
|
+
// output (e.g. "00 00 00", "0, 0, 0", "| -- | -- |") and must not trip.
|
|
1824
|
+
if (!/[\p{L}\p{Extended_Pictographic}]/u.test(pattern)) continue;
|
|
1825
|
+
|
|
1826
|
+
let count = 0;
|
|
1827
|
+
let pos = searchSpace.length;
|
|
1828
|
+
while (pos >= len) {
|
|
1829
|
+
const chunk = searchSpace.slice(pos - len, pos);
|
|
1830
|
+
if (chunk === pattern) {
|
|
1831
|
+
count++;
|
|
1832
|
+
pos -= len;
|
|
1833
|
+
} else {
|
|
1834
|
+
break;
|
|
1835
|
+
}
|
|
1836
|
+
}
|
|
1837
|
+
|
|
1838
|
+
if (count >= 4 && len * count >= REPETITION_MIN_REPEATED_CHARS) {
|
|
1839
|
+
return [pattern, count];
|
|
1840
|
+
}
|
|
1841
|
+
}
|
|
1842
|
+
return null;
|
|
1843
|
+
}
|
|
1844
|
+
|
|
1845
|
+
function truncateRepetition(message: AssistantMessage, kind: "text" | "thinking", pattern: string): void {
|
|
1846
|
+
// A repetition loop streams into a single growing block (real providers) or a run
|
|
1847
|
+
// of same-kind blocks (some transports), always at the tail of the message. Gather
|
|
1848
|
+
// that trailing contiguous run and collapse its repeated copies down to one, so the
|
|
1849
|
+
// committed transcript keeps a representative sample instead of the full runaway.
|
|
1850
|
+
const matches = (block: AssistantContentBlock): boolean =>
|
|
1851
|
+
kind === "text" ? block.type === "text" : block.type === "thinking";
|
|
1852
|
+
const readBlock = (block: AssistantContentBlock): string =>
|
|
1853
|
+
block.type === "text" ? block.text : block.type === "thinking" ? block.thinking : "";
|
|
1854
|
+
const clearThinkingReplayAnchors = (block: AssistantContentBlock): void => {
|
|
1855
|
+
if (block.type !== "thinking") return;
|
|
1856
|
+
block.thinkingSignature = undefined;
|
|
1857
|
+
block.itemId = undefined;
|
|
1858
|
+
};
|
|
1859
|
+
const writeBlock = (block: AssistantContentBlock, value: string): void => {
|
|
1860
|
+
if (block.type === "text") {
|
|
1861
|
+
block.text = value;
|
|
1862
|
+
} else if (block.type === "thinking") {
|
|
1863
|
+
block.thinking = value;
|
|
1864
|
+
clearThinkingReplayAnchors(block);
|
|
1865
|
+
}
|
|
1866
|
+
};
|
|
1867
|
+
|
|
1868
|
+
const trailing: AssistantContentBlock[] = [];
|
|
1869
|
+
for (let i = message.content.length - 1; i >= 0; i--) {
|
|
1870
|
+
const block = message.content[i];
|
|
1871
|
+
if (!matches(block)) break;
|
|
1872
|
+
trailing.unshift(block);
|
|
1873
|
+
}
|
|
1874
|
+
if (trailing.length === 0) return;
|
|
1875
|
+
if (kind === "thinking") {
|
|
1876
|
+
for (const block of trailing) clearThinkingReplayAnchors(block);
|
|
1877
|
+
}
|
|
1878
|
+
|
|
1879
|
+
let joined = "";
|
|
1880
|
+
for (const block of trailing) joined += readBlock(block);
|
|
1881
|
+
|
|
1882
|
+
let kept = joined;
|
|
1883
|
+
while (kept.length >= pattern.length * 2 && kept.slice(kept.length - pattern.length * 2) === pattern + pattern) {
|
|
1884
|
+
kept = kept.slice(0, kept.length - pattern.length);
|
|
1885
|
+
}
|
|
1886
|
+
|
|
1887
|
+
let remainingToRemove = joined.length - kept.length;
|
|
1888
|
+
for (let i = trailing.length - 1; i >= 0 && remainingToRemove > 0; i--) {
|
|
1889
|
+
const block = trailing[i];
|
|
1890
|
+
const value = readBlock(block);
|
|
1891
|
+
if (value.length <= remainingToRemove) {
|
|
1892
|
+
remainingToRemove -= value.length;
|
|
1893
|
+
writeBlock(block, "");
|
|
1894
|
+
} else {
|
|
1895
|
+
writeBlock(block, value.slice(0, value.length - remainingToRemove));
|
|
1896
|
+
remainingToRemove = 0;
|
|
1897
|
+
}
|
|
1898
|
+
}
|
|
1899
|
+
}
|
package/src/agent.ts
CHANGED
|
@@ -974,8 +974,13 @@ export class Agent {
|
|
|
974
974
|
}
|
|
975
975
|
: undefined;
|
|
976
976
|
|
|
977
|
-
const getToolChoice = () =>
|
|
978
|
-
this.#getToolChoice?.()
|
|
977
|
+
const getToolChoice = () => {
|
|
978
|
+
const queuedToolChoice = this.#getToolChoice?.();
|
|
979
|
+
if (queuedToolChoice !== undefined) {
|
|
980
|
+
return refreshToolChoiceForActiveTools(queuedToolChoice, this.#state.tools);
|
|
981
|
+
}
|
|
982
|
+
return refreshToolChoiceForActiveTools(options?.toolChoice, this.#state.tools);
|
|
983
|
+
};
|
|
979
984
|
|
|
980
985
|
const config: AgentLoopConfig = {
|
|
981
986
|
model,
|