@mostra/runtime 0.0.0-placeholder → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +38 -1
- package/dist/bin.d.ts +1 -0
- package/dist/bin.js +236 -0
- package/dist/bin.js.map +1 -0
- package/dist/builder/child/main.d.ts +1 -0
- package/dist/builder/child/main.js +1408 -0
- package/dist/builder/child/main.js.map +1 -0
- package/dist/child-xrHmytks.js +49 -0
- package/dist/child-xrHmytks.js.map +1 -0
- package/dist/deps/child/install.d.ts +1 -0
- package/dist/deps/child/install.js +33 -0
- package/dist/deps/child/install.js.map +1 -0
- package/dist/index.d.ts +221 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/instance-D7_UVdoN.js +15752 -0
- package/dist/instance-D7_UVdoN.js.map +1 -0
- package/dist/types-NjNy7gXX.js +57 -0
- package/dist/types-NjNy7gXX.js.map +1 -0
- package/package.json +87 -4
- package/src/actions/index.ts +622 -0
- package/src/addressing/index.ts +281 -0
- package/src/bin.ts +240 -0
- package/src/builder/child/dev.ts +383 -0
- package/src/builder/child/fields.ts +363 -0
- package/src/builder/child/main.ts +47 -0
- package/src/builder/child/plugins.ts +195 -0
- package/src/builder/child/scan.ts +459 -0
- package/src/builder/child/spa.ts +348 -0
- package/src/builder/coverage.ts +72 -0
- package/src/builder/index.ts +406 -0
- package/src/builder/types.ts +180 -0
- package/src/deps/child/install.ts +30 -0
- package/src/deps/index.ts +918 -0
- package/src/deps/installer.ts +321 -0
- package/src/deps/lock.ts +82 -0
- package/src/deps/scan.ts +266 -0
- package/src/deps/specifier.ts +64 -0
- package/src/deps/warm.ts +57 -0
- package/src/dev/index.ts +518 -0
- package/src/faults.ts +65 -0
- package/src/gate.ts +55 -0
- package/src/gateway/actions.ts +268 -0
- package/src/gateway/browser.ts +650 -0
- package/src/gateway/capabilities.ts +739 -0
- package/src/gateway/controls.ts +91 -0
- package/src/gateway/dev.ts +119 -0
- package/src/gateway/http.ts +229 -0
- package/src/gateway/index.ts +1209 -0
- package/src/gateway/integrity.ts +117 -0
- package/src/gateway/listener.ts +217 -0
- package/src/gateway/routes.ts +197 -0
- package/src/gateway/serve.ts +138 -0
- package/src/gc/index.ts +451 -0
- package/src/home/acquire.ts +276 -0
- package/src/home/index.ts +16 -0
- package/src/home/paths.ts +192 -0
- package/src/home/platform.ts +34 -0
- package/src/home/record.ts +76 -0
- package/src/home/settings.ts +113 -0
- package/src/index.ts +30 -0
- package/src/instance/index.ts +942 -0
- package/src/interaction/ephemeral.ts +300 -0
- package/src/interaction/events.ts +355 -0
- package/src/interaction/fields.ts +392 -0
- package/src/interaction/index.ts +1009 -0
- package/src/interaction/outbox.ts +493 -0
- package/src/interaction/policy.ts +26 -0
- package/src/interaction/results.ts +341 -0
- package/src/interaction/runs.ts +313 -0
- package/src/interaction/signals.ts +120 -0
- package/src/interaction/submissions.ts +585 -0
- package/src/interaction/subscriptions.ts +258 -0
- package/src/interaction/viewers.ts +173 -0
- package/src/interaction/wait.ts +201 -0
- package/src/ledger/index.ts +442 -0
- package/src/ledger/migrations.ts +583 -0
- package/src/ledger/requests.ts +105 -0
- package/src/ledger/types.ts +28 -0
- package/src/live/index.ts +573 -0
- package/src/log.ts +316 -0
- package/src/processes/child.ts +67 -0
- package/src/processes/environment.ts +64 -0
- package/src/processes/identity.ts +191 -0
- package/src/processes/index.ts +19 -0
- package/src/processes/manager.ts +543 -0
- package/src/protocol-server/client.ts +94 -0
- package/src/protocol-server/index.ts +91 -0
- package/src/protocol-server/server.ts +509 -0
- package/src/protocol-server/streams.ts +81 -0
- package/src/publish/checks.ts +92 -0
- package/src/publish/digest.ts +87 -0
- package/src/publish/index.ts +23 -0
- package/src/publish/manifest.ts +409 -0
- package/src/publish/methods.ts +96 -0
- package/src/publish/publisher.ts +913 -0
- package/src/publish/source.ts +272 -0
- package/src/publish/tar.ts +110 -0
- package/src/registry/index.ts +481 -0
- package/src/registry/store.ts +136 -0
- package/src/secrets/index.ts +223 -0
- package/src/service/idle.ts +64 -0
- package/src/service/index.ts +3 -0
- package/src/service/unit.ts +250 -0
- package/src/verify/fields.ts +125 -0
- package/src/verify/index.ts +913 -0
- package/src/verify/regression.ts +157 -0
- package/src/verify/runner.ts +230 -0
- package/src/verify/sandbox.ts +407 -0
- package/src/verify/typecheck.ts +371 -0
- package/src/workers/index.ts +17 -0
- package/src/workers/launcher.ts +260 -0
- package/src/workers/pool.ts +708 -0
package/src/gc/index.ts
ADDED
|
@@ -0,0 +1,451 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `mostra gc` (VR-06, D47, D140 item 14): removes what nothing references, on request only.
|
|
3
|
+
*
|
|
4
|
+
* - **Versions.** A version is kept while any {@link REFERENCE_SOURCES} names it. One nothing names
|
|
5
|
+
* loses its files; its ledger row, ID and number stay forever, and `version_collected` records
|
|
6
|
+
* the collection, so a content-addressed ID never gets a second number. Publishing its exact
|
|
7
|
+
* source again restores the files (publish's repair path) and clears the record.
|
|
8
|
+
* - **Everything else** gc collects belongs to the module that owns it: lock layouts and the store
|
|
9
|
+
* (`deps`), verify's caches (`verify`), events (`interaction`'s compaction, only when asked).
|
|
10
|
+
* - **Order.** A run holds the instance's gate alone, so no publish, verify or install runs beside
|
|
11
|
+
* it. It decides and records the collected versions in one transaction, then removes their files
|
|
12
|
+
* after the commit; when the process dies in between, recovery at start removes what the record
|
|
13
|
+
* says is gone. A real run lists exactly what a dry run lists.
|
|
14
|
+
*/
|
|
15
|
+
import { readdir, readFile, stat } from "node:fs/promises";
|
|
16
|
+
import { join } from "node:path";
|
|
17
|
+
import {
|
|
18
|
+
createDiagnostic,
|
|
19
|
+
type Diagnostic,
|
|
20
|
+
type GcCompacted,
|
|
21
|
+
type GcRunParams,
|
|
22
|
+
type GcRunResult,
|
|
23
|
+
type GcVersion,
|
|
24
|
+
gcRunMethod,
|
|
25
|
+
RpcError,
|
|
26
|
+
} from "@mostra/protocol";
|
|
27
|
+
import type { Dependencies } from "../deps/index.ts";
|
|
28
|
+
import type { Faults } from "../faults.ts";
|
|
29
|
+
import type { Gate } from "../gate.ts";
|
|
30
|
+
import { compactEvents } from "../interaction/index.ts";
|
|
31
|
+
import type { Ledger, LedgerDb } from "../ledger/index.ts";
|
|
32
|
+
import type { Log } from "../log.ts";
|
|
33
|
+
import { handler, type MethodHandler } from "../protocol-server/index.ts";
|
|
34
|
+
import { type Registry, removeTree } from "../registry/index.ts";
|
|
35
|
+
|
|
36
|
+
/** One reason a version is kept: a named source over the ledger columns it accounts for. */
|
|
37
|
+
export interface ReferenceSource {
|
|
38
|
+
/** What references the version, in words: `submissions`, `pinned links` … */
|
|
39
|
+
readonly name: string;
|
|
40
|
+
/** The ledger columns it accounts for, as `table.column` (the schema test checks them). */
|
|
41
|
+
readonly covers: readonly string[];
|
|
42
|
+
/** The version IDs it keeps, read inside gc's transaction at `now` (RFC 3339). */
|
|
43
|
+
keep(db: LedgerDb, now: string): Iterable<string>;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** A ledger column that names a version but keeps none, with the reason. */
|
|
47
|
+
export interface ExemptColumn {
|
|
48
|
+
readonly column: string;
|
|
49
|
+
readonly reason: string;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Every version ID written in a text: snapshots, anchors and review choices name them (D89). */
|
|
53
|
+
const VERSION_IDS = /ver_[0-9A-HJKMNP-TV-Z]{26}/g;
|
|
54
|
+
|
|
55
|
+
function* idsIn(texts: Iterable<string | null>): Iterable<string> {
|
|
56
|
+
for (const text of texts) if (text) yield* text.match(VERSION_IDS) ?? [];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const column = <T>(db: LedgerDb, sql: string, ...params: string[]): T[] =>
|
|
60
|
+
db.all<{ value: T }>(sql, ...params).map((row) => row.value);
|
|
61
|
+
|
|
62
|
+
const tableExists = (db: LedgerDb, table: string): boolean =>
|
|
63
|
+
db.get("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = ?", table) !== undefined;
|
|
64
|
+
|
|
65
|
+
/** The version a number names within its artifact: `SELECT id FROM version` joined by number. */
|
|
66
|
+
const BY_NUMBER = (alias: string, numberColumn: string): string =>
|
|
67
|
+
`(SELECT version.id FROM version WHERE version.artifact_id = ${alias}.artifact_id AND version.number = ${alias}.${numberColumn})`;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Why versions are kept, one named source each (D140 item 14). A table that names versions joins
|
|
71
|
+
* this list, or {@link EXEMPT_COLUMNS} with a reason; the schema test fails until it does.
|
|
72
|
+
*/
|
|
73
|
+
export const REFERENCE_SOURCES: readonly ReferenceSource[] = [
|
|
74
|
+
{
|
|
75
|
+
name: "latest versions",
|
|
76
|
+
covers: ["artifact.latest_version_id"],
|
|
77
|
+
keep: (db) =>
|
|
78
|
+
column<string>(
|
|
79
|
+
db,
|
|
80
|
+
"SELECT latest_version_id AS value FROM artifact WHERE latest_version_id IS NOT NULL",
|
|
81
|
+
),
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
// Following loads each new latest version (D63, LL-03): the latest is kept above, and this
|
|
85
|
+
// source says so, so a change to either rule shows in its test.
|
|
86
|
+
name: "followed artifacts",
|
|
87
|
+
covers: [],
|
|
88
|
+
keep: (db) =>
|
|
89
|
+
column<string>(
|
|
90
|
+
db,
|
|
91
|
+
"SELECT artifact.latest_version_id AS value FROM follow JOIN artifact ON artifact.id = follow.artifact_id WHERE artifact.latest_version_id IS NOT NULL",
|
|
92
|
+
),
|
|
93
|
+
},
|
|
94
|
+
{
|
|
95
|
+
name: "submissions",
|
|
96
|
+
covers: [
|
|
97
|
+
"submission.version_id",
|
|
98
|
+
"submission.latest_version_number",
|
|
99
|
+
"submission.snapshot_json",
|
|
100
|
+
],
|
|
101
|
+
keep: function* (db) {
|
|
102
|
+
yield* column<string>(db, "SELECT version_id AS value FROM submission");
|
|
103
|
+
yield* column<string>(
|
|
104
|
+
db,
|
|
105
|
+
`SELECT ${BY_NUMBER("submission", "latest_version_number")} AS value FROM submission WHERE latest_version_number IS NOT NULL`,
|
|
106
|
+
).filter((id) => id !== null);
|
|
107
|
+
yield* idsIn(column<string>(db, "SELECT snapshot_json AS value FROM submission"));
|
|
108
|
+
},
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
name: "views",
|
|
112
|
+
covers: ["view.version_id"],
|
|
113
|
+
keep: (db) => column<string>(db, "SELECT version_id AS value FROM view"),
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
name: "comment threads",
|
|
117
|
+
covers: ["thread.version_id", "thread.anchor_json"],
|
|
118
|
+
keep: function* (db) {
|
|
119
|
+
if (!tableExists(db, "thread")) return;
|
|
120
|
+
yield* column<string>(db, "SELECT version_id AS value FROM thread");
|
|
121
|
+
yield* idsIn(column<string>(db, "SELECT anchor_json AS value FROM thread"));
|
|
122
|
+
},
|
|
123
|
+
},
|
|
124
|
+
{
|
|
125
|
+
name: "action runs",
|
|
126
|
+
covers: ["action_run.version_id"],
|
|
127
|
+
keep: (db) => column<string>(db, "SELECT version_id AS value FROM action_run"),
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
name: "live sessions",
|
|
131
|
+
covers: ["session.version_id"],
|
|
132
|
+
keep: (db) =>
|
|
133
|
+
column<string>(db, "SELECT version_id AS value FROM session WHERE version_id IS NOT NULL"),
|
|
134
|
+
},
|
|
135
|
+
{
|
|
136
|
+
name: "live producer values",
|
|
137
|
+
covers: ["live_value.producer_version_id"],
|
|
138
|
+
keep: (db) =>
|
|
139
|
+
column<string>(
|
|
140
|
+
db,
|
|
141
|
+
"SELECT producer_version_id AS value FROM live_value WHERE producer_version_id IS NOT NULL",
|
|
142
|
+
),
|
|
143
|
+
},
|
|
144
|
+
{
|
|
145
|
+
// A review set's candidates may be versions (D23); a choice, a draft's or the artifact
|
|
146
|
+
// state's, names the one chosen. The version whose view last wrote a field is its orphan's
|
|
147
|
+
// `fromVersion` (D109).
|
|
148
|
+
name: "review sets and fields",
|
|
149
|
+
covers: ["field.value_json", "field.version_number"],
|
|
150
|
+
keep: function* (db) {
|
|
151
|
+
yield* idsIn(
|
|
152
|
+
column<string>(db, "SELECT value_json AS value FROM field WHERE field_id LIKE 'review.%'"),
|
|
153
|
+
);
|
|
154
|
+
yield* column<string>(
|
|
155
|
+
db,
|
|
156
|
+
`SELECT (SELECT version.id FROM version WHERE version.artifact_id = document.artifact_id AND version.number = field.version_number) AS value FROM field JOIN document ON document.id = field.document_id WHERE field.version_number IS NOT NULL`,
|
|
157
|
+
).filter((id) => id !== null);
|
|
158
|
+
},
|
|
159
|
+
},
|
|
160
|
+
{
|
|
161
|
+
name: "pinned links",
|
|
162
|
+
covers: ["token.pinned_version"],
|
|
163
|
+
keep: (db, now) =>
|
|
164
|
+
column<string>(
|
|
165
|
+
db,
|
|
166
|
+
`SELECT ${BY_NUMBER("token", "pinned_version")} AS value FROM token WHERE pinned_version IS NOT NULL AND revoked_at IS NULL AND (expires_at IS NULL OR expires_at > ?)`,
|
|
167
|
+
now,
|
|
168
|
+
).filter((id) => id !== null),
|
|
169
|
+
},
|
|
170
|
+
{
|
|
171
|
+
// The regression baseline (D138 item 4): the newest version whose recorded verdict passed.
|
|
172
|
+
name: "regression baselines",
|
|
173
|
+
covers: ["version.verify_json"],
|
|
174
|
+
keep: (db) =>
|
|
175
|
+
column<string>(
|
|
176
|
+
db,
|
|
177
|
+
"SELECT (SELECT id FROM version AS newest WHERE newest.artifact_id = artifact.id AND json_extract(newest.verify_json, '$.status') = 'pass' ORDER BY newest.number DESC LIMIT 1) AS value FROM artifact",
|
|
178
|
+
).filter((id) => id !== null),
|
|
179
|
+
},
|
|
180
|
+
];
|
|
181
|
+
|
|
182
|
+
/** Columns that name versions but keep none. */
|
|
183
|
+
export const EXEMPT_COLUMNS: readonly ExemptColumn[] = [
|
|
184
|
+
{ column: "version.id", reason: "the version itself" },
|
|
185
|
+
{ column: "version.number", reason: "the version itself" },
|
|
186
|
+
{
|
|
187
|
+
column: "version_collected.version_id",
|
|
188
|
+
reason: "the record that gc collected the version",
|
|
189
|
+
},
|
|
190
|
+
{
|
|
191
|
+
column: "schema_migration.version",
|
|
192
|
+
reason: "the ledger's schema version, not an artifact version",
|
|
193
|
+
},
|
|
194
|
+
{
|
|
195
|
+
column: "dependency_package.version",
|
|
196
|
+
reason: "an npm package's version in the store index, not an artifact version",
|
|
197
|
+
},
|
|
198
|
+
{
|
|
199
|
+
column: "schema_migration.runtime_version",
|
|
200
|
+
reason: "the runtime release that ran a migration",
|
|
201
|
+
},
|
|
202
|
+
];
|
|
203
|
+
|
|
204
|
+
/** The versions some source keeps. */
|
|
205
|
+
export function keptVersions(db: LedgerDb, now: string): Set<string> {
|
|
206
|
+
const kept = new Set<string>();
|
|
207
|
+
for (const source of REFERENCE_SOURCES) for (const id of source.keep(db, now)) kept.add(id);
|
|
208
|
+
return kept;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** A version gc would collect: not collected yet, and kept by no source. */
|
|
212
|
+
export interface Candidate {
|
|
213
|
+
readonly artifactId: string;
|
|
214
|
+
readonly versionId: string;
|
|
215
|
+
readonly number: number;
|
|
216
|
+
readonly sourceDigest: string;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/** The versions gc collects now: not collected yet, and kept by no {@link REFERENCE_SOURCES}. */
|
|
220
|
+
export function collectableVersions(db: LedgerDb, now: string): Candidate[] {
|
|
221
|
+
const kept = keptVersions(db, now);
|
|
222
|
+
return db
|
|
223
|
+
.all<{ id: string; artifact_id: string; number: number; source_digest: string }>(
|
|
224
|
+
"SELECT id, artifact_id, number, source_digest FROM version WHERE id NOT IN (SELECT version_id FROM version_collected) ORDER BY artifact_id, number",
|
|
225
|
+
)
|
|
226
|
+
.filter((row) => !kept.has(row.id))
|
|
227
|
+
.map((row) => ({
|
|
228
|
+
artifactId: row.artifact_id,
|
|
229
|
+
versionId: row.id,
|
|
230
|
+
number: row.number,
|
|
231
|
+
sourceDigest: row.source_digest,
|
|
232
|
+
}));
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** What gc needs of its instance. */
|
|
236
|
+
export interface GcOptions {
|
|
237
|
+
readonly ledger: Ledger;
|
|
238
|
+
readonly registry: Registry;
|
|
239
|
+
readonly log: Log;
|
|
240
|
+
/** The instance's gate: gc holds it alone. */
|
|
241
|
+
readonly gate: Gate;
|
|
242
|
+
readonly dependencies: () => Dependencies;
|
|
243
|
+
/** Verify's caches past their cap; absent while verify was never used. */
|
|
244
|
+
readonly caches: (dryRun: boolean) => Promise<{ readonly count: number; readonly bytes: number }>;
|
|
245
|
+
/** The clock (tests move it); default `Date.now`. */
|
|
246
|
+
readonly now?: () => number;
|
|
247
|
+
/**
|
|
248
|
+
* Lockfiles in use outside the ledger: open dev sessions run on them (Opus review finding 2).
|
|
249
|
+
* Read under the gate, which a session's resolution shares, so none appears mid-run.
|
|
250
|
+
*/
|
|
251
|
+
readonly locksInUse?: () => readonly string[];
|
|
252
|
+
/** Test fault points: `gc:dependencies` fails the dependency step. */
|
|
253
|
+
readonly faults?: Faults;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/** The home's garbage collector. */
|
|
257
|
+
export interface Gc {
|
|
258
|
+
/** `gc/run`: collects, or with `dryRun` lists, what nothing references. */
|
|
259
|
+
run(params: GcRunParams): Promise<GcRunResult>;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/** Creates the garbage collector of one instance. */
|
|
263
|
+
export function createGc(options: GcOptions): Gc {
|
|
264
|
+
const { ledger, registry, log } = options;
|
|
265
|
+
const now = options.now ?? Date.now;
|
|
266
|
+
|
|
267
|
+
/** The bytes a directory or file holds; 0 when it is gone. */
|
|
268
|
+
const sizeOf = async (path: string): Promise<number> => {
|
|
269
|
+
const info = await stat(path).catch(() => undefined);
|
|
270
|
+
if (!info) return 0;
|
|
271
|
+
if (!info.isDirectory()) return info.size;
|
|
272
|
+
let total = 0;
|
|
273
|
+
for (const entry of await readdir(path, { withFileTypes: true }).catch(() => [])) {
|
|
274
|
+
total += await sizeOf(join(path, entry.name));
|
|
275
|
+
}
|
|
276
|
+
return total;
|
|
277
|
+
};
|
|
278
|
+
|
|
279
|
+
return {
|
|
280
|
+
run: (params) =>
|
|
281
|
+
options.gate.exclusive(async () => {
|
|
282
|
+
const dryRun = params.dryRun === true;
|
|
283
|
+
const at = new Date(now()).toISOString();
|
|
284
|
+
// 1. Decide, and for a real run record, in one transaction.
|
|
285
|
+
const chosen = ledger.write((db) => {
|
|
286
|
+
const found = collectableVersions(db, at);
|
|
287
|
+
if (!dryRun) {
|
|
288
|
+
for (const candidate of found) {
|
|
289
|
+
db.run(
|
|
290
|
+
"INSERT INTO version_collected (version_id, collected_at) VALUES (?, ?)",
|
|
291
|
+
candidate.versionId,
|
|
292
|
+
at,
|
|
293
|
+
);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
return found;
|
|
297
|
+
});
|
|
298
|
+
// Source snapshots, and lockfiles, of the versions that keep their files.
|
|
299
|
+
const chosenIds = new Set(chosen.map((candidate) => candidate.versionId));
|
|
300
|
+
const keptRows = ledger
|
|
301
|
+
.read((db) =>
|
|
302
|
+
db.all<{ id: string; artifact_id: string; source_digest: string }>(
|
|
303
|
+
"SELECT id, artifact_id, source_digest FROM version WHERE id NOT IN (SELECT version_id FROM version_collected)",
|
|
304
|
+
),
|
|
305
|
+
)
|
|
306
|
+
.filter((row) => !chosenIds.has(row.id));
|
|
307
|
+
const keptSnapshots = new Set(
|
|
308
|
+
keptRows.map((row) => `${row.artifact_id}/${row.source_digest}`),
|
|
309
|
+
);
|
|
310
|
+
const snapshots = [
|
|
311
|
+
...new Set(
|
|
312
|
+
chosen
|
|
313
|
+
.map((candidate) => `${candidate.artifactId}/${candidate.sourceDigest}`)
|
|
314
|
+
.filter((key) => !keptSnapshots.has(key)),
|
|
315
|
+
),
|
|
316
|
+
];
|
|
317
|
+
// A step that fails leaves its files and becomes a warning; what earlier steps removed
|
|
318
|
+
// is still reported (Opus review finding 1). The tombstones are committed: recovery at
|
|
319
|
+
// start removes any version file a failed removal left.
|
|
320
|
+
const warnings: Diagnostic[] = [];
|
|
321
|
+
const incomplete = (step: string, error: unknown): void => {
|
|
322
|
+
const cause =
|
|
323
|
+
error instanceof RpcError
|
|
324
|
+
? `${error.diagnostic.code}: ${error.diagnostic.message}`
|
|
325
|
+
: error instanceof Error
|
|
326
|
+
? error.message
|
|
327
|
+
: String(error);
|
|
328
|
+
warnings.push(
|
|
329
|
+
createDiagnostic("MOSTRA_HOME_GC_INCOMPLETE", {
|
|
330
|
+
message: `gc couldn't finish ${step}: ${cause}`,
|
|
331
|
+
}),
|
|
332
|
+
);
|
|
333
|
+
log.warn("gc.incomplete", { step, cause });
|
|
334
|
+
};
|
|
335
|
+
// 2. Sizes, then the files: after the commit, so a crash leaves only what recovery removes.
|
|
336
|
+
const versions: GcVersion[] = [];
|
|
337
|
+
for (const candidate of chosen) {
|
|
338
|
+
const dir = registry.store.versionDir(candidate.versionId);
|
|
339
|
+
versions.push({
|
|
340
|
+
artifactId: candidate.artifactId,
|
|
341
|
+
versionId: candidate.versionId,
|
|
342
|
+
number: candidate.number,
|
|
343
|
+
bytes: await sizeOf(dir),
|
|
344
|
+
});
|
|
345
|
+
if (!dryRun)
|
|
346
|
+
await removeTree(dir).catch((error: unknown) =>
|
|
347
|
+
incomplete(`removing ${candidate.versionId}'s files`, error),
|
|
348
|
+
);
|
|
349
|
+
}
|
|
350
|
+
let snapshotBytes = 0;
|
|
351
|
+
for (const key of snapshots) {
|
|
352
|
+
const [artifactId, sourceDigest] = key.split("/") as [string, string];
|
|
353
|
+
const path = registry.store.snapshotPath(artifactId, sourceDigest);
|
|
354
|
+
snapshotBytes += await sizeOf(path);
|
|
355
|
+
if (!dryRun)
|
|
356
|
+
await removeTree(path).catch((error: unknown) =>
|
|
357
|
+
incomplete("removing a source snapshot", error),
|
|
358
|
+
);
|
|
359
|
+
}
|
|
360
|
+
// 3. Lock layouts and the store: what the versions that keep their files still need.
|
|
361
|
+
const keep: string[] = [...(options.locksInUse?.() ?? [])];
|
|
362
|
+
for (const row of keptRows) {
|
|
363
|
+
const text = await readFile(
|
|
364
|
+
join(registry.store.versionDir(row.id), "lock.yaml"),
|
|
365
|
+
"utf8",
|
|
366
|
+
).catch(() => undefined);
|
|
367
|
+
if (text !== undefined) keep.push(text);
|
|
368
|
+
}
|
|
369
|
+
const none = { count: 0, bytes: 0 };
|
|
370
|
+
const deps = await Promise.resolve()
|
|
371
|
+
.then(() => {
|
|
372
|
+
options.faults?.at("gc:dependencies");
|
|
373
|
+
return options.dependencies().collect({ keep, dryRun });
|
|
374
|
+
})
|
|
375
|
+
.catch((error: unknown) => {
|
|
376
|
+
incomplete("collecting lock layouts and the package store", error);
|
|
377
|
+
return {
|
|
378
|
+
lockLayouts: { ...none, names: [] as string[] },
|
|
379
|
+
storePackages: { ...none, packages: [] as string[] },
|
|
380
|
+
};
|
|
381
|
+
});
|
|
382
|
+
if ("failed" in deps.storePackages && deps.storePackages.failed)
|
|
383
|
+
incomplete("pruning the package store", new RpcError(deps.storePackages.failed));
|
|
384
|
+
// 4. Verify's caches.
|
|
385
|
+
const caches = await options.caches(dryRun).catch((error: unknown) => {
|
|
386
|
+
incomplete("collecting verify's caches", error);
|
|
387
|
+
return none;
|
|
388
|
+
});
|
|
389
|
+
// 5. Events, only when asked (D47).
|
|
390
|
+
let compacted: GcCompacted | undefined;
|
|
391
|
+
if (params.compactEvents !== undefined) {
|
|
392
|
+
const before = Number(params.compactEvents.before);
|
|
393
|
+
const pending = (db: LedgerDb, artifactId: string): number =>
|
|
394
|
+
db.get<{ n: number }>(
|
|
395
|
+
"SELECT COUNT(*) AS n FROM event WHERE artifact_id = ? AND cursor < ? AND type <> 'artifact.compacted'",
|
|
396
|
+
artifactId,
|
|
397
|
+
before,
|
|
398
|
+
)?.n ?? 0;
|
|
399
|
+
const compact = (db: LedgerDb): GcCompacted => {
|
|
400
|
+
const done: { artifactId: string; removed: number }[] = [];
|
|
401
|
+
for (const { artifact_id: artifactId } of db.all<{ artifact_id: string }>(
|
|
402
|
+
"SELECT DISTINCT artifact_id FROM event WHERE cursor < ? AND artifact_id IS NOT NULL ORDER BY artifact_id",
|
|
403
|
+
before,
|
|
404
|
+
)) {
|
|
405
|
+
const removed = pending(db, artifactId);
|
|
406
|
+
if (removed === 0) continue;
|
|
407
|
+
if (!dryRun) compactEvents(db, artifactId, before);
|
|
408
|
+
done.push({ artifactId, removed });
|
|
409
|
+
}
|
|
410
|
+
return { artifacts: done, removed: done.reduce((acc, item) => acc + item.removed, 0) };
|
|
411
|
+
};
|
|
412
|
+
try {
|
|
413
|
+
compacted = dryRun ? ledger.read(compact) : ledger.write(compact);
|
|
414
|
+
} catch (error) {
|
|
415
|
+
incomplete("compacting events", error);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
const result: GcRunResult = {
|
|
419
|
+
dryRun,
|
|
420
|
+
versions,
|
|
421
|
+
snapshots: { count: snapshots.length, bytes: snapshotBytes },
|
|
422
|
+
lockLayouts: { count: deps.lockLayouts.count, bytes: deps.lockLayouts.bytes },
|
|
423
|
+
storePackages: { count: deps.storePackages.count, bytes: deps.storePackages.bytes },
|
|
424
|
+
caches,
|
|
425
|
+
bytes:
|
|
426
|
+
versions.reduce((acc, version) => acc + version.bytes, 0) +
|
|
427
|
+
snapshotBytes +
|
|
428
|
+
deps.lockLayouts.bytes +
|
|
429
|
+
deps.storePackages.bytes +
|
|
430
|
+
caches.bytes,
|
|
431
|
+
...(compacted ? { compacted } : {}),
|
|
432
|
+
...(warnings.length > 0 ? { warnings } : {}),
|
|
433
|
+
};
|
|
434
|
+
log.info(dryRun ? "gc.planned" : "gc.collected", {
|
|
435
|
+
versions: versions.length,
|
|
436
|
+
snapshots: snapshots.length,
|
|
437
|
+
lockLayouts: deps.lockLayouts.count,
|
|
438
|
+
storePackages: deps.storePackages.count,
|
|
439
|
+
caches: caches.count,
|
|
440
|
+
bytes: result.bytes,
|
|
441
|
+
...(compacted ? { compactedEvents: compacted.removed } : {}),
|
|
442
|
+
});
|
|
443
|
+
return result;
|
|
444
|
+
}),
|
|
445
|
+
};
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
/** The handler of `gc/run`. */
|
|
449
|
+
export function gcMethods(gc: Gc): MethodHandler[] {
|
|
450
|
+
return [handler(gcRunMethod, (params) => gc.run(params))];
|
|
451
|
+
}
|