Skip to content

fix(automations): Repair run cost and last run from the 0117 rollout - #3615

Merged
BYK merged 26 commits into
mainfrom
feat/automations-rollout-repair
Nov 18, 2026
Merged

BYK merged 26 commits into
mainfrom
feat/automations-rollout-repair

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Repairs data written during the #3572 rollout. Merge this only after the #3572 deploy has fully finished, when no old instance is still running. If it runs earlier, a run that an old instance settles after it is not repaired.

While 0117 rolled out, old instances still settled Automation runs. They did not copy the Conversation's cost to cost_micro_usd, and they did not move last_run_id. As a result, those runs show no cost in the summary and the charts, and their Automations sort and filter on an older last run until they run again.

Migration 0122 recomputes both for the runs that ended after 0117 was written (its journal time):

  • cost_micro_usd for each settled run with a Conversation. It uses the same rounding as the runtime copy.
  • last_run_id for each Automation with a run in that window. It uses the same rule as the runtime: the newest execution by end time, then by id.

Both statements reach their rows through the completed_at_ms index and the primary keys. They skip rows that already match, so a second run changes nothing.

On the local bench (synthetic data, 19k Automations and 3.1M runs), with 203 runs and 2 Automations left broken, the first pass fixed all of them: runs in 408 ms, Automations in 86 ms. A second pass updated nothing. On a bench with nothing to repair, both statements finish in under 5 ms.

#3392 also wants the next migration numbers. Whichever lands second renumbers.

via @BYK.

--

View Session [Sentry]

sentry-junior Bot and others added 24 commits November 17, 2026 11:03
…omations faster

The new UI's Automations list reads one page of 50 by latest activity,
with a Mine / All toggle, Inbox-style date groups, infinite scroll, and
rows that expand in place. List rows skip the per-Automation run totals,
and only the attention filters pay for the trigger check. A partial
index on (team_id, creator_slack_user_id) serves the Mine filter.

The Automation page starts its runs and versions reads with the summary
and paints the header from the list row while the summary loads.

Co-Authored-By: BYK <[email protected]>
…-speed

Co-Authored-By: BYK <[email protected]>

# Conflicts:
#	packages/junior-dashboard/e2e/inbox/automations.spec.ts
The activity sort read the newest run per row. It now orders by a stored
last_run_at_ms column, which each run write refreshes. Pages after the
first skip the total, counts and filter options.

Co-Authored-By: BYK <[email protected]>
The activity page no longer waits for the counts and filter options before
it reads its rows. Run stats load while the Destination and creator
details do.

Co-Authored-By: BYK <[email protected]>
The attention filter read the newest run per Automation. It now reads a
stored last_run_status, which the same refresh as last_run_at_ms sets.

Co-Authored-By: BYK <[email protected]>
The executions read built the full summary again, with its cost totals.
The Runs tab now reads the totals from the summary that the page already
loads.

Co-Authored-By: BYK <[email protected]>
Automation reads use key factories. A change writes the returned edit read
and refreshes the lists and that one Automation, not every Automation
read. Rows prefetch the summary and runs on hover or focus. Attention
pages after the first load at the same time, at most 8 at once.

Co-Authored-By: BYK <[email protected]>
…-speed

# Conflicts:
#	packages/junior/src/chat/automations/read.ts
…-followups

# Conflicts:
#	packages/junior-dashboard/e2e/inbox/automations-list.spec.ts
#	packages/junior-dashboard/src/client/api.ts
#	packages/junior-dashboard/src/client/inbox/pages/automations/AutomationPage.tsx
#	packages/junior-dashboard/src/client/inbox/pages/automations/AutomationRow.tsx
#	packages/junior/migrations/meta/_journal.json
#	packages/junior/src/api/automations/routes.ts
#	packages/junior/src/api/schema.ts
#	packages/junior/src/chat/automations/collection.ts
#	packages/junior/src/chat/automations/read.ts
#	packages/junior/tests/integration/api/automations.test.ts
Each usage write of a Conversation copies its cost to its run, and each run
write copies it when it links the Conversation. The summary windows and the
90-day and hourly charts sum the stored column instead of joining every run
to its Conversation.

Co-Authored-By: BYK <[email protected]>
…board

Remove the Automations path exclusion for no-await-in-loop and
no-broad-query-invalidation. The attention pages now read through a p-limit
pool instead of awaited batches.

Co-Authored-By: BYK <[email protected]>
…-followups

Co-Authored-By: BYK <[email protected]>

# Conflicts:
#	packages/junior/migrations/meta/0116_snapshot.json
#	packages/junior/migrations/meta/_journal.json
…rite

A data-modifying CTE updates the Conversation's usage and returns its cost,
and the outer update copies it to the run. The copy commits with the usage
write, adds no round trip, and changes nothing for a Conversation without a
run. Run writes set the cost in the same insert or upsert.

Co-Authored-By: BYK <[email protected]>
…micro-USD

junior_automations.last_run_id replaces the copied end time and status. The
activity page walks runs newest first through the completed index and keeps
each Automation's last run, so it reads one run per listed row instead of the
last run of every row. junior_automation_runs.cost_micro_usd replaces
cost_usd: integer sums are exact, and the read layer converts them to USD.

Co-Authored-By: BYK <[email protected]>
…7 rollout

The previous runtime kept settling runs while 0117 rolled out, without
copying run cost or moving last_run_id. Recompute both for runs that ended
after 0117 was written. Rows that already match are skipped, so it is safe
to run again.

Co-Authored-By: BYK <[email protected]>
recordAutomationExecution stored the run, then moved last_run_id in a second
statement. When the second one failed, the catch logged it and the list kept
an older last run. Both now run in one transaction, so a failure leaves
nothing stored and the heartbeat retries both.
…out-repair

Co-Authored-By: BYK <[email protected]>

# Conflicts:
#	packages/junior/migrations/meta/_journal.json
@sentry-junior
sentry-junior Bot requested a review from BYK November 17, 2026 20:57
@vercel

vercel Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
junior-docs Ready Ready Preview Nov 18, 2026 9:58am UTC

Request Review

@github-actions github-actions Bot added the risk: high PR risk score: high label Nov 17, 2026
@BYK
BYK marked this pull request as draft November 17, 2026 21:02
@BYK

BYK commented Nov 17, 2026

Copy link
Copy Markdown
Member

Converted to draft to prevent accidental merging before the next deploy

…out-repair

Co-Authored-By: BYK <[email protected]>

# Conflicts:
#	packages/junior/migrations/meta/0119_snapshot.json
#	packages/junior/migrations/meta/_journal.json
sentry-junior Bot added a commit that referenced this pull request Nov 17, 2026
#3615 lands first with 0121_automation_rollout_repair,
so the wake migrations follow it. 0122's snapshot chains from that 0121
snapshot. The custom SQL (NOT VALID, CONCURRENTLY) is kept.

Co-Authored-By: BYK <[email protected]>
@github-actions

github-actions Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

11 screenshot changes — 11 changed · 0 added · 0 removed

Review screenshots in Frameshift

Automation Run Details · Desktop
Automation Run Details · Desktop
Changed
Automation Skill Editor · Desktop
Automation Skill Editor · Desktop
Changed
Automations Expanded Row · Desktop
Automations Expanded Row · Desktop
Changed
Automations Expanded Row · Mobile
Automations Expanded Row · Mobile
Changed
Automations Expanded Row Light · Desktop
Automations Expanded Row Light · Desktop
Changed
Automations Expanded Row Light · Mobile
Automations Expanded Row Light · Mobile
Changed
Show 5 more changed screenshots
Automations Runs Filters · Desktop
Automations Runs Filters · Desktop
Changed
Conversation Attachment Zoom · Mobile
Conversation Attachment Zoom · Mobile
Changed
Conversation Event Log Virtual · Desktop
Conversation Event Log Virtual · Desktop
Changed
System Cache Tokens · Desktop
System Cache Tokens · Desktop
Changed
System Cache Warming Refill · Desktop
System Cache Warming Refill · Desktop
Changed

BYK added a commit that referenced this pull request Nov 18, 2026
…#3647)

Gets `GET /api/people` under 1 s: 2.29 s goes to 0.72 s on the local
bench. Follows #3629.

## Changes

**Turn range from index lookups.** Who is listed, `firstSeenAt`,
`lastSeenAt` and the Slack fields stay all-time. They come from lookups
on the existing `(actor_identity_id, occurred_at)` index: one lookup for
each verified identity's first Turn and one for its last. A person whose
last Turn is older than every range still appears, with zero windows.

**Range counts from the last 90 days only.** Per-range Conversation
counts read only `duration_ms` rows since the start of the 90-day range.
They are grouped before any join. The list no longer joins root
Conversations for status. It is still 5 statements in 1 round.

**Dropped list fields.** The list item no longer has `active`,
`activeDays`, `conversations`, `durationMs`, `failed` or `tokens`. These
were all-time totals that nothing read: the dashboard shows only
`windows[1|7|30|90]`, `firstSeenAt`, `lastSeenAt` and the actor. The
People profile keeps its all-time `totals`. Ties on `lastSeenAt` in the
server sort are now broken by 90-day Conversations.

**Index.** Migration 0121 adds
`junior_conversation_metrics_turn_metric_occurred_at_idx`:

- Columns: `(metric, occurred_at, actor_identity_id, conversation_id,
value) WHERE metric IN ('duration_ms', 'total_tokens',
'cost_micro_usd')`.
- With it, the bounded activity, range and spend reads scan only the
index.
- It is built `CONCURRENTLY IF NOT EXISTS` in its own migration.

## Index cost (local bench)

- Size: 58 MB. The table is 231 MB, with 416 MB of existing indexes.
- Build: 2.0 s, concurrent, over 2.25M metric rows.
- Writes: one more btree entry for 3 of the roughly 7.5 metric rows a
Turn writes, on insert and on a Run's delete and rewrite.

## Rollout

The index builds without blocking writes. Until it exists, the new query
runs at about 0.85 s. Any client that still expects the six dropped
fields fails the strict list schema. The only such clients were the
dashboard and its mocks, and both change here.

## Bench

Local synthetic data: 400 people, 100k Conversations, 2.25M metric rows
over 240 days. Per statement means totals / day activity / hour /
six-hour / spend.

| Variant | Endpoint | Per statement (ms) |
| --- | --- | --- |
| `main` | 2.29 s | 2260 / 839 / 126 / 235 / 530 |
| This PR, before the index is built | 0.85 s | 740 / 747 / 133 / 178 /
444 |
| This PR, with the index | 0.72 s | 262 / 704 / 106 / 118 / 229 |

- **Output:** same as `main` apart from the six dropped fields.
- **Profile:** the index takes the People profile read from 846 ms to
799 ms, with the same output.
- **What remains:** mostly the 90-day activity statement, at about 0.7
s.

## Migration number

`0121_people_list_metric_index` collides with #3615
(0121) and #3392 (0122/0123). If either lands first,
this PR renumbers its own migration to the next free number. Theirs stay
as they are.

<!-- junior-request-attribution:start -->
via @BYK.
<!-- junior-request-attribution:end -->

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:agent%3Ae97d254c3473690092af5aa581043d47 -->

--

[View
Session](https://junior-prod.sentry.dev/conversations/agent%3Ae97d254c3473690092af5aa581043d47)
[[Sentry]](https://sentry.sentry.io/explore/conversations/agent%3Ae97d254c3473690092af5aa581043d47/?project=4510944073809921)

<!-- junior-session-footer:end -->

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Co-authored-by: BYK <[email protected]>
…out-repair

Co-Authored-By: BYK <[email protected]>

# Conflicts:
#	packages/junior/migrations/meta/0121_snapshot.json
#	packages/junior/migrations/meta/_journal.json
sentry-junior Bot added a commit that referenced this pull request Nov 18, 2026
#3615 lands first with 0122_automation_rollout_repair,
after main's 0121_people_list_metric_index. 0123's snapshot chains from
that 0122 snapshot. The custom SQL (NOT VALID, CONCURRENTLY) is kept.

Co-Authored-By: BYK <[email protected]>
@BYK
BYK marked this pull request as ready for review November 18, 2026 14:13
@sentry-junior
sentry-junior Bot requested a review from BYK November 18, 2026 14:13
@BYK
BYK merged commit 20cbe7c into main Nov 18, 2026
48 checks passed
@BYK
BYK deleted the feat/automations-rollout-repair branch November 18, 2026 14:13

@cursor cursor 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.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 5904172. Configure here.

)
) AS "repair"
WHERE "junior_automations"."id" = "repair"."id"
AND "junior_automations"."last_run_id" IS DISTINCT FROM "repair"."last_run_id";

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Repair can revert concurrent updates

Low Severity

The last_run_id update computes the desired id from a snapshot, then writes it whenever the live column differs. A run the new runtime settles during that statement can move last_run_id forward, after which IS DISTINCT FROM treats the newer id as a mismatch and writes the older snapshot value back. The same UPDATE … FROM pattern on cost_micro_usd can replace a later usage copy with a stale amount.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 5904172. Configure here.

BYK added a commit that referenced this pull request Nov 22, 2026
…out (#4041)

#3621 moved money to integer micro-USD. While its
migrations (0119, 0120) rolled out, the previous runtime still wrote
money in the old units, and nothing converted those rows afterwards:

- `cost_usd` metric rows, which every cost read now skips. The usage
cost of those Conversations, and the cost of their Automation runs, came
from one runtime and missed the other runtime's rows.
- Brief versions with only `cost_usd` set.
- Stats counters added to the legacy `count` column, including cache
warming dollar counters under their old names and units.

Migration 0160 converts the old cost rows to `cost_micro_usd`. When both
runtimes wrote a cost row for the same Run, the later row wins. It then
sums the usage cost of each affected Conversation again and copies it to
their Automation runs. It also fills `cost_micro_usd` on those Brief
versions and moves `count` amounts into `value`, using the micro-USD
names for cache warming counters. Each statement touches only old-unit
rows, so running it a second time changes nothing.

The metrics table is large and has no index on `cost_usd`. So the cost
repair is one statement with data-modifying CTEs: it scans the table
once, and finds the other rows of each affected Conversation by primary
key.

#3615 is a different repair: it fixed Automation run
cost and last run after the 0117 rollout.

<!-- junior-request-attribution:start -->
via @BYK.
<!-- junior-request-attribution:end -->

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:agent%3Af3d48a21456a0e73f7ba562c44119dca -->

--

[View
Session](https://junior-prod.sentry.dev/conversations/agent%3Af3d48a21456a0e73f7ba562c44119dca)
[[Sentry]](https://sentry.sentry.io/explore/conversations/agent%3Af3d48a21456a0e73f7ba562c44119dca/?project=4510944073809921)

<!-- junior-session-footer:end -->

---------

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Co-authored-by: BYK <[email protected]>

This branch was successfully deployed

1 active deployment
Preview – junior-docs — 59041720 Deployed Nov 18, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk: high PR risk score: high

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant