Repository navigation
Add package services runtime #264
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 30 commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
4b8c2a7
Add package services runtime
cursoragent 8147b28
Bind package services in preview and test
cursoragent 05e040d
Polish package services review feedback
cursoragent 28f7fb8
Fix oauth workers env for package services
cursoragent 8397a00
Tighten package services runtime parity
cursoragent f3d0d47
Schedule package service auto-start alarms
cursoragent 21a9553
Tighten package service capability inputs
cursoragent b2c5bb3
Make package services gateway-friendly
cursoragent 5c894ad
Add package service timeout controls
cursoragent 97404a3
Export service helper from kody runtime
cursoragent cf3b668
Add Discord gateway package guide
cursoragent 1bf4480
Keep service runtime helpers in module execution
cursoragent a3a31bd
Polish package service runtime flow
cursoragent 02503bc
Recover package services after DO restart
cursoragent 9f66ded
Preserve explicit package service stops
cursoragent 7c6611c
Harden package service alarm behavior
cursoragent f9c49d4
Polish package service RPC handling
cursoragent 54641bc
Generalize package service examples
cursoragent 4790002
Tighten package service helper semantics
cursoragent 5428196
Retry MCP test server port binding
cursoragent fdf5f67
Clear stale package service stop requests
cursoragent 55301df
Refine package service lifecycle edges
cursoragent e5ccfe3
Isolate package service auto-start failures
cursoragent aa62f27
Polish package service runtime lifecycle
cursoragent 2cf25f4
Preserve null service timeout status
cursoragent 7d0a93e
Reuse saved package in service listing
cursoragent e4e06f4
Drop unused package service auto-start helper
cursoragent 2489d44
Rearm auto-start services after unexpected exits
cursoragent 9217317
Share package service status normalization
cursoragent 17cf643
Treat service_start as restart intent
cursoragent 4678d67
Retry auto-start services after alarm load errors
cursoragent 93d5de5
Back off auto-start alarm retries
cursoragent cf53f5b
Keep service status validation in runtime
cursoragent ad49d08
Honor autoStart when alarm retries fire
cursoragent cfe34b4
Back off runtime auto-start restarts
cursoragent 31d3271
Polish package service review follow-ups
cursoragent 7b06950
Trim package service runtime surface
cursoragent 597effc
Harden package service registry calls
cursoragent 07aba12
Align package app service list shape
cursoragent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,193 @@ | ||
| # Package service pattern | ||
|
|
||
| Use this guide when you want a **native Kody package** to own a long-lived | ||
| runtime instead of relying on a separate external service. | ||
|
|
||
| This guide assumes: | ||
|
|
||
| - the package runtime already supports `package.json#kody.services` | ||
| - the package will likely also expose a package app for operator UI | ||
| - protocol credentials and downstream delivery details are handled separately | ||
|
|
||
| ## Recommended package shape | ||
|
|
||
| Prefer one saved package with: | ||
|
|
||
| - `package.json#kody.app.entry` for setup UI, dashboards, and callback endpoints | ||
| - `package.json#kody.services.realtime-supervisor.entry` for the long-lived | ||
| runtime | ||
| - package exports for reusable helpers and formatting logic | ||
|
|
||
| Example manifest shape: | ||
|
|
||
| ```json | ||
| { | ||
| "name": "@scope/realtime-supervisor", | ||
| "exports": { | ||
| ".": "./src/index.ts", | ||
| "./format-event": "./src/format-event.ts" | ||
| }, | ||
| "kody": { | ||
| "id": "realtime-supervisor", | ||
| "description": "Native long-lived service package", | ||
| "app": { | ||
| "entry": "./src/app.ts" | ||
| }, | ||
| "services": { | ||
| "realtime-supervisor": { | ||
| "entry": "./src/services/realtime-supervisor.ts", | ||
| "autoStart": true, | ||
| "timeoutMs": 300000 | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## Runtime model | ||
|
|
||
| Package services run as **background-managed** service instances: | ||
|
|
||
| - `service_start` returns immediately with a running state | ||
| - the service Durable Object keeps lifecycle state such as `status`, | ||
| `active_run_id`, `stop_requested`, and `next_alarm_at` | ||
| - the service module runs with: | ||
| - `packageContext` | ||
| - `serviceContext` | ||
| - service-owned writable `storage` | ||
| - a `service` helper from `kody:runtime` | ||
|
|
||
| Import shape inside a service module: | ||
|
|
||
| ```ts | ||
| import { service, serviceContext, storage } from 'kody:runtime' | ||
| ``` | ||
|
|
||
| The `service` helper exposes: | ||
|
|
||
| - `await service.getStatus()` | ||
| - `await service.shouldStop()` | ||
| - `await service.setAlarm(runAt)` | ||
| - `await service.clearAlarm()` | ||
|
|
||
| ## Recommended service loop | ||
|
|
||
| Treat the service entry as a **runtime supervisor**, not just a one-shot job. | ||
|
|
||
| Recommended loop: | ||
|
|
||
| 1. Load persisted session state from `storage` | ||
| - configuration references | ||
| - last checkpoint / cursor / offset | ||
| - reconnect endpoint or transport metadata | ||
| - topology metadata if applicable | ||
| 2. Open the outbound connection or initialize the runtime session | ||
| 3. Authenticate, subscribe, or resume | ||
| 4. Enter a loop that: | ||
| - reads remote events or messages | ||
| - persists resumable state before acting on them | ||
| - publishes normalized events to the package app or package exports | ||
| - periodically checks `await service.shouldStop()` | ||
| 5. On clean shutdown: | ||
| - close the connection | ||
| - clear alarms when no reconnect is desired | ||
| 6. On transient disconnect: | ||
| - persist the latest resumable state | ||
| - schedule reconnect with `await service.setAlarm(...)` | ||
| - exit the current run | ||
|
|
||
| Pseudo-code shape: | ||
|
|
||
| ```ts | ||
| import { service, storage } from 'kody:runtime' | ||
|
|
||
| async function openSocket(session: unknown) { | ||
| void session | ||
| return await new Promise<WebSocket>((resolve, reject) => { | ||
| const socket = new WebSocket('wss://example.com/stream') | ||
| socket.addEventListener('open', () => resolve(socket), { once: true }) | ||
| socket.addEventListener('error', () => reject(new Error('Failed to open stream')), { | ||
| once: true, | ||
| }) | ||
| }) | ||
| } | ||
|
|
||
| export default async function run() { | ||
| const session = (await storage.get('session-state')) ?? null | ||
| const socket = await openSocket(session) | ||
|
|
||
| try { | ||
| // Authenticate, subscribe, or resume here. | ||
|
|
||
| while ( | ||
| socket.readyState !== WebSocket.CLOSING && | ||
| socket.readyState !== WebSocket.CLOSED | ||
| ) { | ||
| if (await service.shouldStop()) { | ||
| socket.close(1000, 'service stop requested') | ||
| await service.clearAlarm() | ||
| return { stopped: true } | ||
| } | ||
|
|
||
| const event = await readNextEvent(socket) | ||
| await persistRuntimeState(storage, event) | ||
| await handleRuntimeEvent(event) | ||
| } | ||
| } catch (error) { | ||
| await persistFailure(storage, error) | ||
| await service.setAlarm(new Date(Date.now() + 5_000)) | ||
| throw error | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## Use the package app as the operator plane | ||
|
|
||
| Do not push all operational interaction into the service itself. | ||
|
|
||
| Use the package app for: | ||
|
|
||
| - health dashboards | ||
| - reconnect / pause / resume controls | ||
| - logs | ||
| - live event inspection | ||
| - setup flows | ||
|
|
||
| The package app can call the package service lifecycle surface and consume | ||
| service status through the existing package runtime bridge. | ||
|
|
||
| ## Prefer bounded reconnect loops over one immortal run | ||
|
|
||
| Cloudflare Durable Objects can open outbound WebSockets, but outgoing WebSockets | ||
| do **not** hibernate. Favor a design where the service can: | ||
|
|
||
| - reconnect intentionally | ||
| - reschedule itself with alarms | ||
| - persist resumable state early | ||
| - stop cooperatively | ||
|
|
||
| Use `timeoutMs` to give the service enough room to run, but do not rely on | ||
| indefinite execution as the only lifecycle mechanism. | ||
|
|
||
| ## What to persist | ||
|
|
||
| At minimum, persist: | ||
|
|
||
| - reconnect/session identifiers | ||
| - latest offset / sequence / cursor | ||
| - topology metadata if needed | ||
| - reconnect URL or endpoint metadata | ||
| - last error / last disconnect reason | ||
|
|
||
| Persist before any expensive or lossy downstream work so reconnects can resume | ||
| from durable state. | ||
|
|
||
| ## Downstream delivery | ||
|
|
||
| A long-lived package service usually needs a second delivery plane: | ||
|
|
||
| - package app realtime sessions for human/operator UIs | ||
| - package exports or jobs for reusable downstream logic | ||
|
|
||
| Keep connection ingest, downstream formatting, and UI concerns separate so the | ||
| service can stay focused on connection lifecycle and event normalization. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.