Skip to content

Commit 4fada35

Browse files
anandgupta42claude
andauthored
fix: address Sentry review findings from PR #144 (#147)
- Use unique `MessageID`/`SessionID` instead of hardcoded `"enhance-prompt"` string for synthetic LLM calls (prevents future API validation breakage) - Add `enhancingInProgress` guard to manual enhance handler to prevent concurrent enhancement race with auto-enhance on submit - Fix auto-enhance early `return` that orphaned sessions: now uses latest user text and continues submission instead of silently abandoning it Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent c189028 commit 4fada35

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

packages/opencode/src/altimate/enhance-prompt.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@ import { Agent } from "@/agent/agent"
55
import { Config } from "@/config/config"
66
import { Log } from "@/util/log"
77
import { MessageV2 } from "@/session/message-v2"
8+
import { MessageID, SessionID } from "@/session/schema"
89

910
const ENHANCE_NAME = "enhance-prompt"
1011
const ENHANCE_TIMEOUT_MS = 15_000
11-
// MessageV2.User requires branded MessageID/SessionID types, but this is a
12-
// synthetic message that never enters the session store — cast is safe here.
13-
const ENHANCE_ID = ENHANCE_NAME as any
1412

1513
const log = Log.create({ service: ENHANCE_NAME })
1614

@@ -105,8 +103,8 @@ export async function enhancePrompt(text: string): Promise<string> {
105103
}
106104

107105
const user: MessageV2.User = {
108-
id: ENHANCE_ID,
109-
sessionID: ENHANCE_ID,
106+
id: MessageID.ascending(),
107+
sessionID: SessionID.descending(),
110108
role: "user",
111109
time: { created: Date.now() },
112110
agent: ENHANCE_NAME,
@@ -124,7 +122,7 @@ export async function enhancePrompt(text: string): Promise<string> {
124122
tools: {},
125123
model,
126124
abort: controller.signal,
127-
sessionID: ENHANCE_ID,
125+
sessionID: user.sessionID,
128126
retries: 2,
129127
messages: [
130128
{

packages/opencode/src/cli/cmd/tui/component/prompt/index.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,10 @@ export function Prompt(props: PromptProps) {
207207
enabled: !!store.prompt.input,
208208
onSelect: async (dialog) => {
209209
if (!store.prompt.input.trim()) return
210+
if (enhancingInProgress) return
210211
dialog.clear()
211212
const original = store.prompt.input
213+
enhancingInProgress = true
212214
toast.show({
213215
message: "Enhancing prompt...",
214216
variant: "info",
@@ -241,6 +243,8 @@ export function Prompt(props: PromptProps) {
241243
variant: "error",
242244
duration: 3000,
243245
})
246+
} finally {
247+
enhancingInProgress = false
244248
}
245249
},
246250
},
@@ -625,9 +629,11 @@ export function Prompt(props: PromptProps) {
625629
enhancingInProgress = true
626630
toast.show({ message: "Enhancing prompt...", variant: "info", duration: 2000 })
627631
const enhanced = await enhancePrompt(inputText)
628-
// Discard if user changed the prompt during enhancement
629-
if (store.prompt.input !== inputText) return
630-
if (enhanced !== inputText) {
632+
// If user changed the prompt during enhancement, skip enhancement
633+
// but continue submission with the original text (don't abandon it)
634+
if (store.prompt.input !== inputText) {
635+
inputText = store.prompt.input
636+
} else if (enhanced !== inputText) {
631637
inputText = enhanced
632638
setStore("prompt", "input", enhanced)
633639
}

packages/opencode/test/altimate/enhance-prompt.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@ mock.module("@/session/message-v2", () => ({
6060
MessageV2: {},
6161
}))
6262

63+
let idCounter = 0
64+
mock.module("@/session/schema", () => ({
65+
MessageID: {
66+
ascending: () => `msg-${++idCounter}`,
67+
},
68+
SessionID: {
69+
descending: () => `session-${++idCounter}`,
70+
},
71+
}))
72+
6373
// Import after mocking
6474
const { enhancePrompt, isAutoEnhanceEnabled } = await import("../../src/altimate/enhance-prompt")
6575

0 commit comments

Comments
 (0)