File size: 9,328 Bytes
849ee7b
 
 
 
 
 
 
f4e9a2f
849ee7b
 
6ac8ef6
849ee7b
 
c8055f7
849ee7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ac8ef6
849ee7b
 
 
 
 
c8055f7
f4e9a2f
849ee7b
 
 
 
 
 
 
 
 
 
 
 
 
 
c8055f7
f4e9a2f
849ee7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ac8ef6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
849ee7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c8055f7
 
 
 
 
 
 
 
f4e9a2f
 
 
 
 
 
 
 
 
 
849ee7b
 
 
 
 
 
 
 
 
 
 
 
 
 
c8055f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
849ee7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c8055f7
849ee7b
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
"""Gradio entrypoint for the Trace Field Notes Hugging Face Space."""

from __future__ import annotations

import json
import tempfile
from pathlib import Path
from typing import Any, Optional

import gradio as gr
import spaces

from analyzer import analyze_trace_file
from model_runtime import MODEL_CHOICES
from parser import TraceParseError
from report_renderer import render_report


SPACE_URL = "https://huggingface.co/spaces/build-small-hackathon/trace-field-notes"

PRIVACY_WARNING = (
    "Agent traces can contain prompts, tool inputs, command outputs, local file paths, "
    "screenshots, secrets, private source code, and personal data. Redact before uploading. "
    "This app analyzes only visible agent narrative messages by default and does not need raw tool outputs."
)

HERO_MD = f"""
# Trace Field Notes

See how your coding agent got stuck, detoured, recovered, and claimed success.

Upload a Codex, Claude Code, or Pi Agent session log. The app extracts visible narrative messages, classifies difficulty episodes, and turns the session into a qualitative field report.

> {PRIVACY_WARNING}
"""

SESSION_PATHS_MD = """
## Find Your Session Log

| Agent | Local session directory |
|---|---|
| Codex | `~/.codex/sessions` |
| Claude Code | `~/.claude/projects` |
| Pi Agent | `~/.pi/agent/sessions` |

```bash
# Codex
ls ~/.codex/sessions

# Claude Code
ls ~/.claude/projects

# Pi Agent
ls ~/.pi/agent/sessions
```
"""

AGENT_PROMPT = f"""Use this Space as a tool.
1. Read: {SPACE_URL}/agents.md
2. Find my latest local agent session log:
   - Codex: ~/.codex/sessions
   - Claude Code: ~/.claude/projects
   - Pi Agent: ~/.pi/agent/sessions
3. Review and redact secrets or private code before upload.
4. Upload the JSONL to the Space.
5. Ask for narrative difficulty analysis.
6. Return the report. Do not publish the raw trace.
"""

CUSTOM_CSS = """
:root {
  --field-border: #d7d0c2;
  --field-ink: #202124;
  --field-muted: #605b52;
  --field-paper: #fbfaf7;
  --field-accent: #326b59;
}
.gradio-container {
  max-width: 1180px !important;
  color: var(--field-ink);
}
.trace-panel {
  border: 1px solid var(--field-border);
  border-radius: 8px;
  padding: 14px;
  background: var(--field-paper);
}
button.primary {
  background: var(--field-accent) !important;
  border-color: var(--field-accent) !important;
}
textarea, input {
  border-radius: 6px !important;
}
"""


def _analyze_trace_impl(
    trace_file: Any,
    include_user_context: bool = True,
    redact_secrets: bool = True,
    ignore_tool_calls: bool = True,
    report_style: str = "field_notes",
    analysis_engine: str = "deterministic",
    oauth_token: Optional[gr.OAuthToken] = None,
) -> tuple[str, dict[str, Any], str, str, str]:
    """Gradio-callable analysis endpoint."""

    if trace_file is None:
        raise gr.Error("Upload a .jsonl, .json, .txt, or .log trace file first.")

    path = uploaded_path(trace_file)
    try:
        result, redacted_narrative = analyze_trace_file(
            path,
            include_user_context=include_user_context,
            redact_secrets=redact_secrets,
            ignore_tool_calls=ignore_tool_calls,
            report_style=report_style,
            analysis_engine=analysis_engine,
            hf_token=oauth_token.token if oauth_token else None,
        )
    except TraceParseError as exc:
        raise gr.Error(str(exc)) from exc
    except Exception as exc:  # pragma: no cover - surfaced to the Space UI.
        raise gr.Error(f"Analysis failed: {exc}") from exc

    report_markdown = render_report(result)
    result_json = result.to_dict()
    redacted_file = write_temp_artifact("trace-field-notes-redacted-", ".md", redacted_narrative)
    report_file = write_temp_artifact("trace-field-notes-report-", ".md", report_markdown)
    json_file = write_temp_artifact(
        "trace-field-notes-episodes-",
        ".json",
        json.dumps(result_json, indent=2, ensure_ascii=False) + "\n",
    )
    return report_markdown, result_json, redacted_file, report_file, json_file


@spaces.GPU(duration=90)
def analyze_trace(
    trace_file: Any,
    include_user_context: bool = True,
    redact_secrets: bool = True,
    ignore_tool_calls: bool = True,
    report_style: str = "field_notes",
    analysis_engine: str = "deterministic",
    oauth_token: Optional[gr.OAuthToken] = None,
) -> tuple[str, dict[str, Any], str, str, str]:
    """ZeroGPU-visible Gradio endpoint."""

    return _analyze_trace_impl(
        trace_file=trace_file,
        include_user_context=include_user_context,
        redact_secrets=redact_secrets,
        ignore_tool_calls=ignore_tool_calls,
        report_style=report_style,
        analysis_engine=analysis_engine,
        oauth_token=oauth_token,
    )


def uploaded_path(trace_file: Any) -> Path:
    if isinstance(trace_file, (str, Path)):
        return Path(trace_file)
    name = getattr(trace_file, "name", None)
    if name:
        return Path(name)
    path = getattr(trace_file, "path", None)
    if path:
        return Path(path)
    raise gr.Error("Could not resolve the uploaded file path.")


def write_temp_artifact(prefix: str, suffix: str, content: str) -> str:
    with tempfile.NamedTemporaryFile(
        "w",
        encoding="utf-8",
        prefix=prefix,
        suffix=suffix,
        delete=False,
    ) as handle:
        handle.write(content)
        return handle.name


with gr.Blocks(
    title="Trace Field Notes",
    css=CUSTOM_CSS,
    theme=gr.themes.Base(
        primary_hue="green",
        neutral_hue="stone",
        font=[gr.themes.GoogleFont("Inter"), "system-ui", "sans-serif"],
        font_mono=[gr.themes.GoogleFont("IBM Plex Mono"), "ui-monospace", "monospace"],
    ),
) as demo:
    gr.Markdown(HERO_MD)

    with gr.Row(equal_height=False):
        with gr.Column(scale=3, elem_classes=["trace-panel"]):
            trace_input = gr.File(
                label="Upload Agent Session Log",
                file_types=[".jsonl", ".json", ".txt", ".log"],
                type="filepath",
            )
            with gr.Row():
                include_user_context = gr.Checkbox(
                    value=True,
                    label="Include user prompts as context",
                )
                redact_secrets = gr.Checkbox(
                    value=True,
                    label="Redact likely secrets before analysis",
                )
            ignore_tool_calls = gr.Checkbox(
                value=True,
                label="Ignore tool call contents",
                interactive=False,
            )
            report_style = gr.Radio(
                choices=[("Field notes", "field_notes")],
                value="field_notes",
                label="Report style",
                interactive=False,
            )
            analysis_engine = gr.Radio(
                choices=[
                    (str(choice["label"]), key)
                    for key, choice in MODEL_CHOICES.items()
                ],
                value="deterministic",
                label="Analysis engine",
            )
            with gr.Row():
                gr.LoginButton(
                    value="Sign in for model assist",
                    logout_value="Signed in as {}",
                    size="sm",
                )
            gr.Markdown(
                "Model-assisted modes use your signed-in Hugging Face OAuth token with the `inference-api` scope. "
                "The deterministic engine does not require sign-in."
            )
            analyze_button = gr.Button("Analyze My Trace", variant="primary")
        with gr.Column(scale=2):
            gr.Markdown(SESSION_PATHS_MD)

    with gr.Accordion("Agent-callable prompt", open=False):
        gr.Textbox(
            value=AGENT_PROMPT,
            label="Prompt for Codex or Claude Code",
            lines=9,
            interactive=False,
            show_copy_button=True,
        )

    gr.Examples(
        examples=[
            [
                "examples/sample_trace_redacted.jsonl",
                True,
                True,
                True,
                "field_notes",
                "deterministic",
            ]
        ],
        inputs=[
            trace_input,
            include_user_context,
            redact_secrets,
            ignore_tool_calls,
            report_style,
            analysis_engine,
        ],
        label="Try a redacted sample trace",
    )

    report_output = gr.Markdown(label="Field Report")
    with gr.Row():
        episode_json = gr.JSON(label="Structured Episode JSON")
    with gr.Row():
        redacted_download = gr.File(label="Download Redacted Narrative")
        report_download = gr.File(label="Download Markdown Report")
        json_download = gr.File(label="Download Structured JSON")

    analyze_button.click(
        analyze_trace,
        inputs=[
            trace_input,
            include_user_context,
            redact_secrets,
            ignore_tool_calls,
            report_style,
            analysis_engine,
        ],
        outputs=[
            report_output,
            episode_json,
            redacted_download,
            report_download,
            json_download,
        ],
        api_name="analyze_trace",
    )


if __name__ == "__main__":
    demo.launch()