coding-agents: process-local turn counter re-injects the session preamble after every host restart

Aperta Adatta ai principianti
#7,039 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
84/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
javascript
Ambito
ai

Direzione di ricerca

Inizia dal gate del conteo dei turni e da buildHookOutput in dist/index.js, quindi ispeziona i percorsi di lettura e scrittura della cache di sessione persistente intorno a writeSessionCache, readSessionCache e resetSessionCache. Usa la riproduzione di riavvio e ripresa per verificare che una nuova sessione inietti il preambolo una sola volta, mentre una sessione ripresa non lo inietti di nuovo a meno che la compattazione non reimposti la cache.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

integration:coding-agents

Bug Description

Expected: a session gets the once-per-session preamble (tool guide + page roster) exactly once — on its first user turn.

Actual: on persistent-plugin harnesses (opencode2, pi, dsh), every restart of the host process re-injects the full ~4 KB preamble into the same session, because the turn-1 gate consults a counter that lives in process memory:

// dist/index.js (0.9.0)
turnCount = new Map();                                        // :19546
const turns = (this.turnCount.get(sessionId) ?? 0) + 1;      // :19649
if (turns === 1) {                                           // :19665
  blocks.push(buildPagesPreamble(output.pages, ...));
}

Restarting the host resets the counter, so resuming the same session takes turns back to 1 and the gate fires again. Each injected copy lands in the user message, so it stays in the transcript permanently — a long-lived session that survives several host restarts collects one duplicate block per restart.

Impact: since #6053 moved the injection into the user message (fixing the prefix-cache break from #5914), the damage is bounded to transcript pollution — no cache miss — but it is unbounded in count and never cleaned up.

What makes this a one-line-ish fix

The persistent session cache already tracks the session's real turn count — it just isn't used by this gate:

// dist/index.js:19494
writeSessionCache(cacheFile, { turns, reflectAnswer, reflectAttempts, ... });

buildHookOutput already reads it back for cadence decisions (cached3.turns), and #6943 added injectedAtTurn?: number to the cache for autoInjectEveryTurns. The session's durable turn count exists; only the preamble gate consults the in-process map instead.

The gate could read the persisted count the same way onCompacted (dist/index.js:19684) already models "this session needs the preamble again": compaction deliberately resets the cache (resetSessionCache(...) + turnCount.delete(...)) because the model lost the block. A plain process restart is the mirror case with the opposite desired outcome — the model still has the block in the transcript, and re-injecting only duplicates it.

To reproduce

  1. Start a session on a persistent-plugin host (opencode or pi; any host whose plugin lives in the agent process).
  2. Send one prompt; observe the preamble block injected on turn 1.
  3. Restart the host process; reopen/resume the same session.
  4. Send the next prompt: the full preamble is injected a second time (turn counter is back at 1), even though the first copy is still in the transcript.

Observed with: plugin 0.9.0, server 1.3.0-slim, opencode + pi harnesses.

Suggested fix

Key the gate on the persisted session cache instead of the in-process map, e.g.:

const cachedTurns = readSessionCache(cacheFile).turns ?? 0;
if (turns === 1 && cachedTurns === 0) {
  blocks.push(buildPagesPreamble(output.pages, ...));
}

This keeps the intended behavior (fresh session ⇒ one preamble; compaction ⇒ cache reset makes it re-inject) and stops the duplicate after a host restart in the same session.

Happy to send a PR if the approach sounds right.

Lingua principale
Python
Stelle
45.2k
Fork
5.9k
Merge medio
5h 57m
PR unite (30g)
351

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di vectorize-io/hindsight

Tutte le issue di vectorize-io/hindsight

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.