@tiens.nguyen/gu-cli 1.0.847 → 1.0.849

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.
@@ -7972,7 +7972,7 @@ def _ws_shell_command_risk(scan, denyset, wd="", allowset=None, roots=None):
7972
7972
  if "$" in a[0] or "`" in a[0]:
7973
7973
  return (None, None, f"the program to run is only known at run time ({a[0][:40]})")
7974
7974
  for c in every:
7975
- if _WS_DESTRUCTIVE_RE.search(" ".join(c["argv"])):
7975
+ if _WS_DESTRUCTIVE_RE.search(" ".join(c["argv"])) and not _agent_created_owns_delete(c["argv"], wd):
7976
7976
  return (None, None, "a destructive command (deletes files / wipes disk / powers off)")
7977
7977
  return (None, None, None)
7978
7978
 
@@ -8796,6 +8796,71 @@ _WS_DESTRUCTIVE_RE = re.compile(
8796
8796
  # files) and the system temp folder deleting stays free; anywhere else it asks.
8797
8797
  _WS_DELETE_RUNNERS = {"rm", "rmdir", "unlink", "del", "erase", "rd", "remove-item", "trash"}
8798
8798
 
8799
+ # ---- GU'S OWN FILES (issue #194): deleting a file gu itself made is cleanup, not risk -------
8800
+ # The edit/create tools and run_command redirects register every path they make in this session
8801
+ # set. When a later command deletes NOTHING BUT registered files - a bare `rm FILE...` with no
8802
+ # flags (no -r), no globs, no recursion - the destructive question would be gu asking to undo
8803
+ # its own work, so it just runs. Anything else (a flag, a path gu did not make, `rm -r`) asks
8804
+ # exactly as before. Entries leave the set when gu deletes the file, and the set is capped.
8805
+ _AGENT_CREATED_FILES = set()
8806
+ _AGENT_CREATED_MAX = 500
8807
+
8808
+
8809
+ def _agent_created_add(path):
8810
+ """Remember a file gu just made, so cleaning it up later asks nothing."""
8811
+ if not path:
8812
+ return
8813
+ try:
8814
+ path = os.path.realpath(path)
8815
+ except OSError:
8816
+ return
8817
+ while len(_AGENT_CREATED_FILES) >= _AGENT_CREATED_MAX:
8818
+ _AGENT_CREATED_FILES.pop() # arbitrary member - the cap is the point
8819
+ _AGENT_CREATED_FILES.add(path)
8820
+
8821
+
8822
+ def _agent_created_forget(path):
8823
+ try:
8824
+ _AGENT_CREATED_FILES.discard(os.path.realpath(path))
8825
+ except OSError:
8826
+ pass
8827
+
8828
+
8829
+ def _agent_created_rm_targets(argv, wd):
8830
+ """Resolved targets of a bare `rm FILE...`, or [] when anything makes it more than that."""
8831
+ runner = os.path.basename(argv[0]).lower() if argv else ""
8832
+ if runner.endswith(".exe"):
8833
+ runner = runner[:-4]
8834
+ if runner not in ("rm", "unlink"):
8835
+ return []
8836
+ args = [a for a in argv[1:] if a != "--"]
8837
+ files = [a for a in args if not a.startswith("-")]
8838
+ flags = "".join(a.lstrip("-") for a in args if a.startswith("-"))
8839
+ if not files:
8840
+ return []
8841
+ # The destructive gate wakes on -r/-f; a delete gets DANGEROUS on -r (a whole tree, maybe
8842
+ # more than what gu wrote), so that keeps asking even for our own. -f (and any other
8843
+ # non-recursive flag) just silences prompts for the SAME files - inherits the own-files pass.
8844
+ if "r" in flags.lower():
8845
+ return []
8846
+ targets = []
8847
+ for t in files:
8848
+ if any(ch in t for ch in "*?["):
8849
+ return [] # a GLOB can match files gu never made
8850
+ p = os.path.expanduser(_shell_path_to_native(t))
8851
+ p = p if os.path.isabs(p) else os.path.join(wd or os.getcwd(), p)
8852
+ try:
8853
+ targets.append(os.path.realpath(p))
8854
+ except OSError:
8855
+ return []
8856
+ return targets
8857
+
8858
+
8859
+ def _agent_created_owns_delete(argv, wd=""):
8860
+ """True only when the command deletes NOTHING BUT files gu itself made this session."""
8861
+ targets = _agent_created_rm_targets(argv, wd)
8862
+ return bool(targets) and all(t in _AGENT_CREATED_FILES for t in targets)
8863
+
8799
8864
 
8800
8865
  def _ws_delete_targets(argv):
8801
8866
  """The paths a delete command would remove ([] = not a delete)."""
@@ -8972,7 +9037,7 @@ def _ws_command_risk(command, argv, denyset, wd=""):
8972
9037
  outside = _ws_delete_outside_reason(argv, wd)
8973
9038
  if outside:
8974
9039
  return (outside, outside)
8975
- if _WS_DESTRUCTIVE_RE.search(cmd):
9040
+ if _WS_DESTRUCTIVE_RE.search(cmd) and not _agent_created_owns_delete(argv, wd):
8976
9041
  return (None, "a destructive command (deletes files / wipes disk / powers off)")
8977
9042
  return (None, None)
8978
9043
 
@@ -9207,6 +9272,7 @@ def _ws_write_file(path: str, content: str, action: str, backup) -> None:
9207
9272
  with open(target, "w", encoding="utf-8") as fh:
9208
9273
  fh.write(content)
9209
9274
  os.unlink(tmp)
9275
+ _agent_created_add(target)
9210
9276
  except BaseException:
9211
9277
  try:
9212
9278
  os.unlink(tmp)
@@ -15579,6 +15645,26 @@ def run_agent_chat(cfg):
15579
15645
  if _interactive_approval and _job_id:
15580
15646
  _accept_edits = _live_accept_edits(worker_api_base, worker_api_key,
15581
15647
  _job_id, _accept_edits)
15648
+ # Track what this line CREATES, so cleaning it up later is not a question
15649
+ # (issue #194): redirect targets inside the project join the own-files set;
15650
+ # an rm of nothing-but-own-files leaves it again once the line ran.
15651
+ if _use_shell and _scan.get("tokens"):
15652
+ for _oc in _shell_commands(_scan["tokens"]):
15653
+ _oa = _oc["argv"]
15654
+ if _agent_created_owns_delete(_oa, wd):
15655
+ for _ot in _agent_created_rm_targets(_oa, wd):
15656
+ _agent_created_forget(_ot)
15657
+ continue
15658
+ for _o, _otgt in _oc["redirects"]:
15659
+ if _o in (">", ">>", "1>", "1>>") and _otgt and not _otgt.startswith("&"):
15660
+ _op = os.path.expanduser(_otgt)
15661
+ _op = _op if os.path.isabs(_op) else os.path.join(wd or os.getcwd(), _op)
15662
+ try:
15663
+ _opr = os.path.realpath(_op)
15664
+ if any(_ws_is_inside(_opr, [os.path.realpath(r["path"])]) for r in _WS_ROOTS if r.get("path")):
15665
+ _agent_created_add(_opr)
15666
+ except Exception: # noqa: BLE001
15667
+ pass
15582
15668
  # ACCEPT EDITS OFF ⇒ ASK BEFORE RUNNING ANYTHING (task #179). The risk gate is
15583
15669
  # the FLOOR, not the whole policy: sudo and rm -rf ask either way, and turning
15584
15670
  # the toggle off raises the bar to every command. Off is the cautious setting,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiens.nguyen/gu-cli",
3
- "version": "1.0.847",
3
+ "version": "1.0.849",
4
4
  "description": "Gu CLI — the gu terminal plus the local worker that runs agent / OCR / PDF / embedding jobs on your Mac (Ollama / MLX / OpenAI-compatible).",
5
5
  "type": "module",
6
6
  "license": "MIT",