Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

refactor(community): route ws-do broadcasts through WS_EVENTS + extend lint gate (#5 T2)

Melly0c88842043f0
diff --git a/scripts/lint-ws-events.mjs b/scripts/lint-ws-events.mjs
index 4f604934c..f49a99a02 100755
--- a/scripts/lint-ws-events.mjs
+++ b/scripts/lint-ws-events.mjs
@@ -3,8 +3,9 @@
* Guardrail lint for community WS events.
*
* 1. `type: "community:*"` hardcoded literals under
- * `src/web/src/app/api/community` — should be `WS_EVENTS.*` from
- * `@alook/shared` so a rename fires a typecheck error at the send site.
+ * `src/web/src/app/api/community` and `src/ws-do/src` (the Durable Object
+ * that emits presence/machine/typing broadcasts) — should be `WS_EVENTS.*`
+ * from `@alook/shared` so a rename fires a typecheck error at the send site.
*
* 2. `as never` inside any `broadcastToUser*` / `fanOutTo*` call under
* `src/web/src` — defeats the discriminated `CommunityWsEvent` union.
@@ -19,7 +20,7 @@ import { readFileSync } from "node:fs"
const ROOT = new URL("..", import.meta.url).pathname.replace(/\/$/, "")
const PATTERN_LITERAL = 'type:\\s*"community:'
-const LITERAL_PATHS = ["src/web/src/app/api/community"]
+const LITERAL_PATHS = ["src/web/src/app/api/community", "src/ws-do/src"]
const PATTERN_AS_NEVER =
"(broadcastToUser(?:Safe)?|fanOutToChannel|fanOutToDM|fanOutToServerMembers)\\([^)]*as never"
diff --git a/src/ws-do/src/index.test.ts b/src/ws-do/src/index.test.ts
index 327e24e42..166d239f3 100644
--- a/src/ws-do/src/index.test.ts
+++ b/src/ws-do/src/index.test.ts
@@ -16,6 +16,12 @@ const mockGetActiveDoNamesForMachine = vi.fn(async (_db: unknown, _machineId: st
vi.mock("@alook/shared", () => {
const noopLogger = { debug: () => { }, info: () => { }, warn: () => { }, error: () => { }, child() { return this } }
return {
+ // Real WS event-type strings the DO reads at runtime (#5 T2 — ws-do
+ // broadcasts now use WS_EVENTS.* instead of raw literals). Only the keys
+ // this suite exercises are needed; values match @alook/shared.
+ WS_EVENTS: {
+ BOT_AUDIT_EVENT: "community:bot.audit_event",
+ },
createDb: () => ({}),
createLogger: () => noopLogger,
queries: {
diff --git a/src/ws-do/src/index.ts b/src/ws-do/src/index.ts
index 7b17f42d1..83757f7af 100644
--- a/src/ws-do/src/index.ts
+++ b/src/ws-do/src/index.ts
@@ -1,4 +1,4 @@
-import { createLogger, queries } from "@alook/shared"
+import { createLogger, queries, WS_EVENTS } from "@alook/shared"
export { WebSocketDurableObject } from "./ws-durable"
export { RateLimitDurableObject } from "./rate-limit-do"
@@ -124,7 +124,7 @@ export default {
return new Response("invalid kind", { status: 400 })
}
const frame = {
- type: "community:bot.audit_event",
+ type: WS_EVENTS.BOT_AUDIT_EVENT,
botId: b.botId,
id: b.id,
kind: b.kind,
diff --git a/src/ws-do/src/ws-durable.test.ts b/src/ws-do/src/ws-durable.test.ts
index 7cbb984b4..170da86a9 100644
--- a/src/ws-do/src/ws-durable.test.ts
+++ b/src/ws-do/src/ws-durable.test.ts
@@ -233,6 +233,18 @@ vi.mock("@alook/shared", () => {
},
}
return {
+ // Real WS event-type strings the DO reads at runtime (#5 T2 — ws-do
+ // broadcasts now use WS_EVENTS.* instead of raw literals). Values match
+ // @alook/shared so the tests' `type: "community:*"` assertions still hold.
+ WS_EVENTS: {
+ TYPING_START: "community:typing.start",
+ TYPING_STOP: "community:typing.stop",
+ MACHINE_STATUS: "community:machine.status",
+ MACHINE_UPDATED: "community:machine.updated",
+ STATUS_UPDATE: "community:status.update",
+ BOT_AUDIT_EVENT: "community:bot.audit_event",
+ PRESENCE_UPDATE: "community:presence.update",
+ },
createDb: (d1: unknown) => mockCreateDb(d1),
createLogger: () => noopLogger,
// Real type-guard impls — fanOutTyping branches on these to route a
diff --git a/src/ws-do/src/ws-durable.ts b/src/ws-do/src/ws-durable.ts
index 614896c57..db8aa0b0a 100644
--- a/src/ws-do/src/ws-durable.ts
+++ b/src/ws-do/src/ws-durable.ts
@@ -17,6 +17,7 @@ import {
isBotActivityStatus,
isThread,
isForumPost,
+ WS_EVENTS,
} from "@alook/shared"
import type { CommunityMachineRuntime, CommunityMachineSummary } from "@alook/shared"
@@ -414,7 +415,7 @@ export class WebSocketDurableObject extends DurableObject<Env> {
}
// ── Community: typing.start — dedup and fan-out ─────────────────────────
- if (msg.type === "community:typing.start" && state.type === "user") {
+ if (msg.type === WS_EVENTS.TYPING_START && state.type === "user") {
const typingMsg = parsed as {
type: string
channelId?: string
@@ -457,7 +458,7 @@ export class WebSocketDurableObject extends DurableObject<Env> {
// lookup — no per-event DB. Undefined on a pre-existing connection that
// predates this field; the client falls back to roster resolution then.
const event = JSON.stringify({
- type: "community:typing.start",
+ type: WS_EVENTS.TYPING_START,
channelId: typingMsg.channelId,
userId: state.userId,
name: state.name,
@@ -511,7 +512,7 @@ export class WebSocketDurableObject extends DurableObject<Env> {
if (flipped) {
// Real transition — broadcast + clean up storage. Alarm no longer needed.
await this.notifyUserDO(identity.userId, {
- type: "community:machine.status",
+ type: WS_EVENTS.MACHINE_STATUS,
machineId: identity.machineId,
status: "offline",
lastSeenAt: flipped.lastSeenAt ?? new Date().toISOString(),
@@ -563,7 +564,7 @@ export class WebSocketDurableObject extends DurableObject<Env> {
})
if (backfilled) {
await this.notifyUserDO(identity.userId, {
- type: "community:machine.status",
+ type: WS_EVENTS.MACHINE_STATUS,
machineId: identity.machineId,
status: "online",
lastSeenAt: backfilled.lastSeenAt ?? new Date().toISOString(),
@@ -611,7 +612,7 @@ export class WebSocketDurableObject extends DurableObject<Env> {
})
if (flipped) {
await this.notifyUserDO(stored.userId, {
- type: "community:machine.status",
+ type: WS_EVENTS.MACHINE_STATUS,
machineId: stored.machineId,
status: "offline",
lastSeenAt: flipped.lastSeenAt ?? new Date().toISOString(),
@@ -627,7 +628,7 @@ export class WebSocketDurableObject extends DurableObject<Env> {
// otherwise the machine chip stays green until reload. Broadcast
// using the row's own lastSeenAt.
await this.notifyUserDO(stored.userId, {
- type: "community:machine.status",
+ type: WS_EVENTS.MACHINE_STATUS,
machineId: stored.machineId,
status: "offline",
lastSeenAt: machine.lastSeenAt ?? new Date().toISOString(),
@@ -830,7 +831,7 @@ export class WebSocketDurableObject extends DurableObject<Env> {
statusText: preset.text,
})
await this.broadcastToAudience(agentId, {
- type: "community:status.update",
+ type: WS_EVENTS.STATUS_UPDATE,
userId: agentId,
statusEmoji: preset.emoji,
statusText: preset.text,
@@ -863,7 +864,7 @@ export class WebSocketDurableObject extends DurableObject<Env> {
// binding (already read above) so the client renders the bot's name
// without a roster lookup — no per-event DB.
const event = JSON.stringify({
- type: "community:typing.start",
+ type: WS_EVENTS.TYPING_START,
channelId,
userId: agentId,
name: binding.name,
@@ -939,7 +940,7 @@ export class WebSocketDurableObject extends DurableObject<Env> {
})
if (!inserted) return
await this.notifyUserDO(binding.ownerUserId, {
- type: "community:bot.audit_event",
+ type: WS_EVENTS.BOT_AUDIT_EVENT,
botId: frame.agentId,
id: inserted.id,
kind: frame.event.kind,
@@ -999,7 +1000,7 @@ export class WebSocketDurableObject extends DurableObject<Env> {
await Promise.allSettled(
activityChanges.map(({ botUserId, statusEmoji, statusText }) =>
this.broadcastToAudience(botUserId, {
- type: "community:status.update",
+ type: WS_EVENTS.STATUS_UPDATE,
userId: botUserId,
statusEmoji,
statusText,
@@ -1026,7 +1027,7 @@ export class WebSocketDurableObject extends DurableObject<Env> {
// status='online', so `priorStatus !== 'online'` is the exact transition.
if (priorStatus !== "online") {
await this.notifyUserDO(identity.userId, {
- type: "community:machine.status",
+ type: WS_EVENTS.MACHINE_STATUS,
machineId: machine.id,
status: "online",
lastSeenAt: machine.lastSeenAt ?? new Date().toISOString(),
@@ -1040,7 +1041,7 @@ export class WebSocketDurableObject extends DurableObject<Env> {
const nextCanonical = canonicalRuntimes(availableRuntimes)
if (priorCanonical !== nextCanonical) {
await this.notifyUserDO(identity.userId, {
- type: "community:machine.updated",
+ type: WS_EVENTS.MACHINE_UPDATED,
machine: summary,
}).catch(() => { })
}
@@ -1115,7 +1116,7 @@ export class WebSocketDurableObject extends DurableObject<Env> {
if (!row) return
const summary = await this.summaryWithOverlay(row)
await this.notifyUserDO(userId, {
- type: "community:machine.updated",
+ type: WS_EVENTS.MACHINE_UPDATED,
machine: summary,
})
}
@@ -1191,7 +1192,7 @@ export class WebSocketDurableObject extends DurableObject<Env> {
private machineStatusPayload(payload: unknown): { machineId: string; online: boolean } | null {
if (typeof payload !== "object" || payload === null) return null
const p = payload as { type?: unknown; machineId?: unknown; status?: unknown }
- if (p.type !== "community:machine.status") return null
+ if (p.type !== WS_EVENTS.MACHINE_STATUS) return null
if (typeof p.machineId !== "string") return null
if (p.status !== "online" && p.status !== "offline") return null
return { machineId: p.machineId, online: p.status === "online" }
@@ -1353,7 +1354,7 @@ export class WebSocketDurableObject extends DurableObject<Env> {
recipientUserIds = recipientUserIds.filter((id) => id !== senderUserId)
if (recipientUserIds.length === 0) return
const body = JSON.stringify({
- type: "community:typing.stop",
+ type: WS_EVENTS.TYPING_STOP,
channelId,
userId: senderUserId,
})
@@ -1386,7 +1387,7 @@ export class WebSocketDurableObject extends DurableObject<Env> {
private static readonly SUBREQUEST_BATCH_SIZE = 40
private async broadcastPresence(userId: string, online: boolean): Promise<void> {
- await this.broadcastToAudience(userId, { type: "community:presence.update", userId, online })
+ await this.broadcastToAudience(userId, { type: WS_EVENTS.PRESENCE_UPDATE, userId, online })
}
/**
@@ -1470,7 +1471,7 @@ export class WebSocketDurableObject extends DurableObject<Env> {
}
}
for (const id of onlineIds) {
- ws.send(JSON.stringify({ type: "community:presence.update", userId: id, online: true }))
+ ws.send(JSON.stringify({ type: WS_EVENTS.PRESENCE_UPDATE, userId: id, online: true }))
}
}
}