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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
ci: memoize the database job through run-ci-tests.js (#12297)
[._.]/ Adam Eivya2ed28bd6eb5
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.ymlindex 698bc643b3..8d4c805675 100644--- a/.github/workflows/ci.yml+++ b/.github/workflows/ci.yml@@ -765,17 +765,33 @@ jobs:PG_MAJOR: '17'run: bash scripts/ci-install-db-tools.sh+ # Same contract as the server job's memo steps. The Postgres schema is+ # not visible to a key; scripts/lib/testMemo.js lists the tracked files+ # that build it as db base inputs.+ - name: Restore the test memo+ uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0+ with:+ path: ${{ runner.temp }}/portos-test-memo+ key: test-memo-${{ runner.os }}-${{ runner.arch }}-db-${{ github.run_id }}+ restore-keys: |+ test-memo-${{ runner.os }}-${{ runner.arch }}-db-++ # Full mode: the runner subtracts what the memo verified and records the+ # `⏱ db full suite` duration line itself.- name: Run DB-backed tests (portos_test)- run: |- set +e- start=$(date +%s)- npm run test:db:ci --prefix server- status=$?- elapsed=$(( $(date +%s) - start ))- echo "⏱ server db suite: ${elapsed}s" | tee -a "$GITHUB_STEP_SUMMARY"- exit $status+ id: db-testsenv:PGDATABASE: portos_test+ PORTOS_TEST_MEMO_DIR: ${{ runner.temp }}/portos-test-memo+ CI_TEST_MODE: full+ run: node scripts/run-ci-tests.js db++ - name: Save the test memo+ if: steps.db-tests.outcome == 'success' && github.event_name != 'pull_request'+ uses: actions/cache/save@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0+ with:+ path: ${{ runner.temp }}/portos-test-memo+ key: test-memo-${{ runner.os }}-${{ runner.arch }}-db-${{ github.run_id }}browser:# Owns the registered browser suites; the Linux unit job excludes thatdiff --git a/docs/GITHUB_ACTIONS.md b/docs/GITHUB_ACTIONS.mdindex 8e406807d9..0055a8000c 100644--- a/docs/GITHUB_ACTIONS.md+++ b/docs/GITHUB_ACTIONS.md@@ -981,8 +981,11 @@ Pull requests only read. The scheduled nightly, `workflow_dispatch` and`workflow_call` runs on the default/release branches save a new entry keyedby run id, and only when the test step itself succeeded, so the cache holdsnothing a pull-request branch or a red run wrote and every PR restores the-newest trusted lineage. The DB job does not go through `run-ci-tests.js`-and is not memoized.+newest trusted lineage. The DB job runs the `db` scope+(`node scripts/run-ci-tests.js db`, the serial `portos_test` suite) and keeps+its own `-db-` lineage. The DB config, `server/scripts/init-db.sql` and+`server/scripts/setup-db-test.mjs` are base inputs (a schema change+invalidates every key) and `PGDATABASE` is a keyed env var.**Disable.** `PORTOS_TEST_MEMO=off` turns reads and writes off together andrestores the pre-memo behavior. A test spawned under Vitest (`VITEST` set)diff --git a/scripts/lib/testMemo.js b/scripts/lib/testMemo.jsindex 5654de617f..33ba26f81e 100644--- a/scripts/lib/testMemo.js+++ b/scripts/lib/testMemo.js@@ -71,6 +71,7 @@ export const TEST_MEMO_ENV = Object.freeze(['LC_ALL','MEMORY_BACKEND','NODE_ENV',+ 'PGDATABASE','PORTOS_BROWSER_TEST_PROFILE','PORTOS_CI_BROWSER_OWNER','PORTOS_REQUIRE_BROWSER_SUITES',@@ -125,6 +126,9 @@ export const TEST_MEMO_NEVER = Object.freeze({export const WORKSPACE_BASE_INPUTS = Object.freeze({server: ['server/vitest.config.js', 'server/vitest.setup.js', 'scripts/vitestCiPool.js', 'server/package.json', 'server/package-lock.json'],client: ['client/vitest.config.js', 'client/src/test/setup.js', 'scripts/vitestCiPool.js', 'client/package.json', 'client/package-lock.json'],+ // The Postgres service is not visible to a key; the schema its suites run against is applied from these tracked files.+ db: ['server/vitest.config.db.js', 'server/vitest.setup.js', 'scripts/vitestCiPool.js', 'scripts/lib/dbTestFiles.js', 'server/package.json', 'server/package-lock.json',+ 'server/scripts/init-db.sql', 'server/scripts/setup-db-test.mjs'],});const JS_EXT = /\.(?:[cm]?js|jsx|tsx?)$/;diff --git a/scripts/lib/testMemoRunner.js b/scripts/lib/testMemoRunner.jsindex 2301ec04a2..47a499031a 100644--- a/scripts/lib/testMemoRunner.js+++ b/scripts/lib/testMemoRunner.js@@ -34,7 +34,7 @@ import { join, relative, sep } from 'node:path';import { resolveTestChrome } from '../../server/lib/browserSuiteGate.js';import { WORKSPACE_BASE_INPUTS, createDiskFs, createTestMemoAnalyzer, relatedTests } from './testMemo.js';import { readMemoEntry, readMemoRecord, writeMemoEntry, writeMemoRecord } from './testMemoStore.js';-import { listWorkspaceTests, toRepoPath, toWorkspacePath } from './workspaceTests.js';+import { TEST_SCOPES, listWorkspaceTests, toRepoPath, toWorkspacePath, workspaceOf } from './workspaceTests.js';const uniqueSorted = (values) => [...new Set(values)].sort();const VIRTUAL_MODULE = /(?:^|\/)@vite\/|^\0|^virtual:/;@@ -43,7 +43,7 @@ const VIRTUAL_MODULE = /(?:^|\/)@vite\/|^\0|^virtual:/;* The runner's selection and memo verdicts for one workspace.** @param {object} options- * @param {'server'|'client'} options.scope+ * @param {'server'|'client'|'db'} options.scope* @param {'full'|'files'|'related'} options.mode* @param {string[]} options.repoFiles planner-selected test files (repo-relative)* @param {string[]} options.repoSources changed sources for related mode (repo-relative)@@ -54,7 +54,7 @@ const VIRTUAL_MODULE = /(?:^|\/)@vite\/|^\0|^virtual:/;export function planTestMemo({ scope, mode, repoFiles, repoSources, memoDir, repoRoot, shard = null, env = process.env, platform, arch, nodeVersion, toolVersion = () => null, listTests = listWorkspaceTests, readRecord = readMemoRecord, readEntry = readMemoEntry }) {const workspaceTests = listTests(scope, { repoRoot, platform, env }).map((rel) => toRepoPath(scope, rel));const candidates = mode === 'files' ? uniqueSorted(repoFiles) : uniqueSorted([...workspaceTests, ...repoFiles]);- const options = { fs: createDiskFs(repoRoot, nodeFs, nodePath), workspaceRel: scope, env, platform, arch, nodeVersion, toolVersion };+ const options = { fs: createDiskFs(repoRoot, nodeFs, nodePath), workspaceRel: workspaceOf(scope), env, platform, arch, nodeVersion, toolVersion };const analyzer = createTestMemoAnalyzer(options);const baseClosure = analyzer.workspaceBase(WORKSPACE_BASE_INPUTS[scope]);const analyze = (test, recorded) => analyzer.analyzeTest(test, { baseClosure, recorded });@@ -126,7 +126,7 @@ export function vitestShardRange(count, index, total) {*/export function vitestShardHashInput(rel, { repoRoot, scope, path = nodePath }) {const slash = (value) => value.replace(/\\/g, '/');- const root = path.resolve(repoRoot, scope);+ const root = path.resolve(repoRoot, workspaceOf(scope));return slash(path.resolve(repoRoot, ...rel.split('/'))).slice(root.length);}@@ -302,11 +302,11 @@ export function recordTestMemo({ scope, repoRoot, memoDir, plan, passed, depende/*** Vitest writes the depfile under its config-hashed cache directory- * (`<workspace>/.cache/vitest/vitest/<hash>/dependencies.json`); the newest+ * (`<workspace>/.cache/vitest[-db]/vitest/<hash>/dependencies.json`); the newest* one is this run's. Null when none exists.*/export function findVitestDependenciesFile(repoRoot, scope) {- const root = join(repoRoot, scope, '.cache', 'vitest');+ const root = join(repoRoot, workspaceOf(scope), TEST_SCOPES[scope]?.cacheDir ?? '.cache/vitest');let newest = null;const walk = (dir, depth) => {let entries;diff --git a/scripts/lib/workspaceTests.js b/scripts/lib/workspaceTests.jsindex 231f676636..7e588ee39a 100644--- a/scripts/lib/workspaceTests.js+++ b/scripts/lib/workspaceTests.js@@ -59,7 +59,22 @@ export function browserOwnerExcludes({ env = process.env } = {}) {? BROWSER_SUITES.map(path => path.slice('server/'.length)) : [];}+/**+ * Runner scopes. `db` is the serial `portos_test` suite: it lives in the+ * server workspace but has its own config, npm script and Vitest cache+ * directory (where the recorded-dependency depfile lands).+ */+export const TEST_SCOPES = Object.freeze({+ server: { workspace: 'server', script: 'test:ci', cacheDir: '.cache/vitest' },+ client: { workspace: 'client', script: 'test:ci', cacheDir: '.cache/vitest' },+ db: { workspace: 'server', script: 'test:db:ci', cacheDir: '.cache/vitest-db' },+});++/** The workspace directory a scope runs in (`db` -> `server`); an unknown name passes through. */+export const workspaceOf = (scope) => TEST_SCOPES[scope]?.workspace ?? scope;+export function workspaceTestGlobs(workspace, options = {}) {+ if (workspace === 'db') return { include: [...DB_TEST_INCLUDE], exclude: ['**/node_modules/**', '../lib/slashdo/**'] };if (workspace === 'server') return { include: [...SERVER_TEST_INCLUDE], exclude: [...SERVER_TEST_EXCLUDE, ...browserOwnerExcludes(options)] };if (workspace === 'client') return { include: [...CLIENT_TEST_INCLUDE], exclude: [...CLIENT_TEST_EXCLUDE] };throw new Error(`Unknown workspace: ${workspace}`);@@ -105,7 +120,7 @@ export function listWorkspaceTests(workspace, { repoRoot, platform, env } = {})const { include, exclude } = workspaceTestGlobs(workspace, { platform, env });const includes = include.map(globToRegExp);const excludes = exclude.map(globToRegExp);- const workspaceAbs = join(repoRoot, workspace);+ const workspaceAbs = join(repoRoot, workspaceOf(workspace));const found = [];const walk = (relDir) => {const absDir = relDir === '.' ? workspaceAbs : join(workspaceAbs, ...relDir.split('/'));@@ -123,16 +138,17 @@ export function listWorkspaceTests(workspace, { repoRoot, platform, env } = {})}};for (const root of walkRoots(include)) walk(root);- return found.sort();+ return [...new Set(found)].sort(); // overlapping walk roots (db globs) reach a file twice}/** `lib/x.test.js` in `server` -> `server/lib/x.test.js`; `../scripts/x.test.js` -> `scripts/x.test.js`. */-export function toRepoPath(workspace, workspaceRel) {- return posix.normalize(posix.join(workspace, workspaceRel.split(sep).join('/')));+export function toRepoPath(scope, workspaceRel) {+ return posix.normalize(posix.join(workspaceOf(scope), workspaceRel.split(sep).join('/')));}/** The inverse of `toRepoPath`. */-export function toWorkspacePath(workspace, repoRel) {+export function toWorkspacePath(scope, repoRel) {+ const workspace = workspaceOf(scope);const normalized = repoRel.split(sep).join('/');return normalized.startsWith(`${workspace}/`) ? normalized.slice(workspace.length + 1) : `../${normalized}`;}diff --git a/scripts/lib/workspaceTests.test.js b/scripts/lib/workspaceTests.test.jsindex be17a4984e..713dfc87e8 100644--- a/scripts/lib/workspaceTests.test.js+++ b/scripts/lib/workspaceTests.test.js@@ -7,6 +7,7 @@import { execFileSync } from 'node:child_process';import { join } from 'node:path';import { describe, expect, it } from 'vitest';+import { DB_TEST_INCLUDE } from './dbTestFiles.js';import { globToRegExp, listWorkspaceTests, toRepoPath, toWorkspacePath } from './workspaceTests.js';const REPO_ROOT = join(import.meta.dirname, '..', '..');@@ -49,6 +50,18 @@ describe('workspace test enumeration', () => {expect(listWorkspaceTests('server', { repoRoot: REPO_ROOT, env: {} })).toEqual(vitestListedFiles(REPO_ROOT, 'server'));}, 120_000);+ it('collects the same files as `vitest list --config vitest.config.db.js` for the db scope', () => {+ const listed = execFileSync(process.execPath, [join(REPO_ROOT, 'server', 'node_modules', 'vitest', 'vitest.mjs'),+ 'list', '--filesOnly', '--config', 'vitest.config.db.js', '--configLoader', 'runner'], {+ cwd: join(REPO_ROOT, 'server'), encoding: 'utf8', maxBuffer: 16 * 1024 * 1024,+ env: { ...process.env, PORTOS_CI_TEST_SELECTION: '' },+ }).split('\n').map((line) => line.trim().replace(/^\[[^\]]+\] /, '')).filter(Boolean);+ const found = listWorkspaceTests('db', { repoRoot: REPO_ROOT, env: {} });+ expect(found).toEqual([...new Set(listed)].sort());+ expect(found.length).toBeGreaterThanOrEqual(DB_TEST_INCLUDE.filter((glob) => !glob.includes('*')).length);+ expect(toRepoPath('db', found[0])).toMatch(/^(server|scripts)\//);+ }, 120_000);+it('drops the registered browser suites for an explicit unit owner on every platform', () => {const all = listWorkspaceTests('server', { repoRoot: REPO_ROOT, platform: 'linux', env: {} });expect(all).toContain('lib/filmLook.browser.test.js');diff --git a/scripts/run-ci-tests.js b/scripts/run-ci-tests.jsindex 7f8c8cdaa9..2fa7cf29ca 100644--- a/scripts/run-ci-tests.js+++ b/scripts/run-ci-tests.js@@ -18,6 +18,7 @@ import {memoSummaryLine, passedTestFiles, planTestMemo, readVitestDependencies, recordTestMemo, writeSelectionFile,} from './lib/testMemoRunner.js';import { resolveMemoDir } from './lib/testMemoStore.js';+import { TEST_SCOPES, workspaceOf } from './lib/workspaceTests.js';const repoRoot = join(dirname(fileURLToPath(import.meta.url)), '..');@@ -347,15 +348,16 @@ function runNpmWithReport(scope, script, extraArgs, reportPath, env, abortSignal});}+// `scope` here is the workspace npm runs in; `name` is only the runner scope shown on the duration line.export async function spawnNpm(scope, script, extraArgs, label, {- run = runNpm, diagnose = reportWindowsCrashDiagnostics, onResult = () => {},+ run = runNpm, diagnose = reportWindowsCrashDiagnostics, onResult = () => {}, name = scope,} = {}) {console.log(`Running ${scope} ${label}${extraArgs.length ? ` (${extraArgs.length} selector argument(s))` : ''}.`);const startedAt = Date.now();const profileArgs = process.env.PORTOS_BROWSER_TEST_PROFILE !== undefined? browserTestProfileArgs(process.env.PORTOS_BROWSER_TEST_PROFILE) : [];const result = await run(scope, script, [...extraArgs, ...profileArgs]);- recordVitestDuration(scope, label, startedAt);+ recordVitestDuration(name, label, startedAt);onResult(result);if (result.error) {console.error(result.error.message);@@ -381,7 +383,7 @@ export async function spawnNpm(scope, script, extraArgs, label, {const retryStartedAt = Date.now();// The retry keeps the run's flags (recording, pass-with-no-tests); only the selectors shrink to the crashed file.const retry = await run(scope, script, [selector, ...profileArgs, ...extraArgs.filter(arg => arg.startsWith('--'))]);- recordVitestDuration(scope, `crash retry: ${relPath}`, retryStartedAt);+ recordVitestDuration(name, `crash retry: ${relPath}`, retryStartedAt);onResult(retry);if (!retry.error && retry.status === 0) {if (hasOtherTestFailures(result.output)) {@@ -408,17 +410,19 @@ export async function spawnNpm(scope, script, extraArgs, label, {async function main() {const scope = process.argv[2];- if (!['server', 'client'].includes(scope)) {- console.error('Usage: node scripts/run-ci-tests.js <server|client>');+ if (!Object.hasOwn(TEST_SCOPES, scope)) {+ console.error('Usage: node scripts/run-ci-tests.js <server|client|db>');process.exit(2);}+ const workspace = workspaceOf(scope);+ const { script } = TEST_SCOPES[scope];const mode = process.env.CI_TEST_MODE || 'full';const inputFiles = JSON.parse(process.env.CI_TEST_FILES || '[]');const repoFiles = process.env.PORTOS_BROWSER_TEST_PROFILE !== undefined? browserTestProfileFiles(process.env.PORTOS_BROWSER_TEST_PROFILE, inputFiles) : inputFiles;const repoSources = JSON.parse(process.env.CI_TEST_SOURCES || '[]');- const selectedFiles = repoFiles.map((path) => toRunnerPath(scope, path));+ const selectedFiles = repoFiles.map((path) => toRunnerPath(workspace, path));if (!['full', 'files', 'related'].includes(mode)) {console.error(`Unsupported CI test mode: ${mode}`);@@ -450,7 +454,7 @@ async function main() {// returns.const memoDir = resolveMemoDir();if (mode === 'full' && !memoDir && !shard) {- process.exitCode = await spawnNpm(scope, 'test:ci', [], label);+ process.exitCode = await spawnNpm(workspace, script, [], label, { name: scope });return;}@@ -479,7 +483,8 @@ async function main() {const extraArgs = memoDir ? ['--experimental.recordDependencies'] : [];const passed = [];try {- process.exitCode = await spawnNpm(scope, 'test:ci', extraArgs, label, {+ process.exitCode = await spawnNpm(workspace, script, extraArgs, label, {+ name: scope,onResult: (result) => passed.push(...passedTestFiles(result.fileResults ?? [], {repoRoot, namePattern: process.env.PORTOS_BROWSER_TEST_PROFILE !== undefined, status: result.status, runReport: result.runReport,})),diff --git a/scripts/run-ci-tests.test.js b/scripts/run-ci-tests.test.jsindex 5eda0caf68..37825110ee 100644--- a/scripts/run-ci-tests.test.js+++ b/scripts/run-ci-tests.test.js@@ -108,6 +108,27 @@ it('hands Vitest the selection as a file, sliced by the runner for the matrix sh}}, 150_000);+it('runs the db scope through the server workspace\'s test:db:ci script with the same selection file', () => {+ const fixture = mkdtempSync(join(tmpdir(), 'portos-db-scope-'));+ try {+ installFakeNpm(fixture, REPORTING_NPM);+ const result = spawnSync(process.execPath, [join(import.meta.dirname, 'run-ci-tests.js'), 'db'], {+ encoding: 'utf8', timeout: 120000,+ env: { ...process.env, PATH: `${fixture}${delimiter}${process.env.PATH}`,+ CI_TEST_MODE: 'files', CI_SHARD: '', PORTOS_TEST_MEMO: 'off',+ CI_TEST_FILES: JSON.stringify(['server/services/dbAdmin.db.test.js']),+ GITHUB_ENV: '', GITHUB_STEP_SUMMARY: '' },+ });+ expect(result.status, result.stderr).toBe(0);+ const { args, selection } = JSON.parse(result.stdout.split('\n').find(line => line.startsWith('{"args"')));+ expect(args.slice(0, 5)).toEqual(['run', 'test:db:ci', '--prefix', 'server', '--']);+ expect(selection.include).toEqual(['services/dbAdmin.db.test.js']);+ expect(result.stdout).toMatch(/⏱ db files|⏱ db selected tests/);+ } finally {+ rmSync(fixture, { recursive: true, force: true });+ }+}, 150_000);+describe('test memo through the real runner', () => {it('runs a selected file once, then skips it while its inputs are unchanged, and runs it again when they change', () => {const fixture = mkdtempSync(join(tmpdir(), 'portos-memo-e2e-'));@@ -220,11 +241,12 @@ describe('ci.yml shard wiring', () => {const runners = Object.entries(workflowJobs(WORKFLOW)).filter(([, body]) => /(?:run|watch)-ci-tests\.js/.test(body));it('builds every test runner matrix from the planner and hands each slice to the runner', () => {- expect(runners.map(([id]) => id).sort()).toEqual(['browser', 'client', 'server', 'windows-server']);+ expect(runners.map(([id]) => id).sort()).toEqual(['browser', 'client', 'database', 'server', 'windows-server']);// `browser` runs the planner's cross-workspace browser suites in `files`// mode (#10312): an exact file list has nothing to shard, so it is the one// runner without a matrix.- for (const [id, body] of runners.filter(([id]) => id !== 'browser')) {+ // `database` is one serial job (a single `portos_test`), also without a matrix.+ for (const [id, body] of runners.filter(([id]) => id !== 'browser' && id !== 'database')) {// The fan-out is decided by the impact job, never hardcoded here: a// scoped plan must collapse to one runner, and a job-level `if` cannot// read `matrix` to skip the extra shards itself.@@ -236,6 +258,18 @@ describe('ci.yml shard wiring', () => {}});+ it('memoizes the serial database job through the db scope', () => {+ const body = workflowJobs(WORKFLOW).database;+ const start = body.indexOf('- name: Run DB-backed tests (portos_test)');+ const step = body.slice(start, body.indexOf('- name:', start + 1));+ expect(step).toContain('run: node scripts/run-ci-tests.js db');+ expect(step).toContain('CI_TEST_MODE: full');+ expect(step).toContain('PGDATABASE: portos_test');+ expect(step).toContain('PORTOS_TEST_MEMO_DIR:');+ expect(body).toMatch(/key: test-memo-[^\n]*-db-\$\{\{ github\.run_id \}\}/);+ expect(body).toContain("steps.db-tests.outcome == 'success' && github.event_name != 'pull_request'");+ });+it('runs the planner-selected browser suites on their own job with prerequisites required (#10312)', () => {const body = workflowJobs(WORKFLOW).browser;expect(body).toContain("if: needs.impact.outputs.browser_files != '[]'");diff --git a/server/vitest.config.db.js b/server/vitest.config.db.jsindex 163cfebfa7..38772cdade 100644--- a/server/vitest.config.db.js+++ b/server/vitest.config.db.js@@ -1,5 +1,6 @@import { defineConfig } from 'vitest/config';import { fileURLToPath } from 'node:url';+import { applyRunnerSelection, vitestRunnerSelection } from '../scripts/vitestCiPool.js';import { DB_TEST_INCLUDE } from '../scripts/lib/dbTestFiles.js';import { bootstrapVitestTempRoot } from '../scripts/lib/vitestTempRoot.js';@@ -37,7 +38,7 @@ export { DB_TEST_INCLUDE } from '../scripts/lib/dbTestFiles.js';* suite silently never runs until it's listed. db.guards.test.js fails if a* checkHealth consumer is left out.*/-export default defineConfig({+const config = defineConfig({// Anchor to the config, even when --root or the caller cwd differs.cacheDir: fileURLToPath(new URL('./.cache/vitest-db', import.meta.url)),test: {@@ -60,6 +61,9 @@ export default defineConfig({MAC_CHROMIUM_TMPDIR: RUN_TEMP_ROOT,// Keeps spawned npm from writing debug logs into the developer's ~/.npm/_logs.npm_config_logs_max: '0',+ // The runner's selection and memo directory are for THIS Vitest process only.+ PORTOS_CI_TEST_SELECTION: '',+ PORTOS_TEST_MEMO_DIR: '',},include: DB_TEST_INCLUDE,exclude: [@@ -68,3 +72,7 @@ export default defineConfig({],},});++// The CI runner's explicit selection (files the test memo has not verified) arrives as a file.+config.test = applyRunnerSelection(config.test, vitestRunnerSelection());+export default config;