principles 0.0.5 → 0.0.7

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 (192) hide show
  1. checksums.yaml +4 -4
  2. data/lib/principles/after_commit_hook_options.rb +4 -1
  3. data/lib/principles/cli.rb +9 -0
  4. data/lib/principles/command_line.rb +31 -30
  5. data/lib/principles/command_routes.rb +9 -3
  6. data/lib/principles/controllers/acknowledgement.rb +82 -95
  7. data/lib/principles/controllers/answer.rb +14 -11
  8. data/lib/principles/controllers/assessment.rb +27 -24
  9. data/lib/principles/controllers/audit.rb +24 -4
  10. data/lib/principles/controllers/audit_completion.rb +10 -4
  11. data/lib/principles/controllers/command.rb +2 -2
  12. data/lib/principles/controllers/correction.rb +16 -7
  13. data/lib/principles/controllers/full_audit_request.rb +8 -3
  14. data/lib/principles/controllers/guidance_search_paths.rb +31 -0
  15. data/lib/principles/controllers/inventory.rb +3 -22
  16. data/lib/principles/controllers/mapping.rb +9 -1
  17. data/lib/principles/controllers/member_acknowledgement.rb +137 -0
  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 +33 -25
  21. data/lib/principles/controllers/members_to_assess.rb +1 -1
  22. data/lib/principles/controllers/meta_principle_setup.rb +1 -1
  23. data/lib/principles/controllers/repoint.rb +5 -2
  24. data/lib/principles/controllers/survey.rb +60 -0
  25. data/lib/principles/data/audit_files.rb +4 -0
  26. data/lib/principles/data/candidate_files_found.rb +48 -13
  27. data/lib/principles/data/command-steps.json +86 -25
  28. data/lib/principles/data/committed_tree.rb +84 -0
  29. data/lib/principles/data/first_parent_log.rb +48 -0
  30. data/lib/principles/data/first_parent_range.rb +122 -0
  31. data/lib/principles/data/git_command.rb +37 -0
  32. data/lib/principles/data/guidance_search_paths_file.rb +107 -0
  33. data/lib/principles/data/map_files.rb +35 -10
  34. data/lib/principles/data/principles_directory.rb +40 -0
  35. data/lib/principles/data/principles_subfolder.rb +39 -16
  36. data/lib/principles/data/project_files.rb +11 -3
  37. data/lib/principles/data/repository.rb +55 -29
  38. data/lib/principles/data/requested_candidate_files.rb +43 -0
  39. data/lib/principles/data/running_version.rb +3 -5
  40. data/lib/principles/data/schemas/acknowledgement-answer.schema.json +6 -6
  41. data/lib/principles/data/schemas/acknowledgements-answer.schema.json +88 -0
  42. data/lib/principles/data/schemas/answer-guide.schema.json +255 -63
  43. data/lib/principles/data/schemas/assessment-answer.schema.json +15 -1
  44. data/lib/principles/data/schemas/command-steps.schema.json +1 -1
  45. data/lib/principles/data/schemas/correction-answer-guide.schema.json +2 -1
  46. data/lib/principles/data/schemas/correction-answer.schema.json +12 -54
  47. data/lib/principles/data/schemas/guidance-search-paths.schema.json +13 -0
  48. data/lib/principles/data/schemas/member-map-history.schema.json +20 -0
  49. data/lib/principles/data/verified_head.rb +162 -0
  50. data/lib/principles/invocation_options.rb +5 -4
  51. data/lib/principles/list_open_findings_options.rb +23 -0
  52. data/lib/principles/models/acknowledged_check.rb +94 -0
  53. data/lib/principles/models/acknowledgement.rb +40 -36
  54. data/lib/principles/models/acknowledgement_left_as_printed.rb +72 -0
  55. data/lib/principles/models/acknowledgements.rb +99 -0
  56. data/lib/principles/models/answered_time.rb +9 -0
  57. data/lib/principles/models/assessment_report.rb +13 -13
  58. data/lib/principles/models/audit.rb +17 -4
  59. data/lib/principles/models/audit_entry_protecting_map.rb +28 -0
  60. data/lib/principles/models/audit_record.rb +9 -0
  61. data/lib/principles/models/audit_records.rb +34 -8
  62. data/lib/principles/models/candidate_files.rb +17 -6
  63. data/lib/principles/models/candidate_inventory.rb +51 -7
  64. data/lib/principles/models/captured_head.rb +72 -0
  65. data/lib/principles/models/check_since_last_audit.rb +4 -8
  66. data/lib/principles/models/correction_basis.rb +14 -5
  67. data/lib/principles/models/declared_map_changes.rb +25 -9
  68. data/lib/principles/models/declared_member_removals.rb +75 -0
  69. data/lib/principles/models/disposition.rb +4 -2
  70. data/lib/principles/models/fix_only_fields.rb +12 -0
  71. data/lib/principles/models/guidance_search_path.rb +26 -0
  72. data/lib/principles/models/guidance_search_paths.rb +57 -0
  73. data/lib/principles/models/line_span.rb +6 -0
  74. data/lib/principles/models/list_member_problems_check.rb +80 -0
  75. data/lib/principles/models/listed_members.rb +49 -0
  76. data/lib/principles/models/loaded_files.rb +1 -2
  77. data/lib/principles/models/loading_decision.rb +17 -8
  78. data/lib/principles/models/loading_scope.rb +61 -0
  79. data/lib/principles/models/loads_to_check.rb +0 -1
  80. data/lib/principles/models/locator.rb +6 -0
  81. data/lib/principles/models/mapped_principles.rb +39 -37
  82. data/lib/principles/models/member_assessments.rb +11 -2
  83. data/lib/principles/models/member_file.rb +20 -71
  84. data/lib/principles/models/member_history.rb +7 -5
  85. data/lib/principles/models/member_loading_scopes.rb +49 -0
  86. data/lib/principles/models/member_loads.rb +4 -0
  87. data/lib/principles/models/member_mapping_run.rb +30 -0
  88. data/lib/principles/models/member_moves.rb +4 -0
  89. data/lib/principles/models/member_problems.rb +57 -29
  90. data/lib/principles/models/member_region.rb +129 -0
  91. data/lib/principles/models/member_repair_moves.rb +9 -11
  92. data/lib/principles/models/member_sighting.rb +5 -8
  93. data/lib/principles/models/member_snapshots.rb +14 -1
  94. data/lib/principles/models/members_in_consecutive_sections.rb +56 -0
  95. data/lib/principles/models/meta_principle.rb +14 -0
  96. data/lib/principles/models/missing_files.rb +2 -2
  97. data/lib/principles/models/other_templates_placeholders.rb +1 -1
  98. data/lib/principles/models/outline_budget.rb +29 -14
  99. data/lib/principles/models/principle.rb +14 -7
  100. data/lib/principles/models/principle_file_revision.rb +48 -0
  101. data/lib/principles/models/principle_history.rb +46 -19
  102. data/lib/principles/models/principle_lineage.rb +118 -0
  103. data/lib/principles/models/principle_map_revision.rb +39 -0
  104. data/lib/principles/models/principle_mapping_history.rb +132 -0
  105. data/lib/principles/models/principle_members.rb +14 -0
  106. data/lib/principles/models/principle_predecessor.rb +45 -0
  107. data/lib/principles/models/principle_publication.rb +177 -0
  108. data/lib/principles/models/principle_revision.rb +104 -0
  109. data/lib/principles/models/problem_report.rb +36 -9
  110. data/lib/principles/models/project.rb +47 -30
  111. data/lib/principles/models/project_history.rb +45 -25
  112. data/lib/principles/models/record_format.rb +6 -0
  113. data/lib/principles/models/reported_check.rb +16 -12
  114. data/lib/principles/models/request_id.rb +1 -1
  115. data/lib/principles/models/sent_answer.rb +35 -5
  116. data/lib/principles/models/survey_search_paths.rb +26 -0
  117. data/lib/principles/models/uncommitted_files.rb +50 -0
  118. data/lib/principles/models/unknown_principle.rb +29 -0
  119. data/lib/principles/positional_arguments.rb +38 -0
  120. data/lib/principles/session_start_options.rb +3 -3
  121. data/lib/principles/start_audit_options.rb +10 -2
  122. data/lib/principles/version.rb +2 -2
  123. data/lib/principles/views/acknowledgement_result.rb +54 -16
  124. data/lib/principles/views/added_member.rb +41 -0
  125. data/lib/principles/views/answered_request.rb +21 -0
  126. data/lib/principles/views/assessed_members.rb +26 -5
  127. data/lib/principles/views/assessment_request.rb +24 -4
  128. data/lib/principles/views/audit_result.rb +30 -27
  129. data/lib/principles/views/candidate_files_result.rb +56 -12
  130. data/lib/principles/views/candidate_outlines_result.rb +25 -0
  131. data/lib/principles/views/check_result_template.rb +18 -17
  132. data/lib/principles/views/consecutive_sections_elsewhere.rb +27 -0
  133. data/lib/principles/views/correction_request.rb +38 -10
  134. data/lib/principles/views/findings_result.rb +21 -0
  135. data/lib/principles/views/guidance_search_paths_result.rb +26 -0
  136. data/lib/principles/views/invocation.rb +19 -4
  137. data/lib/principles/views/json_with_entries_on_lines.rb +12 -4
  138. data/lib/principles/views/loads_to_check_result.rb +3 -2
  139. data/lib/principles/views/map_added_to.rb +56 -2
  140. data/lib/principles/views/map_result.rb +22 -42
  141. data/lib/principles/views/maps_needing_members_or_audit.rb +47 -0
  142. data/lib/principles/views/member_change_since_baseline.rb +2 -1
  143. data/lib/principles/views/member_repairs.rb +2 -1
  144. data/lib/principles/views/member_row.rb +61 -0
  145. data/lib/principles/views/members_a_commit_changed.rb +2 -1
  146. data/lib/principles/views/members_with_no_baseline.rb +8 -1
  147. data/lib/principles/views/new_loading_scope.rb +31 -0
  148. data/lib/principles/views/one_member_check_result_template.rb +37 -21
  149. data/lib/principles/views/open_findings_line.rb +2 -1
  150. data/lib/principles/views/principle_read_result.rb +4 -1
  151. data/lib/principles/views/principles_with_no_completed_audit.rb +9 -3
  152. data/lib/principles/views/problem_lines.rb +50 -45
  153. data/lib/principles/views/problem_result.rb +59 -25
  154. data/lib/principles/views/prompts/acknowledge-a-change/report-the-exit-status-its-own-listing-gives.md +3 -0
  155. data/lib/principles/views/prompts/acknowledge-a-change/run-the-check-and-report-it-passing.md +3 -3
  156. data/lib/principles/views/prompts/acknowledge-a-change/say-whether-each-change-keeps-its-meaning.md +8 -0
  157. data/lib/principles/views/prompts/acknowledge-a-change/say-whether-the-change-keeps-the-meaning.md +6 -6
  158. data/lib/principles/views/prompts/assess-every-member/answer-guide.json +67 -43
  159. data/lib/principles/views/prompts/assess-every-member/decide-each-member-loading.md +2 -1
  160. data/lib/principles/views/prompts/assess-every-member/run-the-check-then-report-it.md +2 -2
  161. data/lib/principles/views/prompts/assess-every-member/run-the-reviewed-check-and-say-what-its-review-rested-on.md +5 -3
  162. data/lib/principles/views/prompts/dispose-of-each-finding/answer-guide.json +1 -0
  163. data/lib/principles/views/prompts/dispose-of-each-finding/fix-or-record-each-finding.md +12 -0
  164. data/lib/principles/views/prompts/dispose-of-each-finding/retain-assessment-and-correction.md +4 -0
  165. data/lib/principles/views/prompts/find-a-principle-to-map/ask-about-other-guidance-search-paths.md +6 -0
  166. data/lib/principles/views/prompts/find-a-principle-to-map/choose-an-existing-or-new-principle.md +2 -0
  167. data/lib/principles/views/prompts/find-a-principle-to-map/choose-one-principle.md +4 -2
  168. data/lib/principles/views/prompts/find-a-principle-to-map/compare-with-mapped-principles.md +11 -0
  169. data/lib/principles/views/prompts/find-a-principle-to-map/read-left-out-files.md +2 -2
  170. data/lib/principles/views/prompts/find-a-principle-to-map/survey-a-later-range.md +3 -0
  171. data/lib/principles/views/prompts/find-a-principle-to-map/what-each-command-writes.md +4 -4
  172. data/lib/principles/views/prompts/find-a-principle-to-map/where-the-inventory-looks.md +5 -4
  173. data/lib/principles/views/prompts/learn-what-a-command-does/add-guidance-search-path.md +17 -0
  174. data/lib/principles/views/prompts/learn-what-a-command-does/add-member.md +35 -6
  175. data/lib/principles/views/prompts/learn-what-a-command-does/create-principle.md +16 -0
  176. data/lib/principles/views/prompts/learn-what-a-command-does/every-command-and-how-they-chain.md +1 -2
  177. data/lib/principles/views/prompts/learn-what-a-command-does/list-candidate-files.md +37 -4
  178. data/lib/principles/views/prompts/learn-what-a-command-does/list-member-problems.md +20 -19
  179. data/lib/principles/views/prompts/learn-what-a-command-does/list-open-findings.md +65 -61
  180. data/lib/principles/views/prompts/learn-what-a-command-does/move-member.md +5 -0
  181. data/lib/principles/views/prompts/learn-what-a-command-does/remove-guidance-search-path.md +13 -0
  182. data/lib/principles/views/prompts/learn-what-a-command-does/remove-principle.md +25 -0
  183. data/lib/principles/views/prompts/learn-what-a-command-does/show-answer-check.md +33 -7
  184. data/lib/principles/views/prompts/learn-what-a-command-does/show-candidate-outlines.md +21 -0
  185. data/lib/principles/views/prompts/learn-what-a-command-does/start-audit.md +10 -10
  186. data/lib/principles/views/prompts/learn-what-a-command-does/submit-answer.md +51 -54
  187. data/lib/principles/views/prompts/read-requested-outlines/continue-the-survey.md +7 -0
  188. data/lib/principles/views/prompts/start-a-session/act-on-the-open-findings.md +1 -1
  189. data/lib/principles/views/prompts/start-a-session/every-loaded-line-costs-every-session.md +1 -0
  190. data/lib/principles/views/prompts/start-a-session/start-with-the-principles-skill.md +1 -0
  191. metadata +67 -4
  192. data/lib/principles/models/candidate_listing.rb +0 -41
@@ -5,6 +5,8 @@ require_relative "../error"
5
5
  require_relative "../data/map_files"
6
6
  require_relative "principle"
7
7
  require_relative "meta_principle"
8
+ require_relative "unknown_principle"
9
+ require_relative "project_history"
8
10
 
9
11
  module Principles
10
12
  # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
@@ -14,9 +16,10 @@ module Principles
14
16
  # A principle id names its principle in lower-kebab-case.
15
17
  ID_FORMAT = Data::PrincipleId::FORMAT #: Regexp
16
18
 
17
- #: (Data::MapFiles) -> void
18
- def initialize(files)
19
+ #: (Data::MapFiles, ProjectHistory) -> void
20
+ def initialize(files, history)
19
21
  @files = files
22
+ @history = history
20
23
  end
21
24
 
22
25
  #: () -> Array[String]
@@ -24,6 +27,16 @@ module Principles
24
27
  @files.ids
25
28
  end
26
29
 
30
+ # The mapped rules a survey compares with its candidate principles, without audit or freshness reads.
31
+ #: () -> Array[Hash[String, untyped]]
32
+ def descriptions
33
+ ids.map do |id|
34
+ find(id).description_with_id
35
+ rescue Error => e
36
+ { "id" => id, "refusal" => e.payload }
37
+ end
38
+ end
39
+
27
40
  # The id of map id, or of every map when id is nil; an id no map has is refused, as find refuses it.
28
41
  #: (String?) -> Array[String]
29
42
  def ids_of(id)
@@ -66,13 +79,18 @@ module Principles
66
79
  #: (String) -> Principle
67
80
  def find(id)
68
81
  refuse_unknown(id)
69
- unique(Principle.new(id, @files.read(id)))
82
+ Principle.from_map(id, @files.read(id), files: @files)
70
83
  end
71
84
 
72
85
  # The map id as text read elsewhere holds it, such as its file at commit, the one an audit began from.
73
86
  #: (String, String, ?commit: String?) -> Principle
74
87
  def parsed(id, text, commit: nil)
75
- unique(Principle.new(id, @files.parse(id, text, commit: commit)), commit)
88
+ Principle.from_map(id, @files.parse(id, text, commit: commit), files: @files, commit: commit)
89
+ end
90
+
91
+ #: (String, String, commit: String?) -> Principle
92
+ def parsed_history(id, text, commit:)
93
+ Principle.from_map(id, @files.parse_history(id, text, commit: commit), files: @files, commit: commit)
76
94
  end
77
95
 
78
96
  # A new map for id with no members, not yet saved. It is refused unless id is lower-kebab-case and not
@@ -80,7 +98,8 @@ module Principles
80
98
  #: (String) { () -> String } -> Principle
81
99
  def create(id, &)
82
100
  refuse_unavailable(id)
83
- refuse_meta_principle_id(id)
101
+ MetaPrinciple.refuse_explicit_creation(id)
102
+ refuse_uncommitted_removal(id)
84
103
  Principle.new(id, { "description" => yield, "members" => [] })
85
104
  end
86
105
 
@@ -89,8 +108,9 @@ module Principles
89
108
  # has is refused, as find refuses it.
90
109
  #: (String) -> Principle
91
110
  def addable(id)
92
- return find(id) if mapped?(id) || !started_by_setup?(id)
111
+ return find(id) if mapped?(id) || !MetaPrinciple.id?(id)
93
112
 
113
+ refuse_uncommitted_removal(id)
94
114
  Principle.new(id, MetaPrinciple.starting_fields)
95
115
  end
96
116
 
@@ -111,6 +131,13 @@ module Principles
111
131
  file(map.id)
112
132
  end
113
133
 
134
+ # Removes only the map, after the command's audit-entry guard; no member or audit file is changed.
135
+ #: (Principle) -> String
136
+ def remove(map)
137
+ @files.remove(map.id)
138
+ file(map.id)
139
+ end
140
+
114
141
  # The file map id is kept in, relative to the project.
115
142
  #: (String) -> String
116
143
  def file(id)
@@ -125,17 +152,12 @@ module Principles
125
152
 
126
153
  private
127
154
 
128
- # Whether principle id's map is started by setup and written by the add-member it prints, never by
129
- # create-principle: only the meta-principle's, whose description and activation ship with the tool (T74).
130
- #: (String) -> bool
131
- def started_by_setup?(id) = id == MetaPrinciple::ID
155
+ #: (String) -> void
156
+ def refuse_uncommitted_removal(id)
157
+ return unless [file(id), earlier_file(id)].any? { |path| @history.entry_at_head?(path) }
132
158
 
133
- # map, refused when two of its members share an id or a key, as PrincipleMembers#refuse_repeats says.
134
- #: (Principle, ?String?) -> Principle
135
- def unique(map, commit = nil)
136
- where = commit ? "#{file(map.id)} at commit #{commit[0, 12]}" : file(map.id)
137
- map.members.refuse_repeats(where: where, file: file(map.id))
138
- map
159
+ raise Error.new("map_removal_not_committed", "#{id}'s map is still present at committed HEAD",
160
+ "Commit its removal or ID change before recreating #{id}, then retry this command.", file(id))
139
161
  end
140
162
 
141
163
  # A principle no map names, with the commands that create one and list the rest. For the meta-principle's id the
@@ -145,27 +167,7 @@ module Principles
145
167
  def refuse_unknown(id)
146
168
  return if mapped?(id)
147
169
 
148
- raise Error.new("unknown_principle", "No map named #{id}",
149
- "#{creating(id)}\nTo see the mapped ids: #{Error::EXECUTABLE} list-principles")
150
- end
151
-
152
- # The repair line that creates principle id's map.
153
- #: (String) -> String
154
- def creating(id)
155
- return MetaPrinciple::SET_UP if started_by_setup?(id)
156
-
157
- "To create it: #{Error::EXECUTABLE} create-principle #{id} --description TEXT"
158
- end
159
-
160
- # The meta-principle's map is written by the add-member setup prints, with its first member; create-principle
161
- # would write it with no members, another description and no activation (T74).
162
- #: (String) -> void
163
- def refuse_meta_principle_id(id)
164
- return unless started_by_setup?(id)
165
-
166
- raise Error.new("meta_principle_id",
167
- "#{id} is set up by show-meta-principle-passage, whose printed add-member creates its map " \
168
- "with its first member", MetaPrinciple::SET_UP)
170
+ raise UnknownPrinciple.new(id).refusal
169
171
  end
170
172
 
171
173
  #: (String) -> void
@@ -7,6 +7,7 @@ require_relative "answer_fields"
7
7
  require_relative "answer_place"
8
8
  require_relative "carried_assessments"
9
9
  require_relative "principle_members"
10
+ require_relative "answered_time"
10
11
  require_relative "member_sighting"
11
12
  require_relative "misspelt_member_id"
12
13
  require_relative "record_judged_against"
@@ -88,12 +89,20 @@ module Principles
88
89
 
89
90
  private
90
91
 
91
- # assessments without the sighting fields each leaves as printed, which it does not give (#1010).
92
+ # assessments without the sighting fields each leaves as printed, which it does not give (#1010), each
93
+ # observed_at given with an offset read in audited_at's form (#1187).
92
94
  #: (untyped) -> untyped
93
95
  def given(assessments)
94
96
  return assessments unless assessments.is_a?(Array)
95
97
 
96
- assessments.map { |assessment| assessment.is_a?(Hash) ? SIGHTING.given(assessment) : assessment }
98
+ assessments.map { |assessment| assessment.is_a?(Hash) ? in_utc(SIGHTING.given(assessment)) : assessment }
99
+ end
100
+
101
+ #: (Hash[String, untyped]) -> Hash[String, untyped]
102
+ def in_utc(assessment)
103
+ return assessment unless assessment.key?("observed_at")
104
+
105
+ assessment.merge("observed_at" => AnsweredTime.new(assessment["observed_at"]).in_audited_at_form)
97
106
  end
98
107
 
99
108
  # Refuses answered, the ids the assessments name, unless they are the mapped ids, each once, hinting at any
@@ -3,8 +3,9 @@
3
3
 
4
4
  require "digest"
5
5
  require_relative "../error"
6
- require_relative "../prose_list"
7
6
  require_relative "locator"
7
+ require_relative "member_region"
8
+ require_relative "line_span"
8
9
  require_relative "write_up_opening"
9
10
 
10
11
  module Principles
@@ -27,10 +28,6 @@ module Principles
27
28
  PLACE_LOST = %w[section_not_found passage_not_found passage_end_not_found ambiguous_heading
28
29
  ambiguous_passage field_not_found].freeze #: Array[String]
29
30
 
30
- # How many openings of an ambiguous passage its refusal names by line, so a one-character opening does not
31
- # name every line of the file.
32
- OPENINGS_NAMED = 5 #: Integer
33
-
34
31
  # The file name endings read as Markdown, where a # line is a heading and not a comment.
35
32
  MARKDOWN = /\.(md|markdown|mdx)\z/i #: Regexp
36
33
 
@@ -60,10 +57,27 @@ module Principles
60
57
  first, last = region(lines, locator)
61
58
  selected = lines[first...last].to_a.join
62
59
  start_text = locator.starting_at
63
- first, last, selected = passage(lines, first, selected, start_text, locator.ending_at) if start_text
60
+ if start_text
61
+ selected_region = MemberRegion.new(self, span: LineSpan.new(first + 1, last))
62
+ first, last, selected = selected_region.passage(start_text, locator.ending_at)
63
+ end
64
64
  snapshot(locator, selected, first + 1, last)
65
65
  end
66
66
 
67
+ # Only a newly supplied add/move location needs an unambiguous ending; existing references keep lookup.
68
+ #: (Locator) -> Hash[String, untyped]
69
+ def locate_for_authoring(locator)
70
+ found = locate(locator)
71
+ opening = locator.starting_at
72
+ ending = locator.ending_at
73
+ if opening && ending
74
+ first, last = region(@text.lines, locator)
75
+ selected_region = MemberRegion.new(self, span: LineSpan.new(first + 1, last))
76
+ selected_region.check_authored_ending(opening, ending)
77
+ end
78
+ found
79
+ end
80
+
67
81
  # The lines of the region locator names, as the agent reads them (D33).
68
82
  #: (Locator) -> Array[String]
69
83
  def lines_of(locator)
@@ -107,71 +121,6 @@ module Principles
107
121
  def region(lines, locator)
108
122
  raise NotImplementedError, "#{self.class} names no region"
109
123
  end
110
-
111
- # Narrows a member to a passage of its section, or of the whole text. starting_at must occur exactly
112
- # once. With ending_at, the passage runs from the start of that match to the end of the first ending_at
113
- # after it; without, to the end of the line it starts on. Anchors, never line numbers, so the passage
114
- # stays found while text around and inside it changes. A lost anchor is refused, never widened to the
115
- # whole file. Returns the passage's first and last lines, counted from the start of the text, and the
116
- # passage.
117
- #: (Array[String], Integer, String, String, String?) -> [Integer, Integer, String]
118
- def passage(lines, first, selected, start_text, end_text)
119
- before = lines[0...first].to_a.join.length
120
- start = only_opening(selected, start_text, before)
121
- found = selected[start...passage_end(selected, start, start_text, end_text)].to_s
122
- line = @text[0, before + start].to_s.count("\n")
123
- [line, line + line_count(found), found]
124
- end
125
-
126
- # How many lines a passage spans, counting a last line with no line break.
127
- #: (String) -> Integer
128
- def line_count(found)
129
- found.count("\n") + (found.end_with?("\n") ? 0 : 1)
130
- end
131
-
132
- # Where start_text's one occurrence in text begins; text starts before characters into the file.
133
- #: (String, String, Integer) -> Integer
134
- def only_opening(text, start_text, before)
135
- starts = text.enum_for(:scan, start_text).filter_map { Regexp.last_match&.begin(0) }
136
- return starts.first.to_i if starts.length == 1
137
-
138
- starts.empty? ? opening_not_found(start_text) : ambiguous_opening(start_text, starts.map { before + it })
139
- end
140
-
141
- #: (String) -> bot
142
- def opening_not_found(start_text)
143
- raise Error.new("passage_not_found", "The passage starting #{start_text.inspect} is not in the file",
144
- "Quote the passage's opening text exactly.", @path)
145
- end
146
-
147
- # Names the line of each opening, for the agent to quote more of the one it means (dogfood D5).
148
- #: (String, Array[Integer]) -> bot
149
- def ambiguous_opening(start_text, offsets)
150
- raise Error.new("ambiguous_passage",
151
- "#{start_text.inspect} occurs #{offsets.length} times, at lines #{lines_named(offsets)}",
152
- "Quote more of the passage's start so it occurs once.", @path)
153
- end
154
-
155
- # The lines, as first_line counts them, of the first OPENINGS_NAMED offsets into the file, and how many more.
156
- #: (Array[Integer]) -> String
157
- def lines_named(offsets)
158
- named = offsets.first(OPENINGS_NAMED).map { (@text[0, it].to_s.count("\n") + 1).to_s }
159
- more = offsets.length - named.length
160
- more.zero? ? ProseList.new(named).to_s : "#{named.join(", ")} and #{more} more"
161
- end
162
-
163
- #: (String, Integer, String, String?) -> Integer
164
- def passage_end(text, start, start_text, end_text)
165
- return (text.index("\n", start) || (text.length - 1)) + 1 unless end_text
166
-
167
- found = text.index(end_text, start + start_text.length)
168
- unless found
169
- raise Error.new("passage_end_not_found", "No #{end_text.inspect} after #{start_text.inspect}",
170
- "Quote the passage's closing text exactly.", @path)
171
- end
172
-
173
- found + end_text.length
174
- end
175
124
  end
176
125
  end
177
126
  end
@@ -12,13 +12,15 @@ module Principles
12
12
  # The most commits listed for one member; the walk says when it stopped here.
13
13
  LIMIT = 20
14
14
 
15
- # commits are the commits that touched the member's file or its map, newest first; hash_at gives the
16
- # member's hash at a revision, or nil where it cannot be found there.
17
- #: (baseline: String, now: String, commits: Array[Hash[String, String]], hash_at: ^(String) -> String?) -> void
18
- def initialize(baseline:, now:, commits:, hash_at:)
15
+ # commits are the commits that touched the member's file or its map, newest first, up to head, the commit the
16
+ # command reads as HEAD; hash_at gives the member's hash at a revision, or nil where it cannot be found there.
17
+ #: (baseline: String, now: String, commits: Array[Hash[String, String]], head: String,
18
+ #| hash_at: ^(String) -> String?) -> void
19
+ def initialize(baseline:, now:, commits:, head:, hash_at:)
19
20
  @baseline = baseline
20
21
  @now = now
21
22
  @commits = commits
23
+ @head = head
22
24
  @hash_at = hash_at
23
25
  freeze
24
26
  end
@@ -29,7 +31,7 @@ module Principles
29
31
  def to_h
30
32
  listed = [] #: Array[Hash[String, String]]
31
33
  stopped = walk(listed)
32
- { "commits" => listed, "stopped_at" => stopped, "uncommitted" => @hash_at.call("HEAD") != @now }
34
+ { "commits" => listed, "stopped_at" => stopped, "uncommitted" => @hash_at.call(@head) != @now }
33
35
  end
34
36
 
35
37
  private
@@ -0,0 +1,49 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "member_loads"
5
+ require_relative "loading_scope"
6
+
7
+ module Principles
8
+ module Models
9
+ # One map's per-member loading scopes and the scope an added member newly represents.
10
+ class MemberLoadingScopes
11
+ #: (Hash[String, MemberLoads]) -> void
12
+ def initialize(loads)
13
+ @scopes = loads.transform_values { |member| LoadingScope.new(member.claude_code) }
14
+ .freeze #: Hash[String, LoadingScope]
15
+ freeze
16
+ end
17
+
18
+ # A new known scope absent from prior known ones, beside at least one differing known scope.
19
+ #: (String) -> Hash[String, untyped]
20
+ def introduced_by(id)
21
+ added = @scopes.fetch(id)
22
+ prior = @scopes.except(id)
23
+ known = prior.select { |_member_id, scope| scope.known? }
24
+ return {} unless added.known? && known.any? && known.values.none? { |scope| scope == added }
25
+
26
+ unknown = unclassified(prior)
27
+ { "loading_basis" => MemberLoads::BASIS.dup, "scope" => added.to_h, "already_mapped" => grouped(known),
28
+ **(unknown.empty? ? {} : { "not_compared" => unknown }) }
29
+ end
30
+
31
+ private
32
+
33
+ #: (Hash[String, LoadingScope]) -> Array[Hash[String, untyped]]
34
+ def grouped(known)
35
+ groups = known.group_by { |_id, scope| scope }.map do |scope, members|
36
+ { "member_ids" => members.map(&:first).sort, "scope" => scope.to_h }
37
+ end
38
+ groups.sort_by { |group| group.fetch("member_ids").first }
39
+ end
40
+
41
+ #: (Hash[String, LoadingScope]) -> Array[Hash[String, untyped]]
42
+ def unclassified(prior)
43
+ prior.reject { |_id, scope| scope.known? }.sort_by(&:first).map do |id, scope|
44
+ { "member_id" => id, "scope" => scope.to_h }
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -19,6 +19,10 @@ module Principles
19
19
  # audit-record.schema.json names each field so show-audit prints it; member_loads_spec holds a sample of each
20
20
  # branch to hold the two together, so add one there for a new branch.
21
21
  class MemberLoads
22
+ # The derivation shared by member and aggregate reports, never evidence of receipt in a session.
23
+ BASIS = { "host" => "claude_code", "kind" => "documented_prediction",
24
+ "session_start_assumption" => "project_root" }.freeze #: Hash[String, String]
25
+
22
26
  # The instruction files Claude Code loads at session start in the folder it starts in, and once a file is
23
27
  # read, written or edited in a folder below it, as its memory documentation says. This tool assumes a session
24
28
  # started at the project root.
@@ -0,0 +1,30 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require "json"
5
+ require_relative "project_history"
6
+
7
+ module Principles
8
+ # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
9
+ module Models
10
+ # One member key's ordered historical placements and its oldest locatable mapping baseline.
11
+ class MemberMappingRun
12
+ #: (String, Array[[String, Hash[String, untyped]]]) -> void
13
+ def initialize(key, entries)
14
+ @key = key.dup.freeze #: String
15
+ @entries = JSON.parse(JSON.generate(entries), freeze: true) #: Array[[String, Hash[String, untyped]]]
16
+ freeze
17
+ end
18
+
19
+ #: (ProjectHistory) -> Hash[String, String]?
20
+ def baseline(history)
21
+ @entries.reverse_each do |sha, fields|
22
+ hash = history.hashes_by_key_at(sha, [fields])[@key] or next
23
+
24
+ return { "commit" => sha, "sha256" => hash }
25
+ end
26
+ nil
27
+ end
28
+ end
29
+ end
30
+ end
@@ -24,6 +24,10 @@ module Principles
24
24
  @moves.empty?
25
25
  end
26
26
 
27
+ # Whether any id participates in either end of a declared move, including a move keeping its id.
28
+ #: (Array[String]) -> bool
29
+ def involving_any?(ids) = @moves.keys.intersect?(ids) || @moves.values.intersect?(ids)
30
+
27
31
  # Refuses moves outside git, where there is no base map to check them against.
28
32
  #: (String?, principle_id: String) -> String
29
33
  def check_in_git(base, principle_id:)
@@ -6,6 +6,7 @@ require_relative "acknowledgement_refusal"
6
6
  require_relative "audit_record"
7
7
  require_relative "principle_members"
8
8
  require_relative "principle_history"
9
+ require_relative "principle_mapping_history"
9
10
  require_relative "member_state"
10
11
  require_relative "locate_refusal"
11
12
 
@@ -22,21 +23,19 @@ module Principles
22
23
  class Baselines
23
24
  # The last audit's hashes for members' map, and each member's hash and commit at the commit that mapped it
24
25
  # where it is, read with the map as that commit held it; none outside git.
25
- #: (Project, PrincipleMembers) -> Baselines
26
- def self.of(project, members)
26
+ #: (Project, PrincipleMembers, ?mapping: PrincipleMappingHistory?) -> Baselines
27
+ def self.of(project, members, mapping: nil)
27
28
  records = project.audit_records
28
- mapped = mapped_where_it_is(project, members)
29
- new(audit: records.readable(members.principle_id), mapped: mapped.transform_values { it.fetch("sha256") },
30
- mapped_in: mapped.transform_values { it.fetch("commit") }, in_git: project.history.inside_git?,
31
- hashes: records.hashes_to_compare(members.principle_id))
29
+ mapped = mapped_where_it_is(project, members, mapping: mapping)
30
+ new(audit: records.readable(members.principle_id), mapped: mapped, in_git: project.history.inside_git?,
31
+ hashes: records.hashes_to_compare(members.principle_id), mapping: mapping)
32
32
  end
33
33
 
34
34
  # Each member's hash and commit at the commit that mapped it where it is, read with the map as that commit
35
35
  # held it.
36
- #: (Project, PrincipleMembers) -> Hash[String, Hash[String, String]]
37
- def self.mapped_where_it_is(project, members)
38
- PrincipleHistory.new(id: members.principle_id, history: project.history, principles: project.principles)
39
- .mapped_baselines(members.keys)
36
+ #: (Project, PrincipleMembers, ?mapping: PrincipleMappingHistory?) -> Hash[String, Hash[String, String]]
37
+ def self.mapped_where_it_is(project, members, mapping: nil)
38
+ project.principle_history(members.principle_id, mapping: mapping).mapped_baselines(members.keys)
40
39
  end
41
40
 
42
41
  # Whether the project is in git, so a member with no baseline is one no commit holds yet.
@@ -50,18 +49,26 @@ module Principles
50
49
  # audit is the principle's audit record, if any: its hashes, the commit it began from and whether it is
51
50
  # completed, so a one-member audit is offered only where one would be accepted (#626). hashes is the member
52
51
  # hashes the record holds, which a later copy may have written, so its members are compared read-only (#1066).
53
- #: (audit: AuditRecord?, mapped: Hash[String, String], mapped_in: Hash[String, String], in_git: bool,
54
- #| ?hashes: RecordedHashes) -> void
55
- def initialize(audit:, mapped:, mapped_in:, in_git:, hashes: audit&.members&.hashes || RecordedHashes::NONE)
52
+ #: (audit: AuditRecord?, mapped: Hash[String, Hash[String, String]], in_git: bool,
53
+ #| ?hashes: RecordedHashes, ?mapping: PrincipleMappingHistory?) -> void
54
+ def initialize(audit:, mapped:, in_git:, hashes: audit&.members&.hashes || RecordedHashes::NONE, mapping: nil)
56
55
  @hashes = hashes
57
56
  @audit = audit #: AuditRecord?
58
57
  @audit_base = audit&.base_commit #: String?
59
58
  @mapped = mapped
60
- @mapped_in = mapped_in
61
59
  @in_git = in_git
60
+ @mapping = mapping #: PrincipleMappingHistory?
62
61
  freeze
63
62
  end
64
63
 
64
+ # Only a mapping baseline supplies automatic history; a recorded hash needs its own provenance.
65
+ #: (String) -> PrincipleMappingHistory?
66
+ def mapping_for(key)
67
+ return if @hashes.hash_for(key)
68
+
69
+ @mapping if @mapped.key?(key)
70
+ end
71
+
65
72
  # The hash this tool last knew for the member known by key, and what it was: the last audit's, or else its
66
73
  # hash at the commit that mapped it, with why no audit gave one; neither when it has no baseline.
67
74
  #: (String) -> [String?, Hash[String, untyped]?]
@@ -69,8 +76,9 @@ module Principles
69
76
  if (audited = @hashes.hash_for(key))
70
77
  [audited, { "baseline" => "audit", "commit" => @audit_base }]
71
78
  elsif @mapped.key?(key)
72
- [@mapped[key], { "baseline" => "mapping_commit", "commit" => @mapped_in[key],
73
- "no_audit_because" => @hashes.empty? ? "no_audit_record" : "not_in_last_audit" }]
79
+ mapped = @mapped.fetch(key)
80
+ [mapped.fetch("sha256"), { "baseline" => "mapping_commit", "commit" => mapped.fetch("commit"),
81
+ "no_audit_because" => @hashes.empty? ? "no_audit_record" : "not_in_last_audit" }]
74
82
  else
75
83
  [nil, nil]
76
84
  end
@@ -103,8 +111,11 @@ module Principles
103
111
  # refused when it was asked for, and listed as broken among every map.
104
112
  #: (Project, String, ?listing_all: bool) -> MemberProblems
105
113
  def self.for(project, id, listing_all: false)
106
- members = project.principles.find(id).members
107
- new(members: members, now: project.current_by_key(members), baselines: Baselines.of(project, members))
114
+ map = project.principles.find(id)
115
+ members = map.members
116
+ mapping = PrincipleMappingHistory.load_if_linked(current: map, project: project)
117
+ new(members: members, now: project.current_by_key(members),
118
+ baselines: Baselines.of(project, members, mapping: mapping))
108
119
  rescue Error => e
109
120
  raise unless listing_all
110
121
 
@@ -143,24 +154,26 @@ module Principles
143
154
  # Whether a member an audit or acknowledgement assessed changed since, rather than only a member edited since
144
155
  # the commit that mapped it, which no audit assessed (D44).
145
156
  #: () -> bool
146
- def assessed_change? = changes_to_act_on.any? { |member| assessed?(member) }
157
+ def assessed_change? = assessed_changes.positive?
147
158
 
148
- # Whether a member no audit assessed was edited since the commit that mapped it, which --short exits 4 for
149
- # when it is the only kind of change (D44).
150
- #: () -> bool
151
- def unassessed_change? = changes_to_act_on.any? { |member| !assessed?(member) }
159
+ # How many such members changed, which the session-start hook counts by principle (#1261).
160
+ #: () -> Integer
161
+ def assessed_changes = changes_to_act_on.count { |member| assessed?(member) }
162
+
163
+ # How many members no audit has assessed, compared only with the commit that mapped them, edited or not, which
164
+ # --short counts on stdout (#1254); none while the principle's audits wait on an upgrade.
165
+ #: () -> Integer
166
+ def never_assessed = @baselines.waiting? ? 0 : compared_with.fetch("mapping_commit", 0)
152
167
 
153
168
  # The entries whose own text changed since this tool last knew it, those an audit assessed first. Given a
154
169
  # block, each gains "history", what the block makes of its member, the hash it was compared with, and its
155
- # hash now.
156
- #: () ?{ (Member, String, String) -> Hash[String, untyped] } -> Array[Hash[String, untyped]]
170
+ # hash now and the selected automatic history when that mapping supplies its baseline.
171
+ #: () ?{ (Member, String, String, PrincipleMappingHistory?) -> Hash[String, untyped] }
172
+ #| -> Array[Hash[String, untyped]]
157
173
  def changed(&history)
158
174
  assessed, unassessed = changed_members.partition { |member| assessed?(member) }
159
175
  (assessed + unassessed).map do |member|
160
- listed = entry(member)
161
- next listed unless history
162
-
163
- listed.merge("history" => history.call(member, baseline(member.key).first.to_s, @now[member.key].to_s))
176
+ history ? history_entry(member, history) : entry(member)
164
177
  end
165
178
  end
166
179
 
@@ -201,6 +214,15 @@ module Principles
201
214
  found.map { |member| baseline(member.key).last&.fetch("baseline") || "no_baseline" }.tally.sort.to_h
202
215
  end
203
216
 
217
+ # Whether this readable map has no completed audit, independent of baseline readiness; a newer record is
218
+ # unknown to this copy, rather than evidence that no audit completed.
219
+ #: () -> bool
220
+ def no_completed_audit? = @unreadable.nil? && !@baselines.waiting? && @baselines.no_completed_audit?
221
+
222
+ # Whether this readable map has no members, even when an older full audit completed before it was emptied.
223
+ #: () -> bool
224
+ def empty_map? = @unreadable.nil? && @members.empty?
225
+
204
226
  # Whether the principle awaits its first full audit: none has completed and a member is found, in git one a
205
227
  # commit or an open audit gives a baseline, since until a commit holds its map committing gives its members
206
228
  # one (D58, #731 R7).
@@ -232,6 +254,12 @@ module Principles
232
254
 
233
255
  private
234
256
 
257
+ #: (Member, ^(Member, String, String, PrincipleMappingHistory?) -> Hash[String, untyped]) -> Hash[String, untyped]
258
+ def history_entry(member, history)
259
+ entry(member).merge("history" => history.call(member, baseline(member.key).first.to_s, @now[member.key].to_s,
260
+ @baselines.mapping_for(member.key)))
261
+ end
262
+
235
263
  # The members whose own text changed since this tool last knew it; none in a map that cannot be read.
236
264
  #: () -> Array[Member]
237
265
  def changed_members