Beautiful, AI-native markdown IDE and LLM wiki
TypeScript 2nd-brainagent-skillsclaudecodexcompany-braindocsknowledge-knowledge-baseknowledge-managementllmllm-wikillm-wiki-karpathymarkdownmarkdown-editormdmdxnotespkmsecond-brainskills
Launch log queue and npx cooldown follow-ups (PRD-8025, PRD-8554) (#4910)
Abrahamc7e0d3456159
diff --git a/.changeset/prd-8025-8554-launch-log-followups.md b/.changeset/prd-8025-8554-launch-log-followups.mdnew file mode 100644index 000000000..2ae8272ef--- /dev/null+++ b/.changeset/prd-8025-8554-launch-log-followups.md@@ -0,0 +1,5 @@+---+"@inkeep/open-knowledge": patch+---++A launch failure log entry that cannot be written no longer risks the server, and an npx cache entry one launch left alone is checked again by the next launch instead of counting as already cleared.diff --git a/docs/content/reference/what-open-knowledge-writes.mdx b/docs/content/reference/what-open-knowledge-writes.mdxindex 72f71833c..3af90b7a7 100644--- a/docs/content/reference/what-open-knowledge-writes.mdx+++ b/docs/content/reference/what-open-knowledge-writes.mdx@@ -152,7 +152,7 @@ On each launch and project open, the app also runs **repair sweeps**: it rewrite| `~/.agents/skills/<name>/`, `~/.claude/skills/<name>/`, … | Your global-scope skills, versioned in place in your home editor dirs (kept by `ok uninstall` unless `--purge-content`) | No || `~/.ok/local/installed-skills.json`, `~/.ok/local/skill-move-retained.json` | The same skill-install markers and unfinished-scope-move bookkeeping the tables above list under a project's `.ok/local/`, written under your home directory for Global-scope skills instead | No || `~/.ok/stats.jsonl` | Local "open with AI" handoff stats — local-only, no phone-home | No |-| `_npx/<hash>/` under npm's cache (`~/.npm` unless your `npm_config_cache` says otherwise) | Deleted, not written: when a catalog-managed in-app agent that OpenKnowledge launched through npx fails because npm reports that entry is missing its `package.json`, OpenKnowledge removes that one directory so npx rebuilds it on the single relaunch that follows. Never for custom agent commands or uv-managed adapters | No |+| `_npx/<hash>/` in the npm cache that launch uses: your own npm cache (`~/.npm` unless your `npm_config_cache` says otherwise) when the agent runs through your system npx, or OpenKnowledge's managed Node.js runtime cache at `~/.ok/runtimes/cache/node` when no usable system Node.js was found and the agent runs on that runtime | Deleted, not written: when a catalog-managed in-app agent that OpenKnowledge launched through npx fails because npm reports that entry is missing its `package.json`, OpenKnowledge removes that one directory so npx rebuilds it on the single relaunch that follows. Never for custom agent commands or uv-managed adapters | No || `~/.ok/themes/` | Your saved color themes from the [Themes plugin](../plugins/themes.mdx), one Tinted Theming base16 YAML file per theme, written when you create or edit a theme | No || `~/.ok/mcp-status.json` | First-launch MCP consent record | No || `~/.ok/logs/`, `~/.ok/bug-reports/` | Desktop and CLI logs, and bug-report bundles from `ok bug-report` and the app's **Help → Report a bug…** dialog (`ok diagnose bundle` writes into the project, under `.ok/local/diagnostics/`). Each report from the dialog also gets a small YAML record holding its send state and a secret-redacted copy of the note you wrote, so report history can still show what a report was about. The zip is deleted once the report is sent; the record is kept, and both go when you delete the report from report history. If that record ever can't be read back, OpenKnowledge leaves it exactly as it is rather than overwriting your note, and writes a second tiny `.sent.yaml` file beside it when a report does send, so history still shows it as sent instead of offering to send it again | No |@@ -288,7 +288,7 @@ Projects initialized by an older CLI may lack the `local/` rule in `.ok/.gitigno| `~/.agents/skills/<name>/`, `~/.claude/skills/<name>/`, … | Your global-scope skills, versioned in place in your home editor dirs (kept by `ok uninstall` unless `--purge-content`) | No || `~/.ok/local/installed-skills.json`, `~/.ok/local/skill-move-retained.json` | The same skill-install markers and unfinished-scope-move bookkeeping the tables above list under a project's `.ok/local/`, written under your home directory for Global-scope skills instead | No || `~/.ok/stats.jsonl` | Local "open with AI" handoff stats — local-only, no phone-home | No |-| `_npx/<hash>/` under npm's cache (`~/.npm` unless your `npm_config_cache` says otherwise) | Deleted, not written: when a catalog-managed in-app agent that OpenKnowledge launched through npx fails because npm reports that entry is missing its `package.json`, OpenKnowledge removes that one directory so npx rebuilds it on the single relaunch that follows. Never for custom agent commands or uv-managed adapters | No |+| `_npx/<hash>/` in the npm cache that launch uses: your own npm cache (`~/.npm` unless your `npm_config_cache` says otherwise) when the agent runs through your system npx, or OpenKnowledge's managed Node.js runtime cache at `~/.ok/runtimes/cache/node` when no usable system Node.js was found and the agent runs on that runtime | Deleted, not written: when a catalog-managed in-app agent that OpenKnowledge launched through npx fails because npm reports that entry is missing its `package.json`, OpenKnowledge removes that one directory so npx rebuilds it on the single relaunch that follows. Never for custom agent commands or uv-managed adapters | No || `~/.ok/themes/` | Your saved color themes from the [Themes plugin](../plugins/themes.mdx), one Tinted Theming base16 YAML file per theme, written when you create or edit a theme | No || `~/.ok/logs/` | CLI logs (`cli.<date>.log`), size- and age-capped | No || `~/.ok/bug-reports/` | Bug-report bundles from `ok bug-report` — the desktop app's **Help → Report a bug…** dialog writes here too (`ok diagnose bundle` writes into the project, under `.ok/local/diagnostics/`). A report filed from the dialog also leaves a small YAML record beside its bundle, holding the report's send state and a secret-redacted copy of the note you wrote so report history can name it. That record outlives the bundle: the zip is deleted once the report is sent, and the record is kept until you delete the report from report history. If the record ever can't be read back, OpenKnowledge leaves it exactly as it is rather than overwriting your note, and writes a second tiny `.sent.yaml` file beside it when a report does send. That marker holds only the send time and the report's reference, never your note, and it is removed with the rest of the report | No |diff --git a/packages/server/src/acp/launch-failure-log.test.ts b/packages/server/src/acp/launch-failure-log.test.tsindex ae1a9f976..861a010c0 100644--- a/packages/server/src/acp/launch-failure-log.test.ts+++ b/packages/server/src/acp/launch-failure-log.test.ts@@ -1,4 +1,4 @@-import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';+import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';import { tmpdir } from 'node:os';import { join } from 'node:path';import { SPAWN_ERROR_LOG_MAX_BYTES } from '@inkeep/open-knowledge-core';@@ -76,6 +76,30 @@ test('starts over once the log reaches its size bound', async () => {expect(text).not.toContain('xxxx');});+test('a write that fails rejects only its own promise and leaves no unhandled rejection', async () => {+ const localDir = tmp();+ mkdirSync(acpLaunchFailureLogPath(localDir));+ const unhandled: unknown[] = [];+ const onUnhandled = (reason: unknown): void => {+ unhandled.push(reason);+ };+ process.on('unhandledRejection', onUnhandled);+ try {+ const outcomes = await Promise.allSettled([+ recordAcpLaunchFailure(localDir, entry()),+ recordAcpLaunchFailure(localDir, entry({ threadId: 'thread-2' })),+ ]);+ expect(outcomes.map((outcome) => outcome.status)).toEqual(['rejected', 'rejected']);+ await new Promise((resolve) => setTimeout(resolve, 20));+ expect(unhandled).toEqual([]);+ } finally {+ process.off('unhandledRejection', onUnhandled);+ }+ rmSync(acpLaunchFailureLogPath(localDir), { recursive: true, force: true });+ await recordAcpLaunchFailure(localDir, entry({ threadId: 'thread-3' }));+ expect(readFileSync(acpLaunchFailureLogPath(localDir), 'utf8')).toContain('thread=thread-3');+});+test('concurrent writes at the bound keep every entry', async () => {const localDir = tmp();writeFileSync(acpLaunchFailureLogPath(localDir), 'x'.repeat(SPAWN_ERROR_LOG_MAX_BYTES));diff --git a/packages/server/src/acp/launch-failure-log.ts b/packages/server/src/acp/launch-failure-log.tsindex e2e3e6dc4..77a3e6b73 100644--- a/packages/server/src/acp/launch-failure-log.ts+++ b/packages/server/src/acp/launch-failure-log.ts@@ -50,19 +50,21 @@ export function recordAcpLaunchFailure(const path = acpLaunchFailureLogPath(localDir);const text = formatAcpLaunchFailureEntry(entry);const previous = pendingWrites.get(path) ?? Promise.resolve();- const write = previous- .catch(() => undefined)- .then(async () => {- const currentSize = await stat(path).then(- (stats) => stats.size,- () => undefined,- );- if (spawnErrorLogOpenMode(currentSize) === 'w') await tracedWriteFile(path, text);- else await tracedAppendFile(path, text);- });- pendingWrites.set(path, write);- void write.finally(() => {- if (pendingWrites.get(path) === write) pendingWrites.delete(path);+ const write = previous.then(async () => {+ const currentSize = await stat(path).then(+ (stats) => stats.size,+ () => undefined,+ );+ if (spawnErrorLogOpenMode(currentSize) === 'w') await tracedWriteFile(path, text);+ else await tracedAppendFile(path, text);+ });+ const settled = write.then(+ () => undefined,+ () => undefined,+ );+ pendingWrites.set(path, settled);+ void settled.then(() => {+ if (pendingWrites.get(path) === settled) pendingWrites.delete(path);});return write;}diff --git a/packages/server/src/acp/thread-manager.integration.test.ts b/packages/server/src/acp/thread-manager.integration.test.tsindex 332632af9..ae514cb16 100644--- a/packages/server/src/acp/thread-manager.integration.test.ts+++ b/packages/server/src/acp/thread-manager.integration.test.ts@@ -7100,6 +7100,33 @@ describe('launch failure diagnostics and npx cache recovery', () => {40_000,);+ test('an entry left alone is probed again by the next launch, not treated as cleared', async () => {+ await withNpxLaunchFixture(+ 'while-entry-exists',+ async ({ manager, agentId, entryDir, warn }) => {+ mkdirSync(entryDir, { recursive: true });+ writeFileSync(join(entryDir, 'concurrency.lock'), '');+ const held = await manager.createThread({ agent: { source: 'registry', id: agentId } });+ await waitUntil(+ () => manager.getInfo(held.threadId)?.status === 'error',+ 20_000,+ 'launch failure while the lock is held',+ );+ expect(existsSync(join(entryDir, 'concurrency.lock'))).toBe(true);+ rmSync(join(entryDir, 'concurrency.lock'));+ const retried = await manager.createThread({ agent: { source: 'registry', id: agentId } });+ await waitUntil(+ () => manager.getInfo(retried.threadId)?.status === 'ready',+ 20_000,+ 'second launch ready',+ );+ expect(existsSync(entryDir)).toBe(false);+ expect(logCount(warn, NPX_RELAUNCH_LOG)).toBe(1);+ expect(logCount(warn, NPX_JOIN_LOG)).toBe(0);+ },+ );+ }, 60_000);+test('a second launch does not delete an entry another launch just cleared', async () => {await withNpxLaunchFixture('while-entry-exists',diff --git a/packages/server/src/acp/thread-manager.ts b/packages/server/src/acp/thread-manager.tsindex 31275e94c..f3e428236 100644--- a/packages/server/src/acp/thread-manager.ts+++ b/packages/server/src/acp/thread-manager.ts@@ -1028,14 +1028,14 @@ export class AcpThreadManager {conn.closed.then(async () => {await drainStderr();- if (launchStatusMuted()) return;+ if (record.conn !== conn || launchStatusMuted()) return;if (record.info.status !== 'exited' && record.info.status !== 'error') {this.emitStatus(record, 'exited', 'agent connection closed');}},async (err: unknown) => {await drainStderr();- if (launchStatusMuted()) return;+ if (record.conn !== conn || launchStatusMuted()) return;this.opts.log.warn({ err, threadId: record.info.threadId },'[acp-threads] agent connection closed with error',@@ -1107,7 +1107,11 @@ export class AcpThreadManager {await this.discardFailedLaunch(record);return !isThreadClosed(record);}- this.npxCacheClears.set(entryDir, Date.now());+ const claimedAt = Date.now();+ this.npxCacheClears.set(entryDir, claimedAt);+ const releaseClaim = (): void => {+ if (this.npxCacheClears.get(entryDir) === claimedAt) this.npxCacheClears.delete(entryDir);+ };for (const file of ['package.json', 'concurrency.lock']) {const code = await stat(join(entryDir, file)).then(() => 'present',@@ -1118,6 +1122,7 @@ export class AcpThreadManager {{ ...logContext, file, code },'[acp-threads] leaving an npx cache entry alone',);+ releaseClaim();return false;}}@@ -1128,6 +1133,7 @@ export class AcpThreadManager {{ err, ...logContext },'[acp-threads] stale npx cache entry could not be cleared',);+ releaseClaim();return false;}this.opts.log.warn(