Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

Preserve terminal PID marker row boundaries (PRD-9986) (#7652)

Nick Gomezfbcc218226f1
diff --git a/knip.config.ts b/knip.config.ts
index fac2580fe..e04bf5228 100644
--- a/knip.config.ts
+++ b/knip.config.ts
@@ -157,15 +157,11 @@ export default {
'src/shadow-fixture-launch.test-helper.mjs',
],
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.ts
index 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.ts
index 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.ts
index 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.ts
index 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.ts
index 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.ts
index 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.ts
index 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.ts
index 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.ts
index 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.ts
index 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.ts
index 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.ts
index 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.ts
index ffd8fce06..e7e63f43d 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.ts
index 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.ts
index ae8cb390b..861003fb4 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.ts
index 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('|');
diff --git a/packages/desktop/tests/smoke/_helpers/terminal-smoke-shell.ts b/packages/desktop/tests/smoke/_helpers/terminal-smoke-shell.ts
index 945ee956d..da086f13c 100644
--- a/packages/desktop/tests/smoke/_helpers/terminal-smoke-shell.ts
+++ b/packages/desktop/tests/smoke/_helpers/terminal-smoke-shell.ts
@@ -53,7 +53,7 @@ export function terminalSmokeShellCommands(
oscTitle: (title, marker) =>
`[Console]::Write("$([char]27)]0;${title}$([char]7)"); Write-Output ${psQuoteArg(marker)}`,
output: (value) => `Write-Output ${psQuoteArg(value)}`,
- processId: (marker) => `Write-Output "${marker}=$PID"`,
+ processId: (marker) => `Write-Output "${marker}=$PID;"`,
readEnvironment: (name, label) => {
validateEnvironmentName(name);
return `Write-Output "${label}=[$env:${name}]"`;
@@ -78,7 +78,7 @@ export function terminalSmokeShellCommands(
oscTitle: (title, marker) =>
`printf '\\033]0;${title}\\007'; printf '%s\\n' ${quotePosix(marker)}`,
output: (value) => `printf '%s\\n' ${quotePosix(value)}`,
- processId: (marker) => `printf '${marker}=%s\\n' "$$"`,
+ processId: (marker) => `printf '${marker}=%s;\\n' "$$"`,
readEnvironment: (name, label) => {
validateEnvironmentName(name);
return `echo "${label}=[$${name}]"`;
@@ -110,6 +110,22 @@ export function buildInputReadyProbe(
};
}
+export interface ProcessIdProbe {
+ marker: string;
+ command: string;
+}
+
+export function buildProcessIdProbe(
+ marker: string,
+ platform: NodeJS.Platform = process.platform,
+): ProcessIdProbe {
+ const probeMarker = `${marker}_${randomUUID().replaceAll('-', '')}`;
+ return {
+ marker: probeMarker,
+ command: terminalSmokeShellCommands(platform).processId(probeMarker),
+ };
+}
+
export const WINDOWS_PRIMARY_PROMPT_AT_END = /PS [^>]*>\s*$/u;
export function windowsPrimaryPromptAfter(marker: string): RegExp {
@@ -128,6 +144,14 @@ export function reportedWorkingDirectory(marker: string, text: string): string |
return match[1];
}
+export function readTerminalProcessId(marker: string, text: string): number {
+ const opening = `${marker}=`;
+ const start = text.lastIndexOf(opening);
+ if (start === -1) return 0;
+ const match = /^(\d+);/u.exec(text.slice(start + opening.length));
+ return match ? Number(match[1]) : 0;
+}
+
interface TerminalSmokeEnvironmentOptions {
env?: Record<string, string | undefined>;
pathPrefix?: string;
diff --git a/packages/desktop/tests/smoke/terminal-movement.e2e.ts b/packages/desktop/tests/smoke/terminal-movement.e2e.ts
index b913332bb..06679817f 100644
--- a/packages/desktop/tests/smoke/terminal-movement.e2e.ts
+++ b/packages/desktop/tests/smoke/terminal-movement.e2e.ts
@@ -36,6 +36,8 @@ import {
type ScrollbackExpectation,
} from './_helpers/terminal-scrollback';
import {
+ buildProcessIdProbe,
+ readTerminalProcessId,
seedTerminalShellProfiles,
terminalSmokeEnvironment,
terminalSmokeShellCommands,
@@ -427,14 +429,13 @@ function readBurstOutcome(
}
async function readShellPid(page: Page, marker: string): Promise<number> {
- await typeInActiveTerminal(page, `${SHELL_COMMANDS.processId(marker)}\r`);
+ const probe = buildProcessIdProbe(marker);
+ await typeInActiveTerminal(page, `${probe.command}\r`);
let processId = 0;
await expect
.poll(
async () => {
- const rows = await readTerminalRows(page);
- const matches = [...rows.matchAll(new RegExp(`${marker}=(\\d+)`, 'g'))];
- processId = Number(matches.at(-1)?.[1] ?? 0);
+ processId = readTerminalProcessId(probe.marker, await readTerminalRows(page));
return processId;
},
{ timeout: 15_000 },
diff --git a/packages/desktop/tests/unit/terminal-smoke-shell.test.ts b/packages/desktop/tests/unit/terminal-smoke-shell.test.ts
index a1d249ed6..fc432fcb1 100644
--- a/packages/desktop/tests/unit/terminal-smoke-shell.test.ts
+++ b/packages/desktop/tests/unit/terminal-smoke-shell.test.ts
@@ -11,6 +11,8 @@ import { tmpdir } from 'node:os';
import { join, win32 } from 'node:path';
import { afterEach, describe, expect, test } from 'vitest';
import {
+ buildProcessIdProbe,
+ readTerminalProcessId,
readWindowsShellProfileFailure,
reportedWorkingDirectory,
seedTerminalShellProfiles,
@@ -133,7 +135,7 @@ describe('terminal smoke shell commands', () => {
expect(commands.readEnvironment('OK_MARK', 'marker')).toBe(
'Write-Output "marker=[$env:OK_MARK]"',
);
- expect(commands.processId('SHELLPID')).toBe('Write-Output "SHELLPID=$PID"');
+ expect(commands.processId('SHELLPID')).toBe('Write-Output "SHELLPID=$PID;"');
expect(commands.output('MARK_ONE')).toBe("Write-Output 'MARK_ONE'");
expect(commands.oscTitle('program', 'OSC_FED')).toBe(
'[Console]::Write("$([char]27)]0;program$([char]7)"); Write-Output \'OSC_FED\'',
@@ -154,7 +156,7 @@ describe('terminal smoke shell commands', () => {
expect(commands.columns('COLS')).toBe('echo COLS=$(tput cols)');
expect(commands.setEnvironment('OK_MARK', 'survived')).toBe("export OK_MARK='survived'");
expect(commands.readEnvironment('OK_MARK', 'marker')).toBe('echo "marker=[$OK_MARK]"');
- expect(commands.processId('SHELLPID')).toBe('printf \'SHELLPID=%s\\n\' "$$"');
+ expect(commands.processId('SHELLPID')).toBe('printf \'SHELLPID=%s;\\n\' "$$"');
expect(commands.output('MARK_ONE')).toBe("printf '%s\\n' 'MARK_ONE'");
}
});
@@ -559,3 +561,83 @@ describe('working directory reported by the evaluated command', () => {
});
}
});
+
+describe('process id reported by terminal rows', () => {
+ const marker = 'RESTART_PROCESS_abcdef0123456789abcdef0123456789';
+
+ test('reads the marker pid when the following rendered row begins with digits', () => {
+ const text = [`${marker}=5784;`, '671PS C:\\Users\\runneradmin> '].join('');
+
+ expect(readTerminalProcessId(marker, text)).toBe(5784);
+ });
+
+ test('keeps a genuinely different marker pid different', () => {
+ const text = [`${marker}=8449;`, 'PS C:\\Users\\runneradmin> '].join('');
+
+ expect(readTerminalProcessId(marker, text)).toBe(8449);
+ });
+
+ test('uses the latest complete marker row after replay', () => {
+ const text = [`${marker}=5784;`, 'PS C:\\Users\\runneradmin> ', `${marker}=8448;`].join('');
+
+ expect(readTerminalProcessId(marker, text)).toBe(8448);
+ });
+
+ test('reads the full pid of a marker line wrapped at any column', () => {
+ const line = `${marker}=5784;`;
+ for (let column = 1; column < line.length; column += 1) {
+ const text = [
+ `${marker}=1111;`,
+ 'PS C:\\Users\\runneradmin> ',
+ line.slice(0, column),
+ line.slice(column),
+ '671PS C:\\Users\\runneradmin> ',
+ ].join('');
+
+ expect({ column, pid: readTerminalProcessId(marker, text) }).toEqual({ column, pid: 5784 });
+ }
+ });
+
+ test('reads no pid from a newest marker line whose ending has not arrived', () => {
+ const text = [`${marker}=1111;`, 'PS C:\\Users\\runneradmin> ', `${marker}=57`].join('');
+
+ expect(readTerminalProcessId(marker, text)).toBe(0);
+ });
+
+ test('reads no pid while the newest marker is the echoed command', () => {
+ for (const platform of ['win32', 'linux'] as const) {
+ const text = [
+ `${marker}=1111;`,
+ `PS C:\\Users\\runneradmin> ${terminalSmokeShellCommands(platform).processId(marker)}`,
+ ].join('');
+
+ expect({ platform, pid: readTerminalProcessId(marker, text) }).toEqual({ platform, pid: 0 });
+ }
+ });
+});
+
+describe('process id probes repeated for one marker', () => {
+ const marker = 'RESTART_PROCESS_abcdef0123456789abcdef0123456789';
+ const prompt = 'PS C:\\Users\\runneradmin> ';
+
+ test('reads no pid for a later probe until its own output arrives', () => {
+ for (const platform of ['win32', 'linux'] as const) {
+ const earlier = buildProcessIdProbe(marker, platform);
+ const later = buildProcessIdProbe(marker, platform);
+ const earlierAnswered = [
+ `${prompt}${earlier.command}`,
+ `${earlier.marker}=1111;`,
+ prompt,
+ ].join('');
+ const laterEchoed = `${earlierAnswered}${later.command}`;
+ const laterAnswered = `${laterEchoed}${later.marker}=2222;${prompt}`;
+
+ expect({
+ platform,
+ earlierAnswered: readTerminalProcessId(later.marker, earlierAnswered),
+ laterEchoed: readTerminalProcessId(later.marker, laterEchoed),
+ laterAnswered: readTerminalProcessId(later.marker, laterAnswered),
+ }).toEqual({ platform, earlierAnswered: 0, laterEchoed: 0, laterAnswered: 2222 });
+ }
+ });
+});