@atbash/sdk 0.10.13-dev.0 → 0.12.0-dev.0

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/dist/index.mjs CHANGED
@@ -3311,6 +3311,8 @@ var ENV_MAP = {
3311
3311
  agentKey: "ATBASH_AGENT_KEY",
3312
3312
  orgName: "ATBASH_ORG_NAME",
3313
3313
  judgeEndpoint: "ATBASH_ENDPOINT",
3314
+ // Same variable name the Hermes plugin already documents.
3315
+ judgeVerifyPubKey: "ATBASH_JUDGE_VERIFY_PUBKEY",
3314
3316
  blockchainRid: "ATBASH_BLOCKCHAIN_RID",
3315
3317
  provider: "ATBASH_PROVIDER",
3316
3318
  providerModel: "ATBASH_PROVIDER_MODEL",
@@ -3409,11 +3411,18 @@ var Atbash = class _Atbash {
3409
3411
  static environmentLogged = false;
3410
3412
  constructor(privkey, options = {}) {
3411
3413
  this.auth = native.loadAgent(privkey);
3412
- this.endpoint = (options.endpoint ?? DEFAULT_ENDPOINT).replace(/\/+$/, "") || DEFAULT_ENDPOINT;
3414
+ const validated = validateJudgeEndpoint(
3415
+ options.verifyPubKey ? {
3416
+ policy: "self-hosted",
3417
+ endpoint: options.endpoint ?? DEFAULT_ENDPOINT,
3418
+ verifyPubKey: options.verifyPubKey
3419
+ } : { endpoint: options.endpoint }
3420
+ );
3421
+ this.endpoint = validated.url;
3413
3422
  this.nodeUrls = options.nodeUrls ? [...options.nodeUrls] : DEFAULT_CHROMIA_NODE_URLS;
3414
3423
  this.blockchainRid = options.blockchainRid ?? native.DEFAULT_BLOCKCHAIN_RID;
3415
3424
  this.orgName = options.orgName;
3416
- this.verifyPubKey = options.verifyPubKey;
3425
+ this.verifyPubKey = validated.verifyPubKey ?? void 0;
3417
3426
  this.orgEncryptionPubKey = options.orgEncryptionPubKey;
3418
3427
  this.failClosed = options.failClosed !== false;
3419
3428
  this.debug = options.debug === true;
@@ -3471,8 +3480,19 @@ var Atbash = class _Atbash {
3471
3480
  * self-hosted endpoint's `verifyPubKey` becomes the client default.
3472
3481
  */
3473
3482
  static fromConfig(options = {}) {
3483
+ const configuredEndpoint = resolve("judgeEndpoint") || void 0;
3484
+ const configuredVerifyPubKey = resolve("judgeVerifyPubKey") || void 0;
3485
+ if (!options.judge && configuredVerifyPubKey && !configuredEndpoint) {
3486
+ throw new Error(
3487
+ "judgeVerifyPubKey / ATBASH_JUDGE_VERIFY_PUBKEY is set but no judge endpoint is configured: set judgeEndpoint / ATBASH_ENDPOINT to the self-hosted judge"
3488
+ );
3489
+ }
3474
3490
  const validated = validateJudgeEndpoint(
3475
- options.judge ?? { endpoint: resolve("judgeEndpoint") || void 0 }
3491
+ options.judge ?? (configuredVerifyPubKey && configuredEndpoint ? {
3492
+ policy: "self-hosted",
3493
+ endpoint: configuredEndpoint,
3494
+ verifyPubKey: configuredVerifyPubKey
3495
+ } : { endpoint: configuredEndpoint })
3476
3496
  );
3477
3497
  const agentKey = resolve("agentKey", options.agentKey);
3478
3498
  const auth = agentKey ? native.loadAgent(agentKey) : loadAgentFromFile(options.keyPath);
@@ -4028,21 +4048,22 @@ var Atbash = class _Atbash {
4028
4048
  * entry (caller falls back to per-chain subscription resolution).
4029
4049
  */
4030
4050
  async getActiveNetworkForOrg(orgName) {
4051
+ let resp;
4031
4052
  try {
4032
- const resp = await this.http.get(
4053
+ resp = await this.http.get(
4033
4054
  "/api/org-network",
4034
- { org: orgName },
4055
+ { org: orgName.trim() },
4035
4056
  this.authHeaders()
4036
4057
  );
4037
- if (resp.status !== 200) return null;
4038
- const data = await this.json(resp);
4039
- if (data?.network === "public" || data?.network === "private") {
4040
- return data.network;
4041
- }
4042
- return null;
4043
- } catch {
4044
- return null;
4058
+ } catch (err) {
4059
+ throw this.transportError(err);
4045
4060
  }
4061
+ if (resp.status !== 200) throw await this.httpError(resp);
4062
+ const data = await this.json(resp);
4063
+ if (data?.network === "public" || data?.network === "private") {
4064
+ return data.network;
4065
+ }
4066
+ return null;
4046
4067
  }
4047
4068
  /**
4048
4069
  * Resolve which chain an org's actions should run against. Cached
@@ -4054,10 +4075,11 @@ var Atbash = class _Atbash {
4054
4075
  * Defaults to the public chain when nothing else resolves.
4055
4076
  */
4056
4077
  async resolveChainForOrg(orgName) {
4057
- const cached = this._chainCache.get(orgName);
4078
+ const name2 = orgName.trim();
4079
+ const cached = this._chainCache.get(name2);
4058
4080
  if (cached) return cached;
4059
- const mapNetwork = await this.getActiveNetworkForOrg(orgName);
4060
- return this.resolveChainFromMap(orgName, mapNetwork);
4081
+ const mapNetwork = await this.getActiveNetworkForOrg(name2);
4082
+ return this.resolveChainFromMap(name2, mapNetwork);
4061
4083
  }
4062
4084
  /**
4063
4085
  * Resolve a chain given an already-fetched `org_networks` map result.
@@ -4073,29 +4095,38 @@ var Atbash = class _Atbash {
4073
4095
  this._chainCache.set(orgName, chain);
4074
4096
  return chain;
4075
4097
  }
4076
- try {
4077
- const [pubSub, privSub] = await Promise.all([
4078
- this.getOrgSubscription(orgName, "public").catch(() => null),
4079
- this.getOrgSubscription(orgName, "private").catch(() => null)
4080
- ]);
4081
- if (pubSub?.is_private_blockchain) {
4082
- this._chainCache.set(orgName, PRIVATE_CHAIN);
4083
- return PRIVATE_CHAIN;
4084
- }
4085
- if (pubSub && privSub) {
4086
- const chain = privSub.assigned_at > pubSub.assigned_at ? PRIVATE_CHAIN : PUBLIC_CHAIN;
4087
- this._chainCache.set(orgName, chain);
4088
- return chain;
4089
- }
4090
- if (pubSub) {
4091
- this._chainCache.set(orgName, PUBLIC_CHAIN);
4092
- return PUBLIC_CHAIN;
4093
- }
4094
- if (privSub?.is_private_blockchain) {
4095
- this._chainCache.set(orgName, PRIVATE_CHAIN);
4096
- return PRIVATE_CHAIN;
4097
- }
4098
- } catch {
4098
+ const [pubRes, privRes] = await Promise.allSettled([
4099
+ this.getOrgSubscription(orgName, "public"),
4100
+ this.getOrgSubscription(orgName, "private")
4101
+ ]);
4102
+ const pubSub = pubRes.status === "fulfilled" ? pubRes.value : null;
4103
+ const privSub = privRes.status === "fulfilled" ? privRes.value : null;
4104
+ const failure = pubRes.status === "rejected" ? pubRes.reason : privRes.status === "rejected" ? privRes.reason : null;
4105
+ if (pubSub?.is_private_blockchain) {
4106
+ this._chainCache.set(orgName, PRIVATE_CHAIN);
4107
+ return PRIVATE_CHAIN;
4108
+ }
4109
+ if (pubSub && privSub) {
4110
+ const chain = privSub.assigned_at > pubSub.assigned_at ? PRIVATE_CHAIN : PUBLIC_CHAIN;
4111
+ this._chainCache.set(orgName, chain);
4112
+ return chain;
4113
+ }
4114
+ if (pubSub) {
4115
+ this._chainCache.set(orgName, PUBLIC_CHAIN);
4116
+ return PUBLIC_CHAIN;
4117
+ }
4118
+ if (privSub?.is_private_blockchain) {
4119
+ this._chainCache.set(orgName, PRIVATE_CHAIN);
4120
+ return PRIVATE_CHAIN;
4121
+ }
4122
+ if (failure) {
4123
+ const detail = failure instanceof Error ? failure.message : String(failure);
4124
+ throw new AtbashAPIError(
4125
+ failure instanceof AtbashAPIError ? failure.status : 0,
4126
+ `could not resolve the chain for org "${orgName}": ${detail}`,
4127
+ "",
4128
+ this.endpoint
4129
+ );
4099
4130
  }
4100
4131
  this._chainCache.set(orgName, PUBLIC_CHAIN);
4101
4132
  return PUBLIC_CHAIN;
@@ -42664,6 +42695,7 @@ async function resolveChainOptsForOrg(opts, auth) {
42664
42695
  if (opts?.orgName && !opts.chainOpts?.blockchainRid && auth) {
42665
42696
  const atbash = new Atbash(auth.privkey, {
42666
42697
  endpoint: opts.endpoint,
42698
+ verifyPubKey: opts.verifyPubKey,
42667
42699
  orgName: opts.orgName
42668
42700
  });
42669
42701
  const resolved = await atbash.resolveChainForOrg(opts.orgName);
@@ -42703,13 +42735,15 @@ function buildSigner(auth) {
42703
42735
  }
42704
42736
  async function commitMemoryVersion(plaintext, auth, opts) {
42705
42737
  const score = opts?.score ?? 5;
42738
+ const filePath = opts?.filePath ?? "";
42706
42739
  const key3 = await deriveMemoryKey(auth.privkey);
42707
42740
  const { ciphertext, nonce } = await encryptMemoryContent(plaintext, key3);
42708
42741
  const args = native.buildAddAgentMemoryArgs(
42709
42742
  auth.pubkey,
42710
42743
  ciphertext,
42711
42744
  nonce,
42712
- score
42745
+ score,
42746
+ filePath
42713
42747
  );
42714
42748
  const chainOpts = await resolveChainOptsForOrg(opts, auth);
42715
42749
  const client = await buildChainClient(chainOpts);
@@ -42721,7 +42755,8 @@ async function commitMemoryVersion(plaintext, auth, opts) {
42721
42755
  toGtxBytes(args.agent_pubkey),
42722
42756
  toGtxBytes(args.content_cipher),
42723
42757
  toGtxBytes(args.nonce),
42724
- args.score
42758
+ args.score,
42759
+ args.file_path
42725
42760
  ]
42726
42761
  },
42727
42762
  sigProvider
@@ -42743,6 +42778,7 @@ async function decryptRow(row, key3) {
42743
42778
  }
42744
42779
  return {
42745
42780
  id: parsed.id,
42781
+ filePath: parsed.file_path,
42746
42782
  content,
42747
42783
  decryptError,
42748
42784
  score: parsed.score,
@@ -42751,39 +42787,52 @@ async function decryptRow(row, key3) {
42751
42787
  updatedAt: parsed.updated_at
42752
42788
  };
42753
42789
  }
42754
- async function getActiveMemoryId(auth, chainOpts) {
42790
+ function paramsWithOptionalFilePath(agentPubkeyHex, filePath) {
42791
+ const params = {
42792
+ [native.ARG_AGENT_PUBKEY]: agentPubkeyHex
42793
+ };
42794
+ if (filePath !== null) {
42795
+ params[native.ARG_FILE_PATH] = filePath;
42796
+ }
42797
+ return params;
42798
+ }
42799
+ async function getActiveMemoryId(auth, chainOpts, filePath) {
42755
42800
  const client = await buildChainClient(chainOpts);
42756
- const q = native.buildGetActiveMemoryIdQuery(auth.pubkey);
42757
- const raw2 = await client.query(native.QUERY_GET_ACTIVE_MEMORY_ID, {
42758
- [native.ARG_AGENT_PUBKEY]: q.agent_pubkey_hex
42759
- });
42801
+ const q = native.buildGetActiveMemoryIdQuery(auth.pubkey, filePath ?? null);
42802
+ const raw2 = await client.query(
42803
+ native.QUERY_GET_ACTIVE_MEMORY_ID,
42804
+ paramsWithOptionalFilePath(q.agent_pubkey_hex, q.file_path)
42805
+ );
42760
42806
  return native.parseActiveMemoryId(raw2);
42761
42807
  }
42762
- async function getActiveMemory(auth, chainOpts) {
42808
+ async function getRecentAgentMemory(auth, chainOpts, filePath) {
42763
42809
  const client = await buildChainClient(chainOpts);
42764
42810
  const key3 = await deriveMemoryKey(auth.privkey);
42765
- const q = native.buildGetAgentMemoryQuery(auth.pubkey);
42766
- const rows = await client.query(native.QUERY_GET_AGENT_MEMORY, {
42767
- [native.ARG_AGENT_PUBKEY]: q.agent_pubkey_hex
42768
- });
42811
+ const q = native.buildGetRecentAgentMemoryQuery(auth.pubkey, filePath ?? null);
42812
+ const rows = await client.query(
42813
+ native.QUERY_GET_RECENT_AGENT_MEMORY,
42814
+ paramsWithOptionalFilePath(q.agent_pubkey_hex, q.file_path)
42815
+ );
42769
42816
  return Promise.all((rows ?? []).map((r2) => decryptRow(r2, key3)));
42770
42817
  }
42771
- async function getAllAgentMemory(auth, chainOpts) {
42818
+ async function getActiveAgentMemory(auth, chainOpts, filePath) {
42772
42819
  const client = await buildChainClient(chainOpts);
42773
42820
  const key3 = await deriveMemoryKey(auth.privkey);
42774
- const q = native.buildGetAllAgentMemoryQuery(auth.pubkey);
42775
- const rows = await client.query(native.QUERY_GET_ALL_AGENT_MEMORY, {
42776
- [native.ARG_AGENT_PUBKEY]: q.agent_pubkey_hex
42777
- });
42821
+ const q = native.buildGetActiveAgentMemoryQuery(auth.pubkey, filePath ?? null);
42822
+ const rows = await client.query(
42823
+ native.QUERY_GET_ACTIVE_AGENT_MEMORY,
42824
+ paramsWithOptionalFilePath(q.agent_pubkey_hex, q.file_path)
42825
+ );
42778
42826
  return Promise.all((rows ?? []).map((r2) => decryptRow(r2, key3)));
42779
42827
  }
42780
- async function getMemoryHistory(auth, chainOpts) {
42828
+ async function getMemoryHistory(auth, chainOpts, filePath) {
42781
42829
  const client = await buildChainClient(chainOpts);
42782
42830
  const key3 = await deriveMemoryKey(auth.privkey);
42783
- const q = native.buildGetAgentMemoryHistoryQuery(auth.pubkey);
42784
- const rows = await client.query(native.QUERY_GET_AGENT_MEMORY_HISTORY, {
42785
- [native.ARG_AGENT_PUBKEY]: q.agent_pubkey_hex
42786
- });
42831
+ const q = native.buildGetAgentMemoryHistoryQuery(auth.pubkey, filePath ?? null);
42832
+ const rows = await client.query(
42833
+ native.QUERY_GET_AGENT_MEMORY_HISTORY,
42834
+ paramsWithOptionalFilePath(q.agent_pubkey_hex, q.file_path)
42835
+ );
42787
42836
  return Promise.all((rows ?? []).map((r2) => decryptRow(r2, key3)));
42788
42837
  }
42789
42838
  async function getMemoryById(id, auth, chainOpts) {
@@ -42796,17 +42845,21 @@ async function getMemoryById(id, auth, chainOpts) {
42796
42845
  });
42797
42846
  return decryptRow(row, key3);
42798
42847
  }
42799
- async function getRollbackHistory(auth, chainOpts) {
42848
+ async function getRollbackHistory(auth, chainOpts, filePath) {
42800
42849
  const client = await buildChainClient(chainOpts);
42801
- const q = native.buildGetAgentMemoryRollbackHistoryQuery(auth.pubkey);
42850
+ const q = native.buildGetAgentMemoryRollbackHistoryQuery(
42851
+ auth.pubkey,
42852
+ filePath ?? null
42853
+ );
42802
42854
  const raw2 = await client.query(
42803
42855
  native.QUERY_GET_AGENT_MEMORY_ROLLBACK_HISTORY,
42804
- { [native.ARG_AGENT_PUBKEY]: q.agent_pubkey_hex }
42856
+ paramsWithOptionalFilePath(q.agent_pubkey_hex, q.file_path)
42805
42857
  );
42806
42858
  const parsed = native.parseRollbackRows(raw2 ?? []);
42807
42859
  return parsed.map((r2) => ({
42808
42860
  fromId: r2.from_id,
42809
42861
  toId: r2.to_id,
42862
+ filePath: r2.file_path,
42810
42863
  reason: r2.reason,
42811
42864
  signer: Buffer.from(r2.signer).toString("hex"),
42812
42865
  createdAt: r2.created_at
@@ -42874,8 +42927,7 @@ async function guardMemoryWrite(input) {
42874
42927
  toolNames,
42875
42928
  enforce = true,
42876
42929
  debug: debug2 = false,
42877
- logger: logger2,
42878
- memoryFilePath
42930
+ logger: logger2
42879
42931
  } = input;
42880
42932
  const memEntry = classifyMemoryWrite(event, ctx, { patterns, toolNames });
42881
42933
  if (debug2) emitDebugProbe(event, ctx, memEntry, logger2);
@@ -42911,37 +42963,35 @@ async function guardMemoryWrite(input) {
42911
42963
  committed: false
42912
42964
  };
42913
42965
  }
42914
- const isManagedMemoryFile = memoryFilePath !== void 0 && path3.resolve(memEntry.key) === path3.resolve(memoryFilePath);
42915
- if (isManagedMemoryFile) {
42916
- commitMemoryVersion(memEntry.value, auth, {
42917
- score: scanResult.score,
42918
- orgName,
42919
- endpoint
42920
- }).catch((err) => {
42921
- const reason = err instanceof Error ? err.message : String(err);
42922
- logger2?.warn?.("[atbash] memory commit to chain failed", {
42923
- path: memEntry.key,
42924
- reason
42925
- });
42966
+ const filePath = path3.basename(memEntry.key);
42967
+ commitMemoryVersion(memEntry.value, auth, {
42968
+ score: scanResult.score,
42969
+ filePath,
42970
+ orgName,
42971
+ endpoint,
42972
+ verifyPubKey
42973
+ }).catch((err) => {
42974
+ const reason = err instanceof Error ? err.message : String(err);
42975
+ logger2?.warn?.("[atbash] memory commit to chain failed", {
42976
+ path: memEntry.key,
42977
+ filePath,
42978
+ reason
42926
42979
  });
42927
- } else {
42928
- logger2?.info?.(
42929
- "[atbash] scanned but not committed \u2014 not the managed memory file",
42930
- {
42931
- path: memEntry.key,
42932
- memoryFilePath: memoryFilePath ?? "(not configured)"
42933
- }
42934
- );
42935
- }
42980
+ });
42936
42981
  logger2?.info?.(
42937
42982
  scanResult.verdict === "yellow" ? "[atbash] memory HOLD" : "[atbash] memory ALLOW",
42938
- { path: memEntry.key, score: scanResult.score, reason: scanResult.reason }
42983
+ {
42984
+ path: memEntry.key,
42985
+ filePath,
42986
+ score: scanResult.score,
42987
+ reason: scanResult.reason
42988
+ }
42939
42989
  );
42940
42990
  return {
42941
42991
  handled: true,
42942
42992
  decision: { allow: true },
42943
42993
  scanResult,
42944
- committed: isManagedMemoryFile
42994
+ committed: true
42945
42995
  };
42946
42996
  }
42947
42997
 
@@ -43181,9 +43231,7 @@ var MemoryGuardManager = class {
43181
43231
  toolNames: this.opts.memoryWriteToolNames,
43182
43232
  enforce: this.enforce,
43183
43233
  debug: this.opts.debug,
43184
- logger: guardLogger,
43185
- // Only this file may reach the single, path-less chain memory slot.
43186
- memoryFilePath: this.memoryFilePath
43234
+ logger: guardLogger
43187
43235
  });
43188
43236
  return this.mapGuardResult(guard);
43189
43237
  }
@@ -43483,13 +43531,13 @@ export {
43483
43531
  encryptedLength,
43484
43532
  flushTelemetry,
43485
43533
  generateKeypair,
43486
- getActiveMemory,
43534
+ getActiveAgentMemory,
43487
43535
  getActiveMemoryId,
43488
- getAllAgentMemory,
43489
43536
  getConfigDir,
43490
43537
  getConfigPath,
43491
43538
  getMemoryById,
43492
43539
  getMemoryHistory,
43540
+ getRecentAgentMemory,
43493
43541
  getRollbackHistory,
43494
43542
  guardMemoryWrite,
43495
43543
  isEnvelope,