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