code-foundry 1.7.0 → 1.9.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/.github/code-foundry.yml +2 -0
- package/.gitignore +3 -0
- package/CHANGELOG.md +14 -0
- package/README.md +2 -0
- package/docs/CONFIGURATION.md +36 -2
- package/docs/PERFORMANCE.md +58 -0
- package/docs/README.md +1 -0
- package/docs/WORKFLOWS.md +9 -9
- package/package.json +2 -1
- package/src/cli.mjs +13 -12
- package/src/commands/sync.mjs +2 -0
- package/src/lib/node-package-performance.mjs +224 -0
- package/src/runtime.mjs +110 -16
- package/src/templates/gitignore +3 -0
package/.github/code-foundry.yml
CHANGED
package/.gitignore
CHANGED
|
@@ -39,11 +39,14 @@ htmlcov/
|
|
|
39
39
|
.coverage*
|
|
40
40
|
.next/
|
|
41
41
|
artifacts/
|
|
42
|
+
performance-results.json
|
|
43
|
+
performance-results/
|
|
42
44
|
cache/
|
|
43
45
|
!.github/actions/cache/
|
|
44
46
|
!.github/actions/cache/action.yml
|
|
45
47
|
typechain/
|
|
46
48
|
coverage.json
|
|
49
|
+
performance-results/
|
|
47
50
|
tests/.bin/
|
|
48
51
|
**/.app/
|
|
49
52
|
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [1.9.0](https://github.com/0xPlayerOne/code-foundry/compare/v1.8.0...v1.9.0) (2026-09-08)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Features
|
|
7
|
+
|
|
8
|
+
* **performance:** standardize package audit contracts ([#501](https://github.com/0xPlayerOne/code-foundry/issues/501)) ([ab30081](https://github.com/0xPlayerOne/code-foundry/commit/ab3008120772922a1affd1e5648a99b97b02e455))
|
|
9
|
+
|
|
10
|
+
## [1.8.0](https://github.com/0xPlayerOne/code-foundry/compare/v1.7.0...v1.8.0) (2026-09-08)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
### Features
|
|
14
|
+
|
|
15
|
+
* **performance:** enforce source budgets ([#499](https://github.com/0xPlayerOne/code-foundry/issues/499)) ([2f65ba1](https://github.com/0xPlayerOne/code-foundry/commit/2f65ba127f8fe186eda5d6bcae77150f13458428))
|
|
16
|
+
|
|
3
17
|
## [1.7.0](https://github.com/0xPlayerOne/code-foundry/compare/v1.6.1...v1.7.0) (2026-09-08)
|
|
4
18
|
|
|
5
19
|
|
package/README.md
CHANGED
|
@@ -37,6 +37,8 @@ contract. For normal updates, edit that file and run `npx code-foundry sync`.
|
|
|
37
37
|
|
|
38
38
|
- Short workflow callers for CI, Test, Security, CodeQL, Draft PR, Release PR,
|
|
39
39
|
and Release.
|
|
40
|
+
- A deterministic performance lane with ordered command support, stable result
|
|
41
|
+
artifacts, and an optional shared Node package budget profile.
|
|
40
42
|
- A small `.githooks/pre-commit` launcher with language-aware formatting and
|
|
41
43
|
linting.
|
|
42
44
|
- An optional `.mise.toml`/`mise.lock`, repository profile configuration, and
|
package/docs/CONFIGURATION.md
CHANGED
|
@@ -37,7 +37,9 @@ repository manifests and source
|
|
|
37
37
|
| `toolchain` | `auto`, `native`, `mise` | Environment setup policy; defaults to `auto` |
|
|
38
38
|
| `staging_validation_mode` | `fast`, `audit` | Validation tier for pull requests targeting `staging`; defaults to `fast` |
|
|
39
39
|
| `performance` | `auto`, `true`, `false` | Run a deterministic performance task when a supported entrypoint exists; defaults to `auto` |
|
|
40
|
-
| `performance_command` | JSON
|
|
40
|
+
| `performance_command` | JSON argv array or array of argv arrays | One or more ordered commands for non-package harnesses; package scripts take precedence |
|
|
41
|
+
| `performance_profile` | empty, `node-package` | Optional shared package import, memory, archive, and dependency budget harness |
|
|
42
|
+
| `performance_budget_file` | repository path | Budget policy for the shared package harness; defaults to `performance-package-budgets.json` |
|
|
41
43
|
| `features` | `all` or a list | Standard workflow callers |
|
|
42
44
|
| `codeql` | `auto`, `true`, `false` | CodeQL policy; public repositories default to enabled, non-public repositories default to disabled |
|
|
43
45
|
| `codeql_rust_shards` | JSON array of paths | Rust scan scopes; `["all"]` keeps the safe single full scan |
|
|
@@ -62,7 +64,8 @@ Supported features are `ci`, `codeql`, `security`, `test`, `draft-pr`,
|
|
|
62
64
|
|
|
63
65
|
The shared Test workflow exposes a deterministic `Performance` job. In JavaScript
|
|
64
66
|
repositories it discovers `performance:check` first and `perf:check` second. Other
|
|
65
|
-
repositories can declare
|
|
67
|
+
repositories can declare one argv array, or an ordered array of argv arrays, without
|
|
68
|
+
shell interpolation:
|
|
66
69
|
|
|
67
70
|
```yaml
|
|
68
71
|
performance: true
|
|
@@ -70,6 +73,37 @@ performance_command: '["python3","scripts/performance_audit.py","--check"]'
|
|
|
70
73
|
performance_runner: ubuntu-latest
|
|
71
74
|
```
|
|
72
75
|
|
|
76
|
+
The `node-package` profile adds a shared, repository-configured audit:
|
|
77
|
+
|
|
78
|
+
```yaml
|
|
79
|
+
performance: true
|
|
80
|
+
performance_profile: node-package
|
|
81
|
+
performance_budget_file: performance-package-budgets.json
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
```json
|
|
85
|
+
{
|
|
86
|
+
"schemaVersion": 1,
|
|
87
|
+
"importTarget": "./dist/index.js",
|
|
88
|
+
"controlImport": "typebox",
|
|
89
|
+
"samples": 7,
|
|
90
|
+
"budgets": {
|
|
91
|
+
"coldImportP95Ms": 150,
|
|
92
|
+
"coldImportRssMaxBytes": 25000000,
|
|
93
|
+
"packedBytes": 500000,
|
|
94
|
+
"productionDependencyCount": 20
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
Supported budgets are `coldImportP50Ms`, `coldImportP95Ms`,
|
|
100
|
+
`coldImportRssMaxBytes`, `coldImportRelativeP50`, `packedBytes`,
|
|
101
|
+
`unpackedBytes`, `packageFileCount`, `packageMapFileCount`, and
|
|
102
|
+
`productionDependencyCount`. The profile writes
|
|
103
|
+
`performance-results/node-package.json`. Every performance run also writes
|
|
104
|
+
`performance-results/summary.json`, including repository-owned scripts and
|
|
105
|
+
configured commands, so artifact consumers have one stable status contract.
|
|
106
|
+
|
|
73
107
|
`performance: false` disables discovery. Performance budgets, fixtures, mock
|
|
74
108
|
providers, and live endpoint credentials remain repository-owned. The shared job
|
|
75
109
|
owns checkout, pinned setup, billing controls, concurrency, and artifact upload;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# Performance budgets and baselines
|
|
2
|
+
|
|
3
|
+
Code Foundry measures its source runtime with `bun run performance:check`. The
|
|
4
|
+
check writes `performance-results.json` for CI artifact upload and fails when a
|
|
5
|
+
budget is exceeded. Generated reports are ignored by Git because measurements
|
|
6
|
+
belong to the run that produced them, not the source tree.
|
|
7
|
+
|
|
8
|
+
## Enforced budgets
|
|
9
|
+
|
|
10
|
+
| Metric | Budget | Why it is bounded |
|
|
11
|
+
| ----------------------------------- | -----: | ---------------------------------------------------- |
|
|
12
|
+
| CLI help startup p95 | 250 ms | Detect eager imports and startup regressions |
|
|
13
|
+
| Runtime mode startup p95 | 250 ms | Detect baseline runtime initialization regressions |
|
|
14
|
+
| Focused runtime tests | 10 s | Keep the representative runtime contract inexpensive |
|
|
15
|
+
| Format, lint, type-check, and build | 15 s | Bound the local CI feedback loop |
|
|
16
|
+
| Runtime dependencies | 0 | Keep the installed CLI dependency-free |
|
|
17
|
+
| Development dependencies | 4 | Prevent unreviewed toolchain growth |
|
|
18
|
+
| Packed artifact | 200 kB | Bound registry transfer and install cost |
|
|
19
|
+
| Unpacked artifact | 750 kB | Bound installed footprint |
|
|
20
|
+
| Packed files | 80 | Detect accidental release contents |
|
|
21
|
+
|
|
22
|
+
The performance workflow disables build-cache reads and writes for this task.
|
|
23
|
+
That makes timing comparisons independent of a warm protected-branch cache and
|
|
24
|
+
prevents benchmark code from populating shared cache entries.
|
|
25
|
+
|
|
26
|
+
## v1.6.1 baseline
|
|
27
|
+
|
|
28
|
+
Measurements were taken on 2026-09-08 with Node 24.18.0. Process startup used
|
|
29
|
+
30 measured samples after four warmups; before and after samples were
|
|
30
|
+
interleaved on the same host.
|
|
31
|
+
|
|
32
|
+
| Measurement | Before | After | Change |
|
|
33
|
+
| -------------------------- | -------: | ----------------------: | -------: |
|
|
34
|
+
| CLI help median | 36.11 ms | 29.57 ms | -18.1% |
|
|
35
|
+
| CLI help p95 | 38.11 ms | 31.56 ms | -17.2% |
|
|
36
|
+
| Runtime mode median | - | 35.94 ms | baseline |
|
|
37
|
+
| Focused runtime tests | - | 2.08 s | baseline |
|
|
38
|
+
| Local CI checks | - | 0.50 s | baseline |
|
|
39
|
+
| Packed / unpacked artifact | - | 160,114 / 616,887 bytes | baseline |
|
|
40
|
+
| Packed files | - | 74 | baseline |
|
|
41
|
+
|
|
42
|
+
The startup reduction comes from loading command implementations only after
|
|
43
|
+
argument parsing selects a command. `--help` no longer imports sync, release,
|
|
44
|
+
fleet, doctor, and CI command modules.
|
|
45
|
+
|
|
46
|
+
The first isolated full hosted audit completed in 3 minutes 2 seconds. Its
|
|
47
|
+
longest jobs were TypeScript CodeQL (66 s), dependency audit (62 s), and unit
|
|
48
|
+
tests (46 s); all other suite jobs completed in 20 seconds or less. Those
|
|
49
|
+
measurements identify the three lanes to optimize before adding more CI fanout.
|
|
50
|
+
|
|
51
|
+
## Release path
|
|
52
|
+
|
|
53
|
+
Treat budget changes like source changes: explain the measured reason in a
|
|
54
|
+
pull request, run the complete validation gate, and merge only when the new
|
|
55
|
+
result artifact is available. A Release Please PR then carries the change into
|
|
56
|
+
the next version. Never raise a budget solely to clear CI; include before/after
|
|
57
|
+
measurements and the expected effect on local feedback, hosted runner time,
|
|
58
|
+
package transfer, or installed footprint.
|
package/docs/README.md
CHANGED
|
@@ -12,6 +12,7 @@ its own names, environments, and deployment details.
|
|
|
12
12
|
- [Release management](RELEASES.md)
|
|
13
13
|
- [Publishing packages](PUBLISHING.md)
|
|
14
14
|
- [Caching and remote caching](CACHING.md)
|
|
15
|
+
- [Performance budgets and baselines](PERFORMANCE.md)
|
|
15
16
|
|
|
16
17
|
## Repository-specific documentation
|
|
17
18
|
|
package/docs/WORKFLOWS.md
CHANGED
|
@@ -76,15 +76,15 @@ repositories can opt in or out without a code change.
|
|
|
76
76
|
|
|
77
77
|
## Standard workflow responsibilities
|
|
78
78
|
|
|
79
|
-
| Workflow | Responsibility
|
|
80
|
-
| ---------- |
|
|
81
|
-
| CI | Format, lint, type-check, and build
|
|
82
|
-
| Test | Unit, performance, integration, E2E, and smoke tests
|
|
83
|
-
| Security | Profile, audits, and public-only Dependency Review
|
|
84
|
-
| CodeQL | GitHub-native code scanning, kept separate from CI
|
|
85
|
-
| Draft PR | Create/update development pull requests
|
|
86
|
-
| Release PR | Promote `staging` into `main` (staging-release topology only)
|
|
87
|
-
| Release | Release Please, GitHub release, and optional npm publication
|
|
79
|
+
| Workflow | Responsibility |
|
|
80
|
+
| ---------- | ----------------------------------------------------------------- |
|
|
81
|
+
| CI | Format, lint, type-check, and build |
|
|
82
|
+
| Test | Unit, standardized performance, integration, E2E, and smoke tests |
|
|
83
|
+
| Security | Profile, audits, and public-only Dependency Review |
|
|
84
|
+
| CodeQL | GitHub-native code scanning, kept separate from CI |
|
|
85
|
+
| Draft PR | Create/update development pull requests |
|
|
86
|
+
| Release PR | Promote `staging` into `main` (staging-release topology only) |
|
|
87
|
+
| Release | Release Please, GitHub release, and optional npm publication |
|
|
88
88
|
|
|
89
89
|
Use concise job names such as `CI / Format`, `Test / Unit`, and
|
|
90
90
|
`CodeQL / Analyze (Python)`. Per-language CodeQL analyzers (Rust shards
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "code-foundry",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.9.0",
|
|
4
4
|
"description": "A fast, language-aware repository factory for agent-ready workflows, testing, security, and release automation.",
|
|
5
5
|
"homepage": "https://github.com/0xPlayerOne/code-foundry#readme",
|
|
6
6
|
"bugs": {
|
|
@@ -43,6 +43,7 @@
|
|
|
43
43
|
"format:check": "oxfmt --check .",
|
|
44
44
|
"fmt": "oxfmt .",
|
|
45
45
|
"lint": "oxlint",
|
|
46
|
+
"performance:check": "node scripts/performance-check.mjs",
|
|
46
47
|
"test": "node --test",
|
|
47
48
|
"test:unit": "node --test",
|
|
48
49
|
"type-check": "tsc --project jsconfig.json",
|
package/src/cli.mjs
CHANGED
|
@@ -4,16 +4,6 @@
|
|
|
4
4
|
import { resolve } from 'node:path'
|
|
5
5
|
import { readFileSync } from 'node:fs'
|
|
6
6
|
import { fileURLToPath } from 'node:url'
|
|
7
|
-
import { doctor } from './commands/doctor.mjs'
|
|
8
|
-
import {
|
|
9
|
-
dispatchPostReleaseHook,
|
|
10
|
-
reconcileRelease,
|
|
11
|
-
releaseRecoveryPlan,
|
|
12
|
-
validateReleasePullRequestDiffs,
|
|
13
|
-
} from './commands/release.mjs'
|
|
14
|
-
import { discoverRepositories, upgradeFleet } from './commands/fleet.mjs'
|
|
15
|
-
import { syncRepository } from './commands/sync.mjs'
|
|
16
|
-
import { manageCiBilling } from './commands/ci.mjs'
|
|
17
7
|
|
|
18
8
|
const packageRoot = resolve(fileURLToPath(new URL('..', import.meta.url)))
|
|
19
9
|
|
|
@@ -133,12 +123,13 @@ function parseArgs(argv) {
|
|
|
133
123
|
return { command, options }
|
|
134
124
|
}
|
|
135
125
|
|
|
136
|
-
function main() {
|
|
126
|
+
async function main() {
|
|
137
127
|
const { command, options } = parseArgs(process.argv.slice(2))
|
|
138
128
|
const target = resolve(options.target)
|
|
139
129
|
|
|
140
130
|
if (command === 'init') {
|
|
141
131
|
try {
|
|
132
|
+
const { syncRepository } = await import('./commands/sync.mjs')
|
|
142
133
|
syncRepository({
|
|
143
134
|
target,
|
|
144
135
|
source: packageRoot,
|
|
@@ -151,12 +142,14 @@ function main() {
|
|
|
151
142
|
}
|
|
152
143
|
} else if (command === 'sync') {
|
|
153
144
|
try {
|
|
145
|
+
const { syncRepository } = await import('./commands/sync.mjs')
|
|
154
146
|
syncRepository({ target, source: packageRoot, dryRun: options.dryRun, force: options.force })
|
|
155
147
|
} catch (error) {
|
|
156
148
|
fail(error instanceof Error ? error.message : String(error))
|
|
157
149
|
}
|
|
158
150
|
} else if (command === 'doctor') {
|
|
159
151
|
try {
|
|
152
|
+
const { doctor } = await import('./commands/doctor.mjs')
|
|
160
153
|
doctor(target, { github: options.github })
|
|
161
154
|
} catch (error) {
|
|
162
155
|
console.error(`code-foundry: ${error instanceof Error ? error.message : String(error)}`)
|
|
@@ -164,6 +157,12 @@ function main() {
|
|
|
164
157
|
}
|
|
165
158
|
} else if (command === 'release') {
|
|
166
159
|
try {
|
|
160
|
+
const {
|
|
161
|
+
dispatchPostReleaseHook,
|
|
162
|
+
reconcileRelease,
|
|
163
|
+
releaseRecoveryPlan,
|
|
164
|
+
validateReleasePullRequestDiffs,
|
|
165
|
+
} = await import('./commands/release.mjs')
|
|
167
166
|
if (options.releaseSubcommand === 'hook') dispatchPostReleaseHook(target, options)
|
|
168
167
|
else if (options.releaseSubcommand === 'validate-prs') validateReleasePullRequestDiffs(target)
|
|
169
168
|
else if (options.releaseSubcommand === 'recovery-plan') releaseRecoveryPlan(target)
|
|
@@ -173,6 +172,7 @@ function main() {
|
|
|
173
172
|
}
|
|
174
173
|
} else if (command === 'fleet') {
|
|
175
174
|
try {
|
|
175
|
+
const { discoverRepositories, upgradeFleet } = await import('./commands/fleet.mjs')
|
|
176
176
|
const root = resolve(options.root)
|
|
177
177
|
if (options.fleetSubcommand === 'status')
|
|
178
178
|
console.log(JSON.stringify(discoverRepositories(root), null, 2))
|
|
@@ -189,6 +189,7 @@ function main() {
|
|
|
189
189
|
}
|
|
190
190
|
} else if (command === 'ci') {
|
|
191
191
|
try {
|
|
192
|
+
const { manageCiBilling } = await import('./commands/ci.mjs')
|
|
192
193
|
manageCiBilling(target, /** @type {'pause'|'resume'|'status'} */ (options.ciSubcommand))
|
|
193
194
|
} catch (error) {
|
|
194
195
|
fail(error instanceof Error ? error.message : String(error))
|
|
@@ -196,7 +197,7 @@ function main() {
|
|
|
196
197
|
} else fail(`unknown command: ${command}`)
|
|
197
198
|
}
|
|
198
199
|
|
|
199
|
-
main()
|
|
200
|
+
await main()
|
|
200
201
|
|
|
201
202
|
/** @param {string} root @returns {string} */
|
|
202
203
|
function readPackageVersion(root) {
|
package/src/commands/sync.mjs
CHANGED
|
@@ -1092,6 +1092,8 @@ function createDefaultConfig(root, source, configuredWorkflow) {
|
|
|
1092
1092
|
staging_validation_mode: 'fast',
|
|
1093
1093
|
performance: 'auto',
|
|
1094
1094
|
performance_command: '',
|
|
1095
|
+
performance_profile: '',
|
|
1096
|
+
performance_budget_file: 'performance-package-budgets.json',
|
|
1095
1097
|
prune_standard: 'false',
|
|
1096
1098
|
cache_packages: 'auto',
|
|
1097
1099
|
cache_build: 'auto',
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
|
|
4
|
+
import { dirname, resolve } from 'node:path'
|
|
5
|
+
import { pathToFileURL } from 'node:url'
|
|
6
|
+
import { spawnSync } from 'node:child_process'
|
|
7
|
+
|
|
8
|
+
const METRICS = new Set([
|
|
9
|
+
'coldImportP50Ms',
|
|
10
|
+
'coldImportP95Ms',
|
|
11
|
+
'coldImportRssMaxBytes',
|
|
12
|
+
'coldImportRelativeP50',
|
|
13
|
+
'packedBytes',
|
|
14
|
+
'unpackedBytes',
|
|
15
|
+
'packageFileCount',
|
|
16
|
+
'packageMapFileCount',
|
|
17
|
+
'productionDependencyCount',
|
|
18
|
+
])
|
|
19
|
+
|
|
20
|
+
/** @param {number[]} values @param {number} percent */
|
|
21
|
+
export function percentile(values, percent) {
|
|
22
|
+
if (values.length === 0) throw new Error('percentile requires at least one value')
|
|
23
|
+
// oxlint-disable-next-line unicorn/no-array-sort -- The project typechecks against ES2022.
|
|
24
|
+
const ordered = [...values].sort((a, b) => a - b)
|
|
25
|
+
const position = ((ordered.length - 1) * percent) / 100
|
|
26
|
+
const lower = Math.floor(position)
|
|
27
|
+
const upper = Math.min(lower + 1, ordered.length - 1)
|
|
28
|
+
const fraction = position - lower
|
|
29
|
+
return ordered[lower] + (ordered[upper] - ordered[lower]) * fraction
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** @param {string} root @param {string} budgetFile @param {string} resultsDirectory */
|
|
33
|
+
export function runNodePackagePerformance(
|
|
34
|
+
root,
|
|
35
|
+
budgetFile = 'performance-package-budgets.json',
|
|
36
|
+
resultsDirectory = 'performance-results'
|
|
37
|
+
) {
|
|
38
|
+
const packageJson = readJson(resolve(root, 'package.json'), 'package.json')
|
|
39
|
+
const policy = readJson(resolve(root, budgetFile), budgetFile)
|
|
40
|
+
if (policy.schemaVersion !== 1) throw new Error(`${budgetFile} must declare schemaVersion 1.`)
|
|
41
|
+
|
|
42
|
+
const samples = policy.samples ?? 7
|
|
43
|
+
if (!Number.isInteger(samples) || samples < 3 || samples > 50)
|
|
44
|
+
throw new Error(`${budgetFile} samples must be an integer from 3 through 50.`)
|
|
45
|
+
const budgets = policy.budgets
|
|
46
|
+
if (!budgets || typeof budgets !== 'object' || Array.isArray(budgets))
|
|
47
|
+
throw new Error(`${budgetFile} budgets must be an object.`)
|
|
48
|
+
for (const [name, maximum] of Object.entries(budgets)) {
|
|
49
|
+
if (!METRICS.has(name)) throw new Error(`${budgetFile} contains unknown metric ${name}.`)
|
|
50
|
+
if (typeof maximum !== 'number' || !Number.isFinite(maximum) || maximum < 0)
|
|
51
|
+
throw new Error(`${budgetFile} budget ${name} must be a non-negative finite number.`)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const importTarget = resolve(root, policy.importTarget ?? packageEntry(packageJson))
|
|
55
|
+
if (!existsSync(importTarget))
|
|
56
|
+
throw new Error(`Node package performance import target does not exist: ${importTarget}`)
|
|
57
|
+
const imports = measureImports(root, pathToFileURL(importTarget).href, samples)
|
|
58
|
+
const control = policy.controlImport
|
|
59
|
+
? measureImports(root, String(policy.controlImport), samples)
|
|
60
|
+
: null
|
|
61
|
+
const pack = npmJson(root, ['pack', '--dry-run', '--json', '--ignore-scripts'], 'npm pack')
|
|
62
|
+
const packed = Array.isArray(pack) ? pack[0] : pack
|
|
63
|
+
if (!packed || typeof packed !== 'object') throw new Error('npm pack returned no package result.')
|
|
64
|
+
/** @type {Array<{path?: string}>} */
|
|
65
|
+
const files = Array.isArray(packed.files) ? packed.files : []
|
|
66
|
+
const productionDependencyCount = npmProductionDependencyCount(root)
|
|
67
|
+
const metrics = {
|
|
68
|
+
coldImportP50Ms: round(
|
|
69
|
+
percentile(
|
|
70
|
+
imports.map((sample) => sample.durationMs),
|
|
71
|
+
50
|
|
72
|
+
)
|
|
73
|
+
),
|
|
74
|
+
coldImportP95Ms: round(
|
|
75
|
+
percentile(
|
|
76
|
+
imports.map((sample) => sample.durationMs),
|
|
77
|
+
95
|
|
78
|
+
)
|
|
79
|
+
),
|
|
80
|
+
coldImportRssMaxBytes: Math.max(...imports.map((sample) => sample.rssDeltaBytes)),
|
|
81
|
+
...(control
|
|
82
|
+
? {
|
|
83
|
+
coldImportRelativeP50: round(
|
|
84
|
+
percentile(
|
|
85
|
+
imports.map((sample) => sample.durationMs),
|
|
86
|
+
50
|
|
87
|
+
) /
|
|
88
|
+
percentile(
|
|
89
|
+
control.map((sample) => sample.durationMs),
|
|
90
|
+
50
|
|
91
|
+
)
|
|
92
|
+
),
|
|
93
|
+
}
|
|
94
|
+
: {}),
|
|
95
|
+
packedBytes: numberField(packed, 'size'),
|
|
96
|
+
unpackedBytes: numberField(packed, 'unpackedSize'),
|
|
97
|
+
packageFileCount: files.length,
|
|
98
|
+
packageMapFileCount: files.filter(
|
|
99
|
+
(file) => file && typeof file === 'object' && String(file.path ?? '').endsWith('.map')
|
|
100
|
+
).length,
|
|
101
|
+
productionDependencyCount,
|
|
102
|
+
}
|
|
103
|
+
const failures = Object.entries(budgets).flatMap(([name, maximum]) => {
|
|
104
|
+
const value = metrics[/** @type {keyof typeof metrics} */ (name)]
|
|
105
|
+
if (typeof value !== 'number' || !Number.isFinite(value)) return [`${name}: metric unavailable`]
|
|
106
|
+
return value > maximum ? [`${name}: ${value} > ${maximum}`] : []
|
|
107
|
+
})
|
|
108
|
+
const result = {
|
|
109
|
+
schemaVersion: 1,
|
|
110
|
+
kind: 'code-foundry-node-package-performance',
|
|
111
|
+
recordedAt: new Date().toISOString(),
|
|
112
|
+
package: typeof packageJson.name === 'string' ? packageJson.name : null,
|
|
113
|
+
importTarget: String(policy.importTarget ?? packageEntry(packageJson)),
|
|
114
|
+
controlImport: policy.controlImport ?? null,
|
|
115
|
+
samples,
|
|
116
|
+
metrics,
|
|
117
|
+
budgets,
|
|
118
|
+
failures,
|
|
119
|
+
passed: failures.length === 0,
|
|
120
|
+
}
|
|
121
|
+
const output = resolve(root, resultsDirectory, 'node-package.json')
|
|
122
|
+
mkdirSync(dirname(output), { recursive: true })
|
|
123
|
+
writeFileSync(output, `${JSON.stringify(result, null, 2)}\n`)
|
|
124
|
+
return result
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** @param {string} root @param {string} specifier @param {number} samples */
|
|
128
|
+
function measureImports(root, specifier, samples) {
|
|
129
|
+
return Array.from({ length: samples }, () => {
|
|
130
|
+
const source = [
|
|
131
|
+
'const before = process.memoryUsage().rss',
|
|
132
|
+
'const started = performance.now()',
|
|
133
|
+
`await import(${JSON.stringify(specifier)})`,
|
|
134
|
+
'const durationMs = performance.now() - started',
|
|
135
|
+
'const rssDeltaBytes = Math.max(0, process.memoryUsage().rss - before)',
|
|
136
|
+
'process.stdout.write(JSON.stringify({ durationMs, rssDeltaBytes }))',
|
|
137
|
+
].join(';')
|
|
138
|
+
const result = spawnSync(process.execPath, ['--input-type=module', '--eval', source], {
|
|
139
|
+
cwd: root,
|
|
140
|
+
encoding: 'utf8',
|
|
141
|
+
env: process.env,
|
|
142
|
+
})
|
|
143
|
+
if (result.error) throw result.error
|
|
144
|
+
if (result.status !== 0)
|
|
145
|
+
throw new Error(`cold import failed for ${specifier}: ${result.stderr.trim()}`)
|
|
146
|
+
const value = JSON.parse(result.stdout)
|
|
147
|
+
if (
|
|
148
|
+
typeof value.durationMs !== 'number' ||
|
|
149
|
+
!Number.isFinite(value.durationMs) ||
|
|
150
|
+
typeof value.rssDeltaBytes !== 'number' ||
|
|
151
|
+
!Number.isFinite(value.rssDeltaBytes)
|
|
152
|
+
)
|
|
153
|
+
throw new Error(`cold import returned invalid metrics for ${specifier}.`)
|
|
154
|
+
return value
|
|
155
|
+
})
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** @param {Record<string, any>} packageJson */
|
|
159
|
+
function packageEntry(packageJson) {
|
|
160
|
+
const rootExport = packageJson.exports?.['.'] ?? packageJson.exports
|
|
161
|
+
if (typeof rootExport === 'string') return rootExport
|
|
162
|
+
if (rootExport && typeof rootExport === 'object') {
|
|
163
|
+
for (const key of ['import', 'default', 'node', 'require']) {
|
|
164
|
+
if (typeof rootExport[key] === 'string') return rootExport[key]
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
for (const key of ['module', 'main']) {
|
|
168
|
+
if (typeof packageJson[key] === 'string') return packageJson[key]
|
|
169
|
+
}
|
|
170
|
+
throw new Error('Unable to infer a package import target; set importTarget in the budget file.')
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** @param {string} root */
|
|
174
|
+
function npmProductionDependencyCount(root) {
|
|
175
|
+
const result = spawnSync('npm', ['ls', '--omit=dev', '--all', '--parseable', '--silent'], {
|
|
176
|
+
cwd: root,
|
|
177
|
+
encoding: 'utf8',
|
|
178
|
+
env: process.env,
|
|
179
|
+
})
|
|
180
|
+
if (result.error || result.status !== 0) return null
|
|
181
|
+
return Math.max(
|
|
182
|
+
0,
|
|
183
|
+
result.stdout
|
|
184
|
+
.split(/\r?\n/)
|
|
185
|
+
.map((line) => line.trim())
|
|
186
|
+
.filter(Boolean).length - 1
|
|
187
|
+
)
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** @param {string} root @param {string[]} args @param {string} label */
|
|
191
|
+
function npmJson(root, args, label) {
|
|
192
|
+
const result = spawnSync('npm', args, { cwd: root, encoding: 'utf8', env: process.env })
|
|
193
|
+
if (result.error) throw result.error
|
|
194
|
+
if (result.status !== 0) throw new Error(`${label} failed: ${result.stderr.trim()}`)
|
|
195
|
+
try {
|
|
196
|
+
return JSON.parse(result.stdout)
|
|
197
|
+
} catch {
|
|
198
|
+
throw new Error(`${label} did not return valid JSON.`)
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** @param {string} file @param {string} label */
|
|
203
|
+
function readJson(file, label) {
|
|
204
|
+
try {
|
|
205
|
+
return JSON.parse(readFileSync(file, 'utf8'))
|
|
206
|
+
} catch (error) {
|
|
207
|
+
throw new Error(
|
|
208
|
+
`${label} must contain valid JSON: ${error instanceof Error ? error.message : error}`,
|
|
209
|
+
{ cause: error }
|
|
210
|
+
)
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** @param {Record<string, any>} value @param {string} field */
|
|
215
|
+
function numberField(value, field) {
|
|
216
|
+
if (typeof value[field] !== 'number' || !Number.isFinite(value[field]))
|
|
217
|
+
throw new Error(`npm pack result is missing numeric ${field}.`)
|
|
218
|
+
return value[field]
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/** @param {number} value */
|
|
222
|
+
function round(value) {
|
|
223
|
+
return Math.round(value * 1000) / 1000
|
|
224
|
+
}
|
package/src/runtime.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
// @ts-check
|
|
3
3
|
|
|
4
|
-
import { appendFileSync, existsSync, readFileSync } from 'node:fs'
|
|
4
|
+
import { appendFileSync, existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
|
|
5
5
|
import { resolve } from 'node:path'
|
|
6
6
|
import { spawnSync } from 'node:child_process'
|
|
7
7
|
import { detectPackageManager, resolveProfile } from './lib/profile.mjs'
|
|
@@ -9,6 +9,7 @@ import { configured, readConfig } from './lib/config.mjs'
|
|
|
9
9
|
import { classifyTestFiles } from './lib/test-discovery.mjs'
|
|
10
10
|
import { classifyValidationMode, evaluateValidationGate } from './lib/validation-policy.mjs'
|
|
11
11
|
import { readReleaseConfig, validateGeneratedReleaseDiff } from './lib/release-policy.mjs'
|
|
12
|
+
import { runNodePackagePerformance } from './lib/node-package-performance.mjs'
|
|
12
13
|
|
|
13
14
|
const root = process.cwd()
|
|
14
15
|
const config = readConfig(resolve(root, '.github/code-foundry.yml'))
|
|
@@ -56,23 +57,118 @@ function performanceEnabled() {
|
|
|
56
57
|
return configured(config.performance, 'auto') !== 'false'
|
|
57
58
|
}
|
|
58
59
|
|
|
59
|
-
function
|
|
60
|
+
function performanceCommands() {
|
|
60
61
|
const raw = configured(config.performance_command, '').trim()
|
|
61
|
-
if (!raw) return
|
|
62
|
+
if (!raw) return []
|
|
62
63
|
let command
|
|
63
64
|
try {
|
|
64
65
|
command = JSON.parse(raw)
|
|
65
66
|
} catch {
|
|
66
|
-
throw new Error('performance_command must be a JSON array of
|
|
67
|
+
throw new Error('performance_command must be a JSON argv array or an array of argv arrays.')
|
|
67
68
|
}
|
|
69
|
+
if (!Array.isArray(command) || command.length === 0)
|
|
70
|
+
throw new Error('performance_command must be a non-empty JSON array.')
|
|
71
|
+
const commands = command.every((argument) => typeof argument === 'string') ? [command] : command
|
|
68
72
|
if (
|
|
69
|
-
!
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
73
|
+
!commands.every(
|
|
74
|
+
(argv) =>
|
|
75
|
+
Array.isArray(argv) &&
|
|
76
|
+
argv.length > 0 &&
|
|
77
|
+
argv.every((argument) => typeof argument === 'string' && argument.length > 0)
|
|
78
|
+
)
|
|
79
|
+
)
|
|
80
|
+
throw new Error(
|
|
81
|
+
'performance_command must contain one argv array or only non-empty argv arrays of non-empty strings.'
|
|
82
|
+
)
|
|
83
|
+
return /** @type {string[][]} */ (commands)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function performanceProfile() {
|
|
87
|
+
const profile = configured(config.performance_profile, '').trim()
|
|
88
|
+
if (!profile) return ''
|
|
89
|
+
if (profile !== 'node-package')
|
|
90
|
+
throw new Error(`Unsupported performance_profile: ${profile}. Expected node-package.`)
|
|
91
|
+
return profile
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const performanceResultsDirectory = 'performance-results'
|
|
95
|
+
|
|
96
|
+
/** @returns {{source: string, argv: string[]}[]} */
|
|
97
|
+
function selectedPerformanceCommands() {
|
|
98
|
+
const name = ['performance:check', 'perf:check'].find((candidate) => hasScript(candidate))
|
|
99
|
+
if (name) {
|
|
100
|
+
const [manager, args] = packageCommand(['run', name])
|
|
101
|
+
return manager ? [{ source: `package-script:${name}`, argv: [manager, ...args] }] : []
|
|
102
|
+
}
|
|
103
|
+
return performanceCommands().map((argv) => ({ source: 'configuration', argv }))
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** @param {string} startedAt @param {'passed'|'failed'} status @param {{source: string, argv: string[], status: number}[]} commands @param {string[]} artifacts @param {string|null} error */
|
|
107
|
+
function writePerformanceSummary(startedAt, status, commands, artifacts, error = null) {
|
|
108
|
+
const directory = resolve(root, performanceResultsDirectory)
|
|
109
|
+
mkdirSync(directory, { recursive: true })
|
|
110
|
+
writeFileSync(
|
|
111
|
+
resolve(directory, 'summary.json'),
|
|
112
|
+
`${JSON.stringify(
|
|
113
|
+
{
|
|
114
|
+
schemaVersion: 1,
|
|
115
|
+
kind: 'code-foundry-performance-summary',
|
|
116
|
+
status,
|
|
117
|
+
startedAt,
|
|
118
|
+
completedAt: new Date().toISOString(),
|
|
119
|
+
commands,
|
|
120
|
+
profile: performanceProfile() || null,
|
|
121
|
+
artifacts,
|
|
122
|
+
error,
|
|
123
|
+
},
|
|
124
|
+
null,
|
|
125
|
+
2
|
|
126
|
+
)}\n`
|
|
127
|
+
)
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function runPerformance() {
|
|
131
|
+
if (!performanceEnabled()) return
|
|
132
|
+
const startedAt = new Date().toISOString()
|
|
133
|
+
/** @type {{source: string, argv: string[], status: number}[]} */
|
|
134
|
+
const records = []
|
|
135
|
+
/** @type {string[]} */
|
|
136
|
+
const artifacts = []
|
|
137
|
+
try {
|
|
138
|
+
for (const command of selectedPerformanceCommands()) {
|
|
139
|
+
const result = spawnSync(command.argv[0], command.argv.slice(1), {
|
|
140
|
+
cwd: root,
|
|
141
|
+
stdio: 'inherit',
|
|
142
|
+
env: process.env,
|
|
143
|
+
})
|
|
144
|
+
if (result.error) throw result.error
|
|
145
|
+
const status = result.status ?? 1
|
|
146
|
+
records.push({ ...command, status })
|
|
147
|
+
if (status !== 0) {
|
|
148
|
+
writePerformanceSummary(startedAt, 'failed', records, artifacts, `command exited ${status}`)
|
|
149
|
+
process.exitCode = status
|
|
150
|
+
return
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
if (performanceProfile() === 'node-package') {
|
|
154
|
+
const result = runNodePackagePerformance(
|
|
155
|
+
root,
|
|
156
|
+
configured(config.performance_budget_file, 'performance-package-budgets.json'),
|
|
157
|
+
performanceResultsDirectory
|
|
158
|
+
)
|
|
159
|
+
artifacts.push(`${performanceResultsDirectory}/node-package.json`)
|
|
160
|
+
if (!result.passed) {
|
|
161
|
+
writePerformanceSummary(startedAt, 'failed', records, artifacts, result.failures.join('; '))
|
|
162
|
+
process.exitCode = 1
|
|
163
|
+
return
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
writePerformanceSummary(startedAt, 'passed', records, artifacts)
|
|
167
|
+
} catch (error) {
|
|
168
|
+
const message = error instanceof Error ? error.message : String(error)
|
|
169
|
+
writePerformanceSummary(startedAt, 'failed', records, artifacts, message)
|
|
170
|
+
throw error
|
|
74
171
|
}
|
|
75
|
-
return command
|
|
76
172
|
}
|
|
77
173
|
|
|
78
174
|
/** @param {string} task */
|
|
@@ -299,7 +395,9 @@ function relevant(task) {
|
|
|
299
395
|
if (task === 'performance') {
|
|
300
396
|
if (!performanceEnabled()) return false
|
|
301
397
|
return Boolean(
|
|
302
|
-
(scripted && scripted.some((candidate) => hasScript(candidate))) ||
|
|
398
|
+
(scripted && scripted.some((candidate) => hasScript(candidate))) ||
|
|
399
|
+
performanceCommands().length > 0 ||
|
|
400
|
+
performanceProfile()
|
|
303
401
|
)
|
|
304
402
|
}
|
|
305
403
|
if (scripted && scripted.some((candidate) => hasScript(candidate)))
|
|
@@ -465,11 +563,7 @@ function ci(task) {
|
|
|
465
563
|
return
|
|
466
564
|
}
|
|
467
565
|
if (task === 'performance') {
|
|
468
|
-
|
|
469
|
-
if (runScript(['performance:check', 'perf:check'])) return
|
|
470
|
-
const command = performanceCommand()
|
|
471
|
-
if (command) run(command[0], command.slice(1))
|
|
472
|
-
return
|
|
566
|
+
return runPerformance()
|
|
473
567
|
}
|
|
474
568
|
/** @type {Record<string, string[]>} */
|
|
475
569
|
const scriptsByTask = {
|
package/src/templates/gitignore
CHANGED
|
@@ -39,11 +39,14 @@ htmlcov/
|
|
|
39
39
|
.coverage*
|
|
40
40
|
.next/
|
|
41
41
|
artifacts/
|
|
42
|
+
performance-results.json
|
|
43
|
+
performance-results/
|
|
42
44
|
cache/
|
|
43
45
|
!.github/actions/cache/
|
|
44
46
|
!.github/actions/cache/action.yml
|
|
45
47
|
typechain/
|
|
46
48
|
coverage.json
|
|
49
|
+
performance-results/
|
|
47
50
|
tests/.bin/
|
|
48
51
|
**/.app/
|
|
49
52
|
|