@aria_asi/cli 0.2.36 → 0.2.37
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CLIENT-ONBOARDING.md +4 -2
- package/bin/aria.js +11 -7
- package/dist/aria-connector/src/auth.d.ts +14 -0
- package/dist/aria-connector/src/auth.d.ts.map +1 -1
- package/dist/aria-connector/src/auth.js +103 -1
- package/dist/aria-connector/src/auth.js.map +1 -1
- package/dist/aria-connector/src/chat.d.ts.map +1 -1
- package/dist/aria-connector/src/chat.js +13 -8
- package/dist/aria-connector/src/chat.js.map +1 -1
- package/dist/aria-connector/src/config.d.ts +6 -1
- package/dist/aria-connector/src/config.d.ts.map +1 -1
- package/dist/aria-connector/src/config.js.map +1 -1
- package/dist/aria-connector/src/connectors/claude-code.d.ts.map +1 -1
- package/dist/aria-connector/src/connectors/claude-code.js +50 -6
- package/dist/aria-connector/src/connectors/claude-code.js.map +1 -1
- package/dist/aria-connector/src/connectors/codex.d.ts.map +1 -1
- package/dist/aria-connector/src/connectors/codex.js +310 -10
- package/dist/aria-connector/src/connectors/codex.js.map +1 -1
- package/dist/aria-connector/src/connectors/opencode.d.ts.map +1 -1
- package/dist/aria-connector/src/connectors/opencode.js +35 -11
- package/dist/aria-connector/src/connectors/opencode.js.map +1 -1
- package/dist/aria-connector/src/connectors/repo-guard.d.ts +10 -0
- package/dist/aria-connector/src/connectors/repo-guard.d.ts.map +1 -1
- package/dist/aria-connector/src/connectors/repo-guard.js +110 -164
- package/dist/aria-connector/src/connectors/repo-guard.js.map +1 -1
- package/dist/aria-connector/src/connectors/runtime.d.ts.map +1 -1
- package/dist/aria-connector/src/connectors/runtime.js +17 -7
- package/dist/aria-connector/src/connectors/runtime.js.map +1 -1
- package/dist/aria-connector/src/connectors/shell.d.ts.map +1 -1
- package/dist/aria-connector/src/connectors/shell.js +12 -8
- package/dist/aria-connector/src/connectors/shell.js.map +1 -1
- package/dist/aria-connector/src/harness-client.d.ts +3 -1
- package/dist/aria-connector/src/harness-client.d.ts.map +1 -1
- package/dist/aria-connector/src/harness-client.js +7 -20
- package/dist/aria-connector/src/harness-client.js.map +1 -1
- package/dist/aria-connector/src/model-context.d.ts.map +1 -1
- package/dist/aria-connector/src/model-context.js +5 -0
- package/dist/aria-connector/src/model-context.js.map +1 -1
- package/dist/aria-connector/src/providers/types.d.ts +1 -1
- package/dist/aria-connector/src/providers/types.d.ts.map +1 -1
- package/dist/aria-connector/src/providers/xai.d.ts +3 -0
- package/dist/aria-connector/src/providers/xai.d.ts.map +1 -0
- package/dist/aria-connector/src/providers/xai.js +40 -0
- package/dist/aria-connector/src/providers/xai.js.map +1 -0
- package/dist/aria-connector/src/setup-wizard.js +1 -0
- package/dist/aria-connector/src/setup-wizard.js.map +1 -1
- package/dist/aria-connector/src/types.d.ts +2 -0
- package/dist/aria-connector/src/types.d.ts.map +1 -1
- package/dist/assets/hooks/aria-cognition-substrate-binding.mjs +51 -9
- package/dist/assets/hooks/aria-first-class-coach.mjs +129 -0
- package/dist/assets/hooks/aria-harness-via-sdk.mjs +33 -6
- package/dist/assets/hooks/aria-pre-tool-gate.mjs +33 -8
- package/dist/assets/hooks/aria-preprompt-consult.mjs +5 -6
- package/dist/assets/hooks/aria-preturn-memory-gate.mjs +5 -0
- package/dist/assets/hooks/aria-repo-doctrine-gate.mjs +15 -0
- package/dist/assets/hooks/aria-stop-gate.mjs +125 -17
- package/dist/assets/hooks/doctrine_trigger_map.json +11 -0
- package/dist/assets/hooks/lib/emergency-gateoff-impl.mjs +39 -0
- package/dist/assets/hooks/lib/emergency-gateoff.mjs +6 -0
- package/dist/assets/hooks/lib/first-class-coach.mjs +755 -0
- package/dist/assets/hooks/lib/skill-autoload-gate-impl.mjs +103 -0
- package/dist/assets/hooks/lib/skill-autoload-gate.mjs +1 -14
- package/dist/assets/opencode-plugins/harness-context/auth-token.mjs +126 -0
- package/dist/assets/opencode-plugins/harness-context/inject-context.mjs +62 -22
- package/dist/assets/opencode-plugins/harness-context/task-project-ledger.mjs +290 -0
- package/dist/assets/opencode-plugins/harness-gate/index.js +87 -27
- package/dist/assets/opencode-plugins/harness-gate/lib/skill-autoload-gate.js +1 -14
- package/dist/assets/opencode-plugins/harness-outcome/index.js +29 -24
- package/dist/assets/opencode-plugins/harness-stop/index.js +229 -68
- package/dist/assets/opencode-plugins/harness-stop/lib/skill-autoload-gate.js +1 -14
- package/dist/runtime/auth-token.mjs +121 -0
- package/dist/runtime/coach-kernel.mjs +371 -0
- package/dist/runtime/codex-bridge.mjs +440 -69
- package/dist/runtime/discipline/doctrine_trigger_map.json +11 -0
- package/dist/runtime/discipline/skills/aria-cognition/aria-essence/SKILL.md +18 -0
- package/dist/runtime/discipline/skills/aria-cognition/aria-forge-guardrails/SKILL.md +18 -0
- package/dist/runtime/discipline/skills/aria-cognition/aria-repo-doctrine/SKILL.md +18 -0
- package/dist/runtime/discipline/skills/aria-cognition/forge-quality-rules/SKILL.md +18 -0
- package/dist/runtime/discipline/skills/aria-cognition/ghazali-8lens/SKILL.md +18 -0
- package/dist/runtime/discipline/skills/aria-cognition/istiqra-induction/SKILL.md +18 -0
- package/dist/runtime/discipline/skills/aria-cognition/ladunni-22/SKILL.md +18 -0
- package/dist/runtime/discipline/skills/aria-cognition/mizan/SKILL.md +18 -0
- package/dist/runtime/discipline/skills/aria-cognition/nadia/SKILL.md +18 -0
- package/dist/runtime/discipline/skills/aria-cognition/nadia-psi/SKILL.md +18 -0
- package/dist/runtime/discipline/skills/aria-cognition/predictor/SKILL.md +18 -0
- package/dist/runtime/discipline/skills/aria-cognition/qiyas-analogy/SKILL.md +18 -0
- package/dist/runtime/discipline/skills/aria-cognition/soul-domains/SKILL.md +18 -0
- package/dist/runtime/discipline/skills/aria-harness/aria-aristotle-intra-phase/SKILL.md +18 -0
- package/dist/runtime/discipline/skills/aria-harness/aria-aristotle-post-phase/SKILL.md +18 -0
- package/dist/runtime/discipline/skills/aria-harness/aria-aristotle-pre-phase/SKILL.md +18 -0
- package/dist/runtime/discipline/skills/aria-harness/aria-harness-deploy/SKILL.md +18 -0
- package/dist/runtime/discipline/skills/aria-harness/aria-harness-no-stripping/SKILL.md +18 -0
- package/dist/runtime/discipline/skills/aria-harness/aria-harness-onboarding/SKILL.md +18 -0
- package/dist/runtime/discipline/skills/aria-harness/aria-harness-output-discipline/SKILL.md +18 -0
- package/dist/runtime/discipline/skills/aria-harness/aria-harness-substrate-binding/SKILL.md +18 -0
- package/dist/runtime/doctrine_trigger_map.json +11 -0
- package/dist/runtime/hooks/aria-cognition-substrate-binding.mjs +51 -9
- package/dist/runtime/hooks/aria-first-class-coach.mjs +129 -0
- package/dist/runtime/hooks/aria-harness-via-sdk.mjs +33 -6
- package/dist/runtime/hooks/aria-pre-tool-gate.mjs +33 -8
- package/dist/runtime/hooks/aria-preprompt-consult.mjs +5 -6
- package/dist/runtime/hooks/aria-preturn-memory-gate.mjs +5 -0
- package/dist/runtime/hooks/aria-repo-doctrine-gate.mjs +15 -0
- package/dist/runtime/hooks/aria-stop-gate.mjs +125 -17
- package/dist/runtime/hooks/doctrine_trigger_map.json +11 -0
- package/dist/runtime/hooks/lib/emergency-gateoff-impl.mjs +39 -0
- package/dist/runtime/hooks/lib/emergency-gateoff.mjs +6 -0
- package/dist/runtime/hooks/lib/first-class-coach.mjs +755 -0
- package/dist/runtime/hooks/lib/skill-autoload-gate-impl.mjs +103 -0
- package/dist/runtime/hooks/lib/skill-autoload-gate.mjs +1 -14
- package/dist/runtime/local-phase.mjs +8 -0
- package/dist/runtime/manifest.json +2 -2
- package/dist/runtime/provider-proxy.mjs +136 -33
- package/dist/runtime/sdk/BUNDLED.json +2 -2
- package/dist/runtime/sdk/auth.d.ts +17 -0
- package/dist/runtime/sdk/auth.js +158 -0
- package/dist/runtime/sdk/auth.js.map +1 -0
- package/dist/runtime/sdk/index.d.ts +8 -1
- package/dist/runtime/sdk/index.js +15 -1
- package/dist/runtime/sdk/index.js.map +1 -1
- package/dist/runtime/service.mjs +1711 -74
- package/dist/runtime/task-project-ledger.mjs +290 -0
- package/dist/sdk/BUNDLED.json +2 -2
- package/dist/sdk/auth.d.ts +17 -0
- package/dist/sdk/auth.js +158 -0
- package/dist/sdk/auth.js.map +1 -0
- package/dist/sdk/index.d.ts +8 -1
- package/dist/sdk/index.js +15 -1
- package/dist/sdk/index.js.map +1 -1
- package/hooks/aria-cognition-substrate-binding.mjs +51 -9
- package/hooks/aria-first-class-coach.mjs +129 -0
- package/hooks/aria-harness-via-sdk.mjs +33 -6
- package/hooks/aria-pre-tool-gate.mjs +33 -8
- package/hooks/aria-preprompt-consult.mjs +5 -6
- package/hooks/aria-preturn-memory-gate.mjs +5 -0
- package/hooks/aria-repo-doctrine-gate.mjs +15 -0
- package/hooks/aria-stop-gate.mjs +125 -17
- package/hooks/doctrine_trigger_map.json +11 -0
- package/hooks/lib/emergency-gateoff-impl.mjs +39 -0
- package/hooks/lib/emergency-gateoff.mjs +6 -0
- package/hooks/lib/first-class-coach.mjs +755 -0
- package/hooks/lib/skill-autoload-gate-impl.mjs +103 -0
- package/hooks/lib/skill-autoload-gate.mjs +1 -14
- package/opencode-plugins/harness-context/auth-token.mjs +126 -0
- package/opencode-plugins/harness-context/inject-context.mjs +62 -22
- package/opencode-plugins/harness-context/task-project-ledger.mjs +290 -0
- package/opencode-plugins/harness-gate/index.js +87 -27
- package/opencode-plugins/harness-gate/lib/skill-autoload-gate.js +1 -14
- package/opencode-plugins/harness-outcome/index.js +29 -24
- package/opencode-plugins/harness-stop/index.js +229 -68
- package/opencode-plugins/harness-stop/lib/skill-autoload-gate.js +1 -14
- package/package.json +8 -2
- package/runtime-src/auth-token.mjs +121 -0
- package/runtime-src/coach-kernel.mjs +371 -0
- package/runtime-src/codex-bridge.mjs +440 -69
- package/runtime-src/local-phase.mjs +8 -0
- package/runtime-src/provider-proxy.mjs +136 -33
- package/runtime-src/service.mjs +1711 -74
- package/scripts/bundle-sdk.mjs +8 -0
- package/scripts/check-client-compatibility.mjs +422 -0
- package/scripts/check-coach-kernel.mjs +204 -0
- package/scripts/check-managed-runtime-ledger.mjs +107 -0
- package/scripts/check-opencode-config-contract.mjs +78 -0
- package/scripts/check-quality-ledger.mjs +121 -0
- package/scripts/self-test-harness-gates.mjs +179 -11
- package/scripts/self-test-repo-guard.mjs +38 -0
- package/scripts/validate-skill-prompts.mjs +14 -1
- package/skills/aria-cognition/aria-essence/SKILL.md +18 -0
- package/skills/aria-cognition/aria-forge-guardrails/SKILL.md +18 -0
- package/skills/aria-cognition/aria-repo-doctrine/SKILL.md +18 -0
- package/skills/aria-cognition/forge-quality-rules/SKILL.md +18 -0
- package/skills/aria-cognition/ghazali-8lens/SKILL.md +18 -0
- package/skills/aria-cognition/istiqra-induction/SKILL.md +18 -0
- package/skills/aria-cognition/ladunni-22/SKILL.md +18 -0
- package/skills/aria-cognition/mizan/SKILL.md +18 -0
- package/skills/aria-cognition/nadia/SKILL.md +18 -0
- package/skills/aria-cognition/nadia-psi/SKILL.md +18 -0
- package/skills/aria-cognition/predictor/SKILL.md +18 -0
- package/skills/aria-cognition/qiyas-analogy/SKILL.md +18 -0
- package/skills/aria-cognition/soul-domains/SKILL.md +18 -0
- package/src/auth.ts +136 -1
- package/src/chat.ts +13 -8
- package/src/config.ts +6 -1
- package/src/connectors/claude-code.ts +62 -18
- package/src/connectors/codex.ts +308 -10
- package/src/connectors/opencode.ts +35 -12
- package/src/connectors/repo-guard.ts +117 -172
- package/src/connectors/runtime.ts +19 -7
- package/src/connectors/shell.ts +12 -8
- package/src/harness-client.ts +8 -22
- package/src/model-context.ts +6 -0
- package/src/providers/types.ts +1 -1
- package/src/providers/xai.ts +55 -0
- package/src/setup-wizard.ts +1 -0
- package/src/types.ts +2 -0
|
@@ -0,0 +1,371 @@
|
|
|
1
|
+
import { appendFileSync, existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
3
|
+
import { dirname, join } from 'node:path';
|
|
4
|
+
import { fileURLToPath } from 'node:url';
|
|
5
|
+
|
|
6
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
7
|
+
|
|
8
|
+
export const COACH_EVENT_SCHEMA = 'aria.coach_kernel_event.v1';
|
|
9
|
+
export const COACH_STATE_SCHEMA = 'aria.coach_kernel_state.v1';
|
|
10
|
+
export const DEFAULT_COACH_STATE_DIR = join(__dirname, 'state');
|
|
11
|
+
export const DEFAULT_COACH_LEDGER_PATH = join(DEFAULT_COACH_STATE_DIR, 'coach-events.jsonl');
|
|
12
|
+
export const DEFAULT_COACH_STATE_PATH = join(DEFAULT_COACH_STATE_DIR, 'coach-state.json');
|
|
13
|
+
|
|
14
|
+
export const COACH_PHASES = Object.freeze([
|
|
15
|
+
'pre_turn',
|
|
16
|
+
'pre_cognition',
|
|
17
|
+
'post_cognition',
|
|
18
|
+
'pre_generation',
|
|
19
|
+
'post_generation',
|
|
20
|
+
'pre_tool',
|
|
21
|
+
'post_tool',
|
|
22
|
+
'pre_output',
|
|
23
|
+
'post_output',
|
|
24
|
+
'background_start',
|
|
25
|
+
'background_checkpoint',
|
|
26
|
+
'background_complete',
|
|
27
|
+
'claim_or_release',
|
|
28
|
+
]);
|
|
29
|
+
|
|
30
|
+
const ALLOWED_PHASES = new Set(COACH_PHASES);
|
|
31
|
+
const OUTPUT_PHASES = new Set(['post_generation', 'pre_output', 'post_output', 'claim_or_release']);
|
|
32
|
+
const TOOL_PHASES = new Set(['pre_tool', 'post_tool']);
|
|
33
|
+
const SECRET_KEY_RX = /\b(?:api[_-]?key|secret|token|authorization|password|credential|private[_-]?key)\b/i;
|
|
34
|
+
const SECRET_VALUE_RX = /\b(?:sk-[A-Za-z0-9_-]{16,}|xox[baprs]-[A-Za-z0-9-]{20,}|gh[pousr]_[A-Za-z0-9_]{20,}|Bearer\s+[A-Za-z0-9._~+\/-]{20,}|AKIA[0-9A-Z]{16})\b/i;
|
|
35
|
+
const COGNITION_RX = /<cognition>[\s\S]*?<\/cognition>/i;
|
|
36
|
+
const APPLIED_COGNITION_RX = /<applied_cognition>[\s\S]*?<\/applied_cognition>/i;
|
|
37
|
+
const VERIFY_RX = /<verify>[\s\S]*?(?:verified|rollback|target|predicate)[\s\S]*?<\/verify>/i;
|
|
38
|
+
const COMPLETION_CLAIM_RX = /\b(?:done|complete|completed|ready|verified|fixed|shipped|production-ready|release-ready|passing|passed)\b/i;
|
|
39
|
+
const MEASURABLE_EVIDENCE_RX = /\b(?:exit\s*0|0\s+failures?|passed|status\s*[:=]\s*(?:ok|200|healthy|pass)|verified\s*[:=]\s*true|ledger_record_id|receiptId|sha256|http\s*2\d\d)\b/i;
|
|
40
|
+
|
|
41
|
+
function nowIso() {
|
|
42
|
+
return new Date().toISOString();
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function stableString(value) {
|
|
46
|
+
if (typeof value === 'string') return value;
|
|
47
|
+
try {
|
|
48
|
+
return JSON.stringify(value);
|
|
49
|
+
} catch {
|
|
50
|
+
return String(value || '');
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function hashValue(value) {
|
|
55
|
+
return createHash('sha256').update(stableString(value)).digest('hex');
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function normalizeString(value, fallback = '') {
|
|
59
|
+
return typeof value === 'string' && value.trim() ? value.trim() : fallback;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function normalizeStringArray(values) {
|
|
63
|
+
return Array.from(new Set((Array.isArray(values) ? values : [])
|
|
64
|
+
.map((value) => normalizeString(value))
|
|
65
|
+
.filter(Boolean)))
|
|
66
|
+
.sort();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function redactPotentialSecrets(text) {
|
|
70
|
+
return String(text || '')
|
|
71
|
+
.replace(/Bearer\s+[A-Za-z0-9._~+\/-]{12,}/gi, 'Bearer [redacted]')
|
|
72
|
+
.replace(/\bsk-[A-Za-z0-9_-]{12,}\b/g, '[redacted-secret]')
|
|
73
|
+
.replace(/\bgh[pousr]_[A-Za-z0-9_]{12,}\b/g, '[redacted-secret]')
|
|
74
|
+
.replace(/\bxox[baprs]-[A-Za-z0-9-]{12,}\b/g, '[redacted-secret]')
|
|
75
|
+
.replace(/\bAKIA[0-9A-Z]{16}\b/g, '[redacted-secret]');
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function redactedClone(value, depth = 0) {
|
|
79
|
+
if (depth > 5) return '[truncated]';
|
|
80
|
+
if (typeof value === 'string') return redactPotentialSecrets(value).slice(0, 2000);
|
|
81
|
+
if (typeof value === 'number' || typeof value === 'boolean' || value == null) return value;
|
|
82
|
+
if (Array.isArray(value)) return value.slice(0, 50).map((entry) => redactedClone(entry, depth + 1));
|
|
83
|
+
if (typeof value !== 'object') return String(value);
|
|
84
|
+
const out = {};
|
|
85
|
+
for (const [key, entry] of Object.entries(value).slice(0, 80)) {
|
|
86
|
+
if (SECRET_KEY_RX.test(key)) {
|
|
87
|
+
out[key] = '[redacted]';
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
out[key] = redactedClone(entry, depth + 1);
|
|
91
|
+
}
|
|
92
|
+
return out;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function normalizeEvidenceRefs(values) {
|
|
96
|
+
const raw = Array.isArray(values) ? values : [];
|
|
97
|
+
return raw.slice(0, 50).map((entry) => {
|
|
98
|
+
if (typeof entry === 'string') return redactPotentialSecrets(entry).slice(0, 500);
|
|
99
|
+
if (entry && typeof entry === 'object') return redactedClone(entry);
|
|
100
|
+
return String(entry || '').slice(0, 500);
|
|
101
|
+
}).filter((entry) => Boolean(typeof entry === 'string' ? entry.trim() : entry));
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function phaseOrDefault(phase) {
|
|
105
|
+
const candidate = normalizeString(phase, 'pre_turn');
|
|
106
|
+
return ALLOWED_PHASES.has(candidate) ? candidate : 'pre_turn';
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function hasEvidence(event, text) {
|
|
110
|
+
if (Array.isArray(event.evidence_refs) && event.evidence_refs.length > 0) return true;
|
|
111
|
+
if (event.validation?.passed === true || event.validation?.severity === 'pass') return true;
|
|
112
|
+
if (event.layer3?.pass === true) return true;
|
|
113
|
+
if (event.metadata?.validated_output === true || event.metadata?.verified === true) return true;
|
|
114
|
+
return MEASURABLE_EVIDENCE_RX.test(text || event.text_preview || '');
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function hasVerifyEvidence(event, text) {
|
|
118
|
+
if (event.metadata?.requireVerify === false) return true;
|
|
119
|
+
if (event.metadata?.verify === true || event.metadata?.verified === true) return true;
|
|
120
|
+
if (VERIFY_RX.test(text || event.text_preview || '')) return true;
|
|
121
|
+
return hasEvidence(event, text) && /\b(?:rollback|verify|verified|deployment|rollout|kubectl|terraform|helm|docker)\b/i.test(text || event.text_preview || '');
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function inferRiskClass(highRisk, repairable) {
|
|
125
|
+
if (highRisk.length > 0) return 'high';
|
|
126
|
+
if (repairable.length > 0) return 'medium';
|
|
127
|
+
return 'low';
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function decisionFromSignals(highRisk, repairable, warnings) {
|
|
131
|
+
if (highRisk.length > 0) {
|
|
132
|
+
return {
|
|
133
|
+
decision: 'hard_block',
|
|
134
|
+
permitted: false,
|
|
135
|
+
nextAction: 'do_not_release; write operator evidence and re-author only after the high-risk condition is removed',
|
|
136
|
+
reasons: highRisk,
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
if (repairable.length > 0) {
|
|
140
|
+
return {
|
|
141
|
+
decision: 'repair_once',
|
|
142
|
+
permitted: false,
|
|
143
|
+
nextAction: 'repair_or_regenerate_once_before_user_visible_release',
|
|
144
|
+
reasons: repairable,
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
if (warnings.length > 0) {
|
|
148
|
+
return {
|
|
149
|
+
decision: 'warn_operator_only',
|
|
150
|
+
permitted: true,
|
|
151
|
+
nextAction: 'continue_and_preserve_operator_warning',
|
|
152
|
+
reasons: warnings,
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
return {
|
|
156
|
+
decision: 'allow',
|
|
157
|
+
permitted: true,
|
|
158
|
+
nextAction: 'continue',
|
|
159
|
+
reasons: [],
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export function normalizeCoachEvent(input = {}) {
|
|
164
|
+
const phase = phaseOrDefault(input.phase);
|
|
165
|
+
const rawText = normalizeString(input.text || input.output || input.message || input.target || '');
|
|
166
|
+
const metadata = redactedClone(input.metadata && typeof input.metadata === 'object' ? input.metadata : {});
|
|
167
|
+
const record = {
|
|
168
|
+
coach_event_id: normalizeString(input.coachEventId || input.coach_event_id) || `coach_${randomUUID().replace(/-/g, '')}`,
|
|
169
|
+
request_id: normalizeString(input.requestId || input.request_id) || `req_${randomUUID().replace(/-/g, '')}`,
|
|
170
|
+
session_id: normalizeString(input.sessionId || input.session_id, 'unknown'),
|
|
171
|
+
surface: normalizeString(input.surface || input.client || input.platform, 'aria-runtime'),
|
|
172
|
+
lane: normalizeString(input.lane, 'managed_aria_provider'),
|
|
173
|
+
phase,
|
|
174
|
+
action: normalizeString(input.action),
|
|
175
|
+
target_hash: input.target ? hashValue(input.target) : null,
|
|
176
|
+
target_preview: input.target ? redactPotentialSecrets(String(input.target)).slice(0, 500) : null,
|
|
177
|
+
text_hash: rawText ? hashValue(rawText) : null,
|
|
178
|
+
text_preview: rawText ? redactPotentialSecrets(rawText).slice(0, 1000) : '',
|
|
179
|
+
harness_packet_hash: normalizeString(input.harnessPacketHash || input.harness_packet_hash),
|
|
180
|
+
role_profile: normalizeString(input.roleProfile || input.role_profile),
|
|
181
|
+
required_skill_ids: normalizeStringArray(input.requiredSkillIds || input.required_skill_ids),
|
|
182
|
+
loaded_skill_ids: normalizeStringArray(input.loadedSkillIds || input.loaded_skill_ids),
|
|
183
|
+
missing_skill_ids: normalizeStringArray(input.missingSkillIds || input.missing_skill_ids),
|
|
184
|
+
evidence_refs: normalizeEvidenceRefs(input.evidenceRefs || input.evidence_refs),
|
|
185
|
+
validation: redactedClone(input.validation || null),
|
|
186
|
+
layer3: redactedClone(input.layer3 || null),
|
|
187
|
+
quality_gate_status: normalizeString(input.qualityGateStatus || input.quality_gate_status, 'pending'),
|
|
188
|
+
compliance_gate_status: normalizeString(input.complianceGateStatus || input.compliance_gate_status, 'pending'),
|
|
189
|
+
metadata,
|
|
190
|
+
};
|
|
191
|
+
Object.defineProperty(record, 'rawText', { value: rawText, enumerable: false });
|
|
192
|
+
return record;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
export function evaluateCoachEvent(event = {}) {
|
|
196
|
+
const normalized = event.phase ? event : normalizeCoachEvent(event);
|
|
197
|
+
const text = normalized.rawText || normalized.text_preview || '';
|
|
198
|
+
const highRisk = [];
|
|
199
|
+
const repairable = [];
|
|
200
|
+
const warnings = [];
|
|
201
|
+
const action = String(normalized.action || '').toLowerCase();
|
|
202
|
+
|
|
203
|
+
if (!ALLOWED_PHASES.has(normalized.phase)) {
|
|
204
|
+
highRisk.push('unknown_coach_phase');
|
|
205
|
+
}
|
|
206
|
+
if (SECRET_VALUE_RX.test(text) || SECRET_VALUE_RX.test(stableString(normalized.metadata))) {
|
|
207
|
+
highRisk.push('secret_or_credential_exposure');
|
|
208
|
+
}
|
|
209
|
+
if (normalized.phase === 'pre_generation' && normalized.missing_skill_ids.length > 0) {
|
|
210
|
+
highRisk.push('required_skill_unavailable_before_generation');
|
|
211
|
+
}
|
|
212
|
+
if (TOOL_PHASES.has(normalized.phase) || action) {
|
|
213
|
+
if ((action === 'delete' || /\b(?:rm\s+-rf|drop\s+(?:table|database|schema)|git\s+reset\s+--hard)\b/i.test(text)) && normalized.metadata?.approved !== true) {
|
|
214
|
+
highRisk.push('unapproved_destructive_action');
|
|
215
|
+
}
|
|
216
|
+
if ((action === 'deploy' || /\b(?:kubectl\s+(?:apply|set|rollout|delete)|helm\s+upgrade|terraform\s+apply|docker\s+push)\b/i.test(text)) && !hasVerifyEvidence(normalized, text)) {
|
|
217
|
+
highRisk.push('unverified_deploy_or_infra_mutation');
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
if (OUTPUT_PHASES.has(normalized.phase)) {
|
|
221
|
+
const nonTrivial = text.length >= 300 || COMPLETION_CLAIM_RX.test(text);
|
|
222
|
+
if (nonTrivial && !COGNITION_RX.test(text) && normalized.metadata?.requireCognitionBlock !== false) {
|
|
223
|
+
repairable.push('missing_readable_cognition_before_release');
|
|
224
|
+
}
|
|
225
|
+
if (nonTrivial && !APPLIED_COGNITION_RX.test(text) && normalized.metadata?.requireAppliedCognition !== false) {
|
|
226
|
+
repairable.push('missing_applied_cognition_before_release');
|
|
227
|
+
}
|
|
228
|
+
if (COMPLETION_CLAIM_RX.test(text) && !hasEvidence(normalized, text)) {
|
|
229
|
+
repairable.push('unsupported_completion_or_verification_claim');
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
if (normalized.lane.includes('unmanaged') || normalized.metadata?.complianceGuarantee === 'best_effort_only') {
|
|
233
|
+
warnings.push('unmanaged_direct_provider_best_effort_only');
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const verdict = decisionFromSignals(highRisk, repairable, warnings);
|
|
237
|
+
return {
|
|
238
|
+
...verdict,
|
|
239
|
+
riskClass: inferRiskClass(highRisk, repairable),
|
|
240
|
+
highRisk,
|
|
241
|
+
repairable,
|
|
242
|
+
warnings,
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function readJson(pathname, fallback) {
|
|
247
|
+
if (!existsSync(pathname)) return fallback;
|
|
248
|
+
try {
|
|
249
|
+
return JSON.parse(readFileSync(pathname, 'utf8'));
|
|
250
|
+
} catch {
|
|
251
|
+
return fallback;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function summarizeRecord(record) {
|
|
256
|
+
return {
|
|
257
|
+
at: record.at,
|
|
258
|
+
coach_event_id: record.coach_event_id,
|
|
259
|
+
request_id: record.request_id,
|
|
260
|
+
session_id: record.session_id,
|
|
261
|
+
surface: record.surface,
|
|
262
|
+
lane: record.lane,
|
|
263
|
+
phase: record.phase,
|
|
264
|
+
decision: record.decision,
|
|
265
|
+
risk_class: record.risk_class,
|
|
266
|
+
action: record.action || null,
|
|
267
|
+
reasons: Array.isArray(record.reasons) ? record.reasons.slice(0, 6) : [],
|
|
268
|
+
evidence_count: Array.isArray(record.evidence_refs) ? record.evidence_refs.length : 0,
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
function updateCoachState(record, statePath) {
|
|
273
|
+
const state = readJson(statePath, {
|
|
274
|
+
schema: COACH_STATE_SCHEMA,
|
|
275
|
+
updated_at: null,
|
|
276
|
+
counts: { total: 0, by_phase: {}, by_decision: {} },
|
|
277
|
+
last_events: [],
|
|
278
|
+
last_by_session: {},
|
|
279
|
+
});
|
|
280
|
+
state.schema = COACH_STATE_SCHEMA;
|
|
281
|
+
state.updated_at = record.at;
|
|
282
|
+
state.counts = state.counts || { total: 0, by_phase: {}, by_decision: {} };
|
|
283
|
+
state.counts.total = Number(state.counts.total || 0) + 1;
|
|
284
|
+
state.counts.by_phase = state.counts.by_phase || {};
|
|
285
|
+
state.counts.by_decision = state.counts.by_decision || {};
|
|
286
|
+
state.counts.by_phase[record.phase] = Number(state.counts.by_phase[record.phase] || 0) + 1;
|
|
287
|
+
state.counts.by_decision[record.decision] = Number(state.counts.by_decision[record.decision] || 0) + 1;
|
|
288
|
+
const summary = summarizeRecord(record);
|
|
289
|
+
state.last_events = [...(Array.isArray(state.last_events) ? state.last_events : []), summary].slice(-100);
|
|
290
|
+
state.last_by_session = state.last_by_session || {};
|
|
291
|
+
state.last_by_session[record.session_id] = summary;
|
|
292
|
+
writeFileSync(statePath, `${JSON.stringify(state, null, 2)}\n`, { mode: 0o600 });
|
|
293
|
+
return state;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
export function recordCoachPhase(input = {}, options = {}) {
|
|
297
|
+
const ledgerPath = options.ledgerPath || DEFAULT_COACH_LEDGER_PATH;
|
|
298
|
+
const statePath = options.statePath || DEFAULT_COACH_STATE_PATH;
|
|
299
|
+
mkdirSync(dirname(ledgerPath), { recursive: true, mode: 0o700 });
|
|
300
|
+
mkdirSync(dirname(statePath), { recursive: true, mode: 0o700 });
|
|
301
|
+
const event = normalizeCoachEvent(input);
|
|
302
|
+
const evaluation = evaluateCoachEvent(event);
|
|
303
|
+
const record = {
|
|
304
|
+
schema: COACH_EVENT_SCHEMA,
|
|
305
|
+
at: nowIso(),
|
|
306
|
+
...event,
|
|
307
|
+
risk_class: evaluation.riskClass,
|
|
308
|
+
decision: evaluation.decision,
|
|
309
|
+
permitted: evaluation.permitted,
|
|
310
|
+
reasons: evaluation.reasons,
|
|
311
|
+
high_risk_signals: evaluation.highRisk,
|
|
312
|
+
repairable_signals: evaluation.repairable,
|
|
313
|
+
warnings: evaluation.warnings,
|
|
314
|
+
next_action: evaluation.nextAction,
|
|
315
|
+
};
|
|
316
|
+
appendFileSync(ledgerPath, `${JSON.stringify(record)}\n`, { mode: 0o600 });
|
|
317
|
+
const state = updateCoachState(record, statePath);
|
|
318
|
+
return {
|
|
319
|
+
ok: true,
|
|
320
|
+
permitted: record.permitted,
|
|
321
|
+
decision: record.decision,
|
|
322
|
+
ledgerPath,
|
|
323
|
+
statePath,
|
|
324
|
+
record,
|
|
325
|
+
state: summarizeCoachState(state),
|
|
326
|
+
clientMessage: formatCoachClientBlock(record),
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
export function summarizeCoachState(state = null) {
|
|
331
|
+
const source = state || readJson(DEFAULT_COACH_STATE_PATH, null);
|
|
332
|
+
if (!source) {
|
|
333
|
+
return {
|
|
334
|
+
schema: COACH_STATE_SCHEMA,
|
|
335
|
+
updated_at: null,
|
|
336
|
+
counts: { total: 0, by_phase: {}, by_decision: {} },
|
|
337
|
+
last_events: [],
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
return {
|
|
341
|
+
schema: source.schema || COACH_STATE_SCHEMA,
|
|
342
|
+
updated_at: source.updated_at || null,
|
|
343
|
+
counts: source.counts || { total: 0, by_phase: {}, by_decision: {} },
|
|
344
|
+
last_events: Array.isArray(source.last_events) ? source.last_events.slice(-25) : [],
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
export function readCoachState(options = {}) {
|
|
349
|
+
const statePath = options.statePath || DEFAULT_COACH_STATE_PATH;
|
|
350
|
+
const state = readJson(statePath, null);
|
|
351
|
+
return options.includeState === true ? state : summarizeCoachState(state);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
export function formatCoachClientBlock(recordOrResult = {}) {
|
|
355
|
+
const record = recordOrResult.record || recordOrResult;
|
|
356
|
+
const reasons = Array.isArray(record.reasons) && record.reasons.length
|
|
357
|
+
? record.reasons
|
|
358
|
+
: ['coach_kernel_policy_block'];
|
|
359
|
+
const status = record.decision === 'repair_once'
|
|
360
|
+
? 'Aria Coach held this turn for repair before release.'
|
|
361
|
+
: record.decision === 'hard_block'
|
|
362
|
+
? 'Aria Coach blocked this turn before release.'
|
|
363
|
+
: 'Aria Coach recorded an operator warning for this turn.';
|
|
364
|
+
const next = record.next_action || (record.decision === 'hard_block' ? 'remove the high-risk condition before retrying' : 'continue');
|
|
365
|
+
return [
|
|
366
|
+
status,
|
|
367
|
+
'',
|
|
368
|
+
`Reason: ${reasons.slice(0, 3).join('; ')}`,
|
|
369
|
+
`Next: ${next}`,
|
|
370
|
+
].join('\n');
|
|
371
|
+
}
|