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,142 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require "json"
5
+ require_relative "candidate_outline"
6
+ require_relative "entry_files"
7
+
8
+ module Principles
9
+ # The domain logic, which loads and saves itself through data/. models/CLAUDE.md owns the boundary.
10
+ module Models
11
+ # How much list-candidate-files may print: its listed files' outlines, cut a whole file's list items at a time
12
+ # until the result fits. The second alpha user's run printed 66,145 B for 45 files, against a ceiling of about
13
+ # 37 KB, and no cap on items under each heading both fitted and kept a core list of 8 whole (#1155).
14
+ class OutlineBudget
15
+ # The most bytes the result may print, under the host's inline limit for a tool result. Claude Code's tools
16
+ # reference, "Output limits" (code.claude.com/docs/en/tools-reference, checked 2026-11-16), gives a Bash result
17
+ # inline "up to roughly 30,000 characters by default; past that, the path of a file" and a 2,000-character
18
+ # preview. The second alpha user's 40.4 KB result arrived that way, and its agent scripted over the saved file
19
+ # (22645b61, #1155). A character is at least a byte, so 28,000 B stays under 30,000 characters with room.
20
+ BYTES = 28_000
21
+
22
+ # The fields of a file's entry its outline gives, which a cut replaces.
23
+ OUTLINE_FIELDS = %w[description opening_line outline].freeze
24
+
25
+ # The lists naming the files cut: those whose outline's lines were cut, and those whose whole outline was.
26
+ NAMING_LISTS = %w[outlines_cut outlines_left_out].freeze
27
+
28
+ # Each outlined file's outline, by path.
29
+ #: (Hash[String, CandidateOutline]) -> void
30
+ def initialize(outlines)
31
+ @outlines = outlines
32
+ freeze
33
+ end
34
+
35
+ # The listing with its files' outlines cut, one cut of one file at a time in cut order, until what the block says
36
+ # it prints fits BYTES, each file whose items were cut named under outlines_cut. If every cut is made and it
37
+ # still does not fit, every file cut is named, and range_outlines_bytes gives what the outlines of the files a
38
+ # range changed print, when there are any: they are never left out, and which to drop is the agent's call (#1171).
39
+ #: (Hash[String, untyped]) { (Hash[String, untyped]) -> Integer } -> Hash[String, untyped]
40
+ def cut(listing, &printed_bytes)
41
+ shortest = cut_order(listing).reduce(listing) do |shorter, (path, cuts)|
42
+ return shorter if printed_bytes.call(shorter) <= BYTES
43
+
44
+ cut_file(shorter, path, cuts)
45
+ end
46
+ printed_bytes.call(shortest) <= BYTES ? shortest : past_budget(shortest)
47
+ end
48
+
49
+ private
50
+
51
+ # The listing every cut left past BYTES, naming what its range's outlines print, when it lists any.
52
+ #: (Hash[String, untyped]) -> Hash[String, untyped]
53
+ def past_budget(listing)
54
+ range = listing.fetch("files").select { |file| file["changed_in_range"] }
55
+ return listing if range.empty?
56
+
57
+ bytes = range.sum { |file| JSON.generate(file.slice(*OUTLINE_FIELDS)).bytesize }
58
+ bytes > BYTES ? listing.merge("range_outlines_bytes" => bytes) : listing
59
+ end
60
+
61
+ # Each cut that shortens a file, as its path and how many of CandidateOutline::CUTS it then has made. Every file
62
+ # but the root entry files and those marked changed_in_range comes first; then the changed files, which a survey
63
+ # of a change is about (#1171); last the entry files, since a host loads those in every session, so they keep
64
+ # theirs longest, in EntryFiles' order. Within each group, each cut is made across the files in CUTS' order:
65
+ # those with a description first, since the agent still reads what each is for, then the deepest path, then the
66
+ # most bytes the cut saves (#1155, review F5).
67
+ #: (Hash[String, untyped]) -> Array[[String, Integer]]
68
+ def cut_order(listing)
69
+ outlined = listing.fetch("files").select { |file| @outlines.key?(file.fetch("path")) }
70
+ outlined.group_by { |file| kept_rank(file) }.sort.flat_map do |rank, files|
71
+ (1..most_cuts(rank)).flat_map { |cuts| cuts_across(files, cuts) }
72
+ end
73
+ end
74
+
75
+ # How many of CUTS are made on a file of this rank: on a changed or entry file every one but leaving its outline
76
+ # out.
77
+ #: (Integer) -> Integer
78
+ def most_cuts(rank) = rank.zero? ? CandidateOutline::CUTS.length : CandidateOutline::ENTRY_FILE_CUTS
79
+
80
+ # The files the cuts'th cut shortens, in the order it is made across them.
81
+ #: (Array[Hash[String, untyped]], Integer) -> Array[[String, Integer]]
82
+ def cuts_across(files, cuts)
83
+ files.select { |file| saved(file.fetch("path"), cuts).positive? }
84
+ .sort_by { |file| cut_rank(file, cuts) }.map { |file| [file.fetch("path"), cuts] }
85
+ end
86
+
87
+ # Where a file comes among those one cut shortens: described or not, depth, bytes saved, then path.
88
+ #: (Hash[String, untyped], Integer) -> [Integer, Integer, Integer, String]
89
+ def cut_rank(file, cuts)
90
+ path = file.fetch("path")
91
+ [file.key?("description") ? 0 : 1, -path.count("/"), -saved(path, cuts), path]
92
+ end
93
+
94
+ # 0 for a file that is neither a root entry file nor changed in the range, 1 for a changed one, then each entry
95
+ # file in EntryFiles::PATHS reversed, so the first, CLAUDE.md, keeps its items longest.
96
+ #: (Hash[String, untyped]) -> Integer
97
+ def kept_rank(file)
98
+ index = EntryFiles::PATHS.index(file.fetch("path"))
99
+ return 1 + EntryFiles::PATHS.length - index if index
100
+
101
+ file["changed_in_range"] ? 1 : 0
102
+ end
103
+
104
+ # The bytes the cuts'th cut of path's outline saves over the cuts made before it.
105
+ #: (String, Integer) -> Integer
106
+ def saved(path, cuts)
107
+ outline = @outlines.fetch(path)
108
+ JSON.generate(outline.to_h(cuts: cuts - 1)).bytesize - JSON.generate(outline.to_h(cuts:)).bytesize
109
+ end
110
+
111
+ # The listing with path's outline cut to its first `cuts` cuts, and the file named as its cuts say.
112
+ #: (Hash[String, untyped], String, Integer) -> Hash[String, untyped]
113
+ def cut_file(listing, path, cuts)
114
+ outline = @outlines.fetch(path)
115
+ files = listing.fetch("files").map do |file|
116
+ file.fetch("path") == path ? file.except(*OUTLINE_FIELDS).merge(outline.to_h(cuts:)) : file
117
+ end
118
+ named(listing.merge("files" => files), path, outline, cuts)
119
+ end
120
+
121
+ # The listing naming path under outlines_cut once its outline's lines are cut, and under outlines_left_out instead
122
+ # once its whole outline is; a cut description says so in its own text. A list that names no path is left out.
123
+ #: (Hash[String, untyped], String, CandidateOutline, Integer) -> Hash[String, untyped]
124
+ def named(listing, path, outline, cuts)
125
+ named_in = naming_list(outline, cuts)
126
+ lists = NAMING_LISTS.to_h do |list|
127
+ [list, listing.fetch(list, []) - [path] + (list == named_in ? [path] : [])]
128
+ end
129
+ listing.except(*NAMING_LISTS).merge(lists.reject { |_, paths| paths.empty? })
130
+ end
131
+
132
+ # The one of NAMING_LISTS a file with these cuts made belongs in, the whole outline left out moving it from the
133
+ # first to the second; nil while only its description is cut.
134
+ #: (CandidateOutline, Integer) -> String?
135
+ def naming_list(outline, cuts)
136
+ return "outlines_left_out" if outline.left_out?(cuts)
137
+
138
+ "outlines_cut" if outline.outline_cut?(cuts)
139
+ end
140
+ end
141
+ end
142
+ end
@@ -0,0 +1,31 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "outline_section"
5
+
6
+ module Principles
7
+ # The domain logic, which loads and saves itself through data/. models/CLAUDE.md owns the boundary.
8
+ module Models
9
+ # One section of a candidate file's outline and the deeper sections under it, which a cut outline folds into it
10
+ # once its headings are cut too (#1155).
11
+ class OutlineGroup
12
+ #: (OutlineSection, Array[OutlineSection]) -> void
13
+ def initialize(section, deeper)
14
+ @section = section
15
+ @deeper = deeper
16
+ freeze
17
+ end
18
+
19
+ # The group as a cut outline prints it once its deeper headings are folded: the section's heading, then how many
20
+ # items it and they hold under how many headings, and the lines they span.
21
+ #: () -> Array[String]
22
+ def folded_lines
23
+ last = @deeper.last or return @section.cut_lines
24
+
25
+ items = @section.item_count + @deeper.sum(&:item_count)
26
+ headings = "#{@deeper.length} heading#{"s" unless @deeper.one?}"
27
+ [*@section.heading, "(#{items} more under #{headings}, lines #{@section.first_line}-#{last.last_line})"]
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,56 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ module Principles
5
+ # The domain logic, which loads and saves itself through data/. models/CLAUDE.md owns the boundary.
6
+ module Models
7
+ # One heading of a candidate file's outline and the top-level list items under it, with the lines of the file it
8
+ # spans, so an outline whose items are cut can say where to read them back (#1155).
9
+ class OutlineSection
10
+ # The most items printed under one heading; a longer list says how many more it holds.
11
+ MOST_ITEMS = 20
12
+ # A heading's marks, as many as its level.
13
+ MARKS = /\A#+/
14
+
15
+ # The heading as Markdown; nil for the items before a file's first heading.
16
+ #: String?
17
+ attr_reader :heading
18
+
19
+ # The file's lines the section spans, from its heading, counted from 1.
20
+ #: Integer
21
+ attr_reader :first_line
22
+
23
+ #: Integer
24
+ attr_reader :last_line
25
+
26
+ # The heading's level, its number of #s; 0 for the items before a file's first heading.
27
+ #: Integer
28
+ attr_reader :level
29
+
30
+ #: (heading: String?, items: Array[String], first_line: Integer, last_line: Integer) -> void
31
+ def initialize(heading:, items:, first_line:, last_line:)
32
+ @heading = heading
33
+ @level = heading.to_s[MARKS].to_s.length #: Integer
34
+ @items = items
35
+ @first_line = first_line
36
+ @last_line = last_line
37
+ freeze
38
+ end
39
+
40
+ # The section as the outline prints it: its heading, its items up to MOST_ITEMS, then how many more.
41
+ #: () -> Array[String]
42
+ def lines
43
+ more = @items.length - MOST_ITEMS
44
+ [*@heading, *@items.first(MOST_ITEMS), *("(#{more} more)" if more.positive?)]
45
+ end
46
+
47
+ # The section as a cut outline prints it: its heading, then, if it holds items, how many and the lines of the file
48
+ # that hold them, so the agent reads just that section back (#1155, review F2).
49
+ #: () -> Array[String]
50
+ def cut_lines = [*@heading, *("(#{@items.length} more, lines #{@first_line}-#{@last_line})" if @items.any?)]
51
+
52
+ #: () -> Integer
53
+ def item_count = @items.length
54
+ end
55
+ end
56
+ end
@@ -4,6 +4,7 @@
4
4
  require "json"
5
5
  require_relative "../error"
6
6
  require_relative "../data/schema_check"
7
+ require_relative "../data/map_files"
7
8
  require_relative "member"
8
9
  require_relative "principle_members"
9
10
  require_relative "activation"
@@ -20,6 +21,15 @@ module Principles
20
21
  #: String
21
22
  attr_reader :id
22
23
 
24
+ # A parsed map validates repeated member identities beside the member state it constructs.
25
+ #: (String, Hash[String, untyped], files: Data::MapFiles, ?commit: String?) -> Principle
26
+ def self.from_map(id, fields, files:, commit: nil)
27
+ map = new(id, fields)
28
+ where = commit ? "#{files.relative(map.id)} at commit #{commit[0, 12]}" : files.relative(map.id)
29
+ map.members.refuse_repeats(where: where, file: files.relative(map.id))
30
+ map
31
+ end
32
+
23
33
  #: (String, Hash[String, untyped]) -> void
24
34
  def initialize(id, fields)
25
35
  @id = id
@@ -37,17 +47,14 @@ module Principles
37
47
  @fields["description"]
38
48
  end
39
49
 
50
+ # Its identity and stated rule, for a listing that compares principles without their members.
51
+ #: () -> Hash[String, untyped]
52
+ def description_with_id = { "id" => @id, "description" => description }
53
+
40
54
  # Its members as Member values, which say each one's id, key, place and check.
41
55
  #: PrincipleMembers
42
56
  attr_reader :members
43
57
 
44
- # Each member in one of paths, by its id and path, as show-meta-principle-passage reports the members already in
45
- # an entry file, which the agent judges against the passage (D68).
46
- #: (Array[String]) -> Array[Hash[String, String]]
47
- def members_in(paths)
48
- @members.in_files(paths).map { |member| { "id" => member.id, "path" => member.path } }
49
- end
50
-
51
58
  # Whether this principle's audit keeps the release of principles it ran under, and its next full audit states
52
59
  # what changed since: only the meta-principle's, which judges whether the keeping that release ships still
53
60
  # works (D68).
@@ -0,0 +1,48 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "../error"
5
+ require_relative "captured_head"
6
+
7
+ module Principles
8
+ # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
9
+ module Models
10
+ # One committed principle file's exact bytes, or verified absence across its folder alternatives.
11
+ class PrincipleFileRevision
12
+ #: (String, String, paths: Array[String], captured: CapturedHead) -> PrincipleFileRevision
13
+ def self.load(id, commit, paths:, captured:) = new(id, commit, paths, captured)
14
+
15
+ #: String?
16
+ attr_reader :text
17
+
18
+ #: (String, String, Array[String], CapturedHead) -> void
19
+ def initialize(id, commit, paths, captured)
20
+ @id = id.dup.freeze #: String
21
+ @commit = commit.dup.freeze #: String
22
+ @paths = paths.map { |path| path.dup.freeze }.freeze #: Array[String]
23
+ @text = selected_text(captured)&.dup&.freeze #: String?
24
+ freeze
25
+ end
26
+
27
+ private
28
+
29
+ #: (CapturedHead) -> String?
30
+ def selected_text(captured)
31
+ entries = @paths.filter_map { |path| captured.raw_entry_at(@commit, path) }
32
+ raise unavailable if entries.length > 1
33
+
34
+ entry = entries.first or return nil
35
+ raise unavailable unless entry.fetch("type") == "blob" && %w[100644 100755].include?(entry.fetch("mode"))
36
+
37
+ entry.fetch("bytes")
38
+ end
39
+
40
+ #: () -> Error
41
+ def unavailable
42
+ Error.new("git_history_unavailable", "The committed pair of #{@id} cannot be selected at #{@commit}",
43
+ "Preserve all map and audit copies and inspect their exact committed entries.")
44
+ .including("principle_id" => @id, "commit" => @commit)
45
+ end
46
+ end
47
+ end
48
+ end
@@ -7,31 +7,47 @@ require_relative "member_history"
7
7
  require_relative "mapped_principles"
8
8
  require_relative "placement"
9
9
  require_relative "project_history"
10
+ require_relative "member_mapping_run"
11
+ require_relative "principle_mapping_history"
10
12
 
11
13
  module Principles
12
14
  # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
13
15
  module Models
14
- # One principle's members as the commits that changed its map held them, walking back from HEAD on its
15
- # first-parent line. Each member's run is the unbroken stretch of those commits whose map holds a member
16
- # with its key, so its oldest commit is the one that mapped the member where it is (#243). The walk stops
17
- # at a map it cannot read, so a run may start later than the true mapping; that can only hide an older change.
16
+ # One principle's member mappings, newest first: each key's run ends where a selected map no longer holds it.
17
+ # Supplied certified mapping history selects earlier IDs and refuses unprovable reads. The legacy direct-ID
18
+ # walk stops at unreadable maps, which can only hide an older change (#243).
18
19
  class PrincipleHistory
19
- #: (id: String, history: ProjectHistory, principles: MappedPrinciples) -> void
20
- def initialize(id:, history:, principles:)
20
+ #: (id: String, history: ProjectHistory, principles: MappedPrinciples, ?mapping: PrincipleMappingHistory?) -> void
21
+ def initialize(id:, history:, principles:, mapping: nil)
21
22
  @id = id
22
23
  @history = history
23
24
  @principles = principles
25
+ @mapping = mapping #: PrincipleMappingHistory?
24
26
  freeze
25
27
  end
26
28
 
29
+ # A request belongs to this current ID only while its map stayed present since its supplied base.
30
+ # Earlier-ID member history cannot extend a retired current-ID request's lifetime.
31
+ #: (String?) -> void
32
+ def confirm_current_id_since(base)
33
+ return unless base
34
+
35
+ paths = [@principles.file(@id), @principles.earlier_file(@id)]
36
+ return if @history.commits_touching_since(base, paths).all? do |commit|
37
+ paths.any? { |path| @history.entry_at?(commit, path) }
38
+ end
39
+
40
+ raise Error.new("stale_request", "#{@id}'s map was absent in committed history after this request began",
41
+ "Start the audit again and answer the new request: #{Error::EXECUTABLE} start-audit #{@id}")
42
+ end
43
+
27
44
  # Each key's run, newest first, as [commit, the member's fields at that commit]; a key HEAD's map does
28
45
  # not hold has an empty run. Each commit's map is read once for every key.
29
46
  #: (Array[String]) -> Hash[String, Array[[String, Hash[String, untyped]]]]
30
47
  def runs(keys)
31
48
  runs = keys.to_h { |key| [key, []] } #: Hash[String, Array[[String, Hash[String, untyped]]]]
32
49
  open = keys.dup
33
- @history.commits_touching(map_paths).each do |commit|
34
- sha = commit.fetch("sha")
50
+ mapping_commits.each do |sha|
35
51
  members = members_at(sha) or break
36
52
  # A key the map at sha does not hold ends its run.
37
53
  open.select! { |key| members.key?(key) && runs.fetch(key).push([sha, members.fetch(key)]) }
@@ -45,11 +61,8 @@ module Principles
45
61
  #: (Array[String]) -> Hash[String, Hash[String, String]]
46
62
  def mapped_baselines(keys)
47
63
  runs(keys).each_with_object({}) do |(key, run), found|
48
- run.reverse_each do |sha, fields|
49
- hash = @history.hashes_by_key_at(sha, [fields])[key] or next
50
- found[key] = { "commit" => sha, "sha256" => hash }
51
- break
52
- end
64
+ baseline = MemberMappingRun.new(key, run).baseline(@history)
65
+ found[key] = baseline if baseline
53
66
  end
54
67
  end
55
68
 
@@ -62,14 +75,19 @@ module Principles
62
75
  hashes = {} #: Hash[String, String?]
63
76
  hash_at = ->(revision) { hashes.fetch(revision) { hashes[revision] = hash_at(key, now_at, revision) } }
64
77
  commits = @history.commits_touching([*paths_of(member), *map_paths])
65
- MemberHistory.new(baseline: baseline, now: now, commits: commits, hash_at: hash_at).to_h
78
+ MemberHistory.new(baseline: baseline, now: now, commits: commits, head: @history.head || "HEAD",
79
+ hash_at: hash_at).to_h
66
80
  end
67
81
 
68
82
  # The map as commit holds it, such as the one an audit began from, refused as MappedPrinciples refuses its text;
69
83
  # a commit before the folder took its name now holds it under the earlier name (#536), and a commit that lacks
70
- # the map under both gives empty text, which is refused as unparseable.
84
+ # the map under both gives empty text, which is refused as unparseable in the legacy path. A supplied
85
+ # mapping history owns strict earlier-ID selection and its refusal outside the proven interval.
71
86
  #: (String) -> Principle
72
87
  def map_at(commit)
88
+ mapping = @mapping
89
+ return mapping.map_at_revision(commit, captured: @history.captured_head) if mapping
90
+
73
91
  text = map_paths.lazy.filter_map { |path| @history.text_at(commit, path) }.first
74
92
  @principles.parsed(@id, text.to_s, commit: commit)
75
93
  end
@@ -81,6 +99,8 @@ module Principles
81
99
  def startable_at_head?
82
100
  head = @history.head or return false
83
101
  map_at(head).auditable?
102
+ rescue PrinciplePublication::ProofError
103
+ raise
84
104
  rescue Error
85
105
  false
86
106
  end
@@ -122,9 +142,14 @@ module Principles
122
142
 
123
143
  private
124
144
 
125
- # The map's file now and under the folder's earlier name, so a walk of its history reaches back past the move.
126
145
  #: () -> Array[String]
127
- def map_paths = [@principles.file(@id), @principles.earlier_file(@id)]
146
+ def mapping_commits
147
+ @mapping&.revisions&.keys || @history.commits_touching(map_paths).map { |commit| commit.fetch("sha") }
148
+ end
149
+
150
+ # Map files used by member history: certified selected IDs, or the legacy current ID, in either folder.
151
+ #: () -> Array[String]
152
+ def map_paths = @mapping&.map_paths || [@principles.file(@id), @principles.earlier_file(@id)]
128
153
 
129
154
  # The member's placements at commit, as that commit's map placed it and as now_at does, once each.
130
155
  #: (String, String, Hash[String, untyped]) -> Array[Placement]
@@ -153,11 +178,13 @@ module Principles
153
178
  @history.hashes_by_key_at(revision, [fields])[key] || @history.hashes_by_key_at(revision, [now_at])[key]
154
179
  end
155
180
 
156
- # Each member the map at commit holds, by key, or nil when that commit lacks the map or
157
- # MappedPrinciples refuses it.
181
+ # Each member the map at commit holds, by key. Legacy unreadable maps give nil; certified proof failures
182
+ # remain failures rather than shortening a run or inventing an absent placement.
158
183
  #: (String) -> Hash[String, Hash[String, untyped]]?
159
184
  def members_at(commit)
160
185
  map_at(commit).members.to_h { |member| [member.key, member.to_h] }
186
+ rescue PrinciplePublication::ProofError
187
+ raise
161
188
  rescue Error
162
189
  nil
163
190
  end
@@ -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