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
|
@@ -18,6 +18,7 @@ import { forgottenLogPath } from '../../hq/forgotten-log.ts'
|
|
|
18
18
|
import { hqDbPath } from '../../hq/hq-db.ts'
|
|
19
19
|
import { daemonDbPath } from '../../hq/outbox.ts'
|
|
20
20
|
import { loadRepoConfig, type RepoBackup } from '../../repo-config.ts'
|
|
21
|
+
import { realGit, type GitIo } from '../../core/git.ts'
|
|
21
22
|
import type { SqliteCut } from './snapshot.ts'
|
|
22
23
|
|
|
23
24
|
export interface Source {
|
|
@@ -108,29 +109,26 @@ const shipkitKeep = (rel: string, isDir: boolean): boolean => {
|
|
|
108
109
|
const comfyKeep = (rel: string): boolean => COMFY_KEEP.some((k) => rel === k || rel.startsWith(`${k}${sep}`))
|
|
109
110
|
const comfyWalkKeep = (rel: string, isDir: boolean): boolean => (isDir ? COMFY_KEEP.some((k) => k === rel.split(sep)[0]) : comfyKeep(rel))
|
|
110
111
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
const
|
|
114
|
-
|
|
115
|
-
return text.split('\0').filter((l) => l !== '')
|
|
112
|
+
/** the NUL separated names git printed, none when it failed */
|
|
113
|
+
async function gitNames(io: GitIo, repo: string, args: string[]): Promise<string[]> {
|
|
114
|
+
const r = await io.run(repo, args)
|
|
115
|
+
return r.code === 0 ? r.out.split('\0').filter((l) => l !== '') : []
|
|
116
116
|
}
|
|
117
117
|
|
|
118
118
|
/** the gitignored `*.local.*` files of a repo, never a settings file or code */
|
|
119
|
-
export async function localFiles(repo: string): Promise<string[]> {
|
|
120
|
-
const names = await
|
|
119
|
+
export async function localFiles(repo: string, io: GitIo = realGit): Promise<string[]> {
|
|
120
|
+
const names = await gitNames(io, repo, ['ls-files', '-z', '-o', '-i', '--exclude-standard', '--', ':(glob)**/*.local.*'])
|
|
121
121
|
return names.filter((n) => basename(n) !== 'settings.local.json' && !CODE.test(n) && !n.split('/').some((p) => p === 'node_modules')).map((n) => join(repo, n))
|
|
122
122
|
}
|
|
123
123
|
|
|
124
124
|
/** a repo's kept `*.local.*` list with these heard paths read again: one `git check-ignore` of them, never the
|
|
125
125
|
* repo's walk (seconds in a repo with large ignored folders) */
|
|
126
|
-
export async function patchLocals(repo: string, files: readonly string[], heard: readonly string[]): Promise<string[]> {
|
|
126
|
+
export async function patchLocals(repo: string, files: readonly string[], heard: readonly string[], io: GitIo = realGit): Promise<string[]> {
|
|
127
127
|
const rels = heard.filter((p) => p.startsWith(`${repo}${sep}`) && isLocalName(p.slice(repo.length + 1))).map((p) => p.slice(repo.length + 1))
|
|
128
128
|
if (rels.length === 0) return [...files]
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
const ignored = new Set((await new Response(p.stdout).text()).split('\0').filter((n) => n !== ''))
|
|
133
|
-
await p.exited
|
|
129
|
+
// exit 1 is « none ignored »: the names printed are the answer whatever the code
|
|
130
|
+
const r = await io.run(repo, ['check-ignore', '-z', '--stdin'], { input: rels.join('\0') + '\0' })
|
|
131
|
+
const ignored = new Set(r.out.split('\0').filter((n) => n !== ''))
|
|
134
132
|
const out = new Set(files)
|
|
135
133
|
for (const rel of rels) {
|
|
136
134
|
const abs = join(repo, rel)
|
|
@@ -158,6 +156,8 @@ export interface Collect {
|
|
|
158
156
|
backupOf?: (repo: string) => Promise<RepoBackup | null>
|
|
159
157
|
/** only the sources at or under these paths: what a full collect gives there, nothing else is read */
|
|
160
158
|
scope?: Scope
|
|
159
|
+
/** what git tracks and ignores in each repo; the real git by default */
|
|
160
|
+
git?: GitIo
|
|
161
161
|
}
|
|
162
162
|
|
|
163
163
|
const configs = new Map<string, { mtime: number; backup: RepoBackup | null }>()
|
|
@@ -174,8 +174,9 @@ export async function backupOfRepo(repo: string): Promise<RepoBackup | null> {
|
|
|
174
174
|
return backup
|
|
175
175
|
}
|
|
176
176
|
|
|
177
|
-
/** the review cards' tables
|
|
178
|
-
|
|
177
|
+
/** the review cards' tables and the threads' events with their projections (wiki tools/shipkit/threads-events.md): in the
|
|
178
|
+
* daemon's file, or in the machine db once it is folded */
|
|
179
|
+
export const CARD_TABLES = ['review_cards', 'review_msgs', 'review_gone', 'review_replaced', 'review_repos', 'thread_events', 'thread_post_groups', 'thread_posts']
|
|
179
180
|
|
|
180
181
|
/** the machine db as a run holds it. The Mac's own names stay out: a run restored on another Mac must not make it
|
|
181
182
|
* take this one's name, nor its place in an hq (the hq it joined, the switch that opens its own, its key:
|
|
@@ -232,7 +233,7 @@ export async function collect(o: Collect): Promise<Source[]> {
|
|
|
232
233
|
for (const root of await agentRoots(o.home)) for (const repo of o.repos) await walk(join(root, sessionName(repo), 'memory'), () => true, files, undefined, scope)
|
|
233
234
|
const repos = scope ? o.repos.filter((r) => scope.reaches(r)) : o.repos
|
|
234
235
|
// the git walks of ignored trees take seconds each: all at once
|
|
235
|
-
const locals = new Map(await Promise.all(repos.map(async (r) => [r, await (o.locals ?? localFiles)(r)] as const)))
|
|
236
|
+
const locals = new Map(await Promise.all(repos.map(async (r) => [r, await (o.locals ?? ((x: string) => localFiles(x, o.git)))(r)] as const)))
|
|
236
237
|
const whole = new Set<string>()
|
|
237
238
|
const extra: Source[] = []
|
|
238
239
|
for (const repo of repos) {
|
|
@@ -243,7 +244,7 @@ export async function collect(o: Collect): Promise<Source[]> {
|
|
|
243
244
|
extra.push({ abs: join(repo, 'repo.config.ts'), rel: relative(o.home, join(repo, 'repo.config.ts')), size: 0, mtime: 0, broken: `its backup key is unread, the config does not load: ${(e as Error).message.split('\n')[0]}` })
|
|
244
245
|
}
|
|
245
246
|
const named = cfg?.paths ?? []
|
|
246
|
-
const tracked = new Set((await git
|
|
247
|
+
const tracked = new Set((await gitNames(o.git ?? realGit, repo, ['ls-files', '-z', '--', '.shipkit', '.comfy-ts', ...REPO_EXTRA, ...named])).map((n) => join(repo, n)))
|
|
247
248
|
for (const p of named) {
|
|
248
249
|
const abs = join(repo, p)
|
|
249
250
|
const st = await lstat(abs).catch(() => null)
|
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
// the thread store's second copy (wiki tools/shipkit/media-store-backup.md, The thread store's second copy): its
|
|
2
|
+
// tables snapshotted like a run's, gzipped, sent over ssh to another machine of the house (its own disk, a provider of
|
|
3
|
+
// its own), checked there by its sha before it takes its name; and the drill that brings back the newest Space run
|
|
4
|
+
// and the newest copy and compares every ask thread with the live store.
|
|
5
|
+
// The one constraint a reader breaks first: a copy is named by the time and the sha of its bytes, and the drill trusts
|
|
6
|
+
// a copy only when its bytes give that sha again
|
|
7
|
+
import { Database } from 'bun:sqlite'
|
|
8
|
+
import { existsSync, mkdtempSync, readdirSync, readFileSync, rmSync, statSync, writeFileSync } from 'fs'
|
|
9
|
+
import { tmpdir } from 'os'
|
|
10
|
+
import { join, relative } from 'path'
|
|
11
|
+
import { machineDbPath, machineGet, machineSet } from '../../core/machine-db.ts'
|
|
12
|
+
import { machineName } from '../../home/machine-host.ts'
|
|
13
|
+
import type { Space } from '../space.ts'
|
|
14
|
+
import { restore } from './restore.ts'
|
|
15
|
+
import { retain, type BackupRetention } from './retention.ts'
|
|
16
|
+
import { snapshotSqlite, type SqliteCut } from './snapshot.ts'
|
|
17
|
+
import { CARD_TABLES } from './sources.ts'
|
|
18
|
+
|
|
19
|
+
export const threadCut: SqliteCut = { tables: [], maybe: [...CARD_TABLES] }
|
|
20
|
+
/** the folder of the copies under the peer's home, one folder a machine */
|
|
21
|
+
export const COPY_DIR = '.shipkit/thread-copies'
|
|
22
|
+
/** a copy at most each 10 min while the store moves */
|
|
23
|
+
export const COPY_FLOOR_MS = 10 * 60_000
|
|
24
|
+
|
|
25
|
+
const PEER = 'thread-copy-peer'
|
|
26
|
+
const LAST = 'thread-copy-last'
|
|
27
|
+
export type CopyLast = { stamp: string; at: number; name: string } | { stamp: string; at: number; failed: string }
|
|
28
|
+
|
|
29
|
+
const NAME = /^(\d{8}T\d{6}Z)-([0-9a-f]{12})\.sqlite\.gz$/
|
|
30
|
+
const SAFE = /^[A-Za-z0-9._-]+$/
|
|
31
|
+
|
|
32
|
+
export type Ran = { code: number; out: Uint8Array<ArrayBuffer>; err: string }
|
|
33
|
+
/** one command on the peer, its stdin given; a test passes a local shell */
|
|
34
|
+
export type Ssh = (peer: string, command: string, stdin?: Uint8Array) => Promise<Ran>
|
|
35
|
+
|
|
36
|
+
export const houseSsh: Ssh = async (peer, command, stdin) => {
|
|
37
|
+
const p = Bun.spawn(['ssh', '-o', 'BatchMode=yes', '-o', 'ConnectTimeout=8', peer, command], { stdin: stdin ?? 'ignore', stdout: 'pipe', stderr: 'pipe' })
|
|
38
|
+
const [out, err] = await Promise.all([new Response(p.stdout).bytes(), new Response(p.stderr).text()])
|
|
39
|
+
return { code: await p.exited, out, err: err.trim() }
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export const copyPeer = (): string | null => machineGet<string>(PEER) || null
|
|
43
|
+
export function setCopyPeer(peer: string): void {
|
|
44
|
+
if (!SAFE.test(peer)) throw new Error(`${peer}: an ssh host name of letters, digits, dots, dashes`)
|
|
45
|
+
machineSet(PEER, peer)
|
|
46
|
+
}
|
|
47
|
+
export const lastCopy = (): CopyLast | null => machineGet<CopyLast>(LAST)
|
|
48
|
+
|
|
49
|
+
/** the store's stamp: its newest write number, its cards, messages and tombstones; a query of a few ms */
|
|
50
|
+
export function threadStamp(db = machineDbPath()): string {
|
|
51
|
+
const d = new Database(db, { readonly: true })
|
|
52
|
+
try {
|
|
53
|
+
const has = new Set((d.query(`select name from sqlite_schema where type = 'table'`).all() as { name: string }[]).map((r) => r.name))
|
|
54
|
+
if (!has.has('review_cards')) return 'none'
|
|
55
|
+
const c = d.query('select coalesce(max(seq), 0) as s, count(*) as n from review_cards').get() as { s: number; n: number }
|
|
56
|
+
const m = d.query('select count(*) as n from review_msgs').get() as { n: number }
|
|
57
|
+
const g = has.has('review_gone') ? (d.query('select count(*) as n from review_gone').get() as { n: number }).n : 0
|
|
58
|
+
return `${c.s}:${c.n}:${m.n}:${g}`
|
|
59
|
+
} finally {
|
|
60
|
+
d.close()
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** a copy is due once a peer is set, the store moved since the last copy, and that copy is 10 min old */
|
|
65
|
+
export function threadCopyDue(now: number, stamp: () => string = threadStamp): boolean {
|
|
66
|
+
if (copyPeer() == null) return false
|
|
67
|
+
const last = lastCopy()
|
|
68
|
+
if (last != null && now - last.at < COPY_FLOOR_MS) return false
|
|
69
|
+
return last == null || 'failed' in last || last.stamp !== stamp()
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const stampOf = (d: Date): string => d.toISOString().replace(/[-:]/g, '').replace(/\.\d+Z$/, 'Z')
|
|
73
|
+
const timeOf = (name: string): number => {
|
|
74
|
+
const m = NAME.exec(name)
|
|
75
|
+
if (m == null) return NaN
|
|
76
|
+
const t = m[1]!
|
|
77
|
+
return Date.parse(`${t.slice(0, 4)}-${t.slice(4, 6)}-${t.slice(6, 8)}T${t.slice(9, 11)}:${t.slice(11, 13)}:${t.slice(13, 15)}Z`)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** which copies stay under the backup's retention (retention.ts): the newest always, the drill's last good copy, the
|
|
81
|
+
* slots of the policy; a name of another shape is never touched */
|
|
82
|
+
export function keptCopies(names: readonly string[], now: number, policy: BackupRetention, pinned?: string | null): { keep: string[]; drop: string[] } {
|
|
83
|
+
const r = retain(names.filter((n) => NAME.test(n)), { at: timeOf, now, policy, pinned: (n) => n === pinned })
|
|
84
|
+
return { keep: r.keep.map((k) => k.item).sort(), drop: r.drop.sort() }
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const sha256 = (b: Uint8Array): string => new Bun.CryptoHasher('sha256').update(b).digest('hex')
|
|
88
|
+
const q = (s: string): string => `'${s.replace(/'/g, `'\\''`)}'`
|
|
89
|
+
|
|
90
|
+
/** the copy now, when the store moved since the last one (or `force`): sent, checked by its sha on the peer, then the
|
|
91
|
+
* copies no retention slot keeps pruned (none while the house config names no retention: `policy` null). Each
|
|
92
|
+
* failure is said and kept, so the next look tries again */
|
|
93
|
+
export async function sendThreadCopy(o: { peer?: string; machine?: string; now?: Date; force?: boolean; ssh?: Ssh; db?: string; policy?: BackupRetention | null } = {}): Promise<{ sent: string | null; why: string; pruned: number; bytes: number }> {
|
|
94
|
+
const peer = o.peer ?? copyPeer()
|
|
95
|
+
if (peer == null) return { sent: null, why: 'no peer set: sk backup threads --peer <ssh host>', pruned: 0, bytes: 0 }
|
|
96
|
+
const machine = o.machine ?? machineName()
|
|
97
|
+
if (!SAFE.test(machine)) return { sent: null, why: `${machine}: not a safe folder name`, pruned: 0, bytes: 0 }
|
|
98
|
+
const db = o.db ?? machineDbPath()
|
|
99
|
+
const stamp = threadStamp(db)
|
|
100
|
+
const last = lastCopy()
|
|
101
|
+
if (!o.force && last != null && !('failed' in last) && last.stamp === stamp) return { sent: null, why: `the store did not move since ${last.name}`, pruned: 0, bytes: 0 }
|
|
102
|
+
const now = o.now ?? new Date()
|
|
103
|
+
const ssh = o.ssh ?? houseSsh
|
|
104
|
+
const tmp = mkdtempSync(join(tmpdir(), 'thread-copy-'))
|
|
105
|
+
const fail = (why: string) => {
|
|
106
|
+
machineSet(LAST, { stamp, at: now.getTime(), failed: why } satisfies CopyLast)
|
|
107
|
+
return { sent: null, why, pruned: 0, bytes: 0 }
|
|
108
|
+
}
|
|
109
|
+
try {
|
|
110
|
+
const file = join(tmp, 'threads.sqlite')
|
|
111
|
+
snapshotSqlite(db, file, threadCut)
|
|
112
|
+
const gz = Bun.gzipSync(readFileSync(file))
|
|
113
|
+
const sha = sha256(gz)
|
|
114
|
+
const name = `${stampOf(now)}-${sha.slice(0, 12)}.sqlite.gz`
|
|
115
|
+
const dir = `${COPY_DIR}/${machine}`
|
|
116
|
+
const put = await ssh(peer, `set -e; d=${q(dir)}; mkdir -p "$d"; cat > "$d/${name}.part"; s=$(shasum -a 256 "$d/${name}.part" | cut -d' ' -f1); if [ "$s" = ${q(sha)} ]; then mv "$d/${name}.part" "$d/${name}"; echo ok; else rm -f "$d/${name}.part"; echo "sha $s"; exit 3; fi`, gz)
|
|
117
|
+
if (put.code !== 0) return fail(`${peer} did not keep the copy: ${put.err || new TextDecoder().decode(put.out).trim() || `exit ${put.code}`}`)
|
|
118
|
+
machineSet(LAST, { stamp, at: now.getTime(), name } satisfies CopyLast)
|
|
119
|
+
const sent = `${peer}:~/${dir}/${name}`
|
|
120
|
+
try {
|
|
121
|
+
const policy = o.policy !== undefined ? o.policy : (await (await import('./prune.ts')).houseRetention()).policy
|
|
122
|
+
if (policy == null) return { sent, why: '', pruned: 0, bytes: gz.length }
|
|
123
|
+
const listed = await ssh(peer, `date -u +%s; ls -1 ${q(dir)}`)
|
|
124
|
+
if (listed.code !== 0) throw new Error(listed.err || `exit ${listed.code}`)
|
|
125
|
+
const [clock, ...names] = new TextDecoder().decode(listed.out).split('\n')
|
|
126
|
+
// a clock that jumped would empty the peer: this machine's and the peer's must agree within the hour
|
|
127
|
+
const peerNow = Number(clock) * 1000
|
|
128
|
+
if (!Number.isFinite(peerNow) || Math.abs(peerNow - now.getTime()) > 3_600_000) throw new Error(`${peer} says the time is ${clock}, this machine ${Math.round(now.getTime() / 1000)}: a clock is off, none pruned`)
|
|
129
|
+
const { drop } = keptCopies(names, now.getTime(), policy, drilledCopy())
|
|
130
|
+
if (drop.length > 0) {
|
|
131
|
+
const rm = await ssh(peer, `cd ${q(dir)} && rm -f -- ${drop.map(q).join(' ')}`)
|
|
132
|
+
if (rm.code !== 0) throw new Error(rm.err || `exit ${rm.code}`)
|
|
133
|
+
}
|
|
134
|
+
return { sent, why: '', pruned: drop.length, bytes: gz.length }
|
|
135
|
+
} catch (e) {
|
|
136
|
+
return { sent, why: `the old copies were not pruned: ${(e as Error).message}`, pruned: 0, bytes: gz.length }
|
|
137
|
+
}
|
|
138
|
+
} catch (e) {
|
|
139
|
+
return fail((e as Error).message)
|
|
140
|
+
} finally {
|
|
141
|
+
rmSync(tmp, { recursive: true, force: true })
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** a thread as the drill compares it: its head's rev and time, its address, each message's rev */
|
|
146
|
+
export type ThreadRow = { on: string; rev: string; at: number; msgs: Map<string, string> }
|
|
147
|
+
|
|
148
|
+
/** the ask threads of a store file (`on` an `ask:` address), by repo and id */
|
|
149
|
+
export function askThreads(file: string): Map<string, ThreadRow> {
|
|
150
|
+
const d = new Database(file, { readonly: true })
|
|
151
|
+
try {
|
|
152
|
+
const out = new Map<string, ThreadRow>()
|
|
153
|
+
for (const r of d.query('select repo, id, rev, at, head from review_cards').all() as { repo: string; id: string; rev: string; at: number; head: string }[]) {
|
|
154
|
+
let on = ''
|
|
155
|
+
try {
|
|
156
|
+
on = String((JSON.parse(r.head) as { on?: unknown }).on ?? '')
|
|
157
|
+
} catch {
|
|
158
|
+
on = ''
|
|
159
|
+
}
|
|
160
|
+
if (!on.startsWith('ask:')) continue
|
|
161
|
+
out.set(`${r.repo} ${r.id}`, { on, rev: r.rev, at: r.at, msgs: new Map() })
|
|
162
|
+
}
|
|
163
|
+
for (const m of d.query('select repo, card, id, rev from review_msgs').all() as { repo: string; card: string; id: string; rev: string }[]) out.get(`${m.repo} ${m.card}`)?.msgs.set(m.id, m.rev)
|
|
164
|
+
return out
|
|
165
|
+
} finally {
|
|
166
|
+
d.close()
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** the ids of the threads the live store dropped on purpose (its tombstones) */
|
|
171
|
+
function goneIn(file: string): Set<string> {
|
|
172
|
+
const d = new Database(file, { readonly: true })
|
|
173
|
+
try {
|
|
174
|
+
const t = d.query(`select 1 from sqlite_schema where name = 'review_gone'`).get()
|
|
175
|
+
return t == null ? new Set() : new Set((d.query('select repo, id from review_gone').all() as { repo: string; id: string }[]).map((r) => `${r.repo} ${r.id}`))
|
|
176
|
+
} finally {
|
|
177
|
+
d.close()
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** a write a few minutes around the copy's time may be on either side of it */
|
|
182
|
+
export const SLACK_MS = 5 * 60_000
|
|
183
|
+
|
|
184
|
+
export type Compare = { live: number; copy: number; same: number; moved: number; gone: number; missing: string[]; differ: string[]; lost: string[] }
|
|
185
|
+
|
|
186
|
+
/** every ask thread of the copy against the live store: the same head and messages, or moved since the copy (its
|
|
187
|
+
* time after it), or dropped since (a tombstone); anything else is a failure */
|
|
188
|
+
export function compareThreads(live: Map<string, ThreadRow>, copy: Map<string, ThreadRow>, copyAt: number, gone: ReadonlySet<string> = new Set()): Compare {
|
|
189
|
+
const r: Compare = { live: live.size, copy: copy.size, same: 0, moved: 0, gone: 0, missing: [], differ: [], lost: [] }
|
|
190
|
+
for (const [k, l] of live) {
|
|
191
|
+
const c = copy.get(k)
|
|
192
|
+
if (l.at > copyAt - SLACK_MS) {
|
|
193
|
+
r.moved++
|
|
194
|
+
continue
|
|
195
|
+
}
|
|
196
|
+
if (c == null) {
|
|
197
|
+
r.missing.push(l.on)
|
|
198
|
+
continue
|
|
199
|
+
}
|
|
200
|
+
// a message moves no thread's time: a thread that only grew after the copy holds every message the copy has
|
|
201
|
+
const kept = c.rev === l.rev && [...c.msgs].every(([id, rev]) => l.msgs.get(id) === rev)
|
|
202
|
+
if (!kept) r.differ.push(l.on)
|
|
203
|
+
else if (l.msgs.size > c.msgs.size) r.moved++
|
|
204
|
+
else r.same++
|
|
205
|
+
}
|
|
206
|
+
for (const [k, c] of copy) {
|
|
207
|
+
if (live.has(k)) continue
|
|
208
|
+
if (gone.has(k)) r.gone++
|
|
209
|
+
else r.lost.push(c.on)
|
|
210
|
+
}
|
|
211
|
+
return r
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
export const compareOk = (c: Compare): boolean => c.missing.length === 0 && c.differ.length === 0 && c.lost.length === 0
|
|
215
|
+
|
|
216
|
+
/** the threads' events of a copy against the live table (wiki tools/shipkit/threads-events.md, Kept by the backups):
|
|
217
|
+
* append only, so every event of the copy is in the live table unchanged, and one the live table holds past the copy's
|
|
218
|
+
* newest is newer; each side's projections are what its own events make */
|
|
219
|
+
export type EventsCompare = { live: number; copy: number; same: number; newer: number; lost: string[]; differ: string[]; drift: string[] }
|
|
220
|
+
|
|
221
|
+
type EvRow = { repo: string; id: string; thread: string; seq: number; type: string; state_key: string | null; content: string }
|
|
222
|
+
function eventsIn(file: string): { rows: Map<string, EvRow>; drift: string[] } | null {
|
|
223
|
+
const d = new Database(file, { readonly: true })
|
|
224
|
+
try {
|
|
225
|
+
if (d.query(`select 1 from sqlite_schema where name = 'thread_events'`).get() == null) return null
|
|
226
|
+
const rows = new Map<string, EvRow>()
|
|
227
|
+
for (const r of d.query('select repo, id, thread, seq, type, state_key, content from thread_events').all() as EvRow[]) rows.set(`${r.repo} ${r.id}`, r)
|
|
228
|
+
const { projectionDrift } = require('../../review/projections.ts') as typeof import('../../review/projections.ts')
|
|
229
|
+
const drift = d.query(`select 1 from sqlite_schema where name = 'thread_post_groups'`).get() == null ? [] : projectionDrift(d)
|
|
230
|
+
return { rows, drift }
|
|
231
|
+
} finally {
|
|
232
|
+
d.close()
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
export function compareEvents(liveFile: string, copyFile: string): EventsCompare {
|
|
237
|
+
const live = eventsIn(liveFile)
|
|
238
|
+
const copy = eventsIn(copyFile)
|
|
239
|
+
const r: EventsCompare = { live: live?.rows.size ?? 0, copy: copy?.rows.size ?? 0, same: 0, newer: 0, lost: [], differ: [], drift: [...(live?.drift ?? []).map((x) => `live ${x}`), ...(copy?.drift ?? []).map((x) => `copy ${x}`)] }
|
|
240
|
+
for (const [k, c] of copy?.rows ?? []) {
|
|
241
|
+
const l = live?.rows.get(k)
|
|
242
|
+
if (l == null) r.lost.push(k)
|
|
243
|
+
else if (l.seq !== c.seq || l.thread !== c.thread || l.type !== c.type || l.state_key !== c.state_key || l.content !== c.content) r.differ.push(k)
|
|
244
|
+
else r.same++
|
|
245
|
+
}
|
|
246
|
+
for (const k of live?.rows.keys() ?? []) if (!copy?.rows.has(k)) r.newer++
|
|
247
|
+
return r
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
export const eventsOk = (c: EventsCompare): boolean => c.lost.length === 0 && c.differ.length === 0 && c.drift.length === 0
|
|
251
|
+
|
|
252
|
+
/** `key`: the Space run's key, or the copy's name on the peer */
|
|
253
|
+
export type Side = { what: string; at: number | null; ok: boolean; why?: string; cmp?: Compare; events?: EventsCompare; key?: string; side: 'space' | 'peer' }
|
|
254
|
+
const DRILL = 'thread-drill'
|
|
255
|
+
/** `good`: the key of the last copy this side brought back whole, kept across a drill that failed */
|
|
256
|
+
export type ThreadDrill = { at: number; ok: boolean; sides: { what: string; ok: boolean; why?: string; same?: number; moved?: number; events?: number; key?: string; good?: string; side?: 'space' | 'peer' }[] }
|
|
257
|
+
export const lastThreadDrill = (): ThreadDrill | null => machineGet<ThreadDrill>(DRILL)
|
|
258
|
+
|
|
259
|
+
/** the copy a side of the drill last brought back whole, even when a later drill failed: the retention never deletes it */
|
|
260
|
+
const drilled = (side: 'space' | 'peer'): string | null => lastThreadDrill()?.sides.find((s) => s.side === side)?.good ?? null
|
|
261
|
+
export const drilledRun = (): string | null => drilled('space')
|
|
262
|
+
export const drilledCopy = (): string | null => drilled('peer')
|
|
263
|
+
|
|
264
|
+
const findFile = (dir: string, base: string): string | null => {
|
|
265
|
+
if (!existsSync(dir)) return null
|
|
266
|
+
for (const e of readdirSync(dir)) {
|
|
267
|
+
const p = join(dir, e)
|
|
268
|
+
if (statSync(p).isDirectory()) {
|
|
269
|
+
const f = findFile(p, base)
|
|
270
|
+
if (f) return f
|
|
271
|
+
} else if (e === base) return p
|
|
272
|
+
}
|
|
273
|
+
return null
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/** the newest Space run and the newest copy on the peer, each brought back into a scratch folder and compared with the
|
|
277
|
+
* live store; kept in the machine db for the hub and the clock's check */
|
|
278
|
+
export async function threadDrill(o: { space: Space | null; spaceWhy?: string; home: string; peer?: string | null; machine?: string; ssh?: Ssh; db?: string; keep?: boolean }): Promise<{ ok: boolean; sides: Side[]; scratch: string }> {
|
|
279
|
+
const db = o.db ?? machineDbPath()
|
|
280
|
+
const machine = o.machine ?? machineName()
|
|
281
|
+
const scratch = mkdtempSync(join(tmpdir(), 'thread-drill-'))
|
|
282
|
+
const live = askThreads(db)
|
|
283
|
+
const gone = goneIn(db)
|
|
284
|
+
const spaceSide = async (): Promise<Side> => {
|
|
285
|
+
if (o.space == null) return { side: 'space', what: 'the Space run', at: null, ok: false, why: `the Space is not reachable: ${o.spaceWhy ?? 'no Space'}` }
|
|
286
|
+
try {
|
|
287
|
+
const to = join(scratch, 'space')
|
|
288
|
+
const r = await restore(o.space, { home: o.home, to, only: relative(o.home, db), machine })
|
|
289
|
+
const f = findFile(to, db.split('/').pop()!)
|
|
290
|
+
const at = Date.parse(r.manifest.at)
|
|
291
|
+
if (f == null) return { side: 'space', what: 'the Space run', at, ok: false, why: `the run of ${r.manifest.at} holds no ${relative(o.home, db)}` }
|
|
292
|
+
const cmp = compareThreads(live, askThreads(f), at, gone)
|
|
293
|
+
const events = compareEvents(db, f)
|
|
294
|
+
return { side: 'space', key: r.run, what: `the Space run of ${r.manifest.at}`, at, ok: compareOk(cmp) && eventsOk(events), cmp, events }
|
|
295
|
+
} catch (e) {
|
|
296
|
+
return { side: 'space', what: 'the Space run', at: null, ok: false, why: (e as Error).message }
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
const peerSide = async (): Promise<Side> => {
|
|
300
|
+
const peer = o.peer === undefined ? copyPeer() : o.peer
|
|
301
|
+
if (peer == null) return { side: 'peer', what: 'the second copy', at: null, ok: false, why: 'no peer set: sk backup threads --peer <ssh host>' }
|
|
302
|
+
const ssh = o.ssh ?? houseSsh
|
|
303
|
+
const dir = `${COPY_DIR}/${machine}`
|
|
304
|
+
const listed = await ssh(peer, `ls -1 ${q(dir)}`)
|
|
305
|
+
const newest = new TextDecoder().decode(listed.out).split('\n').filter((n) => NAME.test(n)).sort().pop()
|
|
306
|
+
if (listed.code !== 0 || newest == null) return { side: 'peer', what: `the copy on ${peer}`, at: null, ok: false, why: `${peer} holds no copy of ${machine}: ${listed.err || 'its folder is empty'}` }
|
|
307
|
+
const got = await ssh(peer, `cat ${q(`${dir}/${newest}`)}`)
|
|
308
|
+
const at = timeOf(newest)
|
|
309
|
+
const sha = sha256(got.out)
|
|
310
|
+
const what = `the copy ${peer}:~/${dir}/${newest}`
|
|
311
|
+
if (got.code !== 0 || !sha.startsWith(NAME.exec(newest)![2]!)) return { side: 'peer', what, at, ok: false, why: got.code !== 0 ? got.err : `its bytes give the sha ${sha.slice(0, 12)}, not the one of its name` }
|
|
312
|
+
const f = join(scratch, 'peer.sqlite')
|
|
313
|
+
writeFileSync(f, Bun.gunzipSync(got.out))
|
|
314
|
+
const cmp = compareThreads(live, askThreads(f), at, gone)
|
|
315
|
+
const events = compareEvents(db, f)
|
|
316
|
+
return { side: 'peer', key: newest, what, at, ok: compareOk(cmp) && eventsOk(events), cmp, events }
|
|
317
|
+
}
|
|
318
|
+
try {
|
|
319
|
+
const sides = await Promise.all([spaceSide(), peerSide()])
|
|
320
|
+
const ok = sides.every((s) => s.ok)
|
|
321
|
+
const was = lastThreadDrill()
|
|
322
|
+
const goodOf = (s: Side): string | undefined => (s.ok && s.key ? s.key : was?.sides.find((p) => p.side === s.side)?.good)
|
|
323
|
+
machineSet(DRILL, { at: Date.now(), ok, sides: sides.map((s) => ({ what: s.what, ok: s.ok, side: s.side, ...(s.key ? { key: s.key } : {}), ...(goodOf(s) ? { good: goodOf(s)! } : {}), ...(s.why ? { why: s.why } : {}), ...(s.cmp ? { same: s.cmp.same, moved: s.cmp.moved } : {}), ...(s.events ? { events: s.events.same } : {}) })) } satisfies ThreadDrill)
|
|
324
|
+
return { ok, sides, scratch }
|
|
325
|
+
} finally {
|
|
326
|
+
if (!o.keep) rmSync(scratch, { recursive: true, force: true })
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/** one line a side, for the cli and the check */
|
|
331
|
+
export function sideLine(s: Side): string {
|
|
332
|
+
const c = s.cmp
|
|
333
|
+
if (c == null) return `${s.ok ? 'ok' : 'FAIL'}: ${s.what}: ${s.why ?? ''}`
|
|
334
|
+
const bad = [...c.missing.map((a) => `missing ${a}`), ...c.differ.map((a) => `differs ${a}`), ...c.lost.map((a) => `only in the copy ${a}`)]
|
|
335
|
+
const e = s.events
|
|
336
|
+
const evBad = e == null ? [] : [...e.lost.map((k) => `event only in the copy ${k}`), ...e.differ.map((k) => `event differs ${k}`), ...e.drift]
|
|
337
|
+
const evLine = e == null ? '' : `; ${e.same} events the same, ${e.newer} newer (live ${e.live}, copy ${e.copy})`
|
|
338
|
+
const all = [...bad, ...evBad]
|
|
339
|
+
return `${s.ok ? 'ok' : 'FAIL'}: ${s.what}: ${c.same} ask threads the same, ${c.moved} moved since, ${c.gone} dropped since (live ${c.live}, copy ${c.copy})${evLine}${all.length ? `; ${all.slice(0, 5).join(', ')}` : ''}`
|
|
340
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// the second place of the Space (wiki tools/shipkit/media-store.md): the house config's `serve.spaceCopy`, a bucket at
|
|
2
2
|
// another provider that holds a copy of every object of the Space and the copy's own snapshots. Both key pairs are
|
|
3
|
-
// read through the house
|
|
3
|
+
// read through the house store and held in memory only
|
|
4
4
|
import { houseSpaceConfig, type SpaceConfig } from '../space.ts'
|
|
5
5
|
|
|
6
6
|
/** the house config's `serve.spaceCopy` */
|
|
@@ -14,7 +14,7 @@ export interface SpaceCopy {
|
|
|
14
14
|
region: string
|
|
15
15
|
/** the bucket in the path, not the host: an account wide origin (R2) wants it */
|
|
16
16
|
path?: boolean
|
|
17
|
-
/** the secret whose value is `KEY:SECRET`, read through
|
|
17
|
+
/** the secret whose value is `KEY:SECRET`, read through the house store */
|
|
18
18
|
secret: string
|
|
19
19
|
}
|
|
20
20
|
|
|
@@ -37,10 +37,9 @@ export async function houseCopy(cwd: string = process.cwd()): Promise<CopyEnds |
|
|
|
37
37
|
const c = await loadRepoConfig(house)
|
|
38
38
|
const copy = c.serve?.spaceCopy
|
|
39
39
|
if (!copy) return { why: `the house config (${house}) names no serve.spaceCopy: the Space has no second place`, configured: false }
|
|
40
|
-
if (!c.credsReadCommand) return { why: `the house config (${house}) declares no credsReadCommand to read ${copy.secret}`, configured: true }
|
|
41
40
|
const read = async (name: string): Promise<string | { why: string }> => {
|
|
42
41
|
try {
|
|
43
|
-
return await readThrough(c.credsReadCommand
|
|
42
|
+
return await readThrough(c.credsReadCommand, name)
|
|
44
43
|
} catch (e) {
|
|
45
44
|
return { why: `the secret ${name}: ${(e as Error).message}` }
|
|
46
45
|
}
|
|
@@ -5,7 +5,7 @@ import { Database } from 'bun:sqlite'
|
|
|
5
5
|
import { mkdirSync } from 'node:fs'
|
|
6
6
|
import { dirname, join } from 'node:path'
|
|
7
7
|
import { homeDir } from '../../home/home-paths.ts'
|
|
8
|
-
import { ensureWal, migrateTo } from '../../ledger/db.ts'
|
|
8
|
+
import { ensureWal, migrateTo, storeSync } from '../../ledger/db.ts'
|
|
9
9
|
|
|
10
10
|
export const COPY_DB_ENV = 'SHIPKIT_SPACE_COPY_DB'
|
|
11
11
|
export const copyDbPath = (): string => process.env[COPY_DB_ENV] || join(homeDir(), 'space-copy.sqlite')
|
|
@@ -29,7 +29,7 @@ export function openCopyDb(path: string = copyDbPath()): Database {
|
|
|
29
29
|
const db = new Database(path, { create: true, readwrite: true })
|
|
30
30
|
db.exec('pragma busy_timeout = 3000')
|
|
31
31
|
ensureWal(db, 3000)
|
|
32
|
-
db
|
|
32
|
+
storeSync(db, 'NORMAL')
|
|
33
33
|
migrateTo(db, COPY_DB_VERSION, () => db.exec(V1))
|
|
34
34
|
return db
|
|
35
35
|
}
|
package/src/media-store/db.ts
CHANGED
|
@@ -8,7 +8,7 @@ import { mkdirSync } from 'node:fs'
|
|
|
8
8
|
import { dirname } from 'node:path'
|
|
9
9
|
import { foldRefusal, machineFileCreate, ownerOnlyFile, StoreRefused, storeHome } from '../core/machine-db.ts'
|
|
10
10
|
import { createRefusal } from '../core/machine-layout.ts'
|
|
11
|
-
import { ensureWal } from '../ledger/db.ts'
|
|
11
|
+
import { ensureWal, storeSync } from '../ledger/db.ts'
|
|
12
12
|
|
|
13
13
|
/** the file that holds the index's tables for one process (a suite, a scratch); else the machine db */
|
|
14
14
|
export const MEDIA_DB_ENV = 'SHIPKIT_MEDIA_DB'
|
|
@@ -52,7 +52,7 @@ export function mediaDb(): Database | null {
|
|
|
52
52
|
db.exec(`pragma busy_timeout = ${BUSY_MS}`)
|
|
53
53
|
machineFileCreate(path, db)
|
|
54
54
|
ensureWal(db, BUSY_MS)
|
|
55
|
-
db
|
|
55
|
+
storeSync(db, 'NORMAL')
|
|
56
56
|
db.exec(SCHEMA)
|
|
57
57
|
open = { path, db }
|
|
58
58
|
return db
|
|
@@ -53,6 +53,124 @@ export async function forgetForGood(keys: readonly string[], o: { owner: string
|
|
|
53
53
|
return { gone, kept }
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
+
/** the backup's own objects (media-store/backup/keys.ts): a run, a part, a file's bytes by its sha */
|
|
57
|
+
const isBackupObject = (key: string): boolean => /^backup\/(?:runs\/\d{8}T\d{6}Z-(?:[A-Za-z0-9-]+-)?[0-9a-f]{12}\.json\.gz|parts\/[0-9a-f]{64}\.json\.gz|[0-9a-f]{64}(?:\.[a-z0-9]{1,16})?)$/.test(key)
|
|
58
|
+
|
|
59
|
+
/** the backup's keys refused before any call: every one must be a backup object */
|
|
60
|
+
export function checkBackupKeys(keys: Iterable<string>): void {
|
|
61
|
+
for (const key of keys) if (!isBackupObject(key)) throw new Error(`${key} is no backup object: nothing is deleted`)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** the third way out: the backup's runs that no retention slot keeps, and the parts and bytes only they named. Its one
|
|
65
|
+
* caller is the prune (media-store/backup/prune.ts), which decides what goes. Every key is checked before any call;
|
|
66
|
+
* each key's index row goes before its object, so no run ever takes a deleted object for one still up. A plain
|
|
67
|
+
* delete: the bucket keeps its versions behind a delete marker, gone from every listing, until emptyBackupTrash
|
|
68
|
+
* removes them TRASH_DAYS later; until then deleting the marker brings the object back */
|
|
69
|
+
export async function forgetBackupObjects(keys: readonly string[], o: { jobs?: number; remove?: (key: string) => Promise<void> } = {}): Promise<{ gone: string[]; failed: { key: string; error: string }[] }> {
|
|
70
|
+
checkBackupKeys(keys)
|
|
71
|
+
if (keys.length === 0) return { gone: [], failed: [] }
|
|
72
|
+
let remove = o.remove
|
|
73
|
+
if (!remove) {
|
|
74
|
+
const { config, why } = await houseSpaceConfig()
|
|
75
|
+
if (!config) throw new Error(`nothing is deleted: ${why}`)
|
|
76
|
+
remove = async (key) => void (await signedDelete(config, key, {}))
|
|
77
|
+
}
|
|
78
|
+
const db = mediaDb()
|
|
79
|
+
if (!db) throw new Error('the media index does not open: nothing is deleted')
|
|
80
|
+
const unindex = db.query('delete from media_objects where key = ?')
|
|
81
|
+
const gone: string[] = []
|
|
82
|
+
const failed: { key: string; error: string }[] = []
|
|
83
|
+
let next = 0
|
|
84
|
+
const work = async (): Promise<void> => {
|
|
85
|
+
for (let i = next++; i < keys.length; i = next++) {
|
|
86
|
+
const key = keys[i]!
|
|
87
|
+
try {
|
|
88
|
+
unindex.run(key)
|
|
89
|
+
await remove(key)
|
|
90
|
+
gone.push(key)
|
|
91
|
+
} catch (e) {
|
|
92
|
+
failed.push({ key, error: (e as Error).message })
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
await Promise.all(Array.from({ length: Math.max(1, o.jobs ?? 8) }, work))
|
|
97
|
+
return { gone, failed }
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** how long a pruned backup object stays behind its delete marker, recoverable */
|
|
101
|
+
export const TRASH_DAYS = 7
|
|
102
|
+
|
|
103
|
+
/** the backup's trash emptied: under `backup/`, each key whose newest entry is a delete marker older than TRASH_DAYS
|
|
104
|
+
* loses the versions and the marker the listing named, by their ids, so a version put after the listing stays. Its
|
|
105
|
+
* one caller is the prune */
|
|
106
|
+
export async function emptyBackupTrash(o: { now?: number; jobs?: number } = {}): Promise<{ keys: number; bytes: number; failed: { key: string; error: string }[] }> {
|
|
107
|
+
const { config, why } = await houseSpaceConfig()
|
|
108
|
+
if (!config) throw new Error(`nothing is deleted: ${why}`)
|
|
109
|
+
const { eachVersion } = await import('./space-listing.ts')
|
|
110
|
+
const now = o.now ?? Date.now()
|
|
111
|
+
const byKey = new Map<string, { ids: string[]; bytes: number; trashed: boolean }>()
|
|
112
|
+
await eachVersion(config, { prefix: 'backup/' }, (v) => {
|
|
113
|
+
let k = byKey.get(v.key)
|
|
114
|
+
if (!k) {
|
|
115
|
+
k = { ids: [], bytes: 0, trashed: false }
|
|
116
|
+
byKey.set(v.key, k)
|
|
117
|
+
}
|
|
118
|
+
k.ids.push(v.versionId)
|
|
119
|
+
k.bytes += v.size
|
|
120
|
+
if (v.latest && v.marker && v.modified != null && now - v.modified > TRASH_DAYS * 86_400_000) k.trashed = true
|
|
121
|
+
})
|
|
122
|
+
const due = [...byKey].filter(([key, k]) => k.trashed && isBackupObject(key))
|
|
123
|
+
const failed: { key: string; error: string }[] = []
|
|
124
|
+
let keys = 0
|
|
125
|
+
let bytes = 0
|
|
126
|
+
let next = 0
|
|
127
|
+
const work = async (): Promise<void> => {
|
|
128
|
+
for (let i = next++; i < due.length; i = next++) {
|
|
129
|
+
const [key, k] = due[i]!
|
|
130
|
+
try {
|
|
131
|
+
for (const v of k.ids) if (v !== '') await signedDelete(config, key, { versionId: v })
|
|
132
|
+
keys++
|
|
133
|
+
bytes += k.bytes
|
|
134
|
+
} catch (e) {
|
|
135
|
+
failed.push({ key, error: (e as Error).message })
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
await Promise.all(Array.from({ length: Math.max(1, o.jobs ?? 8) }, work))
|
|
140
|
+
return { keys, bytes, failed }
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** the undo of a prune within TRASH_DAYS: under `backup/`, each key named (or every key deleted at or after `since`)
|
|
144
|
+
* whose newest entry is a delete marker loses that marker, so its newest version is the object again. Deletes only
|
|
145
|
+
* markers. Its one caller is `sk backup undelete` */
|
|
146
|
+
export async function undeleteBackup(o: { keys?: readonly string[]; since?: number }): Promise<{ back: string[]; failed: { key: string; error: string }[] }> {
|
|
147
|
+
if (o.keys) checkBackupKeys(o.keys)
|
|
148
|
+
if (!o.keys?.length && o.since == null) throw new Error('name the keys, or --since a time: nothing is undone')
|
|
149
|
+
const { config, why } = await houseSpaceConfig()
|
|
150
|
+
if (!config) throw new Error(`nothing is undone: ${why}`)
|
|
151
|
+
const { eachVersion } = await import('./space-listing.ts')
|
|
152
|
+
const want = o.keys ? new Set(o.keys) : null
|
|
153
|
+
const markers: { key: string; versionId: string }[] = []
|
|
154
|
+
const prefixes = want ? [...want] : ['backup/']
|
|
155
|
+
for (const prefix of prefixes)
|
|
156
|
+
await eachVersion(config, { prefix }, (v) => {
|
|
157
|
+
if (!v.latest || !v.marker || !isBackupObject(v.key) || (want && !want.has(v.key))) return
|
|
158
|
+
if (o.since != null && (v.modified == null || v.modified < o.since)) return
|
|
159
|
+
markers.push({ key: v.key, versionId: v.versionId })
|
|
160
|
+
})
|
|
161
|
+
const back: string[] = []
|
|
162
|
+
const failed: { key: string; error: string }[] = []
|
|
163
|
+
for (const m of markers) {
|
|
164
|
+
try {
|
|
165
|
+
await signedDelete(config, m.key, { versionId: m.versionId })
|
|
166
|
+
back.push(m.key)
|
|
167
|
+
} catch (e) {
|
|
168
|
+
failed.push({ key: m.key, error: (e as Error).message })
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
return { back, failed }
|
|
172
|
+
}
|
|
173
|
+
|
|
56
174
|
/** the vault's own sealed snapshots: the one folder the retention may empty */
|
|
57
175
|
export const VAULT_SNAPSHOTS_PREFIX = '_snapshots/vault/'
|
|
58
176
|
const isVaultSnapshot = (key: string): boolean => /^_snapshots\/vault\/(?!\.{1,2}\/)[a-z0-9._-]+\/\d{8}T\d{6}Z\.snap$/.test(key)
|
package/src/media-store/purge.ts
CHANGED
|
@@ -79,10 +79,11 @@ export interface PurgeResult {
|
|
|
79
79
|
/** free the disk of the candidates, oldest first, until `enough` says stop, `jobs` files at a time: each file waits
|
|
80
80
|
* on its HEAD and its kind's check, so one at a time spends the purge on round trips. A file some process holds open
|
|
81
81
|
* stays */
|
|
82
|
-
export async function purge(kind: MediaKind, dir: string, space: Space, list: readonly Candidate[], o: { enough?: () => boolean; say?: (l: string) => void; jobs?: number } = {}): Promise<PurgeResult> {
|
|
82
|
+
export async function purge(kind: MediaKind, dir: string, space: Space, list: readonly Candidate[], o: { enough?: () => boolean; say?: (l: string) => void; jobs?: number; openOf?: (paths: readonly string[]) => Set<string> } = {}): Promise<PurgeResult> {
|
|
83
83
|
const out: PurgeResult = { freed: 0, files: 0, refused: [], annexed: { files: 0, bytes: 0 } }
|
|
84
84
|
const store = resolve(dir)
|
|
85
|
-
|
|
85
|
+
// the paths some process holds open (`lsof`, about 600 ms a call, by default)
|
|
86
|
+
const open = (o.openOf ?? openOf)(list.map((c) => join(store, c.item, c.name)))
|
|
86
87
|
let said = Date.now()
|
|
87
88
|
let next = 0
|
|
88
89
|
const one = async (c: Candidate): Promise<void> => {
|