Skip to content

feat(bots): icon rules storage and roster timestamps (#692) - #348

Merged
notFrost merged 5 commits into
feat/bot-icon-timelinefrom
feat/bot-icon-timeline-server
Dec 1, 2026
Merged

notFrost merged 5 commits into
feat/bot-icon-timelinefrom
feat/bot-icon-timeline-server

Conversation

@notFrost

@notFrost notFrost commented Dec 1, 2026

Copy link
Copy Markdown

Part A of notFrost/sophie#692, phase 3: contract, storage and server. The client evaluator and display come in part B.

Bots get per-rule icon timelines and rule shapes (iconRules). The roster now says when a Bot started working, delivered a reply, or failed, so clients can pick which icon to show without extra requests.

What changed

Contract (packages/contracts/src/bot.ts)

  • BotRecord.iconRules: lenient when read back, so a later limit change can't break an older client. Absent means the default.
  • BotIconRulesInput on create and update enforces the limits:
    • at most 6 entries per rule;
    • unique start minutes within a rule;
    • order holds each rule exactly once;
    • shape ids from BOT_AVATAR_SHAPE_IDS;
    • lastCalledShapeAfterMinutes between 1 and 525,600;
    • images are png, jpeg or webp only. GIF is refused, both as an upload and as an already stored image.
  • BotRosterEntry gains workingSince, deliveredAt and erroredAt: an ISO time or null, and null when absent.
  • iconRules is left out of the bot_create and bot_update MCP tools (D25, D33).

Storage (BotStore.ts)

  • New icon_rules_json column, added for existing databases by migrateColumns. A Bot saved before this reads as the default.

Server (BotService.ts, new BotIconRules.ts and BotRosterTimes.ts)

  • Timeline images go through the same storage path as the icon. A timeline entry may reuse an already stored image but not a GIF.
  • Avatar files: removeAvatarsExcept keeps the icon plus every timeline image. avatarPath serves only an image the Bot currently shows.
  • Validation: create and update re-check icon rules against the input schema, because agents and other in-process callers skip RPC decoding.
  • D32: a rule shape equal to the Default Icon's shape is stored as null. This applies on create, on update, and when the icon changes.
  • D31: changing the icon, including to another kind, keeps iconRules. An update without iconRules keeps the stored value.
  • Roster fields:
    • workingSince is the later of the active work's start (activityRunStartedAt) and the user's last answer to a request that blocked the Bot (D16). Null unless the Bot is working.
    • deliveredAt is the latest run's completion time when it completed and its last assistant message isn't NO_REPLY or empty (D18).
    • erroredAt is the latest run's completion time when it failed (D23).
  • Roster refresh: the reactor now re-sends the roster when any request in a Bot thread is answered, dismissed, expired or cancelled. Before, nothing re-sent it when a Bot went from waiting back to working.

No changes to orchestrationV2.ts or ProjectionStore.ts.

Answers from the investigation

a) Does a failed run set completedAt? Yes.

  • Every path that ends a run as failed writes completedAt.
  • Normal turn end: RunExecutionService.ts:771 sets it, :866-870 puts it on the run, :1032 emits it. This covers failed as well as completed, interrupted and cancelled.
  • Also: a queued run that fails (Orchestrator.ts:2589), a start failure (Orchestrator.ts:13723), and ProviderTurnStartService.ts:627.
  • The shell passes it through as latestRunCompletedAt (ProjectionStore.ts:1955), and failed runs are never treated as silent (ProjectionStore.ts:2074).

b) How are NO_REPLY and silent runs detected?

  • Silent runs: ProjectionStore already drops them from the shell's runs. That covers idle compactions, memory consolidations, and check-ins or mention notices the Bot answered with NO_REPLY (isSilentRunForShell, ProjectionStore.ts:2068-2085, applied at :1881). So the shell's latest run is always the latest non-silent one, and deliveredAt only looks at that run. A silent run afterwards doesn't replace an earlier delivery.
  • NO_REPLY: the shell leaves latestVisibleMessage null (ProjectionStore.ts:1977), so the server reads that run's assistant messages. It does that once per run and caches the verdict per Bot thread (BotService.ts:336). isDelivery (BotRosterTimes.ts:52) takes the last non-empty assistant message and checks it with the shared isBotNoReplyMessage, which also catches NO_REPLY wrapped in markdown.
  • Only completed runs count as a delivery, matching isAnsweredRun in ProjectionStore. An interrupted run with a partial reply does not.

c) Which event re-sends the roster on waiting → running? Before this PR, none.

  • A blocking request leaves the run running. The Bot reads as waiting only through shell.pendingRequests (statusOf), so answering it emits no run.updated.
  • The resolution arrives as runtime-request.updated. The reactor used to return early for every settled request (only a resolved user_input reached onHumanInput, which doesn't notify).
  • Now onRequestSettled (BotService.ts:1191, reached from :1223) notifies for any settled request in a Bot thread. It also records the wait's end for workingSince. run.updated (:1219) still covers deliveries and failures.
  • Proof: with that notify removed, the D16 test times out (TestTimeoutError: Test timed out in 120000ms). With it, the test passes.

How workingSince learns when a wait ended:

  • A Bot thread keeps every request it ever made, and the records query can't filter requests by run.
  • So the server reads a Bot thread's answered requests once, then keeps the latest wait end per Bot thread from the reactor's events (BotService.ts:324).
  • This survives a server restart: the first read after a restart comes from the thread's records.

Tests

  • vp run typecheck: vp run: 42/44 cache hit (95%), 44/44 successful (exit 0)
  • vp lint on the changed files: exit 0. The pre-commit hook passed fmt, lint and knip.
  • vp test run apps/server/src/bots apps/server/src/serverPush apps/server/src/mcp packages/contracts/src/bot.test.ts:
    × t3_worktree_handoff from a Full access thread runs an approved layout command
    × uses the fetched commit and remote PR base for an explicit local alias
    Test Files  2 failed | 48 passed (50)
         Tests  2 failed | 755 passed (757)
    

The 2 failing tests were already failing before this PR. I ran both on a separate clean checkout of origin/feat/bot-icon-timeline (d7519d4), and both fail there the same way:

× uses the fetched commit and remote PR base for an explicit local alias
× t3_worktree_handoff from a Full access thread runs an approved layout command
Tests  2 failed | 32 passed (34)

One compares a Windows path separator (\worktree vs /worktree); the other runs a layout shell command that exits non-zero on Windows. Neither touches Bot code.

New tests:

  • BotService.test.ts:
    • timeline images: stored, sorted, served, and the ones no longer shown deleted and refused by avatarPath;
    • saving to the database and reading back;
    • an update without iconRules keeps it;
    • D31 icon kind change keeps rules and images;
    • D32 on create, on icon change, and on new rules;
    • 10 rejections of rules that skip the schema (7th entry, repeated minute, minute over a year, repeated or missing rule in order, unknown shape, last-called delay of 0 or over a year, GIF upload, stored GIF);
    • roster times for a real reply, NO_REPLY, **NO_REPLY**, no reply, a failed run and a working Bot;
    • D16: a wait that ends restarts workingSince and re-sends the roster, and a non-blocking answer doesn't move it;
    • after a restart, the wait end is read from the thread.
  • BotStore.test.ts: a Bot keeps its icon rules, and a database without the column reads the default.
  • BotRosterTimes.test.ts, BotIconRules.test.ts: pure helpers.
  • mcp/toolkits/bots/handlers.test.ts: its fake thread service gained an empty getThreadRecords, because the roster now reads records while a Bot is working.

Left out

  • Client evaluator and display (part B: packages/client-runtime, apps/web).
  • Editor UI.
  • Mobile (D21: desktop only).
  • Exposing iconRules through MCP (D25, D33).

🤖 Generated with Claude Code

notFrost and others added 5 commits November 30, 2026 23:12
…#692)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ck-fixtures

test(bots): icon rule fields in test fixtures (pingdotgg#692)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@notFrost notFrost left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Review (Lukas, lead engineer): approved, merging into feat/bot-icon-timeline. Posted as a comment because this account can't formally approve.

Checked against the issue pingdotgg#692 decisions and phase3-contract.md:

  • Storage: additive icon_rules_json column. NULL decodes to DEFAULT_BOT_ICON_RULES. Tested with a legacy row that has the column dropped.
  • Validation: BotIconRulesInput is decoded again in the service for in-process callers. Timeline images accept png/jpeg/webp only, so a GIF data URL or reference is refused (D19). Timeline images share the 512 KB limit.
  • D32: rule shapes equal to the Default Icon's shape are nulled on create and on update, including an icon-only update.
  • D31: an icon change keeps iconRules.
  • Images: removeAvatarsExcept keeps the icon and every timeline image, and avatarPath serves timeline images. Tested.
  • Roster timestamps:
    • workingSince is set only while the Bot is working, and restarts after a blocking wait (D16). Tested, including after a restart.
    • deliveredAt counts completed, non-silent runs and excludes NO_REPLY (D18).
    • erroredAt is set on a failed run (D23).
    • The waiting → running roster push is now fixed via onRequestSettled. The proof: the D16 test times out without the fix.
  • D25/D33: iconRules is not in the MCP tools.
  • Scope: the diff against the base touches only the 21 expected files, so no stray reverts.

Run locally on fb9458aa9a (Windows):

vp run typecheck                → 44/44 successful, exit 0
vp lint --report-unused-disable-directives <21 changed files> → exit 0
vp test run apps/server/src/bots apps/server/src/serverPush apps/server/src/mcp \
  packages/contracts/src/bot.test.ts packages/client-runtime/src/botAvatarShapeIds.test.ts
  Test Files  2 failed | 49 passed (51)
  Tests       2 failed | 756 passed (758)

The two failures are the Windows-only worktree tests (WorktreeMcpService.test.ts explicit local alias, handlers.test.ts approved layout command). They also fail on the clean base d7519d4, so they are not caused by this PR.

Noted, not blocking:

  • An interrupted run with a partial reply doesn't count as delivered. That's fine for now, and I'll raise it with Frost if he wants otherwise.
  • onRequestSettled now pushes the roster on every settled request in a Bot thread, not only user_input ones. That's a little extra traffic, and it's acceptable.

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