agent-recall-core 3.4.47 → 3.4.49
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/README.md +25 -0
- package/dist/digest/store.d.ts +15 -6
- package/dist/digest/store.d.ts.map +1 -1
- package/dist/digest/store.js +33 -20
- package/dist/digest/store.js.map +1 -1
- package/dist/embeddings/chunker.d.ts +102 -0
- package/dist/embeddings/chunker.d.ts.map +1 -0
- package/dist/embeddings/chunker.js +260 -0
- package/dist/embeddings/chunker.js.map +1 -0
- package/dist/embeddings/config.d.ts +93 -0
- package/dist/embeddings/config.d.ts.map +1 -0
- package/dist/embeddings/config.js +174 -0
- package/dist/embeddings/config.js.map +1 -0
- package/dist/embeddings/index-store.d.ts +61 -0
- package/dist/embeddings/index-store.d.ts.map +1 -0
- package/dist/embeddings/index-store.js +158 -0
- package/dist/embeddings/index-store.js.map +1 -0
- package/dist/embeddings/indexer.d.ts +75 -0
- package/dist/embeddings/indexer.d.ts.map +1 -0
- package/dist/embeddings/indexer.js +196 -0
- package/dist/embeddings/indexer.js.map +1 -0
- package/dist/embeddings/runtime.d.ts +66 -0
- package/dist/embeddings/runtime.d.ts.map +1 -0
- package/dist/embeddings/runtime.js +197 -0
- package/dist/embeddings/runtime.js.map +1 -0
- package/dist/helpers/associative-link.d.ts.map +1 -1
- package/dist/helpers/associative-link.js +26 -1
- package/dist/helpers/associative-link.js.map +1 -1
- package/dist/helpers/tokenize.d.ts +1 -0
- package/dist/helpers/tokenize.d.ts.map +1 -1
- package/dist/helpers/tokenize.js +18 -0
- package/dist/helpers/tokenize.js.map +1 -1
- package/dist/index.d.ts +27 -10
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +50 -6
- package/dist/index.js.map +1 -1
- package/dist/palace/awareness.d.ts +34 -10
- package/dist/palace/awareness.d.ts.map +1 -1
- package/dist/palace/awareness.js +249 -180
- package/dist/palace/awareness.js.map +1 -1
- package/dist/palace/consolidate.d.ts +1 -1
- package/dist/palace/consolidate.d.ts.map +1 -1
- package/dist/palace/consolidate.js +9 -9
- package/dist/palace/consolidate.js.map +1 -1
- package/dist/palace/decay-pass.d.ts +1 -1
- package/dist/palace/decay-pass.d.ts.map +1 -1
- package/dist/palace/decay-pass.js +2 -2
- package/dist/palace/decay-pass.js.map +1 -1
- package/dist/palace/fan-out.d.ts +1 -1
- package/dist/palace/fan-out.d.ts.map +1 -1
- package/dist/palace/fan-out.js +5 -5
- package/dist/palace/fan-out.js.map +1 -1
- package/dist/palace/index-manager.d.ts +1 -1
- package/dist/palace/index-manager.d.ts.map +1 -1
- package/dist/palace/index-manager.js +1 -1
- package/dist/palace/index-manager.js.map +1 -1
- package/dist/palace/insights-index.d.ts +19 -6
- package/dist/palace/insights-index.d.ts.map +1 -1
- package/dist/palace/insights-index.js +36 -13
- package/dist/palace/insights-index.js.map +1 -1
- package/dist/palace/keystone.d.ts +1 -1
- package/dist/palace/keystone.d.ts.map +1 -1
- package/dist/palace/keystone.js +2 -2
- package/dist/palace/keystone.js.map +1 -1
- package/dist/palace/rooms.d.ts +3 -3
- package/dist/palace/rooms.d.ts.map +1 -1
- package/dist/palace/rooms.js +34 -6
- package/dist/palace/rooms.js.map +1 -1
- package/dist/retrieval/candidates.d.ts +27 -2
- package/dist/retrieval/candidates.d.ts.map +1 -1
- package/dist/retrieval/candidates.js +170 -3
- package/dist/retrieval/candidates.js.map +1 -1
- package/dist/retrieval/contradiction.d.ts +65 -13
- package/dist/retrieval/contradiction.d.ts.map +1 -1
- package/dist/retrieval/contradiction.js +40 -17
- package/dist/retrieval/contradiction.js.map +1 -1
- package/dist/retrieval/query-memory.d.ts +112 -9
- package/dist/retrieval/query-memory.d.ts.map +1 -1
- package/dist/retrieval/query-memory.js +623 -62
- package/dist/retrieval/query-memory.js.map +1 -1
- package/dist/retrieval/semantic-leg.d.ts +74 -0
- package/dist/retrieval/semantic-leg.d.ts.map +1 -0
- package/dist/retrieval/semantic-leg.js +180 -0
- package/dist/retrieval/semantic-leg.js.map +1 -0
- package/dist/storage/archive-write.d.ts.map +1 -1
- package/dist/storage/archive-write.js +29 -8
- package/dist/storage/archive-write.js.map +1 -1
- package/dist/storage/capture-router.d.ts +1 -1
- package/dist/storage/capture-router.d.ts.map +1 -1
- package/dist/storage/capture-router.js +26 -9
- package/dist/storage/capture-router.js.map +1 -1
- package/dist/storage/consolidation-queue.d.ts +11 -2
- package/dist/storage/consolidation-queue.d.ts.map +1 -1
- package/dist/storage/consolidation-queue.js +45 -16
- package/dist/storage/consolidation-queue.js.map +1 -1
- package/dist/storage/corrections.d.ts +185 -6
- package/dist/storage/corrections.d.ts.map +1 -1
- package/dist/storage/corrections.js +525 -126
- package/dist/storage/corrections.js.map +1 -1
- package/dist/storage/durable-intent.d.ts.map +1 -1
- package/dist/storage/durable-intent.js +24 -1
- package/dist/storage/durable-intent.js.map +1 -1
- package/dist/storage/filelock.d.ts +84 -12
- package/dist/storage/filelock.d.ts.map +1 -1
- package/dist/storage/filelock.js +309 -47
- package/dist/storage/filelock.js.map +1 -1
- package/dist/storage/fs-utils.d.ts +20 -0
- package/dist/storage/fs-utils.d.ts.map +1 -1
- package/dist/storage/fs-utils.js +27 -0
- package/dist/storage/fs-utils.js.map +1 -1
- package/dist/storage/paths.d.ts +34 -0
- package/dist/storage/paths.d.ts.map +1 -1
- package/dist/storage/paths.js +71 -0
- package/dist/storage/paths.js.map +1 -1
- package/dist/storage/pending.d.ts +154 -0
- package/dist/storage/pending.d.ts.map +1 -0
- package/dist/storage/pending.js +454 -0
- package/dist/storage/pending.js.map +1 -0
- package/dist/storage/project.d.ts.map +1 -1
- package/dist/storage/project.js +68 -16
- package/dist/storage/project.js.map +1 -1
- package/dist/storage/session-card.d.ts +13 -0
- package/dist/storage/session-card.d.ts.map +1 -1
- package/dist/storage/session-card.js +34 -3
- package/dist/storage/session-card.js.map +1 -1
- package/dist/storage/unclaimed.d.ts +165 -0
- package/dist/storage/unclaimed.d.ts.map +1 -0
- package/dist/storage/unclaimed.js +442 -0
- package/dist/storage/unclaimed.js.map +1 -0
- package/dist/storage/working-memory.d.ts.map +1 -1
- package/dist/storage/working-memory.js +19 -1
- package/dist/storage/working-memory.js.map +1 -1
- package/dist/supabase/recall-backend.d.ts +29 -2
- package/dist/supabase/recall-backend.d.ts.map +1 -1
- package/dist/supabase/recall-backend.js +231 -29
- package/dist/supabase/recall-backend.js.map +1 -1
- package/dist/supabase/sync.d.ts.map +1 -1
- package/dist/supabase/sync.js +13 -0
- package/dist/supabase/sync.js.map +1 -1
- package/dist/tools-logic/alignment-check.js +1 -1
- package/dist/tools-logic/alignment-check.js.map +1 -1
- package/dist/tools-logic/awareness-update.d.ts.map +1 -1
- package/dist/tools-logic/awareness-update.js +25 -16
- package/dist/tools-logic/awareness-update.js.map +1 -1
- package/dist/tools-logic/check-action.js +1 -1
- package/dist/tools-logic/check-action.js.map +1 -1
- package/dist/tools-logic/check.d.ts +55 -1
- package/dist/tools-logic/check.d.ts.map +1 -1
- package/dist/tools-logic/check.js +230 -57
- package/dist/tools-logic/check.js.map +1 -1
- package/dist/tools-logic/confidence.d.ts +11 -6
- package/dist/tools-logic/confidence.d.ts.map +1 -1
- package/dist/tools-logic/confidence.js +11 -6
- package/dist/tools-logic/confidence.js.map +1 -1
- package/dist/tools-logic/context-synthesize.js +2 -2
- package/dist/tools-logic/context-synthesize.js.map +1 -1
- package/dist/tools-logic/digest-read.js +2 -2
- package/dist/tools-logic/digest-read.js.map +1 -1
- package/dist/tools-logic/digest-recall.js +1 -1
- package/dist/tools-logic/digest-recall.js.map +1 -1
- package/dist/tools-logic/insight-promotion.d.ts +1 -1
- package/dist/tools-logic/insight-promotion.d.ts.map +1 -1
- package/dist/tools-logic/insight-promotion.js +17 -6
- package/dist/tools-logic/insight-promotion.js.map +1 -1
- package/dist/tools-logic/journal-capture.js +3 -3
- package/dist/tools-logic/journal-capture.js.map +1 -1
- package/dist/tools-logic/journal-write.js +3 -3
- package/dist/tools-logic/journal-write.js.map +1 -1
- package/dist/tools-logic/knowledge-write.js +2 -2
- package/dist/tools-logic/knowledge-write.js.map +1 -1
- package/dist/tools-logic/palace-lint.js +1 -1
- package/dist/tools-logic/palace-lint.js.map +1 -1
- package/dist/tools-logic/palace-read.js +1 -1
- package/dist/tools-logic/palace-read.js.map +1 -1
- package/dist/tools-logic/palace-search.js +1 -1
- package/dist/tools-logic/palace-search.js.map +1 -1
- package/dist/tools-logic/palace-walk.js +3 -3
- package/dist/tools-logic/palace-walk.js.map +1 -1
- package/dist/tools-logic/palace-write.js +4 -4
- package/dist/tools-logic/palace-write.js.map +1 -1
- package/dist/tools-logic/predict-correction.js +1 -1
- package/dist/tools-logic/predict-correction.js.map +1 -1
- package/dist/tools-logic/recall-backend.d.ts +11 -2
- package/dist/tools-logic/recall-backend.d.ts.map +1 -1
- package/dist/tools-logic/recall-backend.js +2 -2
- package/dist/tools-logic/recall-backend.js.map +1 -1
- package/dist/tools-logic/resurrect.d.ts.map +1 -1
- package/dist/tools-logic/resurrect.js +9 -1
- package/dist/tools-logic/resurrect.js.map +1 -1
- package/dist/tools-logic/safety-consolidation.d.ts.map +1 -1
- package/dist/tools-logic/safety-consolidation.js +32 -30
- package/dist/tools-logic/safety-consolidation.js.map +1 -1
- package/dist/tools-logic/session-end.d.ts +8 -0
- package/dist/tools-logic/session-end.d.ts.map +1 -1
- package/dist/tools-logic/session-end.js +51 -13
- package/dist/tools-logic/session-end.js.map +1 -1
- package/dist/tools-logic/session-start.d.ts +23 -0
- package/dist/tools-logic/session-start.d.ts.map +1 -1
- package/dist/tools-logic/session-start.js +44 -1
- package/dist/tools-logic/session-start.js.map +1 -1
- package/dist/tools-logic/smart-recall.d.ts +185 -10
- package/dist/tools-logic/smart-recall.d.ts.map +1 -1
- package/dist/tools-logic/smart-recall.js +364 -60
- package/dist/tools-logic/smart-recall.js.map +1 -1
- package/dist/tools-logic/store-doctor.d.ts.map +1 -1
- package/dist/tools-logic/store-doctor.js +7 -4
- package/dist/tools-logic/store-doctor.js.map +1 -1
- package/dist/tools-logic/store-repair.js +1 -1
- package/dist/tools-logic/store-repair.js.map +1 -1
- package/dist/tools-logic/supersession.d.ts +64 -3
- package/dist/tools-logic/supersession.d.ts.map +1 -1
- package/dist/tools-logic/supersession.js +155 -50
- package/dist/tools-logic/supersession.js.map +1 -1
- package/dist/types.d.ts +12 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +1 -1
- package/dist/vector/embedding.d.ts +8 -0
- package/dist/vector/embedding.d.ts.map +1 -1
- package/dist/vector/embedding.js +8 -0
- package/dist/vector/embedding.js.map +1 -1
- package/package.json +1 -1
package/dist/storage/filelock.js
CHANGED
|
@@ -1,87 +1,349 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* File-based locking for shared store files (awareness, insights-index,
|
|
3
|
+
* corrections, digest, palace index/rooms, journal, pipeline, feedback,
|
|
4
|
+
* alignment).
|
|
3
5
|
*
|
|
4
|
-
* Uses mkdir atomicity: mkdir fails if dir already exists, which is
|
|
5
|
-
*
|
|
6
|
+
* Uses mkdir atomicity: mkdir fails if dir already exists, which is atomic
|
|
7
|
+
* on all platforms. No external dependencies.
|
|
6
8
|
*
|
|
7
|
-
*
|
|
9
|
+
* fix6-locks redesign (2026-09-11, S6 remediation):
|
|
10
|
+
*
|
|
11
|
+
* 1. WAITING IS ASYNC. The old primitive busy-wait spun the CPU and blocked
|
|
12
|
+
* the event loop for up to 5 s. `acquireLock`/`withLock` now await a
|
|
13
|
+
* setTimeout-based backoff, so a waiting process keeps serving its event
|
|
14
|
+
* loop (timers, MCP traffic, hook I/O) while contended.
|
|
15
|
+
*
|
|
16
|
+
* 2. NO STEALING FROM A LIVE HOLDER. The old primitive force-broke the lock
|
|
17
|
+
* unconditionally at timeout (rmdir+mkdir while the holder could be
|
|
18
|
+
* mid-write — silent corruption). The lock dir now records the holder's
|
|
19
|
+
* pid + acquisition timestamp (`owner.json`); a contender may reclaim
|
|
20
|
+
* ONLY when the holder process is provably dead (`process.kill(pid, 0)`
|
|
21
|
+
* probe). A dead holder is reclaimed immediately (no reason to wait out
|
|
22
|
+
* the timeout on a corpse); a live holder is NEVER stolen from — at
|
|
23
|
+
* timeout the contender gets a descriptive `LockContentionError` that
|
|
24
|
+
* callers handle explicitly (log + best-effort skip, or propagate),
|
|
25
|
+
* never silent corruption. Boot-time guard (review MEDIUM-1): a lock
|
|
26
|
+
* whose acquired_at predates the current boot is reclaimed even if its
|
|
27
|
+
* recorded pid probes alive — pids recycle across reboots, and no lock
|
|
28
|
+
* legitimately survives one.
|
|
29
|
+
*
|
|
30
|
+
* 3. RECLAIM IS RACE-SAFE. Reclaim renames the lock dir aside before
|
|
31
|
+
* removing it — rename is atomic, so when several contenders spot the
|
|
32
|
+
* same dead holder, exactly one wins the rename; the rest simply retry
|
|
33
|
+
* mkdir. (The old rmdir+mkdir reclaim let two contenders both "win".)
|
|
34
|
+
*
|
|
35
|
+
* 4. LEGACY/CRASH FALLBACK. A lock dir with no readable owner.json (created
|
|
36
|
+
* by pre-fix code, or a holder that died between mkdir and the owner
|
|
37
|
+
* write) falls back to the old mtime rule: reclaimable once older than
|
|
38
|
+
* STALE_LOCK_MS.
|
|
39
|
+
*
|
|
40
|
+
* CRITICAL SECTIONS: keep them synchronous wherever possible — hold times
|
|
41
|
+
* stay bounded by one synchronous block. An async fn is permitted ONLY for
|
|
42
|
+
* the documented lock nesting below (awareness-state → awareness); a critical
|
|
43
|
+
* section must NEVER await anything that acquires the SAME lock (the lock is
|
|
44
|
+
* not reentrant — that is a guaranteed 5 s stall + LockContentionError).
|
|
45
|
+
* Cross-process safety does not depend on the fn being sync: the mkdir lock
|
|
46
|
+
* itself excludes concurrent critical sections either way.
|
|
47
|
+
*
|
|
48
|
+
* LOCK ORDERING (deadlock discipline — the only nesting in the codebase):
|
|
49
|
+
* "awareness-state" → "awareness"
|
|
50
|
+
* awareness.ts acquires "awareness" (awareness.md render) while holding
|
|
51
|
+
* "awareness-state" (awareness-state.json + awareness-archive.json RMW).
|
|
52
|
+
* Never acquire "awareness-state" while holding "awareness". Same-name
|
|
53
|
+
* nesting is a deadlock (the lock is NOT reentrant): modules expose
|
|
54
|
+
* *Unlocked internals for use inside their own critical sections instead.
|
|
55
|
+
*
|
|
56
|
+
* fix9 (2026-09-12): the synchronous twin (`acquireLockSync`/`withLockSync`,
|
|
57
|
+
* Atomics.wait-based — no CPU spin, but it parked the whole event loop) is
|
|
58
|
+
* RETIRED. It existed only to pin the published sync SDK signature
|
|
59
|
+
* `AgentRecall.digestInvalidate(): void`; the owner approved converting that
|
|
60
|
+
* signature to async, so the last sanctioned caller is gone. The async
|
|
61
|
+
* `withLock` is the only locking primitive.
|
|
8
62
|
*/
|
|
9
63
|
import * as fs from "node:fs";
|
|
64
|
+
import * as os from "node:os";
|
|
10
65
|
import * as path from "node:path";
|
|
11
66
|
import { getRoot } from "../types.js";
|
|
12
67
|
import { ensureDir } from "./fs-utils.js";
|
|
13
68
|
const LOCK_TIMEOUT_MS = 5000;
|
|
14
69
|
/**
|
|
15
|
-
* A lock dir
|
|
16
|
-
* the
|
|
17
|
-
*
|
|
70
|
+
* A lock dir with NO readable owner.json (legacy pre-pid code, or a crash in
|
|
71
|
+
* the mkdir→owner-write window) older than this (by mtime) is considered
|
|
72
|
+
* stale and reclaimed on the next acquire. Locks WITH a live owner pid are
|
|
73
|
+
* never age-reclaimed. Exported so the read-only store-doctor scans
|
|
74
|
+
* `.lock-*` dirs against the SAME threshold the locker uses (single source
|
|
75
|
+
* of truth — no drift).
|
|
18
76
|
*/
|
|
19
77
|
export const STALE_LOCK_MS = 30000;
|
|
78
|
+
const MAX_BACKOFF_MS = 200;
|
|
79
|
+
const OWNER_FILE = "owner.json";
|
|
80
|
+
/**
|
|
81
|
+
* Slack subtracted when comparing a lock's acquired_at against the current
|
|
82
|
+
* boot time (review MEDIUM-1, 2026-09-11): absorbs clock steps/NTP slew and
|
|
83
|
+
* os.uptime() coarseness so a lock acquired seconds before a suspend/resume
|
|
84
|
+
* is never misjudged as pre-boot.
|
|
85
|
+
*/
|
|
86
|
+
const BOOT_SLACK_MS = 120_000;
|
|
87
|
+
/** Thrown when the lock timeout elapses while the holder process is still
|
|
88
|
+
* alive. NEVER a signal to force-break the lock: the holder may be mid-write.
|
|
89
|
+
* Callers either propagate (surfacing an explicit tool error) or catch it for
|
|
90
|
+
* advisory writes (log + best-effort skip) — silent corruption is not an
|
|
91
|
+
* option the primitive offers. */
|
|
92
|
+
export class LockContentionError extends Error {
|
|
93
|
+
lockName;
|
|
94
|
+
holderPid;
|
|
95
|
+
heldForMs;
|
|
96
|
+
constructor(lockName, holderPid, heldForMs) {
|
|
97
|
+
const held = heldForMs !== null ? `${Math.round(heldForMs)}ms` : "unknown duration";
|
|
98
|
+
// Review LOW-2 (2026-09-11): only claim "live process" when a pid was
|
|
99
|
+
// actually probed alive; a no-owner-record lock's holder is UNVERIFIABLE
|
|
100
|
+
// (young legacy lock / crash in the mkdir→owner-write window), not
|
|
101
|
+
// known-live.
|
|
102
|
+
const claim = holderPid !== null
|
|
103
|
+
? `is held by a live process (pid ${holderPid}, held for ${held})`
|
|
104
|
+
: `appears to be held (no owner record — holder liveness unverifiable; held for ${held})`;
|
|
105
|
+
super(`Lock "${lockName}" ${claim} and was not ` +
|
|
106
|
+
`released within ${LOCK_TIMEOUT_MS}ms. Refusing to steal a possibly-live holder's lock (the holder ` +
|
|
107
|
+
`may be mid-write). agent_instruction: another AgentRecall process is writing this file; ` +
|
|
108
|
+
`retry the operation, or skip this advisory update and log the skip.`);
|
|
109
|
+
this.name = "LockContentionError";
|
|
110
|
+
this.lockName = lockName;
|
|
111
|
+
this.holderPid = holderPid;
|
|
112
|
+
this.heldForMs = heldForMs;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
20
115
|
function lockDir(name) {
|
|
21
|
-
|
|
116
|
+
// Lock names embed project slugs (e.g. `corrections-${project}`); slugs are
|
|
117
|
+
// sanitized upstream, but a lock name reaches path.join — sanitize here too
|
|
118
|
+
// so no caller can ever traverse out of the root (defence in depth).
|
|
119
|
+
const safe = name.replace(/[^A-Za-z0-9._-]/g, "_");
|
|
120
|
+
return path.join(getRoot(), `.lock-${safe}`);
|
|
121
|
+
}
|
|
122
|
+
function ownerPath(dir) {
|
|
123
|
+
return path.join(dir, OWNER_FILE);
|
|
124
|
+
}
|
|
125
|
+
function readOwner(dir) {
|
|
126
|
+
try {
|
|
127
|
+
const parsed = JSON.parse(fs.readFileSync(ownerPath(dir), "utf-8"));
|
|
128
|
+
if (typeof parsed?.pid !== "number")
|
|
129
|
+
return null;
|
|
130
|
+
return parsed;
|
|
131
|
+
}
|
|
132
|
+
catch {
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
/** Signal-0 liveness probe. EPERM means "alive but not ours" — still alive. */
|
|
137
|
+
function isPidAlive(pid) {
|
|
138
|
+
if (!Number.isInteger(pid) || pid <= 0)
|
|
139
|
+
return false;
|
|
140
|
+
try {
|
|
141
|
+
process.kill(pid, 0);
|
|
142
|
+
return true;
|
|
143
|
+
}
|
|
144
|
+
catch (err) {
|
|
145
|
+
return err.code === "EPERM";
|
|
146
|
+
}
|
|
22
147
|
}
|
|
23
148
|
/**
|
|
24
|
-
*
|
|
25
|
-
*
|
|
149
|
+
* Review MEDIUM-1 (2026-09-11): pid liveness alone cannot distinguish the
|
|
150
|
+
* original holder from an UNRELATED process that recycled the pid after a
|
|
151
|
+
* reboot — that corner made the lock permanently unreclaimable (every acquire
|
|
152
|
+
* waits the full timeout, then throws, forever). No lock legitimately
|
|
153
|
+
* survives a reboot (the holder process died with it), so a lock whose
|
|
154
|
+
* acquired_at predates the current boot is reclaimable even when its recorded
|
|
155
|
+
* pid is alive today. Unparsable timestamps are conservatively treated as
|
|
156
|
+
* current-boot (never a steal).
|
|
26
157
|
*/
|
|
27
|
-
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
158
|
+
function predatesBoot(acquiredAt) {
|
|
159
|
+
const t = Date.parse(acquiredAt);
|
|
160
|
+
if (!Number.isFinite(t))
|
|
161
|
+
return false;
|
|
162
|
+
return t < Date.now() - os.uptime() * 1000 - BOOT_SLACK_MS;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Try to remove a lock dir whose holder is dead (or which is legacy-stale).
|
|
166
|
+
* Rename-then-remove: rename is atomic, so of N contenders that all saw the
|
|
167
|
+
* same dead holder, exactly one wins; losers just retry mkdir.
|
|
168
|
+
* Returns true if this process removed (or observed the disappearance of)
|
|
169
|
+
* the lock dir.
|
|
170
|
+
*/
|
|
171
|
+
function tryReclaim(dir) {
|
|
172
|
+
const owner = readOwner(dir);
|
|
173
|
+
if (owner) {
|
|
174
|
+
// Live holder from THIS boot — NEVER steal. A pre-boot acquired_at means
|
|
175
|
+
// the "live" pid is a post-reboot recycle, not the holder (see
|
|
176
|
+
// predatesBoot's doc comment).
|
|
177
|
+
if (isPidAlive(owner.pid) && !predatesBoot(owner.acquired_at))
|
|
178
|
+
return false;
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
// No owner record: legacy lock or crash inside the mkdir→owner-write
|
|
182
|
+
// window. Fall back to the old mtime staleness rule.
|
|
183
|
+
let stat;
|
|
34
184
|
try {
|
|
35
|
-
|
|
36
|
-
if (Date.now() - stat.mtimeMs > STALE_LOCK_MS) {
|
|
37
|
-
fs.rmdirSync(dir);
|
|
38
|
-
}
|
|
185
|
+
stat = fs.statSync(dir);
|
|
39
186
|
}
|
|
40
187
|
catch {
|
|
41
|
-
//
|
|
188
|
+
return true; // vanished — holder released; caller retries mkdir
|
|
42
189
|
}
|
|
190
|
+
if (Date.now() - stat.mtimeMs <= STALE_LOCK_MS)
|
|
191
|
+
return false;
|
|
43
192
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
193
|
+
const trash = `${dir}.reclaim-${process.pid}-${Date.now()}`;
|
|
194
|
+
try {
|
|
195
|
+
fs.renameSync(dir, trash);
|
|
196
|
+
}
|
|
197
|
+
catch {
|
|
198
|
+
return false; // lost the reclaim race (or holder released) — retry mkdir
|
|
199
|
+
}
|
|
200
|
+
// TOCTOU guard (review MEDIUM finding, 2026-09-11): between our readOwner
|
|
201
|
+
// above and the rename, ANOTHER contender may have completed this same
|
|
202
|
+
// reclaim AND a NEW holder may have acquired a fresh lock at the same path —
|
|
203
|
+
// in which case the rename we just won moved a LIVE holder's lock aside.
|
|
204
|
+
// Verify the dir we captured is still the one we observed: same dead (or
|
|
205
|
+
// pre-boot) owner, or still owner-less for the legacy branch. On mismatch,
|
|
206
|
+
// put it back and report no-reclaim.
|
|
207
|
+
const captured = readOwner(trash);
|
|
208
|
+
const sameAsObserved = owner
|
|
209
|
+
? captured !== null && captured.pid === owner.pid && captured.acquired_at === owner.acquired_at
|
|
210
|
+
: captured === null;
|
|
211
|
+
if (!sameAsObserved) {
|
|
47
212
|
try {
|
|
48
|
-
fs.
|
|
49
|
-
|
|
50
|
-
return () => {
|
|
51
|
-
try {
|
|
52
|
-
fs.rmdirSync(dir);
|
|
53
|
-
}
|
|
54
|
-
catch { /* already released */ }
|
|
55
|
-
};
|
|
213
|
+
fs.renameSync(trash, dir); // restore the displaced holder's lock
|
|
214
|
+
return false;
|
|
56
215
|
}
|
|
57
216
|
catch {
|
|
58
|
-
//
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
217
|
+
// Restore collided with yet another acquirer taking the name. The
|
|
218
|
+
// displaced holder loses exclusion for the rest of its critical section
|
|
219
|
+
// — a sub-microsecond triple-race residual; surface it loudly.
|
|
220
|
+
console.error(`[agent-recall] filelock: reclaim race on ${path.basename(dir)} displaced a concurrent ` +
|
|
221
|
+
`acquirer's lock and could not restore it (displaced pid ${captured?.pid ?? "unknown"}).`);
|
|
222
|
+
try {
|
|
223
|
+
fs.rmSync(trash, { recursive: true, force: true });
|
|
224
|
+
}
|
|
225
|
+
catch { /* inert */ }
|
|
226
|
+
return true;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
try {
|
|
230
|
+
fs.rmSync(trash, { recursive: true, force: true });
|
|
231
|
+
}
|
|
232
|
+
catch {
|
|
233
|
+
// Leftover trash dir is inert (never matches an active lock path); the
|
|
234
|
+
// store-doctor's .lock-* scan will surface it.
|
|
235
|
+
}
|
|
236
|
+
return true;
|
|
237
|
+
}
|
|
238
|
+
/** One non-blocking acquisition attempt. Returns a release fn or null.
|
|
239
|
+
* Only EEXIST means "contended"; ENOENT (store root vanished) is repaired and
|
|
240
|
+
* retried; any OTHER error (EACCES/EROFS/EMFILE, …) is rethrown immediately —
|
|
241
|
+
* spinning 5 s on a read-only store and then reporting "held by a live
|
|
242
|
+
* process" would misdiagnose an environment problem as contention. */
|
|
243
|
+
function tryAcquire(dir) {
|
|
244
|
+
try {
|
|
245
|
+
fs.mkdirSync(dir, { recursive: false });
|
|
246
|
+
}
|
|
247
|
+
catch (err) {
|
|
248
|
+
const code = err.code;
|
|
249
|
+
if (code === "ENOENT") {
|
|
250
|
+
// Parent (store root) vanished mid-wait — e.g. a test wiping its temp
|
|
251
|
+
// root while a straggler write is still queued. Recreate and retry once;
|
|
252
|
+
// without this, mkdir(ENOENT) + statSync(ENOENT)-as-"reclaimed" could
|
|
253
|
+
// loop instead of acquiring.
|
|
254
|
+
try {
|
|
255
|
+
ensureDir(path.dirname(dir));
|
|
256
|
+
fs.mkdirSync(dir, { recursive: false });
|
|
257
|
+
}
|
|
258
|
+
catch (err2) {
|
|
259
|
+
if (err2.code === "EEXIST")
|
|
260
|
+
return null;
|
|
261
|
+
throw err2;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
else if (code === "EEXIST") {
|
|
265
|
+
return null;
|
|
266
|
+
}
|
|
267
|
+
else {
|
|
268
|
+
throw err;
|
|
62
269
|
}
|
|
63
270
|
}
|
|
64
|
-
// Timeout — force-break the lock and take it
|
|
65
271
|
try {
|
|
66
|
-
fs.
|
|
272
|
+
fs.writeFileSync(ownerPath(dir), JSON.stringify({ pid: process.pid, acquired_at: new Date().toISOString() }), "utf-8");
|
|
67
273
|
}
|
|
68
|
-
catch {
|
|
69
|
-
|
|
274
|
+
catch {
|
|
275
|
+
// Owner record is advisory: without it the lock degrades to the legacy
|
|
276
|
+
// mtime staleness rule instead of pid-liveness. NOTE (review LOW-2): in
|
|
277
|
+
// this degraded mode a LIVE holder that keeps the lock past STALE_LOCK_MS
|
|
278
|
+
// IS mtime-reclaimable by a contender — a double-failure corner
|
|
279
|
+
// (owner-write failed AND >30s hold) equivalent to the pre-fix behavior,
|
|
280
|
+
// accepted rather than failing the acquire on a full disk.
|
|
281
|
+
}
|
|
282
|
+
let released = false;
|
|
70
283
|
return () => {
|
|
284
|
+
if (released)
|
|
285
|
+
return;
|
|
286
|
+
released = true;
|
|
287
|
+
// Guard: only remove the dir if it is still OURS (paranoia against the
|
|
288
|
+
// owner-write-failed + legacy-reclaimed corner).
|
|
289
|
+
const owner = readOwner(dir);
|
|
290
|
+
if (owner && owner.pid !== process.pid)
|
|
291
|
+
return;
|
|
71
292
|
try {
|
|
72
|
-
fs.
|
|
293
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
294
|
+
}
|
|
295
|
+
catch {
|
|
296
|
+
/* already released */
|
|
73
297
|
}
|
|
74
|
-
catch { /* already released */ }
|
|
75
298
|
};
|
|
76
299
|
}
|
|
300
|
+
function contentionError(name, dir) {
|
|
301
|
+
const owner = readOwner(dir);
|
|
302
|
+
const heldForMs = owner ? Date.now() - Date.parse(owner.acquired_at) : null;
|
|
303
|
+
return new LockContentionError(name, owner?.pid ?? null, Number.isFinite(heldForMs) ? heldForMs : null);
|
|
304
|
+
}
|
|
305
|
+
function sleep(ms) {
|
|
306
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* Acquire a named lock. Retries with async (event-loop-friendly) backoff
|
|
310
|
+
* until LOCK_TIMEOUT_MS; reclaims dead-holder / legacy-stale locks; throws
|
|
311
|
+
* LockContentionError if the holder is alive at timeout.
|
|
312
|
+
* Returns a release function (idempotent).
|
|
313
|
+
*/
|
|
314
|
+
export async function acquireLock(name) {
|
|
315
|
+
const dir = lockDir(name);
|
|
316
|
+
const deadline = Date.now() + LOCK_TIMEOUT_MS;
|
|
317
|
+
// Ensure root dir exists before attempting lock
|
|
318
|
+
ensureDir(path.dirname(dir));
|
|
319
|
+
let delay = 5;
|
|
320
|
+
for (;;) {
|
|
321
|
+
const release = tryAcquire(dir);
|
|
322
|
+
if (release)
|
|
323
|
+
return release;
|
|
324
|
+
// Deadline is checked BEFORE the reclaim-retry shortcut so the loop is
|
|
325
|
+
// always bounded — a pathological reclaim-always-true state (e.g. the
|
|
326
|
+
// store root being deleted out from under us) can never spin forever.
|
|
327
|
+
if (Date.now() >= deadline) {
|
|
328
|
+
throw contentionError(name, dir);
|
|
329
|
+
}
|
|
330
|
+
// Held. Dead holder (or legacy-stale dir) → reclaim and retry at once.
|
|
331
|
+
if (tryReclaim(dir))
|
|
332
|
+
continue;
|
|
333
|
+
await sleep(Math.min(delay, Math.max(1, deadline - Date.now())));
|
|
334
|
+
delay = Math.min(delay * 2, MAX_BACKOFF_MS);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
77
337
|
/**
|
|
78
338
|
* Execute a function while holding a lock.
|
|
79
|
-
* Lock is always released, even on error.
|
|
339
|
+
* Lock is always released, even on error. Prefer a synchronous fn (bounded
|
|
340
|
+
* hold); an async fn is allowed only for the documented awareness nesting —
|
|
341
|
+
* see the header's lock-ordering section.
|
|
80
342
|
*/
|
|
81
|
-
export function withLock(name, fn) {
|
|
82
|
-
const release = acquireLock(name);
|
|
343
|
+
export async function withLock(name, fn) {
|
|
344
|
+
const release = await acquireLock(name);
|
|
83
345
|
try {
|
|
84
|
-
return fn();
|
|
346
|
+
return await fn();
|
|
85
347
|
}
|
|
86
348
|
finally {
|
|
87
349
|
release();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"filelock.js","sourceRoot":"","sources":["../../src/storage/filelock.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"filelock.js","sourceRoot":"","sources":["../../src/storage/filelock.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6DG;AAEH,OAAO,KAAK,EAAE,MAAM,SAAS,CAAC;AAC9B,OAAO,KAAK,EAAE,MAAM,SAAS,CAAC;AAC9B,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAClC,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AACtC,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAE1C,MAAM,eAAe,GAAG,IAAI,CAAC;AAC7B;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,KAAK,CAAC;AACnC,MAAM,cAAc,GAAG,GAAG,CAAC;AAC3B,MAAM,UAAU,GAAG,YAAY,CAAC;AAChC;;;;;GAKG;AACH,MAAM,aAAa,GAAG,OAAO,CAAC;AAO9B;;;;kCAIkC;AAClC,MAAM,OAAO,mBAAoB,SAAQ,KAAK;IACnC,QAAQ,CAAS;IACjB,SAAS,CAAgB;IACzB,SAAS,CAAgB;IAElC,YAAY,QAAgB,EAAE,SAAwB,EAAE,SAAwB;QAC9E,MAAM,IAAI,GAAG,SAAS,KAAK,IAAI,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,kBAAkB,CAAC;QACpF,sEAAsE;QACtE,yEAAyE;QACzE,mEAAmE;QACnE,cAAc;QACd,MAAM,KAAK,GAAG,SAAS,KAAK,IAAI;YAC9B,CAAC,CAAC,kCAAkC,SAAS,cAAc,IAAI,GAAG;YAClE,CAAC,CAAC,gFAAgF,IAAI,GAAG,CAAC;QAC5F,KAAK,CACH,SAAS,QAAQ,KAAK,KAAK,e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AAC,GAAG,EAAE,GAAG,eAAe,CAAC;IAE9C,gDAAgD;IAChD,SAAS,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC;IAE7B,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,SAAS,CAAC;QACR,MAAM,OAAO,GAAG,UAAU,CAAC,GAAG,CAAC,CAAC;QAChC,IAAI,OAAO;YAAE,OAAO,OAAO,CAAC;QAE5B,uEAAuE;QACvE,sEAAsE;QACtE,sEAAsE;QACtE,IAAI,IAAI,CAAC,GAAG,EAAE,IAAI,QAAQ,EAAE,CAAC;YAC3B,MAAM,eAAe,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;QACnC,CAAC;QAED,uEAAuE;QACvE,IAAI,UAAU,CAAC,GAAG,CAAC;YAAE,SAAS;QAE9B,MAAM,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,QAAQ,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC;QACjE,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,GAAG,CAAC,EAAE,cAAc,CAAC,CAAC;IAC9C,CAAC;AACH,CAAC;AAED;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,QAAQ,CAAI,IAAY,EAAE,EAAwB;IACtE,MAAM,OAAO,GAAG,MAAM,WAAW,CAAC,IAAI,CAAC,CAAC;IACxC,IAAI,CAAC;QACH,OAAO,MAAM,EAAE,EAAE,CAAC;IACpB,CAAC;YAAS,CAAC;QACT,OAAO,EAAE,CAAC;IACZ,CAAC;AACH,CAAC"}
|
|
@@ -3,8 +3,28 @@
|
|
|
3
3
|
*/
|
|
4
4
|
export declare function ensureDir(dir: string): void;
|
|
5
5
|
export declare function todayISO(): string;
|
|
6
|
+
/**
|
|
7
|
+
* fix5 review MEDIUM-2 (2026-09-11): validate a caller-supplied ISO date
|
|
8
|
+
* before it is interpolated into a FILENAME (session cards, staged cards).
|
|
9
|
+
* `SessionCardResult.date` can originate from hook-stdin `meta.date` — an
|
|
10
|
+
* untrusted string — and both card write sites build
|
|
11
|
+
* `path.join(dir, `${date}--card--${sid}.md`)` with it; a value like
|
|
12
|
+
* `../../x` would escape the target dir. Strict `YYYY-MM-DD` shape or
|
|
13
|
+
* today's date — one shared helper so both write sites (and any future
|
|
14
|
+
* date-in-filename writer) apply the SAME rule (class-not-instance).
|
|
15
|
+
*/
|
|
16
|
+
export declare function safeIsoDateOrToday(date: string | undefined | null): string;
|
|
6
17
|
export declare function readJsonSafe<T>(filePath: string): T | null;
|
|
7
18
|
export declare function writeJsonAtomic(filePath: string, data: unknown): void;
|
|
19
|
+
/**
|
|
20
|
+
* Atomic PRE-SERIALIZED text write (tmp + rename) — for callers that must
|
|
21
|
+
* transform the payload before it touches disk (e.g. scrubForCloud over the
|
|
22
|
+
* serialized JSON). fix6-locks review finding: a locked read-modify-write
|
|
23
|
+
* span that ends in plain writeFileSync (truncate-then-write) still exposes
|
|
24
|
+
* LOCK-FREE readers (and crash-mid-write) to torn files; rename-atomicity
|
|
25
|
+
* closes that without requiring readers to lock.
|
|
26
|
+
*/
|
|
27
|
+
export declare function writeTextAtomic(filePath: string, text: string): void;
|
|
8
28
|
/**
|
|
9
29
|
* Largest N in [0, buf.length] such that `buf.subarray(0, N)` never ends
|
|
10
30
|
* mid-way through a multi-byte UTF-8 sequence. Used both for a plain
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fs-utils.d.ts","sourceRoot":"","sources":["../../src/storage/fs-utils.ts"],"names":[],"mappings":"AAAA;;GAEG;AAKH,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAI3C;AAED,wBAAgB,QAAQ,IAAI,MAAM,CAEjC;AAED,wBAAgB,YAAY,CAAC,CAAC,EAAE,QAAQ,EAAE,MAAM,GAAG,CAAC,GAAG,IAAI,CAO1D;AAED,wBAAgB,eAAe,CAAC,Q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C,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAKzE"}
|
package/dist/storage/fs-utils.js
CHANGED
|
@@ -11,6 +11,19 @@ export function ensureDir(dir) {
|
|
|
11
11
|
export function todayISO() {
|
|
12
12
|
return new Date().toISOString().slice(0, 10);
|
|
13
13
|
}
|
|
14
|
+
/**
|
|
15
|
+
* fix5 review MEDIUM-2 (2026-09-11): validate a caller-supplied ISO date
|
|
16
|
+
* before it is interpolated into a FILENAME (session cards, staged cards).
|
|
17
|
+
* `SessionCardResult.date` can originate from hook-stdin `meta.date` — an
|
|
18
|
+
* untrusted string — and both card write sites build
|
|
19
|
+
* `path.join(dir, `${date}--card--${sid}.md`)` with it; a value like
|
|
20
|
+
* `../../x` would escape the target dir. Strict `YYYY-MM-DD` shape or
|
|
21
|
+
* today's date — one shared helper so both write sites (and any future
|
|
22
|
+
* date-in-filename writer) apply the SAME rule (class-not-instance).
|
|
23
|
+
*/
|
|
24
|
+
export function safeIsoDateOrToday(date) {
|
|
25
|
+
return date && /^\d{4}-\d{2}-\d{2}$/.test(date) ? date : todayISO();
|
|
26
|
+
}
|
|
14
27
|
export function readJsonSafe(filePath) {
|
|
15
28
|
if (!fs.existsSync(filePath))
|
|
16
29
|
return null;
|
|
@@ -27,6 +40,20 @@ export function writeJsonAtomic(filePath, data) {
|
|
|
27
40
|
fs.writeFileSync(tmp, JSON.stringify(data, null, 2), "utf-8");
|
|
28
41
|
fs.renameSync(tmp, filePath); // atomic on POSIX
|
|
29
42
|
}
|
|
43
|
+
/**
|
|
44
|
+
* Atomic PRE-SERIALIZED text write (tmp + rename) — for callers that must
|
|
45
|
+
* transform the payload before it touches disk (e.g. scrubForCloud over the
|
|
46
|
+
* serialized JSON). fix6-locks review finding: a locked read-modify-write
|
|
47
|
+
* span that ends in plain writeFileSync (truncate-then-write) still exposes
|
|
48
|
+
* LOCK-FREE readers (and crash-mid-write) to torn files; rename-atomicity
|
|
49
|
+
* closes that without requiring readers to lock.
|
|
50
|
+
*/
|
|
51
|
+
export function writeTextAtomic(filePath, text) {
|
|
52
|
+
ensureDir(path.dirname(filePath));
|
|
53
|
+
const tmp = filePath + ".tmp." + process.pid;
|
|
54
|
+
fs.writeFileSync(tmp, text, "utf-8");
|
|
55
|
+
fs.renameSync(tmp, filePath); // atomic on POSIX
|
|
56
|
+
}
|
|
30
57
|
// ---------------------------------------------------------------------------
|
|
31
58
|
// M8 (review fix, 2026-07-31) — shared UTF-8-safe byte-boundary helpers.
|
|
32
59
|
//
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fs-utils.js","sourceRoot":"","sources":["../../src/storage/fs-utils.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,MAAM,SAAS,CAAC;AAC9B,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAElC,MAAM,UAAU,SAAS,CAAC,GAAW;IACnC,IAAI,CAAC,EAAE,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;QACxB,EAAE,CAAC,SAAS,CAAC,GAAG,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;IACzC,CAAC;AACH,CAAC;AAED,MAAM,UAAU,QAAQ;IACtB,OAAO,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;AAC/C,CAAC;AAED,MAAM,UAAU,YAAY,CAAI,QAAgB;IAC9C,IAAI,CAAC,EAAE,CAAC,UAAU,CAAC,QAAQ,CAAC;QAAE,OAAO,IAAI,CAAC;IAC1C,IAAI,CAAC;QACH,OAAO,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,YAAY,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAM,CAAC;IAC7D,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC;AAED,MAAM,UAAU,eAAe,CAAC,QAAgB,EAAE,IAAa;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,IAAI,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC;IACtD,OAAO,KAAK,GAAG,GAAG,CAAC,MAAM,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC,KAAK,IAAI;QAAE,KAAK,EAAE,CAAC;IACnE,OAAO,KAAK,CAAC;AACf,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,iBAAiB,CAAC,KAAa,EAAE,QAAgB;IAC/D,MAAM,GAAG,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;IACxC,IAAI,GAAG,CAAC,MAAM,IAAI,QAAQ;QAAE,OAAO,KAAK,CAAC;IACzC,MAAM,GAAG,GAAG,mBAAmB,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAC;IAC/C,OAAO,GAAG,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;AAChD,CAAC"}
|
package/dist/storage/paths.d.ts
CHANGED
|
@@ -5,6 +5,40 @@
|
|
|
5
5
|
* AND verifies the resolved path stays under root with a trailing-separator check
|
|
6
6
|
* (preventing `~/.agent-recallEVIL` prefix bypass).
|
|
7
7
|
*/
|
|
8
|
+
/**
|
|
9
|
+
* Root-level staging namespace for writes whose project resolution FAILED or
|
|
10
|
+
* carried zero confidence. Lives at `<AR_ROOT>/_unclaimed/` — deliberately a
|
|
11
|
+
* SIBLING of `projects/`, never inside it, so every scan that enumerates
|
|
12
|
+
* `projects/` (recall corpus, listAllProjects, the scoreboard script's
|
|
13
|
+
* ghost-dir census, ar-sync-status.py's wholesale registry regen) is
|
|
14
|
+
* structurally blind to it. The leading `_` is the same BY-NAME reserved-
|
|
15
|
+
* namespace convention the rest of the store already uses for infrastructure
|
|
16
|
+
* (`_index.md`, `_archive/`, fix4's `_pending/`/`_quarantine/` class):
|
|
17
|
+
* class-not-instance — one underscore rule, not one branch per known dir.
|
|
18
|
+
*/
|
|
19
|
+
export declare const UNCLAIMED_DIRNAME = "_unclaimed";
|
|
20
|
+
/**
|
|
21
|
+
* The staging SENTINEL slug `resolveProject()` returns when resolution fails
|
|
22
|
+
* (blocked cwd, deny-listed detection, zero-confidence guess). It is NOT a
|
|
23
|
+
* valid project slug on purpose (`isValidProjectSlug` rejects the `_`
|
|
24
|
+
* prefix), so nothing downstream can register it into a cwd-allowlist or
|
|
25
|
+
* treat it as a real project — and `projectSubPath()` below routes every
|
|
26
|
+
* path built for it into `_unclaimed/<session>/` instead of `projects/`,
|
|
27
|
+
* which is the single choke point that closes ALL ~20 project-dir writers
|
|
28
|
+
* at once (journal, palace, corrections, digest, handoff, …) without
|
|
29
|
+
* per-writer gates.
|
|
30
|
+
*/
|
|
31
|
+
export declare const UNCLAIMED_PROJECT = "_unclaimed";
|
|
32
|
+
/** Is this slug the staging sentinel? (Exact match — never a prefix test.) */
|
|
33
|
+
export declare function isUnclaimedProject(project: string | undefined | null): boolean;
|
|
34
|
+
/** `<AR_ROOT>/_unclaimed` — the staging namespace root. */
|
|
35
|
+
export declare function unclaimedRootDir(): string;
|
|
36
|
+
/**
|
|
37
|
+
* Per-session staging subdir: `<AR_ROOT>/_unclaimed/<sid>`. `sid` is
|
|
38
|
+
* UNTRUSTED (may arrive from hook stdin) → sanitized with the same
|
|
39
|
+
* `sanitizeSlug` grammar every other sid-derived path in this package uses.
|
|
40
|
+
*/
|
|
41
|
+
export declare function unclaimedSessionDir(sid: string): string;
|
|
8
42
|
/**
|
|
9
43
|
* Sanitize a project name for safe use in path.join().
|
|
10
44
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"paths.d.ts","sourceRoot":"","sources":["../../src/storage/paths.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;
|
|
1
|
+
{"version":3,"file":"paths.d.ts","sourceRoot":"","sources":["../../src/storage/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