kodingo-cli 1.4.1 → 1.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -37,8 +37,13 @@ async function doctorCommand() {
37
37
  process.exitCode = 1;
38
38
  }
39
39
  async function runDoctor(results) {
40
- const { initDb, saveMemory, queryMemory } = (0, persistence_config_1.getPersistence)();
41
40
  const cloudMode = (0, persistence_config_1.isCloudMode)();
41
+ const credentials = (0, persistence_config_1.getCredentials)(process.cwd());
42
+ if (cloudMode && !credentials.token) {
43
+ results.push({ name: "Project token", ok: false, error: "No project token for this folder. Run kodingo init here." });
44
+ return null;
45
+ }
46
+ const { initDb, saveMemory, queryMemory } = (0, persistence_config_1.getPersistence)();
42
47
  // ── Mode-specific connectivity checks ──────────────────────────────────────
43
48
  if (cloudMode) {
44
49
  await runCloudChecks(results);
@@ -104,28 +109,28 @@ async function runLocalChecks(results) {
104
109
  }
105
110
  // ── Cloud mode checks ─────────────────────────────────────────────────────────
106
111
  async function runCloudChecks(results) {
107
- const config = (0, persistence_config_1.readConfig)();
112
+ const credentials = (0, persistence_config_1.getCredentials)(process.cwd());
108
113
  await runStep("KODINGO_API_URL is configured", async () => {
109
- if (!config.apiUrl) {
114
+ if (!credentials.apiUrl) {
110
115
  throw new Error("apiUrl is not set in ~/.kodingo/config.json");
111
116
  }
112
117
  }, results);
113
118
  await runStep("KODINGO_API_TOKEN is configured", async () => {
114
- if (!config.token) {
115
- throw new Error("token is not set in ~/.kodingo/config.json");
119
+ if (!credentials.token) {
120
+ throw new Error("No project token for this folder. Run kodingo init here.");
116
121
  }
117
122
  }, results);
118
123
  await runStep("kodingo-api reachable", async () => {
119
- const apiUrl = config.apiUrl.replace(/\/$/, "");
124
+ const apiUrl = credentials.apiUrl.replace(/\/$/, "");
120
125
  const res = await fetch(`${apiUrl}/health`);
121
126
  if (!res.ok) {
122
127
  throw new Error(`Health check failed: HTTP ${res.status}`);
123
128
  }
124
129
  }, results);
125
130
  await runStep("kodingo-api token valid", async () => {
126
- const apiUrl = config.apiUrl.replace(/\/$/, "");
131
+ const apiUrl = credentials.apiUrl.replace(/\/$/, "");
127
132
  const res = await fetch(`${apiUrl}/memory?limit=1`, {
128
- headers: { "X-Kodingo-Token": config.token },
133
+ headers: { "X-Kodingo-Token": credentials.token },
129
134
  });
130
135
  if (res.status === 401) {
131
136
  throw new Error("Token is invalid or expired");
@@ -2,20 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.guardrailsCommand = guardrailsCommand;
4
4
  const persistence_config_1 = require("../utils/persistence-config");
5
- const API_BASE_DEFAULT = "https://kodingo-api.onrender.com";
6
- function normalizePath(p) {
7
- return p.replace(/\\\\/g, "/").toLowerCase();
8
- }
9
- function getCredentials() {
10
- const config = (0, persistence_config_1.readConfig)();
11
- const cwd = normalizePath(process.cwd());
12
- const ws = (0, persistence_config_1.readWorkspaceConfig)(cwd);
13
- const token = ws?.token ?? config.token ?? "";
14
- const apiUrl = ws?.apiUrl ?? config.apiUrl ?? API_BASE_DEFAULT;
15
- return { token, apiUrl };
16
- }
17
5
  async function guardrailsCommand(options) {
18
- const { token, apiUrl } = getCredentials();
6
+ const { token, apiUrl } = (0, persistence_config_1.getCredentials)(process.cwd());
19
7
  if (!token) {
20
8
  console.error("No token found. Run `kodingo init` first.");
21
9
  process.exit(1);
@@ -42,6 +42,7 @@ var __importStar = (this && this.__importStar) || (function () {
42
42
  };
43
43
  })();
44
44
  Object.defineProperty(exports, "__esModule", { value: true });
45
+ exports.ensureLocalExclude = ensureLocalExclude;
45
46
  exports.checkoutCoverageComplete = checkoutCoverageComplete;
46
47
  exports.shouldSkipLocalScan = shouldSkipLocalScan;
47
48
  exports.registerInitCommand = registerInitCommand;
@@ -49,6 +50,7 @@ const readline = __importStar(require("node:readline"));
49
50
  const fs = __importStar(require("fs"));
50
51
  const path = __importStar(require("path"));
51
52
  const os = __importStar(require("os"));
53
+ const node_child_process_1 = require("node:child_process");
52
54
  // open is imported dynamically to avoid ESM/CJS compatibility issues on Windows
53
55
  const persistence_config_1 = require("../utils/persistence-config");
54
56
  const repo_identity_1 = require("../utils/repo-identity");
@@ -112,18 +114,20 @@ function findRepoRoot(startPath) {
112
114
  current = parent;
113
115
  }
114
116
  }
115
- // ── .gitignore helper ─────────────────────────────────────────────────────────
116
- function ensureGitignoreEntry(repoRoot, entry) {
117
- const gitignorePath = path.join(repoRoot, ".gitignore");
117
+ // ── Local git exclusion; never edit the working tree or index ───────────────
118
+ function ensureLocalExclude(repoRoot, entry) {
119
+ const exclude = (0, node_child_process_1.execFileSync)("git", ["-C", repoRoot, "rev-parse", "--git-path", "info/exclude"], { encoding: "utf8" }).trim();
120
+ const gitignorePath = path.resolve(repoRoot, exclude);
118
121
  const line = entry.endsWith("\n") ? entry : `${entry}\n`;
122
+ fs.mkdirSync(path.dirname(gitignorePath), { recursive: true });
119
123
  if (fs.existsSync(gitignorePath)) {
120
124
  const contents = fs.readFileSync(gitignorePath, "utf-8");
121
125
  if (contents.split("\n").some(l => l.trim() === entry.trim()))
122
126
  return;
123
- fs.appendFileSync(gitignorePath, `\n# Kortex\n${line}`, "utf-8");
127
+ fs.appendFileSync(gitignorePath, `${contents.endsWith("\n") ? "" : "\n"}${line}`, "utf-8");
124
128
  }
125
129
  else {
126
- fs.writeFileSync(gitignorePath, `# Kortex\n${line}`, "utf-8");
130
+ fs.writeFileSync(gitignorePath, line, "utf-8");
127
131
  }
128
132
  }
129
133
  // ── Browser login flow ────────────────────────────────────────────────────────
@@ -248,7 +252,7 @@ function registerInitCommand(program) {
248
252
  const existingConfig = (0, persistence_config_1.readConfig)();
249
253
  (0, persistence_config_1.writeConfig)({ ...existingConfig, mode: "cloud", apiUrl, machineId: getMachineId() });
250
254
  (0, persistence_config_1.writeWorkspaceConfig)(repoRoot, { mode: "cloud", apiUrl, token: environmentToken });
251
- ensureGitignoreEntry(repoRoot, ".kortex/");
255
+ ensureLocalExclude(repoRoot, ".kortex/");
252
256
  const registration = await (0, repo_identity_1.resolveRepoRegistration)({ repoRoot, apiUrl, token: environmentToken });
253
257
  const coverageComplete = (!opts.force && !opts.dryRun) ? await checkoutCoverageComplete(apiUrl, environmentToken, registration.alreadyRegistered) : false;
254
258
  const alreadyComplete = shouldSkipLocalScan(opts.force, opts.dryRun, coverageComplete);
@@ -345,7 +349,7 @@ function registerInitCommand(program) {
345
349
  const machineId = getMachineId();
346
350
  (0, persistence_config_1.writeConfig)({ ...existingConfig, mode: "cloud", apiUrl, machineId });
347
351
  (0, persistence_config_1.writeWorkspaceConfig)(repoRoot, { mode: "cloud", apiUrl, token: selectedProject.token });
348
- ensureGitignoreEntry(repoRoot, ".kortex/");
352
+ ensureLocalExclude(repoRoot, ".kortex/");
349
353
  // Register this repo with the project on the API.
350
354
  // This is the authoritative binding that prevents cross-project
351
355
  // contamination — resolved from the git remote (stable across
@@ -4,7 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.queryMemoryCommand = queryMemoryCommand;
7
- const fs_1 = __importDefault(require("fs"));
7
+ exports.resolveRepoScope = resolveRepoScope;
8
8
  const path_1 = __importDefault(require("path"));
9
9
  const persistence_config_1 = require("../utils/persistence-config");
10
10
  async function queryMemoryCommand(text, options) {
@@ -17,7 +17,7 @@ async function queryMemoryCommand(text, options) {
17
17
  includeNonCanonical: options.includeNonCanonical === true,
18
18
  });
19
19
  if (results.length === 0) {
20
- console.log("No results.");
20
+ console.log(`No results for ${JSON.stringify(text)} in this project.`);
21
21
  return;
22
22
  }
23
23
  for (const r of results) {
@@ -43,19 +43,8 @@ function resolveRepoScope(options) {
43
43
  if (options.global === true)
44
44
  return undefined;
45
45
  if (options.repo && options.repo.trim()) {
46
- return path_1.default.resolve(options.repo.trim());
47
- }
48
- return findRepoRoot(process.cwd());
49
- }
50
- function findRepoRoot(startPath) {
51
- let current = startPath;
52
- while (true) {
53
- const gitPath = path_1.default.join(current, ".git");
54
- if (fs_1.default.existsSync(gitPath))
55
- return current;
56
- const parent = path_1.default.dirname(current);
57
- if (parent === current)
58
- return startPath;
59
- current = parent;
46
+ const value = options.repo.trim().replace(/\\/g, "/").replace(/\/+$/, "");
47
+ return value.includes("/") ? path_1.default.posix.basename(value) : value;
60
48
  }
49
+ return undefined;
61
50
  }
@@ -43,20 +43,8 @@ exports.sessionBriefCommand = sessionBriefCommand;
43
43
  */
44
44
  const persistence_config_1 = require("../utils/persistence-config");
45
45
  const path = __importStar(require("path"));
46
- const API_BASE_DEFAULT = "https://kodingo-api.onrender.com";
47
- function normalizePath(p) {
48
- return p.replace(/\\\\/g, "/").toLowerCase();
49
- }
50
- function getCredentials() {
51
- const config = (0, persistence_config_1.readConfig)();
52
- const cwd = normalizePath(process.cwd());
53
- const ws = (0, persistence_config_1.readWorkspaceConfig)(cwd);
54
- const token = ws?.token ?? config.token ?? "";
55
- const apiUrl = ws?.apiUrl ?? config.apiUrl ?? API_BASE_DEFAULT;
56
- return { token, apiUrl };
57
- }
58
46
  async function sessionBriefCommand(options) {
59
- const { token, apiUrl } = getCredentials();
47
+ const { token, apiUrl } = (0, persistence_config_1.getCredentials)(process.cwd());
60
48
  if (!token) {
61
49
  console.error("No token found. Run `kodingo init` first.");
62
50
  process.exit(1);
@@ -4,6 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.CONFIG_PATH = void 0;
7
+ exports.findWorkspaceEntry = findWorkspaceEntry;
7
8
  exports.readConfig = readConfig;
8
9
  exports.writeConfig = writeConfig;
9
10
  exports.writeWorkspaceConfig = writeWorkspaceConfig;
@@ -28,14 +29,55 @@ const node_fs_1 = __importDefault(require("node:fs"));
28
29
  const node_path_1 = __importDefault(require("node:path"));
29
30
  const node_os_1 = __importDefault(require("node:os"));
30
31
  exports.CONFIG_PATH = node_path_1.default.join(node_os_1.default.homedir(), ".kodingo", "config.json");
31
- function normalizePath(p) {
32
- return p.replace(/\\/g, '/').toLowerCase();
32
+ let permissionsWarningPrinted = false;
33
+ function comparablePath(value) {
34
+ const normalized = node_path_1.default.resolve(value.replace(/\\/g, "/")).replace(/\/+$/, "") || "/";
35
+ try {
36
+ return node_fs_1.default.realpathSync(normalized).replace(/\\/g, "/").replace(/\/+$/, "").toLowerCase() || "/";
37
+ }
38
+ catch {
39
+ return normalized.toLowerCase();
40
+ }
41
+ }
42
+ /** Find the nearest workspace without changing its stored key. */
43
+ function findWorkspaceEntry(cwd, config) {
44
+ const entries = Object.entries(config.workspaces ?? {}).map(([key, value]) => [comparablePath(key), value]);
45
+ let current = node_path_1.default.resolve(cwd.replace(/\\/g, "/"));
46
+ for (;;) {
47
+ const match = entries.find(([key]) => key === comparablePath(current));
48
+ if (match)
49
+ return match[1];
50
+ const parent = node_path_1.default.dirname(current);
51
+ if (parent === current)
52
+ return null;
53
+ current = parent;
54
+ }
55
+ }
56
+ function tightenPermissions() {
57
+ if (process.platform === "win32")
58
+ return;
59
+ const dir = node_path_1.default.dirname(exports.CONFIG_PATH);
60
+ let tightened = false;
61
+ for (const [target, mode] of [[dir, 0o700], [exports.CONFIG_PATH, 0o600]]) {
62
+ try {
63
+ if (node_fs_1.default.existsSync(target) && (node_fs_1.default.statSync(target).mode & 0o077)) {
64
+ node_fs_1.default.chmodSync(target, mode);
65
+ tightened = true;
66
+ }
67
+ }
68
+ catch { /* Reading config must survive permission repair failures. */ }
69
+ }
70
+ if (tightened && !permissionsWarningPrinted) {
71
+ console.error("Tightened permissions on ~/.kodingo/config.json (it holds project tokens).");
72
+ permissionsWarningPrinted = true;
73
+ }
33
74
  }
34
75
  function readConfig() {
35
76
  if (!node_fs_1.default.existsSync(exports.CONFIG_PATH)) {
36
77
  return { mode: "cloud", apiUrl: "https://kodingo-api.onrender.com" };
37
78
  }
38
79
  try {
80
+ tightenPermissions();
39
81
  const raw = node_fs_1.default.readFileSync(exports.CONFIG_PATH, "utf-8");
40
82
  const parsed = JSON.parse(raw);
41
83
  if (!parsed.mode)
@@ -49,10 +91,24 @@ function readConfig() {
49
91
  }
50
92
  function writeConfig(config) {
51
93
  const dir = node_path_1.default.dirname(exports.CONFIG_PATH);
52
- if (!node_fs_1.default.existsSync(dir)) {
53
- node_fs_1.default.mkdirSync(dir, { recursive: true });
94
+ node_fs_1.default.mkdirSync(dir, { recursive: true, mode: 0o700 });
95
+ if (process.platform !== "win32") {
96
+ try {
97
+ node_fs_1.default.chmodSync(dir, 0o700);
98
+ }
99
+ catch { }
100
+ }
101
+ const temporary = node_path_1.default.join(dir, `.config-${process.pid}-${Math.random().toString(16).slice(2)}.tmp`);
102
+ try {
103
+ node_fs_1.default.writeFileSync(temporary, JSON.stringify(config, null, 2), { encoding: "utf-8", mode: 0o600, flag: "wx" });
104
+ node_fs_1.default.renameSync(temporary, exports.CONFIG_PATH);
105
+ }
106
+ finally {
107
+ try {
108
+ node_fs_1.default.unlinkSync(temporary);
109
+ }
110
+ catch { }
54
111
  }
55
- node_fs_1.default.writeFileSync(exports.CONFIG_PATH, JSON.stringify(config, null, 2), "utf-8");
56
112
  }
57
113
  function writeWorkspaceConfig(repoPath, wsConfig) {
58
114
  const existing = readConfig();
@@ -61,8 +117,7 @@ function writeWorkspaceConfig(repoPath, wsConfig) {
61
117
  writeConfig({ ...existing, workspaces });
62
118
  }
63
119
  function readWorkspaceConfig(repoPath) {
64
- const config = readConfig();
65
- return config.workspaces?.[repoPath] ?? null;
120
+ return findWorkspaceEntry(repoPath, readConfig());
66
121
  }
67
122
  function isCloudMode() {
68
123
  return readConfig().mode === "cloud";
@@ -82,14 +137,8 @@ const API_BASE_DEFAULT = "https://kodingo-api.onrender.com";
82
137
  */
83
138
  function getCredentials(repoRoot) {
84
139
  const config = readConfig();
85
- const normRoot = repoRoot.replace(/\/+$/, "");
86
- let wsEntry = config.workspaces?.[normRoot] ?? config.workspaces?.[normRoot + "/"];
87
- let current = normRoot;
88
- while (!wsEntry && current !== node_path_1.default.dirname(current)) {
89
- current = node_path_1.default.dirname(current);
90
- wsEntry = config.workspaces?.[current] ?? config.workspaces?.[current + "/"];
91
- }
92
- const token = wsEntry?.token ?? "";
140
+ const wsEntry = findWorkspaceEntry(repoRoot, config);
141
+ const token = wsEntry?.token ?? config.token ?? "";
93
142
  const apiUrl = wsEntry?.apiUrl ?? config.apiUrl ?? API_BASE_DEFAULT;
94
143
  return { token, apiUrl };
95
144
  }
@@ -100,15 +149,16 @@ function getCredentials(repoRoot) {
100
149
  function getPersistence() {
101
150
  const config = readConfig();
102
151
  if (config.mode === "cloud") {
103
- if (!config.apiUrl)
152
+ const wsConfig = findWorkspaceEntry(process.cwd(), config);
153
+ const apiUrl = wsConfig?.apiUrl ?? config.apiUrl;
154
+ if (!apiUrl)
104
155
  throw new Error("Cloud mode requires apiUrl in ~/.kodingo/config.json");
105
156
  // Try workspace-scoped token first, fall back to global token
106
- const wsConfig = readWorkspaceConfig(normalizePath(process.cwd()));
107
157
  const token = wsConfig?.token ?? config.token ?? "";
108
158
  if (!token)
109
159
  throw new Error("No project token found. Run `kodingo init` in your project directory first.");
110
160
  // Inject into env so cloud-persistence picks them up
111
- process.env.KODINGO_API_URL = wsConfig?.apiUrl ?? config.apiUrl;
161
+ process.env.KODINGO_API_URL = apiUrl;
112
162
  process.env.KODINGO_API_TOKEN = token;
113
163
  return require("../adapters/cloud-adapter/cloud-persistence");
114
164
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kodingo-cli",
3
- "version": "1.4.1",
3
+ "version": "1.4.3",
4
4
  "description": "Kodingo CLI",
5
5
  "license": "MIT",
6
6
  "private": false,
@@ -1,22 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const node_fs_1 = __importDefault(require("node:fs"));
7
- const node_path_1 = __importDefault(require("node:path"));
8
- const node_worker_threads_1 = require("node:worker_threads");
9
- const worker = new node_worker_threads_1.Worker(node_path_1.default.join(__dirname, "deep-scan-worker.js"));
10
- process.on("message", (message) => {
11
- try {
12
- const bytes = node_fs_1.default.readFileSync(message.absolutePath);
13
- const transferable = bytes.buffer instanceof ArrayBuffer && bytes.byteOffset === 0 && bytes.byteLength === bytes.buffer.byteLength ? new Uint8Array(bytes.buffer) : Uint8Array.from(bytes);
14
- worker.postMessage({ id: message.id, filePath: message.filePath, blobSha: message.blobSha, bytes: transferable, includeExcerpts: message.includeExcerpts }, [transferable.buffer]);
15
- }
16
- catch (error) {
17
- process.send?.({ id: message.id, error: error instanceof Error ? error.message : String(error) });
18
- }
19
- });
20
- worker.on("message", (message) => process.send?.({ ...message, processRss: process.memoryUsage().rss }));
21
- worker.on("error", (error) => process.send?.({ id: -1, error: error.message }));
22
- process.on("disconnect", async () => { await worker.terminate(); process.exit(0); });