Skip to content

Persistent Context Across Sessions for Every Agent – Captures everything your agent does during sessions, compresses it with AI, and injects relevant context back into future sessions. Works with Claude Code, OpenClaw, Codex, Gemini, Hermes, Copilot, OpenCode + More

28.6 KB
Raw Blame
import { afterEach, test } from 'bun:test';
import assert from 'node:assert/strict';
import { mkdtempSync, readFileSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { HumanCopyWorker, MAX_OUTPUT_REJECTION_ATTEMPTS, chatCompletionsHumanCopyProvider, fitGeneratedCopy } from '../../../src/services/work-view/human-copy-worker.mjs';
import { createHumanCopyInput, validateHumanCopy } from '../../../src/services/work-view/human-copy.mjs';

const disposals = [];
afterEach(() => { for (const dispose of disposals.splice(0).reverse()) dispose(); });

const request = (id = 'request-one', progress = 'The local source is connected.') => ({
  id, original: { text: 'Show my work in a live tracker.', source: 'user-prompt', sourceId: 10 },
  scopeUpdates: [], lifecycle: null, updatedAtEpoch: 1000,
  workstreams: [{ id: `${id}:stream`, items: [{
    id: 'task-one', title: 'Connect recorded work', status: 'doing',
    progress: { text: progress }, nextStep: { text: 'Verify mobile layout.' },
    sourceRefs: [{ kind: 'work-state', id: 1 }], lastUpdateEpoch: 1000,
  }], activity: [] }],
});
const view = (...humanRequests) => ({ humanRequests });
const copy = (input, title = 'See work clearly') => ({
  title, summary: 'Recorded work is available in one place.', next: 'Check the mobile layout.',
  workstreams: input.workstreams.map(stream => ({ id: stream.id, title: 'Connect your work', summary: 'The recorded source is connected.' })),
});
const deferred = () => {
  let resolve, reject;
  const promise = new Promise((yes, no) => { resolve = yes; reject = no; });
  return { promise, resolve, reject };
};
const settle = () => new Promise(resolve => setImmediate(resolve));
const worker = (provider, options = {}) => new HumanCopyWorker({
  provider, cachePath: null, quietMs: 0, minIntervalMs: 0, retryMs: 1000, warn: () => {},
  now: () => 1000, ...options,
});
const waitForWorker = async instance => {
  for (let i = 0; i < 100 && instance.running.size; i++) await new Promise(resolve => setTimeout(resolve, 1));
  assert.equal(instance.running.size, 0, 'worker request settles');
};

test('setup pause suppresses already queued and newly observed user-token calls until setup completes', async () => {
  let paused = false, now = 0, calls = 0;
  const instance = worker(async input => { calls++; return { output: copy(input) }; }, { now: () => now, quietMs: 100 });
  disposals.push(() => instance.close());
  instance.pauseWhile(() => paused);
  instance.observe(view(request()));
  assert.equal(instance.status().queued, 1);
  paused = true;
  now = 1000;
  instance.pump();
  instance.observe(view(request('request-two')));
  assert.equal(calls, 0);
  assert.equal(instance.status().queued, 1);
  paused = false;
  instance.pump();
  await settle();
  assert.equal(calls, 1);
});

test('a pause that begins between pump and run starts nothing and leaves no request counted as generating', async () => {
  let now = 0, calls = 0;
  const instance = worker(async input => { calls++; return { output: copy(input) }; }, { now: () => now, quietMs: 100 });
  disposals.push(() => instance.close());
  instance.observe(view(request()));
  assert.equal(instance.status().queued, 1);
  now = 1000;
  // pump() reads the pause, then run() reads it again: setup becomes active between the two reads.
  let pauseReads = 0;
  instance.pauseWhile(() => ++pauseReads > 1);
  instance.pump();
  assert.equal(pauseReads, 2, 'pump and run each read the pause');
  assert.equal(calls, 0);
  // The auto-updater reads this count as busy (WorkViewRoutes.cardWordingRequestsRunning).
  assert.equal(instance.status().generating, 0);
  // Once setup ends, the request is generated: no stale entry holds it back.
  instance.pauseWhile(() => false);
  instance.pump();
  await waitForWorker(instance);
  assert.equal(calls, 1);
  assert.equal(instance.status().generating, 0);
});

test('one request stays single-flight and reuses its copy across polling, source ages and display-label changes', async () => {
  let calls = 0;
  const pending = deferred();
  const source = request();
  const instance = worker(async input => { calls++; return pending.promise; });
  disposals.push(() => instance.close());
  for (let i = 0; i < 8; i++) instance.observe(view(source));
  assert.equal(calls, 1);
  pending.resolve({ output: copy(createHumanCopyInput(source)) });
  await settle();
  const changedMetadata = structuredClone(source);
  changedMetadata.title = 'A cached presentation label';
  changedMetadata.updatedAtEpoch = 9000;
  changedMetadata.workstreams[0].items[0].lastUpdateEpoch = 9000;
  for (let i = 0; i < 8; i++) assert.equal(instance.observe(view(changedMetadata)).humanRequests[0].humanCopy.state, 'current');
  assert.equal(calls, 1);
});

test('new unrelated requests generate only their own copy and one call includes every workstream', async () => {
  const inputs = [];
  const source = request();
  source.workstreams.push({ id: 'second-stream', items: [], activity: [] });
  const instance = worker(async input => { inputs.push(input); return { output: copy(input) }; });
  disposals.push(() => instance.close());
  instance.observe(view(source));
  await settle();
  instance.observe(view(source, request('request-two')));
  await settle();
  assert.equal(inputs.length, 2);
  assert.equal(inputs[0].workstreams.length, 2);
  assert.equal(instance.observe(view(source)).humanRequests[0].humanCopy.state, 'current');
  assert.equal(inputs.length, 2);
});

test('a changed in-flight input never publishes stale copy and the next call uses the latest input', async () => {
  const jobs = [];
  const instance = worker(async input => {
    const job = { input, ...deferred() }; jobs.push(job); return job.promise;
  });
  disposals.push(() => instance.close());
  const first = request(), latest = request('request-one', 'Mobile layout is now verified.');
  instance.observe(view(first));
  instance.observe(view(latest));
  assert.equal(jobs.length, 1);
  jobs[0].resolve({ output: copy(jobs[0].input, 'Old copy') });
  await settle();
  assert.equal(instance.cache.has(first.id), false);
  assert.equal(jobs.length, 2);
  assert.match(jobs[1].input.workstreams[0].currentItems[0].progress, /Mobile layout/);
  jobs[1].resolve({ output: copy(jobs[1].input, 'Latest copy') });
  await settle();
  assert.equal(instance.observe(view(latest)).humanRequests[0].humanCopy.title, 'Latest copy');
});

test('per-request minimum interval prevents costly repeated generations for changing work', async () => {
  let now = 1000, calls = 0;
  const instance = worker(async input => { calls++; return { output: copy(input) }; }, { now: () => now, minIntervalMs: 1000 });
  disposals.push(() => instance.close());
  instance.observe(view(request())); await settle();
  now = 1100;
  const changed = request('request-one', 'A new supported result is recorded.');
  assert.equal(instance.observe(view(changed)).humanRequests[0].humanCopy.state, 'outdated');
  now = 1999; instance.pump(); assert.equal(calls, 1);
  now = 2000; instance.pump(); await settle();
  assert.equal(calls, 2);
  assert.equal(instance.observe(view(changed)).humanRequests[0].humanCopy.state, 'current');
});

test('a burst is debounced while maximum wait eventually processes its newest content', async () => {
  let now = 0;
  const inputs = [];
  const instance = worker(async input => { inputs.push(input); return { output: copy(input) }; }, { now: () => now, quietMs: 100, maxWaitMs: 300 });
  disposals.push(() => instance.close());
  for (const at of [0, 80, 160, 240]) {
    now = at; instance.observe(view(request('request-one', `Supported update ${at}.`)));
  }
  assert.equal(inputs.length, 0);
  now = 320; instance.observe(view(request('request-one', 'Supported latest update.')));
  await settle();
  assert.equal(inputs.length, 1);
  assert.equal(inputs[0].workstreams[0].currentItems[0].progress, 'Supported latest update.');
});

test('provider failure preserves last good copy, reports it outdated, and respects retry timing', async () => {
  let now = 1000, calls = 0;
  const instance = worker(async input => {
    calls++;
    if (calls === 2) throw new Error('Temporary gateway failure');
    return { output: copy(input, calls === 1 ? 'Last good copy' : 'Recovered copy') };
  }, { now: () => now, retryMs: 100 });
  disposals.push(() => instance.close());
  instance.observe(view(request())); await settle();
  const changed = request('request-one', 'The underlying work changed.');
  now = 1010; instance.observe(view(changed)); await settle();
  const outdated = instance.observe(view(changed)).humanRequests[0].humanCopy;
  assert.equal(outdated.state, 'outdated');
  assert.equal(outdated.title, 'Last good copy');
  now = 1109; instance.pump(); assert.equal(calls, 2);
  now = 1110; instance.pump(); await settle();
  assert.equal(calls, 3);
  assert.equal(instance.observe(view(changed)).humanRequests[0].humanCopy.title, 'Recovered copy');
});

test('permanent HTTP failures are logged safely and wait for provider configuration changes', async () => {
  let now = 1000, configKey = 'first-config', calls = 0;
  const warnings = [];
  const directory = mkdtempSync(join(tmpdir(), 'human-copy-failure-'));
  const provider = Object.assign(async input => {
    calls++;
    if (configKey === 'first-config') throw Object.assign(new Error('Bearer secret-response-detail'), { status: 404 });
    return { output: copy(input, 'Recovered after settings change') };
  }, { getConfigKey: () => configKey });
  const instance = new HumanCopyWorker({ provider, cachePath: join(directory, 'human-copy.json'), quietMs: 0, minIntervalMs: 0,
    retryMs: 100, now: () => now, warn: message => warnings.push(message) });
  disposals.push(() => { instance.close(); rmSync(directory, { recursive: true, force: true }); });

  instance.observe(view(request())); await waitForWorker(instance);
  assert.equal(calls, 1);
  assert.equal(instance.status().failures[0].reason, 'HTTP 404');
  const logPath = join(directory, 'human-copy-usage.jsonl');
  const failureLine = readFileSync(logPath, 'utf8').trim();
  const failure = JSON.parse(failureLine);
  assert.equal(failure.status, 'failed');
  assert.equal(failure.reason, 'HTTP 404');
  assert.doesNotMatch(failureLine, /secret-response-detail/);
  assert.equal(warnings.length, 1);

  now += 60 * 60 * 1000;
  instance.pump(); await waitForWorker(instance);
  assert.equal(calls, 1, 'a permanent 4xx does not retry on a timer');

  configKey = 'second-config';
  instance.observe(view(request())); await waitForWorker(instance);
  assert.equal(calls, 2, 'a provider-settings change retries immediately');
  assert.equal(instance.observe(view(request())).humanRequests[0].humanCopy.title, 'Recovered after settings change');
});

test('429 failures use exponential backoff', async () => {
  let now = 0, calls = 0;
  const instance = worker(async () => { calls++; throw Object.assign(new Error('rate limited'), { status: 429 }); },
    { now: () => now, retryMs: 100, maxRetryMs: 1000 });
  disposals.push(() => instance.close());
  instance.observe(view(request())); await settle();
  assert.equal(calls, 1);
  now = 99; instance.pump(); await settle(); assert.equal(calls, 1);
  now = 100; instance.pump(); await settle(); assert.equal(calls, 2);
  now = 299; instance.pump(); await settle(); assert.equal(calls, 2);
  now = 300; instance.pump(); await settle(); assert.equal(calls, 3);
});

test('invalid or invented output identities cannot publish copy or change recorded status', async () => {
  const source = request();
  const instance = worker(async input => ({ output: {
    ...copy(input), status: 'done', workstreams: [{ id: 'invented-stream', title: 'Done', summary: 'Everything delivered.' }],
  } }));
  disposals.push(() => instance.close());
  instance.observe(view(source)); await settle();
  const result = instance.observe(view(source)).humanRequests[0];
  assert.equal(result.humanCopy.state, 'unavailable');
  assert.equal(instance.cache.size, 0);
  assert.equal(result.workstreams[0].items[0].status, 'doing');
});

test('text fitting enforces display limits without repairing malformed identities or status fields', () => {
  const input = createHumanCopyInput(request());
  const long = copy(input);
  long.title = 'Useful title words '.repeat(15);
  long.summary = 'A supported sentence. '.repeat(20);
  const fitted = fitGeneratedCopy(long);
  assert.ok(fitted.title.length <= 100);
  assert.ok(fitted.summary.length <= 240);
  assert.doesNotThrow(() => validateHumanCopy(fitted, input));
  assert.throws(() => validateHumanCopy(fitGeneratedCopy({ ...long, status: 'done' }), input));
  assert.throws(() => validateHumanCopy(fitGeneratedCopy({ ...long, workstreams: [{ ...long.workstreams[0], id: 'foreign' }] }), input));
  assert.equal(fitGeneratedCopy(null), null);
  assert.equal(fitGeneratedCopy('invalid text'), 'invalid text');
});

test('unchanged current items reordered by a timestamp-only write do not incur a new generation', async () => {
  let calls = 0;
  const source = request();
  source.workstreams[0].items.push({ ...structuredClone(source.workstreams[0].items[0]), id: 'task-two', title: 'Review the result' });
  const instance = worker(async input => { calls++; return { output: copy(input) }; });
  disposals.push(() => instance.close());
  instance.observe(view(source)); await settle();
  const reordered = structuredClone(source);
  reordered.workstreams[0].items.reverse();
  reordered.workstreams[0].items[0].lastUpdateEpoch = 9000;
  const result = instance.observe(view(reordered));
  assert.equal(result.humanRequests[0].humanCopy.state, 'current');
  assert.equal(calls, 1);
  await settle();
});

test('a malformed stale cache entry is never published as outdated display copy', () => {
  const instance = worker(async input => ({ output: copy(input) }), { quietMs: 1000 });
  disposals.push(() => instance.close());
  const source = request();
  instance.cache.set(source.id, { fingerprint: 'old-fingerprint', generatedAt: 1, copy: { title: 'Invalid cached shape', workstreams: 'not-an-array' } });
  const result = instance.observe(view(source)).humanRequests[0].humanCopy;
  assert.equal(result.title, undefined);
  assert.ok(['pending', 'unavailable'].includes(result.state));
});

test('without a configured summary provider, requests are marked unavailable and nothing is queued', () => {
  const instance = new HumanCopyWorker({ provider: null, cachePath: null });
  disposals.push(() => instance.close());
  const result = instance.observe(view(request())).humanRequests[0].humanCopy;
  assert.equal(result.state, 'unavailable');
  assert.equal(instance.status().queued, 0);
});

test('a provider whose settings are off is not queued or called, then resolves after opt-in', async () => {
  let enabled = false, calls = 0;
  const provider = Object.assign(async input => { calls++; return { output: copy(input) }; }, { isAvailable: () => enabled });
  const instance = worker(provider);
  disposals.push(() => instance.close());
  const source = request();
  assert.equal(instance.observe(view(source)).humanRequests[0].humanCopy.state, 'unavailable');
  assert.equal(instance.status().queued, 0);
  assert.equal(calls, 0);

  enabled = true;
  assert.equal(instance.observe(view(source)).humanRequests[0].humanCopy.state, 'pending');
  await settle();
  assert.equal(calls, 1);
  assert.equal(instance.observe(view(source)).humanRequests[0].humanCopy.state, 'current');
});

test('explicit off hides cached wording while retaining it for later reuse', async () => {
  let enabled = true, off = false, configKey = 'enabled', calls = 0;
  const provider = Object.assign(async input => { calls++; return { output: copy(input, 'Stored wording') }; }, {
    isAvailable: () => enabled, isOff: () => off, getConfigKey: () => configKey,
  });
  const instance = worker(provider);
  disposals.push(() => instance.close());
  const source = request();
  instance.observe(view(source)); await settle();
  assert.equal(instance.observe(view(source)).humanRequests[0].humanCopy.title, 'Stored wording');

  enabled = false; off = true; configKey = null;
  const disabled = instance.observe(view(source)).humanRequests[0].humanCopy;
  assert.equal(disabled.state, 'unavailable');
  assert.equal(disabled.title, undefined);
  assert.equal(instance.cache.size, 1);

  enabled = true; off = false; configKey = 'enabled-again';
  const reenabled = instance.observe(view(source)).humanRequests[0].humanCopy;
  assert.equal(reenabled.state, 'current');
  assert.equal(reenabled.title, 'Stored wording');
  assert.equal(calls, 1);
});

test('a strict server that rejects the reasoning field gets one resend without it, and is remembered', async () => {
  const bodies = [];
  const provider = chatCompletionsHumanCopyProvider({
    url: 'http://127.0.0.1:8000/v1/strict-reasoning/chat/completions', apiKey: '', model: 'vllm-model',
    fetchImpl: async (_url, init) => {
      const body = JSON.parse(init.body); bodies.push(body);
      if (Object.hasOwn(body, 'reasoning')) return new Response(JSON.stringify({ object: 'error', message: "[{'type': 'extra_forbidden', 'loc': ('body', 'reasoning'), 'msg': 'Extra inputs are not permitted'}]" }), { status: 400 });
      return new Response(JSON.stringify({ choices: [{ message: { content: '{"title":"t"}' } }] }));
    },
  });
  assert.deepEqual((await provider({ version: 1 })).output, { title: 't' });
  assert.deepEqual(bodies.map(body => Object.hasOwn(body, 'reasoning')), [true, false]);
  // The next request to that URL leaves the field out from the start.
  await provider({ version: 2 });
  assert.deepEqual(bodies.map(body => Object.hasOwn(body, 'reasoning')), [true, false, false]);
});

test('any other 400 is not resent', async () => {
  let calls = 0;
  const provider = chatCompletionsHumanCopyProvider({
    url: 'http://127.0.0.1:8000/v1/other-400/chat/completions', apiKey: '', model: 'm',
    fetchImpl: async () => { calls++; return new Response(JSON.stringify({ error: { message: 'model is required' } }), { status: 400 }); },
  });
  await assert.rejects(provider({ version: 1 }), error => error.status === 400);
  assert.equal(calls, 1);
});

test('402 (out of credits) retries on the capped backoff instead of waiting for a settings change', async () => {
  let now = 0, calls = 0;
  const instance = worker(async () => { calls++; throw Object.assign(new Error('insufficient credits'), { status: 402 }); },
    { now: () => now, retryMs: 100, maxRetryMs: 150 });
  disposals.push(() => instance.close());
  instance.observe(view(request())); await settle();
  assert.equal(calls, 1);
  now = 99; instance.pump(); await settle(); assert.equal(calls, 1);
  now = 100; instance.pump(); await settle(); assert.equal(calls, 2);
  // Capped at maxRetryMs: the next wait is 150, not 200.
  now = 249; instance.pump(); await settle(); assert.equal(calls, 2);
  now = 250; instance.pump(); await settle(); assert.equal(calls, 3);
});

test('the chat-completions provider omits authorization for a configured keyless local endpoint', async () => {
  let sent;
  const provider = chatCompletionsHumanCopyProvider({
    url: 'http://127.0.0.1:1234/v1/chat/completions', apiKey: '', model: 'local-model',
    fetchImpl: async (url, init) => { sent = { url, init }; return new Response(JSON.stringify({ choices: [{ message: { content: '{"title":"t"}' } }] })); },
  });
  await provider({ version: 1 });
  assert.equal(sent.url, 'http://127.0.0.1:1234/v1/chat/completions');
  assert.equal(Object.hasOwn(sent.init.headers, 'Authorization'), false);
});

test('the chat-completions provider sends the configured model and key and parses JSON copy', async () => {
  let sent;
  const provider = chatCompletionsHumanCopyProvider({
    url: 'https://example.test/v1/chat/completions', apiKey: 'key-1', model: 'configured-model',
    fetchImpl: async (url, init) => { sent = { url, init }; return new Response(JSON.stringify({ model: 'configured-model', choices: [{ message: { content: '{"title":"t"}' } }] })); },
  });
  const input = { original: { text: 'Lütfen iş kartlarımı göster.' } };
  const result = await provider(input);
  assert.equal(sent.url, 'https://example.test/v1/chat/completions');
  assert.equal(sent.init.headers.Authorization, 'Bearer key-1');
  const body = JSON.parse(sent.init.body);
  assert.equal(body.model, 'configured-model');
  assert.ok(body.messages[0].content.includes('First decide which human language the person wrote original.text in, ignoring code, file paths, URLs, repository and product names; write every value in that language. It overrides the language of these instructions and of the rest of the payload.'));
  assert.deepEqual(JSON.parse(body.messages[1].content), input);
  assert.deepEqual(result.output, { title: 't' });
  assert.equal(result.error, null);
});

// A temporary source read failure is not evidence that a queued request was deleted.
test('failed and stale snapshots do not retire pending requests', async () => {
  let now = 0, calls = 0;
  const instance = worker(async input => { calls++; return { output: copy(input) }; }, { now: () => now, quietMs: 100 });
  disposals.push(() => instance.close());
  instance.observe({ ...view(request()), source: { connected: true, stale: false } });
  instance.observe({ ...view(), source: { connected: false, stale: true } });
  assert.equal(instance.status().queued, 1);
  instance.observe({ ...view(), source: { connected: true, stale: true } });
  assert.equal(instance.status().queued, 1);
  now = 1000;
  instance.pump();
  await waitForWorker(instance);
  assert.equal(calls, 1);
});

test('validator rejections back off exponentially and wait for change after max attempts', async () => {
  let now = 0, calls = 0;
  const source = request();
  const instance = worker(async input => {
    calls++;
    return { output: { ...copy(input), workstreams: [{ id: 'invented-stream', title: 'Done', summary: 'Everything delivered.' }] } };
  }, { now: () => now, retryMs: 100, maxRetryMs: 1000 });
  disposals.push(() => instance.close());

  instance.observe(view(source));
  await settle();
  assert.equal(calls, 1);
  assert.equal(instance.status().failures[0].reason, 'Unknown or duplicate workstream id');
  assert.equal(instance.failures.get(source.id).attempt, 1);
  assert.equal(instance.failures.get(source.id).waitForChange, false);
  assert.equal(instance.failures.get(source.id).retryAfterMs, 100);

  // Attempt 2 backoff: 100ms
  now = 99; instance.pump(); await settle(); assert.equal(calls, 1);
  now = 100; instance.pump(); await settle(); assert.equal(calls, 2);
  assert.equal(instance.failures.get(source.id).attempt, 2);
  assert.equal(instance.failures.get(source.id).waitForChange, false);
  assert.equal(instance.failures.get(source.id).retryAfterMs, 200);

  // Attempt 3 backoff: 200ms (100 + 200 = 300)
  now = 299; instance.pump(); await settle(); assert.equal(calls, 2);
  now = 300; instance.pump(); await settle(); assert.equal(calls, 3);
  assert.equal(instance.failures.get(source.id).attempt, 3);
  assert.equal(instance.failures.get(source.id).waitForChange, true);

  // Reached MAX_OUTPUT_REJECTION_ATTEMPTS (3). waitForChange is now true.
  assert.equal(MAX_OUTPUT_REJECTION_ATTEMPTS, 3);
  now = 3600 * 1000;
  instance.pump();
  await settle();
  assert.equal(calls, 3, 'validator rejections stop retrying on a timer after MAX_OUTPUT_REJECTION_ATTEMPTS');

  // New input / fingerprint resets attempt and retries
  const updatedSource = request('request-one', 'Different progress.');
  instance.observe(view(updatedSource));
  now += 100;
  instance.pump();
  await settle();
  assert.equal(calls, 4, 'new fingerprint triggers a new attempt');
  assert.equal(instance.failures.get(source.id).attempt, 1);
});

test('surfaces clean validation message in failure reason', async () => {
  const source = request();
  const instance = worker(async input => {
    return { output: { ...copy(input), title: '' } };
  });
  disposals.push(() => instance.close());

  instance.observe(view(source));
  await settle();
  assert.equal(instance.status().failures[0].reason, 'Request title must be 1–100 characters');
});

test('temporary connection failures do not count as validation errors or block recovered endpoints', async () => {
  let now = 0, calls = 0;
  const source = request();
  const instance = worker(async input => {
    calls++;
    if (calls <= 3) throw new TypeError('fetch failed');
    return { output: copy(input, 'Recovered after network restored') };
  }, { now: () => now, retryMs: 100 });
  disposals.push(() => instance.close());

  instance.observe(view(source));
  await settle();
  assert.equal(calls, 1);
  assert.equal(instance.status().failures[0].reason, 'Summary request failed');
  assert.equal(instance.failures.get(source.id).outputRejectionAttempts, 0);
  assert.equal(instance.failures.get(source.id).waitForChange, false);

  // Connection fails 3 times
  now = 100; instance.pump(); await settle(); assert.equal(calls, 2);
  now = 200; instance.pump(); await settle(); assert.equal(calls, 3);
  assert.equal(instance.failures.get(source.id).attempt, 3);
  assert.equal(instance.failures.get(source.id).outputRejectionAttempts, 0);
  assert.equal(instance.failures.get(source.id).waitForChange, false, 'network TypeError does not set waitForChange');

  // Next retry at now = 300 should run and recover
  now = 300; instance.pump(); await settle();
  assert.equal(calls, 4);
  assert.equal(instance.observe(view(source)).humanRequests[0].humanCopy.title, 'Recovered after network restored');
});

test('unrelated HTTP failures do not consume validation attempts', async () => {
  let now = 0, calls = 0;
  const source = request();
  const instance = worker(async input => {
    calls++;
    if (calls <= 2) throw Object.assign(new Error('rate limited'), { status: 429 });
    // Returns malformed copy on call 3
    return { output: { ...copy(input), workstreams: [{ id: 'bad-stream', title: 'T', summary: 'S' }] } };
  }, { now: () => now, retryMs: 100, maxRetryMs: 1000 });
  disposals.push(() => instance.close());

  // Call 1: 429
  instance.observe(view(source)); await settle(); assert.equal(calls, 1);
  assert.equal(instance.failures.get(source.id).outputRejectionAttempts, 0);

  // Call 2: 429
  now = 100; instance.pump(); await settle(); assert.equal(calls, 2);
  assert.equal(instance.failures.get(source.id).outputRejectionAttempts, 0);

  // Call 3: Malformed copy (validation error). attempt is 3, but outputRejectionAttempts should be 1!
  now = 300; instance.pump(); await settle(); assert.equal(calls, 3);
  assert.equal(instance.failures.get(source.id).attempt, 3);
  assert.equal(instance.failures.get(source.id).outputRejectionAttempts, 1);
  assert.equal(instance.failures.get(source.id).waitForChange, false, 'one validation failure after 429s does not block retries');

  // Call 4: Validation attempt 2
  now = 400; instance.pump(); await settle(); assert.equal(calls, 4);
  assert.equal(instance.failures.get(source.id).outputRejectionAttempts, 2);
  assert.equal(instance.failures.get(source.id).waitForChange, false);

  // Call 5: Validation attempt 3 -> now reaches MAX_OUTPUT_REJECTION_ATTEMPTS
  now = 600; instance.pump(); await settle(); assert.equal(calls, 5);
  assert.equal(instance.failures.get(source.id).outputRejectionAttempts, 3);
  assert.equal(instance.failures.get(source.id).waitForChange, true);
});



test('an empty model answer stops after the rejection cap instead of retrying forever (#6037)', async () => {
  let now = 0, calls = 0;
  const source = request();
  const instance = worker(async () => {
    calls++;
    return { output: null, error: 'No answer text (finish: length).' };
  }, { now: () => now, retryMs: 100, maxRetryMs: 1000 });
  disposals.push(() => instance.close());

  instance.observe(view(source)); await settle();
  for (let pump = 0; pump < 20; pump++) { now += 1000; instance.pump(); await settle(); }

  assert.equal(calls, MAX_OUTPUT_REJECTION_ATTEMPTS, 'one unchanged request version gets at most the capped number of calls');
  assert.equal(instance.failures.get(source.id).reason, 'No answer text');
  assert.equal(instance.failures.get(source.id).waitForChange, true);

  instance.observe(view(request('request-one', 'Different progress.')));
  now += 1000; instance.pump(); await settle();
  assert.equal(calls, MAX_OUTPUT_REJECTION_ATTEMPTS + 1, 'changed input is tried again');
});

test('a reply body that is not JSON keeps the flat retry, since that is a transport problem', async () => {
  let now = 0, calls = 0;
  const source = request();
  const instance = worker(async () => {
    calls++;
    throw new SyntaxError('Unexpected token < in JSON');
  }, { now: () => now, retryMs: 100, maxRetryMs: 1000 });
  disposals.push(() => instance.close());

  instance.observe(view(source)); await settle();
  for (let pump = 0; pump < 5; pump++) { now += 100; instance.pump(); await settle(); }

  assert.equal(calls, 6);
  assert.equal(instance.failures.get(source.id).reason, 'Invalid JSON response');
  assert.equal(instance.failures.get(source.id).outputRejectionAttempts, 0);
  assert.equal(instance.failures.get(source.id).waitForChange, false);
});