@zabaca/zbc 0.21.1 → 0.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/package.json +6 -1
- package/templates/CLAUDE.md +5 -0
- package/templates/apps/walgit/CONTEXT.md +5 -1
- package/templates/apps/walgit/README.md +159 -24
- package/templates/apps/walgit/package.json +2 -2
- package/templates/apps/walgit/registry.json +1 -1
- package/templates/apps/walgit/shared/browse.ts +18 -4
- package/templates/apps/walgit/shared/capabilities.ts +28 -3
- package/templates/apps/walgit/shared/container-env.ts +41 -2
- package/templates/apps/walgit/shared/container-lifecycle.ts +239 -0
- package/templates/apps/walgit/shared/container-snapshot.ts +219 -0
- package/templates/apps/walgit/shared/edge-refs.ts +612 -0
- package/templates/apps/walgit/{src → shared}/instructions.ts +8 -4
- package/templates/apps/walgit/shared/keys.ts +17 -0
- package/templates/apps/walgit/shared/landing.ts +10 -3
- package/templates/apps/walgit/shared/llms.ts +14 -5
- package/templates/apps/walgit/shared/policy.ts +1 -1
- package/templates/apps/walgit/shared/protocol.ts +35 -0
- package/templates/apps/walgit/shared/sleep-after.ts +44 -0
- package/templates/apps/walgit/shared/wal-index.ts +22 -1
- package/templates/apps/walgit/src/boot-residue.ts +58 -0
- package/templates/apps/walgit/src/cache.ts +9 -0
- package/templates/apps/walgit/src/cli.ts +39 -4
- package/templates/apps/walgit/src/delete-repo.ts +58 -9
- package/templates/apps/walgit/src/edge-refs.ts +148 -0
- package/templates/apps/walgit/src/expire.ts +75 -7
- package/templates/apps/walgit/src/hook-main.ts +2 -0
- package/templates/apps/walgit/src/http.ts +17 -4
- package/templates/apps/walgit/src/materialize.ts +34 -15
- package/templates/apps/walgit/src/push.ts +34 -8
- package/templates/apps/walgit/src/reconcile.ts +6 -1
- package/templates/apps/walgit/src/server.ts +69 -8
- package/templates/apps/walgit/src/sync.ts +10 -2
- package/templates/apps/walgit/worker/container-binding.ts +104 -0
- package/templates/apps/walgit/worker/durable-container.ts +786 -0
- package/templates/apps/walgit/worker/events-do.ts +16 -12
- package/templates/apps/walgit/worker/index.ts +235 -17
- package/templates/apps/walgit/worker/mcp.ts +9 -4
- package/templates/apps/walgit/wrangler.jsonc +44 -33
- package/templates/claude/settings.json +8 -0
- package/templates/infra/modules/cloudflare/index.ts +9 -0
- package/templates/infra/modules/cloudflare-zone/index.ts +83 -9
- package/templates/infra/modules/d1/index.ts +30 -4
- package/templates/infra/modules/d1/registry.json +1 -1
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* The page a browser gets at `/`.
|
|
3
3
|
*
|
|
4
4
|
* The same URL answers two audiences. git and curl send an `Accept` that never
|
|
5
|
-
* names HTML, and get
|
|
5
|
+
* names HTML, and get shared/instructions.ts — plain text, the whole protocol, no
|
|
6
6
|
* markup to parse. A browser sends `Accept: text/html` and gets this. One
|
|
7
7
|
* hostname, one route, and the pitch is never a second deployment to keep in
|
|
8
8
|
* sync with the service it describes.
|
|
@@ -72,6 +72,7 @@ export function wantsLanding(method: string, pathname: string, accept: string):
|
|
|
72
72
|
}
|
|
73
73
|
|
|
74
74
|
export function describeHours(hours: number): string {
|
|
75
|
+
if (hours >= 48 && hours % 24 === 0) return `${hours / 24} days`
|
|
75
76
|
if (hours === 1) return '1 hour'
|
|
76
77
|
return `${hours} hours`
|
|
77
78
|
}
|
|
@@ -135,7 +136,9 @@ function limits(caps: Capabilities): string {
|
|
|
135
136
|
function permanence(caps: Capabilities): string {
|
|
136
137
|
return caps.retentionHours === null
|
|
137
138
|
? 'Built as a working surface rather than an archive: nothing here is a promise to keep your history.'
|
|
138
|
-
:
|
|
139
|
+
: caps.claimedRetentionHours === null
|
|
140
|
+
? `Not permanent: ${describeHours(caps.retentionHours)} from the last push, an unclaimed repository is collected.`
|
|
141
|
+
: `Not permanent: ${describeHours(caps.retentionHours)} from the last push, an unclaimed repository is collected — a claimed one, ${describeHours(caps.claimedRetentionHours)}.`
|
|
139
142
|
}
|
|
140
143
|
|
|
141
144
|
/**
|
|
@@ -957,7 +960,11 @@ function operatorSection(caps: Capabilities, operator: Operator | null): string
|
|
|
957
960
|
rows.push(
|
|
958
961
|
claim(
|
|
959
962
|
'Expiry',
|
|
960
|
-
`<b>A repository is collected ${window} after its last push</b
|
|
963
|
+
`<b>A repository is collected ${window} after its last push</b>${
|
|
964
|
+
caps.claimedRetentionHours === null
|
|
965
|
+
? ''
|
|
966
|
+
: ` (${describeHours(caps.claimedRetentionHours)} once claimed)`
|
|
967
|
+
}, whether or not anybody asks. Nothing here is archived.`,
|
|
961
968
|
),
|
|
962
969
|
)
|
|
963
970
|
}
|
|
@@ -42,7 +42,8 @@ export function wantsLlms(method: string, pathname: string): boolean {
|
|
|
42
42
|
return pathname === '/llms.txt'
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
-
const hours = (n: number) =>
|
|
45
|
+
const hours = (n: number) =>
|
|
46
|
+
n >= 48 && n % 24 === 0 ? `${n / 24} days` : n === 1 ? '1 hour' : `${n} hours`
|
|
46
47
|
|
|
47
48
|
export function renderLlms(
|
|
48
49
|
host: string,
|
|
@@ -108,7 +109,11 @@ export function renderLlms(
|
|
|
108
109
|
}
|
|
109
110
|
if (caps.retentionHours !== null) {
|
|
110
111
|
limits.push(
|
|
111
|
-
`- **A repository is deleted ${hours(caps.retentionHours)} after its LAST push.** Cloning does not extend it; pushing does
|
|
112
|
+
`- **A repository is deleted ${hours(caps.retentionHours)} after its LAST push.** Cloning does not extend it; pushing does.${
|
|
113
|
+
caps.claimedRetentionHours !== null
|
|
114
|
+
? ` A claimed name (see Ownership below) is kept ${hours(caps.claimedRetentionHours)} instead.`
|
|
115
|
+
: ''
|
|
116
|
+
} This is scratch space. Copy anything that must outlive that window.`,
|
|
112
117
|
)
|
|
113
118
|
}
|
|
114
119
|
if (caps.maxPushBytes !== null) {
|
|
@@ -293,7 +298,7 @@ it will say so on this page first.`
|
|
|
293
298
|
const lostKey =
|
|
294
299
|
caps.retentionHours === null
|
|
295
300
|
? 'A lost key ends the name: nobody can push to it again.'
|
|
296
|
-
: `Where a key is lost, ${hours(caps.retentionHours)} without a push collects the repository
|
|
301
|
+
: `Where a key is lost, ${hours(caps.claimedRetentionHours ?? caps.retentionHours)} without a push collects the repository
|
|
297
302
|
and frees the name with it — the only way back.`
|
|
298
303
|
|
|
299
304
|
/**
|
|
@@ -655,7 +660,11 @@ ${[
|
|
|
655
660
|
: `- **Contact.** ${operator.contact} — takedowns, abuse and anything else about this host.`,
|
|
656
661
|
caps.retentionHours === null
|
|
657
662
|
? null
|
|
658
|
-
: `- **Expiry.** A repository is collected ${hours(caps.retentionHours)} after its last push
|
|
663
|
+
: `- **Expiry.** A repository is collected ${hours(caps.retentionHours)} after its last push${
|
|
664
|
+
caps.claimedRetentionHours !== null
|
|
665
|
+
? ` (${hours(caps.claimedRetentionHours)} once claimed)`
|
|
666
|
+
: ''
|
|
667
|
+
}, whether or not anybody asks. Nothing here is archived.`,
|
|
659
668
|
]
|
|
660
669
|
.filter((line): line is string => line !== null)
|
|
661
670
|
.join('\n')}
|
|
@@ -865,6 +874,6 @@ Not a forge: no pull requests, no code review, no CI, no issues.${
|
|
|
865
874
|
caps.publicAccess && !caps.namesCanBePrivate
|
|
866
875
|
? ' Not private: everything here is readable by everyone.'
|
|
867
876
|
: ''
|
|
868
|
-
}${caps.retentionHours !== null ? ` Not permanent: ${hours(caps.retentionHours)} from the last push, a repository is collected.` : ' Not an archive: nothing here is a promise to keep your history.'} Not a place for anything you cannot lose.
|
|
877
|
+
}${caps.retentionHours !== null ? ` Not permanent: ${hours(caps.retentionHours)} from the last push, a repository is collected${caps.claimedRetentionHours !== null ? ` (${hours(caps.claimedRetentionHours)} once claimed)` : ''}.` : ' Not an archive: nothing here is a promise to keep your history.'} Not a place for anything you cannot lose.
|
|
869
878
|
`
|
|
870
879
|
}
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Both halves state walgit's limits and only one of them enforces them: the
|
|
5
5
|
* container refuses an oversized push in `pre-receive` (`src/limits.ts`), while
|
|
6
|
-
* `GET /` (`
|
|
6
|
+
* `GET /` (`shared/instructions.ts`) and the landing page (`shared/landing.ts`)
|
|
7
7
|
* describe the caps to whoever is about to push. A page that promised a number
|
|
8
8
|
* the hook does not hold would be a lie told at the top of the funnel, and the
|
|
9
9
|
* only reliable way not to tell it is for the statement and the enforcement to
|
|
@@ -78,6 +78,21 @@ export const HEALTH_PATH = '/_walgit/health'
|
|
|
78
78
|
/** The sweeper's front door. `INTERNAL_HEADER` only — see above. */
|
|
79
79
|
export const EXPIRE_PATH = '/_walgit/expire'
|
|
80
80
|
|
|
81
|
+
/**
|
|
82
|
+
* The sweeper's two Cron Triggers, which `wrangler.jsonc` must list verbatim —
|
|
83
|
+
* the Worker tells them apart by the expression it was fired with.
|
|
84
|
+
*
|
|
85
|
+
* The first is a full sweep: it tombstones what has gone idle and collects
|
|
86
|
+
* what is past its grace. The second runs a grace period later and only
|
|
87
|
+
* collects, so a repository tombstoned at 04:00 is gone by 05:15 rather than
|
|
88
|
+
* waiting a day for the next full sweep — 75 minutes covers the default
|
|
89
|
+
* one-hour `WALGIT_DELETE_GRACE_MS` and the time the first sweep itself takes.
|
|
90
|
+
* A longer grace is safe, just slower: whatever the second sweep finds still
|
|
91
|
+
* inside it is collected by the next day's first.
|
|
92
|
+
*/
|
|
93
|
+
export const SWEEP_CRON = '0 4 * * *'
|
|
94
|
+
export const COLLECT_CRON = '15 5 * * *'
|
|
95
|
+
|
|
81
96
|
/** One repository's ref state, for the event stream's handshake. Internal. */
|
|
82
97
|
export const REFS_PATH = '/_walgit/refs'
|
|
83
98
|
|
|
@@ -318,6 +333,26 @@ export const SMART_HTTP = /^\/([^/]+)\.git\/(info\/refs|git-upload-pack|git-rece
|
|
|
318
333
|
*/
|
|
319
334
|
export const PROPOSALS_HTTP = /^\/([^/]+)\.git\/proposals$/
|
|
320
335
|
|
|
336
|
+
/**
|
|
337
|
+
* Would the container route this path at all?
|
|
338
|
+
*
|
|
339
|
+
* Everything the container answers lives under one of four shapes: `/`, the
|
|
340
|
+
* reserved `/_walgit/` prefix, the three smart-HTTP endpoints, and the Proposal
|
|
341
|
+
* read. Any other path is a 404 there, and on a public deployment the edge says
|
|
342
|
+
* so itself rather than waking the one container to hear it — a scanner asking
|
|
343
|
+
* for `/.env` every few minutes is otherwise enough to keep it from ever
|
|
344
|
+
* sleeping. A route added to `src/http.ts` outside these shapes must be added
|
|
345
|
+
* here too; `src/edge-answers.test.ts` holds the two lists against each other.
|
|
346
|
+
*/
|
|
347
|
+
export function containerRoutes(pathname: string): boolean {
|
|
348
|
+
return (
|
|
349
|
+
pathname === '/' ||
|
|
350
|
+
pathname.startsWith('/_walgit/') ||
|
|
351
|
+
SMART_HTTP.test(pathname) ||
|
|
352
|
+
PROPOSALS_HTTP.test(pathname)
|
|
353
|
+
)
|
|
354
|
+
}
|
|
355
|
+
|
|
321
356
|
/**
|
|
322
357
|
* A repo id is one flat segment.
|
|
323
358
|
*
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How long the container stays up after the last request it served.
|
|
3
|
+
*
|
|
4
|
+
* Every request that reaches the container buys this much container time, so
|
|
5
|
+
* on a quiet deployment it is most of the bill. The Durable Object in front of
|
|
6
|
+
* it no longer pays the same: it sleeps between requests and wakes once, on an
|
|
7
|
+
* alarm, to stop the container (`WalgitDurableContainer`, worker/durable-container.ts).
|
|
8
|
+
* Shorter costs cold starts instead: the first request after a sleep waits for
|
|
9
|
+
* boot, then for its repository to be materialized from the log — or, with
|
|
10
|
+
* `WALGIT_SNAPSHOTS` on, for a re-sync against a restored disk. Which trade is
|
|
11
|
+
* right depends on the traffic, so it is the instance's choice
|
|
12
|
+
* (`WALGIT_SLEEP_AFTER`), and the default is the cautious one.
|
|
13
|
+
*
|
|
14
|
+
* Edge-only: the Durable Object reads it and the container never needs to, so
|
|
15
|
+
* it is not on `CONTAINER_ENV` and changing it restarts nothing.
|
|
16
|
+
*/
|
|
17
|
+
export const DEFAULT_SLEEP_AFTER = '20m'
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* A number and one unit — the syntax `@cloudflare/containers` read when its
|
|
21
|
+
* `Container` class ran the idle stop. Kept after the class went, because it
|
|
22
|
+
* is instance configuration: a deployment that wrote `5m` must not have to
|
|
23
|
+
* learn a second spelling for the same knob.
|
|
24
|
+
*/
|
|
25
|
+
const DURATION = /^[1-9][0-9]*(s|m|h)$/
|
|
26
|
+
|
|
27
|
+
const UNIT_MS = { s: 1000, m: 60 * 1000, h: 60 * 60 * 1000 } as const
|
|
28
|
+
|
|
29
|
+
/** A value that does not parse is ignored for the default, not obeyed. */
|
|
30
|
+
export function sleepAfterFrom(env: { WALGIT_SLEEP_AFTER?: string }): string {
|
|
31
|
+
const value = (env.WALGIT_SLEEP_AFTER ?? '').trim()
|
|
32
|
+
return DURATION.test(value) ? value : DEFAULT_SLEEP_AFTER
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The same choice, in milliseconds — what the Durable Object's alarm is armed
|
|
37
|
+
* with. Derived from `sleepAfterFrom` rather than parsed beside it, so the two
|
|
38
|
+
* cannot accept different spellings.
|
|
39
|
+
*/
|
|
40
|
+
export function sleepAfterMsFrom(env: { WALGIT_SLEEP_AFTER?: string }): number {
|
|
41
|
+
const value = sleepAfterFrom(env)
|
|
42
|
+
const unit = value.slice(-1) as keyof typeof UNIT_MS
|
|
43
|
+
return Number(value.slice(0, -1)) * UNIT_MS[unit]
|
|
44
|
+
}
|
|
@@ -62,10 +62,31 @@ export interface Tombstone {
|
|
|
62
62
|
* twice does not shorten the wait — the first request's `collect_after` stands.
|
|
63
63
|
*/
|
|
64
64
|
export interface RepoDeletion {
|
|
65
|
-
/** ISO instant the
|
|
65
|
+
/** ISO instant the repository was asked to go — by an operator, or by expiry. */
|
|
66
66
|
requested_at: string
|
|
67
67
|
/** ISO instant before which nothing under the repo prefix may be deleted. */
|
|
68
68
|
collect_after: string
|
|
69
|
+
/**
|
|
70
|
+
* `'expiry'` when the sweeper wrote this marker; absent when an operator did
|
|
71
|
+
* (`walgit delete`), which is also how every marker written before the field
|
|
72
|
+
* existed reads.
|
|
73
|
+
*
|
|
74
|
+
* The difference is what a push means. Expiry marks a repository because
|
|
75
|
+
* nobody pushed to it, so a push answers the question and lifts the mark
|
|
76
|
+
* (`liftExpiry`, `src/push.ts`). An operator's mark is a decision about the
|
|
77
|
+
* repository, and a push does not overrule it.
|
|
78
|
+
*/
|
|
79
|
+
by?: 'expiry'
|
|
80
|
+
/**
|
|
81
|
+
* Set by the collection itself, under compare-and-swap, before it deletes
|
|
82
|
+
* anything. From then on a push is refused rather than published: deletion
|
|
83
|
+
* does not re-read the Index between removing `index.json` and removing the
|
|
84
|
+
* objects, so a push that landed in that gap would be acknowledged and then
|
|
85
|
+
* lost. The write is what turns that race into a refusal — a push reading
|
|
86
|
+
* the Index before it loses its own compare-and-swap, and one reading it
|
|
87
|
+
* after sees this field.
|
|
88
|
+
*/
|
|
89
|
+
collecting_at?: string
|
|
69
90
|
}
|
|
70
91
|
|
|
71
92
|
export interface WalIndex {
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Clear what a previous process left on the disk that only a process can own.
|
|
3
|
+
*
|
|
4
|
+
* The Cache is reconciled against the Index on every access, so whatever the
|
|
5
|
+
* disk believes about refs and objects is corrected before it is served
|
|
6
|
+
* (src/sync.ts). Two things on it are NOT about the repository at all, and
|
|
7
|
+
* reconcile never looks at them:
|
|
8
|
+
*
|
|
9
|
+
* - a push's hand-off record (src/pending.ts), keyed by the pid of the
|
|
10
|
+
* `git-receive-pack` that wrote it. The sweep that removes dead ones asks
|
|
11
|
+
* whether that pid is alive — and a pid from before a restart can be alive
|
|
12
|
+
* again as somebody else, in a container whose pids start from 1 every
|
|
13
|
+
* time. Within the record's hour a push whose receive-pack drew that pid
|
|
14
|
+
* would read a stranger's hand-off.
|
|
15
|
+
* - a materialize lock (src/materialize.ts). Broken after six seconds by
|
|
16
|
+
* design, so it costs a wait rather than a wedge — but a wait on the first
|
|
17
|
+
* clone after a wake, which is the request this path exists to make fast.
|
|
18
|
+
*
|
|
19
|
+
* Neither could survive a restart while the disk was wiped on every one. With
|
|
20
|
+
* filesystem snapshots (shared/container-snapshot.ts) the disk outlives the
|
|
21
|
+
* process, so the process clears them itself, once, before it serves anything.
|
|
22
|
+
* At that moment the answer is exact rather than a heuristic: nothing that
|
|
23
|
+
* could hold either exists yet, so every one found is residue.
|
|
24
|
+
*
|
|
25
|
+
* The materialize MARKER is deliberately left alone. It is not process state —
|
|
26
|
+
* it is the evidence that a restore died partway, and `syncRepo` trusts it over
|
|
27
|
+
* the refs, which is exactly what a disk restored mid-restore needs.
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
import * as fs from 'node:fs'
|
|
31
|
+
import * as path from 'node:path'
|
|
32
|
+
|
|
33
|
+
import { lockPath } from './materialize'
|
|
34
|
+
import { pendingDir } from './pending'
|
|
35
|
+
|
|
36
|
+
/** Remove every hand-off record and materialize lock under `reposDir`. */
|
|
37
|
+
export function clearBootResidue(reposDir: string): { repos: number; cleared: string[] } {
|
|
38
|
+
let names: string[]
|
|
39
|
+
try {
|
|
40
|
+
names = fs.readdirSync(reposDir)
|
|
41
|
+
} catch {
|
|
42
|
+
// No repos directory yet is the ordinary first boot of an empty disk.
|
|
43
|
+
return { repos: 0, cleared: [] }
|
|
44
|
+
}
|
|
45
|
+
const cleared: string[] = []
|
|
46
|
+
let repos = 0
|
|
47
|
+
for (const name of names) {
|
|
48
|
+
if (!name.endsWith('.git')) continue
|
|
49
|
+
repos += 1
|
|
50
|
+
const gitDir = path.join(reposDir, name)
|
|
51
|
+
for (const residue of [pendingDir(gitDir), lockPath(gitDir)]) {
|
|
52
|
+
if (!fs.existsSync(residue)) continue
|
|
53
|
+
fs.rmSync(residue, { recursive: true, force: true })
|
|
54
|
+
cleared.push(residue)
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return { repos, cleared }
|
|
58
|
+
}
|
|
@@ -91,6 +91,15 @@ export function ensureBareRepo(repo: ResolvedRepo): ResolvedRepo {
|
|
|
91
91
|
// is what makes a small push arrive as a packfile rather than exploding into
|
|
92
92
|
// loose objects, and there is no packfile to upload if it does.
|
|
93
93
|
ensureConfig(repo.dir, 'receive.unpackLimit', '0')
|
|
94
|
+
// HEAD follows the Index now (`ensureHead`, re-run on every sync), so it is
|
|
95
|
+
// always on a branch that exists — and git's default refuses to delete the
|
|
96
|
+
// branch HEAD names, even in a bare repo. That refusal used to be unreachable
|
|
97
|
+
// because HEAD sat on an unborn `main`; with HEAD kept honest it would make a
|
|
98
|
+
// repo's only-other branch undeletable, and an atomic "create main, delete
|
|
99
|
+
// master" impossible. The next sync moves HEAD off a deleted branch, which is
|
|
100
|
+
// the job `ensureHead` already does. Append-only deployments refuse every
|
|
101
|
+
// deletion anyway (`receive.denyDeletes` below).
|
|
102
|
+
ensureConfig(repo.dir, 'receive.denyDeleteCurrent', 'ignore')
|
|
94
103
|
// git's own housekeeping is turned OFF, and that is not a performance
|
|
95
104
|
// choice. `receive.autogc` runs `git gc --auto` after a push, which on a repo
|
|
96
105
|
// holding one pack per WAL entry fires almost immediately (gc.autoPackLimit
|
|
@@ -27,7 +27,7 @@ import * as path from 'node:path'
|
|
|
27
27
|
|
|
28
28
|
import { compact, configuredGraceMs, type CompactResult } from './compact'
|
|
29
29
|
import { configuredDeleteGraceMs, deleteRepo } from './delete-repo'
|
|
30
|
-
import { configuredExpiryMs, expireRepos } from './expire'
|
|
30
|
+
import { configuredClaimedExpiryMs, configuredExpiryMs, expireRepos } from './expire'
|
|
31
31
|
import { collectGarbage } from './gc'
|
|
32
32
|
import { materialize, round } from './materialize'
|
|
33
33
|
import { normalizeRepoId, resolveRepo } from './repo'
|
|
@@ -52,7 +52,15 @@ export interface ParsedArgs {
|
|
|
52
52
|
* `gc myrepo --yes myotherrepo` swallow a repo id: a flag takes the next token
|
|
53
53
|
* only when it is declared to want one.
|
|
54
54
|
*/
|
|
55
|
-
const KNOWN_VALUE_FLAGS = new Set([
|
|
55
|
+
const KNOWN_VALUE_FLAGS = new Set([
|
|
56
|
+
'min-age',
|
|
57
|
+
'repos-dir',
|
|
58
|
+
'grace',
|
|
59
|
+
'after',
|
|
60
|
+
'claimed-after',
|
|
61
|
+
'since',
|
|
62
|
+
'top',
|
|
63
|
+
])
|
|
56
64
|
|
|
57
65
|
export function parseArgs(argv: readonly string[]): ParsedArgs {
|
|
58
66
|
const positional: string[] = []
|
|
@@ -96,6 +104,10 @@ Options
|
|
|
96
104
|
--min-age <minutes> gc: never collect an object younger than this (default 60)
|
|
97
105
|
--grace <minutes> delete/expire: how long a repo is tombstoned first (default 60)
|
|
98
106
|
--after <hours> expire: idle window (default $WALGIT_RETENTION_HOURS; unset = off)
|
|
107
|
+
--claimed-after <hours>
|
|
108
|
+
expire: window for a claimed repo (default
|
|
109
|
+
$WALGIT_CLAIMED_RETENTION_HOURS; unset = same as --after)
|
|
110
|
+
--collect-only expire: collect tombstoned repos, tombstone nothing new
|
|
99
111
|
--since <duration> usage: push window, e.g. 24h, 7d, 30m (default 24h)
|
|
100
112
|
--top <n> usage: how many repositories to name (0 = all, default 10)
|
|
101
113
|
|
|
@@ -321,6 +333,17 @@ async function expireCommand(args: ParsedArgs, env: NodeJS.ProcessEnv): Promise<
|
|
|
321
333
|
)
|
|
322
334
|
return MISUSE
|
|
323
335
|
}
|
|
336
|
+
const claimedFlag = args.flags['claimed-after']
|
|
337
|
+
const claimedWindowMs =
|
|
338
|
+
typeof claimedFlag === 'string'
|
|
339
|
+
? Number(claimedFlag) * 3_600_000
|
|
340
|
+
: configuredClaimedExpiryMs(env)
|
|
341
|
+
if (claimedWindowMs !== null && (!Number.isFinite(claimedWindowMs) || claimedWindowMs <= 0)) {
|
|
342
|
+
console.error(
|
|
343
|
+
`walgit expire: --claimed-after must be a positive number of hours, got ${String(claimedFlag)}`,
|
|
344
|
+
)
|
|
345
|
+
return MISUSE
|
|
346
|
+
}
|
|
324
347
|
const graceFlag = args.flags.grace
|
|
325
348
|
const graceMs =
|
|
326
349
|
typeof graceFlag === 'string' ? Number(graceFlag) * 60_000 : configuredDeleteGraceMs(env)
|
|
@@ -334,7 +357,14 @@ async function expireCommand(args: ParsedArgs, env: NodeJS.ProcessEnv): Promise<
|
|
|
334
357
|
// and the timer calling this should not start failing because of it.
|
|
335
358
|
emit(
|
|
336
359
|
args,
|
|
337
|
-
{
|
|
360
|
+
{
|
|
361
|
+
collected: [],
|
|
362
|
+
retained: [],
|
|
363
|
+
windowMs: null,
|
|
364
|
+
claimedWindowMs: null,
|
|
365
|
+
collectOnly: false,
|
|
366
|
+
dryRun: true,
|
|
367
|
+
},
|
|
338
368
|
'expiry is not configured — set WALGIT_RETENTION_HOURS or pass --after <hours>',
|
|
339
369
|
)
|
|
340
370
|
return OK
|
|
@@ -344,6 +374,8 @@ async function expireCommand(args: ParsedArgs, env: NodeJS.ProcessEnv): Promise<
|
|
|
344
374
|
const dryRun = args.flags.yes !== true
|
|
345
375
|
const result = await expireRepos(store, {
|
|
346
376
|
windowMs,
|
|
377
|
+
claimedWindowMs,
|
|
378
|
+
collectOnly: args.flags['collect-only'] === true,
|
|
347
379
|
graceMs,
|
|
348
380
|
dryRun,
|
|
349
381
|
reposDir: reposDirOf(args, env),
|
|
@@ -352,7 +384,10 @@ async function expireCommand(args: ParsedArgs, env: NodeJS.ProcessEnv): Promise<
|
|
|
352
384
|
})
|
|
353
385
|
|
|
354
386
|
const lines: string[] = [
|
|
355
|
-
`window ${windowMs / 3_600_000}h
|
|
387
|
+
`window ${windowMs / 3_600_000}h` +
|
|
388
|
+
(claimedWindowMs === null ? '' : ` (claimed ${claimedWindowMs / 3_600_000}h)`) +
|
|
389
|
+
(result.collectOnly ? ', collect only' : '') +
|
|
390
|
+
` — ${result.collected.length} collected, ` +
|
|
356
391
|
`${result.retained.length} retained`,
|
|
357
392
|
]
|
|
358
393
|
for (const outcome of result.collected) {
|
|
@@ -30,7 +30,7 @@ import * as fs from 'node:fs'
|
|
|
30
30
|
import type { ObjectStore } from '../shared/store'
|
|
31
31
|
import { indexKey, repoPrefix } from '../shared/keys'
|
|
32
32
|
import { loadIndex, type WalIndex } from '../shared/wal-index'
|
|
33
|
-
import { updateIndex } from './wal-index'
|
|
33
|
+
import { commitIndex, updateIndex } from './wal-index'
|
|
34
34
|
|
|
35
35
|
/**
|
|
36
36
|
* How long a repository sits tombstoned before its objects may be deleted.
|
|
@@ -50,6 +50,12 @@ export function configuredDeleteGraceMs(env = process.env): number {
|
|
|
50
50
|
export type DeleteStatus =
|
|
51
51
|
/** Nothing under this repo's prefix and no index: there was nothing to do. */
|
|
52
52
|
| 'absent'
|
|
53
|
+
/**
|
|
54
|
+
* The repository moved under the request — a push landed between the caller
|
|
55
|
+
* deciding and this writing — and `stillDue` said it no longer should go, or
|
|
56
|
+
* a collection lost its compare-and-swap. Nothing was written or removed.
|
|
57
|
+
*/
|
|
58
|
+
| 'superseded'
|
|
53
59
|
/** The marker was written (or would be); the grace period starts now. */
|
|
54
60
|
| 'tombstoned'
|
|
55
61
|
/** Already marked, still inside the grace period. Nothing was removed. */
|
|
@@ -87,6 +93,16 @@ export interface DeleteOptions {
|
|
|
87
93
|
dryRun?: boolean
|
|
88
94
|
/** The bare repo on disk to remove once the log has let go of it. */
|
|
89
95
|
dir?: string
|
|
96
|
+
/** Who is asking: `'expiry'` writes a marker a push lifts (`RepoDeletion.by`). */
|
|
97
|
+
by?: 'expiry'
|
|
98
|
+
/**
|
|
99
|
+
* Re-asked against the Index the marker would be written on. The caller
|
|
100
|
+
* decided on an earlier read, and a push can land between that read and this
|
|
101
|
+
* write — marking a repository its owner just pushed to would delete work
|
|
102
|
+
* the push was acknowledged for. Absent means always due, which is an
|
|
103
|
+
* operator's `walgit delete`.
|
|
104
|
+
*/
|
|
105
|
+
stillDue?: (current: WalIndex) => boolean
|
|
90
106
|
}
|
|
91
107
|
|
|
92
108
|
/**
|
|
@@ -137,25 +153,35 @@ export async function deleteRepo(
|
|
|
137
153
|
const existing = index.deletion
|
|
138
154
|
if (!existing) {
|
|
139
155
|
const collectAfter = new Date(now.getTime() + graceMs).toISOString()
|
|
156
|
+
if (opts.stillDue && !opts.stillDue(index)) {
|
|
157
|
+
return superseded(repoId, keys, dryRun, 'pushed to since it was judged idle')
|
|
158
|
+
}
|
|
140
159
|
if (!dryRun) {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
160
|
+
let lifted = false
|
|
161
|
+
const committed = await updateIndex(store, repoId, (current): WalIndex => {
|
|
162
|
+
// Re-read inside the mutation: a delete that raced another one must
|
|
163
|
+
// keep the FIRST request's deadline, never restart the clock — and a
|
|
164
|
+
// push that raced this one must not be marked at all.
|
|
165
|
+
if (!current.deletion && opts.stillDue && !opts.stillDue(current)) {
|
|
166
|
+
lifted = true
|
|
167
|
+
return current
|
|
168
|
+
}
|
|
169
|
+
lifted = false
|
|
170
|
+
return {
|
|
145
171
|
...current,
|
|
146
|
-
// Re-read inside the mutation: a delete that raced another one must
|
|
147
|
-
// keep the FIRST request's deadline, never restart the clock.
|
|
148
172
|
deletion: current.deletion ?? {
|
|
149
173
|
requested_at: now.toISOString(),
|
|
150
174
|
collect_after: collectAfter,
|
|
175
|
+
...(opts.by ? { by: opts.by } : {}),
|
|
151
176
|
},
|
|
152
|
-
}
|
|
153
|
-
)
|
|
177
|
+
}
|
|
178
|
+
})
|
|
154
179
|
if (!committed.ok) {
|
|
155
180
|
throw new Error(
|
|
156
181
|
`walgit: delete for ${repoId} could not update the index; nothing was changed`,
|
|
157
182
|
)
|
|
158
183
|
}
|
|
184
|
+
if (lifted) return superseded(repoId, keys, false, 'pushed to since it was judged idle')
|
|
159
185
|
return {
|
|
160
186
|
repoId,
|
|
161
187
|
status: 'tombstoned',
|
|
@@ -206,6 +232,19 @@ export async function deleteRepo(
|
|
|
206
232
|
}
|
|
207
233
|
}
|
|
208
234
|
|
|
235
|
+
// Claim the collection under compare-and-swap on the Index this decision was
|
|
236
|
+
// read from. Losing it means something wrote in between — most likely a push,
|
|
237
|
+
// which may have lifted the mark — and the next sweep decides again. Winning
|
|
238
|
+
// it means every later push is refused (`RepoDeletion.collecting_at`).
|
|
239
|
+
const claimed = await commitIndex(
|
|
240
|
+
store,
|
|
241
|
+
{ ...index, deletion: { ...existing, collecting_at: now.toISOString() } },
|
|
242
|
+
etag,
|
|
243
|
+
)
|
|
244
|
+
if (!claimed.ok) {
|
|
245
|
+
return superseded(repoId, keys, false, 'the index moved under the collection')
|
|
246
|
+
}
|
|
247
|
+
|
|
209
248
|
// The record goes first. From here on the objects are unreferenced, so a
|
|
210
249
|
// crash at any point below leaves orphans the collector reclaims — never an
|
|
211
250
|
// index naming an object that is gone.
|
|
@@ -236,3 +275,13 @@ export async function deleteRepo(
|
|
|
236
275
|
dryRun,
|
|
237
276
|
}
|
|
238
277
|
}
|
|
278
|
+
|
|
279
|
+
function superseded(repoId: string, keys: string[], dryRun: boolean, why: string): DeleteResult {
|
|
280
|
+
return {
|
|
281
|
+
repoId,
|
|
282
|
+
status: 'superseded',
|
|
283
|
+
deleted: [],
|
|
284
|
+
retained: keys.map((key) => ({ key, reason: `${why} — nothing was changed` })),
|
|
285
|
+
dryRun,
|
|
286
|
+
}
|
|
287
|
+
}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The container's half of answering ref checks at the edge: saying, once per
|
|
3
|
+
* boot, what its own git advertises (`shared/edge-refs.ts`).
|
|
4
|
+
*
|
|
5
|
+
* The edge answers a protocol-v2 `info/refs` and an `ls-refs` without waking
|
|
6
|
+
* this process, and both answers carry bytes only git can author — the
|
|
7
|
+
* capability advertisement, and the headers `git http-backend` puts on each.
|
|
8
|
+
* Typing them out at the edge would be a promise about a git binary the edge
|
|
9
|
+
* cannot see: the image's git is Alpine's, it moves when the image is rebuilt,
|
|
10
|
+
* and an advertisement that outlived it would offer a client something the
|
|
11
|
+
* container then refuses. So the container asks its own git, through the same
|
|
12
|
+
* `runGitHttpBackend` every real request goes through, against a probe
|
|
13
|
+
* repository provisioned by the same `ensureBareRepo` every Cache is — and
|
|
14
|
+
* publishes the answers under the fingerprint of the environment it booted
|
|
15
|
+
* with. The edge reads only the fingerprint IT would boot a container with, so
|
|
16
|
+
* it serves this git's words exactly while this is the container behind it.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { spawnSync } from 'node:child_process'
|
|
20
|
+
import * as fs from 'node:fs'
|
|
21
|
+
import * as os from 'node:os'
|
|
22
|
+
import * as path from 'node:path'
|
|
23
|
+
|
|
24
|
+
import {
|
|
25
|
+
REQUEST_TYPE,
|
|
26
|
+
serializeUploadPackRecord,
|
|
27
|
+
uploadPackKeyFor,
|
|
28
|
+
type CapturedResponse,
|
|
29
|
+
type UploadPackRecord,
|
|
30
|
+
} from '../shared/edge-refs'
|
|
31
|
+
import { UPLOAD_PACK_PREFIX } from '../shared/keys'
|
|
32
|
+
import type { ObjectStore } from '../shared/store'
|
|
33
|
+
import { ensureBareRepo } from './cache'
|
|
34
|
+
import { runGitHttpBackend } from './git-backend'
|
|
35
|
+
import type { BackendRequest } from './http'
|
|
36
|
+
|
|
37
|
+
/** The probe's name. Never a repository: it lives in a temporary directory, not under the repos dir. */
|
|
38
|
+
const PROBE = 'walgit-probe'
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Ask this image's git what it advertises, the way a client would.
|
|
42
|
+
*
|
|
43
|
+
* Two requests through `runGitHttpBackend` — the v2 `info/refs`, and an
|
|
44
|
+
* `ls-refs` against the probe, which holds no refs and so answers a bare flush
|
|
45
|
+
* whose status and headers are the ones every `ls-refs` gets. The probe is
|
|
46
|
+
* created in `probeParent` and removed afterwards; it is never a repository
|
|
47
|
+
* anyone can name.
|
|
48
|
+
*/
|
|
49
|
+
export async function captureUploadPack(
|
|
50
|
+
probeParent: string = os.tmpdir(),
|
|
51
|
+
runBackend: (req: BackendRequest) => Promise<Response> = runGitHttpBackend,
|
|
52
|
+
): Promise<UploadPackRecord> {
|
|
53
|
+
const root = fs.mkdtempSync(path.join(probeParent, 'walgit-probe-'))
|
|
54
|
+
try {
|
|
55
|
+
const repo = ensureBareRepo({ repoId: PROBE, dir: path.join(root, `${PROBE}.git`) })
|
|
56
|
+
const origin = 'http://walgit.internal'
|
|
57
|
+
const advertisement = await capture(
|
|
58
|
+
runBackend({
|
|
59
|
+
repo,
|
|
60
|
+
pathInfo: `/${PROBE}.git/info/refs`,
|
|
61
|
+
request: new Request(`${origin}/${PROBE}.git/info/refs?service=git-upload-pack`, {
|
|
62
|
+
headers: { 'git-protocol': 'version=2' },
|
|
63
|
+
}),
|
|
64
|
+
}),
|
|
65
|
+
)
|
|
66
|
+
const lsRefs = await capture(
|
|
67
|
+
runBackend({
|
|
68
|
+
repo,
|
|
69
|
+
pathInfo: `/${PROBE}.git/git-upload-pack`,
|
|
70
|
+
request: new Request(`${origin}/${PROBE}.git/git-upload-pack`, {
|
|
71
|
+
method: 'POST',
|
|
72
|
+
headers: { 'git-protocol': 'version=2', 'content-type': REQUEST_TYPE },
|
|
73
|
+
body: '0014command=ls-refs\n00010000',
|
|
74
|
+
}),
|
|
75
|
+
}),
|
|
76
|
+
)
|
|
77
|
+
const version = spawnSync('git', ['--version'], { encoding: 'utf8' })
|
|
78
|
+
return { version: 1, git: version.stdout.trim(), advertisement, lsRefs }
|
|
79
|
+
} finally {
|
|
80
|
+
fs.rmSync(root, { recursive: true, force: true })
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async function capture(pending: Promise<Response>): Promise<CapturedResponse> {
|
|
85
|
+
const response = await pending
|
|
86
|
+
return {
|
|
87
|
+
status: response.status,
|
|
88
|
+
headers: [...response.headers],
|
|
89
|
+
body: await response.text(),
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Capture and publish this container's record, and collect the ones earlier
|
|
95
|
+
* environments left behind. Returns the key written, or `null` when this
|
|
96
|
+
* environment gives the edge no way to tell images apart (`uploadPackKeyFor`)
|
|
97
|
+
* and nothing is published.
|
|
98
|
+
*
|
|
99
|
+
* The older records are deleted because nothing will read them again: the
|
|
100
|
+
* Worker computes one fingerprint, and it is this container's. A Worker
|
|
101
|
+
* version still mid-rollout that reads a deleted one falls through, which is
|
|
102
|
+
* what it would do anyway. The collection is best effort for the same reason.
|
|
103
|
+
*/
|
|
104
|
+
export async function publishUploadPack(
|
|
105
|
+
store: ObjectStore,
|
|
106
|
+
env: Record<string, string | undefined> = process.env,
|
|
107
|
+
probeParent?: string,
|
|
108
|
+
): Promise<string | null> {
|
|
109
|
+
const key = uploadPackKeyFor(env)
|
|
110
|
+
if (key === null) return null
|
|
111
|
+
const record = await captureUploadPack(probeParent)
|
|
112
|
+
const written = await store.put(key, serializeUploadPackRecord(record))
|
|
113
|
+
if (!written.ok) throw new Error(`walgit: could not publish ${key}`)
|
|
114
|
+
try {
|
|
115
|
+
for (const old of await store.list(UPLOAD_PACK_PREFIX)) {
|
|
116
|
+
if (old !== key) await store.delete(old)
|
|
117
|
+
}
|
|
118
|
+
} catch {
|
|
119
|
+
// An extra object of a few hundred bytes, read by nobody.
|
|
120
|
+
}
|
|
121
|
+
return key
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* `publishUploadPack` at boot, retried, and never fatal.
|
|
126
|
+
*
|
|
127
|
+
* Off the request path: the request that woke this container is already being
|
|
128
|
+
* served the ordinary way, and until the record lands every ref check simply
|
|
129
|
+
* keeps coming here, which is exactly what happened before the edge answered
|
|
130
|
+
* any. Retried because a boot is when the store is likeliest to be slow, and a
|
|
131
|
+
* record that never lands costs a whole container lifetime of wake-ups.
|
|
132
|
+
*/
|
|
133
|
+
export async function publishUploadPackAtBoot(store: ObjectStore): Promise<void> {
|
|
134
|
+
for (const delayMs of [0, 2_000, 10_000, 60_000]) {
|
|
135
|
+
if (delayMs > 0) await new Promise((resolve) => setTimeout(resolve, delayMs))
|
|
136
|
+
try {
|
|
137
|
+
const key = await publishUploadPack(store)
|
|
138
|
+
console.log(
|
|
139
|
+
key === null
|
|
140
|
+
? 'walgit edge-refs: no WALGIT_BUILD_ID, so the edge answers no ref checks'
|
|
141
|
+
: `walgit edge-refs: published ${key}`,
|
|
142
|
+
)
|
|
143
|
+
return
|
|
144
|
+
} catch (err) {
|
|
145
|
+
console.error(`walgit edge-refs: publish failed: ${(err as Error).message}`)
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|