rvlib-shipkit 2.22.0 → 2.23.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 +23 -0
- package/package.json +1 -1
- package/src/annex/annex.ts +3 -0
- package/src/annex/born.ts +464 -0
- package/src/annex/flush.ts +46 -0
- package/src/annex/offload.ts +171 -38
- package/src/annex/snapshots.ts +53 -0
- package/src/annex/tidy.ts +368 -0
- package/src/checks/SK041-annex-links.check.ts +4 -2
- package/src/checks/SK043-scratch-on-the-mac.check.ts +45 -0
- package/src/cli/cmd_Annex.ts +60 -9
- package/src/cli/cmd_Home.ts +28 -0
- package/src/cli/cmd_Rec.ts +17 -7
- package/src/cli/cmd_Review.ts +27 -3
- package/src/core/machine-db.ts +8 -0
- package/src/home/claude-temp.ts +139 -0
- package/src/home/daemon-jobs.ts +10 -3
- package/src/home/global-units.ts +3 -0
- package/src/home/machine-stores.ts +27 -3
- package/src/home/pm.ts +4 -1
- package/src/home/run-pm.ts +1 -0
- package/src/identities/identities.ts +92 -24
- package/src/media-store/daemon.ts +2 -2
- package/src/media-store/purge.ts +4 -0
- package/src/recordings/load.ts +3 -3
- package/src/recordings/model.ts +7 -0
- package/src/recordings/open.ts +12 -0
- package/src/recordings/presets.ts +12 -5
- package/src/recordings/recorder-app/Capture.swift +34 -5
- package/src/recordings/recorder-app/Clip.swift +218 -54
- package/src/recordings/recorder-app/Editor.swift +177 -7
- package/src/recordings/recorder-app/Hud.swift +189 -0
- package/src/recordings/recorder-app/Tray.swift +179 -36
- package/src/recordings/recorder-app/icon.svg +16 -0
- package/src/recordings/recorder-app/main.swift +363 -29
- package/src/recordings/recorder-app.ts +8 -4
- package/src/recordings/recorder.shipkit.ts +2 -1
- package/src/recordings/routes.ts +52 -6
- package/src/recordings/run-recorder.ts +2 -1
- package/src/recordings/share.ts +27 -0
- package/src/recordings/take.ts +36 -4
- package/src/recordings/view.ts +11 -0
- package/src/repo-config.ts +9 -1
- package/src/review/model.ts +33 -4
- package/src/serve/hub-app/hub.css +4 -0
- package/src/serve/hub-app/recordings-st.ts +135 -24
- package/src/serve/hub-app/recordings-view.tsx +293 -54
- package/src/serve/hub-app/recordings.css +77 -24
- package/src/shipkit-files.ts +5 -0
- package/src/social/drafts.ts +4 -3
- package/src/social/load.ts +7 -2
- package/src/social/publish/discord.ts +10 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,26 @@
|
|
|
1
|
+
## 2.23.0
|
|
2
|
+
|
|
3
|
+
- **The capture editor is one window.** The recent captures sit in a band under the editor while it is open, the one shown ringed, and are their own small panel while it is closed: never two windows. While its window is open the recorder is in ⌘Tab and the Dock, under its own icon (a viewfinder and a record dot on red), and a menu bar app again once it closes.
|
|
4
|
+
- **Every save is a version.** ⌘S renders the edit as v1, v2 and so on, each kept with its time and the text that made it; the Edit menu loads any of them again. A save with nothing changed renders nothing, cuts no version and uploads nothing, and a save keeps the editor as it was (playhead, tool, undo). A save goes up at once instead of waiting for the media store's next pass.
|
|
5
|
+
- **Where a capture stands**, one row under the toolbar: on the disk, which save plays, up in the Space, and its WebP, each with a light (done, running, not yet, waiting on a save).
|
|
6
|
+
- **⌘C in the editor** copies the capture as the toolbar's pick: the file, WebP (a screenshot as WebP HQ, a video up to 60 s as an animated WebP) or a private link. The WebP is made on its first copy and kept until another save plays; a screenshot shot with WebP HQ as its clipboard format has it from the shot.
|
|
7
|
+
- **A clipboard indicator** in the screen's corner and in the Capture window says only what the clipboard read back: copied, then the format and the size it serves, or that another copy took its place.
|
|
8
|
+
- **A cuts lane** under the play bar: paint the parts to cut, as many as wanted, and playback skips them; a drag from a red part puts that span back. It replaces the two trim handles.
|
|
9
|
+
- **All captures as a grid** in the Capture window: the grid button, G, or « All captures » in the band.
|
|
10
|
+
- **The editor reads better**: drawn tool icons in one bar, one toolbar row, copy colours dark enough for white text (the tray's too), and a two tone line around the capture so its edge shows on any ground. Tooltips stay inside the window.
|
|
11
|
+
- **Recent captures**: a × on a thumb's hover deletes the capture (two clicks), and `recorder.trayKey` is a global hotkey that shows or hides them.
|
|
12
|
+
- **The record bar keeps its mic and system sound** as they were left, per preset, across restarts.
|
|
13
|
+
- **The editor opens the instant a take ends**, a spinner over it while the take is filed; a capture hotkey pressed twice never stacks two captures.
|
|
14
|
+
- **A global process can start with pm**: `atLogin: true` starts it whenever pm starts cold (a login, a reboot, a kill), which the recorder uses so its hotkeys survive. pm and the recorder log the signal that stopped them.
|
|
15
|
+
- **Annex: scratch goes to the annex by itself** for a repo that opts in, behind the owner's switch; a move never loses a file, and a drive counts as back only with its marker.
|
|
16
|
+
- **Retention**: the sweep trims Claude Code's session temp folders, behind a switch, the newest sessions kept.
|
|
17
|
+
- **Review**: one bare `sk review watch` per repo, and an agent's card is answered by its lane alone, never twice.
|
|
18
|
+
- **Identities**: every account inherits its list of links, built from the other accounts' live links.
|
|
19
|
+
|
|
20
|
+
## 2.22.1
|
|
21
|
+
|
|
22
|
+
- **A screenshot is on the clipboard the instant it is shot.** The recorder writes the clipboard before any encode, its file, PNG and WebP made behind it; a paste that comes first waits for them, so a quick ⌘V never pastes the clipboard from before, whatever the format.
|
|
23
|
+
|
|
1
24
|
## 2.22.0
|
|
2
25
|
|
|
3
26
|
- **A screen recorder replaces Snagit.** A menu bar app, built and run by shipkit, shoots videos and screenshots on global hotkeys. Each preset in `recorder.presets` (`repo.config.ts`) names its keys (one or several), what it shoots, the mic, the system sound, clicks, a countdown and what runs after: upload, a private or public link, the file, markdown, the editor, the Finder. `sk rec presets` lists them and every problem.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rvlib-shipkit",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.23.0",
|
|
4
4
|
"funding": "https://github.com/sponsors/rvion",
|
|
5
5
|
"description": "All-in-one repo companion: ci gate (task DAG), auto-discovered health checks, release-contract fulfillness, asks queue, md-wiki tools, asset atelier, live tui",
|
|
6
6
|
"license": "MIT",
|
package/src/annex/annex.ts
CHANGED
|
@@ -24,6 +24,9 @@ export type AnnexSpec = {
|
|
|
24
24
|
maxGb?: number
|
|
25
25
|
/** the free space its drive keeps, in GB (ANNEX_FREE_MIN when none is given) */
|
|
26
26
|
keepFreeGb?: number
|
|
27
|
+
/** the most its scratch (`offload/`: born and offloaded folders) may hold, in GB: past it, files untouched for
|
|
28
|
+
* SCRATCH_KEEP_DAYS go, oldest first (tidy.ts). None: only `keepFreeGb` trims it */
|
|
29
|
+
scratchMaxGb?: number
|
|
27
30
|
}
|
|
28
31
|
|
|
29
32
|
export const keepFree = (spec: AnnexSpec): number => (spec.keepFreeGb === undefined ? ANNEX_FREE_MIN : spec.keepFreeGb * GB)
|
|
@@ -0,0 +1,464 @@
|
|
|
1
|
+
// scratch folders that live on the annex from their first byte, and every record's spill while no annex is present
|
|
2
|
+
// (wiki tools/shipkit/annex.md, Scratch by itself). A folder in use moves COPY FIRST, THEN SWAP: its files are copied
|
|
3
|
+
// to the annex while it stays live (what was copied is noted), the aside is recorded, the folder renamed aside and the
|
|
4
|
+
// link put in its place, then only what changed on either side since the copy is merged. A copy then compare refuses
|
|
5
|
+
// a cache a tool writes during the copy; a link put in before the copy hands a tool an empty folder.
|
|
6
|
+
//
|
|
7
|
+
// the one constraint a reader breaks first: no Mac file that differs from the annex's copy is deleted. A file both
|
|
8
|
+
// sides hold in two versions keeps one at its name and the other beside it (`<name>.mac-<time>`, `<name>.annex-<time>`),
|
|
9
|
+
// on the record's `kept` until someone deletes it; the aside goes only once the annex is flushed
|
|
10
|
+
import { closeSync, copyFileSync, existsSync, lstatSync, mkdirSync, openSync, readdirSync, readlinkSync, readSync, renameSync, rmSync, type Stats, symlinkSync, utimesSync } from 'node:fs'
|
|
11
|
+
import { basename, dirname, join, resolve } from 'node:path'
|
|
12
|
+
import { machineGet, machineRead, machineSet } from '../core/machine-db.ts'
|
|
13
|
+
import { type AnnexSpec, driveFree, isPresent, keepFree } from './annex.ts'
|
|
14
|
+
import { flushDrive, syncFile } from './flush.ts'
|
|
15
|
+
import { asidesOf, type Checks, editOffloads, entries, git, inScratch, type Offload, offloads, ownerOf, PART_MARK, refusal, stageOf, stampOf, tally, toTrash } from './offload.ts'
|
|
16
|
+
|
|
17
|
+
const GB = 1e9
|
|
18
|
+
export type Say = (line: string) => void
|
|
19
|
+
|
|
20
|
+
/** a file as a merge knows it: its size and its mtime in ms (a copy keeps both); a link is [-1, 0] */
|
|
21
|
+
type Mark = [number, number]
|
|
22
|
+
/** one file as the move last handled it: the Mac's version it took, and the annex's version it left there (null: none).
|
|
23
|
+
* A later pass compares each side with its own: the Mac changed since, a tool wrote the annex since */
|
|
24
|
+
type Handled = { mac: Mark; annex: Mark | null }
|
|
25
|
+
export type Seen = Record<string, Handled>
|
|
26
|
+
const markOf = (s: Stats): Mark => (s.isSymbolicLink() ? [-1, 0] : [s.size, s.mtimeMs])
|
|
27
|
+
/** a time a copy set reads back within a ms: the float seconds of utimes round */
|
|
28
|
+
const sameMark = (a: Mark, b: Mark): boolean => a[0] === b[0] && Math.abs(a[1] - b[1]) < 2
|
|
29
|
+
|
|
30
|
+
/** two files of one size with the same bytes, read 1 MB at a time */
|
|
31
|
+
function sameBytes(a: string, b: string): boolean {
|
|
32
|
+
const fa = openSync(a, 'r')
|
|
33
|
+
const fb = openSync(b, 'r')
|
|
34
|
+
try {
|
|
35
|
+
const ba = Buffer.alloc(1 << 20)
|
|
36
|
+
const bb = Buffer.alloc(1 << 20)
|
|
37
|
+
for (let pos = 0; ; ) {
|
|
38
|
+
const na = readSync(fa, ba, 0, ba.length, pos)
|
|
39
|
+
const nb = readSync(fb, bb, 0, bb.length, pos)
|
|
40
|
+
if (na !== nb || !ba.subarray(0, na).equals(bb.subarray(0, nb))) return false
|
|
41
|
+
if (na === 0) return true
|
|
42
|
+
pos += na
|
|
43
|
+
}
|
|
44
|
+
} finally {
|
|
45
|
+
closeSync(fa)
|
|
46
|
+
closeSync(fb)
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const alive = (pid: number): boolean => {
|
|
51
|
+
try {
|
|
52
|
+
return pid > 0 && process.kill(pid, 0)
|
|
53
|
+
} catch (e) {
|
|
54
|
+
return (e as NodeJS.ErrnoException).code === 'EPERM'
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** a staging folder shipkit made and marked (`<target>.<pid>.part/` holding PART_MARK) whose process is gone: what
|
|
59
|
+
* it holds is always a second copy. A user's own `name.<digits>.part` is never one */
|
|
60
|
+
export const deadStage = (dir: string): boolean => {
|
|
61
|
+
const m = /\.(\d+)\.part$/.exec(dir)
|
|
62
|
+
return m !== null && existsSync(join(dir, PART_MARK)) && !alive(Number(m[1]))
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** one merge's writes into `to`: each file copied to a staging folder beside it (`<to>.<pid>.part/`, never the live
|
|
66
|
+
* folder), its times kept, fsynced, then renamed into place; a loser renamed beside its winner */
|
|
67
|
+
class Writer {
|
|
68
|
+
private n = 0
|
|
69
|
+
readonly stage: string
|
|
70
|
+
readonly kept: string[] = []
|
|
71
|
+
constructor(
|
|
72
|
+
readonly to: string,
|
|
73
|
+
private readonly stamp: string,
|
|
74
|
+
) {
|
|
75
|
+
this.stage = `${to}.${process.pid}.part`
|
|
76
|
+
for (const name of existsSync(dirname(to)) ? readdirSync(dirname(to)) : []) {
|
|
77
|
+
const p = join(dirname(to), name)
|
|
78
|
+
if (name.startsWith(`${basename(to)}.`) && p !== this.stage && deadStage(p)) rmSync(p, { recursive: true, force: true })
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/** `src` at `dst`, replacing what is there in one rename. What landed: a file a writer appends to grows between
|
|
82
|
+
* its stat and its copy, so the copy's own size is what the manifest notes */
|
|
83
|
+
copy(src: string, dst: string, s: Stats): Mark {
|
|
84
|
+
// a drive pulled mid copy: its folders are never made again on the Mac's disk
|
|
85
|
+
if (!existsSync(this.to)) throw new Error(`${this.to} is gone: the annex left during the copy`)
|
|
86
|
+
if (!existsSync(this.stage)) stageOf(this.to)
|
|
87
|
+
const part = join(this.stage, String(this.n++))
|
|
88
|
+
copyFileSync(src, part)
|
|
89
|
+
utimesSync(part, s.atime, s.mtime)
|
|
90
|
+
syncFile(part)
|
|
91
|
+
const landed = markOf(lstatSync(part))
|
|
92
|
+
mkdirSync(dirname(dst), { recursive: true })
|
|
93
|
+
renameSync(part, dst)
|
|
94
|
+
return landed
|
|
95
|
+
}
|
|
96
|
+
/** the name a loser of `dst` takes beside it */
|
|
97
|
+
beside(dst: string, side: 'mac' | 'annex'): string {
|
|
98
|
+
let p = `${dst}.${side}-${this.stamp}`
|
|
99
|
+
for (let i = 2; lstatSync(p, { throwIfNoEntry: false }); i++) p = `${dst}.${side}-${this.stamp}-${i}`
|
|
100
|
+
this.kept.push(p)
|
|
101
|
+
return p
|
|
102
|
+
}
|
|
103
|
+
done(): void {
|
|
104
|
+
rmSync(this.stage, { recursive: true, force: true })
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const same = (d: Stats, s: Stats, src: string, dst: string): boolean => d.isFile() && (sameMark(markOf(d), markOf(s)) || (d.size === s.size && sameBytes(src, dst)))
|
|
109
|
+
|
|
110
|
+
/** every file of the live folder `from` onto the annex at `to`, before the swap. A file both hold in two versions:
|
|
111
|
+
* `from`'s takes the name (it was written later: a spill while the drive was away, the Mac's live folder), the
|
|
112
|
+
* annex's is kept beside it. What it handled, by path under `from`, is the manifest the merge after the swap reads */
|
|
113
|
+
function copyIn(from: string, to: string, w: Writer): { seen: Seen; missed: string[] } {
|
|
114
|
+
const out = { seen: {} as Seen, missed: [] as string[] }
|
|
115
|
+
mkdirSync(to, { recursive: true })
|
|
116
|
+
for (const e of entries(from)) {
|
|
117
|
+
const src = join(from, e.rel)
|
|
118
|
+
const dst = join(to, e.rel)
|
|
119
|
+
try {
|
|
120
|
+
const s = lstatSync(src, { throwIfNoEntry: false })
|
|
121
|
+
if (!s) continue
|
|
122
|
+
const d = lstatSync(dst, { throwIfNoEntry: false })
|
|
123
|
+
if (s.isDirectory()) {
|
|
124
|
+
if (d && !d.isDirectory()) renameSync(dst, w.beside(dst, 'annex'))
|
|
125
|
+
mkdirSync(dst, { recursive: true })
|
|
126
|
+
continue
|
|
127
|
+
}
|
|
128
|
+
if (s.isSymbolicLink()) {
|
|
129
|
+
const want = readlinkSync(src)
|
|
130
|
+
// a folder offloaded inside this one: its link points at the very place it lands, its content is there
|
|
131
|
+
if (resolve(dirname(src), want) === dst) continue
|
|
132
|
+
if (!(d?.isSymbolicLink() && readlinkSync(dst) === want)) {
|
|
133
|
+
if (d) renameSync(dst, w.beside(dst, 'annex'))
|
|
134
|
+
symlinkSync(want, dst)
|
|
135
|
+
}
|
|
136
|
+
out.seen[e.rel] = { mac: markOf(s), annex: markOf(s) }
|
|
137
|
+
continue
|
|
138
|
+
}
|
|
139
|
+
if (!s.isFile()) continue
|
|
140
|
+
if (d && same(d, s, src, dst)) out.seen[e.rel] = { mac: markOf(s), annex: markOf(d) }
|
|
141
|
+
else {
|
|
142
|
+
if (d) renameSync(dst, w.beside(dst, 'annex'))
|
|
143
|
+
out.seen[e.rel] = { mac: markOf(s), annex: w.copy(src, dst, s) }
|
|
144
|
+
}
|
|
145
|
+
} catch (err) {
|
|
146
|
+
if (existsSync(src)) out.missed.push(`${e.rel} (${(err as Error).message})`)
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return out
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** what changed in the aside since the move last handled it (`seen`, empty for an aside no copy noted: every file of
|
|
153
|
+
* it counts as changed), merged into `to`, which tools wrote through the link since the swap; `seen` follows what it
|
|
154
|
+
* does, so a pass that waits on an open file hands the next one the truth:
|
|
155
|
+
* - the Mac's unchanged since handled: nothing (a file a tool deleted on the annex since does not come back);
|
|
156
|
+
* - the Mac's changed, the annex's untouched since: the Mac's takes the name;
|
|
157
|
+
* - new on the Mac, nothing on the annex: copied in;
|
|
158
|
+
* - both changed: the annex's keeps the name (written through the link, after), the Mac's is kept beside it */
|
|
159
|
+
function mergeDelta(aside: string, to: string, seen: Seen, w: Writer): string[] {
|
|
160
|
+
const missed: string[] = []
|
|
161
|
+
for (const e of entries(aside)) {
|
|
162
|
+
if (e.dir) continue
|
|
163
|
+
const src = join(aside, e.rel)
|
|
164
|
+
const dst = join(to, e.rel)
|
|
165
|
+
try {
|
|
166
|
+
const s = lstatSync(src, { throwIfNoEntry: false })
|
|
167
|
+
if (!s) continue
|
|
168
|
+
const was = seen[e.rel]
|
|
169
|
+
if (was && sameMark(markOf(s), was.mac)) continue
|
|
170
|
+
const d = lstatSync(dst, { throwIfNoEntry: false })
|
|
171
|
+
if (s.isSymbolicLink()) {
|
|
172
|
+
if (!d && !was && resolve(dirname(src), readlinkSync(src)) !== dst) {
|
|
173
|
+
mkdirSync(dirname(dst), { recursive: true })
|
|
174
|
+
symlinkSync(readlinkSync(src), dst)
|
|
175
|
+
}
|
|
176
|
+
seen[e.rel] = { mac: markOf(s), annex: markOf(s) }
|
|
177
|
+
continue
|
|
178
|
+
}
|
|
179
|
+
if (!s.isFile()) continue
|
|
180
|
+
let annex: Mark | null
|
|
181
|
+
if (!d) {
|
|
182
|
+
if (was) w.copy(src, w.beside(dst, 'mac'), s)
|
|
183
|
+
annex = was ? null : w.copy(src, dst, s)
|
|
184
|
+
} else if (same(d, s, src, dst)) annex = markOf(d)
|
|
185
|
+
else if (was?.annex && d.isFile() && sameMark(markOf(d), was.annex)) annex = w.copy(src, dst, s)
|
|
186
|
+
else {
|
|
187
|
+
w.copy(src, w.beside(dst, 'mac'), s)
|
|
188
|
+
annex = markOf(d)
|
|
189
|
+
}
|
|
190
|
+
seen[e.rel] = { mac: markOf(s), annex }
|
|
191
|
+
} catch (err) {
|
|
192
|
+
if (existsSync(src)) missed.push(`${e.rel} (${(err as Error).message})`)
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return missed
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** the files under `dir` some process holds open: one `lsof +D` (about 4 s on 84 000 files, against 38 s by lists) */
|
|
199
|
+
export function openUnder(dir: string): Set<string> {
|
|
200
|
+
const r = Bun.spawnSync(['lsof', '-Fn', '+D', dir], { stdout: 'pipe', stderr: 'ignore' })
|
|
201
|
+
return new Set(r.stdout.toString().split('\n').filter((l) => l.startsWith('n/')).map((l) => l.slice(1)))
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export type Pass = Checks & {
|
|
205
|
+
say: Say
|
|
206
|
+
/** the open files under a folder (`openUnder` by default) */
|
|
207
|
+
openUnder?: (dir: string) => Set<string>
|
|
208
|
+
now?: number
|
|
209
|
+
/** a hand started the pass: a line said before is said again */
|
|
210
|
+
loud?: boolean
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const recKey = (r: Pick<Offload, 'repo' | 'rel'>): string => `${r.repo}\0${r.rel}`
|
|
214
|
+
const update = (rec: Offload, patch: Partial<Offload>): Offload => {
|
|
215
|
+
const next = { ...rec, ...patch }
|
|
216
|
+
for (const k of Object.keys(patch) as (keyof Offload)[]) if (patch[k] === undefined) delete next[k]
|
|
217
|
+
editOffloads((list) => [...list.filter((x) => recKey(x) !== recKey(rec)), next])
|
|
218
|
+
return next
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/** the manifests of the moves in flight, `{ [repo\0rel]: Seen }`: what each move handled of each file */
|
|
222
|
+
const COPIED = 'annex-copied'
|
|
223
|
+
const copiedOf = (rec: Offload): Seen | null => machineGet<Record<string, Seen>>(COPIED)?.[recKey(rec)] ?? null
|
|
224
|
+
function setCopied(rec: Offload, seen: Seen | null): void {
|
|
225
|
+
const all = machineRead<Record<string, Seen>>(COPIED) ?? {}
|
|
226
|
+
if (seen === null && !(recKey(rec) in all)) return
|
|
227
|
+
if (seen === null) delete all[recKey(rec)]
|
|
228
|
+
else all[recKey(rec)] = seen
|
|
229
|
+
if (!machineSet(COPIED, all)) throw new Error('the machine db does not open: the copy of the move is not noted')
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** a line said once while it stays the same, in the machine db: a pass runs every 30 s, its log must not repeat. A
|
|
233
|
+
* pass a hand started (`loud`) says it again */
|
|
234
|
+
export function sayOnce(key: string, line: string | null, say: Say, loud = false): void {
|
|
235
|
+
const said = machineGet<Record<string, string>>('annex-said') ?? {}
|
|
236
|
+
if (loud && line !== null) say(line)
|
|
237
|
+
if ((said[key] ?? null) === line) return
|
|
238
|
+
if (line === null) delete said[key]
|
|
239
|
+
else {
|
|
240
|
+
said[key] = line
|
|
241
|
+
if (!loud) say(line)
|
|
242
|
+
}
|
|
243
|
+
machineSet('annex-said', said)
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/** the annex `target` sits on is mounted and marked: its listed root, or the part before `/offload/` when the list did
|
|
247
|
+
* not load. Never that the target path exists: a copy cut by an unplug can leave a phantom of it */
|
|
248
|
+
const markedAt = (specs: readonly AnnexSpec[], target: string): boolean => {
|
|
249
|
+
const root = specs.find((a) => target.startsWith(`${a.path}/`))?.path ?? target.slice(0, Math.max(0, target.indexOf('/offload/')))
|
|
250
|
+
return root !== '' && isPresent({ path: root })
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/** the record's losers that still exist, with the writer's new ones */
|
|
254
|
+
const withKept = (rec: Offload, w: Writer): Offload => {
|
|
255
|
+
if (w.kept.length === 0) return rec
|
|
256
|
+
return update(rec, { kept: [...new Set([...(rec.kept ?? []).filter((p) => lstatSync(p, { throwIfNoEntry: false })), ...w.kept])] })
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/** the aside of a move merged into the annex, then deleted once it stopped moving, nothing holds it open and the
|
|
260
|
+
* annex is flushed. A kill at any step before leaves a state this completes: the aside recorded and never renamed,
|
|
261
|
+
* renamed with no link yet, or linked */
|
|
262
|
+
export function finishAside(rec: Offload, o: Pass): Offload {
|
|
263
|
+
const aside = rec.aside
|
|
264
|
+
if (aside === undefined) return rec
|
|
265
|
+
const key = `aside:${recKey(rec)}`
|
|
266
|
+
if (!existsSync(aside)) {
|
|
267
|
+
setCopied(rec, null)
|
|
268
|
+
return update(rec, { aside: undefined })
|
|
269
|
+
}
|
|
270
|
+
const at = join(rec.repo, rec.rel)
|
|
271
|
+
if (!lstatSync(at, { throwIfNoEntry: false })) {
|
|
272
|
+
mkdirSync(rec.target, { recursive: true })
|
|
273
|
+
symlinkSync(rec.target, at)
|
|
274
|
+
}
|
|
275
|
+
const w = new Writer(rec.target, stampOf(o.now ?? Date.now()))
|
|
276
|
+
const before = tally(aside)
|
|
277
|
+
const seen = copiedOf(rec) ?? {}
|
|
278
|
+
const missed = mergeDelta(aside, rec.target, seen, w)
|
|
279
|
+
w.done()
|
|
280
|
+
rec = withKept(rec, w)
|
|
281
|
+
const after = tally(aside)
|
|
282
|
+
let waits: string | null = null
|
|
283
|
+
if (missed.length > 0) waits = `WARN annex: ${rec.rel} of ${rec.repo}: ${missed.length} files of the Mac's copy did not land on the annex (first: ${missed[0]}); it stays at ${aside}, tried again next pass`
|
|
284
|
+
else if (after.files !== before.files || after.bytes !== before.bytes) waits = `annex: ${rec.rel} of ${rec.repo}: the Mac's copy moved during its merge, kept at ${aside} for the next pass`
|
|
285
|
+
else {
|
|
286
|
+
const open = (o.openUnder ?? openUnder)(aside)
|
|
287
|
+
if (open.size > 0) waits = `annex: ${rec.rel} of ${rec.repo}: a process holds ${[...open][0]} open, the Mac's copy waits at ${aside}`
|
|
288
|
+
}
|
|
289
|
+
if (waits !== null) {
|
|
290
|
+
setCopied(rec, seen)
|
|
291
|
+
sayOnce(key, waits, o.say, o.loud)
|
|
292
|
+
return rec
|
|
293
|
+
}
|
|
294
|
+
;(o.flush ?? flushDrive)(rec.target)
|
|
295
|
+
rmSync(aside, { recursive: true, force: true })
|
|
296
|
+
setCopied(rec, null)
|
|
297
|
+
sayOnce(key, null, o.say, o.loud)
|
|
298
|
+
const kept = (rec.kept ?? []).length
|
|
299
|
+
o.say(`ok: annex: ${rec.rel} of ${rec.repo} is on the annex (${before.files} files, ${(before.bytes / GB).toFixed(1)} GB moved off the Mac)${kept > 0 ? `; ${kept} files kept beside another version of them: sk annex names them` : ''}`)
|
|
300
|
+
return update(rec, { aside: undefined, files: before.files, bytes: before.bytes })
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/** the records of folders offloaded inside a born one, dropped once its link holds: their content sits at its place
|
|
304
|
+
* under the born target, their own path resolves there, and left alone each would read as a split. Never before:
|
|
305
|
+
* until the link holds, they are the only record of that content */
|
|
306
|
+
function absorb(rec: Offload): void {
|
|
307
|
+
const nested = (x: Offload): boolean => x.repo === rec.repo && x.rel.startsWith(`${rec.rel}/`) && x.target === join(rec.target, x.rel.slice(rec.rel.length + 1))
|
|
308
|
+
if (offloads().some(nested)) editOffloads((list) => list.filter((x) => !nested(x)))
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/** the real folder at `rel` copied to `rec.target` and flushed, then swapped for the link to it, then its aside
|
|
312
|
+
* merged. The manifest is saved only for bytes the drive has, and with the aside before the rename; the spill flag
|
|
313
|
+
* leaves, and `held` comes, only once the link holds */
|
|
314
|
+
function swapIn(rec: Offload, o: Pass): Offload {
|
|
315
|
+
const at = join(rec.repo, rec.rel)
|
|
316
|
+
const w = new Writer(rec.target, stampOf(o.now ?? Date.now()))
|
|
317
|
+
const c = copyIn(at, rec.target, w)
|
|
318
|
+
w.done()
|
|
319
|
+
rec = withKept(rec, w)
|
|
320
|
+
if (c.missed.length > 0) throw new Error(`${c.missed.length} files did not copy to the annex (first: ${c.missed[0]}), the folder stays on the Mac`)
|
|
321
|
+
;(o.flush ?? flushDrive)(rec.target)
|
|
322
|
+
const aside = `${at}.${process.pid}.moving`
|
|
323
|
+
setCopied(rec, c.seen)
|
|
324
|
+
rec = update(rec, { aside })
|
|
325
|
+
renameSync(at, aside)
|
|
326
|
+
try {
|
|
327
|
+
symlinkSync(rec.target, at)
|
|
328
|
+
} catch (e) {
|
|
329
|
+
renameSync(aside, at)
|
|
330
|
+
update(rec, { aside: undefined })
|
|
331
|
+
throw e
|
|
332
|
+
}
|
|
333
|
+
rec = update(rec, { spill: undefined, held: true })
|
|
334
|
+
absorb(rec)
|
|
335
|
+
return finishAside(rec, o)
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/** one record brought in line with the drive: a dangling link becomes a local spill while no annex is present (its
|
|
339
|
+
* flag saved first), a spill goes back once one is, a move left half done is finished (an aside no record names is
|
|
340
|
+
* adopted by the record of its folder; a first copy cut short is copied again), a born folder whose link held and
|
|
341
|
+
* that a tool wiped stays wiped (its old annex content to the trash, the new folder moved in), a missing born link
|
|
342
|
+
* made again */
|
|
343
|
+
export function settle(rec: Offload, specs: readonly AnnexSpec[], o: Pass): Offload {
|
|
344
|
+
const at = join(rec.repo, rec.rel)
|
|
345
|
+
if (!existsSync(rec.repo)) return rec
|
|
346
|
+
const up = markedAt(specs, rec.target)
|
|
347
|
+
// a recorded aside that is not there: killed before its rename, so the real folder is no wipe
|
|
348
|
+
const interrupted = rec.aside !== undefined && !existsSync(rec.aside)
|
|
349
|
+
if (rec.aside === undefined) {
|
|
350
|
+
const orphan = asidesOf(rec.repo, rec.rel)[0]
|
|
351
|
+
if (orphan !== undefined) rec = update(rec, { aside: orphan })
|
|
352
|
+
}
|
|
353
|
+
let l = lstatSync(at, { throwIfNoEntry: false })
|
|
354
|
+
const now = o.now ?? Date.now()
|
|
355
|
+
if (!up) {
|
|
356
|
+
const aside = rec.aside
|
|
357
|
+
if (aside !== undefined && existsSync(aside) && !l) {
|
|
358
|
+
rec = update(rec, { spill: rec.spill ?? now })
|
|
359
|
+
renameSync(aside, at)
|
|
360
|
+
setCopied(rec, null)
|
|
361
|
+
o.say(`WARN annex: no annex holds ${rec.target}: ${rec.rel} of ${rec.repo}, its move stopped, spills on the Mac until the drive returns`)
|
|
362
|
+
return update(rec, { aside: undefined })
|
|
363
|
+
}
|
|
364
|
+
const dangling = l?.isSymbolicLink() === true && !existsSync(at)
|
|
365
|
+
if ((dangling || !l) && (rec.born || rec.spill === undefined)) {
|
|
366
|
+
// the flag first: a spill folder no record names would read as a wipe when the drive returns
|
|
367
|
+
if (rec.spill === undefined) rec = update(rec, { spill: now })
|
|
368
|
+
if (dangling) rmSync(at)
|
|
369
|
+
mkdirSync(at, { recursive: true })
|
|
370
|
+
o.say(`WARN annex: no annex holds ${rec.target}: ${rec.rel} of ${rec.repo} spills on the Mac until the drive returns, then the daemon merges it back`)
|
|
371
|
+
} else if (l?.isDirectory() && rec.born && rec.held === true && rec.spill === undefined) {
|
|
372
|
+
// a tool replaced the dangling link with a real folder before this pass: written while the drive was away,
|
|
373
|
+
// it is a spill, never a wipe of what the annex holds
|
|
374
|
+
rec = update(rec, { spill: now })
|
|
375
|
+
o.say(`WARN annex: no annex holds ${rec.target}: ${rec.rel} of ${rec.repo}, a real folder made while the drive is away, spills on the Mac until the drive returns`)
|
|
376
|
+
}
|
|
377
|
+
return rec
|
|
378
|
+
}
|
|
379
|
+
if (rec.aside !== undefined) rec = finishAside(rec, o)
|
|
380
|
+
if (rec.aside !== undefined) return rec
|
|
381
|
+
l = lstatSync(at, { throwIfNoEntry: false })
|
|
382
|
+
if (l?.isSymbolicLink()) {
|
|
383
|
+
if (!existsSync(at) && rec.born) mkdirSync(rec.target, { recursive: true })
|
|
384
|
+
if (existsSync(at) && (rec.spill !== undefined || rec.held !== true)) {
|
|
385
|
+
rec = update(rec, { spill: undefined, held: true })
|
|
386
|
+
if (rec.born) absorb(rec)
|
|
387
|
+
}
|
|
388
|
+
return rec
|
|
389
|
+
}
|
|
390
|
+
if (l?.isDirectory() && (rec.spill !== undefined || interrupted || (rec.born && rec.held !== true))) {
|
|
391
|
+
const t = tally(at)
|
|
392
|
+
const why = rec.spill !== undefined ? 'its spill' : 'a move that did not finish'
|
|
393
|
+
o.say(`ok: annex: ${rec.rel} of ${rec.repo} goes on the annex (${why}: ${t.files} files, ${(t.bytes / GB).toFixed(1)} GB)`)
|
|
394
|
+
return swapIn(rec, o)
|
|
395
|
+
}
|
|
396
|
+
if (l?.isDirectory() && rec.born) {
|
|
397
|
+
const trash = toTrash(rec, now)
|
|
398
|
+
const t = tally(at)
|
|
399
|
+
o.say(`ok: annex: ${rec.rel} of ${rec.repo} was wiped on the Mac and stays wiped${trash ? `: its old annex content is in ${trash}` : ''}; the new folder goes on the annex (${t.files} files)`)
|
|
400
|
+
return swapIn(rec, o)
|
|
401
|
+
}
|
|
402
|
+
if (!l && rec.born) {
|
|
403
|
+
mkdirSync(rec.target, { recursive: true })
|
|
404
|
+
mkdirSync(dirname(at), { recursive: true })
|
|
405
|
+
symlinkSync(rec.target, at)
|
|
406
|
+
if (rec.held !== true) rec = update(rec, { held: true })
|
|
407
|
+
}
|
|
408
|
+
return rec
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/** why `rel` of `root`, declared born, may not go on the annex now (the words after "<rel> of <root> "), or where it
|
|
412
|
+
* goes: the real pass and the dry run read the same answer */
|
|
413
|
+
export async function bornCheck(root: string, rel: string, specs: readonly AnnexSpec[], o: Pass): Promise<{ why: string } | { target: string; exists: boolean }> {
|
|
414
|
+
if (!inScratch(rel)) return { why: 'is declared born on the annex, and only .shipkit/cache and .shipkit/tmp folders are scratch' }
|
|
415
|
+
const at = join(root, rel)
|
|
416
|
+
const parts = rel.split('/')
|
|
417
|
+
const parent = parts.map((_, i) => parts.slice(0, i).join('/')).find((p) => p !== '' && lstatSync(join(root, p), { throwIfNoEntry: false })?.isSymbolicLink() === true)
|
|
418
|
+
if (parent !== undefined) return { why: `is declared born on the annex, and sits under ${parent}, a link (an offloaded folder): it is on the annex already; drop it from born, or bring ${parent} back first` }
|
|
419
|
+
const l = lstatSync(at, { throwIfNoEntry: false })
|
|
420
|
+
if (l?.isSymbolicLink()) return { why: 'is declared born on the annex, and is a symlink no record knows: look, then remove it' }
|
|
421
|
+
if (l) {
|
|
422
|
+
const why = await refusal(root, rel, { ...o, open: false })
|
|
423
|
+
if (why) return { why: `stays on the Mac: ${why}` }
|
|
424
|
+
} else if (!(o.git ?? git)(root, ['check-ignore', '-q', '--', rel]).ok) return { why: 'is declared born on the annex, and git does not ignore it' }
|
|
425
|
+
const bytes = l ? tally(at).bytes : 0
|
|
426
|
+
const annex = specs.find((a) => isPresent(a) && driveFree(a.path) - bytes >= keepFree(a))
|
|
427
|
+
if (!annex) return { why: 'is declared born on the annex, and no annex is present with room: it stays on the Mac' }
|
|
428
|
+
const target = join(annex.path, 'offload', basename(root), rel)
|
|
429
|
+
const owner = ownerOf(root, target)
|
|
430
|
+
if (owner) return { why: `stays on the Mac: another repo (${owner.repo}) holds ${owner.target} on the annex, two repos of one folder name` }
|
|
431
|
+
return { target, exists: l !== undefined }
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
/** the born folders a pass could not link, with why, as last said: `sk annex` prints them, so none waits in silence */
|
|
435
|
+
export function waitingBorn(): string[] {
|
|
436
|
+
return Object.entries(machineGet<Record<string, string>>('annex-said') ?? {})
|
|
437
|
+
.filter(([k]) => k.startsWith('born:'))
|
|
438
|
+
.map(([, line]) => line)
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/** `rel` of `root`, declared born: linked to the annex, its Mac folder moved copy first. Its record, or null with the
|
|
442
|
+
* reason said once (no annex present, a refusal) */
|
|
443
|
+
export async function bear(root: string, rel: string, specs: readonly AnnexSpec[], o: Pass): Promise<Offload | null> {
|
|
444
|
+
const key = `born:${root}\0${rel}`
|
|
445
|
+
const had = offloads().find((x) => x.repo === root && x.rel === rel)
|
|
446
|
+
if (had) return settle(had.born ? had : update(had, { born: true }), specs, o)
|
|
447
|
+
const c = await bornCheck(root, rel, specs, o)
|
|
448
|
+
if ('why' in c) {
|
|
449
|
+
sayOnce(key, `WARN annex: ${rel} of ${root} ${c.why}`, o.say, o.loud)
|
|
450
|
+
return null
|
|
451
|
+
}
|
|
452
|
+
sayOnce(key, null, o.say, o.loud)
|
|
453
|
+
const at = join(root, rel)
|
|
454
|
+
const rec: Offload = { repo: root, rel, target: c.target, files: 0, bytes: 0, at: o.now ?? Date.now(), born: true }
|
|
455
|
+
editOffloads((list) => [...list.filter((x) => recKey(x) !== recKey(rec)), rec])
|
|
456
|
+
if (!c.exists) {
|
|
457
|
+
mkdirSync(rec.target, { recursive: true })
|
|
458
|
+
mkdirSync(dirname(at), { recursive: true })
|
|
459
|
+
symlinkSync(rec.target, at)
|
|
460
|
+
o.say(`ok: annex: ${rel} of ${root} is born on the annex: ${rec.target}`)
|
|
461
|
+
return update(rec, { held: true })
|
|
462
|
+
}
|
|
463
|
+
return swapIn(rec, o)
|
|
464
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// what a move wrote is on the drive itself before a Mac copy goes (wiki tools/shipkit/annex.md): each copied file
|
|
2
|
+
// fsynced as it lands, then one F_FULLFSYNC on the annex, which on macOS also empties the drive's own cache (a plain
|
|
3
|
+
// fsync leaves it there, and a USB drive pulled seconds after a merge loses it)
|
|
4
|
+
import { closeSync, fsyncSync, openSync } from 'node:fs'
|
|
5
|
+
|
|
6
|
+
/** F_FULLFSYNC of <sys/fcntl.h> on darwin */
|
|
7
|
+
const F_FULLFSYNC = 51
|
|
8
|
+
|
|
9
|
+
let fcntl: ((fd: number, cmd: number) => number) | null | undefined
|
|
10
|
+
|
|
11
|
+
function fullFsync(): ((fd: number, cmd: number) => number) | null {
|
|
12
|
+
if (fcntl !== undefined) return fcntl
|
|
13
|
+
fcntl = null
|
|
14
|
+
if (process.platform !== 'darwin') return null
|
|
15
|
+
try {
|
|
16
|
+
const { dlopen } = require('bun:ffi') as typeof import('bun:ffi')
|
|
17
|
+
// fcntl is variadic: F_FULLFSYNC takes no third argument, so the two fixed ones are the whole call
|
|
18
|
+
const lib = dlopen('/usr/lib/libSystem.B.dylib', { fcntl: { args: ['i32', 'i32'], returns: 'i32' } })
|
|
19
|
+
fcntl = (fd, cmd) => lib.symbols.fcntl(fd, cmd)
|
|
20
|
+
} catch {
|
|
21
|
+
fcntl = null
|
|
22
|
+
}
|
|
23
|
+
return fcntl
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** one file's bytes handed to its drive */
|
|
27
|
+
export function syncFile(path: string): void {
|
|
28
|
+
const fd = openSync(path, 'r')
|
|
29
|
+
try {
|
|
30
|
+
fsyncSync(fd)
|
|
31
|
+
} finally {
|
|
32
|
+
closeSync(fd)
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** everything written on the drive that holds `dir` out of every cache, the drive's own included: it throws when that
|
|
37
|
+
* fails, so no Mac copy goes after it. `fcntl`: the call (F_FULLFSYNC on macOS; none elsewhere, a plain fsync) */
|
|
38
|
+
export function flushDrive(dir: string, fcntl: ((fd: number, cmd: number) => number) | null = fullFsync()): void {
|
|
39
|
+
const fd = openSync(dir, 'r')
|
|
40
|
+
try {
|
|
41
|
+
if (fcntl === null) fsyncSync(fd)
|
|
42
|
+
else if (fcntl(fd, F_FULLFSYNC) !== 0) throw new Error(`F_FULLFSYNC failed on ${dir}: the drive may still hold the copy in its cache, no Mac copy goes`)
|
|
43
|
+
} finally {
|
|
44
|
+
closeSync(fd)
|
|
45
|
+
}
|
|
46
|
+
}
|