Skip to content

Instantly share code, notes, and snippets.

@jcmpgit
Created August 7, 2026 08:05
Show Gist options
  • Select an option

  • Save jcmpgit/60087ac45da47c58ffe654781bada8fc to your computer and use it in GitHub Desktop.

Select an option

Save jcmpgit/60087ac45da47c58ffe654781bada8fc to your computer and use it in GitHub Desktop.
Hermes Agent workaround: bypass mid-stream-drop guard for gpt-5.6-luna / kimi-k3 on OpenCode Go (anomalyco/opencode#39881)
#!/usr/bin/env python3
"""Bypass the Hermes mid-stream-drop guard for models that omit finish_reason.
WHY THIS EXISTS
---------------
Some upstream endpoints (e.g. OpenCode Go's chat/completions compatibility
path for gpt-5.6-luna and kimi-k3 — see anomalyco/opencode#39881) deliver
complete responses but never send a terminal `finish_reason` / `[DONE]`.
Hermes deliberately refuses to stamp `finish_reason="stop"` on such streams
(guards a real truncation class, upstream issue #32086), so every response
from those models is treated as a mid-stream drop and the turn fails with
"Response remained truncated after 4 continuation attempts".
This script adds a narrow, model-scoped exception to that guard. It is a
WORKAROUND for upstream bugs, not a fix. When the upstream endpoints are
fixed, remove the bypass and delete this script.
WHAT IT CHANGES
---------------
One boolean clause in the `_text_only_dropped_no_finish` guard inside
`agent/chat_completion_helpers.py` (function `interruptible_streaming_api_call`):
_text_only_dropped_no_finish = (
finish_reason is None
and content_parts
and not tool_calls_acc
and "gpt-5.6-luna" not in (model_name or "") # <-- added
)
Scope is ONLY the text-only drop guard. The tool-call truncation guard
(`_tool_args_dropped_no_finish`) is left untouched — real truncation still
flags. Extend the `SKIPPED_MODEL_SUBSTRINGS` list to bypass other affected
models (kimi-k3 is known-affected upstream).
USAGE
-----
python3 luna_finish_reason_patch.py status # show current state
python3 luna_finish_reason_patch.py apply # backup + patch
python3 luna_finish_reason_patch.py revert # restore .bak
The target file is taken from HERMES_HELPERS_FILE if set, else the default
below. After apply/revert, RESTART the process that serves your sessions
(Hermes: the gateway and/or dashboard service) — the code is read at
process start. The patch is NOT persistent across container/image rebuilds
(no git tracking on the target); the .bak file is the safety net.
TRADE-OFF
---------
With the bypass active, a GENUINE mid-stream truncation from the bypassed
model is silently accepted as complete (the #32086 concern), scoped to only
the listed models.
"""
import os
import shutil
import sys
FILE = os.environ.get(
"HERMES_HELPERS_FILE",
"/opt/hermes/agent/chat_completion_helpers.py",
)
BAK = FILE + ".bak"
MARKER = "# HACK luna finish_reason bypass (anomalyco/opencode#39881)"
# Models known to omit finish_reason on complete responses (upstream bug).
# Substring match, so both "opencode-go/gpt-5.6-luna" and bare "gpt-5.6-luna" hit.
SKIPPED_MODEL_SUBSTRINGS = ("gpt-5.6-luna",)
_SKIP_COND = "".join(
f' and "{m}" not in (model_name or "")\n'
for m in SKIPPED_MODEL_SUBSTRINGS
)
OLD_BLOCK = """ _text_only_dropped_no_finish = (
finish_reason is None
and content_parts
and not tool_calls_acc
)
"""
NEW_BLOCK = f""" _text_only_dropped_no_finish = (
finish_reason is None
and content_parts
and not tool_calls_acc
# HACK luna finish_reason bypass (anomalyco/opencode#39881)
# Model omits finish_reason on complete responses; text is fully
# delivered, only the terminal marker is missing. Treat as complete.
# REVERT: python3 {os.path.basename(__file__)} revert + restart service.
# Also lost automatically on image rebuild.
{_SKIP_COND} )
"""
def status() -> None:
src = _read()
if MARKER in src:
print(f"STATE: patched (bypass active for: {', '.join(SKIPPED_MODEL_SUBSTRINGS)})")
print("STATE: backup %s" % ("present" if os.path.exists(BAK) else "MISSING!"))
else:
print("STATE: pristine (no bypass)")
def apply() -> None:
src = _read()
if MARKER in src or 'not in (model_name or "")' in src:
print("Already patched — nothing to do.")
return
if OLD_BLOCK not in src:
print("ERROR: guard block not found. File changed upstream? Aborting.")
sys.exit(1)
if not os.path.exists(BAK):
shutil.copy2(FILE, BAK)
print(f"Backup written: {BAK}")
_write(src.replace(OLD_BLOCK, NEW_BLOCK, 1))
print("Patched. Verify below, then restart the service.")
show()
def revert() -> None:
if os.path.exists(BAK):
shutil.copy2(BAK, FILE)
print(f"Restored {FILE} from backup.")
else:
print(f"ERROR: no {BAK} found. Manual revert needed.")
sys.exit(1)
if MARKER in _read():
print("ERROR: marker still present after restore!")
sys.exit(1)
print("Reverted cleanly. Restart the service.")
def show() -> None:
src = _read()
i = src.find("_text_only_dropped_no_finish")
print(src[max(0, i - 50):i + 700])
def _read() -> str:
with open(FILE) as f:
return f.read()
def _write(content: str) -> None:
with open(FILE, "w") as f:
f.write(content)
if __name__ == "__main__":
cmd = sys.argv[1] if len(sys.argv) > 1 else "status"
{"status": status, "apply": apply, "revert": revert}[cmd]()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment