@sayknow-cli/coding-agent 0.3.2 → 0.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/dist/types/config/model-profiles.d.ts +2 -2
  2. package/dist/types/config/model-registry.d.ts +3 -3
  3. package/dist/types/config/models-config-schema.d.ts +0 -5
  4. package/dist/types/config/settings-schema.d.ts +78 -9
  5. package/dist/types/export/html/template.generated.d.ts +1 -1
  6. package/dist/types/skc-runtime/state-renderer.d.ts +5 -0
  7. package/dist/types/skc-runtime/ultragoal-guard.d.ts +37 -1
  8. package/dist/types/skc-runtime/ultragoal-runtime.d.ts +80 -0
  9. package/dist/types/tools/browser/tab-supervisor.d.ts +31 -0
  10. package/dist/types/tools/computer-gc.d.ts +23 -0
  11. package/dist/types/tools/cron.d.ts +36 -61
  12. package/dist/types/tools/index.d.ts +0 -1
  13. package/dist/types/tools/resource-gc.d.ts +54 -0
  14. package/dist/types/web/search/index.d.ts +1 -0
  15. package/dist/types/web/search/providers/utils.d.ts +11 -4
  16. package/package.json +7 -7
  17. package/src/cli/args.ts +0 -1
  18. package/src/cli/fast-help.ts +0 -1
  19. package/src/cli/plugin-cli.ts +1 -1
  20. package/src/cli/web-search-cli.ts +5 -0
  21. package/src/config/model-profile-activation.ts +7 -1
  22. package/src/config/model-profiles.ts +3 -4
  23. package/src/config/model-registry.ts +3 -6
  24. package/src/config/models-config-schema.ts +1 -1
  25. package/src/config/settings-schema.ts +80 -10
  26. package/src/export/html/template.generated.ts +1 -1
  27. package/src/export/html/template.js +0 -12
  28. package/src/goals/tools/goal-tool.ts +14 -1
  29. package/src/internal-urls/docs-index.generated.ts +3 -4
  30. package/src/modes/components/model-selector.ts +9 -1
  31. package/src/modes/controllers/selector-controller.ts +6 -0
  32. package/src/prompts/system/system-prompt.md +2 -2
  33. package/src/prompts/tools/cron.md +5 -3
  34. package/src/prompts/tools/read.md +1 -1
  35. package/src/sdk.ts +5 -0
  36. package/src/session/agent-session.ts +44 -0
  37. package/src/skc-runtime/state-renderer.ts +13 -0
  38. package/src/skc-runtime/ultragoal-guard.ts +167 -0
  39. package/src/skc-runtime/ultragoal-runtime.ts +221 -1
  40. package/src/tools/browser/tab-supervisor.ts +86 -2
  41. package/src/tools/computer-gc.ts +66 -0
  42. package/src/tools/computer.ts +2 -0
  43. package/src/tools/cron.ts +75 -112
  44. package/src/tools/index.ts +2 -8
  45. package/src/tools/read.ts +25 -55
  46. package/src/tools/renderers.ts +0 -2
  47. package/src/tools/resource-gc.ts +291 -0
  48. package/src/tools/ultragoal-ask-guard.ts +7 -1
  49. package/src/web/search/index.ts +1 -0
  50. package/src/web/search/providers/utils.ts +22 -5
  51. package/vendor/insane-search/MANIFEST.json +3 -1
  52. package/vendor/insane-search/engine/__init__.py +14 -0
  53. package/vendor/insane-search/engine/content_safety.py +151 -0
  54. package/vendor/insane-search/engine/fetch_chain.py +32 -0
  55. package/vendor/insane-search/engine/tests/test_u8.py +216 -0
  56. package/dist/types/tools/inspect-image-renderer.d.ts +0 -26
  57. package/dist/types/tools/inspect-image.d.ts +0 -31
  58. package/src/prompts/tools/inspect-image-system.md +0 -20
  59. package/src/prompts/tools/inspect-image.md +0 -32
  60. package/src/tools/inspect-image-renderer.ts +0 -103
  61. package/src/tools/inspect-image.ts +0 -172
@@ -20,6 +20,7 @@ import {
20
20
  import type { ModelRegistry, SkcModelAssignmentTargetId } from "../../config/model-registry";
21
21
  import {
22
22
  isAuthenticated,
23
+ kNoAuth,
23
24
  SKC_MODEL_ASSIGNMENT_TARGET_IDS,
24
25
  SKC_MODEL_ASSIGNMENT_TARGETS,
25
26
  } from "../../config/model-registry";
@@ -804,7 +805,14 @@ export class ModelSelectorComponent extends Container {
804
805
  const entries = await Promise.all(
805
806
  [...providers].map(async provider => {
806
807
  const apiKey = await this.#modelRegistry.getApiKeyForProvider(provider, this.#authSessionId);
807
- return [provider, isAuthenticated(apiKey)] as const;
808
+ // "Usable" — not "has a real API key". getApiKeyForProvider returns the
809
+ // kNoAuth ("N/A") sentinel for keyless/no-auth providers (local LLMs,
810
+ // `--auth none` custom providers), which are usable WITHOUT a key. But
811
+ // isAuthenticated() deliberately rejects kNoAuth, so using it alone would
812
+ // flag those providers unauthenticated and silently bail their presets
813
+ // into a login flow instead of applying. Treat kNoAuth as usable here,
814
+ // matching setModel()/getApiKey() which already accept it.
815
+ return [provider, isAuthenticated(apiKey) || apiKey === kNoAuth] as const;
808
816
  }),
809
817
  );
810
818
  this.#providerAuthById = new Map(entries);
@@ -51,6 +51,7 @@ import {
51
51
  setPreferredImageProvider,
52
52
  setPreferredSearchProvider,
53
53
  setSearchFallbackProviders,
54
+ setSearchHardTimeoutMs,
54
55
  } from "../../tools";
55
56
  import { setSessionTerminalTitle } from "../../utils/title-generator";
56
57
  import { AgentDashboard } from "../components/agent-dashboard";
@@ -645,6 +646,11 @@ export class SelectorController {
645
646
  );
646
647
  }
647
648
  break;
649
+ case "web_search.timeout":
650
+ if (typeof value === "number" && Number.isFinite(value) && value > 0) {
651
+ setSearchHardTimeoutMs(value * 1000);
652
+ }
653
+ break;
648
654
  case "providers.image":
649
655
  if (
650
656
  value === "auto" ||
@@ -194,9 +194,9 @@ Delegate by default for multi-file changes, refactors, new features, tests, and
194
194
  </detached-subagents>
195
195
  {{/has}}
196
196
 
197
- {{#has tools "inspect_image"}}
197
+ {{#has tools "read"}}
198
198
  <images>
199
- For image understanding, use `{{toolRefs.inspect_image}}` with a specific question instead of reading raw image metadata only.
199
+ For image understanding, call `{{toolRefs.read}}` on the image path; the image is returned inline for direct visual inspection.
200
200
  </images>
201
201
  {{/has}}
202
202
 
@@ -1,12 +1,14 @@
1
1
  Schedule a prompt to fire on a recurring cron schedule, or one-shot at the next match. Cron tasks let you re-run a prompt automatically on an interval — poll a deployment, babysit a PR, check back on a long-running build, or remind yourself to do something later in the session.
2
2
 
3
- `CronCreate` accepts a standard 5-field cron expression in your local timezone, the prompt to run, and whether the job recurs or fires once. It returns an 8-character job id you can pass to `CronDelete`. Each session can hold up to 50 scheduled tasks. Recurring tasks auto-expire 7 days after creation; one-shot tasks self-delete after firing.
3
+ Use a single `op` field to select the operation:
4
4
 
5
- `CronList` enumerates every scheduled task in the session. `CronDelete` cancels a task by id.
5
+ - `op: "create"` accepts a standard 5-field `cron_expression` in your local timezone, the `prompt` to run, and `recurring` (whether the job recurs or fires once). It returns an 8-character job id you can pass to `op: "delete"`. Each session can hold up to 50 scheduled tasks. Recurring tasks auto-expire 7 days after creation; one-shot tasks self-delete after firing.
6
+ - `op: "list"` enumerates every scheduled task in the session.
7
+ - `op: "delete"` cancels a task by `id`.
6
8
 
7
9
  ## Cron expressions
8
10
 
9
- `CronCreate` accepts 5-field cron: `minute hour day-of-month month day-of-week`. All fields support `*`, single values (`5`), steps (`*/15`), ranges (`1-5`), and comma lists (`1,15,30`). Day-of-week uses `0`/`7` for Sunday through `6` for Saturday. Extended syntax like `L`, `W`, `?`, or month/weekday names is not supported.
11
+ `op: "create"` accepts 5-field cron: `minute hour day-of-month month day-of-week`. All fields support `*`, single values (`5`), steps (`*/15`), ranges (`1-5`), and comma lists (`1,15,30`). Day-of-week uses `0`/`7` for Sunday through `6` for Saturday. Extended syntax like `L`, `W`, `?`, or month/weekday names is not supported.
10
12
 
11
13
  | Example | Meaning |
12
14
  | :------------ | :--------------------------- |
@@ -46,7 +46,7 @@ Extracts text from PDF, Word, PowerPoint, Excel, RTF, and EPUB. Notebooks (`.ipy
46
46
 
47
47
  # Images
48
48
 
49
- Reading an image path returns metadata (mime, bytes, dimensions, channels, alpha). For actual visual analysis, call `inspect_image` with the path and a question describing what to inspect.
49
+ Reading an image path returns the image itself for visual inspection by a vision-capable model.
50
50
 
51
51
  # Archives
52
52
 
package/src/sdk.ts CHANGED
@@ -142,6 +142,7 @@ import {
142
142
  setPreferredImageProvider,
143
143
  setPreferredSearchProvider,
144
144
  setSearchFallbackProviders,
145
+ setSearchHardTimeoutMs,
145
146
  type Tool,
146
147
  type ToolSession,
147
148
  WebSearchTool,
@@ -943,6 +944,10 @@ export async function createAgentSession(options: CreateAgentSessionOptions = {}
943
944
  webSearchFallback.filter(value => typeof value === "string" && isConfigurableSearchProviderId(value)),
944
945
  );
945
946
  }
947
+ const webSearchTimeout = settings.get("web_search.timeout");
948
+ if (typeof webSearchTimeout === "number" && Number.isFinite(webSearchTimeout) && webSearchTimeout > 0) {
949
+ setSearchHardTimeoutMs(webSearchTimeout * 1000);
950
+ }
946
951
 
947
952
  const imageProvider = settings.get("providers.image");
948
953
  if (
@@ -250,6 +250,7 @@ import { releaseTabsForOwner } from "../tools/browser/tab-supervisor";
250
250
  import type { CheckpointState } from "../tools/checkpoint";
251
251
  import { outputMeta, wrapToolWithMetaNotice } from "../tools/output-meta";
252
252
  import { normalizeLocalScheme, resolveToCwd } from "../tools/path-utils";
253
+ import { registerResourceGcSession } from "../tools/resource-gc";
253
254
  import { getLatestTodoPhasesFromEntries, type TodoItem, type TodoPhase } from "../tools/todo-write";
254
255
  import { ToolAbortError, ToolError } from "../tools/tool-errors";
255
256
  import { clampTimeout } from "../tools/tool-timeouts";
@@ -964,6 +965,8 @@ export class AgentSession {
964
965
  // Python execution state
965
966
  #evalAbortControllers = new Set<AbortController>();
966
967
  #evalKernelOwnerId: string;
968
+ /** Idempotent unregister handle for this session's resource-GC registration. */
969
+ #unregisterResourceGc?: () => void;
967
970
  /**
968
971
  * AsyncJobManager owned by this session (top-level only). Subagents leave
969
972
  * this undefined and **MUST NOT** dispose the global instance on teardown.
@@ -1180,6 +1183,15 @@ export class AgentSession {
1180
1183
  this.sessionManager = config.sessionManager;
1181
1184
  this.settings = config.settings;
1182
1185
  this.taskDepth = config.taskDepth ?? 0;
1186
+ // Register this session with the process-wide resource GC (idle/RSS browser-tab eviction
1187
+ // + stale screenshot cleanup). Session-keyed so concurrent sessions share one timer safely.
1188
+ const resourceGcSessionId = this.sessionManager.getSessionId();
1189
+ if (resourceGcSessionId) {
1190
+ this.#unregisterResourceGc = registerResourceGcSession({
1191
+ sessionId: resourceGcSessionId,
1192
+ settings: this.settings,
1193
+ });
1194
+ }
1183
1195
  // Power assertions are taken per turn (see #beginInFlight); nothing acquired here.
1184
1196
  this.#evalKernelOwnerId = config.evalKernelOwnerId ?? `agent-session:${Snowflake.next()}`;
1185
1197
  this.#ownedAsyncJobManager = config.ownedAsyncJobManager;
@@ -3285,6 +3297,8 @@ export class AgentSession {
3285
3297
  // browsers disconnect, headless close gracefully). Scoped by the session id the
3286
3298
  // browser tool tagged tabs with, so other live sessions' tabs are untouched.
3287
3299
  // No-op when this session opened no tabs. Failure is logged, not thrown.
3300
+ this.#unregisterResourceGc?.();
3301
+ this.#unregisterResourceGc = undefined;
3288
3302
  await releaseTabsForOwner(this.sessionManager.getSessionId()).catch((error: unknown) =>
3289
3303
  logger.warn("session dispose: releaseTabsForOwner failed", { error }),
3290
3304
  );
@@ -3324,6 +3338,8 @@ export class AgentSession {
3324
3338
  async disposeChildSubprocesses(timeoutMs = SIGNAL_TEARDOWN_TIMEOUT_MS): Promise<void> {
3325
3339
  const sessionId = this.sessionManager.getSessionId();
3326
3340
  const kernelOwnerId = this.#evalKernelOwnerId;
3341
+ this.#unregisterResourceGc?.();
3342
+ this.#unregisterResourceGc = undefined;
3327
3343
  const work = Promise.allSettled([
3328
3344
  // kill:true so a forced exit also reaps spawned-app Chrome we own (headless
3329
3345
  // always closes; connected/attached browsers only disconnect — never killed).
@@ -5231,6 +5247,19 @@ export class AgentSession {
5231
5247
  attribution: "user",
5232
5248
  timestamp: Date.now(),
5233
5249
  });
5250
+ // A live agent loop polls the steering queue at every tool/turn boundary
5251
+ // and consumes this message on its own. But when a steer is queued while no
5252
+ // loop is actively running — e.g. the session still reports busy only
5253
+ // because a finished prompt is unwinding (deferred agent_end / post-prompt
5254
+ // work) — nothing delivers it until the next explicit prompt or a
5255
+ // user-interrupt abort, so it stalls until the user presses Esc. Schedule a
5256
+ // continue so the steer is delivered promptly. A live loop (or an
5257
+ // already-drained queue) makes the scheduled continue a no-op.
5258
+ if (this.#canAutoContinueForSteer()) {
5259
+ this.#scheduleAgentContinue({
5260
+ shouldContinue: () => this.#canAutoContinueForSteer() && this.agent.hasQueuedSteering(),
5261
+ });
5262
+ }
5234
5263
  }
5235
5264
 
5236
5265
  /**
@@ -5275,6 +5304,21 @@ export class AgentSession {
5275
5304
  return last?.role === "assistant";
5276
5305
  }
5277
5306
 
5307
+ /**
5308
+ * Gate for idle / winding-down steer auto-continue. Unlike the follow-up gate
5309
+ * this checks `agent.state.isStreaming` (a live agent loop) rather than the
5310
+ * public `isStreaming` (which stays true while a finished prompt unwinds), so a
5311
+ * steer queued during the unwind window is still delivered. A live loop returns
5312
+ * false here because it polls the steering queue itself.
5313
+ */
5314
+ #canAutoContinueForSteer(): boolean {
5315
+ if (this.agent.state.isStreaming) return false;
5316
+ if (this.isRetrying) return false;
5317
+ const messages = this.agent.state.messages;
5318
+ const last = messages[messages.length - 1];
5319
+ return last?.role === "assistant";
5320
+ }
5321
+
5278
5322
  queueDeferredMessage(message: CustomMessage): void {
5279
5323
  this.#queueHiddenNextTurnMessage(message, true);
5280
5324
  }
@@ -256,6 +256,11 @@ export function renderUltragoalStatusMarkdown(summary: {
256
256
  currentGoal?: { id: string; status: string; title?: string; objective?: string };
257
257
  counts: Record<string, number>;
258
258
  goals: unknown[];
259
+ nudgeBudget?: number;
260
+ nudgeCount?: number;
261
+ nudgeRemaining?: number;
262
+ nudgeGoalId?: string;
263
+ nudgeTargetKind?: string;
259
264
  }): string {
260
265
  if (!summary.exists)
261
266
  return `# ultragoal status\n\n- status: missing\n- No ultragoal plan found at ${summary.paths.goalsPath}. Run \`skc ultragoal create-goals --brief "..."\` first.\n`;
@@ -270,6 +275,14 @@ export function renderUltragoalStatusMarkdown(summary: {
270
275
  ];
271
276
  if (summary.skcObjective) lines.push(`- objective: ${summary.skcObjective}`);
272
277
  if (summary.currentGoal) lines.push(`- current: ${summary.currentGoal.id} (${summary.currentGoal.status})`);
278
+ if (summary.nudgeBudget !== undefined && summary.nudgeGoalId) {
279
+ const used = summary.nudgeCount ?? 0;
280
+ const remaining = summary.nudgeRemaining ?? Math.max(0, summary.nudgeBudget - used);
281
+ const target = summary.nudgeTargetKind ? ` target=${summary.nudgeTargetKind}` : "";
282
+ lines.push(
283
+ `- nudge: ${summary.nudgeGoalId} ${used}/${summary.nudgeBudget} used (${remaining} remaining)${target}`,
284
+ );
285
+ }
273
286
  lines.push(`- goals_path: ${summary.paths.goalsPath}`);
274
287
  if (summary.paths.ledgerPath) lines.push(`- ledger_path: ${summary.paths.ledgerPath}`);
275
288
  return `${lines.join("\n")}\n`;
@@ -8,9 +8,13 @@ import {
8
8
  hashStructuredValue,
9
9
  readUltragoalLedger,
10
10
  readUltragoalPlan,
11
+ recordUltragoalNudgeIfBudgetRemaining,
12
+ resolveUltragoalNudgeBudget,
13
+ selectUltragoalNudgeTarget,
11
14
  type UltragoalCompletionVerification,
12
15
  type UltragoalGoal,
13
16
  type UltragoalLedgerEvent,
17
+ type UltragoalNudgeSurface,
14
18
  type UltragoalPaths,
15
19
  type UltragoalPlan,
16
20
  type UltragoalReceiptKind,
@@ -494,6 +498,96 @@ export async function isUltragoalAskBlocked(cwd: string): Promise<UltragoalAskBl
494
498
  });
495
499
  }
496
500
 
501
+ const NUDGE_SURFACE_LABEL: Record<UltragoalNudgeSurface, string> = {
502
+ pause: "pausing the goal",
503
+ drop: "dropping the run",
504
+ ask: "asking the user",
505
+ premature_complete: "finishing the story early",
506
+ };
507
+
508
+ const NUDGE_SURFACE_REASON: Record<UltragoalNudgeSurface, string> = {
509
+ pause: "an active story still has resolvable work",
510
+ drop: "the aggregate run still has unfinished stories",
511
+ ask: "the decision can be resolved as durable story work",
512
+ premature_complete: "story verification is not yet satisfied",
513
+ };
514
+
515
+ /**
516
+ * Escalating per-attempt refusal text. Deliberately avoids every
517
+ * `isUltragoalBypassPrompt` trigger: no `update_goal(`, no "skip/weaken verification",
518
+ * no "mark ... complete", no "--status complete", and the word "complete" never
519
+ * appears (so the `goal` ... `complete` proximity rule cannot match).
520
+ */
521
+ export function formatUltragoalNudgeMessage(input: {
522
+ surface: UltragoalNudgeSurface;
523
+ attempt: number;
524
+ budget: number;
525
+ goalId: string;
526
+ }): string {
527
+ const label = NUDGE_SURFACE_LABEL[input.surface];
528
+ const reason = NUDGE_SURFACE_REASON[input.surface];
529
+ return [
530
+ `Ultragoal try-harder nudge (${input.attempt}/${input.budget}) for ${input.goalId}: ${label} was refused before the normal gate.`,
531
+ `Resolving this is part of the goal, not a reason to stop. Try a different approach first: inspect the failure, run a focused test or replay, find local credentials/config if access is the blocker, split the obstacle with \`skc ultragoal steer --kind add_subgoal\`, delegate an executor, or record concrete review blockers.`,
532
+ `Reason: ${reason}.`,
533
+ ].join("\n");
534
+ }
535
+
536
+ /**
537
+ * Consume one nudge for a guarded give-up attempt. MUST only be called from assert/
538
+ * consume paths, never from read-style `is...Blocked` diagnostics. Returns a nudge
539
+ * message while the per-story budget remains; otherwise reports not-nudged so the
540
+ * caller falls through to today's gate.
541
+ */
542
+ async function consumeUltragoalNudge(input: {
543
+ cwd: string;
544
+ surface: UltragoalNudgeSurface;
545
+ currentGoal?: CurrentGoalLike | null;
546
+ sessionId?: string | null;
547
+ }): Promise<{ nudged: true; message: string } | { nudged: false }> {
548
+ const sessionId = input.sessionId?.trim() || (await ultragoalReadPaths(input.cwd)).sessionId;
549
+ if (!sessionId) return { nudged: false };
550
+ const plan = await readUltragoalPlan(input.cwd, sessionId);
551
+ if (!plan) return { nudged: false };
552
+ const target = selectUltragoalNudgeTarget(plan, { currentGoalObjective: input.currentGoal?.objective });
553
+ if (!target) return { nudged: false };
554
+ const { budget } = await resolveUltragoalNudgeBudget(input.cwd);
555
+ const outcome = await recordUltragoalNudgeIfBudgetRemaining({
556
+ cwd: input.cwd,
557
+ sessionId,
558
+ target,
559
+ surface: input.surface,
560
+ budget,
561
+ reason: NUDGE_SURFACE_REASON[input.surface],
562
+ ...(input.currentGoal?.objective ? { currentGoalObjective: input.currentGoal.objective } : {}),
563
+ });
564
+ if (outcome.nudged) {
565
+ return {
566
+ nudged: true,
567
+ message: formatUltragoalNudgeMessage({
568
+ surface: input.surface,
569
+ attempt: outcome.attempt,
570
+ budget: outcome.budget,
571
+ goalId: outcome.goalId,
572
+ }),
573
+ };
574
+ }
575
+ return { nudged: false };
576
+ }
577
+
578
+ /**
579
+ * Assert-path entry for the `ask` surface (the ask guard lives in another module).
580
+ * Resolves the active (leader) Ultragoal session so subagent/headless asks consume
581
+ * the leader run's budget rather than a fresh child ledger.
582
+ */
583
+ export async function consumeUltragoalAskNudge(
584
+ cwd: string,
585
+ sessionId?: string | null,
586
+ ): Promise<{ nudged: true; message: string } | { nudged: false }> {
587
+ if (!cwd) return { nudged: false };
588
+ return consumeUltragoalNudge({ cwd, surface: "ask", sessionId });
589
+ }
590
+
497
591
  export async function assertCanCompleteCurrentGoal(input: {
498
592
  cwd: string;
499
593
  currentGoal?: CurrentGoalLike | null;
@@ -502,6 +596,13 @@ export async function assertCanCompleteCurrentGoal(input: {
502
596
  if (!input.cwd) return;
503
597
  const diagnostic = await readUltragoalVerificationState(input);
504
598
  if (["inactive", "unrelated_goal", "active_verified_complete"].includes(diagnostic.state)) return;
599
+ const nudge = await consumeUltragoalNudge({
600
+ cwd: input.cwd,
601
+ surface: "premature_complete",
602
+ currentGoal: input.currentGoal,
603
+ sessionId: input.sessionId,
604
+ });
605
+ if (nudge.nudged) throw new Error(nudge.message);
505
606
  throw new Error(
506
607
  `${diagnostic.message} Run strict \`skc ultragoal checkpoint --status complete --quality-gate-json <file> --skc-goal-json <file>\` first, or record review blockers and rerun verification.`,
507
608
  );
@@ -558,6 +659,10 @@ export async function isUltragoalPauseBlocked(cwd: string): Promise<UltragoalPau
558
659
  }
559
660
 
560
661
  export async function assertUltragoalPauseAllowed(cwd: string): Promise<void> {
662
+ if (cwd) {
663
+ const nudge = await consumeUltragoalNudge({ cwd, surface: "pause" });
664
+ if (nudge.nudged) throw new Error(nudge.message);
665
+ }
561
666
  const diagnostic = await isUltragoalPauseBlocked(cwd);
562
667
  if (!diagnostic.blocked) return;
563
668
  throw new Error(
@@ -568,3 +673,65 @@ export async function assertUltragoalPauseAllowed(cwd: string): Promise<void> {
568
673
  ].join("\n"),
569
674
  );
570
675
  }
676
+ /**
677
+ * Guard `goal({"op":"drop"})` during an active Ultragoal run. A *real give-up* (an
678
+ * aggregate run still mid-flight with incomplete required stories) is nudged while
679
+ * budget remains; once exhausted it falls through to today's drop behavior. Legitimate
680
+ * aggregate-reset drops — no durable run, unrelated goal, an already dropped/stale
681
+ * aggregate, or an all-stories-complete run — are never nudged. If durable state
682
+ * exists but cannot be read to classify the drop, fail closed.
683
+ */
684
+ export async function assertUltragoalDropAllowed(input: {
685
+ cwd: string;
686
+ currentGoal?: CurrentGoalLike | null;
687
+ sessionId?: string | null;
688
+ }): Promise<void> {
689
+ if (!input.cwd) return;
690
+ let paths: UltragoalPaths;
691
+ let sessionId: string | null;
692
+ try {
693
+ ({ paths, sessionId } = await ultragoalReadPaths(input.cwd));
694
+ } catch (error) {
695
+ throw new Error(
696
+ `Unable to classify Ultragoal drop (durable state unreadable): ${error instanceof Error ? error.message : String(error)}`,
697
+ );
698
+ }
699
+ if (sessionId === null) return;
700
+ try {
701
+ await fs.stat(paths.dir);
702
+ } catch (error) {
703
+ if (isEnoent(error)) return;
704
+ throw new Error(
705
+ `Unable to classify Ultragoal drop (durable state present but unreadable): ${error instanceof Error ? error.message : String(error)}`,
706
+ );
707
+ }
708
+ let plan: UltragoalPlan | null;
709
+ try {
710
+ plan = await readUltragoalPlan(input.cwd, sessionId);
711
+ } catch (error) {
712
+ throw new Error(
713
+ `Unable to classify Ultragoal drop (goals.json unreadable): ${error instanceof Error ? error.message : String(error)}`,
714
+ );
715
+ }
716
+ if (!plan) {
717
+ throw new Error("Unable to classify Ultragoal drop: durable state exists but goals.json is missing or empty.");
718
+ }
719
+ // Out of scope: per-story mode drops keep today's behavior.
720
+ if (plan.skcGoalMode !== "aggregate") return;
721
+ // A real give-up requires an active aggregate goal to actually be abandoned. With no
722
+ // current goal-mode goal (or a non-active one), `drop` is a no-op/reset before a fresh
723
+ // `create`, never a give-up — so it is left un-nudged.
724
+ if (!input.currentGoal) return;
725
+ if (input.currentGoal.status !== "active") return;
726
+ // Unrelated active goal: not this aggregate run.
727
+ if (!objectiveMatches(input.currentGoal.objective, plan)) return;
728
+ // All required stories complete: a legitimate reset, not a give-up.
729
+ if (getUltragoalRunCompletionState(plan).allComplete) return;
730
+ const nudge = await consumeUltragoalNudge({
731
+ cwd: input.cwd,
732
+ surface: "drop",
733
+ currentGoal: input.currentGoal,
734
+ sessionId,
735
+ });
736
+ if (nudge.nudged) throw new Error(nudge.message);
737
+ }