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
@@ -0,0 +1,129 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "../error"
5
+ require_relative "../prose_list"
6
+ require_relative "member_file"
7
+ require_relative "line_span"
8
+
9
+ module Principles
10
+ # The domain logic, which loads and saves itself through data/. models/CLAUDE.md owns the boundary.
11
+ module Models
12
+ # A file-selected region's text and full-file character origin, for literal passage matching.
13
+ class MemberRegion
14
+ # How many ambiguous passage openings the refusal names, so one character cannot list the whole file.
15
+ OPENINGS_NAMED = 5 #: Integer
16
+ ENDINGS_NAMED = 5 #: Integer
17
+
18
+ #: (MemberFile, span: LineSpan) -> void
19
+ def initialize(file, span:)
20
+ @path = file.path.dup.freeze #: String
21
+ @text = file.text.dup.freeze #: String
22
+ region = captured_region(span)
23
+ @before = region[0] #: Integer
24
+ @selected = region[1] #: String
25
+ freeze
26
+ end
27
+
28
+ # Existing references use one opening and the first eligible ending, or the opening's whole line.
29
+ #: (String, String?) -> [Integer, Integer, String]
30
+ def passage(start_text, end_text)
31
+ start = only_opening(@selected, start_text, @before)
32
+ found = @selected[start...passage_end(@selected, start, start_text, end_text)].to_s
33
+ line = @text[0, @before + start].to_s.count("\n")
34
+ [line, line + line_count(found), found]
35
+ end
36
+
37
+ # The narrower constraint applied only before persisting a newly authored locator.
38
+ #: (String, String) -> void
39
+ def check_authored_ending(start_text, end_text)
40
+ start = only_opening(@selected, start_text, @before)
41
+ offsets = ending_offsets(start + start_text.length, end_text)
42
+ ambiguous_ending(end_text, offsets) if offsets.length > 1
43
+ end
44
+
45
+ private
46
+
47
+ # Every eligible literal start counts, including endings that overlap a previous ending.
48
+ #: (Integer, String) -> Array[Integer]
49
+ def ending_offsets(after, ending)
50
+ offsets = [] #: Array[Integer]
51
+ while (found = @selected.index(ending, after))
52
+ offsets << (@before + found)
53
+ after = found + 1
54
+ end
55
+ offsets
56
+ end
57
+
58
+ #: (String, Array[Integer]) -> bot
59
+ def ambiguous_ending(ending, offsets)
60
+ candidates = offsets.first(ENDINGS_NAMED).map { ending_candidate(it) }
61
+ raise Error.new("ambiguous_passage_end", "#{ending.inspect} has #{offsets.length} eligible endings",
62
+ "Give a more specific --ending-at closing quote that occurs once after the complete opening.",
63
+ @path).including("eligible_ending_count" => offsets.length, "ending_candidates" => candidates,
64
+ "ending_candidates_omitted" => offsets.length - candidates.length)
65
+ end
66
+
67
+ # Coordinates use characters, consistently with String#index and the complete-opening boundary.
68
+ #: (Integer) -> Hash[String, Integer]
69
+ def ending_candidate(offset)
70
+ prefix = @text[0, offset].to_s
71
+ { "line" => prefix.count("\n") + 1, "column" => offset - (prefix.rindex("\n") || -1) }
72
+ end
73
+
74
+ # Selected text and its character origin from the file-type's inclusive line span.
75
+ #: (LineSpan) -> [Integer, String]
76
+ def captured_region(span)
77
+ lines = @text.lines
78
+ first = span.first_line - 1
79
+ [lines[0...first].to_a.join.length, lines[first...span.last_line].to_a.join.freeze]
80
+ end
81
+
82
+ #: (String) -> Integer
83
+ def line_count(found)
84
+ found.count("\n") + (found.end_with?("\n") ? 0 : 1)
85
+ end
86
+
87
+ #: (String, String, Integer) -> Integer
88
+ def only_opening(text, start_text, before)
89
+ starts = text.enum_for(:scan, start_text).filter_map { Regexp.last_match&.begin(0) }
90
+ return starts.first.to_i if starts.length == 1
91
+
92
+ starts.empty? ? opening_not_found(start_text) : ambiguous_opening(start_text, starts.map { before + it })
93
+ end
94
+
95
+ #: (String) -> bot
96
+ def opening_not_found(start_text)
97
+ raise Error.new("passage_not_found", "The passage starting #{start_text.inspect} is not in the file",
98
+ "Quote the passage's opening text exactly.", @path)
99
+ end
100
+
101
+ #: (String, Array[Integer]) -> bot
102
+ def ambiguous_opening(start_text, offsets)
103
+ raise Error.new("ambiguous_passage",
104
+ "#{start_text.inspect} occurs #{offsets.length} times, at lines #{lines_named(offsets)}",
105
+ "Quote more of the passage's start so it occurs once.", @path)
106
+ end
107
+
108
+ #: (Array[Integer]) -> String
109
+ def lines_named(offsets)
110
+ named = offsets.first(OPENINGS_NAMED).map { (@text[0, it].to_s.count("\n") + 1).to_s }
111
+ more = offsets.length - named.length
112
+ more.zero? ? ProseList.new(named).to_s : "#{named.join(", ")} and #{more} more"
113
+ end
114
+
115
+ #: (String, Integer, String, String?) -> Integer
116
+ def passage_end(text, start, start_text, end_text)
117
+ return (text.index("\n", start) || (text.length - 1)) + 1 unless end_text
118
+
119
+ found = text.index(end_text, start + start_text.length)
120
+ unless found
121
+ raise Error.new("passage_end_not_found", "No #{end_text.inspect} after #{start_text.inspect}",
122
+ "Quote the passage's closing text exactly.", @path)
123
+ end
124
+
125
+ found + end_text.length
126
+ end
127
+ end
128
+ end
129
+ end
@@ -8,12 +8,15 @@ module Principles
8
8
  # fills in, which Views::MemberRepairs prints as move-member commands. A member whose file is gone moves to the
9
9
  # path the agent finds, keeping its own section, passage or query, so a rename keeps its audit (#268). A member
10
10
  # whose place in its file is gone moves within it, or into another file its text moved to (#378); a passage keeps
11
- # its anchors, so a move cannot silently widen it to its whole new section (#251), but not an opening text that is
12
- # gone, which a move repeating it would fail on again as printed (#1104); and it is given no section a passage
13
- # located by its opening alone never had (#1134). Which fits is the agent's to judge.
11
+ # its anchors, so a move cannot silently widen it to its whole new section (#251), but not an opening or ending text
12
+ # that is gone, which a move repeating it would fail on again as printed (#1104, #1139); and it is given no section
13
+ # a passage located by its opening alone never had (#1134). Which fits is the agent's to judge.
14
14
  class MemberRepairMoves
15
15
  # The locator fields a member may give beside its path.
16
16
  LOCATOR = %w[section starting_at ending_at query].freeze #: Array[String]
17
+ # The passage anchor each refusal code says is no longer in its file, which a move fills in afresh.
18
+ LOST_ANCHOR = { "passage_not_found" => %w[starting_at],
19
+ "passage_end_not_found" => %w[ending_at] }.freeze #: Hash[String, Array[String]]
17
20
 
18
21
  # member is the member's locator fields, as its map holds them; code is that of the refusal saying why it cannot
19
22
  # be found.
@@ -30,11 +33,10 @@ module Principles
30
33
  return [move(@member, %w[path])] if @code == "unreadable_file"
31
34
  return [move({}, %w[query])] if @member["query"]
32
35
 
33
- kept = opening_lost? ? %w[ending_at] : %w[starting_at ending_at]
34
- anchors = given(@member, kept)
35
- opening = opening_lost? ? %w[starting_at] : []
36
+ lost = LOST_ANCHOR.fetch(@code.to_s, [])
37
+ anchors = given(@member, %w[starting_at ending_at] - lost)
36
38
  section = @member.key?("section") ? %w[section] : []
37
- [move(anchors, [*opening, *section]), move(anchors, [*opening, "path", *section])]
39
+ [move(anchors, [*lost, *section]), move(anchors, [*lost, "path", *section])]
38
40
  end
39
41
 
40
42
  private
@@ -43,10 +45,6 @@ module Principles
43
45
  #: (Hash[String, untyped], Array[String]) -> Hash[String, untyped]
44
46
  def given(member, fields) = fields.filter_map { [it, member[it]] unless member[it].nil? }.to_h.freeze
45
47
 
46
- # Whether the passage's opening text is no longer in its file.
47
- #: () -> bool
48
- def opening_lost? = @code == "passage_not_found"
49
-
50
48
  #: (Hash[String, untyped], Array[String]) -> Hash[String, untyped]
51
49
  def move(keep, fill) = { "keep" => keep, "fill" => fill }
52
50
  end
@@ -111,8 +111,8 @@ module Principles
111
111
  end
112
112
 
113
113
  # An observed_at is a UTC time in audited_at's form, as the record dates every other sighting, to compare; a
114
- # date alone, an offset or fractional seconds cannot, nor a time no calendar has (#699). Null or none is this
115
- # audit's.
114
+ # date alone or fractional seconds cannot, nor a time no calendar has (#699). One given with an offset arrives
115
+ # here already in that form (#1187). Null or none is this audit's.
116
116
  #: () -> void
117
117
  def refuse_unshaped_observed_at
118
118
  observed_at = @assessment["observed_at"]
@@ -125,15 +125,12 @@ module Principles
125
125
  "problem" => "must be a UTC time like #{SHAPE_EXAMPLE}" }])
126
126
  end
127
127
 
128
- # The repair for an observed_at not in audited_at's form: the value it names exactly, to copy, when that is no
129
- # later than the answer, which would refuse it in turn; otherwise the form, and how to date a date alone (#801).
128
+ # The repair for an observed_at not in audited_at's form: for a date alone, its midnight, to copy, when that is
129
+ # no later than the answer, which would refuse it in turn; otherwise the form to follow (#801).
130
130
  #: (AnsweredTime) -> String
131
131
  def shape_repair(given)
132
132
  exact = given.exact_utc
133
- return FORM_REPAIR if exact.nil? || AnsweredTime.new(exact).after?(@answered_at)
134
- unless given.date_alone?
135
- return %(Give "observed_at" as "#{exact}", the same time in UTC, #{OR_NULL_FOR_THIS_AUDIT})
136
- end
133
+ return FORM_REPAIR if !given.date_alone? || exact.nil? || AnsweredTime.new(exact).after?(@answered_at)
137
134
 
138
135
  %(Give "observed_at" as "#{exact}", that date at 00:00:00Z, and say in observation that the time is ) +
139
136
  "unknown, #{OR_NULL_FOR_THIS_AUDIT}"
@@ -8,8 +8,21 @@ module Principles
8
8
  # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
9
9
  module Models
10
10
  # Each of a map's members by id, where it is in its file now and the hash of what it says there, as
11
- # Project#snapshots located them all.
11
+ # the project located them all.
12
12
  class MemberSnapshots
13
+ # Locate every member before refusing, so one broken reference never hides another.
14
+ #: (Project, Principle) -> MemberSnapshots
15
+ def self.read(project, map)
16
+ broken = [] #: Array[Hash[String, untyped]]
17
+ found = map.members.to_h do |member|
18
+ [member.id, project.locate(member.locator)]
19
+ rescue Error => e
20
+ broken << { "id" => member.id }.merge(e.payload.except("status"))
21
+ [member.id, {}]
22
+ end
23
+ broken.empty? ? new(found) : project.refuse_broken(map.id, broken)
24
+ end
25
+
13
26
  #: (Hash[String, Hash[String, untyped]]) -> void
14
27
  def initialize(by_id)
15
28
  @by_id = by_id.freeze #: Hash[String, Hash[String, untyped]]
@@ -0,0 +1,56 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "../error"
5
+ require_relative "member"
6
+ require_relative "located_text"
7
+
8
+ module Principles
9
+ module Models
10
+ # Members in complete sections meeting the new section at a literal line boundary, in other unaudited maps.
11
+ class MembersInConsecutiveSections
12
+ #: (Project, Member) -> void
13
+ def initialize(project, member)
14
+ @project = project
15
+ @member = member
16
+ @located = project.located_text(member.locator) #: LocatedText
17
+ @records = project.audit_records #: AuditRecords
18
+ freeze
19
+ end
20
+
21
+ # Literal section boundaries are not evidence that the rules belong to one principle.
22
+ #: (except: String) -> Array[Hash[String, untyped]]
23
+ def elsewhere(except:)
24
+ return [] unless @member.locator.complete_section? && @records.known_no_completed_audit?(except)
25
+
26
+ maps = @project.principles
27
+ (maps.ids - [except]).flat_map { |id| in_map(maps, id) }
28
+ end
29
+
30
+ private
31
+
32
+ # Optional feedback leaves unreadable maps to list-member-problems rather than blocking the write.
33
+ #: (MappedPrinciples, String) -> Array[Hash[String, untyped]]
34
+ def in_map(maps, id)
35
+ return [] unless @records.known_no_completed_audit?(id)
36
+
37
+ maps.find(id).members.to_a.filter_map { |member| consecutive(member, id) }
38
+ rescue Error
39
+ []
40
+ end
41
+
42
+ #: (Member, String) -> Hash[String, untyped]?
43
+ def consecutive(member, id)
44
+ return unless member.locator.complete_section? && @project.same_file?(member.path, @located.path)
45
+
46
+ found = @project.located_text(member.locator)
47
+ return unless @located.span.consecutive_with?(found.span)
48
+
49
+ { "principle_id" => id, "member_id" => member.id, "path" => member.path,
50
+ "role" => member.role }.merge(found.lines)
51
+ rescue Error
52
+ nil
53
+ end
54
+ end
55
+ end
56
+ end
@@ -46,6 +46,20 @@ module Principles
46
46
  # writes the map with its first member (T74).
47
47
  #: () -> Hash[String, untyped]
48
48
  def self.starting_fields = { "description" => DESCRIPTION, "members" => [], "activation" => ACTIVATION.to_h }
49
+
50
+ # Whether this is the built-in principle whose first member setup maps.
51
+ #: (String) -> bool
52
+ def self.id?(id) = id == ID
53
+
54
+ # Setup's add-member writes this map with its first member, preserving its shipped description/activation.
55
+ #: (String) -> void
56
+ def self.refuse_explicit_creation(id)
57
+ return unless id?(id)
58
+
59
+ raise Error.new("meta_principle_id",
60
+ "#{id} is set up by show-meta-principle-passage, whose printed add-member creates its map " \
61
+ "with its first member", SET_UP)
62
+ end
49
63
  end
50
64
  end
51
65
  end
@@ -47,8 +47,8 @@ module Principles
47
47
  def refusal(message, rerun: nil, note: "")
48
48
  Error.new("broken_members", message,
49
49
  "For each member listed, say whether it moved or is gone, and run the matching command in its " \
50
- "repair_with: the move-member, with its NEW_PATH, HEADING, NEW_OPENING_TEXT or QUERY filled in, if " \
51
- "it moved, or the remove-member if it is gone. #{note}#{REPAIR}" \
50
+ "repair_with: the move-member, with its NEW_PATH, HEADING, NEW_OPENING_TEXT, NEW_ENDING_TEXT or " \
51
+ "QUERY filled in, if it moved, or the remove-member if it is gone. #{note}#{REPAIR}" \
52
52
  "#{rerun ? "\nThen run it again: #{rerun}" : " Then run the command again."}")
53
53
  .including(**({ "principle" => @principle } if @principle), "members" => members, "missing_files" => to_a)
54
54
  end
@@ -66,7 +66,7 @@ module Principles
66
66
  MemberAssessments::PLACEHOLDER, AssessmentAnswer::COLLECTIVE_PLACEHOLDER, RecentDecision::PLACEHOLDERS.to_h,
67
67
  ReportedCheck::PLACEHOLDERS.to_h, ReportedCheck::UNCHANGED_PLACEHOLDERS.to_h,
68
68
  LoadingDecision::PLACEHOLDERS.to_h, PriorDecisionFields::TEMPLATE_PLACEHOLDERS,
69
- Disposition::TEMPLATE_PLACEHOLDERS, Acknowledgement::PLACEHOLDERS.to_h
69
+ Disposition::TEMPLATE_PLACEHOLDERS, Acknowledgement::PLACEHOLDERS.to_h, Acknowledgement::EXIT_STATUS_PLACEHOLDER
70
70
  ].then { Strings.new(it).by_pointer.values.to_set }.freeze #: Set[String]
71
71
 
72
72
  # Each string in template, a request's template as printed, which its answer may send back.
@@ -34,32 +34,46 @@ module Principles
34
34
 
35
35
  # The listing with its files' outlines cut, one cut of one file at a time in cut order, until what the block says
36
36
  # it prints fits BYTES, each file whose items were cut named under outlines_cut. If every cut is made and it
37
- # still does not fit, every file cut is named.
37
+ # still does not fit, every file cut is named, and range_outlines_bytes gives what the outlines of the files a
38
+ # range changed print, when there are any: they are never left out, and which to drop is the agent's call (#1171).
38
39
  #: (Hash[String, untyped]) { (Hash[String, untyped]) -> Integer } -> Hash[String, untyped]
39
40
  def cut(listing, &printed_bytes)
40
- cut_order(listing).reduce(listing) do |shorter, (path, cuts)|
41
+ shortest = cut_order(listing).reduce(listing) do |shorter, (path, cuts)|
41
42
  return shorter if printed_bytes.call(shorter) <= BYTES
42
43
 
43
44
  cut_file(shorter, path, cuts)
44
45
  end
46
+ printed_bytes.call(shortest) <= BYTES ? shortest : past_budget(shortest)
45
47
  end
46
48
 
47
49
  private
48
50
 
51
+ # The listing every cut left past BYTES, naming what its range's outlines print, when it lists any.
52
+ #: (Hash[String, untyped]) -> Hash[String, untyped]
53
+ def past_budget(listing)
54
+ range = listing.fetch("files").select { |file| file["changed_in_range"] }
55
+ return listing if range.empty?
56
+
57
+ bytes = range.sum { |file| JSON.generate(file.slice(*OUTLINE_FIELDS)).bytesize }
58
+ bytes > BYTES ? listing.merge("range_outlines_bytes" => bytes) : listing
59
+ end
60
+
49
61
  # Each cut that shortens a file, as its path and how many of CandidateOutline::CUTS it then has made. Every file
50
- # but the root entry files comes first, since a host loads those in every session, so they keep theirs longest,
51
- # in EntryFiles' order. Within each group, each cut is made across the files in CUTS' order: those with a
52
- # description first, since the agent still reads what each is for, then the deepest path, then the most bytes
53
- # the cut saves (#1155, review F5).
62
+ # but the root entry files and those marked changed_in_range comes first; then the changed files, which a survey
63
+ # of a change is about (#1171); last the entry files, since a host loads those in every session, so they keep
64
+ # theirs longest, in EntryFiles' order. Within each group, each cut is made across the files in CUTS' order:
65
+ # those with a description first, since the agent still reads what each is for, then the deepest path, then the
66
+ # most bytes the cut saves (#1155, review F5).
54
67
  #: (Hash[String, untyped]) -> Array[[String, Integer]]
55
68
  def cut_order(listing)
56
69
  outlined = listing.fetch("files").select { |file| @outlines.key?(file.fetch("path")) }
57
- outlined.group_by { |file| entry_rank(file.fetch("path")) }.sort.flat_map do |rank, files|
70
+ outlined.group_by { |file| kept_rank(file) }.sort.flat_map do |rank, files|
58
71
  (1..most_cuts(rank)).flat_map { |cuts| cuts_across(files, cuts) }
59
72
  end
60
73
  end
61
74
 
62
- # How many of CUTS are made on a file of this entry rank: on an entry file every one but leaving its outline out.
75
+ # How many of CUTS are made on a file of this rank: on a changed or entry file every one but leaving its outline
76
+ # out.
63
77
  #: (Integer) -> Integer
64
78
  def most_cuts(rank) = rank.zero? ? CandidateOutline::CUTS.length : CandidateOutline::ENTRY_FILE_CUTS
65
79
 
@@ -77,13 +91,14 @@ module Principles
77
91
  [file.key?("description") ? 0 : 1, -path.count("/"), -saved(path, cuts), path]
78
92
  end
79
93
 
80
- # 0 for a file that is not a root entry file, then each entry file in EntryFiles::PATHS reversed, so the first,
81
- # CLAUDE.md, keeps its items longest.
82
- #: (String) -> Integer
83
- def entry_rank(path)
84
- index = EntryFiles::PATHS.index(path) or return 0
94
+ # 0 for a file that is neither a root entry file nor changed in the range, 1 for a changed one, then each entry
95
+ # file in EntryFiles::PATHS reversed, so the first, CLAUDE.md, keeps its items longest.
96
+ #: (Hash[String, untyped]) -> Integer
97
+ def kept_rank(file)
98
+ index = EntryFiles::PATHS.index(file.fetch("path"))
99
+ return 1 + EntryFiles::PATHS.length - index if index
85
100
 
86
- EntryFiles::PATHS.length - index
101
+ file["changed_in_range"] ? 1 : 0
87
102
  end
88
103
 
89
104
  # The bytes the cuts'th cut of path's outline saves over the cuts made before it.
@@ -4,6 +4,7 @@
4
4
  require "json"
5
5
  require_relative "../error"
6
6
  require_relative "../data/schema_check"
7
+ require_relative "../data/map_files"
7
8
  require_relative "member"
8
9
  require_relative "principle_members"
9
10
  require_relative "activation"
@@ -20,6 +21,15 @@ module Principles
20
21
  #: String
21
22
  attr_reader :id
22
23
 
24
+ # A parsed map validates repeated member identities beside the member state it constructs.
25
+ #: (String, Hash[String, untyped], files: Data::MapFiles, ?commit: String?) -> Principle
26
+ def self.from_map(id, fields, files:, commit: nil)
27
+ map = new(id, fields)
28
+ where = commit ? "#{files.relative(map.id)} at commit #{commit[0, 12]}" : files.relative(map.id)
29
+ map.members.refuse_repeats(where: where, file: files.relative(map.id))
30
+ map
31
+ end
32
+
23
33
  #: (String, Hash[String, untyped]) -> void
24
34
  def initialize(id, fields)
25
35
  @id = id
@@ -37,17 +47,14 @@ module Principles
37
47
  @fields["description"]
38
48
  end
39
49
 
50
+ # Its identity and stated rule, for a listing that compares principles without their members.
51
+ #: () -> Hash[String, untyped]
52
+ def description_with_id = { "id" => @id, "description" => description }
53
+
40
54
  # Its members as Member values, which say each one's id, key, place and check.
41
55
  #: PrincipleMembers
42
56
  attr_reader :members
43
57
 
44
- # Each member in one of paths, by its id and path, as show-meta-principle-passage reports the members already in
45
- # an entry file, which the agent judges against the passage (D68).
46
- #: (Array[String]) -> Array[Hash[String, String]]
47
- def members_in(paths)
48
- @members.in_files(paths).map { |member| { "id" => member.id, "path" => member.path } }
49
- end
50
-
51
58
  # Whether this principle's audit keeps the release of principles it ran under, and its next full audit states
52
59
  # what changed since: only the meta-principle's, which judges whether the keeping that release ships still
53
60
  # works (D68).
@@ -0,0 +1,48 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "../error"
5
+ require_relative "captured_head"
6
+
7
+ module Principles
8
+ # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
9
+ module Models
10
+ # One committed principle file's exact bytes, or verified absence across its folder alternatives.
11
+ class PrincipleFileRevision
12
+ #: (String, String, paths: Array[String], captured: CapturedHead) -> PrincipleFileRevision
13
+ def self.load(id, commit, paths:, captured:) = new(id, commit, paths, captured)
14
+
15
+ #: String?
16
+ attr_reader :text
17
+
18
+ #: (String, String, Array[String], CapturedHead) -> void
19
+ def initialize(id, commit, paths, captured)
20
+ @id = id.dup.freeze #: String
21
+ @commit = commit.dup.freeze #: String
22
+ @paths = paths.map { |path| path.dup.freeze }.freeze #: Array[String]
23
+ @text = selected_text(captured)&.dup&.freeze #: String?
24
+ freeze
25
+ end
26
+
27
+ private
28
+
29
+ #: (CapturedHead) -> String?
30
+ def selected_text(captured)
31
+ entries = @paths.filter_map { |path| captured.raw_entry_at(@commit, path) }
32
+ raise unavailable if entries.length > 1
33
+
34
+ entry = entries.first or return nil
35
+ raise unavailable unless entry.fetch("type") == "blob" && %w[100644 100755].include?(entry.fetch("mode"))
36
+
37
+ entry.fetch("bytes")
38
+ end
39
+
40
+ #: () -> Error
41
+ def unavailable
42
+ Error.new("git_history_unavailable", "The committed pair of #{@id} cannot be selected at #{@commit}",
43
+ "Preserve all map and audit copies and inspect their exact committed entries.")
44
+ .including("principle_id" => @id, "commit" => @commit)
45
+ end
46
+ end
47
+ end
48
+ end
@@ -7,31 +7,47 @@ require_relative "member_history"
7
7
  require_relative "mapped_principles"
8
8
  require_relative "placement"
9
9
  require_relative "project_history"
10
+ require_relative "member_mapping_run"
11
+ require_relative "principle_mapping_history"
10
12
 
11
13
  module Principles
12
14
  # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
13
15
  module Models
14
- # One principle's members as the commits that changed its map held them, walking back from HEAD on its
15
- # first-parent line. Each member's run is the unbroken stretch of those commits whose map holds a member
16
- # with its key, so its oldest commit is the one that mapped the member where it is (#243). The walk stops
17
- # at a map it cannot read, so a run may start later than the true mapping; that can only hide an older change.
16
+ # One principle's member mappings, newest first: each key's run ends where a selected map no longer holds it.
17
+ # Supplied certified mapping history selects earlier IDs and refuses unprovable reads. The legacy direct-ID
18
+ # walk stops at unreadable maps, which can only hide an older change (#243).
18
19
  class PrincipleHistory
19
- #: (id: String, history: ProjectHistory, principles: MappedPrinciples) -> void
20
- def initialize(id:, history:, principles:)
20
+ #: (id: String, history: ProjectHistory, principles: MappedPrinciples, ?mapping: PrincipleMappingHistory?) -> void
21
+ def initialize(id:, history:, principles:, mapping: nil)
21
22
  @id = id
22
23
  @history = history
23
24
  @principles = principles
25
+ @mapping = mapping #: PrincipleMappingHistory?
24
26
  freeze
25
27
  end
26
28
 
29
+ # A request belongs to this current ID only while its map stayed present since its supplied base.
30
+ # Earlier-ID member history cannot extend a retired current-ID request's lifetime.
31
+ #: (String?) -> void
32
+ def confirm_current_id_since(base)
33
+ return unless base
34
+
35
+ paths = [@principles.file(@id), @principles.earlier_file(@id)]
36
+ return if @history.commits_touching_since(base, paths).all? do |commit|
37
+ paths.any? { |path| @history.entry_at?(commit, path) }
38
+ end
39
+
40
+ raise Error.new("stale_request", "#{@id}'s map was absent in committed history after this request began",
41
+ "Start the audit again and answer the new request: #{Error::EXECUTABLE} start-audit #{@id}")
42
+ end
43
+
27
44
  # Each key's run, newest first, as [commit, the member's fields at that commit]; a key HEAD's map does
28
45
  # not hold has an empty run. Each commit's map is read once for every key.
29
46
  #: (Array[String]) -> Hash[String, Array[[String, Hash[String, untyped]]]]
30
47
  def runs(keys)
31
48
  runs = keys.to_h { |key| [key, []] } #: Hash[String, Array[[String, Hash[String, untyped]]]]
32
49
  open = keys.dup
33
- @history.commits_touching(map_paths).each do |commit|
34
- sha = commit.fetch("sha")
50
+ mapping_commits.each do |sha|
35
51
  members = members_at(sha) or break
36
52
  # A key the map at sha does not hold ends its run.
37
53
  open.select! { |key| members.key?(key) && runs.fetch(key).push([sha, members.fetch(key)]) }
@@ -45,11 +61,8 @@ module Principles
45
61
  #: (Array[String]) -> Hash[String, Hash[String, String]]
46
62
  def mapped_baselines(keys)
47
63
  runs(keys).each_with_object({}) do |(key, run), found|
48
- run.reverse_each do |sha, fields|
49
- hash = @history.hashes_by_key_at(sha, [fields])[key] or next
50
- found[key] = { "commit" => sha, "sha256" => hash }
51
- break
52
- end
64
+ baseline = MemberMappingRun.new(key, run).baseline(@history)
65
+ found[key] = baseline if baseline
53
66
  end
54
67
  end
55
68
 
@@ -62,14 +75,19 @@ module Principles
62
75
  hashes = {} #: Hash[String, String?]
63
76
  hash_at = ->(revision) { hashes.fetch(revision) { hashes[revision] = hash_at(key, now_at, revision) } }
64
77
  commits = @history.commits_touching([*paths_of(member), *map_paths])
65
- MemberHistory.new(baseline: baseline, now: now, commits: commits, hash_at: hash_at).to_h
78
+ MemberHistory.new(baseline: baseline, now: now, commits: commits, head: @history.head || "HEAD",
79
+ hash_at: hash_at).to_h
66
80
  end
67
81
 
68
82
  # The map as commit holds it, such as the one an audit began from, refused as MappedPrinciples refuses its text;
69
83
  # a commit before the folder took its name now holds it under the earlier name (#536), and a commit that lacks
70
- # the map under both gives empty text, which is refused as unparseable.
84
+ # the map under both gives empty text, which is refused as unparseable in the legacy path. A supplied
85
+ # mapping history owns strict earlier-ID selection and its refusal outside the proven interval.
71
86
  #: (String) -> Principle
72
87
  def map_at(commit)
88
+ mapping = @mapping
89
+ return mapping.map_at_revision(commit, captured: @history.captured_head) if mapping
90
+
73
91
  text = map_paths.lazy.filter_map { |path| @history.text_at(commit, path) }.first
74
92
  @principles.parsed(@id, text.to_s, commit: commit)
75
93
  end
@@ -81,6 +99,8 @@ module Principles
81
99
  def startable_at_head?
82
100
  head = @history.head or return false
83
101
  map_at(head).auditable?
102
+ rescue PrinciplePublication::ProofError
103
+ raise
84
104
  rescue Error
85
105
  false
86
106
  end
@@ -122,9 +142,14 @@ module Principles
122
142
 
123
143
  private
124
144
 
125
- # The map's file now and under the folder's earlier name, so a walk of its history reaches back past the move.
126
145
  #: () -> Array[String]
127
- def map_paths = [@principles.file(@id), @principles.earlier_file(@id)]
146
+ def mapping_commits
147
+ @mapping&.revisions&.keys || @history.commits_touching(map_paths).map { |commit| commit.fetch("sha") }
148
+ end
149
+
150
+ # Map files used by member history: certified selected IDs, or the legacy current ID, in either folder.
151
+ #: () -> Array[String]
152
+ def map_paths = @mapping&.map_paths || [@principles.file(@id), @principles.earlier_file(@id)]
128
153
 
129
154
  # The member's placements at commit, as that commit's map placed it and as now_at does, once each.
130
155
  #: (String, String, Hash[String, untyped]) -> Array[Placement]
@@ -153,11 +178,13 @@ module Principles
153
178
  @history.hashes_by_key_at(revision, [fields])[key] || @history.hashes_by_key_at(revision, [now_at])[key]
154
179
  end
155
180
 
156
- # Each member the map at commit holds, by key, or nil when that commit lacks the map or
157
- # MappedPrinciples refuses it.
181
+ # Each member the map at commit holds, by key. Legacy unreadable maps give nil; certified proof failures
182
+ # remain failures rather than shortening a run or inventing an absent placement.
158
183
  #: (String) -> Hash[String, Hash[String, untyped]]?
159
184
  def members_at(commit)
160
185
  map_at(commit).members.to_h { |member| [member.key, member.to_h] }
186
+ rescue PrinciplePublication::ProofError
187
+ raise
161
188
  rescue Error
162
189
  nil
163
190
  end