Skip to content
14 changes: 14 additions & 0 deletions packages/opencode/src/session/llm/repetition-guard.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { LLMEvent } from "@opencode-ai/llm"
import { isRecord } from "@/util/record"
import { Effect, Stream } from "effect"

// SessionRetry.retryable excludes messages with this prefix, so a tripped
Expand Down Expand Up @@ -58,6 +59,19 @@ export class RepetitionDetectedError extends Error {
}
}

// Stable identity for a tripped guard on the persisted assistant message.
// MessageV2.fromError records this marker in UnknownError.data.ref; the error
// keeps its name and message text, so every existing renderer and the retry
// exclusion see exactly what they saw before. Recovery reads the marker instead
// of matching REPETITION_MESSAGE, because a provider HTTP error can quote the
// guard text while being a different failure entirely (issue #738).
export const ERROR_REF = "repetition-guard"

/** True when a persisted `Assistant["error"]` is the harness's own guard abort. */
export function isStoredError(error: unknown): boolean {
return isRecord(error) && error.name === "UnknownError" && isRecord(error.data) && error.data.ref === ERROR_REF
}

type GuardOptions = {
readonly thresholds: Thresholds
}
Expand Down
32 changes: 26 additions & 6 deletions packages/opencode/src/session/message-v2.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { SessionID, MessageID } from "./schema"
import { RepetitionGuard } from "./llm/repetition-guard"
import { SessionV1 } from "@opencode-ai/core/v1/session"
import { ProviderV2 } from "@opencode-ai/core/provider"
import {
Expand Down Expand Up @@ -68,10 +69,9 @@ type Cursor = typeof Cursor.Type

const decodeCursor = Schema.decodeUnknownSync(Cursor)

export class InvalidCursorError extends Schema.TaggedErrorClass<InvalidCursorError>()(
"MessageV2.InvalidCursorError",
{ message: Schema.String },
) {
export class InvalidCursorError extends Schema.TaggedErrorClass<InvalidCursorError>()("MessageV2.InvalidCursorError", {
message: Schema.String,
}) {
static isInstance(input: unknown): input is InvalidCursorError {
return input instanceof InvalidCursorError
}
Expand Down Expand Up @@ -263,8 +263,18 @@ export const toModelMessagesEffect = Effect.fnUntraced(function* (
if (
msg.info.error &&
!(
AbortedError.isInstance(msg.info.error) &&
msg.parts.some((part) => part.type !== "step-start" && part.type !== "reasoning")
(AbortedError.isInstance(msg.info.error) &&
msg.parts.some((part) => part.type !== "step-start" && part.type !== "reasoning")) ||
// Issue #738: a guard abort is a deliberate cutoff of a generation,
// not evidence that its work should disappear. The parts persisted
// before the abort (streamed text and any tool call that already
// completed) stay visible to the recovery request, so the model can
// build on what happened instead of re-issuing a finished call.
// Replaying this transcript state never re-executes a tool: completed
// calls are presented with their recorded output, and calls still
// pending/running keep their existing "[Tool execution was
// interrupted]" rendering below.
RepetitionGuard.isStoredError(msg.info.error)
)
) {
continue
Expand Down Expand Up @@ -732,6 +742,16 @@ export function fromError(
},
{ cause: e },
).toObject()
// Issue #738: a tripped repetition guard is the harness's own abort, so it
// is recorded with a stable identity instead of being inferred later from
// its message text. The UnknownError name and message stay byte-identical
// (renderers and SessionRetry.retryable read them); data.ref is the typed
// marker recovery checks, and a provider HTTP error can never carry it.
case e instanceof RepetitionGuard.RepetitionDetectedError:
return new NamedError.Unknown(
{ message: errorMessage(e), ref: RepetitionGuard.ERROR_REF },
{ cause: e },
).toObject()
// Named session errors thrown upstream (e.g. from a provider-error stream
// event, #409) keep their classification instead of degrading to Unknown.
case ContextOverflowError.isInstance(e):
Expand Down
Loading
Loading