@narumitw/pi-goal 0.24.0 → 0.27.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/README.md +29 -11
- package/package.json +3 -3
- package/src/commands.ts +12 -3
- package/src/errors.ts +147 -0
- package/src/goal.ts +195 -43
- package/src/markers.ts +25 -0
- package/src/persistence.ts +26 -1
- package/src/queue.ts +4 -1
- package/src/runtime.ts +350 -191
- package/src/safety.ts +78 -0
- package/src/settings.ts +41 -0
package/src/safety.ts
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import type { ActiveGoal } from "./persistence.js";
|
|
3
|
+
|
|
4
|
+
export interface ToolFreeRepeatState {
|
|
5
|
+
toolFreeRepeatCount: number;
|
|
6
|
+
lastToolFreeOutputFingerprint?: string;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function queueGoalSafetyReset(goal: ActiveGoal): ActiveGoal {
|
|
10
|
+
return { ...goal, safetyResetPending: true };
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function resetGoalSafetyEpoch(goal: ActiveGoal): ActiveGoal {
|
|
14
|
+
return {
|
|
15
|
+
...goal,
|
|
16
|
+
automaticModelTurns: 0,
|
|
17
|
+
toolFreeRepeatCount: 0,
|
|
18
|
+
lastToolFreeOutputFingerprint: undefined,
|
|
19
|
+
safetyPauseCause: undefined,
|
|
20
|
+
safetyResetPending: undefined,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function nextToolFreeRepeatState(
|
|
25
|
+
current: ToolFreeRepeatState,
|
|
26
|
+
messages: readonly unknown[],
|
|
27
|
+
toolAttempted: boolean,
|
|
28
|
+
): ToolFreeRepeatState {
|
|
29
|
+
if (toolAttempted) return { toolFreeRepeatCount: 0 };
|
|
30
|
+
const fingerprint = fingerprintVisibleAssistantOutput(messages);
|
|
31
|
+
return {
|
|
32
|
+
toolFreeRepeatCount:
|
|
33
|
+
fingerprint === current.lastToolFreeOutputFingerprint
|
|
34
|
+
? Math.min(Number.MAX_SAFE_INTEGER, current.toolFreeRepeatCount + 1)
|
|
35
|
+
: 1,
|
|
36
|
+
lastToolFreeOutputFingerprint: fingerprint,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function hasAssistantToolCall(messages: readonly unknown[]) {
|
|
41
|
+
for (const message of messages) {
|
|
42
|
+
if (!isRecord(message) || message.role !== "assistant" || !Array.isArray(message.content)) {
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
if (message.content.some((block) => isRecord(block) && block.type === "toolCall")) return true;
|
|
46
|
+
}
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function fingerprintVisibleAssistantOutput(messages: readonly unknown[]) {
|
|
51
|
+
const normalized = normalizeVisibleAssistantOutput(messages);
|
|
52
|
+
return createHash("sha256").update(normalized, "utf8").digest("hex");
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function normalizeVisibleAssistantOutput(messages: readonly unknown[]) {
|
|
56
|
+
const text: string[] = [];
|
|
57
|
+
for (const message of messages) {
|
|
58
|
+
if (!isRecord(message) || message.role !== "assistant" || !Array.isArray(message.content)) {
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
for (const block of message.content) {
|
|
62
|
+
if (!isRecord(block) || block.type !== "text" || typeof block.text !== "string") continue;
|
|
63
|
+
text.push(block.text);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
const normalized = text
|
|
67
|
+
.join("\n")
|
|
68
|
+
.normalize("NFKC")
|
|
69
|
+
.toLowerCase()
|
|
70
|
+
.replace(/\s+/gu, " ")
|
|
71
|
+
.replace(/[\p{Cc}\p{Cf}]/gu, "")
|
|
72
|
+
.trim();
|
|
73
|
+
return normalized === "" || /^[\p{P}\s]+$/u.test(normalized) ? "" : normalized;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
77
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
78
|
+
}
|
package/src/settings.ts
CHANGED
|
@@ -6,17 +6,23 @@ export const GOAL_SETTINGS_FILE = "pi-goal.json";
|
|
|
6
6
|
export const GOAL_TOOL_VISIBILITIES = ["always", "after-first-goal"] as const;
|
|
7
7
|
|
|
8
8
|
export type GoalToolVisibility = (typeof GOAL_TOOL_VISIBILITIES)[number];
|
|
9
|
+
export type ContinuationLimit = number | null;
|
|
9
10
|
|
|
10
11
|
export interface GoalSettings {
|
|
11
12
|
toolVisibility: GoalToolVisibility;
|
|
12
13
|
experimental: {
|
|
13
14
|
goals: boolean;
|
|
14
15
|
};
|
|
16
|
+
continuationLimits: {
|
|
17
|
+
automaticTurns: ContinuationLimit;
|
|
18
|
+
noProgressTurns: ContinuationLimit;
|
|
19
|
+
};
|
|
15
20
|
}
|
|
16
21
|
|
|
17
22
|
export const DEFAULT_GOAL_SETTINGS: GoalSettings = {
|
|
18
23
|
toolVisibility: "always",
|
|
19
24
|
experimental: { goals: false },
|
|
25
|
+
continuationLimits: { automaticTurns: 25, noProgressTurns: 3 },
|
|
20
26
|
};
|
|
21
27
|
|
|
22
28
|
export type GoalSettingsLoadResult =
|
|
@@ -48,12 +54,47 @@ export function normalizeGoalSettings(value: unknown): GoalSettings | undefined
|
|
|
48
54
|
: DEFAULT_GOAL_SETTINGS.experimental.goals;
|
|
49
55
|
if (typeof goals !== "boolean") return undefined;
|
|
50
56
|
|
|
57
|
+
const continuationLimitsValue = Object.hasOwn(value, "continuationLimits")
|
|
58
|
+
? Reflect.get(value, "continuationLimits")
|
|
59
|
+
: undefined;
|
|
60
|
+
if (
|
|
61
|
+
continuationLimitsValue !== undefined &&
|
|
62
|
+
(typeof continuationLimitsValue !== "object" ||
|
|
63
|
+
continuationLimitsValue === null ||
|
|
64
|
+
Array.isArray(continuationLimitsValue))
|
|
65
|
+
) {
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|
|
68
|
+
const automaticTurns = continuationLimitsValue
|
|
69
|
+
? normalizeContinuationLimit(
|
|
70
|
+
Reflect.get(continuationLimitsValue, "automaticTurns"),
|
|
71
|
+
DEFAULT_GOAL_SETTINGS.continuationLimits.automaticTurns,
|
|
72
|
+
)
|
|
73
|
+
: DEFAULT_GOAL_SETTINGS.continuationLimits.automaticTurns;
|
|
74
|
+
const noProgressTurns = continuationLimitsValue
|
|
75
|
+
? normalizeContinuationLimit(
|
|
76
|
+
Reflect.get(continuationLimitsValue, "noProgressTurns"),
|
|
77
|
+
DEFAULT_GOAL_SETTINGS.continuationLimits.noProgressTurns,
|
|
78
|
+
)
|
|
79
|
+
: DEFAULT_GOAL_SETTINGS.continuationLimits.noProgressTurns;
|
|
80
|
+
if (automaticTurns === undefined || noProgressTurns === undefined) return undefined;
|
|
81
|
+
|
|
51
82
|
return {
|
|
52
83
|
toolVisibility: toolVisibility as GoalToolVisibility,
|
|
53
84
|
experimental: { goals },
|
|
85
|
+
continuationLimits: { automaticTurns, noProgressTurns },
|
|
54
86
|
};
|
|
55
87
|
}
|
|
56
88
|
|
|
89
|
+
function normalizeContinuationLimit(
|
|
90
|
+
value: unknown,
|
|
91
|
+
fallback: ContinuationLimit,
|
|
92
|
+
): ContinuationLimit | undefined {
|
|
93
|
+
if (value === undefined) return fallback;
|
|
94
|
+
if (value === null) return null;
|
|
95
|
+
return typeof value === "number" && Number.isSafeInteger(value) && value > 0 ? value : undefined;
|
|
96
|
+
}
|
|
97
|
+
|
|
57
98
|
export function readGoalSettings(
|
|
58
99
|
settingsPath = join(getAgentDir(), GOAL_SETTINGS_FILE),
|
|
59
100
|
): GoalSettingsLoadResult {
|