kovence-cli 1.2.0 → 1.3.0

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.
@@ -1,17 +1,111 @@
1
1
  "use strict";
2
2
 
3
+ const crypto = require("node:crypto");
3
4
  const fs = require("node:fs");
4
5
  const os = require("node:os");
5
6
  const path = require("node:path");
7
+ const { DEFAULT_SYSTEM_PROMPT, hashPromptText } = require("../settings/schema");
6
8
 
7
9
  const MAX_INSTRUCTION_CHARS = 12000;
8
10
 
11
+ const FAMILY_ADDENDA = {
12
+ claude: "Prefer parallel tool calls for independent reads and searches. Keep tool arguments compact; for large files write a skeleton and then edit.",
13
+ gpt: "Carry the task through to a verified result; do not stop at a plan. Use apply_patch when it is available.",
14
+ gemini: "Follow tool schemas exactly and emit strictly valid JSON arguments. Keep explanations short.",
15
+ default:
16
+ "Follow tool schemas exactly and emit valid JSON arguments. Make the tool call instead of describing it. After each tool result decide the next single step. Keep each write small.",
17
+ };
18
+
19
+ /**
20
+ * Determine model family by substring match on model ID, falling back to provider type when ID is empty/unknown.
21
+ */
22
+ function getModelFamily(modelId = "", providerType = "") {
23
+ const id = String(modelId || "").toLowerCase();
24
+ if (id.includes("claude")) return "claude";
25
+ if (/(?:gpt|codex|(?:^|[/\-_.])o[134](?:$|[/\-_.]|\b))/.test(id)) return "gpt";
26
+ if (id.includes("gemini")) return "gemini";
27
+ if (!id.trim() && String(providerType || "").toLowerCase() === "anthropic") return "claude";
28
+ if (!id.trim() && String(providerType || "").toLowerCase() === "gemini") return "gemini";
29
+ return "default";
30
+ }
31
+
32
+ function getSystemPromptAddendum(family) {
33
+ return FAMILY_ADDENDA[family] || FAMILY_ADDENDA.default;
34
+ }
35
+
36
+ const DELEGATION_PROMPT_SECTION = [
37
+ "# Delegation",
38
+ "- Use the task tool for broad exploration across many files or for independent sub-jobs. Launch several explore agents in ONE message when the questions are independent. Do not use it for simple lookups that 1-3 direct tool calls can answer. Write self-contained prompts and say exactly what to return.",
39
+ ].join("\n");
40
+
41
+ /**
42
+ * Build the byte-stable prefix of the system prompt (no timestamps, no cwd, deterministic order).
43
+ */
44
+ function buildStableSystemPrompt({
45
+ systemPrompt = DEFAULT_SYSTEM_PROMPT,
46
+ model = "",
47
+ providerType = "",
48
+ language = "",
49
+ includeDelegation = false,
50
+ } = {}) {
51
+ const rawPrompt = String(systemPrompt || DEFAULT_SYSTEM_PROMPT);
52
+ const isDefault = hashPromptText(rawPrompt) === hashPromptText(DEFAULT_SYSTEM_PROMPT);
53
+
54
+ const parts = [];
55
+ if (isDefault) {
56
+ parts.push(DEFAULT_SYSTEM_PROMPT);
57
+ if (includeDelegation) {
58
+ parts.push(DELEGATION_PROMPT_SECTION);
59
+ }
60
+ parts.push(getSystemPromptAddendum(getModelFamily(model, providerType)));
61
+ } else {
62
+ // User-customized systemPrompt replaces default base + family addendum
63
+ parts.push(rawPrompt);
64
+ }
65
+
66
+ if (language) {
67
+ const langMap = {
68
+ banglish: "Language Preference: Respond in Banglish (Bengali written cleanly in Latin/Roman alphabet).",
69
+ bangla: "Language Preference: Respond in Bengali.",
70
+ english: "Language Preference: Respond in clear English.",
71
+ };
72
+ if (langMap[language]) parts.push(langMap[language]);
73
+ }
74
+
75
+ return parts.join("\n\n");
76
+ }
77
+
78
+ function findGitRoot(startDir) {
79
+ let cursor = path.resolve(startDir);
80
+ const visited = new Set();
81
+ while (true) {
82
+ let real = cursor;
83
+ try {
84
+ real = fs.realpathSync(cursor);
85
+ } catch {
86
+ // Ignore unresolvable path
87
+ }
88
+ if (visited.has(real)) break;
89
+ visited.add(real);
90
+
91
+ if (fs.existsSync(path.join(cursor, ".git"))) {
92
+ return cursor;
93
+ }
94
+ const parent = path.dirname(cursor);
95
+ if (parent === cursor) break;
96
+ cursor = parent;
97
+ }
98
+ return "";
99
+ }
100
+
9
101
  /**
10
102
  * Read current git branch directly from .git/HEAD when inside a git repository.
11
103
  */
12
104
  function readGitBranch(workspaceRoot) {
13
105
  try {
14
- const headPath = path.join(workspaceRoot, ".git", "HEAD");
106
+ const gitRoot = findGitRoot(workspaceRoot);
107
+ if (!gitRoot) return "";
108
+ const headPath = path.join(gitRoot, ".git", "HEAD");
15
109
  if (!fs.existsSync(headPath)) return "";
16
110
  const raw = fs.readFileSync(headPath, "utf8").trim();
17
111
  const m = raw.match(/^ref:\s*refs\/heads\/(.+)$/);
@@ -22,13 +116,19 @@ function readGitBranch(workspaceRoot) {
22
116
  }
23
117
 
24
118
  /**
25
- * Collect concise runtime environment facts for the system prompt.
119
+ * Collect concise runtime environment facts (placed AFTER the byte-stable prompt prefix).
26
120
  */
27
121
  function gatherEnvironmentSummary(workspaceRoot = process.cwd()) {
28
- const branch = readGitBranch(workspaceRoot);
122
+ const absRoot = path.resolve(workspaceRoot);
123
+ const gitRoot = findGitRoot(absRoot);
124
+ const branch = gitRoot ? readGitBranch(absRoot) : "";
125
+ const shell = process.env.SHELL || process.env.ComSpec || (process.platform === "win32" ? "cmd.exe" : "sh");
29
126
  const parts = [
127
+ `CWD: ${absRoot}`,
30
128
  `OS: ${os.type()} ${os.release()} (${process.arch})`,
129
+ `Shell: ${path.basename(shell)}`,
31
130
  `Node: ${process.version}`,
131
+ `Git Repo: ${gitRoot ? "yes" : "no"}`,
32
132
  `Date: ${new Date().toISOString().slice(0, 10)}`,
33
133
  ];
34
134
  if (branch) parts.push(`Git Branch: ${branch}`);
@@ -36,35 +136,122 @@ function gatherEnvironmentSummary(workspaceRoot = process.cwd()) {
36
136
  }
37
137
 
38
138
  /**
39
- * Auto-load project instruction files (KOVENCE.md, AGENTS.md, .kovence/KOVENCE.md).
139
+ * Discover and load instruction files (KOVENCE.md, AGENTS.md, CLAUDE.md) walking UP from cwd
140
+ * to the repo root (or home directory if outside a repo), plus global ~/.config/kovence/AGENTS.md.
141
+ * Nearest file has highest priority; drops farthest files first when over 12k chars; deduplicates content.
40
142
  */
41
- function loadProjectInstructions(workspaceRoot = process.cwd()) {
42
- const candidates = [
43
- path.join(workspaceRoot, "KOVENCE.md"),
44
- path.join(workspaceRoot, "AGENTS.md"),
45
- path.join(workspaceRoot, ".kovence", "KOVENCE.md"),
46
- ];
47
- const sections = [];
48
- let total = 0;
143
+ function loadProjectInstructionsDetailed(
144
+ workspaceRoot = process.cwd(),
145
+ { xdgConfigHome = process.env.XDG_CONFIG_HOME || "", homeDir = os.homedir(), maxChars = MAX_INSTRUCTION_CHARS } = {}
146
+ ) {
147
+ const startDir = path.resolve(workspaceRoot || process.cwd());
148
+ const resolvedHome = path.resolve(homeDir || os.homedir());
149
+ const gitRoot = findGitRoot(startDir);
150
+ const stopDir = gitRoot || resolvedHome;
49
151
 
50
- for (const file of candidates) {
51
- if (!fs.existsSync(file)) continue;
152
+ // 1. Walk UP from startDir to stopDir collecting candidate paths (nearest -> farthest)
153
+ const dirs = [];
154
+ const visitedDirs = new Set();
155
+ let cursor = startDir;
156
+ while (true) {
157
+ let realDir = cursor;
52
158
  try {
53
- const stat = fs.statSync(file);
159
+ realDir = fs.realpathSync(cursor);
160
+ } catch {
161
+ // Keep logical path if realpath fails
162
+ }
163
+ if (visitedDirs.has(realDir)) break;
164
+ visitedDirs.add(realDir);
165
+ dirs.push(cursor);
166
+
167
+ if (cursor === stopDir || realDir === stopDir) break;
168
+ const parent = path.dirname(cursor);
169
+ if (parent === cursor) break;
170
+ cursor = parent;
171
+ }
172
+
173
+ const candidatePaths = [];
174
+ for (let idx = 0; idx < dirs.length; idx++) {
175
+ const dir = dirs[idx];
176
+ candidatePaths.push(path.join(dir, "KOVENCE.md"));
177
+ candidatePaths.push(path.join(dir, "AGENTS.md"));
178
+ candidatePaths.push(path.join(dir, "CLAUDE.md"));
179
+ if (idx === 0) {
180
+ candidatePaths.push(path.join(dir, ".kovence", "KOVENCE.md"));
181
+ }
182
+ }
183
+
184
+ const cfgRoot = xdgConfigHome ? path.resolve(xdgConfigHome) : path.join(resolvedHome, ".config");
185
+ candidatePaths.push(path.join(cfgRoot, "kovence", "AGENTS.md"));
186
+
187
+ // 2. Read files safely, deduplicating by realpath and content hash
188
+ const seenRealFiles = new Set();
189
+ const seenHashes = new Set();
190
+ const loadedBlocks = [];
191
+
192
+ for (const file of candidatePaths) {
193
+ try {
194
+ if (!fs.existsSync(file)) continue;
195
+ const realFile = fs.realpathSync(file);
196
+ if (seenRealFiles.has(realFile)) continue;
197
+ seenRealFiles.add(realFile);
198
+
199
+ const stat = fs.statSync(realFile);
54
200
  if (!stat.isFile()) continue;
55
- const raw = fs.readFileSync(file, "utf8").trim();
201
+
202
+ const raw = fs.readFileSync(realFile, "utf8").trim();
56
203
  if (!raw) continue;
57
- const remain = MAX_INSTRUCTION_CHARS - total;
58
- if (remain <= 0) break;
59
- const slice = raw.slice(0, remain);
60
- total += slice.length;
61
- const rel = path.relative(workspaceRoot, file);
62
- sections.push(`[Project Instructions: ${rel}]\n${slice}`);
204
+
205
+ const contentHash = crypto.createHash("sha256").update(raw).digest("hex");
206
+ if (seenHashes.has(contentHash)) continue;
207
+ seenHashes.add(contentHash);
208
+
209
+ const rel = path.relative(startDir, file);
210
+ const label = rel && !rel.startsWith("..") ? rel.replace(/\\/g, "/") : file.replace(/\\/g, "/");
211
+ loadedBlocks.push({
212
+ path: file,
213
+ label,
214
+ content: raw,
215
+ });
63
216
  } catch {
64
- // Skip unreadable file cleanly.
217
+ // Skip unreadable file or broken symlink cleanly
218
+ }
219
+ }
220
+
221
+ // 3. Apply cap with nearest-first priority; drop farthest first
222
+ const kept = [];
223
+ const dropped = [];
224
+ let totalChars = 0;
225
+
226
+ for (const item of loadedBlocks) {
227
+ const block = `[Project Instructions: ${item.label}]\n${item.content}`;
228
+ const separatorLen = kept.length > 0 ? 2 : 0;
229
+ if (totalChars + separatorLen + block.length <= maxChars) {
230
+ kept.push({ ...item, block });
231
+ totalChars += separatorLen + block.length;
232
+ } else if (kept.length === 0) {
233
+ // Single nearest file exceeds the entire cap: keep truncated prefix rather than empty
234
+ const prefix = `[Project Instructions: ${item.label}]\n`;
235
+ const sliceLen = Math.max(0, maxChars - prefix.length);
236
+ const truncatedBlock = `${prefix}${item.content.slice(0, sliceLen)}`;
237
+ kept.push({ ...item, block: truncatedBlock, truncated: true });
238
+ totalChars = truncatedBlock.length;
239
+ dropped.push(`${item.label} (truncated to ${maxChars} chars)`);
240
+ } else {
241
+ dropped.push(item.label);
65
242
  }
66
243
  }
67
- return sections.join("\n\n");
244
+
245
+ return {
246
+ text: kept.map((k) => k.block).join("\n\n"),
247
+ files: kept.map((k) => k.label),
248
+ dropped,
249
+ totalChars,
250
+ };
251
+ }
252
+
253
+ function loadProjectInstructions(workspaceRoot = process.cwd(), options = {}) {
254
+ return loadProjectInstructionsDetailed(workspaceRoot, options).text;
68
255
  }
69
256
 
70
257
  /**
@@ -129,8 +316,16 @@ function generateProjectInitDoc(workspaceRoot = process.cwd()) {
129
316
  }
130
317
 
131
318
  module.exports = {
319
+ MAX_INSTRUCTION_CHARS,
320
+ FAMILY_ADDENDA,
321
+ DELEGATION_PROMPT_SECTION,
322
+ getModelFamily,
323
+ getSystemPromptAddendum,
324
+ buildStableSystemPrompt,
325
+ findGitRoot,
132
326
  readGitBranch,
133
327
  gatherEnvironmentSummary,
328
+ loadProjectInstructionsDetailed,
134
329
  loadProjectInstructions,
135
330
  generateProjectInitDoc,
136
331
  };
@@ -0,0 +1,148 @@
1
+ "use strict";
2
+
3
+ const fs = require("node:fs");
4
+ const path = require("node:path");
5
+
6
+ const SENSITIVE_HEADER_KEYS = new Set([
7
+ "authorization",
8
+ "proxy-authorization",
9
+ "x-api-key",
10
+ "api-key",
11
+ "x-goog-api-key",
12
+ "x-auth-token",
13
+ "cookie",
14
+ "set-cookie",
15
+ ]);
16
+
17
+ const SECRET_ENV_KEY_RE =
18
+ /(?:^|_)(API_KEY|SECRET|TOKEN|PASSWORD|PASSWD|PRIVATE_KEY|ACCESS_KEY|CREDENTIAL|AUTH)(?:$|_)/i;
19
+
20
+ const EXTRA_SCRUB_KEYS = new Set([
21
+ "OPENAI_API_KEY",
22
+ "ANTHROPIC_API_KEY",
23
+ "LITELLM_API_KEY",
24
+ "GOOGLE_API_KEY",
25
+ "BRAVE_API_KEY",
26
+ "TAVILY_API_KEY",
27
+ "API_KEY",
28
+ ]);
29
+
30
+ function collectSecretEnvValues(envObj = process.env, extraValues = []) {
31
+ const secrets = new Set();
32
+ for (const [k, v] of Object.entries(envObj || {})) {
33
+ if (!v || typeof v !== "string") continue;
34
+ if (v.length < 6) continue;
35
+ if (/^XDG_|^GNOME_|^DBUS_|^GPG_|^SSH_AUTH_SOCK$/i.test(k)) continue;
36
+ if (EXTRA_SCRUB_KEYS.has(k) || SECRET_ENV_KEY_RE.test(k)) {
37
+ secrets.add(v);
38
+ }
39
+ }
40
+ for (const val of Array.isArray(extraValues) ? extraValues : []) {
41
+ if (typeof val === "string" && val.length >= 6) {
42
+ secrets.add(val);
43
+ }
44
+ }
45
+ return [...secrets].sort((a, b) => b.length - a.length);
46
+ }
47
+
48
+ function scrubString(input, secretValues = []) {
49
+ let out = String(input ?? "");
50
+ if (!out) return out;
51
+ out = out.replace(/\bBearer\s+[A-Za-z0-9._~+/=-]{8,}/gi, "Bearer [REDACTED]");
52
+ out = out.replace(/([?&](?:key|api_key|token|access_token|secret)=)[^&\s"']+/gi, "$1[REDACTED]");
53
+ out = out.replace(/\bsk-[A-Za-z0-9_-]{8,}/g, "[REDACTED]");
54
+ out = out.replace(/\bAIza[0-9A-Za-z_-]{12,}/g, "[REDACTED]");
55
+ out = out.replace(/\bghp_[A-Za-z0-9]{16,}/g, "[REDACTED]");
56
+ out = out.replace(/\bgithub_pat_[A-Za-z0-9_]{16,}/g, "[REDACTED]");
57
+ out = out.replace(/\bxox[baprs]-[A-Za-z0-9-]{8,}/g, "[REDACTED]");
58
+ for (const secret of secretValues) {
59
+ if (secret && out.includes(secret)) {
60
+ out = out.split(secret).join("[REDACTED]");
61
+ }
62
+ }
63
+ return out;
64
+ }
65
+
66
+ /**
67
+ * Recursively scrub secret headers, API key tokens, URL query secrets, and known secret env values.
68
+ */
69
+ function scrubSecrets(value, options = {}) {
70
+ const secretValues = Array.isArray(options.secretValues)
71
+ ? options.secretValues
72
+ : collectSecretEnvValues(options.env || process.env, options.extraSecretValues || []);
73
+ const seen = new WeakSet();
74
+
75
+ const walk = (node, keyName = "") => {
76
+ if (node === null || node === undefined) return node;
77
+ if (typeof node === "string") {
78
+ if (SENSITIVE_HEADER_KEYS.has(String(keyName).toLowerCase())) {
79
+ return /^Bearer\s+/i.test(node) ? "Bearer [REDACTED]" : "[REDACTED]";
80
+ }
81
+ return scrubString(node, secretValues);
82
+ }
83
+ if (typeof node !== "object") return node;
84
+ if (seen.has(node)) return "[Circular]";
85
+ seen.add(node);
86
+ if (Array.isArray(node)) {
87
+ return node.map((item) => walk(item, ""));
88
+ }
89
+ const copy = {};
90
+ for (const [k, v] of Object.entries(node)) {
91
+ if (SENSITIVE_HEADER_KEYS.has(String(k).toLowerCase())) {
92
+ copy[k] = typeof v === "string" && /^Bearer\s+/i.test(v) ? "Bearer [REDACTED]" : "[REDACTED]";
93
+ } else {
94
+ copy[k] = walk(v, k);
95
+ }
96
+ }
97
+ return copy;
98
+ };
99
+
100
+ return walk(value, "");
101
+ }
102
+
103
+ function createRequestLogger(filePath, options = {}) {
104
+ const absPath = path.resolve(String(filePath || "requests.jsonl"));
105
+ let seq = 0;
106
+ let dirEnsured = false;
107
+
108
+ return {
109
+ filePath: absPath,
110
+ /**
111
+ * Append one scrubbed outgoing HTTP request record as JSONL.
112
+ * @param {{ url?: string, method?: string, headers?: Record<string, any>, body?: any, provider?: any, model?: string }} [reqData]
113
+ */
114
+ log({ url = "", method = "POST", headers = {}, body = null, provider = "", model = "" } = {}) {
115
+ if (!dirEnsured) {
116
+ fs.mkdirSync(path.dirname(absPath), { recursive: true });
117
+ dirEnsured = true;
118
+ }
119
+ seq += 1;
120
+ const extraSecretValues = [...(options.extraSecretValues || [])];
121
+ if (provider && typeof provider === "object" && provider.apiKey) {
122
+ extraSecretValues.push(provider.apiKey);
123
+ }
124
+ const secretValues = collectSecretEnvValues(options.env || process.env, extraSecretValues);
125
+ const providerName =
126
+ provider && typeof provider === "object" ? String(provider.name || "") : String(provider || "");
127
+ const entry = {
128
+ seq,
129
+ timestamp: new Date().toISOString(),
130
+ provider: providerName,
131
+ model: String(model || ""),
132
+ method,
133
+ url: scrubSecrets(String(url || ""), { secretValues }),
134
+ headers: scrubSecrets(headers || {}, { secretValues }),
135
+ body: scrubSecrets(body, { secretValues }),
136
+ };
137
+ fs.appendFileSync(absPath, `${JSON.stringify(entry)}\n`, "utf8");
138
+ return entry;
139
+ },
140
+ };
141
+ }
142
+
143
+ module.exports = {
144
+ SENSITIVE_HEADER_KEYS,
145
+ collectSecretEnvValues,
146
+ scrubSecrets,
147
+ createRequestLogger,
148
+ };