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
@@ -121,7 +121,8 @@ module Principles
121
121
  return map unless map.members.empty?
122
122
 
123
123
  raise Error.new("map_has_no_members", "#{id} has no members to audit",
124
- "Add its members: #{Error::EXECUTABLE} add-member #{id} PATH --role ROLE", file(id))
124
+ "Add its members: #{Error::EXECUTABLE} add-member #{id} PATH --role ROLE " \
125
+ "--name NAME --reason REASON", file(id))
125
126
  end
126
127
 
127
128
  # Writes map's file and names it, relative to the project.
@@ -32,7 +32,7 @@ module Principles
32
32
  # A new member field that changes what an audit asks or records gets the next format, and
33
33
  # Data::MapFiles::NEWEST_FORMAT moves with it.
34
34
  FIELD_FORMATS = { "path" => 1, "section" => 1, "starting_at" => 1, "ending_at" => 1, "role" => 1,
35
- "key" => 1, "check" => 2, "query" => 2,
35
+ "key" => 1, "check" => 2, "query" => 2, "name" => 5, "reason" => 5,
36
36
  "pinned" => 4 }.freeze #: Hash[String, Integer]
37
37
 
38
38
  # The fields that say where a member's text is; a move replaces them whole.
@@ -113,6 +113,11 @@ module Principles
113
113
  @fields["role"]
114
114
  end
115
115
 
116
+ # The short name stating its intent, which output leads with, as {"name" => NAME}; empty for a member with none.
117
+ # Display only: no command finds a member by it.
118
+ #: () -> Hash[String, String]
119
+ def named = @fields.slice("name")
120
+
116
121
  # What add-member read of a plugin's file when it mapped it, its version and the hash of its text, which no
117
122
  # commit holds; nil for a project file.
118
123
  #: () -> Hash[String, String]?
@@ -0,0 +1,52 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "../error"
5
+ require_relative "../prose_list"
6
+
7
+ module Principles
8
+ # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
9
+ module Models
10
+ # What justifies a new member's place in its principle: a name stating its intent and a reason its carrier puts the
11
+ # principle in context only when its work is in hand. A principle bloats when members are added at will, so a new
12
+ # member needs both.
13
+ class MemberJustification
14
+ # The options that justify a member, as the agent types them.
15
+ OPTIONS = %w[--name --reason].freeze #: Array[String]
16
+
17
+ # One a reason may follow: what it says, ending in what would show it false.
18
+ EXAMPLE = "--name \"Name things for their purpose\" --reason \"CLAUDE.md loads in every session, and naming is " \
19
+ "in hand in every session; false if no turn opening holds it in a reminder, or only " \
20
+ "seen_only_as_read lists it\""
21
+
22
+ # given is the name and reason the agent gave, by field, each absent when not given.
23
+ #: (Hash[String, untyped]) -> void
24
+ def initialize(given)
25
+ @given = given
26
+ freeze
27
+ end
28
+
29
+ # Refuses unless both a name and a reason are given, naming those missing; one left as its placeholder in a
30
+ # printed command, NAME or REASON, is missing.
31
+ #: () -> void
32
+ def check
33
+ missing = OPTIONS.reject { |option| given?(option.delete_prefix("--")) }
34
+ return if missing.empty?
35
+
36
+ raise Error.new("unjustified_member",
37
+ "A new member needs --name and --reason; #{ProseList.new(missing)} " \
38
+ "#{missing.one? ? "is" : "are"} missing",
39
+ "Give --name, a short name stating what the member makes the agent do, and --reason, why " \
40
+ "its carrier and place put the principle in context when its work is in hand and keep it " \
41
+ "out otherwise, ending in the observation that would show it false, by host channel or " \
42
+ "seen_only_as_read. For example: #{EXAMPLE}")
43
+ end
44
+
45
+ private
46
+
47
+ # Whether field was given as more than the placeholder a printed command leaves for it, NAME or REASON.
48
+ #: (String) -> bool
49
+ def given?(field) = !@given[field].nil? && @given[field] != field.upcase
50
+ end
51
+ end
52
+ end
@@ -11,6 +11,7 @@ require_relative "member_state"
11
11
  require_relative "locate_refusal"
12
12
  require_relative "mapped_baseline"
13
13
  require_relative "member_entry"
14
+ require_relative "member_snapshots"
14
15
 
15
16
  module Principles
16
17
  # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
@@ -118,7 +119,7 @@ module Principles
118
119
  map = project.principles.find(id)
119
120
  members = map.members
120
121
  mapping = PrincipleMappingHistory.load_if_linked(current: map, project: project)
121
- now = project.current_by_key(members)
122
+ now = MemberSnapshots.hashes_by_key(project, members)
122
123
  new(members: members, now: now, baselines: Baselines.of(project, members, mapping: mapping),
123
124
  sources: plugin_sources(project, members, now))
124
125
  rescue Error => e
@@ -1,6 +1,8 @@
1
1
  # typed: strict
2
2
  # frozen_string_literal: true
3
3
 
4
+ require_relative "request_channel"
5
+
4
6
  module Principles
5
7
  # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
6
8
  module Models
@@ -14,14 +16,15 @@ module Principles
14
16
  freeze
15
17
  end
16
18
 
17
- # Its key, its id where it is now once moved, status, the requests seen holding its lines and its most bytes in
18
- # one.
19
+ # Its key, its id where it is now once moved, status, the requests seen holding its lines, how many of them held
20
+ # it by each channel it came by, and its most bytes in one.
19
21
  #: () -> Hash[String, untyped]
20
22
  def to_h
21
- by_request = @member.fetch("seen").group_by { it.fetch("request") }.values
23
+ by_request = seen.group_by { it.fetch("request") }.values
22
24
  bytes = by_request.map { |places| places.sum { it.fetch("bytes") } }
23
25
  { **@member.slice("member", "id"),
24
- "status" => @member.fetch("status"), "seen_in" => bytes.size, "most_bytes" => bytes.max || 0 }
26
+ "status" => @member.fetch("status"), "seen_in" => bytes.size, "channels" => channels,
27
+ "most_bytes" => bytes.max || 0 }
25
28
  end
26
29
 
27
30
  # Its reach as to_h gives it, after its principle, with the segments it was seen in, each once: bounded by the
@@ -29,7 +32,27 @@ module Principles
29
32
  #: () -> Hash[String, untyped]
30
33
  def listed
31
34
  { "principle" => @member.fetch("principle"), **to_h,
32
- "segments" => @member.fetch("seen").map { it.fetch("segment") }.uniq }
35
+ "segments" => seen.map { it.fetch("segment") }.uniq }
36
+ end
37
+
38
+ # Whether it was seen, but in no channel the host loads by, so only where the agent read, sent or quoted it.
39
+ #: () -> bool
40
+ def seen_only_as_read?
41
+ held = channels.keys
42
+ held.any? && !held.intersect?(RequestChannel::HOST)
43
+ end
44
+
45
+ private
46
+
47
+ #: () -> Array[Hash[String, untyped]]
48
+ def seen = @member.fetch("seen")
49
+
50
+ # How many requests held its lines by each channel, a request counted once a channel, in RequestChannel's order.
51
+ #: () -> Hash[String, Integer]
52
+ def channels
53
+ requests = seen.group_by { it.fetch("channel") }
54
+ .transform_values { |places| places.map { it.fetch("request") }.uniq.size }
55
+ requests.sort_by { |name, _| RequestChannel::NAMES.index(name).to_i }.to_h
33
56
  end
34
57
  end
35
58
  end
@@ -25,6 +25,17 @@ module Principles
25
25
  new(found)
26
26
  end
27
27
 
28
+ # Each of members' hash now by key, or the refusal that says why its text cannot be found or read, so every
29
+ # member is listed whether or not it can be located.
30
+ #: (Project, PrincipleMembers) -> Hash[String, untyped]
31
+ def self.hashes_by_key(project, members)
32
+ members.to_h do |member|
33
+ [member.key, project.located_text(member.locator).sha256]
34
+ rescue Error => e
35
+ [member.key, e.payload]
36
+ end
37
+ end
38
+
28
39
  # Each of members located by id, and the entry of each that cannot be, with its refusal: the project's
29
40
  # members apart from the plugin members no installed plugin gives.
30
41
  #: (Project, PrincipleMembers)
@@ -0,0 +1,67 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ module Principles
5
+ # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
6
+ module Models
7
+ # The bytes of mapped members' text one request sent, by principle and by the part of its context each was sent
8
+ # in, from where MembersInLoggedRequests saw them. Each occurrence costs its bytes, so a member sent twice counts
9
+ # twice in bytes and once among the members. It gives principle ids and counts alone, never text (034).
10
+ class MembersByPart
11
+ # The part of a request's context each host channel sends; any other channel is the conversation's.
12
+ PARTS = { "system" => "system_prompt", "tool_definition" => "tool_definitions", "reminder" => "reminders",
13
+ "system_message" => "system_notices" }.freeze #: Hash[String, String]
14
+ # The parts in the order ContextParts lists them.
15
+ ORDER = %w[system_prompt tool_definitions deferred_tool_definitions reminders system_notices
16
+ conversation].freeze #: Array[String]
17
+
18
+ # Each request's members by part, by request file, from members as MembersInLoggedRequests reports them, each
19
+ # with the places it was seen, and deferred, each tool a request defers loading as [request, path].
20
+ #: (Array[Hash[String, untyped]], ?Array[[String, String]]) -> Hash[String, MembersByPart]
21
+ def self.by_request(members, deferred = [])
22
+ places = members.flat_map { |member| member.fetch("seen").map { [member, it] } }
23
+ places.group_by { |_, place| place.fetch("request").to_s }.to_h do |request, held|
24
+ tools = deferred.filter_map { |at, path| path if at == request }
25
+ [request, new(held.map { |member, place| sent(member, place, tools) })]
26
+ end
27
+ end
28
+
29
+ # Several requests' members by part together, as a session's turn openings sent them.
30
+ #: (Array[MembersByPart]) -> MembersByPart
31
+ def self.together(each) = new(each.flat_map(&:held))
32
+
33
+ # Where member was seen at place, as [principle, member, part, bytes]: a tool among deferred, the paths of
34
+ # those the request defers loading, is its own part.
35
+ #: (Hash[String, untyped], Hash[String, untyped], Array[String]) -> [String, String, String, Integer]
36
+ def self.sent(member, place, deferred)
37
+ path = place.fetch("path").to_s
38
+ part = if deferred.any? { path == it || path.start_with?("#{it}.") } then "deferred_tool_definitions"
39
+ else PARTS.fetch(place.fetch("channel"), "conversation")
40
+ end
41
+ [member.fetch("principle"), member.fetch("member"), part, place.fetch("bytes")]
42
+ end
43
+
44
+ # held is each place a member was seen, as [principle, member, part, bytes].
45
+ #: (Array[[String, String, String, Integer]]) -> void
46
+ def initialize(held)
47
+ @held = held
48
+ freeze
49
+ end
50
+
51
+ #: Array[[String, String, String, Integer]]
52
+ attr_reader :held
53
+
54
+ # Each principle's members seen in each part, how many distinct members and the bytes of every occurrence, by
55
+ # principle and in the parts' order.
56
+ #: () -> Array[Hash[String, untyped]]
57
+ def to_a
58
+ @held.group_by { |principle, _, part| [principle, part] }
59
+ .sort_by { |(principle, part), _| [principle, ORDER.index(part) || ORDER.size] }
60
+ .map do |(principle, part), seen|
61
+ { "principle" => principle, "part" => part, "members" => seen.map { it[1] }.uniq.size,
62
+ "bytes" => seen.sum(&:last) }
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -7,6 +7,10 @@ require_relative "carrier_kind"
7
7
  require_relative "member"
8
8
  require_relative "member_text_as_sent"
9
9
  require_relative "misspelt_member_id"
10
+ require_relative "request_channel"
11
+ require_relative "request_kind"
12
+ require_relative "request_time"
13
+ require_relative "turn_position"
10
14
 
11
15
  module Principles
12
16
  # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
@@ -56,18 +60,21 @@ module Principles
56
60
  seen.all? { inside_any?(it, deferred) } ? "present_but_not_loaded" : "present"
57
61
  end
58
62
 
59
- # Where its lines were seen among matches, each place a request, path and segment, with the share of its lines
60
- # there, the bytes of every occurrence of them, how many occurrences there are, and the byte offset of each in
61
- # its string. holding gives the patterns each matched line holds.
62
- #: (Array[Data::KeptLine], Hash[String, String?], Hash[String, Array[String]])
63
+ # Where its lines were seen among matches, in the order their requests were sent, each place a request, with
64
+ # what requests says of it by file, a path, segment and channel, with the share of its lines there, the bytes
65
+ # of every occurrence of them, how many occurrences there are, and the byte offset of each in its string.
66
+ # holding gives the patterns each matched line holds.
67
+ #: (Array[Data::KeptLine], Hash[String, Hash[String, String?]], Hash[String, Array[String]])
63
68
  #| -> Array[Hash[String, untyped]]
64
- def seen(matches, sources, holding)
65
- found = matches.group_by(&:place)
66
- .transform_values { occurrences(it, holding) }
67
- found.reject { |_, occurring| occurring.empty? }.map do |(request, path, segment), occurring|
68
- { "request" => request, "query_source" => sources[request], "path" => path, "segment" => segment,
69
- **measures(occurring) }
69
+ def seen(matches, requests, holding)
70
+ places = matches.group_by(&:place).filter_map do |(request, path, segment), kept|
71
+ occurring = occurrences(kept, holding)
72
+ next if occurring.empty?
73
+
74
+ { "request" => request, **about(requests.fetch(request, {})), "path" => path, "segment" => segment,
75
+ "channel" => RequestChannel.new(path, segment, role: kept.fetch(0).role).to_s, **measures(occurring) }
70
76
  end
77
+ in_sent_order(places)
71
78
  end
72
79
 
73
80
  # Its id, where it is now.
@@ -84,6 +91,22 @@ module Principles
84
91
 
85
92
  private
86
93
 
94
+ # What a place says of the request it is in, from request as the scan labelled it: when it was sent, its
95
+ # query_source, its kind and, for a model turn, its turn position.
96
+ #: (Hash[String, String?]) -> Hash[String, String?]
97
+ def about(request)
98
+ sent = request.slice("request_at", "request_at_from")
99
+ { **sent, "query_source" => request["query_source"], "request_kind" => request["kind"] }
100
+ .merge(request.slice("turn_position"))
101
+ end
102
+
103
+ # places in the order their requests were sent, as RequestTime compares them, each keeping the order it was
104
+ # found in among those sent at the same time.
105
+ #: (Array[Hash[String, untyped]]) -> Array[Hash[String, untyped]]
106
+ def in_sent_order(places)
107
+ places.each_with_index.sort_by { |place, found| [RequestTime.new(place["request_at"]), found] }.map(&:first)
108
+ end
109
+
87
110
  # Whether place, one of where it was seen, lies inside one of tools.
88
111
  #: (Hash[String, untyped], Array[[String, String]]) -> bool
89
112
  def inside_any?(place, tools)
@@ -151,7 +174,7 @@ module Principles
151
174
  # carrier it is.
152
175
  #: (Project, String, PrincipleMembers) { (Member) -> String } -> Array[Measured]
153
176
  def self.measured(project, principle, members, &name)
154
- loads = project.member_loads(members)
177
+ loads = project.harness.member_loads(members)
155
178
  members.map do |member|
156
179
  Measured.new(principle:, member: name.call(member), mapped: member,
157
180
  text: MemberTextAsSent.of(project, member.locator).to_s,
@@ -183,15 +206,21 @@ module Principles
183
206
  def patterns = [*@members.flat_map(&:lines), QUOTED].uniq
184
207
 
185
208
  # How many members have each status, every status counted, then each member with where it was seen, given the
186
- # scan's requests, its matches and the tools it found deferred, how many requests the index gives no
187
- # query_source, and which requests quote a logged request's path.
209
+ # scan's requests, each with its kind where its shape was read, its matches and the tools it found deferred, how
210
+ # many requests the index gives no query_source, which requests quote a logged request's path, and how many
211
+ # requests are of each kind, and how many model turns are at each turn position, in all and by query_source.
188
212
  #: (Array[Hash[String, String?]], Array[Data::KeptLine], Array[[String, String]])
189
213
  #| -> { members_by_status: Hash[String, Integer], members: Array[Hash[String, untyped]], orphans: Integer,
190
- #| quoting_requests: Array[String] }
214
+ #| quoting_requests: Array[String], requests_by_kind: Hash[String, Integer],
215
+ #| model_turns_by_position: Hash[String, Integer],
216
+ #| model_turns_by_query_source: Hash[String, Hash[String, Integer]] }
191
217
  def report(requests, matches, deferred)
192
218
  members = members_seen(requests, matches, deferred)
193
219
  { members_by_status: by_status(members), members: members, orphans: requests.count { it["query_source"].nil? },
194
- quoting_requests: matches.select { QUOTING.match?(it.text) }.map(&:request).uniq.sort }
220
+ quoting_requests: matches.select { QUOTING.match?(it.text) }.map(&:request).uniq.sort,
221
+ requests_by_kind: RequestKind::NAMES.to_h { |kind| [kind, requests.count { it["kind"] == kind }] },
222
+ model_turns_by_position: TurnPosition.counted(requests),
223
+ model_turns_by_query_source: TurnPosition.counted_by_query_source(requests) }
195
224
  end
196
225
 
197
226
  private
@@ -214,15 +243,25 @@ module Principles
214
243
  #: (Array[Hash[String, String?]], Array[Data::KeptLine], Array[[String, String]])
215
244
  #| -> Array[Hash[String, untyped]]
216
245
  def members_seen(requests, matches, deferred)
217
- sources = requests.to_h { [it.fetch("file").to_s, it["query_source"]] }
246
+ by_file = requests.to_h { [it.fetch("file").to_s, with_request_at(it)] }
218
247
  holding = patterns_held(matches)
219
248
  @members.map do |member|
220
- seen = member.seen(matches, sources, holding)
249
+ seen = member.seen(matches, by_file, holding)
221
250
  { "principle" => member.principle, "member" => member.member, **moved_to(member),
222
251
  "status" => member.status(seen, deferred), "seen" => seen }
223
252
  end
224
253
  end
225
254
 
255
+ # request with when it was sent, in UTC, from its index line's timestamp, or nil for none; a request with no
256
+ # index line takes when its file was written, and says so, since a file is written after its request is sent.
257
+ #: (Hash[String, String?]) -> Hash[String, String?]
258
+ def with_request_at(request)
259
+ written = request["written_at"]
260
+ return request.merge("request_at" => RequestTime.new(request["timestamp"]).utc) unless written
261
+
262
+ request.merge("request_at" => RequestTime.new(written).utc, "request_at_from" => "file_written")
263
+ end
264
+
226
265
  # Where member is now, as its id, once a move has left its key naming where it was; nothing while they agree.
227
266
  #: (Measured) -> Hash[String, String]
228
267
  def moved_to(member) = member.id == member.member ? {} : { "id" => member.id }
@@ -14,9 +14,9 @@ module Principles
14
14
  ID = "sticking-to-your-principles"
15
15
 
16
16
  DESCRIPTION = "Every other principle stays held: each place that carries one is mapped, a change to any of " \
17
- "them is judged against the rest before work goes on, and the agent loads only the guidance its " \
18
- "task needs, so harness drift and debt stay visible and an agent can be trusted with longer, " \
19
- "wider unsupervised work."
17
+ "them is judged against the rest before work goes on, and each member is in context when its " \
18
+ "work is in hand and out of it otherwise, so usable context stays as large as it can, harness " \
19
+ "drift and debt stay visible, and an agent can be trusted with longer, wider unsupervised work."
20
20
 
21
21
  # Its work is in hand in every session, since it brings the other principles back into attention.
22
22
  ACTIVATION = Activation.new({ "when" => "always" }) #: Activation
@@ -40,7 +40,7 @@ module Principles
40
40
  # so the step is said once.
41
41
  STEP_ROLE = "Says, for this project, one of the keeping steps"
42
42
 
43
- # The part any other place that keeps the principles held plays as a member, such as a timer or the committed
43
+ # The part any other place that has the principles checked plays as a member, such as a timer or the committed
44
44
  # setting that enables the plugin: the agent replaces STEP with the keeping step it runs and TRIGGER with what
45
45
  # starts it, a hook, a timer or a reader.
46
46
  KEEPING_PLACE_ROLE = "Runs STEP, started by TRIGGER"
@@ -0,0 +1,60 @@
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
+ # Each part of a turn opening's context with an estimate of its tokens: its share of the request's bytes, at
8
+ # that request's own bytes a token, of the input tokens the host counted. The host counts no part's tokens, so
9
+ # each is an estimate. Shares are rounded down and the tokens left go one each to the largest remainders, the
10
+ # earlier part first on a tie, so the estimates sum to the host's count exactly.
11
+ class PartTokenEstimates
12
+ # parts each give their bytes; input_tokens is the host's count, or nil where it gave none.
13
+ #: (Array[Hash[String, untyped]], Integer?) -> void
14
+ def initialize(parts, input_tokens)
15
+ @parts = parts
16
+ @input_tokens = input_tokens
17
+ freeze
18
+ end
19
+
20
+ # Each part with its estimated_tokens: null for every part where the host gave no count, or the request has no
21
+ # bytes to share it by.
22
+ #: () -> Array[Hash[String, untyped]]
23
+ def to_a
24
+ estimates = estimated
25
+ @parts.each_with_index.map { |part, index| part.merge("estimated_tokens" => estimates&.fetch(index)) }
26
+ end
27
+
28
+ # The request's bytes for each token the host counted, to two places, or nil where it gave none.
29
+ #: () -> Float?
30
+ def bytes_per_token
31
+ tokens = @input_tokens
32
+ tokens&.positive? ? total.fdiv(tokens).round(2).to_f : nil
33
+ end
34
+
35
+ private
36
+
37
+ #: () -> Integer
38
+ def total = @parts.sum { it.fetch("bytes") }
39
+
40
+ # Each part's estimate in order, by largest remainder; nil where there is nothing to share or share it by.
41
+ #: () -> Array[Integer]?
42
+ def estimated
43
+ tokens = @input_tokens
44
+ bytes = total
45
+ return unless tokens && bytes.positive?
46
+
47
+ shares = @parts.map { (it.fetch("bytes") * tokens).divmod(bytes) }
48
+ raised = largest_remainders(shares, tokens - shares.sum(&:first))
49
+ shares.each_with_index.map { |(floor, _), index| raised.include?(index) ? floor + 1 : floor }
50
+ end
51
+
52
+ # The indexes of the count shares, each [floor, remainder], with the largest remainders, the earlier first on
53
+ # a tie.
54
+ #: (Array[[Integer, Integer]], Integer) -> Array[Integer]
55
+ def largest_remainders(shares, count)
56
+ shares.each_index.sort_by { |index| [-shares.fetch(index).last, index] }.first(count)
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,61 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require "json"
5
+ require_relative "../error"
6
+ require_relative "../data/project_files"
7
+
8
+ module Principles
9
+ # The domain logic, and the mediation of persistence. models/CLAUDE.md owns the boundary.
10
+ module Models
11
+ # The line of the project's settings that enables the principles plugin for every session in the project, its
12
+ # enabledPlugins entry, which can be the meta-principle's first member: mapping it writes only .principles/ and
13
+ # edits none of the project's guidance (019 F8). It is a fact about the file, read as Claude Code's project-scope
14
+ # settings shape it; whether the team can install the plugin is not something this tool can see.
15
+ class PluginEnabledInProjectSettings
16
+ # The project-scope settings file a team commits, unlike the local one each user keeps.
17
+ PATH = ".claude/settings.json"
18
+
19
+ # The plugin's name in an enabledPlugins key, before the marketplace it comes from.
20
+ PLUGIN = "principles"
21
+
22
+ # The enabling line in files' project settings, or nil when the file is absent, unreadable or does not enable
23
+ # the plugin.
24
+ #: (Data::ProjectFiles) -> PluginEnabledInProjectSettings?
25
+ def self.read(files)
26
+ return nil unless files.file?(PATH)
27
+
28
+ text = files.read_text(PATH)
29
+ key = enabling_key(files.parse_json(text, PATH)) or return nil
30
+ line = text[/#{Regexp.escape(JSON.generate(key))}\s*:\s*true/] or return nil
31
+ new(line)
32
+ rescue Error
33
+ nil
34
+ end
35
+
36
+ # The enabledPlugins key in settings that turns this plugin on, from any marketplace, or nil.
37
+ #: (untyped) -> String?
38
+ def self.enabling_key(settings)
39
+ plugins = settings.is_a?(Hash) ? settings["enabledPlugins"] : nil
40
+ return nil unless plugins.is_a?(Hash)
41
+
42
+ plugins.find { |name, on| on == true && name.to_s.split("@").first == PLUGIN }&.first
43
+ end
44
+ private_class_method :enabling_key
45
+
46
+ # starting_at is the enabling entry's text as the file holds it.
47
+ #: (String) -> void
48
+ def initialize(starting_at)
49
+ @starting_at = starting_at
50
+ freeze
51
+ end
52
+
53
+ # The literal opening that locates the line in the file, as add-member's --starting-at takes it.
54
+ #: String
55
+ attr_reader :starting_at
56
+
57
+ #: () -> String
58
+ def path = PATH
59
+ end
60
+ end
61
+ end
@@ -89,9 +89,7 @@ module Principles
89
89
 
90
90
  # Each member as this tool prints it: its id beside the fields shown gives it.
91
91
  #: () -> Array[Hash[String, untyped]]
92
- def members_shown
93
- @members.zip(shown.fetch("members")).map { |member, fields| { "id" => member.id }.merge(fields) }
94
- end
92
+ def members_shown = @members.shown(shown.fetch("members"))
95
93
 
96
94
  # The map as its file holds it.
97
95
  #: () -> Hash[String, untyped]
@@ -146,7 +144,12 @@ module Principles
146
144
  end
147
145
 
148
146
  #: (String, String) -> Principle
149
- def changing_role(member_id, role) = changing(member_id, "role" => role)
147
+ def changing_role(member_id, role) = saying(member_id, "role", role)
148
+
149
+ # This principle with member_id's entry giving value as field, one of the fields that say what the member is,
150
+ # never where: its role, its reason, or its name, which is for display only, so its id and key stay.
151
+ #: (String, String, String) -> Principle
152
+ def saying(member_id, field, value) = changing(member_id, field => value)
150
153
 
151
154
  # Names the command that enforces a member, which an audit asks the agent to run. This tool
152
155
  # stores it and never runs it.
@@ -87,6 +87,13 @@ module Principles
87
87
  in_files(paths).map { |member| { "id" => member.id, "path" => member.path } }
88
88
  end
89
89
 
90
+ # Each member as this tool prints it: led by its name when it has one, since the name says what it is for and
91
+ # the id only where it is, then its id beside fields, its entry as shown, in the map's order.
92
+ #: (Array[Hash[String, untyped]]) -> Array[Hash[String, untyped]]
93
+ def shown(fields)
94
+ @list.each_with_index.map { |member, at| { **member.named, "id" => member.id }.merge(fields.fetch(at)) }
95
+ end
96
+
90
97
  # A small first map prompts an agent question, not a minimum member count or a verdict of incompleteness.
91
98
  # no_completed_audit is known absence from the record owner; false also covers unknown state.
92
99
  #: (no_completed_audit: bool) -> bool