pi-better-goal 0.18.0 → 0.18.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.18.0",
3
+ "version": "0.18.2",
4
4
  "description": "Pi extension for goal tracking with background-aware continuation.",
5
5
  "license": "MIT",
6
6
  "type": "module",
package/src/goal-state.ts CHANGED
@@ -16,6 +16,7 @@ interface GoalLike {
16
16
  goalId: string;
17
17
  objective: string;
18
18
  command?: unknown;
19
+ workflowBinding?: { name?: unknown; path?: unknown } | null;
19
20
  status: GoalStatus;
20
21
  pauseReason?: unknown;
21
22
  tokenBudget?: number | null;
@@ -133,6 +134,9 @@ function normalizeGoal(goal: GoalLike): GoalSnapshot {
133
134
  goalId: goal.goalId,
134
135
  objective: goal.objective,
135
136
  ...(isCommandBinding(goal.command) ? { command: goal.command } : {}),
137
+ ...(goal.workflowBinding === null ? { workflowBinding: null } :
138
+ typeof goal.workflowBinding?.name === "string" && typeof goal.workflowBinding.path === "string"
139
+ ? { workflowBinding: { name: goal.workflowBinding.name, path: goal.workflowBinding.path } } : {}),
136
140
  status: goal.status,
137
141
  ...(pauseReason ? { pauseReason } : {}),
138
142
  tokenBudget: typeof goal.tokenBudget === "number" ? goal.tokenBudget : null,
package/src/index.ts CHANGED
@@ -43,8 +43,8 @@ import {
43
43
  import { createRenderScheduler } from "./shared-render-scheduler.js";
44
44
  import { initializeNavigatorPanels, setNavigatorPanel } from "./shared-navigator-panels.js";
45
45
  import { collectSubagentActivity } from "./subagents.js";
46
- import { currentWorkflowOwner, ISSUE_WORKFLOW_SKILLS, skillCommandName, workflowEntry, workflowOwnerFromSkill, WORKFLOW_ENTRY_TYPE } from "./workflow.js";
47
- import { completionGate, pauseGate, schedulerWaitUntil, operationResumeSchedulerGate } from "./workflow-gates.js";
46
+ import { currentWorkflowOwner, restoredWorkflowMatches, ISSUE_WORKFLOW_SKILLS, skillCommandName, workflowEntry, workflowOwnerFromSkill, WORKFLOW_ENTRY_TYPE } from "./workflow.js";
47
+ import { boundPlanEntry, WORKFLOW_PLAN_ENTRY, completionGate, pauseGate, schedulerWaitUntil, operationResumeSchedulerGate } from "./workflow-gates.js";
48
48
  import { workflowRehydrateMessage, WORKFLOW_REHYDRATE_MESSAGE_TYPE } from "./rehydrate.js";
49
49
  import { canStartGoalFromOutcome, GOAL_PANEL_START_EVENT, type GoalPanelStartRequest, OUTCOME_ACCEPTED_EVENT, requestTaskContext, type OutcomeAcceptedEvent } from "./task-context.js";
50
50
  import {
@@ -398,8 +398,9 @@ export default function (pi: ExtensionAPI): void {
398
398
  return false;
399
399
  }
400
400
  };
401
- const recordWorkflow = (owner: ReturnType<typeof currentWorkflowOwner>, resumePlan = false): void => {
402
- pi.appendEntry(WORKFLOW_ENTRY_TYPE, { ...workflowEntry(owner), ...(resumePlan && { resumePlan }) });
401
+ const recordWorkflow = (owner: ReturnType<typeof currentWorkflowOwner>, plan?: ReturnType<typeof boundPlanEntry>): void => {
402
+ pi.appendEntry(WORKFLOW_ENTRY_TYPE, workflowEntry(owner));
403
+ if (plan) pi.appendEntry(WORKFLOW_PLAN_ENTRY, plan);
403
404
  pi.events.emit("pi-better-workflow:changed", owner);
404
405
  };
405
406
 
@@ -529,15 +530,13 @@ export default function (pi: ExtensionAPI): void {
529
530
  ? workflowOwnerFromSkill(goal.command.name.slice("skill:".length), goal.command.path, registeredSkillPath)
530
531
  : null;
531
532
 
532
- /**
533
- * The session's workflow owner when an active goal is bound to it through its skill command, else
534
- * null. Only these goals are gated by the workflow's scheduler.
535
- */
536
533
  const boundWorkflow = (goal: GoalSnapshot, ctx: ExtensionContext): NonNullable<ReturnType<typeof currentWorkflowOwner>> | null => {
537
534
  const owner = getWorkflow(ctx);
538
- if (!owner || goal.status !== "active" || goal.command?.source !== "skill") return null;
535
+ if (!owner || goal.command?.source !== "skill") return null;
539
536
  try {
540
537
  const bound = goalWorkflowOwner(goal);
538
+ if (goal.workflowBinding !== undefined &&
539
+ (goal.workflowBinding?.name !== bound?.name || goal.workflowBinding?.path !== bound?.path)) return null;
541
540
  return bound?.name === owner.name && bound.path === owner.path ? owner : null;
542
541
  } catch {
543
542
  return null;
@@ -698,6 +697,7 @@ export default function (pi: ExtensionAPI): void {
698
697
  const owner = command?.source === "skill"
699
698
  ? workflowOwnerFromSkill(command.name.slice("skill:".length), command.path, registeredSkillPath)
700
699
  : null;
700
+ if (command?.source === "skill") goal.workflowBinding = owner ? { name: owner.name, path: owner.path } : null;
701
701
  if (getWorkflow(ctx)) recordWorkflow(null);
702
702
  setGoal(goal, ctx, source);
703
703
  if (command) {
@@ -1037,7 +1037,7 @@ export default function (pi: ExtensionAPI): void {
1037
1037
  const invalid = validateObjective(objective);
1038
1038
  if (invalid) return invalid;
1039
1039
  const owner = getWorkflow(ctx);
1040
- if (owner && !workflowAvailable(owner)) return `Restore /skill:${owner.name} before continuing its Todo.`;
1040
+ if (!previous && owner && !workflowAvailable(owner)) return `Restore /skill:${owner.name} before continuing its Todo.`;
1041
1041
  if (previous) {
1042
1042
  const blocker = resumeBlocker(previous, ctx);
1043
1043
  if (blocker) return blocker;
@@ -1045,17 +1045,19 @@ export default function (pi: ExtensionAPI): void {
1045
1045
  if (objective.startsWith("/") && (!previous?.command || previous.objective.trim() !== objective)) {
1046
1046
  return "Reinvoke the original command to continue this Goal.";
1047
1047
  }
1048
- const command = previous?.command ?? (owner ? resolveGoalCommand(pi, `/skill:${owner.name}`) : null);
1048
+ const command = previous ? previous.command : owner ? resolveGoalCommand(pi, `/skill:${owner.name}`) : null;
1049
1049
  const goal = createGoalSnapshot(objective, null, undefined, command ?? undefined);
1050
1050
  const restoredOwner = previous?.command ? goalWorkflowOwner(goal) : null;
1051
- const completedOwner = previous?.command?.source === "skill"
1052
- ? currentWorkflowOwner(ctx.sessionManager.getBranch(), previous.goalId) : null;
1053
- if ((completedOwner && (restoredOwner?.name !== completedOwner.name || restoredOwner.path !== completedOwner.path)) ||
1054
- (handoffOwner && restoredOwner?.name !== handoffOwner) ||
1055
- (restoredOwner && owner && (restoredOwner.name !== owner.name || restoredOwner.path !== owner.path))) {
1051
+ if (previous?.command?.source === "skill" && !restoredWorkflowMatches(previous, ctx.sessionManager.getBranch(), restoredOwner)) {
1056
1052
  return "Reinvoke the original workflow to continue its Todo.";
1057
1053
  }
1058
- return { goal, restore: owner ? null : restoredOwner };
1054
+ if (handoffOwner && restoredOwner?.name !== handoffOwner) return "Reinvoke the original workflow to continue its Todo.";
1055
+ if (command?.source === "skill") {
1056
+ const binding = previous ? restoredOwner : owner;
1057
+ goal.workflowBinding = binding ? { name: binding.name, path: binding.path } : null;
1058
+ }
1059
+ const plan = restoredOwner && previous ? boundPlanEntry(ctx.sessionManager.getBranch(), restoredOwner, previous.goalId) : undefined;
1060
+ return { goal, restore: restoredOwner, plan };
1059
1061
  };
1060
1062
 
1061
1063
  const unsubscribeOutcomeAccepted = pi.events.on(OUTCOME_ACCEPTED_EVENT, (value: unknown) => {
@@ -1070,8 +1072,8 @@ export default function (pi: ExtensionAPI): void {
1070
1072
  if (!canStartGoalFromOutcome(event.outcome, reopenedGoal)) return;
1071
1073
  const prepared = prepareSavedGoal(ctx, event.outcome.objective.trim(), reopenedGoal);
1072
1074
  if (typeof prepared === "string") return;
1075
+ if (prepared.restore) recordWorkflow(prepared.restore, prepared.plan);
1073
1076
  setGoal(prepared.goal, ctx, "runtime", "bind");
1074
- if (prepared.restore) recordWorkflow(prepared.restore, true);
1075
1077
  resetContinuationState(prepared.goal);
1076
1078
  });
1077
1079
 
@@ -1118,8 +1120,8 @@ export default function (pi: ExtensionAPI): void {
1118
1120
  const prepared = prepareSavedGoal(ctx, savedGoal.objective, previous, handoffOwner);
1119
1121
  if (typeof prepared === "string") { warn(prepared); return; }
1120
1122
  const { goal } = prepared;
1123
+ if (prepared.restore) recordWorkflow(prepared.restore, prepared.plan);
1121
1124
  setGoal(action === "pause" ? goalWithStatus(goal, "paused") : goal, ctx, "command", "adopt");
1122
- if (prepared.restore) recordWorkflow(prepared.restore, true);
1123
1125
  if (action === "resume") queueGoalContinuation(goal, ctx);
1124
1126
  } else if (action === "resume") {
1125
1127
  if (!current) { warn("No saved Goal or unfinished Todo to resume."); return; }
@@ -1240,8 +1242,9 @@ export default function (pi: ExtensionAPI): void {
1240
1242
  notifyGoal(ctx, "No goal is set.", "warning");
1241
1243
  return;
1242
1244
  }
1245
+ const releaseOwner = current.status !== "complete" ? boundWorkflow(current, ctx) : null;
1243
1246
  setGoal(goalWithStatus(current, "complete"), ctx, "command");
1244
- if (getWorkflow(ctx)) recordWorkflow(null);
1247
+ if (releaseOwner) recordWorkflow(null);
1245
1248
  notifyGoal(ctx, "Goal marked complete.");
1246
1249
  return;
1247
1250
  }
@@ -1314,7 +1317,8 @@ export default function (pi: ExtensionAPI): void {
1314
1317
  details: { ok: true, goal: current },
1315
1318
  };
1316
1319
  }
1317
- const gated = boundWorkflow(current, ctx);
1320
+ const releaseOwner = boundWorkflow(current, ctx);
1321
+ const gated = current.status === "active" ? releaseOwner : null;
1318
1322
  const verdict = gated ? await completionGate(ctx.sessionManager.getBranch(), gated) : null;
1319
1323
  if (verdict && !verdict.allow) {
1320
1324
  return {
@@ -1326,13 +1330,15 @@ export default function (pi: ExtensionAPI): void {
1326
1330
  if (latest?.goalId !== current.goalId || latest.status !== current.status) {
1327
1331
  return { content: [{ type: "text", text: "Goal changed during the completion check. Read the current Goal." }], details: { ok: false, goal: latest } };
1328
1332
  }
1329
- if (gated) {
1330
- const owner = getWorkflow(ctx);
1331
- if (owner?.name !== gated.name || owner.path !== gated.path) {
1332
- return { content: [{ type: "text", text: "Workflow changed during the completion check. Read the current Goal." }], details: { ok: false, goal: latest } };
1333
- }
1333
+ const activeOwner = boundWorkflow(latest, ctx);
1334
+ if (releaseOwner?.name !== activeOwner?.name || releaseOwner?.path !== activeOwner?.path) {
1335
+ return { content: [{ type: "text", text: "Workflow changed during the completion check. Read the current Goal." }], details: { ok: false, goal: latest } };
1336
+ }
1337
+ const audit = requestTaskContext(pi, "audit", current);
1338
+ if (audit.enabled && !audit.auditHandled) {
1339
+ return { content: [{ type: "text", text: "Harness did not confirm the Todo audit. Update Harness before completing this Goal." }],
1340
+ details: { ok: false, goal: current } };
1334
1341
  }
1335
- const audit = requestTaskContext(pi, { action: "audit", workflowOwnsPlan: !!gated }, current);
1336
1342
  const criteria = audit.doneWhen?.length ? audit.doneWhen : audit.objective ? [audit.objective] : [];
1337
1343
  const evidence = [...input.evidence ?? [], ...audit.evidence ?? []];
1338
1344
  const missing = criteria.filter(criterion => !evidence.some(item => item.criterion === criterion && item.result.trim()));
@@ -1347,7 +1353,7 @@ export default function (pi: ExtensionAPI): void {
1347
1353
  }
1348
1354
  const goal = goalWithStatus(latest, "complete");
1349
1355
  setGoal(goal, ctx, "tool");
1350
- if (gated) recordWorkflow(null);
1356
+ if (releaseOwner) recordWorkflow(null);
1351
1357
  const warning = verdict?.allow ? verdict.warning : undefined;
1352
1358
  return {
1353
1359
  content: [{ type: "text", text: warning ? `Goal marked complete. ${warning}` : "Goal marked complete." }],
@@ -1431,7 +1437,7 @@ export default function (pi: ExtensionAPI): void {
1431
1437
  const owner = getWorkflow(ctx);
1432
1438
  if (!owner) return { content: [{ type: "text", text: "No workflow owns this session." }], details: { released: false } };
1433
1439
  const goal = getGoal(ctx);
1434
- const gated = goal ? boundWorkflow(goal, ctx) : null;
1440
+ const gated = goal?.status === "active" ? boundWorkflow(goal, ctx) : null;
1435
1441
  const verdict = gated ? await pauseGate(ctx.sessionManager.getBranch(), gated) : null;
1436
1442
  if (verdict && !verdict.allow) {
1437
1443
  return { content: [{ type: "text", text: verdict.message }], details: { released: false, goalPaused: false } };
@@ -44,6 +44,7 @@ export function canStartGoalFromOutcome(value: unknown, reopenedGoal?: Pick<Goal
44
44
  */
45
45
  export interface TaskContextRequest {
46
46
  action: TaskContextAction;
47
+ auditHandled?: boolean;
47
48
  savedGoal?: { objective: string; status: string; goalId?: string; reopenedGoalId?: string };
48
49
  handoffOwner?: string;
49
50
  manual?: boolean;
package/src/types.ts CHANGED
@@ -102,6 +102,8 @@ export interface GoalSnapshot {
102
102
  goalId: string;
103
103
  objective: string;
104
104
  command?: GoalCommandBinding;
105
+ /** Canonical coordinator of a skill at creation; older Goals predate this field. */
106
+ workflowBinding?: { name: string; path: string } | null;
105
107
  status: GoalStatus;
106
108
  /** Present only while `status` is "paused". */
107
109
  pauseReason?: GoalPauseReason;
@@ -4,10 +4,11 @@ import { mkdir, mkdtemp, readdir, readFile, rm, stat, utimes, writeFile } from "
4
4
  import { tmpdir } from "node:os";
5
5
  import { dirname, join } from "node:path";
6
6
 
7
+ import { EXTENSION_NAME } from "./types.js";
7
8
  import { ISSUE_WORKFLOW_SKILLS, type WorkflowOwner } from "./workflow.js";
8
9
 
9
10
  /** Branch entry the plan extension writes when it binds a workflow-owned task plan. */
10
- const WORKFLOW_PLAN_ENTRY = "pi-better-workflow-plan";
11
+ export const WORKFLOW_PLAN_ENTRY = "pi-better-workflow-plan";
11
12
  const WORKFLOW_ENTRY = "pi-better-workflow";
12
13
  export const SCHEDULER_TIMEOUT_MS = 20_000;
13
14
  // Live tree queries allow three 60-second attempts plus follow-ups; cached probes stay cheap.
@@ -68,19 +69,63 @@ export function findScheduler(owner: WorkflowOwner): string | undefined {
68
69
  * The task-plan.json the session bound for `owner`, or undefined before a plan is bound.
69
70
  * Invoking a workflow skill again starts a new run, so a later owner entry drops the old binding.
70
71
  */
71
- export function boundPlanPath(entries: Iterable<EntryLike>, owner: WorkflowOwner): string | undefined {
72
- let path: string | undefined;
72
+ export function boundPlanEntry(entries: Iterable<EntryLike>, owner: WorkflowOwner, goalId?: string) {
73
+ let plan: { version: 1; kind: "set"; owner: string; path: string; runId?: string } | undefined;
74
+ let ownsRun = false;
75
+ let foundGoal = false;
76
+ let handback: "paused" | "released" | "rebound" | undefined;
73
77
  for (const entry of entries) {
74
- if (entry.type !== "custom") continue;
78
+ // resumeGoal records the owner and then activates the same Goal. A separate
79
+ // invocation can bind the same skill, but cannot supply that Goal transition.
80
+ if (handback === "rebound" && entry.type === "message") handback = "released";
81
+ if (entry.type !== "custom" || !isRecord(entry.data)) continue;
75
82
  const data = entry.data;
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;
83
+ if (goalId && entry.customType === EXTENSION_NAME && data.kind === "set" && isRecord(data.goal)) {
84
+ if (foundGoal && (data.goal.goalId !== goalId || data.goal.status === "complete")) break;
85
+ if (data.goal.goalId === goalId) foundGoal = true;
86
+ if (handback === "released") {
87
+ if (data.goal.status === "paused" && data.goal.pauseReason === "handback") continue;
88
+ break;
89
+ }
90
+ if (handback === "rebound") {
91
+ if (data.goal.status !== "active") break;
92
+ plan = undefined;
93
+ }
94
+ handback = foundGoal && data.goal.status === "paused" && data.goal.pauseReason === "handback" ? "paused" : undefined;
95
+ }
96
+ if (data.version !== 1) continue;
97
+ if (entry.customType === WORKFLOW_ENTRY) {
98
+ if (handback === "paused" && ownsRun && data.kind === "clear") {
99
+ handback = "released";
100
+ continue;
101
+ }
102
+ if (handback === "released" || handback === "rebound") {
103
+ handback = data.kind === "set" && isRecord(data.owner) && data.owner.name === owner.name && data.owner.path === owner.path
104
+ ? "rebound" : "released";
105
+ continue;
106
+ }
107
+ // A later invocation, even of the same skill, starts a different run.
108
+ if (foundGoal && ownsRun && (data.kind === "set" || data.kind === "clear")) break;
109
+ if (data.kind === "clear") ownsRun = false;
110
+ if (data.kind === "set") {
111
+ ownsRun = isRecord(data.owner) && data.owner.name === owner.name && data.owner.path === owner.path;
112
+ if (data.resumePlan !== true || !ownsRun) plan = undefined;
113
+ }
114
+ }
115
+ if (entry.customType !== WORKFLOW_PLAN_ENTRY) continue;
116
+ if (handback === "released") continue;
117
+ if (handback === "rebound") { handback = "released"; continue; }
118
+ if (data.kind === "clear") plan = undefined;
119
+ else if (data.kind === "set" && data.owner === owner.name && typeof data.path === "string") {
120
+ plan = { version: 1, kind: "set", owner: owner.name, path: data.path,
121
+ ...(typeof data.runId === "string" && { runId: data.runId }) };
78
122
  }
79
- if (entry.customType !== WORKFLOW_PLAN_ENTRY || !isRecord(data) || data.version !== 1) continue;
80
- if (data.kind === "clear") path = undefined;
81
- else if (data.kind === "set" && data.owner === owner.name && typeof data.path === "string") path = data.path;
82
123
  }
83
- return path;
124
+ return !goalId || (foundGoal && ownsRun) ? plan : undefined;
125
+ }
126
+
127
+ export function boundPlanPath(entries: Iterable<EntryLike>, owner: WorkflowOwner): string | undefined {
128
+ return boundPlanEntry(entries, owner)?.path;
84
129
  }
85
130
 
86
131
  function parseSchedulerOutput(stdout: string): SchedulerRun {
package/src/workflow.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { readFileSync } from "node:fs";
2
2
  import { parse } from "yaml";
3
- import { EXTENSION_NAME } from "./types.js";
3
+ import { EXTENSION_NAME, type GoalSnapshot } from "./types.js";
4
4
 
5
5
  export const WORKFLOW_ENTRY_TYPE = "pi-better-workflow";
6
6
 
@@ -84,13 +84,19 @@ export function workflowEntry(owner: WorkflowOwner | null) {
84
84
  : { version: 1, kind: "set" as const, owner };
85
85
  }
86
86
 
87
- export function currentWorkflowOwner(entries: Iterable<SessionEntryLike>, atGoalCompletion?: string): WorkflowOwner | null {
87
+ export function currentWorkflowOwner(entries: Iterable<SessionEntryLike>, atGoalStart?: string): WorkflowOwner | null {
88
88
  let owner: WorkflowOwner | null = null;
89
+ let awaitingInitialBinding = false;
89
90
  for (const entry of entries) {
91
+ if (awaitingInitialBinding && (entry.type === "message" || entry.customType === EXTENSION_NAME)) return null;
90
92
  if (entry.type !== "custom") continue;
91
- if (atGoalCompletion && entry.customType === EXTENSION_NAME) {
93
+ if (atGoalStart && entry.customType === EXTENSION_NAME) {
92
94
  const data = entry.data as { kind?: string; goal?: { goalId?: string; status?: string } } | null;
93
- if (data?.kind === "set" && data.goal?.goalId === atGoalCompletion && data.goal.status === "complete") return owner;
95
+ if (data?.kind === "set" && data.goal?.goalId === atGoalStart) {
96
+ // Automatic Goals inherit the active owner. Explicit skill Goals record it just after creation.
97
+ if (owner) return owner;
98
+ awaitingInitialBinding = true;
99
+ }
94
100
  }
95
101
  if (entry.customType !== WORKFLOW_ENTRY_TYPE) continue;
96
102
  const data = entry.data as { version?: unknown; kind?: unknown; owner?: Partial<WorkflowOwner> } | null;
@@ -100,8 +106,15 @@ export function currentWorkflowOwner(entries: Iterable<SessionEntryLike>, atGoal
100
106
  typeof data.owner.path === "string" && data.owner.role === "coordinator" && data.owner.planOwner === "workflow") {
101
107
  owner = data.owner as WorkflowOwner;
102
108
  }
109
+ if (awaitingInitialBinding) return owner;
103
110
  }
104
- return atGoalCompletion ? null : owner;
111
+ return atGoalStart ? null : owner;
112
+ }
113
+
114
+ export function restoredWorkflowMatches(goal: GoalSnapshot, entries: Iterable<SessionEntryLike>, restored: WorkflowOwner | null): boolean {
115
+ const original = goal.workflowBinding === undefined ? currentWorkflowOwner(entries, goal.goalId) : goal.workflowBinding;
116
+ if (goal.workflowBinding === undefined && !original) return true;
117
+ return original?.name === restored?.name && original?.path === restored?.path;
105
118
  }
106
119
 
107
120
  export function skillCommandName(text: string): string | null {