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GAAS,OAAO,IAAA,EAAK;AAC3B,EAAA,KAAA,MAAW,SAAS,MAAA,EAAQ;AAC1B,IAAA,MAAM,KAAA,CAAM,OAAO,KAAK,CAAA;AAAA,EAC1B;AACA,EAAA,OAAO,MAAA,CAAO,MAAA;AAChB;AAaO,SAAS,cAAA,CAAe,QAAgB,KAAA,EAAkC;AAC/E,EAAA,OAAO,MAAA,CAAO,EAAA,CAAG,CAAC,KAAA,KAAU;AAC1B,IAAA,IAAI,KAAA,CAAM,YAAY,MAAA,EAAW;AAC/B,MAAA;AAAA,IACF;AACA,IAAA,MAAM,KAAA,GAAQ,MAAA,CAAO,GAAA,CAAI,KAAA,CAAM,OAAO,CAAA;AACtC,IAAA,IAAI;AACF,MAAA,MAAM,MAAA,GAAS,KAAA,KAAU,KAAA,CAAA,GAAY,KAAA,CAAM,MAAA,CAAO,KAAK,CAAA,GAAI,KAAA,CAAM,MAAA,CAAO,KAAA,CAAM,OAAO,CAAA;AACrF,MAAA,IAAI,kBAAkB,OAAA,EAAS;AAC7B,QAAA,MAAA,CAAO,KAAA,CAAM,MAAM,KAAA,CAAS,CAAA;AAAA,MAC9B;AAAA,IACF,CAAA,CAAA,MAAQ;AAAA,IAER;AAAA,EACF,CAAC,CAAA;AACH","file":"index.cjs","sourcesContent":["/**\n * Structural registry adapters for Krikos: write-through mirrors of the agent registry into an\n * external store for cross-process query, without Krikos importing any database driver.\n *\n * The engine's own persistence is a single snapshot via a {@link import('../types.js').StateBackend}.\n * These adapters are a different concern: they project each agent as an individually queryable\n * row or key so a dashboard, a CISO report, or another service can read the fleet inventory\n * directly from Postgres, SQLite, or a key-value store. Krikos matches the minimal client shape\n * structurally; the host passes a ready, credentialed object.\n *\n * @packageDocumentation\n */\n\nimport type { Agent, Krikos } from '../types.js';\n\n/** A write-through registry mirror: upsert and remove one agent at a time. */\nexport interface RegistryStore {\n  /** Inserts or replaces the row/entry for `agent`. */\n  upsert(agent: Agent): Promise<void> | void;\n  /** Removes the row/entry for `agentId`, if present. */\n  remove(agentId: string): Promise<void> | void;\n}\n\n/** The flat row shape an agent projects to, stable and driver-agnostic. */\nexport interface AgentRow {\n  readonly id: string;\n  readonly name: string;\n  readonly status: string;\n  readonly issuer: string;\n  readonly framework: string | null;\n  readonly fingerprint: string;\n  readonly credential_version: number;\n  readonly capabilities: string;\n  readonly issued_at: number;\n  readonly expires_at: number | null;\n  readonly updated_at: number;\n}\n\n/** Projects an agent to its flat row. Capabilities and metadata are JSON-encoded. */\nexport function toRow(agent: Agent): AgentRow {\n  return {\n    id: agent.id,\n    name: agent.name,\n    status: agent.status,\n    issuer: agent.issuer.id,\n    framework: agent.framework ?? null,\n    fingerprint: agent.fingerprint,\n    credential_version: agent.credentialVersion,\n    capabilities: JSON.stringify(agent.capabilities),\n    issued_at: agent.issuedAt,\n    expires_at: agent.expiresAt,\n    updated_at: agent.updatedAt,\n  };\n}\n\n/** Structural SQL executor: runs a parameterized statement. Postgres, SQLite, or any wrapper. */\nexport type SqlExecutor = (sql: string, params: readonly unknown[]) => Promise<unknown> | unknown;\n\n/**\n * Creates a write-through registry store backed by a SQL table.\n *\n * The host supplies an executor and a table name; Krikos issues upsert and delete statements with\n * positional `?` placeholders (the most portable form across SQLite and, via a wrapper, Postgres).\n * Krikos imports no driver and never sees credentials.\n *\n * @param exec - The parameterized statement executor.\n * @param table - The table name. Defaults to `krikos_agents`.\n * @returns A {@link RegistryStore}.\n */\nexport function sqlRegistryStore(exec: SqlExecutor, table = 'krikos_agents'): RegistryStore {\n  const columns =\n    'id, name, status, issuer, framework, fingerprint, credential_version, capabilities, issued_at, expires_at, updated_at';\n  return {\n    upsert(agent: Agent): Promise<void> | void {\n      const row = toRow(agent);\n      const sql =\n        `INSERT INTO ${table} (${columns}) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ` +\n        'ON CONFLICT(id) DO UPDATE SET name=excluded.name, status=excluded.status, ' +\n        'issuer=excluded.issuer, framework=excluded.framework, fingerprint=excluded.fingerprint, ' +\n        'credential_version=excluded.credential_version, capabilities=excluded.capabilities, ' +\n        'issued_at=excluded.issued_at, expires_at=excluded.expires_at, updated_at=excluded.updated_at';\n      const result = exec(sql, [\n        row.id,\n        row.name,\n        row.status,\n        row.issuer,\n        row.framework,\n        row.fingerprint,\n        row.credential_version,\n        row.capabilities,\n        row.issued_at,\n        row.expires_at,\n        row.updated_at,\n      ]);\n      return result instanceof Promise ? result.then(() => undefined) : undefined;\n    },\n    remove(agentId: string): Promise<void> | void {\n      const result = exec(`DELETE FROM ${table} WHERE id = ?`, [agentId]);\n      return result instanceof Promise ? result.then(() => undefined) : undefined;\n    },\n  };\n}\n\n/** Structural key-value client for the registry mirror; satisfied by Redis, Upstash, CF KV. */\nexport interface KvClientLike {\n  set(key: string, value: string): Promise<unknown> | unknown;\n  del?(key: string): Promise<unknown> | unknown;\n}\n\n/**\n * Creates a write-through registry store backed by a key-value client, one JSON value per agent\n * under `<prefix><id>`. When the client exposes `del`, removal deletes the key; otherwise removal\n * writes an empty tombstone value.\n *\n * @param kv - The structural key-value client.\n * @param prefix - The key prefix. Defaults to `krikos:agent:`.\n * @returns A {@link RegistryStore}.\n */\nexport function kvRegistryStore(kv: KvClientLike, prefix = 'krikos:agent:'): RegistryStore {\n  return {\n    upsert(agent: Agent): Promise<void> | void {\n      const result = kv.set(`${prefix}${agent.id}`, JSON.stringify(toRow(agent)));\n      return result instanceof Promise ? result.then(() => undefined) : undefined;\n    },\n    remove(agentId: string): Promise<void> | void {\n      const result =\n        kv.del !== undefined ? kv.del(`${prefix}${agentId}`) : kv.set(`${prefix}${agentId}`, '');\n      return result instanceof Promise ? result.then(() => undefined) : undefined;\n    },\n  };\n}\n\n/**\n * Synchronizes the entire current registry into a {@link RegistryStore} in one pass. Use it to\n * seed a fresh mirror or to reconcile after pruning. Returns the number of agents written.\n *\n * @param krikos - The registry to read.\n * @param store - The destination store.\n * @returns The number of agents upserted.\n */\nexport async function syncAll(krikos: Krikos, store: RegistryStore): Promise<number> {\n  const agents = krikos.list();\n  for (const agent of agents) {\n    await store.upsert(agent);\n  }\n  return agents.length;\n}\n\n/**\n * Wires a live write-through mirror: on every agent lifecycle event, re-read the agent and upsert\n * it (or remove it when it no longer exists). Best-effort and non-blocking; store faults are\n * swallowed so a mirror outage never breaks the registry. Returns an unsubscribe function.\n *\n * For exactness after a prune, follow up with {@link syncAll}.\n *\n * @param krikos - The registry to observe.\n * @param store - The destination store.\n * @returns An unsubscribe function.\n */\nexport function mirrorRegistry(krikos: Krikos, store: RegistryStore): () => void {\n  return krikos.on((event) => {\n    if (event.agentId === undefined) {\n      return;\n    }\n    const agent = krikos.get(event.agentId);\n    try {\n      const result = agent !== undefined ? store.upsert(agent) : store.remove(event.agentId);\n      if (result instanceof Promise) {\n        result.catch(() => undefined);\n      }\n    } catch {\n      // A mirror fault must never break the registry.\n    }\n  });\n}\n"]}