rvlib-shipkit 2.23.0 → 2.24.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 +25 -0
- package/package.json +1 -1
- package/src/agent-time/render.ts +28 -14
- package/src/agent-time/report.ts +46 -25
- package/src/agent-time/store.ts +30 -10
- package/src/agent-time/timeline.ts +43 -15
- package/src/asks/asks-queue.ts +32 -11
- package/src/checks/SK044-behind-upstream.check.ts +38 -0
- package/src/cli/cmd_Asks.ts +2 -0
- package/src/cli/cmd_Backup.ts +7 -5
- package/src/cli/cmd_Bump.ts +1 -0
- package/src/cli/cmd_Capacity.ts +26 -0
- package/src/cli/cmd_Check.ts +1 -0
- package/src/cli/cmd_Checks.ts +1 -0
- package/src/cli/cmd_Ci.ts +1 -0
- package/src/cli/cmd_Commands.ts +50 -14
- package/src/cli/cmd_Home.ts +0 -29
- package/src/cli/cmd_Hooks.ts +1 -0
- package/src/cli/cmd_Mv.ts +1 -0
- package/src/cli/cmd_Reflect.ts +1 -0
- package/src/cli/cmd_Releases.ts +1 -0
- package/src/cli/cmd_Review.ts +97 -14
- package/src/cli/cmd_Secret.ts +41 -2
- package/src/cli/cmd_Shot.ts +1 -0
- package/src/cli/cmd_Status.ts +1 -0
- package/src/cli/cmd_Time.ts +3 -1
- package/src/cli/cmd_Tmp.ts +82 -0
- package/src/cli/cmd_Wiki.ts +1 -0
- package/src/cli/registry.ts +13 -1
- package/src/collections/pings.ts +1 -1
- package/src/core/capacity.ts +71 -0
- package/src/core/check-runner.ts +86 -15
- package/src/core/motherhome.ts +67 -9
- package/src/core/slots.ts +49 -12
- package/src/core/trace-audit.ts +4 -1
- package/src/drill/run.ts +5 -2
- package/src/home/gui-app/ShipkitGui.swift +1 -0
- package/src/home/home-launchd.ts +11 -3
- package/src/home/home-routes.ts +11 -4
- package/src/home/machine-host.ts +65 -0
- package/src/home/machine-stores.ts +19 -17
- package/src/home/machine-temp.ts +493 -0
- package/src/home/other-macs.ts +74 -0
- package/src/home/run-daemon.ts +10 -2
- package/src/home/serve-web.ts +2 -2
- package/src/home/via-hub.ts +6 -1
- package/src/init-repo.ts +7 -3
- package/src/ledger/ledger.ts +6 -0
- package/src/ledger/store.ts +6 -0
- package/src/media-store/backup/keys.ts +21 -6
- package/src/media-store/backup/manifest.ts +52 -11
- package/src/media-store/backup/restore.ts +17 -10
- package/src/media-store/backup/run.ts +5 -2
- package/src/media-store/backup/sources.ts +2 -1
- package/src/mv/mv-md.ts +121 -42
- package/src/reflect/transcript.ts +56 -11
- package/src/releases/from-card.ts +45 -3
- package/src/releases/model.ts +6 -0
- package/src/releases/store.ts +8 -1
- package/src/repo-config.ts +5 -3
- package/src/review/cells.ts +24 -3
- package/src/review/format.ts +33 -9
- package/src/review/media-kind.ts +14 -11
- package/src/review/media-read.ts +6 -5
- package/src/review/model.ts +71 -3
- package/src/review/paste.ts +2 -1
- package/src/review/previews.ts +123 -36
- package/src/review/routes.ts +29 -18
- package/src/review/store.ts +74 -12
- package/src/review/threads.ts +7 -5
- package/src/secrets/copy.ts +110 -0
- package/src/secrets/list.ts +17 -7
- package/src/serve/hub-app/home-strip.css +5 -0
- package/src/serve/hub-app/home-strip.tsx +18 -0
- package/src/serve/hub-app/hub-app.tsx +2 -1
- package/src/serve/hub-app/hub.css +14 -0
- package/src/serve/hub-app/inbox.css +1 -0
- package/src/serve/hub-app/pull-refresh.tsx +88 -0
- package/src/serve/hub-app/review/review-cells.tsx +52 -13
- package/src/serve/hub-app/review/review-list.tsx +6 -1
- package/src/serve/hub-app/review/review-st.ts +6 -5
- package/src/serve/hub-app/review/review-thread.tsx +3 -1
- package/src/serve/hub-app/review/review-views.tsx +18 -11
- package/src/serve/hub-app/review/thread-st.ts +82 -14
- package/src/serve/hub-app/review.css +15 -2
- package/src/serve/hub-runtime.ts +2 -1
- package/src/serve/run-serve.ts +4 -2
- package/src/serve/serve-hub.ts +9 -2
- package/src/suite/child-tools.ts +45 -0
- package/src/suite/judge-suites.ts +10 -22
- package/src/suite/select.ts +83 -8
- package/src/suite/suite-cli.ts +38 -14
- package/src/suite/suite-cores.ts +68 -0
- package/src/suite/suite-reads-preload.ts +8 -6
- package/src/ts/config.ts +44 -0
- package/src/ts/rename.ts +11 -1
- package/src/home/claude-temp.ts +0 -139
package/src/review/previews.ts
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
|
-
// a view's image at the size the page draws it (wiki tools/shipkit/review.md,
|
|
2
|
-
//
|
|
3
|
-
//
|
|
1
|
+
// a view's image at the size the page draws it, a clip as the page plays it (wiki tools/shipkit/review-store.md,
|
|
2
|
+
// Previews, A clip's play copy): made once per media file, kept in the repo's cache
|
|
3
|
+
// (`.shipkit/cache/review-previews/<id>/<name>.<size>.<webp|mp4>`, local, never in the store), made again only when its
|
|
4
|
+
// source is newer. The full file is fetched only on zoom and download
|
|
4
5
|
import { existsSync, mkdirSync, readdirSync, readFileSync, renameSync, rmSync, statSync } from 'node:fs'
|
|
5
6
|
import { dirname, join } from 'node:path'
|
|
6
7
|
import { LAYOUT } from '../core/layout.ts'
|
|
7
|
-
import {
|
|
8
|
+
import { ffmpeg } from '../trailer/ffmpeg.ts'
|
|
9
|
+
import { FILE_RE, firstView, ID_RE, isGallery, isPair, thumbFileOf, type Item, type ReviewItem, type ReviewView } from './model.ts'
|
|
8
10
|
import { probe, stillAt } from './media.ts'
|
|
9
|
-
import { ensureLocal, remotePreviewUrl } from './media-read.ts'
|
|
11
|
+
import { ensureLocal, remotePreviewUrl, remoteUrl } from './media-read.ts'
|
|
10
12
|
|
|
11
13
|
/** `thumb` a list row's picture, `fit` a view in the card column: the widest each is drawn, at 2x */
|
|
12
14
|
export const PREVIEW_W = { thumb: 480, fit: 1600 } as const
|
|
@@ -14,11 +16,26 @@ export type PreviewSize = keyof typeof PREVIEW_W
|
|
|
14
16
|
export const isPreviewSize = (s: string): s is PreviewSize => s in PREVIEW_W
|
|
15
17
|
|
|
16
18
|
const IMAGE = /\.(png|jpe?g|webp)$/i
|
|
19
|
+
export const CLIP = /\.(mp4|webm|mov|m4v)$/i
|
|
20
|
+
/** a clip over this many bits a second plays from its copy: what a phone streams over the tailnet and decodes smoothly */
|
|
21
|
+
export const PLAY_OVER = 25_000_000
|
|
22
|
+
|
|
23
|
+
/** the copy: the clip's own size and frames, each at its own pts, a keyframe every half second, at most 20 Mbit/s */
|
|
24
|
+
const playArgs = (src: string, fps: number, out: string): string[] => [
|
|
25
|
+
'-i', src, '-map', '0:v:0', '-map', '0:a:0?',
|
|
26
|
+
'-vf', 'crop=trunc(iw/2)*2:trunc(ih/2)*2', '-fps_mode', 'passthrough',
|
|
27
|
+
'-c:v', 'libx264', '-preset', 'veryfast', '-profile:v', 'high', '-pix_fmt', 'yuv420p',
|
|
28
|
+
'-crf', '16', '-maxrate', '20M', '-bufsize', '40M', '-g', String(Math.max(1, Math.round(fps / 2))),
|
|
29
|
+
'-c:a', 'aac', '-b:a', '192k', '-movflags', '+faststart', '-f', 'mp4', out,
|
|
30
|
+
]
|
|
17
31
|
|
|
18
32
|
const JOBS = 3
|
|
19
33
|
let running = 0
|
|
20
34
|
const waiting: (() => void)[] = []
|
|
21
35
|
const making = new Map<string, Promise<string | null>>()
|
|
36
|
+
/** copies that failed, by their path and their clip's time: logged once, then the clip itself plays, never encoded
|
|
37
|
+
* again on each range request */
|
|
38
|
+
const failed = new Set<string>()
|
|
22
39
|
|
|
23
40
|
async function slot<T>(work: () => Promise<T>): Promise<T> {
|
|
24
41
|
if (running >= JOBS) await new Promise<void>((go) => waiting.push(go))
|
|
@@ -34,34 +51,75 @@ async function slot<T>(work: () => Promise<T>): Promise<T> {
|
|
|
34
51
|
/** the preview folder of a store: `<repo>/.shipkit/cache/review-previews`, the store being `<repo>/.shipkit/review` */
|
|
35
52
|
export const previewDir = (dir: string): string => join(dirname(dirname(dir)), LAYOUT.cache, 'review-previews')
|
|
36
53
|
|
|
54
|
+
/** what a preview of a file is: a webp of an image, the play copy of a clip; null for any other file or size */
|
|
55
|
+
const kindOf = (name: string, size: PreviewSize): 'image' | 'clip' | null => (IMAGE.test(name) ? 'image' : CLIP.test(name) && size === 'fit' ? 'clip' : null)
|
|
56
|
+
|
|
37
57
|
// a poster's preview may come from a lit frame of its clip: its own name, so the black ones made before are not reused
|
|
38
|
-
const outOf = (dir: string, id: string, name: string, size: PreviewSize): string =>
|
|
58
|
+
const outOf = (dir: string, id: string, name: string, size: PreviewSize): string =>
|
|
59
|
+
join(previewDir(dir), id, CLIP.test(name) ? `${name}.${size}.mp4` : `${name}.${size}${POSTER.test(name) ? '.lit' : ''}.webp`)
|
|
60
|
+
|
|
61
|
+
/** the item.json of a card as a preview reads it: its media facts and its views */
|
|
62
|
+
const headOf = (dir: string, id: string): Pick<Item, 'media' | 'views'> => {
|
|
63
|
+
try {
|
|
64
|
+
return JSON.parse(readFileSync(join(dir, id, 'item.json'), 'utf8')) as Item
|
|
65
|
+
} catch {
|
|
66
|
+
return { views: [] }
|
|
67
|
+
}
|
|
68
|
+
}
|
|
39
69
|
|
|
40
|
-
/** the
|
|
41
|
-
|
|
70
|
+
/** the length in seconds of the clip a view names, from the views alone */
|
|
71
|
+
const lengthOf = (views: readonly ReviewView[], name: string): number | undefined => {
|
|
72
|
+
for (const v of views) if (!isGallery(v) && v.clip && Object.values(v.files).includes(name)) return v.clip.duration
|
|
73
|
+
return undefined
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** a clip of a card over the play line: its bytes from the disk, else from its fact, over the length its view keeps,
|
|
77
|
+
* so a clip the disk let go is judged without fetching it back */
|
|
78
|
+
export function playsCopy(dir: string, id: string, name: string): boolean {
|
|
79
|
+
if (!ID_RE.test(id) || !FILE_RE.test(name) || !CLIP.test(name)) return false
|
|
80
|
+
const head = headOf(dir, id)
|
|
81
|
+
const bytes = statSync(join(dir, id, name), { throwIfNoEntry: false })?.size ?? head.media?.[name]?.size
|
|
82
|
+
const seconds = lengthOf(head.views, name)
|
|
83
|
+
return bytes !== undefined && seconds !== undefined && seconds > 0 && (bytes * 8) / seconds > PLAY_OVER
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** the preview of an item's file at a size, made on the first ask; null when the name is no image or clip of the item
|
|
87
|
+
* or it does not read. A clip under the play line is its own preview, and a clip whose copy failed plays itself. A
|
|
88
|
+
* made preview of a file the disk let go stays good; else the file is fetched back */
|
|
42
89
|
export async function previewFile(dir: string, id: string, name: string, size: PreviewSize): Promise<string | null> {
|
|
43
|
-
if (!ID_RE.test(id) || !FILE_RE.test(name)
|
|
90
|
+
if (!ID_RE.test(id) || !FILE_RE.test(name)) return null
|
|
91
|
+
const kind = kindOf(name, size)
|
|
92
|
+
if (!kind) return null
|
|
93
|
+
if (kind === 'clip' && !playsCopy(dir, id, name)) return ensureLocal(dir, id, name)
|
|
44
94
|
const out = outOf(dir, id, name, size)
|
|
45
95
|
const here = join(dir, id, name)
|
|
46
96
|
if (!existsSync(here) && existsSync(out)) return out
|
|
47
97
|
const src = await ensureLocal(dir, id, name)
|
|
48
98
|
if (!src) return null
|
|
49
|
-
|
|
99
|
+
const at = statSync(src).mtimeMs
|
|
100
|
+
if (existsSync(out) && statSync(out).mtimeMs >= at) return out
|
|
101
|
+
if (failed.has(`${out}@${at}`)) return src
|
|
50
102
|
const pending = making.get(out)
|
|
51
103
|
if (pending) return pending
|
|
52
104
|
const job = slot(async () => {
|
|
53
105
|
mkdirSync(dirname(out), { recursive: true })
|
|
54
106
|
const tmp = `${out}.${process.pid}.tmp`
|
|
55
107
|
try {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
.
|
|
60
|
-
|
|
61
|
-
|
|
108
|
+
if (kind === 'clip') await ffmpeg(playArgs(src, probe(src).video?.fps ?? 30, tmp), { nice: 10 })
|
|
109
|
+
else {
|
|
110
|
+
const { default: sharp } = await import('sharp')
|
|
111
|
+
const still = POSTER.test(name) ? await litStill(dir, id, name, src, out) : src
|
|
112
|
+
await sharp(still === src ? src : readFileSync(still))
|
|
113
|
+
.resize({ width: PREVIEW_W[size], withoutEnlargement: true })
|
|
114
|
+
.webp({ quality: size === 'thumb' ? 75 : 82 })
|
|
115
|
+
.toFile(tmp)
|
|
116
|
+
}
|
|
62
117
|
} catch (e) {
|
|
63
|
-
console.error(`FAIL: review preview ${size} of ${id}/${name}: ${(e as Error).message}`)
|
|
64
|
-
|
|
118
|
+
console.error(`FAIL: review ${kind === 'clip' ? 'play copy' : `preview ${size}`} of ${id}/${name}: ${(e as Error).message}`)
|
|
119
|
+
rmSync(tmp, { force: true })
|
|
120
|
+
if (kind === 'image') return null
|
|
121
|
+
failed.add(`${out}@${at}`)
|
|
122
|
+
return src
|
|
65
123
|
}
|
|
66
124
|
renameSync(tmp, out)
|
|
67
125
|
rmSync(`${out}.still.png`, { force: true })
|
|
@@ -74,18 +132,26 @@ export async function previewFile(dir: string, id: string, name: string, size: P
|
|
|
74
132
|
/** every preview being made has landed: a test removes its folder only after */
|
|
75
133
|
export const previewsSettled = async (): Promise<void> => void (await Promise.all([...making.values()]))
|
|
76
134
|
|
|
77
|
-
/** a preview to answer: its file, or when neither it nor its
|
|
135
|
+
/** a preview to answer: its file, or when neither it nor its source is on the disk, a signed URL of the Space's copy
|
|
136
|
+
* (a clip under the play line: the clip's own) */
|
|
78
137
|
export async function previewOrRemote(dir: string, id: string, name: string, size: PreviewSize): Promise<{ file: string } | { url: string } | null> {
|
|
79
|
-
if (!ID_RE.test(id) || !FILE_RE.test(name)
|
|
80
|
-
|
|
81
|
-
|
|
138
|
+
if (!ID_RE.test(id) || !FILE_RE.test(name)) return null
|
|
139
|
+
const kind = kindOf(name, size)
|
|
140
|
+
if (!kind) return null
|
|
141
|
+
const here = join(dir, id, name)
|
|
142
|
+
if (kind === 'clip' && !playsCopy(dir, id, name)) {
|
|
143
|
+
if (existsSync(here)) return { file: here }
|
|
144
|
+
const url = await remoteUrl(dir, id, name)
|
|
145
|
+
return url ? { url } : null
|
|
146
|
+
}
|
|
147
|
+
if (!existsSync(here) && !existsSync(outOf(dir, id, name, size))) {
|
|
148
|
+
const url = await remotePreviewUrl(dir, id, name, size, kind === 'clip' ? 'mp4' : 'webp')
|
|
82
149
|
if (url) return { url }
|
|
83
150
|
}
|
|
84
151
|
const file = await previewFile(dir, id, name, size)
|
|
85
152
|
return file ? { file } : null
|
|
86
153
|
}
|
|
87
154
|
|
|
88
|
-
|
|
89
155
|
const POSTER = /^(v\d+-)?poster\.png$/
|
|
90
156
|
/** a picture whose brightest channel averages under this is black: a trailer's first frame fades in from black */
|
|
91
157
|
const BLACK = 12
|
|
@@ -105,7 +171,7 @@ async function litStill(dir: string, id: string, name: string, poster: string, o
|
|
|
105
171
|
if (!(await isBlack(poster))) return poster
|
|
106
172
|
const prefix = name.slice(0, name.length - 'poster.png'.length)
|
|
107
173
|
const clipRe = new RegExp(`^${prefix}(video|after)\\.(mp4|webm)$`)
|
|
108
|
-
const clip = readdirSync(join(dir, id)).find((f) => clipRe.test(f)) ?? Object.keys(
|
|
174
|
+
const clip = readdirSync(join(dir, id)).find((f) => clipRe.test(f)) ?? Object.keys(headOf(dir, id).media ?? {}).find((f) => clipRe.test(f))
|
|
109
175
|
const video = clip ? await ensureLocal(dir, id, clip) : null
|
|
110
176
|
if (!video) return poster
|
|
111
177
|
const still = `${out}.still.png`
|
|
@@ -121,15 +187,6 @@ async function litStill(dir: string, id: string, name: string, poster: string, o
|
|
|
121
187
|
return poster
|
|
122
188
|
}
|
|
123
189
|
|
|
124
|
-
/** the media facts a head keeps, by name: what names a file the disk let go */
|
|
125
|
-
const mediaOf = (dir: string, id: string): Record<string, unknown> => {
|
|
126
|
-
try {
|
|
127
|
-
return (JSON.parse(readFileSync(join(dir, id, 'item.json'), 'utf8')) as { media?: Record<string, unknown> }).media ?? {}
|
|
128
|
-
} catch {
|
|
129
|
-
return {}
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
|
|
133
190
|
/** every image a card draws: each view's image or pair, a clip's poster, a sound's waveforms, a gallery's tiles */
|
|
134
191
|
export function imagesOf(i: Item): string[] {
|
|
135
192
|
const names = i.views.flatMap((v) => {
|
|
@@ -140,14 +197,44 @@ export function imagesOf(i: Item): string[] {
|
|
|
140
197
|
return [...new Set(names)].filter((n) => IMAGE.test(n))
|
|
141
198
|
}
|
|
142
199
|
|
|
143
|
-
/**
|
|
144
|
-
|
|
200
|
+
/** every clip a card plays: each clip view's video, or both sides of its pair */
|
|
201
|
+
export const clipsOf = (i: Pick<Item, 'views'>): string[] => [...new Set(i.views.flatMap((v) => (isGallery(v) || !v.clip ? [] : Object.values(v.files).filter((n) => CLIP.test(n)))))]
|
|
202
|
+
|
|
203
|
+
/** the previews of these cards made ahead, so each opens on them: its list thumb, every image at the fit size, the
|
|
204
|
+
* play copy of every clip over the line. Already made ones cost a stat, a copy the Space holds costs nothing */
|
|
145
205
|
export async function warmPreviews(dir: string, cards: readonly ReviewItem[]): Promise<void> {
|
|
146
206
|
await Promise.all(
|
|
147
207
|
cards.flatMap((x) => {
|
|
208
|
+
const id = x.item.id
|
|
148
209
|
const first = firstView(x.item)
|
|
149
210
|
const thumb = first ? thumbFileOf(first) : ''
|
|
150
|
-
return [
|
|
211
|
+
return [
|
|
212
|
+
...(IMAGE.test(thumb) ? [previewFile(dir, id, thumb, 'thumb')] : []),
|
|
213
|
+
...imagesOf(x.item).map((n) => previewFile(dir, id, n, 'fit')),
|
|
214
|
+
...clipsOf(x.item).filter((n) => playsCopy(dir, id, n)).map((n) => previewOrRemote(dir, id, n, 'fit')),
|
|
215
|
+
]
|
|
151
216
|
}),
|
|
152
217
|
)
|
|
153
218
|
}
|
|
219
|
+
|
|
220
|
+
const mb = (bytes: number): string => `${(bytes / 1e6).toFixed(1)} MB`
|
|
221
|
+
const mbit = (bytes: number, seconds: number): string => `${((bytes * 8) / seconds / 1e6).toFixed(1)} Mbit/s`
|
|
222
|
+
|
|
223
|
+
/** the play copies of a card's clips from view `from` on, made now: one line each, what it weighs and what it cost */
|
|
224
|
+
export async function playCopies(dir: string, item: Pick<Item, 'id' | 'views'>, from = 0): Promise<string[]> {
|
|
225
|
+
const views = item.views.slice(from)
|
|
226
|
+
return Promise.all(
|
|
227
|
+
clipsOf({ views })
|
|
228
|
+
.filter((n) => playsCopy(dir, item.id, n))
|
|
229
|
+
.map(async (n) => {
|
|
230
|
+
const t0 = performance.now()
|
|
231
|
+
const src = join(dir, item.id, n)
|
|
232
|
+
const out = await previewFile(dir, item.id, n, 'fit')
|
|
233
|
+
const took = ((performance.now() - t0) / 1000).toFixed(1)
|
|
234
|
+
if (!out || out === src) return `play copy of ${n}: FAIL after ${took} s, the hub plays the clip itself`
|
|
235
|
+
const seconds = lengthOf(views, n) ?? 1
|
|
236
|
+
const [a, b] = [statSync(src).size, statSync(out).size]
|
|
237
|
+
return `play copy of ${n}: ${mb(a)} at ${mbit(a, seconds)} → ${mb(b)} at ${mbit(b, seconds)}, made in ${took} s`
|
|
238
|
+
}),
|
|
239
|
+
)
|
|
240
|
+
}
|
package/src/review/routes.ts
CHANGED
|
@@ -7,13 +7,13 @@ import { dirname, extname, join } from 'node:path'
|
|
|
7
7
|
import { isAnimFormat, isAnimLabels, isAnimMode, pairAnimation } from './anim.ts'
|
|
8
8
|
import { startAuto } from './auto.ts'
|
|
9
9
|
import { bad, KEEP, NO_STORE, ranged, tagged, TYPES } from './http.ts'
|
|
10
|
-
import { viewFilesOf, type Rect, type ReviewItem, type Subject } from './model.ts'
|
|
10
|
+
import { verdictOf, viewFilesOf, type Rect, type ReviewItem, type ReviewStatus, type Subject } from './model.ts'
|
|
11
11
|
import { stageGet, stagePost } from './stage-routes.ts'
|
|
12
12
|
import { pasteHosts } from './paste.ts'
|
|
13
13
|
import { openReview, partFile, viewOfItem } from './project.ts'
|
|
14
|
-
import { isPreviewSize, previewOrRemote, warmPreviews } from './previews.ts'
|
|
14
|
+
import { CLIP, isPreviewSize, previewOrRemote, warmPreviews } from './previews.ts'
|
|
15
15
|
import { roleOf } from './role.ts'
|
|
16
|
-
import { addMessage, imageFile, itemOfRef, markRead, raiseItem, rateSubject, readAllCached, readItem, specOf, storeDir, storeStamp, threadImage, writeVerdict } from './store.ts'
|
|
16
|
+
import { addMessage, imageFile, itemOfRef, markRead, raiseItem, rateSubject, readAllCached, readItem, reopen, specOf, storeDir, storeStamp, threadImage, writeVerdict } from './store.ts'
|
|
17
17
|
import { boardOf, pageOf, queryOf, threadIndex, threadOfId, threadView, type ThreadIndex } from './cells.ts'
|
|
18
18
|
import { membersOf, threadsOf, type Thread } from './threads.ts'
|
|
19
19
|
import { putMedia } from '../media-store/put.ts'
|
|
@@ -50,17 +50,15 @@ async function project(root: string): Promise<{ dir: string; unit: ReviewUnit |
|
|
|
50
50
|
return { dir, unit }
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
-
/**
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
addMessage(dir, id, 'releases', `the release did not take this change: ${why}. Run sk releases add --card ${id}`)
|
|
63
|
-
}
|
|
53
|
+
/** next.md follows his answer (releases/from-card.ts), after the request answered; `was` the status before it */
|
|
54
|
+
async function releaseFollows(dir: string, id: string, was: ReviewStatus | null): Promise<void> {
|
|
55
|
+
const { followAnswer } = await import('../releases/from-card.ts')
|
|
56
|
+
await followAnswer(dirname(dirname(dir)), id, was)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const statusNow = (dir: string, id: string): ReviewStatus | null => {
|
|
60
|
+
const x = readItem(dir, id)
|
|
61
|
+
return x ? (verdictOf(x)?.status ?? null) : null
|
|
64
62
|
}
|
|
65
63
|
|
|
66
64
|
/** an image the page sent as a data url, written to a scratch file the caller removes */
|
|
@@ -230,14 +228,15 @@ async function reviewRoute(req: Request, path: string, dir: string, unit: Review
|
|
|
230
228
|
const url = await remoteUrl(dir, t[1] ?? '', `thread/${t[2] ?? ''}`)
|
|
231
229
|
return url ? toSpace(url) : bad('no such image', 404)
|
|
232
230
|
}
|
|
233
|
-
// a view's image at the size the page draws it; `thumb/<name>` is the preview's thumb size
|
|
231
|
+
// a view's image at the size the page draws it, a clip as the page plays it; `thumb/<name>` is the preview's thumb size
|
|
234
232
|
const pv = /^\/api\/review\/([^/]+)\/(?:preview\/([^/]+)|thumb)\/([^/]+)$/.exec(path)
|
|
235
233
|
if (pv && req.method === 'GET') {
|
|
236
234
|
const size = pv[2] ?? 'thumb'
|
|
237
235
|
if (!isPreviewSize(size)) return bad(`preview size ${size}: thumb or fit`, 404)
|
|
238
236
|
const p = await previewOrRemote(dir, pv[1] ?? '', pv[3] ?? '', size)
|
|
239
237
|
if (p && 'url' in p) return toSpace(p.url)
|
|
240
|
-
|
|
238
|
+
if (p && CLIP.test(p.file)) return ranged(req, p.file)
|
|
239
|
+
return p ? new Response(Bun.file(p.file), { headers: { 'Content-Type': 'image/webp', ...KEEP } }) : bad('no such image or clip', 404)
|
|
241
240
|
}
|
|
242
241
|
const m = /^\/api\/review\/([^/]+)\/([^/]+)$/.exec(path)
|
|
243
242
|
if (!m) return null
|
|
@@ -245,12 +244,24 @@ async function reviewRoute(req: Request, path: string, dir: string, unit: Review
|
|
|
245
244
|
if (req.method === 'POST') {
|
|
246
245
|
try {
|
|
247
246
|
if (name === 'feedback') {
|
|
247
|
+
const was = statusNow(dir, id)
|
|
248
248
|
const r = writeVerdict(dir, id, await req.json())
|
|
249
|
-
if (r.ok) void
|
|
249
|
+
if (r.ok) void releaseFollows(dir, id, was)
|
|
250
250
|
return r.ok ? Response.json(r.message, { headers: NO_STORE }) : Response.json({ error: r.errors.join('\n'), errors: r.errors }, { status: 400, headers: NO_STORE })
|
|
251
251
|
}
|
|
252
|
+
// a click on his lit verdict: he takes it back, the card waits for him again (the CLI's reopen, by the owner)
|
|
253
|
+
if (name === 'reopen') {
|
|
254
|
+
const v = reopen(dir, id, { who: 'owner' })
|
|
255
|
+
if (!v) return bad(`${id}: no such card, or no answer to take back`, 404)
|
|
256
|
+
void releaseFollows(dir, id, v.status)
|
|
257
|
+
return Response.json({ reopened: v.id }, { headers: NO_STORE })
|
|
258
|
+
}
|
|
252
259
|
if (name === 'stage') return await stagePost(dirname(dirname(dir)), dir, id, unit, (await req.json().catch(() => ({}))) as Record<string, unknown>)
|
|
253
|
-
if (name === 'reply')
|
|
260
|
+
if (name === 'reply') {
|
|
261
|
+
// `re`: the message his word answers, a hint on an agent's update
|
|
262
|
+
const b = (await req.json()) as { text?: unknown; re?: unknown }
|
|
263
|
+
return Response.json(addMessage(dir, id, 'owner', b.text, undefined, new Date(), b.re), { headers: NO_STORE })
|
|
264
|
+
}
|
|
254
265
|
if (name === 'read') {
|
|
255
266
|
// the page names the words it drew: a word that came after them stays unread
|
|
256
267
|
const b = (await req.json().catch(() => ({}))) as { of?: unknown }
|
package/src/review/store.ts
CHANGED
|
@@ -37,6 +37,11 @@ import {
|
|
|
37
37
|
verdictOf,
|
|
38
38
|
viewIndexOf,
|
|
39
39
|
withdrawnOf,
|
|
40
|
+
inWork,
|
|
41
|
+
UPDATE_STATES,
|
|
42
|
+
UPDATE_WORDS,
|
|
43
|
+
updatesOf,
|
|
44
|
+
type UpdateState,
|
|
40
45
|
type GalleryTile,
|
|
41
46
|
type CardChangelog,
|
|
42
47
|
type Item,
|
|
@@ -53,6 +58,7 @@ import {
|
|
|
53
58
|
type TrailerCard,
|
|
54
59
|
type Spec,
|
|
55
60
|
type Subject,
|
|
61
|
+
type Verdict,
|
|
56
62
|
viewFilesOf,
|
|
57
63
|
mediaFilesOf,
|
|
58
64
|
threadMediaOf,
|
|
@@ -418,11 +424,15 @@ function copyFiles(v: Loaded, n: number, into: string): void {
|
|
|
418
424
|
}
|
|
419
425
|
}
|
|
420
426
|
|
|
421
|
-
/**
|
|
422
|
-
|
|
427
|
+
/** the command that posts an update, as a refusal names it */
|
|
428
|
+
const updateLine = (id: string): string => `sk review update ${id} <${UPDATE_STATES.join('|')}> "<one line>" <file> | <before> <after>`
|
|
429
|
+
|
|
430
|
+
/** another angle of an open card's subject, appended as its next view; refused once he answered, except the view of an
|
|
431
|
+
* update (`update`), which goes on at any time */
|
|
432
|
+
export async function addView(dir: string, id: string, v: ViewInput, o: { marks?: ReviewMark[]; light?: 'review'; update?: true } = {}): Promise<Item> {
|
|
423
433
|
const x = readItem(dir, id)
|
|
424
434
|
if (!x) throw new Error(`no card ${id}`)
|
|
425
|
-
if (verdictOf(x)) throw new Error(`${id} has his answer already: a new version passes --supersedes ${id}`)
|
|
435
|
+
if (verdictOf(x) && !o.update) throw new Error(`${id} has his answer already: a picture of the work since goes on as an update, ${updateLine(id)}; a new version passes --supersedes ${id}`)
|
|
426
436
|
if (v.note !== undefined && v.gallery) throw new Error(`"${v.label}": a note goes on an image, a clip or a sound, never a gallery`)
|
|
427
437
|
if (!v.label.trim()) throw new Error('add-view: the view needs its label')
|
|
428
438
|
const loaded = await loadView(v, `the view "${v.label}"`)
|
|
@@ -556,12 +566,56 @@ export function appendMessage(dir: string, id: string, m: Omit<Message, 'id' | '
|
|
|
556
566
|
return out
|
|
557
567
|
}
|
|
558
568
|
|
|
559
|
-
/** a reply on an item from `who` (the owner, or an agent by its name), with an optional image;
|
|
560
|
-
|
|
561
|
-
|
|
569
|
+
/** a reply on an item from `who` (the owner, or an agent by its name), with an optional image; `re` the message of the
|
|
570
|
+
* item it answers (his hint on an update); throws with the reason */
|
|
571
|
+
export function addMessage(dir: string, id: string, who: string, text: unknown, image?: string, now = new Date(), re?: unknown): Message {
|
|
572
|
+
const x = readItem(dir, id)
|
|
573
|
+
if (!x) throw new Error(`no card ${id}`)
|
|
562
574
|
const t = textOf(text)
|
|
563
575
|
if ('error' in t) throw new Error(t.error)
|
|
564
|
-
|
|
576
|
+
if (re !== undefined && !(typeof re === 'string' && x.thread.some((m) => m.id === re))) throw new Error(`re: ${String(re)} is no message of ${id}`)
|
|
577
|
+
return appendMessage(dir, id, { who, text: t.text, ...(typeof re === 'string' ? { re } : {}) }, { ...(image !== undefined ? { image } : {}), now })
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
/** a view of files as typed: one file an image, a clip or a sound (by its extension), two a before and an after; loadView
|
|
581
|
+
* checks them */
|
|
582
|
+
export function filesView(label: string, files: readonly string[], usage: string): ViewInput {
|
|
583
|
+
const [a, b] = files
|
|
584
|
+
if (files.length === 1 && a !== undefined) return { label, image: a }
|
|
585
|
+
if (files.length === 2 && a !== undefined && b !== undefined) return { label, before: a, after: b }
|
|
586
|
+
throw new Error(usage)
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
/** what an agent's update says: its state, its line, its media as it is, one file or a before and an after (none: the
|
|
590
|
+
* state alone) */
|
|
591
|
+
export interface UpdateInput {
|
|
592
|
+
who: string
|
|
593
|
+
state: string
|
|
594
|
+
text: unknown
|
|
595
|
+
files?: readonly string[]
|
|
596
|
+
now?: Date
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
/** an agent's update on its card, at any time, before or after his answer: its file, or its pair, becomes the card's next view
|
|
600
|
+
* (labelled `update <k>: <state>`, the line its note), then one message carries the state and the line. Still broken
|
|
601
|
+
* or better holds the card in work, fixed asks the owner's verdict again (model.ts stateOf) */
|
|
602
|
+
export async function postUpdate(dir: string, id: string, u: UpdateInput): Promise<{ item: Item; message: Message; tag: string; view: number | null }> {
|
|
603
|
+
const x = readItem(dir, id)
|
|
604
|
+
if (!x) throw new Error(`no card ${id}`)
|
|
605
|
+
if (x.item.origin.by !== 'agent') throw new Error(`${id} was raised, not sent by an agent: an update goes on the card that answers it`)
|
|
606
|
+
if (x.item.wip) throw new Error(`${id} is a draft on a live shot: its frame refreshes itself, sk review ready ${id} when it is done`)
|
|
607
|
+
if (!UPDATE_STATES.includes(u.state as UpdateState)) throw new Error(`the state: one of ${UPDATE_STATES.join(', ')}, not « ${u.state} »`)
|
|
608
|
+
const state = u.state as UpdateState
|
|
609
|
+
const t = textOf(u.text)
|
|
610
|
+
if ('error' in t) throw new Error(`the line: ${t.error.replace(/^text: /, '')}`)
|
|
611
|
+
const files = u.files ?? []
|
|
612
|
+
for (const f of files) if (!existsSync(f)) throw new Error(`${f}: no such file`)
|
|
613
|
+
const k = updatesOf(x).length + 1
|
|
614
|
+
const label = `update ${k}: ${UPDATE_WORDS[state]}`
|
|
615
|
+
const item = files.length === 0 ? x.item : await addView(dir, id, { ...filesView(label, files, `an update holds one file, or a before and an after: ${updateLine(id)}`), note: t.text }, { update: true })
|
|
616
|
+
const view = files.length === 0 ? null : item.views.length - 1
|
|
617
|
+
const message = appendMessage(dir, id, { who: u.who, text: t.text, update: { state, ...(view !== null ? { view } : {}) } }, u.now ? { now: u.now } : {})
|
|
618
|
+
return { item, message, tag: `update ${k} (${UPDATE_WORDS[state]})`, view }
|
|
565
619
|
}
|
|
566
620
|
|
|
567
621
|
/** `party` read what the other party wrote on an item: one `seen` message naming them; how many it read. `only`: the
|
|
@@ -748,12 +802,14 @@ export function withdraw(dir: string, id: string, why: string, now = new Date())
|
|
|
748
802
|
return true
|
|
749
803
|
}
|
|
750
804
|
|
|
751
|
-
/** an answered card waits for his answer again: its verdict stays in the thread, before the reopen
|
|
752
|
-
|
|
805
|
+
/** an answered card waits for his answer again: its verdict stays in the thread, before the reopen. `who`: an agent
|
|
806
|
+
* (the CLI), or the owner taking the verdict back (the hub). The verdict reopened, null when it has none */
|
|
807
|
+
export function reopen(dir: string, id: string, o: { who?: string; now?: Date } = {}): Verdict | null {
|
|
753
808
|
const x = ID_RE.test(id) ? readItem(dir, id) : null
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
809
|
+
const v = x ? verdictOf(x) : null
|
|
810
|
+
if (!v) return null
|
|
811
|
+
appendMessage(dir, id, { who: o.who ?? agentName(), op: 'reopen' }, { now: o.now ?? new Date() })
|
|
812
|
+
return v
|
|
757
813
|
}
|
|
758
814
|
|
|
759
815
|
/** the path of one image, clip or poster of an item, null for any other name */
|
|
@@ -1031,6 +1087,12 @@ export async function readyItem(dir: string, id: string, o: { image?: string; no
|
|
|
1031
1087
|
const x = readItem(dir, id)
|
|
1032
1088
|
if (!x) throw new Error(`no card ${id}`)
|
|
1033
1089
|
const { wip, ...rest } = x.item
|
|
1090
|
+
if (!wip && inWork(x)) {
|
|
1091
|
+
// a card its updates hold in work: ready asks the owner's verdict, its newest picture goes on as an update
|
|
1092
|
+
if (o.image !== undefined) throw new Error(`${id} is in work by its updates: its new picture goes on as one, ${updateLine(id).replace('<broken|better|fixed>', 'fixed')}`)
|
|
1093
|
+
appendMessage(dir, id, { who: agentName(), op: 'ready' }, o.now ? { now: o.now } : {})
|
|
1094
|
+
return x.item
|
|
1095
|
+
}
|
|
1034
1096
|
if (!wip) throw new Error(`${id} is not a draft card: it already takes a verdict`)
|
|
1035
1097
|
let item: Item = rest
|
|
1036
1098
|
if (o.image !== undefined) {
|
package/src/review/threads.ts
CHANGED
|
@@ -2,11 +2,13 @@
|
|
|
2
2
|
// items (a report, his note) the card answers, read as one thread per bug; their status, conversation and order, and
|
|
3
3
|
// the active thread a new card already covers. Pure, over the items of review/model.ts
|
|
4
4
|
import {
|
|
5
|
+
inWork,
|
|
5
6
|
isReply,
|
|
6
7
|
partyOf,
|
|
7
8
|
reworkOf,
|
|
8
9
|
stateOf,
|
|
9
10
|
subjectKey,
|
|
11
|
+
takenBackOf,
|
|
10
12
|
unreadBy,
|
|
11
13
|
verdictOf,
|
|
12
14
|
withdrawnOf,
|
|
@@ -147,12 +149,12 @@ export function conversationOf(t: Thread): (Message & { on: string })[] {
|
|
|
147
149
|
}
|
|
148
150
|
|
|
149
151
|
/** a head waits for the owner's verdict: an agent's card, none yet, not withdrawn, not a draft */
|
|
150
|
-
export const waits = (x: ReviewItem): boolean => x.item.origin.by === 'agent' && !x
|
|
152
|
+
export const waits = (x: ReviewItem): boolean => x.item.origin.by === 'agent' && !inWork(x) && verdictOf(x) === null && withdrawnOf(x) === undefined
|
|
151
153
|
|
|
152
154
|
/** the active pending heads first in the order they were sent, then every other thread, newest first */
|
|
153
155
|
export function orderedThreads(items: readonly ReviewItem[]): Thread[] {
|
|
154
156
|
const all = threadsOf(items)
|
|
155
|
-
const first = (x: ReviewItem): boolean => waits(x) || (x.item.
|
|
157
|
+
const first = (x: ReviewItem): boolean => waits(x) || (x.item.origin.by === 'agent' && inWork(x))
|
|
156
158
|
const pending = all.filter((t) => first(t.head)).sort((a, b) => oldest(a.head, b.head))
|
|
157
159
|
const rest = all.filter((t) => !first(t.head)).sort((a, b) => newest(a.head, b.head))
|
|
158
160
|
return [...pending, ...rest]
|
|
@@ -209,16 +211,16 @@ export function statusOf(x: ReviewItem, items: readonly ReviewItem[]): ShownStat
|
|
|
209
211
|
const v = verdictOf(x)
|
|
210
212
|
if (v) return v.status
|
|
211
213
|
if (withdrawnOf(x) !== undefined) return 'withdrawn'
|
|
212
|
-
if (x
|
|
214
|
+
if (inWork(x)) return 'wip'
|
|
213
215
|
return supersederOf(x.item.id, items) ? 'superseded' : 'pending'
|
|
214
216
|
}
|
|
215
217
|
|
|
216
|
-
/** the latest thing on `x` the agent has not seen: its verdict
|
|
218
|
+
/** the latest thing on `x` the agent has not seen: its verdict, a reply of the owner, or a verdict he took back */
|
|
217
219
|
const lastUnseen = (x: ReviewItem, answered: boolean): string => {
|
|
218
220
|
const v = verdictOf(x)
|
|
219
221
|
const opening = answered || stateOf(x, false).state === 'closed' ? openingOf(x) : undefined
|
|
220
222
|
const replies = unreadBy(x, 'agent').filter((m) => m !== opening)
|
|
221
|
-
return [...(v && !v.seen ? [v.at] : []), ...replies.map((m) => m.at)].sort().at(-1) ?? ''
|
|
223
|
+
return [...(v && !v.seen ? [v.at] : []), ...replies.map((m) => m.at), ...takenBackOf(x).map((t) => t.m.at)].sort().at(-1) ?? ''
|
|
222
224
|
}
|
|
223
225
|
|
|
224
226
|
/** the items with a verdict or an owner reply the agent has not marked seen yet, oldest first */
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
// secrets copied to another Mac over ssh: each read through this house's manager and written by the peer's own
|
|
2
|
+
// `sk secret set`, from the house checkout at the same place under its home. A value only ever travels on ssh's
|
|
3
|
+
// stdin: never an argv, a file or a printed line. A secret the peer already answers is left as it is.
|
|
4
|
+
import { secretArgv } from '../smoke/run-smoke.ts'
|
|
5
|
+
|
|
6
|
+
export type Copied = { name: string; did: 'copied' | 'there' | 'not here' | 'failed'; why?: string }
|
|
7
|
+
|
|
8
|
+
/** what macOS's `security` says when the login keychain of an ssh session is locked */
|
|
9
|
+
export const LOCKED = /User interaction is not allowed/
|
|
10
|
+
|
|
11
|
+
type Ran = { code: number; out: string; err: string }
|
|
12
|
+
export type Run = (argv: string[], stdin?: string) => Promise<Ran>
|
|
13
|
+
|
|
14
|
+
const spawnRun: Run = async (argv, stdin) => {
|
|
15
|
+
const p = Bun.spawn(argv, { stdin: stdin == null ? 'ignore' : new Blob([stdin]), stdout: 'pipe', stderr: 'pipe' })
|
|
16
|
+
const [out, err] = await Promise.all([new Response(p.stdout).text(), new Response(p.stderr).text()])
|
|
17
|
+
return { code: await p.exited, out, err }
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** a word quoted for the peer's sh */
|
|
21
|
+
const q = (s: string): string => `'${s.replaceAll("'", `'\\''`)}'`
|
|
22
|
+
|
|
23
|
+
/** the line the peer's shell runs: an ssh command shell reads no rc file, so the tools' folders are named */
|
|
24
|
+
export const peerLine = (houseRel: string, args: string): string => `cd ~/${houseRel} && PATH="$HOME/.local/bin:$HOME/.bun/bin:$PATH" sk secret ${args}`
|
|
25
|
+
|
|
26
|
+
/** the unlock a locked peer needs, said once */
|
|
27
|
+
export const unlockHint = (to: string): string => `the login keychain of ${to} is locked to ssh, and an unlock holds for its own ssh session only: run \`sk secret copy --to ${to} --unlock\` from your terminal (it asks for that Mac's login password once)`
|
|
28
|
+
|
|
29
|
+
export async function copySecrets(o: { names: string[]; to: string; reader: string; houseRel: string; run?: Run }): Promise<{ copied: Copied[]; locked: boolean }> {
|
|
30
|
+
const run = o.run ?? spawnRun
|
|
31
|
+
const ssh = (args: string, stdin?: string): Promise<Ran> => run(['ssh', '-o', 'BatchMode=yes', o.to, peerLine(o.houseRel, args)], stdin)
|
|
32
|
+
const copied: Copied[] = []
|
|
33
|
+
for (const name of o.names) {
|
|
34
|
+
const value = await run(secretArgv(o.reader, name, 'get'))
|
|
35
|
+
if (value.code !== 0 || value.out.trim() === '') {
|
|
36
|
+
copied.push({ name, did: 'not here' })
|
|
37
|
+
continue
|
|
38
|
+
}
|
|
39
|
+
if ((await ssh(`get ${q(name)} > /dev/null 2>&1`)).code === 0) {
|
|
40
|
+
copied.push({ name, did: 'there' })
|
|
41
|
+
continue
|
|
42
|
+
}
|
|
43
|
+
const account = await run(secretArgv(o.reader, name, 'account'))
|
|
44
|
+
const who = account.code === 0 && account.out.trim() !== '' ? ` ${q(account.out.trim())}` : ''
|
|
45
|
+
const set = await ssh(`set ${q(name)}${who}`, value.out)
|
|
46
|
+
if (set.code === 0) {
|
|
47
|
+
copied.push({ name, did: 'copied' })
|
|
48
|
+
continue
|
|
49
|
+
}
|
|
50
|
+
const said = `${set.out}\n${set.err}`.replaceAll(value.out.trim(), '‹value›')
|
|
51
|
+
if (LOCKED.test(said)) return { copied: [...copied, { name, did: 'failed', why: unlockHint(o.to) }], locked: true }
|
|
52
|
+
copied.push({ name, did: 'failed', why: said.trim().split('\n').slice(-1)[0] ?? `exit ${set.code}` })
|
|
53
|
+
}
|
|
54
|
+
return { copied, locked: false }
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** one line a secret on the pipe to the peer: its name, then its account and its value in base64, so no byte of
|
|
58
|
+
* either can end a line or a word */
|
|
59
|
+
export const record = (name: string, account: string, value: string): string =>
|
|
60
|
+
`${name} ${Buffer.from(account).toString('base64')} ${Buffer.from(value).toString('base64')}\n`
|
|
61
|
+
|
|
62
|
+
/** what the peer runs in the ONE ssh session that unlocks its keychain (an unlock holds for its own session only):
|
|
63
|
+
* the unlock on the owner's terminal, then each line of the pipe stored by its own `sk secret set`, the value on
|
|
64
|
+
* stdin. `fifo` is relative to the peer's home. A failed unlock still drains the pipe, so the laptop's writer never hangs */
|
|
65
|
+
export const receiverScript = (fifo: string, houseRel: string): string =>
|
|
66
|
+
[
|
|
67
|
+
// the pipe is named from the home, where ssh starts, before the move into the house checkout
|
|
68
|
+
`f="$HOME"/${q(fifo)}; PATH="$HOME/.local/bin:$HOME/.bun/bin:$PATH"`,
|
|
69
|
+
`cd ~/${houseRel} || { cat "$f" > /dev/null; rm -f "$f"; exit 1; }`,
|
|
70
|
+
`if ! security unlock-keychain; then cat "$f" > /dev/null; rm -f "$f"; exit 1; fi`,
|
|
71
|
+
`while read -r name a64 v64; do`,
|
|
72
|
+
` if sk secret get "$name" > /dev/null 2>&1; then echo "skip $name already there"; continue; fi`,
|
|
73
|
+
` acct=$(printf %s "$a64" | base64 -d)`,
|
|
74
|
+
` if printf %s "$v64" | base64 -d | sk secret set "$name" "$acct" > /dev/null 2>&1; then echo "ok $name copied"; else echo "FAIL $name not stored"; fi`,
|
|
75
|
+
`done < "$f"`,
|
|
76
|
+
`rm -f "$f"`,
|
|
77
|
+
].join('\n')
|
|
78
|
+
|
|
79
|
+
/** the copy for a peer whose keychain ssh sees locked: run from the owner's terminal, since the unlock asks for that
|
|
80
|
+
* Mac's login password. Values ride a fifo on the peer (nothing at rest), filled by a second ssh session */
|
|
81
|
+
export async function copyWithUnlock(o: { names: string[]; to: string; reader: string; houseRel: string; say: (l: string) => void }): Promise<number> {
|
|
82
|
+
let lines = ''
|
|
83
|
+
for (const name of o.names) {
|
|
84
|
+
const value = await spawnRun(secretArgv(o.reader, name, 'get'))
|
|
85
|
+
if (value.code !== 0 || value.out.trim() === '') {
|
|
86
|
+
o.say(`skip ${name} not on this Mac`)
|
|
87
|
+
continue
|
|
88
|
+
}
|
|
89
|
+
const account = await spawnRun(secretArgv(o.reader, name, 'account'))
|
|
90
|
+
lines += record(name, account.code === 0 ? account.out.trim() : '', value.out.replace(/\n$/, ''))
|
|
91
|
+
}
|
|
92
|
+
if (lines === '') return 0
|
|
93
|
+
const fifo = `.shipkit-secret-copy-${process.pid}-${Date.now()}.fifo`
|
|
94
|
+
const made = await spawnRun(['ssh', '-o', 'BatchMode=yes', o.to, `mkfifo -m 600 ${q(fifo)}`])
|
|
95
|
+
if (made.code !== 0) {
|
|
96
|
+
o.say(`FAIL no pipe on ${o.to}: ${made.err.trim() || `exit ${made.code}`}`)
|
|
97
|
+
return 1
|
|
98
|
+
}
|
|
99
|
+
const writer = Bun.spawn(['ssh', '-o', 'BatchMode=yes', o.to, `cat > ${q(fifo)}`], { stdin: new Blob([lines]), stdout: 'ignore', stderr: 'pipe' })
|
|
100
|
+
o.say(`${o.to} asks for its login password once, to unlock its keychain for this session:`)
|
|
101
|
+
const receiver = Bun.spawn(['ssh', '-t', o.to, receiverScript(fifo, o.houseRel)], { stdin: 'inherit', stdout: 'inherit', stderr: 'inherit' })
|
|
102
|
+
const code = await receiver.exited
|
|
103
|
+
// a receiver that never opened the pipe leaves the writer blocked on it
|
|
104
|
+
const done = await Promise.race([writer.exited, new Promise<null>((ok) => setTimeout(() => ok(null), 3000))])
|
|
105
|
+
if (done === null) {
|
|
106
|
+
writer.kill()
|
|
107
|
+
await spawnRun(['ssh', '-o', 'BatchMode=yes', o.to, `rm -f ${q(fifo)}`])
|
|
108
|
+
}
|
|
109
|
+
return code
|
|
110
|
+
}
|