Beautiful, AI-native markdown IDE and LLM wiki
TypeScript 2nd-brainagent-skillsclaudecodexcompany-braindocsknowledge-knowledge-baseknowledge-managementllmllm-wikillm-wiki-karpathymarkdownmarkdown-editormdmdxnotespkmsecond-brainskills
Fail tests on a blocked child process instead of hanging (#5860)
Nick Gomezff293dd5acf5
diff --git a/.github/scripts/child-tripwire.test-helper.mjs b/.github/scripts/child-tripwire.test-helper.mjsnew file mode 100644index 000000000..5309103c5--- /dev/null+++ b/.github/scripts/child-tripwire.test-helper.mjs@@ -0,0 +1,86 @@+import { execFileSync as unboundedExecFileSync } from 'node:child_process';+import { performance } from 'node:perf_hooks';++export const CHILD_TRIPWIRE_CODE = 'ECHILD_TRIPWIRE';+export const CHILD_TRIPWIRE_OPEN_CODE = 'ECHILD_TRIPWIRE_OPEN';+export const STRUCTURAL_FOLLOW_UP =+ 'Structural follow-up: run these children through an asynchronous spawn whose watchdog bounds the ' ++ "time without output and stops the child's own process group, which a synchronous spawn cannot do.";++const INTERIM_CHILD_TRIPWIRE_MS = 20_000;+const COMMAND_LINE_CHARS = 400;+const OUTPUT_TAIL_CHARS = 2000;++const commandLineOf = (file, args) => {+ const line = [file, ...args].map(String).join(' ');+ return line.length > COMMAND_LINE_CHARS ? `${line.slice(0, COMMAND_LINE_CHARS)}...` : line;+};++const tailOf = (output) => {+ if (output === undefined || output === null) return String(output);+ const text = Buffer.isBuffer(output) ? output.toString('utf8') : String(output);+ return JSON.stringify(+ text.length > OUTPUT_TAIL_CHARS ? `...${text.slice(-OUTPUT_TAIL_CHARS)}` : text,+ );+};++const howItEnded = (error) =>+ error.status === null+ ? `It was killed (pid ${error.pid}, signal=${error.signal}).`+ : `It had itself exited with status ${error.status}, but a descendant kept its stdout or stderr ` ++ 'open, so this harness closed its own ends of those pipes.';++export function createChildTripwire({ boundMs = INTERIM_CHILD_TRIPWIRE_MS } = {}) {+ let blockedChild = null;+ return function execFileSync(file, args, options) {+ const argv = Array.isArray(args) ? args : [];+ const execOptions = (Array.isArray(args) || args == null ? options : args) ?? {};+ const commandLine = commandLineOf(file, argv);+ if ('timeout' in execOptions || 'killSignal' in execOptions) {+ throw new TypeError(+ `This execFileSync owns the timeout and the kill signal, and \`${commandLine}\` passed its ` ++ 'own. Use node:child_process directly for a child whose expiry the test itself observes.',+ );+ }+ if (blockedChild !== null) {+ const refusal = new Error(+ `Not starting \`${commandLine}\`: an earlier child in this test file, ${blockedChild}, was ` ++ `still running at the ${boundMs} ms tripwire. A host that blocks one child tends to block the ` ++ 'next (the bash here-document deadlock blocked every here-document over 512 bytes while it ' ++ `lasted), and each further child would cost another ${boundMs} ms and could leave another ` ++ 'blocked descendant behind, so this file starts no more children. Diagnose the first ' ++ 'failure, then run the tests again.',+ );+ refusal.code = CHILD_TRIPWIRE_OPEN_CODE;+ throw refusal;+ }+ const started = performance.now();+ try {+ return unboundedExecFileSync(file, argv, {+ ...execOptions,+ timeout: boundMs,+ killSignal: 'SIGKILL',+ });+ } catch (error) {+ if (error?.code !== 'ETIMEDOUT') throw error;+ const elapsedMs = Math.round(performance.now() - started);+ blockedChild = `\`${commandLine}\` (pid ${error.pid})`;+ const diagnosis = new Error(+ `\`${commandLine}\` was still holding this test ${elapsedMs} ms after it started, past the ` ++ `interim ${boundMs} ms child tripwire, so the test failed instead of the tier waiting on it ` ++ `forever. ${howItEnded(error)} Captured before the bound: stdout=${tailOf(error.stdout)} ` ++ `stderr=${tailOf(error.stderr)}. No healthy child in this file comes near this bound, so one ` ++ 'still running here is blocked rather than slow. The case the tripwire was added for is ' ++ 'Homebrew bash 5.3.9, whose here-document write deadlocks when the kernel hands it a 512-byte ' ++ 'pipe; bash 5.3.16 and later fix it, so check `bash --version` first. Descendants were not ' ++ 'signalled, because this harness signals only the process it spawned, so any it left blocked ' ++ 'keeps running after this test. The bound measures elapsed time because a synchronous spawn ' ++ `cannot observe progress. ${STRUCTURAL_FOLLOW_UP}`,+ );+ diagnosis.code = CHILD_TRIPWIRE_CODE;+ throw diagnosis;+ }+ };+}++export const execFileSync = createChildTripwire();diff --git a/.github/scripts/child-tripwire.test.mjs b/.github/scripts/child-tripwire.test.mjsnew file mode 100644index 000000000..ffa29883c--- /dev/null+++ b/.github/scripts/child-tripwire.test.mjs@@ -0,0 +1,181 @@+import { execFileSync as unboundedExecFileSync, spawnSync } from 'node:child_process';+import { dirname, join } from 'node:path';+import { fileURLToPath, pathToFileURL } from 'node:url';+import { describe, expect, test } from 'vitest';+import {+ CHILD_TRIPWIRE_CODE,+ CHILD_TRIPWIRE_OPEN_CODE,+ createChildTripwire,+ STRUCTURAL_FOLLOW_UP,+} from './child-tripwire.test-helper.mjs';++const HELPER_URL = pathToFileURL(+ join(dirname(fileURLToPath(import.meta.url)), 'child-tripwire.test-helper.mjs'),+).href;+const BOUND_MS = 2_000;+const SCENARIO_GUARD_MS = 25_000;+const OWNER_ENV = 'CHILD_TRIPWIRE_TEST_OWNER_PID';+const WHILE_THE_TEST_LIVES = `while kill -0 "$${OWNER_ENV}" 2>/dev/null; do sleep 0.2; done`;++const IGNORES_TERM = ['bash', ['-c', `trap '' TERM; echo ready; ${WHILE_THE_TEST_LIVES}`]];+const WAITS_ON_A_DESCENDANT_HOLDING_ITS_PIPE = [+ 'bash',+ ['-c', `(echo ready; ${WHILE_THE_TEST_LIVES}); echo unreachable`],+];+const EXITS_WHILE_A_DESCENDANT_HOLDS_ITS_PIPE = [+ 'bash',+ ['-c', `(${WHILE_THE_TEST_LIVES}) & echo ready; exit 0`],+];+const QUICK = [process.execPath, ['-e', '']];++function throughOneTripwire(calls) {+ const script =+ `import { createChildTripwire } from ${JSON.stringify(HELPER_URL)};\n` ++ `const execFileSync = createChildTripwire({ boundMs: ${BOUND_MS} });\n` ++ 'const outcomes = [];\n' ++ `for (const [file, args] of ${JSON.stringify(calls)}) {\n` ++ ' const started = performance.now();\n' ++ ' try {\n' ++ " const stdout = execFileSync(file, args, { encoding: 'utf8' });\n" ++ ' outcomes.push({ ms: performance.now() - started, returned: { stdout } });\n' ++ ' } catch (error) {\n' ++ ' outcomes.push({ ms: performance.now() - started, thrown: { code: error.code, ' ++ "message: error.message, numericStatus: typeof error.status === 'number' } });\n" ++ ' }\n' ++ '}\n' ++ 'process.stdout.write(JSON.stringify(outcomes));\n';+ const scenario = spawnSync(process.execPath, ['--input-type=module', '-e', script], {+ encoding: 'utf8',+ env: { ...process.env, [OWNER_ENV]: String(process.pid) },+ timeout: SCENARIO_GUARD_MS,+ killSignal: 'SIGKILL',+ });+ expect(+ scenario.error,+ `the scenario was still running ${SCENARIO_GUARD_MS} ms in, so a ${BOUND_MS} ms tripwire left ` ++ 'its caller waiting on a blocked child, which is the hang the tripwire exists to end: ' ++ `stderr=${scenario.stderr}`,+ ).toBeUndefined();+ expect(scenario.status, `the scenario process failed before reporting: ${scenario.stderr}`).toBe(+ 0,+ );+ return JSON.parse(scenario.stdout);+}++function expectTripped(outcome, why) {+ expect(outcome.thrown?.code, `${why}: ${JSON.stringify(outcome)}`).toBe(CHILD_TRIPWIRE_CODE);+ expect(+ outcome.thrown.message,+ 'the child never reported ready before the bound fired, so this row did not drive the shape it ' ++ 'names, and the diagnosis also has to carry what the child printed',+ ).toContain('stdout="ready');+ expect(+ outcome.thrown.message,+ 'an interim bound has to name its structural follow-up wherever it fires',+ ).toContain(STRUCTURAL_FOLLOW_UP);+ expect(+ outcome.thrown.numericStatus,+ 'a caller that keeps any execFileSync error carrying a numeric status as a verdict would read ' ++ 'the diagnosis as an exit code',+ ).toBe(false);+}++describe('a child that blocks fails its test at the bound instead of holding the tier', () => {+ test('a child that ignores SIGTERM fails its caller at the bound, not when it chooses to exit', () => {+ const [outcome] = throughOneTripwire([IGNORES_TERM]);+ expectTripped(+ outcome,+ 'execFileSync waits for its child to exit after the kill, so a SIGTERM the child ignores leaves ' ++ 'the caller waiting as long as the child chooses, and only SIGKILL ends it at the bound',+ );+ expect(outcome.thrown.message).toContain('signal=SIGKILL');+ });++ test('a child waiting on a descendant that holds its stdout fails its caller at the bound', () => {+ const [outcome] = throughOneTripwire([WAITS_ON_A_DESCENDANT_HOLDING_ITS_PIPE]);+ expectTripped(+ outcome,+ 'a child blocked on its own descendant is the shape of the bash here-document deadlock, and the ' ++ 'descendant outlives the killed child while it holds the pipe the caller reads',+ );+ expect(outcome.thrown.message).toMatch(/It was killed \(pid \d+, signal=SIGKILL\)/);+ });++ test('a child that exits while a descendant holds its stdout is not reported as its exit status', () => {+ const [outcome] = throughOneTripwire([EXITS_WHILE_A_DESCENDANT_HOLDS_ITS_PIPE]);+ expectTripped(+ outcome,+ 'execFileSync throws ETIMEDOUT with status 0 here, so a caller keeping errors that carry a ' ++ 'status takes a cut-off capture as the verdict of a clean run',+ );+ expect(outcome.thrown.message).toMatch(+ /exited with status 0, but a descendant kept its stdout or stderr\s+open/,+ );+ });++ test('after one child trips the bound, that tripwire starts no further child', () => {+ const [first, next] = throughOneTripwire([IGNORES_TERM, QUICK]);+ expectTripped(+ first,+ 'the first child has to trip the bound for this row to say anything about the next',+ );+ expect(+ next.thrown?.code,+ 'a host that blocked one child is likely to block the next, and every further child costs ' ++ `another bound: ${JSON.stringify(next)}`,+ ).toBe(CHILD_TRIPWIRE_OPEN_CODE);+ expect(next.ms, 'the refusal waited for a bound instead of refusing at once').toBeLessThan(+ BOUND_MS,+ );+ expect(next.thrown.message, 'the refusal has to name the earlier child it acts on').toContain(+ `\`${IGNORES_TERM[0]} ${IGNORES_TERM[1].join(' ')}\` (pid `,+ );+ expect(+ createChildTripwire()(...QUICK, { encoding: 'utf8' }),+ 'a separate tripwire does not inherit a refusal it never tripped',+ ).toBe('');+ });+});++describe('a child that finishes is unaffected by the tripwire', () => {+ const outcomeOf = (run) => {+ try {+ return { returned: run() };+ } catch (error) {+ return {+ thrown: {+ code: error.code,+ status: error.status,+ stdout: error.stdout,+ stderr: error.stderr,+ },+ };+ }+ };++ test('each child that finishes returns or throws exactly what node:child_process gives', () => {+ const execFileSync = createChildTripwire();+ const cases = [+ ['bash', ['-c', 'printf out; printf err >&2; exit 0']],+ ['bash', ['-c', 'printf out; printf err >&2; exit 7']],+ ['definitely-not-an-installed-command-7f3a', []],+ ];+ for (const [file, args] of cases) {+ const options = { encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] };+ expect(+ outcomeOf(() => execFileSync(file, args, options)),+ `the tripwire changed the outcome of \`${file} ${args.join(' ')}\` although it finished`,+ ).toEqual(outcomeOf(() => unboundedExecFileSync(file, args, options)));+ }+ });++ test('a caller cannot hand the tripwire its own timeout or kill signal', () => {+ const execFileSync = createChildTripwire();+ for (const options of [{ timeout: 1 }, { killSignal: 'SIGTERM' }]) {+ expect(+ () => execFileSync(...QUICK, options),+ `a caller's ${JSON.stringify(options)} would otherwise be overwritten without notice`,+ ).toThrow(/owns the timeout and the kill signal/);+ }+ });+});diff --git a/.github/scripts/release-cascade-shape.test.mjs b/.github/scripts/release-cascade-shape.test.mjsindex b3c785403..29ff406a9 100644--- a/.github/scripts/release-cascade-shape.test.mjs+++ b/.github/scripts/release-cascade-shape.test.mjs@@ -1,5 +1,4 @@// biome-ignore-all lint/suspicious/noTemplateCurlyInString: shell and GitHub expression fixtures must remain literal.-import { execFileSync } from 'node:child_process';import { createHash } from 'node:crypto';import { existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';import { createRequire } from 'node:module';@@ -9,6 +8,7 @@ import { fileURLToPath } from 'node:url';import { afterAll, describe, expect, test } from 'vitest';import { parse } from 'yaml';import { buildSlackPayload } from './build-smoke-alert-payload.mjs';+import { execFileSync } from './child-tripwire.test-helper.mjs';import { selectPromotion } from './select-beta-to-promote.mjs';import { smokePackagedDmg, VERDICT } from './smoke-packaged-dmg.mjs';import { credentialReasons, holdsCredential } from './workflow-credentials.test-helper.mjs';diff --git a/knip.config.ts b/knip.config.tsindex d3638888e..65f0e8807 100644--- a/knip.config.ts+++ b/knip.config.ts@@ -147,15 +147,11 @@ export default {'packages/server': {entry: ['src/**/*.test.ts', 'src/parse-worker.ts'],project: 'src/**',- ignoreDependencies: [- '@types/shell-quote',- ],+ ignoreDependencies: ['@types/shell-quote'],},'packages/cli': {entry: ['src/**/*.test.ts', 'scripts/*.ts', 'tests/**/*.ts', 'src/parse-worker.ts'],- ignoreDependencies: [- 'yjs',- ],+ ignoreDependencies: ['yjs'],},'packages/desktop': {entry: [diff --git a/packages/app/tests/integration/bug3-source-mode-writeback.test.ts b/packages/app/tests/integration/bug3-source-mode-writeback.test.tsindex 4549c74b3..a50b00123 100644--- a/packages/app/tests/integration/bug3-source-mode-writeback.test.ts+++ b/packages/app/tests/integration/bug3-source-mode-writeback.test.ts@@ -1,4 +1,3 @@-import { readFileSync } from 'node:fs';import { join } from 'node:path';import { setTimeout as wait } from 'node:timers/promises';diff --git a/packages/app/tests/integration/indented-jsx-byte-sacred.test.ts b/packages/app/tests/integration/indented-jsx-byte-sacred.test.tsindex 4fc2c11f9..6fcdc23f2 100644--- a/packages/app/tests/integration/indented-jsx-byte-sacred.test.ts+++ b/packages/app/tests/integration/indented-jsx-byte-sacred.test.ts@@ -1,4 +1,3 @@-import { setTimeout as wait } from 'node:timers/promises';import { normalizeBridge } from '@inkeep/open-knowledge-core';import { updateYFragment } from '@tiptap/y-tiptap';diff --git a/packages/app/tests/integration/interior-edit-freshness.test.ts b/packages/app/tests/integration/interior-edit-freshness.test.tsindex 9f2f6feb6..40b5cdfd6 100644--- a/packages/app/tests/integration/interior-edit-freshness.test.ts+++ b/packages/app/tests/integration/interior-edit-freshness.test.ts@@ -1,4 +1,3 @@-import { setTimeout as wait } from 'node:timers/promises';import { updateYFragment, yXmlFragmentToProseMirrorRootNode } from '@tiptap/y-tiptap';import { afterAll, beforeAll, describe, expect, test } from 'vitest';diff --git a/packages/app/tests/integration/keystroke-cadence-danger-space.test.ts b/packages/app/tests/integration/keystroke-cadence-danger-space.test.tsindex 2a085742b..93666af70 100644--- a/packages/app/tests/integration/keystroke-cadence-danger-space.test.ts+++ b/packages/app/tests/integration/keystroke-cadence-danger-space.test.ts@@ -1,4 +1,3 @@-import { setTimeout as wait } from 'node:timers/promises';import { updateYFragment, yXmlFragmentToProseMirrorRootNode } from '@tiptap/y-tiptap';import { afterAll, beforeAll, describe, expect, test } from 'vitest';diff --git a/packages/app/tests/integration/producer-guard-wiring.test.ts b/packages/app/tests/integration/producer-guard-wiring.test.tsindex 878d5196b..867598d34 100644--- a/packages/app/tests/integration/producer-guard-wiring.test.ts+++ b/packages/app/tests/integration/producer-guard-wiring.test.ts@@ -1,4 +1,3 @@-import { setTimeout as wait } from 'node:timers/promises';import { updateYFragment, yXmlFragmentToProseMirrorRootNode } from '@tiptap/y-tiptap';import { afterAll, beforeAll, describe, expect, test } from 'vitest';diff --git a/packages/app/tests/integration/qa-039-reconciliation-counters-http.test.ts b/packages/app/tests/integration/qa-039-reconciliation-counters-http.test.tsindex 4ada3cf26..0970ac12f 100644--- a/packages/app/tests/integration/qa-039-reconciliation-counters-http.test.ts+++ b/packages/app/tests/integration/qa-039-reconciliation-counters-http.test.ts@@ -1,4 +1,3 @@-import { randomUUID } from 'node:crypto';import { afterEach, describe, expect, test } from 'vitest';import { agentWriteMd, createTestServer, type TestServer } from './test-harness';diff --git a/packages/app/tests/integration/qa-045-restore-after-gc.test.ts b/packages/app/tests/integration/qa-045-restore-after-gc.test.tsindex cbe2b705f..b632185e1 100644--- a/packages/app/tests/integration/qa-045-restore-after-gc.test.ts+++ b/packages/app/tests/integration/qa-045-restore-after-gc.test.ts@@ -1,4 +1,3 @@-import { execFileSync } from 'node:child_process';import { randomUUID } from 'node:crypto';import { join } from 'node:path';diff --git a/packages/app/tests/integration/qa-050-rollback-undo-shipped-path.test.ts b/packages/app/tests/integration/qa-050-rollback-undo-shipped-path.test.tsindex 89bc9b4d9..c530ce4ba 100644--- a/packages/app/tests/integration/qa-050-rollback-undo-shipped-path.test.ts+++ b/packages/app/tests/integration/qa-050-rollback-undo-shipped-path.test.ts@@ -1,4 +1,3 @@-import { randomUUID } from 'node:crypto';import { afterAll, afterEach, beforeAll, describe, expect, test } from 'vitest';import type * as Y from 'yjs';diff --git a/packages/app/tests/integration/undo-after-rollback.test.ts b/packages/app/tests/integration/undo-after-rollback.test.tsindex a78aeb0e4..d3767adc9 100644--- a/packages/app/tests/integration/undo-after-rollback.test.ts+++ b/packages/app/tests/integration/undo-after-rollback.test.ts@@ -1,4 +1,3 @@-import { ROLLBACK_ORIGIN } from '@inkeep/open-knowledge-server';import { afterAll, beforeAll, describe, expect, test } from 'vitest';import * as Y from 'yjs';diff --git a/packages/app/tests/integration/undo-recycle-reset.test.ts b/packages/app/tests/integration/undo-recycle-reset.test.tsindex e7cc69c3f..b076f955c 100644--- a/packages/app/tests/integration/undo-recycle-reset.test.ts+++ b/packages/app/tests/integration/undo-recycle-reset.test.ts@@ -1,4 +1,3 @@-import { writeFileSync } from 'node:fs';import { join } from 'node:path';import { setTimeout as wait } from 'node:timers/promises';diff --git a/packages/app/tests/integration/undo-rederive-survival.test.ts b/packages/app/tests/integration/undo-rederive-survival.test.tsindex 9ddcd0730..7935e35f8 100644--- a/packages/app/tests/integration/undo-rederive-survival.test.ts+++ b/packages/app/tests/integration/undo-rederive-survival.test.ts@@ -1,4 +1,3 @@-import { prependFrontmatter } from '@inkeep/open-knowledge-core';import { Plugin, PluginKey } from '@tiptap/pm/state';import { updateYFragment, yXmlFragmentToProseMirrorRootNode } from '@tiptap/y-tiptap';diff --git a/packages/core/src/bridge/compose.ts b/packages/core/src/bridge/compose.tsindex 2a597ec3d..f4cc8a286 100644--- a/packages/core/src/bridge/compose.ts+++ b/packages/core/src/bridge/compose.ts@@ -1,4 +1,3 @@-import { prependFrontmatter, stripFrontmatter } from '../extensions/frontmatter.ts';export type ComposeAdjustment = 'none' | 'body' | 'frontmatter' | 'unresolved';diff --git a/packages/core/src/bridge/doc-boundary-space.ts b/packages/core/src/bridge/doc-boundary-space.tsindex 01ef76a9f..8fb428b90 100644--- a/packages/core/src/bridge/doc-boundary-space.ts+++ b/packages/core/src/bridge/doc-boundary-space.ts@@ -1,4 +1,3 @@-import { FRONTMATTER_RE, stripFrontmatter } from '../extensions/frontmatter.ts';import { carriedEdgeEmpties, type DocEdgeEmpties } from '../markdown/doc-edge-blank-runs.ts';diff --git a/packages/core/src/markdown/comment-promoter.ts b/packages/core/src/markdown/comment-promoter.tsindex fb0ba5a7c..fc267cbea 100644--- a/packages/core/src/markdown/comment-promoter.ts+++ b/packages/core/src/markdown/comment-promoter.ts@@ -1,4 +1,3 @@-import type { Nodes, Paragraph, PhrasingContent, Root, RootContent, Text } from 'mdast';import { SKIP, visit } from 'unist-util-visit';import type { VFile } from 'vfile';diff --git a/packages/core/src/markdown/merged-walker.ts b/packages/core/src/markdown/merged-walker.tsindex fa3cfcd86..bfd7e3109 100644--- a/packages/core/src/markdown/merged-walker.ts+++ b/packages/core/src/markdown/merged-walker.ts@@ -1,4 +1,3 @@-import type { Nodes, Parent, Root } from 'mdast';import { SKIP, visit } from 'unist-util-visit';import type { VFile } from 'vfile';diff --git a/packages/core/src/markdown/safe-url.ts b/packages/core/src/markdown/safe-url.tsindex 8be632ba4..4d3250d3a 100644--- a/packages/core/src/markdown/safe-url.ts+++ b/packages/core/src/markdown/safe-url.ts@@ -1,4 +1,3 @@-export const SAFE_URL_SCHEMES = ['https', 'http', 'mailto', 'tel', 'ftp', 'sms'] as const;const SCHEME_ALT = SAFE_URL_SCHEMES.map((s) => `${s}:`).join('|');