Skip to content

AI-3511: fix five small wrong results in spend, prices, activity and call legs - #2270

Merged
Loschcode merged 8 commits into
mainfrom
laurentschaffner/ai-3511-small-wrong-results-spend-other
Nov 24, 2026
Merged

Loschcode merged 8 commits into
mainfrom
laurentschaffner/ai-3511-small-wrong-results-spend-other

Conversation

@Loschcode

@Loschcode Loschcode commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Why

https://linear.app/stream/issue/AI-3511/small-wrong-results-spend-other-cancelling-a-price-already-in-force

Five small wrong results from the data layer review. Each one reproduced on current main, and each is fixed in its own commit.

What changed

  • Spend "other" (store/request.go, store/shard_merge.go, api/server.go).
    • The remainder row now carries a new optional field, other: true, and keeps value: "other".
    • A cost label whose value is other is now a row of its own, without the flag, instead of being merged into the remainder.
    • The field is omitempty and optional in the spec, so a client reading an older router still parses its rows. An old client that picks the remainder by value == "other" will now see two rows with that value when a label really is other. Before, it saw them summed into one.
    • Regenerated openapi.yaml and the Go, Python, JS, .NET, PHP, Ruby and Rust SDKs. Swift's generator is not available locally, but the Swift client has no SpendBucket, and its CI check passes.
    • Added an SDK note under .claude/skills/sdk/notes/.
    • The Volt dashboard still keys on value == "other". Follow-up: AI-3653.
  • Cancelling a price already in force (store/price_change.go).
    • Cancel now checks EffectiveAt against clock_timestamp(), read once the price list's lock is held, and writes the same time as cancelled_at.
    • The price_changes trigger that backs this up still uses now(), so it has the same gap. The new test shows the trigger lets such a cancel through when the Go check does. Fixing the trigger needs a migration, which I left out while chore(store): squash migrations into one per set #2247 squashes them. Follow-up: AI-3654, which also covers Insert and syncPrices, both still stamped from now() after the same lock wait.
  • Activity counted twice (store/activity.go, activity/activity.go).
    • A write whose COMMIT was sent and got no answer is now store.ErrActivityUnsure, and the recorder drops and logs it instead of keeping its deltas. A dropped write holds nothing back, so write reports true and a full record is not left waiting for the next tick.
    • Commit failures that did not land are still retried. database/sql answers a context that ended before the commit with ErrTxDone or the context's error, without sending COMMIT; pgdriver sends COMMIT on context.Background(), so neither comes from a COMMIT on the wire. An error the server answered (pgdriver.Error) is a commit it refused.
    • The trade-off: an ambiguous commit now undercounts, by at most one write's rows, instead of double-counting.
    • A write that fails before its commit wrote nothing and is still written again (TestAFailedWriteIsWrittenByTheNextOne).
    • I chose this over a batch id because a batch id needs a new table, so a migration.
  • 59 or 61 seconds (store/call_leg.go, joinTaken).
    • An inbound leg opens started at the call's arrival. A join delivered after the leg already kept that start (Connect uses COALESCE).
    • A join made after the arrival but delivered before it moved the start to the join, so the same call billed a second less.
    • The kept join now starts only a leg that has not started yet. Outbound legs open unstarted, so they are unchanged.
  • Prices drift and BYOK (store/request.go, vendorCosts).
    • BYOK rows are left out of the drift query. They are priced with no cost at the vendor's rates, so each one read as 100% under.
    • Rows written before the byok column existed read false and stay in. The default 168 h window ages them out.

Tests

  • api TestALabelValuedOtherIsKeptApartFromTheRest: a spend query grouped by a tag with a value other returns it apart from the remainder.
  • store TestACancelThatWaitedPastTheChangesTimeIsRefused: a cancel whose transaction starts before the change's time and waits on the lock past it is refused. The change's time is read off the database's clock. With now() it fails: the cancel goes through.
  • activity, three tests:
    • TestAWriteWhoseCommitAnswerWasLostIsNotWrittenAgain: a TCP proxy passes COMMIT on and then cuts the client's connection, so the commit lands and its answer is lost. The row is counted once. Without the drop it is counted twice.
    • TestAWriteWhoseContextEndedBeforeItsCommitIsWrittenAgain: a query hook ends the context after the upsert. Commit returns without sending COMMIT, nothing is written, and the error is not ErrActivityUnsure, so it is retried.
    • TestACommitTheDatabaseRefusedIsNotUnsure: a deferred constraint fails the commit with an answer from the server, which is not ErrActivityUnsure.
    • Treating every commit error as unsure fails the last two. Never treating one as unsure fails the first.
  • phone TestACallerIsBilledFromTheArrivalWhicheverEventIsDeliveredFirst: before the fix it billed 59 s in one order and 60 s in the other. It now bills 60 s both ways.
  • cmd/router TestDriftLeavesOutWorkOnTheCustomersOwnKey: fails without the filter (2 rows, drift).
  • These pass, in both store suites where they apply: go test -tags integration on internal/store (price, call leg, activity, TestEveryHotTableIsReadByIndex, TestRequestQueryBudgets), internal/phone, internal/activity, the spend tests of internal/api TestServerSuite, and the drift tests of cmd/router TestPriceSyncSuite. go vet ./... and gofmt are clean.

🤖 Generated with Claude Code

Laurent Schaffner and others added 5 commits November 23, 2026 19:36
Spend summed every group outside the biggest few into the value "other",
so a cost label whose value is "other" was merged into it. The remainder
row now carries other: true; a label of that value is a row of its own.
The field is optional, so clients reading an older router still parse.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Cancel compared the change's effective time with now(), the start of its
transaction, which is taken before the wait of up to 5 s for the price
list's lock. A change that came into force during that wait could still
be cancelled. It now reads clock_timestamp() once the lock is held.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A BYOK row is priced at the BYOK fee with no cost at the vendor's rates,
since the vendor bills the customer, so drift read every such row as
billed 100% under what the vendor reported.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
An inbound leg opens started at the call's arrival. A join delivered after
it kept that start (Connect), but a join delivered before it was taken as
the leg opened and moved the start to the join, so the same call billed a
second more or less by delivery order. The kept join now starts only a leg
not started yet.

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

The activity upsert adds each write's counts to the stored ones, and the
recorder kept a failed write's deltas for the next. A commit whose answer
was lost may have landed, so writing it again counted it twice. A write
that failed in its commit is now ErrActivityUnsure, and the recorder drops
and logs it: an ambiguous commit undercounts rather than double counts.
A write that failed before its commit is still written again.

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

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

Copy link
Copy Markdown
Contributor Author

/jeffery review

@getstream-metis

Copy link
Copy Markdown

Thanks for asking Jeffery for review. I'll stay on this PR until I can approve it.

How it works

  • Findings come as inline comments, each starting with [Must Fix], [Should Fix], nit: or consider:.
  • Push fixes whenever you're ready. Once the branch has been quiet for 10 minutes I review the new head and resolve the threads it fixes. To skip the wait, comment /jeffery review.
  • If you disagree or have a question, reply in the thread. I'll answer there, and resolve it if you're right.
  • I approve once every must-fix and should-fix is closed and nobody has requested changes. CI isn't part of that; branch protection still holds the merge until it's green. From round 3 on, open should-fix findings stop blocking.
  • I don't approve release/* branches or changes to protected paths, and I'll say so when that applies.
  • If a must-fix is still open after round 8, I stop and hand the PR to a human. Draft PRs wait until they're marked ready.

Commands (each as a whole comment)

Command What it does
/jeffery review Review the current head now
/jeffery rereview Start over at round 1 on the current head and withdraw my approval. This redoes the whole review and costs as much as the first one, so use it only when you don't trust the earlier rounds. If nothing changed since my last round I say so instead; add force to redo it anyway
/jeffery pause / /jeffery resume Stop or restart automatic reviews on this PR
/jeffery status What I last reviewed, and why I have or haven't approved
/jeffery help This list

/review-agent or a mention of me works in place of /jeffery.

getstream-metis[bot]
getstream-metis Bot previously approved these changes Nov 23, 2026

@getstream-metis getstream-metis Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ Approved f76a9c82c94b after 1 round

Laurent Schaffner and others added 2 commits November 23, 2026 20:42
database/sql answers a context that ended before the commit with ErrTxDone
or the context's error, without sending COMMIT, and pgdriver sends COMMIT
on a context of its own. Those, and an error the server answered, are a
commit that did not land, so the write is kept and written again. Only
another failure, such as a connection cut after COMMIT was sent, is
ErrActivityUnsure. A dropped write holds nothing back, so write reports
true and a full record is written again at once.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The cancel is checked against clock_timestamp(), so the change's time and
the wait past it are read off the database's clock too. The SDK note lists
every SDK regenerated for spend's other.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@getstream-metis
getstream-metis Bot dismissed their stale review November 23, 2026 19:43

Withdrawing approval until eab2263 is reviewed.

@getstream-metis getstream-metis Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ Approved eab22633c658 after 2 rounds

@Loschcode

Copy link
Copy Markdown
Contributor Author

/jeffery review

@getstream-metis

Copy link
Copy Markdown

/jeffery review

Nothing changed since round 2 on eab22633c658; comment /jeffery rereview force to redo the full review anyway.

@Loschcode
Loschcode merged commit c78b8e8 into main Nov 24, 2026
21 checks passed
@Loschcode
Loschcode deleted the laurentschaffner/ai-3511-small-wrong-results-spend-other branch November 24, 2026 08:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant