pi-advisor-flow 0.13.1 → 0.13.2-dev.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/CHANGELOG.md +4 -0
- package/dist/index.js +14 -7
- package/extensions/index.ts +6 -0
- package/package.json +9 -9
- package/scripts/check-bun-version.mjs +9 -0
- package/src/attachments.ts +36 -0
- package/src/child-session.ts +1 -0
- package/src/commands/activation-preparation.ts +12 -0
- package/src/commands/activation.ts +43 -0
- package/src/commands/advisor-tools.ts +3 -0
- package/src/commands/lifecycle.ts +15 -0
- package/src/commands/manual-command.ts +19 -0
- package/src/commands/manual-consultation.ts +20 -0
- package/src/commands/manual-progress.ts +13 -0
- package/src/commands/model-commands.ts +4 -0
- package/src/commands/model-options.ts +18 -0
- package/src/commands/model-picker.ts +16 -0
- package/src/commands/outcome-stats.ts +9 -0
- package/src/commands/registration.ts +1 -0
- package/src/commands/renderers.ts +14 -0
- package/src/commands/runtime.ts +5 -0
- package/src/commands/settings-commands.ts +18 -0
- package/src/commands/settings-persistence.ts +2 -0
- package/src/commands.ts +1 -0
- package/src/config/args.ts +7 -0
- package/src/config/defaults.ts +1 -0
- package/src/config/schema.ts +1 -0
- package/src/config/state.ts +64 -1
- package/src/config/storage.ts +37 -0
- package/src/config/types.ts +9 -0
- package/src/config/validation.ts +7 -0
- package/src/config.ts +6 -0
- package/src/content-utils.ts +5 -0
- package/src/conversation.ts +39 -0
- package/src/follow-up.ts +8 -0
- package/src/git.ts +8 -0
- package/src/herdr-block.ts +7 -0
- package/src/herdr-shared.ts +23 -0
- package/src/herdr.ts +18 -0
- package/src/image-attachments.ts +24 -0
- package/src/image-validation.ts +58 -0
- package/src/images.ts +24 -0
- package/src/jev/base-url.ts +14 -0
- package/src/jev/client.ts +49 -0
- package/src/jev/decisions-client.ts +14 -0
- package/src/jev/decisions-response.ts +38 -0
- package/src/jev/endpoint-key-store.ts +18 -0
- package/src/jev/key-store.ts +28 -0
- package/src/jev/ledger.ts +30 -12
- package/src/jev/openai-key-store.ts +17 -0
- package/src/jev/questions.ts +15 -0
- package/src/jev/state.ts +5 -0
- package/src/jev/transport.ts +35 -0
- package/src/model-stream.ts +32 -0
- package/src/outcome-stats.ts +16 -0
- package/src/outcomes.ts +31 -0
- package/src/pi-settings.ts +9 -0
- package/src/preferences.ts +26 -0
- package/src/redaction.ts +8 -0
- package/src/scout-context.ts +17 -0
- package/src/scout-curation.ts +12 -0
- package/src/scout-groups.ts +47 -0
- package/src/scout-protocol.ts +17 -0
- package/src/scout-reconstruct.ts +12 -0
- package/src/scout.ts +47 -0
- package/src/session-context.ts +11 -0
- package/src/session-state.ts +73 -13
- package/src/tool-result-cap.ts +11 -0
- package/src/tools/consult-context.ts +24 -0
- package/src/tools/consultation.ts +43 -0
- package/src/tools/gate-policy.ts +18 -0
- package/src/tools/gate-protocol.ts +22 -0
- package/src/tools/jev-filter.ts +26 -0
- package/src/tools/jev-turn-gate.ts +17 -0
- package/src/tools/loop-gate.ts +25 -0
- package/src/tools/model-access.ts +12 -0
- package/src/tools/outage-notifier.ts +4 -0
- package/src/tools/prompts.ts +17 -0
- package/src/tools/register-ask-advisor.ts +44 -0
- package/src/tools/register-lifecycle.ts +32 -0
- package/src/tools/register-outcome.ts +7 -0
- package/src/tools/register-renderers.ts +14 -0
- package/src/tools/registration.ts +3 -0
- package/src/tools/render-advisor-result.ts +44 -0
- package/src/tools/render-common.ts +10 -0
- package/src/tools/scout-status.ts +34 -0
- package/src/tools/session.ts +3 -0
- package/src/tools.ts +8 -0
- package/src/ui/jev-filter-submenu.ts +21 -0
- package/src/ui/jev-provider-submenu-view.ts +22 -0
- package/src/ui/jev-provider-submenu.ts +81 -0
- package/src/ui/jev-setup-support.ts +26 -0
- package/src/ui/manual-dialog-render.ts +20 -0
- package/src/ui/manual-dialog.ts +38 -0
- package/src/ui/masked-input.ts +2 -0
- package/src/ui/searchable-model-list.ts +28 -0
- package/src/ui/settings-formatting.ts +26 -0
- package/src/ui/settings-items.ts +11 -0
- package/src/ui/settings-list-adapter.ts +13 -0
- package/src/ui/settings-mutations.ts +60 -0
- package/src/ui/settings-selector.ts +30 -0
- package/src/ui/text-setting-submenu.ts +9 -0
- package/src/ui-guard.ts +1 -0
- package/src/ui.ts +3 -0
- package/src/usage.ts +31 -0
package/src/model-stream.ts
CHANGED
|
@@ -33,13 +33,17 @@ export const resolveConfiguredModel = async (
|
|
|
33
33
|
if (!ref) {
|
|
34
34
|
throw new Error(`${label} model not configured`);
|
|
35
35
|
}
|
|
36
|
+
|
|
36
37
|
const [provider, modelId] = splitRef(ref);
|
|
37
38
|
const lookup: [string, string] = [provider, modelId];
|
|
38
39
|
const model = ctx.modelRegistry.find(...lookup);
|
|
40
|
+
|
|
39
41
|
if (!model) {
|
|
40
42
|
throw new Error(`${label} model not found: ${ref}`);
|
|
41
43
|
}
|
|
44
|
+
|
|
42
45
|
const { streamSimple } = ctx.modelRegistry;
|
|
46
|
+
|
|
43
47
|
if (streamSimple) {
|
|
44
48
|
return {
|
|
45
49
|
model,
|
|
@@ -47,13 +51,17 @@ export const resolveConfiguredModel = async (
|
|
|
47
51
|
streamSimple: streamSimple.bind(ctx.modelRegistry),
|
|
48
52
|
};
|
|
49
53
|
}
|
|
54
|
+
|
|
50
55
|
const auth = await ctx.modelRegistry.getApiKeyAndHeaders(model);
|
|
56
|
+
|
|
51
57
|
if (!auth.ok) {
|
|
52
58
|
throw new Error(auth.error);
|
|
53
59
|
}
|
|
60
|
+
|
|
54
61
|
if (!auth.apiKey) {
|
|
55
62
|
throw new Error(`No API key for ${ref}`);
|
|
56
63
|
}
|
|
64
|
+
|
|
57
65
|
return {
|
|
58
66
|
apiKey: auth.apiKey,
|
|
59
67
|
env: auth.env,
|
|
@@ -131,12 +139,15 @@ export const createCoalescedUpdate = <T>(
|
|
|
131
139
|
|
|
132
140
|
const publishPending = () => {
|
|
133
141
|
timer = undefined;
|
|
142
|
+
|
|
134
143
|
if (!pending) {
|
|
135
144
|
return;
|
|
136
145
|
}
|
|
146
|
+
|
|
137
147
|
const { value } = pending;
|
|
138
148
|
pending = undefined;
|
|
139
149
|
lastPublishedAt = scheduler.now();
|
|
150
|
+
|
|
140
151
|
try {
|
|
141
152
|
publish(value);
|
|
142
153
|
} catch (error) {
|
|
@@ -152,13 +163,17 @@ export const createCoalescedUpdate = <T>(
|
|
|
152
163
|
lastPublishedAt === undefined
|
|
153
164
|
? intervalMs
|
|
154
165
|
: scheduler.now() - lastPublishedAt;
|
|
166
|
+
|
|
155
167
|
const delay = Math.max(0, intervalMs - elapsed);
|
|
168
|
+
|
|
156
169
|
if (delay === 0) {
|
|
157
170
|
// A zero delay publishes synchronously rather than through the timer,
|
|
158
171
|
// so the first update of a burst lands before the caller continues.
|
|
159
172
|
publishPending();
|
|
173
|
+
|
|
160
174
|
return;
|
|
161
175
|
}
|
|
176
|
+
|
|
162
177
|
timer = scheduler.setTimeout(publishPending, delay);
|
|
163
178
|
timer.unref?.();
|
|
164
179
|
};
|
|
@@ -175,19 +190,24 @@ export const createCoalescedUpdate = <T>(
|
|
|
175
190
|
clearTimer();
|
|
176
191
|
publishPending();
|
|
177
192
|
}
|
|
193
|
+
|
|
178
194
|
return { error: publishError, failed: publishFailed };
|
|
179
195
|
},
|
|
180
196
|
update: (value) => {
|
|
181
197
|
if (closed) {
|
|
182
198
|
return;
|
|
183
199
|
}
|
|
200
|
+
|
|
184
201
|
if (publishFailed) {
|
|
185
202
|
throw publishError;
|
|
186
203
|
}
|
|
204
|
+
|
|
187
205
|
pending = { value };
|
|
206
|
+
|
|
188
207
|
if (timer === undefined) {
|
|
189
208
|
schedule();
|
|
190
209
|
}
|
|
210
|
+
|
|
191
211
|
if (publishFailed) {
|
|
192
212
|
throw publishError;
|
|
193
213
|
}
|
|
@@ -205,12 +225,15 @@ const toSimpleReasoning = (
|
|
|
205
225
|
if (effort === undefined || effort === "off") {
|
|
206
226
|
return undefined;
|
|
207
227
|
}
|
|
228
|
+
|
|
208
229
|
const level = SIMPLE_REASONING_LEVELS.find(
|
|
209
230
|
(candidate) => candidate === effort
|
|
210
231
|
);
|
|
232
|
+
|
|
211
233
|
if (!level) {
|
|
212
234
|
throw new Error(`Unsupported Advisor reasoning level: ${effort}`);
|
|
213
235
|
}
|
|
236
|
+
|
|
214
237
|
return level;
|
|
215
238
|
};
|
|
216
239
|
|
|
@@ -223,12 +246,14 @@ const createTextEventStream = (
|
|
|
223
246
|
messages: options.messages,
|
|
224
247
|
systemPrompt: options.systemPrompt,
|
|
225
248
|
};
|
|
249
|
+
|
|
226
250
|
if (!streamModel && resolved.streamSimple) {
|
|
227
251
|
return resolved.streamSimple(resolved.model, context, {
|
|
228
252
|
reasoning: toSimpleReasoning(options.reasoning),
|
|
229
253
|
signal: options.signal,
|
|
230
254
|
});
|
|
231
255
|
}
|
|
256
|
+
|
|
232
257
|
const streamOptions: NonNullable<Parameters<typeof stream>[2]> = {
|
|
233
258
|
apiKey: resolved.apiKey,
|
|
234
259
|
env: resolved.env,
|
|
@@ -236,9 +261,11 @@ const createTextEventStream = (
|
|
|
236
261
|
reasoning: options.reasoning,
|
|
237
262
|
signal: options.signal,
|
|
238
263
|
};
|
|
264
|
+
|
|
239
265
|
if (options.reasoning !== undefined) {
|
|
240
266
|
streamOptions.reasoningEffort = options.reasoning;
|
|
241
267
|
}
|
|
268
|
+
|
|
242
269
|
return (streamModel ?? stream)(resolved.model, context, streamOptions);
|
|
243
270
|
};
|
|
244
271
|
|
|
@@ -262,14 +289,17 @@ export const collectTextStream = async (
|
|
|
262
289
|
}
|
|
263
290
|
|
|
264
291
|
const response = await eventStream.result();
|
|
292
|
+
|
|
265
293
|
if (options.signal?.aborted) {
|
|
266
294
|
throw options.signal.reason instanceof Error
|
|
267
295
|
? options.signal.reason
|
|
268
296
|
: new Error("Advisor operation cancelled.");
|
|
269
297
|
}
|
|
298
|
+
|
|
270
299
|
const lastAssistant = [response].find(
|
|
271
300
|
(message): message is AssistantMessage => message.role === "assistant"
|
|
272
301
|
);
|
|
302
|
+
|
|
273
303
|
if (
|
|
274
304
|
lastAssistant?.stopReason === "error" ||
|
|
275
305
|
lastAssistant?.stopReason === "aborted"
|
|
@@ -279,6 +309,7 @@ export const collectTextStream = async (
|
|
|
279
309
|
`Advisor stream ended with ${lastAssistant.stopReason}.`
|
|
280
310
|
);
|
|
281
311
|
}
|
|
312
|
+
|
|
282
313
|
const finalText =
|
|
283
314
|
lastAssistant?.content
|
|
284
315
|
.filter(
|
|
@@ -286,6 +317,7 @@ export const collectTextStream = async (
|
|
|
286
317
|
)
|
|
287
318
|
.map((part) => part.text)
|
|
288
319
|
.join("\n") || text;
|
|
320
|
+
|
|
289
321
|
return {
|
|
290
322
|
text: finalText,
|
|
291
323
|
thinking,
|
package/src/outcome-stats.ts
CHANGED
|
@@ -21,6 +21,7 @@ interface OutcomeValidationStats {
|
|
|
21
21
|
total: number;
|
|
22
22
|
unknown: number;
|
|
23
23
|
}
|
|
24
|
+
|
|
24
25
|
export interface OutcomeStats {
|
|
25
26
|
adoption: Record<OutcomeAdoption, number>;
|
|
26
27
|
byTrigger: Record<OutcomeTrigger, number>;
|
|
@@ -31,6 +32,7 @@ export interface OutcomeStats {
|
|
|
31
32
|
total: number;
|
|
32
33
|
validationByAdoption: Record<OutcomeAdoption, OutcomeValidationStats>;
|
|
33
34
|
}
|
|
35
|
+
|
|
34
36
|
export interface OutcomeLogRead {
|
|
35
37
|
malformedLines: number;
|
|
36
38
|
missing: boolean;
|
|
@@ -40,6 +42,7 @@ export interface OutcomeLogRead {
|
|
|
40
42
|
const ADOPTION_SET = new Set<string>(ADOPTIONS);
|
|
41
43
|
const VALIDATION_SET = new Set<string>(VALIDATIONS);
|
|
42
44
|
const TRIGGER_SET = new Set<string>(OUTCOME_TRIGGERS);
|
|
45
|
+
|
|
43
46
|
const isErrnoException = (error: unknown): error is NodeJS.ErrnoException =>
|
|
44
47
|
error instanceof Error && "code" in error;
|
|
45
48
|
|
|
@@ -59,12 +62,15 @@ const isOutcomeRecord = (value: unknown): value is OutcomeRecord =>
|
|
|
59
62
|
export const parseOutcomeLog = (text: string): OutcomeLogRead => {
|
|
60
63
|
const records: OutcomeRecord[] = [];
|
|
61
64
|
let malformedLines = 0;
|
|
65
|
+
|
|
62
66
|
for (const line of text.split(/\r?\n/u)) {
|
|
63
67
|
if (!line.trim()) {
|
|
64
68
|
continue;
|
|
65
69
|
}
|
|
70
|
+
|
|
66
71
|
try {
|
|
67
72
|
const value: unknown = JSON.parse(line);
|
|
73
|
+
|
|
68
74
|
if (isOutcomeRecord(value)) {
|
|
69
75
|
records.push(value);
|
|
70
76
|
} else {
|
|
@@ -74,6 +80,7 @@ export const parseOutcomeLog = (text: string): OutcomeLogRead => {
|
|
|
74
80
|
malformedLines += 1;
|
|
75
81
|
}
|
|
76
82
|
}
|
|
83
|
+
|
|
77
84
|
return { malformedLines, missing: false, records };
|
|
78
85
|
};
|
|
79
86
|
|
|
@@ -86,6 +93,7 @@ export const readOutcomeLog = async (
|
|
|
86
93
|
if (isErrnoException(error) && error.code === "ENOENT") {
|
|
87
94
|
return { malformedLines: 0, missing: true, records: [] };
|
|
88
95
|
}
|
|
96
|
+
|
|
89
97
|
throw error;
|
|
90
98
|
}
|
|
91
99
|
};
|
|
@@ -106,14 +114,17 @@ export const aggregateOutcomeRecords = (
|
|
|
106
114
|
const adoption = Object.fromEntries(
|
|
107
115
|
ADOPTIONS.map((value) => [value, 0])
|
|
108
116
|
) as Record<OutcomeAdoption, number>;
|
|
117
|
+
|
|
109
118
|
// SAFETY: OUTCOME_TRIGGERS enumerates every key in the returned trigger record.
|
|
110
119
|
const byTrigger = Object.fromEntries(
|
|
111
120
|
OUTCOME_TRIGGERS.map((value) => [value, 0])
|
|
112
121
|
) as Record<OutcomeTrigger, number>;
|
|
122
|
+
|
|
113
123
|
// SAFETY: ADOPTIONS enumerates every key and each value gets a fresh stats object.
|
|
114
124
|
const validationByAdoption = Object.fromEntries(
|
|
115
125
|
ADOPTIONS.map((value) => [value, emptyValidationStats()])
|
|
116
126
|
) as Record<OutcomeAdoption, OutcomeValidationStats>;
|
|
127
|
+
|
|
117
128
|
const adviceHashes = new Set<string>();
|
|
118
129
|
let first: { timestamp: string; time: number } | undefined;
|
|
119
130
|
let last: { timestamp: string; time: number } | undefined;
|
|
@@ -128,19 +139,24 @@ export const aggregateOutcomeRecords = (
|
|
|
128
139
|
record.validationStatus === "not-run" ? "notRun" : record.validationStatus
|
|
129
140
|
] += 1;
|
|
130
141
|
const time = Date.parse(record.timestamp);
|
|
142
|
+
|
|
131
143
|
if (!first || time < first.time) {
|
|
132
144
|
first = { time, timestamp: record.timestamp };
|
|
133
145
|
}
|
|
146
|
+
|
|
134
147
|
if (!last || time > last.time) {
|
|
135
148
|
last = { time, timestamp: record.timestamp };
|
|
136
149
|
}
|
|
137
150
|
}
|
|
151
|
+
|
|
138
152
|
for (const value of Object.values(validationByAdoption)) {
|
|
139
153
|
const validated = value.passed + value.failed;
|
|
154
|
+
|
|
140
155
|
if (validated > 0) {
|
|
141
156
|
value.passRate = value.passed / validated;
|
|
142
157
|
}
|
|
143
158
|
}
|
|
159
|
+
|
|
144
160
|
return {
|
|
145
161
|
adoption,
|
|
146
162
|
byTrigger,
|
package/src/outcomes.ts
CHANGED
|
@@ -16,29 +16,35 @@ import { setTimeout as sleep } from "node:timers/promises";
|
|
|
16
16
|
import { getAgentDir } from "@earendil-works/pi-coding-agent";
|
|
17
17
|
|
|
18
18
|
export type OutcomeAdoption = "followed" | "not-followed" | "unknown";
|
|
19
|
+
|
|
19
20
|
export type OutcomeValidation = "passed" | "failed" | "not-run" | "unknown";
|
|
21
|
+
|
|
20
22
|
export type OutcomeTrigger =
|
|
21
23
|
| "manual"
|
|
22
24
|
| "executor-requested"
|
|
23
25
|
| "turn-gate"
|
|
24
26
|
| "repeated-tool-call";
|
|
27
|
+
|
|
25
28
|
export const ADOPTIONS: OutcomeAdoption[] = [
|
|
26
29
|
"followed",
|
|
27
30
|
"not-followed",
|
|
28
31
|
"unknown",
|
|
29
32
|
];
|
|
33
|
+
|
|
30
34
|
export const VALIDATIONS: OutcomeValidation[] = [
|
|
31
35
|
"passed",
|
|
32
36
|
"failed",
|
|
33
37
|
"not-run",
|
|
34
38
|
"unknown",
|
|
35
39
|
];
|
|
40
|
+
|
|
36
41
|
export const OUTCOME_TRIGGERS: OutcomeTrigger[] = [
|
|
37
42
|
"manual",
|
|
38
43
|
"executor-requested",
|
|
39
44
|
"turn-gate",
|
|
40
45
|
"repeated-tool-call",
|
|
41
46
|
];
|
|
47
|
+
|
|
42
48
|
export interface OutcomeRecord {
|
|
43
49
|
adoption: OutcomeAdoption;
|
|
44
50
|
adviceHash: string;
|
|
@@ -47,9 +53,11 @@ export interface OutcomeRecord {
|
|
|
47
53
|
v: 1;
|
|
48
54
|
validationStatus: OutcomeValidation;
|
|
49
55
|
}
|
|
56
|
+
|
|
50
57
|
const MAX_LOG_BYTES = 1024 * 1024;
|
|
51
58
|
export const OUTCOME_LOG_MAX_BYTES = MAX_LOG_BYTES;
|
|
52
59
|
const statePath = () => join(getAgentDir(), "advisor-outcomes-salt");
|
|
60
|
+
|
|
53
61
|
export const outcomeLogPath = () =>
|
|
54
62
|
join(getAgentDir(), "advisor-outcomes.jsonl");
|
|
55
63
|
|
|
@@ -59,13 +67,16 @@ const isErrnoException = (error: unknown): error is NodeJS.ErrnoException =>
|
|
|
59
67
|
const salt = async () => {
|
|
60
68
|
const path = statePath();
|
|
61
69
|
await mkdir(getAgentDir(), { mode: 0o700, recursive: true });
|
|
70
|
+
|
|
62
71
|
for (let attempt = 0; attempt < 20; attempt += 1) {
|
|
63
72
|
try {
|
|
64
73
|
// Contenders retry sequentially until one salt is atomically published.
|
|
65
74
|
const existing = await readFile(path);
|
|
75
|
+
|
|
66
76
|
if (existing.length === 32) {
|
|
67
77
|
return existing;
|
|
68
78
|
}
|
|
79
|
+
|
|
69
80
|
// Recover a salt file left incomplete by an interrupted older writer.
|
|
70
81
|
await unlink(path);
|
|
71
82
|
} catch (error) {
|
|
@@ -73,11 +84,14 @@ const salt = async () => {
|
|
|
73
84
|
throw error;
|
|
74
85
|
}
|
|
75
86
|
}
|
|
87
|
+
|
|
76
88
|
const value = randomBytes(32);
|
|
77
89
|
const temporary = `${path}.${process.pid}.${randomBytes(8).toString("hex")}.${attempt}`;
|
|
78
90
|
await writeFile(temporary, value, { mode: 0o600 });
|
|
91
|
+
|
|
79
92
|
try {
|
|
80
93
|
await link(temporary, path);
|
|
94
|
+
|
|
81
95
|
return value;
|
|
82
96
|
} catch (error) {
|
|
83
97
|
if (!isErrnoException(error) || error.code !== "EEXIST") {
|
|
@@ -87,8 +101,10 @@ const salt = async () => {
|
|
|
87
101
|
await unlink(temporary).catch(() => undefined);
|
|
88
102
|
}
|
|
89
103
|
}
|
|
104
|
+
|
|
90
105
|
throw new Error("Advisor outcome salt initialization did not complete.");
|
|
91
106
|
};
|
|
107
|
+
|
|
92
108
|
const sameFile = (
|
|
93
109
|
left: { dev: number | bigint; ino: number | bigint },
|
|
94
110
|
right: { dev: number | bigint; ino: number | bigint }
|
|
@@ -96,16 +112,19 @@ const sameFile = (
|
|
|
96
112
|
|
|
97
113
|
const withOutcomeLock = async <T>(run: () => Promise<T>): Promise<T> => {
|
|
98
114
|
const lockPath = `${outcomeLogPath()}.lock`;
|
|
115
|
+
|
|
99
116
|
for (let attempt = 0; attempt < 200; attempt += 1) {
|
|
100
117
|
try {
|
|
101
118
|
// Lock acquisition must retry sequentially across processes.
|
|
102
119
|
const lock = await open(lockPath, "wx", 0o600);
|
|
103
120
|
const identity = await lock.stat();
|
|
121
|
+
|
|
104
122
|
try {
|
|
105
123
|
return await run();
|
|
106
124
|
} finally {
|
|
107
125
|
await lock.close();
|
|
108
126
|
const current = await stat(lockPath).catch(() => undefined);
|
|
127
|
+
|
|
109
128
|
if (current && sameFile(identity, current)) {
|
|
110
129
|
await unlink(lockPath).catch(() => undefined);
|
|
111
130
|
}
|
|
@@ -114,17 +133,23 @@ const withOutcomeLock = async <T>(run: () => Promise<T>): Promise<T> => {
|
|
|
114
133
|
if (!isErrnoException(error) || error.code !== "EEXIST") {
|
|
115
134
|
throw error;
|
|
116
135
|
}
|
|
136
|
+
|
|
117
137
|
const observed = await stat(lockPath).catch(() => undefined);
|
|
138
|
+
|
|
118
139
|
if (observed && Date.now() - observed.mtimeMs > 30_000) {
|
|
119
140
|
const current = await stat(lockPath).catch(() => undefined);
|
|
141
|
+
|
|
120
142
|
if (current && sameFile(observed, current)) {
|
|
121
143
|
await unlink(lockPath).catch(() => undefined);
|
|
122
144
|
}
|
|
145
|
+
|
|
123
146
|
continue;
|
|
124
147
|
}
|
|
148
|
+
|
|
125
149
|
await sleep(5);
|
|
126
150
|
}
|
|
127
151
|
}
|
|
152
|
+
|
|
128
153
|
throw new Error("Timed out waiting to append an Advisor outcome.");
|
|
129
154
|
};
|
|
130
155
|
|
|
@@ -139,6 +164,7 @@ export const appendOutcome = async (
|
|
|
139
164
|
) => {
|
|
140
165
|
const path = outcomeLogPath();
|
|
141
166
|
await mkdir(getAgentDir(), { mode: 0o700, recursive: true });
|
|
167
|
+
|
|
142
168
|
return withOutcomeLock(async () => {
|
|
143
169
|
const next: OutcomeRecord = {
|
|
144
170
|
adoption: record.adoption,
|
|
@@ -148,20 +174,25 @@ export const appendOutcome = async (
|
|
|
148
174
|
v: 1,
|
|
149
175
|
validationStatus: record.validationStatus,
|
|
150
176
|
};
|
|
177
|
+
|
|
151
178
|
const line = `${JSON.stringify(next)}\n`;
|
|
179
|
+
|
|
152
180
|
const currentBytes = await stat(path)
|
|
153
181
|
.then((value) => value.size)
|
|
154
182
|
.catch((error: NodeJS.ErrnoException) => {
|
|
155
183
|
if (error.code === "ENOENT") {
|
|
156
184
|
return 0;
|
|
157
185
|
}
|
|
186
|
+
|
|
158
187
|
throw error;
|
|
159
188
|
});
|
|
189
|
+
|
|
160
190
|
const overflow = currentBytes + Buffer.byteLength(line) > MAX_LOG_BYTES;
|
|
161
191
|
await (overflow
|
|
162
192
|
? writeFile(path, line, { encoding: "utf-8", mode: 0o600 })
|
|
163
193
|
: appendFile(path, line, { encoding: "utf-8", mode: 0o600 }));
|
|
164
194
|
await chmod(path, 0o600);
|
|
195
|
+
|
|
165
196
|
return next;
|
|
166
197
|
});
|
|
167
198
|
};
|
package/src/pi-settings.ts
CHANGED
|
@@ -18,6 +18,7 @@ const cache = new Map<string, CachedSetting>();
|
|
|
18
18
|
const settingsIdentity = (path: string): string => {
|
|
19
19
|
try {
|
|
20
20
|
const stats = statSync(path, { bigint: true });
|
|
21
|
+
|
|
21
22
|
return `${stats.mtimeNs}:${stats.ctimeNs}:${stats.size}:${stats.ino}`;
|
|
22
23
|
} catch {
|
|
23
24
|
return `unstattable:${process.hrtime.bigint()}`;
|
|
@@ -27,6 +28,7 @@ const settingsIdentity = (path: string): string => {
|
|
|
27
28
|
const readHideThinking = (path: string): boolean => {
|
|
28
29
|
try {
|
|
29
30
|
const parsed: unknown = JSON.parse(readFileSync(path, "utf-8"));
|
|
31
|
+
|
|
30
32
|
return isRecord(parsed) && parsed.hideThinkingBlock === true;
|
|
31
33
|
} catch {
|
|
32
34
|
return false;
|
|
@@ -36,19 +38,26 @@ const readHideThinking = (path: string): boolean => {
|
|
|
36
38
|
/** Whether Pi's `hide_thinking` setting hides rendered thinking blocks. */
|
|
37
39
|
export const piHideThinkingEnabled = (): boolean => {
|
|
38
40
|
const path = join(getAgentDir(), "settings.json");
|
|
41
|
+
|
|
39
42
|
if (!existsSync(path)) {
|
|
40
43
|
return false;
|
|
41
44
|
}
|
|
45
|
+
|
|
42
46
|
const now = Date.now();
|
|
43
47
|
const cached = cache.get(path);
|
|
48
|
+
|
|
44
49
|
if (cached && now - cached.readAt < SETTING_RECHECK_INTERVAL_MS) {
|
|
45
50
|
return cached.hidden;
|
|
46
51
|
}
|
|
52
|
+
|
|
47
53
|
const identity = settingsIdentity(path);
|
|
54
|
+
|
|
48
55
|
const hidden =
|
|
49
56
|
cached && cached.identity === identity
|
|
50
57
|
? cached.hidden
|
|
51
58
|
: readHideThinking(path);
|
|
59
|
+
|
|
52
60
|
cache.set(path, { hidden, identity, readAt: now });
|
|
61
|
+
|
|
53
62
|
return hidden;
|
|
54
63
|
};
|
package/src/preferences.ts
CHANGED
|
@@ -8,9 +8,11 @@ import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
|
8
8
|
import { redactAndCapText } from "./redaction.ts";
|
|
9
9
|
|
|
10
10
|
const PREFERENCES_MAX_BYTES = 8 * 1024;
|
|
11
|
+
|
|
11
12
|
// Keep local filename components separate from Socket's URL-string heuristic.
|
|
12
13
|
const PREFERENCES_FILENAME = ["advisor-preferences", "md"].join(".");
|
|
13
14
|
const AGENTS_FILENAME = "AGENTS.md";
|
|
15
|
+
|
|
14
16
|
export interface TextAttachment {
|
|
15
17
|
bytes: number;
|
|
16
18
|
text: string;
|
|
@@ -25,6 +27,7 @@ export interface ProjectRulesAttachment {
|
|
|
25
27
|
|
|
26
28
|
const inside = (root: string, candidate: string) => {
|
|
27
29
|
const path = relative(root, candidate);
|
|
30
|
+
|
|
28
31
|
return (
|
|
29
32
|
path === "" ||
|
|
30
33
|
(path !== ".." && !path.startsWith(`..${sep}`) && !isAbsolute(path))
|
|
@@ -34,17 +37,22 @@ const inside = (root: string, candidate: string) => {
|
|
|
34
37
|
const hasSymlinkBelow = async (rootCandidate: string, candidate: string) => {
|
|
35
38
|
const root = resolve(rootCandidate);
|
|
36
39
|
const target = resolve(candidate);
|
|
40
|
+
|
|
37
41
|
if (!inside(root, target)) {
|
|
38
42
|
return true;
|
|
39
43
|
}
|
|
44
|
+
|
|
40
45
|
let current = root;
|
|
46
|
+
|
|
41
47
|
for (const part of relative(root, target).split(sep).filter(Boolean)) {
|
|
42
48
|
current = join(current, part);
|
|
43
49
|
const stats = await lstat(current);
|
|
50
|
+
|
|
44
51
|
if (stats.isSymbolicLink()) {
|
|
45
52
|
return true;
|
|
46
53
|
}
|
|
47
54
|
}
|
|
55
|
+
|
|
48
56
|
return false;
|
|
49
57
|
};
|
|
50
58
|
|
|
@@ -57,28 +65,39 @@ const readTrustedText = async (
|
|
|
57
65
|
if (maxBytes <= 0) {
|
|
58
66
|
return;
|
|
59
67
|
}
|
|
68
|
+
|
|
60
69
|
try {
|
|
61
70
|
const root = await realpath(rootCandidate);
|
|
71
|
+
|
|
62
72
|
if (await hasSymlinkBelow(rootCandidate, candidate)) {
|
|
63
73
|
return;
|
|
64
74
|
}
|
|
75
|
+
|
|
65
76
|
const stats = await lstat(candidate);
|
|
77
|
+
|
|
66
78
|
if (stats.isSymbolicLink() || !stats.isFile()) {
|
|
67
79
|
return;
|
|
68
80
|
}
|
|
81
|
+
|
|
69
82
|
const resolved = await realpath(candidate);
|
|
83
|
+
|
|
70
84
|
if (!inside(root, resolved)) {
|
|
71
85
|
return;
|
|
72
86
|
}
|
|
87
|
+
|
|
73
88
|
if (await hasSymlinkBelow(rootCandidate, candidate)) {
|
|
74
89
|
return;
|
|
75
90
|
}
|
|
91
|
+
|
|
76
92
|
const flags = constants.O_NOFOLLOW
|
|
77
93
|
? constants.O_RDONLY + constants.O_NOFOLLOW
|
|
78
94
|
: constants.O_RDONLY;
|
|
95
|
+
|
|
79
96
|
const file = await open(resolved, flags);
|
|
97
|
+
|
|
80
98
|
try {
|
|
81
99
|
const opened = await file.stat();
|
|
100
|
+
|
|
82
101
|
if (
|
|
83
102
|
!opened.isFile() ||
|
|
84
103
|
opened.dev !== stats.dev ||
|
|
@@ -86,10 +105,12 @@ const readTrustedText = async (
|
|
|
86
105
|
) {
|
|
87
106
|
return;
|
|
88
107
|
}
|
|
108
|
+
|
|
89
109
|
const buffer = Buffer.alloc(maxBytes + 1);
|
|
90
110
|
const { bytesRead } = await file.read(buffer, 0, buffer.length, 0);
|
|
91
111
|
const source = buffer.subarray(0, bytesRead).toString("utf-8");
|
|
92
112
|
const capped = redactAndCapText(source, maxBytes, redact);
|
|
113
|
+
|
|
93
114
|
return { bytes: Buffer.byteLength(capped, "utf-8"), text: capped };
|
|
94
115
|
} finally {
|
|
95
116
|
await file.close();
|
|
@@ -107,6 +128,7 @@ export const readProjectPreferences = (
|
|
|
107
128
|
if (!ctx.isProjectTrusted()) {
|
|
108
129
|
return Promise.resolve(undefined);
|
|
109
130
|
}
|
|
131
|
+
|
|
110
132
|
return readTrustedText(
|
|
111
133
|
join(ctx.cwd, ".pi", PREFERENCES_FILENAME),
|
|
112
134
|
ctx.cwd,
|
|
@@ -123,19 +145,23 @@ export const readProjectRules = async (
|
|
|
123
145
|
if (!ctx.isProjectTrusted()) {
|
|
124
146
|
return { bytes: 0, withheldReason: "untrusted" };
|
|
125
147
|
}
|
|
148
|
+
|
|
126
149
|
const project = await readTrustedText(
|
|
127
150
|
join(ctx.cwd, AGENTS_FILENAME),
|
|
128
151
|
ctx.cwd,
|
|
129
152
|
maxBytes,
|
|
130
153
|
redact
|
|
131
154
|
);
|
|
155
|
+
|
|
132
156
|
const remaining = Math.max(0, maxBytes * 2 - (project?.bytes ?? 0));
|
|
157
|
+
|
|
133
158
|
const global = await readTrustedText(
|
|
134
159
|
join(getAgentDir(), AGENTS_FILENAME),
|
|
135
160
|
getAgentDir(),
|
|
136
161
|
Math.min(maxBytes, remaining),
|
|
137
162
|
redact
|
|
138
163
|
);
|
|
164
|
+
|
|
139
165
|
return {
|
|
140
166
|
bytes: (project?.bytes ?? 0) + (global?.bytes ?? 0),
|
|
141
167
|
global,
|
package/src/redaction.ts
CHANGED
|
@@ -4,10 +4,12 @@ const REDACTION_MARKER = "[REDACTED SECRET]";
|
|
|
4
4
|
const PEM_BEGIN_PATTERN = /-----BEGIN(?: [A-Z0-9]+)? PRIVATE KEY-----/giu;
|
|
5
5
|
const PEM_END_PATTERN = /-----END(?: [A-Z0-9]+)? PRIVATE KEY-----/iu;
|
|
6
6
|
const CREDENTIAL_NAME = String.raw`(?:api[_-]?key|token|secret|password|passwd)`;
|
|
7
|
+
|
|
7
8
|
const SECRET_ASSIGNMENT_PATTERN = new RegExp(
|
|
8
9
|
String.raw`(?:(?:"(?:[a-z0-9]+[_-])*?${CREDENTIAL_NAME}"|'(?:[a-z0-9]+[_-])*?${CREDENTIAL_NAME}')|(?:\b|_)${CREDENTIAL_NAME})[ \t]*[:=][ \t]*(?:"(?:\\.|[^"])*"|'(?:\\.|[^'])*'|"(?:\\.|[^"\r\n])*(?=\r?\n|$)|'(?:\\.|[^'\r\n])*(?=\r?\n|$)|[^\s"'&,;)}\]]+)`,
|
|
9
10
|
"giu"
|
|
10
11
|
);
|
|
12
|
+
|
|
11
13
|
const SECRET_PATTERNS = [
|
|
12
14
|
/-----BEGIN(?: [A-Z0-9]+)? PRIVATE KEY-----[\s\S]*?-----END(?: [A-Z0-9]+)? PRIVATE KEY-----/giu,
|
|
13
15
|
/\bBearer\s+[A-Za-z0-9._~+/=-]{8,}/giu,
|
|
@@ -20,12 +22,15 @@ const SECRET_PATTERNS = [
|
|
|
20
22
|
const redactUnterminatedPem = (value: string): string => {
|
|
21
23
|
const begins = [...value.matchAll(PEM_BEGIN_PATTERN)];
|
|
22
24
|
const lastBegin = begins.at(-1);
|
|
25
|
+
|
|
23
26
|
if (lastBegin?.index === undefined) {
|
|
24
27
|
return value;
|
|
25
28
|
}
|
|
29
|
+
|
|
26
30
|
const hasEnd = PEM_END_PATTERN.test(
|
|
27
31
|
value.slice(lastBegin.index + lastBegin[0].length)
|
|
28
32
|
);
|
|
33
|
+
|
|
29
34
|
return hasEnd
|
|
30
35
|
? value
|
|
31
36
|
: `${value.slice(0, lastBegin.index)}${REDACTION_MARKER}`;
|
|
@@ -34,11 +39,13 @@ const redactUnterminatedPem = (value: string): string => {
|
|
|
34
39
|
/** Redacts common credential forms locally; it is not a data-classification system. */
|
|
35
40
|
export const redactSecrets = (value: string): string => {
|
|
36
41
|
let redacted = redactUnterminatedPem(value);
|
|
42
|
+
|
|
37
43
|
for (const pattern of SECRET_PATTERNS) {
|
|
38
44
|
redacted = redacted.replace(pattern, (_match, scheme) =>
|
|
39
45
|
isString(scheme) ? `${scheme}${REDACTION_MARKER}@` : REDACTION_MARKER
|
|
40
46
|
);
|
|
41
47
|
}
|
|
48
|
+
|
|
42
49
|
return redacted;
|
|
43
50
|
};
|
|
44
51
|
|
|
@@ -49,5 +56,6 @@ export const redactAndCapText = (
|
|
|
49
56
|
redact = true
|
|
50
57
|
): string => {
|
|
51
58
|
const source = redact ? redactSecrets(value) : value;
|
|
59
|
+
|
|
52
60
|
return capUtf8Bytes(source, maxBytes);
|
|
53
61
|
};
|