Skip to content

Fix cumulative tokens for resumed workflow runs - #2353

Draft
Wirasm wants to merge 3 commits into
devfrom
archon/task-archon-fix-github-issue-experimental-1785485856991
Draft

Fix cumulative tokens for resumed workflow runs#2353
Wirasm wants to merge 3 commits into
devfrom
archon/task-archon-fix-github-issue-experimental-1785485856991

Conversation

@Wirasm

@Wirasm Wirasm commented Jul 31, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Problem: resumed DAG workflow runs persisted only tokens consumed after resumption, omitting completed nodes from an earlier failed attempt.
  • Why it matters: run metadata and downstream workflow: nodes must report total provider usage for the complete workflow-run ID.
  • What changed: resume hydration now reconstructs prior node outputs and finite token totals from node_completed events, then seeds the existing DAG accumulator before the terminal completion write.
  • What did not change (scope boundary): workflow lifecycle/CAS behavior, token aggregation for a single invocation, child-run forwarding, and paused/loop-finalization semantics are unchanged.

UX Journey

Before

  User                   Archon workflow                 Provider
  ----                   ---------------                 --------
  starts run ─────────▶  executes node A ─────────────▶  returns 40/4 tokens
                         persists node_completed
                         node B fails; run is failed
  resumes run ────────▶  restores A output; tokens reset to 0
                         executes B ──────────────────▶  returns 60/6 tokens
                         completes with metadata 60/6

After

  User                   Archon workflow                 Provider
  ----                   ---------------                 --------
  starts run ─────────▶  executes node A ─────────────▶  returns 40/4 tokens
                         persists node_completed
                         node B fails; run is failed
  resumes run ────────▶  [restores A output and 40/4 event usage]
                         executes B ──────────────────▶  returns 60/6 tokens
                         completes with metadata [100/10]

Architecture Diagram

Before

workflow-events DB ──▶ core event reader ──▶ store adapter ──▶ executor ──▶ DAG executor ──▶ workflow_runs metadata
                                      outputs only                                  zero token counters

After

workflow-events DB ──▶ [~ core event reader] ──▶ [~ store adapter] ──▶ [~ executor] ──▶ [~ DAG executor] ──▶ workflow_runs metadata
                    completed outputs + finite usage snapshot            passes prior usage       seeds accumulator

Connection inventory (list every module-to-module edge, mark changes):

From To Status Notes
Workflow event rows Core workflow-event reader modified Reconstructs outputs and sums finite tokens from node_completed rows only.
Core event reader Core workflow-store adapter modified Exposes the typed resume snapshot through the engine contract.
Workflow-store adapter Workflow executor modified Resume hydration receives outputs plus prior token usage.
Workflow executor DAG executor modified Threads prior usage only with the pre-created resumed run.
DAG executor Workflow-run completion metadata modified Existing final write receives cumulative rather than invocation-local totals.

Label Snapshot

  • Risk: risk: medium
  • Size: size: M
  • Scope: core, workflows, tests
  • Module: workflows:resume-accounting

Change Metadata

  • Change type: bug
  • Primary scope: multi

Linked Issue

Validation Evidence (required)

Commands and result summary:

bun run type-check
bun run lint
bun run format:check
bun run test
bun run validate
  • Evidence provided (test/log/trace/screenshot): implementation and validation artifacts report type-check, lint (zero warnings), format check, build, full package-isolated tests (6,417 passed), and bun run validate passing after bundled-default regeneration. Focused coverage included DAG executor (441), executor (69), sub-run (12), workflow-event DB (19), and store-adapter (13) tests.
  • If any command is intentionally skipped, explain why: none. The implementation record notes an earlier validation attempt stopped on pre-existing local .archon/ bundled-default drift; the later validation record refreshed the local generated bundle and reports the complete suite passed. Those local workflow-definition/generated-file edits are intentionally not part of this PR.

Security Impact (required)

  • New permissions/capabilities? (No)
  • New external network calls? (No)
  • Secrets/tokens handling changed? (No; provider-usage accounting only, not credential handling)
  • File system access scope changed? (No)
  • If any Yes, describe risk and mitigation: not applicable.

Compatibility / Migration

  • Backward compatible? (Yes)
  • Config/env changes? (No)
  • Database migration needed? (No)
  • If yes, exact upgrade steps: not applicable.

Human Verification (required)

What was personally validated beyond CI:

  • Verified scenarios: a failed first invocation with persisted node usage followed by a resumed invocation reconciles final metadata to the combined token totals.
  • Edge cases checked: malformed, missing, and non-finite event token payloads are ignored; node_skipped_prior_success rows do not double-count usage; outputs still restore during resume.
  • What was not verified: live provider billing dashboards and paused-run/loop-finalization accounting were not changed or manually exercised.

Side Effects / Blast Radius (required)

  • Affected subsystems/workflows: workflow event reading, core-to-engine store adapter, resume hydration, DAG completion metadata, and child workflow token propagation.
  • Potential unintended effects: incorrectly shaped historical event data could affect reconstructed usage; only finite numeric tokens from authoritative completion events are included.
  • Guardrails/monitoring for early detection: focused malformed-event and fail→resume regression tests; existing DAG logging for non-finite provider usage; terminal completion retains its existing single-writer status guard.

Rollback Plan (required)

  • Fast rollback command/path: revert commit 8d3bbfbc87e6456f716ec7cb2b204a42d7521df4 after merge.
  • Feature flags or config toggles (if any): none.
  • Observable failure symptoms: resumed workflow metadata reports unexpected cumulative token values or resume hydration failures; affected runs can be inspected through their persisted node_completed events.

Risks and Mitigations

  • Risk: historical event payloads may be malformed or omit usage values.
    • Mitigation: validate both token values as finite numbers and ignore invalid/missing payloads while preserving output hydration.
  • Risk: restored nodes could be counted twice during resume.
    • Mitigation: only node_completed events contribute usage; node_skipped_prior_success events restore outputs without contributing tokens.

Fixes #2352

Wirasm added 2 commits July 31, 2026 11:35
Resumed DAG runs discarded token usage from earlier failed executions because resume hydration restored only node outputs.

Changes:
- Restore cumulative token usage from completed-node events
- Seed resumed DAG token counters with prior usage
- Add reconciliation and malformed-event regression coverage

Fixes #2352
Fixed:\n- Log malformed persisted gate token metadata with run and node context\n- Avoid warnings for absent optional historical token usage\n- Cover resume snapshot hydration into DAG execution\n\nTests added:\n- Gate-token fallback, missing token usage, and resume hydration coverage\n\nSkipped:\n- None
@coderabbitai

coderabbitai Bot commented Jul 31, 2026

Copy link
Copy Markdown

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 103f914f-801c-4530-85ec-cbdf3a7ee1dc

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@Wirasm

Wirasm commented Jul 31, 2026

Copy link
Copy Markdown
Collaborator Author

⚡ Self-Fix Report (Aggressive)

Status: COMPLETE
Pushed: ✅ Changes pushed to archon/task-archon-fix-github-issue-experimental-1785485856991
Philosophy: Fix everything unless clearly a new concern


Fixes Applied (3 total)

Severity Count
🔴 CRITICAL 0
🟠 HIGH 0
🟡 MEDIUM 2
🟢 LOW 1
View all fixes
  • Malformed gate usage audit trail (packages/workflows/src/dag-executor.ts:3039) — logs invalid supplied gate usage with run/node context.
  • Resume hydration coverage (packages/workflows/src/executor.test.ts:856) — verifies hydrated outputs and usage reach resumed DAG execution.
  • Historical usage warning noise (packages/core/src/db/workflow-events.ts:251) — absent optional usage is silent; malformed supplied usage still warns.

Tests Added

  • Gate-token fallback, missing historical usage, and resume-hydration coverage.

Skipped (0)

(none — all findings addressed)


Suggested Follow-up Issues

(none)


Validation

✅ Type check | ✅ Lint | ✅ Tests (bun run test)


Self-fix by Archon · aggressive mode · fixes pushed to archon/task-archon-fix-github-issue-experimental-1785485856991

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.

fix(workflows): run token totals count only the last execution — every resumed run undercounts

1 participant