principles 0.1.3 → 0.1.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 (312) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +151 -1
  3. data/README.md +10 -6
  4. data/exe/principles +1 -1
  5. data/lib/principles/cli.rb +9 -10
  6. data/lib/principles/command_line.rb +15 -6
  7. data/lib/principles/command_routes.rb +18 -2
  8. data/lib/principles/controllers/acknowledgement.rb +19 -4
  9. data/lib/principles/controllers/acknowledgements.rb +43 -12
  10. data/lib/principles/controllers/after_commit_report.rb +15 -2
  11. data/lib/principles/controllers/assessment.rb +7 -3
  12. data/lib/principles/controllers/audit.rb +17 -8
  13. data/lib/principles/controllers/audit_completion.rb +34 -9
  14. data/lib/principles/controllers/carrier_edit_report.rb +40 -0
  15. data/lib/principles/controllers/carrier_practices.rb +26 -0
  16. data/lib/principles/controllers/command.rb +36 -5
  17. data/lib/principles/controllers/context_budget.rb +61 -0
  18. data/lib/principles/controllers/correction.rb +15 -5
  19. data/lib/principles/controllers/full_audit_request.rb +38 -42
  20. data/lib/principles/controllers/inventory.rb +78 -71
  21. data/lib/principles/controllers/last_record_notice_beside.rb +51 -0
  22. data/lib/principles/controllers/member_addition.rb +20 -5
  23. data/lib/principles/controllers/member_entries.rb +50 -39
  24. data/lib/principles/controllers/member_entry_changes.rb +71 -0
  25. data/lib/principles/controllers/member_problems_report.rb +26 -6
  26. data/lib/principles/controllers/members_a_commit_changed.rb +14 -5
  27. data/lib/principles/controllers/members_in_logged_requests.rb +52 -0
  28. data/lib/principles/controllers/members_to_assess.rb +35 -8
  29. data/lib/principles/controllers/meta_principle_setup.rb +12 -4
  30. data/lib/principles/controllers/open_correction.rb +3 -2
  31. data/lib/principles/controllers/parts_to_read.rb +48 -6
  32. data/lib/principles/controllers/plugin_member_addition.rb +48 -0
  33. data/lib/principles/controllers/principle_listing.rb +84 -0
  34. data/lib/principles/controllers/request_logging.rb +65 -0
  35. data/lib/principles/controllers/running_tool.rb +55 -15
  36. data/lib/principles/controllers/session_start_report.rb +26 -2
  37. data/lib/principles/data/branches_bringing_in.rb +52 -0
  38. data/lib/principles/data/captured_requests.rb +118 -0
  39. data/lib/principles/data/carrier_page_markers.rb +38 -0
  40. data/lib/principles/data/changes_since.rb +56 -0
  41. data/lib/principles/data/claude_code_home.rb +75 -0
  42. data/lib/principles/data/command-steps.json +139 -23
  43. data/lib/principles/data/decoded_leaf.rb +63 -0
  44. data/lib/principles/data/head_reflog.rb +13 -2
  45. data/lib/principles/data/kept_line.rb +39 -0
  46. data/lib/principles/data/known_marketplaces.rb +50 -0
  47. data/lib/principles/data/logged_request_matches.rb +144 -0
  48. data/lib/principles/data/logged_request_scan.rb +188 -0
  49. data/lib/principles/data/map_files.rb +18 -4
  50. data/lib/principles/data/plugin_folder.rb +85 -0
  51. data/lib/principles/data/plugin_registry.rb +73 -0
  52. data/lib/principles/data/principles_store.rb +49 -0
  53. data/lib/principles/data/project_files.rb +15 -0
  54. data/lib/principles/data/ran.rb +25 -0
  55. data/lib/principles/data/raw_request_filter.rb +61 -0
  56. data/lib/principles/data/repository.rb +29 -27
  57. data/lib/principles/data/request_logging_folder.rb +135 -0
  58. data/lib/principles/data/request_shape.rb +33 -0
  59. data/lib/principles/data/running_version.rb +1 -1
  60. data/lib/principles/data/schemas/acknowledgement-answer.schema.json +16 -1
  61. data/lib/principles/data/schemas/acknowledgements-answer.schema.json +16 -1
  62. data/lib/principles/data/schemas/assessment-answer.schema.json +8 -0
  63. data/lib/principles/data/schemas/command-steps.schema.json +1 -1
  64. data/lib/principles/data/schemas/correction-answer-guide.schema.json +2 -1
  65. data/lib/principles/data/schemas/correction-answer.schema.json +14 -0
  66. data/lib/principles/data/schemas/member-map.schema.json +12 -1
  67. data/lib/principles/data/settings_env_keys.rb +26 -0
  68. data/lib/principles/data/string_counts.rb +39 -0
  69. data/lib/principles/data/tools_on_path.rb +25 -0
  70. data/lib/principles/data/user_request_logging_folder.rb +73 -0
  71. data/lib/principles/invocation_options.rb +5 -3
  72. data/lib/principles/launch.rb +29 -10
  73. data/lib/principles/models/acknowledged_check.rb +11 -1
  74. data/lib/principles/models/acknowledgement.rb +5 -0
  75. data/lib/principles/models/acknowledgement_refusal.rb +20 -11
  76. data/lib/principles/models/acknowledgements_to_offer.rb +86 -0
  77. data/lib/principles/models/after_commit_input.rb +5 -0
  78. data/lib/principles/models/answer_fields.rb +3 -0
  79. data/lib/principles/models/assessment_basis.rb +10 -3
  80. data/lib/principles/models/audit.rb +10 -10
  81. data/lib/principles/models/audit_record.rb +10 -0
  82. data/lib/principles/models/audit_records.rb +4 -3
  83. data/lib/principles/models/audit_start.rb +9 -0
  84. data/lib/principles/models/authored_locator.rb +33 -0
  85. data/lib/principles/models/candidate_files.rb +23 -3
  86. data/lib/principles/models/candidate_outline.rb +18 -125
  87. data/lib/principles/models/captured_sessions.rb +166 -0
  88. data/lib/principles/models/carrier_edit_input.rb +56 -0
  89. data/lib/principles/models/carrier_kind.rb +79 -0
  90. data/lib/principles/models/carrier_pages_shown.rb +28 -0
  91. data/lib/principles/models/carved_message_bytes.rb +115 -0
  92. data/lib/principles/models/cited_files.rb +12 -3
  93. data/lib/principles/models/context_parts.rb +59 -0
  94. data/lib/principles/models/decided_prior_findings.rb +5 -0
  95. data/lib/principles/models/declared_map_changes.rb +22 -13
  96. data/lib/principles/models/disposition.rb +37 -6
  97. data/lib/principles/models/disposition_placeholders.rb +12 -0
  98. data/lib/principles/models/disposition_text_field.rb +4 -0
  99. data/lib/principles/models/dropped_thinking.rb +73 -0
  100. data/lib/principles/models/feature.rb +54 -0
  101. data/lib/principles/models/finding_outcomes.rb +55 -0
  102. data/lib/principles/models/findings_left_open.rb +9 -11
  103. data/lib/principles/models/head_moves.rb +27 -10
  104. data/lib/principles/models/installed_component.rb +55 -0
  105. data/lib/principles/models/installed_plugins.rb +174 -0
  106. data/lib/principles/models/kept_sessions.rb +34 -0
  107. data/lib/principles/models/locator.rb +8 -0
  108. data/lib/principles/models/logged_request_reach.rb +52 -0
  109. data/lib/principles/models/logged_request_scan_completeness.rb +43 -0
  110. data/lib/principles/models/map_format_raise.rb +28 -0
  111. data/lib/principles/models/mapped_baseline.rb +46 -0
  112. data/lib/principles/models/mapped_principles.rb +2 -1
  113. data/lib/principles/models/maps_holding_file_whole.rb +42 -0
  114. data/lib/principles/models/markdown_text.rb +143 -0
  115. data/lib/principles/models/member.rb +19 -1
  116. data/lib/principles/models/member_entry.rb +56 -0
  117. data/lib/principles/models/member_justification.rb +52 -0
  118. data/lib/principles/models/member_lines.rb +11 -0
  119. data/lib/principles/models/member_loads.rb +17 -2
  120. data/lib/principles/models/member_problems.rb +46 -42
  121. data/lib/principles/models/member_reach.rb +59 -0
  122. data/lib/principles/models/member_snapshots.rb +34 -4
  123. data/lib/principles/models/member_text_as_sent.rb +31 -0
  124. data/lib/principles/models/member_texts.rb +45 -0
  125. data/lib/principles/models/members_by_part.rb +67 -0
  126. data/lib/principles/models/members_in_logged_requests.rb +279 -0
  127. data/lib/principles/models/meta_principle.rb +4 -4
  128. data/lib/principles/models/missing_files.rb +41 -16
  129. data/lib/principles/models/nearly_matching_maps.rb +56 -0
  130. data/lib/principles/models/open_correction.rb +5 -0
  131. data/lib/principles/models/open_finding.rb +4 -3
  132. data/lib/principles/models/outline_budget.rb +10 -1
  133. data/lib/principles/models/outline_item.rb +25 -0
  134. data/lib/principles/models/outline_section.rb +15 -4
  135. data/lib/principles/models/part_named.rb +16 -2
  136. data/lib/principles/models/part_token_estimates.rb +60 -0
  137. data/lib/principles/models/plugin_component.rb +37 -0
  138. data/lib/principles/models/plugin_enabled_in_project_settings.rb +61 -0
  139. data/lib/principles/models/plugin_install.rb +33 -0
  140. data/lib/principles/models/plugin_name.rb +23 -0
  141. data/lib/principles/models/plugin_reference.rb +53 -0
  142. data/lib/principles/models/principle.rb +7 -4
  143. data/lib/principles/models/principle_members.rb +22 -0
  144. data/lib/principles/models/prior_finding_decision.rb +1 -1
  145. data/lib/principles/models/prior_finding_outcomes.rb +12 -1
  146. data/lib/principles/models/problem_report.rb +14 -25
  147. data/lib/principles/models/project.rb +51 -56
  148. data/lib/principles/models/project_harness.rb +48 -0
  149. data/lib/principles/models/project_history.rb +19 -5
  150. data/lib/principles/models/recorded_members.rb +7 -0
  151. data/lib/principles/models/report_hooks.rb +9 -4
  152. data/lib/principles/models/request_channel.rb +62 -0
  153. data/lib/principles/models/request_kind.rb +62 -0
  154. data/lib/principles/models/request_logging.rb +231 -0
  155. data/lib/principles/models/request_logging_guard.rb +84 -0
  156. data/lib/principles/models/request_time.rb +46 -0
  157. data/lib/principles/models/runs_from.rb +0 -5
  158. data/lib/principles/models/script_comments.rb +58 -0
  159. data/lib/principles/models/session_registry.rb +61 -0
  160. data/lib/principles/models/session_start_input.rb +11 -2
  161. data/lib/principles/models/session_trend.rb +36 -0
  162. data/lib/principles/models/stale_request.rb +9 -0
  163. data/lib/principles/models/turn_opening.rb +83 -0
  164. data/lib/principles/models/turn_position.rb +43 -0
  165. data/lib/principles/models/unfixed_part.rb +82 -0
  166. data/lib/principles/models/unresolved_plugin_members.rb +35 -0
  167. data/lib/principles/models/user_logged_requests.rb +90 -0
  168. data/lib/principles/models/user_settings_entries.rb +49 -0
  169. data/lib/principles/models/whole_file_member_read.rb +48 -0
  170. data/lib/principles/models/without_prose.rb +20 -0
  171. data/lib/principles/models/workflow_jobs.rb +92 -0
  172. data/lib/principles/show_members_in_logged_requests_options.rb +27 -0
  173. data/lib/principles/show_principle_options.rb +32 -0
  174. data/lib/principles/version.rb +2 -2
  175. data/lib/principles/views/acknowledged_result.rb +31 -12
  176. data/lib/principles/views/acknowledgement_result.rb +18 -13
  177. data/lib/principles/views/acknowledgement_template_alone.rb +55 -0
  178. data/lib/principles/views/acknowledgements_offered.rb +54 -0
  179. data/lib/principles/views/added_member.rb +22 -6
  180. data/lib/principles/views/answer_in_a_file.rb +15 -15
  181. data/lib/principles/views/assessed_members.rb +19 -8
  182. data/lib/principles/views/assessment_request.rb +14 -6
  183. data/lib/principles/views/audit_changes.rb +14 -8
  184. data/lib/principles/views/audit_findings.rb +34 -0
  185. data/lib/principles/views/audit_result.rb +26 -24
  186. data/lib/principles/views/candidate_files_result.rb +1 -1
  187. data/lib/principles/views/carrier_checks.rb +40 -0
  188. data/lib/principles/views/carrier_edit_denial.rb +36 -0
  189. data/lib/principles/views/carrier_practices_page.rb +49 -0
  190. data/lib/principles/views/completed_audit_findings.rb +33 -0
  191. data/lib/principles/views/completed_audit_next_step.rb +12 -4
  192. data/lib/principles/views/consecutive_sections_elsewhere.rb +4 -0
  193. data/lib/principles/views/context_budget_result.rb +99 -0
  194. data/lib/principles/views/context_parts.rb +129 -25
  195. data/lib/principles/views/correction_request.rb +32 -17
  196. data/lib/principles/views/disposition_template.rb +35 -0
  197. data/lib/principles/views/first_audit.rb +29 -15
  198. data/lib/principles/views/first_audit_line.rb +4 -5
  199. data/lib/principles/views/hook_setup_result.rb +3 -1
  200. data/lib/principles/views/invocation.rb +14 -5
  201. data/lib/principles/views/last_record_notice.rb +41 -0
  202. data/lib/principles/views/lines_shared_with_member.rb +16 -22
  203. data/lib/principles/views/logged_request_status_meanings.rb +29 -0
  204. data/lib/principles/views/logged_requests_evidence.rb +84 -0
  205. data/lib/principles/views/map_added_to.rb +14 -12
  206. data/lib/principles/views/map_format_raised.rb +28 -0
  207. data/lib/principles/views/map_result.rb +41 -18
  208. data/lib/principles/views/maps_needing_members_or_audit.rb +1 -1
  209. data/lib/principles/views/member_change_since_baseline.rb +4 -0
  210. data/lib/principles/views/member_row.rb +13 -4
  211. data/lib/principles/views/member_sets_overlap.rb +31 -0
  212. data/lib/principles/views/members_a_commit_changed.rb +42 -25
  213. data/lib/principles/views/members_changed_during_audit.rb +27 -0
  214. data/lib/principles/views/members_in_logged_requests_result.rb +123 -0
  215. data/lib/principles/views/members_too_short_to_scan.rb +45 -0
  216. data/lib/principles/views/members_with_no_baseline.rb +6 -6
  217. data/lib/principles/views/meta_principle_result.rb +52 -15
  218. data/lib/principles/views/open_findings_line.rb +31 -92
  219. data/lib/principles/views/parts_index_result.rb +9 -5
  220. data/lib/principles/views/principle_read_result.rb +52 -11
  221. data/lib/principles/views/prior_findings_result.rb +9 -1
  222. data/lib/principles/views/problem_facts.rb +5 -3
  223. data/lib/principles/views/problem_lines.rb +54 -6
  224. data/lib/principles/views/problem_result.rb +7 -15
  225. data/lib/principles/views/prompts/assess-every-member/ask-whether-a-member-seen-only-as-read-is-loaded.md +3 -0
  226. data/lib/principles/views/prompts/assess-every-member/ask-whether-each-reason-holds.md +2 -0
  227. data/lib/principles/views/prompts/assess-every-member/claim-an-earlier-first-raised-at.md +6 -6
  228. data/lib/principles/views/prompts/assess-every-member/judge-the-keeping-against-the-other-principles.md +2 -0
  229. data/lib/principles/views/prompts/assess-every-member/read-a-compact-carried-member.md +9 -8
  230. data/lib/principles/views/prompts/assess-every-member/read-a-flagged-member-s-text-whole.md +3 -0
  231. data/lib/principles/views/prompts/assess-every-member/read-a-judged-member-s-text-whole.md +3 -0
  232. data/lib/principles/views/prompts/assess-every-member/turn-on-request-logging-to-see-what-reaches-context.md +2 -0
  233. data/lib/principles/views/prompts/assess-every-member/weigh-what-reached-the-context.md +7 -0
  234. data/lib/principles/views/prompts/dispose-of-each-finding/answer-guide.json +1 -0
  235. data/lib/principles/views/prompts/dispose-of-each-finding/fix-or-record-each-finding.md +6 -1
  236. data/lib/principles/views/prompts/dispose-of-each-finding/read-the-carrier-page-before-fixing.md +1 -0
  237. data/lib/principles/views/prompts/edit-a-carrier/read-the-page-before-the-first-edit.md +2 -0
  238. data/lib/principles/views/prompts/find-a-principle-to-map/read-a-cut-section-back.md +1 -1
  239. data/lib/principles/views/prompts/follow-each-carriers-best-practices/practices-for-hook-configuration.md +41 -0
  240. data/lib/principles/views/prompts/follow-each-carriers-best-practices/practices-for-hook-scripts.md +35 -0
  241. data/lib/principles/views/prompts/follow-each-carriers-best-practices/practices-for-instruction-files.md +37 -0
  242. data/lib/principles/views/prompts/follow-each-carriers-best-practices/practices-for-other-text-an-agent-reads.md +38 -0
  243. data/lib/principles/views/prompts/follow-each-carriers-best-practices/practices-for-rule-files.md +34 -0
  244. data/lib/principles/views/prompts/follow-each-carriers-best-practices/practices-for-skills.md +35 -0
  245. data/lib/principles/views/prompts/follow-each-carriers-best-practices/practices-for-subagent-definitions.md +35 -0
  246. data/lib/principles/views/prompts/learn-what-a-command-does/add-member.md +49 -51
  247. data/lib/principles/views/prompts/learn-what-a-command-does/change-member-name.md +12 -0
  248. data/lib/principles/views/prompts/learn-what-a-command-does/change-member-reason.md +12 -0
  249. data/lib/principles/views/prompts/learn-what-a-command-does/change-member-role.md +2 -1
  250. data/lib/principles/views/prompts/learn-what-a-command-does/every-command-and-how-they-chain.md +1 -1
  251. data/lib/principles/views/prompts/learn-what-a-command-does/list-candidate-files.md +1 -1
  252. data/lib/principles/views/prompts/learn-what-a-command-does/list-member-problems.md +32 -32
  253. data/lib/principles/views/prompts/learn-what-a-command-does/list-open-findings.md +10 -11
  254. data/lib/principles/views/prompts/learn-what-a-command-does/move-member.md +7 -6
  255. data/lib/principles/views/prompts/learn-what-a-command-does/remove-logged-requests.md +22 -0
  256. data/lib/principles/views/prompts/learn-what-a-command-does/remove-member.md +2 -1
  257. data/lib/principles/views/prompts/learn-what-a-command-does/report-at-session-start.md +8 -4
  258. data/lib/principles/views/prompts/learn-what-a-command-does/report-before-carrier-edit.md +13 -0
  259. data/lib/principles/views/prompts/learn-what-a-command-does/show-audit.md +7 -0
  260. data/lib/principles/views/prompts/learn-what-a-command-does/show-candidate-outlines.md +8 -2
  261. data/lib/principles/views/prompts/learn-what-a-command-does/show-carrier-practices.md +13 -0
  262. data/lib/principles/views/prompts/learn-what-a-command-does/show-context-budget.md +69 -0
  263. data/lib/principles/views/prompts/learn-what-a-command-does/show-hook-setup.md +4 -4
  264. data/lib/principles/views/prompts/learn-what-a-command-does/show-logged-requests.md +22 -0
  265. data/lib/principles/views/prompts/learn-what-a-command-does/show-members-in-logged-requests.md +62 -0
  266. data/lib/principles/views/prompts/learn-what-a-command-does/show-meta-principle-passage.md +13 -10
  267. data/lib/principles/views/prompts/learn-what-a-command-does/show-principle.md +16 -16
  268. data/lib/principles/views/prompts/learn-what-a-command-does/start-audit.md +27 -27
  269. data/lib/principles/views/prompts/learn-what-a-command-does/submit-answer.md +17 -17
  270. data/lib/principles/views/prompts/learn-what-a-command-does/turn-off-request-logging.md +18 -0
  271. data/lib/principles/views/prompts/learn-what-a-command-does/turn-on-request-logging.md +44 -0
  272. data/lib/principles/views/prompts/read-requested-outlines/continue-the-survey.md +3 -2
  273. data/lib/principles/views/prompts/read-what-reached-the-context/explain-request-logging-to-the-user.md +11 -0
  274. data/lib/principles/views/prompts/read-what-reached-the-context/explain-user-request-logging-to-the-user.md +10 -0
  275. data/lib/principles/views/prompts/read-what-reached-the-context/see-whether-members-reach-the-agent.md +2 -0
  276. data/lib/principles/views/prompts/read-what-reached-the-context/what-a-thread-continuation-holds.md +3 -0
  277. data/lib/principles/views/prompts/read-what-reached-the-context/what-absent-means.md +2 -0
  278. data/lib/principles/views/prompts/read-what-reached-the-context/what-each-context-budget-line-says.md +4 -0
  279. data/lib/principles/views/prompts/read-what-reached-the-context/what-each-member-line-says.md +2 -0
  280. data/lib/principles/views/prompts/read-what-reached-the-context/what-not-a-carrier-means.md +2 -0
  281. data/lib/principles/views/prompts/read-what-reached-the-context/what-present-but-not-loaded-means.md +2 -0
  282. data/lib/principles/views/prompts/read-what-reached-the-context/what-present-means.md +1 -0
  283. data/lib/principles/views/prompts/read-what-reached-the-context/what-unmeasurable-means.md +2 -0
  284. data/lib/principles/views/prompts/report-after-a-commit/a-row-ending-audit-asks-to-acknowledge-or-audit-again.md +1 -0
  285. data/lib/principles/views/prompts/report-after-a-commit/a-row-ending-mapping-commit-asks-for-an-audit.md +1 -0
  286. data/lib/principles/views/prompts/report-after-a-commit/a-row-ending-pinned-asks-for-an-audit.md +1 -0
  287. data/lib/principles/views/prompts/report-after-a-commit/plugin-members-are-still-listed.md +1 -0
  288. data/lib/principles/views/prompts/report-after-a-commit/the-commits-changed-these-members-waiting-on-an-upgrade.md +2 -2
  289. data/lib/principles/views/prompts/report-after-a-commit/the-commits-changed-these-members.md +2 -2
  290. data/lib/principles/views/prompts/report-after-a-commit/this-command-shows-what-each-needs-before-the-next-commit.md +1 -0
  291. data/lib/principles/views/prompts/run-a-first-audit-in-the-background/follow-the-brief.md +1 -0
  292. data/lib/principles/views/prompts/set-principles-up/ask-to-set-principles-up-after-an-audit.md +1 -0
  293. data/lib/principles/views/prompts/set-principles-up/entry-file-passage.md +1 -1
  294. data/lib/principles/views/prompts/set-principles-up/map-the-enabling-line-at-no-edit.md +3 -0
  295. data/lib/principles/views/prompts/set-principles-up/name-the-step-each-member-lacks.md +2 -2
  296. data/lib/principles/views/prompts/set-principles-up/place-the-passage-with-the-user.md +4 -2
  297. data/lib/principles/views/prompts/set-principles-up/what-the-passage-presumes.md +1 -1
  298. data/lib/principles/views/prompts/set-principles-up/why-set-principles-up.md +1 -0
  299. data/lib/principles/views/prompts/start-a-session/act-on-the-open-findings.md +2 -1
  300. data/lib/principles/views/prompts/start-a-session/ask-to-set-principles-up.md +2 -0
  301. data/lib/principles/views/prompts/start-a-session/hand-the-first-audit-to-a-subagent.md +2 -3
  302. data/lib/principles/views/prompts/start-a-session/name-the-principles-nothing-checks.md +1 -0
  303. data/lib/principles/views/prompts/start-a-session/raise-the-open-findings-waiting-on-no-one.md +1 -0
  304. data/lib/principles/views/request_logging_result.rb +108 -0
  305. data/lib/principles/views/request_part.rb +10 -4
  306. data/lib/principles/views/request_parts.rb +9 -0
  307. data/lib/principles/views/set_up_offer_line.rb +43 -0
  308. data/lib/principles/views/whole_file_elsewhere.rb +30 -0
  309. metadata +154 -5
  310. data/lib/principles/views/prompts/report-after-a-commit/the-principles-skill-is-where-to-start.md +0 -1
  311. data/lib/principles/views/prompts/report-after-a-commit/this-command-shows-each-one.md +0 -1
  312. data/lib/principles/views/prompts/set-principles-up/keep-them-held-between-audits.md +0 -2
@@ -0,0 +1,144 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require "json"
5
+ require "open3"
6
+ require "tempfile"
7
+ require_relative "decoded_leaf"
8
+ require_relative "kept_line"
9
+ require_relative "raw_request_filter"
10
+ require_relative "string_counts"
11
+
12
+ module Principles
13
+ # Every file and process this tool reads or writes. data/CLAUDE.md owns the boundary.
14
+ module Data
15
+ # What a scan found in the captured requests. RawRequestFilter picks the request files that may hold a pattern,
16
+ # StringCounts counts the strings of the others, and only the picked ones are decoded: one jq run writes each
17
+ # string in them as one line, request<TAB>path<TAB>role<TAB>string, the path, its message's role and the string as
18
+ # JSON, and one rg run keeps those holding a pattern, escaped by the same jq, as fixed strings. Each distinct kept
19
+ # string is then split into its lines once. It holds each line holding a pattern as a KeptLine, how many strings
20
+ # each request it read through held, by request, and whether the scan was whole.
21
+ class LoggedRequestMatches
22
+ # Each string in a request, prefixed by the request file, its JSON path and the role of the message it sits in,
23
+ # null outside a message or for a role not a string, the path and role written as JSON, so no string can stop the
24
+ # run or split a line, and no absent role leaves an empty column. After a request's strings comes its end line,
25
+ # request<TAB><TAB>strings, giving how many it held; only an end line has an empty path, so a request with none
26
+ # was not decoded to its end.
27
+ DECODE = <<~JQ
28
+ (input_filename | split("/") | last) as $request
29
+ | [paths(type == "string")] as $paths
30
+ | ($paths[] as $path
31
+ | ((if $path[0] == "messages" then getpath($path[0:2]).role? | strings else empty end) // null) as $role
32
+ | [$request, ($path | tojson), ($role | tojson), (getpath($path) | tojson)] | join("\\t")),
33
+ ([$request, "", ($paths | length | tostring)] | join("\\t"))
34
+ JQ
35
+ # The fixed string rg keeps each end line by: no other line holds two tabs together.
36
+ ENDING = "\t\t"
37
+ # Each pattern line as jq writes it inside a JSON string, so a pattern finds the strings DECODE wrote.
38
+ ESCAPE = "tojson | .[1:-1]"
39
+
40
+ # Runs jq and rg over files in the requests folder, keeping each line holding one of patterns, none of them
41
+ # empty.
42
+ #: (String, Array[String], Array[String]) -> LoggedRequestMatches
43
+ def self.run(requests, files, patterns)
44
+ listed, listing = RawRequestFilter.listing(requests, files, patterns)
45
+ counts, counting = StringCounts.of(requests, files - listed)
46
+ decoded = listed.empty? ? new(found: [], leaves: {}, whole: true) : decoded(requests, listed, patterns)
47
+ new(found: decoded.found, leaves: { **counts, **decoded.leaves }, whole: listing && counting && decoded.whole)
48
+ end
49
+
50
+ # One jq and rg run over files, keeping each line holding one of patterns.
51
+ #: (String, Array[String], Array[String]) -> LoggedRequestMatches
52
+ def self.decoded(requests, files, patterns)
53
+ written, escaping = escaped(patterns)
54
+ Tempfile.create("principles-patterns") do |listed|
55
+ listed.write([*written, ENDING].join("\n"))
56
+ listed.flush
57
+ Tempfile.create("principles-decode-errors") do |errors|
58
+ kept, statuses = decode_and_match(requests, files, listed.path, errors.path)
59
+ kept_lines(kept, patterns, escaping && whole?(statuses, errors.path))
60
+ end
61
+ end
62
+ end
63
+
64
+ # patterns as ESCAPE writes them, one a line, and whether jq wrote each of them and no error: a pattern it did
65
+ # not write finds nothing, so the run is not whole without it. capture3 reads while it writes, so many patterns
66
+ # cannot fill the pipes and stall, and it keeps jq's errors off this tool's output.
67
+ #: (Array[String]) -> [Array[String], bool]
68
+ def self.escaped(patterns)
69
+ written, errors, status = Open3.capture3("jq", "-rR", ESCAPE, stdin_data: patterns.join("\n"))
70
+ lines = written.split("\n")
71
+ [lines, status.success? == true && errors.empty? && lines.size == patterns.size]
72
+ end
73
+
74
+ # Each line rg kept, and both processes' exit statuses, their errors going to errors_path.
75
+ #: (String, Array[String], String, String) -> [Array[String], Array[Process::Status]]
76
+ def self.decode_and_match(requests, files, patterns_file, errors_path)
77
+ kept = [] #: Array[String]
78
+ statuses = [] #: Array[Process::Status]
79
+ Open3.pipeline_r(["jq", "-r", DECODE, *files], ["rg", "--no-config", "-F", "-f", patterns_file, "-"],
80
+ chdir: requests, err: errors_path) do |output, waits|
81
+ kept = output.each_line(chomp: true).to_a
82
+ statuses = waits.map(&:value)
83
+ end
84
+ [kept, statuses]
85
+ end
86
+
87
+ # Whether jq ran through every file, which its exit status alone does not say, as it names only the last file's
88
+ # error, so any error either process wrote counts too; and rg read all jq gave it, finding a line or none. The
89
+ # errors' words are never kept, since jq's can quote a value from a request.
90
+ #: (Array[Process::Status], String) -> bool
91
+ def self.whole?(statuses, errors_path)
92
+ case statuses.map(&:exitstatus)
93
+ in [0, 0 | 1] then File.empty?(errors_path)
94
+ else false
95
+ end
96
+ end
97
+
98
+ # The lines rg kept: the end lines count each request's strings, and each other names a string holding a pattern.
99
+ #: (Array[String], Array[String], bool) -> LoggedRequestMatches
100
+ def self.kept_lines(kept, patterns, whole)
101
+ endings, strings = kept.map { it.split("\t", 4).map(&:to_s) }.partition { |_, path, _| path.to_s.empty? }
102
+ leaves = endings.to_h { |request, _, count| [request.to_s, Integer(count)] }
103
+ new(found: lines_holding(strings, patterns), leaves:, whole:)
104
+ end
105
+
106
+ # Each line of strings, each as [request, path, role, string], the last three in JSON, holding one of patterns,
107
+ # as a KeptLine, each distinct string read once.
108
+ #: (Array[Array[String]], Array[String]) -> Array[KeptLine]
109
+ def self.lines_holding(strings, patterns)
110
+ held = strings.map(&:last).uniq.to_h { [it, DecodedLeaf.new(JSON.parse(it.to_s)).holding(patterns)] }
111
+ strings.flat_map do |request, path, role, leaf|
112
+ kept_in(request.to_s, path.to_s, JSON.parse(role.to_s), held.fetch(leaf))
113
+ end
114
+ end
115
+
116
+ # Each of lines, as DecodedLeaf#holding gives them, kept in request at path, the path in JSON, in a message of
117
+ # role, or none.
118
+ #: (String, String, String?, Array[[String, String, Integer]]) -> Array[KeptLine]
119
+ def self.kept_in(request, path, role, lines)
120
+ place = JSON.parse(path).join(".")
121
+ lines.map { KeptLine.new(request, place, role, it) }
122
+ end
123
+ private_class_method :decoded, :escaped, :decode_and_match, :whole?, :kept_lines, :lines_holding, :kept_in
124
+
125
+ #: Array[KeptLine]
126
+ attr_reader :found
127
+ #: Hash[String, Integer]
128
+ attr_reader :leaves
129
+
130
+ # Whether the run read every file through: jq exited 0, neither process wrote an error, and rg read all it was
131
+ # given.
132
+ #: bool
133
+ attr_reader :whole
134
+
135
+ #: (found: Array[KeptLine], leaves: Hash[String, Integer], whole: bool) -> void
136
+ def initialize(found:, leaves:, whole:)
137
+ @found = found
138
+ @leaves = leaves
139
+ @whole = whole
140
+ freeze
141
+ end
142
+ end
143
+ end
144
+ end
@@ -0,0 +1,188 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require "json"
5
+ require "time"
6
+ require_relative "logged_request_matches"
7
+ require_relative "request_shape"
8
+
9
+ module Principles
10
+ # Every file and process this tool reads or writes. data/CLAUDE.md owns the boundary.
11
+ module Data
12
+ # The requests the host captured in a request-logging folder, scanned by fast standard tools rather than Ruby:
13
+ # LoggedRequestMatches runs the jq and rg that decode and match them. A segment is reminder inside a
14
+ # system-reminder tag and plain outside one. jq and rg must be on PATH; Models::Feature refuses before a scan
15
+ # runs without them.
16
+ class LoggedRequestScan
17
+ REQUEST = "*.request.json"
18
+ INDEX = "index.jsonl"
19
+ # What requests gives of each request's index line, the response file by its name here.
20
+ INDEXED = %w[session_id query_source timestamp response_file].freeze #: Array[String]
21
+
22
+ # Each tool a request lists with defer_loading, as request<TAB>path: the host sends such a tool by name, and its
23
+ # definition reaches the model only once a tool search loads it.
24
+ DEFERRED = <<~'JQ'
25
+ (input_filename | split("/") | last) as $request
26
+ | (.tools // []) | to_entries[] | select(.value.defer_loading == true)
27
+ | "\($request)\ttools.\(.key)"
28
+ JQ
29
+
30
+ # Each request's count of tools and of messages, how its last message ends, and its thread role, as
31
+ # request<TAB>tools<TAB>messages<TAB>ending<TAB>thread. The ending is tool_result when it holds a block of that
32
+ # type, text when it is a user or system message holding none, and other for anything else, a request with no
33
+ # messages among them. The thread role is continues when its thread object names a previous_message_id, starts
34
+ # when it names none, and none without a thread object; only the id's presence is read.
35
+ SHAPES = <<~'JQ'
36
+ (input_filename | split("/") | last) as $request
37
+ | ((.messages // []) | last) as $last
38
+ | (try (if ($last.content | type) == "array" and any($last.content[]; (.type? // null) == "tool_result")
39
+ then "tool_result" elif $last.role == "user" or $last.role == "system" then "text" else "other" end)
40
+ catch "other") as $ending
41
+ | (if (.thread | type) != "object" then "none" elif .thread.previous_message_id != null then "continues"
42
+ else "starts" end) as $thread
43
+ | "\($request)\t\((.tools // []) | length)\t\((.messages // []) | length)\t\($ending)\t\($thread)"
44
+ JQ
45
+
46
+ # DEFERRED, printing nothing, so a file it stops at fails it. It stands for both programs the other reads run:
47
+ # LoggedRequestMatches::DECODE reads any whole JSON through, so a file it stops at DEFERRED stops at too, and
48
+ # probing with it alone spares decoding every string of every request twice.
49
+ PROBE = "(#{DEFERRED}) | empty".freeze #: String
50
+
51
+ # Reads which request files the scan can read through once, here, so every read of this scan leaves the same ones
52
+ # out. That runs jq as the scan is built, which is fine as a command builds one scan per run.
53
+ # only names the request files to read, by name, when the folder holds other sessions' too; every one when nil.
54
+ #: (String, ?only: Array[String]?) -> void
55
+ def initialize(requests, only: nil)
56
+ @requests = requests
57
+ @only = only
58
+ @unreadable = unreadable_files.freeze #: Array[Hash[String, String]]
59
+ freeze
60
+ end
61
+
62
+ # Each request file, by name, with the session_id, query_source, timestamp and response file its index line
63
+ # gives, each nil when the response failed and the host wrote it no line; such a request also gives written_at,
64
+ # when its file was last written, in UTC, its only time.
65
+ #: () -> Array[Hash[String, String?]]
66
+ def requests
67
+ lines = index_lines
68
+ request_files.map do |name|
69
+ line = lines[name]
70
+ next { "file" => name, **line } if line
71
+
72
+ { "file" => name, **INDEXED.to_h { [it, nil] }, "written_at" => written_at(name) }
73
+ end
74
+ end
75
+
76
+ # The request file name here, parsed, as the host sent it.
77
+ #: (String) -> untyped
78
+ def body(name) = JSON.parse(File.read(File.join(@requests, name)))
79
+
80
+ # The response file name here, parsed, or nil where the host wrote none by that name.
81
+ #: (String) -> untyped
82
+ def response(name)
83
+ path = File.join(@requests, File.basename(name))
84
+ JSON.parse(File.read(path)) if File.file?(path)
85
+ end
86
+
87
+ # Each decoded line holding one of patterns, and each request's count of strings. Each pattern is one line; an
88
+ # empty one is left out, since rg would match every line by it.
89
+ #: (Array[String]) -> LoggedRequestMatches
90
+ def matches(patterns)
91
+ patterns = patterns.reject(&:empty?)
92
+ files = readable_files
93
+ return LoggedRequestMatches.new(found: [], leaves: {}, whole: true) if files.empty? || patterns.empty?
94
+
95
+ LoggedRequestMatches.run(@requests, files, patterns)
96
+ end
97
+
98
+ # Each request file the scan cannot read through, by its request and its reason: not_whole_json for one that is
99
+ # not whole JSON, such as one a host left truncated, and not_decoded for whole JSON a read stops at, such as a
100
+ # list. jq stops at such a file, so the other reads leave these out rather than read it in part.
101
+ #: Array[Hash[String, String]]
102
+ attr_reader :unreadable
103
+
104
+ # Each tool a request defers loading, as [request, path].
105
+ #: () -> Array[[String, String]]
106
+ def deferred_tools
107
+ files = readable_files
108
+ return [] if files.empty?
109
+
110
+ listed = IO.popen(["jq", "-r", DEFERRED, *files], chdir: @requests, &:read)
111
+ listed.to_s.lines.map do |line|
112
+ request, path = line.chomp.split("\t", 2)
113
+ [request.to_s, path.to_s]
114
+ end
115
+ end
116
+
117
+ # The shape of each request it reads through, by request.
118
+ #: () -> Hash[String, RequestShape]
119
+ def request_shapes
120
+ files = readable_files
121
+ return {} if files.empty?
122
+
123
+ listed = IO.popen(["jq", "-r", SHAPES, *files], chdir: @requests, &:read)
124
+ listed.to_s.lines.to_h do |line|
125
+ request, tools, messages, ending, thread = line.chomp.split("\t", 5)
126
+ [request.to_s, RequestShape.new(tools: Integer(tools), messages: Integer(messages), ending: ending.to_s,
127
+ thread: thread.to_s)]
128
+ end
129
+ end
130
+
131
+ private
132
+
133
+ # When request file name was last written, in UTC to the millisecond.
134
+ #: (String) -> String
135
+ def written_at(name) = File.mtime(File.join(@requests, name)).utc.iso8601(3)
136
+
137
+ #: () -> Array[String]
138
+ def request_files
139
+ found = Dir.exist?(@requests) ? Dir.glob(REQUEST, base: @requests).sort : []
140
+ @only ? found & @only : found
141
+ end
142
+
143
+ # One jq run probes every request file, and each is probed alone only when that run fails. The reason is a fixed
144
+ # code, never jq's message, which can quote a value from the request.
145
+ #: () -> Array[Hash[String, String]]
146
+ def unreadable_files
147
+ files = request_files
148
+ return [] if files.empty? || runs?(PROBE, files)
149
+
150
+ files.reject { runs?(PROBE, [it]) }
151
+ .map { { "request" => it, "reason" => runs?("empty", [it]) ? "not_decoded" : "not_whole_json" } }
152
+ end
153
+
154
+ #: () -> Array[String]
155
+ def readable_files = request_files - @unreadable.map { it.fetch("request") }
156
+
157
+ # Whether jq runs program, which prints nothing, through every one of files. jq's exit status tells only whether
158
+ # the last file failed, so an error it printed for any other file fails it too; its words are never kept.
159
+ #: (String, Array[String]) -> bool
160
+ def runs?(program, files)
161
+ printed = IO.popen(["jq", program, *files], chdir: @requests, err: %i[child out], &:read)
162
+ Process.last_status&.success? == true && printed.to_s.empty?
163
+ end
164
+
165
+ # The query_source and timestamp of each request the index names, by its file name.
166
+ #: () -> Hash[String, Hash[String, String?]]
167
+ def index_lines
168
+ index = File.join(@requests, INDEX)
169
+ return {} unless File.file?(index)
170
+
171
+ File.foreach(index).filter_map do |line|
172
+ entry = JSON.parse(line)
173
+ [File.basename(entry.fetch("request_file")), indexed(entry)] if entry.is_a?(Hash)
174
+ rescue JSON::ParserError, KeyError, TypeError
175
+ nil
176
+ end.to_h
177
+ end
178
+
179
+ # What requests gives of entry, an index line: the response file by its name, as it is here.
180
+ #: (Hash[String, untyped]) -> Hash[String, String?]
181
+ def indexed(entry)
182
+ response = entry["response_file"]
183
+ named = response.is_a?(String) ? File.basename(response) : nil
184
+ INDEXED.to_h { [it, entry[it]] }.merge("response_file" => named)
185
+ end
186
+ end
187
+ end
188
+ end
@@ -16,7 +16,11 @@ module Principles
16
16
  class MapFiles
17
17
  # The newest map format ordinary current-map reads accept. Models::Principle says which member fields need which
18
18
  # format, and a spec ties the two together.
19
- NEWEST_FORMAT = 2
19
+ NEWEST_FORMAT = 5
20
+
21
+ # The formats up to NEWEST_FORMAT a current read still refuses: 3 is an earlier map's history link, which only
22
+ # parse_history reads.
23
+ HISTORY_ONLY_FORMATS = [3].freeze #: Array[Integer]
20
24
 
21
25
  #: (PrinciplesSubfolder) -> void
22
26
  def initialize(directory)
@@ -111,15 +115,25 @@ module Principles
111
115
  #: (String, untyped, String?) -> void
112
116
  def refuse_newer(id, map, commit)
113
117
  format = map.is_a?(Hash) ? map["format"] : nil
114
- return unless format.is_a?(Integer) && format > NEWEST_FORMAT
118
+ return unless format.is_a?(Integer)
119
+ return unless format > NEWEST_FORMAT || HISTORY_ONLY_FORMATS.include?(format)
115
120
 
116
- raise Error.new("newer_format", "#{where(id, commit)} is format #{format}, written by a newer version " \
117
- "than this one; this one reads up to format #{NEWEST_FORMAT}",
121
+ raise Error.new("newer_format", "#{where(id, commit)} is format #{format}, #{unread_because(format)}",
118
122
  "Update the installed principles plugin or gem, then run the command again. " \
119
123
  "Leave the map as it is: it is not broken.",
120
124
  relative(id))
121
125
  end
122
126
 
127
+ # Why a current read cannot read a map at format.
128
+ #: (Integer) -> String
129
+ def unread_because(format)
130
+ if format > NEWEST_FORMAT
131
+ "written by a newer version than this one; this one reads up to format #{NEWEST_FORMAT}"
132
+ else
133
+ "which this version reads only as an earlier map's history"
134
+ end
135
+ end
136
+
123
137
  #: (String, String?) -> String
124
138
  def where(id, commit)
125
139
  commit ? "#{relative(id)} at commit #{commit[0, 12]}" : relative(id)
@@ -0,0 +1,85 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "bounded_file"
5
+
6
+ module Principles
7
+ # Every file and process this tool reads or writes. data/CLAUDE.md owns the boundary.
8
+ module Data
9
+ # The folder Claude Code installed a copy of a plugin in. This tool only reads it, and walks no symbolic link, so
10
+ # every file it names is inside the folder as the disk holds it.
11
+ class PluginFolder
12
+ # The most entries a walk looks at, far above any plugin, so a vast folder cannot hang a run.
13
+ ENTRIES = 10_000 #: Integer
14
+
15
+ #: (String) -> void
16
+ def initialize(path)
17
+ @path = path
18
+ freeze
19
+ end
20
+
21
+ # Whether relative names a regular file in the folder, neither it nor any folder on the way a symbolic link.
22
+ #: (String) -> bool
23
+ def file?(relative)
24
+ *folders, name = relative.split("/")
25
+ inside = @path
26
+ folders.each do |folder|
27
+ inside = File.join(inside, folder)
28
+ return false unless File.lstat(inside).directory?
29
+ end
30
+ File.lstat(File.join(inside, name.to_s)).file?
31
+ rescue SystemCallError
32
+ false
33
+ end
34
+
35
+ # The text of the file at relative, which file? holds, refused when it is not UTF-8 or is larger than BoundedFile
36
+ # reads; a refusal names it by relative alone, never by where the host installed it.
37
+ #: (String) -> String
38
+ def read(relative)
39
+ BoundedFile.new(File.join(@path, relative), shown_as: relative, named: "#{relative} in the plugin").read_text
40
+ end
41
+
42
+ # Whether relative, or a folder on the way to it, or the plugin's folder itself, is a symbolic link, which this
43
+ # tool never follows.
44
+ #: (String) -> bool
45
+ def linked?(relative)
46
+ segments = relative.split("/")
47
+ (0..segments.size).any? { |count| File.lstat(File.join([@path] + segments.first(count))).symlink? }
48
+ rescue SystemCallError
49
+ false
50
+ end
51
+
52
+ # Each regular file in the folder by its path inside it, sorted, from a walk of at most ENTRIES entries that
53
+ # enters no symbolic link; none when the folder is gone.
54
+ #: () -> Array[String]
55
+ def files = walked.take(ENTRIES).select { |_, entry| entry.file? }.map(&:first).sort
56
+
57
+ private
58
+
59
+ # Every entry under the folder, by its path inside it, met one at a time, so taking the first few walks no
60
+ # further.
61
+ #: () -> Enumerator[[String, File::Stat]]
62
+ def walked = Enumerator.new { |found| visit("", found) }
63
+
64
+ #: (String, untyped) -> void
65
+ def visit(inside, found)
66
+ entries(inside).each do |relative, entry|
67
+ found << [relative, entry]
68
+ visit(relative, found) if entry.directory?
69
+ end
70
+ end
71
+
72
+ # Each entry directly in the folder at inside, by its path inside the plugin, as lstat sees it; none when it
73
+ # cannot be read.
74
+ #: (String) -> Array[[String, File::Stat]]
75
+ def entries(inside)
76
+ Dir.children(File.join(@path, inside)).sort.map do |name|
77
+ relative = inside.empty? ? name : "#{inside}/#{name}"
78
+ [relative, File.lstat(File.join(@path, relative))]
79
+ end
80
+ rescue SystemCallError
81
+ []
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,73 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require "json"
5
+ require_relative "../error"
6
+ require_relative "json_text"
7
+
8
+ module Principles
9
+ # Every file and process this tool reads or writes. data/CLAUDE.md owns the boundary.
10
+ module Data
11
+ # Claude Code's record of the plugins installed on this machine, the text of installed_plugins.json under its
12
+ # config folder: for each plugin, by its install name NAME@MARKETPLACE, one row per install, in the order the host
13
+ # keeps them. Which row applies to a project is a model's rule; this reads the rows as the host wrote them.
14
+ class PluginRegistry
15
+ # The registry version this tool was written against, from a probe of Claude Code's own file.
16
+ VERSION = 2 #: Integer
17
+
18
+ # The registry text holds, refused when it is not a registry of the version this tool reads.
19
+ #: (String) -> void
20
+ def initialize(text)
21
+ @plugins = plugins_in(parsed(text)) #: Hash[String, Array[Hash[String, untyped]]]
22
+ freeze
23
+ end
24
+
25
+ # The rows for the plugin named, each with the fields a selection reads, in the host's order; none when the
26
+ # registry names no such plugin.
27
+ #: (String) -> Array[Hash[String, String?]]
28
+ def rows(name)
29
+ @plugins.fetch(name, []).map do |row|
30
+ { "scope" => row["scope"], "version" => row["version"], "project_path" => row["projectPath"],
31
+ "install_path" => row["installPath"] }
32
+ end
33
+ end
34
+
35
+ # Every plugin install name the registry holds, sorted.
36
+ #: () -> Array[String]
37
+ def names = @plugins.keys.sort
38
+
39
+ private
40
+
41
+ #: (Hash[String, untyped]) -> Hash[String, Array[Hash[String, untyped]]]
42
+ def plugins_in(registry)
43
+ unless registry["version"].eql?(VERSION)
44
+ raise Error.new("unsupported_plugin_registry",
45
+ "Claude Code's plugin registry is version #{registry["version"].inspect}; this tool reads " \
46
+ "version #{VERSION}",
47
+ "Map the project's own files instead; this copy cannot read this Claude Code's plugins.")
48
+ end
49
+
50
+ plugins = registry["plugins"]
51
+ plugins.is_a?(Hash) && plugins.each_value.all? { |rows| rows?(rows) } ? plugins : unreadable
52
+ end
53
+
54
+ #: (untyped) -> bool
55
+ def rows?(rows) = rows.is_a?(Array) ? rows.all?(Hash) : false
56
+
57
+ #: (String) -> Hash[String, untyped]
58
+ def parsed(text)
59
+ registry = JsonText.new(text).parsed
60
+ registry.is_a?(Hash) ? registry : unreadable
61
+ rescue JSON::ParserError
62
+ unreadable
63
+ end
64
+
65
+ #: () -> bot
66
+ def unreadable
67
+ raise Error.new("unreadable_plugin_registry",
68
+ "Claude Code's plugin registry is not in the shape this tool reads",
69
+ "Reinstall the plugin with Claude Code, or map the project's own files instead.")
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,49 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "principles_directory"
5
+ require_relative "principles_subfolder"
6
+ require_relative "map_files"
7
+ require_relative "audit_files"
8
+ require_relative "guidance_search_paths_file"
9
+ require_relative "request_logging_folder"
10
+
11
+ module Principles
12
+ # Every file and process this tool reads or writes. data/CLAUDE.md owns the boundary.
13
+ module Data
14
+ # The files this tool keeps for a project under .principles: its member maps, its audit records, its recorded
15
+ # guidance search paths and its git-ignored logged requests, which a project is handed together.
16
+ class PrinciplesStore
17
+ # The store of the project at root.
18
+ #: (String) -> PrinciplesStore
19
+ def self.at(root)
20
+ new(maps: MapFiles.new(PrinciplesSubfolder.new(root, "maps")),
21
+ audits: AuditFiles.new(PrinciplesSubfolder.new(root, "audits")),
22
+ guidance_search_paths: GuidanceSearchPathsFile.new(PrinciplesDirectory.new(root)),
23
+ request_logging: RequestLoggingFolder.new(root))
24
+ end
25
+
26
+ #: MapFiles
27
+ attr_reader :maps
28
+
29
+ #: AuditFiles
30
+ attr_reader :audits
31
+
32
+ #: GuidanceSearchPathsFile
33
+ attr_reader :guidance_search_paths
34
+
35
+ #: RequestLoggingFolder
36
+ attr_reader :request_logging
37
+
38
+ #: (maps: MapFiles, audits: AuditFiles, guidance_search_paths: GuidanceSearchPathsFile,
39
+ #| request_logging: RequestLoggingFolder) -> void
40
+ def initialize(maps:, audits:, guidance_search_paths:, request_logging:)
41
+ @maps = maps
42
+ @audits = audits
43
+ @guidance_search_paths = guidance_search_paths
44
+ @request_logging = request_logging
45
+ freeze
46
+ end
47
+ end
48
+ end
49
+ end
@@ -22,6 +22,21 @@ module Principles
22
22
  @root = File.realpath(root) #: String
23
23
  end
24
24
 
25
+ # absolute's path inside the project, as a hook names the file a call edits, or nil when it is not a full path
26
+ # into the project. The folder it is in, or the nearest one there, is resolved, so a root spelled through a link
27
+ # matches, and a file not written yet has a place.
28
+ #: (String) -> String?
29
+ def path_in_project(absolute)
30
+ return nil unless absolute.start_with?("/")
31
+
32
+ folder = File.dirname(absolute)
33
+ folder = File.dirname(folder) until File.directory?(folder)
34
+ resolved = File.join(File.realpath(folder), absolute.delete_prefix(folder))
35
+ resolved.start_with?("#{@root}/") ? resolved.delete_prefix("#{@root}/") : nil
36
+ rescue SystemCallError
37
+ nil
38
+ end
39
+
25
40
  # A project file's text, refused unless it is a readable UTF-8 file inside the project.
26
41
  # Models::MemberFile finds a member's place in it.
27
42
  #: (String) -> String
@@ -0,0 +1,25 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require "open3"
5
+
6
+ module Principles
7
+ # Every file and process this tool reads or writes. data/CLAUDE.md owns the boundary.
8
+ module Data
9
+ # One run of a command in a folder: what it printed, and its exit status.
10
+ module Ran
11
+ # What command printed, run in folder with its errors going to errors_path, and its exit status, nil for one
12
+ # stopped by a signal.
13
+ #: (Array[String], String, String) -> [String, Integer?]
14
+ def self.run(command, folder, errors_path)
15
+ printed = ""
16
+ status = nil #: Integer?
17
+ Open3.pipeline_r(command, chdir: folder, err: errors_path) do |output, waits|
18
+ printed = output.read
19
+ status = waits.fetch(0).value.exitstatus
20
+ end
21
+ [printed, status]
22
+ end
23
+ end
24
+ end
25
+ end