Skip to content

Commit 6954639

Browse files
authored
fix(opencode): fix 1M context for Opus - compaction token counting (#154) (#155)
* fix(opencode): fix 1M context for Opus - compaction token counting (#154) - Fix compaction to count only input tokens (not output/thinking) - Context limit override for Anthropic models with context-1m header - Restore adaptive-thinking header and effort-based thinking for Opus 4.6 * feat: add /upstream-gap custom command for fork gap analysis
1 parent a2dbf1c commit 6954639

4 files changed

Lines changed: 57 additions & 2 deletions

File tree

‎.opencode/command/upstream-gap.md‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
description: "Analyze gap between our fork and upstream opencode"
3+
---
4+
5+
## Upstream Gap Analysis
6+
7+
You are the project manager for the opencode fork at `randomm/opencode`. Analyze the gap between our fork's `dev` branch and the upstream `anomalyco/opencode` `dev` branch.
8+
9+
### Steps
10+
11+
1. **Fetch upstream:**
12+
!`git fetch anomalyco 2>&1 || echo "Add remote first: git remote add anomalyco [email protected]:anomalyco/opencode.git"`
13+
14+
2. **Get the divergence point:**
15+
!`git merge-base dev anomalyco/dev`
16+
17+
3. **List upstream commits we don't have** (grouped by date):
18+
!`git log --oneline --no-merges anomalyco/dev --not dev -- packages/opencode/ | head -80`
19+
20+
4. **Count by area:**
21+
!`git log --oneline --no-merges anomalyco/dev --not dev -- packages/opencode/src/provider/ | wc -l`
22+
!`git log --oneline --no-merges anomalyco/dev --not dev -- packages/opencode/src/session/ | wc -l`
23+
!`git log --oneline --no-merges anomalyco/dev --not dev -- packages/opencode/src/tool/ | wc -l`
24+
!`git log --oneline --no-merges anomalyco/dev --not dev -- packages/opencode/src/agent/ | wc -l`
25+
!`git log --oneline --no-merges anomalyco/dev --not dev -- packages/opencode/src/config/ | wc -l`
26+
!`git log --oneline --no-merges anomalyco/dev --not dev -- packages/opencode/src/mcp/ | wc -l`
27+
!`git log --oneline --no-merges anomalyco/dev --not dev -- packages/opencode/src/permission/ | wc -l`
28+
!`git log --oneline --no-merges anomalyco/dev --not dev -- packages/sdk/ | wc -l`
29+
30+
5. **Our fork's unique changes** (not in upstream):
31+
!`git log --oneline --no-merges dev --not anomalyco/dev -- packages/opencode/ | head -40`
32+
33+
### Analysis Instructions
34+
35+
Based on the above data:
36+
37+
1. **Create a summary table** of upstream changes grouped by area, with commit count and description of what changed
38+
2. **Flag conflicts** - identify areas where BOTH our fork AND upstream made changes (these need careful 3-way merge)
39+
3. **Recommend priority** - rank which upstream changes are most valuable to port:
40+
- CRITICAL: Security fixes, major bug fixes
41+
- HIGH: New features we need (e.g., SDK upgrades, new providers)
42+
- MEDIUM: Improvements and refactors
43+
- LOW: Minor fixes, cosmetic changes
44+
4. **Note already ported** - identify any upstream changes we already manually ported
45+
46+
Present the results and ask the user which items they want to port.
47+
48+
$ARGUMENTS

‎packages/opencode/src/provider/provider.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,12 @@ export namespace Provider {
638638
variants: {},
639639
}
640640

641+
// Override context limit for Anthropic models with context-1m header support
642+
// The models.dev API returns 200k for these models, but with our context-1m header they support 1M
643+
if (provider.id === "anthropic" && (m.id.startsWith("claude-sonnet-") || m.id.startsWith("claude-opus-4"))) {
644+
m.limit.context = 1_000_000
645+
}
646+
641647
m.variants = mapValues(ProviderTransform.variants(m), (v) => v)
642648

643649
return m

‎packages/opencode/src/session/compaction.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export namespace SessionCompaction {
3232
if (config.compaction?.auto === false) return false
3333
const context = input.model.limit.context
3434
if (context === 0) return false
35-
const count = input.tokens.input + input.tokens.cache.read + input.tokens.output
35+
const count = input.tokens.input + input.tokens.cache.read
3636
const output = Math.min(input.model.limit.output, SessionPrompt.OUTPUT_TOKEN_MAX) || SessionPrompt.OUTPUT_TOKEN_MAX
3737
const usable = input.model.limit.input || context - output
3838
return count > usable

‎packages/opencode/test/session/compaction.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ describe("session.compaction.isOverflow", () => {
7070
directory: tmp.path,
7171
fn: async () => {
7272
const model = createModel({ context: 100_000, output: 32_000 })
73-
const tokens = { input: 50_000, output: 10_000, reasoning: 0, cache: { read: 10_000, write: 0 } }
73+
// Usable = 100k - 32k = 68k. Input + cache.read = 60k + 10k = 70k > 68k
74+
const tokens = { input: 60_000, output: 10_000, reasoning: 0, cache: { read: 10_000, write: 0 } }
7475
expect(await SessionCompaction.isOverflow({ tokens, model })).toBe(true)
7576
},
7677
})

0 commit comments

Comments
 (0)