principles 0.0.4 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (227) hide show
  1. checksums.yaml +4 -4
  2. data/lib/principles/after_commit_hook_options.rb +4 -1
  3. data/lib/principles/cli.rb +3 -1
  4. data/lib/principles/command_line.rb +38 -26
  5. data/lib/principles/command_routes.rb +11 -4
  6. data/lib/principles/controllers/acknowledgement.rb +93 -17
  7. data/lib/principles/controllers/answer.rb +45 -76
  8. data/lib/principles/controllers/answer_phase.rb +54 -0
  9. data/lib/principles/controllers/assessment.rb +46 -11
  10. data/lib/principles/controllers/audit.rb +17 -6
  11. data/lib/principles/controllers/audit_completion.rb +9 -5
  12. data/lib/principles/controllers/command.rb +5 -4
  13. data/lib/principles/controllers/correction.rb +22 -10
  14. data/lib/principles/controllers/full_audit_request.rb +11 -1
  15. data/lib/principles/controllers/guidance_search_paths.rb +31 -0
  16. data/lib/principles/controllers/inventory.rb +15 -29
  17. data/lib/principles/controllers/mapping.rb +9 -1
  18. data/lib/principles/controllers/member_addition.rb +65 -0
  19. data/lib/principles/controllers/member_entries.rb +6 -24
  20. data/lib/principles/controllers/member_problems_report.rb +13 -6
  21. data/lib/principles/controllers/members_a_commit_changed.rb +2 -1
  22. data/lib/principles/controllers/members_to_assess.rb +21 -13
  23. data/lib/principles/controllers/meta_principle_setup.rb +1 -1
  24. data/lib/principles/controllers/repoint.rb +22 -23
  25. data/lib/principles/controllers/survey.rb +60 -0
  26. data/lib/principles/data/audit_files.rb +4 -0
  27. data/lib/principles/data/bounded_file.rb +9 -4
  28. data/lib/principles/data/candidate_files_found.rb +48 -13
  29. data/lib/principles/data/command-steps.json +85 -24
  30. data/lib/principles/data/committed_tree.rb +84 -0
  31. data/lib/principles/data/first_parent_log.rb +48 -0
  32. data/lib/principles/data/first_parent_range.rb +122 -0
  33. data/lib/principles/data/git_command.rb +37 -0
  34. data/lib/principles/data/guidance_search_paths_file.rb +107 -0
  35. data/lib/principles/data/map_files.rb +35 -10
  36. data/lib/principles/data/principles_directory.rb +40 -0
  37. data/lib/principles/data/principles_subfolder.rb +39 -16
  38. data/lib/principles/data/project_files.rb +22 -11
  39. data/lib/principles/data/repository.rb +55 -29
  40. data/lib/principles/data/requested_candidate_files.rb +43 -0
  41. data/lib/principles/data/running_version.rb +3 -5
  42. data/lib/principles/data/schema_check.rb +16 -6
  43. data/lib/principles/data/schemas/acknowledgement-answer.schema.json +18 -5
  44. data/lib/principles/data/schemas/answer-guide.schema.json +255 -68
  45. data/lib/principles/data/schemas/audit-record.schema.json +10 -1
  46. data/lib/principles/data/schemas/command-steps.schema.json +2 -1
  47. data/lib/principles/data/schemas/correction-answer-guide.schema.json +2 -1
  48. data/lib/principles/data/schemas/correction-answer.schema.json +6 -0
  49. data/lib/principles/data/schemas/guidance-search-paths.schema.json +13 -0
  50. data/lib/principles/data/schemas/member-map-history.schema.json +20 -0
  51. data/lib/principles/data/verified_head.rb +162 -0
  52. data/lib/principles/invocation_options.rb +3 -3
  53. data/lib/principles/models/acknowledgement.rb +95 -84
  54. data/lib/principles/models/acknowledgement_left_as_printed.rb +51 -0
  55. data/lib/principles/models/acknowledgement_refusal.rb +90 -0
  56. data/lib/principles/models/answer_place.rb +4 -0
  57. data/lib/principles/models/assessment_answer.rb +0 -45
  58. data/lib/principles/models/assessment_left_as_printed.rb +81 -0
  59. data/lib/principles/models/audit_entry_protecting_map.rb +28 -0
  60. data/lib/principles/models/audit_record.rb +12 -16
  61. data/lib/principles/models/audit_records.rb +52 -23
  62. data/lib/principles/models/candidate_files.rb +44 -10
  63. data/lib/principles/models/candidate_inventory.rb +69 -8
  64. data/lib/principles/models/candidate_outline.rb +195 -0
  65. data/lib/principles/models/captured_head.rb +72 -0
  66. data/lib/principles/models/carried_member.rb +42 -0
  67. data/lib/principles/models/check_since_last_audit.rb +20 -11
  68. data/lib/principles/models/command_steps.rb +17 -1
  69. data/lib/principles/models/declared_map_changes.rb +29 -12
  70. data/lib/principles/models/declared_member_removals.rb +75 -0
  71. data/lib/principles/models/disposition.rb +4 -2
  72. data/lib/principles/models/fix_only_fields.rb +12 -0
  73. data/lib/principles/models/frontmatter_fields.rb +9 -8
  74. data/lib/principles/models/guidance_search_path.rb +26 -0
  75. data/lib/principles/models/guidance_search_paths.rb +57 -0
  76. data/lib/principles/models/instruction_named.rb +35 -0
  77. data/lib/principles/models/left_as_printed.rb +39 -0
  78. data/lib/principles/models/line_span.rb +6 -0
  79. data/lib/principles/models/loaded_files.rb +1 -2
  80. data/lib/principles/models/loading_scope.rb +61 -0
  81. data/lib/principles/models/locator.rb +6 -0
  82. data/lib/principles/models/mapped_principles.rb +39 -37
  83. data/lib/principles/models/member_assessments.rb +9 -31
  84. data/lib/principles/models/member_file.rb +20 -47
  85. data/lib/principles/models/member_hashes_as_written.rb +10 -6
  86. data/lib/principles/models/member_history.rb +7 -5
  87. data/lib/principles/models/member_lines.rb +63 -15
  88. data/lib/principles/models/member_loading_scopes.rb +49 -0
  89. data/lib/principles/models/member_loads.rb +4 -0
  90. data/lib/principles/models/member_mapping_run.rb +30 -0
  91. data/lib/principles/models/member_moves.rb +4 -0
  92. data/lib/principles/models/member_problems.rb +63 -33
  93. data/lib/principles/models/member_region.rb +129 -0
  94. data/lib/principles/models/member_repair_moves.rb +52 -0
  95. data/lib/principles/models/member_snapshots.rb +14 -1
  96. data/lib/principles/models/members_in_consecutive_sections.rb +56 -0
  97. data/lib/principles/models/meta_principle.rb +14 -0
  98. data/lib/principles/models/missing_files.rb +2 -2
  99. data/lib/principles/models/open_findings_elsewhere.rb +4 -0
  100. data/lib/principles/models/other_templates_placeholders.rb +81 -42
  101. data/lib/principles/models/outline_budget.rb +142 -0
  102. data/lib/principles/models/outline_group.rb +31 -0
  103. data/lib/principles/models/outline_section.rb +56 -0
  104. data/lib/principles/models/principle.rb +14 -7
  105. data/lib/principles/models/principle_file_revision.rb +48 -0
  106. data/lib/principles/models/principle_history.rb +46 -19
  107. data/lib/principles/models/principle_lineage.rb +118 -0
  108. data/lib/principles/models/principle_map_revision.rb +39 -0
  109. data/lib/principles/models/principle_mapping_history.rb +132 -0
  110. data/lib/principles/models/principle_members.rb +25 -0
  111. data/lib/principles/models/principle_predecessor.rb +45 -0
  112. data/lib/principles/models/principle_publication.rb +177 -0
  113. data/lib/principles/models/principle_revision.rb +104 -0
  114. data/lib/principles/models/prior_decision_fields.rb +3 -2
  115. data/lib/principles/models/prior_finding_decision.rb +14 -4
  116. data/lib/principles/models/problem_report.rb +17 -5
  117. data/lib/principles/models/project.rb +46 -39
  118. data/lib/principles/models/project_history.rb +45 -25
  119. data/lib/principles/models/quoted_change.rb +137 -0
  120. data/lib/principles/models/record_format.rb +12 -2
  121. data/lib/principles/models/recorded_entry.rb +6 -0
  122. data/lib/principles/models/recorded_hashes.rb +7 -3
  123. data/lib/principles/models/recorded_members.rb +27 -15
  124. data/lib/principles/models/refusal_before_shape.rb +21 -0
  125. data/lib/principles/models/repoint.rb +13 -0
  126. data/lib/principles/models/reported_check.rb +5 -14
  127. data/lib/principles/models/sent_answer.rb +61 -0
  128. data/lib/principles/models/shape_problems_beside.rb +40 -0
  129. data/lib/principles/models/submitted_answer.rb +34 -0
  130. data/lib/principles/models/survey_search_paths.rb +26 -0
  131. data/lib/principles/models/template_placeholders.rb +34 -4
  132. data/lib/principles/models/text_within.rb +55 -0
  133. data/lib/principles/models/unchecked_members.rb +76 -0
  134. data/lib/principles/models/uncommitted_files.rb +50 -0
  135. data/lib/principles/models/unknown_principle.rb +29 -0
  136. data/lib/principles/models/write_up_opening.rb +21 -4
  137. data/lib/principles/positional_arguments.rb +37 -0
  138. data/lib/principles/session_start_options.rb +2 -2
  139. data/lib/principles/start_audit_options.rb +19 -4
  140. data/lib/principles/version.rb +2 -2
  141. data/lib/principles/views/acknowledgement_result.rb +23 -11
  142. data/lib/principles/views/added_member.rb +41 -0
  143. data/lib/principles/views/answered_request.rb +21 -0
  144. data/lib/principles/views/assessed_members.rb +43 -10
  145. data/lib/principles/views/assessment_request.rb +91 -31
  146. data/lib/principles/views/audit_result.rb +40 -28
  147. data/lib/principles/views/candidate_files_result.rb +85 -19
  148. data/lib/principles/views/candidate_outlines_result.rb +25 -0
  149. data/lib/principles/views/check_notes.rb +44 -0
  150. data/lib/principles/views/check_result_template.rb +14 -23
  151. data/lib/principles/views/consecutive_sections_elsewhere.rb +27 -0
  152. data/lib/principles/views/correction_request.rb +32 -1
  153. data/lib/principles/views/guidance_search_paths_result.rb +26 -0
  154. data/lib/principles/views/instruction.rb +74 -0
  155. data/lib/principles/views/json_with_entries_on_lines.rb +59 -0
  156. data/lib/principles/views/loads_to_check_result.rb +2 -2
  157. data/lib/principles/views/map_added_to.rb +56 -2
  158. data/lib/principles/views/map_result.rb +23 -43
  159. data/lib/principles/views/maps_needing_members_or_audit.rb +47 -0
  160. data/lib/principles/views/member_change_since_baseline.rb +23 -5
  161. data/lib/principles/views/member_repairs.rb +22 -52
  162. data/lib/principles/views/members_a_commit_changed.rb +16 -6
  163. data/lib/principles/views/new_loading_scope.rb +31 -0
  164. data/lib/principles/views/one_member_check_result_template.rb +47 -0
  165. data/lib/principles/views/open_findings_elsewhere_result.rb +3 -3
  166. data/lib/principles/views/open_findings_line.rb +77 -23
  167. data/lib/principles/views/principle_read_result.rb +24 -29
  168. data/lib/principles/views/principles_with_no_completed_audit.rb +9 -3
  169. data/lib/principles/views/prior_findings_result.rb +23 -15
  170. data/lib/principles/views/problem_lines.rb +3 -1
  171. data/lib/principles/views/problem_result.rb +8 -3
  172. data/lib/principles/views/prompts/acknowledge-a-change/run-the-check-and-report-it-passing.md +3 -0
  173. data/lib/principles/views/prompts/acknowledge-a-change/say-whether-the-change-keeps-the-meaning.md +6 -12
  174. data/lib/principles/views/prompts/assess-every-member/answer-guide.json +67 -48
  175. data/lib/principles/views/prompts/assess-every-member/assess-each-member-and-the-set.md +0 -4
  176. data/lib/principles/views/prompts/assess-every-member/claim-an-earlier-first-raised-at.md +7 -0
  177. data/lib/principles/views/prompts/assess-every-member/decide-each-prior-finding.md +4 -16
  178. data/lib/principles/views/prompts/assess-every-member/judge-the-set-with-carried-assessments.md +5 -10
  179. data/lib/principles/views/prompts/assess-every-member/keep-or-change-what-a-deferral-waits-on.md +4 -0
  180. data/lib/principles/views/prompts/assess-every-member/read-a-compact-carried-member.md +8 -0
  181. data/lib/principles/views/prompts/assess-every-member/read-a-trimmed-instruction-whole.md +1 -0
  182. data/lib/principles/views/prompts/assess-every-member/review-a-new-or-changed-check-before-running-it.md +9 -0
  183. data/lib/principles/views/prompts/assess-every-member/say-what-observed-means.md +4 -0
  184. data/lib/principles/views/prompts/dispose-of-each-finding/answer-guide.json +1 -0
  185. data/lib/principles/views/prompts/dispose-of-each-finding/fix-or-record-each-finding.md +16 -3
  186. data/lib/principles/views/prompts/dispose-of-each-finding/retain-assessment-and-correction.md +4 -0
  187. data/lib/principles/views/prompts/find-a-principle-to-map/ask-about-other-guidance-search-paths.md +6 -0
  188. data/lib/principles/views/prompts/find-a-principle-to-map/ask-the-owner-after-the-table.md +1 -0
  189. data/lib/principles/views/prompts/find-a-principle-to-map/ask-which-principle-to-map.md +2 -0
  190. data/lib/principles/views/prompts/find-a-principle-to-map/choose-an-existing-or-new-principle.md +2 -0
  191. data/lib/principles/views/prompts/find-a-principle-to-map/choose-one-principle.md +11 -5
  192. data/lib/principles/views/prompts/find-a-principle-to-map/compare-with-mapped-principles.md +11 -0
  193. data/lib/principles/views/prompts/find-a-principle-to-map/leave-test-data-out-of-the-survey.md +1 -0
  194. data/lib/principles/views/prompts/find-a-principle-to-map/read-a-cut-section-back.md +3 -0
  195. data/lib/principles/views/prompts/find-a-principle-to-map/read-left-out-files.md +3 -0
  196. data/lib/principles/views/prompts/find-a-principle-to-map/survey-a-later-range.md +3 -0
  197. data/lib/principles/views/prompts/find-a-principle-to-map/what-each-command-writes.md +4 -4
  198. data/lib/principles/views/prompts/find-a-principle-to-map/where-the-inventory-looks.md +5 -4
  199. data/lib/principles/views/prompts/learn-what-a-command-does/add-guidance-search-path.md +17 -0
  200. data/lib/principles/views/prompts/learn-what-a-command-does/add-member.md +35 -6
  201. data/lib/principles/views/prompts/learn-what-a-command-does/create-principle.md +16 -0
  202. data/lib/principles/views/prompts/learn-what-a-command-does/list-candidate-files.md +55 -7
  203. data/lib/principles/views/prompts/learn-what-a-command-does/list-member-problems.md +9 -8
  204. data/lib/principles/views/prompts/learn-what-a-command-does/list-open-findings.md +10 -11
  205. data/lib/principles/views/prompts/learn-what-a-command-does/list-principles.md +1 -2
  206. data/lib/principles/views/prompts/learn-what-a-command-does/move-member.md +5 -0
  207. data/lib/principles/views/prompts/learn-what-a-command-does/remove-guidance-search-path.md +13 -0
  208. data/lib/principles/views/prompts/learn-what-a-command-does/remove-principle.md +25 -0
  209. data/lib/principles/views/prompts/learn-what-a-command-does/show-answer-check.md +27 -1
  210. data/lib/principles/views/prompts/learn-what-a-command-does/show-candidate-outlines.md +21 -0
  211. data/lib/principles/views/prompts/learn-what-a-command-does/show-meta-principle-passage.md +5 -5
  212. data/lib/principles/views/prompts/learn-what-a-command-does/start-audit.md +24 -24
  213. data/lib/principles/views/prompts/learn-what-a-command-does/submit-answer.md +51 -54
  214. data/lib/principles/views/prompts/read-requested-outlines/continue-the-survey.md +7 -0
  215. data/lib/principles/views/prompts/report-after-a-commit/the-principles-skill-is-where-to-start.md +1 -0
  216. data/lib/principles/views/prompts/set-principles-up/keep-them-held-between-audits.md +2 -0
  217. data/lib/principles/views/prompts/set-principles-up/name-the-step-each-member-lacks.md +2 -0
  218. data/lib/principles/views/prompts/set-principles-up/place-the-passage-with-the-user.md +9 -6
  219. data/lib/principles/views/prompts/start-a-session/see-the-earlier-line.md +2 -0
  220. data/lib/principles/views/record_waiting_on_an_upgrade.rb +3 -3
  221. data/lib/principles/views/request_part.rb +16 -8
  222. data/lib/principles/views/request_parts.rb +4 -0
  223. metadata +92 -7
  224. data/lib/principles/models/candidate_listing.rb +0 -32
  225. data/lib/principles/views/prompts/find-a-principle-to-map/ask-the-owner-before-mapping.md +0 -1
  226. data/lib/principles/views/prompts/report-after-a-commit/the-map-and-audit-skill-says-how-to-audit-these-members.md +0 -1
  227. data/lib/principles/views/prompts/set-principles-up/set-up-first.md +0 -2
@@ -0,0 +1,132 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "principle_lineage"
5
+ require_relative "principle_map_revision"
6
+
7
+ module Principles
8
+ # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
9
+ module Models
10
+ # Immutable map selections for automatic history, bounded by its latest proven incarnation.
11
+ class PrincipleMappingHistory
12
+ #: (current: Principle, project: Project) -> PrincipleMappingHistory
13
+ def self.load(current:, project:) = new(current, project)
14
+
15
+ #: (current: Principle, project: Project) -> PrincipleMappingHistory?
16
+ def self.load_if_linked(current:, project:)
17
+ load(current: current, project: project) if PrinciplePredecessor.from(current, principles: project.principles)
18
+ end
19
+
20
+ #: Hash[String, PrincipleRevision | PrincipleMapRevision]
21
+ attr_reader :revisions
22
+ #: PrincipleRevision | PrincipleMapRevision
23
+ attr_reader :origin
24
+ #: Array[String]
25
+ attr_reader :map_paths
26
+
27
+ #: (Principle, Project) -> void
28
+ def initialize(current, project)
29
+ declared = PrincipleLineage.load(current: current, project: project)
30
+ @revisions = declared.revisions.dup #: Hash[String, PrincipleRevision | PrincipleMapRevision]
31
+ @origin = declared.oldest_revision #: PrincipleRevision | PrincipleMapRevision
32
+ extend_origin(project)
33
+ @map_paths = paths_for_maps(current, project.principles) #: Array[String]
34
+ @revisions.freeze
35
+ freeze
36
+ end
37
+ private_class_method :new
38
+
39
+ #: (String) -> Principle
40
+ def map_at(commit)
41
+ selected = @revisions[commit]
42
+ selected&.map || raise(PrinciplePublication::ProofError.new(
43
+ "git_history_unavailable", "#{commit} has no selected map in this automatic history",
44
+ PrinciplePublication::ProofError::REPAIR
45
+ ).including("selected_commit" => commit))
46
+ end
47
+
48
+ # The selected child permits this raw fact read, not selection of the returned parent's map.
49
+ #: (String, captured: CapturedHead) -> String?
50
+ def first_parent_of(commit, captured:)
51
+ map_at(commit)
52
+ read_first_parent(commit, captured)
53
+ end
54
+
55
+ #: (String, captured: CapturedHead) -> Principle
56
+ def map_at_revision(revision, captured:)
57
+ return map_at(revision) unless revision.end_with?("^")
58
+
59
+ child = revision.delete_suffix("^")
60
+ parent = first_parent_of(child, captured: captured)
61
+ selected_parent_map(child, parent)
62
+ end
63
+
64
+ private
65
+
66
+ #: (Principle, MappedPrinciples) -> Array[String]
67
+ def paths_for_maps(current, maps)
68
+ ids = [current.id, *@revisions.values.map(&:id)].uniq
69
+ ids.flat_map { |id| [maps.file(id), maps.earlier_file(id)] }.map { |path| path.dup.freeze }.freeze
70
+ end
71
+
72
+ #: (String, String?) -> Principle
73
+ def selected_parent_map(child, parent)
74
+ raise root_without_parent(child) unless parent
75
+
76
+ map_at(parent)
77
+ rescue Error => e
78
+ raise PrinciplePublication::ProofError.from(e).including(
79
+ "child_commit" => child, "parent_commit" => parent, "check_phase" => "parent_map"
80
+ )
81
+ end
82
+
83
+ #: (String) -> PrinciplePublication::ProofError
84
+ def root_without_parent(child)
85
+ PrinciplePublication::ProofError.new(
86
+ "git_history_unavailable", "#{child} is an actual root with no first parent",
87
+ "Choose a selected revision with an in-route parent; preserve current map and audit evidence."
88
+ )
89
+ end
90
+
91
+ #: (String, CapturedHead) -> String?
92
+ def read_first_parent(commit, captured)
93
+ captured.first_parent_of(commit)
94
+ rescue Error => e
95
+ raise PrinciplePublication::ProofError.from(e).including(
96
+ "child_commit" => commit, "check_phase" => "parent_read"
97
+ )
98
+ end
99
+
100
+ #: (Project) -> void
101
+ def extend_origin(project)
102
+ captured = project.history.captured_head
103
+ loop do
104
+ parent = previous_commit(captured) or break
105
+ snapshot = earlier_map(parent, project)
106
+ break unless snapshot.map
107
+
108
+ @revisions[snapshot.commit] = snapshot
109
+ @origin = snapshot
110
+ end
111
+ end
112
+
113
+ #: (CapturedHead) -> String?
114
+ def previous_commit(captured)
115
+ captured.first_parent_of(@origin.commit)
116
+ rescue Error => e
117
+ raise PrinciplePublication::ProofError.from(e).including(
118
+ "selected_id" => @origin.id, "selected_commit" => @origin.commit, "check_phase" => "origin_parent"
119
+ )
120
+ end
121
+
122
+ #: (String, Project) -> PrincipleMapRevision
123
+ def earlier_map(commit, project)
124
+ PrincipleMapRevision.load(@origin.id, commit, project: project)
125
+ rescue Error => e
126
+ raise PrinciplePublication::ProofError.from(e).including(
127
+ "selected_id" => @origin.id, "inspected_commit" => commit, "check_phase" => "origin_map"
128
+ )
129
+ end
130
+ end
131
+ end
132
+ end
@@ -58,6 +58,20 @@ module Principles
58
58
  #: (Array[String]) -> Array[Member]
59
59
  def in_files(paths) = @list.select { |member| paths.include?(member.path) }
60
60
 
61
+ # Each member in one of paths, by its id and path, as show-meta-principle-passage reports the members already in
62
+ # an entry file, which the agent judges against the passage (D68).
63
+ #: (Array[String]) -> Array[Hash[String, String]]
64
+ def members_in(paths)
65
+ in_files(paths).map { |member| { "id" => member.id, "path" => member.path } }
66
+ end
67
+
68
+ # A small first map prompts an agent question, not a minimum member count or a verdict of incompleteness.
69
+ # no_completed_audit is known absence from the record owner; false also covers unknown state.
70
+ #: (no_completed_audit: bool) -> bool
71
+ def asks_about_completeness?(no_completed_audit:)
72
+ (1..2).cover?(count) && no_completed_audit
73
+ end
74
+
61
75
  # The member with member_id now, or nil when none has it.
62
76
  #: (untyped) -> Member?
63
77
  def with_id(member_id) = @list.find { |member| member.id == member_id }
@@ -71,6 +85,17 @@ module Principles
71
85
  #: (untyped) -> String?
72
86
  def check(member_id) = with_id(member_id)&.check
73
87
 
88
+ # The id of each member that names a check, in the map's order (#1090).
89
+ #: () -> Array[String]
90
+ def checked_ids = @list.select(&:check).map(&:id)
91
+
92
+ # Whether a member has member_id and names no check, so its assessment takes no check_result (#1088).
93
+ #: (untyped) -> bool
94
+ def unchecked?(member_id)
95
+ member = with_id(member_id)
96
+ !member.nil? && member.check.nil?
97
+ end
98
+
74
99
  # 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
100
  # the members that share it. An audit record files each member under its key, so one would stand for the other.
76
101
  #: () -> Hash[String, Array[String]]
@@ -0,0 +1,45 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require "json"
5
+ require_relative "../error"
6
+ require_relative "principle"
7
+ require_relative "mapped_principles"
8
+
9
+ module Principles
10
+ # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
11
+ module Models
12
+ # One exact source identity/revision named by a supported linked map, distinct from legacy unknown metadata.
13
+ class PrinciplePredecessor
14
+ #: (Principle, principles: MappedPrinciples) -> PrinciplePredecessor?
15
+ def self.from(current, principles:)
16
+ marker = current.to_h.fetch("format", 1)
17
+ return nil if marker.is_a?(Integer) && (1..2).cover?(marker)
18
+
19
+ checked = principles.parsed_history(current.id, JSON.generate(current.to_h), commit: nil)
20
+ return nil unless checked.to_h["format"] == 3
21
+
22
+ new(current.id, checked.to_h.fetch("predecessor"), principles.file(current.id))
23
+ end
24
+
25
+ #: String
26
+ attr_reader :principle_id, :commit
27
+
28
+ #: (String, Hash[String, untyped], String) -> void
29
+ def initialize(target, fields, path)
30
+ @principle_id = fields.fetch("principle_id").dup.freeze #: String
31
+ @commit = fields.fetch("commit").dup.freeze #: String
32
+ if @principle_id == target
33
+ raise Error.new("invalid_map", "#{target} cannot name itself as its immediate predecessor",
34
+ "Restore the exact predecessor from a supported ID change. Preserve map and audit files " \
35
+ "for inspection.", path)
36
+ end
37
+ freeze
38
+ end
39
+ private_class_method :new
40
+
41
+ #: () -> Hash[String, String]
42
+ def to_h = { "principle_id" => @principle_id, "commit" => @commit }.freeze
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,177 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "../error"
5
+ require_relative "principle"
6
+ require_relative "project_history"
7
+ require_relative "mapped_principles"
8
+ require_relative "principle_predecessor"
9
+ require_relative "principle_revision"
10
+
11
+ module Principles
12
+ # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
13
+ module Models
14
+ # Exact committed source/target selections within one predecessor's publication interval.
15
+ class PrinciplePublication
16
+ # A required identity proof failed: retain cause facts and give linked history an applicable repair.
17
+ class ProofError < Error
18
+ REPAIR = "Preserve all surviving map and audit copies. Restore complete ancestry and inspect the " \
19
+ "exact source anchor and coherent publication; do not retry a partial publication blindly."
20
+
21
+ #: (Error) -> ProofError
22
+ def self.from(error)
23
+ return error if error.is_a?(ProofError)
24
+
25
+ code = error.payload.fetch("code")
26
+ repair = code == "git_history_unavailable" ? REPAIR : error.payload.fetch("repair")
27
+ wrapped = new(code, error.message, repair)
28
+ wrapped.including(error.payload.except("repair"))
29
+ wrapped.ending_with(error.commands)
30
+ wrapped
31
+ end
32
+ end
33
+
34
+ #: (current: Principle, project: Project) -> PrinciplePublication
35
+ def self.load(current:, project:) = from(current, project, nil)
36
+
37
+ # This prefix proves only its bounds; its caller selects the endpoint from an enclosing certified interval.
38
+ #: (PrincipleRevision, project: Project) -> PrinciplePublication
39
+ def self.ending_at(revision, project:)
40
+ current = revision.map or
41
+ raise ProofError.new("git_history_unavailable", "#{revision.id} has no map at #{revision.commit}",
42
+ ProofError::REPAIR)
43
+ from(current, project, revision.commit)
44
+ end
45
+
46
+ #: (Principle, Project, String?) -> PrinciplePublication
47
+ def self.from(current, project, through)
48
+ maps = project.principles
49
+ predecessor = PrinciplePredecessor.from(current, principles: maps) or
50
+ raise ProofError.new("git_history_unavailable", "#{current.id} has no supported predecessor",
51
+ "Use direct-ID history for an unlinked map.", maps.file(current.id))
52
+ new(current.id, predecessor, project, through)
53
+ rescue Error => e
54
+ raise ProofError.from(e)
55
+ end
56
+ private_class_method :from
57
+
58
+ #: (String, PrinciplePredecessor, Project, String?) -> void
59
+ def initialize(target, predecessor, project, through)
60
+ @target = target.dup.freeze #: String
61
+ @predecessor = predecessor #: PrinciplePredecessor
62
+ @map_paths = selected_paths(project.principles) #: Array[String]
63
+ @audit_paths = selected_paths(project.audit_records) #: Array[String]
64
+ @states = certify(through, project: project).freeze #: Hash[String, PrincipleRevision]
65
+ freeze
66
+ end
67
+ private_class_method :new
68
+
69
+ #: (String) -> Principle
70
+ def map_at(revision) = @states.fetch(revision) { raise unavailable }.map || raise(unavailable)
71
+
72
+ #: (String) -> AuditRecord?
73
+ def audit_at(revision) = @states.fetch(revision) { raise unavailable }.audit
74
+
75
+ #: () -> PrincipleRevision
76
+ def predecessor_revision = @states.fetch(@predecessor.commit)
77
+
78
+ # Immutable exact snapshots already certified by this publication interval.
79
+ #: () -> Hash[String, PrincipleRevision]
80
+ def revisions = @states
81
+
82
+ # Every certified commit including both endpoints, newest first; an unchanged map never hides member edits.
83
+ #: () -> Array[[String, Principle]]
84
+ def mapping_states = @states.map { |sha, state| [sha, state.map || raise(unavailable)].freeze }.freeze
85
+
86
+ private
87
+
88
+ #: (String?, project: Project) -> Hash[String, PrincipleRevision]
89
+ def certify(through, project:)
90
+ source = nil #: PrincipleRevision?
91
+ source = capture_source(project)
92
+ read_interval(source, through, project: project)
93
+ rescue Error => e
94
+ facts = publication_coordinates
95
+ facts["source_hashes"] = source.fingerprints if source
96
+ raise ProofError.from(e).including(facts)
97
+ end
98
+
99
+ #: (MappedPrinciples | AuditRecords) -> Array[String]
100
+ def selected_paths(files)
101
+ [@predecessor.principle_id, @target].flat_map { |id| [files.file(id), files.earlier_file(id)] }
102
+ .map(&:freeze).freeze
103
+ end
104
+
105
+ #: () -> Hash[String, untyped]
106
+ def publication_coordinates
107
+ { "source_id" => @predecessor.principle_id, "source_commit" => @predecessor.commit,
108
+ "target_id" => @target, "map_paths" => @map_paths, "audit_paths" => @audit_paths }
109
+ end
110
+
111
+ #: (Project) -> PrincipleRevision
112
+ def capture_source(project)
113
+ source = PrincipleRevision.load(@predecessor.principle_id, @predecessor.commit, project: project)
114
+ unless source.map
115
+ raise unavailable.including("inspected_commit" => source.commit, "check_phase" => "source_anchor",
116
+ "source_hashes" => source.fingerprints)
117
+ end
118
+
119
+ source
120
+ end
121
+
122
+ #: (PrincipleRevision, String?, project: Project) -> Hash[String, PrincipleRevision]
123
+ def read_interval(source, through, project:)
124
+ @published_at = nil #: String?
125
+ captured = project.history.captured_head
126
+ head = through || captured.commit or raise unavailable
127
+ commits = captured.first_parent_range(@predecessor.commit, through: head).commits
128
+ commits.reverse.map { |sha| [sha, selected_at(sha, source, project: project)] }.reverse.to_h
129
+ end
130
+
131
+ #: (String, PrincipleRevision, project: Project) -> PrincipleRevision
132
+ def selected_at(sha, original, project:)
133
+ target = PrincipleRevision.load(@target, sha, project: project)
134
+ return checked_target(target) if @published_at
135
+
136
+ source = PrincipleRevision.load(@predecessor.principle_id, sha, project: project)
137
+ return source if target.absent? && source.unchanged_bytes?(original)
138
+ unless source.absent? && target.retains_audit?(original) && target.retains_metadata?(original)
139
+ raise inconsistent_pair(source, target)
140
+ end
141
+
142
+ map = checked_target(target)
143
+ @published_at = sha
144
+ map
145
+ end
146
+
147
+ #: (PrincipleRevision, PrincipleRevision) -> Error
148
+ def inconsistent_pair(source, target)
149
+ unavailable.including("inspected_commit" => target.commit, "check_phase" => "publication_pair",
150
+ "inspected_source_hashes" => source.fingerprints,
151
+ "inspected_target_hashes" => target.fingerprints)
152
+ end
153
+
154
+ #: (PrincipleRevision) -> PrincipleRevision
155
+ def checked_target(state)
156
+ map = state.map or raise unavailable
157
+
158
+ raise unavailable unless map.to_h["format"] == 3 && map.to_h["predecessor"] == @predecessor.to_h
159
+
160
+ state
161
+ rescue Error => e
162
+ raise ProofError.from(e).including("inspected_commit" => state.commit, "check_phase" => "destination_link",
163
+ "inspected_target_hashes" => state.fingerprints)
164
+ end
165
+
166
+ #: () -> ProofError
167
+ def unavailable
168
+ error = ProofError.new(
169
+ "git_history_unavailable", "The linked principle's publication interval cannot be verified",
170
+ ProofError::REPAIR, @map_paths.fetch(2)
171
+ )
172
+ error.including(publication_coordinates)
173
+ error
174
+ end
175
+ end
176
+ end
177
+ end
@@ -0,0 +1,104 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require "digest"
5
+ require_relative "../error"
6
+ require_relative "project"
7
+ require_relative "principle"
8
+ require_relative "audit_record"
9
+ require_relative "principle_file_revision"
10
+
11
+ module Principles
12
+ # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
13
+ module Models
14
+ # The exact committed map and optional completed audit selected under one identity and revision.
15
+ class PrincipleRevision
16
+ #: (String, String, project: Project) -> PrincipleRevision
17
+ def self.load(id, commit, project:) = new(id, commit, project)
18
+
19
+ #: String
20
+ attr_reader :id, :commit
21
+ #: Principle?
22
+ attr_reader :map
23
+ #: AuditRecord?
24
+ attr_reader :audit
25
+
26
+ #: (String, String, Project) -> void
27
+ def initialize(id, commit, project)
28
+ @id = id.dup.freeze #: String
29
+ @commit = commit.dup.freeze #: String
30
+ captured = project.history.captured_head
31
+ @map_text = nil #: String?
32
+ @audit_text = nil #: String?
33
+ @map = selected_map(project.principles, captured) #: Principle?
34
+ @audit = selected_audit(project.audit_records, captured) #: AuditRecord?
35
+ freeze
36
+ end
37
+
38
+ #: () -> bool
39
+ def absent? = @map.nil? && @audit.nil?
40
+
41
+ # Both files were captured completely: null is known absence, never a failed read.
42
+ #: () -> Hash[String, String?]
43
+ def fingerprints
44
+ { "map_sha256" => @map_text && Digest::SHA256.hexdigest(@map_text).freeze,
45
+ "audit_sha256" => @audit_text && Digest::SHA256.hexdigest(@audit_text).freeze }.freeze
46
+ end
47
+
48
+ #: (PrincipleRevision) -> bool
49
+ def unchanged_bytes?(other)
50
+ @id == other.id && @map_text == other.map_text && @audit_text == other.audit_text
51
+ end
52
+
53
+ # One selected identity/revision must agree in raw fingerprints and every interpreted map/audit fact.
54
+ #: (PrincipleRevision) -> bool
55
+ def same_selection?(other) = selection_facts == other.selection_facts
56
+
57
+ #: (PrincipleRevision) -> bool
58
+ def retains_audit?(source)
59
+ @audit&.to_h&.except("principle_id") == source.audit&.to_h&.except("principle_id")
60
+ end
61
+
62
+ #: (PrincipleRevision) -> bool
63
+ def retains_metadata?(source)
64
+ @map&.to_h&.except("description", "members", "format", "predecessor") ==
65
+ source.map&.to_h&.except("description", "members", "format", "predecessor")
66
+ end
67
+
68
+ protected
69
+
70
+ #: String?
71
+ attr_reader :map_text, :audit_text
72
+
73
+ # The complete named value used only for equality; nested snapshot facts are already owned and immutable.
74
+ #: () -> Hash[String, untyped]
75
+ def selection_facts
76
+ { "principle_id" => @id, "commit" => @commit, "fingerprints" => fingerprints,
77
+ "map" => @map&.to_h, "audit" => @audit&.to_h }.freeze
78
+ end
79
+
80
+ private
81
+
82
+ #: (MappedPrinciples, CapturedHead) -> Principle?
83
+ def selected_map(maps, captured)
84
+ @map_text = selected_text([maps.file(@id), maps.earlier_file(@id)], captured)
85
+ @map_text && maps.parsed_history(@id, @map_text, commit: @commit)
86
+ rescue Error => e
87
+ raise e.including("inspected_commit" => @commit, "check_phase" => "capture_map")
88
+ end
89
+
90
+ #: (AuditRecords, CapturedHead) -> AuditRecord?
91
+ def selected_audit(records, captured)
92
+ @audit_text = selected_text([records.file(@id), records.earlier_file(@id)], captured)
93
+ @audit_text && records.parsed_history(@id, @audit_text)
94
+ rescue Error => e
95
+ raise e.including("inspected_commit" => @commit, "check_phase" => "capture_audit")
96
+ end
97
+
98
+ #: (Array[String], CapturedHead) -> String?
99
+ def selected_text(paths, captured)
100
+ PrincipleFileRevision.load(@id, @commit, paths: paths, captured: captured).text
101
+ end
102
+ end
103
+ end
104
+ end
@@ -22,13 +22,14 @@ module Principles
22
22
  # The placeholder printed in each field a decision may give: only which decision takes it, and what, since that
23
23
  # a field left as printed is not given is stated once, at the template's head, and each finding printed every
24
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).
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).
26
27
  PLACEHOLDERS = TemplatePlaceholders.new(
27
28
  {
28
29
  "reason" => "resolved, withdrawn, new action: why, in a line; else leave",
29
30
  "as" => "raised_again: the new finding's id; else leave",
30
31
  "evidence" => [{ "path" => "resolved: where the fix shows, with heading; else leave" }],
31
- "action" => "still_holds: deferred or accepted; else kept",
32
+ "action" => "to change it: deferred or accepted; else kept",
32
33
  "recorded_at" => "still_holds: a write-up pointer or null; else kept",
33
34
  "waits_on" => "deferral: whom it waits on, for what, or null; else kept",
34
35
  "problem" => "still_holds: restated if renamed or moved; else kept",
@@ -20,6 +20,9 @@ module Principles
20
20
  class PriorFindingDecision
21
21
  KINDS = %w[still_holds resolved withdrawn raised_again].freeze
22
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]
23
26
  # What the completed result shows of each closed finding.
24
27
  CLOSED_FIELDS = %w[finding_id decision reason evidence].freeze
25
28
  REPAIR = "Decide it still_holds, raised_again with as, or resolved or withdrawn with a one-line reason."
@@ -98,19 +101,26 @@ module Principles
98
101
  unless @decision["decision"] == "still_holds"
99
102
  invalid("only still_holds may give action", "Drop action from #{@id}'s decision, or decide it still_holds.")
100
103
  end
101
- 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)
102
105
  changed_action(action)
103
106
  end
104
107
 
105
108
  #: (String) -> void
106
109
  def changed_action(action)
107
- if action == @finding.action
108
- invalid("it is already #{action}", "Drop action from #{@id}'s decision; it is already #{action}.")
109
- end
110
+ already(action) if action == @finding.action
110
111
  invalid("a new action needs a one-line reason") unless one_line_reason?
111
112
  unrecorded_deferral if action == "deferred" && !@decision.key?("recorded_at")
112
113
  end
113
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
+
114
124
  #: () -> bot
115
125
  def unrecorded_deferral
116
126
  invalid("deferring it needs recorded_at, or null when it is written up nowhere",
@@ -21,15 +21,17 @@ module Principles
21
21
  end
22
22
 
23
23
  # The changed members and the count of each state, refused when any member is broken. Given a block, each
24
- # changed member gains the history the block makes of its map id, member, baseline hash and hash now.
25
- #: () ?{ (String, Member, String, String) -> Hash[String, untyped] } -> Hash[String, untyped]
24
+ # changed member gains the history the block makes of its map id, member, baseline hash, hash now and
25
+ # the selected automatic history when its mapping supplies the baseline.
26
+ #: () ?{ (String, Member, String, String, PrincipleMappingHistory?) -> Hash[String, untyped] }
27
+ #| -> Hash[String, untyped]
26
28
  def checked(&)
27
29
  broken = @maps.flat_map(&:broken)
28
30
  refuse(broken) unless broken.empty?
29
31
 
30
32
  { "members" => changed(&),
31
33
  "counts" => @maps.flat_map(&:entries).map { |each| each.fetch("state") }.tally.sort.to_h,
32
- "compared_with" => compared_with, **no_baseline_because, **awaiting_first_audit_field }
34
+ "compared_with" => compared_with, **no_baseline_because, **awaiting_first_audit_field, **mapping_facts }
33
35
  end
34
36
 
35
37
  # The principles that await their first full audit, in map order, which list-member-problems names in one
@@ -72,12 +74,15 @@ module Principles
72
74
  @maps.to_h { |map| [map.principle_id, map.no_baseline_paths] }.reject { |_, paths| paths.empty? }
73
75
  end
74
76
 
75
- #: () ?{ (String, Member, String, String) -> Hash[String, untyped] } -> Array[Hash[String, untyped]]
77
+ #: () ?{ (String, Member, String, String, PrincipleMappingHistory?) -> Hash[String, untyped] }
78
+ #| -> Array[Hash[String, untyped]]
76
79
  def changed(&history)
77
80
  @maps.flat_map do |map|
78
81
  next map.changed unless history
79
82
 
80
- map.changed { |member, baseline, now| history.call(map.principle_id, member, baseline, now) }
83
+ map.changed do |member, baseline, now, mapping|
84
+ history.call(map.principle_id, member, baseline, now, mapping)
85
+ end
81
86
  end
82
87
  end
83
88
 
@@ -101,6 +106,13 @@ module Principles
101
106
  awaiting.empty? ? {} : { "awaiting_first_audit" => awaiting }
102
107
  end
103
108
 
109
+ # Completion and empty-map facts, distinct from whether members have a baseline ready for a first audit.
110
+ #: () -> Hash[String, Array[String]]
111
+ def mapping_facts
112
+ { "no_completed_audit" => @maps.select(&:no_completed_audit?).map(&:principle_id),
113
+ "empty_maps" => @maps.select(&:empty_map?).map(&:principle_id) }.reject { |_key, ids| ids.empty? }
114
+ end
115
+
104
116
  #: (Array[Hash[String, untyped]]) -> bot
105
117
  def refuse(broken)
106
118
  named = broken.map { |each| [each["principle"], each["id"]].compact.join(" ") }