Files
cc-1c-skills/hooks/common/support-state.mjs
T
Nick Shirokov ddc1641176 fix(support-guard): не блокировать автономные внешние обработки/отчёты (#39)
При поиске корня конфигурации guard поднимался по дереву вверх и «проскакивал»
собственный корень автономной внешней обработки/отчёта (ExternalDataProcessor /
ExternalReport), лежащей внутри дерева выгрузки конфигурации. Если у охватывающей
конфигурации выключена возможность изменения (G=1), внешний объект ложно
блокировался как «объект типовой конфигурации на поддержке», а info-навыки
выводили нерелевантную строку «Поддержка: конфигурация read-only».

Теперь climb останавливается на границе автономного объекта: если целевой файл или
встреченный по пути <каталог>.xml имеет корень ExternalDataProcessor/ExternalReport,
подъём прекращается и объект не привязывается к конфигурации. Корень внешнего объекта
всегда глубже Configuration.xml, поэтому встречается первым — регрессии для обычных
объектов конфигурации нет.

Синхронно во всех копиях guard-а (навыки автономны): хук support-state.mjs
(decideSupport + findConfigRoot), 16 мутаторов (Assert-EditAllowed), 5 info-навыков
и meta-info (Get-SupportStatusForPath / Get-ObjectSupportStatus) — ps1 и py. Для
info-навыков строка «Поддержка:» для внешнего объекта опускается.

Тесты: hooks/test/run.mjs — секция внешней границы (G=1 + встроенная EPF);
tests/skills — кейсы mxl-compile (guard пропускает) и mxl-info (строка опущена).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-17 19:46:54 +03:00

171 lines
7.2 KiB
JavaScript

// support-state.mjs v1.1 — decode 1C support state (Ext/ParentConfigurations.bin) for Claude Code hooks
// Source: https://github.com/Nikolay-Shirokov/cc-1c-skills
//
// Canonical port of the in-skill guard Assert-EditAllowed / assert_edit_allowed
// (reference: .claude/skills/meta-edit/scripts/meta-edit.ps1:160-261, meta-edit.py:22-148).
// See docs/1c-support-state-spec.md. Detects whether a target file lives under a
// vendor configuration on support and whether editing it is blocked. Never throws —
// any decode error degrades to "not blocked" (allow). configSrc / .v8-project.json
// are NOT used here (reaction lookup lives in project.mjs); the config root is found
// purely by walking up to Ext/ParentConfigurations.bin.
import { readFileSync, existsSync, statSync } from 'node:fs';
import { dirname, join } from 'node:path';
const GUID_RE = /\buuid="([0-9a-fA-F-]{36})"/;
// Root child of an autonomous external object dump (EPF/ERF). Such an object is never
// part of a configuration on support, even when its dump sits inside the config tree —
// the climb must stop at this boundary instead of attributing it to the enclosing config.
const EXT_ROOT_RE = /<MetaDataObject\b[^>]*>\s*<(?:\w+:)?(?:ExternalDataProcessor|ExternalReport)\b/;
// True when xmlPath is the root file of an autonomous ExternalDataProcessor / ExternalReport.
export function isExternalObjectRoot(xmlPath) {
try {
if (!existsSync(xmlPath) || !statSync(xmlPath).isFile()) return false;
return EXT_ROOT_RE.test(readFileSync(xmlPath, 'utf8').slice(0, 1000));
} catch {
return false;
}
}
// First uuid="..." in an object XML == root element uuid (the <MetaDataObject> wrapper
// carries none), matching the reference's "first element child uuid" semantics.
export function rootUuid(xmlPath) {
try {
if (!existsSync(xmlPath) || !statSync(xmlPath).isFile()) return null;
const text = readFileSync(xmlPath, 'utf8');
const m = GUID_RE.exec(text);
return m ? m[1] : null;
} catch {
return null;
}
}
// Walk up from startPath (a file or dir) to find the configuration root: the directory
// that holds Ext/ParentConfigurations.bin or Configuration.xml. Returns
// { cfgDir, binPath, isExtension } or nulls. isExtension is positive recognition via
// <ConfigurationExtensionPurpose> in Configuration.xml (spec §1) — distinguishes an
// extension (no real support) from "support fully removed" (bin also near-empty).
export function findConfigRoot(startPath) {
let cfgDir = null, binPath = null, configXml = null;
let d = startPath;
try {
d = existsSync(startPath) && statSync(startPath).isDirectory() ? startPath : dirname(startPath);
} catch {
d = dirname(startPath);
}
// startPath itself may be the external object root file.
if (isExternalObjectRoot(startPath)) return { cfgDir: null, binPath: null, isExtension: false };
for (let i = 0; i < 12 && d; i++) {
// Crossed the boundary of an autonomous external object → not part of any config.
if (isExternalObjectRoot(d + '.xml')) return { cfgDir: null, binPath: null, isExtension: false };
const cand = join(d, 'Ext', 'ParentConfigurations.bin');
const cfgX = join(d, 'Configuration.xml');
if (existsSync(cand) || existsSync(cfgX)) {
cfgDir = d;
binPath = cand;
configXml = existsSync(cfgX) ? cfgX : null;
break;
}
const parent = dirname(d);
if (parent === d) break;
d = parent;
}
let isExtension = false;
if (configXml) {
try {
isExtension = readFileSync(configXml, 'utf8').includes('ConfigurationExtensionPurpose');
} catch { /* ignore */ }
}
return { cfgDir, binPath, isExtension };
}
// Decode the bin header + per-object rules and apply the support rule for `require`
// ('editable' — blocked if locked f1=0; 'removed' — blocked unless f1=2).
// Returns { blocked, reason, code, cfgDir, targetPath }. `code` discriminates the cause
// ('capability-off' | 'locked' | 'not-removed') so callers can tailor the remedy.
// Never throws.
export function decideSupport(targetPath, require = 'editable') {
const result = { blocked: false, reason: '', code: null, cfgDir: null, targetPath };
try {
// Autonomous external object (EPF/ERF): its root is never part of a configuration on
// support, even when the dump sits inside the config tree — do not attribute it (issue #39).
if (isExternalObjectRoot(targetPath)) return result;
let elemUuid = rootUuid(targetPath);
// Walk up: collect elemUuid (from <dir>.xml of a sub-element) and the config root.
let cfgDir = null, binPath = null;
let d;
try {
d = existsSync(targetPath) && statSync(targetPath).isDirectory() ? targetPath : dirname(targetPath);
} catch {
d = dirname(targetPath);
}
for (let i = 0; i < 12 && d; i++) {
// Crossed the boundary of an autonomous external object → allow (not on support).
if (isExternalObjectRoot(d + '.xml')) return result;
if (!elemUuid) elemUuid = rootUuid(d + '.xml');
if (!cfgDir) {
const cand = join(d, 'Ext', 'ParentConfigurations.bin');
if (existsSync(cand) || existsSync(join(d, 'Configuration.xml'))) {
cfgDir = d;
binPath = cand;
}
}
if (elemUuid && cfgDir) break;
const parent = dirname(d);
if (parent === d) break;
d = parent;
}
result.cfgDir = cfgDir;
// New object (no element file): fall back to config root uuid.
if (!elemUuid && cfgDir) elemUuid = rootUuid(join(cfgDir, 'Configuration.xml'));
if (!binPath || !existsSync(binPath)) return result;
let data = readFileSync(binPath);
if (data.length <= 32) return result;
if (data.length >= 3 && data[0] === 0xef && data[1] === 0xbb && data[2] === 0xbf) data = data.subarray(3);
const text = data.toString('utf8');
const h = /^\{6,(\d+),(\d+),/.exec(text);
if (!h) return result;
const G = parseInt(h[1], 10);
const K = parseInt(h[2], 10);
if (K === 0) return result;
let best = null;
if (elemUuid) {
const re = new RegExp('([0-2]),0,' + escapeRe(elemUuid.toLowerCase()), 'g');
let m;
while ((m = re.exec(text)) !== null) {
const f1 = parseInt(m[1], 10);
if (best === null || f1 < best) best = f1;
}
}
if (G === 1) {
result.blocked = true;
result.code = 'capability-off';
result.reason = 'возможность изменения конфигурации выключена (вся конфигурация read-only)';
} else if (require === 'removed') {
if (best !== null && best !== 2) {
result.blocked = true;
result.code = 'not-removed';
result.reason = 'объект на поддержке (не снят с поддержки) — удаление сломает обновления';
}
} else {
if (best !== null && best === 0) {
result.blocked = true;
result.code = 'locked';
result.reason = 'объект на замке (поддержка поставщика) — прямое редактирование сломает обновления';
}
}
return result;
} catch {
return result;
}
}
function escapeRe(s) {
return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}