@ngockhoale/ukit 3.5.5 → 3.5.7
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.
- package/CHANGELOG.md +25 -0
- package/manifests/engineConformance.yaml +0 -13
- package/manifests/platform.full.yaml +25 -0
- package/package.json +1 -8
- package/src/cli/commands/indexTools.js +1 -0
- package/src/cli/commands/memory.js +33 -4
- package/src/core/applyPlan.js +37 -72
- package/src/core/diffPlan.js +39 -9
- package/src/core/managedTemplateMerge.js +231 -0
- package/src/core/memory/memoryFlags.js +5 -6
- package/src/core/memory/policy.js +5 -0
- package/src/core/memory/store.js +17 -0
- package/src/core/metadata.js +12 -8
- package/src/core/runInstallPipeline.js +16 -10
- package/src/core/runtimeConfig.js +14 -83
- package/src/core/uninstall.js +8 -1
- package/src/decision/registry.js +17 -17
- package/src/decision/runtimeDecide.js +5 -5
- package/src/index/playbookRegistry.js +6 -1
- package/src/index/queryIndex.js +41 -4
- package/src/index/routeResolver.js +2 -2
- package/src/index/semanticLane.js +277 -0
- package/src/learning/lessonClass.js +110 -0
- package/src/learning/patternProposals.js +111 -7
- package/src/learning/reeditMiner.js +104 -0
- package/src/learning/selfImprove.js +9 -1
- package/template_project/.claude/hooks/skill-router.sh +273 -5
- package/template_project/.claude/skills/canvas-design/SKILL.md +2 -2
- package/template_project/.claude/skills/canvas-design/canvas-fonts/{InstrumentSans-OFL.txt → InstrumentSerif-OFL.txt} +3 -3
- package/template_project/.claude/skills/canvas-design/scripts/fetch-fonts.mjs +148 -0
- package/template_project/.claude/ukit/index/embed-index.mjs +26 -0
- package/template_project/.claude/ukit/index/lib/index-core.mjs +43 -4
- package/template_project/.claude/ukit/index/lib/semantic-lane.mjs +277 -0
- package/template_project/.claude/ukit/index/playbook-registry.mjs +6 -1
- package/template_project/.claude/ukit/index/publication-intent.mjs +159 -1
- package/template_project/.claude/ukit/index/query-index.mjs +9 -1
- package/template_project/.claude/ukit/index/route-resolver.mjs +2 -2
- package/template_project/.claude/ukit/index/route-task.mjs +14 -3
- package/template_project/.claude/ukit/runtime/memory-policy.mjs +5 -0
- package/template_project/.claude/ukit/runtime/stop-coordinator.mjs +12 -1
- package/template_project/ukit/storage/config.json +0 -32
- package/template_project/.claude/skills/canvas-design/canvas-fonts/BricolageGrotesque-Bold.ttf +0 -0
- package/template_project/.claude/skills/canvas-design/canvas-fonts/BricolageGrotesque-OFL.txt +0 -93
- package/template_project/.claude/skills/canvas-design/canvas-fonts/BricolageGrotesque-Regular.ttf +0 -0
- package/template_project/.claude/skills/canvas-design/canvas-fonts/InstrumentSans-Bold.ttf +0 -0
- package/template_project/.claude/skills/canvas-design/canvas-fonts/InstrumentSans-BoldItalic.ttf +0 -0
- package/template_project/.claude/skills/canvas-design/canvas-fonts/InstrumentSans-Italic.ttf +0 -0
- package/template_project/.claude/skills/canvas-design/canvas-fonts/InstrumentSans-Regular.ttf +0 -0
- package/template_project/.claude/skills/canvas-design/canvas-fonts/InstrumentSerif-Italic.ttf +0 -0
- package/template_project/.claude/skills/canvas-design/canvas-fonts/JetBrainsMono-Bold.ttf +0 -0
- package/template_project/.claude/skills/canvas-design/canvas-fonts/Lora-Bold.ttf +0 -0
- package/template_project/.claude/skills/canvas-design/canvas-fonts/Lora-BoldItalic.ttf +0 -0
- package/template_project/.claude/skills/canvas-design/canvas-fonts/Lora-Italic.ttf +0 -0
- package/template_project/.claude/skills/canvas-design/canvas-fonts/Lora-OFL.txt +0 -93
- package/template_project/.claude/skills/canvas-design/canvas-fonts/Lora-Regular.ttf +0 -0
- package/template_project/.claude/skills/canvas-design/canvas-fonts/Outfit-Bold.ttf +0 -0
- package/template_project/.claude/skills/canvas-design/canvas-fonts/Tektur-Medium.ttf +0 -0
- package/template_project/.claude/skills/canvas-design/canvas-fonts/YoungSerif-OFL.txt +0 -93
- package/template_project/.claude/skills/canvas-design/canvas-fonts/YoungSerif-Regular.ttf +0 -0
|
@@ -4,6 +4,7 @@ import crypto from 'node:crypto';
|
|
|
4
4
|
import { execFile, spawnSync } from 'node:child_process';
|
|
5
5
|
import { withFileLock } from '../../runtime/token-utils.mjs';
|
|
6
6
|
import { withAsyncLock } from '../../runtime/async-lock.mjs';
|
|
7
|
+
import { embedConfig, looksLikeQuestion, semanticCandidates, fuseSemantic, semanticStoreSignature } from './semantic-lane.mjs';
|
|
7
8
|
// edit-graph.mjs is optional: a partial install must not lose the whole index core.
|
|
8
9
|
let editGraphModule;
|
|
9
10
|
function loadEditGraphModule() {
|
|
@@ -1398,7 +1399,7 @@ export async function getFileOutline({ rootDir = process.cwd(), filePath, limit
|
|
|
1398
1399
|
}
|
|
1399
1400
|
|
|
1400
1401
|
// contentLane=false skips the error-string content search (context-resolution paths never pay for it).
|
|
1401
|
-
export async function queryCodeIndex({ rootDir = process.cwd(), query, limit = 5, contentLane = true } = {}) {
|
|
1402
|
+
export async function queryCodeIndex({ rootDir = process.cwd(), query, limit = 5, contentLane = true, semantic = false } = {}) {
|
|
1402
1403
|
const normalizedQuery = String(query ?? '').trim();
|
|
1403
1404
|
if (!normalizedQuery) return [];
|
|
1404
1405
|
if (limit <= 0) return Object.freeze([]);
|
|
@@ -1411,11 +1412,15 @@ export async function queryCodeIndex({ rootDir = process.cwd(), query, limit = 5
|
|
|
1411
1412
|
query: normalizedQuery,
|
|
1412
1413
|
limit,
|
|
1413
1414
|
contentLane,
|
|
1415
|
+
semantic: semantic && embedConfig() ? `1:${await semanticStoreSignature(absoluteRoot)}` : 0,
|
|
1414
1416
|
});
|
|
1415
1417
|
if (QUERY_RESULT_CACHE.has(queryCacheKey)) {
|
|
1416
1418
|
return QUERY_RESULT_CACHE.get(queryCacheKey);
|
|
1417
1419
|
}
|
|
1418
1420
|
const queryPromise = (async () => {
|
|
1421
|
+
// `laneStatus.fellBack`: the lane was asked for (question-shaped, lexical not decisive) and
|
|
1422
|
+
// produced nothing (timeout / HTTP error / no vectors). Such a result must not be cached.
|
|
1423
|
+
const laneStatus = { fellBack: false };
|
|
1419
1424
|
const { files, searchDescriptorsByFile, tokenWeights } = await loadQuerySearchBundle(absoluteRoot);
|
|
1420
1425
|
const preferredArchetypes = inferPreferredArchetypes(queryDescriptor.tokens);
|
|
1421
1426
|
const includeLikelyTestFiles = shouldIncludeLikelyTestFiles(queryDescriptor);
|
|
@@ -1481,7 +1486,12 @@ export async function queryCodeIndex({ rootDir = process.cwd(), query, limit = 5
|
|
|
1481
1486
|
});
|
|
1482
1487
|
}
|
|
1483
1488
|
|
|
1484
|
-
if (directScores.size === 0)
|
|
1489
|
+
if (directScores.size === 0) {
|
|
1490
|
+
const onlySemantic = semantic ? await applySemanticLane({ absoluteRoot, query: normalizedQuery, ranked: [], includeLikelyTestFiles, files, laneStatus }) : null;
|
|
1491
|
+
if (laneStatus.fellBack) QUERY_RESULT_CACHE.delete(queryCacheKey);
|
|
1492
|
+
if (onlySemantic) return withContentLaneStatus(freezeQueryResults(onlySemantic.slice(0, limit)), contentLaneRan, contentCutShort);
|
|
1493
|
+
return withContentLaneStatus(Object.freeze([]), contentLaneRan, contentCutShort, laneStatus.fellBack);
|
|
1494
|
+
}
|
|
1485
1495
|
|
|
1486
1496
|
const {
|
|
1487
1497
|
hotspotScores,
|
|
@@ -1523,7 +1533,9 @@ export async function queryCodeIndex({ rootDir = process.cwd(), query, limit = 5
|
|
|
1523
1533
|
const fused = contentLane && ranked.length > 0 && ranked[0].score < FUSION_WEAK_SCORE && queryDescriptor.tokens.size >= FUSION_MIN_TOKENS
|
|
1524
1534
|
? await fuseWeakQuery({ absoluteRoot, query: normalizedQuery, ranked, searchDescriptorsByFile, includeLikelyTestFiles })
|
|
1525
1535
|
: null;
|
|
1526
|
-
|
|
1536
|
+
const semanticMerged = !fused && semantic ? await applySemanticLane({ absoluteRoot, query: normalizedQuery, ranked, includeLikelyTestFiles, files, laneStatus }) : null;
|
|
1537
|
+
if (laneStatus.fellBack) QUERY_RESULT_CACHE.delete(queryCacheKey);
|
|
1538
|
+
return withContentLaneStatus(freezeQueryResults((fused ?? semanticMerged ?? ranked).slice(0, limit)), contentLaneRan, contentCutShort, laneStatus.fellBack);
|
|
1527
1539
|
})().catch((error) => {
|
|
1528
1540
|
QUERY_RESULT_CACHE.delete(queryCacheKey);
|
|
1529
1541
|
throw error;
|
|
@@ -1533,6 +1545,31 @@ export async function queryCodeIndex({ rootDir = process.cwd(), query, limit = 5
|
|
|
1533
1545
|
return queryPromise;
|
|
1534
1546
|
}
|
|
1535
1547
|
|
|
1548
|
+
// Real-embedding lane: only when the CLI passes semantic:true AND UKIT_INDEX_EMBED_URL is set.
|
|
1549
|
+
// Applies to question-shaped queries only; any problem returns null (lexical result unchanged).
|
|
1550
|
+
const SEMANTIC_CONFIDENT_MARGIN = 2;
|
|
1551
|
+
async function applySemanticLane({ absoluteRoot, query, ranked, includeLikelyTestFiles, files, laneStatus = {} }) {
|
|
1552
|
+
try {
|
|
1553
|
+
if (!embedConfig() || !looksLikeQuestion(query)) return null;
|
|
1554
|
+
// Lexical is already decisive (top-1 clear of top-2): keep it exactly, no network call.
|
|
1555
|
+
if (ranked.length > 1 && ranked[0].score - ranked[1].score >= SEMANTIC_CONFIDENT_MARGIN) return null;
|
|
1556
|
+
const known = new Set(safeItems(files).map((f) => f?.filePath));
|
|
1557
|
+
const candidates = await semanticCandidates({
|
|
1558
|
+
rootDir: absoluteRoot,
|
|
1559
|
+
query,
|
|
1560
|
+
// Vectors can outlive a deleted/renamed file: only paths in the current index qualify.
|
|
1561
|
+
accept: (filePath) => known.has(filePath) && (includeLikelyTestFiles || !isLikelyTestFilePath(filePath)),
|
|
1562
|
+
weight: mirrorPrior,
|
|
1563
|
+
});
|
|
1564
|
+
const fused = fuseSemantic({ ranked, candidates });
|
|
1565
|
+
if (!fused) laneStatus.fellBack = true;
|
|
1566
|
+
return fused;
|
|
1567
|
+
} catch {
|
|
1568
|
+
laneStatus.fellBack = true;
|
|
1569
|
+
return null;
|
|
1570
|
+
}
|
|
1571
|
+
}
|
|
1572
|
+
|
|
1536
1573
|
// Opt-in lane fusion (default OFF): a weak natural-language query (best score below
|
|
1537
1574
|
// FUSION_WEAK_SCORE, >= FUSION_MIN_TOKENS tokens) is re-ranked by RRF over the scored list
|
|
1538
1575
|
// and a hashed-embedding lane (the retriever.js vector lane). Enabled by
|
|
@@ -1741,9 +1778,11 @@ export function queryMatchedSymbolNames(result) {
|
|
|
1741
1778
|
// Non-enumerable (array equality and JSON stay unchanged), like boundedSourceSearch rows.truncated:
|
|
1742
1779
|
// `contentLane` is true when the content search ran; `contentLaneTruncated` lists why it was cut
|
|
1743
1780
|
// short ('verify-cap', 'time-budget', 'max-files', ...) so a partial scan never reads as "no match".
|
|
1744
|
-
function withContentLaneStatus(results, ran, truncated) {
|
|
1781
|
+
function withContentLaneStatus(results, ran, truncated, semanticFellBack = false) {
|
|
1745
1782
|
// results arrive frozen, so copy before attaching the status and freeze again.
|
|
1746
1783
|
const out = [...results];
|
|
1784
|
+
// true when the embedding lane was asked for and returned nothing: callers must not persist this result.
|
|
1785
|
+
Object.defineProperty(out, 'semanticFellBack', { value: Boolean(semanticFellBack), enumerable: false });
|
|
1747
1786
|
Object.defineProperty(out, 'contentLane', { value: ran, enumerable: false });
|
|
1748
1787
|
Object.defineProperty(out, 'contentLaneTruncated', { value: Object.freeze([...truncated]), enumerable: false });
|
|
1749
1788
|
return Object.freeze(out);
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
// Real-embedding lane for `ukit index query` (opt-in; no dependency; global fetch only).
|
|
2
|
+
//
|
|
3
|
+
// PRIVACY: building vectors SENDS FILE TEXT (path, symbol names, leading comment and,
|
|
4
|
+
// at most ~240 characters per file) to the endpoint in
|
|
5
|
+
// UKIT_INDEX_EMBED_URL, and queries send the question text. With no URL configured nothing
|
|
6
|
+
// here runs and nothing leaves the machine. Sensitive paths (.env, keys, credentials ...)
|
|
7
|
+
// are never embedded; only files already present in the index are considered.
|
|
8
|
+
//
|
|
9
|
+
// Byte-identical mirror: template_project/.claude/ukit/index/lib/semantic-lane.mjs
|
|
10
|
+
// (tests/index/semanticLane.test.js enforces it). Keep this file dependency-free.
|
|
11
|
+
//
|
|
12
|
+
// Env: UKIT_INDEX_EMBED_URL (base URL ending in /v1), UKIT_INDEX_EMBED_MODEL,
|
|
13
|
+
// UKIT_INDEX_EMBED_KEY (optional Bearer).
|
|
14
|
+
// Build is an explicit command (embed-index.mjs), never part of index build/refresh, never in
|
|
15
|
+
// a hook. Query use is reachable only through queryCodeIndex({ semantic: true }), which only
|
|
16
|
+
// the CLI query path passes.
|
|
17
|
+
import fs from 'node:fs/promises';
|
|
18
|
+
import path from 'node:path';
|
|
19
|
+
import crypto from 'node:crypto';
|
|
20
|
+
|
|
21
|
+
export const SEMANTIC_MANIFEST = 'semantic.json';
|
|
22
|
+
export const SEMANTIC_BIN = 'semantic.bin';
|
|
23
|
+
// A CLI query waits at most this long for the embedding of the question. Measured on the
|
|
24
|
+
// reference endpoint: warm question embedding ~0.45 s, 64 descriptors ~24 s (serial service).
|
|
25
|
+
export const QUERY_TIMEOUT_MS = 1500;
|
|
26
|
+
// One descriptor per file, ~60 tokens. Endpoint throughput is ~170-200 tokens/s and serial, so
|
|
27
|
+
// file bodies (>1 h for 2.4k files) are not embedded; descriptors build in minutes.
|
|
28
|
+
export const DOC_CHARS = 240;
|
|
29
|
+
const NAMES_CHARS = 90;
|
|
30
|
+
const LEAD_CHARS = 100;
|
|
31
|
+
const QUERY_INSTRUCTION = 'Instruct: Given a question about a code repository, retrieve the source file that answers it\nQuery: ';
|
|
32
|
+
|
|
33
|
+
export function embedConfig(env = process.env) {
|
|
34
|
+
// Accepts the base (.../v1) or the full endpoint (.../v1/embeddings).
|
|
35
|
+
const url = String(env.UKIT_INDEX_EMBED_URL ?? '').trim().replace(/\/+$/, '').replace(/\/embeddings$/i, '');
|
|
36
|
+
if (!url) return null;
|
|
37
|
+
return { url, model: String(env.UKIT_INDEX_EMBED_MODEL ?? '').trim() || 'unic-embedding', key: String(env.UKIT_INDEX_EMBED_KEY ?? '').trim() };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function isSensitive(value) {
|
|
41
|
+
const segments = String(value ?? '').replace(/\\/g, '/').split('/');
|
|
42
|
+
const base = segments[segments.length - 1] ?? '';
|
|
43
|
+
return base.toLowerCase().startsWith('.env')
|
|
44
|
+
|| /\.(?:pem|key|p12|pfx|jks|keystore)$/i.test(base)
|
|
45
|
+
|| /^(?:credentials?|\.npmrc|\.netrc|\.pypirc)(?:\.[^/]*)?$/i.test(base)
|
|
46
|
+
|| /^id_(?:rsa|dsa|ecdsa|ed25519)(?:\.[^/]*)?$/i.test(base)
|
|
47
|
+
|| segments.includes('.ssh') || segments.includes('.aws') || segments.includes('.gnupg');
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const sha = (text) => crypto.createHash('sha256').update(text).digest('hex').slice(0, 24);
|
|
51
|
+
|
|
52
|
+
// POST /embeddings; returns arrays in input order or throws. Validates shape.
|
|
53
|
+
export async function embedTexts(texts, cfg, { timeoutMs = 60000 } = {}) {
|
|
54
|
+
const headers = { 'content-type': 'application/json' };
|
|
55
|
+
if (cfg.key) headers.authorization = `Bearer ${cfg.key}`;
|
|
56
|
+
const response = await fetch(`${cfg.url}/embeddings`, {
|
|
57
|
+
method: 'POST',
|
|
58
|
+
headers,
|
|
59
|
+
body: JSON.stringify({ model: cfg.model, input: texts }),
|
|
60
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
61
|
+
});
|
|
62
|
+
if (!response.ok) throw new Error(`embedding endpoint HTTP ${response.status}`);
|
|
63
|
+
const data = (await response.json())?.data;
|
|
64
|
+
if (!Array.isArray(data) || data.length !== texts.length) throw new Error('embedding response shape mismatch');
|
|
65
|
+
const rows = [...data].sort((a, b) => (a.index ?? 0) - (b.index ?? 0)).map((d) => d.embedding);
|
|
66
|
+
const dims = rows[0]?.length;
|
|
67
|
+
if (!dims || rows.some((r) => !Array.isArray(r) || r.length !== dims || r.some((x) => !Number.isFinite(x)))) {
|
|
68
|
+
throw new Error('embedding response vectors invalid');
|
|
69
|
+
}
|
|
70
|
+
return rows;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function normalizeTo(vec, dims) {
|
|
74
|
+
const out = vec.slice(0, dims);
|
|
75
|
+
let n = 0;
|
|
76
|
+
for (const x of out) n += x * x;
|
|
77
|
+
n = Math.sqrt(n) || 1;
|
|
78
|
+
return out.map((x) => x / n);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Descriptor for one file: path + exported/declared symbol names + leading doc comment,
|
|
82
|
+
// capped at DOC_CHARS. The path is never truncated; names and comment share the remainder.
|
|
83
|
+
export function buildDocumentText({ filePath, symbols = [], content = '' }) {
|
|
84
|
+
const names = [...new Set(symbols)].join(' ').slice(0, NAMES_CHARS);
|
|
85
|
+
const lines = String(content).split('\n');
|
|
86
|
+
const lead = [];
|
|
87
|
+
for (const line of lines.slice(0, 80)) {
|
|
88
|
+
const t = line.trim();
|
|
89
|
+
if (!t || t.startsWith('#!') || t === '---' || t.startsWith("'use ")) { if (lead.length) break; continue; }
|
|
90
|
+
if (/^(\/\/|\/\*|\*|#|<!--|"""|--)/.test(t)) lead.push(t.replace(/^(\/\/+|\/\*+|\*+\/?|#+|<!--|-->|"""|--)\s?/, ''));
|
|
91
|
+
else break;
|
|
92
|
+
}
|
|
93
|
+
const leading = (lead.length ? lead.join(' ') : '').slice(0, LEAD_CHARS);
|
|
94
|
+
const text = `${filePath}\n${names}\n${leading}`;
|
|
95
|
+
return text.length > DOC_CHARS ? text.slice(0, DOC_CHARS) : text;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async function readJson(file) {
|
|
99
|
+
try { return JSON.parse(await fs.readFile(file, 'utf8')); } catch { return null; }
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
async function loadStore(indexDir) {
|
|
103
|
+
const manifest = await readJson(path.join(indexDir, SEMANTIC_MANIFEST));
|
|
104
|
+
if (!manifest || manifest.v !== 1 || !Array.isArray(manifest.order)) return null;
|
|
105
|
+
let bin;
|
|
106
|
+
try { bin = await fs.readFile(path.join(indexDir, SEMANTIC_BIN)); } catch { return null; }
|
|
107
|
+
const rec = 4 + manifest.dims;
|
|
108
|
+
if (bin.length !== rec * manifest.order.length) return null;
|
|
109
|
+
return { manifest, bin, rec };
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// Explicit build step. Embeds only documents whose hash (document text + model) is not in the
|
|
113
|
+
// existing store. Returns stats. Never called from index build/refresh or hooks.
|
|
114
|
+
export async function buildSemanticVectors({
|
|
115
|
+
rootDir = process.cwd(), env = process.env, dims = 0,
|
|
116
|
+
batchSize = 64, onProgress = null,
|
|
117
|
+
} = {}) {
|
|
118
|
+
const cfg = embedConfig(env);
|
|
119
|
+
if (!cfg) throw new Error('UKIT_INDEX_EMBED_URL is not set; semantic vectors are opt-in');
|
|
120
|
+
const root = path.resolve(rootDir);
|
|
121
|
+
const indexDir = path.join(root, '.cache', 'index');
|
|
122
|
+
const filesArtifact = await readJson(path.join(indexDir, 'files.json'));
|
|
123
|
+
const symbolsArtifact = await readJson(path.join(indexDir, 'symbols.json'));
|
|
124
|
+
if (!filesArtifact?.items) throw new Error('index missing; run the index build first');
|
|
125
|
+
const symbolsByFile = new Map();
|
|
126
|
+
for (const s of symbolsArtifact?.items ?? []) {
|
|
127
|
+
if (!symbolsByFile.has(s.filePath)) symbolsByFile.set(s.filePath, []);
|
|
128
|
+
symbolsByFile.get(s.filePath).push(s.name);
|
|
129
|
+
}
|
|
130
|
+
// Reuse vectors from a store built with the same model and truncation.
|
|
131
|
+
const old = await loadStore(indexDir);
|
|
132
|
+
const reusable = old && old.manifest.model === cfg.model
|
|
133
|
+
&& (!dims || old.manifest.dims === dims) ? old : null;
|
|
134
|
+
const oldByHash = new Map();
|
|
135
|
+
if (reusable) reusable.manifest.order.forEach(([, h], i) => oldByHash.set(h, reusable.bin.subarray(i * reusable.rec, (i + 1) * reusable.rec)));
|
|
136
|
+
|
|
137
|
+
const docs = [];
|
|
138
|
+
for (const item of filesArtifact.items) {
|
|
139
|
+
const filePath = item.filePath;
|
|
140
|
+
if (typeof filePath !== 'string' || isSensitive(filePath)) continue;
|
|
141
|
+
let content = '';
|
|
142
|
+
try { content = await fs.readFile(path.join(root, filePath), 'utf8'); } catch { continue; }
|
|
143
|
+
const text = buildDocumentText({ filePath, symbols: symbolsByFile.get(filePath) ?? [], content });
|
|
144
|
+
docs.push({ filePath, text, hash: sha(`${cfg.model}\0${text}`) });
|
|
145
|
+
}
|
|
146
|
+
const missing = [...new Map(docs.filter((d) => !oldByHash.has(d.hash)).map((d) => [d.hash, d])).values()];
|
|
147
|
+
const fresh = new Map();
|
|
148
|
+
let outDims = dims || reusable?.manifest.dims || 0;
|
|
149
|
+
// Requests are strictly sequential: the embedding service is serial, so parallel requests
|
|
150
|
+
// only queue. Partial progress is saved: a failed batch leaves its files without vectors and
|
|
151
|
+
// the error is rethrown after the store is written, so a rerun only embeds what is missing.
|
|
152
|
+
const failures = [];
|
|
153
|
+
try {
|
|
154
|
+
for (let i = 0; i < missing.length; i += batchSize) {
|
|
155
|
+
const batch = missing.slice(i, i + batchSize);
|
|
156
|
+
const rows = await embedTexts(batch.map((d) => d.text), cfg, { timeoutMs: 180000 });
|
|
157
|
+
if (!outDims) outDims = rows[0].length;
|
|
158
|
+
if (rows[0].length < outDims) throw new Error('endpoint returned fewer dimensions than requested');
|
|
159
|
+
batch.forEach((d, k) => fresh.set(d.hash, rows[k]));
|
|
160
|
+
onProgress?.({ done: fresh.size, total: missing.length });
|
|
161
|
+
}
|
|
162
|
+
} catch (e) { failures.push(e); }
|
|
163
|
+
if (!outDims) outDims = old?.manifest.dims ?? 0;
|
|
164
|
+
if (!outDims && docs.length) throw new Error('no vectors produced');
|
|
165
|
+
|
|
166
|
+
const rec = 4 + outDims;
|
|
167
|
+
const order = docs.filter((d) => fresh.has(d.hash) || oldByHash.has(d.hash)).map((d) => [d.filePath, d.hash]);
|
|
168
|
+
const bin = Buffer.alloc(rec * order.length);
|
|
169
|
+
order.forEach(([, hash], i) => {
|
|
170
|
+
const off = i * rec;
|
|
171
|
+
const prior = oldByHash.get(hash);
|
|
172
|
+
if (prior) { prior.copy(bin, off); return; }
|
|
173
|
+
const v = normalizeTo(fresh.get(hash), outDims);
|
|
174
|
+
const maxAbs = Math.max(...v.map(Math.abs)) || 1;
|
|
175
|
+
bin.writeFloatLE(maxAbs / 127, off);
|
|
176
|
+
for (let k = 0; k < outDims; k += 1) bin.writeInt8(Math.max(-127, Math.min(127, Math.round(v[k] / maxAbs * 127))), off + 4 + k);
|
|
177
|
+
});
|
|
178
|
+
const manifest = { v: 1, model: cfg.model, dims: outDims, createdAt: new Date().toISOString(), order };
|
|
179
|
+
const tmp = `.tmp-${process.pid}`;
|
|
180
|
+
await fs.writeFile(path.join(indexDir, SEMANTIC_BIN + tmp), bin);
|
|
181
|
+
await fs.writeFile(path.join(indexDir, SEMANTIC_MANIFEST + tmp), JSON.stringify(manifest));
|
|
182
|
+
await fs.rename(path.join(indexDir, SEMANTIC_BIN + tmp), path.join(indexDir, SEMANTIC_BIN));
|
|
183
|
+
await fs.rename(path.join(indexDir, SEMANTIC_MANIFEST + tmp), path.join(indexDir, SEMANTIC_MANIFEST));
|
|
184
|
+
if (failures.length) throw new Error(`partial store saved (${order.length}/${docs.length} files); rerun to resume: ${failures[0]?.message ?? failures[0]}`);
|
|
185
|
+
return { files: docs.length, embedded: fresh.size, reused: docs.length - missing.length, dims: outDims, bytes: bin.length, model: cfg.model };
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// Cache-key part: changes when the vector store is built, rebuilt or removed, so a result cached
|
|
189
|
+
// before `embed-index` ran never outlives the vectors. Never throws.
|
|
190
|
+
export async function semanticStoreSignature(rootDir) {
|
|
191
|
+
try {
|
|
192
|
+
const stat = await fs.stat(path.join(path.resolve(rootDir), '.cache', 'index', SEMANTIC_MANIFEST));
|
|
193
|
+
return `${stat.mtimeMs}:${stat.size}`;
|
|
194
|
+
} catch {
|
|
195
|
+
return 'none';
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const STORE_CACHE = new Map();
|
|
200
|
+
|
|
201
|
+
// Query-side ranking. Returns [{ filePath, similarity }] (best first) or null on ANY problem
|
|
202
|
+
// (lane off, no vectors, model/dimension mismatch, timeout, HTTP error). Never throws.
|
|
203
|
+
export async function semanticCandidates({
|
|
204
|
+
rootDir, query, env = process.env, timeoutMs = QUERY_TIMEOUT_MS, limit = 100,
|
|
205
|
+
queryInstruction = QUERY_INSTRUCTION, accept = () => true, weight = () => 1,
|
|
206
|
+
} = {}) {
|
|
207
|
+
try {
|
|
208
|
+
const cfg = embedConfig(env);
|
|
209
|
+
if (!cfg) return null;
|
|
210
|
+
const indexDir = path.join(path.resolve(rootDir), '.cache', 'index');
|
|
211
|
+
const stat = await fs.stat(path.join(indexDir, SEMANTIC_MANIFEST));
|
|
212
|
+
const key = `${indexDir}:${stat.mtimeMs}:${stat.size}`;
|
|
213
|
+
let store = STORE_CACHE.get(key);
|
|
214
|
+
if (!store) { store = await loadStore(indexDir); if (!store) return null; STORE_CACHE.clear(); STORE_CACHE.set(key, store); }
|
|
215
|
+
const { manifest, bin, rec } = store;
|
|
216
|
+
if (manifest.model !== cfg.model) return null;
|
|
217
|
+
const [raw] = await embedTexts([`${queryInstruction}${query}`], cfg, { timeoutMs });
|
|
218
|
+
if (raw.length < manifest.dims) return null;
|
|
219
|
+
const q = normalizeTo(raw, manifest.dims);
|
|
220
|
+
const scored = [];
|
|
221
|
+
for (let i = 0; i < manifest.order.length; i += 1) {
|
|
222
|
+
const filePath = manifest.order[i][0];
|
|
223
|
+
if (!accept(filePath)) continue;
|
|
224
|
+
const off = i * rec;
|
|
225
|
+
let dot = 0;
|
|
226
|
+
for (let k = 0; k < manifest.dims; k += 1) dot += q[k] * bin.readInt8(off + 4 + k);
|
|
227
|
+
scored.push({ filePath, similarity: dot * bin.readFloatLE(off) * weight(filePath) });
|
|
228
|
+
}
|
|
229
|
+
scored.sort((a, b) => b.similarity - a.similarity || a.filePath.localeCompare(b.filePath));
|
|
230
|
+
return scored.slice(0, limit);
|
|
231
|
+
} catch {
|
|
232
|
+
return null;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// Plain-language gate: >= 4 words, no path separators or code punctuation, no quotes/colons,
|
|
237
|
+
// no trailing period (a finished statement is error-message shaped), no error-leading word, and
|
|
238
|
+
// no identifier-shaped token (camelCase, snake_case, kebab with digits, dotted name, file
|
|
239
|
+
// extension, token containing a digit, ALL-CAPS code such as ENOENT). Non-ASCII text
|
|
240
|
+
// (Vietnamese) and ordinary lowercase sentences pass; everything else keeps the lexical result.
|
|
241
|
+
const ERROR_LEADS = /^(?:error|errors|failed|failure|cannot|can't|unable|exception|invalid|unexpected|uncaught|warning|fatal|missing|undefined|null|typeerror|referenceerror)\b/i;
|
|
242
|
+
function identifierShaped(token) {
|
|
243
|
+
const t = token.replace(/^[^\w]+|[^\w]+$/g, '');
|
|
244
|
+
if (!t) return false;
|
|
245
|
+
return /[a-z][A-Z]/.test(t) || /_/.test(t) || /\d/.test(t) || /\w\.\w/.test(t) || /\w-\w*\d/.test(t)
|
|
246
|
+
|| /^[A-Z]{3,}$/.test(t) || /^[A-Z][a-z]+[A-Z]/.test(t) || /\.(?:js|mjs|cjs|ts|tsx|jsx|json|md|py|go|rs|java|rb|yml|yaml|sh)\b/i.test(token);
|
|
247
|
+
}
|
|
248
|
+
export function looksLikeQuestion(query) {
|
|
249
|
+
const q = String(query ?? '').trim();
|
|
250
|
+
const words = q.split(/\s+/).filter(Boolean);
|
|
251
|
+
if (words.length < 4 || /[\\/]|[(){}\[\]=;<>@#$%^&*|~`'"]|::|=>|:/.test(q.replace(/[^\x00-\x7f]/g, ''))) return false;
|
|
252
|
+
if (/[^\x20-\x7e]/.test(q)) return true;
|
|
253
|
+
if (/\.\s*$/.test(q) || ERROR_LEADS.test(q) || words.some(identifierShaped)) return false;
|
|
254
|
+
return true;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// Combine: weighted RRF over the lexical ranking and the semantic list. Semantic-only files
|
|
258
|
+
// are injected with a floor score. Returns null (caller keeps lexical) when no candidates.
|
|
259
|
+
export function fuseSemantic({ ranked, candidates, rrfK = 5, weight = 1, depth = 100 }) {
|
|
260
|
+
if (!candidates?.length) return null;
|
|
261
|
+
const rrf = new Map();
|
|
262
|
+
ranked.slice(0, depth).forEach((e, i) => rrf.set(e.filePath, 1 / (rrfK + i + 1)));
|
|
263
|
+
candidates.slice(0, depth).forEach((c, i) => rrf.set(c.filePath, (rrf.get(c.filePath) ?? 0) + weight / (rrfK + i + 1)));
|
|
264
|
+
const byPath = new Map(ranked.map((e) => [e.filePath, e]));
|
|
265
|
+
const floor = ranked.length ? ranked[ranked.length - 1].score : 0.01;
|
|
266
|
+
// The printed `score` is the fused (RRF) value the order was sorted by, so it is monotonic
|
|
267
|
+
// with rank; the pre-fusion lexical score is kept as `lexicalScore`.
|
|
268
|
+
return [...rrf.entries()]
|
|
269
|
+
.sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0]))
|
|
270
|
+
.map(([filePath, fusedScore]) => {
|
|
271
|
+
const lexical = byPath.get(filePath);
|
|
272
|
+
const score = Number(fusedScore.toFixed(4));
|
|
273
|
+
return lexical
|
|
274
|
+
? { ...lexical, score, lexicalScore: lexical.score }
|
|
275
|
+
: { filePath, score, lexicalScore: Math.min(0.01, floor), reasons: ['semantic:vector'], tests: [] };
|
|
276
|
+
});
|
|
277
|
+
}
|
|
@@ -22,6 +22,7 @@ import path from 'node:path';
|
|
|
22
22
|
import { fileURLToPath } from 'node:url';
|
|
23
23
|
|
|
24
24
|
import { isRunAllRequest } from '../runtime/handoff-intent.mjs';
|
|
25
|
+
import { isStandingPolicyPrompt } from './publication-intent.mjs';
|
|
25
26
|
import { deriveEscalationTriggers, deriveRiskFloor } from './route-resolver.mjs';
|
|
26
27
|
|
|
27
28
|
const PLAYBOOK_NAME_RE = /\.md$/i;
|
|
@@ -223,7 +224,7 @@ export const PLAYBOOK_ROUTING_TABLE = Object.freeze([
|
|
|
223
224
|
fastPath: 'bug-fix',
|
|
224
225
|
// T-026 review fix: a skill-ask that happens to mention a fix ("make this
|
|
225
226
|
// repeated fix flow a skill") is skill-authoring, not a defect order.
|
|
226
|
-
discriminator: (s) => !s.explanationAsk && !s.questionAsk && !s.forensicsSignal && !s.checkFirst && !s.skillAsk,
|
|
227
|
+
discriminator: (s) => !s.explanationAsk && !s.questionAsk && !s.forensicsSignal && !s.checkFirst && !s.skillAsk && !s.standingPolicy,
|
|
227
228
|
},
|
|
228
229
|
{
|
|
229
230
|
workGroup: 'investigation',
|
|
@@ -397,6 +398,7 @@ function matchFirst(text, regexes) {
|
|
|
397
398
|
|
|
398
399
|
async function buildRouteSignals({
|
|
399
400
|
signalText,
|
|
401
|
+
rawPromptText = '',
|
|
400
402
|
executionMode,
|
|
401
403
|
escalationTriggers,
|
|
402
404
|
projectRoot,
|
|
@@ -480,6 +482,8 @@ async function buildRouteSignals({
|
|
|
480
482
|
// BL-026 review fix: ask to convert/author a skill — beats defect verbs in
|
|
481
483
|
// the same sentence ("make this repeated fix flow a skill").
|
|
482
484
|
skillAsk: SKILL_ASK_RE.test(signalText),
|
|
485
|
+
// A standing owner policy about bugs in general names no defect: it must not mint the bug-fix floor.
|
|
486
|
+
standingPolicy: isStandingPolicyPrompt(rawPromptText || signalText),
|
|
483
487
|
};
|
|
484
488
|
}
|
|
485
489
|
|
|
@@ -676,6 +680,7 @@ export async function resolvePlaybookId({
|
|
|
676
680
|
])];
|
|
677
681
|
const signals = await buildRouteSignals({
|
|
678
682
|
signalText,
|
|
683
|
+
rawPromptText: String(promptText || ''),
|
|
679
684
|
executionMode,
|
|
680
685
|
escalationTriggers: mergedTriggers,
|
|
681
686
|
projectRoot,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// Canonical task semantics; intent never grants authorization or proves completion.
|
|
2
2
|
import crypto from 'node:crypto';
|
|
3
|
-
export const SEMANTIC_ENGINE_VERSION = 'round4-semantic-1:
|
|
3
|
+
export const SEMANTIC_ENGINE_VERSION = 'round4-semantic-1:02b15877348961e92a6d682beeb94b0dde7e116fb2b2ba9b5c419147772732f8';
|
|
4
4
|
|
|
5
5
|
export function semanticContractIdentity({ promptText = '', targetFile = '', taskInstanceId = null } = {}) {
|
|
6
6
|
return { taskInstanceId, targetFile, engineVersion: SEMANTIC_ENGINE_VERSION,
|
|
@@ -23,7 +23,24 @@ export function compareSemanticContracts({ stored, fresh, previousIdentity, iden
|
|
|
23
23
|
}
|
|
24
24
|
const foldIntent = (text) => String(text).normalize('NFD').replace(/[̀-ͯ]/g, '').replace(/[đĐ]/g, 'd').toLowerCase();
|
|
25
25
|
|
|
26
|
+
// The analysers are pure functions of their arguments but one hook run asks the same question about the same
|
|
27
|
+
// prompt dozens of times. Memoise per exact input (bounded, insertion-order eviction) and hand every caller its
|
|
28
|
+
// own structuredClone, so no caller can mutate the cache or another caller's result.
|
|
29
|
+
const SEMANTIC_MEMO_LIMIT = 16;
|
|
30
|
+
const memoCloned = (cache, key, compute) => {
|
|
31
|
+
const hit = cache.get(key);
|
|
32
|
+
if (hit !== undefined) return structuredClone(hit);
|
|
33
|
+
const value = compute();
|
|
34
|
+
if (cache.size >= SEMANTIC_MEMO_LIMIT) cache.delete(cache.keys().next().value);
|
|
35
|
+
cache.set(key, structuredClone(value));
|
|
36
|
+
return value;
|
|
37
|
+
};
|
|
38
|
+
const analyzeMemo = new Map();
|
|
26
39
|
export function analyzeTaskIntent({ promptText = '', targetFile = '', taskInstanceId = null, sessionId = null } = {}) {
|
|
40
|
+
return memoCloned(analyzeMemo, JSON.stringify([String(promptText), targetFile, taskInstanceId, sessionId]),
|
|
41
|
+
() => analyzeTaskIntentUncached({ promptText, targetFile, taskInstanceId, sessionId }));
|
|
42
|
+
}
|
|
43
|
+
function analyzeTaskIntentUncached({ promptText = '', targetFile = '', taskInstanceId = null, sessionId = null } = {}) {
|
|
27
44
|
const original = String(promptText);
|
|
28
45
|
const spans = [];
|
|
29
46
|
let fence = null;
|
|
@@ -212,7 +229,11 @@ function classifyProhibitionItem(item, authorized, proper) {
|
|
|
212
229
|
return null;
|
|
213
230
|
}
|
|
214
231
|
|
|
232
|
+
const scopeMemo = new Map();
|
|
215
233
|
export function publicationScope(promptText = '') {
|
|
234
|
+
return memoCloned(scopeMemo, String(promptText), () => publicationScopeUncached(promptText));
|
|
235
|
+
}
|
|
236
|
+
function publicationScopeUncached(promptText = '') {
|
|
216
237
|
const plain = String(promptText).normalize('NFC')
|
|
217
238
|
.replace(/\bdùng\b/giu, 'su dung').replace(/đừng/giu, 'khong')
|
|
218
239
|
.replace(/(^|[\n;.]\s*)([ \t]*(?:[-*+•]|\d+[.)])?[ \t]*)dung(?=\s+(?:publish|release|npm|git|run|fix|sua)\b)/gi, '$1$2khong')
|
|
@@ -533,8 +554,139 @@ const OWNER_STOP_EN = /^(?:please\s+)?(?:stop|halt|pause|cancel|abort)(?:\s+(?:a
|
|
|
533
554
|
const OWNER_STOP_VI = /^(?:(?:tam\s+)?(?:dừng|ngừng)|(?:tạm\s+)?(?:dừng|ngừng)|hủy|huỷ|tam\s+dung|tam\s+ngung|ngung|dung\s+(?:lai|thuc\s+thi|ngay)|huy)(?:\s+(?:thực\s+thi|thuc\s+thi|phiên\s+này|phien\s+nay|phiên|phien|lại|lai|ngay|ngay\s+lập\s+tức|tất\s+cả|tat\s+ca|mọi\s+thứ|moi\s+thu|việc|viec|công\s+việc|cong\s+viec|run|handoff|session|task))*$/;
|
|
534
555
|
const OWNER_CONTROL_TASK_WORD = /\b(?:fix|implement|add|update|edit|write|create|deploy|redeploy|publish|commit|refactor|rewrite|install|sua|sửa|them|thêm|chay|chạy|cap nhat|cập nhật)\b|[\w-]+\/[\w.-]+/;
|
|
535
556
|
const OWNER_CONTROL_TASK_LEAD = /^(?:(?:and\s+)?then|also|afterwards?|after that|next|now|sau do|sau đó|roi|rồi|va|và)\b|^(?:please\s+)?(?:fix|implement|add|update|edit|write|create|run|deploy|redeploy|publish|commit|push|refactor|rewrite|install|build|sua|sửa|them|thêm|chay|chạy|trien khai|triển khai|cap nhat|cập nhật)\b/;
|
|
557
|
+
// A STANDING POLICY states how the owner wants bugs handled in general ("luôn nhớ: có bug là phải fix ngay",
|
|
558
|
+
// "always remember: any bug must be fixed immediately, at its root cause"). It names no defect, so it owes no
|
|
559
|
+
// repro/root-cause contract. CLOSED GRAMMAR: the prompt is split into clauses and returns true ONLY if EVERY
|
|
560
|
+
// non-empty clause, whole and anchored (^...$), is one of the enumerated forms below, and at least one clause is
|
|
561
|
+
// a defect rule. No clause is skipped (only clauses of pure whitespace/punctuation are empty), no clause passes
|
|
562
|
+
// by "all its words are in a set", and no script is exempt: text in any alphabet that matches no form is an
|
|
563
|
+
// order. Any doubt returns false (a real bug order keeps its full evidence duty; a wrong release is the worse
|
|
564
|
+
// error). Global rejects first: concrete referent (path/identifier/quote/version/URL/exit code), a symptom
|
|
565
|
+
// statement, a deictic aimed at a present defect, no policy marker anywhere. The splitter (SP_CLAUSE_SPLIT)
|
|
566
|
+
// cuts on punctuation/newline ONLY and never contains a letter, so no connector word is dropped unjudged;
|
|
567
|
+
// "và" exists only inside form RC. Clause forms (judged on raw NFC text, accents exact, see below):
|
|
568
|
+
// F1 MARK a marker phrase alone ("luôn nhớ", "from now on", "nguyên tắc của tôi")
|
|
569
|
+
// R1 [MARK] (có|thấy|gặp|phát hiện) DEF (là|thì) [phải|luôn] FIX ADV*
|
|
570
|
+
// R2 [MARK] mọi DEF [là|đều] phải FIX ADV*
|
|
571
|
+
// R3 [MARK] (any|every|each|all) DEF (must|should|shall|needs to|has to|gets) [be] FIX ADV*
|
|
572
|
+
// R4 [MARK] bare-plural-DEF (must|should|shall|get) [be] FIX ADV*
|
|
573
|
+
// R5 [MARK] no DEF [luôn] [là (tiêu chí|nguyên tắc|quy tắc|quan điểm) [của tôi]] (nominal slogan)
|
|
574
|
+
// RC (R1|R2|R3|R4) và R6 (rule và purpose)
|
|
575
|
+
// R6 [phải] [tận root cause] để không (tái hiện|xảy ra|lặp lại) DEF nữa (purpose)
|
|
576
|
+
// F3 MARK [vẫn] là phải FIX ADV* (marker REQUIRED; no defect word, no scope word anywhere)
|
|
577
|
+
// F3p quan điểm của tôi [lần chạy này] vẫn là phải FIX ADV* (owner's exact opening)
|
|
578
|
+
// F4 fix [trên ukit] không phải chỉ để pass [thôi] [đâu] (negated purpose)
|
|
579
|
+
// F5 one fixed adverb phrase [nhé], or "nhé" alone ("ngay lập tức", "at its root cause", ...); reachable only after punctuation
|
|
580
|
+
// V1/V2/V3 three enumerated product-rationale sentences ("ukit là sản phẩm tôi sử dụng hỗ trợ rất nhiều sản phẩm khác" ...)
|
|
581
|
+
// ADV* is ONE closed whole-phrase adverb (SP_PHRASE: "ngay lập tức", "triệt để", "at its root cause", ...),
|
|
582
|
+
// optionally followed by "nhé"; never a run of loose words.
|
|
583
|
+
// ACCENTS ARE NEVER FOLDED BEFORE MATCHING. The grammar is written once, in canonical accented spelling; spw()
|
|
584
|
+
// compiles each Vietnamese word to an exact alternation of (its fully unaccented form | its ONE canonical
|
|
585
|
+
// spelling). So "lập" and "lắp"/"lấp"/"lặp", "để" and "đè"/"đẻ", "thấy" and "thay"/"thầy", "nữa" and "nửa" are
|
|
586
|
+
// different words: an accented token that is not the canonical spelling matches nothing. A token with no
|
|
587
|
+
// diacritic still stands for the canonical word (unaccented typing keeps working). English words are ASCII
|
|
588
|
+
// literals, so "FİXED" or "fíx" match nothing. SP_CANONICAL lists every accented word the grammar uses.
|
|
589
|
+
const SP_FOLD = (s) => s.normalize('NFD').replace(/[̀-ͯ]/g, '').replace(/đ/g, 'd');
|
|
590
|
+
const SP_ACCENTED = new Set();
|
|
591
|
+
const spw = (token) => {
|
|
592
|
+
const t = token.normalize('NFC');
|
|
593
|
+
const f = SP_FOLD(t);
|
|
594
|
+
if (f === t) return t;
|
|
595
|
+
SP_ACCENTED.add(t);
|
|
596
|
+
return `(?:${f}|${t})`;
|
|
597
|
+
};
|
|
598
|
+
const sp = (phrase) => phrase.trim().split(/\s+/).map(spw).join('\\s+');
|
|
599
|
+
const spAny = (...phrases) => `(?:${phrases.map(sp).join('|')})`;
|
|
600
|
+
const SP_DEF = `(?:bugs?|${sp('lỗi')}|defects?|crash(?:es)?|errors?|exceptions?|broken|regressions?|failures?|failing|fails?)`;
|
|
601
|
+
const SP_DEF_PLURAL = `(?:bugs|${sp('lỗi')}|defects|crashes|errors|exceptions|regressions|failures)`;
|
|
602
|
+
const SP_FIXV = `(?:fix|fixed|fixes|fixing|${sp('sửa')}|${sp('khắc phục')}|${sp('xử lý')}|resolved?)`;
|
|
603
|
+
// Adverbs are a closed list of whole PHRASES, never a run of words: one primary phrase, then optionally "nhé".
|
|
604
|
+
const SP_PHRASE = spAny('ngay lập tức', 'ngay luôn', 'ngay', 'luôn', 'triệt để', 'tận gốc', 'hoàn toàn', 'tận root cause', 'tận root cáue',
|
|
605
|
+
'at its root cause', 'at the root cause', 'at its root', 'immediately', 'right away', 'at once', 'properly', 'fully', 'completely', 'thoroughly');
|
|
606
|
+
const SP_ADVRUN = `(?:\\s+${SP_PHRASE})?(?:\\s+${sp('nhé')})?`;
|
|
607
|
+
const SP_MARK = `(?:${spAny('luôn nhớ', 'luôn luôn', 'từ giờ', 'từ nay', 'nguyên tắc', 'tiêu chí', 'quy tắc', 'quan điểm',
|
|
608
|
+
'always remember', 'from now on', 'as a rule', 'my rule is', 'my policy is', 'my principle is')})(?:\\s+${sp('của tôi')})?`;
|
|
609
|
+
const SP_MARK_PFX = `(?:${SP_MARK}\\s+)?`;
|
|
610
|
+
// The enumerated clause forms (see the header comment). Every one is anchored at both ends. Connector words
|
|
611
|
+
// (và ...) exist ONLY inside the compound form RC below; the clause splitter never consumes a word.
|
|
612
|
+
const SP_R1 = `${SP_MARK_PFX}${spAny('có', 'thấy', 'gặp', 'phát hiện')}\\s+${SP_DEF}\\s+${spAny('là', 'thì')}\\s+(?:${spAny('phải', 'luôn')}\\s+)?${SP_FIXV}${SP_ADVRUN}`;
|
|
613
|
+
const SP_R2 = `${SP_MARK_PFX}${sp('mọi')}\\s+${SP_DEF}\\s+(?:${sp('là')}\\s+|${sp('đều')}\\s+)?${sp('phải')}\\s+${SP_FIXV}${SP_ADVRUN}`;
|
|
614
|
+
const SP_R3 = `${SP_MARK_PFX}(?:any|every|each|all)\\s+${SP_DEF}\\s+(?:must|should|shall|needs? to|has to|gets?)\\s+(?:be\\s+)?${SP_FIXV}${SP_ADVRUN}`;
|
|
615
|
+
const SP_R4 = `${SP_MARK_PFX}${SP_DEF_PLURAL}\\s+(?:must|should|shall|get)\\s+(?:be\\s+)?${SP_FIXV}${SP_ADVRUN}`;
|
|
616
|
+
const SP_R6 = `(?:${sp('phải')}\\s+)?(?:${spAny('tận root cause', 'tận root cáue')}\\s+)?${sp('để không')}\\s+${spAny('tái hiện', 'xảy ra', 'lặp lại')}\\s+${SP_DEF}\\s+${sp('nữa')}`;
|
|
617
|
+
const SP_CLAUSE_FORMS = [
|
|
618
|
+
new RegExp(`^${SP_MARK}$`), // F1
|
|
619
|
+
new RegExp(`^${SP_R1}$`), // R1
|
|
620
|
+
new RegExp(`^${SP_R2}$`), // R2
|
|
621
|
+
new RegExp(`^${SP_R3}$`), // R3
|
|
622
|
+
new RegExp(`^${SP_R4}$`), // R4
|
|
623
|
+
new RegExp(`^${SP_MARK_PFX}no\\s+${SP_DEF}(?:\\s+${sp('luôn')})?(?:\\s+${sp('là')}\\s+${spAny('tiêu chí', 'nguyên tắc', 'quy tắc', 'quan điểm')}(?:\\s+${sp('của tôi')})?)?$`), // R5
|
|
624
|
+
new RegExp(`^(?:${SP_R1}|${SP_R2}|${SP_R3}|${SP_R4})\\s+${sp('và')}\\s+${SP_R6}$`), // RC: rule và purpose (owner's P2)
|
|
625
|
+
new RegExp(`^${SP_R6}$`), // R6
|
|
626
|
+
new RegExp(`^${SP_MARK}\\s+(?:${sp('vẫn')}\\s+)?${sp('là phải')}\\s+${SP_FIXV}${SP_ADVRUN}$`), // F3 (marker required)
|
|
627
|
+
new RegExp(`^${sp('quan điểm của tôi')}(?:\\s+${sp('lần chạy này')})?\\s+${sp('vẫn là phải')}\\s+${SP_FIXV}${SP_ADVRUN}$`), // F3p
|
|
628
|
+
new RegExp(`^fix(?:\\s+${sp('trên ukit')})?\\s+${sp('không phải chỉ để')}\\s+pass(?:\\s+${sp('thôi')})?(?:\\s+${sp('đâu')})?$`), // F4
|
|
629
|
+
new RegExp(`^(?:${SP_PHRASE}(?:\\s+${sp('nhé')})?|${sp('nhé')})$`), // F5 (reachable only after punctuation)
|
|
630
|
+
new RegExp(`^${sp('ukit là sản phẩm tôi sử dụng hỗ trợ rất nhiều sản phẩm khác')}$`), // V1
|
|
631
|
+
new RegExp(`^${sp('ukit là sản phẩm quan trọng')}$`), // V2
|
|
632
|
+
new RegExp(`^${sp('ukit hỗ trợ rất nhiều sản phẩm khác')}$`), // V3
|
|
633
|
+
];
|
|
634
|
+
// Folded key -> canonical accented spelling(s). One spelling per word; a word with two legitimate tone
|
|
635
|
+
// placements would list both here and in the grammar.
|
|
636
|
+
export const SP_CANONICAL = {};
|
|
637
|
+
for (const w of [...SP_ACCENTED].sort()) (SP_CANONICAL[SP_FOLD(w)] ||= []).push(w);
|
|
638
|
+
// The splitter splits on punctuation/newline ONLY. It must never contain a letter: a word consumed by a
|
|
639
|
+
// splitter is discarded unjudged. (Asserted by a test.)
|
|
640
|
+
export const SP_CLAUSE_SPLIT = /[.!?;:\n,]+/;
|
|
641
|
+
// Forms that state a defect rule: at least one clause must be one of them (R1-R5 and RC).
|
|
642
|
+
const SP_RULE_FORM_COUNT = 7;
|
|
643
|
+
const STANDING_POLICY_MARKER = /\b(?:luon nho|luon luon|tu gio|tu nay|nguyen tac|tieu chi|quy tac|quan diem|always remember|from now on|as a rule|my (?:rule|policy|principle) is)\b/;
|
|
644
|
+
const STANDING_POLICY_CONCRETE = /[`"“”«»]|https?:\/\/|www\.|\b\w+\(\)|[\w.-]+\/[\w.-]+|\b[\w-]+\.(?:m?js|cjs|jsx?|tsx?|json|md|sh|py|ya?ml|css|html?|go|rs|java|rb|php|sql|toml|txt|lock|log)\b|\b[a-z]+[A-Z]\w*|\b\w+_\w+|\b\w*Error\b|\bE[A-Z]{3,}\b|\bv?\d+\.\d+|\bexit\s*(?:code|status)?\s*:?\s*-?\d+|\bstack\b|\b\d{3,}\b/;
|
|
645
|
+
const STANDING_POLICY_SYMPTOM = /\b(?:bi loi|khong chay|khong hoat dong|hong|treo|sai|van con|chua (?:duoc )?(?:fix|sua)|crash(?:es|ed)?|(?:does ?n[o']?t|won'?t|is ?n[o']?t) work|(?:is|are) broken|fail(?:s|ed|ing)?)\b/;
|
|
646
|
+
const STANDING_POLICY_DEICTIC_ANYWHERE = /\b(?:the above|above|vua roi|hien tai|current(?:ly)?|dang|failing|following|ben tren|o tren)\b/;
|
|
647
|
+
export function isStandingPolicyPrompt(promptText = '') {
|
|
648
|
+
const raw = String(promptText ?? '').trim();
|
|
649
|
+
if (!raw || raw.length > 800 || STANDING_POLICY_CONCRETE.test(raw)) return false;
|
|
650
|
+
// The global rejects (marker present, no symptom, no deictic) are deliberately lenient and run on a folded
|
|
651
|
+
// copy; the clause forms below run on the RAW lowercase NFC text, where accents are exact. Nothing but
|
|
652
|
+
// Latin combining marks and "đ" is ever removed from the folded copy, and the raw text is what is judged.
|
|
653
|
+
const text = raw.toLowerCase().normalize('NFC').replace(/\s+/g, ' ');
|
|
654
|
+
const folded = SP_FOLD(text);
|
|
655
|
+
if (!STANDING_POLICY_MARKER.test(folded) || STANDING_POLICY_DEICTIC_ANYWHERE.test(folded) || STANDING_POLICY_SYMPTOM.test(folded)) return false;
|
|
656
|
+
const clauses = text.split(SP_CLAUSE_SPLIT);
|
|
657
|
+
let ruleClauses = 0;
|
|
658
|
+
for (const clause of clauses) {
|
|
659
|
+
const norm = clause.replace(/\s+/g, ' ').trim();
|
|
660
|
+
// Only a clause that is empty after whitespace/ASCII punctuation is skipped. Anything else (a letter or
|
|
661
|
+
// digit of any script, an emoji, a symbol) must match an enumerated form, else the prompt is not a policy.
|
|
662
|
+
if (!norm.replace(/[\s!-/:-@[-`{-~]/g, '')) continue;
|
|
663
|
+
const form = SP_CLAUSE_FORMS.findIndex((re) => re.test(norm));
|
|
664
|
+
if (form < 0) return false;
|
|
665
|
+
if (form >= 1 && form < SP_RULE_FORM_COUNT) ruleClauses += 1;
|
|
666
|
+
}
|
|
667
|
+
return ruleClauses > 0;
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
// A host-delivered peer (teammate/subagent/other-session) message: optional banner line, one or more CLOSED
|
|
671
|
+
// peer blocks, then nothing or (banner required) the host's own single-paragraph trailer, recognised by its
|
|
672
|
+
// stable lead words. Owner words anywhere else make it an owner prompt. A host wording change falls back to
|
|
673
|
+
// "not a peer prompt" (old routing), never to dropping owner text. Mirrors the early-exit check in skill-router.sh.
|
|
674
|
+
export function isPeerMessagePrompt(promptText = '') {
|
|
675
|
+
const text = String(promptText ?? '').trim();
|
|
676
|
+
const banner = /^Another Claude session sent a message:[ \t]*\r?\n/i;
|
|
677
|
+
const bannered = banner.test(text);
|
|
678
|
+
let rest = text.replace(banner, '').trim();
|
|
679
|
+
const peerBlock = /^<(agent-message|teammate-message|cross-session-message)(?:\s[^>]*)?>[\s\S]*?<\/\1>\s*/;
|
|
680
|
+
let blocks = 0;
|
|
681
|
+
for (let m = peerBlock.exec(rest); m; m = peerBlock.exec(rest)) { rest = rest.slice(m[0].length); blocks += 1; }
|
|
682
|
+
const trailer = /^(?:This came from another Claude session|That .other Claude session. is an agent working inside this same session)[^\r\n]{0,1500}$/i;
|
|
683
|
+
return blocks > 0 && (rest === '' || (bannered && trailer.test(rest.trim())));
|
|
684
|
+
}
|
|
685
|
+
|
|
536
686
|
export function classifyOwnerControl(promptText = '') {
|
|
537
687
|
const raw = String(promptText ?? '').normalize('NFC').trim();
|
|
688
|
+
// Text shape is never provenance here: an envelope-first/banner-first text already yields kind null because
|
|
689
|
+
// OWNER_STOP_EN/VI are anchored on the first lead (proved 0 diffs vs the former isPeerMessagePrompt guard).
|
|
538
690
|
if (!raw || OWNER_CONTROL_HOOK_TEXT.test(raw)) return { kind: null };
|
|
539
691
|
const stripped = raw.replace(/^[\s*#_-]+/, '');
|
|
540
692
|
if (/^[>"'`\u201c\u2018]/.test(stripped)) return { kind: null };
|
|
@@ -726,7 +878,13 @@ function classifySemanticCore({ promptText = '', targetFile = '' } = {}) {
|
|
|
726
878
|
const cronOperation = actionable.some((clause) => /\b(?:delete|remove|cancel|xoa|huy|ensure|dam bao)\b/.test(clause)
|
|
727
879
|
&& /\bcron\b|scheduled\s+(?:cron\s+)?(?:task|job)|tac vu\s+(?:cron|dinh ky)/.test(clause))
|
|
728
880
|
&& (!codeChangeVerb || hasOperationId);
|
|
881
|
+
// Pure per clause and asked once by runtimeClause and once by gitClause for every clause; compute each clause once.
|
|
882
|
+
const operationNotOrderedMemo = new Map();
|
|
729
883
|
const operationNotOrdered = (clause) => {
|
|
884
|
+
if (!operationNotOrderedMemo.has(clause)) operationNotOrderedMemo.set(clause, operationNotOrderedUncached(clause));
|
|
885
|
+
return operationNotOrderedMemo.get(clause);
|
|
886
|
+
};
|
|
887
|
+
const operationNotOrderedUncached = (clause) => {
|
|
730
888
|
const OP = '(?:restart|redeploy|deploy(?:ment)?|trien khai|khoi dong lai|git\\s+push|push\\s+(?:to\\s+)?origin|commit|push)';
|
|
731
889
|
const BAN = '(?:bi\\s+(?:cam|chan)|forbidden|prohibited|banned|not\\s+allowed|not\\s+permitted|disallowed)';
|
|
732
890
|
if (/\b(?:was|were|previously|before|formerly|no\s+longer|even\s+though|although|though|now\s+allows?|now\s+permits?|owner\s+allows?|khong\s+bi|da\s+duoc\s+phep|except)\b/.test(clause)) return false;
|