export type GoalStatus = | 'active' | 'paused' | 'blocked' | 'usage_limited' | 'budget_limited' | 'ready_for_acceptance' | 'accepted' | 'cancelled'; export type GoalAttemptStatus = | 'running' | 'stopped' | 'failed' | 'verified' | 'superseded'; export interface GoalScope { workspaceId: string; sessionId: string; } export type GoalSource = | { kind: 'standalone' } | { kind: 'group-task'; groupId: string; taskId: string; assigneeAgentId?: string; parentTaskId?: string; }; export interface GoalContractInput { objective: string; deliverables?: string[] | null; acceptanceCriteria?: string[] | null; scope?: string | null; sourceRefs?: string[] | null; confirmedBy?: string; /** 见 GoalContract.tokenBudget。`null` / 缺省 = 不限。 */ tokenBudget?: number | null; } export interface GoalContract { version: number; objective: string; deliverables: string[]; acceptanceCriteria: string[]; scope?: string; sourceRefs: string[]; confirmedBy: string; confirmedAt: number; hash: string; /** * 本 Goal 允许消耗的 token 上限(input + output 之和)。`null` = 不限(默认)。 * * 语义边界,改之前先读: * - 它**只在开新一轮之前**被求值。进行中的 TAOR 循环即使超额也一路跑完到节点 —— * 这是产品决策:宁可超一点预算,也不能把 agent 停在半成品状态。 * - 它挂在**版本化的契约**上,而 `Goal.usage` 挂在 Goal 上(累计、不分契约版本)。 * 改预算 = 新契约版本,**已消耗量不重置** → 「加预算继续跑」是自然操作。 * - 与 `usage_limited` 的分工:这个是**用户自己设的额度**用完了; * `usage_limited` 是**服务端配额/授权**不给了。两者都可 resume,但恢复动作不同。 */ tokenBudget?: number | null; } export interface GoalAttempt { attemptId: string; contractVersion: number; workflow: string; status: GoalAttemptStatus; startedAt: number; finishedAt?: number; artifacts: string[]; evidence: string[]; blocker?: string; } export interface GoalResourceUsage { inputTokens: number; outputTokens: number; elapsedMs: number; } export interface Goal { goalId: string; scope: GoalScope; source: GoalSource; status: GoalStatus; revision: number; contracts: GoalContract[]; currentContractVersion: number; attempts: GoalAttempt[]; createdBy: string; acceptorHumanIds: string[]; createdAt: number; updatedAt: number; usage: GoalResourceUsage; statusReason?: string; } export type GoalEvent = | { type: 'goal.created'; eventId: string; at: number; goalId: string; contract: GoalContract; source: GoalSource; createdBy: string; acceptorHumanIds: string[]; revision?: number; } | { type: 'goal.contract_revised'; eventId: string; at: number; goalId: string; contract: GoalContract; revision?: number; } | { type: 'goal.status_changed'; eventId: string; at: number; goalId: string; from: GoalStatus; to: GoalStatus; reason?: string; actorId?: string; revision?: number; } | { type: 'goal.attempt_started'; eventId: string; at: number; goalId: string; attempt: GoalAttempt; revision?: number; } | { type: 'goal.attempt_finished'; eventId: string; at: number; goalId: string; attemptId: string; status: GoalAttemptStatus; artifacts: string[]; evidence: string[]; blocker?: string; revision?: number; } | { type: 'goal.resources_recorded'; eventId: string; at: number; goalId: string; usage: Partial; revision?: number; }; export interface GoalExecutionContext { goalId: string; contractVersion: number; attemptId: string; groupId?: string; taskId?: string; contract: GoalContract; } export const TERMINAL_GOAL_STATUSES = new Set([ 'accepted', 'cancelled', ]); export function isGoalFinished(status: GoalStatus): boolean { return TERMINAL_GOAL_STATUSES.has(status); }