Harden graph refresh defaults and smoke tests
This commit is contained in:
@@ -3,14 +3,79 @@ set -euo pipefail
|
||||
|
||||
ROOT="${1:-$(cd "$(dirname "$0")/.." && pwd)}"
|
||||
|
||||
if rg -n '/Users/fil|72\.56|root@|Полезные знания|_ВХОДЯЩИЕ|TODOCUPS|ПКК|legal-practice|Adventure Book|AthenaOS|Филипп' "$ROOT" -g '!node_modules/**' -g '!.git/**' -g '!scripts/check-public-release.sh'; then
|
||||
echo "Public release check failed: private marker found." >&2
|
||||
# Private markers (machine paths, IPs, personal/project names) that must never ship publicly.
|
||||
PRIVATE_MARKERS='/Users/fil|72\.56|root@|Полезные знания|_ВХОДЯЩИЕ|TODOCUPS|ПКК|legal-practice|Adventure Book|AthenaOS|Филипп'
|
||||
# Token-like values (GitHub OAuth, OpenAI/Anthropic-style sk-, Slack xox*).
|
||||
TOKEN_MARKERS='gho_[A-Za-z0-9_]+|sk-[A-Za-z0-9_-]+|xox[baprs]-[A-Za-z0-9-]+'
|
||||
|
||||
# Pick a scanner. Prefer ripgrep; fall back to POSIX grep so the gate still runs
|
||||
# on machines without rg. A missing scanner must HARD-FAIL, never silently pass.
|
||||
if command -v rg >/dev/null 2>&1; then
|
||||
SCANNER="rg"
|
||||
elif command -v grep >/dev/null 2>&1; then
|
||||
SCANNER="grep"
|
||||
else
|
||||
echo "Public release check errored: neither 'rg' nor 'grep' is available to scan." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if rg -n 'gho_[A-Za-z0-9_]+|sk-[A-Za-z0-9_-]+|xox[baprs]-[A-Za-z0-9-]+' "$ROOT" -g '!node_modules/**' -g '!.git/**' -g '!scripts/check-public-release.sh'; then
|
||||
echo "Public release check failed: token-like value found." >&2
|
||||
exit 1
|
||||
TRACKED_ONLY=0
|
||||
if git -C "$ROOT" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
|
||||
TRACKED_ONLY=1
|
||||
fi
|
||||
|
||||
tracked_files() {
|
||||
local files=()
|
||||
while IFS= read -r -d '' file; do
|
||||
[ "$file" = "scripts/check-public-release.sh" ] && continue
|
||||
files+=("$ROOT/$file")
|
||||
done < <(git -C "$ROOT" ls-files -z)
|
||||
printf '%s\0' "${files[@]}"
|
||||
}
|
||||
|
||||
# scan PATTERN: print matches under $ROOT, excluding vendored/VCS noise and this
|
||||
# script itself (it embeds the marker patterns). Exit: 0 = match, 1 = clean, >=2 = scanner error.
|
||||
scan() {
|
||||
local pattern="$1"
|
||||
if [ "$TRACKED_ONLY" -eq 1 ]; then
|
||||
local files=()
|
||||
while IFS= read -r -d '' file; do
|
||||
files+=("$file")
|
||||
done < <(tracked_files)
|
||||
if [ "${#files[@]}" -eq 0 ]; then
|
||||
return 1
|
||||
fi
|
||||
if [ "$SCANNER" = "rg" ]; then
|
||||
rg -n "$pattern" "${files[@]}"
|
||||
else
|
||||
grep -En "$pattern" "${files[@]}"
|
||||
fi
|
||||
elif [ "$SCANNER" = "rg" ]; then
|
||||
rg -n "$pattern" "$ROOT" \
|
||||
-g '!node_modules/**' -g '!.git/**' -g '!scripts/check-public-release.sh'
|
||||
else
|
||||
grep -rEn "$pattern" "$ROOT" \
|
||||
--exclude-dir=node_modules --exclude-dir=.git --exclude=check-public-release.sh
|
||||
fi
|
||||
}
|
||||
|
||||
# check PATTERN LABEL: fail on a match; also fail (never pass) if the scanner
|
||||
# itself errors — a security gate must never read a tooling failure as green.
|
||||
check() {
|
||||
local pattern="$1"
|
||||
local label="$2"
|
||||
local status=0
|
||||
scan "$pattern" || status=$?
|
||||
if [ "$status" -eq 0 ]; then
|
||||
echo "Public release check failed: $label found." >&2
|
||||
exit 1
|
||||
elif [ "$status" -ge 2 ]; then
|
||||
echo "Public release check errored: scanner '$SCANNER' failed (exit $status) while checking $label." >&2
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
check "$PRIVATE_MARKERS" "private marker"
|
||||
check "$TOKEN_MARKERS" "token-like value"
|
||||
|
||||
echo "Public release check passed."
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
#!/usr/bin/env node
|
||||
import { promises as fs } from 'node:fs'
|
||||
import path from 'node:path'
|
||||
import os from 'node:os'
|
||||
import { spawn } from 'node:child_process'
|
||||
|
||||
const ROOT = path.resolve(process.cwd())
|
||||
const HELPER = path.join(ROOT, 'scripts', 'mnemazine-refresh-graphify.mjs')
|
||||
|
||||
function runNode(args, timeoutMs = 120000) {
|
||||
return new Promise(resolve => {
|
||||
const child = spawn(process.execPath, args, {
|
||||
cwd: ROOT,
|
||||
env: process.env,
|
||||
stdio: ['ignore', 'pipe', 'pipe']
|
||||
})
|
||||
let stdout = ''
|
||||
let stderr = ''
|
||||
let timedOut = false
|
||||
const timer = setTimeout(() => {
|
||||
timedOut = true
|
||||
child.kill('SIGTERM')
|
||||
setTimeout(() => child.kill('SIGKILL'), 5000).unref()
|
||||
}, timeoutMs)
|
||||
child.stdout.on('data', chunk => { stdout += String(chunk) })
|
||||
child.stderr.on('data', chunk => { stderr += String(chunk) })
|
||||
child.on('close', code => {
|
||||
clearTimeout(timer)
|
||||
resolve({ code: code ?? 1, stdout, stderr, timedOut })
|
||||
})
|
||||
child.on('error', error => {
|
||||
clearTimeout(timer)
|
||||
resolve({ code: 1, stdout, stderr: `${stderr}\n${error.message}`, timedOut })
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function assert(condition, message) {
|
||||
if (!condition) throw new Error(message)
|
||||
}
|
||||
|
||||
const tempRoot = await fs.mkdtemp(path.join(os.tmpdir(), 'mnemazine-refresh-smoke-'))
|
||||
const vault = path.join(tempRoot, 'vault')
|
||||
await fs.mkdir(vault, { recursive: true })
|
||||
await fs.writeFile(path.join(vault, 'sample.js'), 'export const answer = 42\n', 'utf8')
|
||||
await fs.writeFile(path.join(vault, 'sample.md'), '# Sample\n\n## What This Is\n\nSmoke note.\n\n## Source\n\n- local\n', 'utf8')
|
||||
|
||||
const codeRun = await runNode([HELPER, '--vault', vault, '--mode', 'code', '--json'], 180000)
|
||||
assert(codeRun.code === 2, `expected code mode exit 2 for pending semantic refresh, got ${codeRun.code}`)
|
||||
const codeJson = JSON.parse(codeRun.stdout)
|
||||
assert(codeJson.code_refresh?.ok === true, 'code refresh should be ok in code mode')
|
||||
assert(codeJson.semantic_pending_after === true, 'code mode should leave semantic pending on docs corpus')
|
||||
|
||||
const unsupportedRun = await runNode([HELPER, '--vault', vault, '--mode', 'semantic', '--backend', 'fake', '--json'], 180000)
|
||||
assert(unsupportedRun.code === 2, `expected semantic unsupported backend exit 2, got ${unsupportedRun.code}`)
|
||||
const unsupportedJson = JSON.parse(unsupportedRun.stdout)
|
||||
assert(unsupportedJson.semantic_refresh?.status === 'unsupported_backend', 'semantic mode should reject unsupported backend')
|
||||
|
||||
await fs.rm(tempRoot, { recursive: true, force: true })
|
||||
console.log(JSON.stringify({
|
||||
ok: true,
|
||||
checks: [
|
||||
'code mode leaves semantic pending on docs corpus',
|
||||
'semantic mode rejects unsupported backend cleanly'
|
||||
]
|
||||
}, null, 2))
|
||||
@@ -1,6 +1,6 @@
|
||||
#!/usr/bin/env node
|
||||
import { promises as fs } from 'node:fs'
|
||||
import { existsSync } from 'node:fs'
|
||||
import { existsSync, readFileSync } from 'node:fs'
|
||||
import path from 'node:path'
|
||||
import os from 'node:os'
|
||||
import { spawn } from 'node:child_process'
|
||||
@@ -18,17 +18,22 @@ function hasFlag(name) {
|
||||
}
|
||||
|
||||
const ROOT = path.resolve(process.cwd())
|
||||
const VAULT = path.resolve(arg('vault', process.env.MNEMAZINE_VAULT || path.join(os.homedir(), 'Полезные знания')))
|
||||
const CONFIG_PATH = path.join(ROOT, 'config', 'graphify-refresh.json')
|
||||
const CONFIG = existsSync(CONFIG_PATH)
|
||||
? JSON.parse(readFileSync(CONFIG_PATH, 'utf8'))
|
||||
: {}
|
||||
const VAULT = path.resolve(arg('vault', process.env.MNEMAZINE_VAULT || path.join(ROOT, 'vault')))
|
||||
const MODE = arg('mode', 'auto')
|
||||
const BACKEND = arg('backend', 'ollama')
|
||||
const MODEL = arg('model', process.env.MNEMAZINE_GRAPHIFY_MODEL || 'qwen:32b')
|
||||
const MODEL_LADDER = (arg('models', process.env.MNEMAZINE_GRAPHIFY_MODELS || `${MODEL},gemma2:9b,qwen2.5-coder:7b`)
|
||||
const BACKEND = arg('backend', process.env.MNEMAZINE_GRAPHIFY_BACKEND || CONFIG.backend || 'ollama')
|
||||
const MODEL = arg('model', process.env.MNEMAZINE_GRAPHIFY_MODEL || CONFIG.model || 'qwen:32b')
|
||||
const CONFIG_MODELS = Array.isArray(CONFIG.models) ? CONFIG.models.join(',') : ''
|
||||
const MODEL_LADDER = (arg('models', process.env.MNEMAZINE_GRAPHIFY_MODELS || CONFIG_MODELS || `${MODEL},gemma2:9b,qwen2.5-coder:7b`)
|
||||
.split(',')
|
||||
.map(item => item.trim())
|
||||
.filter(Boolean))
|
||||
const TIMEOUT_MS = Number(arg('timeout-seconds', '900')) * 1000
|
||||
const SMOKE_TIMEOUT_MS = Number(arg('smoke-timeout-seconds', '120')) * 1000
|
||||
const SHRINK_THRESHOLD = Number(arg('shrink-threshold', '0.85'))
|
||||
const TIMEOUT_MS = Number(arg('timeout-seconds', String(CONFIG.timeout_seconds || '900'))) * 1000
|
||||
const SMOKE_TIMEOUT_MS = Number(arg('smoke-timeout-seconds', String(CONFIG.smoke_timeout_seconds || '120'))) * 1000
|
||||
const SHRINK_THRESHOLD = Number(arg('shrink-threshold', String(CONFIG.shrink_threshold || '0.85')))
|
||||
const JSON_OUT = hasFlag('json')
|
||||
const GRAPHIFY_OUT = path.join(VAULT, 'graphify-out')
|
||||
const GRAPH_PATH = path.join(GRAPHIFY_OUT, 'graph.json')
|
||||
@@ -404,6 +409,7 @@ async function main() {
|
||||
ok: true,
|
||||
vault: VAULT,
|
||||
mode: MODE,
|
||||
config_path: existsSync(CONFIG_PATH) ? CONFIG_PATH : null,
|
||||
backend: BACKEND,
|
||||
model: MODEL,
|
||||
model_ladder: unique(MODEL_LADDER),
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#!/usr/bin/env node
|
||||
import { promises as fs } from 'node:fs'
|
||||
import path from 'node:path'
|
||||
import os from 'node:os'
|
||||
|
||||
const ROOT = process.env.MNEMAZINE_ROOT || path.resolve(process.cwd())
|
||||
const argv = process.argv.slice(2)
|
||||
@@ -15,7 +16,7 @@ const VAULT = path.resolve(arg('vault', process.env.MNEMAZINE_VAULT || path.join
|
||||
|
||||
const badMarkers = [
|
||||
/raw\s+ocr/i,
|
||||
/сырой\s+ocr/i,
|
||||
/сырой\s+ocr(?!\s+исключ[её]н)/i,
|
||||
/распознанный\s+текст\s+без\s+обработки/i,
|
||||
/lorem ipsum/i,
|
||||
/TODO:\s*rewrite/i,
|
||||
@@ -26,7 +27,7 @@ async function walk(dir) {
|
||||
const out = []
|
||||
for (const item of await fs.readdir(dir, { withFileTypes: true }).catch(() => [])) {
|
||||
const p = path.join(dir, item.name)
|
||||
if (item.isDirectory()) out.push(...await walk(p))
|
||||
if (item.isDirectory() && !['.git', '.obsidian', 'graphify-out'].includes(item.name)) out.push(...await walk(p))
|
||||
else if (item.isFile() && p.endsWith('.md')) out.push(p)
|
||||
}
|
||||
return out
|
||||
@@ -35,12 +36,23 @@ async function walk(dir) {
|
||||
const files = await walk(VAULT)
|
||||
const failures = []
|
||||
for (const file of files) {
|
||||
const rel = path.relative(VAULT, file)
|
||||
if (
|
||||
rel.includes('/graphify-out/') ||
|
||||
rel.includes('/_legacy-index ') ||
|
||||
rel.includes('/_архив-дублей/') ||
|
||||
rel.includes('/Capabilities/_') ||
|
||||
/^99 Система\//.test(rel) ||
|
||||
/(^|\/)_(Содержание|МАСТЕР-ИНДЕКС|ROUTING|ШАБЛОНЫ)\.md$/.test(rel) ||
|
||||
['AGENTS.md', 'CLAUDE.md', '_ROUTING.md', 'Лог обработки.md'].includes(rel)
|
||||
) continue
|
||||
const text = await fs.readFile(file, 'utf8')
|
||||
if (/^##\s+Атомизировано(?:\s|$)/m.test(text)) continue
|
||||
const hit = badMarkers.find(re => re.test(text))
|
||||
const hasSource = /## Source|## Источник|source:/i.test(text)
|
||||
const hasMeaning = /## What This Is|## Что это|## Суть/i.test(text)
|
||||
const hasSource = /#{2,}\s+(Source|Sources|Источник|Источники|Источники и подтверждения)(?:\s|$)|source:/i.test(text)
|
||||
const hasMeaning = /#{2,}\s+(What This Is|Что это|Что это и зачем|Суть)(?:\s|$)/i.test(text)
|
||||
if (hit || !hasSource || !hasMeaning) {
|
||||
failures.push({ file: path.relative(VAULT, file), marker: hit ? String(hit) : 'missing required sections' })
|
||||
failures.push({ file: rel, marker: hit ? String(hit) : 'missing required sections' })
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user