rvlib-shipkit 2.23.0 → 2.25.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 +45 -0
- package/package.json +1 -1
- package/src/agent-time/live.ts +153 -0
- 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/annex/offload.ts +16 -7
- package/src/asks/asks-queue.ts +32 -11
- package/src/check.ts +3 -1
- package/src/checks/SK004-gitignore-policy.check.ts +32 -11
- package/src/checks/SK027-agent-wiki-links.check.ts +1 -1
- package/src/checks/SK033-global-contribution.check.ts +1 -1
- package/src/checks/SK036-draft-media-size.check.ts +1 -1
- package/src/checks/SK040-release-words.check.ts +1 -1
- package/src/checks/SK042-post-kinds.check.ts +1 -1
- package/src/checks/SK044-behind-upstream.check.ts +38 -0
- package/src/cli/cmd_Annex.ts +12 -7
- package/src/cli/cmd_Asks.ts +2 -0
- package/src/cli/cmd_Assets.ts +5 -5
- package/src/cli/cmd_Backup.ts +16 -8
- package/src/cli/cmd_Bump.ts +1 -0
- package/src/cli/cmd_Capacity.ts +30 -0
- package/src/cli/cmd_Check.ts +1 -0
- package/src/cli/cmd_Checks.ts +13 -1
- package/src/cli/cmd_Ci.ts +3 -2
- package/src/cli/cmd_Commands.ts +61 -16
- package/src/cli/cmd_ConfigKeys.ts +17 -0
- package/src/cli/cmd_Home.ts +1 -30
- package/src/cli/cmd_Hooks.ts +1 -0
- package/src/cli/cmd_Keys.ts +4 -1
- package/src/cli/cmd_Lanes.ts +35 -0
- package/src/cli/cmd_Mv.ts +7 -7
- package/src/cli/cmd_Rec.ts +22 -7
- package/src/cli/cmd_Reflect.ts +1 -0
- package/src/cli/cmd_Releases.ts +1 -0
- package/src/cli/cmd_Review.ts +200 -49
- package/src/cli/cmd_Secret.ts +41 -2
- package/src/cli/cmd_Shot.ts +16 -4
- package/src/cli/cmd_Status.ts +7 -5
- package/src/cli/cmd_Time.ts +13 -5
- package/src/cli/cmd_Tmp.ts +82 -0
- package/src/cli/cmd_Veille.ts +10 -6
- package/src/cli/cmd_Wiki.ts +1 -0
- package/src/cli/family-help.ts +136 -0
- package/src/cli/family-pages.ts +0 -19
- package/src/cli/kit/calls.ts +35 -0
- package/src/cli/kit/project-cli.ts +2 -12
- package/src/cli/registry.ts +52 -7
- package/src/cli.ts +8 -4
- package/src/collections/pings.ts +1 -1
- package/src/core/capacity.ts +127 -0
- package/src/core/check-runner.ts +91 -16
- package/src/core/floor-resolve.ts +6 -2
- package/src/core/floor.ts +6 -2
- package/src/core/history-files.ts +18 -0
- package/src/core/jobs.ts +35 -16
- package/src/core/layout.ts +2 -2
- package/src/core/motherhome.ts +67 -9
- package/src/core/nearest.ts +16 -0
- package/src/core/remove-tree.ts +19 -0
- package/src/core/runs.ts +12 -3
- package/src/core/slots.ts +49 -12
- package/src/core/throwaway-worktree.ts +22 -4
- package/src/core/trace-audit.ts +21 -5
- package/src/drill/run.ts +5 -2
- package/src/home/daemon-jobs.ts +41 -4
- 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 +485 -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/gate-checks.ts +3 -2
- package/src/ledger/git-key.ts +25 -4
- package/src/ledger/git-scan.ts +10 -0
- package/src/ledger/ledger.ts +6 -0
- package/src/ledger/store.ts +6 -0
- package/src/media-store/backup/daemon.ts +92 -20
- 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 +26 -17
- package/src/media-store/backup/sources.ts +168 -22
- package/src/media-store/backup/watch.ts +106 -0
- package/src/media-store/daemon.ts +36 -8
- package/src/media-store/hand.ts +3 -1
- package/src/mv/mv-md.ts +167 -56
- package/src/pb/run-pb.ts +2 -10
- package/src/plan/repo-source.ts +25 -14
- package/src/recordings/model.ts +1 -1
- package/src/reflect/transcript.ts +56 -11
- package/src/release/run-bump.ts +3 -2
- package/src/release/run-release.ts +12 -4
- 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 +26 -4
- package/src/review/format.ts +34 -10
- package/src/review/media-kind.ts +14 -11
- package/src/review/media-read.ts +6 -5
- package/src/review/model.ts +85 -4
- 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 +89 -19
- package/src/review/threads.ts +7 -5
- package/src/review/trailer-model.ts +2 -2
- 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/shot/client.ts +4 -2
- package/src/shot/serve-shot.ts +71 -2
- package/src/shot/shooter.ts +65 -11
- package/src/shot/shot-ask.ts +32 -9
- package/src/suite/child-tools.ts +45 -0
- package/src/suite/judge-suites.ts +10 -22
- package/src/suite/select.ts +95 -9
- 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/suite/test-units.ts +31 -12
- package/src/trailer/graph.ts +2 -1
- package/src/ts/config.ts +21 -0
- package/src/ts/rename.ts +37 -13
- package/src/ts/workspaces.ts +45 -0
- package/src/wiki/run-wiki.ts +7 -2
- package/src/wiki/scan-md.ts +2 -1
- package/src/home/claude-temp.ts +0 -139
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// the names a mistyped one was probably meant as, for a refusal that names the
|
|
2
|
+
// next thing to type: equal, a name it begins, or a name that begins it up to
|
|
3
|
+
// a separator (`recipes:check` → `recipes`, never `r`), case, `_` and `-`
|
|
4
|
+
// aside. Short on purpose: a refusal lists a few, never the whole set.
|
|
5
|
+
|
|
6
|
+
const norm = (s: string): string => s.toLowerCase().replace(/[_-]/g, '')
|
|
7
|
+
|
|
8
|
+
/** the names of `have` close enough to `want`, at most `max` */
|
|
9
|
+
export function nearest(want: string, have: readonly string[], max = 5): string[] {
|
|
10
|
+
const w = norm(want)
|
|
11
|
+
if (w === '') return []
|
|
12
|
+
return have.filter((n) => {
|
|
13
|
+
const h = norm(n)
|
|
14
|
+
return h !== '' && (h.startsWith(w) || (w.startsWith(h) && /[^a-z0-9]/.test(w[h.length] ?? '')))
|
|
15
|
+
}).slice(0, max)
|
|
16
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// a tree removed even when it holds a read only folder (a module cache, a snapshot written 0555 on purpose): the
|
|
2
|
+
// caller has already proven the tree is its own to remove. Links are never followed, a link goes as a link.
|
|
3
|
+
import { rmSync } from 'fs'
|
|
4
|
+
|
|
5
|
+
/** every folder and file under `path` made writable by its owner, links never followed */
|
|
6
|
+
export function makeWritable(path: string): void {
|
|
7
|
+
Bun.spawnSync(['chmod', '-R', '-P', 'u+rwx', path], { stdout: 'ignore', stderr: 'ignore' })
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** `path` removed; a refusal for a read only folder inside makes the tree writable and tries once more */
|
|
11
|
+
export function removeTree(path: string): void {
|
|
12
|
+
try {
|
|
13
|
+
rmSync(path, { recursive: true, force: true })
|
|
14
|
+
} catch (e) {
|
|
15
|
+
if (!['EACCES', 'EPERM', 'ENOTEMPTY'].includes((e as { code?: string }).code ?? '')) throw e
|
|
16
|
+
makeWritable(path)
|
|
17
|
+
rmSync(path, { recursive: true, force: true })
|
|
18
|
+
}
|
|
19
|
+
}
|
package/src/core/runs.ts
CHANGED
|
@@ -52,8 +52,10 @@ export type FixWhen = 'none' | 'ci' | 'manual'
|
|
|
52
52
|
|
|
53
53
|
/** how an answer is kept. Every variant names its one parameter */
|
|
54
54
|
export type CacheDecl =
|
|
55
|
-
/** keyed on the content of these repo paths (and the check's own code)
|
|
56
|
-
|
|
55
|
+
/** keyed on the content of these repo paths (and the check's own code).
|
|
56
|
+
* `optional` names the inputs a repo may lack: absent, the key carries
|
|
57
|
+
* `absent` for it; any other input missing from disk refuses the key */
|
|
58
|
+
| { by: 'files'; inputs: string[]; optional?: string[]; key: ((env: CheckEnv) => string) | null }
|
|
57
59
|
/** keyed on what the body read through its context, re-checked before a replay */
|
|
58
60
|
| { by: 'reads' }
|
|
59
61
|
/** a clean answer about the world keeps for `span` */
|
|
@@ -129,9 +131,16 @@ export function cacheProblem(c: unknown): string | null {
|
|
|
129
131
|
const keys = want[by]
|
|
130
132
|
const missing = keys.filter((k) => !(k in o))
|
|
131
133
|
if (missing.length > 0) return `cache { by: '${by}' } is missing ${missing.join(', ')}: every key is explicit`
|
|
132
|
-
|
|
134
|
+
// the one key that may be left out: check files written before it exist in every repo
|
|
135
|
+
const unknown = Object.keys(o).filter((k) => k !== 'by' && !keys.includes(k) && !(by === 'files' && k === 'optional'))
|
|
133
136
|
if (unknown.length > 0) return `cache { by: '${by}' } has unknown key ${unknown.join(', ')} (it takes ${keys.length === 0 ? 'nothing else' : keys.join(', ')})`
|
|
134
137
|
if (by === 'files' && (!Array.isArray(o.inputs) || o.inputs.length === 0 || o.inputs.some((p) => typeof p !== 'string' || p === ''))) return `cache.inputs is a non empty list of repo relative paths`
|
|
138
|
+
if (by === 'files' && 'optional' in o) {
|
|
139
|
+
const opt = o.optional
|
|
140
|
+
if (!Array.isArray(opt) || opt.some((p) => typeof p !== 'string')) return `cache.optional is a list of paths from cache.inputs`
|
|
141
|
+
const stray = opt.filter((p) => !(o.inputs as string[]).includes(p))
|
|
142
|
+
if (stray.length > 0) return `cache.optional names ${stray.join(', ')}, which cache.inputs does not: an optional path is one of the inputs`
|
|
143
|
+
}
|
|
135
144
|
if ((by === 'files' || by === 'ttl') && o.key !== null && typeof o.key !== 'function') return `cache.key is null or a function of { root, config }`
|
|
136
145
|
if (by === 'fact' && typeof o.key !== 'function') return `cache { by: 'fact' } needs key: a function of { root, config } returning the declared value`
|
|
137
146
|
if (by === 'ttl') {
|
package/src/core/slots.ts
CHANGED
|
@@ -1,13 +1,20 @@
|
|
|
1
1
|
// THE MACHINE POOL: every shipkit process on this machine (every repo, every
|
|
2
|
-
// agent, the hub)
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
//
|
|
2
|
+
// agent, the hub) shares parallelCap() slots. One lock file per slot,
|
|
3
|
+
// `slot-<n>.lock` under ~/.cache/shipkit/slots/, taken through the mutate
|
|
4
|
+
// lease's non blocking form (locked-write.ts tryLease): a slot whose holder
|
|
5
|
+
// died is free at once, and a live holder heartbeats it.
|
|
6
6
|
//
|
|
7
|
-
// A
|
|
8
|
-
//
|
|
9
|
-
//
|
|
10
|
-
//
|
|
7
|
+
// A SLOT IS ONE CORE'S WORTH OF WORK. A check holds one slot. A check that
|
|
8
|
+
// runs many processes at once (a suite run and its workers) adds one free slot
|
|
9
|
+
// per worker (takeFreeSlots) and never waits for them, so what the pool holds
|
|
10
|
+
// is close to what the machine burns. A check counted as one slot whatever it
|
|
11
|
+
// started put 30 to 60 test workers on 8 cores, and the load that followed
|
|
12
|
+
// closed the pool to one check for the whole machine.
|
|
13
|
+
//
|
|
14
|
+
// Work the pool cannot see (a browser, a bake, a dev server) still loads the
|
|
15
|
+
// machine, so past the cores the pool admits fewer slots in proportion to the
|
|
16
|
+
// 1 minute load, never fewer than half of them (admittedSlots). A slot already
|
|
17
|
+
// held is never taken back.
|
|
11
18
|
//
|
|
12
19
|
// The one constraint a reader breaks first: a process started BY a check
|
|
13
20
|
// (SHIPKIT_SLOT_HELD=1 in its environment) takes no slot. It already runs on
|
|
@@ -25,6 +32,8 @@ import { PARALLEL_ENV, parallelCap, parallelCapInfo } from './parallel.ts'
|
|
|
25
32
|
export const SLOTS_DIR_ENV = 'SHIPKIT_SLOTS_DIR'
|
|
26
33
|
/** set for every process a check spawns: it runs on its parent's slot */
|
|
27
34
|
export const SLOT_HELD_ENV = 'SHIPKIT_SLOT_HELD'
|
|
35
|
+
/** how many slots the check that spawned this process holds: the workers a suite run may start */
|
|
36
|
+
export const SLOT_CORES_ENV = 'SHIPKIT_SLOT_CORES'
|
|
28
37
|
|
|
29
38
|
/** `load`: the reading that scales what the pool admits. Omitted: the whole
|
|
30
39
|
* size, whatever the machine runs (a test's own pool) */
|
|
@@ -33,6 +42,8 @@ export type SlotPool = { dir: string; size: number; load?: () => MachineLoad }
|
|
|
33
42
|
export type SlotHolder = { slot: number; pid: number; repo: string; check: string; since: number }
|
|
34
43
|
|
|
35
44
|
const SLOT_POLL_MS = 25
|
|
45
|
+
/** a wait grows to this between two looks: sixty checks looking forty times a second burned a core to wait */
|
|
46
|
+
const SLOT_POLL_MAX_MS = 250
|
|
36
47
|
|
|
37
48
|
export const poolDir = (): string => {
|
|
38
49
|
const env = process.env[SLOTS_DIR_ENV]
|
|
@@ -80,10 +91,10 @@ export function machinePool(): SlotPool | null {
|
|
|
80
91
|
}
|
|
81
92
|
|
|
82
93
|
/** how many of `size` slots a machine at this load admits: all of them up to
|
|
83
|
-
* a load of one per core, then size × cores / load, never below
|
|
84
|
-
* on 8 cores a pool of 8 admits
|
|
94
|
+
* a load of one per core, then size × cores / load, never below half. At 12
|
|
95
|
+
* on 8 cores a pool of 8 admits 5, from 16 it admits 4 */
|
|
85
96
|
export const admittedSlots = (size: number, m: MachineLoad): number =>
|
|
86
|
-
Math.max(
|
|
97
|
+
Math.max(Math.ceil(size / 2), Math.min(size, Math.floor((size * m.cores) / Math.max(m.cores, m.load1))))
|
|
87
98
|
|
|
88
99
|
const admitWords = (size: number, admitted: number, m: MachineLoad): string =>
|
|
89
100
|
`the pool admits ${admitted} of its ${size} slots at load ${m.load1.toFixed(1)} on ${m.cores} cores`
|
|
@@ -131,6 +142,7 @@ export async function takeSlot(
|
|
|
131
142
|
): Promise<{ slot: number; release: () => void } | null> {
|
|
132
143
|
mkdirSync(pool.dir, { recursive: true })
|
|
133
144
|
let heard = false
|
|
145
|
+
let sleepMs = o.pollMs ?? SLOT_POLL_MS
|
|
134
146
|
for (;;) {
|
|
135
147
|
const m = pool.load?.() ?? null
|
|
136
148
|
const admitted = m == null ? pool.size : admittedSlots(pool.size, m)
|
|
@@ -143,6 +155,31 @@ export async function takeSlot(
|
|
|
143
155
|
heard = true
|
|
144
156
|
o.onWait?.(poolOccupancy(pool).held, m != null && admitted < pool.size ? admitWords(pool.size, admitted, m) : `all ${pool.size} slots are held`)
|
|
145
157
|
}
|
|
146
|
-
|
|
158
|
+
// an uneven sleep, so waiters that started together do not look together
|
|
159
|
+
await Bun.sleep(sleepMs * (0.5 + Math.random() / 2))
|
|
160
|
+
if (o.pollMs == null) sleepMs = Math.min(SLOT_POLL_MAX_MS, sleepMs * 1.5)
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** the line a check's process prints to say how many slots it uses from now on: its runner gives the rest back
|
|
165
|
+
* at once (`shipkit-slots: 1` once a suite run's parallel phase ended) */
|
|
166
|
+
export const SLOTS_LINE = /^shipkit-slots: (\d+)$/
|
|
167
|
+
export const slotsLine = (n: number): string => `shipkit-slots: ${n}`
|
|
168
|
+
|
|
169
|
+
/** up to `want` more slots, only the ones free right now, never a wait: what a
|
|
170
|
+
* check that holds one slot adds for the processes it runs at once. `keep(n)`
|
|
171
|
+
* gives back all but n of them */
|
|
172
|
+
export function takeFreeSlots(pool: SlotPool, who: { repo: string; check: string }, want: number): { count: number; keep: (n: number) => void; release: () => void } {
|
|
173
|
+
const leases: { release: () => void }[] = []
|
|
174
|
+
if (want > 0) {
|
|
175
|
+
mkdirSync(pool.dir, { recursive: true })
|
|
176
|
+
const m = pool.load?.() ?? null
|
|
177
|
+
const admitted = m == null ? pool.size : admittedSlots(pool.size, m)
|
|
178
|
+
for (let slot = 0; slot < admitted && leases.length < want; slot++) {
|
|
179
|
+
const lease = tryLease(join(pool.dir, `slot-${slot}`), who.check, { repo: who.repo, check: who.check })
|
|
180
|
+
if (lease != null) leases.push(lease)
|
|
181
|
+
}
|
|
147
182
|
}
|
|
183
|
+
const keep = (n: number): void => { for (const l of leases.splice(Math.max(0, n))) l.release() }
|
|
184
|
+
return { count: leases.length, keep, release: () => keep(0) }
|
|
148
185
|
}
|
|
@@ -6,10 +6,11 @@
|
|
|
6
6
|
// a folder it can prove it made: a linked worktree of that repo, at the path
|
|
7
7
|
// this module derives, carrying the mark it wrote into the worktree's own git
|
|
8
8
|
// dir. Anything else at that path is refused and left as it is.
|
|
9
|
-
import { existsSync, lstatSync, readFileSync, realpathSync,
|
|
9
|
+
import { existsSync, lstatSync, readFileSync, realpathSync, writeFileSync } from 'fs'
|
|
10
10
|
import { tmpdir } from 'os'
|
|
11
11
|
import { join } from 'path'
|
|
12
12
|
import { gitBin } from './git-bin.ts'
|
|
13
|
+
import { removeTree } from './remove-tree.ts'
|
|
13
14
|
|
|
14
15
|
const git = (cwd: string, args: string[]): { code: number; out: string } => {
|
|
15
16
|
const p = Bun.spawnSync([gitBin(), ...args], { cwd, stdout: 'pipe', stderr: 'pipe' })
|
|
@@ -52,7 +53,7 @@ export function throwawayCheckout(repo: string, purpose: string, sha: string): {
|
|
|
52
53
|
// the only reset of a working tree to a commit in this toolkit: a proven throwaway
|
|
53
54
|
const back = git(dir, ['reset', '--hard', '--quiet', sha])
|
|
54
55
|
if (back.code !== 0) {
|
|
55
|
-
|
|
56
|
+
removeTree(dir)
|
|
56
57
|
git(repo, ['worktree', 'prune'])
|
|
57
58
|
}
|
|
58
59
|
}
|
|
@@ -66,7 +67,24 @@ export function throwawayCheckout(repo: string, purpose: string, sha: string): {
|
|
|
66
67
|
const own = ownGitDir(repo, dir)
|
|
67
68
|
if ('refuse' in own) return { error: `the throwaway checkout at ${dir} fails its own proof (${own.refuse})` }
|
|
68
69
|
}
|
|
69
|
-
|
|
70
|
-
|
|
70
|
+
if (git(dir, ['clean', '-ffdxq']).code !== 0) {
|
|
71
|
+
// a check may write a folder read only (a snapshot, a module cache), and git clean cannot remove one: each
|
|
72
|
+
// path it would remove goes through removeTree, which makes it writable first
|
|
73
|
+
for (const row of git(dir, ['clean', '-ffdxn']).out.split('\n')) {
|
|
74
|
+
const rel = row.match(/^Would remove (.+?)\/?$/)?.[1]
|
|
75
|
+
if (rel != null) removeTree(join(dir, rel))
|
|
76
|
+
}
|
|
77
|
+
const clean = git(dir, ['clean', '-ffdxq'])
|
|
78
|
+
if (clean.code !== 0) return { error: cleanRefusal(dir, clean.out) }
|
|
79
|
+
}
|
|
71
80
|
return { dir }
|
|
72
81
|
}
|
|
82
|
+
|
|
83
|
+
/** a clean that still fails, as one line: how many paths git refused, the first one, the fix */
|
|
84
|
+
export function cleanRefusal(dir: string, out: string): string {
|
|
85
|
+
const refused = [...out.matchAll(/^warning: failed to remove (.+?): (.+)$/gm)]
|
|
86
|
+
const first = refused[0]
|
|
87
|
+
if (first == null) return `cannot clean the throwaway checkout at ${dir}: ${out.split('\n')[0] ?? 'git clean failed'}`
|
|
88
|
+
const n = refused.length
|
|
89
|
+
return `cannot clean the throwaway checkout at ${dir}: git refused ${n} path${n === 1 ? '' : 's'}, first ${join(dir, first[1] ?? '')} (${first[2]}). fix: chmod -R u+w ${dir}, then run again`
|
|
90
|
+
}
|
package/src/core/trace-audit.ts
CHANGED
|
@@ -22,9 +22,11 @@ import { isNamesInput } from '../ledger/git-key.ts'
|
|
|
22
22
|
import { readTrace } from '../trace/read-trace.ts'
|
|
23
23
|
import { dropTrace, learnBlind, traceUnavailable, tracedCommand } from '../trace/tracer.ts'
|
|
24
24
|
import { spawnWithTimeout } from './spawn-timeout.ts'
|
|
25
|
+
import { SUITE_SELECT_ENV } from '../suite/reads-env.ts'
|
|
25
26
|
import { gitBin } from './git-bin.ts'
|
|
26
27
|
import { auditReportPath, auditedExtras, ignoredFor, pendingByTask, readAuditReport, type AuditReport, type AuditTask, type IgnoredInputs } from './audit-report.ts'
|
|
27
28
|
import { addIgnoredInputs } from './ignored-block.ts'
|
|
29
|
+
import { runLeakGuard } from '../hooks/leak-guard.ts'
|
|
28
30
|
import { loadRepoConfig } from '../repo-config.ts'
|
|
29
31
|
|
|
30
32
|
/** how long an audit answers for the tree: about a day, like SK030 */
|
|
@@ -88,8 +90,11 @@ function inTree(p: string, declared: string[], extras: string[]): boolean {
|
|
|
88
90
|
/** the fewest entries that cover what was read: each file by itself, or a
|
|
89
91
|
* dir by content once more than three files under it were read (its own
|
|
90
92
|
* dir, or the outermost dir the task also listed); each listed dir as
|
|
91
|
-
* `dir/**`, the outermost one only, unless a content entry covers it
|
|
92
|
-
|
|
93
|
+
* `dir/**`, the outermost one only, unless a content entry covers it.
|
|
94
|
+
* An entry `leaks` refuses (the commit leak guard, which reads the config
|
|
95
|
+
* the fix pass writes) becomes its nearest parent it lets through, in the
|
|
96
|
+
* same form; one with no such parent below the root is left out */
|
|
97
|
+
export function proposeInputs(content: string[], listed: string[], leaks: (entry: string) => boolean = () => false): string[] {
|
|
93
98
|
const minimal = (xs: string[]): string[] => {
|
|
94
99
|
const sorted = [...new Set(xs)].sort()
|
|
95
100
|
return sorted.filter((x) => !sorted.some((y) => y !== x && under(x, y)))
|
|
@@ -105,7 +110,15 @@ export function proposeInputs(content: string[], listed: string[]): string[] {
|
|
|
105
110
|
const again = new Map<string, string[]>()
|
|
106
111
|
for (const [d, f] of regroup) again.set(d, [...(again.get(d) ?? []), f])
|
|
107
112
|
const bytes = minimal([...again].flatMap(([dir, files]) => (files.length > 3 && dir !== '.' ? [dir] : files)))
|
|
108
|
-
|
|
113
|
+
const lifted = (entry: string, names: boolean): string | null => {
|
|
114
|
+
const form = (d: string): string => (names ? `${d}/**` : d)
|
|
115
|
+
let d = entry
|
|
116
|
+
while (d !== '.' && leaks(form(d))) d = posix.dirname(d)
|
|
117
|
+
return d === '.' ? null : d
|
|
118
|
+
}
|
|
119
|
+
const byContent = minimal(bytes.map((b) => lifted(b, false)).filter((b): b is string => b != null))
|
|
120
|
+
const byNames = minimal(tops.map((t) => lifted(t, true)).filter((t): t is string => t != null)).filter((d) => !byContent.some((b) => under(d, b)))
|
|
121
|
+
return [...byContent, ...byNames.map((d) => `${d}/**`)].sort()
|
|
109
122
|
}
|
|
110
123
|
|
|
111
124
|
/** the dirs a task depends on as dirs: every one it listed, and every one a
|
|
@@ -130,6 +143,7 @@ export async function traceAudit(root: string, config: RepoConfig, o: { fresh?:
|
|
|
130
143
|
if (off != null) return { at: now, skipped: off, tasks: [] }
|
|
131
144
|
const tasks: AuditTask[] = []
|
|
132
145
|
const toolkit = toolkitDir(root)
|
|
146
|
+
const leaks = (entry: string): boolean => runLeakGuard(root, { keywordFile: config.hooks?.keywordFile, allow: config.hooks?.leakAllow, staged: false, text: [{ label: 'ignoredInputs proposal', content: entry }] }).status === 'hit'
|
|
133
147
|
for (const g of await composeChecks(root, config)) {
|
|
134
148
|
const u = g.runnable
|
|
135
149
|
if (u.body.cmd == null || g.row.runs.off != null || !g.row.runs.onCi || g.row.decl.inputs == null) continue
|
|
@@ -137,7 +151,9 @@ export async function traceAudit(root: string, config: RepoConfig, o: { fresh?:
|
|
|
137
151
|
const t = tracedCommand(u.body.cmd, cwd, env)
|
|
138
152
|
if ('reason' in t) { tasks.push({ id: u.id, status: 'unaudited', why: t.reason, uncovered: [], propose: [] }); continue }
|
|
139
153
|
try {
|
|
140
|
-
|
|
154
|
+
// every suite runs, with no selection: the selection's own walk of the tree (git status, a read of each
|
|
155
|
+
// dirty file) is no suite's read, and a suite it leaves out is a suite the audit never sees
|
|
156
|
+
await spawnWithTimeout(t.cmd, { timeoutMs: u.timeoutMs, env: { ...process.env, ...u.body.env, ...t.env, [SUITE_SELECT_ENV]: '0' }, cwd })
|
|
141
157
|
const r = await readTrace(t.dir, root)
|
|
142
158
|
if (r.reads == null) {
|
|
143
159
|
const learned = r.blind.length > 0 ? learnBlind(r.blind) : null
|
|
@@ -156,7 +172,7 @@ export async function traceAudit(root: string, config: RepoConfig, o: { fresh?:
|
|
|
156
172
|
const outside = declared == null ? new Set<string>() : inWorkTree(root, files.filter((p) => !ignored.has(p) && !(toolkit != null && under(p, toolkit)) && !inTree(p, declared, extras)))
|
|
157
173
|
const missF = files.filter((p) => (ignored.has(p) ? !covered(p, false, extras) : outside.has(p)))
|
|
158
174
|
const missD = dirs.filter((p) => ignored.has(p) && !covered(p, true, extras))
|
|
159
|
-
tasks.push({ id: u.id, status: 'audited', uncovered: [...missF, ...missD].sort(), propose: proposeInputs(missF, missD) })
|
|
175
|
+
tasks.push({ id: u.id, status: 'audited', uncovered: [...missF, ...missD].sort(), propose: proposeInputs(missF, missD, leaks) })
|
|
160
176
|
} finally {
|
|
161
177
|
dropTrace(t.dir)
|
|
162
178
|
}
|
package/src/drill/run.ts
CHANGED
|
@@ -205,10 +205,13 @@ export function drillRun(lines: readonly string[], o: { transcript: string; root
|
|
|
205
205
|
for (const e of eventsOf(entry)) {
|
|
206
206
|
if (e.kind === 'use') {
|
|
207
207
|
const shape = toolShape(e.name, e.detail)
|
|
208
|
-
pending.set(e.id, { id: e.id, start: e.at, end: null, tool: e.name, shape, cmd: e.detail, outcome: null, code: null, approval: false, perm: '', read: isReadOnly({ tool: e.name, shape, cmd: e.detail }) })
|
|
208
|
+
pending.set(e.id, { id: e.id, start: e.at, end: null, tool: e.name, shape, cmd: e.detail, outcome: null, code: null, approval: false, perm: '', read: isReadOnly({ tool: e.name, shape, cmd: e.detail }), bg: false, bgEnd: null })
|
|
209
209
|
} else if (e.kind === 'result') {
|
|
210
210
|
const c = pending.get(e.id)
|
|
211
|
-
if (c != null) Object.assign(c, { end: e.at, outcome: e.outcome, code: e.code, approval: e.perm === 'user', perm: e.perm })
|
|
211
|
+
if (c != null) Object.assign(c, { end: e.at, outcome: e.outcome, code: e.code, approval: e.perm === 'user', perm: e.perm, bg: e.bg })
|
|
212
|
+
} else if (e.kind === 'bgend') {
|
|
213
|
+
const c = pending.get(e.id)
|
|
214
|
+
if (c != null && (c.bgEnd == null || e.at < c.bgEnd)) c.bgEnd = e.at
|
|
212
215
|
} else if (e.kind !== 'prompt') marks.push({ at: e.at, kind: e.kind })
|
|
213
216
|
}
|
|
214
217
|
}
|
package/src/home/daemon-jobs.ts
CHANGED
|
@@ -8,10 +8,11 @@
|
|
|
8
8
|
// every page; a sync walk, a VACUUM or a hash pass there holds every page with it (tests/daemon-jobs.test.ts)
|
|
9
9
|
import { existsSync, mkdirSync, readFileSync, rmSync } from 'node:fs'
|
|
10
10
|
import { join } from 'node:path'
|
|
11
|
+
import { homedir } from 'node:os'
|
|
11
12
|
import { machineGet } from '../core/machine-db.ts'
|
|
12
13
|
import { loopWatch } from '../core/loop-watch.ts'
|
|
13
14
|
import { BACKUP_EVERY_MS } from '../media-store/backup/daemon.ts'
|
|
14
|
-
import { MEDIA_EVERY_MS } from '../media-store/daemon.ts'
|
|
15
|
+
import { MEDIA_EVERY_MS, mediaDue } from '../media-store/daemon.ts'
|
|
15
16
|
import { ANNEX_EVERY_MS, annexDue } from '../annex/tidy.ts'
|
|
16
17
|
import { SWEEP_EVERY_MS, SWEPT_AT } from './machine-stores.ts'
|
|
17
18
|
import { contextSourcesStamp, GLOBAL_CONTEXT_DONE, GLOBAL_CONTEXT_EVERY_MS, GLOBAL_CONTEXT_MAX_MS } from './global-context.ts'
|
|
@@ -28,6 +29,14 @@ export interface DaemonJob {
|
|
|
28
29
|
/** a cheap read in the daemon (a stat, never a walk; async when it gives the loop back between two): false skips
|
|
29
30
|
* the child for this look. `root` is the house repo the children run in */
|
|
30
31
|
due?: (now: number, root: string) => boolean | Promise<boolean>
|
|
32
|
+
/** what the daemon holds for this job while it runs (a file watch whose callback only notes a path): started with
|
|
33
|
+
* the jobs, stopped with them; its `due` replaces the job's */
|
|
34
|
+
hold?: (root: string, printErr: (l: string) => void) => Promise<JobHold>
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface JobHold {
|
|
38
|
+
due: (now: number) => boolean
|
|
39
|
+
stop: () => void
|
|
31
40
|
}
|
|
32
41
|
|
|
33
42
|
/** a child gets this long; past it, it is killed and said */
|
|
@@ -36,17 +45,33 @@ export const JOB_MAX_MS = 60 * 60_000
|
|
|
36
45
|
export const JOB_NICE = 15
|
|
37
46
|
|
|
38
47
|
export const DAEMON_JOBS: readonly DaemonJob[] = [
|
|
39
|
-
|
|
48
|
+
// a stat of each store folder: a pass that found nothing waits MEDIA_QUIET_MS, unless a store moves
|
|
49
|
+
{ id: 'media store', everyMs: MEDIA_EVERY_MS, firstMs: 15_000, due: (now) => mediaDue(now) },
|
|
40
50
|
// the scratch on the annex: a spill when the drive leaves, merged back when it returns, born folders, stale scratch
|
|
41
51
|
{ id: 'annex', everyMs: ANNEX_EVERY_MS, firstMs: 20_000, due: (now) => annexDue(now) },
|
|
42
52
|
{ id: 'retention sweep', everyMs: 5 * 60_000, firstMs: 10_000, due: (now) => now - (machineGet<number>(SWEPT_AT) ?? 0) >= SWEEP_EVERY_MS },
|
|
43
|
-
|
|
53
|
+
// a watch of the sources: a look only once a source may have moved, a run waits, or a clock says so
|
|
54
|
+
{ id: 'backup', everyMs: BACKUP_EVERY_MS, firstMs: BACKUP_EVERY_MS, hold: (root, printErr) => backupHold(root, printErr) },
|
|
44
55
|
// the house's generated context: its unit files are imported to be read, a second of compile the loop never pays
|
|
45
56
|
{ id: 'global context', everyMs: GLOBAL_CONTEXT_EVERY_MS, firstMs: 30_000, due: async (now, root) => contextDue(now, await contextSourcesStamp(root)) },
|
|
46
57
|
// every global process with a url as an app in ~/Applications: built when missing or stale, removed when gone (process-apps.ts)
|
|
47
58
|
{ id: 'apps', everyMs: APPS_EVERY_MS, firstMs: 45_000, due: async (now, root) => appsDue(now, await appsSourcesStamp(root)) },
|
|
48
59
|
]
|
|
49
60
|
|
|
61
|
+
async function backupHold(root: string, printErr: (l: string) => void): Promise<JobHold> {
|
|
62
|
+
const { houseRepos } = await import('../serve/project-gutter.ts')
|
|
63
|
+
const { BackupWatch } = await import('../media-store/backup/watch.ts')
|
|
64
|
+
let w: InstanceType<typeof BackupWatch>
|
|
65
|
+
try {
|
|
66
|
+
const house = houseRepos(root, homedir())
|
|
67
|
+
w = await new BackupWatch({ home: homedir(), repos: [...house.repos, ...house.archived].map((r) => r.path), printErr }).start()
|
|
68
|
+
} catch (e) {
|
|
69
|
+
printErr(`WARN daemon job backup: no watch of its sources (${(e as Error).message}): each look walks every source`)
|
|
70
|
+
w = new BackupWatch({ home: homedir(), repos: [] }).blindly()
|
|
71
|
+
}
|
|
72
|
+
return { due: (now) => w.due(now), stop: () => w.stop() }
|
|
73
|
+
}
|
|
74
|
+
|
|
50
75
|
/** the context is due when its sources' stamp moved since the last write, or that write is old */
|
|
51
76
|
export function contextDue(now: number, stamp: string | null): boolean {
|
|
52
77
|
if (stamp == null) return false
|
|
@@ -99,6 +124,7 @@ export function startJobs(d: JobsDeps, jobs: readonly DaemonJob[] = DAEMON_JOBS,
|
|
|
99
124
|
const pidFile = (id: JobId): string => join(d.dir, `${slug(id)}.pid`)
|
|
100
125
|
const exitFile = (id: JobId): string => join(d.dir, `${slug(id)}.exit`)
|
|
101
126
|
const startedAt = new Map<JobId, number>()
|
|
127
|
+
const holds = new Map<JobId, JobHold>()
|
|
102
128
|
|
|
103
129
|
/** the pid of the job's child that still runs, else null; a finished child's failure said once */
|
|
104
130
|
const running = (id: JobId): number | null => {
|
|
@@ -140,14 +166,22 @@ export function startJobs(d: JobsDeps, jobs: readonly DaemonJob[] = DAEMON_JOBS,
|
|
|
140
166
|
|
|
141
167
|
const look = async (job: DaemonJob, force: boolean): Promise<number | null> => {
|
|
142
168
|
if (running(job.id) != null) return null
|
|
143
|
-
const
|
|
169
|
+
const hold = holds.get(job.id)
|
|
170
|
+
const due = hold != null ? (t: number): boolean => hold.due(t) : job.due
|
|
144
171
|
if (!force && due != null && !(await loopWatch.spanned(`${job.id} (due)`, () => due(now(), d.cwd)))) return null
|
|
145
172
|
return launch(job)
|
|
146
173
|
}
|
|
147
174
|
|
|
148
175
|
const timers: ReturnType<typeof setTimeout>[] = []
|
|
176
|
+
let stopped = false
|
|
149
177
|
if (o.timers !== false)
|
|
150
178
|
for (const job of jobs) {
|
|
179
|
+
// a hold that does not start leaves the job's own due: the look runs as it would with no hold
|
|
180
|
+
if (job.hold != null)
|
|
181
|
+
void job.hold(d.cwd, d.printErr).then(
|
|
182
|
+
(h) => (stopped ? h.stop() : void holds.set(job.id, h)),
|
|
183
|
+
(e) => d.printErr(`WARN daemon job ${job.id}: its watch did not start (${(e as Error).message}): each look runs as with no watch`),
|
|
184
|
+
)
|
|
151
185
|
const tick = (): void => void look(job, false).catch((e) => d.printErr(`WARN daemon job ${job.id}: ${(e as Error).message}`))
|
|
152
186
|
const first = setTimeout(() => {
|
|
153
187
|
tick()
|
|
@@ -161,7 +195,10 @@ export function startJobs(d: JobsDeps, jobs: readonly DaemonJob[] = DAEMON_JOBS,
|
|
|
161
195
|
|
|
162
196
|
return {
|
|
163
197
|
stop: () => {
|
|
198
|
+
stopped = true
|
|
164
199
|
for (const t of timers) clearTimeout(t)
|
|
200
|
+
for (const h of holds.values()) h.stop()
|
|
201
|
+
holds.clear()
|
|
165
202
|
},
|
|
166
203
|
async run(id, ro = {}) {
|
|
167
204
|
const job = jobs.find((j) => j.id === id)
|
|
@@ -300,6 +300,7 @@ final class Delegate: NSObject, NSApplicationDelegate, WKNavigationDelegate, WKU
|
|
|
300
300
|
web.navigationDelegate = self
|
|
301
301
|
web.uiDelegate = self
|
|
302
302
|
web.allowsBackForwardNavigationGestures = true
|
|
303
|
+
web.allowsMagnification = true
|
|
303
304
|
web.pageZoom = zoom
|
|
304
305
|
let frame = NSRect(x: 0, y: 0, width: 1400, height: 900)
|
|
305
306
|
let win = NSWindow(contentRect: frame, styleMask: [.titled, .closable, .miniaturizable, .resizable], backing: .buffered, defer: false)
|
package/src/home/home-launchd.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
// machine resources).
|
|
5
5
|
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'fs'
|
|
6
6
|
import { homedir } from 'os'
|
|
7
|
-
import { join } from 'path'
|
|
7
|
+
import { dirname, join } from 'path'
|
|
8
8
|
import { homeDir, pmLog } from './home-paths.ts'
|
|
9
9
|
|
|
10
10
|
export const LABEL = 'com.shipkit.pm'
|
|
@@ -22,10 +22,18 @@ const esc = (s: string): string => s.replace(/&/g, '&').replace(/</g, '<'
|
|
|
22
22
|
* every command, so any edit would restart pm. pm,
|
|
23
23
|
* the daemon and the web each watch their own code and restart in place on
|
|
24
24
|
* it (lib-watch.ts). PATH is the installing shell's, since launchd starts
|
|
25
|
-
* with almost none and the daemon needs bun, git, ps, lsof and ssh
|
|
25
|
+
* with almost none and the daemon needs bun, git, ps, lsof and ssh; agentPath
|
|
26
|
+
* adds the folders a shell with no rc file lacks (an `ssh mac cmd` install).
|
|
26
27
|
* ProcessType Interactive: with none, launchd throttles the agent's disk
|
|
27
28
|
* reads under load, every child inherits it, and a busy disk left the
|
|
28
29
|
* daemon's boot reads minutes long while its port was already open */
|
|
30
|
+
/** the agent's PATH: the installing shell's, then bun's folder, ~/.local/bin (sk, the secret manager) and homebrew's,
|
|
31
|
+
* each once. An ssh command shell reads no rc file, and an agent installed from one ran with /usr/bin:/bin alone */
|
|
32
|
+
export function agentPath(shell: string | undefined, bun: string, home: string = homedir()): string {
|
|
33
|
+
const dirs = [...(shell ?? '').split(':'), dirname(bun), join(home, '.local/bin'), '/opt/homebrew/bin', '/usr/local/bin', '/usr/bin', '/bin', '/usr/sbin', '/sbin']
|
|
34
|
+
return [...new Set(dirs.filter((d) => d !== ''))].join(':')
|
|
35
|
+
}
|
|
36
|
+
|
|
29
37
|
export function renderPlist(o: { bun: string; cli: string; root: string; path: string; log: string }): string {
|
|
30
38
|
const args = [o.bun, o.cli, 'pm'].map((a) => `\t\t<string>${esc(a)}</string>`).join('\n')
|
|
31
39
|
return `<?xml version="1.0" encoding="UTF-8"?>
|
|
@@ -95,7 +103,7 @@ export async function installHome(o: { root: string; cli: string; print: (l: str
|
|
|
95
103
|
mkdirSync(agentsDir(), { recursive: true })
|
|
96
104
|
mkdirSync(homeDir(), { recursive: true })
|
|
97
105
|
const log = pmLog()
|
|
98
|
-
writeFileSync(file, renderPlist({ bun, cli: o.cli, root: o.root, path: process.env.PATH
|
|
106
|
+
writeFileSync(file, renderPlist({ bun, cli: o.cli, root: o.root, path: agentPath(process.env.PATH, bun), log }))
|
|
99
107
|
// a loaded copy holds the old arguments: out first, then in
|
|
100
108
|
await run(['bootout', `${domain()}/${LABEL}`])
|
|
101
109
|
await run(['bootout', `${domain()}/${LEGACY_LABEL}`])
|
package/src/home/home-routes.ts
CHANGED
|
@@ -32,6 +32,7 @@ export const GIT_PAGE_MS = 7 * 24 * 3600_000
|
|
|
32
32
|
import type { HouseView } from '../ledger/house-runs.ts'
|
|
33
33
|
import { identitiesRoute } from '../identities/machine-route.ts'
|
|
34
34
|
import { siteRoutes } from '../site/routes.ts'
|
|
35
|
+
import type { OtherMac } from './other-macs.ts'
|
|
35
36
|
|
|
36
37
|
/** how often the daemon looks at every repo */
|
|
37
38
|
export const TICK_MS = 2_000
|
|
@@ -48,6 +49,10 @@ export type HomeAnswer = {
|
|
|
48
49
|
modernize: Record<string, ModernizeView>
|
|
49
50
|
/** the daemon runs older code than its tree, and why (core/last-good.ts) */
|
|
50
51
|
held?: string | null
|
|
52
|
+
/** the other Macs whose home answers on the tailnet (other-macs.ts) */
|
|
53
|
+
otherMacs?: OtherMac[]
|
|
54
|
+
/** this Mac's short tailnet name, shown beside them */
|
|
55
|
+
here?: string | null
|
|
51
56
|
}
|
|
52
57
|
|
|
53
58
|
export type HomeDeps = {
|
|
@@ -70,8 +75,10 @@ export type HomeDeps = {
|
|
|
70
75
|
widgets?: Widgets
|
|
71
76
|
/** brings a repo current (modernizeCmd); resolves to its exit code */
|
|
72
77
|
modernize?: (repo: WiredRepo, log: string) => Promise<number>
|
|
73
|
-
/**
|
|
74
|
-
publicHost?: string
|
|
78
|
+
/** this Mac's tailnet name (machine-host.ts, serve.publicHost overriding it): the one name besides localhost the web answers to */
|
|
79
|
+
publicHost?: () => string | null
|
|
80
|
+
/** the other Macs' homes, the last answer at once */
|
|
81
|
+
otherMacs?: () => OtherMac[]
|
|
75
82
|
}
|
|
76
83
|
|
|
77
84
|
/** the sizes the app icon is drawn at: the phone's home screen, the manifest's two */
|
|
@@ -165,7 +172,7 @@ export function homeRoutes(d: HomeDeps) {
|
|
|
165
172
|
const modernize: Record<string, ModernizeView> = {}
|
|
166
173
|
for (const [k, m] of modernizing) modernize[k] = { status: m.status, code: m.code, tail: tailLines(m.log, 40) }
|
|
167
174
|
const h = heldNow()
|
|
168
|
-
return { version: LIB_VERSION, workers: d.hubs.views(repos), active: d.state.data.active, behind, modernize, held: h == null ? null : heldText(h) }
|
|
175
|
+
return { version: LIB_VERSION, workers: d.hubs.views(repos), active: d.state.data.active, behind, modernize, held: h == null ? null : heldText(h), otherMacs: d.otherMacs?.() ?? [], here: d.publicHost?.()?.split('.')[0] ?? null }
|
|
169
176
|
}
|
|
170
177
|
|
|
171
178
|
const icons = new Map<number, Promise<Uint8Array<ArrayBuffer> | null>>()
|
|
@@ -219,7 +226,7 @@ export function homeRoutes(d: HomeDeps) {
|
|
|
219
226
|
// the web asks which page routes to serve: one per house repo
|
|
220
227
|
if (path === '/api/home/repos.json') {
|
|
221
228
|
if (repos.length === 0) await refresh()
|
|
222
|
-
return Response.json({ repos: d.hubs.names(), hosts: d.publicHost
|
|
229
|
+
return Response.json({ repos: d.hubs.names(), hosts: [d.publicHost?.()].filter((h): h is string => h != null) })
|
|
223
230
|
}
|
|
224
231
|
// everything that waits on the owner in every repo, one list (inbox.ts)
|
|
225
232
|
if (path === '/api/inbox') {
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// this Mac's two names. `machineName` keys what one Mac writes beside another's in a store they share
|
|
2
|
+
// (a backup run, its block of a tracked page); `machinePublicHost` is the name its other devices reach it by.
|
|
3
|
+
// Two Macs never share a value of either, so neither is ever written in a repo both check out
|
|
4
|
+
// (tools/shipkit/home.md, Two machines). Both live in the machine db once read.
|
|
5
|
+
import { hostname } from 'os'
|
|
6
|
+
import { machineGet, machineSet } from '../core/machine-db.ts'
|
|
7
|
+
|
|
8
|
+
/** a host name without the `.local` of its Bonjour form */
|
|
9
|
+
export const hostLabel = (raw: string): string => raw.replace(/\.local$/i, '')
|
|
10
|
+
|
|
11
|
+
const NAME = 'machine-name'
|
|
12
|
+
|
|
13
|
+
/** this Mac's name, fixed the first time it is read (the system's host name then): macOS renames a host it meets
|
|
14
|
+
* twice on a network, and a block or a run keyed by a new name would orphan the old one */
|
|
15
|
+
export function machineName(): string {
|
|
16
|
+
const held = machineGet<string>(NAME)
|
|
17
|
+
if (held) return held
|
|
18
|
+
const name = hostLabel(hostname())
|
|
19
|
+
if (!/[A-Za-z0-9]/.test(name)) throw new Error(`this Mac's host name (${JSON.stringify(hostname())}) has no letter or digit to key its backups and blocks by: set one in System Settings, Sharing`)
|
|
20
|
+
machineSet(NAME, name)
|
|
21
|
+
return name
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** the tailscale cli: on PATH when its wrapper is installed, else inside the app */
|
|
25
|
+
export const TAILSCALE = ['tailscale', '/Applications/Tailscale.app/Contents/MacOS/Tailscale']
|
|
26
|
+
|
|
27
|
+
/** `Self.DNSName` of `tailscale status --json`, without its final dot; null when no tailscale answers */
|
|
28
|
+
export function readTailnetName(bins: string[] = TAILSCALE): string | null {
|
|
29
|
+
for (const bin of bins) {
|
|
30
|
+
try {
|
|
31
|
+
const p = Bun.spawnSync([bin, 'status', '--json'], { stdout: 'pipe', stderr: 'ignore', timeout: 3000 })
|
|
32
|
+
if (p.exitCode !== 0) continue
|
|
33
|
+
const name = (JSON.parse(p.stdout.toString()) as { Self?: { DNSName?: string } }).Self?.DNSName?.replace(/\.$/, '')
|
|
34
|
+
if (name) return name
|
|
35
|
+
} catch {
|
|
36
|
+
// a missing binary throws: the next one is tried
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return null
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const TAILNET = 'tailnet-name'
|
|
43
|
+
/** how long a process keeps "tailscale did not answer" before it asks again */
|
|
44
|
+
export const TAILNET_RETRY_MS = 60_000
|
|
45
|
+
let asked: { name: string | null; at: number } | undefined
|
|
46
|
+
|
|
47
|
+
/** this Mac's tailnet name: tailscale's answer, read once a process, else the last name it gave (kept in the machine
|
|
48
|
+
* db, since a device keeps its name while tailscale is stopped), asked again a minute later.
|
|
49
|
+
* `SHIPKIT_TAILNET_NAME` sets it, empty for none */
|
|
50
|
+
export function tailnetName(now: number = Date.now(), read: () => string | null = readTailnetName): string | null {
|
|
51
|
+
const env = process.env['SHIPKIT_TAILNET_NAME']
|
|
52
|
+
if (env !== undefined) return env === '' ? null : env
|
|
53
|
+
if (asked === undefined || (asked.name == null && now - asked.at >= TAILNET_RETRY_MS)) {
|
|
54
|
+
asked = { name: read(), at: now }
|
|
55
|
+
if (asked.name != null && machineGet<string>(TAILNET) !== asked.name) machineSet(TAILNET, asked.name)
|
|
56
|
+
}
|
|
57
|
+
return asked.name ?? machineGet<string>(TAILNET)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** forget what this process asked, for a test */
|
|
61
|
+
export const forgetTailnetAnswer = (): void => void (asked = undefined)
|
|
62
|
+
|
|
63
|
+
/** the name a phone or another Mac uses for this one: the config's `serve.publicHost` (a single Mac's override), else
|
|
64
|
+
* its tailnet name */
|
|
65
|
+
export const machinePublicHost = (c: { serve?: { publicHost?: string } }): string | null => c.serve?.publicHost ?? tailnetName()
|