principles 0.0.1 → 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 (197) hide show
  1. checksums.yaml +4 -4
  2. data/lib/principles/cli.rb +10 -2
  3. data/lib/principles/command_line.rb +46 -35
  4. data/lib/principles/command_routes.rb +4 -3
  5. data/lib/principles/controllers/acknowledgement.rb +84 -18
  6. data/lib/principles/controllers/answer.rb +56 -125
  7. data/lib/principles/controllers/answer_phase.rb +54 -0
  8. data/lib/principles/controllers/assessment.rb +132 -0
  9. data/lib/principles/controllers/audit.rb +45 -140
  10. data/lib/principles/controllers/audit_completion.rb +8 -8
  11. data/lib/principles/controllers/command.rb +29 -14
  12. data/lib/principles/controllers/correction.rb +23 -13
  13. data/lib/principles/controllers/full_audit_request.rb +190 -0
  14. data/lib/principles/controllers/inventory.rb +46 -79
  15. data/lib/principles/controllers/mapping.rb +3 -3
  16. data/lib/principles/controllers/member_entries.rb +24 -12
  17. data/lib/principles/controllers/member_problems_report.rb +79 -0
  18. data/lib/principles/controllers/members_a_commit_changed.rb +12 -3
  19. data/lib/principles/controllers/members_to_assess.rb +35 -20
  20. data/lib/principles/controllers/meta_principle_setup.rb +5 -5
  21. data/lib/principles/controllers/repoint.rb +19 -23
  22. data/lib/principles/controllers/running_tool.rb +1 -1
  23. data/lib/principles/data/audit_files.rb +11 -7
  24. data/lib/principles/data/bounded_file.rb +20 -4
  25. data/lib/principles/data/command-steps.json +14 -13
  26. data/lib/principles/data/map_files.rb +2 -2
  27. data/lib/principles/data/principles_subfolder.rb +8 -1
  28. data/lib/principles/data/project_files.rb +12 -15
  29. data/lib/principles/data/project_root.rb +48 -0
  30. data/lib/principles/data/schema_check.rb +39 -11
  31. data/lib/principles/data/schemas/acknowledgement-answer.schema.json +17 -3
  32. data/lib/principles/data/schemas/answer-guide.schema.json +5 -10
  33. data/lib/principles/data/schemas/assessment-answer.schema.json +18 -9
  34. data/lib/principles/data/schemas/audit-record.schema.json +13 -1
  35. data/lib/principles/data/schemas/command-steps.schema.json +2 -1
  36. data/lib/principles/data/schemas/correction-answer.schema.json +2 -3
  37. data/lib/principles/invocation_options.rb +1 -1
  38. data/lib/principles/models/acknowledgement.rb +73 -84
  39. data/lib/principles/models/acknowledgement_refusal.rb +90 -0
  40. data/lib/principles/models/answer_place.rb +4 -0
  41. data/lib/principles/models/assessment_answer.rb +19 -17
  42. data/lib/principles/models/assessment_left_as_printed.rb +81 -0
  43. data/lib/principles/models/audit.rb +24 -15
  44. data/lib/principles/models/audit_record.rb +19 -20
  45. data/lib/principles/models/audit_records.rb +77 -4
  46. data/lib/principles/models/audit_start.rb +3 -3
  47. data/lib/principles/models/candidate_files.rb +30 -7
  48. data/lib/principles/models/candidate_inventory.rb +23 -6
  49. data/lib/principles/models/candidate_listing.rb +12 -3
  50. data/lib/principles/models/candidate_outline.rb +195 -0
  51. data/lib/principles/models/carried_assessments.rb +1 -1
  52. data/lib/principles/models/carried_member.rb +42 -0
  53. data/lib/principles/models/check_since_last_audit.rb +22 -9
  54. data/lib/principles/models/command_steps.rb +17 -1
  55. data/lib/principles/models/correction_basis.rb +1 -1
  56. data/lib/principles/models/decided_prior_findings.rb +9 -19
  57. data/lib/principles/models/declared_map_changes.rb +13 -12
  58. data/lib/principles/models/disposition.rb +28 -24
  59. data/lib/principles/models/evidence_replacements.rb +8 -1
  60. data/lib/principles/models/fix_only_fields.rb +67 -0
  61. data/lib/principles/models/frontmatter_fields.rb +9 -8
  62. data/lib/principles/models/instruction_named.rb +35 -0
  63. data/lib/principles/models/kept_prior_finding.rb +12 -7
  64. data/lib/principles/models/left_as_printed.rb +39 -0
  65. data/lib/principles/models/loaded_files.rb +5 -3
  66. data/lib/principles/models/loading_decision.rb +9 -2
  67. data/lib/principles/models/{member_maps.rb → mapped_principles.rb} +15 -15
  68. data/lib/principles/models/member.rb +1 -1
  69. data/lib/principles/models/member_assessments.rb +32 -9
  70. data/lib/principles/models/member_file.rb +35 -11
  71. data/lib/principles/models/member_grounds.rb +2 -2
  72. data/lib/principles/models/member_hashes_as_written.rb +44 -0
  73. data/lib/principles/models/member_lines.rb +63 -15
  74. data/lib/principles/models/member_moves.rb +3 -3
  75. data/lib/principles/models/member_problems.rb +50 -24
  76. data/lib/principles/models/member_repair_moves.rb +54 -0
  77. data/lib/principles/models/members_sharing_lines.rb +3 -3
  78. data/lib/principles/models/missing_files.rb +2 -2
  79. data/lib/principles/models/new_findings.rb +55 -0
  80. data/lib/principles/models/open_finding.rb +23 -13
  81. data/lib/principles/models/open_findings_elsewhere.rb +4 -0
  82. data/lib/principles/models/other_principles.rb +1 -1
  83. data/lib/principles/models/other_templates_placeholders.rb +113 -0
  84. data/lib/principles/models/outline_budget.rb +127 -0
  85. data/lib/principles/models/outline_group.rb +31 -0
  86. data/lib/principles/models/outline_section.rb +56 -0
  87. data/lib/principles/models/{member_map.rb → principle.rb} +45 -40
  88. data/lib/principles/models/{member_map_history.rb → principle_history.rb} +38 -15
  89. data/lib/principles/models/{map_members.rb → principle_members.rb} +15 -4
  90. data/lib/principles/models/prior_decision_fields.rb +47 -0
  91. data/lib/principles/models/prior_finding_decision.rb +38 -7
  92. data/lib/principles/models/prior_finding_outcomes.rb +10 -3
  93. data/lib/principles/models/prior_findings.rb +1 -1
  94. data/lib/principles/models/problem_report.rb +7 -3
  95. data/lib/principles/models/project.rb +22 -17
  96. data/lib/principles/models/project_history.rb +2 -2
  97. data/lib/principles/models/quoted_change.rb +137 -0
  98. data/lib/principles/models/recent_decision.rb +90 -0
  99. data/lib/principles/models/record_format.rb +109 -0
  100. data/lib/principles/models/recorded_entry.rb +6 -0
  101. data/lib/principles/models/recorded_finding.rb +4 -0
  102. data/lib/principles/models/recorded_hashes.rb +43 -0
  103. data/lib/principles/models/recorded_members.rb +33 -16
  104. data/lib/principles/models/refusal_before_shape.rb +21 -0
  105. data/lib/principles/models/repoint.rb +13 -0
  106. data/lib/principles/models/reported_check.rb +66 -24
  107. data/lib/principles/models/request_id.rb +24 -4
  108. data/lib/principles/models/sent_answer.rb +51 -0
  109. data/lib/principles/models/sent_prior_decisions.rb +31 -0
  110. data/lib/principles/models/shape_problems_beside.rb +40 -0
  111. data/lib/principles/models/still_holds_restatement.rb +7 -10
  112. data/lib/principles/models/submitted_answer.rb +34 -0
  113. data/lib/principles/models/template_placeholders.rb +71 -0
  114. data/lib/principles/models/text_within.rb +55 -0
  115. data/lib/principles/models/unchecked_members.rb +76 -0
  116. data/lib/principles/models/write_up_opening.rb +21 -4
  117. data/lib/principles/session_start_options.rb +29 -0
  118. data/lib/principles/start_audit_options.rb +52 -0
  119. data/lib/principles/version.rb +2 -2
  120. data/lib/principles/views/acknowledgement_result.rb +15 -7
  121. data/lib/principles/views/answer_head.rb +29 -0
  122. data/lib/principles/views/assessed_members.rb +129 -0
  123. data/lib/principles/views/assessment_request.rb +139 -78
  124. data/lib/principles/views/audit_result.rb +55 -42
  125. data/lib/principles/views/candidate_files_result.rb +41 -19
  126. data/lib/principles/views/check_notes.rb +44 -0
  127. data/lib/principles/views/check_result_template.rb +42 -30
  128. data/lib/principles/views/correction_request.rb +64 -0
  129. data/lib/principles/views/first_audit.rb +71 -0
  130. data/lib/principles/views/help.rb +3 -2
  131. data/lib/principles/views/instruction.rb +74 -0
  132. data/lib/principles/views/json_with_entries_on_lines.rb +51 -0
  133. data/lib/principles/views/loads_to_check_result.rb +13 -6
  134. data/lib/principles/views/map_added_to.rb +31 -0
  135. data/lib/principles/views/map_result.rb +38 -23
  136. data/lib/principles/views/member_change_since_baseline.rb +22 -5
  137. data/lib/principles/views/member_repairs.rb +21 -52
  138. data/lib/principles/views/members_a_commit_changed.rb +40 -10
  139. data/lib/principles/views/meta_principle_result.rb +3 -14
  140. data/lib/principles/views/one_member_check_result_template.rb +43 -0
  141. data/lib/principles/views/open_findings_elsewhere_result.rb +3 -3
  142. data/lib/principles/views/open_findings_line.rb +77 -23
  143. data/lib/principles/views/principle_read_result.rb +37 -28
  144. data/lib/principles/views/prior_finding_decisions_result.rb +13 -14
  145. data/lib/principles/views/prior_findings_result.rb +30 -18
  146. data/lib/principles/views/problem_lines.rb +14 -11
  147. data/lib/principles/views/problem_result.rb +30 -16
  148. data/lib/principles/views/prompts/acknowledge-a-change/run-the-check-and-report-it-passing.md +3 -0
  149. data/lib/principles/views/prompts/acknowledge-a-change/say-whether-the-change-keeps-the-meaning.md +6 -12
  150. data/lib/principles/views/prompts/assess-every-member/answer-guide.json +5 -10
  151. data/lib/principles/views/prompts/assess-every-member/assess-each-member-and-the-set.md +0 -4
  152. data/lib/principles/views/prompts/assess-every-member/claim-an-earlier-first-raised-at.md +7 -0
  153. data/lib/principles/views/prompts/assess-every-member/decide-each-prior-finding.md +6 -17
  154. data/lib/principles/views/prompts/assess-every-member/have-a-changed-check-reviewed-before-running-it.md +2 -2
  155. data/lib/principles/views/prompts/assess-every-member/have-a-new-check-reviewed-before-running-it.md +1 -1
  156. data/lib/principles/views/prompts/assess-every-member/judge-the-set-with-carried-assessments.md +5 -10
  157. data/lib/principles/views/prompts/assess-every-member/keep-or-change-what-a-deferral-waits-on.md +4 -0
  158. data/lib/principles/views/prompts/assess-every-member/read-a-compact-carried-member.md +8 -0
  159. data/lib/principles/views/prompts/assess-every-member/read-a-trimmed-instruction-whole.md +1 -0
  160. data/lib/principles/views/prompts/assess-every-member/review-a-new-or-changed-check-before-running-it.md +9 -0
  161. data/lib/principles/views/prompts/assess-every-member/run-the-check-then-report-it.md +2 -2
  162. data/lib/principles/views/prompts/assess-every-member/run-the-reviewed-check-and-say-what-its-review-rested-on.md +3 -6
  163. data/lib/principles/views/prompts/assess-every-member/say-what-observed-means.md +4 -0
  164. data/lib/principles/views/prompts/dispose-of-each-finding/fix-or-record-each-finding.md +5 -4
  165. data/lib/principles/views/prompts/find-a-principle-to-map/ask-the-owner-after-the-table.md +1 -0
  166. data/lib/principles/views/prompts/find-a-principle-to-map/ask-which-principle-to-map.md +2 -0
  167. data/lib/principles/views/prompts/find-a-principle-to-map/choose-one-principle.md +9 -5
  168. data/lib/principles/views/prompts/find-a-principle-to-map/leave-test-data-out-of-the-survey.md +1 -0
  169. data/lib/principles/views/prompts/find-a-principle-to-map/read-a-cut-section-back.md +3 -0
  170. data/lib/principles/views/prompts/find-a-principle-to-map/read-left-out-files.md +3 -0
  171. data/lib/principles/views/prompts/learn-what-a-command-does/add-member.md +4 -1
  172. data/lib/principles/views/prompts/learn-what-a-command-does/every-command-and-how-they-chain.md +7 -2
  173. data/lib/principles/views/prompts/learn-what-a-command-does/list-candidate-files.md +21 -6
  174. data/lib/principles/views/prompts/learn-what-a-command-does/list-member-problems.md +25 -23
  175. data/lib/principles/views/prompts/learn-what-a-command-does/list-open-findings.md +18 -16
  176. data/lib/principles/views/prompts/learn-what-a-command-does/list-principles.md +4 -2
  177. data/lib/principles/views/prompts/learn-what-a-command-does/select-the-project-root.md +1 -0
  178. data/lib/principles/views/prompts/learn-what-a-command-does/show-answer-check.md +7 -1
  179. data/lib/principles/views/prompts/learn-what-a-command-does/show-audit.md +3 -1
  180. data/lib/principles/views/prompts/learn-what-a-command-does/show-meta-principle-passage.md +7 -6
  181. data/lib/principles/views/prompts/learn-what-a-command-does/show-principle.md +12 -12
  182. data/lib/principles/views/prompts/learn-what-a-command-does/start-audit.md +30 -30
  183. data/lib/principles/views/prompts/learn-what-a-command-does/submit-answer.md +39 -39
  184. data/lib/principles/views/prompts/report-after-a-commit/the-commits-changed-these-members-waiting-on-an-upgrade.md +2 -0
  185. data/lib/principles/views/prompts/report-after-a-commit/the-principles-skill-is-where-to-start.md +1 -0
  186. data/lib/principles/views/prompts/report-the-completed-audit/commit-only-what-the-audit-changed.md +4 -0
  187. data/lib/principles/views/prompts/set-principles-up/first-keeping-audit-after-own-principle.md +10 -0
  188. data/lib/principles/views/prompts/set-principles-up/keep-them-held-between-audits.md +2 -0
  189. data/lib/principles/views/prompts/set-principles-up/name-the-step-each-member-lacks.md +2 -0
  190. data/lib/principles/views/prompts/set-principles-up/place-the-passage-with-the-user.md +11 -7
  191. data/lib/principles/views/prompts/start-a-session/see-the-earlier-line.md +2 -0
  192. data/lib/principles/views/record_waiting_on_an_upgrade.rb +54 -0
  193. data/lib/principles/views/request_part.rb +26 -6
  194. data/lib/principles/views/request_parts.rb +8 -0
  195. metadata +69 -9
  196. data/lib/principles/views/prompts/find-a-principle-to-map/ask-the-owner-before-mapping.md +0 -1
  197. data/lib/principles/views/prompts/set-principles-up/set-up-first.md +0 -2
@@ -4,23 +4,23 @@
4
4
  require_relative "../error"
5
5
  require_relative "member"
6
6
  require_relative "member_history"
7
- require_relative "member_maps"
7
+ require_relative "mapped_principles"
8
8
  require_relative "placement"
9
9
  require_relative "project_history"
10
10
 
11
11
  module Principles
12
12
  # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
13
13
  module Models
14
- # One map's members as the commits that changed it held them, walking back from HEAD on its first-parent
15
- # line. Each member's run is the unbroken stretch of those commits whose map holds a member with its key,
16
- # so its oldest commit is the one that mapped the member where it is (#243). The walk stops at a map it
17
- # cannot read, so a run may start later than the true mapping; that can only hide an older change.
18
- class MemberMapHistory
19
- #: (id: String, history: ProjectHistory, member_maps: MemberMaps) -> void
20
- def initialize(id:, history:, member_maps:)
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.
18
+ class PrincipleHistory
19
+ #: (id: String, history: ProjectHistory, principles: MappedPrinciples) -> void
20
+ def initialize(id:, history:, principles:)
21
21
  @id = id
22
22
  @history = history
23
- @member_maps = member_maps
23
+ @principles = principles
24
24
  freeze
25
25
  end
26
26
 
@@ -58,20 +58,42 @@ module Principles
58
58
  #: (Member, baseline: String, now: String) -> Hash[String, untyped]
59
59
  def member_history(member, baseline:, now:)
60
60
  key = member.key
61
- now_at = @member_maps.find(@id).member(member.id)
61
+ now_at = @principles.find(@id).member(member.id)
62
62
  hashes = {} #: Hash[String, String?]
63
63
  hash_at = ->(revision) { hashes.fetch(revision) { hashes[revision] = hash_at(key, now_at, revision) } }
64
64
  commits = @history.commits_touching([*paths_of(member), *map_paths])
65
65
  MemberHistory.new(baseline: baseline, now: now, commits: commits, hash_at: hash_at).to_h
66
66
  end
67
67
 
68
- # The map as commit holds it, such as the one an audit began from, refused as MemberMaps refuses its text;
68
+ # The map as commit holds it, such as the one an audit began from, refused as MappedPrinciples refuses its text;
69
69
  # a commit before the folder took its name now holds it under the earlier name (#536), and a commit that lacks
70
70
  # the map under both gives empty text, which is refused as unparseable.
71
- #: (String) -> MemberMap
71
+ #: (String) -> Principle
72
72
  def map_at(commit)
73
73
  text = map_paths.lazy.filter_map { |path| @history.text_at(commit, path) }.first
74
- @member_maps.parsed(@id, text.to_s, commit: commit)
74
+ @principles.parsed(@id, text.to_s, commit: commit)
75
+ end
76
+
77
+ # Whether a full audit of this principle can have started from HEAD, so a correction may be open: in git,
78
+ # start-audit refuses a map that is not committed or has no member (D72, #1023). False outside git, or when HEAD
79
+ # holds no map it can read.
80
+ #: () -> bool
81
+ def startable_at_head?
82
+ head = @history.head or return false
83
+ map_at(head).auditable?
84
+ rescue Error
85
+ false
86
+ end
87
+
88
+ # Whether an add-member to map, as it stands before the member joins, is the first since the map was last
89
+ # committed, which add-member's reminders print on (#1025): map has no member, or, in git, its file is as HEAD
90
+ # holds it. Outside git, only with the first member.
91
+ #: (Principle) -> bool
92
+ def first_add_since_commit?(map)
93
+ return true if map.members.empty?
94
+ return false unless @history.head
95
+
96
+ @history.uncommitted([@principles.file(@id)]).empty?
75
97
  end
76
98
 
77
99
  # The member's fields as the map at commit placed it, found by key; nil where that map did not hold it or cannot
@@ -102,7 +124,7 @@ module Principles
102
124
 
103
125
  # The map's file now and under the folder's earlier name, so a walk of its history reaches back past the move.
104
126
  #: () -> Array[String]
105
- def map_paths = [@member_maps.file(@id), @member_maps.earlier_file(@id)]
127
+ def map_paths = [@principles.file(@id), @principles.earlier_file(@id)]
106
128
 
107
129
  # The member's placements at commit, as that commit's map placed it and as now_at does, once each.
108
130
  #: (String, String, Hash[String, untyped]) -> Array[Placement]
@@ -131,7 +153,8 @@ module Principles
131
153
  @history.hashes_by_key_at(revision, [fields])[key] || @history.hashes_by_key_at(revision, [now_at])[key]
132
154
  end
133
155
 
134
- # Each member the map at commit holds, by key, or nil when that commit lacks the map or MemberMaps refuses it.
156
+ # Each member the map at commit holds, by key, or nil when that commit lacks the map or
157
+ # MappedPrinciples refuses it.
135
158
  #: (String) -> Hash[String, Hash[String, untyped]]?
136
159
  def members_at(commit)
137
160
  map_at(commit).members.to_h { |member| [member.key, member.to_h] }
@@ -8,13 +8,13 @@ require_relative "misspelt_member_id"
8
8
  module Principles
9
9
  # The domain logic, which loads and saves itself through data/. models/CLAUDE.md owns the boundary.
10
10
  module Models
11
- # One principle map's members, as Members: finding one by its id, and the rules on ids and keys among them,
12
- # which MemberMap asks as it changes the map. It is Enumerable over its members, in the map's order.
11
+ # One principle's members, as Members: finding one by its id, and the rules on ids and keys among them,
12
+ # which Principle asks as it changes the map. It is Enumerable over its members, in the map's order.
13
13
  #: [out Elem = Member]
14
- class MapMembers
14
+ class PrincipleMembers
15
15
  include Enumerable
16
16
 
17
- # The principle whose map these are.
17
+ # The principle whose members these are.
18
18
  #: String
19
19
  attr_reader :principle_id
20
20
 
@@ -71,6 +71,17 @@ module Principles
71
71
  #: (untyped) -> String?
72
72
  def check(member_id) = with_id(member_id)&.check
73
73
 
74
+ # The id of each member that names a check, in the map's order (#1090).
75
+ #: () -> Array[String]
76
+ def checked_ids = @list.select(&:check).map(&:id)
77
+
78
+ # Whether a member has member_id and names no check, so its assessment takes no check_result (#1088).
79
+ #: (untyped) -> bool
80
+ def unchecked?(member_id)
81
+ member = with_id(member_id)
82
+ !member.nil? && member.check.nil?
83
+ end
84
+
74
85
  # Each key more than one member is known by, a member with no stored key being known by its id, with the ids of
75
86
  # the members that share it. An audit record files each member under its key, so one would stand for the other.
76
87
  #: () -> Hash[String, Array[String]]
@@ -0,0 +1,47 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "template_placeholders"
5
+
6
+ module Principles
7
+ # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
8
+ module Models
9
+ # The fields a decision on a prior finding may give, as start-audit --as template prints them to fill: each with
10
+ # a placeholder saying which decisions take it, so the agent fills those its decision takes and leaves the rest
11
+ # as printed, which is not giving them (#994). A marked finding's decision may start problem_evidence as an empty
12
+ # list instead, which is not given either (#838).
13
+ module PriorDecisionFields
14
+ # The decision's placeholder, naming what it may be (#746); one sent back is no decision, named in the answer's
15
+ # one refusal for every judgement left as printed (#1040).
16
+ DECISION = "still_holds, resolved, withdrawn or raised_again" #: String
17
+ # The fields that take pointers, which the answer's schema lets be text only for their placeholders.
18
+ POINTERS = %w[evidence recorded_at problem_evidence].freeze #: Array[String]
19
+ # raised_in's placeholder, in plain words, since the answer's schema accepts it as the one alternative to a full
20
+ # sha, the only value that reaches git (D53).
21
+ RAISED_IN = "With first_raised_at: its record's commit, full sha; else leave" #: String
22
+ # The placeholder printed in each field a decision may give: only which decision takes it, and what, since that
23
+ # a field left as printed is not given is stated once, at the template's head, and each finding printed every
24
+ # field's whole help (#1033). Each says what to do otherwise: "else kept" where omission keeps what the finding
25
+ # has, "else leave" where it gives nothing (#1043). action's says instead that it is a change, which only
26
+ # still_holds may make, since naming the decision read as a request to restate the action (dogfood D6).
27
+ PLACEHOLDERS = TemplatePlaceholders.new(
28
+ {
29
+ "reason" => "resolved, withdrawn, new action: why, in a line; else leave",
30
+ "as" => "raised_again: the new finding's id; else leave",
31
+ "evidence" => [{ "path" => "resolved: where the fix shows, with heading; else leave" }],
32
+ "action" => "to change it: deferred or accepted; else kept",
33
+ "recorded_at" => "still_holds: a write-up pointer or null; else kept",
34
+ "waits_on" => "deferral: whom it waits on, for what, or null; else kept",
35
+ "problem" => "still_holds: restated if renamed or moved; else kept",
36
+ "problem_evidence" => [{ "path" => "still_holds: where it shows, with heading, if not kept; else leave",
37
+ "replaces" => "if moved: a lost item's position" }],
38
+ "first_raised_at" => "still_holds, if dated late: an earlier audited_at; else leave",
39
+ "raised_in" => RAISED_IN
40
+ },
41
+ empty_lists: ["problem_evidence"]
42
+ ) #: TemplatePlaceholders
43
+ # Every placeholder a prior finding's decision prints: the decision's, and each field's.
44
+ TEMPLATE_PLACEHOLDERS = { "decision" => DECISION, **PLACEHOLDERS.to_h }.freeze #: Hash[String, untyped]
45
+ end
46
+ end
47
+ end
@@ -9,6 +9,7 @@ require_relative "first_raised_claim"
9
9
  require_relative "kept_prior_finding"
10
10
  require_relative "open_finding"
11
11
  require_relative "still_holds_restatement"
12
+ require_relative "prior_decision_fields"
12
13
  require_relative "waits_on"
13
14
 
14
15
  module Principles
@@ -19,16 +20,18 @@ module Principles
19
20
  class PriorFindingDecision
20
21
  KINDS = %w[still_holds resolved withdrawn raised_again].freeze
21
22
  CLOSING = %w[resolved withdrawn].freeze
23
+ # The actions a still_holds decision may keep a finding open with, each with what changing to it needs.
24
+ OPEN_ACTIONS = { "deferred" => "a reason and recorded_at, null when written up nowhere",
25
+ "accepted" => "a reason" }.freeze #: Hash[String, String]
22
26
  # What the completed result shows of each closed finding.
23
27
  CLOSED_FIELDS = %w[finding_id decision reason evidence].freeze
24
28
  REPAIR = "Decide it still_holds, raised_again with as, or resolved or withdrawn with a one-line reason."
25
-
26
29
  # finding is the prior finding as its record left it open; at is the decision's place in the answer, such as
27
30
  # /prior_finding_decisions/0, which a refusal about its pointers names; members are the map's members now, which
28
31
  # a kept finding's members must still be among.
29
- #: (Hash[String, untyped], OpenFinding, Array[String], at: String, members: MapMembers) -> void
32
+ #: (Hash[String, untyped], OpenFinding, Array[String], at: String, members: PrincipleMembers) -> void
30
33
  def initialize(decision, finding, new_ids, at:, members:)
31
- @decision = decision.reject { |key, value| key == "problem_evidence" && value == [] } #: Hash[String, untyped]
34
+ @decision = PriorDecisionFields::PLACEHOLDERS.given(decision) #: Hash[String, untyped]
32
35
  @finding = finding
33
36
  @new_ids = new_ids
34
37
  @members = members
@@ -44,6 +47,7 @@ module Principles
44
47
  kind = @decision["decision"]
45
48
  invalid("#{kind.inspect} is not a decision") unless KINDS.include?(kind)
46
49
  kind == "still_holds" ? holds : closed_or_raised(kind)
50
+ pointers_as_text
47
51
  fix_shown(locate)
48
52
  new_action
49
53
  StillHoldsRestatement.new(@decision, @finding, @place, refuse: refusal).check(locate)
@@ -79,6 +83,16 @@ module Principles
79
83
 
80
84
  private
81
85
 
86
+ # Refuses text, other than its placeholder, in a field that takes pointers, which the schema lets be text only
87
+ # so the placeholder can be left (#994).
88
+ #: () -> void
89
+ def pointers_as_text
90
+ field = PriorDecisionFields::POINTERS.find { |name| @decision[name].is_a?(String) } or return
91
+ invalid("#{field} is text, not pointers into the project",
92
+ "Give #{field} as pointers, such as {\"path\": ..., \"heading\": ...}, or leave its placeholder as " \
93
+ "printed.")
94
+ end
95
+
82
96
  # A still_holds decision may make its finding deferred or accepted, with a one-line reason, and a deferral
83
97
  # says where it is written up, if only as null, as a correction's does (D35 brief, finding 14).
84
98
  #: () -> void
@@ -87,19 +101,26 @@ module Principles
87
101
  unless @decision["decision"] == "still_holds"
88
102
  invalid("only still_holds may give action", "Drop action from #{@id}'s decision, or decide it still_holds.")
89
103
  end
90
- invalid("still_holds may make it only deferred or accepted") unless %w[deferred accepted].include?(action)
104
+ invalid("still_holds may make it only deferred or accepted") unless OPEN_ACTIONS.key?(action)
91
105
  changed_action(action)
92
106
  end
93
107
 
94
108
  #: (String) -> void
95
109
  def changed_action(action)
96
- if action == @finding.action
97
- invalid("it is already #{action}", "Drop action from #{@id}'s decision; it is already #{action}.")
98
- end
110
+ already(action) if action == @finding.action
99
111
  invalid("a new action needs a one-line reason") unless one_line_reason?
100
112
  unrecorded_deferral if action == "deferred" && !@decision.key?("recorded_at")
101
113
  end
102
114
 
115
+ # The template prints action for every finding, so it may be given the action its finding has: the repair
116
+ # names both ways on, keeping it or changing it to the other (dogfood D6).
117
+ #: (String) -> bot
118
+ def already(action)
119
+ other, needs = OPEN_ACTIONS.find { |open, _| open != action }
120
+ invalid("it is already #{action}", "To keep #{@id} #{action}, leave action as printed or drop it; " \
121
+ "to change it, give #{other} with #{needs}.")
122
+ end
123
+
103
124
  #: () -> bot
104
125
  def unrecorded_deferral
105
126
  invalid("deferring it needs recorded_at, or null when it is written up nowhere",
@@ -117,6 +138,16 @@ module Principles
117
138
  #: () -> void
118
139
  def holds
119
140
  gone unless @finding.removed_from(@members).empty?
141
+ unkept_reason if @decision.key?("reason") && !@decision.key?("action")
142
+ end
143
+
144
+ # A reason on a still_holds decision that changes no action, which the record has nowhere to keep, so the fact
145
+ # it corrects would stay as it was (#992).
146
+ #: () -> bot
147
+ def unkept_reason
148
+ invalid("still_holds keeps no reason",
149
+ "Drop reason from #{@id}'s decision. To correct what #{@id} says, give problem, one line restating " \
150
+ "it; to change its action, give action with the reason.")
120
151
  end
121
152
 
122
153
  #: (String) -> void
@@ -26,14 +26,21 @@ module Principles
26
26
  holding = of(["still_holds"]).map(&:kept)
27
27
  DecidedPriorFindings.new(
28
28
  { "kept" => holding.map(&:to_h), "closed" => of(PriorFindingDecision::CLOSING).map(&:closed),
29
- "inherited" => first_raised(of(["raised_again"])), "dropped" => holding.filter_map(&:dropped_write_up),
30
- "waits_on_dropped" => holding.filter_map(&:waits_on_dropped),
31
- "evidence_replaced" => holding.flat_map(&:swaps) }
29
+ "inherited" => first_raised(of(["raised_again"])), **changed(holding) }
32
30
  )
33
31
  end
34
32
 
35
33
  private
36
34
 
35
+ # What the still_holds decisions changed of the findings they kept, which the completed result names.
36
+ #: (Array[KeptPriorFinding]) -> Hash[String, Array[Hash[String, untyped]]]
37
+ def changed(holding)
38
+ { "dropped" => holding.filter_map(&:dropped_write_up),
39
+ "waits_on_dropped" => holding.filter_map(&:waits_on_dropped),
40
+ "evidence_replaced" => holding.flat_map(&:swaps),
41
+ "problems_restated" => holding.filter_map(&:problem_restated) }
42
+ end
43
+
37
44
  #: (Array[String]) -> Array[PriorFindingDecision]
38
45
  def of(kinds) = @decisions.select { |decision| kinds.include?(decision.kind) }
39
46
 
@@ -16,7 +16,7 @@ module Principles
16
16
  # lists them and checks the decisions; whether a finding still holds is the agent's judgement.
17
17
  class PriorFindings
18
18
  # record is the last audit's record, or nil; members are the map's members now, and now each one's hash.
19
- #: (record: AuditRecord?, members: MapMembers, now: Hash[String, String]) -> void
19
+ #: (record: AuditRecord?, members: PrincipleMembers, now: Hash[String, String]) -> void
20
20
  def initialize(record:, members:, now:)
21
21
  @record = record
22
22
  @left_open = record&.left_open || [] #: Array[OpenFinding]
@@ -42,6 +42,10 @@ module Principles
42
42
  #: () -> bool
43
43
  def assessed_change? = @maps.any?(&:assessed_change?)
44
44
 
45
+ # Whether any map's member no audit assessed was edited since the commit that mapped it (D44).
46
+ #: () -> bool
47
+ def unassessed_change? = @maps.any?(&:unassessed_change?)
48
+
45
49
  # For each map with a changed member a one-member audit may judge, the ids of those members, which
46
50
  # list-member-problems offers that audit for, one command each (#626, D56).
47
51
  #: () -> Hash[String, Array[String]]
@@ -52,10 +56,10 @@ module Principles
52
56
  # For each map with members no baseline holds, its map file and the files those members are in, and in git
53
57
  # only those of them that differ from HEAD or are untracked, since committing an unchanged file does nothing
54
58
  # (#710 X3). In git the list is empty when every one is committed already.
55
- #: (MemberMaps) -> Hash[String, Array[String]]
56
- def files_to_commit(member_maps)
59
+ #: (MappedPrinciples) -> Hash[String, Array[String]]
60
+ def files_to_commit(principles)
57
61
  no_baseline_paths.to_h do |id, paths|
58
- files = [member_maps.file(id), *paths]
62
+ files = [principles.file(id), *paths]
59
63
  [id, @history&.inside_git? ? @history.uncommitted(files) : files]
60
64
  end
61
65
  end
@@ -2,6 +2,7 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require_relative "../data/project_files"
5
+ require_relative "../data/project_root"
5
6
  require_relative "../data/schema_check"
6
7
  require_relative "../data/map_files"
7
8
  require_relative "../data/audit_files"
@@ -16,13 +17,14 @@ require_relative "loaded_files"
16
17
  require_relative "located_text"
17
18
  require_relative "member_loads"
18
19
  require_relative "member_lines"
19
- require_relative "member_maps"
20
+ require_relative "mapped_principles"
20
21
  require_relative "member_snapshots"
21
22
  require_relative "missing_files"
22
23
  require_relative "locate_refusal"
23
24
  require_relative "audit_records"
24
25
  require_relative "project_history"
25
26
  require_relative "principles_folder_move"
27
+ require_relative "sent_answer"
26
28
 
27
29
  module Principles
28
30
  # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
@@ -32,6 +34,10 @@ module Principles
32
34
  # project only through this model. It is handed its four data adapters, so a spec can hand it stand-ins
33
35
  # that meet the same contracts (#337).
34
36
  class Project
37
+ # An implicit invocation reuses its nearest established project; an explicit root uses .at directly.
38
+ #: (String) -> Project
39
+ def self.containing(directory) = at(Data::ProjectRoot.new(directory).path)
40
+
35
41
  # The project at root, with its maps, audit records and history.
36
42
  #: (String) -> Project
37
43
  def self.at(root)
@@ -56,10 +62,10 @@ module Principles
56
62
  @files.root
57
63
  end
58
64
 
59
- #: () -> MemberMaps
60
- def member_maps
65
+ #: () -> MappedPrinciples
66
+ def principles
61
67
  refuse_unmoved_folder
62
- MemberMaps.new(@maps)
68
+ MappedPrinciples.new(@maps)
63
69
  end
64
70
 
65
71
  #: () -> AuditRecords
@@ -114,7 +120,7 @@ module Principles
114
120
 
115
121
  # Each of map's members by id, where it is in its file now and the hash of what it says there. It is
116
122
  # refused once, listing every member that cannot be found or read, so the agent repairs them together.
117
- #: (MemberMap) -> MemberSnapshots
123
+ #: (Principle) -> MemberSnapshots
118
124
  def snapshots(map)
119
125
  broken = [] #: Array[Hash[String, untyped]]
120
126
  found = map.members.to_h do |member|
@@ -137,7 +143,7 @@ module Principles
137
143
  end
138
144
 
139
145
  # Each member's hash now by key, or the refusal that says why its text cannot be found or read.
140
- #: (MapMembers) -> Hash[String, untyped]
146
+ #: (PrincipleMembers) -> Hash[String, untyped]
141
147
  def current_by_key(members)
142
148
  members.to_h do |member|
143
149
  [member.key, locate(member.locator).fetch("sha256")]
@@ -166,13 +172,13 @@ module Principles
166
172
 
167
173
  # Each member's MemberLoads by id: when each host loads it into context, with the size of what loads, and
168
174
  # the project hooks whose commands name its file, read once for them all unless the caller already read them.
169
- #: (MapMembers, ?HookCommands) -> Hash[String, MemberLoads]
175
+ #: (PrincipleMembers, ?HookCommands) -> Hash[String, MemberLoads]
170
176
  def member_loads(members, hooks = hook_commands)
171
177
  members.to_h { |member| [member.id, MemberLoads.read(@files, member.locator, hooks: hooks)] }
172
178
  end
173
179
 
174
180
  # The files members load, each read whole once, with the project's hooks as hooks read them (#483).
175
- #: (MapMembers, HookCommands) -> LoadedFiles
181
+ #: (PrincipleMembers, HookCommands) -> LoadedFiles
176
182
  def loaded_files(members, hooks) = LoadedFiles.read(@files, members, hooks: hooks)
177
183
 
178
184
  #: () -> HookCommands
@@ -185,21 +191,20 @@ module Principles
185
191
  #: () -> EntryFiles
186
192
  def entry_files = EntryFiles.read(@files, history)
187
193
 
188
- ANSWER_REPAIR = "Repair each field listed, starting from the request's response_template."
189
-
190
- # An answer to a phase's request from file, where - means it was read from stdin, checked against the
191
- # phase's answer schema (D29).
192
- #: (String, stdin: String?, phase: String) -> untyped
194
+ # An answer to a phase's request from file, where - means it was read from stdin, its head checked against
195
+ # the phase's answer schema (D29) and the rest left to check once its request is known (#1052).
196
+ #: (String, stdin: String?, phase: String) -> SentAnswer
193
197
  def answer(file, stdin:, phase:)
194
- parsed = file == "-" ? @files.parse_json(stdin.to_s, "-") : @files.read_json(file)
195
- Data::SchemaCheck.new("#{phase}-answer").checked(parsed, code: "invalid_answer", source: file,
196
- repair: ANSWER_REPAIR)
198
+ parsed = if file == "-" then @files.parse_json(stdin.to_s, "-", named: SentAnswer::NAMED)
199
+ else @files.read_json(file, named: SentAnswer::FILE_NAMED)
200
+ end
201
+ SentAnswer.new(parsed, file, Data::SchemaCheck.new("#{phase}-answer"))
197
202
  end
198
203
 
199
204
  # What list-candidate-files found: the candidate files as it lists them, and which git does not track.
200
205
  #: () -> CandidateInventory
201
206
  def candidate_files
202
- CandidateInventory.found(@files.inventory, history)
207
+ CandidateInventory.found(@files.inventory, history) { |path| @files.read_text(path) }
203
208
  end
204
209
 
205
210
  private
@@ -5,7 +5,7 @@ require_relative "../error"
5
5
  require_relative "../data/repository"
6
6
  require_relative "../data/project_files"
7
7
  require_relative "../data/principles_subfolder"
8
- require_relative "map_members"
8
+ require_relative "principle_members"
9
9
  require_relative "member"
10
10
  require_relative "member_file"
11
11
  require_relative "commits_brought_in"
@@ -109,7 +109,7 @@ module Principles
109
109
 
110
110
  # Each member's id and the hash of its text at commit, or nil when commit lacks a member's file or the
111
111
  # member's place in it.
112
- #: (String, MapMembers) -> Hash[String, String]?
112
+ #: (String, PrincipleMembers) -> Hash[String, String]?
113
113
  def hashes_at(commit, members)
114
114
  members.to_h do |member|
115
115
  path = member.path
@@ -0,0 +1,137 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "../error"
5
+ require_relative "member_lines"
6
+
7
+ module Principles
8
+ # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
9
+ module Models
10
+ # A one-member audit's change as its answer quotes it (D33): a line of the member before the change and one after,
11
+ # each a whole line of its side or a fragment long enough to show the edit that only its side reads (#352, #379).
12
+ class QuotedChange
13
+ # One side of the change a quote is checked against: the member's lines then, and when they read.
14
+ class SideOfTheChange
15
+ #: String
16
+ attr_reader :reads_at
17
+
18
+ #: (Array[String], String) -> void
19
+ def initialize(lines, reads_at)
20
+ @lines = lines.map(&:chomp) #: Array[String]
21
+ @reads_at = reads_at
22
+ freeze
23
+ end
24
+
25
+ #: (String) -> bool
26
+ def whole_line?(quote) = @lines.include?(quote)
27
+
28
+ #: (String) -> bool
29
+ def holds?(text) = @lines.any? { |line| line.include?(text) }
30
+
31
+ # Its first line the other side lacks, or "" when it has none, as after a pure insertion on the other side.
32
+ #: (SideOfTheChange) -> String
33
+ def first_line_absent_from(other) = @lines.find { |line| !other.whole_line?(line) } || ""
34
+ end
35
+
36
+ # The fewest characters a quoted fragment of a line may have, so it shows which edit was read (#352).
37
+ FRAGMENT_LENGTH = 30
38
+
39
+ # The longest changed line the hint gives whole, beyond which it gives the span the edit changed (#1103).
40
+ LONG_LINE = 200
41
+
42
+ # lines holds the member's lines at its baseline commit and at HEAD; member_id names it in a refusal.
43
+ #: (MemberLines, member_id: String) -> void
44
+ def initialize(lines, member_id:)
45
+ @then = SideOfTheChange.new(lines.at_baseline, "before the change") #: SideOfTheChange
46
+ @now = SideOfTheChange.new(lines.now, "at HEAD") #: SideOfTheChange
47
+ @member_id = member_id
48
+ freeze
49
+ end
50
+
51
+ # The first line on each side the other lacks, which the request hints the quotes from (#1016); of a pair with a
52
+ # line over LONG_LINE characters, the span each side's edit changed, so a small edit is found without comparing
53
+ # two long lines by eye, and the request does not carry them again (#1103).
54
+ #: () -> Hash[String, String]
55
+ def first_changed_lines
56
+ old_line = @then.first_line_absent_from(@now)
57
+ new_line = @now.first_line_absent_from(@then)
58
+ whole = { "old_line" => old_line, "new_line" => new_line }
59
+ return whole if whole.values.all? { it.length <= LONG_LINE }
60
+
61
+ { "old_line" => changed_span(old_line, new_line, @then, @now),
62
+ "new_line" => changed_span(new_line, old_line, @now, @then) }
63
+ end
64
+
65
+ # Refuses an old_line not found before the change, or a new_line not found after it.
66
+ #: (old_line: String, new_line: String) -> void
67
+ def check(old_line:, new_line:)
68
+ quoted(old_line, @then, @now)
69
+ quoted(new_line, @now, @then)
70
+ end
71
+
72
+ private
73
+
74
+ # line's characters between those it shares with other_line from the start and from the end, with
75
+ # FRAGMENT_LENGTH more on each side, widened to whole words, when that fragment is a quote only side holds; else
76
+ # the whole line.
77
+ #: (String, String, SideOfTheChange, SideOfTheChange) -> String
78
+ def changed_span(line, other_line, side, other)
79
+ edit = edited(line, other_line)
80
+ from = word_start(line, [edit.begin - FRAGMENT_LENGTH, 0].max)
81
+ span = line[from...word_end(line, edit.end + FRAGMENT_LENGTH)].to_s
82
+ only_on?(span, side, other) ? span : line
83
+ end
84
+
85
+ # The start of the word at index in line, when it starts within FRAGMENT_LENGTH before it; else index, so a
86
+ # span of a line without spaces keeps its context (#1144).
87
+ #: (String, Integer) -> Integer
88
+ def word_start(line, index)
89
+ return index if index.zero?
90
+
91
+ start = (line.rindex(" ", index - 1) || -1) + 1
92
+ index - start <= FRAGMENT_LENGTH ? start : index
93
+ end
94
+
95
+ # The end of the word at index in line, when it ends within FRAGMENT_LENGTH after it; else index.
96
+ #: (String, Integer) -> Integer
97
+ def word_end(line, index)
98
+ finish = line.index(" ", index) || line.length
99
+ finish - index <= FRAGMENT_LENGTH ? finish : index
100
+ end
101
+
102
+ # The characters of line between those it shares with other_line from the start and from the end, which never
103
+ # overlap, as an insertion repeating its neighbour would make them.
104
+ #: (String, String) -> Range[Integer]
105
+ def edited(line, other_line)
106
+ head = common_length(line, other_line)
107
+ tail = common_length(line[head..].to_s.reverse, other_line[head..].to_s.reverse)
108
+ head...(line.length - tail)
109
+ end
110
+
111
+ # How many characters two strings share from their start.
112
+ #: (String, String) -> Integer
113
+ def common_length(one, other) = one.each_char.zip(other.each_char).take_while { |a, b| a == b }.length
114
+
115
+ # A quote is a whole line, or a fragment long enough to show the edit, that only this side reads; or "" when
116
+ # this side has no line the other lacks, as before a pure insertion (#1070).
117
+ #: (String, SideOfTheChange, SideOfTheChange) -> void
118
+ def quoted(line, side, other)
119
+ return if line == side.first_line_absent_from(other)
120
+ return if !line.empty? && only_on?(line, side, other)
121
+
122
+ raise Error.new("quote_not_found",
123
+ "#{line.inspect} is not a line of #{@member_id} #{side.reads_at} that the change touched",
124
+ "Quote a whole line of the member as it reads #{side.reads_at}, as first_changed_lines " \
125
+ "shows, or at least #{FRAGMENT_LENGTH} characters of one that the member #{other.reads_at} " \
126
+ "does not hold; quote \"\" only where first_changed_lines does.")
127
+ end
128
+
129
+ # Whether a quote is a whole line of side, or a fragment of at least FRAGMENT_LENGTH, that other does not hold.
130
+ #: (String, SideOfTheChange, SideOfTheChange) -> bool
131
+ def only_on?(line, side, other)
132
+ (side.whole_line?(line) && !other.whole_line?(line)) ||
133
+ (line.length >= FRAGMENT_LENGTH && side.holds?(line) && !other.holds?(line))
134
+ end
135
+ end
136
+ end
137
+ end