principles 0.0.5 → 0.0.6

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 (152) hide show
  1. checksums.yaml +4 -4
  2. data/lib/principles/after_commit_hook_options.rb +4 -1
  3. data/lib/principles/command_line.rb +12 -28
  4. data/lib/principles/command_routes.rb +7 -1
  5. data/lib/principles/controllers/acknowledgement.rb +36 -26
  6. data/lib/principles/controllers/answer.rb +7 -5
  7. data/lib/principles/controllers/assessment.rb +14 -3
  8. data/lib/principles/controllers/audit_completion.rb +3 -1
  9. data/lib/principles/controllers/command.rb +2 -2
  10. data/lib/principles/controllers/correction.rb +3 -1
  11. data/lib/principles/controllers/full_audit_request.rb +1 -1
  12. data/lib/principles/controllers/guidance_search_paths.rb +31 -0
  13. data/lib/principles/controllers/inventory.rb +3 -22
  14. data/lib/principles/controllers/mapping.rb +9 -1
  15. data/lib/principles/controllers/member_addition.rb +65 -0
  16. data/lib/principles/controllers/member_entries.rb +6 -24
  17. data/lib/principles/controllers/member_problems_report.rb +2 -2
  18. data/lib/principles/controllers/members_to_assess.rb +1 -1
  19. data/lib/principles/controllers/meta_principle_setup.rb +1 -1
  20. data/lib/principles/controllers/repoint.rb +5 -2
  21. data/lib/principles/controllers/survey.rb +60 -0
  22. data/lib/principles/data/audit_files.rb +4 -0
  23. data/lib/principles/data/candidate_files_found.rb +48 -13
  24. data/lib/principles/data/command-steps.json +77 -16
  25. data/lib/principles/data/committed_tree.rb +84 -0
  26. data/lib/principles/data/first_parent_log.rb +48 -0
  27. data/lib/principles/data/first_parent_range.rb +122 -0
  28. data/lib/principles/data/git_command.rb +37 -0
  29. data/lib/principles/data/guidance_search_paths_file.rb +107 -0
  30. data/lib/principles/data/map_files.rb +35 -10
  31. data/lib/principles/data/principles_directory.rb +40 -0
  32. data/lib/principles/data/principles_subfolder.rb +39 -16
  33. data/lib/principles/data/project_files.rb +11 -3
  34. data/lib/principles/data/repository.rb +55 -29
  35. data/lib/principles/data/requested_candidate_files.rb +43 -0
  36. data/lib/principles/data/running_version.rb +3 -5
  37. data/lib/principles/data/schemas/acknowledgement-answer.schema.json +4 -5
  38. data/lib/principles/data/schemas/answer-guide.schema.json +255 -63
  39. data/lib/principles/data/schemas/command-steps.schema.json +1 -1
  40. data/lib/principles/data/schemas/correction-answer-guide.schema.json +2 -1
  41. data/lib/principles/data/schemas/correction-answer.schema.json +6 -0
  42. data/lib/principles/data/schemas/guidance-search-paths.schema.json +13 -0
  43. data/lib/principles/data/schemas/member-map-history.schema.json +20 -0
  44. data/lib/principles/data/verified_head.rb +162 -0
  45. data/lib/principles/invocation_options.rb +2 -2
  46. data/lib/principles/models/acknowledgement.rb +28 -6
  47. data/lib/principles/models/acknowledgement_left_as_printed.rb +51 -0
  48. data/lib/principles/models/audit_entry_protecting_map.rb +28 -0
  49. data/lib/principles/models/audit_record.rb +4 -0
  50. data/lib/principles/models/audit_records.rb +34 -8
  51. data/lib/principles/models/candidate_files.rb +17 -6
  52. data/lib/principles/models/candidate_inventory.rb +51 -7
  53. data/lib/principles/models/captured_head.rb +72 -0
  54. data/lib/principles/models/check_since_last_audit.rb +4 -8
  55. data/lib/principles/models/declared_map_changes.rb +25 -9
  56. data/lib/principles/models/declared_member_removals.rb +75 -0
  57. data/lib/principles/models/disposition.rb +4 -2
  58. data/lib/principles/models/fix_only_fields.rb +12 -0
  59. data/lib/principles/models/guidance_search_path.rb +26 -0
  60. data/lib/principles/models/guidance_search_paths.rb +57 -0
  61. data/lib/principles/models/line_span.rb +6 -0
  62. data/lib/principles/models/loaded_files.rb +1 -2
  63. data/lib/principles/models/loading_scope.rb +61 -0
  64. data/lib/principles/models/locator.rb +6 -0
  65. data/lib/principles/models/mapped_principles.rb +39 -37
  66. data/lib/principles/models/member_file.rb +20 -71
  67. data/lib/principles/models/member_history.rb +7 -5
  68. data/lib/principles/models/member_loading_scopes.rb +49 -0
  69. data/lib/principles/models/member_loads.rb +4 -0
  70. data/lib/principles/models/member_mapping_run.rb +30 -0
  71. data/lib/principles/models/member_moves.rb +4 -0
  72. data/lib/principles/models/member_problems.rb +48 -24
  73. data/lib/principles/models/member_region.rb +129 -0
  74. data/lib/principles/models/member_repair_moves.rb +9 -11
  75. data/lib/principles/models/member_snapshots.rb +14 -1
  76. data/lib/principles/models/members_in_consecutive_sections.rb +56 -0
  77. data/lib/principles/models/meta_principle.rb +14 -0
  78. data/lib/principles/models/missing_files.rb +2 -2
  79. data/lib/principles/models/other_templates_placeholders.rb +1 -1
  80. data/lib/principles/models/outline_budget.rb +29 -14
  81. data/lib/principles/models/principle.rb +14 -7
  82. data/lib/principles/models/principle_file_revision.rb +48 -0
  83. data/lib/principles/models/principle_history.rb +46 -19
  84. data/lib/principles/models/principle_lineage.rb +118 -0
  85. data/lib/principles/models/principle_map_revision.rb +39 -0
  86. data/lib/principles/models/principle_mapping_history.rb +132 -0
  87. data/lib/principles/models/principle_members.rb +14 -0
  88. data/lib/principles/models/principle_predecessor.rb +45 -0
  89. data/lib/principles/models/principle_publication.rb +177 -0
  90. data/lib/principles/models/principle_revision.rb +104 -0
  91. data/lib/principles/models/problem_report.rb +17 -5
  92. data/lib/principles/models/project.rb +43 -27
  93. data/lib/principles/models/project_history.rb +45 -25
  94. data/lib/principles/models/record_format.rb +6 -0
  95. data/lib/principles/models/reported_check.rb +2 -1
  96. data/lib/principles/models/sent_answer.rb +10 -0
  97. data/lib/principles/models/survey_search_paths.rb +26 -0
  98. data/lib/principles/models/uncommitted_files.rb +50 -0
  99. data/lib/principles/models/unknown_principle.rb +29 -0
  100. data/lib/principles/positional_arguments.rb +37 -0
  101. data/lib/principles/version.rb +2 -2
  102. data/lib/principles/views/acknowledgement_result.rb +16 -12
  103. data/lib/principles/views/added_member.rb +41 -0
  104. data/lib/principles/views/answered_request.rb +21 -0
  105. data/lib/principles/views/assessment_request.rb +2 -1
  106. data/lib/principles/views/audit_result.rb +21 -26
  107. data/lib/principles/views/candidate_files_result.rb +56 -12
  108. data/lib/principles/views/candidate_outlines_result.rb +25 -0
  109. data/lib/principles/views/consecutive_sections_elsewhere.rb +27 -0
  110. data/lib/principles/views/correction_request.rb +32 -1
  111. data/lib/principles/views/guidance_search_paths_result.rb +26 -0
  112. data/lib/principles/views/json_with_entries_on_lines.rb +12 -4
  113. data/lib/principles/views/map_added_to.rb +56 -2
  114. data/lib/principles/views/map_result.rb +22 -42
  115. data/lib/principles/views/maps_needing_members_or_audit.rb +47 -0
  116. data/lib/principles/views/member_change_since_baseline.rb +2 -1
  117. data/lib/principles/views/member_repairs.rb +2 -1
  118. data/lib/principles/views/new_loading_scope.rb +31 -0
  119. data/lib/principles/views/one_member_check_result_template.rb +25 -21
  120. data/lib/principles/views/principle_read_result.rb +4 -1
  121. data/lib/principles/views/principles_with_no_completed_audit.rb +9 -3
  122. data/lib/principles/views/problem_lines.rb +3 -1
  123. data/lib/principles/views/problem_result.rb +8 -3
  124. data/lib/principles/views/prompts/acknowledge-a-change/run-the-check-and-report-it-passing.md +3 -3
  125. data/lib/principles/views/prompts/acknowledge-a-change/say-whether-the-change-keeps-the-meaning.md +6 -6
  126. data/lib/principles/views/prompts/assess-every-member/answer-guide.json +67 -43
  127. data/lib/principles/views/prompts/dispose-of-each-finding/answer-guide.json +1 -0
  128. data/lib/principles/views/prompts/dispose-of-each-finding/fix-or-record-each-finding.md +12 -0
  129. data/lib/principles/views/prompts/dispose-of-each-finding/retain-assessment-and-correction.md +4 -0
  130. data/lib/principles/views/prompts/find-a-principle-to-map/ask-about-other-guidance-search-paths.md +6 -0
  131. data/lib/principles/views/prompts/find-a-principle-to-map/choose-an-existing-or-new-principle.md +2 -0
  132. data/lib/principles/views/prompts/find-a-principle-to-map/choose-one-principle.md +4 -2
  133. data/lib/principles/views/prompts/find-a-principle-to-map/compare-with-mapped-principles.md +11 -0
  134. data/lib/principles/views/prompts/find-a-principle-to-map/read-left-out-files.md +2 -2
  135. data/lib/principles/views/prompts/find-a-principle-to-map/survey-a-later-range.md +3 -0
  136. data/lib/principles/views/prompts/find-a-principle-to-map/what-each-command-writes.md +4 -4
  137. data/lib/principles/views/prompts/find-a-principle-to-map/where-the-inventory-looks.md +5 -4
  138. data/lib/principles/views/prompts/learn-what-a-command-does/add-guidance-search-path.md +17 -0
  139. data/lib/principles/views/prompts/learn-what-a-command-does/add-member.md +35 -6
  140. data/lib/principles/views/prompts/learn-what-a-command-does/create-principle.md +16 -0
  141. data/lib/principles/views/prompts/learn-what-a-command-does/list-candidate-files.md +37 -4
  142. data/lib/principles/views/prompts/learn-what-a-command-does/list-member-problems.md +5 -4
  143. data/lib/principles/views/prompts/learn-what-a-command-does/move-member.md +5 -0
  144. data/lib/principles/views/prompts/learn-what-a-command-does/remove-guidance-search-path.md +13 -0
  145. data/lib/principles/views/prompts/learn-what-a-command-does/remove-principle.md +25 -0
  146. data/lib/principles/views/prompts/learn-what-a-command-does/show-answer-check.md +26 -0
  147. data/lib/principles/views/prompts/learn-what-a-command-does/show-candidate-outlines.md +21 -0
  148. data/lib/principles/views/prompts/learn-what-a-command-does/start-audit.md +2 -2
  149. data/lib/principles/views/prompts/learn-what-a-command-does/submit-answer.md +51 -54
  150. data/lib/principles/views/prompts/read-requested-outlines/continue-the-survey.md +7 -0
  151. metadata +55 -4
  152. data/lib/principles/models/candidate_listing.rb +0 -41
@@ -0,0 +1,122 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "../error"
5
+ require_relative "git_command"
6
+
7
+ module Principles
8
+ # Every file and process this tool reads or writes. data/CLAUDE.md owns the boundary.
9
+ module Data
10
+ # Captured head's first-parent span, bounded by a supplied base or an independently verified actual root.
11
+ class FirstParentRange
12
+ SHA = /\A\h{40}(?:\h{24})?\z/ #: Regexp
13
+
14
+ #: (GitCommand, String?, String) -> void
15
+ def initialize(git, base, head)
16
+ @git = git #: GitCommand
17
+ @base = base&.dup&.freeze #: String?
18
+ @head = head.dup.freeze #: String
19
+ @first_parents = nil #: Array[String]?
20
+ end
21
+
22
+ #: (Array[String]) -> Array[String]
23
+ def touching(paths)
24
+ [@head, *changes(paths), @base].compact.uniq.freeze
25
+ end
26
+
27
+ #: (Array[String]) -> Array[String]
28
+ def changes(paths)
29
+ chain = first_parents
30
+ selected = read(["rev-list", "--first-parent", "--full-history", "--end-of-options", range, "--", *paths])
31
+ .lines.map(&:strip)
32
+ raise unavailable unless selected.all? { |sha| SHA.match?(sha) && chain.include?(sha) }
33
+
34
+ selected.each(&:freeze).freeze
35
+ end
36
+
37
+ #: () -> Array[String]
38
+ def commits = [*first_parents, @base].compact.uniq.freeze
39
+
40
+ private
41
+
42
+ # No path filter or history simplification may rewrite the topology used to certify this range.
43
+ #: () -> Array[String]
44
+ def first_parents
45
+ @first_parents ||= read_parents
46
+ end
47
+
48
+ #: () -> Array[String]
49
+ def read_parents
50
+ confirm_revisions
51
+ rows = read(["rev-list", "--first-parent", "--parents", "--end-of-options", range, "--"])
52
+ .lines.map(&:split)
53
+ return [] if rows.empty? && @head == @base
54
+
55
+ chain = certified_chain(rows)
56
+ confirm_root(chain.last.to_s) unless @base
57
+ chain.each(&:freeze).freeze
58
+ end
59
+
60
+ # Only full commit names enter either native revision expression; nil requests the actual-root proof.
61
+ #: () -> void
62
+ def confirm_revisions
63
+ raise unavailable unless SHA.match?(@head)
64
+ raise unavailable if @base && !SHA.match?(@base)
65
+ end
66
+
67
+ #: (Array[Array[String]]) -> Array[String]
68
+ def certified_chain(rows)
69
+ shas = rows.map(&:first).compact
70
+ parents = rows.map { |row| row[1] }
71
+ connected = shas.first == @head && parents == [*shas.drop(1), @base]
72
+ raise unavailable unless connected && rows.flatten.all? { SHA.match?(it) }
73
+
74
+ shas
75
+ end
76
+
77
+ # rev-list hides a shallow boundary's parents. The raw commit must independently be an actual root.
78
+ #: (String) -> void
79
+ def confirm_root(sha)
80
+ header = root_header(sha)
81
+ kind, tree = header.first.to_s.split(" ", 2)
82
+ return if kind == "tree" && SHA.match?(tree.to_s) && header.none? { it.start_with?("parent ") }
83
+
84
+ raise unavailable
85
+ end
86
+
87
+ #: (String) -> Array[String]
88
+ def root_header(sha)
89
+ read(["cat-file", "-p", sha]).b.split("\n\n", 2).first.to_s.lines.map(&:chomp)
90
+ end
91
+
92
+ #: () -> String
93
+ def range = @base ? "#{@base}..#{@head}" : @head
94
+
95
+ #: (Array[String]) -> String
96
+ def read(arguments)
97
+ output, status = @git.run_with_status(arguments)
98
+ raise unavailable unless status&.zero?
99
+
100
+ output
101
+ end
102
+
103
+ #: () -> Error
104
+ def unavailable
105
+ return incomplete_root unless @base
106
+
107
+ Error.new("git_history_unavailable",
108
+ "The supplied base has no verified first-parent path to selected commit #{@head}",
109
+ "Restore complete Git history and access, including the request's base commit, or start a new " \
110
+ "audit from the current branch. No audit record was written.")
111
+ end
112
+
113
+ #: () -> Error
114
+ def incomplete_root
115
+ Error.new("git_history_unavailable",
116
+ "Selected commit #{@head} lacks a verified first-parent line to its actual root",
117
+ "Restore complete first-parent ancestry and access to its commit and tree objects, then retry. " \
118
+ "No audit record was written.")
119
+ end
120
+ end
121
+ end
122
+ end
@@ -0,0 +1,37 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ module Principles
5
+ # Every file and process this tool reads or writes. data/CLAUDE.md owns the boundary.
6
+ module Data
7
+ # git run in one directory, with its own messages kept off this tool's output: its output and whether it
8
+ # succeeded, and an empty failure where git cannot be started.
9
+ class GitCommand
10
+ # git reads each path this tool passes as itself, never as a glob or a :(magic) pathspec, so a member's
11
+ # file name cannot take in other files.
12
+ LITERAL = { "GIT_LITERAL_PATHSPECS" => "1" }.freeze #: Hash[String, String]
13
+
14
+ #: (String) -> void
15
+ def initialize(directory)
16
+ @directory = directory
17
+ freeze
18
+ end
19
+
20
+ # Legacy readers retain their success/failure pair; permission checks can inspect the exact status.
21
+ #: (Array[String]) -> [String, bool]
22
+ def run(arguments)
23
+ output, status = run_with_status(arguments)
24
+ [output, status&.zero? || false]
25
+ end
26
+
27
+ # nil means no numeric exit code, as a launch failure or termination by signal.
28
+ #: (Array[String]) -> [String, Integer?]
29
+ def run_with_status(arguments)
30
+ output = IO.popen(LITERAL, ["git", "-C", @directory, *arguments], err: File::NULL, &:read)
31
+ [output, Process.last_status&.exitstatus]
32
+ rescue SystemCallError
33
+ ["", nil]
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,107 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "principles_directory"
5
+ require_relative "bounded_file"
6
+ require_relative "json_text"
7
+ require_relative "schema_check"
8
+ require "fileutils"
9
+ require "tempfile"
10
+
11
+ module Principles
12
+ # Every file and process this tool reads or writes. data/CLAUDE.md owns the boundary.
13
+ module Data
14
+ # The project's one committed list of additional guidance globs.
15
+ class GuidanceSearchPathsFile
16
+ NAME = "guidance-search-paths.json"
17
+ FILE = "#{PrinciplesDirectory::FOLDER}/#{NAME}".freeze #: String
18
+ FORMAT = 1
19
+
20
+ #: (PrinciplesDirectory) -> void
21
+ def initialize(directory)
22
+ @directory = directory
23
+ end
24
+
25
+ #: () -> Hash[String, untyped]
26
+ def read
27
+ text = optional_text
28
+ text ? parse(text) : { "format" => FORMAT, "globs" => [] }
29
+ rescue SystemCallError => e
30
+ raise io_error("read", e)
31
+ end
32
+
33
+ #: (Hash[String, untyped]) -> void
34
+ def write(fields)
35
+ target = path
36
+ FileUtils.mkdir_p(@directory.path)
37
+ Tempfile.create(["guidance-search-paths", ".json"], @directory.path) do |temporary|
38
+ temporary.write("#{JSON.pretty_generate(fields)}\n")
39
+ temporary.flush
40
+ File.rename(temporary.path, target)
41
+ end
42
+ rescue SystemCallError => e
43
+ raise io_error("write", e)
44
+ end
45
+
46
+ #: () -> String
47
+ def relative = FILE
48
+
49
+ # Schema validation is shared with the in-memory adapter; filesystem reads remain here.
50
+ #: (String) -> Hash[String, untyped]
51
+ def parse(text)
52
+ parsed = JsonText.new(text).parsed
53
+ refuse_newer(parsed)
54
+ problems = SchemaCheck.new("guidance-search-paths").problems(parsed)
55
+ return parsed if problems.empty?
56
+
57
+ raise Error.new("invalid_guidance_search_paths", "#{FILE} is not in the shape this tool writes",
58
+ "Repair the listed fields or restore the configuration from version control.", FILE,
59
+ fields: problems)
60
+ rescue JSON::ParserError => e
61
+ raise Error.new("unparseable_guidance_search_paths", e.message,
62
+ "Repair this JSON or restore the configuration from version control.", FILE)
63
+ end
64
+
65
+ private
66
+
67
+ # Only a genuinely absent file is optional, never an unreadable store or a linked file.
68
+ #: () -> String?
69
+ def optional_text
70
+ target = path
71
+ unless File.lstat(target).file?
72
+ raise Error.new("invalid_guidance_search_paths", "#{FILE} is not a regular configuration file",
73
+ "Replace it with a regular JSON configuration file, then retry.", FILE)
74
+ end
75
+ BoundedFile.new(target, shown_as: FILE).read
76
+ rescue Errno::ENOENT
77
+ nil
78
+ end
79
+
80
+ #: () -> String
81
+ def path
82
+ target = File.join(@directory.path, NAME)
83
+ if File.symlink?(target)
84
+ raise Error.new("symlinked_guidance_search_paths", "#{FILE} is a symbolic link",
85
+ "Replace the link with a regular committed configuration file.", FILE)
86
+ end
87
+
88
+ target
89
+ end
90
+
91
+ #: (untyped) -> void
92
+ def refuse_newer(parsed)
93
+ format = parsed.is_a?(Hash) ? parsed["format"] : nil
94
+ return unless format.is_a?(Integer) && format > FORMAT
95
+
96
+ raise Error.new("newer_guidance_search_paths", "#{FILE} uses format #{format}; this version reads #{FORMAT}",
97
+ "Update principles and leave the configuration unchanged; it is not broken.", FILE)
98
+ end
99
+
100
+ #: (String, SystemCallError) -> Error
101
+ def io_error(action, error)
102
+ Error.new("guidance_search_paths_io_error", "#{FILE}: cannot #{action}: #{error.class.new(nil).message}",
103
+ "Restore a readable project-local store and file, then retry; no configuration was accepted.", FILE)
104
+ end
105
+ end
106
+ end
107
+ end
@@ -14,7 +14,7 @@ module Principles
14
14
  # .principles/maps/, committed in the audited repository. A map holds a description and its members,
15
15
  # each a project file, optionally narrowed to one Markdown section, with the role it plays.
16
16
  class MapFiles
17
- # The newest map format this version reads. Models::Principle says which member fields need which
17
+ # The newest map format ordinary current-map reads accept. Models::Principle says which member fields need which
18
18
  # format, and a spec ties the two together.
19
19
  NEWEST_FORMAT = 2
20
20
 
@@ -49,17 +49,17 @@ module Principles
49
49
  # The map id from text read elsewhere, such as the map's file at commit, checked against its schema.
50
50
  #: (String, String, ?commit: String?) -> Hash[String, untyped]
51
51
  def parse(id, text, commit: nil)
52
- map = JsonText.new(text).parsed
52
+ map = parsed_fields(id, text)
53
53
  refuse_newer(id, map, commit)
54
- problems = SchemaCheck.new("member-map").problems(map)
55
- return map if problems.empty?
54
+ checked_map(id, map, commit)
55
+ end
56
56
 
57
- raise Error.new("invalid_map", "#{where(id, commit)} is not in the shape this tool writes",
58
- "Repair each field listed, or restore the map from version control.", relative(id),
59
- fields: problems)
60
- rescue JSON::ParserError => e
61
- raise Error.new("unparseable_map", e.message, "Repair this map file or restore it from version control.",
62
- relative(id))
57
+ # Historical link interpretation is separate from the public current-map format ceiling.
58
+ #: (String, String, commit: String?) -> Hash[String, untyped]
59
+ def parse_history(id, text, commit:)
60
+ map = parsed_fields(id, text)
61
+ refuse_newer(id, map, commit) unless linked_format?(map)
62
+ checked_map(id, map, commit)
63
63
  end
64
64
 
65
65
  #: (String, Hash[String, untyped]) -> void
@@ -67,6 +67,9 @@ module Principles
67
67
  @directory.write(id, "#{JSON.pretty_generate(map)}\n")
68
68
  end
69
69
 
70
+ #: (String) -> void
71
+ def remove(id) = @directory.remove(id, shown_as: relative(id))
72
+
70
73
  #: (String) -> String
71
74
  def relative(id)
72
75
  File.join(PrinciplesSubfolder::FOLDER, "maps", "#{id}.json")
@@ -80,6 +83,28 @@ module Principles
80
83
 
81
84
  private
82
85
 
86
+ #: (String, String) -> untyped
87
+ def parsed_fields(id, text)
88
+ JsonText.new(text).parsed
89
+ rescue JSON::ParserError => e
90
+ raise Error.new("unparseable_map", e.message, "Repair this map file or restore it from version control.",
91
+ relative(id))
92
+ end
93
+
94
+ #: (untyped) -> bool
95
+ def linked_format?(map) = map.is_a?(Hash) && map["format"].eql?(3)
96
+
97
+ #: (String, untyped, String?) -> Hash[String, untyped]
98
+ def checked_map(id, map, commit)
99
+ problems = SchemaCheck.new("member-map").problems(map)
100
+ problems += SchemaCheck.new("member-map-history").problems(map) if linked_format?(map)
101
+ return map if problems.empty?
102
+
103
+ raise Error.new("invalid_map", "#{where(id, commit)} is not in the shape this tool writes",
104
+ "Repair each field listed, or restore the map from version control.", relative(id),
105
+ fields: problems)
106
+ end
107
+
83
108
  # Refuses a map a newer version wrote, before its schema is checked: its fields may be ones this
84
109
  # version cannot read without losing what they say, and the agent must not be told to repair or
85
110
  # restore a map that is not broken.
@@ -0,0 +1,40 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "../error"
5
+
6
+ module Principles
7
+ # Every file and process this tool reads or writes. data/CLAUDE.md owns the boundary.
8
+ module Data
9
+ # A directory this project keeps Principles records in, refusing linked stores before any read or write.
10
+ class PrinciplesDirectory
11
+ FOLDER = ".principles"
12
+
13
+ #: (String, ?String) -> void
14
+ def initialize(root, name = "")
15
+ @root = root
16
+ @name = name
17
+ freeze
18
+ end
19
+
20
+ #: () -> String
21
+ def path
22
+ [FOLDER, File.join(FOLDER, @name)].each { |relative| refuse_link(relative) }
23
+ File.join(@root, FOLDER, @name)
24
+ end
25
+
26
+ private
27
+
28
+ #: (String) -> void
29
+ def refuse_link(relative)
30
+ return unless File.symlink?(File.join(@root, relative))
31
+
32
+ raise Error.new("symlinked_directory",
33
+ "#{relative} is a symbolic link, so a read or write there would leave the " \
34
+ "project",
35
+ "Replace the link with a real directory, then restore its files from version control.",
36
+ relative)
37
+ end
38
+ end
39
+ end
40
+ end
@@ -6,6 +6,7 @@ require "tempfile"
6
6
  require_relative "../error"
7
7
  require_relative "principle_id"
8
8
  require_relative "bounded_file"
9
+ require_relative "principles_directory"
9
10
 
10
11
  module Principles
11
12
  # Every file and process this tool reads or writes. data/CLAUDE.md owns the boundary.
@@ -16,7 +17,7 @@ module Principles
16
17
  # writes outside the project, so each use refuses one.
17
18
  class PrinciplesSubfolder
18
19
  # The folder a project keeps its maps and audit records in.
19
- FOLDER = ".principles"
20
+ FOLDER = PrinciplesDirectory::FOLDER
20
21
  # Its name before the rename to principles; a project an older version mapped still holds it (#536).
21
22
  EARLIER_FOLDER = ".memeplex"
22
23
 
@@ -29,10 +30,7 @@ module Principles
29
30
 
30
31
  # The directory's path, once neither it nor FOLDER is a symbolic link.
31
32
  #: () -> String
32
- def path
33
- [FOLDER, File.join(FOLDER, @name)].each { |relative| refuse_link(relative) }
34
- File.join(@root, FOLDER, @name)
35
- end
33
+ def path = PrinciplesDirectory.new(@root, @name).path
36
34
 
37
35
  # Which of FOLDER and EARLIER_FOLDER hold this subfolder, in that order, so a model can refuse a project
38
36
  # whose maps or records have not moved. A file an older version left beside no subfolder holds none.
@@ -58,6 +56,30 @@ module Principles
58
56
  File.exist?(file(id))
59
57
  end
60
58
 
59
+ # Unlike exist?, a dangling final link is still a current entry; permission failures are not absence.
60
+ #: (String, shown_as: String) -> bool
61
+ def entry?(id, shown_as:)
62
+ File.lstat(file(id))
63
+ true
64
+ rescue Errno::ENOENT
65
+ false
66
+ rescue SystemCallError
67
+ raise Error.new("store_entry_unreadable", "Cannot inspect #{shown_as}",
68
+ "Restore access to this entry and its parent directories, then run the command again.",
69
+ shown_as)
70
+ end
71
+
72
+ # Unlink only a regular final entry, rechecking the store parents before the unlink.
73
+ #: (String, shown_as: String) -> void
74
+ def remove(id, shown_as:)
75
+ refuse_unlink_type(File.lstat(file(id)), shown_as)
76
+ File.unlink(file(id))
77
+ rescue SystemCallError
78
+ raise Error.new("store_remove_failed", "Cannot remove #{shown_as}",
79
+ "Check that the map still exists and restore directory access, then run the command again.",
80
+ shown_as)
81
+ end
82
+
61
83
  # The text of id's file, read within BoundedFile's limit and named as shown_as in a refusal.
62
84
  #: (String, shown_as: String) -> String
63
85
  def read(id, shown_as:)
@@ -78,23 +100,24 @@ module Principles
78
100
 
79
101
  private
80
102
 
103
+ #: (File::Stat, String) -> void
104
+ def refuse_unlink_type(stat, shown_as)
105
+ if stat.symlink?
106
+ raise Error.new("symlinked_store_file", "#{shown_as} is a symbolic link",
107
+ "Replace the final link with the intended regular map file before removing it.", shown_as)
108
+ end
109
+ return if stat.file?
110
+
111
+ raise Error.new("not_a_file", "#{shown_as} is not a regular file",
112
+ "Restore the intended regular map file before removing it.", shown_as)
113
+ end
114
+
81
115
  #: (String) -> bool
82
116
  def directory?(path)
83
117
  File.stat(path).directory?
84
118
  rescue Errno::ENOENT, Errno::ENOTDIR
85
119
  false
86
120
  end
87
-
88
- #: (String) -> void
89
- def refuse_link(relative)
90
- return unless File.symlink?(File.join(@root, relative))
91
-
92
- raise Error.new("symlinked_directory",
93
- "#{relative} is a symbolic link, so a read or write there would leave the " \
94
- "project",
95
- "Replace the link with a real directory, then restore its files from version control.",
96
- relative)
97
- end
98
121
  end
99
122
  end
100
123
  end
@@ -8,6 +8,7 @@ require_relative "json_text"
8
8
  require_relative "bounded_file"
9
9
  require_relative "candidate_files_found"
10
10
  require_relative "linked_path"
11
+ require_relative "requested_candidate_files"
11
12
  module Principles
12
13
  # Every file and process this tool reads or writes. data/CLAUDE.md owns the boundary.
13
14
  module Data
@@ -78,9 +79,16 @@ module Principles
78
79
  "Choose a file inside this project; external scopes are not supported in this alpha.", path)
79
80
  end
80
81
 
81
- # The instruction files hosts load by convention, as CandidateFilesFound lists them.
82
- #: () -> Hash[String, untyped]
83
- def inventory = CandidateFilesFound.new(self).to_h
82
+ # The instruction files hosts load by convention, as CandidateFilesFound lists them, and the files also names.
83
+ #: (?also: Array[String], ?matching: Array[String]) -> Hash[String, untyped]
84
+ def inventory(also: [], matching: []) = CandidateFilesFound.new(self, also:, matching:).to_h
85
+
86
+ # Literal outline requests never walk the project or load saved search paths.
87
+ #: (Array[String]) -> Hash[String, untyped]
88
+ def requested_inventory(paths) = RequestedCandidateFiles.new(self, paths).to_h
89
+
90
+ #: (Array[String]) -> Array[String]?
91
+ def ignored_paths(paths) = IgnoredByGit.new(@root, paths).facts&.fetch("ignored")
84
92
 
85
93
  # JSON from the file at path, which a refusal's message calls named (#1118).
86
94
  #: (String, ?named: String) -> untyped
@@ -1,6 +1,10 @@
1
1
  # typed: strict
2
2
  # frozen_string_literal: true
3
3
 
4
+ require_relative "git_command"
5
+ require_relative "verified_head"
6
+ require_relative "first_parent_log"
7
+
4
8
  module Principles
5
9
  # Every file and process this tool reads or writes. data/CLAUDE.md owns the boundary.
6
10
  module Data
@@ -8,19 +12,26 @@ module Principles
8
12
  # which paths differ from it, and a file's text at a commit. git's own messages are kept off this tool's
9
13
  # output.
10
14
  class Repository
11
- # The only revisions this tool hands git: HEAD or a full commit sha (SHA-1 or SHA-256), optionally its first
12
- # parent. None can be read as an option or a wider revision expression.
15
+ # The revisions this tool hands git: HEAD or a full commit sha (SHA-1 or SHA-256), optionally its first parent.
16
+ # None can be read as an option or a wider revision expression. commit_of alone takes a revision the agent names,
17
+ # which git reads after --end-of-options, and gives back the full sha every other read takes.
13
18
  REVISION = /\A(?:HEAD|\h{40}(?:\h{24})?)\^?\z/ #: Regexp
14
19
 
15
- # git reads each path this tool passes as itself, never as a glob or a :(magic) pathspec, so a member's
16
- # file name cannot take in other files.
17
- LITERAL = { "GIT_LITERAL_PATHSPECS" => "1" }.freeze #: Hash[String, String]
20
+ # The revisions among those that always name one commit: a full sha, optionally its first parent.
21
+ FULL_SHA = /\A\h{40}(?:\h{24})?\^?\z/ #: Regexp
18
22
 
19
23
  #: (String) -> void
20
24
  def initialize(root)
21
25
  @root = root
26
+ @git = GitCommand.new(root) #: GitCommand
27
+ @texts = {} #: Hash[[String, String], String?]
28
+ @log = FirstParentLog.new(@git) #: FirstParentLog
22
29
  end
23
30
 
31
+ # A strict permission snapshot; legacy best-effort readers keep their existing behavior.
32
+ #: () -> VerifiedHead
33
+ def verified_head = VerifiedHead.new(@root)
34
+
24
35
  # Whether the project is inside a git work tree.
25
36
  #: () -> bool
26
37
  def inside?
@@ -62,6 +73,23 @@ module Principles
62
73
  ok && !sha.strip.empty? ? sha.strip : nil
63
74
  end
64
75
 
76
+ # The full sha of the commit revision names, as git resolves it, or nil when it names none. The revision is the
77
+ # agent's own, so git reads it after --end-of-options, never as an option (#1171).
78
+ #: (String) -> String?
79
+ def commit_of(revision)
80
+ sha, ok = git(["rev-parse", "--verify", "--quiet", "--end-of-options", "#{revision}^{commit}"])
81
+ ok && !sha.strip.empty? ? sha.strip : nil
82
+ end
83
+
84
+ # Each path the commits after commit changed, up to HEAD, sorted and relative to the project: added, edited or
85
+ # deleted, and a rename by both its names, since git is asked to detect none. commit is a full sha, as commit_of
86
+ # gives it, so git cannot read it as an option (#1171).
87
+ #: (String) -> Array[String]
88
+ def changed_since(commit)
89
+ output, ok = git(["diff", "--name-only", "-z", "--relative", "--no-renames", revision(commit), "HEAD", "--"])
90
+ ok ? output.force_encoding("UTF-8").split("\0").sort : []
91
+ end
92
+
65
93
  # Which of paths, relative to the project, are untracked or ignored, or differ from HEAD in the index
66
94
  # or the working tree. Before the first commit, every path does.
67
95
  #: (Array[String]) -> Array[String]
@@ -105,11 +133,13 @@ module Principles
105
133
  (changed.split("\0") + untracked.split("\0")).uniq.sort
106
134
  end
107
135
 
108
- # path's text at commit, or nil when the commit does not hold it.
136
+ # path's text at commit, frozen, or nil when the commit does not hold it. Text at a full sha never changes, so
137
+ # git is asked for it once; HEAD moves with each commit, so it is asked every time (#1161).
109
138
  #: (String, String) -> String?
110
139
  def text_at(commit, path)
111
- text, ok = git(["show", "--end-of-options", "#{revision(commit)}:./#{path}"])
112
- ok ? text.force_encoding("UTF-8") : nil
140
+ return read_text_at(commit, path) unless FULL_SHA.match?(commit)
141
+
142
+ @texts[[commit, path]] ||= read_text_at(commit, path)
113
143
  end
114
144
 
115
145
  # The commits after commit, up to HEAD, that changed any of paths, newest first, each as its short sha and
@@ -125,25 +155,26 @@ module Principles
125
155
  end
126
156
  end
127
157
 
128
- # Every commit on HEAD's first-parent line that changed any of paths, newest first, each as its full and
129
- # short sha, subject line, author date and whether it is a root commit ("true" or "false"); none outside git.
130
- # since leaves out since and every commit before it.
131
- #: (Array[String], ?since: String?) -> Array[Hash[String, String]]
132
- def commits_touching(paths, since: nil)
133
- range = since ? "#{revision(since)}..HEAD" : "HEAD"
134
- output, ok = git(["log", "--first-parent", "--format=%H%x00%h%x00%s%x00%aI%x00%P", "--end-of-options", range,
135
- "--", *paths])
136
- return [] unless ok
137
-
138
- output.force_encoding("UTF-8").lines.map do |line|
139
- sha, short, subject, authored_at, parents = line.chomp.split("\0", 5)
140
- { "sha" => sha.to_s, "commit" => short.to_s, "subject" => subject.to_s, "authored_at" => authored_at.to_s,
141
- "root" => parents.to_s.empty?.to_s }
142
- end
158
+ # Every commit on up_to's first-parent line, HEAD's unless given, that changed any of paths, newest first, each
159
+ # as its full and short sha, subject line, author date and whether it is a root commit ("true" or "false"); none
160
+ # outside git. since leaves out since and every commit before it. The list and each commit are frozen. Between
161
+ # full shas the list never changes, so git is asked for it once, and a refusal, as for a sha the clone lacks, is
162
+ # kept too, since asking again would be refused again; HEAD moves with each commit, so it is asked every time
163
+ # (#1161).
164
+ #: (Array[String], ?since: String?, ?up_to: String) -> Array[Hash[String, String]]
165
+ def commits_touching(paths, since: nil, up_to: "HEAD")
166
+ range = since ? "#{revision(since)}..#{revision(up_to)}" : revision(up_to)
167
+ @log.touching(range, paths, kept: [since || up_to, up_to].all? { FULL_SHA.match?(it) })
143
168
  end
144
169
 
145
170
  private
146
171
 
172
+ #: (String, String) -> String?
173
+ def read_text_at(commit, path)
174
+ text, ok = git(["show", "--end-of-options", "#{revision(commit)}:./#{path}"])
175
+ ok ? text.force_encoding("UTF-8").freeze : nil
176
+ end
177
+
147
178
  # commit, once it is known to be a revision this tool hands git. Answers are checked against their schema
148
179
  # first, so reaching here with anything else is a defect in this tool, not a refusal for the agent.
149
180
  #: (String) -> String
@@ -154,12 +185,7 @@ module Principles
154
185
  end
155
186
 
156
187
  #: (Array[String]) -> [String, bool]
157
- def git(arguments)
158
- output = IO.popen(LITERAL, ["git", "-C", @root, *arguments], err: File::NULL, &:read)
159
- [output, Process.last_status&.success? || false]
160
- rescue SystemCallError
161
- ["", false]
162
- end
188
+ def git(arguments) = @git.run(arguments)
163
189
  end
164
190
  end
165
191
  end