principles 0.0.4 → 0.0.5

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 (137) hide show
  1. checksums.yaml +4 -4
  2. data/lib/principles/cli.rb +3 -1
  3. data/lib/principles/command_line.rb +35 -7
  4. data/lib/principles/command_routes.rb +4 -3
  5. data/lib/principles/controllers/acknowledgement.rb +80 -14
  6. data/lib/principles/controllers/answer.rb +42 -75
  7. data/lib/principles/controllers/answer_phase.rb +54 -0
  8. data/lib/principles/controllers/assessment.rb +33 -9
  9. data/lib/principles/controllers/audit.rb +17 -6
  10. data/lib/principles/controllers/audit_completion.rb +6 -4
  11. data/lib/principles/controllers/command.rb +3 -2
  12. data/lib/principles/controllers/correction.rb +19 -9
  13. data/lib/principles/controllers/full_audit_request.rb +10 -0
  14. data/lib/principles/controllers/inventory.rb +25 -20
  15. data/lib/principles/controllers/member_problems_report.rb +11 -4
  16. data/lib/principles/controllers/members_a_commit_changed.rb +2 -1
  17. data/lib/principles/controllers/members_to_assess.rb +20 -12
  18. data/lib/principles/controllers/repoint.rb +18 -22
  19. data/lib/principles/data/bounded_file.rb +9 -4
  20. data/lib/principles/data/command-steps.json +9 -9
  21. data/lib/principles/data/project_files.rb +11 -8
  22. data/lib/principles/data/schema_check.rb +16 -6
  23. data/lib/principles/data/schemas/acknowledgement-answer.schema.json +17 -3
  24. data/lib/principles/data/schemas/answer-guide.schema.json +5 -10
  25. data/lib/principles/data/schemas/audit-record.schema.json +10 -1
  26. data/lib/principles/data/schemas/command-steps.schema.json +2 -1
  27. data/lib/principles/invocation_options.rb +1 -1
  28. data/lib/principles/models/acknowledgement.rb +73 -84
  29. data/lib/principles/models/acknowledgement_refusal.rb +90 -0
  30. data/lib/principles/models/answer_place.rb +4 -0
  31. data/lib/principles/models/assessment_answer.rb +0 -45
  32. data/lib/principles/models/assessment_left_as_printed.rb +81 -0
  33. data/lib/principles/models/audit_record.rb +9 -17
  34. data/lib/principles/models/audit_records.rb +19 -16
  35. data/lib/principles/models/candidate_files.rb +30 -7
  36. data/lib/principles/models/candidate_inventory.rb +23 -6
  37. data/lib/principles/models/candidate_listing.rb +12 -3
  38. data/lib/principles/models/candidate_outline.rb +195 -0
  39. data/lib/principles/models/carried_member.rb +42 -0
  40. data/lib/principles/models/check_since_last_audit.rb +16 -3
  41. data/lib/principles/models/command_steps.rb +17 -1
  42. data/lib/principles/models/declared_map_changes.rb +5 -4
  43. data/lib/principles/models/frontmatter_fields.rb +9 -8
  44. data/lib/principles/models/instruction_named.rb +35 -0
  45. data/lib/principles/models/left_as_printed.rb +39 -0
  46. data/lib/principles/models/member_assessments.rb +9 -31
  47. data/lib/principles/models/member_file.rb +35 -11
  48. data/lib/principles/models/member_hashes_as_written.rb +10 -6
  49. data/lib/principles/models/member_lines.rb +63 -15
  50. data/lib/principles/models/member_problems.rb +24 -18
  51. data/lib/principles/models/member_repair_moves.rb +54 -0
  52. data/lib/principles/models/missing_files.rb +2 -2
  53. data/lib/principles/models/open_findings_elsewhere.rb +4 -0
  54. data/lib/principles/models/other_templates_placeholders.rb +81 -42
  55. data/lib/principles/models/outline_budget.rb +127 -0
  56. data/lib/principles/models/outline_group.rb +31 -0
  57. data/lib/principles/models/outline_section.rb +56 -0
  58. data/lib/principles/models/principle_members.rb +11 -0
  59. data/lib/principles/models/prior_decision_fields.rb +3 -2
  60. data/lib/principles/models/prior_finding_decision.rb +14 -4
  61. data/lib/principles/models/project.rb +10 -19
  62. data/lib/principles/models/quoted_change.rb +137 -0
  63. data/lib/principles/models/record_format.rb +6 -2
  64. data/lib/principles/models/recorded_entry.rb +6 -0
  65. data/lib/principles/models/recorded_hashes.rb +7 -3
  66. data/lib/principles/models/recorded_members.rb +27 -15
  67. data/lib/principles/models/refusal_before_shape.rb +21 -0
  68. data/lib/principles/models/repoint.rb +13 -0
  69. data/lib/principles/models/reported_check.rb +3 -13
  70. data/lib/principles/models/sent_answer.rb +51 -0
  71. data/lib/principles/models/shape_problems_beside.rb +40 -0
  72. data/lib/principles/models/submitted_answer.rb +34 -0
  73. data/lib/principles/models/template_placeholders.rb +34 -4
  74. data/lib/principles/models/text_within.rb +55 -0
  75. data/lib/principles/models/unchecked_members.rb +76 -0
  76. data/lib/principles/models/write_up_opening.rb +21 -4
  77. data/lib/principles/session_start_options.rb +2 -2
  78. data/lib/principles/start_audit_options.rb +19 -4
  79. data/lib/principles/version.rb +2 -2
  80. data/lib/principles/views/acknowledgement_result.rb +15 -7
  81. data/lib/principles/views/assessed_members.rb +43 -10
  82. data/lib/principles/views/assessment_request.rb +89 -30
  83. data/lib/principles/views/audit_result.rb +19 -2
  84. data/lib/principles/views/candidate_files_result.rb +41 -19
  85. data/lib/principles/views/check_notes.rb +44 -0
  86. data/lib/principles/views/check_result_template.rb +14 -23
  87. data/lib/principles/views/instruction.rb +74 -0
  88. data/lib/principles/views/json_with_entries_on_lines.rb +51 -0
  89. data/lib/principles/views/loads_to_check_result.rb +2 -2
  90. data/lib/principles/views/map_result.rb +2 -2
  91. data/lib/principles/views/member_change_since_baseline.rb +22 -5
  92. data/lib/principles/views/member_repairs.rb +21 -52
  93. data/lib/principles/views/members_a_commit_changed.rb +16 -6
  94. data/lib/principles/views/one_member_check_result_template.rb +43 -0
  95. data/lib/principles/views/open_findings_elsewhere_result.rb +3 -3
  96. data/lib/principles/views/open_findings_line.rb +77 -23
  97. data/lib/principles/views/principle_read_result.rb +20 -28
  98. data/lib/principles/views/prior_findings_result.rb +23 -15
  99. data/lib/principles/views/prompts/acknowledge-a-change/run-the-check-and-report-it-passing.md +3 -0
  100. data/lib/principles/views/prompts/acknowledge-a-change/say-whether-the-change-keeps-the-meaning.md +6 -12
  101. data/lib/principles/views/prompts/assess-every-member/answer-guide.json +5 -10
  102. data/lib/principles/views/prompts/assess-every-member/assess-each-member-and-the-set.md +0 -4
  103. data/lib/principles/views/prompts/assess-every-member/claim-an-earlier-first-raised-at.md +7 -0
  104. data/lib/principles/views/prompts/assess-every-member/decide-each-prior-finding.md +4 -16
  105. data/lib/principles/views/prompts/assess-every-member/judge-the-set-with-carried-assessments.md +5 -10
  106. data/lib/principles/views/prompts/assess-every-member/keep-or-change-what-a-deferral-waits-on.md +4 -0
  107. data/lib/principles/views/prompts/assess-every-member/read-a-compact-carried-member.md +8 -0
  108. data/lib/principles/views/prompts/assess-every-member/read-a-trimmed-instruction-whole.md +1 -0
  109. data/lib/principles/views/prompts/assess-every-member/review-a-new-or-changed-check-before-running-it.md +9 -0
  110. data/lib/principles/views/prompts/assess-every-member/say-what-observed-means.md +4 -0
  111. data/lib/principles/views/prompts/dispose-of-each-finding/fix-or-record-each-finding.md +4 -3
  112. data/lib/principles/views/prompts/find-a-principle-to-map/ask-the-owner-after-the-table.md +1 -0
  113. data/lib/principles/views/prompts/find-a-principle-to-map/ask-which-principle-to-map.md +2 -0
  114. data/lib/principles/views/prompts/find-a-principle-to-map/choose-one-principle.md +9 -5
  115. data/lib/principles/views/prompts/find-a-principle-to-map/leave-test-data-out-of-the-survey.md +1 -0
  116. data/lib/principles/views/prompts/find-a-principle-to-map/read-a-cut-section-back.md +3 -0
  117. data/lib/principles/views/prompts/find-a-principle-to-map/read-left-out-files.md +3 -0
  118. data/lib/principles/views/prompts/learn-what-a-command-does/list-candidate-files.md +21 -6
  119. data/lib/principles/views/prompts/learn-what-a-command-does/list-member-problems.md +4 -4
  120. data/lib/principles/views/prompts/learn-what-a-command-does/list-open-findings.md +10 -11
  121. data/lib/principles/views/prompts/learn-what-a-command-does/list-principles.md +1 -2
  122. data/lib/principles/views/prompts/learn-what-a-command-does/show-answer-check.md +1 -1
  123. data/lib/principles/views/prompts/learn-what-a-command-does/show-meta-principle-passage.md +5 -5
  124. data/lib/principles/views/prompts/learn-what-a-command-does/start-audit.md +24 -24
  125. data/lib/principles/views/prompts/learn-what-a-command-does/submit-answer.md +3 -3
  126. data/lib/principles/views/prompts/report-after-a-commit/the-principles-skill-is-where-to-start.md +1 -0
  127. data/lib/principles/views/prompts/set-principles-up/keep-them-held-between-audits.md +2 -0
  128. data/lib/principles/views/prompts/set-principles-up/name-the-step-each-member-lacks.md +2 -0
  129. data/lib/principles/views/prompts/set-principles-up/place-the-passage-with-the-user.md +9 -6
  130. data/lib/principles/views/prompts/start-a-session/see-the-earlier-line.md +2 -0
  131. data/lib/principles/views/record_waiting_on_an_upgrade.rb +3 -3
  132. data/lib/principles/views/request_part.rb +16 -8
  133. data/lib/principles/views/request_parts.rb +4 -0
  134. metadata +39 -5
  135. data/lib/principles/views/prompts/find-a-principle-to-map/ask-the-owner-before-mapping.md +0 -1
  136. data/lib/principles/views/prompts/report-after-a-commit/the-map-and-audit-skill-says-how-to-audit-these-members.md +0 -1
  137. data/lib/principles/views/prompts/set-principles-up/set-up-first.md +0 -2
@@ -0,0 +1,39 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "../error"
5
+ require_relative "../prose_list"
6
+ require_relative "shape_problems_beside"
7
+
8
+ module Principles
9
+ # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
10
+ module Models
11
+ # The judgements an answer leaves as its template printed them, each named with its pointer, which the answer
12
+ # refuses in one refusal, so a copied template takes one repair (#991, #1010, #1040).
13
+ class LeftAsPrinted
14
+ # The problem with a judgement sent back as its placeholder.
15
+ UNCHANGED = "is the template's placeholder, unchanged" #: String
16
+
17
+ # pointers holds each judgement left as printed, by its name, with its JSON pointer.
18
+ #: (Hash[String, String]) -> void
19
+ def initialize(pointers)
20
+ @pointers = pointers.freeze #: Hash[String, String]
21
+ freeze
22
+ end
23
+
24
+ # Refuses the answer naming each judgement left as printed, then each of besides, problems its shape has
25
+ # elsewhere, so one refusal names every repair (#1106); one at or under a pointer named is left out, since
26
+ # writing the judgement repairs it. Nothing when none is left as printed, so the shape's own refusal names them.
27
+ #: (?besides: Array[Hash[String, String]]) -> void
28
+ def refuse(besides: [])
29
+ return if @pointers.empty?
30
+
31
+ elsewhere = ShapeProblemsBeside.new(besides, named: @pointers.values)
32
+ raise Error.new("invalid_answer", elsewhere.message("Still to write: #{ProseList.new(@pointers.keys)}"),
33
+ "#{elsewhere.repair("Replace each placeholder named in fields with your own judgement",
34
+ ", and repair each other field listed there")}.",
35
+ fields: @pointers.values.map { { "pointer" => it, "problem" => UNCHANGED } } + elsewhere.fields)
36
+ end
37
+ end
38
+ end
39
+ end
@@ -12,6 +12,7 @@ require_relative "misspelt_member_id"
12
12
  require_relative "record_judged_against"
13
13
  require_relative "reported_check"
14
14
  require_relative "template_placeholders"
15
+ require_relative "unchecked_members"
15
16
 
16
17
  module Principles
17
18
  # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
@@ -31,7 +32,8 @@ module Principles
31
32
  # delivery gives none (#1010).
32
33
  SIGHTING = TemplatePlaceholders.new(
33
34
  { "observation" => "Only with delivery observed: what you saw the member do, where, and what you did not see",
34
- "observed_at" => "Only for a sighting before this audit: when, in UTC, or a dated record's date" }
35
+ "observed_at" => "Only for a sighting before this audit: when, in UTC, or a dated record's date " \
36
+ "at 00:00:00Z" }
35
37
  ) #: TemplatePlaceholders
36
38
 
37
39
  # assessments is the answer's member_assessments; members are the mapped members, with each checked one's
@@ -60,21 +62,6 @@ module Principles
60
62
  assessments.each_with_index { |assessment, index| member_assessment(assessment, place_of(assessment, index)) }
61
63
  end
62
64
 
63
- # Each judgement an assessment leaves as its template printed it, named by whose it is and what, with its
64
- # pointer: its assessment, and an observed delivery's observation, which AssessmentAnswer refuses with any other
65
- # judgement left so, in one refusal (#991, #1010).
66
- #: () -> Hash[String, String]
67
- def left_as_printed
68
- return {} unless @sent.is_a?(Array)
69
-
70
- @sent.each_with_index.flat_map do |assessment, index|
71
- next [] unless assessment.is_a?(Hash)
72
-
73
- place = place_of(assessment, index)
74
- unwritten_fields(assessment).map { |field| ["#{place.subject} #{field}", place.child(field).pointer] }
75
- end.to_h
76
- end
77
-
78
65
  # The result reported for each checked member's check, by member id, once each has been checked. Call it after
79
66
  # check, which refuses an unmapped id first: finding the command the record ran needs each member's key.
80
67
  #: () -> Hash[String, Hash[String, untyped]]
@@ -101,15 +88,6 @@ module Principles
101
88
 
102
89
  private
103
90
 
104
- # The fields of assessment, as sent, that are still the text the template printed there and must be written:
105
- # its assessment, and an observed delivery's observation.
106
- #: (Hash[String, untyped]) -> Array[String]
107
- def unwritten_fields(assessment)
108
- [("assessment" if assessment["assessment"] == PLACEHOLDER),
109
- ("observation" if assessment["delivery"] == "observed" &&
110
- SIGHTING.left?("observation", assessment["observation"]))].compact
111
- end
112
-
113
91
  # assessments without the sighting fields each leaves as printed, which it does not give (#1010).
114
92
  #: (untyped) -> untyped
115
93
  def given(assessments)
@@ -132,9 +110,7 @@ module Principles
132
110
 
133
111
  # The place of the assessment at index in the answer, whose fields each refusal about it points into.
134
112
  #: (untyped, Integer) -> AnswerPlace
135
- def place_of(assessment, index)
136
- AnswerPlace.new("/member_assessments/#{index}", "Member #{assessment["member_id"]}")
137
- end
113
+ def place_of(assessment, index) = AnswerPlace.member_assessment(index, assessment["member_id"])
138
114
 
139
115
  #: (untyped, AnswerPlace) -> void
140
116
  def member_assessment(assessment, place)
@@ -175,13 +151,15 @@ module Principles
175
151
  "the request asks to judge it: judge_because #{reason}" if reason
176
152
  end
177
153
 
178
- # The result reported for the member assessed at place, checked against its check.
154
+ # The result reported for the member assessed at place, checked against its check; nil for a member with none,
155
+ # whose assessment UncheckedMembers refuses if it gives one.
179
156
  #: (untyped, AnswerPlace) -> Hash[String, untyped]?
180
157
  def reported(assessment, place)
181
158
  id = assessment["member_id"]
182
159
  key = @members.found(id).key
183
- ReportedCheck.new(place, @members.check(id), ran: @judged_against&.check_run_for(key),
184
- kept: @judged_against&.check_review_for(key) || {})
160
+ check = @members.check(id) or return UncheckedMembers.new(@members).refuse_at(place, assessment["check_result"])
161
+ ReportedCheck.new(place, check, ran: @judged_against&.check_run_for(key),
162
+ kept: @judged_against&.check_review_for(key) || {})
185
163
  .result(assessment["check_result"])
186
164
  end
187
165
  end
@@ -3,6 +3,7 @@
3
3
 
4
4
  require "digest"
5
5
  require_relative "../error"
6
+ require_relative "../prose_list"
6
7
  require_relative "locator"
7
8
  require_relative "write_up_opening"
8
9
 
@@ -26,6 +27,10 @@ module Principles
26
27
  PLACE_LOST = %w[section_not_found passage_not_found passage_end_not_found ambiguous_heading
27
28
  ambiguous_passage field_not_found].freeze #: Array[String]
28
29
 
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
+
29
34
  # The file name endings read as Markdown, where a # line is a heading and not a comment.
30
35
  MARKDOWN = /\.(md|markdown|mdx)\z/i #: Regexp
31
36
 
@@ -111,9 +116,10 @@ module Principles
111
116
  # passage.
112
117
  #: (Array[String], Integer, String, String, String?) -> [Integer, Integer, String]
113
118
  def passage(lines, first, selected, start_text, end_text)
114
- start = only_opening(selected, start_text)
119
+ before = lines[0...first].to_a.join.length
120
+ start = only_opening(selected, start_text, before)
115
121
  found = selected[start...passage_end(selected, start, start_text, end_text)].to_s
116
- line = @text[0, lines[0...first].to_a.join.length + start].to_s.count("\n")
122
+ line = @text[0, before + start].to_s.count("\n")
117
123
  [line, line + line_count(found), found]
118
124
  end
119
125
 
@@ -123,19 +129,37 @@ module Principles
123
129
  found.count("\n") + (found.end_with?("\n") ? 0 : 1)
124
130
  end
125
131
 
126
- #: (String, String) -> Integer
127
- def only_opening(text, start_text)
128
- matches = text.scan(start_text).length
129
- return text.index(start_text).to_i if matches == 1
130
- if matches.zero?
131
- raise Error.new("passage_not_found", "The passage starting #{start_text.inspect} is not in the file",
132
- "Quote the passage's opening text exactly.", @path)
133
- end
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
134
140
 
135
- raise Error.new("ambiguous_passage", "#{start_text.inspect} occurs #{matches} times",
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)}",
136
152
  "Quote more of the passage's start so it occurs once.", @path)
137
153
  end
138
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
+
139
163
  #: (String, Integer, String, String?) -> Integer
140
164
  def passage_end(text, start, start_text, end_text)
141
165
  return (text.index("\n", start) || (text.length - 1)) + 1 unless end_text
@@ -1,7 +1,7 @@
1
1
  # typed: strict
2
2
  # frozen_string_literal: true
3
3
 
4
- require_relative "recorded_members"
4
+ require_relative "recorded_entry"
5
5
 
6
6
  module Principles
7
7
  # The domain logic, which loads and saves itself through data/. models/CLAUDE.md owns the boundary.
@@ -25,16 +25,20 @@ module Principles
25
25
 
26
26
  private
27
27
 
28
- # Each entry whose hash and key, as RecordedMembers.filed_key gives it, are text.
28
+ # Each entry whose hash is text and whose key, if it keeps one, is text too, by its key as RecordedEntry#key
29
+ # gives it.
29
30
  #: (Hash[untyped, untyped]) -> Hash[String, String]
30
31
  def kept(members)
31
32
  members.each_with_object({}) do |(id, entry), kept|
32
- next unless entry.is_a?(Hash) && entry["sha256"].is_a?(String)
33
-
34
- key = RecordedMembers.filed_key(id, entry)
35
- kept[key] = entry["sha256"] if key.is_a?(String)
33
+ kept[RecordedEntry.new(id, entry).key] = entry["sha256"] if readable?(entry)
36
34
  end
37
35
  end
36
+
37
+ # Whether entry holds its hash, and any key it keeps, as text, the shape a readable record's schema requires.
38
+ #: (untyped) -> bool
39
+ def readable?(entry)
40
+ entry.is_a?(Hash) && [entry["sha256"], entry["key"]] in [String, String | nil]
41
+ end
38
42
  end
39
43
  end
40
44
  end
@@ -13,9 +13,17 @@ module Principles
13
13
  # to a whole file. A first guess, for an alpha test to confirm (#379).
14
14
  SHOWN_AT_MOST = 80
15
15
 
16
- # The unchanged lines shown on each side of a changed region, as git shows by default (#449).
16
+ # The most bytes both sides together show whole: a 4 KB acknowledgement less its 2.7 KB of other fields,
17
+ # so a short file of long lines shows its changed region and not itself twice (D93's review F4, #1069).
18
+ SHOWN_BYTES_AT_MOST = 1300
19
+
20
+ # The most unchanged lines shown on each side of a changed region, as git shows by default (#449); fewer
21
+ # when these would pass SHOWN_BYTES_AT_MOST (#1069).
17
22
  CONTEXT_LINES = 3
18
23
 
24
+ # The most shown around a one-line change, whose edit first_changed_lines already gives (#1146).
25
+ ONE_LINE_CONTEXT_LINES = 1
26
+
19
27
  # Each line is kept without its line ending, so a line the agent copies from the request is a whole line it
20
28
  # can quote (#451).
21
29
  #: (at_baseline: Array[String], now: Array[String]) -> void
@@ -36,36 +44,76 @@ module Principles
36
44
  #: () -> bool
37
45
  def changed? = @at_baseline != @now
38
46
 
39
- # Both sides' lines while each fits; else their changed region, with where it starts within the member and
40
- # each side's length; else how many lines each side has.
41
- #: () -> Hash[String, untyped]
42
- def shown
47
+ # Both sides' lines while each is at most SHOWN_AT_MOST lines and together at most SHOWN_BYTES_AT_MOST less
48
+ # taken, the bytes a request's other parts take beyond its usual ones, as a checked member's check_result
49
+ # template does (#1119); else their changed region, with where it starts within the member and each side's
50
+ # length; else how many lines each side has.
51
+ #: (?taken: Integer) -> Hash[String, untyped]
52
+ def shown(taken: 0)
53
+ budget = SHOWN_BYTES_AT_MOST - taken
43
54
  sides = { "at_baseline" => @at_baseline, "now" => @now }
44
- return sides if fits?(sides.values)
55
+ return sides if within_line_limit?(sides.values) && bytes(sides.values) <= budget
45
56
 
46
57
  counts = sides.transform_values(&:length)
47
- region = changed_region
48
- return { "omitted" => "too_long", "lines" => counts } unless fits?(region.values.map { |each| each["lines"] })
58
+ context, region = region_within(budget)
59
+ return { "omitted" => "too_long", "lines" => counts } unless within_line_limit?(lines_of(region))
49
60
 
50
- { "changed_region" => { "context_lines" => CONTEXT_LINES, **region }, "lines" => counts }
61
+ { "changed_region" => { "context_lines" => context, **region }, "lines" => counts }
51
62
  end
52
63
 
53
64
  private
54
65
 
66
+ # How many bytes the sides' lines hold together.
67
+ #: (Array[Array[String]]) -> Integer
68
+ def bytes(sides) = sides.sum { |lines| lines.sum(&:bytesize) }
69
+
55
70
  #: (Array[Array[String]]) -> bool
56
- def fits?(sides)
71
+ def within_line_limit?(sides)
57
72
  sides.all? { |lines| lines.length <= SHOWN_AT_MOST }
58
73
  end
59
74
 
60
- # Each side's lines from CONTEXT_LINES before its first difference to CONTEXT_LINES after its last, with the
75
+ # The changed region with the most context, up to CONTEXT_LINES, whose lines fit budget, and how many lines of
76
+ # context it has; with none when no context fits, since the changed lines are what is judged, and then the sides
77
+ # first_changed_lines' span and read_the_change stand in for left out (#1103, #1144).
78
+ #: (Integer) -> [Integer, Hash[String, Hash[String, untyped]]]
79
+ def region_within(budget)
80
+ regions = most_context.downto(0).map { |context| [context, changed_region(context)] }
81
+ regions.find { |_, region| bytes(lines_of(region)) <= budget } ||
82
+ [0, with_sides_left_out(regions.fetch(-1).last)]
83
+ end
84
+
85
+ # ONE_LINE_CONTEXT_LINES when each side changed one line at most, else CONTEXT_LINES.
86
+ #: () -> Integer
87
+ def most_context
88
+ one_line = lines_of(changed_region(0)).all? { it.length <= 1 }
89
+ one_line ? ONE_LINE_CONTEXT_LINES : CONTEXT_LINES
90
+ end
91
+
92
+ # region with its baseline side's lines left out, saying so, and its side now too when each side holds one line
93
+ # at most, whose change first_changed_lines' span shows whole (#1144); a longer region's other changed lines no
94
+ # hint shows, so its side now stays (#1069).
95
+ #: (Hash[String, Hash[String, untyped]]) -> Hash[String, Hash[String, untyped]]
96
+ def with_sides_left_out(region)
97
+ one_line = lines_of(region).all? { it.length <= 1 }
98
+ left_out = one_line ? %w[at_baseline now] : %w[at_baseline]
99
+ region.to_h do |name, side|
100
+ [name, left_out.include?(name) ? { "from_line" => side.fetch("from_line"), "omitted" => "too_long" } : side]
101
+ end
102
+ end
103
+
104
+ # Each side's lines in a changed region, none for a side left out.
105
+ #: (Hash[String, Hash[String, untyped]]) -> Array[Array[String]]
106
+ def lines_of(region) = region.values.map { |side| side.fetch("lines", []) }
107
+
108
+ # Each side's lines from context lines before its first difference to context lines after its last, with the
61
109
  # 1-based line, within the member, the region starts at.
62
- #: () -> Hash[String, Hash[String, untyped]]
63
- def changed_region
110
+ #: (Integer) -> Hash[String, Hash[String, untyped]]
111
+ def changed_region(context)
64
112
  same_start = common_length(@at_baseline, @now)
65
113
  same_end = common_length(@at_baseline.drop(same_start).reverse, @now.drop(same_start).reverse)
66
- from = [same_start - CONTEXT_LINES, 0].max
114
+ from = [same_start - context, 0].max
67
115
  { "at_baseline" => @at_baseline, "now" => @now }.transform_values do |lines|
68
- to = [lines.length - same_end + CONTEXT_LINES, lines.length].min
116
+ to = [lines.length - same_end + context, lines.length].min
69
117
  { "from_line" => from + 1, "lines" => lines[from...to] || [] }
70
118
  end
71
119
  end
@@ -2,6 +2,7 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require_relative "../error"
5
+ require_relative "acknowledgement_refusal"
5
6
  require_relative "audit_record"
6
7
  require_relative "principle_members"
7
8
  require_relative "principle_history"
@@ -23,13 +24,19 @@ module Principles
23
24
  # where it is, read with the map as that commit held it; none outside git.
24
25
  #: (Project, PrincipleMembers) -> Baselines
25
26
  def self.of(project, members)
26
- id = members.principle_id
27
- audit, hashes = project.audit_records.for_comparison(id)
28
- mapped = PrincipleHistory.new(id: id, history: project.history, principles: project.principles)
29
- .mapped_baselines(members.keys)
30
- new(audit: audit, mapped: mapped.transform_values { |each| each.fetch("sha256") },
31
- mapped_in: mapped.transform_values { |each| each.fetch("commit") }, in_git: project.history.inside_git?,
32
- hashes: hashes)
27
+ 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))
32
+ end
33
+
34
+ # Each member's hash and commit at the commit that mapped it where it is, read with the map as that commit
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)
33
40
  end
34
41
 
35
42
  # Whether the project is in git, so a member with no baseline is one no commit holds yet.
@@ -37,8 +44,8 @@ module Principles
37
44
  attr_reader :in_git
38
45
 
39
46
  # Whether a later copy wrote the principle's record, so its audits and acknowledgements wait on an upgrade.
40
- #: bool
41
- attr_reader :waiting
47
+ #: () -> bool
48
+ def waiting? = @hashes.waiting?
42
49
 
43
50
  # audit is the principle's audit record, if any: its hashes, the commit it began from and whether it is
44
51
  # completed, so a one-member audit is offered only where one would be accepted (#626). hashes is the member
@@ -46,13 +53,12 @@ module Principles
46
53
  #: (audit: AuditRecord?, mapped: Hash[String, String], mapped_in: Hash[String, String], in_git: bool,
47
54
  #| ?hashes: RecordedHashes) -> void
48
55
  def initialize(audit:, mapped:, mapped_in:, in_git:, hashes: audit&.members&.hashes || RecordedHashes::NONE)
49
- @audited = hashes.by_key #: Hash[String, String]
56
+ @hashes = hashes
50
57
  @audit = audit #: AuditRecord?
51
58
  @audit_base = audit&.base_commit #: String?
52
59
  @mapped = mapped
53
60
  @mapped_in = mapped_in
54
61
  @in_git = in_git
55
- @waiting = hashes.waiting? #: bool
56
62
  freeze
57
63
  end
58
64
 
@@ -60,11 +66,11 @@ module Principles
60
66
  # hash at the commit that mapped it, with why no audit gave one; neither when it has no baseline.
61
67
  #: (String) -> [String?, Hash[String, untyped]?]
62
68
  def of(key)
63
- if @audited.key?(key)
64
- [@audited[key], { "baseline" => "audit", "commit" => @audit_base }]
69
+ if (audited = @hashes.hash_for(key))
70
+ [audited, { "baseline" => "audit", "commit" => @audit_base }]
65
71
  elsif @mapped.key?(key)
66
72
  [@mapped[key], { "baseline" => "mapping_commit", "commit" => @mapped_in[key],
67
- "no_audit_because" => @audited.empty? ? "no_audit_record" : "not_in_last_audit" }]
73
+ "no_audit_because" => @hashes.empty? ? "no_audit_record" : "not_in_last_audit" }]
68
74
  else
69
75
  [nil, nil]
70
76
  end
@@ -89,7 +95,7 @@ module Principles
89
95
  #: (Member, String?) -> bool
90
96
  def acknowledgeable?(member, now_sha256)
91
97
  audit = @audit
92
- @in_git && !@waiting && !audit.nil? && audit.acknowledgement_refusal(member, now_sha256:).nil?
98
+ @in_git && !waiting? && !audit.nil? && AcknowledgementRefusal.of(audit, member, now_sha256:).nil?
93
99
  end
94
100
  end
95
101
 
@@ -200,7 +206,7 @@ module Principles
200
206
  # one (D58, #731 R7).
201
207
  #: () -> bool
202
208
  def awaits_first_audit?
203
- return false if @baselines.waiting || !@baselines.no_completed_audit?
209
+ return false if @baselines.waiting? || !@baselines.no_completed_audit?
204
210
 
205
211
  bases = compared_with.keys
206
212
  @baselines.in_git ? bases.any? { it != "no_baseline" } : !bases.empty?
@@ -237,7 +243,7 @@ module Principles
237
243
  # The changed members this copy can act on: none while a later copy's record makes them wait on an upgrade,
238
244
  # so they give --short neither exit 3 nor 4 (D85 V8, #1066).
239
245
  #: () -> Array[Member]
240
- def changes_to_act_on = @baselines.waiting ? [] : changed_members
246
+ def changes_to_act_on = @baselines.waiting? ? [] : changed_members
241
247
 
242
248
  #: (Member) -> bool
243
249
  def assessed?(member) = baseline(member.key).last&.fetch("baseline") == "audit"
@@ -260,7 +266,7 @@ module Principles
260
266
  return listed.merge("state" => "no_baseline") unless since
261
267
  unless then_sha == sha
262
268
  return listed.merge("state" => "changed", "since" => since,
263
- **(@baselines.waiting ? { "waiting_on_an_upgrade" => true } : {}))
269
+ **(@baselines.waiting? ? { "waiting_on_an_upgrade" => true } : {}))
264
270
  end
265
271
 
266
272
  listed.merge("state" => since["baseline"] == "mapping_commit" ? "unchanged_since_mapping" : "unchanged")
@@ -0,0 +1,54 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ module Principles
5
+ # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
6
+ module Models
7
+ # The moves that may repair a member this tool cannot find, each as the locator fields it keeps and those the agent
8
+ # fills in, which Views::MemberRepairs prints as move-member commands. A member whose file is gone moves to the
9
+ # path the agent finds, keeping its own section, passage or query, so a rename keeps its audit (#268). A member
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.
14
+ class MemberRepairMoves
15
+ # The locator fields a member may give beside its path.
16
+ LOCATOR = %w[section starting_at ending_at query].freeze #: Array[String]
17
+
18
+ # member is the member's locator fields, as its map holds them; code is that of the refusal saying why it cannot
19
+ # be found.
20
+ #: (Hash[String, untyped], code: String?) -> void
21
+ def initialize(member, code:)
22
+ @member = given(member, LOCATOR) #: Hash[String, untyped]
23
+ @code = code
24
+ freeze
25
+ end
26
+
27
+ # Each move, as { "keep" => the locator fields it keeps, by name, "fill" => the fields the agent fills in }.
28
+ #: () -> Array[Hash[String, untyped]]
29
+ def to_a
30
+ return [move(@member, %w[path])] if @code == "unreadable_file"
31
+ return [move({}, %w[query])] if @member["query"]
32
+
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
+ section = @member.key?("section") ? %w[section] : []
37
+ [move(anchors, [*opening, *section]), move(anchors, [*opening, "path", *section])]
38
+ end
39
+
40
+ private
41
+
42
+ # The fields of member named in fields that it gives, in the order fields names them.
43
+ #: (Hash[String, untyped], Array[String]) -> Hash[String, untyped]
44
+ def given(member, fields) = fields.filter_map { [it, member[it]] unless member[it].nil? }.to_h.freeze
45
+
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
+ #: (Hash[String, untyped], Array[String]) -> Hash[String, untyped]
51
+ def move(keep, fill) = { "keep" => keep, "fill" => fill }
52
+ end
53
+ end
54
+ 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 or QUERY filled in, if it moved, or the " \
51
- "remove-member if it is gone. #{note}#{REPAIR}" \
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}" \
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
@@ -31,6 +31,10 @@ module Principles
31
31
  #: (String) -> Array[Hash[String, untyped]]
32
32
  def beside(member_id) = @sharing.fetch(member_id).naming(@left_open_by_principle)
33
33
 
34
+ # Whether another principle leaves a finding open on a member sharing the lines of the audited member member_id.
35
+ #: (String) -> bool
36
+ def open_beside?(member_id) = @sharing.fetch(member_id).open_among(@left_open_by_principle).any?
37
+
34
38
  # Each finding left open on any of them, once, with its principle, and what its evidence's lines are beside
35
39
  # lines, this audit's members, where locate finds each item now (D64): a finding is the same one when its
36
40
  # principle and id are.