peer-bridge 0.0.0-stage → 0.7.0
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/LICENSE +21 -0
- package/README.ko.md +202 -0
- package/README.md +242 -2
- package/adapters/antigravity/bin/agy-hook.mjs +24 -0
- package/adapters/antigravity/hooks.json +23 -0
- package/adapters/antigravity/mcp_config.json +8 -0
- package/adapters/antigravity/plugin.json +4 -0
- package/adapters/antigravity/rules/AGENTS.md +6 -0
- package/adapters/antigravity/skills/peer-bridge/SKILL.md +19 -0
- package/adapters/claude/.claude-plugin/plugin.json +5 -0
- package/adapters/claude/.mcp.json +8 -0
- package/adapters/claude/bin/cc-hook.mjs +20 -0
- package/adapters/claude/hooks/hooks.json +46 -0
- package/adapters/claude/skills/peer-bridge/SKILL.md +19 -0
- package/adapters/codex/bin/codex-hook.mjs +10 -0
- package/adapters/codex/config.toml.example +5 -0
- package/adapters/codex/hooks.json +44 -0
- package/adapters/codex/skills/peer-bridge/SKILL.md +12 -0
- package/adapters/gemini/bin/gemini-hook.mjs +10 -0
- package/adapters/gemini/settings.json +51 -0
- package/adapters/gemini/skills/peer-bridge/SKILL.md +12 -0
- package/adapters/paseo/SKILL.md +24 -0
- package/bin/pbridge-mcp.mjs +37 -0
- package/bin/pbridge-tui.mjs +7 -0
- package/bin/pbridge.mjs +5 -0
- package/bin/pbridged.mjs +16 -0
- package/package.json +57 -4
- package/scripts/postinstall.mjs +25 -0
- package/scripts/preuninstall.mjs +20 -0
- package/skills/peer-bridge/SKILL.md +70 -0
- package/src/adapters/antigravity/hooks.mjs +207 -0
- package/src/adapters/claude/hooks.mjs +204 -0
- package/src/adapters/codex/hooks.mjs +142 -0
- package/src/adapters/gemini/hooks.mjs +159 -0
- package/src/adapters/installer.mjs +328 -0
- package/src/cli/main.mjs +785 -0
- package/src/client/rpc.mjs +243 -0
- package/src/config.mjs +154 -0
- package/src/daemon/broker.mjs +1799 -0
- package/src/daemon/resolver.mjs +200 -0
- package/src/daemon/server.mjs +221 -0
- package/src/index.mjs +60 -0
- package/src/mcp/server.mjs +668 -0
- package/src/orchestrator/workflows.mjs +496 -0
- package/src/project.mjs +81 -0
- package/src/protocol.mjs +203 -0
- package/src/runtime/conv-api.mjs +101 -0
- package/src/runtime/host-discovery.mjs +635 -0
- package/src/runtime/native-claude.mjs +88 -0
- package/src/runtime/paseo.mjs +289 -0
- package/src/runtime/workers.mjs +751 -0
- package/src/skills.mjs +115 -0
- package/src/storage/db.mjs +704 -0
- package/src/tui/render.mjs +472 -0
- package/src/tui/viewer.mjs +443 -0
|
@@ -0,0 +1,496 @@
|
|
|
1
|
+
import { RpcError, WORKFLOW_TEMPLATES, makeId } from '../protocol.mjs';
|
|
2
|
+
|
|
3
|
+
function parseReviewVerdict(text) {
|
|
4
|
+
const raw = String(text || '');
|
|
5
|
+
if (/VERDICT\s*:\s*CHANGES_REQUESTED\b/i.test(raw) || /"verdict"\s*:\s*"changes_requested"/i.test(raw)) {
|
|
6
|
+
return { approved: false, verdict: 'CHANGES_REQUESTED' };
|
|
7
|
+
}
|
|
8
|
+
if (/VERDICT\s*:\s*APPROVED\b/i.test(raw) || /"verdict"\s*:\s*"approved"/i.test(raw)) {
|
|
9
|
+
return { approved: true, verdict: 'APPROVED' };
|
|
10
|
+
}
|
|
11
|
+
return { approved: false, verdict: 'CHANGES_REQUESTED' };
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function formatArtifactLines(report) {
|
|
15
|
+
if (!report || !Array.isArray(report.expected) || report.expected.length === 0) {
|
|
16
|
+
return 'None specified.';
|
|
17
|
+
}
|
|
18
|
+
const present = (report.present || []).map((a) => `- [OK] ${a.path} (${a.size}B)`);
|
|
19
|
+
const missing = (report.missing || []).map((m) => `- [MISSING] ${m}`);
|
|
20
|
+
return [...present, ...missing].join('\n');
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export class WorkflowOrchestrator {
|
|
24
|
+
constructor(broker) {
|
|
25
|
+
this.broker = broker;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async _resolveOrSpawnParticipant(spec, { workflowId, defaultRole, cwd, spawnedPeers }) {
|
|
29
|
+
if (!spec) {
|
|
30
|
+
throw new RpcError('INVALID_PARAMS', `Missing participant spec for "${defaultRole}"`);
|
|
31
|
+
}
|
|
32
|
+
if (typeof spec === 'string') {
|
|
33
|
+
return { handle: spec, spawned: false };
|
|
34
|
+
}
|
|
35
|
+
if (spec.to && !spec.spawn) {
|
|
36
|
+
return { handle: spec.to, spawned: false };
|
|
37
|
+
}
|
|
38
|
+
const spawnSpec = spec.spawn || spec;
|
|
39
|
+
const runtime = spawnSpec.runtime;
|
|
40
|
+
if (!runtime) {
|
|
41
|
+
throw new RpcError('INVALID_PARAMS', `Participant "${defaultRole}" requires "to" or "spawn.runtime"`);
|
|
42
|
+
}
|
|
43
|
+
const name = spawnSpec.name || `${defaultRole}-${workflowId.slice(-6)}`;
|
|
44
|
+
const res = await this.broker.spawnPeer({
|
|
45
|
+
...spawnSpec,
|
|
46
|
+
runtime,
|
|
47
|
+
name,
|
|
48
|
+
cwd: spawnSpec.cwd || cwd || process.cwd(),
|
|
49
|
+
spawned_by: `workflow:${workflowId}`,
|
|
50
|
+
initial_prompt: undefined,
|
|
51
|
+
});
|
|
52
|
+
spawnedPeers.push(res.peer.peer_id);
|
|
53
|
+
return { handle: res.peer.handle, peerId: res.peer.peer_id, spawned: true };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async _cleanupSpawnedPeers(spawnedPeers, { autoClose }) {
|
|
57
|
+
if (!autoClose || !spawnedPeers.length) return [];
|
|
58
|
+
const closed = [];
|
|
59
|
+
for (const peerId of spawnedPeers) {
|
|
60
|
+
try {
|
|
61
|
+
await this.broker.closePeer({
|
|
62
|
+
target: peerId,
|
|
63
|
+
policy: 'archive',
|
|
64
|
+
reason: 'Workflow ephemeral peer cleanup',
|
|
65
|
+
});
|
|
66
|
+
closed.push(peerId);
|
|
67
|
+
} catch {
|
|
68
|
+
// ignore cleanup failures
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return closed;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async _notifyCoordinator(wf, summaryText) {
|
|
75
|
+
if (!wf.coordinator_handle || wf.spec?.notify_on_finish === false) return null;
|
|
76
|
+
try {
|
|
77
|
+
return await this.broker.sendMessage({
|
|
78
|
+
to: wf.coordinator_handle,
|
|
79
|
+
from: { runtime: 'cli', name: `workflow:${wf.template}` },
|
|
80
|
+
kind: 'reply',
|
|
81
|
+
reply_expected: false,
|
|
82
|
+
override_loop_guard: true,
|
|
83
|
+
body: `<workflow-result workflow-id="${wf.workflow_id}" template="${wf.template}" status="${wf.status}">\n${summaryText}\n</workflow-result>`,
|
|
84
|
+
});
|
|
85
|
+
} catch {
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
async _executeCrossReview(workflowId, params) {
|
|
91
|
+
const cwd = params.cwd || process.cwd();
|
|
92
|
+
const maxRounds = Math.max(1, Math.min(5, Number(params.max_rounds ?? 2)));
|
|
93
|
+
const expectedArtifacts = Array.isArray(params.expected_artifacts) ? params.expected_artifacts : [];
|
|
94
|
+
const acceptanceCriteria = Array.isArray(params.acceptance_criteria) ? params.acceptance_criteria : [];
|
|
95
|
+
const requireArtifacts = Boolean(params.require_artifacts);
|
|
96
|
+
const autoClose = params.auto_close !== false;
|
|
97
|
+
const timeoutMs = Number(params.timeout_ms ?? 180_000);
|
|
98
|
+
const taskPrompt = String(params.prompt || params.task || '').trim();
|
|
99
|
+
const spawnedPeers = [];
|
|
100
|
+
const rounds = [];
|
|
101
|
+
const taskIds = [];
|
|
102
|
+
|
|
103
|
+
try {
|
|
104
|
+
if (!taskPrompt) {
|
|
105
|
+
throw new RpcError('INVALID_PARAMS', 'cross-review requires "prompt" (or "task")');
|
|
106
|
+
}
|
|
107
|
+
const author = await this._resolveOrSpawnParticipant(params.author, {
|
|
108
|
+
workflowId,
|
|
109
|
+
defaultRole: 'author',
|
|
110
|
+
cwd,
|
|
111
|
+
spawnedPeers,
|
|
112
|
+
});
|
|
113
|
+
const reviewer = await this._resolveOrSpawnParticipant(params.reviewer, {
|
|
114
|
+
workflowId,
|
|
115
|
+
defaultRole: 'reviewer',
|
|
116
|
+
cwd,
|
|
117
|
+
spawnedPeers,
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
this.broker.db.updateWorkflow(workflowId, {
|
|
121
|
+
state: { phase: 'author_draft', author: author.handle, reviewer: reviewer.handle, round: 0 },
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
const draftTask = await this.broker.delegateTask({
|
|
125
|
+
to: author.handle,
|
|
126
|
+
from: params.coordinator || { runtime: 'cli', name: `workflow:${workflowId}` },
|
|
127
|
+
title: params.name || 'Cross-Review Draft',
|
|
128
|
+
prompt: taskPrompt,
|
|
129
|
+
expected_artifacts: expectedArtifacts,
|
|
130
|
+
acceptance_criteria: acceptanceCriteria,
|
|
131
|
+
require_artifacts: false,
|
|
132
|
+
notify_on_finish: false,
|
|
133
|
+
workflow_id: workflowId,
|
|
134
|
+
step_key: 'author_draft',
|
|
135
|
+
cwd,
|
|
136
|
+
wait: true,
|
|
137
|
+
timeout_ms: timeoutMs,
|
|
138
|
+
});
|
|
139
|
+
taskIds.push(draftTask.request_id);
|
|
140
|
+
if (draftTask.status !== 'completed') {
|
|
141
|
+
throw new Error(draftTask.error || `Author draft task failed (${draftTask.status})`);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
let latestOutput = draftTask.result_summary || '';
|
|
145
|
+
let latestArtifacts = draftTask.artifact_report || null;
|
|
146
|
+
let approved = false;
|
|
147
|
+
let roundsUsed = 0;
|
|
148
|
+
|
|
149
|
+
for (let round = 1; round <= maxRounds; round++) {
|
|
150
|
+
roundsUsed = round;
|
|
151
|
+
this.broker.db.updateWorkflow(workflowId, {
|
|
152
|
+
state: { phase: `review_round_${round}`, author: author.handle, reviewer: reviewer.handle, round },
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
const reviewPrompt = [
|
|
156
|
+
`Cross-review round ${round}/${maxRounds} for workflow ${workflowId}.`,
|
|
157
|
+
`Original Task:\n${taskPrompt}`,
|
|
158
|
+
`Author (${author.handle}) Latest Output:\n${latestOutput}`,
|
|
159
|
+
`Artifact Verification:\n${formatArtifactLines(latestArtifacts)}`,
|
|
160
|
+
`Inspect the output and artifacts carefully. End your response with either "VERDICT: APPROVED" or "VERDICT: CHANGES_REQUESTED" followed by concrete actionable feedback.`,
|
|
161
|
+
].join('\n\n');
|
|
162
|
+
|
|
163
|
+
const reviewTask = await this.broker.delegateTask({
|
|
164
|
+
to: reviewer.handle,
|
|
165
|
+
from: author.handle,
|
|
166
|
+
title: `Cross-Review Round ${round}`,
|
|
167
|
+
prompt: reviewPrompt,
|
|
168
|
+
notify_on_finish: false,
|
|
169
|
+
workflow_id: workflowId,
|
|
170
|
+
step_key: `review_round_${round}`,
|
|
171
|
+
cwd,
|
|
172
|
+
wait: true,
|
|
173
|
+
timeout_ms: timeoutMs,
|
|
174
|
+
});
|
|
175
|
+
taskIds.push(reviewTask.request_id);
|
|
176
|
+
if (reviewTask.status !== 'completed') {
|
|
177
|
+
throw new Error(reviewTask.error || `Reviewer round ${round} failed (${reviewTask.status})`);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const verdictInfo = parseReviewVerdict(reviewTask.result_summary);
|
|
181
|
+
rounds.push({
|
|
182
|
+
round,
|
|
183
|
+
review_task_id: reviewTask.request_id,
|
|
184
|
+
verdict: verdictInfo.verdict,
|
|
185
|
+
feedback: reviewTask.result_summary || '',
|
|
186
|
+
revision_task_id: null,
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
if (verdictInfo.approved) {
|
|
190
|
+
if (requireArtifacts && latestArtifacts && !latestArtifacts.verified) {
|
|
191
|
+
throw new Error(
|
|
192
|
+
`Approved by reviewer, but required artifacts are missing: ${latestArtifacts.missing.join(', ')}`,
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
approved = true;
|
|
196
|
+
break;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
if (round < maxRounds) {
|
|
200
|
+
this.broker.db.updateWorkflow(workflowId, {
|
|
201
|
+
state: { phase: `author_revise_${round}`, author: author.handle, reviewer: reviewer.handle, round },
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
const revisePrompt = [
|
|
205
|
+
`Reviewer (${reviewer.handle}) requested changes in round ${round}/${maxRounds}.`,
|
|
206
|
+
`Reviewer Feedback:\n${reviewTask.result_summary || ''}`,
|
|
207
|
+
`Please address all feedback and update any expected artifacts.`,
|
|
208
|
+
].join('\n\n');
|
|
209
|
+
|
|
210
|
+
const reviseTask = await this.broker.delegateTask({
|
|
211
|
+
to: author.handle,
|
|
212
|
+
from: reviewer.handle,
|
|
213
|
+
title: `Cross-Review Revision ${round}`,
|
|
214
|
+
prompt: revisePrompt,
|
|
215
|
+
expected_artifacts: expectedArtifacts,
|
|
216
|
+
acceptance_criteria: acceptanceCriteria,
|
|
217
|
+
require_artifacts: false,
|
|
218
|
+
notify_on_finish: false,
|
|
219
|
+
workflow_id: workflowId,
|
|
220
|
+
step_key: `author_revise_${round}`,
|
|
221
|
+
cwd,
|
|
222
|
+
wait: true,
|
|
223
|
+
timeout_ms: timeoutMs,
|
|
224
|
+
});
|
|
225
|
+
taskIds.push(reviseTask.request_id);
|
|
226
|
+
if (reviseTask.status !== 'completed') {
|
|
227
|
+
throw new Error(reviseTask.error || `Author revision ${round} failed (${reviseTask.status})`);
|
|
228
|
+
}
|
|
229
|
+
rounds[rounds.length - 1].revision_task_id = reviseTask.request_id;
|
|
230
|
+
latestOutput = reviseTask.result_summary || '';
|
|
231
|
+
latestArtifacts = reviseTask.artifact_report || null;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const closedPeers = await this._cleanupSpawnedPeers(spawnedPeers, { autoClose });
|
|
236
|
+
const result = {
|
|
237
|
+
template: 'cross-review',
|
|
238
|
+
author: author.handle,
|
|
239
|
+
reviewer: reviewer.handle,
|
|
240
|
+
approved,
|
|
241
|
+
status_detail: approved ? 'approved' : 'max_rounds_reached',
|
|
242
|
+
rounds_used: roundsUsed,
|
|
243
|
+
max_rounds: maxRounds,
|
|
244
|
+
final_output: latestOutput,
|
|
245
|
+
artifact_report: latestArtifacts,
|
|
246
|
+
rounds,
|
|
247
|
+
task_ids: taskIds,
|
|
248
|
+
closed_ephemeral_peers: closedPeers,
|
|
249
|
+
};
|
|
250
|
+
const updated = this.broker.db.updateWorkflow(workflowId, {
|
|
251
|
+
status: 'completed',
|
|
252
|
+
state: { phase: 'done', approved, rounds_used: roundsUsed },
|
|
253
|
+
result,
|
|
254
|
+
});
|
|
255
|
+
await this._notifyCoordinator(
|
|
256
|
+
updated,
|
|
257
|
+
`Cross-review finished (approved=${approved}, rounds=${roundsUsed}/${maxRounds}).\nFinal Output:\n${latestOutput}`,
|
|
258
|
+
);
|
|
259
|
+
return updated;
|
|
260
|
+
} catch (err) {
|
|
261
|
+
await this._cleanupSpawnedPeers(spawnedPeers, { autoClose });
|
|
262
|
+
const failed = this.broker.db.updateWorkflow(workflowId, {
|
|
263
|
+
status: 'failed',
|
|
264
|
+
error: err.message,
|
|
265
|
+
result: { template: 'cross-review', rounds, task_ids: taskIds },
|
|
266
|
+
});
|
|
267
|
+
await this._notifyCoordinator(failed, `Cross-review failed: ${err.message}`);
|
|
268
|
+
return failed;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
async _executeFanoutJoin(workflowId, params) {
|
|
273
|
+
const cwd = params.cwd || process.cwd();
|
|
274
|
+
const rawTasks = Array.isArray(params.tasks)
|
|
275
|
+
? params.tasks
|
|
276
|
+
: Array.isArray(params.workers)
|
|
277
|
+
? params.workers
|
|
278
|
+
: [];
|
|
279
|
+
const tasksSpec = rawTasks.map((item) => ({
|
|
280
|
+
...item,
|
|
281
|
+
to: item?.to || item?.target || undefined,
|
|
282
|
+
}));
|
|
283
|
+
const autoClose = params.auto_close !== false;
|
|
284
|
+
const timeoutMs = Number(params.timeout_ms ?? 180_000);
|
|
285
|
+
const spawnedPeers = [];
|
|
286
|
+
const taskIds = [];
|
|
287
|
+
|
|
288
|
+
try {
|
|
289
|
+
if (tasksSpec.length === 0) {
|
|
290
|
+
throw new RpcError('INVALID_PARAMS', 'fanout-join requires a non-empty "tasks" (or "workers") array');
|
|
291
|
+
}
|
|
292
|
+
const prepared = [];
|
|
293
|
+
for (let i = 0; i < tasksSpec.length; i++) {
|
|
294
|
+
const item = tasksSpec[i];
|
|
295
|
+
const label = item.label || `worker-${i + 1}`;
|
|
296
|
+
const participant = await this._resolveOrSpawnParticipant(
|
|
297
|
+
item.to ? item.to : item.spawn ? { spawn: item.spawn } : null,
|
|
298
|
+
{ workflowId, defaultRole: label, cwd: item.cwd || cwd, spawnedPeers },
|
|
299
|
+
);
|
|
300
|
+
prepared.push({ index: i, label, participant, item });
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
this.broker.db.updateWorkflow(workflowId, {
|
|
304
|
+
state: {
|
|
305
|
+
phase: 'fanout',
|
|
306
|
+
total: prepared.length,
|
|
307
|
+
workers: prepared.map((p) => ({ label: p.label, handle: p.participant.handle })),
|
|
308
|
+
},
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
const settled = await Promise.allSettled(
|
|
312
|
+
prepared.map(async ({ label, participant, item }) => {
|
|
313
|
+
const t = await this.broker.delegateTask({
|
|
314
|
+
to: participant.handle,
|
|
315
|
+
from: params.coordinator || { runtime: 'cli', name: `workflow:${workflowId}` },
|
|
316
|
+
title: item.title || `${params.name || 'Fan-out'}: ${label}`,
|
|
317
|
+
prompt: item.prompt,
|
|
318
|
+
expected_artifacts: item.expected_artifacts || [],
|
|
319
|
+
acceptance_criteria: item.acceptance_criteria || [],
|
|
320
|
+
require_artifacts: Boolean(item.require_artifacts),
|
|
321
|
+
notify_on_finish: false,
|
|
322
|
+
workflow_id: workflowId,
|
|
323
|
+
step_key: label,
|
|
324
|
+
cwd: item.cwd || cwd,
|
|
325
|
+
wait: true,
|
|
326
|
+
timeout_ms: item.timeout_ms || timeoutMs,
|
|
327
|
+
});
|
|
328
|
+
taskIds.push(t.request_id);
|
|
329
|
+
return { label, handle: participant.handle, task: t };
|
|
330
|
+
}),
|
|
331
|
+
);
|
|
332
|
+
|
|
333
|
+
const branches = [];
|
|
334
|
+
let succeededCount = 0;
|
|
335
|
+
let failedCount = 0;
|
|
336
|
+
for (let i = 0; i < settled.length; i++) {
|
|
337
|
+
const s = settled[i];
|
|
338
|
+
const meta = prepared[i];
|
|
339
|
+
if (s.status === 'fulfilled') {
|
|
340
|
+
const { label, handle, task } = s.value;
|
|
341
|
+
const ok = task.status === 'completed';
|
|
342
|
+
if (ok) succeededCount++;
|
|
343
|
+
else failedCount++;
|
|
344
|
+
branches.push({
|
|
345
|
+
label,
|
|
346
|
+
handle,
|
|
347
|
+
request_id: task.request_id,
|
|
348
|
+
status: task.status,
|
|
349
|
+
summary: task.result_summary || '',
|
|
350
|
+
artifact_report: task.artifact_report || null,
|
|
351
|
+
error: task.error || null,
|
|
352
|
+
});
|
|
353
|
+
} else {
|
|
354
|
+
failedCount++;
|
|
355
|
+
branches.push({
|
|
356
|
+
label: meta.label,
|
|
357
|
+
handle: meta.participant.handle,
|
|
358
|
+
request_id: null,
|
|
359
|
+
status: 'failed',
|
|
360
|
+
summary: '',
|
|
361
|
+
artifact_report: null,
|
|
362
|
+
error: s.reason?.message || String(s.reason),
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
let joinResult = null;
|
|
368
|
+
const joinSpec = params.join_to || params.join_spawn ? (params.join_to || { spawn: params.join_spawn }) : null;
|
|
369
|
+
if (joinSpec) {
|
|
370
|
+
const joinPeer = await this._resolveOrSpawnParticipant(joinSpec, {
|
|
371
|
+
workflowId,
|
|
372
|
+
defaultRole: 'join',
|
|
373
|
+
cwd,
|
|
374
|
+
spawnedPeers,
|
|
375
|
+
});
|
|
376
|
+
this.broker.db.updateWorkflow(workflowId, {
|
|
377
|
+
state: { phase: 'join', join_peer: joinPeer.handle, succeeded: succeededCount, failed: failedCount },
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
const branchDigest = branches
|
|
381
|
+
.map(
|
|
382
|
+
(b) =>
|
|
383
|
+
`### ${b.label} (${b.handle}) [${b.status}]\nSummary: ${b.summary || b.error || '(empty)'}\nArtifacts:\n${formatArtifactLines(b.artifact_report)}`,
|
|
384
|
+
)
|
|
385
|
+
.join('\n\n');
|
|
386
|
+
const joinPrompt = [
|
|
387
|
+
params.join_prompt || 'Synthesize the parallel worker outputs below into a concise unified report.',
|
|
388
|
+
branchDigest,
|
|
389
|
+
].join('\n\n');
|
|
390
|
+
|
|
391
|
+
const joinTask = await this.broker.delegateTask({
|
|
392
|
+
to: joinPeer.handle,
|
|
393
|
+
from: params.coordinator || { runtime: 'cli', name: `workflow:${workflowId}` },
|
|
394
|
+
title: `${params.name || 'Fan-out'} Join Synthesis`,
|
|
395
|
+
prompt: joinPrompt,
|
|
396
|
+
expected_artifacts: params.join_expected_artifacts || [],
|
|
397
|
+
require_artifacts: Boolean(params.join_require_artifacts),
|
|
398
|
+
notify_on_finish: false,
|
|
399
|
+
workflow_id: workflowId,
|
|
400
|
+
step_key: 'join',
|
|
401
|
+
cwd,
|
|
402
|
+
wait: true,
|
|
403
|
+
timeout_ms: timeoutMs,
|
|
404
|
+
});
|
|
405
|
+
taskIds.push(joinTask.request_id);
|
|
406
|
+
joinResult = {
|
|
407
|
+
handle: joinPeer.handle,
|
|
408
|
+
request_id: joinTask.request_id,
|
|
409
|
+
status: joinTask.status,
|
|
410
|
+
summary: joinTask.result_summary || '',
|
|
411
|
+
artifact_report: joinTask.artifact_report || null,
|
|
412
|
+
error: joinTask.error || null,
|
|
413
|
+
};
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
const closedPeers = await this._cleanupSpawnedPeers(spawnedPeers, { autoClose });
|
|
417
|
+
const overallStatus =
|
|
418
|
+
failedCount > 0 && succeededCount === 0
|
|
419
|
+
? 'failed'
|
|
420
|
+
: joinResult && joinResult.status !== 'completed'
|
|
421
|
+
? 'failed'
|
|
422
|
+
: 'completed';
|
|
423
|
+
|
|
424
|
+
const result = {
|
|
425
|
+
template: 'fanout-join',
|
|
426
|
+
total: branches.length,
|
|
427
|
+
succeeded: succeededCount,
|
|
428
|
+
failed: failedCount,
|
|
429
|
+
branches,
|
|
430
|
+
join: joinResult,
|
|
431
|
+
synthesized_output: joinResult?.summary || null,
|
|
432
|
+
task_ids: taskIds,
|
|
433
|
+
closed_ephemeral_peers: closedPeers,
|
|
434
|
+
};
|
|
435
|
+
|
|
436
|
+
const updated = this.broker.db.updateWorkflow(workflowId, {
|
|
437
|
+
status: overallStatus,
|
|
438
|
+
state: { phase: 'done', succeeded: succeededCount, failed: failedCount },
|
|
439
|
+
result,
|
|
440
|
+
error: overallStatus === 'failed' ? joinResult?.error || 'All fan-out branches failed' : null,
|
|
441
|
+
});
|
|
442
|
+
|
|
443
|
+
const summaryLine = joinResult
|
|
444
|
+
? `Fan-out/Join finished (${succeededCount}/${branches.length} succeeded).\nSynthesis (${joinResult.handle}):\n${joinResult.summary}`
|
|
445
|
+
: `Fan-out finished (${succeededCount}/${branches.length} succeeded).`;
|
|
446
|
+
await this._notifyCoordinator(updated, summaryLine);
|
|
447
|
+
return updated;
|
|
448
|
+
} catch (err) {
|
|
449
|
+
await this._cleanupSpawnedPeers(spawnedPeers, { autoClose });
|
|
450
|
+
const failed = this.broker.db.updateWorkflow(workflowId, {
|
|
451
|
+
status: 'failed',
|
|
452
|
+
error: err.message,
|
|
453
|
+
result: { template: 'fanout-join', task_ids: taskIds },
|
|
454
|
+
});
|
|
455
|
+
await this._notifyCoordinator(failed, `Fan-out/Join failed: ${err.message}`);
|
|
456
|
+
return failed;
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
async runWorkflow(params = {}) {
|
|
461
|
+
const template = String(params.template || '').toLowerCase();
|
|
462
|
+
if (!WORKFLOW_TEMPLATES.has(template)) {
|
|
463
|
+
throw new RpcError(
|
|
464
|
+
'INVALID_PARAMS',
|
|
465
|
+
`Unsupported workflow template "${params.template}". Use "cross-review" or "fanout-join".`,
|
|
466
|
+
);
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
const workflowId = params.workflow_id || makeId('wf');
|
|
470
|
+
const wf = this.broker.db.insertWorkflow({
|
|
471
|
+
workflow_id: workflowId,
|
|
472
|
+
template,
|
|
473
|
+
name: params.name || template,
|
|
474
|
+
status: 'running',
|
|
475
|
+
coordinator_handle: params.coordinator || null,
|
|
476
|
+
spec: params,
|
|
477
|
+
state: { phase: 'starting' },
|
|
478
|
+
});
|
|
479
|
+
|
|
480
|
+
const execute = async () => {
|
|
481
|
+
const done =
|
|
482
|
+
template === 'cross-review'
|
|
483
|
+
? await this._executeCrossReview(workflowId, params)
|
|
484
|
+
: await this._executeFanoutJoin(workflowId, params);
|
|
485
|
+
this.broker._resolveWorkflowWaiters(workflowId, done);
|
|
486
|
+
return done;
|
|
487
|
+
};
|
|
488
|
+
|
|
489
|
+
if (params.async) {
|
|
490
|
+
execute().catch(() => {});
|
|
491
|
+
return wf;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
return execute();
|
|
495
|
+
}
|
|
496
|
+
}
|
package/src/project.mjs
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import os from 'node:os';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
|
|
5
|
+
const PROJECT_MARKERS = ['.git', '.peer-bridge.json'];
|
|
6
|
+
|
|
7
|
+
export function normalizeProjectPath(dirPath) {
|
|
8
|
+
if (!dirPath || typeof dirPath !== 'string') return null;
|
|
9
|
+
const resolved = path.resolve(dirPath);
|
|
10
|
+
try {
|
|
11
|
+
return fs.realpathSync.native(resolved);
|
|
12
|
+
} catch {
|
|
13
|
+
return resolved;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function findProjectRoot(startDir) {
|
|
18
|
+
const normalized = normalizeProjectPath(startDir);
|
|
19
|
+
if (!normalized) return null;
|
|
20
|
+
|
|
21
|
+
const home = normalizeProjectPath(os.homedir());
|
|
22
|
+
const tmp = normalizeProjectPath(os.tmpdir());
|
|
23
|
+
const fsRoot = path.parse(normalized).root;
|
|
24
|
+
|
|
25
|
+
let cur = normalized;
|
|
26
|
+
while (cur && cur !== fsRoot && cur !== home && cur !== tmp) {
|
|
27
|
+
for (const marker of PROJECT_MARKERS) {
|
|
28
|
+
try {
|
|
29
|
+
if (fs.existsSync(path.join(cur, marker))) {
|
|
30
|
+
return cur;
|
|
31
|
+
}
|
|
32
|
+
} catch {
|
|
33
|
+
// ignore permission errors
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
const parent = path.dirname(cur);
|
|
37
|
+
if (!parent || parent === cur) break;
|
|
38
|
+
cur = parent;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return normalized;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function detectDefaultProjectCwd(startDir = process.cwd()) {
|
|
45
|
+
const normalized = normalizeProjectPath(startDir);
|
|
46
|
+
if (!normalized) return null;
|
|
47
|
+
|
|
48
|
+
const home = normalizeProjectPath(os.homedir());
|
|
49
|
+
const tmp = normalizeProjectPath(os.tmpdir());
|
|
50
|
+
const fsRoot = path.parse(normalized).root;
|
|
51
|
+
|
|
52
|
+
if (normalized === home || normalized === tmp || normalized === fsRoot) {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
return findProjectRoot(normalized);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function isSameOrSubPath(childDir, parentDir) {
|
|
59
|
+
const child = normalizeProjectPath(childDir);
|
|
60
|
+
const parent = normalizeProjectPath(parentDir);
|
|
61
|
+
if (!child || !parent) return false;
|
|
62
|
+
if (child === parent) return true;
|
|
63
|
+
const prefix = parent.endsWith(path.sep) ? parent : `${parent}${path.sep}`;
|
|
64
|
+
return child.startsWith(prefix);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function isSameProjectScope(cwdA, cwdB) {
|
|
68
|
+
if (!cwdA || !cwdB) return false;
|
|
69
|
+
const rootA = findProjectRoot(cwdA);
|
|
70
|
+
const rootB = findProjectRoot(cwdB);
|
|
71
|
+
if (!rootA || !rootB) return false;
|
|
72
|
+
return rootA === rootB || isSameOrSubPath(cwdA, rootB) || isSameOrSubPath(cwdB, rootA);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function getProjectLabel(cwd) {
|
|
76
|
+
if (!cwd) return 'all';
|
|
77
|
+
const root = findProjectRoot(cwd) || normalizeProjectPath(cwd);
|
|
78
|
+
if (!root) return 'all';
|
|
79
|
+
const base = path.basename(root);
|
|
80
|
+
return base || root;
|
|
81
|
+
}
|