pi-crew 0.1.30 → 0.1.32
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/CHANGELOG.md +9 -0
- package/README.md +80 -27
- package/agents/analyst.md +11 -11
- package/agents/critic.md +11 -11
- package/agents/executor.md +11 -11
- package/agents/explorer.md +11 -11
- package/agents/planner.md +11 -11
- package/agents/reviewer.md +11 -11
- package/agents/security-reviewer.md +11 -11
- package/agents/test-engineer.md +11 -11
- package/agents/verifier.md +11 -11
- package/agents/writer.md +11 -11
- package/docs/architecture.md +173 -164
- package/docs/refactor-tasks-phase3.md +394 -394
- package/docs/refactor-tasks-phase4.md +564 -564
- package/docs/refactor-tasks-phase5.md +402 -402
- package/docs/refactor-tasks-phase6.md +662 -662
- package/docs/research-extension-examples.md +297 -0
- package/docs/research-extension-system.md +324 -0
- package/docs/research-optimization-plan.md +548 -0
- package/docs/research-pi-coding-agent.md +357 -0
- package/docs/resource-formats.md +4 -3
- package/docs/runtime-flow.md +148 -148
- package/docs/source-runtime-refactor-map.md +83 -83
- package/docs/usage.md +3 -3
- package/index.ts +6 -6
- package/package.json +1 -1
- package/schema.json +53 -1
- package/skills/git-master/SKILL.md +24 -19
- package/skills/read-only-explorer/SKILL.md +26 -21
- package/skills/safe-bash/SKILL.md +21 -16
- package/skills/task-packet/SKILL.md +28 -23
- package/skills/verify-evidence/SKILL.md +27 -22
- package/src/agents/agent-serializer.ts +34 -34
- package/src/agents/discover-agents.ts +102 -102
- package/src/config/config.ts +14 -1
- package/src/config/defaults.ts +3 -2
- package/src/extension/cross-extension-rpc.ts +82 -82
- package/src/extension/import-index.ts +4 -3
- package/src/extension/management.ts +2 -2
- package/src/extension/project-init.ts +9 -7
- package/src/extension/register.ts +63 -0
- package/src/extension/registration/artifact-cleanup.ts +15 -14
- package/src/extension/registration/commands.ts +208 -208
- package/src/extension/registration/compaction-guard.ts +125 -0
- package/src/extension/registration/subagent-tools.ts +27 -8
- package/src/extension/registration/team-tool.ts +61 -44
- package/src/extension/result-watcher.ts +98 -98
- package/src/extension/run-import.ts +4 -4
- package/src/extension/run-index.ts +14 -14
- package/src/extension/run-maintenance.ts +24 -24
- package/src/extension/team-tool/api.ts +3 -0
- package/src/extension/team-tool/cancel.ts +31 -31
- package/src/extension/team-tool/doctor.ts +179 -178
- package/src/extension/team-tool/inspect.ts +41 -41
- package/src/extension/team-tool/lifecycle-actions.ts +79 -79
- package/src/extension/team-tool/plan.ts +19 -19
- package/src/extension/team-tool/status.ts +73 -73
- package/src/prompt/prompt-runtime.ts +68 -68
- package/src/runtime/agent-control.ts +64 -64
- package/src/runtime/agent-memory.ts +72 -72
- package/src/runtime/agent-observability.ts +113 -113
- package/src/runtime/async-marker.ts +26 -26
- package/src/runtime/background-runner.ts +53 -53
- package/src/runtime/crew-agent-runtime.ts +58 -58
- package/src/runtime/direct-run.ts +35 -35
- package/src/runtime/foreground-control.ts +82 -82
- package/src/runtime/green-contract.ts +46 -46
- package/src/runtime/group-join.ts +88 -88
- package/src/runtime/live-agent-control.ts +78 -78
- package/src/runtime/live-agent-manager.ts +85 -85
- package/src/runtime/live-control-realtime.ts +36 -36
- package/src/runtime/live-session-runtime.ts +299 -299
- package/src/runtime/manifest-cache.ts +212 -214
- package/src/runtime/model-fallback.ts +261 -261
- package/src/runtime/parallel-research.ts +44 -44
- package/src/runtime/parallel-utils.ts +99 -99
- package/src/runtime/pi-json-output.ts +111 -111
- package/src/runtime/pi-spawn.ts +96 -96
- package/src/runtime/policy-engine.ts +78 -78
- package/src/runtime/post-exit-stdio-guard.ts +86 -86
- package/src/runtime/process-status.ts +56 -56
- package/src/runtime/progress-event-coalescer.ts +43 -43
- package/src/runtime/recovery-recipes.ts +74 -74
- package/src/runtime/role-permission.ts +39 -39
- package/src/runtime/session-usage.ts +79 -79
- package/src/runtime/sidechain-output.ts +28 -28
- package/src/runtime/subagent-manager.ts +29 -2
- package/src/runtime/task-display.ts +38 -38
- package/src/runtime/task-output-context.ts +106 -106
- package/src/runtime/task-packet.ts +84 -84
- package/src/runtime/task-runner/live-executor.ts +98 -98
- package/src/runtime/task-runner/progress.ts +111 -111
- package/src/runtime/task-runner/prompt-builder.ts +72 -72
- package/src/runtime/task-runner/result-utils.ts +14 -14
- package/src/runtime/task-runner/state-helpers.ts +22 -22
- package/src/runtime/worker-heartbeat.ts +21 -21
- package/src/runtime/worker-startup.ts +57 -57
- package/src/schema/config-schema.ts +12 -0
- package/src/schema/team-tool-schema.ts +100 -100
- package/src/state/artifact-store.ts +108 -108
- package/src/state/contracts.ts +105 -105
- package/src/state/jsonl-writer.ts +77 -77
- package/src/state/state-store.ts +8 -9
- package/src/state/task-claims.ts +42 -42
- package/src/state/types.ts +180 -180
- package/src/state/usage.ts +29 -29
- package/src/subagents/async-entry.ts +1 -1
- package/src/subagents/index.ts +3 -3
- package/src/subagents/live/control.ts +1 -1
- package/src/subagents/live/manager.ts +1 -1
- package/src/subagents/live/realtime.ts +1 -1
- package/src/subagents/live/session-runtime.ts +1 -1
- package/src/subagents/manager.ts +1 -1
- package/src/subagents/spawn.ts +1 -1
- package/src/teams/discover-teams.ts +2 -2
- package/src/teams/team-serializer.ts +36 -36
- package/src/types/diff.d.ts +18 -0
- package/src/ui/crew-footer.ts +101 -101
- package/src/ui/crew-select-list.ts +111 -111
- package/src/ui/crew-widget.ts +285 -285
- package/src/ui/dynamic-border.ts +25 -25
- package/src/ui/layout-primitives.ts +106 -106
- package/src/ui/loaders.ts +158 -158
- package/src/ui/mascot.ts +441 -441
- package/src/ui/powerbar-publisher.ts +94 -94
- package/src/ui/render-diff.ts +119 -119
- package/src/ui/run-dashboard.ts +372 -372
- package/src/ui/status-colors.ts +54 -54
- package/src/ui/syntax-highlight.ts +116 -116
- package/src/ui/transcript-viewer.ts +302 -302
- package/src/utils/completion-dedupe.ts +63 -63
- package/src/utils/file-coalescer.ts +84 -84
- package/src/utils/frontmatter.ts +36 -36
- package/src/utils/fs-watch.ts +31 -31
- package/src/utils/git.ts +262 -262
- package/src/utils/ids.ts +12 -12
- package/src/utils/names.ts +26 -26
- package/src/utils/paths.ts +34 -7
- package/src/utils/sleep.ts +32 -32
- package/src/utils/timings.ts +31 -31
- package/src/utils/visual.ts +159 -159
- package/src/workflows/discover-workflows.ts +2 -2
- package/src/workflows/validate-workflow.ts +40 -40
- package/src/worktree/branch-freshness.ts +45 -45
- package/src/worktree/cleanup.ts +71 -69
- package/src/worktree/worktree-manager.ts +3 -1
- package/teams/default.team.md +12 -12
- package/teams/fast-fix.team.md +11 -11
- package/teams/implementation.team.md +18 -18
- package/teams/parallel-research.team.md +14 -14
- package/teams/research.team.md +11 -11
- package/teams/review.team.md +12 -12
- package/workflows/default.workflow.md +29 -29
- package/workflows/fast-fix.workflow.md +22 -22
- package/workflows/implementation.workflow.md +38 -38
- package/workflows/parallel-research.workflow.md +46 -46
- package/workflows/research.workflow.md +22 -22
- package/workflows/review.workflow.md +30 -30
|
@@ -1,82 +1,82 @@
|
|
|
1
|
-
import * as fs from "node:fs";
|
|
2
|
-
import * as path from "node:path";
|
|
3
|
-
import { appendEvent } from "../state/event-log.ts";
|
|
4
|
-
import type { TeamRunManifest, TeamTaskState } from "../state/types.ts";
|
|
5
|
-
import { checkProcessLiveness, isActiveRunStatus } from "./process-status.ts";
|
|
6
|
-
import { readCrewAgents } from "./crew-agent-records.ts";
|
|
7
|
-
|
|
8
|
-
export type ForegroundControlRequestType = "interrupt" | "status";
|
|
9
|
-
|
|
10
|
-
export interface ForegroundControlStatus {
|
|
11
|
-
runId: string;
|
|
12
|
-
status: TeamRunManifest["status"];
|
|
13
|
-
active: boolean;
|
|
14
|
-
asyncPid?: number;
|
|
15
|
-
asyncAlive?: boolean;
|
|
16
|
-
runningTasks: string[];
|
|
17
|
-
runningAgents: string[];
|
|
18
|
-
controlPath: string;
|
|
19
|
-
lastRequest?: ForegroundControlRequest;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export interface ForegroundControlRequest {
|
|
23
|
-
id: string;
|
|
24
|
-
type: ForegroundControlRequestType;
|
|
25
|
-
createdAt: string;
|
|
26
|
-
reason: string;
|
|
27
|
-
acknowledged: boolean;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export function foregroundControlPath(manifest: TeamRunManifest): string {
|
|
31
|
-
return path.join(manifest.stateRoot, "foreground-control.json");
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
function readLastRequest(controlPath: string): ForegroundControlRequest | undefined {
|
|
35
|
-
if (!fs.existsSync(controlPath)) return undefined;
|
|
36
|
-
try {
|
|
37
|
-
const parsed = JSON.parse(fs.readFileSync(controlPath, "utf-8")) as { requests?: ForegroundControlRequest[] };
|
|
38
|
-
return parsed.requests?.at(-1);
|
|
39
|
-
} catch {
|
|
40
|
-
return undefined;
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export function readForegroundControlStatus(manifest: TeamRunManifest, tasks: TeamTaskState[]): ForegroundControlStatus {
|
|
45
|
-
const controlPath = foregroundControlPath(manifest);
|
|
46
|
-
const asyncAlive = manifest.async?.pid !== undefined ? checkProcessLiveness(manifest.async.pid).alive : undefined;
|
|
47
|
-
return {
|
|
48
|
-
runId: manifest.runId,
|
|
49
|
-
status: manifest.status,
|
|
50
|
-
active: isActiveRunStatus(manifest.status),
|
|
51
|
-
asyncPid: manifest.async?.pid,
|
|
52
|
-
asyncAlive,
|
|
53
|
-
runningTasks: tasks.filter((task) => task.status === "running").map((task) => task.id),
|
|
54
|
-
runningAgents: readCrewAgents(manifest).filter((agent) => agent.status === "running").map((agent) => agent.id),
|
|
55
|
-
controlPath,
|
|
56
|
-
lastRequest: readLastRequest(controlPath),
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export function writeForegroundInterruptRequest(manifest: TeamRunManifest, reason = "User requested foreground interrupt."): ForegroundControlRequest {
|
|
61
|
-
const controlPath = foregroundControlPath(manifest);
|
|
62
|
-
let requests: ForegroundControlRequest[] = [];
|
|
63
|
-
if (fs.existsSync(controlPath)) {
|
|
64
|
-
try {
|
|
65
|
-
const parsed = JSON.parse(fs.readFileSync(controlPath, "utf-8")) as { requests?: ForegroundControlRequest[] };
|
|
66
|
-
requests = Array.isArray(parsed.requests) ? parsed.requests : [];
|
|
67
|
-
} catch {
|
|
68
|
-
requests = [];
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
const request: ForegroundControlRequest = {
|
|
72
|
-
id: `fg_${Date.now().toString(36)}_${Math.random().toString(16).slice(2, 10)}`,
|
|
73
|
-
type: "interrupt",
|
|
74
|
-
createdAt: new Date().toISOString(),
|
|
75
|
-
reason,
|
|
76
|
-
acknowledged: false,
|
|
77
|
-
};
|
|
78
|
-
fs.mkdirSync(path.dirname(controlPath), { recursive: true });
|
|
79
|
-
fs.writeFileSync(controlPath, `${JSON.stringify({ requests: [...requests, request] }, null, 2)}\n`, "utf-8");
|
|
80
|
-
appendEvent(manifest.eventsPath, { type: "foreground.interrupt_requested", runId: manifest.runId, message: reason, data: { requestId: request.id, controlPath } });
|
|
81
|
-
return request;
|
|
82
|
-
}
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
import { appendEvent } from "../state/event-log.ts";
|
|
4
|
+
import type { TeamRunManifest, TeamTaskState } from "../state/types.ts";
|
|
5
|
+
import { checkProcessLiveness, isActiveRunStatus } from "./process-status.ts";
|
|
6
|
+
import { readCrewAgents } from "./crew-agent-records.ts";
|
|
7
|
+
|
|
8
|
+
export type ForegroundControlRequestType = "interrupt" | "status";
|
|
9
|
+
|
|
10
|
+
export interface ForegroundControlStatus {
|
|
11
|
+
runId: string;
|
|
12
|
+
status: TeamRunManifest["status"];
|
|
13
|
+
active: boolean;
|
|
14
|
+
asyncPid?: number;
|
|
15
|
+
asyncAlive?: boolean;
|
|
16
|
+
runningTasks: string[];
|
|
17
|
+
runningAgents: string[];
|
|
18
|
+
controlPath: string;
|
|
19
|
+
lastRequest?: ForegroundControlRequest;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface ForegroundControlRequest {
|
|
23
|
+
id: string;
|
|
24
|
+
type: ForegroundControlRequestType;
|
|
25
|
+
createdAt: string;
|
|
26
|
+
reason: string;
|
|
27
|
+
acknowledged: boolean;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function foregroundControlPath(manifest: TeamRunManifest): string {
|
|
31
|
+
return path.join(manifest.stateRoot, "foreground-control.json");
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function readLastRequest(controlPath: string): ForegroundControlRequest | undefined {
|
|
35
|
+
if (!fs.existsSync(controlPath)) return undefined;
|
|
36
|
+
try {
|
|
37
|
+
const parsed = JSON.parse(fs.readFileSync(controlPath, "utf-8")) as { requests?: ForegroundControlRequest[] };
|
|
38
|
+
return parsed.requests?.at(-1);
|
|
39
|
+
} catch {
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function readForegroundControlStatus(manifest: TeamRunManifest, tasks: TeamTaskState[]): ForegroundControlStatus {
|
|
45
|
+
const controlPath = foregroundControlPath(manifest);
|
|
46
|
+
const asyncAlive = manifest.async?.pid !== undefined ? checkProcessLiveness(manifest.async.pid).alive : undefined;
|
|
47
|
+
return {
|
|
48
|
+
runId: manifest.runId,
|
|
49
|
+
status: manifest.status,
|
|
50
|
+
active: isActiveRunStatus(manifest.status),
|
|
51
|
+
asyncPid: manifest.async?.pid,
|
|
52
|
+
asyncAlive,
|
|
53
|
+
runningTasks: tasks.filter((task) => task.status === "running").map((task) => task.id),
|
|
54
|
+
runningAgents: readCrewAgents(manifest).filter((agent) => agent.status === "running").map((agent) => agent.id),
|
|
55
|
+
controlPath,
|
|
56
|
+
lastRequest: readLastRequest(controlPath),
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function writeForegroundInterruptRequest(manifest: TeamRunManifest, reason = "User requested foreground interrupt."): ForegroundControlRequest {
|
|
61
|
+
const controlPath = foregroundControlPath(manifest);
|
|
62
|
+
let requests: ForegroundControlRequest[] = [];
|
|
63
|
+
if (fs.existsSync(controlPath)) {
|
|
64
|
+
try {
|
|
65
|
+
const parsed = JSON.parse(fs.readFileSync(controlPath, "utf-8")) as { requests?: ForegroundControlRequest[] };
|
|
66
|
+
requests = Array.isArray(parsed.requests) ? parsed.requests : [];
|
|
67
|
+
} catch {
|
|
68
|
+
requests = [];
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
const request: ForegroundControlRequest = {
|
|
72
|
+
id: `fg_${Date.now().toString(36)}_${Math.random().toString(16).slice(2, 10)}`,
|
|
73
|
+
type: "interrupt",
|
|
74
|
+
createdAt: new Date().toISOString(),
|
|
75
|
+
reason,
|
|
76
|
+
acknowledged: false,
|
|
77
|
+
};
|
|
78
|
+
fs.mkdirSync(path.dirname(controlPath), { recursive: true });
|
|
79
|
+
fs.writeFileSync(controlPath, `${JSON.stringify({ requests: [...requests, request] }, null, 2)}\n`, "utf-8");
|
|
80
|
+
appendEvent(manifest.eventsPath, { type: "foreground.interrupt_requested", runId: manifest.runId, message: reason, data: { requestId: request.id, controlPath } });
|
|
81
|
+
return request;
|
|
82
|
+
}
|
|
@@ -1,46 +1,46 @@
|
|
|
1
|
-
import type { GreenLevel, VerificationContract, VerificationEvidence } from "../state/types.ts";
|
|
2
|
-
|
|
3
|
-
const GREEN_ORDER: Record<GreenLevel, number> = {
|
|
4
|
-
none: 0,
|
|
5
|
-
targeted: 1,
|
|
6
|
-
package: 2,
|
|
7
|
-
workspace: 3,
|
|
8
|
-
merge_ready: 4,
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
export interface GreenContractOutcome {
|
|
12
|
-
requiredGreenLevel: GreenLevel;
|
|
13
|
-
observedGreenLevel: GreenLevel;
|
|
14
|
-
satisfied: boolean;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export function greenLevelSatisfies(observed: GreenLevel, required: GreenLevel): boolean {
|
|
18
|
-
return GREEN_ORDER[observed] >= GREEN_ORDER[required];
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export function evaluateGreenContract(contract: VerificationContract, evidence?: VerificationEvidence): GreenContractOutcome {
|
|
22
|
-
const observedGreenLevel = evidence?.observedGreenLevel ?? "none";
|
|
23
|
-
return {
|
|
24
|
-
requiredGreenLevel: contract.requiredGreenLevel,
|
|
25
|
-
observedGreenLevel,
|
|
26
|
-
satisfied: greenLevelSatisfies(observedGreenLevel, contract.requiredGreenLevel),
|
|
27
|
-
};
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export function inferGreenLevelFromTask(success: boolean, contract: VerificationContract): GreenLevel {
|
|
31
|
-
if (!success) return "none";
|
|
32
|
-
if (contract.requiredGreenLevel === "none") return "none";
|
|
33
|
-
return contract.allowManualEvidence ? contract.requiredGreenLevel : "targeted";
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export function createVerificationEvidence(contract: VerificationContract, success: boolean, notes: string): VerificationEvidence {
|
|
37
|
-
const observedGreenLevel = inferGreenLevelFromTask(success, contract);
|
|
38
|
-
const outcome = evaluateGreenContract(contract, { requiredGreenLevel: contract.requiredGreenLevel, observedGreenLevel, satisfied: false, commands: [], notes });
|
|
39
|
-
return {
|
|
40
|
-
requiredGreenLevel: contract.requiredGreenLevel,
|
|
41
|
-
observedGreenLevel,
|
|
42
|
-
satisfied: outcome.satisfied,
|
|
43
|
-
commands: contract.commands.map((cmd) => ({ cmd, status: "not_run" as const })),
|
|
44
|
-
notes,
|
|
45
|
-
};
|
|
46
|
-
}
|
|
1
|
+
import type { GreenLevel, VerificationContract, VerificationEvidence } from "../state/types.ts";
|
|
2
|
+
|
|
3
|
+
const GREEN_ORDER: Record<GreenLevel, number> = {
|
|
4
|
+
none: 0,
|
|
5
|
+
targeted: 1,
|
|
6
|
+
package: 2,
|
|
7
|
+
workspace: 3,
|
|
8
|
+
merge_ready: 4,
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export interface GreenContractOutcome {
|
|
12
|
+
requiredGreenLevel: GreenLevel;
|
|
13
|
+
observedGreenLevel: GreenLevel;
|
|
14
|
+
satisfied: boolean;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function greenLevelSatisfies(observed: GreenLevel, required: GreenLevel): boolean {
|
|
18
|
+
return GREEN_ORDER[observed] >= GREEN_ORDER[required];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function evaluateGreenContract(contract: VerificationContract, evidence?: VerificationEvidence): GreenContractOutcome {
|
|
22
|
+
const observedGreenLevel = evidence?.observedGreenLevel ?? "none";
|
|
23
|
+
return {
|
|
24
|
+
requiredGreenLevel: contract.requiredGreenLevel,
|
|
25
|
+
observedGreenLevel,
|
|
26
|
+
satisfied: greenLevelSatisfies(observedGreenLevel, contract.requiredGreenLevel),
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function inferGreenLevelFromTask(success: boolean, contract: VerificationContract): GreenLevel {
|
|
31
|
+
if (!success) return "none";
|
|
32
|
+
if (contract.requiredGreenLevel === "none") return "none";
|
|
33
|
+
return contract.allowManualEvidence ? contract.requiredGreenLevel : "targeted";
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function createVerificationEvidence(contract: VerificationContract, success: boolean, notes: string): VerificationEvidence {
|
|
37
|
+
const observedGreenLevel = inferGreenLevelFromTask(success, contract);
|
|
38
|
+
const outcome = evaluateGreenContract(contract, { requiredGreenLevel: contract.requiredGreenLevel, observedGreenLevel, satisfied: false, commands: [], notes });
|
|
39
|
+
return {
|
|
40
|
+
requiredGreenLevel: contract.requiredGreenLevel,
|
|
41
|
+
observedGreenLevel,
|
|
42
|
+
satisfied: outcome.satisfied,
|
|
43
|
+
commands: contract.commands.map((cmd) => ({ cmd, status: "not_run" as const })),
|
|
44
|
+
notes,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
@@ -1,88 +1,88 @@
|
|
|
1
|
-
import type { CrewRuntimeConfig } from "../config/config.ts";
|
|
2
|
-
import { writeArtifact } from "../state/artifact-store.ts";
|
|
3
|
-
import { appendEvent } from "../state/event-log.ts";
|
|
4
|
-
import { appendMailboxMessage } from "../state/mailbox.ts";
|
|
5
|
-
import type { ArtifactDescriptor, TeamRunManifest, TeamTaskState } from "../state/types.ts";
|
|
6
|
-
import { aggregateTaskOutputs } from "./task-output-context.ts";
|
|
7
|
-
|
|
8
|
-
export type CrewGroupJoinMode = "off" | "group" | "smart";
|
|
9
|
-
|
|
10
|
-
export interface CrewGroupJoinDelivery {
|
|
11
|
-
batchId: string;
|
|
12
|
-
mode: CrewGroupJoinMode;
|
|
13
|
-
partial: boolean;
|
|
14
|
-
taskIds: string[];
|
|
15
|
-
completed: string[];
|
|
16
|
-
failed: string[];
|
|
17
|
-
skipped: string[];
|
|
18
|
-
remaining: string[];
|
|
19
|
-
artifact?: ArtifactDescriptor;
|
|
20
|
-
messageId?: string;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export function resolveGroupJoinMode(runtime?: CrewRuntimeConfig): CrewGroupJoinMode {
|
|
24
|
-
return runtime?.groupJoin ?? "smart";
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export function shouldGroupJoin(mode: CrewGroupJoinMode, batch: TeamTaskState[]): boolean {
|
|
28
|
-
if (mode === "off") return false;
|
|
29
|
-
if (mode === "group") return batch.length > 0;
|
|
30
|
-
return batch.length > 1;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function batchIdFor(runId: string, taskIds: string[]): string {
|
|
34
|
-
return `${runId}_${taskIds.join("+").replace(/[^a-zA-Z0-9_+-]/g, "_")}`;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function statusList(tasks: TeamTaskState[], status: TeamTaskState["status"]): string[] {
|
|
38
|
-
return tasks.filter((task) => task.status === status).map((task) => task.id);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export function deliverGroupJoin(input: {
|
|
42
|
-
manifest: TeamRunManifest;
|
|
43
|
-
mode: CrewGroupJoinMode;
|
|
44
|
-
batch: TeamTaskState[];
|
|
45
|
-
allTasks: TeamTaskState[];
|
|
46
|
-
partial?: boolean;
|
|
47
|
-
}): CrewGroupJoinDelivery | undefined {
|
|
48
|
-
if (!shouldGroupJoin(input.mode, input.batch)) return undefined;
|
|
49
|
-
const taskIds = input.batch.map((task) => task.id);
|
|
50
|
-
const latest = taskIds.map((id) => input.allTasks.find((task) => task.id === id)).filter((task): task is TeamTaskState => Boolean(task));
|
|
51
|
-
const completed = statusList(latest, "completed");
|
|
52
|
-
const failed = statusList(latest, "failed");
|
|
53
|
-
const skipped = statusList(latest, "skipped");
|
|
54
|
-
const remaining = latest.filter((task) => task.status === "queued" || task.status === "running").map((task) => task.id);
|
|
55
|
-
const partial = input.partial ?? remaining.length > 0;
|
|
56
|
-
const batchId = batchIdFor(input.manifest.runId, taskIds);
|
|
57
|
-
const summary = aggregateTaskOutputs(latest);
|
|
58
|
-
const delivery: CrewGroupJoinDelivery = { batchId, mode: input.mode, partial, taskIds, completed, failed, skipped, remaining };
|
|
59
|
-
const content = `${JSON.stringify({ ...delivery, createdAt: new Date().toISOString() }, null, 2)}\n`;
|
|
60
|
-
const artifact = writeArtifact(input.manifest.artifactsRoot, {
|
|
61
|
-
kind: "metadata",
|
|
62
|
-
relativePath: `metadata/group-joins/${batchId}.json`,
|
|
63
|
-
producer: "group-join",
|
|
64
|
-
content,
|
|
65
|
-
});
|
|
66
|
-
const mailbox = appendMailboxMessage(input.manifest, {
|
|
67
|
-
direction: "outbox",
|
|
68
|
-
from: "group-join",
|
|
69
|
-
to: "leader",
|
|
70
|
-
body: [
|
|
71
|
-
`Group join ${partial ? "partial" : "completed"}: ${taskIds.join(", ")}`,
|
|
72
|
-
`Completed: ${completed.join(", ") || "none"}`,
|
|
73
|
-
`Failed: ${failed.join(", ") || "none"}`,
|
|
74
|
-
`Skipped: ${skipped.join(", ") || "none"}`,
|
|
75
|
-
`Remaining: ${remaining.join(", ") || "none"}`,
|
|
76
|
-
"",
|
|
77
|
-
summary,
|
|
78
|
-
].join("\n"),
|
|
79
|
-
status: "delivered",
|
|
80
|
-
});
|
|
81
|
-
appendEvent(input.manifest.eventsPath, {
|
|
82
|
-
type: partial ? "agent.group_join.partial" : "agent.group_join.completed",
|
|
83
|
-
runId: input.manifest.runId,
|
|
84
|
-
message: `Group join ${partial ? "partial" : "completed"} for ${taskIds.length} task(s).`,
|
|
85
|
-
data: { ...delivery, artifactPath: artifact.path, messageId: mailbox.id },
|
|
86
|
-
});
|
|
87
|
-
return { ...delivery, artifact, messageId: mailbox.id };
|
|
88
|
-
}
|
|
1
|
+
import type { CrewRuntimeConfig } from "../config/config.ts";
|
|
2
|
+
import { writeArtifact } from "../state/artifact-store.ts";
|
|
3
|
+
import { appendEvent } from "../state/event-log.ts";
|
|
4
|
+
import { appendMailboxMessage } from "../state/mailbox.ts";
|
|
5
|
+
import type { ArtifactDescriptor, TeamRunManifest, TeamTaskState } from "../state/types.ts";
|
|
6
|
+
import { aggregateTaskOutputs } from "./task-output-context.ts";
|
|
7
|
+
|
|
8
|
+
export type CrewGroupJoinMode = "off" | "group" | "smart";
|
|
9
|
+
|
|
10
|
+
export interface CrewGroupJoinDelivery {
|
|
11
|
+
batchId: string;
|
|
12
|
+
mode: CrewGroupJoinMode;
|
|
13
|
+
partial: boolean;
|
|
14
|
+
taskIds: string[];
|
|
15
|
+
completed: string[];
|
|
16
|
+
failed: string[];
|
|
17
|
+
skipped: string[];
|
|
18
|
+
remaining: string[];
|
|
19
|
+
artifact?: ArtifactDescriptor;
|
|
20
|
+
messageId?: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function resolveGroupJoinMode(runtime?: CrewRuntimeConfig): CrewGroupJoinMode {
|
|
24
|
+
return runtime?.groupJoin ?? "smart";
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function shouldGroupJoin(mode: CrewGroupJoinMode, batch: TeamTaskState[]): boolean {
|
|
28
|
+
if (mode === "off") return false;
|
|
29
|
+
if (mode === "group") return batch.length > 0;
|
|
30
|
+
return batch.length > 1;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function batchIdFor(runId: string, taskIds: string[]): string {
|
|
34
|
+
return `${runId}_${taskIds.join("+").replace(/[^a-zA-Z0-9_+-]/g, "_")}`;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function statusList(tasks: TeamTaskState[], status: TeamTaskState["status"]): string[] {
|
|
38
|
+
return tasks.filter((task) => task.status === status).map((task) => task.id);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function deliverGroupJoin(input: {
|
|
42
|
+
manifest: TeamRunManifest;
|
|
43
|
+
mode: CrewGroupJoinMode;
|
|
44
|
+
batch: TeamTaskState[];
|
|
45
|
+
allTasks: TeamTaskState[];
|
|
46
|
+
partial?: boolean;
|
|
47
|
+
}): CrewGroupJoinDelivery | undefined {
|
|
48
|
+
if (!shouldGroupJoin(input.mode, input.batch)) return undefined;
|
|
49
|
+
const taskIds = input.batch.map((task) => task.id);
|
|
50
|
+
const latest = taskIds.map((id) => input.allTasks.find((task) => task.id === id)).filter((task): task is TeamTaskState => Boolean(task));
|
|
51
|
+
const completed = statusList(latest, "completed");
|
|
52
|
+
const failed = statusList(latest, "failed");
|
|
53
|
+
const skipped = statusList(latest, "skipped");
|
|
54
|
+
const remaining = latest.filter((task) => task.status === "queued" || task.status === "running").map((task) => task.id);
|
|
55
|
+
const partial = input.partial ?? remaining.length > 0;
|
|
56
|
+
const batchId = batchIdFor(input.manifest.runId, taskIds);
|
|
57
|
+
const summary = aggregateTaskOutputs(latest);
|
|
58
|
+
const delivery: CrewGroupJoinDelivery = { batchId, mode: input.mode, partial, taskIds, completed, failed, skipped, remaining };
|
|
59
|
+
const content = `${JSON.stringify({ ...delivery, createdAt: new Date().toISOString() }, null, 2)}\n`;
|
|
60
|
+
const artifact = writeArtifact(input.manifest.artifactsRoot, {
|
|
61
|
+
kind: "metadata",
|
|
62
|
+
relativePath: `metadata/group-joins/${batchId}.json`,
|
|
63
|
+
producer: "group-join",
|
|
64
|
+
content,
|
|
65
|
+
});
|
|
66
|
+
const mailbox = appendMailboxMessage(input.manifest, {
|
|
67
|
+
direction: "outbox",
|
|
68
|
+
from: "group-join",
|
|
69
|
+
to: "leader",
|
|
70
|
+
body: [
|
|
71
|
+
`Group join ${partial ? "partial" : "completed"}: ${taskIds.join(", ")}`,
|
|
72
|
+
`Completed: ${completed.join(", ") || "none"}`,
|
|
73
|
+
`Failed: ${failed.join(", ") || "none"}`,
|
|
74
|
+
`Skipped: ${skipped.join(", ") || "none"}`,
|
|
75
|
+
`Remaining: ${remaining.join(", ") || "none"}`,
|
|
76
|
+
"",
|
|
77
|
+
summary,
|
|
78
|
+
].join("\n"),
|
|
79
|
+
status: "delivered",
|
|
80
|
+
});
|
|
81
|
+
appendEvent(input.manifest.eventsPath, {
|
|
82
|
+
type: partial ? "agent.group_join.partial" : "agent.group_join.completed",
|
|
83
|
+
runId: input.manifest.runId,
|
|
84
|
+
message: `Group join ${partial ? "partial" : "completed"} for ${taskIds.length} task(s).`,
|
|
85
|
+
data: { ...delivery, artifactPath: artifact.path, messageId: mailbox.id },
|
|
86
|
+
});
|
|
87
|
+
return { ...delivery, artifact, messageId: mailbox.id };
|
|
88
|
+
}
|
|
@@ -1,78 +1,78 @@
|
|
|
1
|
-
import * as fs from "node:fs";
|
|
2
|
-
import * as path from "node:path";
|
|
3
|
-
import type { TeamRunManifest } from "../state/types.ts";
|
|
4
|
-
|
|
5
|
-
export type LiveAgentControlOperation = "steer" | "stop" | "resume";
|
|
6
|
-
|
|
7
|
-
export interface LiveAgentControlRequest {
|
|
8
|
-
id: string;
|
|
9
|
-
runId: string;
|
|
10
|
-
taskId: string;
|
|
11
|
-
agentId?: string;
|
|
12
|
-
operation: LiveAgentControlOperation;
|
|
13
|
-
message?: string;
|
|
14
|
-
createdAt: string;
|
|
15
|
-
processedAt?: string;
|
|
16
|
-
error?: string;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export interface LiveAgentControlCursor {
|
|
20
|
-
offset: number;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export function liveAgentControlPath(manifest: TeamRunManifest, taskId: string): string {
|
|
24
|
-
return path.join(manifest.stateRoot, "agents", taskId, "live-control.jsonl");
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function requestId(): string {
|
|
28
|
-
return `ctrl_${Date.now().toString(36)}_${Math.random().toString(16).slice(2, 10)}`;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export function appendLiveAgentControlRequest(manifest: TeamRunManifest, input: { taskId: string; agentId?: string; operation: LiveAgentControlOperation; message?: string }): LiveAgentControlRequest {
|
|
32
|
-
const request: LiveAgentControlRequest = {
|
|
33
|
-
id: requestId(),
|
|
34
|
-
runId: manifest.runId,
|
|
35
|
-
taskId: input.taskId,
|
|
36
|
-
agentId: input.agentId,
|
|
37
|
-
operation: input.operation,
|
|
38
|
-
message: input.message,
|
|
39
|
-
createdAt: new Date().toISOString(),
|
|
40
|
-
};
|
|
41
|
-
const filePath = liveAgentControlPath(manifest, input.taskId);
|
|
42
|
-
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
43
|
-
fs.appendFileSync(filePath, `${JSON.stringify(request)}\n`, "utf-8");
|
|
44
|
-
return request;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export function readLiveAgentControlRequests(manifest: TeamRunManifest, taskId: string, cursor: LiveAgentControlCursor = { offset: 0 }): { requests: LiveAgentControlRequest[]; cursor: LiveAgentControlCursor } {
|
|
48
|
-
const filePath = liveAgentControlPath(manifest, taskId);
|
|
49
|
-
if (!fs.existsSync(filePath)) return { requests: [], cursor };
|
|
50
|
-
const text = fs.readFileSync(filePath, "utf-8");
|
|
51
|
-
const lines = text.split(/\r?\n/).filter(Boolean);
|
|
52
|
-
const requests = lines.slice(cursor.offset).flatMap((line) => {
|
|
53
|
-
try {
|
|
54
|
-
const parsed = JSON.parse(line) as LiveAgentControlRequest;
|
|
55
|
-
return parsed && parsed.runId === manifest.runId && parsed.taskId === taskId ? [parsed] : [];
|
|
56
|
-
} catch {
|
|
57
|
-
return [];
|
|
58
|
-
}
|
|
59
|
-
});
|
|
60
|
-
return { requests, cursor: { offset: lines.length } };
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export async function applyLiveAgentControlRequest(input: { request: LiveAgentControlRequest; taskId: string; agentId: string; session: { steer?: (text: string) => Promise<void>; prompt?: (text: string, options?: Record<string, unknown>) => Promise<void>; abort?: () => Promise<void> | void }; seenRequestIds?: Set<string> }): Promise<boolean> {
|
|
64
|
-
const { request, taskId, agentId, session, seenRequestIds } = input;
|
|
65
|
-
if (seenRequestIds?.has(request.id)) return false;
|
|
66
|
-
if (request.agentId && request.agentId !== agentId && request.agentId !== taskId) return false;
|
|
67
|
-
seenRequestIds?.add(request.id);
|
|
68
|
-
if (request.operation === "steer") await session.steer?.(request.message ?? "Please report current status and wrap up if possible.");
|
|
69
|
-
else if (request.operation === "resume") await session.prompt?.(request.message ?? "Please resume and report final status.", { source: "api", expandPromptTemplates: false });
|
|
70
|
-
else if (request.operation === "stop") await session.abort?.();
|
|
71
|
-
return true;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
export async function applyLiveAgentControlRequests(input: { manifest: TeamRunManifest; taskId: string; agentId: string; session: { steer?: (text: string) => Promise<void>; prompt?: (text: string, options?: Record<string, unknown>) => Promise<void>; abort?: () => Promise<void> | void }; cursor: LiveAgentControlCursor; seenRequestIds?: Set<string> }): Promise<LiveAgentControlCursor> {
|
|
75
|
-
const batch = readLiveAgentControlRequests(input.manifest, input.taskId, input.cursor);
|
|
76
|
-
for (const request of batch.requests) await applyLiveAgentControlRequest({ request, taskId: input.taskId, agentId: input.agentId, session: input.session, seenRequestIds: input.seenRequestIds });
|
|
77
|
-
return batch.cursor;
|
|
78
|
-
}
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
import type { TeamRunManifest } from "../state/types.ts";
|
|
4
|
+
|
|
5
|
+
export type LiveAgentControlOperation = "steer" | "stop" | "resume";
|
|
6
|
+
|
|
7
|
+
export interface LiveAgentControlRequest {
|
|
8
|
+
id: string;
|
|
9
|
+
runId: string;
|
|
10
|
+
taskId: string;
|
|
11
|
+
agentId?: string;
|
|
12
|
+
operation: LiveAgentControlOperation;
|
|
13
|
+
message?: string;
|
|
14
|
+
createdAt: string;
|
|
15
|
+
processedAt?: string;
|
|
16
|
+
error?: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface LiveAgentControlCursor {
|
|
20
|
+
offset: number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function liveAgentControlPath(manifest: TeamRunManifest, taskId: string): string {
|
|
24
|
+
return path.join(manifest.stateRoot, "agents", taskId, "live-control.jsonl");
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function requestId(): string {
|
|
28
|
+
return `ctrl_${Date.now().toString(36)}_${Math.random().toString(16).slice(2, 10)}`;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function appendLiveAgentControlRequest(manifest: TeamRunManifest, input: { taskId: string; agentId?: string; operation: LiveAgentControlOperation; message?: string }): LiveAgentControlRequest {
|
|
32
|
+
const request: LiveAgentControlRequest = {
|
|
33
|
+
id: requestId(),
|
|
34
|
+
runId: manifest.runId,
|
|
35
|
+
taskId: input.taskId,
|
|
36
|
+
agentId: input.agentId,
|
|
37
|
+
operation: input.operation,
|
|
38
|
+
message: input.message,
|
|
39
|
+
createdAt: new Date().toISOString(),
|
|
40
|
+
};
|
|
41
|
+
const filePath = liveAgentControlPath(manifest, input.taskId);
|
|
42
|
+
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
43
|
+
fs.appendFileSync(filePath, `${JSON.stringify(request)}\n`, "utf-8");
|
|
44
|
+
return request;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function readLiveAgentControlRequests(manifest: TeamRunManifest, taskId: string, cursor: LiveAgentControlCursor = { offset: 0 }): { requests: LiveAgentControlRequest[]; cursor: LiveAgentControlCursor } {
|
|
48
|
+
const filePath = liveAgentControlPath(manifest, taskId);
|
|
49
|
+
if (!fs.existsSync(filePath)) return { requests: [], cursor };
|
|
50
|
+
const text = fs.readFileSync(filePath, "utf-8");
|
|
51
|
+
const lines = text.split(/\r?\n/).filter(Boolean);
|
|
52
|
+
const requests = lines.slice(cursor.offset).flatMap((line) => {
|
|
53
|
+
try {
|
|
54
|
+
const parsed = JSON.parse(line) as LiveAgentControlRequest;
|
|
55
|
+
return parsed && parsed.runId === manifest.runId && parsed.taskId === taskId ? [parsed] : [];
|
|
56
|
+
} catch {
|
|
57
|
+
return [];
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
return { requests, cursor: { offset: lines.length } };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export async function applyLiveAgentControlRequest(input: { request: LiveAgentControlRequest; taskId: string; agentId: string; session: { steer?: (text: string) => Promise<void>; prompt?: (text: string, options?: Record<string, unknown>) => Promise<void>; abort?: () => Promise<void> | void }; seenRequestIds?: Set<string> }): Promise<boolean> {
|
|
64
|
+
const { request, taskId, agentId, session, seenRequestIds } = input;
|
|
65
|
+
if (seenRequestIds?.has(request.id)) return false;
|
|
66
|
+
if (request.agentId && request.agentId !== agentId && request.agentId !== taskId) return false;
|
|
67
|
+
seenRequestIds?.add(request.id);
|
|
68
|
+
if (request.operation === "steer") await session.steer?.(request.message ?? "Please report current status and wrap up if possible.");
|
|
69
|
+
else if (request.operation === "resume") await session.prompt?.(request.message ?? "Please resume and report final status.", { source: "api", expandPromptTemplates: false });
|
|
70
|
+
else if (request.operation === "stop") await session.abort?.();
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export async function applyLiveAgentControlRequests(input: { manifest: TeamRunManifest; taskId: string; agentId: string; session: { steer?: (text: string) => Promise<void>; prompt?: (text: string, options?: Record<string, unknown>) => Promise<void>; abort?: () => Promise<void> | void }; cursor: LiveAgentControlCursor; seenRequestIds?: Set<string> }): Promise<LiveAgentControlCursor> {
|
|
75
|
+
const batch = readLiveAgentControlRequests(input.manifest, input.taskId, input.cursor);
|
|
76
|
+
for (const request of batch.requests) await applyLiveAgentControlRequest({ request, taskId: input.taskId, agentId: input.agentId, session: input.session, seenRequestIds: input.seenRequestIds });
|
|
77
|
+
return batch.cursor;
|
|
78
|
+
}
|