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,162 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "../error"
5
+ require_relative "git_command"
6
+ require_relative "first_parent_range"
7
+ require_relative "committed_tree"
8
+
9
+ module Principles
10
+ # Every file and process this tool reads or writes. data/CLAUDE.md owns the boundary.
11
+ module Data
12
+ # A permission-specific captured Git head, distinct from best-effort history display. Absence needs positive
13
+ # unborn evidence or inspectable physical ancestry without repository markers; failed access never grants it.
14
+ class VerifiedHead
15
+ OVERRIDES = %w[GIT_DIR GIT_WORK_TREE GIT_COMMON_DIR].freeze #: Array[String]
16
+ SHA = /\A\h{40}(?:\h{24})?\z/ #: Regexp
17
+
18
+ #: (String) -> void
19
+ def initialize(root)
20
+ @root = root
21
+ @git = GitCommand.new(root) #: GitCommand
22
+ @commit = nil #: String?
23
+ @read = false #: bool
24
+ end
25
+
26
+ #: () -> String?
27
+ def commit
28
+ return @commit if @read
29
+
30
+ @commit = capture&.freeze
31
+ @read = true
32
+ @commit
33
+ end
34
+
35
+ # A raw tree entry counts even when it is not readable JSON, a tree or a dangling link. Paths remain relative
36
+ # to the project, not the repository top; literal pathspecs come from GitCommand.
37
+ #: (String) -> bool
38
+ def entry?(path)
39
+ sha = commit or return false
40
+ entry_at?(sha, path)
41
+ end
42
+
43
+ #: (String, String) -> bool
44
+ def entry_at?(revision, path) = CommittedTree.new(@git, revision, unavailable).entry?(path)
45
+
46
+ #: (String, String) -> Hash[String, String]?
47
+ def raw_entry_at(revision, path) = CommittedTree.new(@git, revision, unavailable).raw_entry(path)
48
+
49
+ # This span proves only base to through; its upper's membership in captured HEAD needs a separate proof.
50
+ #: (String?, through: String) -> FirstParentRange
51
+ def first_parent_range(base, through:) = FirstParentRange.new(@git, base, through)
52
+
53
+ #: (String) -> String?
54
+ def first_parent_of(revision)
55
+ raise unavailable unless SHA.match?(revision)
56
+
57
+ output, status = @git.run_with_status(["cat-file", "commit", revision])
58
+ raise unavailable unless status&.zero?
59
+
60
+ parent_names(output, revision.length).first&.freeze
61
+ end
62
+
63
+ #: (String, Array[String]) -> Array[String]
64
+ def commits_touching_since(base, paths)
65
+ head = commit or raise unavailable
66
+ FirstParentRange.new(@git, base, head).touching(paths)
67
+ end
68
+
69
+ private
70
+
71
+ #: (String, Integer) -> Array[String]
72
+ def parent_names(output, name_length)
73
+ header, message = output.b.split("\n\n", 2)
74
+ rows = header.to_s.lines.map(&:chomp)
75
+ tree = rows.first.to_s
76
+ raise unavailable unless message && tree.start_with?("tree ")
77
+
78
+ parents = rows.grep(/\Aparent\b/).map { it.delete_prefix("parent ") }
79
+ confirm_object_names([tree.delete_prefix("tree "), *parents], name_length)
80
+
81
+ parents
82
+ end
83
+
84
+ #: (Array[String], Integer) -> void
85
+ def confirm_object_names(names, name_length)
86
+ raise unavailable unless names.all? { SHA.match?(it) && it.length == name_length }
87
+ end
88
+
89
+ #: () -> String?
90
+ def capture
91
+ output, status = @git.run_with_status(["rev-parse", "--is-inside-work-tree"])
92
+ return inside_commit if status&.zero? && output.strip == "true"
93
+ return nil if status == 128 && physically_outside?
94
+
95
+ raise unavailable
96
+ end
97
+
98
+ #: () -> String?
99
+ def inside_commit
100
+ output, status = @git.run_with_status(["rev-parse", "--verify", "--quiet", "HEAD^{commit}"])
101
+ return output.strip if status&.zero? && SHA.match?(output.strip)
102
+
103
+ raise unavailable unless status == 1
104
+
105
+ unborn_commit
106
+ end
107
+
108
+ # Only a symbolic branch whose ref is positively absent certifies an unborn work tree.
109
+ #: () -> nil
110
+ def unborn_commit
111
+ branch, symbolic = @git.run_with_status(["symbolic-ref", "--quiet", "HEAD"])
112
+ raise unavailable unless symbolic&.zero? && branch.start_with?("refs/heads/")
113
+
114
+ _, exists = @git.run_with_status(["show-ref", "--exists", branch.strip])
115
+ raise unavailable unless exists == 2
116
+
117
+ nil
118
+ end
119
+
120
+ # Git performs positive discovery. This only proves a failed discovery has no physical repository to hide;
121
+ # ambiguous metadata, explicit discovery overrides and filesystem errors are not evidence of absence.
122
+ #: () -> bool
123
+ def physically_outside?
124
+ return false if OVERRIDES.any? { |name| ENV[name] }
125
+
126
+ directory = File.realpath(@root)
127
+ loop do
128
+ return false if repository_marker?(directory)
129
+
130
+ parent = File.dirname(directory)
131
+ return true if parent == directory
132
+
133
+ directory = parent
134
+ end
135
+ rescue SystemCallError
136
+ false
137
+ end
138
+
139
+ #: (String) -> bool
140
+ def repository_marker?(directory)
141
+ return true if entry_exists?(File.join(directory, ".git"))
142
+
143
+ %w[HEAD objects refs].count { |name| entry_exists?(File.join(directory, name)) } >= 2
144
+ end
145
+
146
+ #: (String) -> bool
147
+ def entry_exists?(path)
148
+ File.lstat(path)
149
+ true
150
+ rescue Errno::ENOENT
151
+ false
152
+ end
153
+
154
+ #: () -> Error
155
+ def unavailable
156
+ Error.new("git_history_unavailable", "Git history could not be verified safely",
157
+ "Restore access to a healthy Git work tree and retry. Before the first commit, make an initial " \
158
+ "commit or use Git supporting show-ref --exists. No map was created or removed.")
159
+ end
160
+ end
161
+ end
162
+ end
@@ -11,9 +11,9 @@ module Principles
11
11
  VALUE_OPTIONS = %w[project file description path section starting-at ending-at query role
12
12
  check member session-start commits-after hook-event as instruction].freeze #: Array[String]
13
13
  # The options that take no value, as list-member-problems --short (#291).
14
- FLAG_OPTIONS = %w[short when-invoked always write-ups].freeze #: Array[String]
14
+ FLAG_OPTIONS = %w[short when-invoked always write-ups confirm].freeze #: Array[String]
15
15
  # The options that may be given more than once, each value kept in order, as change-activation --when-files.
16
- LIST_OPTIONS = %w[when-files].freeze #: Array[String]
16
+ LIST_OPTIONS = %w[when-files also].freeze #: Array[String]
17
17
  # The words that name an option taking the next word as its value.
18
18
  VALUE_WORDS = (VALUE_OPTIONS + LIST_OPTIONS).map { |name| "--#{name}" }.freeze #: Array[String]
19
19
  # Every word the parser reads as an option, which an option taking a value refuses as its value (#750).
@@ -2,6 +2,7 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require_relative "../error"
5
+ require_relative "acknowledgement_left_as_printed"
5
6
  require_relative "acknowledgement_refusal"
6
7
  require_relative "request_id"
7
8
  require_relative "audit_record"
@@ -22,15 +23,22 @@ module Principles
22
23
  class Acknowledgement
23
24
  PHASE = "acknowledgement"
24
25
  # The text the answer's template prints in each field the agent judges, which an answer may not send back, so
25
- # a template returned as printed acknowledges nothing (#773). Its quotes print first_changed_lines instead,
26
- # which the answer may send back as printed (#1111). After a pure insertion, meaning_kept's says that an
26
+ # a template returned as printed acknowledges nothing (#773). It prints no quotes, which an answer leaving them
27
+ # out reads as first_changed_lines (#1111, #1166, #1170). After a pure insertion, meaning_kept's says that an
27
28
  # inserted line usually adds a rule, for an agent that reads the template alone (#1142).
28
29
  PLACEHOLDERS = TemplatePlaceholders.new(
29
30
  { "meaning_kept" => "true only if the member still says the same thing",
30
31
  "reason" => "Why, in one line of your own" },
31
- also_printed: { "meaning_kept" => "true only if the member still says the same thing; old_line \"\" means a " \
32
- "line was inserted, which usually adds a rule" }
32
+ also_printed: { "meaning_kept" => "true only if the member still says the same thing; this change inserted " \
33
+ "a line, which usually adds a rule" }
33
34
  ) #: TemplatePlaceholders
35
+ # The text a checked member's template prints where its check's exit status goes, short since the request's
36
+ # context says how to run the check (#1170).
37
+ EXIT_STATUS_PLACEHOLDER = "the check's exit status, an integer" #: String
38
+ # The fields an answer may not send back as the template printed them, refused before its shape (#773, #1170).
39
+ LEFT_AS_PRINTED = AcknowledgementLeftAsPrinted.new(
40
+ PLACEHOLDERS, exit_status: EXIT_STATUS_PLACEHOLDER
41
+ ) #: AcknowledgementLeftAsPrinted
34
42
 
35
43
  # The placeholders the template prints beside quotes, its first_changed_lines.
36
44
  #: (Hash[String, String]) -> Hash[String, untyped]
@@ -94,6 +102,14 @@ module Principles
94
102
  record.members.acknowledgements_for(@member.key).last&.fetch("commit") || record.base_commit.to_s
95
103
  end
96
104
 
105
+ # Keep the record's named reference and stable member key together during placement lookup.
106
+ #: (Project) -> Enumerator::Lazy[Placement]
107
+ def placements_in(project)
108
+ commit = last_judged_commit
109
+ fields = project.principles.find(@principle_id).member(@member.id)
110
+ project.principle_history(@principle_id).placements_since(commit, @member.key, fields)
111
+ end
112
+
97
113
  # The request's id: the member, its hash at the baseline and now, and the commit it is judged at.
98
114
  #: () -> RequestId
99
115
  def request_id
@@ -135,7 +151,7 @@ module Principles
135
151
  record = acknowledgeable
136
152
  passed(ReportedCheck.new(AnswerPlace.new("", "The acknowledgement"), check,
137
153
  ran: record.check_run_for(@member.key), kept: record.check_review_for(@member.key))
138
- .result(answer["check_result"]))
154
+ .result(reported_check(answer["check_result"], check)))
139
155
  end
140
156
 
141
157
  # The first line on each side of the change that the other lacks, which the request hints the quotes from.
@@ -154,6 +170,10 @@ module Principles
154
170
  refuse("its check `#{result.fetch("command")}` exited #{status}, so a full audit judges it")
155
171
  end
156
172
 
173
+ # The check_result the answer reports, its command read as the request printed it when left out (#1166).
174
+ #: (untyped, String) -> untyped
175
+ def reported_check(reported, check) = reported.is_a?(Hash) ? { "command" => check, **reported } : reported
176
+
157
177
  # The change the answer quotes, between the member's lines at the baseline and now.
158
178
  #: (MemberLines) -> QuotedChange
159
179
  def quoted_change(lines) = QuotedChange.new(lines, member_id: @member.id)
@@ -179,7 +199,9 @@ module Principles
179
199
  refuse("the answer says the change does not keep #{@member.id}'s meaning") unless answer["meaning_kept"] == true
180
200
  reason = answer.fetch("reason").to_s
181
201
  refuse("the reason must be one line") if reason.include?("\n")
182
- quoted_change(lines).check(old_line: answer.fetch("old_line"), new_line: answer.fetch("new_line"))
202
+ change = quoted_change(lines)
203
+ quotes = change.first_changed_lines.merge(answer.slice("old_line", "new_line"))
204
+ change.check(old_line: quotes.fetch("old_line"), new_line: quotes.fetch("new_line"))
183
205
  end
184
206
 
185
207
  #: (String) -> bot
@@ -0,0 +1,51 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "left_as_printed"
5
+ require_relative "refusal_before_shape"
6
+ require_relative "template_placeholders"
7
+
8
+ module Principles
9
+ # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
10
+ module Models
11
+ # The fields a one-member audit's answer leaves as its template printed them, refused in one refusal before the
12
+ # answer's shape: its judgement's, meaning_kept and reason (#773), and a checked member's exit_status, which was
13
+ # otherwise refused as text with a repair offering the not_run_reason this template refuses (#1170). It holds the
14
+ # placeholders the template prints in each; Acknowledgement::LEFT_AS_PRINTED is the one a one-member audit uses.
15
+ class AcknowledgementLeftAsPrinted
16
+ include RefusalBeforeShape
17
+
18
+ # The field, named as a refusal names it, where a checked member's answer reports its exit status.
19
+ EXIT_STATUS = "check_result.exit_status" #: String
20
+
21
+ # judged holds the placeholders of the judgement's fields, exit_status the one printed where the status goes.
22
+ #: (TemplatePlaceholders, exit_status: String) -> void
23
+ def initialize(judged, exit_status:)
24
+ @judged = judged
25
+ @exit_status = exit_status
26
+ freeze
27
+ end
28
+
29
+ # @override
30
+ #: (untyped, String, Array[Hash[String, String]]) -> void
31
+ def refuse_in(answer, _source, shape_problems)
32
+ return unless answer.is_a?(Hash)
33
+
34
+ LeftAsPrinted.new(left_in(answer).to_h { [it, "/#{it.tr(".", "/")}"] }).refuse(besides: shape_problems)
35
+ end
36
+
37
+ private
38
+
39
+ # Each field answer leaves as printed, by name.
40
+ #: (Hash[String, untyped]) -> Array[String]
41
+ def left_in(answer)
42
+ judged = @judged.to_h.keys.select { @judged.left?(it, answer[it]) }
43
+ status_left?(answer["check_result"]) ? [*judged, EXIT_STATUS] : judged
44
+ end
45
+
46
+ # Whether check_result's exit_status is the placeholder the template prints there.
47
+ #: (untyped) -> bool
48
+ def status_left?(check_result) = check_result.is_a?(Hash) && check_result["exit_status"] == @exit_status
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,28 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "../error"
5
+
6
+ module Principles
7
+ module Models
8
+ # A current audit entry protects its map from unconfirmed removal, whether the entry is readable or not.
9
+ class AuditEntryProtectingMap
10
+ #: (String, String) -> void
11
+ def initialize(id, path)
12
+ @id = id.dup.freeze #: String
13
+ @path = path.dup.freeze #: String
14
+ freeze
15
+ end
16
+
17
+ #: () -> Error
18
+ def refusal
19
+ Error.new("principle_has_audit_record", "#{@id} has a current audit entry",
20
+ "Removing this map deactivates it in routine reports; retained findings remain unresolved. " \
21
+ "Keep it unless that is intended.\n" \
22
+ "To inspect a readable record: #{Error::EXECUTABLE} show-audit #{@id}\n" \
23
+ "To deactivate only the map, retaining its audit entry: " \
24
+ "#{Error::EXECUTABLE} remove-principle #{@id} --confirm", @path)
25
+ end
26
+ end
27
+ end
28
+ end
@@ -98,6 +98,10 @@ module Principles
98
98
  "map_changed" => @fields["map_version"] != map_version }
99
99
  end
100
100
 
101
+ # Completion certainty for optional mapping feedback; the record owns its format and kept history.
102
+ #: () -> bool
103
+ def known_no_completed_audit? = !RecordFormat.new(@fields).newer? && last_completed.nil?
104
+
101
105
  # Whether the audit this record describes completed, rather than being left open outside git.
102
106
  #: () -> bool
103
107
  def completed?
@@ -1,9 +1,12 @@
1
1
  # typed: strict
2
2
  # frozen_string_literal: true
3
3
 
4
+ require "json"
5
+
4
6
  require_relative "../error"
5
7
  require_relative "../data/audit_files"
6
8
  require_relative "audit_record"
9
+ require_relative "audit_entry_protecting_map"
7
10
  require_relative "member_hashes_as_written"
8
11
  require_relative "recorded_hashes"
9
12
  require_relative "project_history"
@@ -39,6 +42,27 @@ module Principles
39
42
  fields && AuditRecord.new(fields)
40
43
  end
41
44
 
45
+ # One exact historical selection, without a routed lookup or a format stamp.
46
+ #: (String, String) -> AuditRecord
47
+ def parsed_history(id, text)
48
+ fields = @files.parse(id, text) { refuse_unreadable(it, file(id)) }
49
+ refuse_unreadable(fields, file(id))
50
+ record = AuditRecord.new(JSON.parse(JSON.generate(fields), freeze: true))
51
+ return record if record.principle_id == id && record.completed?
52
+
53
+ raise Error.new("invalid_audit_record", "#{file(id)} is not a completed record of #{id}",
54
+ "Preserve this record and inspect its identity and status before changing an ID.", file(id))
55
+ end
56
+
57
+ # Optional mapping feedback: a missing record is known absence; unreadable records leave this unknown.
58
+ #: (String) -> bool
59
+ def known_no_completed_audit?(id)
60
+ audit = find(id) or return true
61
+ audit.known_no_completed_audit?
62
+ rescue Error
63
+ false
64
+ end
65
+
42
66
  # The findings id's record leaves open: none when it has no record, and none when its record cannot be read,
43
67
  # which list-member-problems reports, so a command reading other principles' findings beside its own is not
44
68
  # refused for theirs (#772).
@@ -61,8 +85,8 @@ module Principles
61
85
  # when it has none, is not newer, or is refused for another reason, which reading it reports (D85).
62
86
  #: (String) -> RecordFormat?
63
87
  def newer_format(id)
64
- fields = @files.read(id) { |raw| return newer_of(raw) }
65
- fields && newer_of(fields)
88
+ fields = @files.read(id) { |raw| return RecordFormat.newer_of(raw) }
89
+ fields && RecordFormat.newer_of(fields)
66
90
  rescue Error
67
91
  nil
68
92
  end
@@ -110,6 +134,14 @@ module Principles
110
134
  @files.exist?(id)
111
135
  end
112
136
 
137
+ #: (String, ?confirmed: bool) -> void
138
+ def refuse_map_removal(id, confirmed: false)
139
+ return unless @files.entry?(id)
140
+ return if confirmed
141
+
142
+ raise AuditEntryProtectingMap.new(id, file(id)).refusal
143
+ end
144
+
113
145
  # The record as a change to write, unless it says what the record on disk says, apart from when.
114
146
  #: (AuditRecord) -> Array[AuditRecord]
115
147
  def changes(record)
@@ -159,12 +191,6 @@ module Principles
159
191
  MemberHashesAsWritten.new(nil)
160
192
  end
161
193
 
162
- #: (untyped) -> RecordFormat?
163
- def newer_of(fields)
164
- format = RecordFormat.new(fields) if fields.is_a?(Hash)
165
- format if format&.newer?
166
- end
167
-
168
194
  # Refuses fields, a record at file the schema refused, as one a later copy wrote when its format says so, so
169
195
  # its repair is never to repair or restore a record that is not broken (D85, V3).
170
196
  #: (untyped, String) -> void
@@ -10,7 +10,8 @@ module Principles
10
10
  # skill's templates, references and examples once filled most of a 10KB inventory (alpha 011
11
11
  # friction 2). So Markdown files and harness settings are listed one by one, except inside a skill,
12
12
  # where only SKILL.md, the file the harness loads, is listed. Every other file is counted under its
13
- # directory. The agent still sees where every file is, and decides what to read.
13
+ # directory. Explicit selections are always named, including skill references and non-Markdown text files.
14
+ # The agent still sees where every file is, and decides what to read.
14
15
  class CandidateFiles
15
16
  # A file a harness reads as settings, listed by name like guidance.
16
17
  SETTINGS = %r{(?:\A|/)\.claude/settings[^/]*\.json\z}
@@ -19,23 +20,25 @@ module Principles
19
20
  IN_SKILL = %r{(?:\A|/)\.claude/skills/[^/]+/}
20
21
  SKILL_ENTRY = %r{(?:\A|/)\.claude/skills/[^/]+/SKILL\.md\z}
21
22
 
22
- #: (Array[Hash[String, untyped]], text_of: ^(String) -> String) -> void
23
- def initialize(files, text_of:)
23
+ #: (Array[Hash[String, untyped]], text_of: ^(String) -> String, ?selected: Array[String]) -> void
24
+ def initialize(files, text_of:, selected: [])
24
25
  @files = files #: Array[Hash[String, untyped]]
25
26
  @text_of = text_of
27
+ @selected = selected
26
28
  freeze
27
29
  end
28
30
 
29
31
  # The inventory's files, each Markdown one the survey reads with its outline, the other files counted by
30
32
  # directory, and those test-data hints that name a listed file, whose files the survey leaves out unread (#1153).
31
- #: (Array[Hash[String, untyped]]) -> Hash[String, untyped]
32
- def listing(test_data_hints)
33
+ # The files changed names, which a range of commits changed, list first, each marked changed_in_range (#1171).
34
+ #: (Array[Hash[String, untyped]], ?changed: Array[String]) -> Hash[String, untyped]
35
+ def listing(test_data_hints, changed: [])
33
36
  outlines = outlines(test_data_hints)
34
37
  files = listed.map do |file|
35
38
  outline = outlines[file.fetch("path")]
36
39
  outline ? file.merge(outline.to_h) : file
37
40
  end
38
- { "files" => files,
41
+ { "files" => changed_first(files, changed),
39
42
  "other_files_by_directory" => other_files_by_directory, "possibly_test_data" => hints(test_data_hints) }
40
43
  end
41
44
 
@@ -65,6 +68,13 @@ module Principles
65
68
 
66
69
  private
67
70
 
71
+ # files with those changed names first, each marked changed_in_range, and the rest after, each in its order.
72
+ #: (Array[Hash[String, untyped]], Array[String]) -> Array[Hash[String, untyped]]
73
+ def changed_first(files, changed)
74
+ in_range, rest = files.partition { |file| changed.include?(file.fetch("path")) }
75
+ in_range.map { |file| file.merge("changed_in_range" => true) } + rest
76
+ end
77
+
68
78
  # The test-data hints that name a listed file.
69
79
  #: (Array[Hash[String, untyped]]) -> Array[Hash[String, untyped]]
70
80
  def hints(test_data_hints)
@@ -80,6 +90,7 @@ module Principles
80
90
 
81
91
  #: (String) -> bool
82
92
  def named?(path)
93
+ return true if @selected.include?(path)
83
94
  return path.match?(SKILL_ENTRY) if path.match?(IN_SKILL)
84
95
 
85
96
  path.match?(MARKDOWN) || path.match?(SETTINGS)
@@ -1,14 +1,17 @@
1
1
  # typed: strict
2
2
  # frozen_string_literal: true
3
3
 
4
- require_relative "candidate_listing"
4
+ require_relative "../data/project_files"
5
+ require_relative "candidate_files"
6
+ require_relative "ignored_candidates"
5
7
  require_relative "outline_budget"
6
8
  require_relative "project_history"
9
+ require_relative "survey_search_paths"
7
10
 
8
11
  module Principles
9
12
  # The domain logic, which loads and saves itself through data/. models/CLAUDE.md owns the boundary.
10
13
  module Models
11
- # What list-candidate-files found: the inventory of candidate files as CandidateListing lists them, and in git
14
+ # What list-candidate-files found: the inventory of candidate files as it lists them, and in git
12
15
  # which of them, or of the files their links lead to, git does not track. Each list that names a file is
13
16
  # something the agent asks the project's owner about before mapping, and only those are asked, since asks for
14
17
  # empty lists hid the one that applied (#405).
@@ -20,17 +23,58 @@ module Principles
20
23
  # longer one would bury the ask, so the agent reads it from its list instead (#412).
21
24
  NAMED_INLINE_AT_MOST = 3
22
25
 
26
+ # What list-candidate-files finds in the project's files, each listed file's text read from them. After a commit,
27
+ # a full sha, the files its range changed come first, the Markdown ones outside the places hosts load listed too
28
+ # (#1171). The Markdown files not yet committed are named apart then, under uncommitted, since no commit in the
29
+ # range holds them.
30
+ #: (Data::ProjectFiles, ProjectHistory, search_paths: SurveySearchPaths, ?after: String?,
31
+ #| ?mapped: Array[String]) -> CandidateInventory
32
+ def self.read(files, history, search_paths:, after: nil, mapped: [])
33
+ changed = after ? history.changed_after(after) : []
34
+ inventory = files.inventory(also: changed.grep(CandidateFiles::MARKDOWN) | (changed & mapped),
35
+ matching: search_paths.globs).merge(search_paths.recorded_listing)
36
+ inventory = inventory.merge("uncommitted" => uncommitted_markdown(history)) if after
37
+ found(inventory, history, changed:) { |path| files.read_text(path) }
38
+ end
39
+
40
+ # An explicit file request uses the survey's entries and budget without its broad search.
41
+ #: (Data::ProjectFiles, ProjectHistory, paths: Array[String]) -> CandidateInventory
42
+ def self.requested(files, history, paths:)
43
+ found(files.requested_inventory(paths), history) { |path| files.read_text(path) }
44
+ end
45
+
46
+ # The Markdown files that differ from HEAD or are untracked, ignored ones aside.
47
+ #: (ProjectHistory) -> Array[String]
48
+ def self.uncommitted_markdown(history) = (history.uncommitted_outside([]) || []).grep(CandidateFiles::MARKDOWN)
49
+ private_class_method :uncommitted_markdown
50
+
23
51
  # What the project's inventory lists, and which listed files, or files their links lead to, history says
24
52
  # git does not track.
25
53
  # Each listed file's text, which its outline is read from, is what the block gives for its path.
26
- #: (Hash[String, untyped], ProjectHistory) { (String) -> String } -> CandidateInventory
27
- def self.found(inventory, history, &text_of)
54
+ # changed names the files a range of commits changed, which list first, marked (#1171).
55
+ #: (Hash[String, untyped], ProjectHistory, ?changed: Array[String]) { (String) -> String } -> CandidateInventory
56
+ def self.found(inventory, history, changed: [], &text_of)
28
57
  texts = {} #: Hash[String, String]
29
- listing = CandidateListing.new(inventory, text_of: ->(path) { texts[path] ||= text_of.call(path) })
30
- listed = listing.to_h
58
+ files = CandidateFiles.new(inventory.fetch("files"), selected: inventory.fetch("selected_files", []),
59
+ text_of: ->(path) { texts[path] ||= text_of.call(path) })
60
+ hints = inventory.fetch("possibly_test_data")
61
+ listed = listed(inventory, files.listing(hints, changed:))
31
62
  new(listed, untracked: history.untracked(listed.fetch("files").map { it.fetch("path") }),
32
- outlines: listing.outlines)
63
+ outlines: files.outlines(hints))
64
+ end
65
+
66
+ # The inventory as list-candidate-files lists it: the instruction files agent hosts load by convention, the other
67
+ # files counted by directory, the listed files that look like test data, and in git the ignored ones, unread, each
68
+ # folder git ignores counted once.
69
+ #: (Hash[String, untyped], Hash[String, untyped]) -> Hash[String, untyped]
70
+ def self.listed(inventory, files_listing)
71
+ listed = inventory.except("ignored", "ignored_ancestor_folders", "selected_files").merge(files_listing)
72
+ return listed unless inventory.key?("ignored")
73
+
74
+ listed.merge(IgnoredCandidates.new(paths: inventory.fetch("ignored"),
75
+ folders: inventory.fetch("ignored_ancestor_folders")).listing)
33
76
  end
77
+ private_class_method :listed
34
78
 
35
79
  # The listing, as list-candidate-files prints it.
36
80
  #: Hash[String, untyped]
@@ -0,0 +1,72 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "../data/repository"
5
+
6
+ module Principles
7
+ # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
8
+ module Models
9
+ # The commit first read by one command, including a captured absence. Later history reads share this baseline.
10
+ class CapturedHead
11
+ #: (Data::Repository) -> void
12
+ def initialize(repository)
13
+ @repository = repository
14
+ @commit = nil #: String?
15
+ @read = false #: bool
16
+ @verified = nil #: Data::VerifiedHead?
17
+ @ranges = {} #: Hash[[String?, String], Data::FirstParentRange]
18
+ end
19
+
20
+ #: () -> String?
21
+ def commit
22
+ return @commit if @read
23
+
24
+ @read = true
25
+ @commit = (@repository.head if @repository.inside?)
26
+ end
27
+
28
+ # Permission checks share the command's baseline, refusing disagreement with a prior best-effort read.
29
+ #: (String) -> bool
30
+ def entry?(path) = verified.entry?(path)
31
+
32
+ #: (String, String) -> bool
33
+ def entry_at?(revision, path) = verified.entry_at?(revision, path)
34
+
35
+ #: (String, String) -> Hash[String, String]?
36
+ def raw_entry_at(revision, path) = verified.raw_entry_at(revision, path)
37
+
38
+ # Cache constructed values by owned coordinates; each value certifies its span lazily when queried.
39
+ #: (String?, through: String) -> Data::FirstParentRange
40
+ def first_parent_range(base, through:)
41
+ captured = verified
42
+ lower = base&.dup&.freeze
43
+ upper = through.dup.freeze
44
+ key = [lower, upper].freeze
45
+ @ranges[key] ||= captured.first_parent_range(lower, through: upper)
46
+ end
47
+
48
+ #: (String) -> String?
49
+ def first_parent_of(revision) = verified.first_parent_of(revision)
50
+
51
+ #: (String, Array[String]) -> Array[String]
52
+ def commits_touching_since(base, paths) = verified.commits_touching_since(base, paths)
53
+
54
+ private
55
+
56
+ #: () -> Data::VerifiedHead
57
+ def verified
58
+ return @verified if @verified
59
+
60
+ captured = @repository.verified_head
61
+ revision = captured.commit
62
+ if @read && @commit != revision
63
+ raise Error.new("git_history_unavailable", "HEAD changed after this command captured its history",
64
+ "Stop concurrent Git writers and retry this command. No map was created or removed.")
65
+ end
66
+ @commit = revision
67
+ @read = true
68
+ @verified = captured
69
+ end
70
+ end
71
+ end
72
+ end