@jjanczur/tyran 0.1.40 → 0.1.43

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.
@@ -84,6 +84,7 @@ import {
84
84
  } from './secrets-gate.mjs';
85
85
  import {
86
86
  MANDATORY_KERNEL_PATHS,
87
+ boundariesOf,
87
88
  classifyPath,
88
89
  globMatches,
89
90
  normalizePath,
@@ -554,6 +555,50 @@ export async function loadDeployClass(root) {
554
555
  return value;
555
556
  }
556
557
 
558
+ /**
559
+ * How far this repo has turned the gate down (`boundaries:` in config.yaml).
560
+ *
561
+ * Fail-closed at every step, which for THIS loader means something different
562
+ * from the others in this file: a missing config, an unreadable one, a
563
+ * malformed one and a typo in a value all resolve to STRICT, because every
564
+ * failure here would otherwise remove a refusal. `loadDeployClass` refuses
565
+ * outright when it cannot read the class; this one cannot, because it runs on
566
+ * every tool call including the ones in a repo that never adopted Tyran — so
567
+ * its safe answer is "every boundary on", not an error.
568
+ *
569
+ * The resolution rule itself is NOT spelled here. `boundariesOf` in
570
+ * schema.mjs is the one implementation, shared with doctor and the Settings
571
+ * tab, because three spellings of one rule is the defect ADR-21 names and this
572
+ * rule decides whether a refusal happens at all.
573
+ */
574
+ export async function loadBoundaries(root) {
575
+ // NOT memoized, deliberately. A process-lifetime cache keyed on the root
576
+ // would be correct in production — one process per tool call — and it was
577
+ // tried; what it actually bought was sub-millisecond, and what it cost was
578
+ // module state that survives between calls in every caller that is not the
579
+ // platform. The first thing it broke was a test reusing one root for two
580
+ // configs, which is precisely the shape of "the gate answered from a config
581
+ // that is no longer on disk". A second small read is the cheaper answer.
582
+ let text = null;
583
+ for (const candidate of [root, mainWorktreeOf(root)].filter((r) => r !== null)) {
584
+ try {
585
+ text = await readBounded(join(candidate, CONFIG_PATH), 'the Tyran config');
586
+ } catch {
587
+ // An unreadable or oversized config is a refusal for the callers that
588
+ // need a VALUE out of it. Here it is simply "no relaxations", which is
589
+ // the strictest answer available and never the reason a call proceeds.
590
+ return boundariesOf(null);
591
+ }
592
+ if (text !== null) break;
593
+ }
594
+ if (text === null) return boundariesOf(null);
595
+ try {
596
+ return boundariesOf(parse(text));
597
+ } catch {
598
+ return boundariesOf(null);
599
+ }
600
+ }
601
+
557
602
  /**
558
603
  * A leading `~` expanded to the home directory; everything else untouched so
559
604
  * the `*`/`**` in a glob survive (`resolve` would collapse a trailing `**`).
@@ -1067,7 +1112,9 @@ export async function decide({ input, runner = runChild, startedAt = Date.now(),
1067
1112
  // modes must keep the hard deny — see verdictForClass's matrix.
1068
1113
  const askable = actor === 'main' && field(input, 'permission_mode') === 'acceptEdits';
1069
1114
 
1070
- if (toolName === 'Bash') return await decideBash({ input, toolInput, root, runner, budget });
1115
+ const boundaries = await loadBoundaries(root);
1116
+
1117
+ if (toolName === 'Bash') return await decideBash({ input, toolInput, root, runner, budget, boundaries });
1071
1118
 
1072
1119
  const targets = pathTargets(toolInput);
1073
1120
  const isWriteTool = typeof toolName === 'string' && WRITE_TOOLS.includes(toolName);
@@ -1101,6 +1148,7 @@ export async function decide({ input, runner = runChild, startedAt = Date.now(),
1101
1148
  // exact inversion of this file's own argument that two spellings of one
1102
1149
  // location must not resolve to two classes. The cost of skipping it is
1103
1150
  // higher here than on the write path, not lower.
1151
+ if (boundaries.credentials === 'allow') continue;
1104
1152
  const resolved = canonicalDeepest(target);
1105
1153
  const hits = [...new Set([...secretReadRules(target), ...secretReadRules(resolved)])];
1106
1154
  if (hits.length === 0) continue;
@@ -1159,6 +1207,12 @@ export async function decide({ input, runner = runChild, startedAt = Date.now(),
1159
1207
  // exempts those built-in locations; `main_writable_paths` in config.yaml adds
1160
1208
  // operator-chosen ones. Both are MAIN-thread only: a subagent still falls
1161
1209
  // through to KERNEL.
1210
+ // `outside_repo: allow` is the ONE relaxation that is not actor-split.
1211
+ // The narrow exemptions below stay main-thread-only because they are
1212
+ // guesses about the harness; this one is an operator saying, in writing,
1213
+ // that agents may work outside this repository — and a subagent is the
1214
+ // actor that does the work.
1215
+ if (normalized === null && boundaries.outside_repo === 'allow') continue;
1162
1216
  if (normalized === null && actor === 'main') {
1163
1217
  if (harnessWritable(target, env)) continue;
1164
1218
  if (mainWritable === undefined) mainWritable = await loadMainWritablePaths(root);
@@ -1180,7 +1234,14 @@ export async function decide({ input, runner = runChild, startedAt = Date.now(),
1180
1234
  if (normalized !== null && decidingRule(policy, normalized) === null && !isGoverned(normalized)) {
1181
1235
  continue;
1182
1236
  }
1183
- const verdict = verdictForClass(cls, unsupervised, askable);
1237
+ // The floor is consulted BEFORE the flag, so no ordering of settings can
1238
+ // reach the files that enforce every other boundary.
1239
+ const relaxClasses =
1240
+ boundaries.path_classes === 'allow' &&
1241
+ normalized !== null &&
1242
+ boundaryFloorGlobFor(normalized) === null &&
1243
+ protectedGlobFor(normalized) === null;
1244
+ const verdict = relaxClasses ? 'pass' : verdictForClass(cls, unsupervised, askable);
1184
1245
 
1185
1246
  // ADR-21, applied as a check rather than as a refactor: the rule this
1186
1247
  // refusal names and the class the resolver returned must agree. They are
@@ -1384,6 +1445,38 @@ export const SHELL_PROTECTED_GLOBS = Object.freeze([
1384
1445
  '.claude/settings.local.json',
1385
1446
  ]);
1386
1447
 
1448
+ /**
1449
+ * Paths no `boundaries:` setting can reach, checked before `path_classes` is
1450
+ * consulted.
1451
+ *
1452
+ * `SHELL_PROTECTED_GLOBS` is already "the files that switch every gate off":
1453
+ * the enforcement hooks, the policy that classifies them, and the settings
1454
+ * file that registers them. `.tyran/STOP` joins them here for a reason of the
1455
+ * same kind rather than the same shape — it is the operator's brake, and a
1456
+ * flag the loop can use to clear its own stop signal is not a flag, it is the
1457
+ * end of the boundary.
1458
+ *
1459
+ * Note what this does NOT do: it does not change what a policy file may say.
1460
+ * A user can still classify `.claude/settings.json` however they like; what
1461
+ * they cannot do is reach it by turning `path_classes` down, which is a new
1462
+ * door and is therefore closed on the way in.
1463
+ *
1464
+ * It lives HERE, below `SHELL_PROTECTED_GLOBS` rather than beside the loader
1465
+ * that motivates it, because a `const` that reads another `const` declared
1466
+ * later in the file throws on import — which is the whole gate failing to load
1467
+ * rather than one check misbehaving.
1468
+ */
1469
+ export const BOUNDARY_FLOOR_GLOBS = Object.freeze([...SHELL_PROTECTED_GLOBS, '.tyran/STOP']);
1470
+
1471
+ /** The floor glob covering this path, or null. */
1472
+ export function boundaryFloorGlobFor(normalized) {
1473
+ if (normalized === null || normalized === undefined) return null;
1474
+ for (const glob of BOUNDARY_FLOOR_GLOBS) {
1475
+ if (globMatches(glob, normalized)) return glob;
1476
+ }
1477
+ return null;
1478
+ }
1479
+
1387
1480
  /** The shell-protected glob covering this path, or null. */
1388
1481
  export function shellProtectedGlobFor(normalized) {
1389
1482
  if (normalized === null || normalized === undefined) return null;
@@ -1756,7 +1849,7 @@ export function shellPathFindings(command, startDir, root) {
1756
1849
  }
1757
1850
 
1758
1851
  /** The refusal for a protected path named in a shell command. */
1759
- function refuseShellPaths(findings, command = '') {
1852
+ function refuseShellPaths(findings, command = '', allowCredentials = false) {
1760
1853
  const credentials = findings.filter((f) => f.kind === 'credential');
1761
1854
  const kernel = findings.filter((f) => f.kind === 'kernel');
1762
1855
  // The credential the finding list never saw, because it sat where a message
@@ -1765,7 +1858,7 @@ function refuseShellPaths(findings, command = '') {
1765
1858
  // out loud, because otherwise this refusal would offer the "rewrite it as a
1766
1859
  // read-only command" way forward for a line whose problem is the secret in it.
1767
1860
  // A refusal that states the wrong reason is worse than one that states none.
1768
- const hidden = credentials.length === 0 ? rawCredentialWords(command) : [];
1861
+ const hidden = credentials.length === 0 && allowCredentials !== true ? rawCredentialWords(command) : [];
1769
1862
  // True when the PATHS were all in the readable class, so the command itself
1770
1863
  // is the only reason this is a refusal. Worth saying: the reader is one
1771
1864
  // rewrite away from an allowed call, and a refusal with no reachable way
@@ -1831,7 +1924,7 @@ function refuseShellPaths(findings, command = '') {
1831
1924
  return { decision: 'deny', reason: lines.join('\n') };
1832
1925
  }
1833
1926
 
1834
- async function decideBash({ input, toolInput, root, runner, budget }) {
1927
+ async function decideBash({ input, toolInput, root, runner, budget, boundaries }) {
1835
1928
  const command = field(toolInput, 'command');
1836
1929
  if (typeof command !== 'string') {
1837
1930
  return {
@@ -1851,11 +1944,28 @@ async function decideBash({ input, toolInput, root, runner, budget }) {
1851
1944
  // "the model was refused a Read and reached for Bash in the next call", and a
1852
1945
  // check that only runs once a policy loads is a check an unconfigured repo
1853
1946
  // does not have.
1854
- const findings = shellPathFindings(command, startDir, root);
1947
+ // `credentials: allow` drops the CREDENTIAL findings and nothing else. A
1948
+ // kernel finding is a path that protects the gate, and no setting reaches
1949
+ // those — which is why this filters the list rather than skipping the call.
1950
+ const findings = shellPathFindings(command, startDir, root).filter(
1951
+ (f) => !(f.kind === 'credential' && boundaries.credentials === 'allow'),
1952
+ );
1855
1953
  // The exemption SKIPS this refusal; it does not return PASS. A `git log` that
1856
1954
  // names a protected path still travels the deployment class and every other
1857
1955
  // check below, exactly as `git log` on any other path does.
1858
- if (findings.length > 0 && !shellReadExempt(findings, command)) return refuseShellPaths(findings, command);
1956
+ if (findings.length > 0 && !shellReadExempt(findings, command)) {
1957
+ return refuseShellPaths(findings, command, boundaries.credentials === 'allow');
1958
+ }
1959
+
1960
+ // `push: allow` skips the whole push analysis, not just the verdict at the
1961
+ // end of it. The refusals in between — an alias, a refspec the shell would
1962
+ // rewrite, a remote whose default branch cannot be read — all exist to stop
1963
+ // the gate GUESSING a destination it is about to judge. With nothing to
1964
+ // judge there is nothing to guess, and keeping them would refuse commands
1965
+ // for being unreadable to a check that is switched off.
1966
+ //
1967
+ // The path rules above still ran. This is the deployment class only.
1968
+ if (boundaries.push === 'allow') return PASS;
1859
1969
 
1860
1970
  const plan = planCommand(command, startDir);
1861
1971
  const pushes = plan.targets.filter((t) => t.scanPush === true && Array.isArray(t.pushArgv));
@@ -2039,10 +2149,32 @@ const DEPLOY_REMEDY = Object.freeze(Object.assign(Object.create(null), {
2039
2149
  'What to do instead: push to a branch of your own and open a pull request from it.',
2040
2150
  }));
2041
2151
 
2042
- /** Turn a PolicyFailure into the refusal it always has to be. */
2152
+ /**
2153
+ * Turn a PolicyFailure into the refusal it always has to be — and, when the
2154
+ * operator asked for it, turn "no objection" into an auto-approval.
2155
+ *
2156
+ * ONE place, deliberately, rather than a branch at every `return PASS`. The
2157
+ * property that has to hold is "a deny or an ask is never rewritten", and here
2158
+ * that is a single readable line instead of a claim about a dozen return
2159
+ * sites. The catch below is unchanged: a PolicyFailure is still a refusal, and
2160
+ * `boundaries` is not consulted on that path at all — a gate that could not
2161
+ * complete its check must not auto-approve, whatever the config says.
2162
+ */
2043
2163
  export async function handle({ input, runner, startedAt, env }) {
2044
2164
  try {
2045
- return await decide({ input, runner, startedAt, env });
2165
+ const verdict = await decide({ input, runner, startedAt, env });
2166
+ const isPass = verdict === PASS || (verdict !== null && typeof verdict === 'object' && verdict.decision === 'pass');
2167
+ if (!isPass) return verdict;
2168
+ const boundaries = await loadBoundaries(repoRootOf(input, env ?? process.env));
2169
+ if (boundaries.prompts !== 'skip') return verdict;
2170
+ return {
2171
+ decision: 'allow',
2172
+ reason:
2173
+ 'tyran policy-gate: approved without asking. This repository sets `boundaries.prompts: skip` ' +
2174
+ 'in .tyran/config.yaml, so a tool call no gate objects to is auto-approved.\n' +
2175
+ 'What this does NOT reach: a refusal from any other gate still wins, so secret scanning at ' +
2176
+ 'commit and push is unaffected, and so are the paths that protect the gates themselves.',
2177
+ };
2046
2178
  } catch (err) {
2047
2179
  if (err instanceof PolicyFailure) {
2048
2180
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jjanczur/tyran",
3
- "version": "0.1.40",
3
+ "version": "0.1.43",
4
4
  "description": "Command-line scripts for the Tyran task conductor (doctor, journal, schema, and repo tooling) for CI and terminals outside Claude Code. Installing this package does NOT install the Claude Code plugin — add that separately with `/plugin marketplace add jjanczur/tyran` inside Claude Code.",
5
5
  "type": "module",
6
6
  "engines": {
@@ -9,6 +9,9 @@
9
9
  "bin": {
10
10
  "tyran": "./bin/tyran.mjs"
11
11
  },
12
+ "scripts": {
13
+ "test": "node --test \"tests/**/*.test.mjs\""
14
+ },
12
15
  "publishConfig": {
13
16
  "access": "public"
14
17
  },