@codyswann/lisa 4.103.13 → 4.104.0

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 (91) hide show
  1. package/all/copy-overwrite/scripts/lisa-hooks/worktree-binding-guard.mjs +722 -5
  2. package/all/copy-overwrite/scripts/lisa-hooks/worktree-binding-guard.sh +4 -1
  3. package/dist/core/lisa-owned-hash-ledger.d.ts.map +1 -1
  4. package/dist/core/lisa-owned-hash-ledger.js +10 -0
  5. package/dist/core/lisa-owned-hash-ledger.js.map +1 -1
  6. package/dist/core/nightly-e2e-guard-behavior-certificate.js +2 -2
  7. package/dist/core/nightly-e2e-guard-behavior-certificate.js.map +1 -1
  8. package/dist/core/upstream-evidence-manifest.d.ts.map +1 -1
  9. package/dist/core/upstream-evidence-manifest.js +6 -4
  10. package/dist/core/upstream-evidence-manifest.js.map +1 -1
  11. package/package.json +4 -4
  12. package/plugins/lisa/.claude-plugin/plugin.json +1 -1
  13. package/plugins/lisa/.codex-plugin/plugin.json +1 -1
  14. package/plugins/lisa/hooks/worktree-binding-guard.mjs +722 -5
  15. package/plugins/lisa/hooks/worktree-binding-guard.sh +4 -1
  16. package/plugins/lisa-agy/plugin.json +1 -1
  17. package/plugins/lisa-cdk/.claude-plugin/plugin.json +1 -1
  18. package/plugins/lisa-cdk/.codex-plugin/plugin.json +1 -1
  19. package/plugins/lisa-cdk-agy/plugin.json +1 -1
  20. package/plugins/lisa-cdk-copilot/.claude-plugin/plugin.json +1 -1
  21. package/plugins/lisa-cdk-cursor/.claude-plugin/plugin.json +1 -1
  22. package/plugins/lisa-cdk-devin/.claude-plugin/plugin.json +1 -1
  23. package/plugins/lisa-cdk-devin/.devin-plugin/plugin.json +1 -1
  24. package/plugins/lisa-copilot/.claude-plugin/plugin.json +1 -1
  25. package/plugins/lisa-copilot/hooks/worktree-binding-guard.mjs +722 -5
  26. package/plugins/lisa-copilot/hooks/worktree-binding-guard.sh +4 -1
  27. package/plugins/lisa-cursor/.claude-plugin/plugin.json +1 -1
  28. package/plugins/lisa-cursor/hooks/worktree-binding-guard.mjs +722 -5
  29. package/plugins/lisa-cursor/hooks/worktree-binding-guard.sh +4 -1
  30. package/plugins/lisa-devin/.claude-plugin/plugin.json +1 -1
  31. package/plugins/lisa-devin/.devin-plugin/plugin.json +1 -1
  32. package/plugins/lisa-devin/hooks/worktree-binding-guard.mjs +722 -5
  33. package/plugins/lisa-devin/hooks/worktree-binding-guard.sh +4 -1
  34. package/plugins/lisa-expo/.claude-plugin/plugin.json +1 -1
  35. package/plugins/lisa-expo/.codex-plugin/plugin.json +1 -1
  36. package/plugins/lisa-expo-agy/plugin.json +1 -1
  37. package/plugins/lisa-expo-copilot/.claude-plugin/plugin.json +1 -1
  38. package/plugins/lisa-expo-cursor/.claude-plugin/plugin.json +1 -1
  39. package/plugins/lisa-expo-devin/.claude-plugin/plugin.json +1 -1
  40. package/plugins/lisa-expo-devin/.devin-plugin/plugin.json +1 -1
  41. package/plugins/lisa-harper-fabric/.claude-plugin/plugin.json +1 -1
  42. package/plugins/lisa-harper-fabric/.codex-plugin/plugin.json +1 -1
  43. package/plugins/lisa-harper-fabric-agy/plugin.json +1 -1
  44. package/plugins/lisa-harper-fabric-copilot/.claude-plugin/plugin.json +1 -1
  45. package/plugins/lisa-harper-fabric-cursor/.claude-plugin/plugin.json +1 -1
  46. package/plugins/lisa-harper-fabric-devin/.claude-plugin/plugin.json +1 -1
  47. package/plugins/lisa-harper-fabric-devin/.devin-plugin/plugin.json +1 -1
  48. package/plugins/lisa-nestjs/.claude-plugin/plugin.json +1 -1
  49. package/plugins/lisa-nestjs/.codex-plugin/plugin.json +1 -1
  50. package/plugins/lisa-nestjs-agy/plugin.json +1 -1
  51. package/plugins/lisa-nestjs-copilot/.claude-plugin/plugin.json +1 -1
  52. package/plugins/lisa-nestjs-cursor/.claude-plugin/plugin.json +1 -1
  53. package/plugins/lisa-nestjs-devin/.claude-plugin/plugin.json +1 -1
  54. package/plugins/lisa-nestjs-devin/.devin-plugin/plugin.json +1 -1
  55. package/plugins/lisa-openclaw/.claude-plugin/plugin.json +1 -1
  56. package/plugins/lisa-openclaw/.codex-plugin/plugin.json +1 -1
  57. package/plugins/lisa-openclaw-agy/plugin.json +1 -1
  58. package/plugins/lisa-openclaw-copilot/.claude-plugin/plugin.json +1 -1
  59. package/plugins/lisa-openclaw-cursor/.claude-plugin/plugin.json +1 -1
  60. package/plugins/lisa-openclaw-devin/.claude-plugin/plugin.json +1 -1
  61. package/plugins/lisa-openclaw-devin/.devin-plugin/plugin.json +1 -1
  62. package/plugins/lisa-phaser/.claude-plugin/plugin.json +1 -1
  63. package/plugins/lisa-phaser/.codex-plugin/plugin.json +1 -1
  64. package/plugins/lisa-phaser-agy/plugin.json +1 -1
  65. package/plugins/lisa-phaser-copilot/.claude-plugin/plugin.json +1 -1
  66. package/plugins/lisa-phaser-cursor/.claude-plugin/plugin.json +1 -1
  67. package/plugins/lisa-phaser-devin/.claude-plugin/plugin.json +1 -1
  68. package/plugins/lisa-phaser-devin/.devin-plugin/plugin.json +1 -1
  69. package/plugins/lisa-rails/.claude-plugin/plugin.json +1 -1
  70. package/plugins/lisa-rails/.codex-plugin/plugin.json +1 -1
  71. package/plugins/lisa-rails-agy/plugin.json +1 -1
  72. package/plugins/lisa-rails-copilot/.claude-plugin/plugin.json +1 -1
  73. package/plugins/lisa-rails-cursor/.claude-plugin/plugin.json +1 -1
  74. package/plugins/lisa-rails-devin/.claude-plugin/plugin.json +1 -1
  75. package/plugins/lisa-rails-devin/.devin-plugin/plugin.json +1 -1
  76. package/plugins/lisa-typescript/.claude-plugin/plugin.json +1 -1
  77. package/plugins/lisa-typescript/.codex-plugin/plugin.json +1 -1
  78. package/plugins/lisa-typescript-agy/plugin.json +1 -1
  79. package/plugins/lisa-typescript-copilot/.claude-plugin/plugin.json +1 -1
  80. package/plugins/lisa-typescript-cursor/.claude-plugin/plugin.json +1 -1
  81. package/plugins/lisa-typescript-devin/.claude-plugin/plugin.json +1 -1
  82. package/plugins/lisa-typescript-devin/.devin-plugin/plugin.json +1 -1
  83. package/plugins/lisa-wiki/.claude-plugin/plugin.json +1 -1
  84. package/plugins/lisa-wiki/.codex-plugin/plugin.json +1 -1
  85. package/plugins/lisa-wiki-agy/plugin.json +1 -1
  86. package/plugins/lisa-wiki-copilot/.claude-plugin/plugin.json +1 -1
  87. package/plugins/lisa-wiki-cursor/.claude-plugin/plugin.json +1 -1
  88. package/plugins/lisa-wiki-devin/.claude-plugin/plugin.json +1 -1
  89. package/plugins/lisa-wiki-devin/.devin-plugin/plugin.json +1 -1
  90. package/plugins/src/base/hooks/worktree-binding-guard.mjs +722 -5
  91. package/plugins/src/base/hooks/worktree-binding-guard.sh +4 -1
@@ -29,10 +29,19 @@
29
29
  * `EnterWorktree` to fail is met one tool call later, by the guard, not by the
30
30
  * tool.
31
31
  *
32
- * WHY BLOCKING AND NOT A WARNING. The failure mode is that nothing feels wrong.
33
- * There is no symptom to notice, so an advisory line scrolls past exactly when
34
- * it matters. A refusal is the only form of this check the agent cannot read
35
- * past.
32
+ * WHY BLOCKING AND NOT A WARNING — FOR WRITES. The failure mode is that nothing
33
+ * feels wrong. There is no symptom to notice, so an advisory line scrolls past
34
+ * exactly when it matters. A refusal is the only form of this check the agent
35
+ * cannot read past.
36
+ *
37
+ * WHY ONLY A WARNING FOR PROVABLE READS (CodySwannGT/lisa#4816). Only a write
38
+ * can damage another session's work, and refusing `git status` or `rg` in a
39
+ * mismatched tree cost a manual acknowledge-and-retry for nothing. So while the
40
+ * trees disagree, a Bash command this guard can PROVE read-only — every
41
+ * statement an allowlisted read shape, see {@link isReadOnlyCommand} — is
42
+ * allowed with a non-blocking warning naming both trees. Everything else,
43
+ * including every command the guard cannot classify, is refused exactly as
44
+ * before. The allowlist fails closed: an unknown program is a write.
36
45
  *
37
46
  * WHY AN EXPLICIT ACKNOWLEDGEMENT AND NOT "BLOCK ONCE". A guard that blocks the
38
47
  * first attempt and lets the retry through is defeated by a blind retry, which
@@ -543,6 +552,705 @@ function reachesOut(script, foreign, bound) {
543
552
  ]);
544
553
  }
545
554
 
555
+ // ---------------------------------------------------------------------------
556
+ // Read-only classification for a MISMATCHED session (CodySwannGT/lisa#4816)
557
+ //
558
+ // Used only once the guard has already decided the session's expected and
559
+ // actual worktrees disagree. It answers one question — "is this Bash command
560
+ // provably unable to change anything?" — and its only effect is to turn a
561
+ // refusal into an allow-with-warning. So every doubt answers "no": an
562
+ // unparseable command, an expansion the guard cannot see through, a program
563
+ // nobody listed, a listed program given an option that writes or executes.
564
+ //
565
+ // It is an ALLOWLIST of read shapes, not a denylist of writes. A denylist
566
+ // fails open on the write nobody enumerated; this fails closed on the read
567
+ // nobody enumerated, which costs one acknowledgement — the pre-#4816
568
+ // behaviour — and nothing else.
569
+ //
570
+ // Tests and builds are deliberately absent even though they mostly read: a
571
+ // `bun run test` or `npm run build` executes whatever the package's scripts
572
+ // say, writes coverage and build output into the tree it runs in, and is
573
+ // exactly the "executing a script that reaches that tree" write class.
574
+ // Inspection is enumerated; execution is not.
575
+
576
+ /** Programs that only read and print, whatever operands they are given. */
577
+ const PURE_READERS = new Set([
578
+ "basename",
579
+ "cat",
580
+ "cksum",
581
+ "cmp",
582
+ "column",
583
+ "comm",
584
+ "cut",
585
+ "df",
586
+ "diff",
587
+ "dirname",
588
+ "du",
589
+ "echo",
590
+ "egrep",
591
+ "false",
592
+ "fgrep",
593
+ "fold",
594
+ "grep",
595
+ "head",
596
+ "id",
597
+ "jq",
598
+ "ls",
599
+ "md5",
600
+ "md5sum",
601
+ "nl",
602
+ "od",
603
+ "printenv",
604
+ "printf",
605
+ "pwd",
606
+ "readlink",
607
+ "realpath",
608
+ "rev",
609
+ "sha1sum",
610
+ "sha256sum",
611
+ "sha512sum",
612
+ "shasum",
613
+ "sleep",
614
+ "stat",
615
+ "strings",
616
+ "tac",
617
+ "tail",
618
+ "test",
619
+ "tr",
620
+ "true",
621
+ "type",
622
+ "uname",
623
+ "wc",
624
+ "which",
625
+ "whoami",
626
+ ]);
627
+
628
+ /** A command word this guard will look up: a bare name, never a path. */
629
+ const COMMAND_WORD = /^\w[\w.+-]*$/u;
630
+
631
+ /** Whether a short-option cluster (`-abc`) contains `letter`. */
632
+ function shortCluster(arg, letter) {
633
+ return /^-[^-]/u.test(arg) && arg.includes(letter);
634
+ }
635
+
636
+ /** `find` primaries that execute, delete, or write a file. */
637
+ const FIND_WRITES = new Set([
638
+ "-delete",
639
+ "-exec",
640
+ "-execdir",
641
+ "-fls",
642
+ "-fprint",
643
+ "-fprint0",
644
+ "-fprintf",
645
+ "-ok",
646
+ "-okdir",
647
+ ]);
648
+
649
+ /** Git subcommands that only read the repository. */
650
+ const GIT_READS = new Set([
651
+ "annotate",
652
+ "blame",
653
+ "cat-file",
654
+ "check-attr",
655
+ "check-ignore",
656
+ "cherry",
657
+ "count-objects",
658
+ "describe",
659
+ "diff",
660
+ "diff-files",
661
+ "diff-index",
662
+ "diff-tree",
663
+ "for-each-ref",
664
+ "grep",
665
+ "help",
666
+ "log",
667
+ "ls-files",
668
+ "ls-remote",
669
+ "ls-tree",
670
+ "merge-base",
671
+ "name-rev",
672
+ "range-diff",
673
+ "rev-list",
674
+ "rev-parse",
675
+ "shortlog",
676
+ "show",
677
+ "show-branch",
678
+ "show-ref",
679
+ "status",
680
+ "var",
681
+ "version",
682
+ "whatchanged",
683
+ ]);
684
+
685
+ /** Git options, before the subcommand, that change nothing. */
686
+ const GIT_SAFE_GLOBALS = new Set([
687
+ "--bare",
688
+ "--literal-pathspecs",
689
+ "--no-optional-locks",
690
+ "--no-pager",
691
+ "--no-replace-objects",
692
+ "-P",
693
+ ]);
694
+
695
+ /** Options that put `git branch` / `git tag` in list mode. */
696
+ const GIT_LIST_MODE = new Set([
697
+ "--contains",
698
+ "--list",
699
+ "--merged",
700
+ "--no-contains",
701
+ "--no-merged",
702
+ "--points-at",
703
+ "-l",
704
+ ]);
705
+
706
+ /** Options `git branch` / `git tag` accept without creating or deleting. */
707
+ const GIT_LIST_OPTIONS = new Set([
708
+ ...GIT_LIST_MODE,
709
+ "--all",
710
+ "--color",
711
+ "--column",
712
+ "--ignore-case",
713
+ "--no-color",
714
+ "--no-column",
715
+ "--remotes",
716
+ "--show-current",
717
+ "--verbose",
718
+ "-a",
719
+ "-i",
720
+ "-r",
721
+ "-v",
722
+ "-vv",
723
+ ]);
724
+
725
+ /** `--opt=value` filters that, like their bare forms, imply list mode. */
726
+ const GIT_LIST_FILTER_PREFIXES = [
727
+ "--contains=",
728
+ "--merged=",
729
+ "--no-contains=",
730
+ "--no-merged=",
731
+ "--points-at=",
732
+ ];
733
+
734
+ /** Prefixes of `--opt=value` spellings that only shape a listing. */
735
+ const GIT_LIST_VALUE_PREFIXES = [
736
+ ...GIT_LIST_FILTER_PREFIXES,
737
+ "--color=",
738
+ "--format=",
739
+ "--sort=",
740
+ ];
741
+
742
+ /** `git config` options that read a value. */
743
+ const GIT_CONFIG_READS = new Set([
744
+ "--get",
745
+ "--get-all",
746
+ "--get-color",
747
+ "--get-colorbool",
748
+ "--get-regexp",
749
+ "--get-urlmatch",
750
+ "--list",
751
+ "-l",
752
+ ]);
753
+
754
+ /** `git config` options that write, whatever else is present. */
755
+ const GIT_CONFIG_WRITES = new Set([
756
+ "--add",
757
+ "--edit",
758
+ "--remove-section",
759
+ "--rename-section",
760
+ "--replace-all",
761
+ "--unset",
762
+ "--unset-all",
763
+ "-e",
764
+ ]);
765
+
766
+ /** `git branch` / `git tag` in list mode, with no creating option. */
767
+ function gitListing(args) {
768
+ const known = args.every(
769
+ arg =>
770
+ !arg.startsWith("-") ||
771
+ GIT_LIST_OPTIONS.has(arg) ||
772
+ /^-n\d*$/u.test(arg) ||
773
+ GIT_LIST_VALUE_PREFIXES.some(prefix => arg.startsWith(prefix))
774
+ );
775
+ const listing =
776
+ !args.some(arg => !arg.startsWith("-")) ||
777
+ args.some(
778
+ arg =>
779
+ GIT_LIST_MODE.has(arg) ||
780
+ GIT_LIST_FILTER_PREFIXES.some(prefix => arg.startsWith(prefix))
781
+ );
782
+ return known && listing;
783
+ }
784
+
785
+ /** The first argument that is not an option, or undefined. */
786
+ function firstOperand(args) {
787
+ return args.find(arg => !arg.startsWith("-"));
788
+ }
789
+
790
+ /** Git subcommands that read only in some forms, each with its reading test. */
791
+ const GIT_CONDITIONAL_READS = new Map([
792
+ ["branch", gitListing],
793
+ ["tag", gitListing],
794
+ [
795
+ "remote",
796
+ args => {
797
+ const operand = firstOperand(args);
798
+ return operand === undefined
799
+ ? args.every(arg => arg === "-v" || arg === "--verbose")
800
+ : operand === "get-url" || operand === "show";
801
+ },
802
+ ],
803
+ ["stash", args => args[0] === "list" || args[0] === "show"],
804
+ ["worktree", args => args[0] === "list"],
805
+ ["reflog", args => [undefined, "show"].includes(firstOperand(args))],
806
+ [
807
+ "config",
808
+ args =>
809
+ args[0] === "get" ||
810
+ args[0] === "list" ||
811
+ (args.some(arg => GIT_CONFIG_READS.has(arg)) &&
812
+ !args.some(arg => GIT_CONFIG_WRITES.has(arg))),
813
+ ],
814
+ ]);
815
+
816
+ /**
817
+ * A git invocation that only reads.
818
+ *
819
+ * `-c` is refused rather than skipped: `-c core.fsmonitor=<cmd>` and friends
820
+ * execute programs, and a guard that read past it would be read past.
821
+ */
822
+ function gitReadOnly(args) {
823
+ let index = 0;
824
+ while (index < args.length && args[index].startsWith("-")) {
825
+ const arg = args[index];
826
+ if (arg === "-C") index += 2;
827
+ else if (
828
+ GIT_SAFE_GLOBALS.has(arg) ||
829
+ arg.startsWith("--git-dir=") ||
830
+ arg.startsWith("--work-tree=")
831
+ )
832
+ index += 1;
833
+ else return false;
834
+ }
835
+ const [subcommand, ...rest] = args.slice(index);
836
+ if (subcommand === undefined) return false;
837
+ // `--output=<file>` writes for diff/log/show; `-O` runs a pager program.
838
+ if (rest.some(arg => arg.startsWith("--output") || arg.startsWith("-O")))
839
+ return false;
840
+ if (GIT_READS.has(subcommand)) return true;
841
+ return GIT_CONDITIONAL_READS.get(subcommand)?.(rest) ?? false;
842
+ }
843
+
844
+ /** `gh` verbs that only fetch and print. `gh api` can POST, so it is out. */
845
+ const GH_READ_VERBS = new Set([
846
+ "checks",
847
+ "diff",
848
+ "list",
849
+ "status",
850
+ "view",
851
+ "watch",
852
+ ]);
853
+
854
+ /** A `gh <noun> <verb>` invocation whose verb only reads. */
855
+ function ghReadOnly(args) {
856
+ const [noun, verb] = args.filter(arg => !arg.startsWith("-"));
857
+ return noun !== "api" && GH_READ_VERBS.has(verb);
858
+ }
859
+
860
+ /**
861
+ * One sed command that only prints: an optional address or range, an optional
862
+ * `!`, then `p d l q Q = n N`, or an `s///` whose flags write nothing. The
863
+ * `w` and `e` flags and commands — the ones that write a file or execute one —
864
+ * are absent from the grammar rather than denied by it.
865
+ */
866
+ const SED_ADDRESS = String.raw`(?:\d+(?:~\d+)?|\$|/(?:\\.|[^/\\\n])*/I?)`;
867
+ const SED_READ_SCRIPT = new RegExp(
868
+ String.raw`^\s*(?:${SED_ADDRESS}(?:\s*,\s*(?:${SED_ADDRESS}|[+~]\d+))?)?\s*!?\s*` +
869
+ String.raw`(?:[pdlqQ=nN]|s([^\s\\])(?:\\.|(?!\1)[^\\\n])*\1(?:\\.|(?!\1)[^\\\n])*\1[gpiImM\d]*)\s*$`,
870
+ "u"
871
+ );
872
+
873
+ /** sed short options that neither edit in place nor read a script file. */
874
+ const SED_SAFE_FLAGS = /^[nErsuz]*$/u;
875
+
876
+ /** sed long options that neither edit in place nor read a script file. */
877
+ const SED_SAFE_LONG = new Set([
878
+ "--debug",
879
+ "--null-data",
880
+ "--posix",
881
+ "--quiet",
882
+ "--regexp-extended",
883
+ "--sandbox",
884
+ "--separate",
885
+ "--silent",
886
+ "--unbuffered",
887
+ ]);
888
+
889
+ /**
890
+ * A sed invocation that only prints. Every script must be literal text in
891
+ * {@link SED_READ_SCRIPT}'s grammar; `-i` and `-f` are refused outright.
892
+ */
893
+ function sedReadOnly(words) {
894
+ const scripts = [];
895
+ const operands = [];
896
+ let index = 0;
897
+ while (index < words.length) {
898
+ const { text } = words[index];
899
+ if (text === "--expression" || text === "-e") {
900
+ index += 1;
901
+ scripts.push(words[index]);
902
+ } else if (text.startsWith("--expression=")) {
903
+ scripts.push({ ...words[index], text: text.slice(13) });
904
+ } else if (text.startsWith("--")) {
905
+ if (!SED_SAFE_LONG.has(text)) return false;
906
+ } else if (text.startsWith("-") && text.length > 1) {
907
+ const letters = text.slice(1);
908
+ const scriptAt = letters.indexOf("e");
909
+ const flags = scriptAt === -1 ? letters : letters.slice(0, scriptAt);
910
+ if (!SED_SAFE_FLAGS.test(flags)) return false;
911
+ if (scriptAt !== -1) {
912
+ const attached = text.slice(scriptAt + 2);
913
+ if (attached) scripts.push({ ...words[index], text: attached });
914
+ else {
915
+ index += 1;
916
+ scripts.push(words[index]);
917
+ }
918
+ }
919
+ } else operands.push(words[index]);
920
+ index += 1;
921
+ }
922
+ if (scripts.length === 0 && operands.length > 0) scripts.push(operands[0]);
923
+ return (
924
+ scripts.length > 0 &&
925
+ scripts.every(
926
+ script => script?.exact === true && SED_READ_SCRIPT.test(script.text)
927
+ )
928
+ );
929
+ }
930
+
931
+ /**
932
+ * An awk invocation that only prints. Its program must be literal and free of
933
+ * the three ways awk reaches outside itself: `>` (write a file), `|` (pipe to
934
+ * or from a command), and `system`/`getline`.
935
+ */
936
+ function awkReadOnly(words) {
937
+ let index = 0;
938
+ while (index < words.length && words[index].text.startsWith("-")) {
939
+ const { text } = words[index];
940
+ if (text === "--") {
941
+ index += 1;
942
+ break;
943
+ }
944
+ if (text === "-F" || text === "-v") index += 2;
945
+ else if (text.startsWith("-F") || text.startsWith("-v")) index += 1;
946
+ else return false;
947
+ }
948
+ const program = words[index];
949
+ return program?.exact === true && !/[>|]|system|getline/u.test(program.text);
950
+ }
951
+
952
+ /** xargs flags that take no value. */
953
+ const XARGS_FLAGS = new Set([
954
+ "--exit",
955
+ "--no-run-if-empty",
956
+ "--null",
957
+ "--verbose",
958
+ "-0",
959
+ "-r",
960
+ "-t",
961
+ "-x",
962
+ ]);
963
+
964
+ /** xargs options whose value is the next word or attached (`-n1`). */
965
+ const XARGS_VALUE_OPTIONS = new Set([
966
+ "-E",
967
+ "-I",
968
+ "-J",
969
+ "-L",
970
+ "-P",
971
+ "-R",
972
+ "-S",
973
+ "-d",
974
+ "-n",
975
+ "-s",
976
+ ]);
977
+
978
+ /**
979
+ * xargs running a pure reader. The items arrive on stdin and could be any
980
+ * option, so only a program no option can turn into a writer qualifies.
981
+ */
982
+ function xargsReadOnly(words) {
983
+ let index = 0;
984
+ while (index < words.length && words[index].text.startsWith("-")) {
985
+ const { text } = words[index];
986
+ if (XARGS_FLAGS.has(text)) index += 1;
987
+ else if (XARGS_VALUE_OPTIONS.has(text)) index += 2;
988
+ else if (XARGS_VALUE_OPTIONS.has(text.slice(0, 2))) index += 1;
989
+ else return false;
990
+ }
991
+ const inner = words[index];
992
+ return inner?.exact === true && PURE_READERS.has(inner.text);
993
+ }
994
+
995
+ /**
996
+ * Programs that read unless given a particular option, each with the test that
997
+ * its arguments contain none. Each test sees only the argument texts; a word
998
+ * whose leading text is an expansion never reaches it (see {@link optionVisible}).
999
+ */
1000
+ const CHECKED_READERS = new Map([
1001
+ ["awk", awkReadOnly],
1002
+ [
1003
+ "date",
1004
+ words =>
1005
+ !words.some(
1006
+ ({ text }) => text.startsWith("--set") || shortCluster(text, "s")
1007
+ ),
1008
+ ],
1009
+ [
1010
+ "file",
1011
+ words =>
1012
+ !words.some(
1013
+ ({ text }) => text === "--compile" || shortCluster(text, "C")
1014
+ ),
1015
+ ],
1016
+ ["find", words => !words.some(({ text }) => FIND_WRITES.has(text))],
1017
+ ["gh", words => ghReadOnly(words.map(({ text }) => text))],
1018
+ ["git", words => gitReadOnly(words.map(({ text }) => text))],
1019
+ [
1020
+ "rg",
1021
+ words =>
1022
+ !words.some(
1023
+ ({ text }) =>
1024
+ text.startsWith("--pre") || text.startsWith("--hostname-bin")
1025
+ ),
1026
+ ],
1027
+ ["sed", sedReadOnly],
1028
+ [
1029
+ "sort",
1030
+ words =>
1031
+ !words.some(
1032
+ ({ text }) =>
1033
+ text.startsWith("--output") ||
1034
+ text.startsWith("--compress-program") ||
1035
+ shortCluster(text, "o")
1036
+ ),
1037
+ ],
1038
+ ["tree", words => !words.some(({ text }) => shortCluster(text, "o"))],
1039
+ ["xargs", xargsReadOnly],
1040
+ ]);
1041
+
1042
+ /**
1043
+ * Whether the guard can see if this word is an option.
1044
+ *
1045
+ * An exact word is what the program receives. A word that STARTS with literal
1046
+ * text other than `-` cannot become an option however its tail expands. A
1047
+ * word that starts with an expansion — `"$X"`, `*` — could become `--pre=…` or
1048
+ * `-i`, so a program with a writing option cannot be cleared past it.
1049
+ */
1050
+ function optionVisible(word) {
1051
+ return word.exact || (word.leading && !word.text.startsWith("-"));
1052
+ }
1053
+
1054
+ /** One statement: a literal command word, then a listed read shape. */
1055
+ function isReadOnlyStatement(words) {
1056
+ const [head, ...args] = words;
1057
+ if (!head.exact || !COMMAND_WORD.test(head.text)) return false;
1058
+ if (head.text === "cd" || PURE_READERS.has(head.text)) return true;
1059
+ const check = CHECKED_READERS.get(head.text);
1060
+ return check !== undefined && args.every(optionVisible) && check(args);
1061
+ }
1062
+
1063
+ /**
1064
+ * Consume one redirection operator starting at `index` (a `<`, `>`, or the
1065
+ * `&` of `&>`), returning the index after its target, or -1 when the
1066
+ * redirection may write or cannot be read. Input redirections read; output
1067
+ * is allowed only as an fd duplication (`2>&1`, `>&2`, `1>&-`) or into
1068
+ * `/dev/null`.
1069
+ */
1070
+ function skipRedirect(text, index) {
1071
+ const output = text[index] !== "<";
1072
+ let at = index + (text[index] === "&" ? 2 : 1);
1073
+ if (!output && "<>(".includes(text[at] ?? "")) return -1;
1074
+ if (output && (text[at] === ">" || text[at] === "|")) at += 1;
1075
+ if (text[at] === "(") return -1;
1076
+ if (text[at] === "&") {
1077
+ const dup = /^(?:\d+|-)/u.exec(text.slice(at + 1));
1078
+ return dup ? at + 1 + dup[0].length : -1;
1079
+ }
1080
+ while (text[at] === " " || text[at] === "\t") at += 1;
1081
+ const target = /^[^\s;|&<>()]+/u.exec(text.slice(at));
1082
+ if (!target) return -1;
1083
+ // Reading any file changes nothing; writing is cleared only into /dev/null.
1084
+ if (output && target[0].replace(/^(["'])(.*)\1$/u, "$2") !== "/dev/null")
1085
+ return -1;
1086
+ return at + target[0].length;
1087
+ }
1088
+
1089
+ /**
1090
+ * Split a Bash command into statements of words, or null when the command
1091
+ * uses anything this lexer will not reason about: command or process
1092
+ * substitution, subshells, here-documents, an unterminated quote, or a
1093
+ * redirection that may write.
1094
+ *
1095
+ * Each word records whether it is EXACT (no expansion or glob can change it)
1096
+ * and whether its LEADING character is literal text, which together decide
1097
+ * whether an option could hide inside it.
1098
+ */
1099
+ function lexStatements(command) {
1100
+ const statements = [[]];
1101
+ let word = null;
1102
+ let quote = null;
1103
+ const begin = () => {
1104
+ word ??= { text: "", exact: true, leading: null };
1105
+ };
1106
+ const add = (char, literal) => {
1107
+ begin();
1108
+ if (word.leading === null) word.leading = literal;
1109
+ if (!literal) word.exact = false;
1110
+ word.text += char;
1111
+ };
1112
+ const finish = () => {
1113
+ if (word)
1114
+ statements.at(-1).push({ ...word, leading: word.leading ?? true });
1115
+ word = null;
1116
+ };
1117
+ const split = () => {
1118
+ finish();
1119
+ statements.push([]);
1120
+ };
1121
+ let index = 0;
1122
+ while (index < command.length) {
1123
+ const char = command[index];
1124
+ const next = command[index + 1];
1125
+ if (quote === "'") {
1126
+ if (char === "'") quote = null;
1127
+ else add(char, true);
1128
+ } else if (quote === '"') {
1129
+ if (char === '"') quote = null;
1130
+ else if (char === "`" || (char === "$" && next === "(")) return null;
1131
+ else if (
1132
+ char === "\\" &&
1133
+ next !== undefined &&
1134
+ '"\\$`\n'.includes(next)
1135
+ ) {
1136
+ index += 1;
1137
+ if (next !== "\n") add(next, true);
1138
+ } else add(char, char !== "$");
1139
+ } else if (char === "\\") {
1140
+ if (next === undefined) return null;
1141
+ index += 1;
1142
+ if (next !== "\n") add(next, true);
1143
+ } else if (char === "'" || char === '"') {
1144
+ begin();
1145
+ quote = char;
1146
+ } else if (char === " " || char === "\t") finish();
1147
+ else if (char === "\n" || char === ";") split();
1148
+ else if (char === "|") {
1149
+ if (next === "|" || next === "&") index += 1;
1150
+ split();
1151
+ } else if (char === "&" && next !== ">") {
1152
+ if (next === "&") index += 1;
1153
+ split();
1154
+ } else if (char === ">" || char === "<" || char === "&") {
1155
+ // A word of bare digits touching the operator is its fd, not an operand.
1156
+ if (word && word.exact && /^\d+$/u.test(word.text)) word = null;
1157
+ else finish();
1158
+ const after = skipRedirect(command, index);
1159
+ if (after === -1) return null;
1160
+ index = after - 1;
1161
+ } else if ("`()".includes(char) || (char === "$" && next === "(")) {
1162
+ return null;
1163
+ } else if (char === "#" && word === null) {
1164
+ while (index + 1 < command.length && command[index + 1] !== "\n")
1165
+ index += 1;
1166
+ } else add(char, !"$*?[{".includes(char));
1167
+ index += 1;
1168
+ }
1169
+ if (quote !== null) return null;
1170
+ finish();
1171
+ return statements.filter(statement => statement.length > 0);
1172
+ }
1173
+
1174
+ /**
1175
+ * Whether this Bash command provably changes nothing: every statement in it
1176
+ * is a listed read shape. Unparseable means no.
1177
+ * @param {string} command - The Bash command, as the hook received it
1178
+ * @returns {boolean} True only for a command proven read-only
1179
+ */
1180
+ function isReadOnlyCommand(command) {
1181
+ const statements = lexStatements(command);
1182
+ return (
1183
+ statements !== null &&
1184
+ statements.length > 0 &&
1185
+ statements.every(isReadOnlyStatement)
1186
+ );
1187
+ }
1188
+
1189
+ /**
1190
+ * Whether a guarded call is a provable read. Write/Edit/MultiEdit are writes by
1191
+ * definition; only a Bash command can be cleared, and only by proof.
1192
+ */
1193
+ function isReadOnlyCall(payload) {
1194
+ const command = payload.tool_input?.command;
1195
+ return (
1196
+ payload.tool_name === "Bash" &&
1197
+ typeof command === "string" &&
1198
+ isReadOnlyCommand(command)
1199
+ );
1200
+ }
1201
+
1202
+ /**
1203
+ * Allow a provable read in a mismatched worktree, and say so to the agent.
1204
+ *
1205
+ * ## The channel, per agent — and where it stops
1206
+ *
1207
+ * stderr on exit 0 reaches a transcript, not the model, so on Claude Code the
1208
+ * warning is ALSO printed as PreToolUse JSON in
1209
+ * `hookSpecificOutput.additionalContext` — the channel
1210
+ * `operational-hazards.mjs` and `block-migration-edits.sh` use. No
1211
+ * `permissionDecision` is emitted: "allow" would also skip the user's
1212
+ * permission prompt, a wider grant than "this guard does not refuse".
1213
+ *
1214
+ * The JSON is printed ONLY where `CLAUDECODE` is set, the one runtime measured
1215
+ * to read that shape on PreToolUse. Codex was measured NOT to honour
1216
+ * PreToolUse `additionalContext` (see `src/codex/scripts/block-migration-edits.sh`),
1217
+ * and Cursor and Copilot were never measured reading it. On those the read is
1218
+ * allowed with the warning on stderr only, which is NOT known to reach the
1219
+ * model. That is a documented gap in the warning, never in the block: a write
1220
+ * is refused identically on every agent this guard runs on. Devin sends no
1221
+ * `cwd`, so this guard does not run there at all; OpenCode and Antigravity do
1222
+ * not ship it (see the wrapper's parity note).
1223
+ * @param {string} expected - The worktree the session believes it is in
1224
+ * @param {string} observed - The worktree this call actually runs in
1225
+ * @param {boolean} claimed - Whether `expected` came from EnterWorktree
1226
+ * @returns {number} Always 0: the read is allowed
1227
+ */
1228
+ function warnRead(expected, observed, claimed) {
1229
+ const notice = [
1230
+ "worktree-binding-guard: allowed a READ-ONLY command in a worktree this session is not bound to.",
1231
+ claimed
1232
+ ? ` EnterWorktree claimed: ${expected}`
1233
+ : ` bound to: ${expected}`,
1234
+ ` acting in: ${observed}`,
1235
+ "Another session may own that worktree and its uncommitted work. This call was",
1236
+ "allowed only because it is provably read-only; any write there — file edits,",
1237
+ "redirects, git commit/checkout/reset/stash, package installs, running a",
1238
+ "script — is still refused. Use absolute paths under the tree you mean.",
1239
+ ].join("\n");
1240
+ process.stderr.write(`${notice}\n`);
1241
+ if (process.env.CLAUDECODE) {
1242
+ process.stdout.write(
1243
+ `${JSON.stringify({
1244
+ hookSpecificOutput: {
1245
+ hookEventName: "PreToolUse",
1246
+ additionalContext: notice,
1247
+ },
1248
+ })}\n`
1249
+ );
1250
+ }
1251
+ return 0;
1252
+ }
1253
+
546
1254
  function stateFile(bindingKey) {
547
1255
  const home = process.env.LISA_STATE_HOME || join(homedir(), ".lisa");
548
1256
  return join(home, "worktree-binding", `${bindingKey}.json`);
@@ -1320,7 +2028,12 @@ function evaluate(payload) {
1320
2028
  !isLinkedWorktree(state.boundRoot, observed)
1321
2029
  )
1322
2030
  return 0;
2031
+ // A mismatch. From here a provable read is allowed with a warning and
2032
+ // everything else — every write and every command the guard cannot classify
2033
+ // — is refused exactly as before (CodySwannGT/lisa#4816).
1323
2034
  if (state.claimedRoot && state.claimedRoot !== observed) {
2035
+ if (isReadOnlyCall(payload))
2036
+ return warnRead(state.claimedRoot, observed, true);
1324
2037
  return unconfirmedSwitch(state.claimedRoot, observed);
1325
2038
  }
1326
2039
  if (state.claimedRoot === observed) {
@@ -1337,7 +2050,11 @@ function evaluate(payload) {
1337
2050
  if (written === BINDING_WRITE_CONFLICT) return bindingWriteConflict();
1338
2051
  return 0;
1339
2052
  }
1340
- if (state.boundRoot !== observed) return displaced(state.boundRoot, observed);
2053
+ if (state.boundRoot !== observed) {
2054
+ if (isReadOnlyCall(payload))
2055
+ return warnRead(state.boundRoot, observed, false);
2056
+ return displaced(state.boundRoot, observed);
2057
+ }
1341
2058
  // Last, and only once the session is provably where it should be: a script
1342
2059
  // that moves somewhere else after this check has already run.
1343
2060
  const reach = scriptReachingForeignTree(payload, observed);