principles 0.0.11 → 0.1.1

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 (157) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +38 -0
  3. data/lib/principles/controllers/answer.rb +2 -2
  4. data/lib/principles/controllers/assessment.rb +19 -3
  5. data/lib/principles/controllers/audit.rb +14 -4
  6. data/lib/principles/controllers/full_audit_request.rb +16 -10
  7. data/lib/principles/controllers/guidance_search_paths.rb +1 -1
  8. data/lib/principles/controllers/member_addition.rb +4 -1
  9. data/lib/principles/controllers/member_problems_report.rb +7 -6
  10. data/lib/principles/controllers/open_finding_decisions.rb +66 -0
  11. data/lib/principles/controllers/running_tool.rb +22 -5
  12. data/lib/principles/controllers/survey.rb +3 -2
  13. data/lib/principles/data/candidate_files_found.rb +6 -4
  14. data/lib/principles/data/command-steps.json +17 -17
  15. data/lib/principles/data/committed_texts.rb +58 -0
  16. data/lib/principles/data/first_parent_log.rb +50 -10
  17. data/lib/principles/data/json_text.rb +5 -1
  18. data/lib/principles/data/parser_message.rb +61 -0
  19. data/lib/principles/data/project_files.rb +4 -2
  20. data/lib/principles/data/repository.rb +29 -37
  21. data/lib/principles/data/running_version.rb +1 -1
  22. data/lib/principles/data/schemas/audit-record.schema.json +15 -0
  23. data/lib/principles/data/schemas/command-steps.schema.json +1 -1
  24. data/lib/principles/data/schemas/{repointing-answer.schema.json → deciding-answer.schema.json} +12 -6
  25. data/lib/principles/data/working_tree_changes.rb +62 -0
  26. data/lib/principles/invocation_options.rb +1 -1
  27. data/lib/principles/launch.rb +5 -3
  28. data/lib/principles/models/after_commit_input.rb +1 -0
  29. data/lib/principles/models/assessment_basis.rb +1 -1
  30. data/lib/principles/models/audit.rb +23 -3
  31. data/lib/principles/models/audit_entry_protecting_map.rb +1 -0
  32. data/lib/principles/models/audit_record.rb +5 -14
  33. data/lib/principles/models/audit_records.rb +1 -1
  34. data/lib/principles/models/audited_member_lines.rb +1 -1
  35. data/lib/principles/models/candidate_files.rb +1 -1
  36. data/lib/principles/models/candidate_inventory.rb +19 -9
  37. data/lib/principles/models/candidate_outline.rb +1 -1
  38. data/lib/principles/models/changed_regions.rb +1 -1
  39. data/lib/principles/models/command_steps.rb +1 -2
  40. data/lib/principles/models/correction_basis.rb +1 -1
  41. data/lib/principles/models/declared_map_changes.rb +1 -2
  42. data/lib/principles/models/finding_decision.rb +84 -0
  43. data/lib/principles/models/finding_write_ups.rb +2 -1
  44. data/lib/principles/models/first_full_audit.rb +1 -0
  45. data/lib/principles/models/frontmatter_fields.rb +1 -1
  46. data/lib/principles/models/guidance_search_path.rb +1 -1
  47. data/lib/principles/models/guidance_search_paths.rb +5 -1
  48. data/lib/principles/models/head_moves.rb +1 -0
  49. data/lib/principles/models/ignored_candidates.rb +1 -1
  50. data/lib/principles/models/instruction_named.rb +1 -1
  51. data/lib/principles/models/json_path_query.rb +1 -1
  52. data/lib/principles/models/json_path_selectors.rb +1 -1
  53. data/lib/principles/models/json_path_string.rb +1 -1
  54. data/lib/principles/models/last_completed_revision.rb +1 -1
  55. data/lib/principles/models/line_diff.rb +1 -1
  56. data/lib/principles/models/line_span.rb +1 -1
  57. data/lib/principles/models/loading_scope.rb +1 -0
  58. data/lib/principles/models/located_text.rb +1 -1
  59. data/lib/principles/models/map_change.rb +66 -0
  60. data/lib/principles/models/mapped_principles.rb +4 -0
  61. data/lib/principles/models/markdown_file.rb +22 -1
  62. data/lib/principles/models/member.rb +1 -1
  63. data/lib/principles/models/member_elsewhere.rb +1 -1
  64. data/lib/principles/models/member_file.rb +6 -1
  65. data/lib/principles/models/member_hashes_as_written.rb +2 -2
  66. data/lib/principles/models/member_history.rb +1 -1
  67. data/lib/principles/models/member_lines.rb +1 -1
  68. data/lib/principles/models/member_loading_scopes.rb +1 -0
  69. data/lib/principles/models/member_mapping_run.rb +4 -4
  70. data/lib/principles/models/member_moves.rb +1 -2
  71. data/lib/principles/models/member_problems.rb +5 -1
  72. data/lib/principles/models/member_region.rb +1 -1
  73. data/lib/principles/models/members_elsewhere.rb +1 -1
  74. data/lib/principles/models/members_in_consecutive_sections.rb +1 -0
  75. data/lib/principles/models/members_sharing_lines.rb +1 -1
  76. data/lib/principles/models/misspelt_member_id.rb +1 -1
  77. data/lib/principles/models/open_finding.rb +4 -3
  78. data/lib/principles/models/open_findings_decision.rb +124 -0
  79. data/lib/principles/models/open_findings_elsewhere.rb +1 -1
  80. data/lib/principles/models/other_templates_placeholders.rb +1 -0
  81. data/lib/principles/models/outline_budget.rb +1 -1
  82. data/lib/principles/models/outline_group.rb +1 -1
  83. data/lib/principles/models/outline_section.rb +1 -1
  84. data/lib/principles/models/part_named.rb +1 -1
  85. data/lib/principles/models/plain_text_file.rb +1 -1
  86. data/lib/principles/models/principle_history.rb +3 -3
  87. data/lib/principles/models/principle_members.rb +1 -1
  88. data/lib/principles/models/problem_report.rb +26 -3
  89. data/lib/principles/models/project.rb +8 -3
  90. data/lib/principles/models/project_at_commit.rb +9 -19
  91. data/lib/principles/models/project_history.rb +8 -0
  92. data/lib/principles/models/prose_line.rb +36 -0
  93. data/lib/principles/models/record_format.rb +17 -4
  94. data/lib/principles/models/recorded_finding.rb +47 -7
  95. data/lib/principles/models/recorded_findings.rb +7 -7
  96. data/lib/principles/models/recorded_hashes.rb +1 -1
  97. data/lib/principles/models/recorded_members.rb +6 -8
  98. data/lib/principles/models/report_hooks.rb +1 -0
  99. data/lib/principles/models/request_id.rb +2 -2
  100. data/lib/principles/models/sent_answer.rb +1 -0
  101. data/lib/principles/models/session_start_input.rb +1 -0
  102. data/lib/principles/models/stale_request.rb +22 -8
  103. data/lib/principles/models/submitted_answer.rb +1 -1
  104. data/lib/principles/models/survey_search_paths.rb +13 -5
  105. data/lib/principles/models/text_within.rb +1 -1
  106. data/lib/principles/models/unchecked_members.rb +1 -0
  107. data/lib/principles/models/uncommitted_outside_the_map.rb +1 -1
  108. data/lib/principles/models/unknown_principle.rb +1 -0
  109. data/lib/principles/models/write_up_opening.rb +5 -31
  110. data/lib/principles/models/write_up_pointer.rb +2 -1
  111. data/lib/principles/start_audit_options.rb +25 -18
  112. data/lib/principles/version.rb +2 -2
  113. data/lib/principles/views/acknowledgement_result.rb +12 -9
  114. data/lib/principles/views/added_member.rb +10 -1
  115. data/lib/principles/views/answer_in_a_file.rb +16 -4
  116. data/lib/principles/views/assessment_answer_guide.rb +59 -0
  117. data/lib/principles/views/assessment_request.rb +2 -26
  118. data/lib/principles/views/audit_result.rb +15 -6
  119. data/lib/principles/views/candidate_files_result.rb +24 -13
  120. data/lib/principles/views/context_parts.rb +40 -9
  121. data/lib/principles/views/correction_request.rb +8 -5
  122. data/lib/principles/views/first_audit_brief.rb +2 -1
  123. data/lib/principles/views/guidance_search_paths_result.rb +13 -6
  124. data/lib/principles/views/invocation.rb +14 -4
  125. data/lib/principles/views/json_on_one_line.rb +18 -0
  126. data/lib/principles/views/open_findings_decision_result.rb +60 -0
  127. data/lib/principles/views/problem_facts.rb +46 -0
  128. data/lib/principles/views/problem_result.rb +42 -31
  129. data/lib/principles/views/prompts/answer-any-judgement-request/fill-the-answer-in-a-file.md +1 -1
  130. data/lib/principles/views/prompts/assess-every-member/read-the-request-in-parts.md +2 -1
  131. data/lib/principles/views/prompts/decide-open-findings/give-only-what-changed-of-each.md +6 -0
  132. data/lib/principles/views/prompts/dispose-of-each-finding/confirm-each-deferral-written-up-nowhere.md +2 -1
  133. data/lib/principles/views/prompts/dispose-of-each-finding/fix-or-record-each-finding.md +2 -2
  134. data/lib/principles/views/prompts/find-a-principle-to-map/ask-about-other-guidance-search-paths.md +1 -1
  135. data/lib/principles/views/prompts/find-a-principle-to-map/ask-in-this-order-after-the-table.md +1 -0
  136. data/lib/principles/views/prompts/find-a-principle-to-map/ask-the-owner.md +1 -0
  137. data/lib/principles/views/prompts/find-a-principle-to-map/ask-which-principle-to-map.md +2 -2
  138. data/lib/principles/views/prompts/find-a-principle-to-map/choose-an-existing-or-new-principle.md +3 -2
  139. data/lib/principles/views/prompts/find-a-principle-to-map/choose-one-principle.md +2 -2
  140. data/lib/principles/views/prompts/find-a-principle-to-map/compare-with-mapped-principles.md +2 -2
  141. data/lib/principles/views/prompts/find-a-principle-to-map/group-rules-into-principles.md +3 -0
  142. data/lib/principles/views/prompts/find-a-principle-to-map/where-a-survey-with-only-looks.md +4 -0
  143. data/lib/principles/views/prompts/learn-what-a-command-does/add-guidance-search-path.md +2 -1
  144. data/lib/principles/views/prompts/learn-what-a-command-does/add-member.md +1 -0
  145. data/lib/principles/views/prompts/learn-what-a-command-does/list-candidate-files.md +12 -12
  146. data/lib/principles/views/prompts/learn-what-a-command-does/list-open-findings.md +11 -12
  147. data/lib/principles/views/prompts/learn-what-a-command-does/show-answer-check.md +2 -2
  148. data/lib/principles/views/prompts/learn-what-a-command-does/show-open-correction.md +2 -2
  149. data/lib/principles/views/prompts/learn-what-a-command-does/start-audit.md +8 -8
  150. data/lib/principles/views/prompts/run-a-first-audit-in-the-background/follow-the-brief.md +1 -1
  151. metadata +20 -9
  152. data/lib/principles/controllers/repoint.rb +0 -67
  153. data/lib/principles/models/repoint.rb +0 -125
  154. data/lib/principles/models/write_up_repoint.rb +0 -38
  155. data/lib/principles/views/prompts/find-a-principle-to-map/ask-the-owner-after-the-table.md +0 -1
  156. data/lib/principles/views/prompts/repoint-write-ups/point-each-moved-write-up-at-its-new-place.md +0 -10
  157. data/lib/principles/views/repoint_result.rb +0 -80
@@ -6,8 +6,7 @@ require_relative "member"
6
6
  require_relative "principle"
7
7
 
8
8
  module Principles
9
- # The domain: maps, audits and command steps, with every rule a schema cannot express.
10
- # models/CLAUDE.md owns the boundary.
9
+ # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
11
10
  module Models
12
11
  # The members a correction says the agent moved after the audit began, from each old id to its new one. A
13
12
  # fix can rewrite the text a member's locator quotes; the agent then runs move-member and declares the
@@ -11,7 +11,7 @@ require_relative "member_state"
11
11
  require_relative "locate_refusal"
12
12
 
13
13
  module Principles
14
- # The domain logic, which loads and saves itself through data/. models/CLAUDE.md owns the boundary.
14
+ # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
15
15
  module Models
16
16
  # One map's members, each as broken, changed, unchanged, unchanged since its mapping commit or with no
17
17
  # baseline, against what this tool last knew of its text: the last audit's hash for it, or else its text at
@@ -242,6 +242,10 @@ module Principles
242
242
  @baselines.none_because
243
243
  end
244
244
 
245
+ # The map's members, whose files an audit of it judges as committed.
246
+ #: PrincipleMembers
247
+ attr_reader :members
248
+
245
249
  # Each file holding a member found with no baseline, once.
246
250
  #: () -> Array[String]
247
251
  def no_baseline_paths
@@ -7,7 +7,7 @@ require_relative "member_file"
7
7
  require_relative "line_span"
8
8
 
9
9
  module Principles
10
- # The domain logic, which loads and saves itself through data/. models/CLAUDE.md owns the boundary.
10
+ # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
11
11
  module Models
12
12
  # A file-selected region's text and full-file character origin, for literal passage matching.
13
13
  class MemberRegion
@@ -5,7 +5,7 @@ require_relative "member_elsewhere"
5
5
  require_relative "open_finding"
6
6
 
7
7
  module Principles
8
- # The domain logic, which loads and saves itself through data/. models/CLAUDE.md owns the boundary.
8
+ # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
9
9
  module Models
10
10
  # The members of other principles' maps sharing a line with one located member, as MembersSharingLines finds
11
11
  # them: what each result prints of them under mapped_elsewhere, and whether the agent should check the text
@@ -6,6 +6,7 @@ require_relative "member"
6
6
  require_relative "located_text"
7
7
 
8
8
  module Principles
9
+ # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
9
10
  module Models
10
11
  # Members in complete sections meeting the new section at a literal line boundary, in other unaudited maps.
11
12
  class MembersInConsecutiveSections
@@ -9,7 +9,7 @@ require_relative "member_elsewhere"
9
9
  require_relative "members_elsewhere"
10
10
 
11
11
  module Principles
12
- # The domain logic, which loads and saves itself through data/. models/CLAUDE.md owns the boundary.
12
+ # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
13
13
  module Models
14
14
  # The members whose text shares a line with one member located in its file, so one edit there shows on
15
15
  # each. The project locates the others; a member this tool cannot find now is left out, since show-principle
@@ -2,7 +2,7 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  module Principles
5
- # The domain logic, which loads and saves itself through data/. models/CLAUDE.md owns the boundary.
5
+ # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
6
6
  module Models
7
7
  # A member id an answer named that the map does not hold. A member id copies its heading exactly, so
8
8
  # an agent typing it may use a straight apostrophe for a curly one, a hyphen for a dash, or other
@@ -38,14 +38,15 @@ module Principles
38
38
  "audited_at" => @audited_at, "first_raised_at" => first_raised_at, **raised_in, **waiting, **kept_evidence }
39
39
  end
40
40
 
41
- # The finding as the next full audit's request lists it, with when it was first raised, and what it waits on,
42
- # since when, for the agent to confirm.
41
+ # The finding as the next full audit's request lists it, with when it was first raised, what it waits on,
42
+ # since when, for the agent to confirm, and each change made to it since its record.
43
43
  #: () -> Hash[String, untyped]
44
44
  def for_next_audit
45
45
  { "finding_id" => id, "member_ids" => @finding.member_ids, **problem,
46
46
  **present("recorded_at", recorded_at&.to_h),
47
47
  "action" => @finding.action,
48
- "first_raised_at" => first_raised_at, **raised_in, **waiting }
48
+ "first_raised_at" => first_raised_at, **raised_in, **waiting,
49
+ **present("changed_since_audit", @finding.changes_since_audit) }
49
50
  end
50
51
 
51
52
  # The finding as the next full audit's request lists it, against the map's members now and each one's hash
@@ -0,0 +1,124 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "../error"
5
+ require_relative "answer_fields"
6
+ require_relative "answer_place"
7
+ require_relative "audit_record"
8
+ require_relative "audit_records"
9
+ require_relative "finding_decision"
10
+ require_relative "last_completed_revision"
11
+ require_relative "project_history"
12
+ require_relative "request_id"
13
+
14
+ module Principles
15
+ # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
16
+ module Models
17
+ # A decision on a principle's open findings between full audits: the agent gives, for each finding it names,
18
+ # only what changes of its wait, action or write-up, and the last completed record keeps each change beside it,
19
+ # with the old value, when and why. Whether the change is right is the agent's judgement.
20
+ class OpenFindingsDecision
21
+ # The phase of start-audit --open-findings, which decides a finding's wait, action or write-up.
22
+ DECIDING = "deciding"
23
+
24
+ # record is the principle's audit record now; history says whether a new pointer's file is committed.
25
+ #: (principle_id: String, record: AuditRecord?, history: ProjectHistory) -> void
26
+ def initialize(principle_id:, record:, history:)
27
+ @principle_id = principle_id
28
+ @record = record
29
+ @history = history
30
+ freeze
31
+ end
32
+
33
+ # The request's id, digesting the last completed record's revision it was printed after, so an answer is
34
+ # matched to the record it read.
35
+ #: () -> RequestId
36
+ def request_id = RequestId.for(principle_id: @principle_id, phase: DECIDING, parts: [revision])
37
+
38
+ # The completed record's revision, which the request prints and the answer hands back.
39
+ #: () -> String
40
+ def revision = completed.revision
41
+
42
+ # The record with each decision an answer to start-audit --open-findings gives applied at at, once each names a
43
+ # different open finding and each new recorded_at is located by the block, in a committed file; a decision that
44
+ # changes no field leaves its finding as it is.
45
+ #: (untyped, at: String) { (Locator) -> void } -> AuditRecord
46
+ def decided(response, at:, &locate)
47
+ fields = AnswerFields.new(locate)
48
+ decisions = response.fetch("decisions")
49
+ each_open_once(decisions.map { |decision| decision.fetch("finding_id") })
50
+ completed.decided(decisions.each_with_index.map { |decision, index| decision_at(decision, index, fields) },
51
+ at:)
52
+ end
53
+
54
+ # The records decided changes among records, not yet written: refused once the record changed after the
55
+ # request, whose revision response quotes back in its basis, was printed, unless it changes none.
56
+ #: (AuditRecord, untyped, records: AuditRecords) -> Array[AuditRecord]
57
+ def confirmed_writable(decided, response, records:)
58
+ changes = records.changes(decided)
59
+ LastCompletedRevision.new(response.dig("basis", "revision")).confirm_writable(@principle_id, @record, changes)
60
+ changes
61
+ end
62
+
63
+ # The ids of the completed record's open findings that decided, the record accepted, changes.
64
+ #: (AuditRecord) -> Array[String]
65
+ def changed_ids(decided)
66
+ before = completed.left_open.to_h { |finding| [finding.id, finding.listed] }
67
+ decided.left_open.reject { |finding| before[finding.id] == finding.listed }.map(&:id)
68
+ end
69
+
70
+ # Refuses unless the principle's record is a completed audit's: an open record outside git keeps the completed
71
+ # one, whose findings its correction will decide, and with none there is nothing to decide.
72
+ #: () -> AuditRecord
73
+ def completed
74
+ record = @record
75
+ return record if record&.completed?
76
+
77
+ start = "#{Error::EXECUTABLE} start-audit #{@principle_id}"
78
+ if record
79
+ raise Error.new("cannot_decide_open_findings",
80
+ "#{@principle_id} has an audit left open, whose correction decides its findings",
81
+ "Answer that correction, or start the audit again: #{start}")
82
+ end
83
+
84
+ raise Error.new("cannot_decide_open_findings", "#{@principle_id} has no completed audit, so no finding is open",
85
+ "Run a full audit first: #{start}")
86
+ end
87
+
88
+ private
89
+
90
+ # Refuses ids, the findings an answer's decisions name, when one is not open in the completed record or two
91
+ # name one finding, since either would otherwise read as a success.
92
+ #: (Array[String]) -> void
93
+ def each_open_once(ids)
94
+ open = completed.open_ids
95
+ unknown = ids - open
96
+ unless unknown.empty?
97
+ raise Error.new("unknown_finding", "#{@principle_id}'s last completed audit leaves no finding " \
98
+ "#{unknown.join(", ")} open",
99
+ "Decide only its open findings: #{open.join(", ")}")
100
+ end
101
+ each_once(ids)
102
+ end
103
+
104
+ #: (Array[String]) -> void
105
+ def each_once(ids)
106
+ repeated = ids.tally.select { |_, count| count > 1 }.keys
107
+ return if repeated.empty?
108
+
109
+ raise Error.new("duplicate_finding", "Decisions name #{repeated.join(", ")} more than once",
110
+ "Give each finding one decision.")
111
+ end
112
+
113
+ # The decision at index, its new recorded_at, when it gives one, located and committed.
114
+ #: (Hash[String, untyped], Integer, AnswerFields) -> FindingDecision
115
+ def decision_at(decision, index, fields)
116
+ place = AnswerPlace.new("/decisions/#{index}", "Finding #{decision["finding_id"]}")
117
+ reference = decision["recorded_at"]
118
+ pointer = fields.write_up_pointer(reference, place.child("recorded_at")) if reference
119
+ @history.refuse_uncommitted_write_up(pointer.path) if pointer
120
+ FindingDecision.new(decision, place, principle_id: @principle_id, recorded_at: pointer)
121
+ end
122
+ end
123
+ end
124
+ end
@@ -6,7 +6,7 @@ require_relative "members_elsewhere"
6
6
  require_relative "open_finding"
7
7
 
8
8
  module Principles
9
- # The domain logic, which loads and saves itself through data/. models/CLAUDE.md owns the boundary.
9
+ # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
10
10
  module Models
11
11
  # The findings other principles' records leave open on members sharing lines with a full audit's members, read
12
12
  # once per principle: each member's mapped_elsewhere names them by id, and the request lists each once, since a
@@ -15,6 +15,7 @@ require_relative "reported_check"
15
15
  require_relative "shape_problems_beside"
16
16
 
17
17
  module Principles
18
+ # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
18
19
  module Models
19
20
  # The text in an answer that ends as this tool's placeholders end, "Leave as printed ...", "else leave" or "else
20
21
  # kept", or is a placeholder some template of this release prints, but is none the request's own template
@@ -6,7 +6,7 @@ require_relative "candidate_outline"
6
6
  require_relative "entry_files"
7
7
 
8
8
  module Principles
9
- # The domain logic, which loads and saves itself through data/. models/CLAUDE.md owns the boundary.
9
+ # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
10
10
  module Models
11
11
  # How much list-candidate-files may print: its listed files' outlines, cut a whole file's list items at a time
12
12
  # until the result fits. One user's run printed 66,145 B for 45 files, against a ceiling of about
@@ -4,7 +4,7 @@
4
4
  require_relative "outline_section"
5
5
 
6
6
  module Principles
7
- # The domain logic, which loads and saves itself through data/. models/CLAUDE.md owns the boundary.
7
+ # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
8
8
  module Models
9
9
  # One section of a candidate file's outline and the deeper sections under it, which a cut outline folds into it
10
10
  # once its headings are cut too.
@@ -2,7 +2,7 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  module Principles
5
- # The domain logic, which loads and saves itself through data/. models/CLAUDE.md owns the boundary.
5
+ # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
6
6
  module Models
7
7
  # One heading of a candidate file's outline and the top-level list items under it, with the lines of the file it
8
8
  # spans, so an outline whose items are cut can say where to read them back.
@@ -5,7 +5,7 @@ require_relative "../error"
5
5
  require_relative "../prose_list"
6
6
 
7
7
  module Principles
8
- # The domain of principles and their maps: each model owns its rules and loads and saves itself through data/.
8
+ # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
9
9
  module Models
10
10
  # The part start-audit --part names, printed alone from the request a full audit asks, so an agent that needs one
11
11
  # part of it need not read the whole request or script it. Only a part the request has, or leaves
@@ -4,7 +4,7 @@
4
4
  require_relative "member_file"
5
5
 
6
6
  module Principles
7
- # The domain logic, which loads and saves itself through data/. models/CLAUDE.md owns the boundary.
7
+ # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
8
8
  module Models
9
9
  # A file that is not Markdown, such as a script or a config file. It has no sections, so a member is the
10
10
  # whole file or a passage of it, and a # line is never read as a heading.
@@ -61,7 +61,7 @@ module Principles
61
61
  #: (Array[String]) -> Hash[String, Hash[String, String]]
62
62
  def mapped_baselines(keys)
63
63
  runs(keys).each_with_object({}) do |(key, run), found|
64
- baseline = MemberMappingRun.new(key, run).baseline(@history)
64
+ baseline = MemberMappingRun.new(run).baseline(@history)
65
65
  found[key] = baseline if baseline
66
66
  end
67
67
  end
@@ -157,7 +157,7 @@ module Principles
157
157
  fields_at = [members_at(commit)&.fetch(key, nil), now_at].compact
158
158
  fields_at.uniq { |fields| Member.new(fields).reference }.map do |fields|
159
159
  Placement.new(commit: commit, reference: Member.new(fields).reference,
160
- sha256: @history.at(commit).hashes_by_key([fields]).values.first)
160
+ sha256: @history.at(commit).hash_of(Member.new(fields)))
161
161
  end
162
162
  end
163
163
 
@@ -176,7 +176,7 @@ module Principles
176
176
  def hash_at(key, now_at, revision)
177
177
  fields = members_at(revision)&.fetch(key, nil) or return nil
178
178
  then_project = @history.at(revision)
179
- then_project.hashes_by_key([fields])[key] || then_project.hashes_by_key([now_at])[key]
179
+ then_project.hash_of(Member.new(fields)) || then_project.hash_of(Member.new(now_at))
180
180
  end
181
181
 
182
182
  # Each member the map at commit holds, by key. Legacy unreadable maps give nil; certified proof failures
@@ -6,7 +6,7 @@ require_relative "member"
6
6
  require_relative "misspelt_member_id"
7
7
 
8
8
  module Principles
9
- # The domain logic, which loads and saves itself through data/. models/CLAUDE.md owns the boundary.
9
+ # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
10
10
  module Models
11
11
  # One principle's members, as Members: finding one by its id, and the rules on ids and keys among them,
12
12
  # which Principle asks as it changes the map. It is Enumerable over its members, in the map's order.
@@ -6,22 +6,33 @@ require_relative "member_problems"
6
6
  require_relative "missing_files"
7
7
 
8
8
  module Principles
9
- # The domain logic, which loads and saves itself through data/. models/CLAUDE.md owns the boundary.
9
+ # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
10
10
  module Models
11
11
  # What list-member-problems reports across the maps it reads: refused while any member's reference is
12
12
  # broken, so a gate can stop on its exit status alone, and otherwise every changed member and a count of
13
13
  # each state.
14
14
  class ProblemReport
15
15
  # What list-member-problems reports for map id, or every map, listing a map it cannot read among them, with the
16
- # project's history, which lets each missing file carry the rename git detects for it.
16
+ # project's history, which lets each missing file carry the rename git detects for it. Every map's history is
17
+ # read in one walk of git's, not one each.
17
18
  #: (Project, String?) -> ProblemReport
18
19
  def self.for(project, id)
19
20
  maps = if id then [MemberProblems.for(project, id)]
20
- else project.principles.ids.map { MemberProblems.for(project, it, listing_all: true) }
21
+ else every_map(project)
21
22
  end
22
23
  new(maps, history: project.history)
23
24
  end
24
25
 
26
+ # Each map's problems, its history read ahead at the files PrincipleHistory walks for a map, under the folder's
27
+ # name now and its earlier one.
28
+ #: (Project) -> Array[MemberProblems]
29
+ def self.every_map(project)
30
+ ids = project.principles.ids
31
+ project.history.read_ahead(ids.map { [project.principles.file(it), project.principles.earlier_file(it)] })
32
+ ids.map { MemberProblems.for(project, it, listing_all: true) }
33
+ end
34
+ private_class_method :every_map
35
+
25
36
  # history, when given, lets each missing file carry the rename git detects for it.
26
37
  #: (Array[MemberProblems], ?history: ProjectHistory?) -> void
27
38
  def initialize(maps, history: nil)
@@ -91,6 +102,18 @@ module Principles
91
102
  end
92
103
  end
93
104
 
105
+ # In git, for each map whose map or member files differ from HEAD, those files, which an audit of it refuses
106
+ # until they are committed; empty outside git, where an audit judges files as they are.
107
+ #: (MappedPrinciples) -> Hash[String, Array[String]]
108
+ def audit_files_to_commit(principles)
109
+ return {} unless @history&.inside_git?
110
+
111
+ @maps.filter_map do |map|
112
+ files = @history.uncommitted(principles.audited_files(map.principle_id, map.members))
113
+ [map.principle_id, files] unless files.empty?
114
+ end.to_h
115
+ end
116
+
94
117
  private
95
118
 
96
119
  # Each map's files holding a member with no baseline, by map id; a map with none is left out.
@@ -117,6 +117,11 @@ module Principles
117
117
  #: (Locator) -> LocatedText
118
118
  def locate_for_authoring(locator) = member_file(locator.path).locate_for_authoring(locator)
119
119
 
120
+ # The last line of the paragraph or list item a passage named by its opening alone maps only the first line
121
+ # of, as its file type finds it; nil when none runs on.
122
+ #: (Locator) -> Integer?
123
+ def continues_to(locator) = member_file(locator.path).continues_to(locator)
124
+
120
125
  # The lines locator finds now, as one text; refused as located_text refuses.
121
126
  #: (Locator) -> String
122
127
  def member_text(locator) = member_file(locator.path).lines_of(locator).join
@@ -220,10 +225,10 @@ module Principles
220
225
  def answer(file, stdin:, given:) = SentAnswer.read(@files, file, stdin:, given:)
221
226
 
222
227
  # What list-candidate-files found, as CandidateInventory reads it from the project, after a commit when given.
223
- #: (?after: String?, ?also: Array[String]) -> CandidateInventory
224
- def candidate_files(after: nil, also: [])
228
+ #: (?after: String?, ?also: Array[String], ?only: Array[String]) -> CandidateInventory
229
+ def candidate_files(after: nil, also: [], only: [])
225
230
  mapped = after ? principles.readable_paths : []
226
- search_paths = SurveySearchPaths.new(recorded: guidance_search_paths.globs, also:)
231
+ search_paths = SurveySearchPaths.new(recorded: guidance_search_paths.globs, also:, only:)
227
232
  CandidateInventory.read(@files, history, search_paths:, after:, mapped:)
228
233
  end
229
234
 
@@ -40,31 +40,21 @@ module Principles
40
40
  end
41
41
 
42
42
  # Each member's id and the hash of its text at the commit, or nil when the commit lacks a member's file or the
43
- # member's place in it.
43
+ # member's place in it, since an audit's base needs every member.
44
44
  #: (PrincipleMembers) -> Hash[String, String]?
45
45
  def hashes(members)
46
- members.to_h do |member|
47
- path = member.path
48
- found = text(path)
49
- return nil unless found
46
+ members.to_h { |member| [member.id, hash_of(member) || (return nil)] }
47
+ end
50
48
 
51
- [member.id, MemberFile.for(path: path, text: found).locate(member.locator).sha256]
52
- end
49
+ # The hash of member's text at the commit, or nil when the commit lacks its file or its place in it, as a
50
+ # member's history walks commits where an earlier placement may no longer be found.
51
+ #: (Member) -> String?
52
+ def hash_of(member)
53
+ committed = text(member.path) or return nil
54
+ MemberFile.for(path: member.path, text: committed).locate(member.locator).sha256
53
55
  rescue Error
54
56
  nil
55
57
  end
56
-
57
- # Each member's key and the hash of its text at the commit, leaving out a member the commit cannot find.
58
- #: (Array[Hash[String, untyped]]) -> Hash[String, String]
59
- def hashes_by_key(members)
60
- members.map { |fields| Member.new(fields) }.each_with_object({}) do |member, found|
61
- path = member.path
62
- committed = text(path) or next
63
- found[member.key] = MemberFile.for(path: path, text: committed).locate(member.locator).sha256
64
- rescue Error
65
- next
66
- end
67
- end
68
58
  end
69
59
  end
70
60
  end
@@ -151,6 +151,14 @@ module Principles
151
151
  @repository.commits_touching(through_links(paths), since: since, up_to: head || "HEAD")
152
152
  end
153
153
 
154
+ # Reads the commits on head's first-parent line touching each of path_sets, and the links and files they lead
155
+ # through, in one git log, so commits_touching asks git no more for any of them. Nothing outside git.
156
+ #: (Array[Array[String]]) -> void
157
+ def read_ahead(path_sets)
158
+ up_to = head or return
159
+ @repository.read_ahead(path_sets.map { through_links(it) }, up_to: up_to)
160
+ end
161
+
154
162
  # The commits after commit, up to HEAD, that one move of HEAD brought in, whatever they changed.
155
163
  #: (String) -> CommitsBroughtIn
156
164
  def commits_brought_in(commit)
@@ -0,0 +1,36 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ module Principles
5
+ # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
6
+ module Models
7
+ # One line of a file as prose reads it: blank, a heading, or continuing the paragraph or list item before it.
8
+ class ProseLine
9
+ # A line that starts a new block, a list item or a heading, rather than continuing the paragraph before it.
10
+ NEW_BLOCK = /\A(([-*+]|\d+[.)])\s|#)/
11
+ # What a heading line starts with, a block of its own line.
12
+ HEADING = "#"
13
+
14
+ #: (String) -> void
15
+ def initialize(text)
16
+ @text = text
17
+ freeze
18
+ end
19
+
20
+ # The line without its indentation or line break.
21
+ #: () -> String
22
+ def text = @text.strip
23
+
24
+ #: () -> bool
25
+ def blank? = text.empty?
26
+
27
+ #: () -> bool
28
+ def heading? = text.start_with?(HEADING)
29
+
30
+ # Whether the line continues the paragraph before it: it is not blank and starts no new block. It keeps its
31
+ # line break, which NEW_BLOCK's \s may match, so a bare "-" line still starts a list item.
32
+ #: () -> bool
33
+ def continues? = !blank? && !@text.lstrip.match?(NEW_BLOCK)
34
+ end
35
+ end
36
+ end
@@ -19,9 +19,11 @@ module Principles
19
19
  # The release that first wrote each format. In the commit that adds a field an older copy would drop on a
20
20
  # rewrite, add the next format here, set by hand to the release that commit ships in, and name the field in
21
21
  # FIELD_FORMATS, so only a record carrying it moves up.
22
- FIRST_RELEASES = { 1 => "0.0.4", 2 => "0.0.8" }.freeze #: Hash[Integer, String]
23
- # The format each later field needs: the open record's correction, which show-open-correction prints again.
24
- FIELD_FORMATS = { "correction" => 2 }.freeze #: Hash[String, Integer]
22
+ # Add its row to prompts/docs/AUDIT_RECORD_FORMAT_HISTORY.md too.
23
+ FIRST_RELEASES = { 1 => "0.0.4", 2 => "0.0.8", 3 => "0.1.0" }.freeze #: Hash[Integer, String]
24
+ # The format each later field needs, by its path of property names: the open record's correction, which
25
+ # show-open-correction prints again, and a finding's changes since its audit, which the next full audit shows.
26
+ FIELD_FORMATS = { "correction" => 2, "findings.changed_since_audit" => 3 }.freeze #: Hash[String, Integer]
25
27
  # The newest format this copy writes and rewrites without losing a field; formats count up from 1.
26
28
  NEWEST = FIRST_RELEASES.size #: Integer
27
29
  FIELDS = %w[format format_first_release].freeze
@@ -42,7 +44,7 @@ module Principles
42
44
  # format's first release, worked out again on each write.
43
45
  #: () -> Hash[String, untyped]
44
46
  def stamped
45
- format = FIELD_FORMATS.filter_map { |field, needs| needs if @fields.key?(field) }.max || 1
47
+ format = FIELD_FORMATS.filter_map { |path, needs| needs if holds?(@fields, path.split(".")) }.max || 1
46
48
  { "format" => format, "format_first_release" => FIRST_RELEASES.fetch(format), **@fields.except(*FIELDS) }
47
49
  end
48
50
 
@@ -88,6 +90,17 @@ module Principles
88
90
 
89
91
  private
90
92
 
93
+ # Whether value holds the property names leads to, through any entry of an array on the way.
94
+ #: (untyped, Array[String]) -> bool
95
+ def holds?(value, names)
96
+ first, *rest = names
97
+ return true unless first
98
+ return value.any? { holds?(it, names) } if value.is_a?(Array)
99
+ return false unless value.is_a?(Hash) && value.key?(first)
100
+
101
+ holds?(value[first], rest)
102
+ end
103
+
91
104
  #: (String, String, String) -> bot
92
105
  def refuse(file, format_stated, update)
93
106
  raise NewerFormatError.new("newer_audit_record", "#{file} is format #{format_stated}",
@@ -82,20 +82,60 @@ module Principles
82
82
  #: () -> bool
83
83
  def fixed? = action == "fixed"
84
84
 
85
- # This finding pointed at recorded_at, the move from where it pointed appended to its repointed with when and
86
- # why, or itself when it already points there, so an identical retry writes nothing.
87
- #: (WriteUpPointer, at: String, reason: String) -> RecordedFinding
88
- def moved_to(pointer, at:, reason:)
85
+ # Each change made to it since the full audit that kept it, as field, from, to, at and reason, for the next full
86
+ # audit to see before its record drops them; a repoint's move, which records written by earlier releases keep
87
+ # under repointed, is a change to recorded_at. Nil when none was made.
88
+ #: () -> Array[Hash[String, untyped]]?
89
+ def changes_since_audit
90
+ moves = (@fields["repointed"] || []).map { |moved| { "field" => "recorded_at", **moved } }
91
+ changes = [*moves, *@fields["changed_since_audit"]]
92
+ changes unless changes.empty?
93
+ end
94
+
95
+ # This finding pointed at pointer, or written up nowhere for nil, the change kept beside it with when and why;
96
+ # or itself when it already points there, so an identical retry writes nothing.
97
+ #: (WriteUpPointer?, at: String, reason: String) -> RecordedFinding
98
+ def pointed_at(pointer, at:, reason:)
89
99
  return self if recorded_at == pointer
90
100
 
91
- moved = { "from" => @fields["recorded_at"], "to" => pointer.to_h, "at" => at, "reason" => reason }
92
- RecordedFinding.new(@fields.merge("recorded_at" => pointer.to_h,
93
- "repointed" => [*@fields["repointed"], moved]))
101
+ change = { "field" => "recorded_at", "from" => @fields["recorded_at"], "to" => pointer&.to_h, "at" => at,
102
+ "reason" => reason }
103
+ with(@fields.except("recorded_at").merge(pointer ? { "recorded_at" => pointer.to_h } : {}), change)
104
+ end
105
+
106
+ # This finding left open as action, deferred or accepted, its reason the decision's, the change kept beside it,
107
+ # an accepted one waiting on no one; or itself when it already is, so an identical retry writes nothing.
108
+ #: (String, at: String, reason: String) -> RecordedFinding
109
+ def acting(new_action, at:, reason:)
110
+ return self if action == new_action
111
+
112
+ change = { "field" => "action", "from" => action, "to" => new_action, "at" => at, "reason" => reason }
113
+ acted = with(@fields.merge("action" => new_action, "reason" => reason), change)
114
+ new_action == "accepted" ? acted.waiting_on(nil, at:, reason:) : acted
115
+ end
116
+
117
+ # This finding waiting on line, dated at, or on no one for nil, the change kept beside it with why, or itself
118
+ # when it already waits on line, so an identical retry writes nothing.
119
+ #: (String?, at: String, reason: String) -> RecordedFinding
120
+ def waiting_on(line, at:, reason:)
121
+ return self if waits_on == line
122
+
123
+ change = { "field" => "waits_on", "from" => waits_on, "to" => line, "at" => at, "reason" => reason }
124
+ waiting = line ? { "waits_on" => line, "waits_on_since" => at } : {}
125
+ with(@fields.except("waits_on", "waits_on_since").merge(waiting), change)
94
126
  end
95
127
 
96
128
  # The finding as its record's file holds it.
97
129
  #: () -> Hash[String, untyped]
98
130
  def to_h = @fields
131
+
132
+ private
133
+
134
+ # The finding with fields, change appended to the changes kept beside it since its audit.
135
+ #: (Hash[String, untyped], Hash[String, untyped]) -> RecordedFinding
136
+ def with(fields, change)
137
+ RecordedFinding.new(fields.merge("changed_since_audit" => [*@fields["changed_since_audit"], change]))
138
+ end
99
139
  end
100
140
  end
101
141
  end