scout-ai 2.1.0 → 2.2.0
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.
- checksums.yaml +4 -4
- data/.vimproject +133 -58
- data/README.md +2 -1
- data/VERSION +1 -1
- data/doc/Improvements.md +490 -148
- data/doc/Model.md +53 -10
- data/doc/StartHere.md +44 -19
- data/doc/developer/Architecture.md +25 -6
- data/doc/developer/Backends.md +207 -48
- data/doc/developer/ChatLifecycle.md +121 -20
- data/doc/developer/DelegationInternals.md +319 -33
- data/doc/developer/DesignPrinciples.md +205 -16
- data/doc/developer/PathEngine.md +628 -0
- data/doc/developer/PromptProcessing.md +332 -82
- data/doc/developer/Provenance.md +388 -40
- data/doc/developer/Sandbox.md +213 -0
- data/doc/user/BuildingAgents.md +130 -18
- data/doc/user/Cookbook.md +29 -8
- data/doc/user/CoreConcepts.md +7 -6
- data/doc/user/Delegation.md +166 -15
- data/doc/user/GettingStarted.md +20 -16
- data/doc/user/ManagingContext.md +155 -17
- data/doc/user/MultiAgentWorkflows.md +105 -15
- data/doc/user/Python.md +11 -1
- data/doc/user/RunningInference.md +90 -47
- data/doc/user/ToolCalling.md +134 -29
- data/doc/user/WritingChats.md +56 -27
- data/lib/scout/llm/agent/attach.rb +13 -17
- data/lib/scout/llm/agent/chat.rb +32 -0
- data/lib/scout/llm/agent/construction.rb +64 -0
- data/lib/scout/llm/agent/conversation.rb +304 -0
- data/lib/scout/llm/agent/delegate.rb +56 -238
- data/lib/scout/llm/agent/path/edit.rb +230 -0
- data/lib/scout/llm/agent/path/patch.rb +252 -0
- data/lib/scout/llm/agent/path/workflow_kinds.rb +175 -0
- data/lib/scout/llm/agent/path.rb +1777 -0
- data/lib/scout/llm/agent/save.rb +11 -9
- data/lib/scout/llm/agent/workflow.rb +77 -16
- data/lib/scout/llm/agent.rb +147 -31
- data/lib/scout/llm/ask.rb +28 -17
- data/lib/scout/llm/backends/anthropic.rb +1 -1
- data/lib/scout/llm/backends/bedrock.rb +3 -2
- data/lib/scout/llm/backends/default.rb +127 -34
- data/lib/scout/llm/backends/glm.rb +28 -1
- data/lib/scout/llm/backends/huggingface.rb +1 -1
- data/lib/scout/llm/backends/ollama.rb +1 -1
- data/lib/scout/llm/backends/openai.rb +7 -6
- data/lib/scout/llm/backends/openwebui.rb +14 -7
- data/lib/scout/llm/backends/relay.rb +8 -2
- data/lib/scout/llm/chat/agent_meta.rb +24 -60
- data/lib/scout/llm/chat/parse.rb +1 -1
- data/lib/scout/llm/chat/process/files.rb +10 -0
- data/lib/scout/llm/chat/process/meta.rb +5 -7
- data/lib/scout/llm/chat/process/tools.rb +104 -49
- data/lib/scout/llm/chat/prompt/epoch_tool_compactation.rb +355 -0
- data/lib/scout/llm/chat/prompt/inbox.rb +188 -0
- data/lib/scout/llm/chat/prompt/shorten_tools.rb +797 -13
- data/lib/scout/llm/chat/prompt/shorten_tools_epoch.rb +1 -365
- data/lib/scout/llm/chat/prompt/shorten_tools_epoch_increment.rb +1 -0
- data/lib/scout/llm/chat/prompt.rb +24 -10
- data/lib/scout/llm/chat/provenance/live.rb +1080 -0
- data/lib/scout/llm/chat/provenance/tokens.rb +386 -0
- data/lib/scout/llm/chat/provenance.rb +343 -676
- data/lib/scout/llm/chat.rb +8 -2
- data/lib/scout/llm/embed.rb +15 -3
- data/lib/scout/llm/image.rb +4 -1
- data/lib/scout/llm/mcp.rb +4 -2
- data/lib/scout/llm/request_context.rb +234 -0
- data/lib/scout/llm/sandbox.rb +263 -0
- data/lib/scout/llm/tools/call.rb +163 -42
- data/lib/scout/llm/tools/definition.rb +48 -0
- data/lib/scout/llm/tools/knowledge_base.rb +1 -0
- data/lib/scout/llm/tools/mcp.rb +50 -43
- data/lib/scout/llm/tools/workflow.rb +90 -20
- data/lib/scout/llm/tools.rb +14 -41
- data/lib/scout/model/decision/base.rb +43 -0
- data/lib/scout/model/decision/jev.rb +161 -0
- data/lib/scout/model/python/torch.rb +1 -1
- data/research/clear-tools-inherit.md +171 -0
- data/research/path-engine-dc1-dc2-dc3-fixes.md +279 -0
- data/research/path-kind-chat-role.md +96 -0
- data/research/path-kind-contract-fixes-engine.md +307 -0
- data/research/scout-ai-runtime-availability.md +65 -0
- data/research/subsys/README.md +127 -0
- data/research/subsys/agent-delegation.md +201 -0
- data/research/subsys/agent-society.md +275 -0
- data/research/subsys/agentworkflow.md +233 -0
- data/research/subsys/backends.md +221 -0
- data/research/subsys/chat.md +314 -0
- data/research/subsys/embed-image.md +244 -0
- data/research/subsys/entry-cli.md +253 -0
- data/research/subsys/model-ml.md +326 -0
- data/research/subsys/provenance.md +290 -0
- data/research/subsys/tools.md +334 -0
- data/scout-ai.gemspec +71 -27
- data/scout_commands/agent/ask +8 -7
- data/scout_commands/llm/abridged +40 -0
- data/scout_commands/llm/ask +9 -9
- data/scout_commands/llm/md +2 -0
- data/scout_commands/llm/prov +724 -59
- data/scout_commands/llm/word +4 -0
- data/share/word/reference.docx +0 -0
- data/test/integration/scout/model/decision/test_jev.rb +144 -0
- data/test/scout/llm/agent/test_attach.rb +117 -0
- data/test/scout/llm/agent/test_conversation.rb +407 -0
- data/test/scout/llm/agent/test_delegate.rb +209 -0
- data/test/scout/llm/agent/test_path.rb +1812 -0
- data/test/scout/llm/agent/test_path_edit.rb +908 -0
- data/test/scout/llm/agent/test_path_errors.rb +390 -0
- data/test/scout/llm/agent/test_path_filesystem.rb +532 -0
- data/test/scout/llm/agent/test_path_patch.rb +233 -0
- data/test/scout/llm/agent/test_path_promotion.rb +386 -0
- data/test/scout/llm/agent/test_path_workflow_integration.rb +665 -0
- data/test/scout/llm/agent/test_save.rb +70 -15
- data/test/scout/llm/agent/test_workflow.rb +130 -4
- data/test/scout/llm/backends/test_anthropic.rb +4 -4
- data/test/scout/llm/backends/test_bedrock.rb +4 -4
- data/test/scout/llm/backends/test_format_other.rb +43 -0
- data/test/scout/llm/backends/test_ollama.rb +2 -2
- data/test/scout/llm/backends/test_openwebui.rb +12 -8
- data/test/scout/llm/backends/test_reasoning.rb +114 -0
- data/test/scout/llm/chat/agent_meta_fixtures.rb +28 -27
- data/test/scout/llm/chat/prompt/test_epoch_tool_compactation.rb +612 -0
- data/test/scout/llm/chat/prov_plot_fixtures.rb +131 -0
- data/test/scout/llm/chat/test_agent_meta.rb +40 -80
- data/test/scout/llm/chat/test_agent_meta_provenance.rb +67 -28
- data/test/scout/llm/chat/test_agent_meta_tokens.rb +59 -8
- data/test/scout/llm/chat/test_agent_view.rb +178 -0
- data/test/scout/llm/chat/test_live_report.rb +282 -0
- data/test/scout/llm/chat/test_live_workload.rb +193 -0
- data/test/scout/llm/chat/test_parse.rb +32 -2
- data/test/scout/llm/chat/test_prompt.rb +411 -0
- data/test/scout/llm/chat/test_prov_cli.rb +310 -15
- data/test/scout/llm/chat/test_prov_cli_live.rb +152 -0
- data/test/scout/llm/chat/test_prov_plot.rb +213 -0
- data/test/scout/llm/chat/test_prov_timeline.rb +355 -0
- data/test/scout/llm/chat/test_provenance.rb +67 -22
- data/test/scout/llm/chat/test_provenance_index.rb +479 -0
- data/test/scout/llm/chat/test_society_view.rb +159 -0
- data/test/scout/llm/test_agent.rb +1 -1
- data/test/scout/llm/test_embed.rb +103 -1
- data/test/scout/llm/test_request_context.rb +299 -0
- data/test/scout/llm/test_sandbox.rb +249 -0
- data/test/scout/llm/tools/test_agent_pairing.rb +150 -0
- data/test/scout/llm/tools/test_call_delegation_conflict.rb +316 -0
- data/test/scout/llm/tools/test_definition.rb +332 -0
- data/test/scout/llm/tools/test_jobs_file.rb +211 -0
- data/test/scout/llm/tools/test_mcp.rb +37 -8
- data/test/scout/llm/tools/test_workflow.rb +45 -1
- data/test/scout/model/decision/test_jev.rb +453 -0
- data/test/scout/model/python/huggingface/causal/test_next_token.rb +7 -10
- data/test/scout/model/python/huggingface/test_causal.rb +11 -2
- data/test/scout/model/python/huggingface/test_classification.rb +5 -3
- data/test/scout/model/python/test_availability.rb +116 -0
- data/test/scout/model/python/test_torch.rb +54 -2
- data/test/support/availability.rb +72 -174
- data/test/support/fake_clients.rb +18 -9
- data/test/support/fixtures.rb +75 -6
- data/test/support/mock_backend.rb +9 -1
- data/test/support/test_fixtures.rb +156 -0
- data/test/test_helper.rb +14 -4
- metadata +70 -26
- data/research/ChatAnalyst-required-changes.md +0 -167
- data/research/agent-delegation-analysis.md +0 -810
- data/research/agent-meta-provenance-integration-plan.md +0 -622
- data/research/agent-workflow-analysis.md +0 -1120
- data/research/backends-analysis.md +0 -836
- data/research/chat-core-analysis.md +0 -946
- data/research/chatanalyst-provenance/00-baseline.md +0 -30
- data/research/chatanalyst-provenance/01-repo-map.md +0 -60
- data/research/chatanalyst-provenance/02-event-reconstruction.md +0 -55
- data/research/chatanalyst-provenance/03-duplication-evidence.md +0 -45
- data/research/chatanalyst-provenance/04-tooling-root-cause.md +0 -57
- data/research/chatanalyst-provenance/05-fix-plan.md +0 -46
- data/research/chatanalyst-provenance/07-critic-review.md +0 -25
- data/research/chatanalyst-provenance/final-report.md +0 -45
- data/research/chatanalyst-provenance/resumption.md +0 -37
- data/research/coding-philosophy-analysis.md +0 -928
- data/research/commands-analysis.md +0 -947
- data/research/multi-agent-patterns-analysis.md +0 -853
- data/research/prompt-strategies-analysis.md +0 -630
- data/research/prov-verbosity-fix-notes.md +0 -77
- data/research/provenance-analysis.md +0 -469
- data/research/provenance-continuation-accounting.md +0 -255
- data/research/synthesis-report.md +0 -487
- data/research/tools-system-analysis.md +0 -779
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
# Extracted from the existing provenance token-accounting implementation.
|
|
2
|
+
module Chat
|
|
3
|
+
# ------------------------------------------------------------------
|
|
4
|
+
# Provenance-aware token accounting
|
|
5
|
+
# ------------------------------------------------------------------
|
|
6
|
+
|
|
7
|
+
# Canonical-evidence rule for grouped token events, applied in this order:
|
|
8
|
+
#
|
|
9
|
+
# 1. prefer :chat_meta evidence over :agent_meta evidence (a saved child
|
|
10
|
+
# log chat is the richer, primary representation of the same request);
|
|
11
|
+
# 2. within the same origin, the first evidence in discovery order
|
|
12
|
+
# (provenance_chat_files order, then message index, then receipt
|
|
13
|
+
# index).
|
|
14
|
+
#
|
|
15
|
+
# Event tokens always come from the canonical evidence only. Duplicate
|
|
16
|
+
# evidence records are retained (never summed), so a total can always be
|
|
17
|
+
# explained from the evidence list.
|
|
18
|
+
|
|
19
|
+
# One event Hash per deduplicated direct inference event reachable from
|
|
20
|
+
# `root`. A `job=` projection meta is never a token event, and only
|
|
21
|
+
# evidence carrying at least one TOKEN_KEYS field becomes an event.
|
|
22
|
+
#
|
|
23
|
+
# Warning routing: when the caller supplies a `warnings` Array, two kinds of
|
|
24
|
+
# problems are reported into it instead of raising:
|
|
25
|
+
# * traversal-stage agent_meta problems (malformed receipts and unresolved
|
|
26
|
+
# job references) arrive as the agent_meta_error_reference Hash plus
|
|
27
|
+
# `message:`; the traversal keeps going so one bad receipt never hides
|
|
28
|
+
# the rest of the provenance;
|
|
29
|
+
# * identity_conflict records (see below).
|
|
30
|
+
# Unrelated traversal errors keep strict semantics: they raise, both with and
|
|
31
|
+
# without a warnings Array. Without the Array, agent_meta problems raise
|
|
32
|
+
# exactly like Chat.traverse_provenance strict mode.
|
|
33
|
+
#
|
|
34
|
+
# Identity conflicts never raise here; pass `strict: true` to make them
|
|
35
|
+
# raise instead of being reported (see below).
|
|
36
|
+
#
|
|
37
|
+
# Event shape:
|
|
38
|
+
#
|
|
39
|
+
# {
|
|
40
|
+
# inference_id: <String or nil>,
|
|
41
|
+
# identity: <the Array used for grouping, e.g. [:inference_id, "w1"]>,
|
|
42
|
+
# deduplication: :inference_id | :provider_response_id |
|
|
43
|
+
# :legacy_lineage | :receipt_unresolved,
|
|
44
|
+
# meta: <canonical parsed meta>,
|
|
45
|
+
# tokens: {pt:, ct:, tt:, cct:, cwt:, rt:} (symbols, zero-filled,
|
|
46
|
+
# canonical evidence only),
|
|
47
|
+
# evidence: [
|
|
48
|
+
# {origin: :chat_meta, source:, meta_address:, meta:, call_id: nil,
|
|
49
|
+
# tool_name: nil},
|
|
50
|
+
# {origin: :agent_meta, source:, evidence_address:, output_address:,
|
|
51
|
+
# agent_meta_index:, call_id:, tool_name:, meta:}
|
|
52
|
+
# ],
|
|
53
|
+
# conflict: true|false,
|
|
54
|
+
# incomplete_evidence: true|false
|
|
55
|
+
# }
|
|
56
|
+
#
|
|
57
|
+
# `evidence` holds EVERY persisted location of the event (the same
|
|
58
|
+
# inference copied into several saved chats/logs appears once per chat plus
|
|
59
|
+
# once per receipt). Only the canonical evidence supplies `meta` and
|
|
60
|
+
# `tokens`.
|
|
61
|
+
#
|
|
62
|
+
# Identity (grouping) rules, in priority order:
|
|
63
|
+
#
|
|
64
|
+
# * meta[:inference_id] -> [:inference_id, id]
|
|
65
|
+
# * meta[:provider_response_id] otherwise -> [:provider_response_id, id]
|
|
66
|
+
# * chat-side legacy (neither) -> [:lineage, lineage_id], the
|
|
67
|
+
# existing global lineage dedup of Chat.trace_chat_sources is preserved
|
|
68
|
+
# * receipt-side legacy (neither) -> [:receipt, evidence_address];
|
|
69
|
+
# there is no exact rule, so a receipt legacy meta is never merged with
|
|
70
|
+
# anything (documented possible overcount for legacy data)
|
|
71
|
+
#
|
|
72
|
+
# Identity conflicts: when evidence sharing an identity disagree on any
|
|
73
|
+
# TOKEN_KEYS value (compared as integers, missing counts as 0) or on
|
|
74
|
+
# provider_response_id (two DIFFERENT non-empty values), the event keeps
|
|
75
|
+
# every evidence record, counts only the canonical one, sets conflict: true,
|
|
76
|
+
# and - when a warnings Array was supplied - appends one Hash per conflicting
|
|
77
|
+
# event:
|
|
78
|
+
#
|
|
79
|
+
# {reason: :identity_conflict, inference_id:, identity:,
|
|
80
|
+
# fields: <Array of disputed field symbols>,
|
|
81
|
+
# values: <Hash field => the raw meta values in evidence order>,
|
|
82
|
+
# evidence: <the event evidence records>}
|
|
83
|
+
#
|
|
84
|
+
# A missing provider_response_id in one record and a present one in another
|
|
85
|
+
# is INCOMPLETE EVIDENCE, not a conflict: one copy simply carries richer
|
|
86
|
+
# metadata (e.g. across a metadata-format migration). Such events set
|
|
87
|
+
# incomplete_evidence: true, are counted normally, and are never warned about
|
|
88
|
+
# as conflicts. The same leniency applies to optional detailed usage fields
|
|
89
|
+
# (cct/cwt/rt): only the immutable core (inference identity, pt/ct/tt,
|
|
90
|
+
# non-empty provider_response_id) can conflict.
|
|
91
|
+
#
|
|
92
|
+
# A conflicting event is counted once from its canonical evidence, but that
|
|
93
|
+
# number is NOT authoritative: reports must label any total containing
|
|
94
|
+
# conflicts as unresolved (see Chat.provenance_token_totals `conflicts:`).
|
|
95
|
+
# Pass `strict: true` to raise ScoutException on the first conflict instead.
|
|
96
|
+
#
|
|
97
|
+
# Checkpoint fields (*_c, *_s) are never read or summed here.
|
|
98
|
+
def self.provenance_token_events(root, warnings: nil, strict: false, chats: nil, **traversal_options)
|
|
99
|
+
unless Thread.current[:scout_ai_provenance_run_cache]
|
|
100
|
+
return with_provenance_run_cache do
|
|
101
|
+
provenance_token_events(root, warnings: warnings, strict: strict, chats: chats, **traversal_options)
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
# Route traversal-stage agent_meta problems into the caller's warnings
|
|
105
|
+
# Array instead of letting them raise. Other traversal errors stay strict
|
|
106
|
+
# (raise), and an explicitly supplied on_error keeps being called.
|
|
107
|
+
if Array === warnings
|
|
108
|
+
caller_on_error = traversal_options[:on_error]
|
|
109
|
+
traversal_options = traversal_options.merge(
|
|
110
|
+
on_error: lambda do |error, kind, object, relation, reference|
|
|
111
|
+
if relation == :agent_job && reference.is_a?(Hash) && reference[:reason] &&
|
|
112
|
+
error.message.to_s.include?('agent_meta receipt')
|
|
113
|
+
warnings << reference.merge(message: error.message)
|
|
114
|
+
caller_on_error.call(error, kind, object, relation, reference) if caller_on_error
|
|
115
|
+
elsif caller_on_error
|
|
116
|
+
caller_on_error.call(error, kind, object, relation, reference)
|
|
117
|
+
else
|
|
118
|
+
raise error
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
)
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
sources = chats || provenance_chat_files(root, **traversal_options).to_h do |file|
|
|
125
|
+
[file.to_s, provenance_chat_load(file)]
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
evidences = []
|
|
129
|
+
|
|
130
|
+
# Chat-side direct events. `deduplicate: false` keeps one entry per
|
|
131
|
+
# persisted location: an inference copied into several saved chats/logs
|
|
132
|
+
# (or projected into several places) yields several evidence records, one
|
|
133
|
+
# per address. Grouping by identity happens exactly once, below, so the
|
|
134
|
+
# resulting event can list every location.
|
|
135
|
+
trace_chat_sources(sources, false).each do |entry|
|
|
136
|
+
meta = entry[:meta]
|
|
137
|
+
next if meta[:job]
|
|
138
|
+
next unless TOKEN_KEYS.any? { |key| meta.include?(key) }
|
|
139
|
+
address = entry[:meta_address]
|
|
140
|
+
evidences << {
|
|
141
|
+
origin: :chat_meta,
|
|
142
|
+
discovery: evidences.length,
|
|
143
|
+
source: address ? address[0] : nil,
|
|
144
|
+
meta_address: address,
|
|
145
|
+
meta: meta,
|
|
146
|
+
call_id: nil,
|
|
147
|
+
tool_name: nil,
|
|
148
|
+
inference_id: entry[:inference_id],
|
|
149
|
+
lineage_id: entry[:lineage_id]
|
|
150
|
+
}
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
# Receipt-side direct events. Malformed receipts are reported through a
|
|
154
|
+
# private buffer and merged into the caller's warnings Array only when the
|
|
155
|
+
# traversal stage has not already reported the same problem (it reports
|
|
156
|
+
# with the richer reference + message shape whenever :agent_job is
|
|
157
|
+
# followed); without an Array, agent_meta_evidence skips them silently.
|
|
158
|
+
receipt_problems = Array === warnings ? [] : nil
|
|
159
|
+
sources.each do |path, chat|
|
|
160
|
+
agent_meta_evidence(chat, source: path, warnings: receipt_problems).each do |record|
|
|
161
|
+
meta = record[:meta]
|
|
162
|
+
next if meta[:job]
|
|
163
|
+
next unless TOKEN_KEYS.any? { |key| meta.include?(key) }
|
|
164
|
+
evidences << {
|
|
165
|
+
origin: :agent_meta,
|
|
166
|
+
discovery: evidences.length,
|
|
167
|
+
source: path,
|
|
168
|
+
evidence_address: record[:evidence_address],
|
|
169
|
+
output_address: record[:output_address],
|
|
170
|
+
agent_meta_index: record[:agent_meta_index],
|
|
171
|
+
meta: meta,
|
|
172
|
+
call_id: record[:call_id],
|
|
173
|
+
tool_name: record[:tool_name],
|
|
174
|
+
inference_id: meta[:inference_id],
|
|
175
|
+
lineage_id: nil
|
|
176
|
+
}
|
|
177
|
+
end
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
if receipt_problems && !receipt_problems.empty?
|
|
181
|
+
reported = Set.new(warnings.collect do |warning|
|
|
182
|
+
next if warning[:reason] == :identity_conflict
|
|
183
|
+
[warning[:reason], warning[:output_address], warning[:agent_meta_index]]
|
|
184
|
+
end)
|
|
185
|
+
receipt_problems.each do |problem|
|
|
186
|
+
key = [problem[:reason], problem[:output_address], problem[:agent_meta_index]]
|
|
187
|
+
warnings << problem unless reported.include?(key)
|
|
188
|
+
end
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
origin_rank = { chat_meta: 0, agent_meta: 1 }
|
|
192
|
+
events = []
|
|
193
|
+
by_identity = {}
|
|
194
|
+
|
|
195
|
+
evidences.each do |evidence|
|
|
196
|
+
meta = evidence[:meta]
|
|
197
|
+
# Blank identifiers are not identity evidence: a truthy-but-empty
|
|
198
|
+
# inference_id / provider_response_id ("") must fall through to the
|
|
199
|
+
# lineage / receipt rules instead of keying DISTINCT inferences onto
|
|
200
|
+
# one shared blank identity. Same blank normalization the conflict
|
|
201
|
+
# detection below applies to provider_response_id.
|
|
202
|
+
inference_id = evidence[:inference_id]
|
|
203
|
+
inference_id = nil if inference_id.to_s.strip.empty?
|
|
204
|
+
provider_response_id = meta[:provider_response_id]
|
|
205
|
+
provider_response_id = nil if provider_response_id.to_s.strip.empty?
|
|
206
|
+
|
|
207
|
+
if inference_id
|
|
208
|
+
identity = [:inference_id, inference_id]
|
|
209
|
+
deduplication = :inference_id
|
|
210
|
+
elsif provider_response_id
|
|
211
|
+
identity = [:provider_response_id, provider_response_id]
|
|
212
|
+
deduplication = :provider_response_id
|
|
213
|
+
elsif evidence[:origin] == :chat_meta
|
|
214
|
+
identity = [:lineage, evidence[:lineage_id]]
|
|
215
|
+
deduplication = :legacy_lineage
|
|
216
|
+
else
|
|
217
|
+
identity = [:receipt, evidence[:evidence_address]]
|
|
218
|
+
deduplication = :receipt_unresolved
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
event = by_identity[identity]
|
|
222
|
+
unless event
|
|
223
|
+
event = { identity: identity, deduplication: deduplication, _evidence: [] }
|
|
224
|
+
by_identity[identity] = event
|
|
225
|
+
events << event
|
|
226
|
+
end
|
|
227
|
+
event[:_evidence] << evidence
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
events.each do |event|
|
|
231
|
+
ordered = event.delete(:_evidence).sort_by do |evidence|
|
|
232
|
+
[origin_rank[evidence[:origin]], evidence[:discovery]]
|
|
233
|
+
end
|
|
234
|
+
canonical = ordered.first
|
|
235
|
+
|
|
236
|
+
event[:evidence] = ordered.collect do |evidence|
|
|
237
|
+
record = {
|
|
238
|
+
origin: evidence[:origin],
|
|
239
|
+
source: evidence[:source],
|
|
240
|
+
meta: evidence[:meta],
|
|
241
|
+
call_id: evidence[:call_id],
|
|
242
|
+
tool_name: evidence[:tool_name]
|
|
243
|
+
}
|
|
244
|
+
if evidence[:origin] == :chat_meta
|
|
245
|
+
record[:meta_address] = evidence[:meta_address]
|
|
246
|
+
else
|
|
247
|
+
record[:evidence_address] = evidence[:evidence_address]
|
|
248
|
+
record[:output_address] = evidence[:output_address]
|
|
249
|
+
record[:agent_meta_index] = evidence[:agent_meta_index]
|
|
250
|
+
end
|
|
251
|
+
record
|
|
252
|
+
end
|
|
253
|
+
|
|
254
|
+
event[:inference_id] = canonical[:inference_id]
|
|
255
|
+
event[:meta] = canonical[:meta]
|
|
256
|
+
event[:tokens] = TOKEN_KEYS.each_with_object({}) do |key, hash|
|
|
257
|
+
hash[key.to_sym] = canonical[:meta][key].to_i
|
|
258
|
+
end
|
|
259
|
+
|
|
260
|
+
# Immutable core only: pt/ct/tt and a genuinely different non-empty
|
|
261
|
+
# provider_response_id. Optional detail fields (cct, cwt, rt) and a
|
|
262
|
+
# missing-vs-present provider_response_id are incomplete evidence, not
|
|
263
|
+
# conflicts.
|
|
264
|
+
core_fields = %i[pt ct tt]
|
|
265
|
+
fields = []
|
|
266
|
+
values = {}
|
|
267
|
+
core_fields.each do |key|
|
|
268
|
+
next unless ordered.collect { |evidence| evidence[:meta][key].to_i }.uniq.length > 1
|
|
269
|
+
fields << key
|
|
270
|
+
values[key] = ordered.collect { |evidence| evidence[:meta][key] }
|
|
271
|
+
end
|
|
272
|
+
provider_ids = ordered.collect { |evidence| evidence[:meta][:provider_response_id].to_s }
|
|
273
|
+
.reject { |value| value.strip.empty? }.uniq
|
|
274
|
+
if provider_ids.length > 1
|
|
275
|
+
fields << :provider_response_id
|
|
276
|
+
values[:provider_response_id] = ordered.collect { |evidence| evidence[:meta][:provider_response_id] }
|
|
277
|
+
end
|
|
278
|
+
|
|
279
|
+
# Incomplete evidence: exactly one non-empty provider_response_id value
|
|
280
|
+
# while at least one copy lacks it (metadata-format migration), so there
|
|
281
|
+
# is nothing to disagree about.
|
|
282
|
+
present_ids = ordered.collect { |evidence| evidence[:meta][:provider_response_id].to_s }
|
|
283
|
+
.reject { |value| value.strip.empty? }
|
|
284
|
+
if present_ids.uniq.length <= 1 &&
|
|
285
|
+
present_ids.length != ordered.length && !present_ids.empty?
|
|
286
|
+
event[:incomplete_evidence] = true
|
|
287
|
+
end
|
|
288
|
+
|
|
289
|
+
next unless fields.any?
|
|
290
|
+
event[:conflict] = true
|
|
291
|
+
if strict
|
|
292
|
+
raise ScoutException, "agent_meta identity conflict on #{event[:identity] * '='}: disputed #{fields * ','}"
|
|
293
|
+
end
|
|
294
|
+
next unless Array === warnings
|
|
295
|
+
warnings << {
|
|
296
|
+
reason: :identity_conflict,
|
|
297
|
+
inference_id: event[:inference_id],
|
|
298
|
+
identity: event[:identity],
|
|
299
|
+
fields: fields,
|
|
300
|
+
values: values,
|
|
301
|
+
evidence: event[:evidence]
|
|
302
|
+
}
|
|
303
|
+
end
|
|
304
|
+
|
|
305
|
+
# Reorder keys readably; conflict defaults to false.
|
|
306
|
+
events.collect do |event|
|
|
307
|
+
{
|
|
308
|
+
inference_id: event[:inference_id],
|
|
309
|
+
identity: event[:identity],
|
|
310
|
+
deduplication: event[:deduplication],
|
|
311
|
+
meta: event[:meta],
|
|
312
|
+
tokens: event[:tokens],
|
|
313
|
+
evidence: event[:evidence],
|
|
314
|
+
conflict: event[:conflict] || false,
|
|
315
|
+
incomplete_evidence: event[:incomplete_evidence] || false
|
|
316
|
+
}
|
|
317
|
+
end
|
|
318
|
+
end
|
|
319
|
+
|
|
320
|
+
# Aggregate token totals over deduplicated provenance events. The result is
|
|
321
|
+
# symbol keyed and zero initialized exactly like Chat.token_totals:
|
|
322
|
+
# {pt:, ct:, tt:, cct:, cwt:, rt:}.
|
|
323
|
+
#
|
|
324
|
+
# Scopes. These are EVIDENCE COVERAGE descriptions, not a partition of cost:
|
|
325
|
+
# an event represented both in a saved child log and in a receipt belongs to
|
|
326
|
+
# :chat_evidence AND to :receipt_evidence, so the two are not additive and
|
|
327
|
+
# must never be summed together. Only :deduplicated_total and :receipt_only
|
|
328
|
+
# are safe to compare additively.
|
|
329
|
+
#
|
|
330
|
+
# * :deduplicated_total - every event counted once (default);
|
|
331
|
+
# * :chat_evidence - events with at least one :chat_meta evidence
|
|
332
|
+
# (physically stored in a chat/log file);
|
|
333
|
+
# * :receipt_evidence - events with at least one :agent_meta evidence
|
|
334
|
+
# (embedded in a function_call_output receipt);
|
|
335
|
+
# * :receipt_only - events with receipt evidence and NO saved-chat
|
|
336
|
+
# evidence (the disjoint delegated contribution).
|
|
337
|
+
#
|
|
338
|
+
# Conflict handling: a conflicting event still contributes its canonical
|
|
339
|
+
# tokens, so a total containing conflicts is a best-effort view, not an
|
|
340
|
+
# authoritative cost. The `conflicts:` keyword makes that explicit in
|
|
341
|
+
# machine readable form; with `strict: true` conflicts raise instead (the
|
|
342
|
+
# keyword is forwarded to provenance_token_events).
|
|
343
|
+
def self.provenance_token_totals(root, scope: :deduplicated_total, warnings: nil,
|
|
344
|
+
conflicts: nil, **traversal_options)
|
|
345
|
+
events = provenance_token_events(root, warnings: warnings, **traversal_options)
|
|
346
|
+
|
|
347
|
+
selected = case scope.to_sym
|
|
348
|
+
when :deduplicated_total
|
|
349
|
+
events
|
|
350
|
+
when :chat_evidence
|
|
351
|
+
events.select { |event| event[:evidence].any? { |e| e[:origin] == :chat_meta } }
|
|
352
|
+
when :receipt_evidence
|
|
353
|
+
events.select { |event| event[:evidence].any? { |e| e[:origin] == :agent_meta } }
|
|
354
|
+
when :receipt_only
|
|
355
|
+
events.select do |event|
|
|
356
|
+
event[:evidence].any? { |e| e[:origin] == :agent_meta } &&
|
|
357
|
+
event[:evidence].none? { |e| e[:origin] == :chat_meta }
|
|
358
|
+
end
|
|
359
|
+
else
|
|
360
|
+
raise ParameterException, "Unknown token scope: #{scope}"
|
|
361
|
+
end
|
|
362
|
+
|
|
363
|
+
totals = TOKEN_KEYS.each_with_object({}) { |key, hash| hash[key.to_sym] = 0 }
|
|
364
|
+
selected.each do |event|
|
|
365
|
+
TOKEN_KEYS.each { |key| totals[key.to_sym] += event[:tokens][key.to_sym] }
|
|
366
|
+
end
|
|
367
|
+
|
|
368
|
+
if conflicts.is_a?(Hash)
|
|
369
|
+
conflicting = events.select { |event| event[:conflict] }
|
|
370
|
+
incomplete = events.select { |event| event[:incomplete_evidence] }
|
|
371
|
+
conflicts[:events] = conflicting.length
|
|
372
|
+
conflicts[:incomplete_evidence_events] = incomplete.length
|
|
373
|
+
conflicts[:authoritative] = conflicting.empty?
|
|
374
|
+
end
|
|
375
|
+
|
|
376
|
+
totals
|
|
377
|
+
end
|
|
378
|
+
|
|
379
|
+
# Provenance token aggregate. Receipt (agent_meta) child usage is now
|
|
380
|
+
# included through the event collector, so child inference paid inside a
|
|
381
|
+
# parent tool call is no longer invisible when the child chat was not saved.
|
|
382
|
+
def self.tokens(root, **options)
|
|
383
|
+
provenance_token_totals(root, **options)
|
|
384
|
+
end
|
|
385
|
+
|
|
386
|
+
end
|