@narumitw/pi-goal 0.53.0 → 0.53.1

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": "@narumitw/pi-goal",
3
- "version": "0.53.0",
3
+ "version": "0.53.1",
4
4
  "description": "Pi extension for autonomous single-objective /goal completion.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -0,0 +1,62 @@
1
+ import type { GoalPromptContext } from "./prompts.js";
2
+ import { buildGoalContextPrompt } from "./prompts.js";
3
+
4
+ export const GOAL_CONTRACT_MESSAGE_TYPE = "goal-contract";
5
+ export const GOAL_CONTRACT_VERSION = 1;
6
+
7
+ interface ContractMessage {
8
+ role?: string;
9
+ customType?: string;
10
+ content?: unknown;
11
+ }
12
+
13
+ export function createGoalContextContract(goal: GoalPromptContext) {
14
+ return {
15
+ role: "custom" as const,
16
+ customType: GOAL_CONTRACT_MESSAGE_TYPE,
17
+ content: buildGoalContextPrompt(goal),
18
+ display: false,
19
+ details: { version: GOAL_CONTRACT_VERSION, goalId: goal.id },
20
+ timestamp: 0,
21
+ };
22
+ }
23
+
24
+ export function reconcileGoalContextContract(messages: unknown[], goal: GoalPromptContext) {
25
+ const summaryBoundary = leadingSummaryBoundary(messages);
26
+ const expected = createGoalContextContract(goal);
27
+ const contractIndexes = messages.flatMap((message, index) =>
28
+ unwrapMessage(message).customType === GOAL_CONTRACT_MESSAGE_TYPE ? [index] : [],
29
+ );
30
+ if (
31
+ contractIndexes.length === 1 &&
32
+ contractIndexes[0] === summaryBoundary &&
33
+ unwrapMessage(messages[summaryBoundary]).content === expected.content
34
+ ) {
35
+ return messages;
36
+ }
37
+
38
+ const withoutContracts = messages.filter(
39
+ (message) => unwrapMessage(message).customType !== GOAL_CONTRACT_MESSAGE_TYPE,
40
+ );
41
+ const insertionIndex = leadingSummaryBoundary(withoutContracts);
42
+ return [
43
+ ...withoutContracts.slice(0, insertionIndex),
44
+ expected,
45
+ ...withoutContracts.slice(insertionIndex),
46
+ ];
47
+ }
48
+
49
+ function leadingSummaryBoundary(messages: readonly unknown[]) {
50
+ let index = 0;
51
+ while (index < messages.length) {
52
+ const role = unwrapMessage(messages[index]).role;
53
+ if (role !== "compactionSummary" && role !== "branchSummary") break;
54
+ index += 1;
55
+ }
56
+ return index;
57
+ }
58
+
59
+ function unwrapMessage(message: unknown): ContractMessage {
60
+ const entry = message as { message?: unknown } | undefined;
61
+ return (entry?.message ?? message ?? {}) as ContractMessage;
62
+ }
package/src/lifecycle.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
2
2
  import { currentTokenTotal } from "./accounting.js";
3
3
  import { notifyTerminal } from "./errors.js";
4
+ import { reconcileGoalContextContract } from "./goal-contract.js";
4
5
  import { type ActiveGoal, loadGoalStateFromSession } from "./persistence.js";
5
- import { buildGoalSystemPrompt } from "./prompts.js";
6
6
  import type { GoalRunController } from "./run-protocol.js";
7
7
  import {
8
8
  type AssistantMessageLike,
@@ -337,7 +337,13 @@ export function registerGoalLifecycle(
337
337
  });
338
338
 
339
339
  pi.on("context", (event, ctx) => {
340
- const messages = event.messages.filter((message) => runtime.keepBudgetWrapUpMessage(message));
340
+ const keptMessages = event.messages.filter((message) =>
341
+ runtime.keepBudgetWrapUpMessage(message),
342
+ );
343
+ const messages =
344
+ runtime.activeGoal?.status === "active" && runtime.ownsWorkflow(runtime.activeGoal)
345
+ ? reconcileGoalContextContract(keptMessages, runtime.activeGoal)
346
+ : keptMessages;
341
347
  if (
342
348
  runtime.activeGoal?.status === "paused" &&
343
349
  runtime.guardAbortGoalId === runtime.activeGoal.id
@@ -346,7 +352,9 @@ export function registerGoalLifecycle(
346
352
  // context transformation aborts before the provider adapter receives the signal.
347
353
  abortCurrentTurn(ctx);
348
354
  }
349
- if (messages.length !== event.messages.length) return { messages };
355
+ if (messages !== keptMessages || keptMessages.length !== event.messages.length) {
356
+ return { messages: messages as typeof event.messages };
357
+ }
350
358
  });
351
359
 
352
360
  pi.on("tool_call", (event, ctx) => {
@@ -465,10 +473,6 @@ export function registerGoalLifecycle(
465
473
  runtime.persistGoal(runtime.activeGoal);
466
474
  runtime.updateStatus(ctx, runtime.activeGoal);
467
475
  }
468
-
469
- return {
470
- systemPrompt: `${event.systemPrompt}\n\n${buildGoalSystemPrompt(runtime.activeGoal)}`,
471
- };
472
476
  });
473
477
 
474
478
  pi.on("agent_start", (_event, _ctx) => {
@@ -82,12 +82,6 @@ export function loadGoalStateFromSession(ctx: SessionContext): LoadedGoalState {
82
82
  return legacyEntry ? loadLegacyGoalsState(legacyEntry.data) : emptyGoalState("none");
83
83
  }
84
84
 
85
- export function loadGoalFromSession(ctx: SessionContext): ActiveGoal | undefined {
86
- const loaded = loadGoalStateFromSession(ctx);
87
- if (loaded.legacyQueueState || loaded.goal?.status === "complete") return undefined;
88
- return loaded.goal;
89
- }
90
-
91
85
  function loadCanonicalGoalState(data: unknown): LoadedGoalState {
92
86
  if (!isRecord(data)) return emptyGoalState("canonical");
93
87
  const rawGoal = data.goal;
package/src/prompts.ts CHANGED
@@ -56,8 +56,14 @@ export function buildGoalSystemPrompt(goal: GoalPromptContext) {
56
56
  return `Active /goal:\n${goalContextBlock(goal)}\n\n${goalModeRules("the active goal")}${budgetLine}`;
57
57
  }
58
58
 
59
+ export function buildGoalContextPrompt(goal: GoalPromptContext) {
60
+ return `Active /goal context:\n${goalContextBlock(goal)}\n\n${goalModeRules("the active goal")}`;
61
+ }
62
+
59
63
  export function buildContinuePrompt(goal: GoalPromptContext, marker: string) {
60
- return `Continue the active /goal until it is complete:\n\n${goalContextBlock(goal)}\n\nThis is automatic continuation #${goal.iteration}. The full objective persists across turns; continue from the authoritative current state.\n\n${goalModeRules("this goal")}\n\n${continuationMarkerComment(marker)}`;
64
+ const budgetLine =
65
+ goal.tokenBudget === undefined ? "" : `\nToken budget: ${formatBudget(goal)} used.`;
66
+ return `Continue the active /goal until it is complete:\n\n${goalContextBlock(goal)}${budgetLine}\n\nThis is automatic continuation #${goal.iteration}. The full objective persists across turns; continue from the authoritative current state.\n\n${goalModeRules("this goal")}\n\n${continuationMarkerComment(marker)}`;
61
67
  }
62
68
 
63
69
  function goalContextBlock(goal: GoalPromptContext) {
@@ -389,10 +389,6 @@ export function parseGoalLimit(value: string): number | undefined {
389
389
  return Number.isSafeInteger(parsed) && parsed > 0 ? parsed : undefined;
390
390
  }
391
391
 
392
- export function formatGoalLimit(value: number | null) {
393
- return value === null ? "Unlimited" : String(value);
394
- }
395
-
396
392
  async function resolveLimitSelection(
397
393
  field: LimitField,
398
394
  selection: LimitSelection,