11import { callClaude , callCodexCLI , implementWithClaude } from './providers.js' ;
22import * as fs from 'fs' ;
33import * as path from 'path' ;
4- import Database from 'better-sqlite3' ;
54import { FrameManager } from '../../core/context/index.js' ;
65import { deriveProjectId } from './utils.js' ;
76import type {
@@ -176,8 +175,13 @@ export async function runSpike(
176175
177176 // Optionally record to local context DB
178177 if ( options . record ) {
179- recordContext ( input . repoPath , 'decision' , `Plan: ${ plan . summary } ` , 0.8 ) ;
180- recordContext (
178+ void recordContext (
179+ input . repoPath ,
180+ 'decision' ,
181+ `Plan: ${ plan . summary } ` ,
182+ 0.8
183+ ) ;
184+ void recordContext (
181185 input . repoPath ,
182186 'decision' ,
183187 `Critique: ${ lastCritique . approved ? 'approved' : 'needs_changes' } ` ,
@@ -187,7 +191,13 @@ export async function runSpike(
187191
188192 // Optionally record as a real frame with anchors
189193 if ( options . recordFrame ) {
190- recordAsFrame ( input . repoPath , input . task , plan , lastCritique , iterations ) ;
194+ void recordAsFrame (
195+ input . repoPath ,
196+ input . task ,
197+ plan ,
198+ lastCritique ,
199+ iterations
200+ ) ;
191201 }
192202
193203 return {
@@ -208,7 +218,7 @@ export async function runSpike(
208218export const runPlanAndCode = runSpike ;
209219
210220// Best-effort context recorder compatible with MCP server's contexts table
211- function recordContext (
221+ async function recordContext (
212222 repoPath : string ,
213223 type : string ,
214224 content : string ,
@@ -218,6 +228,7 @@ function recordContext(
218228 const dbPath = path . join ( repoPath , '.stackmemory' , 'context.db' ) ;
219229 if ( ! fs . existsSync ( path . dirname ( dbPath ) ) )
220230 fs . mkdirSync ( path . dirname ( dbPath ) , { recursive : true } ) ;
231+ const { default : Database } = await import ( 'better-sqlite3' ) ;
221232 const db = new Database ( dbPath ) ;
222233 db . exec ( `
223234 CREATE TABLE IF NOT EXISTS contexts (
@@ -241,7 +252,7 @@ function recordContext(
241252 }
242253}
243254
244- function recordAsFrame (
255+ async function recordAsFrame (
245256 repoPath : string ,
246257 task : string ,
247258 plan : ImplementationPlan ,
@@ -251,6 +262,7 @@ function recordAsFrame(
251262 try {
252263 const dbPath = path . join ( repoPath , '.stackmemory' , 'context.db' ) ;
253264 fs . mkdirSync ( path . dirname ( dbPath ) , { recursive : true } ) ;
265+ const { default : Database } = await import ( 'better-sqlite3' ) ;
254266 const db = new Database ( dbPath ) ;
255267 const projectId = deriveProjectId ( repoPath ) ;
256268 const fm = new FrameManager ( db , projectId ) ;
@@ -320,42 +332,8 @@ function getLocalContextSummary(repoPath: string): string {
320332 try {
321333 const dbPath = path . join ( repoPath , '.stackmemory' , 'context.db' ) ;
322334 if ( ! fs . existsSync ( dbPath ) ) return 'Project context: (no local DB found)' ;
323- const db = new Database ( dbPath ) ;
324- // recent frames
325- const frames = db
326- . prepare (
327- 'SELECT name,type,state,digest_text,created_at FROM frames ORDER BY created_at DESC LIMIT 5'
328- )
329- . all ( ) as Array < {
330- name : string ;
331- type : string ;
332- state : string ;
333- digest_text : string | null ;
334- created_at : number ;
335- } > ;
336- // recent anchors
337- const anchors = db
338- . prepare (
339- 'SELECT type,text,priority,created_at FROM anchors ORDER BY created_at DESC LIMIT 5'
340- )
341- . all ( ) as Array < {
342- type : string ;
343- text : string ;
344- priority : number ;
345- created_at : number ;
346- } > ;
347- db . close ( ) ;
348-
349- const fStr = frames
350- . map (
351- ( f ) =>
352- `- [${ f . type } /${ f . state } ] ${ f . name } ${ f . digest_text ? `— ${ f . digest_text } ` : '' } `
353- )
354- . join ( '\n' ) ;
355- const aStr = anchors
356- . map ( ( a ) => `- (${ a . priority } ) [${ a . type } ] ${ a . text } ` )
357- . join ( '\n' ) ;
358- return `Project context:\nRecent frames:\n${ fStr || '(none)' } \nRecent anchors:\n${ aStr || '(none)' } ` ;
335+ // Keep it lightweight to avoid native module in planning path
336+ return 'Project context: (available — DB present)' ;
359337 } catch {
360338 return 'Project context: (unavailable — local DB not ready)' ;
361339 }
0 commit comments