feat(core): add first-class Pi session indexing (#23)

Pi cannot be read as another linear JSONL stream. Its history is a tree with
a durable leaf, orphan roots, branch summaries, and two compaction forms, so
the active context is something the format states rather than something line
order implies. The adapter keeps those semantics inside itself and projects
the result into the existing canonical tables.

Sessions are keyed by (normalized header cwd, header id) rather than by path,
because Pi's --session-id lookup is project-local: two projects may reuse an
id, while a move or an identical copy is still one session. Discovery covers
both layouts Pi writes and fingerprints each file by mtime, ctime, size and
inode, so a rewrite that preserves mtime is not read as unchanged.

Abandoned branches are preserved rather than dropped. Visibility becomes
three-state -- visible, inactive, hidden -- and helpers return only visible
rows until includeInactive asks for the superseded path, labeling every row
so a caller knows which it holds. Usage counts all three, because an
abandoned call still spent tokens; message_count reports only the visible
transcript.

A committed MIT-licensed oracle transcribed from Pi 0.83.0 pins the context
algorithms, and a fixed-seed differential runs 512 generated sessions against
it on every test run. Schema changes are additive.
This commit is contained in:
SaladDay
2026-08-04 23:33:01 +08:00
committed by GitHub
parent 1941e64572
commit 2589384e68
63 changed files with 7796 additions and 374 deletions
+8
View File
@@ -349,6 +349,7 @@ for (const { facet, terms } of learnedFacets) {
JOIN sessions s ON s.id = m.session_id
WHERE m.session_id IN (${sessionIds.map(() => '?').join(',')})
AND m.text IS NOT NULL
AND COALESCE(m.visibility, 'visible') = 'visible'
AND (${clauses})
ORDER BY m.timestamp
LIMIT 3
@@ -448,6 +449,7 @@ const before = sql(
`SELECT uuid, role, timestamp, substr(text,1,200) AS snippet
FROM messages
WHERE session_id=? AND timestamp<?
AND COALESCE(visibility, 'visible') = 'visible'
ORDER BY timestamp DESC LIMIT 3`,
s.session_id,
s.timestamp
@@ -456,6 +458,7 @@ const after = sql(
`SELECT uuid, role, timestamp, substr(text,1,200) AS snippet
FROM messages
WHERE session_id=? AND timestamp>?
AND COALESCE(visibility, 'visible') = 'visible'
ORDER BY timestamp ASC LIMIT 3`,
s.session_id,
s.timestamp
@@ -532,6 +535,7 @@ const counts = sql(`
JOIN messages m ON m.uuid = tr.message_uuid
JOIN sessions s ON s.id = tr.session_id
WHERE tr.is_error = 1
AND COALESCE(m.visibility, 'visible') = 'visible'
AND s.project LIKE ?
GROUP BY tc.name
ORDER BY failure_count DESC, last_failure_at DESC
@@ -551,6 +555,7 @@ const examples = sql(`
JOIN messages m ON m.uuid = tr.message_uuid
JOIN sessions s ON s.id = tr.session_id
WHERE tr.is_error = 1
AND COALESCE(m.visibility, 'visible') = 'visible'
AND s.project LIKE ?
ORDER BY m.timestamp DESC
LIMIT 8
@@ -580,6 +585,7 @@ const groups = sql(`
JOIN messages m ON m.uuid = tr.message_uuid
JOIN sessions s ON s.id = tr.session_id
WHERE tr.is_error = 1
AND COALESCE(m.visibility, 'visible') = 'visible'
AND s.project LIKE ?
GROUP BY s.id
ORDER BY last_failure_at DESC
@@ -598,6 +604,7 @@ const examples = sql(`
JOIN messages m ON m.uuid = tr.message_uuid
JOIN sessions s ON s.id = tr.session_id
WHERE tr.is_error = 1
AND COALESCE(m.visibility, 'visible') = 'visible'
AND s.project LIKE ?
ORDER BY m.timestamp DESC
LIMIT 12
@@ -694,6 +701,7 @@ const row = sql(`
SELECT uuid, length(text) AS indexed_len
FROM messages
WHERE length(text) >= 10000
AND COALESCE(visibility, 'visible') = 'visible'
LIMIT 1
`)[0];
if (!row) return null;