8000 [pull] main from can1357:main by pull[bot] · Pull Request #121 · ericrihm/oh-my-pi · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
cc40975
feat(advisor): evict stale tool results from finished reviews
Sep 25, 2026
1cd0e83
docs(changelog): credit #13238
Sep 25, 2026
7731c4a
refactor(advisor): reuse the prunedAt staleness rule instead of an ev…
Sep 25, 2026
4cb6e8c
test(advisor): anchor search skips usage reported before the newest p…
Sep 25, 2026
0c60e06
fix(ai): add thinking headroom to capped anthropic requests
alphastorm Sep 25, 2026
887e217
fix(compaction): name the stop reason when anthropic compaction write…
alphastorm Sep 25, 2026
c374826
docs(changelog): note the anthropic thinking headroom fix
alphastorm Sep 25, 2026
b57ea6f
fix(coding-agent): stop re-sending a native compaction that failed fo…
alphastorm Sep 25, 2026
732ef34
docs(changelog): note the native speculation retry fix
alphastorm Sep 25, 2026
6624111
feat(extensions): expose the running agent's identity as ctx.agent
andrebrait Sep 25, 2026
3645c2f
fix(ai): add thinking headroom to capped adaptive claude on bedrock
Sep 25, 2026
afca183
docs(coding-agent): document native speculation failure key tradeoffs
Sep 25, 2026
fa5a1b3
feat(advisor): scope eviction to read/grep/glob, protect the latest r…
Sep 25, 2026
4f6b096
Merge PR #13238: feat(advisor): evict stale tool results from finishe…
H4vC Sep 25, 2026
7c750b7
Merge PR #13300: fix(ai): add thinking headroom to capped anthropic r…
H4vC Sep 25, 2026
04f58a9
Merge PR #13310: fix(coding-agent): stop re-sending a native compacti…
H4vC Sep 25, 2026
04477d1
Merge remote-tracking branch 'origin/main' into feat/extension-agent-…
Sep 25, 2026
983fec7
fix(extensions): document /tan agent identity, use shared constants, …
Sep 25, 2026
533826a
Merge PR #13314: feat(extensions): expose the running agent's identit…
H4vC Sep 25, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
8000
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ Handlers and tool `execute` receive `ctx` with:
- `isIdle()`, `hasPendingMessages()`, `abort()`
- `shutdown()`
- `getSystemPrompt()`
- `agent` — the agent this session runs: `{ kind: "main" | "sub", id, name, depth, parentId? }`. Factories are rebound to every subagent session (task tool, eval `agent()`, `/tan` clones), so a handler can check `ctx.agent.kind === "sub"` or the lowercased agent definition `name` (for example `"explore"`) to act only in subagents. Use `kind`, not `depth`: `depth` counts `task` nesting only, so `/tan` clones are subagents at depth 0 and report `name: "sub"`
- `runEphemeralTurn(...)` (optional; see below)
- `memory` (optional structured memory runtime — status/search/save across the configured backend)
- `setInterval(fn, ms, ...args)` / `setTimeout(fn, ms, ...args)` / `clearTimer(timer)` — managed timers (see below)
Expand Down
2 changes: 1 addition & 1 deletion docs/provider-quirks.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ The Anthropic provider (`packages/ai/src/providers/anthropic.ts`) implements the
- **Thinking Signatures & Redacted Thinking**: Replaying modified or unsigned thinking blocks causes Anthropic API errors (`invalid signature in thinking block`). `convertAnthropicMessages` converts `ThinkingContent` and `RedactedThinkingContent` (`type: "redacted_thinking"`, `data`). `maybeAddReplayUnsignedThinkingHint` attaches recovery hints on signature errors, while `unwrapAnthropicThinkingEnvelope` strips legacy `<thinking>` XML wrappers.
- **Tool Use Replay & Prefixes**: `encodeAnthropicToolName` / `decodeAnthropicToolName` (`packages/ai/src/providers/anthropic.ts`) prefixes custom tool names with `_` (`claudeToolPrefix`) when using OAuth to prevent collisions with built-in tools (`web_search`, `code_execution`, `text_editor`, `computer`). Server-executed web searches and tool searches (`AnthropicServerToolHistoryBlockParam` in `anthropic-wire.ts`) are detected via `isAnthropicServerToolHistoryBlock` for turn replay. Empty tool errors are filled by `ensureErrorToolResultWireContent`.
- **Strict-Tool Schema Normalization & Fallback**: `normalizeAnthropicToolSchema` and `normalizeAnthropicStrictSchema` strip unsupported JSON schema keywords (e.g. `minItems`/`maxItems` on objects) for the `structured-outputs-2025-12-15` beta. If a strict tool schema causes HTTP 400, `streamAnthropicOnce` calls `dropAnthropicStrictTools` and automatically retries without strict mode.
- **Adaptive vs Budget Thinking**: `ThinkingConfigParam` (`anthropic-wire.ts`) supports budget thinking (`{ type: "enabled", budget_tokens: N }` enforced by `ensureMaxTokensForThinking`) and adaptive thinking (`{ type: "adaptive" }` paired with `output_config: { effort: level }` via `effort-2025-11-24` beta). Forced tool choices (`disableThinkingIfToolChoiceForced`) automatically disable thinking.
- **Adaptive vs Budget Thinking**: `ThinkingConfigParam` (`anthropic-wire.ts`) supports budget thinking (`{ type: "enabled", budget_tokens: N }` enforced by `ensureMaxTokensForThinking`) and adaptive thinking (`{ type: "adaptive" }` paired with `output_config: { effort: level }` via `effort-2025-11-24` beta). Forced tool choices (`disableThinkingIfToolChoiceForced`) automatically disable thinking. Thinking and visible output share `max_tokens`, so `streamSimple` treats a caller's `maxTokens` as the output it wants and adds the effort's thinking budget on top on every `anthropic-messages` thinking path and for adaptive Claude on Bedrock (capped at the model ceiling); without it, adaptive thinking can spend the whole cap and return nothing — an on-demand compaction then ends at `max_tokens` with no `compaction` block.
- **Prompt Cache Breakpoints**: `applyPromptCaching` (`packages/ai/src/providers/anthropic.ts:3195`, called at `:3506`) marks a two-message rolling window at the tail of the conversation: it attaches `cache_control: { type: "ephemeral" }` (plus `ttl: "1h"` only for long retention on models that support it, built by `getCacheControl` at `:497`) to the last ordinary content block of each of the two trailing turns, skipping `thinking`, `redacted_thinking`, and `fallback` blocks (`applyCacheControlToLastBlock` at `:3179`). When the trailing user message is the neutral `"Continue."` pad appended after an assistant prefill, the window anchors on the preceding real assistant instead. Caching is never applied to system prompts or tool definitions, and there is no total-breakpoint cap.

### Stream behavior
Expand Down
1 change: 1 addition & 0 deletions docs/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ See [Advisor and WATCHDOG.md](./advisor-watchdog.md) for runtime behavior, `WATC
| `advisor.syncBacklog` | enum | `off` | Bounded advisor catch-up delay: `off`, `1`, `3`, or `5`. The primary waits up to 30 seconds only while advisor backlog is at or above the threshold. |
| `advisor.immuneTurns` | number | `3` | After a `concern`/`blocker` interrupts, route further concerns/blockers as non-interrupting asides for this many completed primary turns. |
| `advisor.maxNotesPerUpdate` | number | `4` | Non-blocker notes accepted per advisor review, from 1–32. Higher-severity notes can replace only pending notes from the same review. `WATCHDOG.yml` top-level or per-advisor values override this default. |
| `advisor.evictStaleResults` | boolean | `true` | Before each review, replace the advisor's `read`/`grep`/`glob` output from older reviews with a short placeholder. The latest review is kept. |

### Thinking

Expand Down
1 change: 1 addition & 0 deletions packages/agent/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Fixed

- Fixed tool calls that put their payload in the intent field `i` (for example a file body in `write`) silently running with the leftover arguments; they now fail with an error telling the model to retry ([#13140](https://github.com/can1357/oh-my-pi/issues/13140), [#13141](https://github.com/can1357/oh-my-pi/pull/13141) by [@radkawar](https://github.com/radkawar))
- Fixed the Anthropic compaction failure log omitting why no compaction block came back; it now names the stop reason ([#13300](https://github.com/can1357/oh-my-pi/pull/13300) by [@alphastorm](https://github.com/alphastorm))

## [18.3.1] - 2026-09-25

Expand Down
2 changes: 1 addition & 1 deletion packages/agent/src/compaction/anthropic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ export async function requestAnthropicNativeCompaction(
throw new Error(
response.stopDetails?.type === "compaction"
? "Anthropic compaction returned no signed summary"
: "Anthropic compaction response carried no compaction block",
: `Anthropic compaction response carried no compaction block (stop reason: ${response.stopDetails?.type ?? response.stopReason})`,
);
}
return {
Expand Down
2 changes: 1 addition & 1 deletion packages/agent/src/compaction/pruning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ function createPrunedNotice(tokens: number): string {
* own rules: useless already drops no-savings candidates, superseded prunes for
* correctness regardless of size.
*/
const MIN_PRUNE_TOKENS = 50;
export const MIN_PRUNE_TOKENS = 50;

function getToolResultMessage(entry: SessionEntry): ToolResultMessage | undefined {
if (entry.type !== "message") return undefined;
Expand Down
35 changes: 33 additions & 2 deletions packages/agent/src/compaction/transcript-tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,31 @@ export function isTranscriptUsageAnchor(message: AgentMessage): message is Assis
return assistant.usage !== undefined && hasContextTokenUsage(assistant.usage);
}

/**
* Newest `prunedAt` in `messages`, or `-Infinity` when nothing was pruned.
*
* A pruned tool result was rewritten in place, so every usage report made at
* or before this time still counts the removed bytes.
*/
function newestPrunedAt(messages: readonly AgentMessage[]): number {
let rewriteAt = Number.NEGATIVE_INFINITY;
for (const message of messages) {
if (message.role === "toolResult" && message.prunedAt !== undefined) {
rewriteAt = Math.max(rewriteAt, message.prunedAt);
}
}
return rewriteAt;
}

/** Options for {@link findTranscriptUsageAnchor}. */
export interface TranscriptUsageAnchorOptions {
/**
* Also treat usage reported at or before the newest `prunedAt` as stale,
* the same rule {@link findRequestUsageAnchor} applies to request contexts.
*/
skipPrunedAnchors?: boolean;
}

/**
* Newest assistant turn in `messages[fromIndex..]` whose usage can anchor the
* transcript, or `undefined` when none qualifies (fresh context, or every
Expand All @@ -58,10 +83,12 @@ export function isTranscriptUsageAnchor(message: AgentMessage): message is Assis
export function findTranscriptUsageAnchor(
messages: readonly AgentMessage[],
fromIndex = 0,
options?: TranscriptUsageAnchorOptions,
): TranscriptUsageAnchor | undefined {
const rewriteAt = options?.skipPrunedAnchors === true ? newestPrunedAt(messages) : Number.NEGATIVE_INFINITY;
for (let index = messages.length - 1; index >= fromIndex; index--) {
const message = messages[index];
if (!isTranscriptUsageAnchor(message)) continue;
if (!isTranscriptUsageAnchor(message) || message.timestamp <= rewriteAt) continue;
return { index, message, tokens: calculateContextTokens(message.usage) };
}
return undefined;
Expand Down Expand Up @@ -105,6 +132,8 @@ export interface TranscriptTokenOptions {
* accounting is governed separately by {@link countFromIndex}.
*/
anchorFromIndex?: number;
/** Forwarded to {@link findTranscriptUsageAnchor}. */
skipPrunedAnchors?: boolean;
/**
* First message whose content is counted locally when no anchor is found.
* Defaults to 0 (count the whole transcript), which is what a floor
Expand Down Expand Up @@ -132,7 +161,9 @@ export function estimateTranscriptTokens(
): number {
const estimateOptions: MessageCountOptions | undefined =
options?.excludeEncryptedReasoning === true ? { excludeEncryptedReasoning: true } : undefined;
const anchor = findTranscriptUsageAnchor(messages, options?.anchorFromIndex ?? 0);
const anchor = findTranscriptUsageAnchor(messages, options?.anchorFromIndex ?? 0, {
skipPrunedAnchors: options?.skipPrunedAnchors,
});
let total = anchor?.tokens ?? 0;
for (let index = anchor ? anchor.index + 1 : (options?.countFromIndex ?? 0); index < messages.length; index++) {
total += tokenizer.countMessage(messages[index], estimateOptions);
Expand Down
7 changes: 4 additions & 3 deletions packages/agent/test/anthropic-native-compaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,9 +441,10 @@ describe("compact() Anthropic native lane", () => {
const { calls, completeImpl } = recordingCompleteImpl(() =>
assistantMessage(model, { ...stop, content: [{ type: "text", text: "not a summary" }] }),
);
await expect(
compact(makePreparation(), model, "sk-ant-test", undefined, undefined, { completeImpl }),
).rejects.toBeInstanceOf(NativeCompactionError);
const failure = compact(makePreparation(), model, "sk-ant-test", undefined, undefined, { completeImpl });
await expect(failure).rejects.toBeInstanceOf(NativeCompactionError);
// The log line is the only place a user sees why native compaction fell back.
await expect(failure).rejects.toThrow(`stop reason: ${stop.stopDetails?.type}`);
expect(calls).toHaveLength(1);
}
expect(completeSimple).not.toHaveBeenCalled();
Expand Down
4 changes: 4 additions & 0 deletions packages/ai/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

### Fixed

- Fixed capped Anthropic and Bedrock Claude requests with thinking enabled, including on-demand compaction, ending at `max_tokens` with no answer; every capped request now gets its effort's thinking budget on top of the requested output ([#13300](https://github.com/can1357/oh-my-pi/pull/13300) by [@alphastorm](https://github.com/alphastorm))

## [18.3.1] - 2026-09-25

### Added
Expand Down
32 changes: 30 additions & 2 deletions packages/ai/src/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2138,11 +2138,21 @@ function mapOptionsForApi<TApi extends Api>(
? mapEffortToAnthropicAdaptiveEffort(model, reasoning)
: undefined;

// A caller's maxTokens is the output it asked for, but thinking spends the
// same max_tokens: adaptive thinking can use all of it and leave no answer.
// Give thinking its budget on top, as the budget-only path below does. An
// uncapped request keeps the provider default.
const maxTokensWithThinking =
base.maxTokens === undefined
? undefined
: maxTokensWithThinkingBudget(base.maxTokens, model.maxTokens, thinkingBudget);

// For Opus 4.6+ and Sonnet 4.6+: use adaptive thinking with effort level
// For older models: use budget-based thinking
if (thinkingMode === "anthropic-adaptive") {
return castApi<"anthropic-messages">({
...base,
maxTokens: maxTokensWithThinking,
requestModelId: resolveWireModelId(model, reasoning),
thinkingEnabled: true,
effort,
Expand All @@ -2155,6 +2165,7 @@ function mapOptionsForApi<TApi extends Api>(
if (ANTHROPIC_USE_INTERLEAVED_THINKING) {
return castApi<"anthropic-messages">({
...base,
maxTokens: maxTokensWithThinking,
requestModelId: resolveWireModelId(model, reasoning),
thinkingEnabled: true,
thinkingBudgetTokens: thinkingBudget,
Expand Down Expand Up @@ -2214,8 +2225,25 @@ function mapOptionsForApi<TApi extends Api>(
guardrailTrace: model.guardrailTrace ?? options?.guardrailTrace,
requestMetadata: options?.requestMetadata,
};
// Effort modes send effort directly, no budget_tokens — skip budget inflation.
if (model.thinking?.mode === "effort" || model.thinking?.mode === "anthropic-adaptive") {
// Adaptive Claude shares max_tokens between thinking and the answer, like
// the anthropic-messages adaptive path: a caller's cap is the output it
// wants, so add the effort's budget on top. Uncapped requests keep the
// provider default.
if (model.thinking?.mode === "anthropic-adaptive") {
const reasoning = bedrockBase.reasoning;
const budget = reasoning
? (options?.thinkingBudgets?.[reasoning] ?? BEDROCK_CLAUDE_THINKING[reasoning])
: 0;
if (!model.reasoning || bedrockBase.maxTokens === undefined || budget <= 0) {
return castApi<"bedrock-converse-stream">(bedrockBase);
}
return castApi<"bedrock-converse-stream">({
...bedrockBase,
maxTokens: maxTokensWithThinkingBudget(bedrockBase.maxTokens, model.maxTokens, budget),
});
}
// Effort mode sends effort directly, no budget_tokens — skip budget inflation.
if (model.thinking?.mode === "effort") {
return castApi<"bedrock-converse-stream">(bedrockBase);
}
const budgetInfo = resolveBedrockThinkingBudget(model as Model<"bedrock-converse-stream">, options);
Expand Down
117 changes: 117 additions & 0 deletions packages/ai/test/anthropic-thinking-max-tokens.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import { describe, expect, it } from "bun:test";
import { ANTHROPIC_THINKING, streamSimple } from "@oh-my-pi/pi-ai/stream";
import type { Context, FetchImpl, Model, ModelSpec } from "@oh-my-pi/pi-ai/types";
import { buildModel } from "@oh-my-pi/pi-catalog/build";
import { Effort } from "@oh-my-pi/pi-catalog/effort";

const context: Context = {
systemPrompt: ["Summarize the conversation."],
messages: [{ role: "user", content: "ping", timestamp: Date.now() }],
};

function anthropicModel(id: string, maxTokens: number): Model<"anthropic-messages"> {
return buildModel({
id,
name: id,
api: "anthropic-messages",
provider: "anthropic",
baseUrl: "https://api.anthropic.com",
reasoning: true,
input: ["text"],
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
contextWindow: 1_000_000,
maxTokens,
} satisfies ModelSpec<"anthropic-messages">);
}

interface WirePayload {
max_tokens?: number;
thinking?: { type?: string; budget_tokens?: number };
}

/** The request `streamSimple` puts on the wire; the endpoint answers 400 so nothing streams. */
async function wirePayload(
model: Model<"anthropic-messages">,
options: { maxTokens?: number; reasoning: Effort },
): Promise<WirePayload> {
let payload: WirePayload | undefined;
const fetch: FetchImpl = async (_url, init) => {
payload = JSON.parse(init?.body as string) as WirePayload;
return new Response("{}", { status: 400 });
};
await streamSimple(model, context, { apiKey: "test-key", fetch, ...options }).result();
if (!payload) throw new Error("streamSimple sent no request");
return payload;
}

describe("Anthropic thinking leaves a capped request its output budget", () => {
it("adds the effort's thinking budget to a capped adaptive request", async () => {
// Adaptive thinking and the answer share max_tokens. A 13,107-token
// compaction summary cap at max effort used to go entirely to thinking,
// so on-demand compaction ended at max_tokens without a compaction block.
const payload = await wirePayload(anthropicModel("claude-opus-5-5", 128_000), {
maxTokens: 13_107,
reasoning: Effort.Max,
});
expect(payload.thinking?.type).toBe("adaptive");
expect(payload.max_tokens).toBe(13_107 + ANTHROPIC_THINKING.max);
});

it("adds the thinking budget on the interleaved budget path", async () => {
// Budget thinking only guaranteed the provider's 4k fallback beyond the
// budget, not the output the caller asked for.
const payload = await wirePayload(anthropicModel("claude-sonnet-4-5", 64_000), {
maxTokens: 13_107,
reasoning: Effort.High,
});
expect(payload.thinking).toMatchObject({ type: "enabled", budget_tokens: ANTHROPIC_THINKING.high });
expect(payload.max_tokens).toBe(13_107 + ANTHROPIC_THINKING.high);
});

it("never raises a capped request above the model's output ceiling", async () => {
const payload = await wirePayload(anthropicModel("claude-opus-5-5", 32_000), {
maxTokens: 13_107,
reasoning: Effort.Max,
});
expect(payload.max_tokens).toBe(32_000);
});

it("keeps an uncapped request at the model's output ceiling", async () => {
// Agent turns pass no cap; they must not fall to the finite fallback the
// budget-only path uses when neither the caller nor the model sets one.
const payload = await wirePayload(anthropicModel("claude-opus-5-5", 128_000), { reasoning: Effort.Max });
expect(payload.max_tokens).toBe(128_000);
});

it("adds the thinking budget to a capped adaptive request on Bedrock", async () => {
// Bedrock-hosted adaptive Claude shares maxTokens with thinking the same way.
const model = buildModel({
id: "anthropic.claude-opus-4-7",
name: "Claude Opus 4.7",
api: "bedrock-converse-stream",
provider: "amazon-bedrock",
baseUrl: "https://bedrock-runtime.us-east-1.amazonaws.com",
reasoning: true,
input: ["text"],
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
contextWindow: 200_000,
maxTokens: 64_000,
thinking: { mode: "anthropic-adaptive", efforts: [Effort.Low, Effort.Medium, Effort.High] },
} satisfies ModelSpec<"bedrock-converse-stream">);
const controller = new AbortController();
const { promise, resolve } = Promise.withResolvers<{ inferenceConfig?: { maxTokens?: number } }>();
void streamSimple(model, context, {
apiKey: "test-key",
signal: controller.signal,
maxTokens: 13_107,
reasoning: Effort.High,
onPayload: payload => {
resolve(payload as { inferenceConfig?: { maxTokens?: number } });
controller.abort();
return undefined;
},
});
// 16,384 is Bedrock's high-effort Claude thinking budget.
expect((await promise).inferenceConfig?.maxTokens).toBe(13_107 + 16_384);
});
});
Loading
Loading
0