@astrale-os/cli 0.5.0-alpha.0 → 0.6.1-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/dist/astrale.js +12962 -487
- package/package.json +7 -3
- 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__/install-identity-override.test.ts +1 -1
- package/src/commands/__tests__/view.test.ts +100 -0
- package/src/commands/call.ts +1 -1
- package/src/commands/domain/install.ts +5 -5
- package/src/commands/domain/list.ts +2 -2
- package/src/commands/domain/publish.ts +3 -3
- 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/query.ts +27 -8
- package/src/commands/session/analyze.ts +50 -0
- package/src/commands/session/list.ts +36 -0
- package/src/commands/token.ts +1 -1
- 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/client.ts +30 -24
- package/src/kernel/expand.ts +3 -2
- package/src/kernel/index.ts +7 -1
- package/src/kernel/options.ts +1 -1
- package/src/lib/__tests__/instance-target.test.ts +34 -0
- 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/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/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 +12 -1
- package/src/registry.ts +1 -1
- 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 +2 -1
- package/studio/server/agent/runner.ts +4 -1
- package/studio/server/agent/session-id.ts +13 -0
- package/studio/server/api.ts +43 -25
- 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 +122 -22
- package/studio/server/state/views.test.ts +90 -0
- package/studio/server/state/views.ts +396 -99
- package/studio/server/state/visibility.ts +5 -5
- 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/studio/client/dist/assets/index-BcejyJpa.css +0 -1
- package/studio/client/dist/assets/index-Cqz3Oy_B.js +0 -179
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { afterEach, beforeAll, beforeEach, describe, expect, test } from 'bun:test'
|
|
2
|
+
import {
|
|
3
|
+
existsSync,
|
|
4
|
+
mkdirSync,
|
|
5
|
+
mkdtempSync,
|
|
6
|
+
readFileSync,
|
|
7
|
+
rmSync,
|
|
8
|
+
utimesSync,
|
|
9
|
+
writeFileSync,
|
|
10
|
+
} from 'node:fs'
|
|
11
|
+
import { tmpdir } from 'node:os'
|
|
12
|
+
import { join } from 'node:path'
|
|
13
|
+
|
|
14
|
+
import type { ResolvedSession } from '../session'
|
|
15
|
+
import type { SessionMeta } from '../types'
|
|
16
|
+
|
|
17
|
+
/** Seed an ambient session on disk with a chosen id + last-activity age. */
|
|
18
|
+
function seedAmbient(id: string, root: string, ageMs: number): void {
|
|
19
|
+
mkdirSync(sessionDir(id), { recursive: true })
|
|
20
|
+
const meta: SessionMeta = { id, root, explicit: false, createdAt: new Date().toISOString() }
|
|
21
|
+
writeFileSync(metaPath(id), JSON.stringify(meta))
|
|
22
|
+
writeFileSync(eventsPath(id), '{}\n')
|
|
23
|
+
const when = new Date(Date.now() - ageMs)
|
|
24
|
+
utimesSync(eventsPath(id), when, when)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Set ASTRALE_HOME before the store/paths singleton is first evaluated (below,
|
|
28
|
+
// via dynamic import). All on-disk access goes through the store helpers so it
|
|
29
|
+
// stays consistent whichever home the singleton captured.
|
|
30
|
+
process.env.ASTRALE_HOME = mkdtempSync(join(tmpdir(), 'astrale-tele-session-'))
|
|
31
|
+
|
|
32
|
+
let resolveSession: (cwd: string) => ResolvedSession
|
|
33
|
+
let ensureSession: (cwd: string) => ResolvedSession
|
|
34
|
+
let eventsPath: (id: string) => string
|
|
35
|
+
let metaPath: (id: string) => string
|
|
36
|
+
let sessionDir: (id: string) => string
|
|
37
|
+
let sessionsRoot: () => string
|
|
38
|
+
let IDLE_WINDOW_MS: number
|
|
39
|
+
|
|
40
|
+
let work: string
|
|
41
|
+
|
|
42
|
+
beforeAll(async () => {
|
|
43
|
+
const session = await import('../session')
|
|
44
|
+
const store = await import('../store')
|
|
45
|
+
resolveSession = session.resolveSession
|
|
46
|
+
ensureSession = session.ensureSession
|
|
47
|
+
eventsPath = store.eventsPath
|
|
48
|
+
metaPath = store.metaPath
|
|
49
|
+
sessionDir = store.sessionDir
|
|
50
|
+
sessionsRoot = store.sessionsRoot
|
|
51
|
+
IDLE_WINDOW_MS = store.IDLE_WINDOW_MS
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
beforeEach(() => {
|
|
55
|
+
if (!sessionsRoot().startsWith(tmpdir())) throw new Error('refusing to clean a non-tmp home')
|
|
56
|
+
rmSync(sessionsRoot(), { recursive: true, force: true })
|
|
57
|
+
delete process.env.ASTRALE_SESSION
|
|
58
|
+
work = mkdtempSync(join(tmpdir(), 'astrale-tele-work-'))
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
afterEach(() => {
|
|
62
|
+
rmSync(work, { recursive: true, force: true })
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
describe('resolveSession — ambient', () => {
|
|
66
|
+
test('reuses an open ambient session bucketed to the same root', () => {
|
|
67
|
+
// An old-stamp id proves reuse: a fresh mint would carry the current minute.
|
|
68
|
+
const existing = 'amb-deadbeef-200001010000'
|
|
69
|
+
seedAmbient(existing, work, 5 * 60_000)
|
|
70
|
+
|
|
71
|
+
const r = resolveSession(work)
|
|
72
|
+
|
|
73
|
+
expect(r).toEqual({ id: existing, root: work, explicit: false })
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
test('mints a fresh session once the previous one is idle', () => {
|
|
77
|
+
const existing = 'amb-deadbeef-200001010000'
|
|
78
|
+
seedAmbient(existing, work, IDLE_WINDOW_MS + 60_000)
|
|
79
|
+
|
|
80
|
+
const r = resolveSession(work)
|
|
81
|
+
|
|
82
|
+
expect(r.id).not.toBe(existing)
|
|
83
|
+
expect(r.explicit).toBe(false)
|
|
84
|
+
expect(r.id).toMatch(/^amb-[0-9a-f]{8}-\d{12}$/)
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
test('does not reuse a session bucketed to a different root', () => {
|
|
88
|
+
const existing = 'amb-deadbeef-200001010000'
|
|
89
|
+
seedAmbient(existing, work, 5 * 60_000)
|
|
90
|
+
|
|
91
|
+
const other = mkdtempSync(join(tmpdir(), 'astrale-tele-other-'))
|
|
92
|
+
try {
|
|
93
|
+
expect(resolveSession(other).id).not.toBe(existing)
|
|
94
|
+
} finally {
|
|
95
|
+
rmSync(other, { recursive: true, force: true })
|
|
96
|
+
}
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
test('ensureSession creates then reuses one session across calls', () => {
|
|
100
|
+
const first = ensureSession(work)
|
|
101
|
+
const second = ensureSession(work)
|
|
102
|
+
expect(second.id).toBe(first.id)
|
|
103
|
+
expect(first.id).toMatch(/^amb-[0-9a-f]{8}-\d{12}$/)
|
|
104
|
+
})
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
describe('resolveSession — root bucketing', () => {
|
|
108
|
+
test('buckets by the nearest .git ancestor, not cwd', () => {
|
|
109
|
+
mkdirSync(join(work, '.git'), { recursive: true })
|
|
110
|
+
const deep = join(work, 'sub', 'deep')
|
|
111
|
+
mkdirSync(deep, { recursive: true })
|
|
112
|
+
|
|
113
|
+
expect(resolveSession(deep).root).toBe(work)
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
test('falls back to cwd when there is no git root', () => {
|
|
117
|
+
expect(resolveSession(work).root).toBe(work)
|
|
118
|
+
})
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
describe('resolveSession — explicit ASTRALE_SESSION', () => {
|
|
122
|
+
test('honors an explicit id, sanitizing unsafe chars and capping at 64', () => {
|
|
123
|
+
process.env.ASTRALE_SESSION = 'my/weird:sess!!'
|
|
124
|
+
const r = resolveSession(work)
|
|
125
|
+
expect(r).toEqual({ id: 'myweirdsess', root: work, explicit: true })
|
|
126
|
+
|
|
127
|
+
process.env.ASTRALE_SESSION = 'a'.repeat(80)
|
|
128
|
+
expect(resolveSession(work).id).toBe('a'.repeat(64))
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
test('falls through to ambient when the id is empty after sanitizing', () => {
|
|
132
|
+
process.env.ASTRALE_SESSION = '///'
|
|
133
|
+
const r = resolveSession(work)
|
|
134
|
+
expect(r.explicit).toBe(false)
|
|
135
|
+
expect(r.id).toMatch(/^amb-/)
|
|
136
|
+
})
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
describe('ensureSession', () => {
|
|
140
|
+
test('creates the session dir and a readable meta.json once', () => {
|
|
141
|
+
const r = ensureSession(work)
|
|
142
|
+
|
|
143
|
+
expect(existsSync(metaPath(r.id))).toBe(true)
|
|
144
|
+
const meta = JSON.parse(readFileSync(metaPath(r.id), 'utf-8')) as SessionMeta
|
|
145
|
+
expect(meta.id).toBe(r.id)
|
|
146
|
+
expect(meta.root).toBe(work)
|
|
147
|
+
expect(meta.explicit).toBe(false)
|
|
148
|
+
expect(typeof meta.createdAt).toBe('string')
|
|
149
|
+
})
|
|
150
|
+
})
|
|
151
|
+
|
|
152
|
+
describe('analyzed sessions are never reused', () => {
|
|
153
|
+
test('an open ambient session with an .analyzed marker gets a fresh mint', async () => {
|
|
154
|
+
const { markerPath } = await import('../store')
|
|
155
|
+
const root = join(work, 'analyzed-root')
|
|
156
|
+
mkdirSync(root, { recursive: true })
|
|
157
|
+
seedAmbient('amb-analyzed-open', root, 0) // fresh mtime → open
|
|
158
|
+
writeFileSync(
|
|
159
|
+
markerPath('amb-analyzed-open'),
|
|
160
|
+
JSON.stringify({ analyzedAt: new Date().toISOString(), outcome: 'reported' }),
|
|
161
|
+
)
|
|
162
|
+
const resolved = resolveSession(root)
|
|
163
|
+
expect(resolved.id).not.toBe('amb-analyzed-open')
|
|
164
|
+
expect(resolved.id.startsWith('amb-')).toBe(true)
|
|
165
|
+
})
|
|
166
|
+
})
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { beforeAll, describe, expect, test } from 'bun:test'
|
|
2
|
+
import { existsSync, mkdirSync, mkdtempSync, rmSync, utimesSync, writeFileSync } from 'node:fs'
|
|
3
|
+
import { tmpdir } from 'node:os'
|
|
4
|
+
import { join } from 'node:path'
|
|
5
|
+
|
|
6
|
+
// Set the home before the paths singleton is captured (dynamic imports below).
|
|
7
|
+
process.env.ASTRALE_HOME = mkdtempSync(join(tmpdir(), 'astrale-tele-trigger-'))
|
|
8
|
+
|
|
9
|
+
let maybeTriggerAnalysis: (argv: string[]) => void
|
|
10
|
+
let sessionDir: (id: string) => string
|
|
11
|
+
|
|
12
|
+
beforeAll(async () => {
|
|
13
|
+
;({ maybeTriggerAnalysis } = await import('../trigger'))
|
|
14
|
+
;({ sessionDir } = await import('../store'))
|
|
15
|
+
// Sibling test files mutate shared state (env vars, and recorder.test.ts
|
|
16
|
+
// leaves a telemetry-disabled config.json in the shared home) — own it here.
|
|
17
|
+
delete process.env.ASTRALE_TELEMETRY
|
|
18
|
+
delete process.env.ASTRALE_TELEMETRY_NO_TRIGGER
|
|
19
|
+
const { CONFIG_PATH } = await import('../../lib/paths')
|
|
20
|
+
rmSync(CONFIG_PATH, { force: true })
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
function seed(id: string, ageMs: number, analyzed: boolean): void {
|
|
24
|
+
const dir = sessionDir(id)
|
|
25
|
+
mkdirSync(dir, { recursive: true })
|
|
26
|
+
writeFileSync(join(dir, 'meta.json'), JSON.stringify({ id, root: '/w', explicit: false }))
|
|
27
|
+
writeFileSync(join(dir, 'events.jsonl'), '{}\n')
|
|
28
|
+
if (analyzed) {
|
|
29
|
+
writeFileSync(
|
|
30
|
+
join(dir, '.analyzed'),
|
|
31
|
+
JSON.stringify({ analyzedAt: new Date().toISOString(), outcome: 'reported' }),
|
|
32
|
+
)
|
|
33
|
+
}
|
|
34
|
+
const when = new Date(Date.now() - ageMs)
|
|
35
|
+
utimesSync(join(dir, 'events.jsonl'), when, when)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
describe('opportunistic GC', () => {
|
|
39
|
+
test('removes analyzed sessions past retention, keeps recent and unanalyzed ones', () => {
|
|
40
|
+
const DAY = 24 * 60 * 60 * 1000
|
|
41
|
+
seed('gc-old-analyzed', 40 * DAY, true)
|
|
42
|
+
seed('gc-recent-analyzed', 2 * DAY, true)
|
|
43
|
+
// NOTE: no closed-unanalyzed sessions seeded — the trigger must find no
|
|
44
|
+
// analysis target, so this test never spawns a child process.
|
|
45
|
+
maybeTriggerAnalysis(['bun', 'astrale', 'status'])
|
|
46
|
+
expect(existsSync(sessionDir('gc-old-analyzed'))).toBe(false)
|
|
47
|
+
expect(existsSync(sessionDir('gc-recent-analyzed'))).toBe(true)
|
|
48
|
+
})
|
|
49
|
+
})
|
|
@@ -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
|
+
}
|