ODS V3 Pre-Release: Public testing and refinement ahead of the official V3 launch. Turn your PC, Mac, or Linux box into a private AI server.
Python ai-agentsamdcomfyuidockerllama-cppllmlocal-ain8nnvidiaopen-webuiragself-hostedspeech-to-textstrix-halotext-to-speechworkflow-automation
fix(pixel): reuse explicitly requested same-chat source receipts (#8268)
Mike Bradleya9c963ab3bc1
diff --git a/.github/workflows/portal-runtime.yml b/.github/workflows/portal-runtime.ymlindex 0759a580b..f25325fbc 100644--- a/.github/workflows/portal-runtime.yml+++ b/.github/workflows/portal-runtime.yml@@ -73,6 +73,8 @@ jobs:run: node --test ods/extensions/services/pixel-agent/tests/ingress_owner_no_tools.test.mjs- name: Verify explicit Python execution requests and bounded recoveryrun: node --test ods/extensions/services/pixel-agent/tests/explicit_python_compute.test.mjs+ - name: Verify same-chat source receipts and follow-up execution+ run: node --test ods/extensions/services/pixel-agent/tests/current_source_receipts.test.mjs ods/extensions/services/pixel-agent/tests/prior_source_reuse.test.mjs- name: Verify bounded preview delivery across platformsrun: |node --test ods/extensions/services/pixel-agent/tests/preview_delivery_recovery.test.mjsdiff --git a/ods/extensions/services/pixel-agent/plugin/completion-assurance.mjs b/ods/extensions/services/pixel-agent/plugin/completion-assurance.mjsindex 1cb87c41f..e69e6d1f7 100644--- a/ods/extensions/services/pixel-agent/plugin/completion-assurance.mjs+++ b/ods/extensions/services/pixel-agent/plugin/completion-assurance.mjs@@ -362,10 +362,94 @@ export function portugueseDateHint(ownerText) {return /\b(?:voce|noticias|hoje|consulte|pesquise|busque|procure|crie|arquivo|instale|instalar|baixar|configurar|quais|qual (?:e|o|a|os|as|sao))\b/.test(normalize(ownerText));}-export function createCompletionAssurance({englishOnly = false} = {}) {+// Conservative, English-only policy: the owner explicitly authorizes reusing+// a source/page/documentation page that was already read or found earlier in+// this chat. A bare "official" mention, a request to refresh/reread, or a+// negated/quoted/code/example reuse phrase does not authorize reuse. "Execute+// now" and freshness requests unrelated to sources (e.g. a current balance)+// do not reject an otherwise explicit reuse authorization.+export function ownerAuthorizesPriorSourceReuse(ownerText) {+ const raw = String(ownerText ?? '');+ // Strip fenced code, inline code, block quotes and quoted spans: reuse+ // phrases inside them are examples, not owner instructions.+ const stripped = raw+ .replace(/```[\s\S]*?```/g, ' ')+ .replace(/`[^`\n]*`/g, ' ')+ .replace(/^\s*>.*$/gm, ' ')+ .replace(/"[^"\n]*"/g, ' ')+ .replace(/(^|[\s([{])'[^'\n]*'(?=$|[\s)\]},.!?])/g, '$1 ')+ .replace(/“[^”\n]*”|‘[^’\n]*’/g, ' ');+ const value = normalize(stripped).replaceAll('’', "'");+ if (/^\s*(?:(?:please|could you)\s+)?(?:explain|translate|quote|repeat|define|write an example|give an example)\b/.test(value) ||+ /\b(?:example|phrase|quotation)\b[^.!?\n]{0,30}(?:says?|:)/.test(value)) return false;+ // Explicit refusal to reuse prior sources.+ if (/\b(?:without|do not|don't|never|ignore|discard|forget|no need to)\b[^.!?;\n]{0,100}\b(?:sources?|pages?|docs?|documentation|links?|reus(?:e|ing))\b/.test(value)) return false;+ // Explicit refresh/reread requests override reuse authorization.+ if (/\b(?:refresh|reread|re-read|re-?fetch|re-?open|re-?check)\b[^,.!?;\n]{0,60}\b(?:sources?|pages?|docs?|documentation|links?)\b/.test(value) ||+ /\b(?:latest|newest|up-?to-?date|current)\s+(?:official\s+)?(?:(?:version|versions)\s+of\s+(?:the\s+)?)?(?:sources?|pages?|docs?|documentation|links?)\b/.test(value)) return false;+ if (/\b(?:sources?|pages?|docs?|documentation|links?)\b[^.!?\n]{0,60}\b(?:refresh|reread|re-read|re-?fetch|re-?open|re-?check|latest|newest|up-?to-?date)\b/.test(value)) return false;+ // Explicit affirmative reuse of a source/page/doc already read or found.+ return /\b(?:reuse|re-use|use|cite|reference|rely on)\b[^.!?\n]{0,60}\b(?:sources?|pages?|docs?|documentation|links?)\b[^.!?\n]{0,60}\b(?:you (?:already )?(?:read|found|opened|consulted)|already (?:read|found|opened|consulted)|read earlier|found earlier|opened earlier|consulted earlier|from earlier|from before|previously (?:read|found|opened|consulted))\b/.test(value) ||+ /\b(?:sources?|pages?|docs?|documentation|links?)\b[^.!?\n]{0,60}\b(?:you (?:already )?(?:read|found|opened|consulted)|already (?:read|found|opened|consulted)|read earlier|found earlier|opened earlier|consulted earlier|from earlier|from before|previously (?:read|found|opened|consulted))\b[^.!?\n]{0,60}\b(?:reuse|re-use|use|cite|reference|rely on)\b/.test(value) ||+ /\b(?:the|that|this|same)\b[^.!?\n]{0,40}\b(?:official )?(?:documentation|docs?|page|source)\b[^.!?\n]{0,60}\b(?:you (?:already )?(?:found|read|opened|consulted)|already (?:found|read|opened|consulted)|found earlier|read earlier|opened earlier|consulted earlier)\b/.test(value);+}++export function createCompletionAssurance({englishOnly = false, now = Date.now} = {}) {let initialized = false, research = false, portuguese = false, conversational = false, attempts = 0;+ // Bounded current-run source receipts: URL -> timestamp of the actual+ // successful openedSourceUrls observation. Insertion order is recency order+ // (a re-observation moves the URL to the end and refreshes its timestamp).+ const sourceReceipts = new Map();+ const MAX_SOURCE_RECEIPTS = 32;+ const SOURCE_RECEIPT_MAX_AGE_MS = 30 * 60 * 1000;+ const recordSourceReceipt = url => {+ const at = now();+ if (!Number.isFinite(at)) return;+ sourceReceipts.delete(url);+ sourceReceipts.set(url, at);+ while (sourceReceipts.size > MAX_SOURCE_RECEIPTS) {+ sourceReceipts.delete(sourceReceipts.keys().next().value);+ }+ };let readsRequired = false, attributionAttempts = 0;let sourceReadAttempted = false, workspaceReadReminderIssued = false;+ // Trusted same-chat prior receipts, seeded once at first begin() when the+ // owner explicitly authorizes reuse. Kept separate from current-run+ // receipts: never exported, never counted as current reads, never fed to+ // readPages/synthesisSources/currentRunSourceReceipts.+ const priorReceipts = new Map();+ let priorReuseAuthorized = false, priorSeeded = false, ownerBeganAt;+ const MAX_PRIOR_RECEIPTS = 32;+ const PRIOR_RECEIPT_MAX_AGE_MS = 30 * 60 * 1000;+ const seedPriorReceipts = (list) => {+ if (!Array.isArray(list) || !list.length) return;+ const at = now();+ if (!Number.isFinite(at)) return;+ const seen = new Set();+ for (const entry of list) {+ if (priorReceipts.size >= MAX_PRIOR_RECEIPTS) break;+ const url = citationKey(entry?.url);+ const timestamp = entry?.timestamp;+ if (!url || !Number.isFinite(timestamp) || timestamp > at || timestamp > ownerBeganAt ||+ at - timestamp > PRIOR_RECEIPT_MAX_AGE_MS) continue;+ if (seen.has(url)) continue;+ seen.add(url);+ priorReceipts.set(url, timestamp);+ }+ };+ // Fresh prior receipts at the moment of use: re-filtered for age so a+ // receipt that expired during the turn no longer counts.+ const freshPriorReceipts = () => {+ const at = now();+ if (!Number.isFinite(at)) return new Map();+ const out = new Map();+ for (const [url, timestamp] of priorReceipts) {+ if (timestamp > at) continue;+ if (at - timestamp > PRIOR_RECEIPT_MAX_AGE_MS) continue;+ out.set(url, timestamp);+ }+ return out;+ };let workObserved = false, webObserved = false, terminal, terminalStatus = 'failed';// Run binding. An owner cancel ends this run's assurance for good: no later// revision, host read or replacement text. A run that follows a cancel can@@ -408,16 +492,32 @@ export function createCompletionAssurance({englishOnly = false} = {}) {readPages.push(page);};return {- begin(ownerText, event) {- if (initialized) return;- initialized = true;- ownerRequest = normalize(ownerText).trim();- const precedingRequest = continuedOwnerRequest(ownerText, event);- readsRequired = sourceReadsRequested(ownerText) || sourceReadsRequested(precedingRequest);- research = readsRequired || researchRequested(ownerText) || researchRequested(precedingRequest);- terms = requestTerms(ownerText, precedingRequest);- conversational = /^(?:(?:please|por favor)[,\s]+)?(?:traduza|translate|reescreva|rewrite|repita|repeat|diga apenas|say exactly|responda apenas|return exactly|explique|explain|rascunho|draft|exemplo|example)\b/.test(normalize(ownerText).trim()) && !research;- portuguese = !englishOnly && portugueseDateHint(ownerText);+ begin(ownerText, event, priorReceiptsInput) {+ if (!initialized) {+ initialized = true;+ ownerBeganAt = now();+ ownerRequest = normalize(ownerText).trim();+ const precedingRequest = continuedOwnerRequest(ownerText, event);+ readsRequired = sourceReadsRequested(ownerText) || sourceReadsRequested(precedingRequest);+ research = readsRequired || researchRequested(ownerText) || researchRequested(precedingRequest);+ terms = requestTerms(ownerText, precedingRequest);+ conversational = /^(?:(?:please|por favor)[,\s]+)?(?:traduza|translate|reescreva|rewrite|repita|repeat|diga apenas|say exactly|responda apenas|return exactly|explique|explain|rascunho|draft|exemplo|example)\b/.test(normalize(ownerText).trim()) && !research;+ portuguese = !englishOnly && portugueseDateHint(ownerText);+ priorReuseAuthorized = ownerAuthorizesPriorSourceReuse(ownerText);+ }+ // Native prompt hooks may omit the session key. The first fully bound+ // hook may supply receipts later, but cannot change the owner's intent+ // or import a read performed after this owner turn began.+ if (!priorSeeded && Array.isArray(priorReceiptsInput)) {+ priorSeeded = true;+ if (priorReuseAuthorized) seedPriorReceipts(priorReceiptsInput);+ }+ },+ discardPriorSources() {+ priorSeeded = true;+ priorReceipts.clear();+ if (terminalStatus === 'passed') terminal = undefined;+ terminalStatus = 'failed';},// The owner cancelled this run. Any revision it armed, its replacement// text and any pending host read are void; nothing here outlives it.@@ -450,9 +550,12 @@ export function createCompletionAssurance({englishOnly = false} = {}) {publicSourceUrl(details.url) && event.result.content?.some(block =>block?.type === 'text' && typeof block.text === 'string' && block.text.trim()) &&browserSnapshots.size < 128) browserSnapshots.add(publicSourceUrl(details.url));- for (const url of openedSourceUrls(tool, event.result)) if (opened.size < 128) {- opened.add(url);- sources.add(url);+ for (const url of openedSourceUrls(tool, event.result)) {+ recordSourceReceipt(url);+ if (opened.size < 128) {+ opened.add(url);+ sources.add(url);+ }}recordReadPage(readPageEntry(tool, event.result), typeof event.params?.query === 'string' ? event.params.query : undefined);workObserved = true;@@ -492,6 +595,22 @@ export function createCompletionAssurance({englishOnly = false} = {}) {}},get hostVerifiedSources() { return [...hostVerified]; },+ // Current-run source receipts: cloned fresh entries with timestamp <= now+ // and age <= 30 minutes. Cancelled runs return none. Never includes+ // browser snapshots, host verifications, search-only results, or any+ // imported prior-run receipts.+ currentRunSourceReceipts() {+ if (cancelled) return [];+ const at = now();+ if (!Number.isFinite(at)) return [];+ const out = [];+ for (const [url, timestamp] of sourceReceipts) {+ if (timestamp > at) continue;+ if (at - timestamp > SOURCE_RECEIPT_MAX_AGE_MS) continue;+ out.push({url, timestamp});+ }+ return out;+ },// Pages read successfully in this response: current-run model read// receipts (web_fetch, targeted extraction) and host verifications.get readPages() { return readPages.map(({url, title}) => (title ? {url, title} : {url})); },@@ -510,7 +629,14 @@ export function createCompletionAssurance({englishOnly = false} = {}) {if (cancelled) return;if (conversational) return;const read = readSources();- const unread = readsRequired ? unreadCitations(text, read) : [];+ // Prior receipts may satisfy the cited-page read check only when the+ // owner authorized reuse and the receipt is still fresh. They never+ // enter `read`/`opened`/`sources`; they are consulted only here.+ const prior = freshPriorReceipts();+ const priorKeys = new Set([...prior.keys()].map(citationKey).filter(Boolean));+ const currentKeys = readKeys(read);+ const combinedRead = priorKeys.size ? new Set([...read, ...prior.keys()]) : read;+ const unread = readsRequired ? unreadCitations(text, combinedRead) : [];if (unread.length) {// Never mark the answer successful or leave an unread link presented// as a source. When some cited pages were read, keep the answer with@@ -529,16 +655,22 @@ export function createCompletionAssurance({englishOnly = false} = {}) {: 'Bounded source-read attribution recovery exhausted.'};}const promise = promisesExecution(text);- const missingResearch = research && (!webObserved || sources.size === 0);- const cited = sources.size ? new Set(citationSpans(String(text)).map(span => span.key).filter(Boolean)) : new Set();+ // A cited prior receipt that the owner authorized counts as evidence+ // for the research gate even without a current web observation.+ const citedPrior = priorKeys.size+ ? citationSpans(String(text)).some(span => span.key && priorKeys.has(span.key) && !currentKeys.has(span.key))+ : false;+ const missingResearch = research && (!webObserved || sources.size === 0) && !citedPrior;+ const cited = new Set(citationSpans(String(text)).map(span => span.key).filter(Boolean));// Returned web evidence normally answers this run's owner request. After// a cancel it may answer the withdrawn one instead (tower1 r067: a// conversational reply was revised into the cancelled research). Only// the current request, or a resend of the withdrawn one, binds it.const evidenceBound = !withdrawnRequest || research || readsRequired ||Boolean(withdrawnRequest.text && withdrawnRequest.text === ownerRequest);- const missingCitations = evidenceBound && sources.size > 0 && ![...sources, ...hostVerified].some(url => text.includes(url) ||- text.includes(url.replaceAll('(', '%28').replaceAll(')', '%29')) || cited.has(citationKey(url)));+ const missingCitations = evidenceBound && (sources.size > 0 || priorKeys.size > 0) &&+ ![...sources, ...hostVerified, ...prior.keys()].some(url => text.includes(url) ||+ text.includes(url.replaceAll('(', '%28').replaceAll(')', '%29')) || cited.has(citationKey(url)));// A candid failure or clarification is a valid terminal answer. It must// not be turned into another attempt that repeats denied work.// "If a tool is unavailable, I will report it" is a contingency, not@@ -550,7 +682,22 @@ export function createCompletionAssurance({englishOnly = false} = {}) {(/\?/.test(text) && /\b(?:posso|autoriza|confirma|may i|can i|would you|please confirm)\b/.test(normalize(text)));// After partial work a short promise still isn't a delivered result.const promiseOnly = promise && (!workObserved || (text.length < 900 && !/https?:\/\//.test(text)));- if ((!missingResearch && !promiseOnly && !missingCitations) || limitation) { terminal = undefined; return; }+ if ((!missingResearch && !promiseOnly && !missingCitations) || limitation) {+ // When the answer passes every gate and cites at least one prior+ // receipt that was not reread this turn, attach a fixed short note so+ // the owner sees the historical provenance. If every cited prior URL+ // was reread this turn, no note is added.+ const citedPriorOnly = priorKeys.size+ ? citationSpans(String(text)).some(span => span.key && priorKeys.has(span.key) && !currentKeys.has(span.key))+ : false;+ if (!limitation && citedPriorOnly) {+ terminalStatus = 'passed';+ terminal = `${text}\n\n**Source check:** Some cited pages were read earlier in this chat and were not reread this turn.`;+ return {action:'finalize', reason:'Answer passed with prior same-chat source receipts.'};+ }+ terminal = undefined;+ return;+ }// The harness may refuse a revision after a side effect. Arm truthful// delivery now, and clear it only if a later final answer passes.const attributionOnly = missingCitations && !promiseOnly && !missingResearch &&diff --git a/ods/extensions/services/pixel-agent/plugin/tool-loop-guard.mjs b/ods/extensions/services/pixel-agent/plugin/tool-loop-guard.mjsindex cc623fc1e..d7327f1e2 100644--- a/ods/extensions/services/pixel-agent/plugin/tool-loop-guard.mjs+++ b/ods/extensions/services/pixel-agent/plugin/tool-loop-guard.mjs@@ -8258,7 +8258,7 @@ export function createToolLoopGuard({if (!state) {pruneRuns();state = {- completionAssurance: createCompletionAssurance({englishOnly: englishOnly(responseLanguage)}),+ completionAssurance: createCompletionAssurance({englishOnly: englishOnly(responseLanguage), now}),extensionCompletionGate: undefined,workspaceLaneRequested: false,workspaceExtensionIsolated: false,@@ -10764,7 +10764,29 @@ export function createToolLoopGuard({// its successor. Each admitted call has its own private scope object.state.artifactReservations = new WeakSet();}- state.completionAssurance.begin(currentOwnerIntentText(event?.messages, event?.prompt), event);+ // Page receipts cross turns only within this trusted owner chat. The+ // assurance object freezes the first owner intent and receipt selection.+ let priorSourceReceipts;+ if (!teamRole && state.artifactOwnerInteractive && !state.managedTeamWorker &&+ !state.clientCancelled && !state.odsAborted && !state.recursiveDeleteDenied &&+ artifactContext.agentId === agentId && artifactContext.sessionId && artifactContext.sessionKey) {+ priorSourceReceipts = [];+ for (const [otherRunId, other] of runs) {+ if (otherRunId === runId || !other.artifactOwnerInteractive || other.managedTeamWorker ||+ other.artifactIdentityConflict || other.artifactNoninteractiveObserved ||+ other.clientCancelled || other.odsAborted || other.recursiveDeleteDenied ||+ other.artifactOwnerContext?.agentId !== agentId ||+ other.currentSessionId !== artifactContext.sessionId ||+ other.currentSessionKey !== artifactContext.sessionKey) continue;+ priorSourceReceipts.push(...other.completionAssurance.currentRunSourceReceipts());+ }+ priorSourceReceipts.sort((a, b) => b.timestamp - a.timestamp);+ }+ if (teamRole || state.managedTeamWorker || state.artifactIdentityConflict ||+ state.artifactNoninteractiveObserved || state.clientCancelled || state.odsAborted ||+ state.recursiveDeleteDenied) state.completionAssurance.discardPriorSources();+ state.completionAssurance.begin(currentOwnerIntentText(event?.messages, event?.prompt), event,+ priorSourceReceipts);const ownerIntent=currentOwnerIntentText(event?.messages,event?.prompt);if (ownerIntent && state.ownerNoTools === undefined) state.ownerNoTools=ownerForbidsTools(ownerIntent);const pictureIds=attachedPictureIds(currentUserText(event?.messages,event?.prompt));@@ -13757,6 +13779,13 @@ export function createToolLoopGuard({// Before the reply, so the owner's notice can travel with it.recordExampleOutcome({...context, runId}, examples.check(runId));const state = runs.get(runId);+ // A final hook cannot carry accepted history across an identity change.+ if (state && (state.artifactIdentityConflict || state.artifactNoninteractiveObserved ||+ (context.trigger && context.trigger !== 'user') ||+ [context, event].some(value =>+ (value?.sessionId && value.sessionId !== state.artifactOwnerContext?.sessionId) ||+ (value?.sessionKey && value.sessionKey !== state.artifactOwnerContext?.sessionKey) ||+ (value?.runId && value.runId !== runId)))) state.completionAssurance.discardPriorSources();// The agent's latest final reply. A failed preview delivery keeps it ahead// of the host facts (preview-explanation.mjs). The pinned harness refuses a// revision after side effects, so this text is usually what is sent; adiff --git a/ods/extensions/services/pixel-agent/tests/current_source_receipts.test.mjs b/ods/extensions/services/pixel-agent/tests/current_source_receipts.test.mjsnew file mode 100644index 000000000..0c4e4ff75--- /dev/null+++ b/ods/extensions/services/pixel-agent/tests/current_source_receipts.test.mjs@@ -0,0 +1,327 @@+import test from 'node:test';+import assert from 'node:assert/strict';+import { createCompletionAssurance } from '../plugin/completion-assurance.mjs';++// Helpers to build realistic tool events matching the shapes the plugin+// actually observes. These mirror the fields openedSourceUrls()/observe()+// read, so the tests exercise the real receipt path rather than constants.++function webFetchEvent({ url, finalUrl, status = 200, text = 'page body', title } = {}) {+ return {+ params: { url },+ result: {+ details: {+ status,+ url,+ finalUrl: finalUrl ?? url,+ text,+ ...(title !== undefined ? { title } : {}),+ },+ content: [{ type: 'text', text }],+ },+ };+}++function webExtractEvent({ sourceUrl, matched = true, mode, text = 'extracted body' } = {}) {+ return {+ params: { url: sourceUrl },+ result: {+ details: {+ boundary: 'public-web-read-only',+ matched,+ ...(mode !== undefined ? { mode } : {}),+ source_url: sourceUrl,+ },+ content: [{ type: 'text', text }],+ },+ };+}++function failedFetchEvent({ url, status = 500 } = {}) {+ return {+ params: { url },+ result: {+ details: { status, url, finalUrl: url, text: '' },+ content: [],+ },+ };+}++function searchEvent({ urls = [] } = {}) {+ return {+ params: { query: 'q' },+ result: {+ details: { results: urls.map(url => ({ url })) },+ content: [{ type: 'text', text: JSON.stringify({ results: urls.map(url => ({ url })) }) }],+ },+ };+}++function browserSnapshotEvent({ url, ok = true, status = 200, text = 'snapshot body' } = {}) {+ return {+ params: { action: 'snapshot' },+ result: {+ details: { ok, status, url },+ content: [{ type: 'text', text }],+ },+ };+}++function makeClock(start = 1_000_000) {+ let t = start;+ const now = () => t;+ now.advance = ms => { t += ms; };+ now.set = v => { t = v; };+ return now;+}++function freshAssurance(opts = {}) {+ const clock = opts.clock ?? makeClock();+ const assurance = createCompletionAssurance({ now: clock, ...opts });+ assurance.begin('please read the sources and summarize', {});+ return { assurance, clock };+}++test('successful web_fetch records a current-run source receipt', () => {+ const { assurance, clock } = freshAssurance();+ assurance.observe('web_fetch', webFetchEvent({ url: 'https://example.com/a' }));+ const receipts = assurance.currentRunSourceReceipts();+ assert.equal(receipts.length, 1);+ assert.equal(receipts[0].url, 'https://example.com/a');+ assert.equal(receipts[0].timestamp, clock());+});++test('successful pixel_ods_web_extract records a receipt', () => {+ const { assurance } = freshAssurance();+ assurance.observe('pixel_ods_web_extract', webExtractEvent({ sourceUrl: 'https://example.com/extract' }));+ const receipts = assurance.currentRunSourceReceipts();+ assert.equal(receipts.length, 1);+ assert.equal(receipts[0].url, 'https://example.com/extract');+});++test('web_extract overview mode also records a receipt', () => {+ const { assurance } = freshAssurance();+ assurance.observe('pixel_ods_web_extract', webExtractEvent({ sourceUrl: 'https://example.com/overview', matched: false, mode: 'overview' }));+ const receipts = assurance.currentRunSourceReceipts();+ assert.equal(receipts.length, 1);+ assert.equal(receipts[0].url, 'https://example.com/overview');+});++test('failed fetch (non-2xx) does not record a receipt', () => {+ const { assurance } = freshAssurance();+ assurance.observe('web_fetch', failedFetchEvent({ url: 'https://example.com/fail', status: 500 }));+ assert.deepEqual(assurance.currentRunSourceReceipts(), []);+});++test('failed fetch (empty text) does not record a receipt', () => {+ const { assurance } = freshAssurance();+ assurance.observe('web_fetch', webFetchEvent({ url: 'https://example.com/empty', text: ' ' }));+ assert.deepEqual(assurance.currentRunSourceReceipts(), []);+});++test('search results alone do not record receipts', () => {+ const { assurance } = freshAssurance();+ assurance.observe('web_search', searchEvent({ urls: ['https://example.com/lead1', 'https://example.com/lead2'] }));+ assert.deepEqual(assurance.currentRunSourceReceipts(), []);+});++test('browser snapshot does not record a source receipt', () => {+ const { assurance } = freshAssurance();+ assurance.observe('browser', browserSnapshotEvent({ url: 'https://example.com/browser' }));+ assert.deepEqual(assurance.currentRunSourceReceipts(), []);+});++test('host verification does not record a source receipt', () => {+ const { assurance } = freshAssurance();+ assurance.observeHostVerification('https://example.com/host-verified');+ assert.deepEqual(assurance.currentRunSourceReceipts(), []);+});++test('web_extract without matched/overview does not record a receipt', () => {+ const { assurance } = freshAssurance();+ assurance.observe('pixel_ods_web_extract', webExtractEvent({ sourceUrl: 'https://example.com/nomatch', matched: false }));+ assert.deepEqual(assurance.currentRunSourceReceipts(), []);+});++test('returned receipts are cloned, not live references', () => {+ const { assurance } = freshAssurance();+ assurance.observe('web_fetch', webFetchEvent({ url: 'https://example.com/clone' }));+ const first = assurance.currentRunSourceReceipts();+ first[0].url = 'https://evil.example/mutated';+ first[0].timestamp = -1;+ first.push({ url: 'https://evil.example/extra', timestamp: 0 });+ const second = assurance.currentRunSourceReceipts();+ assert.equal(second.length, 1);+ assert.equal(second[0].url, 'https://example.com/clone');+ assert.notEqual(second[0].timestamp, -1);+});++test('receipts are capped at 32 with most-recent-unique eviction', () => {+ const { assurance } = freshAssurance();+ for (let i = 0; i < 40; i++) {+ assurance.observe('web_fetch', webFetchEvent({ url: `https://example.com/page-${i}` }));+ }+ const receipts = assurance.currentRunSourceReceipts();+ assert.equal(receipts.length, 32);+ // The oldest 8 (page-0..page-7) were evicted; page-8..page-39 remain.+ assert.equal(receipts[0].url, 'https://example.com/page-8');+ assert.equal(receipts.at(-1).url, 'https://example.com/page-39');+});++test('re-observing a URL refreshes its timestamp and moves it to the end', () => {+ const { assurance, clock } = freshAssurance();+ assurance.observe('web_fetch', webFetchEvent({ url: 'https://example.com/first' }));+ const t1 = clock();+ clock.advance(1000);+ assurance.observe('web_fetch', webFetchEvent({ url: 'https://example.com/second' }));+ clock.advance(1000);+ assurance.observe('web_fetch', webFetchEvent({ url: 'https://example.com/first' }));+ const receipts = assurance.currentRunSourceReceipts();+ assert.equal(receipts.length, 2);+ assert.equal(receipts[0].url, 'https://example.com/second');+ assert.equal(receipts[1].url, 'https://example.com/first');+ assert.ok(receipts[1].timestamp > t1);+ assert.equal(receipts[1].timestamp, clock());+});++test('re-observing a URL does not grow the receipt set', () => {+ const { assurance } = freshAssurance();+ for (let i = 0; i < 5; i++) {+ assurance.observe('web_fetch', webFetchEvent({ url: 'https://example.com/same' }));+ }+ assert.equal(assurance.currentRunSourceReceipts().length, 1);+});++test('receipts older than 30 minutes are excluded', () => {+ const { assurance, clock } = freshAssurance();+ assurance.observe('web_fetch', webFetchEvent({ url: 'https://example.com/old' }));+ clock.advance(30 * 60 * 1000 + 1);+ assert.deepEqual(assurance.currentRunSourceReceipts(), []);+});++test('receipts exactly at the 30-minute boundary are still included', () => {+ const { assurance, clock } = freshAssurance();+ assurance.observe('web_fetch', webFetchEvent({ url: 'https://example.com/boundary' }));+ clock.advance(30 * 60 * 1000);+ const receipts = assurance.currentRunSourceReceipts();+ assert.equal(receipts.length, 1);+ assert.equal(receipts[0].url, 'https://example.com/boundary');+});++test('receipts with a future timestamp are excluded', () => {+ const clock = makeClock(1_000_000);+ const assurance = createCompletionAssurance({ now: clock });+ assurance.begin('please read the sources', {});+ assurance.observe('web_fetch', webFetchEvent({ url: 'https://example.com/future' }));+ // Rewind the clock so the recorded timestamp is now in the future.+ clock.set(1_000_000 - 1);+ assert.deepEqual(assurance.currentRunSourceReceipts(), []);+});++test('a cancelled run returns no receipts', () => {+ const { assurance } = freshAssurance();+ assurance.observe('web_fetch', webFetchEvent({ url: 'https://example.com/cancelled' }));+ assert.equal(assurance.currentRunSourceReceipts().length, 1);+ assurance.cancel();+ assert.deepEqual(assurance.currentRunSourceReceipts(), []);+});++test('a non-finite clock yields no receipts', () => {+ const clock = makeClock(1_000_000);+ const assurance = createCompletionAssurance({ now: clock });+ assurance.begin('please read the sources', {});+ assurance.observe('web_fetch', webFetchEvent({ url: 'https://example.com/nan' }));+ clock.set(NaN);+ assert.deepEqual(assurance.currentRunSourceReceipts(), []);+});++test('receipts are not shared across assurance instances', () => {+ const a = freshAssurance();+ a.assurance.observe('web_fetch', webFetchEvent({ url: 'https://example.com/instance-a' }));+ const b = freshAssurance();+ assert.deepEqual(b.assurance.currentRunSourceReceipts(), []);+ assert.equal(a.assurance.currentRunSourceReceipts().length, 1);+});++test('web_fetch with a private/localhost URL records no receipt', () => {+ const { assurance } = freshAssurance();+ assurance.observe('web_fetch', webFetchEvent({ url: 'http://localhost/secret' }));+ assurance.observe('web_fetch', webFetchEvent({ url: 'http://service.internal/secret' }));+ assert.deepEqual(assurance.currentRunSourceReceipts(), []);+});++test('web_fetch with a non-http(s) URL records no receipt', () => {+ const { assurance } = freshAssurance();+ assurance.observe('web_fetch', webFetchEvent({ url: 'ftp://example.com/file' }));+ assert.deepEqual(assurance.currentRunSourceReceipts(), []);+});++test('a failed event (event.error) records no receipt', () => {+ const { assurance } = freshAssurance();+ const event = webFetchEvent({ url: 'https://example.com/errored' });+ event.error = new Error('boom');+ assurance.observe('web_fetch', event);+ assert.deepEqual(assurance.currentRunSourceReceipts(), []);+});++test('a result flagged isError records no receipt', () => {+ const { assurance } = freshAssurance();+ const event = webFetchEvent({ url: 'https://example.com/iserror' });+ event.result.isError = true;+ assurance.observe('web_fetch', event);+ assert.deepEqual(assurance.currentRunSourceReceipts(), []);+});++test('a details.status of failed/error/blocked records no receipt', () => {+ const { assurance } = freshAssurance();+ for (const status of ['failed', 'error', 'blocked', 'unavailable', 'invalid_request']) {+ const event = webFetchEvent({ url: `https://example.com/${status}` });+ event.result.details.status = status;+ assurance.observe('web_fetch', event);+ }+ assert.deepEqual(assurance.currentRunSourceReceipts(), []);+});++test('tool_call wrapper envelopes do not record receipts', () => {+ const { assurance } = freshAssurance();+ const inner = webFetchEvent({ url: 'https://example.com/wrapped' });+ assurance.observe('tool_call', inner);+ assert.deepEqual(assurance.currentRunSourceReceipts(), []);+});++test('discovery tools do not record receipts', () => {+ const { assurance } = freshAssurance();+ assurance.observe('tool_search', webFetchEvent({ url: 'https://example.com/discovery' }));+ assurance.observe('tool_describe', webFetchEvent({ url: 'https://example.com/discovery2' }));+ assert.deepEqual(assurance.currentRunSourceReceipts(), []);+});++test('a mix of successes and failures records only the successes', () => {+ const { assurance } = freshAssurance();+ assurance.observe('web_fetch', webFetchEvent({ url: 'https://example.com/ok-1' }));+ assurance.observe('web_fetch', failedFetchEvent({ url: 'https://example.com/bad-1' }));+ assurance.observe('web_search', searchEvent({ urls: ['https://example.com/lead'] }));+ assurance.observe('web_fetch', webFetchEvent({ url: 'https://example.com/ok-2' }));+ assurance.observe('browser', browserSnapshotEvent({ url: 'https://example.com/snap' }));+ const receipts = assurance.currentRunSourceReceipts();+ assert.deepEqual(receipts.map(r => r.url), ['https://example.com/ok-1', 'https://example.com/ok-2']);+});++test('web_fetch finalUrl is recorded alongside the requested url', () => {+ const { assurance } = freshAssurance();+ assurance.observe('web_fetch', webFetchEvent({+ url: 'https://example.com/redirect',+ finalUrl: 'https://example.com/final',+ }));+ const urls = assurance.currentRunSourceReceipts().map(r => r.url).sort();+ assert.deepEqual(urls, ['https://example.com/final', 'https://example.com/redirect']);+});++test('receipts survive a later failed observation of the same URL', () => {+ const { assurance } = freshAssurance();+ assurance.observe('web_fetch', webFetchEvent({ url: 'https://example.com/flaky' }));+ assurance.observe('web_fetch', failedFetchEvent({ url: 'https://example.com/flaky', status: 503 }));+ const receipts = assurance.currentRunSourceReceipts();+ assert.equal(receipts.length, 1);+ assert.equal(receipts[0].url, 'https://example.com/flaky');+});diff --git a/ods/extensions/services/pixel-agent/tests/prior_source_reuse.test.mjs b/ods/extensions/services/pixel-agent/tests/prior_source_reuse.test.mjsnew file mode 100644index 000000000..0daaa3c23--- /dev/null+++ b/ods/extensions/services/pixel-agent/tests/prior_source_reuse.test.mjs@@ -0,0 +1,188 @@+import test from 'node:test';+import assert from 'node:assert/strict';+import {createToolLoopGuard} from '../plugin/tool-loop-guard.mjs';+import {createCompletionAssurance, ownerAuthorizesPriorSourceReuse} from '../plugin/completion-assurance.mjs';++const url='https://docs.python.org/3/library/decimal.html#rounding-modes-2';+const user='ods-'+'d'.repeat(64);+const command=`python3 -c "from decimal import Decimal, ROUND_HALF_UP; print(Decimal('1.005').quantize(Decimal('0.01'), rounding=ROUND_HALF_UP))"`;+const prompt=`Continue and execute this exact terminal command now: ${command}. Report the observed stdout and the official documentation page you already found.`;+const answer=`Observed stdout: 1.01. Official documentation: ${url}`;+function fixture() {+ let time=1_000_000;+ const guard=createToolLoopGuard({now:()=>time,+ abortRunAndDrain:async()=>({aborted:true,drained:true,forceCleared:false})});+ const context=(runId, overrides={})=>({agentId:'pixel',runId,sessionId:'decimal-session',+ sessionKey:'agent:pixel:openai-user:'+user,trigger:'user',...overrides});+ const begin=(runId, text=prompt, overrides={})=>{+ const c=context(runId,overrides);guard.observeRun(c,'pixel',{prompt:text});return c;+ };+ const tool=(c,name,params,result,id=name)=>{+ const scoped={...c,toolName:name,toolCallId:id};+ const prepared=guard.beforeToolCall({toolName:name,params},scoped);+ assert.notEqual(prepared?.block,true,prepared?.blockReason);+ guard.afterToolCall({toolName:name,params:prepared?.params??params,result},scoped);+ };+ const read=(c,source=url,status=200)=>tool(c,'web_fetch',{url:source},+ {details:{status,url:source,finalUrl:source,text:'ROUND_HALF_UP rounds ties away from zero.'},+ content:[{type:'text',text:'Decimal rounding documentation.'}]});+ const exec=c=>tool(c,'exec',{command,workdir:'/workspace'},+ {details:{status:'completed',exitCode:0,aggregated:'1.01'}});+ const finish=(c,text=answer)=>{+ const decision=guard.beforeAgentFinalize({runId:c.runId,lastAssistantMessage:text,+ messages:[{role:'assistant',content:[{type:'text',text}]}]},c);+ return {decision,delivery:guard.deliveryVerificationForRun(c.runId)};+ };+ const first=()=>{+ const c=begin('first','Search the web for the official Python Decimal rounding documentation. Then execute Python.');+ read(c);guard.observeAgentEnd({success:false},c);return c;+ };+ return {guard,context,begin,tool,read,exec,finish,first,advance:ms=>{time+=ms;},now:()=>time};+}+function passed(result) {+ assert.notEqual(result.decision?.action,'revise',JSON.stringify(result.decision));+ assert.equal(result.delivery.status,'passed',JSON.stringify(result.delivery));+ assert.match(result.delivery.text,/1\.01/);+ assert.match(result.delivery.text,/read earlier in this chat and were not reread this turn/);+}+function failed(result) {assert.equal(result.delivery.status,'failed',JSON.stringify(result));}++test('the real two-turn Decimal request preserves successful stdout and the previously read page',()=>{+ const f=fixture();f.first();const c=f.begin('second');f.exec(c);passed(f.finish(c));+});+test('same source identity accepts fragments and trailing slashes without another read',()=>{+ const f=fixture();const c1=f.begin('first','Read official documentation.');+ f.read(c1,'https://docs.python.org/3/library/decimal.html/');+ f.guard.observeAgentEnd({success:true},c1);+ const c2=f.begin('second');f.exec(c2);passed(f.finish(c2));+});+test('a successful source read does not stand in for execution, including failed commands',()=>{+ for(const executed of [false,true]) {+ const f=fixture();f.first();const c=f.begin('second');+ if(executed)f.tool(c,'exec',{command,workdir:'/workspace'},+ {details:{status:'completed',exitCode:1,aggregated:'Python failed'}});+ failed(f.finish(c));+ }+});+test('different chats or owner session keys cannot import a prior receipt',()=>{+ for(const overrides of [{sessionId:'other-chat'},+ {sessionKey:'agent:pixel:openai-user:ods-'+'e'.repeat(64)}]) {+ const f=fixture();f.first();const c=f.begin('second',prompt,overrides);f.exec(c);failed(f.finish(c));+ }+});+test('a conflicting identity after seeding cannot keep historical source acceptance',()=>{+ const f=fixture();f.first();const c=f.begin('second');f.exec(c);+ f.guard.observeRun({...c,sessionId:'changed-chat'},'pixel',{prompt});+ failed(f.finish({...c,sessionId:'changed-chat'}));+});+test('a final hook with a different session cannot keep historical source acceptance',()=>{+ const f=fixture();f.first();const c=f.begin('second');f.exec(c);+ failed(f.finish({...c,sessionId:'changed-chat'}));+});+test('a native prompt hook without a session key can bind once at the first tool hook',()=>{+ const f=fixture();f.first();const c=f.context('second');+ f.guard.observeRun({...c,sessionKey:undefined},'pixel',{prompt});+ f.exec(c);passed(f.finish(c));+});+test('noninteractive prior turns cannot supply an owner-chat receipt',()=>{+ const f=fixture();const c1=f.begin('first','Read official documentation.',{trigger:'cron'});+ f.read(c1);f.guard.observeAgentEnd({success:true},c1);+ const c2=f.begin('second');f.exec(c2);failed(f.finish(c2));+});+test('a failed page fetch or search result cannot become a historical read',()=>{+ for(const kind of ['failed','search']) {+ const f=fixture();const c1=f.begin('first','Read official documentation.');+ if(kind==='failed')f.read(c1,url,500);+ else f.tool(c1,'web_search',{query:'Python Decimal'},+ {content:[{type:'text',text:JSON.stringify({results:[{url}]})}],details:{results:[{url}]}});+ f.guard.observeAgentEnd({success:true},c1);+ const c2=f.begin('second');f.exec(c2);failed(f.finish(c2));+ }+});+test('cancelling the prior run withdraws its source receipts',async()=>{+ const f=fixture();const c1=f.begin('first','Read official documentation.');f.read(c1);+ assert.equal(await f.guard.abortUserRun(user),true);+ const c2=f.begin('second');f.exec(c2);failed(f.finish(c2));+});+test('cancelling the current run prevents a successful historical-source delivery',async()=>{+ const f=fixture();f.first();const c=f.begin('second');f.exec(c);passed(f.finish(c));+ assert.equal(await f.guard.abortUserRun(user),true);+ assert.notEqual(f.guard.deliveryVerificationForRun(c.runId).status,'passed');+});+test('receipt age expires before or during a follow-up and is not renewed by an intervening answer',()=>{+ for(const delayBefore of [31,29]) {+ const f=fixture();f.first();f.advance(delayBefore*60_000);const c=f.begin('second');+ if(delayBefore===29)f.advance(2*60_000);+ f.exec(c);failed(f.finish(c));+ }+ const f=fixture();f.first();f.advance(20*60_000);const c2=f.begin('second');f.exec(c2);+ passed(f.finish(c2));f.guard.observeAgentEnd({success:true},c2);+ f.advance(20*60_000);const c3=f.begin('third');f.exec(c3);failed(f.finish(c3));+});+test('fresh research and ordinary official-source requests do not implicitly reuse history',()=>{+ for(const request of [+ `Execute ${command}. Report stdout and the official documentation.`,+ `Execute ${command}. Reread the official documentation page you already found.`,+ `Execute ${command}. Do not use the official documentation page you already found.`,+ ]) {const f=fixture();f.first();const c=f.begin('second',request);f.exec(c);failed(f.finish(c));}+});+test('an extra unread citation still prevents successful delivery',()=>{+ const f=fixture();f.first();const c=f.begin('second');f.exec(c);+ failed(f.finish(c,answer+' Another source: https://example.com/unread.'));+});+test('same-run prompt retries cannot replace the original authorization or receipt selection',()=>{+ const f=fixture();f.first();const c=f.begin('second',`Execute ${command}. Report stdout and the official documentation.`);+ f.guard.observeRun(c,'pixel',{prompt});f.exec(c);failed(f.finish(c));+ const a=createCompletionAssurance({now:()=>100});+ a.begin(prompt,{},[]);+ a.begin(prompt,{},[{url,timestamp:99}]);+ assert.equal(a.finalize(answer)?.action,'revise');+ assert.equal(a.terminalStatus,'failed');+});+test('late identity binding cannot import later reads or change original owner intent',()=>{+ let time=100;const a=createCompletionAssurance({now:()=>time});a.begin(prompt,{});+ time=102;a.begin(prompt,{},[{url,timestamp:101}]);+ assert.equal(a.finalize(answer)?.action,'revise');+ const b=createCompletionAssurance({now:()=>100});+ b.begin('Report the official documentation.',{});+ b.begin(prompt,{},[{url,timestamp:99}]);+ assert.equal(b.finalize(answer)?.action,'revise');+});+test('imported receipts stay separate from current reads and source exports',()=>{+ const a=createCompletionAssurance({now:()=>100});a.begin(prompt,{},[{url,timestamp:99}]);+ assert.equal(a.finalize(answer)?.action,'finalize');+ assert.deepEqual(a.currentRunSourceReceipts(),[]);+ assert.deepEqual(a.readPages,[]);assert.deepEqual(a.synthesisSources(),[]);+ assert.deepEqual(a.unverifiedCitations(answer),['https://docs.python.org/3/library/decimal.html']);+});+test('prior receipt selection is bounded to the newest 32 supplied unique valid URLs',()=>{+ const receipts=Array.from({length:40},(_,i)=>({url:`https://example.com/page-${i}`,timestamp:100-i}));+ const make=()=>{const a=createCompletionAssurance({now:()=>100});a.begin('Use the official sources you read earlier.',{},receipts);return a;};+ const accepted=make();assert.equal(accepted.finalize('Verified answer https://example.com/page-31')?.action,'finalize');+ assert.equal(accepted.terminalStatus,'passed');+ const excluded=make();assert.equal(excluded.finalize('Verified answer https://example.com/page-32')?.action,'revise');+ assert.equal(excluded.terminalStatus,'failed');+});+test('rereading the cited page this turn removes the historical-source note',()=>{+ const f=fixture();f.first();const c=f.begin('second');f.read(c);f.exec(c);+ const result=f.finish(c);assert.notEqual(result.decision?.action,'revise');+ assert.doesNotMatch(result.delivery.text??'',/not reread this turn/);+});+test('reuse intent excludes quoted examples and source refreshes while allowing execution now',()=>{+ for(const text of [prompt,'Compute the current balance, use docs read earlier.','Use the sources you read earlier.'])+ assert.equal(ownerAuthorizesPriorSourceReuse(text),true,text);+ for(const text of [+ 'Do not use the official documentation page you already found.',+ 'Without reusing the sources you already found, answer.',+ 'Ignore the documentation page you already found.',+ 'The example says: reuse the sources you read earlier.',+ 'Explain the phrase reuse the sources you read earlier.',+ 'The docs say "reuse the sources you read earlier".',+ "'Reuse the sources you read earlier.' Explain this.",+ '“Reuse the sources you read earlier.” Explain this.',+ '```\nReuse the sources you read earlier.\n```',+ '> Reuse the sources you read earlier.',+ 'Reuse the sources you read earlier and fetch the latest documentation.',+ 'Use the official documentation.',+ ])assert.equal(ownerAuthorizesPriorSourceReuse(text),false,text);+});