principles 0.0.4 → 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 (227) hide show
  1. checksums.yaml +4 -4
  2. data/lib/principles/after_commit_hook_options.rb +4 -1
  3. data/lib/principles/cli.rb +3 -1
  4. data/lib/principles/command_line.rb +38 -26
  5. data/lib/principles/command_routes.rb +11 -4
  6. data/lib/principles/controllers/acknowledgement.rb +93 -17
  7. data/lib/principles/controllers/answer.rb +45 -76
  8. data/lib/principles/controllers/answer_phase.rb +54 -0
  9. data/lib/principles/controllers/assessment.rb +46 -11
  10. data/lib/principles/controllers/audit.rb +17 -6
  11. data/lib/principles/controllers/audit_completion.rb +9 -5
  12. data/lib/principles/controllers/command.rb +5 -4
  13. data/lib/principles/controllers/correction.rb +22 -10
  14. data/lib/principles/controllers/full_audit_request.rb +11 -1
  15. data/lib/principles/controllers/guidance_search_paths.rb +31 -0
  16. data/lib/principles/controllers/inventory.rb +15 -29
  17. data/lib/principles/controllers/mapping.rb +9 -1
  18. data/lib/principles/controllers/member_addition.rb +65 -0
  19. data/lib/principles/controllers/member_entries.rb +6 -24
  20. data/lib/principles/controllers/member_problems_report.rb +13 -6
  21. data/lib/principles/controllers/members_a_commit_changed.rb +2 -1
  22. data/lib/principles/controllers/members_to_assess.rb +21 -13
  23. data/lib/principles/controllers/meta_principle_setup.rb +1 -1
  24. data/lib/principles/controllers/repoint.rb +22 -23
  25. data/lib/principles/controllers/survey.rb +60 -0
  26. data/lib/principles/data/audit_files.rb +4 -0
  27. data/lib/principles/data/bounded_file.rb +9 -4
  28. data/lib/principles/data/candidate_files_found.rb +48 -13
  29. data/lib/principles/data/command-steps.json +85 -24
  30. data/lib/principles/data/committed_tree.rb +84 -0
  31. data/lib/principles/data/first_parent_log.rb +48 -0
  32. data/lib/principles/data/first_parent_range.rb +122 -0
  33. data/lib/principles/data/git_command.rb +37 -0
  34. data/lib/principles/data/guidance_search_paths_file.rb +107 -0
  35. data/lib/principles/data/map_files.rb +35 -10
  36. data/lib/principles/data/principles_directory.rb +40 -0
  37. data/lib/principles/data/principles_subfolder.rb +39 -16
  38. data/lib/principles/data/project_files.rb +22 -11
  39. data/lib/principles/data/repository.rb +55 -29
  40. data/lib/principles/data/requested_candidate_files.rb +43 -0
  41. data/lib/principles/data/running_version.rb +3 -5
  42. data/lib/principles/data/schema_check.rb +16 -6
  43. data/lib/principles/data/schemas/acknowledgement-answer.schema.json +18 -5
  44. data/lib/principles/data/schemas/answer-guide.schema.json +255 -68
  45. data/lib/principles/data/schemas/audit-record.schema.json +10 -1
  46. data/lib/principles/data/schemas/command-steps.schema.json +2 -1
  47. data/lib/principles/data/schemas/correction-answer-guide.schema.json +2 -1
  48. data/lib/principles/data/schemas/correction-answer.schema.json +6 -0
  49. data/lib/principles/data/schemas/guidance-search-paths.schema.json +13 -0
  50. data/lib/principles/data/schemas/member-map-history.schema.json +20 -0
  51. data/lib/principles/data/verified_head.rb +162 -0
  52. data/lib/principles/invocation_options.rb +3 -3
  53. data/lib/principles/models/acknowledgement.rb +95 -84
  54. data/lib/principles/models/acknowledgement_left_as_printed.rb +51 -0
  55. data/lib/principles/models/acknowledgement_refusal.rb +90 -0
  56. data/lib/principles/models/answer_place.rb +4 -0
  57. data/lib/principles/models/assessment_answer.rb +0 -45
  58. data/lib/principles/models/assessment_left_as_printed.rb +81 -0
  59. data/lib/principles/models/audit_entry_protecting_map.rb +28 -0
  60. data/lib/principles/models/audit_record.rb +12 -16
  61. data/lib/principles/models/audit_records.rb +52 -23
  62. data/lib/principles/models/candidate_files.rb +44 -10
  63. data/lib/principles/models/candidate_inventory.rb +69 -8
  64. data/lib/principles/models/candidate_outline.rb +195 -0
  65. data/lib/principles/models/captured_head.rb +72 -0
  66. data/lib/principles/models/carried_member.rb +42 -0
  67. data/lib/principles/models/check_since_last_audit.rb +20 -11
  68. data/lib/principles/models/command_steps.rb +17 -1
  69. data/lib/principles/models/declared_map_changes.rb +29 -12
  70. data/lib/principles/models/declared_member_removals.rb +75 -0
  71. data/lib/principles/models/disposition.rb +4 -2
  72. data/lib/principles/models/fix_only_fields.rb +12 -0
  73. data/lib/principles/models/frontmatter_fields.rb +9 -8
  74. data/lib/principles/models/guidance_search_path.rb +26 -0
  75. data/lib/principles/models/guidance_search_paths.rb +57 -0
  76. data/lib/principles/models/instruction_named.rb +35 -0
  77. data/lib/principles/models/left_as_printed.rb +39 -0
  78. data/lib/principles/models/line_span.rb +6 -0
  79. data/lib/principles/models/loaded_files.rb +1 -2
  80. data/lib/principles/models/loading_scope.rb +61 -0
  81. data/lib/principles/models/locator.rb +6 -0
  82. data/lib/principles/models/mapped_principles.rb +39 -37
  83. data/lib/principles/models/member_assessments.rb +9 -31
  84. data/lib/principles/models/member_file.rb +20 -47
  85. data/lib/principles/models/member_hashes_as_written.rb +10 -6
  86. data/lib/principles/models/member_history.rb +7 -5
  87. data/lib/principles/models/member_lines.rb +63 -15
  88. data/lib/principles/models/member_loading_scopes.rb +49 -0
  89. data/lib/principles/models/member_loads.rb +4 -0
  90. data/lib/principles/models/member_mapping_run.rb +30 -0
  91. data/lib/principles/models/member_moves.rb +4 -0
  92. data/lib/principles/models/member_problems.rb +63 -33
  93. data/lib/principles/models/member_region.rb +129 -0
  94. data/lib/principles/models/member_repair_moves.rb +52 -0
  95. data/lib/principles/models/member_snapshots.rb +14 -1
  96. data/lib/principles/models/members_in_consecutive_sections.rb +56 -0
  97. data/lib/principles/models/meta_principle.rb +14 -0
  98. data/lib/principles/models/missing_files.rb +2 -2
  99. data/lib/principles/models/open_findings_elsewhere.rb +4 -0
  100. data/lib/principles/models/other_templates_placeholders.rb +81 -42
  101. data/lib/principles/models/outline_budget.rb +142 -0
  102. data/lib/principles/models/outline_group.rb +31 -0
  103. data/lib/principles/models/outline_section.rb +56 -0
  104. data/lib/principles/models/principle.rb +14 -7
  105. data/lib/principles/models/principle_file_revision.rb +48 -0
  106. data/lib/principles/models/principle_history.rb +46 -19
  107. data/lib/principles/models/principle_lineage.rb +118 -0
  108. data/lib/principles/models/principle_map_revision.rb +39 -0
  109. data/lib/principles/models/principle_mapping_history.rb +132 -0
  110. data/lib/principles/models/principle_members.rb +25 -0
  111. data/lib/principles/models/principle_predecessor.rb +45 -0
  112. data/lib/principles/models/principle_publication.rb +177 -0
  113. data/lib/principles/models/principle_revision.rb +104 -0
  114. data/lib/principles/models/prior_decision_fields.rb +3 -2
  115. data/lib/principles/models/prior_finding_decision.rb +14 -4
  116. data/lib/principles/models/problem_report.rb +17 -5
  117. data/lib/principles/models/project.rb +46 -39
  118. data/lib/principles/models/project_history.rb +45 -25
  119. data/lib/principles/models/quoted_change.rb +137 -0
  120. data/lib/principles/models/record_format.rb +12 -2
  121. data/lib/principles/models/recorded_entry.rb +6 -0
  122. data/lib/principles/models/recorded_hashes.rb +7 -3
  123. data/lib/principles/models/recorded_members.rb +27 -15
  124. data/lib/principles/models/refusal_before_shape.rb +21 -0
  125. data/lib/principles/models/repoint.rb +13 -0
  126. data/lib/principles/models/reported_check.rb +5 -14
  127. data/lib/principles/models/sent_answer.rb +61 -0
  128. data/lib/principles/models/shape_problems_beside.rb +40 -0
  129. data/lib/principles/models/submitted_answer.rb +34 -0
  130. data/lib/principles/models/survey_search_paths.rb +26 -0
  131. data/lib/principles/models/template_placeholders.rb +34 -4
  132. data/lib/principles/models/text_within.rb +55 -0
  133. data/lib/principles/models/unchecked_members.rb +76 -0
  134. data/lib/principles/models/uncommitted_files.rb +50 -0
  135. data/lib/principles/models/unknown_principle.rb +29 -0
  136. data/lib/principles/models/write_up_opening.rb +21 -4
  137. data/lib/principles/positional_arguments.rb +37 -0
  138. data/lib/principles/session_start_options.rb +2 -2
  139. data/lib/principles/start_audit_options.rb +19 -4
  140. data/lib/principles/version.rb +2 -2
  141. data/lib/principles/views/acknowledgement_result.rb +23 -11
  142. data/lib/principles/views/added_member.rb +41 -0
  143. data/lib/principles/views/answered_request.rb +21 -0
  144. data/lib/principles/views/assessed_members.rb +43 -10
  145. data/lib/principles/views/assessment_request.rb +91 -31
  146. data/lib/principles/views/audit_result.rb +40 -28
  147. data/lib/principles/views/candidate_files_result.rb +85 -19
  148. data/lib/principles/views/candidate_outlines_result.rb +25 -0
  149. data/lib/principles/views/check_notes.rb +44 -0
  150. data/lib/principles/views/check_result_template.rb +14 -23
  151. data/lib/principles/views/consecutive_sections_elsewhere.rb +27 -0
  152. data/lib/principles/views/correction_request.rb +32 -1
  153. data/lib/principles/views/guidance_search_paths_result.rb +26 -0
  154. data/lib/principles/views/instruction.rb +74 -0
  155. data/lib/principles/views/json_with_entries_on_lines.rb +59 -0
  156. data/lib/principles/views/loads_to_check_result.rb +2 -2
  157. data/lib/principles/views/map_added_to.rb +56 -2
  158. data/lib/principles/views/map_result.rb +23 -43
  159. data/lib/principles/views/maps_needing_members_or_audit.rb +47 -0
  160. data/lib/principles/views/member_change_since_baseline.rb +23 -5
  161. data/lib/principles/views/member_repairs.rb +22 -52
  162. data/lib/principles/views/members_a_commit_changed.rb +16 -6
  163. data/lib/principles/views/new_loading_scope.rb +31 -0
  164. data/lib/principles/views/one_member_check_result_template.rb +47 -0
  165. data/lib/principles/views/open_findings_elsewhere_result.rb +3 -3
  166. data/lib/principles/views/open_findings_line.rb +77 -23
  167. data/lib/principles/views/principle_read_result.rb +24 -29
  168. data/lib/principles/views/principles_with_no_completed_audit.rb +9 -3
  169. data/lib/principles/views/prior_findings_result.rb +23 -15
  170. data/lib/principles/views/problem_lines.rb +3 -1
  171. data/lib/principles/views/problem_result.rb +8 -3
  172. data/lib/principles/views/prompts/acknowledge-a-change/run-the-check-and-report-it-passing.md +3 -0
  173. data/lib/principles/views/prompts/acknowledge-a-change/say-whether-the-change-keeps-the-meaning.md +6 -12
  174. data/lib/principles/views/prompts/assess-every-member/answer-guide.json +67 -48
  175. data/lib/principles/views/prompts/assess-every-member/assess-each-member-and-the-set.md +0 -4
  176. data/lib/principles/views/prompts/assess-every-member/claim-an-earlier-first-raised-at.md +7 -0
  177. data/lib/principles/views/prompts/assess-every-member/decide-each-prior-finding.md +4 -16
  178. data/lib/principles/views/prompts/assess-every-member/judge-the-set-with-carried-assessments.md +5 -10
  179. data/lib/principles/views/prompts/assess-every-member/keep-or-change-what-a-deferral-waits-on.md +4 -0
  180. data/lib/principles/views/prompts/assess-every-member/read-a-compact-carried-member.md +8 -0
  181. data/lib/principles/views/prompts/assess-every-member/read-a-trimmed-instruction-whole.md +1 -0
  182. data/lib/principles/views/prompts/assess-every-member/review-a-new-or-changed-check-before-running-it.md +9 -0
  183. data/lib/principles/views/prompts/assess-every-member/say-what-observed-means.md +4 -0
  184. data/lib/principles/views/prompts/dispose-of-each-finding/answer-guide.json +1 -0
  185. data/lib/principles/views/prompts/dispose-of-each-finding/fix-or-record-each-finding.md +16 -3
  186. data/lib/principles/views/prompts/dispose-of-each-finding/retain-assessment-and-correction.md +4 -0
  187. data/lib/principles/views/prompts/find-a-principle-to-map/ask-about-other-guidance-search-paths.md +6 -0
  188. data/lib/principles/views/prompts/find-a-principle-to-map/ask-the-owner-after-the-table.md +1 -0
  189. data/lib/principles/views/prompts/find-a-principle-to-map/ask-which-principle-to-map.md +2 -0
  190. data/lib/principles/views/prompts/find-a-principle-to-map/choose-an-existing-or-new-principle.md +2 -0
  191. data/lib/principles/views/prompts/find-a-principle-to-map/choose-one-principle.md +11 -5
  192. data/lib/principles/views/prompts/find-a-principle-to-map/compare-with-mapped-principles.md +11 -0
  193. data/lib/principles/views/prompts/find-a-principle-to-map/leave-test-data-out-of-the-survey.md +1 -0
  194. data/lib/principles/views/prompts/find-a-principle-to-map/read-a-cut-section-back.md +3 -0
  195. data/lib/principles/views/prompts/find-a-principle-to-map/read-left-out-files.md +3 -0
  196. data/lib/principles/views/prompts/find-a-principle-to-map/survey-a-later-range.md +3 -0
  197. data/lib/principles/views/prompts/find-a-principle-to-map/what-each-command-writes.md +4 -4
  198. data/lib/principles/views/prompts/find-a-principle-to-map/where-the-inventory-looks.md +5 -4
  199. data/lib/principles/views/prompts/learn-what-a-command-does/add-guidance-search-path.md +17 -0
  200. data/lib/principles/views/prompts/learn-what-a-command-does/add-member.md +35 -6
  201. data/lib/principles/views/prompts/learn-what-a-command-does/create-principle.md +16 -0
  202. data/lib/principles/views/prompts/learn-what-a-command-does/list-candidate-files.md +55 -7
  203. data/lib/principles/views/prompts/learn-what-a-command-does/list-member-problems.md +9 -8
  204. data/lib/principles/views/prompts/learn-what-a-command-does/list-open-findings.md +10 -11
  205. data/lib/principles/views/prompts/learn-what-a-command-does/list-principles.md +1 -2
  206. data/lib/principles/views/prompts/learn-what-a-command-does/move-member.md +5 -0
  207. data/lib/principles/views/prompts/learn-what-a-command-does/remove-guidance-search-path.md +13 -0
  208. data/lib/principles/views/prompts/learn-what-a-command-does/remove-principle.md +25 -0
  209. data/lib/principles/views/prompts/learn-what-a-command-does/show-answer-check.md +27 -1
  210. data/lib/principles/views/prompts/learn-what-a-command-does/show-candidate-outlines.md +21 -0
  211. data/lib/principles/views/prompts/learn-what-a-command-does/show-meta-principle-passage.md +5 -5
  212. data/lib/principles/views/prompts/learn-what-a-command-does/start-audit.md +24 -24
  213. data/lib/principles/views/prompts/learn-what-a-command-does/submit-answer.md +51 -54
  214. data/lib/principles/views/prompts/read-requested-outlines/continue-the-survey.md +7 -0
  215. data/lib/principles/views/prompts/report-after-a-commit/the-principles-skill-is-where-to-start.md +1 -0
  216. data/lib/principles/views/prompts/set-principles-up/keep-them-held-between-audits.md +2 -0
  217. data/lib/principles/views/prompts/set-principles-up/name-the-step-each-member-lacks.md +2 -0
  218. data/lib/principles/views/prompts/set-principles-up/place-the-passage-with-the-user.md +9 -6
  219. data/lib/principles/views/prompts/start-a-session/see-the-earlier-line.md +2 -0
  220. data/lib/principles/views/record_waiting_on_an_upgrade.rb +3 -3
  221. data/lib/principles/views/request_part.rb +16 -8
  222. data/lib/principles/views/request_parts.rb +4 -0
  223. metadata +92 -7
  224. data/lib/principles/models/candidate_listing.rb +0 -32
  225. data/lib/principles/views/prompts/find-a-principle-to-map/ask-the-owner-before-mapping.md +0 -1
  226. data/lib/principles/views/prompts/report-after-a-commit/the-map-and-audit-skill-says-how-to-audit-these-members.md +0 -1
  227. data/lib/principles/views/prompts/set-principles-up/set-up-first.md +0 -2
@@ -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,21 +79,31 @@ 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
84
85
 
85
- #: (String) -> untyped
86
- def read_json(path)
87
- parse_json(BoundedFile.new(path).read, path)
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")
92
+
93
+ # JSON from the file at path, which a refusal's message calls named (#1118).
94
+ #: (String, ?named: String) -> untyped
95
+ def read_json(path, named: path)
96
+ parse_json(BoundedFile.new(path, named:).read, path, named:)
88
97
  rescue SystemCallError => e
89
- raise Error.new("unreadable_file", e.message, "Supply a readable JSON file.", path)
98
+ # The system's reason alone, such as "Permission denied", without the path its message ends with.
99
+ reason = e.class.new(nil).message
100
+ raise Error.new("unreadable_file", "#{named} cannot be read: #{reason}", "Supply a readable JSON file.", path)
90
101
  end
91
102
 
92
- # JSON from text read elsewhere, such as stdin, named by source in a refusal.
93
- #: (String, String) -> untyped
94
- def parse_json(text, source)
95
- BoundedFile.new(source).refuse_larger(text.bytesize)
103
+ # JSON from text read elsewhere, such as stdin, given by source in a refusal, whose message calls it named.
104
+ #: (String, String, ?named: String) -> untyped
105
+ def parse_json(text, source, named: source)
106
+ BoundedFile.new(source, named:).refuse_larger(text.bytesize)
96
107
  JsonText.new(text).parsed
97
108
  rescue JSON::ParserError => e
98
109
  raise Error.new("unparseable_json", e.message, "Repair this JSON and retry; no response was accepted.",
@@ -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
@@ -0,0 +1,43 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "../error"
5
+ require_relative "candidate_files_found"
6
+
7
+ module Principles
8
+ module Data
9
+ # Only the literal files the agent asked to outline, with each unread or refused request retained.
10
+ class RequestedCandidateFiles
11
+ #: (ProjectFiles, Array[String]) -> void
12
+ def initialize(files, paths)
13
+ @files = files
14
+ @paths = paths.map { |path| path.sub(%r{\A(?:\./)+}, "") }.uniq.sort #: Array[String]
15
+ freeze
16
+ end
17
+
18
+ #: () -> Hash[String, untyped]
19
+ def to_h
20
+ results = @paths.map { |path| requested(path) }
21
+ listing = { "files" => results.filter_map { |result| result["file"] },
22
+ "unreadable" => results.filter_map { |result| result["refusal"] },
23
+ "possibly_test_data" => [], "selected_files" => @paths }
24
+ ignored = results.filter_map { |result| result["ignored"] }
25
+ ignored.empty? ? listing : listing.merge("ignored" => ignored, "ignored_ancestor_folders" => [])
26
+ end
27
+
28
+ private
29
+
30
+ # Resolve before asking git: an invalid request cannot stop the other paths' ignore checks.
31
+ #: (String) -> Hash[String, untyped]
32
+ def requested(path)
33
+ @files.resolve(path)
34
+ ignored = @files.ignored_paths(@files.text_paths(path))
35
+ return { "ignored" => path } if ignored&.any?
36
+
37
+ { "file" => CandidateFilesFound.listed(path, text: @files.read_text(path), leads_to: @files.leads_to(path)) }
38
+ rescue Error => e
39
+ { "refusal" => e.payload }
40
+ end
41
+ end
42
+ end
43
+ end
@@ -1,8 +1,8 @@
1
1
  # typed: strict
2
2
  # frozen_string_literal: true
3
3
 
4
- require "English"
5
4
  require_relative "../version"
5
+ require_relative "git_command"
6
6
 
7
7
  module Principles
8
8
  # Every file and process this tool reads or writes. data/CLAUDE.md owns the boundary.
@@ -34,10 +34,8 @@ module Principles
34
34
  def commit
35
35
  return @archived_commit if @archived_commit.match?(/\A\h+\z/)
36
36
 
37
- sha = IO.popen(["git", "-C", @checkout, "rev-parse", "--short", "HEAD"], err: File::NULL, &:read).to_s.strip
38
- $CHILD_STATUS&.success? && !sha.empty? ? sha : "unknown"
39
- rescue SystemCallError
40
- "unknown"
37
+ sha, ok = GitCommand.new(@checkout).run(%w[rev-parse --short HEAD])
38
+ ok && !sha.strip.empty? ? sha.strip : "unknown"
41
39
  end
42
40
  end
43
41
  end
@@ -3,6 +3,7 @@
3
3
 
4
4
  require "json"
5
5
  require_relative "../error"
6
+ require_relative "../prose_list"
6
7
  require_relative "expected_type"
7
8
  require_relative "known_fields"
8
9
 
@@ -56,13 +57,14 @@ module Principles
56
57
  KnownFields.new(@schema).of(data)
57
58
  end
58
59
 
59
- # data when it fits the schema; otherwise a refusal with code, naming source and each failing field.
60
- #: (untyped, code: String, source: String, repair: String) -> untyped
61
- def checked(data, code:, source:, repair:)
60
+ # data when it fits the schema; otherwise a refusal with code, giving source and each failing field, whose
61
+ # message calls data named.
62
+ #: (untyped, code: String, source: String, repair: String, ?named: String) -> untyped
63
+ def checked(data, code:, source:, repair:, named: source)
62
64
  found = problems(data)
63
65
  return data if found.empty?
64
66
 
65
- raise Error.new(code, "#{source} is not in the shape this tool reads", repair, source, fields: found)
67
+ raise Error.new(code, "#{named} is not in the shape this tool reads", repair, source, fields: found)
66
68
  end
67
69
 
68
70
  private
@@ -131,11 +133,19 @@ module Principles
131
133
  if property.is_a?(Hash)
132
134
  check(property, value, child(pointer, key), found)
133
135
  elsif property == false
134
- advice = schema.fetch(UNREAD_FIELD_ADVICE, {})[key]
135
- found << problem(child(pointer, key), ["is not a field this tool reads here", *advice].join("; "))
136
+ found << problem(child(pointer, key), unread(schema, key))
136
137
  end
137
138
  end
138
139
 
140
+ # Why key is refused in an object schema checks: not a field read there, the fields that are, so the agent need
141
+ # not guess them (#1115), and what the schema says of key, if anything (#769).
142
+ #: (Hash[String, untyped], String) -> String
143
+ def unread(schema, key)
144
+ read = schema.fetch("properties", {}).keys
145
+ ["is not a field this tool reads here", *("it reads #{ProseList.new(read)}" unless read.empty?),
146
+ *schema.fetch(UNREAD_FIELD_ADVICE, {})[key]].join("; ")
147
+ end
148
+
139
149
  #: (Hash[String, untyped], Array[untyped], String, Array[Hash[String, String]]) -> void
140
150
  def check_items(schema, value, pointer, found)
141
151
  items = schema["items"] or return
@@ -2,16 +2,16 @@
2
2
  "$schema": "https://json-schema.org/draft/2020-12/schema",
3
3
  "$id": "acknowledgement-answer.schema.json",
4
4
  "title": "Acknowledgement answer",
5
- "description": "What an agent answers a one-member audit with (D33): the request's id and basis, whether the change keeps the member's meaning, a one-line reason, and a line quoted from the member's text before the change and one from after it. Fields this tool does not read are refused (D29). Models::Acknowledgement checks the quotes and the reason.",
5
+ "description": "What an agent answers a one-member audit with (D33): the request's id and basis, whether the change keeps the member's meaning, a one-line reason, and a line quoted from the member's text before the change and one from after it, or \"\" for a side with no line the other lacks; a quote left out, as is the basis's base_commit, which the request id already pins, is read as the request's first_changed_lines printed it (#1166). Fields this tool does not read are refused (D29). Models::Acknowledgement checks the quotes and the reason.",
6
6
  "type": "object",
7
7
  "additionalProperties": false,
8
- "required": ["request_id", "basis", "meaning_kept", "reason", "old_line", "new_line"],
8
+ "required": ["request_id", "basis", "meaning_kept", "reason"],
9
9
  "properties": {
10
10
  "request_id": { "type": "string" },
11
11
  "basis": {
12
12
  "type": "object",
13
13
  "additionalProperties": false,
14
- "required": ["base_commit", "member_id"],
14
+ "required": ["member_id"],
15
15
  "properties": {
16
16
  "base_commit": { "type": "string" },
17
17
  "member_id": { "type": "string" }
@@ -19,7 +19,20 @@
19
19
  },
20
20
  "meaning_kept": { "type": "boolean" },
21
21
  "reason": { "type": "string", "minLength": 1 },
22
- "old_line": { "type": "string", "minLength": 1 },
23
- "new_line": { "type": "string", "minLength": 1 }
22
+ "old_line": { "type": "string" },
23
+ "new_line": { "type": "string" },
24
+ "check_result": {
25
+ "type": "object",
26
+ "additionalProperties": false,
27
+ "x-unread-field-advice": {
28
+ "not_run_reason": "a check that cannot be run here needs a full audit: start one instead"
29
+ },
30
+ "description": "The result of the member's check, which the answer reports when the member has one: the command as the map names it, or left out to read as the request printed it (#1166), and its exit_status, which must be 0, since a check that fails or cannot be run is a full audit's to judge, with review_basis when a new line says what its review rested on (D30, D63, #1115). Models::ReportedCheck checks it.",
31
+ "properties": {
32
+ "command": { "type": "string" },
33
+ "exit_status": { "type": ["integer", "string"] },
34
+ "review_basis": { "type": "string" }
35
+ }
36
+ }
24
37
  }
25
38
  }