@pikku/core 0.12.52 → 0.12.53

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.
@@ -0,0 +1,66 @@
1
+ import { describe, test, afterEach } from 'node:test'
2
+ import assert from 'node:assert/strict'
3
+ import { IstanbulCoverageService } from './istanbul-coverage-service.js'
4
+
5
+ const FILE = '/proj/src/things.function.ts'
6
+
7
+ const seedCoverage = () => {
8
+ ;(globalThis as any).__coverage__ = {
9
+ [FILE]: {
10
+ path: FILE,
11
+ statementMap: {
12
+ '0': { start: { line: 2, column: 2 }, end: { line: 2, column: 20 } },
13
+ '1': { start: { line: 3, column: 4 }, end: { line: 3, column: 30 } },
14
+ '2': { start: { line: 5, column: 2 }, end: { line: 6, column: 18 } },
15
+ },
16
+ s: { '0': 3, '1': 0, '2': 3 },
17
+ branchMap: {},
18
+ b: {},
19
+ fnMap: {},
20
+ f: {},
21
+ },
22
+ }
23
+ }
24
+
25
+ afterEach(() => {
26
+ delete (globalThis as any).__coverage__
27
+ })
28
+
29
+ describe('IstanbulCoverageService', () => {
30
+ test('takeCoverage converts __coverage__ statement counts into line hits', async () => {
31
+ seedCoverage()
32
+ const service = new IstanbulCoverageService()
33
+ await service.start()
34
+ const snapshot = await service.takeCoverage()
35
+ assert.equal(snapshot.kind, 'line-hits')
36
+ if (snapshot.kind !== 'line-hits') return
37
+ const hits = snapshot.lineHits.get(FILE)
38
+ assert.ok(hits, 'file should have line hits')
39
+ assert.equal(hits.get(2), 3)
40
+ assert.equal(hits.get(3), 0)
41
+ assert.equal(hits.get(5), 3)
42
+ assert.equal(
43
+ hits.get(6),
44
+ undefined,
45
+ 'counts attach to statement start lines only'
46
+ )
47
+ })
48
+
49
+ test('reset zeroes counters so per-scenario attribution is possible', async () => {
50
+ seedCoverage()
51
+ const service = new IstanbulCoverageService()
52
+ await service.reset()
53
+ const snapshot = await service.takeCoverage()
54
+ if (snapshot.kind !== 'line-hits') return assert.fail('expected line-hits')
55
+ const hits = snapshot.lineHits.get(FILE)
56
+ assert.equal(hits?.get(2), 0)
57
+ assert.equal(hits?.get(5), 0)
58
+ })
59
+
60
+ test('takeCoverage with no __coverage__ global returns an empty snapshot', async () => {
61
+ const service = new IstanbulCoverageService()
62
+ const snapshot = await service.takeCoverage()
63
+ if (snapshot.kind !== 'line-hits') return assert.fail('expected line-hits')
64
+ assert.equal(snapshot.lineHits.size, 0)
65
+ })
66
+ })
@@ -0,0 +1,65 @@
1
+ import type {
2
+ CoverageService,
3
+ CoverageSnapshot,
4
+ LineHits,
5
+ } from './v8-coverage-service.js'
6
+
7
+ interface IstanbulStatementLocation {
8
+ start: { line: number }
9
+ end: { line: number }
10
+ }
11
+
12
+ interface IstanbulFileCoverage {
13
+ path: string
14
+ statementMap: Record<string, IstanbulStatementLocation>
15
+ s: Record<string, number>
16
+ b: Record<string, number[]>
17
+ f: Record<string, number>
18
+ }
19
+
20
+ type IstanbulCoverageGlobal = Record<string, IstanbulFileCoverage>
21
+
22
+ const getCoverageGlobal = (): IstanbulCoverageGlobal | undefined =>
23
+ (globalThis as { __coverage__?: IstanbulCoverageGlobal }).__coverage__
24
+
25
+ /**
26
+ * Reads istanbul-instrumented counters from the `__coverage__` global —
27
+ * the coverage backend for runtimes without V8 precise coverage (e.g. Bun,
28
+ * where source files are instrumented at load time by a Bun loader plugin).
29
+ */
30
+ export class IstanbulCoverageService implements CoverageService {
31
+ async start(): Promise<void> {}
32
+
33
+ async takeCoverage(): Promise<CoverageSnapshot> {
34
+ const coverage = getCoverageGlobal()
35
+ const lineHits: LineHits = new Map()
36
+ for (const file of Object.values(coverage ?? {})) {
37
+ let hits = lineHits.get(file.path)
38
+ if (!hits) {
39
+ hits = new Map()
40
+ lineHits.set(file.path, hits)
41
+ }
42
+ // istanbul line semantics: a statement's count belongs to its start
43
+ // line only, so an enclosing multi-line statement never masks an
44
+ // unexecuted inner statement (e.g. a throw inside a taken if).
45
+ for (const [id, location] of Object.entries(file.statementMap)) {
46
+ const count = file.s[id] ?? 0
47
+ const line = location.start.line
48
+ hits.set(line, Math.max(hits.get(line) ?? 0, count))
49
+ }
50
+ }
51
+ return { kind: 'line-hits', lineHits }
52
+ }
53
+
54
+ async reset(): Promise<void> {
55
+ for (const file of Object.values(getCoverageGlobal() ?? {})) {
56
+ for (const id of Object.keys(file.s)) file.s[id] = 0
57
+ for (const id of Object.keys(file.f)) file.f[id] = 0
58
+ for (const id of Object.keys(file.b)) {
59
+ file.b[id] = file.b[id]!.map(() => 0)
60
+ }
61
+ }
62
+ }
63
+
64
+ async stop(): Promise<void> {}
65
+ }
@@ -3,14 +3,7 @@ import type {
3
3
  ActorFlowVerdict,
4
4
  } from '../wirings/actor-flow/actor-flow.types.js'
5
5
 
6
- /**
7
- * A scenario actor: a synthetic user (a normal user row flagged `actor`) that
8
- * workflow steps can run as. Passed to `workflow.do(step, rpc, data, { actor })`
9
- * — the step then goes through the actor's authenticated client over the REAL
10
- * transport (auth middleware, permissions, serialization all exercised),
11
- * never through internal dispatch. Login is lazy: the first `invoke` signs the
12
- * actor in and the session is cached for the actor's lifetime.
13
- */
6
+ /** A synthetic user (a user row flagged `actor`) that workflow steps run as over the real transport */
14
7
  export interface ScenarioActor<TAgentName extends string = string> {
15
8
  /** Stable actor name (the key in pikku.config.json's actor registry). */
16
9
  readonly name: string
@@ -18,22 +11,11 @@ export interface ScenarioActor<TAgentName extends string = string> {
18
11
  readonly email: string
19
12
  /** Invoke an exposed RPC as this actor over the real transport. */
20
13
  invoke(rpcName: string, data: unknown): Promise<unknown>
21
- /**
22
- * Hold a dynamic conversation with a target Pikku AI agent, in THIS actor's
23
- * persona (personality/jobTitle). Drives the target over the real transport
24
- * as the signed-in actor, answers its tool-approval requests in-persona, and
25
- * returns the actor's verdict on whether the task was met. Deterministic
26
- * checks are the caller's job — use `invoke` afterwards. In a typed project
27
- * `agent` is constrained to the generated union of agent names.
28
- */
14
+ /** Converse with a Pikku AI agent in this actor's persona and return its verdict */
29
15
  converse(options: ConverseOptions<TAgentName>): Promise<ActorFlowVerdict>
30
16
  }
31
17
 
32
- /**
33
- * Display/config metadata for an actor (from pikku.config.json). The email
34
- * identifies the actor's user row; personality/jobTitle exist for the console
35
- * screen and for agent-driven flows (the agent plays the persona).
36
- */
18
+ /** Display/config metadata for an actor (from pikku.config.json) */
37
19
  export interface ScenarioActorConfig {
38
20
  email: string
39
21
  name?: string
@@ -0,0 +1,104 @@
1
+ import { describe, test, beforeEach, afterEach } from 'node:test'
2
+ import assert from 'node:assert/strict'
3
+ import {
4
+ StubTracker,
5
+ stub,
6
+ spy,
7
+ isTestRun,
8
+ getStubTracker,
9
+ } from './stub-tracker.js'
10
+
11
+ describe('StubTracker', () => {
12
+ test('records calls and exposes them via getCalls', () => {
13
+ const tracker = new StubTracker()
14
+ tracker.record('email', 'send', [{ to: 'a@b.c' }])
15
+ tracker.record('payments', 'charge', [{ amount: 5 }])
16
+
17
+ assert.equal(tracker.getCalls().length, 2)
18
+ const emailCalls = tracker.getCalls('email')
19
+ assert.equal(emailCalls.length, 1)
20
+ assert.deepEqual(emailCalls[0], {
21
+ service: 'email',
22
+ method: 'send',
23
+ args: [{ to: 'a@b.c' }],
24
+ })
25
+ })
26
+
27
+ test('reset clears recorded calls and strict-mode touches', () => {
28
+ const tracker = new StubTracker()
29
+ tracker.record('email', 'send', [])
30
+ tracker.reset()
31
+ assert.equal(tracker.getCalls().length, 0)
32
+ tracker.verify()
33
+ })
34
+
35
+ test('legacy stub() proxy records through the same tracker', async () => {
36
+ const tracker = new StubTracker()
37
+ const email = tracker.stub<{ send: (msg: unknown) => Promise<void> }>(
38
+ 'email'
39
+ )
40
+ await email.send({ to: 'x@y.z' })
41
+ assert.deepEqual(tracker.getCalls('email')[0]?.args, [{ to: 'x@y.z' }])
42
+ tracker.assert('email', 'send')
43
+ })
44
+ })
45
+
46
+ describe('stub/spy core utils', () => {
47
+ beforeEach(() => getStubTracker().reset())
48
+
49
+ test('stub() records into the default tracker and uses the implementation', async () => {
50
+ const email = stub<{ send: (msg: unknown) => Promise<{ ok: boolean }> }>(
51
+ 'email',
52
+ { send: async () => ({ ok: true }) }
53
+ )
54
+ const result = await email.send({ to: 'a@b.c' })
55
+ assert.deepEqual(result, { ok: true })
56
+ assert.deepEqual(getStubTracker().getCalls('email')[0], {
57
+ service: 'email',
58
+ method: 'send',
59
+ args: [{ to: 'a@b.c' }],
60
+ })
61
+ })
62
+
63
+ test('stub() without an implementation resolves undefined', async () => {
64
+ const payments = stub<{ charge: (x: unknown) => Promise<unknown> }>(
65
+ 'payments'
66
+ )
67
+ assert.equal(await payments.charge({ amount: 5 }), undefined)
68
+ assert.equal(getStubTracker().getCalls('payments').length, 1)
69
+ })
70
+
71
+ test('spy() records calls and passes through to the real service', async () => {
72
+ let realCalled = 0
73
+ const real = {
74
+ config: { retries: 3 },
75
+ send: async (msg: { to: string }) => {
76
+ realCalled++
77
+ return { delivered: msg.to }
78
+ },
79
+ }
80
+ const email = spy('email', real)
81
+
82
+ assert.deepEqual(await email.send({ to: 'a@b.c' }), {
83
+ delivered: 'a@b.c',
84
+ })
85
+ assert.equal(realCalled, 1)
86
+ assert.equal(email.config.retries, 3)
87
+ assert.equal(getStubTracker().getCalls('email').length, 1)
88
+ })
89
+ })
90
+
91
+ describe('isTestRun', () => {
92
+ const original = process.env.PIKKU_TEST_RUN
93
+ afterEach(() => {
94
+ if (original === undefined) delete process.env.PIKKU_TEST_RUN
95
+ else process.env.PIKKU_TEST_RUN = original
96
+ })
97
+
98
+ test('reflects the PIKKU_TEST_RUN environment variable', () => {
99
+ delete process.env.PIKKU_TEST_RUN
100
+ assert.equal(isTestRun(), false)
101
+ process.env.PIKKU_TEST_RUN = 'true'
102
+ assert.equal(isTestRun(), true)
103
+ })
104
+ })
@@ -0,0 +1,185 @@
1
+ export type StubCall = { service: string; method: string; args: unknown[] }
2
+
3
+ type Call = { method: string; args: unknown[]; verified: boolean }
4
+
5
+ /**
6
+ * Tracks calls made to stubbed services and enforces strict mode:
7
+ * once a scenario asserts any call on a service, every recorded call on that
8
+ * service must be verified by end of scenario — otherwise the After hook fails.
9
+ * Services never touched by an assertion stay lenient.
10
+ */
11
+ export class StubTracker {
12
+ private readonly calls = new Map<string, Call[]>()
13
+ private readonly touched = new Set<string>()
14
+
15
+ record(service: string, method: string, args: unknown[]): void {
16
+ const list = this.calls.get(service) ?? []
17
+ list.push({ method, args, verified: false })
18
+ this.calls.set(service, list)
19
+ }
20
+
21
+ getCalls(service?: string): StubCall[] {
22
+ const result: StubCall[] = []
23
+ for (const [name, list] of this.calls) {
24
+ if (service && name !== service) continue
25
+ for (const call of list) {
26
+ result.push({ service: name, method: call.method, args: call.args })
27
+ }
28
+ }
29
+ return result
30
+ }
31
+
32
+ reset(): void {
33
+ this.calls.clear()
34
+ this.touched.clear()
35
+ }
36
+
37
+ stub<T>(service: string): T {
38
+ const self = this
39
+ return new Proxy(Object.create(null) as object, {
40
+ get(_, method: string) {
41
+ return (...args: unknown[]) => {
42
+ self.record(service, method, args)
43
+ return Promise.resolve()
44
+ }
45
+ },
46
+ }) as unknown as T
47
+ }
48
+
49
+ assert(service: string, method: string): void {
50
+ this.touched.add(service)
51
+ const list = this.calls.get(service) ?? []
52
+ const idx = list.findIndex((c) => c.method === method && !c.verified)
53
+ if (idx === -1) {
54
+ const seen = list.map((c) => c.method).join(', ') || '(none)'
55
+ throw new Error(
56
+ `Expected "${service}.${method}" to have been called. Recorded: ${seen}`
57
+ )
58
+ }
59
+ list[idx]!.verified = true
60
+ }
61
+
62
+ assertCall(
63
+ service: string,
64
+ method: string,
65
+ predicate: (args: unknown[]) => boolean,
66
+ description: string
67
+ ): void {
68
+ this.touched.add(service)
69
+ const list = this.calls.get(service) ?? []
70
+ const idx = list.findIndex(
71
+ (c) => c.method === method && !c.verified && predicate(c.args)
72
+ )
73
+ if (idx === -1) {
74
+ const seen =
75
+ list
76
+ .filter((c) => c.method === method)
77
+ .map((c) => JSON.stringify(c.args[0]))
78
+ .join('\n ') || '(none)'
79
+ throw new Error(`Expected ${description} but found:\n ${seen}`)
80
+ }
81
+ list[idx]!.verified = true
82
+ }
83
+
84
+ assertNoCalls(
85
+ service: string,
86
+ method?: string,
87
+ predicate?: (args: unknown[]) => boolean,
88
+ description?: string
89
+ ): void {
90
+ this.touched.add(service)
91
+ const list = this.calls.get(service) ?? []
92
+ const relevant = (
93
+ method ? list.filter((c) => c.method === method) : list
94
+ ).filter((c) => !predicate || predicate(c.args))
95
+ if (relevant.length > 0) {
96
+ const calls = relevant
97
+ .map(
98
+ (c) =>
99
+ `${c.method}(${c.args.map((a) => JSON.stringify(a)).join(', ')})`
100
+ )
101
+ .join('\n ')
102
+ const what = description ?? `"${service}${method ? '.' + method : ''}"`
103
+ throw new Error(`Expected no ${what} calls but got:\n ${calls}`)
104
+ }
105
+ }
106
+
107
+ verify(): void {
108
+ const errors: string[] = []
109
+ for (const service of this.touched) {
110
+ const unverified = (this.calls.get(service) ?? []).filter(
111
+ (c) => !c.verified
112
+ )
113
+ for (const c of unverified) {
114
+ const argStr = c.args.map((a) => JSON.stringify(a)).join(', ')
115
+ errors.push(` ${service}.${c.method}(${argStr})`)
116
+ }
117
+ }
118
+ if (errors.length) {
119
+ throw new Error(
120
+ `Unexpected stub calls — assert them in the scenario or remove the side effect:\n${errors.join('\n')}`
121
+ )
122
+ }
123
+ }
124
+ }
125
+
126
+ /**
127
+ * Creates a Proxy suitable for passing as `existingServices` to
128
+ * `createSingletonServices`. Every property returns `tracker.stub(prop)`
129
+ * EXCEPT `schema`, which returns `undefined` so the service factory creates
130
+ * a real schema service. Stubbing the schema makes validation a no-op —
131
+ * required fields pass silently and tests validate nothing.
132
+ */
133
+ export function createStubProxy(tracker: StubTracker): Record<string, unknown> {
134
+ return new Proxy({} as Record<string, unknown>, {
135
+ get(_, prop: string) {
136
+ if (prop === 'schema') return undefined
137
+ return tracker.stub(prop)
138
+ },
139
+ })
140
+ }
141
+
142
+ const defaultStubTracker = new StubTracker()
143
+
144
+ /** The process-wide tracker that `stub()`/`spy()` record into — read by the console's getStubCalls/resetStubs RPCs */
145
+ export const getStubTracker = (): StubTracker => defaultStubTracker
146
+
147
+ /** True when the server was started by `pikku dev --test` (sets PIKKU_TEST_RUN) */
148
+ export const isTestRun = (): boolean =>
149
+ (globalThis as { process?: { env?: Record<string, string | undefined> } })
150
+ .process?.env?.PIKKU_TEST_RUN === 'true'
151
+
152
+ /**
153
+ * Creates a recording fake for a service. Methods present on `impl` run and
154
+ * their result is returned; missing methods resolve `undefined`. Every call
155
+ * is recorded so scenarios can assert via workflow.expectService().
156
+ */
157
+ export function stub<T = any>(service: string, impl?: Partial<T>): T {
158
+ return new Proxy((impl ?? {}) as object, {
159
+ get(target: any, method: string | symbol) {
160
+ if (typeof method === 'symbol') return target[method]
161
+ const real = target[method]
162
+ if (typeof real !== 'function' && real !== undefined) return real
163
+ return (...args: unknown[]) => {
164
+ defaultStubTracker.record(service, method, args)
165
+ return real ? real.apply(target, args) : Promise.resolve(undefined)
166
+ }
167
+ },
168
+ }) as T
169
+ }
170
+
171
+ /** Wraps a real service so every method call is recorded and passed through */
172
+ export function spy<T extends object>(service: string, real: T): T {
173
+ return new Proxy(real, {
174
+ get(target: any, method: string | symbol) {
175
+ const value = target[method]
176
+ if (typeof method === 'symbol' || typeof value !== 'function') {
177
+ return value
178
+ }
179
+ return (...args: unknown[]) => {
180
+ defaultStubTracker.record(service, method, args)
181
+ return value.apply(target, args)
182
+ }
183
+ },
184
+ }) as T
185
+ }
@@ -0,0 +1,69 @@
1
+ import { describe, test, after } from 'node:test'
2
+ import assert from 'node:assert/strict'
3
+ import { V8CoverageService } from './v8-coverage-service.js'
4
+
5
+ const service = new V8CoverageService()
6
+
7
+ function coveredProbe(n: number): number {
8
+ return n * 2
9
+ }
10
+
11
+ describe('V8CoverageService', () => {
12
+ after(async () => {
13
+ await service.stop()
14
+ })
15
+
16
+ test('start + takeCoverage reports call counts for functions invoked after start', async () => {
17
+ await service.start()
18
+ coveredProbe(21)
19
+
20
+ const snapshot = await service.takeCoverage()
21
+ assert.equal(snapshot.kind, 'v8-scripts')
22
+ if (snapshot.kind !== 'v8-scripts') return
23
+ const own = snapshot.scripts.find((s) =>
24
+ s.url.includes('v8-coverage-service.test')
25
+ )
26
+ assert.ok(own, 'own test script should appear in precise coverage')
27
+ const probe = own.functions.find((f) =>
28
+ f.functionName.includes('coveredProbe')
29
+ )
30
+ assert.ok(probe, 'coveredProbe should be tracked')
31
+ assert.ok(
32
+ probe.ranges[0].count >= 1,
33
+ `coveredProbe should have been counted, got ${probe.ranges[0].count}`
34
+ )
35
+ })
36
+
37
+ test('reset clears call counts so attribution per run is possible', async () => {
38
+ coveredProbe(1)
39
+ await service.reset()
40
+ const snapshot = await service.takeCoverage()
41
+ if (snapshot.kind !== 'v8-scripts') return assert.fail('expected scripts')
42
+ const own = snapshot.scripts.find((s) =>
43
+ s.url.includes('v8-coverage-service.test')
44
+ )
45
+ const probe = own?.functions.find((f) =>
46
+ f.functionName.includes('coveredProbe')
47
+ )
48
+ const count = probe?.ranges[0].count ?? 0
49
+ assert.equal(
50
+ count,
51
+ 0,
52
+ `after reset coveredProbe count should be 0, got ${count}`
53
+ )
54
+ })
55
+
56
+ test('getScriptSource returns the executed source for mapping', async () => {
57
+ const snapshot = await service.takeCoverage()
58
+ if (snapshot.kind !== 'v8-scripts') return assert.fail('expected scripts')
59
+ const own = snapshot.scripts.find((s) =>
60
+ s.url.includes('v8-coverage-service.test')
61
+ )
62
+ assert.ok(own)
63
+ const source = await snapshot.getScriptSource(own.scriptId)
64
+ assert.ok(
65
+ source.includes('coveredProbe'),
66
+ 'script source should contain the probe function'
67
+ )
68
+ })
69
+ })
@@ -0,0 +1,121 @@
1
+ // node:inspector is imported lazily inside start() so this module stays
2
+ // loadable on runtimes without it (e.g. Cloudflare Workers).
3
+
4
+ export interface CoverageRange {
5
+ startOffset: number
6
+ endOffset: number
7
+ count: number
8
+ }
9
+
10
+ export interface FunctionCoverage {
11
+ functionName: string
12
+ isBlockCoverage: boolean
13
+ ranges: CoverageRange[]
14
+ }
15
+
16
+ export interface ScriptCoverage {
17
+ scriptId: string
18
+ url: string
19
+ functions: FunctionCoverage[]
20
+ }
21
+
22
+ export type LineHits = Map<string, Map<number, number>>
23
+
24
+ export type CoverageSnapshot =
25
+ | {
26
+ kind: 'v8-scripts'
27
+ scripts: ScriptCoverage[]
28
+ getScriptSource: (scriptId: string) => Promise<string>
29
+ }
30
+ | { kind: 'line-hits'; lineHits: LineHits }
31
+
32
+ export interface CoverageService {
33
+ start(): Promise<void>
34
+ takeCoverage(): Promise<CoverageSnapshot>
35
+ reset(): Promise<void>
36
+ stop(): Promise<void>
37
+ }
38
+
39
+ type InspectorSession = {
40
+ connect(): void
41
+ disconnect(): void
42
+ post(
43
+ method: string,
44
+ params?: unknown,
45
+ callback?: (err: Error | null, result?: any) => void
46
+ ): void
47
+ }
48
+
49
+ export class V8CoverageService implements CoverageService {
50
+ private session: InspectorSession | null = null
51
+ private startPromise: Promise<void> | null = null
52
+
53
+ private post<T>(method: string, params?: unknown): Promise<T> {
54
+ const session = this.session
55
+ if (!session) {
56
+ throw new Error('V8CoverageService not started — call start() first')
57
+ }
58
+ return new Promise<T>((resolve, reject) => {
59
+ session.post(method, params, (err, result) =>
60
+ err ? reject(err) : resolve(result as T)
61
+ )
62
+ })
63
+ }
64
+
65
+ start(): Promise<void> {
66
+ this.startPromise ??= this.doStart()
67
+ return this.startPromise
68
+ }
69
+
70
+ private async doStart(): Promise<void> {
71
+ const inspector = await import('node:inspector')
72
+ this.session = new inspector.Session() as unknown as InspectorSession
73
+ this.session.connect()
74
+ await this.post('Profiler.enable')
75
+ await this.post('Debugger.enable')
76
+ await this.post('Profiler.startPreciseCoverage', {
77
+ callCount: true,
78
+ detailed: true,
79
+ })
80
+ }
81
+
82
+ async takeCoverage(): Promise<CoverageSnapshot> {
83
+ const { result } = await this.post<{ result: ScriptCoverage[] }>(
84
+ 'Profiler.takePreciseCoverage'
85
+ )
86
+ return {
87
+ kind: 'v8-scripts',
88
+ scripts: result.filter((script) => script.url.startsWith('file://')),
89
+ getScriptSource: (scriptId) => this.getScriptSource(scriptId),
90
+ }
91
+ }
92
+
93
+ async getScriptSource(scriptId: string): Promise<string> {
94
+ const { scriptSource } = await this.post<{ scriptSource: string }>(
95
+ 'Debugger.getScriptSource',
96
+ { scriptId }
97
+ )
98
+ return scriptSource
99
+ }
100
+
101
+ async reset(): Promise<void> {
102
+ await this.post('Profiler.stopPreciseCoverage')
103
+ await this.post('Profiler.startPreciseCoverage', {
104
+ callCount: true,
105
+ detailed: true,
106
+ })
107
+ }
108
+
109
+ async stop(): Promise<void> {
110
+ if (!this.session) return
111
+ try {
112
+ await this.post('Profiler.stopPreciseCoverage')
113
+ await this.post('Profiler.disable')
114
+ await this.post('Debugger.disable')
115
+ } finally {
116
+ this.session.disconnect()
117
+ this.session = null
118
+ this.startPromise = null
119
+ }
120
+ }
121
+ }
@@ -38,6 +38,7 @@ import type { WorkflowRunService } from '../wirings/workflow/workflow.types.js'
38
38
  import type { CredentialService } from '../services/credential-service.js'
39
39
  import type { EmailService } from '../services/email-service.js'
40
40
  import type { MetaService } from '../services/meta-service.js'
41
+ import type { CoverageService } from '../services/v8-coverage-service.js'
41
42
  import type { SessionStore } from '../services/session-store.js'
42
43
  import type {
43
44
  AuditDurability,
@@ -150,6 +151,12 @@ export type FunctionMeta = FunctionRuntimeMeta &
150
151
  isDirectFunction: boolean
151
152
  sourceFile: string
152
153
  exportedName: string
154
+ /** File containing the handler body when it differs from sourceFile (imported handlers) */
155
+ bodySourceFile?: string
156
+ /** 1-indexed first line of the handler body (verbose meta; coverage mapping) */
157
+ bodyStart: number
158
+ /** 1-indexed last line of the handler body (verbose meta; coverage mapping) */
159
+ bodyEnd: number
153
160
  } & CommonWireMeta
154
161
  >
155
162
 
@@ -293,6 +300,8 @@ export interface CoreSingletonServices<Config extends CoreConfig = CoreConfig> {
293
300
  emailService?: EmailService
294
301
  /** Meta service for reading .pikku metadata files (filesystem on Node, R2/KV on CF) */
295
302
  metaService?: MetaService
303
+ /** V8 precise-coverage collector (`pikku dev --coverage` only) */
304
+ coverageService?: CoverageService
296
305
  /** Audit service for durable or staged audit event capture */
297
306
  audit?: AuditService
298
307
  /**