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
@@ -50,6 +50,9 @@ export const createNotificationTriggerRuntime = (deps) => {
error: 'Agent hit an error',
question: 'Agent needs your input',
permission: 'Agent needs permission',
goal_complete: 'Goal complete',
goal_blocked: 'Goal blocked',
goal_budget: 'Goal reached its token budget',
};
const toApnsGenericPayload = (payload) => {
@@ -272,6 +275,28 @@ export const createNotificationTriggerRuntime = (deps) => {
.join(' ');
};
// A session with an ACTIVE goal suppresses per-turn ready notifications;
// the session-goal runtime sends its own notification when the goal
// settles. Fetch failures fall through to normal notification behavior.
const hasActiveSessionGoal = async (sessionId, directory) => {
if (!sessionId) return false;
try {
const base = buildOpenCodeUrl(`/session/${encodeURIComponent(sessionId)}`, '');
const url = directory ? `${base}?directory=${encodeURIComponent(directory)}` : base;
const response = await fetch(url, {
method: 'GET',
headers: { Accept: 'application/json', ...getOpenCodeAuthHeaders() },
signal: AbortSignal.timeout(2000),
});
if (!response.ok) return false;
const session = await response.json().catch(() => null);
const goal = session?.metadata?.openchamber?.goal;
return Boolean(goal && typeof goal === 'object' && goal.status === 'active');
} catch {
return false;
}
};
const maybeSendPushForTrigger = async (payload) => {
if (!payload || typeof payload !== 'object') {
return;
@@ -301,6 +326,13 @@ export const createNotificationTriggerRuntime = (deps) => {
return;
}
// While a goal drives the session, per-turn "ready" notifications are
// noise produced by the goal loop itself — the goal's own settle
// notification (complete/blocked/budget) is the final word instead.
if (await hasActiveSessionGoal(sessionId, notificationDirectory)) {
return;
}
if (settings.notificationMode !== 'always' && getIsWindowFocused?.()) {
return;
}
@@ -644,10 +676,48 @@ export const createNotificationTriggerRuntime = (deps) => {
}
};
// Goal settle push: same fanout as the trigger paths (web-push with the
// full text; APNs with the generic per-type title and the session name as
// body, so the relay never sees content).
const sendGoalSettlePush = async ({ sessionId, directory, status, title, body }) => {
let sessionName = '';
try {
const base = buildOpenCodeUrl(`/session/${encodeURIComponent(sessionId)}`, '');
const url = directory ? `${base}?directory=${encodeURIComponent(directory)}` : base;
const response = await fetch(url, {
method: 'GET',
headers: { Accept: 'application/json', ...getOpenCodeAuthHeaders() },
signal: AbortSignal.timeout(2000),
});
if (response.ok) {
const session = await response.json().catch(() => null);
if (typeof session?.title === 'string') sessionName = session.title.trim();
}
} catch {
// Session name is presentation sugar for the mobile push — never block on it.
}
const type = status === 'complete' ? 'goal_complete' : (status === 'budgetLimited' ? 'goal_budget' : 'goal_blocked');
await fanoutPush(
{
title,
body,
tag: `goal-${sessionId}`,
data: {
url: buildSessionDeepLinkUrl(sessionId),
sessionId,
sessionName,
type,
},
},
{ requireNoSse: true },
);
};
return {
maybeSendPushForTrigger,
setAutoAcceptSession,
setGetIsWindowFocused,
clearPendingPushBadge,
sendGoalSettlePush,
};
};