Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

Pre-commit hook runs pregate and never publishes the client build

[._.]/ Adam Eivy506ab4178236
diff --git a/.githooks/pre-commit b/.githooks/pre-commit
index b7bec19509..7a14e1f3c7 100755
--- a/.githooks/pre-commit
+++ b/.githooks/pre-commit
@@ -10,50 +10,12 @@ if [ -z "$STAGED" ]; then
exit 0
fi
-run_client=false
-run_server=false
-
-while IFS= read -r file; do
- [ -z "$file" ] && continue
- case "$file" in
- client/*)
- run_client=true
- ;;
- server/*)
- run_server=true
- ;;
- package.json|package-lock.json)
- run_client=true
- run_server=true
- ;;
- esac
-done <<< "$STAGED"
-
-if [ "$run_server" = false ] && [ "$run_client" = false ]; then
- exit 0
-fi
-
-printf -- "\n🔎 pre-commit: validating staged PortOS changes\n"
-
-if [ "$run_server" = true ]; then
- printf -- "- running server tests...\n"
- # NODE_ENV=test is what points the suite at the file storage backend instead
- # of the developer's live Postgres. server/vitest.config.js now forces it, but
- # a commit fired from a PortOS-spawned shell inherits NODE_ENV=development
- # from PM2, so state it here too — the hook must be correct on its own (#4554).
- # Match the supported local suite budget, preserving a smaller caller cap.
- case "${PORTOS_PREGATE_MAX_WORKERS:-}" in
- [1-4]) ;;
- *) PORTOS_PREGATE_MAX_WORKERS=4 ;;
- esac
- PORTOS_PREGATE_MAX_WORKERS="$PORTOS_PREGATE_MAX_WORKERS" NODE_ENV=test npm test --prefix server
-fi
-
-if [ "$run_client" = true ]; then
- printf -- "- running client lint...\n"
- npm run lint --prefix client
- printf -- "- running client build...\n"
- npm run build --prefix client
-fi
+# The same planner and runners CI uses (scripts/pregate.js), bounded to the
+# changed files and the always-run guards. The hook deliberately runs nothing
+# else: it used to run the whole server suite and `npm run build --prefix
+# client`, which publishes the branch's build into the dist the running
+# install serves (#12045). Validation never swaps what `npm start` serves.
+printf -- "\n🔎 pre-commit: running pregate on the staged PortOS changes\n"
+node scripts/pregate.js
printf -- "✅ pre-commit checks passed\n"
diff --git a/scripts/precommitHook.test.js b/scripts/precommitHook.test.js
new file mode 100644
index 0000000000..63a3322d43
--- /dev/null
+++ b/scripts/precommitHook.test.js
@@ -0,0 +1,62 @@
+import { afterEach, expect, it } from 'vitest';
+import { mkdtempSync, writeFileSync, chmodSync, readFileSync, rmSync, existsSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join, delimiter } from 'node:path';
+import { fileURLToPath } from 'node:url';
+import { spawnSync } from 'node:child_process';
+
+const hook = fileURLToPath(new URL('../.githooks/pre-commit', import.meta.url));
+const roots = [];
+afterEach(() => { for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true }); });
+const shellTest = process.platform === 'win32' ? it.skip : it;
+
+// Fixture PATH: `git` answers rev-parse with the fixture root and reports the
+// staged list from FIXTURE_STAGED; `node` records its argv; `npm` records that
+// it was reached at all. The hook must reach pregate through node and nothing
+// through npm — `npm run build --prefix client` publishes the branch's client
+// build into the dist the running install serves (#12045).
+function fixture() {
+ const root = mkdtempSync(join(tmpdir(), 'hook-pregate-'));
+ roots.push(root);
+ for (const [name, text] of Object.entries({
+ git: '#!/bin/bash\nif [ "$1" = rev-parse ]; then printf "%s\\n" "$FIXTURE_ROOT"; else printf "%s\\n" "$FIXTURE_STAGED"; fi\n',
+ node: '#!/bin/bash\nprintf "%s" "$*" > "$FIXTURE_ROOT/node-args"\nexit "${FIXTURE_EXIT:-0}"\n',
+ npm: '#!/bin/bash\nprintf "%s" "$*" > "$FIXTURE_ROOT/npm-args"\nexit 1\n',
+ })) {
+ writeFileSync(join(root, name), text);
+ chmodSync(join(root, name), 0o755);
+ }
+ return root;
+}
+
+function runHook(root, env) {
+ return spawnSync('bash', [hook], { encoding: 'utf8', env: {
+ ...process.env, PATH: `${root}${delimiter}${process.env.PATH}`, FIXTURE_ROOT: root, ...env,
+ } });
+}
+
+shellTest('runs pregate for staged changes and propagates its exit code', () => {
+ for (const [exitCode, staged] of [[0, 'server/example.js'], [7, 'client/src/App.jsx'], [0, 'docs/README.md']]) {
+ const root = fixture();
+ const result = runHook(root, { FIXTURE_STAGED: staged, FIXTURE_EXIT: String(exitCode) });
+ expect(result.status, result.stderr).toBe(exitCode);
+ expect(readFileSync(join(root, 'node-args'), 'utf8')).toBe('scripts/pregate.js');
+ expect(existsSync(join(root, 'npm-args')), 'the hook reached npm').toBe(false);
+ expect(result.stdout.includes('checks passed')).toBe(exitCode === 0);
+ }
+});
+
+shellTest('does nothing when nothing is staged', () => {
+ const root = fixture();
+ const result = runHook(root, { FIXTURE_STAGED: '' });
+ expect(result.status, result.stderr).toBe(0);
+ expect(existsSync(join(root, 'node-args'))).toBe(false);
+ expect(existsSync(join(root, 'npm-args'))).toBe(false);
+});
+
+it('never publishes the client build or runs a workspace suite directly', () => {
+ // Comment lines may name the old commands as history; only executable lines count.
+ const source = readFileSync(hook, 'utf8').split('\n').filter((line) => !/^\s*#/.test(line)).join('\n');
+ expect(source).not.toMatch(/publish-client-build|npm run build|npm test|vitest/);
+ expect(source).toContain('node scripts/pregate.js');
+});
diff --git a/scripts/precommitWorkerCap.test.js b/scripts/precommitWorkerCap.test.js
deleted file mode 100644
index 2418b5bc47..0000000000
--- a/scripts/precommitWorkerCap.test.js
+++ /dev/null
@@ -1,33 +0,0 @@
-import { afterEach, expect, it } from 'vitest';
-import { mkdtempSync, writeFileSync, chmodSync, readFileSync, rmSync } from 'node:fs';
-import { tmpdir } from 'node:os';
-import { join, delimiter } from 'node:path';
-import { fileURLToPath } from 'node:url';
-import { spawnSync } from 'node:child_process';
-
-const hook = fileURLToPath(new URL('../.githooks/pre-commit', import.meta.url));
-const roots = [];
-afterEach(() => { for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true }); });
-const shellTest = process.platform === 'win32' ? it.skip : it;
-shellTest('caps the actual hook suite, preserves lower budgets and propagates test failure', () => {
- const root = mkdtempSync(join(tmpdir(), 'hook-budget-'));
- roots.push(root);
- const marker = join(root, 'invocation');
- for (const [name, text] of Object.entries({
- git: '#!/bin/bash\nif [ "$1" = rev-parse ]; then printf "%s\\n" "$FIXTURE_ROOT"; else printf "server/example.js\\n"; fi\n',
- npm: '#!/bin/bash\nprintf "%s %s %s" "$PORTOS_PREGATE_MAX_WORKERS" "$NODE_ENV" "$*" > "$FIXTURE_MARKER"\nexit "${FIXTURE_EXIT:-0}"\n',
- })) {
- writeFileSync(join(root, name), text);
- chmodSync(join(root, name), 0o755);
- }
- for (const [budget, expected, exitCode] of [['', '4', 0], ['2', '2', 0], ['12', '4', 0], ['invalid', '4', 7]]) {
- const result = spawnSync('bash', [hook], { encoding: 'utf8', env: {
- ...process.env, PATH: `${root}${delimiter}${process.env.PATH}`,
- FIXTURE_ROOT: root, FIXTURE_MARKER: marker, FIXTURE_EXIT: String(exitCode),
- PORTOS_PREGATE_MAX_WORKERS: budget, NODE_ENV: 'development',
- } });
- expect(result.status, result.stderr).toBe(exitCode);
- expect(readFileSync(marker, 'utf8')).toBe(`${expected} test test --prefix server`);
- if (exitCode) expect(result.stdout).not.toContain('checks passed');
- }
-});