Skip to content

AI-3902: a money refusal survives a session reopen - #2556

Merged
kanat merged 9 commits into
mainfrom
kanat/ai-3902-money-refusal-survives-reopen
Nov 26, 2026
Merged

kanat merged 9 commits into
mainfrom
kanat/ai-3902-money-refusal-survives-reopen

Conversation

@kanat

@kanat kanat commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator

Why

A scheduled reply refused for money must end its run failed, final, under the money code (AI-2055, AI-3183). #2278 made the wait read how the turn ended from the conversation (FollowUpFailure), but the conversation kept the failure in memory only (commandRecord.Failure, "kept in memory only"). A session reopens after a router drain or restart, or after its 5-minute unwatched grace. After a reopen, commandsIn rebuilt the records from the Chat messages without the failure. A retry of a reply refused for money then recorded reply_failed (text channels) or delivered (conversation channel), although the user got nothing (AI-3902, #2278 R1.4).

The placed-call path (carryOutAnswered, AI-3904) is a separate PR.

How it works

first attempt (router A)                   retry after a reopen (router B)
──────────────────────────                 ───────────────────────────────
agent.Error{money refusal}                 carryOn: session unknown here
  └─ Conversation.Observe                    └─ conversation opened from Chat
       ├─ record.Failure = err (memory)           └─ messageFromWire
       └─ m.refusal = err                              └─ support_money_refusal
            └─ sendWith: Chat custom                        -> policy.Refusal(code, unchecked)
               support_money_refusal =                 └─ commandsIn: record.Failure
               {code, unchecked}                  └─ turnRefusal / refusedAgain
attempt cut short (drain, restart)                     -> unpaid: money code, Final
                                                          = !policy.Transient

What is kept, and why:

Kept Why Where it is read
code (policy.Code) the run records only the code deliveryReason, scheduled_delivery.go:374; scheduler Error: reason, scheduler.go:452
unchecked (policy.Transient) decides final or retried; insufficient_credits is transient when the balance is unread (policy/money.go:105) unpaid, scheduled_delivery.go:422
not the text no run field and nothing a user sees carries it; only the worker's warn log line shows it scheduler.go:444

A message without the field (any message written before this change, or a failure not about money) reads back with no failure, as before.

Changes

  • policy.Refusal(code, unchecked): the refusal of the kind Code names, for a refusal read back from where it was kept.
  • conversation: Observe keeps a money refusal on the failed reply; sendWith writes it as the support_money_refusal custom field; messageFromWire reads it back; commandsIn restores Failure for a follow-up and for a person's command. The Message field is unexported, so no API or openapi.yaml change, and no migration.

Validation

Path (brief's table) Test Base 92fcbb8 Head
conversation, first attempt, refused for money existing TestAConversationTaskWhoseTurnIsRefusedForMoneyIsNotRetried pass pass
conversation, retry after a reopen, refused for money TestAConversationTurnRefusedForMoneyBeforeAReopenFailsUnderTheMoney fail: delivered failed, final, insufficient_credits
text (SMS), retry after a reopen, refused for money TestATextTurnRefusedForMoneyBeforeAReopenFailsUnderTheMoney fail: reply_failed failed, final, insufficient_credits
transient money check before a reopen TestAConversationTurnRefusedUncheckedBeforeAReopenRunsAgainOnItsRetry fail: delivered with no rerun retried, then the rerun completes
failed not for money before a reopen (control) TestATextTurnFailedNotForMoneyBeforeAReopenFailsAsAReplyThatFailed reply_failed reply_failed
completed before a reopen (control) TestATextTurnCompletedBeforeAReopenIsTexted texted texted
  • The reopen is forced: the turn runs on the suite's router and its session is stopped. A second router with a conversation service of its own (restartedNode) then carries the session on. That router holds nothing in memory, so it reads the conversation from Chat, as after a restart, a drain to a peer or the end of the grace. A worker that takes the run over after the first attempt was cut short reads no reason from the row (Run.Error nil), which these tests use.
  • Thread delivery reads the same readBackReply / refusedAgain (scheduled_thread_delivery.go:93,108) as the SMS test.
  • internal/conversation MoneyRefusalSuite: each of the 7 money kinds, checked and unchecked, reads back after a reopen. A person's command reads back too. A failure not about money keeps no field. A table of field shapes (none, empty, a bare code, no flag, a flag of text, a number code, an unknown code, an empty code) reads as no refusal.
  • internal/policy: Refusal round trips every code, checked and unchecked, and refuses unknown codes.
  • Mutations: each of the 13 changed conditions, writes and reads was mutated, and a test failed for each one.

🤖 Generated with Claude Code

https://claude.ai/code/session_01EZsRxWUwySyG1JAn9T7GpE

kanat and others added 5 commits November 26, 2026 11:59
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZsRxWUwySyG1JAn9T7GpE
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZsRxWUwySyG1JAn9T7GpE

@kanat kanat left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VERDICT: GO

[security R1]: one Nit, no Blocker and no Should fix.

Who can read support_money_refusal (author QUESTION, suspicion 1). It can stay in the reply's Chat custom data, and router-only storage is not needed. The field holds only a fixed code from policy.Code and one bool. It holds no amount and no error text. A device already gets more than this for the same refusal: the HTTP answer carries the code and the message (api/money.go moneyError/refusedRequest), and the session socket's error frame carries the full message (sessionws.go refusalText). refused is a member-visible flag of the same kind. Note: in a group or thread channel, every member, not only the turn's user, can read the code. That is acceptable.

Forging or erasing it (suspicion 2). On a reopen, the field is read only from a reply the agent wrote (readsAsWritten, in historyIn and in the receipt read), and only off assistant records (commandsIn). A member's message that carries the agent's fields plus a forged refusal reads back as no refusal. A member's later message saying completed does not erase a real refusal. A scratch-copy test as a member confirms this on this head, and that test fails when the author check is removed. The value is also limited to a fixed list (moneyRefusalOf + policy.Refusal).

Checked and fine: the error-text grep finds 0 hits. A rollback to the previous build ignores the extra custom key and falls back to the base behaviour. The decode of legacy messages cannot set the unexported field. There is no migration. Mutations: 4 checked, 0 survived (the flag check, the unknown-code case of policy.Refusal, the Observe money-only check, and readsAsWritten).

// What the field holds is read back as a refusal only when it is one the router writes: any other
// shape, type or code reads as no refusal, so a reply never fails a delivery under a code no
// refusal gave.
func (s *MoneyRefusalSuite) TestOnlyAKeptRefusalReadsAsOne() {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Nit] S1.1 Nothing in this suite pins that a channel member's message carrying support_money_refusal under a follow-up's command id is ignored after a reopen. Today readsAsWritten covers it, and TestAMembersMessageCarryingTheAgentsFieldsNamesNoCommand fails if that check goes. A row here would tie the new field to that guard. Suggested row: a member writes a copy of the reply with the field set, after a reply that failed for another reason, then the conversation reopens. Expect FollowUpFailure nil. Also: a member writes completed with no field after a real refusal. Expect the refusal kept. Optional.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 5d61d2d: TestAMembersForgedRefusalIsNotRead and TestAMembersCopyOfARefusedReplyDoesNotEraseTheRefusal (money_refusal_test.go). Mutation: readsAsWritten returning true fails both.

@kanat kanat left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VERDICT: GO

Reviewed 7dfe085 (it already contains main at 21b2551). CI: the draft skips go and integration; they run at gh pr ready.

[Nit] R1.1 .claude/skills/scheduled-tasks/SKILL.md:211 still says the error "is kept in memory only" and that a retry after a reopen records reply_failed or delivered ("known gap"). This PR closes that gap. The brief gives the SKILL lines to AI-3904, so AI-3904 must drop that sentence. Not a blocker here.

Rulings on the author's open points:

  • Thread retry after a reopen, no test: this goes to a ticket, not this PR. The PR changes no thread-only code. A reviewer probe ran deliverOnThread on a restarted node after a failed st_run. It fails under insufficient_credits, final, on this head, and records reply_failed on base. Add the test once AI-3903 (deliverOnThread) lands.
  • Regenerated reply gets no refusal back: not a finding. The only reader of FollowUpFailure is turnRefusal (scheduled_delivery.go:603), and it never asks for a regenerated command id. The Failure comment says so.
  • support_money_refusal in Chat custom data: acceptable. It holds a code and a flag only, and the socket already sends the refusal text (sessionws.go:1049). Its only reader is messageFromWire -> commandsIn. Message.refusal is unexported, so it is not in the socket, history or snapshot JSON. Model context, cards and omnichannel never read it. A branch resend copies it only under new ids, which resent() adds without overwriting. A message without the field reads back as on base. An old build ignores the extra top-level key (messageFromWire on main reads keys by name).

Checked and sound:

  • The 3 new regression tests fail on base (delivered, no error, reply_failed). The 2 controls pass on base and head.
  • TestTelnyxUnpaidSuite, conversation and policy unit and integration all pass on the head. Vet and gofmt are clean.
  • 7 more mutations, 7 killed: refusal set after finish, field written or read on any failed reply, any error kept, fixed code, stored flag ignored, spend check forced unchecked.
  • The retry after a reopen keeps the same code, and Final = !policy.Transient. The text is not needed: the run records only ReasonOf.

// What the field holds is read back as a refusal only when it is one the router writes: any other
// shape, type or code reads as no refusal, so a reply never fails a delivery under a code no
// refusal gave.
func (s *MoneyRefusalSuite) TestOnlyAKeptRefusalReadsAsOne() {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Nit] R1.2 This is a table test with an if tc.kind == nil branch in its body. The go-testing skill says to avoid table tests and write one test per behaviour ("reads as no refusal" and "reads back as its kind" are two). This is for a ticket, not this PR.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 5d61d2d: split into TestAShapeTheRouterNeverWritesReadsAsNoRefusal and TestAShapeTheRouterWritesReadsAsItsRefusal; same cases and assertions, no branch in either body.

@github-actions github-actions Bot added the docs label Nov 26, 2026
@kanat
kanat marked this pull request as ready for review November 26, 2026 18:02
@kanat
kanat merged commit d3bd88e into main Nov 26, 2026
15 checks passed
@kanat
kanat deleted the kanat/ai-3902-money-refusal-survives-reopen branch November 26, 2026 21:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant