pi-chat-recap 0.1.0 → 0.1.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 CHANGED
@@ -44,9 +44,12 @@ pi -e ./pi-chat-recap/index.ts
44
44
 
45
45
  ## Journal (for renderers)
46
46
 
47
- The extension appends an append-only JSONL journal next to the Pi session
48
- file: `pi-chat-recap.journal.jsonl`. Each line is self-sufficient so
49
- consumers can rebuild state by tailing:
47
+ The extension appends an append-only JSONL journal PER SESSION, next to the
48
+ session file: `<session-file>.pi-chat-recap.journal.jsonl` (the session
49
+ file's stem + the producer name). Each session owns its journal, so two
50
+ sessions in the same directory never share history and a resumed session
51
+ continues its exchange ids above the ones already recorded. Each line is
52
+ self-sufficient so consumers can rebuild state by tailing:
50
53
 
51
54
  ```json
52
55
  {"v":1,"producer":"pi-chat-recap","sessionId":"…","ts":"…","type":"exchange","data":{"id":1,"status":"aborted","user":{"text":"…","ts":"…","cancelled":true},"assistant":{"text":null,"ts":"…","marker":"No response (operation aborted)"}}}
package/index.ts CHANGED
@@ -5,7 +5,7 @@ import {
5
5
  assistantText,
6
6
  EXCHANGE_STATUS,
7
7
  } from './lib/exchange.mjs';
8
- import { createJournal } from './lib/journal.mjs';
8
+ import { createJournal, maxExchangeIdIn } from './lib/journal.mjs';
9
9
  import { createSummarizer, loadPromptFile } from './lib/summary.mjs';
10
10
  import { loadRecapConfig, recapPaths } from './lib/config.mjs';
11
11
  import { join } from 'node:path';
@@ -77,10 +77,14 @@ export default function chatRecap(pi: ExtensionAPI) {
77
77
  pi.on('session_start', async (_event, ctx) => {
78
78
  ctxRef = ctx;
79
79
  uiRef = ctx;
80
- journal.openSession(
80
+ const bound = journal.openSession(
81
81
  ctx.sessionManager?.getSessionFile?.(),
82
82
  ctx.sessionManager?.getSessionId?.(),
83
83
  );
84
+ // S3 (per-session resume): Pi resume or an extension reload rebinds to
85
+ // a session file whose journal already holds exchanges with ids 1..N.
86
+ // Continue above N so a new id never collides with a recorded one.
87
+ log.seedNextId(maxExchangeIdIn(bound));
84
88
  // Surface config/prompt problems once at startup, not per exchange.
85
89
  const { warnings } = loadRecapConfig(join(homedir(), '.pi', 'agent'));
86
90
  for (const w of warnings) warnOnce(w);
package/lib/exchange.mjs CHANGED
@@ -75,6 +75,14 @@ export function createExchangeLog({ detect = detectLanguage } = {}) {
75
75
  let nextId = 1;
76
76
 
77
77
  const log = {
78
+ // S3 (per-session resume): raise the counter above the max exchange id
79
+ // already in a resumed journal so the renderer's last-record-per-id
80
+ // fold never collides within one session. A smaller value never
81
+ // regresses the natural counter.
82
+ seedNextId(max) {
83
+ const n = Number(max);
84
+ if (Number.isFinite(n) && n >= nextId) nextId = Math.floor(n) + 1;
85
+ },
78
86
  // A user message starts (or reopens) the pending exchange.
79
87
  beginUser(text, ts) {
80
88
  const exchange = {
package/lib/journal.mjs CHANGED
@@ -1,16 +1,44 @@
1
- // Append-only JSONL journal (producer-only). One file per session, written
2
- // next to the Pi session file; consumers (herdr-sidecar or any renderer)
3
- // tail it and own all presentation. Line contract:
1
+ // Append-only JSONL journal (producer-only). One file per session, named
2
+ // after the session file's stem and written next to the Pi session file;
3
+ // consumers (herdr-sidecar or any renderer) tail it and own all
4
+ // presentation. Line contract:
4
5
  // {v:1, producer, sessionId, ts, type, data}
5
6
  // Consumers must tolerate unknown fields and unknown `type` values.
6
7
 
7
- import { appendFileSync, mkdirSync } from 'node:fs';
8
- import { dirname, join } from 'node:path';
8
+ import { appendFileSync, mkdirSync, readFileSync } from 'node:fs';
9
+ import { basename, dirname, extname, join } from 'node:path';
9
10
 
10
11
  export const JOURNAL_VERSION = 1;
11
12
 
13
+ // Per-session layout (S1/S5): one journal per session file, named after the
14
+ // session file's stem, so two sessions in the same directory never share
15
+ // history. The stem keeps everything before the final extension (a multi-dot
16
+ // Pi session name like `2026-11-22T03-12-00Z_abc.jsonl` is preserved whole).
12
17
  export function journalPathFor(sessionFile, name) {
13
- return join(dirname(sessionFile), `${name}.journal.jsonl`);
18
+ const base = basename(sessionFile);
19
+ const stem = base.slice(0, base.length - extname(base).length);
20
+ return join(dirname(sessionFile), `${stem}.${name}.journal.jsonl`);
21
+ }
22
+
23
+ // S3 (per-session resume): the max `data.id` of the exchange records a
24
+ // journal file already holds, so a resumed session continues above it and
25
+ // the renderer's last-record-per-id fold never collides. A missing or
26
+ // unreadable file (fresh session, torn write) yields 0 — never an error.
27
+ export function maxExchangeIdIn(file) {
28
+ if (!file) return 0;
29
+ try {
30
+ const text = readFileSync(file, 'utf8');
31
+ let max = 0;
32
+ for (const record of parseJournal(text)) {
33
+ if (record?.type === 'exchange') {
34
+ const id = Number(record?.data?.id);
35
+ if (Number.isFinite(id) && id > max) max = id;
36
+ }
37
+ }
38
+ return max;
39
+ } catch {
40
+ return 0;
41
+ }
14
42
  }
15
43
 
16
44
  export function createJournal({ file, sessionId, producer, now = () => new Date().toISOString() } = {}) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-chat-recap",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Producer-only chat recap data for Pi: conversation exchanges with real timestamps, cancellation states and session-fixed language. Renders nothing; built to feed renderers like herdr-sidecar.",
5
5
  "license": "MIT",
6
6
  "type": "module",