Skip to content

armgit / Orkas

Public Imported from GitHub
0

Orkas is an open-source (MIT), local-first AI team for your desktop: a Commander turns your goal into to-dos and coordinates 16 built-in specialist agents plus Claude Code, Codex, OpenCode, OpenClaw and Hermes, with projects, scheduled automations and connectors. Free app for macOS & Windows; built-in models or your own keys.

Harden custom quality policies and wire desktop write gates

wyt978079f0ad46
diff --git a/src/main/features/agents.ts b/src/main/features/agents.ts
index 4c3c60d3..5727599a 100644
--- a/src/main/features/agents.ts
+++ b/src/main/features/agents.ts
@@ -15,6 +15,7 @@
* field.
*/
+import { validateWithOperatorPolicy } from './operator-policy';
import { addEntryWithMaintenance, replaceEntryWithMaintenance } from './memory-maintenance';
import * as crypto from 'node:crypto';
import * as fs from 'node:fs';
@@ -82,7 +83,7 @@ import {
} from '../model/core-agent/skill-registry';
import { readDisabledSets, setAgentEnabled } from './component_enabled';
import { renameAgentInMembers } from './group_chat/state';
-import { validateAgentSpec, ValidationReport as QualityReport } from '../quality';
+import { ValidationReport as QualityReport } from '../quality';
import { persistReport as persistQualityReport } from '../quality/report';
import {
DEFAULT_MARKETPLACE_CATEGORY_CODE,
@@ -1844,8 +1845,6 @@ export async function createCustomAgent(
do { agentId = genAgentId(); }
while (fs.existsSync(path.join(userMarketplaceAgentDir(userId, agentId), 'agent.json'))
|| fs.existsSync(agentDefinitionFile(userId, agentId)));
- // mkdir <aid>/ first so writeJson on agent.json has a parent.
- fs.mkdirSync(agentDir(userId, agentId), { recursive: true });
const desc = resolveBilingualDescription(description, description_zh, description_en);
const normalizedWorkflow = await _normalizeWorkflowSkillIds(String(workflow || ''), userId);
const data: AgentRaw = {
@@ -1905,18 +1904,15 @@ export async function createCustomAgent(
// flags in workflow text / etc.) block the write; MEDIUM warnings pass
// through but are persisted for UI surfacing. Persist runs in both branches
// so the latest report on disk always matches the most recent intent.
- const report = validateAgentSpec({ agentJson: data });
+ const [report] = await validateWithOperatorPolicy(userId, { kind: 'agent', args: { agentJson: data } });
void persistQualityReport({
uid: userId, kind: 'agent', id: agentId, report,
});
if (!report.ok) {
- // Roll back the freshly-mkdir'd directory so a rejected create doesn't
- // leave behind an empty <aid>/ that would later confuse `listAgents`.
- try { fs.rmSync(agentDir(userId, agentId), { recursive: true, force: true }); }
- catch { /* tolerate cleanup failure */ }
throw new Error(_validationErrorMessage(report));
}
+ fs.mkdirSync(agentDir(userId, agentId), { recursive: true });
await writeJson(agentDefinitionFile(userId, agentId), data);
_invalidateAgentListCache({ userId });
log.info('custom agent created', { agent_id: maskId(agentId) });
@@ -2127,7 +2123,7 @@ export async function updateCustomAgent(
// Quality gate (same policy as createCustomAgent): EXTREME blocks the
// write so the on-disk spec doesn't regress; MEDIUM persists but writes
// through.
- const report = validateAgentSpec({ agentJson: data });
+ const [report] = await validateWithOperatorPolicy(userId, { kind: 'agent', args: { agentJson: data } });
void persistQualityReport({
uid: userId, kind: 'agent', id: agentId, report,
});
diff --git a/src/main/features/group_chat/bus.ts b/src/main/features/group_chat/bus.ts
index 67f8bc4b..eed84742 100644
--- a/src/main/features/group_chat/bus.ts
+++ b/src/main/features/group_chat/bus.ts
@@ -6704,7 +6704,7 @@ async function runActorTurnWithDisplay(
// the chip slot only fills when the spec was actually written.
for (const container of skillR.containers) {
try {
- let result = await skillsFeat.applySkillContainerFromCommander(container);
+ let result = await skillsFeat.applySkillContainerFromCommander(container, { isCancelled: () => w.stopRequested || getActiveUserId() !== uid });
if (!result.ok && !errText && !aborted && !w.stopRequested
&& skillR.containers.length === 1 && !r.blocks.length
&& !toolCreatedSkills.length && !terminalHandoffCompleted
diff --git a/src/main/features/group_chat/skill-creation-correction.ts b/src/main/features/group_chat/skill-creation-correction.ts
index e1b61934..9062e46b 100644
--- a/src/main/features/group_chat/skill-creation-correction.ts
+++ b/src/main/features/group_chat/skill-creation-correction.ts
@@ -83,7 +83,6 @@ export async function applySkillCreationCorrection(
if (otherFiles.some(file => correctedByPath.get(file.path) !== file.content)) return null;
if (original.files.length && corrected.files.some(file => file.path !== 'SKILL.md'
&& !originalPaths.has(file.path))) return null;
- // Creation prevalidation, allocation and writes have no async yield. Existing
- // identity collisions still reject; never turn a correction into an edit.
- return applySkillContainerFromCommander(corrected);
+ // Async validation checks cancellation again before the synchronous commit.
+ return applySkillContainerFromCommander(corrected, { isCancelled: () => signal.aborted || getActiveUserId() !== userId });
}
diff --git a/src/main/features/marketplace.ts b/src/main/features/marketplace.ts
index a4304066..f5725c86 100644
--- a/src/main/features/marketplace.ts
+++ b/src/main/features/marketplace.ts
@@ -39,6 +39,7 @@
* `marketplace_dev.ts` — excluded from packaged builds via `package.json::build.files`.
*/
+import { validateWithOperatorPolicy, hasBlockingOperatorPolicy } from './operator-policy';
import AdmZip from 'adm-zip';
import { app } from 'electron';
import * as fs from 'node:fs';
@@ -89,7 +90,6 @@ import {
} from './marketplace_bundle';
import { createLogger } from '../logger';
import {
- validateAgentSpec, validateSkillDir,
ValidationReport as QualityReport,
} from '../quality';
import { persistReport as persistQualityReport } from '../quality/report';
@@ -929,16 +929,16 @@ async function _installMarketplaceAgentLocked(
// on a fresh user account, and the validator is the choke point that
// catches it before write. EXTREME → abort + persist report; MEDIUM
// only persists the report and lets the install proceed.
- const preReport = validateAgentSpec({
+ const [preReport] = await validateWithOperatorPolicy(uid, { kind: 'agent', args: {
agentJson: detail.agent_json,
// Installation restores published bytes verbatim. Runner compatibility
// is enforced while authoring/publishing, not retroactively on install.
enforceSkillRunner: false,
- });
+ } });
await persistQualityReport({
uid, kind: 'agent', id: agentId, report: preReport,
});
- if (!preReport.ok && opts.force !== true) {
+ if (!preReport.ok && (opts.force !== true || hasBlockingOperatorPolicy(preReport))) {
throw _qualityInstallError('agent', agentId, preReport);
}
// 4. Now materialize the agent: cache content → `<uid>/local/marketplace/agents/<id>/`.
@@ -955,6 +955,10 @@ async function _installMarketplaceAgentLocked(
const privateSkillsDir = path.join(staged, 'skills');
await fsp.mkdir(privateSkillsDir, { recursive: true });
extractBundleSafely(privateSkillsZip, privateSkillsDir);
+ for (const privateId of privateSkillIds) {
+ const [report] = await validateWithOperatorPolicy(uid, { kind: 'directory', dir: path.join(privateSkillsDir, privateId), options: { source: 'agent-private', enforceSkillRunner: false } });
+ if (hasBlockingOperatorPolicy(report)) throw _qualityInstallError('agent', agentId, report);
+ }
}
// `_install.json` stores everything the in-app UI needs without re-hitting the network:
// version pin for reconcile; freshness timestamps for catalog/cache metadata;
@@ -1084,16 +1088,16 @@ async function _installMarketplaceSkillLocked(
// Validate the staged update before replacing a previously working
// install. A rejected or malformed update must leave the old version
// intact and retryable.
- const skillReport = validateSkillDir(staged, {
+ const [skillReport] = await validateWithOperatorPolicy(uid, { kind: 'directory', dir: staged, options: {
source: 'marketplace',
// Installation restores published bytes verbatim. Runner compatibility
// is enforced while authoring/publishing, not retroactively on install.
enforceSkillRunner: false,
- });
+ } });
await persistQualityReport({
uid, kind: 'skill', id: skillId, report: skillReport,
});
- if (!skillReport.ok && opts.force !== true) {
+ if (!skillReport.ok && (opts.force !== true || hasBlockingOperatorPolicy(skillReport))) {
throw _qualityInstallError('skill', skillId, skillReport);
}
diff --git a/src/main/features/operator-policy.ts b/src/main/features/operator-policy.ts
new file mode 100644
index 00000000..04b83336
--- /dev/null
+++ b/src/main/features/operator-policy.ts
@@ -0,0 +1,69 @@
+/** User/device-local opt-in policy. No model/tool API and no cloud sync. */
+import * as fs from 'node:fs/promises';
+import { userLocalConfigDir, userOperatorPolicyFile as policyFile, userOperatorPolicyEnabledFile as enabledFile } from '../paths';
+import { writeJson } from '../storage';
+import { t } from '../i18n';
+import {
+ runOperatorPolicy, validateSkillFile, validateSkillDir, validateAgentSpec,
+ VALIDATOR_VERSION, type PolicyRequest, type ValidationReport,
+} from '../quality';
+
+async function readBounded(file: string): Promise<string> {
+ const handle = await fs.open(file, 'r');
+ try {
+ const buffer = Buffer.alloc(65537);
+ const { bytesRead } = await handle.read(buffer, 0, buffer.length, 0);
+ if (bytesRead > 65536) throw new Error('policy size');
+ return buffer.subarray(0, bytesRead).toString('utf8');
+ } finally { await handle.close(); }
+}
+async function isEnabled(uid: string): Promise<boolean> {
+ try {
+ const value = JSON.parse(await readBounded(enabledFile(uid)));
+ if (typeof value.enabled !== 'boolean') throw new Error('policy setting');
+ return value.enabled;
+ } catch (error) {
+ if ((error as NodeJS.ErrnoException).code === 'ENOENT') return false;
+ throw error;
+ }
+}
+function incomplete(): ValidationReport {
+ return { ok: false, validator_version: VALIDATOR_VERSION, validated_at: new Date().toISOString(), violations: [{
+ rule: 'operator:incomplete', level: 'EXTREME', source: 'operator-policy', field: '', snippet: '',
+ suggested_fix: t('quality.operator.incomplete'),
+ }] };
+}
+export function hasBlockingOperatorPolicy(report: ValidationReport): boolean {
+ return report.violations.some(v => v.source === 'operator-policy' && v.level === 'EXTREME');
+}
+export async function validateWithOperatorPolicy(uid: string, request: Exclude<PolicyRequest, { kind: 'config' }>): Promise<ValidationReport[]> {
+ try {
+ const enabled = await isEnabled(uid);
+ if (!enabled) {
+ if (request.kind === 'files') return request.files.map(validateSkillFile);
+ if (request.kind === 'agent') return [validateAgentSpec(request.args)];
+ return [validateSkillDir(request.dir, request.options)];
+ }
+ const result = await runOperatorPolicy(await readBounded(policyFile(uid)), request);
+ if (!('reports' in result)) return [incomplete()];
+ return result.reports;
+ } catch { return [incomplete()]; }
+}
+export async function operatorPolicyStatus(uid: string): Promise<{ enabled: boolean; state: string; ruleCount: number }> {
+ try {
+ if (!(await isEnabled(uid))) return { enabled: false, state: 'disabled', ruleCount: 0 };
+ const result = await runOperatorPolicy(await readBounded(policyFile(uid)), { kind: 'config' });
+ return !('reports' in result) ? { enabled: true, state: 'invalid', ruleCount: 0 }
+ : { enabled: true, state: 'ready', ruleCount: result.ruleCount };
+ } catch { return { enabled: true, state: 'invalid', ruleCount: 0 }; }
+}
+export async function setOperatorPolicyEnabled(uid: string, enabled: boolean): Promise<void> {
+ // Disabling must work even when the policy is missing or malformed.
+ await writeJson(enabledFile(uid), { enabled });
+}
+export async function prepareOperatorPolicyFile(uid: string): Promise<string> {
+ await fs.mkdir(userLocalConfigDir(uid), { recursive: true });
+ try { await fs.writeFile(policyFile(uid), JSON.stringify({ version: 1, rules: [] }, null, 2) + '\n', { flag: 'wx' }); }
+ catch (error) { if ((error as NodeJS.ErrnoException).code !== 'EEXIST') throw error; }
+ return policyFile(uid);
+}
diff --git a/src/main/features/skills.ts b/src/main/features/skills.ts
index 60ad07de..89e790c2 100644
--- a/src/main/features/skills.ts
+++ b/src/main/features/skills.ts
@@ -16,6 +16,7 @@
* chat turn picks up the new / updated / removed skill.
*/
+import { validateWithOperatorPolicy, hasBlockingOperatorPolicy } from './operator-policy';
import * as crypto from 'node:crypto';
import * as fs from 'node:fs';
import * as fsp from 'node:fs/promises';
@@ -66,7 +67,6 @@ import {
import { findOuterTagRanges } from '../util/markdown-prose-code';
import {
validateSkillFile,
- validateSkillDir,
ValidationReport as QualityReport,
} from '../quality';
import { persistReport as persistQualityReport } from '../quality/report';
@@ -1159,6 +1159,12 @@ async function _listSkillFilesAt(skillDir: string): Promise<SkillFileInfo[]> {
export async function createCustomSkill(
name: string, description: string, category = '',
): Promise<CustomSkill | null> {
+ const uid = getActiveUserId();
+ const reports = await validateWithOperatorPolicy(uid, { kind: 'files', files: [
+ { relpath: 'SKILL.md', content: skillMdContent(name, description, '', category, 'approved') },
+ { relpath: '_meta.json', content: JSON.stringify({ category, status: 'approved' }) },
+ ] });
+ if (uid !== getActiveUserId() || reports.some(hasBlockingOperatorPolicy)) throw new Error(t('quality.operator.incomplete'));
return _createCustomSkillSync(name, description, category);
}
@@ -1242,6 +1248,11 @@ export async function updateCustomSkill(
? String(updates.category || '')
: ((meta.category as string) || '');
+ const policyReports = await validateWithOperatorPolicy(userId, { kind: 'files', files: [
+ { relpath: 'SKILL.md', content: skillMdContent(newName, { zh: newZh, en: newEn }, body) },
+ { relpath: '_meta.json', content: JSON.stringify({ ...readSkillOrkasMetaSync(d), category: newCategory }) },
+ ] });
+ if (userId !== getActiveUserId() || policyReports.some(hasBlockingOperatorPolicy)) throw new Error(t('quality.operator.incomplete'));
let currentId = skillId;
// `skipRename` is the in-progress-edit hook used by the skill detail name
// editor: while the user is typing, write the new `name:` into SKILL.md
@@ -1643,27 +1654,27 @@ function _filesForSourceSkillRoot(
.filter((f) => f.rel && !f.rel.startsWith('..') && f.rel !== '.');
}
-function _copyImportedSkillFilesPreservingSource(skillDir: string, files: { src: string; rel: string; size: number }[]): void {
+function _copyImportedSkillFilesPreservingSource(skillDir: string, files: { src: string; rel: string; size: number }[], fallbackName = path.basename(skillDir)): void {
for (const { src, rel } of files) {
const dst = path.join(skillDir, rel);
fs.mkdirSync(path.dirname(dst), { recursive: true });
if (rel.toUpperCase() === 'SKILL.MD') {
const raw = fs.readFileSync(src, 'utf8');
- writeTextAtomicSync(dst, normalizeSkillMdForWrite(raw, path.basename(skillDir)));
+ writeTextAtomicSync(dst, normalizeSkillMdForWrite(raw, fallbackName));
} else {
fs.copyFileSync(src, dst);
}
}
}
-function _copyImportedDraftFiles(skillDir: string, files: { src: string; rel: string; size: number }[]): void {
+function _copyImportedDraftFiles(skillDir: string, files: { src: string; rel: string; size: number }[], fallbackName = path.basename(skillDir)): void {
for (const { src, rel } of files) {
if (path.basename(rel).toLowerCase() === '_meta.json') continue;
const dst = path.join(skillDir, rel);
fs.mkdirSync(path.dirname(dst), { recursive: true });
if (rel.toUpperCase() === 'SKILL.MD') {
const raw = fs.readFileSync(src, 'utf8');
- writeTextAtomicSync(dst, normalizeSkillMdForWrite(raw, path.basename(skillDir)));
+ writeTextAtomicSync(dst, normalizeSkillMdForWrite(raw, fallbackName));
} else {
fs.copyFileSync(src, dst);
}
@@ -1724,7 +1735,7 @@ async function _markSourceSkillMetadataSession(firstSkillId: string, targetSkill
}
function _hasNonOverrideableAuthoringViolation(report: QualityReport): boolean {
- return report.violations.some((violation) => violation.rule === 'skill_script_requires_runner');
+ return hasBlockingOperatorPolicy(report) || report.violations.some((violation) => violation.rule === 'skill_script_requires_runner');
}
async function _installSourceSkillRoots(
@@ -1783,6 +1794,8 @@ async function _installSourceSkillRoots(
? (description || '').trim()
: _sourceSkillImportDescription(sourceSkillMd, t('skills.import.default_desc_dir'));
let created: CustomSkill | null = null;
+ const uid = getActiveUserId();
+ const staged = fs.mkdtempSync(path.join(os.tmpdir(), 'orkas-policy-import-'));
try {
if (opts.requireSkillMd) {
// Check the source before normalization can fill a missing name or
@@ -1805,41 +1818,38 @@ async function _installSourceSkillRoots(
continue;
}
}
- created = await createCustomSkill(effectiveName, effectiveDesc);
- if (!created) throw new Error(t('skills.errors.create_failed'));
-
- const skillDir = customSkillDir(created.id);
const rootFiles = _dropSourceMetaFiles(_filesForSourceSkillRoot(sourceRoot, files, sourceRoots));
- fs.rmSync(skillMetaFile(skillDir), { force: true });
- _copyImportedSkillFilesPreservingSource(skillDir, rootFiles);
- writeSkillOrkasMetaFullSync(skillDir, _sourceSkillInstallMeta(sourceRoot, sourceSkillMd));
-
- const report = validateSkillDir(skillDir);
+ _copyImportedSkillFilesPreservingSource(staged, rootFiles, effectiveName);
+ writeSkillOrkasMetaFullSync(staged, _sourceSkillInstallMeta(sourceRoot, sourceSkillMd));
+ const [report] = await validateWithOperatorPolicy(uid, { kind: 'directory', dir: staged });
+ if (uid !== getActiveUserId()) throw new Error(t('quality.operator.incomplete'));
void persistQualityReport({
- uid: getActiveUserId(), kind: 'skill', id: created.id, report,
+ uid: getActiveUserId(), kind: 'skill', id: effectiveName, report,
});
if (!report.ok && (opts.force !== true || _hasNonOverrideableAuthoringViolation(report))) {
const failure: ImportFailure = {
sourceName,
- skillId: created.id,
+ skillId: effectiveName,
error: t('skills.errors.validation_blocked'),
report,
};
failures.push(failure);
- try { await deleteCustomSkill(created.id); } catch { /* best-effort rollback */ }
if (!opts.continueOnError) {
await rollbackCreated();
return {
ok: false,
error: failure.error,
report,
- skillId: created.id,
+ skillId: effectiveName,
failures,
};
}
continue;
}
+ created = _createCustomSkillSync(effectiveName, effectiveDesc);
+ if (!created) throw new Error(t('skills.errors.create_failed'));
+ fs.cpSync(staged, customSkillDir(created.id), { recursive: true });
const fresh = await getCustomSkill(created.id);
if (!fresh) throw new Error(t('skills.errors.create_failed'));
createdIds.push(created.id);
@@ -1865,7 +1875,7 @@ async function _installSourceSkillRoots(
await rollbackCreated();
return { ok: false, error: failure.error, failures };
}
- }
+ } finally { fs.rmSync(staged, { recursive: true, force: true }); }
}
if (createdSkills.length === 0) {
@@ -1908,35 +1918,27 @@ async function _createEditableDraftFromImportDir(
): Promise<ImportResult> {
const effectiveName = (name || '').trim() || _defaultSkillNameFromDir(realSrc);
const effectiveDesc = (description || '').trim() || t('skills.import.default_desc_dir');
- const created = await createCustomSkill(effectiveName, effectiveDesc);
- if (!created) return { ok: false, error: t('skills.errors.create_failed') };
-
- const skillDir = customSkillDir(created.id);
+ const uid = getActiveUserId();
+ const staged = fs.mkdtempSync(path.join(os.tmpdir(), 'orkas-policy-draft-'));
+ let created: CustomSkill | null = null;
try {
- _copyImportedDraftFiles(skillDir, files);
- markSkillImportDraftSync(skillDir, 'dir');
- } catch (err) {
- log.warn(`import-dir draft copy failed skill=${created.id}: ${(err as Error).message}`);
- try { await deleteCustomSkill(created.id); } catch { /* ignore */ }
- return { ok: false, error: t('skills.errors.copy_failed', { message: (err as Error).message }) };
- }
-
+ writeTextAtomicSync(path.join(staged, 'SKILL.md'), skillMdContent(effectiveName, effectiveDesc, '', '', 'approved'));
+ _copyImportedDraftFiles(staged, files, effectiveName);
+ markSkillImportDraftSync(staged, 'dir');
+ const [report] = await validateWithOperatorPolicy(uid, { kind: 'directory', dir: staged });
+ void persistQualityReport({ uid, kind: 'skill', id: effectiveName, report });
+ if (uid !== getActiveUserId() || (!report.ok && (opts.force !== true || _hasNonOverrideableAuthoringViolation(report)))) {
+ return { ok: false, error: t('skills.errors.validation_blocked'), report, skillId: effectiveName };
+ }
+ created = _createCustomSkillSync(effectiveName, effectiveDesc);
+ if (!created) return { ok: false, error: t('skills.errors.create_failed') };
+ fs.cpSync(staged, customSkillDir(created.id), { recursive: true });
+ } catch {
+ if (created) await deleteCustomSkill(created.id);
+ return { ok: false, error: t('skills.errors.create_failed') };
+ } finally { fs.rmSync(staged, { recursive: true, force: true }); }
_invalidateSkillListCache();
- invalidateCoreAgentSkills().catch(() => { /* runner may not be loaded yet */ });
-
- const report = validateSkillDir(skillDir);
- void persistQualityReport({
- uid: getActiveUserId(), kind: 'skill', id: created.id, report,
- });
- if (!report.ok && (opts.force !== true || _hasNonOverrideableAuthoringViolation(report))) {
- try { await deleteCustomSkill(created.id); } catch { /* best-effort rollback */ }
- return {
- ok: false,
- error: t('skills.errors.validation_blocked'),
- report,
- skillId: created.id,
- };
- }
+ invalidateCoreAgentSkills().catch(() => {});
const fresh = await getCustomSkill(created.id) || created;
log.info('created-from-dir import draft', {
@@ -2235,11 +2237,13 @@ export interface WriteSkillFileResult {
* `<<<skill-file>>>` apply path; future inline edit chat). Callers that
* only need a yes/no go through the legacy `writeCustomSkillFile()` wrapper.
*/
-export function writeCustomSkillFileChecked(
+export async function writeCustomSkillFileChecked(
skillId: string,
relpath: string,
content: string,
-): WriteSkillFileResult {
+ isCancelled?: () => boolean,
+): Promise<WriteSkillFileResult> {
+ const uid = getActiveUserId();
const d = path.resolve(customSkillDir(skillId));
if (!fs.existsSync(d) || !fs.statSync(d).isDirectory()) {
return { ok: false, reason: 'missing_dir' };
@@ -2252,7 +2256,14 @@ export function writeCustomSkillFileChecked(
const contentForWrite = relpath.toUpperCase() === 'SKILL.MD'
? normalizeSkillMdForWrite(content, skillId)
: content;
- const report = validateSkillFile({ relpath, content: contentForWrite });
+ const files = [{ relpath, content: contentForWrite }];
+ if (isSkillMdWrite && _hasSkillSidecarPatch(sidecarPatch)) {
+ files.push({ relpath: '_meta.json', content: JSON.stringify({ ...readSkillOrkasMetaSync(d), ...sidecarPatch }) });
+ }
+ const reports = await validateWithOperatorPolicy(uid, { kind: 'files', files });
+ const violations = reports.flatMap((r, index) => index === 0 ? r.violations : r.violations.filter(v => v.source === 'operator-policy'));
+ const report = { ...reports[0], violations, ok: !violations.some(v => v.level === 'EXTREME') };
+ if (uid !== getActiveUserId() || isCancelled?.() || !fs.existsSync(d)) return { ok: false, reason: 'missing_dir' };
// Persist best-effort regardless of outcome — the report's also the input
// for the future evolution / reflection signal stream.
void persistQualityReport({
@@ -2274,12 +2285,12 @@ export function writeCustomSkillFileChecked(
* Boolean-returning wrapper for callers that don't consume the quality
* report. Internally delegates to `writeCustomSkillFileChecked`.
*/
-export function writeCustomSkillFile(
+export async function writeCustomSkillFile(
skillId: string,
relpath: string,
content: string,
-): boolean {
- return writeCustomSkillFileChecked(skillId, relpath, content).ok;
+): Promise<boolean> {
+ return (await writeCustomSkillFileChecked(skillId, relpath, content)).ok;
}
/** Path-validated write into a resolved skill directory. Returns false on
@@ -2323,6 +2334,7 @@ export async function writeSkillFileForEditChecked(
skillId: string,
relpath: string,
content: string,
+ isCancelled?: () => boolean,
): Promise<WriteSkillFileResult> {
const isSkillMdWrite = relpath.toUpperCase() === 'SKILL.MD';
const sidecarPatch = isSkillMdWrite
@@ -2331,7 +2343,7 @@ export async function writeSkillFileForEditChecked(
const contentForWrite = isSkillMdWrite ? normalizeSkillMdForWrite(content, skillId) : content;
const customDir = customSkillDir(skillId);
if (fs.existsSync(customDir) && fs.statSync(customDir).isDirectory()) {
- return writeCustomSkillFileChecked(skillId, relpath, content);
+ return writeCustomSkillFileChecked(skillId, relpath, content, isCancelled);
}
if (isBuiltinSkill(skillId)) {
return { ok: false };
@@ -2361,18 +2373,27 @@ interface SkillMetadataApplyResult {
export async function applySkillMetadataForEdit(
skillId: string,
updates: SkillMetadataUpdate,
- opts: { replaceSidecar?: boolean } = {},
+ opts: { replaceSidecar?: boolean; isCancelled?: () => boolean } = {},
): Promise<SkillMetadataApplyResult> {
const skill = await getSkillForEdit(skillId);
if (!skill) return { ok: false, skillId, written: false, reason: 'missing_dir' };
const mdPath = path.join(skill.dir, 'SKILL.md');
const current = fs.existsSync(mdPath) ? fs.readFileSync(mdPath, 'utf8') : '';
const next = _applyMetadataToSkillMdContent(current, updates, skill.id, skill.source);
+ const uid = getActiveUserId();
+ const proposedSidecar = opts.replaceSidecar
+ ? _skillSidecarReplacementFromMetadataUpdate(updates, readSkillOrkasMetaSync(skill.dir))
+ : { ...readSkillOrkasMetaSync(skill.dir), ..._skillSidecarPatchFromMetadataUpdate(updates) };
+ const policyReports = await validateWithOperatorPolicy(uid, { kind: 'files', files: [
+ { relpath: 'SKILL.md', content: next }, { relpath: '_meta.json', content: JSON.stringify(proposedSidecar) },
+ ] });
+ const blocked = policyReports.find(hasBlockingOperatorPolicy);
+ if (blocked || uid !== getActiveUserId() || opts.isCancelled?.()) return { ok: false, skillId, written: false, report: blocked };
let report: QualityReport | undefined;
let wrote = false;
if (next !== current) {
- const res = await writeSkillFileForEditChecked(skillId, 'SKILL.md', next);
+ const res = await writeSkillFileForEditChecked(skillId, 'SKILL.md', next, opts.isCancelled);
if (!res.ok) {
return { ok: false, skillId, written: false, report: res.report, reason: res.reason };
}
@@ -3005,7 +3026,7 @@ export interface SkillContainerResult {
* `streamSendToSkillChat`'s file-by-file outcome). */
export async function applySkillContainerFromCommander(
container: SkillContainerExtracted,
- opts: { replaceSidecar?: boolean } = {},
+ opts: { replaceSidecar?: boolean; isCancelled?: () => boolean } = {},
): Promise<SkillContainerResult> {
const hasMetadata = _hasSkillMetadataUpdate(container.metadata);
if (!container.files.length && !hasMetadata) {
@@ -3025,12 +3046,13 @@ export async function applySkillContainerFromCommander(
if (container.skillId) {
return _applySkillContainerEdit(container.skillId, container.files, container.metadata, opts);
}
- return _applySkillContainerCreate(container.files, container.metadata);
+ return _applySkillContainerCreate(container.files, container.metadata, opts);
}
async function _applySkillContainerCreate(
files: SkillFileBlock[],
metadata?: SkillMetadataUpdate,
+ opts: { isCancelled?: () => boolean } = {},
): Promise<SkillContainerResult> {
files = files.map((f) => (
f.path.toUpperCase() === 'SKILL.MD'
@@ -3064,10 +3086,19 @@ async function _applySkillContainerCreate(
// boilerplate SKILL.md remains and `listSkills` still shows the
// half-created skill. Pre-validation moves the EXTREME gate to a single
// decision point: any EXTREME → abort the whole create, no dir touched.
+ const uid = getActiveUserId();
+ const policyFiles = files.map(f => ({ relpath: f.path, content: f.path.toUpperCase() === 'SKILL.MD' ? normalizeSkillMdForWrite(f.content, name) : f.content }));
+ policyFiles.push({ relpath: '_meta.json', content: JSON.stringify({ ..._skillSidecarPatchFromFrontmatter(meta), ...(metadata ? _skillSidecarPatchFromMetadataUpdate(metadata) : {}), status: 'approved' }) });
+ const reports = await validateWithOperatorPolicy(uid, { kind: 'files', files: policyFiles });
+ if (uid !== getActiveUserId() || opts.isCancelled?.()) return { ok: false, error: t('skills.errors.validation_blocked') };
const validationFailed: { path: string; report: QualityReport }[] = [];
const validationWarnings: { path: string; report: QualityReport }[] = [];
- for (const fb of files) {
- const report = validateSkillFile({ relpath: fb.path, content: fb.content });
+ for (const [index, report] of reports.entries()) {
+ if (index === files.length) {
+ report.violations = report.violations.filter(v => v.source === 'operator-policy');
+ report.ok = !report.violations.some(v => v.level === 'EXTREME');
+ }
+ const fb = { path: policyFiles[index].relpath };
if (!report.ok) {
validationFailed.push({ path: fb.path, report });
} else if (report.violations.length > 0) {
@@ -3100,9 +3131,9 @@ async function _applySkillContainerCreate(
const rejected: string[] = [];
for (const fb of files) {
// After pre-validation passed, the only remaining reason to reject is
- // a path-escape attempt (handled inside `writeCustomSkillFileChecked`).
- const res = writeCustomSkillFileChecked(name, fb.path, fb.content);
- if (res.ok) {
+ // a path-escape attempt (handled inside `_writeSkillFileAt`).
+ const content = fb.path.toUpperCase() === 'SKILL.MD' ? normalizeSkillMdForWrite(fb.content, name) : fb.content;
+ if (_writeSkillFileAt(customSkillDir(name), fb.path, content)) {
written.push(fb.path);
} else {
rejected.push(fb.path);
@@ -3118,9 +3149,12 @@ async function _applySkillContainerCreate(
try { fs.utimesSync(path.join(customSkillDir(name), w), stampNow, stampNow); }
catch { /* best effort */ }
}
- if (_hasSkillSidecarPatch(metadataSidecar)) {
- writeSkillOrkasMetaSync(customSkillDir(name), metadataSidecar);
+ if (_hasSkillSidecarPatch({ ...fileSidecar, ...metadataSidecar })) {
+ writeSkillOrkasMetaSync(customSkillDir(name), { ...fileSidecar, ...metadataSidecar });
}
+ const violations = reports.flatMap(report => report.violations);
+ void persistQualityReport({ uid, kind: 'skill', id: name,
+ report: { ...reports[0], violations, ok: !violations.some(v => v.level === 'EXTREME') } });
if (written.length) log.info(`commander created skill=${name} files=${written.length}`);
return {
ok: true,
@@ -3137,7 +3171,7 @@ async function _applySkillContainerEdit(
skillId: string,
files: SkillFileBlock[],
metadata?: SkillMetadataUpdate,
- opts: { replaceSidecar?: boolean } = {},
+ opts: { replaceSidecar?: boolean; isCancelled?: () => boolean } = {},
): Promise<SkillContainerResult> {
const hasMetadata = _hasSkillMetadataUpdate(metadata);
if (!files.length && !hasMetadata) {
@@ -3148,7 +3182,7 @@ async function _applySkillContainerEdit(
const hasSkillMd = files.some((f) => f.path.toUpperCase() === 'SKILL.MD');
if (hasSkillMd) {
log.info(`commander target skill=${skillId} missing; treating SKILL.md payload as create`);
- return _applySkillContainerCreate(files);
+ return _applySkillContainerCreate(files, undefined, opts);
}
return { ok: false, error: t('skills.errors.skill_not_found', { id: skillId }) };
}
@@ -3167,7 +3201,7 @@ async function _applySkillContainerEdit(
const validationWarnings: { path: string; report: QualityReport }[] = [];
let touchedSkillMd = false;
for (const fb of files) {
- const res = await writeSkillFileForEditChecked(skillId, fb.path, fb.content);
+ const res = await writeSkillFileForEditChecked(skillId, fb.path, fb.content, opts.isCancelled);
if (res.ok) {
written.push(fb.path);
if (fb.path.toUpperCase() === 'SKILL.MD') touchedSkillMd = true;
@@ -3628,8 +3662,8 @@ export async function* streamSendToSkillChat(
&& container.files.some((f) => f.path.toUpperCase() === 'SKILL.MD');
const targetBefore = currentSkillId;
const res = useCurrentDraft
- ? await _applySkillContainerEdit(currentSkillId, container.files, container.metadata)
- : await applySkillContainerFromCommander(container);
+ ? await _applySkillContainerEdit(currentSkillId, container.files, container.metadata, { isCancelled: () => !!opts.abortSignal?.aborted })
+ : await applySkillContainerFromCommander(container, { isCancelled: () => !!opts.abortSignal?.aborted });
if (res.ok && res.skillId) {
if (useCurrentDraft) {
usedImportDraftAsSkill = true;
@@ -3675,7 +3709,7 @@ export async function* streamSendToSkillChat(
container.skillId,
[],
metadataOnly,
- { replaceSidecar: true },
+ { replaceSidecar: true, isCancelled: () => !!opts.abortSignal?.aborted },
);
if (res.ok && res.skillId) {
createdSkills.push({
diff --git a/src/main/ipc/quality.ts b/src/main/ipc/quality.ts
index 726e8a34..a52b14e5 100644
--- a/src/main/ipc/quality.ts
+++ b/src/main/ipc/quality.ts
@@ -1,16 +1,6 @@
-/**
- * IPC handlers for the quality validator.
- *
- * Renderer reads persisted ValidationReports to display the violation list
- * when a write / install rejection happens. The validator runs in-process at
- * the chokepoints (see `features/skills.ts` + `features/agents.ts` +
- * `features/marketplace.ts`); these channels are read-only — never run the
- * validator on demand from the renderer.
- *
- * Logical channels:
- * - `quality.readSkillReport` → latest report for a skill id
- * - `quality.readAgentReport` → latest report for an agent id
- */
+import { shell } from 'electron';
+import { operatorPolicyStatus, setOperatorPolicyEnabled, prepareOperatorPolicyFile } from '../features/operator-policy';
+/** Quality reports and device-local custom-validation settings. */
import { readReport } from '../quality/report';
import { getActiveUserId } from '../features/users';
@@ -19,6 +9,18 @@ import { safeId } from '../storage';
type InvokeHandler = (payload: Record<string, unknown>) => Promise<Record<string, unknown>>;
export const invokeHandlers: Record<string, InvokeHandler> = {
+ 'quality.operatorStatus': async () => operatorPolicyStatus(getActiveUserId()),
+ 'quality.setOperatorEnabled': async ({ enabled }) => {
+ if (typeof enabled !== 'boolean') throw new Error('invalid enabled');
+ const uid = getActiveUserId();
+ await setOperatorPolicyEnabled(uid, enabled);
+ return operatorPolicyStatus(uid);
+ },
+ 'quality.openOperatorFile': async () => {
+ const file = await prepareOperatorPolicyFile(getActiveUserId());
+ shell.showItemInFolder(file);
+ return { ok: true };
+ },
'quality.readSkillReport': async ({ id }) => {
if (typeof id !== 'string' || !safeId(id)) throw new Error('invalid id');
const report = await readReport({ uid: getActiveUserId(), kind: 'skill', id });
diff --git a/src/main/locales/en.json b/src/main/locales/en.json
index 47156bf6..6f4e96e6 100644
--- a/src/main/locales/en.json
+++ b/src/main/locales/en.json
@@ -280,5 +280,6 @@
"sync.conflict_resolution.title_single": "Cloud sync conflict resolution: {name}",
"sync.conflict_resolution.title_batch": "Cloud sync conflict resolution: {count} conflicts",
"sync.conflict_resolution.message_single": "Please help me resolve this cloud sync conflict.",
- "sync.conflict_resolution.message_batch": "Please help me resolve these cloud sync conflicts."
+ "sync.conflict_resolution.message_batch": "Please help me resolve these cloud sync conflicts.",
+ "quality.operator.incomplete": "Custom validation did not complete. Fix the policy file or disable custom validation in Settings, then retry."
}
diff --git a/src/main/locales/zh.json b/src/main/locales/zh.json
index 2431eecb..4bb73ae4 100644
--- a/src/main/locales/zh.json
+++ b/src/main/locales/zh.json
@@ -280,5 +280,6 @@
"sync.conflict_resolution.title_single": "云同步冲突处理:{name}",
"sync.conflict_resolution.title_batch": "云同步冲突处理:{count} 条冲突",
"sync.conflict_resolution.message_single": "请帮我处理这条云同步冲突。",
- "sync.conflict_resolution.message_batch": "请帮我处理这些云同步冲突。"
+ "sync.conflict_resolution.message_batch": "请帮我处理这些云同步冲突。",
+ "quality.operator.incomplete": "自定义校验未完成。请修复规则文件,或在设置中停用自定义校验后重试。"
}
diff --git a/src/main/paths.ts b/src/main/paths.ts
index befcf95d..26fe6543 100644
--- a/src/main/paths.ts
+++ b/src/main/paths.ts
@@ -430,6 +430,8 @@ export const userDeviceConnectorEnabledFile = (uid: string) => path.join(userLoc
export const userAuthProfilesFile = (uid: string) => path.join(userLocalConfigDir(uid), 'auth-profiles.json');
export const userWebSearchCache = (uid: string) => path.join(userLocalConfigDir(uid), 'web-search-cache.json');
export const userReflectionStateFile = (uid: string) => path.join(userLocalConfigDir(uid), 'reflection-state.json');
+export const userOperatorPolicyFile = (uid: string) => path.join(userLocalConfigDir(uid), 'operator-policy.json');
+export const userOperatorPolicyEnabledFile = (uid: string) => path.join(userLocalConfigDir(uid), 'operator-policy-enabled.json');
export const userDevtoolsFile = (uid: string) => path.join(userLocalConfigDir(uid), 'devtools.json');
// Compact crash-recovery journal. Keeping this local avoids syncing transient
// process state and lets startup inspect only conversations that were running.
diff --git a/src/main/quality/README.md b/src/main/quality/README.md
index a4eb832d..acf47b5f 100644
--- a/src/main/quality/README.md
+++ b/src/main/quality/README.md
@@ -43,19 +43,46 @@ quality/
## Operator policy rules
-Teams can add their own red flags without forking: write a JSON rule file,
-parse it with `parseOperatorPolicy` (exported from `index.ts`; reading the
-file is the caller's job — `quality/` stays stateless), then pass the parsed
-rules as `operatorRules` to `validateSkillFile` / `validateSkillDir` /
-`validateAgentSpec` / `validateAgentDir`.
-
-The built-in rules stay the security floor: operator rules are additive only
-— they never suppress, downgrade or rewrite a built-in finding, and a
-colliding id is rejected. Their findings carry `source: 'operator-policy'`.
-`appliesTo` scopes operator rules by artifact: `skill_md` means executable
-fenced blocks in `SKILL.md`, `script` means standalone script files,
-`skill_meta` means `_meta.json`, and `agent_json` means an Agent spec. Still
-not a sandbox. Field reference lives in `rules/operator-policy.ts`.
+Custom validation is disabled by default. In Settings → General, select **Rule
+file** to reveal the current account's device-local `operator-policy.json`, edit
+it, then enable custom validation. **Reload** checks the file again. The file is
+not synchronized. It has this format:
+
+```json
+{"version":1,"rules":[{"id":"internal_bucket","level":"EXTREME","pattern":"s3://acme-internal","message":"Internal buckets are off limits."}]}
+```
+
+Up to 50 rules are accepted. Each pattern is at most 500 characters; optional
+flags are a unique subset of `imsu`, and messages are at most 300 characters.
+Unknown fields, wrong types, duplicate ids, unsupported versions and invalid
+regexes reject the entire file. Rule ids use `[a-z0-9][a-z0-9_.-]{0,63}` and
+findings use the separate `operator:` namespace with `source: 'operator-policy'`.
+Operator messages are displayed as text, never as a built-in translated hint.
+
+The `features/operator-policy.ts` coordinator owns configuration reads; the
+stateless `runOperatorPolicy` API executes compilation and validation in a
+terminable Worker. Direct synchronous validator calls with operator rules fail
+closed on the main thread. No custom regex executes there. Scans have a two-second
+wall deadline, two concurrent slots (no waiting queue), a 64 KiB policy limit,
+a 2 MiB input limit, at most 512 findings, and 512 directory entries with depth 32. Linked, oversized
+or unreadable directories fail closed. Limits apply only when enabled.
+
+`appliesTo` defaults to `skill_md`, `script`, and `agent_json`. `skill_md` scans
+executable fenced blocks, not prose; `script` scans recognized script extensions;
+`skill_meta` scans `_meta.json`; `agent_json` scans serialized Agent JSON. This is
+static content validation, not runtime access enforcement.
+
+Create, edit, directory/ZIP import and Marketplace installation use the shared
+coordinator. Imports validate staging before publication; Agent private Skills
+are checked with their owner. EXTREME operator findings, configuration errors,
+timeouts and overload cannot be bypassed with force. Repair the file or disable
+custom validation to recover. Built-in checks remain additive and unchanged
+when disabled; existing built-in-only force behavior remains compatible.
+
+This capability is host-only: configuration is a local desktop preference, not
+a Web application SDK or model-visible tool. It does not retroactively scan
+already installed content, global/external-package Skills, synchronization or
+runtime-generated learned Skills.
## Levels
diff --git a/src/main/quality/index.ts b/src/main/quality/index.ts
index 057a7e3e..81b64f11 100644
--- a/src/main/quality/index.ts
+++ b/src/main/quality/index.ts
@@ -20,6 +20,7 @@
*/
import * as fs from 'node:fs';
+import { isMainThread } from 'node:worker_threads';
import * as path from 'node:path';
import { Violation, ValidationReport, ScanKind, RuleDef, VALIDATOR_VERSION } from './types';
@@ -44,6 +45,8 @@ import {
// Re-export the types so callers only need one import path.
export type { Violation, ValidationReport, Level } from './types';
export { VALIDATOR_VERSION } from './types';
+export { runOperatorPolicy } from './operator-runner';
+export type { PolicyRequest, PolicyResult } from './operator-runner';
export { parseOperatorPolicy } from './rules/operator-policy';
export type { OperatorPolicyParseResult } from './rules/operator-policy';
@@ -144,6 +147,7 @@ export function validateSkillDir(
violations.push(...validateSkillMeta(meta));
}
} catch (err) {
+ if (options.operatorRules !== undefined) throw err;
violations.push(parseFailureViolation({
kind: 'frontmatter',
message: (err as Error).message,
@@ -162,7 +166,8 @@ export function validateSkillDir(
const scanArgs: RuleScanArgs = { content, kind, field: rel };
if (kind === 'script') violations.push(...scanRedFlags(scanArgs));
violations.push(..._policyScan(options.operatorRules, scanArgs));
- } catch {
+ } catch (err) {
+ if (options.operatorRules !== undefined) throw err;
// unreadable file (binary / permission) — skip; no violation surfaced
}
}
@@ -256,6 +261,7 @@ function _policyScan(
args: RuleScanArgs,
): Violation[] {
if (!rules?.length) return [];
+ if (isMainThread) return [{ level: 'EXTREME', rule: 'operator:incomplete', source: 'operator-policy', field: '', snippet: '', suggested_fix: 'Custom policy requires asynchronous worker validation.' }];
return scanRuleSet(rules, args, 'operator-policy');
}
diff --git a/src/main/quality/operator-runner.ts b/src/main/quality/operator-runner.ts
new file mode 100644
index 00000000..63cbbe9d
--- /dev/null
+++ b/src/main/quality/operator-runner.ts
@@ -0,0 +1,43 @@
+import { Worker } from 'node:worker_threads';
+import * as path from 'node:path';
+import type { ValidationReport } from './types';
+import type { SkillValidationOptions } from './index';
+
+export type PolicyRequest =
+ | { kind: 'config' }
+ | { kind: 'files'; files: Array<{ relpath: string; content: string }> }
+ | { kind: 'agent'; args: { agentJson: unknown; enforceSkillRunner?: boolean } }
+ | { kind: 'directory'; dir: string; options?: Omit<SkillValidationOptions, 'operatorRules'> };
+export type PolicyResult = { reports: ValidationReport[]; ruleCount: number; error?: never }
+ | { error: 'config' | 'scan' | 'timeout' | 'busy' | 'size'; errors?: string[] };
+let active = 0;
+
+/** No queue: at most two scans, with a bounded wall-clock deadline including
+ * bootstrap. Completion waits for termination before releasing the slot. */
+export async function runOperatorPolicy(policy: string, request: PolicyRequest): Promise<PolicyResult> {
+ if (active >= 2) return { error: 'busy' };
+ if (request.kind === 'files' && request.files.some(file => file.relpath.length > 512)) return { error: 'size' };
+ let serialized: string;
+ try { serialized = JSON.stringify(request); } catch { return { error: 'scan' }; }
+ if (policy.length > 64 * 1024 || serialized.length > 2 * 1024 * 1024
+ || (request.kind === 'files' && request.files.length > 512)) return { error: 'size' };
+ active++;
+ let worker: Worker | undefined;
+ let timer: NodeJS.Timeout | undefined;
+ try {
+ worker = new Worker(path.join(__dirname, 'operator-worker.js'), {
+ execArgv: [], workerData: { policy, request },
+ resourceLimits: { maxOldGenerationSizeMb: 64, maxYoungGenerationSizeMb: 16, stackSizeMb: 4 },
+ });
+ return await new Promise<PolicyResult>((resolve) => {
+ timer = setTimeout(() => resolve({ error: 'timeout' }), 2000);
+ worker!.once('message', resolve);
+ worker!.once('error', () => resolve({ error: 'scan' }));
+ worker!.once('exit', () => resolve({ error: 'scan' }));
+ });
+ } catch { return { error: 'scan' }; }
+ finally {
+ clearTimeout(timer);
+ try { await worker?.terminate(); } finally { active--; }
+ }
+}
diff --git a/src/main/quality/operator-worker-budget.ts b/src/main/quality/operator-worker-budget.ts
new file mode 100644
index 00000000..fe4ba6b0
--- /dev/null
+++ b/src/main/quality/operator-worker-budget.ts
@@ -0,0 +1,24 @@
+import * as fs from 'node:fs';
+import * as path from 'node:path';
+
+// Bound the complete directory before the synchronous validator reads it.
+// Symlinks are rejected: the scan must not escape the staged resource.
+export function checkDirectoryBudget(root: string): void {
+ if (!fs.lstatSync(root).isDirectory() || !fs.statSync(path.join(root, 'SKILL.md')).isFile()) throw new Error('scan directory');
+ let bytes = 0;
+ let count = 0;
+ function walk(dir: string, depth: number): void {
+ if (depth > 32) throw new Error('scan limit');
+ for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
+ if (++count > 512) throw new Error('scan limit');
+ const file = path.join(dir, entry.name);
+ if (entry.isSymbolicLink()) throw new Error('scan link');
+ if (entry.isDirectory()) walk(file, depth + 1);
+ else if (entry.isFile()) {
+ bytes += fs.statSync(file).size;
+ if (bytes > 2 * 1024 * 1024) throw new Error('scan limit');
+ } else throw new Error('scan file');
+ }
+ }
+ walk(root, 0);
+}
diff --git a/src/main/quality/operator-worker.js b/src/main/quality/operator-worker.js
new file mode 100644
index 00000000..40387ba6
--- /dev/null
+++ b/src/main/quality/operator-worker.js
@@ -0,0 +1,27 @@
+'use strict';
+// Same source/ASAR TypeScript bootstrap as the existing history worker.
+require('tsx/cjs');
+const { parentPort, workerData } = require('node:worker_threads');
+const { parseOperatorPolicy, validateSkillFile, validateSkillDir, validateAgentSpec } = require('./index');
+const { checkDirectoryBudget } = require('./operator-worker-budget');
+try {
+ const parsed = parseOperatorPolicy(workerData.policy);
+ if (parsed.errors.length) {
+ parentPort.postMessage({ error: 'config', errors: parsed.errors });
+ } else {
+ const request = workerData.request;
+ let reports;
+ if (request.kind === 'config') reports = [];
+ else if (request.kind === 'files') reports = request.files.map(file => validateSkillFile({ ...file, operatorRules: parsed.rules }));
+ else if (request.kind === 'agent') reports = [validateAgentSpec({ ...request.args, operatorRules: parsed.rules })];
+ else {
+ checkDirectoryBudget(request.dir);
+ reports = [validateSkillDir(request.dir, { ...request.options, operatorRules: parsed.rules })];
+ }
+ if (reports.reduce((count, report) => count + report.violations.length, 0) > 512) {
+ parentPort.postMessage({ error: 'size' });
+ } else parentPort.postMessage({ reports, ruleCount: parsed.rules.length });
+ }
+} catch {
+ parentPort.postMessage({ error: 'scan' });
+}
diff --git a/src/main/quality/rules/operator-policy.ts b/src/main/quality/rules/operator-policy.ts
index 863ace79..62c2907e 100644
--- a/src/main/quality/rules/operator-policy.ts
+++ b/src/main/quality/rules/operator-policy.ts
@@ -1,12 +1,12 @@
/**
* Operator policy rules — additive red flags supplied by an operator at
* runtime. RED_FLAGS stays the security floor: rules here can only ADD
- * findings, never suppress or downgrade a built-in one, and a colliding id is
- * rejected. Findings carry `source: 'operator-policy'`. Pure: no filesystem.
+ * findings, never suppress or downgrade a built-in one. Operator ids have their own
+ * namespace. Findings carry `source: 'operator-policy'`. Pure: no filesystem.
*/
import { Level, RuleDef, ScanKind } from '../types';
-import { RED_FLAGS } from './red-flags';
+
const KINDS: ReadonlyArray<ScanKind> = ['skill_md', 'skill_meta', 'script', 'agent_json'];
const DEFAULT_APPLIES_TO: ScanKind[] = ['skill_md', 'script', 'agent_json'];
const MAX_RULES = 50;
@@ -16,13 +16,13 @@ const ALLOWED_FLAGS = /^[imsu]*$/;
export interface OperatorPolicyParseResult {
rules: RuleDef[];
- /** Reasons individual rules were skipped (never thrown). */
+ /** Configuration errors. Any error rejects the entire policy. */
errors: string[];
}
-/** Parse an operator rule file (`{ "rules": [ ... ] }`). Invalid rules are
- * skipped and reported in `errors`; the rest still load. */
+/** Parse an operator rule file (`{ "rules": [ ... ] }`). Invalid configuration is rejected atomically; no partial rule set loads. */
export function parseOperatorPolicy(content: string): OperatorPolicyParseResult {
+ if (content.length > 64 * 1024) return { rules: [], errors: ['operator policy exceeds 64 KiB'] };
let parsed: unknown;
try {
parsed = JSON.parse(content);
@@ -32,15 +32,19 @@ export function parseOperatorPolicy(content: string): OperatorPolicyParseResult
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
return { rules: [], errors: ['operator policy must be a JSON object like { "rules": [...] }'] };
}
- const raw = (parsed as { rules?: unknown }).rules;
+ const config = parsed as Record<string, unknown>;
+ if (config.version !== 1) return { rules: [], errors: ['operator policy requires version 1'] };
+ if (Object.keys(config).some((key) => !['version', 'rules'].includes(key))) {
+ return { rules: [], errors: ['unknown operator policy field'] };
+ }
+ const raw = config.rules;
if (!Array.isArray(raw)) {
return { rules: [], errors: ['operator policy must contain a "rules" array'] };
}
const errors: string[] = [];
- if (raw.length > MAX_RULES) errors.push(`operator policy has ${raw.length} rules; only the first ${MAX_RULES} load`);
+ if (raw.length > MAX_RULES) return { rules: [], errors: [`operator policy allows at most ${MAX_RULES} rules`] };
- const builtinIds = new Set(RED_FLAGS.map((r) => r.id));
const seen = new Set<string>();
const rules: RuleDef[] = [];
@@ -50,9 +54,15 @@ export function parseOperatorPolicy(content: string): OperatorPolicyParseResult
if (!entry || typeof entry !== 'object' || Array.isArray(entry)) { fail('must be an object'); continue; }
const r = entry as Record<string, unknown>;
+ if (Object.keys(r).some((key) => !['id', 'level', 'pattern', 'flags', 'message', 'appliesTo'].includes(key))) {
+ fail('unknown rule field'); continue;
+ }
+ if (r.flags !== undefined && typeof r.flags !== 'string') { fail('"flags" must be a string'); continue; }
+ if (r.message !== undefined && (typeof r.message !== 'string' || r.message.length > 300)) {
+ fail('"message" must be a string of at most 300 chars'); continue;
+ }
const id = typeof r.id === 'string' ? r.id.trim() : '';
if (!RULE_ID.test(id)) { fail(`"id" must match ${RULE_ID}`); continue; }
- if (builtinIds.has(id)) { fail(`id "${id}" collides with a built-in rule; operator rules may not shadow the security floor`); continue; }
if (seen.has(id)) { fail(`duplicate id "${id}"`); continue; }
const level = r.level;
@@ -76,7 +86,7 @@ export function parseOperatorPolicy(content: string): OperatorPolicyParseResult
seen.add(id);
rules.push({
- id,
+ id: `operator:${id}`,
level: level as Level,
appliesTo,
pattern,
@@ -85,7 +95,7 @@ export function parseOperatorPolicy(content: string): OperatorPolicyParseResult
});
}
- return { rules, errors };
+ return { rules: errors.length ? [] : rules, errors };
}
function _appliesTo(raw: unknown, where: string, errors: string[]): ScanKind[] | null {
diff --git a/src/renderer/index.html b/src/renderer/index.html
index f1f618da..d8ecb26d 100644
--- a/src/renderer/index.html
+++ b/src/renderer/index.html
@@ -1183,6 +1183,16 @@
<input type="checkbox" id="settings-task-notifications-toggle" checked aria-labelledby="settings-task-notifications-title" />
</label>
</div>
+ <div class="settings-row">
+ <div class="settings-row-copy">
+ <div class="settings-row-title" id="settings-operator-title" data-i18n="quality.operator.title">Custom validation</div>
+ <div class="settings-row-desc" data-i18n="quality.operator.scope">Checks scripts, executable SKILL.md code blocks, metadata and Agent JSON before saving or installing. This is not a runtime sandbox.</div>
+ <div class="settings-row-desc" id="settings-operator-status" role="status" aria-live="polite"></div>
+ </div>
+ <button class="btn btn-sm" id="settings-operator-file" data-i18n="quality.operator.file">Rule file</button>
+ <button class="btn btn-sm" id="settings-operator-reload" data-i18n="quality.operator.reload">Reload</button>
+ <label class="toggle-switch settings-switch"><input type="checkbox" id="settings-operator-toggle" aria-labelledby="settings-operator-title" /></label>
+ </div>
<div class="settings-row settings-task-notification-permission" id="settings-task-notification-permission" role="status" aria-live="polite" hidden>
<span class="settings-task-notification-permission-text" id="settings-task-notification-permission-text" data-i18n="settings.task_notifications.permission_disabled">系统已关闭 Orkas 的通知权限。请前往系统设置开启,否则无法收到任务进度提醒。</span>
<button type="button" class="btn btn-sm" id="settings-task-notification-open-settings" data-i18n="settings.task_notifications.open_settings">打开系统设置</button>
diff --git a/src/renderer/locales/en.json b/src/renderer/locales/en.json
index b85a0aa2..2c16844f 100644
--- a/src/renderer/locales/en.json
+++ b/src/renderer/locales/en.json
@@ -2522,5 +2522,12 @@
"chat.conv_moved_out": "Moved out of the project.",
"chat.conv_filing_failed": "Project “{name}” was created, but the conversation could not be added. Use “Add to project” in its menu and select this project to try again.",
"chat.conv_filing_failed_with_todos": "Project “{name}” and {count} to-dos were created, but the conversation could not be added. Use “Add to project” in its menu and select this project to try again.",
- "chat.conv_move_not_filed": "This task is not in a project."
+ "chat.conv_move_not_filed": "This task is not in a project.",
+ "quality.operator.title": "Custom validation",
+ "quality.operator.scope": "Checks scripts, executable SKILL.md code blocks, metadata and Agent JSON before saving or installing. This is not a runtime sandbox.",
+ "quality.operator.file": "Rule file",
+ "quality.operator.reload": "Reload",
+ "quality.operator.disabled": "Disabled on this device.",
+ "quality.operator.ready": "Ready: {count} rules.",
+ "quality.operator.invalid": "Validation unavailable. Fix operator-policy.json or turn off custom validation, then retry."
}
diff --git a/src/renderer/locales/zh.json b/src/renderer/locales/zh.json
index e1b1fb12..adc4b12a 100644
--- a/src/renderer/locales/zh.json
+++ b/src/renderer/locales/zh.json
@@ -2522,5 +2522,12 @@
"chat.conv_moved_out": "已移出项目。",
"chat.conv_filing_failed": "项目「{name}」已创建,但对话未能加入项目。请通过对话菜单的「添加到项目」选择该项目重试。",
"chat.conv_filing_failed_with_todos": "项目「{name}」及 {count} 项待办已创建,但对话未能加入项目。请通过对话菜单的「添加到项目」选择该项目重试。",
- "chat.conv_move_not_filed": "这个任务不在任何项目里。"
+ "chat.conv_move_not_filed": "这个任务不在任何项目里。",
+ "quality.operator.title": "自定义校验",
+ "quality.operator.scope": "保存或安装前检查脚本、SKILL.md 可执行代码块、元数据及 Agent JSON;不限制运行时权限。",
+ "quality.operator.file": "规则文件",
+ "quality.operator.reload": "重新加载",
+ "quality.operator.disabled": "本机已停用。",
+ "quality.operator.ready": "已加载 {count} 条规则。",
+ "quality.operator.invalid": "校验不可用。请修复 operator-policy.json,或停用自定义校验后重试。"
}
diff --git a/src/renderer/modules/settings.js b/src/renderer/modules/settings.js
index 9753ce9b..2f2848a5 100644
--- a/src/renderer/modules/settings.js
+++ b/src/renderer/modules/settings.js
@@ -111,6 +111,7 @@ async function loadSettings() {
_settingsRenderLocalExec();
_settingsSyncLanguageRadio();
await Promise.all([
+ _settingsSafeCall('custom validation refresh', _settingsRefreshOperatorPolicy),
_settingsSafeCall('settings providers refresh', _settingsRefreshProviders),
_settingsSafeCall('settings entries refresh', _settingsRefreshEntries),
_settingsSafeCall('settings local execution refresh', _settingsRefreshLocalExec),
@@ -901,6 +902,7 @@ function _settingsSyncLanguageRadio() {
// re-render sections whose text is written by JS (so their content
// isn't refreshed by applyDomI18n's data-i18n sweep).
window.addEventListener('i18n-change', () => {
+ void _settingsRefreshOperatorPolicy();
_settingsSyncLanguageRadio();
_settingsRenderLocalExec();
_settingsRenderPicker();
@@ -3053,3 +3055,30 @@ function _settingsSelectedImageOptionId() {
|| document.getElementById('settings-image-provider')?.dataset?.value
|| '';
}
+
+
+async function _settingsRefreshOperatorPolicy() {
+ const toggle = document.getElementById('settings-operator-toggle');
+ const status = document.getElementById('settings-operator-status');
+ if (!toggle || !status) return;
+ const render = (value) => {
+ toggle.checked = !!value.enabled;
+ status.textContent = t(`quality.operator.${value.state || 'invalid'}`, { count: value.ruleCount || 0 });
+ };
+ if (!toggle.dataset.bound) {
+ toggle.dataset.bound = '1';
+ toggle.addEventListener('change', async () => {
+ toggle.disabled = true;
+ try { render(await window.orkas.invoke('quality.setOperatorEnabled', { enabled: toggle.checked })); }
+ catch (_) { status.textContent = t('quality.operator.invalid'); }
+ finally { toggle.disabled = false; }
+ });
+ document.getElementById('settings-operator-file').addEventListener('click', async () => {
+ try { await window.orkas.invoke('quality.openOperatorFile'); }
+ catch (_) { status.textContent = t('quality.operator.invalid'); }
+ });
+ document.getElementById('settings-operator-reload').addEventListener('click', _settingsRefreshOperatorPolicy);
+ }
+ try { render(await window.orkas.invoke('quality.operatorStatus')); }
+ catch (_) { status.textContent = t('quality.operator.invalid'); }
+}
diff --git a/src/renderer/modules/validation-report-view.js b/src/renderer/modules/validation-report-view.js
index a8bf9b02..a83332c2 100644
--- a/src/renderer/modules/validation-report-view.js
+++ b/src/renderer/modules/validation-report-view.js
@@ -39,6 +39,7 @@ function _levelLabel(level) {
function _suggestedFixText(v) {
const rule = v && v.rule ? String(v.rule) : '';
+ if (v && v.source === 'operator-policy') return String(v.suggested_fix || '');
if (rule) {
const key = `quality.fix.${rule}`;
try {
@@ -91,6 +92,7 @@ function showValidationReport({ title, report, okLabel, forceLabel } = {}) {
overlay.className = 'modal-overlay ui-dialog-overlay open';
const violations = _reportViolations(report);
+ if (violations.some(v => v.source === 'operator-policy' && v.level === 'EXTREME')) forceLabel = undefined;
// Sort: EXTREME first, then MEDIUM, then LOW. Within a level keep
// original order (the validator already emits them in detection order).
const order = { EXTREME: 0, MEDIUM: 1, LOW: 2 };
diff --git a/test/e2e/operator-policy_e2e_guards.spec.ts b/test/e2e/operator-policy_e2e_guards.spec.ts
new file mode 100644
index 00000000..0bb439db
--- /dev/null
+++ b/test/e2e/operator-policy_e2e_guards.spec.ts
@@ -0,0 +1,31 @@
+import * as fs from 'node:fs';
+import * as path from 'node:path';
+import { expect, test } from './fixtures/orkas';
+
+test('custom policy settings persist and invalid configuration blocks forced Marketplace installation until disabled', async ({ marketplaceOrkas: app }) => {
+ const page = app.page!;
+ const user = await app.invoke<{ user_id: string }>('user.init');
+ const userRoot = path.join(app.workspaceRoot, user.user_id);
+ const config = path.join(userRoot, 'local', 'config');
+ fs.mkdirSync(config, { recursive: true });
+ fs.writeFileSync(path.join(config, 'operator-policy.json'), '{invalid');
+ await page.locator('#settings-btn').click();
+ await page.locator('.settings-tab[data-settings-tab="general"]').click();
+ const toggle = page.locator('#settings-operator-toggle');
+ await expect(toggle).not.toBeChecked();
+ await toggle.check();
+ await expect(page.locator('#settings-operator-status')).toContainText('Validation unavailable');
+ const result = await app.invoke<any>('marketplace.installSkill', { id: 'marketplace-e2e-skill', version: '1.0.0', published_at: 1, force: true });
+ expect(result.ok).toBe(false);
+ expect(result.qualityReport.violations).toContainEqual(expect.objectContaining({ rule: 'operator:incomplete' }));
+ expect(fs.existsSync(path.join(userRoot, 'local', 'marketplace', 'skills', 'marketplace-e2e-skill', 'SKILL.md'))).toBe(false);
+ const relaunched = await app.relaunch();
+ await relaunched.locator('#settings-btn').click();
+ await relaunched.locator('.settings-tab[data-settings-tab="general"]').click();
+ await expect(relaunched.locator('#settings-operator-toggle')).toBeChecked();
+ await relaunched.locator('#settings-operator-toggle').uncheck();
+ await expect(relaunched.locator('#settings-operator-status')).toContainText('Disabled');
+ const installed = await app.invoke<any>('marketplace.installSkill', { id: 'marketplace-e2e-skill', version: '1.0.0', published_at: 1 });
+ expect(installed.ok).not.toBe(false);
+ expect(fs.existsSync(path.join(userRoot, 'local', 'marketplace', 'skills', 'marketplace-e2e-skill', 'SKILL.md'))).toBe(true);
+});
diff --git a/test/main/features/agents.test.ts b/test/main/features/agents.test.ts
index 6dc6cf0c..e97d9cc3 100644
--- a/test/main/features/agents.test.ts
+++ b/test/main/features/agents.test.ts
@@ -3288,3 +3288,22 @@ describe('agents › list cache invalidation', () => {
// (The legacy marketplace-sentinel sync tests are gone. Marketplace installs now live at
// `<uid>/local/marketplace/agents/<id>/` and are reconciled from
// the cloud-synced `installs.json` manifest — see features/marketplace_*.ts.)
+
+describe('agents › operator policy', () => {
+ it('preserves rejected updates and allocates no rejected Agent, including colliding built-in names', async () => {
+ const a = await loadAgents();
+ const agent = await a.createCustomAgent({ name: 'Writer', description: 'Test' });
+ const file = path.join(customAgentsDir(), agent!.agent_id, 'agent.json');
+ const before = fs.readFileSync(file, 'utf8');
+ const ids = fs.readdirSync(customAgentsDir());
+ const config = path.join(tmpDir, TEST_UID, 'local', 'config');
+ fs.writeFileSync(path.join(config, 'operator-policy-enabled.json'), '{"enabled":true}');
+ fs.writeFileSync(path.join(config, 'operator-policy.json'), JSON.stringify({ version: 1, rules: [
+ { id: 'agent_name_missing', pattern: 'private-resource', level: 'EXTREME', message: 'Do not reference private-resource.' },
+ ] }));
+ await expect(a.updateCustomAgent(agent!.agent_id, { workflow: 'private-resource' })).rejects.toThrow('Do not reference private-resource.');
+ expect(fs.readFileSync(file, 'utf8')).toBe(before);
+ await expect(a.createCustomAgent({ name: 'Blocked', workflow: 'private-resource' })).rejects.toThrow('Do not reference private-resource.');
+ expect(fs.readdirSync(customAgentsDir())).toEqual(ids);
+ });
+});
diff --git a/test/main/features/skills.test.ts b/test/main/features/skills.test.ts
index 35eb4ae8..379b6c82 100644
--- a/test/main/features/skills.test.ts
+++ b/test/main/features/skills.test.ts
@@ -1867,7 +1867,7 @@ describe('skills › discardImportDraftIfPristine', () => {
await s.createCustomSkill('edited-draft', 'desc');
markImportDraft('edited-draft');
- expect(s.writeCustomSkillFile('edited-draft', 'notes.md', '# note')).toBe(true);
+ expect(await s.writeCustomSkillFile('edited-draft', 'notes.md', '# note')).toBe(true);
const meta = JSON.parse(fs.readFileSync(path.join(customSkillsDir(), 'edited-draft', '_meta.json'), 'utf8'));
expect(meta._import).toBeUndefined();
});
@@ -1892,7 +1892,7 @@ describe('skills › writeCustomSkillFile (path safety)', () => {
it('writes a file inside the skill dir', async () => {
writeCustomSkill('alpha');
const s = await loadSkills();
- expect(s.writeCustomSkillFile('alpha', 'note.md', '# content')).toBe(true);
+ expect(await s.writeCustomSkillFile('alpha', 'note.md', '# content')).toBe(true);
const written = fs.readFileSync(
path.join(customSkillsDir(), 'alpha', 'note.md'), 'utf8');
expect(written).toBe('# content');
@@ -1901,25 +1901,25 @@ describe('skills › writeCustomSkillFile (path safety)', () => {
it('rejects path traversal attempts', async () => {
writeCustomSkill('alpha');
const s = await loadSkills();
- expect(s.writeCustomSkillFile('alpha', '../../evil.md', 'x')).toBe(false);
- expect(s.writeCustomSkillFile('alpha', 'sub/../../evil.md', 'x')).toBe(false);
+ expect(await s.writeCustomSkillFile('alpha', '../../evil.md', 'x')).toBe(false);
+ expect(await s.writeCustomSkillFile('alpha', 'sub/../../evil.md', 'x')).toBe(false);
});
it('rejects empty relpath', async () => {
writeCustomSkill('alpha');
const s = await loadSkills();
- expect(s.writeCustomSkillFile('alpha', '', 'x')).toBe(false);
+ expect(await s.writeCustomSkillFile('alpha', '', 'x')).toBe(false);
});
it('returns false when skill does not exist', async () => {
const s = await loadSkills();
- expect(s.writeCustomSkillFile('ghost', 'note.md', 'x')).toBe(false);
+ expect(await s.writeCustomSkillFile('ghost', 'note.md', 'x')).toBe(false);
});
it('normalizes SKILL.md writes to Orkas-supported frontmatter fields', async () => {
writeCustomSkill('alpha', 'name: "alpha"\ndescription_en: "old"\ncategory: "general"', 'old body');
const s = await loadSkills();
- const result = s.writeCustomSkillFileChecked('alpha', 'SKILL.md', [
+ const result = await s.writeCustomSkillFileChecked('alpha', 'SKILL.md', [
'---',
'name: "alpha"',
'description: "Legacy English"',
@@ -2805,3 +2805,67 @@ describe('skills › bounded creation correction', () => {
expect(fs.existsSync(path.join(tmpDir, 'another-account', 'cloud', 'skills', 'repaired-skill'))).toBe(false);
});
});
+
+describe('skills › operator policy write gates', () => {
+ function enablePolicy(rules: unknown[], raw?: string) {
+ const config = path.join(tmpDir, TEST_UID, 'local', 'config');
+ fs.mkdirSync(config, { recursive: true });
+ fs.writeFileSync(path.join(config, 'operator-policy-enabled.json'), '{"enabled":true}');
+ fs.writeFileSync(path.join(config, 'operator-policy.json'), raw ?? JSON.stringify({ version: 1, rules }));
+ }
+ const restriction = { id: 'private', level: 'EXTREME', pattern: 'private-resource', appliesTo: ['script', 'skill_meta'] };
+ it('keeps an existing file intact on invalid configuration and recovers after disable', async () => {
+ const s = await loadSkills();
+ writeCustomSkill('policy-edit');
+ const file = path.join(customSkillsDir(), 'policy-edit', 'run.sh');
+ fs.writeFileSync(file, 'original');
+ enablePolicy([], '{broken');
+ const blocked = await s.writeCustomSkillFileChecked('policy-edit', 'run.sh', 'replacement');
+ expect(blocked).toMatchObject({ ok: false, report: { violations: [expect.objectContaining({ rule: 'operator:incomplete' })] } });
+ expect(fs.readFileSync(file, 'utf8')).toBe('original');
+ const policy = await import('../../../src/main/features/operator-policy');
+ await policy.setOperatorPolicyEnabled(TEST_UID, false);
+ expect((await s.writeCustomSkillFileChecked('policy-edit', 'run.sh', 'replacement')).ok).toBe(true);
+ expect(fs.readFileSync(file, 'utf8')).toBe('replacement');
+ });
+ it('rejects a complete creation before allocation and cancels a clean proposal after async validation', async () => {
+ const s = await loadSkills();
+ enablePolicy([restriction]);
+ const proposal = { raw: '', files: [
+ { path: 'SKILL.md', content: '---\nname: policy-create\ndescription: Test\n---\nBody' },
+ { path: 'run.sh', content: 'echo private-resource' },
+ ] };
+ expect((await s.applySkillContainerFromCommander(proposal)).ok).toBe(false);
+ expect(fs.existsSync(path.join(customSkillsDir(), 'policy-create'))).toBe(false);
+ proposal.files[1].content = 'echo harmless';
+ let cancelled = false;
+ const pending = s.applySkillContainerFromCommander(proposal, { isCancelled: () => cancelled });
+ cancelled = true;
+ expect((await pending).ok).toBe(false);
+ expect(fs.existsSync(path.join(customSkillsDir(), 'policy-create'))).toBe(false);
+ });
+ it('rejects blocked imports even with force and publishes no partial Skill', async () => {
+ const s = await loadSkills();
+ const source = fs.mkdtempSync(path.join(process.cwd(), '.tmp-policy-import-'));
+ try {
+ fs.writeFileSync(path.join(source, 'SKILL.md'), '---\nname: policy-import\ndescription: Test\n---\nBody');
+ fs.writeFileSync(path.join(source, 'run.sh'), 'echo private-resource');
+ enablePolicy([restriction]);
+ const result = await s.createFromDir(null, null, source, { force: true });
+ expect(result.ok).toBe(false);
+ expect(result.report?.violations).toContainEqual(expect.objectContaining({ rule: 'operator:private' }));
+ expect(fs.readdirSync(customSkillsDir())).toEqual([]);
+ } finally { fs.rmSync(source, { recursive: true, force: true }); }
+ });
+ it('checks metadata before changing either Markdown or the sidecar', async () => {
+ const s = await loadSkills();
+ writeCustomSkill('policy-meta');
+ enablePolicy([restriction]);
+ const file = path.join(customSkillsDir(), 'policy-meta', 'SKILL.md');
+ const before = fs.readFileSync(file, 'utf8');
+ const result = await s.applySkillMetadataForEdit('policy-meta', { category: 'private-resource', description_en: 'new description' });
+ expect(result.ok).toBe(false);
+ expect(fs.readFileSync(file, 'utf8')).toBe(before);
+ expect(fs.existsSync(path.join(customSkillsDir(), 'policy-meta', '_meta.json'))).toBe(false);
+ });
+});
diff --git a/test/main/quality/operator-policy.test.ts b/test/main/quality/operator-policy.test.ts
index 4700e385..0d7850a5 100644
--- a/test/main/quality/operator-policy.test.ts
+++ b/test/main/quality/operator-policy.test.ts
@@ -2,124 +2,100 @@ import { describe, it, expect } from 'vitest';
import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
-
-import { validateSkillFile, validateSkillDir, parseOperatorPolicy } from '../../../src/main/quality';
-
-type Rules = ReturnType<typeof parseOperatorPolicy>['rules'];
-
-const POLICY = JSON.stringify({ rules: [
- { id: 'no_internal_bucket', level: 'EXTREME', pattern: 's3://acme-internal',
- message: 'Internal buckets are off limits for skills.' },
- { id: 'host_allowlist', level: 'MEDIUM', pattern: 'curl\\b[^\\n]*https?://(?!api\\.acme\\.example)',
- appliesTo: ['script'] },
-] });
-
-const md = (b: string) => ['---', 'name: internal-report', 'description: Build the weekly report', '---', b].join('\n');
-
-function report(content: string, operatorRules?: Rules) {
- const r = validateSkillFile({ relpath: 'SKILL.md', content, operatorRules });
- return { ok: r.ok, violations: r.violations, validator_version: r.validator_version };
+import { parseOperatorPolicy, runOperatorPolicy, validateSkillFile } from '../../../src/main/quality';
+const rule = { id: 'internal_bucket', level: 'EXTREME', pattern: 's3://acme-internal', message: 'Internal buckets are off limits.' };
+const policy = (rules: unknown[] = [rule]) => JSON.stringify({ version: 1, rules });
+const md = (body: string) => `---\nname: weekly-report\ndescription: Build the weekly report\n---\n${body}`;
+async function scan(content: string, rules: unknown[] = [rule], relpath = 'scripts/run.sh') {
+ const result = await runOperatorPolicy(policy(rules), { kind: 'files', files: [{ relpath, content }] });
+ expect(result).toHaveProperty('reports');
+ if (!('reports' in result)) throw new Error('scan failed');
+ return result.reports[0];
}
-describe('quality › operator policy rules', () => {
- it('parses a valid rule file and applies the documented defaults', () => {
- const { rules, errors } = parseOperatorPolicy(POLICY);
- expect(rules.map((r) => r.id)).toEqual(['no_internal_bucket', 'host_allowlist']);
- expect(rules[0].appliesTo).toEqual(['skill_md', 'script', 'agent_json']);
- expect(rules[1].appliesTo).toEqual(['script']);
+describe('operator policy', () => {
+ it('loads versioned policy with a separate rule namespace and defaults', () => {
+ const parsed = parseOperatorPolicy(policy());
+ expect(parsed.errors).toEqual([]);
+ expect(parsed.rules[0]).toMatchObject({ id: 'operator:internal_bucket', appliesTo: ['skill_md', 'script', 'agent_json'] });
});
-
- it('leaves the report byte-identical when no operator rules are configured', () => {
- const content = md('```bash\naws s3 cp s3://acme-internal/x .\n```\n');
- expect(report(content).violations).toEqual([]); // nothing on the built-in floor fires here
- for (const rules of [undefined, []]) expect(report(content, rules)).toEqual(report(content));
- });
-
- it('adds an operator finding tagged source=operator-policy', () => {
- const { rules } = parseOperatorPolicy(POLICY);
- const r = validateSkillFile({
- relpath: 'SKILL.md',
- content: md('```bash\naws s3 cp s3://acme-internal/x .\n```\n'),
- operatorRules: rules,
- });
- const hit = r.violations.find((v) => v.rule === 'no_internal_bucket');
- expect(hit).toMatchObject({ level: 'EXTREME', source: 'operator-policy' });
- expect(r.ok).toBe(false);
- });
-
- it('cannot suppress, downgrade, or rewrite a built-in finding', () => {
- const { rules } = parseOperatorPolicy(JSON.stringify({ rules: [
- { id: 'downgrade_attempt', level: 'LOW', pattern: 'security\\s+find-generic-password' }] }));
- const content = md('```bash\nsecurity find-generic-password -s x -w\n```\n');
- const before = report(content);
- const after = report(content, rules);
- expect(before.violations.find((v) => v.rule === 'no_credential_path_read')?.level).toBe('EXTREME');
- // Operator rules only ever add: every built-in finding survives unchanged.
- for (const v of before.violations) {
- expect(after.violations.find((x) => x.rule === v.rule && x.field === v.field)).toEqual(v);
+ it('rejects the entire config for unsupported versions, typos, types, duplicates, regex syntax or excess rules', () => {
+ for (const config of [
+ { rules: [rule] }, { version: 2, rules: [rule] }, { version: 1, rules: [rule], typo: true },
+ ...[{ ...rule, flags: 5 }, { ...rule, message: false }, { ...rule, typo: 1 }, { ...rule, pattern: '(' },
+ { ...rule, flags: 'g' }, { ...rule, appliesTo: ['other'] }].map(bad => ({ version: 1, rules: [rule, bad] })),
+ { version: 1, rules: [rule, rule] }, { version: 1, rules: Array.from({ length: 51 }, (_, i) => ({ ...rule, id: `r${i}` })) },
+ ]) {
+ const parsed = parseOperatorPolicy(JSON.stringify(config));
+ expect(parsed.errors.length).toBeGreaterThan(0);
+ expect(parsed.rules).toEqual([]);
}
+ });
+ it('retains built-in findings verbatim and allows only additive policy findings', async () => {
+ const content = 'security find-generic-password -s x -w';
+ const before = validateSkillFile({ relpath: 'scripts/run.sh', content });
+ const after = await scan(content, [{ ...rule, id: 'no_credential_path_read', level: 'LOW', pattern: 'security' }]);
expect(after.ok).toBe(false);
+ for (const finding of before.violations) expect(after.violations).toContainEqual(finding);
+ expect(after.violations).toContainEqual(expect.objectContaining({ rule: 'operator:no_credential_path_read', source: 'operator-policy', level: 'LOW' }));
});
-
- it('rejects malformed rules and ids that would shadow the floor', () => {
- expect(parseOperatorPolicy('{not json').errors[0]).toMatch(/not valid JSON/);
- expect(parseOperatorPolicy('{}').errors[0]).toMatch(/"rules" array/);
- const cases: Array<[unknown, RegExp]> = [
- [{ id: 'no_credential_path_read', level: 'LOW', pattern: 'x' }, /collides with a built-in rule/],
- [{ id: 'other_only', level: 'LOW', pattern: 'x', appliesTo: ['other'] }, /"appliesTo" must be a non-empty array/],
- ];
- for (const [rule, expected] of cases) {
- const parsed = parseOperatorPolicy(JSON.stringify({ rules: [rule] }));
- expect(parsed.rules).toEqual([]);
- expect(parsed.errors[0]).toMatch(expected);
- }
+ it('routes only executable Markdown fences to skill_md, not prose or script-only rules', async () => {
+ const content = md('s3://acme-internal prose\n```bash\necho s3://acme-internal\n```');
+ expect((await scan(content, [rule], 'SKILL.md')).violations).toContainEqual(expect.objectContaining({ rule: 'operator:internal_bucket' }));
+ expect((await scan(content, [{ ...rule, appliesTo: ['script'] }], 'SKILL.md')).violations).toEqual([]);
+ expect((await scan(md('s3://acme-internal prose'), [rule], 'SKILL.md')).violations).toEqual([]);
+ });
+ it('checks sidecar and Agent JSON using their own target kinds', async () => {
+ expect((await scan('{"category":"s3://acme-internal"}', [{ ...rule, appliesTo: ['skill_meta'] }], '_meta.json')).violations)
+ .toContainEqual(expect.objectContaining({ rule: 'operator:internal_bucket', source: 'operator-policy' }));
+ const result = await runOperatorPolicy(policy(), { kind: 'agent', args: { agentJson: { name: 'Writer', workflow: 's3://acme-internal' } } });
+ expect(result).toMatchObject({ reports: [{ ok: false, violations: expect.arrayContaining([expect.objectContaining({ rule: 'operator:internal_bucket' })]) }] });
});
-
- it('honours skill_md rules on executable blocks and keeps script rules scoped to scripts', () => {
- const content = md('```bash\necho acme-secret\n```\n');
- const onlyMd = parseOperatorPolicy(JSON.stringify({ rules: [
- { id: 'skill_md_only', level: 'MEDIUM', pattern: 'acme-secret', appliesTo: ['skill_md'] }] })).rules;
- const onlyScript = parseOperatorPolicy(JSON.stringify({ rules: [
- { id: 'script_only', level: 'MEDIUM', pattern: 'acme-secret', appliesTo: ['script'] }] })).rules;
-
- expect(validateSkillFile({ relpath: 'SKILL.md', content, operatorRules: onlyMd }).violations)
- .toEqual(expect.arrayContaining([expect.objectContaining({ rule: 'skill_md_only', source: 'operator-policy' })]));
- expect(validateSkillFile({ relpath: 'SKILL.md', content, operatorRules: onlyScript }).violations)
- .not.toEqual(expect.arrayContaining([expect.objectContaining({ rule: 'script_only' })]));
+ it('scans directory scripts and sidecars and rejects unreadable or linked trees', async () => {
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'policy-dir-'));
+ try {
+ fs.writeFileSync(path.join(dir, 'SKILL.md'), md('Body'));
+ fs.writeFileSync(path.join(dir, '_meta.json'), '{"category":"s3://acme-internal"}');
+ fs.mkdirSync(path.join(dir, 'scripts'));
+ fs.writeFileSync(path.join(dir, 'scripts/run.sh'), 'echo s3://acme-internal');
+ const result = await runOperatorPolicy(policy([{ ...rule, appliesTo: ['script', 'skill_meta'] }]), { kind: 'directory', dir });
+ expect(result).toMatchObject({ reports: [{ violations: expect.arrayContaining([
+ expect.objectContaining({ rule: 'operator:internal_bucket', field: '_meta.json:1' }),
+ expect.objectContaining({ rule: 'operator:internal_bucket', field: 'scripts/run.sh:1' }),
+ ]) }] });
+ fs.unlinkSync(path.join(dir, 'SKILL.md'));
+ expect(await runOperatorPolicy(policy(), { kind: 'directory', dir })).toEqual({ error: 'scan' });
+ fs.writeFileSync(path.join(dir, 'SKILL.md'), md('Body'));
+ fs.symlinkSync(path.join(dir, 'SKILL.md'), path.join(dir, 'link'));
+ expect(await runOperatorPolicy(policy(), { kind: 'directory', dir })).toEqual({ error: 'scan' });
+ } finally { fs.rmSync(dir, { recursive: true, force: true }); }
});
-
- it('applies skill_meta rules to file- and directory-level sidecar validation', () => {
- const content = JSON.stringify({ category: 'acme-secret' });
- const { rules } = parseOperatorPolicy(JSON.stringify({ rules: [
- { id: 'meta_secret', level: 'MEDIUM', pattern: 'acme-secret', appliesTo: ['skill_meta'] }] }));
-
- expect(validateSkillFile({ relpath: '_meta.json', content, operatorRules: rules }).violations)
- .toEqual(expect.arrayContaining([expect.objectContaining({ rule: 'meta_secret', field: '_meta.json:1' })]));
-
- const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'quality-op-meta-'));
- fs.writeFileSync(path.join(dir, 'SKILL.md'), md('No executable content.'));
- fs.writeFileSync(path.join(dir, '_meta.json'), content);
- expect(validateSkillDir(dir, { operatorRules: rules }).violations)
- .toEqual(expect.arrayContaining([expect.objectContaining({ rule: 'meta_secret', field: '_meta.json:1' })]));
+ it('terminates catastrophic regex without blocking the event loop and recovers on the next scan', async () => {
+ let ticks = 0;
+ const timer = setInterval(() => ticks++, 10);
+ const start = performance.now();
+ try {
+ const result = await runOperatorPolicy(policy([{ ...rule, pattern: '^(a+)+$' }]), { kind: 'files', files: [{ relpath: 'run.sh', content: 'a'.repeat(31) + '!' }] });
+ expect(result).toEqual({ error: 'timeout' });
+ expect(ticks).toBeGreaterThan(10);
+ expect(performance.now() - start).toBeLessThan(4000);
+ expect((await scan('echo harmless')).ok).toBe(true);
+ } finally { clearInterval(timer); }
+ }, 7000);
+ it('bounds concurrent work and input without spawning unbounded workers', async () => {
+ const slow = () => runOperatorPolicy(policy([{ ...rule, pattern: '^(a+)+$' }]), { kind: 'files', files: [{ relpath: 'run.sh', content: 'a'.repeat(31) + '!' }] });
+ const a = slow(); const b = slow();
+ expect(await slow()).toEqual({ error: 'busy' });
+ await Promise.all([a, b]);
+ expect(await runOperatorPolicy(policy(), { kind: 'files', files: [{ relpath: 'run.sh', content: 'x'.repeat(2 * 1024 * 1024) }] })).toEqual({ error: 'size' });
+ }, 7000);
+ it('bounds finding volume instead of returning an oversized partial report', async () => {
+ const rules = Array.from({ length: 50 }, (_, index) => ({ ...rule, id: `match${index}`, pattern: 'hit' }));
+ const result = await runOperatorPolicy(policy(rules), { kind: 'files', files: Array.from({ length: 12 }, (_, index) => ({ relpath: `run${index}.sh`, content: 'echo hit' })) });
+ expect(result).toEqual({ error: 'size' });
});
-
- it('scans on-disk scripts, honours appliesTo, and never flips the floor verdict', () => {
- const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'quality-op-'));
- fs.writeFileSync(path.join(dir, 'SKILL.md'), md('See scripts/report.sh.'));
- fs.mkdirSync(path.join(dir, 'scripts'));
- fs.writeFileSync(path.join(dir, 'scripts', 'report.sh'), 'curl -fsS https://evil.example/x\n');
-
- const base = validateSkillDir(dir);
- const { rules } = parseOperatorPolicy(POLICY);
- const withPolicy = validateSkillDir(dir, { operatorRules: rules });
-
- expect(withPolicy.violations.find((v) => v.rule === 'host_allowlist')).toMatchObject({
- level: 'MEDIUM', source: 'operator-policy', field: 'scripts/report.sh:1',
- });
- // MEDIUM is advisory: it must not flip the built-in verdict.
- // appliesTo=['script'] keeps the rule out of a bare SKILL.md body.
- const onlyMd = validateSkillFile({
- relpath: 'SKILL.md', content: md('curl https://evil.example'), operatorRules: rules,
- });
- expect(onlyMd.violations).toEqual([]);
+ it('fails closed when a synchronous caller mistakenly supplies operator rules', () => {
+ const result = validateSkillFile({ relpath: 'run.sh', content: 'a'.repeat(31) + '!', operatorRules: parseOperatorPolicy(policy([{ ...rule, pattern: '^(a+)+$' }])).rules });
+ expect(result.ok).toBe(false);
+ expect(result.violations).toContainEqual(expect.objectContaining({ rule: 'operator:incomplete' }));
});
});
diff --git a/test/renderer/validation-report-view.test.ts b/test/renderer/validation-report-view.test.ts
index 246f37f9..26724ed9 100644
--- a/test/renderer/validation-report-view.test.ts
+++ b/test/renderer/validation-report-view.test.ts
@@ -165,6 +165,20 @@ describe('quality validation report renderer', () => {
expect(ctx._documentListeners.has('keydown')).toBe(false);
});
+ it('shows operator guidance verbatim and removes the force action for policy failures', async () => {
+ const ctx = loadValidationReportView();
+ const result = ctx.showValidationReport({ report: { ok: false, violations: [{
+ rule: 'no_eval_with_external_input', source: 'operator-policy', level: 'EXTREME',
+ field: 'run.sh', snippet: '', suggested_fix: 'Operator <private> guidance',
+ }] }, forceLabel: 'Install anyway' });
+ const overlay = ctx._overlays[0] as FakeOverlay;
+ expect(overlay.innerHTML).toContain('Operator &lt;private&gt; guidance');
+ expect(overlay.innerHTML).not.toContain('Use a fixed command.');
+ expect(overlay.innerHTML).not.toContain('Install anyway');
+ overlay.okButton.click();
+ await expect(result).resolves.toBe('close');
+ });
+
it('lets keyboard activation of the focused override button choose force', async () => {
const ctx = loadValidationReportView();
const result = ctx.showValidationReport({