Skip to content

refactor(runtime): make a single provider step the ModelAdapter primitive - #1425

Merged
Astro-Han merged 19 commits into
mainfrom
refactor/1381-runtime-owned-agent-loop
Jul 24, 2026
Merged

refactor(runtime): make a single provider step the ModelAdapter primitive#1425
Astro-Han merged 19 commits into
mainfrom
refactor/1381-runtime-owned-agent-loop

Conversation

@Astro-Han

Copy link
Copy Markdown
Contributor

Summary

  • make each ModelAdapter.startStream call exactly one schema-only provider request with SDK retries disabled
  • move continuation, step limits, retry decisions, tool settlement, and durable replay into the Runtime-owned loop
  • reload the authoritative turn ledger before every continuation and preserve rich provider tool outputs only after their durable outcomes land
  • migrate deferred-tool and compaction hooks to a Runtime request-projection contract, including overflow retry re-projection
  • keep parent and child durable readers independent from parent-only mid-turn history compaction eligibility

Closes #1381

Verification

  • npm test --workspace @maka/runtime — 2,432 passed, 7 skipped, 0 failed
  • npm test --workspace @maka/headless — 1,314 passed, 1 skipped, 0 failed
  • npm run build
  • npm run typecheck
  • npm run format:check
  • npm run lint

Review focus

The loop waits for every tool call in a provider step with Promise.allSettled, then waits for the event consumer durability boundary before reloading the current-run ledger. Overflow recovery rebuilds from durable facts and re-enters active tool-result pruning before the retry so archived payloads cannot reappear.

@Astro-Han Astro-Han left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The inline image-budget bug blocks this head.

  • [P2] packages/runtime/src/model-adapter.ts:180 sets maxRetries: 0. On main, streamText uses the AI SDK default of two retries. The replacement path in ai-sdk-backend.ts:1577-1597 retries only errors classified as Network, so 429 RateLimit and 5xx ProviderUnavailable responses now fail immediately. Preserve retries for those classes or add them to the runtime-owned retry policy, with tests for both cases.
  • [P2] packages/runtime/src/ai-sdk-backend.ts:1634 makes an exported optional input mandatory only after the model returns a tool call. A direct AiSdkBackend caller that omits loadTurnRuntimeEvents worked on main, but now fails before tool settlement with durable current-run reader is required for tool continuation. Either make the reader a required constructor contract or retain a non-durable continuation path. Add a tool-loop test with the reader omitted.
  • [P2] The overflow retry at packages/runtime/src/ai-sdk-backend.ts:1560 keeps returnedToolCalls from the failed attempt. A stream that emits a tool call and then an in-stream context-length error will compact and retry. If the retry finishes normally, the stale call is still settled at line 1638. Clear attempt-local calls before retrying, or decline recovery once the failed attempt has emitted a tool call. The overflow tests cover an error-only stream, not this sequence.

Please revise before merging.

Comment thread packages/runtime/src/ai-sdk-backend.ts
@Astro-Han
Astro-Han merged commit a82d4fc into main Jul 24, 2026
3 checks passed
@Astro-Han
Astro-Han deleted the refactor/1381-runtime-owned-agent-loop branch July 24, 2026 09:36
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.

refactor(runtime): make a single provider step the ModelAdapter primitive

1 participant