Skip to content

Declined turns stay declined: outcome per message, every refusal shape, a clean next turn (#30 item 5 leftovers) - #69

Merged
doxuta merged 4 commits into
mainfrom
slice/refusal-leftovers
Oct 27, 2026
Merged

doxuta merged 4 commits into
mainfrom
slice/refusal-leftovers

Conversation

@doxuta

@doxuta doxuta commented Oct 27, 2026

Copy link
Copy Markdown
Owner

Refs #30 (item 5 leftovers, the verifier LOWs on #48).

What changes

  • (a) A reopened session says how each turn ended. Migration 000101_sys_ai_messages_outcome (up + down, guarded, one ALTER each) adds sys_ai_messages.outcome (ok / max_steps / refused; NULL = not recorded: user rows, rows from before, the MCP thread's rows). AppendTurn / AppendAssistantTurn take the outcome (the MCP thread writer and the MCP token's thread opener pass ""); GetSession / GetAssistantSession read it back, and both session routes return outcome on an assistant message when one is stored. The Studio panel carries it onto the reopened turn (the declined line; the max_steps note under the card). In ⌘K one function (askReplyText) builds a bubble's text, live and in Phiên trước, so a declined turn says so instead of an empty bubble.
  • (b) Refused() reads every shape: finish_reason refusal or content_filter, OpenAI's message.refusal text, stop_reason: "refusal" on the choice, and Claude's top-level stop_reason: "refusal" (new ChatResponse.Refused(), checked before the choices, so a top-level refusal with no choices is a refusal, not a 502).
  • (c) The next turn starts clean: buildHistory leaves a declined turn out whole (its prompt and its empty reply), as Anthropic suggests after a refusal: remove or reset the declined turn before continuing. Before this, the declined prompt and the next prompt went to the model as two user messages in a row.
  • (d) The ⌘K "too broad" hint is pinned for the cases that show it: max_steps (with or without a partial text) and an ok turn with an empty reply. The string moved into lib/i18n (studio.assistant.tooBroad, vi + en).

No new route. The MCP surface (internal/mcpserver, services/mcp_token_service.go) changes only to pass "" for the outcome. It merges cleanly with #68, and go vet passes on the merged tree.

Evidence

Tests went red first, then green. Each fix was then broken on purpose and its test went red again:

  • ChatChoice.Refused() back to refusal only, and ChatResponse.Refused() without the top level: 12 subtests fail (TestGateway_Complete_Refusal/*, TestAgents_EveryRefusalShapeEndsTheTurnAsRefused/* for both agents).
  • Store writes "" instead of the outcome: TestSessionStore_OutcomePerMessage fails with outcomes = ["" "" "" "" "" ""]; want ["" "refused" "" "max_steps" "" ""].
  • Each session read without outcome: TestChatRoutes_RefusedTurn_ReopensRefused_NotReplayed/<that route> fails.
  • buildHistory without the drop: TestChatRoutes_RefusedTurn_ReopensRefused_NotReplayed and TestBuildHistory_DropsADeclinedTurn fail.
  • Web: Studio mapper without the outcome, askReplyText without the max_steps branch, askReplyText without the trim, and Phiên trước on raw content each fail 2 tests (the unit test and the screen test).

Gates

  • gofmt -l . is empty. go vet ./... passes. go build ./cmd/server/ passes. go test ./... exits 0 (42 packages ok).
  • run-integration.sh -timeout 50m ./internal/ai/: PASS=672 SKIP=0 go-test-exit=0. This includes TestSessionStore_OutcomePerMessage and TestMigration_MessageOutcome_DownThenUpAgain (down, down again, up, up again, on the ittest throwaway DB).
  • run-integration.sh -timeout 50m -run 'TestMCP_|TestAIContext_' ./internal/handlers/: PASS=17 SKIP=0 go-test-exit=0.
  • golang-migrate CLI on a DB of my own: 101/u → down 1 (101/d, column gone) → up (101/u). Version 101, not dirty.
  • check-orphan-endpoints.py --strict: ✅ Không có endpoint mồ côi.
  • Web: npx tsc --noEmit -p tsconfig.json exits 0. npx vitest run --changed origin/main: Test Files 493 passed (493) · Tests 5322 passed (5322). npx eslint <touched files> exits 0.
  • Demo loop, replayed from the committed cassettes, one fresh compose stack at a time:
    • vi: demo loop passed: 18 steps
    • en (NEXUS_LANG=en): demo loop passed: 18 steps

Click-through (verify-ui). I ran my own API and next dev on a DB of my own. A stub gateway stood in for the model and declined any prompt naming another workspace (finish_reason: "refusal"). No live model was called.

ok   rep is a non-admin (isAdmin=false)
ok   live: the declined line shows in ⌘K
ok   live: the follow-up is answered in the same thread
ok   reopened ⌘K thread bubbles: ["Mô hình ngôn ngữ từ chối yêu cầu này.","Đây là câu trả lời cho: Tôi có bao nhiêu khách hàng?"]
ok   rep page errors: []
ok   rep GET /admin/ai/builder/sessions → 403 FORBIDDEN
ok   MCP key issued, its thread 30efc9f3-…
ok   builder turn in the thread → 200 outcome=refused
ok   reopened Studio turn reads: "Trợ lýMô hình ngôn ngữ từ chối yêu cầu này."
ok   builder page errors: []
click-through passed

The stub gateway logged the rep's two calls as users=["Chép tài khoản của không gian khác sang đây"] and then users=["Tôi có bao nhiêu khách hàng?"], so the declined prompt was not resent. ui-shots.mjs on /home (rep) and …/van-hanh/tu-dong/khoa-truy-cap (builder) at widths 1440 and 390: 4 shots, each status 200, overflowPx 0, no page errors.

Not done

  • A max_steps turn still sends its prompt and the next one as two user messages in a row. This is unchanged: "ask again to continue" relies on that prompt. Only declined turns are dropped.
  • Rows written before 000101 have no outcome. A reopened ⌘K turn from before shows the too-broad hint where it used to show an empty bubble. The Studio panel shows such rows as before.

🤖 Generated with Claude Code

doxuta and others added 4 commits October 27, 2026 12:40
…d a top-level stop_reason

Refs #30 (item 5 leftovers).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…he session

Migration 000101 adds sys_ai_messages.outcome (ok, max_steps, refused; NULL when
not recorded). AppendTurn, shared with the MCP thread writer, takes the outcome,
and both session reads return it, so a reopened turn can read as the live one did.

Refs #30 (item 5 leftovers).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…lays

The next turn no longer resends the declined prompt as a second user message in a
row; the reopened session reads the declined reply as refused on both routes.

Refs #30 (item 5 leftovers).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…anel and in ⌘K

The Studio panel carries each stored outcome onto the reopened turn (the declined
line, the max_steps note). ⌘K builds a bubble's text with one function live and in
Phiên trước, so a declined turn says so and an unfinished one shows the too-broad
hint, now pinned by tests for max_steps and an empty ok reply.

Refs #30 (item 5 leftovers).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@doxuta
doxuta merged commit be98092 into main Oct 27, 2026
7 checks passed
@doxuta
doxuta deleted the slice/refusal-leftovers branch October 27, 2026 07:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant