Skip to content

fix(core): accept usage_recorded in AgentRun event schema (desktop fails to start) - #1945

Merged
Astro-Han merged 2 commits into
apache:mainfrom
songlairui:fix/agent-run-usage-recorded-event-type
Aug 3, 2026
Merged

fix(core): accept usage_recorded in AgentRun event schema (desktop fails to start)#1945
Astro-Han merged 2 commits into
apache:mainfrom
songlairui:fix/agent-run-usage-recorded-event-type

Conversation

@songlairui

Copy link
Copy Markdown
Contributor

Problem

Desktop app fails to start after any normal run:

AgentRun 99272103-6b74-4dd2-b367-b231bbe0b6c7 has a corrupt JSONL record at line 11: Invalid AgentRun event schema

The run event ledger (workspaces/<ws>/sessions/<session>/runs/<run>/events.jsonl) contains type: "usage_recorded" events, but usage_recorded is not in AGENT_RUN_EVENT_TYPES in packages/core/src/agent-run.ts. The strict read path in @maka/storage (readEvents with strict=true) throws on any event whose type is not in the enum, which blocks app startup.

Verified: all 4 runs on a real machine contained a usage_recorded event, i.e. every run that completes a turn with token usage writes one — so this is a guaranteed recurrence, not one-off corruption. The event JSON itself is valid; the failure is purely schema-level.

Fix

Add usage_recorded to AGENT_RUN_EVENT_TYPES. AgentRunEvent.data is Record<string, unknown> and projection handling is generic, so no other changes are required.

Verification

  • packages/core typecheck + build pass (tsc).
  • node --test dist/__tests__/agent-run-*.test.js → 9/9 pass.
  • Re-ran decodeAgentRunEvent against the original on-disk ledger from the failing machine (backup of 4 runs, 62 events including 4 usage_recorded): all 62 decode successfully, 0 failures.

Fixes #1942

The agent runtime writes a 'usage_recorded' event after each provider
request completes, but 'usage_recorded' was missing from
AGENT_RUN_EVENT_TYPES. Strict AgentRun ledger reads (used at desktop
startup) throw 'Invalid AgentRun event schema' for any event with an
unknown type, so a single normal run bricked the whole app on next
launch.

Add the missing event type to the enum. Token-usage data on these
events is free-form (Record<string, unknown>), and projection handling
is generic, so no other changes are required.

Fixes apache#1942
Signed-off-by: pi + deepseek-v4-flash
@me2seeks

me2seeks commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Hi @Astro-Han, would you mind taking a look when you have a chance? I know this is mostly a schema compatibility issue, but it’s currently blocking me from using Desktop—the app can no longer start after a normal run. If the fix looks right to you, could you help get it merged? Thanks!

@Astro-Han

Copy link
Copy Markdown
Contributor

@me2seeks Happy to do it! will merge this fix asap.

@Astro-Han
Astro-Han merged commit 6149458 into apache:main Aug 3, 2026
9 checks passed
Astro-Han added a commit that referenced this pull request Aug 3, 2026
An AgentRun ledger is append-only and outlives the build that wrote it, but
`decodeAgentRunEvent` rejected any event whose type was absent from
`AGENT_RUN_EVENT_TYPES` — conflating "this build does not write it" with "this
record is damaged". #1755 retired the `usage_recorded` writer and dropped the
enum entry in one step, which is correct for the writer and retroactively
illegal for every ledger already on disk: v0.1.3 then refused to start on any
machine that had run v0.1.2 (#1942).

Decode now accepts any non-empty string type and keeps validating the envelope,
so strict recovery still rejects genuinely damaged records while tolerating both
retired types and types from a newer build a user downgraded away from — a
direction no enum discipline can cover. `AGENT_RUN_EVENT_TYPES` becomes what
this build emits rather than what it can read, and the `usage_recorded` entry
added by #1945 is dropped as the dead entry it now is.

Fixes #1942
Astro-Han added a commit that referenced this pull request Aug 3, 2026
`AGENT_RUN_EVENT_TYPES` was both the set this build writes and the set a reader accepts.
The ledger is append-only and outlives the build that wrote it, so retiring a writer and
deleting its entry in one commit (#1755) left the next build unable to decode records the
previous one had persisted, and the desktop app failed to start (#1942).

Only the write side can be a closed set. Reads take `type` as an open string with the
envelope around it still validated, so a damaged record is still rejected; appends take
`EmittedAgentRunEvent`, so a misspelled or retired type fails to compile at the call that
would persist it. That makes retiring a writer safe, which is why `usage_recorded` is gone
again: #1945 could only fix the crash by adding a type nothing writes back to the catalogue
this build writes from.

A copy drops an event this build does not emit rather than carrying its unrewritten
source-owned ids into the target, since the rewriters cannot inspect a payload they do not
know.
Astro-Han added a commit that referenced this pull request Aug 3, 2026
`AGENT_RUN_EVENT_TYPES` was both the set this build writes and the set a reader accepts.
The ledger is append-only and outlives the build that wrote it, so retiring a writer and
deleting its entry in one commit (#1755) left the next build unable to decode records the
previous one had persisted, and the desktop app failed to start (#1942).

Only the write side can be a closed set. Reads take `type` as an open string with the
envelope around it still validated, so a damaged record is still rejected; appends take
`EmittedAgentRunEvent`, so a misspelled or retired type fails to compile at the call that
would persist it. That makes retiring a writer safe, which is why `usage_recorded` is gone
again: #1945 could only fix the crash by adding a type nothing writes back to the catalogue
this build writes from.

A copy drops an event this build does not emit rather than carrying its unrewritten
source-owned ids into the target, since the rewriters cannot inspect a payload they do not
know.
Astro-Han added a commit that referenced this pull request Aug 3, 2026
`AGENT_RUN_EVENT_TYPES` was both the set this build writes and the set a reader accepts.
The ledger is append-only and outlives the build that wrote it, so retiring a writer and
deleting its entry in one commit (#1755) left the next build unable to decode records the
previous one had persisted, and the desktop app failed to start (#1942).

Only the write side can be a closed set. Reads take `type` as an open string with the
envelope around it still validated, so a damaged record is still rejected; appends take
`EmittedAgentRunEvent`, so a misspelled or retired type fails to compile at the call that
would persist it. That makes retiring a writer safe, which is why `usage_recorded` is gone
again: #1945 could only fix the crash by adding a type nothing writes back to the catalogue
this build writes from.

A copy drops an event this build does not emit rather than carrying its unrewritten
source-owned ids into the target, since the rewriters cannot inspect a payload they do not
know.
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.

AgentRun events.jsonl records type=usage_recorded which is missing from AGENT_RUN_EVENT_TYPES → strict decode fails, desktop app won't start

3 participants