diff --git a/packages/core/src/plugin/provider/claude-agent/claude/sessions.ts b/packages/core/src/plugin/provider/claude-agent/claude/sessions.ts index 394b56748d2..86ec9eac3e0 100644 --- a/packages/core/src/plugin/provider/claude-agent/claude/sessions.ts +++ b/packages/core/src/plugin/provider/claude-agent/claude/sessions.ts @@ -197,7 +197,7 @@ export function registry() { ) const process = opened.process // A new process reports its version, login and models, which keeps the model list current without a separate check. - if (starting) yield* input.status.report({ type: "ready", probe: opened.probe }) + if (starting) yield* input.status.report({ type: "ready", probe: opened.probe, fromChat: true }) // Apply controls before sending input or releasing parked tool results. if (process.model !== request.model) { yield* attempt(() => process.session.setModel(request.model)) diff --git a/packages/core/src/plugin/provider/claude-agent/claude/status.ts b/packages/core/src/plugin/provider/claude-agent/claude/status.ts index 8711c588303..6d3cbd0657a 100644 --- a/packages/core/src/plugin/provider/claude-agent/claude/status.ts +++ b/packages/core/src/plugin/provider/claude-agent/claude/status.ts @@ -9,7 +9,7 @@ export type State = | { readonly type: "checking" } | { readonly type: "missing" } | { readonly type: "failed"; readonly message: string } - | { readonly type: "ready"; readonly probe: ClaudeProbe.Result } + | { readonly type: "ready"; readonly probe: ClaudeProbe.Result; readonly fromChat?: boolean } export interface Interface { readonly state: SubscriptionRef.SubscriptionRef diff --git a/packages/core/src/plugin/provider/claude-agent/provider.ts b/packages/core/src/plugin/provider/claude-agent/provider.ts index 15ed23e5203..ead270b6ac1 100644 --- a/packages/core/src/plugin/provider/claude-agent/provider.ts +++ b/packages/core/src/plugin/provider/claude-agent/provider.ts @@ -63,6 +63,7 @@ export const register = Effect.fn("ClaudeProvider.register")(function* ( }) }) + const checked = new Set() yield* SubscriptionRef.changes(status.state).pipe( Stream.runForEach((state) => Effect.gen(function* () { @@ -72,6 +73,11 @@ export const register = Effect.fn("ClaudeProvider.register")(function* ( priceSource: priceSource(state.probe.account, baseURL), models: fromClaude(state.probe.models, current.catalog.models), } + const unknown = next.models.filter((model) => model.contextWindow === undefined && !checked.has(model.id)) + if (state.fromChat && unknown.length > 0) { + for (const model of unknown) checked.add(model.id) + yield* status.refresh().pipe(Effect.forkScoped) + } if (JSON.stringify(next) === JSON.stringify(current.catalog)) return current.catalog = next yield* ctx.storage.set(CATALOG_KEY, current.catalog) diff --git a/packages/core/test/plugin/provider/claude-agent/plugin.test.ts b/packages/core/test/plugin/provider/claude-agent/plugin.test.ts index 05636a17ae4..98c1084f270 100644 --- a/packages/core/test/plugin/provider/claude-agent/plugin.test.ts +++ b/packages/core/test/plugin/provider/claude-agent/plugin.test.ts @@ -226,16 +226,15 @@ describe("ClaudeAgentPlugin models", () => { }), ) - it.live("binds chats to plugin storage, and a chat's startup report keeps saved context sizes", () => + it.live("binds chats to plugin storage, and checks context sizes for models a chat reports without one", () => Effect.gen(function* () { const store = yield* storage yield* store.set("models", { models: [{ id: "haiku", name: "Haiku", efforts: [], contextWindow: 123_456 }] }) yield* connect - const claude = FakeClaude.make([ - FakeClaude.expectStartup(), - FakeClaude.expectTurn(), - FakeClaude.expectInterrupt(), - ]) + const claude = FakeClaude.make( + [FakeClaude.expectStartup(), FakeClaude.expectTurn(), FakeClaude.expectInterrupt()], + { probes: true }, + ) const registry = yield* addPlugin(FakeIO.make({ claude })) const aisdk = yield* AISDK.Service @@ -258,8 +257,9 @@ describe("ClaudeAgentPlugin models", () => { expect(TestSessions.text(yield* Effect.promise(() => TestSessions.collect(result.stream)))).toBe("COPPER-OTTER") expect(yield* store.get("sessions/session-a")).toMatchObject({ claudeSessionID: expect.any(String) }) - const models = yield* eventually(claudeModels, (models) => models.length > 1) - expect(find(models, "haiku").limit.context).toBe(123_456) + yield* eventually(claudeModels, (models) => + models.some((model) => model.id === "sonnet" && model.limit.context === 1_000_000), + ) yield* registry.close() claude.check() }), diff --git a/specs/v2/claude-agent-provider.md b/specs/v2/claude-agent-provider.md index cecb073e18a..188b1b05ff8 100644 --- a/specs/v2/claude-agent-provider.md +++ b/specs/v2/claude-agent-provider.md @@ -122,7 +122,7 @@ Users can configure the `claude-agent` provider and its models in opencode confi - Why not Claude's own cost: it is a running total for the turn, so it cannot be split per response. - Claude Code reports usage for one model response several times while it streams, and again as a total for the turn. Adding these up would count the same response several times and make opencode compact too early. We keep the highest count for each response, and use the last response's input as the context size. - Each model's context size comes from Claude. Without it, opencode's 200k default applies. -- The model list is refreshed whenever Claude reports it: on `/connect`, when the login changes, and each time a new Claude process starts. It is saved, with where the login's prices come from, so opencode can show models and costs at startup without running `claude`. +- The model list is refreshed whenever Claude reports it: on `/connect`, when the login changes, and each time a new Claude process starts. A new process doesn't report context sizes, so a model it lists without a saved size triggers one full check. The list is saved, with where the login's prices come from, so opencode can show models and costs at startup without running `claude`. ### Titles, Summaries and Compaction