@fiale-plus/pi-rogue 0.2.1 → 0.2.2

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.
Files changed (32) hide show
  1. package/README.md +2 -1
  2. package/node_modules/@fiale-plus/pi-rogue-context-broker/README.md +4 -3
  3. package/node_modules/@fiale-plus/pi-rogue-context-broker/src/extension.test.ts +38 -4
  4. package/node_modules/@fiale-plus/pi-rogue-context-broker/src/extension.ts +52 -6
  5. package/node_modules/@fiale-plus/pi-rogue-router/README.md +32 -0
  6. package/node_modules/@fiale-plus/pi-rogue-router/package.json +30 -0
  7. package/node_modules/@fiale-plus/pi-rogue-router/src/checkpoints.test.ts +84 -0
  8. package/node_modules/@fiale-plus/pi-rogue-router/src/checkpoints.ts +355 -0
  9. package/node_modules/@fiale-plus/pi-rogue-router/src/cli.ts +277 -0
  10. package/node_modules/@fiale-plus/pi-rogue-router/src/completions.ts +34 -0
  11. package/node_modules/@fiale-plus/pi-rogue-router/src/config-extension.test.ts +133 -0
  12. package/node_modules/@fiale-plus/pi-rogue-router/src/config.ts +168 -0
  13. package/node_modules/@fiale-plus/pi-rogue-router/src/dataset.ts +154 -0
  14. package/node_modules/@fiale-plus/pi-rogue-router/src/decision-ledger.test.ts +148 -0
  15. package/node_modules/@fiale-plus/pi-rogue-router/src/decision.ts +138 -0
  16. package/node_modules/@fiale-plus/pi-rogue-router/src/extension.ts +139 -0
  17. package/node_modules/@fiale-plus/pi-rogue-router/src/git-features.ts +119 -0
  18. package/node_modules/@fiale-plus/pi-rogue-router/src/hash.ts +19 -0
  19. package/node_modules/@fiale-plus/pi-rogue-router/src/index.ts +15 -0
  20. package/node_modules/@fiale-plus/pi-rogue-router/src/learning.test.ts +241 -0
  21. package/node_modules/@fiale-plus/pi-rogue-router/src/learning.ts +382 -0
  22. package/node_modules/@fiale-plus/pi-rogue-router/src/ledger.ts +94 -0
  23. package/node_modules/@fiale-plus/pi-rogue-router/src/observe.ts +119 -0
  24. package/node_modules/@fiale-plus/pi-rogue-router/src/outcomes.ts +128 -0
  25. package/node_modules/@fiale-plus/pi-rogue-router/src/progress.ts +93 -0
  26. package/node_modules/@fiale-plus/pi-rogue-router/src/session-reader.ts +217 -0
  27. package/node_modules/@fiale-plus/pi-rogue-router/src/subagents.ts +178 -0
  28. package/node_modules/@fiale-plus/pi-rogue-router/src/types.ts +150 -0
  29. package/node_modules/@fiale-plus/pi-rogue-router/src/v1-telemetry.test.ts +293 -0
  30. package/package.json +5 -3
  31. package/src/extension.test.ts +1 -0
  32. package/src/extension.ts +2 -0
package/README.md CHANGED
@@ -8,6 +8,7 @@ It stitches together (and bundles for a true single-package install):
8
8
  - `@fiale-plus/pi-rogue-advisor` (logic; direct releases paused)
9
9
  - `@fiale-plus/pi-rogue-context-broker` (context-broker runtime; registered by default with an env kill switch)
10
10
  - `@fiale-plus/pi-rogue-orchestration` (logic; direct releases paused)
11
+ - `@fiale-plus/pi-rogue-router` (observe-only trajectory-router lab; direct releases paused)
11
12
 
12
13
  Direct installs of the advisor/orchestration packages are paused (marked private). All users and future releases go through the bundle. See `docs/release.md` and root `AGENTS.md` / `README.md` for the release policy.
13
14
 
@@ -37,7 +38,7 @@ npm install
37
38
 
38
39
  ## Command surface
39
40
 
40
- - Default: `/advisor`, `/goal`, `/loop`, `/autoresearch`, `/autoresearch-lab` plus status/config/command paths (all provided via the bundle).
41
+ - Default: `/advisor`, `/goal`, `/loop`, `/autoresearch`, `/autoresearch-lab`, `/router` plus status/config/command paths (all provided via the bundle).
41
42
  - Context broker: enabled by default; `PI_CONTEXT_BROKER_ENABLED=false` disables `/context status`, `/context brief`, `/context lookup <handle|text>`, `/context pin <handle>`, `/context export <handle>`, and `/context prune` with autocomplete.
42
43
 
43
44
  ## Status
@@ -22,7 +22,7 @@ PI_CONTEXT_BROKER_ENABLED=false pi
22
22
 
23
23
  When active, the bundle registers:
24
24
 
25
- - `/context status` — enabled state, record/byte counts, pinned counts, and routing telemetry.
25
+ - `/context status` — enabled state, record/byte counts, pinned counts, routing telemetry, and prompt rewrite savings bytes.
26
26
  - `/context brief` — bounded prompt-safe broker brief with handles and summaries.
27
27
  - `/context lookup <handle|text>` — exact handle rehydration or current-session text search.
28
28
  - `/context pin <handle>` — protect an artifact from normal TTL/cap pruning.
@@ -33,9 +33,9 @@ The command includes autocomplete for subcommands and known artifact handles. Ex
33
33
 
34
34
  Optional durability is available with `PI_CONTEXT_BROKER_DURABLE=true` or `PI_CONTEXT_BROKER_STORE_DIR=/path/to/store`. Durable mode now defaults to SQLite (`artifacts.sqlite`) with an FTS index for text lookup, so exact handles, tier, and pin state survive restarts without replay reconstruction. Set `PI_CONTEXT_BROKER_BACKEND=jsonl` to use the legacy JSONL/blob backend.
35
35
 
36
- - `PI_CONTEXT_BROKER_REWRITE_THRESHOLD_BYTES` controls when large `toolResult` / `bashExecution` payloads are rewritten in-context. The default is `0` (rewritten), so raw tool evidence is replaced by handles by default.
36
+ - `PI_CONTEXT_BROKER_REWRITE_THRESHOLD_BYTES` controls when large `toolResult` / `bashExecution` payloads are rewritten in-context. The default is `8192` bytes, so small tool evidence remains inline while larger outputs are replaced by handles.
37
37
 
38
- For quieter sessions, set `PI_CONTEXT_BROKER_REWRITE_THRESHOLD_BYTES` to a higher value to only rewrite larger outputs.
38
+ For more aggressive prompt reduction, set `PI_CONTEXT_BROKER_REWRITE_THRESHOLD_BYTES=0`. For quieter sessions, set it to a higher value to only rewrite larger outputs.
39
39
 
40
40
 
41
41
  ## Session behavior and limits
@@ -45,6 +45,7 @@ For quieter sessions, set `PI_CONTEXT_BROKER_REWRITE_THRESHOLD_BYTES` to a highe
45
45
  - Without durable mode, restarting Pi loses broker state until the current branch is backfilled again.
46
46
  - Prompt integration injects a bounded, tier-aware broker brief and lookup guidance; the LLM also gets a `context_lookup` tool for exact handle dereferencing. Payloads that hit hostile-binary heuristics are represented in prompt as handles plus short guidance to export the full content.
47
47
  - The `context` hook rewrites prompt-visible `toolResult` and `bashExecution` payloads in the LLM-bound message copy to broker handles and summaries, reducing prompt load while preserving exact `/context lookup` rehydration.
48
+ - Current-turn `context_lookup` results are left visible so the model can consume requested exact evidence once. Historical `context_lookup` results that already have a later assistant response are omitted from later prompt assembly to avoid recursive prompt growth.
48
49
  - Pi `excludeFromContext` bash entries are not backfilled or rewritten into broker prompts.
49
50
  - Basic secret redaction runs before broker storage and display for common token/password/API-key patterns.
50
51
  - Optional global caps can be configured via env vars:
@@ -382,7 +382,7 @@ lookupBytes: 500,
382
382
  await commands.get("context").handler(`export ${handle}`, ctx);
383
383
  const result = await handlers.get("context")?.[0]({
384
384
  type: "context",
385
- messages: [{ role: "toolResult", toolCallId: "tool-result-telemetry", toolName: "bash", content: [{ type: "text", text: "telemetry_payload_" + "y".repeat(150) }], isError: false, timestamp: 1 }],
385
+ messages: [{ role: "toolResult", toolCallId: "tool-result-telemetry", toolName: "bash", content: [{ type: "text", text: "telemetry_payload_" + "y".repeat(1000) }], isError: false, timestamp: 1 }],
386
386
  }, ctx);
387
387
 
388
388
  await commands.get("context").handler("status", ctx);
@@ -392,15 +392,46 @@ lookupBytes: 500,
392
392
  expect(result).toBeDefined();
393
393
  const telemetry = notifications.at(-1)?.message ?? "";
394
394
  expect(telemetry).toContain("Context broker routing telemetry:");
395
+ expect(telemetry).toContain("rewriteSavings rawBytes=");
396
+ expect(telemetry).toContain("replacementBytes=");
397
+ expect(telemetry).toContain("savedBytes=");
398
+ expect(telemetry).toMatch(/savedBytes=[1-9]\d*/);
399
+ expect(telemetry).toContain("contextLookupHistoryOmitted=");
395
400
  expect(telemetry).toContain("lookups tool(calls=");
396
401
  expect(telemetry).toContain("lookups slash(calls=");
397
402
  expect(telemetry).toContain("exports=");
398
403
  expect(telemetry).toContain("pins=");
399
404
  });
400
405
 
401
- it("does not broker context_lookup results recursively", async () => {
406
+ it("keeps current context_lookup results visible before the model consumes them", async () => {
402
407
  const { pi, handlers, commands, tools } = createPiMock();
403
- registerContextBrokerBeta(pi, { lookupBytes: 500, rewriteThresholdBytes: 1 });
408
+ registerContextBrokerBeta(pi, { lookupBytes: 1000, rewriteThresholdBytes: 1 });
409
+ const { ctx } = createCtx();
410
+
411
+ await runHandlers(handlers, "session_start", { type: "session_start" }, ctx);
412
+ await runHandlers(handlers, "tool_result", {
413
+ type: "tool_result",
414
+ toolCallId: "call-current-lookup-source",
415
+ toolName: "bash",
416
+ input: { command: "printf current-lookup" },
417
+ content: [{ type: "text", text: "CURRENT_LOOKUP_EVIDENCE_" + "x".repeat(120) }],
418
+ isError: false,
419
+ }, ctx);
420
+ const handle = commands.get("context").getArgumentCompletions("lookup ")?.[0].value.replace(/^lookup /, "");
421
+ const lookupResult = await tools.get("context_lookup").execute("lookup-current", { handle }, undefined, undefined, ctx);
422
+
423
+ const contextResult = await handlers.get("context")?.[0]({
424
+ type: "context",
425
+ messages: [{ role: "toolResult", toolCallId: "lookup-current", toolName: "context_lookup", content: lookupResult.content, isError: false }],
426
+ }, ctx);
427
+
428
+ expect(contextResult).toBeUndefined();
429
+ expect(lookupResult.content[0].text).toContain("CURRENT_LOOKUP_EVIDENCE_");
430
+ });
431
+
432
+ it("does not broker historical context_lookup results recursively", async () => {
433
+ const { pi, handlers, commands, tools } = createPiMock();
434
+ registerContextBrokerBeta(pi, { lookupBytes: 500 });
404
435
  const { ctx, notifications } = createCtx();
405
436
 
406
437
  await runHandlers(handlers, "session_start", { type: "session_start" }, ctx);
@@ -425,7 +456,10 @@ lookupBytes: 500,
425
456
  }, ctx);
426
457
  const contextResult = await handlers.get("context")?.[0]({
427
458
  type: "context",
428
- messages: [{ role: "toolResult", toolCallId: "lookup-call", toolName: "context_lookup", content: lookupResult.content, isError: false }],
459
+ messages: [
460
+ { role: "toolResult", toolCallId: "lookup-call", toolName: "context_lookup", content: lookupResult.content, isError: false },
461
+ { role: "assistant", content: [{ type: "text", text: "I consumed the lookup." }] },
462
+ ],
429
463
  }, ctx);
430
464
  await commands.get("context").handler("brief", ctx);
431
465
 
@@ -176,6 +176,16 @@ function compact(value: string, max = 120): string {
176
176
  return truncateUtf8(value.replace(/\s+/g, " ").trim(), max);
177
177
  }
178
178
 
179
+ function utf8Bytes(text: string): number {
180
+ return Buffer.byteLength(text, "utf8");
181
+ }
182
+
183
+ function promptPayloadBytes(message: any): number {
184
+ if (message?.role === "bashExecution") return utf8Bytes(String(message.output ?? ""));
185
+ if (message?.role === "toolResult") return utf8Bytes(contentText(message.content));
186
+ return utf8Bytes(toText(message));
187
+ }
188
+
179
189
  function stableHash(value: string): string {
180
190
  return createHash("sha256").update(value).digest("hex").slice(0, 16);
181
191
  }
@@ -308,6 +318,9 @@ export async function registerContextBrokerBeta(pi: ExtensionAPI, options: Conte
308
318
  contextHookBash: 0,
309
319
  contextHookBashRewrites: 0,
310
320
  contextHookBashHostile: 0,
321
+ contextHookRewriteRawBytes: 0,
322
+ contextHookRewriteReplacementBytes: 0,
323
+ contextHookContextLookupHistoryOmissions: 0,
311
324
  toolResultEvents: 0,
312
325
  toolResultArtifacts: 0,
313
326
  backfillScans: 0,
@@ -329,11 +342,21 @@ export async function registerContextBrokerBeta(pi: ExtensionAPI, options: Conte
329
342
  pruneCalls: 0,
330
343
  };
331
344
 
345
+ function recordContextRewrite(rawBytes: number, replacementBytes: number): void {
346
+ routingTelemetry.contextHookRewriteRawBytes += Math.max(0, rawBytes);
347
+ routingTelemetry.contextHookRewriteReplacementBytes += Math.max(0, replacementBytes);
348
+ }
349
+
332
350
  function formatRoutingTelemetry(): string {
351
+ const savedBytes = Math.max(0, routingTelemetry.contextHookRewriteRawBytes - routingTelemetry.contextHookRewriteReplacementBytes);
352
+ const savedPct = routingTelemetry.contextHookRewriteRawBytes > 0
353
+ ? ((savedBytes / routingTelemetry.contextHookRewriteRawBytes) * 100).toFixed(1)
354
+ : "0.0";
333
355
  const line = [
334
356
  `contextHook calls=${routingTelemetry.contextHookCalls}`,
335
357
  `toolResults seen=${routingTelemetry.contextHookToolResults} rewritten=${routingTelemetry.contextHookToolResultRewrites} hostile=${routingTelemetry.contextHookToolResultHostile}`,
336
358
  `bash seen=${routingTelemetry.contextHookBash} rewritten=${routingTelemetry.contextHookBashRewrites} hostile=${routingTelemetry.contextHookBashHostile}`,
359
+ `rewriteSavings rawBytes=${routingTelemetry.contextHookRewriteRawBytes} replacementBytes=${routingTelemetry.contextHookRewriteReplacementBytes} savedBytes=${savedBytes} savedPct=${savedPct}% contextLookupHistoryOmitted=${routingTelemetry.contextHookContextLookupHistoryOmissions}`,
337
360
  `lookups tool(calls=${routingTelemetry.toolLookupCalls}, hits=${routingTelemetry.toolLookupHits}, misses=${routingTelemetry.toolLookupMisses})`,
338
361
  `lookups slash(calls=${routingTelemetry.commandLookupCalls}, hits=${routingTelemetry.commandLookupHits}, misses=${routingTelemetry.commandLookupMisses})`,
339
362
  `exports=${routingTelemetry.exportCalls}`,
@@ -572,19 +595,35 @@ export async function registerContextBrokerBeta(pi: ExtensionAPI, options: Conte
572
595
  activeSessionId = sessionIdFor(ctx);
573
596
  routingTelemetry.contextHookCalls += 1;
574
597
  const toolInputs = collectToolInputs(event.messages);
575
- const drafts = event.messages.map((message: any): { original: any; replacement?: any; artifact?: ContextArtifact; rewrite?: (artifact: ContextArtifact) => any; safeFallback?: any } => {
598
+ type RewriteDraft = {
599
+ original: any;
600
+ replacement?: any;
601
+ rawBytes?: number;
602
+ artifact?: ContextArtifact;
603
+ rewrite?: (artifact: ContextArtifact) => any;
604
+ safeFallback?: any;
605
+ };
606
+ const drafts = event.messages.map((message: any, index: number): RewriteDraft => {
576
607
  if (message?.role === "toolResult") {
577
608
  routingTelemetry.contextHookToolResults += 1;
578
609
  const raw = contentText(message.content);
610
+ const rawBytes = utf8Bytes(raw);
579
611
  const toolInput = typeof message.toolCallId === "string" ? toolInputs.get(message.toolCallId) : undefined;
580
612
  const toolName = String(message.toolName ?? toolInput?.toolName ?? "tool");
581
613
  const hostile = hasHostileText(raw) || hasHostileValue(message.content);
582
614
  if (hostile) routingTelemetry.contextHookToolResultHostile += 1;
583
- const shouldRewrite = Buffer.byteLength(raw, "utf8") > rewriteThresholdBytes || hostile;
584
- if (!shouldRewrite) return { original: message };
585
615
  if (!shouldBrokerToolName(toolName)) {
586
- return { original: message, replacement: { ...message, content: [{ type: "text", text: contextLookupHistoryPlaceholder() }] } };
616
+ const hasLaterAssistant = event.messages.slice(index + 1).some((candidate: any) => candidate?.role === "assistant");
617
+ if (!hasLaterAssistant) return { original: message };
618
+ routingTelemetry.contextHookContextLookupHistoryOmissions += 1;
619
+ return {
620
+ original: message,
621
+ rawBytes,
622
+ replacement: { ...message, content: [{ type: "text", text: contextLookupHistoryPlaceholder() }] },
623
+ };
587
624
  }
625
+ const shouldRewrite = rawBytes > rewriteThresholdBytes || hostile;
626
+ if (!shouldRewrite) return { original: message };
588
627
  const artifact = publishToolArtifact({
589
628
  toolName,
590
629
  input: message.input ?? toolInput?.input,
@@ -599,6 +638,7 @@ export async function registerContextBrokerBeta(pi: ExtensionAPI, options: Conte
599
638
  routingTelemetry.contextHookToolResultRewrites += 1;
600
639
  return {
601
640
  original: message,
641
+ rawBytes,
602
642
  artifact,
603
643
  rewrite: (live) => ({ ...message, content: [{ type: "text", text: brokerPlaceholder(live) }] }),
604
644
  safeFallback: { ...message, content: [{ type: "text", text: prunedPayloadPlaceholder(hostile) }] },
@@ -608,9 +648,10 @@ export async function registerContextBrokerBeta(pi: ExtensionAPI, options: Conte
608
648
  if (message?.role === "bashExecution" && message.excludeFromContext !== true) {
609
649
  routingTelemetry.contextHookBash += 1;
610
650
  const raw = String(message.output ?? "");
651
+ const rawBytes = utf8Bytes(raw);
611
652
  const hostile = hasHostileText(raw) || hasHostileValue(message.output);
612
653
  if (hostile) routingTelemetry.contextHookBashHostile += 1;
613
- const shouldRewrite = Buffer.byteLength(raw, "utf8") > rewriteThresholdBytes || hostile;
654
+ const shouldRewrite = rawBytes > rewriteThresholdBytes || hostile;
614
655
  if (!shouldRewrite) return { original: message };
615
656
  const sourceId = typeof message.timestamp === "number"
616
657
  ? `bash:${message.timestamp}:${stableHash([message.command ?? "", raw, message.exitCode ?? "", message.cancelled ?? ""].join("\n"))}`
@@ -634,6 +675,7 @@ export async function registerContextBrokerBeta(pi: ExtensionAPI, options: Conte
634
675
  routingTelemetry.contextHookBashRewrites += 1;
635
676
  return {
636
677
  original: message,
678
+ rawBytes,
637
679
  artifact,
638
680
  rewrite: (live) => ({ ...message, output: brokerPlaceholder(live), truncated: true }),
639
681
  safeFallback: { ...message, output: prunedPayloadPlaceholder(hostile), truncated: true },
@@ -647,6 +689,7 @@ export async function registerContextBrokerBeta(pi: ExtensionAPI, options: Conte
647
689
  const messages = drafts.map((draft) => {
648
690
  if (draft.replacement) {
649
691
  changed = true;
692
+ recordContextRewrite(draft.rawBytes ?? promptPayloadBytes(draft.original), promptPayloadBytes(draft.replacement));
650
693
  return draft.replacement;
651
694
  }
652
695
  if (!draft.artifact || !draft.rewrite) return draft.original;
@@ -655,12 +698,15 @@ export async function registerContextBrokerBeta(pi: ExtensionAPI, options: Conte
655
698
  for (const parentId of draft.artifact.parentIds) sourceHandles.delete(parentId);
656
699
  if (draft.safeFallback) {
657
700
  changed = true;
701
+ recordContextRewrite(draft.rawBytes ?? promptPayloadBytes(draft.original), promptPayloadBytes(draft.safeFallback));
658
702
  return draft.safeFallback;
659
703
  }
660
704
  return draft.original;
661
705
  }
662
706
  changed = true;
663
- return draft.rewrite(live);
707
+ const replacement = draft.rewrite(live);
708
+ recordContextRewrite(draft.rawBytes ?? promptPayloadBytes(draft.original), promptPayloadBytes(replacement));
709
+ return replacement;
664
710
  });
665
711
 
666
712
  return changed ? { messages } : undefined;
@@ -0,0 +1,32 @@
1
+ # Pi-Rogue Router
2
+
3
+ Local-only offline trajectory router experiments for Pi-Rogue.
4
+
5
+ This package intentionally does **not** change live advisor or orchestration behavior. It reads existing Pi session JSONL files, derives compact checkpoints, and computes cheap progress/loop signals without copying raw transcript content into derived artifacts.
6
+
7
+ ```bash
8
+ npm run router:rebuild -- --session ~/.pi/agent/sessions/.../session.jsonl --output .pi/router/checkpoints.jsonl
9
+ npm run router:rebuild -- --session-dir ~/.pi/agent/sessions/... --output .pi/router/checkpoints.jsonl
10
+ npm run router:rebuild -- --session ./current-session.jsonl --workspace-diff --output .pi/router/checkpoints-with-live-diff.jsonl
11
+ npm run router:decide -- --checkpoint-file .pi/router/checkpoints.jsonl --ledger .pi/router/events.jsonl
12
+ npm run router:cards -- --events .pi/router/events.jsonl --output .pi/router/model-cards.jsonl
13
+ npm run router:outcomes -- --checkpoint-file .pi/router/checkpoints.jsonl --events .pi/router/events.jsonl --output .pi/router/outcomes.jsonl
14
+ npm run router:teacher-requests -- --checkpoint-file .pi/router/checkpoints.jsonl --output .pi/router/teacher-requests.jsonl --teacher openai-codex/gpt-5.5
15
+ npm run router:reflect -- --checkpoint-file .pi/router/checkpoints.jsonl --labels .pi/router/labels/teacher-labels.jsonl --reflection .pi/router/reflections/session.md --teacher local-rule
16
+ npm run router:dataset -- --checkpoint-file .pi/router/checkpoints.jsonl --events .pi/router/events.jsonl --outcomes .pi/router/outcomes.jsonl --labels .pi/router/labels/teacher-labels.jsonl --output .pi/router/training.jsonl
17
+ npm run router:shadow -- --checkpoint-file .pi/router/checkpoints.jsonl --ledger .pi/router/events.jsonl --output .pi/router/shadow-report.json
18
+
19
+ # Live observe-only extension commands:
20
+ # /router on|off|status|profile|profiles|models|configure|cycle
21
+ # ctrl+alt+p cycles router profiles (Ctrl-P is reserved by Pi model cycling).
22
+ ```
23
+
24
+ ## V1 telemetry notes
25
+
26
+ Router v1 is still observe-only. It adds outcome skeletons, stronger diff/error fingerprints, teacher-label request export, binary gate dataset export, and subagent-aware telemetry schemas. It does not switch models, spawn agents, or promote policies automatically.
27
+
28
+ - Diff telemetry stores counts and hashes from `git diff`, not raw patches. Offline rebuilds remain deterministic by default; use `--workspace-diff` only with one current live session/worktree snapshot.
29
+ - Error fingerprints normalize paths, line numbers, timestamps, UUIDs, ports, and object ids before hashing.
30
+ - `router:teacher-requests` writes local JSONL requests for an explicit teacher model; imported teacher decisions are still required before labels become training truth.
31
+ - `router:dataset` excludes `local-rule` labels by default so a future model does not merely imitate the current rules.
32
+ - Subagent route/ledger schemas describe parent-child evidence flow, but live autonomous spawning remains out of scope.
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "@fiale-plus/pi-rogue-router",
3
+ "version": "0.1.0",
4
+ "description": "Local-only offline trajectory router experiments for Pi-Rogue.",
5
+ "private": true,
6
+ "type": "module",
7
+ "license": "MIT",
8
+ "scripts": {
9
+ "check": "tsc -p ../../tsconfig.json --noEmit",
10
+ "test": "cd ../.. && vitest run packages/router/src/*.test.ts"
11
+ },
12
+ "main": "./src/index.ts",
13
+ "exports": {
14
+ ".": "./src/index.ts",
15
+ "./extension": "./src/extension.ts"
16
+ },
17
+ "pi": {
18
+ "extensions": [
19
+ "./src/extension.ts"
20
+ ]
21
+ },
22
+ "peerDependencies": {
23
+ "@earendil-works/pi-coding-agent": "^0.74.0"
24
+ },
25
+ "files": [
26
+ "src",
27
+ "README.md",
28
+ "package.json"
29
+ ]
30
+ }
@@ -0,0 +1,84 @@
1
+ import { mkdtempSync, readFileSync, writeFileSync } from "node:fs";
2
+ import { tmpdir } from "node:os";
3
+ import { join } from "node:path";
4
+ import { describe, expect, it } from "vitest";
5
+ import { buildCheckpoints, rebuildCheckpointsFromSession, streamCheckpointsFromSessionPath, writeCheckpointsJsonl } from "./checkpoints.js";
6
+ import { readPiSession } from "./session-reader.js";
7
+
8
+ function writeFixture(lines: Array<Record<string, unknown>>): string {
9
+ const dir = mkdtempSync(join(tmpdir(), "pi-router-"));
10
+ const path = join(dir, "2026-06-12T00-00-00Z_fixture.jsonl");
11
+ writeFileSync(path, lines.map((line) => JSON.stringify(line)).join("\n") + "\n");
12
+ return path;
13
+ }
14
+
15
+ function fixtureSession(): string {
16
+ return writeFixture([
17
+ { type: "session", version: 1, id: "session-1", timestamp: "2026-06-12T00:00:00.000Z", cwd: "/repo/example" },
18
+ { type: "model_change", id: "m1", timestamp: "2026-06-12T00:00:01.000Z", provider: "local", modelId: "qwen-local" },
19
+ { type: "message", id: "u1", timestamp: "2026-06-12T00:00:02.000Z", message: { role: "user", content: [{ type: "text", text: "please fix the failing tests" }] } },
20
+ { type: "message", id: "a1", timestamp: "2026-06-12T00:00:03.000Z", message: { role: "assistant", provider: "local", model: "qwen-local", usage: { inputTokens: 1234 }, content: [{ type: "toolCall", id: "call-1", name: "bash", arguments: { command: "npm test -- --runInBand src/foo.test.ts" } }] } },
21
+ { type: "message", id: "t1", timestamp: "2026-06-12T00:00:04.000Z", message: { role: "toolResult", toolCallId: "call-1", toolName: "bash", isError: true, content: [{ type: "text", text: "FAIL src/foo.test.ts\nError: boom" }] } },
22
+ { type: "message", id: "a2", timestamp: "2026-06-12T00:00:05.000Z", message: { role: "assistant", provider: "local", model: "qwen-local", content: [{ type: "toolCall", id: "call-2", name: "bash", arguments: { command: "npm test -- --runInBand src/foo.test.ts" } }] } },
23
+ { type: "message", id: "t2", timestamp: "2026-06-12T00:00:06.000Z", message: { role: "toolResult", toolCallId: "call-2", toolName: "bash", isError: true, content: [{ type: "text", text: "FAIL src/foo.test.ts\nError: boom" }] } },
24
+ ]);
25
+ }
26
+
27
+ describe("trajectory router checkpoint rebuild", () => {
28
+ it("reads Pi session JSONL and extracts command/tool metadata", () => {
29
+ const session = readPiSession(fixtureSession());
30
+
31
+ expect(session.id).toBe("2026-06-12T00-00-00Z_fixture");
32
+ expect(session.cwd).toBe("/repo/example");
33
+ expect(session.events).toHaveLength(7);
34
+ expect(session.events[3].commandEvents[0]).toMatchObject({ toolName: "bash", isVerifier: true });
35
+ expect(session.events[4].toolResult).toMatchObject({ toolName: "bash", isError: true });
36
+ });
37
+
38
+ it("builds compact derived checkpoints without raw transcript content", () => {
39
+ const checkpoints = rebuildCheckpointsFromSession(fixtureSession());
40
+ const last = checkpoints.at(-1);
41
+
42
+ expect(last?.schema).toBe("pi-router.checkpoint.v1");
43
+ expect(last?.rawSessionRef).toMatchObject({ schema: "pi-router.raw-session-ref.v1", fromEvent: 0, toEvent: 6 });
44
+ expect(last?.activeModel).toBe("qwen-local");
45
+ expect(last?.provider).toBe("local");
46
+ expect(last?.phase).toBe("debug");
47
+ expect(last?.features.contextTokensApprox).toBe(1234);
48
+ expect(last?.features.sameCommandRepeatedCount).toBe(2);
49
+ expect(last?.features.sameErrorRepeatedCount).toBe(2);
50
+ expect(last?.features.verifierUsed).toBe(true);
51
+ expect(last?.features.loopScore).toBeGreaterThan(0);
52
+ expect(last?.recent.lastCommandHash).toBeTruthy();
53
+ expect(last?.recent.lastErrorHash).toBeTruthy();
54
+ expect(last?.recent.touchedFileHashes).toHaveLength(1);
55
+
56
+ const serialized = JSON.stringify(last);
57
+ expect(serialized).not.toContain("please fix the failing tests");
58
+ expect(serialized).not.toContain("npm test");
59
+ expect(serialized).not.toContain("Error: boom");
60
+ expect(serialized).not.toContain("src/foo.test.ts");
61
+ });
62
+
63
+ it("streams checkpoints equivalent to the sync fixture API", async () => {
64
+ const path = fixtureSession();
65
+ const sync = rebuildCheckpointsFromSession(path).map((checkpoint) => checkpoint.checkpointId);
66
+ const streamed: string[] = [];
67
+
68
+ for await (const checkpoint of streamCheckpointsFromSessionPath(path)) streamed.push(checkpoint.checkpointId);
69
+
70
+ expect(streamed).toEqual(sync);
71
+ });
72
+
73
+ it("writes checkpoints as JSONL", () => {
74
+ const session = readPiSession(fixtureSession());
75
+ const checkpoints = buildCheckpoints(session);
76
+ const output = join(mkdtempSync(join(tmpdir(), "pi-router-out-")), "checkpoints.jsonl");
77
+
78
+ writeCheckpointsJsonl(checkpoints, output);
79
+
80
+ const lines = readFileSync(output, "utf8").trim().split("\n");
81
+ expect(lines).toHaveLength(checkpoints.length);
82
+ expect(JSON.parse(lines.at(-1) || "{}").checkpointId).toBe(checkpoints.at(-1)?.checkpointId);
83
+ });
84
+ });