@fiale-plus/pi-rogue 0.2.0 → 0.2.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/node_modules/@fiale-plus/pi-rogue-advisor/src/extension.ts +75 -31
- package/node_modules/@fiale-plus/pi-rogue-advisor/src/loop-convergence.test.ts +2 -2
- package/node_modules/@fiale-plus/pi-rogue-advisor/src/state-versioning.test.ts +25 -4
- package/node_modules/@fiale-plus/pi-rogue-orchestration/src/advisor-checkins.test.ts +10 -0
- package/node_modules/@fiale-plus/pi-rogue-orchestration/src/advisor-checkins.ts +17 -2
- package/node_modules/@fiale-plus/pi-rogue-orchestration/src/goal.ts +2 -2
- package/node_modules/@fiale-plus/pi-rogue-orchestration/src/internal.ts +11 -2
- package/package.json +1 -1
|
@@ -5,7 +5,7 @@ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
|
5
5
|
import { Box, Text } from "@earendil-works/pi-tui";
|
|
6
6
|
import { completeSimple, type ThinkingLevel } from "@earendil-works/pi-ai";
|
|
7
7
|
import { Type } from "typebox";
|
|
8
|
-
import { featureFile, readText, truncate, writeText, atomicWriteText } from "./internal.js";
|
|
8
|
+
import { featureDir, featureFile, readText, truncate, writeText, atomicWriteText } from "./internal.js";
|
|
9
9
|
import { advisorArgumentCompletions, piRogueArgumentCompletions } from "./completions.js";
|
|
10
10
|
import {
|
|
11
11
|
appendRouteLog,
|
|
@@ -46,10 +46,10 @@ const DEFAULT_CONFIG: AdvisorConfig = {
|
|
|
46
46
|
};
|
|
47
47
|
|
|
48
48
|
const CONFIG_PATH = featureFile("advisor", "config.json");
|
|
49
|
-
const
|
|
49
|
+
const LEGACY_STATE_PATH = featureFile("advisor", "state.json");
|
|
50
50
|
const CACHE_PATH = featureFile("advisor", "cache.json");
|
|
51
|
-
const CURRENT_PATH = featureFile("advisor", "current.md");
|
|
52
51
|
const HISTORY_PATH = featureFile("advisor", "history.jsonl");
|
|
52
|
+
const SESSION_STATE_PROP = "__piRogueAdvisorStatePath";
|
|
53
53
|
const ORCHESTRATION_DIR = join(homedir(), ".pi", "agent", "fiale-plus", "orchestration");
|
|
54
54
|
|
|
55
55
|
const MAX_CACHE = 64;
|
|
@@ -169,8 +169,47 @@ function saveConfig(c: AdvisorConfig) {
|
|
|
169
169
|
writeJson(CONFIG_PATH, c);
|
|
170
170
|
}
|
|
171
171
|
|
|
172
|
-
function
|
|
173
|
-
const
|
|
172
|
+
function advisorSessionDir(ctxOrKey?: any): string {
|
|
173
|
+
const key = typeof ctxOrKey === "string" ? ctxOrKey : sessionKey(ctxOrKey);
|
|
174
|
+
return join(featureDir("advisor"), "sessions", safeSessionKey(key));
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export function advisorSessionStatePath(ctxOrKey?: any): string {
|
|
178
|
+
return join(advisorSessionDir(ctxOrKey), "state.json");
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function advisorCurrentPath(ctxOrKey?: any): string {
|
|
182
|
+
return join(advisorSessionDir(ctxOrKey), "current.md");
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function safeSessionKey(key: string): string {
|
|
186
|
+
const safe = String(key || "session").replace(/[^A-Za-z0-9._-]+/g, "-").replace(/^-+|-+$/g, "");
|
|
187
|
+
return safe || "session";
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function statePathFor(state: SessionState): string {
|
|
191
|
+
return String((state as any)[SESSION_STATE_PROP] || LEGACY_STATE_PATH);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function attachStatePath<T extends SessionState>(state: T, path: string): T {
|
|
195
|
+
Object.defineProperty(state, SESSION_STATE_PROP, {
|
|
196
|
+
value: path,
|
|
197
|
+
enumerable: false,
|
|
198
|
+
configurable: true,
|
|
199
|
+
writable: true,
|
|
200
|
+
});
|
|
201
|
+
return state;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function loadState(ctxOrKey?: any): SessionState {
|
|
205
|
+
// Do not fall back to LEGACY_STATE_PATH here: that file was unscoped and is
|
|
206
|
+
// the source of issue #103 context bleed. New/resumed sessions must only load
|
|
207
|
+
// their own namespaced mutable advisor state.
|
|
208
|
+
return loadStateFromPath(advisorSessionStatePath(ctxOrKey));
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function loadStateFromPath(path: string): SessionState {
|
|
212
|
+
const raw = readJson<Partial<SessionState>>(path, {});
|
|
174
213
|
// Handle state versioning: migrate old versions to current
|
|
175
214
|
const version = raw._v ?? 0;
|
|
176
215
|
if (version < STATE_VERSION) {
|
|
@@ -181,7 +220,7 @@ function loadState(): SessionState {
|
|
|
181
220
|
}
|
|
182
221
|
const control = raw.reviewControl;
|
|
183
222
|
const pauseUntil = Number(raw.advisorPauseUntilTurn);
|
|
184
|
-
return {
|
|
223
|
+
return attachStatePath({
|
|
185
224
|
_v: STATE_VERSION,
|
|
186
225
|
turns: raw.turns ?? 0,
|
|
187
226
|
lastTask: raw.lastTask ?? "",
|
|
@@ -217,11 +256,11 @@ function loadState(): SessionState {
|
|
|
217
256
|
lastAppliedAt: control?.lastAppliedAt,
|
|
218
257
|
},
|
|
219
258
|
advisorPauseUntilTurn: Number.isFinite(pauseUntil) ? pauseUntil : undefined,
|
|
220
|
-
};
|
|
259
|
+
}, path);
|
|
221
260
|
}
|
|
222
261
|
|
|
223
262
|
function saveState(s: SessionState) {
|
|
224
|
-
atomicWriteText(
|
|
263
|
+
atomicWriteText(statePathFor(s), JSON.stringify(s, null, 2) + "\n");
|
|
225
264
|
}
|
|
226
265
|
|
|
227
266
|
function loadCache(): Record<string, string> {
|
|
@@ -489,7 +528,7 @@ function markReviewApplied(state: SessionState, signature: string, trigger: stri
|
|
|
489
528
|
}
|
|
490
529
|
|
|
491
530
|
function persistReviewState(state: SessionState, includeReviewRoute: boolean): void {
|
|
492
|
-
const persisted =
|
|
531
|
+
const persisted = loadStateFromPath(statePathFor(state));
|
|
493
532
|
persisted.reviewControl = state.reviewControl;
|
|
494
533
|
persisted.followUp = state.followUp;
|
|
495
534
|
persisted.followUpTask = state.followUpTask;
|
|
@@ -771,8 +810,12 @@ function mergeRouteReview(configReview: AdvisorConfig["review"], route?: ReviewP
|
|
|
771
810
|
|
|
772
811
|
function sessionKey(ctx: any): string {
|
|
773
812
|
const sessionFile = ctx?.sessionManager?.getSessionFile?.();
|
|
774
|
-
if (
|
|
775
|
-
|
|
813
|
+
if (typeof sessionFile === "string" && sessionFile.length > 0) {
|
|
814
|
+
return safeSessionKey(basename(String(sessionFile)).replace(/\.[^.]+$/, ""));
|
|
815
|
+
}
|
|
816
|
+
const sessionId = ctx?.session?.id || process.env.PI_ROGUE_SESSION_ID;
|
|
817
|
+
if (typeof sessionId === "string" && sessionId.length > 0) return safeSessionKey(sessionId);
|
|
818
|
+
return "session";
|
|
776
819
|
}
|
|
777
820
|
|
|
778
821
|
type OrchestrationSnapshot = {
|
|
@@ -846,12 +889,13 @@ function checkinDescription(config: AdvisorConfig): string {
|
|
|
846
889
|
return `checkins ${config.checkinIntervalMinutes}m`;
|
|
847
890
|
}
|
|
848
891
|
|
|
849
|
-
function setPiRogueStatus(ctx: any, config = loadConfig(), state
|
|
892
|
+
function setPiRogueStatus(ctx: any, config = loadConfig(), state?: SessionState): void {
|
|
893
|
+
const currentState = state ?? loadState(ctx);
|
|
850
894
|
const normalized = normalizeAdvisorConfig(config);
|
|
851
895
|
const checkin = checkinDescription(normalized);
|
|
852
|
-
const pause = advisorPauseRemaining(
|
|
896
|
+
const pause = advisorPauseRemaining(currentState, currentState.turns);
|
|
853
897
|
const pauseText = pause > 0 ? ` · pause ${pause} turn${pause === 1 ? "" : "s"}` : "";
|
|
854
|
-
const last =
|
|
898
|
+
const last = currentState.checkin.lastAt ? ` · last ${new Date(currentState.checkin.lastAt).toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" })}` : "";
|
|
855
899
|
ctx.ui.setStatus("pi-rogue", `☠︎ advisor ${normalized.mode}/${normalized.review} · ${checkin}${pauseText}${last}`);
|
|
856
900
|
}
|
|
857
901
|
|
|
@@ -896,7 +940,7 @@ async function maybeAdvisorCheckin(pi: ExtensionAPI, ctx: any, source: string):
|
|
|
896
940
|
if (checkinLocks.has(key)) return false;
|
|
897
941
|
|
|
898
942
|
const config = loadConfig();
|
|
899
|
-
const state = loadState();
|
|
943
|
+
const state = loadState(ctx);
|
|
900
944
|
const reason = shouldRunCheckin(config, state, Date.now(), Date.now());
|
|
901
945
|
if (!reason) {
|
|
902
946
|
if (state.checkin.queued) {
|
|
@@ -935,7 +979,7 @@ async function maybeAdvisorCheckin(pi: ExtensionAPI, ctx: any, source: string):
|
|
|
935
979
|
);
|
|
936
980
|
if (!completed) return false;
|
|
937
981
|
|
|
938
|
-
const next = loadState();
|
|
982
|
+
const next = loadState(ctx);
|
|
939
983
|
next.checkin = {
|
|
940
984
|
lastAt: new Date().toISOString(),
|
|
941
985
|
lastTurn: next.turns,
|
|
@@ -1056,7 +1100,7 @@ export async function completeWithHigherAdvisorModel(
|
|
|
1056
1100
|
|
|
1057
1101
|
async function askAdvisor(pi: ExtensionAPI, ctx: any, question: string, scope: string, includeWork: boolean) {
|
|
1058
1102
|
const config = loadConfig();
|
|
1059
|
-
const state = loadState();
|
|
1103
|
+
const state = loadState(ctx);
|
|
1060
1104
|
if (!question.trim()) return { text: "Ask a question.", error: "empty" };
|
|
1061
1105
|
|
|
1062
1106
|
const brokerBrief = includeWork ? contextBrokerBrief(pi) : "";
|
|
@@ -1085,7 +1129,7 @@ async function askAdvisor(pi: ExtensionAPI, ctx: any, question: string, scope: s
|
|
|
1085
1129
|
async function doReview(pi: ExtensionAPI, ctx: any, trigger: string, delta: string, meta: ReviewMaterialMeta) {
|
|
1086
1130
|
const config = loadConfig();
|
|
1087
1131
|
if (config.review === "off") return;
|
|
1088
|
-
const state = loadState();
|
|
1132
|
+
const state = loadState(ctx);
|
|
1089
1133
|
|
|
1090
1134
|
const signature = reviewMaterialSignature(state, delta, meta);
|
|
1091
1135
|
if (state.reviewControl.running) {
|
|
@@ -1252,7 +1296,7 @@ async function doReview(pi: ExtensionAPI, ctx: any, trigger: string, delta: stri
|
|
|
1252
1296
|
finalReason = (parsed.reason || parsed.summary || "review result").slice(0, 120);
|
|
1253
1297
|
|
|
1254
1298
|
const display = formatAdvisorDisplay("advisor:llm", decision, finalReason);
|
|
1255
|
-
writeText(
|
|
1299
|
+
writeText(advisorCurrentPath(ctx), `${display}\n`);
|
|
1256
1300
|
|
|
1257
1301
|
const reviewTask = parsed.activeTask || state.lastTask || "";
|
|
1258
1302
|
const hasTaskActions = parsed.taskActions.length > 0;
|
|
@@ -1300,7 +1344,7 @@ export function registerAdvisor(pi: ExtensionAPI): void {
|
|
|
1300
1344
|
pi.on("session_start", (_event, ctx) => {
|
|
1301
1345
|
const key = sessionKey(ctx);
|
|
1302
1346
|
checkinLocks.delete(key);
|
|
1303
|
-
const state = loadState();
|
|
1347
|
+
const state = loadState(ctx);
|
|
1304
1348
|
recoverReviewControl(state);
|
|
1305
1349
|
saveState(state);
|
|
1306
1350
|
setPiRogueStatus(ctx, loadConfig(), state);
|
|
@@ -1334,7 +1378,7 @@ export function registerAdvisor(pi: ExtensionAPI): void {
|
|
|
1334
1378
|
// ── Preflight (heuristics only — no LLM call, <1ms) ──────────────────
|
|
1335
1379
|
pi.on("before_agent_start", async (event: any, ctx: any) => {
|
|
1336
1380
|
const cfg = loadConfig();
|
|
1337
|
-
const state = loadState();
|
|
1381
|
+
const state = loadState(ctx);
|
|
1338
1382
|
const hasFollowUp = Boolean(state.followUp);
|
|
1339
1383
|
if ((isAdvisorAutoRunSuppressed(state, state.turns) && !hasFollowUp) || cfg.mode === "off" || cfg.mode === "manual") {
|
|
1340
1384
|
return { systemPrompt: event.systemPrompt };
|
|
@@ -1391,7 +1435,7 @@ export function registerAdvisor(pi: ExtensionAPI): void {
|
|
|
1391
1435
|
const note = routeNote(route);
|
|
1392
1436
|
const control = state.reviewControl;
|
|
1393
1437
|
const controlTag = control.status === "needed" || control.status === "running" ? `Review-control: ${control.status}${control.lastDecision ? ` (${control.lastDecision})` : ""}` : "";
|
|
1394
|
-
writeText(
|
|
1438
|
+
writeText(advisorCurrentPath(ctx), `${note}\n`);
|
|
1395
1439
|
return {
|
|
1396
1440
|
systemPrompt: [
|
|
1397
1441
|
event.systemPrompt,
|
|
@@ -1409,7 +1453,7 @@ export function registerAdvisor(pi: ExtensionAPI): void {
|
|
|
1409
1453
|
pi.on("turn_end", async (event: any, ctx: any) => {
|
|
1410
1454
|
const cfg = loadConfig();
|
|
1411
1455
|
if (cfg.mode === "off") return;
|
|
1412
|
-
const state = loadState();
|
|
1456
|
+
const state = loadState(ctx);
|
|
1413
1457
|
const suppressedThisTurn = isAdvisorAutoRunSuppressedForTurnContext(state, state.turns);
|
|
1414
1458
|
const tools = (event.toolResults || []).map((t: any) => String(t?.toolName || t?.name || "tool"));
|
|
1415
1459
|
const fileChanged = tools.some((t: string) => /^(edit|write)$/i.test(t));
|
|
@@ -1431,7 +1475,7 @@ export function registerAdvisor(pi: ExtensionAPI): void {
|
|
|
1431
1475
|
});
|
|
1432
1476
|
}
|
|
1433
1477
|
|
|
1434
|
-
const post = loadState();
|
|
1478
|
+
const post = loadState(ctx);
|
|
1435
1479
|
if (!isAdvisorAutoRunSuppressed(post, post.turns)) {
|
|
1436
1480
|
void maybeAdvisorCheckin(pi, ctx, "turn_end");
|
|
1437
1481
|
}
|
|
@@ -1441,7 +1485,7 @@ export function registerAdvisor(pi: ExtensionAPI): void {
|
|
|
1441
1485
|
pi.on("agent_end", async (event: any, ctx: any) => {
|
|
1442
1486
|
const cfg = loadConfig();
|
|
1443
1487
|
if (cfg.mode === "off") return;
|
|
1444
|
-
const state = loadState();
|
|
1488
|
+
const state = loadState(ctx);
|
|
1445
1489
|
const suppressed = isAdvisorAutoRunSuppressedForTurnContext(state, state.turns);
|
|
1446
1490
|
if (cfg.review === "off" || suppressed) {
|
|
1447
1491
|
if (!suppressed) {
|
|
@@ -1466,7 +1510,7 @@ export function registerAdvisor(pi: ExtensionAPI): void {
|
|
|
1466
1510
|
materialSignals: signals,
|
|
1467
1511
|
});
|
|
1468
1512
|
|
|
1469
|
-
const post = loadState();
|
|
1513
|
+
const post = loadState(ctx);
|
|
1470
1514
|
if (!isAdvisorAutoRunSuppressed(post, post.turns)) {
|
|
1471
1515
|
void maybeAdvisorCheckin(pi, ctx, "agent_end");
|
|
1472
1516
|
}
|
|
@@ -1478,12 +1522,12 @@ export function registerAdvisor(pi: ExtensionAPI): void {
|
|
|
1478
1522
|
getArgumentCompletions: (prefix: string) => piRogueArgumentCompletions(prefix),
|
|
1479
1523
|
handler: async (args, ctx) => {
|
|
1480
1524
|
const cfg = loadConfig();
|
|
1481
|
-
const state = loadState();
|
|
1525
|
+
const state = loadState(ctx);
|
|
1482
1526
|
const arg = String(args ?? "").trim().toLowerCase();
|
|
1483
1527
|
setPiRogueStatus(ctx, cfg, state);
|
|
1484
1528
|
|
|
1485
1529
|
if (!arg || arg === "status" || arg === "help") {
|
|
1486
|
-
ctx.ui.notify(piRogueCockpitText(cfg, state, readText(
|
|
1530
|
+
ctx.ui.notify(piRogueCockpitText(cfg, state, readText(advisorCurrentPath(ctx)).trim(), orchestrationSnapshotText(ctx)), "info");
|
|
1487
1531
|
return;
|
|
1488
1532
|
}
|
|
1489
1533
|
|
|
@@ -1519,7 +1563,7 @@ export function registerAdvisor(pi: ExtensionAPI): void {
|
|
|
1519
1563
|
return;
|
|
1520
1564
|
}
|
|
1521
1565
|
|
|
1522
|
-
ctx.ui.notify(piRogueCockpitText(cfg, state, readText(
|
|
1566
|
+
ctx.ui.notify(piRogueCockpitText(cfg, state, readText(advisorCurrentPath(ctx)).trim(), orchestrationSnapshotText(ctx)), "info");
|
|
1523
1567
|
},
|
|
1524
1568
|
});
|
|
1525
1569
|
|
|
@@ -1531,10 +1575,10 @@ export function registerAdvisor(pi: ExtensionAPI): void {
|
|
|
1531
1575
|
const a = String(args ?? "").trim().toLowerCase();
|
|
1532
1576
|
const [cmd, ...rest] = a.split(/\s+/);
|
|
1533
1577
|
const cfg = loadConfig();
|
|
1534
|
-
const state = loadState();
|
|
1578
|
+
const state = loadState(ctx);
|
|
1535
1579
|
|
|
1536
1580
|
if (!a || cmd === "status") {
|
|
1537
|
-
const note = readText(
|
|
1581
|
+
const note = readText(advisorCurrentPath(ctx)).trim();
|
|
1538
1582
|
const resolved = await resolveModel(ctx, cfg);
|
|
1539
1583
|
const route = state.router.review ?? state.router.preflight;
|
|
1540
1584
|
const pause = advisorPauseRemaining(state, state.turns);
|
|
@@ -4,7 +4,7 @@ import { dirname } from "node:path";
|
|
|
4
4
|
import { homedir } from "node:os";
|
|
5
5
|
import { join } from "node:path";
|
|
6
6
|
import { completeSimple } from "@earendil-works/pi-ai";
|
|
7
|
-
import { registerAdvisor } from "./extension.js";
|
|
7
|
+
import { advisorSessionStatePath, registerAdvisor } from "./extension.js";
|
|
8
8
|
|
|
9
9
|
const testHome = vi.hoisted(() => `/tmp/pi-rogue-advisor-loop-convergence-${process.pid}-${Date.now()}-${Math.random().toString(36).slice(2)}`);
|
|
10
10
|
|
|
@@ -57,7 +57,7 @@ function makeHandlers() {
|
|
|
57
57
|
}
|
|
58
58
|
|
|
59
59
|
const ADVISOR_STATE_DIR = join(homedir(), ".pi", "agent", "pi-rogue", "advisor");
|
|
60
|
-
const ADVISOR_STATE_PATH =
|
|
60
|
+
const ADVISOR_STATE_PATH = advisorSessionStatePath("session");
|
|
61
61
|
const ADVISOR_CONFIG_PATH = join(ADVISOR_STATE_DIR, "config.json");
|
|
62
62
|
const ADVISOR_CACHE_PATH = join(ADVISOR_STATE_DIR, "cache.json");
|
|
63
63
|
|
|
@@ -2,7 +2,7 @@ import { describe, expect, it, vi, beforeEach, afterEach } from "vitest";
|
|
|
2
2
|
import { existsSync, readFileSync, writeFileSync, mkdirSync } from "node:fs";
|
|
3
3
|
import { join, dirname } from "node:path";
|
|
4
4
|
import { homedir } from "node:os";
|
|
5
|
-
import { registerAdvisor } from "./extension.js";
|
|
5
|
+
import { advisorSessionStatePath, registerAdvisor } from "./extension.js";
|
|
6
6
|
|
|
7
7
|
const testHome = vi.hoisted(() => `/tmp/pi-rogue-advisor-state-versioning-${process.pid}-${Date.now()}-${Math.random().toString(36).slice(2)}`);
|
|
8
8
|
|
|
@@ -37,17 +37,17 @@ function makeHandlers() {
|
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
const ADVISOR_STATE_DIR = join(homedir(), ".pi", "agent", "pi-rogue", "advisor");
|
|
40
|
-
const ADVISOR_STATE_PATH = join(ADVISOR_STATE_DIR, "state.json");
|
|
41
40
|
const ADVISOR_CONFIG_PATH = join(ADVISOR_STATE_DIR, "config.json");
|
|
41
|
+
const ADVISOR_STATE_PATH = advisorSessionStatePath("session");
|
|
42
42
|
|
|
43
43
|
function readAdvisorState(): any {
|
|
44
44
|
return JSON.parse(readFileSync(ADVISOR_STATE_PATH, "utf8"));
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
function mkCtx() {
|
|
47
|
+
function mkCtx(session = "session") {
|
|
48
48
|
return {
|
|
49
49
|
sessionManager: {
|
|
50
|
-
getSessionFile: () => join(homedir(), ".pi", "agent", "pi-rogue", "advisor",
|
|
50
|
+
getSessionFile: () => join(homedir(), ".pi", "agent", "pi-rogue", "advisor", `${session}.jsonl`),
|
|
51
51
|
},
|
|
52
52
|
isIdle: () => true,
|
|
53
53
|
modelRegistry: {
|
|
@@ -224,4 +224,25 @@ describe("state versioning and recovery", () => {
|
|
|
224
224
|
expect(recovered.reviewControl.pending).toBe(true);
|
|
225
225
|
expect(recovered.reviewControl.lastDecision).toBe("review");
|
|
226
226
|
});
|
|
227
|
+
|
|
228
|
+
it("keeps mutable advisor state isolated by session", async () => {
|
|
229
|
+
const setup = makeHandlers();
|
|
230
|
+
const { handlers: h, pi } = setup;
|
|
231
|
+
registerAdvisor(pi);
|
|
232
|
+
|
|
233
|
+
const ctxA = mkCtx("model-training");
|
|
234
|
+
const ctxB = mkCtx("runpod");
|
|
235
|
+
|
|
236
|
+
void h.session_start?.[0]?.({}, ctxA);
|
|
237
|
+
await h.before_agent_start?.[0]?.({ prompt: "train advisor on regex logs", systemPrompt: "base" }, ctxA);
|
|
238
|
+
|
|
239
|
+
const stateA = JSON.parse(readFileSync(advisorSessionStatePath("model-training"), "utf8"));
|
|
240
|
+
expect(stateA.lastTask).toBe("train advisor on regex logs");
|
|
241
|
+
|
|
242
|
+
void h.session_start?.[0]?.({}, ctxB);
|
|
243
|
+
const stateB = JSON.parse(readFileSync(advisorSessionStatePath("runpod"), "utf8"));
|
|
244
|
+
expect(stateB.lastTask).toBe("");
|
|
245
|
+
expect(stateB.followUp).toBe("");
|
|
246
|
+
expect(stateB.reviewSignals).toEqual([]);
|
|
247
|
+
});
|
|
227
248
|
});
|
|
@@ -62,6 +62,16 @@ describe("advisor check-in lifecycle bridge", () => {
|
|
|
62
62
|
expect(JSON.parse(readFileSync(file, "utf8")).checkins).toBe("off");
|
|
63
63
|
});
|
|
64
64
|
|
|
65
|
+
it("preserves legacy one-argument reset signature", () => {
|
|
66
|
+
const { config } = tempState();
|
|
67
|
+
writeFileSync(config, JSON.stringify({ checkins: "off" }), "utf8");
|
|
68
|
+
|
|
69
|
+
const next = resetAdvisorSessionContext(config);
|
|
70
|
+
|
|
71
|
+
expect(next.config.checkins).toBe("off");
|
|
72
|
+
expect(JSON.parse(readFileSync(config, "utf8")).checkins).toBe("off");
|
|
73
|
+
});
|
|
74
|
+
|
|
65
75
|
it("resets advisor brief context and check-in timing for a new goal", () => {
|
|
66
76
|
const { config, state } = tempState();
|
|
67
77
|
const startedAt = Date.now();
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
2
|
import { dirname, join } from "node:path";
|
|
3
3
|
import { homedir } from "node:os";
|
|
4
|
+
import { sessionKey } from "./internal.js";
|
|
4
5
|
|
|
5
6
|
type AdvisorConfig = Record<string, unknown> & { checkins?: "mid-hour" | "off"; checkinStartedAt?: number };
|
|
6
7
|
type AdvisorState = Record<string, unknown> & {
|
|
@@ -34,6 +35,15 @@ const ADVISOR_DIR = join(homedir(), ".pi", "agent", "pi-rogue", "advisor");
|
|
|
34
35
|
const ADVISOR_CONFIG_PATH = join(homedir(), ".pi", "agent", "pi-rogue", "advisor", "config.json");
|
|
35
36
|
const ADVISOR_STATE_PATH = join(ADVISOR_DIR, "state.json");
|
|
36
37
|
|
|
38
|
+
function safeSessionKey(key: string): string {
|
|
39
|
+
const safe = String(key || "session").replace(/[^A-Za-z0-9._-]+/g, "-").replace(/^-+|-+$/g, "");
|
|
40
|
+
return safe || "session";
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function advisorSessionStatePath(ctx: any): string {
|
|
44
|
+
return join(ADVISOR_DIR, "sessions", safeSessionKey(sessionKey(ctx)), "state.json");
|
|
45
|
+
}
|
|
46
|
+
|
|
37
47
|
function readJson<T>(file: string): T {
|
|
38
48
|
if (!existsSync(file)) return {} as T;
|
|
39
49
|
try {
|
|
@@ -63,9 +73,14 @@ export function setAdvisorCheckinsEnabled(enabled: boolean, configPath = ADVISOR
|
|
|
63
73
|
}
|
|
64
74
|
|
|
65
75
|
export function resetAdvisorSessionContext(
|
|
66
|
-
|
|
67
|
-
|
|
76
|
+
ctxOrConfigPath?: any,
|
|
77
|
+
configPathOrStatePath = ADVISOR_STATE_PATH,
|
|
78
|
+
explicitStatePath?: string,
|
|
68
79
|
): { config: AdvisorConfig; state: AdvisorState } {
|
|
80
|
+
const configPath = typeof ctxOrConfigPath === "string" ? ctxOrConfigPath : ADVISOR_CONFIG_PATH;
|
|
81
|
+
const statePath = typeof ctxOrConfigPath === "string"
|
|
82
|
+
? configPathOrStatePath
|
|
83
|
+
: explicitStatePath || (ctxOrConfigPath ? advisorSessionStatePath(ctxOrConfigPath) : ADVISOR_STATE_PATH);
|
|
69
84
|
const currentConfig = cleanAdvisorConfig(readJson<AdvisorConfig>(configPath));
|
|
70
85
|
const nextConfig: AdvisorConfig = {
|
|
71
86
|
...currentConfig,
|
|
@@ -54,7 +54,7 @@ export function setGoal(ctx: any, goal: string, options: { restartDuplicate?: bo
|
|
|
54
54
|
}
|
|
55
55
|
clearLoop(ctx, { clearResearch: true, preserveCheckins: true });
|
|
56
56
|
writeText(sessionFile(FEATURE, ctx, CURRENT_FILE), note ? `${note}\n` : "");
|
|
57
|
-
resetAdvisorSessionContext();
|
|
57
|
+
resetAdvisorSessionContext(ctx);
|
|
58
58
|
if (note) {
|
|
59
59
|
setAdvisorCheckinsEnabled(true);
|
|
60
60
|
}
|
|
@@ -68,7 +68,7 @@ export function setGoal(ctx: any, goal: string, options: { restartDuplicate?: bo
|
|
|
68
68
|
|
|
69
69
|
export function clearGoal(ctx: any): void {
|
|
70
70
|
writeText(sessionFile(FEATURE, ctx, CURRENT_FILE), "");
|
|
71
|
-
resetAdvisorSessionContext();
|
|
71
|
+
resetAdvisorSessionContext(ctx);
|
|
72
72
|
}
|
|
73
73
|
|
|
74
74
|
function goalBlock(goal: string): string {
|
|
@@ -77,10 +77,19 @@ export function featureDir(feature: string): string {
|
|
|
77
77
|
return dir;
|
|
78
78
|
}
|
|
79
79
|
|
|
80
|
+
function safeSessionKey(key: string): string {
|
|
81
|
+
const safe = String(key || "session").replace(/[^A-Za-z0-9._-]+/g, "-").replace(/^-+|-+$/g, "");
|
|
82
|
+
return safe || "session";
|
|
83
|
+
}
|
|
84
|
+
|
|
80
85
|
export function sessionKey(ctx: any): string {
|
|
81
86
|
const sessionFile = ctx?.sessionManager?.getSessionFile?.();
|
|
82
|
-
if (
|
|
83
|
-
|
|
87
|
+
if (typeof sessionFile === "string" && sessionFile.length > 0) {
|
|
88
|
+
return safeSessionKey(basename(String(sessionFile)).replace(/\.[^.]+$/, ""));
|
|
89
|
+
}
|
|
90
|
+
const sessionId = ctx?.session?.id || process.env.PI_ROGUE_SESSION_ID;
|
|
91
|
+
if (typeof sessionId === "string" && sessionId.length > 0) return safeSessionKey(sessionId);
|
|
92
|
+
return "session";
|
|
84
93
|
}
|
|
85
94
|
|
|
86
95
|
export function sessionDir(feature: string, ctx: any): string {
|