rvlib-shipkit 2.26.0 → 2.27.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 +20 -0
- package/package.json +1 -1
- package/src/agent-time/diet.ts +77 -0
- package/src/agent-time/family.ts +72 -0
- package/src/agent-time/hooks.ts +6 -4
- package/src/agent-time/limits.ts +44 -0
- package/src/agent-time/live.ts +1 -3
- package/src/agent-time/mask.ts +96 -18
- package/src/agent-time/refused.ts +45 -0
- package/src/agent-time/render.ts +122 -21
- package/src/agent-time/report-cache.ts +57 -0
- package/src/agent-time/report.ts +314 -40
- package/src/agent-time/shape.ts +50 -3
- package/src/agent-time/store.ts +320 -99
- package/src/agent-time/timeline.ts +140 -21
- package/src/agent-time/turns.ts +18 -8
- package/src/annex/offload.ts +17 -6
- package/src/annex/tidy.ts +70 -10
- package/src/asks/ask-after.ts +40 -0
- package/src/asks/asks-queue.ts +205 -47
- package/src/assets/kit-config.ts +7 -3
- package/src/assets/kits/identity-kit.ts +289 -10
- package/src/check.ts +3 -1
- package/src/checks/SK004-gitignore-policy.check.ts +12 -5
- package/src/checks/SK013-one-way-in-policy.check.ts +8 -3
- package/src/checks/SK026-asks-autoclose.check.ts +5 -5
- package/src/checks/SK028-readme.check.ts +7 -66
- package/src/checks/SK030-cache-audit.check.ts +6 -1
- package/src/checks/SK031-trace-audit.check.ts +2 -0
- package/src/checks/SK033-global-contribution.check.ts +3 -1
- package/src/checks/SK035-public-clean.check.ts +2 -2
- package/src/checks/SK043-scratch-on-the-mac.check.ts +59 -4
- package/src/checks/SK047-login-in-description.check.ts +36 -0
- package/src/checks/SK048-one-local-file.check.ts +36 -0
- package/src/cli/cmd_Asks.ts +71 -5
- package/src/cli/cmd_Backup.ts +2 -2
- package/src/cli/cmd_Capacity.ts +2 -2
- package/src/cli/cmd_CrossRepoActivity.ts +5 -4
- package/src/cli/cmd_Db.ts +253 -0
- package/src/cli/cmd_History.ts +212 -0
- package/src/cli/cmd_Home.ts +31 -3
- package/src/cli/cmd_Hq.ts +516 -0
- package/src/cli/cmd_Machines.ts +36 -0
- package/src/cli/cmd_Media.ts +38 -6
- package/src/cli/cmd_Reflect.ts +23 -2
- package/src/cli/cmd_Review.ts +548 -116
- package/src/cli/cmd_Secret.ts +62 -12
- package/src/cli/cmd_Social.ts +83 -1
- package/src/cli/cmd_Space.ts +324 -0
- package/src/cli/cmd_Time.ts +76 -5
- package/src/cli/cmd_Tmp.ts +28 -4
- package/src/cli/cmd_Trailer.ts +14 -10
- package/src/cli/cmd_Urls.ts +56 -0
- package/src/cli/cmd_Vault.ts +583 -0
- package/src/cli/family-help.ts +4 -2
- package/src/cli/family-pages.ts +1 -1
- package/src/cli/registry.ts +24 -3
- package/src/collections/actions.ts +63 -13
- package/src/collections/collection-types.ts +23 -1
- package/src/collections/providers.ts +117 -23
- package/src/collections/review.ts +4 -4
- package/src/contracts/contract-manifests.ts +6 -5
- package/src/contracts/contract-types.ts +6 -4
- package/src/contracts/contracts-engine.ts +11 -0
- package/src/contracts/manifests/chrome-web-store.contract.ts +31 -0
- package/src/core/budget.ts +12 -17
- package/src/core/capacity.ts +27 -18
- package/src/core/check-runner.ts +7 -4
- package/src/core/layout.ts +3 -0
- package/src/core/machine-busy.ts +127 -0
- package/src/core/machine-commit.ts +6 -1
- package/src/core/machine-db.ts +368 -20
- package/src/core/machine-layout.ts +137 -0
- package/src/core/machine-read.ts +81 -0
- package/src/core/migrate-layout.ts +1 -1
- package/src/core/one-way-in-policy.ts +64 -7
- package/src/core/polling-policy.ts +27 -0
- package/src/core/retention.ts +16 -2
- package/src/core/scope.ts +107 -14
- package/src/core/slots.ts +5 -4
- package/src/drill/clean.ts +25 -1
- package/src/drill/run.ts +1 -1
- package/src/drill/seal.ts +27 -4
- package/src/home/activity-shape.ts +2 -0
- package/src/home/activity.ts +66 -3
- package/src/home/app-sign.ts +15 -4
- package/src/home/daemon-jobs.ts +149 -7
- package/src/home/database-projects.ts +111 -0
- package/src/home/database-words.ts +203 -0
- package/src/home/databases.ts +460 -0
- package/src/home/global-units.ts +6 -0
- package/src/home/gui-app/ShipkitGui.swift +122 -1
- package/src/home/gui-app.ts +6 -3
- package/src/home/home-move.ts +514 -0
- package/src/home/home-paths.ts +12 -3
- package/src/home/home-routes.ts +68 -0
- package/src/home/inbox-shape.ts +24 -14
- package/src/home/machine-fold.ts +820 -0
- package/src/home/machine-host.ts +20 -5
- package/src/home/machine-repair.ts +503 -0
- package/src/home/machine-stores.ts +66 -15
- package/src/home/machine-temp.ts +547 -86
- package/src/home/resume.ts +79 -0
- package/src/home/run-daemon.ts +25 -4
- package/src/home/run-pm.ts +18 -5
- package/src/home/serve-daemon.ts +18 -3
- package/src/home/serve-web.ts +175 -7
- package/src/home/space-job.ts +115 -0
- package/src/home/space-page.ts +257 -0
- package/src/home/space-route.ts +167 -0
- package/src/home/space-words.ts +103 -0
- package/src/home/time-page-shape.ts +202 -0
- package/src/home/time-page.ts +64 -0
- package/src/home/vault-page.ts +482 -0
- package/src/home/vault-words.ts +132 -0
- package/src/hooks/leak-guard.ts +40 -13
- package/src/hooks/one-way-in-guard.ts +17 -4
- package/src/hq/cards.ts +581 -0
- package/src/hq/door-route.ts +43 -0
- package/src/hq/door.ts +163 -0
- package/src/hq/files-send.ts +348 -0
- package/src/hq/forgotten-log.ts +35 -0
- package/src/hq/history-check.ts +255 -0
- package/src/hq/history-place.ts +146 -0
- package/src/hq/history-send.ts +487 -0
- package/src/hq/hq-db.ts +99 -0
- package/src/hq/hq-local.ts +469 -0
- package/src/hq/hq-routes.ts +196 -0
- package/src/hq/hq-store.ts +599 -0
- package/src/hq/kinds/ask.ts +147 -0
- package/src/hq/kinds/card-gone.ts +83 -0
- package/src/hq/kinds/card-msg.ts +66 -0
- package/src/hq/kinds/card.ts +99 -0
- package/src/hq/kinds/index.ts +21 -0
- package/src/hq/kinds/kind.ts +126 -0
- package/src/hq/kinds/media.ts +125 -0
- package/src/hq/kinds/prompt.ts +219 -0
- package/src/hq/kinds/reflection.ts +128 -0
- package/src/hq/kinds/report.ts +55 -0
- package/src/hq/kinds/time-day.ts +236 -0
- package/src/hq/ledger.ts +473 -0
- package/src/hq/machine-key.ts +44 -0
- package/src/hq/machine-notes.ts +131 -0
- package/src/hq/outbox.ts +372 -0
- package/src/hq/replica.ts +168 -0
- package/src/hq/repo-id.ts +4 -0
- package/src/hq/report-facts.ts +117 -0
- package/src/hq/report.ts +274 -0
- package/src/hq/switch.ts +151 -0
- package/src/hq/vault-grant.ts +92 -0
- package/src/identities/identities.ts +15 -3
- package/src/identities/steps.ts +31 -0
- package/src/ledger/db.ts +73 -26
- package/src/ledger/gate-checks.ts +6 -2
- package/src/ledger/run-log.ts +11 -3
- package/src/ledger/store.ts +26 -4
- package/src/media-store/backup/daemon.ts +25 -17
- package/src/media-store/backup/kept.ts +241 -0
- package/src/media-store/backup/restore.ts +14 -4
- package/src/media-store/backup/run.ts +11 -11
- package/src/media-store/backup/secret.ts +6 -4
- package/src/media-store/backup/snapshot.ts +4 -2
- package/src/media-store/backup/sources.ts +67 -16
- package/src/media-store/copy/config.ts +77 -0
- package/src/media-store/copy/copy.ts +194 -0
- package/src/media-store/copy/lifecycle.ts +43 -0
- package/src/media-store/copy/night.ts +90 -0
- package/src/media-store/copy/proof.ts +151 -0
- package/src/media-store/copy/snapshot.ts +152 -0
- package/src/media-store/copy/store.ts +62 -0
- package/src/media-store/copy/stream.ts +176 -0
- package/src/media-store/daemon.ts +66 -16
- package/src/media-store/db.ts +22 -11
- package/src/media-store/forget.ts +23 -3
- package/src/media-store/hand.ts +4 -3
- package/src/media-store/hq-find.ts +75 -0
- package/src/media-store/hq-send.ts +277 -0
- package/src/media-store/kind.ts +2 -0
- package/src/media-store/purge.ts +18 -15
- package/src/media-store/put.ts +24 -8
- package/src/media-store/remote.ts +16 -7
- package/src/media-store/space-listing.ts +240 -0
- package/src/media-store/space.ts +40 -21
- package/src/media-store/sync.ts +5 -5
- package/src/media-store/tidy.ts +98 -0
- package/src/media-store/upload.ts +3 -3
- package/src/media-store/webp.ts +66 -0
- package/src/messages/model.ts +16 -7
- package/src/messages/routes.ts +14 -4
- package/src/messages/score.ts +2 -2
- package/src/messages/suggest.ts +96 -0
- package/src/pages/frame-client.ts +10 -0
- package/src/plan/planner.ts +28 -17
- package/src/recordings/media-kind.ts +1 -0
- package/src/recordings/model.ts +1 -1
- package/src/recordings/recorder-app/Capture.swift +9 -7
- package/src/reflect/hq-corpus.ts +56 -0
- package/src/reflect/prompt-corpus.ts +3 -1
- package/src/reflect/transcript.ts +129 -37
- package/src/release/machine-words.ts +67 -0
- package/src/release/public-clean.ts +8 -3
- package/src/releases/from-card.ts +1 -1
- package/src/releases/routes.ts +10 -5
- package/src/releases/store.ts +10 -1
- package/src/repo-config.ts +32 -2
- package/src/review/anim.ts +2 -2
- package/src/review/auto.ts +2 -2
- package/src/review/card-db.ts +244 -0
- package/src/review/card-rows.ts +266 -0
- package/src/review/cells.ts +76 -5
- package/src/review/draft-card.ts +41 -0
- package/src/review/draft-model.ts +78 -0
- package/src/review/format.ts +29 -10
- package/src/review/gone.ts +98 -0
- package/src/review/inbox-hook.ts +4 -4
- package/src/review/marks.ts +1 -1
- package/src/review/media-kind.ts +7 -5
- package/src/review/media-read.ts +9 -7
- package/src/review/media.ts +32 -5
- package/src/review/model.ts +202 -39
- package/src/review/move.ts +491 -0
- package/src/review/notes.ts +12 -20
- package/src/review/previews.ts +15 -19
- package/src/review/project.ts +1 -1
- package/src/review/rev.ts +8 -0
- package/src/review/role.ts +1 -1
- package/src/review/routes.ts +64 -13
- package/src/review/stage-card.ts +8 -4
- package/src/review/stage-routes.ts +1 -1
- package/src/review/store.ts +529 -83
- package/src/review/thread-fold.ts +51 -0
- package/src/review/threads.ts +147 -9
- package/src/review/trailer-auto-mock.ts +63 -0
- package/src/review/trailer-card.ts +4 -4
- package/src/review/trailer-desk.ts +41 -7
- package/src/review/trailer-media.ts +1 -0
- package/src/review/trailer-model.ts +8 -0
- package/src/review/trailer-notes.ts +1 -1
- package/src/review/trailer-routes.ts +9 -1
- package/src/review/unit.ts +6 -6
- package/src/review/watchers.ts +1 -1
- package/src/rules/untrack-generated.ts +92 -8
- package/src/secrets/held.ts +16 -0
- package/src/secrets/keychain.ts +113 -0
- package/src/secrets/list.ts +16 -6
- package/src/secrets/manager.ts +24 -10
- package/src/secrets/read.ts +257 -0
- package/src/secrets/who-reads.ts +49 -0
- package/src/serve/door-forward.ts +268 -0
- package/src/serve/door-names.ts +47 -0
- package/src/serve/embed.ts +1 -1
- package/src/serve/hub-app/activity-view.tsx +56 -32
- package/src/serve/hub-app/activity.css +7 -1
- package/src/serve/hub-app/asks-page.tsx +48 -0
- package/src/serve/hub-app/asks-st.ts +25 -0
- package/src/serve/hub-app/clock-svc.ts +1 -2
- package/src/serve/hub-app/col-card.tsx +64 -13
- package/src/serve/hub-app/col-list-page.tsx +122 -0
- package/src/serve/hub-app/col-list-st.ts +103 -0
- package/src/serve/hub-app/col-tables-view.tsx +10 -41
- package/src/serve/hub-app/col-tables.ts +76 -8
- package/src/serve/hub-app/cols-svc.ts +70 -16
- package/src/serve/hub-app/database-st.ts +260 -0
- package/src/serve/hub-app/database-view.tsx +289 -0
- package/src/serve/hub-app/database.css +32 -0
- package/src/serve/hub-app/entry-bar.css +14 -0
- package/src/serve/hub-app/entry-bar.tsx +33 -0
- package/src/serve/hub-app/entry-pick.ts +31 -0
- package/src/serve/hub-app/floating.tsx +1 -0
- package/src/serve/hub-app/hash-route.ts +11 -1
- package/src/serve/hub-app/header-bar.tsx +14 -18
- package/src/serve/hub-app/home-st.ts +89 -9
- package/src/serve/hub-app/home-strip.css +5 -19
- package/src/serve/hub-app/home-strip.tsx +58 -92
- package/src/serve/hub-app/home.html +7 -0
- package/src/serve/hub-app/hub-app.tsx +31 -142
- package/src/serve/hub-app/hub-types.ts +3 -1
- package/src/serve/hub-app/hub.css +109 -23
- package/src/serve/hub-app/icons.tsx +18 -1
- package/src/serve/hub-app/identities-st.ts +56 -6
- package/src/serve/hub-app/identities-view.tsx +100 -58
- package/src/serve/hub-app/identities.css +17 -15
- package/src/serve/hub-app/identity-address.ts +5 -0
- package/src/serve/hub-app/inbox-st.ts +41 -24
- package/src/serve/hub-app/inbox-view.tsx +37 -41
- package/src/serve/hub-app/inbox.css +2 -5
- package/src/serve/hub-app/index.html +7 -0
- package/src/serve/hub-app/kept-cols.tsx +7 -0
- package/src/serve/hub-app/keys.ts +28 -15
- package/src/serve/hub-app/later.tsx +26 -0
- package/src/serve/hub-app/list-filter.tsx +14 -0
- package/src/serve/hub-app/list-walk.ts +72 -3
- package/src/serve/hub-app/machines-st.ts +251 -0
- package/src/serve/hub-app/machines-view.tsx +224 -0
- package/src/serve/hub-app/machines.css +76 -0
- package/src/serve/hub-app/md-copy.tsx +79 -0
- package/src/serve/hub-app/media-view.tsx +2 -0
- package/src/serve/hub-app/messages/messages-page.tsx +79 -7
- package/src/serve/hub-app/messages/messages-st.ts +99 -3
- package/src/serve/hub-app/messages.css +14 -0
- package/src/serve/hub-app/page-keys.ts +175 -0
- package/src/serve/hub-app/pages-frames.tsx +11 -3
- package/src/serve/hub-app/palette-st.ts +58 -11
- package/src/serve/hub-app/palette-view.tsx +3 -1
- package/src/serve/hub-app/project-list.css +54 -8
- package/src/serve/hub-app/project-list.tsx +128 -59
- package/src/serve/hub-app/projects-svc.ts +78 -7
- package/src/serve/hub-app/reader-chips.tsx +13 -0
- package/src/serve/hub-app/readme-view.tsx +2 -3
- package/src/serve/hub-app/readme.css +27 -5
- package/src/serve/hub-app/reflections-page.tsx +42 -0
- package/src/serve/hub-app/reflections-st.ts +17 -0
- package/src/serve/hub-app/releases/releases-page.tsx +25 -2
- package/src/serve/hub-app/releases/releases-st.ts +1 -1
- package/src/serve/hub-app/repo-sections.ts +9 -5
- package/src/serve/hub-app/repo-st.ts +35 -8
- package/src/serve/hub-app/review/draft-card.tsx +163 -0
- package/src/serve/hub-app/review/kit.tsx +50 -7
- package/src/serve/hub-app/review/note-place.ts +1 -1
- package/src/serve/hub-app/review/review-board.tsx +2 -2
- package/src/serve/hub-app/review/review-cells.tsx +145 -65
- package/src/serve/hub-app/review/review-kit.ts +27 -1
- package/src/serve/hub-app/review/review-list.tsx +68 -30
- package/src/serve/hub-app/review/review-page.tsx +7 -8
- package/src/serve/hub-app/review/review-st.ts +111 -24
- package/src/serve/hub-app/review/review-thread.tsx +49 -19
- package/src/serve/hub-app/review/review-views.tsx +475 -144
- package/src/serve/hub-app/review/thread-control-st.ts +115 -0
- package/src/serve/hub-app/review/thread-control.tsx +74 -0
- package/src/serve/hub-app/review/thread-st.ts +73 -26
- package/src/serve/hub-app/review/trailer-card.tsx +1 -1
- package/src/serve/hub-app/review/trailer-page.tsx +37 -16
- package/src/serve/hub-app/review/trailer-st.ts +41 -7
- package/src/serve/hub-app/review.css +267 -61
- package/src/serve/hub-app/side-lists.ts +72 -0
- package/src/serve/hub-app/sidebar.tsx +6 -3
- package/src/serve/hub-app/site/site-part.tsx +2 -8
- package/src/serve/hub-app/site/site-st.ts +4 -4
- package/src/serve/hub-app/social/comms-page.tsx +20 -5
- package/src/serve/hub-app/social/kit.tsx +30 -4
- package/src/serve/hub-app/social/post-card.tsx +139 -49
- package/src/serve/hub-app/social/serial-writes.ts +13 -0
- package/src/serve/hub-app/social/social-model.ts +11 -0
- package/src/serve/hub-app/social/social-page.tsx +94 -35
- package/src/serve/hub-app/social/social-st.ts +120 -36
- package/src/serve/hub-app/social/stage-popup.tsx +1 -1
- package/src/serve/hub-app/social/stage-st.ts +1 -1
- package/src/serve/hub-app/social/step-layout.ts +1 -1
- package/src/serve/hub-app/social.css +3 -0
- package/src/serve/hub-app/space-st.ts +209 -0
- package/src/serve/hub-app/space-view.tsx +196 -0
- package/src/serve/hub-app/space.css +27 -0
- package/src/serve/hub-app/stores/stores-page.tsx +91 -23
- package/src/serve/hub-app/stores/stores-st.ts +44 -19
- package/src/serve/hub-app/stores.css +6 -1
- package/src/serve/hub-app/theme-st.ts +22 -9
- package/src/serve/hub-app/time-charts.tsx +291 -0
- package/src/serve/hub-app/time-pages.tsx +749 -0
- package/src/serve/hub-app/time-st.ts +162 -0
- package/src/serve/hub-app/time-view.tsx +76 -0
- package/src/serve/hub-app/time.css +138 -0
- package/src/serve/hub-app/tree-build.ts +52 -42
- package/src/serve/hub-app/tree-row.tsx +4 -4
- package/src/serve/hub-app/tree-svc.ts +18 -9
- package/src/serve/hub-app/urls-svc.ts +154 -0
- package/src/serve/hub-app/urls-view.tsx +117 -0
- package/src/serve/hub-app/urls.css +30 -0
- package/src/serve/hub-app/vault-st.ts +127 -0
- package/src/serve/hub-app/vault-view.tsx +154 -0
- package/src/serve/hub-app/vault.css +38 -0
- package/src/serve/hub-app/veille/veille-st.ts +12 -10
- package/src/serve/hub-app/waiting-svc.ts +29 -0
- package/src/serve/hub-app/wikis-view.tsx +2 -2
- package/src/serve/hub-runtime.ts +15 -2
- package/src/serve/project-gutter-route.ts +27 -14
- package/src/serve/project-gutter.ts +25 -12
- package/src/serve/same-origin.ts +10 -0
- package/src/serve/serve-hub.ts +13 -4
- package/src/shipkit-files.ts +16 -2
- package/src/shipkit.ts +1 -1
- package/src/shot/shooter.ts +616 -420
- package/src/shot/shot-ask.ts +246 -155
- package/src/site/desk.ts +2 -2
- package/src/smoke/run-smoke.ts +3 -2
- package/src/social/cover.ts +202 -0
- package/src/social/discord-layout.ts +37 -13
- package/src/social/drafts.ts +3 -0
- package/src/social/load.ts +23 -2
- package/src/social/media-kind.ts +2 -1
- package/src/social/publish/discord.ts +5 -2
- package/src/social/publish/secret.ts +2 -2
- package/src/social/release-stage.ts +28 -13
- package/src/social/routes.ts +26 -1
- package/src/social/stage.ts +1 -1
- package/src/social/stats.ts +12 -9
- package/src/social/strip.ts +4 -1
- package/src/social/thumbnail.ts +255 -0
- package/src/stores/collection.ts +15 -11
- package/src/stores/desk.ts +236 -42
- package/src/stores/model.ts +72 -6
- package/src/stores/routes.ts +37 -12
- package/src/stores/unit.ts +14 -8
- package/src/suite/judge-suites.ts +41 -11
- package/src/suite/select.ts +45 -3
- package/src/suite/suite-cli.ts +16 -10
- package/src/trailer/bed.ts +1 -1
- package/src/trailer/build.ts +134 -21
- package/src/trailer/ffmpeg.ts +7 -0
- package/src/trailer/fonts.ts +3 -2
- package/src/trailer/grammar.ts +201 -2
- package/src/trailer/graph.ts +42 -5
- package/src/trailer/look.ts +21 -0
- package/src/trailer/mock.ts +154 -0
- package/src/trailer/passage.ts +65 -26
- package/src/trailer/score/heard.ts +89 -0
- package/src/trailer/score/sound.ts +14 -8
- package/src/trailer/sound/run.ts +16 -6
- package/src/trailer/stitch.ts +26 -5
- package/src/trailer/titles.ts +82 -19
- package/src/trailer/versions.ts +1 -3
- package/src/trailer/vertical.ts +3 -1
- package/src/urls/frame.ts +195 -0
- package/src/urls/routes.ts +43 -0
- package/src/urls/rows.ts +171 -0
- package/src/urls/unit.ts +73 -0
- package/src/vault/house-keys.ts +42 -0
- package/src/vault/layout.ts +50 -0
- package/src/vault/policy.ts +165 -0
- package/src/vault/vault-audit.ts +276 -0
- package/src/vault/vault-client.ts +40 -0
- package/src/vault/vault-config.ts +159 -0
- package/src/vault/vault-destroy.ts +78 -0
- package/src/vault/vault-handover.ts +305 -0
- package/src/vault/vault-http.ts +352 -0
- package/src/vault/vault-import.ts +208 -0
- package/src/vault/vault-init.ts +328 -0
- package/src/vault/vault-minter.ts +271 -0
- package/src/vault/vault-restore.ts +333 -0
- package/src/vault/vault-serve.ts +342 -0
- package/src/vault/vault-snapshot.ts +281 -0
- package/src/veille/asks.ts +1 -1
- package/src/veille/desk.ts +2 -2
package/src/home/machine-temp.ts
CHANGED
|
@@ -1,17 +1,21 @@
|
|
|
1
1
|
// the machine's temp folders: claude code's per session scratch (`<tmp>/claude-<uid>/<project>/<session>/`), the
|
|
2
|
-
// user's temp dir and /tmp
|
|
3
|
-
// retention sweep (machine-stores.ts) deletes each unit nothing wrote for the guard hours
|
|
4
|
-
// and
|
|
2
|
+
// user's temp dir, /tmp, and each house repo's `.shipkit/tmp`. Nothing else trims them, and every agent and test run
|
|
3
|
+
// writes there. A pass of the retention sweep (machine-stores.ts) deletes each unit nothing wrote for the guard hours
|
|
4
|
+
// and no live process uses (a big unit of a repo moves to the annex first, when one is plugged in), and keeps what it
|
|
5
|
+
// measured, so `sk tmp` answers with no walk.
|
|
5
6
|
//
|
|
6
7
|
// the one constraint a reader breaks first: this deletes for real. Nothing outside a checked root, no link followed
|
|
7
8
|
// (lstat everywhere, a link is removed as a link), nothing another user owns, nothing a live process holds, no git
|
|
8
9
|
// work a remote does not hold: with no reading of the live processes the pass deletes nothing.
|
|
9
10
|
import { closeSync, existsSync, lstatSync, mkdirSync, openSync, readdirSync, readFileSync, realpathSync, rmSync, statfsSync, writeSync, type Stats } from 'node:fs'
|
|
10
11
|
import { homedir } from 'node:os'
|
|
11
|
-
import { basename, dirname, join, sep } from 'node:path'
|
|
12
|
+
import { basename, dirname, isAbsolute, join, relative, sep } from 'node:path'
|
|
12
13
|
import { gitBin } from '../core/git-bin.ts'
|
|
14
|
+
import { JOBS } from '../core/jobs.ts'
|
|
15
|
+
import { CI_LOG_DIR, LAYOUT } from '../core/layout.ts'
|
|
13
16
|
import { machineDbPath, machineGet, machineSet } from '../core/machine-db.ts'
|
|
14
17
|
import { removeTree } from '../core/remove-tree.ts'
|
|
18
|
+
import type { Probe } from '../annex/offload.ts'
|
|
15
19
|
|
|
16
20
|
/** a unit written this recently is never touched while the disk has room */
|
|
17
21
|
export const TEMP_GUARD_MS = 24 * 60 * 60 * 1000
|
|
@@ -43,8 +47,45 @@ export function setTempAuto(on: boolean): boolean {
|
|
|
43
47
|
/** since when the switch is on */
|
|
44
48
|
export const tempAutoAt = (): number | null => machineGet<number>(AUTO_AT)
|
|
45
49
|
|
|
46
|
-
|
|
47
|
-
|
|
50
|
+
export const REPOS_FROM = 'temp-repos-from'
|
|
51
|
+
/** a start date is set this many days ahead at the least: SK043 tells each repo what would go before a file of it goes */
|
|
52
|
+
export const REPOS_NOTICE_DAYS = 3
|
|
53
|
+
const DAY_RE = /^(\d{4})-(\d{2})-(\d{2})$/
|
|
54
|
+
/** the local midnight of a day written `YYYY-MM-DD`, or null for anything else (a number, `true`, a day that does not
|
|
55
|
+
* exist) */
|
|
56
|
+
export function dayAt(day: unknown): number | null {
|
|
57
|
+
if (typeof day !== 'string') return null
|
|
58
|
+
const m = DAY_RE.exec(day)
|
|
59
|
+
if (m == null) return null
|
|
60
|
+
const at = new Date(Number(m[1]), Number(m[2]) - 1, Number(m[3])).getTime()
|
|
61
|
+
return dayOf(at) === day ? at : null
|
|
62
|
+
}
|
|
63
|
+
/** from when the pass deletes in the repos' `.shipkit/tmp`; before it, with none set, or with anything stored that is
|
|
64
|
+
* no well formed day, it only counts and names what would go */
|
|
65
|
+
export const reposFrom = (): number | null => dayAt(machineGet<unknown>(REPOS_FROM))
|
|
66
|
+
/** the start date set: a day REPOS_NOTICE_DAYS after today at the least, or null to clear it; why not, or null once set */
|
|
67
|
+
export function setReposFrom(day: string | null, now = Date.now()): string | null {
|
|
68
|
+
if (day != null) {
|
|
69
|
+
const at = dayAt(day)
|
|
70
|
+
if (at == null) return `${day} is no day: YYYY-MM-DD`
|
|
71
|
+
const today = new Date(now)
|
|
72
|
+
const earliest = new Date(today.getFullYear(), today.getMonth(), today.getDate() + REPOS_NOTICE_DAYS).getTime()
|
|
73
|
+
if (at < earliest) return `${day} is too soon: the earliest start date is ${dayOf(earliest)}, so each repo is told for ${REPOS_NOTICE_DAYS} days what would go`
|
|
74
|
+
}
|
|
75
|
+
return machineSet(REPOS_FROM, day) ? null : 'the machine db does not open: the date is not set'
|
|
76
|
+
}
|
|
77
|
+
/** the repo roots delete at `now`: their start date passed */
|
|
78
|
+
export const reposDelete = (now: number): boolean => {
|
|
79
|
+
const from = reposFrom()
|
|
80
|
+
return from != null && now >= from
|
|
81
|
+
}
|
|
82
|
+
/** the sweep owns the repos' `.shipkit/tmp` now: the owner's switch is on and the start date passed (annex/tidy.ts
|
|
83
|
+
* then leaves it) */
|
|
84
|
+
export const repoSweepOn = (now: number): boolean => tempAuto() && reposDelete(now)
|
|
85
|
+
|
|
86
|
+
/** sessions: claude's `<project>/<session>/<folder>/<piece>` tree; entries: each child is a unit; repo: a house repo's
|
|
87
|
+
* `.shipkit/tmp`, each child a unit */
|
|
88
|
+
export type RootKind = 'sessions' | 'entries' | 'repo'
|
|
48
89
|
export type TempRoot = {
|
|
49
90
|
id: string
|
|
50
91
|
path: string
|
|
@@ -52,6 +93,43 @@ export type TempRoot = {
|
|
|
52
93
|
capBytes: number
|
|
53
94
|
/** set on a root the system and apps share: a child with no temp shaped name is a unit only at this size or more */
|
|
54
95
|
fixedNameMinBytes?: number
|
|
96
|
+
/** a repo root: the repo as the house lists it, the annex records' own form */
|
|
97
|
+
repo?: string
|
|
98
|
+
/** a repo root: the folder its annex config declares that keeps a repo relative path, or null (annex/tidy.ts) */
|
|
99
|
+
declared?: (rel: string) => string | null
|
|
100
|
+
/** a repo root: the first database or lock a unit holds, as the annex names one (annex/offload.ts), or null */
|
|
101
|
+
db?: (path: string) => string | null
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** children of a repo's `.shipkit/tmp` another sweep owns: the gate's run logs and the detached jobs (core/retention.ts),
|
|
105
|
+
* the commands page's runs (commands/runs.ts, swept as jobs) */
|
|
106
|
+
export const REPO_OWNED: ReadonlySet<string> = new Set([basename(CI_LOG_DIR), basename(JOBS), 'cli-runs'])
|
|
107
|
+
/** an idle unit of a repo this big moves to the annex when one has room; with none, it waits BIG_WAIT_MS */
|
|
108
|
+
export const MOVE_MIN_BYTES = 100e6
|
|
109
|
+
/** a big unit no annex takes is deleted once idle this long: a drive unplugged tonight is no reason to lose it */
|
|
110
|
+
export const BIG_WAIT_MS = 7 * 24 * 60 * 60 * 1000
|
|
111
|
+
|
|
112
|
+
/** the `.shipkit/tmp` roots of the repos named, each an absolute folder holding a `.git` folder (no linked worktree),
|
|
113
|
+
* never `/`, the home folder or a folder above it, nor a folder inside a `.shipkit`, one root a repo, where neither
|
|
114
|
+
* `.shipkit` nor its `tmp` is a link (a `.shipkit/tmp` behind a link is the annex's). The caller hands the house repos */
|
|
115
|
+
export function repoTempRoots(repos: readonly unknown[], home = homedir()): TempRoot[] {
|
|
116
|
+
const out: TempRoot[] = []
|
|
117
|
+
const seen = new Set<string>()
|
|
118
|
+
for (const repo of repos) {
|
|
119
|
+
// a control character, a `..`, a repo inside a repo's scratch: never a house repo
|
|
120
|
+
if (typeof repo !== 'string' || !isAbsolute(repo) || /[\x00-\x1f]/.test(repo) || repo.split(/[\\/]/).includes('..')) continue
|
|
121
|
+
let real: string
|
|
122
|
+
try { real = realpathSync(repo) } catch { continue }
|
|
123
|
+
if (real === sep || real === home || home.startsWith(real + sep) || seen.has(real) || real.split(sep).includes('.shipkit')) continue
|
|
124
|
+
// a linked worktree's `.git` is a file: its scratch is its checkout's lane, never a house repo
|
|
125
|
+
if (stat(join(real, '.git'))?.isDirectory() !== true) continue
|
|
126
|
+
const dot = stat(join(real, '.shipkit'))
|
|
127
|
+
const tmp = stat(join(real, LAYOUT.tmp))
|
|
128
|
+
if (dot == null || !dot.isDirectory() || tmp == null || !tmp.isDirectory()) continue
|
|
129
|
+
seen.add(real)
|
|
130
|
+
out.push({ id: `repo ${basename(real)}`, path: join(real, LAYOUT.tmp), kind: 'repo', capBytes: 0, repo })
|
|
131
|
+
}
|
|
132
|
+
return out
|
|
55
133
|
}
|
|
56
134
|
|
|
57
135
|
/** the user's temp dir as the system names it, never as `$TMPDIR` says: a caller's env must not pick what is swept */
|
|
@@ -81,10 +159,12 @@ export function tempRoots(env: Record<string, string | undefined> = process.env,
|
|
|
81
159
|
})
|
|
82
160
|
}
|
|
83
161
|
|
|
84
|
-
/** the root, resolved once and checked; null when it does not exist; throws on any
|
|
162
|
+
/** the root, resolved once and checked; null when it does not exist (a repo's: or sits behind a link); throws on any
|
|
163
|
+
* other shape */
|
|
85
164
|
export function checkedRoot(root: string, kind: RootKind = 'sessions', uid = process.getuid?.()): string | null {
|
|
86
165
|
if (uid == null) throw new Error('no uid on this platform: the temp pass does not run')
|
|
87
166
|
if (root.split(/[\\/]/).includes('..')) throw new Error(`${root}: a path with .. is refused`)
|
|
167
|
+
if (kind === 'repo') return checkedRepoRoot(root, uid)
|
|
88
168
|
let real: string
|
|
89
169
|
try { real = realpathSync(root) } catch (e) {
|
|
90
170
|
if ((e as { code?: string }).code === 'ENOENT') return null
|
|
@@ -99,12 +179,29 @@ export function checkedRoot(root: string, kind: RootKind = 'sessions', uid = pro
|
|
|
99
179
|
return real
|
|
100
180
|
}
|
|
101
181
|
|
|
182
|
+
/** a repo's `.shipkit/tmp`: named so, a real folder of the user's under a real `.shipkit` of a folder holding `.git` */
|
|
183
|
+
function checkedRepoRoot(root: string, uid: number): string | null {
|
|
184
|
+
if (!isAbsolute(root) || basename(root) !== 'tmp' || basename(dirname(root)) !== '.shipkit') throw new Error(`${root}: not a repo's .shipkit/tmp`)
|
|
185
|
+
const tmp = stat(root)
|
|
186
|
+
const dot = stat(dirname(root))
|
|
187
|
+
if (tmp == null || dot == null) return null
|
|
188
|
+
if (tmp.isSymbolicLink() || dot.isSymbolicLink()) return null
|
|
189
|
+
if (!tmp.isDirectory() || !dot.isDirectory()) throw new Error(`${root}: not a folder`)
|
|
190
|
+
const real = realpathSync(root)
|
|
191
|
+
const repo = dirname(dirname(real))
|
|
192
|
+
if (repo === sep || repo === homedir() || homedir().startsWith(repo + sep)) throw new Error(`${real}: not a temp folder of its own`)
|
|
193
|
+
if (stat(join(repo, '.git')) == null) throw new Error(`${real}: its folder holds no .git, so it is no repo`)
|
|
194
|
+
if (tmp.uid !== uid) throw new Error(`${real}: owned by uid ${tmp.uid}, not ${uid}`)
|
|
195
|
+
return real
|
|
196
|
+
}
|
|
197
|
+
|
|
102
198
|
export type Unit = { path: string; at: number; bytes: number; files: number }
|
|
103
199
|
/** a unit that stays, and why; `floor`: its walk stopped, the size is a least */
|
|
104
|
-
export type Kept = Unit & { why: string; floor?: boolean }
|
|
105
|
-
/** an idle unit, with every folder of it that holds a `.git
|
|
106
|
-
type Idle = Unit & { gits: string[] }
|
|
107
|
-
|
|
200
|
+
export type Kept = Unit & { why: string; floor?: boolean; live?: boolean }
|
|
201
|
+
/** an idle unit, with every folder of it that holds a `.git`, and whether it is a link itself */
|
|
202
|
+
type Idle = Unit & { gits: string[]; link?: boolean }
|
|
203
|
+
/** the units of a root to drop and to keep; `dev` and `ino`: the root's folder as the walk read it */
|
|
204
|
+
export type Plan = { root: string; drop: Idle[]; keep: Kept[]; fresh: number; dev: number; ino: number }
|
|
108
205
|
|
|
109
206
|
/** when an entry was last written: its content or its inode, so a tree copied with its old times is new */
|
|
110
207
|
const written = (s: Stats): number => Math.max(s.mtimeMs, s.ctimeMs)
|
|
@@ -128,7 +225,11 @@ function scan(top: string, c: Ctx): Scan | null {
|
|
|
128
225
|
if (r.fresh && ++seen > MEASURE_MAX_ENTRIES) { r.floor = true; return r }
|
|
129
226
|
if (basename(p) === '.git' && p !== top) r.gits.push(dirname(p))
|
|
130
227
|
if (!s.isDirectory()) { r.files++; continue }
|
|
131
|
-
|
|
228
|
+
let kids: string[]
|
|
229
|
+
try { kids = readdirSync(p) } catch { return null }
|
|
230
|
+
// a bare repo: git with no work tree, its `.git` folder's own shape
|
|
231
|
+
if (kids.includes('HEAD') && kids.includes('objects') && kids.includes('refs') && !p.slice(top.length).split(sep).includes('.git')) r.gits.push(p)
|
|
232
|
+
for (const n of kids) stack.push(join(p, n))
|
|
132
233
|
}
|
|
133
234
|
return r
|
|
134
235
|
}
|
|
@@ -164,7 +265,7 @@ function judge(p: string, c: Ctx, now: number, out: Found): void {
|
|
|
164
265
|
const u: Unit = { path: r.path, at: r.at, bytes: r.bytes, files: r.files }
|
|
165
266
|
if (r.fresh) out.keep.push({ ...u, why: `written ${ago(now - r.at)} ago`, ...(r.floor ? { floor: true } : {}) })
|
|
166
267
|
else if (r.foreign) out.keep.push({ ...u, why: 'holds a file of another user' })
|
|
167
|
-
else out.idle.push({ ...u, gits: r.gits })
|
|
268
|
+
else out.idle.push({ ...u, gits: r.gits, ...(stat(p)?.isSymbolicLink() === true ? { link: true } : {}) })
|
|
168
269
|
}
|
|
169
270
|
|
|
170
271
|
/** a root of entries: each child the user owns is a unit, a socket or a pipe at the top never is; with `fixedMin`, a
|
|
@@ -219,9 +320,28 @@ function sessions(real: string, c: Ctx, now: number, printErr: (l: string) => vo
|
|
|
219
320
|
}
|
|
220
321
|
}
|
|
221
322
|
|
|
323
|
+
/** a repo's `.shipkit/tmp`: each child of the user's a unit, except a link (never followed nor removed: an annex folder
|
|
324
|
+
* is one), a folder another sweep owns (REPO_OWNED), and a folder its annex config declares or one that holds one,
|
|
325
|
+
* kept unwalked */
|
|
326
|
+
function repoUnits(real: string, root: TempRoot, c: Ctx, now: number, printErr: (l: string) => void, out: Found): void {
|
|
327
|
+
const repo = dirname(dirname(real))
|
|
328
|
+
for (const n of names(real, printErr)) {
|
|
329
|
+
const p = join(real, n)
|
|
330
|
+
const s = stat(p)
|
|
331
|
+
if (s == null || REPO_OWNED.has(n) || s.uid !== c.uid) continue
|
|
332
|
+
if (s.isSymbolicLink()) { out.keep.push({ path: p, at: c.stamp(s), bytes: 0, files: 0, why: 'a link: never followed, never removed (the annex\'s, when it points there)' }); continue }
|
|
333
|
+
if (!s.isFile() && !s.isDirectory()) continue
|
|
334
|
+
const declared = root.declared?.(relative(repo, p)) ?? null
|
|
335
|
+
if (declared != null) { out.keep.push({ path: p, at: c.stamp(s), bytes: 0, files: 0, why: `declared in the annex config (${declared}): never swept, not measured` }); continue }
|
|
336
|
+
judge(p, c, now, out)
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/** git, read only: `--no-optional-locks` and GIT_OPTIONAL_LOCKS=0, so a status never refreshes an index, which would
|
|
341
|
+
* make an idle clone look written and change what the next pass does */
|
|
222
342
|
const gitOut = (cwd: string, args: string[]): { ok: boolean; out: string } => {
|
|
223
|
-
const p = Bun.spawnSync([gitBin(), ...args], { cwd, stdout: 'pipe', stderr: 'pipe' })
|
|
224
|
-
return { ok: p.exitCode === 0, out: p.stdout.toString()
|
|
343
|
+
const p = Bun.spawnSync([gitBin(), '--no-optional-locks', ...args], { cwd, stdout: 'pipe', stderr: 'pipe', env: { ...process.env, GIT_OPTIONAL_LOCKS: '0' } })
|
|
344
|
+
return { ok: p.exitCode === 0, out: p.stdout.toString() }
|
|
225
345
|
}
|
|
226
346
|
|
|
227
347
|
/** the record a linked worktree at `dir` has in its repo (`<repo git dir>/worktrees/<name>`), or null */
|
|
@@ -234,35 +354,111 @@ function worktreeRecord(dir: string): string | null {
|
|
|
234
354
|
} catch { return null }
|
|
235
355
|
}
|
|
236
356
|
|
|
357
|
+
/** what a tool makes again from the tracked files: an ignored path holding only these is no work */
|
|
358
|
+
const BUILD_OUTPUTS = new Set(['node_modules', 'dist', 'build', 'out', 'target', 'coverage', '.next', '.nuxt', '.turbo', '.cache', '.vite', '.parcel-cache', '__pycache__', '.pytest_cache', '.venv', '.DS_Store'])
|
|
359
|
+
const builds = (path: string): boolean => {
|
|
360
|
+
const parts = path.replace(/\/$/, '').split('/')
|
|
361
|
+
return parts.some((p) => BUILD_OUTPUTS.has(p)) || /^\.shipkit\/(tmp|cache)(\/|$)/.test(path)
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/** what `git status` lists that a delete would lose: an untracked or changed path, an ignored one no build makes again */
|
|
365
|
+
function looseFiles(dir: string): string | null {
|
|
366
|
+
const st = gitOut(dir, ['status', '--porcelain', '-z', '--ignored=matching', '--untracked-files=normal'])
|
|
367
|
+
if (!st.ok) return 'git cannot read it'
|
|
368
|
+
for (const e of st.out.split('\0').filter((x) => x !== '')) {
|
|
369
|
+
const code = e.slice(0, 2)
|
|
370
|
+
const path = e.slice(3)
|
|
371
|
+
if (code === '!!' && builds(path)) continue
|
|
372
|
+
return code === '!!' ? `an ignored file git keeps nowhere (${path})` : `a change git has not saved (${path})`
|
|
373
|
+
}
|
|
374
|
+
return null
|
|
375
|
+
}
|
|
376
|
+
|
|
237
377
|
/** work in one git folder that a delete would lose, in words, or null.
|
|
238
378
|
* - a linked worktree of a repo: a change git has not saved, or a commit no branch holds; a throwaway checkout of
|
|
239
379
|
* shipkit's (core/throwaway-worktree.ts) holds none; one whose repo is gone is plain files
|
|
240
380
|
* - a repo with a remote: a change git has not saved, or a commit no remote holds
|
|
241
|
-
* - a repo with no remote is a fixture or a scratch copy: nothing of it was ever meant to leave the folder
|
|
242
|
-
|
|
381
|
+
* - a repo with no remote is a fixture or a scratch copy: nothing of it was ever meant to leave the folder
|
|
382
|
+
* `strict`, in a repo's scratch, where lanes `git init` their studies: a git folder goes only when it is provably a
|
|
383
|
+
* clean copy of what a remote holds. A repo with no remote, a stash, a tag or branch no remote holds, an untracked
|
|
384
|
+
* file, an ignored one no build makes again (BUILD_OUTPUTS), a worktree whose repo is gone, and any git that does not
|
|
385
|
+
* answer: it stays */
|
|
386
|
+
function unsavedAt(dir: string, strict = false): string | null {
|
|
243
387
|
const dot = stat(join(dir, '.git'))
|
|
244
|
-
if (dot == null) return null
|
|
388
|
+
if (dot == null) return strict && bare(dir) ? 'a bare git repo: what it holds exists only here' : null
|
|
245
389
|
if (dot.isFile()) {
|
|
246
390
|
const record = worktreeRecord(dir)
|
|
247
|
-
if (record
|
|
248
|
-
|
|
249
|
-
if (
|
|
391
|
+
if (record != null && existsSync(join(record, 'shipkit-throwaway'))) return null
|
|
392
|
+
if (record == null || !existsSync(record)) return strict ? 'a git worktree whose repo is gone: git cannot tell what it holds' : null
|
|
393
|
+
if (strict) {
|
|
394
|
+
const loose = looseFiles(dir)
|
|
395
|
+
if (loose != null) return `a git worktree with ${loose}`
|
|
396
|
+
} else {
|
|
397
|
+
const status = gitOut(dir, ['status', '--porcelain'])
|
|
398
|
+
if (!status.ok || status.out.trim() !== '') return 'a git worktree with unsaved changes'
|
|
399
|
+
}
|
|
250
400
|
const held = gitOut(dir, ['for-each-ref', '--count=1', '--contains', 'HEAD'])
|
|
251
|
-
return held.ok && held.out !== '' ? null : 'a git worktree on a commit no branch holds'
|
|
401
|
+
return held.ok && held.out.trim() !== '' ? null : 'a git worktree on a commit no branch holds'
|
|
252
402
|
}
|
|
253
403
|
let config = ''
|
|
254
|
-
try { config = readFileSync(join(dir, '.git', 'config'), 'utf8') } catch { return null }
|
|
255
|
-
if (!/^\s*\[remote /m.test(config)) return null
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
404
|
+
try { config = readFileSync(join(dir, '.git', 'config'), 'utf8') } catch { return strict ? 'a git repo git cannot read' : null }
|
|
405
|
+
if (!/^\s*\[remote /m.test(config)) return strict ? 'a git repo with no remote: what it holds exists only here' : null
|
|
406
|
+
if (!strict) {
|
|
407
|
+
const status = gitOut(dir, ['status', '--porcelain'])
|
|
408
|
+
if (!status.ok || status.out.trim() !== '') return 'a git clone with unsaved changes'
|
|
409
|
+
const ahead = gitOut(dir, ['rev-list', '--max-count=1', '--branches', 'HEAD', '--not', '--remotes'])
|
|
410
|
+
return ahead.ok && ahead.out.trim() === '' ? null : 'a git clone with commits no remote holds'
|
|
411
|
+
}
|
|
412
|
+
// a remote on this machine is one more copy here: only one off the machine proves the work is held
|
|
413
|
+
const urls = gitOut(dir, ['config', '--get-regexp', '^remote\\..*\\.url$'])
|
|
414
|
+
const off = urls.out.split('\n').map((l) => /^remote\.(.+)\.url (.+)$/.exec(l.trim())).filter((m): m is RegExpExecArray => m != null && offMachine(m[2]!)).map((m) => m[1]!)
|
|
415
|
+
if (off.length === 0) return 'a git clone whose remotes are all on this machine: they prove nothing'
|
|
416
|
+
const not = ['--not', ...off.map((n) => `--remotes=${n}`)]
|
|
417
|
+
const loose = looseFiles(dir)
|
|
418
|
+
if (loose != null) return `a git clone with ${loose}`
|
|
419
|
+
if (gitOut(dir, ['rev-parse', '-q', '--verify', 'refs/stash']).ok) return 'a git clone with work held only in a stash'
|
|
420
|
+
const notes = gitOut(dir, ['for-each-ref', '--count=1', 'refs/notes'])
|
|
421
|
+
if (!notes.ok || notes.out.trim() !== '') return 'a git clone with git notes, which no remote is proven to hold'
|
|
422
|
+
const ahead = gitOut(dir, ['rev-list', '--max-count=1', '--branches', '--tags', 'HEAD', ...not])
|
|
423
|
+
if (!ahead.ok || ahead.out.trim() !== '') return 'a git clone with commits no remote off this machine holds'
|
|
424
|
+
const logged = reflogHeads(join(dir, '.git', 'logs'), Date.now() - REFLOG_KEEP_MS)
|
|
425
|
+
if (logged.length === 0) return null
|
|
426
|
+
const lost = gitOut(dir, ['rev-list', '--max-count=1', '--ignore-missing', ...logged, ...not])
|
|
427
|
+
return lost.ok && lost.out.trim() === '' ? null : `a git clone with work held only in its reflog (git keeps it ${REFLOG_KEEP_MS / 86_400_000} days)`
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
/** a folder shaped as git's own: `HEAD`, `objects/`, `refs/` (a bare repo) */
|
|
431
|
+
const bare = (dir: string): boolean => stat(join(dir, 'HEAD'))?.isFile() === true && stat(join(dir, 'objects'))?.isDirectory() === true && stat(join(dir, 'refs'))?.isDirectory() === true
|
|
432
|
+
|
|
433
|
+
/** a remote URL that names another machine: a URL with a scheme other than `file:`, or `host:path`; a path is here */
|
|
434
|
+
export const offMachine = (url: string): boolean => /^(https?|ssh|git|git\+ssh):\/\//i.test(url) || /^[\w.-]+@[\w.-]+:/.test(url) || /^[\w-]+(\.[\w-]+)+:[^/\\]/.test(url)
|
|
435
|
+
|
|
436
|
+
/** git keeps an unreachable reflog entry this long (its `gc.reflogExpireUnreachable`): work only there stays as long */
|
|
437
|
+
export const REFLOG_KEEP_MS = 30 * 24 * 60 * 60 * 1000
|
|
438
|
+
|
|
439
|
+
/** the commits the reflogs under `logs` reached since `since`, read from git's own files, newest first, at most 200 */
|
|
440
|
+
function reflogHeads(logs: string, since: number): string[] {
|
|
441
|
+
const out: { sha: string; at: number }[] = []
|
|
442
|
+
const walk = (p: string): void => {
|
|
443
|
+
const s = stat(p)
|
|
444
|
+
if (s == null) return
|
|
445
|
+
if (s.isDirectory()) { for (const n of names(p, () => {})) walk(join(p, n)); return }
|
|
446
|
+
if (!s.isFile()) return
|
|
447
|
+
let text = ''
|
|
448
|
+
try { text = readFileSync(p, 'utf8') } catch { return }
|
|
449
|
+
for (const l of text.split('\n')) {
|
|
450
|
+
const m = /^[0-9a-f]{40,64} ([0-9a-f]{40,64}) .*> (\d+) [+-]\d{4}/.exec(l)
|
|
451
|
+
if (m != null && !/^0+$/.test(m[1]!) && Number(m[2]) * 1000 >= since) out.push({ sha: m[1]!, at: Number(m[2]) })
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
walk(logs)
|
|
455
|
+
return [...new Set(out.sort((a, b) => b.at - a.at).map((x) => x.sha))].slice(0, 200)
|
|
260
456
|
}
|
|
261
457
|
|
|
262
458
|
/** the first git folder of a unit that holds unsaved work, with where */
|
|
263
|
-
function unsaved(u: Idle): string | null {
|
|
459
|
+
function unsaved(u: Idle, strict: boolean): string | null {
|
|
264
460
|
for (const dir of new Set([u.path, ...u.gits])) {
|
|
265
|
-
const why = unsavedAt(dir)
|
|
461
|
+
const why = unsavedAt(dir, strict)
|
|
266
462
|
if (why != null) return dir === u.path ? why : `${why} (${dir.slice(u.path.length + 1)})`
|
|
267
463
|
}
|
|
268
464
|
return null
|
|
@@ -286,31 +482,50 @@ export type PlanOptions = {
|
|
|
286
482
|
stamp?: (s: Stats) => number
|
|
287
483
|
/** the other roots: one inside this root is never a unit of it */
|
|
288
484
|
others?: string[]
|
|
485
|
+
/** why another owner keeps an idle unit (an annex record, a media file the store indexes), or null */
|
|
486
|
+
claimed?: (path: string) => string | null
|
|
289
487
|
}
|
|
290
488
|
|
|
291
|
-
/**
|
|
292
|
-
|
|
293
|
-
|
|
489
|
+
/** a root walked: its units fresh or idle, nothing judged yet against the processes */
|
|
490
|
+
export type Walked = { root: TempRoot; real: string; dev: number; ino: number; fresh: number; found: Found }
|
|
491
|
+
|
|
492
|
+
/** a root's units by their writes alone: the walk, before the live processes are read */
|
|
493
|
+
export function walkTemp(root: TempRoot, o: Pick<PlanOptions, 'now' | 'needBytes' | 'printErr' | 'uid' | 'stamp' | 'others'>): Walked | null {
|
|
294
494
|
const real = checkedRoot(root.path, root.kind)
|
|
295
495
|
const uid = o.uid ?? process.getuid?.()
|
|
296
496
|
if (real == null || uid == null) return null
|
|
297
|
-
const
|
|
298
|
-
|
|
299
|
-
|
|
497
|
+
const at = stat(real)
|
|
498
|
+
if (at == null) return null
|
|
499
|
+
// a repo's scratch keeps its day whatever the pressure: its agents are told a day, and lanes write there all day
|
|
500
|
+
const need = root.kind === 'repo' ? 0 : Math.max(0, o.needBytes ?? 0)
|
|
501
|
+
const c: Ctx = { fresh: need > 0 ? o.now - TEMP_GUARD_LOW_MS : o.now - TEMP_GUARD_MS, uid, stamp: o.stamp ?? written }
|
|
300
502
|
const found: Found = { idle: [], keep: [] }
|
|
301
503
|
if (root.kind === 'sessions') sessions(real, c, o.now, o.printErr, found)
|
|
504
|
+
else if (root.kind === 'repo') repoUnits(real, root, c, o.now, o.printErr, found)
|
|
302
505
|
else entries(real, o.others ?? [], root.fixedNameMinBytes, c, o.now, o.printErr, found)
|
|
303
|
-
|
|
304
|
-
|
|
506
|
+
return { root, real, dev: at.dev, ino: at.ino, fresh: c.fresh, found }
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
/** every unit of a walked root: fresh, used, claimed, live state, unsaved or under the cap kept, the idle rest dropped:
|
|
510
|
+
* past the guard oldest first, then, while the disk needs room, past the short guard largest first */
|
|
511
|
+
export function decideTemp(w: Walked, inUse: InUse | null, o: Pick<PlanOptions, 'now' | 'needBytes' | 'capBytes' | 'claimed'>): Plan {
|
|
512
|
+
const repo = w.root.kind === 'repo'
|
|
513
|
+
const need = repo ? 0 : Math.max(0, o.needBytes ?? 0)
|
|
514
|
+
const old = o.now - TEMP_GUARD_MS
|
|
515
|
+
const keep = [...w.found.keep]
|
|
305
516
|
const free: Idle[] = []
|
|
306
|
-
for (const u of found.idle) {
|
|
517
|
+
for (const u of w.found.idle) {
|
|
307
518
|
const use = inUse == null ? 'the live processes cannot be read' : inUse(u.path)
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
519
|
+
if (use != null) { keep.push({ ...u, why: inUse == null ? use : `in use: ${use}` }); continue }
|
|
520
|
+
const owner = o.claimed?.(u.path) ?? null
|
|
521
|
+
if (owner != null) { keep.push({ ...u, why: owner }); continue }
|
|
522
|
+
const db = repo ? (w.root.db?.(u.path) ?? null) : null
|
|
523
|
+
if (db != null) { keep.push({ ...u, why: `holds a database or a lock (${db}): live state, never taken`, live: true }); continue }
|
|
524
|
+
const work = unsaved(u, repo)
|
|
525
|
+
if (work != null) keep.push({ ...u, why: work })
|
|
311
526
|
else free.push(u)
|
|
312
527
|
}
|
|
313
|
-
const cap = need > 0 ? 0 : (o.capBytes ?? root.capBytes)
|
|
528
|
+
const cap = need > 0 ? 0 : (o.capBytes ?? w.root.capBytes)
|
|
314
529
|
const drop: Idle[] = []
|
|
315
530
|
let kept = 0
|
|
316
531
|
let freed = 0
|
|
@@ -322,57 +537,174 @@ export function planTemp(root: TempRoot, o: PlanOptions): Plan | null {
|
|
|
322
537
|
for (const u of free.filter((x) => x.at >= old).sort((a, b) => b.bytes - a.bytes)) {
|
|
323
538
|
if (freed < need) { drop.push(u); freed += u.bytes } else keep.push({ ...u, why: `idle ${ago(o.now - u.at)}, inside the guard` })
|
|
324
539
|
}
|
|
325
|
-
return { root: real, drop, keep, fresh:
|
|
540
|
+
return { root: w.real, drop, keep, fresh: w.fresh, dev: w.dev, ino: w.ino }
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
/** a root walked, its processes read once the walk is done, then judged */
|
|
544
|
+
export function planTemp(root: TempRoot, o: PlanOptions): Plan | null {
|
|
545
|
+
const w = walkTemp(root, o)
|
|
546
|
+
if (w == null) return null
|
|
547
|
+
return decideTemp(w, w.found.idle.length === 0 ? null : o.live(), o)
|
|
326
548
|
}
|
|
327
549
|
|
|
328
550
|
export const gb = (n: number): string => `${(n / 1e9).toFixed(2)} GB`
|
|
329
551
|
|
|
330
|
-
/** what one pass did to one root, as `sk tmp` reads it back
|
|
331
|
-
|
|
332
|
-
|
|
552
|
+
/** what one pass did to one root, as `sk tmp` reads it back. `notice`: a repo root before its start date, so the
|
|
553
|
+
* pass only counted what would go (`freed`, `dropped`, `going`); `going`: the largest units that went or would go;
|
|
554
|
+
* `waiting`: big units kept because no annex took them, until idle BIG_WAIT_MS */
|
|
555
|
+
export type Pass = { id: string; root: string; freed: number; dropped: number; failed: number; kept: number; keptBytes: number; top: Kept[]; going?: Unit[]; waiting?: Unit[]; moved?: number; movedBytes?: number; notice?: boolean; live?: LiveState }
|
|
556
|
+
/** the units a root keeps for a database or a lock, which no pass takes: how many, their size, the oldest write, and
|
|
557
|
+
* the 20 idle longest */
|
|
558
|
+
export type LiveState = { units: number; bytes: number; oldest: number; old: Unit[] }
|
|
559
|
+
/** a unit kept for live state this long is named in its repo (SK043): a person or its agent decides */
|
|
560
|
+
export const LIVE_NAME_MS = 30 * 24 * 60 * 60 * 1000
|
|
561
|
+
/** what a unit of MOVE_MIN_BYTES or more becomes: moved to the annex (`dry`: it would be), `none` (no annex is plugged
|
|
562
|
+
* in, or none has room), or why the annex refuses it */
|
|
563
|
+
export type Move = (u: Unit, dry: boolean) => Promise<'moved' | 'none' | string>
|
|
564
|
+
export type TrimOptions = PlanOptions & { dry?: boolean; print: (l: string) => void; top?: number; move?: Move; moveMinBytes?: number; list?: boolean }
|
|
565
|
+
type ActOptions = Omit<TrimOptions, 'live' | 'claimed' | 'others' | 'uid' | 'needBytes' | 'capBytes'>
|
|
566
|
+
|
|
567
|
+
const unitOf = (u: Unit): Unit => ({ path: u.path, at: u.at, bytes: u.bytes, files: u.files })
|
|
568
|
+
|
|
569
|
+
/** the root is still the folder its walk read: the same shape, not a link, the same inode */
|
|
570
|
+
function sameRoot(root: TempRoot, plan: Plan): boolean {
|
|
571
|
+
try {
|
|
572
|
+
if (checkedRoot(root.path, root.kind) !== plan.root) return false
|
|
573
|
+
} catch { return false }
|
|
574
|
+
const s = stat(plan.root)
|
|
575
|
+
return s != null && s.isDirectory() && s.dev === plan.dev && s.ino === plan.ino
|
|
576
|
+
}
|
|
333
577
|
|
|
334
|
-
/**
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
const
|
|
339
|
-
const
|
|
340
|
-
|
|
578
|
+
/** a unit removed through its root's own folder: the process stands in that folder (its inode, checked), every
|
|
579
|
+
* folder on the way is checked a real folder, and the unit is still what the walk saw, so a link put anywhere on its
|
|
580
|
+
* way since the walk is never followed. Why it stays, or null once it is gone */
|
|
581
|
+
function removeUnit(plan: Plan, u: Idle): string | null {
|
|
582
|
+
const rel = u.path.slice(plan.root.length + 1)
|
|
583
|
+
const back = process.cwd()
|
|
584
|
+
try {
|
|
585
|
+
process.chdir(plan.root)
|
|
586
|
+
} catch (e) { return `its root cannot be entered (${(e as Error).message})` }
|
|
587
|
+
try {
|
|
588
|
+
const here = lstatSync('.')
|
|
589
|
+
if (here.dev !== plan.dev || here.ino !== plan.ino) return 'its root changed since the walk'
|
|
590
|
+
const parts = rel.split(sep)
|
|
591
|
+
for (let i = 1; i < parts.length; i++) if (stat(parts.slice(0, i).join(sep))?.isDirectory() !== true) return `${parts.slice(0, i).join(sep)} is no longer a folder`
|
|
592
|
+
const s = stat(rel)
|
|
593
|
+
if (s == null) return null
|
|
594
|
+
if (s.isSymbolicLink() !== (u.link === true)) return 'it became a link since the walk'
|
|
595
|
+
removeTree(rel)
|
|
596
|
+
return null
|
|
597
|
+
} finally {
|
|
598
|
+
process.chdir(back)
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
/** the drop of a judged root acted on: each unit removed (or only named, `dry`), a big one of a repo moved to the
|
|
603
|
+
* annex first when `move` is given, else kept until idle BIG_WAIT_MS; the root checked again before each act */
|
|
604
|
+
export async function actTemp(root: TempRoot, plan: Plan, o: ActOptions): Promise<Pass> {
|
|
605
|
+
const keep = [...plan.keep]
|
|
606
|
+
const pass: Pass = { id: root.id, root: plan.root, freed: 0, dropped: 0, failed: 0, kept: 0, keptBytes: 0, top: [] }
|
|
607
|
+
const went: Unit[] = []
|
|
608
|
+
const waiting: Unit[] = []
|
|
609
|
+
const dry = o.dry === true
|
|
341
610
|
const stamp = o.stamp ?? written
|
|
611
|
+
const relOf = (u: Unit): string => u.path.slice(plan.root.length + 1)
|
|
342
612
|
let small = 0
|
|
613
|
+
let stopped = false
|
|
343
614
|
for (const u of plan.drop) {
|
|
344
615
|
if (!u.path.startsWith(plan.root + sep) || u.path.split(sep).includes('..')) throw new Error(`${u.path}: outside ${plan.root}, the pass stops`)
|
|
345
|
-
if (
|
|
616
|
+
if (stopped) { keep.push({ ...u, why: 'its root changed since the walk' }); continue }
|
|
617
|
+
let verb = dry ? 'would delete' : 'deleted'
|
|
618
|
+
if (!dry) {
|
|
619
|
+
if (!sameRoot(root, plan)) {
|
|
620
|
+
o.printErr(`WARN ${root.id}: ${plan.root} changed since the walk (a link, another folder): nothing more of it is touched this pass`)
|
|
621
|
+
stopped = true
|
|
622
|
+
keep.push({ ...u, why: 'its root changed since the walk' })
|
|
623
|
+
continue
|
|
624
|
+
}
|
|
346
625
|
// the walk can be minutes old: a unit written since stays
|
|
347
626
|
const now = stat(u.path)
|
|
348
627
|
if (now == null) continue
|
|
349
|
-
if (stamp(now) >= plan.fresh) {
|
|
628
|
+
if (stamp(now) >= plan.fresh) { keep.push({ ...u, why: 'written during the pass' }); continue }
|
|
629
|
+
}
|
|
630
|
+
if (o.move != null && u.bytes >= (o.moveMinBytes ?? MOVE_MIN_BYTES)) {
|
|
631
|
+
const moved = await o.move(unitOf(u), dry)
|
|
632
|
+
if (moved === 'moved') {
|
|
633
|
+
if (!dry) {
|
|
634
|
+
pass.moved = (pass.moved ?? 0) + 1
|
|
635
|
+
pass.movedBytes = (pass.movedBytes ?? 0) + u.bytes
|
|
636
|
+
}
|
|
637
|
+
pass.freed += u.bytes
|
|
638
|
+
pass.dropped++
|
|
639
|
+
went.push(unitOf(u))
|
|
640
|
+
o.print(`${root.id}: ${dry ? 'would move to the annex' : 'moved to the annex'} ${relOf(u)} idle ${ago(o.now - u.at)} ${gb(u.bytes)}`)
|
|
641
|
+
continue
|
|
642
|
+
}
|
|
643
|
+
if (moved !== 'none') { keep.push({ ...u, why: dry ? `the annex would refuse it: ${moved}` : `the annex refuses it, so it stays while one is plugged in: ${moved}` }); continue }
|
|
644
|
+
if (o.now - u.at < BIG_WAIT_MS) {
|
|
645
|
+
keep.push({ ...u, why: `${dry ? 'no annex plugged in' : 'no annex has room'}: kept until idle ${BIG_WAIT_MS / 86_400_000} days, then deleted` })
|
|
646
|
+
waiting.push(unitOf(u))
|
|
647
|
+
continue
|
|
648
|
+
}
|
|
649
|
+
verb = dry ? 'would delete (no annex, idle a week)' : 'deleted (no annex, idle a week)'
|
|
650
|
+
}
|
|
651
|
+
if (!dry) {
|
|
350
652
|
const records = [...new Set([u.path, ...u.gits])].map(worktreeRecord).filter((r): r is string => r != null)
|
|
351
|
-
|
|
653
|
+
let why: string | null
|
|
654
|
+
try { why = removeUnit(plan, u) } catch (e) {
|
|
352
655
|
o.printErr(`WARN ${root.id}: ${u.path} not deleted (${(e as Error).message})`)
|
|
353
656
|
pass.failed++
|
|
354
657
|
continue
|
|
355
658
|
}
|
|
659
|
+
if (why != null) { keep.push({ ...u, why }); continue }
|
|
356
660
|
// only the record of the worktree just deleted: a prune would also drop one whose drive is unplugged
|
|
357
661
|
for (const r of records) rmSync(r, { recursive: true, force: true })
|
|
358
662
|
}
|
|
359
663
|
pass.freed += u.bytes
|
|
360
664
|
pass.dropped++
|
|
361
|
-
|
|
665
|
+
went.push(unitOf(u))
|
|
666
|
+
if (u.bytes >= SAY_BYTES || o.list === true) o.print(`${root.id}: ${verb} ${relOf(u)} idle ${ago(o.now - u.at)} ${gb(u.bytes)}`)
|
|
362
667
|
else small++
|
|
363
668
|
}
|
|
364
|
-
|
|
669
|
+
const top = (k: Kept): Kept => ({ path: k.path, at: k.at, bytes: k.bytes, files: k.files, why: k.why, ...(k.floor === true ? { floor: true } : {}) })
|
|
670
|
+
pass.kept = keep.length
|
|
671
|
+
pass.keptBytes = keep.reduce((n, u) => n + u.bytes, 0)
|
|
672
|
+
pass.top = [...keep].sort((a, b) => b.bytes - a.bytes).slice(0, o.top ?? 20).map(top)
|
|
673
|
+
pass.going = went.sort((a, b) => b.bytes - a.bytes).slice(0, 10)
|
|
674
|
+
if (waiting.length > 0) pass.waiting = waiting.sort((a, b) => b.bytes - a.bytes).slice(0, 10)
|
|
675
|
+
const live = keep.filter((k) => k.live === true)
|
|
676
|
+
if (live.length > 0) pass.live = { units: live.length, bytes: live.reduce((n, u) => n + u.bytes, 0), oldest: Math.min(...live.map((u) => u.at)), old: live.sort((a, b) => a.at - b.at).slice(0, 20).map(unitOf) }
|
|
677
|
+
if (dry && o.list === true) for (const k of [...keep].sort((a, b) => b.bytes - a.bytes)) o.print(`${root.id}: kept ${relOf(k)} ${k.why} ${gb(k.bytes)}`)
|
|
678
|
+
if (dry || pass.dropped > 0) o.print(`${root.id}: ${dry ? 'would free' : 'freed'} ${gb(pass.freed)} in ${pass.dropped} units${small > 0 ? ` (${small} under ${SAY_BYTES / 1e6} MB each)` : ''}${pass.moved ? `, ${pass.moved} of them moved to the annex` : ''}; kept ${pass.kept} units, ${gb(pass.keptBytes)}`)
|
|
365
679
|
return pass
|
|
366
680
|
}
|
|
367
681
|
|
|
682
|
+
/** one pass over one root: walked, judged, acted on */
|
|
683
|
+
export async function trimTemp(root: TempRoot, o: TrimOptions): Promise<Pass | null> {
|
|
684
|
+
const plan = planTemp(root, o)
|
|
685
|
+
return plan == null ? null : actTemp(root, plan, o)
|
|
686
|
+
}
|
|
687
|
+
|
|
368
688
|
/** a path as the kernel names it: macOS keeps /tmp and /var under /private */
|
|
369
689
|
const kernelPath = (p: string): string => (/^\/(tmp|var)(\/|$)/.test(p) ? `/private${p}` : p)
|
|
370
690
|
|
|
691
|
+
/** the user's processes as lsof (`-F pfn`: each file, its descriptor, its name) and ps (`-AEo pid=,args=`) list
|
|
692
|
+
* them, or null when either cannot be read */
|
|
693
|
+
export type Listings = { lsof: string; ps: string }
|
|
694
|
+
function readProcesses(uid: number): Listings | null {
|
|
695
|
+
const lsof = Bun.spawnSync(['lsof', '-u', String(uid), '-F', 'pfn'], { stdout: 'pipe', stderr: 'ignore' })
|
|
696
|
+
// lsof exits 1 when one file of one process cannot be read: its listing is still whole
|
|
697
|
+
const ps = Bun.spawnSync(['ps', '-AEo', 'pid=,args='], { stdout: 'pipe', stderr: 'ignore' })
|
|
698
|
+
return ps.exitCode === 0 ? { lsof: lsof.stdout.toString(), ps: ps.stdout.toString() } : null
|
|
699
|
+
}
|
|
700
|
+
|
|
371
701
|
/** what the live processes of this user hold under `roots`: each open file, working folder and path on a command
|
|
372
702
|
* line or in an environment, with every folder above it. null when the processes cannot be read: the pass then
|
|
373
703
|
* deletes nothing */
|
|
374
|
-
export function liveUse(roots: string[], uid = process.getuid?.()): InUse | null {
|
|
704
|
+
export function liveUse(roots: string[], uid = process.getuid?.(), read: (uid: number) => Listings | null = readProcesses): InUse | null {
|
|
375
705
|
if (uid == null) return null
|
|
706
|
+
const got = read(uid)
|
|
707
|
+
if (got == null) return null
|
|
376
708
|
const held = new Map<string, string>()
|
|
377
709
|
const hold = (path: string, why: string): void => {
|
|
378
710
|
const p = kernelPath(path)
|
|
@@ -380,23 +712,33 @@ export function liveUse(roots: string[], uid = process.getuid?.()): InUse | null
|
|
|
380
712
|
if (root == null) return
|
|
381
713
|
for (let at = p; at.length > root.length; at = dirname(at)) if (!held.has(at)) held.set(at, why)
|
|
382
714
|
}
|
|
383
|
-
const
|
|
384
|
-
const open = lsof.stdout.toString()
|
|
385
|
-
// lsof exits 1 when one file of one process cannot be read: its listing is still whole
|
|
715
|
+
const open = got.lsof
|
|
386
716
|
if (!/^p\d+$/m.test(open)) return null
|
|
387
717
|
let pid = '?'
|
|
718
|
+
let fd = ''
|
|
719
|
+
const cwd = new Map<string, string>()
|
|
388
720
|
for (const l of open.split('\n')) {
|
|
389
721
|
if (l.startsWith('p')) pid = l.slice(1)
|
|
390
|
-
else if (l.startsWith('
|
|
722
|
+
else if (l.startsWith('f')) fd = l.slice(1)
|
|
723
|
+
else if (l.startsWith('n/')) {
|
|
724
|
+
hold(l.slice(1), `pid ${pid} holds it open`)
|
|
725
|
+
if (fd === 'cwd') cwd.set(pid, l.slice(1))
|
|
726
|
+
}
|
|
391
727
|
}
|
|
392
|
-
// -E adds each process's environment after its command: a PATH entry or a variable that names a unit holds it
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
const
|
|
396
|
-
|
|
728
|
+
// -E adds each process's environment after its command: a PATH entry or a variable that names a unit holds it. A
|
|
729
|
+
// repo's scratch is also named from inside the repo, `.shipkit/tmp/x`: read against the process's working folder
|
|
730
|
+
const STOP = `[^\\s'":=,;]`
|
|
731
|
+
const own = roots.map((r) => r.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'))
|
|
732
|
+
// a root first: at one start the first branch that matches wins, and a root's own path may hold a space
|
|
733
|
+
const paths = new RegExp(`${own.map((r) => `${r}\\/${STOP}+|`).join('')}(?:\\/private)?\\/(?:tmp|var\\/folders)\\/${STOP}+`, 'g')
|
|
734
|
+
const inRepo = new RegExp(`(?:^|[\\s'"=:,;])(?:\\.\\/)?(${LAYOUT.tmp.replace(/\./g, '\\.')}\\/${STOP}+)`, 'g')
|
|
735
|
+
for (const l of got.ps.split('\n')) {
|
|
397
736
|
const m = /^\s*(\d+)\s+(.*)$/.exec(l)
|
|
398
737
|
if (m == null) continue
|
|
399
|
-
|
|
738
|
+
const why = `the command or the environment of pid ${m[1]} names it`
|
|
739
|
+
for (const p of m[2]!.match(paths) ?? []) hold(p.replace(/\/+$/, ''), why)
|
|
740
|
+
const at = cwd.get(m[1]!)
|
|
741
|
+
if (at != null) for (const r of m[2]!.matchAll(inRepo)) hold(join(at, r[1]!).replace(/\/+$/, ''), why)
|
|
400
742
|
}
|
|
401
743
|
return (path) => held.get(path) ?? null
|
|
402
744
|
}
|
|
@@ -431,12 +773,100 @@ function takeLock(): { release: () => void } | { pid: number } {
|
|
|
431
773
|
return { pid: 0 }
|
|
432
774
|
}
|
|
433
775
|
|
|
434
|
-
|
|
776
|
+
/** the repo roots of the house repos `repos` (repoTempRoots), each with what its annex config declares. A repo whose
|
|
777
|
+
* config does not load is left out, said once: nothing of it is judged without its declarations */
|
|
778
|
+
export async function houseTempRoots(repos: readonly unknown[], printErr: (l: string) => void, house?: (repo: string) => string | null): Promise<TempRoot[]> {
|
|
779
|
+
const { annexDeclared, declaredOf } = await import('../annex/tidy.ts')
|
|
780
|
+
const { entries, isDb } = await import('../annex/offload.ts')
|
|
781
|
+
const houseOf = house ?? (await import('./home-start.ts')).houseRootOf
|
|
782
|
+
// the annex's own test of a db or a lock, on every file of a unit; a folder that cannot be read counts as one
|
|
783
|
+
const db = (path: string): string | null => {
|
|
784
|
+
const s = stat(path)
|
|
785
|
+
if (s == null || !s.isDirectory()) return s?.isFile() === true && isDb(dirname(path), basename(path), true) ? basename(path) : null
|
|
786
|
+
try {
|
|
787
|
+
return entries(path).find((e) => !e.link && isDb(path, e.rel, e.file))?.rel ?? null
|
|
788
|
+
} catch { return 'a folder of it cannot be read' }
|
|
789
|
+
}
|
|
790
|
+
const out: TempRoot[] = []
|
|
791
|
+
for (const r of repoTempRoots(repos)) {
|
|
792
|
+
try {
|
|
793
|
+
const { which } = declaredOf(await annexDeclared(r.repo!, houseOf(r.repo!)))
|
|
794
|
+
out.push({ ...r, declared: which, db })
|
|
795
|
+
} catch (e) {
|
|
796
|
+
printErr(`WARN ${r.id}: its annex config does not load, so its .shipkit/tmp is left as it is: ${(e as Error).message}`)
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
return out
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
/** what keeps an idle unit of a repo root that another owner holds: a move of the annex in flight, a folder an annex
|
|
803
|
+
* record names (a spill, an aside), a file the media store indexes on the disk (its purge lets it go) */
|
|
804
|
+
async function repoClaims(root: TempRoot, real: string): Promise<(path: string) => string | null> {
|
|
805
|
+
const { IN_FLIGHT } = await import('../annex/tidy.ts')
|
|
806
|
+
const { offloads } = await import('../annex/offload.ts')
|
|
807
|
+
const { mediaDb } = await import('../media-store/db.ts')
|
|
808
|
+
const repo = dirname(dirname(real))
|
|
809
|
+
const given = root.repo ?? repo
|
|
810
|
+
// a record or a row in the repo's form as the house lists it reads in the real form
|
|
811
|
+
const real0 = (p: string): string => (p === given || p.startsWith(given + sep) ? repo + p.slice(given.length) : p)
|
|
812
|
+
const under = (p: string): boolean => p === real || p.startsWith(real + sep)
|
|
813
|
+
const records = offloads()
|
|
814
|
+
.filter((x) => real0(x.repo) === repo)
|
|
815
|
+
.flatMap((x) => [join(repo, x.rel), ...(x.aside === undefined ? [] : [real0(x.aside)])])
|
|
816
|
+
.filter(under)
|
|
817
|
+
const rows = (mediaDb()?.query('select store, item, name from media_files where local = 1').all() ?? []) as { store: string; item: string; name: string }[]
|
|
818
|
+
const media = rows.map((x) => real0(join(x.store, x.item, x.name))).filter(under)
|
|
819
|
+
const touches = (p: string, a: string): boolean => a === p || a.startsWith(p + sep) || p.startsWith(a + sep)
|
|
820
|
+
return (p) => {
|
|
821
|
+
if (IN_FLIGHT.test(basename(p))) return 'a move of the annex in flight: the annex pass finishes it'
|
|
822
|
+
const r = records.find((a) => touches(p, a))
|
|
823
|
+
if (r != null) return `the annex's: its record holds ${relative(repo, r)}`
|
|
824
|
+
const m = media.find((a) => touches(p, a))
|
|
825
|
+
if (m != null) return `the media store indexes ${relative(repo, m)}: its purge lets it go`
|
|
826
|
+
return null
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
/** a big idle unit of a repo to the annex through its own offload, a link left in its place (`dry`: its checks
|
|
831
|
+
* only): `none` when no annex is present or none has room */
|
|
832
|
+
const repoMove = (root: TempRoot, real: string, probe: Probe): Move => async (u, dry) => {
|
|
833
|
+
const { presentAnnexes } = await import('../annex/annex.ts')
|
|
834
|
+
const { NoRoom, offload } = await import('../annex/offload.ts')
|
|
835
|
+
if ((await presentAnnexes()).length === 0) return 'none'
|
|
836
|
+
try {
|
|
837
|
+
await offload(root.repo ?? dirname(dirname(real)), relative(dirname(dirname(real)), u.path), { ...probe, dry })
|
|
838
|
+
return 'moved'
|
|
839
|
+
} catch (e) {
|
|
840
|
+
return e instanceof NoRoom ? 'none' : (e as Error).message
|
|
841
|
+
}
|
|
842
|
+
}
|
|
435
843
|
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
844
|
+
export type SweepOptions = {
|
|
845
|
+
now?: number
|
|
846
|
+
dry?: boolean
|
|
847
|
+
/** the machine's own roots (tempRoots by default) */
|
|
848
|
+
roots?: TempRoot[]
|
|
849
|
+
/** the house repos, whose `.shipkit/tmp` are roots too (houseTempRoots), swept first */
|
|
850
|
+
repos?: readonly string[]
|
|
851
|
+
print: (l: string) => void
|
|
852
|
+
printErr: (l: string) => void
|
|
853
|
+
free?: number
|
|
854
|
+
live?: () => InUse | null
|
|
855
|
+
stamp?: (s: Stats) => number
|
|
856
|
+
/** the house repo of a repo, whose config holds the house's annex scratch (home-start.ts by default) */
|
|
857
|
+
house?: (repo: string) => string | null
|
|
858
|
+
/** the probes the annex's offload takes (its backup inventory, lsof, git), its own by default */
|
|
859
|
+
probe?: Probe
|
|
860
|
+
moveMinBytes?: number
|
|
861
|
+
/** a dry pass names every unit, not only the big ones */
|
|
862
|
+
list?: boolean
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
/** one pass over every temp root, one process at a time on the machine: every root walked, then the live processes
|
|
866
|
+
* read once, then each root judged and acted on. A real pass keeps what it measured for `sk tmp`, after each root, so
|
|
867
|
+
* a pass cut short still tells; a dry one keeps nothing. A repo root before its start date (reposFrom) only counts
|
|
868
|
+
* what would go. null when another process sweeps */
|
|
869
|
+
export async function sweepTemp(o: SweepOptions): Promise<TempUse | null> {
|
|
440
870
|
const lock = takeLock()
|
|
441
871
|
if ('pid' in lock) {
|
|
442
872
|
o.printErr(`temp: a sweep already runs (pid ${lock.pid}): \`sk tmp\` prints what it measured once it ends`)
|
|
@@ -445,13 +875,13 @@ export function sweepTemp(o: SweepOptions): TempUse | null {
|
|
|
445
875
|
try {
|
|
446
876
|
const started = Date.now()
|
|
447
877
|
const now = o.now ?? started
|
|
448
|
-
const roots = o.roots ?? tempRoots()
|
|
878
|
+
const roots = [...(o.repos == null ? [] : await houseTempRoots(o.repos, o.printErr, o.house)), ...(o.roots ?? tempRoots())]
|
|
449
879
|
const reals: string[] = []
|
|
450
880
|
for (const r of roots) {
|
|
451
881
|
try {
|
|
452
882
|
const real = checkedRoot(r.path, r.kind)
|
|
453
883
|
if (real != null) reals.push(real)
|
|
454
|
-
} catch { /*
|
|
884
|
+
} catch { /* the walk says why below */ }
|
|
455
885
|
}
|
|
456
886
|
let free = o.free
|
|
457
887
|
if (free == null) {
|
|
@@ -460,17 +890,40 @@ export function sweepTemp(o: SweepOptions): TempUse | null {
|
|
|
460
890
|
}
|
|
461
891
|
const use: TempUse = { at: now, tookMs: 0, free, low: free < TEMP_FLOOR_BYTES, passes: [] }
|
|
462
892
|
let need = Math.max(0, TEMP_FLOOR_BYTES - free)
|
|
463
|
-
|
|
464
|
-
const live = o.live ?? ((): InUse | null => {
|
|
465
|
-
const l = liveUse(reals)
|
|
466
|
-
if (l == null && !warned) { warned = true; o.printErr('WARN temp: the live processes cannot be read (lsof, ps), so nothing is deleted') }
|
|
467
|
-
return l
|
|
468
|
-
})
|
|
893
|
+
const walked: Walked[] = []
|
|
469
894
|
for (const r of roots) {
|
|
470
895
|
try {
|
|
471
|
-
const
|
|
472
|
-
if (
|
|
473
|
-
|
|
896
|
+
const w = walkTemp(r, { now, needBytes: need, printErr: o.printErr, others: reals, ...(o.stamp ? { stamp: o.stamp } : {}) })
|
|
897
|
+
if (w != null) walked.push(w)
|
|
898
|
+
} catch (e) {
|
|
899
|
+
o.printErr(`WARN ${r.id}: ${(e as Error).message}`)
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
// once, after every root is walked: a process started during any walk is seen
|
|
903
|
+
let inUse: InUse | null = null
|
|
904
|
+
if (walked.some((w) => w.found.idle.length > 0)) {
|
|
905
|
+
inUse = (o.live ?? (() => liveUse(reals)))()
|
|
906
|
+
if (inUse == null) o.printErr('WARN temp: the live processes cannot be read (lsof, ps), so nothing is deleted')
|
|
907
|
+
}
|
|
908
|
+
const deletes = reposDelete(now)
|
|
909
|
+
for (const w of walked) {
|
|
910
|
+
const r = w.root
|
|
911
|
+
try {
|
|
912
|
+
const notice = r.kind === 'repo' && !deletes
|
|
913
|
+
if (notice && o.dry === true) o.print(`${r.id}: before the start date of the repo roots (${fromWords(reposFrom())}), a pass only counts these`)
|
|
914
|
+
const plan = decideTemp(w, inUse, { now, needBytes: need, ...(r.kind === 'repo' ? { claimed: await repoClaims(r, w.real) } : {}) })
|
|
915
|
+
const pass = await actTemp(r, plan, {
|
|
916
|
+
now,
|
|
917
|
+
dry: o.dry === true || notice,
|
|
918
|
+
print: notice && o.dry !== true ? () => {} : o.print,
|
|
919
|
+
printErr: o.printErr,
|
|
920
|
+
list: o.list === true,
|
|
921
|
+
...(o.stamp ? { stamp: o.stamp } : {}),
|
|
922
|
+
...(o.moveMinBytes == null ? {} : { moveMinBytes: o.moveMinBytes }),
|
|
923
|
+
...(r.kind === 'repo' ? { move: repoMove(r, w.real, o.probe ?? {}) } : {}),
|
|
924
|
+
})
|
|
925
|
+
if (notice) pass.notice = true
|
|
926
|
+
else need = Math.max(0, need - pass.freed)
|
|
474
927
|
use.passes.push(pass)
|
|
475
928
|
use.tookMs = Date.now() - started
|
|
476
929
|
if (o.dry !== true) machineSet(TEMP_USE, use)
|
|
@@ -483,3 +936,11 @@ export function sweepTemp(o: SweepOptions): TempUse | null {
|
|
|
483
936
|
lock.release()
|
|
484
937
|
}
|
|
485
938
|
}
|
|
939
|
+
|
|
940
|
+
/** the start date of the repo roots in words */
|
|
941
|
+
export const fromWords = (from: number | null): string => (from == null ? 'none set: sk tmp --repos-from <YYYY-MM-DD>' : `from ${dayOf(from)}`)
|
|
942
|
+
/** a day as the owner types it, in the machine's own time zone */
|
|
943
|
+
export const dayOf = (at: number): string => {
|
|
944
|
+
const d = new Date(at)
|
|
945
|
+
return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`
|
|
946
|
+
}
|