rvlib-shipkit 2.24.0 → 2.26.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/CHANGELOG.md +33 -0
- package/package.json +2 -2
- package/src/agent-time/hooks.ts +329 -0
- package/src/agent-time/live.ts +153 -0
- package/src/agent-time/render.ts +14 -2
- package/src/agent-time/report.ts +56 -4
- package/src/agent-time/store.ts +59 -5
- package/src/annex/offload.ts +16 -7
- package/src/check.ts +3 -1
- package/src/checks/SK004-gitignore-policy.check.ts +32 -11
- package/src/checks/SK027-agent-wiki-links.check.ts +1 -1
- package/src/checks/SK033-global-contribution.check.ts +1 -1
- package/src/checks/SK036-draft-media-size.check.ts +1 -1
- package/src/checks/SK040-release-words.check.ts +1 -1
- package/src/checks/SK042-post-kinds.check.ts +1 -1
- package/src/checks/SK045-slow-hooks.check.ts +47 -0
- package/src/checks/SK046-hook-records.check.ts +76 -0
- package/src/cli/cmd_Annex.ts +12 -7
- package/src/cli/cmd_Assets.ts +5 -5
- package/src/cli/cmd_Backup.ts +14 -6
- package/src/cli/cmd_Capacity.ts +10 -6
- package/src/cli/cmd_Checks.ts +12 -1
- package/src/cli/cmd_Ci.ts +2 -2
- package/src/cli/cmd_Commands.ts +11 -2
- package/src/cli/cmd_ConfigKeys.ts +17 -0
- package/src/cli/cmd_Home.ts +1 -1
- package/src/cli/cmd_Keys.ts +4 -1
- package/src/cli/cmd_Lanes.ts +35 -0
- package/src/cli/cmd_Mv.ts +6 -7
- package/src/cli/cmd_Rec.ts +22 -7
- package/src/cli/cmd_Releases.ts +30 -0
- package/src/cli/cmd_Review.ts +130 -43
- package/src/cli/cmd_Shot.ts +15 -4
- package/src/cli/cmd_Status.ts +6 -5
- package/src/cli/cmd_Time.ts +18 -6
- package/src/cli/cmd_Veille.ts +10 -6
- package/src/cli/family-help.ts +136 -0
- package/src/cli/family-pages.ts +0 -19
- package/src/cli/kit/calls.ts +35 -0
- package/src/cli/kit/project-cli.ts +2 -12
- package/src/cli/registry.ts +41 -7
- package/src/cli.ts +8 -4
- package/src/commands/runner.ts +5 -1
- package/src/core/capacity.ts +89 -33
- package/src/core/check-runner.ts +5 -1
- package/src/core/floor-resolve.ts +6 -2
- package/src/core/floor.ts +6 -2
- package/src/core/history-files.ts +22 -0
- package/src/core/jobs.ts +35 -16
- package/src/core/layout.ts +2 -2
- package/src/core/nearest.ts +16 -0
- package/src/core/remove-tree.ts +19 -0
- package/src/core/runs.ts +12 -3
- package/src/core/signal-name.ts +6 -0
- package/src/core/throwaway-worktree.ts +22 -4
- package/src/core/trace-audit.ts +22 -4
- package/src/core/transient.ts +6 -0
- package/src/drill/run.ts +1 -1
- package/src/home/daemon-jobs.ts +41 -4
- package/src/home/machine-temp.ts +2 -10
- package/src/ledger/gate-checks.ts +26 -4
- package/src/ledger/git-key.ts +25 -4
- package/src/ledger/git-scan.ts +59 -7
- package/src/media-store/backup/daemon.ts +103 -20
- package/src/media-store/backup/run.ts +21 -15
- package/src/media-store/backup/sources.ts +163 -21
- package/src/media-store/backup/watch.ts +106 -0
- package/src/media-store/daemon.ts +36 -8
- package/src/media-store/hand.ts +3 -1
- package/src/mv/mv-md.ts +50 -18
- package/src/pb/run-pb.ts +2 -10
- package/src/plan/repo-source.ts +25 -14
- package/src/recordings/model.ts +1 -1
- package/src/reflect/transcript.ts +21 -3
- package/src/release/run-bump.ts +3 -2
- package/src/release/run-release.ts +12 -4
- package/src/releases/model.ts +18 -0
- package/src/releases/store.ts +13 -1
- package/src/repo-config.ts +11 -1
- package/src/review/cells.ts +2 -1
- package/src/review/format.ts +1 -1
- package/src/review/inbox-hook.ts +35 -0
- package/src/review/model.ts +26 -7
- package/src/review/store.ts +15 -7
- package/src/review/trailer-model.ts +2 -2
- package/src/shot/client.ts +4 -2
- package/src/shot/serve-shot.ts +71 -2
- package/src/shot/shooter.ts +65 -11
- package/src/shot/shot-ask.ts +32 -9
- package/src/suite/judge-suites.ts +77 -10
- package/src/suite/select.ts +13 -2
- package/src/suite/suite-cli.ts +72 -31
- package/src/suite/test-units.ts +31 -12
- package/src/trailer/graph.ts +2 -1
- package/src/ts/config.ts +12 -35
- package/src/ts/rename.ts +31 -17
- package/src/ts/workspaces.ts +45 -0
- package/src/wiki/run-wiki.ts +8 -2
- package/src/wiki/scan-md.ts +42 -1
package/src/ledger/git-scan.ts
CHANGED
|
@@ -55,6 +55,10 @@ export type ScanResult = {
|
|
|
55
55
|
oidByPath: Map<string, string>
|
|
56
56
|
/** declared paths that are not on disk — the caller must not cache them */
|
|
57
57
|
missing: string[]
|
|
58
|
+
/** declared paths that are a submodule, keyed (by the commit it stands at) or not. Whether one holds a change
|
|
59
|
+
* is read from its own repo, not from this index, so a result that looked at one is never kept from one scan
|
|
60
|
+
* to the next, in either direction */
|
|
61
|
+
submodules?: string[]
|
|
58
62
|
/** repo-relative path → ignored content under it that is NOT in the key
|
|
59
63
|
* (node_modules only, by policy: toolchain-identity.ts owns installs) */
|
|
60
64
|
excluded: Map<string, string[]>
|
|
@@ -200,9 +204,19 @@ function dropAtExit(p: string): void {
|
|
|
200
204
|
/** a scratch index older than this is swept whatever its pid: pids are reused */
|
|
201
205
|
export const SCAN_INDEX_KEEP_MS = 60 * 60 * 1000
|
|
202
206
|
|
|
207
|
+
/** the lock git leaves on the warm ignored index is stale past this: git holds it for one command, seconds at most
|
|
208
|
+
* on the largest tree measured, and a git that was killed leaves it forever. One left in a repo failed every scan
|
|
209
|
+
* there for eleven days: no check had a key, nothing was cached, and each save ran the whole gate again */
|
|
210
|
+
export const SCAN_LOCK_STALE_MS = 2 * 60 * 1000
|
|
211
|
+
const IGNORED_LOCK = 'scan-ignored.idx.lock'
|
|
212
|
+
|
|
203
213
|
/** the scratch indexes of processes that are gone (killed ones leave theirs),
|
|
204
214
|
* and any past the keep window */
|
|
205
215
|
export function sweepScanIndexes(dir: string, now: number): void {
|
|
216
|
+
try {
|
|
217
|
+
const lock = join(dir, IGNORED_LOCK)
|
|
218
|
+
if (statSync(lock).mtimeMs < now - SCAN_LOCK_STALE_MS) rmSync(lock, { force: true })
|
|
219
|
+
} catch { /* no lock, or a live git just released it */ }
|
|
206
220
|
try {
|
|
207
221
|
for (const f of readdirSync(dir)) {
|
|
208
222
|
const m = /^scan-(\d+)\.idx(\.tmp)?$/.exec(f)
|
|
@@ -339,14 +353,17 @@ function stage(root: string, wanted: string[], prior: ScanResult | undefined, o:
|
|
|
339
353
|
// an earlier scan's object dir held and dropped: their ids are right, and
|
|
340
354
|
// `--missing-ok` lets the tree be written from the ids alone
|
|
341
355
|
const ignoredTreeOid = forceAdd.length > 0 ? gitOrThrow(root, ['write-tree', '--missing-ok'], on(ignoredIndex)).trim() : null
|
|
356
|
+
const submodules: string[] = []
|
|
357
|
+
/** the declared paths the tree holds as a submodule, with the commit each stands at */
|
|
358
|
+
const gitlinks = new Map<string, string>()
|
|
342
359
|
const done = (oidByPath: Map<string, string>, files?: readonly string[]): ScanResult => {
|
|
343
|
-
const r: ScanResult = { treeOid, oidByPath, missing: [...new Set(missing)].sort(), excluded, ignored, ignoredTreeOid, ...(files == null ? {} : { files }), ms: performance.now() - started }
|
|
344
|
-
if (memoKey != null) remember(memoKey, { ...r, oidByPath: new Map(oidByPath), missing: [...r.missing] })
|
|
360
|
+
const r: ScanResult = { treeOid, oidByPath, missing: [...new Set(missing)].sort(), excluded, ignored, ignoredTreeOid, ...(submodules.length > 0 ? { submodules: [...submodules] } : {}), ...(files == null ? {} : { files }), ms: performance.now() - started }
|
|
361
|
+
if (memoKey != null && submodules.length === 0) remember(memoKey, { ...r, oidByPath: new Map(oidByPath), missing: [...r.missing] })
|
|
345
362
|
return r
|
|
346
363
|
}
|
|
347
364
|
// both root trees unchanged and the same paths on disk: every path under
|
|
348
365
|
// them names the same object, so the per path answer is the prior one
|
|
349
|
-
if (prior != null && prior.treeOid === treeOid && prior.ignoredTreeOid === ignoredTreeOid && prior.missing.join('\n') === [...new Set(missing)].sort().join('\n') && (o.treeOnly !== true || prior.files != null))
|
|
366
|
+
if (prior != null && !(prior.submodules?.length) && prior.treeOid === treeOid && prior.ignoredTreeOid === ignoredTreeOid && prior.missing.join('\n') === [...new Set(missing)].sort().join('\n') && (o.treeOnly !== true || prior.files != null))
|
|
350
367
|
return done(new Map(prior.oidByPath), prior.files)
|
|
351
368
|
const oidByPath = new Map<string, string>()
|
|
352
369
|
// a declared path is relative to root, and `<tree>:<path>` reads from the
|
|
@@ -376,19 +393,54 @@ function stage(root: string, wanted: string[], prior: ScanResult | undefined, o:
|
|
|
376
393
|
throw new GitScanError(`git cat-file --batch-check answered ${lines.length} of ${paths.length} paths`)
|
|
377
394
|
paths.forEach((path, i) => {
|
|
378
395
|
const oid = lines[i]!.split(' ')[0] ?? ''
|
|
379
|
-
|
|
380
|
-
//
|
|
381
|
-
|
|
382
|
-
|
|
396
|
+
if (/^[0-9a-f]{40}$/.test(oid)) return void oidByPath.set(path, oid)
|
|
397
|
+
// `missing` is also git's word for an entry whose OBJECT is gone: the warm index keeps a file's entry
|
|
398
|
+
// from scan to scan, git writes no blob for an entry that did not move, and the objects of the scan
|
|
399
|
+
// that wrote it were dropped with that scan. The tree still names the file, and that id is its content
|
|
400
|
+
const entry = entryOf(root, root_, underRoot(path), on(index))
|
|
401
|
+
if (entry?.mode === GITLINK) gitlinks.set(path, entry.oid)
|
|
402
|
+
else if (entry != null) return void oidByPath.set(path, entry.oid)
|
|
403
|
+
// a path git cannot name is a path we cannot key on
|
|
404
|
+
if (!missing.includes(path)) missing.push(path)
|
|
383
405
|
})
|
|
384
406
|
}
|
|
385
407
|
|
|
408
|
+
// a submodule is a commit of another repo, so no object here names it. While it holds no change of its own, the
|
|
409
|
+
// commit it stands at is its content; with one, it stays a path git cannot name, and nothing is keyed on it
|
|
410
|
+
for (const path of [...missing]) {
|
|
411
|
+
if (path === WHOLE_TREE || !existsSync(join(root, path))) continue
|
|
412
|
+
const link = gitlinks.get(path)
|
|
413
|
+
if (link == null) continue
|
|
414
|
+
submodules.push(path)
|
|
415
|
+
if (!submoduleClean(join(root, path))) continue
|
|
416
|
+
oidByPath.set(path, link)
|
|
417
|
+
missing.splice(missing.indexOf(path), 1)
|
|
418
|
+
}
|
|
386
419
|
if (o.treeOnly !== true) return done(oidByPath)
|
|
387
420
|
const whole = oidByPath.get(WHOLE_TREE)
|
|
388
421
|
if (whole == null) throw new GitScanError('git could not name the work tree')
|
|
389
422
|
return done(oidByPath, filesOf(root, whole, on(indexFile)))
|
|
390
423
|
}
|
|
391
424
|
|
|
425
|
+
/** the mode of a tree entry that is a submodule: its id is a commit of another repo */
|
|
426
|
+
const GITLINK = '160000'
|
|
427
|
+
|
|
428
|
+
/** what a tree holds at `path`, read from the tree itself: its mode and its id, whether the object is at hand or not */
|
|
429
|
+
function entryOf(root: string, tree: string, path: string, env: Record<string, string>): { mode: string; oid: string } | null {
|
|
430
|
+
const p = Bun.spawnSync([gitBin(), 'ls-tree', tree, '--', path], { cwd: root, stdout: 'pipe', stderr: 'ignore', env: { ...process.env, ...env } })
|
|
431
|
+
const m = /^(\d{6}) \w+ ([0-9a-f]{40})\t/.exec(p.stdout.toString())
|
|
432
|
+
return p.exitCode === 0 && m ? { mode: m[1]!, oid: m[2]! } : null
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/** a submodule's work tree is there and holds no change of its own: nothing modified, nothing untracked, in it or
|
|
436
|
+
* in a submodule of its own. A folder with no `.git` is a submodule nobody filled (a new worktree has them so): git
|
|
437
|
+
* would answer there for the repo around it, so it is never clean. The two flags hold whatever its config says */
|
|
438
|
+
function submoduleClean(dir: string): boolean {
|
|
439
|
+
if (!existsSync(join(dir, '.git'))) return false
|
|
440
|
+
const p = Bun.spawnSync([gitBin(), 'status', '--porcelain', '--untracked-files=all', '--ignore-submodules=none'], { cwd: dir, stdout: 'pipe', stderr: 'ignore' })
|
|
441
|
+
return p.exitCode === 0 && p.stdout.toString().trim() === ''
|
|
442
|
+
}
|
|
443
|
+
|
|
392
444
|
/** a tree id names its content, so its file list is a pure function of the
|
|
393
445
|
* id and one listing serves every scan of that tree. `--full-tree` keeps it
|
|
394
446
|
* so: without it git filters the listing by the cwd's path under the work
|
|
@@ -1,26 +1,55 @@
|
|
|
1
|
-
// the daemon's backup job (wiki tools/shipkit/media-store.md,
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
1
|
+
// the daemon's backup job (wiki tools/shipkit/media-store-backup.md, The daemon). The daemon watches the sources
|
|
2
|
+
// (watch.ts) and starts a look only when one may have moved, a run waits, or a clock says so (`backupDue`, a few
|
|
3
|
+
// reads of the machine db). The look, in its child process, reads again only the paths the watch heard, on the list
|
|
4
|
+
// the last look kept; a full walk every SAFETY_MS catches what no event told. Once the stamp moved and then held
|
|
5
|
+
// still for one look (or waited WAIT_MAX_MS while sources kept moving), a run, at most one each 10 min; a run every
|
|
6
|
+
// 6 h whatever moved, which reads every byte again
|
|
5
7
|
import { machineGet, machineSet } from '../../core/machine-db.ts'
|
|
8
|
+
import type { RepoBackup } from '../../repo-config.ts'
|
|
6
9
|
import type { Space } from '../space.ts'
|
|
7
10
|
import { backupRun, lastRun } from './run.ts'
|
|
8
|
-
import { collect, localFiles, stampOfSources } from './sources.ts'
|
|
11
|
+
import { backupOfRepo, collect, localFiles, missedBy, movesLocals, patchLocals, refreshSources, stampOfSources, type Collect, type Source } from './sources.ts'
|
|
9
12
|
|
|
13
|
+
/** the daemon looks this often: a look starts the child only when `backupDue` says so */
|
|
10
14
|
export const BACKUP_EVERY_MS = 120_000
|
|
11
|
-
const FLOOR_MS = 10 * 60_000
|
|
12
|
-
const EVERY_MS = 6 * 3600_000
|
|
15
|
+
export const FLOOR_MS = 10 * 60_000
|
|
16
|
+
export const EVERY_MS = 6 * 3600_000
|
|
17
|
+
/** a full walk at least this often: a watch that died, a root nobody watches, an event the system dropped */
|
|
18
|
+
export const SAFETY_MS = 30 * 60_000
|
|
19
|
+
/** a change that waits this long goes up even while other sources keep moving: an agent that writes every minute
|
|
20
|
+
* never holds the rest back */
|
|
21
|
+
export const WAIT_MAX_MS = 30 * 60_000
|
|
13
22
|
/** the git walk for `*.local.*` files, kept this long per repo */
|
|
14
23
|
const LOCALS_MS = 30 * 60_000
|
|
15
24
|
|
|
16
|
-
/** the stamp the last look saw
|
|
17
|
-
|
|
25
|
+
/** the stamp the last look saw, the one the last good run sent, and the last full walk; `backup-` rows are left out
|
|
26
|
+
* of the snapshot */
|
|
27
|
+
export const STAMPS = 'backup-stamps'
|
|
18
28
|
const LOCALS = 'backup-locals'
|
|
19
29
|
const NO_SPACE = 'backup-no-space'
|
|
30
|
+
/** the list the last look kept, so the next one reads only what moved */
|
|
31
|
+
const SOURCES = 'backup-sources'
|
|
32
|
+
/** the paths the daemon heard, handed to the look it starts: kept undone until a look took them */
|
|
33
|
+
export const BATCH = 'backup-moved'
|
|
34
|
+
/** each repo's `backup.paths`, for the daemon's watch: a look writes them, the watch reads them */
|
|
35
|
+
export const NAMED = 'backup-named'
|
|
20
36
|
|
|
21
|
-
|
|
37
|
+
/** `waitAt`: since when a stamp the last run did not send waits */
|
|
38
|
+
export type Stamps = { seen: string; ran: string; fullAt?: number; waitAt?: number }
|
|
39
|
+
export type Batch = { paths: string[]; all: boolean; done: boolean }
|
|
22
40
|
type Locals = Record<string, { at: number; files: string[] }>
|
|
23
41
|
|
|
42
|
+
/** each repo's `*.local.*` files as the daemon's looks keep them, for a reader that only looks (`sk backup --check`):
|
|
43
|
+
* the kept list while it is fresh, else the repo's own git walk, which takes seconds in a repo with a large ignored
|
|
44
|
+
* folder. Nothing is written */
|
|
45
|
+
export function keptLocals(now: () => number = Date.now): (repo: string) => Promise<string[]> {
|
|
46
|
+
const locals: Locals = machineGet<Locals>(LOCALS) ?? {}
|
|
47
|
+
return async (repo) => {
|
|
48
|
+
const held = locals[repo]
|
|
49
|
+
return held && now() - held.at < LOCALS_MS ? held.files : localFiles(repo)
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
24
53
|
export interface BackupTick {
|
|
25
54
|
home: string
|
|
26
55
|
repos: string[]
|
|
@@ -28,10 +57,27 @@ export interface BackupTick {
|
|
|
28
57
|
print: (l: string) => void
|
|
29
58
|
printErr: (l: string) => void
|
|
30
59
|
now?: () => number
|
|
60
|
+
/** each repo's `backup` key; by default its config */
|
|
61
|
+
backupOf?: (repo: string) => Promise<RepoBackup | null>
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** the daemon's read before it starts a look (no walk, no child): something heard, a batch a look did not take, the
|
|
65
|
+
* safety walk due, a run the floor held back, or the 6 h run */
|
|
66
|
+
export function backupDue(now: number, heard: boolean): boolean {
|
|
67
|
+
if (heard) return true
|
|
68
|
+
const s = machineGet<Stamps>(STAMPS)
|
|
69
|
+
if (s == null || now - (s.fullAt ?? 0) >= SAFETY_MS) return true
|
|
70
|
+
if (machineGet<Batch>(BATCH)?.done === false) return true
|
|
71
|
+
const last = lastRun()
|
|
72
|
+
const since = last ? now - Date.parse(last.at) : Infinity
|
|
73
|
+
if (since >= EVERY_MS) return true
|
|
74
|
+
// with no Space a run never lands: only a change or a clock brings the next look
|
|
75
|
+
return s.seen !== s.ran && since >= FLOOR_MS && !machineGet<string>(NO_SPACE)
|
|
31
76
|
}
|
|
32
77
|
|
|
33
|
-
/** one look of the daemon's backup job, in its child process:
|
|
34
|
-
* remembers lives in the machine db, so a daemon restart
|
|
78
|
+
/** one look of the daemon's backup job, in its child process: the moved paths read again (or every source, on the
|
|
79
|
+
* safety clock), then a run when it is due. What a look remembers lives in the machine db, so a daemon restart
|
|
80
|
+
* never makes a run of its own */
|
|
35
81
|
export async function backupTick(o: BackupTick): Promise<void> {
|
|
36
82
|
const now = o.now ?? Date.now
|
|
37
83
|
const locals: Locals = machineGet<Locals>(LOCALS) ?? {}
|
|
@@ -45,13 +91,50 @@ export async function backupTick(o: BackupTick): Promise<void> {
|
|
|
45
91
|
return files
|
|
46
92
|
}
|
|
47
93
|
try {
|
|
48
|
-
const
|
|
49
|
-
if (walked) machineSet(LOCALS, locals)
|
|
94
|
+
const started = now()
|
|
50
95
|
const held: Stamps = machineGet<Stamps>(STAMPS) ?? { seen: '', ran: '' }
|
|
96
|
+
const batch = machineGet<Batch>(BATCH)
|
|
97
|
+
const moved = batch && !batch.done ? batch.paths : []
|
|
98
|
+
const prev = machineGet<Source[]>(SOURCES)
|
|
51
99
|
const last = lastRun()
|
|
52
|
-
const since = last ?
|
|
53
|
-
const
|
|
54
|
-
|
|
100
|
+
const since = last ? started - Date.parse(last.at) : Infinity
|
|
101
|
+
const sixHours = since >= EVERY_MS
|
|
102
|
+
const all = batch?.done === false && batch.all
|
|
103
|
+
const full = prev == null || all || started - (held.fullAt ?? 0) >= SAFETY_MS || sixHours
|
|
104
|
+
// a heard `*.local.*` path is read again in the kept git walk, alone
|
|
105
|
+
for (const r of new Set(moved.map((p) => movesLocals(p, o.repos)).filter((r) => r != null))) {
|
|
106
|
+
const kept = locals[r]
|
|
107
|
+
if (kept == null) continue
|
|
108
|
+
locals[r] = { at: kept.at, files: await patchLocals(r, kept.files, moved) }
|
|
109
|
+
walked = true
|
|
110
|
+
}
|
|
111
|
+
// each config read says its named paths: the daemon's watch hears them
|
|
112
|
+
const named = machineGet<Record<string, string[]>>(NAMED) ?? {}
|
|
113
|
+
const namedWas = JSON.stringify(named)
|
|
114
|
+
const backupOf = async (repo: string): Promise<RepoBackup | null> => {
|
|
115
|
+
const b = await (o.backupOf ?? backupOfRepo)(repo)
|
|
116
|
+
named[repo] = b?.paths ?? []
|
|
117
|
+
return b
|
|
118
|
+
}
|
|
119
|
+
const c: Collect = { home: o.home, repos: o.repos, locals: cachedLocals, backupOf }
|
|
120
|
+
// on a full walk, what the events told is read first: what the walk finds beyond it, no event told
|
|
121
|
+
const told = full && prev != null && !all ? await refreshSources(prev, moved, c) : null
|
|
122
|
+
const list = full ? await collect(c) : await refreshSources(prev, moved, c)
|
|
123
|
+
if (told != null) {
|
|
124
|
+
// said: a watch that went quiet is seen here, not after a lost file
|
|
125
|
+
const missed = missedBy(told, list)
|
|
126
|
+
if (missed.length > 0) o.printErr(`note: backup: the safety walk found ${missed.length} changes no file event told, first ${missed[0]}`)
|
|
127
|
+
}
|
|
128
|
+
if (walked) machineSet(LOCALS, locals)
|
|
129
|
+
if (JSON.stringify(named) !== namedWas) machineSet(NAMED, named)
|
|
130
|
+
machineSet(SOURCES, list)
|
|
131
|
+
if (batch && !batch.done) machineSet(BATCH, { ...batch, done: true } satisfies Batch)
|
|
132
|
+
const stamp = stampOfSources(list)
|
|
133
|
+
const waitAt = stamp === held.ran ? undefined : (held.waitAt ?? started)
|
|
134
|
+
const waited = waitAt != null && started - waitAt >= WAIT_MAX_MS
|
|
135
|
+
const due = sixHours || (stamp !== held.ran && (stamp === held.seen || waited) && since >= FLOOR_MS)
|
|
136
|
+
const fullAt = full ? started : held.fullAt
|
|
137
|
+
machineSet(STAMPS, { seen: stamp, ran: held.ran, ...(fullAt != null ? { fullAt } : {}), ...(waitAt != null ? { waitAt } : {}) } satisfies Stamps)
|
|
55
138
|
if (!due) return
|
|
56
139
|
const { space, why } = await o.space()
|
|
57
140
|
// said once per reason: a look is a child of its own
|
|
@@ -61,10 +144,10 @@ export async function backupTick(o: BackupTick): Promise<void> {
|
|
|
61
144
|
return
|
|
62
145
|
}
|
|
63
146
|
machineSet(NO_SPACE, '')
|
|
64
|
-
const r = await backupRun(space, {
|
|
65
|
-
if (r.failed.length === 0) machineSet(STAMPS, { seen: stamp, ran: stamp } satisfies Stamps)
|
|
147
|
+
const r = await backupRun(space, { ...c, sources: list, rehash: sixHours, now: new Date(now()) })
|
|
148
|
+
if (r.failed.length === 0) machineSet(STAMPS, { seen: stamp, ran: stamp, ...(fullAt != null ? { fullAt } : {}) } satisfies Stamps)
|
|
66
149
|
if (r.failed.length > 0) o.printErr(`WARN backup: ${r.failed.length} objects did not go up, no manifest written; first: ${r.failed[0]?.key}: ${r.failed[0]?.error}`)
|
|
67
|
-
else if (r.manifest) o.print(`ok: backup: ${r.files} files, ${r.sent} objects up (${(r.sentBytes / 1e6).toFixed(1)} MB), ${r.refused.length} refused, manifest ${r.manifest}`)
|
|
150
|
+
else if (r.manifest) o.print(`ok: backup: ${r.files} files, ${r.sent} objects up (${(r.sentBytes / 1e6).toFixed(1)} MB), ${r.read} read again, ${r.refused.length} refused, manifest ${r.manifest}`)
|
|
68
151
|
} catch (e) {
|
|
69
152
|
// a hand run holds the lock: the next look tries again, and says so (a lock that never goes stops every backup)
|
|
70
153
|
const msg = (e as Error).message
|
|
@@ -21,7 +21,8 @@ import { collect, type Collect, type Source } from './sources.ts'
|
|
|
21
21
|
import { machineName } from '../../home/machine-host.ts'
|
|
22
22
|
|
|
23
23
|
const LAST = 'backup-last'
|
|
24
|
-
/**
|
|
24
|
+
/** every file a run held, by path: its size, time, sha and key, so a file whose size and time held is never read
|
|
25
|
+
* again (the 6 h run reads every byte again) */
|
|
25
26
|
const SEEN = 'backup-seen'
|
|
26
27
|
|
|
27
28
|
export interface Last {
|
|
@@ -44,11 +45,14 @@ export interface Ready {
|
|
|
44
45
|
file?: string
|
|
45
46
|
/** the object's size: a sqlite's is its gzip */
|
|
46
47
|
size: number
|
|
47
|
-
/** a path the repo names whole:
|
|
48
|
+
/** a path the repo names whole: no size bound, read as a stream */
|
|
48
49
|
whole?: true
|
|
50
|
+
/** read from the disk by this run, not taken from the last run's sha */
|
|
51
|
+
read?: true
|
|
49
52
|
}
|
|
50
53
|
|
|
51
|
-
/** every source read: hashed, snapshotted, or refused
|
|
54
|
+
/** every source read: hashed, snapshotted, or refused. A file whose size and time equal the last run's, its object
|
|
55
|
+
* up, keeps that run's sha unread (`seen`; empty reads every byte) */
|
|
52
56
|
export async function prepare(sources: readonly Source[], scratch: string, seen: Seen = machineGet<Seen>(SEEN) ?? {}): Promise<{ ready: Ready[]; refused: Refused[] }> {
|
|
53
57
|
const ready: Ready[] = []
|
|
54
58
|
const refused: Refused[] = []
|
|
@@ -71,15 +75,15 @@ export async function prepare(sources: readonly Source[], scratch: string, seen:
|
|
|
71
75
|
const gz = new Uint8Array(gzipSync(raw))
|
|
72
76
|
// a snapshot is a new file each run: its live file's time would move the part with no change
|
|
73
77
|
const entry: Entry = { path: s.rel, sha: sha256(raw), size: raw.length, mtime: 0, key: `${BACKUP_LABEL}/${sha256(gz)}.gz`, sqlite: { ...s.sqlite, rows: snap.rows } }
|
|
74
|
-
ready.push({ entry, bytes: gz, size: gz.length })
|
|
78
|
+
ready.push({ entry, bytes: gz, size: gz.length, read: true })
|
|
79
|
+
continue
|
|
80
|
+
}
|
|
81
|
+
const was = seen[s.rel]
|
|
82
|
+
if (was && was.size === s.size && was.mtime === s.mtime && isUp(was.key)) {
|
|
83
|
+
ready.push({ entry: { path: s.rel, sha: was.sha, size: was.size, mtime: s.mtime, key: was.key }, size: was.size, ...(s.whole ? { whole: true as const } : {}) })
|
|
75
84
|
continue
|
|
76
85
|
}
|
|
77
86
|
if (s.whole) {
|
|
78
|
-
const was = seen[s.rel]
|
|
79
|
-
if (was && was.size === s.size && was.mtime === s.mtime && isUp(was.key)) {
|
|
80
|
-
ready.push({ entry: { path: s.rel, sha: was.sha, size: was.size, mtime: s.mtime, key: was.key }, size: was.size, whole: true })
|
|
81
|
-
continue
|
|
82
|
-
}
|
|
83
87
|
const copy = join(scratch, `${ready.length}-whole${extname(s.abs)}`)
|
|
84
88
|
copyFileSync(s.abs, copy, constants.COPYFILE_FICLONE)
|
|
85
89
|
const r = await fileRefusal(copy, basename(s.abs))
|
|
@@ -88,7 +92,7 @@ export async function prepare(sources: readonly Source[], scratch: string, seen:
|
|
|
88
92
|
refused.push({ path: s.rel, why: r.why })
|
|
89
93
|
continue
|
|
90
94
|
}
|
|
91
|
-
ready.push({ entry: { path: s.rel, sha: r.sha, size: r.size, mtime: s.mtime, key: mediaKey(BACKUP_LABEL, { sha: r.sha }, s.abs) }, file: copy, size: r.size, whole: true })
|
|
95
|
+
ready.push({ entry: { path: s.rel, sha: r.sha, size: r.size, mtime: s.mtime, key: mediaKey(BACKUP_LABEL, { sha: r.sha }, s.abs) }, file: copy, size: r.size, whole: true, read: true })
|
|
92
96
|
continue
|
|
93
97
|
}
|
|
94
98
|
const bytes = new Uint8Array(await Bun.file(s.abs).arrayBuffer())
|
|
@@ -98,7 +102,7 @@ export async function prepare(sources: readonly Source[], scratch: string, seen:
|
|
|
98
102
|
continue
|
|
99
103
|
}
|
|
100
104
|
const sha = sha256(bytes)
|
|
101
|
-
ready.push({ entry: { path: s.rel, sha, size: bytes.length, mtime: s.mtime, key: mediaKey(BACKUP_LABEL, { sha }, s.abs) }, bytes, size: bytes.length })
|
|
105
|
+
ready.push({ entry: { path: s.rel, sha, size: bytes.length, mtime: s.mtime, key: mediaKey(BACKUP_LABEL, { sha }, s.abs) }, bytes, size: bytes.length, read: true })
|
|
102
106
|
} catch (e) {
|
|
103
107
|
refused.push({ path: s.rel, why: `not read: ${(e as Error).message}` })
|
|
104
108
|
}
|
|
@@ -117,6 +121,8 @@ export interface RunResult {
|
|
|
117
121
|
there: number
|
|
118
122
|
refused: Refused[]
|
|
119
123
|
failed: { key: string; error: string }[]
|
|
124
|
+
/** files read from the disk; the others kept the last run's sha */
|
|
125
|
+
read: number
|
|
120
126
|
/** the run written, null when nothing changed since `last` or an upload failed */
|
|
121
127
|
manifest: string | null
|
|
122
128
|
/** what the record of the run cost: its parts sent (of all), their bytes, and the run's own bytes */
|
|
@@ -208,20 +214,20 @@ function lock(): () => void {
|
|
|
208
214
|
}
|
|
209
215
|
|
|
210
216
|
/** one run: every source to the Space, then the run's parts the Space lacks, then the run itself, last */
|
|
211
|
-
export async function backupRun(space: Space, o: Collect & { now?: Date; jobs?: number }): Promise<RunResult> {
|
|
217
|
+
export async function backupRun(space: Space, o: Collect & { now?: Date; jobs?: number; sources?: readonly Source[]; rehash?: boolean }): Promise<RunResult> {
|
|
212
218
|
const unlock = lock()
|
|
213
219
|
const scratch = mkdtempSync(join(tmpdir(), 'shipkit-backup-'))
|
|
214
220
|
try {
|
|
215
221
|
const now = o.now ?? new Date()
|
|
216
|
-
const { ready, refused } = await prepare(await collect(o), scratch)
|
|
222
|
+
const { ready, refused } = await prepare(o.sources ?? (await collect(o)), scratch, o.rehash ? {} : undefined)
|
|
217
223
|
const files = ready.map((r) => r.entry)
|
|
218
224
|
const digest = digestOf(files, refused)
|
|
219
225
|
const last = await lastOf(space, scratch)
|
|
220
|
-
const out: RunResult = { files: files.length, bytes: ready.reduce((n, r) => n + r.entry.size, 0), sent: 0, sentBytes: 0, there: 0, refused, failed: [], manifest: null, record: { parts: 0, partsSent: 0, partsBytes: 0, runBytes: 0 }, last, digest }
|
|
226
|
+
const out: RunResult = { files: files.length, bytes: ready.reduce((n, r) => n + r.entry.size, 0), sent: 0, sentBytes: 0, there: 0, refused, failed: [], read: ready.filter((r) => r.read).length, manifest: null, record: { parts: 0, partsSent: 0, partsBytes: 0, runBytes: 0 }, last, digest }
|
|
221
227
|
await sendAll(space, ready, scratch, o.jobs ?? 8, out)
|
|
222
228
|
if (out.failed.length > 0) return out
|
|
223
229
|
const seen: Seen = {}
|
|
224
|
-
for (const r of ready) if (r.
|
|
230
|
+
for (const r of ready) if (!r.entry.sqlite) seen[r.entry.path] = { size: r.entry.size, mtime: r.entry.mtime, sha: r.entry.sha, key: r.entry.key }
|
|
225
231
|
machineSet(SEEN, seen)
|
|
226
232
|
if (last?.digest === digest) return out
|
|
227
233
|
const enc = encodeRun({ at: now.toISOString(), host: hostname(), home: o.home, digest, files, refused }, o.repos.map((r) => relative(o.home, r)))
|