( params: BridgeCoreParams, )
| 258 | * Returns null on registration or session-creation failure. |
| 259 | */ |
| 260 | export async function initBridgeCore( |
| 261 | params: BridgeCoreParams, |
| 262 | ): Promise<BridgeCoreHandle | null> { |
| 263 | const { |
| 264 | dir, |
| 265 | machineName, |
| 266 | branch, |
| 267 | gitRepoUrl, |
| 268 | title, |
| 269 | baseUrl, |
| 270 | sessionIngressUrl, |
| 271 | workerType, |
| 272 | getAccessToken, |
| 273 | createSession, |
| 274 | archiveSession, |
| 275 | getCurrentTitle = () => title, |
| 276 | toSDKMessages = () => { |
| 277 | throw new Error( |
| 278 | 'BridgeCoreParams.toSDKMessages not provided. Pass it if you use writeMessages() or initialMessages — daemon callers that only use writeSdkMessages() never hit this path.', |
| 279 | ) |
| 280 | }, |
| 281 | onAuth401, |
| 282 | getPollIntervalConfig = () => DEFAULT_POLL_CONFIG, |
| 283 | initialHistoryCap = 200, |
| 284 | initialMessages, |
| 285 | previouslyFlushedUUIDs, |
| 286 | onInboundMessage, |
| 287 | onPermissionResponse, |
| 288 | onInterrupt, |
| 289 | onSetModel, |
| 290 | onSetMaxThinkingTokens, |
| 291 | onSetPermissionMode, |
| 292 | onStateChange, |
| 293 | onUserMessage, |
| 294 | perpetual, |
| 295 | initialSSESequenceNum = 0, |
| 296 | } = params |
| 297 | |
| 298 | const seq = ++initSequence |
| 299 | |
| 300 | // bridgePointer import hoisted: perpetual mode reads it before register; |
| 301 | // non-perpetual writes it after session create; both use clear at teardown. |
| 302 | const { writeBridgePointer, clearBridgePointer, readBridgePointer } = |
| 303 | await import('./bridgePointer.js') |
| 304 | |
| 305 | // Perpetual mode: read the crash-recovery pointer and treat it as prior |
| 306 | // state. The pointer is written unconditionally after session create |
| 307 | // (crash-recovery for all sessions); perpetual mode just skips the |
| 308 | // teardown clear so it survives clean exits too. Only reuse 'repl' |
| 309 | // pointers — a crashed standalone bridge (`claude remote-control`) |
| 310 | // writes source:'standalone' with a different workerType. |
| 311 | const rawPrior = perpetual ? await readBridgePointer(dir) : null |
| 312 | const prior = rawPrior?.source === 'repl' ? rawPrior : null |
| 313 | |
| 314 | logForDebugging( |
| 315 | `[bridge:repl] initBridgeCore #${seq} starting (initialMessages=${initialMessages?.length ?? 0}${prior ? ` perpetual prior=env:${prior.environmentId}` : ''})`, |
| 316 | ) |
| 317 |
no test coverage detected