@bli-cockpit/cli 0.2.51 → 0.2.53
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/commands/clean.js +244 -0
- package/dist/commands/doctor.js +73 -0
- package/dist/commands/jarvis.js +101 -9
- package/dist/commands/local-args-collector.js +30 -0
- package/dist/commands/local-args.js +3 -1
- package/dist/commands/local-help.js +26 -0
- package/dist/commands/local.js +3 -0
- package/dist/commands/public-root.js +1 -1
- package/dist/commands/session-sync-attribution.js +55 -0
- package/dist/commands/session-sync-failures.js +140 -0
- package/dist/commands/session-sync-health.js +102 -0
- package/dist/commands/session-sync-plan.js +81 -0
- package/dist/commands/session-sync-record.js +279 -0
- package/dist/commands/session-sync-scan.js +209 -0
- package/dist/commands/session-sync-types.js +12 -0
- package/dist/commands/session-sync-upload.js +215 -0
- package/dist/commands/session-sync.js +44 -987
- package/dist/commands/sync-followups.js +140 -2
- package/dist/commands/sync.js +4 -1
- package/dist/cursors/raw-evidence-reconcile-cursor.js +132 -0
- package/dist/disk-prune.js +246 -0
- package/dist/disk-retention.js +157 -0
- package/dist/disk-usage.js +392 -0
- package/dist/evidence-reconcile-client.js +224 -0
- package/dist/log-rotation.js +106 -2
- package/dist/tower-stream.js +5 -1
- package/package.json +3 -3
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/** How long a committed spare is kept so a retry still has local bytes. */
|
|
2
|
+
export const DEFAULT_STAGING_RETENTION_MS = 48 * 60 * 60 * 1000;
|
|
3
|
+
/** Ceiling on everything under `raw-evidence`, manifests included. */
|
|
4
|
+
export const DEFAULT_STAGING_CAP_BYTES = 2 * 1024 * 1024 * 1024;
|
|
5
|
+
/** Local knobs, read from the environment like `COCKPIT_DISABLE_GC`. */
|
|
6
|
+
export const STAGING_CAP_ENV = "COCKPIT_STAGING_CAP_BYTES";
|
|
7
|
+
export const STAGING_RETENTION_ENV = "COCKPIT_STAGING_RETENTION_HOURS";
|
|
8
|
+
/** The two knobs, from the environment, falling back to the defaults above. */
|
|
9
|
+
export function retentionOptionsFromEnv(env) {
|
|
10
|
+
const hours = Number(env[STAGING_RETENTION_ENV]);
|
|
11
|
+
const cap = Number(env[STAGING_CAP_ENV]);
|
|
12
|
+
return {
|
|
13
|
+
retentionMs: Number.isFinite(hours) && hours >= 0
|
|
14
|
+
? hours * 60 * 60 * 1000
|
|
15
|
+
: DEFAULT_STAGING_RETENTION_MS,
|
|
16
|
+
capBytes: Number.isFinite(cap) && cap > 0 ? cap : DEFAULT_STAGING_CAP_BYTES,
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
export function planStagingRetention(inventory, options = {}) {
|
|
20
|
+
const retentionMs = options.retentionMs ?? DEFAULT_STAGING_RETENTION_MS;
|
|
21
|
+
const capBytes = options.capBytes ?? DEFAULT_STAGING_CAP_BYTES;
|
|
22
|
+
const plan = emptyPlan(capBytes, retentionMs, inventory.total_bytes);
|
|
23
|
+
const committedInWindow = [];
|
|
24
|
+
for (const pack of inventory.packs) {
|
|
25
|
+
for (const object of pack.objects) {
|
|
26
|
+
if (object.state !== "committed") {
|
|
27
|
+
keepUnvouched(plan, object);
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
if (options.allCommitted || object.age_ms >= retentionMs) {
|
|
31
|
+
pushDeletion(plan, object, "committed_past_retry_window");
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
committedInWindow.push(object);
|
|
35
|
+
addToBucket(plan.kept_in_window, object);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
applyByteCap(plan, committedInWindow, capBytes, inventory);
|
|
39
|
+
markEmptyPacks(plan, inventory.packs);
|
|
40
|
+
plan.bytes_after =
|
|
41
|
+
inventory.total_bytes - plan.deleted_bytes - plan.empty_pack_manifest_bytes;
|
|
42
|
+
// Over the cap is only "blocked by uncommitted" when uncommitted bytes are
|
|
43
|
+
// what is left. Over the cap with nothing unvouched on disk is a different
|
|
44
|
+
// sentence — the remaining manifests alone exceed it — and saying the wrong
|
|
45
|
+
// one would send an operator hunting evidence that is not missing.
|
|
46
|
+
const unvouched = plan.kept_uncommitted.count + plan.kept_unknown.count;
|
|
47
|
+
plan.cap_blocked_by_uncommitted = plan.bytes_after > capBytes && unvouched > 0;
|
|
48
|
+
plan.cap_blocked_count = plan.cap_blocked_by_uncommitted ? unvouched : 0;
|
|
49
|
+
return plan;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* The oldest committed spares go until the total fits, newest kept last. Sorted
|
|
53
|
+
* by the ledger's `committed_at` age rather than a file mtime: mtime moves when
|
|
54
|
+
* a pack is refilled, and the question here is how long ago the bytes became
|
|
55
|
+
* safe elsewhere.
|
|
56
|
+
*
|
|
57
|
+
* It does not run at all when the floor is already above the cap. That branch
|
|
58
|
+
* is not theoretical: on the reference Mac (2026-09-05) 8.0 GB of staging is
|
|
59
|
+
* evidence no ledger vouches for, against a 2 GB cap, so spending the retry
|
|
60
|
+
* window on 1,027 committed spares would have deleted a day of local retry
|
|
61
|
+
* copies and still ended over the cap. The cap takes a spare only when taking
|
|
62
|
+
* it can actually get this machine under the cap.
|
|
63
|
+
*/
|
|
64
|
+
function applyByteCap(plan, committedInWindow, capBytes, inventory) {
|
|
65
|
+
let projected = plan.bytes_before - plan.deleted_bytes;
|
|
66
|
+
if (projected <= capBytes)
|
|
67
|
+
return;
|
|
68
|
+
if (unremovableFloor(inventory) >= capBytes)
|
|
69
|
+
return;
|
|
70
|
+
const oldestFirst = [...committedInWindow].sort((a, b) => b.age_ms - a.age_ms);
|
|
71
|
+
for (const object of oldestFirst) {
|
|
72
|
+
if (projected <= capBytes)
|
|
73
|
+
break;
|
|
74
|
+
pushDeletion(plan, object, "cap_committed_oldest_first");
|
|
75
|
+
removeFromBucket(plan.kept_in_window, object);
|
|
76
|
+
projected -= object.byte_size;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Bytes no retention rule may take: every object the ledger does not vouch for,
|
|
81
|
+
* plus the `manifest.json` of each pack holding one, because that pack survives
|
|
82
|
+
* whatever else is deleted from it.
|
|
83
|
+
*/
|
|
84
|
+
function unremovableFloor(inventory) {
|
|
85
|
+
let floor = 0;
|
|
86
|
+
for (const pack of inventory.packs) {
|
|
87
|
+
const unvouched = pack.objects.filter((object) => object.state !== "committed");
|
|
88
|
+
if (unvouched.length === 0)
|
|
89
|
+
continue;
|
|
90
|
+
floor += pack.manifest_bytes;
|
|
91
|
+
for (const object of unvouched)
|
|
92
|
+
floor += object.byte_size;
|
|
93
|
+
}
|
|
94
|
+
return floor;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* A pack with no payload left is deleted whole, `manifest.json` included. The
|
|
98
|
+
* manifest is pack metadata — its only job is to describe files that are no
|
|
99
|
+
* longer there — and on the reference Mac those manifests were 836 MB across
|
|
100
|
+
* 1,612 packs, so leaving them behind would keep almost a gigabyte of
|
|
101
|
+
* descriptions of nothing.
|
|
102
|
+
*/
|
|
103
|
+
function markEmptyPacks(plan, packs) {
|
|
104
|
+
const deleted = new Set(plan.delete.map((entry) => `${entry.pack_id}/${entry.relative_path}`));
|
|
105
|
+
for (const pack of packs) {
|
|
106
|
+
const survives = pack.objects.some((object) => !deleted.has(`${pack.pack_id}/${object.relative_path}`));
|
|
107
|
+
if (survives)
|
|
108
|
+
continue;
|
|
109
|
+
plan.empty_packs.push({
|
|
110
|
+
pack_id: pack.pack_id,
|
|
111
|
+
manifest_bytes: pack.manifest_bytes,
|
|
112
|
+
});
|
|
113
|
+
plan.empty_pack_manifest_bytes += pack.manifest_bytes;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
function keepUnvouched(plan, object) {
|
|
117
|
+
const bucket = object.state === "unknown" ? plan.kept_unknown : plan.kept_uncommitted;
|
|
118
|
+
addToBucket(bucket, object);
|
|
119
|
+
}
|
|
120
|
+
function pushDeletion(plan, object, reason) {
|
|
121
|
+
plan.delete.push({
|
|
122
|
+
pack_id: object.pack_id,
|
|
123
|
+
relative_path: object.relative_path,
|
|
124
|
+
byte_size: object.byte_size,
|
|
125
|
+
content_hash: object.content_hash,
|
|
126
|
+
reason,
|
|
127
|
+
age_ms: object.age_ms,
|
|
128
|
+
});
|
|
129
|
+
plan.deleted_bytes += object.byte_size;
|
|
130
|
+
}
|
|
131
|
+
function addToBucket(bucket, object) {
|
|
132
|
+
bucket.count += 1;
|
|
133
|
+
bucket.bytes += object.byte_size;
|
|
134
|
+
bucket.oldest_age_ms = Math.max(bucket.oldest_age_ms, object.age_ms);
|
|
135
|
+
}
|
|
136
|
+
function removeFromBucket(bucket, object) {
|
|
137
|
+
bucket.count -= 1;
|
|
138
|
+
bucket.bytes -= object.byte_size;
|
|
139
|
+
}
|
|
140
|
+
function emptyPlan(capBytes, retentionMs, bytesBefore) {
|
|
141
|
+
const bucket = () => ({ count: 0, bytes: 0, oldest_age_ms: 0 });
|
|
142
|
+
return {
|
|
143
|
+
delete: [],
|
|
144
|
+
deleted_bytes: 0,
|
|
145
|
+
empty_packs: [],
|
|
146
|
+
empty_pack_manifest_bytes: 0,
|
|
147
|
+
kept_uncommitted: bucket(),
|
|
148
|
+
kept_unknown: bucket(),
|
|
149
|
+
kept_in_window: bucket(),
|
|
150
|
+
cap_bytes: capBytes,
|
|
151
|
+
retention_ms: retentionMs,
|
|
152
|
+
bytes_before: bytesBefore,
|
|
153
|
+
bytes_after: bytesBefore,
|
|
154
|
+
cap_blocked_by_uncommitted: false,
|
|
155
|
+
cap_blocked_count: 0,
|
|
156
|
+
};
|
|
157
|
+
}
|
|
@@ -0,0 +1,392 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What this laptop is actually holding, and which of it the upload ledger can
|
|
3
|
+
* vouch for.
|
|
4
|
+
*
|
|
5
|
+
* BLI-3619. Measured on the reference Mac 2026-09-05, not estimated:
|
|
6
|
+
* `~/.local/state/bli-cockpit/raw-evidence` held 1,612 pack directories,
|
|
7
|
+
* 5,776 payload files and 18.76 GB, plus 836 MB of `manifest.json` beside them
|
|
8
|
+
* — 20 GB of staging on a machine whose whole job is to upload it and move on.
|
|
9
|
+
* The collector staged raw evidence, uploaded it, committed it, and then kept
|
|
10
|
+
* the local copy forever.
|
|
11
|
+
*
|
|
12
|
+
* Nothing here deletes anything. This module answers ONE question per file —
|
|
13
|
+
* "does a ledger on this machine say these bytes are durable remotely?" — and
|
|
14
|
+
* the answer has three values, not two, because the honest third one is what
|
|
15
|
+
* the same probe turned up:
|
|
16
|
+
*
|
|
17
|
+
* committed the content hash is in `cursors/raw-evidence.json`
|
|
18
|
+
* `objects`, so it is durable remotely and the local
|
|
19
|
+
* copy is a spare.
|
|
20
|
+
* uncommitted the hash is absent AND the ledger's own memory
|
|
21
|
+
* reaches back past this object, so the ledger CAN
|
|
22
|
+
* speak about it and says no. Never deleted.
|
|
23
|
+
* unknown the ledger cannot answer: its `objects` map is
|
|
24
|
+
* capped at 5,000 rows and prunes the oldest, so on
|
|
25
|
+
* the reference Mac it only reached back to
|
|
26
|
+
* 2026-08-28 while 518 packs (7.93 GB) predate that.
|
|
27
|
+
* Also never deleted — but counted apart, because
|
|
28
|
+
* 7.93 GB of "I forgot" must not read as 7.93 GB of
|
|
29
|
+
* "it never landed".
|
|
30
|
+
*
|
|
31
|
+
* BLI-3619's second half: `cockpit clean --reconcile` asks the dashboard's own
|
|
32
|
+
* upload ledger about a hash this laptop can no longer classify and writes the
|
|
33
|
+
* answer into `cursors/raw-evidence-reconcile.json` — read here, alongside the
|
|
34
|
+
* commit ledger, before an object is allowed to fall back to `unknown`. A
|
|
35
|
+
* server-confirmed `committed` object counts as `committed`; `not_committed` /
|
|
36
|
+
* `unknown_to_server` count as `uncommitted`, never `unknown` — the server
|
|
37
|
+
* spoke, so "the ledger cannot say" is no longer true of that hash.
|
|
38
|
+
*
|
|
39
|
+
* Content hashes come from each pack's own `manifest.json`, which already
|
|
40
|
+
* records `relative_path` + `content_hash_sha256` + `byte_size` for every file
|
|
41
|
+
* it staged. That is why a daily inventory does not re-read 20 GB. A physical
|
|
42
|
+
* file its own manifest does not name is hashed instead, under a byte budget
|
|
43
|
+
* (3 such files, 37 MB, on the reference Mac); past the budget it stays
|
|
44
|
+
* `unknown` with a reason rather than being guessed at.
|
|
45
|
+
*
|
|
46
|
+
* Metadata only: pack ids, pack-relative paths, byte counts, hashes, reason
|
|
47
|
+
* labels. Never a person's absolute path and never a byte of content.
|
|
48
|
+
*/
|
|
49
|
+
import crypto from "node:crypto";
|
|
50
|
+
import fs from "node:fs/promises";
|
|
51
|
+
import path from "node:path";
|
|
52
|
+
import { readRawEvidenceCursor } from "./cursors/raw-evidence-cursor.js";
|
|
53
|
+
import { readReconcileCursor, } from "./cursors/raw-evidence-reconcile-cursor.js";
|
|
54
|
+
import { describeError } from "./health-detail.js";
|
|
55
|
+
import { rotatedLogFootprint } from "./log-rotation.js";
|
|
56
|
+
import { readRawEvidenceStagingState } from "./raw-evidence-staging.js";
|
|
57
|
+
/** Bytes this inventory may spend hashing files no manifest names. */
|
|
58
|
+
export const UNNAMED_FILE_HASH_BUDGET_BYTES = 256 * 1024 * 1024;
|
|
59
|
+
/** Directory name of the staged raw-evidence tree under the state directory. */
|
|
60
|
+
export const RAW_EVIDENCE_DIR = "raw-evidence";
|
|
61
|
+
/** One-off evidence vaults a person made by hand; named, never auto-deleted. */
|
|
62
|
+
export const MANUAL_VAULT_PREFIX = "manual-study-evidence-vault-";
|
|
63
|
+
/**
|
|
64
|
+
* Everything on this machine Cockpit put there, in one read. Used by the sync
|
|
65
|
+
* tick's prune, by `cockpit clean` and by `cockpit doctor`'s disk row, so all
|
|
66
|
+
* three quote the same numbers rather than three near-misses.
|
|
67
|
+
*/
|
|
68
|
+
export async function readDiskFootprint(paths, now = new Date()) {
|
|
69
|
+
return {
|
|
70
|
+
staging: await readStagingInventory(paths, now),
|
|
71
|
+
logs: await rotatedLogFootprint(paths),
|
|
72
|
+
spool_bytes: await directoryBytes(paths.spool_dir),
|
|
73
|
+
vaults: await readManualVaults(paths),
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
export async function readStagingInventory(paths, now = new Date()) {
|
|
77
|
+
const root = path.join(paths.state_dir, RAW_EVIDENCE_DIR);
|
|
78
|
+
const ledger = await readCommitLedger(paths);
|
|
79
|
+
const reconciled = await readReconcileLedger(paths);
|
|
80
|
+
const stagedAt = await readStagedAtIndex(paths);
|
|
81
|
+
const entries = await fs
|
|
82
|
+
.readdir(root, { withFileTypes: true })
|
|
83
|
+
.catch(() => []);
|
|
84
|
+
const inventory = emptyStagingInventory();
|
|
85
|
+
const budget = { remaining: UNNAMED_FILE_HASH_BUDGET_BYTES };
|
|
86
|
+
for (const entry of entries) {
|
|
87
|
+
if (!entry.isDirectory())
|
|
88
|
+
continue;
|
|
89
|
+
const dir = path.join(root, entry.name);
|
|
90
|
+
if (entry.name.startsWith(".staging-")) {
|
|
91
|
+
inventory.orphan_staging_dirs += 1;
|
|
92
|
+
inventory.orphan_staging_bytes += await directoryBytes(dir);
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
if (!entry.name.startsWith("work-"))
|
|
96
|
+
continue;
|
|
97
|
+
const pack = await readPack(dir, entry.name, {
|
|
98
|
+
ledger,
|
|
99
|
+
reconciled,
|
|
100
|
+
stagedAt,
|
|
101
|
+
now,
|
|
102
|
+
budget,
|
|
103
|
+
});
|
|
104
|
+
inventory.packs.push(pack);
|
|
105
|
+
accumulatePack(inventory, pack);
|
|
106
|
+
}
|
|
107
|
+
return inventory;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* The commit record, read from the one place that writes it: the raw-evidence
|
|
111
|
+
* cursor's `objects` map (`upload.ts` → `markObjectCommitted`, only ever for an
|
|
112
|
+
* outcome that was not `upload_failed`).
|
|
113
|
+
*/
|
|
114
|
+
async function readCommitLedger(paths) {
|
|
115
|
+
const cursor = await readRawEvidenceCursor(paths);
|
|
116
|
+
const committed = new Map();
|
|
117
|
+
let oldest = null;
|
|
118
|
+
for (const [hash, entry] of Object.entries(cursor.objects)) {
|
|
119
|
+
committed.set(hash, entry.committed_at);
|
|
120
|
+
if (!oldest || entry.committed_at.localeCompare(oldest) < 0) {
|
|
121
|
+
oldest = entry.committed_at;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return { committed, reachesBackTo: oldest };
|
|
125
|
+
}
|
|
126
|
+
/** `pack_id/relative_path` → when this machine staged those bytes. */
|
|
127
|
+
async function readStagedAtIndex(paths) {
|
|
128
|
+
const staging = await readRawEvidenceStagingState(paths.state_dir);
|
|
129
|
+
const index = new Map();
|
|
130
|
+
for (const entry of Object.values(staging.staged)) {
|
|
131
|
+
index.set(`${entry.pack_id}/${entry.relative_path}`, entry.staged_at);
|
|
132
|
+
}
|
|
133
|
+
return index;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* The reconcile cursor as a lookup map. Read alongside the commit ledger so an
|
|
137
|
+
* object the server confirmed committed classifies exactly like one this
|
|
138
|
+
* machine's own ledger still remembers.
|
|
139
|
+
*/
|
|
140
|
+
async function readReconcileLedger(paths) {
|
|
141
|
+
const cursor = await readReconcileCursor(paths);
|
|
142
|
+
return new Map(Object.entries(cursor.results));
|
|
143
|
+
}
|
|
144
|
+
async function readPack(dir, packId, options) {
|
|
145
|
+
const manifest = await readManifestIndex(dir);
|
|
146
|
+
const pack = {
|
|
147
|
+
pack_id: packId,
|
|
148
|
+
objects: [],
|
|
149
|
+
manifest_bytes: 0,
|
|
150
|
+
manifest_present: manifest !== null,
|
|
151
|
+
};
|
|
152
|
+
for (const relative of await listPackFiles(dir)) {
|
|
153
|
+
const size = await fs
|
|
154
|
+
.stat(path.join(dir, relative))
|
|
155
|
+
.then((info) => (info.isFile() ? info.size : 0))
|
|
156
|
+
.catch(() => 0);
|
|
157
|
+
if (relative === "manifest.json") {
|
|
158
|
+
pack.manifest_bytes += size;
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
pack.objects.push(await classifyObject(dir, packId, relative, size, manifest, options));
|
|
162
|
+
}
|
|
163
|
+
return pack;
|
|
164
|
+
}
|
|
165
|
+
async function classifyObject(dir, packId, relative, size, manifest, options) {
|
|
166
|
+
const hash = manifest?.get(relative) ??
|
|
167
|
+
(await hashUnnamedFile(path.join(dir, relative), size, options.budget));
|
|
168
|
+
const stagedAt = options.stagedAt.get(`${packId}/${relative}`) ?? null;
|
|
169
|
+
const committedAt = hash ? options.ledger.committed.get(hash) : undefined;
|
|
170
|
+
const base = { pack_id: packId, relative_path: relative, byte_size: size };
|
|
171
|
+
if (committedAt) {
|
|
172
|
+
return {
|
|
173
|
+
...base,
|
|
174
|
+
content_hash: hash ?? null,
|
|
175
|
+
state: "committed",
|
|
176
|
+
reason: "committed_in_ledger",
|
|
177
|
+
decided_at: committedAt,
|
|
178
|
+
age_ms: ageMs(committedAt, options.now),
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
if (hash) {
|
|
182
|
+
const reconciled = options.reconciled.get(hash);
|
|
183
|
+
if (reconciled) {
|
|
184
|
+
return classifyFromReconcileAnswer(base, hash, reconciled, options.now);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
const unknownReason = unknownStateReason(hash, manifest, stagedAt, options);
|
|
188
|
+
return {
|
|
189
|
+
...base,
|
|
190
|
+
content_hash: hash ?? null,
|
|
191
|
+
state: unknownReason ? "unknown" : "uncommitted",
|
|
192
|
+
reason: unknownReason ?? "absent_from_ledger",
|
|
193
|
+
decided_at: stagedAt,
|
|
194
|
+
age_ms: ageMs(stagedAt, options.now),
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* The server's own answer for this hash, already on disk from an earlier
|
|
199
|
+
* `cockpit clean --reconcile` run. This takes priority over
|
|
200
|
+
* `unknownStateReason`'s guesswork about the LOCAL ledger's memory, because
|
|
201
|
+
* the server was actually asked and actually answered.
|
|
202
|
+
*/
|
|
203
|
+
function classifyFromReconcileAnswer(base, hash, reconciled, now) {
|
|
204
|
+
if (reconciled.verdict === "committed") {
|
|
205
|
+
const decidedAt = reconciled.server_committed_at ?? reconciled.checked_at;
|
|
206
|
+
return {
|
|
207
|
+
...base,
|
|
208
|
+
content_hash: hash,
|
|
209
|
+
state: "committed",
|
|
210
|
+
reason: "reconciled_committed",
|
|
211
|
+
decided_at: decidedAt,
|
|
212
|
+
age_ms: ageMs(decidedAt, now),
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
return {
|
|
216
|
+
...base,
|
|
217
|
+
content_hash: hash,
|
|
218
|
+
state: "uncommitted",
|
|
219
|
+
reason: reconciled.verdict === "not_committed"
|
|
220
|
+
? "reconciled_not_committed"
|
|
221
|
+
: "reconciled_unknown_to_server",
|
|
222
|
+
decided_at: reconciled.checked_at,
|
|
223
|
+
age_ms: ageMs(reconciled.checked_at, now),
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Why the ledger's silence about this object is NOT the same as a "no".
|
|
228
|
+
*
|
|
229
|
+
* The `objects` map is capped at 5,000 rows and drops the oldest, so anything
|
|
230
|
+
* staged before the oldest row it still holds is outside its memory. Saying
|
|
231
|
+
* "uncommitted" there would be a claim the ledger never made.
|
|
232
|
+
*/
|
|
233
|
+
function unknownStateReason(hash, manifest, stagedAt, options) {
|
|
234
|
+
if (!manifest)
|
|
235
|
+
return "manifest_unreadable";
|
|
236
|
+
if (!hash)
|
|
237
|
+
return "no_manifest_row";
|
|
238
|
+
const reachesBackTo = options.ledger.reachesBackTo;
|
|
239
|
+
if (!reachesBackTo)
|
|
240
|
+
return "ledger_empty";
|
|
241
|
+
if (stagedAt && stagedAt.localeCompare(reachesBackTo) < 0) {
|
|
242
|
+
return "ledger_evicted";
|
|
243
|
+
}
|
|
244
|
+
if (!stagedAt)
|
|
245
|
+
return "ledger_evicted";
|
|
246
|
+
return null;
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* A file its own manifest does not name gets hashed — but only while the run's
|
|
250
|
+
* byte budget lasts. Past it the object stays `unknown` and says which budget
|
|
251
|
+
* ran out, because reading 20 GB to answer a housekeeping question is how the
|
|
252
|
+
* old GC earned its once-a-day throttle.
|
|
253
|
+
*/
|
|
254
|
+
async function hashUnnamedFile(file, size, budget) {
|
|
255
|
+
if (size > budget.remaining)
|
|
256
|
+
return null;
|
|
257
|
+
const bytes = await fs.readFile(file).catch(() => null);
|
|
258
|
+
if (!bytes)
|
|
259
|
+
return null;
|
|
260
|
+
budget.remaining -= bytes.byteLength;
|
|
261
|
+
return crypto.createHash("sha256").update(bytes).digest("hex");
|
|
262
|
+
}
|
|
263
|
+
async function readManifestIndex(dir) {
|
|
264
|
+
const raw = await fs
|
|
265
|
+
.readFile(path.join(dir, "manifest.json"), "utf8")
|
|
266
|
+
.catch(() => null);
|
|
267
|
+
if (!raw)
|
|
268
|
+
return null;
|
|
269
|
+
try {
|
|
270
|
+
const parsed = JSON.parse(raw);
|
|
271
|
+
const index = new Map();
|
|
272
|
+
for (const file of parsed.files ?? []) {
|
|
273
|
+
if (typeof file.relative_path === "string" &&
|
|
274
|
+
typeof file.content_hash_sha256 === "string" &&
|
|
275
|
+
file.content_hash_sha256.length === 64) {
|
|
276
|
+
index.set(file.relative_path, file.content_hash_sha256);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
return index;
|
|
280
|
+
}
|
|
281
|
+
catch (error) {
|
|
282
|
+
// Named, not swallowed: an unreadable manifest turns every file in the
|
|
283
|
+
// pack into `unknown`, which is the difference between a pack that gets
|
|
284
|
+
// pruned and one that is kept forever (BLI-3238's rule, BLI-3619's cost).
|
|
285
|
+
console.error("[collector disk] pack manifest unreadable, its files cannot be classified", JSON.stringify({
|
|
286
|
+
reason: "manifest_unreadable",
|
|
287
|
+
pack_id: path.basename(dir),
|
|
288
|
+
...describeError(error),
|
|
289
|
+
}));
|
|
290
|
+
return null;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
async function listPackFiles(dir) {
|
|
294
|
+
const files = [];
|
|
295
|
+
const stack = [{ dir, prefix: "" }];
|
|
296
|
+
while (stack.length > 0) {
|
|
297
|
+
const current = stack.pop();
|
|
298
|
+
if (!current)
|
|
299
|
+
continue;
|
|
300
|
+
const entries = await fs
|
|
301
|
+
.readdir(current.dir, { withFileTypes: true })
|
|
302
|
+
.catch(() => []);
|
|
303
|
+
for (const entry of entries) {
|
|
304
|
+
const relative = current.prefix
|
|
305
|
+
? `${current.prefix}/${entry.name}`
|
|
306
|
+
: entry.name;
|
|
307
|
+
if (entry.isDirectory()) {
|
|
308
|
+
stack.push({ dir: path.join(current.dir, entry.name), prefix: relative });
|
|
309
|
+
}
|
|
310
|
+
else if (entry.isFile()) {
|
|
311
|
+
files.push(relative);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
return files;
|
|
316
|
+
}
|
|
317
|
+
async function readManualVaults(paths) {
|
|
318
|
+
const entries = await fs
|
|
319
|
+
.readdir(paths.state_dir, { withFileTypes: true })
|
|
320
|
+
.catch(() => []);
|
|
321
|
+
const vaults = [];
|
|
322
|
+
for (const entry of entries) {
|
|
323
|
+
if (!entry.isDirectory() || !entry.name.startsWith(MANUAL_VAULT_PREFIX)) {
|
|
324
|
+
continue;
|
|
325
|
+
}
|
|
326
|
+
const dir = path.join(paths.state_dir, entry.name);
|
|
327
|
+
vaults.push({
|
|
328
|
+
name: entry.name,
|
|
329
|
+
byte_size: await directoryBytes(dir),
|
|
330
|
+
file_count: (await listPackFiles(dir)).length,
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
return vaults;
|
|
334
|
+
}
|
|
335
|
+
export async function directoryBytes(dir) {
|
|
336
|
+
let total = 0;
|
|
337
|
+
for (const relative of await listPackFiles(dir)) {
|
|
338
|
+
const info = await fs.stat(path.join(dir, relative)).catch(() => null);
|
|
339
|
+
total += info?.isFile() ? info.size : 0;
|
|
340
|
+
}
|
|
341
|
+
return total;
|
|
342
|
+
}
|
|
343
|
+
function accumulatePack(inventory, pack) {
|
|
344
|
+
inventory.pack_count += 1;
|
|
345
|
+
inventory.manifest_bytes += pack.manifest_bytes;
|
|
346
|
+
inventory.total_bytes += pack.manifest_bytes;
|
|
347
|
+
for (const object of pack.objects) {
|
|
348
|
+
inventory.object_count += 1;
|
|
349
|
+
inventory.total_bytes += object.byte_size;
|
|
350
|
+
if (object.state === "committed") {
|
|
351
|
+
inventory.committed_count += 1;
|
|
352
|
+
inventory.committed_bytes += object.byte_size;
|
|
353
|
+
continue;
|
|
354
|
+
}
|
|
355
|
+
if (object.state === "unknown") {
|
|
356
|
+
inventory.unknown_count += 1;
|
|
357
|
+
inventory.unknown_bytes += object.byte_size;
|
|
358
|
+
continue;
|
|
359
|
+
}
|
|
360
|
+
inventory.uncommitted_count += 1;
|
|
361
|
+
inventory.uncommitted_bytes += object.byte_size;
|
|
362
|
+
if (object.decided_at &&
|
|
363
|
+
(!inventory.oldest_uncommitted_at ||
|
|
364
|
+
object.decided_at.localeCompare(inventory.oldest_uncommitted_at) < 0)) {
|
|
365
|
+
inventory.oldest_uncommitted_at = object.decided_at;
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
function emptyStagingInventory() {
|
|
370
|
+
return {
|
|
371
|
+
pack_count: 0,
|
|
372
|
+
object_count: 0,
|
|
373
|
+
total_bytes: 0,
|
|
374
|
+
committed_count: 0,
|
|
375
|
+
committed_bytes: 0,
|
|
376
|
+
uncommitted_count: 0,
|
|
377
|
+
uncommitted_bytes: 0,
|
|
378
|
+
oldest_uncommitted_at: null,
|
|
379
|
+
unknown_count: 0,
|
|
380
|
+
unknown_bytes: 0,
|
|
381
|
+
manifest_bytes: 0,
|
|
382
|
+
orphan_staging_dirs: 0,
|
|
383
|
+
orphan_staging_bytes: 0,
|
|
384
|
+
packs: [],
|
|
385
|
+
};
|
|
386
|
+
}
|
|
387
|
+
function ageMs(at, now) {
|
|
388
|
+
if (!at)
|
|
389
|
+
return 0;
|
|
390
|
+
const parsed = Date.parse(at);
|
|
391
|
+
return Number.isFinite(parsed) ? Math.max(0, now.getTime() - parsed) : 0;
|
|
392
|
+
}
|