@astrale-os/cli 0.4.0-alpha.13 → 0.6.0-alpha.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/LICENSE +202 -0
- package/README.md +2 -2
- package/THIRD-PARTY-NOTICES.md +27 -0
- package/dist/astrale.js +24396 -9632
- package/package.json +24 -22
- package/src/command.ts +2 -0
- package/src/commands/__tests__/admin-instance.test.ts +3 -2
- package/src/commands/__tests__/domain-list.test.ts +6 -2
- package/src/commands/__tests__/help-contract.test.ts +27 -14
- package/src/commands/__tests__/install-identity-override.test.ts +2 -2
- package/src/commands/__tests__/ls.test.ts +1 -1
- package/src/commands/__tests__/read-commands.test.ts +201 -0
- package/src/commands/__tests__/view.test.ts +100 -0
- package/src/commands/call.ts +27 -44
- package/src/commands/describe.ts +57 -58
- package/src/commands/domain/install.ts +9 -9
- package/src/commands/domain/list.ts +2 -2
- package/src/commands/domain/publish.ts +3 -3
- package/src/commands/get.ts +48 -23
- package/src/commands/identity/register.ts +27 -33
- package/src/commands/instance/active.ts +2 -2
- package/src/commands/instance/create.ts +1 -1
- package/src/commands/instance/delete.ts +3 -3
- package/src/commands/instance/list.ts +8 -3
- package/src/commands/instance/status.ts +4 -3
- package/src/commands/instance/use.ts +2 -2
- package/src/commands/logs.ts +8 -8
- package/src/commands/ls.ts +77 -55
- package/src/commands/mutate.ts +191 -0
- package/src/commands/query.ts +307 -20
- package/src/commands/session/analyze.ts +50 -0
- package/src/commands/session/list.ts +36 -0
- package/src/commands/token.ts +4 -7
- package/src/commands/view-serve.ts +26 -0
- package/src/commands/view.ts +614 -0
- package/src/connect-core.test.ts +42 -0
- package/src/connect-core.ts +53 -0
- package/src/kernel/__tests__/auth.test.ts +1 -0
- package/src/kernel/__tests__/expand.test.ts +123 -0
- package/src/kernel/client.ts +33 -37
- package/src/kernel/expand.ts +55 -61
- package/src/kernel/graph.ts +96 -0
- package/src/kernel/index.ts +18 -3
- package/src/kernel/options.ts +1 -1
- package/src/kernel/run.ts +0 -13
- package/src/lib/__tests__/instance-target.test.ts +34 -0
- package/src/lib/__tests__/table.test.ts +1 -1
- package/src/lib/__tests__/view-open-intent.test.ts +308 -0
- package/src/lib/__tests__/view-snapshot.test.ts +77 -0
- package/src/lib/admin-domain.ts +8 -4
- package/src/lib/admin-instance.ts +5 -1
- package/src/lib/config.ts +1 -0
- package/src/lib/domain-identity.ts +1 -1
- package/src/lib/instance-target.ts +5 -0
- package/src/lib/instance.ts +11 -0
- package/src/lib/log.ts +12 -2
- package/src/lib/login-flow.ts +41 -4
- package/src/lib/provision-instance.ts +2 -2
- package/src/lib/self.ts +1 -3
- package/src/lib/view/open-intent.ts +97 -0
- package/src/lib/view/resolve.ts +104 -0
- package/src/lib/view/server.ts +294 -0
- package/src/lib/view/session.ts +123 -0
- package/src/lib/view/snapshot.ts +101 -0
- package/src/program.ts +16 -3
- package/src/registry.ts +1 -1
- package/src/setup/render.ts +1 -3
- package/src/setup/steps/instance.ts +2 -2
- package/src/telemetry/__tests__/gate.test.ts +63 -0
- package/src/telemetry/__tests__/recorder.test.ts +110 -0
- package/src/telemetry/__tests__/redact.test.ts +79 -0
- package/src/telemetry/__tests__/session.test.ts +166 -0
- package/src/telemetry/__tests__/trigger.test.ts +49 -0
- package/src/telemetry/adapters/__tests__/claude-code.test.ts +89 -0
- package/src/telemetry/adapters/__tests__/codex.test.ts +138 -0
- package/src/telemetry/adapters/__tests__/index.test.ts +88 -0
- package/src/telemetry/adapters/claude-code.ts +90 -0
- package/src/telemetry/adapters/codex.ts +160 -0
- package/src/telemetry/adapters/index.ts +45 -0
- package/src/telemetry/adapters/types.ts +21 -0
- package/src/telemetry/analyze.ts +227 -0
- package/src/telemetry/gate.ts +79 -0
- package/src/telemetry/recorder.ts +57 -0
- package/src/telemetry/redact.ts +53 -0
- package/src/telemetry/session.ts +88 -0
- package/src/telemetry/settings.ts +26 -0
- package/src/telemetry/store.ts +91 -0
- package/src/telemetry/trigger.ts +119 -0
- package/src/telemetry/types.ts +64 -0
- package/studio/client/dist/assets/index-CyN5G8IA.js +109 -0
- package/studio/client/dist/assets/index-DKKMHBBC.css +1 -0
- package/studio/client/dist/index.html +2 -2
- package/studio/server/agent/ask.ts +7 -2
- package/studio/server/agent/claude.ts +15 -3
- package/studio/server/agent/runner.ts +4 -1
- package/studio/server/agent/session-id.ts +13 -0
- package/studio/server/api.ts +50 -32
- package/studio/server/cache.ts +17 -6
- package/studio/server/client-package.test.ts +147 -0
- package/studio/server/client-package.ts +242 -0
- package/studio/server/index.ts +13 -0
- package/studio/server/introspect/anatomy-extras.test.ts +91 -0
- package/studio/server/introspect/anatomy-extras.ts +48 -49
- package/studio/server/introspect/anatomy.ts +7 -7
- package/studio/server/introspect/overlay-tsmorph.test.ts +104 -0
- package/studio/server/introspect/overlay-tsmorph.ts +230 -72
- package/studio/server/state/harness-gateway.ts +12 -3
- package/studio/server/state/harness-token.ts +0 -0
- package/studio/server/state/views.test.ts +90 -0
- package/studio/server/state/views.ts +396 -99
- package/studio/server/state/visibility.ts +10 -6
- package/studio/server/view-dev-server.test.ts +111 -0
- package/studio/server/view-dev-server.ts +372 -0
- package/studio/shared/types.ts +57 -10
- package/viewer/dist/index.html +93 -0
- package/viewer/dist/main.js +71 -0
- package/src/kernel/__tests__/remote-routing.test.ts +0 -70
- package/src/kernel/remote-routing.ts +0 -88
- package/studio/client/dist/assets/index-DOwzZAEK.css +0 -1
- package/studio/client/dist/assets/index-wtU0Zxhy.js +0 -183
- package/studio/tsconfig.json +0 -23
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, test } from 'bun:test'
|
|
2
|
+
import { mkdirSync, mkdtempSync, rmSync, utimesSync, writeFileSync } from 'node:fs'
|
|
3
|
+
import { tmpdir } from 'node:os'
|
|
4
|
+
import { join } from 'node:path'
|
|
5
|
+
|
|
6
|
+
import { claudeCodeAdapter } from '../claude-code'
|
|
7
|
+
|
|
8
|
+
const ROOT = '/tmp/fake/proj'
|
|
9
|
+
const MUNGED = '-tmp-fake-proj'
|
|
10
|
+
|
|
11
|
+
let base: string
|
|
12
|
+
|
|
13
|
+
function project(dir: string, file: string, mtime: Date): string {
|
|
14
|
+
const projPath = join(base, 'projects', dir)
|
|
15
|
+
mkdirSync(projPath, { recursive: true })
|
|
16
|
+
const p = join(projPath, file)
|
|
17
|
+
writeFileSync(p, '{"type":"user"}\n')
|
|
18
|
+
utimesSync(p, mtime, mtime)
|
|
19
|
+
return p
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
beforeEach(() => {
|
|
23
|
+
base = mkdtempSync(join(tmpdir(), 'cc-adapter-'))
|
|
24
|
+
})
|
|
25
|
+
afterEach(() => {
|
|
26
|
+
rmSync(base, { recursive: true, force: true })
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
describe('claudeCodeAdapter', () => {
|
|
30
|
+
test('detect is false without a projects dir, true with one', () => {
|
|
31
|
+
expect(claudeCodeAdapter(base).detect()).toBe(false)
|
|
32
|
+
mkdirSync(join(base, 'projects'), { recursive: true })
|
|
33
|
+
expect(claudeCodeAdapter(base).detect()).toBe(true)
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
test('matches the exact munged dir and subdir-prefixed dirs, excludes unrelated', async () => {
|
|
37
|
+
const now = new Date()
|
|
38
|
+
const wide = {
|
|
39
|
+
start: new Date(now.getTime() - 2 * 3600_000),
|
|
40
|
+
end: new Date(now.getTime() + 2 * 3600_000),
|
|
41
|
+
}
|
|
42
|
+
project(MUNGED, 'exact.jsonl', now)
|
|
43
|
+
project(`${MUNGED}-sub-deeper`, 'child.jsonl', now)
|
|
44
|
+
project('-other-unrelated-path', 'nope.jsonl', now)
|
|
45
|
+
|
|
46
|
+
const sessions = await claudeCodeAdapter(base).discover(ROOT, wide)
|
|
47
|
+
const ids = sessions.map((s) => s.sessionId).sort()
|
|
48
|
+
expect(ids).toEqual(['child', 'exact'])
|
|
49
|
+
for (const s of sessions) {
|
|
50
|
+
expect(s.harness).toBe('claude-code')
|
|
51
|
+
expect(s.sizeBytes).toBeGreaterThan(0)
|
|
52
|
+
expect(typeof s.endedAt).toBe('string')
|
|
53
|
+
expect(typeof s.startedAt).toBe('string')
|
|
54
|
+
}
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
test('window filtering: excludes a transcript whose span sits entirely after the window', async () => {
|
|
58
|
+
const t = Date.now()
|
|
59
|
+
const window = { start: new Date(t - 2 * 3600_000), end: new Date(t - 3600_000) }
|
|
60
|
+
project(MUNGED, 'inside.jsonl', new Date(t - 90 * 60_000))
|
|
61
|
+
project(MUNGED, 'after.jsonl', new Date(t - 30 * 60_000))
|
|
62
|
+
|
|
63
|
+
const ids = (await claudeCodeAdapter(base).discover(ROOT, window)).map((s) => s.sessionId)
|
|
64
|
+
expect(ids).toContain('inside')
|
|
65
|
+
expect(ids).not.toContain('after')
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
test('only *.jsonl files are considered', async () => {
|
|
69
|
+
const now = new Date()
|
|
70
|
+
const wide = {
|
|
71
|
+
start: new Date(now.getTime() - 3600_000),
|
|
72
|
+
end: new Date(now.getTime() + 3600_000),
|
|
73
|
+
}
|
|
74
|
+
project(MUNGED, 'keep.jsonl', now)
|
|
75
|
+
project(MUNGED, 'ignore.txt', now)
|
|
76
|
+
|
|
77
|
+
const ids = (await claudeCodeAdapter(base).discover(ROOT, wide)).map((s) => s.sessionId)
|
|
78
|
+
expect(ids).toEqual(['keep'])
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
test('never throws and returns [] when the projects dir is missing', async () => {
|
|
82
|
+
const now = new Date()
|
|
83
|
+
const sessions = await claudeCodeAdapter(join(base, 'does-not-exist')).discover(ROOT, {
|
|
84
|
+
start: new Date(now.getTime() - 3600_000),
|
|
85
|
+
end: now,
|
|
86
|
+
})
|
|
87
|
+
expect(sessions).toEqual([])
|
|
88
|
+
})
|
|
89
|
+
})
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, test } from 'bun:test'
|
|
2
|
+
import { mkdirSync, mkdtempSync, rmSync, utimesSync, writeFileSync } from 'node:fs'
|
|
3
|
+
import { tmpdir } from 'node:os'
|
|
4
|
+
import { join } from 'node:path'
|
|
5
|
+
|
|
6
|
+
import { codexAdapter } from '../codex'
|
|
7
|
+
|
|
8
|
+
const ROOT = '/work/proj'
|
|
9
|
+
const WINDOW = {
|
|
10
|
+
start: new Date('2026-07-10T00:00:00.000Z'),
|
|
11
|
+
end: new Date('2026-07-10T23:59:59.000Z'),
|
|
12
|
+
}
|
|
13
|
+
const IN_DAY = ['2026', '07', '10']
|
|
14
|
+
|
|
15
|
+
let base: string
|
|
16
|
+
|
|
17
|
+
function meta(cwd: string, timestamp = '2026-07-10T10:00:00.000Z'): string {
|
|
18
|
+
return JSON.stringify({
|
|
19
|
+
type: 'session_meta',
|
|
20
|
+
payload: { session_id: `sid-${cwd}`, timestamp, cwd, cli_version: '1.2.3' },
|
|
21
|
+
})
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function rollout(
|
|
25
|
+
day: string[],
|
|
26
|
+
name: string,
|
|
27
|
+
firstLine: string,
|
|
28
|
+
mtime = new Date('2026-07-10T10:05:00.000Z'),
|
|
29
|
+
): string {
|
|
30
|
+
const dir = join(base, 'sessions', ...day)
|
|
31
|
+
mkdirSync(dir, { recursive: true })
|
|
32
|
+
const p = join(dir, `rollout-${name}.jsonl`)
|
|
33
|
+
writeFileSync(p, `${firstLine}\n{"type":"response_item"}\n{"type":"event_msg"}\n`)
|
|
34
|
+
utimesSync(p, mtime, mtime)
|
|
35
|
+
return p
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
beforeEach(() => {
|
|
39
|
+
base = mkdtempSync(join(tmpdir(), 'codex-adapter-'))
|
|
40
|
+
})
|
|
41
|
+
afterEach(() => {
|
|
42
|
+
rmSync(base, { recursive: true, force: true })
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
describe('codexAdapter', () => {
|
|
46
|
+
test('detect follows the sessions dir', () => {
|
|
47
|
+
expect(codexAdapter(base).detect()).toBe(false)
|
|
48
|
+
mkdirSync(join(base, 'sessions'), { recursive: true })
|
|
49
|
+
expect(codexAdapter(base).detect()).toBe(true)
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
test('cwd filtering: exact root and subdir included, unrelated excluded', async () => {
|
|
53
|
+
rollout(IN_DAY, 'exact', meta(ROOT))
|
|
54
|
+
rollout(IN_DAY, 'sub', meta(`${ROOT}/packages/app`))
|
|
55
|
+
rollout(IN_DAY, 'sibling', meta('/work/project-x'))
|
|
56
|
+
rollout(IN_DAY, 'elsewhere', meta('/somewhere/else'))
|
|
57
|
+
|
|
58
|
+
const sessions = await codexAdapter(base).discover(ROOT, WINDOW)
|
|
59
|
+
const cwds = sessions.map((s) => s.cwd).sort()
|
|
60
|
+
expect(cwds).toEqual([ROOT, `${ROOT}/packages/app`])
|
|
61
|
+
for (const s of sessions) {
|
|
62
|
+
expect(s.harness).toBe('codex')
|
|
63
|
+
expect(s.sessionId).toBe(`sid-${s.cwd}`)
|
|
64
|
+
expect(s.startedAt).toBe('2026-07-10T10:00:00.000Z')
|
|
65
|
+
expect(typeof s.endedAt).toBe('string')
|
|
66
|
+
expect(s.sizeBytes).toBeGreaterThan(0)
|
|
67
|
+
}
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
test('window filtering: rollout with mtime before the window is excluded', async () => {
|
|
71
|
+
rollout(IN_DAY, 'in', meta(ROOT))
|
|
72
|
+
rollout(
|
|
73
|
+
IN_DAY,
|
|
74
|
+
'stale',
|
|
75
|
+
meta(ROOT, '2026-07-08T10:00:00.000Z'),
|
|
76
|
+
new Date('2026-07-08T10:00:00.000Z'),
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
const ids = (await codexAdapter(base).discover(ROOT, WINDOW)).map((s) => s.sessionId)
|
|
80
|
+
// both share the same synthetic session_id; assert exactly one survived
|
|
81
|
+
expect(ids.length).toBe(1)
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
test('date-shard pruning skips days outside the window', async () => {
|
|
85
|
+
rollout(['2026', '01', '01'], 'jan', meta(ROOT), new Date('2026-01-01T10:00:00.000Z'))
|
|
86
|
+
const ids = await codexAdapter(base).discover(ROOT, WINDOW)
|
|
87
|
+
expect(ids).toEqual([])
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
test('a corrupt first line is skipped without throwing', async () => {
|
|
91
|
+
rollout(IN_DAY, 'good', meta(ROOT))
|
|
92
|
+
rollout(IN_DAY, 'corrupt', '{ this is not json ]]]')
|
|
93
|
+
|
|
94
|
+
const sessions = await codexAdapter(base).discover(ROOT, WINDOW)
|
|
95
|
+
expect(sessions.length).toBe(1)
|
|
96
|
+
expect(sessions[0]?.cwd).toBe(ROOT)
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
test('never throws and returns [] when the sessions dir is missing', async () => {
|
|
100
|
+
expect(await codexAdapter(join(base, 'absent')).discover(ROOT, WINDOW)).toEqual([])
|
|
101
|
+
})
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
describe('codexAdapter large session_meta', () => {
|
|
105
|
+
test('first line larger than one read chunk still parses (real rollouts embed base_instructions)', async () => {
|
|
106
|
+
// Padding before cwd pushes it past the first 64 KB chunk boundary.
|
|
107
|
+
const huge = JSON.stringify({
|
|
108
|
+
type: 'session_meta',
|
|
109
|
+
payload: {
|
|
110
|
+
base_instructions: { text: 'x'.repeat(80 * 1024) },
|
|
111
|
+
session_id: 'sid-huge',
|
|
112
|
+
timestamp: '2026-07-10T10:00:00.000Z',
|
|
113
|
+
cwd: ROOT,
|
|
114
|
+
cli_version: '1.2.3',
|
|
115
|
+
},
|
|
116
|
+
})
|
|
117
|
+
rollout(IN_DAY, 'huge', huge)
|
|
118
|
+
const sessions = await codexAdapter(base).discover(ROOT, WINDOW)
|
|
119
|
+
expect(sessions.map((s) => s.sessionId)).toEqual(['sid-huge'])
|
|
120
|
+
})
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
describe('codexAdapter timezone shard boundary', () => {
|
|
124
|
+
test('rollout in the next local-date shard still matches a window ending today (UTC+ offsets)', async () => {
|
|
125
|
+
const w = {
|
|
126
|
+
start: new Date('2026-07-10T20:00:00.000Z'),
|
|
127
|
+
end: new Date('2026-07-10T23:00:00.000Z'),
|
|
128
|
+
}
|
|
129
|
+
rollout(
|
|
130
|
+
['2026', '07', '11'],
|
|
131
|
+
'tzedge',
|
|
132
|
+
meta(ROOT, '2026-07-10T22:25:00.000Z'),
|
|
133
|
+
new Date('2026-07-10T22:30:00.000Z'),
|
|
134
|
+
)
|
|
135
|
+
const sessions = await codexAdapter(base).discover(ROOT, w)
|
|
136
|
+
expect(sessions.map((s) => s.transcriptPath.includes('tzedge'))).toEqual([true])
|
|
137
|
+
})
|
|
138
|
+
})
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, test } from 'bun:test'
|
|
2
|
+
import { mkdirSync, mkdtempSync, rmSync, utimesSync, writeFileSync } from 'node:fs'
|
|
3
|
+
import { tmpdir } from 'node:os'
|
|
4
|
+
import { join } from 'node:path'
|
|
5
|
+
|
|
6
|
+
import { claudeCodeAdapter } from '../claude-code'
|
|
7
|
+
import { codexAdapter } from '../codex'
|
|
8
|
+
import { defaultAdapters, discoverAll } from '../index'
|
|
9
|
+
|
|
10
|
+
const ROOT = '/work/proj'
|
|
11
|
+
const WINDOW = {
|
|
12
|
+
start: new Date('2026-07-01T00:00:00.000Z'),
|
|
13
|
+
end: new Date(Date.now() + 86_400_000),
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
let ccBase: string
|
|
17
|
+
let cxBase: string
|
|
18
|
+
|
|
19
|
+
function seedClaude(mtime: Date): void {
|
|
20
|
+
const dir = join(ccBase, 'projects', '-work-proj')
|
|
21
|
+
mkdirSync(dir, { recursive: true })
|
|
22
|
+
const p = join(dir, 'cc-session.jsonl')
|
|
23
|
+
writeFileSync(p, '{"type":"user"}\n')
|
|
24
|
+
utimesSync(p, mtime, mtime)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function seedCodex(mtime: Date): void {
|
|
28
|
+
const dir = join(cxBase, 'sessions', '2026', '07', '10')
|
|
29
|
+
mkdirSync(dir, { recursive: true })
|
|
30
|
+
const p = join(dir, 'rollout-x.jsonl')
|
|
31
|
+
const line = JSON.stringify({
|
|
32
|
+
type: 'session_meta',
|
|
33
|
+
payload: { session_id: 'cx-session', timestamp: '2026-07-10T10:00:00.000Z', cwd: ROOT },
|
|
34
|
+
})
|
|
35
|
+
writeFileSync(p, `${line}\n`)
|
|
36
|
+
utimesSync(p, mtime, mtime)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
beforeEach(() => {
|
|
40
|
+
ccBase = mkdtempSync(join(tmpdir(), 'cc-base-'))
|
|
41
|
+
cxBase = mkdtempSync(join(tmpdir(), 'cx-base-'))
|
|
42
|
+
})
|
|
43
|
+
afterEach(() => {
|
|
44
|
+
rmSync(ccBase, { recursive: true, force: true })
|
|
45
|
+
rmSync(cxBase, { recursive: true, force: true })
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
describe('discoverAll', () => {
|
|
49
|
+
test('defaultAdapters exposes both harnesses', () => {
|
|
50
|
+
expect(
|
|
51
|
+
defaultAdapters()
|
|
52
|
+
.map((a) => a.name)
|
|
53
|
+
.sort(),
|
|
54
|
+
).toEqual(['claude-code', 'codex'])
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
test('merges detected adapters and sorts by endedAt descending', async () => {
|
|
58
|
+
seedClaude(new Date('2026-07-10T09:00:00.000Z'))
|
|
59
|
+
seedCodex(new Date('2026-07-10T11:00:00.000Z'))
|
|
60
|
+
|
|
61
|
+
const sessions = await discoverAll(
|
|
62
|
+
[claudeCodeAdapter(ccBase), codexAdapter(cxBase)],
|
|
63
|
+
ROOT,
|
|
64
|
+
WINDOW,
|
|
65
|
+
)
|
|
66
|
+
expect(sessions.map((s) => s.harness)).toEqual(['codex', 'claude-code'])
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
test('never throws when a base dir is missing; returns only the detected harness', async () => {
|
|
70
|
+
seedClaude(new Date('2026-07-10T09:00:00.000Z'))
|
|
71
|
+
|
|
72
|
+
const sessions = await discoverAll(
|
|
73
|
+
[claudeCodeAdapter(ccBase), codexAdapter(join(cxBase, 'absent'))],
|
|
74
|
+
ROOT,
|
|
75
|
+
WINDOW,
|
|
76
|
+
)
|
|
77
|
+
expect(sessions.map((s) => s.harness)).toEqual(['claude-code'])
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
test('returns [] when nothing is detected', async () => {
|
|
81
|
+
const sessions = await discoverAll(
|
|
82
|
+
[claudeCodeAdapter(join(ccBase, 'absent')), codexAdapter(join(cxBase, 'absent'))],
|
|
83
|
+
ROOT,
|
|
84
|
+
WINDOW,
|
|
85
|
+
)
|
|
86
|
+
expect(sessions).toEqual([])
|
|
87
|
+
})
|
|
88
|
+
})
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Claude Code adapter: transcripts live at
|
|
3
|
+
* `<base>/projects/<munged-cwd>/<session-uuid>.jsonl`, where the project dir is
|
|
4
|
+
* the absolute cwd with every non-alphanumeric char replaced by '-'. Discovery
|
|
5
|
+
* is stat/readdir only — it never opens a transcript.
|
|
6
|
+
*/
|
|
7
|
+
import { existsSync, readdirSync, statSync } from 'node:fs'
|
|
8
|
+
import { homedir } from 'node:os'
|
|
9
|
+
import { join } from 'node:path'
|
|
10
|
+
|
|
11
|
+
import type { HarnessSession } from '../types'
|
|
12
|
+
import type { HarnessAdapter, TimeWindow } from './types'
|
|
13
|
+
|
|
14
|
+
/** cwd → project dir name: every non-alphanumeric char becomes '-'. */
|
|
15
|
+
function mungeCwd(cwd: string): string {
|
|
16
|
+
return cwd.replace(/[^a-zA-Z0-9]/g, '-')
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const READING_GUIDE =
|
|
20
|
+
'This is a Claude Code transcript: one JSON object per line (JSONL). Each entry has a `type` ' +
|
|
21
|
+
'(user, assistant, system, file-history-snapshot, …). Assistant entries carry the model’s ' +
|
|
22
|
+
'verbatim `thinking` blocks plus `tool_use` blocks with full tool inputs; tool results come back as ' +
|
|
23
|
+
'user-type entries. Files are large and append-only, so grep for tool names, error strings, or file ' +
|
|
24
|
+
'paths and sample the surrounding lines rather than reading the whole transcript. The last line ' +
|
|
25
|
+
'reflects where the session ended.'
|
|
26
|
+
|
|
27
|
+
export function claudeCodeAdapter(base: string = join(homedir(), '.claude')): HarnessAdapter {
|
|
28
|
+
const projectsDir = join(base, 'projects')
|
|
29
|
+
|
|
30
|
+
function detect(): boolean {
|
|
31
|
+
try {
|
|
32
|
+
return existsSync(projectsDir)
|
|
33
|
+
} catch {
|
|
34
|
+
return false
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function discover(root: string, window: TimeWindow): Promise<HarnessSession[]> {
|
|
39
|
+
const sessions: HarnessSession[] = []
|
|
40
|
+
try {
|
|
41
|
+
const munged = mungeCwd(root)
|
|
42
|
+
const prefix = `${munged}-`
|
|
43
|
+
let dirs: string[]
|
|
44
|
+
try {
|
|
45
|
+
dirs = readdirSync(projectsDir)
|
|
46
|
+
} catch {
|
|
47
|
+
return Promise.resolve([])
|
|
48
|
+
}
|
|
49
|
+
const startMs = window.start.getTime()
|
|
50
|
+
const endMs = window.end.getTime()
|
|
51
|
+
for (const dir of dirs) {
|
|
52
|
+
if (dir !== munged && !dir.startsWith(prefix)) continue
|
|
53
|
+
const projectPath = join(projectsDir, dir)
|
|
54
|
+
let files: string[]
|
|
55
|
+
try {
|
|
56
|
+
files = readdirSync(projectPath)
|
|
57
|
+
} catch {
|
|
58
|
+
continue
|
|
59
|
+
}
|
|
60
|
+
for (const file of files) {
|
|
61
|
+
if (!file.endsWith('.jsonl')) continue
|
|
62
|
+
const transcriptPath = join(projectPath, file)
|
|
63
|
+
try {
|
|
64
|
+
const st = statSync(transcriptPath)
|
|
65
|
+
const mtimeMs = st.mtime.getTime()
|
|
66
|
+
const birthMs = st.birthtime.getTime()
|
|
67
|
+
const spanStart = Math.min(birthMs, mtimeMs)
|
|
68
|
+
const spanEnd = Math.max(birthMs, mtimeMs)
|
|
69
|
+
if (spanStart > endMs || spanEnd < startMs) continue
|
|
70
|
+
sessions.push({
|
|
71
|
+
harness: 'claude-code',
|
|
72
|
+
sessionId: file.slice(0, -'.jsonl'.length),
|
|
73
|
+
transcriptPath,
|
|
74
|
+
sizeBytes: st.size,
|
|
75
|
+
startedAt: st.birthtime.toISOString(),
|
|
76
|
+
endedAt: st.mtime.toISOString(),
|
|
77
|
+
})
|
|
78
|
+
} catch {
|
|
79
|
+
/* unreadable transcript — skip it, never fail discovery */
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
} catch {
|
|
84
|
+
return Promise.resolve([])
|
|
85
|
+
}
|
|
86
|
+
return Promise.resolve(sessions)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return { name: 'claude-code', detect, discover, readingGuide: READING_GUIDE }
|
|
90
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Codex adapter: rollouts live at
|
|
3
|
+
* `<base>/sessions/YYYY/MM/DD/rollout-<ts>-<uuid>.jsonl`, date-sharded. Line 1 is
|
|
4
|
+
* a `session_meta` object carrying cwd + session_id + timestamp. Discovery reads
|
|
5
|
+
* only that first line — never the transcript body — and prunes by date shard.
|
|
6
|
+
*/
|
|
7
|
+
import { existsSync, openSync, readdirSync, readSync, statSync } from 'node:fs'
|
|
8
|
+
import { closeSync } from 'node:fs'
|
|
9
|
+
import { homedir } from 'node:os'
|
|
10
|
+
import { join } from 'node:path'
|
|
11
|
+
|
|
12
|
+
import type { HarnessSession } from '../types'
|
|
13
|
+
import type { HarnessAdapter, TimeWindow } from './types'
|
|
14
|
+
|
|
15
|
+
const DAY_MS = 24 * 60 * 60 * 1000
|
|
16
|
+
const HEAD_CHUNK = 64 * 1024
|
|
17
|
+
// session_meta embeds the harness's full base_instructions — real first lines
|
|
18
|
+
// run tens of KB, so read in chunks until the newline (cap = malformed guard).
|
|
19
|
+
const MAX_HEAD_BYTES = 512 * 1024
|
|
20
|
+
|
|
21
|
+
const READING_GUIDE =
|
|
22
|
+
'This is a Codex rollout: one JSON object per line (JSONL). Line 1 is `session_meta` (session_id, ' +
|
|
23
|
+
'timestamp, cwd, cli_version). Later lines are either `response_item` — payload.type of message, ' +
|
|
24
|
+
'function_call, function_call_output, or reasoning (reasoning is a short summary, not verbatim chain ' +
|
|
25
|
+
'of thought) — or `event_msg` carrying task_started, task_complete, token_count, agent_message, and ' +
|
|
26
|
+
'user_message. Files can be large, so grep for function_call names, outputs, or error strings and ' +
|
|
27
|
+
'sample around them rather than reading the whole rollout.'
|
|
28
|
+
|
|
29
|
+
/** Read the first line of a file without loading the body. */
|
|
30
|
+
function readFirstLine(path: string): string | null {
|
|
31
|
+
let fd: number | null = null
|
|
32
|
+
try {
|
|
33
|
+
fd = openSync(path, 'r')
|
|
34
|
+
const chunks: Buffer[] = []
|
|
35
|
+
let pos = 0
|
|
36
|
+
while (pos < MAX_HEAD_BYTES) {
|
|
37
|
+
const buf = Buffer.alloc(HEAD_CHUNK)
|
|
38
|
+
const bytes = readSync(fd, buf, 0, HEAD_CHUNK, pos)
|
|
39
|
+
if (bytes === 0) break
|
|
40
|
+
chunks.push(buf.subarray(0, bytes))
|
|
41
|
+
pos += bytes
|
|
42
|
+
// Newline is ASCII, so a byte search is safe; decode only after concat
|
|
43
|
+
// so a multibyte char split across chunks can't corrupt the text.
|
|
44
|
+
if (buf.subarray(0, bytes).includes(0x0a)) break
|
|
45
|
+
}
|
|
46
|
+
const text = Buffer.concat(chunks).toString('utf-8')
|
|
47
|
+
const nl = text.indexOf('\n')
|
|
48
|
+
if (nl !== -1) return text.slice(0, nl)
|
|
49
|
+
return pos >= MAX_HEAD_BYTES ? null : text || null
|
|
50
|
+
} catch {
|
|
51
|
+
return null
|
|
52
|
+
} finally {
|
|
53
|
+
if (fd !== null) {
|
|
54
|
+
try {
|
|
55
|
+
closeSync(fd)
|
|
56
|
+
} catch {
|
|
57
|
+
/* ignore */
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Numeric names only (YYYY / MM / DD shards), sorted for determinism. */
|
|
64
|
+
function numericDirs(path: string): string[] {
|
|
65
|
+
try {
|
|
66
|
+
return readdirSync(path)
|
|
67
|
+
.filter((n) => /^\d+$/.test(n))
|
|
68
|
+
.sort()
|
|
69
|
+
} catch {
|
|
70
|
+
return []
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function codexAdapter(base: string = join(homedir(), '.codex')): HarnessAdapter {
|
|
75
|
+
const sessionsDir = join(base, 'sessions')
|
|
76
|
+
|
|
77
|
+
function detect(): boolean {
|
|
78
|
+
try {
|
|
79
|
+
return existsSync(sessionsDir)
|
|
80
|
+
} catch {
|
|
81
|
+
return false
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function discover(root: string, window: TimeWindow): Promise<HarnessSession[]> {
|
|
86
|
+
const sessions: HarnessSession[] = []
|
|
87
|
+
try {
|
|
88
|
+
const startMs = window.start.getTime()
|
|
89
|
+
const endMs = window.end.getTime()
|
|
90
|
+
const lowerMs = startMs - DAY_MS
|
|
91
|
+
for (const yyyy of numericDirs(sessionsDir)) {
|
|
92
|
+
for (const mm of numericDirs(join(sessionsDir, yyyy))) {
|
|
93
|
+
for (const dd of numericDirs(join(sessionsDir, yyyy, mm))) {
|
|
94
|
+
// Shard names are LOCAL dates but dayStart is computed as UTC —
|
|
95
|
+
// pad both bounds a day so no timezone offset can skip a shard.
|
|
96
|
+
const dayStart = Date.UTC(Number(yyyy), Number(mm) - 1, Number(dd))
|
|
97
|
+
if (dayStart > endMs + DAY_MS || dayStart + DAY_MS <= lowerMs) continue
|
|
98
|
+
scanDay(join(sessionsDir, yyyy, mm, dd), root, startMs, endMs, sessions)
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
} catch {
|
|
103
|
+
return Promise.resolve([])
|
|
104
|
+
}
|
|
105
|
+
return Promise.resolve(sessions)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return { name: 'codex', detect, discover, readingGuide: READING_GUIDE }
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function scanDay(
|
|
112
|
+
dayPath: string,
|
|
113
|
+
root: string,
|
|
114
|
+
startMs: number,
|
|
115
|
+
endMs: number,
|
|
116
|
+
out: HarnessSession[],
|
|
117
|
+
): void {
|
|
118
|
+
let files: string[]
|
|
119
|
+
try {
|
|
120
|
+
files = readdirSync(dayPath)
|
|
121
|
+
} catch {
|
|
122
|
+
return
|
|
123
|
+
}
|
|
124
|
+
const rootPrefix = `${root}/`
|
|
125
|
+
for (const file of files) {
|
|
126
|
+
if (!file.startsWith('rollout-') || !file.endsWith('.jsonl')) continue
|
|
127
|
+
const transcriptPath = join(dayPath, file)
|
|
128
|
+
try {
|
|
129
|
+
const st = statSync(transcriptPath)
|
|
130
|
+
const mtimeMs = st.mtime.getTime()
|
|
131
|
+
if (mtimeMs < startMs) continue
|
|
132
|
+
const line = readFirstLine(transcriptPath)
|
|
133
|
+
if (line === null) continue
|
|
134
|
+
let meta: unknown
|
|
135
|
+
try {
|
|
136
|
+
meta = JSON.parse(line)
|
|
137
|
+
} catch {
|
|
138
|
+
continue
|
|
139
|
+
}
|
|
140
|
+
const payload = (meta as { payload?: Record<string, unknown> })?.payload
|
|
141
|
+
if (!payload || typeof payload.cwd !== 'string') continue
|
|
142
|
+
const cwd = payload.cwd
|
|
143
|
+
if (cwd !== root && !cwd.startsWith(rootPrefix)) continue
|
|
144
|
+
const startedAt = typeof payload.timestamp === 'string' ? payload.timestamp : undefined
|
|
145
|
+
const startedMs = startedAt ? Date.parse(startedAt) : Number.NaN
|
|
146
|
+
if (!Number.isNaN(startedMs) && startedMs > endMs) continue
|
|
147
|
+
out.push({
|
|
148
|
+
harness: 'codex',
|
|
149
|
+
sessionId: typeof payload.session_id === 'string' ? payload.session_id : undefined,
|
|
150
|
+
transcriptPath,
|
|
151
|
+
cwd,
|
|
152
|
+
startedAt,
|
|
153
|
+
endedAt: st.mtime.toISOString(),
|
|
154
|
+
sizeBytes: st.size,
|
|
155
|
+
})
|
|
156
|
+
} catch {
|
|
157
|
+
/* unreadable rollout — skip it, never fail discovery */
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Harness adapter registry: the set of transcript sources probed for a session,
|
|
3
|
+
* plus a fan-out that runs every detected adapter's discovery and merges the
|
|
4
|
+
* results newest-first. Nothing here throws — a broken adapter degrades to [].
|
|
5
|
+
*/
|
|
6
|
+
import type { HarnessSession } from '../types'
|
|
7
|
+
import type { HarnessAdapter, TimeWindow } from './types'
|
|
8
|
+
|
|
9
|
+
import { claudeCodeAdapter } from './claude-code'
|
|
10
|
+
import { codexAdapter } from './codex'
|
|
11
|
+
|
|
12
|
+
export type { HarnessAdapter, TimeWindow } from './types'
|
|
13
|
+
export { claudeCodeAdapter } from './claude-code'
|
|
14
|
+
export { codexAdapter } from './codex'
|
|
15
|
+
|
|
16
|
+
/** All built-in adapters at their default home-directory bases. */
|
|
17
|
+
export function defaultAdapters(): HarnessAdapter[] {
|
|
18
|
+
return [claudeCodeAdapter(), codexAdapter()]
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function endedMs(s: HarnessSession): number {
|
|
22
|
+
const t = s.endedAt ? Date.parse(s.endedAt) : Number.NaN
|
|
23
|
+
return Number.isNaN(t) ? 0 : t
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Detected adapters' discover() in parallel, flattened, newest endedAt first. */
|
|
27
|
+
export async function discoverAll(
|
|
28
|
+
adapters: HarnessAdapter[],
|
|
29
|
+
root: string,
|
|
30
|
+
window: TimeWindow,
|
|
31
|
+
): Promise<HarnessSession[]> {
|
|
32
|
+
const active = adapters.filter((a) => {
|
|
33
|
+
try {
|
|
34
|
+
return a.detect()
|
|
35
|
+
} catch {
|
|
36
|
+
return false
|
|
37
|
+
}
|
|
38
|
+
})
|
|
39
|
+
const settled = await Promise.allSettled(active.map((a) => a.discover(root, window)))
|
|
40
|
+
const sessions: HarnessSession[] = []
|
|
41
|
+
for (const r of settled) {
|
|
42
|
+
if (r.status === 'fulfilled') sessions.push(...r.value)
|
|
43
|
+
}
|
|
44
|
+
return sessions.sort((a, b) => endedMs(b) - endedMs(a))
|
|
45
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Harness adapters: discovery + a reading guide, deliberately NOT normalizers.
|
|
3
|
+
* The analyzer is an agent that reads native transcript formats; each adapter
|
|
4
|
+
* only says where sessions live and how to read that format.
|
|
5
|
+
*/
|
|
6
|
+
import type { HarnessSession } from '../types'
|
|
7
|
+
|
|
8
|
+
export type TimeWindow = { start: Date; end: Date }
|
|
9
|
+
|
|
10
|
+
export interface HarnessAdapter {
|
|
11
|
+
name: string
|
|
12
|
+
/** Cheap machine-level presence check (a directory stat, never network). */
|
|
13
|
+
detect(): boolean
|
|
14
|
+
/**
|
|
15
|
+
* Sessions of this harness whose cwd sits at/under `root` and whose activity
|
|
16
|
+
* overlaps `window`. Must never throw — errors degrade to [].
|
|
17
|
+
*/
|
|
18
|
+
discover(root: string, window: TimeWindow): Promise<HarnessSession[]>
|
|
19
|
+
/** Prompt fragment: how the analyzer should read this transcript format. */
|
|
20
|
+
readingGuide: string
|
|
21
|
+
}
|