Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .context/TECHSTACK.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,18 @@
is checked again when it runs) and `get_canvas` (`customAgents`) read it. Work left for an agent
that is deleted or off goes to Doop. Routes: `/api/agents` and `/api/agents/builtin`
(`?workspace=`, members only). Tests: `customAgents.test.ts`, `customAgentsHttp.test.ts`.
- Agent identity (`shared/agentIdentity.ts`): an outside agent is its account, its MCP session and
its `agent_name`, not the name alone, so two people's "Claude" on a shared canvas, or one person's
two Claude Code sessions, are two agents with their own presence and tasks. `/mcp` is stateless,
but `mcpSessionId` hands each `initialize` a fresh `Mcp-Session-Id` that clients send back (the
SDK's stateless transport does not check it); `mcpAgentKey` hashes the three into the opaque
`agentKey` on the `Actor`, the presence (keyed and `clientId`'d by it) and the task
(`tasks.agent_key`, migration 0060). `set_status`, auto tasks, `endAgentTasks` on presence expiry
and the guidelines nudge match on it; a client that sends no session back is known by its account
alone. Resident roles carry no key and are their name. Work is still addressed by name
(`@Claude`, feedback, card pipelines): the first agent of that name to ask takes it. One owner's
same-named agents read "Claude", "Claude 2" in the presence stack (`distinctAgentNames`). Tests:
`agentIdentity.test.ts`, `agentIdentityHttp.test.ts`.
- Agent feedback (`server/agentFeedback.ts`, `shared/agentFeedback.ts`): agents talk to the Doop
team through two MCP tools, which the resident agent has under the same names. `report_issue`
(kind `issue`: `bug`, `tool_error`, `confusing`, `missing_capability`, `other`) is for where Doop
Expand Down
50 changes: 33 additions & 17 deletions server/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { MAX_PAGES, PageLimitError, store } from './store.ts'
import * as persist from './db/persist.ts'
import * as thumbs from './thumbs.ts'
import { colorFor } from '../shared/types.ts'
import { agentKeyOf, isAgentsTask, taskAgentKey } from '../shared/agentIdentity.ts'
import type { AgentBrand } from '../shared/agentBrand.ts'
import type { StackWrite } from '../shared/frameStack.ts'
import {
Expand Down Expand Up @@ -92,10 +93,9 @@ import type {
type Broadcast = (canvasId: string, msg: ServerMessage, excludeClientId?: string) => void
type AgentTouch = (
canvasId: string,
agentName: string,
agent: Pick<Actor, 'name' | 'owner' | 'brand' | 'agentKey'>,
frameId?: string | null,
status?: string | null,
identity?: Pick<Actor, 'owner' | 'brand'>,
) => void

let send: Broadcast = () => {}
Expand Down Expand Up @@ -282,15 +282,25 @@ function logActivity(canvasId: string, actor: Actor, message: string, frameId?:
}

export function resolveActor(
raw: { name?: string; kind?: string; clientId?: string; owner?: string; brand?: AgentBrand } | undefined,
raw:
| { name?: string; kind?: string; clientId?: string; owner?: string; brand?: AgentBrand; agentKey?: string }
| undefined,
): Actor {
const kind = raw?.kind === 'agent' ? 'agent' : raw?.kind === 'user' ? 'user' : 'agent'
const name = raw?.name?.trim() || (kind === 'agent' ? 'AI Agent' : 'Anonymous')
return { name, kind, color: colorFor(name), clientId: raw?.clientId, owner: raw?.owner, brand: raw?.brand }
return {
name,
kind,
color: colorFor(name),
clientId: raw?.clientId,
owner: raw?.owner,
brand: raw?.brand,
...(kind === 'agent' && raw?.agentKey ? { agentKey: raw.agentKey } : {}),
}
}

function touch(canvasId: string, actor: Actor, frameId?: string | null) {
if (actor.kind === 'agent') agentTouch(canvasId, actor.name, frameId, undefined, actor)
if (actor.kind === 'agent') agentTouch(canvasId, actor, frameId)
}

/** Refresh an agent's presence without changing its frame or status. A model
Expand All @@ -301,9 +311,10 @@ export function heartbeatAgent(canvasId: string, actor: Actor) {
touch(canvasId, actor)
}

/** Same agent identity = same name. (Owner-scoping deferred until MCP auth.) */
function sameAgent(t: { agentName: string }, actor: Actor): boolean {
return t.agentName === actor.name
/** Same agent = same identity: its key where it has one (an outside agent,
* scoped to its account and MCP session), else its name. */
function sameAgent(t: AgentTask, actor: Actor): boolean {
return isAgentsTask(t, actor)
}

/* ------------------------------------------------------------------ */
Expand Down Expand Up @@ -468,7 +479,7 @@ export function taskCanvasId(taskId: string): string | undefined {
/** Agent announces what it is working on right now (empty string clears it). */
export function setAgentStatus(canvasId: string, actor: Actor, status: string) {
const clean = status.trim()
agentTouch(canvasId, actor.name, undefined, clean || null, actor)
agentTouch(canvasId, actor, undefined, clean || null)

const list = taskLog.get(canvasId) ?? []
/* board cards stay open until explicitly completed — a status change
Expand All @@ -485,6 +496,7 @@ export function setAgentStatus(canvasId: string, actor: Actor, status: string) {
const task: AgentTask = {
id: nanoid(8),
agentName: actor.name,
...(actor.agentKey ? { agentKey: actor.agentKey } : {}),
owner: actor.owner,
color: actor.color,
status: clean,
Expand Down Expand Up @@ -1341,6 +1353,7 @@ function autoTask(canvasId: string, actor: Actor, status: string, frameId: strin
const task: AgentTask = {
id: nanoid(8),
agentName: actor.name,
...(actor.agentKey ? { agentKey: actor.agentKey } : {}),
owner: actor.owner,
color: actor.color,
status,
Expand All @@ -1362,10 +1375,13 @@ function endAutoTask(canvasId: string, actor: Actor) {
}
}

/** Close an agent's open tasks, e.g. when its presence expires. */
export function endAgentTasks(canvasId: string, agentName: string) {
/** Close an agent's open tasks, e.g. when its presence expires. Only that
* agent's: another one sharing its name keeps working. */
export function endAgentTasks(canvasId: string, agent: Pick<Actor, 'name' | 'agentKey'>) {
const agentName = agent.name
const key = agentKeyOf(agent)
for (const t of taskLog.get(canvasId) ?? []) {
if (t.agentName === agentName && !t.endedAt) {
if (t.agentName && taskAgentKey(t) === key && !t.endedAt) {
if (t.queuedBy && t.failedAt) continue
edit(t)
if (t.queuedBy) {
Expand Down Expand Up @@ -1726,7 +1742,7 @@ function noteHtmlChange(prior: PriorState, actor: Actor, tweak: boolean) {
const joins =
tweak &&
open !== undefined &&
open.actor.name === actor.name &&
agentKeyOf(open.actor) === agentKeyOf(actor) &&
open.actor.kind === actor.kind &&
now - open.lastAt < BURST_IDLE_MS
setKey(bursts, prior.id, { actor, lastAt: now })
Expand Down Expand Up @@ -2858,12 +2874,12 @@ export function patchGuideline(
task log's announce tracking. */
const guidelinesSeen = new Set<string>()

export function markGuidelinesSeen(canvasId: string, agentName: string) {
guidelinesSeen.add(`${canvasId}:${agentName}`)
export function markGuidelinesSeen(canvasId: string, agentKey: string) {
guidelinesSeen.add(`${canvasId}:${agentKey}`)
}

export function hasSeenGuidelines(canvasId: string, agentName: string): boolean {
return guidelinesSeen.has(`${canvasId}:${agentName}`)
export function hasSeenGuidelines(canvasId: string, agentKey: string): boolean {
return guidelinesSeen.has(`${canvasId}:${agentKey}`)
}

/* ------------------------------------------------------------------ */
Expand Down
1 change: 1 addition & 0 deletions server/db/migrations/0060_agent_key.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "tasks" ADD COLUMN "agent_key" text;
Loading
Loading