pi-better-goal 0.16.0 → 0.16.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-better-goal",
3
- "version": "0.16.0",
3
+ "version": "0.16.2",
4
4
  "description": "Pi extension for goal tracking with background-aware continuation.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -1,4 +1,3 @@
1
- import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
2
1
  import { existsSync } from "node:fs";
3
2
 
4
3
  export interface GoalCommandBinding {
@@ -8,12 +7,20 @@ export interface GoalCommandBinding {
8
7
  args: string;
9
8
  }
10
9
 
10
+ interface CommandRegistry {
11
+ getCommands(): readonly {
12
+ name: string;
13
+ source: GoalCommandBinding["source"];
14
+ sourceInfo: { path?: string | undefined };
15
+ }[];
16
+ }
17
+
11
18
  export function leadingCommand(text: string): { name: string; args: string } | null {
12
19
  const match = /^\/([^\s/]+)(?:\s+([\s\S]*))?$/.exec(text.trim());
13
20
  return match ? { name: match[1]!, args: match[2]?.trim() ?? "" } : null;
14
21
  }
15
22
 
16
- export function resolveGoalCommand(pi: ExtensionAPI, text: string): GoalCommandBinding | null {
23
+ export function resolveGoalCommand(pi: CommandRegistry, text: string): GoalCommandBinding | null {
17
24
  const parsed = leadingCommand(text);
18
25
  if (!parsed) return null;
19
26
  if (parsed.name === "goal") throw new Error("A goal cannot invoke /goal recursively.");
@@ -23,7 +30,7 @@ export function resolveGoalCommand(pi: ExtensionAPI, text: string): GoalCommandB
23
30
  return { name: command.name, source: command.source, path: command.sourceInfo.path, args: parsed.args };
24
31
  }
25
32
 
26
- export function commandAvailable(pi: ExtensionAPI, binding: GoalCommandBinding): boolean {
33
+ export function commandAvailable(pi: CommandRegistry, binding: GoalCommandBinding): boolean {
27
34
  return (binding.source === "extension" || existsSync(binding.path)) &&
28
35
  pi.getCommands().some((command) => command.name === binding.name &&
29
36
  command.source === binding.source && command.sourceInfo.path === binding.path);
package/src/index.ts CHANGED
@@ -368,8 +368,8 @@ export default function (pi: ExtensionAPI): void {
368
368
  return false;
369
369
  }
370
370
  };
371
- const recordWorkflow = (owner: ReturnType<typeof currentWorkflowOwner>): void => {
372
- pi.appendEntry(WORKFLOW_ENTRY_TYPE, workflowEntry(owner));
371
+ const recordWorkflow = (owner: ReturnType<typeof currentWorkflowOwner>, resumePlan = false): void => {
372
+ pi.appendEntry(WORKFLOW_ENTRY_TYPE, { ...workflowEntry(owner), ...(resumePlan && { resumePlan }) });
373
373
  pi.events.emit("pi-better-workflow:changed", owner);
374
374
  };
375
375
 
@@ -1008,14 +1008,14 @@ export default function (pi: ExtensionAPI): void {
1008
1008
  if (validateObjective(objective)) return;
1009
1009
  const owner = getWorkflow(ctx);
1010
1010
  if (owner && !workflowAvailable(owner)) return;
1011
- const retainedCommand = objective.startsWith("/") ? reopenedGoal?.command : undefined;
1011
+ const retainedCommand = reopenedGoal?.command;
1012
+ if (retainedCommand && !commandAvailable(pi, retainedCommand)) return;
1012
1013
  const command = retainedCommand ?? (owner ? resolveGoalCommand(pi, `/skill:${owner.name}`) : null);
1013
1014
  const goal = createGoalSnapshot(objective, null, undefined, command ?? undefined);
1015
+ const restoredOwner = retainedCommand ? goalWorkflowOwner(goal) : null;
1016
+ if (restoredOwner && owner && (restoredOwner.name !== owner.name || restoredOwner.path !== owner.path)) return;
1014
1017
  setGoal(goal, ctx, "runtime", true);
1015
- if (retainedCommand) {
1016
- const retainedOwner = goalWorkflowOwner(goal);
1017
- if (retainedOwner && (owner?.name !== retainedOwner.name || owner?.path !== retainedOwner.path)) recordWorkflow(retainedOwner);
1018
- }
1018
+ if (restoredOwner && !owner) recordWorkflow(restoredOwner, true);
1019
1019
  resetContinuationState(goal);
1020
1020
  });
1021
1021
 
@@ -1230,7 +1230,8 @@ export default function (pi: ExtensionAPI): void {
1230
1230
  }
1231
1231
  const audit = requestTaskContext(pi, "audit", current);
1232
1232
  const criteria = audit.doneWhen?.length ? audit.doneWhen : audit.objective ? [audit.objective] : [];
1233
- const missing = criteria.filter(criterion => !input.evidence?.some(item => item.criterion === criterion && item.result.trim()));
1233
+ const evidence = [...input.evidence ?? [], ...audit.evidence ?? []];
1234
+ const missing = criteria.filter(criterion => !evidence.some(item => item.criterion === criterion && item.result.trim()));
1234
1235
  if (audit.ledgerBlocker || audit.openSteps?.length || missing.length) {
1235
1236
  const message = [
1236
1237
  "Verify the requested outcome before completing the Goal.",
@@ -1,14 +1,11 @@
1
1
  // Generated from packages/harness-settings/index.ts. Do not edit directly.
2
2
  import { existsSync, lstatSync, realpathSync, readFileSync, mkdirSync, renameSync, rmSync, writeFileSync } from "node:fs";
3
3
  import { randomUUID } from "node:crypto";
4
- import { createRequire } from "node:module";
5
4
  import { join } from "node:path";
6
5
  import { getAgentDir } from "@earendil-works/pi-coding-agent";
6
+ // A static import lets the compiled Pi loader resolve dependencies of the settings lock.
7
+ import lockfile from "proper-lockfile";
7
8
 
8
- // Match Pi's settings lock without depending on a private SDK import.
9
- const lockfile = createRequire(import.meta.url)("proper-lockfile") as {
10
- lockSync(path: string, options: { realpath: boolean }): () => void;
11
- };
12
9
  const NAMESPACE = "piBetterHarness";
13
10
  export interface HarnessSettingsSeams { agentDir?: () => string }
14
11
 
@@ -40,6 +40,8 @@ export interface TaskContextRequest {
40
40
  action: TaskContextAction;
41
41
  doneWhen?: readonly string[];
42
42
  openSteps?: readonly string[];
43
+ /** Evidence retained by a finished workflow bound to this Goal. */
44
+ evidence?: readonly { criterion: string; result: string }[];
43
45
  ledgerBlocker?: string;
44
46
  goalId?: string;
45
47
  /** Current widget width on a visual ownership inspection. */
@@ -1,5 +1,6 @@
1
1
  import { execFile } from "node:child_process";
2
- import { cpSync, existsSync, mkdtempSync, readFileSync, readdirSync, rmSync, statSync } from "node:fs";
2
+ import { readFileSync, readdirSync, statSync } from "node:fs";
3
+ import { mkdir, mkdtemp, readdir, readFile, rm, stat, utimes, writeFile } from "node:fs/promises";
3
4
  import { tmpdir } from "node:os";
4
5
  import { dirname, join } from "node:path";
5
6
 
@@ -72,7 +73,9 @@ export function boundPlanPath(entries: Iterable<EntryLike>, owner: WorkflowOwner
72
73
  for (const entry of entries) {
73
74
  if (entry.type !== "custom") continue;
74
75
  const data = entry.data;
75
- if (entry.customType === WORKFLOW_ENTRY && isRecord(data) && data.version === 1 && data.kind === "set") path = undefined;
76
+ if (entry.customType === WORKFLOW_ENTRY && isRecord(data) && data.version === 1 && data.kind === "set") {
77
+ if (data.resumePlan !== true || !isRecord(data.owner) || data.owner.name !== owner.name) path = undefined;
78
+ }
76
79
  if (entry.customType !== WORKFLOW_PLAN_ENTRY || !isRecord(data) || data.version !== 1) continue;
77
80
  if (data.kind === "clear") path = undefined;
78
81
  else if (data.kind === "set" && data.owner === owner.name && typeof data.path === "string") path = data.path;
@@ -139,18 +142,54 @@ async function settlementScheduler(script: string, runDir: string, timeoutMs?: n
139
142
 
140
143
  // --peek suppresses seen bookkeeping, not syncRun's issue-tree write. Give the live
141
144
  // probe private inputs; keep cwd so relative workspace paths retain their meaning.
145
+ const budget = timeoutMs ?? SCHEDULER_SYNC_TIMEOUT_MS;
146
+ const startedAt = performance.now();
147
+ const signal = AbortSignal.timeout(budget);
142
148
  let snapshot: string | undefined;
143
149
  try {
144
- snapshot = mkdtempSync(join(tmpdir(), "pi-goal-scheduler-"));
145
- for (const name of ["task-plan.json", "issue-tree.json", "next-actions-seen.json", "reviews", "results"]) {
150
+ snapshot = await mkdtemp(join(tmpdir(), "pi-goal-scheduler-"));
151
+ for (const name of ["task-plan.json", "issue-tree.json", "next-actions-seen.json"]) {
152
+ await copySchedulerRecord(join(runDir, name), join(snapshot, name), signal);
153
+ }
154
+ // next-actions reads top-level JSON records. Review checkout trees and worker
155
+ // artifacts can be much larger and aren't inputs to the settlement decision.
156
+ for (const name of ["reviews", "results"]) {
157
+ signal.throwIfAborted();
146
158
  const source = join(runDir, name);
147
- if (existsSync(source)) cpSync(source, join(snapshot, name), { recursive: true, dereference: true });
159
+ const names = await readdir(source).catch((error: NodeJS.ErrnoException) => {
160
+ if (error.code === "ENOENT") return [];
161
+ throw error;
162
+ });
163
+ if (!names.length) continue;
164
+ const target = join(snapshot, name);
165
+ await mkdir(target);
166
+ for (const file of names) {
167
+ if (file.endsWith(".json")) {
168
+ await copySchedulerRecord(join(source, file), join(target, file), signal);
169
+ }
170
+ }
148
171
  }
149
- return await executeScheduler(script, snapshot, timeoutMs ?? SCHEDULER_SYNC_TIMEOUT_MS, true, runDir);
172
+ const remaining = budget - Math.ceil(performance.now() - startedAt);
173
+ if (signal.aborted || remaining <= 0) return { ok: false, reason: `the live scheduler check timed out after ${budget} ms` };
174
+ return await executeScheduler(script, snapshot, remaining, true, runDir);
150
175
  } catch (error) {
176
+ if (signal.aborted) return { ok: false, reason: `the live scheduler check timed out after ${budget} ms` };
151
177
  return { ok: false, reason: `could not prepare the live scheduler check (${String(error).slice(0, DETAIL_LIMIT)})` };
152
178
  } finally {
153
- if (snapshot) rmSync(snapshot, { recursive: true, force: true });
179
+ if (snapshot) await rm(snapshot, { recursive: true, force: true });
180
+ }
181
+ }
182
+
183
+ async function copySchedulerRecord(source: string, target: string, signal: AbortSignal): Promise<void> {
184
+ signal.throwIfAborted();
185
+ try {
186
+ const info = await stat(source);
187
+ if (!info.isFile()) return;
188
+ await writeFile(target, await readFile(source, { signal }), { signal });
189
+ // Review mtimes tell the scheduler when the last round was written.
190
+ await utimes(target, info.atime, info.mtime);
191
+ } catch (error) {
192
+ if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
154
193
  }
155
194
  }
156
195