principles 0.1.3 → 0.1.4

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 (237) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +120 -0
  3. data/README.md +10 -6
  4. data/lib/principles/command_line.rb +15 -6
  5. data/lib/principles/command_routes.rb +11 -1
  6. data/lib/principles/controllers/acknowledgement.rb +19 -4
  7. data/lib/principles/controllers/acknowledgements.rb +43 -12
  8. data/lib/principles/controllers/after_commit_report.rb +15 -2
  9. data/lib/principles/controllers/assessment.rb +7 -3
  10. data/lib/principles/controllers/audit.rb +17 -8
  11. data/lib/principles/controllers/audit_completion.rb +34 -9
  12. data/lib/principles/controllers/carrier_practices.rb +26 -0
  13. data/lib/principles/controllers/command.rb +29 -2
  14. data/lib/principles/controllers/correction.rb +15 -5
  15. data/lib/principles/controllers/full_audit_request.rb +38 -42
  16. data/lib/principles/controllers/inventory.rb +77 -71
  17. data/lib/principles/controllers/last_record_notice_beside.rb +51 -0
  18. data/lib/principles/controllers/member_addition.rb +19 -4
  19. data/lib/principles/controllers/member_entries.rb +25 -6
  20. data/lib/principles/controllers/member_problems_report.rb +26 -6
  21. data/lib/principles/controllers/members_a_commit_changed.rb +14 -5
  22. data/lib/principles/controllers/members_in_logged_requests.rb +52 -0
  23. data/lib/principles/controllers/members_to_assess.rb +24 -7
  24. data/lib/principles/controllers/meta_principle_setup.rb +2 -2
  25. data/lib/principles/controllers/open_correction.rb +3 -2
  26. data/lib/principles/controllers/parts_to_read.rb +46 -5
  27. data/lib/principles/controllers/plugin_member_addition.rb +48 -0
  28. data/lib/principles/controllers/principle_listing.rb +84 -0
  29. data/lib/principles/controllers/request_logging.rb +52 -0
  30. data/lib/principles/controllers/running_tool.rb +39 -13
  31. data/lib/principles/data/branches_bringing_in.rb +52 -0
  32. data/lib/principles/data/changes_since.rb +56 -0
  33. data/lib/principles/data/claude_code_home.rb +58 -0
  34. data/lib/principles/data/command-steps.json +91 -15
  35. data/lib/principles/data/decoded_leaf.rb +63 -0
  36. data/lib/principles/data/head_reflog.rb +13 -2
  37. data/lib/principles/data/kept_line.rb +32 -0
  38. data/lib/principles/data/known_marketplaces.rb +50 -0
  39. data/lib/principles/data/logged_request_matches.rb +138 -0
  40. data/lib/principles/data/logged_request_scan.rb +120 -0
  41. data/lib/principles/data/map_files.rb +18 -4
  42. data/lib/principles/data/plugin_folder.rb +85 -0
  43. data/lib/principles/data/plugin_registry.rb +73 -0
  44. data/lib/principles/data/principles_store.rb +49 -0
  45. data/lib/principles/data/ran.rb +25 -0
  46. data/lib/principles/data/raw_request_filter.rb +61 -0
  47. data/lib/principles/data/repository.rb +29 -27
  48. data/lib/principles/data/request_logging_folder.rb +167 -0
  49. data/lib/principles/data/running_version.rb +1 -1
  50. data/lib/principles/data/schemas/acknowledgement-answer.schema.json +16 -1
  51. data/lib/principles/data/schemas/acknowledgements-answer.schema.json +16 -1
  52. data/lib/principles/data/schemas/assessment-answer.schema.json +8 -0
  53. data/lib/principles/data/schemas/command-steps.schema.json +1 -1
  54. data/lib/principles/data/schemas/correction-answer-guide.schema.json +2 -1
  55. data/lib/principles/data/schemas/correction-answer.schema.json +14 -0
  56. data/lib/principles/data/schemas/member-map.schema.json +10 -1
  57. data/lib/principles/data/string_counts.rb +39 -0
  58. data/lib/principles/data/tools_on_path.rb +25 -0
  59. data/lib/principles/invocation_options.rb +4 -2
  60. data/lib/principles/launch.rb +14 -4
  61. data/lib/principles/models/acknowledged_check.rb +11 -1
  62. data/lib/principles/models/acknowledgement.rb +5 -0
  63. data/lib/principles/models/acknowledgement_refusal.rb +20 -11
  64. data/lib/principles/models/acknowledgements_to_offer.rb +86 -0
  65. data/lib/principles/models/after_commit_input.rb +5 -0
  66. data/lib/principles/models/answer_fields.rb +3 -0
  67. data/lib/principles/models/assessment_basis.rb +10 -3
  68. data/lib/principles/models/audit.rb +10 -10
  69. data/lib/principles/models/audit_record.rb +10 -0
  70. data/lib/principles/models/audit_records.rb +4 -3
  71. data/lib/principles/models/audit_start.rb +9 -0
  72. data/lib/principles/models/authored_locator.rb +33 -0
  73. data/lib/principles/models/candidate_files.rb +23 -3
  74. data/lib/principles/models/candidate_outline.rb +18 -125
  75. data/lib/principles/models/captured_sessions.rb +128 -0
  76. data/lib/principles/models/carrier_kind.rb +79 -0
  77. data/lib/principles/models/cited_files.rb +12 -3
  78. data/lib/principles/models/decided_prior_findings.rb +5 -0
  79. data/lib/principles/models/disposition.rb +37 -6
  80. data/lib/principles/models/disposition_placeholders.rb +12 -0
  81. data/lib/principles/models/disposition_text_field.rb +4 -0
  82. data/lib/principles/models/feature.rb +53 -0
  83. data/lib/principles/models/finding_outcomes.rb +55 -0
  84. data/lib/principles/models/findings_left_open.rb +9 -11
  85. data/lib/principles/models/head_moves.rb +27 -10
  86. data/lib/principles/models/installed_component.rb +55 -0
  87. data/lib/principles/models/installed_plugins.rb +174 -0
  88. data/lib/principles/models/kept_sessions.rb +34 -0
  89. data/lib/principles/models/locator.rb +8 -0
  90. data/lib/principles/models/logged_request_reach.rb +44 -0
  91. data/lib/principles/models/logged_request_scan_completeness.rb +43 -0
  92. data/lib/principles/models/mapped_baseline.rb +46 -0
  93. data/lib/principles/models/maps_holding_file_whole.rb +42 -0
  94. data/lib/principles/models/markdown_text.rb +143 -0
  95. data/lib/principles/models/member.rb +14 -1
  96. data/lib/principles/models/member_entry.rb +56 -0
  97. data/lib/principles/models/member_lines.rb +11 -0
  98. data/lib/principles/models/member_loads.rb +17 -2
  99. data/lib/principles/models/member_problems.rb +45 -42
  100. data/lib/principles/models/member_reach.rb +36 -0
  101. data/lib/principles/models/member_snapshots.rb +23 -4
  102. data/lib/principles/models/member_text_as_sent.rb +31 -0
  103. data/lib/principles/models/member_texts.rb +45 -0
  104. data/lib/principles/models/members_in_logged_requests.rb +240 -0
  105. data/lib/principles/models/missing_files.rb +41 -16
  106. data/lib/principles/models/nearly_matching_maps.rb +56 -0
  107. data/lib/principles/models/open_correction.rb +5 -0
  108. data/lib/principles/models/open_finding.rb +4 -3
  109. data/lib/principles/models/outline_budget.rb +10 -1
  110. data/lib/principles/models/outline_item.rb +25 -0
  111. data/lib/principles/models/outline_section.rb +15 -4
  112. data/lib/principles/models/part_named.rb +16 -2
  113. data/lib/principles/models/plugin_component.rb +37 -0
  114. data/lib/principles/models/plugin_install.rb +33 -0
  115. data/lib/principles/models/plugin_name.rb +23 -0
  116. data/lib/principles/models/plugin_reference.rb +53 -0
  117. data/lib/principles/models/principle_members.rb +15 -0
  118. data/lib/principles/models/prior_finding_decision.rb +1 -1
  119. data/lib/principles/models/prior_finding_outcomes.rb +12 -1
  120. data/lib/principles/models/problem_report.rb +14 -25
  121. data/lib/principles/models/project.rb +35 -29
  122. data/lib/principles/models/project_history.rb +19 -5
  123. data/lib/principles/models/recorded_members.rb +7 -0
  124. data/lib/principles/models/request_logging.rb +196 -0
  125. data/lib/principles/models/runs_from.rb +0 -5
  126. data/lib/principles/models/script_comments.rb +58 -0
  127. data/lib/principles/models/stale_request.rb +9 -0
  128. data/lib/principles/models/unfixed_part.rb +82 -0
  129. data/lib/principles/models/unresolved_plugin_members.rb +35 -0
  130. data/lib/principles/models/whole_file_member_read.rb +48 -0
  131. data/lib/principles/models/without_prose.rb +20 -0
  132. data/lib/principles/models/workflow_jobs.rb +92 -0
  133. data/lib/principles/show_members_in_logged_requests_options.rb +27 -0
  134. data/lib/principles/show_principle_options.rb +32 -0
  135. data/lib/principles/version.rb +2 -2
  136. data/lib/principles/views/acknowledged_result.rb +31 -12
  137. data/lib/principles/views/acknowledgement_result.rb +18 -13
  138. data/lib/principles/views/acknowledgement_template_alone.rb +55 -0
  139. data/lib/principles/views/acknowledgements_offered.rb +54 -0
  140. data/lib/principles/views/added_member.rb +22 -6
  141. data/lib/principles/views/answer_in_a_file.rb +15 -15
  142. data/lib/principles/views/assessed_members.rb +18 -7
  143. data/lib/principles/views/assessment_request.rb +8 -3
  144. data/lib/principles/views/audit_changes.rb +14 -8
  145. data/lib/principles/views/audit_findings.rb +34 -0
  146. data/lib/principles/views/audit_result.rb +14 -19
  147. data/lib/principles/views/carrier_checks.rb +40 -0
  148. data/lib/principles/views/carrier_practices_page.rb +49 -0
  149. data/lib/principles/views/completed_audit_findings.rb +33 -0
  150. data/lib/principles/views/completed_audit_next_step.rb +12 -4
  151. data/lib/principles/views/consecutive_sections_elsewhere.rb +4 -0
  152. data/lib/principles/views/context_parts.rb +129 -25
  153. data/lib/principles/views/correction_request.rb +21 -16
  154. data/lib/principles/views/disposition_template.rb +35 -0
  155. data/lib/principles/views/first_audit.rb +29 -15
  156. data/lib/principles/views/first_audit_line.rb +4 -5
  157. data/lib/principles/views/invocation.rb +14 -5
  158. data/lib/principles/views/last_record_notice.rb +41 -0
  159. data/lib/principles/views/lines_shared_with_member.rb +16 -22
  160. data/lib/principles/views/logged_request_status_meanings.rb +29 -0
  161. data/lib/principles/views/logged_requests_evidence.rb +69 -0
  162. data/lib/principles/views/map_added_to.rb +14 -12
  163. data/lib/principles/views/map_result.rb +39 -17
  164. data/lib/principles/views/member_change_since_baseline.rb +4 -0
  165. data/lib/principles/views/member_row.rb +13 -4
  166. data/lib/principles/views/member_sets_overlap.rb +31 -0
  167. data/lib/principles/views/members_a_commit_changed.rb +42 -25
  168. data/lib/principles/views/members_changed_during_audit.rb +27 -0
  169. data/lib/principles/views/members_in_logged_requests_result.rb +68 -0
  170. data/lib/principles/views/members_too_short_to_scan.rb +45 -0
  171. data/lib/principles/views/members_with_no_baseline.rb +6 -6
  172. data/lib/principles/views/meta_principle_result.rb +14 -2
  173. data/lib/principles/views/open_findings_line.rb +31 -92
  174. data/lib/principles/views/parts_index_result.rb +9 -5
  175. data/lib/principles/views/principle_read_result.rb +52 -11
  176. data/lib/principles/views/prior_findings_result.rb +9 -1
  177. data/lib/principles/views/problem_facts.rb +5 -3
  178. data/lib/principles/views/problem_lines.rb +32 -6
  179. data/lib/principles/views/problem_result.rb +7 -15
  180. data/lib/principles/views/prompts/assess-every-member/claim-an-earlier-first-raised-at.md +6 -6
  181. data/lib/principles/views/prompts/assess-every-member/read-a-compact-carried-member.md +9 -8
  182. data/lib/principles/views/prompts/assess-every-member/read-a-flagged-member-s-text-whole.md +3 -0
  183. data/lib/principles/views/prompts/assess-every-member/read-a-judged-member-s-text-whole.md +3 -0
  184. data/lib/principles/views/prompts/assess-every-member/turn-on-request-logging-to-see-what-reaches-context.md +2 -0
  185. data/lib/principles/views/prompts/assess-every-member/weigh-what-reached-the-context.md +7 -0
  186. data/lib/principles/views/prompts/dispose-of-each-finding/answer-guide.json +1 -0
  187. data/lib/principles/views/prompts/dispose-of-each-finding/fix-or-record-each-finding.md +5 -0
  188. data/lib/principles/views/prompts/find-a-principle-to-map/read-a-cut-section-back.md +1 -1
  189. data/lib/principles/views/prompts/follow-each-carriers-best-practices/practices-for-hook-configuration.md +41 -0
  190. data/lib/principles/views/prompts/follow-each-carriers-best-practices/practices-for-hook-scripts.md +35 -0
  191. data/lib/principles/views/prompts/follow-each-carriers-best-practices/practices-for-instruction-files.md +37 -0
  192. data/lib/principles/views/prompts/follow-each-carriers-best-practices/practices-for-other-text-an-agent-reads.md +38 -0
  193. data/lib/principles/views/prompts/follow-each-carriers-best-practices/practices-for-rule-files.md +34 -0
  194. data/lib/principles/views/prompts/follow-each-carriers-best-practices/practices-for-skills.md +35 -0
  195. data/lib/principles/views/prompts/follow-each-carriers-best-practices/practices-for-subagent-definitions.md +35 -0
  196. data/lib/principles/views/prompts/learn-what-a-command-does/add-member.md +50 -51
  197. data/lib/principles/views/prompts/learn-what-a-command-does/change-member-role.md +2 -1
  198. data/lib/principles/views/prompts/learn-what-a-command-does/list-candidate-files.md +1 -1
  199. data/lib/principles/views/prompts/learn-what-a-command-does/list-member-problems.md +32 -32
  200. data/lib/principles/views/prompts/learn-what-a-command-does/list-open-findings.md +10 -11
  201. data/lib/principles/views/prompts/learn-what-a-command-does/move-member.md +7 -6
  202. data/lib/principles/views/prompts/learn-what-a-command-does/remove-logged-requests.md +16 -0
  203. data/lib/principles/views/prompts/learn-what-a-command-does/remove-member.md +2 -1
  204. data/lib/principles/views/prompts/learn-what-a-command-does/report-at-session-start.md +2 -2
  205. data/lib/principles/views/prompts/learn-what-a-command-does/show-audit.md +7 -0
  206. data/lib/principles/views/prompts/learn-what-a-command-does/show-candidate-outlines.md +8 -2
  207. data/lib/principles/views/prompts/learn-what-a-command-does/show-carrier-practices.md +13 -0
  208. data/lib/principles/views/prompts/learn-what-a-command-does/show-logged-requests.md +20 -0
  209. data/lib/principles/views/prompts/learn-what-a-command-does/show-members-in-logged-requests.md +48 -0
  210. data/lib/principles/views/prompts/learn-what-a-command-does/show-principle.md +14 -14
  211. data/lib/principles/views/prompts/learn-what-a-command-does/start-audit.md +27 -27
  212. data/lib/principles/views/prompts/learn-what-a-command-does/submit-answer.md +10 -10
  213. data/lib/principles/views/prompts/learn-what-a-command-does/turn-off-request-logging.md +10 -0
  214. data/lib/principles/views/prompts/learn-what-a-command-does/turn-on-request-logging.md +32 -0
  215. data/lib/principles/views/prompts/read-requested-outlines/continue-the-survey.md +3 -2
  216. data/lib/principles/views/prompts/read-what-reached-the-context/explain-request-logging-to-the-user.md +11 -0
  217. data/lib/principles/views/prompts/read-what-reached-the-context/see-whether-members-reach-the-agent.md +2 -0
  218. data/lib/principles/views/prompts/read-what-reached-the-context/what-absent-means.md +2 -0
  219. data/lib/principles/views/prompts/read-what-reached-the-context/what-not-a-carrier-means.md +2 -0
  220. data/lib/principles/views/prompts/read-what-reached-the-context/what-present-but-not-loaded-means.md +2 -0
  221. data/lib/principles/views/prompts/read-what-reached-the-context/what-present-means.md +1 -0
  222. data/lib/principles/views/prompts/read-what-reached-the-context/what-unmeasurable-means.md +2 -0
  223. data/lib/principles/views/prompts/report-after-a-commit/plugin-members-are-still-listed.md +1 -0
  224. data/lib/principles/views/prompts/report-after-a-commit/the-commits-changed-these-members-waiting-on-an-upgrade.md +2 -2
  225. data/lib/principles/views/prompts/report-after-a-commit/the-commits-changed-these-members.md +2 -2
  226. data/lib/principles/views/prompts/report-after-a-commit/this-command-shows-what-each-needs-before-the-next-commit.md +1 -0
  227. data/lib/principles/views/prompts/set-principles-up/entry-file-passage.md +1 -1
  228. data/lib/principles/views/prompts/start-a-session/act-on-the-open-findings.md +2 -1
  229. data/lib/principles/views/prompts/start-a-session/hand-the-first-audit-to-a-subagent.md +2 -3
  230. data/lib/principles/views/prompts/start-a-session/raise-the-open-findings-waiting-on-no-one.md +1 -0
  231. data/lib/principles/views/request_logging_result.rb +62 -0
  232. data/lib/principles/views/request_part.rb +10 -4
  233. data/lib/principles/views/request_parts.rb +9 -0
  234. data/lib/principles/views/whole_file_elsewhere.rb +30 -0
  235. metadata +101 -4
  236. data/lib/principles/views/prompts/report-after-a-commit/the-principles-skill-is-where-to-start.md +0 -1
  237. data/lib/principles/views/prompts/report-after-a-commit/this-command-shows-each-one.md +0 -1
@@ -0,0 +1,174 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "../error"
5
+ require_relative "../prose_list"
6
+ require_relative "../data/claude_code_home"
7
+ require_relative "plugin_install"
8
+ require_relative "plugin_component"
9
+ require_relative "plugin_name"
10
+ require_relative "installed_component"
11
+ require_relative "member_file"
12
+
13
+ module Principles
14
+ # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
15
+ module Models
16
+ # The plugins Claude Code installed that a session in the project loads, as the registry in its home records them;
17
+ # none while it has installed none.
18
+ class InstalledPlugins
19
+ # The most files a refusal names; it counts the rest.
20
+ LISTED = 20 #: Integer
21
+
22
+ # The marketplace kinds whose plugins Claude Code copies into the install folder its registry names, which is
23
+ # where a session reads them.
24
+ COPIED = %w[github git url].freeze #: Array[String]
25
+
26
+ #: (Data::ClaudeCodeHome, main_checkout: String) -> void
27
+ def initialize(claude_code, main_checkout:)
28
+ @claude_code = claude_code
29
+ @main_checkout = main_checkout
30
+ freeze
31
+ end
32
+
33
+ # The install of the plugin named that a session here loads, refused when there is none, naming those there
34
+ # are; when its marketplace is a local folder, whose session reads the folder rather than the install; and when
35
+ # its marketplace is of no kind COPIED names, since a session may read that from elsewhere too.
36
+ #: (String) -> PluginInstall
37
+ def install(name)
38
+ install = PluginInstall.applying(rows(name), main_checkout: @main_checkout) or refuse_not_installed(name)
39
+ kind = @claude_code.marketplaces&.source_kind(PluginName.new(name).marketplace)
40
+ refuse_directory_marketplace(name) if kind == "directory"
41
+ refuse_unknown_marketplace(name) unless COPIED.include?(kind)
42
+ install.version.to_s.empty? ? refuse_unversioned(name) : install
43
+ end
44
+
45
+ # The file component names in the plugin named, with its text, refused when it is reached through a symbolic
46
+ # link, which this tool never follows; when the plugin holds no such file, naming those it does; and when its
47
+ # text is not UTF-8 or larger than this tool reads.
48
+ #: (String, PluginComponent) -> InstalledComponent
49
+ def holding(name, component)
50
+ install = install(name)
51
+ folder = @claude_code.plugin_folder(install.install_path.to_s)
52
+ refuse_linked(name, component) if folder.linked?(component.path)
53
+ refuse_not_held(name, folder.files) unless folder.file?(component.path)
54
+ InstalledComponent.new(PluginReference.new(name, component), install, folder.read(component.path))
55
+ end
56
+
57
+ # Whether the plugin installed now holds the file reference names, as holding finds it.
58
+ #: (PluginReference) -> bool
59
+ def holds?(reference)
60
+ holding(reference.name, reference.component)
61
+ true
62
+ rescue Error
63
+ false
64
+ end
65
+
66
+ # Where the file reference names comes from, at the version installed now, refused as install refuses.
67
+ #: (PluginReference) -> Hash[String, untyped]
68
+ def source(reference) = reference.source(install(reference.name).version)
69
+
70
+ # Where member comes from when a plugin gives it, as source says; nil for a member in a project file.
71
+ #: (Member) -> Hash[String, untyped]?
72
+ def source_of(member)
73
+ reference = member.plugin_reference or return
74
+ source(reference)
75
+ end
76
+
77
+ # The file a mapped plugin member's reference names, as the type that finds the member in it, refused as
78
+ # holding refuses it.
79
+ #: (PluginReference) -> MemberFile
80
+ def member_file(reference)
81
+ MemberFile.for(path: reference.to_s, text: holding(reference.name, reference.component).text)
82
+ end
83
+
84
+ # The install names of the plugins a session here loads, sorted.
85
+ #: () -> Array[String]
86
+ def names
87
+ (@claude_code.registry&.names || []).select do |name|
88
+ PluginInstall.applying(rows(name), main_checkout: @main_checkout)
89
+ end
90
+ end
91
+
92
+ private
93
+
94
+ #: (String) -> Array[Hash[String, String?]]
95
+ def rows(name) = @claude_code.registry&.rows(name) || []
96
+
97
+ #: (String, Array[String]) -> bot
98
+ def refuse_not_held(name, files)
99
+ plain, skipped = files.partition { |path| PluginComponent.plain?(path) }
100
+ listed = plain.first(LISTED) + (plain.size > LISTED ? ["#{plain.size - LISTED} more"] : [])
101
+ repair = if listed.empty?
102
+ "#{name} holds no file this tool can map.#{left_out(skipped, "")} Map the project's own files " \
103
+ "instead."
104
+ else
105
+ "Name one of the files #{name} holds: #{ProseList.new(listed)}.#{left_out(skipped, " more")}"
106
+ end
107
+ raise Error.new("plugin_component_not_found", "#{name} holds no such file", repair)
108
+ end
109
+
110
+ # A sentence, after a space, counting the files skipped that a refusal leaves out for their paths, as more
111
+ # when it lists others; nothing when there are none.
112
+ #: (Array[String], String) -> String
113
+ def left_out(skipped, more)
114
+ return "" if skipped.empty?
115
+
116
+ " #{skipped.size}#{more} #{skipped.one? ? "is" : "are"} left out, since a member's path takes only plain " \
117
+ "characters."
118
+ end
119
+
120
+ #: (String, PluginComponent) -> bot
121
+ def refuse_linked(name, component)
122
+ raise Error.new("plugin_component_linked",
123
+ "#{component.path} in #{name} is reached through a symbolic link, which this tool does not " \
124
+ "follow, since it could lead out of the plugin",
125
+ "Name the file the link leads to by its own path inside the plugin, or map the project's own " \
126
+ "files instead.")
127
+ end
128
+
129
+ #: (String) -> bot
130
+ def refuse_directory_marketplace(name)
131
+ raise Error.new("plugin_from_directory_marketplace", "#{name} comes from a marketplace in a local folder",
132
+ "Map the project's own files instead; a plugin from a marketplace in a local folder is " \
133
+ "not supported yet.")
134
+ end
135
+
136
+ # A probe saw the host record a version in every row, a commit's first digits when the plugin names none.
137
+ #: (String) -> bot
138
+ def refuse_unversioned(name)
139
+ raise Error.new("unreadable_plugin_registry", "Claude Code's plugin registry records no version for #{name}",
140
+ "Reinstall the plugin with Claude Code, or map the project's own files instead.")
141
+ end
142
+
143
+ #: (String) -> bot
144
+ def refuse_unknown_marketplace(name)
145
+ raise Error.new("plugin_marketplace_unknown",
146
+ "Claude Code records no marketplace kind this tool reads for #{name}",
147
+ "Map the project's own files instead; this tool reads a plugin only from a marketplace " \
148
+ "Claude Code records as #{ProseList.new(COPIED, conjunction: "or")}.")
149
+ end
150
+
151
+ #: (String) -> bot
152
+ def refuse_not_installed(name)
153
+ here = names
154
+ choices = if here.empty?
155
+ "No plugin is installed for this project. Install #{name} with Claude Code first."
156
+ else
157
+ "Installed for this project: #{ProseList.new(here)}. Name one of them with --plugin, or install " \
158
+ "#{name} with Claude Code first."
159
+ end
160
+ repair = elsewhere(name) + choices
161
+ raise Error.new("plugin_not_installed", "#{name} is not installed for this project", repair)
162
+ end
163
+
164
+ # Why name's rows, when it has some, do not apply here: each installs it for another project.
165
+ #: (String) -> String
166
+ def elsewhere(name)
167
+ return "" if rows(name).empty?
168
+
169
+ "#{name} is installed only for another project, and only a user-scope install is read outside its own " \
170
+ "project. "
171
+ end
172
+ end
173
+ end
174
+ end
@@ -0,0 +1,34 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "../error"
5
+
6
+ module Principles
7
+ # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
8
+ module Models
9
+ # How many of the newest captured sessions remove-logged-requests keeps, as --keep-sessions gives it: a whole
10
+ # number, 0 keeping none, or 5 when it is not given.
11
+ class KeptSessions
12
+ DEFAULT = 5
13
+
14
+ #: Integer
15
+ attr_reader :count
16
+
17
+ #: (String?) -> void
18
+ def initialize(given)
19
+ @count = given ? whole(given) : DEFAULT #: Integer
20
+ freeze
21
+ end
22
+
23
+ private
24
+
25
+ #: (String) -> Integer
26
+ def whole(given)
27
+ return given.to_i if given.match?(/\A\d+\z/)
28
+
29
+ raise Error.new("invalid_keep_sessions", "--keep-sessions takes a whole number, not #{given.inspect}",
30
+ "Give how many of the newest sessions to keep, such as --keep-sessions 5; 0 keeps none.")
31
+ end
32
+ end
33
+ end
34
+ end
@@ -50,6 +50,14 @@ module Principles
50
50
  !heading.nil? && starting_at.nil? && ending_at.nil? && query.nil?
51
51
  end
52
52
 
53
+ # This locator with its passage's opening quoted without its leading whitespace; nil when the opening has none.
54
+ #: () -> Locator?
55
+ def with_opening_unindented
56
+ opening = starting_at or return nil
57
+ unindented = opening.lstrip
58
+ Locator.new(@fields.merge("starting_at" => unindented)) unless unindented == opening
59
+ end
60
+
53
61
  # The locator as it was given.
54
62
  #: () -> Hash[String, untyped]
55
63
  def to_h = @fields
@@ -0,0 +1,44 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "member_reach"
5
+
6
+ module Principles
7
+ # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
8
+ module Models
9
+ # How far each member's text reached, compactly, from where the scan of the captured requests saw it: how many of
10
+ # the scanned requests held its lines, and the most bytes of them one request held. A full audit's request prints
11
+ # it, so the agent judging a member weighs whether it reaches the agent and what it costs there.
12
+ class LoggedRequestReach
13
+ # members is each member as the scan reports it, with its status and the places it was seen; scanned is how many
14
+ # requests the scan read whole, and unreadable how many it could not, since what they held is unknown; carried
15
+ # names each member a full audit carries forward, which is listed too, marked, since one that never loads still
16
+ # fails its principle.
17
+ #: (Array[Hash[String, untyped]], scanned: Integer, ?unreadable: Integer, ?carried: Array[String]) -> void
18
+ def initialize(members, scanned:, unreadable: 0, carried: [])
19
+ @members = members
20
+ @carried = carried
21
+ @scanned = scanned
22
+ @unreadable = unreadable
23
+ freeze
24
+ end
25
+
26
+ # The requests scanned, any it could not read, and each member's status, the requests seen holding its lines and
27
+ # its most bytes in one, and whether the audit carries it forward.
28
+ #: () -> Hash[String, untyped]
29
+ def to_h
30
+ { "requests" => @scanned, **({ "unreadable" => @unreadable } if @unreadable.positive?),
31
+ "members" => @members.map { reach(it) } }
32
+ end
33
+
34
+ private
35
+
36
+ # member's reach, marked when the audit carries it forward.
37
+ #: (Hash[String, untyped]) -> Hash[String, untyped]
38
+ def reach(member)
39
+ reach = MemberReach.new(member).to_h
40
+ @carried.include?(member.fetch("member")) ? reach.merge("carried_forward" => true) : reach
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,43 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "../data/logged_request_scan"
5
+
6
+ module Principles
7
+ # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
8
+ module Models
9
+ # How much of the captured requests one scan read: how many there were, how many strings it decoded, and whether
10
+ # it read every one through. A member reported absent is proven absent only when it did, so each request it did
11
+ # not decode to its end is named, beside those it could not read at all.
12
+ class LoggedRequestScanCompleteness
13
+ # What scan read in its run matched.
14
+ #: (Data::LoggedRequestScan, Data::LoggedRequestMatches) -> LoggedRequestScanCompleteness
15
+ def self.of(scan, matched)
16
+ new(requests: scan.requests.map { it.fetch("file").to_s }, unreadable: scan.unreadable, matched:)
17
+ end
18
+
19
+ #: (requests: Array[String], unreadable: Array[Hash[String, String]],
20
+ #| matched: Data::LoggedRequestMatches) -> void
21
+ def initialize(requests:, unreadable:, matched:)
22
+ @requests = requests
23
+ @matched = matched
24
+ undecoded = requests - unreadable.map { it.fetch("request") } - matched.leaves.keys
25
+ named = undecoded.map { { "request" => it, "reason" => "not_decoded" } }
26
+ @unreadable = [*unreadable, *named].freeze #: Array[Hash[String, String]]
27
+ freeze
28
+ end
29
+
30
+ # Each request the scan could not read through, by its request and reason.
31
+ #: Array[Hash[String, String]]
32
+ attr_reader :unreadable
33
+
34
+ # The requests captured, those it could not read too, the strings decoded in them, and whether the scan read
35
+ # every one through.
36
+ #: () -> Hash[String, untyped]
37
+ def to_h
38
+ { "captured" => @requests.size, "leaves" => @matched.leaves.values.sum,
39
+ "complete" => @unreadable.empty? && @matched.whole }
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,46 @@
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
+ # What this tool knew of a member's text when it was mapped, which no audit has assessed: its hash at the commit
8
+ # that mapped it where it is, or, for a plugin member, which no commit holds, the hash its map pinned.
9
+ class MappedBaseline
10
+ # What "since" names each kind as, every one a baseline no audit gave, so a member compared with one alone has
11
+ # never been assessed.
12
+ KINDS = %w[mapping_commit pinned].freeze #: Array[String]
13
+
14
+ # The member's hash at the commit that mapped it where it is, from which its automatic history walks.
15
+ #: (String, String) -> MappedBaseline
16
+ def self.at_commit(sha256, commit)
17
+ new(sha256, { "baseline" => "mapping_commit", "commit" => commit }, walks_history: true)
18
+ end
19
+
20
+ # A plugin member's pin, its version and hash when add-member read it, which git holds no history of.
21
+ #: (Hash[String, String]) -> MappedBaseline
22
+ def self.pinned(pin)
23
+ new(pin.fetch("sha256"), { "baseline" => "pinned", "version" => pin.fetch("version") }, walks_history: false)
24
+ end
25
+
26
+ #: String
27
+ attr_reader :sha256
28
+
29
+ # What the baseline was, as a changed member's "since" says.
30
+ #: Hash[String, String]
31
+ attr_reader :since
32
+
33
+ #: (String, Hash[String, String], walks_history: bool) -> void
34
+ def initialize(sha256, since, walks_history:)
35
+ @sha256 = sha256
36
+ @since = since.freeze #: Hash[String, String]
37
+ @walks_history = walks_history
38
+ freeze
39
+ end
40
+
41
+ # Whether the member's history in git walks back to it.
42
+ #: () -> bool
43
+ def walks_history? = @walks_history
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,42 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "../error"
5
+ require_relative "member"
6
+
7
+ module Principles
8
+ # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
9
+ module Models
10
+ # The other maps holding a new whole-file member's file whole too, by its own path or through a link, so a hub
11
+ # file mapped whole into several principles is seen as it is mapped.
12
+ class MapsHoldingFileWhole
13
+ #: (Project, Member) -> void
14
+ def initialize(project, member)
15
+ @project = project
16
+ @member = member
17
+ freeze
18
+ end
19
+
20
+ # The ids of the maps other than except holding the file whole; none when the member is a passage. A map that
21
+ # cannot be read is left out, since list-member-problems reports it.
22
+ #: (except: String) -> Array[String]
23
+ def elsewhere(except:)
24
+ return [] unless @member.whole_file?
25
+
26
+ maps = @project.principles
27
+ (maps.ids - [except]).select { |id| holds_whole?(maps, id) }
28
+ end
29
+
30
+ private
31
+
32
+ #: (MappedPrinciples, String) -> bool
33
+ def holds_whole?(maps, id)
34
+ maps.find(id).members.to_a.any? do |member|
35
+ member.whole_file? && @project.same_file?(member.path, @member.path)
36
+ end
37
+ rescue Error
38
+ false
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,143 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "../error"
5
+ require_relative "frontmatter_fields"
6
+ require_relative "outline_section"
7
+ require_relative "text_within"
8
+
9
+ module Principles
10
+ # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
11
+ module Models
12
+ # A Markdown file's text as its outline reads it: its sections, each a heading and the top-level list items under
13
+ # it, its frontmatter description and its opening line. One user scripted a heading grep over 45 files to survey
14
+ # them, and missed a principles section written as one numbered list, so the outline reads both.
15
+ class MarkdownText
16
+ HEADING = /\A#+\s/
17
+ FENCE = /\A\s*(?:```|~~~)/
18
+ # A top-level list item, its marker and its text; an indented one is nested, and left out.
19
+ ITEM = /\A(\d+\.|[-*])\s+(.*)/
20
+ # A horizontal rule, such as `* * *` or `- - -`, which is no item.
21
+ RULE = /\A([-*_])(?:\s*\1){2,}\s*\z/
22
+ LINK = /\[([^\]]*)\]\([^)]*\)/
23
+ BOLD_LEAD = /\A\*\*[^*]+\*\*/
24
+ # The most characters of the opening line printed, cut back to a sentence end, or else to a whole word. A cut
25
+ # mid-clause left 30 of one user's 45 opening lines a fragment. The line is short only when
26
+ # its first sentence is and the next runs past the cap, or an abbreviation such as "e.g." ends it early; that is
27
+ # accepted, since the file's path and outline still say what it holds.
28
+ OPENING_CHARACTERS = 160
29
+ # The most characters of a description a cut keeps, cut back to a sentence end, or else to a whole word. The
30
+ # one user's descriptions printed 12,537 B uncut, and suggested about 300.
31
+ DESCRIPTION_CHARACTERS = 300
32
+
33
+ # A description's key and any block scalar indicator, which its text starts after.
34
+ DESCRIPTION_KEY = /\Adescription:\s*(?:[>|][-+]?\s)?/
35
+ # A value inside a matching pair of quotes, printed without them; its escapes are left as written.
36
+ QUOTED = /\A(["'])(.*)\1\z/
37
+
38
+ #: (String) -> void
39
+ def initialize(text)
40
+ @lines = text.lines #: Array[String]
41
+ @frontmatter = FrontmatterFields.new(path: "", lines: @lines) #: FrontmatterFields
42
+ freeze
43
+ end
44
+
45
+ # Each heading with the items under it, and the items before the first heading when there are any, each with
46
+ # the lines of the file it spans.
47
+ #: () -> Array[OutlineSection]
48
+ def sections
49
+ body = unfenced
50
+ starts = [0, *body.each_index.select { |index| body.fetch(index).match?(HEADING) }].uniq
51
+ starts.zip(starts.drop(1)).filter_map do |first, after|
52
+ section(body[first...(after || body.length)] || [], first_line: body_start + first + 1)
53
+ end
54
+ end
55
+
56
+ # The frontmatter's description as one line of text; nil without one, or where this tool refuses to read it.
57
+ #: () -> String?
58
+ def description
59
+ written = @frontmatter.field_lines("$.description") or return nil
60
+ text = written.map(&:strip).join(" ").sub(DESCRIPTION_KEY, "").strip
61
+ text[QUOTED, 2] || text
62
+ rescue Error
63
+ nil
64
+ end
65
+
66
+ # The description, past DESCRIPTION_CHARACTERS cut to its last sentence end within them, saying how many more
67
+ # characters it holds and the lines of the file that hold it, so the agent reads just those back.
68
+ #: () -> String?
69
+ def cut_description
70
+ text = description or return nil
71
+ within = TextWithin.new(text, at_most: DESCRIPTION_CHARACTERS)
72
+ return text unless within.cut?
73
+
74
+ kept = within.kept
75
+ first, last = @frontmatter.field("$.description")
76
+ "#{kept} (#{text.length - kept.length} more characters, lines #{first + 1}-#{last})"
77
+ end
78
+
79
+ # The first prose paragraph of the body, outside headings, lists, tables and code, its hard-wrapped lines
80
+ # joined, with links as their text. The first physical line was a fragment in a hard-wrapped file.
81
+ #: () -> String?
82
+ def opening_line
83
+ body = unfenced
84
+ first = body.index { |line| prose?(line) } or return nil
85
+ paragraph = body.drop(first).take_while { |line| prose?(line) }.map(&:strip).join(" ")
86
+ TextWithin.new(paragraph.gsub(LINK, '\1'), at_most: OPENING_CHARACTERS).by_sentences
87
+ end
88
+
89
+ private
90
+
91
+ # The section these body lines hold, the first of them at first_line in the file; nil when they hold neither a
92
+ # heading nor an item.
93
+ #: (Array[String], first_line: Integer) -> OutlineSection?
94
+ def section(lines, first_line:)
95
+ heading = lines.first if lines.first&.match?(HEADING)
96
+ items = outline_items(lines, first_line:)
97
+ return nil unless heading || items.any?
98
+
99
+ OutlineSection.new(heading:, items:, first_line:, last_line: first_line + lines.length - 1)
100
+ end
101
+
102
+ # The top-level list items among these body lines, each on its line of the file, the first of them first_line.
103
+ #: (Array[String], first_line: Integer) -> Array[OutlineItem]
104
+ def outline_items(lines, first_line:)
105
+ lines.each_index.select { |index| item?(lines.fetch(index)) }
106
+ .map { |index| OutlineItem.new(item(lines.fetch(index)), line: first_line + index) }
107
+ end
108
+
109
+ # A top-level list item, not a horizontal rule written with the same marks.
110
+ #: (String) -> bool
111
+ def item?(line) = line.match?(ITEM) && !line.match?(RULE)
112
+
113
+ # The index in the file of the body's first line, after any frontmatter.
114
+ #: () -> Integer
115
+ def body_start = (@frontmatter.closing_line || -1) + 1
116
+
117
+ #: (String) -> bool
118
+ def prose?(line)
119
+ text = line.strip
120
+ !text.empty? && !text.match?(HEADING) && !text.match?(ITEM) && !text.match?(RULE) && !text.start_with?("|")
121
+ end
122
+
123
+ # The body's lines, after any frontmatter, each line of a code fence blank, so a fence ends a paragraph.
124
+ #: () -> Array[String]
125
+ def unfenced
126
+ fenced = false #: bool
127
+ body = @lines.drop(body_start).map(&:chomp)
128
+ body.map do |line|
129
+ fenced = !fenced if line.match?(FENCE)
130
+ fenced || line.match?(FENCE) ? "" : line
131
+ end
132
+ end
133
+
134
+ # An item by its marker and its bold lead, or its opening words, with links as their text.
135
+ #: (String) -> String
136
+ def item(line)
137
+ marker, text = line.match(ITEM)&.captures
138
+ text = text.to_s.gsub(LINK, '\1')
139
+ "#{marker} #{text[BOLD_LEAD] || TextWithin.new(text, at_most: OutlineSection::ITEM_CHARACTERS).by_words}"
140
+ end
141
+ end
142
+ end
143
+ end
@@ -4,6 +4,7 @@
4
4
  require_relative "../error"
5
5
  require_relative "locator"
6
6
  require_relative "meta_principle"
7
+ require_relative "plugin_reference"
7
8
  require_relative "report_hooks"
8
9
 
9
10
  module Principles
@@ -31,7 +32,8 @@ module Principles
31
32
  # A new member field that changes what an audit asks or records gets the next format, and
32
33
  # Data::MapFiles::NEWEST_FORMAT moves with it.
33
34
  FIELD_FORMATS = { "path" => 1, "section" => 1, "starting_at" => 1, "ending_at" => 1, "role" => 1,
34
- "key" => 1, "check" => 2, "query" => 2 }.freeze #: Hash[String, Integer]
35
+ "key" => 1, "check" => 2, "query" => 2,
36
+ "pinned" => 4 }.freeze #: Hash[String, Integer]
35
37
 
36
38
  # The fields that say where a member's text is; a move replaces them whole.
37
39
  LOCATORS = %w[path section starting_at ending_at query].freeze #: [String, String, String, String, String]
@@ -111,6 +113,17 @@ module Principles
111
113
  @fields["role"]
112
114
  end
113
115
 
116
+ # What add-member read of a plugin's file when it mapped it, its version and the hash of its text, which no
117
+ # commit holds; nil for a project file.
118
+ #: () -> Hash[String, String]?
119
+ def pinned
120
+ @fields["pinned"]
121
+ end
122
+
123
+ # The installed plugin's file the member is in, which git holds no history of; nil for a project file.
124
+ #: () -> PluginReference?
125
+ def plugin_reference = PluginReference.in(path)
126
+
114
127
  # The command that enforces the member, which an audit asks the agent to run; nil when it has none.
115
128
  #: () -> String?
116
129
  def check
@@ -0,0 +1,56 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "locate_refusal"
5
+ require_relative "mapped_baseline"
6
+
7
+ module Principles
8
+ # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
9
+ module Models
10
+ # One member as list-member-problems lists it: its principle, id and state against what this tool last knew of
11
+ # its text, with what a changed member was compared with, or, when it cannot be found, its refusal.
12
+ class MemberEntry
13
+ # now is the member's hash now, or the refusal that says why it cannot be found. source is where a plugin
14
+ # member found now comes from, at the version installed now.
15
+ #: (String, Member, untyped, MemberProblems::Baselines, ?source: Hash[String, untyped]?) -> void
16
+ def initialize(principle_id, member, now, baselines, source: nil)
17
+ @principle_id = principle_id
18
+ @member = member
19
+ @now = now #: untyped
20
+ @baselines = baselines
21
+ @source = source
22
+ freeze
23
+ end
24
+
25
+ #: () -> Hash[String, untyped]
26
+ def to_h
27
+ listed = { "principle" => @principle_id, "id" => @member.id }
28
+ now = @now
29
+ return compared(listed, now.to_s).merge(@source ? { "source" => @source } : {}) unless now.is_a?(Hash)
30
+ # A plugin member found in no installed plugin, for any reason, is one quiet state, not broken, so a teammate
31
+ # without the plugin, or with it in a shape this tool does not read, keeps every other member and map.
32
+ return listed.merge("state" => "plugin_unresolved", "reason" => now.fetch("code")) if @member.plugin_reference
33
+
34
+ listed.merge("state" => LocateRefusal.new(now).member_state).merge(now.except("status"))
35
+ end
36
+
37
+ private
38
+
39
+ # listed, compared with the last audit's hash for the member, or else with its hash at the commit that mapped
40
+ # it. An unchanged member compared only with the commit that mapped it has never been assessed, so it is
41
+ # counted apart from one an audit assessed: counts is read first, and "unchanged" alone reads as audited.
42
+ #: (Hash[String, untyped], String) -> Hash[String, untyped]
43
+ def compared(listed, sha)
44
+ then_sha, since = @baselines.of(@member.key)
45
+ return listed.merge("state" => "no_baseline") unless since
46
+ unless then_sha == sha
47
+ return listed.merge("state" => "changed", "since" => since,
48
+ **(@baselines.waiting? ? { "waiting_on_an_upgrade" => true } : {}))
49
+ end
50
+
51
+ assessed = !MappedBaseline::KINDS.include?(since["baseline"])
52
+ listed.merge("state" => assessed ? "unchanged" : "unchanged_since_mapping")
53
+ end
54
+ end
55
+ end
56
+ end
@@ -64,6 +64,17 @@ module Principles
64
64
  { **regions.shown, "lines" => counts }
65
65
  end
66
66
 
67
+ # Its changed regions with context unchanged lines around each, as a full audit's request shows a whole file
68
+ # whose text changed beside the text it reads apart; left out once both sides' lines pass half the bytes of
69
+ # its text now, since the text, read whole, then costs less than the regions beside it.
70
+ #: (context: Integer) -> Hash[String, untyped]
71
+ def regions(context:)
72
+ found = ChangedRegions.new(@at_baseline, @now, hunks: LineDiff.new(@at_baseline, @now).hunks, context:)
73
+ return { "omitted" => "over_half_the_text" } if 2 * bytes(found.lines) > @now.sum(&:bytesize)
74
+
75
+ found.shown
76
+ end
77
+
67
78
  private
68
79
 
69
80
  # How many bytes the sides' lines hold together.