zam-core 0.3.3 → 0.3.5
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.
- package/{.gemini → .agent}/skills/zam/SKILL.md +361 -335
- package/.agents/skills/zam/SKILL.md +364 -0
- package/.claude/skills/zam/SKILL.md +331 -331
- package/LICENSE +199 -199
- package/README.de.md +127 -86
- package/README.md +147 -86
- package/dist/cli/index.js +6367 -2467
- package/dist/cli/index.js.map +1 -1
- package/dist/index.d.ts +823 -547
- package/dist/index.js +2738 -1322
- package/dist/index.js.map +1 -1
- package/package.json +57 -56
package/dist/cli/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/cli/index.ts","../../src/cli/commands/init.ts","../../src/kernel/db/connection.ts","../../src/kernel/db/schema.ts","../../src/kernel/models/token.ts","../../src/kernel/models/prerequisite.ts","../../src/kernel/models/card.ts","../../src/kernel/models/review.ts","../../src/kernel/models/session.ts","../../src/kernel/models/agent-skill.ts","../../src/kernel/models/settings.ts","../../src/kernel/scheduler/fsrs.ts","../../src/kernel/scheduler/blocker.ts","../../src/kernel/scheduler/interleaver.ts","../../src/kernel/scheduler/queue.ts","../../src/kernel/recall/prompter.ts","../../src/kernel/recall/evaluator.ts","../../src/kernel/analytics/stats.ts","../../src/kernel/observation/analyzer.ts","../../src/kernel/observation/monitor-io.ts","../../src/kernel/observation/shell-hooks.ts","../../src/kernel/observation/skill-discovery.ts","../../src/kernel/goals/engine.ts","../../src/kernel/goals/parser.ts","../../src/kernel/connectors/azure-devops.ts","../../src/cli/commands/setup.ts","../../src/cli/commands/token.ts","../../src/cli/commands/resolve-user.ts","../../src/cli/commands/card.ts","../../src/cli/commands/session.ts","../../src/cli/commands/stats.ts","../../src/cli/commands/review.ts","../../src/cli/commands/bridge.ts","../../src/cli/commands/skill.ts","../../src/cli/commands/monitor.ts","../../src/cli/commands/settings.ts","../../src/cli/commands/whoami.ts","../../src/cli/commands/connector.ts","../../src/cli/commands/goal.ts"],"sourcesContent":["import { Command } from \"commander\";\r\nimport { initCommand } from \"./commands/init.js\";\r\nimport { setupCommand } from \"./commands/setup.js\";\r\nimport { tokenCommand } from \"./commands/token.js\";\r\nimport { cardCommand } from \"./commands/card.js\";\r\nimport { sessionCommand } from \"./commands/session.js\";\r\nimport { statsCommand } from \"./commands/stats.js\";\r\nimport { reviewCommand } from \"./commands/review.js\";\r\nimport { bridgeCommand } from \"./commands/bridge.js\";\r\nimport { skillCommand } from \"./commands/skill.js\";\r\nimport { monitorCommand } from \"./commands/monitor.js\";\r\nimport { settingsCommand } from \"./commands/settings.js\";\r\nimport { whoamiCommand } from \"./commands/whoami.js\";\r\nimport { connectorCommand } from \"./commands/connector.js\";\r\nimport { goalCommand } from \"./commands/goal.js\";\r\n\r\nconst program = new Command();\r\n\r\nprogram\r\n .name(\"zam\")\r\n .description(\r\n \"The Symbiotic Learning Kernel: Elevating Human Intelligence through AI Collaboration.\",\r\n )\r\n .version(\"0.3.0\");\r\n\r\nprogram.addCommand(initCommand);\r\nprogram.addCommand(setupCommand);\r\nprogram.addCommand(tokenCommand);\r\nprogram.addCommand(cardCommand);\r\nprogram.addCommand(sessionCommand);\r\nprogram.addCommand(statsCommand);\r\nprogram.addCommand(reviewCommand);\r\nprogram.addCommand(bridgeCommand);\r\nprogram.addCommand(skillCommand);\r\nprogram.addCommand(monitorCommand);\r\nprogram.addCommand(settingsCommand);\r\nprogram.addCommand(whoamiCommand);\r\nprogram.addCommand(connectorCommand);\r\nprogram.addCommand(goalCommand);\r\n\r\nprogram.parse();\r\n","/**\r\n * `zam init` — Initialize ~/.zam/ directory and database.\r\n */\r\n\r\nimport { Command } from \"commander\";\r\nimport { openDatabase, getDefaultDbPath } from \"../../kernel/index.js\";\r\n\r\nexport const initCommand = new Command(\"init\")\r\n .description(\"Initialize the ZAM database and config directory\")\r\n .action(() => {\r\n try {\r\n const dbPath = getDefaultDbPath();\r\n const db = openDatabase({ initialize: true });\r\n db.close();\r\n console.log(`Initialized ZAM database at ${dbPath}`);\r\n } catch (err) {\r\n console.error(\"Failed to initialize:\", (err as Error).message);\r\n process.exit(1);\r\n }\r\n });\r\n","import Database, { type Database as DatabaseType } from \"libsql\";\r\nimport { existsSync, mkdirSync, rmSync } from \"node:fs\";\r\nimport { homedir } from \"node:os\";\r\nimport { dirname, join } from \"node:path\";\r\nimport { SCHEMA } from \"./schema.js\";\r\n\r\nconst DEFAULT_DB_DIR = join(homedir(), \".zam\");\r\nconst DEFAULT_DB_PATH = join(DEFAULT_DB_DIR, \"zam.db\");\r\n\r\nexport interface ConnectionOptions {\r\n /** Path to the SQLite database file. Defaults to ~/.zam/zam.db */\r\n dbPath?: string;\r\n /** If true, create the directory and run schema migrations on open */\r\n initialize?: boolean;\r\n /** Turso sync URL for cloud replication (e.g. libsql://db-name.turso.io) */\r\n syncUrl?: string;\r\n /** Turso auth token for cloud replication */\r\n authToken?: string;\r\n}\r\n\r\n/**\r\n * Open (or create) the ZAM database.\r\n * Uses WAL mode for concurrent access from AI CLI and user CLI.\r\n * When syncUrl is provided, enables embedded replica sync with Turso.\r\n */\r\nexport function openDatabase(options: ConnectionOptions = {}): DatabaseType {\r\n const dbPath = options.dbPath ?? DEFAULT_DB_PATH;\r\n\r\n if (options.initialize) {\r\n const dir = dirname(dbPath);\r\n if (!existsSync(dir)) {\r\n mkdirSync(dir, { recursive: true });\r\n }\r\n }\r\n\r\n // Build constructor options for libsql\r\n const dbOpts: Record<string, unknown> = {};\r\n if (options.syncUrl) {\r\n dbOpts.syncUrl = options.syncUrl;\r\n }\r\n if (options.authToken) {\r\n dbOpts.authToken = options.authToken;\r\n }\r\n\r\n let db: DatabaseType;\r\n try {\r\n db = new Database(dbPath, dbOpts as Database.Options);\r\n } catch (err) {\r\n // libsql throws \"InvalidLocalState\" when the db file exists as plain SQLite\r\n // but has no sync metadata (.meta file). This happens when Turso is configured\r\n // after the db was created locally. Delete the stale file and retry.\r\n if (options.syncUrl && (err as Error).message?.includes(\"InvalidLocalState\")) {\r\n for (const suffix of [\"\", \"-wal\", \"-shm\"]) {\r\n const f = `${dbPath}${suffix}`;\r\n if (existsSync(f)) rmSync(f, { force: true });\r\n }\r\n db = new Database(dbPath, dbOpts as Database.Options);\r\n } else {\r\n throw err;\r\n }\r\n }\r\n\r\n // Enable WAL mode and foreign keys\r\n db.pragma(\"journal_mode = WAL\");\r\n db.pragma(\"foreign_keys = ON\");\r\n db.pragma(\"busy_timeout = 5000\");\r\n\r\n if (options.initialize) {\r\n db.exec(SCHEMA);\r\n }\r\n\r\n runMigrations(db);\r\n\r\n // Sync after migrations if cloud is configured\r\n if (options.syncUrl) {\r\n (db as unknown as { sync: () => void }).sync();\r\n }\r\n\r\n return db;\r\n}\r\n\r\n/**\r\n * Open the database with Turso cloud sync auto-detected from stored settings.\r\n * Reads turso.url and turso.token from user_config. If present, reopens\r\n * the database with embedded replica sync enabled.\r\n */\r\nexport function openDatabaseWithSync(options: Omit<ConnectionOptions, \"syncUrl\" | \"authToken\"> = {}): DatabaseType {\r\n // First open locally to read settings\r\n const db = openDatabase(options);\r\n const syncUrl = db.prepare(\"SELECT value FROM user_config WHERE key = ?\").get(\"turso.url\") as { value: string } | undefined;\r\n const authToken = db.prepare(\"SELECT value FROM user_config WHERE key = ?\").get(\"turso.token\") as { value: string } | undefined;\r\n\r\n if (!syncUrl || !authToken) return db;\r\n\r\n // Checkpoint WAL and switch to DELETE journal mode before closing.\r\n // On Windows, WAL mode holds auxiliary file locks (.wal, .shm) that prevent\r\n // deletion even after close(). Switching to DELETE mode releases them cleanly.\r\n db.pragma(\"wal_checkpoint(TRUNCATE)\");\r\n db.pragma(\"journal_mode = DELETE\");\r\n db.close();\r\n\r\n return openDatabase({ ...options, syncUrl: syncUrl.value, authToken: authToken.value });\r\n}\r\n\r\n/** Get the default database path */\r\nexport function getDefaultDbPath(): string {\r\n return DEFAULT_DB_PATH;\r\n}\r\n\r\n/**\r\n * Run incremental schema migrations on every open.\r\n * Each migration is idempotent — safe to run repeatedly.\r\n */\r\nfunction runMigrations(db: DatabaseType): void {\r\n // M001: add execution_context to sessions\r\n const sessionCols = db.pragma(\"table_info(sessions)\") as Array<{ name: string }>;\r\n if (sessionCols.length > 0 && !sessionCols.some((c) => c.name === \"execution_context\")) {\r\n db.exec(\r\n `ALTER TABLE sessions ADD COLUMN execution_context TEXT NOT NULL DEFAULT 'shell'`,\r\n );\r\n }\r\n\r\n // M002: add deprecated_at to tokens\r\n const tokenCols = db.pragma(\"table_info(tokens)\") as Array<{ name: string }>;\r\n if (tokenCols.length > 0 && !tokenCols.some((c) => c.name === \"deprecated_at\")) {\r\n db.exec(`ALTER TABLE tokens ADD COLUMN deprecated_at TEXT`);\r\n }\r\n\r\n // M003: create agent_skills table (idempotent via IF NOT EXISTS in SCHEMA,\r\n // but also needed for databases that skipped the init path)\r\n db.exec(`\r\n CREATE TABLE IF NOT EXISTS agent_skills (\r\n id TEXT PRIMARY KEY,\r\n slug TEXT NOT NULL UNIQUE,\r\n description TEXT NOT NULL,\r\n steps TEXT NOT NULL DEFAULT '[]',\r\n token_slugs TEXT NOT NULL DEFAULT '[]',\r\n source TEXT NOT NULL DEFAULT 'learned',\r\n created_at TEXT NOT NULL DEFAULT (datetime('now')),\r\n updated_at TEXT NOT NULL DEFAULT (datetime('now'))\r\n )\r\n `);\r\n}\r\n","/**\r\n * ZAM Learning Kernel — SQLite Schema\r\n *\r\n * Evolves the PoC's schema with:\r\n * - FSRS scheduling fields (replaces SM-2's ef/interval_days)\r\n * - Bloom taxonomy levels on tokens\r\n * - Symbiosis modes (shadowing/copilot/autonomy)\r\n * - ULID-based IDs\r\n * - Immutable review log\r\n */\r\n\r\nexport const SCHEMA = `\r\n-- Use WAL mode for concurrent reads (AI CLI + user CLI)\r\nPRAGMA journal_mode = WAL;\r\nPRAGMA foreign_keys = ON;\r\n\r\n-- Knowledge tokens: atomic concepts/facts with Bloom levels\r\nCREATE TABLE IF NOT EXISTS tokens (\r\n id TEXT PRIMARY KEY,\r\n slug TEXT UNIQUE NOT NULL,\r\n concept TEXT NOT NULL,\r\n domain TEXT NOT NULL DEFAULT '',\r\n bloom_level INTEGER NOT NULL DEFAULT 1 CHECK (bloom_level BETWEEN 1 AND 5),\r\n context TEXT NOT NULL DEFAULT '',\r\n symbiosis_mode TEXT CHECK (symbiosis_mode IN ('shadowing', 'copilot', 'autonomy')),\r\n created_at TEXT NOT NULL DEFAULT (datetime('now')),\r\n updated_at TEXT NOT NULL DEFAULT (datetime('now')),\r\n deprecated_at TEXT\r\n);\r\n\r\n-- Prerequisite dependency graph: \"to learn A, first know B\"\r\nCREATE TABLE IF NOT EXISTS prerequisites (\r\n token_id TEXT NOT NULL REFERENCES tokens(id) ON DELETE CASCADE,\r\n requires_id TEXT NOT NULL REFERENCES tokens(id) ON DELETE CASCADE,\r\n PRIMARY KEY (token_id, requires_id)\r\n);\r\n\r\n-- Per-user scheduling state for each token (FSRS fields)\r\nCREATE TABLE IF NOT EXISTS cards (\r\n id TEXT PRIMARY KEY,\r\n token_id TEXT NOT NULL REFERENCES tokens(id) ON DELETE CASCADE,\r\n user_id TEXT NOT NULL,\r\n stability REAL NOT NULL DEFAULT 0.0,\r\n difficulty REAL NOT NULL DEFAULT 0.5,\r\n elapsed_days REAL NOT NULL DEFAULT 0.0,\r\n scheduled_days REAL NOT NULL DEFAULT 0.0,\r\n reps INTEGER NOT NULL DEFAULT 0,\r\n lapses INTEGER NOT NULL DEFAULT 0,\r\n state TEXT NOT NULL DEFAULT 'new' CHECK (state IN ('new', 'learning', 'review', 'relearning')),\r\n due_at TEXT NOT NULL DEFAULT (datetime('now')),\r\n last_review_at TEXT,\r\n blocked INTEGER NOT NULL DEFAULT 0,\r\n UNIQUE(token_id, user_id)\r\n);\r\n\r\n-- Immutable review log: every rating event\r\nCREATE TABLE IF NOT EXISTS review_logs (\r\n id TEXT PRIMARY KEY,\r\n card_id TEXT NOT NULL REFERENCES cards(id) ON DELETE CASCADE,\r\n token_id TEXT NOT NULL REFERENCES tokens(id) ON DELETE CASCADE,\r\n user_id TEXT NOT NULL,\r\n rating INTEGER NOT NULL CHECK (rating BETWEEN 1 AND 4),\r\n response_time_ms INTEGER,\r\n reviewed_at TEXT NOT NULL DEFAULT (datetime('now')),\r\n scheduled_at TEXT NOT NULL,\r\n session_id TEXT REFERENCES sessions(id)\r\n);\r\n\r\n-- Work+learning sessions\r\nCREATE TABLE IF NOT EXISTS sessions (\r\n id TEXT PRIMARY KEY,\r\n user_id TEXT NOT NULL,\r\n task TEXT NOT NULL,\r\n started_at TEXT NOT NULL DEFAULT (datetime('now')),\r\n completed_at TEXT\r\n);\r\n\r\n-- Steps within a session: who did what\r\nCREATE TABLE IF NOT EXISTS session_steps (\r\n id TEXT PRIMARY KEY,\r\n session_id TEXT NOT NULL REFERENCES sessions(id) ON DELETE CASCADE,\r\n token_id TEXT NOT NULL REFERENCES tokens(id) ON DELETE CASCADE,\r\n done_by TEXT NOT NULL CHECK (done_by IN ('user', 'agent')),\r\n rating INTEGER CHECK (rating BETWEEN 1 AND 4),\r\n notes TEXT,\r\n created_at TEXT NOT NULL DEFAULT (datetime('now'))\r\n);\r\n\r\n-- User configuration\r\nCREATE TABLE IF NOT EXISTS user_config (\r\n key TEXT PRIMARY KEY,\r\n value TEXT NOT NULL,\r\n updated_at TEXT NOT NULL DEFAULT (datetime('now'))\r\n);\r\n\r\n-- Agent skills: task recipes the agent learns from user guidance\r\nCREATE TABLE IF NOT EXISTS agent_skills (\r\n id TEXT PRIMARY KEY,\r\n slug TEXT NOT NULL UNIQUE,\r\n description TEXT NOT NULL,\r\n steps TEXT NOT NULL DEFAULT '[]', -- JSON array of step strings\r\n token_slugs TEXT NOT NULL DEFAULT '[]', -- JSON array of related token slugs\r\n source TEXT NOT NULL DEFAULT 'learned'\r\n CHECK(source IN ('learned', 'builtin')),\r\n created_at TEXT NOT NULL DEFAULT (datetime('now')),\r\n updated_at TEXT NOT NULL DEFAULT (datetime('now'))\r\n);\r\n\r\n-- Performance indexes\r\nCREATE INDEX IF NOT EXISTS idx_tokens_domain ON tokens(domain);\r\nCREATE INDEX IF NOT EXISTS idx_tokens_slug ON tokens(slug);\r\nCREATE INDEX IF NOT EXISTS idx_prereqs_token ON prerequisites(token_id);\r\nCREATE INDEX IF NOT EXISTS idx_prereqs_requires ON prerequisites(requires_id);\r\nCREATE INDEX IF NOT EXISTS idx_cards_user_due ON cards(user_id, blocked, due_at);\r\nCREATE INDEX IF NOT EXISTS idx_cards_token_user ON cards(token_id, user_id);\r\nCREATE INDEX IF NOT EXISTS idx_review_logs_card ON review_logs(card_id);\r\nCREATE INDEX IF NOT EXISTS idx_review_logs_user ON review_logs(user_id, reviewed_at);\r\nCREATE INDEX IF NOT EXISTS idx_session_steps_session ON session_steps(session_id);\r\n`;\r\n","/**\r\n * Token repository — typed wrappers around the tokens table.\r\n *\r\n * Tokens are atomic knowledge concepts with Bloom taxonomy levels\r\n * and optional symbiosis modes (shadowing / copilot / autonomy).\r\n */\r\n\r\nimport type { Database } from \"libsql\";\r\nimport { ulid } from \"ulid\";\r\n\r\n// ── Types ────────────────────────────────────────────────────────────────────\r\n\r\nexport type BloomLevel = 1 | 2 | 3 | 4 | 5;\r\n\r\nexport type SymbiosisMode = \"shadowing\" | \"copilot\" | \"autonomy\";\r\n\r\nexport interface Token {\r\n id: string;\r\n slug: string;\r\n concept: string;\r\n domain: string;\r\n bloom_level: BloomLevel;\r\n context: string;\r\n symbiosis_mode: SymbiosisMode | null;\r\n created_at: string;\r\n updated_at: string;\r\n deprecated_at: string | null;\r\n}\r\n\r\nexport interface CreateTokenInput {\r\n slug: string;\r\n concept: string;\r\n domain?: string;\r\n bloom_level?: BloomLevel;\r\n context?: string;\r\n symbiosis_mode?: SymbiosisMode | null;\r\n}\r\n\r\nexport interface ListTokensOptions {\r\n domain?: string;\r\n}\r\n\r\n// ── Scored result from fuzzy search ──────────────────────────────────────────\r\n\r\nexport interface ScoredToken extends Token {\r\n score: number;\r\n}\r\n\r\n// ── Functions ────────────────────────────────────────────────────────────────\r\n\r\n/**\r\n * Create a new knowledge token.\r\n * Throws if a token with the same slug already exists.\r\n */\r\nexport function createToken(db: Database, input: CreateTokenInput): Token {\r\n const id = ulid();\r\n const now = new Date().toISOString();\r\n\r\n const bloom = input.bloom_level ?? 1;\r\n if (bloom < 1 || bloom > 5) {\r\n throw new Error(`bloom_level must be between 1 and 5, got ${bloom}`);\r\n }\r\n\r\n db.prepare(`\r\n INSERT INTO tokens (id, slug, concept, domain, bloom_level, context, symbiosis_mode, created_at, updated_at)\r\n VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)\r\n `).run(\r\n id,\r\n input.slug,\r\n input.concept,\r\n input.domain ?? \"\",\r\n bloom,\r\n input.context ?? \"\",\r\n input.symbiosis_mode ?? null,\r\n now,\r\n now,\r\n );\r\n\r\n return getTokenById(db, id)!;\r\n}\r\n\r\n/**\r\n * Look up a token by its unique slug.\r\n * Returns undefined if not found.\r\n */\r\nexport function getTokenBySlug(db: Database, slug: string): Token | undefined {\r\n return db.prepare(\"SELECT * FROM tokens WHERE slug = ?\").get(slug) as Token | undefined;\r\n}\r\n\r\n/**\r\n * Look up a token by its ULID.\r\n * Returns undefined if not found.\r\n */\r\nexport function getTokenById(db: Database, id: string): Token | undefined {\r\n return db.prepare(\"SELECT * FROM tokens WHERE id = ?\").get(id) as Token | undefined;\r\n}\r\n\r\n/**\r\n * Mark a token as deprecated. Deprecated tokens are excluded from review queues\r\n * and search results but are not deleted — they can still be consulted.\r\n *\r\n * Throws if the token does not exist or is already deprecated.\r\n */\r\nexport function deprecateToken(db: Database, slug: string): Token {\r\n const token = getTokenBySlug(db, slug);\r\n if (!token) {\r\n throw new Error(`Token not found: ${slug}`);\r\n }\r\n if (token.deprecated_at) {\r\n throw new Error(`Token already deprecated: ${slug}`);\r\n }\r\n\r\n const now = new Date().toISOString();\r\n db.prepare(\"UPDATE tokens SET deprecated_at = ?, updated_at = ? WHERE slug = ?\").run(\r\n now,\r\n now,\r\n slug,\r\n );\r\n\r\n return getTokenBySlug(db, slug)!;\r\n}\r\n\r\n/**\r\n * Fuzzy search for tokens by keyword query.\r\n *\r\n * Ported from the PoC's find-token command: splits the query into word\r\n * tokens, scores each database token by word overlap plus a substring\r\n * bonus on the concept field, and returns all matches sorted by score\r\n * descending.\r\n */\r\nexport function findTokens(db: Database, query: string): ScoredToken[] {\r\n const normalised = query.toLowerCase();\r\n const qTokens = new Set(\r\n normalised\r\n .split(/[\\s,.\\-_/\\\\:;!?()\\[\\]{}]+/)\r\n .filter((t) => t.length > 2),\r\n );\r\n\r\n const tokens = db\r\n .prepare(\"SELECT * FROM tokens WHERE deprecated_at IS NULL\")\r\n .all() as Token[];\r\n\r\n const scored: ScoredToken[] = [];\r\n\r\n for (const t of tokens) {\r\n const words = (t.slug + \" \" + t.concept + \" \" + t.domain)\r\n .toLowerCase()\r\n .split(/[\\s,.\\-_/\\\\:;!?()\\[\\]{}]+/)\r\n .filter(Boolean);\r\n\r\n let score = 0;\r\n for (const w of words) {\r\n if (qTokens.has(w)) score++;\r\n }\r\n\r\n // Substring bonus: if the concept contains the start of the query\r\n if (t.concept.toLowerCase().includes(normalised.slice(0, 25))) {\r\n score += 3;\r\n }\r\n\r\n if (score > 0) {\r\n scored.push({ score, ...t });\r\n }\r\n }\r\n\r\n scored.sort((a, b) => b.score - a.score);\r\n return scored;\r\n}\r\n\r\n/**\r\n * List all tokens, optionally filtered by domain.\r\n * Results are ordered by bloom_level then slug.\r\n */\r\nexport function listTokens(db: Database, options?: ListTokensOptions): Token[] {\r\n if (options?.domain) {\r\n return db\r\n .prepare(\r\n \"SELECT * FROM tokens WHERE domain = ? AND deprecated_at IS NULL ORDER BY bloom_level, slug\",\r\n )\r\n .all(options.domain) as Token[];\r\n }\r\n return db\r\n .prepare(\r\n \"SELECT * FROM tokens WHERE deprecated_at IS NULL ORDER BY bloom_level, domain, slug\",\r\n )\r\n .all() as Token[];\r\n}\r\n","/**\r\n * Prerequisite repository — typed wrappers around the prerequisites table.\r\n *\r\n * Models the dependency graph: \"to learn token A, first know token B.\"\r\n */\r\n\r\nimport type { Database } from \"libsql\";\r\n\r\n// ── Types ────────────────────────────────────────────────────────────────────\r\n\r\nexport interface Prerequisite {\r\n token_id: string;\r\n requires_id: string;\r\n}\r\n\r\n/** A prerequisite row joined with the token it points to. */\r\nexport interface PrerequisiteWithToken extends Prerequisite {\r\n slug: string;\r\n concept: string;\r\n domain: string;\r\n bloom_level: number;\r\n}\r\n\r\n// ── Functions ────────────────────────────────────────────────────────────────\r\n\r\n/**\r\n * Add a prerequisite edge: tokenId requires requiresId.\r\n *\r\n * Idempotent — silently ignores duplicate edges.\r\n * Throws if either token ID does not exist (FK constraint).\r\n * Throws if a token is declared as its own prerequisite.\r\n */\r\nexport function addPrerequisite(\r\n db: Database,\r\n tokenId: string,\r\n requiresId: string,\r\n): void {\r\n if (tokenId === requiresId) {\r\n throw new Error(\"A token cannot be a prerequisite of itself\");\r\n }\r\n\r\n db.prepare(\r\n \"INSERT OR IGNORE INTO prerequisites (token_id, requires_id) VALUES (?, ?)\",\r\n ).run(tokenId, requiresId);\r\n}\r\n\r\n/**\r\n * Get the direct prerequisites of a token — \"what does token X require?\"\r\n *\r\n * Returns prerequisite rows joined with the required token's details.\r\n */\r\nexport function getPrerequisites(\r\n db: Database,\r\n tokenId: string,\r\n): PrerequisiteWithToken[] {\r\n return db\r\n .prepare(\r\n `SELECT p.token_id, p.requires_id, t.slug, t.concept, t.domain, t.bloom_level\r\n FROM prerequisites p\r\n JOIN tokens t ON t.id = p.requires_id\r\n WHERE p.token_id = ?`,\r\n )\r\n .all(tokenId) as PrerequisiteWithToken[];\r\n}\r\n\r\n/**\r\n * Get the direct dependents of a token — \"what depends on token X?\"\r\n *\r\n * Returns prerequisite rows joined with the dependent token's details.\r\n */\r\nexport function getDependents(\r\n db: Database,\r\n tokenId: string,\r\n): PrerequisiteWithToken[] {\r\n return db\r\n .prepare(\r\n `SELECT p.token_id, p.requires_id, t.slug, t.concept, t.domain, t.bloom_level\r\n FROM prerequisites p\r\n JOIN tokens t ON t.id = p.token_id\r\n WHERE p.requires_id = ?`,\r\n )\r\n .all(tokenId) as PrerequisiteWithToken[];\r\n}\r\n","/**\r\n * Card repository — typed wrappers around the cards table.\r\n *\r\n * Each card tracks one user's scheduling state for one token,\r\n * using FSRS fields (stability, difficulty, elapsed_days, etc.).\r\n */\r\n\r\nimport type { Database } from \"libsql\";\r\nimport { ulid } from \"ulid\";\r\n\r\n// ── Types ────────────────────────────────────────────────────────────────────\r\n\r\nexport type CardState = \"new\" | \"learning\" | \"review\" | \"relearning\";\r\n\r\nexport interface Card {\r\n id: string;\r\n token_id: string;\r\n user_id: string;\r\n stability: number;\r\n difficulty: number;\r\n elapsed_days: number;\r\n scheduled_days: number;\r\n reps: number;\r\n lapses: number;\r\n state: CardState;\r\n due_at: string;\r\n last_review_at: string | null;\r\n blocked: number; // 0 or 1\r\n}\r\n\r\nexport interface UpdateCardInput {\r\n stability?: number;\r\n difficulty?: number;\r\n elapsed_days?: number;\r\n scheduled_days?: number;\r\n reps?: number;\r\n lapses?: number;\r\n state?: CardState;\r\n due_at?: string;\r\n last_review_at?: string | null;\r\n blocked?: number;\r\n}\r\n\r\n/** A due card joined with its token details. */\r\nexport interface DueCard extends Card {\r\n slug: string;\r\n concept: string;\r\n domain: string;\r\n bloom_level: number;\r\n}\r\n\r\n/** A blocked card joined with its token details. */\r\nexport interface BlockedCard extends Card {\r\n slug: string;\r\n concept: string;\r\n domain: string;\r\n bloom_level: number;\r\n}\r\n\r\n// ── Functions ────────────────────────────────────────────────────────────────\r\n\r\n/**\r\n * Ensure a card exists for the given token+user pair.\r\n *\r\n * If one already exists, return it. Otherwise create a new card with\r\n * default FSRS values (due immediately) and return it.\r\n *\r\n * Ported from the PoC's ensureCard helper.\r\n */\r\nexport function ensureCard(\r\n db: Database,\r\n tokenId: string,\r\n userId: string,\r\n): Card {\r\n const existing = db\r\n .prepare(\"SELECT * FROM cards WHERE token_id = ? AND user_id = ?\")\r\n .get(tokenId, userId) as Card | undefined;\r\n\r\n if (existing) return existing;\r\n\r\n const id = ulid();\r\n const now = new Date().toISOString();\r\n\r\n db.prepare(\r\n `INSERT INTO cards (id, token_id, user_id, due_at)\r\n VALUES (?, ?, ?, ?)`,\r\n ).run(id, tokenId, userId, now);\r\n\r\n return db\r\n .prepare(\"SELECT * FROM cards WHERE id = ?\")\r\n .get(id) as Card;\r\n}\r\n\r\n/**\r\n * Get a card by token+user. Returns undefined if no card exists.\r\n */\r\nexport function getCard(\r\n db: Database,\r\n tokenId: string,\r\n userId: string,\r\n): Card | undefined {\r\n return db\r\n .prepare(\"SELECT * FROM cards WHERE token_id = ? AND user_id = ?\")\r\n .get(tokenId, userId) as Card | undefined;\r\n}\r\n\r\n/**\r\n * Update a card's scheduling fields.\r\n *\r\n * Only the fields present in `updates` are changed. Throws if the card\r\n * does not exist.\r\n */\r\nexport function updateCard(\r\n db: Database,\r\n cardId: string,\r\n updates: UpdateCardInput,\r\n): Card {\r\n const fields: string[] = [];\r\n const values: unknown[] = [];\r\n\r\n if (updates.stability !== undefined) {\r\n fields.push(\"stability = ?\");\r\n values.push(updates.stability);\r\n }\r\n if (updates.difficulty !== undefined) {\r\n fields.push(\"difficulty = ?\");\r\n values.push(updates.difficulty);\r\n }\r\n if (updates.elapsed_days !== undefined) {\r\n fields.push(\"elapsed_days = ?\");\r\n values.push(updates.elapsed_days);\r\n }\r\n if (updates.scheduled_days !== undefined) {\r\n fields.push(\"scheduled_days = ?\");\r\n values.push(updates.scheduled_days);\r\n }\r\n if (updates.reps !== undefined) {\r\n fields.push(\"reps = ?\");\r\n values.push(updates.reps);\r\n }\r\n if (updates.lapses !== undefined) {\r\n fields.push(\"lapses = ?\");\r\n values.push(updates.lapses);\r\n }\r\n if (updates.state !== undefined) {\r\n fields.push(\"state = ?\");\r\n values.push(updates.state);\r\n }\r\n if (updates.due_at !== undefined) {\r\n fields.push(\"due_at = ?\");\r\n values.push(updates.due_at);\r\n }\r\n if (updates.last_review_at !== undefined) {\r\n fields.push(\"last_review_at = ?\");\r\n values.push(updates.last_review_at);\r\n }\r\n if (updates.blocked !== undefined) {\r\n fields.push(\"blocked = ?\");\r\n values.push(updates.blocked);\r\n }\r\n\r\n if (fields.length === 0) {\r\n throw new Error(\"updateCard called with no fields to update\");\r\n }\r\n\r\n values.push(cardId);\r\n\r\n const result = db\r\n .prepare(`UPDATE cards SET ${fields.join(\", \")} WHERE id = ?`)\r\n .run(...values);\r\n\r\n if (result.changes === 0) {\r\n throw new Error(`Card not found: ${cardId}`);\r\n }\r\n\r\n return db.prepare(\"SELECT * FROM cards WHERE id = ?\").get(cardId) as Card;\r\n}\r\n\r\n/**\r\n * Get all cards that are due for review.\r\n *\r\n * A card is due when it is not blocked and due_at <= now.\r\n * Results are ordered by bloom_level ascending (fundamentals first),\r\n * then by due_at ascending (oldest first).\r\n *\r\n * Ported from the PoC's due-tokens command.\r\n */\r\nexport function getDueCards(\r\n db: Database,\r\n userId: string,\r\n now?: string,\r\n): DueCard[] {\r\n const cutoff = now ?? new Date().toISOString();\r\n\r\n return db\r\n .prepare(\r\n `SELECT c.*, t.slug, t.concept, t.domain, t.bloom_level\r\n FROM cards c\r\n JOIN tokens t ON t.id = c.token_id\r\n WHERE c.user_id = ? AND c.blocked = 0 AND c.due_at <= ?\r\n ORDER BY t.bloom_level ASC, c.due_at ASC`,\r\n )\r\n .all(userId, cutoff) as DueCard[];\r\n}\r\n\r\n/**\r\n * Get all blocked cards for a user.\r\n *\r\n * Returns cards joined with their token details so the caller can\r\n * see what is waiting and why.\r\n */\r\nexport function getBlockedCards(\r\n db: Database,\r\n userId: string,\r\n): BlockedCard[] {\r\n return db\r\n .prepare(\r\n `SELECT c.*, t.slug, t.concept, t.domain, t.bloom_level\r\n FROM cards c\r\n JOIN tokens t ON t.id = c.token_id\r\n WHERE c.user_id = ? AND c.blocked = 1\r\n ORDER BY t.bloom_level ASC, t.slug ASC`,\r\n )\r\n .all(userId) as BlockedCard[];\r\n}\r\n","/**\r\n * Review log repository — typed wrappers around the review_logs table.\r\n *\r\n * The review log is immutable: every rating event is appended, never\r\n * updated or deleted. This provides a complete audit trail of a user's\r\n * learning history.\r\n */\r\n\r\nimport type { Database } from \"libsql\";\r\nimport { ulid } from \"ulid\";\r\n\r\n// ── Types ────────────────────────────────────────────────────────────────────\r\n\r\nexport interface ReviewLog {\r\n id: string;\r\n card_id: string;\r\n token_id: string;\r\n user_id: string;\r\n rating: number; // 1-4\r\n response_time_ms: number | null;\r\n reviewed_at: string;\r\n scheduled_at: string;\r\n session_id: string | null;\r\n}\r\n\r\nexport interface CreateReviewInput {\r\n card_id: string;\r\n token_id: string;\r\n user_id: string;\r\n rating: number; // 1-4\r\n scheduled_at: string;\r\n response_time_ms?: number | null;\r\n session_id?: string | null;\r\n}\r\n\r\nexport interface ListReviewsOptions {\r\n /** Maximum number of reviews to return. */\r\n limit?: number;\r\n /** Return reviews after this ISO timestamp. */\r\n after?: string;\r\n /** Return reviews before this ISO timestamp. */\r\n before?: string;\r\n}\r\n\r\n// ── Functions ────────────────────────────────────────────────────────────────\r\n\r\n/**\r\n * Log an immutable review event.\r\n *\r\n * Validates that the rating is between 1 and 4 (matching the schema CHECK).\r\n * Returns the created review log entry.\r\n */\r\nexport function logReview(db: Database, input: CreateReviewInput): ReviewLog {\r\n if (input.rating < 1 || input.rating > 4) {\r\n throw new Error(`Rating must be between 1 and 4, got ${input.rating}`);\r\n }\r\n\r\n const id = ulid();\r\n const now = new Date().toISOString();\r\n\r\n db.prepare(\r\n `INSERT INTO review_logs (id, card_id, token_id, user_id, rating, response_time_ms, reviewed_at, scheduled_at, session_id)\r\n VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,\r\n ).run(\r\n id,\r\n input.card_id,\r\n input.token_id,\r\n input.user_id,\r\n input.rating,\r\n input.response_time_ms ?? null,\r\n now,\r\n input.scheduled_at,\r\n input.session_id ?? null,\r\n );\r\n\r\n return db.prepare(\"SELECT * FROM review_logs WHERE id = ?\").get(id) as ReviewLog;\r\n}\r\n\r\n/**\r\n * Get all reviews for a specific card, ordered by reviewed_at ascending.\r\n */\r\nexport function getReviewsForCard(db: Database, cardId: string): ReviewLog[] {\r\n return db\r\n .prepare(\r\n \"SELECT * FROM review_logs WHERE card_id = ? ORDER BY reviewed_at ASC\",\r\n )\r\n .all(cardId) as ReviewLog[];\r\n}\r\n\r\n/**\r\n * Get reviews for a user, with optional filtering.\r\n *\r\n * Results are ordered by reviewed_at descending (most recent first).\r\n */\r\nexport function getReviewsForUser(\r\n db: Database,\r\n userId: string,\r\n options?: ListReviewsOptions,\r\n): ReviewLog[] {\r\n const conditions = [\"user_id = ?\"];\r\n const params: unknown[] = [userId];\r\n\r\n if (options?.after) {\r\n conditions.push(\"reviewed_at > ?\");\r\n params.push(options.after);\r\n }\r\n if (options?.before) {\r\n conditions.push(\"reviewed_at < ?\");\r\n params.push(options.before);\r\n }\r\n\r\n let sql = `SELECT * FROM review_logs WHERE ${conditions.join(\" AND \")} ORDER BY reviewed_at DESC`;\r\n\r\n if (options?.limit) {\r\n sql += \" LIMIT ?\";\r\n params.push(options.limit);\r\n }\r\n\r\n return db.prepare(sql).all(...params) as ReviewLog[];\r\n}\r\n","/**\r\n * Session repository — typed wrappers around sessions and session_steps.\r\n *\r\n * A session represents a work+learning episode. Steps within a session\r\n * record which tokens were touched and by whom (user or agent).\r\n */\r\n\r\nimport type { Database } from \"libsql\";\r\nimport { ulid } from \"ulid\";\r\n\r\n// ── Types ────────────────────────────────────────────────────────────────────\r\n\r\nexport type ExecutionContext = \"shell\" | \"ui\" | \"reallife\";\r\n\r\nexport interface Session {\r\n id: string;\r\n user_id: string;\r\n task: string;\r\n execution_context: ExecutionContext;\r\n started_at: string;\r\n completed_at: string | null;\r\n}\r\n\r\nexport interface SessionStep {\r\n id: string;\r\n session_id: string;\r\n token_id: string;\r\n done_by: \"user\" | \"agent\";\r\n rating: number | null; // 1-4 or null\r\n notes: string | null;\r\n created_at: string;\r\n}\r\n\r\nexport interface CreateSessionInput {\r\n user_id: string;\r\n task: string;\r\n execution_context?: ExecutionContext;\r\n}\r\n\r\nexport interface LogStepInput {\r\n session_id: string;\r\n token_id: string;\r\n done_by: \"user\" | \"agent\";\r\n rating?: number | null;\r\n notes?: string | null;\r\n}\r\n\r\n/** A step joined with its token details, returned by getSessionSummary. */\r\nexport interface StepWithToken extends SessionStep {\r\n slug: string;\r\n concept: string;\r\n domain: string;\r\n bloom_level: number;\r\n}\r\n\r\nexport interface SessionSummary {\r\n session: Session;\r\n steps: StepWithToken[];\r\n}\r\n\r\n// ── Functions ────────────────────────────────────────────────────────────────\r\n\r\n/**\r\n * Start a new session. Returns the created session.\r\n *\r\n * Ported from the PoC's start-session command.\r\n */\r\nexport function startSession(db: Database, input: CreateSessionInput): Session {\r\n const id = ulid();\r\n const now = new Date().toISOString();\r\n const ctx = input.execution_context ?? \"shell\";\r\n\r\n db.prepare(\r\n `INSERT INTO sessions (id, user_id, task, execution_context, started_at)\r\n VALUES (?, ?, ?, ?, ?)`,\r\n ).run(id, input.user_id, input.task, ctx, now);\r\n\r\n return db.prepare(\"SELECT * FROM sessions WHERE id = ?\").get(id) as Session;\r\n}\r\n\r\n/**\r\n * End a session by setting its completed_at timestamp.\r\n *\r\n * Throws if the session does not exist or is already completed.\r\n *\r\n * Ported from the PoC's end-session command.\r\n */\r\nexport function endSession(db: Database, sessionId: string): Session {\r\n const session = db.prepare(\"SELECT * FROM sessions WHERE id = ?\").get(sessionId) as\r\n | Session\r\n | undefined;\r\n\r\n if (!session) {\r\n throw new Error(`Session not found: ${sessionId}`);\r\n }\r\n if (session.completed_at) {\r\n throw new Error(`Session already completed: ${sessionId}`);\r\n }\r\n\r\n const now = new Date().toISOString();\r\n db.prepare(\"UPDATE sessions SET completed_at = ? WHERE id = ?\").run(now, sessionId);\r\n\r\n return db.prepare(\"SELECT * FROM sessions WHERE id = ?\").get(sessionId) as Session;\r\n}\r\n\r\n/**\r\n * Log a step within a session.\r\n *\r\n * Validates that done_by is 'user' or 'agent' and that the rating\r\n * (if provided) is between 1 and 4.\r\n *\r\n * Ported from the PoC's log-step command.\r\n */\r\nexport function logStep(db: Database, input: LogStepInput): SessionStep {\r\n if (input.done_by !== \"user\" && input.done_by !== \"agent\") {\r\n throw new Error(`done_by must be 'user' or 'agent', got '${input.done_by}'`);\r\n }\r\n if (input.rating != null && (input.rating < 1 || input.rating > 4)) {\r\n throw new Error(`Rating must be between 1 and 4, got ${input.rating}`);\r\n }\r\n\r\n // Verify the session exists\r\n const session = db.prepare(\"SELECT id FROM sessions WHERE id = ?\").get(input.session_id);\r\n if (!session) {\r\n throw new Error(`Session not found: ${input.session_id}`);\r\n }\r\n\r\n const id = ulid();\r\n const now = new Date().toISOString();\r\n\r\n db.prepare(\r\n `INSERT INTO session_steps (id, session_id, token_id, done_by, rating, notes, created_at)\r\n VALUES (?, ?, ?, ?, ?, ?, ?)`,\r\n ).run(\r\n id,\r\n input.session_id,\r\n input.token_id,\r\n input.done_by,\r\n input.rating ?? null,\r\n input.notes ?? null,\r\n now,\r\n );\r\n\r\n return db.prepare(\"SELECT * FROM session_steps WHERE id = ?\").get(id) as SessionStep;\r\n}\r\n\r\n/**\r\n * Get a full session summary: the session record plus all steps\r\n * joined with their token details.\r\n *\r\n * Ported from the PoC's session-summary command.\r\n * Throws if the session does not exist.\r\n */\r\nexport function getSessionSummary(\r\n db: Database,\r\n sessionId: string,\r\n): SessionSummary {\r\n const session = db\r\n .prepare(\"SELECT * FROM sessions WHERE id = ?\")\r\n .get(sessionId) as Session | undefined;\r\n\r\n if (!session) {\r\n throw new Error(`Session not found: ${sessionId}`);\r\n }\r\n\r\n const steps = db\r\n .prepare(\r\n `SELECT ss.*, t.slug, t.concept, t.domain, t.bloom_level\r\n FROM session_steps ss\r\n JOIN tokens t ON t.id = ss.token_id\r\n WHERE ss.session_id = ?\r\n ORDER BY ss.created_at ASC`,\r\n )\r\n .all(sessionId) as StepWithToken[];\r\n\r\n return { session, steps };\r\n}\r\n","/**\r\n * Agent skills: task recipes the agent learns from user guidance.\r\n *\r\n * When the agent cannot execute a step, it admits it, asks for guidance,\r\n * and saves the successful approach here. Skills are linked to tokens so\r\n * FSRS decay naturally resurfaces them for review — automation ≠ retention.\r\n */\r\n\r\nimport type { Database } from \"libsql\";\r\nimport { ulid } from \"ulid\";\r\n\r\n// ── Types ────────────────────────────────────────────────────────────────────\r\n\r\nexport type SkillSource = \"learned\" | \"builtin\";\r\n\r\nexport interface AgentSkill {\r\n id: string;\r\n slug: string;\r\n description: string;\r\n steps: string[]; // parsed from JSON\r\n token_slugs: string[]; // parsed from JSON\r\n source: SkillSource;\r\n created_at: string;\r\n updated_at: string;\r\n}\r\n\r\n/** Raw DB row — steps and token_slugs are stored as JSON strings */\r\ninterface AgentSkillRow {\r\n id: string;\r\n slug: string;\r\n description: string;\r\n steps: string;\r\n token_slugs: string;\r\n source: SkillSource;\r\n created_at: string;\r\n updated_at: string;\r\n}\r\n\r\nexport interface CreateAgentSkillInput {\r\n slug: string;\r\n description: string;\r\n steps: string[];\r\n token_slugs?: string[];\r\n source?: SkillSource;\r\n}\r\n\r\n// ── Helpers ──────────────────────────────────────────────────────────────────\r\n\r\nfunction parseRow(row: AgentSkillRow): AgentSkill {\r\n return {\r\n ...row,\r\n steps: JSON.parse(row.steps) as string[],\r\n token_slugs: JSON.parse(row.token_slugs) as string[],\r\n };\r\n}\r\n\r\n// ── Functions ────────────────────────────────────────────────────────────────\r\n\r\nexport function createAgentSkill(\r\n db: Database,\r\n input: CreateAgentSkillInput,\r\n): AgentSkill {\r\n const existing = db\r\n .prepare(\"SELECT * FROM agent_skills WHERE slug = ?\")\r\n .get(input.slug) as AgentSkillRow | undefined;\r\n\r\n if (existing) {\r\n throw new Error(`Agent skill already exists: ${input.slug}`);\r\n }\r\n\r\n const id = ulid();\r\n const now = new Date().toISOString();\r\n\r\n db.prepare(\r\n `INSERT INTO agent_skills (id, slug, description, steps, token_slugs, source, created_at, updated_at)\r\n VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,\r\n ).run(\r\n id,\r\n input.slug,\r\n input.description,\r\n JSON.stringify(input.steps),\r\n JSON.stringify(input.token_slugs ?? []),\r\n input.source ?? \"learned\",\r\n now,\r\n now,\r\n );\r\n\r\n return parseRow(\r\n db.prepare(\"SELECT * FROM agent_skills WHERE id = ?\").get(id) as AgentSkillRow,\r\n );\r\n}\r\n\r\nexport function getAgentSkill(\r\n db: Database,\r\n slug: string,\r\n): AgentSkill | undefined {\r\n const row = db\r\n .prepare(\"SELECT * FROM agent_skills WHERE slug = ?\")\r\n .get(slug) as AgentSkillRow | undefined;\r\n\r\n return row ? parseRow(row) : undefined;\r\n}\r\n\r\nexport function listAgentSkills(db: Database): AgentSkill[] {\r\n const rows = db\r\n .prepare(\"SELECT * FROM agent_skills ORDER BY created_at ASC\")\r\n .all() as AgentSkillRow[];\r\n\r\n return rows.map(parseRow);\r\n}\r\n","/**\r\n * User settings — key/value store backed by the user_config table.\r\n */\r\n\r\nimport type { Database } from \"libsql\";\r\n\r\nexport interface UserSetting {\r\n key: string;\r\n value: string;\r\n updated_at: string;\r\n}\r\n\r\n/** Get a single setting by key. Returns undefined if not set. */\r\nexport function getSetting(db: Database, key: string): string | undefined {\r\n const row = db\r\n .prepare(\"SELECT value FROM user_config WHERE key = ?\")\r\n .get(key) as { value: string } | undefined;\r\n return row?.value;\r\n}\r\n\r\n/** Get all settings as a key-value map. */\r\nexport function getAllSettings(db: Database): Record<string, string> {\r\n const rows = db\r\n .prepare(\"SELECT key, value FROM user_config ORDER BY key\")\r\n .all() as { key: string; value: string }[];\r\n const map: Record<string, string> = {};\r\n for (const row of rows) {\r\n map[row.key] = row.value;\r\n }\r\n return map;\r\n}\r\n\r\n/** Get all settings with metadata. */\r\nexport function getAllSettingsDetailed(db: Database): UserSetting[] {\r\n return db\r\n .prepare(\"SELECT key, value, updated_at FROM user_config ORDER BY key\")\r\n .all() as UserSetting[];\r\n}\r\n\r\n/** Set a setting (insert or update). */\r\nexport function setSetting(db: Database, key: string, value: string): void {\r\n db.prepare(\r\n `INSERT INTO user_config (key, value, updated_at)\r\n VALUES (?, ?, datetime('now'))\r\n ON CONFLICT(key) DO UPDATE SET value = excluded.value, updated_at = excluded.updated_at`,\r\n ).run(key, value);\r\n}\r\n\r\n/** Delete a setting. Returns true if it existed. */\r\nexport function deleteSetting(db: Database, key: string): boolean {\r\n const result = db.prepare(\"DELETE FROM user_config WHERE key = ?\").run(key);\r\n return result.changes > 0;\r\n}\r\n","/**\r\n * FSRS-5 — Free Spaced Repetition Scheduler (v5)\r\n *\r\n * Pure-function implementation of the FSRS algorithm that replaces\r\n * the PoC's SM-2 scheduler. This is the mathematical heart of ZAM's\r\n * spaced-repetition engine.\r\n *\r\n * Reference: https://github.com/open-spaced-repetition/fsrs4anki/wiki/The-Algorithm\r\n */\r\n\r\n// ── Rating scale ────────────────────────────────────────────────────────────\r\n\r\n/** 1 = Again (forgot), 2 = Hard, 3 = Good, 4 = Easy */\r\nexport type Rating = 1 | 2 | 3 | 4;\r\n\r\n// ── Card states ─────────────────────────────────────────────────────────────\r\n\r\nexport type CardState = \"new\" | \"learning\" | \"review\" | \"relearning\";\r\n\r\n// ── Scheduling card ─────────────────────────────────────────────────────────\r\n\r\nexport interface SchedulingCard {\r\n /** Memory stability in days — expected half-life of recall probability. */\r\n stability: number;\r\n /** Intrinsic difficulty on a 1–10 scale. */\r\n difficulty: number;\r\n /** Days elapsed since the last review. */\r\n elapsedDays: number;\r\n /** Currently scheduled interval in days. */\r\n scheduledDays: number;\r\n /** Count of successful consecutive reviews. */\r\n reps: number;\r\n /** Times the card was forgotten (rated Again). */\r\n lapses: number;\r\n /** Current learning state. */\r\n state: CardState;\r\n /** When the card is next due. */\r\n dueAt: Date;\r\n /** When the card was last reviewed (null for new cards). */\r\n lastReviewAt: Date | null;\r\n}\r\n\r\n// ── Parameters ──────────────────────────────────────────────────────────────\r\n\r\nexport interface FSRSParameters {\r\n /** 19 optimised weight parameters (w0 – w18). */\r\n w: number[];\r\n /** Target retention rate, e.g. 0.9 means we aim for 90% recall. */\r\n requestRetention: number;\r\n}\r\n\r\n// ── FSRS-5 default weights ──────────────────────────────────────────────────\r\n\r\nconst DEFAULT_W: number[] = [\r\n 0.4072, 1.1829, 3.1262, 15.4722, // w0–w3: initial stability per rating\r\n 7.2102, 0.5316, 1.0651, // w4–w6: difficulty\r\n 0.0092, 1.5988, 0.1176, 1.0014, // w7–w10: stability after forgetting\r\n 2.0032, 0.0266, 0.3077, 0.15, // w11–w14: stability increase\r\n 0.0, 2.7849, 0.3477, 0.6831, // w15–w18: additional parameters\r\n];\r\n\r\nconst DEFAULT_REQUEST_RETENTION = 0.9;\r\n\r\n// ── FSRS object returned by the factory ─────────────────────────────────────\r\n\r\nexport interface FSRS {\r\n /** Return a fully updated card after applying a rating. Pure function. */\r\n schedule(card: SchedulingCard, rating: Rating, now?: Date): SchedulingCard;\r\n /** The parameters baked into this instance. */\r\n readonly params: Readonly<FSRSParameters>;\r\n}\r\n\r\n// ── Helpers ─────────────────────────────────────────────────────────────────\r\n\r\n/** Clamp a number to [lo, hi]. */\r\nfunction clamp(value: number, lo: number, hi: number): number {\r\n return Math.min(hi, Math.max(lo, value));\r\n}\r\n\r\n/** Days between two Date objects (may be fractional). */\r\nfunction daysBetween(a: Date, b: Date): number {\r\n return (b.getTime() - a.getTime()) / (1000 * 60 * 60 * 24);\r\n}\r\n\r\n// ── Core FSRS-5 formulas ────────────────────────────────────────────────────\r\n\r\n/**\r\n * Initial stability for a brand-new card.\r\n * S₀ = w[rating - 1]\r\n */\r\nfunction initialStability(w: number[], rating: Rating): number {\r\n return w[rating - 1];\r\n}\r\n\r\n/**\r\n * Initial difficulty for a brand-new card.\r\n * D₀(rating) = w4 - exp(w5 * (rating - 1)) + 1\r\n * Clamped to [1, 10].\r\n */\r\nfunction initialDifficulty(w: number[], rating: Rating): number {\r\n return clamp(w[4] - Math.exp(w[5] * (rating - 1)) + 1, 1, 10);\r\n}\r\n\r\n/**\r\n * Updated difficulty after a review.\r\n * D' = w7 * D₀(3) + (1 - w7) * (D - w6 * (rating - 3))\r\n * Clamped to [1, 10].\r\n */\r\nfunction nextDifficulty(w: number[], d: number, rating: Rating): number {\r\n const d0ForGood = initialDifficulty(w, 3);\r\n const updated = w[7] * d0ForGood + (1 - w[7]) * (d - w[6] * (rating - 3));\r\n return clamp(updated, 1, 10);\r\n}\r\n\r\n/**\r\n * Retrievability — probability of recall.\r\n * R = (1 + elapsed / (9 * S))^(-1)\r\n */\r\nfunction retrievability(elapsed: number, stability: number): number {\r\n if (stability <= 0) return 0;\r\n return Math.pow(1 + elapsed / (9 * stability), -1);\r\n}\r\n\r\n/**\r\n * Stability after a **successful** recall (rating >= 2).\r\n * S' = S * (exp(w8) * (11 - D) * S^(-w9) * (exp(w10 * (1 - R)) - 1) * hard_penalty * easy_bonus + 1)\r\n */\r\nfunction stabilityAfterSuccess(\r\n w: number[],\r\n s: number,\r\n d: number,\r\n r: number,\r\n rating: Rating,\r\n): number {\r\n const hardPenalty = rating === 2 ? w[15] : 1;\r\n const easyBonus = rating === 4 ? w[16] : 1;\r\n\r\n const inner =\r\n Math.exp(w[8]) *\r\n (11 - d) *\r\n Math.pow(s, -w[9]) *\r\n (Math.exp(w[10] * (1 - r)) - 1) *\r\n hardPenalty *\r\n easyBonus;\r\n\r\n return s * (inner + 1);\r\n}\r\n\r\n/**\r\n * Stability after **forgetting** (rating = 1).\r\n * S' = w11 * D^(-w12) * ((S+1)^w13 - 1) * exp(w14 * (1 - R))\r\n */\r\nfunction stabilityAfterForgetting(\r\n w: number[],\r\n s: number,\r\n d: number,\r\n r: number,\r\n): number {\r\n return (\r\n w[11] *\r\n Math.pow(d, -w[12]) *\r\n (Math.pow(s + 1, w[13]) - 1) *\r\n Math.exp(w[14] * (1 - r))\r\n );\r\n}\r\n\r\n/**\r\n * Optimal interval given new stability and target retention.\r\n * I = 9 * S' * (1/requestRetention - 1)\r\n * Minimum 1 day.\r\n */\r\nfunction nextInterval(stability: number, requestRetention: number): number {\r\n const interval = 9 * stability * (1 / requestRetention - 1);\r\n return Math.max(1, Math.round(interval));\r\n}\r\n\r\n// ── Factory ─────────────────────────────────────────────────────────────────\r\n\r\n/**\r\n * Create a new card with default initial values.\r\n */\r\nexport function createEmptyCard(now?: Date): SchedulingCard {\r\n const ts = now ?? new Date();\r\n return {\r\n stability: 0,\r\n difficulty: 0,\r\n elapsedDays: 0,\r\n scheduledDays: 0,\r\n reps: 0,\r\n lapses: 0,\r\n state: \"new\",\r\n dueAt: ts,\r\n lastReviewAt: null,\r\n };\r\n}\r\n\r\n/**\r\n * Create an FSRS scheduler instance.\r\n *\r\n * All scheduling is done through pure functions — no side effects,\r\n * no database access, no mutation of the input card.\r\n */\r\nexport function createFSRS(params?: Partial<FSRSParameters>): FSRS {\r\n const resolvedParams: FSRSParameters = {\r\n w: params?.w ?? [...DEFAULT_W],\r\n requestRetention: params?.requestRetention ?? DEFAULT_REQUEST_RETENTION,\r\n };\r\n\r\n function schedule(\r\n card: SchedulingCard,\r\n rating: Rating,\r\n now?: Date,\r\n ): SchedulingCard {\r\n const reviewTime = now ?? new Date();\r\n const w = resolvedParams.w;\r\n\r\n // Compute elapsed days since last review (or 0 for new cards).\r\n const elapsed =\r\n card.lastReviewAt !== null\r\n ? Math.max(0, daysBetween(card.lastReviewAt, reviewTime))\r\n : 0;\r\n\r\n // ── New card ──────────────────────────────────────────────────────\r\n if (card.state === \"new\") {\r\n const s = initialStability(w, rating);\r\n const d = initialDifficulty(w, rating);\r\n const interval = nextInterval(s, resolvedParams.requestRetention);\r\n\r\n const dueAt = new Date(reviewTime);\r\n dueAt.setDate(dueAt.getDate() + interval);\r\n\r\n // New cards always move to \"learning\" after first rating.\r\n\r\n return {\r\n stability: s,\r\n difficulty: d,\r\n elapsedDays: 0,\r\n scheduledDays: interval,\r\n reps: rating >= 2 ? 1 : 0,\r\n lapses: rating === 1 ? 1 : 0,\r\n state: \"learning\",\r\n dueAt,\r\n lastReviewAt: reviewTime,\r\n };\r\n }\r\n\r\n // ── Existing card (learning / review / relearning) ───────────────\r\n\r\n const r = retrievability(elapsed, card.stability);\r\n\r\n let newStability: number;\r\n let newDifficulty: number;\r\n let newReps: number;\r\n let newLapses: number;\r\n let newState: CardState;\r\n\r\n if (rating === 1) {\r\n // Forgot — apply forgetting formula\r\n newStability = stabilityAfterForgetting(w, card.stability, card.difficulty, r);\r\n newDifficulty = nextDifficulty(w, card.difficulty, rating);\r\n newReps = 0;\r\n newLapses = card.lapses + 1;\r\n newState = \"relearning\";\r\n } else {\r\n // Recalled — apply success formula\r\n newStability = stabilityAfterSuccess(w, card.stability, card.difficulty, r, rating);\r\n newDifficulty = nextDifficulty(w, card.difficulty, rating);\r\n newReps = card.reps + 1;\r\n newLapses = card.lapses;\r\n // Successful recall transitions any state to review.\r\n newState = \"review\";\r\n }\r\n\r\n const interval = nextInterval(newStability, resolvedParams.requestRetention);\r\n\r\n const dueAt = new Date(reviewTime);\r\n dueAt.setDate(dueAt.getDate() + interval);\r\n\r\n return {\r\n stability: newStability,\r\n difficulty: newDifficulty,\r\n elapsedDays: elapsed,\r\n scheduledDays: interval,\r\n reps: newReps,\r\n lapses: newLapses,\r\n state: newState,\r\n dueAt,\r\n lastReviewAt: reviewTime,\r\n };\r\n }\r\n\r\n return {\r\n schedule,\r\n params: Object.freeze(resolvedParams),\r\n };\r\n}\r\n","/**\r\n * Cascade Block & Unblock — prerequisite-aware blocking logic.\r\n *\r\n * Ported from the PoC's cascade-block and unblock-ready commands.\r\n *\r\n * When a user rates a token as \"forgot\" (rating 1) and that token has\r\n * prerequisites, we block the token and surface its prerequisites into\r\n * the active deck. When all prerequisites are met, we unblock.\r\n */\r\n\r\nimport type { Database } from \"libsql\";\r\nimport { ensureCard } from \"../models/card.js\";\r\nimport { getTokenBySlug } from \"../models/token.js\";\r\nimport { getPrerequisites } from \"../models/prerequisite.js\";\r\n\r\n// ── Types ────────────────────────────────────────────────────────────────────\r\n\r\nexport interface CascadeBlockResult {\r\n blockedSlug: string;\r\n prerequisites: Array<{ slug: string; concept: string; bloomLevel: number }>;\r\n}\r\n\r\nexport interface UnblockResult {\r\n unblocked: Array<{ slug: string; concept: string }>;\r\n}\r\n\r\n// ── Functions ────────────────────────────────────────────────────────────────\r\n\r\n/**\r\n * Block a token and surface its prerequisites.\r\n *\r\n * Called when a user rates a token as \"forgot\" (rating 1). The token is\r\n * marked as blocked so it won't appear in review queues. All direct\r\n * prerequisites are ensured to have cards (unblocked, due now) so they\r\n * appear in the user's next review session.\r\n *\r\n * @param db - Database connection\r\n * @param userId - The user whose card to block\r\n * @param tokenSlug - Slug of the token the user forgot\r\n * @returns Info about what was blocked and which prerequisites were surfaced\r\n */\r\nexport function cascadeBlock(\r\n db: Database,\r\n userId: string,\r\n tokenSlug: string,\r\n): CascadeBlockResult {\r\n const token = getTokenBySlug(db, tokenSlug);\r\n if (!token) {\r\n throw new Error(`Unknown token slug: ${tokenSlug}`);\r\n }\r\n\r\n // Ensure a card exists, then block it\r\n ensureCard(db, token.id, userId);\r\n db.prepare(\r\n \"UPDATE cards SET blocked = 1 WHERE token_id = ? AND user_id = ?\",\r\n ).run(token.id, userId);\r\n\r\n // Surface all direct prerequisites — ensure cards exist (unblocked, due now)\r\n const prereqs = getPrerequisites(db, token.id);\r\n const surfaced: Array<{ slug: string; concept: string; bloomLevel: number }> = [];\r\n\r\n for (const prereq of prereqs) {\r\n // ensureCard creates a new card if missing (defaults: blocked=0, due_at=now)\r\n const card = ensureCard(db, prereq.requires_id, userId);\r\n\r\n // If the prerequisite card was somehow blocked with no prereqs of its own,\r\n // make sure it's unblocked and due now so it surfaces\r\n if (card.blocked === 1) {\r\n const prereqOfPrereq = db\r\n .prepare(\"SELECT COUNT(*) as n FROM prerequisites WHERE token_id = ?\")\r\n .get(prereq.requires_id) as { n: number };\r\n\r\n // Only force-unblock if it has no prerequisites of its own\r\n if (prereqOfPrereq.n === 0) {\r\n const now = new Date().toISOString();\r\n db.prepare(\r\n \"UPDATE cards SET blocked = 0, due_at = ? WHERE token_id = ? AND user_id = ?\",\r\n ).run(now, prereq.requires_id, userId);\r\n }\r\n }\r\n\r\n surfaced.push({\r\n slug: prereq.slug,\r\n concept: prereq.concept,\r\n bloomLevel: prereq.bloom_level,\r\n });\r\n }\r\n\r\n return {\r\n blockedSlug: tokenSlug,\r\n prerequisites: surfaced,\r\n };\r\n}\r\n\r\n/**\r\n * Scan all blocked cards for a user and unblock any whose prerequisites are met.\r\n *\r\n * A blocked card is ready to unblock when ALL of its direct prerequisites have:\r\n * - reps >= 1 (the user has successfully recalled it at least once)\r\n * - blocked = 0 (the prerequisite itself is not blocked)\r\n *\r\n * If a blocked card has no prerequisites at all, it is unblocked immediately\r\n * (it was likely blocked in error or its prerequisites were removed).\r\n *\r\n * @param db - Database connection\r\n * @param userId - The user whose blocked cards to check\r\n * @returns List of cards that were unblocked\r\n */\r\nexport function unblockReady(\r\n db: Database,\r\n userId: string,\r\n): UnblockResult {\r\n const blockedCards = db\r\n .prepare(\r\n `SELECT c.token_id, t.slug, t.concept\r\n FROM cards c\r\n JOIN tokens t ON t.id = c.token_id\r\n WHERE c.user_id = ? AND c.blocked = 1`,\r\n )\r\n .all(userId) as Array<{ token_id: string; slug: string; concept: string }>;\r\n\r\n const unblocked: Array<{ slug: string; concept: string }> = [];\r\n\r\n for (const card of blockedCards) {\r\n const totalPrereqs = db\r\n .prepare(\"SELECT COUNT(*) as n FROM prerequisites WHERE token_id = ?\")\r\n .get(card.token_id) as { n: number };\r\n\r\n const metPrereqs = db\r\n .prepare(\r\n `SELECT COUNT(*) as n FROM cards c\r\n JOIN prerequisites p ON p.requires_id = c.token_id\r\n WHERE p.token_id = ? AND c.user_id = ? AND c.reps >= 1 AND c.blocked = 0`,\r\n )\r\n .get(card.token_id, userId) as { n: number };\r\n\r\n if (totalPrereqs.n === 0 || metPrereqs.n === totalPrereqs.n) {\r\n const now = new Date().toISOString();\r\n db.prepare(\r\n \"UPDATE cards SET blocked = 0, due_at = ? WHERE token_id = ? AND user_id = ?\",\r\n ).run(now, card.token_id, userId);\r\n\r\n unblocked.push({ slug: card.slug, concept: card.concept });\r\n }\r\n }\r\n\r\n return { unblocked };\r\n}\r\n","/**\r\n * Cross-domain interleaving — prevents same-domain streaks in review queues.\r\n *\r\n * Given an array of items with a `domain` field, reorders them using a\r\n * round-robin strategy so that consecutive items come from different domains.\r\n * This leverages the interleaving effect: mixing topics during practice\r\n * strengthens discrimination and long-term retention.\r\n */\r\n\r\n// ── Types ────────────────────────────────────────────────────────────────────\r\n\r\nexport interface QueueItem {\r\n cardId: string;\r\n tokenId: string;\r\n domain: string;\r\n}\r\n\r\n// ── Functions ────────────────────────────────────────────────────────────────\r\n\r\n/**\r\n * Reorder items so no domain appears more than `maxConsecutive` times in a row.\r\n *\r\n * Algorithm: group items by domain, then round-robin across domain groups.\r\n * Each round picks one item from each non-exhausted domain. Within a domain,\r\n * the original order is preserved (so urgency sorting survives).\r\n *\r\n * If a domain has more items than others, its extras will appear after all\r\n * other domains are exhausted — but the `maxConsecutive` cap is still\r\n * respected by inserting items from the largest remaining domains first.\r\n *\r\n * @param items - Array of items to interleave. Not mutated.\r\n * @param maxConsecutive - Max consecutive items from the same domain. Defaults to 2.\r\n * @returns A new array with the same items in interleaved order.\r\n */\r\nexport function interleave<T extends { domain: string }>(\r\n items: T[],\r\n maxConsecutive: number = 2,\r\n): T[] {\r\n if (items.length <= 1) return [...items];\r\n\r\n // Group items by domain, preserving original order within each group\r\n const byDomain = new Map<string, T[]>();\r\n for (const item of items) {\r\n const group = byDomain.get(item.domain);\r\n if (group) {\r\n group.push(item);\r\n } else {\r\n byDomain.set(item.domain, [item]);\r\n }\r\n }\r\n\r\n // If there's only one domain, no interleaving possible\r\n if (byDomain.size === 1) return [...items];\r\n\r\n const result: T[] = [];\r\n let consecutiveCount = 0;\r\n let lastDomain: string | null = null;\r\n\r\n // Track how many items we've consumed from each domain\r\n const cursors = new Map<string, number>();\r\n for (const domain of byDomain.keys()) {\r\n cursors.set(domain, 0);\r\n }\r\n\r\n // Round-robin: sort domains by remaining count (largest first) each round\r\n while (result.length < items.length) {\r\n // Get domains that still have items, sorted by remaining count descending\r\n const activeDomains = [...byDomain.entries()]\r\n .filter(([domain]) => cursors.get(domain)! < byDomain.get(domain)!.length)\r\n .sort((a, b) => {\r\n const remainA = a[1].length - cursors.get(a[0])!;\r\n const remainB = b[1].length - cursors.get(b[0])!;\r\n return remainB - remainA;\r\n });\r\n\r\n if (activeDomains.length === 0) break;\r\n\r\n let pickedThisRound = false;\r\n\r\n for (const [domain, group] of activeDomains) {\r\n const cursor = cursors.get(domain)!;\r\n if (cursor >= group.length) continue;\r\n\r\n // Check if adding from this domain would exceed maxConsecutive\r\n if (domain === lastDomain && consecutiveCount >= maxConsecutive) {\r\n // Try to find another domain first\r\n continue;\r\n }\r\n\r\n // Pick one item from this domain\r\n result.push(group[cursor]);\r\n cursors.set(domain, cursor + 1);\r\n pickedThisRound = true;\r\n\r\n if (domain === lastDomain) {\r\n consecutiveCount++;\r\n } else {\r\n lastDomain = domain;\r\n consecutiveCount = 1;\r\n }\r\n\r\n break;\r\n }\r\n\r\n // If we couldn't pick without exceeding maxConsecutive, we must accept\r\n // a streak from whatever domain has items left\r\n if (!pickedThisRound) {\r\n for (const [domain, group] of activeDomains) {\r\n const cursor = cursors.get(domain)!;\r\n if (cursor >= group.length) continue;\r\n\r\n result.push(group[cursor]);\r\n cursors.set(domain, cursor + 1);\r\n\r\n if (domain === lastDomain) {\r\n consecutiveCount++;\r\n } else {\r\n lastDomain = domain;\r\n consecutiveCount = 1;\r\n }\r\n\r\n break;\r\n }\r\n }\r\n }\r\n\r\n return result;\r\n}\r\n","/**\r\n * Review Queue Builder — assembles a session's review queue.\r\n *\r\n * Combines due-card fetching, new-card selection, urgency sorting,\r\n * and cross-domain interleaving into a single ready-to-review queue.\r\n */\r\n\r\nimport type { Database } from \"libsql\";\r\nimport { interleave } from \"./interleaver.js\";\r\n\r\n// ── Types ────────────────────────────────────────────────────────────────────\r\n\r\nexport interface ReviewQueueOptions {\r\n userId: string;\r\n maxNew?: number; // default 10\r\n maxReviews?: number; // default 50\r\n now?: Date;\r\n}\r\n\r\nexport interface ReviewQueueItem {\r\n cardId: string;\r\n tokenId: string;\r\n slug: string;\r\n concept: string;\r\n domain: string;\r\n bloomLevel: number;\r\n state: string; // 'new' | 'learning' | 'review' | 'relearning'\r\n dueAt: string;\r\n}\r\n\r\nexport interface ReviewQueue {\r\n items: ReviewQueueItem[];\r\n newCount: number;\r\n reviewCount: number;\r\n relearnCount: number;\r\n totalDomains: string[];\r\n}\r\n\r\n// ── Internal row type from SQL queries ───────────────────────────────────────\r\n\r\ninterface CardRow {\r\n card_id: string;\r\n token_id: string;\r\n slug: string;\r\n concept: string;\r\n domain: string;\r\n bloom_level: number;\r\n state: string;\r\n due_at: string;\r\n}\r\n\r\n// ── Functions ────────────────────────────────────────────────────────────────\r\n\r\n/**\r\n * Build a review queue for a user's study session.\r\n *\r\n * The queue is assembled in stages:\r\n * 1. Fetch all due cards (not blocked, due_at <= now, state in review/relearning/learning)\r\n * 2. Fetch new cards (state = 'new', not blocked) up to maxNew\r\n * 3. Sort overdue cards by urgency — most overdue first\r\n * 4. Apply cross-domain interleaving to prevent same-domain streaks\r\n * 5. Intersperse new cards at regular intervals (every 5th position)\r\n * 6. Cap total at maxReviews\r\n *\r\n * @param db - Database connection\r\n * @param options - Queue building options\r\n * @returns The assembled review queue with metadata\r\n */\r\nexport function buildReviewQueue(\r\n db: Database,\r\n options: ReviewQueueOptions,\r\n): ReviewQueue {\r\n const maxNew = options.maxNew ?? 10;\r\n const maxReviews = options.maxReviews ?? 50;\r\n const now = options.now ?? new Date();\r\n const nowISO = now.toISOString();\r\n\r\n // ── Step 1: Fetch due cards (review, relearning, learning — not new) ───\r\n const dueRows = db\r\n .prepare(\r\n `SELECT\r\n c.id AS card_id,\r\n c.token_id AS token_id,\r\n t.slug AS slug,\r\n t.concept AS concept,\r\n t.domain AS domain,\r\n t.bloom_level AS bloom_level,\r\n c.state AS state,\r\n c.due_at AS due_at\r\n FROM cards c\r\n JOIN tokens t ON t.id = c.token_id\r\n WHERE c.user_id = ?\r\n AND c.blocked = 0\r\n AND c.due_at <= ?\r\n AND c.state IN ('review', 'relearning', 'learning')\r\n AND t.deprecated_at IS NULL\r\n ORDER BY c.due_at ASC`,\r\n )\r\n .all(options.userId, nowISO) as CardRow[];\r\n\r\n // ── Step 2: Fetch new cards ────────────────────────────────────────────\r\n const newRows = db\r\n .prepare(\r\n `SELECT\r\n c.id AS card_id,\r\n c.token_id AS token_id,\r\n t.slug AS slug,\r\n t.concept AS concept,\r\n t.domain AS domain,\r\n t.bloom_level AS bloom_level,\r\n c.state AS state,\r\n c.due_at AS due_at\r\n FROM cards c\r\n JOIN tokens t ON t.id = c.token_id\r\n WHERE c.user_id = ?\r\n AND c.blocked = 0\r\n AND c.state = 'new'\r\n AND t.deprecated_at IS NULL\r\n ORDER BY t.bloom_level ASC, t.slug ASC\r\n LIMIT ?`,\r\n )\r\n .all(options.userId, maxNew) as CardRow[];\r\n\r\n // ── Step 3: Sort overdue cards by urgency (most overdue first) ─────────\r\n const nowMs = now.getTime();\r\n const sortedDue = [...dueRows].sort((a, b) => {\r\n const overdueA = nowMs - new Date(a.due_at).getTime();\r\n const overdueB = nowMs - new Date(b.due_at).getTime();\r\n return overdueB - overdueA; // most overdue first\r\n });\r\n\r\n // ── Step 4: Apply cross-domain interleaving to due cards ───────────────\r\n const interleavedDue = interleave(\r\n sortedDue.map((row) => ({ ...rowToItem(row), domain: row.domain })),\r\n );\r\n\r\n // ── Step 5: Intersperse new cards at regular intervals ─────────────────\r\n const newItems = newRows.map(rowToItem);\r\n const merged = intersperseNew(interleavedDue, newItems, 5);\r\n\r\n // ── Step 6: Cap total at maxReviews ────────────────────────────────────\r\n const capped = merged.slice(0, maxReviews);\r\n\r\n // ── Compute metadata ──────────────────────────────────────────────────\r\n let newCount = 0;\r\n let reviewCount = 0;\r\n let relearnCount = 0;\r\n const domainSet = new Set<string>();\r\n\r\n for (const item of capped) {\r\n domainSet.add(item.domain);\r\n switch (item.state) {\r\n case \"new\":\r\n newCount++;\r\n break;\r\n case \"relearning\":\r\n relearnCount++;\r\n break;\r\n default:\r\n reviewCount++;\r\n break;\r\n }\r\n }\r\n\r\n return {\r\n items: capped,\r\n newCount,\r\n reviewCount,\r\n relearnCount,\r\n totalDomains: [...domainSet].sort(),\r\n };\r\n}\r\n\r\n// ── Helpers ──────────────────────────────────────────────────────────────────\r\n\r\n/** Convert a SQL row to a ReviewQueueItem. */\r\nfunction rowToItem(row: CardRow): ReviewQueueItem {\r\n return {\r\n cardId: row.card_id,\r\n tokenId: row.token_id,\r\n slug: row.slug,\r\n concept: row.concept,\r\n domain: row.domain,\r\n bloomLevel: row.bloom_level,\r\n state: row.state,\r\n dueAt: row.due_at,\r\n };\r\n}\r\n\r\n/**\r\n * Intersperse new cards into the review queue at regular intervals.\r\n *\r\n * Instead of front-loading or back-loading new cards, places one new card\r\n * every `interval` positions (e.g., positions 4, 9, 14, ...).\r\n * This gives the user a mix of familiar reviews and new material.\r\n *\r\n * @param reviews - The interleaved review cards\r\n * @param newCards - New cards to intersperse\r\n * @param interval - Place a new card every N positions (default 5)\r\n * @returns Merged array with new cards interspersed\r\n */\r\nfunction intersperseNew(\r\n reviews: ReviewQueueItem[],\r\n newCards: ReviewQueueItem[],\r\n interval: number,\r\n): ReviewQueueItem[] {\r\n if (newCards.length === 0) return [...reviews];\r\n if (reviews.length === 0) return [...newCards];\r\n\r\n const result: ReviewQueueItem[] = [];\r\n let reviewIdx = 0;\r\n let newIdx = 0;\r\n\r\n // Position counter tracks where we are in the final queue\r\n let position = 0;\r\n\r\n while (reviewIdx < reviews.length || newIdx < newCards.length) {\r\n // Insert a new card every `interval` positions (0-indexed: at 4, 9, 14, ...)\r\n if (\r\n newIdx < newCards.length &&\r\n position > 0 &&\r\n position % interval === interval - 1\r\n ) {\r\n result.push(newCards[newIdx]);\r\n newIdx++;\r\n } else if (reviewIdx < reviews.length) {\r\n result.push(reviews[reviewIdx]);\r\n reviewIdx++;\r\n } else if (newIdx < newCards.length) {\r\n // No more reviews — append remaining new cards\r\n result.push(newCards[newIdx]);\r\n newIdx++;\r\n }\r\n\r\n position++;\r\n }\r\n\r\n return result;\r\n}\r\n","/**\r\n * Active Recall Prompt Generation\r\n *\r\n * Generates review prompts from tokens, adapting the question style\r\n * to the token's Bloom taxonomy level. This is NOT an LLM call —\r\n * it's template-based prompt assembly for the CLI and bridge.\r\n */\r\n\r\nexport type BloomLevel = 1 | 2 | 3 | 4 | 5;\r\n\r\nexport interface RecallPrompt {\r\n cardId: string;\r\n tokenId: string;\r\n slug: string;\r\n question: string;\r\n concept: string;\r\n domain: string;\r\n bloomLevel: BloomLevel;\r\n bloomVerb: string;\r\n hints: string[];\r\n}\r\n\r\nconst BLOOM_VERBS: Record<BloomLevel, string> = {\r\n 1: \"Remember\",\r\n 2: \"Understand\",\r\n 3: \"Apply\",\r\n 4: \"Analyze\",\r\n 5: \"Synthesize\",\r\n};\r\n\r\nconst BLOOM_PROMPTS: Record<BloomLevel, (concept: string) => string> = {\r\n 1: (c) => `What is: ${c}?`,\r\n 2: (c) => `Explain how this works: ${c}`,\r\n 3: (c) => `Apply this concept: ${c}`,\r\n 4: (c) => `Analyze the trade-offs: ${c}`,\r\n 5: (c) => `Design a solution using: ${c}`,\r\n};\r\n\r\nexport interface PromptInput {\r\n cardId: string;\r\n tokenId: string;\r\n slug: string;\r\n concept: string;\r\n domain: string;\r\n bloomLevel: BloomLevel;\r\n}\r\n\r\n/**\r\n * Generate a recall prompt for a token at its Bloom level.\r\n * When called from the CLI, the prompt is rendered in the terminal.\r\n * When called from the AI bridge, the JSON is returned for the AI to present conversationally.\r\n */\r\nexport function generatePrompt(input: PromptInput): RecallPrompt {\r\n const bloom = (input.bloomLevel >= 1 && input.bloomLevel <= 5\r\n ? input.bloomLevel\r\n : 1) as BloomLevel;\r\n\r\n return {\r\n cardId: input.cardId,\r\n tokenId: input.tokenId,\r\n slug: input.slug,\r\n question: BLOOM_PROMPTS[bloom](input.concept),\r\n concept: input.concept,\r\n domain: input.domain,\r\n bloomLevel: bloom,\r\n bloomVerb: BLOOM_VERBS[bloom],\r\n hints: [],\r\n };\r\n}\r\n","/**\r\n * Rating Evaluator\r\n *\r\n * Processes a user's self-assessment rating after a recall attempt.\r\n * Coordinates between FSRS scheduling, review logging, and blocking.\r\n */\r\n\r\nimport type { Database } from \"libsql\";\r\nimport { ulid } from \"ulid\";\r\nimport { updateCard } from \"../models/card.js\";\r\nimport type { Rating, SchedulingCard } from \"../scheduler/fsrs.js\";\r\nimport { createFSRS } from \"../scheduler/fsrs.js\";\r\n\r\nexport interface EvaluateInput {\r\n cardId: string;\r\n tokenId: string;\r\n userId: string;\r\n rating: Rating;\r\n sessionId?: string;\r\n responseTimeMs?: number;\r\n}\r\n\r\nexport interface EvaluateResult {\r\n nextDueAt: string;\r\n stability: number;\r\n difficulty: number;\r\n state: string;\r\n scheduledDays: number;\r\n reps: number;\r\n lapses: number;\r\n}\r\n\r\n/**\r\n * Process a rating: update the card via FSRS, log the review.\r\n * Returns the updated scheduling state.\r\n *\r\n * Note: blocking logic (cascade-block) is handled separately by the caller\r\n * when rating === 1 and the token has prerequisites.\r\n */\r\nexport function evaluateRating(\r\n db: Database,\r\n input: EvaluateInput,\r\n): EvaluateResult {\r\n // Get current card state\r\n const card = db\r\n .prepare(\"SELECT * FROM cards WHERE id = ?\")\r\n .get(input.cardId) as {\r\n stability: number;\r\n difficulty: number;\r\n elapsed_days: number;\r\n scheduled_days: number;\r\n reps: number;\r\n lapses: number;\r\n state: string;\r\n due_at: string;\r\n last_review_at: string | null;\r\n } | undefined;\r\n\r\n if (!card) {\r\n throw new Error(`Card not found: ${input.cardId}`);\r\n }\r\n\r\n const now = new Date();\r\n const fsrs = createFSRS();\r\n\r\n // Build scheduling card from DB state\r\n const schedulingCard: SchedulingCard = {\r\n stability: card.stability,\r\n difficulty: card.difficulty,\r\n elapsedDays: card.elapsed_days,\r\n scheduledDays: card.scheduled_days,\r\n reps: card.reps,\r\n lapses: card.lapses,\r\n state: card.state as SchedulingCard[\"state\"],\r\n dueAt: new Date(card.due_at),\r\n lastReviewAt: card.last_review_at ? new Date(card.last_review_at) : null,\r\n };\r\n\r\n // Run FSRS\r\n const updated = fsrs.schedule(schedulingCard, input.rating, now);\r\n\r\n // Update the card in the DB\r\n updateCard(db, input.cardId, {\r\n stability: updated.stability,\r\n difficulty: updated.difficulty,\r\n elapsed_days: updated.elapsedDays,\r\n scheduled_days: updated.scheduledDays,\r\n reps: updated.reps,\r\n lapses: updated.lapses,\r\n state: updated.state,\r\n due_at: updated.dueAt.toISOString(),\r\n last_review_at: now.toISOString(),\r\n });\r\n\r\n // Log the review (immutable)\r\n db.prepare(\r\n `INSERT INTO review_logs (id, card_id, token_id, user_id, rating, response_time_ms, reviewed_at, scheduled_at, session_id)\r\n VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,\r\n ).run(\r\n ulid(),\r\n input.cardId,\r\n input.tokenId,\r\n input.userId,\r\n input.rating,\r\n input.responseTimeMs ?? null,\r\n now.toISOString(),\r\n card.due_at,\r\n input.sessionId ?? null,\r\n );\r\n\r\n return {\r\n nextDueAt: updated.dueAt.toISOString(),\r\n stability: updated.stability,\r\n difficulty: updated.difficulty,\r\n state: updated.state,\r\n scheduledDays: updated.scheduledDays,\r\n reps: updated.reps,\r\n lapses: updated.lapses,\r\n };\r\n}\r\n","/**\r\n * Learning Analytics\r\n *\r\n * Progress statistics, competence tracking, and session summaries.\r\n * Ported from PoC's `stats` command with additions for FSRS and symbiosis modes.\r\n */\r\n\r\nimport type { Database } from \"libsql\";\r\n\r\nexport interface UserStats {\r\n userId: string;\r\n totalTokens: number;\r\n cardsInDeck: number;\r\n dueToday: number;\r\n blocked: number;\r\n mature: number;\r\n avgStability: number | null;\r\n totalSessions: number;\r\n lastSession: string | null;\r\n}\r\n\r\nexport interface DomainCompetence {\r\n domain: string;\r\n totalCards: number;\r\n matureCards: number;\r\n avgStability: number;\r\n retentionRate: number;\r\n suggestedMode: \"shadowing\" | \"copilot\" | \"autonomy\";\r\n}\r\n\r\nfunction q(db: Database, sql: string, ...params: unknown[]) {\r\n return db.prepare(sql).get(...params) as Record<string, unknown>;\r\n}\r\n\r\n/**\r\n * Get overall learning stats for a user (ported from PoC's `stats` command).\r\n */\r\nexport function getUserStats(db: Database, userId: string): UserStats {\r\n return {\r\n userId,\r\n totalTokens: (q(db, \"SELECT COUNT(*) as n FROM tokens\") as { n: number }).n,\r\n cardsInDeck: (q(db, \"SELECT COUNT(*) as n FROM cards WHERE user_id = ?\", userId) as { n: number }).n,\r\n dueToday: (q(\r\n db,\r\n \"SELECT COUNT(*) as n FROM cards WHERE user_id = ? AND blocked = 0 AND due_at <= datetime('now')\",\r\n userId,\r\n ) as { n: number }).n,\r\n blocked: (q(db, \"SELECT COUNT(*) as n FROM cards WHERE user_id = ? AND blocked = 1\", userId) as { n: number }).n,\r\n mature: (q(\r\n db,\r\n \"SELECT COUNT(*) as n FROM cards WHERE user_id = ? AND reps >= 3 AND stability >= 21\",\r\n userId,\r\n ) as { n: number }).n,\r\n avgStability: (() => {\r\n const v = q(db, \"SELECT AVG(stability) as v FROM cards WHERE user_id = ? AND reps > 0\", userId) as { v: number | null };\r\n return v.v ? Math.round(v.v * 100) / 100 : null;\r\n })(),\r\n totalSessions: (q(db, \"SELECT COUNT(*) as n FROM sessions WHERE user_id = ?\", userId) as { n: number }).n,\r\n lastSession: (() => {\r\n const r = db\r\n .prepare(\r\n \"SELECT started_at FROM sessions WHERE user_id = ? ORDER BY started_at DESC LIMIT 1\",\r\n )\r\n .get(userId) as { started_at: string } | undefined;\r\n return r?.started_at ?? null;\r\n })(),\r\n };\r\n}\r\n\r\n/**\r\n * Get competence per domain for a user.\r\n * Used to suggest symbiosis mode transitions.\r\n */\r\nexport function getDomainCompetence(\r\n db: Database,\r\n userId: string,\r\n): DomainCompetence[] {\r\n const domains = db\r\n .prepare(\r\n `SELECT DISTINCT t.domain FROM cards c\r\n JOIN tokens t ON t.id = c.token_id\r\n WHERE c.user_id = ? AND t.domain != ''`,\r\n )\r\n .all(userId) as { domain: string }[];\r\n\r\n return domains.map((d) => {\r\n const total = (q(\r\n db,\r\n `SELECT COUNT(*) as n FROM cards c\r\n JOIN tokens t ON t.id = c.token_id\r\n WHERE c.user_id = ? AND t.domain = ?`,\r\n userId,\r\n d.domain,\r\n ) as { n: number }).n;\r\n\r\n const mature = (q(\r\n db,\r\n `SELECT COUNT(*) as n FROM cards c\r\n JOIN tokens t ON t.id = c.token_id\r\n WHERE c.user_id = ? AND t.domain = ? AND c.reps >= 3 AND c.stability >= 21`,\r\n userId,\r\n d.domain,\r\n ) as { n: number }).n;\r\n\r\n const avgStab = (q(\r\n db,\r\n `SELECT AVG(c.stability) as v FROM cards c\r\n JOIN tokens t ON t.id = c.token_id\r\n WHERE c.user_id = ? AND t.domain = ? AND c.reps > 0`,\r\n userId,\r\n d.domain,\r\n ) as { v: number | null }).v ?? 0;\r\n\r\n // Estimate retention from review history\r\n const reviews = q(\r\n db,\r\n `SELECT COUNT(*) as total,\r\n SUM(CASE WHEN rating >= 2 THEN 1 ELSE 0 END) as passed\r\n FROM review_logs\r\n WHERE user_id = ? AND token_id IN (SELECT id FROM tokens WHERE domain = ?)`,\r\n userId,\r\n d.domain,\r\n ) as { total: number; passed: number };\r\n\r\n const retentionRate =\r\n reviews.total > 0 ? reviews.passed / reviews.total : 0;\r\n\r\n let suggestedMode: DomainCompetence[\"suggestedMode\"];\r\n if (retentionRate > 0.9 && avgStab > 30) {\r\n suggestedMode = \"autonomy\";\r\n } else if (retentionRate > 0.7 && avgStab > 7) {\r\n suggestedMode = \"copilot\";\r\n } else {\r\n suggestedMode = \"shadowing\";\r\n }\r\n\r\n return {\r\n domain: d.domain,\r\n totalCards: total,\r\n matureCards: mature,\r\n avgStability: Math.round(avgStab * 100) / 100,\r\n retentionRate: Math.round(retentionRate * 1000) / 1000,\r\n suggestedMode,\r\n };\r\n });\r\n}\r\n","/**\r\n * Monitor log analyzer — maps observed shell commands to token ratings.\r\n *\r\n * Pure functions, no DB or filesystem access. Takes parsed command records\r\n * and a token-to-pattern mapping, returns ratings with evidence.\r\n */\r\n\r\n// ── Types ────────────────────────────────────────────────────────────────────\r\n\r\nexport interface MonitorEvent {\r\n type: \"command_start\" | \"command_end\" | \"monitor_meta\";\r\n ts: string;\r\n seq?: number;\r\n pid?: number;\r\n command?: string;\r\n cwd?: string;\r\n exit_code?: number;\r\n event?: \"start\" | \"stop\";\r\n session_id?: string;\r\n shell?: string;\r\n}\r\n\r\nexport interface CommandRecord {\r\n seq: number;\r\n pid: number;\r\n command: string;\r\n cwd: string;\r\n startedAt: string;\r\n endedAt: string | null;\r\n durationMs: number | null;\r\n exitCode: number | null;\r\n}\r\n\r\nexport interface TokenPattern {\r\n slug: string;\r\n patterns: string[]; // command prefixes or regex strings\r\n}\r\n\r\nexport interface ObservationRating {\r\n tokenSlug: string;\r\n rating: 1 | 2 | 3 | 4 | null;\r\n confidence: \"high\" | \"medium\" | \"low\";\r\n evidence: {\r\n matchedCommands: number;\r\n helpSeeking: boolean;\r\n errorCount: number;\r\n selfCorrections: number;\r\n medianGapMs: number | null;\r\n thinkingGapMs: number | null;\r\n };\r\n matchedCommandTexts: string[];\r\n}\r\n\r\nexport interface AnalysisResult {\r\n ratings: ObservationRating[];\r\n unmatchedCommands: string[];\r\n timeSpan: { start: string; end: string; durationMs: number } | null;\r\n}\r\n\r\n// ── Parsing ──────────────────────────────────────────────────────────────────\r\n\r\n/**\r\n * Parse a JSONL string into MonitorEvent objects.\r\n * Skips malformed lines silently.\r\n */\r\nexport function parseMonitorLog(jsonl: string): MonitorEvent[] {\r\n const events: MonitorEvent[] = [];\r\n for (const line of jsonl.split(\"\\n\")) {\r\n const trimmed = line.trim();\r\n if (!trimmed) continue;\r\n try {\r\n events.push(JSON.parse(trimmed) as MonitorEvent);\r\n } catch {\r\n // skip malformed lines\r\n }\r\n }\r\n return events;\r\n}\r\n\r\n/**\r\n * Pair command_start and command_end events by (pid, seq) into CommandRecords.\r\n */\r\nexport function pairCommands(events: MonitorEvent[]): CommandRecord[] {\r\n const starts = new Map<string, MonitorEvent>();\r\n const records: CommandRecord[] = [];\r\n\r\n for (const e of events) {\r\n if (e.type === \"command_start\" && e.seq != null) {\r\n const key = `${e.pid ?? 0}:${e.seq}`;\r\n starts.set(key, e);\r\n } else if (e.type === \"command_end\" && e.seq != null) {\r\n const key = `${e.pid ?? 0}:${e.seq}`;\r\n const start = starts.get(key);\r\n if (start) {\r\n const startMs = new Date(start.ts).getTime();\r\n const endMs = new Date(e.ts).getTime();\r\n records.push({\r\n seq: e.seq,\r\n pid: e.pid ?? 0,\r\n command: start.command ?? \"\",\r\n cwd: start.cwd ?? \"\",\r\n startedAt: start.ts,\r\n endedAt: e.ts,\r\n durationMs: endMs - startMs,\r\n exitCode: e.exit_code ?? null,\r\n });\r\n starts.delete(key);\r\n }\r\n }\r\n }\r\n\r\n // Remaining unpaired starts (monitoring stopped mid-command)\r\n for (const [, start] of starts) {\r\n records.push({\r\n seq: start.seq ?? 0,\r\n pid: start.pid ?? 0,\r\n command: start.command ?? \"\",\r\n cwd: start.cwd ?? \"\",\r\n startedAt: start.ts,\r\n endedAt: null,\r\n durationMs: null,\r\n exitCode: null,\r\n });\r\n }\r\n\r\n records.sort((a, b) => new Date(a.startedAt).getTime() - new Date(b.startedAt).getTime());\r\n return records;\r\n}\r\n\r\n// ── Analysis ─────────────────────────────────────────────────────────────────\r\n\r\nconst HELP_PATTERNS = [\"--help\", \"man \", \"tldr \", \"help \"];\r\nconst HELP_WINDOW_MS = 60_000;\r\nconst RETRY_WINDOW_MS = 30_000;\r\n\r\nfunction matchesToken(command: string, patterns: string[]): boolean {\r\n const lower = command.toLowerCase();\r\n return patterns.some((p) => lower.includes(p.toLowerCase()));\r\n}\r\n\r\nfunction isHelpCommand(command: string): boolean {\r\n const lower = command.toLowerCase();\r\n return HELP_PATTERNS.some((p) => lower.includes(p));\r\n}\r\n\r\nfunction commandPrefix(command: string): string {\r\n return command.split(/\\s+/).slice(0, 2).join(\" \").toLowerCase();\r\n}\r\n\r\nfunction computeMedian(values: number[]): number | null {\r\n if (values.length === 0) return null;\r\n const sorted = [...values].sort((a, b) => a - b);\r\n const mid = Math.floor(sorted.length / 2);\r\n return sorted.length % 2 !== 0 ? sorted[mid] : (sorted[mid - 1] + sorted[mid]) / 2;\r\n}\r\n\r\n/**\r\n * Analyze observed commands against token patterns and produce ratings.\r\n */\r\nexport function analyzeObservation(\r\n commands: CommandRecord[],\r\n tokenPatterns: TokenPattern[],\r\n): AnalysisResult {\r\n const matchedSet = new Set<number>(); // indices of commands matched to any token\r\n const ratings: ObservationRating[] = [];\r\n\r\n for (const tp of tokenPatterns) {\r\n const matchIndices: number[] = [];\r\n const matchedTexts: string[] = [];\r\n\r\n for (let i = 0; i < commands.length; i++) {\r\n if (matchesToken(commands[i].command, tp.patterns)) {\r\n matchIndices.push(i);\r\n matchedTexts.push(commands[i].command);\r\n matchedSet.add(i);\r\n }\r\n }\r\n\r\n if (matchIndices.length === 0) {\r\n ratings.push({\r\n tokenSlug: tp.slug,\r\n rating: null,\r\n confidence: \"low\",\r\n evidence: {\r\n matchedCommands: 0,\r\n helpSeeking: false,\r\n errorCount: 0,\r\n selfCorrections: 0,\r\n medianGapMs: null,\r\n thinkingGapMs: null,\r\n },\r\n matchedCommandTexts: [],\r\n });\r\n continue;\r\n }\r\n\r\n // Help-seeking: any help command within HELP_WINDOW_MS before a matched command\r\n let helpSeeking = false;\r\n for (const mi of matchIndices) {\r\n const matchTime = new Date(commands[mi].startedAt).getTime();\r\n for (let j = 0; j < commands.length; j++) {\r\n if (j === mi) continue;\r\n const cmdTime = new Date(commands[j].startedAt).getTime();\r\n if (cmdTime >= matchTime - HELP_WINDOW_MS && cmdTime < matchTime) {\r\n if (isHelpCommand(commands[j].command)) {\r\n helpSeeking = true;\r\n break;\r\n }\r\n }\r\n }\r\n if (helpSeeking) break;\r\n }\r\n\r\n // Error count: matched commands with non-zero exit code\r\n let errorCount = 0;\r\n for (const mi of matchIndices) {\r\n if (commands[mi].exitCode != null && commands[mi].exitCode !== 0) {\r\n errorCount++;\r\n }\r\n }\r\n\r\n // Self-corrections: same command prefix run multiple times with different args\r\n let selfCorrections = 0;\r\n const prefixGroups = new Map<string, number>();\r\n for (const mi of matchIndices) {\r\n const prefix = commandPrefix(commands[mi].command);\r\n prefixGroups.set(prefix, (prefixGroups.get(prefix) ?? 0) + 1);\r\n }\r\n for (const count of prefixGroups.values()) {\r\n if (count > 1) selfCorrections += count - 1;\r\n }\r\n\r\n // Speed: inter-command gaps between matched commands\r\n const gaps: number[] = [];\r\n for (let k = 1; k < matchIndices.length; k++) {\r\n const prev = commands[matchIndices[k - 1]];\r\n const curr = commands[matchIndices[k]];\r\n if (prev.endedAt) {\r\n const gap = new Date(curr.startedAt).getTime() - new Date(prev.endedAt).getTime();\r\n if (gap >= 0) gaps.push(gap);\r\n }\r\n }\r\n\r\n // Thinking gap: time before first matched command from previous command's end\r\n let thinkingGapMs: number | null = null;\r\n const firstMatchIdx = matchIndices[0];\r\n if (firstMatchIdx > 0) {\r\n const prev = commands[firstMatchIdx - 1];\r\n if (prev.endedAt) {\r\n thinkingGapMs =\r\n new Date(commands[firstMatchIdx].startedAt).getTime() -\r\n new Date(prev.endedAt).getTime();\r\n }\r\n }\r\n\r\n const medianGapMs = computeMedian(gaps);\r\n\r\n // Determine rating\r\n const rating = inferRating({\r\n helpSeeking,\r\n errorCount,\r\n selfCorrections,\r\n medianGapMs,\r\n thinkingGapMs,\r\n matchedCommands: matchIndices.length,\r\n });\r\n\r\n // Confidence: more matched commands = higher confidence\r\n const confidence =\r\n matchIndices.length >= 3 ? \"high\" : matchIndices.length >= 2 ? \"medium\" : \"low\";\r\n\r\n ratings.push({\r\n tokenSlug: tp.slug,\r\n rating,\r\n confidence,\r\n evidence: {\r\n matchedCommands: matchIndices.length,\r\n helpSeeking,\r\n errorCount,\r\n selfCorrections,\r\n medianGapMs,\r\n thinkingGapMs,\r\n },\r\n matchedCommandTexts: matchedTexts,\r\n });\r\n }\r\n\r\n // Unmatched commands\r\n const unmatchedCommands: string[] = [];\r\n for (let i = 0; i < commands.length; i++) {\r\n if (!matchedSet.has(i) && !isHelpCommand(commands[i].command)) {\r\n unmatchedCommands.push(commands[i].command);\r\n }\r\n }\r\n\r\n // Time span\r\n let timeSpan: AnalysisResult[\"timeSpan\"] = null;\r\n if (commands.length > 0) {\r\n const first = commands[0];\r\n const last = commands[commands.length - 1];\r\n const endTs = last.endedAt ?? last.startedAt;\r\n timeSpan = {\r\n start: first.startedAt,\r\n end: endTs,\r\n durationMs: new Date(endTs).getTime() - new Date(first.startedAt).getTime(),\r\n };\r\n }\r\n\r\n return { ratings, unmatchedCommands, timeSpan };\r\n}\r\n\r\n// ── Rating Inference ─────────────────────────────────────────────────────────\r\n\r\ninterface RatingSignals {\r\n helpSeeking: boolean;\r\n errorCount: number;\r\n selfCorrections: number;\r\n medianGapMs: number | null;\r\n thinkingGapMs: number | null;\r\n matchedCommands: number;\r\n}\r\n\r\nfunction inferRating(signals: RatingSignals): 1 | 2 | 3 | 4 {\r\n const { helpSeeking, errorCount, selfCorrections, medianGapMs, thinkingGapMs } = signals;\r\n\r\n // Count negative signals\r\n let negatives = 0;\r\n if (helpSeeking) negatives += 2;\r\n if (errorCount >= 3) negatives += 3;\r\n else if (errorCount >= 1) negatives += 1;\r\n if (selfCorrections >= 2) negatives += 2;\r\n else if (selfCorrections >= 1) negatives += 1;\r\n if (medianGapMs != null && medianGapMs > 30_000) negatives += 2;\r\n else if (medianGapMs != null && medianGapMs > 10_000) negatives += 1;\r\n if (thinkingGapMs != null && thinkingGapMs > 30_000) negatives += 1;\r\n\r\n if (negatives >= 5) return 1;\r\n if (negatives >= 3) return 2;\r\n if (negatives >= 1) return 3;\r\n return 4;\r\n}\r\n","/**\r\n * Monitor I/O — read/write JSONL files for shell observation.\r\n *\r\n * Monitor logs live at ~/.zam/monitor/<session-id>.jsonl.\r\n * Separated from analyzer.ts so the analyzer remains pure-function testable.\r\n */\r\n\r\nimport { existsSync, mkdirSync, readFileSync, appendFileSync, statSync } from \"node:fs\";\r\nimport { homedir } from \"node:os\";\r\nimport { join } from \"node:path\";\r\nimport type { MonitorEvent } from \"./analyzer.js\";\r\nimport { parseMonitorLog } from \"./analyzer.js\";\r\n\r\nconst MONITOR_DIR = join(homedir(), \".zam\", \"monitor\");\r\n\r\n/** Get the monitor directory path. */\r\nexport function getMonitorDir(): string {\r\n return MONITOR_DIR;\r\n}\r\n\r\n/** Get the JSONL file path for a session. */\r\nexport function getMonitorPath(sessionId: string): string {\r\n return join(MONITOR_DIR, `${sessionId}.jsonl`);\r\n}\r\n\r\n/** Ensure the monitor directory exists (mode 0700 for privacy). */\r\nexport function ensureMonitorDir(): void {\r\n if (!existsSync(MONITOR_DIR)) {\r\n mkdirSync(MONITOR_DIR, { recursive: true, mode: 0o700 });\r\n }\r\n}\r\n\r\n/** Append a single event to the session's JSONL file. */\r\nexport function writeMonitorEvent(sessionId: string, event: MonitorEvent): void {\r\n ensureMonitorDir();\r\n const path = getMonitorPath(sessionId);\r\n appendFileSync(path, JSON.stringify(event) + \"\\n\");\r\n}\r\n\r\n/** Read and parse all events from a session's monitor log. */\r\nexport function readMonitorLog(sessionId: string): MonitorEvent[] {\r\n const path = getMonitorPath(sessionId);\r\n if (!existsSync(path)) {\r\n return [];\r\n }\r\n const content = readFileSync(path, \"utf-8\");\r\n return parseMonitorLog(content);\r\n}\r\n\r\n/** Check if a monitor log exists for a session. */\r\nexport function monitorLogExists(sessionId: string): boolean {\r\n return existsSync(getMonitorPath(sessionId));\r\n}\r\n\r\n/** Get basic stats about a monitor log without full parsing. */\r\nexport function getMonitorLogStats(sessionId: string): {\r\n exists: boolean;\r\n sizeBytes: number;\r\n lineCount: number;\r\n} {\r\n const path = getMonitorPath(sessionId);\r\n if (!existsSync(path)) {\r\n return { exists: false, sizeBytes: 0, lineCount: 0 };\r\n }\r\n const stat = statSync(path);\r\n const content = readFileSync(path, \"utf-8\");\r\n const lineCount = content.split(\"\\n\").filter((l) => l.trim()).length;\r\n return { exists: true, sizeBytes: stat.size, lineCount };\r\n}\r\n","/**\r\n * Shell hook code generation for zsh and bash.\r\n *\r\n * Pure functions that return shell code strings. The CLI command\r\n * `zam monitor start/stop` calls these and prints to stdout.\r\n * The user wraps with `eval \"$(zam monitor start ...)\"`.\r\n */\r\n\r\n/**\r\n * Generate zsh hooks that capture commands to a JSONL file.\r\n * Uses $EPOCHREALTIME for sub-second timestamp precision.\r\n */\r\nexport function generateZshHooks(monitorFile: string, sessionId: string): string {\r\n return `\r\n# ZAM monitor hooks for session ${sessionId}\r\nexport __ZAM_MONITOR_FILE=\"${monitorFile}\"\r\nexport __ZAM_MONITOR_SEQ=0\r\nexport __ZAM_MONITOR_SESSION=\"${sessionId}\"\r\n\r\n__zam_ts() {\r\n if [[ -n \"\\${EPOCHREALTIME:-}\" ]]; then\r\n local sec=\"\\${EPOCHREALTIME%%.*}\"\r\n local frac=\"\\${EPOCHREALTIME##*.}\"\r\n frac=\"\\${frac:0:3}\"\r\n printf '%s.%sZ' \"$(date -u -r \"\\$sec\" '+%Y-%m-%dT%H:%M:%S' 2>/dev/null || date -u '+%Y-%m-%dT%H:%M:%S')\" \"\\$frac\"\r\n else\r\n date -u '+%Y-%m-%dT%H:%M:%SZ'\r\n fi\r\n}\r\n\r\n__zam_preexec() {\r\n (( __ZAM_MONITOR_SEQ++ ))\r\n local cmd=\"\\${1//\\\\\"/\\\\\\\\\\\\\"}\"\r\n local cwd=\"\\${PWD//\\\\\"/\\\\\\\\\\\\\"}\"\r\n local ts=\"$(__zam_ts)\"\r\n printf '{\"type\":\"command_start\",\"ts\":\"%s\",\"command\":\"%s\",\"cwd\":\"%s\",\"seq\":%d,\"pid\":%d}\\\\n' \\\\\r\n \"\\$ts\" \"\\$cmd\" \"\\$cwd\" \"\\$__ZAM_MONITOR_SEQ\" \"\\$\\$\" \\\\\r\n >> \"\\$__ZAM_MONITOR_FILE\"\r\n}\r\n\r\n__zam_precmd() {\r\n local exit_code=\\$?\r\n [[ \\$__ZAM_MONITOR_SEQ -eq 0 ]] && return\r\n local ts=\"$(__zam_ts)\"\r\n printf '{\"type\":\"command_end\",\"ts\":\"%s\",\"exit_code\":%d,\"seq\":%d,\"pid\":%d}\\\\n' \\\\\r\n \"\\$ts\" \"\\$exit_code\" \"\\$__ZAM_MONITOR_SEQ\" \"\\$\\$\" \\\\\r\n >> \"\\$__ZAM_MONITOR_FILE\"\r\n}\r\n\r\nautoload -Uz add-zsh-hook\r\nadd-zsh-hook preexec __zam_preexec\r\nadd-zsh-hook precmd __zam_precmd\r\n\r\necho \"ZAM monitor active for session \\$__ZAM_MONITOR_SESSION\"\r\n`.trim();\r\n}\r\n\r\n/**\r\n * Generate bash hooks that capture commands to a JSONL file.\r\n * Uses DEBUG trap for preexec, PROMPT_COMMAND for precmd.\r\n */\r\nexport function generateBashHooks(monitorFile: string, sessionId: string): string {\r\n return `\r\n# ZAM monitor hooks for session ${sessionId}\r\nexport __ZAM_MONITOR_FILE=\"${monitorFile}\"\r\nexport __ZAM_MONITOR_SEQ=0\r\nexport __ZAM_MONITOR_SESSION=\"${sessionId}\"\r\nexport __ZAM_MONITOR_CMD_ACTIVE=0\r\n\r\n__zam_ts() {\r\n date -u '+%Y-%m-%dT%H:%M:%SZ'\r\n}\r\n\r\n__zam_debug_trap() {\r\n [[ \"\\$__ZAM_MONITOR_CMD_ACTIVE\" -eq 1 ]] && return\r\n __ZAM_MONITOR_CMD_ACTIVE=1\r\n (( __ZAM_MONITOR_SEQ++ ))\r\n local cmd=\"\\${BASH_COMMAND//\\\\\"/\\\\\\\\\\\\\"}\"\r\n local cwd=\"\\${PWD//\\\\\"/\\\\\\\\\\\\\"}\"\r\n local ts=\"$(__zam_ts)\"\r\n printf '{\"type\":\"command_start\",\"ts\":\"%s\",\"command\":\"%s\",\"cwd\":\"%s\",\"seq\":%d,\"pid\":%d}\\\\n' \\\\\r\n \"\\$ts\" \"\\$cmd\" \"\\$cwd\" \"\\$__ZAM_MONITOR_SEQ\" \"\\$\\$\" \\\\\r\n >> \"\\$__ZAM_MONITOR_FILE\"\r\n}\r\n\r\n__zam_prompt_cmd() {\r\n local exit_code=\\$?\r\n if [[ \"\\$__ZAM_MONITOR_CMD_ACTIVE\" -eq 1 ]]; then\r\n __ZAM_MONITOR_CMD_ACTIVE=0\r\n local ts=\"$(__zam_ts)\"\r\n printf '{\"type\":\"command_end\",\"ts\":\"%s\",\"exit_code\":%d,\"seq\":%d,\"pid\":%d}\\\\n' \\\\\r\n \"\\$ts\" \"\\$exit_code\" \"\\$__ZAM_MONITOR_SEQ\" \"\\$\\$\" \\\\\r\n >> \"\\$__ZAM_MONITOR_FILE\"\r\n fi\r\n}\r\n\r\ntrap '__zam_debug_trap' DEBUG\r\nPROMPT_COMMAND=\"__zam_prompt_cmd;\\${PROMPT_COMMAND:-}\"\r\n\r\necho \"ZAM monitor active for session \\$__ZAM_MONITOR_SESSION\"\r\n`.trim();\r\n}\r\n\r\n/** Generate zsh code to remove monitor hooks. */\r\nexport function generateZshUnhooks(): string {\r\n return `\r\n# Remove ZAM monitor hooks\r\nadd-zsh-hook -d preexec __zam_preexec 2>/dev/null\r\nadd-zsh-hook -d precmd __zam_precmd 2>/dev/null\r\nunset -f __zam_preexec __zam_precmd __zam_ts 2>/dev/null\r\nunset __ZAM_MONITOR_FILE __ZAM_MONITOR_SEQ __ZAM_MONITOR_SESSION 2>/dev/null\r\necho \"ZAM monitor stopped.\"\r\n`.trim();\r\n}\r\n\r\n/** Generate bash code to remove monitor hooks. */\r\nexport function generateBashUnhooks(): string {\r\n return `\r\n# Remove ZAM monitor hooks\r\ntrap - DEBUG\r\nPROMPT_COMMAND=\"\\${PROMPT_COMMAND/__zam_prompt_cmd;/}\"\r\nunset -f __zam_debug_trap __zam_prompt_cmd __zam_ts 2>/dev/null\r\nunset __ZAM_MONITOR_FILE __ZAM_MONITOR_SEQ __ZAM_MONITOR_SESSION __ZAM_MONITOR_CMD_ACTIVE 2>/dev/null\r\necho \"ZAM monitor stopped.\"\r\n`.trim();\r\n}\r\n","/**\r\n * Skill Discovery — identifies recurring non-standard command patterns\r\n * across multiple sessions and proposes them as minimal reusable skills.\r\n *\r\n * The key insight from Increment 2: \"The human's demonstrated competence\r\n * is the gate for automation — not the other way around.\" A pattern must\r\n * appear consistently across sessions before being proposed as a skill.\r\n *\r\n * Pure functions — no DB access. Callers provide command records and\r\n * existing skills; this module returns proposed skills.\r\n */\r\n\r\nimport type { CommandRecord } from \"./analyzer.js\";\r\n\r\n// ── Types ────────────────────────────────────────────────────────────────────\r\n\r\nexport interface CommandSequence {\r\n /** The ordered command prefixes forming the pattern (e.g., [\"git checkout\", \"npm install\", \"npm run build\"]) */\r\n steps: string[];\r\n /** How many sessions contained this sequence */\r\n sessionCount: number;\r\n /** Total occurrences across all sessions */\r\n totalOccurrences: number;\r\n /** Example full commands from the most recent occurrence */\r\n examples: string[];\r\n}\r\n\r\nexport interface SkillProposal {\r\n /** Suggested slug for the skill */\r\n slug: string;\r\n /** Human-readable description of what the pattern does */\r\n description: string;\r\n /** The command steps forming the skill */\r\n steps: string[];\r\n /** How many sessions demonstrated this pattern */\r\n sessionCount: number;\r\n /** Confidence that this is a real, repeatable skill */\r\n confidence: \"high\" | \"medium\" | \"low\";\r\n /** Example commands from actual usage */\r\n examples: string[];\r\n}\r\n\r\nexport interface DiscoveryOptions {\r\n /** Minimum number of sessions a pattern must appear in (default: 2) */\r\n minSessions?: number;\r\n /** Minimum sequence length to consider (default: 2) */\r\n minSequenceLength?: number;\r\n /** Maximum sequence length to consider (default: 5) */\r\n maxSequenceLength?: number;\r\n /** Existing skill slugs to exclude from proposals */\r\n existingSkillSlugs?: string[];\r\n}\r\n\r\n// ── Discovery ────────────────────────────────────────────────────────────────\r\n\r\n/**\r\n * Discover recurring command patterns across multiple sessions.\r\n *\r\n * Takes a map of session ID → command records, finds command sequences\r\n * that appear in multiple sessions, and proposes them as skills.\r\n *\r\n * @param sessionCommands - Map of session ID to that session's commands\r\n * @param options - Discovery configuration\r\n * @returns Array of skill proposals, sorted by confidence then session count\r\n */\r\nexport function discoverSkills(\r\n sessionCommands: Map<string, CommandRecord[]>,\r\n options: DiscoveryOptions = {},\r\n): SkillProposal[] {\r\n const minSessions = options.minSessions ?? 2;\r\n const minLen = options.minSequenceLength ?? 2;\r\n const maxLen = options.maxSequenceLength ?? 5;\r\n const existing = new Set(options.existingSkillSlugs ?? []);\r\n\r\n // Step 1: Extract normalized command sequences from each session\r\n const sessionSequences = new Map<string, string[][]>();\r\n for (const [sessionId, commands] of sessionCommands) {\r\n const sequences = extractSequences(commands, minLen, maxLen);\r\n if (sequences.length > 0) {\r\n sessionSequences.set(sessionId, sequences);\r\n }\r\n }\r\n\r\n // Step 2: Count how many sessions contain each unique sequence\r\n const sequenceIndex = new Map<string, CommandSequence>();\r\n\r\n for (const [, sequences] of sessionSequences) {\r\n // Deduplicate within a session — count each sequence once per session\r\n const seen = new Set<string>();\r\n for (const seq of sequences) {\r\n const key = seq.join(\" → \");\r\n if (seen.has(key)) continue;\r\n seen.add(key);\r\n\r\n const entry = sequenceIndex.get(key);\r\n if (entry) {\r\n entry.sessionCount++;\r\n entry.totalOccurrences++;\r\n } else {\r\n sequenceIndex.set(key, {\r\n steps: seq,\r\n sessionCount: 1,\r\n totalOccurrences: 1,\r\n examples: [],\r\n });\r\n }\r\n }\r\n }\r\n\r\n // Step 3: Collect examples from the most recent session for qualifying sequences\r\n const lastSessionId = [...sessionCommands.keys()].pop();\r\n if (lastSessionId) {\r\n const lastCommands = sessionCommands.get(lastSessionId)!;\r\n for (const [key, entry] of sequenceIndex) {\r\n if (entry.sessionCount >= minSessions) {\r\n entry.examples = findExamplesForSequence(lastCommands, entry.steps);\r\n }\r\n }\r\n }\r\n\r\n // Step 4: Filter to patterns that appear in enough sessions\r\n const candidates = [...sequenceIndex.values()].filter(\r\n (s) => s.sessionCount >= minSessions,\r\n );\r\n\r\n // Step 5: Remove subsequences of longer patterns (prefer maximal patterns)\r\n const pruned = removeSubsequences(candidates);\r\n\r\n // Step 6: Convert to skill proposals\r\n const proposals: SkillProposal[] = [];\r\n for (const seq of pruned) {\r\n const slug = generateSlug(seq.steps);\r\n\r\n // Skip if this skill already exists\r\n if (existing.has(slug)) continue;\r\n\r\n proposals.push({\r\n slug,\r\n description: describeSequence(seq.steps),\r\n steps: seq.steps,\r\n sessionCount: seq.sessionCount,\r\n confidence: seq.sessionCount >= 4 ? \"high\" : seq.sessionCount >= 3 ? \"medium\" : \"low\",\r\n examples: seq.examples,\r\n });\r\n }\r\n\r\n // Sort: high confidence first, then by session count\r\n const confidenceOrder = { high: 0, medium: 1, low: 2 };\r\n proposals.sort((a, b) => {\r\n const confDiff = confidenceOrder[a.confidence] - confidenceOrder[b.confidence];\r\n if (confDiff !== 0) return confDiff;\r\n return b.sessionCount - a.sessionCount;\r\n });\r\n\r\n return proposals;\r\n}\r\n\r\n// ── Helpers ──────────────────────────────────────────────────────────────────\r\n\r\n/**\r\n * Normalize a command to its tool + subcommand prefix.\r\n * \"git checkout -b feat/foo\" → \"git checkout\"\r\n * \"npm run build\" → \"npm run build\"\r\n * \"docker compose up -d\" → \"docker compose up\"\r\n */\r\nfunction normalizeCommand(command: string): string {\r\n const parts = command.trim().split(/\\s+/);\r\n\r\n // Known multi-word tools\r\n const multiWord = [\"docker compose\", \"npm run\", \"npx\", \"git\"];\r\n const lower = command.toLowerCase();\r\n\r\n for (const mw of multiWord) {\r\n if (lower.startsWith(mw) && parts.length >= mw.split(\" \").length + 1) {\r\n return parts.slice(0, mw.split(\" \").length + 1).join(\" \").toLowerCase();\r\n }\r\n }\r\n\r\n // Default: first two words\r\n return parts.slice(0, Math.min(2, parts.length)).join(\" \").toLowerCase();\r\n}\r\n\r\n/**\r\n * Extract all command subsequences of length minLen..maxLen from a session.\r\n * Uses normalized command prefixes for comparison.\r\n */\r\nfunction extractSequences(\r\n commands: CommandRecord[],\r\n minLen: number,\r\n maxLen: number,\r\n): string[][] {\r\n // Filter out trivial commands\r\n const filtered = commands.filter((c) => {\r\n const lower = c.command.toLowerCase().trim();\r\n return (\r\n lower.length > 0 &&\r\n !lower.startsWith(\"cd \") &&\r\n lower !== \"cd\" &&\r\n lower !== \"ls\" &&\r\n lower !== \"pwd\" &&\r\n lower !== \"clear\" &&\r\n lower !== \"exit\" &&\r\n !lower.startsWith(\"echo \")\r\n );\r\n });\r\n\r\n const normalized = filtered.map((c) => normalizeCommand(c.command));\r\n const sequences: string[][] = [];\r\n\r\n for (let len = minLen; len <= maxLen; len++) {\r\n for (let i = 0; i <= normalized.length - len; i++) {\r\n const seq = normalized.slice(i, i + len);\r\n // Only include if at least 2 distinct commands (not just \"git commit\" repeated)\r\n if (new Set(seq).size >= 2) {\r\n sequences.push(seq);\r\n }\r\n }\r\n }\r\n\r\n return sequences;\r\n}\r\n\r\n/**\r\n * Find example full commands for a given normalized sequence in a command list.\r\n */\r\nfunction findExamplesForSequence(\r\n commands: CommandRecord[],\r\n steps: string[],\r\n): string[] {\r\n const normalized = commands.map((c) => ({\r\n norm: normalizeCommand(c.command),\r\n full: c.command,\r\n }));\r\n\r\n for (let i = 0; i <= normalized.length - steps.length; i++) {\r\n let match = true;\r\n for (let j = 0; j < steps.length; j++) {\r\n if (normalized[i + j].norm !== steps[j]) {\r\n match = false;\r\n break;\r\n }\r\n }\r\n if (match) {\r\n return normalized.slice(i, i + steps.length).map((n) => n.full);\r\n }\r\n }\r\n\r\n return [];\r\n}\r\n\r\n/**\r\n * Remove sequences that are strict subsequences of longer qualifying sequences.\r\n */\r\nfunction removeSubsequences(candidates: CommandSequence[]): CommandSequence[] {\r\n // Sort by length descending so we check long sequences first\r\n const sorted = [...candidates].sort((a, b) => b.steps.length - a.steps.length);\r\n const result: CommandSequence[] = [];\r\n\r\n for (const candidate of sorted) {\r\n const key = candidate.steps.join(\" → \");\r\n const isSubsequence = result.some((longer) => {\r\n const longerKey = longer.steps.join(\" → \");\r\n return longerKey.includes(key) && longerKey !== key;\r\n });\r\n\r\n if (!isSubsequence) {\r\n result.push(candidate);\r\n }\r\n }\r\n\r\n return result;\r\n}\r\n\r\n/**\r\n * Generate a slug from command steps.\r\n * [\"git checkout\", \"npm install\", \"npm run build\"] → \"checkout-install-build\"\r\n */\r\nfunction generateSlug(steps: string[]): string {\r\n return steps\r\n .map((s) => {\r\n const parts = s.split(/\\s+/);\r\n return parts[parts.length - 1]; // last word of each step\r\n })\r\n .join(\"-\");\r\n}\r\n\r\n/**\r\n * Generate a human-readable description of what a command sequence does.\r\n */\r\nfunction describeSequence(steps: string[]): string {\r\n return `Recurring pattern: ${steps.join(\" → \")}`;\r\n}\r\n","/**\r\n * Goal Engine — manages goal lifecycle via markdown files.\r\n *\r\n * Goals live as markdown files in a directory (typically the personal repo's\r\n * goals/ folder). The engine reads, creates, and updates these files.\r\n * It does not depend on the database — goals are git-tracked, not DB-tracked.\r\n */\r\n\r\nimport { readdirSync, readFileSync, writeFileSync, existsSync } from \"node:fs\";\r\nimport { join, basename } from \"node:path\";\r\nimport {\r\n parseGoalFile,\r\n serializeGoal,\r\n extractTasks,\r\n extractTokenRefs,\r\n} from \"./parser.js\";\r\nimport type { Goal, GoalStatus } from \"./parser.js\";\r\n\r\n// ── Types ────────────────────────────────────────────────────────────────────\r\n\r\nexport interface GoalSummary {\r\n slug: string;\r\n title: string;\r\n status: GoalStatus;\r\n parent: string | null;\r\n taskCount: number;\r\n tasksDone: number;\r\n tokenCount: number;\r\n}\r\n\r\nexport interface CreateGoalInput {\r\n slug: string;\r\n title: string;\r\n status?: GoalStatus;\r\n parent?: string;\r\n description?: string;\r\n}\r\n\r\n// ── Functions ────────────────────────────────────────────────────────────────\r\n\r\n/**\r\n * List all goals in the goals directory.\r\n * Returns summaries sorted by status (active first) then title.\r\n */\r\nexport function listGoals(goalsDir: string): GoalSummary[] {\r\n if (!existsSync(goalsDir)) return [];\r\n\r\n const files = readdirSync(goalsDir).filter(\r\n (f) => f.endsWith(\".md\") && f !== \"README.md\",\r\n );\r\n\r\n const summaries: GoalSummary[] = [];\r\n\r\n for (const file of files) {\r\n const filePath = join(goalsDir, file);\r\n const content = readFileSync(filePath, \"utf-8\");\r\n const slug = basename(file, \".md\");\r\n const goal = parseGoalFile(content, slug, filePath);\r\n const tasks = extractTasks(goal.body);\r\n const tokens = extractTokenRefs(goal.body);\r\n\r\n summaries.push({\r\n slug: goal.slug,\r\n title: goal.title,\r\n status: goal.status,\r\n parent: goal.parent,\r\n taskCount: tasks.length,\r\n tasksDone: tasks.filter((t) => t.done).length,\r\n tokenCount: tokens.length,\r\n });\r\n }\r\n\r\n const statusOrder: Record<GoalStatus, number> = {\r\n active: 0,\r\n paused: 1,\r\n completed: 2,\r\n abandoned: 3,\r\n };\r\n\r\n summaries.sort((a, b) => {\r\n const statusDiff = statusOrder[a.status] - statusOrder[b.status];\r\n if (statusDiff !== 0) return statusDiff;\r\n return a.title.localeCompare(b.title);\r\n });\r\n\r\n return summaries;\r\n}\r\n\r\n/**\r\n * Get a single goal by slug (filename without .md).\r\n * Returns undefined if the file doesn't exist.\r\n */\r\nexport function getGoal(goalsDir: string, slug: string): Goal | undefined {\r\n const filePath = join(goalsDir, `${slug}.md`);\r\n if (!existsSync(filePath)) return undefined;\r\n\r\n const content = readFileSync(filePath, \"utf-8\");\r\n return parseGoalFile(content, slug, filePath);\r\n}\r\n\r\n/**\r\n * Create a new goal file. Throws if a goal with this slug already exists.\r\n */\r\nexport function createGoal(goalsDir: string, input: CreateGoalInput): Goal {\r\n const filePath = join(goalsDir, `${input.slug}.md`);\r\n\r\n if (existsSync(filePath)) {\r\n throw new Error(`Goal already exists: ${input.slug}`);\r\n }\r\n\r\n const now = new Date().toISOString().slice(0, 10);\r\n\r\n const goal: Goal = {\r\n slug: input.slug,\r\n title: input.title,\r\n status: input.status ?? \"active\",\r\n parent: input.parent ?? null,\r\n created: now,\r\n updated: now,\r\n body: input.description\r\n ? `## Description\\n${input.description}\\n\\n## Tasks\\n\\n## Tokens`\r\n : \"## Description\\n\\n## Tasks\\n\\n## Tokens\",\r\n filePath,\r\n };\r\n\r\n writeFileSync(filePath, serializeGoal(goal), \"utf-8\");\r\n return goal;\r\n}\r\n\r\n/**\r\n * Update a goal's status. Writes the updated file back to disk.\r\n */\r\nexport function updateGoalStatus(\r\n goalsDir: string,\r\n slug: string,\r\n status: GoalStatus,\r\n): Goal {\r\n const goal = getGoal(goalsDir, slug);\r\n if (!goal) throw new Error(`Goal not found: ${slug}`);\r\n\r\n goal.status = status;\r\n goal.updated = new Date().toISOString().slice(0, 10);\r\n\r\n writeFileSync(goal.filePath, serializeGoal(goal), \"utf-8\");\r\n return goal;\r\n}\r\n\r\n/**\r\n * Get the goal tree — goals organized by parent relationships.\r\n * Returns root goals (no parent) with nested children.\r\n */\r\nexport function getGoalTree(goalsDir: string): Array<GoalSummary & { children: GoalSummary[] }> {\r\n const all = listGoals(goalsDir);\r\n const bySlug = new Map(all.map((g) => [g.slug, g]));\r\n\r\n const roots: Array<GoalSummary & { children: GoalSummary[] }> = [];\r\n const children = new Map<string, GoalSummary[]>();\r\n\r\n for (const g of all) {\r\n if (g.parent && bySlug.has(g.parent)) {\r\n const list = children.get(g.parent) ?? [];\r\n list.push(g);\r\n children.set(g.parent, list);\r\n }\r\n }\r\n\r\n for (const g of all) {\r\n if (!g.parent || !bySlug.has(g.parent)) {\r\n roots.push({ ...g, children: children.get(g.slug) ?? [] });\r\n }\r\n }\r\n\r\n return roots;\r\n}\r\n","/**\r\n * Goal file parser — reads markdown files with YAML-style frontmatter.\r\n *\r\n * Goals are persisted as markdown files in the personal repo.\r\n * Each file has simple key: value frontmatter (no nested structures)\r\n * and a markdown body with description, tasks, and token references.\r\n */\r\n\r\n// ── Types ────────────────────────────────────────────────────────────────────\r\n\r\nexport type GoalStatus = \"active\" | \"completed\" | \"paused\" | \"abandoned\";\r\n\r\nexport interface Goal {\r\n slug: string; // derived from filename (e.g., \"learn-rust\" from \"learn-rust.md\")\r\n title: string;\r\n status: GoalStatus;\r\n parent: string | null; // slug of parent goal\r\n created: string; // ISO date\r\n updated: string; // ISO date\r\n body: string; // markdown body after frontmatter\r\n filePath: string; // absolute path to the file\r\n}\r\n\r\nexport interface GoalFrontmatter {\r\n title?: string;\r\n status?: string;\r\n parent?: string;\r\n created?: string;\r\n updated?: string;\r\n}\r\n\r\n// ── Parser ───────────────────────────────────────────────────────────────────\r\n\r\n/**\r\n * Parse a goal markdown file into a Goal object.\r\n *\r\n * Expected format:\r\n * ```\r\n * ---\r\n * title: Learn Rust fundamentals\r\n * status: active\r\n * parent: become-systems-programmer\r\n * created: 2026-03-28\r\n * updated: 2026-03-28\r\n * ---\r\n *\r\n * ## Description\r\n * ...\r\n * ```\r\n *\r\n * @param content - Raw file content\r\n * @param slug - Goal slug (derived from filename by caller)\r\n * @param filePath - Absolute path to the file\r\n */\r\nexport function parseGoalFile(content: string, slug: string, filePath: string): Goal {\r\n const { frontmatter, body } = splitFrontmatter(content);\r\n\r\n const validStatuses: GoalStatus[] = [\"active\", \"completed\", \"paused\", \"abandoned\"];\r\n const status = validStatuses.includes(frontmatter.status as GoalStatus)\r\n ? (frontmatter.status as GoalStatus)\r\n : \"active\";\r\n\r\n const now = new Date().toISOString().slice(0, 10);\r\n\r\n return {\r\n slug,\r\n title: frontmatter.title || slug,\r\n status,\r\n parent: frontmatter.parent || null,\r\n created: frontmatter.created || now,\r\n updated: frontmatter.updated || now,\r\n body,\r\n filePath,\r\n };\r\n}\r\n\r\n/**\r\n * Serialize a Goal back to markdown with frontmatter.\r\n */\r\nexport function serializeGoal(goal: Goal): string {\r\n const lines = [\r\n \"---\",\r\n `title: ${goal.title}`,\r\n `status: ${goal.status}`,\r\n ];\r\n\r\n if (goal.parent) {\r\n lines.push(`parent: ${goal.parent}`);\r\n }\r\n\r\n lines.push(`created: ${goal.created}`);\r\n lines.push(`updated: ${goal.updated}`);\r\n lines.push(\"---\");\r\n lines.push(\"\");\r\n\r\n if (goal.body.trim()) {\r\n lines.push(goal.body.trim());\r\n lines.push(\"\");\r\n }\r\n\r\n return lines.join(\"\\n\");\r\n}\r\n\r\n/**\r\n * Extract tasks (checklist items) from goal body.\r\n * Returns items like { text: \"Complete Rustlings\", done: false }.\r\n */\r\nexport function extractTasks(body: string): Array<{ text: string; done: boolean }> {\r\n const tasks: Array<{ text: string; done: boolean }> = [];\r\n const taskRegex = /^[-*]\\s+\\[([ xX])\\]\\s+(.+)$/gm;\r\n let match: RegExpExecArray | null;\r\n\r\n while ((match = taskRegex.exec(body)) !== null) {\r\n tasks.push({\r\n done: match[1] !== \" \",\r\n text: match[2].trim(),\r\n });\r\n }\r\n\r\n return tasks;\r\n}\r\n\r\n/**\r\n * Extract token references from goal body.\r\n * Looks for lines like `- token/slug` under a \"## Tokens\" section.\r\n */\r\nexport function extractTokenRefs(body: string): string[] {\r\n const tokensSection = body.match(/## Tokens\\n([\\s\\S]*?)(?=\\n## |\\n*$)/);\r\n if (!tokensSection) return [];\r\n\r\n const refs: string[] = [];\r\n const lines = tokensSection[1].split(\"\\n\");\r\n\r\n for (const line of lines) {\r\n const match = line.match(/^[-*]\\s+(\\S+)/);\r\n if (match) {\r\n refs.push(match[1]);\r\n }\r\n }\r\n\r\n return refs;\r\n}\r\n\r\n// ── Internal helpers ─────────────────────────────────────────────────────────\r\n\r\nfunction splitFrontmatter(content: string): { frontmatter: GoalFrontmatter; body: string } {\r\n const trimmed = content.trim();\r\n\r\n if (!trimmed.startsWith(\"---\")) {\r\n return { frontmatter: {}, body: trimmed };\r\n }\r\n\r\n const endIndex = trimmed.indexOf(\"---\", 3);\r\n if (endIndex === -1) {\r\n return { frontmatter: {}, body: trimmed };\r\n }\r\n\r\n const fmBlock = trimmed.slice(3, endIndex).trim();\r\n const body = trimmed.slice(endIndex + 3).trim();\r\n\r\n const frontmatter: GoalFrontmatter = {};\r\n for (const line of fmBlock.split(\"\\n\")) {\r\n const colonIndex = line.indexOf(\":\");\r\n if (colonIndex === -1) continue;\r\n\r\n const key = line.slice(0, colonIndex).trim();\r\n const value = line.slice(colonIndex + 1).trim();\r\n\r\n if (key && value) {\r\n (frontmatter as Record<string, string>)[key] = value;\r\n }\r\n }\r\n\r\n return { frontmatter, body };\r\n}\r\n","/**\r\n * Azure DevOps connector — fetches work items from ADO boards.\r\n */\r\n\r\nimport type { Database } from \"libsql\";\r\nimport { getSetting } from \"../models/settings.js\";\r\n\r\nexport interface ADOConfig {\r\n orgUrl: string;\r\n project: string;\r\n pat: string;\r\n}\r\n\r\nexport interface WorkItem {\r\n id: number;\r\n title: string;\r\n state: string;\r\n type: string;\r\n assignedTo: string;\r\n}\r\n\r\n/** Load ADO config from user settings. Returns null if not configured. */\r\nexport function loadADOConfig(db: Database): ADOConfig | null {\r\n const orgUrl = getSetting(db, \"ado.org_url\");\r\n const project = getSetting(db, \"ado.project\");\r\n const pat = getSetting(db, \"ado.pat\");\r\n\r\n if (!orgUrl || !project || !pat) return null;\r\n\r\n return { orgUrl: orgUrl.replace(/\\/+$/, \"\"), project, pat };\r\n}\r\n\r\nfunction authHeader(pat: string): string {\r\n return `Basic ${Buffer.from(`:${pat}`).toString(\"base64\")}`;\r\n}\r\n\r\n/**\r\n * Fetch active work items assigned to the current user.\r\n * Uses WIQL to query, then batch-fetches work item details.\r\n */\r\nexport async function fetchActiveWorkItems(config: ADOConfig): Promise<WorkItem[]> {\r\n const { orgUrl, project, pat } = config;\r\n\r\n // Step 1: WIQL query for work item IDs\r\n const wiqlUrl = `${orgUrl}/${project}/_apis/wit/wiql?api-version=7.1`;\r\n const wiqlBody = {\r\n query: `SELECT [System.Id] FROM WorkItems WHERE [System.AssignedTo] = @me AND [System.State] NOT IN ('Closed', 'Completed', 'Done', 'Removed') ORDER BY [Microsoft.VSTS.Common.Priority] ASC, [System.ChangedDate] DESC`,\r\n };\r\n\r\n const wiqlRes = await fetch(wiqlUrl, {\r\n method: \"POST\",\r\n headers: {\r\n Authorization: authHeader(pat),\r\n \"Content-Type\": \"application/json\",\r\n },\r\n body: JSON.stringify(wiqlBody),\r\n });\r\n\r\n if (!wiqlRes.ok) {\r\n const text = await wiqlRes.text();\r\n throw new Error(`ADO WIQL query failed (${wiqlRes.status}): ${text}`);\r\n }\r\n\r\n const wiqlData = (await wiqlRes.json()) as { workItems: { id: number }[] };\r\n const ids = wiqlData.workItems.map((wi) => wi.id);\r\n\r\n if (ids.length === 0) return [];\r\n\r\n // Step 2: Batch fetch work item details (max 200 per request)\r\n const batchIds = ids.slice(0, 200);\r\n const fields = \"System.Id,System.Title,System.State,System.WorkItemType,System.AssignedTo\";\r\n const detailUrl = `${orgUrl}/${project}/_apis/wit/workitems?ids=${batchIds.join(\",\")}&fields=${fields}&api-version=7.1`;\r\n\r\n const detailRes = await fetch(detailUrl, {\r\n headers: { Authorization: authHeader(pat) },\r\n });\r\n\r\n if (!detailRes.ok) {\r\n const text = await detailRes.text();\r\n throw new Error(`ADO work items fetch failed (${detailRes.status}): ${text}`);\r\n }\r\n\r\n const detailData = (await detailRes.json()) as {\r\n value: Array<{\r\n id: number;\r\n fields: {\r\n \"System.Title\": string;\r\n \"System.State\": string;\r\n \"System.WorkItemType\": string;\r\n \"System.AssignedTo\"?: { displayName: string };\r\n };\r\n }>;\r\n };\r\n\r\n return detailData.value.map((wi) => ({\r\n id: wi.id,\r\n title: wi.fields[\"System.Title\"],\r\n state: wi.fields[\"System.State\"],\r\n type: wi.fields[\"System.WorkItemType\"],\r\n assignedTo: wi.fields[\"System.AssignedTo\"]?.displayName ?? \"\",\r\n }));\r\n}\r\n","/**\r\n * `zam setup` — Distribute skill files from the zam package into the current\r\n * personal instance's .claude/ and .gemini/ directories, and optionally\r\n * initialize the ZAM database and generate a CLAUDE.md.\r\n *\r\n * Run this once after cloning a ZAM personal instance, and again after\r\n * upgrading zam (with --force) to refresh the skill files.\r\n */\r\n\r\nimport { Command } from \"commander\";\r\nimport { fileURLToPath } from \"url\";\r\nimport { existsSync, mkdirSync, copyFileSync, writeFileSync } from \"fs\";\r\nimport { join, dirname, basename } from \"path\";\r\nimport { openDatabase, getDefaultDbPath } from \"../../kernel/index.js\";\r\n\r\n// The compiled CLI lives at dist/cli/index.js inside the package.\r\n// Two levels up from there is the package root (dist/ → package root).\r\n// This same relative path also works when running via `tsx src/cli/index.ts`\r\n// (src/ → package root), so no branch logic is needed.\r\nconst packageRoot = fileURLToPath(new URL(\"../..\", import.meta.url));\r\n\r\nconst SKILL_PAIRS: Array<{ from: string; to: string }> = [\r\n {\r\n from: join(packageRoot, \".claude\", \"skills\", \"zam\", \"SKILL.md\"),\r\n to: join(\".claude\", \"skills\", \"zam\", \"SKILL.md\"),\r\n },\r\n {\r\n from: join(packageRoot, \".gemini\", \"skills\", \"zam\", \"SKILL.md\"),\r\n to: join(\".gemini\", \"skills\", \"zam\", \"SKILL.md\"),\r\n },\r\n];\r\n\r\nfunction copySkills(force: boolean): void {\r\n const cwd = process.cwd();\r\n let anyAction = false;\r\n\r\n for (const { from, to } of SKILL_PAIRS) {\r\n const dest = join(cwd, to);\r\n\r\n if (!existsSync(from)) {\r\n console.warn(` warn source not found, skipping: ${from}`);\r\n continue;\r\n }\r\n\r\n if (existsSync(dest) && !force) {\r\n console.log(` skip ${to} (already present — use --force to update)`);\r\n continue;\r\n }\r\n\r\n mkdirSync(dirname(dest), { recursive: true });\r\n copyFileSync(from, dest);\r\n console.log(` copy ${to}`);\r\n anyAction = true;\r\n }\r\n\r\n if (!anyAction && !force) {\r\n console.log(\r\n \"\\nSkill files are already up to date. Run with --force to overwrite.\",\r\n );\r\n }\r\n}\r\n\r\nfunction initDatabase(skipInit: boolean): void {\r\n if (skipInit) return;\r\n\r\n try {\r\n const dbPath = getDefaultDbPath();\r\n const db = openDatabase({ initialize: true });\r\n db.close();\r\n console.log(` init ZAM database at ${dbPath}`);\r\n } catch (err) {\r\n // Database may already exist — not an error during setup.\r\n const msg = (err as Error).message;\r\n if (!msg.includes(\"already\")) {\r\n console.warn(` warn database init: ${msg}`);\r\n } else {\r\n console.log(` skip database already initialized`);\r\n }\r\n }\r\n}\r\n\r\nfunction writeClaudeMd(skipClaudeMd: boolean): void {\r\n if (skipClaudeMd) return;\r\n\r\n const dest = join(process.cwd(), \"CLAUDE.md\");\r\n if (existsSync(dest)) {\r\n console.log(` skip CLAUDE.md (already present)`);\r\n return;\r\n }\r\n\r\n const name = basename(process.cwd());\r\n writeFileSync(\r\n dest,\r\n `# ZAM Personal Kernel — ${name}\r\n\r\nThis is a ZAM personal instance. ZAM builds lasting skills through spaced\r\nrepetition during real work — not separate study sessions.\r\n\r\n## First time here?\r\nRun \\`/setup\\` in Claude Code or Gemini CLI to complete first-time setup.\r\n\r\n## Regular use\r\nRun \\`/zam\\` to start a learning session on whatever you are working on.\r\n\r\n## What lives here\r\n- \\`beliefs/\\` — your worldview, approved by git commit\r\n- \\`goals/\\` — your objectives, decomposed into tasks and learning tokens\r\n\r\n## Fast-changing data\r\nLearning tokens, cards, and review history live in \\`~/.zam/zam.db\\` (local\r\nSQLite, not committed to git). Use \\`zam connector setup turso\\` to enable\r\ncloud sync across machines.\r\n`,\r\n \"utf8\",\r\n );\r\n console.log(` write CLAUDE.md`);\r\n}\r\n\r\nexport const setupCommand = new Command(\"setup\")\r\n .description(\r\n \"Distribute ZAM skill files into this personal instance and initialize the database\",\r\n )\r\n .option(\r\n \"--force\",\r\n \"overwrite existing skill files (use after upgrading zam)\",\r\n false,\r\n )\r\n .option(\"--skip-init\", \"skip database initialization\", false)\r\n .option(\"--skip-claude-md\", \"skip CLAUDE.md generation\", false)\r\n .action(\r\n (opts: { force: boolean; skipInit: boolean; skipClaudeMd: boolean }) => {\r\n console.log(`Setting up ZAM in ${process.cwd()}\\n`);\r\n\r\n copySkills(opts.force);\r\n initDatabase(opts.skipInit);\r\n writeClaudeMd(opts.skipClaudeMd);\r\n\r\n console.log(\r\n \"\\nDone. Run `zam whoami --set <your-id>` to set your identity, then open Claude Code or Gemini CLI and run /zam to start a learning session.\",\r\n );\r\n },\r\n );\r\n","/**\r\n * `zam token` — Token management subcommand group.\r\n */\r\n\r\nimport { Command } from \"commander\";\r\nimport type { Database } from \"libsql\";\r\nimport {\r\n openDatabase,\r\n createToken,\r\n findTokens,\r\n listTokens,\r\n getTokenBySlug,\r\n addPrerequisite,\r\n getPrerequisites,\r\n getDependents,\r\n ensureCard,\r\n getCard,\r\n deprecateToken,\r\n} from \"../../kernel/index.js\";\r\nimport type { BloomLevel } from \"../../kernel/index.js\";\r\nimport { resolveUser } from \"./resolve-user.js\";\r\n\r\nfunction withDb(fn: (db: Database) => void): void {\r\n let db: Database | undefined;\r\n try {\r\n db = openDatabase();\r\n fn(db);\r\n } catch (err) {\r\n console.error(\"Error:\", (err as Error).message);\r\n process.exit(1);\r\n } finally {\r\n db?.close();\r\n }\r\n}\r\n\r\nexport const tokenCommand = new Command(\"token\")\r\n .description(\"Manage knowledge tokens\");\r\n\r\n// ── zam token register ────────────────────────────────────────────────────\r\n\r\ntokenCommand\r\n .command(\"register\")\r\n .description(\"Register a new knowledge token\")\r\n .requiredOption(\"--slug <slug>\", \"Unique token slug\")\r\n .requiredOption(\"--concept <concept>\", \"Concept description\")\r\n .option(\"--domain <domain>\", \"Knowledge domain\", \"\")\r\n .option(\"--bloom <level>\", \"Bloom taxonomy level (1-5)\", \"1\")\r\n .option(\"--json\", \"Output as JSON\")\r\n .action((opts) => {\r\n withDb((db) => {\r\n const token = createToken(db, {\r\n slug: opts.slug,\r\n concept: opts.concept,\r\n domain: opts.domain,\r\n bloom_level: Number(opts.bloom) as BloomLevel,\r\n });\r\n\r\n if (opts.json) {\r\n console.log(JSON.stringify(token, null, 2));\r\n } else {\r\n console.log(`Registered token: ${token.slug} (${token.id})`);\r\n console.log(` Concept: ${token.concept}`);\r\n console.log(` Domain: ${token.domain || \"(none)\"}`);\r\n console.log(` Bloom: ${token.bloom_level}`);\r\n }\r\n });\r\n });\r\n\r\n// ── zam token find ────────────────────────────────────────────────────────\r\n\r\ntokenCommand\r\n .command(\"find\")\r\n .description(\"Fuzzy search for tokens\")\r\n .requiredOption(\"--query <query>\", \"Search query\")\r\n .option(\"--json\", \"Output as JSON\")\r\n .action((opts) => {\r\n withDb((db) => {\r\n const results = findTokens(db, opts.query);\r\n\r\n if (opts.json) {\r\n console.log(JSON.stringify(results, null, 2));\r\n return;\r\n }\r\n\r\n if (results.length === 0) {\r\n console.log(\"No tokens found.\");\r\n return;\r\n }\r\n\r\n console.log(`Found ${results.length} token(s):\\n`);\r\n console.log(\r\n \"Score Slug Concept Domain Bloom\",\r\n );\r\n console.log(\"─\".repeat(90));\r\n for (const t of results) {\r\n console.log(\r\n `${String(t.score).padEnd(6)} ${t.slug.padEnd(21)} ${t.concept.slice(0, 31).padEnd(31)} ${(t.domain || \"-\").padEnd(11)} ${t.bloom_level}`,\r\n );\r\n }\r\n });\r\n });\r\n\r\n// ── zam token list ────────────────────────────────────────────────────────\r\n\r\ntokenCommand\r\n .command(\"list\")\r\n .description(\"List all tokens\")\r\n .option(\"--domain <domain>\", \"Filter by domain\")\r\n .option(\"--json\", \"Output as JSON\")\r\n .action((opts) => {\r\n withDb((db) => {\r\n const tokens = listTokens(db, opts.domain ? { domain: opts.domain } : undefined);\r\n\r\n if (opts.json) {\r\n console.log(JSON.stringify(tokens, null, 2));\r\n return;\r\n }\r\n\r\n if (tokens.length === 0) {\r\n console.log(\"No tokens registered.\");\r\n return;\r\n }\r\n\r\n console.log(\r\n \"Slug Concept Domain Bloom\",\r\n );\r\n console.log(\"─\".repeat(80));\r\n for (const t of tokens) {\r\n console.log(\r\n `${t.slug.padEnd(21)} ${t.concept.slice(0, 31).padEnd(31)} ${(t.domain || \"-\").padEnd(11)} ${t.bloom_level}`,\r\n );\r\n }\r\n console.log(`\\n${tokens.length} token(s) total.`);\r\n });\r\n });\r\n\r\n// ── zam token prereq ─────────────────────────────────────────────────────\r\n\r\ntokenCommand\r\n .command(\"prereq\")\r\n .description(\"Add a prerequisite edge between tokens\")\r\n .requiredOption(\"--token <slug>\", \"Token that requires a prerequisite\")\r\n .requiredOption(\"--requires <slug>\", \"Required prerequisite token\")\r\n .option(\"--json\", \"Output as JSON\")\r\n .action((opts) => {\r\n withDb((db) => {\r\n const token = getTokenBySlug(db, opts.token);\r\n if (!token) {\r\n console.error(`Token not found: ${opts.token}`);\r\n process.exit(1);\r\n }\r\n\r\n const requires = getTokenBySlug(db, opts.requires);\r\n if (!requires) {\r\n console.error(`Prerequisite token not found: ${opts.requires}`);\r\n process.exit(1);\r\n }\r\n\r\n addPrerequisite(db, token.id, requires.id);\r\n\r\n if (opts.json) {\r\n console.log(JSON.stringify({ token: opts.token, requires: opts.requires }, null, 2));\r\n } else {\r\n console.log(`Added prerequisite: ${opts.token} requires ${opts.requires}`);\r\n }\r\n });\r\n });\r\n\r\n// ── zam token deprecate ───────────────────────────────────────────────────\r\n\r\ntokenCommand\r\n .command(\"deprecate\")\r\n .description(\"Mark a token as deprecated (excluded from reviews, not deleted)\")\r\n .requiredOption(\"--slug <slug>\", \"Token slug to deprecate\")\r\n .option(\"--json\", \"Output as JSON\")\r\n .action((opts) => {\r\n withDb((db) => {\r\n const token = deprecateToken(db, opts.slug);\r\n\r\n if (opts.json) {\r\n console.log(JSON.stringify(token, null, 2));\r\n } else {\r\n console.log(`Deprecated: ${token.slug}`);\r\n console.log(` Concept: ${token.concept}`);\r\n console.log(` At: ${token.deprecated_at}`);\r\n }\r\n });\r\n });\r\n\r\n// ── zam token status ─────────────────────────────────────────────────────\r\n\r\ntokenCommand\r\n .command(\"status\")\r\n .description(\"Show full status of a token for a user\")\r\n .requiredOption(\"--token <slug>\", \"Token slug\")\r\n .option(\"--user <id>\", \"User ID (default: whoami)\")\r\n .option(\"--json\", \"Output as JSON\")\r\n .action((opts) => {\r\n withDb((db) => {\r\n const userId = resolveUser(opts, db);\r\n const token = getTokenBySlug(db, opts.token);\r\n if (!token) {\r\n console.error(`Token not found: ${opts.token}`);\r\n process.exit(1);\r\n }\r\n\r\n const card = getCard(db, token.id, userId);\r\n const prereqs = getPrerequisites(db, token.id);\r\n const dependents = getDependents(db, token.id);\r\n\r\n const status = {\r\n token,\r\n card: card ?? null,\r\n prerequisites: prereqs,\r\n dependents,\r\n };\r\n\r\n if (opts.json) {\r\n console.log(JSON.stringify(status, null, 2));\r\n return;\r\n }\r\n\r\n console.log(`Token: ${token.slug} (${token.id})`);\r\n console.log(` Concept: ${token.concept}`);\r\n console.log(` Domain: ${token.domain || \"(none)\"}`);\r\n console.log(` Bloom: ${token.bloom_level}`);\r\n console.log();\r\n\r\n if (card) {\r\n console.log(\"Card status:\");\r\n console.log(` State: ${card.state}`);\r\n console.log(` Due at: ${card.due_at}`);\r\n console.log(` Stability: ${card.stability}`);\r\n console.log(` Difficulty: ${card.difficulty}`);\r\n console.log(` Reps: ${card.reps}`);\r\n console.log(` Lapses: ${card.lapses}`);\r\n console.log(` Blocked: ${card.blocked ? \"Yes\" : \"No\"}`);\r\n } else {\r\n console.log(\"No card exists for this user yet.\");\r\n }\r\n\r\n console.log();\r\n if (prereqs.length > 0) {\r\n console.log(\"Prerequisites:\");\r\n for (const p of prereqs) {\r\n console.log(` - ${p.slug}: ${p.concept} (bloom ${p.bloom_level})`);\r\n }\r\n } else {\r\n console.log(\"No prerequisites.\");\r\n }\r\n\r\n if (dependents.length > 0) {\r\n console.log(\"\\nDependents:\");\r\n for (const d of dependents) {\r\n console.log(` - ${d.slug}: ${d.concept} (bloom ${d.bloom_level})`);\r\n }\r\n }\r\n });\r\n });\r\n","/**\r\n * Resolve the active user ID from --user flag or stored whoami setting.\r\n */\r\n\r\nimport type { Database } from \"libsql\";\r\nimport { getSetting } from \"../../kernel/index.js\";\r\n\r\nexport interface ResolveUserOptions {\r\n /** If true, output JSON error instead of console.error (for bridge commands). */\r\n json?: boolean;\r\n}\r\n\r\n/**\r\n * Returns the user ID from the explicit --user flag, or falls back to the\r\n * stored `user.id` setting. Exits with an error if neither is available.\r\n */\r\nexport function resolveUser(\r\n opts: { user?: string },\r\n db: Database,\r\n resolveOpts?: ResolveUserOptions,\r\n): string {\r\n if (opts.user) return opts.user;\r\n\r\n const stored = getSetting(db, \"user.id\");\r\n if (stored) return stored;\r\n\r\n const message = 'No user specified. Set a default with: zam whoami --set <id>';\r\n if (resolveOpts?.json) {\r\n console.log(JSON.stringify({ error: message }, null, 2));\r\n } else {\r\n console.error(message);\r\n }\r\n process.exit(1);\r\n}\r\n","/**\r\n * `zam card` — Card scheduling subcommand group.\r\n */\r\n\r\nimport { Command } from \"commander\";\r\nimport type { Database } from \"libsql\";\r\nimport {\r\n openDatabase,\r\n getDueCards,\r\n getTokenBySlug,\r\n ensureCard,\r\n evaluateRating,\r\n cascadeBlock,\r\n unblockReady,\r\n getPrerequisites,\r\n} from \"../../kernel/index.js\";\r\nimport type { Rating } from \"../../kernel/index.js\";\r\nimport { resolveUser } from \"./resolve-user.js\";\r\n\r\nfunction withDb(fn: (db: Database) => void): void {\r\n let db: Database | undefined;\r\n try {\r\n db = openDatabase();\r\n fn(db);\r\n } catch (err) {\r\n console.error(\"Error:\", (err as Error).message);\r\n process.exit(1);\r\n } finally {\r\n db?.close();\r\n }\r\n}\r\n\r\nexport const cardCommand = new Command(\"card\")\r\n .description(\"Manage spaced-repetition cards\");\r\n\r\n// ── zam card due ──────────────────────────────────────────────────────────\r\n\r\ncardCommand\r\n .command(\"due\")\r\n .description(\"Show due tokens for a user\")\r\n .option(\"--user <id>\", \"User ID (default: whoami)\")\r\n .option(\"--json\", \"Output as JSON\")\r\n .option(\"--summary\", \"Show only counts per domain (no slugs or concepts)\")\r\n .action((opts) => {\r\n withDb((db) => {\r\n const userId = resolveUser(opts, db);\r\n const dueCards = getDueCards(db, userId);\r\n\r\n if (opts.json) {\r\n console.log(JSON.stringify(dueCards, null, 2));\r\n return;\r\n }\r\n\r\n if (dueCards.length === 0) {\r\n console.log(\"No cards due for review.\");\r\n return;\r\n }\r\n\r\n if (opts.summary) {\r\n const byDomain = new Map<string, { count: number; blooms: number[] }>();\r\n for (const c of dueCards) {\r\n const d = c.domain || \"general\";\r\n const entry = byDomain.get(d) ?? { count: 0, blooms: [] };\r\n entry.count++;\r\n entry.blooms.push(c.bloom_level);\r\n byDomain.set(d, entry);\r\n }\r\n console.log(`${dueCards.length} card(s) due:\\n`);\r\n console.log(\r\n \"Domain Count Bloom levels\",\r\n );\r\n console.log(\"─\".repeat(45));\r\n for (const [domain, { count, blooms }] of [...byDomain.entries()].sort()) {\r\n const bloomStr = blooms.sort().join(\", \");\r\n console.log(\r\n `${domain.padEnd(16)} ${String(count).padEnd(6)} ${bloomStr}`,\r\n );\r\n }\r\n return;\r\n }\r\n\r\n console.log(`${dueCards.length} card(s) due:\\n`);\r\n console.log(\r\n \"Slug Concept Domain Bloom State\",\r\n );\r\n console.log(\"─\".repeat(90));\r\n for (const c of dueCards) {\r\n console.log(\r\n `${c.slug.padEnd(21)} ${c.concept.slice(0, 31).padEnd(31)} ${(c.domain || \"-\").padEnd(11)} ${String(c.bloom_level).padEnd(6)} ${c.state}`,\r\n );\r\n }\r\n });\r\n });\r\n\r\n// ── zam card update ───────────────────────────────────────────────────────\r\n\r\ncardCommand\r\n .command(\"update\")\r\n .description(\"Apply a rating to a card\")\r\n .option(\"--user <id>\", \"User ID (default: whoami)\")\r\n .requiredOption(\"--token <slug>\", \"Token slug\")\r\n .requiredOption(\"--rating <n>\", \"Rating (1=Again, 2=Hard, 3=Good, 4=Easy)\")\r\n .option(\"--json\", \"Output as JSON\")\r\n .option(\"--quiet\", \"Suppress output (exit code only)\")\r\n .action((opts) => {\r\n withDb((db) => {\r\n const userId = resolveUser(opts, db);\r\n const token = getTokenBySlug(db, opts.token);\r\n if (!token) {\r\n console.error(`Token not found: ${opts.token}`);\r\n process.exit(1);\r\n }\r\n\r\n const card = ensureCard(db, token.id, userId);\r\n const rating = Number(opts.rating) as Rating;\r\n\r\n if (rating < 1 || rating > 4) {\r\n console.error(\"Rating must be between 1 and 4.\");\r\n process.exit(1);\r\n }\r\n\r\n const result = evaluateRating(db, {\r\n cardId: card.id,\r\n tokenId: token.id,\r\n userId,\r\n rating,\r\n });\r\n\r\n // If rating is 1 (forgot) and token has prerequisites, cascade block\r\n if (rating === 1) {\r\n const prereqs = getPrerequisites(db, token.id);\r\n if (prereqs.length > 0) {\r\n const blockResult = cascadeBlock(db, userId, token.slug);\r\n if (opts.quiet) return;\r\n if (opts.json) {\r\n console.log(JSON.stringify({ evaluation: result, blocked: blockResult }, null, 2));\r\n } else {\r\n console.log(`Rated ${token.slug} as Again (1) — next due: ${result.nextDueAt}`);\r\n console.log(`Blocked ${blockResult.blockedSlug}. Prerequisites surfaced:`);\r\n for (const p of blockResult.prerequisites) {\r\n console.log(` - ${p.slug}: ${p.concept}`);\r\n }\r\n }\r\n return;\r\n }\r\n }\r\n\r\n if (opts.quiet) return;\r\n if (opts.json) {\r\n console.log(JSON.stringify(result, null, 2));\r\n } else {\r\n const ratingLabels: Record<number, string> = { 1: \"Again\", 2: \"Hard\", 3: \"Good\", 4: \"Easy\" };\r\n console.log(`Rated ${token.slug} as ${ratingLabels[rating]} (${rating})`);\r\n console.log(` Next due: ${result.nextDueAt}`);\r\n console.log(` Stability: ${result.stability.toFixed(2)}`);\r\n console.log(` State: ${result.state}`);\r\n console.log(` Reps: ${result.reps}`);\r\n }\r\n });\r\n });\r\n\r\n// ── zam card unblock ──────────────────────────────────────────────────────\r\n\r\ncardCommand\r\n .command(\"unblock\")\r\n .description(\"Unblock cards whose prerequisites are met\")\r\n .option(\"--user <id>\", \"User ID (default: whoami)\")\r\n .option(\"--json\", \"Output as JSON\")\r\n .option(\"--quiet\", \"Suppress output (exit code only)\")\r\n .action((opts) => {\r\n withDb((db) => {\r\n const userId = resolveUser(opts, db);\r\n const result = unblockReady(db, userId);\r\n\r\n if (opts.quiet) return;\r\n if (opts.json) {\r\n console.log(JSON.stringify(result, null, 2));\r\n return;\r\n }\r\n\r\n if (result.unblocked.length === 0) {\r\n console.log(\"No cards ready to unblock.\");\r\n } else {\r\n console.log(`Unblocked ${result.unblocked.length} card(s):`);\r\n for (const u of result.unblocked) {\r\n console.log(` - ${u.slug}: ${u.concept}`);\r\n }\r\n }\r\n });\r\n });\r\n","/**\r\n * `zam session` — Session management subcommand group.\r\n *\r\n * Session start follows the two-phase flow from Increment 2:\r\n * Phase 1 — Repetition: review due cards (pure recall first, skippable)\r\n * Phase 2 — Task execution: pick a work item from ADO or enter a custom task\r\n */\r\n\r\nimport { Command } from \"commander\";\r\nimport type { Database } from \"libsql\";\r\nimport { select, input } from \"@inquirer/prompts\";\r\nimport {\r\n openDatabase,\r\n startSession,\r\n logStep,\r\n endSession,\r\n getSessionSummary,\r\n getTokenBySlug,\r\n loadADOConfig,\r\n fetchActiveWorkItems,\r\n buildReviewQueue,\r\n generatePrompt,\r\n evaluateRating,\r\n cascadeBlock,\r\n getPrerequisites,\r\n getSetting,\r\n} from \"../../kernel/index.js\";\r\nimport type { ExecutionContext, Rating, BloomLevel } from \"../../kernel/index.js\";\r\nimport { resolveUser } from \"./resolve-user.js\";\r\n\r\nfunction withDb(fn: (db: Database) => void): void {\r\n let db: Database | undefined;\r\n try {\r\n db = openDatabase();\r\n fn(db);\r\n } catch (err) {\r\n console.error(\"Error:\", (err as Error).message);\r\n process.exit(1);\r\n } finally {\r\n db?.close();\r\n }\r\n}\r\n\r\nexport const sessionCommand = new Command(\"session\")\r\n .description(\"Manage learning sessions\");\r\n\r\n// ── zam session start ─────────────────────────────────────────────────────\r\n\r\nsessionCommand\r\n .command(\"start\")\r\n .description(\"Start a new learning session (review → task)\")\r\n .option(\"--user <id>\", \"User ID (default: whoami)\")\r\n .option(\"--task <description>\", \"Task description (interactive if omitted)\")\r\n .option(\"--context <level>\", \"Execution context: shell | ui | reallife (default: shell)\", \"shell\")\r\n .option(\"--skip-review\", \"Skip the repetition phase and go straight to task selection\")\r\n .option(\"--review-minutes <n>\", \"Maximum minutes for the repetition phase (default: 20)\", \"20\")\r\n .option(\"--json\", \"Output as JSON\")\r\n .option(\"--quiet\", \"Output only the session ID\")\r\n .action(async (opts) => {\r\n let db: Database | undefined;\r\n try {\r\n db = openDatabase();\r\n\r\n const validContexts = [\"shell\", \"ui\", \"reallife\"];\r\n if (!validContexts.includes(opts.context)) {\r\n console.error(`Invalid context: ${opts.context}. Must be one of: ${validContexts.join(\", \")}`);\r\n process.exit(1);\r\n }\r\n\r\n const userId = resolveUser(opts, db);\r\n const reviewMinutes = Number(opts.reviewMinutes);\r\n\r\n // ── Phase 1: Repetition ────────────────────────────────────────────\r\n if (!opts.skipReview && !opts.quiet && !opts.json) {\r\n const reviewResults = await runRepetitionPhase(db, userId, reviewMinutes);\r\n if (reviewResults.reviewed > 0) {\r\n console.log();\r\n }\r\n }\r\n\r\n // ── Phase 2: Task Selection ────────────────────────────────────────\r\n let task: string = opts.task;\r\n\r\n if (!task && !opts.quiet && !opts.json) {\r\n task = await selectTask(db);\r\n }\r\n\r\n if (!task) {\r\n // Fallback for --quiet/--json without --task\r\n console.error(\"Task description is required. Use --task or run interactively.\");\r\n process.exit(1);\r\n }\r\n\r\n const session = startSession(db, {\r\n user_id: userId,\r\n task,\r\n execution_context: opts.context as ExecutionContext,\r\n });\r\n\r\n db.close();\r\n\r\n if (opts.quiet) {\r\n console.log(session.id);\r\n } else if (opts.json) {\r\n console.log(JSON.stringify(session, null, 2));\r\n } else {\r\n console.log(`\\nSession started: ${session.id}`);\r\n console.log(` User: ${session.user_id}`);\r\n console.log(` Task: ${session.task}`);\r\n console.log(` Context: ${session.execution_context}`);\r\n console.log(` Started: ${session.started_at}`);\r\n }\r\n } catch (err) {\r\n db?.close();\r\n if ((err as Error).name === \"ExitPromptError\") {\r\n console.log(\"\\nSession cancelled.\");\r\n process.exit(0);\r\n }\r\n console.error(\"Error:\", (err as Error).message);\r\n process.exit(1);\r\n }\r\n });\r\n\r\n// ── Phase 1: Repetition ─────────────────────────────────────────────────────\r\n\r\ninterface RepetitionResult {\r\n reviewed: number;\r\n skipped: boolean;\r\n}\r\n\r\nasync function runRepetitionPhase(\r\n db: Database,\r\n userId: string,\r\n maxMinutes: number,\r\n): Promise<RepetitionResult> {\r\n const queue = buildReviewQueue(db, { userId });\r\n\r\n if (queue.items.length === 0) {\r\n console.log(\"No cards due for review — moving to task selection.\\n\");\r\n return { reviewed: 0, skipped: false };\r\n }\r\n\r\n console.log(\"═\".repeat(50));\r\n console.log(\"Phase 1: Repetition\");\r\n console.log(\"═\".repeat(50));\r\n console.log(`${queue.items.length} card(s) due`);\r\n console.log(` New: ${queue.newCount} Review: ${queue.reviewCount} Relearn: ${queue.relearnCount}`);\r\n console.log(` Domains: ${queue.totalDomains.join(\", \")}`);\r\n console.log(` Time limit: ${maxMinutes} minutes (skip anytime with 's')`);\r\n console.log();\r\n\r\n const startTime = Date.now();\r\n const timeLimitMs = maxMinutes * 60 * 1000;\r\n let reviewed = 0;\r\n\r\n for (const item of queue.items) {\r\n // Check time limit\r\n if (Date.now() - startTime >= timeLimitMs) {\r\n console.log(`\\nTime limit reached (${maxMinutes} min). Moving to task selection.`);\r\n break;\r\n }\r\n\r\n reviewed++;\r\n\r\n const prompt = generatePrompt({\r\n cardId: item.cardId,\r\n tokenId: item.tokenId,\r\n slug: item.slug,\r\n concept: item.concept,\r\n domain: item.domain,\r\n bloomLevel: item.bloomLevel as BloomLevel,\r\n });\r\n\r\n const elapsed = Math.round((Date.now() - startTime) / 60000);\r\n console.log(`[${reviewed}/${queue.items.length}] ${prompt.bloomVerb} (Bloom ${prompt.bloomLevel}) — ${elapsed}/${maxMinutes} min`);\r\n console.log(`Domain: ${prompt.domain || \"(none)\"}`);\r\n console.log(`\\n ${prompt.question}\\n`);\r\n\r\n const rating = await select({\r\n message: \"How did you do?\",\r\n choices: [\r\n { name: \"1 - Again (forgot)\", value: 1 },\r\n { name: \"2 - Hard\", value: 2 },\r\n { name: \"3 - Good\", value: 3 },\r\n { name: \"4 - Easy\", value: 4 },\r\n { name: \"s - Skip to task selection\", value: 0 },\r\n ],\r\n }) as number;\r\n\r\n if (rating === 0) {\r\n console.log(\"Skipping to task selection.\");\r\n reviewed--; // Don't count the skipped card\r\n return { reviewed, skipped: true };\r\n }\r\n\r\n const evalResult = evaluateRating(db, {\r\n cardId: item.cardId,\r\n tokenId: item.tokenId,\r\n userId,\r\n rating: rating as Rating,\r\n });\r\n\r\n if (rating === 1) {\r\n const prereqs = getPrerequisites(db, item.tokenId);\r\n if (prereqs.length > 0) {\r\n const blockResult = cascadeBlock(db, userId, item.slug);\r\n console.log(` Blocked ${blockResult.blockedSlug}. Review these prerequisites:`);\r\n for (const p of blockResult.prerequisites) {\r\n console.log(` - ${p.slug}: ${p.concept}`);\r\n }\r\n }\r\n }\r\n\r\n const ratingLabels: Record<number, string> = { 1: \"Again\", 2: \"Hard\", 3: \"Good\", 4: \"Easy\" };\r\n console.log(` ${ratingLabels[rating]} — next due: ${evalResult.nextDueAt}\\n`);\r\n }\r\n\r\n if (reviewed > 0) {\r\n console.log(\"─\".repeat(50));\r\n console.log(`Repetition complete — ${reviewed} card(s) reviewed.`);\r\n }\r\n\r\n return { reviewed, skipped: false };\r\n}\r\n\r\n// ── Phase 2: Task Selection ─────────────────────────────────────────────────\r\n\r\nasync function selectTask(db: Database): Promise<string> {\r\n console.log(\"═\".repeat(50));\r\n console.log(\"Phase 2: Task Selection\");\r\n console.log(\"═\".repeat(50));\r\n\r\n const adoConfig = loadADOConfig(db);\r\n\r\n if (adoConfig) {\r\n const items = await fetchActiveWorkItems(adoConfig);\r\n\r\n if (items.length > 0) {\r\n const choices = items.map((wi) => ({\r\n name: `[${wi.type}] ${wi.title} (${wi.state})`,\r\n value: `[ADO-${wi.id}] ${wi.title}`,\r\n }));\r\n choices.push({ name: \"Enter a custom task...\", value: \"__custom__\" });\r\n\r\n const picked = await select({\r\n message: `${items.length} active work item(s) — pick one:`,\r\n choices,\r\n });\r\n\r\n if (picked !== \"__custom__\") return picked;\r\n } else {\r\n console.log(\"No active work items found in Azure DevOps.\");\r\n }\r\n }\r\n\r\n return input({ message: \"Task description:\" });\r\n}\r\n\r\n// ── zam session log ───────────────────────────────────────────────────────\r\n\r\nsessionCommand\r\n .command(\"log\")\r\n .description(\"Log a step within a session\")\r\n .requiredOption(\"--session <id>\", \"Session ID\")\r\n .requiredOption(\"--token <slug>\", \"Token slug\")\r\n .requiredOption(\"--done-by <who>\", \"Who performed the step (user or agent)\")\r\n .option(\"--rating <n>\", \"Rating (1-4)\")\r\n .option(\"--json\", \"Output as JSON\")\r\n .option(\"--quiet\", \"Suppress output (exit code only)\")\r\n .action((opts) => {\r\n withDb((db) => {\r\n const token = getTokenBySlug(db, opts.token);\r\n if (!token) {\r\n console.error(`Token not found: ${opts.token}`);\r\n process.exit(1);\r\n }\r\n\r\n const step = logStep(db, {\r\n session_id: opts.session,\r\n token_id: token.id,\r\n done_by: opts.doneBy as \"user\" | \"agent\",\r\n rating: opts.rating ? Number(opts.rating) : undefined,\r\n });\r\n\r\n if (opts.quiet) return;\r\n if (opts.json) {\r\n console.log(JSON.stringify(step, null, 2));\r\n } else {\r\n console.log(`Step logged: ${step.id}`);\r\n console.log(` Token: ${opts.token}`);\r\n console.log(` Done by: ${step.done_by}`);\r\n if (step.rating != null) {\r\n console.log(` Rating: ${step.rating}`);\r\n }\r\n }\r\n });\r\n });\r\n\r\n// ── zam session end ───────────────────────────────────────────────────────\r\n\r\nsessionCommand\r\n .command(\"end\")\r\n .description(\"End a session and show summary\")\r\n .requiredOption(\"--session <id>\", \"Session ID\")\r\n .option(\"--json\", \"Output as JSON\")\r\n .action((opts) => {\r\n withDb((db) => {\r\n endSession(db, opts.session);\r\n const summary = getSessionSummary(db, opts.session);\r\n\r\n if (opts.json) {\r\n console.log(JSON.stringify(summary, null, 2));\r\n return;\r\n }\r\n\r\n console.log(`Session ${summary.session.id} completed.`);\r\n console.log(` Task: ${summary.session.task}`);\r\n console.log(` Started: ${summary.session.started_at}`);\r\n console.log(` Completed: ${summary.session.completed_at}`);\r\n console.log(` Steps: ${summary.steps.length}`);\r\n\r\n if (summary.steps.length > 0) {\r\n console.log(\"\\nSteps:\");\r\n console.log(\r\n \" Token Done by Rating Concept\",\r\n );\r\n console.log(\" \" + \"─\".repeat(70));\r\n for (const s of summary.steps) {\r\n console.log(\r\n ` ${s.slug.padEnd(21)} ${s.done_by.padEnd(8)} ${String(s.rating ?? \"-\").padEnd(7)} ${s.concept.slice(0, 30)}`,\r\n );\r\n }\r\n }\r\n });\r\n });\r\n","/**\r\n * `zam stats` — Learning dashboard.\r\n */\r\n\r\nimport { Command } from \"commander\";\r\nimport type { Database } from \"libsql\";\r\nimport {\r\n openDatabase,\r\n getUserStats,\r\n getDomainCompetence,\r\n} from \"../../kernel/index.js\";\r\nimport { resolveUser } from \"./resolve-user.js\";\r\n\r\nfunction withDb(fn: (db: Database) => void): void {\r\n let db: Database | undefined;\r\n try {\r\n db = openDatabase();\r\n fn(db);\r\n } catch (err) {\r\n console.error(\"Error:\", (err as Error).message);\r\n process.exit(1);\r\n } finally {\r\n db?.close();\r\n }\r\n}\r\n\r\nexport const statsCommand = new Command(\"stats\")\r\n .description(\"Show learning dashboard for a user\")\r\n .option(\"--user <id>\", \"User ID (default: whoami)\")\r\n .option(\"--json\", \"Output as JSON\")\r\n .action((opts) => {\r\n withDb((db) => {\r\n const userId = resolveUser(opts, db);\r\n const stats = getUserStats(db, userId);\r\n const domains = getDomainCompetence(db, userId);\r\n\r\n if (opts.json) {\r\n console.log(JSON.stringify({ stats, domains }, null, 2));\r\n return;\r\n }\r\n\r\n console.log(`Learning Dashboard — ${stats.userId}`);\r\n console.log(\"═\".repeat(50));\r\n console.log(` Total tokens: ${stats.totalTokens}`);\r\n console.log(` Cards in deck: ${stats.cardsInDeck}`);\r\n console.log(` Due today: ${stats.dueToday}`);\r\n console.log(` Blocked: ${stats.blocked}`);\r\n console.log(` Mature: ${stats.mature}`);\r\n console.log(` Avg stability: ${stats.avgStability ?? \"N/A\"}`);\r\n console.log(` Total sessions: ${stats.totalSessions}`);\r\n console.log(` Last session: ${stats.lastSession ?? \"N/A\"}`);\r\n\r\n if (domains.length > 0) {\r\n console.log(\"\\nDomain Competence:\");\r\n console.log(\"─\".repeat(80));\r\n console.log(\r\n \" Domain Cards Mature Stability Retention Suggested Mode\",\r\n );\r\n console.log(\" \" + \"─\".repeat(74));\r\n for (const d of domains) {\r\n console.log(\r\n ` ${d.domain.padEnd(17)} ${String(d.totalCards).padEnd(6)} ${String(d.matureCards).padEnd(7)} ${String(d.avgStability).padEnd(10)} ${(d.retentionRate * 100).toFixed(1).padStart(5)}% ${d.suggestedMode}`,\r\n );\r\n }\r\n }\r\n });\r\n });\r\n","/**\r\n * `zam review` — Interactive review session.\r\n */\r\n\r\nimport { Command } from \"commander\";\r\nimport type { Database } from \"libsql\";\r\nimport { select, input } from \"@inquirer/prompts\";\r\nimport {\r\n openDatabase,\r\n buildReviewQueue,\r\n generatePrompt,\r\n evaluateRating,\r\n cascadeBlock,\r\n getPrerequisites,\r\n} from \"../../kernel/index.js\";\r\nimport type { Rating, BloomLevel } from \"../../kernel/index.js\";\r\nimport { resolveUser } from \"./resolve-user.js\";\r\n\r\nexport const reviewCommand = new Command(\"review\")\r\n .description(\"Start an interactive review session\")\r\n .option(\"--user <id>\", \"User ID (default: whoami)\")\r\n .option(\"--max-new <n>\", \"Maximum new cards\", \"10\")\r\n .option(\"--max-reviews <n>\", \"Maximum review cards\", \"50\")\r\n .action(async (opts) => {\r\n let db: Database | undefined;\r\n try {\r\n db = openDatabase();\r\n const userId = resolveUser(opts, db);\r\n\r\n const queue = buildReviewQueue(db, {\r\n userId,\r\n maxNew: Number(opts.maxNew),\r\n maxReviews: Number(opts.maxReviews),\r\n });\r\n\r\n if (queue.items.length === 0) {\r\n console.log(\"No cards due for review. You're all caught up!\");\r\n db.close();\r\n return;\r\n }\r\n\r\n console.log(`\\nReview session: ${queue.items.length} card(s)`);\r\n console.log(` New: ${queue.newCount} Review: ${queue.reviewCount} Relearn: ${queue.relearnCount}`);\r\n console.log(` Domains: ${queue.totalDomains.join(\", \")}`);\r\n console.log();\r\n\r\n let completed = 0;\r\n const results: Array<{\r\n slug: string;\r\n rating: number;\r\n nextDue: string;\r\n }> = [];\r\n\r\n for (const item of queue.items) {\r\n completed++;\r\n\r\n const prompt = generatePrompt({\r\n cardId: item.cardId,\r\n tokenId: item.tokenId,\r\n slug: item.slug,\r\n concept: item.concept,\r\n domain: item.domain,\r\n bloomLevel: item.bloomLevel as BloomLevel,\r\n });\r\n\r\n console.log(`\\n[${ completed }/${queue.items.length}] ${prompt.bloomVerb} (Bloom ${prompt.bloomLevel})`);\r\n console.log(`Domain: ${prompt.domain || \"(none)\"}`);\r\n console.log(`\\n ${prompt.question}\\n`);\r\n\r\n const rating = await select({\r\n message: \"How did you do?\",\r\n choices: [\r\n { name: \"1 - Again (forgot)\", value: 1 },\r\n { name: \"2 - Hard\", value: 2 },\r\n { name: \"3 - Good\", value: 3 },\r\n { name: \"4 - Easy\", value: 4 },\r\n ],\r\n }) as Rating;\r\n\r\n const evalResult = evaluateRating(db, {\r\n cardId: item.cardId,\r\n tokenId: item.tokenId,\r\n userId,\r\n rating,\r\n });\r\n\r\n // If rating 1 and token has prereqs, cascade block\r\n if (rating === 1) {\r\n const prereqs = getPrerequisites(db, item.tokenId);\r\n if (prereqs.length > 0) {\r\n const blockResult = cascadeBlock(db, userId, item.slug);\r\n console.log(` Blocked ${blockResult.blockedSlug}. Review these prerequisites:`);\r\n for (const p of blockResult.prerequisites) {\r\n console.log(` - ${p.slug}: ${p.concept}`);\r\n }\r\n }\r\n }\r\n\r\n const ratingLabels: Record<number, string> = { 1: \"Again\", 2: \"Hard\", 3: \"Good\", 4: \"Easy\" };\r\n console.log(` ${ratingLabels[rating]} — next due: ${evalResult.nextDueAt}`);\r\n\r\n results.push({\r\n slug: item.slug,\r\n rating,\r\n nextDue: evalResult.nextDueAt,\r\n });\r\n }\r\n\r\n // Session summary\r\n console.log(\"\\n\" + \"═\".repeat(50));\r\n console.log(\"Review session complete!\");\r\n console.log(` Cards reviewed: ${results.length}`);\r\n\r\n const avgRating = results.reduce((s, r) => s + r.rating, 0) / results.length;\r\n console.log(` Average rating: ${avgRating.toFixed(1)}`);\r\n\r\n const forgot = results.filter((r) => r.rating === 1).length;\r\n if (forgot > 0) {\r\n console.log(` Forgot: ${forgot} card(s)`);\r\n }\r\n\r\n db.close();\r\n } catch (err) {\r\n db?.close();\r\n // User cancelled with Ctrl+C — exit gracefully\r\n if ((err as Error).name === \"ExitPromptError\") {\r\n console.log(\"\\nReview session cancelled.\");\r\n process.exit(0);\r\n }\r\n console.error(\"Error:\", (err as Error).message);\r\n process.exit(1);\r\n }\r\n });\r\n","/**\r\n * `zam bridge` — Machine-readable JSON protocol for AI integration.\r\n *\r\n * All output is valid JSON only. No human-readable formatting.\r\n * Errors are also JSON: { \"error\": \"message\" }\r\n */\r\n\r\nimport { Command } from \"commander\";\r\nimport type { Database } from \"libsql\";\r\nimport {\r\n openDatabase,\r\n getDueCards,\r\n buildReviewQueue,\r\n generatePrompt,\r\n evaluateRating,\r\n ensureCard,\r\n createToken,\r\n getTokenBySlug,\r\n cascadeBlock,\r\n getPrerequisites,\r\n getAgentSkill,\r\n listAgentSkills,\r\n readMonitorLog,\r\n pairCommands,\r\n analyzeObservation,\r\n monitorLogExists,\r\n discoverSkills,\r\n} from \"../../kernel/index.js\";\r\nimport type { Rating, BloomLevel, TokenPattern } from \"../../kernel/index.js\";\r\nimport { readdirSync } from \"node:fs\";\r\nimport { homedir } from \"node:os\";\r\nimport { join } from \"node:path\";\r\nimport { resolveUser } from \"./resolve-user.js\";\r\n\r\nfunction jsonOut(data: unknown): void {\r\n console.log(JSON.stringify(data, null, 2));\r\n}\r\n\r\nfunction jsonError(message: string): never {\r\n console.log(JSON.stringify({ error: message }, null, 2));\r\n process.exit(1);\r\n}\r\n\r\nfunction withDb(fn: (db: Database) => void): void {\r\n let db: Database | undefined;\r\n try {\r\n db = openDatabase();\r\n fn(db);\r\n } catch (err) {\r\n db?.close();\r\n jsonError((err as Error).message);\r\n } finally {\r\n db?.close();\r\n }\r\n}\r\n\r\nexport const bridgeCommand = new Command(\"bridge\")\r\n .description(\"Machine-readable JSON protocol for AI integration\");\r\n\r\n// ── zam bridge check-due ──────────────────────────────────────────────────\r\n\r\nbridgeCommand\r\n .command(\"check-due\")\r\n .description(\"Check due cards for a user (JSON)\")\r\n .option(\"--user <id>\", \"User ID (default: whoami)\")\r\n .action((opts) => {\r\n withDb((db) => {\r\n const userId = resolveUser(opts, db, { json: true });\r\n const dueCards = getDueCards(db, userId);\r\n const domains = [...new Set(dueCards.map((c) => c.domain).filter(Boolean))].sort();\r\n\r\n jsonOut({\r\n userId,\r\n dueCount: dueCards.length,\r\n domains,\r\n cards: dueCards.map((c) => ({\r\n cardId: c.id,\r\n tokenId: c.token_id,\r\n slug: c.slug,\r\n concept: c.concept,\r\n domain: c.domain,\r\n bloomLevel: c.bloom_level,\r\n state: c.state,\r\n dueAt: c.due_at,\r\n })),\r\n });\r\n });\r\n });\r\n\r\n// ── zam bridge get-review ─────────────────────────────────────────────────\r\n\r\nbridgeCommand\r\n .command(\"get-review\")\r\n .description(\"Get next review card with prompt (JSON)\")\r\n .option(\"--user <id>\", \"User ID (default: whoami)\")\r\n .action((opts) => {\r\n withDb((db) => {\r\n const userId = resolveUser(opts, db, { json: true });\r\n const queue = buildReviewQueue(db, { userId, maxReviews: 1, maxNew: 1 });\r\n\r\n if (queue.items.length === 0) {\r\n jsonOut({\r\n userId,\r\n hasReview: false,\r\n card: null,\r\n prompt: null,\r\n queueSize: 0,\r\n });\r\n return;\r\n }\r\n\r\n const item = queue.items[0];\r\n const prompt = generatePrompt({\r\n cardId: item.cardId,\r\n tokenId: item.tokenId,\r\n slug: item.slug,\r\n concept: item.concept,\r\n domain: item.domain,\r\n bloomLevel: item.bloomLevel as BloomLevel,\r\n });\r\n\r\n // Get full queue size for context\r\n const fullQueue = buildReviewQueue(db, { userId });\r\n\r\n jsonOut({\r\n userId,\r\n hasReview: true,\r\n card: item,\r\n prompt,\r\n queueSize: fullQueue.items.length,\r\n });\r\n });\r\n });\r\n\r\n// ── zam bridge submit ─────────────────────────────────────────────────────\r\n\r\nbridgeCommand\r\n .command(\"submit\")\r\n .description(\"Submit a rating for a card (JSON)\")\r\n .option(\"--user <id>\", \"User ID (default: whoami)\")\r\n .requiredOption(\"--card-id <id>\", \"Card ID\")\r\n .requiredOption(\"--rating <n>\", \"Rating (1-4)\")\r\n .action((opts) => {\r\n withDb((db) => {\r\n const userId = resolveUser(opts, db, { json: true });\r\n const rating = Number(opts.rating) as Rating;\r\n if (rating < 1 || rating > 4) {\r\n jsonError(\"Rating must be between 1 and 4\");\r\n }\r\n\r\n // Look up the card to get tokenId\r\n const card = db\r\n .prepare(\"SELECT * FROM cards WHERE id = ?\")\r\n .get(opts.cardId) as { id: string; token_id: string; user_id: string } | undefined;\r\n\r\n if (!card) {\r\n jsonError(`Card not found: ${opts.cardId}`);\r\n }\r\n\r\n const result = evaluateRating(db, {\r\n cardId: opts.cardId,\r\n tokenId: card!.token_id,\r\n userId,\r\n rating,\r\n });\r\n\r\n let blocked = null;\r\n if (rating === 1) {\r\n const token = db\r\n .prepare(\"SELECT slug FROM tokens WHERE id = ?\")\r\n .get(card!.token_id) as { slug: string } | undefined;\r\n\r\n if (token) {\r\n const prereqs = getPrerequisites(db, card!.token_id);\r\n if (prereqs.length > 0) {\r\n blocked = cascadeBlock(db, userId, token.slug);\r\n }\r\n }\r\n }\r\n\r\n jsonOut({\r\n success: true,\r\n rating,\r\n evaluation: result,\r\n blocked,\r\n });\r\n });\r\n });\r\n\r\n// ── zam bridge get-skill ──────────────────────────────────────────────────\r\n\r\nbridgeCommand\r\n .command(\"get-skill\")\r\n .description(\"Get an agent skill by slug (JSON)\")\r\n .requiredOption(\"--slug <slug>\", \"Skill slug\")\r\n .action((opts) => {\r\n withDb((db) => {\r\n const skill = getAgentSkill(db, opts.slug);\r\n if (!skill) {\r\n jsonError(`Skill not found: ${opts.slug}`);\r\n }\r\n\r\n jsonOut({\r\n slug: skill!.slug,\r\n description: skill!.description,\r\n steps: skill!.steps,\r\n tokenSlugs: skill!.token_slugs,\r\n source: skill!.source,\r\n });\r\n });\r\n });\r\n\r\n// ── zam bridge get-monitor ────────────────────────────────────────────────\r\n\r\nbridgeCommand\r\n .command(\"get-monitor\")\r\n .description(\"Read monitor log for a session (JSON)\")\r\n .requiredOption(\"--session <id>\", \"Session ID\")\r\n .action((opts) => {\r\n if (!monitorLogExists(opts.session)) {\r\n jsonOut({ sessionId: opts.session, exists: false, commands: [], timeSpan: null });\r\n return;\r\n }\r\n\r\n const events = readMonitorLog(opts.session);\r\n const commands = pairCommands(events);\r\n\r\n let timeSpan: { start: string; end: string; durationMs: number } | null = null;\r\n if (commands.length > 0) {\r\n const first = commands[0];\r\n const last = commands[commands.length - 1];\r\n const endTs = last.endedAt ?? last.startedAt;\r\n timeSpan = {\r\n start: first.startedAt,\r\n end: endTs,\r\n durationMs: new Date(endTs).getTime() - new Date(first.startedAt).getTime(),\r\n };\r\n }\r\n\r\n jsonOut({\r\n sessionId: opts.session,\r\n exists: true,\r\n commands: commands.map((c) => ({\r\n seq: c.seq,\r\n command: c.command,\r\n cwd: c.cwd,\r\n startedAt: c.startedAt,\r\n endedAt: c.endedAt,\r\n durationMs: c.durationMs,\r\n exitCode: c.exitCode,\r\n })),\r\n timeSpan,\r\n });\r\n });\r\n\r\n// ── zam bridge analyze-monitor ───────────────────────────────────────────\r\n\r\nbridgeCommand\r\n .command(\"analyze-monitor\")\r\n .description(\"Analyze monitor log with token patterns from stdin (JSON)\")\r\n .requiredOption(\"--session <id>\", \"Session ID\")\r\n .action(async (opts) => {\r\n try {\r\n if (!monitorLogExists(opts.session)) {\r\n jsonOut({ sessionId: opts.session, ratings: [], unmatchedCommands: [], timeSpan: null });\r\n return;\r\n }\r\n\r\n // Read token patterns from stdin\r\n const chunks: Buffer[] = [];\r\n for await (const chunk of process.stdin) {\r\n chunks.push(chunk as Buffer);\r\n }\r\n const raw = Buffer.concat(chunks).toString(\"utf-8\").trim();\r\n\r\n if (!raw) {\r\n jsonError(\"No input received on stdin. Pipe JSON with token patterns.\");\r\n }\r\n\r\n let data: { patterns: TokenPattern[] };\r\n try {\r\n data = JSON.parse(raw);\r\n } catch {\r\n jsonError(\"Invalid JSON input\");\r\n }\r\n\r\n if (!Array.isArray(data!.patterns)) {\r\n jsonError(\"JSON must include 'patterns' array\");\r\n }\r\n\r\n const events = readMonitorLog(opts.session);\r\n const commands = pairCommands(events);\r\n const result = analyzeObservation(commands, data!.patterns);\r\n\r\n jsonOut({\r\n sessionId: opts.session,\r\n ...result,\r\n });\r\n } catch (err) {\r\n jsonError((err as Error).message);\r\n }\r\n });\r\n\r\n// ── zam bridge add-token ──────────────────────────────────────────────────\r\n\r\nbridgeCommand\r\n .command(\"add-token\")\r\n .description(\"Create a token + card from JSON stdin\")\r\n .option(\"--user <id>\", \"User ID (default: whoami)\")\r\n .action(async (opts) => {\r\n let db: Database | undefined;\r\n try {\r\n // Read JSON from stdin\r\n const chunks: Buffer[] = [];\r\n for await (const chunk of process.stdin) {\r\n chunks.push(chunk as Buffer);\r\n }\r\n const raw = Buffer.concat(chunks).toString(\"utf-8\").trim();\r\n\r\n if (!raw) {\r\n jsonError(\"No input received on stdin. Pipe JSON with token data.\");\r\n }\r\n\r\n let data: {\r\n slug: string;\r\n concept: string;\r\n domain?: string;\r\n bloom_level?: number;\r\n context?: string;\r\n symbiosis_mode?: string | null;\r\n };\r\n\r\n try {\r\n data = JSON.parse(raw);\r\n } catch {\r\n jsonError(\"Invalid JSON input\");\r\n }\r\n\r\n if (!data!.slug || !data!.concept) {\r\n jsonError(\"JSON must include 'slug' and 'concept' fields\");\r\n }\r\n\r\n db = openDatabase();\r\n const userId = resolveUser(opts, db, { json: true });\r\n\r\n const token = createToken(db, {\r\n slug: data!.slug,\r\n concept: data!.concept,\r\n domain: data!.domain,\r\n bloom_level: (data!.bloom_level ?? 1) as BloomLevel,\r\n context: data!.context,\r\n symbiosis_mode: data!.symbiosis_mode as \"shadowing\" | \"copilot\" | \"autonomy\" | null | undefined,\r\n });\r\n\r\n const card = ensureCard(db, token.id, userId);\r\n\r\n jsonOut({\r\n success: true,\r\n token,\r\n card: {\r\n id: card.id,\r\n tokenId: card.token_id,\r\n userId: card.user_id,\r\n state: card.state,\r\n dueAt: card.due_at,\r\n blocked: card.blocked,\r\n },\r\n });\r\n\r\n db.close();\r\n } catch (err) {\r\n db?.close();\r\n // If it's already a JSON error exit, let it propagate\r\n if ((err as Error).message) {\r\n jsonError((err as Error).message);\r\n }\r\n }\r\n });\r\n\r\n// ── zam bridge discover-skills ──────────────────────────────────────────────\r\n\r\nbridgeCommand\r\n .command(\"discover-skills\")\r\n .description(\"Analyze monitor logs across sessions to discover recurring patterns\")\r\n .option(\"--min-sessions <n>\", \"Minimum sessions a pattern must appear in (default: 2)\", \"2\")\r\n .option(\"--limit <n>\", \"Max number of sessions to analyze (default: 20)\", \"20\")\r\n .action((opts) => {\r\n try {\r\n const monitorDir = join(homedir(), \".zam\", \"monitor\");\r\n let files: string[];\r\n try {\r\n files = readdirSync(monitorDir).filter((f) => f.endsWith(\".jsonl\"));\r\n } catch {\r\n jsonOut({ proposals: [], message: \"No monitor logs found.\" });\r\n return;\r\n }\r\n\r\n if (files.length === 0) {\r\n jsonOut({ proposals: [], message: \"No monitor logs found.\" });\r\n return;\r\n }\r\n\r\n // Take the most recent N sessions by file modification time\r\n const limit = Number(opts.limit);\r\n const sorted = files\r\n .map((f) => ({ name: f, path: join(monitorDir, f) }))\r\n .sort((a, b) => b.name.localeCompare(a.name)) // ULID session IDs sort chronologically\r\n .slice(0, limit);\r\n\r\n // Load and parse each session's commands\r\n const sessionCommands = new Map<string, ReturnType<typeof pairCommands>>();\r\n for (const file of sorted) {\r\n const sessionId = file.name.replace(\".jsonl\", \"\");\r\n const events = readMonitorLog(sessionId);\r\n const commands = pairCommands(events);\r\n if (commands.length > 0) {\r\n sessionCommands.set(sessionId, commands);\r\n }\r\n }\r\n\r\n if (sessionCommands.size === 0) {\r\n jsonOut({ proposals: [], message: \"No command data in monitor logs.\" });\r\n return;\r\n }\r\n\r\n // Get existing skills to exclude\r\n let existingSkillSlugs: string[] = [];\r\n let db;\r\n try {\r\n db = openDatabase();\r\n existingSkillSlugs = listAgentSkills(db).map((s) => s.slug);\r\n } catch {\r\n // DB not available — proceed without exclusion\r\n } finally {\r\n db?.close();\r\n }\r\n\r\n const proposals = discoverSkills(sessionCommands, {\r\n minSessions: Number(opts.minSessions),\r\n existingSkillSlugs,\r\n });\r\n\r\n jsonOut({\r\n sessionsAnalyzed: sessionCommands.size,\r\n proposals,\r\n });\r\n } catch (err) {\r\n jsonError((err as Error).message);\r\n }\r\n });\r\n","/**\r\n * `zam skill` — Manage agent skill entries (task recipes).\r\n */\r\n\r\nimport { Command } from \"commander\";\r\nimport type { Database } from \"libsql\";\r\nimport {\r\n openDatabase,\r\n createAgentSkill,\r\n getAgentSkill,\r\n listAgentSkills,\r\n} from \"../../kernel/index.js\";\r\n\r\nfunction withDb(fn: (db: Database) => void): void {\r\n let db: Database | undefined;\r\n try {\r\n db = openDatabase();\r\n fn(db);\r\n } catch (err) {\r\n console.error(\"Error:\", (err as Error).message);\r\n process.exit(1);\r\n } finally {\r\n db?.close();\r\n }\r\n}\r\n\r\nexport const skillCommand = new Command(\"skill\")\r\n .description(\"Manage agent skill entries (task recipes)\");\r\n\r\n// ── zam skill list ────────────────────────────────────────────────────────\r\n\r\nskillCommand\r\n .command(\"list\")\r\n .description(\"List all agent skills\")\r\n .option(\"--json\", \"Output as JSON\")\r\n .action((opts) => {\r\n withDb((db) => {\r\n const skills = listAgentSkills(db);\r\n\r\n if (opts.json) {\r\n console.log(JSON.stringify(skills, null, 2));\r\n return;\r\n }\r\n\r\n if (skills.length === 0) {\r\n console.log(\"No agent skills registered yet.\");\r\n return;\r\n }\r\n\r\n console.log(`Agent Skills (${skills.length})`);\r\n console.log(\"─\".repeat(60));\r\n for (const s of skills) {\r\n console.log(` ${s.slug.padEnd(30)} [${s.source}] ${s.description.slice(0, 40)}`);\r\n console.log(` ${s.steps.length} step(s) tokens: ${s.token_slugs.join(\", \") || \"none\"}`);\r\n }\r\n });\r\n });\r\n\r\n// ── zam skill show ────────────────────────────────────────────────────────\r\n\r\nskillCommand\r\n .command(\"show\")\r\n .description(\"Show a specific agent skill\")\r\n .requiredOption(\"--slug <slug>\", \"Skill slug\")\r\n .option(\"--json\", \"Output as JSON\")\r\n .action((opts) => {\r\n withDb((db) => {\r\n const skill = getAgentSkill(db, opts.slug);\r\n if (!skill) {\r\n console.error(`Skill not found: ${opts.slug}`);\r\n process.exit(1);\r\n }\r\n\r\n if (opts.json) {\r\n console.log(JSON.stringify(skill, null, 2));\r\n return;\r\n }\r\n\r\n console.log(`Skill: ${skill.slug}`);\r\n console.log(` Description: ${skill.description}`);\r\n console.log(` Source: ${skill.source}`);\r\n console.log(` Tokens: ${skill.token_slugs.join(\", \") || \"none\"}`);\r\n console.log(` Created: ${skill.created_at}`);\r\n console.log(`\\nSteps:`);\r\n skill.steps.forEach((step, i) => {\r\n console.log(` ${i + 1}. ${step}`);\r\n });\r\n });\r\n });\r\n\r\n// ── zam skill add ─────────────────────────────────────────────────────────\r\n\r\nskillCommand\r\n .command(\"add\")\r\n .description(\"Register a new agent skill\")\r\n .requiredOption(\"--slug <slug>\", \"Unique skill identifier\")\r\n .requiredOption(\"--description <text>\", \"One-sentence description\")\r\n .requiredOption(\"--steps <json>\", \"JSON array of step strings\")\r\n .option(\"--tokens <slugs>\", \"Comma-separated token slugs related to this skill\")\r\n .option(\"--source <type>\", \"Source: learned | builtin (default: learned)\", \"learned\")\r\n .option(\"--json\", \"Output as JSON\")\r\n .action((opts) => {\r\n withDb((db) => {\r\n let steps: string[];\r\n try {\r\n steps = JSON.parse(opts.steps) as string[];\r\n if (!Array.isArray(steps)) throw new Error(\"steps must be a JSON array\");\r\n } catch {\r\n console.error(\"Invalid --steps: must be a valid JSON array of strings\");\r\n process.exit(1);\r\n }\r\n\r\n const tokenSlugs = opts.tokens\r\n ? opts.tokens.split(\",\").map((s: string) => s.trim()).filter(Boolean)\r\n : [];\r\n\r\n const skill = createAgentSkill(db, {\r\n slug: opts.slug,\r\n description: opts.description,\r\n steps,\r\n token_slugs: tokenSlugs,\r\n source: opts.source as \"learned\" | \"builtin\",\r\n });\r\n\r\n if (opts.json) {\r\n console.log(JSON.stringify(skill, null, 2));\r\n } else {\r\n console.log(`Skill registered: ${skill.slug}`);\r\n console.log(` ${skill.steps.length} step(s) saved`);\r\n }\r\n });\r\n });\r\n","/**\r\n * `zam monitor` — Shell observation for real-time task monitoring.\r\n *\r\n * Installs shell hooks (zsh/bash) that capture commands with timing,\r\n * exit codes, and working directory to a JSONL file. The agent reads\r\n * this log to infer ratings for knowledge tokens.\r\n *\r\n * Usage:\r\n * eval \"$(zam monitor start --session <id>)\" # install hooks\r\n * eval \"$(zam monitor stop --session <id>)\" # remove hooks\r\n * zam monitor status --session <id> # check log stats\r\n */\r\n\r\nimport { Command } from \"commander\";\r\nimport { basename, join } from \"node:path\";\r\nimport { execSync } from \"node:child_process\";\r\nimport { writeFileSync, unlinkSync } from \"node:fs\";\r\nimport { tmpdir } from \"node:os\";\r\nimport type { Database } from \"libsql\";\r\nimport {\r\n openDatabase,\r\n ensureMonitorDir,\r\n getMonitorPath,\r\n writeMonitorEvent,\r\n readMonitorLog,\r\n pairCommands,\r\n monitorLogExists,\r\n getMonitorLogStats,\r\n generateZshHooks,\r\n generateBashHooks,\r\n generateZshUnhooks,\r\n generateBashUnhooks,\r\n getSetting,\r\n setSetting,\r\n} from \"../../kernel/index.js\";\r\nimport type { MonitorEvent } from \"../../kernel/index.js\";\r\n\r\nfunction detectShell(): \"zsh\" | \"bash\" {\r\n const shell = process.env.SHELL ?? \"\";\r\n return basename(shell) === \"bash\" ? \"bash\" : \"zsh\";\r\n}\r\n\r\nexport const monitorCommand = new Command(\"monitor\")\r\n .description(\"Shell observation for real-time task monitoring\");\r\n\r\n// ── zam monitor start ─────────────────────────────────────────────────────\r\n\r\nmonitorCommand\r\n .command(\"start\")\r\n .description(\"Output shell hook code to install monitoring (wrap with eval)\")\r\n .requiredOption(\"--session <id>\", \"Session ID to monitor\")\r\n .option(\"--shell <type>\", \"Shell type: zsh | bash (auto-detected from $SHELL)\")\r\n .action((opts) => {\r\n // Validate session exists\r\n let db: Database | undefined;\r\n try {\r\n db = openDatabase();\r\n const session = db\r\n .prepare(\"SELECT id, completed_at FROM sessions WHERE id = ?\")\r\n .get(opts.session) as { id: string; completed_at: string | null } | undefined;\r\n\r\n if (!session) {\r\n console.error(`# Error: Session not found: ${opts.session}`);\r\n process.exit(1);\r\n }\r\n if (session.completed_at) {\r\n console.error(`# Error: Session already completed: ${opts.session}`);\r\n process.exit(1);\r\n }\r\n } catch (err) {\r\n console.error(`# Error: ${(err as Error).message}`);\r\n process.exit(1);\r\n } finally {\r\n db?.close();\r\n }\r\n\r\n ensureMonitorDir();\r\n const monitorFile = getMonitorPath(opts.session);\r\n\r\n // Write initial meta event\r\n const meta: MonitorEvent = {\r\n type: \"monitor_meta\",\r\n ts: new Date().toISOString(),\r\n event: \"start\",\r\n session_id: opts.session,\r\n shell: opts.shell ?? detectShell(),\r\n pid: process.pid,\r\n };\r\n writeMonitorEvent(opts.session, meta);\r\n\r\n // Output hook code to stdout\r\n const shell = opts.shell ?? detectShell();\r\n if (shell === \"bash\") {\r\n console.log(generateBashHooks(monitorFile, opts.session));\r\n } else {\r\n console.log(generateZshHooks(monitorFile, opts.session));\r\n }\r\n });\r\n\r\n// ── zam monitor stop ──────────────────────────────────────────────────────\r\n\r\nmonitorCommand\r\n .command(\"stop\")\r\n .description(\"Output shell code to remove monitoring hooks (wrap with eval)\")\r\n .requiredOption(\"--session <id>\", \"Session ID\")\r\n .option(\"--shell <type>\", \"Shell type: zsh | bash (auto-detected from $SHELL)\")\r\n .action((opts) => {\r\n // Write stop meta event\r\n if (monitorLogExists(opts.session)) {\r\n const meta: MonitorEvent = {\r\n type: \"monitor_meta\",\r\n ts: new Date().toISOString(),\r\n event: \"stop\",\r\n session_id: opts.session,\r\n };\r\n writeMonitorEvent(opts.session, meta);\r\n }\r\n\r\n const shell = opts.shell ?? detectShell();\r\n if (shell === \"bash\") {\r\n console.log(generateBashUnhooks());\r\n } else {\r\n console.log(generateZshUnhooks());\r\n }\r\n });\r\n\r\n// ── zam monitor status ────────────────────────────────────────────────────\r\n\r\nmonitorCommand\r\n .command(\"status\")\r\n .description(\"Show monitoring status for a session\")\r\n .requiredOption(\"--session <id>\", \"Session ID\")\r\n .option(\"--json\", \"Output as JSON\")\r\n .action((opts) => {\r\n const stats = getMonitorLogStats(opts.session);\r\n\r\n if (!stats.exists) {\r\n if (opts.json) {\r\n console.log(JSON.stringify({ exists: false }));\r\n } else {\r\n console.log(`No monitor log found for session ${opts.session}`);\r\n }\r\n return;\r\n }\r\n\r\n const events = readMonitorLog(opts.session);\r\n const commands = pairCommands(events);\r\n const errors = commands.filter((c) => c.exitCode != null && c.exitCode !== 0).length;\r\n\r\n const meta = events.find((e) => e.type === \"monitor_meta\" && e.event === \"start\");\r\n const stopped = events.some((e) => e.type === \"monitor_meta\" && e.event === \"stop\");\r\n\r\n const result = {\r\n sessionId: opts.session,\r\n exists: true,\r\n active: !stopped,\r\n shell: meta?.shell ?? \"unknown\",\r\n totalCommands: commands.length,\r\n errors,\r\n sizeBytes: stats.sizeBytes,\r\n timeSpan: commands.length > 0\r\n ? {\r\n start: commands[0].startedAt,\r\n end: commands[commands.length - 1].endedAt ?? commands[commands.length - 1].startedAt,\r\n }\r\n : null,\r\n };\r\n\r\n if (opts.json) {\r\n console.log(JSON.stringify(result, null, 2));\r\n return;\r\n }\r\n\r\n console.log(`Monitor: ${opts.session}`);\r\n console.log(` Status: ${result.active ? \"active\" : \"stopped\"}`);\r\n console.log(` Shell: ${result.shell}`);\r\n console.log(` Commands: ${result.totalCommands}`);\r\n console.log(` Errors: ${result.errors}`);\r\n if (result.timeSpan) {\r\n console.log(` From: ${result.timeSpan.start}`);\r\n console.log(` To: ${result.timeSpan.end}`);\r\n }\r\n });\r\n\r\n// ── zam monitor open ─────────────────────────────────────────────────────\r\n\r\n/**\r\n * Resolve the `zam` binary path — built CLI if available, otherwise tsx source.\r\n * This ensures the eval in the spawned terminal uses the correct entrypoint.\r\n */\r\nfunction resolveZamBin(): string {\r\n try {\r\n // Prefer the built CLI (installed via npm link or global install)\r\n const which = execSync(\"which zam 2>/dev/null\", { encoding: \"utf-8\" }).trim();\r\n if (which) return which;\r\n } catch {\r\n // not installed globally\r\n }\r\n // Fallback: use absolute path to the built CLI or source\r\n const projectRoot = join(import.meta.dirname, \"..\", \"..\", \"..\");\r\n return `npx --prefix ${JSON.stringify(projectRoot)} tsx ${join(projectRoot, \"src/cli/index.ts\")}`;\r\n}\r\n\r\n/**\r\n * Detect whether iTerm2 is running (preferred on macOS).\r\n */\r\nfunction isItermRunning(): boolean {\r\n try {\r\n const result = execSync(\r\n \"osascript -e 'tell application \\\"System Events\\\" to (name of processes) contains \\\"iTerm2\\\"' 2>/dev/null\",\r\n { encoding: \"utf-8\" },\r\n ).trim();\r\n return result === \"true\";\r\n } catch {\r\n return false;\r\n }\r\n}\r\n\r\nmonitorCommand\r\n .command(\"open\")\r\n .description(\"Open a new monitored terminal window for a session\")\r\n .requiredOption(\"--session <id>\", \"Session ID to monitor\")\r\n .option(\"--dir <path>\", \"Working directory (defaults to cwd)\")\r\n .option(\"--shell <type>\", \"Shell type: zsh | bash (auto-detected from $SHELL)\")\r\n .action((opts) => {\r\n // Validate session exists\r\n let db: Database | undefined;\r\n try {\r\n db = openDatabase();\r\n const session = db\r\n .prepare(\"SELECT id, completed_at FROM sessions WHERE id = ?\")\r\n .get(opts.session) as { id: string; completed_at: string | null } | undefined;\r\n\r\n if (!session) {\r\n console.error(`Error: Session not found: ${opts.session}`);\r\n process.exit(1);\r\n }\r\n if (session.completed_at) {\r\n console.error(`Error: Session already completed: ${opts.session}`);\r\n process.exit(1);\r\n }\r\n\r\n // Save monitor preference so the agent knows to default to terminal\r\n if (!getSetting(db, \"monitor_method\")) {\r\n setSetting(db, \"monitor_method\", \"terminal\");\r\n }\r\n } catch (err) {\r\n console.error(`Error: ${(err as Error).message}`);\r\n process.exit(1);\r\n } finally {\r\n db?.close();\r\n }\r\n\r\n const dir = opts.dir ?? process.cwd();\r\n const zamBin = resolveZamBin();\r\n const shellSetup = `cd ${JSON.stringify(dir)} && eval \"$(${zamBin} monitor start --session ${opts.session})\"`;\r\n\r\n if (process.platform === \"darwin\") {\r\n openMacTerminal(shellSetup, opts.session, dir);\r\n } else {\r\n console.log(`Run this in a new terminal:\\n`);\r\n console.log(` ${shellSetup}\\n`);\r\n console.log(`(Automatic terminal opening is only supported on macOS for now.)`);\r\n }\r\n });\r\n\r\n/**\r\n * Open a macOS terminal window via AppleScript.\r\n * Uses a temp .scpt file to avoid shell quoting hell.\r\n */\r\nfunction openMacTerminal(shellSetup: string, sessionId: string, dir: string): void {\r\n const useIterm = isItermRunning();\r\n const escaped = shellSetup.replace(/\\\\/g, \"\\\\\\\\\").replace(/\"/g, '\\\\\"');\r\n\r\n const appleScript = useIterm\r\n ? `tell application \"iTerm2\"\r\n activate\r\n set newWindow to (create window with default profile)\r\n tell current session of newWindow\r\n write text \"${escaped}\"\r\n end tell\r\nend tell`\r\n : `tell application \"Terminal\"\r\n activate\r\n do script \"${escaped}\"\r\nend tell`;\r\n\r\n const tmpFile = join(tmpdir(), `zam-monitor-${sessionId}.scpt`);\r\n try {\r\n writeFileSync(tmpFile, appleScript);\r\n execSync(`osascript ${JSON.stringify(tmpFile)}`, { stdio: \"ignore\" });\r\n console.log(`Opened ${useIterm ? \"iTerm2\" : \"Terminal.app\"} window with monitoring for session ${sessionId}`);\r\n console.log(` Directory: ${dir}`);\r\n } catch (err) {\r\n console.error(`Failed to open terminal: ${(err as Error).message}`);\r\n console.log(`\\nRun this manually in a new terminal:\\n`);\r\n console.log(` ${shellSetup}`);\r\n } finally {\r\n try { unlinkSync(tmpFile); } catch { /* ignore */ }\r\n }\r\n}\r\n","/**\r\n * `zam settings` — User settings management.\r\n */\r\n\r\nimport { Command } from \"commander\";\r\nimport type { Database } from \"libsql\";\r\nimport {\r\n openDatabase,\r\n getSetting,\r\n getAllSettings,\r\n getAllSettingsDetailed,\r\n setSetting,\r\n deleteSetting,\r\n} from \"../../kernel/index.js\";\r\n\r\nfunction withDb(fn: (db: Database) => void): void {\r\n let db: Database | undefined;\r\n try {\r\n db = openDatabase();\r\n fn(db);\r\n } catch (err) {\r\n console.error(\"Error:\", (err as Error).message);\r\n process.exit(1);\r\n } finally {\r\n db?.close();\r\n }\r\n}\r\n\r\nexport const settingsCommand = new Command(\"settings\")\r\n .description(\"Manage user settings\");\r\n\r\n// ── zam settings show ─────────────────────────────────────────────────────\r\n\r\nsettingsCommand\r\n .command(\"show\")\r\n .description(\"Show all settings\")\r\n .option(\"--json\", \"Output as JSON\")\r\n .action((opts) => {\r\n withDb((db) => {\r\n if (opts.json) {\r\n console.log(JSON.stringify(getAllSettings(db), null, 2));\r\n return;\r\n }\r\n\r\n const settings = getAllSettingsDetailed(db);\r\n if (settings.length === 0) {\r\n console.log(\"No settings configured.\");\r\n return;\r\n }\r\n\r\n console.log(\"Settings:\\n\");\r\n console.log(\"Key Value Updated\");\r\n console.log(\"─\".repeat(65));\r\n for (const s of settings) {\r\n console.log(\r\n `${s.key.padEnd(20)} ${s.value.padEnd(20)} ${s.updated_at}`,\r\n );\r\n }\r\n });\r\n });\r\n\r\n// ── zam settings get ──────────────────────────────────────────────────────\r\n\r\nsettingsCommand\r\n .command(\"get\")\r\n .description(\"Get a single setting\")\r\n .requiredOption(\"--key <key>\", \"Setting key\")\r\n .option(\"--json\", \"Output as JSON\")\r\n .action((opts) => {\r\n withDb((db) => {\r\n const value = getSetting(db, opts.key);\r\n\r\n if (opts.json) {\r\n console.log(JSON.stringify({ key: opts.key, value: value ?? null }));\r\n return;\r\n }\r\n\r\n if (value === undefined) {\r\n console.log(`Not set: ${opts.key}`);\r\n } else {\r\n console.log(value);\r\n }\r\n });\r\n });\r\n\r\n// ── zam settings set ──────────────────────────────────────────────────────\r\n\r\nsettingsCommand\r\n .command(\"set\")\r\n .description(\"Set a setting\")\r\n .requiredOption(\"--key <key>\", \"Setting key\")\r\n .requiredOption(\"--value <value>\", \"Setting value\")\r\n .option(\"--quiet\", \"Suppress output\")\r\n .action((opts) => {\r\n withDb((db) => {\r\n setSetting(db, opts.key, opts.value);\r\n if (!opts.quiet) {\r\n console.log(`Set ${opts.key} = ${opts.value}`);\r\n }\r\n });\r\n });\r\n\r\n// ── zam settings delete ───────────────────────────────────────────────────\r\n\r\nsettingsCommand\r\n .command(\"delete\")\r\n .description(\"Delete a setting\")\r\n .requiredOption(\"--key <key>\", \"Setting key\")\r\n .option(\"--quiet\", \"Suppress output\")\r\n .action((opts) => {\r\n withDb((db) => {\r\n const deleted = deleteSetting(db, opts.key);\r\n if (!opts.quiet) {\r\n if (deleted) {\r\n console.log(`Deleted: ${opts.key}`);\r\n } else {\r\n console.log(`Not found: ${opts.key}`);\r\n }\r\n }\r\n });\r\n });\r\n","/**\r\n * `zam whoami` — Manage default user identity.\r\n */\r\n\r\nimport { Command } from \"commander\";\r\nimport type { Database } from \"libsql\";\r\nimport {\r\n openDatabase,\r\n getSetting,\r\n setSetting,\r\n deleteSetting,\r\n} from \"../../kernel/index.js\";\r\n\r\nfunction withDb(fn: (db: Database) => void): void {\r\n let db: Database | undefined;\r\n try {\r\n db = openDatabase();\r\n fn(db);\r\n } catch (err) {\r\n console.error(\"Error:\", (err as Error).message);\r\n process.exit(1);\r\n } finally {\r\n db?.close();\r\n }\r\n}\r\n\r\nexport const whoamiCommand = new Command(\"whoami\")\r\n .description(\"Show or set the default user identity\")\r\n .option(\"--set <id>\", \"Set the default user ID\")\r\n .option(\"--clear\", \"Remove the default user ID\")\r\n .option(\"--json\", \"Output as JSON\")\r\n .action((opts) => {\r\n withDb((db) => {\r\n if (opts.set) {\r\n setSetting(db, \"user.id\", opts.set);\r\n if (opts.json) {\r\n console.log(JSON.stringify({ userId: opts.set }));\r\n } else {\r\n console.log(`Default user set to: ${opts.set}`);\r\n }\r\n return;\r\n }\r\n\r\n if (opts.clear) {\r\n const deleted = deleteSetting(db, \"user.id\");\r\n if (opts.json) {\r\n console.log(JSON.stringify({ userId: null, cleared: deleted }));\r\n } else if (deleted) {\r\n console.log(\"Default user cleared.\");\r\n } else {\r\n console.log(\"No default user was set.\");\r\n }\r\n return;\r\n }\r\n\r\n const userId = getSetting(db, \"user.id\");\r\n if (opts.json) {\r\n console.log(JSON.stringify({ userId: userId ?? null }));\r\n return;\r\n }\r\n\r\n if (userId) {\r\n console.log(userId);\r\n } else {\r\n console.log(\"No default user set. Use: zam whoami --set <id>\");\r\n }\r\n });\r\n });\r\n","/**\r\n * `zam connector` — Manage external service connectors.\r\n */\r\n\r\nimport { Command } from \"commander\";\r\nimport type { Database } from \"libsql\";\r\nimport { input, password } from \"@inquirer/prompts\";\r\nimport {\r\n openDatabase,\r\n openDatabaseWithSync,\r\n getSetting,\r\n setSetting,\r\n deleteSetting,\r\n} from \"../../kernel/index.js\";\r\nimport {\r\n loadADOConfig,\r\n fetchActiveWorkItems,\r\n} from \"../../kernel/connectors/azure-devops.js\";\r\n\r\nfunction withDb(fn: (db: Database) => void): void {\r\n let db: Database | undefined;\r\n try {\r\n db = openDatabase();\r\n fn(db);\r\n } catch (err) {\r\n console.error(\"Error:\", (err as Error).message);\r\n process.exit(1);\r\n } finally {\r\n db?.close();\r\n }\r\n}\r\n\r\nexport const connectorCommand = new Command(\"connector\")\r\n .description(\"Manage external service connectors\");\r\n\r\n// ── zam connector setup ado ─────────────────────────────────────────────────\r\n\r\nconnectorCommand\r\n .command(\"setup\")\r\n .description(\"Configure a connector\")\r\n .argument(\"<type>\", \"Connector type (ado, turso)\")\r\n .action(async (type) => {\r\n if (type === \"turso\") {\r\n return setupTurso();\r\n }\r\n if (type !== \"ado\") {\r\n console.error(`Unknown connector type: ${type}. Supported: ado, turso`);\r\n process.exit(1);\r\n }\r\n\r\n let db: Database | undefined;\r\n try {\r\n const orgUrl = await input({\r\n message: \"Organization URL (e.g. https://dev.azure.com/myorg):\",\r\n });\r\n const project = await input({\r\n message: \"Project name:\",\r\n });\r\n const pat = await password({\r\n message: \"Personal Access Token:\",\r\n });\r\n\r\n if (!orgUrl || !project || !pat) {\r\n console.error(\"All fields are required.\");\r\n process.exit(1);\r\n }\r\n\r\n db = openDatabase();\r\n setSetting(db, \"ado.org_url\", orgUrl.replace(/\\/+$/, \"\"));\r\n setSetting(db, \"ado.project\", project);\r\n setSetting(db, \"ado.pat\", pat);\r\n db.close();\r\n\r\n console.log(`Azure DevOps connector configured for ${orgUrl}/${project}`);\r\n } catch (err) {\r\n db?.close();\r\n if ((err as Error).name === \"ExitPromptError\") {\r\n console.log(\"\\nSetup cancelled.\");\r\n process.exit(0);\r\n }\r\n console.error(\"Error:\", (err as Error).message);\r\n process.exit(1);\r\n }\r\n });\r\n\r\n// ── zam connector tasks ─────────────────────────────────────────────────────\r\n\r\nconnectorCommand\r\n .command(\"tasks\")\r\n .description(\"List active tasks from connected board\")\r\n .option(\"--json\", \"Output as JSON\")\r\n .action(async (opts) => {\r\n let db: Database | undefined;\r\n try {\r\n db = openDatabase();\r\n const config = loadADOConfig(db);\r\n db.close();\r\n\r\n if (!config) {\r\n console.error(\"No connector configured. Run: zam connector setup ado\");\r\n process.exit(1);\r\n }\r\n\r\n const items = await fetchActiveWorkItems(config);\r\n\r\n if (opts.json) {\r\n console.log(JSON.stringify(items, null, 2));\r\n return;\r\n }\r\n\r\n if (items.length === 0) {\r\n console.log(\"No active work items assigned to you.\");\r\n return;\r\n }\r\n\r\n console.log(`${items.length} active work item(s):\\n`);\r\n console.log(\r\n \"ID Type State Title\",\r\n );\r\n console.log(\"─\".repeat(80));\r\n for (const wi of items) {\r\n console.log(\r\n `${String(wi.id).padEnd(8)} ${wi.type.padEnd(13)} ${wi.state.padEnd(11)} ${wi.title.slice(0, 45)}`,\r\n );\r\n }\r\n } catch (err) {\r\n db?.close();\r\n console.error(\"Error:\", (err as Error).message);\r\n process.exit(1);\r\n }\r\n });\r\n\r\n// ── zam connector clear ─────────────────────────────────────────────────────\r\n\r\nconnectorCommand\r\n .command(\"clear\")\r\n .description(\"Remove a connector configuration\")\r\n .argument(\"<type>\", \"Connector type (ado, turso)\")\r\n .action((type) => {\r\n if (type === \"turso\") {\r\n withDb((db) => {\r\n deleteSetting(db, \"turso.url\");\r\n deleteSetting(db, \"turso.token\");\r\n console.log(\"Turso cloud sync removed. Database remains local-only.\");\r\n });\r\n return;\r\n }\r\n\r\n if (type !== \"ado\") {\r\n console.error(`Unknown connector type: ${type}. Supported: ado, turso`);\r\n process.exit(1);\r\n }\r\n\r\n withDb((db) => {\r\n deleteSetting(db, \"ado.org_url\");\r\n deleteSetting(db, \"ado.project\");\r\n deleteSetting(db, \"ado.pat\");\r\n console.log(\"Azure DevOps connector removed.\");\r\n });\r\n });\r\n\r\n// ── zam connector sync ──────────────────────────────────────────────────────\r\n\r\nconnectorCommand\r\n .command(\"sync\")\r\n .description(\"Trigger a manual sync with Turso cloud database\")\r\n .action(() => {\r\n let db: Database | undefined;\r\n try {\r\n db = openDatabaseWithSync();\r\n const url = getSetting(db, \"turso.url\");\r\n if (!url) {\r\n console.error(\"No Turso cloud database configured. Run: zam connector setup turso\");\r\n process.exit(1);\r\n }\r\n (db as unknown as { sync: () => void }).sync();\r\n console.log(`Synced with ${url}`);\r\n db.close();\r\n } catch (err) {\r\n db?.close();\r\n console.error(\"Error:\", (err as Error).message);\r\n process.exit(1);\r\n }\r\n });\r\n\r\n// ── Turso setup helper ──────────────────────────────────────────────────────\r\n\r\nasync function setupTurso(): Promise<void> {\r\n let db: Database | undefined;\r\n try {\r\n const url = await input({\r\n message: \"Turso database URL (e.g. libsql://my-db-user.turso.io):\",\r\n });\r\n const token = await password({\r\n message: \"Auth token:\",\r\n });\r\n\r\n if (!url || !token) {\r\n console.error(\"Both URL and token are required.\");\r\n process.exit(1);\r\n }\r\n\r\n db = openDatabase();\r\n setSetting(db, \"turso.url\", url);\r\n setSetting(db, \"turso.token\", token);\r\n db.close();\r\n\r\n // Verify by opening with sync\r\n db = openDatabaseWithSync();\r\n (db as unknown as { sync: () => void }).sync();\r\n db.close();\r\n\r\n console.log(`Turso cloud sync configured and verified: ${url}`);\r\n } catch (err) {\r\n db?.close();\r\n if ((err as Error).name === \"ExitPromptError\") {\r\n console.log(\"\\nSetup cancelled.\");\r\n process.exit(0);\r\n }\r\n console.error(\"Error:\", (err as Error).message);\r\n process.exit(1);\r\n }\r\n}\r\n","/**\r\n * `zam goal` — Goal management subcommand group.\r\n *\r\n * Goals are markdown files in the personal repo's goals/ directory.\r\n * The directory is configured via the `personal.goals_dir` setting,\r\n * or defaults to `./goals` relative to the current working directory.\r\n */\r\n\r\nimport { Command } from \"commander\";\r\nimport { existsSync, mkdirSync } from \"node:fs\";\r\nimport { resolve } from \"node:path\";\r\nimport { input, select } from \"@inquirer/prompts\";\r\nimport {\r\n openDatabase,\r\n getSetting,\r\n listGoals,\r\n getGoal,\r\n createGoal,\r\n updateGoalStatus,\r\n getGoalTree,\r\n extractTasks,\r\n extractTokenRefs,\r\n} from \"../../kernel/index.js\";\r\nimport type { GoalStatus } from \"../../kernel/index.js\";\r\n\r\nfunction resolveGoalsDir(): string {\r\n let goalsDir: string | undefined;\r\n\r\n let db;\r\n try {\r\n db = openDatabase();\r\n goalsDir = getSetting(db, \"personal.goals_dir\");\r\n } catch {\r\n // DB not available — fall back to default\r\n } finally {\r\n db?.close();\r\n }\r\n\r\n return goalsDir ? resolve(goalsDir) : resolve(\"goals\");\r\n}\r\n\r\nexport const goalCommand = new Command(\"goal\")\r\n .description(\"Manage learning goals (markdown files)\");\r\n\r\n// ── zam goal list ────────────────────────────────────────────────────────────\r\n\r\ngoalCommand\r\n .command(\"list\")\r\n .description(\"List all goals\")\r\n .option(\"--status <status>\", \"Filter by status (active, completed, paused, abandoned)\")\r\n .option(\"--tree\", \"Show goals as a tree with parent/child relationships\")\r\n .option(\"--json\", \"Output as JSON\")\r\n .action((opts) => {\r\n const goalsDir = resolveGoalsDir();\r\n\r\n if (!existsSync(goalsDir)) {\r\n console.error(`Goals directory not found: ${goalsDir}`);\r\n console.error(\"Set it with: zam settings set personal.goals_dir /path/to/goals\");\r\n process.exit(1);\r\n }\r\n\r\n if (opts.tree) {\r\n const tree = getGoalTree(goalsDir);\r\n const filtered = opts.status\r\n ? tree.filter((g) => g.status === opts.status)\r\n : tree;\r\n\r\n if (opts.json) {\r\n console.log(JSON.stringify(filtered, null, 2));\r\n return;\r\n }\r\n\r\n if (filtered.length === 0) {\r\n console.log(\"No goals found.\");\r\n return;\r\n }\r\n\r\n for (const root of filtered) {\r\n printGoalLine(root, 0);\r\n for (const child of root.children) {\r\n printGoalLine(child, 1);\r\n }\r\n }\r\n return;\r\n }\r\n\r\n let goals = listGoals(goalsDir);\r\n\r\n if (opts.status) {\r\n goals = goals.filter((g) => g.status === opts.status);\r\n }\r\n\r\n if (opts.json) {\r\n console.log(JSON.stringify(goals, null, 2));\r\n return;\r\n }\r\n\r\n if (goals.length === 0) {\r\n console.log(\"No goals found.\");\r\n return;\r\n }\r\n\r\n console.log(\"Goals:\");\r\n console.log(\" \" + \"─\".repeat(70));\r\n for (const g of goals) {\r\n printGoalLine(g, 0);\r\n }\r\n });\r\n\r\nfunction printGoalLine(\r\n g: { slug: string; title: string; status: string; taskCount: number; tasksDone: number },\r\n indent: number,\r\n): void {\r\n const prefix = \" \".repeat(indent + 1);\r\n const statusIcon: Record<string, string> = {\r\n active: \"[*]\",\r\n paused: \"[-]\",\r\n completed: \"[x]\",\r\n abandoned: \"[ ]\",\r\n };\r\n const icon = statusIcon[g.status] || \"[ ]\";\r\n const tasks = g.taskCount > 0 ? ` (${g.tasksDone}/${g.taskCount} tasks)` : \"\";\r\n console.log(`${prefix}${icon} ${g.title}${tasks} — ${g.slug}`);\r\n}\r\n\r\n// ── zam goal show ────────────────────────────────────────────────────────────\r\n\r\ngoalCommand\r\n .command(\"show <slug>\")\r\n .description(\"Show a goal's details\")\r\n .option(\"--json\", \"Output as JSON\")\r\n .action((slug, opts) => {\r\n const goalsDir = resolveGoalsDir();\r\n const goal = getGoal(goalsDir, slug);\r\n\r\n if (!goal) {\r\n console.error(`Goal not found: ${slug}`);\r\n process.exit(1);\r\n }\r\n\r\n if (opts.json) {\r\n const tasks = extractTasks(goal.body);\r\n const tokens = extractTokenRefs(goal.body);\r\n console.log(JSON.stringify({ ...goal, tasks, tokens }, null, 2));\r\n return;\r\n }\r\n\r\n console.log(`Title: ${goal.title}`);\r\n console.log(`Slug: ${goal.slug}`);\r\n console.log(`Status: ${goal.status}`);\r\n if (goal.parent) console.log(`Parent: ${goal.parent}`);\r\n console.log(`Created: ${goal.created}`);\r\n console.log(`Updated: ${goal.updated}`);\r\n\r\n const tasks = extractTasks(goal.body);\r\n if (tasks.length > 0) {\r\n console.log(`\\nTasks (${tasks.filter((t) => t.done).length}/${tasks.length}):`);\r\n for (const t of tasks) {\r\n console.log(` [${t.done ? \"x\" : \" \"}] ${t.text}`);\r\n }\r\n }\r\n\r\n const tokens = extractTokenRefs(goal.body);\r\n if (tokens.length > 0) {\r\n console.log(`\\nTokens:`);\r\n for (const ref of tokens) {\r\n console.log(` - ${ref}`);\r\n }\r\n }\r\n\r\n if (goal.body) {\r\n console.log(`\\n${\"─\".repeat(50)}`);\r\n console.log(goal.body);\r\n }\r\n });\r\n\r\n// ── zam goal create ──────────────────────────────────────────────────────────\r\n\r\ngoalCommand\r\n .command(\"create\")\r\n .description(\"Create a new goal\")\r\n .option(\"--slug <slug>\", \"Goal slug (used as filename)\")\r\n .option(\"--title <title>\", \"Goal title\")\r\n .option(\"--parent <slug>\", \"Parent goal slug\")\r\n .option(\"--description <text>\", \"Goal description\")\r\n .option(\"--json\", \"Output as JSON\")\r\n .action(async (opts) => {\r\n const goalsDir = resolveGoalsDir();\r\n\r\n if (!existsSync(goalsDir)) {\r\n mkdirSync(goalsDir, { recursive: true });\r\n }\r\n\r\n let slug = opts.slug;\r\n let title = opts.title;\r\n const parent = opts.parent;\r\n const description = opts.description;\r\n\r\n // Interactive mode if slug or title not provided\r\n if (!slug || !title) {\r\n try {\r\n if (!title) {\r\n title = await input({ message: \"Goal title:\" });\r\n }\r\n if (!slug) {\r\n const suggested = title\r\n .toLowerCase()\r\n .replace(/[^a-z0-9]+/g, \"-\")\r\n .replace(/^-|-$/g, \"\");\r\n slug = await input({\r\n message: \"Goal slug (filename):\",\r\n default: suggested,\r\n });\r\n }\r\n } catch (err) {\r\n if ((err as Error).name === \"ExitPromptError\") {\r\n console.log(\"\\nCancelled.\");\r\n process.exit(0);\r\n }\r\n throw err;\r\n }\r\n }\r\n\r\n const goal = createGoal(goalsDir, { slug, title, parent, description });\r\n\r\n if (opts.json) {\r\n console.log(JSON.stringify(goal, null, 2));\r\n return;\r\n }\r\n\r\n console.log(`Goal created: ${goal.slug}`);\r\n console.log(` Title: ${goal.title}`);\r\n console.log(` Status: ${goal.status}`);\r\n console.log(` File: ${goal.filePath}`);\r\n });\r\n\r\n// ── zam goal status ──────────────────────────────────────────────────────────\r\n\r\ngoalCommand\r\n .command(\"status <slug> <status>\")\r\n .description(\"Update a goal's status (active, paused, completed, abandoned)\")\r\n .option(\"--json\", \"Output as JSON\")\r\n .action((slug, status, opts) => {\r\n const validStatuses: GoalStatus[] = [\"active\", \"completed\", \"paused\", \"abandoned\"];\r\n if (!validStatuses.includes(status)) {\r\n console.error(`Invalid status: ${status}. Must be one of: ${validStatuses.join(\", \")}`);\r\n process.exit(1);\r\n }\r\n\r\n const goalsDir = resolveGoalsDir();\r\n const goal = updateGoalStatus(goalsDir, slug, status);\r\n\r\n if (opts.json) {\r\n console.log(JSON.stringify(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ACF,EACC,QAAQ,OAAO;AAElB,QAAQ,WAAW,WAAW;AAC9B,QAAQ,WAAW,YAAY;AAC/B,QAAQ,WAAW,YAAY;AAC/B,QAAQ,WAAW,WAAW;AAC9B,QAAQ,WAAW,cAAc;AACjC,QAAQ,WAAW,YAAY;AAC/B,QAAQ,WAAW,aAAa;AAChC,QAAQ,WAAW,aAAa;AAChC,QAAQ,WAAW,YAAY;AAC/B,QAAQ,WAAW,cAAc;AACjC,QAAQ,WAAW,eAAe;AAClC,QAAQ,WAAW,aAAa;AAChC,QAAQ,WAAW,gBAAgB;AACnC,QAAQ,WAAW,WAAW;AAE9B,QAAQ,MAAM;","names":["Command","input","ulid","ulid","ulid","input","ulid","input","interval","dueAt","input","ulid","input","ulid","existsSync","mkdirSync","homedir","join","join","homedir","join","existsSync","mkdirSync","readFileSync","existsSync","join","existsSync","join","readFileSync","input","Command","existsSync","mkdirSync","writeFileSync","join","dirname","basename","join","existsSync","mkdirSync","dirname","basename","writeFileSync","Command","Command","Command","Command","withDb","Command","Command","withDb","Command","Command","withDb","Command","Command","select","Command","select","Command","readdirSync","homedir","join","withDb","Command","join","homedir","readdirSync","Command","withDb","Command","Command","basename","join","writeFileSync","basename","Command","join","writeFileSync","Command","withDb","Command","Command","withDb","Command","Command","input","withDb","Command","input","Command","existsSync","mkdirSync","input","Command","existsSync","tasks","tokens","mkdirSync","input","Command"]}
|
|
1
|
+
{"version":3,"sources":["../../src/cli/index.ts","../../src/cli/commands/bridge.ts","../../src/kernel/analytics/stats.ts","../../src/kernel/credentials.ts","../../src/kernel/connectors/azure-devops.ts","../../src/kernel/db/connection.ts","../../src/kernel/db/schema.ts","../../src/kernel/goals/engine.ts","../../src/kernel/goals/parser.ts","../../src/kernel/models/agent-skill.ts","../../src/kernel/models/card.ts","../../src/kernel/models/prerequisite.ts","../../src/kernel/models/review.ts","../../src/kernel/models/session.ts","../../src/kernel/models/settings.ts","../../src/kernel/models/token.ts","../../src/kernel/observation/analyzer.ts","../../src/kernel/observation/monitor-io.ts","../../src/kernel/observation/shell-hooks.ts","../../src/kernel/observation/skill-discovery.ts","../../src/kernel/scheduler/blocker.ts","../../src/kernel/recall/evaluator.ts","../../src/kernel/scheduler/fsrs.ts","../../src/kernel/recall/actions.ts","../../src/kernel/recall/prompter.ts","../../src/kernel/recall/reference-resolver.ts","../../src/kernel/scheduler/interleaver.ts","../../src/kernel/scheduler/queue.ts","../../src/kernel/system/hooks.ts","../../src/kernel/system/i18n.ts","../../src/kernel/system/installer.ts","../../src/kernel/system/locale.ts","../../src/kernel/system/profiler.ts","../../src/kernel/system/repos.ts","../../src/cli/llm/client.ts","../../src/cli/commands/resolve-user.ts","../../src/cli/commands/card.ts","../../src/cli/commands/connector.ts","../../src/cli/commands/git-sync.ts","../../src/cli/commands/goal.ts","../../src/cli/commands/init.ts","../../src/cli/commands/learn.ts","../../src/cli/learn-format.ts","../../src/cli/review-actions.ts","../../src/cli/commands/monitor.ts","../../src/cli/commands/review.ts","../../src/cli/commands/session.ts","../../src/cli/commands/settings.ts","../../src/cli/commands/setup.ts","../../src/cli/commands/skill.ts","../../src/cli/commands/stats.ts","../../src/cli/commands/token.ts","../../src/cli/commands/ui.ts","../../src/cli/commands/whoami.ts","../../src/cli/commands/workspace.ts"],"sourcesContent":["import { Command } from \"commander\";\nimport { bridgeCommand } from \"./commands/bridge.js\";\nimport { cardCommand } from \"./commands/card.js\";\nimport { connectorCommand } from \"./commands/connector.js\";\nimport { gitSyncCommand } from \"./commands/git-sync.js\";\nimport { goalCommand } from \"./commands/goal.js\";\nimport { initCommand } from \"./commands/init.js\";\nimport { learnCommand } from \"./commands/learn.js\";\nimport { monitorCommand } from \"./commands/monitor.js\";\nimport { reviewCommand } from \"./commands/review.js\";\nimport { sessionCommand } from \"./commands/session.js\";\nimport { settingsCommand } from \"./commands/settings.js\";\nimport { setupCommand } from \"./commands/setup.js\";\nimport { skillCommand } from \"./commands/skill.js\";\nimport { statsCommand } from \"./commands/stats.js\";\nimport { tokenCommand } from \"./commands/token.js\";\nimport { uiCommand } from \"./commands/ui.js\";\nimport { whoamiCommand } from \"./commands/whoami.js\";\nimport { workspaceCommand } from \"./commands/workspace.js\";\n\nconst program = new Command();\n\nprogram\n .name(\"zam\")\n .description(\n \"The Symbiotic Learning Kernel: Elevating Human Intelligence through AI Collaboration.\",\n )\n .version(\"0.3.5\");\n\nprogram.addCommand(initCommand);\nprogram.addCommand(setupCommand);\nprogram.addCommand(tokenCommand);\nprogram.addCommand(cardCommand);\nprogram.addCommand(sessionCommand);\nprogram.addCommand(statsCommand);\nprogram.addCommand(reviewCommand);\nprogram.addCommand(learnCommand);\nprogram.addCommand(uiCommand);\nprogram.addCommand(bridgeCommand);\nprogram.addCommand(skillCommand);\nprogram.addCommand(monitorCommand);\nprogram.addCommand(settingsCommand);\nprogram.addCommand(whoamiCommand);\nprogram.addCommand(connectorCommand);\nprogram.addCommand(goalCommand);\nprogram.addCommand(gitSyncCommand);\nprogram.addCommand(workspaceCommand);\n\nprogram.parse();\n","/**\n * `zam bridge` — Machine-readable JSON protocol for AI integration.\n *\n * All output is valid JSON only. No human-readable formatting.\n * Errors are also JSON: { \"error\": \"message\" }\n */\n\nimport { readdirSync } from \"node:fs\";\nimport { homedir } from \"node:os\";\nimport { join } from \"node:path\";\nimport { Command } from \"commander\";\nimport type { Database } from \"libsql\";\nimport type {\n BloomLevel,\n Rating,\n ReviewActionType,\n SymbiosisMode,\n TokenPattern,\n} from \"../../kernel/index.js\";\nimport {\n analyzeObservation,\n buildReviewQueue,\n createToken,\n discoverSkills,\n ensureCard,\n executeReviewAction,\n generatePrompt,\n getAgentSkill,\n getCardDeletionImpact,\n getDueCards,\n getSetting,\n getTokenDeleteImpact,\n listAgentSkills,\n monitorLogExists,\n openDatabase,\n pairCommands,\n readMonitorLog,\n resolveReviewContext,\n} from \"../../kernel/index.js\";\nimport {\n ensureHighQualityQuestion,\n ensureLlmReadyHeadless,\n evaluateAnswerViaLLM,\n getAvailableModels,\n getLlmConfig,\n isLlmOnline,\n translateQuestionViaLLM,\n} from \"../llm/client.js\";\nimport { resolveUser } from \"./resolve-user.js\";\n\nfunction jsonOut(data: unknown): void {\n console.log(JSON.stringify(data, null, 2));\n}\n\nfunction jsonError(message: string): never {\n console.log(JSON.stringify({ error: message }, null, 2));\n process.exit(1);\n}\n\nfunction withDb(fn: (db: Database) => void): void {\n let db: Database | undefined;\n try {\n db = openDatabase();\n fn(db);\n } catch (err) {\n db?.close();\n jsonError((err as Error).message);\n } finally {\n db?.close();\n }\n}\n\nasync function withDbAsync(fn: (db: Database) => Promise<void>): Promise<void> {\n let db: Database | undefined;\n try {\n db = openDatabase();\n await fn(db);\n } catch (err) {\n db?.close();\n jsonError((err as Error).message);\n } finally {\n db?.close();\n }\n}\n\ninterface ReviewTargetRow {\n card_id: string;\n token_id: string;\n user_id: string;\n slug: string;\n}\n\nfunction getReviewTarget(\n db: Database,\n cardId: string,\n userId: string,\n): ReviewTargetRow {\n const target = db\n .prepare(\n `SELECT c.id AS card_id, c.token_id, c.user_id, t.slug\n FROM cards c\n JOIN tokens t ON t.id = c.token_id\n WHERE c.id = ?`,\n )\n .get(cardId) as ReviewTargetRow | undefined;\n\n if (!target) {\n jsonError(`Card not found: ${cardId}`);\n }\n if (target.user_id !== userId) {\n jsonError(`Card ${cardId} does not belong to user ${userId}`);\n }\n\n return target!;\n}\n\nfunction parseTokenUpdates(opts: {\n concept?: string;\n domain?: string;\n bloom?: string;\n context?: string;\n mode?: string;\n sourceLink?: string;\n}): {\n concept?: string;\n domain?: string;\n bloom_level?: BloomLevel;\n context?: string;\n symbiosis_mode?: SymbiosisMode | null;\n source_link?: string | null;\n} {\n const updates: {\n concept?: string;\n domain?: string;\n bloom_level?: BloomLevel;\n context?: string;\n symbiosis_mode?: SymbiosisMode | null;\n source_link?: string | null;\n } = {};\n\n if (opts.concept !== undefined) updates.concept = opts.concept;\n if (opts.domain !== undefined) updates.domain = opts.domain;\n if (opts.bloom !== undefined)\n updates.bloom_level = Number(opts.bloom) as BloomLevel;\n if (opts.context !== undefined) updates.context = opts.context;\n if (opts.sourceLink !== undefined) {\n updates.source_link = opts.sourceLink === \"\" ? null : opts.sourceLink;\n }\n if (opts.mode !== undefined) {\n const validModes = [\"shadowing\", \"copilot\", \"autonomy\", \"none\"];\n if (!validModes.includes(opts.mode)) {\n jsonError(`Invalid mode: ${opts.mode}`);\n }\n updates.symbiosis_mode =\n opts.mode === \"none\" ? null : (opts.mode as SymbiosisMode);\n }\n\n return updates;\n}\n\nexport const bridgeCommand = new Command(\"bridge\").description(\n \"Machine-readable JSON protocol for AI integration\",\n);\n\n// ── zam bridge check-due ──────────────────────────────────────────────────\n\nbridgeCommand\n .command(\"check-due\")\n .description(\"Check due cards for a user (JSON)\")\n .option(\"--user <id>\", \"User ID (default: whoami)\")\n .action((opts) => {\n withDb((db) => {\n const userId = resolveUser(opts, db, { json: true });\n const dueCards = getDueCards(db, userId);\n const domains = [\n ...new Set(dueCards.map((c) => c.domain).filter(Boolean)),\n ].sort();\n\n jsonOut({\n userId,\n dueCount: dueCards.length,\n domains,\n cards: dueCards.map((c) => ({\n cardId: c.id,\n tokenId: c.token_id,\n slug: c.slug,\n concept: c.concept,\n domain: c.domain,\n bloomLevel: c.bloom_level,\n state: c.state,\n dueAt: c.due_at,\n })),\n });\n });\n });\n\n// ── zam bridge get-review ─────────────────────────────────────────────────\n\nbridgeCommand\n .command(\"get-review\")\n .description(\"Get next review card with prompt (JSON)\")\n .option(\"--user <id>\", \"User ID (default: whoami)\")\n .option(\"--no-resolve\", \"Skip resolving the token's source_link into context\")\n .action(async (opts) => {\n await withDbAsync(async (db) => {\n const userId = resolveUser(opts, db, { json: true });\n const queue = buildReviewQueue(db, { userId, maxReviews: 1, maxNew: 1 });\n\n if (queue.items.length === 0) {\n jsonOut({\n userId,\n hasReview: false,\n card: null,\n prompt: null,\n resolvedContext: null,\n queueSize: 0,\n });\n return;\n }\n\n const item = queue.items[0];\n const isLlmEnabled = getSetting(db, \"llm.enabled\") === \"true\";\n\n // Dynamically generate a fresh, living active-recall question if LLM is enabled\n let resolvedQuestion = item.question;\n if (isLlmEnabled) {\n try {\n const healed = await ensureHighQualityQuestion(db, {\n id: item.tokenId,\n slug: item.slug,\n concept: item.concept,\n domain: item.domain,\n bloomLevel: item.bloomLevel as BloomLevel,\n sourceLink: item.sourceLink,\n question: item.question,\n });\n if (healed) {\n resolvedQuestion = healed;\n }\n } catch {\n // ignore and proceed\n }\n }\n\n const prompt = generatePrompt({\n cardId: item.cardId,\n tokenId: item.tokenId,\n slug: item.slug,\n concept: item.concept,\n domain: item.domain,\n bloomLevel: item.bloomLevel as BloomLevel,\n sourceLink: item.sourceLink,\n question: resolvedQuestion,\n });\n\n // Resolve the source_link into ready-to-use context for the AI client.\n // Defensive: never let a bad/unreachable reference break the review payload.\n let resolvedContext = null;\n if (opts.resolve !== false) {\n try {\n resolvedContext = await resolveReviewContext(item.sourceLink);\n } catch {\n resolvedContext = null;\n }\n }\n\n // Get full queue size for context\n const fullQueue = buildReviewQueue(db, { userId });\n\n jsonOut({\n userId,\n hasReview: true,\n card: item,\n prompt,\n resolvedContext,\n queueSize: fullQueue.items.length,\n });\n });\n });\n\n// ── zam bridge submit ─────────────────────────────────────────────────────\n\nbridgeCommand\n .command(\"submit\")\n .description(\"Submit a rating for a card (JSON)\")\n .option(\"--user <id>\", \"User ID (default: whoami)\")\n .requiredOption(\"--card-id <id>\", \"Card ID\")\n .requiredOption(\"--rating <n>\", \"Rating (1-4)\")\n .action((opts) => {\n withDb((db) => {\n const userId = resolveUser(opts, db, { json: true });\n const rating = Number(opts.rating) as Rating;\n if (rating < 1 || rating > 4) {\n jsonError(\"Rating must be between 1 and 4\");\n }\n\n const result = executeReviewAction(db, {\n action: \"rate\",\n cardId: opts.cardId,\n userId,\n rating,\n });\n\n jsonOut({\n success: true,\n rating,\n evaluation: result.evaluation,\n blocked: result.blocked ?? null,\n });\n });\n });\n\n// ── zam bridge review-action ───────────────────────────────────────────────\n\nbridgeCommand\n .command(\"review-action\")\n .description(\"Apply a review action (JSON)\")\n .option(\"--user <id>\", \"User ID (default: whoami)\")\n .requiredOption(\"--card-id <id>\", \"Card ID\")\n .requiredOption(\n \"--action <action>\",\n \"Action: rate | skip | edit-token | deprecate-token | delete-token | delete-card | stop\",\n )\n .option(\"--rating <n>\", \"Rating (1-4) for action=rate\")\n .option(\"--concept <concept>\", \"Updated concept text for action=edit-token\")\n .option(\"--domain <domain>\", \"Updated domain for action=edit-token\")\n .option(\"--bloom <level>\", \"Updated Bloom level for action=edit-token\")\n .option(\"--context <context>\", \"Updated context for action=edit-token\")\n .option(\"--mode <mode>\", \"Updated symbiosis mode for action=edit-token\")\n .option(\"--source-link <link>\", \"Updated source link for action=edit-token\")\n .option(\"--confirm\", \"Confirm destructive delete actions\")\n .action((opts) => {\n withDb((db) => {\n const userId = resolveUser(opts, db, { json: true });\n const action = opts.action as ReviewActionType;\n const validActions: ReviewActionType[] = [\n \"rate\",\n \"skip\",\n \"edit-token\",\n \"deprecate-token\",\n \"delete-token\",\n \"delete-card\",\n \"stop\",\n ];\n if (!validActions.includes(action)) {\n jsonError(`Unsupported action: ${opts.action}`);\n }\n\n const target = getReviewTarget(db, opts.cardId, userId);\n if (\n (action === \"delete-token\" || action === \"delete-card\") &&\n !opts.confirm\n ) {\n if (action === \"delete-token\") {\n jsonOut({\n success: true,\n action,\n preview: true,\n requiresConfirmation: true,\n token: { slug: target.slug, tokenId: target.token_id },\n impact: getTokenDeleteImpact(db, target.slug),\n });\n return;\n }\n\n jsonOut({\n success: true,\n action,\n preview: true,\n requiresConfirmation: true,\n token: { slug: target.slug, tokenId: target.token_id },\n impact: getCardDeletionImpact(db, target.token_id, userId),\n });\n return;\n }\n\n const rating =\n opts.rating !== undefined ? (Number(opts.rating) as Rating) : undefined;\n if (action === \"rate\" && (rating == null || rating < 1 || rating > 4)) {\n jsonError(\"Rating must be between 1 and 4 for action=rate\");\n }\n\n const result = executeReviewAction(db, {\n action,\n cardId: opts.cardId,\n userId,\n rating,\n tokenUpdates:\n action === \"edit-token\" ? parseTokenUpdates(opts) : undefined,\n });\n\n jsonOut({\n success: true,\n action,\n token: {\n slug: result.token.slug,\n tokenId: result.token.id,\n },\n rating: rating ?? null,\n evaluation: result.evaluation ?? null,\n blocked: result.blocked ?? null,\n updatedToken: result.updatedToken ?? null,\n deletedToken: result.deletedToken ?? null,\n deletedCard: result.deletedCard ?? null,\n skipped: result.skipped ?? false,\n stopped: result.stopped ?? false,\n });\n });\n });\n\n// ── zam bridge get-skill ──────────────────────────────────────────────────\n\nbridgeCommand\n .command(\"get-skill\")\n .description(\"Get an agent skill by slug (JSON)\")\n .requiredOption(\"--slug <slug>\", \"Skill slug\")\n .action((opts) => {\n withDb((db) => {\n const skill = getAgentSkill(db, opts.slug);\n if (!skill) {\n jsonError(`Skill not found: ${opts.slug}`);\n }\n\n jsonOut({\n slug: skill?.slug,\n description: skill?.description,\n steps: skill?.steps,\n tokenSlugs: skill?.token_slugs,\n source: skill?.source,\n });\n });\n });\n\n// ── zam bridge get-monitor ────────────────────────────────────────────────\n\nbridgeCommand\n .command(\"get-monitor\")\n .description(\"Read monitor log for a session (JSON)\")\n .requiredOption(\"--session <id>\", \"Session ID\")\n .action((opts) => {\n if (!monitorLogExists(opts.session)) {\n jsonOut({\n sessionId: opts.session,\n exists: false,\n commands: [],\n timeSpan: null,\n });\n return;\n }\n\n const events = readMonitorLog(opts.session);\n const commands = pairCommands(events);\n\n let timeSpan: { start: string; end: string; durationMs: number } | null =\n null;\n if (commands.length > 0) {\n const first = commands[0];\n const last = commands[commands.length - 1];\n const endTs = last.endedAt ?? last.startedAt;\n timeSpan = {\n start: first.startedAt,\n end: endTs,\n durationMs:\n new Date(endTs).getTime() - new Date(first.startedAt).getTime(),\n };\n }\n\n jsonOut({\n sessionId: opts.session,\n exists: true,\n commands: commands.map((c) => ({\n seq: c.seq,\n command: c.command,\n cwd: c.cwd,\n startedAt: c.startedAt,\n endedAt: c.endedAt,\n durationMs: c.durationMs,\n exitCode: c.exitCode,\n })),\n timeSpan,\n });\n });\n\n// ── zam bridge analyze-monitor ───────────────────────────────────────────\n\nbridgeCommand\n .command(\"analyze-monitor\")\n .description(\"Analyze monitor log with token patterns from stdin (JSON)\")\n .requiredOption(\"--session <id>\", \"Session ID\")\n .action(async (opts) => {\n try {\n if (!monitorLogExists(opts.session)) {\n jsonOut({\n sessionId: opts.session,\n ratings: [],\n unmatchedCommands: [],\n timeSpan: null,\n });\n return;\n }\n\n // Read token patterns from stdin\n const chunks: Buffer[] = [];\n for await (const chunk of process.stdin) {\n chunks.push(chunk as Buffer);\n }\n const raw = Buffer.concat(chunks).toString(\"utf-8\").trim();\n\n if (!raw) {\n jsonError(\"No input received on stdin. Pipe JSON with token patterns.\");\n }\n\n let data: { patterns: TokenPattern[] };\n try {\n data = JSON.parse(raw);\n } catch {\n jsonError(\"Invalid JSON input\");\n }\n\n if (!Array.isArray(data?.patterns)) {\n jsonError(\"JSON must include 'patterns' array\");\n }\n\n const events = readMonitorLog(opts.session);\n const commands = pairCommands(events);\n const result = analyzeObservation(commands, data?.patterns);\n\n jsonOut({\n sessionId: opts.session,\n ...result,\n });\n } catch (err) {\n jsonError((err as Error).message);\n }\n });\n\n// ── zam bridge add-token ──────────────────────────────────────────────────\n\nbridgeCommand\n .command(\"add-token\")\n .description(\"Create a token + card from JSON stdin\")\n .option(\"--user <id>\", \"User ID (default: whoami)\")\n .action(async (opts) => {\n let db: Database | undefined;\n try {\n // Read JSON from stdin\n const chunks: Buffer[] = [];\n for await (const chunk of process.stdin) {\n chunks.push(chunk as Buffer);\n }\n const raw = Buffer.concat(chunks).toString(\"utf-8\").trim();\n\n if (!raw) {\n jsonError(\"No input received on stdin. Pipe JSON with token data.\");\n }\n\n let data: {\n slug: string;\n concept: string;\n domain?: string;\n bloom_level?: number;\n context?: string;\n symbiosis_mode?: string | null;\n source_link?: string | null;\n };\n\n try {\n data = JSON.parse(raw);\n } catch {\n jsonError(\"Invalid JSON input\");\n }\n\n if (!data?.slug || !data?.concept) {\n jsonError(\"JSON must include 'slug' and 'concept' fields\");\n }\n\n db = openDatabase();\n const userId = resolveUser(opts, db, { json: true });\n\n const token = createToken(db, {\n slug: data?.slug,\n concept: data?.concept,\n domain: data?.domain,\n bloom_level: (data?.bloom_level ?? 1) as BloomLevel,\n context: data?.context,\n symbiosis_mode: data?.symbiosis_mode as\n | \"shadowing\"\n | \"copilot\"\n | \"autonomy\"\n | null\n | undefined,\n source_link: data?.source_link ?? null,\n });\n\n const card = ensureCard(db, token.id, userId);\n\n jsonOut({\n success: true,\n token,\n card: {\n id: card.id,\n tokenId: card.token_id,\n userId: card.user_id,\n state: card.state,\n dueAt: card.due_at,\n blocked: card.blocked,\n },\n });\n\n db.close();\n } catch (err) {\n db?.close();\n // If it's already a JSON error exit, let it propagate\n if ((err as Error).message) {\n jsonError((err as Error).message);\n }\n }\n });\n\n// ── zam bridge discover-skills ──────────────────────────────────────────────\n\nbridgeCommand\n .command(\"discover-skills\")\n .description(\n \"Analyze monitor logs across sessions to discover recurring patterns\",\n )\n .option(\n \"--min-sessions <n>\",\n \"Minimum sessions a pattern must appear in (default: 2)\",\n \"2\",\n )\n .option(\n \"--limit <n>\",\n \"Max number of sessions to analyze (default: 20)\",\n \"20\",\n )\n .action((opts) => {\n try {\n const monitorDir = join(homedir(), \".zam\", \"monitor\");\n let files: string[];\n try {\n files = readdirSync(monitorDir).filter((f) => f.endsWith(\".jsonl\"));\n } catch {\n jsonOut({ proposals: [], message: \"No monitor logs found.\" });\n return;\n }\n\n if (files.length === 0) {\n jsonOut({ proposals: [], message: \"No monitor logs found.\" });\n return;\n }\n\n // Take the most recent N sessions by file modification time\n const limit = Number(opts.limit);\n const sorted = files\n .map((f) => ({ name: f, path: join(monitorDir, f) }))\n .sort((a, b) => b.name.localeCompare(a.name)) // ULID session IDs sort chronologically\n .slice(0, limit);\n\n // Load and parse each session's commands\n const sessionCommands = new Map<\n string,\n ReturnType<typeof pairCommands>\n >();\n for (const file of sorted) {\n const sessionId = file.name.replace(\".jsonl\", \"\");\n const events = readMonitorLog(sessionId);\n const commands = pairCommands(events);\n if (commands.length > 0) {\n sessionCommands.set(sessionId, commands);\n }\n }\n\n if (sessionCommands.size === 0) {\n jsonOut({ proposals: [], message: \"No command data in monitor logs.\" });\n return;\n }\n\n // Get existing skills to exclude\n let existingSkillSlugs: string[] = [];\n let db: Database | undefined;\n try {\n db = openDatabase();\n existingSkillSlugs = listAgentSkills(db).map((s) => s.slug);\n } catch {\n // DB not available — proceed without exclusion\n } finally {\n db?.close();\n }\n\n const proposals = discoverSkills(sessionCommands, {\n minSessions: Number(opts.minSessions),\n existingSkillSlugs,\n });\n\n jsonOut({\n sessionsAnalyzed: sessionCommands.size,\n proposals,\n });\n } catch (err) {\n jsonError((err as Error).message);\n }\n });\n\n// ── zam bridge check-llm ──────────────────────────────────────────────────\n\nbridgeCommand\n .command(\"check-llm\")\n .description(\"Check if LLM is enabled and online (JSON)\")\n .action(async () => {\n await withDbAsync(async (db) => {\n const { enabled, url, model, apiKey } = getLlmConfig(db);\n let online = false;\n let availableModels: string[] = [];\n let modelAvailable = false;\n if (enabled) {\n online = await isLlmOnline(url);\n if (online) {\n availableModels = await getAvailableModels(url, apiKey);\n // Empty list = server doesn't expose /models; don't claim it's wrong.\n modelAvailable =\n availableModels.length === 0 ||\n availableModels.some(\n (m) => m.toLowerCase() === model.toLowerCase(),\n );\n }\n }\n jsonOut({\n enabled,\n online,\n url,\n model,\n modelAvailable,\n availableModels,\n });\n });\n });\n\n// ── zam bridge ensure-llm ─────────────────────────────────────────────────\n\nbridgeCommand\n .command(\"ensure-llm\")\n .description(\n \"Start the local LLM server if needed and report readiness (JSON)\",\n )\n .option(\n \"--timeout <ms>\",\n \"Max time to wait for the server to come online\",\n \"25000\",\n )\n .action(async (opts) => {\n await withDbAsync(async (db) => {\n const result = await ensureLlmReadyHeadless(db, {\n timeoutMs: Number(opts.timeout),\n });\n jsonOut(result);\n });\n });\n\n// ── zam bridge translate-question ──────────────────────────────────────────\n\nbridgeCommand\n .command(\"translate-question\")\n .description(\"Translate a question dynamically using the local LLM (JSON)\")\n .requiredOption(\"--question <text>\", \"Question in English to translate\")\n .action(async (opts) => {\n await withDbAsync(async (db) => {\n const isEnabled = getSetting(db, \"llm.enabled\") === \"true\";\n if (!isEnabled) {\n jsonOut({\n success: false,\n error: \"LLM integration is disabled\",\n translation: opts.question,\n });\n return;\n }\n try {\n const translation = await translateQuestionViaLLM(db, opts.question);\n jsonOut({ success: true, translation });\n } catch (err) {\n jsonOut({\n success: false,\n error: (err as Error).message,\n translation: opts.question,\n });\n }\n });\n });\n\n// ── zam bridge evaluate-answer ────────────────────────────────────────────\n\nbridgeCommand\n .command(\"evaluate-answer\")\n .description(\n \"Evaluate the learner's active-recall answer using the local LLM (JSON)\",\n )\n .requiredOption(\"--slug <slug>\", \"Token slug\")\n .requiredOption(\"--concept <concept>\", \"Target concept text\")\n .requiredOption(\"--domain <domain>\", \"Token domain\")\n .requiredOption(\"--bloom-level <level>\", \"Bloom taxonomy level\")\n .requiredOption(\"--question <question>\", \"Question prompt presented\")\n .requiredOption(\"--user-answer <answer>\", \"User's typed answer\")\n .option(\"--context <context>\", \"Optional token context details\")\n .option(\"--source-link <link>\", \"Optional source link\")\n .action(async (opts) => {\n await withDbAsync(async (db) => {\n const isEnabled = getSetting(db, \"llm.enabled\") === \"true\";\n if (!isEnabled) {\n jsonOut({\n success: false,\n error: \"LLM integration is disabled\",\n evaluation: \"\",\n });\n return;\n }\n\n let resolvedContextContent = null;\n if (opts.sourceLink) {\n try {\n const resolved = await resolveReviewContext(opts.sourceLink);\n resolvedContextContent = resolved?.content ?? null;\n } catch {\n // ignore context resolution errors\n }\n }\n\n try {\n const evaluation = await evaluateAnswerViaLLM(db, {\n slug: opts.slug,\n concept: opts.concept,\n domain: opts.domain,\n bloomLevel: Number(opts.bloomLevel),\n context: opts.context,\n question: opts.question,\n userAnswer: opts.userAnswer,\n sourceLinkContent: resolvedContextContent,\n });\n jsonOut({ success: true, evaluation });\n } catch (err) {\n jsonOut({\n success: false,\n error: (err as Error).message,\n evaluation: \"\",\n });\n }\n });\n });\n\n// ── zam bridge get-settings ───────────────────────────────────────────────\n\nbridgeCommand\n .command(\"get-settings\")\n .description(\"Get active ZAM settings (JSON)\")\n .action(() => {\n withDb((db) => {\n const { enabled, url, model, locale } = getLlmConfig(db);\n jsonOut({\n locale,\n llm: {\n enabled,\n url,\n model,\n },\n });\n });\n });\n","/**\n * Learning Analytics\n *\n * Progress statistics, competence tracking, and session summaries.\n * Ported from PoC's `stats` command with additions for FSRS and symbiosis modes.\n */\n\nimport type { Database } from \"libsql\";\n\nexport interface UserStats {\n userId: string;\n totalTokens: number;\n cardsInDeck: number;\n dueToday: number;\n blocked: number;\n mature: number;\n avgStability: number | null;\n totalSessions: number;\n lastSession: string | null;\n}\n\nexport interface DomainCompetence {\n domain: string;\n totalCards: number;\n matureCards: number;\n avgStability: number;\n retentionRate: number;\n suggestedMode: \"shadowing\" | \"copilot\" | \"autonomy\";\n}\n\nfunction q(db: Database, sql: string, ...params: unknown[]) {\n return db.prepare(sql).get(...params) as Record<string, unknown>;\n}\n\n/**\n * Get overall learning stats for a user (ported from PoC's `stats` command).\n */\nexport function getUserStats(db: Database, userId: string): UserStats {\n return {\n userId,\n totalTokens: (q(db, \"SELECT COUNT(*) as n FROM tokens\") as { n: number }).n,\n cardsInDeck: (\n q(db, \"SELECT COUNT(*) as n FROM cards WHERE user_id = ?\", userId) as {\n n: number;\n }\n ).n,\n dueToday: (\n q(\n db,\n \"SELECT COUNT(*) as n FROM cards WHERE user_id = ? AND blocked = 0 AND due_at <= datetime('now')\",\n userId,\n ) as { n: number }\n ).n,\n blocked: (\n q(\n db,\n \"SELECT COUNT(*) as n FROM cards WHERE user_id = ? AND blocked = 1\",\n userId,\n ) as { n: number }\n ).n,\n mature: (\n q(\n db,\n \"SELECT COUNT(*) as n FROM cards WHERE user_id = ? AND reps >= 3 AND stability >= 21\",\n userId,\n ) as { n: number }\n ).n,\n avgStability: (() => {\n const v = q(\n db,\n \"SELECT AVG(stability) as v FROM cards WHERE user_id = ? AND reps > 0\",\n userId,\n ) as { v: number | null };\n return v.v ? Math.round(v.v * 100) / 100 : null;\n })(),\n totalSessions: (\n q(db, \"SELECT COUNT(*) as n FROM sessions WHERE user_id = ?\", userId) as {\n n: number;\n }\n ).n,\n lastSession: (() => {\n const r = db\n .prepare(\n \"SELECT started_at FROM sessions WHERE user_id = ? ORDER BY started_at DESC LIMIT 1\",\n )\n .get(userId) as { started_at: string } | undefined;\n return r?.started_at ?? null;\n })(),\n };\n}\n\n/**\n * Get competence per domain for a user.\n * Used to suggest symbiosis mode transitions.\n */\nexport function getDomainCompetence(\n db: Database,\n userId: string,\n): DomainCompetence[] {\n const domains = db\n .prepare(\n `SELECT DISTINCT t.domain FROM cards c\n JOIN tokens t ON t.id = c.token_id\n WHERE c.user_id = ? AND t.domain != ''`,\n )\n .all(userId) as { domain: string }[];\n\n return domains.map((d) => {\n const total = (\n q(\n db,\n `SELECT COUNT(*) as n FROM cards c\n JOIN tokens t ON t.id = c.token_id\n WHERE c.user_id = ? AND t.domain = ?`,\n userId,\n d.domain,\n ) as { n: number }\n ).n;\n\n const mature = (\n q(\n db,\n `SELECT COUNT(*) as n FROM cards c\n JOIN tokens t ON t.id = c.token_id\n WHERE c.user_id = ? AND t.domain = ? AND c.reps >= 3 AND c.stability >= 21`,\n userId,\n d.domain,\n ) as { n: number }\n ).n;\n\n const avgStab =\n (\n q(\n db,\n `SELECT AVG(c.stability) as v FROM cards c\n JOIN tokens t ON t.id = c.token_id\n WHERE c.user_id = ? AND t.domain = ? AND c.reps > 0`,\n userId,\n d.domain,\n ) as { v: number | null }\n ).v ?? 0;\n\n // Estimate retention from review history\n const reviews = q(\n db,\n `SELECT COUNT(*) as total,\n SUM(CASE WHEN rating >= 2 THEN 1 ELSE 0 END) as passed\n FROM review_logs\n WHERE user_id = ? AND token_id IN (SELECT id FROM tokens WHERE domain = ?)`,\n userId,\n d.domain,\n ) as { total: number; passed: number };\n\n const retentionRate =\n reviews.total > 0 ? reviews.passed / reviews.total : 0;\n\n let suggestedMode: DomainCompetence[\"suggestedMode\"];\n if (retentionRate > 0.9 && avgStab > 30) {\n suggestedMode = \"autonomy\";\n } else if (retentionRate > 0.7 && avgStab > 7) {\n suggestedMode = \"copilot\";\n } else {\n suggestedMode = \"shadowing\";\n }\n\n return {\n domain: d.domain,\n totalCards: total,\n matureCards: mature,\n avgStability: Math.round(avgStab * 100) / 100,\n retentionRate: Math.round(retentionRate * 1000) / 1000,\n suggestedMode,\n };\n });\n}\n","/**\n * Credential store — reads/writes ~/.zam/credentials.json\n *\n * Connector secrets (Turso URL/token, ADO PAT, etc.) live here instead of\n * inside the SQLite database. This ensures credentials survive db deletion,\n * which is required when migrating from plain SQLite to a libsql embedded\n * replica (Turso cloud sync).\n */\n\nimport { existsSync, mkdirSync, readFileSync, writeFileSync } from \"node:fs\";\nimport { homedir } from \"node:os\";\nimport { dirname, join } from \"node:path\";\n\nconst DEFAULT_CREDENTIALS_PATH = join(homedir(), \".zam\", \"credentials.json\");\n\nexport interface TursoCredentials {\n url: string;\n token: string;\n}\n\nexport interface ADOCredentials {\n org_url: string;\n project: string;\n pat: string;\n}\n\nexport interface Credentials {\n turso?: Partial<TursoCredentials>;\n ado?: Partial<ADOCredentials>;\n}\n\n/** Load credentials from ~/.zam/credentials.json. Returns empty object if missing. */\nexport function loadCredentials(path?: string): Credentials {\n const p = path ?? DEFAULT_CREDENTIALS_PATH;\n if (!existsSync(p)) return {};\n try {\n return JSON.parse(readFileSync(p, \"utf-8\")) as Credentials;\n } catch {\n return {};\n }\n}\n\n/** Save credentials to ~/.zam/credentials.json. */\nexport function saveCredentials(creds: Credentials, path?: string): void {\n const p = path ?? DEFAULT_CREDENTIALS_PATH;\n const dir = dirname(p);\n if (!existsSync(dir)) {\n mkdirSync(dir, { recursive: true });\n }\n writeFileSync(p, `${JSON.stringify(creds, null, 2)}\\n`, \"utf-8\");\n}\n\n/** Get complete Turso credentials, or null if incomplete. */\nexport function getTursoCredentials(path?: string): TursoCredentials | null {\n const creds = loadCredentials(path);\n if (creds.turso?.url && creds.turso?.token) {\n return { url: creds.turso.url, token: creds.turso.token };\n }\n return null;\n}\n\n/** Set Turso credentials. */\nexport function setTursoCredentials(\n url: string,\n token: string,\n path?: string,\n): void {\n const creds = loadCredentials(path);\n creds.turso = { url, token };\n saveCredentials(creds, path);\n}\n\n/** Clear Turso credentials. */\nexport function clearTursoCredentials(path?: string): void {\n const creds = loadCredentials(path);\n delete creds.turso;\n saveCredentials(creds, path);\n}\n\n/** Get complete ADO credentials, or null if incomplete. */\nexport function getADOCredentials(path?: string): ADOCredentials | null {\n const creds = loadCredentials(path);\n if (creds.ado?.org_url && creds.ado?.project && creds.ado?.pat) {\n return {\n org_url: creds.ado.org_url,\n project: creds.ado.project,\n pat: creds.ado.pat,\n };\n }\n return null;\n}\n\n/** Set ADO credentials. */\nexport function setADOCredentials(\n orgUrl: string,\n project: string,\n pat: string,\n path?: string,\n): void {\n const creds = loadCredentials(path);\n creds.ado = { org_url: orgUrl, project, pat };\n saveCredentials(creds, path);\n}\n\n/** Clear ADO credentials. */\nexport function clearADOCredentials(path?: string): void {\n const creds = loadCredentials(path);\n delete creds.ado;\n saveCredentials(creds, path);\n}\n","/**\n * Azure DevOps connector — fetches work items from ADO boards.\n */\n\nimport { getADOCredentials } from \"../credentials.js\";\n\nexport interface ADOConfig {\n orgUrl: string;\n project: string;\n pat: string;\n}\n\nexport interface WorkItem {\n id: number;\n title: string;\n state: string;\n type: string;\n assignedTo: string;\n}\n\n/** Load ADO config from credentials file. Returns null if not configured. */\nexport function loadADOConfig(): ADOConfig | null {\n const creds = getADOCredentials();\n if (!creds) return null;\n return {\n orgUrl: creds.org_url.replace(/\\/+$/, \"\"),\n project: creds.project,\n pat: creds.pat,\n };\n}\n\nfunction authHeader(pat: string): string {\n return `Basic ${Buffer.from(`:${pat}`).toString(\"base64\")}`;\n}\n\n/**\n * Fetch active work items assigned to the current user.\n * Uses WIQL to query, then batch-fetches work item details.\n */\nexport async function fetchActiveWorkItems(\n config: ADOConfig,\n): Promise<WorkItem[]> {\n const { orgUrl, project, pat } = config;\n\n // Step 1: WIQL query for work item IDs\n const wiqlUrl = `${orgUrl}/${project}/_apis/wit/wiql?api-version=7.1`;\n const wiqlBody = {\n query: `SELECT [System.Id] FROM WorkItems WHERE [System.AssignedTo] = @me AND [System.State] NOT IN ('Closed', 'Completed', 'Done', 'Removed') ORDER BY [Microsoft.VSTS.Common.Priority] ASC, [System.ChangedDate] DESC`,\n };\n\n const wiqlRes = await fetch(wiqlUrl, {\n method: \"POST\",\n headers: {\n Authorization: authHeader(pat),\n \"Content-Type\": \"application/json\",\n },\n body: JSON.stringify(wiqlBody),\n });\n\n if (!wiqlRes.ok) {\n const text = await wiqlRes.text();\n throw new Error(`ADO WIQL query failed (${wiqlRes.status}): ${text}`);\n }\n\n const wiqlData = (await wiqlRes.json()) as { workItems: { id: number }[] };\n const ids = wiqlData.workItems.map((wi) => wi.id);\n\n if (ids.length === 0) return [];\n\n // Step 2: Batch fetch work item details (max 200 per request)\n const batchIds = ids.slice(0, 200);\n const fields =\n \"System.Id,System.Title,System.State,System.WorkItemType,System.AssignedTo\";\n const detailUrl = `${orgUrl}/${project}/_apis/wit/workitems?ids=${batchIds.join(\",\")}&fields=${fields}&api-version=7.1`;\n\n const detailRes = await fetch(detailUrl, {\n headers: { Authorization: authHeader(pat) },\n });\n\n if (!detailRes.ok) {\n const text = await detailRes.text();\n throw new Error(\n `ADO work items fetch failed (${detailRes.status}): ${text}`,\n );\n }\n\n const detailData = (await detailRes.json()) as {\n value: Array<{\n id: number;\n fields: {\n \"System.Title\": string;\n \"System.State\": string;\n \"System.WorkItemType\": string;\n \"System.AssignedTo\"?: { displayName: string };\n };\n }>;\n };\n\n return detailData.value.map((wi) => ({\n id: wi.id,\n title: wi.fields[\"System.Title\"],\n state: wi.fields[\"System.State\"],\n type: wi.fields[\"System.WorkItemType\"],\n assignedTo: wi.fields[\"System.AssignedTo\"]?.displayName ?? \"\",\n }));\n}\n","import { existsSync, mkdirSync, readFileSync, rmSync } from \"node:fs\";\nimport { homedir } from \"node:os\";\nimport { dirname, join } from \"node:path\";\nimport Database, { type Database as DatabaseType } from \"libsql\";\nimport { getTursoCredentials } from \"../credentials.js\";\nimport { SCHEMA } from \"./schema.js\";\n\nconst DEFAULT_DB_DIR = join(homedir(), \".zam\");\nconst DEFAULT_DB_PATH = join(DEFAULT_DB_DIR, \"zam.db\");\n\nexport interface ConnectionOptions {\n /** Path to the SQLite database file. Defaults to ~/.zam/zam.db */\n dbPath?: string;\n /** If true, create the directory and run schema migrations on open */\n initialize?: boolean;\n /** Turso sync URL for embedded replica mode (e.g. libsql://db-name.turso.io) */\n syncUrl?: string;\n /** Turso auth token for direct remote or embedded replica access */\n authToken?: string;\n /** If false, ignore ~/.zam/credentials.json and force the local/default database. */\n useConfiguredCloud?: boolean;\n}\n\nfunction isRemoteDatabasePath(dbPath: string): boolean {\n return /^(libsql|https?):\\/\\//i.test(dbPath);\n}\n\n/**\n * Open (or create) the ZAM database.\n * Uses configured Turso credentials for the default database when present.\n * Falls back to local SQLite and WAL mode when no cloud credentials exist.\n * When syncUrl is provided explicitly, enables embedded replica sync with Turso.\n */\nexport function openDatabase(options: ConnectionOptions = {}): DatabaseType {\n const configuredCloud =\n options.useConfiguredCloud !== false && !options.dbPath && !options.syncUrl\n ? getTursoCredentials()\n : null;\n\n let requiresTurso = false;\n try {\n const configPath = join(process.cwd(), \".zam\", \"config.yaml\");\n if (existsSync(configPath)) {\n const configText = readFileSync(configPath, \"utf-8\");\n if (/[\\s\\S]*turso:[\\s\\S]*url:/m.test(configText)) {\n requiresTurso = true;\n }\n }\n } catch (_e) {}\n\n if (\n requiresTurso &&\n !configuredCloud &&\n options.useConfiguredCloud !== false &&\n !options.dbPath &&\n !options.syncUrl\n ) {\n throw new Error(\n \"Turso cloud database is configured in .zam/config.yaml but missing local credentials. Run: zam connector setup turso\",\n );\n }\n const dbPath = configuredCloud?.url ?? options.dbPath ?? DEFAULT_DB_PATH;\n const isRemote = isRemoteDatabasePath(dbPath);\n const isEmbeddedReplica = Boolean(options.syncUrl);\n\n if (options.initialize && !isRemote) {\n const dir = dirname(dbPath);\n if (!existsSync(dir)) {\n mkdirSync(dir, { recursive: true });\n }\n }\n\n // Build constructor options for libsql\n const dbOpts: Record<string, unknown> = {};\n if (options.syncUrl) {\n dbOpts.syncUrl = options.syncUrl;\n\n // When syncUrl is provided, the db must be a libsql embedded replica (not\n // plain SQLite). The presence of a companion .meta (or -info) file proves\n // it was created by libsql.\n //\n // If the db exists WITHOUT metadata, it was created before Turso was\n // configured — delete it so libsql can sync fresh from cloud.\n //\n // If metadata exists WITHOUT the db, libsql throws InvalidLocalState —\n // delete the metadata so it can start fresh.\n const metaPath = `${dbPath}.meta`;\n const infoPath = `${dbPath}-info`;\n\n if (existsSync(dbPath) && !existsSync(metaPath) && !existsSync(infoPath)) {\n for (const suffix of [\"\", \"-wal\", \"-shm\"]) {\n const f = `${dbPath}${suffix}`;\n if (existsSync(f)) rmSync(f, { force: true });\n }\n } else if (\n !existsSync(dbPath) &&\n (existsSync(metaPath) || existsSync(infoPath))\n ) {\n if (existsSync(metaPath)) rmSync(metaPath);\n if (existsSync(infoPath)) rmSync(infoPath);\n }\n }\n const authToken = configuredCloud?.token ?? options.authToken;\n if (authToken) {\n dbOpts.authToken = authToken;\n }\n\n let db: DatabaseType;\n try {\n db = new Database(dbPath, dbOpts as Database.Options);\n } catch (err) {\n const msg = (err as Error).message;\n if (msg.includes(\"InvalidLocalState\") && options.syncUrl) {\n // Last-ditch recovery: metadata is corrupt or mismatched\n const metaPath = `${dbPath}.meta`;\n const infoPath = `${dbPath}-info`;\n if (existsSync(metaPath)) rmSync(metaPath);\n if (existsSync(infoPath)) rmSync(infoPath);\n db = new Database(dbPath, dbOpts as Database.Options);\n } else {\n throw err;\n }\n }\n\n // Enable WAL mode and foreign keys for local SQLite.\n // Remote Turso databases and embedded replicas manage their own journaling.\n if (!isRemote && !isEmbeddedReplica) {\n db.pragma(\"journal_mode = WAL\");\n }\n db.pragma(\"foreign_keys = ON\");\n if (!isRemote) {\n db.pragma(\"busy_timeout = 5000\");\n }\n\n // For embedded replicas: sync from cloud FIRST so the local file has the\n // primary's schema before we try to run migrations or create tables.\n if (isEmbeddedReplica) {\n (db as unknown as { sync: () => void }).sync();\n }\n\n if (options.initialize) {\n db.exec(SCHEMA);\n }\n\n runMigrations(db);\n\n return db;\n}\n\n/**\n * Open the database with Turso cloud credentials auto-detected.\n * Credentials live in ~/.zam/credentials.json (NOT in the db), so a fresh\n * machine only has to collect missing secrets instead of bootstrapping local\n * state first.\n */\nexport function openDatabaseWithSync(\n options: Omit<ConnectionOptions, \"syncUrl\" | \"authToken\"> = {},\n): DatabaseType {\n return openDatabase(options);\n}\n\n/** Get the default database path */\nexport function getDefaultDbPath(): string {\n return DEFAULT_DB_PATH;\n}\n\n/**\n * Run incremental schema migrations on every open.\n * Each migration is idempotent — safe to run repeatedly.\n */\nfunction runMigrations(db: DatabaseType): void {\n // M001: add execution_context to sessions\n const sessionCols = db.pragma(\"table_info(sessions)\") as Array<{\n name: string;\n }>;\n if (\n sessionCols.length > 0 &&\n !sessionCols.some((c) => c.name === \"execution_context\")\n ) {\n db.exec(\n `ALTER TABLE sessions ADD COLUMN execution_context TEXT NOT NULL DEFAULT 'shell'`,\n );\n }\n\n // M002: add deprecated_at to tokens\n const tokenCols = db.pragma(\"table_info(tokens)\") as Array<{ name: string }>;\n if (\n tokenCols.length > 0 &&\n !tokenCols.some((c) => c.name === \"deprecated_at\")\n ) {\n db.exec(`ALTER TABLE tokens ADD COLUMN deprecated_at TEXT`);\n }\n\n // M004: add source_link to tokens\n if (\n tokenCols.length > 0 &&\n !tokenCols.some((c) => c.name === \"source_link\")\n ) {\n db.exec(`ALTER TABLE tokens ADD COLUMN source_link TEXT`);\n }\n\n // M005: add question to tokens\n if (tokenCols.length > 0 && !tokenCols.some((c) => c.name === \"question\")) {\n db.exec(`ALTER TABLE tokens ADD COLUMN question TEXT`);\n }\n\n // M003: create agent_skills table (idempotent via IF NOT EXISTS in SCHEMA,\n // but also needed for databases that skipped the init path)\n db.exec(`\n CREATE TABLE IF NOT EXISTS agent_skills (\n id TEXT PRIMARY KEY,\n slug TEXT NOT NULL UNIQUE,\n description TEXT NOT NULL,\n steps TEXT NOT NULL DEFAULT '[]',\n token_slugs TEXT NOT NULL DEFAULT '[]',\n source TEXT NOT NULL DEFAULT 'learned',\n created_at TEXT NOT NULL DEFAULT (datetime('now')),\n updated_at TEXT NOT NULL DEFAULT (datetime('now'))\n )\n `);\n}\n","/**\n * ZAM Learning Kernel — SQLite Schema\n *\n * Evolves the PoC's schema with:\n * - FSRS scheduling fields (replaces SM-2's ef/interval_days)\n * - Bloom taxonomy levels on tokens\n * - Symbiosis modes (shadowing/copilot/autonomy)\n * - ULID-based IDs\n * - Immutable review log\n */\n\nexport const SCHEMA = `\n-- PRAGMAs (WAL, foreign_keys) are set programmatically in connection.ts,\n-- not here, because libsql embedded replicas manage their own WAL.\n\n-- Knowledge tokens: atomic concepts/facts with Bloom levels\nCREATE TABLE IF NOT EXISTS tokens (\n id TEXT PRIMARY KEY,\n slug TEXT UNIQUE NOT NULL,\n concept TEXT NOT NULL,\n domain TEXT NOT NULL DEFAULT '',\n bloom_level INTEGER NOT NULL DEFAULT 1 CHECK (bloom_level BETWEEN 1 AND 5),\n context TEXT NOT NULL DEFAULT '',\n symbiosis_mode TEXT CHECK (symbiosis_mode IN ('shadowing', 'copilot', 'autonomy')),\n source_link TEXT,\n created_at TEXT NOT NULL DEFAULT (datetime('now')),\n updated_at TEXT NOT NULL DEFAULT (datetime('now')),\n deprecated_at TEXT,\n question TEXT\n);\n\n-- Prerequisite dependency graph: \"to learn A, first know B\"\nCREATE TABLE IF NOT EXISTS prerequisites (\n token_id TEXT NOT NULL REFERENCES tokens(id) ON DELETE CASCADE,\n requires_id TEXT NOT NULL REFERENCES tokens(id) ON DELETE CASCADE,\n PRIMARY KEY (token_id, requires_id)\n);\n\n-- Per-user scheduling state for each token (FSRS fields)\nCREATE TABLE IF NOT EXISTS cards (\n id TEXT PRIMARY KEY,\n token_id TEXT NOT NULL REFERENCES tokens(id) ON DELETE CASCADE,\n user_id TEXT NOT NULL,\n stability REAL NOT NULL DEFAULT 0.0,\n difficulty REAL NOT NULL DEFAULT 0.5,\n elapsed_days REAL NOT NULL DEFAULT 0.0,\n scheduled_days REAL NOT NULL DEFAULT 0.0,\n reps INTEGER NOT NULL DEFAULT 0,\n lapses INTEGER NOT NULL DEFAULT 0,\n state TEXT NOT NULL DEFAULT 'new' CHECK (state IN ('new', 'learning', 'review', 'relearning')),\n due_at TEXT NOT NULL DEFAULT (datetime('now')),\n last_review_at TEXT,\n blocked INTEGER NOT NULL DEFAULT 0,\n UNIQUE(token_id, user_id)\n);\n\n-- Immutable review log: every rating event\nCREATE TABLE IF NOT EXISTS review_logs (\n id TEXT PRIMARY KEY,\n card_id TEXT NOT NULL REFERENCES cards(id) ON DELETE CASCADE,\n token_id TEXT NOT NULL REFERENCES tokens(id) ON DELETE CASCADE,\n user_id TEXT NOT NULL,\n rating INTEGER NOT NULL CHECK (rating BETWEEN 1 AND 4),\n response_time_ms INTEGER,\n reviewed_at TEXT NOT NULL DEFAULT (datetime('now')),\n scheduled_at TEXT NOT NULL,\n session_id TEXT REFERENCES sessions(id)\n);\n\n-- Work+learning sessions\nCREATE TABLE IF NOT EXISTS sessions (\n id TEXT PRIMARY KEY,\n user_id TEXT NOT NULL,\n task TEXT NOT NULL,\n started_at TEXT NOT NULL DEFAULT (datetime('now')),\n completed_at TEXT\n);\n\n-- Steps within a session: who did what\nCREATE TABLE IF NOT EXISTS session_steps (\n id TEXT PRIMARY KEY,\n session_id TEXT NOT NULL REFERENCES sessions(id) ON DELETE CASCADE,\n token_id TEXT NOT NULL REFERENCES tokens(id) ON DELETE CASCADE,\n done_by TEXT NOT NULL CHECK (done_by IN ('user', 'agent')),\n rating INTEGER CHECK (rating BETWEEN 1 AND 4),\n notes TEXT,\n created_at TEXT NOT NULL DEFAULT (datetime('now'))\n);\n\n-- User configuration\nCREATE TABLE IF NOT EXISTS user_config (\n key TEXT PRIMARY KEY,\n value TEXT NOT NULL,\n updated_at TEXT NOT NULL DEFAULT (datetime('now'))\n);\n\n-- Agent skills: task recipes the agent learns from user guidance\nCREATE TABLE IF NOT EXISTS agent_skills (\n id TEXT PRIMARY KEY,\n slug TEXT NOT NULL UNIQUE,\n description TEXT NOT NULL,\n steps TEXT NOT NULL DEFAULT '[]', -- JSON array of step strings\n token_slugs TEXT NOT NULL DEFAULT '[]', -- JSON array of related token slugs\n source TEXT NOT NULL DEFAULT 'learned'\n CHECK(source IN ('learned', 'builtin')),\n created_at TEXT NOT NULL DEFAULT (datetime('now')),\n updated_at TEXT NOT NULL DEFAULT (datetime('now'))\n);\n\n-- Performance indexes\nCREATE INDEX IF NOT EXISTS idx_tokens_domain ON tokens(domain);\nCREATE INDEX IF NOT EXISTS idx_tokens_slug ON tokens(slug);\nCREATE INDEX IF NOT EXISTS idx_prereqs_token ON prerequisites(token_id);\nCREATE INDEX IF NOT EXISTS idx_prereqs_requires ON prerequisites(requires_id);\nCREATE INDEX IF NOT EXISTS idx_cards_user_due ON cards(user_id, blocked, due_at);\nCREATE INDEX IF NOT EXISTS idx_cards_token_user ON cards(token_id, user_id);\nCREATE INDEX IF NOT EXISTS idx_review_logs_card ON review_logs(card_id);\nCREATE INDEX IF NOT EXISTS idx_review_logs_user ON review_logs(user_id, reviewed_at);\nCREATE INDEX IF NOT EXISTS idx_session_steps_session ON session_steps(session_id);\n`;\n","/**\n * Goal Engine — manages goal lifecycle via markdown files.\n *\n * Goals live as markdown files in a directory (typically the personal repo's\n * goals/ folder). The engine reads, creates, and updates these files.\n * It does not depend on the database — goals are git-tracked, not DB-tracked.\n */\n\nimport { existsSync, readdirSync, readFileSync, writeFileSync } from \"node:fs\";\nimport { basename, join } from \"node:path\";\nimport type { Goal, GoalStatus } from \"./parser.js\";\nimport {\n extractTasks,\n extractTokenRefs,\n parseGoalFile,\n serializeGoal,\n} from \"./parser.js\";\n\n// ── Types ────────────────────────────────────────────────────────────────────\n\nexport interface GoalSummary {\n slug: string;\n title: string;\n status: GoalStatus;\n parent: string | null;\n taskCount: number;\n tasksDone: number;\n tokenCount: number;\n}\n\nexport interface CreateGoalInput {\n slug: string;\n title: string;\n status?: GoalStatus;\n parent?: string;\n description?: string;\n}\n\n// ── Functions ────────────────────────────────────────────────────────────────\n\n/**\n * List all goals in the goals directory.\n * Returns summaries sorted by status (active first) then title.\n */\nexport function listGoals(goalsDir: string): GoalSummary[] {\n if (!existsSync(goalsDir)) return [];\n\n const files = readdirSync(goalsDir).filter(\n (f) => f.endsWith(\".md\") && f !== \"README.md\",\n );\n\n const summaries: GoalSummary[] = [];\n\n for (const file of files) {\n const filePath = join(goalsDir, file);\n const content = readFileSync(filePath, \"utf-8\");\n const slug = basename(file, \".md\");\n const goal = parseGoalFile(content, slug, filePath);\n const tasks = extractTasks(goal.body);\n const tokens = extractTokenRefs(goal.body);\n\n summaries.push({\n slug: goal.slug,\n title: goal.title,\n status: goal.status,\n parent: goal.parent,\n taskCount: tasks.length,\n tasksDone: tasks.filter((t) => t.done).length,\n tokenCount: tokens.length,\n });\n }\n\n const statusOrder: Record<GoalStatus, number> = {\n active: 0,\n paused: 1,\n completed: 2,\n abandoned: 3,\n };\n\n summaries.sort((a, b) => {\n const statusDiff = statusOrder[a.status] - statusOrder[b.status];\n if (statusDiff !== 0) return statusDiff;\n return a.title.localeCompare(b.title);\n });\n\n return summaries;\n}\n\n/**\n * Get a single goal by slug (filename without .md).\n * Returns undefined if the file doesn't exist.\n */\nexport function getGoal(goalsDir: string, slug: string): Goal | undefined {\n const filePath = join(goalsDir, `${slug}.md`);\n if (!existsSync(filePath)) return undefined;\n\n const content = readFileSync(filePath, \"utf-8\");\n return parseGoalFile(content, slug, filePath);\n}\n\n/**\n * Create a new goal file. Throws if a goal with this slug already exists.\n */\nexport function createGoal(goalsDir: string, input: CreateGoalInput): Goal {\n const filePath = join(goalsDir, `${input.slug}.md`);\n\n if (existsSync(filePath)) {\n throw new Error(`Goal already exists: ${input.slug}`);\n }\n\n const now = new Date().toISOString().slice(0, 10);\n\n const goal: Goal = {\n slug: input.slug,\n title: input.title,\n status: input.status ?? \"active\",\n parent: input.parent ?? null,\n created: now,\n updated: now,\n body: input.description\n ? `## Description\\n${input.description}\\n\\n## Tasks\\n\\n## Tokens`\n : \"## Description\\n\\n## Tasks\\n\\n## Tokens\",\n filePath,\n };\n\n writeFileSync(filePath, serializeGoal(goal), \"utf-8\");\n return goal;\n}\n\n/**\n * Update a goal's status. Writes the updated file back to disk.\n */\nexport function updateGoalStatus(\n goalsDir: string,\n slug: string,\n status: GoalStatus,\n): Goal {\n const goal = getGoal(goalsDir, slug);\n if (!goal) throw new Error(`Goal not found: ${slug}`);\n\n goal.status = status;\n goal.updated = new Date().toISOString().slice(0, 10);\n\n writeFileSync(goal.filePath, serializeGoal(goal), \"utf-8\");\n return goal;\n}\n\n/**\n * Get the goal tree — goals organized by parent relationships.\n * Returns root goals (no parent) with nested children.\n */\nexport function getGoalTree(\n goalsDir: string,\n): Array<GoalSummary & { children: GoalSummary[] }> {\n const all = listGoals(goalsDir);\n const bySlug = new Map(all.map((g) => [g.slug, g]));\n\n const roots: Array<GoalSummary & { children: GoalSummary[] }> = [];\n const children = new Map<string, GoalSummary[]>();\n\n for (const g of all) {\n if (g.parent && bySlug.has(g.parent)) {\n const list = children.get(g.parent) ?? [];\n list.push(g);\n children.set(g.parent, list);\n }\n }\n\n for (const g of all) {\n if (!g.parent || !bySlug.has(g.parent)) {\n roots.push({ ...g, children: children.get(g.slug) ?? [] });\n }\n }\n\n return roots;\n}\n","/**\n * Goal file parser — reads markdown files with YAML-style frontmatter.\n *\n * Goals are persisted as markdown files in the personal repo.\n * Each file has simple key: value frontmatter (no nested structures)\n * and a markdown body with description, tasks, and token references.\n */\n\n// ── Types ────────────────────────────────────────────────────────────────────\n\nexport type GoalStatus = \"active\" | \"completed\" | \"paused\" | \"abandoned\";\n\nexport interface Goal {\n slug: string; // derived from filename (e.g., \"learn-rust\" from \"learn-rust.md\")\n title: string;\n status: GoalStatus;\n parent: string | null; // slug of parent goal\n created: string; // ISO date\n updated: string; // ISO date\n body: string; // markdown body after frontmatter\n filePath: string; // absolute path to the file\n}\n\nexport interface GoalFrontmatter {\n title?: string;\n status?: string;\n parent?: string;\n created?: string;\n updated?: string;\n}\n\n// ── Parser ───────────────────────────────────────────────────────────────────\n\n/**\n * Parse a goal markdown file into a Goal object.\n *\n * Expected format:\n * ```\n * ---\n * title: Learn Rust fundamentals\n * status: active\n * parent: become-systems-programmer\n * created: 2026-03-28\n * updated: 2026-03-28\n * ---\n *\n * ## Description\n * ...\n * ```\n *\n * @param content - Raw file content\n * @param slug - Goal slug (derived from filename by caller)\n * @param filePath - Absolute path to the file\n */\nexport function parseGoalFile(\n content: string,\n slug: string,\n filePath: string,\n): Goal {\n const { frontmatter, body } = splitFrontmatter(content);\n\n const validStatuses: GoalStatus[] = [\n \"active\",\n \"completed\",\n \"paused\",\n \"abandoned\",\n ];\n const status = validStatuses.includes(frontmatter.status as GoalStatus)\n ? (frontmatter.status as GoalStatus)\n : \"active\";\n\n const now = new Date().toISOString().slice(0, 10);\n\n return {\n slug,\n title: frontmatter.title || slug,\n status,\n parent: frontmatter.parent || null,\n created: frontmatter.created || now,\n updated: frontmatter.updated || now,\n body,\n filePath,\n };\n}\n\n/**\n * Serialize a Goal back to markdown with frontmatter.\n */\nexport function serializeGoal(goal: Goal): string {\n const lines = [\"---\", `title: ${goal.title}`, `status: ${goal.status}`];\n\n if (goal.parent) {\n lines.push(`parent: ${goal.parent}`);\n }\n\n lines.push(`created: ${goal.created}`);\n lines.push(`updated: ${goal.updated}`);\n lines.push(\"---\");\n lines.push(\"\");\n\n if (goal.body.trim()) {\n lines.push(goal.body.trim());\n lines.push(\"\");\n }\n\n return lines.join(\"\\n\");\n}\n\n/**\n * Extract tasks (checklist items) from goal body.\n * Returns items like { text: \"Complete Rustlings\", done: false }.\n */\nexport function extractTasks(\n body: string,\n): Array<{ text: string; done: boolean }> {\n const tasks: Array<{ text: string; done: boolean }> = [];\n const taskRegex = /^[-*]\\s+\\[([ xX])\\]\\s+(.+)$/gm;\n let match: RegExpExecArray | null = taskRegex.exec(body);\n\n while (match !== null) {\n tasks.push({\n done: match[1] !== \" \",\n text: match[2].trim(),\n });\n match = taskRegex.exec(body);\n }\n\n return tasks;\n}\n\n/**\n * Extract token references from goal body.\n * Looks for lines like `- token/slug` under a \"## Tokens\" section.\n */\nexport function extractTokenRefs(body: string): string[] {\n const tokensSection = body.match(/## Tokens\\n([\\s\\S]*?)(?=\\n## |\\n*$)/);\n if (!tokensSection) return [];\n\n const refs: string[] = [];\n const lines = tokensSection[1].split(\"\\n\");\n\n for (const line of lines) {\n const match = line.match(/^[-*]\\s+(\\S+)/);\n if (match) {\n refs.push(match[1]);\n }\n }\n\n return refs;\n}\n\n// ── Internal helpers ─────────────────────────────────────────────────────────\n\nfunction splitFrontmatter(content: string): {\n frontmatter: GoalFrontmatter;\n body: string;\n} {\n const trimmed = content.trim();\n\n if (!trimmed.startsWith(\"---\")) {\n return { frontmatter: {}, body: trimmed };\n }\n\n const endIndex = trimmed.indexOf(\"---\", 3);\n if (endIndex === -1) {\n return { frontmatter: {}, body: trimmed };\n }\n\n const fmBlock = trimmed.slice(3, endIndex).trim();\n const body = trimmed.slice(endIndex + 3).trim();\n\n const frontmatter: GoalFrontmatter = {};\n for (const line of fmBlock.split(\"\\n\")) {\n const colonIndex = line.indexOf(\":\");\n if (colonIndex === -1) continue;\n\n const key = line.slice(0, colonIndex).trim();\n const value = line.slice(colonIndex + 1).trim();\n\n if (key && value) {\n (frontmatter as Record<string, string>)[key] = value;\n }\n }\n\n return { frontmatter, body };\n}\n","/**\n * Agent skills: task recipes the agent learns from user guidance.\n *\n * When the agent cannot execute a step, it admits it, asks for guidance,\n * and saves the successful approach here. Skills are linked to tokens so\n * FSRS decay naturally resurfaces them for review — automation ≠ retention.\n */\n\nimport type { Database } from \"libsql\";\nimport { ulid } from \"ulid\";\n\n// ── Types ────────────────────────────────────────────────────────────────────\n\nexport type SkillSource = \"learned\" | \"builtin\";\n\nexport interface AgentSkill {\n id: string;\n slug: string;\n description: string;\n steps: string[]; // parsed from JSON\n token_slugs: string[]; // parsed from JSON\n source: SkillSource;\n created_at: string;\n updated_at: string;\n}\n\n/** Raw DB row — steps and token_slugs are stored as JSON strings */\ninterface AgentSkillRow {\n id: string;\n slug: string;\n description: string;\n steps: string;\n token_slugs: string;\n source: SkillSource;\n created_at: string;\n updated_at: string;\n}\n\nexport interface CreateAgentSkillInput {\n slug: string;\n description: string;\n steps: string[];\n token_slugs?: string[];\n source?: SkillSource;\n}\n\n// ── Helpers ──────────────────────────────────────────────────────────────────\n\nfunction parseRow(row: AgentSkillRow): AgentSkill {\n return {\n ...row,\n steps: JSON.parse(row.steps) as string[],\n token_slugs: JSON.parse(row.token_slugs) as string[],\n };\n}\n\n// ── Functions ────────────────────────────────────────────────────────────────\n\nexport function createAgentSkill(\n db: Database,\n input: CreateAgentSkillInput,\n): AgentSkill {\n const existing = db\n .prepare(\"SELECT * FROM agent_skills WHERE slug = ?\")\n .get(input.slug) as AgentSkillRow | undefined;\n\n if (existing) {\n throw new Error(`Agent skill already exists: ${input.slug}`);\n }\n\n const id = ulid();\n const now = new Date().toISOString();\n\n db.prepare(\n `INSERT INTO agent_skills (id, slug, description, steps, token_slugs, source, created_at, updated_at)\n VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,\n ).run(\n id,\n input.slug,\n input.description,\n JSON.stringify(input.steps),\n JSON.stringify(input.token_slugs ?? []),\n input.source ?? \"learned\",\n now,\n now,\n );\n\n return parseRow(\n db\n .prepare(\"SELECT * FROM agent_skills WHERE id = ?\")\n .get(id) as AgentSkillRow,\n );\n}\n\nexport function getAgentSkill(\n db: Database,\n slug: string,\n): AgentSkill | undefined {\n const row = db\n .prepare(\"SELECT * FROM agent_skills WHERE slug = ?\")\n .get(slug) as AgentSkillRow | undefined;\n\n return row ? parseRow(row) : undefined;\n}\n\nexport function listAgentSkills(db: Database): AgentSkill[] {\n const rows = db\n .prepare(\"SELECT * FROM agent_skills ORDER BY created_at ASC\")\n .all() as AgentSkillRow[];\n\n return rows.map(parseRow);\n}\n","/**\n * Card repository — typed wrappers around the cards table.\n *\n * Each card tracks one user's scheduling state for one token,\n * using FSRS fields (stability, difficulty, elapsed_days, etc.).\n */\n\nimport type { Database } from \"libsql\";\nimport { ulid } from \"ulid\";\n\n// ── Types ────────────────────────────────────────────────────────────────────\n\nexport type CardState = \"new\" | \"learning\" | \"review\" | \"relearning\";\n\nexport interface Card {\n id: string;\n token_id: string;\n user_id: string;\n stability: number;\n difficulty: number;\n elapsed_days: number;\n scheduled_days: number;\n reps: number;\n lapses: number;\n state: CardState;\n due_at: string;\n last_review_at: string | null;\n blocked: number; // 0 or 1\n}\n\nexport interface UpdateCardInput {\n stability?: number;\n difficulty?: number;\n elapsed_days?: number;\n scheduled_days?: number;\n reps?: number;\n lapses?: number;\n state?: CardState;\n due_at?: string;\n last_review_at?: string | null;\n blocked?: number;\n}\n\nexport interface CardDeletionImpact {\n review_logs: number;\n}\n\nexport interface DeleteCardResult {\n card: Card;\n impact: CardDeletionImpact;\n}\n\n/** A due card joined with its token details. */\nexport interface DueCard extends Card {\n slug: string;\n concept: string;\n domain: string;\n bloom_level: number;\n}\n\n/** A blocked card joined with its token details. */\nexport interface BlockedCard extends Card {\n slug: string;\n concept: string;\n domain: string;\n bloom_level: number;\n}\n\n// ── Functions ────────────────────────────────────────────────────────────────\n\n/**\n * Ensure a card exists for the given token+user pair.\n *\n * If one already exists, return it. Otherwise create a new card with\n * default FSRS values (due immediately) and return it.\n *\n * Ported from the PoC's ensureCard helper.\n */\nexport function ensureCard(\n db: Database,\n tokenId: string,\n userId: string,\n): Card {\n const existing = db\n .prepare(\"SELECT * FROM cards WHERE token_id = ? AND user_id = ?\")\n .get(tokenId, userId) as Card | undefined;\n\n if (existing) return existing;\n\n const id = ulid();\n const now = new Date().toISOString();\n\n db.prepare(\n `INSERT INTO cards (id, token_id, user_id, due_at)\n VALUES (?, ?, ?, ?)`,\n ).run(id, tokenId, userId, now);\n\n return db.prepare(\"SELECT * FROM cards WHERE id = ?\").get(id) as Card;\n}\n\n/**\n * Get a card by token+user. Returns undefined if no card exists.\n */\nexport function getCard(\n db: Database,\n tokenId: string,\n userId: string,\n): Card | undefined {\n return db\n .prepare(\"SELECT * FROM cards WHERE token_id = ? AND user_id = ?\")\n .get(tokenId, userId) as Card | undefined;\n}\n\n/**\n * Get a card by its ULID.\n */\nexport function getCardById(db: Database, cardId: string): Card | undefined {\n return db.prepare(\"SELECT * FROM cards WHERE id = ?\").get(cardId) as\n | Card\n | undefined;\n}\n\n/**\n * Update a card's scheduling fields.\n *\n * Only the fields present in `updates` are changed. Throws if the card\n * does not exist.\n */\nexport function updateCard(\n db: Database,\n cardId: string,\n updates: UpdateCardInput,\n): Card {\n const fields: string[] = [];\n const values: unknown[] = [];\n\n if (updates.stability !== undefined) {\n fields.push(\"stability = ?\");\n values.push(updates.stability);\n }\n if (updates.difficulty !== undefined) {\n fields.push(\"difficulty = ?\");\n values.push(updates.difficulty);\n }\n if (updates.elapsed_days !== undefined) {\n fields.push(\"elapsed_days = ?\");\n values.push(updates.elapsed_days);\n }\n if (updates.scheduled_days !== undefined) {\n fields.push(\"scheduled_days = ?\");\n values.push(updates.scheduled_days);\n }\n if (updates.reps !== undefined) {\n fields.push(\"reps = ?\");\n values.push(updates.reps);\n }\n if (updates.lapses !== undefined) {\n fields.push(\"lapses = ?\");\n values.push(updates.lapses);\n }\n if (updates.state !== undefined) {\n fields.push(\"state = ?\");\n values.push(updates.state);\n }\n if (updates.due_at !== undefined) {\n fields.push(\"due_at = ?\");\n values.push(updates.due_at);\n }\n if (updates.last_review_at !== undefined) {\n fields.push(\"last_review_at = ?\");\n values.push(updates.last_review_at);\n }\n if (updates.blocked !== undefined) {\n fields.push(\"blocked = ?\");\n values.push(updates.blocked);\n }\n\n if (fields.length === 0) {\n throw new Error(\"updateCard called with no fields to update\");\n }\n\n values.push(cardId);\n\n const result = db\n .prepare(`UPDATE cards SET ${fields.join(\", \")} WHERE id = ?`)\n .run(...values);\n\n if (result.changes === 0) {\n throw new Error(`Card not found: ${cardId}`);\n }\n\n return db.prepare(\"SELECT * FROM cards WHERE id = ?\").get(cardId) as Card;\n}\n\n/**\n * Preview the review-log rows that will be removed when deleting a user's card.\n */\nexport function getCardDeletionImpact(\n db: Database,\n tokenId: string,\n userId: string,\n): CardDeletionImpact {\n const card = getCard(db, tokenId, userId);\n if (!card) {\n throw new Error(`Card not found for token ${tokenId} and user ${userId}`);\n }\n\n const reviewLogs = db\n .prepare(\"SELECT COUNT(*) AS n FROM review_logs WHERE card_id = ?\")\n .get(card.id) as { n: number };\n\n return { review_logs: reviewLogs.n };\n}\n\n/**\n * Delete one user's card for a token. Review logs cascade via FK.\n */\nexport function deleteCardForUser(\n db: Database,\n tokenId: string,\n userId: string,\n): DeleteCardResult {\n const card = getCard(db, tokenId, userId);\n if (!card) {\n throw new Error(`Card not found for token ${tokenId} and user ${userId}`);\n }\n\n const impact = getCardDeletionImpact(db, tokenId, userId);\n db.prepare(\"DELETE FROM cards WHERE id = ?\").run(card.id);\n\n return { card, impact };\n}\n\n/**\n * Get all cards that are due for review.\n *\n * A card is due when it is not blocked and due_at <= now.\n * Results are ordered by bloom_level ascending (fundamentals first),\n * then by due_at ascending (oldest first).\n *\n * Ported from the PoC's due-tokens command.\n */\nexport function getDueCards(\n db: Database,\n userId: string,\n now?: string,\n): DueCard[] {\n const cutoff = now ?? new Date().toISOString();\n\n return db\n .prepare(\n `SELECT c.*, t.slug, t.concept, t.domain, t.bloom_level\n FROM cards c\n JOIN tokens t ON t.id = c.token_id\n WHERE c.user_id = ? AND c.blocked = 0 AND c.due_at <= ?\n ORDER BY t.bloom_level ASC, c.due_at ASC`,\n )\n .all(userId, cutoff) as DueCard[];\n}\n\n/**\n * Get all blocked cards for a user.\n *\n * Returns cards joined with their token details so the caller can\n * see what is waiting and why.\n */\nexport function getBlockedCards(db: Database, userId: string): BlockedCard[] {\n return db\n .prepare(\n `SELECT c.*, t.slug, t.concept, t.domain, t.bloom_level\n FROM cards c\n JOIN tokens t ON t.id = c.token_id\n WHERE c.user_id = ? AND c.blocked = 1\n ORDER BY t.bloom_level ASC, t.slug ASC`,\n )\n .all(userId) as BlockedCard[];\n}\n","/**\n * Prerequisite repository — typed wrappers around the prerequisites table.\n *\n * Models the dependency graph: \"to learn token A, first know token B.\"\n */\n\nimport type { Database } from \"libsql\";\n\n// ── Types ────────────────────────────────────────────────────────────────────\n\nexport interface Prerequisite {\n token_id: string;\n requires_id: string;\n}\n\n/** A prerequisite row joined with the token it points to. */\nexport interface PrerequisiteWithToken extends Prerequisite {\n slug: string;\n concept: string;\n domain: string;\n bloom_level: number;\n}\n\n// ── Functions ────────────────────────────────────────────────────────────────\n\n/**\n * Add a prerequisite edge: tokenId requires requiresId.\n *\n * Idempotent — silently ignores duplicate edges.\n * Throws if either token ID does not exist (FK constraint).\n * Throws if a token is declared as its own prerequisite.\n */\nexport function addPrerequisite(\n db: Database,\n tokenId: string,\n requiresId: string,\n): void {\n if (tokenId === requiresId) {\n throw new Error(\"A token cannot be a prerequisite of itself\");\n }\n\n db.prepare(\n \"INSERT OR IGNORE INTO prerequisites (token_id, requires_id) VALUES (?, ?)\",\n ).run(tokenId, requiresId);\n}\n\n/**\n * Get the direct prerequisites of a token — \"what does token X require?\"\n *\n * Returns prerequisite rows joined with the required token's details.\n */\nexport function getPrerequisites(\n db: Database,\n tokenId: string,\n): PrerequisiteWithToken[] {\n return db\n .prepare(\n `SELECT p.token_id, p.requires_id, t.slug, t.concept, t.domain, t.bloom_level\n FROM prerequisites p\n JOIN tokens t ON t.id = p.requires_id\n WHERE p.token_id = ?`,\n )\n .all(tokenId) as PrerequisiteWithToken[];\n}\n\n/**\n * Get the direct dependents of a token — \"what depends on token X?\"\n *\n * Returns prerequisite rows joined with the dependent token's details.\n */\nexport function getDependents(\n db: Database,\n tokenId: string,\n): PrerequisiteWithToken[] {\n return db\n .prepare(\n `SELECT p.token_id, p.requires_id, t.slug, t.concept, t.domain, t.bloom_level\n FROM prerequisites p\n JOIN tokens t ON t.id = p.token_id\n WHERE p.requires_id = ?`,\n )\n .all(tokenId) as PrerequisiteWithToken[];\n}\n","/**\n * Review log repository — typed wrappers around the review_logs table.\n *\n * The review log is immutable: every rating event is appended, never\n * updated or deleted. This provides a complete audit trail of a user's\n * learning history.\n */\n\nimport type { Database } from \"libsql\";\nimport { ulid } from \"ulid\";\n\n// ── Types ────────────────────────────────────────────────────────────────────\n\nexport interface ReviewLog {\n id: string;\n card_id: string;\n token_id: string;\n user_id: string;\n rating: number; // 1-4\n response_time_ms: number | null;\n reviewed_at: string;\n scheduled_at: string;\n session_id: string | null;\n}\n\nexport interface CreateReviewInput {\n card_id: string;\n token_id: string;\n user_id: string;\n rating: number; // 1-4\n scheduled_at: string;\n response_time_ms?: number | null;\n session_id?: string | null;\n}\n\nexport interface ListReviewsOptions {\n /** Maximum number of reviews to return. */\n limit?: number;\n /** Return reviews after this ISO timestamp. */\n after?: string;\n /** Return reviews before this ISO timestamp. */\n before?: string;\n}\n\n// ── Functions ────────────────────────────────────────────────────────────────\n\n/**\n * Log an immutable review event.\n *\n * Validates that the rating is between 1 and 4 (matching the schema CHECK).\n * Returns the created review log entry.\n */\nexport function logReview(db: Database, input: CreateReviewInput): ReviewLog {\n if (input.rating < 1 || input.rating > 4) {\n throw new Error(`Rating must be between 1 and 4, got ${input.rating}`);\n }\n\n const id = ulid();\n const now = new Date().toISOString();\n\n db.prepare(\n `INSERT INTO review_logs (id, card_id, token_id, user_id, rating, response_time_ms, reviewed_at, scheduled_at, session_id)\n VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,\n ).run(\n id,\n input.card_id,\n input.token_id,\n input.user_id,\n input.rating,\n input.response_time_ms ?? null,\n now,\n input.scheduled_at,\n input.session_id ?? null,\n );\n\n return db\n .prepare(\"SELECT * FROM review_logs WHERE id = ?\")\n .get(id) as ReviewLog;\n}\n\n/**\n * Get all reviews for a specific card, ordered by reviewed_at ascending.\n */\nexport function getReviewsForCard(db: Database, cardId: string): ReviewLog[] {\n return db\n .prepare(\n \"SELECT * FROM review_logs WHERE card_id = ? ORDER BY reviewed_at ASC\",\n )\n .all(cardId) as ReviewLog[];\n}\n\n/**\n * Get reviews for a user, with optional filtering.\n *\n * Results are ordered by reviewed_at descending (most recent first).\n */\nexport function getReviewsForUser(\n db: Database,\n userId: string,\n options?: ListReviewsOptions,\n): ReviewLog[] {\n const conditions = [\"user_id = ?\"];\n const params: unknown[] = [userId];\n\n if (options?.after) {\n conditions.push(\"reviewed_at > ?\");\n params.push(options.after);\n }\n if (options?.before) {\n conditions.push(\"reviewed_at < ?\");\n params.push(options.before);\n }\n\n let sql = `SELECT * FROM review_logs WHERE ${conditions.join(\" AND \")} ORDER BY reviewed_at DESC`;\n\n if (options?.limit) {\n sql += \" LIMIT ?\";\n params.push(options.limit);\n }\n\n return db.prepare(sql).all(...params) as ReviewLog[];\n}\n","/**\n * Session repository — typed wrappers around sessions and session_steps.\n *\n * A session represents a work+learning episode. Steps within a session\n * record which tokens were touched and by whom (user or agent).\n */\n\nimport type { Database } from \"libsql\";\nimport { ulid } from \"ulid\";\n\n// ── Types ────────────────────────────────────────────────────────────────────\n\nexport type ExecutionContext = \"shell\" | \"ui\" | \"reallife\";\n\nexport interface Session {\n id: string;\n user_id: string;\n task: string;\n execution_context: ExecutionContext;\n started_at: string;\n completed_at: string | null;\n}\n\nexport interface SessionStep {\n id: string;\n session_id: string;\n token_id: string;\n done_by: \"user\" | \"agent\";\n rating: number | null; // 1-4 or null\n notes: string | null;\n created_at: string;\n}\n\nexport interface CreateSessionInput {\n user_id: string;\n task: string;\n execution_context?: ExecutionContext;\n}\n\nexport interface LogStepInput {\n session_id: string;\n token_id: string;\n done_by: \"user\" | \"agent\";\n rating?: number | null;\n notes?: string | null;\n}\n\n/** A step joined with its token details, returned by getSessionSummary. */\nexport interface StepWithToken extends SessionStep {\n slug: string;\n concept: string;\n domain: string;\n bloom_level: number;\n}\n\nexport interface SessionSummary {\n session: Session;\n steps: StepWithToken[];\n}\n\n// ── Functions ────────────────────────────────────────────────────────────────\n\n/**\n * Start a new session. Returns the created session.\n *\n * Ported from the PoC's start-session command.\n */\nexport function startSession(db: Database, input: CreateSessionInput): Session {\n const id = ulid();\n const now = new Date().toISOString();\n const ctx = input.execution_context ?? \"shell\";\n\n db.prepare(\n `INSERT INTO sessions (id, user_id, task, execution_context, started_at)\n VALUES (?, ?, ?, ?, ?)`,\n ).run(id, input.user_id, input.task, ctx, now);\n\n return db.prepare(\"SELECT * FROM sessions WHERE id = ?\").get(id) as Session;\n}\n\n/**\n * End a session by setting its completed_at timestamp.\n *\n * Throws if the session does not exist or is already completed.\n *\n * Ported from the PoC's end-session command.\n */\nexport function endSession(db: Database, sessionId: string): Session {\n const session = db\n .prepare(\"SELECT * FROM sessions WHERE id = ?\")\n .get(sessionId) as Session | undefined;\n\n if (!session) {\n throw new Error(`Session not found: ${sessionId}`);\n }\n if (session.completed_at) {\n throw new Error(`Session already completed: ${sessionId}`);\n }\n\n const now = new Date().toISOString();\n db.prepare(\"UPDATE sessions SET completed_at = ? WHERE id = ?\").run(\n now,\n sessionId,\n );\n\n return db\n .prepare(\"SELECT * FROM sessions WHERE id = ?\")\n .get(sessionId) as Session;\n}\n\n/**\n * Log a step within a session.\n *\n * Validates that done_by is 'user' or 'agent' and that the rating\n * (if provided) is between 1 and 4.\n *\n * Ported from the PoC's log-step command.\n */\nexport function logStep(db: Database, input: LogStepInput): SessionStep {\n if (input.done_by !== \"user\" && input.done_by !== \"agent\") {\n throw new Error(\n `done_by must be 'user' or 'agent', got '${input.done_by}'`,\n );\n }\n if (input.rating != null && (input.rating < 1 || input.rating > 4)) {\n throw new Error(`Rating must be between 1 and 4, got ${input.rating}`);\n }\n\n // Verify the session exists\n const session = db\n .prepare(\"SELECT id FROM sessions WHERE id = ?\")\n .get(input.session_id);\n if (!session) {\n throw new Error(`Session not found: ${input.session_id}`);\n }\n\n const id = ulid();\n const now = new Date().toISOString();\n\n db.prepare(\n `INSERT INTO session_steps (id, session_id, token_id, done_by, rating, notes, created_at)\n VALUES (?, ?, ?, ?, ?, ?, ?)`,\n ).run(\n id,\n input.session_id,\n input.token_id,\n input.done_by,\n input.rating ?? null,\n input.notes ?? null,\n now,\n );\n\n return db\n .prepare(\"SELECT * FROM session_steps WHERE id = ?\")\n .get(id) as SessionStep;\n}\n\n/**\n * Get a full session summary: the session record plus all steps\n * joined with their token details.\n *\n * Ported from the PoC's session-summary command.\n * Throws if the session does not exist.\n */\nexport function getSessionSummary(\n db: Database,\n sessionId: string,\n): SessionSummary {\n const session = db\n .prepare(\"SELECT * FROM sessions WHERE id = ?\")\n .get(sessionId) as Session | undefined;\n\n if (!session) {\n throw new Error(`Session not found: ${sessionId}`);\n }\n\n const steps = db\n .prepare(\n `SELECT ss.*, t.slug, t.concept, t.domain, t.bloom_level\n FROM session_steps ss\n JOIN tokens t ON t.id = ss.token_id\n WHERE ss.session_id = ?\n ORDER BY ss.created_at ASC`,\n )\n .all(sessionId) as StepWithToken[];\n\n return { session, steps };\n}\n","/**\n * User settings — key/value store backed by the user_config table.\n */\n\nimport type { Database } from \"libsql\";\n\nexport interface UserSetting {\n key: string;\n value: string;\n updated_at: string;\n}\n\n/** Get a single setting by key. Returns undefined if not set. */\nexport function getSetting(db: Database, key: string): string | undefined {\n const row = db\n .prepare(\"SELECT value FROM user_config WHERE key = ?\")\n .get(key) as { value: string } | undefined;\n return row?.value;\n}\n\n/** Get all settings as a key-value map. */\nexport function getAllSettings(db: Database): Record<string, string> {\n const rows = db\n .prepare(\"SELECT key, value FROM user_config ORDER BY key\")\n .all() as { key: string; value: string }[];\n const map: Record<string, string> = {};\n for (const row of rows) {\n map[row.key] = row.value;\n }\n return map;\n}\n\n/** Get all settings with metadata. */\nexport function getAllSettingsDetailed(db: Database): UserSetting[] {\n return db\n .prepare(\"SELECT key, value, updated_at FROM user_config ORDER BY key\")\n .all() as UserSetting[];\n}\n\n/** Set a setting (insert or update). */\nexport function setSetting(db: Database, key: string, value: string): void {\n db.prepare(\n `INSERT INTO user_config (key, value, updated_at)\n VALUES (?, ?, datetime('now'))\n ON CONFLICT(key) DO UPDATE SET value = excluded.value, updated_at = excluded.updated_at`,\n ).run(key, value);\n}\n\n/** Delete a setting. Returns true if it existed. */\nexport function deleteSetting(db: Database, key: string): boolean {\n const result = db.prepare(\"DELETE FROM user_config WHERE key = ?\").run(key);\n return result.changes > 0;\n}\n","/**\n * Token repository — typed wrappers around the tokens table.\n *\n * Tokens are atomic knowledge concepts with Bloom taxonomy levels\n * and optional symbiosis modes (shadowing / copilot / autonomy).\n */\n\nimport type { Database } from \"libsql\";\nimport { ulid } from \"ulid\";\n\n// ── Types ────────────────────────────────────────────────────────────────────\n\nexport type BloomLevel = 1 | 2 | 3 | 4 | 5;\n\nexport type SymbiosisMode = \"shadowing\" | \"copilot\" | \"autonomy\";\n\nexport interface Token {\n id: string;\n slug: string;\n concept: string;\n domain: string;\n bloom_level: BloomLevel;\n context: string;\n symbiosis_mode: SymbiosisMode | null;\n source_link: string | null;\n question: string | null;\n created_at: string;\n updated_at: string;\n deprecated_at: string | null;\n}\n\nexport interface CreateTokenInput {\n slug: string;\n concept: string;\n domain?: string;\n bloom_level?: BloomLevel;\n context?: string;\n symbiosis_mode?: SymbiosisMode | null;\n source_link?: string | null;\n question?: string | null;\n}\n\nexport interface UpdateTokenInput {\n concept?: string;\n domain?: string;\n bloom_level?: BloomLevel;\n context?: string;\n symbiosis_mode?: SymbiosisMode | null;\n source_link?: string | null;\n question?: string | null;\n}\n\nexport interface ListTokensOptions {\n domain?: string;\n}\n\nexport interface TokenDeleteImpact {\n cards: number;\n review_logs: number;\n prerequisite_edges_from_token: number;\n prerequisite_edges_to_token: number;\n session_steps: number;\n sessions_touched: number;\n agent_skills: number;\n}\n\nexport interface DeleteTokenResult {\n token: Token;\n impact: TokenDeleteImpact;\n}\n\n// ── Scored result from fuzzy search ──────────────────────────────────────────\n\nexport interface ScoredToken extends Token {\n score: number;\n}\n\n// ── Functions ────────────────────────────────────────────────────────────────\n\n/**\n * Create a new knowledge token.\n * Throws if a token with the same slug already exists.\n */\nexport function createToken(db: Database, input: CreateTokenInput): Token {\n const id = ulid();\n const now = new Date().toISOString();\n\n const bloom = input.bloom_level ?? 1;\n if (bloom < 1 || bloom > 5) {\n throw new Error(`bloom_level must be between 1 and 5, got ${bloom}`);\n }\n\n db.prepare(`\n INSERT INTO tokens (id, slug, concept, domain, bloom_level, context, symbiosis_mode, source_link, question, created_at, updated_at)\n VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)\n `).run(\n id,\n input.slug,\n input.concept,\n input.domain ?? \"\",\n bloom,\n input.context ?? \"\",\n input.symbiosis_mode ?? null,\n input.source_link ?? null,\n input.question ?? null,\n now,\n now,\n );\n\n return getTokenById(db, id)!;\n}\n\n/**\n * Look up a token by its unique slug.\n * Returns undefined if not found.\n */\nexport function getTokenBySlug(db: Database, slug: string): Token | undefined {\n return db.prepare(\"SELECT * FROM tokens WHERE slug = ?\").get(slug) as\n | Token\n | undefined;\n}\n\n/**\n * Look up a token by its ULID.\n * Returns undefined if not found.\n */\nexport function getTokenById(db: Database, id: string): Token | undefined {\n return db.prepare(\"SELECT * FROM tokens WHERE id = ?\").get(id) as\n | Token\n | undefined;\n}\n\n/**\n * Update mutable fields on a token.\n *\n * Slug is intentionally immutable in v1 because it is referenced by other\n * parts of the system (for example agent skill metadata).\n */\nexport function updateToken(\n db: Database,\n slug: string,\n updates: UpdateTokenInput,\n): Token {\n const token = getTokenBySlug(db, slug);\n if (!token) {\n throw new Error(`Token not found: ${slug}`);\n }\n\n const fields: string[] = [];\n const values: unknown[] = [];\n\n if (updates.concept !== undefined) {\n fields.push(\"concept = ?\");\n values.push(updates.concept);\n }\n if (updates.domain !== undefined) {\n fields.push(\"domain = ?\");\n values.push(updates.domain);\n }\n if (updates.bloom_level !== undefined) {\n if (updates.bloom_level < 1 || updates.bloom_level > 5) {\n throw new Error(\n `bloom_level must be between 1 and 5, got ${updates.bloom_level}`,\n );\n }\n fields.push(\"bloom_level = ?\");\n values.push(updates.bloom_level);\n }\n if (updates.context !== undefined) {\n fields.push(\"context = ?\");\n values.push(updates.context);\n }\n if (updates.symbiosis_mode !== undefined) {\n const validModes = [\"shadowing\", \"copilot\", \"autonomy\"];\n if (\n updates.symbiosis_mode !== null &&\n !validModes.includes(updates.symbiosis_mode)\n ) {\n throw new Error(`Invalid symbiosis_mode: ${updates.symbiosis_mode}`);\n }\n fields.push(\"symbiosis_mode = ?\");\n values.push(updates.symbiosis_mode);\n }\n if (updates.source_link !== undefined) {\n fields.push(\"source_link = ?\");\n values.push(updates.source_link);\n }\n if (updates.question !== undefined) {\n fields.push(\"question = ?\");\n values.push(updates.question);\n }\n\n if (fields.length === 0) {\n throw new Error(\"updateToken called with no fields to update\");\n }\n\n fields.push(\"updated_at = ?\");\n values.push(new Date().toISOString());\n values.push(slug);\n\n db.prepare(`UPDATE tokens SET ${fields.join(\", \")} WHERE slug = ?`).run(\n ...values,\n );\n return getTokenBySlug(db, slug)!;\n}\n\n/**\n * Mark a token as deprecated. Deprecated tokens are excluded from review queues\n * and search results but are not deleted — they can still be consulted.\n *\n * Throws if the token does not exist or is already deprecated.\n */\nexport function deprecateToken(db: Database, slug: string): Token {\n const token = getTokenBySlug(db, slug);\n if (!token) {\n throw new Error(`Token not found: ${slug}`);\n }\n if (token.deprecated_at) {\n throw new Error(`Token already deprecated: ${slug}`);\n }\n\n const now = new Date().toISOString();\n db.prepare(\n \"UPDATE tokens SET deprecated_at = ?, updated_at = ? WHERE slug = ?\",\n ).run(now, now, slug);\n\n return getTokenBySlug(db, slug)!;\n}\n\n/**\n * Preview the rows that will be removed or updated when deleting a token.\n */\nexport function getTokenDeleteImpact(\n db: Database,\n slug: string,\n): TokenDeleteImpact {\n const token = getTokenBySlug(db, slug);\n if (!token) {\n throw new Error(`Token not found: ${slug}`);\n }\n\n const cards = db\n .prepare(\"SELECT COUNT(*) AS n FROM cards WHERE token_id = ?\")\n .get(token.id) as { n: number };\n const reviewLogs = db\n .prepare(\"SELECT COUNT(*) AS n FROM review_logs WHERE token_id = ?\")\n .get(token.id) as { n: number };\n const prereqsFrom = db\n .prepare(\"SELECT COUNT(*) AS n FROM prerequisites WHERE token_id = ?\")\n .get(token.id) as { n: number };\n const prereqsTo = db\n .prepare(\"SELECT COUNT(*) AS n FROM prerequisites WHERE requires_id = ?\")\n .get(token.id) as { n: number };\n const sessionSteps = db\n .prepare(\"SELECT COUNT(*) AS n FROM session_steps WHERE token_id = ?\")\n .get(token.id) as { n: number };\n const sessionsTouched = db\n .prepare(\n \"SELECT COUNT(DISTINCT session_id) AS n FROM session_steps WHERE token_id = ?\",\n )\n .get(token.id) as { n: number };\n\n const skillRows = db\n .prepare(\"SELECT token_slugs FROM agent_skills\")\n .all() as Array<{ token_slugs: string }>;\n const agentSkills = skillRows.filter((row) => {\n const tokenSlugs = JSON.parse(row.token_slugs) as string[];\n return tokenSlugs.includes(slug);\n }).length;\n\n return {\n cards: cards.n,\n review_logs: reviewLogs.n,\n prerequisite_edges_from_token: prereqsFrom.n,\n prerequisite_edges_to_token: prereqsTo.n,\n session_steps: sessionSteps.n,\n sessions_touched: sessionsTouched.n,\n agent_skills: agentSkills,\n };\n}\n\n/**\n * Hard-delete a token and clean up non-FK references that point at its slug.\n */\nexport function deleteToken(db: Database, slug: string): DeleteTokenResult {\n const token = getTokenBySlug(db, slug);\n if (!token) {\n throw new Error(`Token not found: ${slug}`);\n }\n\n const impact = getTokenDeleteImpact(db, slug);\n\n db.exec(\"BEGIN\");\n try {\n const now = new Date().toISOString();\n const skillRows = db\n .prepare(\"SELECT id, token_slugs FROM agent_skills\")\n .all() as Array<{ id: string; token_slugs: string }>;\n\n for (const row of skillRows) {\n const tokenSlugs = JSON.parse(row.token_slugs) as string[];\n const filtered = tokenSlugs.filter((tokenSlug) => tokenSlug !== slug);\n if (filtered.length !== tokenSlugs.length) {\n db.prepare(\n \"UPDATE agent_skills SET token_slugs = ?, updated_at = ? WHERE id = ?\",\n ).run(JSON.stringify(filtered), now, row.id);\n }\n }\n\n db.prepare(\"DELETE FROM tokens WHERE id = ?\").run(token.id);\n db.exec(\"COMMIT\");\n } catch (err) {\n db.exec(\"ROLLBACK\");\n throw err;\n }\n\n return { token, impact };\n}\n\n/**\n * Fuzzy search for tokens by keyword query.\n *\n * Ported from the PoC's find-token command: splits the query into word\n * tokens, scores each database token by word overlap plus a substring\n * bonus on the concept field, and returns all matches sorted by score\n * descending.\n */\nexport function findTokens(db: Database, query: string): ScoredToken[] {\n const normalised = query.toLowerCase();\n const qTokens = new Set(\n normalised.split(/[\\s,.\\-_/\\\\:;!?()[\\]{}]+/).filter((t) => t.length > 2),\n );\n\n const tokens = db\n .prepare(\"SELECT * FROM tokens WHERE deprecated_at IS NULL\")\n .all() as Token[];\n\n const scored: ScoredToken[] = [];\n\n for (const t of tokens) {\n const words = `${t.slug} ${t.concept} ${t.domain}`\n .toLowerCase()\n .split(/[\\s,.\\-_/\\\\:;!?()[\\]{}]+/)\n .filter(Boolean);\n\n let score = 0;\n for (const w of words) {\n if (qTokens.has(w)) score++;\n }\n\n // Substring bonus: if the concept contains the start of the query\n if (t.concept.toLowerCase().includes(normalised.slice(0, 25))) {\n score += 3;\n }\n\n if (score > 0) {\n scored.push({ score, ...t });\n }\n }\n\n scored.sort((a, b) => b.score - a.score);\n return scored;\n}\n\n/**\n * List all tokens, optionally filtered by domain.\n * Results are ordered by bloom_level then slug.\n */\nexport function listTokens(db: Database, options?: ListTokensOptions): Token[] {\n if (options?.domain) {\n return db\n .prepare(\n \"SELECT * FROM tokens WHERE domain = ? AND deprecated_at IS NULL ORDER BY bloom_level, slug\",\n )\n .all(options.domain) as Token[];\n }\n return db\n .prepare(\n \"SELECT * FROM tokens WHERE deprecated_at IS NULL ORDER BY bloom_level, domain, slug\",\n )\n .all() as Token[];\n}\n","/**\n * Monitor log analyzer — maps observed shell commands to token ratings.\n *\n * Pure functions, no DB or filesystem access. Takes parsed command records\n * and a token-to-pattern mapping, returns ratings with evidence.\n */\n\n// ── Types ────────────────────────────────────────────────────────────────────\n\nexport interface MonitorEvent {\n type: \"command_start\" | \"command_end\" | \"monitor_meta\";\n ts: string;\n seq?: number;\n pid?: number;\n command?: string;\n cwd?: string;\n exit_code?: number;\n event?: \"start\" | \"stop\";\n session_id?: string;\n shell?: string;\n}\n\nexport interface CommandRecord {\n seq: number;\n pid: number;\n command: string;\n cwd: string;\n startedAt: string;\n endedAt: string | null;\n durationMs: number | null;\n exitCode: number | null;\n}\n\nexport interface TokenPattern {\n slug: string;\n patterns: string[]; // command prefixes or regex strings\n}\n\nexport interface ObservationRating {\n tokenSlug: string;\n rating: 1 | 2 | 3 | 4 | null;\n confidence: \"high\" | \"medium\" | \"low\";\n evidence: {\n matchedCommands: number;\n helpSeeking: boolean;\n errorCount: number;\n selfCorrections: number;\n medianGapMs: number | null;\n thinkingGapMs: number | null;\n };\n matchedCommandTexts: string[];\n}\n\nexport interface AnalysisResult {\n ratings: ObservationRating[];\n unmatchedCommands: string[];\n timeSpan: { start: string; end: string; durationMs: number } | null;\n}\n\n// ── Parsing ──────────────────────────────────────────────────────────────────\n\n/**\n * Parse a JSONL string into MonitorEvent objects.\n * Skips malformed lines silently.\n */\nexport function parseMonitorLog(jsonl: string): MonitorEvent[] {\n const events: MonitorEvent[] = [];\n for (const line of jsonl.split(\"\\n\")) {\n const trimmed = line.trim();\n if (!trimmed) continue;\n try {\n events.push(JSON.parse(trimmed) as MonitorEvent);\n } catch {\n // skip malformed lines\n }\n }\n return events;\n}\n\n/**\n * Pair command_start and command_end events by (pid, seq) into CommandRecords.\n */\nexport function pairCommands(events: MonitorEvent[]): CommandRecord[] {\n const starts = new Map<string, MonitorEvent>();\n const records: CommandRecord[] = [];\n\n for (const e of events) {\n if (e.type === \"command_start\" && e.seq != null) {\n const key = `${e.pid ?? 0}:${e.seq}`;\n starts.set(key, e);\n } else if (e.type === \"command_end\" && e.seq != null) {\n const key = `${e.pid ?? 0}:${e.seq}`;\n const start = starts.get(key);\n if (start) {\n const startMs = new Date(start.ts).getTime();\n const endMs = new Date(e.ts).getTime();\n records.push({\n seq: e.seq,\n pid: e.pid ?? 0,\n command: start.command ?? \"\",\n cwd: start.cwd ?? \"\",\n startedAt: start.ts,\n endedAt: e.ts,\n durationMs: endMs - startMs,\n exitCode: e.exit_code ?? null,\n });\n starts.delete(key);\n }\n }\n }\n\n // Remaining unpaired starts (monitoring stopped mid-command)\n for (const [, start] of starts) {\n records.push({\n seq: start.seq ?? 0,\n pid: start.pid ?? 0,\n command: start.command ?? \"\",\n cwd: start.cwd ?? \"\",\n startedAt: start.ts,\n endedAt: null,\n durationMs: null,\n exitCode: null,\n });\n }\n\n records.sort(\n (a, b) => new Date(a.startedAt).getTime() - new Date(b.startedAt).getTime(),\n );\n return records;\n}\n\n// ── Analysis ─────────────────────────────────────────────────────────────────\n\nconst HELP_PATTERNS = [\"--help\", \"man \", \"tldr \", \"help \"];\nconst HELP_WINDOW_MS = 60_000;\nconst _RETRY_WINDOW_MS = 30_000;\n\nfunction matchesToken(command: string, patterns: string[]): boolean {\n const lower = command.toLowerCase();\n return patterns.some((p) => lower.includes(p.toLowerCase()));\n}\n\nfunction isHelpCommand(command: string): boolean {\n const lower = command.toLowerCase();\n return HELP_PATTERNS.some((p) => lower.includes(p));\n}\n\nfunction commandPrefix(command: string): string {\n return command.split(/\\s+/).slice(0, 2).join(\" \").toLowerCase();\n}\n\nfunction computeMedian(values: number[]): number | null {\n if (values.length === 0) return null;\n const sorted = [...values].sort((a, b) => a - b);\n const mid = Math.floor(sorted.length / 2);\n return sorted.length % 2 !== 0\n ? sorted[mid]\n : (sorted[mid - 1] + sorted[mid]) / 2;\n}\n\n/**\n * Analyze observed commands against token patterns and produce ratings.\n */\nexport function analyzeObservation(\n commands: CommandRecord[],\n tokenPatterns: TokenPattern[],\n): AnalysisResult {\n const matchedSet = new Set<number>(); // indices of commands matched to any token\n const ratings: ObservationRating[] = [];\n\n for (const tp of tokenPatterns) {\n const matchIndices: number[] = [];\n const matchedTexts: string[] = [];\n\n for (let i = 0; i < commands.length; i++) {\n if (matchesToken(commands[i].command, tp.patterns)) {\n matchIndices.push(i);\n matchedTexts.push(commands[i].command);\n matchedSet.add(i);\n }\n }\n\n if (matchIndices.length === 0) {\n ratings.push({\n tokenSlug: tp.slug,\n rating: null,\n confidence: \"low\",\n evidence: {\n matchedCommands: 0,\n helpSeeking: false,\n errorCount: 0,\n selfCorrections: 0,\n medianGapMs: null,\n thinkingGapMs: null,\n },\n matchedCommandTexts: [],\n });\n continue;\n }\n\n // Help-seeking: any help command within HELP_WINDOW_MS before a matched command\n let helpSeeking = false;\n for (const mi of matchIndices) {\n const matchTime = new Date(commands[mi].startedAt).getTime();\n for (let j = 0; j < commands.length; j++) {\n if (j === mi) continue;\n const cmdTime = new Date(commands[j].startedAt).getTime();\n if (cmdTime >= matchTime - HELP_WINDOW_MS && cmdTime < matchTime) {\n if (isHelpCommand(commands[j].command)) {\n helpSeeking = true;\n break;\n }\n }\n }\n if (helpSeeking) break;\n }\n\n // Error count: matched commands with non-zero exit code\n let errorCount = 0;\n for (const mi of matchIndices) {\n if (commands[mi].exitCode != null && commands[mi].exitCode !== 0) {\n errorCount++;\n }\n }\n\n // Self-corrections: same command prefix run multiple times with different args\n let selfCorrections = 0;\n const prefixGroups = new Map<string, number>();\n for (const mi of matchIndices) {\n const prefix = commandPrefix(commands[mi].command);\n prefixGroups.set(prefix, (prefixGroups.get(prefix) ?? 0) + 1);\n }\n for (const count of prefixGroups.values()) {\n if (count > 1) selfCorrections += count - 1;\n }\n\n // Speed: inter-command gaps between matched commands\n const gaps: number[] = [];\n for (let k = 1; k < matchIndices.length; k++) {\n const prev = commands[matchIndices[k - 1]];\n const curr = commands[matchIndices[k]];\n if (prev.endedAt) {\n const gap =\n new Date(curr.startedAt).getTime() - new Date(prev.endedAt).getTime();\n if (gap >= 0) gaps.push(gap);\n }\n }\n\n // Thinking gap: time before first matched command from previous command's end\n let thinkingGapMs: number | null = null;\n const firstMatchIdx = matchIndices[0];\n if (firstMatchIdx > 0) {\n const prev = commands[firstMatchIdx - 1];\n if (prev.endedAt) {\n thinkingGapMs =\n new Date(commands[firstMatchIdx].startedAt).getTime() -\n new Date(prev.endedAt).getTime();\n }\n }\n\n const medianGapMs = computeMedian(gaps);\n\n // Determine rating\n const rating = inferRating({\n helpSeeking,\n errorCount,\n selfCorrections,\n medianGapMs,\n thinkingGapMs,\n matchedCommands: matchIndices.length,\n });\n\n // Confidence: more matched commands = higher confidence\n const confidence =\n matchIndices.length >= 3\n ? \"high\"\n : matchIndices.length >= 2\n ? \"medium\"\n : \"low\";\n\n ratings.push({\n tokenSlug: tp.slug,\n rating,\n confidence,\n evidence: {\n matchedCommands: matchIndices.length,\n helpSeeking,\n errorCount,\n selfCorrections,\n medianGapMs,\n thinkingGapMs,\n },\n matchedCommandTexts: matchedTexts,\n });\n }\n\n // Unmatched commands\n const unmatchedCommands: string[] = [];\n for (let i = 0; i < commands.length; i++) {\n if (!matchedSet.has(i) && !isHelpCommand(commands[i].command)) {\n unmatchedCommands.push(commands[i].command);\n }\n }\n\n // Time span\n let timeSpan: AnalysisResult[\"timeSpan\"] = null;\n if (commands.length > 0) {\n const first = commands[0];\n const last = commands[commands.length - 1];\n const endTs = last.endedAt ?? last.startedAt;\n timeSpan = {\n start: first.startedAt,\n end: endTs,\n durationMs:\n new Date(endTs).getTime() - new Date(first.startedAt).getTime(),\n };\n }\n\n return { ratings, unmatchedCommands, timeSpan };\n}\n\n// ── Rating Inference ─────────────────────────────────────────────────────────\n\ninterface RatingSignals {\n helpSeeking: boolean;\n errorCount: number;\n selfCorrections: number;\n medianGapMs: number | null;\n thinkingGapMs: number | null;\n matchedCommands: number;\n}\n\nfunction inferRating(signals: RatingSignals): 1 | 2 | 3 | 4 {\n const {\n helpSeeking,\n errorCount,\n selfCorrections,\n medianGapMs,\n thinkingGapMs,\n } = signals;\n\n // Count negative signals\n let negatives = 0;\n if (helpSeeking) negatives += 2;\n if (errorCount >= 3) negatives += 3;\n else if (errorCount >= 1) negatives += 1;\n if (selfCorrections >= 2) negatives += 2;\n else if (selfCorrections >= 1) negatives += 1;\n if (medianGapMs != null && medianGapMs > 30_000) negatives += 2;\n else if (medianGapMs != null && medianGapMs > 10_000) negatives += 1;\n if (thinkingGapMs != null && thinkingGapMs > 30_000) negatives += 1;\n\n if (negatives >= 5) return 1;\n if (negatives >= 3) return 2;\n if (negatives >= 1) return 3;\n return 4;\n}\n","/**\n * Monitor I/O — read/write JSONL files for shell observation.\n *\n * Monitor logs live at ~/.zam/monitor/<session-id>.jsonl.\n * Separated from analyzer.ts so the analyzer remains pure-function testable.\n */\n\nimport {\n appendFileSync,\n existsSync,\n mkdirSync,\n readFileSync,\n statSync,\n} from \"node:fs\";\nimport { homedir } from \"node:os\";\nimport { join } from \"node:path\";\nimport type { MonitorEvent } from \"./analyzer.js\";\nimport { parseMonitorLog } from \"./analyzer.js\";\n\nconst MONITOR_DIR = join(homedir(), \".zam\", \"monitor\");\n\n/** Get the monitor directory path. */\nexport function getMonitorDir(): string {\n return MONITOR_DIR;\n}\n\n/** Get the JSONL file path for a session. */\nexport function getMonitorPath(sessionId: string): string {\n return join(MONITOR_DIR, `${sessionId}.jsonl`);\n}\n\n/** Ensure the monitor directory exists (mode 0700 for privacy). */\nexport function ensureMonitorDir(): void {\n if (!existsSync(MONITOR_DIR)) {\n mkdirSync(MONITOR_DIR, { recursive: true, mode: 0o700 });\n }\n}\n\n/** Append a single event to the session's JSONL file. */\nexport function writeMonitorEvent(\n sessionId: string,\n event: MonitorEvent,\n): void {\n ensureMonitorDir();\n const path = getMonitorPath(sessionId);\n appendFileSync(path, `${JSON.stringify(event)}\\n`);\n}\n\n/** Read and parse all events from a session's monitor log. */\nexport function readMonitorLog(sessionId: string): MonitorEvent[] {\n const path = getMonitorPath(sessionId);\n if (!existsSync(path)) {\n return [];\n }\n const content = readFileSync(path, \"utf-8\");\n return parseMonitorLog(content);\n}\n\n/** Check if a monitor log exists for a session. */\nexport function monitorLogExists(sessionId: string): boolean {\n return existsSync(getMonitorPath(sessionId));\n}\n\n/** Get basic stats about a monitor log without full parsing. */\nexport function getMonitorLogStats(sessionId: string): {\n exists: boolean;\n sizeBytes: number;\n lineCount: number;\n} {\n const path = getMonitorPath(sessionId);\n if (!existsSync(path)) {\n return { exists: false, sizeBytes: 0, lineCount: 0 };\n }\n const stat = statSync(path);\n const content = readFileSync(path, \"utf-8\");\n const lineCount = content.split(\"\\n\").filter((l) => l.trim()).length;\n return { exists: true, sizeBytes: stat.size, lineCount };\n}\n","/**\n * Shell hook code generation for zsh, bash, and PowerShell.\n *\n * Pure functions that return shell code strings. The CLI command\n * `zam monitor start/stop` calls these and prints to stdout.\n */\n\nfunction psSingleQuoted(value: string): string {\n return `'${value.replace(/'/g, \"''\")}'`;\n}\n\n/**\n * Generate zsh hooks that capture commands to a JSONL file.\n * Uses $EPOCHREALTIME for sub-second timestamp precision.\n */\nexport function generateZshHooks(\n monitorFile: string,\n sessionId: string,\n): string {\n return `\n# ZAM monitor hooks for session ${sessionId}\nexport __ZAM_MONITOR_FILE=\"${monitorFile}\"\nexport __ZAM_MONITOR_SEQ=0\nexport __ZAM_MONITOR_SESSION=\"${sessionId}\"\n\n__zam_ts() {\n if [[ -n \"\\${EPOCHREALTIME:-}\" ]]; then\n local sec=\"\\${EPOCHREALTIME%%.*}\"\n local frac=\"\\${EPOCHREALTIME##*.}\"\n frac=\"\\${frac:0:3}\"\n printf '%s.%sZ' \"$(date -u -r \"$sec\" '+%Y-%m-%dT%H:%M:%S' 2>/dev/null || date -u '+%Y-%m-%dT%H:%M:%S')\" \"$frac\"\n else\n date -u '+%Y-%m-%dT%H:%M:%SZ'\n fi\n}\n\n__zam_preexec() {\n (( __ZAM_MONITOR_SEQ++ ))\n local cmd=\"\\${1//\\\\\"/\\\\\\\\\\\\\"}\"\n local cwd=\"\\${PWD//\\\\\"/\\\\\\\\\\\\\"}\"\n local ts=\"$(__zam_ts)\"\n printf '{\"type\":\"command_start\",\"ts\":\"%s\",\"command\":\"%s\",\"cwd\":\"%s\",\"seq\":%d,\"pid\":%d}\\\\n' \\\\\n \"$ts\" \"$cmd\" \"$cwd\" \"$__ZAM_MONITOR_SEQ\" \"$$\" \\\\\n >> \"$__ZAM_MONITOR_FILE\"\n}\n\n__zam_precmd() {\n local exit_code=$?\n [[ $__ZAM_MONITOR_SEQ -eq 0 ]] && return\n local ts=\"$(__zam_ts)\"\n printf '{\"type\":\"command_end\",\"ts\":\"%s\",\"exit_code\":%d,\"seq\":%d,\"pid\":%d}\\\\n' \\\\\n \"$ts\" \"$exit_code\" \"$__ZAM_MONITOR_SEQ\" \"$$\" \\\\\n >> \"$__ZAM_MONITOR_FILE\"\n}\n\nautoload -Uz add-zsh-hook\nadd-zsh-hook preexec __zam_preexec\nadd-zsh-hook precmd __zam_precmd\n\necho \"ZAM monitor active for session $__ZAM_MONITOR_SESSION\"\n`.trim();\n}\n\n/**\n * Generate bash hooks that capture commands to a JSONL file.\n * Uses DEBUG trap for preexec, PROMPT_COMMAND for precmd.\n */\nexport function generateBashHooks(\n monitorFile: string,\n sessionId: string,\n): string {\n return `\n# ZAM monitor hooks for session ${sessionId}\nexport __ZAM_MONITOR_FILE=\"${monitorFile}\"\nexport __ZAM_MONITOR_SEQ=0\nexport __ZAM_MONITOR_SESSION=\"${sessionId}\"\nexport __ZAM_MONITOR_CMD_ACTIVE=0\n\n__zam_ts() {\n date -u '+%Y-%m-%dT%H:%M:%SZ'\n}\n\n__zam_debug_trap() {\n [[ \"$__ZAM_MONITOR_CMD_ACTIVE\" -eq 1 ]] && return\n __ZAM_MONITOR_CMD_ACTIVE=1\n (( __ZAM_MONITOR_SEQ++ ))\n local cmd=\"\\${BASH_COMMAND//\\\\\"/\\\\\\\\\\\\\"}\"\n local cwd=\"\\${PWD//\\\\\"/\\\\\\\\\\\\\"}\"\n local ts=\"$(__zam_ts)\"\n printf '{\"type\":\"command_start\",\"ts\":\"%s\",\"command\":\"%s\",\"cwd\":\"%s\",\"seq\":%d,\"pid\":%d}\\\\n' \\\\\n \"$ts\" \"$cmd\" \"$cwd\" \"$__ZAM_MONITOR_SEQ\" \"$$\" \\\\\n >> \"$__ZAM_MONITOR_FILE\"\n}\n\n__zam_prompt_cmd() {\n local exit_code=$?\n if [[ \"$__ZAM_MONITOR_CMD_ACTIVE\" -eq 1 ]]; then\n __ZAM_MONITOR_CMD_ACTIVE=0\n local ts=\"$(__zam_ts)\"\n printf '{\"type\":\"command_end\",\"ts\":\"%s\",\"exit_code\":%d,\"seq\":%d,\"pid\":%d}\\\\n' \\\\\n \"$ts\" \"$exit_code\" \"$__ZAM_MONITOR_SEQ\" \"$$\" \\\\\n >> \"$__ZAM_MONITOR_FILE\"\n fi\n}\n\ntrap '__zam_debug_trap' DEBUG\nPROMPT_COMMAND=\"__zam_prompt_cmd;\\${PROMPT_COMMAND:-}\"\n\necho \"ZAM monitor active for session $__ZAM_MONITOR_SESSION\"\n`.trim();\n}\n\n/**\n * Generate PowerShell hooks that capture completed commands to a JSONL file.\n * PowerShell has no zsh-style preexec hook, so this records the most recent\n * history item from the prompt function after each command completes.\n */\nexport function generatePowerShellHooks(\n monitorFile: string,\n sessionId: string,\n): string {\n return `\n# ZAM monitor hooks for session ${sessionId}\n$global:__ZAM_MONITOR_FILE = ${psSingleQuoted(monitorFile)}\n$global:__ZAM_MONITOR_SEQ = 0\n$global:__ZAM_MONITOR_SESSION = ${psSingleQuoted(sessionId)}\n$global:__ZAM_MONITOR_SKIP_NEXT_PROMPT = $true\n\nfunction global:__zam_write_monitor_event {\n param([hashtable]$Event)\n $json = $Event | ConvertTo-Json -Compress -Depth 4\n $utf8NoBom = New-Object System.Text.UTF8Encoding $false\n [System.IO.File]::AppendAllText($global:__ZAM_MONITOR_FILE, $json + [Environment]::NewLine, $utf8NoBom)\n}\n\nfunction global:__zam_iso_utc {\n param([datetime]$Date)\n if ($Date -eq [datetime]::MinValue) {\n return (Get-Date).ToUniversalTime().ToString(\"o\")\n }\n return $Date.ToUniversalTime().ToString(\"o\")\n}\n\nfunction global:__zam_update_last_history_id {\n $history = Get-History -Count 1\n if ($null -ne $history) {\n $global:__ZAM_MONITOR_LAST_HISTORY_ID = $history.Id\n } elseif ($null -eq $global:__ZAM_MONITOR_LAST_HISTORY_ID) {\n $global:__ZAM_MONITOR_LAST_HISTORY_ID = 0\n }\n}\n\nfunction global:__zam_record_last_history {\n param(\n [bool]$Success,\n [object]$NativeExitCode\n )\n\n $history = Get-History -Count 1\n if ($null -eq $history) { return }\n if ($history.Id -le $global:__ZAM_MONITOR_LAST_HISTORY_ID) { return }\n\n $global:__ZAM_MONITOR_LAST_HISTORY_ID = $history.Id\n $global:__ZAM_MONITOR_SEQ += 1\n\n $exitCode = 0\n if (-not $Success) {\n if ($NativeExitCode -is [int] -and $NativeExitCode -ne 0) {\n $exitCode = $NativeExitCode\n } else {\n $exitCode = 1\n }\n }\n\n $cwd = (Get-Location).Path\n __zam_write_monitor_event @{\n type = \"command_start\"\n ts = (__zam_iso_utc $history.StartExecutionTime)\n command = $history.CommandLine\n cwd = $cwd\n seq = $global:__ZAM_MONITOR_SEQ\n pid = $PID\n }\n __zam_write_monitor_event @{\n type = \"command_end\"\n ts = (__zam_iso_utc $history.EndExecutionTime)\n exit_code = $exitCode\n seq = $global:__ZAM_MONITOR_SEQ\n pid = $PID\n }\n}\n\nif (-not (Test-Path function:\\\\__zam_previous_prompt) -and (Test-Path function:\\\\prompt)) {\n Set-Item -Path function:\\\\__zam_previous_prompt -Value (Get-Item function:\\\\prompt).ScriptBlock\n}\n__zam_update_last_history_id\n\nfunction global:prompt {\n $zamSuccess = $?\n $zamNativeExitCode = $global:LASTEXITCODE\n\n if ($global:__ZAM_MONITOR_SKIP_NEXT_PROMPT) {\n __zam_update_last_history_id\n $global:__ZAM_MONITOR_SKIP_NEXT_PROMPT = $false\n } else {\n __zam_record_last_history -Success $zamSuccess -NativeExitCode $zamNativeExitCode\n }\n\n if (Test-Path function:\\\\__zam_previous_prompt) {\n & (Get-Item function:\\\\__zam_previous_prompt).ScriptBlock\n } else {\n \"PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) \"\n }\n}\n\nWrite-Host \"ZAM monitor active for session $global:__ZAM_MONITOR_SESSION\"\n`.trim();\n}\n\n/** Generate zsh code to remove monitor hooks. */\nexport function generateZshUnhooks(): string {\n return `\n# Remove ZAM monitor hooks\nadd-zsh-hook -d preexec __zam_preexec 2>/dev/null\nadd-zsh-hook -d precmd __zam_precmd 2>/dev/null\nunset -f __zam_preexec __zam_precmd __zam_ts 2>/dev/null\nunset __ZAM_MONITOR_FILE __ZAM_MONITOR_SEQ __ZAM_MONITOR_SESSION 2>/dev/null\necho \"ZAM monitor stopped.\"\n`.trim();\n}\n\n/** Generate bash code to remove monitor hooks. */\nexport function generateBashUnhooks(): string {\n return `\n# Remove ZAM monitor hooks\ntrap - DEBUG\nPROMPT_COMMAND=\"\\${PROMPT_COMMAND/__zam_prompt_cmd;/}\"\nunset -f __zam_debug_trap __zam_prompt_cmd __zam_ts 2>/dev/null\nunset __ZAM_MONITOR_FILE __ZAM_MONITOR_SEQ __ZAM_MONITOR_SESSION __ZAM_MONITOR_CMD_ACTIVE 2>/dev/null\necho \"ZAM monitor stopped.\"\n`.trim();\n}\n\n/** Generate PowerShell code to remove monitor hooks. */\nexport function generatePowerShellUnhooks(): string {\n return `\n# Remove ZAM monitor hooks\nif (Test-Path function:\\\\__zam_previous_prompt) {\n Set-Item -Path function:\\\\prompt -Value (Get-Item function:\\\\__zam_previous_prompt).ScriptBlock\n Remove-Item function:\\\\__zam_previous_prompt -Force -ErrorAction SilentlyContinue\n}\nRemove-Item function:\\\\__zam_write_monitor_event,function:\\\\__zam_iso_utc,function:\\\\__zam_update_last_history_id,function:\\\\__zam_record_last_history -ErrorAction SilentlyContinue\nRemove-Variable -Name __ZAM_MONITOR_FILE,__ZAM_MONITOR_SEQ,__ZAM_MONITOR_SESSION,__ZAM_MONITOR_LAST_HISTORY_ID,__ZAM_MONITOR_SKIP_NEXT_PROMPT -Scope Global -ErrorAction SilentlyContinue\nWrite-Host \"ZAM monitor stopped.\"\n`.trim();\n}\n","/**\n * Skill Discovery — identifies recurring non-standard command patterns\n * across multiple sessions and proposes them as minimal reusable skills.\n *\n * The key insight from Increment 2: \"The human's demonstrated competence\n * is the gate for automation — not the other way around.\" A pattern must\n * appear consistently across sessions before being proposed as a skill.\n *\n * Pure functions — no DB access. Callers provide command records and\n * existing skills; this module returns proposed skills.\n */\n\nimport type { CommandRecord } from \"./analyzer.js\";\n\n// ── Types ────────────────────────────────────────────────────────────────────\n\nexport interface CommandSequence {\n /** The ordered command prefixes forming the pattern (e.g., [\"git checkout\", \"npm install\", \"npm run build\"]) */\n steps: string[];\n /** How many sessions contained this sequence */\n sessionCount: number;\n /** Total occurrences across all sessions */\n totalOccurrences: number;\n /** Example full commands from the most recent occurrence */\n examples: string[];\n}\n\nexport interface SkillProposal {\n /** Suggested slug for the skill */\n slug: string;\n /** Human-readable description of what the pattern does */\n description: string;\n /** The command steps forming the skill */\n steps: string[];\n /** How many sessions demonstrated this pattern */\n sessionCount: number;\n /** Confidence that this is a real, repeatable skill */\n confidence: \"high\" | \"medium\" | \"low\";\n /** Example commands from actual usage */\n examples: string[];\n}\n\nexport interface DiscoveryOptions {\n /** Minimum number of sessions a pattern must appear in (default: 2) */\n minSessions?: number;\n /** Minimum sequence length to consider (default: 2) */\n minSequenceLength?: number;\n /** Maximum sequence length to consider (default: 5) */\n maxSequenceLength?: number;\n /** Existing skill slugs to exclude from proposals */\n existingSkillSlugs?: string[];\n}\n\n// ── Discovery ────────────────────────────────────────────────────────────────\n\n/**\n * Discover recurring command patterns across multiple sessions.\n *\n * Takes a map of session ID → command records, finds command sequences\n * that appear in multiple sessions, and proposes them as skills.\n *\n * @param sessionCommands - Map of session ID to that session's commands\n * @param options - Discovery configuration\n * @returns Array of skill proposals, sorted by confidence then session count\n */\nexport function discoverSkills(\n sessionCommands: Map<string, CommandRecord[]>,\n options: DiscoveryOptions = {},\n): SkillProposal[] {\n const minSessions = options.minSessions ?? 2;\n const minLen = options.minSequenceLength ?? 2;\n const maxLen = options.maxSequenceLength ?? 5;\n const existing = new Set(options.existingSkillSlugs ?? []);\n\n // Step 1: Extract normalized command sequences from each session\n const sessionSequences = new Map<string, string[][]>();\n for (const [sessionId, commands] of sessionCommands) {\n const sequences = extractSequences(commands, minLen, maxLen);\n if (sequences.length > 0) {\n sessionSequences.set(sessionId, sequences);\n }\n }\n\n // Step 2: Count how many sessions contain each unique sequence\n const sequenceIndex = new Map<string, CommandSequence>();\n\n for (const [, sequences] of sessionSequences) {\n // Deduplicate within a session — count each sequence once per session\n const seen = new Set<string>();\n for (const seq of sequences) {\n const key = seq.join(\" → \");\n if (seen.has(key)) continue;\n seen.add(key);\n\n const entry = sequenceIndex.get(key);\n if (entry) {\n entry.sessionCount++;\n entry.totalOccurrences++;\n } else {\n sequenceIndex.set(key, {\n steps: seq,\n sessionCount: 1,\n totalOccurrences: 1,\n examples: [],\n });\n }\n }\n }\n\n // Step 3: Collect examples from the most recent session for qualifying sequences\n const lastSessionId = [...sessionCommands.keys()].pop();\n if (lastSessionId) {\n const lastCommands = sessionCommands.get(lastSessionId)!;\n for (const [_key, entry] of sequenceIndex) {\n if (entry.sessionCount >= minSessions) {\n entry.examples = findExamplesForSequence(lastCommands, entry.steps);\n }\n }\n }\n\n // Step 4: Filter to patterns that appear in enough sessions\n const candidates = [...sequenceIndex.values()].filter(\n (s) => s.sessionCount >= minSessions,\n );\n\n // Step 5: Remove subsequences of longer patterns (prefer maximal patterns)\n const pruned = removeSubsequences(candidates);\n\n // Step 6: Convert to skill proposals\n const proposals: SkillProposal[] = [];\n for (const seq of pruned) {\n const slug = generateSlug(seq.steps);\n\n // Skip if this skill already exists\n if (existing.has(slug)) continue;\n\n proposals.push({\n slug,\n description: describeSequence(seq.steps),\n steps: seq.steps,\n sessionCount: seq.sessionCount,\n confidence:\n seq.sessionCount >= 4\n ? \"high\"\n : seq.sessionCount >= 3\n ? \"medium\"\n : \"low\",\n examples: seq.examples,\n });\n }\n\n // Sort: high confidence first, then by session count\n const confidenceOrder = { high: 0, medium: 1, low: 2 };\n proposals.sort((a, b) => {\n const confDiff =\n confidenceOrder[a.confidence] - confidenceOrder[b.confidence];\n if (confDiff !== 0) return confDiff;\n return b.sessionCount - a.sessionCount;\n });\n\n return proposals;\n}\n\n// ── Helpers ──────────────────────────────────────────────────────────────────\n\n/**\n * Normalize a command to its tool + subcommand prefix.\n * \"git checkout -b feat/foo\" → \"git checkout\"\n * \"npm run build\" → \"npm run build\"\n * \"docker compose up -d\" → \"docker compose up\"\n */\nfunction normalizeCommand(command: string): string {\n const parts = command.trim().split(/\\s+/);\n\n // Known multi-word tools\n const multiWord = [\"docker compose\", \"npm run\", \"npx\", \"git\"];\n const lower = command.toLowerCase();\n\n for (const mw of multiWord) {\n if (lower.startsWith(mw) && parts.length >= mw.split(\" \").length + 1) {\n return parts\n .slice(0, mw.split(\" \").length + 1)\n .join(\" \")\n .toLowerCase();\n }\n }\n\n // Default: first two words\n return parts.slice(0, Math.min(2, parts.length)).join(\" \").toLowerCase();\n}\n\n/**\n * Extract all command subsequences of length minLen..maxLen from a session.\n * Uses normalized command prefixes for comparison.\n */\nfunction extractSequences(\n commands: CommandRecord[],\n minLen: number,\n maxLen: number,\n): string[][] {\n // Filter out trivial commands\n const filtered = commands.filter((c) => {\n const lower = c.command.toLowerCase().trim();\n return (\n lower.length > 0 &&\n !lower.startsWith(\"cd \") &&\n lower !== \"cd\" &&\n lower !== \"ls\" &&\n lower !== \"pwd\" &&\n lower !== \"clear\" &&\n lower !== \"exit\" &&\n !lower.startsWith(\"echo \")\n );\n });\n\n const normalized = filtered.map((c) => normalizeCommand(c.command));\n const sequences: string[][] = [];\n\n for (let len = minLen; len <= maxLen; len++) {\n for (let i = 0; i <= normalized.length - len; i++) {\n const seq = normalized.slice(i, i + len);\n // Only include if at least 2 distinct commands (not just \"git commit\" repeated)\n if (new Set(seq).size >= 2) {\n sequences.push(seq);\n }\n }\n }\n\n return sequences;\n}\n\n/**\n * Find example full commands for a given normalized sequence in a command list.\n */\nfunction findExamplesForSequence(\n commands: CommandRecord[],\n steps: string[],\n): string[] {\n const normalized = commands.map((c) => ({\n norm: normalizeCommand(c.command),\n full: c.command,\n }));\n\n for (let i = 0; i <= normalized.length - steps.length; i++) {\n let match = true;\n for (let j = 0; j < steps.length; j++) {\n if (normalized[i + j].norm !== steps[j]) {\n match = false;\n break;\n }\n }\n if (match) {\n return normalized.slice(i, i + steps.length).map((n) => n.full);\n }\n }\n\n return [];\n}\n\n/**\n * Remove sequences that are strict subsequences of longer qualifying sequences.\n */\nfunction removeSubsequences(candidates: CommandSequence[]): CommandSequence[] {\n // Sort by length descending so we check long sequences first\n const sorted = [...candidates].sort(\n (a, b) => b.steps.length - a.steps.length,\n );\n const result: CommandSequence[] = [];\n\n for (const candidate of sorted) {\n const key = candidate.steps.join(\" → \");\n const isSubsequence = result.some((longer) => {\n const longerKey = longer.steps.join(\" → \");\n return longerKey.includes(key) && longerKey !== key;\n });\n\n if (!isSubsequence) {\n result.push(candidate);\n }\n }\n\n return result;\n}\n\n/**\n * Generate a slug from command steps.\n * [\"git checkout\", \"npm install\", \"npm run build\"] → \"checkout-install-build\"\n */\nfunction generateSlug(steps: string[]): string {\n return steps\n .map((s) => {\n const parts = s.split(/\\s+/);\n return parts[parts.length - 1]; // last word of each step\n })\n .join(\"-\");\n}\n\n/**\n * Generate a human-readable description of what a command sequence does.\n */\nfunction describeSequence(steps: string[]): string {\n return `Recurring pattern: ${steps.join(\" → \")}`;\n}\n","/**\n * Cascade Block & Unblock — prerequisite-aware blocking logic.\n *\n * Ported from the PoC's cascade-block and unblock-ready commands.\n *\n * When a user rates a token as \"forgot\" (rating 1) and that token has\n * prerequisites, we block the token and surface its prerequisites into\n * the active deck. When all prerequisites are met, we unblock.\n */\n\nimport type { Database } from \"libsql\";\nimport { ensureCard } from \"../models/card.js\";\nimport { getPrerequisites } from \"../models/prerequisite.js\";\nimport { getTokenBySlug } from \"../models/token.js\";\n\n// ── Types ────────────────────────────────────────────────────────────────────\n\nexport interface CascadeBlockResult {\n blockedSlug: string;\n prerequisites: Array<{ slug: string; concept: string; bloomLevel: number }>;\n}\n\nexport interface UnblockResult {\n unblocked: Array<{ slug: string; concept: string }>;\n}\n\n// ── Functions ────────────────────────────────────────────────────────────────\n\n/**\n * Block a token and surface its prerequisites.\n *\n * Called when a user rates a token as \"forgot\" (rating 1). The token is\n * marked as blocked so it won't appear in review queues. All direct\n * prerequisites are ensured to have cards (unblocked, due now) so they\n * appear in the user's next review session.\n *\n * @param db - Database connection\n * @param userId - The user whose card to block\n * @param tokenSlug - Slug of the token the user forgot\n * @returns Info about what was blocked and which prerequisites were surfaced\n */\nexport function cascadeBlock(\n db: Database,\n userId: string,\n tokenSlug: string,\n): CascadeBlockResult {\n const token = getTokenBySlug(db, tokenSlug);\n if (!token) {\n throw new Error(`Unknown token slug: ${tokenSlug}`);\n }\n\n // Ensure a card exists, then block it\n ensureCard(db, token.id, userId);\n db.prepare(\n \"UPDATE cards SET blocked = 1 WHERE token_id = ? AND user_id = ?\",\n ).run(token.id, userId);\n\n // Surface all direct prerequisites — ensure cards exist (unblocked, due now)\n const prereqs = getPrerequisites(db, token.id);\n const surfaced: Array<{ slug: string; concept: string; bloomLevel: number }> =\n [];\n\n for (const prereq of prereqs) {\n // ensureCard creates a new card if missing (defaults: blocked=0, due_at=now)\n const card = ensureCard(db, prereq.requires_id, userId);\n\n // If the prerequisite card was somehow blocked with no prereqs of its own,\n // make sure it's unblocked and due now so it surfaces\n if (card.blocked === 1) {\n const prereqOfPrereq = db\n .prepare(\"SELECT COUNT(*) as n FROM prerequisites WHERE token_id = ?\")\n .get(prereq.requires_id) as { n: number };\n\n // Only force-unblock if it has no prerequisites of its own\n if (prereqOfPrereq.n === 0) {\n const now = new Date().toISOString();\n db.prepare(\n \"UPDATE cards SET blocked = 0, due_at = ? WHERE token_id = ? AND user_id = ?\",\n ).run(now, prereq.requires_id, userId);\n }\n }\n\n surfaced.push({\n slug: prereq.slug,\n concept: prereq.concept,\n bloomLevel: prereq.bloom_level,\n });\n }\n\n return {\n blockedSlug: tokenSlug,\n prerequisites: surfaced,\n };\n}\n\n/**\n * Scan all blocked cards for a user and unblock any whose prerequisites are met.\n *\n * A blocked card is ready to unblock when ALL of its direct prerequisites have:\n * - reps >= 1 (the user has successfully recalled it at least once)\n * - blocked = 0 (the prerequisite itself is not blocked)\n *\n * If a blocked card has no prerequisites at all, it is unblocked immediately\n * (it was likely blocked in error or its prerequisites were removed).\n *\n * @param db - Database connection\n * @param userId - The user whose blocked cards to check\n * @returns List of cards that were unblocked\n */\nexport function unblockReady(db: Database, userId: string): UnblockResult {\n const blockedCards = db\n .prepare(\n `SELECT c.token_id, t.slug, t.concept\n FROM cards c\n JOIN tokens t ON t.id = c.token_id\n WHERE c.user_id = ? AND c.blocked = 1`,\n )\n .all(userId) as Array<{ token_id: string; slug: string; concept: string }>;\n\n const unblocked: Array<{ slug: string; concept: string }> = [];\n\n for (const card of blockedCards) {\n const totalPrereqs = db\n .prepare(\"SELECT COUNT(*) as n FROM prerequisites WHERE token_id = ?\")\n .get(card.token_id) as { n: number };\n\n const metPrereqs = db\n .prepare(\n `SELECT COUNT(*) as n FROM cards c\n JOIN prerequisites p ON p.requires_id = c.token_id\n WHERE p.token_id = ? AND c.user_id = ? AND c.reps >= 1 AND c.blocked = 0`,\n )\n .get(card.token_id, userId) as { n: number };\n\n if (totalPrereqs.n === 0 || metPrereqs.n === totalPrereqs.n) {\n const now = new Date().toISOString();\n db.prepare(\n \"UPDATE cards SET blocked = 0, due_at = ? WHERE token_id = ? AND user_id = ?\",\n ).run(now, card.token_id, userId);\n\n unblocked.push({ slug: card.slug, concept: card.concept });\n }\n }\n\n return { unblocked };\n}\n","/**\n * Rating Evaluator\n *\n * Processes a user's self-assessment rating after a recall attempt.\n * Coordinates between FSRS scheduling, review logging, and blocking.\n */\n\nimport type { Database } from \"libsql\";\nimport { ulid } from \"ulid\";\nimport { updateCard } from \"../models/card.js\";\nimport type { Rating, SchedulingCard } from \"../scheduler/fsrs.js\";\nimport { createFSRS } from \"../scheduler/fsrs.js\";\n\nexport interface EvaluateInput {\n cardId: string;\n tokenId: string;\n userId: string;\n rating: Rating;\n sessionId?: string;\n responseTimeMs?: number;\n}\n\nexport interface EvaluateResult {\n nextDueAt: string;\n stability: number;\n difficulty: number;\n state: string;\n scheduledDays: number;\n reps: number;\n lapses: number;\n}\n\n/**\n * Process a rating: update the card via FSRS, log the review.\n * Returns the updated scheduling state.\n *\n * Note: blocking logic (cascade-block) is handled separately by the caller\n * when rating === 1 and the token has prerequisites.\n */\nexport function evaluateRating(\n db: Database,\n input: EvaluateInput,\n): EvaluateResult {\n // Get current card state\n const card = db\n .prepare(\"SELECT * FROM cards WHERE id = ?\")\n .get(input.cardId) as\n | {\n stability: number;\n difficulty: number;\n elapsed_days: number;\n scheduled_days: number;\n reps: number;\n lapses: number;\n state: string;\n due_at: string;\n last_review_at: string | null;\n }\n | undefined;\n\n if (!card) {\n throw new Error(`Card not found: ${input.cardId}`);\n }\n\n const now = new Date();\n const fsrs = createFSRS();\n\n // Build scheduling card from DB state\n const schedulingCard: SchedulingCard = {\n stability: card.stability,\n difficulty: card.difficulty,\n elapsedDays: card.elapsed_days,\n scheduledDays: card.scheduled_days,\n reps: card.reps,\n lapses: card.lapses,\n state: card.state as SchedulingCard[\"state\"],\n dueAt: new Date(card.due_at),\n lastReviewAt: card.last_review_at ? new Date(card.last_review_at) : null,\n };\n\n // Run FSRS\n const updated = fsrs.schedule(schedulingCard, input.rating, now);\n\n // Update the card in the DB\n updateCard(db, input.cardId, {\n stability: updated.stability,\n difficulty: updated.difficulty,\n elapsed_days: updated.elapsedDays,\n scheduled_days: updated.scheduledDays,\n reps: updated.reps,\n lapses: updated.lapses,\n state: updated.state,\n due_at: updated.dueAt.toISOString(),\n last_review_at: now.toISOString(),\n });\n\n // Log the review (immutable)\n db.prepare(\n `INSERT INTO review_logs (id, card_id, token_id, user_id, rating, response_time_ms, reviewed_at, scheduled_at, session_id)\n VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,\n ).run(\n ulid(),\n input.cardId,\n input.tokenId,\n input.userId,\n input.rating,\n input.responseTimeMs ?? null,\n now.toISOString(),\n card.due_at,\n input.sessionId ?? null,\n );\n\n return {\n nextDueAt: updated.dueAt.toISOString(),\n stability: updated.stability,\n difficulty: updated.difficulty,\n state: updated.state,\n scheduledDays: updated.scheduledDays,\n reps: updated.reps,\n lapses: updated.lapses,\n };\n}\n","/**\n * FSRS-5 — Free Spaced Repetition Scheduler (v5)\n *\n * Pure-function implementation of the FSRS algorithm that replaces\n * the PoC's SM-2 scheduler. This is the mathematical heart of ZAM's\n * spaced-repetition engine.\n *\n * Reference: https://github.com/open-spaced-repetition/fsrs4anki/wiki/The-Algorithm\n */\n\n// ── Rating scale ────────────────────────────────────────────────────────────\n\n/** 1 = Again (forgot), 2 = Hard, 3 = Good, 4 = Easy */\nexport type Rating = 1 | 2 | 3 | 4;\n\n// ── Card states ─────────────────────────────────────────────────────────────\n\nexport type CardState = \"new\" | \"learning\" | \"review\" | \"relearning\";\n\n// ── Scheduling card ─────────────────────────────────────────────────────────\n\nexport interface SchedulingCard {\n /** Memory stability in days — expected half-life of recall probability. */\n stability: number;\n /** Intrinsic difficulty on a 1–10 scale. */\n difficulty: number;\n /** Days elapsed since the last review. */\n elapsedDays: number;\n /** Currently scheduled interval in days. */\n scheduledDays: number;\n /** Count of successful consecutive reviews. */\n reps: number;\n /** Times the card was forgotten (rated Again). */\n lapses: number;\n /** Current learning state. */\n state: CardState;\n /** When the card is next due. */\n dueAt: Date;\n /** When the card was last reviewed (null for new cards). */\n lastReviewAt: Date | null;\n}\n\n// ── Parameters ──────────────────────────────────────────────────────────────\n\nexport interface FSRSParameters {\n /** 19 optimised weight parameters (w0 – w18). */\n w: number[];\n /** Target retention rate, e.g. 0.9 means we aim for 90% recall. */\n requestRetention: number;\n}\n\n// ── FSRS-5 default weights ──────────────────────────────────────────────────\n\nconst DEFAULT_W: number[] = [\n 0.4072,\n 1.1829,\n 3.1262,\n 15.4722, // w0–w3: initial stability per rating\n 7.2102,\n 0.5316,\n 1.0651, // w4–w6: difficulty\n 0.0092,\n 1.5988,\n 0.1176,\n 1.0014, // w7–w10: stability after forgetting\n 2.0032,\n 0.0266,\n 0.3077,\n 0.15, // w11–w14: stability increase\n 0.0,\n 2.7849,\n 0.3477,\n 0.6831, // w15–w18: additional parameters\n];\n\nconst DEFAULT_REQUEST_RETENTION = 0.9;\n\n// ── FSRS object returned by the factory ─────────────────────────────────────\n\nexport interface FSRS {\n /** Return a fully updated card after applying a rating. Pure function. */\n schedule(card: SchedulingCard, rating: Rating, now?: Date): SchedulingCard;\n /** The parameters baked into this instance. */\n readonly params: Readonly<FSRSParameters>;\n}\n\n// ── Helpers ─────────────────────────────────────────────────────────────────\n\n/** Clamp a number to [lo, hi]. */\nfunction clamp(value: number, lo: number, hi: number): number {\n return Math.min(hi, Math.max(lo, value));\n}\n\n/** Days between two Date objects (may be fractional). */\nfunction daysBetween(a: Date, b: Date): number {\n return (b.getTime() - a.getTime()) / (1000 * 60 * 60 * 24);\n}\n\n// ── Core FSRS-5 formulas ────────────────────────────────────────────────────\n\n/**\n * Initial stability for a brand-new card.\n * S₀ = w[rating - 1]\n */\nfunction initialStability(w: number[], rating: Rating): number {\n return w[rating - 1];\n}\n\n/**\n * Initial difficulty for a brand-new card.\n * D₀(rating) = w4 - exp(w5 * (rating - 1)) + 1\n * Clamped to [1, 10].\n */\nfunction initialDifficulty(w: number[], rating: Rating): number {\n return clamp(w[4] - Math.exp(w[5] * (rating - 1)) + 1, 1, 10);\n}\n\n/**\n * Updated difficulty after a review.\n * D' = w7 * D₀(3) + (1 - w7) * (D - w6 * (rating - 3))\n * Clamped to [1, 10].\n */\nfunction nextDifficulty(w: number[], d: number, rating: Rating): number {\n const d0ForGood = initialDifficulty(w, 3);\n const updated = w[7] * d0ForGood + (1 - w[7]) * (d - w[6] * (rating - 3));\n return clamp(updated, 1, 10);\n}\n\n/**\n * Retrievability — probability of recall.\n * R = (1 + elapsed / (9 * S))^(-1)\n */\nfunction retrievability(elapsed: number, stability: number): number {\n if (stability <= 0) return 0;\n return (1 + elapsed / (9 * stability)) ** -1;\n}\n\n/**\n * Stability after a **successful** recall (rating >= 2).\n * S' = S * (exp(w8) * (11 - D) * S^(-w9) * (exp(w10 * (1 - R)) - 1) * hard_penalty * easy_bonus + 1)\n */\nfunction stabilityAfterSuccess(\n w: number[],\n s: number,\n d: number,\n r: number,\n rating: Rating,\n): number {\n const hardPenalty = rating === 2 ? w[15] : 1;\n const easyBonus = rating === 4 ? w[16] : 1;\n\n const inner =\n Math.exp(w[8]) *\n (11 - d) *\n s ** -w[9] *\n (Math.exp(w[10] * (1 - r)) - 1) *\n hardPenalty *\n easyBonus;\n\n return s * (inner + 1);\n}\n\n/**\n * Stability after **forgetting** (rating = 1).\n * S' = w11 * D^(-w12) * ((S+1)^w13 - 1) * exp(w14 * (1 - R))\n */\nfunction stabilityAfterForgetting(\n w: number[],\n s: number,\n d: number,\n r: number,\n): number {\n return (\n w[11] * d ** -w[12] * ((s + 1) ** w[13] - 1) * Math.exp(w[14] * (1 - r))\n );\n}\n\n/**\n * Optimal interval given new stability and target retention.\n * I = 9 * S' * (1/requestRetention - 1)\n * Minimum 1 day.\n */\nfunction nextInterval(stability: number, requestRetention: number): number {\n const interval = 9 * stability * (1 / requestRetention - 1);\n return Math.max(1, Math.round(interval));\n}\n\n// ── Factory ─────────────────────────────────────────────────────────────────\n\n/**\n * Create a new card with default initial values.\n */\nexport function createEmptyCard(now?: Date): SchedulingCard {\n const ts = now ?? new Date();\n return {\n stability: 0,\n difficulty: 0,\n elapsedDays: 0,\n scheduledDays: 0,\n reps: 0,\n lapses: 0,\n state: \"new\",\n dueAt: ts,\n lastReviewAt: null,\n };\n}\n\n/**\n * Create an FSRS scheduler instance.\n *\n * All scheduling is done through pure functions — no side effects,\n * no database access, no mutation of the input card.\n */\nexport function createFSRS(params?: Partial<FSRSParameters>): FSRS {\n const resolvedParams: FSRSParameters = {\n w: params?.w ?? [...DEFAULT_W],\n requestRetention: params?.requestRetention ?? DEFAULT_REQUEST_RETENTION,\n };\n\n function schedule(\n card: SchedulingCard,\n rating: Rating,\n now?: Date,\n ): SchedulingCard {\n const reviewTime = now ?? new Date();\n const w = resolvedParams.w;\n\n // Compute elapsed days since last review (or 0 for new cards).\n const elapsed =\n card.lastReviewAt !== null\n ? Math.max(0, daysBetween(card.lastReviewAt, reviewTime))\n : 0;\n\n // ── New card ──────────────────────────────────────────────────────\n if (card.state === \"new\") {\n const s = initialStability(w, rating);\n const d = initialDifficulty(w, rating);\n const interval = nextInterval(s, resolvedParams.requestRetention);\n\n const dueAt = new Date(reviewTime);\n dueAt.setDate(dueAt.getDate() + interval);\n\n // New cards always move to \"learning\" after first rating.\n\n return {\n stability: s,\n difficulty: d,\n elapsedDays: 0,\n scheduledDays: interval,\n reps: rating >= 2 ? 1 : 0,\n lapses: rating === 1 ? 1 : 0,\n state: \"learning\",\n dueAt,\n lastReviewAt: reviewTime,\n };\n }\n\n // ── Existing card (learning / review / relearning) ───────────────\n\n const r = retrievability(elapsed, card.stability);\n\n let newStability: number;\n let newDifficulty: number;\n let newReps: number;\n let newLapses: number;\n let newState: CardState;\n\n if (rating === 1) {\n // Forgot — apply forgetting formula\n newStability = stabilityAfterForgetting(\n w,\n card.stability,\n card.difficulty,\n r,\n );\n newDifficulty = nextDifficulty(w, card.difficulty, rating);\n newReps = 0;\n newLapses = card.lapses + 1;\n newState = \"relearning\";\n } else {\n // Recalled — apply success formula\n newStability = stabilityAfterSuccess(\n w,\n card.stability,\n card.difficulty,\n r,\n rating,\n );\n newDifficulty = nextDifficulty(w, card.difficulty, rating);\n newReps = card.reps + 1;\n newLapses = card.lapses;\n // Successful recall transitions any state to review.\n newState = \"review\";\n }\n\n const interval = nextInterval(\n newStability,\n resolvedParams.requestRetention,\n );\n\n const dueAt = new Date(reviewTime);\n dueAt.setDate(dueAt.getDate() + interval);\n\n return {\n stability: newStability,\n difficulty: newDifficulty,\n elapsedDays: elapsed,\n scheduledDays: interval,\n reps: newReps,\n lapses: newLapses,\n state: newState,\n dueAt,\n lastReviewAt: reviewTime,\n };\n }\n\n return {\n schedule,\n params: Object.freeze(resolvedParams),\n };\n}\n","import type { Database } from \"libsql\";\nimport type { DeleteCardResult } from \"../models/card.js\";\nimport { deleteCardForUser, getCardById } from \"../models/card.js\";\nimport { getPrerequisites } from \"../models/prerequisite.js\";\nimport type {\n DeleteTokenResult,\n Token,\n UpdateTokenInput,\n} from \"../models/token.js\";\nimport {\n deleteToken,\n deprecateToken,\n getTokenById,\n updateToken,\n} from \"../models/token.js\";\nimport type { CascadeBlockResult } from \"../scheduler/blocker.js\";\nimport { cascadeBlock } from \"../scheduler/blocker.js\";\nimport type { Rating } from \"../scheduler/fsrs.js\";\nimport type { EvaluateResult } from \"./evaluator.js\";\nimport { evaluateRating } from \"./evaluator.js\";\n\nexport type ReviewActionType =\n | \"rate\"\n | \"skip\"\n | \"edit-token\"\n | \"deprecate-token\"\n | \"delete-token\"\n | \"delete-card\"\n | \"stop\";\n\nexport interface ExecuteReviewActionInput {\n cardId: string;\n userId: string;\n action: ReviewActionType;\n rating?: Rating;\n tokenUpdates?: UpdateTokenInput;\n}\n\nexport interface ReviewActionResult {\n action: ReviewActionType;\n token: Token;\n evaluation?: EvaluateResult;\n blocked?: CascadeBlockResult;\n updatedToken?: Token;\n deletedToken?: DeleteTokenResult;\n deletedCard?: DeleteCardResult;\n skipped?: boolean;\n stopped?: boolean;\n}\n\nfunction getReviewTarget(\n db: Database,\n cardId: string,\n userId: string,\n): { cardId: string; token: Token } {\n const card = getCardById(db, cardId);\n if (!card) {\n throw new Error(`Card not found: ${cardId}`);\n }\n if (card.user_id !== userId) {\n throw new Error(`Card ${cardId} does not belong to user ${userId}`);\n }\n\n const token = getTokenById(db, card.token_id);\n if (!token) {\n throw new Error(`Token not found for card ${cardId}`);\n }\n\n return { cardId: card.id, token };\n}\n\nexport function executeReviewAction(\n db: Database,\n input: ExecuteReviewActionInput,\n): ReviewActionResult {\n const target = getReviewTarget(db, input.cardId, input.userId);\n\n switch (input.action) {\n case \"rate\": {\n if (input.rating == null) {\n throw new Error(\"rating is required for action=rate\");\n }\n\n const evaluation = evaluateRating(db, {\n cardId: target.cardId,\n tokenId: target.token.id,\n userId: input.userId,\n rating: input.rating,\n });\n\n let blocked: CascadeBlockResult | undefined;\n if (input.rating === 1) {\n const prereqs = getPrerequisites(db, target.token.id);\n if (prereqs.length > 0) {\n blocked = cascadeBlock(db, input.userId, target.token.slug);\n }\n }\n\n return {\n action: input.action,\n token: target.token,\n evaluation,\n blocked,\n };\n }\n\n case \"skip\":\n return { action: input.action, token: target.token, skipped: true };\n\n case \"stop\":\n return { action: input.action, token: target.token, stopped: true };\n\n case \"edit-token\": {\n const updatedToken = updateToken(\n db,\n target.token.slug,\n input.tokenUpdates ?? {},\n );\n return {\n action: input.action,\n token: target.token,\n updatedToken,\n };\n }\n\n case \"deprecate-token\": {\n const updatedToken = deprecateToken(db, target.token.slug);\n return {\n action: input.action,\n token: target.token,\n updatedToken,\n };\n }\n\n case \"delete-token\": {\n const deletedToken = deleteToken(db, target.token.slug);\n return {\n action: input.action,\n token: target.token,\n deletedToken,\n };\n }\n\n case \"delete-card\": {\n const deletedCard = deleteCardForUser(db, target.token.id, input.userId);\n return {\n action: input.action,\n token: target.token,\n deletedCard,\n };\n }\n\n default: {\n const exhaustive: never = input.action;\n throw new Error(`Unsupported review action: ${exhaustive}`);\n }\n }\n}\n","/**\n * Active Recall Prompt Generation\n *\n * Generates review prompts from tokens, adapting the question style\n * to the token's Bloom taxonomy level. This is NOT an LLM call —\n * it's template-based prompt assembly for the CLI and bridge.\n */\n\nexport type BloomLevel = 1 | 2 | 3 | 4 | 5;\n\nexport interface RecallPrompt {\n cardId: string;\n tokenId: string;\n slug: string;\n question: string;\n concept: string;\n domain: string;\n bloomLevel: BloomLevel;\n bloomVerb: string;\n hints: string[];\n sourceLink?: string | null;\n}\n\nconst BLOOM_VERBS: Record<BloomLevel, string> = {\n 1: \"Remember\",\n 2: \"Understand\",\n 3: \"Apply\",\n 4: \"Analyze\",\n 5: \"Synthesize\",\n};\n\nfunction formatSlugForCue(slug: string): string {\n return slug.replace(/[-_]/g, \" \");\n}\n\nconst BLOOM_CUES: Record<BloomLevel, (slug: string) => string> = {\n 1: (slug) =>\n `Recall the definition and core concept of: ${formatSlugForCue(slug)}`,\n 2: (slug) => `Explain the concept and how ${formatSlugForCue(slug)} works.`,\n 3: (slug) =>\n `Describe how or where you would apply the concept of ${formatSlugForCue(slug)}.`,\n 4: (slug) =>\n `Analyze the trade-offs, advantages, or alternatives of ${formatSlugForCue(slug)}.`,\n 5: (slug) =>\n `How would you design a solution using the concept of ${formatSlugForCue(slug)}?`,\n};\n\nexport interface PromptInput {\n cardId: string;\n tokenId: string;\n slug: string;\n concept: string;\n domain: string;\n bloomLevel: BloomLevel;\n sourceLink?: string | null;\n question?: string | null;\n}\n\n/**\n * Generate a template-based concept-free recall cue using the slug and domain.\n */\nexport function generateConceptFreeCue(\n bloomLevel: BloomLevel,\n slug: string,\n _domain: string,\n): string {\n const bloom = (\n bloomLevel >= 1 && bloomLevel <= 5 ? bloomLevel : 1\n ) as BloomLevel;\n return BLOOM_CUES[bloom](slug);\n}\n\n/**\n * Generate a recall prompt for a token at its Bloom level.\n * When called from the CLI, the prompt is rendered in the terminal.\n * When called from the AI bridge, the JSON is returned for the AI to present conversationally.\n */\nexport function generatePrompt(input: PromptInput): RecallPrompt {\n const bloom = (\n input.bloomLevel >= 1 && input.bloomLevel <= 5 ? input.bloomLevel : 1\n ) as BloomLevel;\n\n const question = input.question?.trim()\n ? input.question.trim()\n : generateConceptFreeCue(bloom, input.slug, input.domain);\n\n return {\n cardId: input.cardId,\n tokenId: input.tokenId,\n slug: input.slug,\n question,\n concept: input.concept,\n domain: input.domain,\n bloomLevel: bloom,\n bloomVerb: BLOOM_VERBS[bloom],\n hints: [],\n sourceLink: input.sourceLink ?? null,\n };\n}\n","import { existsSync, readFileSync } from \"node:fs\";\nimport { dirname, join, resolve } from \"node:path\";\n\nexport interface ResolvedReference {\n sourceType: \"local\" | \"remote_web\" | \"dynamic_search\";\n content: string;\n filePath?: string;\n url?: string;\n}\n\n/**\n * A source reference resolved and bounded for inclusion in a review payload.\n * Same shape as ResolvedReference plus the originating link and a truncation flag.\n */\nexport interface ReviewContext {\n sourceLink: string;\n sourceType: ResolvedReference[\"sourceType\"];\n content: string;\n filePath?: string;\n url?: string;\n truncated: boolean;\n}\n\n/** Default cap on resolved content length, so bridge JSON / terminal output stays bounded. */\nexport const DEFAULT_REVIEW_CONTEXT_MAX_CHARS = 6000;\n\n/**\n * Strips HTML tags and attempts to convert basic structure to readable text/markdown.\n */\nfunction htmlToText(html: string): string {\n // Extract body if present\n let content = html;\n const bodyMatch = /<body[^>]*>([\\s\\S]*?)<\\/body>/i.exec(html);\n if (bodyMatch) {\n content = bodyMatch[1];\n }\n\n // Strip script, style, and head tags completely\n content = content.replace(/<(script|style|head)[^>]*>([\\s\\S]*?)<\\/\\1>/gi, \"\");\n // Replace headings\n content = content.replace(\n /<h[1-6][^>]*>([\\s\\S]*?)<\\/h[1-6]>/gi,\n \"\\n\\n# $1\\n\",\n );\n // Replace paragraph/div/li tags with line breaks\n content = content.replace(/<(p|div|li)[^>]*>/gi, \"\\n\");\n content = content.replace(/<\\/(p|div|li)>/gi, \"\\n\");\n content = content.replace(/<br\\s*\\/?>/gi, \"\\n\");\n // Strip all other HTML tags\n content = content.replace(/<[^>]+>/g, \"\");\n // Decode basic HTML entities\n content = content\n .replace(/ /g, \" \")\n .replace(/</g, \"<\")\n .replace(/>/g, \">\")\n .replace(/&/g, \"&\")\n .replace(/"/g, '\"')\n .replace(/'/g, \"'\");\n\n // Collapse consecutive newlines\n content = content.replace(/\\n{3,}/g, \"\\n\\n\").trim();\n return content;\n}\n\n/**\n * Parse line anchors like #L10-L25 or #L10 and extract the lines from file content.\n */\nfunction extractLines(content: string, anchor: string): string {\n const lines = content.split(/\\r?\\n/);\n const match = /#L(\\d+)(?:-L(\\d+))?$/i.exec(anchor);\n if (!match) return content;\n\n const start = Number.parseInt(match[1], 10) - 1; // 0-indexed\n const end = match[2] ? Number.parseInt(match[2], 10) - 1 : start;\n\n if (start < 0 || start >= lines.length) return content;\n\n const slice = lines.slice(start, Math.min(end + 1, lines.length));\n return slice.join(\"\\n\");\n}\n\n/**\n * Resolves a given token's source_link into readable textual content.\n */\nexport async function resolveReference(\n sourceLink: string,\n): Promise<ResolvedReference> {\n const cleaned = sourceLink.trim();\n\n // 1. Dynamic Web Search\n if (cleaned.startsWith(\"search://\")) {\n try {\n const url = new URL(cleaned);\n const query = url.searchParams.get(\"q\") || \"\";\n return {\n sourceType: \"dynamic_search\",\n content: `QUERY_DIRECTIVE: Run web search for \"${query}\"`,\n url: cleaned,\n };\n } catch {\n // Fallback if URL parsing fails\n const query = cleaned.replace(/^search:\\/\\/(\\??q=)?/, \"\");\n return {\n sourceType: \"dynamic_search\",\n content: `QUERY_DIRECTIVE: Run web search for \"${decodeURIComponent(query)}\"`,\n url: cleaned,\n };\n }\n }\n\n // 2. HTTP/HTTPS URLs\n if (cleaned.startsWith(\"http://\") || cleaned.startsWith(\"https://\")) {\n // 2.a GitHub URIs\n const gitHubMatch =\n /^https?:\\/\\/(?:www\\.)?github\\.com\\/([^/]+)\\/([^/]+)\\/blob\\/([^/]+)\\/(.+)$/i.exec(\n cleaned,\n );\n if (gitHubMatch) {\n const [_, owner, repo, branch, fullPathWithAnchor] = gitHubMatch;\n const anchorIndex = fullPathWithAnchor.indexOf(\"#\");\n const filePath =\n anchorIndex !== -1\n ? fullPathWithAnchor.slice(0, anchorIndex)\n : fullPathWithAnchor;\n const anchor =\n anchorIndex !== -1 ? fullPathWithAnchor.slice(anchorIndex) : \"\";\n\n // Try local resolution: check if repo folder exists in sibling directories\n const parentDir = dirname(process.cwd());\n const localRepoPath = join(parentDir, repo);\n const localFilePath = join(localRepoPath, filePath);\n\n if (existsSync(localFilePath)) {\n try {\n let fileContent = readFileSync(localFilePath, \"utf-8\");\n if (anchor) {\n fileContent = extractLines(fileContent, anchor);\n }\n return {\n sourceType: \"local\",\n content: fileContent,\n filePath: localFilePath,\n };\n } catch (_e) {\n // Fallback to fetch if file read fails\n }\n }\n\n // Remote fallback: fetch raw content from githubusercontent\n const rawUrl = `https://raw.githubusercontent.com/${owner}/${repo}/${branch}/${filePath}`;\n try {\n const response = await fetch(rawUrl);\n if (response.ok) {\n let rawText = await response.text();\n if (anchor) {\n rawText = extractLines(rawText, anchor);\n }\n return {\n sourceType: \"remote_web\",\n content: rawText,\n url: cleaned,\n };\n }\n } catch (_e) {\n // Fallback to generic URL loading\n }\n }\n\n // 2.b Generic HTTPS/HTTP URLs\n try {\n const response = await fetch(cleaned);\n if (response.ok) {\n const text = await response.text();\n const cleanText = htmlToText(text);\n return {\n sourceType: \"remote_web\",\n content: cleanText,\n url: cleaned,\n };\n }\n throw new Error(`HTTP error ${response.status}: ${response.statusText}`);\n } catch (err) {\n return {\n sourceType: \"remote_web\",\n content: `Error fetching URL reference: ${(err as Error).message}\\nLink: ${cleaned}`,\n url: cleaned,\n };\n }\n }\n\n // 3. Local Workspace Path (relative to process.cwd)\n const anchorIndex = cleaned.indexOf(\"#\");\n const relativePath =\n anchorIndex !== -1 ? cleaned.slice(0, anchorIndex) : cleaned;\n const anchor = anchorIndex !== -1 ? cleaned.slice(anchorIndex) : \"\";\n const absolutePath = resolve(process.cwd(), relativePath);\n\n if (existsSync(absolutePath)) {\n try {\n let fileContent = readFileSync(absolutePath, \"utf-8\");\n if (anchor) {\n fileContent = extractLines(fileContent, anchor);\n }\n return {\n sourceType: \"local\",\n content: fileContent,\n filePath: absolutePath,\n };\n } catch (_e) {\n // Fallback\n }\n }\n\n // Final fallback: return the path/link description as string\n return {\n sourceType: \"local\",\n content: `Local reference file not found or unreadable.\\nReference: ${cleaned}`,\n filePath: absolutePath,\n };\n}\n\n/**\n * Resolve a token's source_link into bounded, review-ready context.\n *\n * Wraps {@link resolveReference} for the review/bridge flow: returns `null`\n * for empty links and caps content length so the surrounding payload (bridge\n * JSON or terminal output) stays manageable, flagging when truncation occurred.\n */\nexport async function resolveReviewContext(\n sourceLink: string | null | undefined,\n opts: { maxChars?: number } = {},\n): Promise<ReviewContext | null> {\n const cleaned = sourceLink?.trim();\n if (!cleaned) return null;\n\n const maxChars = opts.maxChars ?? DEFAULT_REVIEW_CONTEXT_MAX_CHARS;\n const resolved = await resolveReference(cleaned);\n\n let content = resolved.content;\n let truncated = false;\n if (content.length > maxChars) {\n content = content.slice(0, maxChars);\n truncated = true;\n }\n\n return {\n sourceLink: cleaned,\n sourceType: resolved.sourceType,\n content,\n filePath: resolved.filePath,\n url: resolved.url,\n truncated,\n };\n}\n\n/**\n * Normalizes a path, stripping anchors and converting separators.\n */\nexport function normalizePath(p: string): string {\n const base = p.split(\"#\")[0].trim();\n return base.replace(/\\\\/g, \"/\").toLowerCase();\n}\n\n/**\n * Checks if a token's source_link references a changed file.\n */\nexport function matchesFilePath(\n sourceLink: string | null,\n changedFile: string,\n): boolean {\n if (!sourceLink) return false;\n\n const normSource = normalizePath(sourceLink);\n const normChanged = normalizePath(changedFile);\n\n if (!normSource || !normChanged) return false;\n\n // 1. GitHub URI matching\n const gitHubMatch =\n /^https?:\\/\\/(?:www\\.)?github\\.com\\/([^/]+)\\/([^/]+)\\/blob\\/([^/]+)\\/(.+)$/i.exec(\n normSource,\n );\n if (gitHubMatch) {\n const filePath = gitHubMatch[4];\n return filePath === normChanged;\n }\n\n // Generic URL check (don't match web references against local paths)\n if (normSource.startsWith(\"http://\") || normSource.startsWith(\"https://\")) {\n return false;\n }\n\n // 2. Relative/Absolute path matching\n return normSource.endsWith(normChanged) || normChanged.endsWith(normSource);\n}\n","/**\n * Cross-domain interleaving — prevents same-domain streaks in review queues.\n *\n * Given an array of items with a `domain` field, reorders them using a\n * round-robin strategy so that consecutive items come from different domains.\n * This leverages the interleaving effect: mixing topics during practice\n * strengthens discrimination and long-term retention.\n */\n\n// ── Types ────────────────────────────────────────────────────────────────────\n\nexport interface QueueItem {\n cardId: string;\n tokenId: string;\n domain: string;\n}\n\n// ── Functions ────────────────────────────────────────────────────────────────\n\n/**\n * Reorder items so no domain appears more than `maxConsecutive` times in a row.\n *\n * Algorithm: group items by domain, then round-robin across domain groups.\n * Each round picks one item from each non-exhausted domain. Within a domain,\n * the original order is preserved (so urgency sorting survives).\n *\n * If a domain has more items than others, its extras will appear after all\n * other domains are exhausted — but the `maxConsecutive` cap is still\n * respected by inserting items from the largest remaining domains first.\n *\n * @param items - Array of items to interleave. Not mutated.\n * @param maxConsecutive - Max consecutive items from the same domain. Defaults to 2.\n * @returns A new array with the same items in interleaved order.\n */\nexport function interleave<T extends { domain: string }>(\n items: T[],\n maxConsecutive: number = 2,\n): T[] {\n if (items.length <= 1) return [...items];\n\n // Group items by domain, preserving original order within each group\n const byDomain = new Map<string, T[]>();\n for (const item of items) {\n const group = byDomain.get(item.domain);\n if (group) {\n group.push(item);\n } else {\n byDomain.set(item.domain, [item]);\n }\n }\n\n // If there's only one domain, no interleaving possible\n if (byDomain.size === 1) return [...items];\n\n const result: T[] = [];\n let consecutiveCount = 0;\n let lastDomain: string | null = null;\n\n // Track how many items we've consumed from each domain\n const cursors = new Map<string, number>();\n for (const domain of byDomain.keys()) {\n cursors.set(domain, 0);\n }\n\n // Round-robin: sort domains by remaining count (largest first) each round\n while (result.length < items.length) {\n // Get domains that still have items, sorted by remaining count descending\n const activeDomains = [...byDomain.entries()]\n .filter(\n ([domain]) =>\n (cursors.get(domain) ?? 0) < (byDomain.get(domain)?.length ?? 0),\n )\n .sort((a, b) => {\n const remainA = a[1].length - (cursors.get(a[0]) ?? 0);\n const remainB = b[1].length - (cursors.get(b[0]) ?? 0);\n return remainB - remainA;\n });\n\n if (activeDomains.length === 0) break;\n\n let pickedThisRound = false;\n\n for (const [domain, group] of activeDomains) {\n const cursor = cursors.get(domain) ?? 0;\n if (cursor >= group.length) continue;\n\n // Check if adding from this domain would exceed maxConsecutive\n if (domain === lastDomain && consecutiveCount >= maxConsecutive) {\n // Try to find another domain first\n continue;\n }\n\n // Pick one item from this domain\n result.push(group[cursor]);\n cursors.set(domain, cursor + 1);\n pickedThisRound = true;\n\n if (domain === lastDomain) {\n consecutiveCount++;\n } else {\n lastDomain = domain;\n consecutiveCount = 1;\n }\n\n break;\n }\n\n // If we couldn't pick without exceeding maxConsecutive, we must accept\n // a streak from whatever domain has items left\n if (!pickedThisRound) {\n for (const [domain, group] of activeDomains) {\n const cursor = cursors.get(domain) ?? 0;\n if (cursor >= group.length) continue;\n\n result.push(group[cursor]);\n cursors.set(domain, cursor + 1);\n\n if (domain === lastDomain) {\n consecutiveCount++;\n } else {\n lastDomain = domain;\n consecutiveCount = 1;\n }\n\n break;\n }\n }\n }\n\n return result;\n}\n","/**\n * Review Queue Builder — assembles a session's review queue.\n *\n * Combines due-card fetching, new-card selection, urgency sorting,\n * and cross-domain interleaving into a single ready-to-review queue.\n */\n\nimport type { Database } from \"libsql\";\nimport { interleave } from \"./interleaver.js\";\n\n// ── Types ────────────────────────────────────────────────────────────────────\n\nexport interface ReviewQueueOptions {\n userId: string;\n maxNew?: number; // default 10\n maxReviews?: number; // default 50\n now?: Date;\n}\n\nexport interface ReviewQueueItem {\n cardId: string;\n tokenId: string;\n slug: string;\n concept: string;\n domain: string;\n bloomLevel: number;\n state: string; // 'new' | 'learning' | 'review' | 'relearning'\n dueAt: string;\n sourceLink: string | null;\n question: string | null;\n}\n\nexport interface ReviewQueue {\n items: ReviewQueueItem[];\n newCount: number;\n reviewCount: number;\n relearnCount: number;\n totalDomains: string[];\n}\n\n// ── Internal row type from SQL queries ───────────────────────────────────────\n\ninterface CardRow {\n card_id: string;\n token_id: string;\n slug: string;\n concept: string;\n domain: string;\n bloom_level: number;\n state: string;\n due_at: string;\n source_link: string | null;\n question: string | null;\n}\n\n// ── Functions ────────────────────────────────────────────────────────────────\n\n/**\n * Build a review queue for a user's study session.\n *\n * The queue is assembled in stages:\n * 1. Fetch all due cards (not blocked, due_at <= now, state in review/relearning/learning)\n * 2. Fetch new cards (state = 'new', not blocked) up to maxNew\n * 3. Sort overdue cards by urgency — most overdue first\n * 4. Apply cross-domain interleaving to prevent same-domain streaks\n * 5. Intersperse new cards at regular intervals (every 5th position)\n * 6. Cap total at maxReviews\n *\n * @param db - Database connection\n * @param options - Queue building options\n * @returns The assembled review queue with metadata\n */\nexport function buildReviewQueue(\n db: Database,\n options: ReviewQueueOptions,\n): ReviewQueue {\n const maxNew = options.maxNew ?? 10;\n const maxReviews = options.maxReviews ?? 50;\n const now = options.now ?? new Date();\n const nowISO = now.toISOString();\n\n // ── Step 1: Fetch due cards (review, relearning, learning — not new) ───\n const dueRows = db\n .prepare(\n `SELECT\n c.id AS card_id,\n c.token_id AS token_id,\n t.slug AS slug,\n t.concept AS concept,\n t.domain AS domain,\n t.bloom_level AS bloom_level,\n c.state AS state,\n c.due_at AS due_at,\n t.source_link AS source_link,\n t.question AS question\n FROM cards c\n JOIN tokens t ON t.id = c.token_id\n WHERE c.user_id = ?\n AND c.blocked = 0\n AND c.due_at <= ?\n AND c.state IN ('review', 'relearning', 'learning')\n AND t.deprecated_at IS NULL\n ORDER BY c.due_at ASC`,\n )\n .all(options.userId, nowISO) as CardRow[];\n\n // ── Step 2: Fetch new cards ────────────────────────────────────────────\n const newRows = db\n .prepare(\n `SELECT\n c.id AS card_id,\n c.token_id AS token_id,\n t.slug AS slug,\n t.concept AS concept,\n t.domain AS domain,\n t.bloom_level AS bloom_level,\n c.state AS state,\n c.due_at AS due_at,\n t.source_link AS source_link,\n t.question AS question\n FROM cards c\n JOIN tokens t ON t.id = c.token_id\n WHERE c.user_id = ?\n AND c.blocked = 0\n AND c.state = 'new'\n AND t.deprecated_at IS NULL\n ORDER BY t.bloom_level ASC, t.slug ASC\n LIMIT ?`,\n )\n .all(options.userId, maxNew) as CardRow[];\n\n // ── Step 3: Sort overdue cards by urgency (most overdue first) ─────────\n const nowMs = now.getTime();\n const sortedDue = [...dueRows].sort((a, b) => {\n const overdueA = nowMs - new Date(a.due_at).getTime();\n const overdueB = nowMs - new Date(b.due_at).getTime();\n return overdueB - overdueA; // most overdue first\n });\n\n // ── Step 4: Apply cross-domain interleaving to due cards ───────────────\n const interleavedDue = interleave(\n sortedDue.map((row) => ({ ...rowToItem(row), domain: row.domain })),\n );\n\n // ── Step 5: Intersperse new cards at regular intervals ─────────────────\n const newItems = newRows.map(rowToItem);\n const merged = intersperseNew(interleavedDue, newItems, 5);\n\n // ── Step 6: Cap total at maxReviews ────────────────────────────────────\n const capped = merged.slice(0, maxReviews);\n\n // ── Compute metadata ──────────────────────────────────────────────────\n let newCount = 0;\n let reviewCount = 0;\n let relearnCount = 0;\n const domainSet = new Set<string>();\n\n for (const item of capped) {\n domainSet.add(item.domain);\n switch (item.state) {\n case \"new\":\n newCount++;\n break;\n case \"relearning\":\n relearnCount++;\n break;\n default:\n reviewCount++;\n break;\n }\n }\n\n return {\n items: capped,\n newCount,\n reviewCount,\n relearnCount,\n totalDomains: [...domainSet].sort(),\n };\n}\n\n// ── Helpers ──────────────────────────────────────────────────────────────────\n\n/** Convert a SQL row to a ReviewQueueItem. */\nfunction rowToItem(row: CardRow): ReviewQueueItem {\n return {\n cardId: row.card_id,\n tokenId: row.token_id,\n slug: row.slug,\n concept: row.concept,\n domain: row.domain,\n bloomLevel: row.bloom_level,\n state: row.state,\n dueAt: row.due_at,\n sourceLink: row.source_link,\n question: row.question,\n };\n}\n\n/**\n * Intersperse new cards into the review queue at regular intervals.\n *\n * Instead of front-loading or back-loading new cards, places one new card\n * every `interval` positions (e.g., positions 4, 9, 14, ...).\n * This gives the user a mix of familiar reviews and new material.\n *\n * @param reviews - The interleaved review cards\n * @param newCards - New cards to intersperse\n * @param interval - Place a new card every N positions (default 5)\n * @returns Merged array with new cards interspersed\n */\nfunction intersperseNew(\n reviews: ReviewQueueItem[],\n newCards: ReviewQueueItem[],\n interval: number,\n): ReviewQueueItem[] {\n if (newCards.length === 0) return [...reviews];\n if (reviews.length === 0) return [...newCards];\n\n const result: ReviewQueueItem[] = [];\n let reviewIdx = 0;\n let newIdx = 0;\n\n // Position counter tracks where we are in the final queue\n let position = 0;\n\n while (reviewIdx < reviews.length || newIdx < newCards.length) {\n // Insert a new card every `interval` positions (0-indexed: at 4, 9, 14, ...)\n if (\n newIdx < newCards.length &&\n position > 0 &&\n position % interval === interval - 1\n ) {\n result.push(newCards[newIdx]);\n newIdx++;\n } else if (reviewIdx < reviews.length) {\n result.push(reviews[reviewIdx]);\n reviewIdx++;\n } else if (newIdx < newCards.length) {\n // No more reviews — append remaining new cards\n result.push(newCards[newIdx]);\n newIdx++;\n }\n\n position++;\n }\n\n return result;\n}\n","import {\n appendFileSync,\n copyFileSync,\n existsSync,\n mkdirSync,\n readFileSync,\n} from \"node:fs\";\nimport { homedir } from \"node:os\";\nimport { join } from \"node:path\";\nimport { fileURLToPath } from \"node:url\";\n\nconst HOME = homedir();\n\n/**\n * Get the path to ZAM's internal package SKILL.md.\n */\nexport function getPackageSkillPath(\n agent: \"default\" | \"claude\" | \"codex\" = \"default\",\n): string {\n // Support source modules plus the dist/index.js and dist/cli/index.js bundles.\n const packageRoot =\n [\n fileURLToPath(new URL(\"../../..\", import.meta.url)),\n fileURLToPath(new URL(\"../..\", import.meta.url)),\n fileURLToPath(new URL(\"..\", import.meta.url)),\n ].find((candidate) => existsSync(join(candidate, \"package.json\"))) ?? \"\";\n\n if (!packageRoot) return \"\";\n\n if (agent === \"codex\") {\n const codexPath = join(packageRoot, \".agents\", \"skills\", \"zam\", \"SKILL.md\");\n if (existsSync(codexPath)) return codexPath;\n return \"\";\n }\n\n if (agent === \"claude\") {\n const claudePath = join(\n packageRoot,\n \".claude\",\n \"skills\",\n \"zam\",\n \"SKILL.md\",\n );\n if (existsSync(claudePath)) return claudePath;\n return \"\";\n }\n\n // Try .agent first\n let path = join(packageRoot, \".agent\", \"skills\", \"zam\", \"SKILL.md\");\n if (existsSync(path)) return path;\n\n // Try .claude\n path = join(packageRoot, \".claude\", \"skills\", \"zam\", \"SKILL.md\");\n if (existsSync(path)) return path;\n\n return \"\";\n}\n\n/**\n * Distribute the ZAM active-recall training skill globally.\n * Copies SKILL.md into global directories for supported coding agents.\n */\nexport function distributeGlobalSkills(home: string = HOME): Array<{\n name: string;\n path: string;\n success: boolean;\n}> {\n const sourceSkill = getPackageSkillPath();\n const claudeSourceSkill = getPackageSkillPath(\"claude\");\n const codexSourceSkill = getPackageSkillPath(\"codex\");\n const results: Array<{ name: string; path: string; success: boolean }> = [];\n\n if (!sourceSkill) {\n console.warn(\"Could not find ZAM source SKILL.md in the package folder.\");\n return results;\n }\n\n // 1. Claude Code global directory\n const claudeSkillsDir = join(home, \".claude\", \"skills\", \"zam\");\n try {\n if (!claudeSourceSkill) {\n throw new Error(\"Claude skill source not found\");\n }\n mkdirSync(claudeSkillsDir, { recursive: true });\n copyFileSync(claudeSourceSkill, join(claudeSkillsDir, \"SKILL.md\"));\n results.push({\n name: \"Claude Code Global\",\n path: join(claudeSkillsDir, \"SKILL.md\"),\n success: true,\n });\n } catch (_err) {\n results.push({\n name: \"Claude Code Global\",\n path: claudeSkillsDir,\n success: false,\n });\n }\n\n // 2. Gemini/agy global directory\n const geminiSkillsDir = join(home, \".gemini\", \"skills\", \"zam\");\n try {\n mkdirSync(geminiSkillsDir, { recursive: true });\n copyFileSync(sourceSkill, join(geminiSkillsDir, \"SKILL.md\"));\n results.push({\n name: \"Gemini CLI Global\",\n path: join(geminiSkillsDir, \"SKILL.md\"),\n success: true,\n });\n } catch (_err) {\n results.push({\n name: \"Gemini CLI Global\",\n path: geminiSkillsDir,\n success: false,\n });\n }\n\n // 3. Codex global skills directory\n const codexSkillsDir = join(home, \".agents\", \"skills\", \"zam\");\n try {\n if (!codexSourceSkill) {\n throw new Error(\"Codex skill source not found\");\n }\n mkdirSync(codexSkillsDir, { recursive: true });\n copyFileSync(codexSourceSkill, join(codexSkillsDir, \"SKILL.md\"));\n results.push({\n name: \"Codex Global\",\n path: join(codexSkillsDir, \"SKILL.md\"),\n success: true,\n });\n } catch (_err) {\n results.push({\n name: \"Codex Global\",\n path: codexSkillsDir,\n success: false,\n });\n }\n\n // 4. Goose skills directory\n const gooseSkillsDir = join(home, \".goose\", \"skills\", \"zam\");\n try {\n mkdirSync(gooseSkillsDir, { recursive: true });\n copyFileSync(sourceSkill, join(gooseSkillsDir, \"SKILL.md\"));\n results.push({\n name: \"Goose Global\",\n path: join(gooseSkillsDir, \"SKILL.md\"),\n success: true,\n });\n } catch (_err) {\n results.push({\n name: \"Goose Global\",\n path: gooseSkillsDir,\n success: false,\n });\n }\n\n return results;\n}\n\n/**\n * Inject the ZAM shell observation hook into user profile scripts so monitoring\n * is automatically initialized on startup.\n */\nexport function injectShellHooks(): Array<{\n shell: string;\n file: string;\n success: boolean;\n alreadyHooked: boolean;\n}> {\n const results: Array<{\n shell: string;\n file: string;\n success: boolean;\n alreadyHooked: boolean;\n }> = [];\n const hookLine = `\\n# ZAM Shell Observation Hooks\\nif (command -v zam >/dev/null 2>&1); then eval \"$(zam monitor start --quiet)\"; fi\\n`;\n const pwshHookLine = `\\n# ZAM Shell Observation Hooks\\nif (Get-Command zam -ErrorAction SilentlyContinue) { Invoke-Expression (& zam monitor start --quiet pwsh) }\\n`;\n\n // 1. Zsh profile (~/.zshrc)\n const zshrc = join(HOME, \".zshrc\");\n if (existsSync(zshrc)) {\n try {\n const content = readFileSync(zshrc, \"utf8\");\n if (content.includes(\"zam monitor start\")) {\n results.push({\n shell: \"zsh\",\n file: zshrc,\n success: true,\n alreadyHooked: true,\n });\n } else {\n appendFileSync(zshrc, hookLine);\n results.push({\n shell: \"zsh\",\n file: zshrc,\n success: true,\n alreadyHooked: false,\n });\n }\n } catch {\n results.push({\n shell: \"zsh\",\n file: zshrc,\n success: false,\n alreadyHooked: false,\n });\n }\n }\n\n // 2. Bash profile (~/.bashrc)\n const bashrc = join(HOME, \".bashrc\");\n if (existsSync(bashrc)) {\n try {\n const content = readFileSync(bashrc, \"utf8\");\n if (content.includes(\"zam monitor start\")) {\n results.push({\n shell: \"bash\",\n file: bashrc,\n success: true,\n alreadyHooked: true,\n });\n } else {\n appendFileSync(bashrc, hookLine);\n results.push({\n shell: \"bash\",\n file: bashrc,\n success: true,\n alreadyHooked: false,\n });\n }\n } catch {\n results.push({\n shell: \"bash\",\n file: bashrc,\n success: false,\n alreadyHooked: false,\n });\n }\n }\n\n // 3. PowerShell Profile ($HOME\\Documents\\PowerShell\\Microsoft.PowerShell_profile.ps1)\n // Check both PowerShell and WindowsPowerShell\n const pwshDirs = [\n join(HOME, \"Documents\", \"PowerShell\"),\n join(HOME, \"Documents\", \"WindowsPowerShell\"),\n ];\n\n for (const dir of pwshDirs) {\n const profileFile = join(dir, \"Microsoft.PowerShell_profile.ps1\");\n try {\n mkdirSync(dir, { recursive: true });\n let content = \"\";\n if (existsSync(profileFile)) {\n content = readFileSync(profileFile, \"utf8\");\n }\n\n if (content.includes(\"zam monitor start\")) {\n results.push({\n shell: \"powershell\",\n file: profileFile,\n success: true,\n alreadyHooked: true,\n });\n } else {\n appendFileSync(profileFile, pwshHookLine);\n results.push({\n shell: \"powershell\",\n file: profileFile,\n success: true,\n alreadyHooked: false,\n });\n }\n } catch {\n results.push({\n shell: \"powershell\",\n file: profileFile,\n success: false,\n alreadyHooked: false,\n });\n }\n }\n\n return results;\n}\n","import type { SupportedLocale } from \"./locale.js\";\n\nexport type TranslationKey =\n | \"welcome\"\n | \"new_review_relearn\"\n | \"domains\"\n | \"instruction\"\n | \"quit_hint\"\n | \"offline_warning\"\n | \"offline_instruction\"\n | \"nothing_due\"\n | \"evaluating\"\n | \"generating_question\"\n | \"translating\"\n | \"prompt_answer\"\n | \"session_ended\"\n | \"session_complete\"\n | \"cards_rated\"\n | \"avg_rating\"\n | \"forgot\"\n | \"feedback_title\"\n | \"answer_title\"\n | \"keep_waiting\"\n | \"local_ai_working\"\n | \"wait_warning\"\n | \"wait_info\"\n | \"keep_waiting_llm\"\n | \"proceeding_offline\"\n | \"eval_skipped\";\n\nexport const TRANSLATIONS: Record<\n SupportedLocale,\n Record<TranslationKey, string>\n> = {\n en: {\n welcome: \"Learning session: {count} card(s)\",\n new_review_relearn: \" New: {newC} Review: {reviewC} Relearn: {relearnC}\",\n domains: \" Domains: {domains}\",\n instruction:\n \"\\nRecall each answer first, reveal it, then rate yourself honestly.\",\n quit_hint:\n \"Type 'q' at the answer prompt (or press Ctrl+C) to stop anytime.\",\n offline_warning:\n \"\\n\\x1b[33m⚠ LLM-Feedback & automatic translation are disabled.\\x1b[0m\",\n offline_instruction:\n \" Enable with: \\x1b[36mnpm run dev -- settings llm on\\x1b[0m\\n\",\n nothing_due: \"Nothing due to learn. You're all caught up!\",\n evaluating: \"Evaluating answer via local AI...\",\n generating_question: \"Generating dynamic question...\",\n translating: \"Translating question dynamically...\",\n prompt_answer: \"Your answer (Enter to reveal · 'q' to stop):\",\n session_ended: \"Learning session ended.\",\n session_complete: \"Learning session complete!\",\n cards_rated: \" Cards rated: {count}\",\n avg_rating: \" Average rating: {avg}\",\n forgot: \" Forgot: {count} card(s)\",\n feedback_title: \"── ZAM Feedback {line}\",\n answer_title: \"── Answer {line}\",\n keep_waiting: \"Would you like to keep waiting?\",\n local_ai_working: \"The local AI is still generating the response.\",\n wait_warning: \"⚠ The LLM server is taking a while to load the model.\",\n wait_info:\n \"(This is expected when transitioning between models or starting up from cold.)\",\n keep_waiting_llm: \"Would you like to keep waiting for the model?\",\n proceeding_offline:\n \"⚠ Proceeding in offline-mode (without active LLM evaluations for this session).\",\n eval_skipped: \" [LLM Evaluation skipped: {reason}]\",\n },\n de: {\n welcome: \"Lern-Session: {count} Karte(n)\",\n new_review_relearn:\n \" Neu: {newC} Wiederholen: {reviewC} Lernen: {relearnC}\",\n domains: \" Domänen: {domains}\",\n instruction:\n \"\\nRufe jede Antwort zuerst ab, decke sie auf und bewerte dich dann ehrlich selbst.\",\n quit_hint:\n \"Gib 'q' bei der Antwortaufforderung ein (oder drücke Strg+C), um jederzeit zu beenden.\",\n offline_warning:\n \"\\n\\x1b[33m⚠ LLM-Feedback & automatische Übersetzung sind deaktiviert.\\x1b[0m\",\n offline_instruction:\n \" Aktivieren mit: \\x1b[36mnpm run dev -- settings llm on\\x1b[0m\\n\",\n nothing_due: \"Nichts fällig zu lernen. Du bist komplett auf dem Laufenden!\",\n evaluating: \"Bewerte Antwort via lokaler KI...\",\n generating_question: \"Generiere dynamische Frage...\",\n translating: \"Übersetze Frage dynamisch...\",\n prompt_answer: \"Deine Antwort (Eingabe zum Aufdecken · 'q' zum Beenden):\",\n session_ended: \"Lern-Session beendet.\",\n session_complete: \"Lern-Session abgeschlossen!\",\n cards_rated: \" Bewertete Karten: {count}\",\n avg_rating: \" Durchschnittliche Bewertung: {avg}\",\n forgot: \" Vergessen: {count} Karte(n)\",\n feedback_title: \"── ZAM Feedback {line}\",\n answer_title: \"── Antwort {line}\",\n keep_waiting: \"Möchtest du weiter auf die Bewertung warten?\",\n local_ai_working: \"Die lokale KI arbeitet noch an der Antwort.\",\n wait_warning:\n \"⚠ Der LLM-Server braucht ungewöhnlich lange, um das Modell zu laden.\",\n wait_info:\n \"(Das ist normal, wenn das Modell gewechselt wird oder kalt startet.)\",\n keep_waiting_llm: \"Möchtest du weiter auf das Modell warten?\",\n proceeding_offline:\n \"⚠ Fahre im Offline-Modus fort (ohne aktive LLM-Bewertungen in dieser Runde).\",\n eval_skipped: \" [LLM-Bewertung übersprungen: {reason}]\",\n },\n es: {\n welcome: \"Sesión de aprendizaje: {count} tarjeta(s)\",\n new_review_relearn:\n \" Nuevas: {newC} Repasar: {reviewC} Reaprender: {relearnC}\",\n domains: \" Dominios: {domains}\",\n instruction:\n \"\\nRecuerda cada respuesta primero, revélala y califícate honestamente.\",\n quit_hint:\n \"Escribe 'q' en la respuesta (o presiona Ctrl+C) para salir en cualquier momento.\",\n offline_warning:\n \"\\n\\x1b[33m⚠ Los comentarios de LLM y la traducción automática están desactivados.\\x1b[0m\",\n offline_instruction:\n \" Activar con: \\x1b[36mnpm run dev -- settings llm on\\x1b[0m\\n\",\n nothing_due: \"No hay nada pendiente para aprender. ¡Estás al día!\",\n evaluating: \"Evaluando respuesta con IA local...\",\n generating_question: \"Generando pregunta dinámica...\",\n translating: \"Traduciendo pregunta dinámicamente...\",\n prompt_answer: \"Tu respuesta (Intro para revelar · 'q' para salir):\",\n session_ended: \"Sesión de aprendizaje finalizada.\",\n session_complete: \"¡Sesión de aprendizaje completada!\",\n cards_rated: \" Tarjetas calificadas: {count}\",\n avg_rating: \" Calificación promedio: {avg}\",\n forgot: \" Olvidadas: {count} tarjeta(s)\",\n feedback_title: \"── Comentarios de ZAM {line}\",\n answer_title: \"── Respuesta {line}\",\n keep_waiting: \"¿Deseas seguir esperando la evaluación?\",\n local_ai_working: \"La IA local todavía está generando la respuesta.\",\n wait_warning: \"⚠ El servidor LLM está tardando en cargar el modelo.\",\n wait_info: \"(Esto es normal al cambiar de modelo o iniciar en frío.)\",\n keep_waiting_llm: \"¿Deseas seguir esperando el modelo?\",\n proceeding_offline:\n \"⚠ Continuando en modo fuera de línea (sin evaluaciones de LLM en esta sesión).\",\n eval_skipped: \" [Evaluación de LLM omitida: {reason}]\",\n },\n fr: {\n welcome: \"Session d'apprentissage : {count} carte(s)\",\n new_review_relearn:\n \" Nouveau: {newC} Révision: {reviewC} Relever: {relearnC}\",\n domains: \" Domaines: {domains}\",\n instruction:\n \"\\nRappelez-vous chaque réponse d'abord, révélez-la, puis évaluez-vous honnêtement.\",\n quit_hint: \"Tapez 'q' (ou Ctrl+C) pour quitter à tout moment.\",\n offline_warning:\n \"\\n\\x1b[33m⚠ Les commentaires LLM et la traduction automatique sont désactivés.\\x1b[0m\",\n offline_instruction:\n \" Activer avec : \\x1b[36mnpm run dev -- settings llm on\\x1b[0m\\n\",\n nothing_due: \"Rien à apprendre. Vous êtes à jour !\",\n evaluating: \"Évaluation de la réponse via l'IA locale...\",\n generating_question: \"Génération d'une question dynamique...\",\n translating: \"Traduction dynamique de la question...\",\n prompt_answer: \"Votre réponse (Entrée pour révéler · 'q' pour quitter) :\",\n session_ended: \"Session d'apprentissage arrêtée.\",\n session_complete: \"Session d'apprentissage terminée !\",\n cards_rated: \" Cartes évaluées: {count}\",\n avg_rating: \" Note moyenne: {avg}\",\n forgot: \" Oubliées: {count} carte(s)\",\n feedback_title: \"── Commentaires ZAM {line}\",\n answer_title: \"── Réponse {line}\",\n keep_waiting: \"Voulez-vous continuer à attendre l'évaluation ?\",\n local_ai_working:\n \"L'IA locale est toujours en train de générer la réponse.\",\n wait_warning: \"⚠ Le serveur LLM prend du temps pour charger le modèle.\",\n wait_info:\n \"(Ceci est normal lors de la transition entre modèles ou du démarrage à froid.)\",\n keep_waiting_llm: \"Voulez-vous continuer à attendre le modèle ?\",\n proceeding_offline:\n \"⚠ Poursuite en mode hors ligne (sans évaluation active de l'IA pour cette session).\",\n eval_skipped: \" [Évaluation LLM ignorée : {reason}]\",\n },\n pt: {\n welcome: \"Sessão de aprendizado: {count} cartão(ões)\",\n new_review_relearn:\n \" Novos: {newC} Revisar: {reviewC} Reaprender: {relearnC}\",\n domains: \" Domínios: {domains}\",\n instruction:\n \"\\nLembre-se de cada resposta primeiro, revele-a e avalie-se honestamente.\",\n quit_hint: \"Digite 'q' (ou Ctrl+C) para parar a qualquer momento.\",\n offline_warning:\n \"\\n\\x1b[33m⚠ O feedback do LLM e a tradução automática estão desativados.\\x1b[0m\",\n offline_instruction:\n \" Ativar com: \\x1b[36mnpm run dev -- settings llm on\\x1b[0m\\n\",\n nothing_due: \"Nada faturado para aprender. Você está atualizado!\",\n evaluating: \"Avaliando a resposta via IA local...\",\n generating_question: \"Gerando pergunta dinâmica...\",\n translating: \"Traduzindo pergunta dinamicamente...\",\n prompt_answer: \"Sua resposta (Enter para revelar · 'q' para parar):\",\n session_ended: \"Sessão de aprendizado encerrada.\",\n session_complete: \"Sessão de aprendizado concluída!\",\n cards_rated: \" Cartões avaliados: {count}\",\n avg_rating: \" Nota média: {avg}\",\n forgot: \" Esquecidos: {count} cartão(ões)\",\n feedback_title: \"── Feedback ZAM {line}\",\n answer_title: \"── Resposta {line}\",\n keep_waiting: \"Deseja continuar esperando pela avaliação?\",\n local_ai_working: \"A IA local ainda está gerando a resposta.\",\n wait_warning: \"⚠ O servidor LLM está demorando para carregar o modelo.\",\n wait_info: \"(Isso é esperado ao alternar modelos ou iniciar do zero.)\",\n keep_waiting_llm: \"Deseja continuar esperando o modelo?\",\n proceeding_offline:\n \"⚠ Continuando no modo offline (sem avaliações de LLM ativas nesta sessão).\",\n eval_skipped: \" [Avaliação LLM omitida: {reason}]\",\n },\n zh: {\n welcome: \"学习课: {count} 张卡片\",\n new_review_relearn: \" 新卡: {newC} 复习: {reviewC} 重学: {relearnC}\",\n domains: \" 知识领域: {domains}\",\n instruction: \"\\n首先在脑中回忆答案,然后揭晓并诚实自我评分。\",\n quit_hint: \"在回答提示处输入 'q' (或按 Ctrl+C) 可随时退出。\",\n offline_warning: \"\\n\\x1b[33m⚠ LLM 反馈与自动翻译已禁用。\\x1b[0m\",\n offline_instruction:\n \" 开启命令: \\x1b[36mnpm run dev -- settings llm on\\x1b[0m\\n\",\n nothing_due: \"目前没有需要学习的内容。您已全部掌握!\",\n evaluating: \"正在通过本地 AI 评估回答...\",\n generating_question: \"正在动态生成问题...\",\n translating: \"正在动态翻译问题...\",\n prompt_answer: \"您的回答 (按回车揭晓 · 输入 'q' 退出):\",\n session_ended: \"学习课已结束。\",\n session_complete: \"学习课已完成!\",\n cards_rated: \" 已评分卡片: {count}\",\n avg_rating: \" 平均分: {avg}\",\n forgot: \" 遗忘: {count} 张卡片\",\n feedback_title: \"── ZAM 反馈 {line}\",\n answer_title: \"── 参考答案 {line}\",\n keep_waiting: \"是否继续等待评分?\",\n local_ai_working: \"本地 AI 仍在生成回答。\",\n wait_warning: \"⚠ LLM 服务器正在加载模型,这可能需要一些时间。\",\n wait_info: \"(这在切换模型或冷启动时是正常现象。)\",\n keep_waiting_llm: \"是否继续等待模型加载?\",\n proceeding_offline:\n \"⚠ 正在以离线模式继续(本次学习课将不包含活跃的 AI 评估)。\",\n eval_skipped: \" [已跳过 LLM 评估: {reason}]\",\n },\n ja: {\n welcome: \"学習セッション: {count} 枚のカード\",\n new_review_relearn: \" 新規: {newC} 復習: {reviewC} 再学習: {relearnC}\",\n domains: \" ドメイン: {domains}\",\n instruction:\n \"\\n最初に回答を思い出し、次に回答を表示して、正直に自己評価してください。\",\n quit_hint:\n \"回答プロンプトで「q」を入力する(または Ctrl+C を押す)と、いつでも終了できます。\",\n offline_warning:\n \"\\n\\x1b[33m⚠ LLM フィードバックと自動翻訳は無効です。\\x1b[0m\",\n offline_instruction:\n \" 有効化するには: \\x1b[36mnpm run dev -- settings llm on\\x1b[0m\\n\",\n nothing_due: \"学習予定のカードはありません。すべて完了しています!\",\n evaluating: \"ローカルAIによる回答の評価中...\",\n generating_question: \"質問を動的に生成中...\",\n translating: \"質問を動的に翻訳中...\",\n prompt_answer: \"あなたの回答 (Enterで表示 · 'q'で終了):\",\n session_ended: \"学習セッションが終了しました。\",\n session_complete: \"学習セッションが完了しました!\",\n cards_rated: \" 評価済みカード数: {count}\",\n avg_rating: \" 平均評価: {avg}\",\n forgot: \" 忘れたカード数: {count} 枚\",\n feedback_title: \"── ZAM フィードバック {line}\",\n answer_title: \"── 解答 {line}\",\n keep_waiting: \"評価の生成を待ちますか?\",\n local_ai_working: \"ローカルAIが回答を生成しています。\",\n wait_warning:\n \"⚠ LLM サーバーがモデルをロードするのに時間がかかっています。\",\n wait_info: \"(モデルの移行中やコールドスタート時には、これが予想されます。)\",\n keep_waiting_llm: \"モデルのロードを待ち続けますか?\",\n proceeding_offline:\n \"⚠ オフラインモードで続行します(このセッションではアクティブな AI 評価は行われません)。\",\n eval_skipped: \" [LLM 評価がスキップされました: {reason}]\",\n },\n};\n\n/**\n * Format and interpolate a translation string with key-value params.\n */\nexport function t(\n locale: SupportedLocale,\n key: TranslationKey,\n params: Record<string, string | number> = {},\n): string {\n const dict = TRANSLATIONS[locale] || TRANSLATIONS.en;\n let str = dict[key] || TRANSLATIONS.en[key] || \"\";\n\n for (const [k, v] of Object.entries(params)) {\n str = str.replace(new RegExp(`{${k}}`, \"g\"), String(v));\n }\n\n return str;\n}\n","import { execSync } from \"node:child_process\";\nimport { existsSync } from \"node:fs\";\nimport { homedir } from \"node:os\";\nimport { join } from \"node:path\";\n\nexport interface InstallResult {\n success: boolean;\n message: string;\n}\n\n/**\n * Check if a command is executable on the system.\n */\nexport function hasCommand(cmd: string): boolean {\n try {\n const checkCmd =\n process.platform === \"win32\" ? `where ${cmd}` : `which ${cmd}`;\n execSync(checkCmd, { stdio: \"ignore\" });\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Install FastFlowLM via winget on Windows.\n */\nexport function installFastFlowLM(): InstallResult {\n if (process.platform !== \"win32\") {\n return {\n success: false,\n message: \"FastFlowLM is only supported on Windows.\",\n };\n }\n\n // Check if already installed\n const hasFlm =\n hasCommand(\"flm\") || existsSync(\"C:\\\\Program Files\\\\flm\\\\flm.exe\");\n if (hasFlm) {\n return { success: true, message: \"FastFlowLM is already installed.\" };\n }\n\n if (!hasCommand(\"winget\")) {\n return {\n success: false,\n message: \"winget package manager was not found on this system.\",\n };\n }\n\n console.log(\"Installing FastFlowLM via winget...\");\n try {\n // -e option exact match, --accept-source-agreements --accept-package-agreements\n execSync(\n \"winget install -e --id FastFlowLM --accept-source-agreements --accept-package-agreements\",\n { stdio: \"inherit\" },\n );\n return { success: true, message: \"FastFlowLM installed successfully.\" };\n } catch (err) {\n return {\n success: false,\n message: `Failed to install FastFlowLM: ${(err as Error).message}`,\n };\n }\n}\n\n/**\n * Install Ollama via Homebrew on macOS.\n */\nexport function installOllama(): InstallResult {\n // Check if already installed\n const isMac = process.platform === \"darwin\";\n const isWin = process.platform === \"win32\";\n const hasOllama =\n hasCommand(\"ollama\") ||\n (isMac && existsSync(\"/Applications/Ollama.app\")) ||\n (isWin &&\n existsSync(\n join(homedir(), \"AppData\", \"Local\", \"Programs\", \"Ollama\", \"ollama.exe\"),\n ));\n\n if (hasOllama) {\n return { success: true, message: \"Ollama is already installed.\" };\n }\n\n if (process.platform === \"darwin\") {\n if (!hasCommand(\"brew\")) {\n return {\n success: false,\n message:\n \"Homebrew was not found. Please install Homebrew from brew.sh first.\",\n };\n }\n console.log(\"Installing Ollama via Homebrew Cask...\");\n try {\n execSync(\"brew install --cask ollama\", { stdio: \"inherit\" });\n return { success: true, message: \"Ollama installed successfully.\" };\n } catch (err) {\n return {\n success: false,\n message: `Failed to install Ollama: ${(err as Error).message}`,\n };\n }\n } else if (process.platform === \"win32\") {\n if (!hasCommand(\"winget\")) {\n return {\n success: false,\n message: \"winget was not found. Please install winget first.\",\n };\n }\n console.log(\"Installing Ollama via winget...\");\n try {\n execSync(\n \"winget install -e --id Ollama.Ollama --accept-source-agreements --accept-package-agreements\",\n { stdio: \"inherit\" },\n );\n return { success: true, message: \"Ollama installed successfully.\" };\n } catch (err) {\n return {\n success: false,\n message: `Failed to install Ollama: ${(err as Error).message}`,\n };\n }\n } else {\n // Linux installer script\n console.log(\"Installing Ollama via official installer script...\");\n try {\n execSync(\"curl -fsSL https://ollama.com/install.sh | sh\", {\n stdio: \"inherit\",\n });\n return { success: true, message: \"Ollama installed successfully.\" };\n } catch (err) {\n return {\n success: false,\n message: `Failed to install Ollama: ${(err as Error).message}`,\n };\n }\n }\n}\n","import { execSync } from \"node:child_process\";\n\nexport type SupportedLocale = \"en\" | \"de\" | \"es\" | \"fr\" | \"pt\" | \"zh\" | \"ja\";\n\nconst SUPPORTED_LOCALES: Set<SupportedLocale> = new Set([\n \"en\",\n \"de\",\n \"es\",\n \"fr\",\n \"pt\",\n \"zh\",\n \"ja\",\n]);\n\n/**\n * Clean and map raw locale string (e.g., \"de_DE.UTF-8\" or \"en-US\") to SupportedLocale.\n */\nexport function normalizeLocale(raw: string): SupportedLocale {\n const clean = raw.trim().toLowerCase().split(/[_-]/)[0];\n if (SUPPORTED_LOCALES.has(clean as SupportedLocale)) {\n return clean as SupportedLocale;\n }\n return \"en\";\n}\n\n/**\n * Detect the operating system's active language code dynamically.\n */\nexport function detectSystemLocale(): SupportedLocale {\n try {\n // 1. Check standard POSIX env vars (common on macOS/Linux/Git Bash/WSL)\n const envVars = [\n process.env.LANG,\n process.env.LANGUAGE,\n process.env.LC_ALL,\n process.env.LC_MESSAGES,\n ];\n\n for (const val of envVars) {\n if (val && val.trim().length > 0) {\n return normalizeLocale(val);\n }\n }\n\n // 2. On Windows, fallback to querying PowerShell Culture\n if (process.platform === \"win32\") {\n const output = execSync(\n 'powershell -NoProfile -Command \"[System.Globalization.CultureInfo]::CurrentCulture.Name\"',\n { stdio: \"pipe\", encoding: \"utf8\", timeout: 2000 },\n ).trim();\n if (output && output.length > 0) {\n return normalizeLocale(output);\n }\n }\n } catch {\n // Ignore errors and default to English\n }\n\n return \"en\";\n}\n","import { execSync } from \"node:child_process\";\n\nexport interface SystemProfile {\n os: \"windows\" | \"macos\" | \"linux\" | \"unknown\";\n arch: \"x64\" | \"arm64\" | \"unknown\";\n hasRyzenNPU: boolean;\n hasAppleSilicon: boolean;\n recommendedRunner: \"fastflowlm\" | \"ollama\" | \"generic\";\n recommendedModel: string;\n}\n\n/**\n * Run a shell command synchronously and return stdout.\n * Returns empty string on failure.\n */\nfunction runCommand(cmd: string): string {\n try {\n return execSync(cmd, { stdio: \"pipe\", encoding: \"utf8\" }).trim();\n } catch {\n return \"\";\n }\n}\n\nfunction detectWindowsAMDIPU(): boolean {\n if (process.platform !== \"win32\") return false;\n\n // WMI query for AMD IPU (Image Processing Unit), NPU, Ryzen AI CPUs, and modern NPU compute devices (DEV_1502, DEV_17F0)\n const cmd = `powershell -NoProfile -Command \"Get-CimInstance Win32_PnPEntity | Where-Object { $_.Name -like '*AMD IPU*' -or $_.Name -like '*AMD NPU*' -or $_.Name -like '*NPU Compute*' -or $_.Name -like '*Ryzen AI*' -or $_.HardwareID -like '*VEN_1022&DEV_1502*' -or $_.HardwareID -like '*VEN_1022&DEV_17F0*' } | Select-Object -First 1 -ExpandProperty Name\"`;\n const output = runCommand(cmd);\n\n return Boolean(\n output &&\n (output.toLowerCase().includes(\"amd\") ||\n output.toLowerCase().includes(\"ipu\") ||\n output.toLowerCase().includes(\"npu\") ||\n output.toLowerCase().includes(\"ryzen\")),\n );\n}\n\n/**\n * Profile the active system hardware and software capabilities.\n */\nexport function getSystemProfile(): SystemProfile {\n const platform = process.platform;\n const archStr = process.arch;\n\n let os: \"windows\" | \"macos\" | \"linux\" | \"unknown\" = \"unknown\";\n if (platform === \"win32\") os = \"windows\";\n else if (platform === \"darwin\") os = \"macos\";\n else if (platform === \"linux\") os = \"linux\";\n\n let arch: \"x64\" | \"arm64\" | \"unknown\" = \"unknown\";\n if (archStr === \"x64\") arch = \"x64\";\n else if (archStr === \"arm64\") arch = \"arm64\";\n\n const hasRyzenNPU = os === \"windows\" && detectWindowsAMDIPU();\n const hasAppleSilicon = os === \"macos\" && arch === \"arm64\";\n\n let recommendedRunner: \"fastflowlm\" | \"ollama\" | \"generic\" = \"generic\";\n let recommendedModel = \"qwen3.5:4b\";\n\n if (hasRyzenNPU) {\n recommendedRunner = \"fastflowlm\";\n recommendedModel = \"qwen3.5:4b\";\n } else if (hasAppleSilicon) {\n recommendedRunner = \"ollama\";\n recommendedModel = \"llama3.2:3b\";\n } else if (os === \"macos\" || os === \"linux\" || os === \"windows\") {\n // Standard PC / generic Mac\n recommendedRunner = \"ollama\";\n recommendedModel = \"llama3.2:3b\";\n }\n\n return {\n os,\n arch,\n hasRyzenNPU,\n hasAppleSilicon,\n recommendedRunner,\n recommendedModel,\n };\n}\n","import { existsSync } from \"node:fs\";\nimport { resolve } from \"node:path\";\nimport type { Database } from \"libsql\";\nimport { getSetting } from \"../models/settings.js\";\n\nexport interface RepoPaths {\n personal: string | null;\n team: string | null;\n org: string | null;\n}\n\n/**\n * Resolve absolute paths for personal, team, and organization repositories.\n * Personal falls back to personal.workspace_dir if repo.personal is not set.\n */\nexport function getRepoPaths(db: Database): RepoPaths {\n const personalSetting =\n getSetting(db, \"repo.personal\") || getSetting(db, \"personal.workspace_dir\");\n const teamSetting = getSetting(db, \"repo.team\");\n const orgSetting = getSetting(db, \"repo.org\");\n\n return {\n personal: personalSetting ? resolve(personalSetting) : null,\n team: teamSetting ? resolve(teamSetting) : null,\n org: orgSetting ? resolve(orgSetting) : null,\n };\n}\n\n/**\n * Resolve a specific repo's path, or null if not configured.\n */\nexport function resolveRepoPath(\n db: Database,\n type: \"personal\" | \"team\" | \"org\",\n): string | null {\n const paths = getRepoPaths(db);\n return paths[type];\n}\n\n/**\n * Resolve paths to all existing \"/beliefs\" directories in the hierarchy,\n * sorted from most specific (personal) to most general (org).\n */\nexport function resolveAllBeliefPaths(db: Database): string[] {\n const paths = getRepoPaths(db);\n const dirs: string[] = [];\n\n if (paths.personal) {\n const personalDir = resolve(paths.personal, \"beliefs\");\n if (existsSync(personalDir)) dirs.push(personalDir);\n }\n if (paths.team) {\n const teamDir = resolve(paths.team, \"beliefs\");\n if (existsSync(teamDir)) dirs.push(teamDir);\n }\n if (paths.org) {\n const orgDir = resolve(paths.org, \"beliefs\");\n if (existsSync(orgDir)) dirs.push(orgDir);\n }\n\n return dirs;\n}\n\n/**\n * Resolve paths to all existing \"/goals\" directories in the hierarchy,\n * sorted from most specific (personal) to most general (org).\n */\nexport function resolveAllGoalPaths(db: Database): string[] {\n const paths = getRepoPaths(db);\n const dirs: string[] = [];\n\n if (paths.personal) {\n const personalDir = resolve(paths.personal, \"goals\");\n if (existsSync(personalDir)) dirs.push(personalDir);\n }\n if (paths.team) {\n const teamDir = resolve(paths.team, \"goals\");\n if (existsSync(teamDir)) dirs.push(teamDir);\n }\n if (paths.org) {\n const orgDir = resolve(paths.org, \"goals\");\n if (existsSync(orgDir)) dirs.push(orgDir);\n }\n\n return dirs;\n}\n","/**\n * Local LLM client — CLI/harness layer.\n *\n * This module talks to a user-run, OpenAI-compatible local LLM server\n * (FastFlowLM `flm serve`, Ollama, LM Studio, …). It deliberately lives in the\n * CLI layer, NOT the kernel: the kernel is AI-agnostic and carries zero LLM\n * dependencies (see CLAUDE.md). Everything that does HTTP, spawns runner\n * processes, prints to the terminal, or prompts interactively belongs here.\n *\n * Config is read from kernel settings via a single `getLlmConfig` helper so the\n * default model lives in exactly one place (it changes every few weeks as local\n * models improve).\n */\n\nimport { spawn } from \"node:child_process\";\nimport { existsSync } from \"node:fs\";\nimport type { Database } from \"libsql\";\nimport type { SupportedLocale } from \"../../kernel/index.js\";\nimport {\n getSetting,\n getSystemProfile,\n hasCommand,\n resolveReviewContext,\n t,\n updateToken,\n} from \"../../kernel/index.js\";\n\n/** Single source of truth for connection defaults (easy to bump as models evolve). */\nexport const DEFAULT_LLM_URL = \"http://localhost:8000/v1\";\nexport const DEFAULT_LLM_MODEL = \"gemma4-it:e4b\";\nexport const DEFAULT_LLM_API_KEY = \"sk-none\";\n\nexport interface LlmConfig {\n enabled: boolean;\n url: string;\n model: string;\n apiKey: string;\n locale: SupportedLocale;\n}\n\n/** Read all LLM-related settings at once, applying defaults in one place. */\nexport function getLlmConfig(db: Database): LlmConfig {\n return {\n enabled: getSetting(db, \"llm.enabled\") === \"true\",\n url: getSetting(db, \"llm.url\") || DEFAULT_LLM_URL,\n model: getSetting(db, \"llm.model\") || DEFAULT_LLM_MODEL,\n apiKey: getSetting(db, \"llm.api_key\") || DEFAULT_LLM_API_KEY,\n locale: (getSetting(db, \"system.locale\") || \"en\") as SupportedLocale,\n };\n}\n\nconst LANGUAGE_NAMES: Record<SupportedLocale, string> = {\n en: \"English\",\n de: \"German\",\n es: \"Spanish\",\n fr: \"French\",\n pt: \"Portuguese\",\n zh: \"Chinese\",\n ja: \"Japanese\",\n};\n\nconst LOCALIZED_RATING_PREFIX: Record<SupportedLocale, string> = {\n en: \"Suggested rating\",\n de: \"Empfohlene Bewertung\",\n es: \"Calificación sugerida\",\n fr: \"Note suggérée\",\n pt: \"Avaliação sugerida\",\n zh: \"建议评分\",\n ja: \"推奨評価\",\n};\n\nconst BLOOM_VERBS = {\n 1: \"Remember\",\n 2: \"Understand\",\n 3: \"Apply\",\n 4: \"Analyze\",\n 5: \"Synthesize\",\n} as const;\n\ninterface ChatCompletionResponse {\n choices?: Array<{ message?: { content?: string } }>;\n}\n\n/** Extract the assistant message content from an OpenAI-compatible response. */\nasync function readChatContent(res: Response, label: string): Promise<string> {\n if (!res.ok) {\n const errorText = await res.text().catch(() => \"\");\n throw new Error(\n `${label} failed: ${res.statusText} (${res.status}) - ${errorText}`,\n );\n }\n const data = (await res.json()) as ChatCompletionResponse;\n const content = data.choices?.[0]?.message?.content;\n if (!content) {\n throw new Error(\"Empty response from LLM\");\n }\n return content.trim();\n}\n\n/**\n * Generate a high-quality, concept-free active-recall question using the local LLM.\n */\nexport async function generateQuestionViaLLM(\n db: Database,\n input: {\n slug: string;\n concept: string;\n domain: string;\n bloomLevel: number;\n context?: string;\n sourceLinkContent?: string | null;\n },\n): Promise<string> {\n const cfg = getLlmConfig(db);\n if (!cfg.enabled) {\n throw new Error(\"LLM integration is disabled in settings (llm.enabled)\");\n }\n\n const bloom = (\n input.bloomLevel >= 1 && input.bloomLevel <= 5 ? input.bloomLevel : 1\n ) as keyof typeof BLOOM_VERBS;\n const verb = BLOOM_VERBS[bloom];\n\n const langName = LANGUAGE_NAMES[cfg.locale] || \"English\";\n\n const systemPrompt = `You are ZAM, a highly precise agentic skills trainer.\nYour task is to generate a single, clear, conceptual active-recall question (flashcard front) in ${langName} for a knowledge token.\n\nGuidelines:\n1. The question MUST match the Bloom level: ${verb} (Level ${bloom}).\n2. CRITICAL: The question MUST NOT contain or reveal the concept text itself! The concept is the answer (flashcard back) that the learner needs to recall.\n3. Keep the question concise, highly specific, and clear. Avoid generic prompts like \"What is the concept of...\" if possible, and ask about the core mechanism, function, or purpose of the slug/concept without giving the answer away.\n4. Output ONLY the raw question text in ${langName}. Do not include any preamble, headers, markdown fences, or conversational filler.`;\n\n const userPrompt = `Domain: ${input.domain}\nSlug: ${input.slug}\nConcept to Recall (DO NOT REVEAL IN QUESTION): ${input.concept}\nContext: ${input.context || \"(none)\"}\n${input.sourceLinkContent ? `Source Reference:\\n${input.sourceLinkContent}` : \"\"}\n\nActive-Recall Question:`;\n\n const res = await fetchWithInteractiveTimeout(`${cfg.url}/chat/completions`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n Authorization: `Bearer ${cfg.apiKey}`,\n },\n body: JSON.stringify({\n model: cfg.model,\n messages: [\n { role: \"system\", content: systemPrompt },\n { role: \"user\", content: userPrompt },\n ],\n temperature: 0.1,\n max_tokens: 150,\n }),\n locale: cfg.locale,\n });\n\n return readChatContent(res, \"LLM request\");\n}\n\n/**\n * Warmly evaluate the learner's active-recall answer against the target concept.\n * Suggests an FSRS rating (1-4) and explains in the active locale with praise/motivation.\n */\nexport async function evaluateAnswerViaLLM(\n db: Database,\n input: {\n slug: string;\n concept: string;\n domain: string;\n bloomLevel: number;\n context?: string;\n question: string;\n userAnswer: string;\n sourceLinkContent?: string | null;\n },\n): Promise<string> {\n const cfg = getLlmConfig(db);\n if (!cfg.enabled) {\n throw new Error(\"LLM integration is disabled in settings (llm.enabled)\");\n }\n const langName = LANGUAGE_NAMES[cfg.locale] || \"English\";\n const ratingPrefix =\n LOCALIZED_RATING_PREFIX[cfg.locale] || \"Suggested rating\";\n\n const systemPrompt = `You are ZAM, an extremely warm, encouraging, and patient skills trainer.\nYour mission is to build lasting autonomy through conceptual knowledge, not rote procedure.\nCompare the learner's active-recall answer against the target concept, context, and optional source code.\n\nFSRS Rating scale:\n- 1: drew a blank / completely forgot or wrong (Again)\n- 2: hard recall / partially correct (Hard)\n- 3: knew it / mostly correct (Good)\n- 4: perfect, instant, and accurate recall (Easy)\n\nGuidelines:\n1. Provide a constructive, encouraging evaluation in ${langName} (2-3 sentences) to promote the joy of learning. Seamlessly weave a brief explanation of the correct solution (target concept) into your feedback paragraphs. Do NOT append a separate, duplicate reference answer or raw \"Musterlösung\" block at the end of your response.\n2. Celebrate every honest attempt! Offer high praise or a motivating word of encouragement in ${langName} if they did well or tried hard.\n3. CRITICAL: ZAM is a strict one-shot card flow, NOT an interactive chat. The correct Musterlösung (reference answer) is revealed alongside your feedback. Therefore, NEVER ask the user to think further, keep guessing, or suggest they try to solve the remaining parts of the question. Instead, immediately evaluate what they wrote, explain the complete solution and target concept directly.\n4. Suggest a clear FSRS rating (1 to 4) at the very end of your response in the exact format: \"${ratingPrefix}: X\" in ${langName}.\n5. Output ONLY the evaluation and rating suggestion. Keep it concise, friendly, and clean. No conversational introduction or markdown wrapper.`;\n\n const userPrompt = `Domain: ${input.domain}\nSlug: ${input.slug}\nRecall Question: ${input.question}\nLearner's Answer: ${input.userAnswer}\n\nTarget Concept (Correct Answer): ${input.concept}\nTarget Context: ${input.context || \"(none)\"}\n${input.sourceLinkContent ? `Source Code Reference:\\n${input.sourceLinkContent}` : \"\"}\n\nEvaluation:`;\n\n const res = await fetchWithInteractiveTimeout(`${cfg.url}/chat/completions`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n Authorization: `Bearer ${cfg.apiKey}`,\n },\n body: JSON.stringify({\n model: cfg.model,\n messages: [\n { role: \"system\", content: systemPrompt },\n { role: \"user\", content: userPrompt },\n ],\n temperature: 0.2,\n max_tokens: 300,\n }),\n locale: cfg.locale,\n });\n\n return readChatContent(res, \"LLM evaluation\");\n}\n\n/**\n * Translate a question into the active locale using the local LLM.\n */\nexport async function translateQuestionViaLLM(\n db: Database,\n question: string,\n): Promise<string> {\n const cfg = getLlmConfig(db);\n if (!cfg.enabled) {\n throw new Error(\"LLM integration is disabled in settings\");\n }\n const targetLang = LANGUAGE_NAMES[cfg.locale] || \"English\";\n\n const systemPrompt = `You are a highly precise translator. Translate the given active-recall question into clear, natural ${targetLang}.\nOutput ONLY the raw translation. Do not include any headers, preamble, quotes, or conversational filler.`;\n\n const res = await fetchWithInteractiveTimeout(`${cfg.url}/chat/completions`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n Authorization: `Bearer ${cfg.apiKey}`,\n },\n body: JSON.stringify({\n model: cfg.model,\n messages: [\n { role: \"system\", content: systemPrompt },\n { role: \"user\", content: question },\n ],\n temperature: 0.1,\n max_tokens: 150,\n }),\n locale: cfg.locale,\n });\n\n return readChatContent(res, \"Translation\");\n}\n\n/**\n * Checks if the LLM server is online and responsive at the specified URL.\n * A reachable server returning any HTTP status counts as online; only network\n * failures / timeouts count as offline.\n */\nexport async function isLlmOnline(url: string): Promise<boolean> {\n try {\n const controller = new AbortController();\n const timeoutId = setTimeout(() => controller.abort(), 1500);\n // Check OpenAI standard /models list to verify readiness\n const res = await fetch(`${url}/models`, {\n method: \"GET\",\n signal: controller.signal,\n });\n clearTimeout(timeoutId);\n return res.ok || res.status < 500;\n } catch {\n return false;\n }\n}\n\n/**\n * List the model ids the server actually serves (OpenAI `/v1/models`).\n * Returns [] on any error so callers can treat \"unknown\" as \"skip validation\".\n */\nexport async function getAvailableModels(\n url: string,\n apiKey = DEFAULT_LLM_API_KEY,\n): Promise<string[]> {\n try {\n const controller = new AbortController();\n const timeoutId = setTimeout(() => controller.abort(), 3000);\n const res = await fetch(`${url}/models`, {\n method: \"GET\",\n headers: { Authorization: `Bearer ${apiKey}` },\n signal: controller.signal,\n });\n clearTimeout(timeoutId);\n if (!res.ok) return [];\n const data = (await res.json()) as { data?: Array<{ id?: string }> };\n return (data.data ?? [])\n .map((m) => m.id)\n .filter((id): id is string => typeof id === \"string\" && id.length > 0);\n } catch {\n return [];\n }\n}\n\n/** Whether the local LLM can actually be used this session, and if not, why. */\nexport interface LlmReadiness {\n usable: boolean;\n reason?: \"disabled\" | \"offline\" | \"model-not-found\";\n}\n\ntype RunnerKind = \"fastflowlm\" | \"ollama\" | \"generic\" | \"unknown\";\n\n/** Pick the local runner from the URL port / model name (shared heuristic). */\nfunction detectRunner(\n url: string,\n model: string,\n): { runner: RunnerKind; port: string } {\n let runner: RunnerKind = \"unknown\";\n let port = \"8000\";\n try {\n const urlObj = new URL(url);\n port = urlObj.port || (urlObj.protocol === \"https:\" ? \"443\" : \"80\");\n if (\n port === \"8000\" ||\n port === \"8080\" ||\n model.includes(\"qwen\") ||\n model.includes(\"gemma\")\n ) {\n runner = \"fastflowlm\";\n } else if (port === \"11434\" || model.includes(\"llama\")) {\n runner = \"ollama\";\n }\n } catch {\n runner = getSystemProfile().recommendedRunner;\n }\n return { runner, port };\n}\n\n/**\n * Best-effort, SILENT runner start for non-interactive contexts (bridge / GUI).\n * No console output, no prompts — just spawn the detached server if we can.\n */\nfunction spawnLocalRunner(url: string, model: string): void {\n const { runner, port } = detectRunner(url, model);\n try {\n if (runner === \"fastflowlm\") {\n const flmExe = existsSync(\"C:\\\\Program Files\\\\flm\\\\flm.exe\")\n ? \"C:\\\\Program Files\\\\flm\\\\flm.exe\"\n : \"flm\";\n if (!hasCommand(\"flm\") && flmExe === \"flm\") return;\n spawn(flmExe, [\"serve\", model, \"--port\", port], {\n detached: true,\n stdio: \"ignore\",\n windowsHide: true,\n }).unref();\n } else if (runner === \"ollama\" || runner === \"generic\") {\n if (!hasCommand(\"ollama\")) return;\n spawn(\"ollama\", [\"serve\"], {\n detached: true,\n stdio: \"ignore\",\n windowsHide: true,\n }).unref();\n }\n } catch {\n // best-effort: caller polls for the server and reports offline if it fails\n }\n}\n\n/**\n * Detect the configured runner and start it, then poll until the server is\n * online (or the user opts out). Returns true once reachable, false otherwise.\n */\nasync function startLocalRunner(\n url: string,\n model: string,\n locale: SupportedLocale,\n): Promise<boolean> {\n const { runner, port } = detectRunner(url, model);\n\n if (runner === \"fastflowlm\") {\n const flmExe = existsSync(\"C:\\\\Program Files\\\\flm\\\\flm.exe\")\n ? \"C:\\\\Program Files\\\\flm\\\\flm.exe\"\n : \"flm\";\n if (!hasCommand(\"flm\") && flmExe === \"flm\") {\n console.warn(\n \"\\x1b[31m✗ FastFlowLM is configured but could not be found on the system.\\x1b[0m\",\n );\n console.warn(\"Please run 'zam init' or install it manually.\");\n return false;\n }\n const args = [\"serve\", model, \"--port\", port];\n console.log(\n `\\x1b[36mStarting FastFlowLM serve process: ${flmExe} ${args.join(\" \")}\\x1b[0m`,\n );\n try {\n spawn(flmExe, args, {\n detached: true,\n stdio: \"ignore\",\n windowsHide: true,\n }).unref();\n } catch (err) {\n console.error(\n `\\x1b[31m✗ Failed to launch FastFlowLM process: ${(err as Error).message}\\x1b[0m`,\n );\n return false;\n }\n } else if (runner === \"ollama\" || runner === \"generic\") {\n if (!hasCommand(\"ollama\")) {\n console.warn(\n \"\\x1b[31m✗ Ollama is configured but the 'ollama' command is not available in PATH.\\x1b[0m\",\n );\n console.warn(\"Please run 'zam init' or install it manually.\");\n return false;\n }\n console.log(\"\\x1b[36mStarting Ollama serve process: ollama serve\\x1b[0m\");\n try {\n spawn(\"ollama\", [\"serve\"], {\n detached: true,\n stdio: \"ignore\",\n windowsHide: true,\n }).unref();\n } catch (err) {\n console.error(\n `\\x1b[31m✗ Failed to launch Ollama process: ${(err as Error).message}\\x1b[0m`,\n );\n return false;\n }\n } else {\n console.warn(\n \"\\x1b[33m⚠ Unknown local LLM runner configured. Cannot auto-start.\\x1b[0m\",\n );\n return false;\n }\n\n // Poll until the server is online (or the user opts out).\n console.log(\n \"Waiting for LLM server to become responsive and load the model...\",\n );\n let attempts = 0;\n const dotsPerLine = 30;\n while (true) {\n await new Promise((resolve) => setTimeout(resolve, 500));\n if (await isLlmOnline(url)) {\n if (attempts > 0) process.stdout.write(\"\\n\");\n return true;\n }\n attempts++;\n process.stdout.write(\".\");\n if (attempts % dotsPerLine === 0) process.stdout.write(\"\\n\");\n\n // 60 attempts * 500ms = 30 seconds\n if (attempts >= 60) {\n process.stdout.write(\"\\n\");\n console.log(`\\x1b[33m${t(locale, \"wait_warning\")}\\x1b[0m`);\n console.log(`\\x1b[2m${t(locale, \"wait_info\")}\\x1b[0m`);\n\n const { confirm } = await import(\"@inquirer/prompts\");\n const keepWaiting = await confirm({\n message: t(locale, \"keep_waiting_llm\"),\n default: true,\n }).catch(() => false);\n\n if (!keepWaiting) {\n return false;\n }\n attempts = 0;\n }\n }\n}\n\n/**\n * Make the local LLM ready for the session and report whether it is usable.\n *\n * Starts the local runner if needed, then — crucially — verifies the configured\n * model is actually served. A wrong model name otherwise leaves the server\n * reachable but every request hanging/failing, which previously looked like\n * \"the AI is just slow\". We now fail fast with an actionable message instead.\n */\nexport async function ensureLocalLlmRunning(\n db: Database,\n): Promise<LlmReadiness> {\n const cfg = getLlmConfig(db);\n if (!cfg.enabled) {\n return { usable: false, reason: \"disabled\" };\n }\n\n const { url, model, apiKey, locale } = cfg;\n const isLocal = url.includes(\"localhost\") || url.includes(\"127.0.0.1\");\n\n console.log(`Checking if local LLM server is online at ${url}...`);\n let online = await isLlmOnline(url);\n\n if (!online && isLocal) {\n console.log(`\\x1b[33m⚠ Local LLM server is offline on ${url}.\\x1b[0m`);\n online = await startLocalRunner(url, model, locale);\n }\n\n if (!online) {\n console.warn(\n `\\x1b[33m⚠ LLM server is not reachable at ${url}. Continuing without AI coaching.\\x1b[0m\\n`,\n );\n return { usable: false, reason: \"offline\" };\n }\n\n console.log(\"\\x1b[32m✓ Local LLM server is online.\\x1b[0m\");\n\n // Validate the configured model against what the server actually serves, so\n // a typo / wrong tag fails immediately instead of hanging on every request.\n const available = await getAvailableModels(url, apiKey);\n const modelKnown =\n available.length === 0 ||\n available.some((m) => m.toLowerCase() === model.toLowerCase());\n\n if (!modelKnown) {\n console.warn(\n `\\x1b[31m✗ Configured model \"${model}\" is not available on the server.\\x1b[0m`,\n );\n console.warn(` Available models: ${available.join(\", \")}`);\n console.warn(\n ` Set the right one: \\x1b[36mzam settings set llm.model <name>\\x1b[0m`,\n );\n console.warn(\n \"\\x1b[33m Continuing this session without AI coaching.\\x1b[0m\\n\",\n );\n return { usable: false, reason: \"model-not-found\" };\n }\n\n return { usable: true };\n}\n\n/** Readiness plus the live status details a UI needs to render. */\nexport interface LlmReadyResult extends LlmReadiness {\n online: boolean;\n model: string;\n availableModels: string[];\n}\n\n/**\n * Non-interactive readiness check for the bridge / desktop GUI: start the local\n * runner if needed, wait (bounded) for it to come online, validate the model —\n * all WITHOUT console output or prompts, so the bridge's JSON stays clean.\n */\nexport async function ensureLlmReadyHeadless(\n db: Database,\n opts: { timeoutMs?: number } = {},\n): Promise<LlmReadyResult> {\n const timeoutMs = opts.timeoutMs ?? 25000;\n const { enabled, url, model, apiKey } = getLlmConfig(db);\n if (!enabled) {\n return {\n usable: false,\n reason: \"disabled\",\n online: false,\n model,\n availableModels: [],\n };\n }\n\n const isLocal = url.includes(\"localhost\") || url.includes(\"127.0.0.1\");\n\n let online = await isLlmOnline(url);\n if (!online && isLocal) {\n spawnLocalRunner(url, model);\n const deadline = Date.now() + timeoutMs;\n while (Date.now() < deadline) {\n await new Promise((r) => setTimeout(r, 1000));\n if (await isLlmOnline(url)) {\n online = true;\n break;\n }\n }\n }\n\n if (!online) {\n return {\n usable: false,\n reason: \"offline\",\n online: false,\n model,\n availableModels: [],\n };\n }\n\n const availableModels = await getAvailableModels(url, apiKey);\n const modelKnown =\n availableModels.length === 0 ||\n availableModels.some((m) => m.toLowerCase() === model.toLowerCase());\n if (!modelKnown) {\n return {\n usable: false,\n reason: \"model-not-found\",\n online: true,\n model,\n availableModels,\n };\n }\n\n return { usable: true, online: true, model, availableModels };\n}\n\n/**\n * Wraps a fetch call in an interactive wait loop with progress dots.\n * Every `timeoutMs`, prompts the user (in their locale) to keep waiting or skip.\n * In non-TTY / bridge contexts it degrades to a plain fetch.\n */\nexport async function fetchWithInteractiveTimeout(\n url: string,\n options: RequestInit & { timeoutMs?: number; locale?: SupportedLocale } = {},\n): Promise<Response> {\n const { timeoutMs = 20000, locale = \"en\", ...fetchOptions } = options;\n const controller = new AbortController();\n const fetchPromise = fetch(url, {\n ...fetchOptions,\n signal: controller.signal,\n });\n\n if (!process.stdout.isTTY || process.env.ZAM_BRIDGE === \"true\") {\n return fetchPromise;\n }\n\n let attempts = 0;\n const dotsPerLine = 30;\n while (true) {\n let timeoutId: NodeJS.Timeout | undefined;\n const timeoutPromise = new Promise<\"timeout\">((resolve) => {\n timeoutId = setTimeout(() => resolve(\"timeout\"), timeoutMs);\n });\n const dotsInterval = setInterval(() => {\n process.stdout.write(\".\");\n attempts++;\n if (attempts % dotsPerLine === 0) process.stdout.write(\"\\n\");\n }, 500);\n\n try {\n const result = await Promise.race([fetchPromise, timeoutPromise]);\n clearInterval(dotsInterval);\n clearTimeout(timeoutId);\n\n if (result !== \"timeout\") {\n if (attempts > 0) process.stdout.write(\"\\n\");\n return result;\n }\n\n // Timeout hit — ask the learner whether to keep waiting.\n console.log(`\\n\\x1b[33m${t(locale, \"local_ai_working\")}\\x1b[0m`);\n const { confirm } = await import(\"@inquirer/prompts\");\n const keepWaiting = await confirm({\n message: t(locale, \"keep_waiting\"),\n default: true,\n }).catch(() => false);\n\n if (!keepWaiting) {\n controller.abort();\n console.log(`\\x1b[33m${t(locale, \"proceeding_offline\")}\\x1b[0m\\n`);\n throw new Error(\"User cancelled waiting for slow LLM response\");\n }\n attempts = 0;\n } catch (err) {\n clearInterval(dotsInterval);\n clearTimeout(timeoutId);\n if (attempts > 0) process.stdout.write(\"\\n\");\n throw err;\n }\n }\n}\n\n/**\n * Ensures a token has a high-quality active-recall question.\n * When LLM is enabled, generates a fresh question on the fly, self-heals it into\n * the database, and returns it. Otherwise falls back to the stored question.\n */\nexport async function ensureHighQualityQuestion(\n db: Database,\n token: {\n id: string;\n slug: string;\n concept: string;\n domain: string;\n bloomLevel: number;\n sourceLink?: string | null;\n question?: string | null;\n },\n): Promise<string | null> {\n const { enabled } = getLlmConfig(db);\n\n if (enabled) {\n try {\n let sourceLinkContent: string | null = null;\n if (token.sourceLink) {\n const resolved = await resolveReviewContext(token.sourceLink).catch(\n () => null,\n );\n if (resolved) {\n sourceLinkContent = resolved.content;\n }\n }\n\n const generated = await generateQuestionViaLLM(db, {\n slug: token.slug,\n concept: token.concept,\n domain: token.domain,\n bloomLevel: token.bloomLevel,\n sourceLinkContent,\n });\n\n if (generated && generated.trim().length > 0) {\n // Persist the latest high-quality question as the offline fallback.\n updateToken(db, token.slug, { question: generated });\n return generated;\n }\n } catch {\n // Fail silently and fall back to the stored database question.\n }\n }\n\n if (token.question && token.question.trim().length > 0) {\n return token.question;\n }\n\n return null;\n}\n","/**\n * Resolve the active user ID from --user flag or stored whoami setting.\n */\n\nimport type { Database } from \"libsql\";\nimport { getSetting } from \"../../kernel/index.js\";\n\nexport interface ResolveUserOptions {\n /** If true, output JSON error instead of console.error (for bridge commands). */\n json?: boolean;\n}\n\n/**\n * Returns the user ID from the explicit --user flag, or falls back to the\n * stored `user.id` setting. Exits with an error if neither is available.\n */\nexport function resolveUser(\n opts: { user?: string },\n db: Database,\n resolveOpts?: ResolveUserOptions,\n): string {\n if (opts.user) return opts.user;\n\n const stored = getSetting(db, \"user.id\");\n if (stored) return stored;\n\n const message =\n \"No user specified. Set a default with: zam whoami --set <id>\";\n if (resolveOpts?.json) {\n console.log(JSON.stringify({ error: message }, null, 2));\n } else {\n console.error(message);\n }\n process.exit(1);\n}\n","/**\n * `zam card` — Card scheduling subcommand group.\n */\n\nimport { Command } from \"commander\";\nimport type { Database } from \"libsql\";\nimport type { Rating } from \"../../kernel/index.js\";\nimport {\n cascadeBlock,\n deleteCardForUser,\n ensureCard,\n evaluateRating,\n getCardDeletionImpact,\n getDueCards,\n getPrerequisites,\n getTokenBySlug,\n openDatabase,\n unblockReady,\n} from \"../../kernel/index.js\";\nimport { resolveUser } from \"./resolve-user.js\";\n\nfunction withDb(fn: (db: Database) => void): void {\n let db: Database | undefined;\n try {\n db = openDatabase();\n fn(db);\n } catch (err) {\n console.error(\"Error:\", (err as Error).message);\n process.exit(1);\n } finally {\n db?.close();\n }\n}\n\nexport const cardCommand = new Command(\"card\").description(\n \"Manage spaced-repetition cards\",\n);\n\n// ── zam card due ──────────────────────────────────────────────────────────\n\ncardCommand\n .command(\"due\")\n .description(\"Show due tokens for a user\")\n .option(\"--user <id>\", \"User ID (default: whoami)\")\n .option(\"--json\", \"Output as JSON\")\n .option(\"--summary\", \"Show only counts per domain (no slugs or concepts)\")\n .action((opts) => {\n withDb((db) => {\n const userId = resolveUser(opts, db);\n const dueCards = getDueCards(db, userId);\n\n if (opts.json) {\n console.log(JSON.stringify(dueCards, null, 2));\n return;\n }\n\n if (dueCards.length === 0) {\n console.log(\"No cards due for review.\");\n return;\n }\n\n if (opts.summary) {\n const byDomain = new Map<string, { count: number; blooms: number[] }>();\n for (const c of dueCards) {\n const d = c.domain || \"general\";\n const entry = byDomain.get(d) ?? { count: 0, blooms: [] };\n entry.count++;\n entry.blooms.push(c.bloom_level);\n byDomain.set(d, entry);\n }\n console.log(`${dueCards.length} card(s) due:\\n`);\n console.log(\"Domain Count Bloom levels\");\n console.log(\"─\".repeat(45));\n for (const [domain, { count, blooms }] of [\n ...byDomain.entries(),\n ].sort()) {\n const bloomStr = blooms.sort().join(\", \");\n console.log(\n `${domain.padEnd(16)} ${String(count).padEnd(6)} ${bloomStr}`,\n );\n }\n return;\n }\n\n console.log(`${dueCards.length} card(s) due:\\n`);\n console.log(\n \"Slug Concept Domain Bloom State\",\n );\n console.log(\"─\".repeat(90));\n for (const c of dueCards) {\n console.log(\n `${c.slug.padEnd(21)} ${c.concept.slice(0, 31).padEnd(31)} ${(c.domain || \"-\").padEnd(11)} ${String(c.bloom_level).padEnd(6)} ${c.state}`,\n );\n }\n });\n });\n\n// ── zam card update ───────────────────────────────────────────────────────\n\ncardCommand\n .command(\"update\")\n .description(\"Apply a rating to a card\")\n .option(\"--user <id>\", \"User ID (default: whoami)\")\n .requiredOption(\"--token <slug>\", \"Token slug\")\n .requiredOption(\"--rating <n>\", \"Rating (1=Again, 2=Hard, 3=Good, 4=Easy)\")\n .option(\"--json\", \"Output as JSON\")\n .option(\"--quiet\", \"Suppress output (exit code only)\")\n .action((opts) => {\n withDb((db) => {\n const userId = resolveUser(opts, db);\n const token = getTokenBySlug(db, opts.token);\n if (!token) {\n console.error(`Token not found: ${opts.token}`);\n process.exit(1);\n }\n\n const card = ensureCard(db, token.id, userId);\n const rating = Number(opts.rating) as Rating;\n\n if (rating < 1 || rating > 4) {\n console.error(\"Rating must be between 1 and 4.\");\n process.exit(1);\n }\n\n const result = evaluateRating(db, {\n cardId: card.id,\n tokenId: token.id,\n userId,\n rating,\n });\n\n // If rating is 1 (forgot) and token has prerequisites, cascade block\n if (rating === 1) {\n const prereqs = getPrerequisites(db, token.id);\n if (prereqs.length > 0) {\n const blockResult = cascadeBlock(db, userId, token.slug);\n if (opts.quiet) return;\n if (opts.json) {\n console.log(\n JSON.stringify(\n { evaluation: result, blocked: blockResult },\n null,\n 2,\n ),\n );\n } else {\n console.log(\n `Rated ${token.slug} as Again (1) — next due: ${result.nextDueAt}`,\n );\n console.log(\n `Blocked ${blockResult.blockedSlug}. Prerequisites surfaced:`,\n );\n for (const p of blockResult.prerequisites) {\n console.log(` - ${p.slug}: ${p.concept}`);\n }\n }\n return;\n }\n }\n\n if (opts.quiet) return;\n if (opts.json) {\n console.log(JSON.stringify(result, null, 2));\n } else {\n const ratingLabels: Record<number, string> = {\n 1: \"Again\",\n 2: \"Hard\",\n 3: \"Good\",\n 4: \"Easy\",\n };\n console.log(\n `Rated ${token.slug} as ${ratingLabels[rating]} (${rating})`,\n );\n console.log(` Next due: ${result.nextDueAt}`);\n console.log(` Stability: ${result.stability.toFixed(2)}`);\n console.log(` State: ${result.state}`);\n console.log(` Reps: ${result.reps}`);\n }\n });\n });\n\n// ── zam card unblock ──────────────────────────────────────────────────────\n\ncardCommand\n .command(\"unblock\")\n .description(\"Unblock cards whose prerequisites are met\")\n .option(\"--user <id>\", \"User ID (default: whoami)\")\n .option(\"--json\", \"Output as JSON\")\n .option(\"--quiet\", \"Suppress output (exit code only)\")\n .action((opts) => {\n withDb((db) => {\n const userId = resolveUser(opts, db);\n const result = unblockReady(db, userId);\n\n if (opts.quiet) return;\n if (opts.json) {\n console.log(JSON.stringify(result, null, 2));\n return;\n }\n\n if (result.unblocked.length === 0) {\n console.log(\"No cards ready to unblock.\");\n } else {\n console.log(`Unblocked ${result.unblocked.length} card(s):`);\n for (const u of result.unblocked) {\n console.log(` - ${u.slug}: ${u.concept}`);\n }\n }\n });\n });\n\n// ── zam card delete ───────────────────────────────────────────────────────\n\ncardCommand\n .command(\"delete\")\n .description(\"Delete one user's card for a token\")\n .option(\"--user <id>\", \"User ID (default: whoami)\")\n .requiredOption(\"--token <slug>\", \"Token slug\")\n .option(\"--json\", \"Output as JSON\")\n .action((opts) => {\n withDb((db) => {\n const userId = resolveUser(opts, db);\n const token = getTokenBySlug(db, opts.token);\n if (!token) {\n console.error(`Token not found: ${opts.token}`);\n process.exit(1);\n }\n\n const impact = getCardDeletionImpact(db, token.id, userId);\n const result = deleteCardForUser(db, token.id, userId);\n\n if (opts.json) {\n console.log(\n JSON.stringify(\n {\n token: opts.token,\n userId,\n deleted: true,\n cardId: result.card.id,\n impact: result.impact,\n },\n null,\n 2,\n ),\n );\n return;\n }\n\n console.log(`Deleted card for ${opts.token}`);\n console.log(` User: ${userId}`);\n console.log(` Card ID: ${result.card.id}`);\n console.log(` Review logs removed: ${impact.review_logs}`);\n });\n });\n","/**\n * `zam connector` — Manage external service connectors.\n */\n\nimport { input, password } from \"@inquirer/prompts\";\nimport { Command } from \"commander\";\nimport type { Database } from \"libsql\";\nimport { fetchActiveWorkItems } from \"../../kernel/connectors/azure-devops.js\";\nimport {\n clearADOCredentials,\n clearTursoCredentials,\n getADOCredentials,\n getTursoCredentials,\n setADOCredentials,\n setTursoCredentials,\n} from \"../../kernel/credentials.js\";\nimport { openDatabaseWithSync } from \"../../kernel/index.js\";\n\nexport const connectorCommand = new Command(\"connector\").description(\n \"Manage external service connectors\",\n);\n\n// ── zam connector setup ado ─────────────────────────────────────────────────\n\nconnectorCommand\n .command(\"setup\")\n .description(\"Configure a connector\")\n .argument(\"<type>\", \"Connector type (ado, turso)\")\n .option(\"--url <url>\", \"Turso database URL (non-interactive)\")\n .option(\"--token <token>\", \"Turso auth token (non-interactive)\")\n .action(async (type, opts) => {\n if (type === \"turso\") {\n return setupTurso(opts.url, opts.token);\n }\n if (type !== \"ado\") {\n console.error(`Unknown connector type: ${type}. Supported: ado, turso`);\n process.exit(1);\n }\n\n try {\n const orgUrl = await input({\n message: \"Organization URL (e.g. https://dev.azure.com/myorg):\",\n });\n const project = await input({\n message: \"Project name:\",\n });\n const pat = await password({\n message: \"Personal Access Token:\",\n });\n\n if (!orgUrl || !project || !pat) {\n console.error(\"All fields are required.\");\n process.exit(1);\n }\n\n setADOCredentials(orgUrl.replace(/\\/+$/, \"\"), project, pat);\n console.log(`Azure DevOps connector configured for ${orgUrl}/${project}`);\n } catch (err) {\n if ((err as Error).name === \"ExitPromptError\") {\n console.log(\"\\nSetup cancelled.\");\n process.exit(0);\n }\n console.error(\"Error:\", (err as Error).message);\n process.exit(1);\n }\n });\n\n// ── zam connector tasks ─────────────────────────────────────────────────────\n\nconnectorCommand\n .command(\"tasks\")\n .description(\"List active tasks from connected board\")\n .option(\"--json\", \"Output as JSON\")\n .action(async (opts) => {\n try {\n const config = getADOCredentials();\n\n if (!config) {\n console.error(\"No connector configured. Run: zam connector setup ado\");\n process.exit(1);\n }\n\n const items = await fetchActiveWorkItems({\n orgUrl: config.org_url,\n project: config.project,\n pat: config.pat,\n });\n\n if (opts.json) {\n console.log(JSON.stringify(items, null, 2));\n return;\n }\n\n if (items.length === 0) {\n console.log(\"No active work items assigned to you.\");\n return;\n }\n\n console.log(`${items.length} active work item(s):\\n`);\n console.log(\"ID Type State Title\");\n console.log(\"─\".repeat(80));\n for (const wi of items) {\n console.log(\n `${String(wi.id).padEnd(8)} ${wi.type.padEnd(13)} ${wi.state.padEnd(11)} ${wi.title.slice(0, 45)}`,\n );\n }\n } catch (err) {\n console.error(\"Error:\", (err as Error).message);\n process.exit(1);\n }\n });\n\n// ── zam connector clear ─────────────────────────────────────────────────────\n\nconnectorCommand\n .command(\"clear\")\n .description(\"Remove a connector configuration\")\n .argument(\"<type>\", \"Connector type (ado, turso)\")\n .action((type) => {\n if (type === \"turso\") {\n clearTursoCredentials();\n console.log(\"Turso cloud sync removed. Database remains local-only.\");\n return;\n }\n\n if (type !== \"ado\") {\n console.error(`Unknown connector type: ${type}. Supported: ado, turso`);\n process.exit(1);\n }\n\n clearADOCredentials();\n console.log(\"Azure DevOps connector removed.\");\n });\n\n// ── zam connector sync ──────────────────────────────────────────────────────\n\nconnectorCommand\n .command(\"sync\")\n .description(\"Verify the Turso cloud database connection\")\n .action(() => {\n const turso = getTursoCredentials();\n if (!turso) {\n console.error(\n \"No Turso cloud database configured. Run: zam connector setup turso\",\n );\n process.exit(1);\n }\n\n let db: Database | undefined;\n try {\n db = openDatabaseWithSync({ initialize: true });\n db.prepare(\"SELECT 1\").get();\n console.log(`Connected to ${turso.url}`);\n db.close();\n } catch (err) {\n db?.close();\n console.error(\"Error:\", (err as Error).message);\n process.exit(1);\n }\n });\n\n// ── Turso setup helper ──────────────────────────────────────────────────────\n\nasync function setupTurso(urlArg?: string, tokenArg?: string): Promise<void> {\n let db: Database | undefined;\n try {\n const url =\n urlArg ??\n (await input({\n message: \"Turso database URL (e.g. libsql://my-db-user.turso.io):\",\n }));\n const token =\n tokenArg ??\n (await password({\n message: \"Auth token:\",\n }));\n\n if (!url || !token) {\n console.error(\"Both URL and token are required.\");\n process.exit(1);\n }\n\n // Store credentials outside the db so they survive db deletion\n setTursoCredentials(url, token);\n\n // Verify by opening the configured cloud database.\n db = openDatabaseWithSync({ initialize: true });\n db.prepare(\"SELECT 1\").get();\n db.close();\n\n console.log(`Turso cloud database configured and verified: ${url}`);\n } catch (err) {\n db?.close();\n if ((err as Error).name === \"ExitPromptError\") {\n console.log(\"\\nSetup cancelled.\");\n process.exit(0);\n }\n console.error(\"Error:\", (err as Error).message);\n process.exit(1);\n }\n}\n","/**\n * `zam git-sync` — Git integration subcommand.\n *\n * Automatically marks cards as stale/due when their source files change in git.\n */\n\nimport { execSync } from \"node:child_process\";\nimport { chmodSync, existsSync, writeFileSync } from \"node:fs\";\nimport { join } from \"node:path\";\nimport { Command } from \"commander\";\nimport type { Database } from \"libsql\";\nimport type { Token } from \"../../kernel/index.js\";\nimport { getCard, matchesFilePath, openDatabase } from \"../../kernel/index.js\";\nimport { resolveUser } from \"./resolve-user.js\";\n\nfunction withDb(fn: (db: Database) => void): void {\n let db: Database | undefined;\n try {\n db = openDatabase();\n fn(db);\n } catch (err) {\n console.error(\"Error:\", (err as Error).message);\n process.exit(1);\n } finally {\n db?.close();\n }\n}\n\n/**\n * Installs the Git post-commit hook.\n */\nfunction installHook(): void {\n const gitDir = join(process.cwd(), \".git\");\n if (!existsSync(gitDir)) {\n console.error(\n \"Error: Current directory is not the root of a Git repository.\",\n );\n process.exit(1);\n }\n\n const hooksDir = join(gitDir, \"hooks\");\n const hookPath = join(hooksDir, \"post-commit\");\n\n const hookContent = `#!/bin/sh\n# ZAM Spaced Repetition Auto-Stale Hook\n# Triggered automatically on git commits to decay modified concept cards.\nzam git-sync --commit HEAD --quiet\n`;\n\n try {\n writeFileSync(hookPath, hookContent, { encoding: \"utf-8\", flag: \"w\" });\n try {\n chmodSync(hookPath, \"755\");\n } catch (_e) {\n // Best-effort chmod (Windows might ignore/fail, which is fine)\n }\n console.log(\n \"Successfully installed ZAM post-commit hook at .git/hooks/post-commit\",\n );\n } catch (err) {\n console.error(\"Failed to write post-commit hook:\", (err as Error).message);\n process.exit(1);\n }\n}\n\nexport const gitSyncCommand = new Command(\"git-sync\")\n .description(\"Sync learning cards with recent Git file modifications\")\n .option(\"--commit <hash>\", \"Git commit hash to check\", \"HEAD\")\n .option(\"--user <id>\", \"User ID (default: whoami)\")\n .option(\"--install\", \"Install git post-commit hook in current repo\")\n .option(\"--quiet\", \"Suppress verbose output\")\n .action((opts) => {\n if (opts.install) {\n installHook();\n return;\n }\n\n withDb((db) => {\n const userId = resolveUser(opts, db);\n\n let changedFiles: string[] = [];\n try {\n const output = execSync(\n `git diff-tree --no-commit-id --name-only -r ${opts.commit}`,\n {\n encoding: \"utf-8\",\n stdio: [\"ignore\", \"pipe\", \"ignore\"],\n },\n );\n changedFiles = output\n .split(/\\r?\\n/)\n .map((f) => f.trim())\n .filter(Boolean);\n } catch (_err) {\n if (!opts.quiet) {\n console.warn(\n \"Notice: Failed to read git modifications. Ensure you are in a Git repo and commit hash is valid.\",\n );\n }\n return;\n }\n\n if (changedFiles.length === 0) {\n if (!opts.quiet) {\n console.log(\"No file changes detected in commit.\");\n }\n return;\n }\n\n // Fetch active tokens\n const tokens = db\n .prepare(`\n SELECT * FROM tokens \n WHERE source_link IS NOT NULL \n AND deprecated_at IS NULL\n `)\n .all() as Token[];\n\n const matchedTokens: Token[] = [];\n\n for (const token of tokens) {\n const matches = changedFiles.some((cf) =>\n matchesFilePath(token.source_link, cf),\n );\n if (matches) {\n matchedTokens.push(token);\n }\n }\n\n if (matchedTokens.length === 0) {\n if (!opts.quiet) {\n console.log(\n `Scanned ${changedFiles.length} file(s), no associated learning tokens found.`,\n );\n }\n return;\n }\n\n let decayedCount = 0;\n const now = new Date().toISOString();\n\n for (const token of matchedTokens) {\n const card = getCard(db, token.id, userId);\n if (card) {\n // Decay stability to a quarter (concept's source changed → likely stale),\n // with a 0.2-day floor so the card surfaces for review soon. Using max,\n // not min: min would collapse every card to <=0.2 regardless of prior strength.\n const newStability = Math.max(0.2, card.stability / 4.0);\n\n db.prepare(`\n UPDATE cards\n SET due_at = ?,\n stability = ?,\n state = CASE WHEN state = 'new' THEN 'new' ELSE 'review' END,\n elapsed_days = 0.0,\n scheduled_days = 0.0\n WHERE id = ?\n `).run(now, newStability, card.id);\n\n decayedCount++;\n if (!opts.quiet) {\n console.log(\n ` Decayed card for: ${token.slug} (Source: ${token.source_link})`,\n );\n }\n }\n }\n\n if (!opts.quiet) {\n console.log(\n `\\nZAM Auto-Stale Complete: Scanned ${changedFiles.length} file(s).`,\n );\n console.log(\n `Successfully decayed FSRS stability and scheduled reviews for ${decayedCount} concept(s).`,\n );\n }\n });\n });\n","/**\n * `zam goal` — Goal management subcommand group.\n *\n * Goals are markdown files in the personal repo's goals/ directory.\n * The directory is configured via the `personal.goals_dir` setting,\n * or defaults to `./goals` relative to the current working directory.\n */\n\nimport { existsSync, mkdirSync } from \"node:fs\";\nimport { resolve } from \"node:path\";\nimport { input } from \"@inquirer/prompts\";\nimport { Command } from \"commander\";\nimport type { GoalStatus } from \"../../kernel/index.js\";\nimport {\n createGoal,\n extractTasks,\n extractTokenRefs,\n getGoal,\n getGoalTree,\n getSetting,\n listGoals,\n openDatabase,\n updateGoalStatus,\n} from \"../../kernel/index.js\";\n\nfunction resolveGoalsDir(): string {\n let goalsDir: string | undefined;\n\n let db: ReturnType<typeof openDatabase> | undefined;\n try {\n db = openDatabase();\n goalsDir = getSetting(db, \"personal.goals_dir\");\n } catch {\n // DB not available — fall back to default\n } finally {\n db?.close();\n }\n\n return goalsDir ? resolve(goalsDir) : resolve(\"goals\");\n}\n\nexport const goalCommand = new Command(\"goal\").description(\n \"Manage learning goals (markdown files)\",\n);\n\n// ── zam goal list ────────────────────────────────────────────────────────────\n\ngoalCommand\n .command(\"list\")\n .description(\"List all goals\")\n .option(\n \"--status <status>\",\n \"Filter by status (active, completed, paused, abandoned)\",\n )\n .option(\"--tree\", \"Show goals as a tree with parent/child relationships\")\n .option(\"--json\", \"Output as JSON\")\n .action((opts) => {\n const goalsDir = resolveGoalsDir();\n\n if (!existsSync(goalsDir)) {\n console.error(`Goals directory not found: ${goalsDir}`);\n console.error(\n \"Set it with: zam settings set personal.goals_dir /path/to/goals\",\n );\n process.exit(1);\n }\n\n if (opts.tree) {\n const tree = getGoalTree(goalsDir);\n const filtered = opts.status\n ? tree.filter((g) => g.status === opts.status)\n : tree;\n\n if (opts.json) {\n console.log(JSON.stringify(filtered, null, 2));\n return;\n }\n\n if (filtered.length === 0) {\n console.log(\"No goals found.\");\n return;\n }\n\n for (const root of filtered) {\n printGoalLine(root, 0);\n for (const child of root.children) {\n printGoalLine(child, 1);\n }\n }\n return;\n }\n\n let goals = listGoals(goalsDir);\n\n if (opts.status) {\n goals = goals.filter((g) => g.status === opts.status);\n }\n\n if (opts.json) {\n console.log(JSON.stringify(goals, null, 2));\n return;\n }\n\n if (goals.length === 0) {\n console.log(\"No goals found.\");\n return;\n }\n\n console.log(\"Goals:\");\n console.log(` ${\"─\".repeat(70)}`);\n for (const g of goals) {\n printGoalLine(g, 0);\n }\n });\n\nfunction printGoalLine(\n g: {\n slug: string;\n title: string;\n status: string;\n taskCount: number;\n tasksDone: number;\n },\n indent: number,\n): void {\n const prefix = \" \".repeat(indent + 1);\n const statusIcon: Record<string, string> = {\n active: \"[*]\",\n paused: \"[-]\",\n completed: \"[x]\",\n abandoned: \"[ ]\",\n };\n const icon = statusIcon[g.status] || \"[ ]\";\n const tasks = g.taskCount > 0 ? ` (${g.tasksDone}/${g.taskCount} tasks)` : \"\";\n console.log(`${prefix}${icon} ${g.title}${tasks} — ${g.slug}`);\n}\n\n// ── zam goal show ────────────────────────────────────────────────────────────\n\ngoalCommand\n .command(\"show <slug>\")\n .description(\"Show a goal's details\")\n .option(\"--json\", \"Output as JSON\")\n .action((slug, opts) => {\n const goalsDir = resolveGoalsDir();\n const goal = getGoal(goalsDir, slug);\n\n if (!goal) {\n console.error(`Goal not found: ${slug}`);\n process.exit(1);\n }\n\n if (opts.json) {\n const tasks = extractTasks(goal.body);\n const tokens = extractTokenRefs(goal.body);\n console.log(JSON.stringify({ ...goal, tasks, tokens }, null, 2));\n return;\n }\n\n console.log(`Title: ${goal.title}`);\n console.log(`Slug: ${goal.slug}`);\n console.log(`Status: ${goal.status}`);\n if (goal.parent) console.log(`Parent: ${goal.parent}`);\n console.log(`Created: ${goal.created}`);\n console.log(`Updated: ${goal.updated}`);\n\n const tasks = extractTasks(goal.body);\n if (tasks.length > 0) {\n console.log(\n `\\nTasks (${tasks.filter((t) => t.done).length}/${tasks.length}):`,\n );\n for (const t of tasks) {\n console.log(` [${t.done ? \"x\" : \" \"}] ${t.text}`);\n }\n }\n\n const tokens = extractTokenRefs(goal.body);\n if (tokens.length > 0) {\n console.log(`\\nTokens:`);\n for (const ref of tokens) {\n console.log(` - ${ref}`);\n }\n }\n\n if (goal.body) {\n console.log(`\\n${\"─\".repeat(50)}`);\n console.log(goal.body);\n }\n });\n\n// ── zam goal create ──────────────────────────────────────────────────────────\n\ngoalCommand\n .command(\"create\")\n .description(\"Create a new goal\")\n .option(\"--slug <slug>\", \"Goal slug (used as filename)\")\n .option(\"--title <title>\", \"Goal title\")\n .option(\"--parent <slug>\", \"Parent goal slug\")\n .option(\"--description <text>\", \"Goal description\")\n .option(\"--json\", \"Output as JSON\")\n .action(async (opts) => {\n const goalsDir = resolveGoalsDir();\n\n if (!existsSync(goalsDir)) {\n mkdirSync(goalsDir, { recursive: true });\n }\n\n let slug = opts.slug;\n let title = opts.title;\n const parent = opts.parent;\n const description = opts.description;\n\n // Interactive mode if slug or title not provided\n if (!slug || !title) {\n try {\n if (!title) {\n title = await input({ message: \"Goal title:\" });\n }\n if (!slug) {\n const suggested = title\n .toLowerCase()\n .replace(/[^a-z0-9]+/g, \"-\")\n .replace(/^-|-$/g, \"\");\n slug = await input({\n message: \"Goal slug (filename):\",\n default: suggested,\n });\n }\n } catch (err) {\n if ((err as Error).name === \"ExitPromptError\") {\n console.log(\"\\nCancelled.\");\n process.exit(0);\n }\n throw err;\n }\n }\n\n const goal = createGoal(goalsDir, { slug, title, parent, description });\n\n if (opts.json) {\n console.log(JSON.stringify(goal, null, 2));\n return;\n }\n\n console.log(`Goal created: ${goal.slug}`);\n console.log(` Title: ${goal.title}`);\n console.log(` Status: ${goal.status}`);\n console.log(` File: ${goal.filePath}`);\n });\n\n// ── zam goal status ──────────────────────────────────────────────────────────\n\ngoalCommand\n .command(\"status <slug> <status>\")\n .description(\"Update a goal's status (active, paused, completed, abandoned)\")\n .option(\"--json\", \"Output as JSON\")\n .action((slug, status, opts) => {\n const validStatuses: GoalStatus[] = [\n \"active\",\n \"completed\",\n \"paused\",\n \"abandoned\",\n ];\n if (!validStatuses.includes(status)) {\n console.error(\n `Invalid status: ${status}. Must be one of: ${validStatuses.join(\", \")}`,\n );\n process.exit(1);\n }\n\n const goalsDir = resolveGoalsDir();\n const goal = updateGoalStatus(goalsDir, slug, status);\n\n if (opts.json) {\n console.log(JSON.stringify(goal, null, 2));\n return;\n }\n\n console.log(`Goal ${slug} updated to: ${status}`);\n });\n","/**\n * `zam init` — Guided interactive onboarding wizard.\n *\n * Bootstraps a fresh ZAM installation:\n * 1. Initializes a zero-dependency \"Local Sandbox\" workspace.\n * 2. Runs hardware profiling to detect NPUs/CPUs.\n * 3. Installs and configures the hardware-optimized local LLM runtime (flm or Ollama).\n * 4. Automatically distributes global agent skill files and hooks.\n * 5. Sets up the local database and configuration.\n */\n\nimport { existsSync, mkdirSync, writeFileSync } from \"node:fs\";\nimport { homedir } from \"node:os\";\nimport { join } from \"node:path\";\nimport { confirm, input } from \"@inquirer/prompts\";\nimport { Command } from \"commander\";\nimport {\n detectSystemLocale,\n distributeGlobalSkills,\n getSystemProfile,\n injectShellHooks,\n installFastFlowLM,\n installOllama,\n openDatabaseWithSync,\n setSetting,\n} from \"../../kernel/index.js\";\n\nconst HOME = homedir();\n\n/**\n * Helper to print thick borders.\n */\nfunction printLine(char = \"═\", len = 60, color = \"\\x1b[36m\") {\n console.log(`${color}${char.repeat(len)}\\x1b[0m`);\n}\n\n/**\n * Bootstrap the default \"Local Sandbox\" workspace structure.\n */\nfunction bootstrapSandboxWorkspace(workspaceDir: string) {\n mkdirSync(join(workspaceDir, \"beliefs\"), { recursive: true });\n mkdirSync(join(workspaceDir, \"goals\"), { recursive: true });\n mkdirSync(join(workspaceDir, \"skills\"), { recursive: true });\n\n const worldviewFile = join(workspaceDir, \"beliefs\", \"worldview.md\");\n if (!existsSync(worldviewFile)) {\n writeFileSync(\n worldviewFile,\n `# Personal Worldview\n\nHere, I declare the core concepts and principles I want to master.\n\n- **Conceptual Autonomy**: I value deep conceptual understanding over copy-pasting rote procedures.\n- **Continuous Retention**: I use spaced repetition to prevent my professional skills from decaying.\n`,\n \"utf8\",\n );\n }\n\n const goalsFile = join(workspaceDir, \"goals\", \"goals.md\");\n if (!existsSync(goalsFile)) {\n writeFileSync(\n goalsFile,\n `# Personal Goals\n\n- **[ ] Learn Spaced Repetition Core Concepts**\n - #fsrs-stability\n - #fsrs-difficulty\n`,\n \"utf8\",\n );\n }\n}\n\nexport const initCommand = new Command(\"init\")\n .description(\"Launch the guided interactive onboarding wizard\")\n .action(async () => {\n printLine();\n console.log(\n \"\\x1b[1m\\x1b[32m ZAM — The Symbiotic Learning Agent Onboarding\\x1b[0m\",\n );\n console.log(\n \"\\x1b[2m Welcome! ZAM helps you build conceptual skills while you work.\\x1b[0m\",\n );\n printLine();\n\n // ── STEP 1: Workspace Selection ──────────────────────────────────────────\n console.log(\"\\n\\x1b[1m[1/5] Setting up Local Workspace Sandbox\\x1b[0m\");\n const defaultWorkspace = join(HOME, \"Documents\", \"zam\");\n\n const workspacePath = await input({\n message: \"Choose your ZAM workspace directory:\",\n default: defaultWorkspace,\n });\n\n try {\n bootstrapSandboxWorkspace(workspacePath);\n console.log(\n `\\x1b[32m✓ Local Sandbox created at: ${workspacePath}\\x1b[0m`,\n );\n } catch (err) {\n console.error(\n `\\x1b[31m✗ Failed to create workspace: ${(err as Error).message}\\x1b[0m`,\n );\n process.exit(1);\n }\n\n // ── STEP 2: Hardware Auto-Detection ──────────────────────────────────────\n console.log(\"\\n\\x1b[1m[2/5] Running Hardware Profiling...\\x1b[0m\");\n const profile = getSystemProfile();\n\n console.log(` OS Detected: \\x1b[36m${profile.os}\\x1b[0m`);\n console.log(` CPU Architecture: \\x1b[36m${profile.arch}\\x1b[0m`);\n if (profile.hasRyzenNPU) {\n console.log(\n \" NPU Detected: \\x1b[32mAMD Ryzen AI NPU detected! (Optimum Setup)\\x1b[0m\",\n );\n } else if (profile.hasAppleSilicon) {\n console.log(\n \" CPU Brand: \\x1b[32mmacOS Apple Silicon detected! (Optimum Setup)\\x1b[0m\",\n );\n } else {\n console.log(\" Hardware: \\x1b[33mStandard CPU detected.\\x1b[0m\");\n }\n\n const runnerLabel =\n profile.recommendedRunner === \"fastflowlm\"\n ? \"FastFlowLM (NPU Optimized)\"\n : \"Ollama\";\n console.log(\n `\\n \\x1b[1mRecommendation:\\x1b[0m ZAM suggests installing \\x1b[32m${runnerLabel}\\x1b[0m with \\x1b[36m${profile.recommendedModel}\\x1b[0m.`,\n );\n\n // ── STEP 3: Local LLM Runner Installation ───────────────────────────────\n console.log(\"\\n\\x1b[1m[3/5] Setting up Local LLM Runner\\x1b[0m\");\n const proceedInstall = await confirm({\n message: `Would you like ZAM to install and configure ${runnerLabel} automatically?`,\n default: true,\n });\n\n let installSuccess = false;\n if (proceedInstall) {\n let result: ReturnType<typeof installFastFlowLM> | undefined;\n if (profile.recommendedRunner === \"fastflowlm\") {\n result = installFastFlowLM();\n } else {\n result = installOllama();\n }\n\n if (result.success) {\n console.log(`\\x1b[32m✓ ${result.message}\\x1b[0m`);\n installSuccess = true;\n } else {\n console.warn(`\\x1b[33m⚠ Installation failed: ${result.message}\\x1b[0m`);\n console.log(\n \"You can install it manually or continue with offline templates.\",\n );\n }\n }\n\n // ── STEP 4: Initialize Database & Write Local Settings ─────────────────\n console.log(\"\\n\\x1b[1m[4/5] Bootstrapping database & settings...\\x1b[0m\");\n let db: ReturnType<typeof openDatabaseWithSync> | undefined;\n try {\n db = openDatabaseWithSync({ initialize: true });\n\n // Save workspace directory to settings\n setSetting(db, \"personal.workspace_dir\", workspacePath);\n\n // Auto-detect and save system locale\n const detectedLocale = detectSystemLocale();\n setSetting(db, \"system.locale\", detectedLocale);\n console.log(\n `\\x1b[32m✓ Detected and set system language to: ${detectedLocale}\\x1b[0m`,\n );\n\n if (installSuccess) {\n setSetting(db, \"llm.enabled\", \"true\");\n if (profile.recommendedRunner === \"fastflowlm\") {\n setSetting(db, \"llm.url\", \"http://localhost:8000/v1\");\n setSetting(db, \"llm.model\", \"gemma4-it:e4b\");\n } else {\n setSetting(db, \"llm.url\", \"http://localhost:11434/v1\");\n setSetting(db, \"llm.model\", \"llama3.2:3b\");\n }\n console.log(\n \"\\x1b[32m✓ Configured LLM runner settings in database.\\x1b[0m\",\n );\n } else {\n setSetting(db, \"llm.enabled\", \"false\");\n }\n db.close();\n console.log(\"\\x1b[32m✓ Database initialized successfully.\\x1b[0m\");\n } catch (err) {\n console.error(\n `\\x1b[31m✗ Database setup failed: ${(err as Error).message}\\x1b[0m`,\n );\n db?.close();\n }\n\n // ── STEP 5: Distribute Agent Skills & Hooks ──────────────────────────────\n console.log(\n \"\\n\\x1b[1m[5/5] Wiring Developer Agents & Terminal Hooks\\x1b[0m\",\n );\n const proceedHooks = await confirm({\n message:\n \"Distribute ZAM active-recall skills and enable automatic terminal command observation?\",\n default: true,\n });\n\n if (proceedHooks) {\n console.log(\"Copying SKILL.md into global agent directories...\");\n const skillResults = distributeGlobalSkills();\n for (const res of skillResults) {\n if (res.success) {\n console.log(` \\x1b[32m✓ Installed ZAM Skill in ${res.name}\\x1b[0m`);\n } else {\n console.log(` \\x1b[31m✗ Failed to install in ${res.name}\\x1b[0m`);\n }\n }\n\n console.log(\"Injecting observation hooks into shell profiles...\");\n const hookResults = injectShellHooks();\n for (const res of hookResults) {\n if (res.success) {\n const action = res.alreadyHooked\n ? \"already up-to-date\"\n : \"injected successfully\";\n console.log(` \\x1b[32m✓ ${res.shell} hook: ${action}\\x1b[0m`);\n } else {\n console.log(\n ` \\x1b[31m✗ Failed to inject hook in ${res.shell} (${res.file})\\x1b[0m`,\n );\n }\n }\n }\n\n printLine();\n console.log(\n \"\\x1b[1m\\x1b[32m Congratulations! ZAM Onboarding Complete!\\x1b[0m\",\n );\n console.log(\n \"\\x1b[1m You are now ready to start your symbiotic learning journey.\\x1b[0m\",\n );\n console.log(\"\\n \\x1b[1mUseful commands to get started:\\x1b[0m\");\n console.log(\n \" \\x1b[36mzam stats\\x1b[0m — Display your learning queue stats\",\n );\n console.log(\n \" \\x1b[36mzam learn\\x1b[0m — Start a standalone learning session\",\n );\n console.log(\n \" \\x1b[36mzam workspace publish\\x1b[0m — Publish your workspace sandbox to GitHub\",\n );\n printLine();\n });\n","/**\n * `zam learn` — Standalone, spoiler-free, in-process learning session.\n *\n * Slice 1 of the Session Harness (Increment 5), variant (a): no LLM.\n * Flow per card: show a concept-free cue → capture the learner's answer →\n * ONLY THEN reveal the stored answer (concept + context + resolved source_link)\n * → single self-rating 1–4 via the shared interactive action handler.\n *\n * Because the harness owns its own prompt and never prints the answer before\n * input is captured, no external autocomplete/ghost-text can spoil it, and\n * ratings/logs are written in-process (no per-subcommand permission prompts).\n */\n\nimport { input } from \"@inquirer/prompts\";\nimport { Command } from \"commander\";\nimport type { Database } from \"libsql\";\nimport type { BloomLevel, SupportedLocale } from \"../../kernel/index.js\";\nimport {\n buildReviewQueue,\n generatePrompt,\n getSetting,\n getTokenById,\n openDatabase,\n resolveReviewContext,\n t,\n} from \"../../kernel/index.js\";\nimport { formatHeader, formatReveal } from \"../learn-format.js\";\nimport {\n ensureHighQualityQuestion,\n ensureLocalLlmRunning,\n evaluateAnswerViaLLM,\n} from \"../llm/client.js\";\nimport { runInteractiveReviewAction } from \"../review-actions.js\";\nimport { resolveUser } from \"./resolve-user.js\";\n\n/** Words the learner can type at the answer prompt to end the session. */\nconst STOP_WORDS = new Set([\"q\", \":q\", \"quit\", \"stop\"]);\n\nfunction isExitPrompt(err: unknown): boolean {\n return err instanceof Error && err.name === \"ExitPromptError\";\n}\n\nexport const learnCommand = new Command(\"learn\")\n .description(\n \"Run a spoiler-free, in-process learning session (recall → reveal → self-rate)\",\n )\n .option(\"--user <id>\", \"User ID (default: whoami)\")\n .option(\"--max-new <n>\", \"Maximum new cards\", \"10\")\n .option(\"--max-reviews <n>\", \"Maximum review cards\", \"50\")\n .option(\"--no-resolve\", \"Skip resolving source_link into the revealed answer\")\n .action(async (opts) => {\n let db: Database | undefined;\n try {\n db = openDatabase();\n const userId = resolveUser(opts, db);\n\n const queue = buildReviewQueue(db, {\n userId,\n maxNew: Number(opts.maxNew),\n maxReviews: Number(opts.maxReviews),\n });\n\n const locale = (getSetting(db, \"system.locale\") ||\n \"en\") as SupportedLocale;\n\n if (queue.items.length === 0) {\n console.log(t(locale, \"nothing_due\"));\n db.close();\n return;\n }\n\n // Start the local LLM if needed and verify it is actually usable\n // (reachable AND serving the configured model). A wrong model name\n // otherwise looks like \"the AI is slow\" — instead we fall back cleanly.\n const llm = await ensureLocalLlmRunning(db);\n const isLlmEnabled = llm.usable;\n\n console.log(`\\n${t(locale, \"welcome\", { count: queue.items.length })}`);\n console.log(\n t(locale, \"new_review_relearn\", {\n newC: queue.newCount,\n reviewC: queue.reviewCount,\n relearnC: queue.relearnCount,\n }),\n );\n console.log(\n t(locale, \"domains\", { domains: queue.totalDomains.join(\", \") }),\n );\n\n // When the LLM is simply switched off, nudge the user; offline /\n // model-not-found already printed their own actionable message.\n if (!isLlmEnabled && llm.reason === \"disabled\") {\n console.log(t(locale, \"offline_warning\"));\n console.log(t(locale, \"offline_instruction\"));\n }\n\n console.log(t(locale, \"instruction\"));\n console.log(t(locale, \"quit_hint\"));\n\n let stoppedEarly = false;\n let maintenanceActions = 0;\n const results: Array<{ slug: string; rating: number }> = [];\n\n for (const [index, item] of queue.items.entries()) {\n // Dynamically generate a fresh, living active-recall question if LLM is enabled\n let resolvedQuestion = item.question;\n if (isLlmEnabled) {\n console.log(` \\x1b[2m${t(locale, \"generating_question\")}\\x1b[0m`);\n try {\n const healed = await ensureHighQualityQuestion(db, {\n id: item.tokenId,\n slug: item.slug,\n concept: item.concept,\n domain: item.domain,\n bloomLevel: item.bloomLevel as BloomLevel,\n sourceLink: item.sourceLink,\n question: item.question,\n });\n if (healed) {\n resolvedQuestion = healed;\n }\n } catch {\n // ignore and proceed\n }\n }\n\n const prompt = generatePrompt({\n cardId: item.cardId,\n tokenId: item.tokenId,\n slug: item.slug,\n concept: item.concept,\n domain: item.domain,\n bloomLevel: item.bloomLevel as BloomLevel,\n sourceLink: item.sourceLink,\n question: resolvedQuestion,\n });\n\n console.log(`\\n${\"─\".repeat(50)}`);\n console.log(\n `[${index + 1}/${queue.items.length}] ${formatHeader(item)}`,\n );\n\n console.log(`\\n ${prompt.question}`);\n\n // Capture the learner's answer FIRST — nothing is revealed yet.\n // Typing a stop word (or Ctrl+C) ends the session gracefully.\n let answer: string;\n try {\n answer = await input({\n message: t(locale, \"prompt_answer\"),\n });\n } catch (err) {\n if (isExitPrompt(err)) {\n stoppedEarly = true;\n console.log(\"\\nStopping session.\");\n break;\n }\n throw err;\n }\n\n if (STOP_WORDS.has(answer.trim().toLowerCase())) {\n stoppedEarly = true;\n console.log(\"Stopping session.\");\n break;\n }\n\n // Now reveal the stored answer.\n let resolved = null;\n if (opts.resolve !== false && item.sourceLink) {\n resolved = await resolveReviewContext(item.sourceLink).catch(\n () => null,\n );\n }\n const token = getTokenById(db, item.tokenId);\n\n // Perform LLM evaluation if enabled and there is a typed answer\n if (isLlmEnabled && answer.trim().length > 0) {\n console.log(`\\n ${t(locale, \"evaluating\")}`);\n try {\n const evaluation = await evaluateAnswerViaLLM(db, {\n slug: item.slug,\n concept: item.concept,\n domain: item.domain,\n bloomLevel: item.bloomLevel,\n context: token?.context,\n question: prompt.question,\n userAnswer: answer,\n sourceLinkContent: resolved?.content,\n });\n console.log(\n `\\n ${t(locale, \"feedback_title\", { line: \"─\".repeat(34) })}`,\n );\n for (const line of evaluation.split(\"\\n\")) {\n console.log(` ${line}`);\n }\n } catch (err) {\n console.warn(\n `\\n${t(locale, \"eval_skipped\", { reason: (err as Error).message })}`,\n );\n }\n }\n\n console.log(\n `\\n ${t(locale, \"answer_title\", { line: \"─\".repeat(38) })}`,\n );\n const reveal = formatReveal({\n slug: item.slug,\n concept: item.concept,\n context: token?.context,\n resolved,\n });\n for (const line of reveal.split(\"\\n\")) {\n console.log(` ${line}`);\n }\n console.log();\n\n let action: Awaited<ReturnType<typeof runInteractiveReviewAction>>;\n try {\n action = await runInteractiveReviewAction({\n db,\n userId,\n item,\n mode: \"review\",\n });\n } catch (err) {\n if (isExitPrompt(err)) {\n stoppedEarly = true;\n console.log(\"\\nStopping session.\");\n break;\n }\n throw err;\n }\n\n if (action.action === \"stop\") {\n stoppedEarly = true;\n console.log(\"\\nStopping session.\");\n break;\n }\n if (action.action === \"rate\") {\n results.push({ slug: item.slug, rating: action.rating! });\n } else if (action.action !== \"skip\") {\n maintenanceActions++;\n }\n }\n\n console.log(`\\n${\"═\".repeat(50)}`);\n console.log(\n stoppedEarly\n ? t(locale, \"session_ended\")\n : t(locale, \"session_complete\"),\n );\n console.log(t(locale, \"cards_rated\", { count: results.length }));\n if (maintenanceActions > 0) {\n console.log(` Maintenance actions: ${maintenanceActions}`);\n }\n if (results.length > 0) {\n const avg = results.reduce((s, r) => s + r.rating, 0) / results.length;\n console.log(t(locale, \"avg_rating\", { avg: avg.toFixed(1) }));\n const forgot = results.filter((r) => r.rating === 1).length;\n if (forgot > 0) {\n console.log(t(locale, \"forgot\", { count: forgot }));\n }\n }\n\n db.close();\n } catch (err) {\n db?.close();\n if ((err as Error).name === \"ExitPromptError\") {\n console.log(\"\\nLearning session cancelled.\");\n process.exit(0);\n }\n console.error(\"Error:\", (err as Error).message);\n process.exit(1);\n }\n });\n","/**\n * Pure formatting helpers for the spoiler-free learning session (`zam learn`).\n *\n * Kept free of terminal/prompt code so the presentation logic is unit-testable.\n * The up-front question is the kernel's Bloom-templated prompt (built in\n * learn.ts via generatePrompt); these helpers render the header line and the\n * answer that is revealed only AFTER the learner has committed an answer.\n */\n\nimport type { ReviewContext } from \"../kernel/index.js\";\n\nexport const BLOOM_VERBS: Record<number, string> = {\n 1: \"Remember\",\n 2: \"Understand\",\n 3: \"Apply\",\n 4: \"Analyze\",\n 5: \"Synthesize\",\n};\n\nfunction clampBloom(n: number): number {\n return Number.isFinite(n) && n >= 1 && n <= 5 ? Math.trunc(n) : 1;\n}\n\n/**\n * Build the card header, e.g. \"Understand (Bloom 2) · web\".\n */\nexport function formatHeader(input: {\n bloomLevel: number;\n domain: string;\n}): string {\n const lvl = clampBloom(input.bloomLevel);\n const parts = [`${BLOOM_VERBS[lvl]} (Bloom ${lvl})`];\n if (input.domain?.trim()) {\n parts.push(input.domain.trim());\n }\n return parts.join(\" · \");\n}\n\nexport interface RevealInput {\n slug: string;\n concept: string;\n context?: string | null;\n resolved?: ReviewContext | null;\n}\n\n/**\n * Format the stored answer revealed AFTER the learner has committed: the\n * token slug, concept, optional context, and resolved source_link content.\n */\nexport function formatReveal(input: RevealInput): string {\n const lines: string[] = [\n `Token: #${input.slug}`,\n `Concept: ${input.concept}`,\n ];\n\n if (input.context?.trim()) {\n lines.push(\"\", `Context: ${input.context.trim()}`);\n }\n\n const resolved = input.resolved;\n if (resolved?.content.trim()) {\n lines.push(\"\");\n if (resolved.sourceType === \"dynamic_search\") {\n // content is a QUERY_DIRECTIVE — there is nothing to quote inline\n lines.push(`Source: ${resolved.content}`);\n } else {\n const label = resolved.url ?? resolved.filePath ?? resolved.sourceLink;\n lines.push(`Source (${resolved.sourceType}: ${label}):`);\n for (const line of resolved.content.trimEnd().split(\"\\n\")) {\n lines.push(` │ ${line}`);\n }\n if (resolved.truncated) {\n lines.push(\" │ … (truncated)\");\n }\n }\n }\n\n return lines.join(\"\\n\");\n}\n","import { confirm, input, select } from \"@inquirer/prompts\";\nimport type { Database } from \"libsql\";\nimport type {\n BloomLevel,\n Rating,\n ReviewActionResult,\n ReviewQueueItem,\n SymbiosisMode,\n Token,\n UpdateTokenInput,\n} from \"../kernel/index.js\";\nimport {\n executeReviewAction,\n generatePrompt,\n getCardDeletionImpact,\n getTokenById,\n getTokenDeleteImpact,\n} from \"../kernel/index.js\";\n\ntype InteractiveTerminalAction =\n | \"rate\"\n | \"skip\"\n | \"deprecate-token\"\n | \"delete-token\"\n | \"delete-card\"\n | \"stop\";\n\nexport interface RunInteractiveReviewActionInput {\n db: Database;\n userId: string;\n item: ReviewQueueItem;\n mode: \"review\" | \"session\";\n}\n\nexport interface RunInteractiveReviewActionResult {\n action: InteractiveTerminalAction;\n result: ReviewActionResult;\n rating?: Rating;\n}\n\nexport async function runInteractiveReviewAction(\n inputData: RunInteractiveReviewActionInput,\n): Promise<RunInteractiveReviewActionResult> {\n let currentItem = { ...inputData.item };\n\n while (true) {\n const choice = (await select({\n message: \"What next?\",\n choices: [\n { name: \"1 - Again (forgot)\", value: 1 },\n { name: \"2 - Hard\", value: 2 },\n { name: \"3 - Good\", value: 3 },\n { name: \"4 - Easy\", value: 4 },\n { name: \"Skip this card\", value: \"skip\" },\n { name: \"Edit token\", value: \"edit-token\" },\n { name: \"Deprecate token\", value: \"deprecate-token\" },\n { name: \"Delete token\", value: \"delete-token\" },\n { name: \"Delete my card\", value: \"delete-card\" },\n {\n name:\n inputData.mode === \"session\"\n ? \"Stop review and continue to task selection\"\n : \"Stop review\",\n value: \"stop\",\n },\n ],\n })) as Rating | InteractiveTerminalAction | \"edit-token\";\n\n if (typeof choice === \"number\") {\n const result = executeReviewAction(inputData.db, {\n action: \"rate\",\n cardId: currentItem.cardId,\n userId: inputData.userId,\n rating: choice,\n });\n\n const ratingLabels: Record<number, string> = {\n 1: \"Again\",\n 2: \"Hard\",\n 3: \"Good\",\n 4: \"Easy\",\n };\n console.log(\n ` ${ratingLabels[choice]} — next due: ${result.evaluation?.nextDueAt}`,\n );\n\n if (result.blocked) {\n console.log(\n ` Blocked ${result.blocked.blockedSlug}. Review these prerequisites:`,\n );\n for (const prereq of result.blocked.prerequisites) {\n console.log(` - ${prereq.slug}: ${prereq.concept}`);\n }\n }\n\n console.log();\n return { action: \"rate\", result, rating: choice };\n }\n\n if (choice === \"skip\") {\n console.log(\"Skipped this card.\\n\");\n return {\n action: \"skip\",\n result: executeReviewAction(inputData.db, {\n action: \"skip\",\n cardId: currentItem.cardId,\n userId: inputData.userId,\n }),\n };\n }\n\n if (choice === \"stop\") {\n return {\n action: \"stop\",\n result: executeReviewAction(inputData.db, {\n action: \"stop\",\n cardId: currentItem.cardId,\n userId: inputData.userId,\n }),\n };\n }\n\n if (choice === \"edit-token\") {\n const token = getTokenById(inputData.db, currentItem.tokenId);\n if (!token) {\n throw new Error(`Token not found: ${currentItem.tokenId}`);\n }\n\n const updates = await promptTokenEdit(token);\n if (!updates) {\n console.log(\"No token changes made.\\n\");\n continue;\n }\n\n const result = executeReviewAction(inputData.db, {\n action: \"edit-token\",\n cardId: currentItem.cardId,\n userId: inputData.userId,\n tokenUpdates: updates,\n });\n\n const updatedToken = result.updatedToken!;\n currentItem = {\n ...currentItem,\n slug: updatedToken.slug,\n concept: updatedToken.concept,\n domain: updatedToken.domain,\n bloomLevel: updatedToken.bloom_level,\n };\n\n const refreshedPrompt = generatePrompt({\n cardId: currentItem.cardId,\n tokenId: currentItem.tokenId,\n slug: currentItem.slug,\n concept: currentItem.concept,\n domain: currentItem.domain,\n bloomLevel: currentItem.bloomLevel as BloomLevel,\n });\n\n console.log(`Updated token: ${updatedToken.slug}`);\n console.log(` Concept: ${updatedToken.concept}`);\n console.log(` Domain: ${updatedToken.domain || \"(none)\"}`);\n console.log(` Bloom: ${updatedToken.bloom_level}`);\n if (updatedToken.source_link) {\n console.log(` Source: ${updatedToken.source_link}`);\n }\n console.log(`\\n ${refreshedPrompt.question}\\n`);\n continue;\n }\n\n if (choice === \"deprecate-token\") {\n const approved = await confirm({\n message: `Deprecate ${currentItem.slug} so it stops appearing in review queues?`,\n default: false,\n });\n if (!approved) {\n console.log(\"Token deprecation cancelled.\\n\");\n continue;\n }\n\n const result = executeReviewAction(inputData.db, {\n action: \"deprecate-token\",\n cardId: currentItem.cardId,\n userId: inputData.userId,\n });\n\n console.log(`Deprecated token: ${currentItem.slug}\\n`);\n return { action: \"deprecate-token\", result };\n }\n\n if (choice === \"delete-token\") {\n const impact = getTokenDeleteImpact(inputData.db, currentItem.slug);\n console.log(`Delete token ${currentItem.slug}?`);\n console.log(` Cards: ${impact.cards}`);\n console.log(` Review logs: ${impact.review_logs}`);\n console.log(\n ` Prereq edges from it: ${impact.prerequisite_edges_from_token}`,\n );\n console.log(\n ` Prereq edges to it: ${impact.prerequisite_edges_to_token}`,\n );\n console.log(` Session steps: ${impact.session_steps}`);\n console.log(` Sessions touched: ${impact.sessions_touched}`);\n console.log(` Agent skills updated: ${impact.agent_skills}`);\n\n const approved = await confirm({\n message:\n \"Permanently delete this token and its dependent learning data?\",\n default: false,\n });\n if (!approved) {\n console.log(\"Token deletion cancelled.\\n\");\n continue;\n }\n\n const result = executeReviewAction(inputData.db, {\n action: \"delete-token\",\n cardId: currentItem.cardId,\n userId: inputData.userId,\n });\n\n console.log(`Deleted token: ${currentItem.slug}\\n`);\n return { action: \"delete-token\", result };\n }\n\n if (choice === \"delete-card\") {\n const impact = getCardDeletionImpact(\n inputData.db,\n currentItem.tokenId,\n inputData.userId,\n );\n console.log(`Delete your card for ${currentItem.slug}?`);\n console.log(` Review logs removed: ${impact.review_logs}`);\n\n const approved = await confirm({\n message: \"Delete only your card for this token?\",\n default: false,\n });\n if (!approved) {\n console.log(\"Card deletion cancelled.\\n\");\n continue;\n }\n\n const result = executeReviewAction(inputData.db, {\n action: \"delete-card\",\n cardId: currentItem.cardId,\n userId: inputData.userId,\n });\n\n console.log(`Deleted your card for: ${currentItem.slug}\\n`);\n return { action: \"delete-card\", result };\n }\n }\n}\n\nasync function promptTokenEdit(token: Token): Promise<UpdateTokenInput | null> {\n const field = (await select({\n message: `Edit which field on ${token.slug}?`,\n choices: [\n { name: \"Concept\", value: \"concept\" },\n { name: \"Domain\", value: \"domain\" },\n { name: \"Bloom level\", value: \"bloom_level\" },\n { name: \"Context\", value: \"context\" },\n { name: \"Symbiosis mode\", value: \"symbiosis_mode\" },\n { name: \"Source link\", value: \"source_link\" },\n { name: \"Cancel\", value: \"cancel\" },\n ],\n })) as keyof UpdateTokenInput | \"cancel\";\n\n if (field === \"cancel\") {\n return null;\n }\n\n switch (field) {\n case \"concept\": {\n const concept = await input({\n message: \"Concept:\",\n default: token.concept,\n });\n return concept === token.concept ? null : { concept };\n }\n\n case \"domain\": {\n const domain = await input({\n message: \"Domain (blank allowed):\",\n default: token.domain,\n });\n return domain === token.domain ? null : { domain };\n }\n\n case \"context\": {\n const context = await input({\n message: \"Context (blank allowed):\",\n default: token.context,\n });\n return context === token.context ? null : { context };\n }\n\n case \"bloom_level\": {\n const bloom = (await select({\n message: \"Bloom level:\",\n choices: [1, 2, 3, 4, 5].map((value) => ({\n name:\n value === token.bloom_level ? `${value} (current)` : String(value),\n value,\n })),\n })) as BloomLevel;\n return bloom === token.bloom_level ? null : { bloom_level: bloom };\n }\n\n case \"symbiosis_mode\": {\n const mode = (await select({\n message: \"Symbiosis mode:\",\n choices: [\n {\n name: token.symbiosis_mode === null ? \"none (current)\" : \"none\",\n value: null,\n },\n {\n name:\n token.symbiosis_mode === \"shadowing\"\n ? \"shadowing (current)\"\n : \"shadowing\",\n value: \"shadowing\",\n },\n {\n name:\n token.symbiosis_mode === \"copilot\"\n ? \"copilot (current)\"\n : \"copilot\",\n value: \"copilot\",\n },\n {\n name:\n token.symbiosis_mode === \"autonomy\"\n ? \"autonomy (current)\"\n : \"autonomy\",\n value: \"autonomy\",\n },\n ],\n })) as SymbiosisMode | null;\n return mode === token.symbiosis_mode ? null : { symbiosis_mode: mode };\n }\n\n case \"source_link\": {\n const link = await input({\n message: \"Source link (blank allowed):\",\n default: token.source_link ?? \"\",\n });\n return link === token.source_link ? null : { source_link: link || null };\n }\n }\n}\n","/**\n * `zam monitor` — Shell observation for real-time task monitoring.\n *\n * Installs shell hooks (zsh/bash/PowerShell) that capture commands with timing,\n * exit codes, and working directory to a JSONL file. The agent reads\n * this log to infer ratings for knowledge tokens.\n *\n * Usage:\n * eval \"$(zam monitor start --session <id>)\" # zsh/bash\n * Invoke-Expression (& zam monitor start --session <id>) # PowerShell\n * zam monitor status --session <id> # check log stats\n */\n\nimport { execFileSync, execSync } from \"node:child_process\";\nimport { unlinkSync, writeFileSync } from \"node:fs\";\nimport { tmpdir } from \"node:os\";\nimport { basename, join } from \"node:path\";\nimport { Command } from \"commander\";\nimport type { Database } from \"libsql\";\nimport type { MonitorEvent } from \"../../kernel/index.js\";\nimport {\n ensureMonitorDir,\n generateBashHooks,\n generateBashUnhooks,\n generatePowerShellHooks,\n generatePowerShellUnhooks,\n generateZshHooks,\n generateZshUnhooks,\n getMonitorLogStats,\n getMonitorPath,\n getSetting,\n monitorLogExists,\n openDatabase,\n pairCommands,\n readMonitorLog,\n setSetting,\n writeMonitorEvent,\n} from \"../../kernel/index.js\";\n\ntype MonitorShell = \"zsh\" | \"bash\" | \"pwsh\" | \"powershell\";\n\nfunction isPowerShellShell(shell: MonitorShell): boolean {\n return shell === \"pwsh\" || shell === \"powershell\";\n}\n\nfunction normalizeShell(shell: string | undefined): MonitorShell {\n if (!shell) return detectShell();\n const normalized = shell.toLowerCase();\n if (\n normalized === \"zsh\" ||\n normalized === \"bash\" ||\n normalized === \"pwsh\" ||\n normalized === \"powershell\"\n ) {\n return normalized;\n }\n throw new Error(\n `Unsupported shell: ${shell}. Expected zsh, bash, pwsh, or powershell.`,\n );\n}\n\nfunction psSingleQuoted(value: string): string {\n return `'${value.replace(/'/g, \"''\")}'`;\n}\n\nfunction detectShell(): MonitorShell {\n if (process.platform === \"win32\")\n return findExecutable(\"pwsh.exe\") ? \"pwsh\" : \"powershell\";\n const shell = process.env.SHELL ?? \"\";\n return basename(shell) === \"bash\" ? \"bash\" : \"zsh\";\n}\n\nexport const monitorCommand = new Command(\"monitor\").description(\n \"Shell observation for real-time task monitoring\",\n);\n\n// ── zam monitor start ─────────────────────────────────────────────────────\n\nmonitorCommand\n .command(\"start\")\n .description(\"Output shell hook code to install monitoring\")\n .requiredOption(\"--session <id>\", \"Session ID to monitor\")\n .option(\n \"--shell <type>\",\n \"Shell type: zsh | bash | pwsh | powershell (auto-detected)\",\n )\n .action((opts) => {\n let shell: MonitorShell;\n try {\n shell = normalizeShell(opts.shell);\n } catch (err) {\n console.error(`# Error: ${(err as Error).message}`);\n process.exit(1);\n }\n\n // Validate session exists\n let db: Database | undefined;\n try {\n db = openDatabase();\n const session = db\n .prepare(\"SELECT id, completed_at FROM sessions WHERE id = ?\")\n .get(opts.session) as\n | { id: string; completed_at: string | null }\n | undefined;\n\n if (!session) {\n console.error(`# Error: Session not found: ${opts.session}`);\n process.exit(1);\n }\n if (session.completed_at) {\n console.error(`# Error: Session already completed: ${opts.session}`);\n process.exit(1);\n }\n } catch (err) {\n console.error(`# Error: ${(err as Error).message}`);\n process.exit(1);\n } finally {\n db?.close();\n }\n\n ensureMonitorDir();\n const monitorFile = getMonitorPath(opts.session);\n\n // Write initial meta event\n const meta: MonitorEvent = {\n type: \"monitor_meta\",\n ts: new Date().toISOString(),\n event: \"start\",\n session_id: opts.session,\n shell,\n pid: process.pid,\n };\n writeMonitorEvent(opts.session, meta);\n\n // Output hook code to stdout\n if (shell === \"bash\") {\n console.log(generateBashHooks(monitorFile, opts.session));\n } else if (isPowerShellShell(shell)) {\n console.log(generatePowerShellHooks(monitorFile, opts.session));\n } else {\n console.log(generateZshHooks(monitorFile, opts.session));\n }\n });\n\n// ── zam monitor stop ──────────────────────────────────────────────────────\n\nmonitorCommand\n .command(\"stop\")\n .description(\"Output shell code to remove monitoring hooks\")\n .requiredOption(\"--session <id>\", \"Session ID\")\n .option(\n \"--shell <type>\",\n \"Shell type: zsh | bash | pwsh | powershell (auto-detected)\",\n )\n .action((opts) => {\n // Write stop meta event\n if (monitorLogExists(opts.session)) {\n const meta: MonitorEvent = {\n type: \"monitor_meta\",\n ts: new Date().toISOString(),\n event: \"stop\",\n session_id: opts.session,\n };\n writeMonitorEvent(opts.session, meta);\n }\n\n let shell: MonitorShell;\n try {\n shell = normalizeShell(opts.shell);\n } catch (err) {\n console.error(`# Error: ${(err as Error).message}`);\n process.exit(1);\n }\n\n if (shell === \"bash\") {\n console.log(generateBashUnhooks());\n } else if (isPowerShellShell(shell)) {\n console.log(generatePowerShellUnhooks());\n } else {\n console.log(generateZshUnhooks());\n }\n });\n\n// ── zam monitor status ────────────────────────────────────────────────────\n\nmonitorCommand\n .command(\"status\")\n .description(\"Show monitoring status for a session\")\n .requiredOption(\"--session <id>\", \"Session ID\")\n .option(\"--json\", \"Output as JSON\")\n .action((opts) => {\n const stats = getMonitorLogStats(opts.session);\n\n if (!stats.exists) {\n if (opts.json) {\n console.log(JSON.stringify({ exists: false }));\n } else {\n console.log(`No monitor log found for session ${opts.session}`);\n }\n return;\n }\n\n const events = readMonitorLog(opts.session);\n const commands = pairCommands(events);\n const errors = commands.filter(\n (c) => c.exitCode != null && c.exitCode !== 0,\n ).length;\n\n const meta = events.find(\n (e) => e.type === \"monitor_meta\" && e.event === \"start\",\n );\n const stopped = events.some(\n (e) => e.type === \"monitor_meta\" && e.event === \"stop\",\n );\n\n const result = {\n sessionId: opts.session,\n exists: true,\n active: !stopped,\n shell: meta?.shell ?? \"unknown\",\n totalCommands: commands.length,\n errors,\n sizeBytes: stats.sizeBytes,\n timeSpan:\n commands.length > 0\n ? {\n start: commands[0].startedAt,\n end:\n commands[commands.length - 1].endedAt ??\n commands[commands.length - 1].startedAt,\n }\n : null,\n };\n\n if (opts.json) {\n console.log(JSON.stringify(result, null, 2));\n return;\n }\n\n console.log(`Monitor: ${opts.session}`);\n console.log(` Status: ${result.active ? \"active\" : \"stopped\"}`);\n console.log(` Shell: ${result.shell}`);\n console.log(` Commands: ${result.totalCommands}`);\n console.log(` Errors: ${result.errors}`);\n if (result.timeSpan) {\n console.log(` From: ${result.timeSpan.start}`);\n console.log(` To: ${result.timeSpan.end}`);\n }\n });\n\n// ── zam monitor open ─────────────────────────────────────────────────────\n\n/**\n * Resolve the `zam` invocation — built CLI if available, otherwise tsx source.\n * This ensures the spawned terminal uses the correct entrypoint.\n */\nfunction findExecutable(command: string): string | null {\n try {\n const lookup =\n process.platform === \"win32\"\n ? `where.exe ${command}`\n : `command -v ${command}`;\n const result = execSync(lookup, { encoding: \"utf-8\" })\n .split(/\\r?\\n/)[0]\n ?.trim();\n return result || null;\n } catch {\n return null;\n }\n}\n\nfunction resolveZamInvocation(shell: MonitorShell): string {\n const installed = findExecutable(\"zam\");\n if (installed) {\n return isPowerShellShell(shell)\n ? `& ${psSingleQuoted(installed)}`\n : installed;\n }\n\n const projectRoot = join(import.meta.dirname, \"..\", \"..\", \"..\");\n const cliSource = join(projectRoot, \"src/cli/index.ts\");\n if (isPowerShellShell(shell)) {\n return `& npx --prefix ${psSingleQuoted(projectRoot)} tsx ${psSingleQuoted(cliSource)}`;\n }\n return `npx --prefix ${JSON.stringify(projectRoot)} tsx ${JSON.stringify(cliSource)}`;\n}\n\nfunction buildMonitorSetupCommand(\n dir: string,\n sessionId: string,\n shell: MonitorShell,\n): string {\n const zamInvocation = resolveZamInvocation(shell);\n if (isPowerShellShell(shell)) {\n return [\n `Set-Location -LiteralPath ${psSingleQuoted(dir)}`,\n `$__zamHook = ${zamInvocation} monitor start --session ${psSingleQuoted(sessionId)} --shell ${shell}`,\n \"Invoke-Expression ($__zamHook -join [Environment]::NewLine)\",\n \"Remove-Variable __zamHook\",\n ].join(\"; \");\n }\n\n return `cd ${JSON.stringify(dir)} && eval \"$(${zamInvocation} monitor start --session ${sessionId} --shell ${shell})\"`;\n}\n\n/**\n * Detect whether iTerm2 is running (preferred on macOS).\n */\nfunction isItermRunning(): boolean {\n try {\n const result = execSync(\n 'osascript -e \\'tell application \"System Events\" to (name of processes) contains \"iTerm2\"\\' 2>/dev/null',\n { encoding: \"utf-8\" },\n ).trim();\n return result === \"true\";\n } catch {\n return false;\n }\n}\n\nmonitorCommand\n .command(\"open\")\n .description(\"Open a new monitored terminal window for a session\")\n .requiredOption(\"--session <id>\", \"Session ID to monitor\")\n .option(\"--dir <path>\", \"Working directory (defaults to cwd)\")\n .option(\n \"--shell <type>\",\n \"Shell type: zsh | bash | pwsh | powershell (auto-detected)\",\n )\n .action((opts) => {\n let shell: MonitorShell;\n try {\n shell = normalizeShell(opts.shell);\n } catch (err) {\n console.error(`Error: ${(err as Error).message}`);\n process.exit(1);\n }\n\n // Validate session exists\n let db: Database | undefined;\n try {\n db = openDatabase();\n const session = db\n .prepare(\"SELECT id, completed_at FROM sessions WHERE id = ?\")\n .get(opts.session) as\n | { id: string; completed_at: string | null }\n | undefined;\n\n if (!session) {\n console.error(`Error: Session not found: ${opts.session}`);\n process.exit(1);\n }\n if (session.completed_at) {\n console.error(`Error: Session already completed: ${opts.session}`);\n process.exit(1);\n }\n\n // Save monitor preference so the agent knows to default to terminal\n if (!getSetting(db, \"monitor_method\")) {\n setSetting(db, \"monitor_method\", \"terminal\");\n }\n } catch (err) {\n console.error(`Error: ${(err as Error).message}`);\n process.exit(1);\n } finally {\n db?.close();\n }\n\n const dir = opts.dir ?? process.cwd();\n const shellSetup = buildMonitorSetupCommand(dir, opts.session, shell);\n\n if (process.platform === \"darwin\" && !isPowerShellShell(shell)) {\n openMacTerminal(shellSetup, opts.session, dir);\n } else if (process.platform === \"win32\" && isPowerShellShell(shell)) {\n openWindowsPowerShell(shellSetup, opts.session, dir, shell);\n } else {\n console.log(`Run this in a new terminal:\\n`);\n console.log(` ${shellSetup}\\n`);\n console.log(\n `(Automatic terminal opening is only supported on macOS Terminal/iTerm2 and Windows PowerShell for now.)`,\n );\n }\n });\n\n/**\n * Open a macOS terminal window via AppleScript.\n * Uses a temp .scpt file to avoid shell quoting hell.\n */\nfunction openMacTerminal(\n shellSetup: string,\n sessionId: string,\n dir: string,\n): void {\n const useIterm = isItermRunning();\n const escaped = shellSetup.replace(/\\\\/g, \"\\\\\\\\\").replace(/\"/g, '\\\\\"');\n\n const appleScript = useIterm\n ? `tell application \"iTerm2\"\n activate\n set newWindow to (create window with default profile)\n tell current session of newWindow\n write text \"${escaped}\"\n end tell\nend tell`\n : `tell application \"Terminal\"\n activate\n do script \"${escaped}\"\nend tell`;\n\n const tmpFile = join(tmpdir(), `zam-monitor-${sessionId}.scpt`);\n try {\n writeFileSync(tmpFile, appleScript);\n execSync(`osascript ${JSON.stringify(tmpFile)}`, { stdio: \"ignore\" });\n console.log(\n `Opened ${useIterm ? \"iTerm2\" : \"Terminal.app\"} window with monitoring for session ${sessionId}`,\n );\n console.log(` Directory: ${dir}`);\n } catch (err) {\n console.error(`Failed to open terminal: ${(err as Error).message}`);\n console.log(`\\nRun this manually in a new terminal:\\n`);\n console.log(` ${shellSetup}`);\n } finally {\n try {\n unlinkSync(tmpFile);\n } catch {\n /* ignore */\n }\n }\n}\n\n/**\n * Open a Windows PowerShell/pwsh window with monitoring installed.\n */\nfunction openWindowsPowerShell(\n shellSetup: string,\n sessionId: string,\n dir: string,\n requestedShell: MonitorShell,\n): void {\n const requestedExecutable =\n requestedShell === \"powershell\" ? \"powershell.exe\" : \"pwsh.exe\";\n const executable = findExecutable(requestedExecutable)\n ? requestedExecutable\n : \"powershell.exe\";\n const startCommand = [\n \"Start-Process\",\n `-FilePath ${psSingleQuoted(executable)}`,\n `-ArgumentList @('-NoExit','-NoProfile','-Command',${psSingleQuoted(shellSetup)})`,\n ].join(\" \");\n\n try {\n execFileSync(\n \"powershell.exe\",\n [\"-NoProfile\", \"-ExecutionPolicy\", \"Bypass\", \"-Command\", startCommand],\n {\n stdio: \"ignore\",\n },\n );\n console.log(\n `Opened ${executable === \"pwsh.exe\" ? \"PowerShell\" : \"Windows PowerShell\"} window with monitoring for session ${sessionId}`,\n );\n console.log(` Directory: ${dir}`);\n } catch (err) {\n console.error(`Failed to open PowerShell: ${(err as Error).message}`);\n console.log(`\\nRun this manually in a new PowerShell terminal:\\n`);\n console.log(` ${shellSetup}`);\n }\n}\n","/**\n * `zam review` — Interactive review session.\n */\n\nimport { Command } from \"commander\";\nimport type { Database } from \"libsql\";\nimport type { BloomLevel } from \"../../kernel/index.js\";\nimport {\n buildReviewQueue,\n generatePrompt,\n openDatabase,\n resolveReviewContext,\n} from \"../../kernel/index.js\";\nimport { runInteractiveReviewAction } from \"../review-actions.js\";\nimport { resolveUser } from \"./resolve-user.js\";\n\nexport const reviewCommand = new Command(\"review\")\n .description(\"Start an interactive review session\")\n .option(\"--user <id>\", \"User ID (default: whoami)\")\n .option(\"--max-new <n>\", \"Maximum new cards\", \"10\")\n .option(\"--max-reviews <n>\", \"Maximum review cards\", \"50\")\n .option(\"--no-resolve\", \"Skip resolving source_link into inline context\")\n .action(async (opts) => {\n let db: Database | undefined;\n try {\n db = openDatabase();\n const userId = resolveUser(opts, db);\n\n const queue = buildReviewQueue(db, {\n userId,\n maxNew: Number(opts.maxNew),\n maxReviews: Number(opts.maxReviews),\n });\n\n if (queue.items.length === 0) {\n console.log(\"No cards due for review. You're all caught up!\");\n db.close();\n return;\n }\n\n console.log(`\\nReview session: ${queue.items.length} card(s)`);\n console.log(\n ` New: ${queue.newCount} Review: ${queue.reviewCount} Relearn: ${queue.relearnCount}`,\n );\n console.log(` Domains: ${queue.totalDomains.join(\", \")}`);\n console.log();\n\n const _completed = 0;\n let stoppedEarly = false;\n let maintenanceActions = 0;\n const results: Array<{\n slug: string;\n rating: number;\n nextDue: string;\n }> = [];\n\n for (const [index, item] of queue.items.entries()) {\n const prompt = generatePrompt({\n cardId: item.cardId,\n tokenId: item.tokenId,\n slug: item.slug,\n concept: item.concept,\n domain: item.domain,\n bloomLevel: item.bloomLevel as BloomLevel,\n sourceLink: item.sourceLink,\n });\n\n console.log(\n `\\n[${index + 1}/${queue.items.length}] ${prompt.bloomVerb} (Bloom ${prompt.bloomLevel})`,\n );\n console.log(`Domain: ${prompt.domain || \"(none)\"}`);\n if (prompt.sourceLink) {\n console.log(`Source: ${prompt.sourceLink}`);\n if (opts.resolve !== false) {\n const ctx = await resolveReviewContext(item.sourceLink, {\n maxChars: 1200,\n }).catch(() => null);\n if (ctx && ctx.sourceType === \"dynamic_search\") {\n console.log(` ↳ ${ctx.content}`);\n } else if (ctx?.content.trim()) {\n const indented = ctx.content\n .trimEnd()\n .split(\"\\n\")\n .map((line) => ` │ ${line}`)\n .join(\"\\n\");\n console.log(\" Context:\");\n console.log(indented);\n if (ctx.truncated) {\n console.log(\" │ … (truncated)\");\n }\n }\n }\n }\n console.log(`\\n ${prompt.question}\\n`);\n\n const action = await runInteractiveReviewAction({\n db,\n userId,\n item,\n mode: \"review\",\n });\n\n if (action.action === \"stop\") {\n stoppedEarly = true;\n console.log(\"\\nStopping review.\");\n break;\n }\n\n if (action.action === \"rate\") {\n results.push({\n slug: item.slug,\n rating: action.rating!,\n nextDue: action.result.evaluation?.nextDueAt,\n });\n } else if (action.action !== \"skip\") {\n maintenanceActions++;\n }\n }\n\n // Session summary\n console.log(`\\n${\"═\".repeat(50)}`);\n console.log(\n stoppedEarly ? \"Review session ended.\" : \"Review session complete!\",\n );\n console.log(` Cards rated: ${results.length}`);\n if (maintenanceActions > 0) {\n console.log(` Maintenance actions: ${maintenanceActions}`);\n }\n\n if (results.length > 0) {\n const avgRating =\n results.reduce((s, r) => s + r.rating, 0) / results.length;\n console.log(` Average rating: ${avgRating.toFixed(1)}`);\n\n const forgot = results.filter((r) => r.rating === 1).length;\n if (forgot > 0) {\n console.log(` Forgot: ${forgot} card(s)`);\n }\n }\n\n db.close();\n } catch (err) {\n db?.close();\n // User cancelled with Ctrl+C — exit gracefully\n if ((err as Error).name === \"ExitPromptError\") {\n console.log(\"\\nReview session cancelled.\");\n process.exit(0);\n }\n console.error(\"Error:\", (err as Error).message);\n process.exit(1);\n }\n });\n","/**\n * `zam session` — Session management subcommand group.\n *\n * Session start follows the two-phase flow from Increment 2:\n * Phase 1 — Repetition: review due cards (pure recall first, skippable)\n * Phase 2 — Task execution: pick a work item from ADO or enter a custom task\n */\n\nimport { input, select } from \"@inquirer/prompts\";\nimport { Command } from \"commander\";\nimport type { Database } from \"libsql\";\nimport type { BloomLevel, ExecutionContext } from \"../../kernel/index.js\";\nimport {\n buildReviewQueue,\n endSession,\n fetchActiveWorkItems,\n generatePrompt,\n getSessionSummary,\n getTokenBySlug,\n loadADOConfig,\n logStep,\n openDatabase,\n startSession,\n} from \"../../kernel/index.js\";\nimport { runInteractiveReviewAction } from \"../review-actions.js\";\nimport { resolveUser } from \"./resolve-user.js\";\n\nfunction withDb(fn: (db: Database) => void): void {\n let db: Database | undefined;\n try {\n db = openDatabase();\n fn(db);\n } catch (err) {\n console.error(\"Error:\", (err as Error).message);\n process.exit(1);\n } finally {\n db?.close();\n }\n}\n\nexport const sessionCommand = new Command(\"session\").description(\n \"Manage learning sessions\",\n);\n\n// ── zam session start ─────────────────────────────────────────────────────\n\nsessionCommand\n .command(\"start\")\n .description(\"Start a new learning session (review → task)\")\n .option(\"--user <id>\", \"User ID (default: whoami)\")\n .option(\"--task <description>\", \"Task description (interactive if omitted)\")\n .option(\n \"--context <level>\",\n \"Execution context: shell | ui | reallife (default: shell)\",\n \"shell\",\n )\n .option(\n \"--skip-review\",\n \"Skip the repetition phase and go straight to task selection\",\n )\n .option(\n \"--review-minutes <n>\",\n \"Maximum minutes for the repetition phase (default: 20)\",\n \"20\",\n )\n .option(\"--json\", \"Output as JSON\")\n .option(\"--quiet\", \"Output only the session ID\")\n .action(async (opts) => {\n let db: Database | undefined;\n try {\n db = openDatabase();\n\n const validContexts = [\"shell\", \"ui\", \"reallife\"];\n if (!validContexts.includes(opts.context)) {\n console.error(\n `Invalid context: ${opts.context}. Must be one of: ${validContexts.join(\", \")}`,\n );\n process.exit(1);\n }\n\n const userId = resolveUser(opts, db);\n const reviewMinutes = Number(opts.reviewMinutes);\n\n // ── Phase 1: Repetition ────────────────────────────────────────────\n if (!opts.skipReview && !opts.quiet && !opts.json) {\n const reviewResults = await runRepetitionPhase(\n db,\n userId,\n reviewMinutes,\n );\n if (reviewResults.reviewed > 0) {\n console.log();\n }\n }\n\n // ── Phase 2: Task Selection ────────────────────────────────────────\n let task: string = opts.task;\n\n if (!task && !opts.quiet && !opts.json) {\n task = await selectTask(db);\n }\n\n if (!task) {\n // Fallback for --quiet/--json without --task\n console.error(\n \"Task description is required. Use --task or run interactively.\",\n );\n process.exit(1);\n }\n\n const session = startSession(db, {\n user_id: userId,\n task,\n execution_context: opts.context as ExecutionContext,\n });\n\n db.close();\n\n if (opts.quiet) {\n console.log(session.id);\n } else if (opts.json) {\n console.log(JSON.stringify(session, null, 2));\n } else {\n console.log(`\\nSession started: ${session.id}`);\n console.log(` User: ${session.user_id}`);\n console.log(` Task: ${session.task}`);\n console.log(` Context: ${session.execution_context}`);\n console.log(` Started: ${session.started_at}`);\n }\n } catch (err) {\n db?.close();\n if ((err as Error).name === \"ExitPromptError\") {\n console.log(\"\\nSession cancelled.\");\n process.exit(0);\n }\n console.error(\"Error:\", (err as Error).message);\n process.exit(1);\n }\n });\n\n// ── Phase 1: Repetition ─────────────────────────────────────────────────────\n\ninterface RepetitionResult {\n reviewed: number;\n maintained: number;\n skipped: boolean;\n}\n\nasync function runRepetitionPhase(\n db: Database,\n userId: string,\n maxMinutes: number,\n): Promise<RepetitionResult> {\n const queue = buildReviewQueue(db, { userId });\n\n if (queue.items.length === 0) {\n console.log(\"No cards due for review — moving to task selection.\\n\");\n return { reviewed: 0, maintained: 0, skipped: false };\n }\n\n console.log(\"═\".repeat(50));\n console.log(\"Phase 1: Repetition\");\n console.log(\"═\".repeat(50));\n console.log(`${queue.items.length} card(s) due`);\n console.log(\n ` New: ${queue.newCount} Review: ${queue.reviewCount} Relearn: ${queue.relearnCount}`,\n );\n console.log(` Domains: ${queue.totalDomains.join(\", \")}`);\n console.log(` Time limit: ${maxMinutes} minutes (skip anytime with 's')`);\n console.log();\n\n const startTime = Date.now();\n const timeLimitMs = maxMinutes * 60 * 1000;\n let reviewed = 0;\n let maintained = 0;\n\n for (const [index, item] of queue.items.entries()) {\n // Check time limit\n if (Date.now() - startTime >= timeLimitMs) {\n console.log(\n `\\nTime limit reached (${maxMinutes} min). Moving to task selection.`,\n );\n break;\n }\n\n const prompt = generatePrompt({\n cardId: item.cardId,\n tokenId: item.tokenId,\n slug: item.slug,\n concept: item.concept,\n domain: item.domain,\n bloomLevel: item.bloomLevel as BloomLevel,\n });\n\n const elapsed = Math.round((Date.now() - startTime) / 60000);\n console.log(\n `[${index + 1}/${queue.items.length}] ${prompt.bloomVerb} (Bloom ${prompt.bloomLevel}) — ${elapsed}/${maxMinutes} min`,\n );\n console.log(`Domain: ${prompt.domain || \"(none)\"}`);\n console.log(`\\n ${prompt.question}\\n`);\n\n const action = await runInteractiveReviewAction({\n db,\n userId,\n item,\n mode: \"session\",\n });\n\n if (action.action === \"stop\") {\n console.log(\"Stopping review and moving to task selection.\");\n return { reviewed, maintained, skipped: true };\n }\n\n if (action.action === \"rate\") {\n reviewed++;\n } else if (action.action !== \"skip\") {\n maintained++;\n }\n }\n\n if (reviewed > 0 || maintained > 0) {\n console.log(\"─\".repeat(50));\n console.log(`Repetition complete — ${reviewed} card(s) rated.`);\n if (maintained > 0) {\n console.log(`Maintenance actions: ${maintained}`);\n }\n }\n\n return { reviewed, maintained, skipped: false };\n}\n\n// ── Phase 2: Task Selection ─────────────────────────────────────────────────\n\nasync function selectTask(db: Database): Promise<string> {\n console.log(\"═\".repeat(50));\n console.log(\"Phase 2: Task Selection\");\n console.log(\"═\".repeat(50));\n\n const adoConfig = loadADOConfig(db);\n\n if (adoConfig) {\n const items = await fetchActiveWorkItems(adoConfig);\n\n if (items.length > 0) {\n const choices = items.map((wi) => ({\n name: `[${wi.type}] ${wi.title} (${wi.state})`,\n value: `[ADO-${wi.id}] ${wi.title}`,\n }));\n choices.push({ name: \"Enter a custom task...\", value: \"__custom__\" });\n\n const picked = await select({\n message: `${items.length} active work item(s) — pick one:`,\n choices,\n });\n\n if (picked !== \"__custom__\") return picked;\n } else {\n console.log(\"No active work items found in Azure DevOps.\");\n }\n }\n\n return input({ message: \"Task description:\" });\n}\n\n// ── zam session log ───────────────────────────────────────────────────────\n\nsessionCommand\n .command(\"log\")\n .description(\"Log a step within a session\")\n .requiredOption(\"--session <id>\", \"Session ID\")\n .requiredOption(\"--token <slug>\", \"Token slug\")\n .requiredOption(\"--done-by <who>\", \"Who performed the step (user or agent)\")\n .option(\"--rating <n>\", \"Rating (1-4)\")\n .option(\"--json\", \"Output as JSON\")\n .option(\"--quiet\", \"Suppress output (exit code only)\")\n .action((opts) => {\n withDb((db) => {\n const token = getTokenBySlug(db, opts.token);\n if (!token) {\n console.error(`Token not found: ${opts.token}`);\n process.exit(1);\n }\n\n const step = logStep(db, {\n session_id: opts.session,\n token_id: token.id,\n done_by: opts.doneBy as \"user\" | \"agent\",\n rating: opts.rating ? Number(opts.rating) : undefined,\n });\n\n if (opts.quiet) return;\n if (opts.json) {\n console.log(JSON.stringify(step, null, 2));\n } else {\n console.log(`Step logged: ${step.id}`);\n console.log(` Token: ${opts.token}`);\n console.log(` Done by: ${step.done_by}`);\n if (step.rating != null) {\n console.log(` Rating: ${step.rating}`);\n }\n }\n });\n });\n\n// ── zam session end ───────────────────────────────────────────────────────\n\nsessionCommand\n .command(\"end\")\n .description(\"End a session and show summary\")\n .requiredOption(\"--session <id>\", \"Session ID\")\n .option(\"--json\", \"Output as JSON\")\n .action((opts) => {\n withDb((db) => {\n endSession(db, opts.session);\n const summary = getSessionSummary(db, opts.session);\n\n if (opts.json) {\n console.log(JSON.stringify(summary, null, 2));\n return;\n }\n\n console.log(`Session ${summary.session.id} completed.`);\n console.log(` Task: ${summary.session.task}`);\n console.log(` Started: ${summary.session.started_at}`);\n console.log(` Completed: ${summary.session.completed_at}`);\n console.log(` Steps: ${summary.steps.length}`);\n\n if (summary.steps.length > 0) {\n console.log(\"\\nSteps:\");\n console.log(\" Token Done by Rating Concept\");\n console.log(` ${\"─\".repeat(70)}`);\n for (const s of summary.steps) {\n console.log(\n ` ${s.slug.padEnd(21)} ${s.done_by.padEnd(8)} ${String(s.rating ?? \"-\").padEnd(7)} ${s.concept.slice(0, 30)}`,\n );\n }\n }\n });\n });\n","/**\n * `zam settings` — User settings management.\n */\n\nimport { existsSync } from \"node:fs\";\nimport { Command } from \"commander\";\nimport type { Database } from \"libsql\";\nimport {\n deleteSetting,\n getAllSettings,\n getAllSettingsDetailed,\n getRepoPaths,\n getSetting,\n openDatabase,\n setSetting,\n} from \"../../kernel/index.js\";\n\nfunction withDb(fn: (db: Database) => void): void {\n let db: Database | undefined;\n try {\n db = openDatabase();\n fn(db);\n } catch (err) {\n console.error(\"Error:\", (err as Error).message);\n process.exit(1);\n } finally {\n db?.close();\n }\n}\n\nexport const settingsCommand = new Command(\"settings\").description(\n \"Manage user settings\",\n);\n\n// ── zam settings show ─────────────────────────────────────────────────────\n\nsettingsCommand\n .command(\"show\")\n .description(\"Show all settings\")\n .option(\"--json\", \"Output as JSON\")\n .action((opts) => {\n withDb((db) => {\n if (opts.json) {\n console.log(JSON.stringify(getAllSettings(db), null, 2));\n return;\n }\n\n const settings = getAllSettingsDetailed(db);\n if (settings.length === 0) {\n console.log(\"No settings configured.\");\n return;\n }\n\n console.log(\"Settings:\\n\");\n console.log(\"Key Value Updated\");\n console.log(\"─\".repeat(65));\n for (const s of settings) {\n console.log(\n `${s.key.padEnd(20)} ${s.value.padEnd(20)} ${s.updated_at}`,\n );\n }\n });\n });\n\n// ── zam settings get ──────────────────────────────────────────────────────\n\nsettingsCommand\n .command(\"get <key>\")\n .description(\"Get a single setting\")\n .option(\"--json\", \"Output as JSON\")\n .action((key, opts) => {\n withDb((db) => {\n const value = getSetting(db, key);\n\n if (opts.json) {\n console.log(JSON.stringify({ key, value: value ?? null }));\n return;\n }\n\n if (value === undefined) {\n console.log(`Not set: ${key}`);\n } else {\n console.log(value);\n }\n });\n });\n\n// ── zam settings set ──────────────────────────────────────────────────────\n\nsettingsCommand\n .command(\"set <key> <value>\")\n .description(\"Set a setting\")\n .option(\"--quiet\", \"Suppress output\")\n .action((key, value, opts) => {\n withDb((db) => {\n let parsedVal = value;\n if (key === \"llm.enabled\") {\n const lower = value.toLowerCase();\n if (\n lower === \"on\" ||\n lower === \"enable\" ||\n lower === \"enabled\" ||\n lower === \"true\"\n ) {\n parsedVal = \"true\";\n } else if (\n lower === \"off\" ||\n lower === \"disable\" ||\n lower === \"disabled\" ||\n lower === \"false\"\n ) {\n parsedVal = \"false\";\n }\n }\n setSetting(db, key, parsedVal);\n if (!opts.quiet) {\n console.log(`Set ${key} = ${parsedVal}`);\n }\n });\n });\n\n// ── zam settings delete ───────────────────────────────────────────────────\n\nsettingsCommand\n .command(\"delete <key>\")\n .description(\"Delete a setting\")\n .option(\"--quiet\", \"Suppress output\")\n .action((key, opts) => {\n withDb((db) => {\n const deleted = deleteSetting(db, key);\n if (!opts.quiet) {\n if (deleted) {\n console.log(`Deleted: ${key}`);\n } else {\n console.log(`Not found: ${key}`);\n }\n }\n });\n });\n\n// ── zam settings llm [state] ──────────────────────────────────────────────\n\nsettingsCommand\n .command(\"llm [state]\")\n .description(\n \"Quickly enable/disable or check local LLM integration (on/off/enable/disable)\",\n )\n .action((state) => {\n withDb((db) => {\n if (!state) {\n const enabled = getSetting(db, \"llm.enabled\") || \"false\";\n console.log(\n `LLM Integration is currently: ${\n enabled === \"true\"\n ? \"\\x1b[32mON (enabled)\\x1b[0m\"\n : \"\\x1b[31mOFF (disabled)\\x1b[0m\"\n }`,\n );\n return;\n }\n\n const lower = state.toLowerCase();\n let value = \"false\";\n if (\n lower === \"on\" ||\n lower === \"enable\" ||\n lower === \"enabled\" ||\n lower === \"true\"\n ) {\n value = \"true\";\n } else if (\n lower === \"off\" ||\n lower === \"disable\" ||\n lower === \"disabled\" ||\n lower === \"false\"\n ) {\n value = \"false\";\n } else {\n console.error(\n `Invalid state: ${state}. Use on, off, enable, or disable.`,\n );\n process.exit(1);\n }\n\n setSetting(db, \"llm.enabled\", value);\n console.log(\n `LLM Integration is now: ${\n value === \"true\"\n ? \"\\x1b[32mON (enabled)\\x1b[0m\"\n : \"\\x1b[31mOFF (disabled)\\x1b[0m\"\n }`,\n );\n });\n });\n\n// ── zam settings locale [lang] ───────────────────────────────────────────\n\nsettingsCommand\n .command(\"locale [lang]\")\n .description(\n \"Quickly set or check manual ZAM language override (en/de/es/fr/pt/zh/ja)\",\n )\n .action((lang) => {\n withDb((db) => {\n if (!lang) {\n const locale = getSetting(db, \"system.locale\") || \"en\";\n console.log(`Active language (locale): \\x1b[36m${locale}\\x1b[0m`);\n return;\n }\n\n const lower = lang.toLowerCase();\n const supported = [\"en\", \"de\", \"es\", \"fr\", \"pt\", \"zh\", \"ja\"];\n if (!supported.includes(lower)) {\n console.error(\n `Invalid language code: ${lang}. Supported: ${supported.join(\", \")}`,\n );\n process.exit(1);\n }\n\n setSetting(db, \"system.locale\", lower);\n console.log(`Language set to: \\x1b[32m${lower}\\x1b[0m`);\n });\n });\n\n// ── zam settings repos ───────────────────────────────────────────────────\n\nsettingsCommand\n .command(\"repos\")\n .description(\"Show or set Personal, Team, and Organization repository paths\")\n .option(\"--personal <path>\", \"Set the Personal Repository path\")\n .option(\"--team <path>\", \"Set the Team Repository path\")\n .option(\"--org <path>\", \"Set the Organization Repository path\")\n .action((opts) => {\n withDb((db) => {\n let changed = false;\n\n if (opts.personal !== undefined) {\n setSetting(db, \"repo.personal\", opts.personal);\n console.log(`Set repo.personal = ${opts.personal}`);\n changed = true;\n }\n if (opts.team !== undefined) {\n setSetting(db, \"repo.team\", opts.team);\n console.log(`Set repo.team = ${opts.team}`);\n changed = true;\n }\n if (opts.org !== undefined) {\n setSetting(db, \"repo.org\", opts.org);\n console.log(`Set repo.org = ${opts.org}`);\n changed = true;\n }\n\n if (changed) {\n console.log(\"\\nUpdated Repository Settings:\\n\");\n } else {\n console.log(\"Repository Settings:\\n\");\n }\n\n const paths = getRepoPaths(db);\n console.log(\n `Personal Repo: ${\n paths.personal\n ? `\\x1b[32m${paths.personal}\\x1b[0m`\n : \"\\x1b[31mNot Configured\\x1b[0m\"\n }`,\n );\n console.log(\n `Team Repo: ${\n paths.team\n ? `\\x1b[32m${paths.team}\\x1b[0m`\n : \"\\x1b[31mNot Configured\\x1b[0m\"\n }`,\n );\n console.log(\n `Org Repo: ${\n paths.org\n ? `\\x1b[32m${paths.org}\\x1b[0m`\n : \"\\x1b[31mNot Configured\\x1b[0m\"\n }`,\n );\n\n // Check if folders exist\n console.log(\"\\nValidation:\");\n for (const [name, path] of Object.entries(paths)) {\n if (path) {\n const exists = existsSync(path);\n console.log(\n ` ${name.padEnd(9)}: ${\n exists\n ? \"\\x1b[32m✓ Valid folder\\x1b[0m\"\n : \"\\x1b[31m✗ Directory does not exist\\x1b[0m\"\n }`,\n );\n } else {\n console.log(` ${name.padEnd(9)}: \\x1b[33m- Not Set\\x1b[0m`);\n }\n }\n });\n });\n","/**\n * `zam setup` — Distribute skill files from the zam package into the current\n * personal instance's agent skill directories, and optionally initialize the\n * ZAM database and generate agent-specific instruction files.\n *\n * Run this once after cloning a ZAM personal instance, and again after\n * upgrading zam (with --force) to refresh the skill files.\n */\n\nimport { copyFileSync, existsSync, mkdirSync, writeFileSync } from \"node:fs\";\nimport { basename, dirname, join } from \"node:path\";\nimport { fileURLToPath } from \"node:url\";\nimport { Command } from \"commander\";\nimport { getDefaultDbPath, openDatabaseWithSync } from \"../../kernel/index.js\";\n\n// The bundled CLI resolves from dist/cli/index.js; source tests resolve from\n// src/cli/commands/setup.ts. Select the first candidate containing package.json.\nconst packageRoot =\n [\n fileURLToPath(new URL(\"../..\", import.meta.url)),\n fileURLToPath(new URL(\"../../..\", import.meta.url)),\n ].find((candidate) => existsSync(join(candidate, \"package.json\"))) ??\n fileURLToPath(new URL(\"../..\", import.meta.url));\n\nconst SKILL_PAIRS: Array<{ from: string; to: string }> = [\n {\n from: join(packageRoot, \".claude\", \"skills\", \"zam\", \"SKILL.md\"),\n to: join(\".claude\", \"skills\", \"zam\", \"SKILL.md\"),\n },\n {\n from: join(packageRoot, \".agent\", \"skills\", \"zam\", \"SKILL.md\"),\n to: join(\".agent\", \"skills\", \"zam\", \"SKILL.md\"),\n },\n {\n from: join(packageRoot, \".agents\", \"skills\", \"zam\", \"SKILL.md\"),\n to: join(\".agents\", \"skills\", \"zam\", \"SKILL.md\"),\n },\n];\n\nexport function copySkills(force: boolean, cwd: string = process.cwd()): void {\n let anyAction = false;\n\n for (const { from, to } of SKILL_PAIRS) {\n const dest = join(cwd, to);\n\n if (!existsSync(from)) {\n console.warn(` warn source not found, skipping: ${from}`);\n continue;\n }\n\n if (existsSync(dest) && !force) {\n console.log(` skip ${to} (already present — use --force to update)`);\n continue;\n }\n\n mkdirSync(dirname(dest), { recursive: true });\n copyFileSync(from, dest);\n console.log(` copy ${to}`);\n anyAction = true;\n }\n\n if (!anyAction && !force) {\n console.log(\n \"\\nSkill files are already up to date. Run with --force to overwrite.\",\n );\n }\n}\n\nfunction initDatabase(skipInit: boolean): void {\n if (skipInit) return;\n\n try {\n const dbPath = getDefaultDbPath();\n const db = openDatabaseWithSync({ initialize: true });\n db.close();\n console.log(` init ZAM database at ${dbPath}`);\n } catch (err) {\n // Database may already exist — not an error during setup.\n const msg = (err as Error).message;\n if (!msg.includes(\"already\")) {\n console.warn(` warn database init: ${msg}`);\n } else {\n console.log(` skip database already initialized`);\n }\n }\n}\n\nexport function writeClaudeMd(\n skipClaudeMd: boolean,\n cwd: string = process.cwd(),\n): void {\n if (skipClaudeMd) return;\n\n const dest = join(cwd, \"CLAUDE.md\");\n if (existsSync(dest)) {\n console.log(` skip CLAUDE.md (already present)`);\n return;\n }\n\n const name = basename(cwd);\n writeFileSync(\n dest,\n `# ZAM Personal Kernel — ${name}\n\nThis is a ZAM personal instance. ZAM builds lasting skills through spaced\nrepetition during real work — not separate study sessions.\n\n## First time here?\nRun \\`/setup\\` in Claude Code or Gemini CLI to complete first-time setup.\n\n## Regular use\nRun \\`/zam\\` to start a learning session on whatever you are working on.\n\n## What lives here\n- \\`beliefs/\\` — your worldview, approved by git commit\n- \\`goals/\\` — your objectives, decomposed into tasks and learning tokens\n\n## Fast-changing data\nLearning tokens, cards, and review history live in local SQLite by default.\nUse \\`zam connector setup turso\\` to store cloud credentials in\n\\`~/.zam/credentials.json\\` and use a Turso database across machines.\n`,\n \"utf8\",\n );\n console.log(` write CLAUDE.md`);\n}\n\nexport function writeAgentsMd(\n skipAgentsMd: boolean,\n cwd: string = process.cwd(),\n): void {\n if (skipAgentsMd) return;\n\n const dest = join(cwd, \"AGENTS.md\");\n if (existsSync(dest)) {\n console.log(` skip AGENTS.md (already present)`);\n return;\n }\n\n const name = basename(cwd);\n writeFileSync(\n dest,\n `# ZAM Personal Kernel - ${name}\n\nThis is a ZAM personal instance. ZAM builds lasting skills through spaced\nrepetition during real work, not separate study sessions.\n\n## First time here?\nRun \\`zam setup\\` from the shell. When this repository includes\n\\`.agents/skills/setup/\\`, you can instead select \\`setup\\` through \\`/skills\\`\nor invoke \\`$setup\\`.\n\n## Regular use\nSelect the \\`zam\\` skill through \\`/skills\\` or invoke \\`$zam\\` to start a\nlearning session on whatever you are working on.\n\n## What lives here\n- \\`beliefs/\\` - your worldview, approved by git commit\n- \\`goals/\\` - your objectives, decomposed into tasks and learning tokens\n\n## Fast-changing data\nLearning tokens, cards, and review history live in local SQLite by default.\nUse \\`zam connector setup turso\\` to store cloud credentials in\n\\`~/.zam/credentials.json\\` and use a Turso database across machines.\n\n## Codex skills\nCodex discovers repository skills under \\`.agents/skills/\\`. Run\n\\`zam setup --force\\` after upgrading \\`zam-core\\` to refresh them.\n`,\n \"utf8\",\n );\n console.log(` write AGENTS.md`);\n}\n\nexport const setupCommand = new Command(\"setup\")\n .description(\n \"Distribute ZAM skill files into this personal instance and initialize the database\",\n )\n .option(\n \"--force\",\n \"overwrite existing skill files (use after upgrading zam)\",\n false,\n )\n .option(\"--skip-init\", \"skip database initialization\", false)\n .option(\"--skip-claude-md\", \"skip CLAUDE.md generation\", false)\n .option(\"--skip-agents-md\", \"skip AGENTS.md generation\", false)\n .action(\n (opts: {\n force: boolean;\n skipInit: boolean;\n skipClaudeMd: boolean;\n skipAgentsMd: boolean;\n }) => {\n console.log(`Setting up ZAM in ${process.cwd()}\\n`);\n\n copySkills(opts.force);\n initDatabase(opts.skipInit);\n writeClaudeMd(opts.skipClaudeMd);\n writeAgentsMd(opts.skipAgentsMd);\n\n console.log(\n \"\\nDone. Run `zam whoami --set <your-id>` to set your identity. Start the `zam` skill with `/zam` in Claude/Gemini-compatible clients or `$zam` (or `/skills`) in Codex.\",\n );\n },\n );\n","/**\n * `zam skill` — Manage agent skill entries (task recipes).\n */\n\nimport { Command } from \"commander\";\nimport type { Database } from \"libsql\";\nimport {\n createAgentSkill,\n getAgentSkill,\n listAgentSkills,\n openDatabase,\n} from \"../../kernel/index.js\";\n\nfunction withDb(fn: (db: Database) => void): void {\n let db: Database | undefined;\n try {\n db = openDatabase();\n fn(db);\n } catch (err) {\n console.error(\"Error:\", (err as Error).message);\n process.exit(1);\n } finally {\n db?.close();\n }\n}\n\nexport const skillCommand = new Command(\"skill\").description(\n \"Manage agent skill entries (task recipes)\",\n);\n\n// ── zam skill list ────────────────────────────────────────────────────────\n\nskillCommand\n .command(\"list\")\n .description(\"List all agent skills\")\n .option(\"--json\", \"Output as JSON\")\n .action((opts) => {\n withDb((db) => {\n const skills = listAgentSkills(db);\n\n if (opts.json) {\n console.log(JSON.stringify(skills, null, 2));\n return;\n }\n\n if (skills.length === 0) {\n console.log(\"No agent skills registered yet.\");\n return;\n }\n\n console.log(`Agent Skills (${skills.length})`);\n console.log(\"─\".repeat(60));\n for (const s of skills) {\n console.log(\n ` ${s.slug.padEnd(30)} [${s.source}] ${s.description.slice(0, 40)}`,\n );\n console.log(\n ` ${s.steps.length} step(s) tokens: ${s.token_slugs.join(\", \") || \"none\"}`,\n );\n }\n });\n });\n\n// ── zam skill show ────────────────────────────────────────────────────────\n\nskillCommand\n .command(\"show\")\n .description(\"Show a specific agent skill\")\n .requiredOption(\"--slug <slug>\", \"Skill slug\")\n .option(\"--json\", \"Output as JSON\")\n .action((opts) => {\n withDb((db) => {\n const skill = getAgentSkill(db, opts.slug);\n if (!skill) {\n console.error(`Skill not found: ${opts.slug}`);\n process.exit(1);\n }\n\n if (opts.json) {\n console.log(JSON.stringify(skill, null, 2));\n return;\n }\n\n console.log(`Skill: ${skill.slug}`);\n console.log(` Description: ${skill.description}`);\n console.log(` Source: ${skill.source}`);\n console.log(` Tokens: ${skill.token_slugs.join(\", \") || \"none\"}`);\n console.log(` Created: ${skill.created_at}`);\n console.log(`\\nSteps:`);\n skill.steps.forEach((step, i) => {\n console.log(` ${i + 1}. ${step}`);\n });\n });\n });\n\n// ── zam skill add ─────────────────────────────────────────────────────────\n\nskillCommand\n .command(\"add\")\n .description(\"Register a new agent skill\")\n .requiredOption(\"--slug <slug>\", \"Unique skill identifier\")\n .requiredOption(\"--description <text>\", \"One-sentence description\")\n .requiredOption(\"--steps <json>\", \"JSON array of step strings\")\n .option(\n \"--tokens <slugs>\",\n \"Comma-separated token slugs related to this skill\",\n )\n .option(\n \"--source <type>\",\n \"Source: learned | builtin (default: learned)\",\n \"learned\",\n )\n .option(\"--json\", \"Output as JSON\")\n .action((opts) => {\n withDb((db) => {\n let steps: string[];\n try {\n steps = JSON.parse(opts.steps) as string[];\n if (!Array.isArray(steps))\n throw new Error(\"steps must be a JSON array\");\n } catch {\n console.error(\"Invalid --steps: must be a valid JSON array of strings\");\n process.exit(1);\n }\n\n const tokenSlugs = opts.tokens\n ? opts.tokens\n .split(\",\")\n .map((s: string) => s.trim())\n .filter(Boolean)\n : [];\n\n const skill = createAgentSkill(db, {\n slug: opts.slug,\n description: opts.description,\n steps,\n token_slugs: tokenSlugs,\n source: opts.source as \"learned\" | \"builtin\",\n });\n\n if (opts.json) {\n console.log(JSON.stringify(skill, null, 2));\n } else {\n console.log(`Skill registered: ${skill.slug}`);\n console.log(` ${skill.steps.length} step(s) saved`);\n }\n });\n });\n","/**\n * `zam stats` — Learning dashboard.\n */\n\nimport { Command } from \"commander\";\nimport type { Database } from \"libsql\";\nimport {\n getDomainCompetence,\n getUserStats,\n openDatabase,\n} from \"../../kernel/index.js\";\nimport { resolveUser } from \"./resolve-user.js\";\n\nfunction withDb(fn: (db: Database) => void): void {\n let db: Database | undefined;\n try {\n db = openDatabase();\n fn(db);\n } catch (err) {\n console.error(\"Error:\", (err as Error).message);\n process.exit(1);\n } finally {\n db?.close();\n }\n}\n\nexport const statsCommand = new Command(\"stats\")\n .description(\"Show learning dashboard for a user\")\n .option(\"--user <id>\", \"User ID (default: whoami)\")\n .option(\"--json\", \"Output as JSON\")\n .action((opts) => {\n withDb((db) => {\n const userId = resolveUser(opts, db);\n const stats = getUserStats(db, userId);\n const domains = getDomainCompetence(db, userId);\n\n if (opts.json) {\n console.log(JSON.stringify({ stats, domains }, null, 2));\n return;\n }\n\n console.log(`Learning Dashboard — ${stats.userId}`);\n console.log(\"═\".repeat(50));\n console.log(` Total tokens: ${stats.totalTokens}`);\n console.log(` Cards in deck: ${stats.cardsInDeck}`);\n console.log(` Due today: ${stats.dueToday}`);\n console.log(` Blocked: ${stats.blocked}`);\n console.log(` Mature: ${stats.mature}`);\n console.log(` Avg stability: ${stats.avgStability ?? \"N/A\"}`);\n console.log(` Total sessions: ${stats.totalSessions}`);\n console.log(` Last session: ${stats.lastSession ?? \"N/A\"}`);\n\n if (domains.length > 0) {\n console.log(\"\\nDomain Competence:\");\n console.log(\"─\".repeat(80));\n console.log(\n \" Domain Cards Mature Stability Retention Suggested Mode\",\n );\n console.log(` ${\"─\".repeat(74)}`);\n for (const d of domains) {\n console.log(\n ` ${d.domain.padEnd(17)} ${String(d.totalCards).padEnd(6)} ${String(d.matureCards).padEnd(7)} ${String(d.avgStability).padEnd(10)} ${(d.retentionRate * 100).toFixed(1).padStart(5)}% ${d.suggestedMode}`,\n );\n }\n }\n });\n });\n","/**\n * `zam token` — Token management subcommand group.\n */\n\nimport { Command } from \"commander\";\nimport type { Database } from \"libsql\";\nimport type { BloomLevel, SymbiosisMode } from \"../../kernel/index.js\";\nimport {\n addPrerequisite,\n createToken,\n deleteToken,\n deprecateToken,\n findTokens,\n generateConceptFreeCue,\n getCard,\n getDependents,\n getPrerequisites,\n getSetting,\n getTokenBySlug,\n getTokenDeleteImpact,\n listTokens,\n openDatabase,\n resolveReviewContext,\n updateToken,\n} from \"../../kernel/index.js\";\nimport { generateQuestionViaLLM } from \"../llm/client.js\";\nimport { resolveUser } from \"./resolve-user.js\";\n\nasync function withDb(\n fn: (db: Database) => Promise<void> | void,\n): Promise<void> {\n let db: Database | undefined;\n try {\n db = openDatabase();\n await fn(db);\n } catch (err) {\n console.error(\"Error:\", (err as Error).message);\n process.exit(1);\n } finally {\n db?.close();\n }\n}\n\nfunction jsonOut(data: unknown): void {\n console.log(JSON.stringify(data, null, 2));\n}\n\nexport const tokenCommand = new Command(\"token\").description(\n \"Manage knowledge tokens\",\n);\n\n// ── zam token register ────────────────────────────────────────────────────\n\ntokenCommand\n .command(\"register\")\n .description(\"Register a new knowledge token\")\n .requiredOption(\"--slug <slug>\", \"Unique token slug\")\n .requiredOption(\"--concept <concept>\", \"Concept description\")\n .option(\"--domain <domain>\", \"Knowledge domain\", \"\")\n .option(\"--bloom <level>\", \"Bloom taxonomy level (1-5)\", \"1\")\n .option(\"--source-link <link>\", \"Source file path or reference URL\", \"\")\n .option(\"--question <question>\", \"Specific question prompt for recall\", \"\")\n .option(\"--json\", \"Output as JSON\")\n .action(async (opts) => {\n await withDb(async (db) => {\n let question: string | null = opts.question || null;\n if (!question) {\n const isLlmEnabled = getSetting(db, \"llm.enabled\") === \"true\";\n if (isLlmEnabled) {\n console.log(\n \"Generating high-quality active-recall question via local LLM...\",\n );\n try {\n let sourceLinkContent: string | null = null;\n if (opts.sourceLink) {\n const resolved = await resolveReviewContext(\n opts.sourceLink,\n ).catch(() => null);\n if (resolved) {\n sourceLinkContent = resolved.content;\n }\n }\n question = await generateQuestionViaLLM(db, {\n slug: opts.slug,\n concept: opts.concept,\n domain: opts.domain,\n bloomLevel: Number(opts.bloom),\n context: \"\",\n sourceLinkContent,\n });\n console.log(`Generated: \"${question}\"`);\n } catch (err) {\n console.warn(\n `LLM question generation failed: ${(err as Error).message}. Falling back to template.`,\n );\n }\n }\n\n if (!question) {\n question = generateConceptFreeCue(\n Number(opts.bloom) as BloomLevel,\n opts.slug,\n opts.domain,\n );\n }\n }\n\n const token = createToken(db, {\n slug: opts.slug,\n concept: opts.concept,\n domain: opts.domain,\n bloom_level: Number(opts.bloom) as BloomLevel,\n source_link: opts.sourceLink || null,\n question,\n });\n\n if (opts.json) {\n console.log(JSON.stringify(token, null, 2));\n } else {\n console.log(`Registered token: ${token.slug} (${token.id})`);\n console.log(` Concept: ${token.concept}`);\n console.log(` Domain: ${token.domain || \"(none)\"}`);\n console.log(` Bloom: ${token.bloom_level}`);\n console.log(` Question: ${token.question}`);\n if (token.source_link) {\n console.log(` Source: ${token.source_link}`);\n }\n }\n });\n });\n\n// ── zam token find ────────────────────────────────────────────────────────\n\ntokenCommand\n .command(\"find\")\n .description(\"Fuzzy search for tokens\")\n .requiredOption(\"--query <query>\", \"Search query\")\n .option(\"--json\", \"Output as JSON\")\n .action((opts) => {\n withDb((db) => {\n const results = findTokens(db, opts.query);\n\n if (opts.json) {\n console.log(JSON.stringify(results, null, 2));\n return;\n }\n\n if (results.length === 0) {\n console.log(\"No tokens found.\");\n return;\n }\n\n console.log(`Found ${results.length} token(s):\\n`);\n console.log(\n \"Score Slug Concept Domain Bloom\",\n );\n console.log(\"─\".repeat(90));\n for (const t of results) {\n console.log(\n `${String(t.score).padEnd(6)} ${t.slug.padEnd(21)} ${t.concept.slice(0, 31).padEnd(31)} ${(t.domain || \"-\").padEnd(11)} ${t.bloom_level}`,\n );\n }\n });\n });\n\n// ── zam token list ────────────────────────────────────────────────────────\n\ntokenCommand\n .command(\"list\")\n .description(\"List all tokens\")\n .option(\"--domain <domain>\", \"Filter by domain\")\n .option(\"--json\", \"Output as JSON\")\n .action((opts) => {\n withDb((db) => {\n const tokens = listTokens(\n db,\n opts.domain ? { domain: opts.domain } : undefined,\n );\n\n if (opts.json) {\n console.log(JSON.stringify(tokens, null, 2));\n return;\n }\n\n if (tokens.length === 0) {\n console.log(\"No tokens registered.\");\n return;\n }\n\n console.log(\n \"Slug Concept Domain Bloom\",\n );\n console.log(\"─\".repeat(80));\n for (const t of tokens) {\n console.log(\n `${t.slug.padEnd(21)} ${t.concept.slice(0, 31).padEnd(31)} ${(t.domain || \"-\").padEnd(11)} ${t.bloom_level}`,\n );\n }\n console.log(`\\n${tokens.length} token(s) total.`);\n });\n });\n\n// ── zam token edit ────────────────────────────────────────────────────────\n\ntokenCommand\n .command(\"edit\")\n .description(\"Edit a token's mutable fields\")\n .requiredOption(\"--slug <slug>\", \"Token slug\")\n .option(\"--concept <concept>\", \"Updated concept text\")\n .option(\"--domain <domain>\", \"Updated domain (blank allowed)\")\n .option(\"--bloom <level>\", \"Updated Bloom taxonomy level (1-5)\")\n .option(\"--context <context>\", \"Updated context (blank allowed)\")\n .option(\n \"--mode <mode>\",\n \"Updated symbiosis mode: shadowing | copilot | autonomy | none\",\n )\n .option(\n \"--source-link <link>\",\n \"Updated source file path or reference URL (blank allowed)\",\n )\n .option(\"--question <question>\", \"Updated question text (blank allowed)\")\n .option(\"--json\", \"Output as JSON\")\n .action(async (opts) => {\n await withDb(async (db) => {\n const updates: {\n concept?: string;\n domain?: string;\n bloom_level?: BloomLevel;\n context?: string;\n symbiosis_mode?: SymbiosisMode | null;\n source_link?: string | null;\n question?: string | null;\n } = {};\n\n if (opts.concept !== undefined) updates.concept = opts.concept;\n if (opts.domain !== undefined) updates.domain = opts.domain;\n if (opts.bloom !== undefined)\n updates.bloom_level = Number(opts.bloom) as BloomLevel;\n if (opts.context !== undefined) updates.context = opts.context;\n if (opts.sourceLink !== undefined) {\n updates.source_link = opts.sourceLink === \"\" ? null : opts.sourceLink;\n }\n if (opts.question !== undefined) {\n updates.question = opts.question === \"\" ? null : opts.question;\n }\n if (opts.mode !== undefined) {\n const validModes = [\"shadowing\", \"copilot\", \"autonomy\", \"none\"];\n if (!validModes.includes(opts.mode)) {\n console.error(`Invalid --mode: ${opts.mode}`);\n process.exit(1);\n }\n updates.symbiosis_mode =\n opts.mode === \"none\" ? null : (opts.mode as SymbiosisMode);\n }\n\n const token = updateToken(db, opts.slug, updates);\n\n if (opts.json) {\n jsonOut(token);\n return;\n }\n\n console.log(`Updated token: ${token.slug}`);\n console.log(` Concept: ${token.concept}`);\n console.log(` Domain: ${token.domain || \"(none)\"}`);\n console.log(` Bloom: ${token.bloom_level}`);\n console.log(` Question: ${token.question || \"(none)\"}`);\n console.log(` Context: ${token.context || \"(none)\"}`);\n console.log(` Mode: ${token.symbiosis_mode ?? \"none\"}`);\n console.log(` Source: ${token.source_link || \"(none)\"}`);\n });\n });\n\n// ── zam token prereq ─────────────────────────────────────────────────────\n\ntokenCommand\n .command(\"prereq\")\n .description(\"Add a prerequisite edge between tokens\")\n .requiredOption(\"--token <slug>\", \"Token that requires a prerequisite\")\n .requiredOption(\"--requires <slug>\", \"Required prerequisite token\")\n .option(\"--json\", \"Output as JSON\")\n .action((opts) => {\n withDb((db) => {\n const token = getTokenBySlug(db, opts.token);\n if (!token) {\n console.error(`Token not found: ${opts.token}`);\n process.exit(1);\n }\n\n const requires = getTokenBySlug(db, opts.requires);\n if (!requires) {\n console.error(`Prerequisite token not found: ${opts.requires}`);\n process.exit(1);\n }\n\n addPrerequisite(db, token.id, requires.id);\n\n if (opts.json) {\n console.log(\n JSON.stringify(\n { token: opts.token, requires: opts.requires },\n null,\n 2,\n ),\n );\n } else {\n console.log(\n `Added prerequisite: ${opts.token} requires ${opts.requires}`,\n );\n }\n });\n });\n\n// ── zam token deprecate ───────────────────────────────────────────────────\n\ntokenCommand\n .command(\"deprecate\")\n .description(\n \"Mark a token as deprecated (excluded from reviews, not deleted)\",\n )\n .requiredOption(\"--slug <slug>\", \"Token slug to deprecate\")\n .option(\"--json\", \"Output as JSON\")\n .action((opts) => {\n withDb((db) => {\n const token = deprecateToken(db, opts.slug);\n\n if (opts.json) {\n console.log(JSON.stringify(token, null, 2));\n } else {\n console.log(`Deprecated: ${token.slug}`);\n console.log(` Concept: ${token.concept}`);\n console.log(` At: ${token.deprecated_at}`);\n }\n });\n });\n\n// ── zam token delete ──────────────────────────────────────────────────────\n\ntokenCommand\n .command(\"delete\")\n .description(\"Hard-delete a token and its dependent learning data\")\n .requiredOption(\"--slug <slug>\", \"Token slug to delete\")\n .option(\"--force\", \"Actually delete the token\")\n .option(\"--json\", \"Output as JSON\")\n .action((opts) => {\n withDb((db) => {\n const impact = getTokenDeleteImpact(db, opts.slug);\n\n if (!opts.force) {\n const preview = {\n slug: opts.slug,\n deleted: false,\n requiresForce: true,\n impact,\n };\n\n if (opts.json) {\n jsonOut(preview);\n return;\n }\n\n console.log(`Delete preview for ${opts.slug}:`);\n console.log(` Cards: ${impact.cards}`);\n console.log(` Review logs: ${impact.review_logs}`);\n console.log(\n ` Prereq edges from it: ${impact.prerequisite_edges_from_token}`,\n );\n console.log(\n ` Prereq edges to it: ${impact.prerequisite_edges_to_token}`,\n );\n console.log(` Session steps: ${impact.session_steps}`);\n console.log(` Sessions touched: ${impact.sessions_touched}`);\n console.log(` Agent skills updated: ${impact.agent_skills}`);\n console.log(\"\\nRe-run with --force to delete.\");\n return;\n }\n\n const result = deleteToken(db, opts.slug);\n\n if (opts.json) {\n jsonOut({\n slug: result.token.slug,\n deleted: true,\n impact: result.impact,\n });\n return;\n }\n\n console.log(`Deleted token: ${result.token.slug}`);\n console.log(` Cards removed: ${result.impact.cards}`);\n console.log(` Review logs removed: ${result.impact.review_logs}`);\n console.log(` Session steps removed: ${result.impact.session_steps}`);\n console.log(` Agent skills updated: ${result.impact.agent_skills}`);\n });\n });\n\n// ── zam token status ─────────────────────────────────────────────────────\n\ntokenCommand\n .command(\"status\")\n .description(\"Show full status of a token for a user\")\n .requiredOption(\"--token <slug>\", \"Token slug\")\n .option(\"--user <id>\", \"User ID (default: whoami)\")\n .option(\"--json\", \"Output as JSON\")\n .action((opts) => {\n withDb((db) => {\n const userId = resolveUser(opts, db);\n const token = getTokenBySlug(db, opts.token);\n if (!token) {\n console.error(`Token not found: ${opts.token}`);\n process.exit(1);\n }\n\n const card = getCard(db, token.id, userId);\n const prereqs = getPrerequisites(db, token.id);\n const dependents = getDependents(db, token.id);\n\n const status = {\n token,\n card: card ?? null,\n prerequisites: prereqs,\n dependents,\n };\n\n if (opts.json) {\n console.log(JSON.stringify(status, null, 2));\n return;\n }\n\n console.log(`Token: ${token.slug} (${token.id})`);\n console.log(` Concept: ${token.concept}`);\n console.log(` Question: ${token.question || \"(none)\"}`);\n console.log(` Domain: ${token.domain || \"(none)\"}`);\n console.log(` Bloom: ${token.bloom_level}`);\n if (token.source_link) {\n console.log(` Source: ${token.source_link}`);\n }\n console.log();\n\n if (card) {\n console.log(\"Card status:\");\n console.log(` State: ${card.state}`);\n console.log(` Due at: ${card.due_at}`);\n console.log(` Stability: ${card.stability}`);\n console.log(` Difficulty: ${card.difficulty}`);\n console.log(` Reps: ${card.reps}`);\n console.log(` Lapses: ${card.lapses}`);\n console.log(` Blocked: ${card.blocked ? \"Yes\" : \"No\"}`);\n } else {\n console.log(\"No card exists for this user yet.\");\n }\n\n console.log();\n if (prereqs.length > 0) {\n console.log(\"Prerequisites:\");\n for (const p of prereqs) {\n console.log(` - ${p.slug}: ${p.concept} (bloom ${p.bloom_level})`);\n }\n } else {\n console.log(\"No prerequisites.\");\n }\n\n if (dependents.length > 0) {\n console.log(\"\\nDependents:\");\n for (const d of dependents) {\n console.log(` - ${d.slug}: ${d.concept} (bloom ${d.bloom_level})`);\n }\n }\n });\n });\n","/**\n * `zam ui` — one-word launcher for the ZAM Desktop GUI (Tauri Studio).\n *\n * The desktop app is a Tauri shell that re-uses the compiled CLI bridge\n * (dist/cli/index.js), so it needs:\n * - a built CLI (npm run build at the repo root), and\n * - either a previously built native binary, or the Rust toolchain to\n * compile one.\n *\n * Usage:\n * zam ui launch the GUI (built binary if present, else guide)\n * zam ui --dev run in hot-reload dev mode (needs Rust)\n * zam ui --build build the native installer (needs Rust, one-time)\n * zam ui --shortcut create Desktop + Start-menu shortcuts to the GUI\n */\n\nimport { type SpawnSyncOptions, spawn, spawnSync } from \"node:child_process\";\nimport { existsSync, mkdirSync, writeFileSync } from \"node:fs\";\nimport { homedir } from \"node:os\";\nimport { dirname, join } from \"node:path\";\nimport { fileURLToPath } from \"node:url\";\nimport { Command } from \"commander\";\nimport { hasCommand } from \"../../kernel/index.js\";\n\nconst C = {\n reset: \"\\x1b[0m\",\n red: \"\\x1b[31m\",\n green: \"\\x1b[32m\",\n yellow: \"\\x1b[33m\",\n cyan: \"\\x1b[36m\",\n dim: \"\\x1b[2m\",\n};\n\n/** Walk up from cwd and from this module to find the repo's `desktop/` dir. */\nfunction findDesktopDir(): string | null {\n const starts = [process.cwd(), dirname(fileURLToPath(import.meta.url))];\n for (const start of starts) {\n let dir = start;\n for (let i = 0; i < 10; i++) {\n if (existsSync(join(dir, \"desktop\", \"src-tauri\", \"tauri.conf.json\"))) {\n return join(dir, \"desktop\");\n }\n const parent = dirname(dir);\n if (parent === dir) break;\n dir = parent;\n }\n }\n return null;\n}\n\n/** Locate a previously built native binary, if any. */\nfunction findBuiltApp(desktopDir: string): string | null {\n const releaseDir = join(desktopDir, \"src-tauri\", \"target\", \"release\");\n if (process.platform === \"win32\") {\n for (const name of [\"ZAM.exe\", \"zam.exe\", \"zam-desktop.exe\"]) {\n const p = join(releaseDir, name);\n if (existsSync(p)) return p;\n }\n } else if (process.platform === \"darwin\") {\n const app = join(releaseDir, \"bundle\", \"macos\", \"ZAM.app\");\n if (existsSync(app)) return app;\n } else {\n for (const name of [\"zam\", \"ZAM\", \"zam-desktop\"]) {\n const p = join(releaseDir, name);\n if (existsSync(p)) return p;\n }\n }\n return null;\n}\n\n/** npm is npm.cmd on Windows, so child processes need a shell there. */\nfunction runNpm(args: string[], opts: SpawnSyncOptions): number {\n const res = spawnSync(\"npm\", args, {\n stdio: \"inherit\",\n shell: process.platform === \"win32\",\n ...opts,\n });\n return res.status ?? 1;\n}\n\nfunction ensureDesktopDeps(desktopDir: string): boolean {\n if (existsSync(join(desktopDir, \"node_modules\"))) return true;\n console.log(\n `${C.cyan}Installing desktop dependencies (one-time)...${C.reset}`,\n );\n return runNpm([\"install\"], { cwd: desktopDir }) === 0;\n}\n\nfunction requireRust(): boolean {\n if (hasCommand(\"cargo\")) return true;\n console.error(\n `${C.red}✗ The desktop GUI needs the Rust toolchain to compile.${C.reset}`,\n );\n console.error(\" Install it once, then re-run this command:\");\n console.error(\n ` ${C.cyan}winget install Rustlang.Rustup${C.reset} (or https://rustup.rs)`,\n );\n return false;\n}\n\n/**\n * On Windows the default MSVC Rust target needs the Visual Studio C++ linker\n * (link.exe), which rustup does NOT install. Detect it via vswhere so we fail\n * with guidance up front instead of after minutes of compiling.\n */\nfunction hasMsvcBuildTools(): boolean {\n if (process.platform !== \"win32\") return true;\n const pf86 = process.env[\"ProgramFiles(x86)\"] || \"C:\\\\Program Files (x86)\";\n const vswhere = join(\n pf86,\n \"Microsoft Visual Studio\",\n \"Installer\",\n \"vswhere.exe\",\n );\n if (!existsSync(vswhere)) return false;\n const res = spawnSync(\n vswhere,\n [\n \"-latest\",\n \"-products\",\n \"*\",\n \"-requires\",\n \"Microsoft.VisualStudio.Component.VC.Tools.x86.x64\",\n \"-property\",\n \"installationPath\",\n ],\n { encoding: \"utf8\" },\n );\n return (res.stdout ?? \"\").trim().length > 0;\n}\n\nfunction requireMsvcOnWindows(): boolean {\n if (hasMsvcBuildTools()) return true;\n console.error(\n `${C.red}✗ The MSVC C++ linker (link.exe) is missing.${C.reset}`,\n );\n console.error(\n \" Rust on Windows needs the Visual Studio Build Tools with the C++ workload\",\n );\n console.error(\" (rustup does not include it). Install it once (~a few GB),\");\n console.error(\" then open a NEW terminal and retry:\");\n console.error(\n ` ${C.cyan}winget install --id Microsoft.VisualStudio.2022.BuildTools -e --override \"--quiet --wait --add Microsoft.VisualStudio.Workload.VCTools --includeRecommended\"${C.reset}`,\n );\n return false;\n}\n\nfunction warnIfCliMissing(repoRoot: string): void {\n if (!existsSync(join(repoRoot, \"dist\", \"cli\", \"index.js\"))) {\n console.warn(\n `${C.yellow}⚠ CLI build not found (dist/cli/index.js). The GUI bridge needs it — run 'npm run build' at the repo root.${C.reset}`,\n );\n }\n}\n\n/**\n * Record this repo root in ~/.zam/cli_path so the installed GUI can locate the\n * CLI bridge regardless of where it is launched from (Start menu / Program\n * Files have no idea where the repo lives). The Tauri backend reads this file.\n */\nfunction recordCliHome(repoRoot: string): void {\n try {\n const zamDir = join(homedir(), \".zam\");\n if (!existsSync(zamDir)) mkdirSync(zamDir, { recursive: true });\n writeFileSync(join(zamDir, \"cli_path\"), repoRoot, \"utf8\");\n } catch {\n // best-effort: a missing marker just falls back to cwd resolution\n }\n}\n\nfunction launchApp(appPath: string, repoRoot: string): void {\n // Run from the repo root so the Tauri backend resolves dist/cli/index.js.\n console.log(`${C.green}✓ Launching ZAM Desktop...${C.reset}`);\n if (process.platform === \"darwin\" && appPath.endsWith(\".app\")) {\n spawn(\"open\", [appPath], {\n cwd: repoRoot,\n detached: true,\n stdio: \"ignore\",\n }).unref();\n } else {\n spawn(appPath, [], {\n cwd: repoRoot,\n detached: true,\n stdio: \"ignore\",\n windowsHide: true,\n }).unref();\n }\n}\n\nfunction createShortcuts(appPath: string, repoRoot: string): void {\n if (process.platform !== \"win32\") {\n console.error(\n `${C.red}✗ --shortcut is currently implemented for Windows only.${C.reset}`,\n );\n console.error(\n \" On macOS drag ZAM.app to the Dock; on Linux create a .desktop entry.\",\n );\n return;\n }\n // WScript.Shell creates a .lnk on the Desktop and in the Start menu.\n const ps = [\n \"$ws = New-Object -ComObject WScript.Shell\",\n \"foreach ($dir in @([Environment]::GetFolderPath('Desktop'), [Environment]::GetFolderPath('Programs'))) {\",\n \" $lnk = $ws.CreateShortcut((Join-Path $dir 'ZAM.lnk'))\",\n ` $lnk.TargetPath = '${appPath}'`,\n ` $lnk.WorkingDirectory = '${repoRoot}'`,\n ` $lnk.IconLocation = '${appPath},0'`,\n \" $lnk.Description = 'ZAM Active-Recall Studio'\",\n \" $lnk.Save()\",\n \"}\",\n ].join(\"\\n\");\n const res = spawnSync(\n \"powershell\",\n [\"-NoProfile\", \"-ExecutionPolicy\", \"Bypass\", \"-Command\", ps],\n { stdio: \"inherit\" },\n );\n if (res.status === 0) {\n console.log(\n `${C.green}✓ Created 'ZAM' shortcuts on your Desktop and in the Start menu.${C.reset}`,\n );\n } else {\n console.error(`${C.red}✗ Could not create shortcuts.${C.reset}`);\n }\n}\n\nexport const uiCommand = new Command(\"ui\")\n .description(\"Launch the ZAM Desktop GUI (Active-Recall Studio)\")\n .option(\"--dev\", \"Run in hot-reload development mode (needs Rust)\")\n .option(\n \"--build\",\n \"Build the native installer for your OS (needs Rust, one-time)\",\n )\n .option(\"--shortcut\", \"Create Desktop + Start-menu shortcuts to the GUI\")\n .action((opts) => {\n const desktopDir = findDesktopDir();\n if (!desktopDir) {\n console.error(\n `${C.red}✗ Could not find the desktop/ app. Run 'zam ui' from inside the ZAM repository.${C.reset}`,\n );\n process.exit(1);\n }\n const repoRoot = dirname(desktopDir);\n\n // Record where the CLI lives so the installed GUI can always find the\n // bridge, no matter which directory it is launched from.\n recordCliHome(repoRoot);\n\n // --build: compile the native installer.\n if (opts.build) {\n if (!requireRust()) process.exit(1);\n if (!requireMsvcOnWindows()) process.exit(1);\n if (!ensureDesktopDeps(desktopDir)) process.exit(1);\n console.log(\n `${C.cyan}Building the native ZAM installer (this takes a few minutes)...${C.reset}`,\n );\n const code = runNpm([\"run\", \"tauri\", \"build\"], { cwd: desktopDir });\n if (code === 0) {\n const bundle = join(\n desktopDir,\n \"src-tauri\",\n \"target\",\n \"release\",\n \"bundle\",\n );\n console.log(\n `\\n${C.green}✓ Done. Installer is in:${C.reset}\\n ${bundle}`,\n );\n console.log(\n `${C.dim}Run that installer once — it adds ZAM to the Start menu and Desktop automatically.${C.reset}`,\n );\n console.log(\n `${C.dim}Recorded this repo (${repoRoot}) in ~/.zam/cli_path so the installed app finds the CLI.${C.reset}`,\n );\n }\n process.exit(code);\n }\n\n // --dev: hot-reload development mode.\n if (opts.dev) {\n if (!requireRust()) process.exit(1);\n if (!requireMsvcOnWindows()) process.exit(1);\n if (!ensureDesktopDeps(desktopDir)) process.exit(1);\n warnIfCliMissing(repoRoot);\n console.log(\n `${C.cyan}Starting ZAM Desktop in dev mode (Ctrl+C to stop)...${C.reset}`,\n );\n process.exit(runNpm([\"run\", \"tauri\", \"dev\"], { cwd: desktopDir }));\n }\n\n const builtApp = findBuiltApp(desktopDir);\n\n // --shortcut: needs a built binary to point at.\n if (opts.shortcut) {\n if (!builtApp) {\n console.error(\n `${C.red}✗ No built app yet. Build it first:${C.reset} ${C.cyan}zam ui --build${C.reset}`,\n );\n process.exit(1);\n }\n createShortcuts(builtApp, repoRoot);\n return;\n }\n\n // Default: launch the built app, or guide the user to get one.\n if (builtApp) {\n warnIfCliMissing(repoRoot);\n launchApp(builtApp, repoRoot);\n return;\n }\n\n console.log(\n `${C.yellow}The ZAM Desktop GUI hasn't been built yet.${C.reset}\\n`,\n );\n if (hasCommand(\"cargo\")) {\n console.log(\"Pick one:\");\n console.log(\n ` ${C.cyan}zam ui --build${C.reset} Build a native installer (Start-menu + Desktop entry)`,\n );\n console.log(\n ` ${C.cyan}zam ui --dev${C.reset} Just run it now in dev mode`,\n );\n } else {\n console.log(\"To get a clickable Start-menu app, do this once:\");\n console.log(\n ` 1. Install Rust: ${C.cyan}winget install Rustlang.Rustup${C.reset}`,\n );\n console.log(` 2. Build the app: ${C.cyan}zam ui --build${C.reset}`);\n console.log(\n ` 3. Run the installer it prints — ZAM then lives in your Start menu.`,\n );\n console.log(\n `\\n${C.dim}After that, 'zam ui' launches it directly.${C.reset}`,\n );\n }\n });\n","/**\n * `zam whoami` — Manage default user identity.\n */\n\nimport { Command } from \"commander\";\nimport type { Database } from \"libsql\";\nimport {\n deleteSetting,\n getSetting,\n openDatabase,\n setSetting,\n} from \"../../kernel/index.js\";\n\nfunction withDb(fn: (db: Database) => void): void {\n let db: Database | undefined;\n try {\n db = openDatabase();\n fn(db);\n } catch (err) {\n console.error(\"Error:\", (err as Error).message);\n process.exit(1);\n } finally {\n db?.close();\n }\n}\n\nexport const whoamiCommand = new Command(\"whoami\")\n .description(\"Show or set the default user identity\")\n .option(\"--set <id>\", \"Set the default user ID\")\n .option(\"--clear\", \"Remove the default user ID\")\n .option(\"--json\", \"Output as JSON\")\n .action((opts) => {\n withDb((db) => {\n if (opts.set) {\n setSetting(db, \"user.id\", opts.set);\n if (opts.json) {\n console.log(JSON.stringify({ userId: opts.set }));\n } else {\n console.log(`Default user set to: ${opts.set}`);\n }\n return;\n }\n\n if (opts.clear) {\n const deleted = deleteSetting(db, \"user.id\");\n if (opts.json) {\n console.log(JSON.stringify({ userId: null, cleared: deleted }));\n } else if (deleted) {\n console.log(\"Default user cleared.\");\n } else {\n console.log(\"No default user was set.\");\n }\n return;\n }\n\n const userId = getSetting(db, \"user.id\");\n if (opts.json) {\n console.log(JSON.stringify({ userId: userId ?? null }));\n return;\n }\n\n if (userId) {\n console.log(userId);\n } else {\n console.log(\"No default user set. Use: zam whoami --set <id>\");\n }\n });\n });\n","/**\n * `zam workspace` — Workspace management commands.\n *\n * `zam workspace publish` initializes a Git repository inside the local sandbox\n * workspace and pushes it to GitHub, establishing a secure version-controlled backup\n * and change-managed team workflow.\n */\n\nimport { execSync } from \"node:child_process\";\nimport { existsSync, writeFileSync } from \"node:fs\";\nimport { join } from \"node:path\";\nimport { confirm, input } from \"@inquirer/prompts\";\nimport { Command } from \"commander\";\nimport { getSetting, hasCommand, openDatabase } from \"../../kernel/index.js\";\n\n/**\n * Execute a shell command inside a specific directory.\n */\nfunction runGit(cwd: string, args: string): string {\n try {\n return execSync(`git ${args}`, {\n cwd,\n stdio: \"pipe\",\n encoding: \"utf8\",\n }).trim();\n } catch (err) {\n throw new Error(`Git command failed: ${(err as Error).message}`);\n }\n}\n\nexport const workspaceCommand = new Command(\"workspace\").description(\n \"Manage your ZAM learning workspace\",\n);\n\nworkspaceCommand\n .command(\"publish\")\n .description(\"Publish your local workspace sandbox to GitHub\")\n .action(async () => {\n let db: ReturnType<typeof openDatabase> | undefined;\n let workspaceDir = \"\";\n\n try {\n db = openDatabase();\n workspaceDir = getSetting(db, \"personal.workspace_dir\") || \"\";\n db.close();\n } catch {\n // Fallback if DB doesn't exist\n db?.close();\n }\n\n if (!workspaceDir) {\n console.error(\n \"\\x1b[31m✗ No active workspace configured. Please run `zam init` first.\\x1b[0m\",\n );\n process.exit(1);\n }\n\n if (!existsSync(workspaceDir)) {\n console.error(\n `\\x1b[31m✗ Workspace directory does not exist: ${workspaceDir}\\x1b[0m`,\n );\n process.exit(1);\n }\n\n console.log(`\\nActive workspace: \\x1b[36m${workspaceDir}\\x1b[0m`);\n\n // ── Check Git command ────────────────────────────────────────────────────\n if (!hasCommand(\"git\")) {\n console.error(\n \"\\x1b[31m✗ Git command was not found on this system. Please install Git first.\\x1b[0m\",\n );\n process.exit(1);\n }\n\n // ── Setup .gitignore ─────────────────────────────────────────────────────\n const gitignorePath = join(workspaceDir, \".gitignore\");\n if (!existsSync(gitignorePath)) {\n writeFileSync(\n gitignorePath,\n \"node_modules/\\n.agent/\\n.agents/\\n.claude/\\n.gemini/\\n.goose/\\n*.log\\n\",\n \"utf8\",\n );\n }\n\n // ── Initialize Local Git Repo ────────────────────────────────────────────\n const hasGitRepo = existsSync(join(workspaceDir, \".git\"));\n if (!hasGitRepo) {\n console.log(\"Initializing local Git repository...\");\n runGit(workspaceDir, \"init -b main\");\n runGit(workspaceDir, \"add .\");\n runGit(\n workspaceDir,\n 'commit -m \"chore: initial workspace sandbox bootstrap\"',\n );\n console.log(\"\\x1b[32m✓ Local Git repository initialized.\\x1b[0m\");\n } else {\n console.log(\"Git repository is already initialized.\");\n }\n\n // ── GitHub Publishing options ────────────────────────────────────────────\n const repoName = await input({\n message: \"Choose a name for your GitHub repository:\",\n default: \"zam-personal\",\n });\n\n const isPrivate = await confirm({\n message: \"Should the repository be private?\",\n default: true,\n });\n\n const repoVisibility = isPrivate ? \"--private\" : \"--public\";\n\n // ── Method A: Using GitHub CLI (gh) ──────────────────────────────────────\n if (hasCommand(\"gh\")) {\n console.log(\"GitHub CLI detected! Automating repository creation...\");\n const proceedGh = await confirm({\n message:\n \"Would you like ZAM to create the repository using the GitHub CLI?\",\n default: true,\n });\n\n if (proceedGh) {\n try {\n console.log(`Creating GitHub repository ${repoName}...`);\n execSync(\n `gh repo create ${repoName} ${repoVisibility} --source=. --push`,\n {\n cwd: workspaceDir,\n stdio: \"inherit\",\n },\n );\n console.log(\n \"\\n\\x1b[32m✓ Successfully published workspace to GitHub!\\x1b[0m\",\n );\n process.exit(0);\n } catch (err) {\n console.warn(\n `\\x1b[33m⚠ GitHub CLI creation failed: ${(err as Error).message}\\x1b[0m`,\n );\n }\n }\n }\n\n // ── Method B: Manual Git setup instructions ──────────────────────────────\n console.log(\n \"\\n\\x1b[1mPlease create the repository manually on GitHub:\\x1b[0m\",\n );\n console.log(\" 1. Go to https://github.com/new\");\n console.log(` 2. Name it exactly: \\x1b[36m${repoName}\\x1b[0m`);\n console.log(\n ` 3. Choose \\x1b[36m${isPrivate ? \"Private\" : \"Public\"}\\x1b[0m`,\n );\n console.log(\n \" 4. Do NOT initialize it with README, .gitignore, or license\",\n );\n console.log(\" 5. Click 'Create repository'\\n\");\n\n const githubUrl = await input({\n message:\n \"Paste the repository Git URL (e.g. git@github.com:user/repo.git):\",\n });\n\n if (githubUrl) {\n try {\n console.log(\"Linking remote repository and pushing...\");\n // Check if origin already exists\n let hasOrigin = false;\n try {\n runGit(workspaceDir, \"remote get-url origin\");\n hasOrigin = true;\n } catch {}\n\n if (hasOrigin) {\n runGit(workspaceDir, `remote set-url origin ${githubUrl}`);\n } else {\n runGit(workspaceDir, `remote add origin ${githubUrl}`);\n }\n\n runGit(workspaceDir, \"push -u origin main\");\n console.log(\n \"\\x1b[32m✓ Successfully linked and pushed to GitHub!\\x1b[0m\",\n );\n } catch (err) {\n console.error(\n `\\x1b[31m✗ Push failed: ${(err as Error).message}\\x1b[0m`,\n );\n console.log(\n \"You can push manually later using: git push -u origin main\",\n );\n }\n }\n });\n"],"mappings":";;;AAAA,SAAS,WAAAA,iBAAe;;;ACOxB,SAAS,eAAAC,oBAAmB;AAC5B,SAAS,WAAAC,gBAAe;AACxB,SAAS,QAAAC,aAAY;AACrB,SAAS,eAAe;;;ACoBxB,SAAS,EAAE,IAAc,QAAgB,QAAmB;AAC1D,SAAO,GAAG,QAAQ,GAAG,EAAE,IAAI,GAAG,MAAM;AACtC;AAKO,SAAS,aAAa,IAAc,QAA2B;AACpE,SAAO;AAAA,IACL;AAAA,IACA,aAAc,EAAE,IAAI,kCAAkC,EAAoB;AAAA,IAC1E,aACE,EAAE,IAAI,qDAAqD,MAAM,EAGjE;AAAA,IACF,UACE;AAAA,MACE;AAAA,MACA;AAAA,MACA;AAAA,IACF,EACA;AAAA,IACF,SACE;AAAA,MACE;AAAA,MACA;AAAA,MACA;AAAA,IACF,EACA;AAAA,IACF,QACE;AAAA,MACE;AAAA,MACA;AAAA,MACA;AAAA,IACF,EACA;AAAA,IACF,eAAe,MAAM;AACnB,YAAM,IAAI;AAAA,QACR;AAAA,QACA;AAAA,QACA;A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,KAAK,EACV;AAAA,EACC;AACF,EACC,QAAQ,OAAO;AAElB,QAAQ,WAAW,WAAW;AAC9B,QAAQ,WAAW,YAAY;AAC/B,QAAQ,WAAW,YAAY;AAC/B,QAAQ,WAAW,WAAW;AAC9B,QAAQ,WAAW,cAAc;AACjC,QAAQ,WAAW,YAAY;AAC/B,QAAQ,WAAW,aAAa;AAChC,QAAQ,WAAW,YAAY;AAC/B,QAAQ,WAAW,SAAS;AAC5B,QAAQ,WAAW,aAAa;AAChC,QAAQ,WAAW,YAAY;AAC/B,QAAQ,WAAW,cAAc;AACjC,QAAQ,WAAW,eAAe;AAClC,QAAQ,WAAW,aAAa;AAChC,QAAQ,WAAW,gBAAgB;AACnC,QAAQ,WAAW,WAAW;AAC9B,QAAQ,WAAW,cAAc;AACjC,QAAQ,WAAW,gBAAgB;AAEnC,QAAQ,MAAM;","names":["Command","readdirSync","homedir","join","existsSync","mkdirSync","readFileSync","homedir","dirname","join","join","homedir","existsSync","readFileSync","dirname","mkdirSync","existsSync","readFileSync","writeFileSync","join","existsSync","join","readFileSync","t","input","writeFileSync","input","ulid","ulid","ulid","input","ulid","input","t","existsSync","mkdirSync","readFileSync","homedir","join","join","homedir","join","existsSync","mkdirSync","readFileSync","ulid","interval","dueAt","input","ulid","input","input","existsSync","readFileSync","dirname","join","anchorIndex","anchor","appendFileSync","existsSync","mkdirSync","readFileSync","homedir","join","packageRoot","existsSync","homedir","join","execSync","execSync","existsSync","resolve","resolve","existsSync","BLOOM_VERBS","input","existsSync","resolve","confirm","getReviewTarget","join","homedir","readdirSync","Command","withDb","Command","Command","Command","execSync","existsSync","writeFileSync","join","Command","withDb","join","existsSync","writeFileSync","Command","execSync","existsSync","mkdirSync","resolve","input","Command","resolve","Command","existsSync","tasks","tokens","t","mkdirSync","input","existsSync","mkdirSync","writeFileSync","homedir","join","input","Command","HOME","homedir","mkdirSync","join","existsSync","writeFileSync","Command","input","input","Command","BLOOM_VERBS","input","confirm","input","confirm","input","Command","input","execSync","writeFileSync","basename","join","Command","psSingleQuoted","basename","Command","execSync","join","writeFileSync","Command","Command","input","select","Command","withDb","Command","select","input","existsSync","Command","withDb","Command","existsSync","copyFileSync","existsSync","mkdirSync","writeFileSync","basename","dirname","join","fileURLToPath","Command","fileURLToPath","existsSync","join","mkdirSync","dirname","copyFileSync","basename","writeFileSync","Command","Command","withDb","Command","Command","withDb","Command","Command","withDb","jsonOut","Command","t","spawn","existsSync","mkdirSync","writeFileSync","homedir","dirname","join","fileURLToPath","Command","dirname","fileURLToPath","existsSync","join","homedir","mkdirSync","writeFileSync","spawn","Command","Command","withDb","Command","execSync","existsSync","writeFileSync","join","confirm","input","Command","execSync","Command","existsSync","join","writeFileSync","input","confirm","Command"]}
|