feat(pi): import and reconcile local sessions (#1365)

* feat(pi): expose local session transcripts over machine rpc

* feat(pi): import and incrementally reconcile local sessions

* feat(web): import and resume local Pi sessions

* build(web): precache the expanded app bundle

* fix(pi): harden imported history reconciliation

* fix(pi): persist import cursors and media placeholders

* docs(web): warn about concurrent native Pi writers

* feat(pi): sync native history from session menu

* fix(pi): address import review findings

* fix(web): preserve delivery mode for abort restores

* test(hub): use the Bun test runtime

* fix(pi): preserve custom names during sync

* docs(pi): clarify concurrent session guidance

* perf(hub): index imported Pi sessions once

* perf(hub): reuse Pi import lookup for batches

* fix(web): ignore stale Pi session scans
This commit is contained in:
KorenKrita
2026-08-05 16:08:08 +08:00
committed by GitHub
parent 5122a3fc5f
commit 0201b9f6d4
34 changed files with 2664 additions and 39 deletions
+63
View File
@@ -115,6 +115,15 @@ async function callListCodexSessions(client: ApiMachineClient, machineId: string
return JSON.parse(raw) as unknown
}
async function callListPiSessions(client: ApiMachineClient, machineId: string, params: { cwd?: string | null; sessionIds?: string[] }): Promise<unknown> {
const manager = (client as unknown as { rpcHandlerManager: { handleRequest: (req: { method: string; params: string }) => Promise<string> } }).rpcHandlerManager
const raw = await manager.handleRequest({
method: `${machineId}:listPiSessions`,
params: JSON.stringify(params)
})
return JSON.parse(raw) as unknown
}
async function callArchiveCodexSession(client: ApiMachineClient, machineId: string, sessionId: string): Promise<unknown> {
const manager = (client as unknown as { rpcHandlerManager: { handleRequest: (req: { method: string; params: string }) => Promise<string> } }).rpcHandlerManager
const raw = await manager.handleRequest({
@@ -135,6 +144,17 @@ function writeCodexTranscript(codexHome: string, fileName: string, payload: Reco
return file
}
function writePiTranscript(sessionsRoot: string, fileName: string, sessionId: string, cwd: string, userText: string): string {
const sessionDir = join(sessionsRoot, '--project--')
mkdirSync(sessionDir, { recursive: true })
const file = join(sessionDir, fileName)
writeFileSync(file, [
JSON.stringify({ type: 'session', version: 3, id: sessionId, cwd }),
JSON.stringify({ type: 'message', id: `${sessionId}-user`, parentId: null, message: { role: 'user', content: userText } })
].join('\n'))
return file
}
describe('ApiMachineClient cursor-chat-store-status handler', () => {
beforeEach(() => {
inspectCursorChatStoreMock.mockReset()
@@ -490,6 +510,49 @@ describe('ApiMachineClient Codex transcript handlers', () => {
})
describe('ApiMachineClient Pi transcript handlers', () => {
const originalPiSessions = process.env.PI_CODING_AGENT_SESSION_DIR
let workspaceRoot: string
let outsideRoot: string
let piSessions: string
beforeEach(() => {
ioMock.mockReset()
workspaceRoot = mkdtempSync(join(tmpdir(), 'hapi-pi-allowed-'))
outsideRoot = mkdtempSync(join(tmpdir(), 'hapi-pi-outside-'))
piSessions = mkdtempSync(join(tmpdir(), 'hapi-pi-sessions-'))
process.env.PI_CODING_AGENT_SESSION_DIR = piSessions
})
afterEach(() => {
if (originalPiSessions === undefined) delete process.env.PI_CODING_AGENT_SESSION_DIR
else process.env.PI_CODING_AGENT_SESSION_DIR = originalPiSessions
rmSync(workspaceRoot, { recursive: true, force: true })
rmSync(outsideRoot, { recursive: true, force: true })
rmSync(piSessions, { recursive: true, force: true })
})
it('filters Pi summaries and full transcripts to workspace roots', async () => {
writePiTranscript(piSessions, 'allowed.jsonl', 'allowed-pi-session', workspaceRoot, 'allowed')
writePiTranscript(piSessions, 'outside.jsonl', 'outside-pi-session', outsideRoot, 'outside')
const machine = makeMachine('pi-machine-1')
const client = new ApiMachineClient('cli-token', machine, [workspaceRoot])
try {
const summaries = await callListPiSessions(client, machine.id, {}) as { success: true; sessions: Array<{ id: string }> }
expect(summaries.sessions.map((session) => session.id)).toEqual(['allowed-pi-session'])
const full = await callListPiSessions(client, machine.id, {
sessionIds: ['allowed-pi-session', 'outside-pi-session']
}) as { success: true; sessions: Array<{ id: string; messages: unknown[] }> }
expect(full.sessions.map((session) => session.id)).toEqual(['allowed-pi-session'])
expect(full.sessions[0]?.messages).toHaveLength(1)
} finally {
client.shutdown()
}
})
})
describe('ApiMachineClient SpawnHappySession handler', () => {
let workspaceRoot: string