openhuman icon indicating copy to clipboard operation
openhuman copied to clipboard

ai_list_artifacts: thread_id filter + ArtifactMeta.thread_id field

Open oxoxDev opened this issue 6 months ago • 5 comments

Summary

`ChatFilesPanel` (#3024, PR #3026) lists artifacts the agent has generated in the current chat thread, but the listing today is sourced from the in-memory redux `chatRuntime.artifactsByThread` map (rehydrated through the persisted ready-only filter). On a fresh machine, after a redux-persist purge, or when a thread is opened from another device, the panel shows zero files even if the on-disk `/artifacts/` ledger contains the original `.pptx`s. The producer events (`ArtifactReady` / `ArtifactFailed`) carry the originating `thread_id`, but the persisted `ArtifactMeta` (`src/openhuman/artifacts/types.rs`) does not — so the on-disk artifact has no concept of which thread produced it, and the panel can't repopulate from `ai_list_artifacts`.

Spotted by @graycyrus on PR #3026: https://github.com/tinyhumansai/openhuman/pull/3026#pullrequestreview-XXX (Note 1, "Cold-load limitation").

Problem

Today: per-chat artifact panel only surfaces what the live redux slice happens to remember. Fresh redux + on-disk ledger = empty panel. Users hit this when:

  • They open openhuman on a new device, sync threads, but artifacts don't follow.
  • A persist-version bump (`persistedReducer`'s migration path) clears `artifactsByThread`.
  • A long-running session triggers a redux-persist eviction.
  • They re-open a thread that produced artifacts in a previous boot of the desktop app, on a host where persist storage was cleared (e.g. `pnpm dev:app` with a fresh OPENHUMAN_WORKSPACE).

The on-disk `artifacts//meta.json` is authoritative — it's just not addressable by thread.

Solution (optional)

Two coordinated changes:

  1. Add `thread_id: Option` to `ArtifactMeta` in `src/openhuman/artifacts/types.rs`. Populate at `create_artifact` time from the same `APPROVAL_CHAT_CONTEXT` task-local that drives `finalize_artifact`'s `thread_id` extraction (`store.rs::current_chat_context`). `None` for CLI / cron / sub-agent paths — same convention as the existing `thread_id` on `DomainEvent::ArtifactReady`.

  2. Add a `thread_id: Option` filter to `ai_list_artifacts` (`src/openhuman/artifacts/schemas.rs`). When provided, the handler walks the on-disk ledger and returns only entries whose `meta.thread_id` matches. `ChatFilesPanel` calls this on mount keyed on the current thread; results merge into `artifactsByThread[threadId]` so a fresh redux slice gets repopulated from disk.

Migration: existing `meta.json` files won't have `thread_id`. `serde(default)` on the field handles legacy reads — they show as `None` and behave like sub-agent-produced artifacts (not surfaced in any thread's panel). A follow-up could backfill from the `audit_log` if needed, but isn't blocking.

Acceptance criteria

  • [ ] `ArtifactMeta.thread_id` populated at create-time — `finalize_artifact` doesn't need to fill it because `create_artifact` writes the snapshot first.
  • [ ] `ai_list_artifacts` accepts `thread_id` filter — match-on-equality; absent filter returns all artifacts (current behaviour).
  • [ ] `ChatFilesPanel` calls `ai_list_artifacts` on mount keyed on the current thread; results merge into the redux slice.
  • [ ] Legacy `meta.json` files load with `thread_id = None` (via `#[serde(default)]`) and are filtered out of thread-scoped listings.
  • [ ] Tests: `store.rs` round-trip with thread_id; `schemas.rs` handler with + without filter; vitest spec for `ChatFilesPanel` repopulating from a cold redux slice.
  • [ ] Diff coverage ≥ 80%.

Related

  • Parent epic: #1535
  • UI surface: #3024 (PR #3026)
  • Producer events already carry thread_id: `DomainEvent::ArtifactReady` / `ArtifactFailed` (`src/core/event_bus/events.rs`).

oxoxDev avatar Jun 02 '26 16:06 oxoxDev

https://github.com/tinyhumansai/openhuman/issues/new/choose

josemkondo-lang avatar Jun 02 '26 21:06 josemkondo-lang

Tracking note: PR #3274 ships the backend half of this issue:

  • [x] `ArtifactMeta.thread_id` persisted at create-time (`store.rs::create_artifact`).
  • [x] `ai_list_artifacts` accepts a `thread_id` filter (`ops.rs` + `schemas.rs`).
  • [x] Legacy `meta.json` files load with `thread_id = None` and are excluded from thread-scoped listings.
  • [x] Unit tests (types / ops / store round-trip + filter behaviour, 6 new).

PR #3274 has been re-titled `Contributes to #3226` rather than `Closes` (per @graycyrus's review on that PR) so this issue stays open as the home for the remaining:

  • [ ] `ChatFilesPanel` calls `ai_list_artifacts({thread_id})` on mount and merges results into `chatRuntime.artifactsByThread[threadId]`, with a Vitest spec for the cold-redux repopulation path.

Will open the frontend follow-up against this issue.

Non-blocking scale follow-up (flagged by @graycyrus on PR #3274)

`store::list_artifacts` currently reads + JSON-parses every `meta.json` in the workspace before applying the `thread_id` filter, since the sort already requires the full set. Not a regression — the unfiltered list always paid this cost — but once `thread_id` is persisted and `ChatFilesPanel` opens per-thread, a workspace with thousands of artifacts will scan all of them on every panel mount.

Worth a tracked follow-up to consider:

  • A lightweight thread→artifact index (e.g. a sidecar `/artifacts/by-thread/<thread_id>/<artifact_id>` symlink tree), or
  • Streaming the directory walk and filtering on `meta.thread_id` before the full parse — only pay the deserialise cost for matching entries.

Not asked for in this PR; flagging here so it's on the record once artifact counts grow.

YOMXXX avatar Jun 03 '26 13:06 YOMXXX

Status / parity check — backend done, frontend not consuming it (~55%)

The backend half landed in #3274 — thread_id is now persisted on artifacts and ai_list_artifacts supports the thread_id filter, so the capability this issue asks for exists server-side.

What's remaining is the frontend: ChatFilesPanel does not call ai_list_artifacts({ thread_id }) on mount, so from the user's side the empty-files-panel symptom (e.g. after a purge / on a fresh thread load) is still present. The data is queryable but the UI isn't querying it per-thread.

Closing this needs the panel wired to the new thread-scoped call. Keeping open until the frontend consumes the filter.

oxoxDev avatar Jun 05 '26 12:06 oxoxDev

Hi, I can take the remaining frontend follow-up if that fits with current ownership. I would keep the scope to ChatFilesPanel calling ai_list_artifacts({ thread_id }) on mount and merging the result into chatRuntime.artifactsByThread[threadId], plus a focused Vitest cold-redux repopulation spec. I would avoid touching the backend half from #3274.

samrusani avatar Jun 23 '26 19:06 samrusani

@samrusani all yours — assigned. Scope is exactly right:

  • ChatFilesPanel calls ai_list_artifacts({ thread_id }) on mount, merging into chatRuntime.artifactsByThread[threadId].
  • Focused Vitest spec for the cold-redux repopulation path.
  • No need to touch the backend half from #3274 — thread_id persistence + the filter are already landed there.

One thing to be aware of: @graycyrus flagged a non-blocking scale follow-up on #3274 — store::list_artifacts parses every meta.json before filtering, so a per-thread panel mount will scan the whole workspace. Not in scope for this PR, but worth keeping the mount call cheap / debounced so it doesn't fire redundantly. Ping me if you need anything on the Rust side.

oxoxDev avatar Jun 25 '26 06:06 oxoxDev