@hearthlog/runtime 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (120) hide show
  1. package/LICENSE +235 -0
  2. package/package.json +51 -3
  3. package/src/access/session.ts +223 -0
  4. package/src/access/user-session.ts +61 -0
  5. package/src/ai/decisions.ts +89 -0
  6. package/src/calendar/draft.ts +84 -0
  7. package/src/calendar/feeds.ts +91 -0
  8. package/src/calendar/groups.ts +198 -0
  9. package/src/calendar/logic.ts +141 -0
  10. package/src/calendar/preferences.ts +40 -0
  11. package/src/calendar/weeks.ts +56 -0
  12. package/src/commands/index.ts +184 -0
  13. package/src/commands/local-commit.ts +214 -0
  14. package/src/commands/standard.ts +8 -0
  15. package/src/commands/validation.ts +206 -0
  16. package/src/db/bootstrap.ts +72 -0
  17. package/src/db/calendar.ts +638 -0
  18. package/src/db/dynamic-ddl.ts +127 -0
  19. package/src/db/entities.ts +208 -0
  20. package/src/db/history.ts +194 -0
  21. package/src/db/index.ts +1240 -0
  22. package/src/db/kv.ts +33 -0
  23. package/src/db/local-statistics.ts +106 -0
  24. package/src/db/module-projections.ts +46 -0
  25. package/src/db/raw-browser.ts +361 -0
  26. package/src/db/read-repositories.ts +29 -0
  27. package/src/db/read-session.ts +47 -0
  28. package/src/db/schema-version.ts +76 -0
  29. package/src/db/session-context.ts +33 -0
  30. package/src/event/code-export.ts +65 -0
  31. package/src/event/dynamic-entities.ts +503 -0
  32. package/src/event/dynamic-materializer.ts +288 -0
  33. package/src/event/handlers.ts +490 -0
  34. package/src/event/index.ts +53 -0
  35. package/src/event/meta-entity-types.ts +165 -0
  36. package/src/event/projection-builder.ts +384 -0
  37. package/src/event/queries.ts +126 -0
  38. package/src/event/rebuild.ts +176 -0
  39. package/src/event/reference-lookup.ts +93 -0
  40. package/src/event/registry.ts +158 -0
  41. package/src/event/schema-effects.ts +45 -0
  42. package/src/event/types.ts +20 -0
  43. package/src/history/action-boundary.ts +18 -0
  44. package/src/history/action-detail.ts +23 -0
  45. package/src/history/change-comparison.ts +29 -0
  46. package/src/history/change-display.ts +41 -0
  47. package/src/history/contracts.ts +63 -0
  48. package/src/history/history.ts +372 -0
  49. package/src/history/legacy-replay.ts +69 -0
  50. package/src/history/legacy-url.ts +9 -0
  51. package/src/history/lineage.ts +123 -0
  52. package/src/history/log-format.ts +17 -0
  53. package/src/history/log-map.ts +110 -0
  54. package/src/history/navigation.ts +14 -0
  55. package/src/history/point-snapshot.ts +229 -0
  56. package/src/history/timeline.ts +215 -0
  57. package/src/history/url.ts +18 -0
  58. package/src/ledger/README.md +118 -0
  59. package/src/ledger/admin-client.ts +616 -0
  60. package/src/ledger/client.ts +10 -0
  61. package/src/ledger/config.ts +117 -0
  62. package/src/ledger/entity-id.ts +115 -0
  63. package/src/ledger/errors.ts +144 -0
  64. package/src/ledger/http-client.ts +590 -0
  65. package/src/ledger/index.ts +67 -0
  66. package/src/ledger/socket.ts +400 -0
  67. package/src/ledger/types.ts +202 -0
  68. package/src/ledger/version-tracker-singleton.ts +12 -0
  69. package/src/ledger/version-tracker.ts +259 -0
  70. package/src/log-management.ts +123 -0
  71. package/src/log-validation.ts +19 -0
  72. package/src/native/reminders.ts +2 -0
  73. package/src/native/shell.svelte.ts +136 -0
  74. package/src/native/shell.ts +102 -0
  75. package/src/pwa/registration.ts +169 -0
  76. package/src/recovery/local-recovery.ts +59 -0
  77. package/src/recovery/reset-sequence.ts +13 -0
  78. package/src/runtime/clock.ts +43 -0
  79. package/src/runtime/kept-page-data.svelte.ts +42 -0
  80. package/src/runtime/page-data.svelte.ts +160 -0
  81. package/src/runtime/phone-header.svelte.ts +17 -0
  82. package/src/runtime/read-coordinator.ts +103 -0
  83. package/src/runtime/refresh.ts +15 -0
  84. package/src/runtime/start-page.ts +56 -0
  85. package/src/runtime/view.ts +83 -0
  86. package/src/runtime/workspace.ts +62 -0
  87. package/src/storage/indexeddb-statistics.ts +153 -0
  88. package/src/store/attention.svelte.ts +51 -0
  89. package/src/store/conflict-merge.ts +169 -0
  90. package/src/store/entity-sync-state.svelte.ts +36 -0
  91. package/src/store/event-log.svelte.ts +109 -0
  92. package/src/store/events.svelte.ts +14 -0
  93. package/src/store/local-context.ts +93 -0
  94. package/src/store/local-store.svelte.ts +58 -0
  95. package/src/store/log-version.svelte.ts +35 -0
  96. package/src/store/logs.svelte.ts +60 -0
  97. package/src/store/moved.svelte.ts +43 -0
  98. package/src/store/offline-readiness.svelte.ts +188 -0
  99. package/src/store/outbox-request.ts +61 -0
  100. package/src/store/outbox.ts +426 -0
  101. package/src/store/pending-work-transfer.ts +49 -0
  102. package/src/store/pending-work.ts +143 -0
  103. package/src/store/sync-coordinator.ts +170 -0
  104. package/src/store/sync-invalidation.ts +10 -0
  105. package/src/store/sync-issues.ts +406 -0
  106. package/src/store/sync-progress.ts +54 -0
  107. package/src/store/sync-state.ts +127 -0
  108. package/src/store/sync.svelte.ts +1010 -0
  109. package/src/store/tab-coordination.ts +164 -0
  110. package/src/testing/e2e-seed.ts +40 -0
  111. package/src/testing/local-database.ts +120 -0
  112. package/src/testing/projection-database.ts +105 -0
  113. package/src/testing/test-entities.ts +28 -0
  114. package/src/theme/appearance.svelte.ts +111 -0
  115. package/src/utils/address.ts +5 -0
  116. package/src/utils/async.ts +30 -0
  117. package/src/utils/long-press.ts +70 -0
  118. package/src/utils/perf-dataset.ts +405 -0
  119. package/src/utils/perf.ts +627 -0
  120. package/README.md +0 -3
@@ -0,0 +1,118 @@
1
+ # Ledger Service Client
2
+
3
+ HTTP client library for the Ledger Service API, which provides entity-level optimistic locking for event storage.
4
+
5
+ The backend-owned OpenAPI contract is `packages/ledger/docs/event-store-service.openapi.yaml` at the monorepo root.
6
+
7
+ ## Files
8
+
9
+ | File | Purpose |
10
+ | -------------------- | ------------------------------------------------------------------ |
11
+ | `http-client.ts` | Ledger client, HTTP retry, API errors, and socket URL construction |
12
+ | `client.ts` | Shared configured browser transport |
13
+ | `admin-client.ts` | Typed, no-retry Ledger administrator client |
14
+ | `config.ts` | Configuration loading and validation |
15
+ | `types.ts` | TypeScript interfaces for API contracts |
16
+ | `entity-id.ts` | Entity ID extraction from AppEvent |
17
+ | `version-tracker.ts` | Entity version tracking in SQLite |
18
+ | `socket.ts` | WebSocket protocol validation, cursors, and reconnect lifecycle |
19
+ | `index.ts` | Public API exports |
20
+
21
+ ## Configuration
22
+
23
+ Configure via environment variables (copy `.env.example` to `.env`):
24
+
25
+ ### Local HTTP connection
26
+
27
+ - `VITE_LEDGER_SERVICE_URL` - Base URL for Ledger Service API
28
+ - Browser builds should not define `VITE_LEDGER_AUTH_TOKEN`; local and
29
+ production gateways inject the Ledger app credential server-side.
30
+
31
+ Browser realtime uses the same-origin gateway because the browser WebSocket
32
+ API cannot attach the app bearer header. The Vite development proxy and the
33
+ deployed frontend Worker both inject it during the upgrade.
34
+
35
+ In a production build both are optional. The client defaults to
36
+ `/api/ledger`, where the frontend Cloudflare Worker injects its
37
+ `LEDGER_AUTH_TOKEN` secret and forwards requests to the Ledger Worker through a
38
+ service binding.
39
+
40
+ The Developer page's Ledger tab uses `/api/ledger/admin/v1`. It first requests
41
+ the verified Access session and does not request application data until that
42
+ succeeds. Create, rotate, and revoke calls are attempted once; a network
43
+ failure has an unknown outcome and must be reconciled by refreshing the list.
44
+ New and rotated credentials are held only in component memory until their
45
+ one-time token drawer closes. Application details load log metadata for active
46
+ or revoked apps and provide a streamed JSON download containing app metadata,
47
+ every log, and every event. Existing bearer keys remain hidden; rotation is
48
+ the only operation that reveals a new key.
49
+
50
+ ### Optional
51
+
52
+ - `VITE_LEDGER_TIMEOUT_MS` - Request timeout (default: 5000ms)
53
+ - `VITE_LEDGER_MAX_RETRIES` - Retry attempts (default: 3)
54
+ - `VITE_LEDGER_ENABLE_REALTIME` - Enable WebSocket realtime delivery (default: true)
55
+
56
+ ## Usage
57
+
58
+ ```typescript
59
+ import { getLedgerClient } from "#lib/ledger/index.js";
60
+
61
+ const client = getLedgerClient();
62
+
63
+ // Resolve the stable tenant and credential principal.
64
+ const session = await client.getSession();
65
+
66
+ // List logs
67
+ const logs = await client.listLogs();
68
+
69
+ // Get events
70
+ const events = await client.getEvents(logId);
71
+
72
+ // Open realtime delivery after HTTP synchronization.
73
+ const socket = client.createEventSocket(logId, events.at(-1)?.version ?? 0);
74
+ socket.on("event", async (storedEvent) => {
75
+ // Persist first; acknowledge only after the local transaction commits.
76
+ socket.acknowledge(storedEvent.version);
77
+ });
78
+ await socket.open();
79
+
80
+ // Append a prepared idempotent batch.
81
+ await client.appendEvents(logId, request);
82
+ ```
83
+
84
+ Use `getLedgerClient()` for the app's default transport so bootstrap, sync, and log
85
+ consumers reuse negotiated capabilities. Concurrent `getSession()` calls share
86
+ only their pending request; the request is cleared on success or failure, and
87
+ later explicit session reads still verify online. Capability getters keep their
88
+ existing per-client cache. Use `new LedgerClient(config)` for a separately
89
+ configured transport; its verification and capabilities stay isolated.
90
+
91
+ ## Retry Policy
92
+
93
+ Default exponential backoff:
94
+
95
+ - Initial delay: 1s
96
+ - Maximum delay: 10s
97
+ - Multiplier: 2x
98
+ - Max retries: 3
99
+
100
+ Delays: 1s → 2s → 4s before failing.
101
+
102
+ ## Error Handling
103
+
104
+ | Error Class | HTTP Status | When |
105
+ | -------------------------- | ----------- | ---------------------------------------------- |
106
+ | `VersionConflictError` | 409 | First base is stale for an existing entity |
107
+ | `EntityVersionChainError` | 409 | Later bases do not form a valid batch chain |
108
+ | `IdempotencyMismatchError` | 409 | A request ID was reused with different content |
109
+ | `NotFoundError` | 404 | Resource not found |
110
+ | `AuthenticationError` | 401/403 | Auth failed |
111
+ | `LedgerServiceError` | Other | General API errors |
112
+
113
+ ## Development
114
+
115
+ For local development, run `pnpm ledger:dev` and then `pnpm dev` from the
116
+ repository root. The browser keeps using `/api/ledger`; Vite provisions a
117
+ local app credential and proxies both HTTP and `ledger.v1` WebSockets to the
118
+ Worker on `http://127.0.0.1:8787`.