mirror of
https://github.com/mastra-ai/mastra.git
synced 2026-11-17 08:44:46 +00:00
refactor(core): share durable and Inngest run lifecycle setup (#29635)
## Summary Extract reusable run-abort and cleanup helpers for durable agents, and make Inngest reuse core's `AgentStreamBus` instead of maintaining its own replay-cache/bus initialization. This is the behavior-neutral shared-session lane (roadmap PR 1 / COR-1509). It is independent of [#29600](https://github.com/mastra-ai/mastra/pull/29600), which changes server authorization organization. Neither branch includes the other. - Replace four repeated DurableAgent abort-controller setups and two Inngest setups with `createRunAbortController`, preserving external-abort callbacks and remote-abort publication. The plain publisher now uses the same forwarding helper and detaches it during teardown, retaining its local no-reason abort default. - Extract `createRunCleanup` for DurableAgent's stream, resume, recover, generate and observe paths, retaining their different release policies, ordering, timer behavior and retry-on-throw behavior. - Resolve Inngest's bus through `AgentStreamBus`, preserving `InngestPubSub`, its router, lazy cache resolution, already-cached inner-bus reuse and fork bus identity. - Replace the internal `super({ durable: false })` publisher-suppression workaround with a protected publisher-ownership hook. DurableAgent still does not start a plain publisher; its historically observable `durable === false` getter is preserved. ### Intentionally unchanged Plain Agent continues to deliver directly to its original caller and publish an observation copy. DurableAgent continues to publish and read back. Public result shapes, event envelopes/topics, execution fencing and path-specific cleanup policies remain distinct. Plain Agent retains its existing publisher/abort/ownership policy rather than being forced through durable-only lifecycle helpers. No server authorization changes, topic-namespace redesign, output-path convergence, durable-by-default execution, worker dispatch, checker changes or propagated peer bumps. The separate streamed-delegation bug fix shipped in [#29592](https://github.com/mastra-ai/mastra/pull/29592). ## Peer compatibility — unresolved review decision Opening the exact reviewed candidate as requested; no additional dependency or checker edits have been made for publication. The candidate currently changes only Inngest's core peer range to `>=1.83.0-0 <2.0.0-0`. Inngest imports the newly exported `AgentStreamBus` and `createRunAbortController`, but published core `1.83.0-alpha.0` satisfies that range and lacks those exports. The range therefore does not yet establish compatibility with every version it accepts. - Stock import checking in the unprepared alpha checkout failed because stable core `1.83.0` could not be downloaded at verification time. - With authentic Changesets CI preparation, the unmodified checker packed matching local core `1.83.0`: **18/18 import paths and 114 named value imports passed**. - Peer validation passed **128 valid / 0 invalid**; that is not proof that published alpha.0 exposes the new APIs. Decide the appropriate peer range during review. The suggested stable-only range `>=1.83.0 <2.0.0-0` has **not** been applied. There is no publish-first prerequisite or proposed checker workaround. ## Proof Original reviewed candidate: `262c34adec`, based on `f82c222119`. Twelve changed files; no PR 2/server files or checker changes. - Core TypeScript, changed-file repository formatting/type-aware lint, peer validation and whitespace checks passed. - Independent bounded core review: **24/24 test executions** across standard and alternate-storage projects. Inspection covered abort callback/reason ordering, cleanup retry/order, generate's lighter timer release, publisher suppression and getter preservation. - Strict retained before/after harness: **15/15 identical ordered frame sequences** across plain, durable and evented engines for text, tool-then-text, approval suspension, provider error and abort-inside-tool. The comparison retains duplicate and lifecycle/control frames. The scratch harness is not committed; this is not generate, Inngest or remote-transport parity proof. - Core suites reported **6,335 passing / 71 skipped**, with two parallel worker processes terminating with SIGABRT in crash-recovery testing. The crash-recovery file passed independently **20/20**. The broad parallel run is not represented as clean. - Earlier Inngest bus-integration-stage evidence: real dev-server abort suites **9/9** and durable-agent suites **247/247**. Two create-agent failures reproduced on main. ### Human-review follow-up —541c618118Two files changed: the plain publisher now reuses `createRunAbortController(callerSignal)` and calls `detach()` on teardown; the patch changeset explicitly documents `AgentStreamBus`, `createRunAbortController`, and type `RunAbortController` with their import path. Remote abort keeps its local `new Error('Aborted')` default. No peer, checker, output-path, server, or cleanup-policy change. - Final focused stream/abort/cleanup suites: **66/66** across 11 test files, including AIMock event parity and remote abort. - Six disposable publisher-contract probes passed: live/already-aborted caller signals with exact explicit/default reason identity, detachment after teardown, and the local remote-abort default. Omitting caller-signal forwarding made exactly **4 fail / 2 pass**; restoring it returned **6/6**. Probe files are scratch evidence, not committed tests. - Fresh `pnpm build:core`: **15/15 tasks passed**. Core `tsc --noEmit` passed after rebuilding stale declarations. Changed-file type-aware Oxlint, Oxfmt, Changesets status, whitespace checks, and the normal commit hook passed. - Broader cancellation/liveness/lease run: **300 passed / 2 failed / 24 skipped**. The lease case passed on isolated retry (12 matched cases passed). The GC-child timeout reproduced with the publisher restored byte-for-byte to original PR head `262c34ad`; this attributes it as pre-existing to this two-file follow-up, **not** as proven main-baseline behavior. The broader run remains non-clean. - Fresh GitHub CI is pending. The disclosed Inngest peer-floor decision remains unresolved and unchanged. No new hosted-service or two-process verification was performed for this follow-up; earlier evidence below retains its original provenance. ### Independent combined compatibility check A disposable checkout applied only PR 2's three-file patch at `71d868c384` to this exact PR 1 candidate. Neither published branch was merged into the other, and the changed-file sets have zero overlap. Fresh core/server builds and both TypeScript checks passed. Executable ESM/CJS source maps were checked against candidate sources, and fixture imports resolved the freshly built distributions. - Server handler tests: **281/281**. - Real-TCP Hono access tests: **4/4**. - Real Redis / two actual HTTP child processes: **8/8**, including caching on/off, approval/finish observation, acknowledged remote abort, unconfirmed abort false, tenant/anonymous/unknown/expired denials and cleanup. - Bounded Inngest cache/bus identity tests: **4/4**, plus the exported `AgentStreamBus` check. This is wiring coverage, not a full Inngest runtime run. The subsequent PR 2 review additions are test-only and are not included in that historical combined-run claim. ## Unverified - Fresh GitHub CI and current-main integration after opening this PR. - A compatible published minimum core version for all newly imported APIs. - A clean full-monorepo/full-parallel-core run; the parallel worker SIGABRTs remain disclosed. - Final-head full Inngest lifecycle/parity, generate parity, hosted Redis, GCP Pub/Sub emulator, real-remote latency, browser Stop/reconnect, load/failover and process-kill recovery. ## Risk Lifecycle extraction can change abort-listener ordering, double publication, cleanup timing or registry release even without intentional behavior changes. Regression tests cover single control publication, the absence of a second plain run record, one replay-cache wrapper, cache identity, cleanup ordering and retry behavior. Explicit hooks preserve path-specific policy rather than asserting identical output/result contracts. The unresolved peer range is a real package-compatibility issue and remains a review/release gate, not a claim of runtime equivalence with old core packages. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## ELI5 This change gives durable and Inngest runs shared tools to handle cancellation and cleanup. It also makes Inngest use core’s stream-bus setup while keeping its own run behavior. ## Changes - Added and exported helpers for forwarding abort signals and coordinating run cleanup. - Updated `DurableAgent` run paths to use the shared helpers. Added a hook to prevent durable runs from using the plain Agent stream publisher. Kept the `durable` getter set to `false`. - Routed Inngest stream pubsub setup through `AgentStreamBus`. The setup reuses an existing caching pubsub and resolves Mastra’s cache when needed. - Raised the Inngest package’s `@mastra/core` peer dependency minimum to `1.83.0-0`. - Added tests for abort forwarding, cleanup behavior, durable stream publishing, and Inngest cache and bus setup. ## Validation and compatibility The supplied PR notes report that focused stream, abort, and cleanup suites passed 66/66 across 11 files. They also report 24/24 bounded core test executions, 15/15 identical ordered frame sequences in a before-and-after harness, six publisher-contract probes passed, and a fresh `pnpm build:core` passed 15/15 tasks. The broad core test run reportedly had 6,335 passing tests and 71 skipped tests, but two workers terminated with `SIGABRT`. Crash-recovery reportedly passed 20/20 when run independently. A broader cancellation, liveness, and lease run was not clean: 300 passed, 2 failed, and 24 were skipped. Fresh GitHub CI results and several runtime and recovery scenarios remain unverified. The PR notes identify a peer compatibility concern: published `@mastra/core` version `1.83.0-alpha.0` lacks exports imported by Inngest. The suggested stable-only peer range was not applied, so compatibility with the published minimum remains unresolved. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: mastra-platform[bot] <mastra-platform[bot]@users.noreply.github.com> Co-authored-by: mastra-platform[bot] <284800079+mastra-platform[bot]@users.noreply.github.com>
This commit is contained in:
parent
a2a03b53fe
commit
ed1cecba5c
14 changed files with 552 additions and 245 deletions
Loading…
Add table
Add a link
Reference in a new issue