@fro.bot/space-bus 0.8.0 → 0.9.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/README.md +1 -0
- package/dist/attach.js +17 -0
- package/dist/cli.js +50 -0
- package/dist/config.js +17 -0
- package/dist/contract.d.ts +25 -0
- package/dist/contract.js +19 -0
- package/dist/core.d.ts +43 -2
- package/dist/core.js +177 -8
- package/dist/format.d.ts +2 -1
- package/dist/format.js +20 -0
- package/dist/index.js +266 -15
- package/dist/mcp.js +287 -15
- package/dist/server.d.ts +36 -0
- package/dist/server.js +51 -0
- package/dist/tools/bus_wait.d.ts +4 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -66,6 +66,7 @@ Set `SPACE_BUS_CONFIG` to override roster discovery — it must be an absolute p
|
|
|
66
66
|
- `bus_task` — Dispatch a prompt to an agent in the given space-bus manifest project, or steer an existing session by passing `sessionId` (answers its pending question, else sends a follow-up prompt). Returns immediately; does not wait for completion. Alongside the formatted text, results carry structured `{sessionId, project, mode}` metadata — plugin `ToolResult.metadata` on the tool surface, MCP `structuredContent` on the stdio surface — for callers that want the dispatch outcome without parsing the string.
|
|
67
67
|
- `bus_status` — Report a space-bus session's status plus a summary of its latest todo and diff. Also reports when the session is blocked on an interactive question awaiting a reply.
|
|
68
68
|
- `bus_result` — Return a completed space-bus session's final assistant message and diff. Errors if the session is still running — use `bus_status` to check first.
|
|
69
|
+
- `bus_wait` — Block until any of the given sessions needs attention (completes, blocks on a question, fails, or is not found) or a bounded timeout elapses. Returns each watched session's normalized state and which session(s) woke the wait; on timeout returns the current snapshot, not an error.
|
|
69
70
|
|
|
70
71
|
## Managed server
|
|
71
72
|
|
package/dist/attach.js
CHANGED
|
@@ -7,6 +7,23 @@ var sessionSchema = z.looseObject({
|
|
|
7
7
|
});
|
|
8
8
|
var sessionListSchema = z.array(sessionSchema);
|
|
9
9
|
var sessionStatusMapSchema = z.record(z.string(), z.looseObject({ type: z.string() }));
|
|
10
|
+
var sessionStateSchema = z.enum([
|
|
11
|
+
"running",
|
|
12
|
+
"blocked",
|
|
13
|
+
"complete",
|
|
14
|
+
"failed",
|
|
15
|
+
"not_found"
|
|
16
|
+
]);
|
|
17
|
+
var sessionStateInfoSchema = z.object({
|
|
18
|
+
sessionId: z.string(),
|
|
19
|
+
project: z.string(),
|
|
20
|
+
state: sessionStateSchema,
|
|
21
|
+
resultAvailable: z.boolean(),
|
|
22
|
+
pendingQuestion: z.object({
|
|
23
|
+
preview: z.string(),
|
|
24
|
+
options: z.array(z.string())
|
|
25
|
+
}).optional()
|
|
26
|
+
});
|
|
10
27
|
var todoSchema = z.array(z.looseObject({
|
|
11
28
|
content: z.string(),
|
|
12
29
|
status: z.string(),
|
package/dist/cli.js
CHANGED
|
@@ -11,6 +11,23 @@ var sessionSchema = z.looseObject({
|
|
|
11
11
|
});
|
|
12
12
|
var sessionListSchema = z.array(sessionSchema);
|
|
13
13
|
var sessionStatusMapSchema = z.record(z.string(), z.looseObject({ type: z.string() }));
|
|
14
|
+
var sessionStateSchema = z.enum([
|
|
15
|
+
"running",
|
|
16
|
+
"blocked",
|
|
17
|
+
"complete",
|
|
18
|
+
"failed",
|
|
19
|
+
"not_found"
|
|
20
|
+
]);
|
|
21
|
+
var sessionStateInfoSchema = z.object({
|
|
22
|
+
sessionId: z.string(),
|
|
23
|
+
project: z.string(),
|
|
24
|
+
state: sessionStateSchema,
|
|
25
|
+
resultAvailable: z.boolean(),
|
|
26
|
+
pendingQuestion: z.object({
|
|
27
|
+
preview: z.string(),
|
|
28
|
+
options: z.array(z.string())
|
|
29
|
+
}).optional()
|
|
30
|
+
});
|
|
14
31
|
var todoSchema = z.array(z.looseObject({
|
|
15
32
|
content: z.string(),
|
|
16
33
|
status: z.string(),
|
|
@@ -565,6 +582,16 @@ function signalGroup(pid, sig) {
|
|
|
565
582
|
} catch {}
|
|
566
583
|
return false;
|
|
567
584
|
}
|
|
585
|
+
function signalGroupOnly(pid, sig) {
|
|
586
|
+
if (pid <= 1)
|
|
587
|
+
return false;
|
|
588
|
+
try {
|
|
589
|
+
process.kill(-pid, sig);
|
|
590
|
+
return true;
|
|
591
|
+
} catch {
|
|
592
|
+
return false;
|
|
593
|
+
}
|
|
594
|
+
}
|
|
568
595
|
function killIdentifiedProcess(pid, identity) {
|
|
569
596
|
try {
|
|
570
597
|
if (identity !== null && identity !== undefined && identity !== "") {
|
|
@@ -821,6 +848,7 @@ async function stopServer(rosterPath) {
|
|
|
821
848
|
if (!discovery)
|
|
822
849
|
return { stopped: false };
|
|
823
850
|
if (!verifyIdentity(discovery.pid, discovery.identity)) {
|
|
851
|
+
await reapSurvivingGroup(discovery.pid);
|
|
824
852
|
removeDiscovery(rosterPath);
|
|
825
853
|
return { stopped: false };
|
|
826
854
|
}
|
|
@@ -855,6 +883,27 @@ async function stopServer(rosterPath) {
|
|
|
855
883
|
}
|
|
856
884
|
return { stopped: false };
|
|
857
885
|
}
|
|
886
|
+
async function reapSurvivingGroup(pid) {
|
|
887
|
+
try {
|
|
888
|
+
if (isAlive(pid))
|
|
889
|
+
return;
|
|
890
|
+
let groupAlive;
|
|
891
|
+
try {
|
|
892
|
+
process.kill(-pid, 0);
|
|
893
|
+
groupAlive = true;
|
|
894
|
+
} catch {
|
|
895
|
+
groupAlive = false;
|
|
896
|
+
}
|
|
897
|
+
if (!groupAlive)
|
|
898
|
+
return;
|
|
899
|
+
if (!signalGroupOnly(pid, "SIGTERM"))
|
|
900
|
+
return;
|
|
901
|
+
if (await waitForGroupDeath(pid, STOP_GRACE_MS))
|
|
902
|
+
return;
|
|
903
|
+
signalGroupOnly(pid, "SIGKILL");
|
|
904
|
+
await waitForGroupDeath(pid, STOP_GRACE_MS);
|
|
905
|
+
} catch {}
|
|
906
|
+
}
|
|
858
907
|
async function stopForHung(tick) {
|
|
859
908
|
try {
|
|
860
909
|
await tick.stop(tick.rosterPath);
|
|
@@ -864,6 +913,7 @@ async function stopForHung(tick) {
|
|
|
864
913
|
async function superviseTick(tick, state) {
|
|
865
914
|
const st = tick.status(tick.rosterPath);
|
|
866
915
|
if (!st.running) {
|
|
916
|
+
await reapSurvivingGroup(tick.handle.pid);
|
|
867
917
|
return { kind: "outcome", outcome: { reason: "died" } };
|
|
868
918
|
}
|
|
869
919
|
const probeOutcome = await tick.probe(tick.handle.baseUrl, tick.password);
|
package/dist/config.js
CHANGED
|
@@ -10,6 +10,23 @@ var sessionSchema = z.looseObject({
|
|
|
10
10
|
});
|
|
11
11
|
var sessionListSchema = z.array(sessionSchema);
|
|
12
12
|
var sessionStatusMapSchema = z.record(z.string(), z.looseObject({ type: z.string() }));
|
|
13
|
+
var sessionStateSchema = z.enum([
|
|
14
|
+
"running",
|
|
15
|
+
"blocked",
|
|
16
|
+
"complete",
|
|
17
|
+
"failed",
|
|
18
|
+
"not_found"
|
|
19
|
+
]);
|
|
20
|
+
var sessionStateInfoSchema = z.object({
|
|
21
|
+
sessionId: z.string(),
|
|
22
|
+
project: z.string(),
|
|
23
|
+
state: sessionStateSchema,
|
|
24
|
+
resultAvailable: z.boolean(),
|
|
25
|
+
pendingQuestion: z.object({
|
|
26
|
+
preview: z.string(),
|
|
27
|
+
options: z.array(z.string())
|
|
28
|
+
}).optional()
|
|
29
|
+
});
|
|
13
30
|
var todoSchema = z.array(z.looseObject({
|
|
14
31
|
content: z.string(),
|
|
15
32
|
status: z.string(),
|
package/dist/contract.d.ts
CHANGED
|
@@ -21,6 +21,31 @@ export declare const sessionStatusMapSchema: z.ZodRecord<z.ZodString, z.ZodObjec
|
|
|
21
21
|
type: z.ZodString;
|
|
22
22
|
}, z.core.$loose>>;
|
|
23
23
|
export type SessionStatusMapSchema = z.infer<typeof sessionStatusMapSchema>;
|
|
24
|
+
export declare const sessionStateSchema: z.ZodEnum<{
|
|
25
|
+
running: "running";
|
|
26
|
+
blocked: "blocked";
|
|
27
|
+
complete: "complete";
|
|
28
|
+
failed: "failed";
|
|
29
|
+
not_found: "not_found";
|
|
30
|
+
}>;
|
|
31
|
+
export type SessionState = z.infer<typeof sessionStateSchema>;
|
|
32
|
+
export declare const sessionStateInfoSchema: z.ZodObject<{
|
|
33
|
+
sessionId: z.ZodString;
|
|
34
|
+
project: z.ZodString;
|
|
35
|
+
state: z.ZodEnum<{
|
|
36
|
+
running: "running";
|
|
37
|
+
blocked: "blocked";
|
|
38
|
+
complete: "complete";
|
|
39
|
+
failed: "failed";
|
|
40
|
+
not_found: "not_found";
|
|
41
|
+
}>;
|
|
42
|
+
resultAvailable: z.ZodBoolean;
|
|
43
|
+
pendingQuestion: z.ZodOptional<z.ZodObject<{
|
|
44
|
+
preview: z.ZodString;
|
|
45
|
+
options: z.ZodArray<z.ZodString>;
|
|
46
|
+
}, z.core.$strip>>;
|
|
47
|
+
}, z.core.$strip>;
|
|
48
|
+
export type SessionStateInfo = z.infer<typeof sessionStateInfoSchema>;
|
|
24
49
|
export declare const todoSchema: z.ZodDefault<z.ZodArray<z.ZodObject<{
|
|
25
50
|
content: z.ZodString;
|
|
26
51
|
status: z.ZodString;
|
package/dist/contract.js
CHANGED
|
@@ -10,6 +10,23 @@ var sessionSchema = z.looseObject({
|
|
|
10
10
|
});
|
|
11
11
|
var sessionListSchema = z.array(sessionSchema);
|
|
12
12
|
var sessionStatusMapSchema = z.record(z.string(), z.looseObject({ type: z.string() }));
|
|
13
|
+
var sessionStateSchema = z.enum([
|
|
14
|
+
"running",
|
|
15
|
+
"blocked",
|
|
16
|
+
"complete",
|
|
17
|
+
"failed",
|
|
18
|
+
"not_found"
|
|
19
|
+
]);
|
|
20
|
+
var sessionStateInfoSchema = z.object({
|
|
21
|
+
sessionId: z.string(),
|
|
22
|
+
project: z.string(),
|
|
23
|
+
state: sessionStateSchema,
|
|
24
|
+
resultAvailable: z.boolean(),
|
|
25
|
+
pendingQuestion: z.object({
|
|
26
|
+
preview: z.string(),
|
|
27
|
+
options: z.array(z.string())
|
|
28
|
+
}).optional()
|
|
29
|
+
});
|
|
13
30
|
var todoSchema = z.array(z.looseObject({
|
|
14
31
|
content: z.string(),
|
|
15
32
|
status: z.string(),
|
|
@@ -121,6 +138,8 @@ export {
|
|
|
121
138
|
todoSchema,
|
|
122
139
|
sessionSummarySchema,
|
|
123
140
|
sessionStatusMapSchema,
|
|
141
|
+
sessionStateSchema,
|
|
142
|
+
sessionStateInfoSchema,
|
|
124
143
|
sessionSchema,
|
|
125
144
|
sessionListSchema,
|
|
126
145
|
rosterSchema,
|
package/dist/core.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { type BusContext } from "./contract";
|
|
1
|
+
import { type BusContext, type SessionState, type SessionStateInfo } from "./contract";
|
|
2
|
+
export type { SessionState, SessionStateInfo } from "./contract";
|
|
2
3
|
type Ok<T> = {
|
|
3
4
|
ok: true;
|
|
4
5
|
} & T;
|
|
@@ -71,7 +72,34 @@ export type SessionStatusResult = {
|
|
|
71
72
|
preview: string;
|
|
72
73
|
options: string[];
|
|
73
74
|
};
|
|
75
|
+
state: SessionState;
|
|
76
|
+
resultAvailable: boolean;
|
|
74
77
|
};
|
|
78
|
+
/**
|
|
79
|
+
* Maps raw session-status inputs to the normalized lifecycle enum. Called
|
|
80
|
+
* identically by status(), snapshot(), and bus_wait so the three emitters
|
|
81
|
+
* cannot diverge (R2).
|
|
82
|
+
*
|
|
83
|
+
* Precedence (highest first) — each check short-circuits the ones below it:
|
|
84
|
+
* 1. !resolved -> "not_found" (session id never resolved to a
|
|
85
|
+
* directory/session; nothing else can be derived)
|
|
86
|
+
* 2. failed -> "failed" (server-reported errored/aborted)
|
|
87
|
+
* 3. pendingQuestion -> "blocked" (wins over both "running" and
|
|
88
|
+
* "complete" — a busy session with an open question
|
|
89
|
+
* is blocked, and a not-busy session with a still-
|
|
90
|
+
* open question is blocked, not complete)
|
|
91
|
+
* 4. busy -> "running"
|
|
92
|
+
* 5. else -> "complete"
|
|
93
|
+
*/
|
|
94
|
+
export declare function deriveSessionState(input: {
|
|
95
|
+
busy: boolean;
|
|
96
|
+
pendingQuestion?: {
|
|
97
|
+
preview: string;
|
|
98
|
+
options: string[];
|
|
99
|
+
} | undefined;
|
|
100
|
+
resolved: boolean;
|
|
101
|
+
failed?: boolean;
|
|
102
|
+
}): SessionState;
|
|
75
103
|
export declare function status(sessionId: string, opts: CoreOpts): Promise<Result<SessionStatusResult>>;
|
|
76
104
|
export type SessionResultResult = {
|
|
77
105
|
sessionId: string;
|
|
@@ -99,6 +127,11 @@ export type SnapshotProject = {
|
|
|
99
127
|
preview: string;
|
|
100
128
|
options: string[];
|
|
101
129
|
}[];
|
|
130
|
+
sessions?: {
|
|
131
|
+
sessionId: string;
|
|
132
|
+
state: SessionState;
|
|
133
|
+
resultAvailable: boolean;
|
|
134
|
+
}[];
|
|
102
135
|
error?: string;
|
|
103
136
|
};
|
|
104
137
|
export declare function snapshot(opts: CoreOpts & {
|
|
@@ -106,4 +139,12 @@ export declare function snapshot(opts: CoreOpts & {
|
|
|
106
139
|
}): Promise<Result<{
|
|
107
140
|
projects: SnapshotProject[];
|
|
108
141
|
}>>;
|
|
109
|
-
export {
|
|
142
|
+
export type WaitResult = {
|
|
143
|
+
sessions: SessionStateInfo[];
|
|
144
|
+
waker: string[];
|
|
145
|
+
timedOut: boolean;
|
|
146
|
+
};
|
|
147
|
+
export declare function wait(sessionIds: string[], opts: CoreOpts & {
|
|
148
|
+
timeoutMs?: number;
|
|
149
|
+
pollIntervalMs?: number;
|
|
150
|
+
}): Promise<Result<WaitResult>>;
|
package/dist/core.js
CHANGED
|
@@ -10,6 +10,23 @@ var sessionSchema = z.looseObject({
|
|
|
10
10
|
});
|
|
11
11
|
var sessionListSchema = z.array(sessionSchema);
|
|
12
12
|
var sessionStatusMapSchema = z.record(z.string(), z.looseObject({ type: z.string() }));
|
|
13
|
+
var sessionStateSchema = z.enum([
|
|
14
|
+
"running",
|
|
15
|
+
"blocked",
|
|
16
|
+
"complete",
|
|
17
|
+
"failed",
|
|
18
|
+
"not_found"
|
|
19
|
+
]);
|
|
20
|
+
var sessionStateInfoSchema = z.object({
|
|
21
|
+
sessionId: z.string(),
|
|
22
|
+
project: z.string(),
|
|
23
|
+
state: sessionStateSchema,
|
|
24
|
+
resultAvailable: z.boolean(),
|
|
25
|
+
pendingQuestion: z.object({
|
|
26
|
+
preview: z.string(),
|
|
27
|
+
options: z.array(z.string())
|
|
28
|
+
}).optional()
|
|
29
|
+
});
|
|
13
30
|
var todoSchema = z.array(z.looseObject({
|
|
14
31
|
content: z.string(),
|
|
15
32
|
status: z.string(),
|
|
@@ -252,6 +269,9 @@ function validateContext(context) {
|
|
|
252
269
|
credentials: credentials ?? {}
|
|
253
270
|
};
|
|
254
271
|
}
|
|
272
|
+
function isStatusBusy(entry) {
|
|
273
|
+
return entry?.type === "busy" || entry?.type === "retry" || false;
|
|
274
|
+
}
|
|
255
275
|
function resolveProjectOrErr(projects, name) {
|
|
256
276
|
const project = findProject(projects, name);
|
|
257
277
|
if (!project) {
|
|
@@ -293,7 +313,7 @@ async function roster(opts) {
|
|
|
293
313
|
}
|
|
294
314
|
const statusMap = sessionStatusMapSchema.parse(JSON.parse(statusRes.bodyText));
|
|
295
315
|
const sessions = sessionListSchema.parse(JSON.parse(listRes.bodyText));
|
|
296
|
-
const busyCount = Object.values(statusMap).filter((s) => s
|
|
316
|
+
const busyCount = Object.values(statusMap).filter((s) => isStatusBusy(s)).length;
|
|
297
317
|
const capped = sessions.length > 100;
|
|
298
318
|
return {
|
|
299
319
|
name: p.name,
|
|
@@ -420,6 +440,17 @@ async function findSessionDirectory(baseUrl, credentials, projects, sessionId) {
|
|
|
420
440
|
}
|
|
421
441
|
return err(`space-bus: no manifest project has a session with id ${sessionId}`);
|
|
422
442
|
}
|
|
443
|
+
function deriveSessionState(input) {
|
|
444
|
+
if (!input.resolved)
|
|
445
|
+
return "not_found";
|
|
446
|
+
if (input.failed)
|
|
447
|
+
return "failed";
|
|
448
|
+
if (input.pendingQuestion)
|
|
449
|
+
return "blocked";
|
|
450
|
+
if (input.busy)
|
|
451
|
+
return "running";
|
|
452
|
+
return "complete";
|
|
453
|
+
}
|
|
423
454
|
function formatQuestionEntry(entry) {
|
|
424
455
|
const firstQuestion = entry.questions?.[0];
|
|
425
456
|
const text = firstQuestion?.question ?? "";
|
|
@@ -472,10 +503,11 @@ async function status(sessionId, opts) {
|
|
|
472
503
|
return err(`space-bus: unexpected response shape for session ${sessionId}: ${e.message}`);
|
|
473
504
|
}
|
|
474
505
|
const entry = statusMap[sessionId];
|
|
475
|
-
const busy = entry
|
|
506
|
+
const busy = isStatusBusy(entry);
|
|
476
507
|
const { diff, diffSource } = diffResult;
|
|
477
508
|
const additions = diff.reduce((sum, d) => sum + d.additions, 0);
|
|
478
509
|
const deletions = diff.reduce((sum, d) => sum + d.deletions, 0);
|
|
510
|
+
const state = deriveSessionState({ busy, pendingQuestion, resolved: true });
|
|
479
511
|
return {
|
|
480
512
|
ok: true,
|
|
481
513
|
sessionId,
|
|
@@ -485,7 +517,9 @@ async function status(sessionId, opts) {
|
|
|
485
517
|
todos,
|
|
486
518
|
diff: { files: diff.length, additions, deletions },
|
|
487
519
|
diffSource,
|
|
488
|
-
pendingQuestion
|
|
520
|
+
pendingQuestion,
|
|
521
|
+
state,
|
|
522
|
+
resultAvailable: state === "complete"
|
|
489
523
|
};
|
|
490
524
|
}
|
|
491
525
|
async function steerSession(baseUrl, credentials, sessionId, message, directory, project) {
|
|
@@ -532,7 +566,7 @@ async function result(sessionId, opts) {
|
|
|
532
566
|
try {
|
|
533
567
|
const statusMap = sessionStatusMapSchema.parse(JSON.parse(statusMapRes.bodyText));
|
|
534
568
|
const entry = statusMap[sessionId];
|
|
535
|
-
if (
|
|
569
|
+
if (isStatusBusy(entry)) {
|
|
536
570
|
return err(`space-bus: session ${sessionId} is still running, use bus_status`);
|
|
537
571
|
}
|
|
538
572
|
} catch {}
|
|
@@ -575,17 +609,35 @@ async function fetchSnapshotProject(baseUrl, credentials, project) {
|
|
|
575
609
|
}
|
|
576
610
|
const statusMap = sessionStatusMapSchema.parse(JSON.parse(statusRes.bodyText));
|
|
577
611
|
const sessions = sessionListSchema.parse(JSON.parse(listRes.bodyText));
|
|
578
|
-
const busyCount = Object.values(statusMap).filter((s) => s
|
|
612
|
+
const busyCount = Object.values(statusMap).filter((s) => isStatusBusy(s)).length;
|
|
579
613
|
const capped = sessions.length > 100;
|
|
580
614
|
let pendingQuestions;
|
|
615
|
+
let pendingQuestionsBySession = new Map;
|
|
581
616
|
if (questionRes.res.ok) {
|
|
582
617
|
try {
|
|
583
618
|
const entries = pendingQuestionListSchema.parse(JSON.parse(questionRes.bodyText));
|
|
584
|
-
|
|
619
|
+
const formatted = entries.map((e) => formatQuestionEntry(e));
|
|
620
|
+
pendingQuestions = formatted;
|
|
621
|
+
pendingQuestionsBySession = new Map(formatted.map((q) => [q.sessionId, q]));
|
|
585
622
|
} catch {
|
|
586
623
|
pendingQuestions = undefined;
|
|
587
624
|
}
|
|
588
625
|
}
|
|
626
|
+
const sessionStates = sessions.map((s) => {
|
|
627
|
+
const entry = statusMap[s.id];
|
|
628
|
+
const busy = isStatusBusy(entry);
|
|
629
|
+
const pendingQuestion = pendingQuestionsBySession.get(s.id);
|
|
630
|
+
const state = deriveSessionState({
|
|
631
|
+
busy,
|
|
632
|
+
pendingQuestion,
|
|
633
|
+
resolved: true
|
|
634
|
+
});
|
|
635
|
+
return {
|
|
636
|
+
sessionId: s.id,
|
|
637
|
+
state,
|
|
638
|
+
resultAvailable: state === "complete"
|
|
639
|
+
};
|
|
640
|
+
});
|
|
589
641
|
return {
|
|
590
642
|
name: project.name,
|
|
591
643
|
path: directory,
|
|
@@ -594,7 +646,8 @@ async function fetchSnapshotProject(baseUrl, credentials, project) {
|
|
|
594
646
|
busyCount,
|
|
595
647
|
sessionCount: capped ? 100 : sessions.length,
|
|
596
648
|
sessionCountCapped: capped,
|
|
597
|
-
pendingQuestions
|
|
649
|
+
pendingQuestions,
|
|
650
|
+
sessions: sessionStates
|
|
598
651
|
};
|
|
599
652
|
} catch (e) {
|
|
600
653
|
return {
|
|
@@ -640,11 +693,127 @@ async function snapshot(opts) {
|
|
|
640
693
|
});
|
|
641
694
|
return { ok: true, projects: results };
|
|
642
695
|
}
|
|
696
|
+
var DEFAULT_WAIT_TIMEOUT_MS = 60000;
|
|
697
|
+
var DEFAULT_WAIT_POLL_INTERVAL_MS = 1500;
|
|
698
|
+
var DEFAULT_WAIT_CONCURRENCY = 4;
|
|
699
|
+
var NEEDS_ATTENTION_STATES = [
|
|
700
|
+
"complete",
|
|
701
|
+
"blocked",
|
|
702
|
+
"failed",
|
|
703
|
+
"not_found"
|
|
704
|
+
];
|
|
705
|
+
function sleep(ms) {
|
|
706
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
707
|
+
}
|
|
708
|
+
async function resolveWaitSessions(baseUrl, credentials, projects, sessionIds) {
|
|
709
|
+
const locations = await mapWithConcurrency(sessionIds, DEFAULT_WAIT_CONCURRENCY, async (id) => {
|
|
710
|
+
const loc = await findSessionDirectory(baseUrl, credentials, projects, id);
|
|
711
|
+
return { id, loc };
|
|
712
|
+
});
|
|
713
|
+
const locationById = new Map;
|
|
714
|
+
for (const { id, loc } of locations) {
|
|
715
|
+
locationById.set(id, loc.ok ? { directory: loc.directory, project: loc.project } : null);
|
|
716
|
+
}
|
|
717
|
+
const groups = new Map;
|
|
718
|
+
for (const id of sessionIds) {
|
|
719
|
+
const loc = locationById.get(id);
|
|
720
|
+
if (!loc)
|
|
721
|
+
continue;
|
|
722
|
+
const group = groups.get(loc.directory) ?? {
|
|
723
|
+
directory: loc.directory,
|
|
724
|
+
sessionIds: []
|
|
725
|
+
};
|
|
726
|
+
group.sessionIds.push(id);
|
|
727
|
+
groups.set(loc.directory, group);
|
|
728
|
+
}
|
|
729
|
+
const lastKnown = new Map;
|
|
730
|
+
for (const id of sessionIds) {
|
|
731
|
+
const loc = locationById.get(id);
|
|
732
|
+
lastKnown.set(id, {
|
|
733
|
+
sessionId: id,
|
|
734
|
+
project: loc?.project ?? "",
|
|
735
|
+
state: loc ? "running" : "not_found",
|
|
736
|
+
resultAvailable: false
|
|
737
|
+
});
|
|
738
|
+
}
|
|
739
|
+
return { locationById, groupList: Array.from(groups.values()), lastKnown };
|
|
740
|
+
}
|
|
741
|
+
async function wait(sessionIds, opts) {
|
|
742
|
+
const ctx = validateContext(opts.context);
|
|
743
|
+
if (!ctx.ok)
|
|
744
|
+
return ctx;
|
|
745
|
+
const { baseUrl, projects, credentials } = ctx;
|
|
746
|
+
const uniqueIds = Array.from(new Set(sessionIds));
|
|
747
|
+
if (uniqueIds.length === 0) {
|
|
748
|
+
return { ok: true, sessions: [], waker: [], timedOut: true };
|
|
749
|
+
}
|
|
750
|
+
const timeoutMs = opts.timeoutMs ?? DEFAULT_WAIT_TIMEOUT_MS;
|
|
751
|
+
const pollIntervalMs = opts.pollIntervalMs ?? DEFAULT_WAIT_POLL_INTERVAL_MS;
|
|
752
|
+
const deadline = Date.now() + timeoutMs;
|
|
753
|
+
const { locationById, groupList, lastKnown } = await resolveWaitSessions(baseUrl, credentials, projects, uniqueIds);
|
|
754
|
+
async function pollGroup(group) {
|
|
755
|
+
const [statusRes, questionRes] = await Promise.all([
|
|
756
|
+
api(baseUrl, credentials, group.directory, "/session/status"),
|
|
757
|
+
api(baseUrl, credentials, group.directory, "/question")
|
|
758
|
+
]);
|
|
759
|
+
if (!statusRes.res.ok)
|
|
760
|
+
return;
|
|
761
|
+
let statusMap;
|
|
762
|
+
try {
|
|
763
|
+
statusMap = sessionStatusMapSchema.parse(JSON.parse(statusRes.bodyText));
|
|
764
|
+
} catch {
|
|
765
|
+
return;
|
|
766
|
+
}
|
|
767
|
+
const pendingBySession = parsePendingQuestions(questionRes);
|
|
768
|
+
for (const id of group.sessionIds) {
|
|
769
|
+
const entry = statusMap[id];
|
|
770
|
+
const busy = isStatusBusy(entry);
|
|
771
|
+
const pendingQuestion = pendingBySession.get(id);
|
|
772
|
+
const state = deriveSessionState({
|
|
773
|
+
busy,
|
|
774
|
+
pendingQuestion,
|
|
775
|
+
resolved: true
|
|
776
|
+
});
|
|
777
|
+
lastKnown.set(id, {
|
|
778
|
+
sessionId: id,
|
|
779
|
+
project: locationById.get(id)?.project ?? "",
|
|
780
|
+
state,
|
|
781
|
+
resultAvailable: state === "complete",
|
|
782
|
+
pendingQuestion
|
|
783
|
+
});
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
while (true) {
|
|
787
|
+
await mapWithConcurrency(groupList, DEFAULT_WAIT_CONCURRENCY, pollGroup);
|
|
788
|
+
const sessions = uniqueIds.map((id) => lastKnown.get(id));
|
|
789
|
+
const waker = sessions.filter((s) => NEEDS_ATTENTION_STATES.includes(s.state)).map((s) => s.sessionId);
|
|
790
|
+
if (waker.length > 0) {
|
|
791
|
+
return { ok: true, sessions, waker, timedOut: false };
|
|
792
|
+
}
|
|
793
|
+
if (Date.now() >= deadline) {
|
|
794
|
+
return { ok: true, sessions, waker: [], timedOut: true };
|
|
795
|
+
}
|
|
796
|
+
const remaining = deadline - Date.now();
|
|
797
|
+
await sleep(Math.min(pollIntervalMs, Math.max(0, remaining)));
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
function parsePendingQuestions(questionRes) {
|
|
801
|
+
if (!questionRes.res.ok)
|
|
802
|
+
return new Map;
|
|
803
|
+
try {
|
|
804
|
+
const entries = pendingQuestionListSchema.parse(JSON.parse(questionRes.bodyText));
|
|
805
|
+
return new Map(entries.map((e) => [e.sessionID, formatQuestionEntry(e)]));
|
|
806
|
+
} catch {
|
|
807
|
+
return new Map;
|
|
808
|
+
}
|
|
809
|
+
}
|
|
643
810
|
export {
|
|
811
|
+
wait,
|
|
644
812
|
toDispatchArgs,
|
|
645
813
|
status,
|
|
646
814
|
snapshot,
|
|
647
815
|
roster,
|
|
648
816
|
result,
|
|
649
|
-
dispatch
|
|
817
|
+
dispatch,
|
|
818
|
+
deriveSessionState
|
|
650
819
|
};
|
package/dist/format.d.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* @experimental
|
|
3
3
|
* Experimental — shapes may change in minor releases.
|
|
4
4
|
*/
|
|
5
|
-
import type { DispatchResult, RosterProject, SessionResultResult, SessionStatusResult } from "./core";
|
|
5
|
+
import type { DispatchResult, RosterProject, SessionResultResult, SessionStatusResult, WaitResult } from "./core";
|
|
6
6
|
export declare function formatRoster(projects: RosterProject[]): string;
|
|
7
7
|
export declare function formatDispatch(r: DispatchResult): string;
|
|
8
8
|
/** Machine-readable dispatch metadata, shared verbatim by both surfaces
|
|
@@ -16,4 +16,5 @@ export type DispatchMetadata = {
|
|
|
16
16
|
};
|
|
17
17
|
export declare function dispatchMetadata(r: DispatchResult): DispatchMetadata;
|
|
18
18
|
export declare function formatStatus(r: SessionStatusResult): string;
|
|
19
|
+
export declare function formatWait(r: WaitResult): string;
|
|
19
20
|
export declare function formatResult(r: SessionResultResult): string;
|
package/dist/format.js
CHANGED
|
@@ -45,6 +45,25 @@ function formatStatus(r) {
|
|
|
45
45
|
return lines.join(`
|
|
46
46
|
`);
|
|
47
47
|
}
|
|
48
|
+
function formatWait(r) {
|
|
49
|
+
const header = r.timedOut ? "timed out — no session reached a needs-attention state" : `woke on: ${r.waker.join(", ")}`;
|
|
50
|
+
const sessionLines = r.sessions.map((s) => {
|
|
51
|
+
const parts = [
|
|
52
|
+
` - ${s.sessionId} (${s.project || "(unresolved)"}): ${s.state}`
|
|
53
|
+
];
|
|
54
|
+
if (s.pendingQuestion) {
|
|
55
|
+
parts.push(` blocked: waiting on a question — "${s.pendingQuestion.preview}"`);
|
|
56
|
+
if (s.pendingQuestion.options.length > 0) {
|
|
57
|
+
parts.push(` options: ${s.pendingQuestion.options.join(" | ")}`);
|
|
58
|
+
}
|
|
59
|
+
parts.push(` (answer with bus_task using sessionId)`);
|
|
60
|
+
}
|
|
61
|
+
return parts.join(`
|
|
62
|
+
`);
|
|
63
|
+
});
|
|
64
|
+
return [header, ...sessionLines].join(`
|
|
65
|
+
`);
|
|
66
|
+
}
|
|
48
67
|
function formatResult(r) {
|
|
49
68
|
const diffLines = r.diff.length ? r.diff.map((d) => ` - ${d.file ?? "(unknown)"} [${d.status ?? "?"}] +${d.additions}/-${d.deletions}`).join(`
|
|
50
69
|
`) : " (no changes)";
|
|
@@ -58,6 +77,7 @@ function formatResult(r) {
|
|
|
58
77
|
`);
|
|
59
78
|
}
|
|
60
79
|
export {
|
|
80
|
+
formatWait,
|
|
61
81
|
formatStatus,
|
|
62
82
|
formatRoster,
|
|
63
83
|
formatResult,
|