@atbash/sdk 0.9.1 → 0.9.2
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/browser.d.mts +23 -13
- package/dist/browser.mjs +96 -206
- package/dist/index.d.mts +23 -13
- package/dist/index.d.ts +23 -13
- package/dist/index.js +173 -73
- package/dist/index.mjs +177 -74
- package/package.json +6 -6
package/dist/index.mjs
CHANGED
|
@@ -2930,6 +2930,13 @@ function buildAllowedJudgeHosts() {
|
|
|
2930
2930
|
]);
|
|
2931
2931
|
}
|
|
2932
2932
|
var ALLOWED_JUDGE_HOSTS = buildAllowedJudgeHosts();
|
|
2933
|
+
function isAllowlistedJudgeHost(endpoint) {
|
|
2934
|
+
try {
|
|
2935
|
+
return ALLOWED_JUDGE_HOSTS.has(new URL(endpoint).hostname.toLowerCase());
|
|
2936
|
+
} catch {
|
|
2937
|
+
return false;
|
|
2938
|
+
}
|
|
2939
|
+
}
|
|
2933
2940
|
function validateJudgeEndpoint(judge) {
|
|
2934
2941
|
const policy = judge?.policy === "self-hosted" ? "self-hosted" : "default";
|
|
2935
2942
|
const candidate = judge?.endpoint?.trim() || DEFAULT_ENDPOINT;
|
|
@@ -3258,7 +3265,10 @@ function canonicalAllow(data) {
|
|
|
3258
3265
|
import { readFileSync as readFileSync2 } from "fs";
|
|
3259
3266
|
import { homedir as homedir2 } from "os";
|
|
3260
3267
|
import { join as join2 } from "path";
|
|
3261
|
-
import {
|
|
3268
|
+
import {
|
|
3269
|
+
AggregationTemporalityPreference,
|
|
3270
|
+
OTLPMetricExporter
|
|
3271
|
+
} from "@opentelemetry/exporter-metrics-otlp-http";
|
|
3262
3272
|
import { resourceFromAttributes } from "@opentelemetry/resources";
|
|
3263
3273
|
import {
|
|
3264
3274
|
MeterProvider,
|
|
@@ -3269,6 +3279,7 @@ var callCounter = null;
|
|
|
3269
3279
|
var durationHistogram = null;
|
|
3270
3280
|
var defaultSource = "sdk";
|
|
3271
3281
|
var sourceExplicitlySet = false;
|
|
3282
|
+
var requestedExportIntervalMs;
|
|
3272
3283
|
function isTelemetryOptedOut() {
|
|
3273
3284
|
const disabled = process.env.ATBASH_TELEMETRY_DISABLED?.trim().toLowerCase();
|
|
3274
3285
|
if (disabled && ["1", "true", "yes", "on"].includes(disabled)) {
|
|
@@ -3297,18 +3308,36 @@ function setupTelemetry(config2) {
|
|
|
3297
3308
|
defaultSource = config2.source;
|
|
3298
3309
|
sourceExplicitlySet = true;
|
|
3299
3310
|
}
|
|
3311
|
+
if (config2.exportIntervalMs !== void 0 && requestedExportIntervalMs === void 0) {
|
|
3312
|
+
requestedExportIntervalMs = config2.exportIntervalMs;
|
|
3313
|
+
}
|
|
3300
3314
|
if (meterProvider) return;
|
|
3301
3315
|
if (!config2.endpoint || !config2.getAuthHeaders) return;
|
|
3302
|
-
if (/^https?:\/\/(localhost|127\.0\.0\.1|0\.0\.0\.0)(:|\/|$)/i.test(
|
|
3316
|
+
if (/^https?:\/\/(localhost|127\.0\.0\.1|0\.0\.0\.0)(:|\/|$)/i.test(
|
|
3317
|
+
config2.endpoint
|
|
3318
|
+
))
|
|
3319
|
+
return;
|
|
3303
3320
|
const proxyUrl = `${config2.endpoint.replace(/\/+$/, "")}/api/telemetry`;
|
|
3304
3321
|
const getAuthHeaders = config2.getAuthHeaders;
|
|
3305
3322
|
const exporter = new OTLPMetricExporter({
|
|
3306
3323
|
url: proxyUrl,
|
|
3307
|
-
headers: async () => getAuthHeaders()
|
|
3324
|
+
headers: async () => getAuthHeaders(),
|
|
3325
|
+
// DELTA, not the OTel default of CUMULATIVE.
|
|
3326
|
+
//
|
|
3327
|
+
// A cumulative counter reports a running total, and the backend recovers
|
|
3328
|
+
// the increment by differencing consecutive points of a series. That needs
|
|
3329
|
+
// at least two points. Most Atbash callers are short-lived — a CLI run
|
|
3330
|
+
// exports once and exits — so their series has exactly one point, nothing
|
|
3331
|
+
// to difference against, and the usage board reads them as zero: the calls
|
|
3332
|
+
// arrive, carry the right `source`, and still count for nothing.
|
|
3333
|
+
//
|
|
3334
|
+
// A delta counter reports the increment itself, so a single export is a
|
|
3335
|
+
// whole truthful datapoint and SUM over any window is the real call count.
|
|
3336
|
+
temporalityPreference: AggregationTemporalityPreference.DELTA
|
|
3308
3337
|
});
|
|
3309
3338
|
const reader = new PeriodicExportingMetricReader({
|
|
3310
3339
|
exporter,
|
|
3311
|
-
exportIntervalMillis:
|
|
3340
|
+
exportIntervalMillis: requestedExportIntervalMs ?? 6e4
|
|
3312
3341
|
});
|
|
3313
3342
|
meterProvider = new MeterProvider({
|
|
3314
3343
|
resource: resourceFromAttributes({
|
|
@@ -3354,6 +3383,7 @@ async function shutdownTelemetry() {
|
|
|
3354
3383
|
durationHistogram = null;
|
|
3355
3384
|
defaultSource = "sdk";
|
|
3356
3385
|
sourceExplicitlySet = false;
|
|
3386
|
+
requestedExportIntervalMs = void 0;
|
|
3357
3387
|
}
|
|
3358
3388
|
|
|
3359
3389
|
// src-ts/userConfig.ts
|
|
@@ -3547,16 +3577,23 @@ var Atbash = class _Atbash {
|
|
|
3547
3577
|
verifying: this.verifyPubKey ? "with response-signature pubkey configured" : "without signature verification"
|
|
3548
3578
|
});
|
|
3549
3579
|
}
|
|
3550
|
-
|
|
3551
|
-
|
|
3552
|
-
|
|
3553
|
-
|
|
3554
|
-
|
|
3555
|
-
|
|
3556
|
-
|
|
3557
|
-
|
|
3558
|
-
|
|
3559
|
-
|
|
3580
|
+
if (isAllowlistedJudgeHost(this.endpoint)) {
|
|
3581
|
+
try {
|
|
3582
|
+
setupTelemetry({
|
|
3583
|
+
enabled: true,
|
|
3584
|
+
endpoint: this.endpoint,
|
|
3585
|
+
getAuthHeaders: () => this.authHeaders()
|
|
3586
|
+
});
|
|
3587
|
+
} catch (err) {
|
|
3588
|
+
this.logger.warn?.(
|
|
3589
|
+
"[atbash] telemetry setup failed \u2014 continuing without metrics",
|
|
3590
|
+
{ error: String(err) }
|
|
3591
|
+
);
|
|
3592
|
+
}
|
|
3593
|
+
} else {
|
|
3594
|
+
this.logger.info?.(
|
|
3595
|
+
"[atbash] telemetry disabled \u2014 the agent bearer is only sent to hosts that already receive it, and this judge endpoint is not one",
|
|
3596
|
+
{ endpoint: this.endpoint }
|
|
3560
3597
|
);
|
|
3561
3598
|
}
|
|
3562
3599
|
}
|
|
@@ -42954,7 +42991,7 @@ async function commitMemoryVersion(plaintext, auth, opts) {
|
|
|
42954
42991
|
const chainOpts = await resolveChainOptsForOrg(opts, auth);
|
|
42955
42992
|
const client = await buildChainClient(chainOpts);
|
|
42956
42993
|
const { sigProvider } = buildSigner(auth);
|
|
42957
|
-
await client.signAndSendUniqueTransaction(
|
|
42994
|
+
const receipt = await client.signAndSendUniqueTransaction(
|
|
42958
42995
|
{
|
|
42959
42996
|
name: native.OP_ADD_AGENT_MEMORY,
|
|
42960
42997
|
args: [
|
|
@@ -42967,6 +43004,9 @@ async function commitMemoryVersion(plaintext, auth, opts) {
|
|
|
42967
43004
|
},
|
|
42968
43005
|
sigProvider
|
|
42969
43006
|
);
|
|
43007
|
+
if (receipt.status !== index.ResponseStatus.Confirmed) {
|
|
43008
|
+
throw new Error(`Memory transaction was not confirmed (status=${receipt.status})`);
|
|
43009
|
+
}
|
|
42970
43010
|
}
|
|
42971
43011
|
async function decryptRow(row, key3) {
|
|
42972
43012
|
const parsed = native.parseMemoryRow(gtvToFfiSafe(row));
|
|
@@ -43076,13 +43116,16 @@ async function rollbackMemory(toId, reason, auth, opts) {
|
|
|
43076
43116
|
const chainOpts = await resolveChainOptsForOrg(opts, auth);
|
|
43077
43117
|
const client = await buildChainClient(chainOpts);
|
|
43078
43118
|
const { sigProvider } = buildSigner(auth);
|
|
43079
|
-
await client.signAndSendUniqueTransaction(
|
|
43119
|
+
const receipt = await client.signAndSendUniqueTransaction(
|
|
43080
43120
|
{
|
|
43081
43121
|
name: native.OP_ROLLBACK_AGENT_MEMORY,
|
|
43082
43122
|
args: [toGtxBytes(args.agent_pubkey), args.to_id, args.reason]
|
|
43083
43123
|
},
|
|
43084
43124
|
sigProvider
|
|
43085
43125
|
);
|
|
43126
|
+
if (receipt.status !== index.ResponseStatus.Confirmed) {
|
|
43127
|
+
throw new Error(`Memory rollback was not confirmed (status=${receipt.status})`);
|
|
43128
|
+
}
|
|
43086
43129
|
}
|
|
43087
43130
|
|
|
43088
43131
|
// src-ts/memory/_json_safe.ts
|
|
@@ -43221,6 +43264,7 @@ async function guardMemoryWrite(input) {
|
|
|
43221
43264
|
endpoint,
|
|
43222
43265
|
verifyPubKey,
|
|
43223
43266
|
orgName,
|
|
43267
|
+
chainOpts,
|
|
43224
43268
|
threshold,
|
|
43225
43269
|
patterns,
|
|
43226
43270
|
toolNames,
|
|
@@ -43277,23 +43321,41 @@ async function guardMemoryWrite(input) {
|
|
|
43277
43321
|
committed: false
|
|
43278
43322
|
};
|
|
43279
43323
|
}
|
|
43324
|
+
if (scanResult.verdict !== "green" && scanResult.verdict !== "yellow") {
|
|
43325
|
+
const reason = "Memory scan returned an unrecognized verdict.";
|
|
43326
|
+
return {
|
|
43327
|
+
handled: true,
|
|
43328
|
+
decision: enforce ? { allow: false, block: true, reason } : { allow: true, reason },
|
|
43329
|
+
scanResult,
|
|
43330
|
+
committed: false
|
|
43331
|
+
};
|
|
43332
|
+
}
|
|
43280
43333
|
const filePath = path3.basename(memEntry.key);
|
|
43281
|
-
|
|
43282
|
-
|
|
43283
|
-
|
|
43284
|
-
|
|
43285
|
-
|
|
43286
|
-
|
|
43287
|
-
|
|
43334
|
+
try {
|
|
43335
|
+
await commitMemoryVersion(memEntry.value, auth, {
|
|
43336
|
+
score: scanResult.score,
|
|
43337
|
+
filePath,
|
|
43338
|
+
orgName,
|
|
43339
|
+
endpoint,
|
|
43340
|
+
verifyPubKey,
|
|
43341
|
+
chainOpts
|
|
43342
|
+
});
|
|
43343
|
+
} catch (err) {
|
|
43288
43344
|
const reason = err instanceof Error ? err.message : String(err);
|
|
43289
43345
|
logger2?.warn?.("[atbash] memory commit to chain failed", {
|
|
43290
43346
|
path: memEntry.key,
|
|
43291
43347
|
filePath,
|
|
43292
43348
|
reason
|
|
43293
43349
|
});
|
|
43294
|
-
|
|
43350
|
+
return {
|
|
43351
|
+
handled: true,
|
|
43352
|
+
decision: enforce ? { allow: false, block: true, reason: `Memory commit failed: ${reason}` } : { allow: true, reason: `Memory commit failed: ${reason}` },
|
|
43353
|
+
scanResult,
|
|
43354
|
+
committed: false
|
|
43355
|
+
};
|
|
43356
|
+
}
|
|
43295
43357
|
logger2?.info?.(
|
|
43296
|
-
scanResult.verdict === "yellow" ? "[atbash] memory
|
|
43358
|
+
scanResult.verdict === "yellow" ? "[atbash] memory ALLOW (yellow, low score)" : "[atbash] memory ALLOW",
|
|
43297
43359
|
{
|
|
43298
43360
|
path: memEntry.key,
|
|
43299
43361
|
filePath,
|
|
@@ -43326,7 +43388,7 @@ async function syncLocalMemory(auth, pointer, opts = {}) {
|
|
|
43326
43388
|
if (withinTtl) {
|
|
43327
43389
|
return { drifted: false, checked: false, pointer };
|
|
43328
43390
|
}
|
|
43329
|
-
const currentId = await getActiveMemoryId(auth, opts.chainOpts);
|
|
43391
|
+
const currentId = await getActiveMemoryId(auth, opts.chainOpts, opts.filePath);
|
|
43330
43392
|
const nextPointer = { activeId: currentId, checkedAt: now };
|
|
43331
43393
|
if (currentId === pointer.activeId) {
|
|
43332
43394
|
return { drifted: false, checked: true, pointer: nextPointer };
|
|
@@ -43408,13 +43470,16 @@ function formatMeta(meta) {
|
|
|
43408
43470
|
}
|
|
43409
43471
|
function createFileLogger(filePath, upstream) {
|
|
43410
43472
|
let queue = Promise.resolve();
|
|
43473
|
+
let writeError;
|
|
43411
43474
|
async function ensureDir() {
|
|
43412
43475
|
await fs2.mkdir(path5.dirname(filePath), { recursive: true });
|
|
43413
43476
|
}
|
|
43414
43477
|
function append(level, message, meta) {
|
|
43415
43478
|
const line = `${(/* @__PURE__ */ new Date()).toISOString()} [${level}] ${message}${formatMeta(meta)}
|
|
43416
43479
|
`;
|
|
43417
|
-
queue = queue.then(ensureDir).then(() => fs2.appendFile(filePath, line, "utf8")).catch(() => {
|
|
43480
|
+
queue = queue.then(ensureDir).then(() => fs2.appendFile(filePath, line, "utf8")).catch((error4) => {
|
|
43481
|
+
writeError = error4 instanceof Error ? error4 : new Error(String(error4));
|
|
43482
|
+
upstream?.warn("[atbash] plugin log write failed", { error: writeError.message });
|
|
43418
43483
|
});
|
|
43419
43484
|
}
|
|
43420
43485
|
return {
|
|
@@ -43425,6 +43490,10 @@ function createFileLogger(filePath, upstream) {
|
|
|
43425
43490
|
warn(message, meta) {
|
|
43426
43491
|
upstream?.warn(message, meta ?? {});
|
|
43427
43492
|
append("warn", message, meta);
|
|
43493
|
+
},
|
|
43494
|
+
async flush() {
|
|
43495
|
+
await queue;
|
|
43496
|
+
if (writeError) throw writeError;
|
|
43428
43497
|
}
|
|
43429
43498
|
};
|
|
43430
43499
|
}
|
|
@@ -43514,7 +43583,15 @@ var MemoryGuardManager = class {
|
|
|
43514
43583
|
orgName: this.opts.orgName
|
|
43515
43584
|
});
|
|
43516
43585
|
const resolved = await atbash.resolveChainForOrg(this.opts.orgName);
|
|
43517
|
-
|
|
43586
|
+
if (!await atbash.checkAgentExists(this.agentPubkeyHex, { network: resolved.network })) {
|
|
43587
|
+
throw new Error(`agent is not registered on the resolved ${resolved.network} chain`);
|
|
43588
|
+
}
|
|
43589
|
+
const chainOpts = {
|
|
43590
|
+
...this.opts.chainOpts,
|
|
43591
|
+
network: resolved.network,
|
|
43592
|
+
blockchainRid: resolved.blockchainRid,
|
|
43593
|
+
nodeUrls: this.opts.chainOpts?.nodeUrls ?? resolved.nodeUrls
|
|
43594
|
+
};
|
|
43518
43595
|
this._resolvedChainOpts = chainOpts;
|
|
43519
43596
|
this.logger.info(
|
|
43520
43597
|
`[atbash] resolved org \u2192 chain \u2014 org=${this.opts.orgName} network=${resolved.network}`
|
|
@@ -43523,9 +43600,9 @@ var MemoryGuardManager = class {
|
|
|
43523
43600
|
} catch (err) {
|
|
43524
43601
|
const msg = err instanceof Error ? err.message : String(err);
|
|
43525
43602
|
this.logger.warn(
|
|
43526
|
-
`[atbash] org \u2192 chain resolution failed (
|
|
43603
|
+
`[atbash] org \u2192 chain resolution failed (memory read will be blocked): ${msg}`
|
|
43527
43604
|
);
|
|
43528
|
-
|
|
43605
|
+
throw err;
|
|
43529
43606
|
} finally {
|
|
43530
43607
|
this._resolveChainInflight = void 0;
|
|
43531
43608
|
}
|
|
@@ -43544,13 +43621,18 @@ var MemoryGuardManager = class {
|
|
|
43544
43621
|
const result = await syncLocalMemory(this.opts.auth, seed, {
|
|
43545
43622
|
ttlMs: 0,
|
|
43546
43623
|
force: true,
|
|
43547
|
-
chainOpts
|
|
43624
|
+
chainOpts,
|
|
43625
|
+
filePath: path6.basename(this.memoryFilePath)
|
|
43548
43626
|
});
|
|
43549
43627
|
if (!result.drifted && result.pointer.activeId == null) {
|
|
43550
43628
|
this.logger.info(
|
|
43551
43629
|
`[atbash] no active memory on chain for agent=${this.agentPubkeyHex.slice(0, 16)}\u2026 org=${this.opts.orgName ?? "(none)"} \u2014 either the agent isn't registered on this chain or hasn't written any memory. Sync will remain a no-op until a write lands.`
|
|
43552
43630
|
);
|
|
43553
43631
|
} else if (result.drifted && result.current) {
|
|
43632
|
+
if (result.current.filePath !== path6.basename(this.memoryFilePath) || result.current.isActive !== true || result.current.decryptError) {
|
|
43633
|
+
this.logger.warn("[atbash] boot sync REFUSED mismatched memory version");
|
|
43634
|
+
return;
|
|
43635
|
+
}
|
|
43554
43636
|
if (result.current.score < this.rollbackMinScore) {
|
|
43555
43637
|
this.logger.warn(
|
|
43556
43638
|
`[atbash] boot sync REFUSED refresh \u2014 id=${result.current.id} score=${result.current.score} below threshold ${this.rollbackMinScore}. Leaving MEMORY.md and pointer untouched; next memory read will be blocked.`
|
|
@@ -43564,7 +43646,8 @@ var MemoryGuardManager = class {
|
|
|
43564
43646
|
await this.writeMemoryAtomic(result.current.content);
|
|
43565
43647
|
} catch (err) {
|
|
43566
43648
|
const msg = err instanceof Error ? err.message : String(err);
|
|
43567
|
-
this.logger.warn("[atbash] boot sync write failed
|
|
43649
|
+
this.logger.warn("[atbash] boot sync write failed; pointer remains unchanged", { error: msg });
|
|
43650
|
+
return;
|
|
43568
43651
|
}
|
|
43569
43652
|
}
|
|
43570
43653
|
await this.pointerStore.set(this.agentPubkeyHex, result.pointer);
|
|
@@ -43578,9 +43661,9 @@ var MemoryGuardManager = class {
|
|
|
43578
43661
|
}
|
|
43579
43662
|
/**
|
|
43580
43663
|
* Returns a `HookDecision` when the guard reached a decision about this event.
|
|
43581
|
-
* Returns `null`
|
|
43582
|
-
*
|
|
43583
|
-
*
|
|
43664
|
+
* Returns `null` for non-memory events, other files this manager does not
|
|
43665
|
+
* own, or unverified observe-mode reads. An enforced read of the managed
|
|
43666
|
+
* file blocks when verification cannot complete.
|
|
43584
43667
|
*
|
|
43585
43668
|
* A returned decision carries `audited` (see `HookDecision`). Only
|
|
43586
43669
|
* `{ allow: true, audited: true }` means "checked and cleared"; anything else
|
|
@@ -43616,6 +43699,7 @@ var MemoryGuardManager = class {
|
|
|
43616
43699
|
endpoint: this.opts.judgeEndpoint,
|
|
43617
43700
|
verifyPubKey: this.opts.judgeVerifyPubKey,
|
|
43618
43701
|
orgName: this.opts.orgName,
|
|
43702
|
+
chainOpts: this.opts.chainOpts,
|
|
43619
43703
|
patterns: this.opts.memoryPathPatterns,
|
|
43620
43704
|
toolNames: this.opts.memoryWriteToolNames,
|
|
43621
43705
|
enforce: this.enforce,
|
|
@@ -43660,6 +43744,14 @@ var MemoryGuardManager = class {
|
|
|
43660
43744
|
reason: `memory scan returned ${sr2.verdict} but this guard is not enforcing it`
|
|
43661
43745
|
};
|
|
43662
43746
|
}
|
|
43747
|
+
if (guard.committed !== true) {
|
|
43748
|
+
return {
|
|
43749
|
+
allow: true,
|
|
43750
|
+
audited: false,
|
|
43751
|
+
verdict: sr2.verdict,
|
|
43752
|
+
reason: "memory scan passed but chain commit did not confirm"
|
|
43753
|
+
};
|
|
43754
|
+
}
|
|
43663
43755
|
return { allow: true, audited: true, verdict: sr2.verdict };
|
|
43664
43756
|
}
|
|
43665
43757
|
/**
|
|
@@ -43694,62 +43786,73 @@ var MemoryGuardManager = class {
|
|
|
43694
43786
|
return found.every((p) => path6.resolve(p) === managed);
|
|
43695
43787
|
}
|
|
43696
43788
|
async handleMemoryRead(event, ctx) {
|
|
43789
|
+
if (!this.vouchesForTarget(event, ctx)) return null;
|
|
43697
43790
|
const pointer = await this.pointerStore.get(this.agentPubkeyHex);
|
|
43698
43791
|
let result;
|
|
43792
|
+
let chainOpts;
|
|
43699
43793
|
try {
|
|
43700
|
-
|
|
43794
|
+
chainOpts = await this.resolveChainOpts();
|
|
43701
43795
|
result = await syncLocalMemory(this.opts.auth, pointer, {
|
|
43702
43796
|
ttlMs: this.ttlMs,
|
|
43703
|
-
|
|
43797
|
+
force: this.enforce,
|
|
43798
|
+
chainOpts,
|
|
43799
|
+
filePath: path6.basename(this.memoryFilePath)
|
|
43704
43800
|
});
|
|
43705
43801
|
} catch (err) {
|
|
43706
43802
|
if (err instanceof MemoryIntegrityError) {
|
|
43707
43803
|
const reason = `Memory integrity check failed on id ${err.id} \u2014 read blocked.`;
|
|
43708
43804
|
this.logger.warn("[atbash] MEMORY INTEGRITY FAILURE", { id: err.id, error: err.message });
|
|
43709
|
-
|
|
43710
|
-
return { block: true, blockReason: reason, allow: false, reason, audited: true };
|
|
43805
|
+
return this.unverifiedRead(reason);
|
|
43711
43806
|
}
|
|
43712
43807
|
const msg = err instanceof Error ? err.message : String(err);
|
|
43713
|
-
this.logger.warn("[atbash] memory sync failed
|
|
43714
|
-
return
|
|
43808
|
+
this.logger.warn("[atbash] memory sync failed", { error: msg });
|
|
43809
|
+
return this.unverifiedRead(`Memory chain sync failed: ${msg}`);
|
|
43715
43810
|
}
|
|
43716
|
-
|
|
43717
|
-
|
|
43718
|
-
|
|
43719
|
-
|
|
43720
|
-
if (fresh.score < this.rollbackMinScore) {
|
|
43721
|
-
const reason = `Rolled-back memory version #${fresh.id} scored ${fresh.score} (below threshold ${this.rollbackMinScore}) \u2014 read blocked.`;
|
|
43722
|
-
this.logger.warn("[atbash] blocking read on low-score rollback", { id: fresh.id, score: fresh.score });
|
|
43723
|
-
if (!this.enforce) return null;
|
|
43724
|
-
return { block: true, blockReason: reason, allow: false, reason, audited: true };
|
|
43725
|
-
}
|
|
43726
|
-
this.logger.info("[atbash] memory drift detected \u2014 refreshing local file", {
|
|
43727
|
-
id: fresh.id,
|
|
43728
|
-
score: fresh.score
|
|
43729
|
-
});
|
|
43730
|
-
try {
|
|
43731
|
-
await this.writeMemoryAtomic(fresh.content);
|
|
43732
|
-
} catch (err) {
|
|
43733
|
-
const msg = err instanceof Error ? err.message : String(err);
|
|
43734
|
-
this.logger.warn("[atbash] failed to write refreshed memory (serving old)", { error: msg });
|
|
43735
|
-
onDiskIsCurrent = false;
|
|
43736
|
-
}
|
|
43737
|
-
} else {
|
|
43738
|
-
this.logger.info("[atbash] active memory removed on chain", { pubkey: this.agentPubkeyHex });
|
|
43739
|
-
onDiskIsCurrent = false;
|
|
43740
|
-
}
|
|
43811
|
+
if (!result.checked) return this.unverifiedRead("Memory chain state was not checked.");
|
|
43812
|
+
const activeId = result.pointer.activeId;
|
|
43813
|
+
if (activeId === null) {
|
|
43814
|
+
return this.unverifiedRead("No active chain version exists for the managed memory file.");
|
|
43741
43815
|
}
|
|
43742
|
-
|
|
43743
|
-
|
|
43744
|
-
|
|
43745
|
-
|
|
43746
|
-
|
|
43747
|
-
);
|
|
43816
|
+
let active;
|
|
43817
|
+
try {
|
|
43818
|
+
active = result.drifted ? result.current : await getMemoryById(activeId, this.opts.auth, chainOpts);
|
|
43819
|
+
} catch (err) {
|
|
43820
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
43821
|
+
this.logger.warn("[atbash] active memory row could not be verified", { error: msg });
|
|
43822
|
+
return this.unverifiedRead(`Memory version could not be verified: ${msg}`);
|
|
43748
43823
|
}
|
|
43749
|
-
|
|
43750
|
-
if (!
|
|
43824
|
+
const filePath = path6.basename(this.memoryFilePath);
|
|
43825
|
+
if (!active || active.id !== activeId || active.filePath !== filePath || active.isActive !== true || active.decryptError) {
|
|
43826
|
+
return this.unverifiedRead("Active memory version does not match the managed file.");
|
|
43827
|
+
}
|
|
43828
|
+
if (active.score < this.rollbackMinScore) {
|
|
43829
|
+
return this.unverifiedRead(`Memory version #${active.id} is below the minimum safety score.`);
|
|
43830
|
+
}
|
|
43831
|
+
try {
|
|
43832
|
+
if (result.drifted) {
|
|
43833
|
+
this.logger.info("[atbash] memory drift detected \u2014 refreshing local file", { id: active.id });
|
|
43834
|
+
await this.writeMemoryAtomic(active.content);
|
|
43835
|
+
}
|
|
43836
|
+
const onDisk = await fs3.readFile(this.memoryFilePath, "utf8");
|
|
43837
|
+
if (onDisk !== active.content) {
|
|
43838
|
+
return this.unverifiedRead("Local memory bytes do not match the active chain version.");
|
|
43839
|
+
}
|
|
43840
|
+
} catch (err) {
|
|
43841
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
43842
|
+
this.logger.warn("[atbash] managed memory file could not be verified", { error: msg });
|
|
43843
|
+
return this.unverifiedRead(`Managed memory file could not be verified: ${msg}`);
|
|
43844
|
+
}
|
|
43845
|
+
await this.pointerStore.set(this.agentPubkeyHex, result.pointer);
|
|
43751
43846
|
return { allow: true, audited: true };
|
|
43752
43847
|
}
|
|
43848
|
+
/** Complete queued audit-log writes before a host shuts down or removes its workspace. */
|
|
43849
|
+
async flushLogs() {
|
|
43850
|
+
await this.logger.flush();
|
|
43851
|
+
}
|
|
43852
|
+
unverifiedRead(reason) {
|
|
43853
|
+
if (!this.enforce) return null;
|
|
43854
|
+
return { block: true, blockReason: reason, allow: false, reason, audited: true };
|
|
43855
|
+
}
|
|
43753
43856
|
async writeMemoryAtomic(content) {
|
|
43754
43857
|
await fs3.mkdir(path6.dirname(this.memoryFilePath), { recursive: true });
|
|
43755
43858
|
const tmp = `${this.memoryFilePath}.${process.pid}.tmp`;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atbash/sdk",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.2",
|
|
4
4
|
"description": "TypeScript SDK for Atbash — the safety layer that evaluates AI agent actions against operator-defined policies before execution.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"atbash",
|
|
@@ -89,10 +89,10 @@
|
|
|
89
89
|
"typescript-eslint": "^8.62.1"
|
|
90
90
|
},
|
|
91
91
|
"optionalDependencies": {
|
|
92
|
-
"@atbash/sdk-linux-x64-gnu": "0.9.
|
|
93
|
-
"@atbash/sdk-linux-arm64-gnu": "0.9.
|
|
94
|
-
"@atbash/sdk-linux-x64-musl": "0.9.
|
|
95
|
-
"@atbash/sdk-darwin-arm64": "0.9.
|
|
96
|
-
"@atbash/sdk-win32-x64-msvc": "0.9.
|
|
92
|
+
"@atbash/sdk-linux-x64-gnu": "0.9.2",
|
|
93
|
+
"@atbash/sdk-linux-arm64-gnu": "0.9.2",
|
|
94
|
+
"@atbash/sdk-linux-x64-musl": "0.9.2",
|
|
95
|
+
"@atbash/sdk-darwin-arm64": "0.9.2",
|
|
96
|
+
"@atbash/sdk-win32-x64-msvc": "0.9.2"
|
|
97
97
|
}
|
|
98
98
|
}
|