Skip to content

Commit 0d1c221

Browse files
EBrownclaude
andcommitted
fix: resolve 8 open bug reports from upstream tracker
- anomalyco#16353: Filter LSP diagnostics to current worktree, preventing external project errors from leaking into tool results and wasting context tokens - anomalyco#16392: Bump session time.updated during compaction so session list correctly reflects recently compacted sessions - anomalyco#16395: Set session time.compacting at start of compaction and clear on completion so TUI can display compacting status indicator - anomalyco#16333/anomalyco#15871: Use max(context - maxOutput, input - reserved) for compaction threshold so large-context models (1M) don't trigger early - anomalyco#16332/anomalyco#15715: Skip empty text parts in toModelMessages and fallback to space for empty tool outputs to prevent provider "non-empty" errors - anomalyco#15888: Update edit.txt tool description error messages to match actual error strings thrown by edit.ts - anomalyco#15724: Handle empty files in apply_patch by collecting chunk new_lines directly instead of failing on context search in empty content - anomalyco#16193: Strip ANSI escape sequences from generic tool output in TUI to prevent terminal corruption from colored/cursor-moving output Co-Authored-By: Claude Opus 4.6 <[email protected]>
1 parent 41f7907 commit 0d1c221

6 files changed

Lines changed: 55 additions & 14 deletions

File tree

‎packages/opencode/src/cli/cmd/tui/routes/session/index.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1571,7 +1571,7 @@ type ToolProps<T extends Tool.Info> = {
15711571
function GenericTool(props: ToolProps<any>) {
15721572
const { theme } = useTheme()
15731573
const ctx = use()
1574-
const output = createMemo(() => props.output?.trim() ?? "")
1574+
const output = createMemo(() => stripAnsi(props.output?.trim() ?? ""))
15751575
const [expanded, setExpanded] = createSignal(false)
15761576
const lines = createMemo(() => output().split("\n"))
15771577
const maxLines = 3

‎packages/opencode/src/lsp/index.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,11 +290,13 @@ export namespace LSP {
290290

291291
export async function diagnostics() {
292292
const results: Record<string, LSPClient.Diagnostic[]> = {}
293+
const worktree = Instance.worktree
293294
for (const result of await runAll(async (client) => client.diagnostics)) {
294-
for (const [path, diagnostics] of result.entries()) {
295-
const arr = results[path] || []
295+
for (const [filePath, diagnostics] of result.entries()) {
296+
if (!filePath.startsWith(worktree)) continue
297+
const arr = results[filePath] || []
296298
arr.push(...diagnostics)
297-
results[path] = arr
299+
results[filePath] = arr
298300
}
299301
}
300302
return results

‎packages/opencode/src/patch/index.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,22 @@ export namespace Patch {
324324
originalLines.pop()
325325
}
326326

327+
// Handle empty files: if no original lines, collect all new lines from chunks
328+
if (originalLines.length === 0) {
329+
const allNewLines: string[] = []
330+
for (const chunk of chunks) {
331+
allNewLines.push(...chunk.new_lines)
332+
}
333+
if (allNewLines.length === 0 || allNewLines[allNewLines.length - 1] !== "") {
334+
allNewLines.push("")
335+
}
336+
const newContent = allNewLines.join("\n")
337+
return {
338+
unified_diff: generateUnifiedDiff(originalContent, newContent),
339+
content: newContent,
340+
}
341+
}
342+
327343
const replacements = computeReplacements(originalLines, filePath, chunks)
328344
let newLines = applyReplacements(originalLines, replacements)
329345

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

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,23 @@ import { Agent } from "@/agent/agent"
1414
import { Plugin } from "@/plugin"
1515
import { Config } from "@/config/config"
1616
import { ProviderTransform } from "@/provider/transform"
17+
import { Database, eq } from "../storage/db"
18+
import { SessionTable } from "./session.sql"
1719

1820
export namespace SessionCompaction {
1921
const log = Log.create({ service: "session.compaction" })
2022

23+
/** Update the session's compacting timestamp and bump time_updated. */
24+
function setCompacting(sessionID: string, compacting: number | null) {
25+
const now = Date.now()
26+
Database.use((db) => {
27+
db.update(SessionTable)
28+
.set({ time_compacting: compacting, time_updated: now })
29+
.where(eq(SessionTable.id, sessionID))
30+
.run()
31+
})
32+
}
33+
2134
export const Event = {
2235
Compacted: BusEvent.define(
2336
"session.compacted",
@@ -41,9 +54,10 @@ export namespace SessionCompaction {
4154

4255
const reserved =
4356
config.compaction?.reserved ?? Math.min(COMPACTION_BUFFER, ProviderTransform.maxOutputTokens(input.model))
44-
const usable = input.model.limit.input
45-
? input.model.limit.input - reserved
46-
: context - ProviderTransform.maxOutputTokens(input.model)
57+
const maxOutput = ProviderTransform.maxOutputTokens(input.model)
58+
const fromContext = context - maxOutput
59+
const fromInput = input.model.limit.input ? input.model.limit.input - reserved : 0
60+
const usable = Math.max(fromContext, fromInput)
4761
return count >= usable
4862
}
4963

@@ -123,6 +137,9 @@ export namespace SessionCompaction {
123137
auto: boolean
124138
overflow?: boolean
125139
}) {
140+
// Bug #16395 + #16392: mark session as compacting and bump time_updated
141+
setCompacting(input.sessionID, Date.now())
142+
126143
const userMessage = input.messages.findLast((m) => m.info.id === input.parentID)!.info as MessageV2.User
127144

128145
let messages = input.messages
@@ -246,6 +263,7 @@ When constructing the summary, try to stick to this template:
246263
}).toObject()
247264
processor.message.finish = "error"
248265
await Session.updateMessage(processor.message)
266+
setCompacting(input.sessionID, null)
249267
return "stop"
250268
}
251269

@@ -305,7 +323,12 @@ When constructing the summary, try to stick to this template:
305323
})
306324
}
307325
}
308-
if (processor.message.error) return "stop"
326+
if (processor.message.error) {
327+
setCompacting(input.sessionID, null)
328+
return "stop"
329+
}
330+
// Bug #16395 + #16392: clear compacting status and bump time_updated
331+
setCompacting(input.sessionID, null)
309332
Bus.publish(Event.Compacted, { sessionID: input.sessionID })
310333
return "continue"
311334
}

‎packages/opencode/src/session/message-v2.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ export namespace MessageV2 {
523523

524524
const toModelOutput = (output: unknown) => {
525525
if (typeof output === "string") {
526-
return { type: "text", value: output }
526+
return { type: "text", value: output || " " }
527527
}
528528

529529
if (typeof output === "object") {
@@ -538,7 +538,7 @@ export namespace MessageV2 {
538538
return {
539539
type: "content",
540540
value: [
541-
{ type: "text", text: outputObject.text },
541+
{ type: "text", text: outputObject.text || " " },
542542
...attachments.map((attachment) => ({
543543
type: "media",
544544
mediaType: attachment.mime,
@@ -565,7 +565,7 @@ export namespace MessageV2 {
565565
}
566566
result.push(userMessage)
567567
for (const part of msg.parts) {
568-
if (part.type === "text" && !part.ignored)
568+
if (part.type === "text" && !part.ignored && part.text.trim())
569569
userMessage.parts.push({
570570
type: "text",
571571
text: part.text,
@@ -621,7 +621,7 @@ export namespace MessageV2 {
621621
parts: [],
622622
}
623623
for (const part of msg.parts) {
624-
if (part.type === "text")
624+
if (part.type === "text" && part.text.trim())
625625
assistantMessage.parts.push({
626626
type: "text",
627627
text: part.text,

‎packages/opencode/src/tool/edit.txt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ Usage:
55
- When editing text from Read tool output, ensure you preserve the exact indentation (tabs/spaces) as it appears AFTER the line number prefix. The line number prefix format is: line number + colon + space (e.g., `1: `). Everything after that space is the actual file content to match. Never include any part of the line number prefix in the oldString or newString.
66
- ALWAYS prefer editing existing files in the codebase. NEVER write new files unless explicitly required.
77
- Only use emojis if the user explicitly requests it. Avoid adding emojis to files unless asked.
8-
- The edit will FAIL if `oldString` is not found in the file with an error "oldString not found in content".
9-
- The edit will FAIL if `oldString` is found multiple times in the file with an error "Found multiple matches for oldString. Provide more surrounding lines in oldString to identify the correct match." Either provide a larger string with more surrounding context to make it unique or use `replaceAll` to change every instance of `oldString`.
8+
- The edit will FAIL if `oldString` is not found in the file with an error "Could not find oldString in the file. It must match exactly, including whitespace, indentation, and line endings."
9+
- The edit will FAIL if `oldString` is found multiple times in the file with an error "Found multiple matches for oldString. Provide more surrounding context to make the match unique." Either provide a larger string with more surrounding context to make it unique or use `replaceAll` to change every instance of `oldString`.
1010
- Use `replaceAll` for replacing and renaming strings across the file. This parameter is useful if you want to rename a variable for instance.

0 commit comments

Comments
 (0)