Skip to content
Closed
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 43 additions & 22 deletions test/e2e/live/common-egress-agent-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export interface PersonalStockToolEvidenceArtifact {
export interface NvdaPersonalStockReply {
as_of: string;
price: number;
source_timestamp: string;
source_url: string;
status: "NVDA_PERSONAL_AGENT_OK";
symbol: "NVDA";
Expand Down Expand Up @@ -448,25 +449,34 @@ export function reduceOpenClawToolEvidence(
}
return false;
};
const dateMatches = (text: string, expected: string): boolean => {
const expectedMs = Date.parse(expected);
if (!Number.isFinite(expectedMs)) return false;
const expectedDate = new Date(expectedMs).toISOString().slice(0, 10);
if (
text.includes(expectedDate) ||
text.includes(expectedDate.replace(/-/gu, "")) ||
text.includes(expected)
) {
return true;
const timestampMatches = (text: string, sourceTimestamp: string, asOf: string): boolean => {
const asOfMs = Date.parse(asOf);
if (!Number.isFinite(asOfMs)) return false;
if (/^\d{4}-\d{2}-\d{2}$/u.test(sourceTimestamp)) {
const dateTokenMatches = (candidate: string): boolean =>
new RegExp(`(?:^|[^0-9])${candidate}(?![0-9]|T|\\s+\\d{2}:)`, "u").test(text);
return asOf === sourceTimestamp && dateTokenMatches(sourceTimestamp);
}
for (const match of text.matchAll(/(?:^|\D)(\d{10}|\d{13})(?!\d)/gu)) {
const raw = match[1];
if (!raw) continue;
const epochMs = Number(raw) * (raw.length === 10 ? 1_000 : 1);
if (!Number.isFinite(epochMs)) continue;
if (new Date(epochMs).toISOString().slice(0, 10) === expectedDate) return true;
if (/^\d{8}$/u.test(sourceTimestamp)) {
const sourceDate = `${sourceTimestamp.slice(0, 4)}-${sourceTimestamp.slice(4, 6)}-${sourceTimestamp.slice(6, 8)}`;
return (
asOf === sourceDate && new RegExp(`(?:^|[^0-9])${sourceTimestamp}(?![0-9])`, "u").test(text)
);
}
return false;
if (/^(?:\d{10}|\d{13})$/u.test(sourceTimestamp)) {
const epochMs = Number(sourceTimestamp) * (sourceTimestamp.length === 10 ? 1_000 : 1);
return (
Number.isFinite(epochMs) &&
epochMs === asOfMs &&
new RegExp(`(?:^|\\D)${sourceTimestamp}(?!\\d)`, "u").test(text)
);
}
const sourceMs = Date.parse(sourceTimestamp);
if (!Number.isFinite(sourceMs) || sourceMs !== asOfMs) return false;
const escapedSource = sourceTimestamp.replace(/[.*+?^${}()|[\]\\]/gu, "\\$&");
return new RegExp(`(?:^|[^0-9A-Za-z_.:+-])${escapedSource}(?![0-9A-Za-z_.:+-])`, "u").test(
text,
);
};
const fingerprint = (value: string): string => {
let hash = 2_166_136_261;
Expand Down Expand Up @@ -598,12 +608,16 @@ export function reduceOpenClawToolEvidence(
const expectedSourceUrl = normalizedUrl(expectedStock?.source_url);
const expectedPrice = expectedStock?.price;
const expectedAsOf = expectedStock?.as_of;
const expectedSourceTimestamp = expectedStock?.source_timestamp;
const expectedStockFingerprint =
expectedSourceUrl !== null &&
typeof expectedPrice === "number" &&
Number.isFinite(expectedPrice) &&
typeof expectedAsOf === "string"
? fingerprint(JSON.stringify([expectedSourceUrl, expectedPrice, expectedAsOf]))
typeof expectedAsOf === "string" &&
typeof expectedSourceTimestamp === "string"
? fingerprint(
JSON.stringify([expectedSourceUrl, expectedPrice, expectedSourceTimestamp, expectedAsOf]),
)
: null;
const nativeExtractors = new Set(["cf-markdown", "json", "raw", "raw-html", "readability"]);
const pairedProjectedTargetCallIds = new Set<string>();
Expand Down Expand Up @@ -643,8 +657,10 @@ export function reduceOpenClawToolEvidence(
}
webFetchResults.push({
asOfMatches:
typeof expectedAsOf === "string" && boundedPayloadText
? dateMatches(boundedPayloadText, expectedAsOf)
typeof expectedSourceTimestamp === "string" &&
typeof expectedAsOf === "string" &&
boundedPayloadText
? timestampMatches(boundedPayloadText, expectedSourceTimestamp, expectedAsOf)
: false,
directFetch,
httpSuccess,
Expand Down Expand Up @@ -918,6 +934,11 @@ export function parseNvdaPersonalStockReply(raw: string): NvdaPersonalStockReply
typeof parsed.source_url === "string" &&
parsed.source_url.length <= 4096 &&
isPublicHttpsTarget(targetFromReplyUrl(parsed.source_url)) &&
typeof parsed.source_timestamp === "string" &&
parsed.source_timestamp.length <= 128 &&
/^(?:\d{8}|\d{10}|\d{13}|\d{4}-\d{2}-\d{2}(?:T\d{2}:\d{2}(?::\d{2}(?:\.\d{1,9})?)?(?:Z|[+-]\d{2}:\d{2}))?)$/u.test(
parsed.source_timestamp,
) &&
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
typeof parsed.as_of === "string" &&
/^\d{4}-\d{2}-\d{2}(?:T\d{2}:\d{2}(?::\d{2}(?:\.\d{1,9})?)?(?:Z|[+-]\d{2}:\d{2}))?$/u.test(
parsed.as_of,
Expand Down Expand Up @@ -964,7 +985,7 @@ function stockReplyFingerprint(reply: NvdaPersonalStockReply): string | null {
try {
const parsed = new URL(reply.source_url);
parsed.hash = "";
const value = JSON.stringify([parsed.href, reply.price, reply.as_of]);
const value = JSON.stringify([parsed.href, reply.price, reply.source_timestamp, reply.as_of]);
let hash = 2_166_136_261;
for (let index = 0; index < value.length; index += 1) {
hash ^= value.charCodeAt(index);
Expand Down
34 changes: 22 additions & 12 deletions test/e2e/live/openclaw-agent-assertion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ import {

const AGENT_TURN_TIMEOUT_MS = 3 * 60_000;
const OPENCLAW_AGENT_ATTEMPTS = 3;
const MAX_LATEST_STOCK_AGE_MS = 8 * 24 * 60 * 60_000;
const MAX_SOURCE_CLOCK_SKEW_MS = 36 * 60 * 60_000;

export interface OpenClawAgentAssertionEvidence {
reply: string;
Expand All @@ -50,7 +48,6 @@ export interface OpenClawAgentAssertionOptions {

export interface PersonalStockAssertionResult {
assessment: PersonalStockToolEvidenceArtifact;
asOfRecent: true;
quote: {
as_of: string;
price: number;
Expand Down Expand Up @@ -203,13 +200,31 @@ export async function runOpenClawAgentAssertion(
}

export const PERSONAL_STOCK_PROMPT = `Find the latest available NVIDIA (NVDA) stock price.
Choose a small, machine-readable public HTTPS source yourself and use web_fetch as the only target tool.
Choose a small, machine-readable public HTTPS source whose response contains one NVDA price and its market or update timestamp.
Use web_fetch as the only target tool.
If progressive tool disclosure is active, you may use tool_search, tool_describe, and tool_call only to discover and invoke web_fetch.
Do not invoke any other target tool. Do not use web_search, Brave Search, or Tavily Search.
Set web_fetch maxChars to no more than 8000.
Only after web_fetch returns a numeric NVDA price with its source date or timestamp, reply with one JSON object and no Markdown.
Set status to NVDA_PERSONAL_AGENT_OK, symbol to NVDA, price to a JSON number, source_url to the exact HTTPS URL passed to web_fetch, and as_of to the quote's own market or update timestamp converted to ISO 8601.
For a Unix-epoch quote field such as regularMarketTime, convert that field to ISO 8601. Never use the current clock, fetch time, or an unrelated date for as_of.`;
If a web_fetch result omits the quote timestamp, do not use its price or infer a timestamp.
Fetch a different machine-readable source instead.
Reply only after one web_fetch result contains NVDA, a numeric price, and its market or update timestamp.
Keep each web_fetch result isolated. Never combine a price from one result with a timestamp from another result.
Select exactly one complete result and ignore every unselected result when constructing the reply.
Use the price and timestamp from that result. Set source_url to the exact URL for its paired web_fetch call.
If no result contains all three values, do not return success.
Reply with one JSON object and no Markdown.
Return these fields:
- status: NVDA_PERSONAL_AGENT_OK
- symbol: NVDA
- price: the quote price as a JSON number
- source_url: the exact URL for the paired web_fetch call
- source_timestamp: the exact timestamp token copied without editing from the selected result, as a JSON string
- as_of: the ISO 8601 representation of source_timestamp
Copy an extended ISO 8601 source date or timestamp exactly. Convert a compact YYYYMMDD source date to YYYY-MM-DD.
If a source value has a calendar date but no clock time, return YYYY-MM-DD. Never add a clock time or timezone to a date-only value.
For a Unix-epoch field such as regularMarketTime, interpret the exact integer as seconds or milliseconds since 1970-01-01T00:00:00Z and convert that instant to UTC ISO 8601.
Before replying, confirm that source_timestamp appears exactly in the selected result and converts to the same instant or date as as_of.
Never use the current clock, fetch time, or an unrelated date for as_of.`;

export async function runPersonalStockAgentAssertion(
host: HostCliClient,
Expand All @@ -234,16 +249,11 @@ export async function runPersonalStockAgentAssertion(
const assessmentArtifact = projectPersonalStockToolEvidenceArtifact(stock.toolEvidence!);
const quote = parseNvdaPersonalStockReply(stock.reply);
expect(quote).not.toBeNull();
const quoteTime = Date.parse(quote!.as_of);
const now = Date.now();
expect(quoteTime).toBeGreaterThanOrEqual(now - MAX_LATEST_STOCK_AGE_MS);
expect(quoteTime).toBeLessThanOrEqual(now + MAX_SOURCE_CLOCK_SKEW_MS);
const sourceUrl = new URL(quote!.source_url);
expect(sourceUrl.protocol).toBe("https:");
await artifacts.writeJson(`actions/${args.label}-assessment.json`, assessmentArtifact);
return {
assessment: assessmentArtifact,
asOfRecent: true,
quote: {
as_of: quote!.as_of,
price: quote!.price,
Expand Down
Loading
Loading