@staix/agent-mem 0.28.0 → 0.28.1

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.
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agent-mem",
3
- "version": "0.28.0",
3
+ "version": "0.28.1",
4
4
  "description": "Bounded project memory tools and seven deterministic capture hooks, without a resident worker.",
5
5
  "mcpServers": {
6
6
  "agent-mem": {
package/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  Curated memory with append-only JSONL logs, a derived SQLite index, short- and long-term tiers, a typed graph, goals with leases, and git sync between machines. Extracted from [agent-hub](https://github.com/STAIxBWLB/agent-hub), with compatible storage and entry IDs.
4
4
 
5
5
  <!-- facts:version -->
6
- Version: 0.28.0. Requires Bun 1.4.2 or later; no runtime dependencies.
6
+ Version: 0.28.1. Requires Bun 1.4.2 or later; no runtime dependencies.
7
7
  <!-- /facts:version -->
8
8
 
9
9
  ## Quick start
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@staix/agent-mem",
3
- "version": "0.28.0",
3
+ "version": "0.28.1",
4
4
  "description": "Curated, shared memory for coding agents: append-only logs, a derived index, tiers and a typed graph.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -0,0 +1,26 @@
1
+ /** Memory home identity bound into sync-placement epochs (#444). */
2
+ import type { BigIntStats } from "node:fs";
3
+ import { hashJSON } from "./generation.ts";
4
+
5
+ type HomeStat = Pick<BigIntStats, "dev" | "ino" | "uid">;
6
+ const hashFor = (home: string, dev: bigint, stat: HomeStat) => hashJSON([home, String(dev), String(stat.ino), String(stat.uid)]);
7
+ const resolved = new Map<string, string>();
8
+
9
+ /**
10
+ * Hash of the home's path, device, inode and uid. Epochs persist this value, and a macOS reboot can renumber the volume's
11
+ * `st.dev` while the directory stays the same. When `pinned` reproduces from the current path, inode and uid with another
12
+ * device number, it is returned unchanged, so a retained epoch keeps binding; a replaced or copied home still differs by inode.
13
+ * ponytail: only device numbers that differ in the low 16 bits are tried (65536 hashes, about 30 ms, memoized per process);
14
+ * widen the window or bind a stored volume UUID if a platform renumbers higher bits.
15
+ */
16
+ export function homeIdentityHash(home: string, stat: HomeStat, pinned?: string): string {
17
+ const current = hashFor(home, stat.dev, stat);
18
+ if (pinned === undefined || pinned === current) return current;
19
+ const key = JSON.stringify([pinned, home, String(stat.dev), String(stat.ino), String(stat.uid)]);
20
+ const known = resolved.get(key); if (known !== undefined) return known;
21
+ let result = current;
22
+ const base = stat.dev & ~0xffffn;
23
+ for (let low = 0n; low <= 0xffffn; low++) if (hashFor(home, base | low, stat) === pinned) { result = pinned; break; }
24
+ resolved.set(key, result); return result;
25
+ }
26
+ export const homeIdentityMatches = (expected: string, home: string, stat: HomeStat): boolean => homeIdentityHash(home, stat, expected) === expected;
@@ -1,7 +1,8 @@
1
1
  /** Internal activation-owner proof. No serialized object or boolean grants lock bypass authority. */
2
2
  import { closeSync, constants, fstatSync, lstatSync, openSync, readSync, type BigIntStats } from "node:fs";
3
3
  import { join } from "node:path";
4
- import { canonicalJSON, hashJSON } from "./generation.ts";
4
+ import { canonicalJSON } from "./generation.ts";
5
+ import { homeIdentityMatches } from "./home-identity.ts";
5
6
  import { assertMutationHandle, type MutationHandle } from "./mutation.ts";
6
7
  import { processSignature, realPath } from "./platform.ts";
7
8
  import type { PlacementActivationMarkerSnapshot } from "./sync-placement-capture.ts";
@@ -53,7 +54,7 @@ export function createPlacementActivationCaptureProof(owner: PlacementActivation
53
54
  const state = owners.get(owner)!;
54
55
  const intent = validatePlacementActivationIntent(input), actual = readPlacementActivationIntent(state.home);
55
56
  if (!intent || !actual || canonicalJSON(intent) !== canonicalJSON(actual)
56
- || actual.home !== state.home || actual.epoch.homeHash !== hashJSON([state.home,String(state.root.dev),String(state.root.ino),String(state.root.uid)])) fail();
57
+ || actual.home !== state.home || !homeIdentityMatches(actual.epoch.homeHash,state.home,state.root)) fail();
57
58
  const token = Object.freeze({}) as PlacementActivationCaptureProof; captures.set(token,{owner,intent:actual}); return token;
58
59
  }
59
60
  export function activationSourceMarkers(token: PlacementActivationCaptureProof, home: string, markers: { profileText: string; shardsText: string; profileStamp: readonly string[]; shardsStamp: readonly string[] }): PlacementActivationMarkerSnapshot {
@@ -5,6 +5,7 @@ import { isAbsolute, join, resolve } from "node:path";
5
5
  import { types } from "node:util";
6
6
  import { canonicalJSON, hashJSON } from "./generation.ts";
7
7
  import { realPath } from "./platform.ts";
8
+ import { homeIdentityMatches } from "./home-identity.ts";
8
9
  import { assertMutationHandle, type MutationHandle } from "./mutation.ts";
9
10
  import { validatePlacementBaseV2, validatePlacementShards, validatePlacementMarkerTransition, PLACEMENT_FORMAT, PLACEMENT_SHARDS_SCHEMA } from "./sync-placement-format.ts";
10
11
  import { validatePlacementPlan, type SyncPlacementActivationPlan } from "./sync-placement-plan.ts";
@@ -104,7 +105,7 @@ function root(home: string): { home: string; stat: BigIntStats; file: string } {
104
105
  if (stat.dev !== current.dev || stat.ino !== current.ino) fail("changed"); return { home: canonical, stat, file: join(canonical, PLACEMENT_ACTIVATION_INTENT_FILE) };
105
106
  }
106
107
  function bindHome(intent: PlacementActivationIntent, current: ReturnType<typeof root>): void {
107
- if (intent.home !== current.home || intent.epoch.homeHash !== hashJSON([current.home, String(current.stat.dev), String(current.stat.ino), String(current.stat.uid)])) fail("changed");
108
+ if (intent.home !== current.home || !homeIdentityMatches(intent.epoch.homeHash, current.home, current.stat)) fail("changed");
108
109
  }
109
110
  interface Read { intent: PlacementActivationIntent; text: string; stat: BigIntStats }
110
111
  function read(current: ReturnType<typeof root>): Read | undefined {
@@ -3,6 +3,7 @@ import { closeSync, constants, fstatSync, lstatSync, openSync, readSync, type Bi
3
3
  import { join } from "node:path";
4
4
  import { canonicalJSON, hashJSON } from "./generation.ts";
5
5
  import { realPath } from "./platform.ts";
6
+ import { homeIdentityHash } from "./home-identity.ts";
6
7
  import { validatePlacementProfile, validatePlacementShards, type PlacementProfile, type PlacementShards } from "./sync-placement-format.ts";
7
8
  import { validatePlacementPlan, type SyncPlacementActivationPlan } from "./sync-placement-plan.ts";
8
9
  import { placementEpochHash, validatePlacementEpoch, validatePlacementActiveFence, type PlacementEpoch } from "./sync-placement-journal-format.ts";
@@ -84,7 +85,7 @@ export function readActivePlacementBinding(homeInput: string, profileInput: unkn
84
85
  const epochFile = read(join(dir, "epoch.json")), activationFile = read(join(dir, "activation.json")), fenceFile = read(join(home, "sync-placement", "active.json"));
85
86
  if (!epochFile || !activationFile || !fenceFile) return undefined;
86
87
  const epoch = validatePlacementEpoch(epochFile.value), activation = validatePlacementPlan(activationFile.value), fence = validatePlacementActiveFence(fenceFile.value);
87
- const homeHash = hashJSON([home, String(root.dev), String(root.ino), String(root.uid)]);
88
+ const homeHash = homeIdentityHash(home, root, epoch.homeHash);
88
89
  if (epoch.homeHash !== homeHash || epoch.generation !== profile.generation || placementEpochHash(epoch) !== profile.placementHead
89
90
  || activation.contextHash !== epoch.contextHash || activation.generation !== epoch.generation || activation.targetProfile.placementHead !== profile.placementHead
90
91
  || fence.epochHash !== profile.placementHead || fence.homeHash !== homeHash || fence.generation !== profile.generation) fail();
@@ -2,7 +2,7 @@ import type { PlacementPublicationSource } from "./sync-placement-publication-pl
2
2
  import { validatePlacementKey, type PlacementRealm } from "./sync-placement-journal-format.ts";
3
3
  import { types } from "node:util";
4
4
  import { createHash } from "node:crypto";
5
- import { closeSync, constants, fstatSync, lstatSync, openSync, opendirSync, readSync, type BigIntStats } from "node:fs";
5
+ import { closeSync, constants, fstatSync, lstatSync, openSync, opendirSync, readFileSync, readSync, type BigIntStats } from "node:fs";
6
6
  import { join } from "node:path";
7
7
  import { canonicalJSON, generationDescriptor, hashJSON, validateVaultLinkRow, type BoundProfile, type VaultLinkRow } from "./generation.ts";
8
8
  import { validatePlacementBaseV2, validatePlacementProfile, validatePlacementShards, PLACEMENT_PROFILE_SCHEMA } from "./sync-placement-format.ts";
@@ -16,7 +16,9 @@ import { readShardLegacySnapshot } from "./shard-bridge.ts";
16
16
  import { coalesceGenerationSource } from "./generation-index.ts";
17
17
  import { validatePruneRow, type PruneSourceRow, type PruneRow } from "./prune.ts";
18
18
  import { shardKeyHash, SHARD_SCHEMA } from "./shard.ts";
19
- import { SyncPlacementJournalError, placementKeyId, type PlacementKey } from "./sync-placement-journal-format.ts";
19
+ import { SyncPlacementJournalError, placementKeyId, validatePlacementActiveFence, type PlacementKey } from "./sync-placement-journal-format.ts";
20
+ import { readPlacementActivationIntent } from "./sync-placement-activation-storage.ts";
21
+ import { homeIdentityHash } from "./home-identity.ts";
20
22
  import type { Entry } from "./store.ts";
21
23
  import { assertPlacementRuntime } from "./sync-placement-guard.ts";
22
24
  import { activationSourceMarkers, assertPlacementActivationOwner, type PlacementActivationOwner, type PlacementActivationCaptureProof } from "./sync-placement-activation-proof.ts";
@@ -288,7 +290,7 @@ function sourcePreview(homeInput: string, selected?: readonly PlacementRealm[],
288
290
  const defaults = memoryPolicySnapshot({ names: DENY_NAMES, segments: DENY_SEGMENTS }); if (!defaults) fail();
289
291
  const configuration = loadAmemConfig(home).config;
290
292
  const contextBase: PlacementSourcePreview["base"] = { ...base,
291
- homeHash: hashJSON([home, String(root.dev), String(root.ino), String(root.uid)]), sourceHash: hashJSON(facts),
293
+ homeHash: homeIdentityHash(home, root, pinnedHomeHash(home, activeHead, !!ownerProof)), sourceHash: hashJSON(facts),
292
294
  aliasesHash: digest(aliases ?? Buffer.alloc(0)), policyHash: hashJSON({ config: configuration, defaults, policyVersion }), realms };
293
295
  // ponytail: passive checks rely on native inode/size/ctime identity for the hashed prefixes.
294
296
  // Immutable descriptor-bound source blocks are the upgrade path for hostile same-user raw writers.
@@ -305,6 +307,14 @@ function sourcePreview(homeInput: string, selected?: readonly PlacementRealm[],
305
307
  return { home, base: contextBase, assertSourcesUnchanged, ...(activeHead ? { activeHead } : {}), markerHash, markers,
306
308
  directories: directories.sort(), prefixes, rows, specialRows, sourceRowsByRealm };
307
309
  }
310
+ /** The homeHash a retained epoch (or an in-flight activation) already committed to, so a reboot-renumbered device keeps binding (#444). */
311
+ function pinnedHomeHash(home: string, activeHead: string | undefined, activating: boolean): string | undefined {
312
+ try {
313
+ if (activeHead) return validatePlacementActiveFence(JSON.parse(readFileSync(join(home, "sync-placement", "active.json"), "utf8"))).homeHash;
314
+ if (activating) return readPlacementActivationIntent(home)?.epoch.homeHash;
315
+ } catch { /* unreadable metadata stays refused by the binding checks */ }
316
+ return undefined;
317
+ }
308
318
  /** Read-only preview shares the source collector, without fabricating a quiescence attestation. */
309
319
  export function previewSyncPlacementSources(home: string): PlacementSourcePreview {
310
320
  return previewSources(home);
@@ -3,7 +3,8 @@ import { createHash } from "node:crypto";
3
3
  import { closeSync, constants, fstatSync, fsyncSync, linkSync, lstatSync, openSync, opendirSync, readSync, unlinkSync, writeSync, type BigIntStats } from "node:fs";
4
4
  import { dirname, join } from "node:path";
5
5
  import { types } from "node:util";
6
- import { canonicalJSON, hashJSON } from "./generation.ts";
6
+ import { canonicalJSON } from "./generation.ts";
7
+ import { homeIdentityMatches } from "./home-identity.ts";
7
8
  import { realPath } from "./platform.ts";
8
9
  import { assertMutationHandle, type MutationHandle } from "./mutation.ts";
9
10
  import {
@@ -70,7 +71,7 @@ function binding(value: WriterHistoryBinding): WriterHistoryBinding {
70
71
  interface Paths { home: string; epoch: string; intent: string; log: string; dirs: [string,BigIntStats][] }
71
72
  function paths(homeInput: string, input: WriterHistoryBinding): Paths {
72
73
  const home=realPath(homeInput), original=checkedDir(homeInput), root=checkedDir(home);
73
- if(identity(original)!==identity(root)||hashJSON([home,String(root.dev),String(root.ino),String(root.uid)])!==input.homeHash)fail("changed");
74
+ if(identity(original)!==identity(root)||!homeIdentityMatches(input.homeHash,home,root))fail("changed");
74
75
  const dirs:[string,BigIntStats][]=[[home,root]];
75
76
  let current=home;for(const part of ["sync-placement","epochs",input.epochHash]){current=join(current,part);dirs.push([current,checkedDir(current)]);}
76
77
  return {home,epoch:current,intent:join(home,WRITER_RENEWAL_INTENT_FILE),log:join(current,WRITER_COMMIT_LOG),dirs};