Skip to content

feat(portal): show a patch's log to its owner and admins - #451

Merged
allisonmahmood merged 6 commits into
mainfrom
tier2/412-log-view
Oct 1, 2026
Merged

allisonmahmood merged 6 commits into
mainfrom
tier2/412-log-view

Conversation

@allisonmahmood

@allisonmahmood allisonmahmood commented Sep 30, 2026 •

Copy link
Copy Markdown
Owner

Tier 2 records every invocation, the calls it makes and its ctx.log lines, but nobody could read that record outside the database. Owners and admins had no way to see who ran what, as whom, or why an action failed.

This adds the per-patch log to the portal, as picked on #385 (L-2 and W-2):

  • /patches/<name>/log, for the patch's current owner and every admin. Everyone else gets a 403, and reassignment moves the owner's access. Rows are top-level invocations, newest first: time, person, version, handler, kind, outcome and duration. A row expands into the calls and nested handlers inside it, in sentence form ("leads.create (mutation) · as the patch"), followed by the patch's ctx.log lines. Failed rows open by default. A reply the page never received shows the real settled outcome plus "reply not delivered". Pages hold 25 entries, paged by cursor. Filters cover outcome, person and handler and search 1,000 entries at a time; the page says when older entries lie beyond that window. Connection calls name the connection by handle. Each entry reads and shows at most 50 steps, outermost first, and says when it made more. The nothing-logged and filtered-empty states follow the mock. The page says the log is an attribution record, not an access audit, and that log lines are written by the patch itself.
  • Recent activity on the card for the owner and admins: the last three entries in sentence form ("Alice Chen · Sales CRM v12 · leads.import (action)"), each linking into the log, then "See the full log".
  • InvocationLog gains two bounded readers over the existing runtime_invocations and runtime_calls columns: page (the card skips its trees) and choices, which lists the filter options from the newest 1,000 entries. No migration. The per-connection recent-calls list is unchanged.
  • The shell component set gains pill-failed, list-tree and an expandable table-expand row. The log table keeps words whole and scrolls inside portal-table on phones; other tables are unchanged.
  • Docs: the ui-consistency spec, the Portal glossary (Log), CONTEXT-MAP.md (Portal → Runtime) and docs/product.md (who reads the log).

Stacked on tier2/411-csv-download (#450); this PR contains only the #412 commits. Implements #412, part of #384.

Verification

  • pnpm lint and pnpm typecheck pass.
  • Tests pass in packages/portal, packages/runtime, packages/core, apps/server, packages/patches/src/Subscriptions.test.ts and packages/patchy/src/devExecution.test.ts. New cases:
    • an owner sees a failed action's tree, with the connection call that succeeded before the failure, and is refused another owner's log;
    • admins read every patch's log, and reassignment moves the owner's access;
    • Recent activity appears for the owner and admins only;
    • cursor paging, filters, and the empty states;
    • InvocationLog.page bounds each entry's tree, reads overdue entries as unknown and scopes cursors to the patch.
  • I checked the log page, card and narrow layouts against the L-2 and W-2 mocks in a real browser against a disposable server with seeded invocations. The versions table on phones is unchanged from before this branch.

Built with Claude Opus 5.5 via Claude Code.

@github-actions github-actions Bot added area:server Touches the server area area:cli Touches the cli area area:core Touches the core area area:docs Touches the docs area area:ui Portal and first-party browser pages, shared rendering and styles size:xl Over 1,000 changed lines; review in multiple passes labels Sep 30, 2026
@allisonmahmood

Copy link
Copy Markdown
Owner Author

Opus review

Two findings, both about read cost on a large log. I checked authorization, escaping and outcome labels and found nothing blocking there (details at the end).

1. The 50-step cap only trims the output: every step is still read, and the card reads trees it never shows

packages/runtime/src/InvocationLog.ts:236-270: treeItems builds the whole recursive tree for each root, joins every runtime_calls row, and then ranks it with row_number() / count(*) OVER (PARTITION BY root). The WHERE n <= 50 filter runs only after that. A window over a partition has to materialize every row in it first, so each entry costs its full step count, and that includes every nested invocation's log_lines.

The registry lets this get big. tier2.callbacks.count is 1,000 per invocation, and "nested invocations count separately". A plain import action that calls one nested mutation per row, 1,000 rows with one insert each, gives about 2,000 steps per entry. A 25-entry page (PortalPages.ts:301) then reads and sorts about 50k rows to show at most 1,250. The limits allow fan-out well past that.

The card makes this worse. recentActivity (PortalPages.ts:123-133) calls page({ limit: 3 }), which always runs treeItems (InvocationLog.ts:353). But renderRecentActivity (log.ts:251) destructures only invocation and drops the tree. So every card view by an owner or admin, POST re-renders included, runs the recursive query over its three latest entries and throws the result away. The card is the portal's most-hit page.

Fix:

  • Give the card a tree-free read, either a page option or a small recent reader.
  • Cap the reads per root rather than the output. For example, fetch TREE_LIMIT + 1 steps per root with a LATERAL … LIMIT, and say "Showing the first 50 calls" without an exact total. Nothing on the ticket needs the exact count.

2. Filtered pages scan the patch's whole history when matches are rare

InvocationLog.ts:212-223: the person, handler and outcome filters are row filters on top of runtime_invocations_patch_recent (company_id, patch_id, started_at DESC, id DESC):

  • handler and initiating_viewer_id are not in the index.
  • outcomeOf(now) is computed per row and can't be indexed.
  • parent_id IS NULL is also a filter, so nested rows get walked too.

When a filter matches fewer than 26 rows, the scan runs to the start of the patch's log on every request.

Three things make this the ordinary case, not an edge case:

  • Filter values come straight from the URL. Any string up to 256 characters is accepted.
  • log.ts:196 deliberately keeps stale filter values "even if it left the recent window". Those are exactly the rare-match values.
  • runtime_invocations has no retention. A query that calls ctx.log on each subscription re-run adds a row per run.

The PR caps choices at 1,000 entries "so reading them stays bounded" (InvocationLog.ts:117), and the ticket says the page "is bounded", but the filter those choices feed has no bound.

Fix, simplest first:

  • Apply the filters inside the same bounded window: the newest N top-level entries after the cursor. Carry the cursor past the window so "Older entries" keeps going, and word the empty state as "No matches in the newest N entries".
  • Or, beyond that, add partial indexes on (company_id, patch_id, handler, started_at DESC, id DESC) WHERE parent_id IS NULL and the same for initiating_viewer_id. The outcome filter would still need the window.

Checked and fine

  • Authorization. Owners, admins and the old owner after reassignment are handled correctly: ownership is read per request (PortalPages.ts logPage). A member who is neither gets a 403 with no log data. Recent activity is hidden from them.
  • Scoping. Every query is scoped to viewer.company.id and the resolved patch id. A cursor from another patch or company resolves to NULL and gives an empty page, and the tests cover this. The recursion is company-scoped.
  • Escaping. ctx.log text, JSON details, handler names, person names, patch titles, outcome codes and every attribute are escaped.
  • Outcome labels. Labels match the stored states: commit proof wins, overdue pending rows read as unknown, and "reply not delivered" is shown only on settled rows with reply_delivered = false.
  • Components and dependency. pill-failed, list-tree and table-expand live in packages/core, and page CSS stays layout. Portal → Runtime is recorded in CONTEXT-MAP.md.

@allisonmahmood

Copy link
Copy Markdown
Owner Author

Astra review

Reviewed d75b245 against origin/tier2/411-csv-download using the three-dot diff, against #412, #384 §17, and the L-2/W-2 picks on #385. One pass, no subagents. I would block merge on the two intent findings below.

Standards

No merge-blocking method or YAGNI findings.

Spec

  1. P2: Preserve the connection identity in operation rows. InvocationLog.ts:253–258 drops the stored connection_id, and log.ts:123–128 consequently cannot identify the connection. feat(portal): the per-patch log view for owners and admins #412 asks for operation rows such as "warehouse.customers.list · as Alice". Two calls to public.customers on different connections render identically as postgres.list public.customers (connection call) · as Alice. For postgres.query, the relation is null too. The owner cannot tell which integration a successful or failed step accessed. Carry connection_id through the reader and render a readable connection identifier, retaining the stored ID as a fallback.

  2. P2: Keep ancestors before their children when bounding the tree. InvocationLog.ts:266–271 ranks by (at, id) before keeping 50 items. feat(portal): the per-patch log view for owners and admins #412 requires "each an invocation tree ... with its operation rows beneath". When a nested invocation and its call share a millisecond timestamp, call_… sorts before inv_…. At the cutoff, the reader can retain the call but discard its parent; the renderer then silently omits that call because it walks from the root. With 49 earlier steps plus that parent/call pair, the reader returns 50 items but the page renders only 49 while claiming "Showing the first 50 of 51 calls." Rank and truncate in an ancestor-preserving order, including timestamp ties.

Verification: reproduced both cases through the real InvocationLog.page and portal renderer over disposable Postgres with the repository's runtime migrations. Expanded the generated pages in Chromium and observed the indistinguishable connection rows and missing cutoff call. No production or daily-driver instance touched.

Summary: Standards 0 blockers; Spec 2 blockers. Worst Spec issue: the log loses which connection an operation accessed.

@allisonmahmood

Copy link
Copy Markdown
Owner Author

Review responses

I fixed all four findings in e86af75 (docs follow-up in the next commit). Nothing was kept as is.

Opus 1: tree reads were unbounded, and the card read trees it never showed. Fixed.

  • The card's Recent activity now calls page({ trees: false }), so it reads only the three top-level rows it renders.
  • InvocationLog reads each entry's steps one level at a time. Each kept parent reads at most "room left + 1" nested handlers and "room left + 1" operation rows, through the existing (company_id, parent_id, started_at, id) and (company_id, invocation_id, at, id) indexes, using LATERAL … LIMIT. What an entry costs is bounded by the 50-step cap, not by how many steps it made. A 60-insert action reads 51 call rows, not all 60.
  • There is no exact total any more. The summary reads "50+ calls" and the cut note reads "Showing 50 calls, outermost first. This entry made more."

Opus 2: filtered pages scanned the whole history. Fixed.

  • Filters now apply inside the newest 1,000 top-level entries after the cursor (FILTER_WINDOW), the same window choices uses.
  • If a window comes back short and older entries exist beyond it, page returns the window's edge as the next cursor. The page says "Filters search 1,000 entries at a time. Older entries continues further back." and keeps "Older entries". An empty result reads "No entries match these filters in the 1,000 entries searched."
  • I added no partial indexes, since the window bounds the read without a migration.

Astra 1: the connection a call went to was lost. Fixed, with no migration: runtime_calls.connection_id already identifies the connection.

  • The tree reader carries connectionId, and the log page resolves handles with ConnectionStore.list only when a page has connection calls.
  • Calls now read "warehouse.customers.list (connection call) · as Alice Chen". A deleted connection falls back to its stored id, and so does a store that can't answer.
  • Portal now depends on @patchy/integrations.

Astra 2: a cut could keep a child and drop its parent. Fixed by the level-wise read above. A step is read only after its parent has been kept, so every kept step's parent is kept regardless of timestamp ties. The tree is cut outermost first, and each parent's own steps stay in time order.

Verification

  • New InvocationLog tests:
    • Astra's exact case: 49 calls, then a nested handler and its own call in the same millisecond. The tree keeps 50 steps, ends with the nested handler, marks the entry truncated, and every kept step's parent is kept.
    • trees: false returns no steps.
    • A 1,001-entry patch where only the oldest entry matches returns an empty first window with windowEnded and the edge cursor; the next window returns the match.
  • The portal test now expects "warehouse.customers.list (connection call) · as Alex …" from a connection fixture.
  • pnpm lint and pnpm typecheck pass, as do the tests for portal, runtime, core, apps/server, Subscriptions and devExecution.
  • I also checked in a browser against a disposable server: the connection handle in the tree, a 60-call action cut to "50+ calls" with the note, and a rare handler filter showing the window note and continuing with Older entries.

CI on the previous head: test (22) failed on two timeouts in code this PR doesn't touch: Supervisor.test.ts "evicts an idle sibling during invocation admission…" at 20 s, and cli.test.ts "keeps src/main.ts failure diagnostics out of the public envelope" at 5 s. test (24) passed on the same commit. The new push re-runs both.

@allisonmahmood allisonmahmood linked an issue Oct 1, 2026 that may be closed by this pull request
@allisonmahmood
allisonmahmood force-pushed the tier2/411-csv-download branch from 50057a1 to 1f8b40e Compare October 1, 2026 06:31
Base automatically changed from tier2/411-csv-download to main October 1, 2026 07:11
allisonmahmood and others added 6 commits October 1, 2026 09:11
A per-patch log page at /patches/<name>/log (L-2): newest-first entries
paged by cursor, filtered by outcome, person and handler, each row
expanding into its calls and nested handlers in sentence form with the
patch's ctx.log lines. The card gains Recent activity (W-2) for the
owner and admins. InvocationLog gains bounded page and choices readers;
the shell set gains pill-failed, list-tree and an expandable table row.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Tables used overflow-wrap: anywhere, which let auto layout squeeze
columns until headers split mid-word ("Versio / n"). break-word keeps
words whole in column widths, so a narrow table scrolls inside its
wrapper; only a token wider than the table still breaks. The log page's
own cell rule is no longer needed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
622cfd8 changed the shared .table wrapping, which made the card's
versions table scroll sideways on phones. Restore the shared rule and
keep words whole only in the log table, which scrolls in portal-table.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Review follow-ups for #412:
- The card's Recent activity reads only top-level rows, not trees.
- Entry trees are read a level at a time, each kept parent reading at
  most the room left plus one step through the parent and invocation
  indexes, so a cut never keeps a step without its parent.
- Filters apply within the newest 1,000 entries after the cursor; the
  page says so and Older entries continues past the window's edge.
- Connection calls name the connection by handle from the stored
  connection_id (no migration), falling back to the id.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@allisonmahmood
allisonmahmood merged commit 3d9e9d3 into main Oct 1, 2026
15 of 16 checks passed
@allisonmahmood
allisonmahmood deleted the tier2/412-log-view branch October 1, 2026 07:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:cli Touches the cli area area:core Touches the core area area:docs Touches the docs area area:server Touches the server area area:ui Portal and first-party browser pages, shared rendering and styles size:xl Over 1,000 changed lines; review in multiple passes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(portal): the per-patch log view for owners and admins

1 participant