principles 0.0.5 → 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 (152) hide show
  1. checksums.yaml +4 -4
  2. data/lib/principles/after_commit_hook_options.rb +4 -1
  3. data/lib/principles/command_line.rb +12 -28
  4. data/lib/principles/command_routes.rb +7 -1
  5. data/lib/principles/controllers/acknowledgement.rb +36 -26
  6. data/lib/principles/controllers/answer.rb +7 -5
  7. data/lib/principles/controllers/assessment.rb +14 -3
  8. data/lib/principles/controllers/audit_completion.rb +3 -1
  9. data/lib/principles/controllers/command.rb +2 -2
  10. data/lib/principles/controllers/correction.rb +3 -1
  11. data/lib/principles/controllers/full_audit_request.rb +1 -1
  12. data/lib/principles/controllers/guidance_search_paths.rb +31 -0
  13. data/lib/principles/controllers/inventory.rb +3 -22
  14. data/lib/principles/controllers/mapping.rb +9 -1
  15. data/lib/principles/controllers/member_addition.rb +65 -0
  16. data/lib/principles/controllers/member_entries.rb +6 -24
  17. data/lib/principles/controllers/member_problems_report.rb +2 -2
  18. data/lib/principles/controllers/members_to_assess.rb +1 -1
  19. data/lib/principles/controllers/meta_principle_setup.rb +1 -1
  20. data/lib/principles/controllers/repoint.rb +5 -2
  21. data/lib/principles/controllers/survey.rb +60 -0
  22. data/lib/principles/data/audit_files.rb +4 -0
  23. data/lib/principles/data/candidate_files_found.rb +48 -13
  24. data/lib/principles/data/command-steps.json +77 -16
  25. data/lib/principles/data/committed_tree.rb +84 -0
  26. data/lib/principles/data/first_parent_log.rb +48 -0
  27. data/lib/principles/data/first_parent_range.rb +122 -0
  28. data/lib/principles/data/git_command.rb +37 -0
  29. data/lib/principles/data/guidance_search_paths_file.rb +107 -0
  30. data/lib/principles/data/map_files.rb +35 -10
  31. data/lib/principles/data/principles_directory.rb +40 -0
  32. data/lib/principles/data/principles_subfolder.rb +39 -16
  33. data/lib/principles/data/project_files.rb +11 -3
  34. data/lib/principles/data/repository.rb +55 -29
  35. data/lib/principles/data/requested_candidate_files.rb +43 -0
  36. data/lib/principles/data/running_version.rb +3 -5
  37. data/lib/principles/data/schemas/acknowledgement-answer.schema.json +4 -5
  38. data/lib/principles/data/schemas/answer-guide.schema.json +255 -63
  39. data/lib/principles/data/schemas/command-steps.schema.json +1 -1
  40. data/lib/principles/data/schemas/correction-answer-guide.schema.json +2 -1
  41. data/lib/principles/data/schemas/correction-answer.schema.json +6 -0
  42. data/lib/principles/data/schemas/guidance-search-paths.schema.json +13 -0
  43. data/lib/principles/data/schemas/member-map-history.schema.json +20 -0
  44. data/lib/principles/data/verified_head.rb +162 -0
  45. data/lib/principles/invocation_options.rb +2 -2
  46. data/lib/principles/models/acknowledgement.rb +28 -6
  47. data/lib/principles/models/acknowledgement_left_as_printed.rb +51 -0
  48. data/lib/principles/models/audit_entry_protecting_map.rb +28 -0
  49. data/lib/principles/models/audit_record.rb +4 -0
  50. data/lib/principles/models/audit_records.rb +34 -8
  51. data/lib/principles/models/candidate_files.rb +17 -6
  52. data/lib/principles/models/candidate_inventory.rb +51 -7
  53. data/lib/principles/models/captured_head.rb +72 -0
  54. data/lib/principles/models/check_since_last_audit.rb +4 -8
  55. data/lib/principles/models/declared_map_changes.rb +25 -9
  56. data/lib/principles/models/declared_member_removals.rb +75 -0
  57. data/lib/principles/models/disposition.rb +4 -2
  58. data/lib/principles/models/fix_only_fields.rb +12 -0
  59. data/lib/principles/models/guidance_search_path.rb +26 -0
  60. data/lib/principles/models/guidance_search_paths.rb +57 -0
  61. data/lib/principles/models/line_span.rb +6 -0
  62. data/lib/principles/models/loaded_files.rb +1 -2
  63. data/lib/principles/models/loading_scope.rb +61 -0
  64. data/lib/principles/models/locator.rb +6 -0
  65. data/lib/principles/models/mapped_principles.rb +39 -37
  66. data/lib/principles/models/member_file.rb +20 -71
  67. data/lib/principles/models/member_history.rb +7 -5
  68. data/lib/principles/models/member_loading_scopes.rb +49 -0
  69. data/lib/principles/models/member_loads.rb +4 -0
  70. data/lib/principles/models/member_mapping_run.rb +30 -0
  71. data/lib/principles/models/member_moves.rb +4 -0
  72. data/lib/principles/models/member_problems.rb +48 -24
  73. data/lib/principles/models/member_region.rb +129 -0
  74. data/lib/principles/models/member_repair_moves.rb +9 -11
  75. data/lib/principles/models/member_snapshots.rb +14 -1
  76. data/lib/principles/models/members_in_consecutive_sections.rb +56 -0
  77. data/lib/principles/models/meta_principle.rb +14 -0
  78. data/lib/principles/models/missing_files.rb +2 -2
  79. data/lib/principles/models/other_templates_placeholders.rb +1 -1
  80. data/lib/principles/models/outline_budget.rb +29 -14
  81. data/lib/principles/models/principle.rb +14 -7
  82. data/lib/principles/models/principle_file_revision.rb +48 -0
  83. data/lib/principles/models/principle_history.rb +46 -19
  84. data/lib/principles/models/principle_lineage.rb +118 -0
  85. data/lib/principles/models/principle_map_revision.rb +39 -0
  86. data/lib/principles/models/principle_mapping_history.rb +132 -0
  87. data/lib/principles/models/principle_members.rb +14 -0
  88. data/lib/principles/models/principle_predecessor.rb +45 -0
  89. data/lib/principles/models/principle_publication.rb +177 -0
  90. data/lib/principles/models/principle_revision.rb +104 -0
  91. data/lib/principles/models/problem_report.rb +17 -5
  92. data/lib/principles/models/project.rb +43 -27
  93. data/lib/principles/models/project_history.rb +45 -25
  94. data/lib/principles/models/record_format.rb +6 -0
  95. data/lib/principles/models/reported_check.rb +2 -1
  96. data/lib/principles/models/sent_answer.rb +10 -0
  97. data/lib/principles/models/survey_search_paths.rb +26 -0
  98. data/lib/principles/models/uncommitted_files.rb +50 -0
  99. data/lib/principles/models/unknown_principle.rb +29 -0
  100. data/lib/principles/positional_arguments.rb +37 -0
  101. data/lib/principles/version.rb +2 -2
  102. data/lib/principles/views/acknowledgement_result.rb +16 -12
  103. data/lib/principles/views/added_member.rb +41 -0
  104. data/lib/principles/views/answered_request.rb +21 -0
  105. data/lib/principles/views/assessment_request.rb +2 -1
  106. data/lib/principles/views/audit_result.rb +21 -26
  107. data/lib/principles/views/candidate_files_result.rb +56 -12
  108. data/lib/principles/views/candidate_outlines_result.rb +25 -0
  109. data/lib/principles/views/consecutive_sections_elsewhere.rb +27 -0
  110. data/lib/principles/views/correction_request.rb +32 -1
  111. data/lib/principles/views/guidance_search_paths_result.rb +26 -0
  112. data/lib/principles/views/json_with_entries_on_lines.rb +12 -4
  113. data/lib/principles/views/map_added_to.rb +56 -2
  114. data/lib/principles/views/map_result.rb +22 -42
  115. data/lib/principles/views/maps_needing_members_or_audit.rb +47 -0
  116. data/lib/principles/views/member_change_since_baseline.rb +2 -1
  117. data/lib/principles/views/member_repairs.rb +2 -1
  118. data/lib/principles/views/new_loading_scope.rb +31 -0
  119. data/lib/principles/views/one_member_check_result_template.rb +25 -21
  120. data/lib/principles/views/principle_read_result.rb +4 -1
  121. data/lib/principles/views/principles_with_no_completed_audit.rb +9 -3
  122. data/lib/principles/views/problem_lines.rb +3 -1
  123. data/lib/principles/views/problem_result.rb +8 -3
  124. data/lib/principles/views/prompts/acknowledge-a-change/run-the-check-and-report-it-passing.md +3 -3
  125. data/lib/principles/views/prompts/acknowledge-a-change/say-whether-the-change-keeps-the-meaning.md +6 -6
  126. data/lib/principles/views/prompts/assess-every-member/answer-guide.json +67 -43
  127. data/lib/principles/views/prompts/dispose-of-each-finding/answer-guide.json +1 -0
  128. data/lib/principles/views/prompts/dispose-of-each-finding/fix-or-record-each-finding.md +12 -0
  129. data/lib/principles/views/prompts/dispose-of-each-finding/retain-assessment-and-correction.md +4 -0
  130. data/lib/principles/views/prompts/find-a-principle-to-map/ask-about-other-guidance-search-paths.md +6 -0
  131. data/lib/principles/views/prompts/find-a-principle-to-map/choose-an-existing-or-new-principle.md +2 -0
  132. data/lib/principles/views/prompts/find-a-principle-to-map/choose-one-principle.md +4 -2
  133. data/lib/principles/views/prompts/find-a-principle-to-map/compare-with-mapped-principles.md +11 -0
  134. data/lib/principles/views/prompts/find-a-principle-to-map/read-left-out-files.md +2 -2
  135. data/lib/principles/views/prompts/find-a-principle-to-map/survey-a-later-range.md +3 -0
  136. data/lib/principles/views/prompts/find-a-principle-to-map/what-each-command-writes.md +4 -4
  137. data/lib/principles/views/prompts/find-a-principle-to-map/where-the-inventory-looks.md +5 -4
  138. data/lib/principles/views/prompts/learn-what-a-command-does/add-guidance-search-path.md +17 -0
  139. data/lib/principles/views/prompts/learn-what-a-command-does/add-member.md +35 -6
  140. data/lib/principles/views/prompts/learn-what-a-command-does/create-principle.md +16 -0
  141. data/lib/principles/views/prompts/learn-what-a-command-does/list-candidate-files.md +37 -4
  142. data/lib/principles/views/prompts/learn-what-a-command-does/list-member-problems.md +5 -4
  143. data/lib/principles/views/prompts/learn-what-a-command-does/move-member.md +5 -0
  144. data/lib/principles/views/prompts/learn-what-a-command-does/remove-guidance-search-path.md +13 -0
  145. data/lib/principles/views/prompts/learn-what-a-command-does/remove-principle.md +25 -0
  146. data/lib/principles/views/prompts/learn-what-a-command-does/show-answer-check.md +26 -0
  147. data/lib/principles/views/prompts/learn-what-a-command-does/show-candidate-outlines.md +21 -0
  148. data/lib/principles/views/prompts/learn-what-a-command-does/start-audit.md +2 -2
  149. data/lib/principles/views/prompts/learn-what-a-command-does/submit-answer.md +51 -54
  150. data/lib/principles/views/prompts/read-requested-outlines/continue-the-survey.md +7 -0
  151. metadata +55 -4
  152. data/lib/principles/models/candidate_listing.rb +0 -41
@@ -46,6 +46,20 @@ module Principles
46
46
  # writes the map with its first member (T74).
47
47
  #: () -> Hash[String, untyped]
48
48
  def self.starting_fields = { "description" => DESCRIPTION, "members" => [], "activation" => ACTIVATION.to_h }
49
+
50
+ # Whether this is the built-in principle whose first member setup maps.
51
+ #: (String) -> bool
52
+ def self.id?(id) = id == ID
53
+
54
+ # Setup's add-member writes this map with its first member, preserving its shipped description/activation.
55
+ #: (String) -> void
56
+ def self.refuse_explicit_creation(id)
57
+ return unless id?(id)
58
+
59
+ raise Error.new("meta_principle_id",
60
+ "#{id} is set up by show-meta-principle-passage, whose printed add-member creates its map " \
61
+ "with its first member", SET_UP)
62
+ end
49
63
  end
50
64
  end
51
65
  end
@@ -47,8 +47,8 @@ module Principles
47
47
  def refusal(message, rerun: nil, note: "")
48
48
  Error.new("broken_members", message,
49
49
  "For each member listed, say whether it moved or is gone, and run the matching command in its " \
50
- "repair_with: the move-member, with its NEW_PATH, HEADING, NEW_OPENING_TEXT or QUERY filled in, if " \
51
- "it moved, or the remove-member if it is gone. #{note}#{REPAIR}" \
50
+ "repair_with: the move-member, with its NEW_PATH, HEADING, NEW_OPENING_TEXT, NEW_ENDING_TEXT or " \
51
+ "QUERY filled in, if it moved, or the remove-member if it is gone. #{note}#{REPAIR}" \
52
52
  "#{rerun ? "\nThen run it again: #{rerun}" : " Then run the command again."}")
53
53
  .including(**({ "principle" => @principle } if @principle), "members" => members, "missing_files" => to_a)
54
54
  end
@@ -66,7 +66,7 @@ module Principles
66
66
  MemberAssessments::PLACEHOLDER, AssessmentAnswer::COLLECTIVE_PLACEHOLDER, RecentDecision::PLACEHOLDERS.to_h,
67
67
  ReportedCheck::PLACEHOLDERS.to_h, ReportedCheck::UNCHANGED_PLACEHOLDERS.to_h,
68
68
  LoadingDecision::PLACEHOLDERS.to_h, PriorDecisionFields::TEMPLATE_PLACEHOLDERS,
69
- Disposition::TEMPLATE_PLACEHOLDERS, Acknowledgement::PLACEHOLDERS.to_h
69
+ Disposition::TEMPLATE_PLACEHOLDERS, Acknowledgement::PLACEHOLDERS.to_h, Acknowledgement::EXIT_STATUS_PLACEHOLDER
70
70
  ].then { Strings.new(it).by_pointer.values.to_set }.freeze #: Set[String]
71
71
 
72
72
  # Each string in template, a request's template as printed, which its answer may send back.
@@ -34,32 +34,46 @@ module Principles
34
34
 
35
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
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.
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).
38
39
  #: (Hash[String, untyped]) { (Hash[String, untyped]) -> Integer } -> Hash[String, untyped]
39
40
  def cut(listing, &printed_bytes)
40
- cut_order(listing).reduce(listing) do |shorter, (path, cuts)|
41
+ shortest = cut_order(listing).reduce(listing) do |shorter, (path, cuts)|
41
42
  return shorter if printed_bytes.call(shorter) <= BYTES
42
43
 
43
44
  cut_file(shorter, path, cuts)
44
45
  end
46
+ printed_bytes.call(shortest) <= BYTES ? shortest : past_budget(shortest)
45
47
  end
46
48
 
47
49
  private
48
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
+
49
61
  # Each cut that shortens a file, as its path and how many of CandidateOutline::CUTS it then has made. Every file
50
- # but the root entry files comes first, since a host loads those in every session, so they keep theirs longest,
51
- # in EntryFiles' order. Within each group, each cut is made across the files in CUTS' order: those with a
52
- # description first, since the agent still reads what each is for, then the deepest path, then the most bytes
53
- # the cut saves (#1155, review F5).
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).
54
67
  #: (Hash[String, untyped]) -> Array[[String, Integer]]
55
68
  def cut_order(listing)
56
69
  outlined = listing.fetch("files").select { |file| @outlines.key?(file.fetch("path")) }
57
- outlined.group_by { |file| entry_rank(file.fetch("path")) }.sort.flat_map do |rank, files|
70
+ outlined.group_by { |file| kept_rank(file) }.sort.flat_map do |rank, files|
58
71
  (1..most_cuts(rank)).flat_map { |cuts| cuts_across(files, cuts) }
59
72
  end
60
73
  end
61
74
 
62
- # How many of CUTS are made on a file of this entry rank: on an entry file every one but leaving its outline out.
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.
63
77
  #: (Integer) -> Integer
64
78
  def most_cuts(rank) = rank.zero? ? CandidateOutline::CUTS.length : CandidateOutline::ENTRY_FILE_CUTS
65
79
 
@@ -77,13 +91,14 @@ module Principles
77
91
  [file.key?("description") ? 0 : 1, -path.count("/"), -saved(path, cuts), path]
78
92
  end
79
93
 
80
- # 0 for a file that is not a root entry file, then each entry file in EntryFiles::PATHS reversed, so the first,
81
- # CLAUDE.md, keeps its items longest.
82
- #: (String) -> Integer
83
- def entry_rank(path)
84
- index = EntryFiles::PATHS.index(path) or return 0
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
85
100
 
86
- EntryFiles::PATHS.length - index
101
+ file["changed_in_range"] ? 1 : 0
87
102
  end
88
103
 
89
104
  # The bytes the cuts'th cut of path's outline saves over the cuts made before it.
@@ -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
@@ -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 }