feat: session goals - server-driven goal loop with independent small-model audit (#2148)

Arm the target button in the composer and the next prompt becomes a goal:
the server keeps the session working toward it (idle tick -> small-model
audit -> continuation) until the objective is verifiably complete, blocked,
or out of budget — even with the UI closed.

Server (packages/web/server/lib/session-goal):
- event-driven loop on the global SSE hub; goal state lives in
  session.metadata.openchamber.goal (merge-safe patches, stale-write guard
  by goal id), so it survives restarts and syncs to every client for free
- the small-model audit (objective + last assistant turn only, language
  pinned to the objective) is the sole termination authority; blocked needs
  3 consecutive verdicts, audit outages tolerate one unaudited continuation
  then stop the goal as resumable-blocked
- hard stops: optional token budget, auto-continuation cap (Resume grants a
  fresh allowance), turn errors; user abort pauses the goal instead of
  blocking it, and resuming over an aborted tail nudges immediately
- token accounting as a snapshot of the latest turn (input + cache.read +
  output), goal-relative via a creation baseline and segmented across
  compactions; a compaction summary skips the audit and continues
- continuations reuse the session's own provider/model/agent/variant

UI:
- three-mode target button (arm / disarm / manage dialog), informational
  goal strip with inline pause/resume and an Evaluating indicator, sidebar
  state glyph, objective length counter (2000-char server clamp),
  read-only completed goals
- goal entry points: composer (sessions and drafts), start-new-session-
  from-answer dialog, plan implement dialog (plan content becomes the
  objective), scheduled tasks (Run as goal + budget)
- Settings -> Chat -> Goal: feature toggle + default token budget with
  three-layer parity (web server, client persistence, VS Code bridge);
  VS Code renders goal state but hides the entry points (the loop runs in
  the web server only)

Notifications: per-turn "ready" notifications are suppressed while a goal
is active; settling sends one final notification (desktop, web-push, APNs
generic titles with the session name as body) honoring the completion
toggle. Error/question/permission notifications are untouched.

Docs: user guide (session-goals) in all 9 locales + sidebar entry,
scheduled-tasks cross-reference, server module DOCUMENTATION.md.
This commit is contained in:
Bohdan Triapitsyn
2026-07-12 01:23:22 +03:00
committed by GitHub
parent 82c039117a
commit bb45164ae8
73 changed files with 3330 additions and 29 deletions
@@ -0,0 +1,74 @@
import type { Session } from '@opencode-ai/sdk/v2';
// Session goal driven by the server's session-goal runtime, stored under
// session.metadata.openchamber.goal. The UI writes goals (create/edit/
// pause/resume/clear) by patching this metadata; the server loop accounts
// usage, audits progress with the small model, and auto-continues the
// session until the goal settles.
export type SessionGoalStatus = 'active' | 'paused' | 'blocked' | 'budgetLimited' | 'complete';
const SESSION_GOAL_STATUSES: SessionGoalStatus[] = ['active', 'paused', 'blocked', 'budgetLimited', 'complete'];
export const SESSION_GOAL_OBJECTIVE_CHAR_LIMIT = 2000;
export interface SessionGoalPayload {
id: string;
objective: string;
status: SessionGoalStatus;
tokenBudget: number | null;
tokensUsed: number;
turnsUsed: number;
blockedStreak: number;
note: string;
statusReason: string;
lastAccountedMessageID: string;
createdAt: number;
updatedAt: number;
}
const isRecord = (value: unknown): value is Record<string, unknown> =>
Boolean(value) && typeof value === 'object' && !Array.isArray(value);
const isGoalStatus = (value: unknown): value is SessionGoalStatus =>
typeof value === 'string' && (SESSION_GOAL_STATUSES as string[]).includes(value);
export function getSessionGoal(session: Session | null | undefined): SessionGoalPayload | null {
const metadata = (session as { metadata?: unknown } | null | undefined)?.metadata;
if (!isRecord(metadata)) return null;
const namespace = metadata.openchamber;
if (!isRecord(namespace)) return null;
const goal = namespace.goal;
if (!isRecord(goal)) return null;
const id = typeof goal.id === 'string' ? goal.id : '';
const objective = typeof goal.objective === 'string' ? goal.objective.trim() : '';
if (!id || !objective || !isGoalStatus(goal.status)) return null;
const tokenBudget = typeof goal.tokenBudget === 'number' && Number.isFinite(goal.tokenBudget) && goal.tokenBudget > 0
? Math.floor(goal.tokenBudget)
: null;
const asCount = (value: unknown): number =>
typeof value === 'number' && Number.isFinite(value) && value > 0 ? Math.floor(value) : 0;
return {
id,
objective: objective.slice(0, SESSION_GOAL_OBJECTIVE_CHAR_LIMIT),
status: goal.status,
tokenBudget,
tokensUsed: asCount(goal.tokensUsed),
turnsUsed: asCount(goal.turnsUsed),
blockedStreak: asCount(goal.blockedStreak),
note: typeof goal.note === 'string' ? goal.note : '',
statusReason: typeof goal.statusReason === 'string' ? goal.statusReason : '',
lastAccountedMessageID: typeof goal.lastAccountedMessageID === 'string' ? goal.lastAccountedMessageID : '',
createdAt: typeof goal.createdAt === 'number' ? goal.createdAt : 0,
updatedAt: typeof goal.updatedAt === 'number' ? goal.updatedAt : 0,
};
}
export function formatGoalTokens(count: number): string {
if (count >= 1_000_000) return `${(count / 1_000_000).toFixed(1)}M`;
if (count >= 10_000) return `${Math.round(count / 1000)}K`;
if (count >= 1_000) return `${(count / 1000).toFixed(1)}K`;
return String(count);
}