@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.js CHANGED
@@ -2896,13 +2896,13 @@ __export(src_ts_exports, {
2896
2896
  encryptedLength: () => encryptedLength,
2897
2897
  flushTelemetry: () => flushTelemetry,
2898
2898
  generateKeypair: () => generateKeypair,
2899
- getActiveMemory: () => getActiveMemory,
2899
+ getActiveAgentMemory: () => getActiveAgentMemory,
2900
2900
  getActiveMemoryId: () => getActiveMemoryId,
2901
- getAllAgentMemory: () => getAllAgentMemory,
2902
2901
  getConfigDir: () => getConfigDir,
2903
2902
  getConfigPath: () => getConfigPath,
2904
2903
  getMemoryById: () => getMemoryById,
2905
2904
  getMemoryHistory: () => getMemoryHistory,
2905
+ getRecentAgentMemory: () => getRecentAgentMemory,
2906
2906
  getRollbackHistory: () => getRollbackHistory,
2907
2907
  guardMemoryWrite: () => guardMemoryWrite,
2908
2908
  isEnvelope: () => isEnvelope,
@@ -3392,6 +3392,8 @@ var ENV_MAP = {
3392
3392
  agentKey: "ATBASH_AGENT_KEY",
3393
3393
  orgName: "ATBASH_ORG_NAME",
3394
3394
  judgeEndpoint: "ATBASH_ENDPOINT",
3395
+ // Same variable name the Hermes plugin already documents.
3396
+ judgeVerifyPubKey: "ATBASH_JUDGE_VERIFY_PUBKEY",
3395
3397
  blockchainRid: "ATBASH_BLOCKCHAIN_RID",
3396
3398
  provider: "ATBASH_PROVIDER",
3397
3399
  providerModel: "ATBASH_PROVIDER_MODEL",
@@ -3490,11 +3492,18 @@ var Atbash = class _Atbash {
3490
3492
  static environmentLogged = false;
3491
3493
  constructor(privkey, options = {}) {
3492
3494
  this.auth = native.loadAgent(privkey);
3493
- this.endpoint = (options.endpoint ?? DEFAULT_ENDPOINT).replace(/\/+$/, "") || DEFAULT_ENDPOINT;
3495
+ const validated = validateJudgeEndpoint(
3496
+ options.verifyPubKey ? {
3497
+ policy: "self-hosted",
3498
+ endpoint: options.endpoint ?? DEFAULT_ENDPOINT,
3499
+ verifyPubKey: options.verifyPubKey
3500
+ } : { endpoint: options.endpoint }
3501
+ );
3502
+ this.endpoint = validated.url;
3494
3503
  this.nodeUrls = options.nodeUrls ? [...options.nodeUrls] : DEFAULT_CHROMIA_NODE_URLS;
3495
3504
  this.blockchainRid = options.blockchainRid ?? native.DEFAULT_BLOCKCHAIN_RID;
3496
3505
  this.orgName = options.orgName;
3497
- this.verifyPubKey = options.verifyPubKey;
3506
+ this.verifyPubKey = validated.verifyPubKey ?? void 0;
3498
3507
  this.orgEncryptionPubKey = options.orgEncryptionPubKey;
3499
3508
  this.failClosed = options.failClosed !== false;
3500
3509
  this.debug = options.debug === true;
@@ -3552,8 +3561,19 @@ var Atbash = class _Atbash {
3552
3561
  * self-hosted endpoint's `verifyPubKey` becomes the client default.
3553
3562
  */
3554
3563
  static fromConfig(options = {}) {
3564
+ const configuredEndpoint = resolve("judgeEndpoint") || void 0;
3565
+ const configuredVerifyPubKey = resolve("judgeVerifyPubKey") || void 0;
3566
+ if (!options.judge && configuredVerifyPubKey && !configuredEndpoint) {
3567
+ throw new Error(
3568
+ "judgeVerifyPubKey / ATBASH_JUDGE_VERIFY_PUBKEY is set but no judge endpoint is configured: set judgeEndpoint / ATBASH_ENDPOINT to the self-hosted judge"
3569
+ );
3570
+ }
3555
3571
  const validated = validateJudgeEndpoint(
3556
- options.judge ?? { endpoint: resolve("judgeEndpoint") || void 0 }
3572
+ options.judge ?? (configuredVerifyPubKey && configuredEndpoint ? {
3573
+ policy: "self-hosted",
3574
+ endpoint: configuredEndpoint,
3575
+ verifyPubKey: configuredVerifyPubKey
3576
+ } : { endpoint: configuredEndpoint })
3557
3577
  );
3558
3578
  const agentKey = resolve("agentKey", options.agentKey);
3559
3579
  const auth = agentKey ? native.loadAgent(agentKey) : loadAgentFromFile(options.keyPath);
@@ -4109,21 +4129,22 @@ var Atbash = class _Atbash {
4109
4129
  * entry (caller falls back to per-chain subscription resolution).
4110
4130
  */
4111
4131
  async getActiveNetworkForOrg(orgName) {
4132
+ let resp;
4112
4133
  try {
4113
- const resp = await this.http.get(
4134
+ resp = await this.http.get(
4114
4135
  "/api/org-network",
4115
- { org: orgName },
4136
+ { org: orgName.trim() },
4116
4137
  this.authHeaders()
4117
4138
  );
4118
- if (resp.status !== 200) return null;
4119
- const data = await this.json(resp);
4120
- if (data?.network === "public" || data?.network === "private") {
4121
- return data.network;
4122
- }
4123
- return null;
4124
- } catch {
4125
- return null;
4139
+ } catch (err) {
4140
+ throw this.transportError(err);
4126
4141
  }
4142
+ if (resp.status !== 200) throw await this.httpError(resp);
4143
+ const data = await this.json(resp);
4144
+ if (data?.network === "public" || data?.network === "private") {
4145
+ return data.network;
4146
+ }
4147
+ return null;
4127
4148
  }
4128
4149
  /**
4129
4150
  * Resolve which chain an org's actions should run against. Cached
@@ -4135,10 +4156,11 @@ var Atbash = class _Atbash {
4135
4156
  * Defaults to the public chain when nothing else resolves.
4136
4157
  */
4137
4158
  async resolveChainForOrg(orgName) {
4138
- const cached = this._chainCache.get(orgName);
4159
+ const name2 = orgName.trim();
4160
+ const cached = this._chainCache.get(name2);
4139
4161
  if (cached) return cached;
4140
- const mapNetwork = await this.getActiveNetworkForOrg(orgName);
4141
- return this.resolveChainFromMap(orgName, mapNetwork);
4162
+ const mapNetwork = await this.getActiveNetworkForOrg(name2);
4163
+ return this.resolveChainFromMap(name2, mapNetwork);
4142
4164
  }
4143
4165
  /**
4144
4166
  * Resolve a chain given an already-fetched `org_networks` map result.
@@ -4154,29 +4176,38 @@ var Atbash = class _Atbash {
4154
4176
  this._chainCache.set(orgName, chain);
4155
4177
  return chain;
4156
4178
  }
4157
- try {
4158
- const [pubSub, privSub] = await Promise.all([
4159
- this.getOrgSubscription(orgName, "public").catch(() => null),
4160
- this.getOrgSubscription(orgName, "private").catch(() => null)
4161
- ]);
4162
- if (pubSub?.is_private_blockchain) {
4163
- this._chainCache.set(orgName, PRIVATE_CHAIN);
4164
- return PRIVATE_CHAIN;
4165
- }
4166
- if (pubSub && privSub) {
4167
- const chain = privSub.assigned_at > pubSub.assigned_at ? PRIVATE_CHAIN : PUBLIC_CHAIN;
4168
- this._chainCache.set(orgName, chain);
4169
- return chain;
4170
- }
4171
- if (pubSub) {
4172
- this._chainCache.set(orgName, PUBLIC_CHAIN);
4173
- return PUBLIC_CHAIN;
4174
- }
4175
- if (privSub?.is_private_blockchain) {
4176
- this._chainCache.set(orgName, PRIVATE_CHAIN);
4177
- return PRIVATE_CHAIN;
4178
- }
4179
- } catch {
4179
+ const [pubRes, privRes] = await Promise.allSettled([
4180
+ this.getOrgSubscription(orgName, "public"),
4181
+ this.getOrgSubscription(orgName, "private")
4182
+ ]);
4183
+ const pubSub = pubRes.status === "fulfilled" ? pubRes.value : null;
4184
+ const privSub = privRes.status === "fulfilled" ? privRes.value : null;
4185
+ const failure = pubRes.status === "rejected" ? pubRes.reason : privRes.status === "rejected" ? privRes.reason : null;
4186
+ if (pubSub?.is_private_blockchain) {
4187
+ this._chainCache.set(orgName, PRIVATE_CHAIN);
4188
+ return PRIVATE_CHAIN;
4189
+ }
4190
+ if (pubSub && privSub) {
4191
+ const chain = privSub.assigned_at > pubSub.assigned_at ? PRIVATE_CHAIN : PUBLIC_CHAIN;
4192
+ this._chainCache.set(orgName, chain);
4193
+ return chain;
4194
+ }
4195
+ if (pubSub) {
4196
+ this._chainCache.set(orgName, PUBLIC_CHAIN);
4197
+ return PUBLIC_CHAIN;
4198
+ }
4199
+ if (privSub?.is_private_blockchain) {
4200
+ this._chainCache.set(orgName, PRIVATE_CHAIN);
4201
+ return PRIVATE_CHAIN;
4202
+ }
4203
+ if (failure) {
4204
+ const detail = failure instanceof Error ? failure.message : String(failure);
4205
+ throw new AtbashAPIError(
4206
+ failure instanceof AtbashAPIError ? failure.status : 0,
4207
+ `could not resolve the chain for org "${orgName}": ${detail}`,
4208
+ "",
4209
+ this.endpoint
4210
+ );
4180
4211
  }
4181
4212
  this._chainCache.set(orgName, PUBLIC_CHAIN);
4182
4213
  return PUBLIC_CHAIN;
@@ -42745,6 +42776,7 @@ async function resolveChainOptsForOrg(opts, auth) {
42745
42776
  if (opts?.orgName && !opts.chainOpts?.blockchainRid && auth) {
42746
42777
  const atbash = new Atbash(auth.privkey, {
42747
42778
  endpoint: opts.endpoint,
42779
+ verifyPubKey: opts.verifyPubKey,
42748
42780
  orgName: opts.orgName
42749
42781
  });
42750
42782
  const resolved = await atbash.resolveChainForOrg(opts.orgName);
@@ -42784,13 +42816,15 @@ function buildSigner(auth) {
42784
42816
  }
42785
42817
  async function commitMemoryVersion(plaintext, auth, opts) {
42786
42818
  const score = opts?.score ?? 5;
42819
+ const filePath = opts?.filePath ?? "";
42787
42820
  const key3 = await deriveMemoryKey(auth.privkey);
42788
42821
  const { ciphertext, nonce } = await encryptMemoryContent(plaintext, key3);
42789
42822
  const args = native.buildAddAgentMemoryArgs(
42790
42823
  auth.pubkey,
42791
42824
  ciphertext,
42792
42825
  nonce,
42793
- score
42826
+ score,
42827
+ filePath
42794
42828
  );
42795
42829
  const chainOpts = await resolveChainOptsForOrg(opts, auth);
42796
42830
  const client = await buildChainClient(chainOpts);
@@ -42802,7 +42836,8 @@ async function commitMemoryVersion(plaintext, auth, opts) {
42802
42836
  toGtxBytes(args.agent_pubkey),
42803
42837
  toGtxBytes(args.content_cipher),
42804
42838
  toGtxBytes(args.nonce),
42805
- args.score
42839
+ args.score,
42840
+ args.file_path
42806
42841
  ]
42807
42842
  },
42808
42843
  sigProvider
@@ -42824,6 +42859,7 @@ async function decryptRow(row, key3) {
42824
42859
  }
42825
42860
  return {
42826
42861
  id: parsed.id,
42862
+ filePath: parsed.file_path,
42827
42863
  content,
42828
42864
  decryptError,
42829
42865
  score: parsed.score,
@@ -42832,39 +42868,52 @@ async function decryptRow(row, key3) {
42832
42868
  updatedAt: parsed.updated_at
42833
42869
  };
42834
42870
  }
42835
- async function getActiveMemoryId(auth, chainOpts) {
42871
+ function paramsWithOptionalFilePath(agentPubkeyHex, filePath) {
42872
+ const params = {
42873
+ [native.ARG_AGENT_PUBKEY]: agentPubkeyHex
42874
+ };
42875
+ if (filePath !== null) {
42876
+ params[native.ARG_FILE_PATH] = filePath;
42877
+ }
42878
+ return params;
42879
+ }
42880
+ async function getActiveMemoryId(auth, chainOpts, filePath) {
42836
42881
  const client = await buildChainClient(chainOpts);
42837
- const q = native.buildGetActiveMemoryIdQuery(auth.pubkey);
42838
- const raw2 = await client.query(native.QUERY_GET_ACTIVE_MEMORY_ID, {
42839
- [native.ARG_AGENT_PUBKEY]: q.agent_pubkey_hex
42840
- });
42882
+ const q = native.buildGetActiveMemoryIdQuery(auth.pubkey, filePath ?? null);
42883
+ const raw2 = await client.query(
42884
+ native.QUERY_GET_ACTIVE_MEMORY_ID,
42885
+ paramsWithOptionalFilePath(q.agent_pubkey_hex, q.file_path)
42886
+ );
42841
42887
  return native.parseActiveMemoryId(raw2);
42842
42888
  }
42843
- async function getActiveMemory(auth, chainOpts) {
42889
+ async function getRecentAgentMemory(auth, chainOpts, filePath) {
42844
42890
  const client = await buildChainClient(chainOpts);
42845
42891
  const key3 = await deriveMemoryKey(auth.privkey);
42846
- const q = native.buildGetAgentMemoryQuery(auth.pubkey);
42847
- const rows = await client.query(native.QUERY_GET_AGENT_MEMORY, {
42848
- [native.ARG_AGENT_PUBKEY]: q.agent_pubkey_hex
42849
- });
42892
+ const q = native.buildGetRecentAgentMemoryQuery(auth.pubkey, filePath ?? null);
42893
+ const rows = await client.query(
42894
+ native.QUERY_GET_RECENT_AGENT_MEMORY,
42895
+ paramsWithOptionalFilePath(q.agent_pubkey_hex, q.file_path)
42896
+ );
42850
42897
  return Promise.all((rows ?? []).map((r2) => decryptRow(r2, key3)));
42851
42898
  }
42852
- async function getAllAgentMemory(auth, chainOpts) {
42899
+ async function getActiveAgentMemory(auth, chainOpts, filePath) {
42853
42900
  const client = await buildChainClient(chainOpts);
42854
42901
  const key3 = await deriveMemoryKey(auth.privkey);
42855
- const q = native.buildGetAllAgentMemoryQuery(auth.pubkey);
42856
- const rows = await client.query(native.QUERY_GET_ALL_AGENT_MEMORY, {
42857
- [native.ARG_AGENT_PUBKEY]: q.agent_pubkey_hex
42858
- });
42902
+ const q = native.buildGetActiveAgentMemoryQuery(auth.pubkey, filePath ?? null);
42903
+ const rows = await client.query(
42904
+ native.QUERY_GET_ACTIVE_AGENT_MEMORY,
42905
+ paramsWithOptionalFilePath(q.agent_pubkey_hex, q.file_path)
42906
+ );
42859
42907
  return Promise.all((rows ?? []).map((r2) => decryptRow(r2, key3)));
42860
42908
  }
42861
- async function getMemoryHistory(auth, chainOpts) {
42909
+ async function getMemoryHistory(auth, chainOpts, filePath) {
42862
42910
  const client = await buildChainClient(chainOpts);
42863
42911
  const key3 = await deriveMemoryKey(auth.privkey);
42864
- const q = native.buildGetAgentMemoryHistoryQuery(auth.pubkey);
42865
- const rows = await client.query(native.QUERY_GET_AGENT_MEMORY_HISTORY, {
42866
- [native.ARG_AGENT_PUBKEY]: q.agent_pubkey_hex
42867
- });
42912
+ const q = native.buildGetAgentMemoryHistoryQuery(auth.pubkey, filePath ?? null);
42913
+ const rows = await client.query(
42914
+ native.QUERY_GET_AGENT_MEMORY_HISTORY,
42915
+ paramsWithOptionalFilePath(q.agent_pubkey_hex, q.file_path)
42916
+ );
42868
42917
  return Promise.all((rows ?? []).map((r2) => decryptRow(r2, key3)));
42869
42918
  }
42870
42919
  async function getMemoryById(id, auth, chainOpts) {
@@ -42877,17 +42926,21 @@ async function getMemoryById(id, auth, chainOpts) {
42877
42926
  });
42878
42927
  return decryptRow(row, key3);
42879
42928
  }
42880
- async function getRollbackHistory(auth, chainOpts) {
42929
+ async function getRollbackHistory(auth, chainOpts, filePath) {
42881
42930
  const client = await buildChainClient(chainOpts);
42882
- const q = native.buildGetAgentMemoryRollbackHistoryQuery(auth.pubkey);
42931
+ const q = native.buildGetAgentMemoryRollbackHistoryQuery(
42932
+ auth.pubkey,
42933
+ filePath ?? null
42934
+ );
42883
42935
  const raw2 = await client.query(
42884
42936
  native.QUERY_GET_AGENT_MEMORY_ROLLBACK_HISTORY,
42885
- { [native.ARG_AGENT_PUBKEY]: q.agent_pubkey_hex }
42937
+ paramsWithOptionalFilePath(q.agent_pubkey_hex, q.file_path)
42886
42938
  );
42887
42939
  const parsed = native.parseRollbackRows(raw2 ?? []);
42888
42940
  return parsed.map((r2) => ({
42889
42941
  fromId: r2.from_id,
42890
42942
  toId: r2.to_id,
42943
+ filePath: r2.file_path,
42891
42944
  reason: r2.reason,
42892
42945
  signer: Buffer.from(r2.signer).toString("hex"),
42893
42946
  createdAt: r2.created_at
@@ -42955,8 +43008,7 @@ async function guardMemoryWrite(input) {
42955
43008
  toolNames,
42956
43009
  enforce = true,
42957
43010
  debug: debug2 = false,
42958
- logger: logger2,
42959
- memoryFilePath
43011
+ logger: logger2
42960
43012
  } = input;
42961
43013
  const memEntry = classifyMemoryWrite(event, ctx, { patterns, toolNames });
42962
43014
  if (debug2) emitDebugProbe(event, ctx, memEntry, logger2);
@@ -42992,37 +43044,35 @@ async function guardMemoryWrite(input) {
42992
43044
  committed: false
42993
43045
  };
42994
43046
  }
42995
- const isManagedMemoryFile = memoryFilePath !== void 0 && import_node_path4.default.resolve(memEntry.key) === import_node_path4.default.resolve(memoryFilePath);
42996
- if (isManagedMemoryFile) {
42997
- commitMemoryVersion(memEntry.value, auth, {
42998
- score: scanResult.score,
42999
- orgName,
43000
- endpoint
43001
- }).catch((err) => {
43002
- const reason = err instanceof Error ? err.message : String(err);
43003
- logger2?.warn?.("[atbash] memory commit to chain failed", {
43004
- path: memEntry.key,
43005
- reason
43006
- });
43047
+ const filePath = import_node_path4.default.basename(memEntry.key);
43048
+ commitMemoryVersion(memEntry.value, auth, {
43049
+ score: scanResult.score,
43050
+ filePath,
43051
+ orgName,
43052
+ endpoint,
43053
+ verifyPubKey
43054
+ }).catch((err) => {
43055
+ const reason = err instanceof Error ? err.message : String(err);
43056
+ logger2?.warn?.("[atbash] memory commit to chain failed", {
43057
+ path: memEntry.key,
43058
+ filePath,
43059
+ reason
43007
43060
  });
43008
- } else {
43009
- logger2?.info?.(
43010
- "[atbash] scanned but not committed \u2014 not the managed memory file",
43011
- {
43012
- path: memEntry.key,
43013
- memoryFilePath: memoryFilePath ?? "(not configured)"
43014
- }
43015
- );
43016
- }
43061
+ });
43017
43062
  logger2?.info?.(
43018
43063
  scanResult.verdict === "yellow" ? "[atbash] memory HOLD" : "[atbash] memory ALLOW",
43019
- { path: memEntry.key, score: scanResult.score, reason: scanResult.reason }
43064
+ {
43065
+ path: memEntry.key,
43066
+ filePath,
43067
+ score: scanResult.score,
43068
+ reason: scanResult.reason
43069
+ }
43020
43070
  );
43021
43071
  return {
43022
43072
  handled: true,
43023
43073
  decision: { allow: true },
43024
43074
  scanResult,
43025
- committed: isManagedMemoryFile
43075
+ committed: true
43026
43076
  };
43027
43077
  }
43028
43078
 
@@ -43262,9 +43312,7 @@ var MemoryGuardManager = class {
43262
43312
  toolNames: this.opts.memoryWriteToolNames,
43263
43313
  enforce: this.enforce,
43264
43314
  debug: this.opts.debug,
43265
- logger: guardLogger,
43266
- // Only this file may reach the single, path-less chain memory slot.
43267
- memoryFilePath: this.memoryFilePath
43315
+ logger: guardLogger
43268
43316
  });
43269
43317
  return this.mapGuardResult(guard);
43270
43318
  }
@@ -43565,13 +43613,13 @@ function diffMemorySnapshots(before, after) {
43565
43613
  encryptedLength,
43566
43614
  flushTelemetry,
43567
43615
  generateKeypair,
43568
- getActiveMemory,
43616
+ getActiveAgentMemory,
43569
43617
  getActiveMemoryId,
43570
- getAllAgentMemory,
43571
43618
  getConfigDir,
43572
43619
  getConfigPath,
43573
43620
  getMemoryById,
43574
43621
  getMemoryHistory,
43622
+ getRecentAgentMemory,
43575
43623
  getRollbackHistory,
43576
43624
  guardMemoryWrite,
43577
43625
  isEnvelope,