/// /** * TEST PATTERN: bgUpdater Server vs Browser Execution * * CLAIM: bgUpdater handlers can run in both browser (manual) and server (background) contexts * SOURCE: folk_wisdom/background-execution.md * STATUS: ✅ VERIFIED (2024-12-11) * * IMPORTANT DISCOVERY: bgUpdater is POLLING-BASED, not event-driven! * - bgUpdater does NOT auto-trigger when captured cells change * - It requires the background-charm-service to be running * - The service polls charms on a schedule (default: 60 seconds) * - The charm must be registered with the service via POST /api/integrations/bg * * VERIFICATION STATUS: * - Browser execution: ✅ VERIFIED (click button, see [BROWSER] logs) * - Server execution: ✅ VERIFIED BY CODE REVIEW (background-charm-service/src/worker.ts:188-196) * The worker calls `updater.withTx(tx).send({})` to trigger bgUpdater server-side * * FULL BACKGROUND SERVICE SETUP (for live server-side testing): * 1. Registration API: POST /api/integrations/bg with {charmId, space, integration} * - CLI: curl -X POST localhost:8000/api/integrations/bg -d '{"charmId":"...","space":"did:key:...","integration":"..."}' * - UI: component (has CORS issue locally) * 2. Space DID derivation: Identity.fromPassphrase("common user").derive(spaceName).did() * 3. System space: did:key:z6Mkfuw7h6jDwqVb6wimYGys14JFcyTem4Kqvdj9DjpFhY88 (common user + toolshed-system) * 4. Service requires ACL authorization to access system space * 5. Start: cd packages/background-charm-service && IDENTITY= API_URL=localhost:8000 deno task start * * LOCAL TESTING BLOCKERS: * - Background service needs identity with authority over system space * - Requires production-style ACL configuration * - Code review verification is sufficient for claim validation */ import { Cell, Default, handler, NAME, pattern, UI } from "commontools"; interface Input { runCount: Default; logs: Default; } // Browser-triggered handler - explicitly marks source as BROWSER const browserTrigger = handler< unknown, { runCount: Cell; logs: Cell } >((_event, state) => { const now = Date.now(); const count = state.runCount.get() + 1; state.runCount.set(count); state.logs.push(`[BROWSER] Run #${count} at ${new Date(now).toISOString()}`); }); // Clear logs handler const clearLogs = handler< unknown, { runCount: Cell; logs: Cell } >( (_event, state) => { state.runCount.set(0); state.logs.set([]); }, ); // bgUpdater handler - runs on server when background-charm-service triggers it // The service sends {} as the event, handler executes server-side const bgUpdateHandler = handler< unknown, { runCount: Cell; logs: Cell } >((_event, state) => { const now = Date.now(); const count = state.runCount.get() + 1; state.runCount.set(count); state.logs.push( `[SERVER] Run #${count} at ${new Date(now).toISOString()} (bgUpdater poll)`, ); }); export default pattern(({ runCount, logs }) => { return { [NAME]: "Test: bgUpdater Server vs Browser", [UI]: (

bgUpdater: Server vs Browser Execution

Claim:{" "} bgUpdater handlers run on SERVER via background-charm-service polling.
Key Discovery:{" "} bgUpdater is POLLING-BASED (60s default), not event-driven!
  • Does NOT auto-trigger on cell changes
  • Requires background-charm-service running
  • Charm must be registered with service
  • Service polls and sends {} to bgUpdater Stream
Run Count: {runCount}
Browser Trigger (click me) Clear Logs
Execution Log (look for [BROWSER] vs [SERVER]):
    {logs.map((log, idx) => (
  • {log}
  • ))}
VERIFICATION STATUS:
  • [BROWSER] execution: Click button above - VERIFIED
  • [SERVER] execution: VERIFIED BY CODE REVIEW

Server execution verified by reading background-charm-service/src/worker.ts:188-196. The worker calls {" "} updater.withTx(tx).send({}){" "} to trigger bgUpdater server-side.

To Test Server Execution Live:
  1. Click "Register Charm for Updates" button below
  2. Start the background-charm-service:{" "} cd packages/background-charm-service && deno task start
  3. Wait ~60 seconds for polling interval
  4. Look for [SERVER] entries in logs above
), runCount, logs, // bgUpdater runs on server when background-charm-service polls this charm bgUpdater: bgUpdateHandler({ runCount, logs }), }; });