principles 0.0.5 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (192) hide show
  1. checksums.yaml +4 -4
  2. data/lib/principles/after_commit_hook_options.rb +4 -1
  3. data/lib/principles/cli.rb +9 -0
  4. data/lib/principles/command_line.rb +31 -30
  5. data/lib/principles/command_routes.rb +9 -3
  6. data/lib/principles/controllers/acknowledgement.rb +82 -95
  7. data/lib/principles/controllers/answer.rb +14 -11
  8. data/lib/principles/controllers/assessment.rb +27 -24
  9. data/lib/principles/controllers/audit.rb +24 -4
  10. data/lib/principles/controllers/audit_completion.rb +10 -4
  11. data/lib/principles/controllers/command.rb +2 -2
  12. data/lib/principles/controllers/correction.rb +16 -7
  13. data/lib/principles/controllers/full_audit_request.rb +8 -3
  14. data/lib/principles/controllers/guidance_search_paths.rb +31 -0
  15. data/lib/principles/controllers/inventory.rb +3 -22
  16. data/lib/principles/controllers/mapping.rb +9 -1
  17. data/lib/principles/controllers/member_acknowledgement.rb +137 -0
  18. data/lib/principles/controllers/member_addition.rb +65 -0
  19. data/lib/principles/controllers/member_entries.rb +6 -24
  20. data/lib/principles/controllers/member_problems_report.rb +33 -25
  21. data/lib/principles/controllers/members_to_assess.rb +1 -1
  22. data/lib/principles/controllers/meta_principle_setup.rb +1 -1
  23. data/lib/principles/controllers/repoint.rb +5 -2
  24. data/lib/principles/controllers/survey.rb +60 -0
  25. data/lib/principles/data/audit_files.rb +4 -0
  26. data/lib/principles/data/candidate_files_found.rb +48 -13
  27. data/lib/principles/data/command-steps.json +86 -25
  28. data/lib/principles/data/committed_tree.rb +84 -0
  29. data/lib/principles/data/first_parent_log.rb +48 -0
  30. data/lib/principles/data/first_parent_range.rb +122 -0
  31. data/lib/principles/data/git_command.rb +37 -0
  32. data/lib/principles/data/guidance_search_paths_file.rb +107 -0
  33. data/lib/principles/data/map_files.rb +35 -10
  34. data/lib/principles/data/principles_directory.rb +40 -0
  35. data/lib/principles/data/principles_subfolder.rb +39 -16
  36. data/lib/principles/data/project_files.rb +11 -3
  37. data/lib/principles/data/repository.rb +55 -29
  38. data/lib/principles/data/requested_candidate_files.rb +43 -0
  39. data/lib/principles/data/running_version.rb +3 -5
  40. data/lib/principles/data/schemas/acknowledgement-answer.schema.json +6 -6
  41. data/lib/principles/data/schemas/acknowledgements-answer.schema.json +88 -0
  42. data/lib/principles/data/schemas/answer-guide.schema.json +255 -63
  43. data/lib/principles/data/schemas/assessment-answer.schema.json +15 -1
  44. data/lib/principles/data/schemas/command-steps.schema.json +1 -1
  45. data/lib/principles/data/schemas/correction-answer-guide.schema.json +2 -1
  46. data/lib/principles/data/schemas/correction-answer.schema.json +12 -54
  47. data/lib/principles/data/schemas/guidance-search-paths.schema.json +13 -0
  48. data/lib/principles/data/schemas/member-map-history.schema.json +20 -0
  49. data/lib/principles/data/verified_head.rb +162 -0
  50. data/lib/principles/invocation_options.rb +5 -4
  51. data/lib/principles/list_open_findings_options.rb +23 -0
  52. data/lib/principles/models/acknowledged_check.rb +94 -0
  53. data/lib/principles/models/acknowledgement.rb +40 -36
  54. data/lib/principles/models/acknowledgement_left_as_printed.rb +72 -0
  55. data/lib/principles/models/acknowledgements.rb +99 -0
  56. data/lib/principles/models/answered_time.rb +9 -0
  57. data/lib/principles/models/assessment_report.rb +13 -13
  58. data/lib/principles/models/audit.rb +17 -4
  59. data/lib/principles/models/audit_entry_protecting_map.rb +28 -0
  60. data/lib/principles/models/audit_record.rb +9 -0
  61. data/lib/principles/models/audit_records.rb +34 -8
  62. data/lib/principles/models/candidate_files.rb +17 -6
  63. data/lib/principles/models/candidate_inventory.rb +51 -7
  64. data/lib/principles/models/captured_head.rb +72 -0
  65. data/lib/principles/models/check_since_last_audit.rb +4 -8
  66. data/lib/principles/models/correction_basis.rb +14 -5
  67. data/lib/principles/models/declared_map_changes.rb +25 -9
  68. data/lib/principles/models/declared_member_removals.rb +75 -0
  69. data/lib/principles/models/disposition.rb +4 -2
  70. data/lib/principles/models/fix_only_fields.rb +12 -0
  71. data/lib/principles/models/guidance_search_path.rb +26 -0
  72. data/lib/principles/models/guidance_search_paths.rb +57 -0
  73. data/lib/principles/models/line_span.rb +6 -0
  74. data/lib/principles/models/list_member_problems_check.rb +80 -0
  75. data/lib/principles/models/listed_members.rb +49 -0
  76. data/lib/principles/models/loaded_files.rb +1 -2
  77. data/lib/principles/models/loading_decision.rb +17 -8
  78. data/lib/principles/models/loading_scope.rb +61 -0
  79. data/lib/principles/models/loads_to_check.rb +0 -1
  80. data/lib/principles/models/locator.rb +6 -0
  81. data/lib/principles/models/mapped_principles.rb +39 -37
  82. data/lib/principles/models/member_assessments.rb +11 -2
  83. data/lib/principles/models/member_file.rb +20 -71
  84. data/lib/principles/models/member_history.rb +7 -5
  85. data/lib/principles/models/member_loading_scopes.rb +49 -0
  86. data/lib/principles/models/member_loads.rb +4 -0
  87. data/lib/principles/models/member_mapping_run.rb +30 -0
  88. data/lib/principles/models/member_moves.rb +4 -0
  89. data/lib/principles/models/member_problems.rb +57 -29
  90. data/lib/principles/models/member_region.rb +129 -0
  91. data/lib/principles/models/member_repair_moves.rb +9 -11
  92. data/lib/principles/models/member_sighting.rb +5 -8
  93. data/lib/principles/models/member_snapshots.rb +14 -1
  94. data/lib/principles/models/members_in_consecutive_sections.rb +56 -0
  95. data/lib/principles/models/meta_principle.rb +14 -0
  96. data/lib/principles/models/missing_files.rb +2 -2
  97. data/lib/principles/models/other_templates_placeholders.rb +1 -1
  98. data/lib/principles/models/outline_budget.rb +29 -14
  99. data/lib/principles/models/principle.rb +14 -7
  100. data/lib/principles/models/principle_file_revision.rb +48 -0
  101. data/lib/principles/models/principle_history.rb +46 -19
  102. data/lib/principles/models/principle_lineage.rb +118 -0
  103. data/lib/principles/models/principle_map_revision.rb +39 -0
  104. data/lib/principles/models/principle_mapping_history.rb +132 -0
  105. data/lib/principles/models/principle_members.rb +14 -0
  106. data/lib/principles/models/principle_predecessor.rb +45 -0
  107. data/lib/principles/models/principle_publication.rb +177 -0
  108. data/lib/principles/models/principle_revision.rb +104 -0
  109. data/lib/principles/models/problem_report.rb +36 -9
  110. data/lib/principles/models/project.rb +47 -30
  111. data/lib/principles/models/project_history.rb +45 -25
  112. data/lib/principles/models/record_format.rb +6 -0
  113. data/lib/principles/models/reported_check.rb +16 -12
  114. data/lib/principles/models/request_id.rb +1 -1
  115. data/lib/principles/models/sent_answer.rb +35 -5
  116. data/lib/principles/models/survey_search_paths.rb +26 -0
  117. data/lib/principles/models/uncommitted_files.rb +50 -0
  118. data/lib/principles/models/unknown_principle.rb +29 -0
  119. data/lib/principles/positional_arguments.rb +38 -0
  120. data/lib/principles/session_start_options.rb +3 -3
  121. data/lib/principles/start_audit_options.rb +10 -2
  122. data/lib/principles/version.rb +2 -2
  123. data/lib/principles/views/acknowledgement_result.rb +54 -16
  124. data/lib/principles/views/added_member.rb +41 -0
  125. data/lib/principles/views/answered_request.rb +21 -0
  126. data/lib/principles/views/assessed_members.rb +26 -5
  127. data/lib/principles/views/assessment_request.rb +24 -4
  128. data/lib/principles/views/audit_result.rb +30 -27
  129. data/lib/principles/views/candidate_files_result.rb +56 -12
  130. data/lib/principles/views/candidate_outlines_result.rb +25 -0
  131. data/lib/principles/views/check_result_template.rb +18 -17
  132. data/lib/principles/views/consecutive_sections_elsewhere.rb +27 -0
  133. data/lib/principles/views/correction_request.rb +38 -10
  134. data/lib/principles/views/findings_result.rb +21 -0
  135. data/lib/principles/views/guidance_search_paths_result.rb +26 -0
  136. data/lib/principles/views/invocation.rb +19 -4
  137. data/lib/principles/views/json_with_entries_on_lines.rb +12 -4
  138. data/lib/principles/views/loads_to_check_result.rb +3 -2
  139. data/lib/principles/views/map_added_to.rb +56 -2
  140. data/lib/principles/views/map_result.rb +22 -42
  141. data/lib/principles/views/maps_needing_members_or_audit.rb +47 -0
  142. data/lib/principles/views/member_change_since_baseline.rb +2 -1
  143. data/lib/principles/views/member_repairs.rb +2 -1
  144. data/lib/principles/views/member_row.rb +61 -0
  145. data/lib/principles/views/members_a_commit_changed.rb +2 -1
  146. data/lib/principles/views/members_with_no_baseline.rb +8 -1
  147. data/lib/principles/views/new_loading_scope.rb +31 -0
  148. data/lib/principles/views/one_member_check_result_template.rb +37 -21
  149. data/lib/principles/views/open_findings_line.rb +2 -1
  150. data/lib/principles/views/principle_read_result.rb +4 -1
  151. data/lib/principles/views/principles_with_no_completed_audit.rb +9 -3
  152. data/lib/principles/views/problem_lines.rb +50 -45
  153. data/lib/principles/views/problem_result.rb +59 -25
  154. data/lib/principles/views/prompts/acknowledge-a-change/report-the-exit-status-its-own-listing-gives.md +3 -0
  155. data/lib/principles/views/prompts/acknowledge-a-change/run-the-check-and-report-it-passing.md +3 -3
  156. data/lib/principles/views/prompts/acknowledge-a-change/say-whether-each-change-keeps-its-meaning.md +8 -0
  157. data/lib/principles/views/prompts/acknowledge-a-change/say-whether-the-change-keeps-the-meaning.md +6 -6
  158. data/lib/principles/views/prompts/assess-every-member/answer-guide.json +67 -43
  159. data/lib/principles/views/prompts/assess-every-member/decide-each-member-loading.md +2 -1
  160. data/lib/principles/views/prompts/assess-every-member/run-the-check-then-report-it.md +2 -2
  161. data/lib/principles/views/prompts/assess-every-member/run-the-reviewed-check-and-say-what-its-review-rested-on.md +5 -3
  162. data/lib/principles/views/prompts/dispose-of-each-finding/answer-guide.json +1 -0
  163. data/lib/principles/views/prompts/dispose-of-each-finding/fix-or-record-each-finding.md +12 -0
  164. data/lib/principles/views/prompts/dispose-of-each-finding/retain-assessment-and-correction.md +4 -0
  165. data/lib/principles/views/prompts/find-a-principle-to-map/ask-about-other-guidance-search-paths.md +6 -0
  166. data/lib/principles/views/prompts/find-a-principle-to-map/choose-an-existing-or-new-principle.md +2 -0
  167. data/lib/principles/views/prompts/find-a-principle-to-map/choose-one-principle.md +4 -2
  168. data/lib/principles/views/prompts/find-a-principle-to-map/compare-with-mapped-principles.md +11 -0
  169. data/lib/principles/views/prompts/find-a-principle-to-map/read-left-out-files.md +2 -2
  170. data/lib/principles/views/prompts/find-a-principle-to-map/survey-a-later-range.md +3 -0
  171. data/lib/principles/views/prompts/find-a-principle-to-map/what-each-command-writes.md +4 -4
  172. data/lib/principles/views/prompts/find-a-principle-to-map/where-the-inventory-looks.md +5 -4
  173. data/lib/principles/views/prompts/learn-what-a-command-does/add-guidance-search-path.md +17 -0
  174. data/lib/principles/views/prompts/learn-what-a-command-does/add-member.md +35 -6
  175. data/lib/principles/views/prompts/learn-what-a-command-does/create-principle.md +16 -0
  176. data/lib/principles/views/prompts/learn-what-a-command-does/every-command-and-how-they-chain.md +1 -2
  177. data/lib/principles/views/prompts/learn-what-a-command-does/list-candidate-files.md +37 -4
  178. data/lib/principles/views/prompts/learn-what-a-command-does/list-member-problems.md +20 -19
  179. data/lib/principles/views/prompts/learn-what-a-command-does/list-open-findings.md +65 -61
  180. data/lib/principles/views/prompts/learn-what-a-command-does/move-member.md +5 -0
  181. data/lib/principles/views/prompts/learn-what-a-command-does/remove-guidance-search-path.md +13 -0
  182. data/lib/principles/views/prompts/learn-what-a-command-does/remove-principle.md +25 -0
  183. data/lib/principles/views/prompts/learn-what-a-command-does/show-answer-check.md +33 -7
  184. data/lib/principles/views/prompts/learn-what-a-command-does/show-candidate-outlines.md +21 -0
  185. data/lib/principles/views/prompts/learn-what-a-command-does/start-audit.md +10 -10
  186. data/lib/principles/views/prompts/learn-what-a-command-does/submit-answer.md +51 -54
  187. data/lib/principles/views/prompts/read-requested-outlines/continue-the-survey.md +7 -0
  188. data/lib/principles/views/prompts/start-a-session/act-on-the-open-findings.md +1 -1
  189. data/lib/principles/views/prompts/start-a-session/every-loaded-line-costs-every-session.md +1 -0
  190. data/lib/principles/views/prompts/start-a-session/start-with-the-principles-skill.md +1 -0
  191. metadata +67 -4
  192. data/lib/principles/models/candidate_listing.rb +0 -41
@@ -0,0 +1,118 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "principle_publication"
5
+
6
+ module Principles
7
+ # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
8
+ module Models
9
+ # Immutable selected snapshots across a principle's exact declared predecessor chain.
10
+ class PrincipleLineage
11
+ #: (current: Principle, project: Project) -> PrincipleLineage
12
+ def self.load(current:, project:)
13
+ new(current, project)
14
+ rescue Error => e
15
+ raise PrinciplePublication::ProofError.from(e)
16
+ end
17
+
18
+ #: Hash[String, PrincipleRevision]
19
+ attr_reader :revisions
20
+ #: PrincipleRevision
21
+ attr_reader :oldest_revision
22
+
23
+ #: (Principle, Project) -> void
24
+ def initialize(current, project)
25
+ @revisions = {} #: Hash[String, PrincipleRevision]
26
+ publication = PrinciplePublication.load(current: current, project: project)
27
+ @oldest_revision = collect(publication, project) #: PrincipleRevision
28
+ @revisions.freeze
29
+ freeze
30
+ end
31
+ private_class_method :new
32
+
33
+ #: (String) -> Principle
34
+ def map_at(commit)
35
+ snapshot = selected(commit)
36
+ snapshot.map || raise(no_map(snapshot))
37
+ end
38
+
39
+ #: (String) -> AuditRecord?
40
+ def audit_at(commit) = selected(commit).audit
41
+
42
+ private
43
+
44
+ #: (PrinciplePublication, Project) -> PrincipleRevision
45
+ def collect(publication, project)
46
+ capture(publication)
47
+ boundary = publication.predecessor_revision
48
+ map = boundary.map or raise no_map(boundary)
49
+ return boundary unless PrinciplePredecessor.from(map, principles: project.principles)
50
+
51
+ prefix = PrinciplePublication.ending_at(boundary, project: project)
52
+ predecessor = prefix.predecessor_revision
53
+ # ending_at proves the ordered first-parent prefix; equality would not descend to an earlier anchor.
54
+ raise non_descending(boundary, predecessor) if predecessor.commit == boundary.commit
55
+
56
+ collect(prefix, project)
57
+ end
58
+
59
+ #: (PrinciplePublication) -> void
60
+ def capture(publication)
61
+ publication.revisions.each do |commit, snapshot|
62
+ previous = @revisions[commit]
63
+ raise conflicting_selection(commit, previous, snapshot) if previous && !previous.same_selection?(snapshot)
64
+
65
+ @revisions[commit] ||= snapshot
66
+ end
67
+ end
68
+
69
+ #: (String) -> PrincipleRevision
70
+ def selected(commit) = @revisions.fetch(commit) { raise unavailable(commit) }
71
+
72
+ #: (String, PrincipleRevision, PrincipleRevision) -> PrinciplePublication::ProofError
73
+ def conflicting_selection(commit, previous, incoming)
74
+ error = PrinciplePublication::ProofError.new(
75
+ "git_history_unavailable", "The route has conflicting selected snapshots at #{commit}",
76
+ PrinciplePublication::ProofError::REPAIR
77
+ )
78
+ error.including("check_phase" => "route_overlap", "selected_commit" => commit,
79
+ "existing_id" => previous.id, "incoming_id" => incoming.id,
80
+ "existing_hashes" => previous.fingerprints, "incoming_hashes" => incoming.fingerprints)
81
+ error
82
+ end
83
+
84
+ #: (PrincipleRevision, PrincipleRevision) -> PrinciplePublication::ProofError
85
+ def non_descending(boundary, predecessor)
86
+ error = PrinciplePublication::ProofError.new(
87
+ "git_history_unavailable", "Predecessor anchor #{predecessor.commit} does not descend from " \
88
+ "selected boundary #{boundary.commit}", PrinciplePublication::ProofError::REPAIR
89
+ )
90
+ error.including("check_phase" => "route_descent", "selected_commit" => boundary.commit,
91
+ "boundary_id" => boundary.id, "predecessor_id" => predecessor.id,
92
+ "predecessor_commit" => predecessor.commit)
93
+ error
94
+ end
95
+
96
+ #: (PrincipleRevision) -> PrinciplePublication::ProofError
97
+ def no_map(snapshot)
98
+ error = PrinciplePublication::ProofError.new(
99
+ "git_history_unavailable", "#{snapshot.id} has no selected map at #{snapshot.commit}",
100
+ PrinciplePublication::ProofError::REPAIR
101
+ )
102
+ error.including("check_phase" => "route_map", "selected_commit" => snapshot.commit,
103
+ "selected_id" => snapshot.id, "selected_hashes" => snapshot.fingerprints)
104
+ error
105
+ end
106
+
107
+ #: (String) -> PrinciplePublication::ProofError
108
+ def unavailable(commit)
109
+ error = PrinciplePublication::ProofError.new(
110
+ "git_history_unavailable", "#{commit} is outside the certified declared principle route",
111
+ PrinciplePublication::ProofError::REPAIR
112
+ )
113
+ error.including("selected_commit" => commit)
114
+ error
115
+ end
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,39 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "project"
5
+ require_relative "principle_file_revision"
6
+
7
+ module Principles
8
+ # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
9
+ module Models
10
+ # One selected committed map and its identity, independent of any audit record.
11
+ class PrincipleMapRevision
12
+ #: (String, String, project: Project) -> PrincipleMapRevision
13
+ def self.load(id, commit, project:) = new(id, commit, project)
14
+
15
+ #: String
16
+ attr_reader :id, :commit
17
+ #: Principle?
18
+ attr_reader :map
19
+
20
+ #: (String, String, Project) -> void
21
+ def initialize(id, commit, project)
22
+ @id = id.dup.freeze #: String
23
+ @commit = commit.dup.freeze #: String
24
+ maps = project.principles
25
+ captured = project.history.captured_head
26
+ @physical = PrincipleFileRevision.load(@id, @commit, paths: [maps.file(@id), maps.earlier_file(@id)],
27
+ captured: captured) #: PrincipleFileRevision
28
+ text = @physical.text
29
+ @map = text && maps.parsed_history(@id, text, commit: @commit) #: Principle?
30
+ freeze
31
+ end
32
+ private_class_method :new
33
+
34
+ # Nil means both candidate entries were successfully checked and absent.
35
+ #: () -> String?
36
+ def text = @physical.text
37
+ end
38
+ end
39
+ end
@@ -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 }
@@ -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