Commit 71a820b
fix(mastracode): coordinate signals over Unix socket PubSub (#16669)
This adds a Unix socket PubSub implementation and wires Mastra Code to
use it for local cross-process signal/stream coordination on macOS/Linux
so that you never see the "thread is locked" message when starting
multiple mc instances in the same dir.
Used MC to test this but I need unix pubsub to make it easier to test
drucker agents.
Mastra Code now defaults to a local socket PubSub per project resource
id, skips file thread locks when that cross-process PubSub is active,
and closes the socket PubSub during CLI cleanup. Windows keeps the
existing in-process PubSub/thread-lock behavior for now.
After:
```ts
const signalsPubSub =
configuredPubSub ?? (process.platform === 'win32' ? undefined : createSignalsPubSub(project.resourceId));
```
I tested this with core signal/runtime tests, Unix socket PubSub tests,
and the Mastra Code build/typecheck.
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
ELI5
This PR makes Mastra Code on macOS/Linux use a per-project local
Unix-domain socket so multiple local mastracode processes can share
signals and live-stream updates without getting blocked by file-based
thread locks; Windows behavior is unchanged. It also ensures the socket
PubSub is closed cleanly on shutdown and preserves configured pubsub
behavior unless cross-process mode is enabled.
---
Overview
Adds a Unix-domain socket PubSub transport and wires Mastra Code to use
a deterministic per-resource Unix socket (derived from
project.resourceId) for local cross-process signal and stream
coordination on macOS/Linux. The Mastra Code TUI enables the Unix-socket
PubSub by default when not on Windows (createMastraCode still honors an
explicit configured pubsub), cross-process mode disables file
thread-locks only when active, and shutdown paths close the socket
PubSub. The UnixSocketPubSub implementation handles broker/client roles,
reconnection, stale-socket reclamation, resubscription on reconnect,
rejecting pending subscribe waiters on broker failure, and robust
close/unlink behavior.
---
Key changes
- New IPC transport
- packages/core/src/events/unix-socket-pubsub.ts
- Implements UnixSocketPubSub: newline-delimited JSON over Unix domain
sockets with broker/client semantics, write batching/flush,
resubscribe-on-reconnect, subscribe waiters, and safe close/unlink.
- Public API: socketPath, supportedModes, isBroker, publish, subscribe,
unsubscribe, flush, close.
- Mastra Code integration
- mastracode/src/index.ts
- MastraCodeConfig.unixSocketPubSub?: boolean added.
- createMastraCode derives signalsPubSub from config.pubsub or
(non-Windows) a per-resource Unix-socket PubSub when enabled;
crossProcessPubSub defaults to the Unix-socket choice only if no
configured pubsub is provided. threadLock remains conditional on
crossProcessPubSub.
- mastracode/src/utils/signals-pubsub.ts
- getSignalsPubSubSocketPath(resourceId): deterministic SHA-256–derived
short socket path under app data signals dir.
- createSignalsPubSub(resourceId) factory that returns a
UnixSocketPubSub bound to that path.
- mastracode/src/main.ts and mastracode/src/headless.ts
- TUI entry initializes createMastraCode({ unixSocketPubSub: true }),
stores returned signalsPubSub, and includes signalsPubSub.close() in
async cleanup; headless cleanup also closes optional signalsPubSub.
- mastracode/src/onboarding/settings.ts
- Introduces SignalSettings and adds
GlobalSettings.signals.unixSocketPubSub (default false) with
migration/load handling.
- Tests
- packages/core/src/events/unix-socket-pubsub.test.ts
- Expanded Vitest suite: fan-out, subscriber exception isolation, broker
promotion after close, stale-socket reclamation, and subscribe rejection
when broker disconnects.
- packages/core/src/agent/__tests__/agent-signals.test.ts
- Non-Windows integration test: two AgentThreadStreamRuntime instances
using UnixSocketPubSub on the same socket both receive streamed text
deltas.
- mastracode/src/__tests__/index.test.ts
- Tests cover createMastraCode behavior for provided pubsub vs
crossProcessPubSub and expected threadLock settings.
- Unix-socket tests are skipped on Windows.
- Exports and release notes
- packages/core/src/events/index.ts re-exports unix-socket-pubsub.
- .changeset/unix-socket-signals-pubsub.md added (patch for
`@mastra/core` and mastracode).
---
Platform behavior
- macOS/Linux: TUI enables per-resource Unix socket PubSub by default
(unless overridden) to allow cross-process coordination and avoid
file-based thread locks.
- Windows: unchanged — continues using in-process PubSub and file-based
thread-lock behavior; Unix-socket tests are skipped.
---
Testing performed
- Unit tests for UnixSocketPubSub covering normal and edge cases (broker
disconnect, stale socket, broker promotion, subscriber failures).
- Integration test validating cross-runtime signal/stream delivery
between runtimes using the same socket.
- Mastra Code build/typecheck and core signal/runtime tests run
successfully with the changeset.
<!-- review_stack_entry_start -->
[](https://app.coderabbit.ai/change-stack/mastra-ai/mastra/pull/16669?utm_source=github_walkthrough&utm_medium=github&utm_campaign=change_stack)
<!-- review_stack_entry_end -->
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
---------
Co-authored-by: Mastra Code (openai/gpt-5.5) <noreply@mastra.ai>1 parent 3ff518e commit 71a820b
11 files changed
Lines changed: 796 additions & 6 deletions
File tree
- .changeset
- mastracode/src
- __tests__
- onboarding
- utils
- packages/core/src
- agent/__tests__
- events
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
324 | 324 | | |
325 | 325 | | |
326 | 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 | + | |
327 | 353 | | |
328 | 354 | | |
329 | 355 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
628 | 628 | | |
629 | 629 | | |
630 | 630 | | |
631 | | - | |
| 631 | + | |
| 632 | + | |
632 | 633 | | |
633 | 634 | | |
634 | 635 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
73 | 73 | | |
74 | 74 | | |
75 | 75 | | |
| 76 | + | |
76 | 77 | | |
77 | 78 | | |
78 | 79 | | |
| |||
135 | 136 | | |
136 | 137 | | |
137 | 138 | | |
| 139 | + | |
| 140 | + | |
138 | 141 | | |
139 | 142 | | |
140 | 143 | | |
| |||
232 | 235 | | |
233 | 236 | | |
234 | 237 | | |
235 | | - | |
236 | | - | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
237 | 243 | | |
238 | 244 | | |
239 | 245 | | |
| |||
675 | 681 | | |
676 | 682 | | |
677 | 683 | | |
| 684 | + | |
678 | 685 | | |
679 | 686 | | |
680 | 687 | | |
681 | 688 | | |
682 | | - | |
683 | 689 | | |
684 | 690 | | |
685 | 691 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
| 24 | + | |
24 | 25 | | |
25 | 26 | | |
26 | 27 | | |
| |||
43 | 44 | | |
44 | 45 | | |
45 | 46 | | |
46 | | - | |
| 47 | + | |
47 | 48 | | |
48 | 49 | | |
49 | 50 | | |
50 | 51 | | |
| 52 | + | |
51 | 53 | | |
52 | 54 | | |
53 | 55 | | |
| |||
119 | 121 | | |
120 | 122 | | |
121 | 123 | | |
122 | | - | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
123 | 131 | | |
124 | 132 | | |
125 | 133 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
207 | 207 | | |
208 | 208 | | |
209 | 209 | | |
| 210 | + | |
| 211 | + | |
210 | 212 | | |
211 | 213 | | |
212 | 214 | | |
213 | 215 | | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
214 | 221 | | |
215 | 222 | | |
216 | 223 | | |
| |||
279 | 286 | | |
280 | 287 | | |
281 | 288 | | |
| 289 | + | |
282 | 290 | | |
283 | 291 | | |
284 | 292 | | |
| |||
513 | 521 | | |
514 | 522 | | |
515 | 523 | | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
516 | 530 | | |
517 | 531 | | |
518 | 532 | | |
| |||
633 | 647 | | |
634 | 648 | | |
635 | 649 | | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
636 | 656 | | |
637 | 657 | | |
638 | 658 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
1 | 5 | | |
2 | 6 | | |
3 | 7 | | |
4 | 8 | | |
| 9 | + | |
5 | 10 | | |
6 | 11 | | |
7 | 12 | | |
| |||
884 | 889 | | |
885 | 890 | | |
886 | 891 | | |
| 892 | + | |
| 893 | + | |
| 894 | + | |
| 895 | + | |
| 896 | + | |
| 897 | + | |
| 898 | + | |
| 899 | + | |
| 900 | + | |
| 901 | + | |
| 902 | + | |
| 903 | + | |
| 904 | + | |
| 905 | + | |
| 906 | + | |
| 907 | + | |
| 908 | + | |
| 909 | + | |
| 910 | + | |
| 911 | + | |
| 912 | + | |
| 913 | + | |
| 914 | + | |
| 915 | + | |
| 916 | + | |
| 917 | + | |
| 918 | + | |
| 919 | + | |
| 920 | + | |
| 921 | + | |
| 922 | + | |
| 923 | + | |
| 924 | + | |
| 925 | + | |
| 926 | + | |
| 927 | + | |
| 928 | + | |
| 929 | + | |
| 930 | + | |
| 931 | + | |
| 932 | + | |
| 933 | + | |
| 934 | + | |
| 935 | + | |
| 936 | + | |
| 937 | + | |
| 938 | + | |
| 939 | + | |
| 940 | + | |
| 941 | + | |
| 942 | + | |
| 943 | + | |
| 944 | + | |
| 945 | + | |
| 946 | + | |
| 947 | + | |
| 948 | + | |
| 949 | + | |
| 950 | + | |
| 951 | + | |
| 952 | + | |
| 953 | + | |
| 954 | + | |
| 955 | + | |
887 | 956 | | |
888 | 957 | | |
889 | 958 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
0 commit comments