103 lines · 3.8 KBRaw
| 1 | /** |
| 2 | * Product direction, declared so the rest of the contract leaves room for it. |
| 3 | * Nothing in this module is implemented yet. |
| 4 | * |
| 5 | * - Agents connect from a terminal (or run inside the platform), work in their |
| 6 | * own branch, and stream their reasoning so humans can watch live. |
| 7 | * - Every commit, thought and decision is a node in a code graph; agents can be started |
| 8 | * from any node, and humans resolve agent work by deciding, not by rewriting it. |
| 9 | */ |
| 10 | import type { AgentIdentity, Attribution } from "./actor.ts"; |
| 11 | import type { Id, Outcome, Page, PageRequest, Timestamp } from "./common.ts"; |
| 12 | import type { Refs } from "./handles.ts"; |
| 13 | import type { PersonRef } from "./views.ts"; |
| 14 | |
| 15 | export type SessionStatus = "running" | "waiting" | "finished" | "failed" | "stale"; |
| 16 | |
| 17 | export interface AgentSessionSummary { |
| 18 | readonly id: Id; |
| 19 | readonly agent: AgentIdentity; |
| 20 | readonly onBehalfOf: PersonRef; |
| 21 | readonly repo: string; |
| 22 | /** Agents push only to their own branch, never to the default branch. */ |
| 23 | readonly branch: string; |
| 24 | readonly status: SessionStatus; |
| 25 | readonly startedAt: Timestamp; |
| 26 | readonly endedAt: Timestamp | null; |
| 27 | } |
| 28 | |
| 29 | export type TraceKind = "thought" | "tool" | "message" | "commit" | "resolution"; |
| 30 | |
| 31 | export interface TraceNode { |
| 32 | readonly id: Id; |
| 33 | readonly parent: Id | null; |
| 34 | readonly kind: TraceKind; |
| 35 | readonly body: string; |
| 36 | readonly commit: string | null; |
| 37 | readonly at: Timestamp; |
| 38 | readonly by: Attribution; |
| 39 | } |
| 40 | |
| 41 | export interface StartSession { |
| 42 | readonly runtime: string; |
| 43 | readonly repo: string; |
| 44 | /** Start from a branch, or from any node in the code graph. */ |
| 45 | readonly from: { readonly ref: string } | { readonly node: Id }; |
| 46 | } |
| 47 | |
| 48 | export interface AgentSessions { |
| 49 | /** `token` lets the agent push to its branch and stream its trace; it is scoped to this one session. */ |
| 50 | start(input: StartSession): Promise<Outcome<{ readonly session: AgentSessionSummary; readonly token: string }>>; |
| 51 | list(filter: { readonly branch?: string; readonly status?: SessionStatus }): Promise<AgentSessionSummary[]>; |
| 52 | session(id: Id): Promise<AgentSession>; |
| 53 | } |
| 54 | |
| 55 | export interface AgentSession { |
| 56 | readonly summary: AgentSessionSummary; |
| 57 | trace(page?: PageRequest): Promise<Page<TraceNode>>; |
| 58 | /** Live updates while the session runs. */ |
| 59 | watch(): Promise<ReadableStream<TraceNode>>; |
| 60 | /** Only the session's own agent may append to its trace. */ |
| 61 | emit(nodes: readonly Omit<TraceNode, "id" | "at" | "by">[]): Promise<Outcome>; |
| 62 | end(outcome: "finished" | "failed"): Promise<Outcome>; |
| 63 | } |
| 64 | |
| 65 | export type Verdict = |
| 66 | | { readonly kind: "approve" } |
| 67 | | { readonly kind: "reject"; readonly reason: string } |
| 68 | | { readonly kind: "redirect"; readonly instruction: string }; |
| 69 | |
| 70 | export interface Review { |
| 71 | readonly session: AgentSessionSummary; |
| 72 | /** Approving lands the agent's branch; redirecting starts a new session from the same point with the instruction. */ |
| 73 | decide(verdict: Verdict): Promise<Outcome>; |
| 74 | } |
| 75 | |
| 76 | export type GraphNodeKind = "commit" | "trace" | "decision"; |
| 77 | |
| 78 | export interface GraphNode { |
| 79 | readonly id: Id; |
| 80 | readonly kind: GraphNodeKind; |
| 81 | readonly summary: string; |
| 82 | readonly by: Attribution; |
| 83 | readonly at: Timestamp; |
| 84 | readonly parents: readonly Id[]; |
| 85 | } |
| 86 | |
| 87 | export interface Graph { |
| 88 | node(id: Id): Promise<GraphNode>; |
| 89 | around(commit: string, depth?: number): Promise<{ readonly nodes: readonly GraphNode[]; readonly focus: Id }>; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Capabilities only a human holds. An agent can read the graph and work in its session, |
| 94 | * but it can never start other agents or decide on agent work, its own included. |
| 95 | */ |
| 96 | export interface HumanRefs extends Refs { |
| 97 | readonly agents: AgentSessions; |
| 98 | review(session: Id): Promise<Review>; |
| 99 | graph(repo: string): Promise<Graph>; |
| 100 | } |
| 101 | |
| 102 | /** Narrows a handle to its human capabilities, or returns null for agents and anonymous actors. */ |
| 103 | export type AsHuman = (refs: Refs) => HumanRefs | null; |