principles 0.0.4 → 0.0.5

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 (137) hide show
  1. checksums.yaml +4 -4
  2. data/lib/principles/cli.rb +3 -1
  3. data/lib/principles/command_line.rb +35 -7
  4. data/lib/principles/command_routes.rb +4 -3
  5. data/lib/principles/controllers/acknowledgement.rb +80 -14
  6. data/lib/principles/controllers/answer.rb +42 -75
  7. data/lib/principles/controllers/answer_phase.rb +54 -0
  8. data/lib/principles/controllers/assessment.rb +33 -9
  9. data/lib/principles/controllers/audit.rb +17 -6
  10. data/lib/principles/controllers/audit_completion.rb +6 -4
  11. data/lib/principles/controllers/command.rb +3 -2
  12. data/lib/principles/controllers/correction.rb +19 -9
  13. data/lib/principles/controllers/full_audit_request.rb +10 -0
  14. data/lib/principles/controllers/inventory.rb +25 -20
  15. data/lib/principles/controllers/member_problems_report.rb +11 -4
  16. data/lib/principles/controllers/members_a_commit_changed.rb +2 -1
  17. data/lib/principles/controllers/members_to_assess.rb +20 -12
  18. data/lib/principles/controllers/repoint.rb +18 -22
  19. data/lib/principles/data/bounded_file.rb +9 -4
  20. data/lib/principles/data/command-steps.json +9 -9
  21. data/lib/principles/data/project_files.rb +11 -8
  22. data/lib/principles/data/schema_check.rb +16 -6
  23. data/lib/principles/data/schemas/acknowledgement-answer.schema.json +17 -3
  24. data/lib/principles/data/schemas/answer-guide.schema.json +5 -10
  25. data/lib/principles/data/schemas/audit-record.schema.json +10 -1
  26. data/lib/principles/data/schemas/command-steps.schema.json +2 -1
  27. data/lib/principles/invocation_options.rb +1 -1
  28. data/lib/principles/models/acknowledgement.rb +73 -84
  29. data/lib/principles/models/acknowledgement_refusal.rb +90 -0
  30. data/lib/principles/models/answer_place.rb +4 -0
  31. data/lib/principles/models/assessment_answer.rb +0 -45
  32. data/lib/principles/models/assessment_left_as_printed.rb +81 -0
  33. data/lib/principles/models/audit_record.rb +9 -17
  34. data/lib/principles/models/audit_records.rb +19 -16
  35. data/lib/principles/models/candidate_files.rb +30 -7
  36. data/lib/principles/models/candidate_inventory.rb +23 -6
  37. data/lib/principles/models/candidate_listing.rb +12 -3
  38. data/lib/principles/models/candidate_outline.rb +195 -0
  39. data/lib/principles/models/carried_member.rb +42 -0
  40. data/lib/principles/models/check_since_last_audit.rb +16 -3
  41. data/lib/principles/models/command_steps.rb +17 -1
  42. data/lib/principles/models/declared_map_changes.rb +5 -4
  43. data/lib/principles/models/frontmatter_fields.rb +9 -8
  44. data/lib/principles/models/instruction_named.rb +35 -0
  45. data/lib/principles/models/left_as_printed.rb +39 -0
  46. data/lib/principles/models/member_assessments.rb +9 -31
  47. data/lib/principles/models/member_file.rb +35 -11
  48. data/lib/principles/models/member_hashes_as_written.rb +10 -6
  49. data/lib/principles/models/member_lines.rb +63 -15
  50. data/lib/principles/models/member_problems.rb +24 -18
  51. data/lib/principles/models/member_repair_moves.rb +54 -0
  52. data/lib/principles/models/missing_files.rb +2 -2
  53. data/lib/principles/models/open_findings_elsewhere.rb +4 -0
  54. data/lib/principles/models/other_templates_placeholders.rb +81 -42
  55. data/lib/principles/models/outline_budget.rb +127 -0
  56. data/lib/principles/models/outline_group.rb +31 -0
  57. data/lib/principles/models/outline_section.rb +56 -0
  58. data/lib/principles/models/principle_members.rb +11 -0
  59. data/lib/principles/models/prior_decision_fields.rb +3 -2
  60. data/lib/principles/models/prior_finding_decision.rb +14 -4
  61. data/lib/principles/models/project.rb +10 -19
  62. data/lib/principles/models/quoted_change.rb +137 -0
  63. data/lib/principles/models/record_format.rb +6 -2
  64. data/lib/principles/models/recorded_entry.rb +6 -0
  65. data/lib/principles/models/recorded_hashes.rb +7 -3
  66. data/lib/principles/models/recorded_members.rb +27 -15
  67. data/lib/principles/models/refusal_before_shape.rb +21 -0
  68. data/lib/principles/models/repoint.rb +13 -0
  69. data/lib/principles/models/reported_check.rb +3 -13
  70. data/lib/principles/models/sent_answer.rb +51 -0
  71. data/lib/principles/models/shape_problems_beside.rb +40 -0
  72. data/lib/principles/models/submitted_answer.rb +34 -0
  73. data/lib/principles/models/template_placeholders.rb +34 -4
  74. data/lib/principles/models/text_within.rb +55 -0
  75. data/lib/principles/models/unchecked_members.rb +76 -0
  76. data/lib/principles/models/write_up_opening.rb +21 -4
  77. data/lib/principles/session_start_options.rb +2 -2
  78. data/lib/principles/start_audit_options.rb +19 -4
  79. data/lib/principles/version.rb +2 -2
  80. data/lib/principles/views/acknowledgement_result.rb +15 -7
  81. data/lib/principles/views/assessed_members.rb +43 -10
  82. data/lib/principles/views/assessment_request.rb +89 -30
  83. data/lib/principles/views/audit_result.rb +19 -2
  84. data/lib/principles/views/candidate_files_result.rb +41 -19
  85. data/lib/principles/views/check_notes.rb +44 -0
  86. data/lib/principles/views/check_result_template.rb +14 -23
  87. data/lib/principles/views/instruction.rb +74 -0
  88. data/lib/principles/views/json_with_entries_on_lines.rb +51 -0
  89. data/lib/principles/views/loads_to_check_result.rb +2 -2
  90. data/lib/principles/views/map_result.rb +2 -2
  91. data/lib/principles/views/member_change_since_baseline.rb +22 -5
  92. data/lib/principles/views/member_repairs.rb +21 -52
  93. data/lib/principles/views/members_a_commit_changed.rb +16 -6
  94. data/lib/principles/views/one_member_check_result_template.rb +43 -0
  95. data/lib/principles/views/open_findings_elsewhere_result.rb +3 -3
  96. data/lib/principles/views/open_findings_line.rb +77 -23
  97. data/lib/principles/views/principle_read_result.rb +20 -28
  98. data/lib/principles/views/prior_findings_result.rb +23 -15
  99. data/lib/principles/views/prompts/acknowledge-a-change/run-the-check-and-report-it-passing.md +3 -0
  100. data/lib/principles/views/prompts/acknowledge-a-change/say-whether-the-change-keeps-the-meaning.md +6 -12
  101. data/lib/principles/views/prompts/assess-every-member/answer-guide.json +5 -10
  102. data/lib/principles/views/prompts/assess-every-member/assess-each-member-and-the-set.md +0 -4
  103. data/lib/principles/views/prompts/assess-every-member/claim-an-earlier-first-raised-at.md +7 -0
  104. data/lib/principles/views/prompts/assess-every-member/decide-each-prior-finding.md +4 -16
  105. data/lib/principles/views/prompts/assess-every-member/judge-the-set-with-carried-assessments.md +5 -10
  106. data/lib/principles/views/prompts/assess-every-member/keep-or-change-what-a-deferral-waits-on.md +4 -0
  107. data/lib/principles/views/prompts/assess-every-member/read-a-compact-carried-member.md +8 -0
  108. data/lib/principles/views/prompts/assess-every-member/read-a-trimmed-instruction-whole.md +1 -0
  109. data/lib/principles/views/prompts/assess-every-member/review-a-new-or-changed-check-before-running-it.md +9 -0
  110. data/lib/principles/views/prompts/assess-every-member/say-what-observed-means.md +4 -0
  111. data/lib/principles/views/prompts/dispose-of-each-finding/fix-or-record-each-finding.md +4 -3
  112. data/lib/principles/views/prompts/find-a-principle-to-map/ask-the-owner-after-the-table.md +1 -0
  113. data/lib/principles/views/prompts/find-a-principle-to-map/ask-which-principle-to-map.md +2 -0
  114. data/lib/principles/views/prompts/find-a-principle-to-map/choose-one-principle.md +9 -5
  115. data/lib/principles/views/prompts/find-a-principle-to-map/leave-test-data-out-of-the-survey.md +1 -0
  116. data/lib/principles/views/prompts/find-a-principle-to-map/read-a-cut-section-back.md +3 -0
  117. data/lib/principles/views/prompts/find-a-principle-to-map/read-left-out-files.md +3 -0
  118. data/lib/principles/views/prompts/learn-what-a-command-does/list-candidate-files.md +21 -6
  119. data/lib/principles/views/prompts/learn-what-a-command-does/list-member-problems.md +4 -4
  120. data/lib/principles/views/prompts/learn-what-a-command-does/list-open-findings.md +10 -11
  121. data/lib/principles/views/prompts/learn-what-a-command-does/list-principles.md +1 -2
  122. data/lib/principles/views/prompts/learn-what-a-command-does/show-answer-check.md +1 -1
  123. data/lib/principles/views/prompts/learn-what-a-command-does/show-meta-principle-passage.md +5 -5
  124. data/lib/principles/views/prompts/learn-what-a-command-does/start-audit.md +24 -24
  125. data/lib/principles/views/prompts/learn-what-a-command-does/submit-answer.md +3 -3
  126. data/lib/principles/views/prompts/report-after-a-commit/the-principles-skill-is-where-to-start.md +1 -0
  127. data/lib/principles/views/prompts/set-principles-up/keep-them-held-between-audits.md +2 -0
  128. data/lib/principles/views/prompts/set-principles-up/name-the-step-each-member-lacks.md +2 -0
  129. data/lib/principles/views/prompts/set-principles-up/place-the-passage-with-the-user.md +9 -6
  130. data/lib/principles/views/prompts/start-a-session/see-the-earlier-line.md +2 -0
  131. data/lib/principles/views/record_waiting_on_an_upgrade.rb +3 -3
  132. data/lib/principles/views/request_part.rb +16 -8
  133. data/lib/principles/views/request_parts.rb +4 -0
  134. metadata +39 -5
  135. data/lib/principles/views/prompts/find-a-principle-to-map/ask-the-owner-before-mapping.md +0 -1
  136. data/lib/principles/views/prompts/report-after-a-commit/the-map-and-audit-skill-says-how-to-audit-these-members.md +0 -1
  137. data/lib/principles/views/prompts/set-principles-up/set-up-first.md +0 -2
@@ -12,6 +12,10 @@ module Principles
12
12
  # make (D85). A record without a format is older than format 1. An open record outside git keeps the last
13
13
  # completed one whole, so that one's format counts too.
14
14
  class RecordFormat
15
+ # The refusal of a record a later copy wrote at a format this copy cannot rewrite or read, which a read across
16
+ # principles, or one comparing members, catches by its kind rather than its code (D85, #1077).
17
+ class NewerFormatError < Error; end
18
+
15
19
  # Raise NEWEST in the commit that adds a field an older copy would drop on a rewrite, and set FIRST_RELEASE by
16
20
  # hand to the release that commit ships in.
17
21
  NEWEST = 1
@@ -74,8 +78,8 @@ module Principles
74
78
 
75
79
  #: (String, String, String) -> bot
76
80
  def refuse(file, format_stated, update)
77
- raise Error.new("newer_audit_record", "#{file} is format #{format_stated}",
78
- "#{update} Leave the record as it is: it is not broken.", file)
81
+ raise NewerFormatError.new("newer_audit_record", "#{file} is format #{format_stated}",
82
+ "#{update} Leave the record as it is: it is not broken.", file)
79
83
  end
80
84
 
81
85
  # The highest format the record holds, with the release that first wrote it.
@@ -16,6 +16,12 @@ module Principles
16
16
  freeze
17
17
  end
18
18
 
19
+ # The key the record knows the member by: the key kept beside its entry when it differs from the id it was
20
+ # filed under, else that id. The one owner of that rule, which MemberHashesAsWritten reads a newer record by
21
+ # too (#1066).
22
+ #: () -> String
23
+ def key = @entry["key"] || @filed_id
24
+
19
25
  # The assessment the audit kept, with when it was made and what it was judged on; nil when it kept none.
20
26
  #: () -> Hash[String, untyped]?
21
27
  def assessment = @entry["assessment"]
@@ -15,9 +15,13 @@ module Principles
15
15
  freeze
16
16
  end
17
17
 
18
- # Each hash, by the key of the member it was recorded for.
19
- #: Hash[String, String]
20
- attr_reader :by_key
18
+ # The hash the record holds for the member known by key, or nil when it holds none.
19
+ #: (String) -> String?
20
+ def hash_for(key) = @by_key[key]
21
+
22
+ # Whether the record holds no hash, as for a principle with no record.
23
+ #: () -> bool
24
+ def empty? = @by_key.empty?
21
25
 
22
26
  # Whether a later copy wrote the record these hashes come from.
23
27
  #: () -> bool
@@ -13,11 +13,6 @@ module Principles
13
13
  # two differ, holding its hash then, the result reported for its check (D30), its accepted loading decision
14
14
  # and the acknowledgements made for it since (D33). Callers reach them through AuditRecord#members.
15
15
  class RecordedMembers
16
- # The key a record knows the member filed under id by: the key kept beside its entry when the two differ, else
17
- # that id. The one owner of that rule, which MemberHashesAsWritten reads a newer record by too (#1066).
18
- #: (untyped, untyped) -> untyped
19
- def self.filed_key(id, entry) = entry["key"] || id
20
-
21
16
  #: (Hash[String, untyped]) -> void
22
17
  def initialize(entries)
23
18
  @entries = entries
@@ -55,7 +50,7 @@ module Principles
55
50
  # Each hash the record assessed, by the key of the member it belongs to.
56
51
  #: () -> Hash[String, String]
57
52
  def hashes_by_key
58
- @entries.to_h { |id, entry| [RecordedMembers.filed_key(id, entry), entry.fetch("sha256")] }
53
+ @entries.to_h { |id, entry| [RecordedEntry.new(id, entry).key, entry.fetch("sha256")] }
59
54
  end
60
55
 
61
56
  # The hash the record assessed for the member known by key, or nil when it assessed no such member.
@@ -72,6 +67,21 @@ module Principles
72
67
  result.key?("exit_status") ? result.fetch("command") : nil
73
68
  end
74
69
 
70
+ # How check, the map's check now for the member known by key, stands to the one the record's audit ran:
71
+ # :unchanged when it ran this same command, or none for a member that has none; :removed when it ran one the
72
+ # member no longer has; :changed when it ran another; :not_run when it was told this one could not be run; :new
73
+ # when it reported none (D30, D63, #1071). The one owner of that comparison, which a full audit's template and a
74
+ # one-member audit both ask.
75
+ #: (String, String?) -> Symbol
76
+ def check_since(key, check)
77
+ ran = check_run_for(key)
78
+ return :unchanged if ran == check
79
+ return :removed unless check
80
+ return :changed if ran
81
+
82
+ entry_for(key)&.dig("check_result", "command") == check ? :not_run : :new
83
+ end
84
+
75
85
  # What the review of that run rested on, for the member known by key, as the record keeps it: its review_basis
76
86
  # and any review_basis_since, or nothing when the record ran no check for it or kept no line (D63).
77
87
  #: (String) -> Hash[String, untyped]
@@ -106,13 +116,15 @@ module Principles
106
116
  end.max_by(&:last)
107
117
  end
108
118
 
109
- # These entries with the member known by key moved to change's to_sha256, and change kept beside it (D33).
110
- #: (String, Hash[String, untyped]) -> RecordedMembers
111
- def acknowledged(key, change)
119
+ # These entries with the member known by key moved to change's to_sha256, and change kept beside it (D33), with
120
+ # check_result, when given, in place of the result it kept (#1071).
121
+ #: (String, Hash[String, untyped], ?check_result: Hash[String, untyped]?) -> RecordedMembers
122
+ def acknowledged(key, change, check_result: nil)
112
123
  RecordedMembers.new(@entries.to_h do |id, entry|
113
- next [id, entry] unless RecordedMembers.filed_key(id, entry) == key
124
+ next [id, entry] unless RecordedEntry.new(id, entry).key == key
114
125
 
115
- [id, entry.merge("sha256" => change.fetch("to_sha256"),
126
+ result = check_result ? { "check_result" => check_result } : {}
127
+ [id, entry.merge("sha256" => change.fetch("to_sha256"), **result,
116
128
  "acknowledgements" => [*entry["acknowledgements"], change])]
117
129
  end)
118
130
  end
@@ -120,13 +132,13 @@ module Principles
120
132
  # The key of the member filed under id: its stored key when it had moved, or else id.
121
133
  #: (String) -> String
122
134
  def key_of(id)
123
- RecordedMembers.filed_key(id, @entries.fetch(id, {}))
135
+ RecordedEntry.new(id, @entries.fetch(id, {})).key
124
136
  end
125
137
 
126
138
  # The entry of the member known by key, under the id it was filed under, or nil when the record has none.
127
139
  #: (String) -> RecordedEntry?
128
140
  def recorded(key)
129
- id, entry = @entries.find { |filed, each| RecordedMembers.filed_key(filed, each) == key }
141
+ id, entry = @entries.find { |filed, each| RecordedEntry.new(filed, each).key == key }
130
142
  RecordedEntry.new(id, entry) if id && entry
131
143
  end
132
144
 
@@ -137,7 +149,7 @@ module Principles
137
149
  RecordedMembers.new(@entries.to_h do |id, entry|
138
150
  next [id, entry] unless entry.dig("assessment", "carried_forward")
139
151
 
140
- [id, entry.merge("assessment" => last&.recorded(RecordedMembers.filed_key(id, entry))&.assessment).compact]
152
+ [id, entry.merge("assessment" => last&.recorded(RecordedEntry.new(id, entry).key)&.assessment).compact]
141
153
  end)
142
154
  end
143
155
 
@@ -147,7 +159,7 @@ module Principles
147
159
  # two differ.
148
160
  #: (String) -> Hash[String, untyped]?
149
161
  def entry_for(key)
150
- @entries.find { |id, entry| RecordedMembers.filed_key(id, entry) == key }&.last
162
+ @entries.find { |id, entry| RecordedEntry.new(id, entry).key == key }&.last
151
163
  end
152
164
  end
153
165
  end
@@ -0,0 +1,21 @@
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
+ # A refusal an answer meets before its shape is checked, for a mistake the shape would refuse without naming, such
8
+ # as a placeholder left in a field of another type (#773, #1084, #1088). A phase lists its own, in order, and
9
+ # SentAnswer reads each, so a new one is declared here once.
10
+ # @interface
11
+ module RefusalBeforeShape
12
+ # Refuses answer, as sent from source, when it makes this mistake; nothing otherwise. shape_problems are the
13
+ # problems its shape check finds, as Data::SchemaCheck#problems gives them, which a refusal may name beside its
14
+ # own, so one refusal names every repair (#1106). Sorbet takes an abstract method only with a forwarding super
15
+ # for its body, which RuboCop would otherwise call useless.
16
+ # @abstract
17
+ #: (untyped, String, Array[Hash[String, String]]) -> void
18
+ def refuse_in(answer, source, shape_problems) = super # rubocop:disable Lint/UselessMethodDefinition
19
+ end
20
+ end
21
+ end
@@ -5,6 +5,8 @@ require_relative "../error"
5
5
  require_relative "answer_fields"
6
6
  require_relative "answer_place"
7
7
  require_relative "audit_record"
8
+ require_relative "audit_records"
9
+ require_relative "last_completed_revision"
8
10
  require_relative "project_history"
9
11
  require_relative "request_id"
10
12
  require_relative "write_up_repoint"
@@ -48,6 +50,17 @@ module Principles
48
50
  completed.repointed(given.map { |repoint, index| located(repoint, index, fields) }, at: at)
49
51
  end
50
52
 
53
+ # The record accepted from the answer, as accepted gives it, and the records it changes among records, not yet
54
+ # written: refused once the record changed after the request, whose revision the answer quotes back in its basis,
55
+ # was printed, unless it changes none.
56
+ #: (untyped, records: AuditRecords, at: String) { (Locator) -> void } -> [AuditRecord, Array[AuditRecord]]
57
+ def writable(response, records:, at:, &locate)
58
+ repointed = accepted(response, at:, &locate)
59
+ changes = records.changes(repointed)
60
+ LastCompletedRevision.new(response.dig("basis", "revision")).confirm_writable(@principle_id, @record, changes)
61
+ [repointed, changes]
62
+ end
63
+
51
64
  # The ids of the completed record's open findings that repointed, the record accepted, points elsewhere (T16).
52
65
  #: (AuditRecord) -> Array[String]
53
66
  def moved_ids(repointed) = repointed.write_ups_moved_since(completed)
@@ -51,7 +51,7 @@ module Principles
51
51
  # none; a check it did not run is reviewed before it runs, and the result says in review_basis what that review
52
52
  # rested on (D63). kept is the line the stored record keeps for that run's review, with its
53
53
  # review_basis_since, which an unchanged check's result carries until the answer gives another (D52).
54
- #: (AnswerPlace, String?, ran: String?, kept: Hash[String, untyped]) -> void
54
+ #: (AnswerPlace, String, ran: String?, kept: Hash[String, untyped]) -> void
55
55
  def initialize(assessment, check, ran:, kept:)
56
56
  @assessment = assessment
57
57
  @check = check
@@ -60,11 +60,9 @@ module Principles
60
60
  freeze
61
61
  end
62
62
 
63
- # The reported result, once it is checked against the member's check; nil when it has none.
64
- #: (untyped) -> Hash[String, untyped]?
63
+ # The reported result, once it is checked against the member's check.
64
+ #: (untyped) -> Hash[String, untyped]
65
65
  def result(reported)
66
- return no_check(reported) unless @check
67
-
68
66
  missing_result unless reported
69
67
  given = filled(reported)
70
68
  same_command(given.fetch("command"))
@@ -105,14 +103,6 @@ module Principles
105
103
  "answer_guide.check_result.limits asks for one. #{SHAPES}")
106
104
  end
107
105
 
108
- #: (untyped) -> nil
109
- def no_check(reported)
110
- return unless reported
111
-
112
- refuse(reported_at, "the member has no check, so its assessment takes no check_result",
113
- "Remove check_result from this member's assessment.")
114
- end
115
-
116
106
  #: (String) -> void
117
107
  def same_command(command)
118
108
  return if command == @check
@@ -0,0 +1,51 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "../data/schema_check"
5
+ require_relative "../error"
6
+ require_relative "refusal_before_shape"
7
+
8
+ module Principles
9
+ module Models
10
+ # An answer an agent sent for a phase's request, read from its source, whose head, the fields every answer hands
11
+ # back as its request printed them, is checked on arrival: they say whether it still answers that request
12
+ # (#990). The rest is checked only once that request is known, against the placeholders its template printed and
13
+ # the phase's schema (D29, #1052, #1084).
14
+ class SentAnswer
15
+ HEAD = %w[request_id basis].freeze #: Array[String]
16
+
17
+ # What a refusal's message calls the answer: the agent holds its file's path, which cost context in every
18
+ # repair (#1112, #1118).
19
+ NAMED = "The answer" #: String
20
+ # What a refusal to read its file calls that file (#1131).
21
+ FILE_NAMED = "The answer file" #: String
22
+
23
+ REPAIR = "Repair each field listed, starting from the request's response_template." #: String
24
+
25
+ # parsed is the answer as its source, a file or - for stdin, holds it; check is its phase's answer schema.
26
+ #: (untyped, String, Data::SchemaCheck) -> void
27
+ def initialize(parsed, source, check)
28
+ @parsed = parsed
29
+ @source = source
30
+ @check = check
31
+ check.only(HEAD).checked(parsed, code: "invalid_answer", source: source, repair: REPAIR, named: NAMED)
32
+ freeze
33
+ end
34
+
35
+ # The answer's head, checked.
36
+ #: () -> Hash[String, untyped]
37
+ def head = @parsed.slice(*HEAD)
38
+
39
+ # The whole answer, once each refusal its request's rules make before its shape, in order, then its shape, is
40
+ # checked: such as a stray check_result on a member with no check, a field holding a placeholder its
41
+ # request's template did not print, and a one-member audit's field left as its template printed it, each handed
42
+ # the shape's problems to name beside its own (#1106).
43
+ #: (Array[RefusalBeforeShape]) -> untyped
44
+ def checked(before_shape)
45
+ shape_problems = @check.problems(@parsed)
46
+ before_shape.each { |refusal| refusal.refuse_in(@parsed, @source, shape_problems) }
47
+ @check.checked(@parsed, code: "invalid_answer", source: @source, repair: REPAIR, named: NAMED)
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,40 @@
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
+ # The problems an answer's shape has beside those a refusal before the shape names, which that refusal names too,
8
+ # so one refusal lists every repair (#1106, #1109), and says so in its message, which an agent reads first (#1130).
9
+ class ShapeProblemsBeside
10
+ # What the message adds when there are any.
11
+ MESSAGE = ", and fields not in the shape this tool reads" #: String
12
+
13
+ # problems are the shape's, as Data::SchemaCheck#problems gives them; each at or under a pointer in named, which
14
+ # the refusal names, is left out, since its repair repairs that one too.
15
+ #: (Array[Hash[String, String]], named: Array[String]) -> void
16
+ def initialize(problems, named:)
17
+ elsewhere = problems.reject { |problem| under?(problem.fetch("pointer"), named) }
18
+ @fields = elsewhere.freeze #: Array[Hash[String, String]]
19
+ freeze
20
+ end
21
+
22
+ #: Array[Hash[String, String]]
23
+ attr_reader :fields
24
+
25
+ # The refusal's own message, saying when it names these too.
26
+ #: (String) -> String
27
+ def message(own) = "#{own}#{MESSAGE unless @fields.empty?}"
28
+
29
+ # The refusal's own repair, with also, how to repair these, when there are any.
30
+ #: (String, String) -> String
31
+ def repair(own, also) = "#{own}#{also unless @fields.empty?}"
32
+
33
+ private
34
+
35
+ # Whether pointer is one of named, or inside one.
36
+ #: (String, Array[String]) -> bool
37
+ def under?(pointer, named) = named.any? { pointer == it || pointer.start_with?("#{it}/") }
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,34 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "prior_findings"
5
+ require_relative "request_id"
6
+
7
+ module Principles
8
+ # The domain logic, which loads and saves itself through data/. models/CLAUDE.md owns the boundary.
9
+ module Models
10
+ # An answer submitted to the request request_id names, its shape checked, with the prior findings a full
11
+ # audit's request listed, which checking its head found (#1052), or nil for any other phase.
12
+ class SubmittedAnswer
13
+ #: RequestId
14
+ attr_reader :request_id
15
+
16
+ #: untyped
17
+ attr_reader :response
18
+
19
+ #: (RequestId, untyped, PriorFindings?) -> void
20
+ def initialize(request_id, response, prior)
21
+ @request_id = request_id
22
+ @response = response
23
+ @prior = prior
24
+ freeze
25
+ end
26
+
27
+ # The prior findings a full audit's assessment request listed, which only that phase's answer has.
28
+ #: () -> PriorFindings
29
+ def prior_findings
30
+ @prior or raise ArgumentError, "an assessment's prior findings come from checking its head"
31
+ end
32
+ end
33
+ end
34
+ end
@@ -1,6 +1,9 @@
1
1
  # typed: strict
2
2
  # frozen_string_literal: true
3
3
 
4
+ require_relative "left_as_printed"
5
+ require_relative "refusal_before_shape"
6
+
4
7
  module Principles
5
8
  # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
6
9
  module Models
@@ -8,16 +11,24 @@ module Principles
8
11
  # fills the fields that apply and never reshapes the answer (#993, #994). The model that owns a part of the answer
9
12
  # holds one, which a controller hands to the view that prints it.
10
13
  class TemplatePlaceholders
14
+ include RefusalBeforeShape
15
+
11
16
  # placeholders is the text printed in each field, by field name, or for a field that takes pointers, one pointer
12
17
  # whose fields hold that text, so the template shows their shape (#1042); empty_lists names the fields a template
13
- # may instead start as an empty list, which is not given either (#838).
14
- #: (Hash[String, untyped], ?empty_lists: Array[String]) -> void
15
- def initialize(placeholders, empty_lists: [])
18
+ # may instead start as an empty list, which is not given either (#838); also_printed, the other text a template
19
+ # may print in a field, refused alike (#1142).
20
+ #: (Hash[String, untyped], ?empty_lists: Array[String], ?also_printed: Hash[String, String]) -> void
21
+ def initialize(placeholders, empty_lists: [], also_printed: {})
16
22
  @placeholders = placeholders.freeze #: Hash[String, untyped]
17
23
  @empty_lists = empty_lists.freeze #: Array[String]
24
+ @also_printed = also_printed.freeze #: Hash[String, String]
18
25
  freeze
19
26
  end
20
27
 
28
+ # The text printed in each field, with field's other text in place of its own.
29
+ #: (String) -> Hash[String, untyped]
30
+ def with_other_text_in(field) = @placeholders.merge(field => @also_printed.fetch(field))
31
+
21
32
  # The text printed in each field, for the view that prints it.
22
33
  #: () -> Hash[String, untyped]
23
34
  def to_h = @placeholders
@@ -30,12 +41,31 @@ module Principles
30
41
  #: (Hash[String, untyped]) -> Hash[String, untyped]
31
42
  def given(fields) = fields.reject { |field, value| left?(field, value) }
32
43
 
44
+ # Refuses answer, as sent, when it leaves a field as its placeholder, for a template whose every field must be
45
+ # written, as a one-member audit's (#773); read before the answer's shape, which would refuse a placeholder in a
46
+ # field of another type without naming it, so the refusal names the shape's other problems too (#1106). source
47
+ # is unused, as other refusals before the shape name it.
48
+ # @override
49
+ #: (untyped, String, Array[Hash[String, String]]) -> void
50
+ def refuse_in(answer, _source, shape_problems)
51
+ return unless answer.is_a?(Hash)
52
+
53
+ left = @placeholders.keys.select { |field| printed?(field, answer[field]) }
54
+ LeftAsPrinted.new(left.to_h { [it, "/#{it}"] }).refuse(besides: shape_problems)
55
+ end
56
+
33
57
  # Whether value is field's placeholder, or the empty list it may start as, as printed; a field with neither
34
58
  # never is.
35
59
  #: (String, untyped) -> bool
36
60
  def left?(field, value)
37
- (@placeholders.key?(field) && @placeholders[field] == value) || (@empty_lists.include?(field) && value == [])
61
+ (@placeholders.key?(field) && printed?(field, value)) || (@empty_lists.include?(field) && value == [])
38
62
  end
63
+
64
+ private
65
+
66
+ # Whether value is a text the template prints in field.
67
+ #: (String, untyped) -> bool
68
+ def printed?(field, value) = [@placeholders[field], @also_printed[field]].compact.include?(value)
39
69
  end
40
70
  end
41
71
  end
@@ -0,0 +1,55 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ module Principles
5
+ # The domain logic, which loads and saves itself through data/. models/CLAUDE.md owns the boundary.
6
+ module Models
7
+ # A text cut to at most some characters, as a candidate file's outline prints its items, opening line and
8
+ # description: whole when it fits, else back to a sentence end or a whole word (#1155).
9
+ class TextWithin
10
+ # Text up to its last sentence end that a space follows.
11
+ SENTENCES = /\A.*[.!?](?=\s)/
12
+ # Text up to its last whole word.
13
+ WORDS = /\A.*(?=\s)/
14
+
15
+ #: (String, at_most: Integer) -> void
16
+ def initialize(text, at_most:)
17
+ @text = text
18
+ @at_most = at_most
19
+ freeze
20
+ end
21
+
22
+ # Whether the text is longer than at_most characters.
23
+ #: () -> bool
24
+ def cut? = @text.length > @at_most
25
+
26
+ # The text whole, else up to its last sentence end within at_most characters, else up to its last whole word.
27
+ #: () -> String
28
+ def kept
29
+ return @text unless cut?
30
+
31
+ sentences_kept || words_kept
32
+ end
33
+
34
+ # The text whole, else up to its last sentence end, else cut on a word ending in an ellipsis.
35
+ #: () -> String
36
+ def by_sentences = cut? && !sentences_kept ? "#{words_kept}…" : kept
37
+
38
+ # The text whole, else cut on a word ending in an ellipsis.
39
+ #: () -> String
40
+ def by_words = cut? ? "#{words_kept}…" : @text
41
+
42
+ private
43
+
44
+ #: () -> String?
45
+ def sentences_kept = window[SENTENCES]
46
+
47
+ #: () -> String
48
+ def words_kept = window[WORDS] || @text[0, @at_most].to_s
49
+
50
+ # The text's first at_most characters and the one after, so a sentence or word ending at the cap is kept.
51
+ #: () -> String
52
+ def window = @text[0, @at_most + 1].to_s
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,76 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "../error"
5
+ require_relative "../prose_list"
6
+ require_relative "answer_place"
7
+ require_relative "principle_members"
8
+ require_relative "refusal_before_shape"
9
+
10
+ module Principles
11
+ module Models
12
+ # The members of a principle that name no check, whose assessments take no check_result. An answer is read for
13
+ # one before its shape is checked, since the shape would ask for the command such a check_result lacks, sending
14
+ # the agent to fill in what it should remove (#1088).
15
+ class UncheckedMembers
16
+ include RefusalBeforeShape
17
+
18
+ #: (PrincipleMembers) -> void
19
+ def initialize(members)
20
+ @members = members
21
+ freeze
22
+ end
23
+
24
+ # Refuses answer when it gives check_result on the assessment of a member with no check. It reads the answer as
25
+ # sent, so it leaves anything malformed to the shape's refusal or the coverage refusal; source is unused, as
26
+ # other refusals before the shape name it.
27
+ # @override
28
+ #: (untyped, String, Array[Hash[String, String]]) -> void
29
+ def refuse_in(answer, _source, _shape_problems)
30
+ assessments = answer["member_assessments"] if answer.is_a?(Hash)
31
+ return unless assessments.is_a?(Array)
32
+
33
+ assessments.each_with_index do |assessment, index|
34
+ next unless stray?(assessment)
35
+
36
+ refuse_at(AnswerPlace.member_assessment(index, assessment["member_id"]), assessment["check_result"])
37
+ end
38
+ end
39
+
40
+ # Refuses reported, the check_result given on the assessment at place of a member with no check, naming the
41
+ # members that hold the principle's checks, since an agent may have taken this one for one of them (#1090); nil
42
+ # when none is given, as such an assessment should.
43
+ #: (AnswerPlace, untyped) -> nil
44
+ def refuse_at(place, reported)
45
+ return unless reported
46
+
47
+ raise place.child("check_result").refusal(
48
+ Error.new("invalid_check_result", "the member has no check, so its assessment takes no check_result",
49
+ ["Remove check_result from this member's assessment.", checked_on].compact.join(" "))
50
+ )
51
+ end
52
+
53
+ private
54
+
55
+ # The sentence naming the members that hold the principle's checks, or nil when none does.
56
+ #: () -> String?
57
+ def checked_on
58
+ checked = @members.checked_ids
59
+ return if checked.empty?
60
+
61
+ "This principle's #{checked.one? ? "check is" : "checks are"} on #{ProseList.new(checked)}."
62
+ end
63
+
64
+ # Whether assessment gives a check_result for a mapped member that names no check.
65
+ #: (untyped) -> bool
66
+ def stray?(assessment)
67
+ return false unless assessment.is_a?(Hash) && assessment["check_result"]
68
+
69
+ id = assessment["member_id"]
70
+ return false unless id.is_a?(String)
71
+
72
+ @members.unchecked?(id)
73
+ end
74
+ end
75
+ end
76
+ end
@@ -55,11 +55,22 @@ module Principles
55
55
  def to_h
56
56
  index = (@first...@last).find { |each| !line(each).blank? } or return nil
57
57
  text = opening_text(index)
58
- { "text" => text[0, LIMIT], "line" => index + 1 }.merge(text.length > LIMIT ? { "truncated" => true } : {})
58
+ { "text" => cut(text), "line" => index + 1 }.merge(text.length > LIMIT ? { "truncated" => true } : {})
59
59
  end
60
60
 
61
61
  private
62
62
 
63
+ # text whole within the limit, or else up to the last whole word that fits, since a cut mid-word reads as a
64
+ # different phrase (#1078); one word longer than the limit is cut where the limit falls.
65
+ #: (String) -> String
66
+ def cut(text)
67
+ return text if text.length <= LIMIT
68
+
69
+ fits = text[0, LIMIT + 1].to_s
70
+ boundary = fits.rindex(" ") or return fits[0, LIMIT].to_s
71
+ fits[0, boundary].to_s
72
+ end
73
+
63
74
  # A heading line whole, since a heading is not a sentence and "F4." may number it (#739). Otherwise the
64
75
  # first sentence: hard-wrapped lines joined with single spaces up to the sentence's end, or else to its
65
76
  # paragraph's end, a blank line, the next list item or heading, or the file's end. It runs past the region's
@@ -78,13 +89,19 @@ module Principles
78
89
  [line(index), *rest].map(&:text)
79
90
  end
80
91
 
81
- # text up to the end of its first sentence, or all of it when none ends.
92
+ # text up to the end of its first sentence, or all of it when none ends. A mark inside a parenthesis, as in
93
+ # "(e.g. this)", ends no sentence, since cutting there leaves a fragment (#1078).
82
94
  #: (String) -> String
83
95
  def sentence(text)
84
- found = SENTENCE_END.match(text) or return text
85
- text[0, found.end(0)].to_s
96
+ ends = text.to_enum(:scan, SENTENCE_END).map { Regexp.last_match&.end(0) }
97
+ found = ends.compact.find { |at| closed?(text[0, at].to_s) } or return text
98
+ text[0, found].to_s
86
99
  end
87
100
 
101
+ # Whether every parenthesis opened in text is closed by its end.
102
+ #: (String) -> bool
103
+ def closed?(text) = text.count("(") <= text.count(")")
104
+
88
105
  # The line at index, or a blank one past the file's end.
89
106
  #: (Integer) -> Line
90
107
  def line(index) = @lines[index] || Line.new("")
@@ -8,7 +8,7 @@ module Principles
8
8
  # is wrong with it as given, which CommandLine refuses as a usage error.
9
9
  class SessionStartOptions
10
10
  # The sources Claude Code's SessionStart hook names.
11
- SOURCES = %w[startup clear compact].freeze #: Array[String]
11
+ SOURCES = %w[startup resume clear compact].freeze #: Array[String]
12
12
 
13
13
  #: (InvocationOptions) -> void
14
14
  def initialize(options)
@@ -21,7 +21,7 @@ module Principles
21
21
  #: () -> String?
22
22
  def fault
23
23
  start = @start or return
24
- return "--session-start takes startup, clear or compact, not #{start}" unless SOURCES.include?(start)
24
+ return "--session-start takes startup, resume, clear or compact, not #{start}" unless SOURCES.include?(start)
25
25
 
26
26
  "--session-start words only the --short line" unless @short
27
27
  end