Enforce runtime report contracts and recoverable session exports
This commit is contained in:
+20
@@ -878,3 +878,23 @@ run test uses a controlled executor, not a model). The isolated installed CLI
|
||||
also exercised the actual OpenCode API's session creation and skill activation,
|
||||
while capturing interactive launch arguments to avoid a model turn. Full
|
||||
model-driven authoring and Linux execution are **not tested**.
|
||||
|
||||
## Per-run report contract
|
||||
|
||||
`pipelines run NAME PROMPT --output-format html` requires a server-written
|
||||
`public/report.html` before run success; `--output-format markdown` requires
|
||||
`public/report.md`. Omit the option for historical behavior. The `run_pipeline`
|
||||
wire request and returned run record carry `output_format` (`html` or `markdown`),
|
||||
and retries inherit it. Request-key replays must retain the same format.
|
||||
Reports use nonempty terminal text output and are bounded to 2 MiB; missing,
|
||||
empty, unsafe or unverifiable files fail the run. This checks structure, not
|
||||
factual correctness. See [audit notes](notes/audit-deliverables.md).
|
||||
|
||||
`pipelines repair-session-export RUN_ID SEQUENCE [--json]` asks the runtime to
|
||||
read back that settled visit's exact recorded native session/backend. It never
|
||||
executes a stage or changes a definition. It writes a versioned repair artifact
|
||||
and appends evidence without replacing original files or outcomes. The protocol
|
||||
command is `repair_session_export` with `run_id` and `export_sequence`; the reply
|
||||
is a run detail containing each visit's `session_export_repairs`. CLI JSON emits
|
||||
the repair record with explicit completeness `status`, and incomplete repair
|
||||
exits nonzero. Recovery still depends on upstream session availability.
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
# Explicit run reports and session export completeness
|
||||
|
||||
Launch with `pipelines run NAME PROMPT --output-format html` (or `markdown`),
|
||||
or choose Report format in the web run dialog. The wire field is
|
||||
`output_format: "html" | "markdown"`; omission preserves prior behavior. It is
|
||||
stored with the run, compared for request-key replay and inherited by retries.
|
||||
This never changes a pipeline definition and is not inferred from prompt text.
|
||||
|
||||
Before success, the server takes the first nonblank string in the terminal
|
||||
output's `markdown`, `report`, `result`, `summary`, `message` fields, renders a
|
||||
report deterministically and records the verified artifact on the terminal
|
||||
visit. Fixed filenames are `public/report.html` and `public/report.md`.
|
||||
Both source and rendered output are capped at 2 MiB. Existing targets and
|
||||
symlinked directories are refused; reports are not overwritten. Missing or
|
||||
empty textual output, invalid targets and failed verification fail the run.
|
||||
HTML has inline styling, no executable raw HTML, and a restrictive content
|
||||
security policy preventing external resource loading. This is a structural
|
||||
file contract, **not factual validation** or proof that every task was fulfilled.
|
||||
|
||||
Session export is owned by the runtime, not the model stage. No stage prompt,
|
||||
definition or execution contract changes. Ownership is recorded during execution;
|
||||
export evidence also snapshots the original session ID/backend/directory before
|
||||
the run finishes. Evidence records `status` as `complete`, `empty`, `failed`,
|
||||
`partial`, or `unavailable`, plus attempt count and capture time. Zero-message
|
||||
readbacks are retried within the runtime's 10-second export budget and then
|
||||
recorded as **empty with a warning**, without producing success-looking HTML.
|
||||
Known temporary transport/store failures and interrupted JSON with verified
|
||||
identity can retry (at most 3 attempts). Wrong-session responses, unsupported
|
||||
envelopes and permanent CLI failures do not retry. A 200 ms delay does not prove
|
||||
that settling writes caused an invalid readback. Expected array/data envelopes
|
||||
are normalized, always retaining exact session identity validation.
|
||||
|
||||
`pipelines repair-session-export RUN_ID SEQUENCE --json` invokes the
|
||||
`repair_session_export` protocol command (`run_id`, `export_sequence`). It is
|
||||
read-only to the native session: no generation, resumption, interruption or
|
||||
stage execution. It requires a settled run/session and exact original ownership
|
||||
metadata, never guesses the backend, and appends a repair attempt to the run.
|
||||
Successful readback writes `public/session-repair-SEQUENCE-RANDOM.html` and
|
||||
records its bounded inventory entry. Original exports/evidence and run outcomes
|
||||
are preserved. Failed/empty/partial attempts remain inspectable warnings; CLI
|
||||
JSON returns the repair record itself and exits nonzero for incomplete evidence.
|
||||
There are at most 20 manual repair attempts per visit. Repairs cannot guarantee
|
||||
recovery if the upstream session or original directory is gone. The web pane
|
||||
explains original failure and lists repair attempts/links separately.
|
||||
|
||||
No production service, live run or historical data was modified for this audit.
|
||||
Unit/fixture tests cover reports, failure gates, retry inheritance, idempotency,
|
||||
migration, transient/permanent export readback, empty-export diagnostics and
|
||||
versioned repairs preserving originals. Browser screenshots are not captured; new web states
|
||||
remain partial pending a permitted isolated browser review.
|
||||
@@ -10,6 +10,7 @@ require (
|
||||
github.com/coder/websocket v1.8.15
|
||||
github.com/pmezard/go-difflib v1.0.0
|
||||
github.com/sahilm/fuzzy v0.1.1
|
||||
github.com/yuin/goldmark v1.7.8
|
||||
golang.org/x/term v0.31.0
|
||||
modernc.org/sqlite v1.60.1
|
||||
)
|
||||
@@ -42,7 +43,6 @@ require (
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||
github.com/rivo/uniseg v0.4.7 // indirect
|
||||
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
|
||||
github.com/yuin/goldmark v1.7.8 // indirect
|
||||
github.com/yuin/goldmark-emoji v1.0.5 // indirect
|
||||
golang.org/x/net v0.33.0 // indirect
|
||||
golang.org/x/sys v0.48.0 // indirect
|
||||
|
||||
+6
-2
@@ -34,6 +34,7 @@ type App struct {
|
||||
In io.Reader
|
||||
}
|
||||
type options struct {
|
||||
outputFormat string
|
||||
harness string
|
||||
url, name, prompt, title, template, workspace, paths, key, opencode, model string
|
||||
retryMode, retryStage, retryInputFile string
|
||||
@@ -147,6 +148,7 @@ func (a App) flags(command string) (*flag.FlagSet, *options) {
|
||||
f.IntVar(&o.version, "version", 0, "Version to run (default: latest)")
|
||||
f.StringVar(&o.model, "model", "", "Override the model for every stage of this run (PROVIDER/MODEL)")
|
||||
f.StringVar(&o.notifyMode, "notify", "auto", "Notifications for this run: auto (pipeline), on, or off")
|
||||
f.StringVar(&o.outputFormat, "output-format", "", "Require a server-written report: html or markdown")
|
||||
o.stageModels = stageModelFlags{}
|
||||
f.Var(o.stageModels, "stage-model", "Override one stage's model; repeatable (STAGE=PROVIDER/MODEL); beats --model")
|
||||
f.BoolVar(&o.wait, "wait", false, "In JSON mode, wait for a terminal run state")
|
||||
@@ -587,6 +589,8 @@ func (a App) Run(ctx context.Context, args []string) error {
|
||||
}
|
||||
_, err = fmt.Fprintln(a.Out, strings.Join(statusview.Lines(response.Status), "\n"))
|
||||
return err
|
||||
case "repair-session-export":
|
||||
return a.repairSessionExport(pos, call, o.json)
|
||||
case "debug":
|
||||
value, err := one()
|
||||
if err != nil {
|
||||
@@ -1309,7 +1313,7 @@ func (a App) run(ctx context.Context, pos []string, o *options, call callFunc, r
|
||||
if o.wait || o.key != "" || o.version > 0 {
|
||||
return fmt.Errorf("--wait, --request-key and --version require --json")
|
||||
}
|
||||
_, err := tea.NewProgram(tui.NewRun(ctx, o.url, tui.RunOptions{Pipeline: o.name, Prompt: o.prompt, Title: o.title, Workspace: workspace, Yes: o.yes, AllowTools: o.allow, WritePaths: paths, Model: o.model, StageModels: o.stageModels, NotifyMode: o.notifyMode}), tea.WithContext(ctx), tea.WithAltScreen()).Run()
|
||||
_, err := tea.NewProgram(tui.NewRun(ctx, o.url, tui.RunOptions{Pipeline: o.name, Prompt: o.prompt, Title: o.title, Workspace: workspace, Yes: o.yes, AllowTools: o.allow, WritePaths: paths, Model: o.model, StageModels: o.stageModels, NotifyMode: o.notifyMode, OutputFormat: o.outputFormat}), tea.WithContext(ctx), tea.WithAltScreen()).Run()
|
||||
return err
|
||||
}
|
||||
if o.name == "" || strings.TrimSpace(o.prompt) == "" {
|
||||
@@ -1331,7 +1335,7 @@ func (a App) run(ctx context.Context, pos []string, o *options, call callFunc, r
|
||||
if key == "" {
|
||||
key = fmt.Sprintf("cli-%d-%d", os.Getpid(), time.Now().UnixNano())
|
||||
}
|
||||
r, err := call(protocol.Message{Type: protocol.RunPipeline, PipelineID: p.ID, Version: version, RequestKey: key, Workspace: workspace, Title: o.title, Input: map[string]any{"prompt": o.prompt}, AllowTools: o.allow, WritePaths: paths, Model: o.model, StageModels: o.stageModels, NotifyMode: o.notifyMode})
|
||||
r, err := call(protocol.Message{Type: protocol.RunPipeline, PipelineID: p.ID, Version: version, RequestKey: key, Workspace: workspace, Title: o.title, Input: map[string]any{"prompt": o.prompt}, AllowTools: o.allow, WritePaths: paths, Model: o.model, StageModels: o.stageModels, NotifyMode: o.notifyMode, OutputFormat: o.outputFormat})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
+55
-53
@@ -2,62 +2,64 @@ package cli
|
||||
|
||||
// Help is also the public command inventory for agents and parity checks.
|
||||
var usage = map[string]string{
|
||||
"provider-check": "provider-check --model PROVIDER/MODEL [--yes] [--json]",
|
||||
"new": "new [name] [prompt] [--template KEY] [--no-open | --json]",
|
||||
"edit": "edit [name|id] [prompt] [--version N] [--no-open | --json]",
|
||||
"drafts": "drafts [--json]",
|
||||
"draft-delete": "draft-delete <draft.md> [--json]",
|
||||
"validate": "validate <draft.md> [--json]",
|
||||
"submit": "submit <draft.md> [--name NAME] [--create-only] [--yes] [--json]",
|
||||
"list": "list [--json]",
|
||||
"search": "search QUERY [--type KIND] [--limit N] [--json]",
|
||||
"show": "show <name|id> [--version N] [--json]",
|
||||
"versions": "versions <name|id> [--json]",
|
||||
"export": "export [name|id ...] [--output FILE.zip] [--all] [--version N]",
|
||||
"import": "import [FILE.zip] [--all] [--on-conflict ask|keep|both|import] [--yes] [--json]",
|
||||
"templates": "templates [--json]",
|
||||
"template": "template <key> [--source | --json]",
|
||||
"run": "run [name|id] [prompt] [--title TITLE] [--model PROVIDER/MODEL] [--stage-model STAGE=PROVIDER/MODEL ...] [--json [--wait [--wait-timeout DURATION]]]",
|
||||
"runs": "runs [--pipeline NAME] [--json]",
|
||||
"inspect": "inspect <run-id> [--json]",
|
||||
"wait": "wait <run-id> [run-id ...] [--timeout DURATION] [--json]",
|
||||
"cancel": "cancel <run-id> [--json]",
|
||||
"decide": "decide <run-id> [stage] --option KEY [--feedback TEXT] [--json]",
|
||||
"retry": "retry <run-id> [--mode resume|restart] [--stage KEY [--visit N]] [--version N] [--input-file FILE|-] [--json [--wait [--wait-timeout DURATION]]]",
|
||||
"title": "title <run-id> \"<title>\" [--json]",
|
||||
"debug": "debug <run-id> [--json | --no-open]",
|
||||
"chat-adopt": "chat-adopt <session-id> [--json]",
|
||||
"connection": "connection [--json]",
|
||||
"ping": "ping [--json]",
|
||||
"version": "version [--json]",
|
||||
"status": "status [--json]",
|
||||
"stats": "stats [--json]",
|
||||
"rpc": "rpc <request.json|->",
|
||||
"retention": "retention report|apply [--older-than DURATION] [--keep N] [--db FILE] [--artifacts DIR] [--json] [--yes]",
|
||||
"tui": "tui",
|
||||
"repair-session-export": "repair-session-export RUN_ID SEQUENCE [--json]",
|
||||
"provider-check": "provider-check --model PROVIDER/MODEL [--yes] [--json]",
|
||||
"new": "new [name] [prompt] [--template KEY] [--no-open | --json]",
|
||||
"edit": "edit [name|id] [prompt] [--version N] [--no-open | --json]",
|
||||
"drafts": "drafts [--json]",
|
||||
"draft-delete": "draft-delete <draft.md> [--json]",
|
||||
"validate": "validate <draft.md> [--json]",
|
||||
"submit": "submit <draft.md> [--name NAME] [--create-only] [--yes] [--json]",
|
||||
"list": "list [--json]",
|
||||
"search": "search QUERY [--type KIND] [--limit N] [--json]",
|
||||
"show": "show <name|id> [--version N] [--json]",
|
||||
"versions": "versions <name|id> [--json]",
|
||||
"export": "export [name|id ...] [--output FILE.zip] [--all] [--version N]",
|
||||
"import": "import [FILE.zip] [--all] [--on-conflict ask|keep|both|import] [--yes] [--json]",
|
||||
"templates": "templates [--json]",
|
||||
"template": "template <key> [--source | --json]",
|
||||
"run": "run [name|id] [prompt] [--title TITLE] [--model PROVIDER/MODEL] [--stage-model STAGE=PROVIDER/MODEL ...] [--json [--wait [--wait-timeout DURATION]]]",
|
||||
"runs": "runs [--pipeline NAME] [--json]",
|
||||
"inspect": "inspect <run-id> [--json]",
|
||||
"wait": "wait <run-id> [run-id ...] [--timeout DURATION] [--json]",
|
||||
"cancel": "cancel <run-id> [--json]",
|
||||
"decide": "decide <run-id> [stage] --option KEY [--feedback TEXT] [--json]",
|
||||
"retry": "retry <run-id> [--mode resume|restart] [--stage KEY [--visit N]] [--version N] [--input-file FILE|-] [--json [--wait [--wait-timeout DURATION]]]",
|
||||
"title": "title <run-id> \"<title>\" [--json]",
|
||||
"debug": "debug <run-id> [--json | --no-open]",
|
||||
"chat-adopt": "chat-adopt <session-id> [--json]",
|
||||
"connection": "connection [--json]",
|
||||
"ping": "ping [--json]",
|
||||
"version": "version [--json]",
|
||||
"status": "status [--json]",
|
||||
"stats": "stats [--json]",
|
||||
"rpc": "rpc <request.json|->",
|
||||
"retention": "retention report|apply [--older-than DURATION] [--keep N] [--db FILE] [--artifacts DIR] [--json] [--yes]",
|
||||
"tui": "tui",
|
||||
}
|
||||
|
||||
var notes = map[string]string{
|
||||
"version": "Identify the local CLI binary without connecting to a server. --version is an alias. Use ping --json to identify the running server separately.",
|
||||
"ping": "Pong includes the running server's compiled build.version, build.commit and build.dirty (null when unknown). This identifies the server, not the client or runtime workspace. Older servers omit build. The client compares its own build/protocol against the server: a same-protocol build mismatch prints one warning to stderr, a server speaking a newer protocol fails the command, and a response carrying a field this client does not model prints a one-time unrecognized-field notice instead of dropping it silently.",
|
||||
"connection": "Prints the server endpoint and phone notification endpoints. Like ping, it reports the client/server build relationship and warns or fails rather than silently proceeding against a mismatched server.",
|
||||
"status": "Reports server, host and ntfy publishing health. The client compares its build/protocol against the server and warns once (or fails on a newer protocol) rather than proceeding silently.",
|
||||
"export": "Without a name, select pipelines with Space and save a ZIP in the current directory. --output writes a portable definition bundle; --all skips selection. One name without --output prints Markdown as before. No runs, credentials or old versions are exported.",
|
||||
"import": "Select definitions from a ZIP. Identical existing definitions are skipped. For differing names, choose keep existing, keep both under a new name, or use imported as a new version (history retained). --on-conflict supplies an explicit automation policy; --yes only approves final publication. Schedules may activate; no run history is imported.",
|
||||
"new": "Create an optional template copy or a one-stage starter. Opens the configured default harness; --harness opencode|pi|nvim overrides it. No submission is performed.",
|
||||
"edit": "Omit the name to open fuzzy selection. Original immutable source is copied to a private draft. The configured harness edits that draft, never the live definition.",
|
||||
"validate": "Uses the same server parser as submission. Does not persist or activate schedules. Structural validity does not prove runtime/tool availability.",
|
||||
"submit": "Confirm publication at the prompt, or use --yes for explicit automation; schedules activate on acceptance. Managed drafts provide the name and base-version guard. --force never bypasses validation.",
|
||||
"run": "Runs in your current directory after action confirmation (--yes skips it). Select a pipeline and compose a multiline task with Ctrl+S. Stage tools are enabled automatically; no file list or typed RUN confirmation. Cancellation does not undo edits. --title sets an explicit run title; without it the title is derived from the prompt. --model PROVIDER/MODEL overrides the model for every stage of that run; --stage-model STAGE=PROVIDER/MODEL overrides one stage and is repeatable, with a repeated key last-wins. Precedence is per-stage > whole-run > the model pinned in the pipeline version; overrides apply to that run only and never change the pinned definition. An unknown stage key or a malformed provider/model is refused before the run starts, naming the valid stage keys. In JSON mode --wait polls until terminal; --wait-timeout bounds only the client wait after admission and is independent of per-request --timeout. A reached wait limit leaves the run active: inspect read-only with `inspect <run-id> --json`, or reattach with --wait reusing the original --request-key (never an automatic replay or cancel).",
|
||||
"retry": "Resume a failed stage by default, restoring the completed prefix handoff; use --mode=restart to start at entry. --stage selects a recorded stage (use --visit for repeated stages). --input-file accepts a bounded JSON object, merged with replacement at the selected stage; '-' reads stdin. Original history and pinned identity are retained; --version selects an explicit published version of the same pipeline only. --wait-timeout leaves an active retry for later read-only inspection or reattachment with the original --request-key, rather than starting a duplicate retry.",
|
||||
"title": "Rename a run's operator-facing title after the fact without touching its input, steps or execution. The title is collapsed to a single bounded line and recorded as explicitly set. `pipelines runs` and `pipelines inspect` then show it.",
|
||||
"wait": "Block until each named run reaches a terminal status - succeeded, failed, canceled or interrupted, and also through a run waiting on a person - then print one line per run with its id, status and outcome, its stage and how long it took, and exit 0 even for a run that finished unsuccessfully. Several ids wait together in one command. The exit is non-zero only for a client-side failure: an unknown run id, a connection that could not be established, or --timeout expiring while a run is still active; a timeout says so plainly, never cancels, retries or otherwise touches a run, and is never reported as its outcome. Read-only: it needs no workspace, is not refused by the workspace guard and writes nothing. --json prints the final run envelope instead of the line (one envelope for one id, an array for several).",
|
||||
"decide": "Choose an option for a run waiting on a human stage. The run must be `waiting`; the option must be one of that stage's non-error edges. The stage is inferred from the run's current stage unless named. The choice and optional --feedback are recorded as the step's evidence (visible in inspect and the TUI) and the run resumes along the chosen edge. An unknown option is refused with the valid list; a repeated decision for an already-decided visit does not double-apply. Works across a server restart because the wait is persisted.",
|
||||
"debug": "Open a fresh configured harness conversation to investigate a run using inspect/show/runs CLI queries. --harness opencode|pi|nvim chooses another configured harness or read-only nvim packet. Does not resume implementation or retry. --json returns the prompt without launching anything.",
|
||||
"rpc": "Accepts a JSON protocol envelope; '-' reads stdin. Every TUI server operation is available here too. Mutating commands are not dry runs. The omni-search command is `search` (fields query, optional kind and limit) and answers with ranked `search_results`.",
|
||||
"chat-adopt": "Adopt a session that already exists on the configured OpenCode endpoint so it appears in chat_sessions and can be continued. The id must be one the endpoint reports; its directory and title are taken from the endpoint's own record (never guessed), and a missing id records nothing. Adopting is always explicit and does not change the server's endpoint choice.",
|
||||
"retention": "Local, explicit run-history retention; never automatic and never a side effect of another command. `report` is strictly read-only and prints what a policy would remove; `apply` removes it after approval (--yes for scripted use). Policy: --older-than DURATION selects runs created before now minus that rolling UTC duration (Go durations plus d/w; not a calendar boundary), --keep N keeps the newest N runs by run id and selects older ranks (--keep 0 keeps none). Both together are the conservative intersection; at least one is required. Protected and never removed: queued/running runs, runs waiting on a person (waiting run or step), runs with an active cleanup agent, and any run referenced by a retained run's retry provenance (transitively). A run that becomes protected before deletion aborts the whole apply. Artifacts at <root>/<run> are removed with their run and kept with protected/retained runs; the database is VACUUMed and its WAL checkpointed, with measured reclaimed bytes. Definitions and versions are immutable and never touched. --db must name an existing database (default: the standard data directory); --artifacts defaults to artifacts/ beside --db. Works without a running server.",
|
||||
"search": "One read-only query over pipelines, versions, runs, steps, field text and artifacts. QUERY is free text, a bare status word (queued, running, succeeded, failed, canceled, interrupted) selecting those runs, or a lone date expression (today, yesterday, this week [Monday 00:00 local], or YYYY-MM-DD) selecting the runs created in that window. --type is the only narrowing mechanism and takes one kind: pipeline, version, run, step, field or artifact; an unknown kind is an error. Ranking: an exact name match (3) beats a name substring (2) beats a body-text match (1); equal tiers break by recency, then a stable kind/target tie-break, and every hit reports score, matched_field and recency so a client can re-rank. Bounds: the result cap defaults to 50 and clamps to 200 (0 = default); every kind scans at most 2000 rows and field-text scanning is additionally capped at 2000 steps / 1 MiB; Truncated plus scanned_steps, scanned_bytes and scan_bound_bytes report when a bound applied. Targets are stable: pipeline:<id>, pipeline:<id>:v<version>, run:<id>, run:<id>:<sequence>, run:<id>:<sequence>:<field>, or an artifact URL.",
|
||||
"repair-session-export": "Read-only native session readback for a settled run visit, using its exact recorded session/backend. Writes a versioned repair file and appends repair evidence, preserving originals and the run outcome. Empty/failed/partial repairs remain warnings; upstream session availability cannot be guaranteed.",
|
||||
"version": "Identify the local CLI binary without connecting to a server. --version is an alias. Use ping --json to identify the running server separately.",
|
||||
"ping": "Pong includes the running server's compiled build.version, build.commit and build.dirty (null when unknown). This identifies the server, not the client or runtime workspace. Older servers omit build. The client compares its own build/protocol against the server: a same-protocol build mismatch prints one warning to stderr, a server speaking a newer protocol fails the command, and a response carrying a field this client does not model prints a one-time unrecognized-field notice instead of dropping it silently.",
|
||||
"connection": "Prints the server endpoint and phone notification endpoints. Like ping, it reports the client/server build relationship and warns or fails rather than silently proceeding against a mismatched server.",
|
||||
"status": "Reports server, host and ntfy publishing health. The client compares its build/protocol against the server and warns once (or fails on a newer protocol) rather than proceeding silently.",
|
||||
"export": "Without a name, select pipelines with Space and save a ZIP in the current directory. --output writes a portable definition bundle; --all skips selection. One name without --output prints Markdown as before. No runs, credentials or old versions are exported.",
|
||||
"import": "Select definitions from a ZIP. Identical existing definitions are skipped. For differing names, choose keep existing, keep both under a new name, or use imported as a new version (history retained). --on-conflict supplies an explicit automation policy; --yes only approves final publication. Schedules may activate; no run history is imported.",
|
||||
"new": "Create an optional template copy or a one-stage starter. Opens the configured default harness; --harness opencode|pi|nvim overrides it. No submission is performed.",
|
||||
"edit": "Omit the name to open fuzzy selection. Original immutable source is copied to a private draft. The configured harness edits that draft, never the live definition.",
|
||||
"validate": "Uses the same server parser as submission. Does not persist or activate schedules. Structural validity does not prove runtime/tool availability.",
|
||||
"submit": "Confirm publication at the prompt, or use --yes for explicit automation; schedules activate on acceptance. Managed drafts provide the name and base-version guard. --force never bypasses validation.",
|
||||
"run": "Runs in your current directory after action confirmation (--yes skips it). Select a pipeline and compose a multiline task with Ctrl+S. Stage tools are enabled automatically; no file list or typed RUN confirmation. Cancellation does not undo edits. --title sets an explicit run title; without it the title is derived from the prompt. --model PROVIDER/MODEL overrides the model for every stage of that run; --stage-model STAGE=PROVIDER/MODEL overrides one stage and is repeatable, with a repeated key last-wins. Precedence is per-stage > whole-run > the model pinned in the pipeline version; overrides apply to that run only and never change the pinned definition. An unknown stage key or a malformed provider/model is refused before the run starts, naming the valid stage keys. In JSON mode --wait polls until terminal; --wait-timeout bounds only the client wait after admission and is independent of per-request --timeout. A reached wait limit leaves the run active: inspect read-only with `inspect <run-id> --json`, or reattach with --wait reusing the original --request-key (never an automatic replay or cancel).",
|
||||
"retry": "Resume a failed stage by default, restoring the completed prefix handoff; use --mode=restart to start at entry. --stage selects a recorded stage (use --visit for repeated stages). --input-file accepts a bounded JSON object, merged with replacement at the selected stage; '-' reads stdin. Original history and pinned identity are retained; --version selects an explicit published version of the same pipeline only. --wait-timeout leaves an active retry for later read-only inspection or reattachment with the original --request-key, rather than starting a duplicate retry.",
|
||||
"title": "Rename a run's operator-facing title after the fact without touching its input, steps or execution. The title is collapsed to a single bounded line and recorded as explicitly set. `pipelines runs` and `pipelines inspect` then show it.",
|
||||
"wait": "Block until each named run reaches a terminal status - succeeded, failed, canceled or interrupted, and also through a run waiting on a person - then print one line per run with its id, status and outcome, its stage and how long it took, and exit 0 even for a run that finished unsuccessfully. Several ids wait together in one command. The exit is non-zero only for a client-side failure: an unknown run id, a connection that could not be established, or --timeout expiring while a run is still active; a timeout says so plainly, never cancels, retries or otherwise touches a run, and is never reported as its outcome. Read-only: it needs no workspace, is not refused by the workspace guard and writes nothing. --json prints the final run envelope instead of the line (one envelope for one id, an array for several).",
|
||||
"decide": "Choose an option for a run waiting on a human stage. The run must be `waiting`; the option must be one of that stage's non-error edges. The stage is inferred from the run's current stage unless named. The choice and optional --feedback are recorded as the step's evidence (visible in inspect and the TUI) and the run resumes along the chosen edge. An unknown option is refused with the valid list; a repeated decision for an already-decided visit does not double-apply. Works across a server restart because the wait is persisted.",
|
||||
"debug": "Open a fresh configured harness conversation to investigate a run using inspect/show/runs CLI queries. --harness opencode|pi|nvim chooses another configured harness or read-only nvim packet. Does not resume implementation or retry. --json returns the prompt without launching anything.",
|
||||
"rpc": "Accepts a JSON protocol envelope; '-' reads stdin. Every TUI server operation is available here too. Mutating commands are not dry runs. The omni-search command is `search` (fields query, optional kind and limit) and answers with ranked `search_results`.",
|
||||
"chat-adopt": "Adopt a session that already exists on the configured OpenCode endpoint so it appears in chat_sessions and can be continued. The id must be one the endpoint reports; its directory and title are taken from the endpoint's own record (never guessed), and a missing id records nothing. Adopting is always explicit and does not change the server's endpoint choice.",
|
||||
"retention": "Local, explicit run-history retention; never automatic and never a side effect of another command. `report` is strictly read-only and prints what a policy would remove; `apply` removes it after approval (--yes for scripted use). Policy: --older-than DURATION selects runs created before now minus that rolling UTC duration (Go durations plus d/w; not a calendar boundary), --keep N keeps the newest N runs by run id and selects older ranks (--keep 0 keeps none). Both together are the conservative intersection; at least one is required. Protected and never removed: queued/running runs, runs waiting on a person (waiting run or step), runs with an active cleanup agent, and any run referenced by a retained run's retry provenance (transitively). A run that becomes protected before deletion aborts the whole apply. Artifacts at <root>/<run> are removed with their run and kept with protected/retained runs; the database is VACUUMed and its WAL checkpointed, with measured reclaimed bytes. Definitions and versions are immutable and never touched. --db must name an existing database (default: the standard data directory); --artifacts defaults to artifacts/ beside --db. Works without a running server.",
|
||||
"search": "One read-only query over pipelines, versions, runs, steps, field text and artifacts. QUERY is free text, a bare status word (queued, running, succeeded, failed, canceled, interrupted) selecting those runs, or a lone date expression (today, yesterday, this week [Monday 00:00 local], or YYYY-MM-DD) selecting the runs created in that window. --type is the only narrowing mechanism and takes one kind: pipeline, version, run, step, field or artifact; an unknown kind is an error. Ranking: an exact name match (3) beats a name substring (2) beats a body-text match (1); equal tiers break by recency, then a stable kind/target tie-break, and every hit reports score, matched_field and recency so a client can re-rank. Bounds: the result cap defaults to 50 and clamps to 200 (0 = default); every kind scans at most 2000 rows and field-text scanning is additionally capped at 2000 steps / 1 MiB; Truncated plus scanned_steps, scanned_bytes and scan_bound_bytes report when a bound applied. Targets are stable: pipeline:<id>, pipeline:<id>:v<version>, run:<id>, run:<id>:<sequence>, run:<id>:<sequence>:<field>, or an artifact URL.",
|
||||
}
|
||||
|
||||
var serviceHelp = map[string]string{
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestOutputFormatFlag(t *testing.T) {
|
||||
for _, format := range []string{"html", "markdown"} {
|
||||
app := App{Out: &bytes.Buffer{}, Err: &bytes.Buffer{}}
|
||||
f, o := app.flags("run")
|
||||
if err := parse(f, []string{"--output-format", format}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if o.outputFormat != format {
|
||||
t.Fatalf("%q", o.outputFormat)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
func (a App) repairSessionExport(pos []string, call func(protocol.Message) (protocol.Message, error), jsonOutput bool) error {
|
||||
if len(pos) != 2 {
|
||||
return fmt.Errorf("repair-session-export requires RUN_ID SEQUENCE")
|
||||
}
|
||||
id, err := strconv.ParseInt(pos[0], 10, 64)
|
||||
if err != nil || id <= 0 {
|
||||
return fmt.Errorf("run ID must be positive")
|
||||
}
|
||||
sequence, err := strconv.Atoi(pos[1])
|
||||
if err != nil || sequence <= 0 {
|
||||
return fmt.Errorf("sequence must be positive")
|
||||
}
|
||||
response, err := call(protocol.Message{Type: protocol.RepairSessionExport, RunID: id, ExportSequence: sequence})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if response.RunDetail == nil {
|
||||
return fmt.Errorf("server returned no repair evidence")
|
||||
}
|
||||
for _, step := range response.RunDetail.Steps {
|
||||
if step.Sequence == sequence && len(step.SessionExportRepairs) > 0 {
|
||||
evidence := step.SessionExportRepairs[len(step.SessionExportRepairs)-1]
|
||||
if jsonOutput {
|
||||
if err := a.output(evidence); err != nil {
|
||||
return err
|
||||
}
|
||||
if evidence.Status != "complete" {
|
||||
return fmt.Errorf("session export repair incomplete: %s", evidence.Status)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
fmt.Fprintf(a.Out, "Session export repair: %s · %d messages\n", evidence.Status, evidence.Messages)
|
||||
if evidence.HTML != "" {
|
||||
fmt.Fprintln(a.Out, evidence.HTML)
|
||||
}
|
||||
if evidence.Diagnostic != "" {
|
||||
fmt.Fprintln(a.Out, evidence.Diagnostic)
|
||||
}
|
||||
if evidence.Status != "complete" {
|
||||
return fmt.Errorf("session export repair incomplete: %s", evidence.Status)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("server returned no recorded repair attempt")
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRepairSessionExportRequestAndIncompleteExit(t *testing.T) {
|
||||
for _, status := range []string{"complete", "empty", "failed", "partial"} {
|
||||
var out bytes.Buffer
|
||||
app := App{Out: &out}
|
||||
err := app.repairSessionExport([]string{"7", "2"}, func(request protocol.Message) (protocol.Message, error) {
|
||||
if request.Type != protocol.RepairSessionExport || request.RunID != 7 || request.ExportSequence != 2 {
|
||||
t.Fatalf("wrong repair request: %+v", request)
|
||||
}
|
||||
return protocol.Message{RunDetail: &protocol.RunDetail{Steps: []protocol.RunStep{{Sequence: 2, SessionExportRepairs: []*protocol.SessionExport{{Status: status, SessionID: "ses_original"}}}}}}, nil
|
||||
}, true)
|
||||
if (err != nil) != (status != "complete") || !strings.Contains(out.String(), `"status": "`+status+`"`) {
|
||||
t.Fatalf("%s: %v %s", status, err, out.String())
|
||||
}
|
||||
if strings.Contains(out.String(), "run_summary") {
|
||||
t.Fatal("repair JSON must not masquerade as original run success")
|
||||
}
|
||||
}
|
||||
}
|
||||
+171
-60
@@ -4,7 +4,9 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
@@ -15,6 +17,10 @@ import (
|
||||
// never presented as the whole session.
|
||||
const MaxSessionExport = 8 << 20
|
||||
|
||||
var ErrEmptySessionExport = errors.New("session recorded but native export contains no messages")
|
||||
var errTransientExport = errors.New("native session export temporarily unavailable")
|
||||
var errSessionIdentity = errors.New("native export session identity mismatch")
|
||||
|
||||
// nativeExportResult is one bounded `opencode export` readback. envelope is the
|
||||
// stdout prefix the CLI wrote, capped at the caller's limit; truncated is true
|
||||
// when the CLI wrote more than the cap, so the prefix is not valid whole JSON.
|
||||
@@ -45,6 +51,15 @@ func exportNative(ctx context.Context, executable, directory, backend, id string
|
||||
cmd.Stdout = stdout
|
||||
cmd.Stderr = stderr
|
||||
if err := cmd.Run(); err != nil {
|
||||
// Only known temporary transport/store failures are retried. Do not
|
||||
// expose stderr: it may contain session text or credentials.
|
||||
text := strings.ToLower(string(stderr.Bytes()))
|
||||
for _, temporary := range []string{"temporarily unavailable", "connection refused", "database is locked", "server is busy"} {
|
||||
if strings.Contains(text, temporary) {
|
||||
err = errors.Join(errTransientExport, err)
|
||||
break
|
||||
}
|
||||
}
|
||||
return nativeExportResult{envelope: stdout.Bytes(), truncated: stdout.overflow}, err
|
||||
}
|
||||
return nativeExportResult{envelope: stdout.Bytes(), truncated: stdout.overflow}, nil
|
||||
@@ -52,18 +67,47 @@ func exportNative(ctx context.Context, executable, directory, backend, id string
|
||||
|
||||
// validNativeExport is the exact-ID and messages-array validation
|
||||
// Connection.Messages and ExportSession share: the envelope must parse, carry an
|
||||
// info.id equal to wantID and a non-empty JSON messages array.
|
||||
// info.id equal to wantID and a JSON messages array. An empty array is valid
|
||||
// readback syntax, not complete evidence; consumers must label it empty.
|
||||
func validNativeExport(data []byte, wantID string) (json.RawMessage, bool) {
|
||||
var envelope struct {
|
||||
Info struct {
|
||||
ID string `json:"id"`
|
||||
} `json:"info"`
|
||||
Messages json.RawMessage `json:"messages"`
|
||||
Data json.RawMessage `json:"data"`
|
||||
}
|
||||
if json.Unmarshal(data, &envelope) != nil || envelope.Info.ID != wantID || len(envelope.Messages) == 0 || envelope.Messages[0] != '[' {
|
||||
if json.Unmarshal(data, &envelope) != nil {
|
||||
return nil, false
|
||||
}
|
||||
return envelope.Messages, true
|
||||
// The HTTP/CLI readback can wrap a payload in data. Only unwrap an
|
||||
// otherwise absent envelope, never use data to override a mismatched ID.
|
||||
if envelope.Info.ID == "" && len(envelope.Messages) == 0 && len(envelope.Data) > 0 {
|
||||
if json.Unmarshal(envelope.Data, &envelope) != nil {
|
||||
return nil, false
|
||||
}
|
||||
}
|
||||
if envelope.Info.ID != wantID || len(envelope.Messages) == 0 {
|
||||
return nil, false
|
||||
}
|
||||
messages := bytes.TrimSpace(envelope.Messages)
|
||||
if len(messages) > 0 && messages[0] == '{' {
|
||||
var wrapped struct {
|
||||
Data json.RawMessage `json:"data"`
|
||||
}
|
||||
if json.Unmarshal(messages, &wrapped) != nil {
|
||||
return nil, false
|
||||
}
|
||||
messages = bytes.TrimSpace(wrapped.Data)
|
||||
}
|
||||
if len(messages) == 0 || messages[0] != '[' {
|
||||
return nil, false
|
||||
}
|
||||
var records []json.RawMessage
|
||||
if json.Unmarshal(messages, &records) != nil {
|
||||
return nil, false
|
||||
}
|
||||
return messages, true
|
||||
}
|
||||
|
||||
// ExportedPart is one decoded content element of an exported session. Raw keeps
|
||||
@@ -94,6 +138,7 @@ type ExportedMessage struct {
|
||||
// export. Truncated is true when the source exceeded MaxSessionExport; Messages
|
||||
// then covers the complete turns decoded before the cut.
|
||||
type ExportedSession struct {
|
||||
Attempts int
|
||||
ID string
|
||||
Backend string
|
||||
Messages []ExportedMessage
|
||||
@@ -105,68 +150,123 @@ type ExportedSession struct {
|
||||
// decodes the v1/v2 message and part shapes through chatMessageRecords and, when
|
||||
// the source exceeded MaxSessionExport, streams the complete turns decoded from
|
||||
// the bounded prefix and reports Truncated instead of failing. It performs no
|
||||
// writes, no resumption, no idleness wait and no retries.
|
||||
// writes, no resumption and no idleness wait. Empty readbacks and explicitly
|
||||
// temporary failures get at most three attempts within the caller's deadline;
|
||||
// wrong identity and permanent failures never retry.
|
||||
func ExportSession(ctx context.Context, executable, directory, backend, id string) (ExportedSession, error) {
|
||||
if !sessionID.MatchString(id) {
|
||||
return ExportedSession{}, fmt.Errorf("invalid session ID")
|
||||
}
|
||||
exported, err := exportNative(ctx, executable, directory, backend, id, MaxSessionExport)
|
||||
if err != nil {
|
||||
if ctx.Err() != nil {
|
||||
return ExportedSession{}, ctx.Err()
|
||||
}
|
||||
return ExportedSession{}, fmt.Errorf("native session export failed: %w", err)
|
||||
}
|
||||
session := ExportedSession{ID: id, Backend: backend}
|
||||
if !exported.truncated {
|
||||
messages, ok := validNativeExport(exported.envelope, id)
|
||||
if !ok {
|
||||
// The CLI can observe a session whose final write is still landing
|
||||
// (the stage just settled). One short, bounded retry recovers the
|
||||
// session evidence instead of dropping session.html for the stage.
|
||||
timer := time.NewTimer(200 * time.Millisecond)
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
timer.Stop()
|
||||
return ExportedSession{}, ctx.Err()
|
||||
case <-timer.C:
|
||||
}
|
||||
retried, retryErr := exportNative(ctx, executable, directory, backend, id, MaxSessionExport)
|
||||
if retryErr != nil {
|
||||
if ctx.Err() != nil {
|
||||
return ExportedSession{}, ctx.Err()
|
||||
}
|
||||
return ExportedSession{}, fmt.Errorf("native session export failed: %w", retryErr)
|
||||
}
|
||||
exported = retried
|
||||
if !exported.truncated {
|
||||
messages, ok = validNativeExport(exported.envelope, id)
|
||||
if !ok {
|
||||
return ExportedSession{}, fmt.Errorf("native export returned invalid or mismatched session evidence")
|
||||
}
|
||||
}
|
||||
}
|
||||
if !exported.truncated {
|
||||
records, err := chatMessageRecords(messages)
|
||||
if err != nil {
|
||||
return ExportedSession{}, err
|
||||
}
|
||||
session.Messages = exportedMessages(records)
|
||||
for attempt := 0; attempt < 3; attempt++ {
|
||||
session, err := exportSessionOnce(ctx, executable, directory, backend, id)
|
||||
session.Attempts = attempt + 1
|
||||
if err == nil {
|
||||
return session, nil
|
||||
}
|
||||
if ctx.Err() != nil {
|
||||
return session, ctx.Err()
|
||||
}
|
||||
if attempt == 2 || (!errors.Is(err, ErrEmptySessionExport) && !errors.Is(err, errTransientExport)) {
|
||||
return session, err
|
||||
}
|
||||
// Bounded recovery, not a causal claim about why readback was empty.
|
||||
timer := time.NewTimer(time.Duration(attempt+1) * 200 * time.Millisecond)
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
timer.Stop()
|
||||
return session, ctx.Err()
|
||||
case <-timer.C:
|
||||
}
|
||||
}
|
||||
// The source exceeded the cap, so the session is larger than what was read
|
||||
// and must never be presented as whole. The prefix still yields every turn
|
||||
// that decoded before the cut.
|
||||
records, _, err := decodeExportPrefix(exported.envelope, id)
|
||||
panic("unreachable")
|
||||
}
|
||||
|
||||
func exportSessionOnce(ctx context.Context, executable, directory, backend, id string) (ExportedSession, error) {
|
||||
session := ExportedSession{ID: id, Backend: backend}
|
||||
exported, err := exportNative(ctx, executable, directory, backend, id, MaxSessionExport)
|
||||
if err != nil {
|
||||
return ExportedSession{}, err
|
||||
return session, fmt.Errorf("native session export failed: %w", err)
|
||||
}
|
||||
if exported.truncated {
|
||||
records, _, err := decodeExportPrefix(exported.envelope, id)
|
||||
if err != nil {
|
||||
return session, err
|
||||
}
|
||||
session.Messages, session.Truncated = exportedMessages(records), true
|
||||
if len(session.Messages) == 0 {
|
||||
return session, ErrEmptySessionExport
|
||||
}
|
||||
return session, nil
|
||||
}
|
||||
messages, ok := validNativeExport(exported.envelope, id)
|
||||
if !ok {
|
||||
// Incomplete JSON is a possible interrupted read; valid but wrong
|
||||
// identity/schema is permanent and must never be retried as settling.
|
||||
var syntax *json.SyntaxError
|
||||
var envelope struct {
|
||||
Info struct {
|
||||
ID string `json:"id"`
|
||||
} `json:"info"`
|
||||
Data json.RawMessage `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(exported.envelope, &envelope); err != nil {
|
||||
identity := exportPrefixIdentity(exported.envelope)
|
||||
if identity != "" && identity != id {
|
||||
return session, errSessionIdentity
|
||||
}
|
||||
if identity == id && errors.As(err, &syntax) && strings.Contains(syntax.Error(), "unexpected end") {
|
||||
return session, errTransientExport
|
||||
}
|
||||
return session, fmt.Errorf("native export returned invalid session evidence")
|
||||
}
|
||||
if envelope.Info.ID == "" && len(envelope.Data) > 0 {
|
||||
_ = json.Unmarshal(envelope.Data, &envelope)
|
||||
}
|
||||
if envelope.Info.ID != "" && envelope.Info.ID != id {
|
||||
return session, errSessionIdentity
|
||||
}
|
||||
return session, fmt.Errorf("native export returned unsupported or missing session evidence")
|
||||
}
|
||||
records, err := chatMessageRecords(messages)
|
||||
if err != nil {
|
||||
return session, err
|
||||
}
|
||||
session.Messages = exportedMessages(records)
|
||||
session.Truncated = true
|
||||
if len(session.Messages) == 0 {
|
||||
return session, ErrEmptySessionExport
|
||||
}
|
||||
return session, nil
|
||||
}
|
||||
|
||||
// exportPrefixIdentity reads only complete metadata preceding an interrupted
|
||||
// messages array. A known wrong session is permanent even when JSON is cut.
|
||||
func exportPrefixIdentity(data []byte) string {
|
||||
decoder := json.NewDecoder(bytes.NewReader(data))
|
||||
if token, err := decoder.Token(); err != nil || token != json.Delim('{') {
|
||||
return ""
|
||||
}
|
||||
for decoder.More() {
|
||||
token, err := decoder.Token()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
if token == "info" {
|
||||
var info struct {
|
||||
ID string `json:"id"`
|
||||
}
|
||||
if decoder.Decode(&info) == nil {
|
||||
return info.ID
|
||||
}
|
||||
return ""
|
||||
}
|
||||
var skip json.RawMessage
|
||||
if decoder.Decode(&skip) != nil {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// exportedMessages maps decoded native turns onto ExportedMessage, preserving
|
||||
// every role and each part's raw JSON for unknown types.
|
||||
func exportedMessages(records []nativeChatMessage) []ExportedMessage {
|
||||
@@ -209,10 +309,17 @@ func decodeExportPrefix(data []byte, wantID string) ([]nativeChatMessage, bool,
|
||||
}
|
||||
var messages []nativeChatMessage
|
||||
sawMessages := false
|
||||
sawIdentity := false
|
||||
partial := func() ([]nativeChatMessage, bool, error) {
|
||||
if !sawIdentity {
|
||||
return nil, false, fmt.Errorf("native export prefix has no verified session identity")
|
||||
}
|
||||
return messages, true, nil
|
||||
}
|
||||
for decoder.More() {
|
||||
keyToken, err := decoder.Token()
|
||||
if err != nil {
|
||||
return messages, true, nil
|
||||
return partial()
|
||||
}
|
||||
key, _ := keyToken.(string)
|
||||
switch key {
|
||||
@@ -221,16 +328,17 @@ func decodeExportPrefix(data []byte, wantID string) ([]nativeChatMessage, bool,
|
||||
ID string `json:"id"`
|
||||
}
|
||||
if err := decoder.Decode(&info); err != nil {
|
||||
return messages, true, nil
|
||||
return partial()
|
||||
}
|
||||
if info.ID != "" && info.ID != wantID {
|
||||
if info.ID != wantID {
|
||||
return nil, false, fmt.Errorf("native export session id mismatch")
|
||||
}
|
||||
sawIdentity = true
|
||||
case "messages":
|
||||
sawMessages = true
|
||||
open, err := decoder.Token()
|
||||
if err != nil {
|
||||
return messages, true, nil
|
||||
return partial()
|
||||
}
|
||||
if delim, ok := open.(json.Delim); !ok || delim != '[' {
|
||||
return nil, false, fmt.Errorf("native export returned invalid session evidence")
|
||||
@@ -238,25 +346,28 @@ func decodeExportPrefix(data []byte, wantID string) ([]nativeChatMessage, bool,
|
||||
for decoder.More() {
|
||||
var message nativeChatMessage
|
||||
if err := decoder.Decode(&message); err != nil {
|
||||
return messages, true, nil
|
||||
return partial()
|
||||
}
|
||||
messages = append(messages, message)
|
||||
}
|
||||
if _, err := decoder.Token(); err != nil {
|
||||
return messages, true, nil
|
||||
return partial()
|
||||
}
|
||||
default:
|
||||
var skip json.RawMessage
|
||||
if err := decoder.Decode(&skip); err != nil {
|
||||
return messages, true, nil
|
||||
return partial()
|
||||
}
|
||||
}
|
||||
}
|
||||
if !sawMessages {
|
||||
return nil, false, fmt.Errorf("native export returned no session messages")
|
||||
}
|
||||
if !sawIdentity {
|
||||
return nil, false, fmt.Errorf("native export prefix has no verified session identity")
|
||||
}
|
||||
if _, err := decoder.Token(); err != nil {
|
||||
return messages, true, nil
|
||||
return partial()
|
||||
}
|
||||
return messages, false, nil
|
||||
}
|
||||
|
||||
@@ -2,6 +2,9 @@ package opencode
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -73,7 +76,30 @@ func TestExportSessionDecodesRolesPartsAndUnknownTypes(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestExportSessionRetriesOneSettlingReadback(t *testing.T) {
|
||||
func TestNativeExportEnvelopeNormalizationRetainsExactIdentity(t *testing.T) {
|
||||
for _, data := range []string{
|
||||
`{"info":{"id":"ses_ok"},"messages":[]}`,
|
||||
`{"info":{"id":"ses_ok"},"messages":{"data":[]}}`,
|
||||
`{"data":{"info":{"id":"ses_ok"},"messages":[]}}`,
|
||||
} {
|
||||
messages, ok := validNativeExport([]byte(data), "ses_ok")
|
||||
if !ok || string(messages) != "[]" {
|
||||
t.Fatalf("envelope not normalized: %s %s %v", data, messages, ok)
|
||||
}
|
||||
}
|
||||
for _, data := range []string{
|
||||
`{"info":{"id":"ses_other"},"messages":[],"data":{"info":{"id":"ses_ok"},"messages":[]}}`,
|
||||
`{"info":{"id":"ses_ok"},"messages":null}`,
|
||||
`{"info":{"id":"ses_ok"},"messages":{}}`,
|
||||
`{"data":{"info":{"id":"ses_other"},"messages":[]}}`,
|
||||
} {
|
||||
if _, ok := validNativeExport([]byte(data), "ses_ok"); ok {
|
||||
t.Fatalf("invalid identity/envelope accepted: %s", data)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestExportSessionRetriesOneInvalidReadbackWithoutCausalClaim(t *testing.T) {
|
||||
id := "ses_settle"
|
||||
dir := t.TempDir()
|
||||
count := filepath.Join(dir, "count")
|
||||
@@ -83,7 +109,7 @@ func TestExportSessionRetriesOneSettlingReadback(t *testing.T) {
|
||||
"[ \"$1\" = " + id + " ] || exit 9\n" +
|
||||
"if [ ! -f " + count + " ]; then\n" +
|
||||
" echo 1 > " + count + "\n" +
|
||||
" echo '{\"info\":{\"id\":\"wrong\"}}'\n" +
|
||||
" echo '{\"info\":{\"id\":\"ses_settle\"},\"messages\":['\n" +
|
||||
" exit 0\n" +
|
||||
"fi\n" +
|
||||
"cat <<'EOF'\n" + valid + "\nEOF\n"
|
||||
@@ -181,3 +207,42 @@ func TestDecodeExportPrefixRejectsMismatchedID(t *testing.T) {
|
||||
t.Fatal("mismatched prefix session accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestExportRecoveryRetriesOnlyTransientOrEmptyReadback(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name, first string
|
||||
wantAttempts int
|
||||
empty bool
|
||||
wantError bool
|
||||
}{
|
||||
{"empty-then-ready", `echo '{"info":{"id":"ses_ok"},"messages":[]}'`, 2, false, false},
|
||||
{"temporary-then-ready", `echo 'database is locked' >&2; exit 1`, 2, false, false},
|
||||
{"wrong-session", `echo '{"info":{"id":"ses_wrong"},"messages":[]}'`, 1, false, true},
|
||||
{"wrong-session-incomplete", `echo '{"info":{"id":"ses_wrong"},"messages":['`, 1, false, true},
|
||||
{"unsupported-schema", `echo '{"info":{"id":"ses_ok"},"messages":{}}'`, 1, false, true},
|
||||
{"permanent-cli-error", `echo 'session not found' >&2; exit 1`, 1, false, true},
|
||||
{"always-empty", `echo '{"info":{"id":"ses_ok"},"messages":[]}'`, 3, true, true},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
count := filepath.Join(dir, "calls")
|
||||
script := "#!/bin/sh\nn=0; [ ! -f '" + count + "' ] || n=$(cat '" + count + "'); n=$((n+1)); echo $n > '" + count + "'\n"
|
||||
if tc.empty {
|
||||
script += tc.first + "\n"
|
||||
} else {
|
||||
script += "if [ $n = 1 ]; then " + tc.first + "; else echo '{\"info\":{\"id\":\"ses_ok\"},\"messages\":[{\"info\":{\"role\":\"assistant\"},\"parts\":[{\"type\":\"text\",\"text\":\"ready\"}]}]}'; fi\n"
|
||||
}
|
||||
session, err := ExportSession(context.Background(), writeFixture(t, script), dir, "v2", "ses_ok")
|
||||
if (err != nil) != tc.wantError {
|
||||
t.Fatalf("unexpected result %+v %v", session, err)
|
||||
}
|
||||
calls, _ := os.ReadFile(count)
|
||||
if strings.TrimSpace(string(calls)) != fmt.Sprint(tc.wantAttempts) || session.Attempts != tc.wantAttempts {
|
||||
t.Fatalf("attempts %s %+v", calls, session)
|
||||
}
|
||||
if tc.empty && !errors.Is(err, ErrEmptySessionExport) {
|
||||
t.Fatalf("not explicitly empty: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package pipeline
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
// RecordDeliverable appends server-observed evidence without replacing the
|
||||
// visit's existing inventory or its bounds diagnostics.
|
||||
func (s *Store) RecordDeliverable(ctx context.Context, id int64, sequence int, file protocol.StageArtifact) error {
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
var data string
|
||||
var evidence stageArtifactsEvidence
|
||||
err = tx.QueryRowContext(ctx, "SELECT artifacts_json FROM step_artifacts WHERE run_id=? AND sequence=?", id, sequence).Scan(&data)
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
return err
|
||||
}
|
||||
if data != "" {
|
||||
if err := json.Unmarshal([]byte(data), &evidence); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
evidence.Files = append(evidence.Files, file)
|
||||
body, err := json.Marshal(evidence)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = tx.ExecContext(ctx, "INSERT INTO step_artifacts(run_id,sequence,artifacts_json) VALUES(?,?,?) ON CONFLICT(run_id,sequence) DO UPDATE SET artifacts_json=excluded.artifacts_json", id, sequence, string(body))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package pipeline
|
||||
|
||||
import (
|
||||
"context"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/summarise"
|
||||
)
|
||||
|
||||
func TestOutputFormatPersistenceAndIdempotency(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s, err := Open(filepath.Join(t.TempDir(), "format.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer s.Close()
|
||||
s.Submit(ctx, "format", Template([]string{"one"}, true))
|
||||
items, _ := s.List(ctx)
|
||||
d, _ := s.Get(ctx, items[0].ID, 1)
|
||||
input := map[string]any{"prompt": "task"}
|
||||
create := func(format string) (int64, bool, error) {
|
||||
return s.CreateRunTitled(ctx, d, "same-key", "workspace", false, nil, input, RunOverrides{OutputFormat: format}, summarise.Title{})
|
||||
}
|
||||
id, created, err := create("html")
|
||||
if err != nil || !created {
|
||||
t.Fatalf("%d %v %v", id, created, err)
|
||||
}
|
||||
again, created, err := create("html")
|
||||
if err != nil || created || again != id {
|
||||
t.Fatalf("%d %v %v", again, created, err)
|
||||
}
|
||||
for _, format := range []string{"", "markdown", "HTML", "../../report.html"} {
|
||||
if _, _, err := create(format); err == nil {
|
||||
t.Fatalf("accepted changed/invalid format %q", format)
|
||||
}
|
||||
}
|
||||
got, err := s.GetRun(ctx, id)
|
||||
if err != nil || got.OutputFormat != "html" {
|
||||
t.Fatalf("%+v %v", got, err)
|
||||
}
|
||||
runs, err := s.ListRuns(ctx)
|
||||
if err != nil || len(runs) != 1 || runs[0].OutputFormat != "html" {
|
||||
t.Fatalf("%+v %v", runs, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOutputFormatMigrationPreservesHistory(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
path := filepath.Join(t.TempDir(), "legacy.db")
|
||||
s, err := Open(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s.Submit(ctx, "old", Template([]string{"one"}, true))
|
||||
items, _ := s.List(ctx)
|
||||
d, _ := s.Get(ctx, items[0].ID, 1)
|
||||
id, _, err := s.CreateRun(ctx, d, "old-key", "workspace", false, nil, map[string]any{"prompt": "old task"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.db.Exec("ALTER TABLE pipeline_runs DROP COLUMN output_format"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.db.Exec("DROP TABLE session_export_repairs"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s.Close()
|
||||
for range 2 {
|
||||
s, err = Open(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := s.GetRun(ctx, id)
|
||||
if err != nil || got.OutputFormat != "" || got.Input["prompt"] != "old task" {
|
||||
t.Fatalf("%+v %v", got, err)
|
||||
}
|
||||
if _, err := s.db.Exec("SELECT export_json FROM session_export_repairs LIMIT 1"); err != nil {
|
||||
t.Fatalf("repair schema not migrated: %v", err)
|
||||
}
|
||||
s.Close()
|
||||
}
|
||||
}
|
||||
@@ -21,12 +21,12 @@ const runStamps = `strftime('%Y-%m-%dT%H:%M:%SZ',r.created_at),COALESCE(strftime
|
||||
const runSelect = `SELECT r.id,r.pipeline_id,p.name,r.version,v.content_hash,r.status,r.outcome,r.current_stage,
|
||||
` + runStamps + `,r.diagnostic,
|
||||
CASE WHEN r.status='succeeded' THEN r.output_json ELSE COALESCE((SELECT output_json FROM run_steps WHERE run_id=r.id AND status='completed' ORDER BY sequence DESC LIMIT 1),'{}') END,
|
||||
(SELECT COUNT(*) FROM run_steps WHERE run_id=r.id AND status='completed'),r.workspace,r.title,r.model,r.stage_models,r.notify_mode,r.cancel_requested,r.cleanup_unconfirmed
|
||||
(SELECT COUNT(*) FROM run_steps WHERE run_id=r.id AND status='completed'),r.workspace,r.title,r.model,r.stage_models,r.notify_mode,r.cancel_requested,r.cleanup_unconfirmed,r.output_format
|
||||
` + runFrom + ` JOIN pipeline_versions v ON v.pipeline_id=r.pipeline_id AND v.version=r.version`
|
||||
|
||||
func scanRun(row interface{ Scan(...any) error }, r *protocol.RunSummary) error {
|
||||
var output, stageModels string
|
||||
if err := row.Scan(&r.ID, &r.PipelineID, &r.Name, &r.Version, &r.ContentHash, &r.Status, &r.Outcome, &r.CurrentStage, &r.CreatedAt, &r.FinishedAt, &r.Diagnostic, &output, &r.CompletedSteps, &r.Workspace, &r.Title, &r.Model, &stageModels, &r.NotifyMode, &r.CancelRequested, &r.CleanupUnconfirmed); err != nil {
|
||||
if err := row.Scan(&r.ID, &r.PipelineID, &r.Name, &r.Version, &r.ContentHash, &r.Status, &r.Outcome, &r.CurrentStage, &r.CreatedAt, &r.FinishedAt, &r.Diagnostic, &output, &r.CompletedSteps, &r.Workspace, &r.Title, &r.Model, &stageModels, &r.NotifyMode, &r.CancelRequested, &r.CleanupUnconfirmed, &r.OutputFormat); err != nil {
|
||||
return err
|
||||
}
|
||||
if stageModels != "" && stageModels != "{}" {
|
||||
@@ -186,8 +186,30 @@ strftime('%Y-%m-%dT%H:%M:%SZ',started_at),COALESCE(strftime('%Y-%m-%dT%H:%M:%SZ'
|
||||
return nil, err
|
||||
}
|
||||
for i := range r.Steps {
|
||||
repairs, err := tx.QueryContext(ctx, "SELECT export_json FROM session_export_repairs WHERE run_id=? AND sequence=? ORDER BY id", id, r.Steps[i].Sequence)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for repairs.Next() {
|
||||
var body string
|
||||
var evidence protocol.SessionExport
|
||||
if err := repairs.Scan(&body); err != nil {
|
||||
repairs.Close()
|
||||
return nil, err
|
||||
}
|
||||
if err := json.Unmarshal([]byte(body), &evidence); err != nil {
|
||||
repairs.Close()
|
||||
return nil, err
|
||||
}
|
||||
r.Steps[i].SessionExportRepairs = append(r.Steps[i].SessionExportRepairs, &evidence)
|
||||
}
|
||||
readErr := repairs.Err()
|
||||
repairs.Close()
|
||||
if readErr != nil {
|
||||
return nil, readErr
|
||||
}
|
||||
var data string
|
||||
err := tx.QueryRowContext(ctx, "SELECT evidence_json FROM step_control WHERE run_id=? AND sequence=?", id, r.Steps[i].Sequence).Scan(&data)
|
||||
err = tx.QueryRowContext(ctx, "SELECT evidence_json FROM step_control WHERE run_id=? AND sequence=?", id, r.Steps[i].Sequence).Scan(&data)
|
||||
if err == sql.ErrNoRows {
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -16,8 +16,9 @@ import (
|
||||
// stages by key (per-stage wins over whole-run). They are recorded so the run
|
||||
// stays reproducible without touching the pinned pipeline version.
|
||||
type RunOverrides struct {
|
||||
Model string
|
||||
StageModels map[string]string
|
||||
OutputFormat string
|
||||
Model string
|
||||
StageModels map[string]string
|
||||
// NotifyMode is auto, on or off for this run. Empty means auto.
|
||||
NotifyMode string
|
||||
}
|
||||
@@ -35,6 +36,9 @@ func (s *Store) CreateRun(ctx context.Context, d *protocol.PipelineDetail, key,
|
||||
// title. The overrides are compared in the request-key idempotency check, so a
|
||||
// replay with different overrides is refused as a different run.
|
||||
func (s *Store) CreateRunTitled(ctx context.Context, d *protocol.PipelineDetail, key, workspace string, tools bool, paths []string, input map[string]any, overrides RunOverrides, title summarise.Title, retryOf ...int64) (int64, bool, error) {
|
||||
if err := protocol.ValidateOutputFormat(overrides.OutputFormat); err != nil {
|
||||
return 0, false, err
|
||||
}
|
||||
var parent int64
|
||||
if len(retryOf) > 0 {
|
||||
parent = retryOf[0]
|
||||
@@ -63,15 +67,15 @@ func (s *Store) CreateRunTitled(ctx context.Context, d *protocol.PipelineDetail,
|
||||
defer s.mu.Unlock()
|
||||
var id, pid int64
|
||||
var version int
|
||||
var oldInput, oldWorkspace, oldPaths, oldModel, oldStageModels, oldNotify string
|
||||
var oldInput, oldWorkspace, oldPaths, oldModel, oldStageModels, oldNotify, oldFormat string
|
||||
var oldTools bool
|
||||
err = s.db.QueryRowContext(ctx, "SELECT id,pipeline_id,version,input_json,workspace,write_paths,allow_tools,model,stage_models,notify_mode FROM pipeline_runs WHERE request_key=?", key).Scan(&id, &pid, &version, &oldInput, &oldWorkspace, &oldPaths, &oldTools, &oldModel, &oldStageModels, &oldNotify)
|
||||
err = s.db.QueryRowContext(ctx, "SELECT id,pipeline_id,version,input_json,workspace,write_paths,allow_tools,model,stage_models,notify_mode,output_format FROM pipeline_runs WHERE request_key=?", key).Scan(&id, &pid, &version, &oldInput, &oldWorkspace, &oldPaths, &oldTools, &oldModel, &oldStageModels, &oldNotify, &oldFormat)
|
||||
if err == nil {
|
||||
var oldParent int64
|
||||
if err := s.db.QueryRowContext(ctx, "SELECT COALESCE((SELECT original_run_id FROM run_retries WHERE run_id=?),0)", id).Scan(&oldParent); err != nil {
|
||||
return 0, false, err
|
||||
}
|
||||
if pid != d.ID || version != d.Version || oldInput != string(encoded) || oldWorkspace != workspace || oldPaths != string(writes) || oldTools != tools || oldParent != parent || oldModel != overrides.Model || oldStageModels != string(encodedModels) || oldNotify != notifyMode(overrides.NotifyMode) {
|
||||
if pid != d.ID || version != d.Version || oldInput != string(encoded) || oldWorkspace != workspace || oldPaths != string(writes) || oldTools != tools || oldParent != parent || oldModel != overrides.Model || oldStageModels != string(encodedModels) || oldNotify != notifyMode(overrides.NotifyMode) || oldFormat != overrides.OutputFormat {
|
||||
return 0, false, fmt.Errorf("request key already used for a different run")
|
||||
}
|
||||
return id, false, nil
|
||||
@@ -84,7 +88,7 @@ func (s *Store) CreateRunTitled(ctx context.Context, d *protocol.PipelineDetail,
|
||||
return 0, false, err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
result, err := tx.ExecContext(ctx, "INSERT INTO pipeline_runs(pipeline_id,version,request_key,workspace,allow_tools,write_paths,input_json,title,title_source,title_model,model,stage_models,notify_mode) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)", d.ID, d.Version, key, workspace, tools, string(writes), string(encoded), title.Text, title.Source, title.Model, overrides.Model, string(encodedModels), notifyMode(overrides.NotifyMode))
|
||||
result, err := tx.ExecContext(ctx, "INSERT INTO pipeline_runs(pipeline_id,version,request_key,workspace,allow_tools,write_paths,input_json,title,title_source,title_model,model,stage_models,notify_mode,output_format) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)", d.ID, d.Version, key, workspace, tools, string(writes), string(encoded), title.Text, title.Source, title.Model, overrides.Model, string(encodedModels), notifyMode(overrides.NotifyMode), overrides.OutputFormat)
|
||||
if err != nil {
|
||||
return 0, false, err
|
||||
}
|
||||
|
||||
@@ -67,6 +67,12 @@ CREATE TABLE IF NOT EXISTS retry_checkpoints (
|
||||
return err
|
||||
}
|
||||
}
|
||||
if _, err := s.db.Exec(`CREATE TABLE IF NOT EXISTS session_export_repairs (id INTEGER PRIMARY KEY, run_id INTEGER NOT NULL, sequence INTEGER NOT NULL, export_json TEXT NOT NULL, FOREIGN KEY(run_id,sequence) REFERENCES run_steps(run_id,sequence) ON DELETE CASCADE); CREATE INDEX IF NOT EXISTS session_export_repairs_visit ON session_export_repairs(run_id,sequence,id);`); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.addColumnIfMissing("pipeline_runs", "output_format", "TEXT NOT NULL DEFAULT ''"); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.addColumnIfMissing("pipeline_runs", "notify_mode", "TEXT NOT NULL DEFAULT 'auto'"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package pipeline
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
// RepairSessionOwner requires actual recorded ownership metadata. Unlike the
|
||||
// legacy display lookup it never guesses a backend for a historical session.
|
||||
func (s *Store) RepairSessionOwner(ctx context.Context, id int64, sequence int) (SessionOwner, error) {
|
||||
var owner SessionOwner
|
||||
err := s.db.QueryRowContext(ctx, `SELECT s.session_id,o.backend,r.workspace,o.reference FROM run_steps s JOIN pipeline_runs r ON r.id=s.run_id JOIN run_session_owners o ON o.run_id=s.run_id AND o.sequence=s.sequence WHERE s.run_id=? AND s.sequence=? AND s.session_id<>'' AND s.agent_active=0`, id, sequence).Scan(&owner.ID, &owner.Backend, &owner.Directory, &owner.Reference)
|
||||
return owner, err
|
||||
}
|
||||
|
||||
// RecordSessionExportRepair appends a repair attempt; original step evidence is
|
||||
// never replaced. A failed/empty attempt is durable and remains inspectable.
|
||||
func (s *Store) RecordSessionExportRepair(ctx context.Context, id int64, sequence int, evidence *protocol.SessionExport) error {
|
||||
body, err := json.Marshal(evidence)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = s.db.ExecContext(ctx, "INSERT INTO session_export_repairs(run_id,sequence,export_json) VALUES(?,?,?)", id, sequence, string(body))
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package protocol
|
||||
|
||||
import "fmt"
|
||||
|
||||
// ValidateOutputFormat deliberately does not infer a contract from prompt text.
|
||||
// Empty preserves historical behavior. Report filenames are server-owned.
|
||||
func ValidateOutputFormat(format string) error {
|
||||
switch format {
|
||||
case "", "html", "markdown":
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("output_format must be html or markdown (or omitted)")
|
||||
}
|
||||
}
|
||||
@@ -153,6 +153,11 @@ type Message struct {
|
||||
// them; the same fields are echoed on the run record.
|
||||
Model string `json:"model,omitempty"`
|
||||
StageModels map[string]string `json:"stage_models,omitempty"`
|
||||
// OutputFormat is an optional per-run runtime report contract, never a
|
||||
// pipeline definition or model-stage prompt change.
|
||||
OutputFormat string `json:"output_format,omitempty"`
|
||||
// ExportSequence identifies the exact recorded visit for repair_session_export.
|
||||
ExportSequence int `json:"export_sequence,omitempty"`
|
||||
// NotifyMode is auto (definition), on (terminal success/failure and attention) or off
|
||||
// (silence this run). It is optional so older clients keep auto.
|
||||
NotifyMode string `json:"notify_mode,omitempty"`
|
||||
|
||||
+41
-32
@@ -3,18 +3,19 @@ package protocol
|
||||
import "strings"
|
||||
|
||||
const (
|
||||
RunPipeline = "run_pipeline"
|
||||
RunStarted = "run_started"
|
||||
ListRuns = "list_runs"
|
||||
Runs = "runs"
|
||||
GetRun = "get_run"
|
||||
Run = "run"
|
||||
CancelRun = "cancel_run"
|
||||
RetryRun = "retry_run"
|
||||
DecideRun = "decide_run"
|
||||
SetRunTitle = "set_run_title"
|
||||
GetRunEvidence = "get_run_evidence"
|
||||
RunEvidence = "run_evidence"
|
||||
RunPipeline = "run_pipeline"
|
||||
RunStarted = "run_started"
|
||||
ListRuns = "list_runs"
|
||||
Runs = "runs"
|
||||
GetRun = "get_run"
|
||||
Run = "run"
|
||||
CancelRun = "cancel_run"
|
||||
RetryRun = "retry_run"
|
||||
DecideRun = "decide_run"
|
||||
SetRunTitle = "set_run_title"
|
||||
GetRunEvidence = "get_run_evidence"
|
||||
RunEvidence = "run_evidence"
|
||||
RepairSessionExport = "repair_session_export"
|
||||
)
|
||||
|
||||
// Waiting statuses. A human stage pauses its run without a live goroutine, so
|
||||
@@ -107,7 +108,13 @@ type SessionEvidence struct {
|
||||
// and a sanitized diagnostic. It is observed server-side evidence, not a model
|
||||
// claim. A record written before the HTML-only change also carried markdown and
|
||||
// markdown_bytes; readers ignore those fields.
|
||||
// Status explicitly describes evidence completeness, independently from the
|
||||
// stage outcome. Directory and capture time preserve export provenance.
|
||||
type SessionExport struct {
|
||||
Directory string `json:"directory,omitempty"`
|
||||
RecordedAt string `json:"recorded_at,omitempty"`
|
||||
Attempts int `json:"attempts,omitempty"`
|
||||
Status string `json:"status,omitempty"`
|
||||
SessionID string `json:"session_id,omitempty"`
|
||||
Backend string `json:"backend,omitempty"`
|
||||
Source string `json:"source,omitempty"`
|
||||
@@ -145,8 +152,9 @@ type RunSummary struct {
|
||||
// stays reproducible: Model covers every model stage and StageModels overrides
|
||||
// individual stages by key. They are empty for a run that used the pinned
|
||||
// models and for historical rows.
|
||||
Model string `json:"model,omitempty"`
|
||||
StageModels map[string]string `json:"stage_models,omitempty"`
|
||||
Model string `json:"model,omitempty"`
|
||||
StageModels map[string]string `json:"stage_models,omitempty"`
|
||||
OutputFormat string `json:"output_format,omitempty"`
|
||||
}
|
||||
|
||||
// Label is the operator-facing name of a run: the generated Title when the
|
||||
@@ -160,24 +168,25 @@ func (r RunSummary) Label() string {
|
||||
}
|
||||
|
||||
type RunStep struct {
|
||||
Control *ControlEvidence `json:"control,omitempty"`
|
||||
FileChanges *StageFileChanges `json:"file_changes,omitempty"`
|
||||
Verdict *StageVerdict `json:"verdict,omitempty"`
|
||||
SessionExport *SessionExport `json:"session_export,omitempty"`
|
||||
Usage *StageUsage `json:"usage,omitempty"`
|
||||
Notifications []NotificationReceipt `json:"notifications,omitempty"`
|
||||
Artifacts []StageArtifact `json:"artifacts,omitempty"`
|
||||
Sequence int `json:"sequence"`
|
||||
Stage string `json:"stage"`
|
||||
Status string `json:"status"`
|
||||
Outcome string `json:"outcome"`
|
||||
Target string `json:"target"`
|
||||
Input map[string]any `json:"input"`
|
||||
Output map[string]any `json:"output"`
|
||||
Diagnostic string `json:"diagnostic"`
|
||||
SessionID string `json:"session_id"`
|
||||
AgentActive bool `json:"agent_active"`
|
||||
Transcript string `json:"transcript"`
|
||||
Control *ControlEvidence `json:"control,omitempty"`
|
||||
FileChanges *StageFileChanges `json:"file_changes,omitempty"`
|
||||
Verdict *StageVerdict `json:"verdict,omitempty"`
|
||||
SessionExport *SessionExport `json:"session_export,omitempty"`
|
||||
SessionExportRepairs []*SessionExport `json:"session_export_repairs,omitempty"`
|
||||
Usage *StageUsage `json:"usage,omitempty"`
|
||||
Notifications []NotificationReceipt `json:"notifications,omitempty"`
|
||||
Artifacts []StageArtifact `json:"artifacts,omitempty"`
|
||||
Sequence int `json:"sequence"`
|
||||
Stage string `json:"stage"`
|
||||
Status string `json:"status"`
|
||||
Outcome string `json:"outcome"`
|
||||
Target string `json:"target"`
|
||||
Input map[string]any `json:"input"`
|
||||
Output map[string]any `json:"output"`
|
||||
Diagnostic string `json:"diagnostic"`
|
||||
SessionID string `json:"session_id"`
|
||||
AgentActive bool `json:"agent_active"`
|
||||
Transcript string `json:"transcript"`
|
||||
// Provider and Model are the effective provider/model this visit actually ran
|
||||
// with, recorded for every model stage (including failures). They are empty
|
||||
// for non-model stages and historical steps.
|
||||
|
||||
@@ -0,0 +1,160 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
"github.com/yuin/goldmark"
|
||||
"github.com/yuin/goldmark/extension"
|
||||
)
|
||||
|
||||
const reportLimit = 2 << 20
|
||||
|
||||
// renderReport is deterministic and deliberately structural: no factual or
|
||||
// completion validation is implied. Raw HTML is disabled by Goldmark's default
|
||||
// renderer; the document has no scripts, stylesheets or remote dependencies.
|
||||
func renderReport(format string, output map[string]any) (string, []byte, error) {
|
||||
if err := protocol.ValidateOutputFormat(format); err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
var markdown string
|
||||
for _, key := range []string{"markdown", "report", "result", "summary", "message"} {
|
||||
if text, ok := output[key].(string); ok && strings.TrimSpace(text) != "" {
|
||||
markdown = text
|
||||
break
|
||||
}
|
||||
}
|
||||
if strings.TrimSpace(markdown) == "" {
|
||||
return "", nil, fmt.Errorf("requested report requires nonempty textual output (markdown, report, result, summary or message)")
|
||||
}
|
||||
if len(markdown) > reportLimit {
|
||||
return "", nil, fmt.Errorf("report source exceeds size limit")
|
||||
}
|
||||
if format == "markdown" {
|
||||
return "report.md", []byte(markdown), nil
|
||||
}
|
||||
var body bytes.Buffer
|
||||
if err := goldmark.New(goldmark.WithExtensions(extension.GFM)).Convert([]byte(markdown), &body); err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
// No external resources are fetched even if markdown includes image links.
|
||||
doc := "<!doctype html>\n<html lang=\"en\"><head><meta charset=\"utf-8\"><meta name=\"viewport\" content=\"width=device-width\"><meta http-equiv=\"Content-Security-Policy\" content=\"default-src 'none'; style-src 'unsafe-inline'; img-src data:; base-uri 'none'; form-action 'none'\"><title>Run report</title><style>body{max-width:70rem;margin:3rem auto;padding:0 1.5rem;font:18px/1.6 system-ui}pre{white-space:pre-wrap}img{max-width:100%}table{border-collapse:collapse}td,th{border:1px solid;padding:.4rem}</style></head><body><main>" + body.String() + "</main></body></html>\n"
|
||||
if len(doc) > reportLimit {
|
||||
return "", nil, fmt.Errorf("rendered report exceeds size limit")
|
||||
}
|
||||
return "report.html", []byte(doc), nil
|
||||
}
|
||||
|
||||
// finalizeDeliverable runs before the successful terminal store transition.
|
||||
// Fixed names and os.Root keep writes confined even if a stage left symlinks.
|
||||
// Existing files (including model-created reports) are never overwritten.
|
||||
func (r *runner) finalizeDeliverable(ctx context.Context, id int64, sequence int, output map[string]any) error {
|
||||
detail, err := r.store.GetRun(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if detail.OutputFormat == "" {
|
||||
return nil
|
||||
}
|
||||
if detail.Status != protocol.RunStatusQueued && detail.Status != protocol.RunStatusRunning {
|
||||
return nil // never create evidence for an already-settled/canceled run
|
||||
}
|
||||
if sequence <= 0 {
|
||||
return fmt.Errorf("requested report has no completed visit")
|
||||
}
|
||||
name, body, err := renderReport(detail.OutputFormat, output)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var visit *protocol.RunStep
|
||||
for i := range detail.Steps {
|
||||
if detail.Steps[i].Sequence == sequence {
|
||||
visit = &detail.Steps[i]
|
||||
break
|
||||
}
|
||||
}
|
||||
if visit == nil || visit.Status != "completed" {
|
||||
return fmt.Errorf("requested report has no completed terminal visit")
|
||||
}
|
||||
var inventoried int64
|
||||
for _, file := range visit.Artifacts {
|
||||
inventoried += file.SizeBytes
|
||||
}
|
||||
if len(visit.Artifacts) >= maxArtifactFiles || inventoried+int64(len(body)) > maxArtifactTotalBytes {
|
||||
return fmt.Errorf("requested report exceeds artifact inventory bounds")
|
||||
}
|
||||
root, err := os.OpenRoot(r.artifactsRoot)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer root.Close()
|
||||
run := strconv.FormatInt(id, 10)
|
||||
public := filepath.Join(run, "public")
|
||||
// Refuse symlinked directories, rather than using another visit's files.
|
||||
for _, dir := range []string{run, public} {
|
||||
info, err := root.Lstat(dir)
|
||||
if os.IsNotExist(err) {
|
||||
err = root.Mkdir(dir, 0755)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
continue
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !info.IsDir() || info.Mode()&os.ModeSymlink != 0 {
|
||||
return fmt.Errorf("report directory is not a normal directory")
|
||||
}
|
||||
}
|
||||
target := filepath.Join(public, name)
|
||||
file, err := root.OpenFile(target, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
return fmt.Errorf("create requested report: %w", err)
|
||||
}
|
||||
written, writeErr := file.Write(body)
|
||||
if writeErr == nil && written != len(body) {
|
||||
writeErr = io.ErrShortWrite
|
||||
}
|
||||
if writeErr == nil {
|
||||
writeErr = file.Sync()
|
||||
}
|
||||
closeErr := file.Close()
|
||||
if writeErr != nil {
|
||||
return writeErr
|
||||
}
|
||||
if closeErr != nil {
|
||||
return closeErr
|
||||
}
|
||||
info, err := root.Lstat(target)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !info.Mode().IsRegular() || info.Size() <= 0 || info.Size() > reportLimit {
|
||||
return fmt.Errorf("requested report is missing, empty, not a normal file or oversized")
|
||||
}
|
||||
check, err := root.Open(target)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
actual, err := io.ReadAll(io.LimitReader(check, reportLimit+1))
|
||||
check.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !bytes.Equal(actual, body) {
|
||||
return fmt.Errorf("requested report changed during verification")
|
||||
}
|
||||
contentType := "text/markdown; charset=utf-8"
|
||||
if detail.OutputFormat == "html" {
|
||||
contentType = "text/html; charset=utf-8"
|
||||
}
|
||||
return r.store.RecordDeliverable(ctx, id, sequence, protocol.StageArtifact{Name: "public/" + name, SizeBytes: info.Size(), ContentType: contentType, URL: artifactURL(id, sequence, "public/"+name)})
|
||||
}
|
||||
@@ -0,0 +1,190 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
"git.a-wan.net/alan/pipelines/internal/stage"
|
||||
"git.a-wan.net/alan/pipelines/internal/summarise"
|
||||
)
|
||||
|
||||
func TestRenderReportContract(t *testing.T) {
|
||||
for _, format := range []string{"html", "markdown"} {
|
||||
name, body, err := renderReport(format, map[string]any{"markdown": "# Report\n\n**Done**\n<script>alert(1)</script>"})
|
||||
if err != nil || name == "" || len(body) == 0 {
|
||||
t.Fatalf("%s: %s %v", format, body, err)
|
||||
}
|
||||
if format == "html" && (!strings.Contains(string(body), "<h1>Report</h1>") || strings.Contains(string(body), "<script>") || !strings.Contains(string(body), "Content-Security-Policy")) {
|
||||
t.Fatalf("unsafe or malformed report: %s", body)
|
||||
}
|
||||
_, again, _ := renderReport(format, map[string]any{"markdown": "# Report\n\n**Done**\n<script>alert(1)</script>"})
|
||||
if string(again) != string(body) {
|
||||
t.Fatal("not deterministic")
|
||||
}
|
||||
}
|
||||
for _, output := range []map[string]any{nil, {}, {"markdown": " \n"}, {"files": []string{"report.html"}}, {"markdown": strings.Repeat("x", reportLimit+1)}} {
|
||||
if _, _, err := renderReport("html", output); err == nil {
|
||||
t.Fatalf("accepted absent/oversized output: %.80v", output)
|
||||
}
|
||||
}
|
||||
if _, _, err := renderReport("../../escape", map[string]any{"summary": "hello"}); err == nil {
|
||||
t.Fatal("accepted invalid format")
|
||||
}
|
||||
}
|
||||
|
||||
func reportRunner(t *testing.T, format string) (*runner, int64) {
|
||||
t.Helper()
|
||||
ctx := context.Background()
|
||||
s, err := pipeline.Open(filepath.Join(t.TempDir(), "report.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { s.Close() })
|
||||
if _, err := s.Submit(ctx, "report", pipeline.Template([]string{"one"}, true)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
items, _ := s.List(ctx)
|
||||
d, _ := s.Get(ctx, items[0].ID, 1)
|
||||
id, _, err := s.CreateRunTitled(ctx, d, "report-key", "workspace", false, nil, map[string]any{"prompt": "task"}, pipeline.RunOverrides{OutputFormat: format}, summarise.Title{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.BeginStep(ctx, id, 1, "one", nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.FinishStep(ctx, id, protocol.RunStep{Sequence: 1, Stage: "one", Status: "completed", Outcome: "success", Output: map[string]any{"markdown": "# Result"}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return &runner{ctx: ctx, store: s, artifactsRoot: t.TempDir(), notifyModes: map[int64]string{id: "off"}}, id
|
||||
}
|
||||
|
||||
func TestDeliverableBeforeSuccess(t *testing.T) {
|
||||
for _, format := range []string{"html", "markdown", ""} {
|
||||
t.Run(format, func(t *testing.T) {
|
||||
r, id := reportRunner(t, format)
|
||||
r.finishRunState(id, "report", "workspace", "succeeded", "success", "", 1, 1, "one", stage.Values{"markdown": "# Result"})
|
||||
d, err := r.store.GetRun(context.Background(), id)
|
||||
if err != nil || d.Status != "succeeded" || d.OutputFormat != format {
|
||||
t.Fatalf("%+v %v", d, err)
|
||||
}
|
||||
if format != "" {
|
||||
if len(d.Steps[0].Artifacts) != 1 {
|
||||
t.Fatalf("missing report inventory: %+v", d.Steps)
|
||||
}
|
||||
file := d.Steps[0].Artifacts[0]
|
||||
if file.SizeBytes <= 0 || !strings.HasPrefix(file.Name, "public/report.") {
|
||||
t.Fatalf("%+v", file)
|
||||
}
|
||||
body, err := os.ReadFile(filepath.Join(r.artifactsRoot, "1", file.Name))
|
||||
if err != nil || len(body) == 0 {
|
||||
t.Fatalf("missing requested report: %v", err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeliverableMissingFailsRun(t *testing.T) {
|
||||
r, id := reportRunner(t, "html")
|
||||
r.finishRunState(id, "report", "workspace", "succeeded", "success", "", 1, 1, "one", nil)
|
||||
d, _ := r.store.GetRun(context.Background(), id)
|
||||
if d.Status != "failed" || !strings.Contains(d.Diagnostic, "deliverable contract") {
|
||||
t.Fatalf("missing report succeeded: %+v", d)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeliverableRejectsExistingFilesAndSymlinks(t *testing.T) {
|
||||
for _, kind := range []string{"file", "directory", "symlink", "public-symlink", "run-symlink"} {
|
||||
t.Run(kind, func(t *testing.T) {
|
||||
r, id := reportRunner(t, "html")
|
||||
public := filepath.Join(r.artifactsRoot, "1", "public")
|
||||
if err := os.MkdirAll(public, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
target := filepath.Join(public, "report.html")
|
||||
outside := filepath.Join(t.TempDir(), "keep")
|
||||
if err := os.WriteFile(outside, []byte("keep"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var err error
|
||||
switch kind {
|
||||
case "file":
|
||||
err = os.WriteFile(target, []byte("keep"), 0600)
|
||||
case "directory":
|
||||
err = os.Mkdir(target, 0755)
|
||||
case "symlink":
|
||||
err = os.Symlink(outside, target)
|
||||
case "public-symlink":
|
||||
os.Remove(public)
|
||||
err = os.Symlink(filepath.Dir(outside), public)
|
||||
case "run-symlink":
|
||||
os.Remove(public)
|
||||
os.Remove(filepath.Dir(public))
|
||||
err = os.Symlink(filepath.Dir(outside), filepath.Dir(public))
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := r.finalizeDeliverable(context.Background(), id, 1, map[string]any{"markdown": "new"}); err == nil {
|
||||
t.Fatal("unsafe/existing target accepted")
|
||||
}
|
||||
body, _ := os.ReadFile(outside)
|
||||
if string(body) != "keep" {
|
||||
t.Fatal("outside evidence overwritten")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestOutputFormatRetryInheritanceThroughRuntime(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
s, err := pipeline.Open(filepath.Join(t.TempDir(), "retry-format.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer s.Close()
|
||||
if _, err := s.Submit(ctx, "report", pipeline.Template([]string{"one"}, true)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
items, _ := s.List(ctx)
|
||||
r, err := newRunner(ctx, s)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer func() { cancel(); r.wg.Wait() }()
|
||||
r.artifactsRoot = t.TempDir()
|
||||
r.execute = func(context.Context, stage.Definition, stage.Values) (stage.Result, error) {
|
||||
return stage.Result{}, fmt.Errorf("fixture failure")
|
||||
}
|
||||
original, err := r.start(protocol.Message{PipelineID: items[0].ID, Version: 1, RequestKey: "original-format", Workspace: t.TempDir(), Input: map[string]any{"prompt": "task"}, OutputFormat: "html", NotifyMode: "off"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r.wg.Wait()
|
||||
r.execute = func(context.Context, stage.Definition, stage.Values) (stage.Result, error) {
|
||||
return stage.Result{Outcome: "success", Output: stage.Values{"markdown": "# Recovered report"}}, nil
|
||||
}
|
||||
retry, err := r.retry(protocol.Message{RunID: original.ID, RequestKey: "retry-format", RetryMode: "restart"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r.wg.Wait()
|
||||
retry, err = s.GetRun(ctx, retry.ID)
|
||||
if err != nil || retry.Status != "succeeded" || retry.OutputFormat != "html" {
|
||||
t.Fatalf("contract not inherited: %+v %v", retry, err)
|
||||
}
|
||||
if _, ok := artifactNamed(retry.Steps[0].Artifacts, "public/report.html"); !ok {
|
||||
t.Fatal("retry report missing")
|
||||
}
|
||||
replay, err := r.retry(protocol.Message{RunID: original.ID, RequestKey: "retry-format", RetryMode: "restart"})
|
||||
if err != nil || replay.ID != retry.ID {
|
||||
t.Fatalf("retry idempotency lost: %+v %v", replay, err)
|
||||
}
|
||||
}
|
||||
@@ -38,6 +38,9 @@ type runner struct {
|
||||
mu sync.Mutex
|
||||
active map[int64]activeRun
|
||||
execute func(context.Context, stage.Definition, stage.Values) (stage.Result, error)
|
||||
|
||||
exportRepairMu sync.Mutex
|
||||
|
||||
// title generates the operator-facing run title from the run prompt. When nil
|
||||
// the store derives a fallback from the prompt with no model call, so tests
|
||||
// and unconfigured servers stay deterministic.
|
||||
@@ -121,6 +124,9 @@ func newRunner(ctx context.Context, s *pipeline.Store) (*runner, error) {
|
||||
}
|
||||
|
||||
func (r *runner) start(request protocol.Message, retryOf ...int64) (*protocol.RunDetail, error) {
|
||||
if err := protocol.ValidateOutputFormat(request.OutputFormat); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if request.Version <= 0 || request.RequestKey == "" || len(request.RequestKey) > 128 {
|
||||
return nil, fmt.Errorf("an explicit version and request key are required")
|
||||
}
|
||||
@@ -283,7 +289,7 @@ func (r *runner) start(request protocol.Message, retryOf ...int64) (*protocol.Ru
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
id, created, err := r.store.CreateRunTitled(r.ctx, d, request.RequestKey, workspace, request.AllowTools, request.WritePaths, request.Input, pipeline.RunOverrides{Model: request.Model, StageModels: request.StageModels, NotifyMode: request.NotifyMode}, runTitle, retryOf...)
|
||||
id, created, err := r.store.CreateRunTitled(r.ctx, d, request.RequestKey, workspace, request.AllowTools, request.WritePaths, request.Input, pipeline.RunOverrides{Model: request.Model, StageModels: request.StageModels, NotifyMode: request.NotifyMode, OutputFormat: request.OutputFormat}, runTitle, retryOf...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -707,7 +713,7 @@ func (r *runner) retry(request protocol.Message) (*protocol.RunDetail, error) {
|
||||
if notifyMode == "" {
|
||||
notifyMode, _ = r.store.RunNotifyMode(r.ctx, original.ID)
|
||||
}
|
||||
return r.start(protocol.Message{PipelineID: original.PipelineID, Version: version, RequestKey: request.RequestKey, RetryMode: mode, RetrySource: original.ID, RetryStage: request.RetryStage, RetryVisit: request.RetryVisit, RetryInput: request.RetryInput, Workspace: original.Workspace, Input: original.Input, AllowTools: original.AllowTools, WritePaths: original.WritePaths, Model: original.Model, StageModels: original.StageModels, NotifyMode: notifyMode}, original.ID)
|
||||
return r.start(protocol.Message{PipelineID: original.PipelineID, Version: version, RequestKey: request.RequestKey, RetryMode: mode, RetrySource: original.ID, RetryStage: request.RetryStage, RetryVisit: request.RetryVisit, RetryInput: request.RetryInput, Workspace: original.Workspace, Input: original.Input, AllowTools: original.AllowTools, WritePaths: original.WritePaths, Model: original.Model, StageModels: original.StageModels, NotifyMode: notifyMode, OutputFormat: original.OutputFormat}, original.ID)
|
||||
}
|
||||
|
||||
func (r *runner) cancel(id int64) error {
|
||||
@@ -1307,6 +1313,11 @@ func (r *runner) finishRunState(id int64, name, workspace, status, outcome, diag
|
||||
r.mu.Lock()
|
||||
c, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
if status == "succeeded" {
|
||||
if err := r.finalizeDeliverable(c, id, sequence, input); err != nil {
|
||||
status, outcome, diagnostic = "failed", "error", "deliverable contract: "+err.Error()
|
||||
}
|
||||
}
|
||||
if err := r.store.FinishRun(c, id, status, outcome, diagnostic, input); err != nil {
|
||||
log.Printf("finish run %d: %v", id, err)
|
||||
}
|
||||
@@ -1965,7 +1976,8 @@ func (r *runner) runLoop(ctx context.Context, id int64, graph protocol.Graph, na
|
||||
// Any export error is recorded as a sanitized diagnostic and never returned to
|
||||
// the stage: the export is read-only, bounded and best-effort.
|
||||
func (r *runner) exportStageSession(ctx context.Context, owner pipeline.SessionOwner, artifactsDir string, runID int64, sequence int, key string, modelConfig stage.ModelConfig) *protocol.SessionExport {
|
||||
evidence := &protocol.SessionExport{SessionID: owner.ID, Backend: owner.Backend}
|
||||
evidence := &protocol.SessionExport{SessionID: owner.ID, Backend: owner.Backend, Directory: owner.Directory, RecordedAt: time.Now().UTC().Format(time.RFC3339)}
|
||||
defer classifySessionExport(evidence)
|
||||
if owner.Backend != "v1" && owner.Backend != "v2" {
|
||||
evidence.Source = "unavailable"
|
||||
evidence.Diagnostic = "session export unavailable: harness has no native export path"
|
||||
@@ -1990,17 +2002,20 @@ func (r *runner) exportStageSession(ctx context.Context, owner pipeline.SessionO
|
||||
Provider: modelConfig.Provider,
|
||||
Model: modelConfig.Model,
|
||||
})
|
||||
evidence.Attempts = result.Attempts
|
||||
if err != nil {
|
||||
evidence.Source = "native_export"
|
||||
evidence.Diagnostic = stage.RedactEvidence(err.Error())
|
||||
return evidence
|
||||
}
|
||||
evidence.Source = result.Source
|
||||
evidence.Status = result.Status
|
||||
evidence.HTML = result.HTML
|
||||
evidence.HTMLBytes = result.HTMLBytes
|
||||
evidence.Messages = result.Messages
|
||||
evidence.Truncated = result.Truncated
|
||||
evidence.Diagnostic = result.Diagnostic
|
||||
verifySessionExportFile(evidence, artifactsDir)
|
||||
return evidence
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
"git.a-wan.net/alan/pipelines/internal/stage"
|
||||
"os"
|
||||
)
|
||||
|
||||
func verifySessionExportFile(evidence *protocol.SessionExport, directory string) {
|
||||
if evidence.HTML == "" || evidence.HTMLBytes <= 0 {
|
||||
return
|
||||
}
|
||||
valid := false
|
||||
if evidence.HTML == stage.SessionHTMLFile {
|
||||
if root, err := os.OpenRoot(directory); err == nil {
|
||||
defer root.Close()
|
||||
info, err := root.Lstat(evidence.HTML)
|
||||
valid = err == nil && info.Mode().IsRegular() && info.Size() > 0 && info.Size() <= stage.SessionExportFileLimit && info.Size() == evidence.HTMLBytes
|
||||
}
|
||||
}
|
||||
if !valid {
|
||||
evidence.HTML = ""
|
||||
evidence.HTMLBytes = 0
|
||||
evidence.Diagnostic = "session readback returned, but exported file failed normal/nonempty/bounded verification"
|
||||
}
|
||||
}
|
||||
|
||||
// classifySessionExport describes evidence completeness, not stage success.
|
||||
// Empty exports prove only that a session ID was recorded, not that its turns
|
||||
// were captured. Keep that distinction even for historical records at read time.
|
||||
func classifySessionExport(evidence *protocol.SessionExport) {
|
||||
if evidence.Status == "empty" {
|
||||
return
|
||||
}
|
||||
switch {
|
||||
case evidence.Source == "unavailable":
|
||||
evidence.Status = "unavailable"
|
||||
case evidence.Messages == 0 && evidence.HTML == "" && evidence.Diagnostic != "":
|
||||
evidence.Status = "failed"
|
||||
case evidence.Messages == 0:
|
||||
evidence.Status = "empty"
|
||||
if evidence.Diagnostic == "" {
|
||||
evidence.Diagnostic = "session recorded but export contains no messages; full session evidence was not captured"
|
||||
}
|
||||
case evidence.Truncated || evidence.Diagnostic != "" || evidence.HTML == "" || evidence.HTMLBytes <= 0:
|
||||
evidence.Status = "partial"
|
||||
default:
|
||||
evidence.Status = "complete"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
"git.a-wan.net/alan/pipelines/internal/stage"
|
||||
)
|
||||
|
||||
// repairSessionExport is runtime-owned and read-only to OpenCode. It never
|
||||
// resumes a session, runs a stage, changes a definition, or overwrites evidence.
|
||||
func (r *runner) repairSessionExport(ctx context.Context, id int64, sequence int) (*protocol.RunDetail, error) {
|
||||
r.exportRepairMu.Lock()
|
||||
defer r.exportRepairMu.Unlock()
|
||||
if id <= 0 || sequence <= 0 {
|
||||
return nil, fmt.Errorf("positive run ID and export_sequence are required")
|
||||
}
|
||||
r.mu.Lock()
|
||||
_, executing := r.active[id]
|
||||
r.mu.Unlock()
|
||||
if executing {
|
||||
return nil, fmt.Errorf("run execution cleanup is still active; repair deferred")
|
||||
}
|
||||
d, err := r.store.GetRun(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if d.Status == "running" || d.Status == "queued" || d.Status == "waiting" {
|
||||
return nil, fmt.Errorf("session export repair requires a settled run")
|
||||
}
|
||||
var step *protocol.RunStep
|
||||
for i := range d.Steps {
|
||||
if d.Steps[i].Sequence == sequence {
|
||||
step = &d.Steps[i]
|
||||
break
|
||||
}
|
||||
}
|
||||
if step == nil || step.AgentActive {
|
||||
return nil, fmt.Errorf("no settled recorded session for this visit")
|
||||
}
|
||||
if len(step.SessionExportRepairs) >= 20 {
|
||||
return nil, fmt.Errorf("repair attempt limit reached for this visit")
|
||||
}
|
||||
owner, err := r.store.RepairSessionOwner(ctx, id, sequence)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("exact original session ownership metadata unavailable: %w", err)
|
||||
}
|
||||
active, err := r.store.ActiveSessionOwners(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, current := range active {
|
||||
if current.ID == owner.ID {
|
||||
return nil, fmt.Errorf("original session is still active; export repair deferred")
|
||||
}
|
||||
}
|
||||
evidence := &protocol.SessionExport{SessionID: owner.ID, Backend: owner.Backend, Directory: owner.Directory, RecordedAt: time.Now().UTC().Format(time.RFC3339), Source: "native_export"}
|
||||
// Capture and retain failures too. The diagnostic is evidence, not a new
|
||||
// run outcome, and cannot change the original stage/run success status.
|
||||
recordFailure := func(message string) (*protocol.RunDetail, error) {
|
||||
recordCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 5*time.Second)
|
||||
defer cancel()
|
||||
evidence.Status = "failed"
|
||||
evidence.Diagnostic = stage.RedactEvidence(message)
|
||||
if err := r.store.RecordSessionExportRepair(recordCtx, id, sequence, evidence); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return r.store.GetRun(recordCtx, id)
|
||||
}
|
||||
if owner.Backend != "v1" && owner.Backend != "v2" {
|
||||
return recordFailure("native export unavailable for original harness")
|
||||
}
|
||||
if len(step.Artifacts) >= maxArtifactFiles {
|
||||
return recordFailure("artifact inventory full; repair not attempted")
|
||||
}
|
||||
root, err := os.OpenRoot(r.artifactsRoot)
|
||||
if err != nil {
|
||||
return recordFailure(err.Error())
|
||||
}
|
||||
defer root.Close()
|
||||
run := strconv.FormatInt(id, 10)
|
||||
public := filepath.Join(run, "public")
|
||||
for _, dir := range []string{run, public} {
|
||||
info, err := root.Lstat(dir)
|
||||
if os.IsNotExist(err) {
|
||||
if err := root.Mkdir(dir, 0700); err != nil {
|
||||
return recordFailure(err.Error())
|
||||
}
|
||||
continue
|
||||
}
|
||||
if err != nil {
|
||||
return recordFailure(err.Error())
|
||||
}
|
||||
if !info.IsDir() || info.Mode()&os.ModeSymlink != 0 {
|
||||
return recordFailure("repair directory is not a normal directory")
|
||||
}
|
||||
}
|
||||
var nonce [16]byte
|
||||
if _, err := rand.Read(nonce[:]); err != nil {
|
||||
return recordFailure(err.Error())
|
||||
}
|
||||
name := fmt.Sprintf("session-repair-%d-%s.html", sequence, hex.EncodeToString(nonce[:]))
|
||||
// Stage.ExportSession is a runtime rendering helper, not a model stage.
|
||||
// No prompts/configs/definitions are involved. Export uses original owner.
|
||||
exportCtx, cancel := context.WithTimeout(ctx, stage.SessionExportTimeout)
|
||||
defer cancel()
|
||||
export := r.exportSession
|
||||
if export == nil {
|
||||
export = stage.ExportSession
|
||||
}
|
||||
result, err := export(exportCtx, stage.SessionExportRequest{SessionID: owner.ID, Backend: owner.Backend, Directory: owner.Directory, ArtifactsDir: filepath.Join(r.artifactsRoot, public), FileName: name, RunID: id, Sequence: sequence, Stage: step.Stage, Provider: step.Provider, Model: step.Model})
|
||||
persistCtx, persistCancel := context.WithTimeout(context.WithoutCancel(ctx), 5*time.Second)
|
||||
defer persistCancel()
|
||||
evidence.Attempts = result.Attempts
|
||||
if err != nil {
|
||||
return recordFailure(err.Error())
|
||||
}
|
||||
evidence.Status = result.Status
|
||||
evidence.Messages = result.Messages
|
||||
evidence.Truncated = result.Truncated
|
||||
evidence.Diagnostic = result.Diagnostic
|
||||
if result.HTML != "" {
|
||||
if result.HTML != name {
|
||||
return recordFailure("repair exporter returned an unexpected filename")
|
||||
}
|
||||
info, err := root.Lstat(filepath.Join(public, name))
|
||||
if err != nil {
|
||||
return recordFailure(err.Error())
|
||||
}
|
||||
if !info.Mode().IsRegular() || info.Size() <= 0 || info.Size() > stage.SessionExportFileLimit || result.Messages <= 0 {
|
||||
return recordFailure("repair export missing, empty, non-normal or oversized")
|
||||
}
|
||||
var used int64
|
||||
for _, file := range step.Artifacts {
|
||||
used += file.SizeBytes
|
||||
}
|
||||
if used+info.Size() > maxArtifactTotalBytes {
|
||||
return recordFailure("repaired export exceeds artifact inventory byte bounds")
|
||||
}
|
||||
evidence.HTML = "public/" + name
|
||||
evidence.HTMLBytes = info.Size()
|
||||
if err := r.store.RecordDeliverable(persistCtx, id, sequence, protocol.StageArtifact{Name: evidence.HTML, SizeBytes: info.Size(), ContentType: "text/html; charset=utf-8", URL: artifactURL(id, sequence, evidence.HTML)}); err != nil {
|
||||
return recordFailure(err.Error())
|
||||
}
|
||||
}
|
||||
classifySessionExport(evidence)
|
||||
if err := r.store.RecordSessionExportRepair(persistCtx, id, sequence, evidence); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return r.store.GetRun(persistCtx, id)
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/cli"
|
||||
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
||||
"git.a-wan.net/alan/pipelines/internal/stage"
|
||||
)
|
||||
|
||||
func TestRuntimeSessionExportRepairPreservesOriginalAndUsesRecordedOwner(t *testing.T) {
|
||||
h, s, id := sessionExportRunner(t, "v2", func(stage.SessionExportRequest) (stage.SessionExportResult, error) {
|
||||
return stage.SessionExportResult{}, errors.New("export read failed")
|
||||
})
|
||||
// The synthetic executor does not own a real agent to settle. Model real
|
||||
// runtime cleanup explicitly before exercising the settled-only repair gate.
|
||||
if err := s.ConfirmRecoveredSession(context.Background(), pipeline.SessionOwner{RunID: id, ID: "ses_run"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
original, err := s.GetRun(context.Background(), id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
before, _ := json.Marshal(original.Steps[0].SessionExport)
|
||||
if original.Steps[0].SessionExport.Status != "failed" {
|
||||
t.Fatalf("failure not durable: %+v", original.Steps[0].SessionExport)
|
||||
}
|
||||
owner, err := s.RepairSessionOwner(context.Background(), id, 1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if owner.ID != "ses_run" || owner.Backend != "v2" || owner.Directory != original.Workspace {
|
||||
t.Fatalf("owner changed: %+v", owner)
|
||||
}
|
||||
public := filepath.Join(h.runner.artifactsRoot, "1", "public")
|
||||
if err := os.MkdirAll(public, 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
old := filepath.Join(public, "session.html")
|
||||
if err := os.WriteFile(old, []byte("historical evidence"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// First repair also fails: it must remain durable without losing original.
|
||||
failed, err := h.runner.repairSessionExport(context.Background(), id, 1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(failed.Steps[0].SessionExportRepairs) != 1 || failed.Steps[0].SessionExportRepairs[0].Status != "failed" {
|
||||
t.Fatalf("repair failure disappeared: %+v", failed.Steps)
|
||||
}
|
||||
// Restore readback availability; this is a fake native export CLI, not a
|
||||
// model stage, no generation/resumption/definition mutation is performed.
|
||||
sessionExportCLI(t)
|
||||
h.runner.exportSession = nil
|
||||
repaired, err := h.runner.repairSessionExport(context.Background(), id, 1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if repaired.Status != original.Status || len(repaired.Steps[0].SessionExportRepairs) != 2 {
|
||||
t.Fatalf("repair changed run outcome: %+v", repaired)
|
||||
}
|
||||
latest := repaired.Steps[0].SessionExportRepairs[1]
|
||||
if latest.Status != "complete" || latest.Messages != 2 || latest.SessionID != owner.ID || latest.Backend != owner.Backend || !strings.HasPrefix(latest.HTML, "public/session-repair-1-") {
|
||||
t.Fatalf("repair not recorded: %+v", latest)
|
||||
}
|
||||
after, _ := json.Marshal(repaired.Steps[0].SessionExport)
|
||||
if string(before) != string(after) {
|
||||
t.Fatal("original evidence overwritten")
|
||||
}
|
||||
body, _ := os.ReadFile(old)
|
||||
if string(body) != "historical evidence" {
|
||||
t.Fatal("historical export overwritten")
|
||||
}
|
||||
body, err = os.ReadFile(filepath.Join(h.runner.artifactsRoot, "1", latest.HTML))
|
||||
if err != nil || len(body) == 0 {
|
||||
t.Fatalf("repair file missing: %v", err)
|
||||
}
|
||||
if _, ok := artifactNamed(repaired.Steps[0].Artifacts, latest.HTML); !ok {
|
||||
t.Fatal("repaired export not discoverable")
|
||||
}
|
||||
// A second successful attempt is a new version, never an overwrite.
|
||||
again, err := h.runner.repairSessionExport(context.Background(), id, 1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if again.Steps[0].SessionExportRepairs[2].HTML == latest.HTML {
|
||||
t.Fatal("repair reused filename")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRuntimeRepairRejectsUnrecordedAndActiveVisits(t *testing.T) {
|
||||
h, _, id := sessionExportRunner(t, "v2", func(stage.SessionExportRequest) (stage.SessionExportResult, error) {
|
||||
return stage.SessionExportResult{}, errors.New("unavailable")
|
||||
})
|
||||
for _, sequence := range []int{0, 1, 2} {
|
||||
if _, err := h.runner.repairSessionExport(context.Background(), id, sequence); err == nil {
|
||||
t.Fatalf("invalid visit %d accepted", sequence)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCLIRepairSessionExportUsesRuntimeProtocol(t *testing.T) {
|
||||
t.Setenv("PIPELINES_HARNESS_CONFIG", filepath.Join(t.TempDir(), "harnesses.json"))
|
||||
h, s, id := sessionExportRunner(t, "v2", func(stage.SessionExportRequest) (stage.SessionExportResult, error) {
|
||||
return stage.SessionExportResult{}, errors.New("fixture readback unavailable")
|
||||
})
|
||||
if err := s.ConfirmRecoveredSession(context.Background(), pipeline.SessionOwner{RunID: id, ID: "ses_run"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
sessionExportCLI(t)
|
||||
h.runner.exportSession = nil
|
||||
server := httptest.NewServer(h)
|
||||
defer server.Close()
|
||||
var out bytes.Buffer
|
||||
app := cli.App{Out: &out, Err: &out, In: strings.NewReader("")}
|
||||
err := app.Run(context.Background(), []string{"--url", "ws" + strings.TrimPrefix(server.URL, "http") + "/connect", "repair-session-export", "1", "1", "--json"})
|
||||
if err != nil {
|
||||
t.Fatalf("repair command failed: %v %s", err, out.String())
|
||||
}
|
||||
var evidence struct {
|
||||
Status string `json:"status"`
|
||||
SessionID string `json:"session_id"`
|
||||
HTML string `json:"html"`
|
||||
}
|
||||
if err := json.Unmarshal(out.Bytes(), &evidence); err != nil || evidence.Status != "complete" || evidence.SessionID != "ses_run" || !strings.HasPrefix(evidence.HTML, "public/session-repair-1-") {
|
||||
t.Fatalf("wrong repair reply: %+v %v %s", evidence, err, out.String())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSessionExportCompleteness(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
evidence protocol.SessionExport
|
||||
want string
|
||||
}{
|
||||
{protocol.SessionExport{Source: "native_export", HTML: "session.html", HTMLBytes: 100}, "empty"},
|
||||
{protocol.SessionExport{Source: "native_export", Diagnostic: "export failed"}, "failed"},
|
||||
{protocol.SessionExport{Source: "native_export", Messages: 2, HTML: "session.html", HTMLBytes: 100, Truncated: true}, "partial"},
|
||||
{protocol.SessionExport{Source: "native_export", Messages: 2, HTML: "session.html", HTMLBytes: 100}, "complete"},
|
||||
{protocol.SessionExport{Source: "unavailable"}, "unavailable"},
|
||||
} {
|
||||
classifySessionExport(&tc.evidence)
|
||||
if tc.evidence.Status != tc.want {
|
||||
t.Fatalf("%+v want %s", tc.evidence, tc.want)
|
||||
}
|
||||
if tc.want == "empty" && tc.evidence.Diagnostic == "" {
|
||||
t.Fatal("empty export has no warning")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRuntimeSessionExportFileVerificationDoesNotClaimMissingEvidence(t *testing.T) {
|
||||
for _, kind := range []string{"missing", "empty", "symlink", "normal"} {
|
||||
t.Run(kind, func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "session.html")
|
||||
switch kind {
|
||||
case "empty":
|
||||
if err := os.WriteFile(path, nil, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
case "normal":
|
||||
if err := os.WriteFile(path, []byte("html"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
case "symlink":
|
||||
if err := os.Symlink(filepath.Join(t.TempDir(), "outside"), path); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
evidence := &protocol.SessionExport{Source: "native_export", Messages: 2, HTML: "session.html", HTMLBytes: 4}
|
||||
verifySessionExportFile(evidence, dir)
|
||||
classifySessionExport(evidence)
|
||||
if (evidence.Status == "complete") != (kind == "normal") {
|
||||
t.Fatalf("%s: %+v", kind, evidence)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -276,9 +276,12 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin
|
||||
response.Error = err.Error()
|
||||
}
|
||||
}
|
||||
} else if runs != nil && (request.Type == protocol.RunPipeline || request.Type == protocol.RetryRun || request.Type == protocol.ListRuns || request.Type == protocol.GetRun || request.Type == protocol.CancelRun || request.Type == protocol.DecideRun || request.Type == protocol.SetRunTitle || request.Type == protocol.GetRunEvidence) {
|
||||
} else if runs != nil && (request.Type == protocol.RunPipeline || request.Type == protocol.RetryRun || request.Type == protocol.ListRuns || request.Type == protocol.GetRun || request.Type == protocol.CancelRun || request.Type == protocol.DecideRun || request.Type == protocol.SetRunTitle || request.Type == protocol.GetRunEvidence || request.Type == protocol.RepairSessionExport) {
|
||||
var err error
|
||||
switch request.Type {
|
||||
case protocol.RepairSessionExport:
|
||||
response.Type = protocol.Run
|
||||
response.RunDetail, err = runs.repairSessionExport(ctx, request.RunID, request.ExportSequence)
|
||||
case protocol.GetRunEvidence:
|
||||
response.Type = protocol.RunEvidence
|
||||
response.Evidence, err = runs.evidence(ctx, request.RunID)
|
||||
|
||||
@@ -3,6 +3,7 @@ package stage
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"html"
|
||||
"os"
|
||||
@@ -39,6 +40,8 @@ const (
|
||||
// rendered files carry. Directory is the session's workspace (the directory the
|
||||
// export CLI runs in); ArtifactsDir is the step's artifacts directory.
|
||||
type SessionExportRequest struct {
|
||||
// FileName is server-owned; repair attempts use unique versioned names.
|
||||
FileName string
|
||||
SessionID string
|
||||
Backend string
|
||||
Directory string
|
||||
@@ -55,6 +58,8 @@ type SessionExportRequest struct {
|
||||
// source or the rendered file was truncated and a sanitized diagnostic (for
|
||||
// example a skipped overwrite).
|
||||
type SessionExportResult struct {
|
||||
Status string
|
||||
Attempts int
|
||||
HTML string
|
||||
HTMLBytes int64
|
||||
Messages int
|
||||
@@ -83,32 +88,50 @@ func ExportSession(ctx context.Context, request SessionExportRequest) (SessionEx
|
||||
if request.ArtifactsDir == "" {
|
||||
return SessionExportResult{}, fmt.Errorf("session export: no artifacts directory")
|
||||
}
|
||||
name := request.FileName
|
||||
if name == "" {
|
||||
name = SessionHTMLFile
|
||||
}
|
||||
if filepath.Base(name) != name || !strings.HasSuffix(name, ".html") || (name != SessionHTMLFile && !strings.HasPrefix(name, "session-repair-")) {
|
||||
return SessionExportResult{}, fmt.Errorf("session export: invalid server filename")
|
||||
}
|
||||
session, err := opencode.ExportSession(ctx, opencode.Executable(), request.Directory, request.Backend, request.SessionID)
|
||||
if err != nil {
|
||||
return SessionExportResult{}, fmt.Errorf("session export: %s", RedactEvidence(err.Error()))
|
||||
if errors.Is(err, opencode.ErrEmptySessionExport) {
|
||||
return SessionExportResult{Source: sessionExportSource, Status: "empty", Attempts: session.Attempts, Truncated: session.Truncated, Diagnostic: "session recorded but export contains no messages after bounded retries; full session evidence was not captured"}, nil
|
||||
}
|
||||
return SessionExportResult{Source: sessionExportSource, Status: "failed", Attempts: session.Attempts}, fmt.Errorf("session export: %s", RedactEvidence(err.Error()))
|
||||
}
|
||||
htmlDoc, htmlTruncated := renderSessionHTML(session, request)
|
||||
result := SessionExportResult{
|
||||
HTML: SessionHTMLFile,
|
||||
HTML: name,
|
||||
Attempts: session.Attempts,
|
||||
Messages: len(session.Messages),
|
||||
Source: sessionExportSource,
|
||||
Truncated: session.Truncated || htmlTruncated,
|
||||
}
|
||||
written, diagnostic, err := writeSessionFile(request.ArtifactsDir, SessionHTMLFile, []byte(htmlDoc))
|
||||
if len(session.Messages) == 0 {
|
||||
result.Diagnostic = "session recorded but export contains no messages; full session evidence was not captured"
|
||||
}
|
||||
written, diagnostic, err := writeSessionFile(request.ArtifactsDir, name, []byte(htmlDoc))
|
||||
if err != nil {
|
||||
return SessionExportResult{}, fmt.Errorf("session export: %s", RedactEvidence(err.Error()))
|
||||
}
|
||||
if written {
|
||||
result.HTMLBytes = int64(len(htmlDoc))
|
||||
}
|
||||
result.Diagnostic = diagnostic
|
||||
if diagnostic != "" {
|
||||
if result.Diagnostic != "" {
|
||||
result.Diagnostic += "; "
|
||||
}
|
||||
result.Diagnostic += diagnostic
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// writeSessionFile stages the rendered file to a temporary file, then renames it
|
||||
// into place, so a failure before the rename leaves no partial session.html
|
||||
// behind. An existing target is never overwritten: it is skipped and the reason
|
||||
// is returned as a diagnostic.
|
||||
// writeSessionFile stages and syncs the rendered file, then links its final
|
||||
// name atomically. Unlike rename, link cannot overwrite a target created after
|
||||
// the existence check. Failed writes leave no partial final export behind.
|
||||
func writeSessionFile(dir, name string, data []byte) (bool, string, error) {
|
||||
target := filepath.Join(dir, name)
|
||||
if _, err := os.Lstat(target); err == nil {
|
||||
@@ -125,14 +148,23 @@ func writeSessionFile(dir, name string, data []byte) (bool, string, error) {
|
||||
_ = os.Remove(tmp.Name())
|
||||
return false, "", err
|
||||
}
|
||||
if err := tmp.Sync(); err != nil {
|
||||
tmp.Close()
|
||||
_ = os.Remove(tmp.Name())
|
||||
return false, "", err
|
||||
}
|
||||
if err := tmp.Close(); err != nil {
|
||||
_ = os.Remove(tmp.Name())
|
||||
return false, "", err
|
||||
}
|
||||
if err := os.Rename(tmp.Name(), target); err != nil {
|
||||
if err := os.Link(tmp.Name(), target); err != nil {
|
||||
_ = os.Remove(tmp.Name())
|
||||
if os.IsExist(err) {
|
||||
return false, "existing " + name + " was not overwritten", nil
|
||||
}
|
||||
return false, "", err
|
||||
}
|
||||
_ = os.Remove(tmp.Name())
|
||||
return true, "", nil
|
||||
}
|
||||
|
||||
@@ -180,7 +212,7 @@ func renderSessionHTML(session opencode.ExportedSession, request SessionExportRe
|
||||
|
||||
var body bytes.Buffer
|
||||
if len(session.Messages) == 0 {
|
||||
body.WriteString("<p><em>No messages were recorded for this session.</em></p>\n")
|
||||
body.WriteString("<p role=\"alert\"><strong>EMPTY EXPORT</strong>: Session recorded, but this export contains no messages. Full session evidence was not captured; this does not prove the session had no messages.</p>\n")
|
||||
}
|
||||
for i, message := range session.Messages {
|
||||
renderHTMLMessage(&body, i+1, message)
|
||||
|
||||
@@ -2,9 +2,11 @@ package stage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/opencode"
|
||||
@@ -84,6 +86,21 @@ func TestExportSessionWritesTheHTMLFileWithRealContent(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestExportSessionEmptyIsWarningNotFullEvidence(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
sessionCLI(t, "ses_ok", `{"info":{"id":"ses_ok"},"messages":[]}`)
|
||||
result, err := ExportSession(context.Background(), sessionRequest(dir))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result.Messages != 0 || result.Status != "empty" || result.Attempts != 3 || result.HTML != "" || !strings.Contains(result.Diagnostic, "no messages") {
|
||||
t.Fatalf("empty export unqualified: %+v", result)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(dir, SessionHTMLFile)); !os.IsNotExist(err) {
|
||||
t.Fatalf("empty success-looking HTML must not be written: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExportSessionRenderedTruncationBanner(t *testing.T) {
|
||||
// A source under the read cap but whose rendering exceeds the file cap.
|
||||
huge := strings.Repeat("line of text\n", SessionExportFileLimit/7)
|
||||
@@ -126,11 +143,44 @@ func TestExportSessionSourceTruncationIsLabelledNotWhole(t *testing.T) {
|
||||
func TestExportSessionEmptySessionIsHonest(t *testing.T) {
|
||||
session := opencode.ExportedSession{ID: "ses_ok", Backend: "v2"}
|
||||
htmlDoc, _ := renderSessionHTML(session, sessionRequest(t.TempDir()))
|
||||
if !strings.Contains(htmlDoc, "<dt>messages</dt><dd>0</dd>") || !strings.Contains(htmlDoc, "No messages were recorded") {
|
||||
if !strings.Contains(htmlDoc, "<dt>messages</dt><dd>0</dd>") || !strings.Contains(htmlDoc, "EMPTY EXPORT") || !strings.Contains(htmlDoc, "does not prove the session had no messages") {
|
||||
t.Fatalf("empty session must be honest, not fabricated:\n%s", htmlDoc)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionExportConcurrentWritersNeverOverwriteEvidence(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
var wg sync.WaitGroup
|
||||
winners := make(chan string, 16)
|
||||
for i := range 16 {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
body := fmt.Sprintf("export attempt %d", i)
|
||||
written, _, err := writeSessionFile(dir, "session.html", []byte(body))
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
if written {
|
||||
winners <- body
|
||||
}
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
close(winners)
|
||||
var expected string
|
||||
count := 0
|
||||
for body := range winners {
|
||||
expected = body
|
||||
count++
|
||||
}
|
||||
actual, err := os.ReadFile(filepath.Join(dir, "session.html"))
|
||||
if err != nil || count != 1 || string(actual) != expected {
|
||||
t.Fatalf("concurrent overwrite: winners=%d actual=%s expected=%s err=%v", count, actual, expected, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExportSessionWritesNoMarkdownAndLeavesALegacyOneAlone(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
sessionCLI(t, "ses_ok", sessionBody())
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
)
|
||||
|
||||
type RunOptions struct {
|
||||
OutputFormat string
|
||||
Pipeline, Prompt, Title, Workspace string
|
||||
Yes, AllowTools bool
|
||||
WritePaths []string
|
||||
|
||||
@@ -147,7 +147,7 @@ func (m *Model) startSelectedRun() tea.Cmd {
|
||||
m.runCollapsed = nil
|
||||
m.runDetail = nil
|
||||
m.runTop = 0
|
||||
request := protocol.Message{Type: protocol.RunPipeline, PipelineID: m.detail.ID, Version: m.detail.Version, RequestKey: fmt.Sprintf("tui-%d", time.Now().UnixNano()), Title: m.cliOptions.Title, Input: initialRunInput(m.runTask), AllowTools: m.runHasMutationTools(), WritePaths: m.runPaths, Workspace: m.runWorkspace, Model: m.cliOptions.Model, StageModels: m.cliOptions.StageModels, NotifyMode: m.cliOptions.NotifyMode}
|
||||
request := protocol.Message{Type: protocol.RunPipeline, PipelineID: m.detail.ID, Version: m.detail.Version, RequestKey: fmt.Sprintf("tui-%d", time.Now().UnixNano()), Title: m.cliOptions.Title, Input: initialRunInput(m.runTask), AllowTools: m.runHasMutationTools(), WritePaths: m.runPaths, Workspace: m.runWorkspace, Model: m.cliOptions.Model, StageModels: m.cliOptions.StageModels, NotifyMode: m.cliOptions.NotifyMode, OutputFormat: m.cliOptions.OutputFormat}
|
||||
updated, cmd := m.runRequest(request)
|
||||
*m = updated.(Model)
|
||||
return cmd
|
||||
|
||||
@@ -26,6 +26,10 @@ The following supersedes older C8/B18 implementation details below:
|
||||
|
||||
Unit/HTTP-fixture regressions cover these changes. No live workflow or deployed
|
||||
service was used; new rendered-state captures are **not yet available**.
|
||||
Audit additions (2026-10-20): **partial** — explicit run Report format selector
|
||||
(`output_format`, HTML/Markdown/no requirement), and session export
|
||||
empty/failed/partial explanations. Unit-tested; browser captures missing under
|
||||
the audit's no-live-runs constraint. Existing captured states below are unchanged.
|
||||
|
||||
## Transport and connection
|
||||
| # | Feature (Android counterpart) | Protocol | Status |
|
||||
|
||||
@@ -185,7 +185,7 @@ export class PipelinesRepository {
|
||||
* Starts a fresh run of one pipeline version. `notifyMode` is auto, on or off;
|
||||
* auto omits the field so the pipeline definition applies.
|
||||
*/
|
||||
async runPipeline({ pipelineId, version, prompt, workspace, notifyMode = "auto", title = "", requestKey = newChatOperationId() } = {}) {
|
||||
async runPipeline({ pipelineId, version, prompt, workspace, notifyMode = "auto", title = "", outputFormat = "", requestKey = newChatOperationId() } = {}) {
|
||||
const request = {
|
||||
type: Protocol.RUN_PIPELINE,
|
||||
pipeline_id: Number(pipelineId),
|
||||
@@ -195,6 +195,7 @@ export class PipelinesRepository {
|
||||
input: { prompt: String(prompt ?? "") },
|
||||
};
|
||||
if (String(title ?? "").trim() !== "") request.title = String(title).trim();
|
||||
if (outputFormat) request.output_format = outputFormat;
|
||||
const mode = String(notifyMode ?? "auto").trim().toLowerCase();
|
||||
if (mode === "on" || mode === "off") request.notify_mode = mode;
|
||||
const response = await this.#client.call(request);
|
||||
|
||||
@@ -388,6 +388,7 @@ async function openRunDialog(pipeline) {
|
||||
prompt: "",
|
||||
workspace: String(state.workspace ?? "").startsWith("/") ? state.workspace : "",
|
||||
notifyMode: "auto",
|
||||
outputFormat: "",
|
||||
directories: [],
|
||||
loading: true,
|
||||
sending: false,
|
||||
@@ -445,6 +446,7 @@ async function startRun() {
|
||||
prompt,
|
||||
workspace: start.workspace,
|
||||
notifyMode: start.notifyMode,
|
||||
outputFormat: start.outputFormat,
|
||||
title: "",
|
||||
});
|
||||
}
|
||||
@@ -545,6 +547,18 @@ function renderRunStartDialog() {
|
||||
notifyField.append(notify);
|
||||
panel.append(notifyField);
|
||||
|
||||
const outputField = h("label", { class: "run-start-field", for: "runOutputFormat" });
|
||||
outputField.append(h("span", { class: "run-start-label", text: "Report format" }));
|
||||
const outputFormat = h("select", { id: "runOutputFormat", onchange: (event) => { start.outputFormat = event.target.value; } });
|
||||
for (const [value, label] of [["", "No report requirement"], ["html", "HTML report"], ["markdown", "Markdown report"]]) {
|
||||
const option = h("option", { value, text: label });
|
||||
option.selected = start.outputFormat === value;
|
||||
outputFormat.append(option);
|
||||
}
|
||||
outputField.append(outputFormat);
|
||||
outputField.append(h("span", { class: "run-start-helper muted", text: "Requires a nonempty server-written report before success. Structure only; facts are not verified." }));
|
||||
panel.append(outputField);
|
||||
|
||||
if (start.loading) panel.append(h("div", { class: "run-start-helper muted", text: "Loading directories…" }));
|
||||
if (start.error) panel.append(h("div", { class: "run-start-error", role: "alert", text: start.error }));
|
||||
|
||||
|
||||
@@ -1294,8 +1294,21 @@ function artifactAction(kind, label, disabled, onclick) {
|
||||
function renderSessionExport(step) {
|
||||
const file = sessionExportArtifact(step);
|
||||
const wrap = h("div", { class: "session-export" });
|
||||
for (const repair of step?.session_export_repairs || []) {
|
||||
const row = h("div", { class: "session-export-repair" });
|
||||
row.append(h("span", { text: `Repair: ${repair.status || "partial"} · ${repair.messages || 0} messages. ${repair.diagnostic || ""}` }));
|
||||
const repaired = sessionExportArtifact({ ...step, session_export: repair });
|
||||
const repairedHref = repaired && resolvedArtifactUrl(repaired, state.url);
|
||||
if (repairedHref) row.append(h("a", { href: repairedHref, target: "_blank", rel: "noreferrer", text: "Open repaired export" }));
|
||||
wrap.append(row);
|
||||
}
|
||||
const evidence = step?.session_export;
|
||||
const incomplete = evidence && (evidence.status && evidence.status !== "complete" || !evidence.messages || evidence.truncated || evidence.diagnostic);
|
||||
if (incomplete) {
|
||||
wrap.append(h("span", { class: "muted", text: `Session recorded, but export ${evidence.status || (!evidence.messages ? "empty" : "partial")}. Full session evidence was not captured.${evidence.diagnostic ? ` ${evidence.diagnostic}` : ""}` }));
|
||||
}
|
||||
if (!file) {
|
||||
wrap.append(h("span", { class: "muted", text: "No session was exported for this visit." }));
|
||||
if (!incomplete) wrap.append(h("span", { class: "muted", text: step?.session_id ? "Session recorded, but export failed or was not captured." : "No session was exported for this visit." }));
|
||||
return wrap;
|
||||
}
|
||||
const href = resolvedArtifactUrl(file, state.url);
|
||||
|
||||
@@ -60,6 +60,17 @@ function lastFrame(ws) {
|
||||
return frame;
|
||||
}
|
||||
|
||||
test("runPipeline sends an explicit output_format and leaves legacy launches unchanged", async () => {
|
||||
const { repo, ws } = connected();
|
||||
ws.reply = (frame) => ({ id: frame.id, type: "run_started", run_detail: { id: 1 } });
|
||||
for (const outputFormat of ["", "html", "markdown"]) {
|
||||
await repo.runPipeline({ pipelineId: 2, version: 1, prompt: "report", workspace: "/scratch", outputFormat });
|
||||
const frame = lastFrame(ws);
|
||||
assert.equal(frame.output_format, outputFormat || undefined);
|
||||
assert.deepEqual(frame.input, { prompt: "report" });
|
||||
}
|
||||
});
|
||||
|
||||
test("listTemplates sends exactly {type:'list_templates'} and returns the catalogue", async () => {
|
||||
const { repo, ws } = connected();
|
||||
ws.reply = (frame) => ({
|
||||
|
||||
Reference in New Issue
Block a user