Commit c853d53
fix(harness): eliminate agent forking — use single shared agent across modes (#18008)
## Description
Eliminates `__fork()` from the shared-agent path in Harness. Instead of
creating per-mode agent clones, the harness reuses `config.agent`
directly and never mutates its instructions or tools.
**Root cause:** PR #17892 introduced shared harness modes that fork the
base agent per-mode. Signal providers connected to the base agent during
construction skip re-connection on forks (`isConnected` guard), so they
remain pointed at the original memoryless base agent. When GitHubSignals
(or any signal provider) tries to deliver notifications, it crashes:
```
[Processor:task-state] computeStateSignal requires Mastra memory
with an active resourceId and threadId
```
**Fix — no more forking, no agent mutation:**
```diff
# getAgentForMode() — shared agent path
- const forkedAgent = this.config.agent.__fork();
- forkedAgent.__updateInstructions(instructions);
- forkedAgent.__setTools(modeTools);
- this.#legacyAgentMode[mode.id] = forkedAgent;
+ return this.config.agent; // single instance, no fork, no mutation
```
- `getAgentForMode()`: returns `config.agent` directly — no fork, no
instructions/tools mutation
- `buildAgentMessageStreamOptions()`: resolves mode instructions at call
time via `resolveCurrentModeInstructions()` and passes them as the
`instructions` stream option — the agent's own instructions are never
touched
- `buildToolsets()`: mode-specific tools (`mode.tools` /
`mode.additionalTools`) are delivered through toolsets — the agent's own
tools (including signal-provider tools) are never replaced
- `init()`: propagates runtime services (memory, storage, etc.) once to
the shared agent — signal providers connected during construction retain
full access
- `setBrowser()`: sets browser on the single agent instead of iterating
forks
- Deprecated `mode.agent` and no-backing-agent construction paths are
unchanged
## Related issue(s)
Reported in Slack #mastracode by Tyler Barnes. Regression from #17892
(shared harness modes via `__fork()`).
## Type of change
- [x] Bug fix (non-breaking change that fixes an issue)
- [x] Code refactoring
## Checklist
- [x] I have linked the related issue(s) in the description above
- [x] I have made corresponding changes to the documentation (if
applicable)
- [x] I have added tests that prove my fix is effective or that my
feature works
- [ ] I have addressed all Coderabbit comments on this PR
Link to Devin session:
https://app.devin.ai/sessions/a7472d903dcd4982bbed590b0bde4c1c
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## ELI5
Harness can run “agent work” in different modes (like different
capabilities). A bug made Harness create separate agent copies per mode,
but some notification components (signal providers) kept using the
original agent that never got the required memory/services—so
notifications crashed. This PR stops the per-mode agent cloning and
instead reuses one shared agent, wiring mode-specific instructions/tools
at runtime so the shared agent has memory and signals work again.
## Summary
Fixes a regression where Harness signal providers (e.g. `GitHubSignals`)
could crash because they were still connected to a base `Agent` that
lacked Mastra runtime services (especially memory) after PR `#17892`
introduced per-mode agent forking/cloning.
### Key changes
- **Removed agent forking/cloning for the shared-agent (`config.agent`)
path**
- `getAgentForMode()` returns `config.agent` directly (no per-mode
cloning).
- The deprecated `mode.agent` path remains supported via its existing
per-mode caching behavior (agents can still be independent there).
- **Mode instructions are applied dynamically at call time (no mutation
of the shared agent)**
- `resolveCurrentModeInstructions()` computes harness-level + current
mode instructions on demand.
- `buildAgentMessageStreamOptions()` passes these as the `instructions`
stream option, instead of updating the agent’s own instructions (avoids
cross-mode instruction bleed).
- **Mode tools are delivered via toolsets (no replacement of the shared
agent’s tools)**
- `buildToolsets()` supplies mode tool overrides at the toolset level
(via `result.modeTools`) for the shared-agent path, rather than
baking/replacing tools on the shared agent.
- **Runtime services and browser propagation happen once for the shared
agent**
- `init()` propagates runtime services
(memory/storage/workspace/browser/pubsub, etc.) once to the shared
`config.agent`.
- `setBrowser()` sets the browser on the single shared agent.
- **Deprecated/no-backing-agent behavior remains unchanged**
- When there is no shared backing agent, the harness continues
constructing/caching per-mode agents with combined harness+mode
instructions and merged mode tools, as before.
### Tests
Extended `packages/core/src/harness/fork-clone-metadata.test.ts` to
cover:
- The same `config.agent` instance is reused across mode switches (no
forking).
- The shared agent’s own instruction text is not mutated across mode
switches.
- Mode-specific instructions are resolved dynamically at call time.
- Mode tools appear/disappear correctly in generated toolsets while
using the shared-agent path.
- `SignalProvider`-based regression coverage: a connected base/shared
agent retains access to runtime services (memory) after
`harness.init()`, staying properly connected across mode switches.
- A regression test confirming the deprecated `mode.agent` path still
keeps agents independent per mode.
### Changeset
- Added a patch changeset for `@mastra/core` documenting that signal
providers no longer lose memory access when Harness previously forked
agents per mode.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
---------
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Abhi Aiyer <abhi@mastra.ai>1 parent 8b984f4 commit c853d53
3 files changed
Lines changed: 448 additions & 35 deletions
File tree
- .changeset
- packages/core/src/harness
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
| |||
199 | 200 | | |
200 | 201 | | |
201 | 202 | | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
202 | 552 | | |
0 commit comments