@ai-devkit/agent-manager 0.29.0 → 0.30.0
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/dist/__tests__/adapters/ClaudeCodeAdapter.test.js +1 -342
- package/dist/__tests__/adapters/ClaudeCodeAdapter.test.js.map +1 -1
- package/dist/__tests__/database/DurableAgentsDatabase.test.js +31 -3
- package/dist/__tests__/database/DurableAgentsDatabase.test.js.map +1 -1
- package/dist/__tests__/durable/CodexCliProbe.test.js +129 -0
- package/dist/__tests__/durable/CodexCliProbe.test.js.map +1 -0
- package/dist/__tests__/durable/CodexDurableAgentRepository.test.js +84 -0
- package/dist/__tests__/durable/CodexDurableAgentRepository.test.js.map +1 -0
- package/dist/__tests__/durable/CodexPrintAgent.integration.test.js +126 -0
- package/dist/__tests__/durable/CodexPrintAgent.integration.test.js.map +1 -0
- package/dist/__tests__/durable/CodexPrintAgentService.test.js +271 -0
- package/dist/__tests__/durable/CodexPrintAgentService.test.js.map +1 -0
- package/dist/__tests__/durable/CodexPrintRunner.test.js +393 -0
- package/dist/__tests__/durable/CodexPrintRunner.test.js.map +1 -0
- package/dist/__tests__/print/ClaudeCliProbe.test.js +3 -7
- package/dist/__tests__/print/ClaudeCliProbe.test.js.map +1 -1
- package/dist/__tests__/print/ClaudePrintAgent.integration.test.js +25 -28
- package/dist/__tests__/print/ClaudePrintAgent.integration.test.js.map +1 -1
- package/dist/__tests__/print/ClaudePrintAgentService.test.js +65 -0
- package/dist/__tests__/print/ClaudePrintAgentService.test.js.map +1 -1
- package/dist/__tests__/print/ClaudePrintRunner.test.js +4 -10
- package/dist/__tests__/print/ClaudePrintRunner.test.js.map +1 -1
- package/dist/__tests__/providers/claude/ClaudeAgentMapper.test.js +79 -0
- package/dist/__tests__/providers/claude/ClaudeAgentMapper.test.js.map +1 -0
- package/dist/__tests__/providers/claude/ClaudeSessionLocator.test.js +436 -0
- package/dist/__tests__/providers/claude/ClaudeSessionLocator.test.js.map +1 -0
- package/dist/__tests__/providers/pi/durable/PiCliProbe.test.js +76 -0
- package/dist/__tests__/providers/pi/durable/PiCliProbe.test.js.map +1 -0
- package/dist/__tests__/providers/pi/durable/PiDurableAgentRepository.test.js +43 -0
- package/dist/__tests__/providers/pi/durable/PiDurableAgentRepository.test.js.map +1 -0
- package/dist/__tests__/providers/pi/durable/PiPrintAgentService.test.js +202 -0
- package/dist/__tests__/providers/pi/durable/PiPrintAgentService.test.js.map +1 -0
- package/dist/__tests__/providers/pi/durable/PiPrintProtocol.test.js +119 -0
- package/dist/__tests__/providers/pi/durable/PiPrintProtocol.test.js.map +1 -0
- package/dist/__tests__/providers/pi/durable/PiPrintRunner.test.js +386 -0
- package/dist/__tests__/providers/pi/durable/PiPrintRunner.test.js.map +1 -0
- package/dist/__tests__/providers/pi/durable/PiStreamParser.test.js +34 -0
- package/dist/__tests__/providers/pi/durable/PiStreamParser.test.js.map +1 -0
- package/dist/__tests__/utils/ClaudeSessionParser.test.js +2 -2
- package/dist/__tests__/utils/ClaudeSessionParser.test.js.map +1 -1
- package/dist/adapters/index.d.ts +1 -1
- package/dist/adapters/index.d.ts.map +1 -1
- package/dist/adapters/index.js +1 -1
- package/dist/adapters/index.js.map +1 -1
- package/dist/database/migrations/004_codex_durable_sessions.sql +1 -0
- package/dist/durable/DurableAgent.d.ts +23 -3
- package/dist/durable/DurableAgent.d.ts.map +1 -1
- package/dist/durable/DurableAgent.js +12 -0
- package/dist/durable/DurableAgent.js.map +1 -1
- package/dist/durable/DurableAgentRepository.d.ts +6 -7
- package/dist/durable/DurableAgentRepository.d.ts.map +1 -1
- package/dist/durable/DurableAgentRepository.js +73 -42
- package/dist/durable/DurableAgentRepository.js.map +1 -1
- package/dist/durable/process.d.ts +8 -0
- package/dist/durable/process.d.ts.map +1 -0
- package/dist/durable/process.js +40 -0
- package/dist/durable/process.js.map +1 -0
- package/dist/durable/run.d.ts +32 -0
- package/dist/durable/run.d.ts.map +1 -0
- package/dist/durable/run.js +46 -0
- package/dist/durable/run.js.map +1 -0
- package/dist/durable/utils.d.ts +19 -0
- package/dist/durable/utils.d.ts.map +1 -0
- package/dist/durable/utils.js +52 -0
- package/dist/durable/utils.js.map +1 -0
- package/dist/index.d.ts +17 -13
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -8
- package/dist/index.js.map +1 -1
- package/dist/providers/claude/ClaudeAgentMapper.d.ts +21 -0
- package/dist/providers/claude/ClaudeAgentMapper.d.ts.map +1 -0
- package/dist/providers/claude/ClaudeAgentMapper.js +42 -0
- package/dist/providers/claude/ClaudeAgentMapper.js.map +1 -0
- package/dist/providers/claude/ClaudeCodeAdapter.d.ts +29 -0
- package/dist/providers/claude/ClaudeCodeAdapter.d.ts.map +1 -0
- package/dist/providers/claude/ClaudeCodeAdapter.js +129 -0
- package/dist/providers/claude/ClaudeCodeAdapter.js.map +1 -0
- package/dist/{adapters/ClaudeCodeAdapter.d.ts → providers/claude/ClaudeSessionLocator.d.ts} +68 -52
- package/dist/providers/claude/ClaudeSessionLocator.d.ts.map +1 -0
- package/dist/{adapters/ClaudeCodeAdapter.js → providers/claude/ClaudeSessionLocator.js} +71 -189
- package/dist/providers/claude/ClaudeSessionLocator.js.map +1 -0
- package/dist/{utils → providers/claude}/ClaudeSessionParser.d.ts +2 -2
- package/dist/providers/claude/ClaudeSessionParser.d.ts.map +1 -0
- package/dist/{utils → providers/claude}/ClaudeSessionParser.js +1 -1
- package/dist/providers/claude/ClaudeSessionParser.js.map +1 -0
- package/dist/providers/claude/durable/ClaudeCliProbe.d.ts.map +1 -0
- package/dist/{durable → providers/claude/durable}/ClaudeCliProbe.js +4 -9
- package/dist/providers/claude/durable/ClaudeCliProbe.js.map +1 -0
- package/dist/{durable → providers/claude/durable}/ClaudePrintAgentService.d.ts +4 -9
- package/dist/providers/claude/durable/ClaudePrintAgentService.d.ts.map +1 -0
- package/dist/providers/claude/durable/ClaudePrintAgentService.js +45 -0
- package/dist/providers/claude/durable/ClaudePrintAgentService.js.map +1 -0
- package/dist/{durable → providers/claude/durable}/ClaudePrintRunner.d.ts +3 -3
- package/dist/providers/claude/durable/ClaudePrintRunner.d.ts.map +1 -0
- package/dist/providers/claude/durable/ClaudePrintRunner.js +75 -0
- package/dist/providers/claude/durable/ClaudePrintRunner.js.map +1 -0
- package/dist/providers/claude/durable/ClaudeStreamParser.d.ts +17 -0
- package/dist/providers/claude/durable/ClaudeStreamParser.d.ts.map +1 -0
- package/dist/providers/claude/durable/ClaudeStreamParser.js +69 -0
- package/dist/providers/claude/durable/ClaudeStreamParser.js.map +1 -0
- package/dist/providers/codex/durable/CodexCliProbe.d.ts +20 -0
- package/dist/providers/codex/durable/CodexCliProbe.d.ts.map +1 -0
- package/dist/providers/codex/durable/CodexCliProbe.js +61 -0
- package/dist/providers/codex/durable/CodexCliProbe.js.map +1 -0
- package/dist/providers/codex/durable/CodexPrintAgentService.d.ts +40 -0
- package/dist/providers/codex/durable/CodexPrintAgentService.d.ts.map +1 -0
- package/dist/providers/codex/durable/CodexPrintAgentService.js +50 -0
- package/dist/providers/codex/durable/CodexPrintAgentService.js.map +1 -0
- package/dist/providers/codex/durable/CodexPrintRunner.d.ts +34 -0
- package/dist/providers/codex/durable/CodexPrintRunner.d.ts.map +1 -0
- package/dist/providers/codex/durable/CodexPrintRunner.js +78 -0
- package/dist/providers/codex/durable/CodexPrintRunner.js.map +1 -0
- package/dist/providers/codex/durable/CodexStreamParser.d.ts +21 -0
- package/dist/providers/codex/durable/CodexStreamParser.d.ts.map +1 -0
- package/dist/providers/codex/durable/CodexStreamParser.js +86 -0
- package/dist/providers/codex/durable/CodexStreamParser.js.map +1 -0
- package/dist/providers/pi/durable/PiCliProbe.d.ts +20 -0
- package/dist/providers/pi/durable/PiCliProbe.d.ts.map +1 -0
- package/dist/providers/pi/durable/PiCliProbe.js +51 -0
- package/dist/providers/pi/durable/PiCliProbe.js.map +1 -0
- package/dist/providers/pi/durable/PiPrintAgentService.d.ts +39 -0
- package/dist/providers/pi/durable/PiPrintAgentService.d.ts.map +1 -0
- package/dist/providers/pi/durable/PiPrintAgentService.js +47 -0
- package/dist/providers/pi/durable/PiPrintAgentService.js.map +1 -0
- package/dist/providers/pi/durable/PiPrintProtocol.d.ts +4 -0
- package/dist/providers/pi/durable/PiPrintProtocol.d.ts.map +1 -0
- package/dist/providers/pi/durable/PiPrintProtocol.js +44 -0
- package/dist/providers/pi/durable/PiPrintProtocol.js.map +1 -0
- package/dist/providers/pi/durable/PiPrintRunner.d.ts +32 -0
- package/dist/providers/pi/durable/PiPrintRunner.d.ts.map +1 -0
- package/dist/providers/pi/durable/PiPrintRunner.js +61 -0
- package/dist/providers/pi/durable/PiPrintRunner.js.map +1 -0
- package/dist/providers/pi/durable/PiStreamParser.d.ts +21 -0
- package/dist/providers/pi/durable/PiStreamParser.d.ts.map +1 -0
- package/dist/providers/pi/durable/PiStreamParser.js +71 -0
- package/dist/providers/pi/durable/PiStreamParser.js.map +1 -0
- package/package.json +1 -1
- package/src/__tests__/adapters/ClaudeCodeAdapter.test.ts +1 -307
- package/src/__tests__/database/DurableAgentsDatabase.test.ts +28 -3
- package/src/__tests__/durable/CodexCliProbe.test.ts +63 -0
- package/src/__tests__/durable/CodexDurableAgentRepository.test.ts +54 -0
- package/src/__tests__/durable/CodexPrintAgent.integration.test.ts +79 -0
- package/src/__tests__/durable/CodexPrintAgentService.test.ts +139 -0
- package/src/__tests__/durable/CodexPrintRunner.test.ts +189 -0
- package/src/__tests__/fixtures/fake-codex.cjs +45 -0
- package/src/__tests__/print/ClaudeCliProbe.test.ts +3 -7
- package/src/__tests__/print/ClaudePrintAgent.integration.test.ts +11 -21
- package/src/__tests__/print/ClaudePrintAgentService.test.ts +33 -1
- package/src/__tests__/print/ClaudePrintRunner.test.ts +4 -10
- package/src/__tests__/providers/claude/ClaudeAgentMapper.test.ts +84 -0
- package/src/__tests__/providers/claude/ClaudeSessionLocator.test.ts +411 -0
- package/src/__tests__/providers/pi/durable/PiCliProbe.test.ts +38 -0
- package/src/__tests__/providers/pi/durable/PiDurableAgentRepository.test.ts +32 -0
- package/src/__tests__/providers/pi/durable/PiPrintAgentService.test.ts +69 -0
- package/src/__tests__/providers/pi/durable/PiPrintProtocol.test.ts +33 -0
- package/src/__tests__/providers/pi/durable/PiPrintRunner.test.ts +107 -0
- package/src/__tests__/providers/pi/durable/PiStreamParser.test.ts +24 -0
- package/src/__tests__/utils/ClaudeSessionParser.test.ts +2 -2
- package/src/adapters/index.ts +1 -1
- package/src/database/migrations/004_codex_durable_sessions.sql +1 -0
- package/src/durable/DurableAgent.ts +51 -3
- package/src/durable/DurableAgentRepository.ts +60 -31
- package/src/durable/process.ts +28 -0
- package/src/durable/run.ts +74 -0
- package/src/durable/utils.ts +61 -0
- package/src/index.ts +31 -15
- package/src/providers/claude/ClaudeAgentMapper.ts +62 -0
- package/src/providers/claude/ClaudeCodeAdapter.ts +160 -0
- package/src/{adapters/ClaudeCodeAdapter.ts → providers/claude/ClaudeSessionLocator.ts} +97 -239
- package/src/{utils → providers/claude}/ClaudeSessionParser.ts +2 -2
- package/src/{durable → providers/claude/durable}/ClaudeCliProbe.ts +4 -10
- package/src/providers/claude/durable/ClaudePrintAgentService.ts +66 -0
- package/src/providers/claude/durable/ClaudePrintRunner.ts +92 -0
- package/src/providers/claude/durable/ClaudeStreamParser.ts +79 -0
- package/src/providers/codex/durable/CodexCliProbe.ts +60 -0
- package/src/providers/codex/durable/CodexPrintAgentService.ts +65 -0
- package/src/providers/codex/durable/CodexPrintRunner.ts +94 -0
- package/src/providers/codex/durable/CodexStreamParser.ts +104 -0
- package/src/providers/pi/durable/PiCliProbe.ts +48 -0
- package/src/providers/pi/durable/PiPrintAgentService.ts +69 -0
- package/src/providers/pi/durable/PiPrintProtocol.ts +39 -0
- package/src/providers/pi/durable/PiPrintRunner.ts +88 -0
- package/src/providers/pi/durable/PiStreamParser.ts +84 -0
- package/dist/adapters/ClaudeCodeAdapter.d.ts.map +0 -1
- package/dist/adapters/ClaudeCodeAdapter.js.map +0 -1
- package/dist/durable/ClaudeCliProbe.d.ts.map +0 -1
- package/dist/durable/ClaudeCliProbe.js.map +0 -1
- package/dist/durable/ClaudePrintAgentService.d.ts.map +0 -1
- package/dist/durable/ClaudePrintAgentService.js +0 -66
- package/dist/durable/ClaudePrintAgentService.js.map +0 -1
- package/dist/durable/ClaudePrintRunner.d.ts.map +0 -1
- package/dist/durable/ClaudePrintRunner.js +0 -128
- package/dist/durable/ClaudePrintRunner.js.map +0 -1
- package/dist/utils/ClaudeSessionParser.d.ts.map +0 -1
- package/dist/utils/ClaudeSessionParser.js.map +0 -1
- package/src/durable/ClaudePrintAgentService.ts +0 -94
- package/src/durable/ClaudePrintRunner.ts +0 -139
- /package/dist/{durable → providers/claude/durable}/ClaudeCliProbe.d.ts +0 -0
|
@@ -0,0 +1,411 @@
|
|
|
1
|
+
import type { MockedFunction } from 'vitest';
|
|
2
|
+
import * as fs from 'fs';
|
|
3
|
+
import * as os from 'os';
|
|
4
|
+
import * as path from 'path';
|
|
5
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
6
|
+
|
|
7
|
+
import { AgentStatus, type ProcessInfo } from '../../../adapters/AgentAdapter.js';
|
|
8
|
+
import { ClaudeSessionLocator } from '../../../providers/claude/ClaudeSessionLocator.js';
|
|
9
|
+
import { batchGetSessionFileBirthtimes } from '../../../utils/session.js';
|
|
10
|
+
import type { SessionFile } from '../../../utils/session.js';
|
|
11
|
+
|
|
12
|
+
vi.mock('../../../utils/session.js', async (importOriginal) => {
|
|
13
|
+
const actual = await importOriginal() as typeof import('../../../utils/session.js');
|
|
14
|
+
return {
|
|
15
|
+
...actual,
|
|
16
|
+
batchGetSessionFileBirthtimes: vi.fn(),
|
|
17
|
+
};
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
const mockedBatchGetSessionFileBirthtimes =
|
|
21
|
+
batchGetSessionFileBirthtimes as MockedFunction<typeof batchGetSessionFileBirthtimes>;
|
|
22
|
+
|
|
23
|
+
const tmpDirs: string[] = [];
|
|
24
|
+
|
|
25
|
+
function makeTmpDir(): string {
|
|
26
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'claude-locator-test-'));
|
|
27
|
+
tmpDirs.push(dir);
|
|
28
|
+
return dir;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function makeProcess(overrides: Partial<ProcessInfo> = {}): ProcessInfo {
|
|
32
|
+
return {
|
|
33
|
+
pid: 123,
|
|
34
|
+
command: 'claude',
|
|
35
|
+
cwd: '/repo/my-app',
|
|
36
|
+
tty: 'ttys001',
|
|
37
|
+
startTime: new Date('2026-08-23T10:00:00.000Z'),
|
|
38
|
+
...overrides,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
type PidFileMatching = (processes: ProcessInfo[]) => {
|
|
43
|
+
direct: Array<{ process: ProcessInfo; sessionFile: SessionFile }>;
|
|
44
|
+
fallback: ProcessInfo[];
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/** `tryPidFileMatching` is private; tests drive it directly like the sibling adapter suites do. */
|
|
48
|
+
function bindPidFileMatching(locator: ClaudeSessionLocator): PidFileMatching {
|
|
49
|
+
return (locator as unknown as { tryPidFileMatching: PidFileMatching })
|
|
50
|
+
.tryPidFileMatching.bind(locator) as PidFileMatching;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** `getProjectDir` is private; bound here so the encoding tests read cleanly. */
|
|
54
|
+
function bindProjectDir(locator: ClaudeSessionLocator): (cwd: string) => string {
|
|
55
|
+
return (locator as unknown as { getProjectDir: (cwd: string) => string })
|
|
56
|
+
.getProjectDir.bind(locator);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
describe('ClaudeSessionLocator', () => {
|
|
60
|
+
beforeEach(() => {
|
|
61
|
+
mockedBatchGetSessionFileBirthtimes.mockReset();
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
afterEach(() => {
|
|
65
|
+
for (const dir of tmpDirs.splice(0)) {
|
|
66
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('matches resumed sessions directly and carries live PID status metadata', () => {
|
|
71
|
+
const root = makeTmpDir();
|
|
72
|
+
const projectsDir = path.join(root, 'projects');
|
|
73
|
+
const sessionsDir = path.join(root, 'sessions');
|
|
74
|
+
const cwd = '/repo/my-app';
|
|
75
|
+
const sessionId = '12345678-1234-1234-1234-123456789abc';
|
|
76
|
+
const projectDir = path.join(projectsDir, '-repo-my-app');
|
|
77
|
+
const sessionFile = path.join(projectDir, `${sessionId}.jsonl`);
|
|
78
|
+
fs.mkdirSync(projectDir, { recursive: true });
|
|
79
|
+
fs.mkdirSync(sessionsDir, { recursive: true });
|
|
80
|
+
fs.writeFileSync(sessionFile, '{}\n');
|
|
81
|
+
fs.writeFileSync(path.join(sessionsDir, '123.json'), JSON.stringify({
|
|
82
|
+
pid: 123,
|
|
83
|
+
sessionId,
|
|
84
|
+
cwd,
|
|
85
|
+
startedAt: new Date('2026-08-23T10:00:00.000Z').getTime(),
|
|
86
|
+
kind: 'interactive',
|
|
87
|
+
entrypoint: 'cli',
|
|
88
|
+
status: 'waiting',
|
|
89
|
+
waitingFor: 'approve Read',
|
|
90
|
+
}));
|
|
91
|
+
|
|
92
|
+
const locator = new ClaudeSessionLocator({ projectsDir, sessionsDir });
|
|
93
|
+
const matches = locator.matchRunningProcesses([
|
|
94
|
+
makeProcess({ command: `claude --resume ${sessionId}`, cwd }),
|
|
95
|
+
]);
|
|
96
|
+
|
|
97
|
+
expect(matches.direct).toHaveLength(1);
|
|
98
|
+
expect(matches.legacyMatches).toEqual([]);
|
|
99
|
+
expect(matches.direct[0]).toMatchObject({
|
|
100
|
+
pidStatus: AgentStatus.WAITING,
|
|
101
|
+
waitingFor: 'approve Read',
|
|
102
|
+
sessionFile: {
|
|
103
|
+
sessionId,
|
|
104
|
+
filePath: sessionFile,
|
|
105
|
+
projectDir,
|
|
106
|
+
resolvedCwd: cwd,
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
describe('discoverLiveSessions', () => {
|
|
111
|
+
let tmpDir: string;
|
|
112
|
+
let projectsDir: string;
|
|
113
|
+
let locator: ClaudeSessionLocator;
|
|
114
|
+
|
|
115
|
+
beforeEach(() => {
|
|
116
|
+
tmpDir = makeTmpDir();
|
|
117
|
+
projectsDir = path.join(tmpDir, 'projects');
|
|
118
|
+
locator = new ClaudeSessionLocator({
|
|
119
|
+
projectsDir,
|
|
120
|
+
sessionsDir: path.join(tmpDir, 'sessions'),
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it('should return empty when projects dir does not exist', () => {
|
|
125
|
+
const missing = new ClaudeSessionLocator({
|
|
126
|
+
projectsDir: path.join(tmpDir, 'nonexistent'),
|
|
127
|
+
sessionsDir: path.join(tmpDir, 'sessions'),
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
const result = missing.discoverLiveSessions([
|
|
131
|
+
{ pid: 1, command: 'claude', cwd: '/test', tty: '' },
|
|
132
|
+
]);
|
|
133
|
+
expect(result).toEqual([]);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it('should scan only directories matching process CWDs', () => {
|
|
137
|
+
// /my/project → -my-project (encoded dir)
|
|
138
|
+
const encodedDir = path.join(projectsDir, '-my-project');
|
|
139
|
+
fs.mkdirSync(encodedDir, { recursive: true });
|
|
140
|
+
|
|
141
|
+
// Also create another dir that should NOT be scanned
|
|
142
|
+
const otherDir = path.join(projectsDir, '-other-project');
|
|
143
|
+
fs.mkdirSync(otherDir, { recursive: true });
|
|
144
|
+
|
|
145
|
+
const mockFiles: SessionFile[] = [
|
|
146
|
+
{
|
|
147
|
+
sessionId: 's1',
|
|
148
|
+
filePath: path.join(encodedDir, 's1.jsonl'),
|
|
149
|
+
projectDir: encodedDir,
|
|
150
|
+
birthtimeMs: 1710800324000,
|
|
151
|
+
resolvedCwd: '',
|
|
152
|
+
},
|
|
153
|
+
];
|
|
154
|
+
mockedBatchGetSessionFileBirthtimes.mockReturnValue(mockFiles);
|
|
155
|
+
|
|
156
|
+
const processes = [
|
|
157
|
+
{ pid: 1, command: 'claude', cwd: '/my/project', tty: '' },
|
|
158
|
+
];
|
|
159
|
+
|
|
160
|
+
const result = locator.discoverLiveSessions(processes);
|
|
161
|
+
expect(result).toHaveLength(1);
|
|
162
|
+
expect(result[0].resolvedCwd).toBe('/my/project');
|
|
163
|
+
// batchGetSessionFileBirthtimes called once with all dirs
|
|
164
|
+
expect(mockedBatchGetSessionFileBirthtimes).toHaveBeenCalledTimes(1);
|
|
165
|
+
expect(mockedBatchGetSessionFileBirthtimes).toHaveBeenCalledWith([encodedDir]);
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it('should return empty when encoded dir does not exist', () => {
|
|
169
|
+
fs.mkdirSync(projectsDir, { recursive: true });
|
|
170
|
+
|
|
171
|
+
// Process CWD /test encodes to -test, but that dir doesn't exist
|
|
172
|
+
const result = locator.discoverLiveSessions([
|
|
173
|
+
{ pid: 1, command: 'claude', cwd: '/test', tty: '' },
|
|
174
|
+
]);
|
|
175
|
+
expect(result).toEqual([]);
|
|
176
|
+
expect(mockedBatchGetSessionFileBirthtimes).not.toHaveBeenCalled();
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
it('should deduplicate when multiple processes share same CWD', () => {
|
|
180
|
+
const encodedDir = path.join(projectsDir, '-my-project');
|
|
181
|
+
fs.mkdirSync(encodedDir, { recursive: true });
|
|
182
|
+
|
|
183
|
+
mockedBatchGetSessionFileBirthtimes.mockReturnValue([
|
|
184
|
+
{ sessionId: 's1', filePath: path.join(encodedDir, 's1.jsonl'), projectDir: encodedDir, birthtimeMs: 1710800324000, resolvedCwd: '' },
|
|
185
|
+
]);
|
|
186
|
+
|
|
187
|
+
const processes = [
|
|
188
|
+
{ pid: 1, command: 'claude', cwd: '/my/project', tty: '' },
|
|
189
|
+
{ pid: 2, command: 'claude', cwd: '/my/project', tty: '' },
|
|
190
|
+
];
|
|
191
|
+
|
|
192
|
+
const result = locator.discoverLiveSessions(processes);
|
|
193
|
+
// Should only call batch once with deduplicated dir
|
|
194
|
+
expect(mockedBatchGetSessionFileBirthtimes).toHaveBeenCalledTimes(1);
|
|
195
|
+
expect(mockedBatchGetSessionFileBirthtimes).toHaveBeenCalledWith([encodedDir]);
|
|
196
|
+
expect(result).toHaveLength(1);
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it('should skip processes with empty cwd', () => {
|
|
200
|
+
fs.mkdirSync(projectsDir, { recursive: true });
|
|
201
|
+
|
|
202
|
+
const result = locator.discoverLiveSessions([
|
|
203
|
+
{ pid: 1, command: 'claude', cwd: '', tty: '' },
|
|
204
|
+
]);
|
|
205
|
+
expect(result).toEqual([]);
|
|
206
|
+
});
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
describe('tryPidFileMatching', () => {
|
|
210
|
+
let tmpDir: string;
|
|
211
|
+
let sessionsDir: string;
|
|
212
|
+
let projectsDir: string;
|
|
213
|
+
let tryMatch: (processes: ProcessInfo[]) => {
|
|
214
|
+
direct: Array<{ process: ProcessInfo; sessionFile: SessionFile }>;
|
|
215
|
+
fallback: ProcessInfo[];
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
beforeEach(() => {
|
|
219
|
+
tmpDir = makeTmpDir();
|
|
220
|
+
sessionsDir = path.join(tmpDir, 'sessions');
|
|
221
|
+
projectsDir = path.join(tmpDir, 'projects');
|
|
222
|
+
fs.mkdirSync(sessionsDir, { recursive: true });
|
|
223
|
+
tryMatch = bindPidFileMatching(new ClaudeSessionLocator({ projectsDir, sessionsDir }));
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
const makeProc = (pid: number, cwd = '/project/test', startTime?: Date): ProcessInfo => ({
|
|
227
|
+
pid, command: 'claude', cwd, tty: 'ttys001', startTime,
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
const writePidFile = (pid: number, sessionId: string, cwd: string, startedAt: number) => {
|
|
231
|
+
fs.writeFileSync(
|
|
232
|
+
path.join(sessionsDir, `${pid}.json`),
|
|
233
|
+
JSON.stringify({ pid, sessionId, cwd, startedAt, kind: 'interactive', entrypoint: 'cli' }),
|
|
234
|
+
);
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
const writeJsonl = (cwd: string, sessionId: string) => {
|
|
238
|
+
const encoded = cwd.replace(/[^a-zA-Z0-9]/g, '-');
|
|
239
|
+
const projDir = path.join(projectsDir, encoded);
|
|
240
|
+
fs.mkdirSync(projDir, { recursive: true });
|
|
241
|
+
const filePath = path.join(projDir, `${sessionId}.jsonl`);
|
|
242
|
+
fs.writeFileSync(filePath, JSON.stringify({ type: 'assistant', timestamp: new Date().toISOString() }));
|
|
243
|
+
return filePath;
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
it('should return direct match when PID file and JSONL both exist within time tolerance', () => {
|
|
247
|
+
const startTime = new Date();
|
|
248
|
+
const proc = makeProc(1001, '/project/test', startTime);
|
|
249
|
+
writePidFile(1001, 'session-abc', '/project/test', startTime.getTime());
|
|
250
|
+
writeJsonl('/project/test', 'session-abc');
|
|
251
|
+
|
|
252
|
+
const { direct, fallback } = tryMatch([proc]);
|
|
253
|
+
|
|
254
|
+
expect(direct).toHaveLength(1);
|
|
255
|
+
expect(fallback).toHaveLength(0);
|
|
256
|
+
expect(direct[0].sessionFile.sessionId).toBe('session-abc');
|
|
257
|
+
expect(direct[0].sessionFile.resolvedCwd).toBe('/project/test');
|
|
258
|
+
expect(direct[0].process.pid).toBe(1001);
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
it('should fall back when PID file exists but JSONL is missing', () => {
|
|
262
|
+
const startTime = new Date();
|
|
263
|
+
const proc = makeProc(1002, '/project/test', startTime);
|
|
264
|
+
writePidFile(1002, 'nonexistent-session', '/project/test', startTime.getTime());
|
|
265
|
+
// No JSONL file written
|
|
266
|
+
|
|
267
|
+
const { direct, fallback } = tryMatch([proc]);
|
|
268
|
+
|
|
269
|
+
expect(direct).toHaveLength(0);
|
|
270
|
+
expect(fallback).toHaveLength(1);
|
|
271
|
+
expect(fallback[0].pid).toBe(1002);
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
it('should fall back when startedAt is stale (>60s from proc.startTime)', () => {
|
|
275
|
+
const startTime = new Date();
|
|
276
|
+
const staleTime = startTime.getTime() - 90_000; // 90 seconds earlier
|
|
277
|
+
const proc = makeProc(1003, '/project/test', startTime);
|
|
278
|
+
writePidFile(1003, 'stale-session', '/project/test', staleTime);
|
|
279
|
+
writeJsonl('/project/test', 'stale-session');
|
|
280
|
+
|
|
281
|
+
const { direct, fallback } = tryMatch([proc]);
|
|
282
|
+
|
|
283
|
+
expect(direct).toHaveLength(0);
|
|
284
|
+
expect(fallback).toHaveLength(1);
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
it('should accept PID file when startedAt is within 60s tolerance', () => {
|
|
288
|
+
const startTime = new Date();
|
|
289
|
+
const closeTime = startTime.getTime() - 30_000; // 30 seconds earlier — within tolerance
|
|
290
|
+
const proc = makeProc(1004, '/project/test', startTime);
|
|
291
|
+
writePidFile(1004, 'close-session', '/project/test', closeTime);
|
|
292
|
+
writeJsonl('/project/test', 'close-session');
|
|
293
|
+
|
|
294
|
+
const { direct, fallback } = tryMatch([proc]);
|
|
295
|
+
|
|
296
|
+
expect(direct).toHaveLength(1);
|
|
297
|
+
expect(fallback).toHaveLength(0);
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
it('should fall back when PID file is absent', () => {
|
|
301
|
+
const proc = makeProc(1005, '/project/test', new Date());
|
|
302
|
+
// No PID file written
|
|
303
|
+
|
|
304
|
+
const { direct, fallback } = tryMatch([proc]);
|
|
305
|
+
|
|
306
|
+
expect(direct).toHaveLength(0);
|
|
307
|
+
expect(fallback).toHaveLength(1);
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
it('should fall back when PID file contains malformed JSON', () => {
|
|
311
|
+
const proc = makeProc(1006, '/project/test', new Date());
|
|
312
|
+
fs.writeFileSync(path.join(sessionsDir, '1006.json'), 'not valid json {{{');
|
|
313
|
+
|
|
314
|
+
expect(() => {
|
|
315
|
+
const { direct, fallback } = tryMatch([proc]);
|
|
316
|
+
expect(direct).toHaveLength(0);
|
|
317
|
+
expect(fallback).toHaveLength(1);
|
|
318
|
+
}).not.toThrow();
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
it('should fall back for all processes when sessions dir does not exist', () => {
|
|
322
|
+
const missingSessions = bindPidFileMatching(new ClaudeSessionLocator({
|
|
323
|
+
projectsDir,
|
|
324
|
+
sessionsDir: path.join(tmpDir, 'nonexistent-sessions'),
|
|
325
|
+
}));
|
|
326
|
+
const processes = [makeProc(2001, '/a', new Date()), makeProc(2002, '/b', new Date())];
|
|
327
|
+
|
|
328
|
+
const { direct, fallback } = missingSessions(processes);
|
|
329
|
+
|
|
330
|
+
expect(direct).toHaveLength(0);
|
|
331
|
+
expect(fallback).toHaveLength(2);
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
it('should correctly split mixed processes (some with PID files, some without)', () => {
|
|
335
|
+
const startTime = new Date();
|
|
336
|
+
const proc1 = makeProc(3001, '/project/one', startTime);
|
|
337
|
+
const proc2 = makeProc(3002, '/project/two', startTime);
|
|
338
|
+
const proc3 = makeProc(3003, '/project/three', startTime);
|
|
339
|
+
|
|
340
|
+
writePidFile(3001, 'session-one', '/project/one', startTime.getTime());
|
|
341
|
+
writeJsonl('/project/one', 'session-one');
|
|
342
|
+
writePidFile(3003, 'session-three', '/project/three', startTime.getTime());
|
|
343
|
+
writeJsonl('/project/three', 'session-three');
|
|
344
|
+
// proc2 has no PID file
|
|
345
|
+
|
|
346
|
+
const { direct, fallback } = tryMatch([proc1, proc2, proc3]);
|
|
347
|
+
|
|
348
|
+
expect(direct).toHaveLength(2);
|
|
349
|
+
expect(fallback).toHaveLength(1);
|
|
350
|
+
expect(direct.map((d) => d.process.pid).sort()).toEqual([3001, 3003]);
|
|
351
|
+
expect(fallback[0].pid).toBe(3002);
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
it('should skip stale-file check when proc.startTime is undefined', () => {
|
|
355
|
+
const proc = makeProc(4001, '/project/test', undefined); // no startTime
|
|
356
|
+
writePidFile(4001, 'no-time-session', '/project/test', Date.now() - 999_999);
|
|
357
|
+
writeJsonl('/project/test', 'no-time-session');
|
|
358
|
+
|
|
359
|
+
const { direct, fallback } = tryMatch([proc]);
|
|
360
|
+
|
|
361
|
+
// startTime undefined → stale check skipped → direct match
|
|
362
|
+
expect(direct).toHaveLength(1);
|
|
363
|
+
expect(fallback).toHaveLength(0);
|
|
364
|
+
});
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
describe('getProjectDir', () => {
|
|
368
|
+
let tmpDir: string;
|
|
369
|
+
let projectsDir: string;
|
|
370
|
+
let encode: (cwd: string) => string;
|
|
371
|
+
|
|
372
|
+
beforeEach(() => {
|
|
373
|
+
tmpDir = makeTmpDir();
|
|
374
|
+
projectsDir = path.join(tmpDir, 'projects');
|
|
375
|
+
encode = bindProjectDir(new ClaudeSessionLocator({
|
|
376
|
+
projectsDir,
|
|
377
|
+
sessionsDir: path.join(tmpDir, 'sessions'),
|
|
378
|
+
}));
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
it('should replace path separators with hyphens', () => {
|
|
382
|
+
expect(encode('/Users/foo/bar')).toBe(path.join(projectsDir, '-Users-foo-bar'));
|
|
383
|
+
});
|
|
384
|
+
|
|
385
|
+
it('should encode underscores as hyphens (matches Claude Code CLI)', () => {
|
|
386
|
+
expect(encode('/Users/foo/my_project')).toBe(path.join(projectsDir, '-Users-foo-my-project'));
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
it('should encode dots as hyphens', () => {
|
|
390
|
+
expect(encode('/Users/foo/.worktrees/x')).toBe(path.join(projectsDir, '-Users-foo--worktrees-x'));
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
it('should collide paths that differ only in non-alphanumeric chars', () => {
|
|
394
|
+
// The encoding is intentionally lossy — callers must
|
|
395
|
+
// disambiguate via session JSONL contents, not dir name.
|
|
396
|
+
expect(encode('/a/b_c')).toBe(encode('/a/b-c'));
|
|
397
|
+
expect(encode('/a/b_c')).toBe(encode('/a/b.c'));
|
|
398
|
+
});
|
|
399
|
+
|
|
400
|
+
it('should resolve to a real session dir when cwd contains underscores', () => {
|
|
401
|
+
const cwd = '/Users/foo/my_project';
|
|
402
|
+
const expectedDir = path.join(projectsDir, '-Users-foo-my-project');
|
|
403
|
+
fs.mkdirSync(expectedDir, { recursive: true });
|
|
404
|
+
const sessionFile = path.join(expectedDir, 'session-underscore.jsonl');
|
|
405
|
+
fs.writeFileSync(sessionFile, '');
|
|
406
|
+
|
|
407
|
+
expect(encode(cwd)).toBe(expectedDir);
|
|
408
|
+
expect(fs.existsSync(path.join(encode(cwd), 'session-underscore.jsonl'))).toBe(true);
|
|
409
|
+
});
|
|
410
|
+
});
|
|
411
|
+
});
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
2
|
+
|
|
3
|
+
describe('PiCliProbe', () => {
|
|
4
|
+
it('validates documented JSON mode and session capabilities', async () => {
|
|
5
|
+
const api = await import('../../../../index.js') as Record<string, unknown>;
|
|
6
|
+
expect(api).toHaveProperty('PiCliProbe');
|
|
7
|
+
const exec = vi.fn()
|
|
8
|
+
.mockResolvedValueOnce({ stdout: 'pi 0.52.8', stderr: '' })
|
|
9
|
+
.mockResolvedValueOnce({ stdout: '--mode json\n--session-id <uuid>\n--session <path|id>', stderr: '' });
|
|
10
|
+
const Probe = api.PiCliProbe as new (options: unknown) => any;
|
|
11
|
+
await expect(new Probe({ executable: 'fake-pi', exec }).validate()).resolves.toEqual({
|
|
12
|
+
executable: 'fake-pi', version: 'pi 0.52.8',
|
|
13
|
+
});
|
|
14
|
+
expect(exec.mock.calls).toEqual([['fake-pi', ['--version']], ['fake-pi', ['--help']]]);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('rejects unsupported and unavailable CLIs with sanitized errors', async () => {
|
|
18
|
+
const api = await import('../../../../index.js') as Record<string, unknown>;
|
|
19
|
+
const Probe = api.PiCliProbe as new (options: unknown) => any;
|
|
20
|
+
await expect(new Probe({ exec: vi.fn()
|
|
21
|
+
.mockResolvedValueOnce({ stdout: '', stderr: '' })
|
|
22
|
+
.mockResolvedValueOnce({ stdout: '--print only', stderr: '' }) }).validate())
|
|
23
|
+
.rejects.toMatchObject({ code: 'PI_CLI_UNSUPPORTED' });
|
|
24
|
+
const unavailable = new Probe({ exec: vi.fn().mockRejectedValue(new Error(`bad\0${'x'.repeat(1000)}`)) });
|
|
25
|
+
const error = await unavailable.validate().catch((value: Error & { code: string }) => value);
|
|
26
|
+
expect(error.code).toBe('PI_CLI_UNAVAILABLE');
|
|
27
|
+
expect(error.message).not.toContain('\0');
|
|
28
|
+
expect(error.message.length).toBeLessThan(600);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('reports an empty version as unknown', async () => {
|
|
32
|
+
const api = await import('../../../../index.js') as Record<string, unknown>;
|
|
33
|
+
const Probe = api.PiCliProbe as new (options: unknown) => any;
|
|
34
|
+
const exec = vi.fn().mockResolvedValueOnce({ stdout: ' \n', stderr: '' })
|
|
35
|
+
.mockResolvedValueOnce({ stdout: '--mode json --session-id --session', stderr: '' });
|
|
36
|
+
await expect(new Probe({ exec }).validate()).resolves.toMatchObject({ version: 'unknown' });
|
|
37
|
+
});
|
|
38
|
+
});
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import os from 'node:os';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { afterEach, describe, expect, it } from 'vitest';
|
|
5
|
+
import { DurableAgentRepository } from '../../../../durable/DurableAgentRepository.js';
|
|
6
|
+
|
|
7
|
+
const roots: string[] = [];
|
|
8
|
+
|
|
9
|
+
afterEach(() => {
|
|
10
|
+
for (const root of roots.splice(0)) fs.rmSync(root, { recursive: true, force: true });
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
describe('Pi durable-agent repository sessions', () => {
|
|
14
|
+
it('assigns a non-null session at creation and rejects deferred binding', async () => {
|
|
15
|
+
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'pi-durable-repository-'));
|
|
16
|
+
roots.push(root);
|
|
17
|
+
const cwd = path.join(root, 'project');
|
|
18
|
+
fs.mkdirSync(cwd);
|
|
19
|
+
const repository = new DurableAgentRepository({
|
|
20
|
+
dbPath: path.join(root, 'agents.db'),
|
|
21
|
+
processInspector: { getIdentity: (pid) => ({ pid, startedAt: 'owner-start' }) },
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
const agent = await repository.create({ name: 'reviewer', cwd, provider: 'pi' });
|
|
25
|
+
expect(agent).toMatchObject({ provider: 'pi', sessionHealth: 'uninitialized' });
|
|
26
|
+
expect(agent.providerSessionId).toMatch(/^[0-9a-f-]{36}$/);
|
|
27
|
+
|
|
28
|
+
const run = await repository.acquireRun(agent.id);
|
|
29
|
+
await expect(repository.bindProviderSession(agent.id, run.token, crypto.randomUUID()))
|
|
30
|
+
.rejects.toThrow('Only Codex durable sessions can be bound after creation.');
|
|
31
|
+
});
|
|
32
|
+
});
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
2
|
+
const SESSION = '22222222-2222-4222-8222-222222222222';
|
|
3
|
+
const base = { id: 'id', name: 'reviewer', provider: 'pi', providerSessionId: SESSION, sessionHealth: 'uninitialized' };
|
|
4
|
+
|
|
5
|
+
describe('PiPrintAgentService', () => {
|
|
6
|
+
it('probes before provider-aware create', async () => {
|
|
7
|
+
const api = await import('../../../../index.js') as Record<string, unknown>; expect(api).toHaveProperty('PiPrintAgentService');
|
|
8
|
+
const probe = { validate: vi.fn() }; const repository = { create: vi.fn().mockResolvedValue(base), list: vi.fn() }; const runner = { run: vi.fn() };
|
|
9
|
+
const Service = api.PiPrintAgentService as new (options: unknown) => any;
|
|
10
|
+
await new Service({ repository, probe, runner }).create({ name: 'reviewer', cwd: '/project' });
|
|
11
|
+
expect(probe.validate).toHaveBeenCalledOnce(); expect(repository.create).toHaveBeenCalledWith({ name: 'reviewer', cwd: '/project', provider: 'pi' });
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it('records successful first and resumed sends', async () => {
|
|
15
|
+
const api = await import('../../../../index.js') as Record<string, unknown>;
|
|
16
|
+
const repository = { list: vi.fn(), resolve: vi.fn().mockResolvedValue(base), acquireRun: vi.fn()
|
|
17
|
+
.mockResolvedValueOnce({ agent: base, token: 'one' }).mockResolvedValueOnce({ agent: { ...base, sessionHealth: 'healthy' }, token: 'two' }),
|
|
18
|
+
recordProviderProcess: vi.fn(), completeRun: vi.fn() };
|
|
19
|
+
const runner = { run: vi.fn(async (request) => { await request.onSpawn({ pid: 42, startedAt: 'start' }); return { sessionId: SESSION, result: 'answer', messages: ['answer'], exitCode: 0 }; }) };
|
|
20
|
+
const Service = api.PiPrintAgentService as new (options: unknown) => any; const service = new Service({ repository, probe: { validate: vi.fn() }, runner });
|
|
21
|
+
await service.send('reviewer', 'first'); await service.send('reviewer', 'later');
|
|
22
|
+
expect(repository.completeRun).toHaveBeenCalledWith('id', 'one', expect.objectContaining({ status: 'succeeded', sessionHealth: 'healthy' }));
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('records mismatches and rejects missing and ambiguous records', async () => {
|
|
26
|
+
const api = await import('../../../../index.js') as Record<string, unknown>; const ErrorType = api.PiPrintError as new (message: string, code: string) => Error;
|
|
27
|
+
const completeRun = vi.fn(); const repository = { list: vi.fn(), resolve: vi.fn().mockResolvedValue(base), acquireRun: vi.fn().mockResolvedValue({ agent: base, token: 'one' }), recordProviderProcess: vi.fn(), completeRun };
|
|
28
|
+
const Service = api.PiPrintAgentService as new (options: unknown) => any;
|
|
29
|
+
await expect(new Service({ repository, probe: { validate: vi.fn() }, runner: { run: vi.fn().mockRejectedValue(new ErrorType('bad', 'PI_SESSION_MISMATCH')) } }).send('reviewer', 'x'))
|
|
30
|
+
.rejects.toMatchObject({ code: 'PI_SESSION_MISMATCH' });
|
|
31
|
+
expect(completeRun).toHaveBeenCalledWith('id', 'one', expect.objectContaining({ sessionHealth: 'mismatch' }));
|
|
32
|
+
const earlyRepository = { ...repository, resolve: vi.fn().mockResolvedValueOnce(null).mockResolvedValueOnce([base, base]), acquireRun: vi.fn() };
|
|
33
|
+
const early = new Service({ repository: earlyRepository, probe: { validate: vi.fn() }, runner: { run: vi.fn() } });
|
|
34
|
+
await expect(early.send('missing', 'x')).rejects.toMatchObject({ code: 'DURABLE_AGENT_NOT_FOUND' });
|
|
35
|
+
await expect(early.send('many', 'x')).rejects.toMatchObject({ code: 'PI_UNSUPPORTED' });
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('rejects a non-Pi target without acquiring or mutating it', async () => {
|
|
39
|
+
const api = await import('../../../../index.js') as Record<string, unknown>;
|
|
40
|
+
const Service = api.PiPrintAgentService as new (options: unknown) => any;
|
|
41
|
+
const repository = {
|
|
42
|
+
resolve: vi.fn().mockResolvedValue({ ...base, provider: 'claude' }),
|
|
43
|
+
acquireRun: vi.fn(), recordProviderProcess: vi.fn(), completeRun: vi.fn(),
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
await expect(new Service({ repository, probe: { validate: vi.fn() }, runner: { run: vi.fn() } })
|
|
47
|
+
.send('reviewer', 'x')).rejects.toMatchObject({ code: 'PI_UNSUPPORTED' });
|
|
48
|
+
expect(repository.acquireRun).not.toHaveBeenCalled();
|
|
49
|
+
expect(repository.completeRun).not.toHaveBeenCalled();
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('does not turn a successful completion write failure into a second completion', async () => {
|
|
53
|
+
const api = await import('../../../../index.js') as Record<string, unknown>;
|
|
54
|
+
const Service = api.PiPrintAgentService as new (options: unknown) => any;
|
|
55
|
+
const completionFailure = new Error('completion failed');
|
|
56
|
+
const repository = {
|
|
57
|
+
resolve: vi.fn().mockResolvedValue(base),
|
|
58
|
+
acquireRun: vi.fn().mockResolvedValue({ agent: base, token: 'one' }),
|
|
59
|
+
recordProviderProcess: vi.fn(), completeRun: vi.fn().mockRejectedValue(completionFailure),
|
|
60
|
+
};
|
|
61
|
+
const runner = { run: vi.fn().mockResolvedValue({
|
|
62
|
+
sessionId: SESSION, result: 'answer', messages: ['answer'], exitCode: 0,
|
|
63
|
+
}) };
|
|
64
|
+
|
|
65
|
+
await expect(new Service({ repository, probe: { validate: vi.fn() }, runner })
|
|
66
|
+
.send('reviewer', 'x')).rejects.toBe(completionFailure);
|
|
67
|
+
expect(repository.completeRun).toHaveBeenCalledOnce();
|
|
68
|
+
});
|
|
69
|
+
});
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { buildPiPrintArgs, readPiAssistantText, readPiSessionId } from '../../../../providers/pi/durable/PiPrintProtocol.js';
|
|
3
|
+
|
|
4
|
+
const SESSION = '22222222-2222-4222-8222-222222222222';
|
|
5
|
+
|
|
6
|
+
describe('Pi print pure protocol mapping', () => {
|
|
7
|
+
it('maps first and resumed runs to exact documented arguments', () => {
|
|
8
|
+
expect(buildPiPrintArgs(SESSION, true)).toEqual(['--mode', 'json', '--session-id', SESSION]);
|
|
9
|
+
expect(buildPiPrintArgs(SESSION, false)).toEqual(['--mode', 'json', '--session', SESSION]);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it('accepts one expected UUID and rejects invalid, duplicate, and mismatched identities', () => {
|
|
13
|
+
expect(readPiSessionId({ id: SESSION }, null, SESSION)).toBe(SESSION);
|
|
14
|
+
expect(() => readPiSessionId({}, null, SESSION)).toThrowError(expect.objectContaining({ code: 'PI_PROTOCOL' }));
|
|
15
|
+
expect(() => readPiSessionId({ id: [SESSION] }, null, SESSION))
|
|
16
|
+
.toThrowError(expect.objectContaining({ code: 'PI_PROTOCOL' }));
|
|
17
|
+
expect(() => readPiSessionId({ id: SESSION }, SESSION, SESSION)).toThrowError(expect.objectContaining({ code: 'PI_PROTOCOL' }));
|
|
18
|
+
expect(() => readPiSessionId({ id: '33333333-3333-4333-8333-333333333333' }, null, SESSION))
|
|
19
|
+
.toThrowError(expect.objectContaining({ code: 'PI_SESSION_MISMATCH' }));
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('extracts only non-empty completed assistant text', () => {
|
|
23
|
+
expect(readPiAssistantText({ type: 'future' })).toBeNull();
|
|
24
|
+
expect(readPiAssistantText({ type: 'message_end' })).toBeNull();
|
|
25
|
+
expect(readPiAssistantText({ type: 'message_end', message: [] })).toBeNull();
|
|
26
|
+
expect(readPiAssistantText({ type: 'message_end', message: { role: 'user', content: 'no' } })).toBeNull();
|
|
27
|
+
expect(readPiAssistantText({ type: 'message_end', message: { role: 'assistant', content: ' ' } })).toBeNull();
|
|
28
|
+
expect(readPiAssistantText({ type: 'message_end', message: { role: 'assistant', content: 'answer' } })).toBe('answer');
|
|
29
|
+
expect(readPiAssistantText({ type: 'message_end', message: { role: 'assistant', content: null } })).toBeNull();
|
|
30
|
+
expect(readPiAssistantText({ type: 'message_end', message: { role: 'assistant', content: [null, [], { type: 'thinking' }] } })).toBeNull();
|
|
31
|
+
expect(readPiAssistantText({ type: 'message_end', message: { role: 'assistant', content: [{ type: 'text', text: 'a' }, { type: 'text', text: 1 }, { type: 'text', text: 'b' }] } })).toBe('ab');
|
|
32
|
+
});
|
|
33
|
+
});
|