@yahyahammad/venastine 0.3.12 → 0.3.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.
@@ -81,6 +81,7 @@ from typing import Literal
81
81
  from pydantic import BaseModel, Field, StrictBool
82
82
 
83
83
  import config
84
+ from core import decision_classifier
84
85
  from security import capability, posture
85
86
  from security.capability import (
86
87
  UNAVAILABLE,
@@ -111,6 +112,7 @@ from security.sandbox import (
111
112
  is_docker_available,
112
113
  known_runtime,
113
114
  run_sandboxed,
115
+ single_command,
114
116
  ssh_enabled,
115
117
  ssh_target,
116
118
  ssh_unavailable_reason,
@@ -454,6 +456,106 @@ def _command_names_root(command) -> bool:
454
456
  return False
455
457
 
456
458
 
459
+ def classifier_key(command, backend: str, network: bool = False,
460
+ needs_root: bool = False):
461
+ """The question the `classified` mode puts to its decision model for
462
+ this call, as a cache key -- or None when it puts none (ROADMAP_v3 §56).
463
+
464
+ ONE answer for the gate, the handlers and the notice, because a
465
+ `reject` the gate decides is enforced by `run` and the session starts
466
+ reading the verdict the gate stored (JV14). Three sites working out
467
+ "was the model asked about this" separately would be #157's shape.
468
+
469
+ None unless every one holds:
470
+
471
+ * the mode is `classified`;
472
+ * nothing above the opt-ins in `_shell_approval_check` answers for
473
+ the call -- not root, not a protected segment, not SSH or WSL
474
+ (JV2). Re-checked here rather than trusted, because the handlers
475
+ call this too and reach it by another road;
476
+ * it runs in the CONTAINER and was characterised (`measured`) --
477
+ ROADMAP_v2 §28's ceiling, "certain safe must not authorise a host
478
+ read", which keeps a host read, the host fallback and WSL out
479
+ (JV12);
480
+ * it is a single command (`single_command`) -- §28's other limit,
481
+ "the model never sees compound shell" (JV12);
482
+ * `tiered` would ASK about it (JV11). What `tiered` already runs
483
+ unasked never waits on a network call.
484
+
485
+ The key is the command, the project, the backend and the declared
486
+ network: everything the answer depends on.
487
+ """
488
+ if posture.current().shell_approval_mode != capability.CLASSIFIED_MODE:
489
+ return None
490
+ return classifier_question(command, backend, network, needs_root)[0]
491
+
492
+
493
+ def classifier_question(command, backend: str, network: bool = False,
494
+ needs_root: bool = False):
495
+ """`classifier_key` without the mode, and with the reason when there is
496
+ no question: `(key, "")` or `(None, why)`. `/classifier test` reads the
497
+ reason, under any mode, to say what `classified` would do."""
498
+ if not isinstance(command, str) or not command.strip():
499
+ return None, "there is no command"
500
+ if backend != BACKEND_CONTAINER:
501
+ return None, (f"it asks for the {backend} backend, which is never "
502
+ f"sent to the decision model")
503
+ if needs_root or _command_names_root(command):
504
+ return None, "it runs as root, which always asks"
505
+ if _command_touches_protected(command) is not None:
506
+ return None, "it names a protected path, which always asks"
507
+ if not single_command(command):
508
+ return None, ("it is not a single command: it contains a shell "
509
+ "metacharacter, a quote, an escape or a control "
510
+ "character, and the decision model never sees one")
511
+ network = bool(network)
512
+ profile, containment = _profile_and_containment(command, network, backend)
513
+ if containment != capability.CONTAINED:
514
+ return None, ("it would not run in a container (no container "
515
+ "runtime answered), and only a contained command is "
516
+ "sent to the decision model")
517
+ if not profile.measured:
518
+ return None, "the harness could not characterise it"
519
+ if auto_approved(profile, containment):
520
+ return None, ("tiered already runs it without asking, so the "
521
+ "decision model is not consulted")
522
+ return (command, config.WORKSPACE_DIR, backend, network), ""
523
+
524
+
525
+ def classifier_refusal(command, backend: str, network: bool = False,
526
+ needs_root: bool = False) -> str | None:
527
+ """Why the decision model's verdict stops this call, or None
528
+ (ROADMAP_v3 §56, JV14, JV18).
529
+
530
+ The handler half of `reject`: the gate answered "nothing to ask" and
531
+ this is what keeps the call from running. A verdict that is no longer
532
+ held -- `/classifier clear` between the gate and the run -- refuses
533
+ rather than runs, `fallback_changed_refusal`'s shape, because a call
534
+ nobody judged must not run on the strength of having been judged.
535
+
536
+ It names no number and no threshold (JV15): the model reads this, and
537
+ the numbers are a gradient a hijacked agent could climb.
538
+ """
539
+ key = classifier_key(command, backend, network, needs_root)
540
+ if key is None:
541
+ return None
542
+ settings = posture.current().shell_classifier
543
+ if settings.on_fail != decision_classifier.ON_FAIL_REJECT:
544
+ return None
545
+ verdict = decision_classifier.CACHE.get(key)
546
+ if verdict is None:
547
+ return ("This command was judged by the decision model and that "
548
+ "verdict has since been cleared, so it was not run. Retry, "
549
+ "and it will be judged again.")
550
+ if verdict.refuses(settings):
551
+ return ("Refused without asking: the decision model judged this "
552
+ "command outside what may run unprompted, and the harness "
553
+ "is set to refuse such commands rather than ask "
554
+ "(shell_classifier_on_fail: reject). Do the work another "
555
+ "way, or ask the user to run it.")
556
+ return None
557
+
558
+
457
559
  def _shell_approval_check(tool_name: str, params: dict) -> bool:
458
560
  """Whether this shell call needs a human to say yes (ROADMAP_v2 §28).
459
561
 
@@ -497,6 +599,15 @@ def _shell_approval_check(tool_name: str, params: dict) -> bool:
497
599
  under `tiered` and `contained` alike (§48, CE5): it is written on
498
600
  purpose and already flagged. Under `always` it is never reached,
499
601
  because step 1 returned (CE6).
602
+ 4a. The `classified` mode (ROADMAP_v3 §56, JV11-JV14) asks its
603
+ decision model about the calls `classifier_key` admits: single,
604
+ contained, and ones `tiered` would ask about. A PASS runs
605
+ unasked; a FAIL asks, or under `on_fail: reject` answers "nothing
606
+ to ask" and `run` refuses it; no verdict asks (JV10, JV13).
607
+ Below every refusal and always-ask above, which is JV2 -- the
608
+ reason a steered verdict cannot reach `.venastine/`, root or
609
+ another machine. Every call `classifier_key` refuses falls
610
+ through to step 5, which is `tiered`.
500
611
  4. The `contained` mode (§48, CE2) is the second visible opt-in, and
501
612
  applied here for the same reason: a command the container
502
613
  confines and that gets no network runs unasked. Below the
@@ -608,6 +719,19 @@ def _shell_approval_check(tool_name: str, params: dict) -> bool:
608
719
  and active.auto_approve_fallback):
609
720
  return False
610
721
 
722
+ # Step 4a, ROADMAP_v3 §56. The only step in this function that can turn
723
+ # an "ask" into "run unasked" on a model's word -- the first layer in
724
+ # the harness that widens rather than tightens, which breaks D14's
725
+ # one-way ratchet as a NAMED decision (ROADMAP_v2 §28 required one).
726
+ if mode == capability.CLASSIFIED_MODE:
727
+ key = classifier_key(command, backend, declared_network(params),
728
+ declared_root(params))
729
+ if key is not None:
730
+ settings = active.shell_classifier
731
+ verdict = decision_classifier.verdict_for(
732
+ key, command, config.WORKSPACE_DIR, settings)
733
+ return verdict.asks(settings)
734
+
611
735
  # Step 4, §48 (CE2): the rule `tiered` had before §48, under the name
612
736
  # that says what it trusts. `measured` first, for G5's reason -- the
613
737
  # container must not argue an uncharacterised call through.
@@ -693,6 +817,16 @@ def _shell_approval_notice(params: dict, _context=None) -> str:
693
817
  # look like a false positive to the person answering.
694
818
  notice += (f" It names {segment}/, a protected path that always "
695
819
  "requires approval.")
820
+ # ROADMAP_v3 §56 (JV15). The verdict is for the person answering, and
821
+ # this prompt is the one place they read it -- the model's result never
822
+ # carries it. Read from the cache the gate filled, never fetched here.
823
+ key = classifier_key(command, backend, declared_network(params),
824
+ declared_root(params))
825
+ if key is not None:
826
+ verdict = decision_classifier.CACHE.get(key)
827
+ if verdict is not None:
828
+ notice += " " + decision_classifier.describe(
829
+ verdict, posture.current().shell_classifier)
696
830
  return notice
697
831
 
698
832
 
@@ -943,6 +1077,11 @@ def run(params: dict) -> dict:
943
1077
  parsed.needs_root)
944
1078
  if refusal is not None:
945
1079
  return {"error": refusal}
1080
+ # ROADMAP_v3 §56 (JV14): a `reject` the gate decided is refused here.
1081
+ refusal = classifier_refusal(parsed.command, parsed.backend,
1082
+ parsed.requires_network, parsed.needs_root)
1083
+ if refusal is not None:
1084
+ return {"error": refusal}
946
1085
 
947
1086
  # Not probed for a WSL or SSH call, and not merely to save the 0.43s:
948
1087
  # the TOCTOU guard below is about a container runtime disappearing
@@ -504,6 +504,15 @@ def _start(name: str, model, params: dict, memory, call_id) -> dict:
504
504
  refusal = shell.uncontained_refusal(parsed.command, backend, needs_root)
505
505
  if refusal is not None:
506
506
  return {"error": refusal}
507
+ # ROADMAP_v3 §56 (JV14): a background or monitor start the decision
508
+ # model refused under `reject`. Not an interactive one: its gate is
509
+ # `_interactive_approval_check`, which never asks the model, and
510
+ # `classifier_key` would judge only its first line.
511
+ if name != INTERACTIVE:
512
+ refusal = shell.classifier_refusal(
513
+ parsed.command, backend, parsed.requires_network, needs_root)
514
+ if refusal is not None:
515
+ return {"error": refusal}
507
516
  # See `shell.run` for why an uncontained call does not probe: the guard
508
517
  # the probe feeds is about the insecure fallback, which it cannot take.
509
518
  docker_up = (True if backend != shell.BACKEND_CONTAINER
package/tui/app.py CHANGED
@@ -71,6 +71,7 @@ from core.file_mentions import FileIndex, active_mention
71
71
  from core.loop import (
72
72
  DEFAULT_PROVIDER,
73
73
  DEFAULT_SYSTEM_PROMPT,
74
+ HARNESS_REASONING_ONLY,
74
75
  RunAgentLoop,
75
76
  advertisement_facts,
76
77
  agent_turn,
@@ -3522,6 +3523,13 @@ class VenastineApp(App):
3522
3523
  elif notice["kind"] in ("compaction_failed",
3523
3524
  "compaction_blocked"):
3524
3525
  transcript.write_error(f"— {notice['text']} —")
3526
+ elif notice["kind"] == HARNESS_REASONING_ONLY:
3527
+ # ROADMAP_v3 §95 (RO5). The harness wrote a row asking the
3528
+ # model to follow its reasoning-only reply, and this notice's
3529
+ # text IS that row's first line -- so it is drawn the way
3530
+ # replay draws the row, and a resumed thread shows the line
3531
+ # the live turn showed, in the same role.
3532
+ transcript.write_role("wake", notice["text"])
3525
3533
  else:
3526
3534
  transcript.write_system(f"— {notice['text']} —")
3527
3535
 
@@ -6139,6 +6147,39 @@ def _cmd_window(app: VenastineApp, args: str) -> None:
6139
6147
  "value.")
6140
6148
 
6141
6149
 
6150
+ def _cmd_classifier(app: VenastineApp, args: str) -> None:
6151
+ """ROADMAP_v3 §56 (JV19): the shell's decision model -- its settings
6152
+ and held verdicts, `test <command>` to see a verdict without running
6153
+ anything, `clear` to ask it again. The text is
6154
+ `core.decision_classifier`'s, which the CLI prints too; `test` is a
6155
+ network call, so it runs in a worker."""
6156
+ from core import decision_classifier
6157
+
6158
+ word, _, rest = args.strip().partition(" ")
6159
+ if word != "test":
6160
+ for line in decision_classifier.command_lines(args):
6161
+ app._transcript.write_system(line)
6162
+ return
6163
+ command = rest.strip()
6164
+ if not command:
6165
+ app._transcript.write_system(decision_classifier.USAGE)
6166
+ return
6167
+
6168
+ def show(lines: list) -> None:
6169
+ for line in lines:
6170
+ app._transcript.write_system(line)
6171
+
6172
+ def work() -> None:
6173
+ try:
6174
+ lines = decision_classifier.test_lines(command)
6175
+ except Exception as exc: # noqa: BLE001 - reported to the user
6176
+ lines = [f"/classifier test failed: {exc}"]
6177
+ app.call_from_thread(show, lines)
6178
+
6179
+ app.run_worker(work, thread=True, exit_on_error=False,
6180
+ name="classifier-test")
6181
+
6182
+
6142
6183
  def _cmd_network(app: VenastineApp, args: str) -> None:
6143
6184
  """ROADMAP_v3 §53 (IO13): every network meter, its use and its room.
6144
6185
 
@@ -8327,6 +8368,7 @@ def register_builtin_commands() -> None:
8327
8368
  bind("window", _cmd_window),
8328
8369
  bind("trigger", _cmd_trigger),
8329
8370
  bind("network", _cmd_network),
8371
+ bind("classifier", _cmd_classifier),
8330
8372
  bind("config", _cmd_config, complete=_config_rows),
8331
8373
  bind("secrets", _cmd_secrets),
8332
8374
  bind("claims", _cmd_claims),