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
@@ -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
@@ -21,15 +21,17 @@ module Principles
21
21
  end
22
22
 
23
23
  # The changed members and the count of each state, refused when any member is broken. Given a block, each
24
- # changed member gains the history the block makes of its map id, member, baseline hash and hash now.
25
- #: () ?{ (String, Member, String, String) -> Hash[String, untyped] } -> Hash[String, untyped]
24
+ # changed member gains the history the block makes of its map id, member, baseline hash, hash now and
25
+ # the selected automatic history when its mapping supplies the baseline.
26
+ #: () ?{ (String, Member, String, String, PrincipleMappingHistory?) -> Hash[String, untyped] }
27
+ #| -> Hash[String, untyped]
26
28
  def checked(&)
27
29
  broken = @maps.flat_map(&:broken)
28
30
  refuse(broken) unless broken.empty?
29
31
 
30
32
  { "members" => changed(&),
31
33
  "counts" => @maps.flat_map(&:entries).map { |each| each.fetch("state") }.tally.sort.to_h,
32
- "compared_with" => compared_with, **no_baseline_because, **awaiting_first_audit_field }
34
+ "compared_with" => compared_with, **no_baseline_because, **awaiting_first_audit_field, **mapping_facts }
33
35
  end
34
36
 
35
37
  # The principles that await their first full audit, in map order, which list-member-problems names in one
@@ -72,12 +74,15 @@ module Principles
72
74
  @maps.to_h { |map| [map.principle_id, map.no_baseline_paths] }.reject { |_, paths| paths.empty? }
73
75
  end
74
76
 
75
- #: () ?{ (String, Member, String, String) -> Hash[String, untyped] } -> Array[Hash[String, untyped]]
77
+ #: () ?{ (String, Member, String, String, PrincipleMappingHistory?) -> Hash[String, untyped] }
78
+ #| -> Array[Hash[String, untyped]]
76
79
  def changed(&history)
77
80
  @maps.flat_map do |map|
78
81
  next map.changed unless history
79
82
 
80
- map.changed { |member, baseline, now| history.call(map.principle_id, member, baseline, now) }
83
+ map.changed do |member, baseline, now, mapping|
84
+ history.call(map.principle_id, member, baseline, now, mapping)
85
+ end
81
86
  end
82
87
  end
83
88
 
@@ -101,6 +106,13 @@ module Principles
101
106
  awaiting.empty? ? {} : { "awaiting_first_audit" => awaiting }
102
107
  end
103
108
 
109
+ # Completion and empty-map facts, distinct from whether members have a baseline ready for a first audit.
110
+ #: () -> Hash[String, Array[String]]
111
+ def mapping_facts
112
+ { "no_completed_audit" => @maps.select(&:no_completed_audit?).map(&:principle_id),
113
+ "empty_maps" => @maps.select(&:empty_map?).map(&:principle_id) }.reject { |_key, ids| ids.empty? }
114
+ end
115
+
104
116
  #: (Array[Hash[String, untyped]]) -> bot
105
117
  def refuse(broken)
106
118
  named = broken.map { |each| [each["principle"], each["id"]].compact.join(" ") }
@@ -3,7 +3,6 @@
3
3
 
4
4
  require_relative "../data/project_files"
5
5
  require_relative "../data/project_root"
6
- require_relative "../data/schema_check"
7
6
  require_relative "../data/map_files"
8
7
  require_relative "../data/audit_files"
9
8
  require_relative "../data/principles_subfolder"
@@ -23,15 +22,18 @@ require_relative "missing_files"
23
22
  require_relative "locate_refusal"
24
23
  require_relative "audit_records"
25
24
  require_relative "project_history"
25
+ require_relative "principle_history"
26
26
  require_relative "principles_folder_move"
27
27
  require_relative "sent_answer"
28
+ require_relative "guidance_search_paths"
29
+ require_relative "survey_search_paths"
28
30
 
29
31
  module Principles
30
32
  # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
31
33
  module Models
32
34
  # The user's project as this tool reads it: its instruction files, each member's text, the answers an
33
35
  # agent hands in, and the maps, audit records and git history kept beside them. Controllers reach the
34
- # project only through this model. It is handed its four data adapters, so a spec can hand it stand-ins
36
+ # project only through this model. It is handed its five data adapters, so a spec can hand it stand-ins
35
37
  # that meet the same contracts (#337).
36
38
  class Project
37
39
  # An implicit invocation reuses its nearest established project; an explicit root uses .at directly.
@@ -44,16 +46,20 @@ module Principles
44
46
  files = Data::ProjectFiles.new(root)
45
47
  new(files: files, maps: Data::MapFiles.new(Data::PrinciplesSubfolder.new(files.root, "maps")),
46
48
  audits: Data::AuditFiles.new(Data::PrinciplesSubfolder.new(files.root, "audits")),
47
- repository: Data::Repository.new(files.root))
49
+ repository: Data::Repository.new(files.root),
50
+ guidance_search_paths: Data::GuidanceSearchPathsFile.new(Data::PrinciplesDirectory.new(files.root)))
48
51
  end
49
52
 
50
53
  #: (files: Data::ProjectFiles, maps: Data::MapFiles, audits: Data::AuditFiles,
51
- #| repository: Data::Repository) -> void
52
- def initialize(files:, maps:, audits:, repository:)
54
+ #| repository: Data::Repository, guidance_search_paths: Data::GuidanceSearchPathsFile) -> void
55
+ def initialize(files:, maps:, audits:, repository:, guidance_search_paths:)
53
56
  @files = files
54
57
  @maps = maps
55
58
  @audits = audits
56
59
  @repository = repository
60
+ @guidance_search_paths = guidance_search_paths
61
+ @history = nil #: ProjectHistory?
62
+ @principle_histories = {} #: Hash[[String, PrincipleMappingHistory?], PrincipleHistory]
57
63
  end
58
64
 
59
65
  # The project's root, resolved.
@@ -65,7 +71,7 @@ module Principles
65
71
  #: () -> MappedPrinciples
66
72
  def principles
67
73
  refuse_unmoved_folder
68
- MappedPrinciples.new(@maps)
74
+ MappedPrinciples.new(@maps, history)
69
75
  end
70
76
 
71
77
  #: () -> AuditRecords
@@ -74,9 +80,24 @@ module Principles
74
80
  AuditRecords.new(@audits, history)
75
81
  end
76
82
 
83
+ #: () -> GuidanceSearchPaths
84
+ def guidance_search_paths
85
+ refuse_unmoved_folder
86
+ GuidanceSearchPaths.read(@guidance_search_paths)
87
+ end
88
+
89
+ # The project's history, one for the whole command, so every read names the HEAD it first read (#1161).
77
90
  #: () -> ProjectHistory
78
91
  def history
79
- ProjectHistory.new(@repository, @files)
92
+ @history ||= ProjectHistory.new(@repository, @files)
93
+ end
94
+
95
+ # Each principle's history view belongs to this command, sharing its captured project history.
96
+ #: (String, ?mapping: PrincipleMappingHistory?) -> PrincipleHistory
97
+ def principle_history(id, mapping: nil)
98
+ key = [id.dup.freeze, mapping].freeze #: [String, PrincipleMappingHistory?]
99
+ @principle_histories[key] ||= PrincipleHistory.new(id: key.first, history: history,
100
+ principles: principles, mapping: mapping)
80
101
  end
81
102
 
82
103
  # Whether two paths name one file, as their symbolic links lead them, so an edit through either is an edit
@@ -90,6 +111,10 @@ module Principles
90
111
  member_file(locator.path).locate(locator)
91
112
  end
92
113
 
114
+ # Check the location an add or move is about to persist, with the authoring-only ending constraint.
115
+ #: (Locator) -> Hash[String, untyped]
116
+ def locate_for_authoring(locator) = member_file(locator.path).locate_for_authoring(locator)
117
+
93
118
  # The text locator finds now, as one value: its path, its lines and the hash of what it says (#909).
94
119
  #: (Locator) -> LocatedText
95
120
  def located_text(locator) = LocatedText.of(locate(locator))
@@ -121,16 +146,7 @@ module Principles
121
146
  # Each of map's members by id, where it is in its file now and the hash of what it says there. It is
122
147
  # refused once, listing every member that cannot be found or read, so the agent repairs them together.
123
148
  #: (Principle) -> MemberSnapshots
124
- def snapshots(map)
125
- broken = [] #: Array[Hash[String, untyped]]
126
- found = map.members.to_h do |member|
127
- [member.id, locate(member.locator)]
128
- rescue Error => e
129
- broken << { "id" => member.id }.merge(e.payload.except("status"))
130
- [member.id, {}]
131
- end
132
- broken.empty? ? MemberSnapshots.new(found) : refuse_broken(map.id, broken)
133
- end
149
+ def snapshots(map) = MemberSnapshots.read(self, map)
134
150
 
135
151
  # The superseded, git-ignored file where an older version kept audit runs; this version never reads it.
136
152
  OLDER_STATE = ".memeplex/state.json" #: String
@@ -194,19 +210,19 @@ module Principles
194
210
  # An answer to a phase's request from file, where - means it was read from stdin, its head checked against
195
211
  # the phase's answer schema (D29) and the rest left to check once its request is known (#1052).
196
212
  #: (String, stdin: String?, phase: String) -> SentAnswer
197
- def answer(file, stdin:, phase:)
198
- parsed = if file == "-" then @files.parse_json(stdin.to_s, "-", named: SentAnswer::NAMED)
199
- else @files.read_json(file, named: SentAnswer::FILE_NAMED)
200
- end
201
- SentAnswer.new(parsed, file, Data::SchemaCheck.new("#{phase}-answer"))
202
- end
213
+ def answer(file, stdin:, phase:) = SentAnswer.read(@files, file, stdin:, phase:)
203
214
 
204
- # What list-candidate-files found: the candidate files as it lists them, and which git does not track.
205
- #: () -> CandidateInventory
206
- def candidate_files
207
- CandidateInventory.found(@files.inventory, history) { |path| @files.read_text(path) }
215
+ # What list-candidate-files found, as CandidateInventory reads it from the project, after a commit when given.
216
+ #: (?after: String?, ?also: Array[String]) -> CandidateInventory
217
+ def candidate_files(after: nil, also: [])
218
+ mapped = after ? principles.readable_paths : []
219
+ search_paths = SurveySearchPaths.new(recorded: guidance_search_paths.globs, also:)
220
+ CandidateInventory.read(@files, history, search_paths:, after:, mapped:)
208
221
  end
209
222
 
223
+ #: (Array[String]) -> CandidateInventory
224
+ def candidate_outlines(paths) = CandidateInventory.requested(@files, history, paths:)
225
+
210
226
  private
211
227
 
212
228
  # Refuses while the maps or records are still in the folder's earlier name (#536).
@@ -9,6 +9,8 @@ require_relative "principle_members"
9
9
  require_relative "member"
10
10
  require_relative "member_file"
11
11
  require_relative "commits_brought_in"
12
+ require_relative "uncommitted_files"
13
+ require_relative "captured_head"
12
14
 
13
15
  module Principles
14
16
  # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
@@ -28,6 +30,7 @@ module Principles
28
30
  def initialize(repository, files)
29
31
  @repository = repository
30
32
  @files = files
33
+ @head = CapturedHead.new(repository) #: CapturedHead
31
34
  end
32
35
 
33
36
  # Which of paths, and of the links and files they lead through, git does not track, which an audit refuses as
@@ -44,6 +47,25 @@ module Principles
44
47
  #: () -> bool
45
48
  def inside_git? = @repository.inside?
46
49
 
50
+ # The full sha of the commit revision names, which a survey of the commits after it starts from. Refuses outside
51
+ # git, where there are no commits, and a revision git resolves to no commit (#1171).
52
+ #: (String) -> String
53
+ def commit_named(revision)
54
+ unless @repository.inside?
55
+ raise Error.new("range_needs_git", "--commits-after needs git, to read the commits after #{revision}",
56
+ "To survey every candidate instead, run it without --commits-after.")
57
+ end
58
+
59
+ @repository.commit_of(revision) or
60
+ raise Error.new("unknown_revision", "git resolves #{revision} to no commit",
61
+ "Name a commit git knows, such as a sha from git log, a branch or HEAD~3.")
62
+ .including("revision" => revision)
63
+ end
64
+
65
+ # Each file the commits after commit, a full sha, changed up to HEAD that the project still holds (#1171).
66
+ #: (String) -> Array[String]
67
+ def changed_after(commit) = @repository.changed_since(commit).select { |path| @files.file?(path) }
68
+
47
69
  # The project's files, other than paths, that differ from HEAD or are untracked, ignored ones aside; nil
48
70
  # outside git or when there are none. A check and an answer's evidence read them as they are now (#409).
49
71
  #: (Array[String]) -> Array[String]?
@@ -58,17 +80,7 @@ module Principles
58
80
  # pointing at it, so a fresh clone would find nothing there (#760). Nothing outside git.
59
81
  #: (String) -> void
60
82
  def refuse_uncommitted_write_up(path)
61
- return unless @repository.inside?
62
-
63
- dirty = uncommitted([path])
64
- return if dirty.empty?
65
-
66
- linked = dirty - [path]
67
- raise Error.new("uncommitted_files",
68
- "The write-up at #{path} differs from the last commit" +
69
- (linked.empty? ? "" : ", through #{linked.join(", ")}"),
70
- "The record will point at the write-up as committed.\n" \
71
- "Commit the write-up, then submit the same answer again.")
83
+ UncommittedFiles.new(uncommitted([path])).refuse_write_up(path) if @repository.inside?
72
84
  end
73
85
 
74
86
  # The commit an audit of id is based on: HEAD, refused while any of files, or a file their links lead to,
@@ -77,15 +89,8 @@ module Principles
77
89
  def base_commit(id, files)
78
90
  return unless @repository.inside?
79
91
 
80
- dirty = uncommitted(files)
81
- unless dirty.empty?
82
- raise Error.new("uncommitted_files", "The map or members of #{id} differ from the last commit: " \
83
- "#{dirty.join(", ")}",
84
- "An audit assesses the map and members as committed, so its record can name the commit " \
85
- "it began from.\nCommit them, then start it again: #{Error::EXECUTABLE} start-audit #{id}")
86
- end
87
-
88
- @repository.head
92
+ UncommittedFiles.new(uncommitted(files)).refuse_audit(id)
93
+ head
89
94
  end
90
95
 
91
96
  # path's text at commit, or nil when commit does not hold it.
@@ -159,9 +164,14 @@ module Principles
159
164
  #: (Array[String]) -> Array[String]
160
165
  def existing(paths) = paths.select { |path| @files.file?(path) }
161
166
 
162
- # The commit at HEAD now; nil outside git.
167
+ # The commit HEAD named when this history first asked, kept for the rest of the command, so a commit landing
168
+ # while it runs leaves no read at one commit and the next at another (#1161); nil outside git. A history serves
169
+ # one command at most, so the next command sees the new commit.
163
170
  #: () -> String?
164
- def head = (@repository.head if @repository.inside?)
171
+ def head = @head.commit
172
+
173
+ #: () -> CapturedHead
174
+ def captured_head = @head
165
175
 
166
176
  # The commits after commit that changed path, a link on the way to it or the file they lead to, newest
167
177
  # first.
@@ -170,11 +180,11 @@ module Principles
170
180
  @repository.commits_since(commit, through_links([path]))
171
181
  end
172
182
 
173
- # Every commit on HEAD's first-parent line that changed any of paths, newest first; with since, only those
174
- # after it.
183
+ # Every commit on the first-parent line of head, the HEAD this history first named, that changed any of paths,
184
+ # newest first; with since, only those after it.
175
185
  #: (Array[String], ?since: String?) -> Array[Hash[String, String]]
176
186
  def commits_touching(paths, since: nil)
177
- @repository.commits_touching(through_links(paths), since: since)
187
+ @repository.commits_touching(through_links(paths), since: since, up_to: head || "HEAD")
178
188
  end
179
189
 
180
190
  # The commits after commit, up to HEAD, that one move of HEAD brought in, whatever they changed (D73).
@@ -191,6 +201,16 @@ module Principles
191
201
  !@repository.commits_since(commit, [*OWN_FOLDERS, *through_links(paths)]).empty?
192
202
  end
193
203
 
204
+ # Whether one raw entry belongs to the permission-specific captured head.
205
+ #: (String) -> bool
206
+ def entry_at_head?(path) = @head.entry?(path)
207
+
208
+ #: (String, Array[String]) -> Array[String]
209
+ def commits_touching_since(base, paths) = @head.commits_touching_since(base, paths)
210
+
211
+ #: (String, String) -> bool
212
+ def entry_at?(revision, path) = @head.entry_at?(revision, path)
213
+
194
214
  private
195
215
 
196
216
  # paths, each with the links on the way to it and the file they lead to, once each.
@@ -22,6 +22,12 @@ module Principles
22
22
  FIRST_RELEASE = "0.0.4"
23
23
  FIELDS = %w[format format_first_release].freeze
24
24
 
25
+ #: (untyped) -> RecordFormat?
26
+ def self.newer_of(fields)
27
+ format = new(fields) if fields.is_a?(Hash)
28
+ format if format&.newer?
29
+ end
30
+
25
31
  #: (Hash[String, untyped]) -> void
26
32
  def initialize(fields)
27
33
  @fields = fields