@cordfuse/crosstalk 5.0.0-alpha.5 → 5.0.0-alpha.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/dispatch.ts +263 -27
- package/src/send.ts +26 -2
- package/template/upstream/PROTOCOL.md +59 -0
- package/template/upstream/actors/concierge.md +20 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cordfuse/crosstalk",
|
|
3
|
-
"version": "5.0.0-alpha.
|
|
3
|
+
"version": "5.0.0-alpha.6",
|
|
4
4
|
"description": "Crosstalk runtime — async messaging between agents over git. The crosstalk CLI plus dispatch, send, attach, chat, and supporting tools.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
package/src/dispatch.ts
CHANGED
|
@@ -124,6 +124,87 @@ function recipients(toField: unknown): string[] {
|
|
|
124
124
|
return [];
|
|
125
125
|
}
|
|
126
126
|
|
|
127
|
+
// Declared lifecycle kind for a message. `work` (default for legacy messages
|
|
128
|
+
// without the field) is the as-tagged intent. The runtime does NOT trust this
|
|
129
|
+
// value directly for the activation decision — see effectiveKind() below.
|
|
130
|
+
// Kept for use as the seed of the effective-kind computation.
|
|
131
|
+
function messageKind(msg: ChannelMessage): 'work' | 'result' {
|
|
132
|
+
const raw = msg.data['kind'];
|
|
133
|
+
return raw === 'result' ? 'result' : 'work';
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// Is `msg` causally a reply to a prior ask? True iff some message strictly
|
|
137
|
+
// before `msg` was sent FROM one of `msg`'s recipients TO `msg`'s sender with
|
|
138
|
+
// declared kind `work`. If so, `msg` is that recipient's answer coming back —
|
|
139
|
+
// regardless of how its sender (a fallible LLM actor, or `crosstalk send`'s
|
|
140
|
+
// `work` default) labelled it.
|
|
141
|
+
//
|
|
142
|
+
// Conservative on multi-recipient `to:` lists: if ANY recipient previously
|
|
143
|
+
// tasked the sender, the message is treated as causally a reply for all
|
|
144
|
+
// recipients. The per-addressee asymmetry in hasPriorWork (below) compensates
|
|
145
|
+
// — only the recipient that actually asked wakes on it. Known v1 limitation:
|
|
146
|
+
// genuine multi-recipient fan-out where one recipient happens to have prior
|
|
147
|
+
// unrelated work to the sender will be demoted to result and suppress wakes
|
|
148
|
+
// for the other recipients. Not observed in Monte Carlo; revisit if it
|
|
149
|
+
// surfaces.
|
|
150
|
+
function isCausalReply(channelMessages: ChannelMessage[], msg: ChannelMessage): boolean {
|
|
151
|
+
const sender = typeof msg.data['from'] === 'string' ? msg.data['from'] : '';
|
|
152
|
+
if (!sender) return false;
|
|
153
|
+
const toList = recipients(msg.data['to']);
|
|
154
|
+
for (const m of channelMessages) {
|
|
155
|
+
if (m.relPath >= msg.relPath) break;
|
|
156
|
+
const mFrom = typeof m.data['from'] === 'string' ? m.data['from'] : '';
|
|
157
|
+
if (!toList.includes(mFrom)) continue;
|
|
158
|
+
if ((m.data['kind'] ?? 'work') === 'result') continue;
|
|
159
|
+
if (recipients(m.data['to']).includes(sender)) return true;
|
|
160
|
+
}
|
|
161
|
+
return false;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// Effective lifecycle kind. The runtime INFERS kind from the causality graph
|
|
165
|
+
// rather than trusting the declared field: a message that is causally a reply
|
|
166
|
+
// is a `result` even if it was labelled `work` (actors routinely report
|
|
167
|
+
// results via `crosstalk send`, which defaults to `work`, and that mislabel
|
|
168
|
+
// forges false reply-causality edges → wake-up loops). Genuine unsolicited
|
|
169
|
+
// tasks (kickoffs, fresh dispatches) have no prior opposite-direction work
|
|
170
|
+
// and keep their `work` kind. See PROTOCOL.md "Message kinds".
|
|
171
|
+
//
|
|
172
|
+
// This is the load-bearing principle the rest of the activation rule rides
|
|
173
|
+
// on: the dispatcher derives semantics from the interaction graph; it never
|
|
174
|
+
// trusts an actor's declaration.
|
|
175
|
+
function effectiveKind(channelMessages: ChannelMessage[], msg: ChannelMessage): 'work' | 'result' {
|
|
176
|
+
if (messageKind(msg) === 'result') return 'result';
|
|
177
|
+
return isCausalReply(channelMessages, msg) ? 'result' : 'work';
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// Reply causality — does `addressee` have a prior `kind: work` outbound to
|
|
181
|
+
// `sender` somewhere in the channel's history strictly before `before`? If
|
|
182
|
+
// yes, an inbound `kind: result` from `sender` to `addressee` is the answer
|
|
183
|
+
// to that ask, and the addressee should wake on it. If no, the result is
|
|
184
|
+
// unsolicited from addressee's POV and is informational only.
|
|
185
|
+
//
|
|
186
|
+
// Uses effectiveKind (not messageKind) when checking prior messages — a
|
|
187
|
+
// mislabeled "work" reply from a prior peer would otherwise forge a false
|
|
188
|
+
// causality edge here, which was the ping-pong root.
|
|
189
|
+
//
|
|
190
|
+
// The channel is already sorted by relPath ascending in
|
|
191
|
+
// listChannelMessages(), so the scan walks chronologically.
|
|
192
|
+
function hasPriorWork(
|
|
193
|
+
channelMessages: ChannelMessage[],
|
|
194
|
+
addressee: string,
|
|
195
|
+
sender: string,
|
|
196
|
+
before: string,
|
|
197
|
+
): boolean {
|
|
198
|
+
for (const m of channelMessages) {
|
|
199
|
+
if (m.relPath >= before) break;
|
|
200
|
+
if (typeof m.data['from'] !== 'string' || m.data['from'] !== addressee) continue;
|
|
201
|
+
if (effectiveKind(channelMessages, m) !== 'work') continue;
|
|
202
|
+
const toList = recipients(m.data['to']);
|
|
203
|
+
if (toList.includes(sender)) return true;
|
|
204
|
+
}
|
|
205
|
+
return false;
|
|
206
|
+
}
|
|
207
|
+
|
|
127
208
|
function composeSystemPrompt(actorPrompt: string): string {
|
|
128
209
|
return [protocolPrompt, actorPrompt]
|
|
129
210
|
.filter((p) => p.length > 0)
|
|
@@ -145,7 +226,12 @@ interface CliResult {
|
|
|
145
226
|
stderr: string;
|
|
146
227
|
}
|
|
147
228
|
|
|
148
|
-
function invokeCli(
|
|
229
|
+
function invokeCli(
|
|
230
|
+
cli: string,
|
|
231
|
+
systemPrompt: string,
|
|
232
|
+
userMessage: string,
|
|
233
|
+
actorName: string,
|
|
234
|
+
): Promise<CliResult> {
|
|
149
235
|
return new Promise((res) => {
|
|
150
236
|
const fullPrompt = `${systemPrompt}\n\n---\n\n${userMessage}`;
|
|
151
237
|
const parts = tokenizeCli(cli);
|
|
@@ -153,14 +239,34 @@ function invokeCli(cli: string, systemPrompt: string, userMessage: string): Prom
|
|
|
153
239
|
res({ status: 1, stdout: '', stderr: 'tokenized cli is empty' });
|
|
154
240
|
return;
|
|
155
241
|
}
|
|
156
|
-
|
|
242
|
+
// detached: true creates a new process group so we can SIGKILL the
|
|
243
|
+
// group (not just the parent) on timeout — orphan children writing
|
|
244
|
+
// to the transport after parent SIGKILL was an observed alpha.5 hazard.
|
|
245
|
+
// Env: CROSSTALK_DISPATCH_ACTOR tells send.ts what to use as --from when
|
|
246
|
+
// the dispatched actor invokes `crosstalk send` without explicit --from.
|
|
247
|
+
const child = spawn(parts[0], parts.slice(1), {
|
|
248
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
249
|
+
detached: true,
|
|
250
|
+
env: { ...process.env, CROSSTALK_DISPATCH_ACTOR: actorName },
|
|
251
|
+
});
|
|
157
252
|
let stdout = '';
|
|
158
253
|
let stderr = '';
|
|
159
254
|
let resolved = false;
|
|
160
255
|
const timeout = setTimeout(() => {
|
|
161
256
|
if (resolved) return;
|
|
162
257
|
resolved = true;
|
|
163
|
-
|
|
258
|
+
// SIGKILL the process group (negative pid) so any children the actor
|
|
259
|
+
// spawned (e.g. crosstalk send subprocesses) die with the parent.
|
|
260
|
+
// Fallback to single-pid kill if the group signal fails (some envs).
|
|
261
|
+
try {
|
|
262
|
+
if (typeof child.pid === 'number') {
|
|
263
|
+
process.kill(-child.pid, 'SIGKILL');
|
|
264
|
+
} else {
|
|
265
|
+
child.kill('SIGKILL');
|
|
266
|
+
}
|
|
267
|
+
} catch {
|
|
268
|
+
try { child.kill('SIGKILL'); } catch { /* already dead */ }
|
|
269
|
+
}
|
|
164
270
|
res({ status: 124, stdout, stderr: stderr + '\n[timeout]' });
|
|
165
271
|
}, 5 * 60_000);
|
|
166
272
|
child.stdout.on('data', (d) => { stdout += d.toString(); });
|
|
@@ -193,14 +299,19 @@ function invokeCli(cli: string, systemPrompt: string, userMessage: string): Prom
|
|
|
193
299
|
function writeReply(
|
|
194
300
|
channelUuid: string,
|
|
195
301
|
fromActor: string,
|
|
196
|
-
toActor: string,
|
|
302
|
+
toActor: string | string[],
|
|
197
303
|
body: string,
|
|
198
304
|
): void {
|
|
199
305
|
const ts = now();
|
|
200
306
|
const dir = join(transportRoot, 'data', 'channels', channelUuid, ts.pathDate);
|
|
201
307
|
mkdirSync(dir, { recursive: true });
|
|
308
|
+
// Auto-replies emitted via stdout are `kind: result` by default — the actor
|
|
309
|
+
// is answering, not initiating new work. Recipients only wake on a result if
|
|
310
|
+
// they previously asked the sender for work in this channel (reply
|
|
311
|
+
// causality, see activation rule below). Actors that want to dispatch new
|
|
312
|
+
// work do so explicitly via `crosstalk send --kind work`.
|
|
202
313
|
const content = serializeFrontmatter(
|
|
203
|
-
{ from: fromActor, to: toActor, type: 'text', timestamp: ts.iso },
|
|
314
|
+
{ from: fromActor, to: toActor, type: 'text', kind: 'result', timestamp: ts.iso },
|
|
204
315
|
body,
|
|
205
316
|
);
|
|
206
317
|
writeFileSync(join(dir, messageFilename(ts)), content);
|
|
@@ -225,16 +336,76 @@ function writeReadReceipt(
|
|
|
225
336
|
interface PendingDispatch {
|
|
226
337
|
actorName: string;
|
|
227
338
|
channelUuid: string;
|
|
228
|
-
|
|
229
|
-
from: string;
|
|
339
|
+
msgs: ChannelMessage[]; // all unread messages addressed to this actor in this channel
|
|
230
340
|
tiers: HostActorTiers;
|
|
231
341
|
}
|
|
232
342
|
|
|
343
|
+
function messageSender(msg: ChannelMessage): string {
|
|
344
|
+
return typeof msg.data['from'] === 'string' ? msg.data['from'] : 'unknown';
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
function formatBatchedUserMessage(msgs: ChannelMessage[]): string {
|
|
348
|
+
if (msgs.length === 1) return msgs[0].body;
|
|
349
|
+
const header = `You have ${msgs.length} new messages in this channel. Process them collectively and reply once.`;
|
|
350
|
+
const parts: string[] = [header];
|
|
351
|
+
for (let i = 0; i < msgs.length; i++) {
|
|
352
|
+
const m = msgs[i];
|
|
353
|
+
const from = messageSender(m);
|
|
354
|
+
const ts = typeof m.data['timestamp'] === 'string' ? (m.data['timestamp'] as string) : '';
|
|
355
|
+
parts.push(`--- Message ${i + 1} of ${msgs.length} (from: ${from}, ref: ${m.relPath}${ts ? `, ts: ${ts}` : ''}) ---`);
|
|
356
|
+
parts.push(m.body);
|
|
357
|
+
}
|
|
358
|
+
return parts.join('\n\n');
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// Split a channel's pending messages (already sorted by relPath) into
|
|
362
|
+
// contiguous batches sized for the actor's concurrency. Contiguous (not
|
|
363
|
+
// round-robin) so each batch's highest relPath is monotone across batches —
|
|
364
|
+
// the cursor advances safely after the dispatch loop's per-batch writes
|
|
365
|
+
// without leaving a gap that would re-dispatch on the next tick.
|
|
366
|
+
//
|
|
367
|
+
// When pending fits within concurrency, every batch is a single message
|
|
368
|
+
// (preserves parallel fan-out — junior-developer with count: 10 and 10
|
|
369
|
+
// pending fan-out messages dispatches 10 parallel CLI invocations of 1
|
|
370
|
+
// message each). When pending exceeds concurrency, batches collapse pending
|
|
371
|
+
// into ~concurrency parallel invocations, each handling ceil(N/concurrency)
|
|
372
|
+
// messages (preserves the fan-in collapse — concierge with count: 1 and 10
|
|
373
|
+
// pending replies dispatches 1 invocation of 10 messages).
|
|
374
|
+
function splitForConcurrency(
|
|
375
|
+
msgs: ChannelMessage[],
|
|
376
|
+
concurrency: number,
|
|
377
|
+
): ChannelMessage[][] {
|
|
378
|
+
if (concurrency <= 1 || msgs.length <= 1) return [msgs];
|
|
379
|
+
const chunkSize = Math.max(1, Math.ceil(msgs.length / concurrency));
|
|
380
|
+
const out: ChannelMessage[][] = [];
|
|
381
|
+
for (let i = 0; i < msgs.length; i += chunkSize) {
|
|
382
|
+
out.push(msgs.slice(i, i + chunkSize));
|
|
383
|
+
}
|
|
384
|
+
return out;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
function distinctSenders(msgs: ChannelMessage[]): string[] {
|
|
388
|
+
const seen = new Set<string>();
|
|
389
|
+
const out: string[] = [];
|
|
390
|
+
for (const m of msgs) {
|
|
391
|
+
const s = messageSender(m);
|
|
392
|
+
if (s !== 'unknown' && !seen.has(s)) {
|
|
393
|
+
seen.add(s);
|
|
394
|
+
out.push(s);
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
return out;
|
|
398
|
+
}
|
|
399
|
+
|
|
233
400
|
async function dispatchOne(p: PendingDispatch): Promise<boolean> {
|
|
234
|
-
//
|
|
235
|
-
//
|
|
236
|
-
|
|
237
|
-
|
|
401
|
+
// Tier resolution uses the first message's `tier:` hint (if any). Batched
|
|
402
|
+
// dispatches assume homogeneous tier preference within an (actor, channel)
|
|
403
|
+
// pairing — true for fan-in (all peer replies omit tier) and for explicit
|
|
404
|
+
// single-message dispatches alike.
|
|
405
|
+
const firstMsg = p.msgs[0];
|
|
406
|
+
const lastMsg = p.msgs[p.msgs.length - 1];
|
|
407
|
+
const preferredTier = typeof firstMsg.data['tier'] === 'string'
|
|
408
|
+
? (firstMsg.data['tier'] as string)
|
|
238
409
|
: undefined;
|
|
239
410
|
let resolved;
|
|
240
411
|
try {
|
|
@@ -257,11 +428,17 @@ async function dispatchOne(p: PendingDispatch): Promise<boolean> {
|
|
|
257
428
|
return false;
|
|
258
429
|
}
|
|
259
430
|
const cli = resolved.cli;
|
|
260
|
-
|
|
431
|
+
|
|
432
|
+
// Quarantine check uses the LAST message's relPath as the batch's identity.
|
|
433
|
+
// Per-message quarantine semantics are preserved because batch boundaries
|
|
434
|
+
// align with cursor checkpoints; if a single message in a batch keeps
|
|
435
|
+
// failing, the cursor never advances past it and it surfaces as a singleton
|
|
436
|
+
// batch on the next tick.
|
|
437
|
+
if (isQuarantined(transportRoot, 'dispatch', p.actorName, p.channelUuid, lastMsg.relPath)) {
|
|
261
438
|
log('dispatch_skipped_quarantined', {
|
|
262
439
|
actor: p.actorName,
|
|
263
440
|
channel: p.channelUuid.slice(0, 8),
|
|
264
|
-
msg:
|
|
441
|
+
msg: lastMsg.relPath,
|
|
265
442
|
});
|
|
266
443
|
return false;
|
|
267
444
|
}
|
|
@@ -269,10 +446,17 @@ async function dispatchOne(p: PendingDispatch): Promise<boolean> {
|
|
|
269
446
|
log('dispatch', {
|
|
270
447
|
actor: p.actorName,
|
|
271
448
|
channel: p.channelUuid.slice(0, 8),
|
|
272
|
-
|
|
449
|
+
batch_size: p.msgs.length,
|
|
450
|
+
first_msg: firstMsg.relPath,
|
|
451
|
+
last_msg: lastMsg.relPath,
|
|
273
452
|
});
|
|
274
453
|
|
|
275
|
-
|
|
454
|
+
// Read receipt per message — preserves the audit trail (each original
|
|
455
|
+
// message gets exactly one receipt) and keeps the stale-receipt sweep
|
|
456
|
+
// correct.
|
|
457
|
+
for (const m of p.msgs) {
|
|
458
|
+
writeReadReceipt(p.channelUuid, p.actorName, messageSender(m), m.relPath);
|
|
459
|
+
}
|
|
276
460
|
|
|
277
461
|
let profile;
|
|
278
462
|
try {
|
|
@@ -296,7 +480,8 @@ async function dispatchOne(p: PendingDispatch): Promise<boolean> {
|
|
|
296
480
|
}
|
|
297
481
|
|
|
298
482
|
const systemPrompt = composeSystemPrompt(profile.systemPrompt);
|
|
299
|
-
const
|
|
483
|
+
const userMessage = formatBatchedUserMessage(p.msgs);
|
|
484
|
+
const result = await invokeCli(cli, systemPrompt, userMessage, p.actorName);
|
|
300
485
|
|
|
301
486
|
if (result.status !== 0) {
|
|
302
487
|
const r = writeDlqEntry(
|
|
@@ -304,12 +489,13 @@ async function dispatchOne(p: PendingDispatch): Promise<boolean> {
|
|
|
304
489
|
'dispatch',
|
|
305
490
|
p.actorName,
|
|
306
491
|
p.channelUuid,
|
|
307
|
-
|
|
492
|
+
lastMsg.relPath,
|
|
308
493
|
`cli exit=${result.status}\n${result.stderr.slice(0, 1000)}`,
|
|
309
494
|
);
|
|
310
495
|
log('dispatch_failed', {
|
|
311
496
|
actor: p.actorName,
|
|
312
497
|
channel: p.channelUuid.slice(0, 8),
|
|
498
|
+
batch_size: p.msgs.length,
|
|
313
499
|
dlq_id: r.id,
|
|
314
500
|
attempts: r.attempts,
|
|
315
501
|
quarantined: r.quarantined,
|
|
@@ -320,12 +506,25 @@ async function dispatchOne(p: PendingDispatch): Promise<boolean> {
|
|
|
320
506
|
|
|
321
507
|
const reply = result.stdout.trim();
|
|
322
508
|
if (reply.length === 0) {
|
|
509
|
+
// Empty stdout on a multi-message batch is treated as success — the
|
|
510
|
+
// actor likely routed via `crosstalk send` and has nothing to add as
|
|
511
|
+
// an auto-reply. For a single-message batch we keep the prior DLQ
|
|
512
|
+
// semantics: a single dispatched message that produces no reply is a
|
|
513
|
+
// protocol violation.
|
|
514
|
+
if (p.msgs.length > 1) {
|
|
515
|
+
log('dispatch_batch_silent_ok', {
|
|
516
|
+
actor: p.actorName,
|
|
517
|
+
channel: p.channelUuid.slice(0, 8),
|
|
518
|
+
batch_size: p.msgs.length,
|
|
519
|
+
});
|
|
520
|
+
return true;
|
|
521
|
+
}
|
|
323
522
|
const r = writeDlqEntry(
|
|
324
523
|
transportRoot,
|
|
325
524
|
'dispatch',
|
|
326
525
|
p.actorName,
|
|
327
526
|
p.channelUuid,
|
|
328
|
-
|
|
527
|
+
lastMsg.relPath,
|
|
329
528
|
'cli returned empty reply',
|
|
330
529
|
);
|
|
331
530
|
log('dispatch_empty_reply', {
|
|
@@ -338,7 +537,14 @@ async function dispatchOne(p: PendingDispatch): Promise<boolean> {
|
|
|
338
537
|
return false;
|
|
339
538
|
}
|
|
340
539
|
|
|
341
|
-
|
|
540
|
+
// Auto-reply addressing: single-sender batches reply to that sender
|
|
541
|
+
// (preserves prior behavior). Multi-sender batches address all distinct
|
|
542
|
+
// senders so each peer sees the response.
|
|
543
|
+
const senders = distinctSenders(p.msgs);
|
|
544
|
+
const replyTo: string | string[] = senders.length <= 1
|
|
545
|
+
? (senders[0] ?? messageSender(firstMsg))
|
|
546
|
+
: senders;
|
|
547
|
+
writeReply(p.channelUuid, p.actorName, replyTo, reply);
|
|
342
548
|
return true;
|
|
343
549
|
}
|
|
344
550
|
|
|
@@ -397,6 +603,13 @@ async function dispatchTick(): Promise<TickResult> {
|
|
|
397
603
|
const tiers = host.actors[actorName];
|
|
398
604
|
const concurrency = actorConcurrency(tiers);
|
|
399
605
|
|
|
606
|
+
// Mailbox batch-drain: for each channel, collect ALL unread messages
|
|
607
|
+
// addressed to this actor into a single PendingDispatch. This collapses
|
|
608
|
+
// fan-in O(N) into O(1) CLI invocations and prevents one actor's deep
|
|
609
|
+
// backlog from starving its peers in the (actor, channel) scan order.
|
|
610
|
+
// Read receipts and self-sent messages are filtered here — receipts
|
|
611
|
+
// are bookkeeping the actor already produced, and self-messages would
|
|
612
|
+
// create a wake-up loop.
|
|
400
613
|
const pending: PendingDispatch[] = [];
|
|
401
614
|
const channels = discoverChannels(transportRoot);
|
|
402
615
|
for (const channelUuid of channels) {
|
|
@@ -412,27 +625,50 @@ async function dispatchTick(): Promise<TickResult> {
|
|
|
412
625
|
post_cursor_msgs: post.length,
|
|
413
626
|
});
|
|
414
627
|
|
|
628
|
+
const channelBatch: ChannelMessage[] = [];
|
|
415
629
|
for (const msg of post) {
|
|
416
630
|
const to = recipients(msg.data['to']);
|
|
417
631
|
const from = typeof msg.data['from'] === 'string' ? msg.data['from'] : 'unknown';
|
|
418
|
-
|
|
632
|
+
const msgType = typeof msg.data['type'] === 'string' ? msg.data['type'] : 'text';
|
|
633
|
+
if (!to.includes(actorName) || from === actorName || msgType === 'read') {
|
|
634
|
+
writeCursor(transportRoot, actorName, channelUuid, msg.relPath);
|
|
635
|
+
continue;
|
|
636
|
+
}
|
|
637
|
+
// Lifecycle activation rule. `work` always wakes. `result` wakes
|
|
638
|
+
// only if reply-causal — actor previously sent the sender a `work`
|
|
639
|
+
// in this channel. The kind used here is the runtime's INFERRED
|
|
640
|
+
// effective kind, not the actor's declared kind: a message that's
|
|
641
|
+
// causally a reply is treated as `result` even when an actor (or
|
|
642
|
+
// `crosstalk send`'s default) labelled it `work`, so a fan-in peer
|
|
643
|
+
// mislabeling its reply can't forge a wake-up loop. See PROTOCOL.md
|
|
644
|
+
// "Message kinds".
|
|
645
|
+
const kind = effectiveKind(messages, msg);
|
|
646
|
+
if (kind === 'result' && !hasPriorWork(messages, actorName, from, msg.relPath)) {
|
|
419
647
|
writeCursor(transportRoot, actorName, channelUuid, msg.relPath);
|
|
420
648
|
continue;
|
|
421
649
|
}
|
|
422
|
-
|
|
650
|
+
channelBatch.push(msg);
|
|
651
|
+
}
|
|
652
|
+
if (channelBatch.length > 0) {
|
|
653
|
+
const groups = splitForConcurrency(channelBatch, concurrency);
|
|
654
|
+
for (const g of groups) {
|
|
655
|
+
pending.push({ actorName, channelUuid, msgs: g, tiers });
|
|
656
|
+
}
|
|
423
657
|
}
|
|
424
658
|
}
|
|
425
659
|
|
|
660
|
+
// Concurrency now applies across (channel) batches, not individual
|
|
661
|
+
// messages. Each batch is one CLI invocation regardless of how many
|
|
662
|
+
// messages it carries. Cursor advances to the last message in the batch
|
|
663
|
+
// on success or skip — failure (DLQ) leaves the cursor behind so the
|
|
664
|
+
// tail of the batch retries.
|
|
426
665
|
for (let i = 0; i < pending.length; i += concurrency) {
|
|
427
666
|
const batch = pending.slice(i, i + concurrency);
|
|
428
667
|
const results = await Promise.all(batch.map((p) => dispatchOne(p)));
|
|
429
668
|
for (let j = 0; j < batch.length; j++) {
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
batch[j].channelUuid,
|
|
434
|
-
batch[j].msg.relPath,
|
|
435
|
-
);
|
|
669
|
+
const p = batch[j];
|
|
670
|
+
const lastRelPath = p.msgs[p.msgs.length - 1].relPath;
|
|
671
|
+
writeCursor(transportRoot, p.actorName, p.channelUuid, lastRelPath);
|
|
436
672
|
if (results[j]) didWork = true;
|
|
437
673
|
}
|
|
438
674
|
}
|
package/src/send.ts
CHANGED
|
@@ -17,17 +17,40 @@ function flag(name: string): string | undefined {
|
|
|
17
17
|
async function main(): Promise<void> {
|
|
18
18
|
const channelUuid = flag('--channel');
|
|
19
19
|
const to = flag('--to');
|
|
20
|
-
|
|
20
|
+
// Sender identity precedence:
|
|
21
|
+
// 1. --from on the command line (explicit operator/actor choice)
|
|
22
|
+
// 2. CROSSTALK_DISPATCH_ACTOR env var (set by dispatch.ts when it spawns
|
|
23
|
+
// an actor's CLI — so the actor's outbound messages route as itself,
|
|
24
|
+
// not as the operator). Fixes the alpha.5 finding where concierge's
|
|
25
|
+
// fan-out messages went out as `from=steve` because send.ts fell
|
|
26
|
+
// through to USER instead.
|
|
27
|
+
// 3. $USER (interactive operator default)
|
|
28
|
+
// 4. literal 'steve' as last resort
|
|
29
|
+
const from = flag('--from')
|
|
30
|
+
?? process.env['CROSSTALK_DISPATCH_ACTOR']
|
|
31
|
+
?? process.env['USER']
|
|
32
|
+
?? 'steve';
|
|
21
33
|
const tier = flag('--tier');
|
|
34
|
+
// Lifecycle kind. `work` (default) — recipient is being asked to act, will
|
|
35
|
+
// wake on receipt. `result` — informational reply, wakes the recipient only
|
|
36
|
+
// if it previously asked the sender for work (reply causality). See
|
|
37
|
+
// PROTOCOL.md "Message kinds". Proactive sends default to `work`; the
|
|
38
|
+
// runtime's auto-reply path defaults to `result`.
|
|
39
|
+
const kind = flag('--kind') ?? 'work';
|
|
22
40
|
const body = argv[argv.length - 1];
|
|
23
41
|
|
|
24
42
|
if (!channelUuid || !to || !body || body.startsWith('--')) {
|
|
25
43
|
console.error(
|
|
26
|
-
'Usage:
|
|
44
|
+
'Usage: crosstalk send --channel <uuid> --to <actor> [--from <actor>] [--tier <name>] [--kind work|result] "<message body>"',
|
|
27
45
|
);
|
|
28
46
|
process.exit(1);
|
|
29
47
|
}
|
|
30
48
|
|
|
49
|
+
if (kind !== 'work' && kind !== 'result') {
|
|
50
|
+
console.error(`Invalid --kind '${kind}'. Must be 'work' or 'result'.`);
|
|
51
|
+
process.exit(1);
|
|
52
|
+
}
|
|
53
|
+
|
|
31
54
|
await withLock('dispatch', async () => {
|
|
32
55
|
const ts = now();
|
|
33
56
|
const dir = join(transportRoot, 'data', 'channels', channelUuid, ts.pathDate);
|
|
@@ -37,6 +60,7 @@ async function main(): Promise<void> {
|
|
|
37
60
|
from,
|
|
38
61
|
to,
|
|
39
62
|
type: 'text',
|
|
63
|
+
kind,
|
|
40
64
|
timestamp: ts.iso,
|
|
41
65
|
};
|
|
42
66
|
if (tier) frontmatter.tier = tier;
|
|
@@ -50,8 +50,22 @@ This does NOT apply to:
|
|
|
50
50
|
|
|
51
51
|
If you are *authoring* an actor profile for a compute role, write the system prompt to require evidence. Without that requirement, downstream validators can't distinguish shortcut results from honest ones — and shortcut results silently corrupt aggregates.
|
|
52
52
|
|
|
53
|
+
### PRNG requirement for compute tasks
|
|
54
|
+
|
|
55
|
+
When a compute task requires pseudo-random numbers (Monte Carlo simulations, sampling, statistical estimation, etc.), **do not pick an ad-hoc PRNG.** Many languages' default `random()` functions, naive LCGs (`a*seed + c mod m` with arbitrary constants), or homegrown XOR-shift schemes produce streams with statistical defects that bias aggregates — particularly when multiple instances run with adjacent seeds and produce correlated streams.
|
|
56
|
+
|
|
57
|
+
Use one of these:
|
|
58
|
+
|
|
59
|
+
- **JavaScript/Node:** `mulberry32(seed)` (one canonical implementation: `function mulberry32(a){return function(){a|=0;a=a+0x6D2B79F5|0;var t=Math.imul(a^a>>>15,1|a);t=t+Math.imul(t^t>>>7,61|t)^t;return((t^t>>>14)>>>0)/4294967296}}`). Derive distinct seeds per instance via a large multiplier (e.g., `instance_index * 1000003`) to decorrelate streams.
|
|
60
|
+
- **Python:** `random.Random(seed)` (per-instance instance, NOT the module-level `random.random`). For higher-quality requirements use `secrets.SystemRandom()` or `numpy.random.Generator(np.random.PCG64(seed))`.
|
|
61
|
+
- **Other:** any well-documented passing-Big-Crush PRNG (PCG, xoshiro256++, ChaCha20-based, etc.) with explicit seeding.
|
|
62
|
+
|
|
63
|
+
If you are unsure, ask. Better to ask once than to pollute an aggregate with biased samples.
|
|
64
|
+
|
|
53
65
|
**Worked example from this protocol's UAT.** 10 junior-developer instances were given "throw 100M darts" with a loose prompt. 7 ran the canonical Monte Carlo loop and produced statistically clean results. 1 produced an estimate 633σ from the expected mean — almost certainly a shortcut. 2 others produced identical wrong values, suggesting a shared shortcut path. When the same 10 were re-prompted with "show your code" plus literal pseudocode, all 10 produced canonical implementations and clean results. The senior validator caught the original outlier; without it, the aggregate would have been silently corrupted.
|
|
54
66
|
|
|
67
|
+
**Second UAT worked example (PRNG-quality).** A subsequent 10-junior fan-out without PRNG guidance got 5/10 valid: instance 1 used a 16-bit-truncated LCG (π≈3.032, badly broken); instances 2/5/8 picked the same `a=1103515245 / 0x7fffffff` LCG and produced **identical** inside-counts from adjacent seeds (correlated streams); instance 9 picked a third biased option. After moving the PRNG requirement into the spec, the same 10-junior fan-out hit 10/10 valid (every instance used the prescribed mulberry32 with the prescribed seed formula). This is why this section exists.
|
|
68
|
+
|
|
55
69
|
## Available tools
|
|
56
70
|
|
|
57
71
|
You have shell access. You can invoke these tools any time you decide they help with your reply. All of them run from the transport root (the current working directory). The tools are documented here so you can pick the right one from natural-language intent — e.g. "check what the dispatch state looks like" → `crosstalk status`.
|
|
@@ -130,6 +144,51 @@ Subchannels exist for focused work — a channel with a `parent:` field in its `
|
|
|
130
144
|
|
|
131
145
|
When dispatch processes your message, it writes a `type: read` receipt before invoking you and a `type: text` reply after you respond. You only ever produce the text reply — the read receipt is the runtime's signal that a message was claimed. You can rely on read receipts when reasoning about whether a previous message was actually processed.
|
|
132
146
|
|
|
147
|
+
Read receipts do NOT themselves trigger a dispatch. They are bookkeeping artefacts; the runtime filters them out of the dispatch scan so a receipt addressed to you will not wake you. If you want a peer to act on something, send a `type: text` message via `crosstalk send`.
|
|
148
|
+
|
|
149
|
+
## Batched delivery
|
|
150
|
+
|
|
151
|
+
When the runtime activates you, it hands you **all** the unread messages addressed to you in the same channel — not one at a time. If there are N pending messages, you'll see one prompt containing all N, prefixed by `--- Message K of N (from: ..., ref: ...) ---` delimiters. Process them collectively and emit a single reply that addresses the batch.
|
|
152
|
+
|
|
153
|
+
This is the mailbox semantics aggregating actors depend on: a coordinator that fans out to 10 peers wakes once after all 10 reply, sees all 10 results in one prompt, and dispatches the aggregator exactly once. Same actor model as Erlang / Akka — one activation drains the mailbox.
|
|
154
|
+
|
|
155
|
+
If your work for the batch is fully routed via `crosstalk send` (e.g. you forwarded results to an aggregator) and you have nothing further to say, you may leave stdout empty — for multi-message batches this is treated as success, not a DLQ entry. For a single-message dispatch, empty stdout remains a protocol violation (you were addressed; respond).
|
|
156
|
+
|
|
157
|
+
## Message kinds
|
|
158
|
+
|
|
159
|
+
Every message carries a `kind:` field describing its purpose. Two kinds are defined:
|
|
160
|
+
|
|
161
|
+
| Kind | Meaning | Wakes recipient? |
|
|
162
|
+
|---|---|---|
|
|
163
|
+
| `work` | A task. Recipient is being asked to act. | **Always.** |
|
|
164
|
+
| `result` | The output of work. Informational. | **Iff reply-causal** — the recipient previously sent the sender a `kind: work` in this channel. |
|
|
165
|
+
|
|
166
|
+
Plus `type: read` (receipts; never wake — already documented above).
|
|
167
|
+
|
|
168
|
+
**The kind is RUNTIME-INFERRED, not authoritative as declared.** When the dispatcher considers waking an actor on a message, it does not trust the declared `kind:` field directly. Instead, it computes the *effective* kind from the channel's interaction graph: if some earlier message in the channel was sent FROM one of this message's recipients TO this message's sender with declared kind `work`, then this message is *causally a reply* and is treated as `kind: result` regardless of how it was labelled. Only genuine unsolicited messages (no prior opposite-direction work) are treated as `work`.
|
|
169
|
+
|
|
170
|
+
This is the load-bearing principle of the dispatch layer: **the runtime derives message semantics from the interaction graph; it never trusts an actor's declaration.** Actors are fallible declarers — LLMs given two valid reply paths (stdout vs. `crosstalk send`) pick between them probabilistically, and `crosstalk send`'s `--kind work` default is the wrong tag when the actor is using `send` to reply. Inferring kind structurally neutralizes mislabels at the dispatcher level so a fan-in peer mis-tagging its reply can't forge a wake-up loop.
|
|
171
|
+
|
|
172
|
+
**Reply causality:** a `kind: result` (declared or inferred) wakes its addressee **only if** the addressee previously sent a `kind: work` (effective kind) to the result's sender in the same channel. Replies wake the asker. If you never asked, a result addressed to you is informational and the runtime will not activate you on it.
|
|
173
|
+
|
|
174
|
+
This is what stops fan-in patterns from oscillating. When a coordinator processes a batch of N peer results and emits a stdout reply, the runtime auto-addresses that reply to the peers — but none of them woke the coordinator with a `work`, so the reply doesn't wake them back. Loop closed at the dispatcher level, no actor profile heroics required.
|
|
175
|
+
|
|
176
|
+
**Defaults:**
|
|
177
|
+
- `crosstalk send` without `--kind` produces `kind: work` (proactive dispatches are tasks).
|
|
178
|
+
- Runtime auto-replies from stdout are `kind: result` (the actor is answering, not initiating).
|
|
179
|
+
- Override either with `crosstalk send --kind work` or `--kind result`.
|
|
180
|
+
|
|
181
|
+
Because kind is runtime-inferred, getting the declared field "wrong" rarely hurts — the effective-kind computation usually fixes it. But declaring it correctly still helps log readability and DLQ diagnostics, and remains the right thing to do.
|
|
182
|
+
|
|
183
|
+
**Backwards compat:** legacy messages without a `kind:` field are treated as `kind: work` declared; effective-kind inference still applies. Existing transports continue to function; older history doesn't need to be re-tagged.
|
|
184
|
+
|
|
185
|
+
**When to use which (still useful as intent, even if non-load-bearing):**
|
|
186
|
+
- Dispatching a peer to do something → `--kind work` (or omit; default).
|
|
187
|
+
- Forwarding finished output to whoever asked (operator, aggregator) → `--kind result` (or rely on the stdout auto-reply).
|
|
188
|
+
- Broadcasting an FYI you don't want peers to act on → `--kind result` addressed to peers who never asked you for work; they won't wake.
|
|
189
|
+
|
|
190
|
+
**Known v1 limitation:** the causality scan is conservative on multi-recipient `to:` lists — if ANY recipient previously tasked the sender, the message is treated as causally a reply for ALL recipients. In practice the per-addressee causality check (only the actual asker wakes on a result) compensates correctly. The edge case worth knowing: genuine multi-recipient fan-out where one recipient happens to have prior unrelated work to the sender will suppress wakes for the other recipients. Not observed in Monte Carlo; revisit if it surfaces in a real topology.
|
|
191
|
+
|
|
133
192
|
## Other actors
|
|
134
193
|
|
|
135
194
|
- Host files at `hosts/<alias>.md` declare which actors run on which machines.
|
|
@@ -103,3 +103,23 @@ You receive requests from any participant — human or machine — and act on th
|
|
|
103
103
|
- **Empty hosts directory:** if `manifest/hosts/` is absent or empty, proceed normally — routing table is empty, use bare actor names only.
|
|
104
104
|
|
|
105
105
|
You do not write production code. You do not make architectural decisions.
|
|
106
|
+
|
|
107
|
+
**Orchestration termination — this is load-bearing.** When your job in a given dispatch is to ROUTE work (fan-out to specialists, forward replies, dispatch follow-ups), the right pattern is:
|
|
108
|
+
|
|
109
|
+
1. Read the incoming message and decide what to dispatch.
|
|
110
|
+
2. Write the dispatch messages via `crosstalk send` (one per recipient).
|
|
111
|
+
3. Write a short stdout reply to the original sender confirming what you dispatched (e.g. "dispatched 10 Monte Carlo runs to junior-developer; will forward aggregation when results arrive").
|
|
112
|
+
4. **EXIT immediately. Do NOT poll the channel waiting for replies.** Do NOT loop reading messages "until the work is done." The runtime re-dispatches you the moment any new message addresses you. Your job in any single dispatch is one turn of work, not a long-running orchestration loop.
|
|
113
|
+
|
|
114
|
+
If you stay alive after step 3 — polling, waiting, "checking back" — you burn the dispatch wall-clock budget until SIGKILL fires at the timeout. That looks like a hang to the operator and pollutes the DLQ even though your useful work already landed. Exit promptly.
|
|
115
|
+
|
|
116
|
+
When the peers reply, the runtime dispatches you a NEW turn. Use THAT turn to read replies and continue the work. Each step is its own dispatch. You don't have to keep state in memory across them — the channel IS the state.
|
|
117
|
+
|
|
118
|
+
**Routing topology — do not fragment fan-outs into subchannels or chain replies through other actors.** When you orchestrate N peers, hold this shape:
|
|
119
|
+
|
|
120
|
+
- **Same channel.** Dispatch all N peers in the SAME channel as the original request. The runtime routes by `to:` field; you do not need a subchannel for isolation. Subchannels are for the operator's narrative organization (e.g. "weekly planning", "incident review"), not for orchestration topology. Creating a fan-out subchannel makes the cursor space sprawl and complicates aggregation.
|
|
121
|
+
- **Peers reply to YOU, not to downstream consumers.** When you dispatch peers, include explicit reply-to guidance in each message body (e.g. "reply to concierge with your result; do NOT send your result to any other actor"). You are the collection point. If peers also send copies to a downstream aggregator, that aggregator will be re-dispatched once per peer message — wasting calls and producing redundant aggregations.
|
|
122
|
+
- **Aggregate exactly once.** Wait until you've seen all N peer replies (across however many subsequent dispatches that takes; you can count by scanning the channel for messages from each peer addressed to you). Only THEN dispatch the aggregator (e.g. senior-software-engineer) in a SINGLE message containing the collected results. Never N messages, never one per peer reply.
|
|
123
|
+
- **Forward the aggregator's final reply to the operator — explicitly, via `crosstalk send`.** When the aggregator replies to you, your stdout auto-reply goes back to the *aggregator*, NOT to the operator — so a stdout-only response means the operator never sees the answer. On the dispatch turn where you read the aggregator's final reply you MUST run `crosstalk send --to <original-requester> --kind result "<the aggregator's final answer, quoted in full>"` so the operator actually receives it. The original requester is the `from:` of the kickoff message that started this orchestration (e.g. `steve`). Do this, then exit. Delivering the final only as a reply to the aggregator is an orchestration failure — the operator asked the question and must get the answer.
|
|
124
|
+
|
|
125
|
+
If you find yourself dispatching the aggregator multiple times for a single orchestration task, you have the topology wrong — peers must reply to you, you must collect, and you must dispatch the aggregator exactly once.
|