pi-better-goal 0.18.0 → 0.18.3

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.3",
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,34 +1045,53 @@ 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
- const unsubscribeOutcomeAccepted = pi.events.on(OUTCOME_ACCEPTED_EVENT, (value: unknown) => {
1062
- const ctx = currentCtx;
1063
- const event = value as Partial<OutcomeAcceptedEvent> | null;
1064
- if (!preferences.autoStartFromOutcome || !ctx || !event ||
1065
- typeof event.sessionId !== "string" || event.sessionId !== ctx.sessionManager.getSessionId()) return;
1063
+ const acceptOutcome = (ctx: ExtensionContext, event: OutcomeAcceptedEvent): string => {
1064
+ const saved = event.outcome;
1066
1065
  const current = getGoal(ctx);
1067
- if (current && current.status !== "complete") return;
1066
+ if (current && current.goalId === saved.goalId) {
1067
+ if (current.status === "active") return "Goal is running.";
1068
+ if (current.status === "budgetLimited") return "Goal reached its token budget.";
1069
+ return `Goal ${current.status}.`;
1070
+ }
1071
+ if (saved.status !== "active") return `Goal ${saved.status}.`;
1072
+ if (current && current.status !== "complete") return "Goal saved. Another Goal owns execution. Inspect it with /goal.";
1068
1073
  const reopenedGoal = current?.status === "complete" && current.goalId === event.reopenedGoalId ? current : undefined;
1069
- if (event.reopenedGoalId && !reopenedGoal) return;
1070
- if (!canStartGoalFromOutcome(event.outcome, reopenedGoal)) return;
1071
- const prepared = prepareSavedGoal(ctx, event.outcome.objective.trim(), reopenedGoal);
1072
- if (typeof prepared === "string") return;
1074
+ if (saved.goalId || (event.reopenedGoalId && !reopenedGoal)) return "Goal saved. Reinvoke its original Goal or workflow to continue.";
1075
+ const objective = saved.objective.trim();
1076
+ if (!canStartGoalFromOutcome(saved, reopenedGoal)) {
1077
+ if (objective.startsWith("/")) return "Goal saved. Reinvoke the original command to continue.";
1078
+ if (preferences.autoStartFromOutcome) return "Goal saved. Auto-start requires completion criteria in goal.doneWhen. Use /goal resume to start it manually.";
1079
+ }
1080
+ if (!preferences.autoStartFromOutcome) return "Goal saved. Use /goal resume to start it.";
1081
+ const prepared = prepareSavedGoal(ctx, objective, reopenedGoal);
1082
+ if (typeof prepared === "string") return `Goal saved. ${prepared}`;
1083
+ if (prepared.restore) recordWorkflow(prepared.restore, prepared.plan);
1073
1084
  setGoal(prepared.goal, ctx, "runtime", "bind");
1074
- if (prepared.restore) recordWorkflow(prepared.restore, true);
1075
1085
  resetContinuationState(prepared.goal);
1086
+ return "Goal is running.";
1087
+ };
1088
+
1089
+ const unsubscribeOutcomeAccepted = pi.events.on(OUTCOME_ACCEPTED_EVENT, (value: unknown) => {
1090
+ const ctx = currentCtx;
1091
+ const event = value as OutcomeAcceptedEvent | null;
1092
+ if (!ctx || typeof event?.outcome?.objective !== "string" ||
1093
+ typeof event.sessionId !== "string" || event.sessionId !== ctx.sessionManager.getSessionId()) return;
1094
+ event.feedback = acceptOutcome(ctx, event);
1076
1095
  });
1077
1096
 
1078
1097
  const inspectGoal = (ctx: ExtensionContext) => {
@@ -1118,8 +1137,8 @@ export default function (pi: ExtensionAPI): void {
1118
1137
  const prepared = prepareSavedGoal(ctx, savedGoal.objective, previous, handoffOwner);
1119
1138
  if (typeof prepared === "string") { warn(prepared); return; }
1120
1139
  const { goal } = prepared;
1140
+ if (prepared.restore) recordWorkflow(prepared.restore, prepared.plan);
1121
1141
  setGoal(action === "pause" ? goalWithStatus(goal, "paused") : goal, ctx, "command", "adopt");
1122
- if (prepared.restore) recordWorkflow(prepared.restore, true);
1123
1142
  if (action === "resume") queueGoalContinuation(goal, ctx);
1124
1143
  } else if (action === "resume") {
1125
1144
  if (!current) { warn("No saved Goal or unfinished Todo to resume."); return; }
@@ -1240,8 +1259,9 @@ export default function (pi: ExtensionAPI): void {
1240
1259
  notifyGoal(ctx, "No goal is set.", "warning");
1241
1260
  return;
1242
1261
  }
1262
+ const releaseOwner = current.status !== "complete" ? boundWorkflow(current, ctx) : null;
1243
1263
  setGoal(goalWithStatus(current, "complete"), ctx, "command");
1244
- if (getWorkflow(ctx)) recordWorkflow(null);
1264
+ if (releaseOwner) recordWorkflow(null);
1245
1265
  notifyGoal(ctx, "Goal marked complete.");
1246
1266
  return;
1247
1267
  }
@@ -1314,7 +1334,8 @@ export default function (pi: ExtensionAPI): void {
1314
1334
  details: { ok: true, goal: current },
1315
1335
  };
1316
1336
  }
1317
- const gated = boundWorkflow(current, ctx);
1337
+ const releaseOwner = boundWorkflow(current, ctx);
1338
+ const gated = current.status === "active" ? releaseOwner : null;
1318
1339
  const verdict = gated ? await completionGate(ctx.sessionManager.getBranch(), gated) : null;
1319
1340
  if (verdict && !verdict.allow) {
1320
1341
  return {
@@ -1326,13 +1347,15 @@ export default function (pi: ExtensionAPI): void {
1326
1347
  if (latest?.goalId !== current.goalId || latest.status !== current.status) {
1327
1348
  return { content: [{ type: "text", text: "Goal changed during the completion check. Read the current Goal." }], details: { ok: false, goal: latest } };
1328
1349
  }
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
- }
1350
+ const activeOwner = boundWorkflow(latest, ctx);
1351
+ if (releaseOwner?.name !== activeOwner?.name || releaseOwner?.path !== activeOwner?.path) {
1352
+ return { content: [{ type: "text", text: "Workflow changed during the completion check. Read the current Goal." }], details: { ok: false, goal: latest } };
1353
+ }
1354
+ const audit = requestTaskContext(pi, "audit", current);
1355
+ if (audit.enabled && !audit.auditHandled) {
1356
+ return { content: [{ type: "text", text: "Harness did not confirm the Todo audit. Update Harness before completing this Goal." }],
1357
+ details: { ok: false, goal: current } };
1334
1358
  }
1335
- const audit = requestTaskContext(pi, { action: "audit", workflowOwnsPlan: !!gated }, current);
1336
1359
  const criteria = audit.doneWhen?.length ? audit.doneWhen : audit.objective ? [audit.objective] : [];
1337
1360
  const evidence = [...input.evidence ?? [], ...audit.evidence ?? []];
1338
1361
  const missing = criteria.filter(criterion => !evidence.some(item => item.criterion === criterion && item.result.trim()));
@@ -1347,7 +1370,7 @@ export default function (pi: ExtensionAPI): void {
1347
1370
  }
1348
1371
  const goal = goalWithStatus(latest, "complete");
1349
1372
  setGoal(goal, ctx, "tool");
1350
- if (gated) recordWorkflow(null);
1373
+ if (releaseOwner) recordWorkflow(null);
1351
1374
  const warning = verdict?.allow ? verdict.warning : undefined;
1352
1375
  return {
1353
1376
  content: [{ type: "text", text: warning ? `Goal marked complete. ${warning}` : "Goal marked complete." }],
@@ -1431,7 +1454,7 @@ export default function (pi: ExtensionAPI): void {
1431
1454
  const owner = getWorkflow(ctx);
1432
1455
  if (!owner) return { content: [{ type: "text", text: "No workflow owns this session." }], details: { released: false } };
1433
1456
  const goal = getGoal(ctx);
1434
- const gated = goal ? boundWorkflow(goal, ctx) : null;
1457
+ const gated = goal?.status === "active" ? boundWorkflow(goal, ctx) : null;
1435
1458
  const verdict = gated ? await pauseGate(ctx.sessionManager.getBranch(), gated) : null;
1436
1459
  if (verdict && !verdict.allow) {
1437
1460
  return { content: [{ type: "text", text: verdict.message }], details: { released: false, goalPaused: false } };
@@ -16,6 +16,8 @@ export type GoalPanelStartRequest =
16
16
 
17
17
  export interface OutcomeAcceptedEvent {
18
18
  sessionId: string;
19
+ /** The Goal extension fills this in synchronously for the update_todo reply. */
20
+ feedback?: string;
19
21
  reopenedGoalId?: string;
20
22
  outcome: {
21
23
  objective: string;
@@ -44,6 +46,7 @@ export function canStartGoalFromOutcome(value: unknown, reopenedGoal?: Pick<Goal
44
46
  */
45
47
  export interface TaskContextRequest {
46
48
  action: TaskContextAction;
49
+ auditHandled?: boolean;
47
50
  savedGoal?: { objective: string; status: string; goalId?: string; reopenedGoalId?: string };
48
51
  handoffOwner?: string;
49
52
  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 {