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.
52 lines
2.1 KiB
JavaScript
52 lines
2.1 KiB
JavaScript
import { test } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { createRequire } from 'node:module';
|
|
import { readFileSync } from 'node:fs';
|
|
|
|
import { createQueryApi } from '../packages/core/src/query.ts';
|
|
import { createProviderRegistry } from '../packages/core/src/providers/registry.ts';
|
|
|
|
const require = createRequire(import.meta.url);
|
|
const { DatabaseSync } = require('node:sqlite');
|
|
const SCHEMA = readFileSync(new URL('../packages/core/src/schema.sql', import.meta.url), 'utf8');
|
|
|
|
test('raw query delegates source semantics to the registered provider', () => {
|
|
const calls = [];
|
|
const registry = createProviderRegistry([{
|
|
name: 'alpha',
|
|
descriptor: { id: 'alpha', name: 'Alpha', vendor: 'Test', defaultRoot: '/alpha', color: '#123456' },
|
|
watchRoots: () => [],
|
|
discover: () => [],
|
|
*parse() { yield* []; return null; },
|
|
raw(input) {
|
|
calls.push(input);
|
|
return { text: '0123456789', totalLength: 10 };
|
|
},
|
|
}]);
|
|
const db = new DatabaseSync(':memory:');
|
|
db.exec(SCHEMA);
|
|
db.prepare('INSERT INTO sessions (id,jsonl_path,source) VALUES (?,?,?)')
|
|
.run('alpha:session', '/alpha/session.data', 'alpha');
|
|
db.prepare('INSERT INTO messages (uuid,session_id,agent_id,source) VALUES (?,?,?,?)')
|
|
.run('alpha:message', 'alpha:session', 'alpha:agent', 'alpha');
|
|
db.prepare('INSERT INTO subagents (agent_id,session_id,description) VALUES (?,?,?)')
|
|
.run('alpha:agent', 'alpha:session', 'agent metadata');
|
|
db.prepare('INSERT INTO index_state (jsonl_path,mtime,lines_processed,cursor) VALUES (?,?,?,?)')
|
|
.run('/alpha/session.data', 10, 1, 'committed-alpha-cursor');
|
|
|
|
const result = createQueryApi(db, { providerRegistry: registry }).raw('alpha:message', {
|
|
offset: 2,
|
|
limit: 4,
|
|
});
|
|
|
|
assert.deepEqual(result, {
|
|
text: '2345', totalLength: 10, offset: 2, limit: 4, hasMore: true, visibility: 'visible',
|
|
});
|
|
assert.equal(calls.length, 1);
|
|
assert.equal(calls[0].source, 'alpha');
|
|
assert.equal(calls[0].session.id, 'alpha:session');
|
|
assert.equal(calls[0].cursor, 'committed-alpha-cursor');
|
|
assert.equal(calls[0].subagent.description, 'agent metadata');
|
|
db.close();
|
|
});
|