aegiscode 6.8.12 → 6.8.14

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.
@@ -42,6 +42,7 @@ const { spawn } = require('node:child_process');
42
42
  const crypto = require('node:crypto');
43
43
  const fs = require('node:fs');
44
44
  const path = require('node:path');
45
+ const { pathToFileURL } = require('node:url');
45
46
  const { agentRoles } = require('./agents.js');
46
47
 
47
48
  const OUTPUT_CAP = 30_000; // chars fed back to the model per tool result
@@ -770,9 +771,121 @@ function isTool(name) {
770
771
  * all land on the error branch rather than rejecting. `ctx` carries per-turn
771
772
  * state (getShell, signal); omit it and exec falls back to a one-shot spawn.
772
773
  */
774
+ /**
775
+ * `tools/agent-gate.mjs` is ESM and this module is CJS, so it is imported
776
+ * dynamically and once. The candidates are tried in order; the first that
777
+ * resolves wins:
778
+ *
779
+ * 1. `../../../tools/agent-gate.mjs` — the repo checkout (dev / CI). This is
780
+ * the SAME file the git hooks execute, so "what is allowed" is computed
781
+ * once, by one implementation.
782
+ * 2. `<process.resourcesPath>/tools/agent-gate.mjs` — where electron-builder's
783
+ * `extraResources` puts it in a packaged build, as a real file OUTSIDE the
784
+ * asar. That placement is load-bearing, not cosmetic: the gate is loaded
785
+ * with dynamic `import()`, and Node's ESM loader does not go through
786
+ * Electron's asar-aware fs patch (electron/asar#249), so a copy living only
787
+ * inside the archive can fail to resolve — leaving the packaged app with no
788
+ * gate at all. This is the app's only dynamic ESM import, so nothing else
789
+ * here exercises that path. Conveniently this is also what candidate (1)
790
+ * resolves to from `resources/app.asar/lib/local/`, so dev and packaged
791
+ * builds agree on one file.
792
+ * 3. `../../vendor/agent-gate.mjs` — the staged copy inside the app dir, kept
793
+ * as the last resort for a build that is not archived (`asar: false`) or an
794
+ * npm-installed tree. It is the candidate that fails inside an asar, hence
795
+ * third and not first.
796
+ *
797
+ * Before (2) existed a packaged build had no gate at all: the manifest is found
798
+ * by climbing from the working directory, so the packaged app would open a
799
+ * guarded tree and every write through it succeeded while the same call through
800
+ * the repo copy was refused.
801
+ *
802
+ * If nothing resolves, that is a broken gate rather than a permissive one, and
803
+ * executeTool refuses mutating calls into a tree that declares a manifest.
804
+ */
805
+ let agentGatePromise;
806
+
807
+ function agentGateCandidates({ resourcesPath = process.resourcesPath, dirname = __dirname } = {}) {
808
+ const candidates = ['../../../tools/agent-gate.mjs'];
809
+ // process.resourcesPath is an Electron-provided value and is absent under
810
+ // plain node, so this is guarded rather than assumed. An absolute path must
811
+ // be a file URL to be imported.
812
+ if (typeof resourcesPath === 'string' && resourcesPath) {
813
+ candidates.push(pathToFileURL(path.join(resourcesPath, 'tools', 'agent-gate.mjs')).href);
814
+ }
815
+ candidates.push('../../vendor/agent-gate.mjs');
816
+ return candidates;
817
+ }
818
+
819
+ function loadAgentGate() {
820
+ if (!agentGatePromise) {
821
+ const candidates = agentGateCandidates();
822
+ const tryNext = (i) => (i >= candidates.length
823
+ ? null
824
+ : import(candidates[i]).catch(() => tryNext(i + 1)));
825
+ agentGatePromise = tryNext(0);
826
+ }
827
+ return agentGatePromise;
828
+ }
829
+
830
+ /**
831
+ * The nearest `.aegis/guard.json` at or above `start`, or null. The identical
832
+ * climb as findManifest() in tools/agent-gate.mjs and src/repoguard.js. It is
833
+ * used ONLY to decide whether a call that could not load the gate module is
834
+ * unsafe; when the module loads it is the sole authority.
835
+ */
836
+ function nearestManifest(start) {
837
+ let dir = path.resolve(start || process.cwd());
838
+ try { if (fs.statSync(dir).isFile()) dir = path.dirname(dir); } catch { /* keep as-is */ }
839
+ for (;;) {
840
+ const candidate = path.join(dir, '.aegis', 'guard.json');
841
+ if (fs.existsSync(candidate)) return candidate;
842
+ const parent = path.dirname(dir);
843
+ if (parent === dir) return null;
844
+ dir = parent;
845
+ }
846
+ }
847
+
773
848
  async function executeTool(name, args, ctx) {
774
849
  if (!isTool(name)) return fail(`unknown tool "${name}" (known: ${toolNames().join(', ')})`);
775
850
  const input = args && typeof args === 'object' ? args : {};
851
+ // ── agent-key gate (tools/agent-gate.mjs) ──────────────────────────────
852
+ // The in-process half of the lock whose other half is .githooks/. A tree
853
+ // that declares .aegis/guard.json must not be mutated through this app
854
+ // without the key, exactly as it cannot be mutated by the engine or
855
+ // committed by git. Reads are never gated. Refused — not warned — when the
856
+ // key is absent, the manifest is unreadable, or the key file is
857
+ // world-readable; AEGIS_GATE_SKIP=1 is the one bypass, and it is refused in
858
+ // an unattended run. The module is required lazily so a packaged app whose
859
+ // tree has no tools/ directory still starts and simply has no gate.
860
+ let gate = null;
861
+ try {
862
+ gate = await loadAgentGate();
863
+ } catch { /* not running from the repo: no manifest to honour */ }
864
+ if (gate) {
865
+ const verdict = gate.guardTool({
866
+ tool: name, args: input, cwd: (ctx && ctx.cwd) || process.cwd(),
867
+ env: process.env, unattended: Boolean(ctx && ctx.autonomous),
868
+ });
869
+ if (!verdict.allowed) return fail(gate.refusalText(verdict));
870
+ if (verdict.code === 'skipped') {
871
+ const res = await EXECUTORS[name](input, ctx || {});
872
+ return res && typeof res === 'object' ? { ...res, output: `⚠ ${verdict.reason}\n${res.output || ''}` } : res;
873
+ }
874
+ } else if (MUTATING_TOOLS.has(name)) {
875
+ // Neither the repo copy nor the staged one loaded. A broken gate must
876
+ // never silently allow (the hooks exit 4 for exactly this reason), so a
877
+ // mutating call into a tree that declares a manifest is refused with the
878
+ // path of the manifest that could not be honoured.
879
+ const man = nearestManifest((ctx && ctx.cwd) || process.cwd());
880
+ if (man) {
881
+ return fail(
882
+ `error: refused — ${man} declares an agent-gated tree, but the agent-key gate module could not be loaded `
883
+ + '(tried tools/agent-gate.mjs beside the app and vendor/agent-gate.mjs inside it), so this '
884
+ + `${name} cannot be authorised. A build that missed the staged copy is the usual cause: `
885
+ + 'run scripts/predist.mjs before packaging.',
886
+ );
887
+ }
888
+ }
776
889
  try {
777
890
  return await EXECUTORS[name](input, ctx || {});
778
891
  } catch (e) {