Skip to content

agency: drop per-thread refine-context file; query the DB instead#113

Merged
MagMueller merged 1 commit intomainfrom
agency-refine-context-via-db
May 9, 2026
Merged

agency: drop per-thread refine-context file; query the DB instead#113
MagMueller merged 1 commit intomainfrom
agency-refine-context-via-db

Conversation

@MagMueller
Copy link
Copy Markdown
Contributor

@MagMueller MagMueller commented May 9, 2026

Why

The Edit (refine) flow used to write the original card's title + description + prompt to `/var/lib/bux/agency-refine-context/.txt` on tap, then `run_task` would read + delete that file on the user's first reply to seed the worker agent with the original context.

Two redundant surfaces — the data was always already in the suggestion row (`title`, `description`, `prompt`). The file was a duplicate-state cache that needed its own ownership / permissions handling on `/var/lib/bux`.

What

`agency_db.pop_refine_context_for_thread(db, thread_id) -> str | None` — atomic SELECT + UPDATE. Returns the assembled plain-text context AND flips a new `refine_context_injected` column 0 → 1 in one call. The context is injected exactly once, on the user's first reply in the worker topic. Subsequent replies in the same thread are normal lane runs.

Schema migration via `ALTER TABLE` with duplicate-column swallow, mirroring how `spawn_topic` was added in #107. Pre-existing rows get `refine_context_injected = 0` by default.

`run_task` swaps the file pop for the DB call. The kind=refine handler stops calling the file-writer. The file-based helpers (`_agency_write_refine_context`, `_agency_pop_refine_context`, `_agency_build_refine_context_plain`, `_AGENCY_REFINE_CTX_DIR`) are removed — `agency_db` owns the plain-text assembly now. The visible HTML context message in the new topic still renders via `_agency_build_refine_context`, just with the new `📎 Context` emoji + title (from #111).

Net

One fewer state surface, one fewer ownership/permissions race, the same UX for the user (Edit → context shown → "what would you change?" → user replies → agent re-drafts with original in scope).

Test plan

  • Schema migration runs cleanly on the existing DB; `refine_context_injected` column appears
  • `pop_refine_context_for_thread` on a non-worker thread → None
  • Tap Edit on a card → context message appears in spawned topic, no file created at `/var/lib/bux/agency-refine-context/`
  • User replies in the spawned topic → agent fires with original context prepended
  • User replies again → agent fires normally (no re-injection of context)

Summary by cubic

Replaced the per-thread refine-context file with a DB lookup that injects the original card context once on the user’s first reply. This removes duplicate state and permission hassles while keeping the same UX.

  • Refactors
    • Added refine_context_injected column (default 0) with duplicate-column-safe migration.
    • Implemented agency_db.pop_refine_context_for_thread(db, thread_id) to assemble context and flip the flag atomically.
    • Updated run_task to use the DB call; removed _agency_write_refine_context, _agency_pop_refine_context, _agency_build_refine_context_plain, and _AGENCY_REFINE_CTX_DIR.
    • Kept the visible context message via _agency_build_refine_context with the new "📎 Context" label.

Written for commit f6a1719. Summary will update on new commits.

The kind=refine flow used to write the original card's title +
description + prompt to /var/lib/bux/agency-refine-context/<thread>.txt
on Edit-tap, then `run_task` would read + delete that file on the
user's first reply to seed the worker agent with the original context.

Two redundant surfaces — the data was always already in the
suggestion row (title, description, prompt). The file was a
duplicate-state cache that needed its own ownership/permissions
handling on /var/lib/bux.

Replaces with a DB lookup:

  agency_db.pop_refine_context_for_thread(db, thread_id) -> str | None

Atomic SELECT + UPDATE: returns the assembled context AND flips a
new `refine_context_injected` column from 0 → 1 in one call. So the
context is injected exactly once, on the user's first reply in the
worker topic. Subsequent replies in the same thread are normal
lane runs.

Schema migration via ALTER TABLE with duplicate-column swallow,
mirroring how spawn_topic was added in #107. Pre-existing rows get
refine_context_injected = 0 by default.

`run_task` swaps the file pop for the DB call. The kind=refine
handler stops calling `_agency_write_refine_context` (the function
+ its sibling `_agency_pop_refine_context` + the `_AGENCY_REFINE_CTX_DIR`
constant + `_agency_build_refine_context_plain` are removed —
agency_db owns the plain-text assembly now). The visible HTML
context message in the new topic still renders via
`_agency_build_refine_context`, just with the new "📎 Context"
emoji + title (from #111).

Net: one fewer state surface, one fewer ownership/permissions race,
the same UX for the user (Edit → context shown → "what would you
change?" → user replies → agent re-drafts with original in scope).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 2 files

@MagMueller MagMueller merged commit 04112af into main May 9, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant