@gotgenes/pi-permission-system 5.3.4 → 5.5.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.
@@ -661,3 +661,322 @@ describe("checkPermission — rule origin provenance", () => {
661
661
  expect(result.origin).toBe("builtin");
662
662
  });
663
663
  });
664
+
665
+ // ---------------------------------------------------------------------------
666
+ // In-memory PolicyLoader stub tests — no filesystem required
667
+ // ---------------------------------------------------------------------------
668
+
669
+ import type { PolicyLoader } from "../src/permission-manager";
670
+ import type { ResolvedPolicyPaths } from "../src/policy-loader";
671
+ import type { ScopeConfig } from "../src/types";
672
+
673
+ /**
674
+ * Minimal in-memory PolicyLoader for testing merge + evaluation logic
675
+ * without touching the filesystem.
676
+ */
677
+ function createInMemoryPolicyLoader(
678
+ scopes: {
679
+ global?: ScopeConfig;
680
+ project?: ScopeConfig;
681
+ agent?: Record<string, ScopeConfig>;
682
+ projectAgent?: Record<string, ScopeConfig>;
683
+ } = {},
684
+ mcpServerNames: readonly string[] = [],
685
+ ): PolicyLoader {
686
+ const issues: string[] = [];
687
+ return {
688
+ loadGlobalConfig: () => scopes.global ?? {},
689
+ loadProjectConfig: () => scopes.project ?? {},
690
+ loadAgentConfig: (name?: string) => (name && scopes.agent?.[name]) || {},
691
+ loadProjectAgentConfig: (name?: string) =>
692
+ (name && scopes.projectAgent?.[name]) || {},
693
+ getConfiguredMcpServerNames: () => mcpServerNames,
694
+ getCacheStamp: () => "in-memory",
695
+ getConfigIssues: () => issues,
696
+ getResolvedPolicyPaths: (): ResolvedPolicyPaths => ({
697
+ globalConfigPath: "/in-memory/config.json",
698
+ globalConfigExists: true,
699
+ projectConfigPath: null,
700
+ projectConfigExists: false,
701
+ agentsDir: "/in-memory/agents",
702
+ agentsDirExists: false,
703
+ projectAgentsDir: null,
704
+ projectAgentsDirExists: false,
705
+ }),
706
+ };
707
+ }
708
+
709
+ /** Create a PermissionManager backed by an in-memory PolicyLoader. */
710
+ function makeInMemoryManager(
711
+ scopes: Parameters<typeof createInMemoryPolicyLoader>[0] = {},
712
+ mcpServerNames: readonly string[] = [],
713
+ ): PermissionManager {
714
+ return new PermissionManager({
715
+ policyLoader: createInMemoryPolicyLoader(scopes, mcpServerNames),
716
+ });
717
+ }
718
+
719
+ describe("PermissionManager with in-memory PolicyLoader", () => {
720
+ describe("universal fallback", () => {
721
+ it("defaults to ask when no config is provided", () => {
722
+ const manager = makeInMemoryManager();
723
+ const result = manager.checkPermission("read", {});
724
+ expect(result.state).toBe("ask");
725
+ expect(result.origin).toBe("builtin");
726
+ });
727
+
728
+ it("respects permission['*'] = 'allow' from global config", () => {
729
+ const manager = makeInMemoryManager({
730
+ global: { permission: { "*": "allow" } },
731
+ });
732
+ const result = manager.checkPermission("read", {});
733
+ expect(result.state).toBe("allow");
734
+ expect(result.origin).toBe("global");
735
+ });
736
+
737
+ it("respects permission['*'] = 'deny' from global config", () => {
738
+ const manager = makeInMemoryManager({
739
+ global: { permission: { "*": "deny" } },
740
+ });
741
+ const result = manager.checkPermission("write", {});
742
+ expect(result.state).toBe("deny");
743
+ });
744
+ });
745
+
746
+ describe("surface routing", () => {
747
+ it("bash surface routes correctly", () => {
748
+ const manager = makeInMemoryManager({
749
+ global: {
750
+ permission: { "*": "ask", bash: { "git *": "allow" } },
751
+ },
752
+ });
753
+ const result = manager.checkPermission("bash", {
754
+ command: "git status",
755
+ });
756
+ expect(result.state).toBe("allow");
757
+ expect(result.source).toBe("bash");
758
+ expect(result.matchedPattern).toBe("git *");
759
+ });
760
+
761
+ it("tool surface routes correctly for built-in tools", () => {
762
+ const manager = makeInMemoryManager({
763
+ global: { permission: { "*": "deny", read: "allow" } },
764
+ });
765
+ const result = manager.checkPermission("read", {});
766
+ expect(result.state).toBe("allow");
767
+ expect(result.source).toBe("tool");
768
+ });
769
+
770
+ it("skill surface routes correctly", () => {
771
+ const manager = makeInMemoryManager({
772
+ global: {
773
+ permission: { "*": "ask", skill: { librarian: "allow" } },
774
+ },
775
+ });
776
+ const result = manager.checkPermission("skill", { name: "librarian" });
777
+ expect(result.state).toBe("allow");
778
+ expect(result.source).toBe("skill");
779
+ });
780
+
781
+ it("mcp surface routes correctly", () => {
782
+ const manager = makeInMemoryManager(
783
+ {
784
+ global: {
785
+ permission: { "*": "ask", mcp: { exa_search: "allow" } },
786
+ },
787
+ },
788
+ ["exa"],
789
+ );
790
+ const result = manager.checkPermission("mcp", {
791
+ tool: "exa:search",
792
+ server: "exa",
793
+ });
794
+ expect(result.state).toBe("allow");
795
+ expect(result.source).toBe("mcp");
796
+ });
797
+
798
+ it("external_directory surface routes correctly", () => {
799
+ const manager = makeInMemoryManager({
800
+ global: {
801
+ permission: {
802
+ "*": "ask",
803
+ external_directory: { "/trusted/*": "allow" },
804
+ },
805
+ },
806
+ });
807
+ const result = manager.checkPermission("external_directory", {
808
+ path: "/trusted/repo",
809
+ });
810
+ expect(result.state).toBe("allow");
811
+ expect(result.source).toBe("special");
812
+ });
813
+
814
+ it("extension tools use 'default' source when no config rule matches", () => {
815
+ const manager = makeInMemoryManager({
816
+ global: { permission: { "*": "ask" } },
817
+ });
818
+ const result = manager.checkPermission("my_custom_tool", {});
819
+ expect(result.state).toBe("ask");
820
+ expect(result.source).toBe("default");
821
+ });
822
+ });
823
+
824
+ describe("multi-scope merge", () => {
825
+ it("project overrides global", () => {
826
+ const manager = makeInMemoryManager({
827
+ global: { permission: { read: "ask" } },
828
+ project: { permission: { read: "allow" } },
829
+ });
830
+ const result = manager.checkPermission("read", {});
831
+ expect(result.state).toBe("allow");
832
+ expect(result.origin).toBe("project");
833
+ });
834
+
835
+ it("agent overrides project", () => {
836
+ const manager = makeInMemoryManager({
837
+ global: { permission: { read: "ask" } },
838
+ project: { permission: { read: "allow" } },
839
+ agent: { coder: { permission: { read: "deny" } } },
840
+ });
841
+ const result = manager.checkPermission("read", {}, "coder");
842
+ expect(result.state).toBe("deny");
843
+ expect(result.origin).toBe("agent");
844
+ });
845
+
846
+ it("project-agent overrides agent", () => {
847
+ const manager = makeInMemoryManager({
848
+ global: { permission: { read: "deny" } },
849
+ agent: { coder: { permission: { read: "deny" } } },
850
+ projectAgent: { coder: { permission: { read: "allow" } } },
851
+ });
852
+ const result = manager.checkPermission("read", {}, "coder");
853
+ expect(result.state).toBe("allow");
854
+ expect(result.origin).toBe("project-agent");
855
+ });
856
+
857
+ it("deep-shallow merge preserves patterns from different scopes", () => {
858
+ const manager = makeInMemoryManager({
859
+ global: { permission: { bash: { "git *": "allow" } } },
860
+ project: { permission: { bash: { "rm *": "deny" } } },
861
+ });
862
+ const gitResult = manager.checkPermission("bash", {
863
+ command: "git status",
864
+ });
865
+ expect(gitResult.state).toBe("allow");
866
+ expect(gitResult.origin).toBe("global");
867
+
868
+ const rmResult = manager.checkPermission("bash", {
869
+ command: "rm -rf /",
870
+ });
871
+ expect(rmResult.state).toBe("deny");
872
+ expect(rmResult.origin).toBe("project");
873
+ });
874
+
875
+ it("string replaces object in override scope", () => {
876
+ const manager = makeInMemoryManager({
877
+ global: {
878
+ permission: { bash: { "git *": "ask", "npm *": "ask" } },
879
+ },
880
+ project: { permission: { bash: "allow" } },
881
+ });
882
+ const result = manager.checkPermission("bash", { command: "anything" });
883
+ expect(result.state).toBe("allow");
884
+ expect(result.origin).toBe("project");
885
+ });
886
+ });
887
+
888
+ describe("session rule composition", () => {
889
+ it("session rule wins over config", () => {
890
+ const manager = makeInMemoryManager({
891
+ global: { permission: { "*": "deny" } },
892
+ });
893
+ const sessionRules: Ruleset = [sessionAllow("read", "*")];
894
+ const result = manager.checkPermission(
895
+ "read",
896
+ {},
897
+ undefined,
898
+ sessionRules,
899
+ );
900
+ expect(result.state).toBe("allow");
901
+ expect(result.source).toBe("session");
902
+ });
903
+
904
+ it("session rule does not bleed across surfaces", () => {
905
+ const manager = makeInMemoryManager({
906
+ global: { permission: { "*": "ask" } },
907
+ });
908
+ const sessionRules: Ruleset = [sessionAllow("bash", "git *")];
909
+ const bashResult = manager.checkPermission(
910
+ "bash",
911
+ { command: "git status" },
912
+ undefined,
913
+ sessionRules,
914
+ );
915
+ expect(bashResult.state).toBe("allow");
916
+
917
+ const readResult = manager.checkPermission(
918
+ "read",
919
+ {},
920
+ undefined,
921
+ sessionRules,
922
+ );
923
+ expect(readResult.state).toBe("ask");
924
+ });
925
+ });
926
+
927
+ describe("origin tracking", () => {
928
+ it("universal fallback from project carries origin 'project'", () => {
929
+ const manager = makeInMemoryManager({
930
+ global: { permission: { "*": "ask" } },
931
+ project: { permission: { "*": "allow" } },
932
+ });
933
+ const result = manager.checkPermission("read", {});
934
+ expect(result.state).toBe("allow");
935
+ expect(result.origin).toBe("project");
936
+ });
937
+
938
+ it("session origin is 'session'", () => {
939
+ const manager = makeInMemoryManager();
940
+ const sessionRules: Ruleset = [sessionAllow("read", "*")];
941
+ const result = manager.checkPermission(
942
+ "read",
943
+ {},
944
+ undefined,
945
+ sessionRules,
946
+ );
947
+ expect(result.origin).toBe("session");
948
+ });
949
+ });
950
+
951
+ describe("getToolPermission", () => {
952
+ it("returns tool-level state for built-in tools", () => {
953
+ const manager = makeInMemoryManager({
954
+ global: { permission: { "*": "deny", read: "allow" } },
955
+ });
956
+ expect(manager.getToolPermission("read")).toBe("allow");
957
+ expect(manager.getToolPermission("write")).toBe("deny");
958
+ });
959
+
960
+ it("returns tool-level state for bash surface", () => {
961
+ const manager = makeInMemoryManager({
962
+ global: { permission: { "*": "deny", bash: "allow" } },
963
+ });
964
+ expect(manager.getToolPermission("bash")).toBe("allow");
965
+ });
966
+ });
967
+
968
+ describe("getComposedConfigRules", () => {
969
+ it("returns only config-layer rules", () => {
970
+ const manager = makeInMemoryManager({
971
+ global: {
972
+ permission: { "*": "ask", bash: { "git *": "allow" } },
973
+ },
974
+ });
975
+ const rules = manager.getComposedConfigRules();
976
+ expect(rules.every((r) => r.layer === "config")).toBe(true);
977
+ expect(
978
+ rules.some((r) => r.surface === "bash" && r.pattern === "git *"),
979
+ ).toBe(true);
980
+ });
981
+ });
982
+ });