principles 0.1.4 → 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 (140) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +31 -1
  3. data/exe/principles +1 -1
  4. data/lib/principles/cli.rb +9 -10
  5. data/lib/principles/command_routes.rb +7 -1
  6. data/lib/principles/controllers/carrier_edit_report.rb +40 -0
  7. data/lib/principles/controllers/command.rb +7 -3
  8. data/lib/principles/controllers/context_budget.rb +61 -0
  9. data/lib/principles/controllers/inventory.rb +3 -2
  10. data/lib/principles/controllers/member_addition.rb +1 -1
  11. data/lib/principles/controllers/member_entries.rb +30 -38
  12. data/lib/principles/controllers/member_entry_changes.rb +71 -0
  13. data/lib/principles/controllers/member_problems_report.rb +1 -1
  14. data/lib/principles/controllers/members_in_logged_requests.rb +5 -5
  15. data/lib/principles/controllers/members_to_assess.rb +11 -1
  16. data/lib/principles/controllers/meta_principle_setup.rb +10 -2
  17. data/lib/principles/controllers/parts_to_read.rb +2 -1
  18. data/lib/principles/controllers/plugin_member_addition.rb +4 -4
  19. data/lib/principles/controllers/request_logging.rb +17 -4
  20. data/lib/principles/controllers/running_tool.rb +19 -5
  21. data/lib/principles/controllers/session_start_report.rb +26 -2
  22. data/lib/principles/data/captured_requests.rb +118 -0
  23. data/lib/principles/data/carrier_page_markers.rb +38 -0
  24. data/lib/principles/data/claude_code_home.rb +17 -0
  25. data/lib/principles/data/command-steps.json +59 -19
  26. data/lib/principles/data/kept_line.rb +14 -7
  27. data/lib/principles/data/logged_request_matches.rb +23 -17
  28. data/lib/principles/data/logged_request_scan.rb +79 -11
  29. data/lib/principles/data/map_files.rb +1 -1
  30. data/lib/principles/data/project_files.rb +15 -0
  31. data/lib/principles/data/request_logging_folder.rb +42 -74
  32. data/lib/principles/data/request_shape.rb +33 -0
  33. data/lib/principles/data/running_version.rb +1 -1
  34. data/lib/principles/data/schemas/command-steps.schema.json +1 -1
  35. data/lib/principles/data/schemas/member-map.schema.json +2 -0
  36. data/lib/principles/data/settings_env_keys.rb +26 -0
  37. data/lib/principles/data/user_request_logging_folder.rb +73 -0
  38. data/lib/principles/invocation_options.rb +4 -4
  39. data/lib/principles/launch.rb +15 -6
  40. data/lib/principles/models/captured_sessions.rb +53 -15
  41. data/lib/principles/models/carrier_edit_input.rb +56 -0
  42. data/lib/principles/models/carrier_pages_shown.rb +28 -0
  43. data/lib/principles/models/carved_message_bytes.rb +115 -0
  44. data/lib/principles/models/context_parts.rb +59 -0
  45. data/lib/principles/models/declared_map_changes.rb +22 -13
  46. data/lib/principles/models/dropped_thinking.rb +73 -0
  47. data/lib/principles/models/feature.rb +2 -1
  48. data/lib/principles/models/installed_component.rb +3 -3
  49. data/lib/principles/models/logged_request_reach.rb +19 -11
  50. data/lib/principles/models/map_format_raise.rb +28 -0
  51. data/lib/principles/models/mapped_principles.rb +2 -1
  52. data/lib/principles/models/member.rb +6 -1
  53. data/lib/principles/models/member_justification.rb +52 -0
  54. data/lib/principles/models/member_problems.rb +2 -1
  55. data/lib/principles/models/member_reach.rb +28 -5
  56. data/lib/principles/models/member_snapshots.rb +11 -0
  57. data/lib/principles/models/members_by_part.rb +67 -0
  58. data/lib/principles/models/members_in_logged_requests.rb +56 -17
  59. data/lib/principles/models/meta_principle.rb +4 -4
  60. data/lib/principles/models/part_token_estimates.rb +60 -0
  61. data/lib/principles/models/plugin_enabled_in_project_settings.rb +61 -0
  62. data/lib/principles/models/principle.rb +7 -4
  63. data/lib/principles/models/principle_members.rb +7 -0
  64. data/lib/principles/models/project.rb +20 -31
  65. data/lib/principles/models/project_harness.rb +48 -0
  66. data/lib/principles/models/report_hooks.rb +9 -4
  67. data/lib/principles/models/request_channel.rb +62 -0
  68. data/lib/principles/models/request_kind.rb +62 -0
  69. data/lib/principles/models/request_logging.rb +107 -72
  70. data/lib/principles/models/request_logging_guard.rb +84 -0
  71. data/lib/principles/models/request_time.rb +46 -0
  72. data/lib/principles/models/session_registry.rb +61 -0
  73. data/lib/principles/models/session_start_input.rb +11 -2
  74. data/lib/principles/models/session_trend.rb +36 -0
  75. data/lib/principles/models/turn_opening.rb +83 -0
  76. data/lib/principles/models/turn_position.rb +43 -0
  77. data/lib/principles/models/user_logged_requests.rb +90 -0
  78. data/lib/principles/models/user_settings_entries.rb +49 -0
  79. data/lib/principles/version.rb +2 -2
  80. data/lib/principles/views/assessed_members.rb +4 -4
  81. data/lib/principles/views/assessment_request.rb +6 -3
  82. data/lib/principles/views/audit_result.rb +13 -6
  83. data/lib/principles/views/candidate_files_result.rb +1 -1
  84. data/lib/principles/views/carrier_edit_denial.rb +36 -0
  85. data/lib/principles/views/context_budget_result.rb +99 -0
  86. data/lib/principles/views/correction_request.rb +11 -1
  87. data/lib/principles/views/hook_setup_result.rb +3 -1
  88. data/lib/principles/views/logged_requests_evidence.rb +18 -3
  89. data/lib/principles/views/map_format_raised.rb +28 -0
  90. data/lib/principles/views/map_result.rb +7 -6
  91. data/lib/principles/views/maps_needing_members_or_audit.rb +1 -1
  92. data/lib/principles/views/members_in_logged_requests_result.rb +75 -20
  93. data/lib/principles/views/meta_principle_result.rb +38 -13
  94. data/lib/principles/views/problem_lines.rb +22 -0
  95. data/lib/principles/views/prompts/assess-every-member/ask-whether-a-member-seen-only-as-read-is-loaded.md +3 -0
  96. data/lib/principles/views/prompts/assess-every-member/ask-whether-each-reason-holds.md +2 -0
  97. data/lib/principles/views/prompts/assess-every-member/judge-the-keeping-against-the-other-principles.md +2 -0
  98. data/lib/principles/views/prompts/assess-every-member/weigh-what-reached-the-context.md +2 -2
  99. data/lib/principles/views/prompts/dispose-of-each-finding/answer-guide.json +1 -1
  100. data/lib/principles/views/prompts/dispose-of-each-finding/fix-or-record-each-finding.md +1 -1
  101. data/lib/principles/views/prompts/dispose-of-each-finding/read-the-carrier-page-before-fixing.md +1 -0
  102. data/lib/principles/views/prompts/edit-a-carrier/read-the-page-before-the-first-edit.md +2 -0
  103. data/lib/principles/views/prompts/learn-what-a-command-does/add-member.md +19 -20
  104. data/lib/principles/views/prompts/learn-what-a-command-does/change-member-name.md +12 -0
  105. data/lib/principles/views/prompts/learn-what-a-command-does/change-member-reason.md +12 -0
  106. data/lib/principles/views/prompts/learn-what-a-command-does/every-command-and-how-they-chain.md +1 -1
  107. data/lib/principles/views/prompts/learn-what-a-command-does/list-member-problems.md +4 -4
  108. data/lib/principles/views/prompts/learn-what-a-command-does/remove-logged-requests.md +7 -1
  109. data/lib/principles/views/prompts/learn-what-a-command-does/report-at-session-start.md +7 -3
  110. data/lib/principles/views/prompts/learn-what-a-command-does/report-before-carrier-edit.md +13 -0
  111. data/lib/principles/views/prompts/learn-what-a-command-does/show-context-budget.md +69 -0
  112. data/lib/principles/views/prompts/learn-what-a-command-does/show-hook-setup.md +4 -4
  113. data/lib/principles/views/prompts/learn-what-a-command-does/show-logged-requests.md +4 -2
  114. data/lib/principles/views/prompts/learn-what-a-command-does/show-members-in-logged-requests.md +21 -7
  115. data/lib/principles/views/prompts/learn-what-a-command-does/show-meta-principle-passage.md +13 -10
  116. data/lib/principles/views/prompts/learn-what-a-command-does/show-principle.md +5 -5
  117. data/lib/principles/views/prompts/learn-what-a-command-does/submit-answer.md +7 -7
  118. data/lib/principles/views/prompts/learn-what-a-command-does/turn-off-request-logging.md +8 -0
  119. data/lib/principles/views/prompts/learn-what-a-command-does/turn-on-request-logging.md +12 -0
  120. data/lib/principles/views/prompts/read-what-reached-the-context/explain-user-request-logging-to-the-user.md +10 -0
  121. data/lib/principles/views/prompts/read-what-reached-the-context/what-a-thread-continuation-holds.md +3 -0
  122. data/lib/principles/views/prompts/read-what-reached-the-context/what-each-context-budget-line-says.md +4 -0
  123. data/lib/principles/views/prompts/read-what-reached-the-context/what-each-member-line-says.md +2 -0
  124. data/lib/principles/views/prompts/report-after-a-commit/a-row-ending-audit-asks-to-acknowledge-or-audit-again.md +1 -0
  125. data/lib/principles/views/prompts/report-after-a-commit/a-row-ending-mapping-commit-asks-for-an-audit.md +1 -0
  126. data/lib/principles/views/prompts/report-after-a-commit/a-row-ending-pinned-asks-for-an-audit.md +1 -0
  127. data/lib/principles/views/prompts/run-a-first-audit-in-the-background/follow-the-brief.md +1 -0
  128. data/lib/principles/views/prompts/set-principles-up/ask-to-set-principles-up-after-an-audit.md +1 -0
  129. data/lib/principles/views/prompts/set-principles-up/entry-file-passage.md +1 -1
  130. data/lib/principles/views/prompts/set-principles-up/map-the-enabling-line-at-no-edit.md +3 -0
  131. data/lib/principles/views/prompts/set-principles-up/name-the-step-each-member-lacks.md +2 -2
  132. data/lib/principles/views/prompts/set-principles-up/place-the-passage-with-the-user.md +4 -2
  133. data/lib/principles/views/prompts/set-principles-up/what-the-passage-presumes.md +1 -1
  134. data/lib/principles/views/prompts/set-principles-up/why-set-principles-up.md +1 -0
  135. data/lib/principles/views/prompts/start-a-session/ask-to-set-principles-up.md +2 -0
  136. data/lib/principles/views/prompts/start-a-session/name-the-principles-nothing-checks.md +1 -0
  137. data/lib/principles/views/request_logging_result.rb +48 -2
  138. data/lib/principles/views/set_up_offer_line.rb +43 -0
  139. metadata +55 -3
  140. data/lib/principles/views/prompts/set-principles-up/keep-them-held-between-audits.md +0 -2
@@ -2,7 +2,9 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require "json"
5
+ require "time"
5
6
  require_relative "logged_request_matches"
7
+ require_relative "request_shape"
6
8
 
7
9
  module Principles
8
10
  # Every file and process this tool reads or writes. data/CLAUDE.md owns the boundary.
@@ -14,6 +16,8 @@ module Principles
14
16
  class LoggedRequestScan
15
17
  REQUEST = "*.request.json"
16
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]
17
21
 
18
22
  # Each tool a request lists with defer_loading, as request<TAB>path: the host sends such a tool by name, and its
19
23
  # definition reaches the model only once a tool search loads it.
@@ -23,6 +27,22 @@ module Principles
23
27
  | "\($request)\ttools.\(.key)"
24
28
  JQ
25
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
+
26
46
  # DEFERRED, printing nothing, so a file it stops at fails it. It stands for both programs the other reads run:
27
47
  # LoggedRequestMatches::DECODE reads any whole JSON through, so a file it stops at DEFERRED stops at too, and
28
48
  # probing with it alone spares decoding every string of every request twice.
@@ -30,19 +50,38 @@ module Principles
30
50
 
31
51
  # Reads which request files the scan can read through once, here, so every read of this scan leaves the same ones
32
52
  # out. That runs jq as the scan is built, which is fine as a command builds one scan per run.
33
- #: (String) -> void
34
- def initialize(requests)
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)
35
56
  @requests = requests
57
+ @only = only
36
58
  @unreadable = unreadable_files.freeze #: Array[Hash[String, String]]
37
59
  freeze
38
60
  end
39
61
 
40
- # Each request file, by name, with the query_source its index line gives, or nil when the response failed and
41
- # the host wrote it none.
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.
42
65
  #: () -> Array[Hash[String, String?]]
43
66
  def requests
44
- sources = index_sources
45
- request_files.map { |name| { "file" => name, "query_source" => sources[name] } }
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)
46
85
  end
47
86
 
48
87
  # Each decoded line holding one of patterns, and each request's count of strings. Each pattern is one line; an
@@ -75,10 +114,31 @@ module Principles
75
114
  end
76
115
  end
77
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
+
78
131
  private
79
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
+
80
137
  #: () -> Array[String]
81
- def request_files = Dir.exist?(@requests) ? Dir.glob(REQUEST, base: @requests).sort : []
138
+ def request_files
139
+ found = Dir.exist?(@requests) ? Dir.glob(REQUEST, base: @requests).sort : []
140
+ @only ? found & @only : found
141
+ end
82
142
 
83
143
  # One jq run probes every request file, and each is probed alone only when that run fails. The reason is a fixed
84
144
  # code, never jq's message, which can quote a value from the request.
@@ -102,19 +162,27 @@ module Principles
102
162
  Process.last_status&.success? == true && printed.to_s.empty?
103
163
  end
104
164
 
105
- # The query_source of each request the index names, by its file name.
106
- #: () -> Hash[String, String?]
107
- def index_sources
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
108
168
  index = File.join(@requests, INDEX)
109
169
  return {} unless File.file?(index)
110
170
 
111
171
  File.foreach(index).filter_map do |line|
112
172
  entry = JSON.parse(line)
113
- [File.basename(entry.fetch("request_file")), entry["query_source"]] if entry.is_a?(Hash)
173
+ [File.basename(entry.fetch("request_file")), indexed(entry)] if entry.is_a?(Hash)
114
174
  rescue JSON::ParserError, KeyError, TypeError
115
175
  nil
116
176
  end.to_h
117
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
118
186
  end
119
187
  end
120
188
  end
@@ -16,7 +16,7 @@ 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 = 4
19
+ NEWEST_FORMAT = 5
20
20
 
21
21
  # The formats up to NEWEST_FORMAT a current read still refuses: 3 is an earlier map's history link, which only
22
22
  # parse_history reads.
@@ -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
@@ -2,7 +2,7 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require_relative "principles_directory"
5
- require_relative "logged_request_scan"
5
+ require_relative "captured_requests"
6
6
  require "fileutils"
7
7
  require "json"
8
8
  require "tempfile"
@@ -18,42 +18,12 @@ module Principles
18
18
  FOLDER = "#{PrinciplesDirectory::FOLDER}/#{NAME}".freeze #: String
19
19
  IGNORE = ".gitignore"
20
20
  SETTINGS = "settings.json"
21
+ # The sessions this project's session-start hook registered while request logging is on in the user's own
22
+ # settings, one JSON object a line.
23
+ REGISTRY = "sessions.jsonl"
21
24
  REQUESTS = "requests"
22
25
  # The host's one line per response it received, naming the request and response files and the session.
23
- INDEX = "#{REQUESTS}/index.jsonl".freeze #: String
24
-
25
- # A file the host wrote under requests/: its name there, its size and when it was last written.
26
- class CapturedFile
27
- #: String
28
- attr_reader :name
29
- #: Integer
30
- attr_reader :bytes
31
- #: Time
32
- attr_reader :modified_at
33
-
34
- #: (name: String, bytes: Integer, modified_at: Time) -> void
35
- def initialize(name:, bytes:, modified_at:)
36
- @name = name
37
- @bytes = bytes
38
- @modified_at = modified_at
39
- freeze
40
- end
41
- end
42
-
43
- # One line of the host's index: the session, when, and the paths of the request and response files it names.
44
- class IndexLine
45
- #: String
46
- attr_reader :session_id, :timestamp, :request_file, :response_file
47
-
48
- #: (Hash[String, String]) -> void
49
- def initialize(fields)
50
- @session_id = fields.fetch("session_id") #: String
51
- @timestamp = fields.fetch("timestamp") #: String
52
- @request_file = fields.fetch("request_file") #: String
53
- @response_file = fields.fetch("response_file") #: String
54
- freeze
55
- end
56
- end
26
+ INDEX = "#{REQUESTS}/#{CapturedRequests::INDEX}".freeze #: String
57
27
 
58
28
  #: (String) -> void
59
29
  def initialize(root)
@@ -80,25 +50,17 @@ module Principles
80
50
  Dir.glob("**/*", File::FNM_DOTMATCH, base: base).select { |name| File.file?(File.join(base, name)) }.sort
81
51
  end
82
52
 
53
+ # The host's requests/ folder here, with what it wrote there.
54
+ #: () -> CapturedRequests
55
+ def requests = CapturedRequests.new(File.join(@directory.path, REQUESTS), shown_as: relative(REQUESTS))
56
+
83
57
  # Every file the host wrote under requests/, but its index, sorted by name.
84
- #: () -> Array[CapturedFile]
85
- def captured_files
86
- base = File.join(@directory.path, REQUESTS)
87
- (Dir.exist?(base) ? Dir.children(base).sort : []).filter_map do |name|
88
- path = File.join(base, name)
89
- next if name == File.basename(INDEX) || !File.file?(path)
90
-
91
- stat = File.stat(path)
92
- CapturedFile.new(name: name, bytes: stat.size, modified_at: stat.mtime.utc)
93
- end
94
- end
58
+ #: () -> Array[CapturedRequests::CapturedFile]
59
+ def captured_files = requests.captured_files
95
60
 
96
61
  # Each line of the host's index, leaving out a line that is not a JSON object naming a session and its files.
97
- #: () -> Array[IndexLine]
98
- def index_lines
99
- path = File.join(@directory.path, INDEX)
100
- File.file?(path) ? IndexLines.new(File.read(path)).lines : []
101
- end
62
+ #: () -> Array[CapturedRequests::IndexLine]
63
+ def index_lines = requests.index_lines
102
64
 
103
65
  # Writes name, creating the folder readable only by its owner, since what it holds is a whole session's context.
104
66
  #: (String, String) -> void
@@ -112,6 +74,23 @@ module Principles
112
74
  end
113
75
  end
114
76
 
77
+ # Each line of the registry, as written; none while it is absent.
78
+ #: () -> Array[String]
79
+ def registry_lines
80
+ path = File.join(@directory.path, REGISTRY)
81
+ File.file?(path) ? File.readlines(path, chomp: true) : []
82
+ end
83
+
84
+ # Each session the registry names, in the order it was registered, leaving out a line that names none.
85
+ #: () -> Array[String]
86
+ def registered_session_ids = RegistryLines.new(registry_lines).session_ids
87
+
88
+ # Adds line to the end of the registry, so two sessions starting at once each keep theirs.
89
+ #: (String) -> void
90
+ def append_to_registry(line)
91
+ File.open(File.join(@directory.path, REGISTRY), "a", 0o600) { it.write("#{line}\n") }
92
+ end
93
+
115
94
  # Writes the launch file, its fields as JSON.
116
95
  #: (Hash[String, untyped]) -> void
117
96
  def write_settings(fields) = write(SETTINGS, "#{JSON.pretty_generate(fields)}\n")
@@ -119,20 +98,13 @@ module Principles
119
98
  #: (String) -> void
120
99
  def remove(name) = File.delete(File.join(@directory.path, name))
121
100
 
122
- # Removes one file the host wrote directly under requests/, refusing any other name, an index line's included,
123
- # so a line naming a path elsewhere or the index itself cannot reach it.
101
+ # Removes one file the host wrote directly under requests/, refusing any other name.
124
102
  #: (String) -> void
125
- def remove_request(name)
126
- unless name.match?(%r{\A[^/]+\z}) && !%w[. ..].include?(name) && name != File.basename(INDEX)
127
- raise ArgumentError, "not a captured file under #{REQUESTS}/: #{name.inspect}"
128
- end
129
-
130
- remove("#{REQUESTS}/#{name}")
131
- end
103
+ def remove_request(name) = requests.remove_request(name)
132
104
 
133
105
  # The requests the host captured here, scanned by jq and rg.
134
106
  #: () -> LoggedRequestScan
135
- def scan = LoggedRequestScan.new(File.join(@directory.path, REQUESTS))
107
+ def scan = requests.scan
136
108
 
137
109
  # Removes the folder when nothing is left in it.
138
110
  #: () -> void
@@ -141,23 +113,19 @@ module Principles
141
113
  end
142
114
  end
143
115
 
144
- # The host's index text read as lines, shared by the folder and its stand-in.
145
- class IndexLines
146
- FIELDS = %w[session_id timestamp request_file response_file].freeze #: Array[String]
147
-
148
- #: (String) -> void
149
- def initialize(text)
150
- @text = text
116
+ # The registry's lines read as the sessions they name, shared by the folder and its stand-in.
117
+ class RegistryLines
118
+ #: (Array[String]) -> void
119
+ def initialize(lines)
120
+ @lines = lines
151
121
  freeze
152
122
  end
153
123
 
154
- #: () -> Array[RequestLoggingFolder::IndexLine]
155
- def lines
156
- @text.each_line.filter_map do |line|
124
+ #: () -> Array[String]
125
+ def session_ids
126
+ @lines.filter_map do |line|
157
127
  fields = JSON.parse(line)
158
- next unless fields.is_a?(Hash) && FIELDS.all? { |field| fields[field].is_a?(String) }
159
-
160
- RequestLoggingFolder::IndexLine.new(fields)
128
+ fields["session_id"] if fields.is_a?(Hash) && fields["session_id"].is_a?(String)
161
129
  rescue JSON::ParserError
162
130
  nil
163
131
  end
@@ -0,0 +1,33 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ module Principles
5
+ # Every file and process this tool reads or writes. data/CLAUDE.md owns the boundary.
6
+ module Data
7
+ # A captured request's shape, as the scan reads it: how many tools it lists, how many messages it holds, how its
8
+ # last message ends, text, tool_result or other, and its thread role, continues when it names the message before
9
+ # it in the host's server-side thread, starts when its thread names none, and none without one: each a fixed
10
+ # word, never the request's own text. A plain class, not ::Data.define, since Sorbet's strict level needs a
11
+ # signature for each reader.
12
+ class RequestShape
13
+ #: (tools: Integer, messages: Integer, ending: String, ?thread: String) -> void
14
+ def initialize(tools:, messages:, ending:, thread: "none")
15
+ @tools = tools
16
+ @messages = messages
17
+ @ending = ending
18
+ @thread = thread
19
+ freeze
20
+ end
21
+
22
+ #: Integer
23
+ attr_reader :tools, :messages
24
+
25
+ #: String
26
+ attr_reader :ending, :thread
27
+
28
+ # Whether the request continues a thread, sending only what it adds to the one before.
29
+ #: () -> bool
30
+ def continues_thread? = @thread == "continues"
31
+ end
32
+ end
33
+ end
@@ -12,7 +12,7 @@ module Principles
12
12
  class RunningVersion
13
13
  # git archive writes the short sha of the commit it archives here, since .gitattributes marks this file
14
14
  # export-subst; a checkout keeps the placeholder as written.
15
- ARCHIVED_COMMIT = "e3a7f9f7" #: String
15
+ ARCHIVED_COMMIT = "bc6c8da7" #: String
16
16
 
17
17
  #: (?String, release: String, ?built: String?, ?archived_commit: String) -> void
18
18
  def initialize(checkout = File.expand_path("../../..", __dir__), release:, built: nil,
@@ -72,7 +72,7 @@
72
72
  "usage": { "$ref": "#/$defs/prose" },
73
73
  "group": { "$ref": "#/$defs/slug" },
74
74
  "options": { "type": "array", "uniqueItems": true,
75
- "items": { "enum": ["description", "path", "section", "starting-at", "ending-at", "query", "plugin", "role", "check", "file", "short", "session-start", "commits-after", "hook-event", "member", "when-files", "when-invoked", "always", "write-ups", "as", "instruction", "part", "also", "only", "confirm", "lines", "open-findings", "history", "loads", "keep-sessions", "findings", "places"] } },
75
+ "items": { "enum": ["description", "path", "section", "starting-at", "ending-at", "query", "plugin", "role", "name", "reason", "check", "file", "short", "session-start", "commits-after", "hook-event", "member", "when-files", "when-invoked", "always", "write-ups", "as", "instruction", "part", "also", "only", "confirm", "lines", "open-findings", "history", "loads", "keep-sessions", "findings", "places", "for-user", "session"] } },
76
76
  "requires": { "type": "array", "uniqueItems": true, "items": { "enum": ["file"] } },
77
77
  "summary": { "$ref": "#/$defs/prose" },
78
78
  "help": { "type": "string", "pattern": "^[a-z]+(-[a-z]+)+/[a-z]+(-[a-z]+)+$" },
@@ -30,6 +30,8 @@
30
30
  "query": { "type": "string", "minLength": 1 },
31
31
  "ending_at": { "type": "string", "minLength": 1 },
32
32
  "role": { "type": "string", "minLength": 1 },
33
+ "name": { "type": "string", "minLength": 1, "description": "A short name stating the member's intent, shown first in output. Display only: commands take the member's id, never its name." },
34
+ "reason": { "type": "string", "minLength": 1, "description": "Why this member's carrier and place put the principle in context when its work is in hand and keep it out otherwise, ending in the observation that would show it false, named by host channel or seen_only_as_read. A full audit asks whether it still holds." },
33
35
  "key": { "type": "string", "minLength": 1, "description": "What the member is known by across moves, which its audit record files its hash under. Absent, it is the member's id. move-member stores the old id here on a member's first move, and add-member gives a member a key of its own when its id is the key of a member that moved away." },
34
36
  "check": { "type": "string", "minLength": 1, "description": "The command that enforces this member, which an audit asks the agent to run and report. This tool never runs it." },
35
37
  "pinned": {
@@ -0,0 +1,26 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require "json"
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 names of the env entries a Claude Code settings file sets, never their values, since the file can hold the
10
+ # user's tokens.
11
+ module SettingsEnvKeys
12
+ # Each name the text's env sets, none when it sets no env, or nil when the text is not a JSON object whose env
13
+ # is an object.
14
+ #: (String) -> Array[String]?
15
+ def self.of(text)
16
+ settings = JSON.parse(text)
17
+ return nil unless settings.is_a?(Hash)
18
+
19
+ env = settings.fetch("env", {})
20
+ env.is_a?(Hash) ? env.keys : nil
21
+ rescue JSON::ParserError
22
+ nil
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,73 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require "fileutils"
5
+ require_relative "request_logging_folder"
6
+
7
+ module Principles
8
+ # Every file and process this tool reads or writes. data/CLAUDE.md owns the boundary.
9
+ module Data
10
+ # The user-level folder the host writes every project's requests to while request logging is on in the user's own
11
+ # settings: this user's state, outside every repository, so no project's working tree holds another's requests.
12
+ class UserRequestLoggingFolder
13
+ # The folder under the user's state folder: XDG_STATE_HOME when set, or else .local/state in the home directory.
14
+ #: (?Hash[String, String], ?home: String) -> UserRequestLoggingFolder
15
+ def self.from_environment(environment = ENV.to_h, home: Dir.home)
16
+ state = environment["XDG_STATE_HOME"].to_s
17
+ new(File.join(state.empty? ? File.join(home, ".local", "state") : state, "principles", "request-logging"))
18
+ end
19
+
20
+ #: String
21
+ attr_reader :path
22
+
23
+ #: (String) -> void
24
+ def initialize(path)
25
+ @path = path
26
+ freeze
27
+ end
28
+
29
+ #: () -> bool
30
+ def exist? = File.directory?(@path)
31
+
32
+ # Makes the folder and its requests/, each readable only by this user, tightening either that was wider, since
33
+ # the host writes each request readable by every user (022 F2); gives whether the folder was new.
34
+ #: () -> bool
35
+ def create
36
+ made = !exist?
37
+ [@path, requests_path].each do |folder|
38
+ FileUtils.mkdir_p(folder)
39
+ File.chmod(0o700, folder)
40
+ end
41
+ made
42
+ end
43
+
44
+ # The host's requests/ folder here, where it writes every project's requests.
45
+ #: () -> CapturedRequests
46
+ def requests = CapturedRequests.new(requests_path)
47
+
48
+ # The host's requests/ folder here, by its path, as the user's settings name it.
49
+ #: () -> String
50
+ def requests_path = File.join(@path, RequestLoggingFolder::REQUESTS)
51
+
52
+ # The request-logging folder of the checkout at root, which holds the sessions registered to its repository.
53
+ #: (String) -> RequestLoggingFolder
54
+ def registry_folder(root) = RequestLoggingFolder.new(root)
55
+
56
+ # The main checkouts whose hook registered a session, one root a line, so a prune can read every registry.
57
+ PROJECTS = "projects"
58
+
59
+ # Each main checkout listed, in the order it was added; none while the list is absent.
60
+ #: () -> Array[String]
61
+ def project_roots
62
+ path = File.join(@path, PROJECTS)
63
+ File.file?(path) ? File.readlines(path, chomp: true).reject(&:empty?) : []
64
+ end
65
+
66
+ # Adds root to the end of the list.
67
+ #: (String) -> void
68
+ def add_project_root(root)
69
+ File.open(File.join(@path, PROJECTS), "a", 0o600) { it.write("#{root}\n") }
70
+ end
71
+ end
72
+ end
73
+ end
@@ -8,12 +8,12 @@ module Principles
8
8
  # against what its command takes.
9
9
  class InvocationOptions
10
10
  # The options that take a value; --help, -h and --version take none.
11
- VALUE_OPTIONS = %w[project file description path section starting-at ending-at query role
12
- check plugin session-start commits-after hook-event as instruction
13
- keep-sessions].freeze #: Array[String]
11
+ VALUE_OPTIONS = %w[project file description path section starting-at ending-at query role name
12
+ reason check plugin session-start commits-after hook-event as instruction
13
+ keep-sessions session].freeze #: Array[String]
14
14
  # The options that take no value, as list-member-problems --short.
15
15
  FLAG_OPTIONS = %w[short lines when-invoked always write-ups confirm open-findings history
16
- loads findings places].freeze #: Array[String]
16
+ loads findings places for-user].freeze #: Array[String]
17
17
  # The options that may be given more than once, each value kept in order, as change-activation --when-files and
18
18
  # start-audit --member and --part.
19
19
  LIST_OPTIONS = %w[when-files also only member part].freeze #: Array[String]
@@ -6,16 +6,24 @@ require_relative "version"
6
6
  require_relative "data/running_version"
7
7
  require_relative "data/principles_on_path"
8
8
  require_relative "data/tools_on_path"
9
+ require_relative "controllers/command"
9
10
  require_relative "controllers/running_tool"
10
11
 
11
12
  module Principles
12
- # This process as its entry point launched it: the executable that entry point names itself by, the file it was
13
- # launched as, PATH, the folder it started in and its temporary folder, read here so that no layer reads them. It
14
- # gives every controller the same RunningTool.
13
+ # This process as its entry point launched it: the executable that entry point names itself by, the Gemfile bundler
14
+ # runs it under, the file it was launched as, PATH, the folder it started in and its temporary folder, read here so
15
+ # that no layer reads them. It gives every controller the same RunningTool.
15
16
  class Launch
16
- #: (String) -> void
17
- def initialize(executable)
17
+ # This process as exe/principles launched it, under bundle exec or not: a BUNDLE_GEMFILE left set with no
18
+ # bundler loaded is not a run under bundler. Only the entry point asks, so a run in process keeps its own.
19
+ #: () -> Launch
20
+ def self.of_this_process = new(bundle_gemfile: (ENV.fetch("BUNDLE_GEMFILE", nil) if defined?(::Bundler)))
21
+
22
+ # bundle_gemfile is the Gemfile bundler runs this process under, or nil when it runs without bundler.
23
+ #: (?String, ?bundle_gemfile: String?) -> void
24
+ def initialize(executable = Controllers::Command::INVOCATION, bundle_gemfile: nil)
18
25
  @executable = executable
26
+ @bundle_gemfile = bundle_gemfile
19
27
  freeze
20
28
  end
21
29
 
@@ -25,7 +33,8 @@ module Principles
25
33
  def running_tool
26
34
  Controllers::RunningTool.new(executable: @executable, version: -> { running_version }, on_path: path_says,
27
35
  folders: Controllers::RunningTool::Folders.new(cwd: started_in,
28
- temp_directory: Dir.tmpdir))
36
+ temp_directory: Dir.tmpdir),
37
+ bundle_gemfile: @bundle_gemfile)
29
38
  end
30
39
 
31
40
  private