@@ -49,7 +49,7 @@ import { EffectLogger } from "@/effect" |
49 | 49 | import { InstanceState } from "@/effect" |
50 | 50 | import { TaskTool, type TaskPromptOps } from "@/tool/task" |
51 | 51 | import { SessionRunState } from "./run-state" |
52 | | -import { SessionAutobestObserver, shouldContinue as autobestShouldContinue } from "./autobest-observer" |
| 52 | +import { SessionAutobestObserver } from "./autobest-observer" |
53 | 53 | import { AdaptiveHooks } from "./adaptive" |
54 | 54 | import { SessionMemoryObserver } from "./memory-observer" |
55 | 55 | import { dequeueEnrichmentBlock } from "@/memory/turn-hooks" |
@@ -78,24 +78,14 @@ export namespace SessionPrompt { |
78 | 78 | const elog = EffectLogger.create({ service: "session.prompt" }) |
79 | 79 |
|
80 | 80 | /** |
81 | | - * Module-level per-session autobest cycle state. |
82 | | - * Tracks iteration count and last stepKind to gate the auto-continue loop. |
83 | | - * Reset on explicit user prompt via `prompt()` so each user-initiated turn |
84 | | - * starts a fresh cycle. |
85 | | - * |
86 | | - * NB: round-1 uses a Map for simplicity. Round-2 should migrate this into |
87 | | - * durable history events (cf. `docs/codex-rs-migration-plan.md` §3.2). |
88 | | - */ |
89 | | - export const autobestCycleState = new Map<string, { iteration: number; stepKind: "a" | "b" | "c" | "d" }>() |
90 | | - export const DEFAULT_AUTOBEST_MAX_ITERATIONS = 3 |
91 | | - /** |
92 | | - * Session ids with an in-flight autobest continuation. |
93 | | - * When `prompt()` sees the sessionID is NOT in this set, the incoming prompt |
94 | | - * is treated as user-initiated and resets the cycle counter. When it IS in |
95 | | - * the set, we leave the cycle counter alone and remove the entry so the next |
96 | | - * `prompt()` call resets again. |
| 81 | + * Round-4 consolidation (§3.2 #12) retired the module-level |
| 82 | + * `autobestCycleState` / `autobestPendingContinuations` maps and the |
| 83 | + * inline extract-and-continue block in `runLoop`. Cycle state now lives |
| 84 | + * in durable `autobest.cycle.advance` / `autobest.cycle.reset` history |
| 85 | + * events managed by the autobest observer. Per-cycle AdaptiveState |
| 86 | + * reset at user-message boundary is handled by `adaptive.resetCycleFor` |
| 87 | + * in the `prompt()` entrypoint. |
97 | 88 | */ |
98 | | - export const autobestPendingContinuations = new Set<string>() |
99 | 89 |
|
100 | 90 | /** |
101 | 91 | * Default per-hook timeout (ms) for {@link runStopHook} when the config |
@@ -1469,16 +1459,14 @@ NOTE: At any point in time through this workflow you should feel free to ask the |
1469 | 1459 |
|
1470 | 1460 | const prompt: (input: PromptInput) => Effect.Effect<MessageV2.WithParts> = Effect.fn("SessionPrompt.prompt")( |
1471 | 1461 | function* (input: PromptInput) { |
1472 | | - // Reset autobest cycle on user-initiated prompts (not autobest continuations). |
1473 | | - if (autobestPendingContinuations.has(input.sessionID)) { |
1474 | | - autobestPendingContinuations.delete(input.sessionID) |
1475 | | - } else { |
1476 | | - autobestCycleState.delete(input.sessionID) |
1477 | | - // Fresh user-message boundary — reset per-cycle AdaptiveState |
1478 | | - // counters. Preserves cross-cycle scratch state such as the |
1479 | | - // injectCount diagnostic counter. |
1480 | | - yield* adaptive.resetCycleFor(input.sessionID) |
1481 | | - } |
| 1462 | + // Fresh user-message boundary — reset per-cycle AdaptiveState |
| 1463 | + // counters. Preserves cross-cycle scratch state such as the |
| 1464 | + // injectCount diagnostic counter. Post round-4 consolidation, |
| 1465 | + // autobest auto-continuations re-enter the loop via the |
| 1466 | + // AdaptiveHooks `Inject` directive (synthetic user message), |
| 1467 | + // which does NOT call `prompt()` — so this reset only fires on |
| 1468 | + // truly user-initiated turns as intended. |
| 1469 | + yield* adaptive.resetCycleFor(input.sessionID) |
1482 | 1470 |
|
1483 | 1471 | const session = yield* sessions.get(input.sessionID) |
1484 | 1472 | yield* revert.cleanup(session) |
@@ -1523,6 +1511,20 @@ NOTE: At any point in time through this workflow you should feel free to ask the |
1523 | 1511 | .ensureRegistered() |
1524 | 1512 | .pipe(Effect.catchCause(() => Effect.void)) |
1525 | 1513 |
|
| 1514 | + // Lazy-register the autobest post-iteration observer. Consolidated |
| 1515 | + // in round-4 from the former inline `SessionAutobestObserver.extract` |
| 1516 | + // block that lived here — autobest cycle progression (Step A → D), |
| 1517 | + // grounding dispatch, and candidate persistence now all happen in |
| 1518 | + // the registered observer's `postIteration` callback. Already- |
| 1519 | + // resolved services are provided so the ensureRegistered Effect |
| 1520 | + // is R-clean at this scope. MCP is optional (observer uses |
| 1521 | + // `Effect.serviceOption`), so we don't need to provide it here. |
| 1522 | + yield* SessionAutobestObserver.ensureRegistered().pipe( |
| 1523 | + Effect.provideService(AdaptiveHooks.Service, adaptive), |
| 1524 | + Effect.provideService(Session.Service, sessions), |
| 1525 | + Effect.catchCause(() => Effect.void), |
| 1526 | + ) |
| 1527 | + |
1526 | 1528 | while (true) { |
1527 | 1529 | yield* status.set(sessionID, { type: "busy" }) |
1528 | 1530 | yield* slog.info("loop", { step }) |
@@ -1857,66 +1859,15 @@ NOTE: At any point in time through this workflow you should feel free to ask the |
1857 | 1859 |
|
1858 | 1860 | yield* compaction.prune({ sessionID }).pipe(Effect.ignore, Effect.forkIn(scope)) |
1859 | 1861 | const out = yield* lastAssistant(sessionID) |
1860 | | - const autobestEnabled = yield* sessions.getAutobestEnabled(sessionID) |
1861 | | - if (autobestEnabled && out.info.role === "assistant") { |
1862 | | - const text = out.parts |
1863 | | - .filter((part): part is MessageV2.TextPart => part.type === "text") |
1864 | | - .map((part) => part.text.trim()) |
1865 | | - .filter(Boolean) |
1866 | | - .join("\n") |
1867 | | - const picks = SessionAutobestObserver.extract(text) |
1868 | | - if (picks.length) { |
1869 | | - const applied = yield* sessions |
1870 | | - .applyAutobest({ sessionID, candidates: picks, ts: Date.now() }) |
1871 | | - .pipe(Effect.match({ onFailure: () => null, onSuccess: (v) => v })) |
1872 | | - |
1873 | | - if (applied && applied.decision.changed && applied.decision.active) { |
1874 | | - // Auto-continue feedback loop — port of |
1875 | | - // `chatwidget.rs::on_autobest_result::submit_user_message`. |
1876 | | - // Per-session iteration is tracked in-memory and gated by shouldContinue. |
1877 | | - const prev = autobestCycleState.get(sessionID) ?? { iteration: 0, stepKind: "a" as const } |
1878 | | - const decision = autobestShouldContinue({ |
1879 | | - enabled: autobestEnabled, |
1880 | | - changed: true, |
1881 | | - activeKey: applied.decision.active.key, |
1882 | | - cycle: prev, |
1883 | | - maxIterations: DEFAULT_AUTOBEST_MAX_ITERATIONS, |
1884 | | - }) |
1885 | | - if (decision.shouldContinue) { |
1886 | | - autobestCycleState.set(sessionID, { |
1887 | | - iteration: prev.iteration + 1, |
1888 | | - stepKind: "a", |
1889 | | - }) |
1890 | | - yield* continuePrompt(sessionID, applied.decision.active.key).pipe( |
1891 | | - Effect.ignore, |
1892 | | - Effect.forkIn(scope), |
1893 | | - ) |
1894 | | - } else if (decision.reason === "max-iterations-reached") { |
1895 | | - // Reset so the next user-initiated turn starts a fresh cycle. |
1896 | | - autobestCycleState.delete(sessionID) |
1897 | | - } |
1898 | | - } |
1899 | | - } |
1900 | | - } |
| 1862 | + // Autobest extract + cycle advance + auto-continuation are now |
| 1863 | + // owned end-to-end by `SessionAutobestObserver` via the |
| 1864 | + // AdaptiveHooks `postIteration` pipeline (see ensureRegistered |
| 1865 | + // call at the top of `runLoop`). The inline duplicate block |
| 1866 | + // was removed during round-4 consolidation. |
1901 | 1867 | return out |
1902 | 1868 | }, |
1903 | 1869 | ) |
1904 | 1870 |
|
1905 | | - // Continuation submitter — schedules a synthetic user prompt carrying |
1906 | | - // the autobest top candidate. Delegates to the public `prompt()` helper |
1907 | | - // so the full run-loop pipeline (including compaction / autobest) re-runs. |
1908 | | - const continuePrompt = Effect.fn("SessionPrompt.autobestContinue")(function* ( |
1909 | | - sessionID: SessionID, |
1910 | | - text: string, |
1911 | | - ) { |
1912 | | - yield* elog.info("autobest.continue", { sessionID, text }) |
1913 | | - autobestPendingContinuations.add(sessionID) |
1914 | | - yield* prompt({ |
1915 | | - sessionID, |
1916 | | - parts: [{ type: "text" as const, text }], |
1917 | | - } as PromptInput).pipe(Effect.ignore) |
1918 | | - }) |
1919 | | - |
1920 | 1871 | const loop: (input: z.infer<typeof LoopInput>) => Effect.Effect<MessageV2.WithParts> = Effect.fn( |
1921 | 1872 | "SessionPrompt.loop", |
1922 | 1873 | )(function* (input: z.infer<typeof LoopInput>) { |
|