Repository navigation
Description
Description
Title: User messages are duplicated — createUserMessage() runs outside the concurrency gate
Environment
- OpenCode version: latest
devbranch - OS: Windows 11
Problem
User messages are duplicated in sessions. The same prompt is persisted as multiple separate messages (different messageIDs) and processed by the AI multiple times. This wastes API tokens/cost and produces confusing duplicate AI responses.
Evidence (from a single 29-minute session)
Session stats: 261 total messages in 29 minutes. Only ~8 unique intents — the rest are duplicates.
Category 1: Long messages duplicated within seconds
| Time (UTC) | Interval | Content |
|---|---|---|
10:07:24.969Z |
— | "Check session X, analyze why messages are duplicated?" |
10:07:37.885Z |
+13s | IDENTICAL prompt, new messageID |
| Time (UTC) | Interval | Content |
|---|---|---|
10:17:24.681Z |
— | "Need to analyze — why are messages being sent multiple times?" |
10:17:26.124Z |
+1.4s | IDENTICAL prompt, new messageID |
10:17:56.807Z |
+30s | THIRD copy of the same prompt |
Category 2: "continue" flood — sub-second duplication at scale
While the AI was busy processing, the user typed "continue" repeatedly to scroll through truncated output. Every single keystroke was persisted as a separate user message. Dozens were duplicated within the same second:
10:20:34.650 user: "continue"
10:20:34.985 user: "continue" ← 335ms apart, different messageID
10:20:36.321 user: "continue"
10:20:36.640 user: "continue" ← 319ms apart, different messageID
10:20:37.696 user: "continue"
10:20:38.032 user: "continue" ← 336ms apart, different messageID
10:20:40.828 user: "continue"
10:20:41.188 user: "continue" ← 360ms apart, different messageID
10:20:42.519 user: "continue"
10:20:42.917 user: "continue" ← 398ms apart, different messageID
... ~100 more "continue" messages follow over 5 minutes ...
This flood alone accounts for ~85% of the 261 messages. Each "continue" triggered a separate prompt() → createUserMessage() → queued runLoop() call, creating a cascading backlog.
What the session should have looked like
Without this bug: ~8 user messages, ~8 assistant responses, ~16 total. Instead: 261 messages, most of which are duplicates that should never have been persisted.
Root Cause
createUserMessage() is called before the ensureRunning() concurrency gate. Every prompt() call unconditionally creates and persists a new user message regardless of session state.
The actual flow:
HTTP POST /:sessionID/message
→ SessionPrompt.prompt() [prompt.ts:1245]
→ sessions.get(sessionID) [prompt.ts:1247]
→ createUserMessage(input) [prompt.ts:1249] ← ⚠️ UNCONDITIONALLY persists
→ sessions.touch(sessionID) [prompt.ts:1250]
→ loop({ sessionID }) [prompt.ts:1262]
→ state.ensureRunning(sessionID, ...) [prompt.ts:1507] ← ⚠️ concurrency gate is HERE
→ Runner.ensureRunning(work) [runner.ts:115]
→ case "Running": awaitDone() // QUEUES [runner.ts:120-122]
→ case "Idle": startRun(work) [runner.ts:131-135]
→ runLoop(sessionID) ← AI processing starts here
The gap (exact lines):
prompt.ts:1249:const message = yield* createUserMessage(input)— outside the gateprompt.ts:1507:return yield* state.ensureRunning(input.sessionID, ...)— gate is deep insideloop()runner.ts:120-122: When alreadyRunning,ensureRunningqueues instead of rejecting → the second call WILL execute later
Race condition trace:
T0: Session is Idle
T1: Call #1 → prompt() → createUserMessage(M1) ✓ → loop() → ensureRunning → startRun
T2: Call #2 → prompt() → createUserMessage(M2) ✓ → loop() → ensureRunning → [Running] → queues
T3: Call #1 finishes, runner → Idle
T4: Call #2 resumes → ensureRunning → [Idle] → startRun → processes M2 → DUPLICATE
M2 was never supposed to be processed. But because createUserMessage runs before the gate, M2 was:
- Persisted to storage (cannot be rolled back)
- Eventually picked up by the queued runner as a "new" user message
Why this is severe
- API cost multiplication: Each duplicate triggers a full AI processing loop. The "继续" flood of ~200 duplicate messages each consumed tokens for the round-trip — hundreds of dollars in API credits wasted on identical prompts.
- Data integrity corruption: A session that should have 16 messages has 261. History becomes unreadable. Revert, fork, and summary features all operate on corrupted data.
- Cascading spiral: Duplicates get queued → each takes time → AI appears "stuck" → user sends more "继续" → more duplicates → session becomes permanently broken.
- No client-side fix possible: Even with frontend debounce, network retries, proxy replays, or HTTP-level idempotency keys can trigger this server-side gap independently.
- No defense at the HTTP route: The
POST /:sessionID/messagehandler (session.ts:852-897) never callsassertNotBusy(). It blindly invokessvc.prompt().
What's missing
| Defense | Current state in codebase |
|---|---|
| Concurrency gate BEFORE message creation | Gate is after (prompt.ts:1249 vs prompt.ts:1507) |
HTTP route assertNotBusy() check |
Route handler has no busy check (session.ts:852-897) |
Idempotency via messageID |
messageID accepted as input but never validated for duplicates |
| Message content dedup | Not implemented |
| Runner rejects concurrent calls | Runner queues them (runner.ts:120-122) |
Suggested fixes (priority order)
Fix 1 (Recommended): Gate message creation
Move createUserMessage to execute after successfully entering ensureRunning. In prompt.ts, pass the raw input into loop() / runLoop() and create the message only when the runner confirms the session is free.
Fix 2: Add assertNotBusy() at the HTTP route
In POST /:sessionID/message handler (session.ts:852), call SessionRunState.assertNotBusy(sessionID) before calling svc.prompt(). Return 409 Conflict if the session is already processing.
Fix 3: Validate messageID for idempotency
If the caller provides a messageID in the request, check whether a message with that ID already exists in the session before creating a new one.
Related files
| File | Role in bug |
|---|---|
packages/opencode/src/session/prompt.ts |
createUserMessage at L1249 (before gate); loop() enters gate at L1507 |
packages/opencode/src/effect/runner.ts |
ensureRunning queues instead of rejecting at L120-122 |
packages/opencode/src/session/run-state.ts |
Has assertNotBusy (L71) but it's never called before prompt() |
packages/opencode/src/server/routes/instance/session.ts |
HTTP route at L852-897 has no busy check |
Plugins
No response
OpenCode version
No response
Steps to reproduce
No response
Screenshot and/or share link
No response
Operating System
No response
Terminal
No response
Activity
github-actions commentedon Apr 28, 2026
This issue doesn't fully meet our contributing guidelines.
What needs to be fixed:
_No response_) — please provide concrete steps to reproduce the issuePlease edit this issue to address the above within 2 hours, or it will be automatically closed.
This issue might be a duplicate of existing issues. Please check:
If you believe this was flagged incorrectly, please let a maintainer know.
github-actions commentedon Apr 28, 2026
This issue has been automatically closed because it was not updated to meet our contributing guidelines within the 2-hour window.
Feel free to open a new issue that follows our issue templates.