Skip to content

fix(openclaw): preserve usage and capture metadata (follow-up to #2327) - #3336

Closed
vincentkoc wants to merge 39 commits into
harbor-framework:mainfrom
vincentkoc:fix/openclaw-accounting-capture-20260921
Closed

vincentkoc wants to merge 39 commits into
harbor-framework:mainfrom
vincentkoc:fix/openclaw-accounting-capture-20260921

Conversation

@vincentkoc

@vincentkoc vincentkoc commented Sep 21, 2026 •

Copy link
Copy Markdown

Dependency

Depends on #2327 (Onur's OpenClaw ATIF integration), pinned at 9468613145492509d92c360eb6d93d27c3dd9392. This is a separate, non-blocking follow-up; it does not change Onur's branch.

Review only the follow-up: one commit / two files.

The upstream diff currently includes the predecessor commits. Keep this draft until #2327 merges, then rebase onto main and drop those commits. This does not need to hold up #2327 or the exporter release.

Changes

  • Preserve missing token totals as None and explicitly reported zero totals as 0 in the OpenClaw adapter.
  • Include observed cache-write tokens in fallback prompt totals, matching the exporter; retain zero-valued step usage and cache-write metadata.
  • Expose receipt capture status, diagnostic codes, trajectory/session identifiers, artifact references and the trajectory digest through existing AgentContext.metadata["openclaw_capture"].
  • Expose fallback/unavailable capture reasons even when no command envelope is available. Preserve unrelated context metadata and leave valid receipt-bound trajectory bytes untouched.

Capture completeness is not a claim about usage coverage or successful execution. Diagnostic messages remain in the receipt; only codes are copied into trial metadata.

The change is limited to the adapter and regression tests. No shared Harbor schema, generic per-model aggregation, source retention, resource collection or execution-timing changes.

Validation

At follow-up commit be142ea691fd80792d181e0383fb44a19c19971b:

  • OpenClaw adapter unit tests: 125 passed.
  • Repository-wide ruff check --fix .: passed.
  • Repository-wide ruff format .: 1,424 files unchanged.
  • Repository-wide ty check: passed.
  • git diff --check: passed; working tree clean.
  • Self-review covered receipt binding, metadata preservation, fallback accounting and the exporter mapping contract.

Commands ran with uv run --no-sync against the existing task environment. No full unit suite, live-provider benchmark or Windows execution was performed locally. Hosted CI and maintainer review are separate checks.

AI assistance

OpenClaw/Codex implemented this follow-up, wrote the regression tests and drafted this description under the author's direction. Human review is pending; this remains a draft.

Dependency recheck (2026-09-21)

PR #2327 independently advanced to 8649c58f3c110f806723bec9545361f639ab2a89 with capture-metadata support after this draft was based. Its metadata overlaps this draft; reconcile that overlap when restacking after the predecessor lands. This draft remains at be142ea691fd80792d181e0383fb44a19c19971b; neither branch was rewritten in this continuation. The nullable/zero and cache-write accounting fixes remain a separate contribution.

The opt-in source retention and controller phase timing follow-up is isolated in #3338.

osolmaz and others added 30 commits August 19, 2026 18:36
Merge main into the trajectory capture work and adopt the current Harbor
agent contracts.

Conflict resolution in openclaw.py keeps main's newer code and adds the
trajectory attributes the capture path needs:

- keep the capability declaration import and add the validation import
- keep the options-based super() call and read the export flag from
  self.options.session_to_trajectory
- drop main's session JSONL branch, which the public export replaces

Changes on top of the merge:

- implement convert_trajectory() for preview conversion without changing
  run results, sharing the export loader with populate_context_post_run
- emit ATIF v1.8
- describe session_to_trajectory as the rich export request
- cover the conversion hook and the agreement between the rolled-up
  totals and compute_model_usage()

Co-Authored-By: DeepSeek V4.1 Flash · Auto · Novita <[email protected]>
Generated-By: pi 0.85.1 (https://pi.dev)
Address two review findings on the trajectory converter.

Cost: a message that reports tokens without a cost total no longer counts as
free. `_metrics` returns whether pricing was present, the session omits
`total_cost_usd` when any token-bearing message is unpriced, and every ancestor
inherits that unknown status through the capture graph. A partial numeric total
can no longer ship.

Media: content mapping keeps image and audio parts that name a media type and a
location, such as a file path or URL, as ATIF content parts. Inline base64 media
cannot be represented in ATIF, so it becomes a truthful placeholder and the
capture is marked partial with the `media-content-not-preserved` warning code.
This covers user messages, assistant messages, and tool results.

Co-Authored-By: DeepSeek V4.1 Flash · Auto · Novita <[email protected]>
Generated-By: pi 0.85.1 (https://pi.dev)
The first pass only recognised `image` and `audio` parts, so any other part type
still disappeared without a trace while the capture reported `complete`. Content
mapping now tracks the part types that have their own converters, either a
tool call or reasoning, and records every other non-empty part as omitted. It
also accepts the `image_url` and `input_image` shapes that carry a declared media
type and location.

Covered by `test_unknown_content_part_marks_capture_partial` and
`test_provider_image_url_shape_is_preserved`.

Co-Authored-By: DeepSeek V4.1 Flash · Auto · Novita <[email protected]>
Generated-By: pi 0.85.1 (https://pi.dev)
Address three more review findings on the converter.

Local media: a path inside the agent environment is deleted with that
environment, so it no longer counts as preserved. The converter copies such a
file next to the trajectory under `media/`, references it with a relative path
that resolves against the trajectory directory, and keeps the copy private. A
file that is missing, a symlink, or larger than the 32 MiB cap becomes a
placeholder with the `media-content-not-preserved` warning instead.

Zero cost: an explicit `cost.total: 0` establishes that pricing is known. Step
metrics now carry a zero cost, and the session reports `total_cost_usd: 0.0`
when every token-bearing message is priced, so a free run no longer reports an
unknown cost.

Identity: `trajectory_id` now derives from the exported `session_id` together
with the session key. Isolated trials reuse the key `agent:main:main`, so the
key alone gave unrelated runs the same identifier.

Co-Authored-By: DeepSeek V4.1 Flash · Auto · Novita <[email protected]>
Generated-By: pi 0.85.1 (https://pi.dev)
Address two more review findings.

Repeated media: the file limit now counts retained files, not references to
them. A payload is content-addressed before the limit is applied, so a file that
is already retained stays referenceable after the limit is reached instead of
turning into a placeholder.

Exposure: local media is copied only when the resolved path stays inside the
session's own export bundle. A crafted part that names a credential,
configuration, or unrelated task file is dropped with the
`media-content-not-preserved` warning instead, so it cannot reach the
synchronized logs.

Co-Authored-By: DeepSeek V4.1 Flash · Auto · Novita <[email protected]>
Generated-By: pi 0.85.1 (https://pi.dev)
A media location can be absolute, a URL, or relative to the export bundle that
contains the event. Resolving a relative location from the process directory
made the containment check reject a valid bundle-local file, so the part became
an omission placeholder.

A relative location now resolves from the bundle root before the containment,
symlink, size, and regular-file checks run.

Co-Authored-By: DeepSeek V4.1 Flash · Auto · Novita <[email protected]>
Generated-By: pi 0.85.1 (https://pi.dev)
Run public session discovery, export, and conversion in one remote helper. Keep all private state and exports under one trial directory with bounded cleanup, and fail the run when cleanup fails after a capture fallback.

Preserve partial trees at capture limits and add standalone helper, cancellation, and combined-failure coverage.

Co-Authored-By: GPT-6 Astra <[email protected]>
Generated-By: pi 0.85.1 (https://pi.dev)
osolmaz and others added 9 commits September 18, 2026 14:02
Download backends recreate remote files with `mkdir` and `Path.write_bytes`, so
an export written as 0600 files inside a 0700 directory arrives on the host as
0644 files inside a 0755 directory under umask 022. `_load_export` narrowed only
`trajectory.json` and `receipt.json`, which left the package's image and audio
files readable by other host users.

Harden every package-owned artifact after the transfer: the two JSON files and
the whole `media/` tree. Directories become 0700 and files 0600, which are the
modes `openclaw-atif` writes. Hardening reuses `validate_output_path`, so a path
that resolves outside the logs directory, or any link, is left untouched; the
shared log directory keeps its mode.

Hardening runs before receipt validation and also when a failed capture leaves a
media bundle behind, so private media is never left readable.

Co-Authored-By: DeepSeek V4.1 Flash · Auto · Novita <[email protected]>
Generated-By: pi 0.85.1 (https://pi.dev)
The converter package moved to the @openclaw npm scope. Install @openclaw/[email protected] instead of [email protected].

The scoped release is not on npm yet. The unit tests use a fake environment, so they pass. A live run needs the published release.

Co-Authored-By: DeepSeek V4.1 Flash · Novita <[email protected]>
Generated-By: pi 0.86.1 (https://pi.dev)
@vercel

vercel Bot commented Sep 21, 2026

Copy link
Copy Markdown

@vincentkoc is attempting to deploy a commit to the Harbor Framework Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions

Copy link
Copy Markdown
Contributor

Enjoy a better diff viewing experience by clicking one of these URLs:

@vincentkoc

Copy link
Copy Markdown
Author

Consolidated into #2327 at aaa2fcb, including the published ATIF 0.1.3 pin. Closing this redundant follow-up; its branch and recovery evidence are retained. The canonical PR remains open and unmerged.

@vincentkoc vincentkoc closed this Sep 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants