Skip to content

Commit 3bfc1b1

Browse files
Dumbrisclaude
andauthored
fix(activity): histogram≙log parity, status-aware grouping, code-exec sub-call transparency (#1016)
* fix(activity): histogram≙log parity, status-aware tray grouping, code-exec sub-call transparency - Histogram now counts internal tool calls (retrieve_tools/describe_tool/code_execution/…) and blocked policy decisions, matching the tray glance population; call_tool_* variant echoes excluded everywhere (list + histogram) since every one pairs with a canonical record - Usage aggregate replays activity records newer than the persisted snapshot on startup (no more permanent drift after unclean shutdown); internal calls now reach the live aggregate - /activity/summary computes over the full period, not the newest 50 records - code_execution: ExecutionID pass-by-value bug fixed; every sandbox sub-call now emits a first-class tool_call activity record with fresh request_id + parent_id linkage - parent_id: new ActivityRecord field, list/export filter, swagger, SSE passthrough - Web UI: parent/child badges, View sub-calls / View parent navigation in Activity drawer - CLI: activity list/export --parent-id, child markers in table+watch, show hints, docs - Tray: run grouping key now includes status class — ok and errored calls never merge; spec 090 FR-002/FR-004 amended; parent_id decoded Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(tray): mirror the widened usage-timeline rule in the live SSE header count countsTowardUsageTimeline still counted only tool_call records, so internal calls (code_execution, retrieve_tools) and blocked policy decisions moved the bars at the next poll but not the header — the header lagged the rows by up to 30s. Now mirrors UsageAggregate.Apply case-for-case (internal non-call_tool_* count, rejected tool_call sheds and call_tool_* echoes stay out, blocked and rejected policy decisions count). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(activity): address cross-review findings — snapshot versioning, glance-set parity, sandbox shed/refusal records Opencode cross-review of #1016 surfaced four genuine defects: - Snapshots built under the old admission rule (no internal calls) were carried forward after upgrade; aggregates now stamp an AdmissionVersion and a mismatched snapshot is rebuilt from a full activity scan (decode clears the constructor default so legacy snapshots can't masquerade as current; clone() now carries the stamp). - Successful management built-ins (upstream_servers, quarantine_security, …) entered the timeline but are not glance rows; admission now counts internal successes only for retrieve_tools/describe_tool/code_execution, failures always. Swift live-count mirror updated in lockstep. - A concurrency shed inside a sandbox sub-call was double-recorded: the limiter's origin-independent observer already emits the canonical rejected record, so emitSubCallActivity now skips limiter sheds. - A sandbox policy refusal was recorded as an executed error call; it is now status=blocked, and blocked tool_calls route like blocked policy decisions (Blocked counter + failed timeline bar, no Calls/latency/bytes). Known accepted trade-offs (documented in code): the catch-up replay's strict timestamp bound can miss a record whose Apply lagged its Save across a crash (the alternative is permanent double-counting), and startup rejected-shed recording is synchronous by design. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(activity): round-2 review — rule-1 parity for management built-ins, narrow the sandbox shed skip - Management built-ins (upstream_servers, quarantine_security) are hidden from the glance whatever their status (GlanceSelection rule 1), so their failures must not bar either; Go admission and the Swift live-count mirror now exclude them entirely (non-management internal failures still count). - emitSubCallActivity skipped every limiter error, but only queue_full/ queue_timeout sheds get the canonical rejected record from the admission observer — a server_unavailable refusal lost its only activity record. The skip (shedHasCanonicalRecord, unit-tested) is now reason-scoped. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(activity): bump usageAdmissionVersion for the rule-1 admission change Round-3 review: the round-2 admission narrowing (management built-ins out) changed which records count, so snapshots stamped 2 must rebuild too. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(macos): parent↔child navigation + formatted code in the Activity window User feedback on #1016: the native Activity window had no way to walk from a code_execution to its sub-calls or back, and the code argument rendered as an escaped one-line JSON string. - Detail panel: 'View sub-calls' on code_execution parents filters the list by parent_id (with a dismissable chip); sub-call records show 'View parent call', which restores the list and selects the parent (fetched by request_id and pinned if it scrolled past the page). - List rows: sub-calls carry an arrow marker in the Details column. - Detail panel: the code argument renders as a Code section (language badge, monospace); one-line agent scripts are broken at statement boundaries — never inside string literals or parens (formatScriptForDisplay, unit-tested). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(macos): round-5 review — load-ticket guard + drop selection on sub-call filter - loadActivities now publishes only the newest in-flight fetch (monotonic ticket), so a slow unfiltered load can't overwrite the sub-call view. - Entering the sub-call view drops the parent's selection so clearing the chip doesn't silently reopen its detail panel. - formatScriptForDisplay documents its display-only limits (regex literals, nested template substitutions); Copy always yields the verbatim script. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * docs(activity): sync docs with parent_id + call_tool_* dedup behavior Docs audit findings (PR #1016): drop the stale 'failed call_tool_* are always shown' claims (all call_tool_* records are hidden by default now — every dispatch has a paired canonical record), add the parent_id query param to the REST + feature docs, document code_execution sub-call records and their parent/child navigation, and note sub-call activity logging in the code-execution security model. Spec audit: no normative amendments needed — spec 096 FR-010 (sub-calls recorded in activity logging) is fulfilled by this PR, spec 090 was amended in-branch, spec 093's rejected-shed exclusion is preserved. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * docs(specs): amend 024 duplicate-filtering note + 069 usage-timeline contract Spec audit for PR #1016: two normative statements were made stale by this PR. Spec 024's note claimed failed call_tool_* records are the sole witness of a failed dispatch (they are paired now, and hidden by default); spec 069's usage contract said the timeline excludes blocked attempts (it now mirrors the glance population). Both amended with superseded notes. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
1 parent 52146a4 commit 3bfc1b1

54 files changed

Lines changed: 2824 additions & 237 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎cmd/mcpproxy/activity_cmd.go‎

Lines changed: 109 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ var (
4141
activityOffset int
4242
activityIntentType string // Spec 018: Filter by operation type (read, write, destructive)
4343
activityRequestID string // Spec 021: Filter by HTTP request ID for correlation
44+
activityParentID string // Activity transparency: filter to sub-calls of a code_execution parent
4445
activityNoIcons bool // Disable emoji icons in output
4546
activityDetectionType string // Spec 026: Filter by detection type (e.g., "aws_access_key")
4647
activitySeverity string // Spec 026: Filter by severity level (critical, high, medium, low)
@@ -73,6 +74,7 @@ type ActivityFilter struct {
7374
Offset int
7475
IntentType string // Spec 018: Filter by operation type (read, write, destructive)
7576
RequestID string // Spec 021: Filter by HTTP request ID for correlation
77+
ParentID string // Activity transparency: filter to sub-calls of a code_execution parent
7678
SensitiveData *bool // Spec 026: Filter by sensitive data detection
7779
DetectionType string // Spec 026: Filter by detection type
7880
Severity string // Spec 026: Filter by severity level
@@ -228,6 +230,11 @@ func (f *ActivityFilter) ToQueryParams() url.Values {
228230
if f.RequestID != "" {
229231
q.Set("request_id", f.RequestID)
230232
}
233+
// Activity transparency: parent_id joins a code_execution record to the
234+
// sandboxed sub-calls it made (child.parent_id == parent.request_id).
235+
if f.ParentID != "" {
236+
q.Set("parent_id", f.ParentID)
237+
}
231238
// Spec 026: Add sensitive data filters
232239
if f.SensitiveData != nil {
233240
q.Set("sensitive_data", fmt.Sprintf("%t", *f.SensitiveData))
@@ -856,6 +863,9 @@ Examples:
856863
# List activity by request ID (for error correlation)
857864
mcpproxy activity list --request-id abc123-def456
858865
866+
# List the sub-calls a code_execution made (value = the parent's request_id)
867+
mcpproxy activity list --parent-id abc123-def456
868+
859869
# List only activities with sensitive data detected
860870
mcpproxy activity list --sensitive-data
861871
@@ -932,7 +942,10 @@ Examples:
932942
mcpproxy activity export --format csv --output activity.csv
933943
934944
# Export to stdout for piping
935-
mcpproxy activity export --format csv | gzip > activity.csv.gz`,
945+
mcpproxy activity export --format csv | gzip > activity.csv.gz
946+
947+
# Export just the sub-calls of one code_execution
948+
mcpproxy activity export --parent-id abc123-def456`,
936949
RunE: runActivityExport,
937950
}
938951
)
@@ -962,6 +975,7 @@ func init() {
962975
activityListCmd.Flags().IntVar(&activityOffset, "offset", 0, "Pagination offset")
963976
activityListCmd.Flags().StringVar(&activityIntentType, "intent-type", "", "Filter by intent operation type: read, write, destructive")
964977
activityListCmd.Flags().StringVar(&activityRequestID, "request-id", "", "Filter by HTTP request ID for log correlation")
978+
activityListCmd.Flags().StringVar(&activityParentID, "parent-id", "", "List child tool calls of a code_execution activity (value = the parent record request_id)")
965979
activityListCmd.Flags().BoolVar(&activityNoIcons, "no-icons", false, "Disable emoji icons in output (use text instead)")
966980
// Spec 026: Sensitive data detection filters
967981
activityListCmd.Flags().Bool("sensitive-data", false, "Filter to show only activities with sensitive data detected")
@@ -995,6 +1009,7 @@ func init() {
9951009
activityExportCmd.Flags().StringVar(&activitySessionID, "session", "", "Filter by session — a work session id (ws-...) or a raw MCP transport session id")
9961010
activityExportCmd.Flags().StringVar(&activityStartTime, "start-time", "", "Filter after this time (RFC3339)")
9971011
activityExportCmd.Flags().StringVar(&activityEndTime, "end-time", "", "Filter before this time (RFC3339)")
1012+
activityExportCmd.Flags().StringVar(&activityParentID, "parent-id", "", "Export only child tool calls of a code_execution activity (value = the parent record request_id)")
9981013
}
9991014

10001015
// getActivityClient creates an HTTP client for the daemon
@@ -1045,6 +1060,7 @@ func runActivityList(cmd *cobra.Command, _ []string) error {
10451060
Offset: activityOffset,
10461061
IntentType: activityIntentType,
10471062
RequestID: activityRequestID,
1063+
ParentID: activityParentID,
10481064
SensitiveData: sensitiveDataPtr,
10491065
DetectionType: activityDetectionType,
10501066
Severity: activitySeverity,
@@ -1147,7 +1163,9 @@ func runActivityList(cmd *cobra.Command, _ []string) error {
11471163
sourceIcon,
11481164
actType,
11491165
server,
1150-
tool,
1166+
// Activity transparency: a sub-call of a code_execution parent is
1167+
// indented so a mixed list reads as a tree, not a flat run of rows.
1168+
activityChildToolCell(act, tool),
11511169
intentStr,
11521170
sensitiveStr, // Spec 026: Show sensitive data indicator
11531171
status,
@@ -1433,6 +1451,8 @@ func formatToolCallEvent(event map[string]interface{}, timestamp string) string
14331451
statusIcon := formatStatusIcon(status)
14341452

14351453
line := fmt.Sprintf("[%s] [%s] %s:%s %s %s", timestamp, sourceIcon, server, tool, statusIcon, formatActivityDuration(int64(durationMs)))
1454+
// Activity transparency: mark sub-calls made from inside a code_execution.
1455+
line += formatParentMarker(event)
14361456
if errMsg != "" {
14371457
line += " " + errMsg
14381458
}
@@ -1465,6 +1485,7 @@ func formatInternalToolCallEvent(event map[string]interface{}, timestamp string)
14651485
}
14661486

14671487
line := fmt.Sprintf("[%s] [INT] %s%s %s %s", timestamp, internalTool, target, statusIcon, formatActivityDuration(int64(durationMs)))
1488+
line += formatParentMarker(event)
14681489
if errMsg != "" {
14691490
line += " " + errMsg
14701491
}
@@ -1547,6 +1568,41 @@ func formatStatusIcon(status string) string {
15471568
}
15481569
}
15491570

1571+
// activityChildToolCell renders the TOOL cell for a list row, marking records
1572+
// that are sub-calls of a code_execution parent (ParentID == parent request_id).
1573+
func activityChildToolCell(act map[string]interface{}, tool string) string {
1574+
if getStringField(act, "parent_id") == "" {
1575+
return tool
1576+
}
1577+
return "└ " + tool
1578+
}
1579+
1580+
// formatParentMarker renders the short "[child of <parent_id>]" suffix used by
1581+
// `activity watch`, so a sub-call is recognisable in the live stream.
1582+
func formatParentMarker(event map[string]interface{}) string {
1583+
parentID := getStringField(event, "parent_id")
1584+
if parentID == "" {
1585+
return ""
1586+
}
1587+
return fmt.Sprintf(" [child of %s]", parentID)
1588+
}
1589+
1590+
// isCodeExecutionParent reports whether a record is the code_execution activity
1591+
// that sandboxed sub-calls hang off. Only the internal_tool_call record spawns
1592+
// children; a same-named upstream tool_call does not.
1593+
func isCodeExecutionParent(activity map[string]interface{}) bool {
1594+
if getStringField(activity, "type") != string(storage.ActivityTypeInternalToolCall) {
1595+
return false
1596+
}
1597+
if getStringField(activity, "tool_name") == "code_execution" {
1598+
return true
1599+
}
1600+
if metadata := getMapField(activity, "metadata"); metadata != nil {
1601+
return getStringField(metadata, "internal_tool_name") == "code_execution"
1602+
}
1603+
return false
1604+
}
1605+
15501606
// runActivityShow implements the activity show command
15511607
func runActivityShow(cmd *cobra.Command, args []string) error {
15521608
activityID := args[0]
@@ -1621,10 +1677,22 @@ func runActivityShow(cmd *cobra.Command, args []string) error {
16211677
// Spec 098 (SC-005): the request id is how a preflight record is joined to
16221678
// the tool calls of the same workflow (`activity list --request-id <id>`),
16231679
// so the detail view has to show it, not just accept it as a filter.
1624-
if requestID := getStringField(activity, "request_id"); requestID != "" {
1680+
requestID := getStringField(activity, "request_id")
1681+
if requestID != "" {
16251682
fmt.Printf("Request ID: %s\n", requestID)
16261683
}
16271684

1685+
// Activity transparency: both directions of the code_execution ↔ sub-call
1686+
// link are navigable from the detail view.
1687+
// child → parent: the Parent ID line below
1688+
// parent → children: the hint below it
1689+
if parentID := getStringField(activity, "parent_id"); parentID != "" {
1690+
fmt.Printf("Parent ID: %s\n", parentID)
1691+
fmt.Printf(" (sub-call of a code_execution — 'mcpproxy activity list --request-id %s' shows the parent)\n", parentID)
1692+
} else if isCodeExecutionParent(activity) && requestID != "" {
1693+
fmt.Printf("\nUse 'mcpproxy activity list --parent-id %s' to see sub-calls.\n", requestID)
1694+
}
1695+
16281696
if errMsg := getStringField(activity, "error_message"); errMsg != "" {
16291697
fmt.Printf("Error: %s\n", errMsg)
16301698
}
@@ -1779,6 +1847,43 @@ func runActivitySummary(cmd *cobra.Command, _ []string) error {
17791847
return nil
17801848
}
17811849

1850+
// activityExportQueryParams builds the export query string from the export
1851+
// flags. Export does NOT go through ActivityFilter.ToQueryParams, so every
1852+
// filter flag has to be wired here as well or it is a silent no-op.
1853+
func activityExportQueryParams() url.Values {
1854+
q := url.Values{}
1855+
q.Set("format", activityExportFormat)
1856+
if activityType != "" {
1857+
q.Set("type", activityType)
1858+
}
1859+
if activityServer != "" {
1860+
q.Set("server", activityServer)
1861+
}
1862+
if activityTool != "" {
1863+
q.Set("tool", activityTool)
1864+
}
1865+
if activityStatus != "" {
1866+
q.Set("status", activityStatus)
1867+
}
1868+
if activitySessionID != "" {
1869+
q.Set(sessionQueryParam(activitySessionID), activitySessionID)
1870+
}
1871+
if activityStartTime != "" {
1872+
q.Set("start_time", activityStartTime)
1873+
}
1874+
if activityEndTime != "" {
1875+
q.Set("end_time", activityEndTime)
1876+
}
1877+
// Activity transparency: export the sub-calls of one code_execution.
1878+
if activityParentID != "" {
1879+
q.Set("parent_id", activityParentID)
1880+
}
1881+
if activityIncludeBodies {
1882+
q.Set("include_bodies", "true")
1883+
}
1884+
return q
1885+
}
1886+
17821887
// runActivityExport implements the activity export command
17831888
func runActivityExport(cmd *cobra.Command, _ []string) error {
17841889
// Setup logger
@@ -1812,34 +1917,7 @@ func runActivityExport(cmd *cobra.Command, _ []string) error {
18121917
transport, baseURL := activityTransport(endpoint, logger.Sugar())
18131918
exportURL := baseURL + "/api/v1/activity/export"
18141919

1815-
q := url.Values{}
1816-
q.Set("format", activityExportFormat)
1817-
if activityType != "" {
1818-
q.Set("type", activityType)
1819-
}
1820-
if activityServer != "" {
1821-
q.Set("server", activityServer)
1822-
}
1823-
if activityTool != "" {
1824-
q.Set("tool", activityTool)
1825-
}
1826-
if activityStatus != "" {
1827-
q.Set("status", activityStatus)
1828-
}
1829-
if activitySessionID != "" {
1830-
q.Set(sessionQueryParam(activitySessionID), activitySessionID)
1831-
}
1832-
if activityStartTime != "" {
1833-
q.Set("start_time", activityStartTime)
1834-
}
1835-
if activityEndTime != "" {
1836-
q.Set("end_time", activityEndTime)
1837-
}
1838-
if activityIncludeBodies {
1839-
q.Set("include_bodies", "true")
1840-
}
1841-
1842-
exportURL += "?" + q.Encode()
1920+
exportURL += "?" + activityExportQueryParams().Encode()
18431921

18441922
// Create HTTP request
18451923
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package main
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
// Activity transparency: a code_execution parent and its sandboxed sub-calls are
11+
// joined by ParentID (the parent record's request_id). The CLI has to be able to
12+
// (a) ask for the children, (b) mark them in the table, (c) point back at the
13+
// parent from the detail view, and (d) carry the filter into export.
14+
15+
func TestActivityFilter_ToQueryParams_ParentID(t *testing.T) {
16+
tests := []struct {
17+
name string
18+
filter ActivityFilter
19+
wantKey string
20+
wantVal string
21+
wantNone bool
22+
}{
23+
{
24+
name: "parent id is sent as parent_id",
25+
filter: ActivityFilter{ParentID: "req-parent-123"},
26+
wantKey: "parent_id",
27+
wantVal: "req-parent-123",
28+
},
29+
{
30+
name: "empty parent id is omitted",
31+
filter: ActivityFilter{},
32+
wantKey: "parent_id",
33+
wantNone: true,
34+
},
35+
}
36+
37+
for _, tt := range tests {
38+
t.Run(tt.name, func(t *testing.T) {
39+
params := tt.filter.ToQueryParams()
40+
if tt.wantNone {
41+
assert.False(t, params.Has(tt.wantKey), "param %s should be omitted", tt.wantKey)
42+
return
43+
}
44+
assert.Equal(t, tt.wantVal, params.Get(tt.wantKey))
45+
})
46+
}
47+
}
48+
49+
func TestActivityListCmd_ParentIDFlag(t *testing.T) {
50+
f := activityListCmd.Flags().Lookup("parent-id")
51+
assert.NotNil(t, f, "--parent-id flag should exist on activity list")
52+
if f != nil {
53+
assert.Equal(t, "", f.DefValue)
54+
assert.Contains(t, f.Usage, "code_execution")
55+
}
56+
}
57+
58+
func TestActivityExportCmd_ParentIDFlag(t *testing.T) {
59+
f := activityExportCmd.Flags().Lookup("parent-id")
60+
assert.NotNil(t, f, "--parent-id flag should exist on activity export")
61+
}
62+
63+
// The export command hand-builds its query string (it does not go through
64+
// ToQueryParams), so the flag has to be wired there separately or it is a
65+
// silent no-op.
66+
func TestActivityExportQueryParams_ParentID(t *testing.T) {
67+
prevParent := activityParentID
68+
prevFormat := activityExportFormat
69+
t.Cleanup(func() {
70+
activityParentID = prevParent
71+
activityExportFormat = prevFormat
72+
})
73+
74+
activityExportFormat = "json"
75+
76+
activityParentID = ""
77+
assert.False(t, activityExportQueryParams().Has("parent_id"), "empty parent id should be omitted")
78+
79+
activityParentID = "req-parent-123"
80+
q := activityExportQueryParams()
81+
assert.Equal(t, "req-parent-123", q.Get("parent_id"))
82+
assert.Equal(t, "json", q.Get("format"))
83+
}
84+
85+
func TestActivityChildToolCell(t *testing.T) {
86+
child := map[string]interface{}{"parent_id": "req-parent-123"}
87+
assert.Equal(t, "└ github:create_issue", activityChildToolCell(child, "github:create_issue"))
88+
89+
parent := map[string]interface{}{}
90+
assert.Equal(t, "code_execution", activityChildToolCell(parent, "code_execution"),
91+
"a record with no parent_id must not be marked as a child")
92+
}
93+
94+
func TestFormatParentMarker(t *testing.T) {
95+
withParent := map[string]interface{}{"parent_id": "req-parent-123"}
96+
marker := formatParentMarker(withParent)
97+
assert.Contains(t, marker, "child of")
98+
assert.Contains(t, marker, "req-parent-123")
99+
100+
assert.Equal(t, "", formatParentMarker(map[string]interface{}{}))
101+
}
102+
103+
func TestIsCodeExecutionParent(t *testing.T) {
104+
byToolName := map[string]interface{}{
105+
"type": "internal_tool_call",
106+
"tool_name": "code_execution",
107+
}
108+
assert.True(t, isCodeExecutionParent(byToolName))
109+
110+
byMetadata := map[string]interface{}{
111+
"type": "internal_tool_call",
112+
"metadata": map[string]interface{}{
113+
"internal_tool_name": "code_execution",
114+
},
115+
}
116+
assert.True(t, isCodeExecutionParent(byMetadata))
117+
118+
otherInternal := map[string]interface{}{
119+
"type": "internal_tool_call",
120+
"tool_name": "retrieve_tools",
121+
}
122+
assert.False(t, isCodeExecutionParent(otherInternal))
123+
124+
// A plain tool_call that happens to be named code_execution is not a parent
125+
// record — only the internal_tool_call record spawns sub-calls.
126+
wrongType := map[string]interface{}{
127+
"type": "tool_call",
128+
"tool_name": "code_execution",
129+
}
130+
assert.False(t, isCodeExecutionParent(wrongType))
131+
}
132+
133+
func TestFormatToolCallEvent_ChildMarker(t *testing.T) {
134+
event := map[string]interface{}{
135+
"source": "mcp",
136+
"server_name": "github",
137+
"tool_name": "create_issue",
138+
"status": "success",
139+
"parent_id": "req-parent-123",
140+
}
141+
line := formatToolCallEvent(event, "12:00:00")
142+
assert.True(t, strings.Contains(line, "[child of req-parent-123]"),
143+
"watch output should mark sub-calls, got: %s", line)
144+
}

0 commit comments

Comments
 (0)