@aria_asi/cli 0.2.31 → 0.2.32

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.
Files changed (52) hide show
  1. package/dist/aria-connector/src/connectors/claude-code.d.ts.map +1 -1
  2. package/dist/aria-connector/src/connectors/claude-code.js +30 -3
  3. package/dist/aria-connector/src/connectors/claude-code.js.map +1 -1
  4. package/dist/aria-connector/src/connectors/codex.d.ts.map +1 -1
  5. package/dist/aria-connector/src/connectors/codex.js +25 -9
  6. package/dist/aria-connector/src/connectors/codex.js.map +1 -1
  7. package/dist/aria-connector/src/connectors/must-read.d.ts.map +1 -1
  8. package/dist/aria-connector/src/connectors/must-read.js +4 -0
  9. package/dist/aria-connector/src/connectors/must-read.js.map +1 -1
  10. package/dist/aria-connector/src/connectors/opencode.js +25 -9
  11. package/dist/aria-connector/src/connectors/opencode.js.map +1 -1
  12. package/dist/assets/hooks/aria-agent-handoff.mjs +23 -0
  13. package/dist/assets/hooks/aria-cognition-substrate-binding.mjs +69 -3
  14. package/dist/assets/hooks/aria-pre-emit-dryrun.mjs +35 -0
  15. package/dist/assets/hooks/aria-pre-tool-gate.mjs +198 -17
  16. package/dist/assets/hooks/aria-preprompt-consult.mjs +28 -2
  17. package/dist/assets/hooks/aria-preturn-memory-gate.mjs +30 -2
  18. package/dist/assets/hooks/aria-repo-doctrine-gate.mjs +31 -1
  19. package/dist/assets/hooks/aria-stop-gate.mjs +127 -35
  20. package/dist/assets/hooks/doctrine_trigger_map.json +55 -0
  21. package/dist/assets/opencode-plugins/harness-gate/index.js +17 -4
  22. package/dist/assets/opencode-plugins/harness-stop/index.js +40 -6
  23. package/dist/runtime/discipline/CLAUDE.md +16 -0
  24. package/dist/runtime/discipline/doctrine_trigger_map.json +55 -0
  25. package/dist/runtime/doctrine_trigger_map.json +55 -0
  26. package/dist/runtime/harness-daemon.mjs +30 -3
  27. package/dist/runtime/manifest.json +1 -1
  28. package/dist/runtime/sdk/BUNDLED.json +1 -1
  29. package/dist/runtime/sdk/index.d.ts +5 -0
  30. package/dist/runtime/sdk/index.js.map +1 -1
  31. package/dist/runtime/service.mjs +46 -1
  32. package/dist/sdk/BUNDLED.json +1 -1
  33. package/dist/sdk/index.d.ts +5 -0
  34. package/dist/sdk/index.js.map +1 -1
  35. package/hooks/aria-agent-handoff.mjs +23 -0
  36. package/hooks/aria-cognition-substrate-binding.mjs +69 -3
  37. package/hooks/aria-pre-emit-dryrun.mjs +35 -0
  38. package/hooks/aria-pre-tool-gate.mjs +198 -17
  39. package/hooks/aria-preprompt-consult.mjs +28 -2
  40. package/hooks/aria-preturn-memory-gate.mjs +30 -2
  41. package/hooks/aria-repo-doctrine-gate.mjs +31 -1
  42. package/hooks/aria-stop-gate.mjs +127 -35
  43. package/hooks/doctrine_trigger_map.json +55 -0
  44. package/opencode-plugins/harness-gate/index.js +17 -4
  45. package/opencode-plugins/harness-stop/index.js +40 -6
  46. package/package.json +1 -1
  47. package/runtime-src/harness-daemon.mjs +30 -3
  48. package/runtime-src/service.mjs +46 -1
  49. package/src/connectors/claude-code.ts +31 -3
  50. package/src/connectors/codex.ts +25 -9
  51. package/src/connectors/must-read.ts +4 -0
  52. package/src/connectors/opencode.ts +25 -9
@@ -72,12 +72,60 @@ const LENS_NAMES = ALL_LENS_NAMES;
72
72
  const ANCHOR_RX = /\b(axiom|frame|memory|doctrine|packet|language):[a-z0-9_\-./]+/gi;
73
73
 
74
74
  const COGNITION_BLOCK_RX = /<cognition>([\s\S]*?)<\/cognition>/i;
75
+ const APPLIED_COGNITION_BLOCK_RX = /<applied_cognition>([\s\S]*?)<\/applied_cognition>/i;
75
76
  const FIRST_PRINCIPLE_RX = /\b(first[_\s-]?principle[s]?|first_principle=)\b/i;
76
77
  const HARNESS_PACKET_PATH = `${HOME}/.claude/.aria-harness-last-packet.json`;
77
78
  const MEMORY_DIR = `${HOME}/.claude/projects/-home-hamzaibrahim1/memory`;
78
79
  const RECENT_VIOLATIONS_PATH = `${HOME}/.claude/.aria-recent-violations.jsonl`;
79
80
  const THRASHING_STATE_PATH = `${HOME}/.claude/.aria-thrashing-state.json`;
80
81
  const CLEAR_VIOLATIONS_SCRIPT = `${HOME}/.claude/aria-clear-violations.sh`;
82
+
83
+ function buildForceReauthorReason({ source, reason, violations = [] }) {
84
+ return [
85
+ '=== ARIA FORCE_REAUTHOR ===',
86
+ `source: ${source}`,
87
+ `reason: ${reason}`,
88
+ '',
89
+ 'This is not a terminal error. It is a forced redo instruction for cognition substrate binding.',
90
+ 'Do not emit the blocked text. Re-author the cognition so every lens is tied to loaded substrate.',
91
+ '',
92
+ 'TEACHING:',
93
+ '- Cognition without live anchors is theater; anchors are the proof of thought.',
94
+ '- A valid redo cites only substrate that is actually loaded this turn.',
95
+ '- The cognition block must reference first_principle and must not cite inactive language state.',
96
+ violations.length ? `\nVIOLATIONS TO FIX:\n${violations.map((v) => `- ${v}`).join('\n')}` : '',
97
+ '',
98
+ 'REQUIRED REDO SHAPE:',
99
+ '1. Re-emit <cognition> with all required lenses.',
100
+ '2. Each lens cites >=1 loaded anchor: axiom:, frame:, memory:, doctrine:, packet:, or active language:.',
101
+ '3. Include first_principle explicitly.',
102
+ '4. Add <applied_cognition> with decision_delta, dominant_domain, binds_to, expected_predicate, and artifact_change.',
103
+ '5. Remove repeated blocked substrings instead of reusing them.',
104
+ '=== END FORCE_REAUTHOR ===',
105
+ ].filter(Boolean).join('\n');
106
+ }
107
+
108
+ function validateAppliedCognitionContract(text) {
109
+ const match = String(text || '').match(APPLIED_COGNITION_BLOCK_RX);
110
+ if (!match) {
111
+ return { ok: false, violations: ['missing <applied_cognition> contract'] };
112
+ }
113
+ const body = match[1] || '';
114
+ const required = [
115
+ ['decision_delta', /\bdecision[_ -]?delta\s*:/i],
116
+ ['dominant_domain', /\bdominant[_ -]?domain\s*:/i],
117
+ ['binds_to', /\bbinds[_ -]?to\s*:/i],
118
+ ['expected_predicate', /\bexpected[_ -]?predicate\s*:/i],
119
+ ['artifact_change', /\bartifact[_ -]?change\s*:/i],
120
+ ];
121
+ const violations = [];
122
+ for (const [name, rx] of required) {
123
+ if (!rx.test(body)) violations.push(`missing ${name}`);
124
+ }
125
+ const weakDelta = /decision[_ -]?delta\s*:\s*(?:none|n\/a|no change|unchanged|same)/i.test(body);
126
+ if (weakDelta) violations.push('decision_delta says cognition changed nothing');
127
+ return { ok: violations.length === 0, violations };
128
+ }
81
129
  const CARRY_FORWARD_WINDOW_MS = 25 * 60 * 1000;
82
130
  const CARRY_FORWARD_MAX_ROWS = 200;
83
131
  const SYSTEM_REMINDER_RX = /<system-reminder>[\s\S]*?<\/system-reminder>|<task-notification>[\s\S]*?<\/task-notification>|🔐 Aria Harness|task-notification|PreToolUse:[A-Z][A-Za-z]* hook blocking error|Stop hook blocking error/g;
@@ -422,7 +470,7 @@ Recovery surfaces:
422
470
 
423
471
  Per feedback_block_and_force_with_recovery.md, repetition of a recently-blocked phrase is not advisory drift; it is a hard block until the phrase is removed or the carry-forward state is intentionally cleared.`;
424
472
 
425
- console.log(JSON.stringify({ decision: 'block', reason }));
473
+ console.log(JSON.stringify({ decision: 'block', reason: buildForceReauthorReason({ source: 'substrate-binding/carry-forward', reason, violations: [`repeated blocked substring: ${carryForwardMatch.substring}`] }) }));
426
474
  process.exit(2);
427
475
  }
428
476
 
@@ -457,11 +505,29 @@ Re-emit with:
457
505
 
458
506
  No process-level disable path per Hamza directive 2026-04-27.`;
459
507
  audit('block_no_cognition_block', { length: assistantText.length });
460
- console.log(JSON.stringify({ decision: 'block', reason: noCogReason }));
508
+ console.log(JSON.stringify({ decision: 'block', reason: buildForceReauthorReason({ source: 'substrate-binding/no-cognition-block', reason: noCogReason, violations: ['missing <cognition>...</cognition> block'] }) }));
461
509
  process.exit(2);
462
510
  }
463
511
 
464
512
  const cognitionInner = cogMatch[1];
513
+ const appliedContract = validateAppliedCognitionContract(assistantText);
514
+ if (!appliedContract.ok) {
515
+ const reason = `Aria substrate-binding gate: cognition was emitted without an applied-cognition execution contract.
516
+
517
+ The harness no longer accepts cognition as prose-only compliance. The response must state what cognition changed and what concrete action/output it binds.
518
+
519
+ Required block:
520
+ <applied_cognition>
521
+ decision_delta: <what changed because cognition ran; not "none">
522
+ dominant_domain: <engineering_quality | trust | product | operations | security | ...>
523
+ binds_to: <next tool/action/output claim this cognition constrains>
524
+ expected_predicate: <numeric, boolean, or state-string predicate proving success>
525
+ artifact_change: <how the produced artifact differs because cognition ran>
526
+ </applied_cognition>`;
527
+ audit('block_applied_cognition_missing', { violations: appliedContract.violations, length: assistantText.length });
528
+ console.log(JSON.stringify({ decision: 'block', reason: buildForceReauthorReason({ source: 'substrate-binding/applied-cognition-contract', reason, violations: appliedContract.violations }) }));
529
+ process.exit(2);
530
+ }
465
531
  const lensTexts = extractLensTexts(cognitionInner);
466
532
 
467
533
  // Substance check — replaces char-count with "lens body has substantive
@@ -691,5 +757,5 @@ Anchor grammar (each anchor must resolve to a real loaded substrate item):
691
757
 
692
758
  Re-emit cognition with: every lens citing ≥1 verifiable loaded anchor, the block referencing first_principle, and language: citations only for active language tiers. No process-level disable path; gates are unconditional from the gated process per Hamza directive 2026-04-27.`;
693
759
 
694
- console.log(JSON.stringify({ decision: 'block', reason }));
760
+ console.log(JSON.stringify({ decision: 'block', reason: buildForceReauthorReason({ source: 'substrate-binding/structural-violations', reason, violations: reasonParts }) }));
695
761
  process.exit(2);
@@ -53,7 +53,9 @@ const REQUIRED_LENSES = 8;
53
53
  const SUBSTANCE_MIN_AFTER_ANCHOR_STRIP = 40;
54
54
  const ANCHOR_RX = /\b(axiom|frame|memory|doctrine|packet|language):[a-z0-9_\-./]+/gi;
55
55
  const COGNITION_BLOCK_RX = /<cognition>([\s\S]*?)<\/cognition>/i;
56
+ const APPLIED_COGNITION_BLOCK_RX = /<applied_cognition>([\s\S]*?)<\/applied_cognition>/i;
56
57
  const FIRST_PRINCIPLE_RX = /\b(first[_\s-]?principle[s]?|first_principle=)\b/i;
58
+ const DECISION_SIGNAL_RX = /(?:should|recommend|propose|suggest|let'?s|go with|i'd|i would|here'?s the plan|i'll|next step|action item|ship it|yes do|let me)/i;
57
59
 
58
60
  // Drift trigger patterns (mirror aria-stop-gate.mjs canonical list).
59
61
  // Keep this conservative — false negatives are tolerable (real gate will
@@ -138,6 +140,27 @@ function verifyAnchor(anchor, substrate) {
138
140
  return { ok: true, reason: '' };
139
141
  }
140
142
 
143
+ function validateAppliedCognitionContract(text) {
144
+ const match = String(text || '').match(APPLIED_COGNITION_BLOCK_RX);
145
+ if (!match) return { ok: false, violations: ['missing <applied_cognition> contract'] };
146
+ const body = match[1] || '';
147
+ const required = [
148
+ ['decision_delta', /\bdecision[_ -]?delta\s*:/i],
149
+ ['dominant_domain', /\bdominant[_ -]?domain\s*:/i],
150
+ ['binds_to', /\bbinds[_ -]?to\s*:/i],
151
+ ['expected_predicate', /\bexpected[_ -]?predicate\s*:/i],
152
+ ['artifact_change', /\bartifact[_ -]?change\s*:/i],
153
+ ];
154
+ const violations = [];
155
+ for (const [name, rx] of required) {
156
+ if (!rx.test(body)) violations.push(`missing ${name}`);
157
+ }
158
+ if (/decision[_ -]?delta\s*:\s*(?:none|n\/a|no change|unchanged|same)/i.test(body)) {
159
+ violations.push('decision_delta says cognition changed nothing');
160
+ }
161
+ return { ok: violations.length === 0, violations };
162
+ }
163
+
141
164
  // ── Read draft from stdin ─────────────────────────────────────────────
142
165
  let stdin = '';
143
166
  try {
@@ -165,6 +188,7 @@ if (draft.length < 10) {
165
188
 
166
189
  const substrate = loadSubstrate();
167
190
  const failures = [];
191
+ const requiresAppliedCognition = draft.length >= 300 || DECISION_SIGNAL_RX.test(draft) || ['deploy', 'edit', 'bash', 'text'].includes(actionType);
168
192
 
169
193
  // ── Cognition block presence + lens count + substance ─────────────────
170
194
  const cogMatch = draft.match(COGNITION_BLOCK_RX);
@@ -225,6 +249,17 @@ if (cogMatch) {
225
249
  }
226
250
  }
227
251
 
252
+ if (requiresAppliedCognition) {
253
+ const applied = validateAppliedCognitionContract(draft);
254
+ if (!applied.ok) {
255
+ failures.push({
256
+ kind: 'applied_cognition_contract_invalid',
257
+ violations: applied.violations,
258
+ message: 'Non-trivial drafts must bind cognition to the actual action/output.',
259
+ });
260
+ }
261
+ }
262
+
228
263
  // ── Drift triggers ────────────────────────────────────────────────────
229
264
  for (const trigger of DRIFT_TRIGGERS) {
230
265
  const match = draft.match(trigger.rx);
@@ -332,12 +332,29 @@ function actionMatchesPattern(action, pattern, target) {
332
332
  }
333
333
  }
334
334
 
335
+ function isOperationalMaintenanceCommand(command) {
336
+ const cmd = String(command || '').trim().replace(/\s+/g, ' ');
337
+ if (!cmd || /[;&|`$()]/.test(cmd)) return false;
338
+
339
+ // A rollout restart requeues existing pod templates; it does not change images,
340
+ // manifests, or cluster policy. Keep this narrow so deploy/apply/delete still gate.
341
+ const kubectlGlobalFlag = String.raw`(?:--(?:namespace|context|kubeconfig|as|as-group|cluster|user|server|request-timeout|field-manager)(?:=|\s+)\S+|-[A-Za-z](?:\s+\S+)?)`;
342
+ const kubectlTailFlag = String.raw`(?:--(?:namespace|context|request-timeout|field-manager|selector)(?:=|\s+)\S+|-[A-Za-z](?:\s+\S+)?)`;
343
+ const workloadTarget = String.raw`(?:deploy(?:ment)?|statefulset|sts|daemonset|ds)(?:\/[^\s]+|\s+[^\s-][^\s]*)`;
344
+ const rolloutRestartRx = new RegExp(
345
+ String.raw`^kubectl(?:\s+${kubectlGlobalFlag})*\s+rollout\s+restart\s+${workloadTarget}(?:\s+${kubectlTailFlag})*$`,
346
+ 'i',
347
+ );
348
+ return rolloutRestartRx.test(cmd);
349
+ }
350
+
335
351
  function classifyToolForBinding(toolName, command, filePath) {
336
352
  // Map Claude Code tool + payload to a binding-vocabulary action.
337
353
  if (toolName === 'Read' || toolName === 'Glob' || toolName === 'Grep') return { action: 'read', target: filePath || '' };
338
354
  if (toolName === 'Edit' || toolName === 'Write' || toolName === 'NotebookEdit') return { action: 'edit', target: filePath || '' };
339
355
  if (toolName === 'Bash') {
340
356
  const cmd = String(command || '');
357
+ if (isOperationalMaintenanceCommand(cmd)) return { action: 'kubectl_maintenance', target: cmd.slice(0, 80) };
341
358
  if (/^kubectl\s+(get|describe|logs|exec\s+\S+\s+--\s+printenv|top|version|api-resources)\b/.test(cmd)) return { action: 'kubectl_get', target: cmd.slice(0, 80) };
342
359
  if (/^kubectl\s+(apply|delete|set|patch|rollout|scale|drain|cordon)\b/.test(cmd)) return { action: 'kubectl_apply', target: cmd.slice(0, 80) };
343
360
  if (/curl\s+.*\/api\/harness\/(delegate|codex|army|plan)/.test(cmd)) return { action: 'consult', target: 'harness' };
@@ -511,6 +528,7 @@ function docRef(canonicalFilename, genericDescription) {
511
528
  // colon, not a placeholder template). The substance check (added
512
529
  // 2026-04-26) defeats ritual emission — `nur: ok` no longer counts.
513
530
  const COGNITION_BLOCK_RX = /<cognition>([\s\S]*?)<\/cognition>/i;
531
+ const APPLIED_COGNITION_BLOCK_RX = /<applied_cognition>([\s\S]*?)<\/applied_cognition>/i;
514
532
  // Hamza directive 2026-04-28: 8-lens enforcement, not 4-of-8. The earlier
515
533
  // REQUIRED_LENSES=4 was a regression — owner caught it ("i no longer see
516
534
  // 8 lens cognition per turn"). All 8 canonical lenses must appear with
@@ -554,7 +572,7 @@ const SHORT_BASH_LIMIT = 30;
554
572
  // (b) Single-line: `# cognition: <label>=<text>; <label>=<text>; ...`
555
573
  //
556
574
  // Substance check applies (≥SUBSTANCE_MIN_CHARS, not a `<placeholder>`).
557
- // 4+ substantive lenses inline → gate passes, no transcript scan
575
+ // All required substantive lenses inline → gate passes, no transcript scan
558
576
  // needed. Cognition becomes a property of THE ACTION, not of some
559
577
  // message somewhere — the deepest reading of "cognition before action."
560
578
  //
@@ -610,6 +628,32 @@ function detectInlineCognition(cmd) {
610
628
  return { count: names.length, names, hasSubstrateCite };
611
629
  }
612
630
 
631
+ function validateAppliedCognitionContract(text, cmdText = '') {
632
+ const bodyText = `${String(text || '')}\n${String(cmdText || '')}`;
633
+ const block = bodyText.match(APPLIED_COGNITION_BLOCK_RX);
634
+ const inlineBody = String(cmdText || '')
635
+ .split('\n')
636
+ .filter((line) => /^\s*#\s*(?:decision[_ -]?delta|dominant[_ -]?domain|binds[_ -]?to|expected[_ -]?predicate|artifact[_ -]?change)\s*:/i.test(line))
637
+ .join('\n');
638
+ const contractBody = block ? block[1] : inlineBody;
639
+ if (!contractBody) return { ok: false, violations: ['missing <applied_cognition> contract or inline applied-cognition comments'] };
640
+ const required = [
641
+ ['decision_delta', /\bdecision[_ -]?delta\s*:/i],
642
+ ['dominant_domain', /\bdominant[_ -]?domain\s*:/i],
643
+ ['binds_to', /\bbinds[_ -]?to\s*:/i],
644
+ ['expected_predicate', /\bexpected[_ -]?predicate\s*:/i],
645
+ ['artifact_change', /\bartifact[_ -]?change\s*:/i],
646
+ ];
647
+ const violations = [];
648
+ for (const [name, rx] of required) {
649
+ if (!rx.test(contractBody)) violations.push(`missing ${name}`);
650
+ }
651
+ if (/decision[_ -]?delta\s*:\s*(?:none|n\/a|no change|unchanged|same)/i.test(contractBody)) {
652
+ violations.push('decision_delta says cognition changed nothing');
653
+ }
654
+ return { ok: violations.length === 0, violations, body: contractBody.trim() };
655
+ }
656
+
613
657
  function normalizeInlineDirectiveBody(rawBody) {
614
658
  if (!rawBody) return '';
615
659
  const segments = String(rawBody)
@@ -671,6 +715,65 @@ function extractCurrentTurnAssistantText(event, toolInput) {
671
715
  return parts.join('\n\n').trim();
672
716
  }
673
717
 
718
+ function extractTextForUserCorrection(content) {
719
+ if (Array.isArray(content)) {
720
+ return content.filter((block) => block && block.type === 'text').map((block) => block.text || '').filter(Boolean).join('\n');
721
+ }
722
+ return typeof content === 'string' ? content : '';
723
+ }
724
+
725
+ function isRuntimeInjectedUserText(text) {
726
+ return /^\s*\[tool_result\b/i.test(text) ||
727
+ /(?:PreToolUse:[A-Za-z]+ hook blocking error|Stop hook feedback:|Aria pre-tool gate:|Aria stop-gate:|<system-reminder>|<task-notification>|task-notification)/i.test(text);
728
+ }
729
+
730
+ function collectRecentRealUserText(event, transcriptPath, limit = 6) {
731
+ const texts = [];
732
+ const seen = new Set();
733
+ const pushText = (text) => {
734
+ const normalized = String(text || '').trim();
735
+ if (!normalized || seen.has(normalized) || isRuntimeInjectedUserText(normalized)) return;
736
+ seen.add(normalized);
737
+ texts.push(normalized);
738
+ };
739
+ const messages = Array.isArray(event?.messages) ? event.messages : [];
740
+ for (let i = messages.length - 1; i >= 0 && texts.length < limit; i--) {
741
+ const role = messages[i]?.message?.role ?? messages[i]?.role ?? messages[i]?.type;
742
+ if (role !== 'user') continue;
743
+ const content = messages[i]?.message?.content ?? messages[i]?.content ?? [];
744
+ if (Array.isArray(content) && content.length > 0 && content.every((block) => block && block.type === 'tool_result')) continue;
745
+ pushText(extractTextForUserCorrection(content));
746
+ }
747
+ if (transcriptPath && existsSync(transcriptPath) && texts.length < limit) {
748
+ try {
749
+ const lines = readFileSync(transcriptPath, 'utf-8').split('\n').filter(Boolean);
750
+ for (let i = lines.length - 1; i >= 0 && texts.length < limit; i--) {
751
+ let entry;
752
+ try { entry = JSON.parse(lines[i]); } catch { continue; }
753
+ const role = entry?.message?.role ?? entry?.role ?? entry?.type;
754
+ if (role !== 'user') continue;
755
+ const content = entry?.message?.content ?? entry?.content ?? [];
756
+ if (Array.isArray(content) && content.length > 0 && content.every((block) => block && block.type === 'tool_result')) continue;
757
+ pushText(extractTextForUserCorrection(content));
758
+ }
759
+ } catch {}
760
+ }
761
+ return texts.join('\n\n');
762
+ }
763
+
764
+ function userCorrectionBlocksCommand(userText, command) {
765
+ const text = String(userText || '');
766
+ const cmd = String(command || '');
767
+ if (!text || !cmd) return null;
768
+ const k8sMutation = /\bkubectl\s+(?:apply|delete|set\s+image|patch|replace|create|scale|rollout\s+(?:restart|undo))\b|scripts\/deploy-|\bdocker\s+push\b/i.test(cmd);
769
+ const explicitStop = /\b(?:stop|do\s+not|don't|quit|cease)\b[\s\S]{0,160}\b(?:deploy|redeploy|restart|rollout|kubectl|command|pods?)\b/i.test(text);
770
+ if (explicitStop && k8sMutation) return 'recent user explicitly told the agent to stop deploy/restart command attempts';
771
+ const macTargetInCommand = /\bmlx-mac\b|\bdeployment\/mlx-mac|\bdeploy(?:ment)?\s+mlx-mac/i.test(cmd);
772
+ const macContradiction = /\b(?:mac\s+lanes?|mac\s+pods?|mlx-mac)\b[\s\S]{0,140}\b(?:not\s+pods?|no\s+such\s+thing|non[-\s]?existent|do(?:es)?\s+not\s+exist|don't\s+exist)\b|\b(?:no\s+such\s+thing|non[-\s]?existent|do(?:es)?\s+not\s+exist|don't\s+exist)\b[\s\S]{0,140}\b(?:mac\s+lanes?|mac\s+pods?|mlx-mac|pods?)\b/i.test(text);
773
+ if (macTargetInCommand && macContradiction) return 'recent user contradicted the mlx-mac Kubernetes-pod/deployment assumption';
774
+ return null;
775
+ }
776
+
674
777
  // Substance-checking lens detection (added 2026-04-26 per Hamza's
675
778
  // gate-improvement doctrine: form-only emission must not satisfy
676
779
  // the gate). For each lens, look for `<lens>: <content>` and verify
@@ -927,6 +1030,7 @@ try {
927
1030
 
928
1031
  const toolName = event.tool_name ?? event.toolName ?? '';
929
1032
  const toolInput = event.tool_input ?? event.toolInput ?? {};
1033
+ const transcriptPath = event.transcript_path ?? event.transcriptPath;
930
1034
 
931
1035
  // Gate every action tool — every tool that mutates state must go through
932
1036
  // cognition challenge. Hamza 2026-04-26: "regardless if u arent even
@@ -949,6 +1053,26 @@ const cmdPreview = toolName === 'Bash'
949
1053
  ? cmd.slice(0, 80).replace(/\s+/g, ' ')
950
1054
  : `${toolName} ${filePath || '(no path)'}`.slice(0, 80);
951
1055
 
1056
+ if (toolName === 'Bash') {
1057
+ const recentUserText = collectRecentRealUserText(event, transcriptPath);
1058
+ const correctionBlockReason = userCorrectionBlocksCommand(recentUserText, cmd);
1059
+ if (correctionBlockReason) {
1060
+ const reason = `Aria pre-tool gate: USER-CORRECTION hard-block.
1061
+
1062
+ ${correctionBlockReason}.
1063
+
1064
+ The next assistant response must stop the command loop, quote the user's correction in one sentence, and re-evaluate the target from substrate before any further mutation. Do not retry this Bash command shape.`;
1065
+ audit('block-user-correction-override', cmdPreview);
1066
+ emitBlock(reason, { source: 'pre-tool/user-correction' });
1067
+ process.exit(2);
1068
+ }
1069
+ }
1070
+
1071
+ if (toolName === 'Bash' && isOperationalMaintenanceCommand(cmd)) {
1072
+ audit('allow-operational-maintenance kubectl-rollout-restart', cmdPreview);
1073
+ process.exit(0);
1074
+ }
1075
+
952
1076
  // V3: per-command bypass removed entirely. The only escape valves are:
953
1077
  // (1) Trivial-bash whitelist — short read-only commands pass without cognition
954
1078
  // (2) ~/.claude/settings.json hook removal — visible user action Hamza controls
@@ -1032,7 +1156,6 @@ const HARD_LOOKBACK_CAP = 50;
1032
1156
  // window since the substance check defends quality regardless.
1033
1157
  const USER_BOUNDARIES_TO_CROSS = 5;
1034
1158
 
1035
- const transcriptPath = event.transcript_path ?? event.transcriptPath;
1036
1159
  const recentAssistantTexts = [];
1037
1160
  const recentAssistantTextSet = new Set();
1038
1161
  function appendAssistantTexts(texts) {
@@ -1129,6 +1252,7 @@ const bestVerifyBody = (() => {
1129
1252
  })();
1130
1253
  const hasVerify = scoreVerifyFields(bestVerifyBody, VERIFY_REQUIRED_FIELDS) === VERIFY_REQUIRED_FIELDS.length;
1131
1254
  const hasCognition = lensCount >= REQUIRED_LENSES;
1255
+ const appliedContract = validateAppliedCognitionContract(unionText, cmd);
1132
1256
  const cognitionSource = inlineCog.count >= REQUIRED_LENSES
1133
1257
  ? 'inline-command'
1134
1258
  : eventCog.count >= REQUIRED_LENSES
@@ -1181,6 +1305,7 @@ function pushDecision(decision, reasonText) {
1181
1305
  hasVerify,
1182
1306
  hasCognition,
1183
1307
  hasSubstrateCite,
1308
+ appliedCognitionContractOk: appliedContract.ok,
1184
1309
  },
1185
1310
  });
1186
1311
  }
@@ -1204,6 +1329,62 @@ Next response must do this in order:
1204
1329
  4. If the blocker is stale gate state or ledger residue, say that explicitly instead of inventing fake proof.`;
1205
1330
  }
1206
1331
 
1332
+ function buildForceRedoActionReason(reasonText, { source = 'pre-tool-gate', tool = toolName || 'unknown', lensCount = 0, requiredLenses = REQUIRED_LENSES } = {}) {
1333
+ if (String(reasonText || '').includes('ARIA FORCE_REDO_ACTION')) return reasonText;
1334
+ return [
1335
+ '=== ARIA FORCE_REDO_ACTION ===',
1336
+ `source: ${source}`,
1337
+ `tool: ${tool}`,
1338
+ '',
1339
+ 'This is not a terminal error. It is a forced redo instruction before tool execution.',
1340
+ 'Do not retry the same tool call unchanged. Re-author the action with cognition, proof, and expected outcome first.',
1341
+ '',
1342
+ 'TEACHING:',
1343
+ '- Pre-tool gates exist to make the model think before acting, not to merely throw after a bad action shape.',
1344
+ '- The redo must name the failed mechanism, cite the current substrate, and state why the next action is safe.',
1345
+ '- Do not bypass by disabling tools, changing endpoints from memory, shortening runtime, or asking Hamza to solve saved-memory ambiguity.',
1346
+ '',
1347
+ 'ORIGINAL GATE REASON:',
1348
+ String(reasonText || '').trim(),
1349
+ '',
1350
+ 'REQUIRED REDO SHAPE:',
1351
+ '1. Emit/attach <cognition> with the required lenses before retrying the tool.',
1352
+ '2. For Bash, prepend inline cognition comments to the command:',
1353
+ ' # nur: <observed state and substrate, >=20 chars>',
1354
+ ' # mizan: <risk/tradeoff and why this action is balanced, >=20 chars>',
1355
+ ' # hikma: <doctrine applied and what it forbids, >=20 chars>',
1356
+ ' # tafakkur: <second-order effects and expected result, >=20 chars>',
1357
+ `3. Current cognition count: ${lensCount}/${requiredLenses}.`,
1358
+ '4. If deploy/shared-infra: include <verify> with target, verified evidence, rollback, and axiom.',
1359
+ '5. If non-trivial action: include <expected> with numeric/boolean/state-string predicate.',
1360
+ '=== END FORCE_REDO_ACTION ===',
1361
+ ].join('\n');
1362
+ }
1363
+
1364
+ function emitBlock(reasonText, meta = {}) {
1365
+ console.log(JSON.stringify({ decision: 'block', reason: buildForceRedoActionReason(reasonText, meta) }));
1366
+ }
1367
+
1368
+ if (hasCognition && !appliedContract.ok) {
1369
+ const reason = `Aria pre-tool gate: cognition exists but is not applied to the action.
1370
+
1371
+ The harness no longer accepts cognition as decorative preface. Before a mutating tool call, cognition must produce an execution contract that changes or constrains the action.
1372
+
1373
+ Violations:
1374
+ ${appliedContract.violations.map((v) => `- ${v}`).join('\n')}
1375
+
1376
+ Required contract, either in the assistant turn as <applied_cognition>...</applied_cognition> or inline Bash comments:
1377
+ decision_delta: <what changed because cognition ran; not "none">
1378
+ dominant_domain: <engineering_quality | trust | operations | security | product | ...>
1379
+ binds_to: <this exact tool call/action>
1380
+ expected_predicate: <numeric, boolean, or state-string predicate proving success>
1381
+ artifact_change: <how the artifact/action is different because cognition ran>`;
1382
+ audit('block-applied-cognition-contract', cmdPreview);
1383
+ pushDecision('block', `applied cognition contract missing: ${appliedContract.violations.join(', ')}`);
1384
+ emitBlock(reason, { source: 'pre-tool/applied-cognition-contract', tool: toolName, lensCount, requiredLenses: REQUIRED_LENSES });
1385
+ process.exit(2);
1386
+ }
1387
+
1207
1388
  // ── Deploy-specific gate (doctrine #104) ────────────────────────────────
1208
1389
  // Per feedback_deploy_requires_verify_cognition.md (Hamza 2026-04-28 after
1209
1390
  // consciousness.ts crash took aria-soul into CrashLoopBackOff): deploys
@@ -1355,7 +1536,7 @@ Re-emit verify+cognition with the missing pieces, then retry the deploy command.
1355
1536
  cmdPreview,
1356
1537
  );
1357
1538
  pushDecision('block', `deploy ${deployMatched.name}: ${reasons.join('; ')}`);
1358
- console.log(JSON.stringify({ decision: 'block', reason: refusal }));
1539
+ emitBlock(refusal, { source: 'pre-tool/deploy', tool: toolName });
1359
1540
  process.exit(2);
1360
1541
  }
1361
1542
 
@@ -1399,7 +1580,7 @@ At least 4 substantive lenses required.`),
1399
1580
  `destructive:${matched.name}:${hasVerify ? 'need-cognition' : 'need-verify'}:${toolName}:${filePath || cmdPreview}`);
1400
1581
  audit(`block ${matched.name} verify=${hasVerify} cognition=${lensCount}`, cmdPreview);
1401
1582
  pushDecision('block', `destructive ${matched.name} missing ${!hasVerify ? 'verify' : 'cognition'}`);
1402
- console.log(JSON.stringify({ decision: 'block', reason }));
1583
+ emitBlock(reason, { source: 'pre-tool/destructive' });
1403
1584
  process.exit(2);
1404
1585
  }
1405
1586
 
@@ -1448,7 +1629,7 @@ No per-tool bypass available (v3 doctrine — the harness's whole purpose is no
1448
1629
 
1449
1630
  audit(`block ${toolName.toLowerCase()} cognition=${lensCount}`, cmdPreview);
1450
1631
  pushDecision('block', `${toolName.toLowerCase()} missing cognition (${lensCount}/${REQUIRED_LENSES})`);
1451
- console.log(JSON.stringify({ decision: 'block', reason }));
1632
+ emitBlock(reason, { source: 'pre-tool/missing-cognition' });
1452
1633
  process.exit(2);
1453
1634
  }
1454
1635
 
@@ -1479,7 +1660,7 @@ No env-var disable path — gates are unconditional from the gated process per H
1479
1660
 
1480
1661
  audit(`block-discovery-unresolved ${toolName.toLowerCase()}`, cmdPreview);
1481
1662
  pushDecision('block', `${toolName.toLowerCase()} cognition has unresolved discovery`);
1482
- console.log(JSON.stringify({ decision: 'block', reason }));
1663
+ emitBlock(reason, { source: 'pre-tool/discovery-binding' });
1483
1664
  process.exit(2);
1484
1665
  }
1485
1666
 
@@ -1543,7 +1724,7 @@ No bypass — doctrine:dalio_expected_required is unconditional for non-trivial
1543
1724
 
1544
1725
  audit(`block-expected-missing ${toolName.toLowerCase()}`, cmdPreview);
1545
1726
  pushDecision('block', `${toolName.toLowerCase()} missing <expected> measurable predicate`);
1546
- console.log(JSON.stringify({ decision: 'block', reason }));
1727
+ emitBlock(reason, { source: 'pre-tool/discovery-binding' });
1547
1728
  process.exit(2);
1548
1729
  }
1549
1730
  }
@@ -1626,7 +1807,7 @@ Read it first (it is in your environment as ARIA_HARNESS_PACKET_PATH), then refe
1626
1807
 
1627
1808
  Cognition-theater rejected per feedback_gates_enforce_form_not_substance.md.`;
1628
1809
  audit(`block-subagent-no-packet-cite ${toolName.toLowerCase()}`, cmdPreview);
1629
- console.log(JSON.stringify({ decision: 'block', reason }));
1810
+ emitBlock(reason, { source: 'pre-tool/architecture-facts' });
1630
1811
  process.exit(2);
1631
1812
  }
1632
1813
  })();
@@ -1669,7 +1850,7 @@ Rule references:
1669
1850
  R10 no_kubectl_apply_for_image_drift — kubectl apply on aria-soul-stateful (project_forge_psi_oom_cascade.md)
1670
1851
  R11 no_console_log_secrets — console.log with TOKEN/PASSWORD/SECRET/API_KEY/JWT/BEARER`;
1671
1852
  audit(`block-arch-facts ${toolName.toLowerCase()} path=${filePath}`, `violations=${archResult.violations?.length ?? 0}`);
1672
- console.log(JSON.stringify({ decision: 'block', reason: archReason }));
1853
+ emitBlock(archReason, { source: 'pre-tool/architecture-facts' });
1673
1854
  process.exit(2);
1674
1855
  }
1675
1856
  }
@@ -1771,7 +1952,7 @@ What Claude must do:
1771
1952
  3. Wait for next user prompt — preprompt-consult will retry; if it succeeds a plan will exist on next tool call
1772
1953
 
1773
1954
  Non-trivial actions are blocked until a plan exists. Trivial reads (ls/cat/grep) bypass automatically per existing whitelist. To temporarily disable binding for emergency: ARIA_BINDING_ENABLED=false (logged).`;
1774
- console.log(JSON.stringify({ decision: 'block', reason }));
1955
+ emitBlock(reason, { source: 'pre-tool/substrate-binding' });
1775
1956
  process.exit(2);
1776
1957
  }
1777
1958
  }
@@ -1790,7 +1971,7 @@ What Claude must do:
1790
1971
  3. Don't continue executing actions beyond the issued plan boundary
1791
1972
 
1792
1973
  This prevents Claude from drifting past Aria's authorized scope.`;
1793
- console.log(JSON.stringify({ decision: 'block', reason }));
1974
+ emitBlock(reason, { source: 'pre-tool/substrate-binding' });
1794
1975
  process.exit(2);
1795
1976
  }
1796
1977
 
@@ -1827,7 +2008,7 @@ This prevents Claude from drifting past Aria's authorized scope.`;
1827
2008
  if (!freshPhaseInfo || freshPhaseInfo.abortedHere) {
1828
2009
  bindingAuditAppend({ event: 'block_aborted_phase_post_fallback_still_bad', sessionId, planId: plan.planId });
1829
2010
  const reason = `Aria binding gate: aborted phase recovery minted plan ${plan.planId} but new plan also has no usable phase. Manual intervention required.`;
1830
- console.log(JSON.stringify({ decision: 'block', reason }));
2011
+ emitBlock(reason, { source: 'pre-tool/dalio-expected' });
1831
2012
  process.exit(2);
1832
2013
  }
1833
2014
  phaseInfo = freshPhaseInfo;
@@ -1836,7 +2017,7 @@ This prevents Claude from drifting past Aria's authorized scope.`;
1836
2017
  const reason = `Aria binding gate: phase ${phaseInfo.phase.id} of plan ${plan.planId} was reported aborted AND inline architect-fallback failed (exit=${architectExit}). Plan progression halted. ${architectStderr ? 'Architect stderr: ' + architectStderr : ''}
1837
2018
 
1838
2019
  What Claude must do: emit [PLAN_BLOCKER reason="<concrete observation>" suggestedAmendment="<if any>"] for Hamza/Aria to issue a corrected plan. Do not continue executing the aborted plan.`;
1839
- console.log(JSON.stringify({ decision: 'block', reason }));
2020
+ emitBlock(reason, { source: 'pre-tool/dalio-expected' });
1840
2021
  process.exit(2);
1841
2022
  }
1842
2023
  }
@@ -1864,7 +2045,7 @@ Forbidden actions for this phase: ${(phase.forbiddenActions || []).join(', ') ||
1864
2045
  Allowed actions for this phase: ${(phase.allowedActions || []).join(', ') || '(none)'}
1865
2046
 
1866
2047
  Claude must either: (a) reframe the action to fit allowedActions, OR (b) emit [PLAN_BLOCKER reason="..."] requesting Aria amend the plan.${recipeAddendum}`;
1867
- console.log(JSON.stringify({ decision: 'block', reason }));
2048
+ emitBlock(reason, { source: 'pre-tool/output-claim' });
1868
2049
  process.exit(2);
1869
2050
  }
1870
2051
 
@@ -1883,7 +2064,7 @@ Phase summary: ${phase.summary}
1883
2064
  Allowed actions: ${(phase.allowedActions || []).join(', ') || '(none — phase is observation-only)'}
1884
2065
 
1885
2066
  Claude must either: (a) reframe action to fit allowedActions, OR (b) emit [PLAN_BLOCKER reason="..."] for plan amendment.${recipeAddendum}`;
1886
- console.log(JSON.stringify({ decision: 'block', reason }));
2067
+ emitBlock(reason, { source: 'pre-tool/output-claim' });
1887
2068
  process.exit(2);
1888
2069
  }
1889
2070
 
@@ -2025,7 +2206,7 @@ try {
2025
2206
  The substrate's contract gate refused this action. Local doctrine gates passed (cognition lenses, binding plan, drift) but the substrate sees a downstream contract that disallows this tool call. Address the substrate's reason above before retrying.`;
2026
2207
  audit(`block-substrate-checkAction ${toolName.toLowerCase()} action=${__action} reason="${(__check.reason || '').slice(0, 80)}"`, cmdPreview);
2027
2208
  pushDecision('block', `substrate checkAction denied: ${(__check.reason || 'unspecified').slice(0, 100)}`);
2028
- console.log(JSON.stringify({ decision: 'block', reason: __reason }));
2209
+ emitBlock(__reason, { source: 'pre-tool/final' });
2029
2210
  process.exit(2);
2030
2211
  }
2031
2212
  }
@@ -2218,7 +2399,7 @@ causes merge conflicts and state divergence. Explicit coordination is the only s
2218
2399
  pushDecision('block', `hive session-lock conflict on ${_lockCheckPath.slice(0, 80)}`);
2219
2400
  console.log(JSON.stringify({
2220
2401
  decision: 'block',
2221
- reason: _lockBlockReason,
2402
+ reason: buildForceRedoActionReason(_lockBlockReason, { source: 'pre-tool/hive-lock-conflict', tool: toolName || 'unknown' }),
2222
2403
  hookSpecificOutput: {
2223
2404
  hookEventName: 'PreToolUse',
2224
2405
  conflicting_locks: _conflictingLocks,
@@ -109,7 +109,27 @@ const HARNESS_URL =
109
109
  process.env.ARIA_HARNESS_BASE_URL ||
110
110
  process.env.ARIA_HARNESS_URL ||
111
111
  'https://harness.ariasos.com';
112
- const HARNESS_TOKEN = process.env.ARIA_HARNESS_TOKEN || '30b18f0a302b03ae862de8a75021238d23e47464fd5d8f6a9324240933745587';
112
+ function resolveHarnessToken() {
113
+ if (process.env.ARIA_HARNESS_TOKEN) return process.env.ARIA_HARNESS_TOKEN;
114
+ if (process.env.ARIA_API_KEY) return process.env.ARIA_API_KEY;
115
+ if (process.env.ARIA_MASTER_TOKEN) return process.env.ARIA_MASTER_TOKEN;
116
+ try {
117
+ if (existsSync(OWNER_TOKEN_PATH)) {
118
+ const token = readFileSync(OWNER_TOKEN_PATH, 'utf8').trim();
119
+ if (token) return token;
120
+ }
121
+ } catch {}
122
+ try {
123
+ const licensePath = `${HOME}/.aria/license.json`;
124
+ if (existsSync(licensePath)) {
125
+ const license = JSON.parse(readFileSync(licensePath, 'utf8'));
126
+ if (license.harnessToken) return String(license.harnessToken).trim();
127
+ if (license.token) return String(license.token).trim();
128
+ }
129
+ } catch {}
130
+ return '';
131
+ }
132
+ const HARNESS_TOKEN = resolveHarnessToken();
113
133
  const MIN_PROMPT_CHARS = 40; // skip auto-consult on trivial prompts
114
134
  const MAX_DIRECTION_CHARS = 4000; // cap injected chunk size
115
135
 
@@ -248,6 +268,8 @@ Respond with EXACTLY this JSON shape:
248
268
  "successCriterion": "<observable signal of completion>",
249
269
  "abortCriterion": "<observable signal of failure>",
250
270
  "doctrineRefs": ["<memory_filename.md>", ...],
271
+ "cognitionUse": "<how Aria cognition should change the executor's tool/input/output shape for this phase>",
272
+ "evidenceRequired": ["<observable proof the executor must collect before reporting complete>"],
251
273
  "allowedActions": ["read", "edit:<path-pattern>", "kubectl_get", "consult", "bash_safe", "..."],
252
274
  "forbiddenActions": ["kubectl_apply", "edit:<path-pattern>", "..."]
253
275
  }
@@ -259,7 +281,9 @@ Respond with EXACTLY this JSON shape:
259
281
  }
260
282
  }
261
283
 
262
- Apply your 8 lenses + substrate. Phases are ordered + small (one logical step each). Doctrine refs cite memory files in /home/hamzaibrahim1/.claude/projects/-home-hamzaibrahim1/memory/. Be specific in allowedActions / forbiddenActions — Claude can ONLY do what's allowed for the current phase.` : `Pre-prompt direction request from Claude orchestrator.
284
+ Apply your 8 lenses + substrate. Phases are ordered + small (one logical step each). Doctrine refs cite memory files in /home/hamzaibrahim1/.claude/projects/-home-hamzaibrahim1/memory/. Be specific in allowedActions / forbiddenActions — Claude can ONLY do what's allowed for the current phase.
285
+
286
+ Every phase must include cognitionUse and evidenceRequired. cognitionUse tells Claude how Aria's cognition should improve the actual tool input, edit shape, review scope, or final output. evidenceRequired tells Claude what observation must be collected before reporting the phase complete.` : `Pre-prompt direction request from Claude orchestrator.
263
287
 
264
288
  The user just submitted this prompt:
265
289
 
@@ -280,6 +304,8 @@ thinking from training reflex. Be concrete:
280
304
  (NOT a reflexive "want me to" deferral).
281
305
  4. Mizan check: any risk patterns in this prompt — over-scope creep,
282
306
  over-replacement temptation, tier-substitution temptation, etc.
307
+ 5. How should Aria cognition improve Claude's actual input/output shape —
308
+ what should be more specific, safer, more evidence-bound, or differently scoped?
283
309
 
284
310
  Keep direction under 1500 chars. This is the pre-load context for Claude's
285
311
  turn — not the final response. Claude will still emit cognition + action;