Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

Fix deinit relative project targeting (PRD-7468) (#7477)

Andrew Mikofalvyd09934cae604
diff --git a/.changeset/deinit-relative-project-target.md b/.changeset/deinit-relative-project-target.md
new file mode 100644
index 000000000..78c8e09cb
--- /dev/null
+++ b/.changeset/deinit-relative-project-target.md
@@ -0,0 +1,5 @@
+---
+'@inkeep/open-knowledge': patch
+---
+
+Resolve explicit `deinit` paths relative to the command's working directory so ancestor project detection cannot remove a different project's setup.
diff --git a/docs/content/reference/cli.mdx b/docs/content/reference/cli.mdx
index 0dc2e7399..0b5f95160 100644
--- a/docs/content/reference/cli.mdx
+++ b/docs/content/reference/cli.mdx
@@ -199,7 +199,9 @@ The rest of the CLI exists mostly for tooling and automation. Your AI editor spa
## Remove OpenKnowledge
-`ok deinit [path]` reverses `ok init` for one project (the current project when `path` is omitted): it removes the project's `.ok/` directory, OpenKnowledge's editor MCP entries, its own git-exclude lines, and shadow repo, leaving your markdown untouched. Shared editor settings and their symlinks are preserved; ignore rules for shared config files stay while those files still exist. User-level project diagnostics remain under `~/.ok/logs/projects/<project-hash>/` (Beta: `~/.ok-beta/logs/projects/<project-hash>/`). Later server starts with local diagnostic sinks enabled can reclaim inactive directories older than seven days; disabling the sinks does not erase existing files.
+`ok deinit [path]` reverses `ok init` for one project: it removes the project's `.ok/` directory, OpenKnowledge's editor MCP entries, its own git-exclude lines, and shadow repo, leaving your markdown untouched. Shared editor settings and their symlinks are preserved; ignore rules for shared config files stay while those files still exist. User-level project diagnostics remain under `~/.ok/logs/projects/<project-hash>/` (Beta: `~/.ok-beta/logs/projects/<project-hash>/`). Later server starts with local diagnostic sinks enabled can reclaim inactive directories older than seven days; disabling the sinks does not erase existing files.
+
+When `path` is omitted, `ok deinit` targets the enclosing project. An explicit path resolves from where the command runs, honoring `--cwd`. From a subdirectory, `ok deinit` selects the enclosing project while `ok deinit .` selects that subdirectory instead.
`ok uninstall` removes the global footprint: running servers, credentials, the PATH shim, OpenKnowledge's editor MCP entries and bundled skills, app data, and `~/.ok`. It keeps the `~/.ok` entries every product shares: `~/.ok/machine-id`, the user skills store `~/.ok/skills`, `~/.ok/skills-lock.json`, the Global-scope skill bookkeeping in `~/.ok/local/installed-skills.json`, `~/.ok/local/skill-placements.json`, and `~/.ok/local/skill-move-retained.json`, plus the shared ownership registry `~/.ok/local/server-authority.sqlite`, its `-journal` file, and `~/.ok/local/server-authority-leases/`. These coordination files stay even with `--purge-content`. It removes only the running product's footprint: `ok-beta uninstall` removes `~/.ok-beta` and Beta's credentials and leaves Stable's `~/.ok` alone. No product's uninstall removes `~/.ok/machine-id`. With `--purge-content`, either product also removes the shared `~/.ok/skills`, unless an earlier uninstall step failed. See [Stable and Beta home folders](/docs/reference/what-open-knowledge-writes#stable-and-beta-home-folders). Your markdown and other skills in your editors' skill directories stay. An interactive run lets you select projects to deinitialize; `--yes` removes only the global footprint unless you also pass `--all-projects`, which covers current, recent, and discovered running projects. The CLI binary stays installed; the command ends by printing how to remove it (`npm uninstall -g @inkeep/open-knowledge` for the install above).
diff --git a/packages/cli/src/cli.ts b/packages/cli/src/cli.ts
index a3aa28d78..04a9f703f 100644
--- a/packages/cli/src/cli.ts
+++ b/packages/cli/src/cli.ts
@@ -106,7 +106,9 @@ program
if (anchorRoot !== null) {
recordInvocationCwd(process.cwd());
process.chdir(anchorRoot);
- console.error(`[ok] Using OpenKnowledge project at ${anchorRoot}`);
+ if (subcommandName !== 'deinit' || actionCommand.args.length === 0) {
+ console.error(`[ok] Using OpenKnowledge project at ${anchorRoot}`);
+ }
}
let config: Config;
diff --git a/packages/cli/src/commands/deinit.ts b/packages/cli/src/commands/deinit.ts
index bc939e621..606327cc7 100644
--- a/packages/cli/src/commands/deinit.ts
+++ b/packages/cli/src/commands/deinit.ts
@@ -4,6 +4,7 @@ import { join, resolve } from 'node:path';
import { currentCliCommandName, OK_USER_HOME_AUTH_FILE } from '@inkeep/open-knowledge-core';
import { isHomeDir } from '@inkeep/open-knowledge-server';
import { Command } from 'commander';
+import { getInvocationCwd } from '../project-anchor.ts';
import { accent, dim, error as errorColor, warning } from '../ui/colors.ts';
import { confirmDestructive } from '../ui/confirm.ts';
import {
@@ -115,7 +116,10 @@ export function deinitCommand(): Command {
.description(
`Remove OpenKnowledge from this project (its .ok/, editor MCP entries, git-exclude lines, shadow repo) while leaving your markdown content untouched. Re-run \`${currentCliCommandName()} start\` to re-scaffold.`,
)
- .argument('[path]', 'Project directory (defaults to the current project)')
+ .argument(
+ '[path]',
+ 'Project directory, relative to where you run the command (defaults to the enclosing project)',
+ )
.option('-y, --yes', 'Skip the confirmation prompt')
.option('--dry-run', 'Print the removal plan and exit without changing anything')
.option('--json', 'Emit a machine-readable plan/outcome (requires --yes or --dry-run)')
@@ -124,8 +128,12 @@ export function deinitCommand(): Command {
pathArg: string | undefined,
options: { yes?: boolean; dryRun?: boolean; json?: boolean },
) => {
+ const cwd = pathArg === undefined ? process.cwd() : resolve(getInvocationCwd(), pathArg);
+ if (pathArg !== undefined) {
+ process.stderr.write(`[ok] Deinit target: ${cwd}\n`);
+ }
const result = await runDeinit({
- cwd: pathArg ?? process.cwd(),
+ cwd,
yes: options.yes,
dryRun: options.dryRun,
json: options.json,
diff --git a/packages/cli/src/project-anchor.test.ts b/packages/cli/src/project-anchor.test.ts
index 5deedc062..aa719f0d7 100644
--- a/packages/cli/src/project-anchor.test.ts
+++ b/packages/cli/src/project-anchor.test.ts
@@ -1,7 +1,15 @@
import { spawn } from 'node:child_process';
-import { mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from 'node:fs';
+import {
+ existsSync,
+ mkdirSync,
+ mkdtempSync,
+ readFileSync,
+ realpathSync,
+ rmSync,
+ writeFileSync,
+} from 'node:fs';
import { hostname, tmpdir } from 'node:os';
-import { join } from 'node:path';
+import { join, relative } from 'node:path';
import { getMachineId } from '@inkeep/open-knowledge-server';
import { afterAll, afterEach, describe, expect, test } from 'vitest';
import { getInvocationCwd, recordInvocationCwd, resolveProjectAnchor } from './project-anchor.ts';
@@ -122,7 +130,10 @@ describe('resolveProjectAnchor — real filesystem walk', () => {
});
});
-function spawnCli(args: string[]): { exitCode: number | null; stdout: string; stderr: string } {
+function spawnCli(
+ args: string[],
+ env: NodeJS.ProcessEnv = {},
+): { exitCode: number | null; stdout: string; stderr: string } {
const result = Bun.spawnSync({
cmd: [
'node',
@@ -136,7 +147,7 @@ function spawnCli(args: string[]): { exitCode: number | null; stdout: string; st
`,
],
cwd: CLI_PACKAGE_ROOT,
- env: { ...process.env, NO_COLOR: '1', OK_BUNDLE_PROXY: '0' },
+ env: { ...process.env, ...env, NO_COLOR: '1', OK_BUNDLE_PROXY: '0' },
stdin: 'ignore',
timeout: 25_000,
});
@@ -148,6 +159,77 @@ function spawnCli(args: string[]): { exitCode: number | null; stdout: string; st
}
describe('CLI preAction project anchoring (cold spawn)', () => {
+ test.each([
+ 'relative',
+ 'absolute',
+ 'missing',
+ 'dot',
+ 'empty',
+ 'global-cwd-relative',
+ 'omitted',
+ ] as const)(
+ '`ok deinit` preserves invocation-relative targeting (%s)',
+ (targetKind) => {
+ const fixture = realpathSync(makeFixture());
+ const home = join(fixture, 'home');
+ const outer = join(fixture, 'outer');
+ const sub = join(outer, 'sub');
+ const intended = join(sub, 'target');
+ const sibling = join(outer, 'target');
+ const missingSibling = join(outer, 'missing');
+ mkdirSync(home);
+ for (const dir of [outer, intended, sibling, missingSibling]) {
+ makeProjectRoot(dir);
+ writeFileSync(join(dir, 'canary.txt'), dir);
+ }
+ const targets = {
+ relative: ['./target'],
+ absolute: [intended],
+ missing: ['./missing'],
+ dot: ['.'],
+ empty: [''],
+ 'global-cwd-relative': ['./target'],
+ omitted: [],
+ };
+ const cwd = targetKind === 'global-cwd-relative' ? relative(CLI_PACKAGE_ROOT, sub) : sub;
+ const { exitCode, stdout, stderr } = spawnCli(
+ ['--cwd', cwd, 'deinit', ...targets[targetKind], '--yes', '--json'],
+ { HOME: home, USERPROFILE: home },
+ );
+
+ expect(exitCode).toBe(0);
+ if (targetKind === 'omitted') {
+ expect(stderr).toContain(`[ok] Using OpenKnowledge project at ${outer}`);
+ } else {
+ const expectedTarget =
+ targetKind === 'relative' ||
+ targetKind === 'absolute' ||
+ targetKind === 'global-cwd-relative'
+ ? intended
+ : targetKind === 'missing'
+ ? join(sub, 'missing')
+ : sub;
+ expect(stderr).not.toContain('Using OpenKnowledge project at');
+ expect(stderr).toContain(`[ok] Deinit target: ${expectedTarget}`);
+ }
+ expect(existsSync(join(outer, '.ok'))).toBe(targetKind !== 'omitted');
+ expect(existsSync(join(intended, '.ok'))).toBe(
+ targetKind !== 'relative' &&
+ targetKind !== 'absolute' &&
+ targetKind !== 'global-cwd-relative',
+ );
+ expect(existsSync(join(sibling, '.ok'))).toBe(true);
+ expect(existsSync(join(missingSibling, '.ok'))).toBe(true);
+ for (const dir of [outer, intended, sibling, missingSibling]) {
+ expect(readFileSync(join(dir, 'canary.txt'), 'utf8')).toBe(dir);
+ }
+ if (targetKind === 'missing' || targetKind === 'dot' || targetKind === 'empty') {
+ expect(stdout).toContain('Nothing to remove.');
+ }
+ },
+ 30_000,
+ );
+
test('`ok status` from a subdirectory anchors to the root and prints the disclosure line', () => {
const root = makeFixture();
makeProjectRoot(root);