pi-better-harness 0.30.1 → 0.31.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.
|
@@ -57,6 +57,15 @@ Invoking the alias (`/skill:resolve-issues`, or `/goal /skill:resolve-issues tas
|
|
|
57
57
|
|
|
58
58
|
After every successful context compaction (Pi's `session_compact` event) while a coordinator owns the session, the goal extension re-injects the skill so the model sees it before its next step; the compaction summary keeps run state but not the skill's rules, and the summary itself is left unchanged. If the skill directory ships `scripts/rehydrate.mjs` (checked first) or `rehydrate.mjs`, it runs as `node <script> --cwd <session cwd>` (no shell, 10-second timeout, stderr ignored) and its trimmed stdout becomes the message; empty output injects nothing, so the script decides whether a run is in progress. Without a script, or when it fails or times out, the message is: "Your context was just compacted while you coordinate the `<name>` workflow. Before your next step, read `<SKILL.md path>` in full, with no line limit." The message is a `workflow-rehydrate` custom message: inside a running agent loop (or before an overflow retry) it steers the next model call; otherwise it is appended without starting a turn.
|
|
59
59
|
|
|
60
|
+
### Scheduler-backed workflow gates
|
|
61
|
+
|
|
62
|
+
A coordinator that ships the resolve-issues scheduler (`scripts/next-actions.mjs` in its skill directory, or in a sibling `resolve-issues` skill) keeps its Goal from ending while the run still has work. The extension runs `node next-actions.mjs --run <run dir> --slots 1 --peek --json` (read-only, 20-second timeout), where the run directory is the folder of the task plan bound through `update_plan`. Every gate fails open: if the scheduler is missing, errors, times out, prints bad output, or no plan is bound yet, the call proceeds and its result carries a warning, so nothing deadlocks.
|
|
63
|
+
|
|
64
|
+
- **Completion.** `update_goal` complete is refused, with each due action (action, unit, first 200 characters of detail), unless the scheduler reports `finish` and nothing else is due (an empty due list means workers are still running, so it is refused too). The Goal stays active.
|
|
65
|
+
- **Pause.** `release_workflow` is refused unless every unfinished unit is quiet (nothing due for it) or blocked `human-decision` with `humanItem` 2-5. A unit blocked `retry-ci` or any other reason the run retries itself, or a `human-decision` item 1, is named in the refusal and the Goal stays active.
|
|
66
|
+
- **Auto-bind.** Invoking `/skill:rush-issues` or `/skill:resolve-issues` with no active goal (none, or a completed one) creates and activates a Goal whose objective is the invocation text, exactly as `/goal <invocation>` would, without sending the command a second time. A paused or active Goal is left alone.
|
|
67
|
+
- **Wait timer.** When the scheduler's only due action is `wait` with a "next automatic attempt is at <ISO time>" detail, the next automatic continuation is scheduled for that time (at most the maximum timer delay, at least 5 seconds) and the wake-up is not counted as a no-progress retry. Other due work, or an unparseable time, uses the ordinary retry schedule. `autoContinue` off disables this timer.
|
|
68
|
+
|
|
60
69
|
When `pi-better-plan` is installed, it defers its prompt, checklist, and `update_plan` tool to a skill-owned task plan. Skills without this metadata retain normal goal and plan behavior. The skill itself owns its planning format and worker policy; the harness does not enumerate skills or impose a shared workflow schema. The former `pi-better-plan-workflow: coordinator` metadata remains supported for installed skills.
|
|
61
70
|
|
|
62
71
|
## Install
|
|
@@ -40,6 +40,7 @@ import {
|
|
|
40
40
|
import { createRenderScheduler } from "./shared-render-scheduler.js";
|
|
41
41
|
import { collectSubagentActivity } from "./subagents.js";
|
|
42
42
|
import { currentWorkflowOwner, skillCommandName, workflowEntry, workflowOwnerFromSkill, WORKFLOW_ENTRY_TYPE } from "./workflow.js";
|
|
43
|
+
import { completionGate, pauseGate, schedulerWaitUntil } from "./workflow-gates.js";
|
|
43
44
|
import { workflowRehydrateMessage, WORKFLOW_REHYDRATE_MESSAGE_TYPE } from "./rehydrate.js";
|
|
44
45
|
import {
|
|
45
46
|
EVENT_ACTIVITY,
|
|
@@ -58,6 +59,10 @@ const POLL_INTERVAL_MS = 2_000;
|
|
|
58
59
|
const DEFAULT_IDLE_CONTINUATION_DELAY_MS = 60_000;
|
|
59
60
|
const DEFAULT_MAX_NO_PROGRESS_RETRIES = 10;
|
|
60
61
|
const MAX_TIMER_DELAY_MS = 2_147_483_647;
|
|
62
|
+
/** Never re-poll a workflow's scheduler sooner than this, even when its stated attempt time has passed. */
|
|
63
|
+
const MIN_WAIT_DELAY_MS = 5_000;
|
|
64
|
+
/** Workflow skills whose invocation binds a goal when none is active, so the run cannot stop silently. */
|
|
65
|
+
const AUTO_BIND_SKILLS = new Set(["rush-issues", "resolve-issues"]);
|
|
61
66
|
const WAKE_DISABLED =
|
|
62
67
|
process.env.PI_BETTER_GOAL_DISABLE_WAKE === "1" ||
|
|
63
68
|
process.env.PI_BETTER_EXTENSION_DISABLE_WAKE === "1";
|
|
@@ -477,6 +482,21 @@ export default function (pi: ExtensionAPI): void {
|
|
|
477
482
|
? workflowOwnerFromSkill(goal.command.name.slice("skill:".length), goal.command.path, registeredSkillPath)
|
|
478
483
|
: null;
|
|
479
484
|
|
|
485
|
+
/**
|
|
486
|
+
* The session's workflow owner when an active goal is bound to it through its skill command, else
|
|
487
|
+
* null. Only these goals are gated by the workflow's scheduler.
|
|
488
|
+
*/
|
|
489
|
+
const boundWorkflow = (goal: GoalSnapshot, ctx: ExtensionContext): NonNullable<ReturnType<typeof currentWorkflowOwner>> | null => {
|
|
490
|
+
const owner = getWorkflow(ctx);
|
|
491
|
+
if (!owner || goal.status !== "active" || goal.command?.source !== "skill") return null;
|
|
492
|
+
try {
|
|
493
|
+
const bound = goalWorkflowOwner(goal);
|
|
494
|
+
return bound?.name === owner.name && bound.path === owner.path ? owner : null;
|
|
495
|
+
} catch {
|
|
496
|
+
return null;
|
|
497
|
+
}
|
|
498
|
+
};
|
|
499
|
+
|
|
480
500
|
/**
|
|
481
501
|
* The one resume path shared by `/goal resume`, the hotkey, and `goal_resume`:
|
|
482
502
|
* reactivate a paused goal or reopen an active no-progress hold.
|
|
@@ -568,6 +588,8 @@ export default function (pi: ExtensionAPI): void {
|
|
|
568
588
|
ctx: ExtensionContext,
|
|
569
589
|
kind: "continuation" | "background-drained",
|
|
570
590
|
snapshot?: ActivitySnapshot,
|
|
591
|
+
/** Fixed delay for a workflow scheduler's wait; replaces any pending timer and ignores no-progress backoff. */
|
|
592
|
+
waitDelayMs?: number,
|
|
571
593
|
): void => {
|
|
572
594
|
if (WAKE_DISABLED || !preferences.autoContinue || !isPokeable(goal) || continuationQueuedFor === goal.goalId) {
|
|
573
595
|
return;
|
|
@@ -576,8 +598,8 @@ export default function (pi: ExtensionAPI): void {
|
|
|
576
598
|
if (currentContinuationState(ctx, goal.goalId)?.blocked) {
|
|
577
599
|
return;
|
|
578
600
|
}
|
|
579
|
-
const signature = `${goal.goalId}:${kind}`;
|
|
580
|
-
if (idleContinuationTimer && (idleContinuationSignature === signature ||
|
|
601
|
+
const signature = `${goal.goalId}:${waitDelayMs === undefined ? kind : "wait"}`;
|
|
602
|
+
if (waitDelayMs === undefined && idleContinuationTimer && (idleContinuationSignature === signature ||
|
|
581
603
|
(kind === "continuation" && idleContinuationSignature === `${goal.goalId}:background-drained`))) {
|
|
582
604
|
return;
|
|
583
605
|
}
|
|
@@ -587,7 +609,7 @@ export default function (pi: ExtensionAPI): void {
|
|
|
587
609
|
idleContinuationTimer = undefined;
|
|
588
610
|
idleContinuationSignature = "";
|
|
589
611
|
void sendIdleContinuationAfterAudit(goal.goalId, ctx, kind, snapshot);
|
|
590
|
-
}, idleContinuationDelay(kind === "continuation" ? currentContinuationState(ctx, goal.goalId)?.noProgressRetries ?? 0 : 0));
|
|
612
|
+
}, waitDelayMs ?? idleContinuationDelay(kind === "continuation" ? currentContinuationState(ctx, goal.goalId)?.noProgressRetries ?? 0 : 0));
|
|
591
613
|
idleContinuationTimer.unref?.();
|
|
592
614
|
};
|
|
593
615
|
|
|
@@ -626,6 +648,8 @@ export default function (pi: ExtensionAPI): void {
|
|
|
626
648
|
tokenBudget: number | null,
|
|
627
649
|
ctx: ExtensionContext,
|
|
628
650
|
source: "command",
|
|
651
|
+
/** False when the user's own message already invoked the command, so the goal must not send it again. */
|
|
652
|
+
dispatch = true,
|
|
629
653
|
): GoalSnapshot => {
|
|
630
654
|
const objectiveError = validateObjective(objective);
|
|
631
655
|
if (objectiveError) {
|
|
@@ -647,8 +671,8 @@ export default function (pi: ExtensionAPI): void {
|
|
|
647
671
|
setGoal(goal, ctx, source);
|
|
648
672
|
if (command) {
|
|
649
673
|
if (owner) recordWorkflow(owner);
|
|
650
|
-
pi.sendUserMessage(commandInvocation(command), { deliverAs: "followUp", expandPromptTemplates: true });
|
|
651
|
-
if (command.source === "extension") scheduleIdleContinuation(goal, ctx, "continuation");
|
|
674
|
+
if (dispatch) pi.sendUserMessage(commandInvocation(command), { deliverAs: "followUp", expandPromptTemplates: true });
|
|
675
|
+
if (dispatch && command.source === "extension") scheduleIdleContinuation(goal, ctx, "continuation");
|
|
652
676
|
} else {
|
|
653
677
|
queueGoalContinuation(goal, ctx);
|
|
654
678
|
}
|
|
@@ -1049,12 +1073,21 @@ export default function (pi: ExtensionAPI): void {
|
|
|
1049
1073
|
details: { ok: true, goal: current },
|
|
1050
1074
|
};
|
|
1051
1075
|
}
|
|
1076
|
+
const gated = boundWorkflow(current, ctx);
|
|
1077
|
+
const verdict = gated ? await completionGate(ctx.sessionManager.getBranch(), gated) : null;
|
|
1078
|
+
if (verdict && !verdict.allow) {
|
|
1079
|
+
return {
|
|
1080
|
+
content: [{ type: "text", text: verdict.message }],
|
|
1081
|
+
details: { ok: false, goal: current, due: verdict.due },
|
|
1082
|
+
};
|
|
1083
|
+
}
|
|
1052
1084
|
const goal = goalWithStatus(current, "complete");
|
|
1053
1085
|
setGoal(goal, ctx, "tool");
|
|
1054
1086
|
if (getWorkflow(ctx)) recordWorkflow(null);
|
|
1087
|
+
const warning = verdict?.allow ? verdict.warning : undefined;
|
|
1055
1088
|
return {
|
|
1056
|
-
content: [{ type: "text", text: "Goal marked complete." }],
|
|
1057
|
-
details: { ok: true, goal },
|
|
1089
|
+
content: [{ type: "text", text: warning ? `Goal marked complete. ${warning}` : "Goal marked complete." }],
|
|
1090
|
+
details: { ok: true, goal, ...(warning ? { warning } : {}) },
|
|
1058
1091
|
};
|
|
1059
1092
|
},
|
|
1060
1093
|
});
|
|
@@ -1135,6 +1168,12 @@ export default function (pi: ExtensionAPI): void {
|
|
|
1135
1168
|
const owner = getWorkflow(ctx);
|
|
1136
1169
|
if (!owner) return { content: [{ type: "text", text: "No workflow owns this session." }], details: { released: false } };
|
|
1137
1170
|
const goal = getGoal(ctx);
|
|
1171
|
+
const gated = goal ? boundWorkflow(goal, ctx) : null;
|
|
1172
|
+
const verdict = gated ? await pauseGate(ctx.sessionManager.getBranch(), gated) : null;
|
|
1173
|
+
if (verdict && !verdict.allow) {
|
|
1174
|
+
return { content: [{ type: "text", text: verdict.message }], details: { released: false, goalPaused: false } };
|
|
1175
|
+
}
|
|
1176
|
+
const warning = verdict?.allow ? verdict.warning : undefined;
|
|
1138
1177
|
let resumeCommand: string | undefined;
|
|
1139
1178
|
if (goal?.status === "active" && goal.command?.source === "skill") {
|
|
1140
1179
|
const boundOwner = workflowOwnerFromSkill(goal.command.name.slice("skill:".length), goal.command.path, registeredSkillPath);
|
|
@@ -1145,7 +1184,7 @@ export default function (pi: ExtensionAPI): void {
|
|
|
1145
1184
|
}
|
|
1146
1185
|
recordWorkflow(null);
|
|
1147
1186
|
return {
|
|
1148
|
-
content: [{ type: "text", text: `Released ${owner.name} workflow ownership.${resumeCommand ? ` Its Goal is paused, not complete; the user's go-ahead or /goal resume rebinds /${resumeCommand}.` : ""}` }],
|
|
1187
|
+
content: [{ type: "text", text: `Released ${owner.name} workflow ownership.${resumeCommand ? ` Its Goal is paused, not complete; the user's go-ahead or /goal resume rebinds /${resumeCommand}.` : ""}${warning ? ` ${warning}` : ""}` }],
|
|
1149
1188
|
details: { released: true, owner: owner.name, goalPaused: resumeCommand !== undefined },
|
|
1150
1189
|
};
|
|
1151
1190
|
},
|
|
@@ -1242,7 +1281,21 @@ export default function (pi: ExtensionAPI): void {
|
|
|
1242
1281
|
if (command) {
|
|
1243
1282
|
try {
|
|
1244
1283
|
const owner = workflowOwnerFromSkill(skillName, command.sourceInfo.path, registeredSkillPath);
|
|
1245
|
-
if (owner)
|
|
1284
|
+
if (owner) {
|
|
1285
|
+
// Without a goal nothing re-prompts the coordinator when it stops early, so bind one now
|
|
1286
|
+
// exactly as `/goal <this invocation>` would. The user's message already runs the skill.
|
|
1287
|
+
const existing = getGoal(ctx);
|
|
1288
|
+
if (AUTO_BIND_SKILLS.has(skillName) && (!existing || existing.status === "complete")) {
|
|
1289
|
+
try {
|
|
1290
|
+
startOrReplaceGoal(event.text.trim(), null, ctx, "command", false);
|
|
1291
|
+
} catch (error) {
|
|
1292
|
+
recordWorkflow(owner);
|
|
1293
|
+
notifyGoal(ctx, `Could not bind a goal to /skill:${skillName}: ${error instanceof Error ? error.message : String(error)}`, "warning");
|
|
1294
|
+
}
|
|
1295
|
+
} else {
|
|
1296
|
+
recordWorkflow(owner);
|
|
1297
|
+
}
|
|
1298
|
+
}
|
|
1246
1299
|
} catch (error) {
|
|
1247
1300
|
notifyGoal(ctx, error instanceof Error ? error.message : String(error), "error");
|
|
1248
1301
|
return { action: "handled" as const };
|
|
@@ -1450,6 +1503,21 @@ export default function (pi: ExtensionAPI): void {
|
|
|
1450
1503
|
if (!isPokeable(goal) || snapshot.backgroundRunning) {
|
|
1451
1504
|
return;
|
|
1452
1505
|
}
|
|
1506
|
+
// A run the scheduler says is only waiting (a retry is due at a known time) is not stuck: wake at
|
|
1507
|
+
// that time and leave the no-progress count alone, since nothing the model could do changes it.
|
|
1508
|
+
const waitOwner = preferences.autoContinue && !WAKE_DISABLED ? boundWorkflow(goal, ctx) : null;
|
|
1509
|
+
if (waitOwner) {
|
|
1510
|
+
const execution = executionGeneration;
|
|
1511
|
+
const waitUntil = await schedulerWaitUntil(ctx.sessionManager.getBranch(), waitOwner);
|
|
1512
|
+
if (execution !== executionGeneration) return;
|
|
1513
|
+
if (waitUntil !== undefined) {
|
|
1514
|
+
resetContinuationState(goal);
|
|
1515
|
+
lastAgentEvidence = null;
|
|
1516
|
+
scheduleIdleContinuation(goal, ctx, "continuation", snapshot,
|
|
1517
|
+
Math.min(MAX_TIMER_DELAY_MS, Math.max(MIN_WAIT_DELAY_MS, waitUntil - Date.now())));
|
|
1518
|
+
return;
|
|
1519
|
+
}
|
|
1520
|
+
}
|
|
1453
1521
|
const evidence = lastAgentEvidence;
|
|
1454
1522
|
if (!evidence) {
|
|
1455
1523
|
scheduleIdleContinuation(goal, ctx, "continuation", snapshot);
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { readFileSync, statSync } from "node:fs";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
|
+
|
|
5
|
+
import type { WorkflowOwner } from "./workflow.js";
|
|
6
|
+
|
|
7
|
+
/** Branch entry the plan extension writes when it binds a workflow-owned task plan. */
|
|
8
|
+
const WORKFLOW_PLAN_ENTRY = "pi-better-workflow-plan";
|
|
9
|
+
const WORKFLOW_ENTRY = "pi-better-workflow";
|
|
10
|
+
export const SCHEDULER_TIMEOUT_MS = 20_000;
|
|
11
|
+
const DETAIL_LIMIT = 200;
|
|
12
|
+
|
|
13
|
+
/** Scheduler locations relative to the owner's skill directory, in precedence order. */
|
|
14
|
+
const SCHEDULER_SCRIPTS = [
|
|
15
|
+
join("scripts", "next-actions.mjs"),
|
|
16
|
+
join("..", "resolve-issues", "scripts", "next-actions.mjs"),
|
|
17
|
+
];
|
|
18
|
+
|
|
19
|
+
/** Units in these states are done; every other unit is unfinished work. */
|
|
20
|
+
const FINISHED_STATUSES = new Set(["succeeded", "cancelled", "canceled"]);
|
|
21
|
+
/** `humanItem` values 2-5 are genuine holds only a human can lift; item 1 is a decision the run takes itself. */
|
|
22
|
+
const HUMAN_HOLD_ITEMS = new Set([2, 3, 4, 5]);
|
|
23
|
+
|
|
24
|
+
export interface SchedulerAction {
|
|
25
|
+
action: string;
|
|
26
|
+
unit: string | null;
|
|
27
|
+
detail: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export type SchedulerRun =
|
|
31
|
+
| { ok: true; due: SchedulerAction[] }
|
|
32
|
+
| { ok: false; reason: string };
|
|
33
|
+
|
|
34
|
+
/** A gate either lets the call through (optionally with a warning when it could not check) or refuses it. */
|
|
35
|
+
export type GateVerdict =
|
|
36
|
+
| { allow: true; warning?: string }
|
|
37
|
+
| { allow: false; message: string; due?: SchedulerAction[] };
|
|
38
|
+
|
|
39
|
+
interface EntryLike {
|
|
40
|
+
type: string;
|
|
41
|
+
customType?: string;
|
|
42
|
+
data?: unknown;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
46
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** The first next-actions scheduler the owner's skill ships, or undefined when it ships none. */
|
|
50
|
+
export function findScheduler(owner: WorkflowOwner): string | undefined {
|
|
51
|
+
const skillDir = dirname(owner.path);
|
|
52
|
+
for (const relative of SCHEDULER_SCRIPTS) {
|
|
53
|
+
const candidate = join(skillDir, relative);
|
|
54
|
+
try {
|
|
55
|
+
if (statSync(candidate).isFile()) return candidate;
|
|
56
|
+
} catch {
|
|
57
|
+
// Missing or unreadable: try the next location.
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return undefined;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* The task-plan.json the session bound for `owner`, or undefined before a plan is bound.
|
|
65
|
+
* Invoking a workflow skill again starts a new run, so a later owner entry drops the old binding.
|
|
66
|
+
*/
|
|
67
|
+
export function boundPlanPath(entries: Iterable<EntryLike>, owner: WorkflowOwner): string | undefined {
|
|
68
|
+
let path: string | undefined;
|
|
69
|
+
for (const entry of entries) {
|
|
70
|
+
if (entry.type !== "custom") continue;
|
|
71
|
+
const data = entry.data;
|
|
72
|
+
if (entry.customType === WORKFLOW_ENTRY && isRecord(data) && data.version === 1 && data.kind === "set") path = undefined;
|
|
73
|
+
if (entry.customType !== WORKFLOW_PLAN_ENTRY || !isRecord(data) || data.version !== 1) continue;
|
|
74
|
+
if (data.kind === "clear") path = undefined;
|
|
75
|
+
else if (data.kind === "set" && data.owner === owner.name && typeof data.path === "string") path = data.path;
|
|
76
|
+
}
|
|
77
|
+
return path;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function parseSchedulerOutput(stdout: string): SchedulerRun {
|
|
81
|
+
let parsed: unknown;
|
|
82
|
+
try {
|
|
83
|
+
parsed = JSON.parse(stdout);
|
|
84
|
+
} catch {
|
|
85
|
+
return { ok: false, reason: "the scheduler printed output that is not JSON" };
|
|
86
|
+
}
|
|
87
|
+
if (!isRecord(parsed) || !Array.isArray(parsed.due)) return { ok: false, reason: "the scheduler output has no due list" };
|
|
88
|
+
const due = parsed.due.flatMap((item): SchedulerAction[] => isRecord(item) && typeof item.action === "string"
|
|
89
|
+
? [{
|
|
90
|
+
action: item.action,
|
|
91
|
+
unit: item.unit === null || item.unit === undefined ? null : String(item.unit),
|
|
92
|
+
detail: typeof item.detail === "string" ? item.detail : "",
|
|
93
|
+
}]
|
|
94
|
+
: []);
|
|
95
|
+
return { ok: true, due };
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Runs the scheduler read-only (`--peek`) for a run directory. The scheduler exits 1 when actions
|
|
100
|
+
* are due and 2 on a usage or plan error, so only the latter, a timeout, or bad output is a failure.
|
|
101
|
+
*/
|
|
102
|
+
export function runScheduler(script: string, runDir: string, timeoutMs = SCHEDULER_TIMEOUT_MS): Promise<SchedulerRun> {
|
|
103
|
+
return new Promise((resolve) => {
|
|
104
|
+
execFile("node", [script, "--run", runDir, "--slots", "1", "--peek", "--json"],
|
|
105
|
+
{ cwd: runDir, timeout: timeoutMs, encoding: "utf8", windowsHide: true, maxBuffer: 8 * 1024 * 1024 },
|
|
106
|
+
(error, stdout, stderr) => {
|
|
107
|
+
if (error) {
|
|
108
|
+
// An exit status of 1 means "actions due"; any other failure (exit 2, timeout, spawn error) is real.
|
|
109
|
+
if ((error as { code?: unknown }).code !== 1) {
|
|
110
|
+
const detail = String(stderr).trim().split("\n")[0] || error.message;
|
|
111
|
+
resolve({ ok: false, reason: `the scheduler failed (${detail.slice(0, DETAIL_LIMIT)})` });
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
resolve(parseSchedulerOutput(stdout));
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
interface PlannedRun {
|
|
121
|
+
runDir: string;
|
|
122
|
+
script: string;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function locateRun(entries: Iterable<EntryLike>, owner: WorkflowOwner): PlannedRun | { warning: string } {
|
|
126
|
+
const script = findScheduler(owner);
|
|
127
|
+
if (!script) return { warning: `${owner.name} ships no scheduler, so the run was not checked` };
|
|
128
|
+
const plan = boundPlanPath(entries, owner);
|
|
129
|
+
if (!plan) return { warning: `no task plan is bound for ${owner.name} yet, so the run was not checked` };
|
|
130
|
+
return { runDir: dirname(plan), script };
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function unchecked(warning: string): GateVerdict {
|
|
134
|
+
return { allow: true, warning: `Warning: ${warning}. The gate is open so nothing deadlocks.` };
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function describe(action: SchedulerAction): string {
|
|
138
|
+
const detail = action.detail.length > DETAIL_LIMIT ? `${action.detail.slice(0, DETAIL_LIMIT)}...` : action.detail;
|
|
139
|
+
return `- ${action.action}${action.unit ? ` (unit ${action.unit})` : ""}: ${detail}`;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** Completion is allowed only when the scheduler reports `finish` and nothing else is due. */
|
|
143
|
+
export async function completionGate(
|
|
144
|
+
entries: Iterable<EntryLike>,
|
|
145
|
+
owner: WorkflowOwner,
|
|
146
|
+
options: { timeoutMs?: number } = {},
|
|
147
|
+
): Promise<GateVerdict> {
|
|
148
|
+
const located = locateRun(entries, owner);
|
|
149
|
+
if ("warning" in located) return unchecked(located.warning);
|
|
150
|
+
const run = await runScheduler(located.script, located.runDir, options.timeoutMs);
|
|
151
|
+
if (!run.ok) return unchecked(`could not check the ${owner.name} run (${run.reason})`);
|
|
152
|
+
const open = run.due.filter((action) => action.action !== "finish");
|
|
153
|
+
if (open.length === 0 && run.due.length > 0) return { allow: true };
|
|
154
|
+
if (open.length === 0) {
|
|
155
|
+
// Nothing due means workers are still running, not that the run is done.
|
|
156
|
+
return {
|
|
157
|
+
allow: false,
|
|
158
|
+
due: [],
|
|
159
|
+
message: `Goal not marked complete: the ${owner.name} run's workers are still running; the scheduler has not reported finish. ` +
|
|
160
|
+
"Wait for them, then call update_goal again once the scheduler reports finish.",
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
return {
|
|
164
|
+
allow: false,
|
|
165
|
+
due: open,
|
|
166
|
+
message: `Goal not marked complete: the ${owner.name} scheduler still has work due, so the run is not finished. ` +
|
|
167
|
+
`Do the due actions, then call update_goal again once the scheduler reports finish.\n${open.map(describe).join("\n")}`,
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function readUnits(runDir: string): Array<Record<string, unknown>> | null {
|
|
172
|
+
try {
|
|
173
|
+
const plan: unknown = JSON.parse(readFileSync(join(runDir, "task-plan.json"), "utf8"));
|
|
174
|
+
return isRecord(plan) && Array.isArray(plan.units) ? plan.units.filter(isRecord) : null;
|
|
175
|
+
} catch {
|
|
176
|
+
return null;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Releasing (pausing the goal) is allowed only for genuine human holds. Every unfinished unit must be
|
|
182
|
+
* either quiet (the scheduler has nothing due for it) or blocked on human-decision item 2-5.
|
|
183
|
+
*/
|
|
184
|
+
export async function pauseGate(
|
|
185
|
+
entries: Iterable<EntryLike>,
|
|
186
|
+
owner: WorkflowOwner,
|
|
187
|
+
options: { timeoutMs?: number } = {},
|
|
188
|
+
): Promise<GateVerdict> {
|
|
189
|
+
const located = locateRun(entries, owner);
|
|
190
|
+
if ("warning" in located) return unchecked(located.warning);
|
|
191
|
+
const units = readUnits(located.runDir);
|
|
192
|
+
if (!units) return unchecked(`could not read the ${owner.name} task plan`);
|
|
193
|
+
const run = await runScheduler(located.script, located.runDir, options.timeoutMs);
|
|
194
|
+
if (!run.ok) return unchecked(`could not check the ${owner.name} run (${run.reason})`);
|
|
195
|
+
|
|
196
|
+
const refusals: string[] = [];
|
|
197
|
+
const unfinished = new Set<string>();
|
|
198
|
+
for (const unit of units) {
|
|
199
|
+
const id = String(unit.id);
|
|
200
|
+
const status = typeof unit.status === "string" ? unit.status : "";
|
|
201
|
+
if (FINISHED_STATUSES.has(status)) continue;
|
|
202
|
+
unfinished.add(id);
|
|
203
|
+
const due = run.due.filter((action) => action.unit === id && action.action !== "finish");
|
|
204
|
+
if (due.length === 0) continue;
|
|
205
|
+
const humanHold = status === "blocked" && unit.blockedReason === "human-decision" &&
|
|
206
|
+
typeof unit.humanItem === "number" && HUMAN_HOLD_ITEMS.has(unit.humanItem);
|
|
207
|
+
if (humanHold) continue;
|
|
208
|
+
refusals.push(status === "blocked" && typeof unit.blockedReason === "string"
|
|
209
|
+
? unit.blockedReason === "human-decision"
|
|
210
|
+
? `unit ${id} is blocked human-decision${unit.humanItem === undefined ? "" : ` item ${String(unit.humanItem)}`}; only items 2-5 are genuine holds, so decide it yourself (${due[0]!.action})`
|
|
211
|
+
: `unit ${id} is blocked ${unit.blockedReason}; the run retries it itself (${due[0]!.action})`
|
|
212
|
+
: `unit ${id} is ${status || "unfinished"} with ${due[0]!.action} due`);
|
|
213
|
+
}
|
|
214
|
+
for (const action of run.due) {
|
|
215
|
+
if (action.action !== "finish" && (action.unit === null || !unfinished.has(action.unit))) {
|
|
216
|
+
refusals.push(`${action.unit ? `unit ${action.unit}` : "the run"} has ${action.action} due: ${action.detail.slice(0, DETAIL_LIMIT)}`);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
if (refusals.length === 0) return { allow: true };
|
|
220
|
+
return {
|
|
221
|
+
allow: false,
|
|
222
|
+
message: `Workflow not released: the ${owner.name} run still has work it can do itself, so the goal stays active.\n` +
|
|
223
|
+
refusals.map((line) => `- ${line}`).join("\n"),
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** The instant of the scheduler's `wait` heartbeat, when it is the only thing due. */
|
|
228
|
+
export function waitUntil(due: readonly SchedulerAction[]): number | undefined {
|
|
229
|
+
if (due.length === 0 || due.some((action) => action.action !== "wait")) return undefined;
|
|
230
|
+
const times = due.map((action) => {
|
|
231
|
+
const match = /next automatic attempt is at (\d{4}-\d{2}-\d{2}T[0-9:.]+Z?)/.exec(action.detail);
|
|
232
|
+
return match ? Date.parse(match[1]!) : Number.NaN;
|
|
233
|
+
}).filter(Number.isFinite);
|
|
234
|
+
return times.length > 0 ? Math.min(...times) : undefined;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/** When the bound run next wants a continuation, or undefined when it is not only waiting. */
|
|
238
|
+
export async function schedulerWaitUntil(
|
|
239
|
+
entries: Iterable<EntryLike>,
|
|
240
|
+
owner: WorkflowOwner,
|
|
241
|
+
options: { timeoutMs?: number } = {},
|
|
242
|
+
): Promise<number | undefined> {
|
|
243
|
+
const located = locateRun(entries, owner);
|
|
244
|
+
if ("warning" in located) return undefined;
|
|
245
|
+
const run = await runScheduler(located.script, located.runDir, options.timeoutMs);
|
|
246
|
+
return run.ok ? waitUntil(run.due) : undefined;
|
|
247
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-better-harness",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.31.0",
|
|
4
4
|
"description": "Pi extension bundle for a write sandbox, subagents, background tasks, SSH, goals, and structured plans.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -54,7 +54,7 @@
|
|
|
54
54
|
},
|
|
55
55
|
"dependencies": {
|
|
56
56
|
"pi-better-background-tasks": "0.9.1",
|
|
57
|
-
"pi-better-goal": "0.
|
|
57
|
+
"pi-better-goal": "0.10.0",
|
|
58
58
|
"pi-better-plan": "0.9.1",
|
|
59
59
|
"pi-better-sandbox": "0.12.1",
|
|
60
60
|
"pi-better-ssh": "0.1.3",
|