Title: BUG:sometime new message and old message all in same id · Issue #205 · coder/agentapi · GitHub
Open Graph Title: BUG:sometime new message and old message all in same id · Issue #205 · coder/agentapi
X Title: BUG:sometime new message and old message all in same id · Issue #205 · coder/agentapi
Description: An occasional bug in the agentapi caused old message history and new user messages to be mixed together in the same SSE message body. data: {"id":68,"role":"agent","message":"old + new"} Note! The "old + new" above "old" refers to a very...
Open Graph Description: An occasional bug in the agentapi caused old message history and new user messages to be mixed together in the same SSE message body. data: {"id":68,"role":"agent","message":"old + new"} Note! The ...
X Description: An occasional bug in the agentapi caused old message history and new user messages to be mixed together in the same SSE message body. data: {"id":68,"role":"agent",&qu...
Opengraph URL: https://github.com/coder/agentapi/issues/205
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"BUG:sometime new message and old message all in same id","articleBody":"\nAn occasional bug in the agentapi caused old message history and new user messages to be mixed together in the same SSE message body.\n\ndata: {\"id\":68,\"role\":\"agent\",\"message\":\"old + new\"}\n\nNote!\n\nThe \"old + new\" above\n\n\"old\" refers to a very, very large collection of historical messages, not a streaming update of the screen.\n\n\n\n\n\nBug 1: A \"ghost update\" at the start of sendMessage\n\n Before appending the user message, sendMessage calls updateLastAgentMessageLocked once:\n\n // pty_conversation.go:386-391\n c.lock.Lock()\n screenBeforeMessage := c.cfg.AgentIO.ReadScreen()\n now := c.cfg.Clock.Now()\n c.updateLastAgentMessageLocked(screenBeforeMessage, now) // ← user msg NOT yet appended\n c.writingMessage = true\n c.lock.Unlock()\n\n At this point c.messages still ends with the agent message (id=68), so:\n\n // line 324\n shouldCreateNewMessage := len(c.messages) == 0 ||\n c.messages[len(c.messages)-1].Role == ConversationRoleUser\n // = false ← overwrites existing slot instead of creating a new one\n\n Any content change here writes into messages[68] — no new ID is created.\n\n ---\n Bug 2: screenDiff set-based matching bleeds historical content in\n\n // diff.go:32-38\n firstNonMatchingLine := len(newLines)\n for i, line := range newLines[dynamicHeaderEnd+1:] {\n if !oldLinesMap[line] { // ← set lookup by line content\n firstNonMatchingLine = i\n break\n }\n }\n newSectionLines := newLines[firstNonMatchingLine:] // ← takes EVERYTHING from that line onward\n\n oldLinesMap is a content-based set of old screen lines (unordered). The algorithm finds the first line not in that set, then takes all subsequent lines — including any historical content that reappears later on screen.\n\n When the terminal accumulates output without clearing, the new screen looks like:\n\n [historical response lines] ← some line here is NOT in oldLinesMap\n [new user input echo]\n [new agent response lines]\n\n So screenDiff returns \"historical_content\\nnew_content\" as a single blob.\n\n ---\n How the two bugs combine\n\n ┌─────────────────────────────────────────────────────────────┬──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┬─────────────────────────────────────────────┐\n │ Step │ State │ Effect │\n ├─────────────────────────────────────────────────────────────┼──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼─────────────────────────────────────────────┤\n │ sendMessage starts, calls updateLastAgentMessageLocked │ writingMessage=false, last message is agent (id=68), shouldCreateNewMessage=false │ Any content difference overwrites id=68 │\n ├─────────────────────────────────────────────────────────────┼──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼─────────────────────────────────────────────┤\n │ screenDiff(screenBeforeLastUserMessage, currentScreen) runs │ New screen contains old response lines + new response lines; set-match finds a boundary in the middle of old content │ Returns \"historical + new content\" │\n ├─────────────────────────────────────────────────────────────┼──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼─────────────────────────────────────────────┤\n │ Write path │ shouldCreateNewMessage=false → takes else branch: messages[last] = conversationMessage │ id=68 now contains both old and new content │\n └─────────────────────────────────────────────────────────────┴──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┴─────────────────────────────────────────────┘\n\n Only after this does the user message get appended (id=69), and the snapshot loop eventually creates a proper new agent message (id=70). But id=68 has already been corrupted and emitted via SSE.\n\n ---\n Fix directions\n\n Fix 1 — Remove the redundant updateLastAgentMessageLocked call at the start of sendMessage. The screen is stable at this point (that's why stableSignal fired), so this call produces no useful work:\n\n // In sendMessage, remove this line from the first lock block:\n - c.updateLastAgentMessageLocked(screenBeforeMessage, now)\n\n Fix 2 — Replace the set-based screenDiff with a positional diff. Instead of \"find the first line whose content isn't in a set\", compare line-by-line from the end of the old screen forward, so historical content that reappears mid-screen doesn't shift\n the boundary:\n\n // Current (buggy): content-set lookup\n if !oldLinesMap[line] { firstNonMatchingLine = i; break }\n\n // Better: compare the tail of newLines against the tail of oldLines positionally,\n // or use a suffix/LCS approach so repeated content doesn't cause false boundaries.","author":{"url":"https://github.com/mrchanglinux-spec","@type":"Person","name":"mrchanglinux-spec"},"datePublished":"2026-03-20T16:10:25.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":3},"url":"https://github.com/205/agentapi/issues/205"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:001cd80d-d5f2-30e2-3f09-6e2ef2861bfa |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B23E:297ED:63396E:8FFCD6:6A4E0C3C |
| html-safe-nonce | f6e169e27e7a47e639bb11ee7df76730828a4403d693bc6a8ebc2a25f9e3f13c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMjNFOjI5N0VEOjYzMzk2RTo4RkZDRDY6NkE0RTBDM0MiLCJ2aXNpdG9yX2lkIjoiNTIxMTkzNDkwODQ0NDcwNzkwMCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | c35d0a6209b5ab3710165c6ee3dfc651479279c5c959fc71c64110c5c3472d07 |
| hovercard-subject-tag | issue:4108743790 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/coder/agentapi/205/issue_layout |
| twitter:image | https://opengraph.githubassets.com/fb058b54968cbdb32626a736e4f93c8acfd86521da71ca555213a56c7d88221f/coder/agentapi/issues/205 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/fb058b54968cbdb32626a736e4f93c8acfd86521da71ca555213a56c7d88221f/coder/agentapi/issues/205 |
| og:image:alt | An occasional bug in the agentapi caused old message history and new user messages to be mixed together in the same SSE message body. data: {"id":68,"role":"agent","message":"old + new"} Note! The ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mrchanglinux-spec |
| hostname | github.com |
| expected-hostname | github.com |
| None | df0492960db29b4938cb72070351d6b1d0c6c0767b27ceb8394bbf4fcc0223c6 |
| turbo-cache-control | no-preview |
| go-import | github.com/coder/agentapi git https://github.com/coder/agentapi.git |
| octolytics-dimension-user_id | 95932066 |
| octolytics-dimension-user_login | coder |
| octolytics-dimension-repository_id | 961832048 |
| octolytics-dimension-repository_nwo | coder/agentapi |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 961832048 |
| octolytics-dimension-repository_network_root_nwo | coder/agentapi |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 51470c353b8a1f52a88d3e3cc2014b17ab8cc1ce |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width