rvlib-shipkit 2.27.0 → 2.28.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 +16 -0
- package/package.json +1 -1
- package/src/agent-time/hooks.ts +10 -3
- package/src/agent-time/live.ts +11 -5
- package/src/agent-time/registry.ts +289 -0
- package/src/agent-time/store.ts +2 -2
- package/src/annex/born.ts +2 -2
- package/src/annex/offload.ts +12 -11
- package/src/asks/ask-after.ts +6 -2
- package/src/asks/ask-bar-fold.ts +57 -0
- package/src/asks/ask-migrate.ts +280 -0
- package/src/asks/ask-steps-fold.ts +166 -0
- package/src/asks/ask-store.ts +586 -0
- package/src/assets/kits/identity-kit.ts +106 -38
- package/src/assets/kits/poster-thumbnail.ts +282 -0
- package/src/checks/SK003-claude-md-shape.check.ts +92 -25
- package/src/checks/SK004-gitignore-policy.check.ts +8 -4
- package/src/checks/SK013-one-way-in-policy.check.ts +10 -0
- package/src/checks/SK026-asks-autoclose.check.ts +9 -23
- package/src/checks/SK038-speaks-as.check.ts +3 -3
- package/src/checks/SK042-post-kinds.check.ts +26 -24
- package/src/checks/SK049-thread-answers-hook.check.ts +81 -0
- package/src/checks/SK050-retired-commands.check.ts +127 -0
- package/src/cli/cmd_Asks.ts +204 -53
- package/src/cli/cmd_Backup.ts +151 -8
- package/src/cli/cmd_Calls.ts +1 -0
- package/src/cli/cmd_Commands.ts +140 -34
- package/src/cli/cmd_Contracts.ts +1 -0
- package/src/cli/cmd_Keys.ts +1 -0
- package/src/cli/cmd_Lanes.ts +15 -4
- package/src/cli/cmd_Messages.ts +152 -1
- package/src/cli/cmd_Needs.ts +1 -0
- package/src/cli/cmd_Review.ts +931 -229
- package/src/cli/cmd_Secret.ts +255 -34
- package/src/cli/cmd_Social.ts +36 -33
- package/src/cli/cmd_SocialDrafts.ts +235 -0
- package/src/cli/cmd_Sound.ts +22 -1
- package/src/cli/cmd_Start.ts +3 -2
- package/src/cli/cmd_ThreadHolds.ts +34 -0
- package/src/cli/cmd_ThreadKinds.ts +146 -0
- package/src/cli/cmd_ThreadReflect.ts +83 -0
- package/src/cli/cmd_ThreadSyncNeeds.ts +62 -0
- package/src/cli/cmd_Vault.ts +1 -1
- package/src/cli/cmd_When.ts +1 -0
- package/src/cli/family-help.ts +2 -2
- package/src/cli/family-pages.ts +1 -1
- package/src/cli/kit/builtins.ts +1 -0
- package/src/cli/kit/verb.ts +6 -2
- package/src/cli/registry.ts +21 -2
- package/src/cli.ts +5 -2
- package/src/collections/actions.ts +25 -74
- package/src/collections/collection-types.ts +9 -5
- package/src/collections/md-body.ts +5 -3
- package/src/collections/pings.ts +13 -6
- package/src/collections/providers.ts +53 -61
- package/src/collections/reflections.ts +9 -4
- package/src/core/discover-checks.ts +18 -3
- package/src/core/git.ts +31 -0
- package/src/core/house-layout.ts +17 -0
- package/src/core/layout.ts +10 -5
- package/src/core/locked-write.ts +4 -1
- package/src/core/machine-db.ts +5 -3
- package/src/core/migrate-layout.ts +8 -10
- package/src/home/activity.ts +8 -15
- package/src/home/daemon-jobs.ts +13 -1
- package/src/home/home-paths.ts +6 -3
- package/src/home/home-routes.ts +8 -0
- package/src/home/inbox-shape.ts +233 -100
- package/src/home/inbox.ts +26 -71
- package/src/home/machine-fold.ts +10 -15
- package/src/home/machine-host.ts +19 -1
- package/src/home/other-macs.ts +2 -2
- package/src/home/serve-web.ts +6 -5
- package/src/home/space-page.ts +1 -1
- package/src/home/vault-page.ts +51 -17
- package/src/home/vault-words.ts +2 -2
- package/src/hooks/one-way-in-guard.ts +24 -2
- package/src/hq/files-send.ts +27 -28
- package/src/hq/hq-db.ts +4 -1
- package/src/hq/hq-local.ts +37 -4
- package/src/hq/hq-routes.ts +10 -2
- package/src/hq/hq-store.ts +6 -2
- package/src/hq/kinds/ask.ts +6 -6
- package/src/hq/outbox.ts +5 -3
- package/src/hq/replica.ts +1 -2
- package/src/hq/vault-route.ts +112 -0
- package/src/identities/collection.ts +2 -2
- package/src/identities/identities.ts +9 -8
- package/src/identities/machine-route.ts +8 -3
- package/src/ledger/db.ts +8 -1
- package/src/media-store/backup/daemon.ts +48 -8
- package/src/media-store/backup/keys.ts +1 -1
- package/src/media-store/backup/manifest.ts +20 -2
- package/src/media-store/backup/prune.ts +365 -0
- package/src/media-store/backup/retention.ts +122 -0
- package/src/media-store/backup/run.ts +14 -8
- package/src/media-store/backup/sources.ts +18 -17
- package/src/media-store/backup/thread-copy.ts +340 -0
- package/src/media-store/copy/config.ts +3 -4
- package/src/media-store/copy/store.ts +2 -2
- package/src/media-store/db.ts +2 -2
- package/src/media-store/forget.ts +118 -0
- package/src/media-store/purge.ts +3 -2
- package/src/media-store/space-listing.ts +20 -4
- package/src/media-store/space.ts +6 -5
- package/src/messages/guard.ts +49 -0
- package/src/messages/model.ts +1 -1
- package/src/messages/objects.ts +201 -0
- package/src/messages/routes.ts +2 -2
- package/src/messages/score.ts +1 -1
- package/src/messages/write.ts +285 -0
- package/src/pages/frame-client.ts +181 -4
- package/src/reflections/reflections-queue.ts +1 -1
- package/src/release/gate-checkout.ts +2 -1
- package/src/release/git-facts.ts +3 -2
- package/src/release/public-clean.ts +3 -2
- package/src/repo-config.ts +40 -18
- package/src/review/anim.ts +1 -1
- package/src/review/answer-kinds.ts +131 -0
- package/src/review/band.ts +53 -0
- package/src/review/branch-hold.ts +84 -0
- package/src/review/card-db.ts +16 -6
- package/src/review/card-rows.ts +4 -3
- package/src/review/cells.ts +297 -65
- package/src/review/close.ts +92 -0
- package/src/review/draft-card.ts +8 -16
- package/src/review/draft-model.ts +2 -2
- package/src/review/events.ts +171 -0
- package/src/review/format.ts +128 -14
- package/src/review/glyphs.ts +63 -0
- package/src/review/gone.ts +1 -1
- package/src/review/holds-sync.ts +62 -0
- package/src/review/holds.ts +85 -0
- package/src/review/house-kind-defs.ts +41 -0
- package/src/review/inbox-hook.ts +22 -9
- package/src/review/inbox-read.ts +201 -0
- package/src/review/instructions.ts +199 -0
- package/src/review/kind-apply.ts +61 -0
- package/src/review/kind-catalog.ts +96 -0
- package/src/review/kinds.ts +278 -0
- package/src/review/lane-ask.ts +109 -0
- package/src/review/model.ts +396 -86
- package/src/review/move.ts +3 -3
- package/src/review/need-address.ts +15 -0
- package/src/review/need-threads.ts +93 -0
- package/src/review/object-card.ts +48 -0
- package/src/review/object-read.ts +31 -0
- package/src/review/owner-verdict.ts +36 -0
- package/src/review/parties.ts +182 -0
- package/src/review/previews.ts +3 -1
- package/src/review/projections.ts +235 -0
- package/src/review/referring.ts +54 -0
- package/src/review/reflection-object.ts +195 -0
- package/src/review/routes.ts +137 -32
- package/src/review/secret-route.ts +80 -0
- package/src/review/snooze.ts +38 -0
- package/src/review/stage-card.ts +4 -4
- package/src/review/store.ts +366 -85
- package/src/review/supersede.ts +116 -0
- package/src/review/take-back.ts +52 -0
- package/src/review/thread-fold.ts +20 -7
- package/src/review/threads.ts +236 -39
- package/src/review/traces.ts +218 -0
- package/src/review/trailer-card.ts +2 -2
- package/src/review/unit.ts +71 -5
- package/src/review/verdict-items.ts +73 -0
- package/src/review/verdict-write.ts +15 -0
- package/src/review/wait.ts +85 -0
- package/src/review/watchers.ts +22 -9
- package/src/scaffold/files/claude-settings-local.json +2 -0
- package/src/secrets/archive-mark.ts +52 -0
- package/src/secrets/archive.ts +83 -0
- package/src/secrets/copy.ts +14 -14
- package/src/secrets/keychain-write.ts +48 -0
- package/src/secrets/keychain.ts +8 -5
- package/src/secrets/lapse-mark.ts +33 -0
- package/src/secrets/lapse.ts +60 -0
- package/src/secrets/list.ts +16 -22
- package/src/secrets/manager.ts +31 -16
- package/src/secrets/read.ts +161 -33
- package/src/secrets/tail.ts +31 -0
- package/src/serve/hub-app/asks-page.tsx +2 -1
- package/src/serve/hub-app/auto-text.tsx +14 -6
- package/src/serve/hub-app/benign-errors.ts +15 -1
- package/src/serve/hub-app/card-head.tsx +49 -0
- package/src/serve/hub-app/check-view.tsx +3 -2
- package/src/serve/hub-app/code-copy.ts +35 -0
- package/src/serve/hub-app/col-card.tsx +31 -40
- package/src/serve/hub-app/col-list-page.tsx +5 -2
- package/src/serve/hub-app/col-tables-view.tsx +7 -4
- package/src/serve/hub-app/col-tables.ts +5 -5
- package/src/serve/hub-app/cols-svc.ts +4 -79
- package/src/serve/hub-app/commands/commands-page.tsx +2 -1
- package/src/serve/hub-app/controls.css +83 -0
- package/src/serve/hub-app/frames-st.ts +17 -1
- package/src/serve/hub-app/git-view.tsx +3 -2
- package/src/serve/hub-app/home-st.ts +57 -6
- package/src/serve/hub-app/home-strip.css +1 -6
- package/src/serve/hub-app/home-strip.tsx +2 -1
- package/src/serve/hub-app/home.html +3 -0
- package/src/serve/hub-app/house-wiki-view.tsx +20 -0
- package/src/serve/hub-app/hub-app.tsx +48 -32
- package/src/serve/hub-app/hub-types.ts +2 -0
- package/src/serve/hub-app/hub.css +50 -61
- package/src/serve/hub-app/icons.tsx +14 -4
- package/src/serve/hub-app/identities-view.tsx +4 -3
- package/src/serve/hub-app/inbox-st.ts +203 -42
- package/src/serve/hub-app/inbox-view.tsx +189 -101
- package/src/serve/hub-app/inbox.css +47 -44
- package/src/serve/hub-app/index.html +3 -0
- package/src/serve/hub-app/instructions.css +30 -0
- package/src/serve/hub-app/keys.ts +5 -3
- package/src/serve/hub-app/machine-st.ts +2 -2
- package/src/serve/hub-app/machines-view.tsx +4 -11
- package/src/serve/hub-app/machines.css +0 -9
- package/src/serve/hub-app/markdown.css +51 -0
- package/src/serve/hub-app/md-copy.tsx +18 -40
- package/src/serve/hub-app/md.ts +25 -9
- package/src/serve/hub-app/media-view.tsx +2 -2
- package/src/serve/hub-app/messages/messages-page.tsx +2 -1
- package/src/serve/hub-app/messages/messages-st.ts +1 -1
- package/src/serve/hub-app/messages.css +0 -1
- package/src/serve/hub-app/objects.css +51 -0
- package/src/serve/hub-app/page-keys.ts +1 -1
- package/src/serve/hub-app/pages-frames.tsx +11 -1
- package/src/serve/hub-app/palette-st.ts +4 -4
- package/src/serve/hub-app/pane-boundary.tsx +72 -0
- package/src/serve/hub-app/pane-errors.ts +54 -0
- package/src/serve/hub-app/plan-view.tsx +3 -2
- package/src/serve/hub-app/planned-view.tsx +3 -3
- package/src/serve/hub-app/planned.css +0 -4
- package/src/serve/hub-app/processes-view.tsx +3 -2
- package/src/serve/hub-app/project-list.css +5 -13
- package/src/serve/hub-app/project-list.tsx +3 -6
- package/src/serve/hub-app/projects-svc.ts +6 -2
- package/src/serve/hub-app/readme.css +9 -15
- package/src/serve/hub-app/reflections-page.tsx +54 -1
- package/src/serve/hub-app/releases/releases-page.tsx +3 -2
- package/src/serve/hub-app/releases.css +0 -1
- package/src/serve/hub-app/review/code-colors.ts +30 -0
- package/src/serve/hub-app/review/diff-view.tsx +75 -0
- package/src/serve/hub-app/review/dock.tsx +104 -0
- package/src/serve/hub-app/review/draft-card.tsx +2 -2
- package/src/serve/hub-app/review/group-block.tsx +209 -0
- package/src/serve/hub-app/review/instructions-view.tsx +116 -0
- package/src/serve/hub-app/review/item-verdict.tsx +38 -0
- package/src/serve/hub-app/review/kind-states.tsx +193 -0
- package/src/serve/hub-app/review/kind-view.tsx +1 -37
- package/src/serve/hub-app/review/kit.tsx +57 -54
- package/src/serve/hub-app/review/lane-bar.tsx +64 -0
- package/src/serve/hub-app/review/object-card.tsx +62 -0
- package/src/serve/hub-app/review/object-panel.tsx +179 -0
- package/src/serve/hub-app/review/question-views.tsx +385 -0
- package/src/serve/hub-app/review/review-cells.tsx +86 -105
- package/src/serve/hub-app/review/review-list.tsx +104 -47
- package/src/serve/hub-app/review/review-page.tsx +5 -4
- package/src/serve/hub-app/review/review-st.ts +68 -28
- package/src/serve/hub-app/review/review-thread.tsx +171 -40
- package/src/serve/hub-app/review/review-views.tsx +35 -31
- package/src/serve/hub-app/review/thread-close.tsx +124 -0
- package/src/serve/hub-app/review/thread-control.tsx +4 -4
- package/src/serve/hub-app/review/thread-st.ts +222 -22
- package/src/serve/hub-app/review/trailer-card.tsx +1 -1
- package/src/serve/hub-app/review/trailer-timeline.tsx +2 -2
- package/src/serve/hub-app/review.css +216 -57
- package/src/serve/hub-app/section-head.tsx +51 -0
- package/src/serve/hub-app/social/calendar.tsx +5 -11
- package/src/serve/hub-app/social/comms-page.tsx +23 -10
- package/src/serve/hub-app/social/kit.tsx +8 -15
- package/src/serve/hub-app/social/next-release.tsx +3 -5
- package/src/serve/hub-app/social/post-card.tsx +3 -0
- package/src/serve/hub-app/social/post-page.tsx +80 -0
- package/src/serve/hub-app/social/problems.tsx +5 -9
- package/src/serve/hub-app/social/social-model.ts +13 -5
- package/src/serve/hub-app/social/social-page.tsx +1 -1
- package/src/serve/hub-app/social/social-st.ts +42 -5
- package/src/serve/hub-app/social.css +7 -17
- package/src/serve/hub-app/space.css +1 -1
- package/src/serve/hub-app/stores/stores-page.tsx +6 -2
- package/src/serve/hub-app/thread-row.tsx +101 -0
- package/src/serve/hub-app/time-ago.tsx +3 -2
- package/src/serve/hub-app/time-view.tsx +2 -1
- package/src/serve/hub-app/time.css +2 -5
- package/src/serve/hub-app/tree-row.tsx +1 -9
- package/src/serve/hub-app/vault-view.tsx +4 -4
- package/src/serve/hub-app/veille/veille-page.tsx +2 -11
- package/src/serve/hub-app/veille.css +0 -1
- package/src/serve/hub-app/waiting-svc.ts +4 -4
- package/src/serve/hub-app/when-view.tsx +6 -3
- package/src/serve/hub-app/wiki-st.ts +30 -4
- package/src/serve/hub-app/wikis-view.tsx +4 -3
- package/src/serve/hub-runtime.ts +20 -6
- package/src/serve/project-gutter-route.ts +16 -6
- package/src/serve/project-gutter.ts +6 -53
- package/src/serve/same-origin.ts +3 -0
- package/src/serve/secret-gate.ts +21 -0
- package/src/serve/serve-hub.ts +29 -20
- package/src/serve/tailnet-caller.ts +117 -0
- package/src/shipkit.ts +2 -2
- package/src/site/desk.ts +2 -2
- package/src/smoke/run-smoke.ts +3 -4
- package/src/social/brands.ts +4 -0
- package/src/social/card-drafts.ts +11 -19
- package/src/social/desk-index.ts +34 -13
- package/src/social/drafts.ts +4 -4
- package/src/social/import-drafts.ts +244 -0
- package/src/social/kind-pages.ts +3 -3
- package/src/social/kinds.ts +1 -1
- package/src/social/load.ts +89 -64
- package/src/social/media-kind.ts +68 -161
- package/src/social/media-of.ts +5 -4
- package/src/social/open.ts +3 -2
- package/src/social/post-object.ts +120 -0
- package/src/social/post-sources.ts +136 -0
- package/src/social/post-text.ts +103 -0
- package/src/social/posts.ts +378 -0
- package/src/social/presence.ts +7 -5
- package/src/social/publish/index.ts +17 -5
- package/src/social/publish/secret.ts +2 -2
- package/src/social/release-stage.ts +58 -57
- package/src/social/routes.ts +64 -24
- package/src/social/stage.ts +42 -57
- package/src/social/text.ts +4 -3
- package/src/social/thumbnail.ts +135 -43
- package/src/social/tracker.ts +1 -1
- package/src/social/writes.ts +12 -29
- package/src/stores/desk.ts +16 -11
- package/src/stores/unit.ts +2 -2
- package/src/trailer/sound/eleven.ts +33 -3
- package/src/urls/rows.ts +1 -1
- package/src/vault/vault-http.ts +11 -3
- package/src/vault/vault-import.ts +6 -1
- package/src/veille/identity.ts +2 -2
- package/src/wiki/graph-pages.ts +13 -1
- package/src/wiki/scan-md.ts +1 -1
- package/src/asks/asks-queue.ts +0 -424
- package/src/messages/suggest.ts +0 -96
- package/src/social/migrate.ts +0 -123
|
@@ -132,13 +132,13 @@ export function encodeRun(m: Omit<Manifest, 'v'>, repos: readonly string[]): { r
|
|
|
132
132
|
export const encodeManifestV1 = (m: Omit<Manifest, 'v'>): Uint8Array => gz(JSON.stringify({ ...m, v: 1 }))
|
|
133
133
|
|
|
134
134
|
/** every run the Space holds, oldest first */
|
|
135
|
-
export type RunRef = { key: string; at: Date; size: number; machine: string | null }
|
|
135
|
+
export type RunRef = { key: string; at: Date; size: number; machine: string | null; modified: number | null }
|
|
136
136
|
|
|
137
137
|
export async function runsOf(space: Space): Promise<RunRef[]> {
|
|
138
138
|
return (await space.list(RUNS_PREFIX))
|
|
139
139
|
.flatMap((o) => {
|
|
140
140
|
const r = ofRunKey(o.key)
|
|
141
|
-
return r ? [{ key: o.key, at: r.at, size: o.size, machine: r.machine }] : []
|
|
141
|
+
return r ? [{ key: o.key, at: r.at, size: o.size, machine: r.machine, modified: o.modified ?? null }] : []
|
|
142
142
|
})
|
|
143
143
|
.sort((a, b) => a.at.getTime() - b.at.getTime() || (a.key < b.key ? -1 : 1))
|
|
144
144
|
}
|
|
@@ -170,6 +170,24 @@ async function hostOfOldRun(space: Space, key: string, tmp: string): Promise<str
|
|
|
170
170
|
return name
|
|
171
171
|
}
|
|
172
172
|
|
|
173
|
+
/** the machine of each run: its key's, else the host its record names (read 8 at a time, once a key); null unread */
|
|
174
|
+
export async function hostsOfRuns(space: Space, runs: readonly RunRef[]): Promise<Map<string, string | null>> {
|
|
175
|
+
const out = new Map<string, string | null>()
|
|
176
|
+
const old = runs.filter((r) => r.machine == null)
|
|
177
|
+
for (const r of runs) if (r.machine != null) out.set(r.key, r.machine)
|
|
178
|
+
const tmp = mkdtempSync(join(tmpdir(), 'shipkit-runs-'))
|
|
179
|
+
try {
|
|
180
|
+
let next = 0
|
|
181
|
+
const work = async (w: number): Promise<void> => {
|
|
182
|
+
for (let i = next++; i < old.length; i = next++) out.set(old[i]!.key, await hostOfOldRun(space, old[i]!.key, join(tmp, String(w))))
|
|
183
|
+
}
|
|
184
|
+
await Promise.all(Array.from({ length: 8 }, (_, w) => work(w)))
|
|
185
|
+
return out
|
|
186
|
+
} finally {
|
|
187
|
+
rmSync(tmp, { recursive: true, force: true })
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
173
191
|
/** the machines the runs name, those from before the name read for their host */
|
|
174
192
|
export async function machinesOf(space: Space): Promise<string[]> {
|
|
175
193
|
const tmp = mkdtempSync(join(tmpdir(), 'shipkit-runs-'))
|
|
@@ -0,0 +1,365 @@
|
|
|
1
|
+
// the backup's retention applied (wiki tools/shipkit/media-store-backup.md, Retention): this machine's runs that no
|
|
2
|
+
// slot keeps go, then the parts and the files' bytes that only they named. The constraint a reader breaks first: a run
|
|
3
|
+
// skips sending an object its machine's media index saw up, so an object leaves only when no kept run of any machine
|
|
4
|
+
// names it, no run of ANOTHER machine names it (that machine's index may still hold it), its bytes are older than
|
|
5
|
+
// GRACE_MS, and this machine's index row goes before the object does (forget.ts forgetBackupObjects). A machine prunes
|
|
6
|
+
// only its own runs. Order: the runs first, so no run is ever left naming an object the Space lacks. A delete leaves a
|
|
7
|
+
// delete marker, the versions recoverable TRASH_DAYS until emptyBackupTrash removes them
|
|
8
|
+
import { existsSync, mkdirSync, mkdtempSync, renameSync, rmSync, writeFileSync } from 'node:fs'
|
|
9
|
+
import { tmpdir } from 'node:os'
|
|
10
|
+
import { join } from 'node:path'
|
|
11
|
+
import { gunzipSync } from 'node:zlib'
|
|
12
|
+
import { machineGet, machineSet } from '../../core/machine-db.ts'
|
|
13
|
+
import { homeDir } from '../../home/home-paths.ts'
|
|
14
|
+
import { machineName } from '../../home/machine-host.ts'
|
|
15
|
+
import { checkBackupKeys, TRASH_DAYS } from '../forget.ts'
|
|
16
|
+
import { mediaDb } from '../db.ts'
|
|
17
|
+
import type { Space } from '../space.ts'
|
|
18
|
+
import { BACKUP_LABEL, RUNS_PREFIX, sameMachine } from './keys.ts'
|
|
19
|
+
import { hostsOfRuns, PARTS_PREFIX, runsOf, type RunRef } from './manifest.ts'
|
|
20
|
+
import { retain, retentionError, type BackupRetention } from './retention.ts'
|
|
21
|
+
|
|
22
|
+
/** an object PUT more recently than this never goes: a run may be sending it now and name it later. A run that finds
|
|
23
|
+
* an older object by HEAD moves no time, so this closes no race on it: the 6 h run's listing check heals that one */
|
|
24
|
+
export const GRACE_MS = 48 * 3_600_000
|
|
25
|
+
|
|
26
|
+
/** the house config's `serve.backupRetention`, null with the reason when it names none (then nothing is deleted) */
|
|
27
|
+
export async function houseRetention(cwd = process.cwd()): Promise<{ policy: BackupRetention | null; why: string }> {
|
|
28
|
+
const { houseRootOf } = await import('../../home/home-start.ts')
|
|
29
|
+
const { loadRepoConfig } = await import('../../repo-config.ts')
|
|
30
|
+
const house = houseRootOf(cwd)
|
|
31
|
+
if (!house) return { policy: null, why: `no house repo seen from ${cwd}: nothing is deleted` }
|
|
32
|
+
const p = (await loadRepoConfig(house)).serve?.backupRetention
|
|
33
|
+
if (p == null) return { policy: null, why: `the house config (${house}) names no serve.backupRetention: nothing is deleted` }
|
|
34
|
+
const why = retentionError(p)
|
|
35
|
+
return why ? { policy: null, why: `${why}: nothing is deleted` } : { policy: p, why: '' }
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface RunSlot {
|
|
39
|
+
run: RunRef
|
|
40
|
+
/** the machine its key or its record names, null when unread */
|
|
41
|
+
host: string | null
|
|
42
|
+
/** the slots that keep it; empty: it goes at the next prune */
|
|
43
|
+
slots: string[]
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** every run with the slots that keep it under `policy`, each machine's runs judged apart; a run whose machine is
|
|
47
|
+
* unread is kept */
|
|
48
|
+
export async function runSlots(space: Space, o: { policy: BackupRetention; now: number; pinned?: readonly string[]; pinLabel?: (key: string) => string | undefined }): Promise<RunSlot[]> {
|
|
49
|
+
const runs = await runsOf(space)
|
|
50
|
+
const hosts = await hostsOfRuns(space, runs)
|
|
51
|
+
const groups = new Map<string, RunRef[]>()
|
|
52
|
+
const out = new Map<string, RunSlot>()
|
|
53
|
+
for (const r of runs) {
|
|
54
|
+
const host = hosts.get(r.key) ?? null
|
|
55
|
+
if (host == null) {
|
|
56
|
+
out.set(r.key, { run: r, host, slots: ['its machine unread: never deleted'] })
|
|
57
|
+
continue
|
|
58
|
+
}
|
|
59
|
+
const g = [...groups.keys()].find((k) => sameMachine(k, host)) ?? host
|
|
60
|
+
groups.set(g, [...(groups.get(g) ?? []), r])
|
|
61
|
+
}
|
|
62
|
+
for (const [host, list] of groups) {
|
|
63
|
+
const r = retain(list, { at: (x) => x.at.getTime(), now: o.now, policy: o.policy, pinned: (x) => (o.pinned?.includes(x.key) ? (o.pinLabel?.(x.key) ?? true) : false) })
|
|
64
|
+
for (const k of r.keep) out.set(k.item.key, { run: k.item, host, slots: k.slots })
|
|
65
|
+
for (const d of r.drop) out.set(d.key, { run: d, host, slots: [] })
|
|
66
|
+
}
|
|
67
|
+
return runs.map((r) => out.get(r.key)!)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export interface PrunePlan {
|
|
71
|
+
machine: string
|
|
72
|
+
/** this machine's runs: kept with their slots, and the ones that go */
|
|
73
|
+
keep: RunSlot[]
|
|
74
|
+
drop: RunRef[]
|
|
75
|
+
/** dropped runs held to a later prune: an object only they name is younger than GRACE_MS */
|
|
76
|
+
deferred: RunRef[]
|
|
77
|
+
/** runs of other machines, or of a machine unread: never touched here */
|
|
78
|
+
others: number
|
|
79
|
+
/** the parts and files' bytes only the dropped runs named, with their sizes */
|
|
80
|
+
objects: { key: string; size: number }[]
|
|
81
|
+
/** keys the dropped runs named that stay in the Space for another machine's run, and no kept run of this one
|
|
82
|
+
* names: this machine's index rows of them go, so its next run checks them in the Space before it trusts one */
|
|
83
|
+
unindex: string[]
|
|
84
|
+
/** what the runs that go weigh, and the objects */
|
|
85
|
+
bytes: number
|
|
86
|
+
/** objects a dropped run names that the Space lacks: said, nothing to delete */
|
|
87
|
+
missing: number
|
|
88
|
+
/** every run of this machine, as listed */
|
|
89
|
+
mine: RunRef[]
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** the objects an apply decided and has not deleted yet (its runs went first): the next prune checks them again */
|
|
93
|
+
const PENDING = 'backup-prune-pending'
|
|
94
|
+
const pendingObjects = (): string[] => machineGet<string[]>(PENDING) ?? []
|
|
95
|
+
|
|
96
|
+
/** runs kept on purpose till a time (`sk backup pin`): before a migration, a run its lane may need back */
|
|
97
|
+
const PINS = 'backup-pins'
|
|
98
|
+
export type Pin = { key: string; until: number; why: string }
|
|
99
|
+
export const backupPins = (now = Date.now()): Pin[] => (machineGet<Pin[]>(PINS) ?? []).filter((p) => p.until > now)
|
|
100
|
+
export function pinRun(key: string, days: number, why: string): Pin {
|
|
101
|
+
if (!/^backup\/runs\//.test(key)) throw new Error(`${key}: no backup run`)
|
|
102
|
+
const pin = { key, until: Date.now() + days * 86_400_000, why }
|
|
103
|
+
machineSet(PINS, [...backupPins().filter((p) => p.key !== key), pin])
|
|
104
|
+
return pin
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
type Got = { bytes: Uint8Array }
|
|
108
|
+
const sha256 = (b: Uint8Array | string): string => new Bun.CryptoHasher('sha256').update(b).digest('hex')
|
|
109
|
+
|
|
110
|
+
/** every key a run names: its parts and each file's bytes (a run of version 1 holds its files itself). A part is
|
|
111
|
+
* checked by its sha, read once */
|
|
112
|
+
let fetched = 0
|
|
113
|
+
/** run records and parts never change once written: each is read from the Space once a machine, kept in the cache */
|
|
114
|
+
const recordsDir = (): string => join(homeDir(), 'cache', 'backup-records')
|
|
115
|
+
const cached = (key: string): string => join(recordsDir(), key.slice(BACKUP_LABEL.length + 1).replace('/', '-'))
|
|
116
|
+
|
|
117
|
+
async function keysOfRun(space: Space, key: string, dir: string, parts: Map<string, Promise<string[]>>): Promise<Set<string>> {
|
|
118
|
+
const get = async (k: string, name: string): Promise<Got> => {
|
|
119
|
+
const keep = cached(k)
|
|
120
|
+
if (existsSync(keep)) return { bytes: new Uint8Array(await Bun.file(keep).arrayBuffer()) }
|
|
121
|
+
const to = join(dir, `${fetched++}-${name}`)
|
|
122
|
+
await space.get(k, to)
|
|
123
|
+
const bytes = new Uint8Array(await Bun.file(to).arrayBuffer())
|
|
124
|
+
rmSync(to, { force: true })
|
|
125
|
+
mkdirSync(recordsDir(), { recursive: true })
|
|
126
|
+
writeFileSync(`${keep}.${process.pid}.tmp`, bytes)
|
|
127
|
+
renameSync(`${keep}.${process.pid}.tmp`, keep)
|
|
128
|
+
return { bytes }
|
|
129
|
+
}
|
|
130
|
+
const json = <T>(g: Got): { text: string; value: T } => {
|
|
131
|
+
const text = gunzipSync(g.bytes).toString('utf8')
|
|
132
|
+
return { text, value: JSON.parse(text) as T }
|
|
133
|
+
}
|
|
134
|
+
const run = json<{ v: number; files?: { key: string }[]; parts?: { key: string; files: number }[] }>(await get(key, 'run')).value
|
|
135
|
+
if (run.v === 1) {
|
|
136
|
+
if (!Array.isArray(run.files)) throw new Error(`${key}: no backup run of version 1`)
|
|
137
|
+
return new Set(run.files.map((f) => f.key))
|
|
138
|
+
}
|
|
139
|
+
if (run.v !== 2 || !Array.isArray(run.parts)) throw new Error(`${key}: no backup run of version 1 or 2`)
|
|
140
|
+
const out = new Set<string>()
|
|
141
|
+
for (const p of run.parts) {
|
|
142
|
+
out.add(p.key)
|
|
143
|
+
let files = parts.get(p.key)
|
|
144
|
+
if (!files) {
|
|
145
|
+
files = get(p.key, 'part').then((g) => {
|
|
146
|
+
const { text, value } = json<{ v: number; files: { key: string }[] }>(g)
|
|
147
|
+
if (`${PARTS_PREFIX}${sha256(text)}.json.gz` !== p.key) throw new Error(`${p.key}: the part does not hash to its key`)
|
|
148
|
+
if (value.v !== 2 || !Array.isArray(value.files) || value.files.length !== p.files) throw new Error(`${p.key}: no backup part of ${p.files} files`)
|
|
149
|
+
return value.files.map((f) => f.key)
|
|
150
|
+
})
|
|
151
|
+
parts.set(p.key, files)
|
|
152
|
+
}
|
|
153
|
+
for (const f of await files) out.add(f)
|
|
154
|
+
}
|
|
155
|
+
return out
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** `fn` over `items`, `jobs` at a time, results in order */
|
|
159
|
+
async function pool<T, R>(items: readonly T[], jobs: number, fn: (t: T, i: number) => Promise<R>): Promise<R[]> {
|
|
160
|
+
const out: R[] = []
|
|
161
|
+
let next = 0
|
|
162
|
+
const work = async (): Promise<void> => {
|
|
163
|
+
for (let i = next++; i < items.length; i = next++) out[i] = await fn(items[i]!, i)
|
|
164
|
+
}
|
|
165
|
+
await Promise.all(Array.from({ length: Math.max(1, jobs) }, work))
|
|
166
|
+
return out
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** what a prune of this machine's runs would delete now. Reads every run's record it needs; a run that does not read
|
|
170
|
+
* stops the plan (an object it names could be deleted from under it) */
|
|
171
|
+
export async function planPrune(space: Space, o: { policy: BackupRetention; now?: number; machine?: string; pinned?: readonly string[]; pinLabel?: (key: string) => string | undefined; jobs?: number; last?: string | null }): Promise<PrunePlan> {
|
|
172
|
+
const now = o.now ?? Date.now()
|
|
173
|
+
const machine = o.machine ?? machineName()
|
|
174
|
+
const slots = await runSlots(space, { policy: o.policy, now, ...(o.pinned ? { pinned: o.pinned } : {}), ...(o.pinLabel ? { pinLabel: o.pinLabel } : {}) })
|
|
175
|
+
const mine = slots.filter((s) => s.host != null && sameMachine(s.host, machine))
|
|
176
|
+
const keep = mine.filter((s) => s.slots.length > 0)
|
|
177
|
+
const drop = mine.filter((s) => s.slots.length === 0).map((s) => s.run)
|
|
178
|
+
const others = slots.length - mine.length
|
|
179
|
+
const plan: PrunePlan = { machine, keep, drop, deferred: [], others, objects: [], unindex: [], bytes: 0, missing: 0, mine: mine.map((s) => s.run) }
|
|
180
|
+
if (o.last != null && !slots.some((s) => s.run.key === o.last)) throw new Error(`this machine's last run ${o.last} is not in the listing: a listing that lags, nothing is deleted`)
|
|
181
|
+
const pending = pendingObjects()
|
|
182
|
+
if (drop.length === 0 && pending.length === 0) return plan
|
|
183
|
+
const dropKeys = new Set(drop.map((d) => d.key))
|
|
184
|
+
const dir = mkdtempSync(join(tmpdir(), 'shipkit-prune-'))
|
|
185
|
+
try {
|
|
186
|
+
const parts = new Map<string, Promise<string[]>>()
|
|
187
|
+
const named = await pool(slots, o.jobs ?? 8, (s) => keysOfRun(space, s.run.key, dir, parts))
|
|
188
|
+
// every key a run that stays names, and every key a run of another machine (or of an unread one) ever named
|
|
189
|
+
const held = new Set<string>()
|
|
190
|
+
const heldHere = new Set<string>()
|
|
191
|
+
const byDropped = new Map<string, Set<string>>()
|
|
192
|
+
for (const [i, s] of slots.entries()) {
|
|
193
|
+
if (dropKeys.has(s.run.key)) byDropped.set(s.run.key, named[i]!)
|
|
194
|
+
else {
|
|
195
|
+
for (const k of named[i]!) held.add(k)
|
|
196
|
+
if (s.host != null && sameMachine(s.host, machine)) for (const k of named[i]!) heldHere.add(k)
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
const listed = new Map((await space.list(`${BACKUP_LABEL}/`)).filter((x) => !x.key.startsWith(RUNS_PREFIX)).map((x) => [x.key, x]))
|
|
200
|
+
const young = (k: string): boolean => {
|
|
201
|
+
const m = listed.get(k)?.modified
|
|
202
|
+
return m == null || now - m < GRACE_MS
|
|
203
|
+
}
|
|
204
|
+
// a dropped run that alone names a young object waits for a later prune, and keeps what it names till then
|
|
205
|
+
for (const d of drop) {
|
|
206
|
+
const ks = byDropped.get(d.key)!
|
|
207
|
+
if ([...ks].some((k) => !held.has(k) && listed.has(k) && young(k))) {
|
|
208
|
+
plan.deferred.push(d)
|
|
209
|
+
for (const k of ks) {
|
|
210
|
+
held.add(k)
|
|
211
|
+
heldHere.add(k)
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
plan.drop = drop.filter((d) => !plan.deferred.includes(d))
|
|
216
|
+
const objects = new Map<string, number>()
|
|
217
|
+
const missing = new Set<string>()
|
|
218
|
+
const unindex = new Set<string>()
|
|
219
|
+
for (const d of plan.drop)
|
|
220
|
+
for (const k of byDropped.get(d.key)!) {
|
|
221
|
+
if (held.has(k)) {
|
|
222
|
+
if (!heldHere.has(k)) unindex.add(k)
|
|
223
|
+
continue
|
|
224
|
+
}
|
|
225
|
+
const l = listed.get(k)
|
|
226
|
+
if (l) objects.set(k, l.size)
|
|
227
|
+
else missing.add(k)
|
|
228
|
+
}
|
|
229
|
+
// what an earlier apply left: its runs are gone, so only this list still reaches it; a key the Space no longer
|
|
230
|
+
// lists is done and leaves the list
|
|
231
|
+
if (pending.some((k) => !listed.has(k))) machineSet(PENDING, pending.filter((k) => listed.has(k)))
|
|
232
|
+
for (const k of pending) {
|
|
233
|
+
const l = listed.get(k)
|
|
234
|
+
if (l && !held.has(k) && !young(k)) objects.set(k, l.size)
|
|
235
|
+
}
|
|
236
|
+
plan.objects = [...objects].map(([key, size]) => ({ key, size })).sort((a, b) => (a.key < b.key ? -1 : 1))
|
|
237
|
+
plan.unindex = [...unindex].sort()
|
|
238
|
+
plan.missing = missing.size
|
|
239
|
+
plan.bytes = plan.drop.reduce((n, d) => n + d.size, 0) + plan.objects.reduce((n, x) => n + x.size, 0)
|
|
240
|
+
return plan
|
|
241
|
+
} finally {
|
|
242
|
+
rmSync(dir, { recursive: true, force: true })
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
export interface PruneResult {
|
|
247
|
+
runs: number
|
|
248
|
+
objects: number
|
|
249
|
+
bytes: number
|
|
250
|
+
failed: { key: string; error: string }[]
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/** the plan applied: every key checked and every index row it touches dropped before any call to the Space, the
|
|
254
|
+
* objects kept as pending (the runs that reach them go first), then the runs, the parts, the bytes. What fails or
|
|
255
|
+
* waits stays pending: the next prune checks it again against the runs then */
|
|
256
|
+
export async function applyPrune(plan: PrunePlan, o: { forget?: Forget } = {}): Promise<PruneResult> {
|
|
257
|
+
const forget = o.forget ?? (async (keys) => (await import('../forget.ts')).forgetBackupObjects(keys))
|
|
258
|
+
checkBackupKeys([...plan.drop.map((d) => d.key), ...plan.objects.map((x) => x.key)])
|
|
259
|
+
unindexKeys([...plan.unindex, ...plan.objects.map((x) => x.key)])
|
|
260
|
+
const left = new Set([...pendingObjects(), ...plan.objects.map((x) => x.key)])
|
|
261
|
+
machineSet(PENDING, [...left])
|
|
262
|
+
const runs = await forget(plan.drop.map((d) => d.key))
|
|
263
|
+
const gone = new Set(runs.gone)
|
|
264
|
+
for (const k of runs.gone) rmSync(cached(k), { force: true })
|
|
265
|
+
const out: PruneResult = { runs: runs.gone.length, objects: 0, bytes: plan.drop.filter((d) => gone.has(d.key)).reduce((n, d) => n + d.size, 0), failed: [...runs.failed] }
|
|
266
|
+
// a run that did not go still names its objects: none of them goes this time, the next prune sees them held
|
|
267
|
+
if (runs.failed.length > 0) return out
|
|
268
|
+
const parts = plan.objects.filter((x) => x.key.startsWith(PARTS_PREFIX))
|
|
269
|
+
const files = plan.objects.filter((x) => !x.key.startsWith(PARTS_PREFIX))
|
|
270
|
+
for (const batch of [parts, files]) {
|
|
271
|
+
const r = await forget(batch.map((x) => x.key))
|
|
272
|
+
const done = new Set(r.gone)
|
|
273
|
+
for (const k of r.gone) {
|
|
274
|
+
left.delete(k)
|
|
275
|
+
if (k.startsWith(PARTS_PREFIX)) rmSync(cached(k), { force: true })
|
|
276
|
+
}
|
|
277
|
+
machineSet(PENDING, [...left])
|
|
278
|
+
out.objects += r.gone.length
|
|
279
|
+
out.bytes += batch.filter((x) => done.has(x.key)).reduce((n, x) => n + x.size, 0)
|
|
280
|
+
out.failed.push(...r.failed)
|
|
281
|
+
if (r.failed.length > 0) break
|
|
282
|
+
}
|
|
283
|
+
return out
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/** the newest run's time (this machine's clock) and its LastModified (the Space's) differ by more: no prune */
|
|
287
|
+
export const CLOCK_SLACK_MS = 3_600_000
|
|
288
|
+
const TRASH_AT = 'backup-trash-at'
|
|
289
|
+
|
|
290
|
+
const gb = (b: number): string => (b >= 1e9 ? `${(b / 1e9).toFixed(2)} GB` : `${(b / 1e6).toFixed(1)} MB`)
|
|
291
|
+
|
|
292
|
+
/** the plan in lines: what stays, what goes, what waits */
|
|
293
|
+
export function planLines(p: PrunePlan): string[] {
|
|
294
|
+
const mine = p.keep.length + p.drop.length + p.deferred.length
|
|
295
|
+
return [
|
|
296
|
+
`${p.machine}: ${mine} runs; ${p.keep.length} stay, ${p.drop.length} go${p.deferred.length ? `, ${p.deferred.length} wait for a later prune (an object only they name is under ${GRACE_MS / 3_600_000} h old)` : ''}; ${p.others} runs of other machines untouched`,
|
|
297
|
+
`${p.objects.length} objects only the runs that go name (${p.objects.filter((x) => x.key.startsWith(PARTS_PREFIX)).length} parts): ${gb(p.bytes)} with the runs${p.missing ? `; ${p.missing} they name are not in the Space` : ''}${p.unindex.length ? `; ${p.unindex.length} stay for another machine, their index rows here go` : ''}`,
|
|
298
|
+
]
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/** the retention now: the plan, then with `apply` the deletes, under the backup's lock (no run of this machine
|
|
302
|
+
* writes meanwhile). The drill's last good run stays. Nothing goes while the house config names no retention */
|
|
303
|
+
export type Trash = () => Promise<{ keys: number; bytes: number; failed: { key: string; error: string }[] }>
|
|
304
|
+
|
|
305
|
+
/** the runs kept on purpose now, each with why: the drill's last good run and every pin not past its time */
|
|
306
|
+
export async function pinnedRuns(): Promise<Map<string, string>> {
|
|
307
|
+
const { drilledRun } = await import('./thread-copy.ts')
|
|
308
|
+
const out = new Map<string, string>()
|
|
309
|
+
for (const p of backupPins()) out.set(p.key, `pinned till ${new Date(p.until).toISOString().slice(0, 10)}: ${p.why}`)
|
|
310
|
+
const d = drilledRun()
|
|
311
|
+
if (d) out.set(d, "the drill's last good copy")
|
|
312
|
+
return out
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/** the retention now: the plan, then with `apply` the deletes, under the backup's lock (no run of this machine
|
|
316
|
+
* writes meanwhile), then the trash past its days (once a day by itself). Nothing goes while the house config names
|
|
317
|
+
* no retention, while this machine's clock and the Space's disagree, or (by itself) over its share of the runs */
|
|
318
|
+
export async function pruneBackups(space: Space, o: { apply: boolean; auto?: boolean; policy?: BackupRetention | null; now?: number; forget?: Forget; trash?: Trash | false }): Promise<{ lines: string[]; plan: PrunePlan | null; result: PruneResult | null }> {
|
|
319
|
+
const { policy, why } = o.policy !== undefined ? { policy: o.policy, why: 'no retention given: nothing is deleted' } : await houseRetention()
|
|
320
|
+
if (policy == null) return { lines: [`note: backup prune: ${why}`], plan: null, result: null }
|
|
321
|
+
const { takeBackupLock, lastRun } = await import('./run.ts')
|
|
322
|
+
const unlock = o.apply ? takeBackupLock() : () => {}
|
|
323
|
+
try {
|
|
324
|
+
const now = o.now ?? Date.now()
|
|
325
|
+
const pins = await pinnedRuns()
|
|
326
|
+
const plan = await planPrune(space, { policy, now, pinned: [...pins.keys()], pinLabel: (k) => pins.get(k), last: lastRun()?.key ?? null })
|
|
327
|
+
const lines = planLines(plan)
|
|
328
|
+
const newest = plan.mine.at(-1)
|
|
329
|
+
if (newest?.modified != null && Math.abs(newest.at.getTime() - newest.modified) > CLOCK_SLACK_MS)
|
|
330
|
+
return { lines: [...lines, `WARN backup prune: the newest run says ${newest.at.toISOString()}, the Space wrote it at ${new Date(newest.modified).toISOString()}: a clock is off, nothing is deleted`], plan, result: null }
|
|
331
|
+
const lines2 = [...lines]
|
|
332
|
+
let result: PruneResult | null = null
|
|
333
|
+
const work = plan.drop.length + plan.objects.length > 0
|
|
334
|
+
if (!o.apply) lines2.push('dry run: nothing deleted; --apply deletes')
|
|
335
|
+
else if (!work) lines2.push('nothing to delete')
|
|
336
|
+
else {
|
|
337
|
+
result = await applyPrune(plan, o.forget ? { forget: o.forget } : {})
|
|
338
|
+
lines2.push(`${result.failed.length ? 'WARN' : 'ok'}: backup prune: ${result.runs} runs and ${result.objects} objects deleted (${gb(result.bytes)}), recoverable ${TRASH_DAYS_TEXT} behind their delete markers`, ...result.failed.slice(0, 5).map((f) => ` failed ${f.key}: ${f.error}`))
|
|
339
|
+
}
|
|
340
|
+
if (o.apply && o.trash !== false && (!o.auto || now - (machineGet<number>(TRASH_AT) ?? 0) > 86_400_000)) {
|
|
341
|
+
const t = await (o.trash ?? (async () => (await import('../forget.ts')).emptyBackupTrash({ now })))()
|
|
342
|
+
if (t.failed.length === 0) machineSet(TRASH_AT, now)
|
|
343
|
+
if (t.keys > 0 || t.failed.length > 0) lines2.push(`${t.failed.length ? 'WARN' : 'ok'}: backup trash: ${t.keys} objects past ${TRASH_DAYS_TEXT} removed for good, ${gb(t.bytes)} freed${t.failed.length ? `, ${t.failed.length} failed, first ${t.failed[0]!.key}: ${t.failed[0]!.error}` : ''}`)
|
|
344
|
+
}
|
|
345
|
+
return { lines: lines2, plan, result }
|
|
346
|
+
} finally {
|
|
347
|
+
unlock()
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
const TRASH_DAYS_TEXT = `${TRASH_DAYS} days`
|
|
352
|
+
|
|
353
|
+
export type Forget = (keys: readonly string[]) => Promise<{ gone: string[]; failed: { key: string; error: string }[] }>
|
|
354
|
+
|
|
355
|
+
/** this machine's index rows of backup keys gone: its next run checks each in the Space before it trusts one */
|
|
356
|
+
function unindexKeys(keys: readonly string[]): void {
|
|
357
|
+
if (keys.length === 0) return
|
|
358
|
+
for (const k of keys) if (!k.startsWith(`${BACKUP_LABEL}/`)) throw new Error(`${k}: no backup key, no index row goes`)
|
|
359
|
+
const db = mediaDb()
|
|
360
|
+
if (!db) throw new Error('the media index does not open: no object goes')
|
|
361
|
+
const drop = db.query('delete from media_objects where key = ?')
|
|
362
|
+
db.transaction(() => {
|
|
363
|
+
for (const k of keys) drop.run(k)
|
|
364
|
+
})()
|
|
365
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
// which backups stay (wiki tools/shipkit/media-store-backup.md, Retention): every one of the last `allHours`, then the
|
|
2
|
+
// newest of the current calendar day, week and month and of the N before each, the newest of all, and any one a
|
|
3
|
+
// caller pins (the drill's last good copy). A slot with no backup keeps the next older one instead, and while history
|
|
4
|
+
// does not reach the oldest slot the oldest backup stays. Slots are UTC
|
|
5
|
+
// calendar slots, so the answer never depends on the clock's timezone, and a kept backup stays kept while its slot
|
|
6
|
+
// counts: the newest of a day that ended never changes. The numbers live in ONE place, the house config's
|
|
7
|
+
// `serve.backupRetention`; with none, nothing is deleted
|
|
8
|
+
|
|
9
|
+
/** the house config's `serve.backupRetention`: how many of each slot stay, counted back from now */
|
|
10
|
+
export interface BackupRetention {
|
|
11
|
+
/** every backup younger than this many hours */
|
|
12
|
+
allHours: number
|
|
13
|
+
/** the newest of today and of each of the N calendar days before it */
|
|
14
|
+
days: number
|
|
15
|
+
/** the newest of this ISO week (Monday first) and of each of the N weeks before it */
|
|
16
|
+
weeks: number
|
|
17
|
+
/** the newest of this calendar month and of each of the N months before it */
|
|
18
|
+
months: number
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const HOUR = 3_600_000
|
|
22
|
+
const DAY = 86_400_000
|
|
23
|
+
|
|
24
|
+
const dayOf = (t: number): number => Math.floor(t / DAY)
|
|
25
|
+
/** the ISO week a day falls in, Monday first: epoch day 0 is a Thursday */
|
|
26
|
+
const weekOf = (t: number): number => Math.floor((dayOf(t) + 3) / 7)
|
|
27
|
+
const monthOf = (t: number): number => {
|
|
28
|
+
const d = new Date(t)
|
|
29
|
+
return d.getUTCFullYear() * 12 + d.getUTCMonth()
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const iso = (t: number): string => new Date(t).toISOString()
|
|
33
|
+
const dayLabel = (day: number): string => iso(day * DAY).slice(0, 10)
|
|
34
|
+
const weekLabel = (week: number): string => {
|
|
35
|
+
// the ISO week year is the year of the week's Thursday
|
|
36
|
+
const thursday = (week * 7 - 3 + 3) * DAY
|
|
37
|
+
const y = new Date(thursday).getUTCFullYear()
|
|
38
|
+
const n = Math.floor((dayOf(thursday) - dayOf(Date.UTC(y, 0, 1))) / 7) + 1
|
|
39
|
+
return `${y}-W${String(n).padStart(2, '0')}`
|
|
40
|
+
}
|
|
41
|
+
const monthLabel = (month: number): string => `${Math.floor(month / 12)}-${String((month % 12) + 1).padStart(2, '0')}`
|
|
42
|
+
|
|
43
|
+
const GRAINS = [
|
|
44
|
+
{ name: 'day', count: (p: BackupRetention) => p.days, of: dayOf, label: dayLabel },
|
|
45
|
+
{ name: 'week', count: (p: BackupRetention) => p.weeks, of: weekOf, label: weekLabel },
|
|
46
|
+
{ name: 'month', count: (p: BackupRetention) => p.months, of: monthOf, label: monthLabel },
|
|
47
|
+
] as const
|
|
48
|
+
|
|
49
|
+
/** a policy whose numbers are whole and not negative, else why not */
|
|
50
|
+
export function retentionError(p: unknown): string | null {
|
|
51
|
+
if (p == null || typeof p !== 'object') return 'serve.backupRetention is no object'
|
|
52
|
+
const r = p as Record<string, unknown>
|
|
53
|
+
for (const k of ['allHours', 'days', 'weeks', 'months'] as const) {
|
|
54
|
+
const v = r[k]
|
|
55
|
+
if (typeof v !== 'number' || !Number.isInteger(v) || v < 0) return `serve.backupRetention.${k} must be a whole number of 0 or more, not ${JSON.stringify(v)}`
|
|
56
|
+
}
|
|
57
|
+
return null
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface Retained<T> {
|
|
61
|
+
/** each kept backup with the slots it fills, newest first */
|
|
62
|
+
keep: { item: T; slots: string[] }[]
|
|
63
|
+
/** the backups no slot keeps, newest first */
|
|
64
|
+
drop: T[]
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** the backups that stay under `policy` at `now`, each with its slots, and the ones that go. A backup with no valid
|
|
68
|
+
* time, or a time ahead of the clock, always stays */
|
|
69
|
+
export function retain<T>(items: readonly T[], o: { at: (t: T) => number; now: number; policy: BackupRetention; pinned?: (t: T) => boolean | string }): Retained<T> {
|
|
70
|
+
const why = retentionError(o.policy)
|
|
71
|
+
if (why) throw new Error(`${why}: nothing is deleted`)
|
|
72
|
+
const slots = new Map<T, string[]>()
|
|
73
|
+
const add = (t: T, s: string): void => {
|
|
74
|
+
const l = slots.get(t)
|
|
75
|
+
if (l) l.push(s)
|
|
76
|
+
else slots.set(t, [s])
|
|
77
|
+
}
|
|
78
|
+
const timed: { item: T; at: number }[] = []
|
|
79
|
+
const untimed: T[] = []
|
|
80
|
+
for (const item of items) {
|
|
81
|
+
const at = o.at(item)
|
|
82
|
+
if (Number.isFinite(at) && at <= o.now) {
|
|
83
|
+
timed.push({ item, at })
|
|
84
|
+
continue
|
|
85
|
+
}
|
|
86
|
+
untimed.push(item)
|
|
87
|
+
add(item, Number.isFinite(at) ? 'ahead of the clock: never deleted' : 'no time read: never deleted')
|
|
88
|
+
}
|
|
89
|
+
// newest first; a tie keeps the input's order
|
|
90
|
+
timed.sort((a, b) => b.at - a.at)
|
|
91
|
+
if (timed[0]) add(timed[0].item, 'the newest')
|
|
92
|
+
for (const t of timed) {
|
|
93
|
+
if (o.now - t.at < o.policy.allHours * HOUR) add(t.item, `the last ${o.policy.allHours} h`)
|
|
94
|
+
const pin = o.pinned?.(t.item)
|
|
95
|
+
if (pin) add(t.item, pin === true ? "the drill's last good copy" : pin)
|
|
96
|
+
}
|
|
97
|
+
for (const g of GRAINS) {
|
|
98
|
+
const cur = g.of(o.now)
|
|
99
|
+
const n = g.count(o.policy)
|
|
100
|
+
// the current slot, then the N before it: « the past 2 weeks » reaches back two whole weeks
|
|
101
|
+
for (let i = 0; n > 0 && i <= n; i++) {
|
|
102
|
+
const slot = cur - i
|
|
103
|
+
// newest first: the first at or before the slot is its newest, or the next older one when it holds none
|
|
104
|
+
const pick = timed.find((t) => g.of(t.at) <= slot)
|
|
105
|
+
if (!pick) {
|
|
106
|
+
// history does not reach this slot: the oldest backup is the furthest back there is
|
|
107
|
+
const oldest = timed.at(-1)
|
|
108
|
+
if (oldest) add(oldest.item, `${g.name} ${g.label(slot)}, none that far back: the oldest`)
|
|
109
|
+
break
|
|
110
|
+
}
|
|
111
|
+
add(pick.item, g.of(pick.at) === slot ? `${g.name} ${g.label(slot)}` : `${g.name} ${g.label(slot)}, none that ${g.name}: the next older`)
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
const keep: Retained<T>['keep'] = []
|
|
115
|
+
const drop: T[] = []
|
|
116
|
+
for (const item of [...untimed, ...timed.map((t) => t.item)]) {
|
|
117
|
+
const s = slots.get(item)
|
|
118
|
+
if (s) keep.push({ item, slots: s })
|
|
119
|
+
else drop.push(item)
|
|
120
|
+
}
|
|
121
|
+
return { keep, drop }
|
|
122
|
+
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
// A BACKUP RUN (wiki tools/shipkit/media-store.md, Backup): every source read once, refused or hashed (a sqlite
|
|
2
2
|
// snapshotted first), each object the Space lacks sent from the bytes that were hashed (never
|
|
3
3
|
// the live file again: it may have moved), then the run's parts the Space lacks, then the run itself, last, so a run
|
|
4
|
-
// never names an object the Space lacks. A run whose state equals the last one writes nothing.
|
|
5
|
-
//
|
|
4
|
+
// never names an object the Space lacks. A run whose state equals the last one writes nothing. Only the retention
|
|
5
|
+
// deletes (prune.ts): runs no slot keeps, and objects only they named
|
|
6
6
|
import { constants, copyFileSync, mkdirSync, mkdtempSync, readFileSync, rmSync, statSync, writeFileSync } from 'node:fs'
|
|
7
7
|
import { hostname, tmpdir } from 'node:os'
|
|
8
8
|
import { basename, dirname, join, relative } from 'node:path'
|
|
@@ -143,11 +143,15 @@ async function sendOne(space: Space, key: string, size: number, from: { bytes?:
|
|
|
143
143
|
}
|
|
144
144
|
}
|
|
145
145
|
|
|
146
|
+
/** is the object up: the Space's own listing when the run took one (the 6 h run: an index row the retention or a
|
|
147
|
+
* restore left stale heals there), else this machine's index */
|
|
148
|
+
const upIn = (listed: ReadonlyMap<string, number> | null, key: string, size: number): boolean => (listed ? listed.get(key) === size : isUp(key))
|
|
149
|
+
|
|
146
150
|
/** the objects of `ready` up, `jobs` at a time */
|
|
147
|
-
async function sendAll(space: Space, ready: readonly Ready[], scratch: string, jobs: number, out: RunResult): Promise<void> {
|
|
151
|
+
async function sendAll(space: Space, ready: readonly Ready[], scratch: string, jobs: number, out: RunResult, listed: ReadonlyMap<string, number> | null): Promise<void> {
|
|
148
152
|
const byKey = new Map<string, Ready>()
|
|
149
153
|
for (const r of ready) byKey.set(r.entry.key, r)
|
|
150
|
-
const list = [...byKey.values()].filter((r) => !
|
|
154
|
+
const list = [...byKey.values()].filter((r) => !upIn(listed, r.entry.key, r.size))
|
|
151
155
|
out.there += byKey.size - list.length
|
|
152
156
|
let next = 0
|
|
153
157
|
const work = async (): Promise<void> => {
|
|
@@ -189,7 +193,7 @@ export const LOCK_STALE_MS = 2 * 3600_000
|
|
|
189
193
|
|
|
190
194
|
/** one run at a time on the machine (the daemon's job and a hand run): a lock file with its pid, taken over once its
|
|
191
195
|
* process is gone or the lock is older than LOCK_STALE_MS */
|
|
192
|
-
function
|
|
196
|
+
export function takeBackupLock(): () => void {
|
|
193
197
|
const path = join(homeDir(), 'backup.lock')
|
|
194
198
|
mkdirSync(dirname(path), { recursive: true })
|
|
195
199
|
for (let i = 0; i < 2; i++) {
|
|
@@ -215,7 +219,7 @@ function lock(): () => void {
|
|
|
215
219
|
|
|
216
220
|
/** one run: every source to the Space, then the run's parts the Space lacks, then the run itself, last */
|
|
217
221
|
export async function backupRun(space: Space, o: Collect & { now?: Date; jobs?: number; sources?: readonly Source[]; rehash?: boolean }): Promise<RunResult> {
|
|
218
|
-
const unlock =
|
|
222
|
+
const unlock = takeBackupLock()
|
|
219
223
|
const scratch = mkdtempSync(join(tmpdir(), 'shipkit-backup-'))
|
|
220
224
|
try {
|
|
221
225
|
const now = o.now ?? new Date()
|
|
@@ -224,7 +228,9 @@ export async function backupRun(space: Space, o: Collect & { now?: Date; jobs?:
|
|
|
224
228
|
const digest = digestOf(files, refused)
|
|
225
229
|
const last = await lastOf(space, scratch)
|
|
226
230
|
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 }
|
|
227
|
-
|
|
231
|
+
// the run that reads every byte again checks every key in one listing of the Space (about 4 s)
|
|
232
|
+
const listed = o.rehash ? new Map((await space.list(`${BACKUP_LABEL}/`)).map((x) => [x.key, x.size])) : null
|
|
233
|
+
await sendAll(space, ready, scratch, o.jobs ?? 8, out, listed)
|
|
228
234
|
if (out.failed.length > 0) return out
|
|
229
235
|
const seen = new Map<string, SeenRow>()
|
|
230
236
|
for (const r of ready) if (!r.entry.sqlite) seen.set(r.entry.path, { size: r.entry.size, mtime: r.entry.mtime, sha: r.entry.sha, key: r.entry.key })
|
|
@@ -233,7 +239,7 @@ export async function backupRun(space: Space, o: Collect & { now?: Date; jobs?:
|
|
|
233
239
|
const enc = encodeRun({ at: now.toISOString(), host: hostname(), home: o.home, digest, files, refused }, o.repos.map((r) => relative(o.home, r)))
|
|
234
240
|
out.record.parts = enc.parts.length
|
|
235
241
|
for (const [i, p] of enc.parts.entries()) {
|
|
236
|
-
if (
|
|
242
|
+
if (upIn(listed, p.ref.key, p.bytes.length)) continue
|
|
237
243
|
try {
|
|
238
244
|
if (await sendOne(space, p.ref.key, p.bytes.length, p, scratch, -1 - i)) {
|
|
239
245
|
out.record.partsSent++
|