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
@@ -3,8 +3,9 @@
3
3
 
4
4
  require "digest"
5
5
  require_relative "../error"
6
- require_relative "../prose_list"
7
6
  require_relative "locator"
7
+ require_relative "member_region"
8
+ require_relative "line_span"
8
9
  require_relative "write_up_opening"
9
10
 
10
11
  module Principles
@@ -27,10 +28,6 @@ module Principles
27
28
  PLACE_LOST = %w[section_not_found passage_not_found passage_end_not_found ambiguous_heading
28
29
  ambiguous_passage field_not_found].freeze #: Array[String]
29
30
 
30
- # How many openings of an ambiguous passage its refusal names by line, so a one-character opening does not
31
- # name every line of the file.
32
- OPENINGS_NAMED = 5 #: Integer
33
-
34
31
  # The file name endings read as Markdown, where a # line is a heading and not a comment.
35
32
  MARKDOWN = /\.(md|markdown|mdx)\z/i #: Regexp
36
33
 
@@ -60,10 +57,27 @@ module Principles
60
57
  first, last = region(lines, locator)
61
58
  selected = lines[first...last].to_a.join
62
59
  start_text = locator.starting_at
63
- first, last, selected = passage(lines, first, selected, start_text, locator.ending_at) if start_text
60
+ if start_text
61
+ selected_region = MemberRegion.new(self, span: LineSpan.new(first + 1, last))
62
+ first, last, selected = selected_region.passage(start_text, locator.ending_at)
63
+ end
64
64
  snapshot(locator, selected, first + 1, last)
65
65
  end
66
66
 
67
+ # Only a newly supplied add/move location needs an unambiguous ending; existing references keep lookup.
68
+ #: (Locator) -> Hash[String, untyped]
69
+ def locate_for_authoring(locator)
70
+ found = locate(locator)
71
+ opening = locator.starting_at
72
+ ending = locator.ending_at
73
+ if opening && ending
74
+ first, last = region(@text.lines, locator)
75
+ selected_region = MemberRegion.new(self, span: LineSpan.new(first + 1, last))
76
+ selected_region.check_authored_ending(opening, ending)
77
+ end
78
+ found
79
+ end
80
+
67
81
  # The lines of the region locator names, as the agent reads them (D33).
68
82
  #: (Locator) -> Array[String]
69
83
  def lines_of(locator)
@@ -107,71 +121,6 @@ module Principles
107
121
  def region(lines, locator)
108
122
  raise NotImplementedError, "#{self.class} names no region"
109
123
  end
110
-
111
- # Narrows a member to a passage of its section, or of the whole text. starting_at must occur exactly
112
- # once. With ending_at, the passage runs from the start of that match to the end of the first ending_at
113
- # after it; without, to the end of the line it starts on. Anchors, never line numbers, so the passage
114
- # stays found while text around and inside it changes. A lost anchor is refused, never widened to the
115
- # whole file. Returns the passage's first and last lines, counted from the start of the text, and the
116
- # passage.
117
- #: (Array[String], Integer, String, String, String?) -> [Integer, Integer, String]
118
- def passage(lines, first, selected, start_text, end_text)
119
- before = lines[0...first].to_a.join.length
120
- start = only_opening(selected, start_text, before)
121
- found = selected[start...passage_end(selected, start, start_text, end_text)].to_s
122
- line = @text[0, before + start].to_s.count("\n")
123
- [line, line + line_count(found), found]
124
- end
125
-
126
- # How many lines a passage spans, counting a last line with no line break.
127
- #: (String) -> Integer
128
- def line_count(found)
129
- found.count("\n") + (found.end_with?("\n") ? 0 : 1)
130
- end
131
-
132
- # Where start_text's one occurrence in text begins; text starts before characters into the file.
133
- #: (String, String, Integer) -> Integer
134
- def only_opening(text, start_text, before)
135
- starts = text.enum_for(:scan, start_text).filter_map { Regexp.last_match&.begin(0) }
136
- return starts.first.to_i if starts.length == 1
137
-
138
- starts.empty? ? opening_not_found(start_text) : ambiguous_opening(start_text, starts.map { before + it })
139
- end
140
-
141
- #: (String) -> bot
142
- def opening_not_found(start_text)
143
- raise Error.new("passage_not_found", "The passage starting #{start_text.inspect} is not in the file",
144
- "Quote the passage's opening text exactly.", @path)
145
- end
146
-
147
- # Names the line of each opening, for the agent to quote more of the one it means (dogfood D5).
148
- #: (String, Array[Integer]) -> bot
149
- def ambiguous_opening(start_text, offsets)
150
- raise Error.new("ambiguous_passage",
151
- "#{start_text.inspect} occurs #{offsets.length} times, at lines #{lines_named(offsets)}",
152
- "Quote more of the passage's start so it occurs once.", @path)
153
- end
154
-
155
- # The lines, as first_line counts them, of the first OPENINGS_NAMED offsets into the file, and how many more.
156
- #: (Array[Integer]) -> String
157
- def lines_named(offsets)
158
- named = offsets.first(OPENINGS_NAMED).map { (@text[0, it].to_s.count("\n") + 1).to_s }
159
- more = offsets.length - named.length
160
- more.zero? ? ProseList.new(named).to_s : "#{named.join(", ")} and #{more} more"
161
- end
162
-
163
- #: (String, Integer, String, String?) -> Integer
164
- def passage_end(text, start, start_text, end_text)
165
- return (text.index("\n", start) || (text.length - 1)) + 1 unless end_text
166
-
167
- found = text.index(end_text, start + start_text.length)
168
- unless found
169
- raise Error.new("passage_end_not_found", "No #{end_text.inspect} after #{start_text.inspect}",
170
- "Quote the passage's closing text exactly.", @path)
171
- end
172
-
173
- found + end_text.length
174
- end
175
124
  end
176
125
  end
177
126
  end
@@ -12,13 +12,15 @@ module Principles
12
12
  # The most commits listed for one member; the walk says when it stopped here.
13
13
  LIMIT = 20
14
14
 
15
- # commits are the commits that touched the member's file or its map, newest first; hash_at gives the
16
- # member's hash at a revision, or nil where it cannot be found there.
17
- #: (baseline: String, now: String, commits: Array[Hash[String, String]], hash_at: ^(String) -> String?) -> void
18
- def initialize(baseline:, now:, commits:, hash_at:)
15
+ # commits are the commits that touched the member's file or its map, newest first, up to head, the commit the
16
+ # command reads as HEAD; hash_at gives the member's hash at a revision, or nil where it cannot be found there.
17
+ #: (baseline: String, now: String, commits: Array[Hash[String, String]], head: String,
18
+ #| hash_at: ^(String) -> String?) -> void
19
+ def initialize(baseline:, now:, commits:, head:, hash_at:)
19
20
  @baseline = baseline
20
21
  @now = now
21
22
  @commits = commits
23
+ @head = head
22
24
  @hash_at = hash_at
23
25
  freeze
24
26
  end
@@ -29,7 +31,7 @@ module Principles
29
31
  def to_h
30
32
  listed = [] #: Array[Hash[String, String]]
31
33
  stopped = walk(listed)
32
- { "commits" => listed, "stopped_at" => stopped, "uncommitted" => @hash_at.call("HEAD") != @now }
34
+ { "commits" => listed, "stopped_at" => stopped, "uncommitted" => @hash_at.call(@head) != @now }
33
35
  end
34
36
 
35
37
  private
@@ -0,0 +1,49 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "member_loads"
5
+ require_relative "loading_scope"
6
+
7
+ module Principles
8
+ module Models
9
+ # One map's per-member loading scopes and the scope an added member newly represents.
10
+ class MemberLoadingScopes
11
+ #: (Hash[String, MemberLoads]) -> void
12
+ def initialize(loads)
13
+ @scopes = loads.transform_values { |member| LoadingScope.new(member.claude_code) }
14
+ .freeze #: Hash[String, LoadingScope]
15
+ freeze
16
+ end
17
+
18
+ # A new known scope absent from prior known ones, beside at least one differing known scope.
19
+ #: (String) -> Hash[String, untyped]
20
+ def introduced_by(id)
21
+ added = @scopes.fetch(id)
22
+ prior = @scopes.except(id)
23
+ known = prior.select { |_member_id, scope| scope.known? }
24
+ return {} unless added.known? && known.any? && known.values.none? { |scope| scope == added }
25
+
26
+ unknown = unclassified(prior)
27
+ { "loading_basis" => MemberLoads::BASIS.dup, "scope" => added.to_h, "already_mapped" => grouped(known),
28
+ **(unknown.empty? ? {} : { "not_compared" => unknown }) }
29
+ end
30
+
31
+ private
32
+
33
+ #: (Hash[String, LoadingScope]) -> Array[Hash[String, untyped]]
34
+ def grouped(known)
35
+ groups = known.group_by { |_id, scope| scope }.map do |scope, members|
36
+ { "member_ids" => members.map(&:first).sort, "scope" => scope.to_h }
37
+ end
38
+ groups.sort_by { |group| group.fetch("member_ids").first }
39
+ end
40
+
41
+ #: (Hash[String, LoadingScope]) -> Array[Hash[String, untyped]]
42
+ def unclassified(prior)
43
+ prior.reject { |_id, scope| scope.known? }.sort_by(&:first).map do |id, scope|
44
+ { "member_id" => id, "scope" => scope.to_h }
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -19,6 +19,10 @@ module Principles
19
19
  # audit-record.schema.json names each field so show-audit prints it; member_loads_spec holds a sample of each
20
20
  # branch to hold the two together, so add one there for a new branch.
21
21
  class MemberLoads
22
+ # The derivation shared by member and aggregate reports, never evidence of receipt in a session.
23
+ BASIS = { "host" => "claude_code", "kind" => "documented_prediction",
24
+ "session_start_assumption" => "project_root" }.freeze #: Hash[String, String]
25
+
22
26
  # The instruction files Claude Code loads at session start in the folder it starts in, and once a file is
23
27
  # read, written or edited in a folder below it, as its memory documentation says. This tool assumes a session
24
28
  # started at the project root.
@@ -0,0 +1,30 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require "json"
5
+ require_relative "project_history"
6
+
7
+ module Principles
8
+ # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
9
+ module Models
10
+ # One member key's ordered historical placements and its oldest locatable mapping baseline.
11
+ class MemberMappingRun
12
+ #: (String, Array[[String, Hash[String, untyped]]]) -> void
13
+ def initialize(key, entries)
14
+ @key = key.dup.freeze #: String
15
+ @entries = JSON.parse(JSON.generate(entries), freeze: true) #: Array[[String, Hash[String, untyped]]]
16
+ freeze
17
+ end
18
+
19
+ #: (ProjectHistory) -> Hash[String, String]?
20
+ def baseline(history)
21
+ @entries.reverse_each do |sha, fields|
22
+ hash = history.hashes_by_key_at(sha, [fields])[@key] or next
23
+
24
+ return { "commit" => sha, "sha256" => hash }
25
+ end
26
+ nil
27
+ end
28
+ end
29
+ end
30
+ end
@@ -24,6 +24,10 @@ module Principles
24
24
  @moves.empty?
25
25
  end
26
26
 
27
+ # Whether any id participates in either end of a declared move, including a move keeping its id.
28
+ #: (Array[String]) -> bool
29
+ def involving_any?(ids) = @moves.keys.intersect?(ids) || @moves.values.intersect?(ids)
30
+
27
31
  # Refuses moves outside git, where there is no base map to check them against.
28
32
  #: (String?, principle_id: String) -> String
29
33
  def check_in_git(base, principle_id:)
@@ -6,6 +6,7 @@ require_relative "acknowledgement_refusal"
6
6
  require_relative "audit_record"
7
7
  require_relative "principle_members"
8
8
  require_relative "principle_history"
9
+ require_relative "principle_mapping_history"
9
10
  require_relative "member_state"
10
11
  require_relative "locate_refusal"
11
12
 
@@ -22,21 +23,19 @@ module Principles
22
23
  class Baselines
23
24
  # The last audit's hashes for members' map, and each member's hash and commit at the commit that mapped it
24
25
  # where it is, read with the map as that commit held it; none outside git.
25
- #: (Project, PrincipleMembers) -> Baselines
26
- def self.of(project, members)
26
+ #: (Project, PrincipleMembers, ?mapping: PrincipleMappingHistory?) -> Baselines
27
+ def self.of(project, members, mapping: nil)
27
28
  records = project.audit_records
28
- mapped = mapped_where_it_is(project, members)
29
- new(audit: records.readable(members.principle_id), mapped: mapped.transform_values { it.fetch("sha256") },
30
- mapped_in: mapped.transform_values { it.fetch("commit") }, in_git: project.history.inside_git?,
31
- hashes: records.hashes_to_compare(members.principle_id))
29
+ mapped = mapped_where_it_is(project, members, mapping: mapping)
30
+ new(audit: records.readable(members.principle_id), mapped: mapped, in_git: project.history.inside_git?,
31
+ hashes: records.hashes_to_compare(members.principle_id), mapping: mapping)
32
32
  end
33
33
 
34
34
  # Each member's hash and commit at the commit that mapped it where it is, read with the map as that commit
35
35
  # held it.
36
- #: (Project, PrincipleMembers) -> Hash[String, Hash[String, String]]
37
- def self.mapped_where_it_is(project, members)
38
- PrincipleHistory.new(id: members.principle_id, history: project.history, principles: project.principles)
39
- .mapped_baselines(members.keys)
36
+ #: (Project, PrincipleMembers, ?mapping: PrincipleMappingHistory?) -> Hash[String, Hash[String, String]]
37
+ def self.mapped_where_it_is(project, members, mapping: nil)
38
+ project.principle_history(members.principle_id, mapping: mapping).mapped_baselines(members.keys)
40
39
  end
41
40
 
42
41
  # Whether the project is in git, so a member with no baseline is one no commit holds yet.
@@ -50,18 +49,26 @@ module Principles
50
49
  # audit is the principle's audit record, if any: its hashes, the commit it began from and whether it is
51
50
  # completed, so a one-member audit is offered only where one would be accepted (#626). hashes is the member
52
51
  # hashes the record holds, which a later copy may have written, so its members are compared read-only (#1066).
53
- #: (audit: AuditRecord?, mapped: Hash[String, String], mapped_in: Hash[String, String], in_git: bool,
54
- #| ?hashes: RecordedHashes) -> void
55
- def initialize(audit:, mapped:, mapped_in:, in_git:, hashes: audit&.members&.hashes || RecordedHashes::NONE)
52
+ #: (audit: AuditRecord?, mapped: Hash[String, Hash[String, String]], in_git: bool,
53
+ #| ?hashes: RecordedHashes, ?mapping: PrincipleMappingHistory?) -> void
54
+ def initialize(audit:, mapped:, in_git:, hashes: audit&.members&.hashes || RecordedHashes::NONE, mapping: nil)
56
55
  @hashes = hashes
57
56
  @audit = audit #: AuditRecord?
58
57
  @audit_base = audit&.base_commit #: String?
59
58
  @mapped = mapped
60
- @mapped_in = mapped_in
61
59
  @in_git = in_git
60
+ @mapping = mapping #: PrincipleMappingHistory?
62
61
  freeze
63
62
  end
64
63
 
64
+ # Only a mapping baseline supplies automatic history; a recorded hash needs its own provenance.
65
+ #: (String) -> PrincipleMappingHistory?
66
+ def mapping_for(key)
67
+ return if @hashes.hash_for(key)
68
+
69
+ @mapping if @mapped.key?(key)
70
+ end
71
+
65
72
  # The hash this tool last knew for the member known by key, and what it was: the last audit's, or else its
66
73
  # hash at the commit that mapped it, with why no audit gave one; neither when it has no baseline.
67
74
  #: (String) -> [String?, Hash[String, untyped]?]
@@ -69,8 +76,9 @@ module Principles
69
76
  if (audited = @hashes.hash_for(key))
70
77
  [audited, { "baseline" => "audit", "commit" => @audit_base }]
71
78
  elsif @mapped.key?(key)
72
- [@mapped[key], { "baseline" => "mapping_commit", "commit" => @mapped_in[key],
73
- "no_audit_because" => @hashes.empty? ? "no_audit_record" : "not_in_last_audit" }]
79
+ mapped = @mapped.fetch(key)
80
+ [mapped.fetch("sha256"), { "baseline" => "mapping_commit", "commit" => mapped.fetch("commit"),
81
+ "no_audit_because" => @hashes.empty? ? "no_audit_record" : "not_in_last_audit" }]
74
82
  else
75
83
  [nil, nil]
76
84
  end
@@ -103,8 +111,11 @@ module Principles
103
111
  # refused when it was asked for, and listed as broken among every map.
104
112
  #: (Project, String, ?listing_all: bool) -> MemberProblems
105
113
  def self.for(project, id, listing_all: false)
106
- members = project.principles.find(id).members
107
- new(members: members, now: project.current_by_key(members), baselines: Baselines.of(project, members))
114
+ map = project.principles.find(id)
115
+ members = map.members
116
+ mapping = PrincipleMappingHistory.load_if_linked(current: map, project: project)
117
+ new(members: members, now: project.current_by_key(members),
118
+ baselines: Baselines.of(project, members, mapping: mapping))
108
119
  rescue Error => e
109
120
  raise unless listing_all
110
121
 
@@ -152,15 +163,13 @@ module Principles
152
163
 
153
164
  # The entries whose own text changed since this tool last knew it, those an audit assessed first. Given a
154
165
  # block, each gains "history", what the block makes of its member, the hash it was compared with, and its
155
- # hash now.
156
- #: () ?{ (Member, String, String) -> Hash[String, untyped] } -> Array[Hash[String, untyped]]
166
+ # hash now and the selected automatic history when that mapping supplies its baseline.
167
+ #: () ?{ (Member, String, String, PrincipleMappingHistory?) -> Hash[String, untyped] }
168
+ #| -> Array[Hash[String, untyped]]
157
169
  def changed(&history)
158
170
  assessed, unassessed = changed_members.partition { |member| assessed?(member) }
159
171
  (assessed + unassessed).map do |member|
160
- listed = entry(member)
161
- next listed unless history
162
-
163
- listed.merge("history" => history.call(member, baseline(member.key).first.to_s, @now[member.key].to_s))
172
+ history ? history_entry(member, history) : entry(member)
164
173
  end
165
174
  end
166
175
 
@@ -201,6 +210,15 @@ module Principles
201
210
  found.map { |member| baseline(member.key).last&.fetch("baseline") || "no_baseline" }.tally.sort.to_h
202
211
  end
203
212
 
213
+ # Whether this readable map has no completed audit, independent of baseline readiness; a newer record is
214
+ # unknown to this copy, rather than evidence that no audit completed.
215
+ #: () -> bool
216
+ def no_completed_audit? = @unreadable.nil? && !@baselines.waiting? && @baselines.no_completed_audit?
217
+
218
+ # Whether this readable map has no members, even when an older full audit completed before it was emptied.
219
+ #: () -> bool
220
+ def empty_map? = @unreadable.nil? && @members.empty?
221
+
204
222
  # Whether the principle awaits its first full audit: none has completed and a member is found, in git one a
205
223
  # commit or an open audit gives a baseline, since until a commit holds its map committing gives its members
206
224
  # one (D58, #731 R7).
@@ -232,6 +250,12 @@ module Principles
232
250
 
233
251
  private
234
252
 
253
+ #: (Member, ^(Member, String, String, PrincipleMappingHistory?) -> Hash[String, untyped]) -> Hash[String, untyped]
254
+ def history_entry(member, history)
255
+ entry(member).merge("history" => history.call(member, baseline(member.key).first.to_s, @now[member.key].to_s,
256
+ @baselines.mapping_for(member.key)))
257
+ end
258
+
235
259
  # The members whose own text changed since this tool last knew it; none in a map that cannot be read.
236
260
  #: () -> Array[Member]
237
261
  def changed_members
@@ -0,0 +1,129 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "../error"
5
+ require_relative "../prose_list"
6
+ require_relative "member_file"
7
+ require_relative "line_span"
8
+
9
+ module Principles
10
+ # The domain logic, which loads and saves itself through data/. models/CLAUDE.md owns the boundary.
11
+ module Models
12
+ # A file-selected region's text and full-file character origin, for literal passage matching.
13
+ class MemberRegion
14
+ # How many ambiguous passage openings the refusal names, so one character cannot list the whole file.
15
+ OPENINGS_NAMED = 5 #: Integer
16
+ ENDINGS_NAMED = 5 #: Integer
17
+
18
+ #: (MemberFile, span: LineSpan) -> void
19
+ def initialize(file, span:)
20
+ @path = file.path.dup.freeze #: String
21
+ @text = file.text.dup.freeze #: String
22
+ region = captured_region(span)
23
+ @before = region[0] #: Integer
24
+ @selected = region[1] #: String
25
+ freeze
26
+ end
27
+
28
+ # Existing references use one opening and the first eligible ending, or the opening's whole line.
29
+ #: (String, String?) -> [Integer, Integer, String]
30
+ def passage(start_text, end_text)
31
+ start = only_opening(@selected, start_text, @before)
32
+ found = @selected[start...passage_end(@selected, start, start_text, end_text)].to_s
33
+ line = @text[0, @before + start].to_s.count("\n")
34
+ [line, line + line_count(found), found]
35
+ end
36
+
37
+ # The narrower constraint applied only before persisting a newly authored locator.
38
+ #: (String, String) -> void
39
+ def check_authored_ending(start_text, end_text)
40
+ start = only_opening(@selected, start_text, @before)
41
+ offsets = ending_offsets(start + start_text.length, end_text)
42
+ ambiguous_ending(end_text, offsets) if offsets.length > 1
43
+ end
44
+
45
+ private
46
+
47
+ # Every eligible literal start counts, including endings that overlap a previous ending.
48
+ #: (Integer, String) -> Array[Integer]
49
+ def ending_offsets(after, ending)
50
+ offsets = [] #: Array[Integer]
51
+ while (found = @selected.index(ending, after))
52
+ offsets << (@before + found)
53
+ after = found + 1
54
+ end
55
+ offsets
56
+ end
57
+
58
+ #: (String, Array[Integer]) -> bot
59
+ def ambiguous_ending(ending, offsets)
60
+ candidates = offsets.first(ENDINGS_NAMED).map { ending_candidate(it) }
61
+ raise Error.new("ambiguous_passage_end", "#{ending.inspect} has #{offsets.length} eligible endings",
62
+ "Give a more specific --ending-at closing quote that occurs once after the complete opening.",
63
+ @path).including("eligible_ending_count" => offsets.length, "ending_candidates" => candidates,
64
+ "ending_candidates_omitted" => offsets.length - candidates.length)
65
+ end
66
+
67
+ # Coordinates use characters, consistently with String#index and the complete-opening boundary.
68
+ #: (Integer) -> Hash[String, Integer]
69
+ def ending_candidate(offset)
70
+ prefix = @text[0, offset].to_s
71
+ { "line" => prefix.count("\n") + 1, "column" => offset - (prefix.rindex("\n") || -1) }
72
+ end
73
+
74
+ # Selected text and its character origin from the file-type's inclusive line span.
75
+ #: (LineSpan) -> [Integer, String]
76
+ def captured_region(span)
77
+ lines = @text.lines
78
+ first = span.first_line - 1
79
+ [lines[0...first].to_a.join.length, lines[first...span.last_line].to_a.join.freeze]
80
+ end
81
+
82
+ #: (String) -> Integer
83
+ def line_count(found)
84
+ found.count("\n") + (found.end_with?("\n") ? 0 : 1)
85
+ end
86
+
87
+ #: (String, String, Integer) -> Integer
88
+ def only_opening(text, start_text, before)
89
+ starts = text.enum_for(:scan, start_text).filter_map { Regexp.last_match&.begin(0) }
90
+ return starts.first.to_i if starts.length == 1
91
+
92
+ starts.empty? ? opening_not_found(start_text) : ambiguous_opening(start_text, starts.map { before + it })
93
+ end
94
+
95
+ #: (String) -> bot
96
+ def opening_not_found(start_text)
97
+ raise Error.new("passage_not_found", "The passage starting #{start_text.inspect} is not in the file",
98
+ "Quote the passage's opening text exactly.", @path)
99
+ end
100
+
101
+ #: (String, Array[Integer]) -> bot
102
+ def ambiguous_opening(start_text, offsets)
103
+ raise Error.new("ambiguous_passage",
104
+ "#{start_text.inspect} occurs #{offsets.length} times, at lines #{lines_named(offsets)}",
105
+ "Quote more of the passage's start so it occurs once.", @path)
106
+ end
107
+
108
+ #: (Array[Integer]) -> String
109
+ def lines_named(offsets)
110
+ named = offsets.first(OPENINGS_NAMED).map { (@text[0, it].to_s.count("\n") + 1).to_s }
111
+ more = offsets.length - named.length
112
+ more.zero? ? ProseList.new(named).to_s : "#{named.join(", ")} and #{more} more"
113
+ end
114
+
115
+ #: (String, Integer, String, String?) -> Integer
116
+ def passage_end(text, start, start_text, end_text)
117
+ return (text.index("\n", start) || (text.length - 1)) + 1 unless end_text
118
+
119
+ found = text.index(end_text, start + start_text.length)
120
+ unless found
121
+ raise Error.new("passage_end_not_found", "No #{end_text.inspect} after #{start_text.inspect}",
122
+ "Quote the passage's closing text exactly.", @path)
123
+ end
124
+
125
+ found + end_text.length
126
+ end
127
+ end
128
+ end
129
+ end
@@ -8,12 +8,15 @@ module Principles
8
8
  # fills in, which Views::MemberRepairs prints as move-member commands. A member whose file is gone moves to the
9
9
  # path the agent finds, keeping its own section, passage or query, so a rename keeps its audit (#268). A member
10
10
  # whose place in its file is gone moves within it, or into another file its text moved to (#378); a passage keeps
11
- # its anchors, so a move cannot silently widen it to its whole new section (#251), but not an opening text that is
12
- # gone, which a move repeating it would fail on again as printed (#1104); and it is given no section a passage
13
- # located by its opening alone never had (#1134). Which fits is the agent's to judge.
11
+ # its anchors, so a move cannot silently widen it to its whole new section (#251), but not an opening or ending text
12
+ # that is gone, which a move repeating it would fail on again as printed (#1104, #1139); and it is given no section
13
+ # a passage located by its opening alone never had (#1134). Which fits is the agent's to judge.
14
14
  class MemberRepairMoves
15
15
  # The locator fields a member may give beside its path.
16
16
  LOCATOR = %w[section starting_at ending_at query].freeze #: Array[String]
17
+ # The passage anchor each refusal code says is no longer in its file, which a move fills in afresh.
18
+ LOST_ANCHOR = { "passage_not_found" => %w[starting_at],
19
+ "passage_end_not_found" => %w[ending_at] }.freeze #: Hash[String, Array[String]]
17
20
 
18
21
  # member is the member's locator fields, as its map holds them; code is that of the refusal saying why it cannot
19
22
  # be found.
@@ -30,11 +33,10 @@ module Principles
30
33
  return [move(@member, %w[path])] if @code == "unreadable_file"
31
34
  return [move({}, %w[query])] if @member["query"]
32
35
 
33
- kept = opening_lost? ? %w[ending_at] : %w[starting_at ending_at]
34
- anchors = given(@member, kept)
35
- opening = opening_lost? ? %w[starting_at] : []
36
+ lost = LOST_ANCHOR.fetch(@code.to_s, [])
37
+ anchors = given(@member, %w[starting_at ending_at] - lost)
36
38
  section = @member.key?("section") ? %w[section] : []
37
- [move(anchors, [*opening, *section]), move(anchors, [*opening, "path", *section])]
39
+ [move(anchors, [*lost, *section]), move(anchors, [*lost, "path", *section])]
38
40
  end
39
41
 
40
42
  private
@@ -43,10 +45,6 @@ module Principles
43
45
  #: (Hash[String, untyped], Array[String]) -> Hash[String, untyped]
44
46
  def given(member, fields) = fields.filter_map { [it, member[it]] unless member[it].nil? }.to_h.freeze
45
47
 
46
- # Whether the passage's opening text is no longer in its file.
47
- #: () -> bool
48
- def opening_lost? = @code == "passage_not_found"
49
-
50
48
  #: (Hash[String, untyped], Array[String]) -> Hash[String, untyped]
51
49
  def move(keep, fill) = { "keep" => keep, "fill" => fill }
52
50
  end
@@ -8,8 +8,21 @@ module Principles
8
8
  # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
9
9
  module Models
10
10
  # Each of a map's members by id, where it is in its file now and the hash of what it says there, as
11
- # Project#snapshots located them all.
11
+ # the project located them all.
12
12
  class MemberSnapshots
13
+ # Locate every member before refusing, so one broken reference never hides another.
14
+ #: (Project, Principle) -> MemberSnapshots
15
+ def self.read(project, map)
16
+ broken = [] #: Array[Hash[String, untyped]]
17
+ found = map.members.to_h do |member|
18
+ [member.id, project.locate(member.locator)]
19
+ rescue Error => e
20
+ broken << { "id" => member.id }.merge(e.payload.except("status"))
21
+ [member.id, {}]
22
+ end
23
+ broken.empty? ? new(found) : project.refuse_broken(map.id, broken)
24
+ end
25
+
13
26
  #: (Hash[String, Hash[String, untyped]]) -> void
14
27
  def initialize(by_id)
15
28
  @by_id = by_id.freeze #: Hash[String, Hash[String, untyped]]
@@ -0,0 +1,56 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "../error"
5
+ require_relative "member"
6
+ require_relative "located_text"
7
+
8
+ module Principles
9
+ module Models
10
+ # Members in complete sections meeting the new section at a literal line boundary, in other unaudited maps.
11
+ class MembersInConsecutiveSections
12
+ #: (Project, Member) -> void
13
+ def initialize(project, member)
14
+ @project = project
15
+ @member = member
16
+ @located = project.located_text(member.locator) #: LocatedText
17
+ @records = project.audit_records #: AuditRecords
18
+ freeze
19
+ end
20
+
21
+ # Literal section boundaries are not evidence that the rules belong to one principle.
22
+ #: (except: String) -> Array[Hash[String, untyped]]
23
+ def elsewhere(except:)
24
+ return [] unless @member.locator.complete_section? && @records.known_no_completed_audit?(except)
25
+
26
+ maps = @project.principles
27
+ (maps.ids - [except]).flat_map { |id| in_map(maps, id) }
28
+ end
29
+
30
+ private
31
+
32
+ # Optional feedback leaves unreadable maps to list-member-problems rather than blocking the write.
33
+ #: (MappedPrinciples, String) -> Array[Hash[String, untyped]]
34
+ def in_map(maps, id)
35
+ return [] unless @records.known_no_completed_audit?(id)
36
+
37
+ maps.find(id).members.to_a.filter_map { |member| consecutive(member, id) }
38
+ rescue Error
39
+ []
40
+ end
41
+
42
+ #: (Member, String) -> Hash[String, untyped]?
43
+ def consecutive(member, id)
44
+ return unless member.locator.complete_section? && @project.same_file?(member.path, @located.path)
45
+
46
+ found = @project.located_text(member.locator)
47
+ return unless @located.span.consecutive_with?(found.span)
48
+
49
+ { "principle_id" => id, "member_id" => member.id, "path" => member.path,
50
+ "role" => member.role }.merge(found.lines)
51
+ rescue Error
52
+ nil
53
+ end
54
+ end
55
+ end
56
+ end