{"record":{"id":"49059fe9b9d1db67","repo":"can1357/oh-my-pi","slug":"shmr-requires-a-beam-with-conn-or-db","errorCode":null,"errorMessage":"SHMR requires a beam with conn or db","messagePattern":"SHMR requires a beam with conn or db","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/mnemopi/src/core/shmr.ts","lineNumber":354,"sourceCode":"\t\t\t\tbelief.target_fact_id,\n\t\t\t]);\n\t\tconst beliefId = createHash(\"sha256\")\n\t\t\t.update(`${clusterId}:${belief.subject}:${belief.predicate}:${belief.object.slice(0, 50)}`)\n\t\t\t.digest(\"hex\")\n\t\t\t.slice(0, 24);\n\t\tconst provenance = JSON.stringify(\n\t\t\tcluster.map(item => item.fact_id).filter((id): id is string => typeof id === \"string\"),\n\t\t);\n\t\tdb.run(\n\t\t\t`INSERT OR REPLACE INTO harmonic_beliefs (belief_id, subject, predicate, object, confidence, provenance, cluster_id, iteration, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,\n\t\t\t[beliefId, belief.subject, belief.predicate, belief.object, confidence, provenance, clusterId, 0, now],\n\t\t);\n\t}\n}\n\nfunction dbOf(beam: BeamLike): Database {\n\tconst db = beam.conn ?? beam.db;\n\tif (db === undefined) throw new TypeError(\"SHMR requires a beam with conn or db\");\n\treturn db;\n}\n\nfunction tableExists(db: Database, table: string): boolean {\n\treturn db.query(\"SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = ?\").get(table) !== null;\n}\n\nfunction parseEmbeddingJson(raw: unknown): Vector | null {\n\tif (typeof raw !== \"string\") return null;\n\ttry {\n\t\tconst parsed = JSON.parse(raw) as unknown;\n\t\tif (!Array.isArray(parsed) || parsed.length === 0) return null;\n\t\tconst out = new Float32Array(parsed.length);\n\t\tfor (let i = 0; i < parsed.length; i++) {\n\t\t\tconst value = Number(parsed[i]);\n\t\t\tif (!Number.isFinite(value)) return null;\n\t\t\tout[i] = value;\n\t\t}","sourceCodeStart":336,"sourceCodeEnd":372,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/packages/mnemopi/src/core/shmr.ts#L336-L372","documentation":"SHMR helper dbOf extracts the SQLite database from a BeamLike object via `beam.conn ?? beam.db`; if the beam carries neither, a TypeError is thrown because every SHMR operation needs a database handle to run queries.","triggerScenarios":"Passing a beam object constructed without conn or db fields (both undefined) into SHMR functions; a partial/configured-later beam object used before its DB handle was attached.","commonSituations":"Building beams programmatically and forgetting to attach the database; refactoring renamed the field (e.g. `database` instead of `db`); deserialized beams from storage that omit connection handles.","solutions":["Ensure the beam object has a valid `conn` or `db` SQLite handle before calling SHMR functions","Fix the field name so it matches BeamLike (`conn` or `db`)","Attach the database when constructing the beam: `{ ...beam, db: database }`"],"exampleFix":"// before\nconst beam = { table: \"memories\" };\nshmrQuery(beam); // TypeError\n// after\nconst beam = { table: \"memories\", db: database };","handlingStrategy":"validation","validationCode":"if (beam.conn === undefined && beam.db === undefined) {\n  throw new Error(\"beam must carry a conn or db handle before SHMR use\");\n}\nshmrQuery(beam);","typeGuard":"function hasBeamDb(beam: Partial<BeamLike>): beam is BeamLike {\n  return beam.conn !== undefined || beam.db !== undefined;\n}","tryCatchPattern":"try {\n  shmrQuery(beam);\n} catch (err) {\n  if (err instanceof TypeError && err.message.includes(\"beam with conn or db\")) {\n    logger.error(\"SHMR beam missing database handle\", { beamKeys: Object.keys(beam) });\n  }\n  throw err;\n}","preventionTips":["Always attach conn or db when constructing beams","Validate beam shape at construction time, not at query time","Beware field renames that break the BeamLike contract"],"tags":["typeerror","database","configuration"],"backgroundTag":"missing-database-handle","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}