rvlib-shipkit 2.11.0 → 2.13.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 +56 -12
- package/README.md +1 -1
- package/package.json +4 -2
- package/src/agent-time/store.ts +2 -2
- package/src/check.ts +1 -1
- package/src/checks/SK001-package-json-shape.check.ts +4 -0
- package/src/checks/SK003-claude-md-shape.check.ts +1 -1
- package/src/checks/SK004-gitignore-policy.check.ts +27 -2
- package/src/checks/SK005-rvlib-versions.check.ts +1 -1
- package/src/checks/SK008-reviewer-agent.check.ts +2 -2
- package/src/checks/SK012-config-barrel-import.check.ts +1 -1
- package/src/checks/SK015-nested-repo.check.ts +1 -1
- package/src/checks/SK035-public-clean.check.ts +46 -0
- package/src/checks/SK036-draft-media-size.check.ts +28 -0
- package/src/cli/cmd_Backup.ts +152 -0
- package/src/cli/cmd_Ci.ts +8 -11
- package/src/cli/cmd_Drill.ts +145 -0
- package/src/cli/cmd_Home.ts +50 -3
- package/src/cli/cmd_Media.ts +96 -5
- package/src/cli/cmd_Review.ts +8 -2
- package/src/cli/cmd_Shot.ts +5 -2
- package/src/cli/cmd_Smoke.ts +4 -1
- package/src/cli/cmd_Social.ts +226 -0
- package/src/cli/cmd_Start.ts +1 -1
- package/src/cli/cmd_Time.ts +1 -1
- package/src/cli/cmd_Trailer.ts +9 -2
- package/src/cli/cmd_Veille.ts +173 -0
- package/src/cli/family-pages.ts +3 -0
- package/src/cli/registry.ts +13 -2
- package/src/cli.ts +5 -2
- package/src/collections/actions.ts +1 -49
- package/src/collections/checks.ts +2 -1
- package/src/collections/collection-types.ts +11 -2
- package/src/collections/providers.ts +4 -4
- package/src/collections/review.ts +2 -1
- package/src/collections/tests.ts +19 -8
- package/src/contracts/apply-contracts.ts +1 -3
- package/src/contracts/contract-manifests.ts +2 -3
- package/src/contracts/contract-types.ts +0 -1
- package/src/contracts/contracts-engine.ts +0 -3
- package/src/core/app-icon.ts +9 -3
- package/src/core/bun-builtins.ts +2 -2
- package/src/core/discover-checks.ts +37 -5
- package/src/core/fix-scope.ts +4 -2
- package/src/core/floor-resolve.ts +68 -48
- package/src/core/floor.ts +9 -0
- package/src/core/git-root.ts +24 -0
- package/src/core/last-good.ts +3 -1
- package/src/core/layout.ts +8 -0
- package/src/core/log-stamp.ts +36 -0
- package/src/core/loop-watch.ts +93 -0
- package/src/core/machine-link.ts +134 -0
- package/src/core/memory-watch.ts +41 -0
- package/src/core/one-way-in-policy.ts +27 -19
- package/src/core/run-ci.ts +6 -8
- package/src/core/run-fixers.ts +9 -9
- package/src/core/slots.ts +21 -0
- package/src/core/tree-rev.ts +60 -0
- package/src/core/unit-files.ts +42 -0
- package/src/drill/render.ts +140 -0
- package/src/drill/run.ts +269 -0
- package/src/fleet/scan-fleet.ts +6 -5
- package/src/home/daemon-jobs.ts +201 -0
- package/src/home/global-context.ts +34 -1
- package/src/home/home-routes.ts +22 -15
- package/src/home/hubs.ts +10 -3
- package/src/home/inbox-shape.ts +61 -6
- package/src/home/inbox.ts +75 -4
- package/src/home/lib-watch.ts +10 -3
- package/src/home/machine-stores.ts +15 -29
- package/src/home/run-daemon.ts +15 -18
- package/src/home/run-pm.ts +2 -0
- package/src/home/serve-daemon.ts +4 -3
- package/src/home/serve-web.ts +110 -1
- package/src/hooks/commit-message.ts +28 -19
- package/src/hooks/leak-guard.ts +10 -3
- package/src/hooks/run-hooks.ts +2 -6
- package/src/identities/identities.ts +334 -0
- package/src/identities/machine-route.ts +126 -0
- package/src/identities/machine.ts +62 -0
- package/src/init-repo.ts +1 -1
- package/src/ledger/check-stats.ts +10 -9
- package/src/ledger/gate-checks.ts +32 -1
- package/src/ledger/record-kept.ts +112 -0
- package/src/ledger/repo-view.ts +10 -0
- package/src/ledger/run-log.ts +17 -12
- package/src/ledger/run-timeline.ts +8 -4
- package/src/ledger/store.ts +7 -0
- package/src/ledger/suite-log.ts +17 -13
- package/src/media-kinds.ts +9 -1
- package/src/media-store/backup/daemon.ts +73 -0
- package/src/media-store/backup/keys.ts +25 -0
- package/src/media-store/backup/manifest.ts +193 -0
- package/src/media-store/backup/restore.ts +192 -0
- package/src/media-store/backup/run.ts +278 -0
- package/src/media-store/backup/secret.ts +86 -0
- package/src/media-store/backup/snapshot.ts +64 -0
- package/src/media-store/backup/sources.ts +199 -0
- package/src/media-store/content.ts +11 -1
- package/src/media-store/daemon.ts +56 -70
- package/src/media-store/hand.ts +58 -0
- package/src/media-store/put.ts +122 -0
- package/src/media-store/space.ts +73 -11
- package/src/pages/forward.ts +35 -0
- package/src/pages/frame-client.ts +118 -0
- package/src/pages/routes.ts +29 -0
- package/src/pages/unit.ts +92 -0
- package/src/plan/planner.ts +12 -0
- package/src/plan/repo-source.ts +19 -5
- package/src/reflect/transcript.ts +35 -10
- package/src/release/public-clean.ts +301 -0
- package/src/release/published-here.ts +22 -0
- package/src/release/run-bump.ts +2 -1
- package/src/release/run-release.ts +4 -1
- package/src/repo-config.ts +53 -2
- package/src/review/http.ts +16 -2
- package/src/review/media-kind.ts +3 -3
- package/src/review/media-read.ts +3 -3
- package/src/review/model.ts +24 -5
- package/src/review/notes.ts +77 -0
- package/src/review/project.ts +6 -38
- package/src/review/routes.ts +8 -13
- package/src/review/store.ts +46 -4
- package/src/review/trailer-media.ts +24 -3
- package/src/review/trailer-shots.ts +106 -0
- package/src/review/unit.ts +1 -1
- package/src/scaffold/files/agent-handoff.md +3 -0
- package/src/scaffold/files/agent-index.md +4 -12
- package/src/scaffold/project-scaffold.ts +5 -4
- package/src/serve/embed.ts +1 -1
- package/src/serve/hub-app/col-card.tsx +3 -17
- package/src/serve/hub-app/col-tables-view.tsx +2 -1
- package/src/serve/hub-app/cols-svc.ts +12 -14
- package/src/serve/hub-app/dock-st.ts +4 -2
- package/src/serve/hub-app/dock-view.tsx +2 -0
- package/src/serve/hub-app/frames-st.ts +35 -0
- package/src/serve/hub-app/hash-route.ts +64 -1
- package/src/serve/hub-app/home-st.ts +31 -7
- package/src/serve/hub-app/home-strip.css +9 -1
- package/src/serve/hub-app/home-strip.tsx +7 -6
- package/src/serve/hub-app/home.html +5 -0
- package/src/serve/hub-app/hub-app.tsx +49 -5
- package/src/serve/hub-app/hub.css +29 -11
- package/src/serve/hub-app/icons.tsx +16 -1
- package/src/serve/hub-app/identities-st.ts +210 -0
- package/src/serve/hub-app/identities-view.tsx +352 -0
- package/src/serve/hub-app/identities.css +48 -0
- package/src/serve/hub-app/identity-address.ts +52 -0
- package/src/serve/hub-app/inbox-st.ts +59 -6
- package/src/serve/hub-app/inbox-view.tsx +48 -6
- package/src/serve/hub-app/inbox.css +12 -1
- package/src/serve/hub-app/index.html +3 -0
- package/src/serve/hub-app/keys.ts +10 -2
- package/src/serve/hub-app/list-walk.ts +89 -0
- package/src/serve/hub-app/machine-st.ts +12 -0
- package/src/serve/hub-app/media-view.tsx +2 -1
- package/src/serve/hub-app/pages-frames.tsx +134 -0
- package/src/serve/hub-app/pages-svc.ts +111 -0
- package/src/serve/hub-app/palette-st.ts +18 -3
- package/src/serve/hub-app/palette-view.tsx +9 -2
- package/src/serve/hub-app/planned-view.tsx +2 -1
- package/src/serve/hub-app/project-list.css +18 -14
- package/src/serve/hub-app/project-list.tsx +88 -49
- package/src/serve/hub-app/projects-svc.ts +33 -3
- package/src/serve/hub-app/repo-sections.ts +19 -2
- package/src/serve/hub-app/repo-st.ts +75 -19
- package/src/serve/hub-app/review/review-cells.tsx +3 -3
- package/src/serve/hub-app/review/review-kit.ts +30 -7
- package/src/serve/hub-app/review/review-page.tsx +1 -1
- package/src/serve/hub-app/review/review-st.ts +33 -11
- package/src/serve/hub-app/review/review-thread.tsx +11 -9
- package/src/serve/hub-app/review/review-views.tsx +84 -19
- package/src/serve/hub-app/review/thread-st.ts +1 -10
- package/src/serve/hub-app/review/trailer-page.tsx +2 -1
- package/src/serve/hub-app/review.css +17 -8
- package/src/serve/hub-app/site/site-part.tsx +173 -0
- package/src/serve/hub-app/site/site-st.ts +157 -0
- package/src/serve/hub-app/site.css +35 -0
- package/src/serve/hub-app/social/calendar.tsx +182 -0
- package/src/serve/hub-app/social/comms-page.tsx +306 -0
- package/src/serve/hub-app/social/kit.tsx +244 -0
- package/src/serve/hub-app/social/post-card.tsx +456 -0
- package/src/serve/hub-app/social/problems.tsx +60 -0
- package/src/serve/hub-app/social/social-model.ts +154 -0
- package/src/serve/hub-app/social/social-page.tsx +674 -0
- package/src/serve/hub-app/social/social-st.ts +986 -0
- package/src/serve/hub-app/social.css +289 -0
- package/src/serve/hub-app/stores/stores-page.tsx +311 -0
- package/src/serve/hub-app/stores/stores-st.ts +286 -0
- package/src/serve/hub-app/stores.css +54 -0
- package/src/serve/hub-app/test-view.tsx +9 -2
- package/src/serve/hub-app/tests-svc.ts +16 -1
- package/src/serve/hub-app/timeline-geometry.ts +1 -1
- package/src/serve/hub-app/timeline-view.tsx +3 -2
- package/src/serve/hub-app/tree-build.ts +6 -0
- package/src/serve/hub-app/tree-row.tsx +27 -4
- package/src/serve/hub-app/tree-svc.ts +18 -49
- package/src/serve/hub-app/veille/veille-page.tsx +514 -0
- package/src/serve/hub-app/veille/veille-st.ts +443 -0
- package/src/serve/hub-app/veille.css +84 -0
- package/src/serve/hub-runtime.ts +14 -9
- package/src/serve/project-gutter-route.ts +44 -12
- package/src/serve/resources-wire.ts +48 -0
- package/src/serve/run-lanes.ts +7 -4
- package/src/serve/same-origin.ts +1 -1
- package/src/serve/serve-hub.ts +27 -18
- package/src/serve/tracked-watch.ts +24 -3
- package/src/serve/warm-cell.ts +5 -1
- package/src/shipkit-files.ts +15 -4
- package/src/shipkit.ts +0 -1
- package/src/shot/box.ts +162 -0
- package/src/shot/browser-slot.ts +62 -0
- package/src/shot/client.ts +43 -7
- package/src/shot/clip-encoder.ts +93 -12
- package/src/shot/film-box.ts +188 -0
- package/src/shot/shooter.ts +50 -15
- package/src/shot/shot-ask.ts +10 -2
- package/src/shot/stepped-clip.ts +5 -4
- package/src/site/desk.ts +242 -0
- package/src/site/routes.ts +66 -0
- package/src/site/unit.ts +62 -0
- package/src/social/brands.ts +22 -0
- package/src/social/compose.ts +97 -0
- package/src/social/desk-index.ts +123 -0
- package/src/social/desk-wire.ts +64 -0
- package/src/social/drafts.ts +183 -0
- package/src/social/house.ts +24 -0
- package/src/social/load.ts +319 -0
- package/src/social/md.ts +70 -0
- package/src/social/media-facts.ts +53 -0
- package/src/social/media-kind.ts +189 -0
- package/src/social/media-limit.ts +42 -0
- package/src/social/media-of.ts +30 -0
- package/src/social/migrate.ts +123 -0
- package/src/social/open.ts +17 -0
- package/src/social/post-url.ts +158 -0
- package/src/social/presence.ts +332 -0
- package/src/social/public-read.ts +53 -0
- package/src/social/publish/bluesky.ts +168 -0
- package/src/social/publish/discord.ts +76 -0
- package/src/social/publish/index.ts +24 -0
- package/src/social/publish/secret.ts +13 -0
- package/src/social/push.ts +31 -0
- package/src/social/richtext.ts +37 -0
- package/src/social/routes.ts +180 -0
- package/src/social/stats.ts +134 -0
- package/src/social/text.ts +267 -0
- package/src/social/tracker-route.ts +39 -0
- package/src/social/tracker.ts +255 -0
- package/src/social/unit.ts +53 -0
- package/src/social/writes.ts +130 -0
- package/src/start-repo.ts +14 -9
- package/src/stores/collection.ts +27 -0
- package/src/stores/desk.ts +336 -0
- package/src/stores/model.ts +187 -0
- package/src/stores/routes.ts +126 -0
- package/src/stores/unit.ts +57 -0
- package/src/suite/calibrate.ts +127 -0
- package/src/suite/core-counters.ts +91 -0
- package/src/suite/judge-suites.ts +94 -34
- package/src/suite/suite-cli.ts +44 -10
- package/src/suite/suite-cpu-preload.ts +34 -9
- package/src/trailer/adapter.ts +4 -0
- package/src/trailer/build.ts +42 -11
- package/src/trailer/ffmpeg.ts +11 -0
- package/src/trailer/footage.ts +30 -13
- package/src/trailer/grammar.ts +18 -2
- package/src/trailer/graph.ts +28 -6
- package/src/trailer/passage.ts +21 -6
- package/src/trailer/plan.ts +74 -3
- package/src/trailer/shelf.ts +16 -0
- package/src/trailer/stitch.ts +67 -26
- package/src/trailer/url.ts +1 -1
- package/src/trailer/versions.ts +24 -0
- package/src/ts/gate-typecheck.ts +32 -0
- package/src/ts/typecheck-cli.ts +9 -11
- package/src/tui/tui-app.tsx +0 -1
- package/src/veille/asks.ts +155 -0
- package/src/veille/auto.ts +31 -0
- package/src/veille/batch.ts +27 -0
- package/src/veille/chrome.ts +127 -0
- package/src/veille/collection.ts +28 -0
- package/src/veille/desk.ts +242 -0
- package/src/veille/identity.ts +80 -0
- package/src/veille/import.ts +68 -0
- package/src/veille/log.ts +183 -0
- package/src/veille/platforms.ts +118 -0
- package/src/veille/reads.ts +66 -0
- package/src/veille/replied.ts +110 -0
- package/src/veille/routes.ts +185 -0
- package/src/veille/service.ts +81 -0
- package/src/veille/watch.ts +116 -0
- package/src/veille/worth.ts +46 -0
- package/src/wiki/scan-md.ts +18 -0
- package/src/collections/posts.ts +0 -142
- package/src/contracts/manifests/social.contract.ts +0 -20
- package/src/scaffold/files/agent-coding.md +0 -35
- package/src/ts/typecheck-cache.ts +0 -11
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
// the stores desk's pure model (wiki build/shipkit/stores.md): a store page's markdown cut into its `## ` sections, one
|
|
2
|
+
// section written back with every other byte kept, the value he pastes out of a section, the account's steps out of
|
|
3
|
+
// its ask, the contract's lines. No fs, no fetch: desk.ts reads the files, the hub page imports this too
|
|
4
|
+
|
|
5
|
+
export const STORE_IDS = ['steam', 'itch'] as const
|
|
6
|
+
export type StoreId = (typeof STORE_IDS)[number]
|
|
7
|
+
export const isStore = (s: unknown): s is StoreId => (STORE_IDS as readonly unknown[]).includes(s)
|
|
8
|
+
|
|
9
|
+
/** a `## ` section: its heading, its body as written (blank lines round it dropped) */
|
|
10
|
+
export interface MdSection {
|
|
11
|
+
heading: string
|
|
12
|
+
body: string
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** where each section's body sits in the file: from the end of its heading line to the next heading */
|
|
16
|
+
function spans(md: string): { heading: string; at: number; from: number; to: number }[] {
|
|
17
|
+
const out: { heading: string; at: number; from: number; to: number }[] = []
|
|
18
|
+
const re = /^## (.+)$/gm
|
|
19
|
+
for (let m = re.exec(md); m; m = re.exec(md)) {
|
|
20
|
+
const last = out.at(-1)
|
|
21
|
+
if (last) last.to = m.index
|
|
22
|
+
out.push({ heading: m[1]!.trim(), at: m.index, from: m.index + m[0].length, to: md.length })
|
|
23
|
+
}
|
|
24
|
+
return out
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** the text before the first section (the title and its status line), and every section in the file's order */
|
|
28
|
+
export function sectionsOf(md: string): { preamble: string; sections: MdSection[] } {
|
|
29
|
+
const all = spans(md)
|
|
30
|
+
const first = all[0]
|
|
31
|
+
const preamble = (first ? md.slice(0, first.at) : md).replace(/^---\n[\s\S]*?\n---\n/, '').trim()
|
|
32
|
+
return { preamble, sections: all.map((s) => ({ heading: s.heading, body: md.slice(s.from, s.to).trim() })) }
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** the file with one section's body replaced: the blank lines round it and every other byte as they were */
|
|
36
|
+
export function setSection(md: string, heading: string, body: string): string {
|
|
37
|
+
const s = spans(md).find((x) => x.heading === heading)
|
|
38
|
+
if (!s) throw new Error(`no section « ## ${heading} »`)
|
|
39
|
+
const old = md.slice(s.from, s.to)
|
|
40
|
+
const text = body.trim()
|
|
41
|
+
if (!text) throw new Error(`« ## ${heading} »: the text is empty`)
|
|
42
|
+
const lead = old.trim() ? (/^\s*/.exec(old)?.[0] ?? '\n\n') : '\n\n'
|
|
43
|
+
const trail = old.trim() ? (/\s*$/.exec(old)?.[0] ?? '\n\n') : s.to === md.length ? '\n' : '\n\n'
|
|
44
|
+
return md.slice(0, s.from) + lead + text + trail + md.slice(s.to)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const paragraphs = (body: string): string[] =>
|
|
48
|
+
body
|
|
49
|
+
.split(/\n\s*\n/)
|
|
50
|
+
.map((p) => p.trim())
|
|
51
|
+
.filter(Boolean)
|
|
52
|
+
|
|
53
|
+
/** what part of a section he pastes: all of it, its first paragraph, its paragraph of names split by commas, or the
|
|
54
|
+
* `word` it opens on (a value to pick in a form's list) */
|
|
55
|
+
export type Pick = 'all' | 'first' | 'names' | 'code'
|
|
56
|
+
|
|
57
|
+
/** the value he pastes and the notes round it */
|
|
58
|
+
export function valueOf(body: string, pick: Pick): { value: string; notes: string } {
|
|
59
|
+
if (pick === 'all') return { value: body, notes: '' }
|
|
60
|
+
const code = pick === 'code' ? /^`([^`]+)`[.:,]?\s*/.exec(body) : null
|
|
61
|
+
if (code) return { value: code[1]!, notes: body.slice(code[0].length).trim() }
|
|
62
|
+
if (pick === 'code') return { value: body, notes: '' }
|
|
63
|
+
const ps = paragraphs(body)
|
|
64
|
+
const commas = (p: string): number => p.split(',').length
|
|
65
|
+
const at = pick === 'first' ? 0 : ps.reduce((best, p, i) => (commas(p) > commas(ps[best] ?? '') ? i : best), 0)
|
|
66
|
+
return { value: ps[at] ?? '', notes: ps.filter((_, i) => i !== at).join('\n\n') }
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** the names of a paragraph of names, its trailing period dropped */
|
|
70
|
+
export const namesOf = (value: string): string[] =>
|
|
71
|
+
value
|
|
72
|
+
.replace(/\.\s*$/, '')
|
|
73
|
+
.split(',')
|
|
74
|
+
.map((x) => x.trim())
|
|
75
|
+
.filter(Boolean)
|
|
76
|
+
|
|
77
|
+
/** the markdown Steam's description box takes as its own formatting: bold as [b], a `- ` run as a [list] */
|
|
78
|
+
export function toSteamMarkup(md: string): string {
|
|
79
|
+
const out: string[] = []
|
|
80
|
+
let list = false
|
|
81
|
+
for (const line of md.split('\n')) {
|
|
82
|
+
const item = /^\s*[-*] (.*)$/.exec(line)
|
|
83
|
+
if (item && !list) out.push('[list]')
|
|
84
|
+
if (!item && list) out.push('[/list]')
|
|
85
|
+
list = !!item
|
|
86
|
+
out.push(item ? `[*]${item[1]}` : line)
|
|
87
|
+
}
|
|
88
|
+
if (list) out.push('[/list]')
|
|
89
|
+
return out.join('\n').replace(/\*\*(.+?)\*\*/g, '[b]$1[/b]')
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** a field of a store page the desk shows: the section it reads, what he pastes of it, the limit the store sets */
|
|
93
|
+
export interface FieldSpec {
|
|
94
|
+
heading: string
|
|
95
|
+
pick: Pick
|
|
96
|
+
/** the store's limit: characters, or names for a paragraph of names */
|
|
97
|
+
max?: number
|
|
98
|
+
/** « Copy » gives Steam's own markup */
|
|
99
|
+
steamMarkup?: boolean
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export const STEAM_DIR = '.shipkit/steam'
|
|
103
|
+
export const ITCH_DIR = '.shipkit/itch'
|
|
104
|
+
|
|
105
|
+
/** a store's own text files, in the desk's order, and the sections that need more than « all of it » */
|
|
106
|
+
export const STORE_FILES: Record<StoreId, { file: string; fields: readonly FieldSpec[] }[]> = {
|
|
107
|
+
steam: [
|
|
108
|
+
{
|
|
109
|
+
file: `${STEAM_DIR}/store.md`,
|
|
110
|
+
fields: [
|
|
111
|
+
{ heading: 'Short description', pick: 'first', max: 300 },
|
|
112
|
+
{ heading: 'About this game', pick: 'all', steamMarkup: true },
|
|
113
|
+
{ heading: 'Tags', pick: 'names', max: 20 },
|
|
114
|
+
],
|
|
115
|
+
},
|
|
116
|
+
],
|
|
117
|
+
itch: [
|
|
118
|
+
{
|
|
119
|
+
file: `${ITCH_DIR}/listing.md`,
|
|
120
|
+
fields: [
|
|
121
|
+
{ heading: 'Tags (max 10)', pick: 'names', max: 10 },
|
|
122
|
+
...['Classification', 'Kind of project', 'Release status', 'Pricing', 'Genre', 'Community', 'Visibility & access'].map((heading) => ({ heading, pick: 'code' as const })),
|
|
123
|
+
],
|
|
124
|
+
},
|
|
125
|
+
{ file: `${ITCH_DIR}/page.md`, fields: [] },
|
|
126
|
+
],
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export const STORE_NAME: Record<StoreId, string> = { steam: 'Steam', itch: 'itch.io' }
|
|
130
|
+
|
|
131
|
+
/** the sections about pictures: shown with their pictures on the right, never as a field to paste */
|
|
132
|
+
export const PICTURE_SECTIONS: ReadonlySet<string> = new Set(['Screenshots', 'Capsules', 'Cover image', 'Trailer', 'Gameplay video or trailer'])
|
|
133
|
+
|
|
134
|
+
/** a capsule row of store.md's `## Capsules` table: its file, the size it must have (none for an icon), its slot */
|
|
135
|
+
export function capsuleRows(body: string): { file: string; w: number | null; h: number | null; slot: string }[] {
|
|
136
|
+
const out: { file: string; w: number | null; h: number | null; slot: string }[] = []
|
|
137
|
+
for (const m of body.matchAll(/^\| `([^`]+)`(?: (\d+)x(\d+))? \| (.+?) \|$/gm))
|
|
138
|
+
out.push({ file: m[1]!, w: m[2] ? Number(m[2]) : null, h: m[3] ? Number(m[3]) : null, slot: m[4]!.trim() })
|
|
139
|
+
return out
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** the repo paths a section names in backticks with one of the extensions */
|
|
143
|
+
export const pathsIn = (body: string, ext: RegExp): string[] => [...body.matchAll(/`([^`\s]+)`/g)].map((m) => m[1]!).filter((p) => ext.test(p))
|
|
144
|
+
|
|
145
|
+
/** where a step or a fact stands: done, waiting on someone, not started */
|
|
146
|
+
export type Tone = 'done' | 'waiting' | 'todo'
|
|
147
|
+
|
|
148
|
+
/** the numbered steps of an ask (`## 6. Pay the Steam Direct fee ✅`), each with its mark, and its answer line */
|
|
149
|
+
export function askSteps(md: string): { steps: { n: number; title: string; tone: Tone }[]; answer: string | null; status: string | null } {
|
|
150
|
+
const steps = [...md.matchAll(/^## (\d+)\. (.+?)\s*$/gm)].map((m) => {
|
|
151
|
+
const title = m[2]!
|
|
152
|
+
const tone: Tone = title.includes('✅') ? 'done' : title.includes('🚧') ? 'waiting' : 'todo'
|
|
153
|
+
return { n: Number(m[1]), title: title.replace(/\s*[✅🚧]\s*/gu, ' ').replace(/\s+\(.*\)\s*$/, '').trim(), tone }
|
|
154
|
+
})
|
|
155
|
+
const raw = /^\*\*Answer\*\*:\s*(.*)$/m.exec(md)?.[1]?.trim() ?? ''
|
|
156
|
+
const answer = raw && !/^_\(.*\)_$/.test(raw) ? raw : null
|
|
157
|
+
return { steps, answer, status: /^status:\s*(\S+)/m.exec(md)?.[1] ?? null }
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** one line `sk contracts --venue <store>` prints, and its fix */
|
|
161
|
+
export interface ContractLine {
|
|
162
|
+
level: 'fail' | 'warn' | 'skip' | 'ok'
|
|
163
|
+
text: string
|
|
164
|
+
fix?: string
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export function contractLines(out: string): ContractLine[] {
|
|
168
|
+
const lines: ContractLine[] = []
|
|
169
|
+
for (const raw of out.split('\n')) {
|
|
170
|
+
const t = raw.trim()
|
|
171
|
+
const fix = /^fix:\s*(.*)$/.exec(t)
|
|
172
|
+
const last = lines.at(-1)
|
|
173
|
+
if (fix && last) last.fix = fix[1]
|
|
174
|
+
const m = /^(FAIL|WARN|ok|skip):?\s+(.*)$/i.exec(t)
|
|
175
|
+
if (m) lines.push({ level: m[1]!.toLowerCase() as ContractLine['level'], text: m[2]! })
|
|
176
|
+
}
|
|
177
|
+
return lines
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export type PageState = 'online' | 'draft' | 'to fill'
|
|
181
|
+
|
|
182
|
+
/** the page's state in one word: online once its URL is known, else a draft while its text says so */
|
|
183
|
+
export const pageState = (preamble: string, url: string | null): PageState =>
|
|
184
|
+
url ? 'online' : /Status:\s*DRAFT|\(paste-ready draft\)/i.test(preamble) ? 'draft' : 'to fill'
|
|
185
|
+
|
|
186
|
+
/** the asset a note names: a picture's repo path, or `field:<heading>` */
|
|
187
|
+
export const fieldAsset = (heading: string): string => `field:${heading}`
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
// the stores desk's routes (wiki build/shipkit/stores.md, Routes): the desk read whole, a picture served, a section
|
|
2
|
+
// written back alone and committed on its own path, a family made again by the unit's command with its log streamed,
|
|
3
|
+
// his note on one thing raised in the review store, a picture shown in the Finder
|
|
4
|
+
import { readdirSync, readFileSync, statSync, writeFileSync } from 'node:fs'
|
|
5
|
+
import { join } from 'node:path'
|
|
6
|
+
import { machineCommit } from '../core/machine-commit.ts'
|
|
7
|
+
import { bad, NO_STORE, ranged } from '../review/http.ts'
|
|
8
|
+
import { noteOn } from '../review/notes.ts'
|
|
9
|
+
import { openReview } from '../review/project.ts'
|
|
10
|
+
import { FILE_ROUTE, loadStoresUnit, shownFile, storesState, textFiles } from './desk.ts'
|
|
11
|
+
import { fieldAsset, isStore, setSection, STEAM_DIR, STORE_NAME, type StoreId } from './model.ts'
|
|
12
|
+
import { RENDER_FAMILIES, type RenderFamily } from './unit.ts'
|
|
13
|
+
|
|
14
|
+
/** one make again at a time, per repo */
|
|
15
|
+
const rendering = new Map<string, RenderFamily>()
|
|
16
|
+
|
|
17
|
+
async function writeText(root: string, b: Record<string, unknown>): Promise<Response> {
|
|
18
|
+
if (!isStore(b.store)) return bad(`text: no store ${String(b.store)}`)
|
|
19
|
+
const store: StoreId = b.store
|
|
20
|
+
const f = textFiles(await loadStoresUnit(root), store).find((x) => x.file === b.file)
|
|
21
|
+
if (!f) return bad(`text: ${String(b.file)} is not a text file of ${store}`)
|
|
22
|
+
const heading = String(b.heading ?? '')
|
|
23
|
+
const abs = join(root, f.file)
|
|
24
|
+
writeFileSync(abs, setSection(readFileSync(abs, 'utf8'), heading, String(b.body ?? '')))
|
|
25
|
+
const committed = machineCommit({ anchor: root, message: `store(${store}): the "${heading}" of the ${STORE_NAME[store]} page`, paths: [abs] })
|
|
26
|
+
return Response.json({ ok: true, committed }, { headers: NO_STORE })
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** the unit's command for a family on his press, its output streamed; the files it rewrote committed */
|
|
30
|
+
async function render(root: string, family: unknown): Promise<Response> {
|
|
31
|
+
if (!(RENDER_FAMILIES as readonly unknown[]).includes(family)) return bad(`render: ${RENDER_FAMILIES.join(' or ')}, not ${String(family)}`)
|
|
32
|
+
const fam = family as RenderFamily
|
|
33
|
+
const r = (await loadStoresUnit(root))?.render?.[fam]
|
|
34
|
+
if (!r) return bad(`render: the repo's stores unit makes no ${fam}`)
|
|
35
|
+
const now = rendering.get(root)
|
|
36
|
+
if (now) return bad(`the ${now} are being made already`, 409)
|
|
37
|
+
rendering.set(root, fam)
|
|
38
|
+
const rel = `${STEAM_DIR}/${fam === 'capsules' ? 'capsules' : 'screenshots'}`
|
|
39
|
+
const dir = join(root, rel)
|
|
40
|
+
const times = (): Map<string, number> => {
|
|
41
|
+
try {
|
|
42
|
+
return new Map(readdirSync(dir).map((f) => [f, statSync(join(dir, f)).mtimeMs]))
|
|
43
|
+
} catch {
|
|
44
|
+
return new Map()
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
const before = times()
|
|
48
|
+
let proc: ReturnType<typeof Bun.spawn<'ignore', 'pipe', 'pipe'>>
|
|
49
|
+
try {
|
|
50
|
+
proc = Bun.spawn([...r.cmd], { cwd: root, stdin: 'ignore', stdout: 'pipe', stderr: 'pipe' })
|
|
51
|
+
} catch (e) {
|
|
52
|
+
rendering.delete(root)
|
|
53
|
+
return bad(`render: ${r.cmd.join(' ')}: ${(e as Error).message}`, 500)
|
|
54
|
+
}
|
|
55
|
+
const enc = new TextEncoder()
|
|
56
|
+
const stream = new ReadableStream<Uint8Array>({
|
|
57
|
+
async start(c) {
|
|
58
|
+
const pump = async (s: ReadableStream<Uint8Array>): Promise<void> => {
|
|
59
|
+
for await (const chunk of s) c.enqueue(chunk)
|
|
60
|
+
}
|
|
61
|
+
await Promise.all([pump(proc.stdout), pump(proc.stderr)])
|
|
62
|
+
const code = await proc.exited
|
|
63
|
+
rendering.delete(root)
|
|
64
|
+
const after = times()
|
|
65
|
+
const moved = [...after.keys()].filter((f) => before.get(f) !== after.get(f))
|
|
66
|
+
const committed = moved.length > 0 && machineCommit({ anchor: root, message: `store(steam): the ${fam} made again`, paths: moved.map((f) => join(dir, f)) })
|
|
67
|
+
c.enqueue(enc.encode(code === 0 ? `\n✓ ${moved.length} file${moved.length === 1 ? '' : 's'} made again${committed ? ', committed' : ''}\n` : `\n✗ failed (exit ${code})\n`))
|
|
68
|
+
c.close()
|
|
69
|
+
},
|
|
70
|
+
})
|
|
71
|
+
return new Response(stream, { headers: { 'Content-Type': 'text/plain; charset=utf-8', ...NO_STORE } })
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async function note(root: string, b: Record<string, unknown>): Promise<Response> {
|
|
75
|
+
if (!isStore(b.store)) return bad(`note: no store ${String(b.store)}`)
|
|
76
|
+
const asset = String(b.asset ?? '')
|
|
77
|
+
const field = asset.startsWith(fieldAsset('')) ? asset.slice(fieldAsset('').length) : null
|
|
78
|
+
const image = field === null ? shownFile(root, asset) : null
|
|
79
|
+
if (field === null && image === null) return bad(`note: ${asset} is no picture of the store`)
|
|
80
|
+
const { dir } = await openReview(root)
|
|
81
|
+
const unit = await loadStoresUnit(root)
|
|
82
|
+
const png = image && /\.(png|jpe?g)$/i.test(image) ? image : undefined
|
|
83
|
+
return Response.json(
|
|
84
|
+
await noteOn(dir, {
|
|
85
|
+
subject: { kind: 'store', store: b.store, asset },
|
|
86
|
+
origin: { by: 'owner', from: 'stores' },
|
|
87
|
+
name: `${STORE_NAME[b.store]}, ${field ?? asset.split('/').pop()}`,
|
|
88
|
+
text: String(b.text ?? ''),
|
|
89
|
+
topic: unit?.topic ?? '',
|
|
90
|
+
...(png ? { image: png } : {}),
|
|
91
|
+
}),
|
|
92
|
+
{ headers: NO_STORE },
|
|
93
|
+
)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function reveal(root: string, b: Record<string, unknown>): Response {
|
|
97
|
+
const abs = shownFile(root, String(b.path ?? ''))
|
|
98
|
+
if (!abs) return bad(`reveal: ${String(b.path)} is no file of a store`, 404)
|
|
99
|
+
if (process.platform !== 'darwin') return bad('the Finder is macOS only', 501)
|
|
100
|
+
const r = Bun.spawnSync(['open', '-R', abs], { stdout: 'ignore', stderr: 'pipe' })
|
|
101
|
+
return r.exitCode === 0 ? Response.json({ ok: true }, { headers: NO_STORE }) : bad(`open -R: ${r.stderr.toString().trim() || `exit ${r.exitCode}`}`, 500)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** the stores routes of the repo at `root`; null for any other path */
|
|
105
|
+
export async function storesRoutes(root: string, req: Request, path: string): Promise<Response | null> {
|
|
106
|
+
if (path !== '/api/stores' && !path.startsWith('/api/stores/')) return null
|
|
107
|
+
try {
|
|
108
|
+
if (path === '/api/stores' && req.method === 'GET') {
|
|
109
|
+
const { dir } = await openReview(root)
|
|
110
|
+
return Response.json(await storesState(root, dir), { headers: NO_STORE })
|
|
111
|
+
}
|
|
112
|
+
if (path.startsWith(FILE_ROUTE) && req.method === 'GET') {
|
|
113
|
+
const abs = shownFile(root, decodeURIComponent(path.slice(FILE_ROUTE.length)))
|
|
114
|
+
return abs ? ranged(req, abs) : bad(`${path}?`, 404)
|
|
115
|
+
}
|
|
116
|
+
if (req.method !== 'POST') return bad(`stores: no route ${req.method} ${path}`, 404)
|
|
117
|
+
const body = (await req.json().catch(() => ({}))) as Record<string, unknown>
|
|
118
|
+
if (path === '/api/stores/text') return await writeText(root, body)
|
|
119
|
+
if (path === '/api/stores/render') return await render(root, body.family)
|
|
120
|
+
if (path === '/api/stores/notes') return await note(root, body)
|
|
121
|
+
if (path === '/api/stores/reveal') return reveal(root, body)
|
|
122
|
+
return bad(`stores: no route ${req.method} ${path}`, 404)
|
|
123
|
+
} catch (e) {
|
|
124
|
+
return bad(e)
|
|
125
|
+
}
|
|
126
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// a repo's parts of the stores desk (wiki build/shipkit/stores.md, What a project adds): its extra texts and the
|
|
2
|
+
// account's ask per store, the commands that make a picture family again, the review topic his notes raise on. Every
|
|
3
|
+
// unit file imports this module, so it stays light: desk.ts finds and reads the unit
|
|
4
|
+
import { STORE_IDS, type StoreId } from './model.ts'
|
|
5
|
+
|
|
6
|
+
/** a command that makes a picture family again: argv from the repo root, and how long it takes in words */
|
|
7
|
+
export interface StoreRender {
|
|
8
|
+
cmd: readonly string[]
|
|
9
|
+
about?: string
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export type RenderFamily = 'capsules' | 'shots'
|
|
13
|
+
export const RENDER_FAMILIES: readonly RenderFamily[] = ['capsules', 'shots']
|
|
14
|
+
|
|
15
|
+
export interface StorePart {
|
|
16
|
+
/** more repo relative text files shown after the store's own, each `## ` section a card */
|
|
17
|
+
texts?: readonly string[]
|
|
18
|
+
/** the account's ask, by its file name in `.shipkit/asks/` (open or done), `.md` left out */
|
|
19
|
+
ask?: string
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface StoresDef {
|
|
23
|
+
steam?: StorePart
|
|
24
|
+
itch?: StorePart
|
|
25
|
+
render?: Partial<Record<RenderFamily, StoreRender>>
|
|
26
|
+
/** the review topic his notes on a store thing raise on */
|
|
27
|
+
topic?: string
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export type StoresUnit = StoresDef & { kind: 'stores' }
|
|
31
|
+
|
|
32
|
+
const REL = /^(?!\/)(?!.*(^|\/)\.\.(\/|$)).+$/
|
|
33
|
+
|
|
34
|
+
/** why a stores unit cannot be read, or null */
|
|
35
|
+
export function storesProblem(d: StoresDef): string | null {
|
|
36
|
+
for (const id of STORE_IDS) {
|
|
37
|
+
const p = d[id]
|
|
38
|
+
if (p === undefined) continue
|
|
39
|
+
for (const t of p.texts ?? []) if (typeof t !== 'string' || !REL.test(t) || !t.endsWith('.md')) return `stores: ${id}.texts '${String(t)}' is a repo relative .md file`
|
|
40
|
+
if (p.ask !== undefined && (typeof p.ask !== 'string' || !/^[\w.-]+$/.test(p.ask))) return `stores: ${id}.ask '${String(p.ask)}' is an ask's file name, .md left out`
|
|
41
|
+
}
|
|
42
|
+
for (const [f, r] of Object.entries(d.render ?? {})) {
|
|
43
|
+
if (!(RENDER_FAMILIES as readonly string[]).includes(f)) return `stores: render '${f}' is one of ${RENDER_FAMILIES.join(', ')}`
|
|
44
|
+
if (!Array.isArray(r?.cmd) || r.cmd.length === 0 || r.cmd.some((a) => typeof a !== 'string')) return `stores: render.${f}.cmd is an argv`
|
|
45
|
+
}
|
|
46
|
+
if (d.topic !== undefined && !/^[a-z][a-z0-9-]*$/.test(d.topic)) return `stores: topic '${d.topic}' is a lowercase word`
|
|
47
|
+
return null
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function stores(d: StoresDef): StoresUnit {
|
|
51
|
+
const why = storesProblem(d)
|
|
52
|
+
if (why !== null) throw new Error(why)
|
|
53
|
+
return { ...d, kind: 'stores' }
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** a store's part of the unit, empty when it declares none */
|
|
57
|
+
export const partOf = (u: StoresDef | null, id: StoreId): StorePart => u?.[id] ?? {}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
// HOW MANY INSTRUCTIONS A PERFORMANCE CORE RETIRES PER MS, on this machine and
|
|
3
|
+
// this bun. The core figure (judge-suites.ts coreMs) converts what a suite's
|
|
4
|
+
// main thread retired on an efficiency core at a rate no faster than this
|
|
5
|
+
// one, and at this one when the thread barely ran on a performance core. A
|
|
6
|
+
// reference workload runs on the main thread of its own process, asking for
|
|
7
|
+
// a performance core, and only its part on a performance core is counted. The
|
|
8
|
+
// best rep of one measure stands: contention only ever lowers the rate.
|
|
9
|
+
//
|
|
10
|
+
// The workload is test shaped on purpose: code built and run once (parse,
|
|
11
|
+
// bytecode, the first tiers), classes, closures, arrays, strings, JSON. Its
|
|
12
|
+
// rate sits at the MEDIAN of real suites' main threads, and a conversion at
|
|
13
|
+
// the median lets a compute bound suite through at half its cost, so the
|
|
14
|
+
// figure converts at LOW_SUITE_SHARE of it: the low end of real suites.
|
|
15
|
+
//
|
|
16
|
+
// Kept per machine in the home dir, and measured again when the key changes
|
|
17
|
+
// (chip, os, bun, workload) or when the machine is quieter than at the last
|
|
18
|
+
// measure. The newest measure stands, never the highest seen.
|
|
19
|
+
import { existsSync, mkdirSync, readFileSync } from 'fs'
|
|
20
|
+
import { dirname, join } from 'path'
|
|
21
|
+
import { atomicWrite } from '../core/locked-write.ts'
|
|
22
|
+
import { homeDir } from '../home/home-paths.ts'
|
|
23
|
+
import { coreCounters, machineKey, type CoreCounters } from './core-counters.ts'
|
|
24
|
+
|
|
25
|
+
export type Calibration = { key: string; instrPerMs: number; loadShare: number; at: number }
|
|
26
|
+
|
|
27
|
+
export const calibrationFile = (home: string = homeDir()): string => join(home, 'suite-calibration.json')
|
|
28
|
+
|
|
29
|
+
/** the reference workload's version: a change to referenceWork bumps it */
|
|
30
|
+
const WORKLOAD_VERSION = 2
|
|
31
|
+
|
|
32
|
+
/** the share of the reference rate the core figure converts at. Measured on
|
|
33
|
+
* an M1 Pro at load 13 to 19, one repo's 103 suites with 100 ms or more on a
|
|
34
|
+
* performance core: their main thread rates there 5.9 to 14.6 G/s, the
|
|
35
|
+
* tenth percentile 7.3, the median 10.4, this workload 10.2 to 10.4. A
|
|
36
|
+
* tight arithmetic loop runs at 4.3 to 4.6 G/s. The tenth percentile it is */
|
|
37
|
+
export const LOW_SUITE_SHARE = 0.7
|
|
38
|
+
|
|
39
|
+
/** what a rate is only true for: the machine, its os and bun, and the workload */
|
|
40
|
+
export const calibrationKey = (perfLevels: number): string => `${machineKey(perfLevels)}, reference workload v${WORKLOAD_VERSION}`
|
|
41
|
+
|
|
42
|
+
/** the instructions per ms the core figure converts at */
|
|
43
|
+
export const conversionRate = (c: Calibration): number => c.instrPerMs * LOW_SUITE_SHARE
|
|
44
|
+
|
|
45
|
+
/** how much quieter the machine must be than at the kept measure to measure again */
|
|
46
|
+
export const QUIETER_BY = 0.1
|
|
47
|
+
const REPS = 8
|
|
48
|
+
/** a rep's part on a performance core shorter than this gives no rate */
|
|
49
|
+
const MIN_P_MS = 4
|
|
50
|
+
|
|
51
|
+
let gen = 0
|
|
52
|
+
/** the reference workload: `n` small modules, each built from fresh source and run once */
|
|
53
|
+
export function referenceWork(n: number): number {
|
|
54
|
+
gen++
|
|
55
|
+
let acc = 0
|
|
56
|
+
for (let k = 0; k < n; k++) {
|
|
57
|
+
const id = gen * 100000 + k
|
|
58
|
+
const src = `class A${id} { constructor(v) { this.v = v; this.items = [] } add(x) { this.items.push({ x, at: this.items.length }); return this } sum() { return this.items.reduce((s, i) => s + i.x * i.at, 0) } }
|
|
59
|
+
const make = (n) => { const a = new A${id}(n); for (let i = 0; i < 12; i++) a.add(i + n); return a };
|
|
60
|
+
const rows = [1, 2, 3, 4, 5].map((n) => make(n + ${k % 7}));
|
|
61
|
+
const text = rows.map((r) => r.sum()).join(':') + '${id}';
|
|
62
|
+
const parts = text.split(':').filter((p) => p.length > 1).map((p) => p.padStart(6, '0'));
|
|
63
|
+
const obj = Object.fromEntries(parts.map((p, i) => ['k' + i, p]));
|
|
64
|
+
return Object.keys(obj).length + JSON.stringify(obj).length + (text.match(/\\d+/g) ?? []).length;`
|
|
65
|
+
acc += (new Function(src) as () => number)()
|
|
66
|
+
}
|
|
67
|
+
return acc
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** the best rep's rate on the performance cores alone, or why no rep ran
|
|
71
|
+
* long enough there. The thread's counts split by core type, so a rep the
|
|
72
|
+
* scheduler spilled still gives the rate of its part on a performance core */
|
|
73
|
+
export function measureRate(c: CoreCounters, reps: number = REPS): { instrPerMs: number } | { reason: string } {
|
|
74
|
+
c.setQos('fast')
|
|
75
|
+
referenceWork(60)
|
|
76
|
+
let best = 0
|
|
77
|
+
for (let r = 0; r < reps; r++) {
|
|
78
|
+
const a = c.thread()
|
|
79
|
+
referenceWork(150)
|
|
80
|
+
const b = c.thread()
|
|
81
|
+
const ms = b.pCpuMs - a.pCpuMs
|
|
82
|
+
if (ms >= MIN_P_MS) best = Math.max(best, (b.pInstructions - a.pInstructions) / ms)
|
|
83
|
+
}
|
|
84
|
+
return best > 0 ? { instrPerMs: best } : { reason: `the reference workload never ran ${MIN_P_MS}ms on a performance core in ${reps} reps` }
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** measure again when nothing is kept, the machine changed, or it is quieter now */
|
|
88
|
+
export const needsMeasure = (kept: Calibration | null, key: string, loadShare: number): boolean =>
|
|
89
|
+
kept == null || kept.key !== key || loadShare < kept.loadShare - QUIETER_BY
|
|
90
|
+
|
|
91
|
+
const readKept = (file: string): Calibration | null => {
|
|
92
|
+
try {
|
|
93
|
+
const c = JSON.parse(readFileSync(file, 'utf8')) as Partial<Calibration>
|
|
94
|
+
return typeof c.key === 'string' && typeof c.instrPerMs === 'number' && c.instrPerMs > 0 && typeof c.loadShare === 'number' && typeof c.at === 'number' ? c as Calibration : null
|
|
95
|
+
} catch {
|
|
96
|
+
return null
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** this machine's rate: the kept one, or a fresh measure in a process of its
|
|
101
|
+
* own (its main thread asks for a performance core, which this one must not) */
|
|
102
|
+
export function machineRate(o: { loadShare: number; file?: string; counters?: CoreCounters | { reason: string } }): Calibration | { reason: string; counters: boolean } {
|
|
103
|
+
const c = o.counters ?? coreCounters()
|
|
104
|
+
if ('reason' in c) return { reason: c.reason, counters: false }
|
|
105
|
+
const file = o.file ?? calibrationFile()
|
|
106
|
+
const key = calibrationKey(c.perfLevels)
|
|
107
|
+
const kept = existsSync(file) ? readKept(file) : null
|
|
108
|
+
if (!needsMeasure(kept, key, o.loadShare)) return kept!
|
|
109
|
+
const run = Bun.spawnSync([process.execPath, join(import.meta.dir, 'calibrate.ts')], { stdout: 'pipe', stderr: 'pipe', timeout: 20_000 })
|
|
110
|
+
let measured: { instrPerMs?: number; reason?: string } = {}
|
|
111
|
+
try { measured = JSON.parse(run.stdout.toString()) as typeof measured } catch { /* reason below */ }
|
|
112
|
+
if (typeof measured.instrPerMs !== 'number') {
|
|
113
|
+
if (kept != null && kept.key === key) return kept
|
|
114
|
+
return { reason: measured.reason ?? `the calibration process failed (exit ${run.exitCode}${run.stderr.length > 0 ? `: ${run.stderr.toString().trim().slice(0, 200)}` : ''})`, counters: true }
|
|
115
|
+
}
|
|
116
|
+
const next = { key, instrPerMs: measured.instrPerMs, loadShare: o.loadShare, at: Date.now() }
|
|
117
|
+
try {
|
|
118
|
+
mkdirSync(dirname(file), { recursive: true })
|
|
119
|
+
atomicWrite(file, `${JSON.stringify(next, null, 2)}\n`)
|
|
120
|
+
} catch { /* kept for this run only: the next one measures again */ }
|
|
121
|
+
return next
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (import.meta.main) {
|
|
125
|
+
const c = coreCounters()
|
|
126
|
+
console.log(JSON.stringify('reason' in c ? c : measureRate(c)))
|
|
127
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
// THE INSTRUCTIONS A THREAD RETIRED, per core type, read through bun:ffi on
|
|
2
|
+
// darwin. CPU time is not a measure of work on Apple silicon: under load the
|
|
3
|
+
// scheduler spills a run onto the efficiency cores, where the same retired
|
|
4
|
+
// instructions cost 3 to 4x the CPU time (measured on an M1 Pro: one suite
|
|
5
|
+
// retired 8.61, 8.80 and 8.60 G on P, E and P cores, for 1.0, 2.8 and 1.1 s
|
|
6
|
+
// of its main thread's CPU). The split by core type lets the budget charge a
|
|
7
|
+
// suite's main thread the CPU it would burn on a performance core
|
|
8
|
+
// (judge-suites.ts coreMs); the process's other threads are charged as
|
|
9
|
+
// measured.
|
|
10
|
+
//
|
|
11
|
+
// One call, proc_pidinfo(PROC_PIDTHREADCOUNTS) on the calling thread's id:
|
|
12
|
+
// per core type, its instructions, cycles and user and system time. Pinned by
|
|
13
|
+
// tests/suite-core-counters.test.ts.
|
|
14
|
+
import { cpus, release } from 'os'
|
|
15
|
+
|
|
16
|
+
/** the calling thread since it started: retired instructions and CPU ms,
|
|
17
|
+
* every core type, and the part the performance cores ran */
|
|
18
|
+
export type ThreadCounts = { instructions: number; pInstructions: number; cpuMs: number; pCpuMs: number }
|
|
19
|
+
|
|
20
|
+
export type CoreCounters = {
|
|
21
|
+
/** must be called on the thread to count */
|
|
22
|
+
thread: () => ThreadCounts
|
|
23
|
+
/** how many core types the machine has: 2 on Apple silicon */
|
|
24
|
+
perfLevels: number
|
|
25
|
+
/** ask the scheduler for a core type for the calling thread */
|
|
26
|
+
setQos: (q: 'fast' | 'slow') => void
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const PROC_PIDTHREADCOUNTS = 34
|
|
30
|
+
/** struct proc_threadcounts: u16 ptc_len, u16 and u32 reserved, then one
|
|
31
|
+
* struct proc_threadcounts_data per core type, performance first */
|
|
32
|
+
const PTC_HEADER = 8
|
|
33
|
+
/** struct proc_threadcounts_data: u64 instructions, cycles, user time, system time (mach ticks), energy */
|
|
34
|
+
export const PTCD = { bytes: 40, instructions: 0, cycles: 8, userTime: 16, systemTime: 24 } as const
|
|
35
|
+
const MAX_LEVELS = 4
|
|
36
|
+
const QOS_USER_INTERACTIVE = 0x21
|
|
37
|
+
const QOS_BACKGROUND = 0x09
|
|
38
|
+
|
|
39
|
+
/** read the counts of one thread out of a PROC_PIDTHREADCOUNTS answer */
|
|
40
|
+
export function parseThreadCounts(view: DataView, msPerTick: number): ThreadCounts {
|
|
41
|
+
const levels = view.getUint16(0, true)
|
|
42
|
+
const out: ThreadCounts = { instructions: 0, pInstructions: 0, cpuMs: 0, pCpuMs: 0 }
|
|
43
|
+
for (let l = 0; l < levels; l++) {
|
|
44
|
+
const o = PTC_HEADER + l * PTCD.bytes
|
|
45
|
+
const instr = Number(view.getBigUint64(o + PTCD.instructions, true))
|
|
46
|
+
const ms = Number(view.getBigUint64(o + PTCD.userTime, true) + view.getBigUint64(o + PTCD.systemTime, true)) * msPerTick
|
|
47
|
+
out.instructions += instr
|
|
48
|
+
out.cpuMs += ms
|
|
49
|
+
if (l === 0) { out.pInstructions = instr; out.pCpuMs = ms }
|
|
50
|
+
}
|
|
51
|
+
return out
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** the counters, or why this machine has none */
|
|
55
|
+
export function coreCounters(): CoreCounters | { reason: string } {
|
|
56
|
+
if (process.platform !== 'darwin') return { reason: `${process.platform} has no per thread instruction counter wired here` }
|
|
57
|
+
try {
|
|
58
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
59
|
+
const { dlopen, FFIType, ptr } = require('bun:ffi') as typeof import('bun:ffi')
|
|
60
|
+
const lib = dlopen('/usr/lib/libSystem.B.dylib', {
|
|
61
|
+
proc_pidinfo: { args: [FFIType.i32, FFIType.i32, FFIType.u64, FFIType.ptr, FFIType.i32], returns: FFIType.i32 },
|
|
62
|
+
pthread_threadid_np: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.i32 },
|
|
63
|
+
mach_timebase_info: { args: [FFIType.ptr], returns: FFIType.i32 },
|
|
64
|
+
pthread_set_qos_class_self_np: { args: [FFIType.u32, FFIType.i32], returns: FFIType.i32 },
|
|
65
|
+
})
|
|
66
|
+
const tb = new Uint32Array(2)
|
|
67
|
+
if (lib.symbols.mach_timebase_info(ptr(tb)) !== 0 || tb[1] === 0) return { reason: 'mach_timebase_info failed' }
|
|
68
|
+
const msPerTick = tb[0]! / tb[1]! / 1e6
|
|
69
|
+
const buf = new BigUint64Array((PTC_HEADER + MAX_LEVELS * PTCD.bytes) / 8)
|
|
70
|
+
const view = new DataView(buf.buffer)
|
|
71
|
+
const tid = new BigUint64Array(1)
|
|
72
|
+
const thread = (): ThreadCounts => {
|
|
73
|
+
if (lib.symbols.pthread_threadid_np(null, ptr(tid)) !== 0) throw new Error('pthread_threadid_np failed')
|
|
74
|
+
if (lib.symbols.proc_pidinfo(process.pid, PROC_PIDTHREADCOUNTS, tid[0]!, ptr(buf), buf.byteLength) <= 0) throw new Error('proc_pidinfo(PROC_PIDTHREADCOUNTS) failed')
|
|
75
|
+
return parseThreadCounts(view, msPerTick)
|
|
76
|
+
}
|
|
77
|
+
const first = thread()
|
|
78
|
+
if (first.instructions <= 0) return { reason: 'PROC_PIDTHREADCOUNTS counts no instruction on this machine' }
|
|
79
|
+
return {
|
|
80
|
+
thread,
|
|
81
|
+
perfLevels: view.getUint16(0, true),
|
|
82
|
+
setQos: (q) => { lib.symbols.pthread_set_qos_class_self_np(q === 'fast' ? QOS_USER_INTERACTIVE : QOS_BACKGROUND, 0) },
|
|
83
|
+
}
|
|
84
|
+
} catch (e) {
|
|
85
|
+
return { reason: `the darwin instruction counters cannot be read (${e instanceof Error ? e.message : String(e)})` }
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** what a rate is only true for: the chip, its core types, the os and bun */
|
|
90
|
+
export const machineKey = (perfLevels: number): string =>
|
|
91
|
+
`${cpus()[0]?.model ?? 'unknown cpu'}, ${cpus().length} cores, ${perfLevels} core types, darwin ${release()}, bun ${Bun.version}`
|