diff --git a/CHANGELOG.md b/CHANGELOG.md index 109de00..9e8b678 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Every embedded CAPTCHA detection now includes a stable `evidenceFamily` for diagnostics and future calibration. Evidence-family attribution does not change scores, thresholds, or recommendations. +- Embedded CAPTCHA analysis now exposes an observe-only human/agent handoff + timeline when bounded action windows are supplied. Ambiguous windows stay + unknown, and the observation does not change scoring or recommendations. ## [0.36.1] - 2026-12-05 diff --git a/packages/webdecoy/src/detection/engine.test.ts b/packages/webdecoy/src/detection/engine.test.ts index ac28985..7097117 100644 --- a/packages/webdecoy/src/detection/engine.test.ts +++ b/packages/webdecoy/src/detection/engine.test.ts @@ -876,3 +876,30 @@ describe('JA4 from trusted headers', () => { expect(hasReasonIncluding(withJA4, 'JA4')).toBe(false); }); }); + +describe('human-agent handoff observation', () => { + it('reports the transition without changing score, recommendation, or detections', () => { + const baseline = score({}); + const verdict = score({ behavioral: { actionWindows: { + version: 1, + quantumMs: 250, + maxWindows: 48, + truncated: false, + windows: [ + { t: 0, kind: 'click', events: 1, + humanEvidenceFamilies: ['pointer_path'], agentEvidenceFamilies: [] }, + { t: 4, kind: 'click', events: 1, + humanEvidenceFamilies: [], agentEvidenceFamilies: ['pointer_provenance'] }, + ], + } } }); + + expect(verdict.score).toBe(baseline.score); + expect(verdict.recommendation).toBe(baseline.recommendation); + expect(verdict.detections).toEqual(baseline.detections); + expect(verdict.observations?.find(({ id }) => id === 'human-agent-handoff-v1')).toMatchObject({ + mode: 'observe', + status: 'detected', + handoffs: [{ direction: 'human_to_agent', atQ: 4 }], + }); + }); +}); diff --git a/packages/webdecoy/src/detection/engine.ts b/packages/webdecoy/src/detection/engine.ts index ec3424d..6c03489 100644 --- a/packages/webdecoy/src/detection/engine.ts +++ b/packages/webdecoy/src/detection/engine.ts @@ -18,6 +18,7 @@ import type { import { DEFAULT_WEIGHTS } from './weights'; import { observeScrollJump } from './scroll-observation'; import { annotateEvidenceFamilies } from './evidence-family'; +import { observeHumanAgentHandoff } from './handoff'; import { isDatacenterIP } from './ip'; import { InMemoryFingerprintStore, @@ -211,6 +212,11 @@ export class DetectionEngine { const currentScore = calculateFinalScore(currentCategoryScores, this.weights); const currentRecommendation = recommend(currentScore); const scrollObservation = observeScrollJump(signals.behavioral); + const handoffObservation = signals.behavioral?.actionWindows === undefined ? undefined : + observeHumanAgentHandoff(signals.behavioral.actionWindows); + const observations = [scrollObservation, handoffObservation].filter( + (observation): observation is NonNullable => observation !== undefined, + ); let candidateCategoryScores: CategoryScores | undefined; let candidateScore: number | undefined; @@ -251,7 +257,7 @@ export class DetectionEngine { recommendation, categoryScores, detections, - ...(scrollObservation ? { observations: [scrollObservation] } : {}), + ...(observations.length ? { observations } : {}), timestamp: Math.floor(Date.now() / 1000), }; } diff --git a/packages/webdecoy/src/detection/fixtures/human-agent-handoff.json b/packages/webdecoy/src/detection/fixtures/human-agent-handoff.json new file mode 100644 index 0000000..2ebc840 --- /dev/null +++ b/packages/webdecoy/src/detection/fixtures/human-agent-handoff.json @@ -0,0 +1,67 @@ +{ + "cases": [ + { + "name": "human to agent", + "windows": { "version": 1, "quantumMs": 250, "maxWindows": 48, "truncated": false, "windows": [ + { "t": 1, "kind": "click", "events": 1, "humanEvidenceFamilies": ["pointer_path"], "agentEvidenceFamilies": [] }, + { "t": 4, "kind": "key", "events": 4, "humanEvidenceFamilies": ["typing_cadence"], "agentEvidenceFamilies": [] }, + { "t": 12, "kind": "click", "events": 1, "humanEvidenceFamilies": [], "agentEvidenceFamilies": ["pointer_provenance"] }, + { "t": 15, "kind": "scroll", "events": 1, "humanEvidenceFamilies": [], "agentEvidenceFamilies": ["scroll_behavior"] } + ] }, + "status": "detected", "actors": ["human", "agent"], "directions": ["human_to_agent"], "atQ": [12] + }, + { + "name": "agent to human", + "windows": { "version": 1, "quantumMs": 250, "maxWindows": 48, "truncated": false, "windows": [ + { "t": 2, "kind": "click", "events": 1, "humanEvidenceFamilies": [], "agentEvidenceFamilies": ["pointer_provenance"] }, + { "t": 9, "kind": "key", "events": 5, "humanEvidenceFamilies": ["typing_cadence"], "agentEvidenceFamilies": [] } + ] }, + "status": "detected", "actors": ["agent", "human"], "directions": ["agent_to_human"], "atQ": [9] + }, + { + "name": "human only", + "windows": { "version": 1, "quantumMs": 250, "maxWindows": 48, "truncated": false, "windows": [ + { "t": 1, "kind": "key", "events": 6, "humanEvidenceFamilies": ["typing_cadence"], "agentEvidenceFamilies": [] }, + { "t": 6, "kind": "scroll", "events": 8, "humanEvidenceFamilies": ["scroll_behavior"], "agentEvidenceFamilies": [] } + ] }, + "status": "clear", "actors": ["human"], "directions": [], "atQ": [] + }, + { + "name": "unknown does not bridge actors", + "windows": { "version": 1, "quantumMs": 250, "maxWindows": 48, "truncated": false, "windows": [ + { "t": 1, "kind": "key", "events": 4, "humanEvidenceFamilies": ["typing_cadence"], "agentEvidenceFamilies": [] }, + { "t": 5, "kind": "touch", "events": 1, "humanEvidenceFamilies": [], "agentEvidenceFamilies": [] }, + { "t": 8, "kind": "click", "events": 1, "humanEvidenceFamilies": [], "agentEvidenceFamilies": ["pointer_provenance"] } + ] }, + "status": "clear", "actors": ["human", "unknown", "agent"], "directions": [], "atQ": [] + }, + { + "name": "conflicting support stays unknown", + "windows": { "version": 1, "quantumMs": 250, "maxWindows": 48, "truncated": false, "windows": [ + { "t": 1, "kind": "click", "events": 1, "humanEvidenceFamilies": ["pointer_path"], "agentEvidenceFamilies": ["pointer_provenance"] } + ] }, + "status": "unknown", "actors": ["unknown"], "directions": [], "atQ": [] + }, + { + "name": "truncated clear prefix is unknown", + "windows": { "version": 1, "quantumMs": 250, "maxWindows": 48, "truncated": true, "windows": [ + { "t": 1, "kind": "key", "events": 4, "humanEvidenceFamilies": ["typing_cadence"], "agentEvidenceFamilies": [] } + ] }, + "status": "unknown", "actors": ["human"], "directions": [], "atQ": [] + }, + { + "name": "malformed evidence is unknown", + "windows": { "version": 1, "quantumMs": 250, "maxWindows": 48, "truncated": false, "windows": [ + { "t": 1, "kind": "click", "events": 1, "humanEvidenceFamilies": [], "agentEvidenceFamilies": ["invented"] } + ] }, + "status": "unknown", "actors": [], "directions": [], "atQ": [] + }, + { + "name": "missing truncation marker is unknown", + "windows": { "version": 1, "quantumMs": 250, "maxWindows": 48, "windows": [ + { "t": 1, "kind": "click", "events": 1, "humanEvidenceFamilies": ["pointer_path"], "agentEvidenceFamilies": [] } + ] }, + "status": "unknown", "actors": [], "directions": [], "atQ": [] + } + ] +} diff --git a/packages/webdecoy/src/detection/handoff.test.ts b/packages/webdecoy/src/detection/handoff.test.ts new file mode 100644 index 0000000..23c939c --- /dev/null +++ b/packages/webdecoy/src/detection/handoff.test.ts @@ -0,0 +1,12 @@ +import fixture from './fixtures/human-agent-handoff.json'; +import { observeHumanAgentHandoff } from './handoff'; + +describe('human-agent handoff observation', () => { + it.each(fixture.cases)('$name', ({ windows, status, actors, directions, atQ }) => { + const result = observeHumanAgentHandoff(windows); + expect(result.status).toBe(status); + expect(result.runs.map(({ actor }) => actor)).toEqual(actors); + expect(result.handoffs.map(({ direction }) => direction)).toEqual(directions); + expect(result.handoffs.map(({ atQ: at }) => at)).toEqual(atQ); + }); +}); diff --git a/packages/webdecoy/src/detection/handoff.ts b/packages/webdecoy/src/detection/handoff.ts new file mode 100644 index 0000000..46a2493 --- /dev/null +++ b/packages/webdecoy/src/detection/handoff.ts @@ -0,0 +1,145 @@ +import type { EvidenceFamily } from './types'; + +export const HANDOFF_POLICY = 'human-agent-handoff-v1' as const; +export const ACTION_WINDOW_QUANTUM_MS = 250; +export const ACTION_WINDOW_LIMIT = 48; +export const ACTION_WINDOW_BYTE_LIMIT = 8192; + +export type ActionKind = 'click' | 'key' | 'scroll' | 'touch'; +export type ActorKind = 'human' | 'agent' | 'unknown'; +export type ActionEvidenceFamily = Extract; + +export interface ActionWindow { + t: number; + kind: ActionKind; + events: number; + humanEvidenceFamilies: ActionEvidenceFamily[]; + agentEvidenceFamilies: ActionEvidenceFamily[]; +} + +export interface ActionWindows { + version: 1; + quantumMs: 250; + maxWindows: 48; + truncated: boolean; + windows: ActionWindow[]; +} + +export interface ActorRun { + actor: ActorKind; + startQ: number; + endQ: number; + windows: number; + actions: number; + confidence: number; + evidenceFamilies: ActionEvidenceFamily[]; +} + +export interface ControlHandoff { + direction: 'human_to_agent' | 'agent_to_human'; + atQ: number; + confidence: number; + beforeEvidenceFamilies: ActionEvidenceFamily[]; + afterEvidenceFamilies: ActionEvidenceFamily[]; +} + +export interface HandoffObservation { + id: typeof HANDOFF_POLICY; + mode: 'observe'; + status: 'detected' | 'clear' | 'unknown'; + truncated: boolean; + quantumMs: 250; + runs: ActorRun[]; + handoffs: ControlHandoff[]; +} + +const ACTION_KINDS = new Set(['click', 'key', 'scroll', 'touch']); +const ACTION_EVIDENCE_FAMILIES = new Set([ + 'pointer_path', 'pointer_provenance', 'typing_cadence', + 'scroll_behavior', 'touch_provenance', 'field_injection', +]); + +function validFamilyList(value: unknown): value is ActionEvidenceFamily[] { + return Array.isArray(value) && value.length <= 4 && + value.every((family) => typeof family === 'string' && ACTION_EVIDENCE_FAMILIES.has(family as ActionEvidenceFamily)) && + new Set(value).size === value.length; +} + +function parseActionWindows(value: unknown): ActionWindows | null { + if (!value || typeof value !== 'object') return null; + const candidate = value as Partial; + if (candidate.version !== 1 || candidate.quantumMs !== ACTION_WINDOW_QUANTUM_MS || + candidate.maxWindows !== ACTION_WINDOW_LIMIT || typeof candidate.truncated !== 'boolean' || + !Array.isArray(candidate.windows) || candidate.windows.length > ACTION_WINDOW_LIMIT) return null; + try { + if (JSON.stringify(value).length > ACTION_WINDOW_BYTE_LIMIT) return null; + } catch { return null; } + let previousT = -1; + for (const item of candidate.windows) { + const window = item as Partial; + if (!window || !Number.isInteger(window.t) || window.t! < previousT || window.t! < 0 || window.t! > 65535 || + !ACTION_KINDS.has(window.kind as ActionKind) || !Number.isInteger(window.events) || window.events! < 1 || window.events! > 255 || + !validFamilyList(window.humanEvidenceFamilies) || !validFamilyList(window.agentEvidenceFamilies)) return null; + previousT = window.t!; + } + return candidate as ActionWindows; +} + +function actorForWindow(window: ActionWindow): ActorKind { + const human = window.humanEvidenceFamilies.length > 0; + const agent = window.agentEvidenceFamilies.length > 0; + if (human === agent) return 'unknown'; + return human ? 'human' : 'agent'; +} + +function confidence(families: ActionEvidenceFamily[]): number { + return families.length > 1 ? 0.9 : 0.75; +} + +function buildRuns(windows: ActionWindow[]): ActorRun[] { + const runs: ActorRun[] = []; + for (const window of windows) { + const actor = actorForWindow(window); + const evidence = actor === 'human' ? window.humanEvidenceFamilies : actor === 'agent' ? window.agentEvidenceFamilies : []; + const last = runs.at(-1); + if (last?.actor === actor) { + last.endQ = window.t; + last.windows++; + last.actions += window.events; + last.evidenceFamilies = [...new Set([...last.evidenceFamilies, ...evidence])].sort() as ActionEvidenceFamily[]; + last.confidence = actor === 'unknown' ? 0 : confidence(last.evidenceFamilies); + } else { + runs.push({ actor, startQ: window.t, endQ: window.t, windows: 1, actions: window.events, + confidence: actor === 'unknown' ? 0 : confidence(evidence), evidenceFamilies: [...evidence].sort() }); + } + } + return runs; +} + +export function observeHumanAgentHandoff(actionWindowsValue: unknown): HandoffObservation { + const actionWindows = parseActionWindows(actionWindowsValue); + if (!actionWindows) { + return { id: HANDOFF_POLICY, mode: 'observe', status: 'unknown', truncated: false, + quantumMs: 250, runs: [], handoffs: [] }; + } + const runs = buildRuns(actionWindows.windows); + const handoffs: ControlHandoff[] = []; + for (let index = 1; index < runs.length; index++) { + const before = runs[index - 1]; + const after = runs[index]; + if (before.actor === 'unknown' || after.actor === 'unknown' || before.actor === after.actor) continue; + handoffs.push({ + direction: `${before.actor}_to_${after.actor}` as ControlHandoff['direction'], + atQ: after.startQ, + confidence: Math.min(before.confidence, after.confidence), + beforeEvidenceFamilies: before.evidenceFamilies, + afterEvidenceFamilies: after.evidenceFamilies, + }); + } + const status = handoffs.length ? 'detected' : actionWindows.truncated ? 'unknown' : + runs.some((run) => run.actor !== 'unknown') ? 'clear' : 'unknown'; + return { id: HANDOFF_POLICY, mode: 'observe', status, truncated: actionWindows.truncated, + quantumMs: 250, runs, handoffs }; +} diff --git a/packages/webdecoy/src/detection/index.ts b/packages/webdecoy/src/detection/index.ts index 8899fc3..f349063 100644 --- a/packages/webdecoy/src/detection/index.ts +++ b/packages/webdecoy/src/detection/index.ts @@ -16,6 +16,16 @@ export { recommend, } from './scoring'; export { annotateEvidenceFamilies, evidenceFamilyForDetection } from './evidence-family'; +export { observeHumanAgentHandoff, HANDOFF_POLICY } from './handoff'; +export type { + ActionKind, + ActionWindow, + ActionWindows, + ActorKind, + ActorRun, + ControlHandoff, + HandoffObservation, +} from './handoff'; export { DEFAULT_WEIGHTS, AUTOMATION_UA_PATTERNS } from './weights'; export { isDatacenterIP, DATACENTER_CIDRS } from './ip'; diff --git a/packages/webdecoy/src/detection/types.ts b/packages/webdecoy/src/detection/types.ts index 53d4f5e..72737c7 100644 --- a/packages/webdecoy/src/detection/types.ts +++ b/packages/webdecoy/src/detection/types.ts @@ -79,6 +79,8 @@ export interface BehavioralSignals { keyHoldAvg?: number; /** Count of recent scroll events and largest CSS pixel step between them. */ scrollMorphology?: { samples?: number; maxStep?: number }; + /** Bounded, content-free action windows used only for observe-only actor timelines. */ + actionWindows?: unknown; microTremorScore?: number; approachDirectness?: number; clickPrecision?: number; @@ -358,7 +360,7 @@ export interface Verdict { /** Every triggered detection. */ detections: Detection[]; /** Scroll diagnostics returned when the browser supplied enough usable evidence. */ - observations?: ScrollJumpObservation[]; + observations?: Array; /** Unix seconds at evaluation time. */ timestamp: number; }