-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstandalone-rt.ts
More file actions
executable file
·230 lines (199 loc) · 8.93 KB
/
standalone-rt.ts
File metadata and controls
executable file
·230 lines (199 loc) · 8.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/env bun
/// <reference types="bun" />
/// <reference types="node" />
const startTime = Date.now()
import { resolve, join } from 'path'
import { access, readdir, writeFile } from 'fs/promises'
import chalk from 'chalk'
import { CapsuleSpineFactory } from "@stream44.studio/encapsulate/spine-factories/CapsuleSpineFactory"
import { CapsuleSpineContract } from "@stream44.studio/encapsulate/spine-contracts/CapsuleSpineContract.v0/Membrane"
import { TimingObserver } from "@stream44.studio/encapsulate/spine-factories/TimingObserver"
async function findPackageRoot(startDir: string): Promise<string> {
let dir = resolve(startDir)
while (true) {
try {
await access(join(dir, 'package.json'))
return dir
} catch { }
const parent = resolve(dir, '..')
if (parent === dir) return resolve(startDir)
dir = parent
}
}
export async function run(encapsulateHandler: any, runHandler: any, options?: { importMeta?: { dir: string }, runFromSnapshot?: boolean, captureEvents?: boolean }) {
const timing = process.argv.includes('--trace') ? TimingObserver({ startTime }) : undefined
const shouldCaptureEvents = options?.captureEvents === true || process.argv.includes('--capture-events')
const saveMembraneEvents = shouldCaptureEvents
timing?.recordMajor('INIT SPINE')
const eventsByKey = new Map<string, any>()
const capturedEvents: any[] = []
let isSerializing = false
const spineFilesystemRoot = options?.importMeta?.dir
? await findPackageRoot(options.importMeta.dir)
: process.cwd()
const { encapsulate, freeze, CapsulePropertyTypes, makeImportStack, hoistSnapshot, run: spineRun } = await CapsuleSpineFactory({
spineFilesystemRoot,
capsuleModuleProjectionRoot: options?.importMeta?.dir!,
enableCallerStackInference: true,
spineContracts: {
['#' + CapsuleSpineContract['#']]: CapsuleSpineContract
},
timing,
onMembraneEvent: (timing || shouldCaptureEvents) ? (event: any) => {
// Suppress spurious events triggered by JSON.stringify traversing proxy objects
if (isSerializing) return
const instanceId = event.target?.spineContractCapsuleInstanceId
const eventIndex = event.eventIndex
// Store event by composite key (instance ID + event index)
const key = `${eventIndex}`
eventsByKey.set(key, event)
// Collect event for .events.json persistence
if (shouldCaptureEvents) {
// Serialize value/result safely — they may contain capsule proxies with cyclic references
let safeValue: any = undefined
let safeResult: any = undefined
isSerializing = true
try {
if (event.value !== undefined) {
JSON.stringify(event.value)
safeValue = event.value
}
} catch {
safeValue = typeof event.value === 'object' ? `[${typeof event.value}]` : String(event.value)
}
try {
if (event.result !== undefined) {
JSON.stringify(event.result)
safeResult = event.result
}
} catch {
safeResult = typeof event.result === 'object' ? `[${typeof event.result}]` : String(event.result)
} finally {
isSerializing = false
}
// Serialize caller safely — strip stack array which may contain non-serializable objects
let safeCaller: any = undefined
if (event.caller) {
const { stack, ...callerRest } = event.caller
safeCaller = { ...callerRest }
if (stack) {
isSerializing = true
try {
JSON.stringify(stack)
safeCaller.stack = stack
} catch {
// stack frames may contain non-serializable objects
} finally {
isSerializing = false
}
}
}
capturedEvents.push({
eventIndex: event.eventIndex,
event: event.event,
membrane: event.membrane,
target: event.target ? { ...event.target } : undefined,
value: safeValue,
result: safeResult,
caller: safeCaller,
callEventIndex: event.callEventIndex,
})
}
if (timing) {
let capsuleRef = event.target?.capsuleSourceLineRef
let prop = event.target?.prop
let callerLocation = event.caller ? `${event.caller.fileUri}:${event.caller.line}` : 'unknown'
const eventType = event.event
// For call-result events, look up the original call event to get all info
if (eventType === 'call-result') {
const callKey = `${event.callEventIndex}`
const callEvent = eventsByKey.get(callKey)
if (callEvent) {
capsuleRef = callEvent.target?.capsuleSourceLineRef || capsuleRef
prop = callEvent.target?.prop || prop
if (callEvent.caller) {
callerLocation = `${callEvent.caller.fileUri}:${callEvent.caller.line}`
}
}
}
console.error(
chalk.gray(`[${eventIndex}]`),
chalk.cyan(eventType.padEnd(12)),
chalk.yellow(capsuleRef),
chalk.magenta(`.${prop}`),
chalk.dim(`from ${callerLocation}`)
)
}
} : undefined
})
timing?.recordMajor('ENCAPSULATE')
const exportedApi = await encapsulateHandler({
encapsulate,
CapsulePropertyTypes,
makeImportStack
})
let run
if (options?.runFromSnapshot === false) {
run = spineRun
timing?.recordMajor('RUN (in-memory)')
} else {
timing?.recordMajor('FREEZE')
const snapshot = await freeze()
timing?.recordMajor('HOIST SNAPSHOT')
const spine = await hoistSnapshot({
snapshot
})
run = spine.run
timing?.recordMajor('RUN (snapshot)')
}
const result = await run({
overrides: {
['@stream44.studio/t44/caps/ProjectTest']: {
'#': {
testRootDir: options?.importMeta?.dir,
verbose: !!process.env.VERBOSE,
}
},
['@stream44.studio/t44-ipfs.tech/caps/IpfsWorkbench']: {
'#': {
cacheDir: join(spineFilesystemRoot, '.~o/workspace.foundation', '@stream44.studio~t44-ipfs.tech~caps~IpfsWorkbench', 'daemons')
}
}
},
saveMembraneEvents,
}, async (opts) => {
const handlerResult = await runHandler({
...opts,
...(exportedApi || {}),
spineFilesystemRoot
})
return handlerResult
})
// Write .events.json alongside .sit.json files when capture is enabled
// This must happen AFTER run() completes so that post-handler lifecycle events
// (e.g. StructDispose, Dispose, property accesses during cleanup) are captured.
if (saveMembraneEvents && capturedEvents.length > 0 && options?.importMeta?.dir) {
try {
const spineInstancesDir = join(options.importMeta.dir, '.~o/encapsulate.dev/spine-instances')
await access(spineInstancesDir)
const dirs = await readdir(spineInstancesDir)
for (const dir of dirs) {
const dirPath = join(spineInstancesDir, String(dir))
const eventsFile = join(dirPath, 'root-capsule.events.json')
try {
isSerializing = true
const serialized = JSON.stringify(capturedEvents, null, 2)
isSerializing = false
await writeFile(eventsFile, serialized, 'utf-8')
} catch (writeErr: any) {
if (process.env.DEBUG_EVENTS) console.error(`[standalone-rt] write failed for ${eventsFile}: ${writeErr.message}`)
}
}
if (process.env.DEBUG_EVENTS) console.error(`[standalone-rt] Wrote events to ${dirs.length} dirs (${capturedEvents.length} events)`)
} catch (outerErr: any) {
if (process.env.DEBUG_EVENTS) console.error(`[standalone-rt] events write skipped: ${outerErr.message}`)
}
}
timing?.recordMajor('DONE')
return result
}