From 94939552130f9f93995a36fed03cdac0c615086a Mon Sep 17 00:00:00 2001 From: sykosyber Date: Sat, 5 Sep 2026 17:21:11 -0700 Subject: [PATCH 01/12] Gate agent drafts on verified facts and batch review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit An autonomous writer needs somewhere to get its facts and somewhere its output can be checked without a human reading every letter. This adds both, and keeps them apart: the fact ledger is objective and gated on human verification, the style card is subjective and learned from corrections. A style mistake is recoverable; an unsupported claim sent under the applicant's name is not. Facts are proposed by local resume extraction and are unusable until verified, with an optional expiry for anything that goes stale. Drafts are logged through a new WebMCP tool at any volume, but every sentence asserting something checkable must cite a verified fact, and every number must appear in a cited fact. A draft failing either rule is refused at the API and never written, so an invented achievement cannot reach a review queue at all. Review is batched and ordered by risk rather than convenience: an unseen role cluster reviews at the first draft, then style drift, then low agent confidence, then a full batch. Sessions group repeated openings so one decision covers several drafts and turn corrections into style rules, since a session that changes no rule leaves the next batch identical. Autonomy is then earned per cluster. Enough near-unchanged reviews graduate a cluster into staging its own drafts; a correction or expired fact demotes it. Staging is not acceptance — status and accepted_draft are untouched, so accepting an exact draft stays a human action, and agents still cannot submit anything. Every draft records the profile version that produced it, without which a bad draft cannot be attributed to a bad agent rather than a stale profile. Co-Authored-By: Claude Opus 5 --- app/agent-tools.ts | 218 ++++++++------ app/api/drafts/route.ts | 245 ++++++++++++++++ app/api/profile/route.ts | 143 ++++++++++ db/schema.ts | 55 ++++ drizzle/0003_profile_calibration.sql | 54 ++++ drizzle/meta/_journal.json | 7 + lib/profile.ts | 405 +++++++++++++++++++++++++++ lib/resume.ts | 69 +++++ lib/store.ts | 40 +++ package.json | 2 +- tests/calibration.test.mjs | 188 +++++++++++++ tests/profile.test.mjs | 352 +++++++++++++++++++++++ 12 files changed, 1695 insertions(+), 83 deletions(-) create mode 100644 app/api/drafts/route.ts create mode 100644 app/api/profile/route.ts create mode 100644 drizzle/0003_profile_calibration.sql create mode 100644 lib/profile.ts create mode 100644 lib/resume.ts create mode 100644 lib/store.ts create mode 100644 tests/calibration.test.mjs create mode 100644 tests/profile.test.mjs diff --git a/app/agent-tools.ts b/app/agent-tools.ts index 3be3801..18ddec1 100644 --- a/app/agent-tools.ts +++ b/app/agent-tools.ts @@ -1,5 +1,34 @@ 'use client'; import { useEffect } from 'react'; +type Json = Record; +type Tool = { + name: string; + description: string; + readOnly: boolean; + schema: Json; + run: (input: Json) => Promise; +}; +const object = (properties: Json, required: string[] = []) => ({ + type: 'object', + properties, + required, + additionalProperties: false, +}); +async function call(url: string, body?: Json) { + const r = await fetch( + url, + body + ? { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify(body), + } + : {}, + ); + const result = (await r.json()) as { error?: string }; + if (!r.ok) throw Error(result.error); + return result; +} export function useRelayTools(refresh: () => Promise) { useEffect(() => { const context = ( @@ -14,96 +43,121 @@ export function useRelayTools(refresh: () => Promise) { ).modelContext; if (!context?.registerTool) return; const lifecycle = new AbortController(); - for (const [name, description, action, readOnly] of [ - [ - 'relay_read_workspace', - 'Read opportunities, blockers and exact saved drafts. Does not modify records.', - null, - true, - ], - [ - 'relay_preview_import', - 'Check an array of research rows for duplicates without importing or changing records.', - 'preview', - true, - ], - [ - 'relay_stage_draft', - 'Save a draft for human review. Never accepts a draft or submits an application. Requires id, current version, draft and blocker.', - 'save', - false, - ], - ] as const) { - const inputSchema = - action === 'save' - ? { - type: 'object', - properties: { - id: { type: 'string' }, - version: { type: 'integer' }, - draft: { type: 'string' }, - blocker: { type: 'string' }, - }, - required: ['id', 'version', 'draft', 'blocker'], - additionalProperties: false, - } - : action === 'preview' - ? { - type: 'object', - properties: { - rows: { type: 'array', items: { type: 'object' } }, - }, - required: ['rows'], - additionalProperties: false, - } - : { type: 'object', properties: {}, additionalProperties: false }; + const tools: Tool[] = [ + { + name: 'relay_read_workspace', + description: + 'Read opportunities, blockers and exact saved drafts. Does not modify records.', + readOnly: true, + schema: object({}), + run: async () => ({ + jobs: ((await call('/api/workspace')) as { jobs: unknown }).jobs, + }), + }, + // The brief a writing agent works from: verified facts and the style + // rules learned so far. Read-only by design — an agent that can edit its + // own instructions drifts toward whatever is cheapest to generate. + { + name: 'relay_read_profile', + description: + 'Read the verified fact ledger and learned style rules that drafts must follow. Cite these fact ids when writing. Does not modify the profile.', + readOnly: true, + schema: object({}), + run: () => call('/api/profile'), + }, + { + name: 'relay_review_status', + description: + 'Read per-cluster trust, pending draft counts and whether a human review is due. Does not modify records.', + readOnly: true, + schema: object({}), + run: async () => { + const d = (await call('/api/drafts')) as Json; + return { trust: d.trust, trigger: d.trigger, batches: d.batches }; + }, + }, + { + name: 'relay_preview_import', + description: + 'Check an array of research rows for duplicates without importing or changing records.', + readOnly: true, + schema: object({ rows: { type: 'array', items: { type: 'object' } } }, [ + 'rows', + ]), + run: (input) => call('/api/workspace', { ...input, action: 'preview' }), + }, + // Logging is the autonomous path: unlimited volume, no human gate, but + // every factual claim must trace to a verified fact or the write is + // refused outright. + { + name: 'relay_log_draft', + description: + 'Log a written draft for later batch review. Every sentence making a factual claim must be supported by a cited verified fact id from relay_read_profile; unsupported claims are rejected. Set confidence "low" when a needed fact is missing instead of guessing. Never accepts or submits.', + readOnly: false, + schema: object( + { + job_id: { type: 'string' }, + body: { type: 'string' }, + cited: { type: 'array', items: { type: 'string' } }, + confidence: { type: 'string', enum: ['high', 'low'] }, + }, + ['job_id', 'body', 'cited'], + ), + run: async (input) => { + const result = await call('/api/drafts', { ...input, action: 'log' }); + await refresh(); + return result; + }, + }, + { + name: 'relay_stage_draft', + description: + 'Save a draft into a job record for human review. Never accepts a draft or submits an application. Requires id, current version, draft and blocker.', + readOnly: false, + schema: object( + { + id: { type: 'string' }, + version: { type: 'integer' }, + draft: { type: 'string' }, + blocker: { type: 'string' }, + }, + ['id', 'version', 'draft', 'blocker'], + ), + run: async (input) => { + const snapshot = (await call('/api/workspace')) as { + jobs: { id: string; status: string }[]; + }; + const job = snapshot.jobs.find((j) => j.id === input.id); + if (!job) throw Error('Record not found.'); + const status = ['Submitted', 'Live loop'].includes(job.status) + ? job.status + : 'Held'; + const result = await call('/api/workspace', { + ...input, + action: 'save', + status, + }); + await refresh(); + return result; + }, + }, + ]; + for (const tool of tools) Promise.resolve( context.registerTool( { - name, - description, - inputSchema, - annotations: { readOnlyHint: readOnly, untrustedContentHint: true }, - async execute(input: Record) { - let status = 'Held'; - if (action === 'save') { - const snapshot = await fetch('/api/workspace'); - if (!snapshot.ok) throw Error('Sign in to stage a draft.'); - const data = (await snapshot.json()) as { - jobs: { id: string; status: string }[]; - }; - const job = data.jobs.find((j) => j.id === input.id); - if (!job) throw Error('Record not found.'); - if (['Submitted', 'Live loop'].includes(job.status)) - status = job.status; - } - const r = await fetch( - '/api/workspace', - action - ? { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ - ...input, - action, - ...(action === 'save' ? { status } : {}), - }), - } - : {}, - ); - const result = (await r.json()) as { - error?: string; - jobs?: unknown; - }; - if (!r.ok) throw Error(result.error); - if (action === 'save') await refresh(); - return action ? result : { jobs: result.jobs }; + name: tool.name, + description: tool.description, + inputSchema: tool.schema, + annotations: { + readOnlyHint: tool.readOnly, + untrustedContentHint: true, }, + execute: tool.run, }, { signal: lifecycle.signal }, ), ).catch(() => console.warn('Relay agent tools could not register.')); - } return () => lifecycle.abort(); }, [refresh]); } diff --git a/app/api/drafts/route.ts b/app/api/drafts/route.ts new file mode 100644 index 0000000..582a7c8 --- /dev/null +++ b/app/api/drafts/route.ts @@ -0,0 +1,245 @@ +import { getChatGPTUser } from '../../chatgpt-auth'; +import { database } from '../../../lib/database'; +import { + clusterOf, + clusterTrust, + proposeRules, + reviewTrigger, + trustMap, + validateDraftLog, + validateRule, +} from '../../../lib/profile'; +import { + bumpProfile, + loadDrafts, + loadFacts, + profileVersion, +} from '../../../lib/store'; +export const dynamic = 'force-dynamic'; +const reply = (data: unknown, status = 200) => + Response.json(data, { status, headers: { 'Cache-Control': 'no-store' } }); +const stale = ( + facts: { status: string; expires: string | null }[], + now: string, +) => + facts.some((f) => f.status === 'Verified' && !!f.expires && f.expires <= now); +export async function GET() { + const user = (await getChatGPTUser())?.userId; + if (!user) return reply({ error: 'Sign in to open your drafts.' }, 401); + const db = database(), + now = new Date().toISOString(); + const [drafts, facts] = await Promise.all([ + loadDrafts(db, user), + loadFacts(db, user), + ]); + const batches = await db + .prepare( + 'SELECT * FROM review_batches WHERE owner=? ORDER BY opened DESC LIMIT 50', + ) + .bind(user) + .all(); + return reply({ + drafts, + batches: batches.results, + trust: trustMap(drafts, stale(facts, now)), + trigger: reviewTrigger(drafts), + }); +} +export async function POST(request: Request) { + const user = (await getChatGPTUser())?.userId; + if (!user) return reply({ error: 'Sign in first.' }, 401); + const origin = request.headers.get('origin'); + if (origin && origin !== new URL(request.url).origin) + return reply({ error: 'Invalid request origin.' }, 403); + try { + if (Number(request.headers.get('content-length') || 0) > 2000000) + return reply({ error: 'Request too large.' }, 413); + const raw = await request.text(); + if (raw.length > 2000000) + return reply({ error: 'Request too large.' }, 413); + const b = JSON.parse(raw), + db = database(), + now = new Date().toISOString(); + // The agent-facing write. A draft that cites an unverified fact, or makes a + // claim no cited fact supports, is refused here rather than queued for a + // reviewer to catch. + if (b.action === 'log') { + const job = await db + .prepare( + 'SELECT id,name,status,version,accepted_draft FROM jobs WHERE id=? AND owner=?', + ) + .bind(b.job_id, user) + .first<{ + id: string; + name: string; + status: string; + version: number; + accepted_draft: string | null; + }>(); + if (!job) return reply({ error: 'Record not found.' }, 404); + const facts = await loadFacts(db, user); + const entry = validateDraftLog(b, facts, now); + const cluster = clusterOf(job.name), + version = await profileVersion(db, user), + existing = await loadDrafts(db, user); + const trust = clusterTrust(cluster, existing, stale(facts, now)); + const statements = [ + db + .prepare( + 'INSERT INTO drafts (id,owner,job_id,cluster,body,profile_version,cited,confidence,verdict,created) VALUES (?,?,?,?,?,?,?,?,?,?)', + ) + .bind( + crypto.randomUUID(), + user, + job.id, + cluster, + entry.body, + version, + entry.cited.join(','), + entry.confidence, + 'Logged', + now, + ), + ]; + // A graduated cluster earns the right to place its draft in the + // workspace unattended. It still only stages: status is untouched and + // acceptance stays a human action on an exact version. + const staged = + trust.state === 'Graduated' && + entry.confidence === 'high' && + job.status === 'Held' && + !job.accepted_draft; + if (staged) { + statements.push( + db + .prepare( + "UPDATE jobs SET draft=?,version=version+1,updated=? WHERE id=? AND owner=? AND version=? AND status='Held'", + ) + .bind(entry.body, now, job.id, user, job.version), + db + .prepare( + 'INSERT INTO events (id,owner,job_id,kind,detail,created) VALUES (?,?,?,?,?,?)', + ) + .bind( + crypto.randomUUID(), + user, + job.id, + 'Draft staged by agent', + JSON.stringify({ cluster, profile_version: version }), + now, + ), + ); + } + await db.batch(statements); + return reply({ + ok: true, + cluster, + staged, + trust: trust.state, + review: reviewTrigger([ + ...existing, + { + id: 'pending', + job_id: job.id, + cluster, + body: entry.body, + corrected: '', + cited: entry.cited.join(','), + confidence: entry.confidence, + verdict: 'Logged', + profile_version: version, + }, + ]), + }); + } + if (b.action === 'open-batch') { + const drafts = await loadDrafts(db, user); + const trigger = reviewTrigger(drafts); + if (!trigger) + return reply({ error: 'No drafts are waiting for review.' }, 400); + const id = crypto.randomUUID(); + await db.batch([ + db + .prepare( + 'INSERT INTO review_batches (id,owner,reason,opened,size) VALUES (?,?,?,?,?)', + ) + .bind(id, user, trigger.reason, now, trigger.ids.length), + db + .prepare( + "UPDATE drafts SET batch=? WHERE owner=? AND verdict='Logged' AND batch IS NULL", + ) + .bind(id, user), + ]); + return reply({ + ok: true, + id, + reason: trigger.reason, + size: trigger.ids.length, + }); + } + // A correction is a style signal first and a text fix second, so the + // generalisations offered back are computed from the edit itself. + if (b.action === 'correct') { + if (typeof b.corrected !== 'string' || b.corrected.length > 20000) + throw Error('Provide corrected text under 20000 characters.'); + const draft = await db + .prepare('SELECT * FROM drafts WHERE id=? AND owner=?') + .bind(b.id, user) + .first<{ body: string }>(); + if (!draft) return reply({ error: 'Draft not found.' }, 404); + await db + .prepare( + "UPDATE drafts SET corrected=?, verdict='Corrected' WHERE id=? AND owner=?", + ) + .bind(b.corrected, b.id, user) + .run(); + return reply({ + ok: true, + proposals: proposeRules(draft.body, b.corrected), + }); + } + if (b.action === 'accept') { + const result = await db + .prepare("UPDATE drafts SET verdict='Reviewed' WHERE id=? AND owner=?") + .bind(b.id, user) + .run(); + if (!result.meta.changes) + return reply({ error: 'Draft not found.' }, 404); + return reply({ ok: true }); + } + // Closing a batch is the point of the whole loop: it is where corrections + // become rules the next drafts are written under. + if (b.action === 'close-batch') { + const rules = Array.isArray(b.rules) ? b.rules.map(validateRule) : []; + if (rules.length > 20) throw Error('Add at most 20 rules in one review.'); + const statements = rules.map((r: { rule: string; scope: string }) => + db + .prepare( + 'INSERT INTO style_rules (id,owner,rule,scope,origin,created) VALUES (?,?,?,?,?,?) ON CONFLICT(owner,scope,rule) DO NOTHING', + ) + .bind(crypto.randomUUID(), user, r.rule, r.scope, String(b.id), now), + ); + statements.push( + db + .prepare( + "UPDATE drafts SET verdict='Reviewed' WHERE owner=? AND batch=? AND verdict='Logged'", + ) + .bind(user, b.id), + db + .prepare( + 'UPDATE review_batches SET closed=?, rules_added=? WHERE id=? AND owner=?', + ) + .bind(now, rules.length, b.id, user), + ); + if (rules.length) statements.push(bumpProfile(db, user, now)); + await db.batch(statements); + return reply({ ok: true, rules_added: rules.length }); + } + return reply({ error: 'Unknown action.' }, 400); + } catch (e) { + return reply( + { error: e instanceof Error ? e.message : 'Unable to complete request.' }, + 400, + ); + } +} diff --git a/app/api/profile/route.ts b/app/api/profile/route.ts new file mode 100644 index 0000000..5f49fd8 --- /dev/null +++ b/app/api/profile/route.ts @@ -0,0 +1,143 @@ +import { getChatGPTUser } from '../../chatgpt-auth'; +import { database } from '../../../lib/database'; +import { validateFact, validateRule, usableFact } from '../../../lib/profile'; +import { parseResume } from '../../../lib/resume'; +import { + bumpProfile, + loadFacts, + loadRules, + profileVersion, +} from '../../../lib/store'; +export const dynamic = 'force-dynamic'; +const reply = (data: unknown, status = 200) => + Response.json(data, { status, headers: { 'Cache-Control': 'no-store' } }); +export async function GET() { + const user = (await getChatGPTUser())?.userId; + if (!user) return reply({ error: 'Sign in to open your profile.' }, 401); + const db = database(), + now = new Date().toISOString(); + const [facts, rules, version] = await Promise.all([ + loadFacts(db, user), + loadRules(db, user), + profileVersion(db, user), + ]); + return reply({ + facts, + rules, + version, + usable: facts.filter((f) => usableFact(f, now)).length, + }); +} +export async function POST(request: Request) { + const user = (await getChatGPTUser())?.userId; + if (!user) return reply({ error: 'Sign in first.' }, 401); + const origin = request.headers.get('origin'); + if (origin && origin !== new URL(request.url).origin) + return reply({ error: 'Invalid request origin.' }, 403); + try { + if (Number(request.headers.get('content-length') || 0) > 2000000) + return reply({ error: 'Request too large.' }, 413); + const raw = await request.text(); + if (raw.length > 2000000) + return reply({ error: 'Request too large.' }, 413); + const b = JSON.parse(raw), + db = database(), + now = new Date().toISOString(); + // Extraction only proposes. It writes nothing, so a noisy resume costs a + // review pass rather than a false fact in the ledger. + if (b.action === 'extract') + return reply({ candidates: parseResume(b.text) }); + if (b.action === 'propose') { + if (!Array.isArray(b.facts) || !b.facts.length || b.facts.length > 100) + throw Error('Propose between 1 and 100 facts.'); + const rows = b.facts.map(validateFact); + await db.batch( + rows.map((f: { claim: string; evidence: string; tag: string }) => + db + .prepare( + 'INSERT INTO profile_facts (id,owner,claim,evidence,tag,status,created) VALUES (?,?,?,?,?,?,?) ON CONFLICT(owner,claim) DO UPDATE SET evidence=excluded.evidence, tag=excluded.tag', + ) + .bind( + crypto.randomUUID(), + user, + f.claim, + f.evidence, + f.tag, + 'Proposed', + now, + ), + ), + ); + return reply({ ok: true, proposed: rows.length }); + } + // Verification is the only path to a usable fact, and it is always a + // human action on one specific claim. + if (b.action === 'verify') { + if (b.expires != null && Number.isNaN(Date.parse(String(b.expires)))) + throw Error('Use an ISO date for the expiry, or leave it empty.'); + const result = await db.batch([ + db + .prepare( + "UPDATE profile_facts SET status='Verified', verified=?, expires=? WHERE id=? AND owner=?", + ) + .bind( + now, + b.expires ? new Date(String(b.expires)).toISOString() : null, + b.id, + user, + ), + bumpProfile(db, user, now), + ]); + if (!result[0].meta.changes) + return reply({ error: 'Fact not found.' }, 404); + return reply({ ok: true }); + } + if (b.action === 'retire') { + const result = await db.batch([ + db + .prepare( + "UPDATE profile_facts SET status='Retired' WHERE id=? AND owner=?", + ) + .bind(b.id, user), + bumpProfile(db, user, now), + ]); + if (!result[0].meta.changes) + return reply({ error: 'Fact not found.' }, 404); + return reply({ ok: true }); + } + if (b.action === 'rule-add') { + const rule = validateRule(b); + await db.batch([ + db + .prepare( + 'INSERT INTO style_rules (id,owner,rule,scope,origin,created) VALUES (?,?,?,?,?,?) ON CONFLICT(owner,scope,rule) DO NOTHING', + ) + .bind( + crypto.randomUUID(), + user, + rule.rule, + rule.scope, + typeof b.origin === 'string' ? b.origin.slice(0, 80) : '', + now, + ), + bumpProfile(db, user, now), + ]); + return reply({ ok: true }); + } + if (b.action === 'rule-remove') { + await db.batch([ + db + .prepare('DELETE FROM style_rules WHERE id=? AND owner=?') + .bind(b.id, user), + bumpProfile(db, user, now), + ]); + return reply({ ok: true }); + } + return reply({ error: 'Unknown action.' }, 400); + } catch (e) { + return reply( + { error: e instanceof Error ? e.message : 'Unable to complete request.' }, + 400, + ); + } +} diff --git a/db/schema.ts b/db/schema.ts index 0afb4fb..c2e93fb 100644 --- a/db/schema.ts +++ b/db/schema.ts @@ -52,3 +52,58 @@ export const events = sqliteTable('events', { detail: text('detail').notNull(), created: text('created').notNull(), }); +export const profileFacts = sqliteTable( + 'profile_facts', + { + id: text('id').primaryKey(), + owner: text('owner').notNull(), + claim: text('claim').notNull(), + evidence: text('evidence').notNull().default(''), + tag: text('tag').notNull().default('detail'), + status: text('status').notNull().default('Proposed'), + verified: text('verified'), + expires: text('expires'), + created: text('created').notNull(), + }, + (t) => [uniqueIndex('facts_owner_claim').on(t.owner, t.claim)], +); +export const styleRules = sqliteTable( + 'style_rules', + { + id: text('id').primaryKey(), + owner: text('owner').notNull(), + rule: text('rule').notNull(), + scope: text('scope').notNull().default('global'), + origin: text('origin').notNull().default(''), + created: text('created').notNull(), + }, + (t) => [uniqueIndex('rules_owner_scope_rule').on(t.owner, t.scope, t.rule)], +); +export const profileState = sqliteTable('profile_state', { + owner: text('owner').primaryKey(), + profile_version: integer('profile_version').notNull().default(1), + updated: text('updated').notNull(), +}); +export const drafts = sqliteTable('drafts', { + id: text('id').primaryKey(), + owner: text('owner').notNull(), + job_id: text('job_id').notNull(), + cluster: text('cluster').notNull(), + body: text('body').notNull(), + corrected: text('corrected').notNull().default(''), + profile_version: integer('profile_version').notNull(), + cited: text('cited').notNull().default(''), + confidence: text('confidence').notNull().default('high'), + batch: text('batch'), + verdict: text('verdict').notNull().default('Logged'), + created: text('created').notNull(), +}); +export const reviewBatches = sqliteTable('review_batches', { + id: text('id').primaryKey(), + owner: text('owner').notNull(), + reason: text('reason').notNull(), + opened: text('opened').notNull(), + closed: text('closed'), + size: integer('size').notNull().default(0), + rules_added: integer('rules_added').notNull().default(0), +}); diff --git a/drizzle/0003_profile_calibration.sql b/drizzle/0003_profile_calibration.sql new file mode 100644 index 0000000..874258a --- /dev/null +++ b/drizzle/0003_profile_calibration.sql @@ -0,0 +1,54 @@ +CREATE TABLE `profile_facts` ( + `id` text PRIMARY KEY NOT NULL, + `owner` text NOT NULL, + `claim` text NOT NULL, + `evidence` text DEFAULT '' NOT NULL, + `tag` text DEFAULT 'detail' NOT NULL, + `status` text DEFAULT 'Proposed' NOT NULL, + `verified` text, + `expires` text, + `created` text NOT NULL +); +--> statement-breakpoint +CREATE UNIQUE INDEX `facts_owner_claim` ON `profile_facts` (`owner`,`claim`);--> statement-breakpoint +CREATE TABLE `style_rules` ( + `id` text PRIMARY KEY NOT NULL, + `owner` text NOT NULL, + `rule` text NOT NULL, + `scope` text DEFAULT 'global' NOT NULL, + `origin` text DEFAULT '' NOT NULL, + `created` text NOT NULL +); +--> statement-breakpoint +CREATE UNIQUE INDEX `rules_owner_scope_rule` ON `style_rules` (`owner`,`scope`,`rule`);--> statement-breakpoint +CREATE TABLE `profile_state` ( + `owner` text PRIMARY KEY NOT NULL, + `profile_version` integer DEFAULT 1 NOT NULL, + `updated` text NOT NULL +); +--> statement-breakpoint +CREATE TABLE `drafts` ( + `id` text PRIMARY KEY NOT NULL, + `owner` text NOT NULL, + `job_id` text NOT NULL, + `cluster` text NOT NULL, + `body` text NOT NULL, + `corrected` text DEFAULT '' NOT NULL, + `profile_version` integer NOT NULL, + `cited` text DEFAULT '' NOT NULL, + `confidence` text DEFAULT 'high' NOT NULL, + `batch` text, + `verdict` text DEFAULT 'Logged' NOT NULL, + `created` text NOT NULL +); +--> statement-breakpoint +CREATE INDEX `drafts_owner_verdict` ON `drafts` (`owner`,`verdict`);--> statement-breakpoint +CREATE TABLE `review_batches` ( + `id` text PRIMARY KEY NOT NULL, + `owner` text NOT NULL, + `reason` text NOT NULL, + `opened` text NOT NULL, + `closed` text, + `size` integer DEFAULT 0 NOT NULL, + `rules_added` integer DEFAULT 0 NOT NULL +); diff --git a/drizzle/meta/_journal.json b/drizzle/meta/_journal.json index b2ce44d..9752c78 100644 --- a/drizzle/meta/_journal.json +++ b/drizzle/meta/_journal.json @@ -22,6 +22,13 @@ "when": 1788642000000, "tag": "0002_job_key_observations", "breakpoints": true + }, + { + "idx": 3, + "version": "6", + "when": 1788643000000, + "tag": "0003_profile_calibration", + "breakpoints": true } ] } \ No newline at end of file diff --git a/lib/profile.ts b/lib/profile.ts new file mode 100644 index 0000000..16f5d0c --- /dev/null +++ b/lib/profile.ts @@ -0,0 +1,405 @@ +// The profile is what directs an autonomous writing agent, and the calibration +// loop is what keeps it honest. Two halves with deliberately different rules: +// the fact ledger is objective and gated on human verification, the style card +// is subjective and learned from corrections. Style mistakes are recoverable; +// an unsupported claim sent under the applicant's name is not. +export type Fact = { + id: string; + claim: string; + evidence: string; + tag: string; + status: string; + verified: string | null; + expires: string | null; +}; +export type Rule = { id: string; rule: string; scope: string }; +export type DraftRow = { + id: string; + job_id: string; + cluster: string; + body: string; + corrected: string; + cited: string; + confidence: string; + verdict: string; + profile_version: number; + batch?: string | null; +}; +export const factStates = ['Proposed', 'Verified', 'Retired'] as const; +export const factTags = ['metric', 'role', 'credential', 'detail'] as const; +export const confidences = ['high', 'low'] as const; +export const verdicts = ['Logged', 'Reviewed', 'Corrected'] as const; +export const limits = { + batchSize: 12, + lowConfidence: 3, + graduationRuns: 5, + graduationEdit: 0.15, + driftEdit: 0.45, + samenessEdit: 0.2, +}; +const stop = new Set( + `a an the and or but if then than that this these those of in on at to for with from by as is are was were be been being am i my me we our us you your it its their there here have has had do does did not no so such very much many more most own same too also just only about into over under after before between during while which who whom what when where how why can could should would will shall may might must`.split( + /\s+/, + ), +); +// Unambiguous achievement verbs. Any of these alongside a first-person +// subject marks a checkable claim. +const strongVerbs = + /\b(led|built|shipped|increased|reduced|managed|founded|scaled|launched|published|won|earned|grew|saved|delivered|architected|created|improved|drove|hired|mentored|taught|wrote|released|migrated|authored|generated|developed|implemented|automated|graduated|spent)\b/i; +// These double as common nouns ("your storage work", "the design", "a lead"), +// so they only count as claims when a first-person subject governs them. A +// missed claim is the dangerous direction, so they are guarded rather than +// dropped. +const ambiguousVerbs = + /(?<=\b(?:i|we|my|our)\s(?:\w+\s){0,1})\b(lead|leads|build|builds|ship|ships|increase|reduce|manage|found|scale|own|owns|owned|launch|publish|save|deliver|design|designed|create|improve|cut|hold|holds|write|release|migrate|author|generate|raise|raised|close|closed|work|works|worked)\b/i; +const firstPerson = /\b(i|my|mine|we|our)\b/i; +const roleFamilies: [RegExp, string][] = [ + [/\b(research|scientist|scientific)\b/i, 'research'], + [/\b(machine learning|ml|ai|deep learning)\b/i, 'ml'], + [/\b(data|analytics|analyst)\b/i, 'data'], + [/\b(infra|infrastructure|sre|reliability|devops)\b/i, 'infrastructure'], + [/\b(security|appsec|cryptograph)\b/i, 'security'], + [/\b(platform)\b/i, 'platform'], + [/\b(backend|back-end|server)\b/i, 'backend'], + [/\b(frontend|front-end|ui engineer)\b/i, 'frontend'], + [/\b(full[ -]?stack)\b/i, 'fullstack'], + [/\b(mobile|ios|android)\b/i, 'mobile'], + [/\b(product manager|product owner|pm)\b/i, 'product'], + [/\b(design|designer|ux)\b/i, 'design'], +]; +export function contentWords(text: string): Set { + return new Set( + text + .toLowerCase() + .replace(/[^a-z0-9\s-]/g, ' ') + .split(/\s+/) + .filter((w) => w.length > 2 && !stop.has(w)), + ); +} +export function numbersIn(text: string): Set { + return new Set( + (text.replace(/,(?=\d{3}\b)/g, '').match(/\d+(?:\.\d+)?/g) || []).map((n) => + String(Number(n)), + ), + ); +} +export function sentences(text: string): string[] { + return text + .split(/(?<=[.!?])\s+|\n+/) + .map((s) => s.trim()) + .filter(Boolean); +} +// A sentence asserts something checkable when it carries a number, or pairs a +// first-person subject with an achievement verb. Interest, questions and +// remarks about the employer are not claims and never need a citation. +export function isClaim(sentence: string): boolean { + if (sentence.endsWith('?')) return false; + if (/\d/.test(sentence)) return true; + if (!firstPerson.test(sentence)) return false; + if (strongVerbs.test(sentence)) return true; + // An ambiguous verb used intransitively is discourse, not assertion: + // "why I write" frames the letter, while "I write release tooling" claims + // something. Requiring a following object keeps the common cover-letter + // framing out of the citation gate without weakening it for real claims. + const match = ambiguousVerbs.exec(sentence); + return ( + !!match && + contentWords(sentence.slice(match.index + match[0].length)).size > 0 + ); +} +export function usableFact(fact: Fact, now: string): boolean { + return fact.status === 'Verified' && (!fact.expires || fact.expires > now); +} +function supports(sentence: string, fact: Fact, pool: Set): boolean { + const sentenceNumbers = numbersIn(sentence); + for (const n of sentenceNumbers) if (!pool.has(n)) return false; + const words = contentWords(sentence), + factWords = contentWords(`${fact.claim} ${fact.evidence}`); + let overlap = 0; + for (const w of words) if (factWords.has(w)) overlap++; + return overlap >= Math.min(2, words.size || 1); +} +// Every number in a claim must trace to some cited fact, and the claim must +// share vocabulary with at least one of them. This is what makes an invented +// achievement machine-detectable instead of something a reviewer has to catch. +export function unsupportedClaims(body: string, cited: Fact[]): string[] { + const pool = new Set(); + for (const f of cited) + for (const n of numbersIn(`${f.claim} ${f.evidence}`)) pool.add(n); + return sentences(body).filter( + (s) => isClaim(s) && !cited.some((f) => supports(s, f, pool)), + ); +} +export function clusterOf(name: string): string { + const role = name.includes('—') ? name.split('—').pop()! : name; + for (const [pattern, family] of roleFamilies) + if (pattern.test(role)) return family; + return 'general'; +} +export function words(text: string): string[] { + return text + .toLowerCase() + .replace(/[^a-z0-9\s]/g, ' ') + .split(/\s+/) + .filter(Boolean); +} +// Word-level Levenshtein: prose comparison at character level is both slower +// and less meaningful than comparing the words a reviewer actually changed. +export function editRatio(a: string, b: string): number { + const x = words(a), + y = words(b); + if (!x.length && !y.length) return 0; + if (!x.length || !y.length) return 1; + let previous = Array.from({ length: y.length + 1 }, (_, i) => i); + for (let i = 1; i <= x.length; i++) { + const row = [i]; + for (let j = 1; j <= y.length; j++) + row[j] = Math.min( + previous[j] + 1, + row[j - 1] + 1, + previous[j - 1] + (x[i - 1] === y[j - 1] ? 0 : 1), + ); + previous = row; + } + return previous[y.length] / Math.max(x.length, y.length); +} +export function draftEdit(draft: DraftRow): number { + return draft.corrected ? editRatio(draft.body, draft.corrected) : 0; +} +function median(values: number[]): number { + const sorted = [...values].sort((a, b) => a - b); + const mid = sorted.length >> 1; + return sorted.length % 2 ? sorted[mid] : (sorted[mid - 1] + sorted[mid]) / 2; +} +// Autonomy is earned per cluster, never globally. A cluster graduates only +// after enough reviewed drafts came back close to unchanged, and any later +// correction or expired fact drops it straight back to full review. +export function clusterTrust( + cluster: string, + all: DraftRow[], + staleFacts = false, +): { cluster: string; state: string; reviewed: number; edit: number } { + const reviewed = all.filter( + (d) => d.cluster === cluster && d.verdict !== 'Logged', + ); + const recent = reviewed.slice(-limits.graduationRuns); + const edit = recent.length ? median(recent.map(draftEdit)) : 1; + const state = + !staleFacts && + reviewed.length >= limits.graduationRuns && + edit <= limits.graduationEdit + ? 'Graduated' + : 'Probation'; + return { cluster, state, reviewed: reviewed.length, edit }; +} +export function trustMap( + all: DraftRow[], + staleFacts = false, +): Record> { + const out: Record> = {}; + for (const cluster of new Set(all.map((d) => d.cluster))) + out[cluster] = clusterTrust(cluster, all, staleFacts); + return out; +} +// Ordered by risk, not by convenience. An unseen role type is reviewed at the +// first draft; a full batch is the weakest signal and comes last. +export function reviewTrigger( + all: DraftRow[], +): { reason: string; ids: string[] } | null { + const pending = all.filter((d) => d.verdict === 'Logged'); + if (!pending.length) return null; + const seen = new Set( + all.filter((d) => d.verdict !== 'Logged').map((d) => d.cluster), + ); + const fresh = pending.filter((d) => !seen.has(d.cluster)); + if (fresh.length) + return { reason: 'New cluster', ids: pending.map((d) => d.id) }; + const drifted = pending.filter((d) => { + const trust = clusterTrust(d.cluster, all); + if (trust.state !== 'Graduated') return false; + const corpus = all.filter( + (o) => o.cluster === d.cluster && o.verdict !== 'Logged', + ); + return ( + median(corpus.map((o) => editRatio(d.body, o.corrected || o.body))) > + limits.driftEdit + ); + }); + if (drifted.length) + return { reason: 'Style drift', ids: pending.map((d) => d.id) }; + if ( + pending.filter((d) => d.confidence === 'low').length >= limits.lowConfidence + ) + return { reason: 'Low confidence', ids: pending.map((d) => d.id) }; + if (pending.length >= limits.batchSize) + return { reason: 'Batch full', ids: pending.map((d) => d.id) }; + return null; +} +export function opening(body: string, count = 6): string { + return words(sentences(body)[0] || '') + .slice(0, count) + .join(' '); +} +// Reviewing twelve documents one by one is the same work as before, batched. +// Grouping by shared opening turns a repeated habit into a single decision. +export function openingGroups( + batch: DraftRow[], +): { phrase: string; ids: string[] }[] { + const groups = new Map(); + for (const d of batch) { + const phrase = opening(d.corrected || d.body); + if (!phrase) continue; + groups.set(phrase, [...(groups.get(phrase) || []), d.id]); + } + return [...groups] + .filter(([, ids]) => ids.length > 1) + .map(([phrase, ids]) => ({ phrase, ids })) + .sort((a, b) => b.ids.length - a.ids.length); +} +// A profile that learns from its own accepted output homogenises. Near-identical +// letters in one batch are a defect worth blocking on, not a sign of success. +export function samenessPairs(batch: DraftRow[]): [string, string][] { + const out: [string, string][] = []; + for (let i = 0; i < batch.length; i++) + for (let j = i + 1; j < batch.length; j++) + if ( + editRatio( + batch[i].corrected || batch[i].body, + batch[j].corrected || batch[j].body, + ) <= limits.samenessEdit + ) + out.push([batch[i].id, batch[j].id]); + return out; +} +export function factUsage( + batch: DraftRow[], + facts: Fact[], +): { fact: Fact; count: number }[] { + const counts = new Map(); + for (const d of batch) + for (const id of d.cited.split(',').filter(Boolean)) + counts.set(id, (counts.get(id) || 0) + 1); + return facts + .filter((f) => counts.has(f.id)) + .map((f) => ({ fact: f, count: counts.get(f.id)! })) + .sort((a, b) => b.count - a.count); +} +// Most people cannot describe their own voice but recognise a violation at a +// glance. Style is therefore elicited by correction: the reviewer edits, and +// these are the generalisations offered back for confirmation. +export function proposeRules(before: string, after: string): string[] { + const out: string[] = []; + if (!after.trim() || before.trim() === after.trim()) return out; + const openBefore = opening(before), + openAfter = opening(after); + if (openBefore && openBefore !== openAfter) + out.push(`Do not open with "${openBefore}".`); + const kept = new Set(words(after)); + const tokens = (before.match(/[A-Za-z0-9'-]+/g) || []).filter(Boolean); + let run: string[] = []; + for (const token of [...tokens, '']) { + const plain = token.toLowerCase().replace(/[^a-z0-9]/g, ''); + if (plain && !kept.has(plain) && !stop.has(plain)) run.push(token); + else { + if (run.length >= 2) out.push(`Avoid the phrase "${run.join(' ')}".`); + run = []; + } + } + const wordsAfter = words(after).length; + if (wordsAfter && words(before).length > wordsAfter * 1.25) + out.push(`Keep drafts near ${Math.round(wordsAfter / 10) * 10} words.`); + return [...new Set(out)].slice(0, 3); +} +// What the writing agent is actually handed. Verified, unexpired facts only; +// rules narrowed to the cluster in play. The agent reads this and never writes +// it — a generator that edits its own instructions drifts toward whatever is +// cheapest to produce. +export function profileBrief( + facts: Fact[], + rules: Rule[], + cluster: string, + now: string, + version: number, +) { + return { + profile_version: version, + cluster, + facts: facts + .filter((f) => usableFact(f, now)) + .map((f) => ({ id: f.id, claim: f.claim, tag: f.tag })), + style: rules + .filter((r) => r.scope === 'global' || r.scope === cluster) + .map((r) => r.rule), + rules_of_use: [ + 'Every sentence making a factual claim must cite a fact id from this brief.', + 'Numbers may only come from cited facts.', + 'Set confidence "low" when a required fact is missing rather than guessing.', + ], + }; +} +export function validateFact(v: unknown): { + claim: string; + evidence: string; + tag: string; +} { + const f = v as { claim?: unknown; evidence?: unknown; tag?: unknown }; + if ( + typeof f?.claim !== 'string' || + !f.claim.trim() || + f.claim.length > 500 || + (f.evidence != null && typeof f.evidence !== 'string') || + ((f.evidence as string)?.length || 0) > 2000 + ) + throw Error('A fact needs a claim under 500 characters.'); + const tag = typeof f.tag === 'string' ? f.tag : 'detail'; + if (!(factTags as readonly string[]).includes(tag)) + throw Error('Use a known fact tag.'); + return { + claim: f.claim.trim(), + evidence: ((f.evidence as string) || '').trim(), + tag, + }; +} +export function validateRule(v: unknown): { rule: string; scope: string } { + const r = v as { rule?: unknown; scope?: unknown }; + if (typeof r?.rule !== 'string' || !r.rule.trim() || r.rule.length > 300) + throw Error('A style rule needs text under 300 characters.'); + const scope = typeof r.scope === 'string' && r.scope ? r.scope : 'global'; + if (scope.length > 40) throw Error('Use a shorter rule scope.'); + return { rule: r.rule.trim(), scope }; +} +// The agent may log a draft, never accept one. A draft carrying an unsupported +// claim is refused outright: it must not reach a batch where a tired reviewer +// might wave it through. +export function validateDraftLog( + v: unknown, + facts: Fact[], + now: string, +): { body: string; cited: string[]; confidence: string } { + const d = v as { body?: unknown; cited?: unknown; confidence?: unknown }; + if (typeof d?.body !== 'string' || !d.body.trim() || d.body.length > 20000) + throw Error('A logged draft needs body text under 20000 characters.'); + const confidence = typeof d.confidence === 'string' ? d.confidence : 'high'; + if (!(confidences as readonly string[]).includes(confidence)) + throw Error('Confidence must be high or low.'); + const cited = Array.isArray(d.cited) ? d.cited : []; + if (cited.some((id) => typeof id !== 'string')) + throw Error('Cited facts must be fact ids.'); + const known = new Map(facts.map((f) => [f.id, f])); + const used: Fact[] = []; + for (const id of cited as string[]) { + const fact = known.get(id); + if (!fact) throw Error(`Cited fact ${id} does not exist.`); + if (!usableFact(fact, now)) + throw Error( + `Fact ${id} is not verified or has expired. Verify it before citing it.`, + ); + used.push(fact); + } + const unsupported = unsupportedClaims(d.body, used); + if (unsupported.length) + throw Error( + `Unsupported claim: "${unsupported[0]}". Cite a verified fact or remove the claim.`, + ); + return { body: d.body, cited: cited as string[], confidence }; +} diff --git a/lib/resume.ts b/lib/resume.ts new file mode 100644 index 0000000..57bb50e --- /dev/null +++ b/lib/resume.ts @@ -0,0 +1,69 @@ +// Resume extraction is deliberately deterministic and local. It proposes +// candidate facts; it never verifies them. Nothing here reaches a draft until +// a human has confirmed the individual line, so a generous extractor with a +// strict verification gate beats a clever extractor that is trusted. +export type Candidate = { claim: string; evidence: string; tag: string }; +const sectionWords = + /^(experience|work|employment|education|projects?|skills?|publications?|awards?|certifications?|summary|profile|objective|interests?|references?|volunteering|leadership)\b/i; +const contact = + /(@|https?:\/\/|www\.|linkedin|github\.com|\+?\d[\d\s().-]{7,}\d)/i; +const credentialWords = + /\b(bachelor|master|mba|ph\.?d|b\.?s\.?c?|m\.?s\.?c?|degree|certified|certificate|certification|licen[cs]e|diploma|graduated|magna|summa|cum laude)\b/i; +const roleWords = + /\b(engineer|developer|scientist|manager|director|analyst|designer|intern|lead|architect|consultant|founder|president|head of|principal|senior|staff)\b/i; +const metricSignal = + /\d+\s*(%|percent|x\b|k\b|m\b|ms\b|s\b|hrs?\b|hours?\b)|[$€£]\s*\d/i; +const claimVerb = + /\b(led|built|shipped|increased|reduced|managed|founded|scaled|owned|launched|published|won|earned|grew|saved|delivered|architected|designed|created|improved|cut|drove|hired|mentored|taught|wrote|released|migrated|authored|raised|generated|developed|implemented|maintained|automated)\b/i; +function tagFor(line: string): string { + if (metricSignal.test(line)) return 'metric'; + if (credentialWords.test(line)) return 'credential'; + if (roleWords.test(line) && /\b(19|20)\d{2}\b/.test(line)) return 'role'; + return 'detail'; +} +export function parseResume(text: string): Candidate[] { + if (typeof text !== 'string' || !text.trim()) + throw Error('Paste resume text to extract facts.'); + if (text.length > 100000) + throw Error('Paste a resume under 100000 characters.'); + const out: Candidate[] = []; + const seen = new Set(); + let section = 'Resume'; + for (const raw of text.split(/\r?\n/)) { + const line = raw.replace(/^[\s•·▪◦*–—>-]+/, '').trim(); + if (!line) continue; + if ( + sectionWords.test(line) && + line.length < 40 && + !claimVerb.test(line) && + !/\d/.test(line) + ) { + section = line.replace(/[:\s]+$/, ''); + continue; + } + if (line.length < 12 || line.length > 300) continue; + if (contact.test(line)) continue; + // A line earns a place in the queue only if it asserts something a + // reviewer could confirm or reject. + if ( + !/\d/.test(line) && + !claimVerb.test(line) && + !credentialWords.test(line) + ) + continue; + const key = line.toLowerCase().replace(/\s+/g, ' '); + if (seen.has(key)) continue; + seen.add(key); + out.push({ + claim: line, + evidence: `Resume · ${section}`, + tag: tagFor(line), + }); + if (out.length >= 60) break; + } + if (!out.length) + throw Error( + 'No candidate facts found. Paste the experience and education sections.', + ); + return out; +} diff --git a/lib/store.ts b/lib/store.ts new file mode 100644 index 0000000..a24d101 --- /dev/null +++ b/lib/store.ts @@ -0,0 +1,40 @@ +import type { Fact, Rule, DraftRow } from './profile'; +export async function loadFacts(db: D1Database, user: string) { + const r = await db + .prepare('SELECT * FROM profile_facts WHERE owner=? ORDER BY created') + .bind(user) + .all(); + return r.results; +} +export async function loadRules(db: D1Database, user: string) { + const r = await db + .prepare('SELECT * FROM style_rules WHERE owner=? ORDER BY created') + .bind(user) + .all(); + return r.results; +} +export async function loadDrafts(db: D1Database, user: string) { + const r = await db + .prepare('SELECT * FROM drafts WHERE owner=? ORDER BY created') + .bind(user) + .all(); + return r.results; +} +export async function profileVersion(db: D1Database, user: string) { + const row = await db + .prepare('SELECT profile_version FROM profile_state WHERE owner=?') + .bind(user) + .first<{ profile_version: number }>(); + return row?.profile_version ?? 1; +} +// Every profile change advances the version, and every draft records the +// version it was written under. Without that stamp a bad draft cannot be +// attributed to a bad agent rather than a stale profile, and the calibration +// loop stops being falsifiable. +export function bumpProfile(db: D1Database, user: string, now: string) { + return db + .prepare( + 'INSERT INTO profile_state (owner,profile_version,updated) VALUES (?,2,?) ON CONFLICT(owner) DO UPDATE SET profile_version=profile_state.profile_version+1, updated=excluded.updated', + ) + .bind(user, now); +} diff --git a/package.json b/package.json index 2b79652..3e43e93 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "lint": "oxlint app lib db tests", "format": "oxfmt", "db:generate": "drizzle-kit generate", - "test": "node --disable-warning=ExperimentalWarning --test tests/domain.test.mjs tests/connectors.test.mjs tests/import.test.mjs tests/editor.test.mjs", + "test": "node --disable-warning=ExperimentalWarning --test tests/domain.test.mjs tests/connectors.test.mjs tests/import.test.mjs tests/editor.test.mjs tests/profile.test.mjs", "relay": "node integrations/relay.mjs" }, "dependencies": { diff --git a/tests/calibration.test.mjs b/tests/calibration.test.mjs new file mode 100644 index 0000000..979f053 --- /dev/null +++ b/tests/calibration.test.mjs @@ -0,0 +1,188 @@ +import assert from 'node:assert/strict'; +const base = process.env.RELAY_TEST_URL || 'http://localhost:3000'; + +// Only run against a local development server, which uses mock authentication. +if (!['localhost', '127.0.0.1'].includes(new URL(base).hostname)) + throw Error('API test is local-only.'); +const signIn = await fetch(base + '/signin-with-chatgpt?return_to=/', { + redirect: 'manual', +}); +const headers = { cookie: signIn.headers.get('set-cookie').split(';')[0] }; +async function call(path, body) { + const r = await fetch(base + path, { + method: body ? 'POST' : 'GET', + headers: { + ...headers, + ...(body ? { 'Content-Type': 'application/json' } : {}), + }, + ...(body ? { body: JSON.stringify(body) } : {}), + }); + return { status: r.status, data: await r.json() }; +} + +await call('/api/workspace', { action: 'bootstrap' }); +const workspace = (await call('/api/workspace')).data; +const job = workspace.jobs.find( + (j) => j.job_key === 'https://example.com/jobs/backend', +); +assert.ok(job, 'fictional backend job is present'); + +// Extraction proposes only. Nothing it returns is usable until verified. +const extracted = await call('/api/profile', { + action: 'extract', + text: [ + 'EXPERIENCE', + 'Led a team of 6 engineers at Northstar Example from 2023 to 2025', + 'Reduced ingestion p99 latency from 400ms to 40ms', + ].join('\n'), +}); +assert.equal(extracted.status, 200, JSON.stringify(extracted.data)); +assert.equal(extracted.data.candidates.length, 2); +const beforePropose = (await call('/api/profile')).data; +assert.equal(beforePropose.usable, 0, 'extraction writes nothing'); + +await call('/api/profile', { + action: 'propose', + facts: extracted.data.candidates, +}); +const proposed = (await call('/api/profile')).data; +assert.equal(proposed.facts.length, 2); +assert.equal(proposed.usable, 0, 'proposed facts are not yet citable'); + +// A draft may not cite a fact that has only been proposed. +const teamFact = proposed.facts.find((f) => f.claim.startsWith('Led a team')); +const early = await call('/api/drafts', { + action: 'log', + job_id: job.id, + body: 'I led a team of 6 engineers.', + cited: [teamFact.id], +}); +assert.equal(early.status, 400); +assert.match(early.data.error, /not verified or has expired/); + +await call('/api/profile', { action: 'verify', id: teamFact.id }); +const afterVerify = (await call('/api/profile')).data; +assert.equal(afterVerify.usable, 1); +assert.ok( + afterVerify.version > beforePropose.version, + 'verification advances the profile version', +); + +// An uncited claim is refused outright rather than queued for review. +const uncited = await call('/api/drafts', { + action: 'log', + job_id: job.id, + body: 'I led a team of 6 engineers and grew revenue 300%.', + cited: [teamFact.id], +}); +assert.equal(uncited.status, 400); +assert.match(uncited.data.error, /Unsupported claim/); +assert.equal( + (await call('/api/drafts')).data.drafts.length, + 0, + 'a refused draft is not written', +); + +// A supported claim logs, and lands on probation rather than in the workspace. +const logged = await call('/api/drafts', { + action: 'log', + job_id: job.id, + body: 'Your storage work is why I write. I led a team of 6 engineers.', + cited: [teamFact.id], +}); +assert.equal(logged.status, 200, JSON.stringify(logged.data)); +assert.equal(logged.data.cluster, 'backend'); +assert.equal(logged.data.staged, false, 'an unproven cluster does not stage'); +assert.equal(logged.data.trust, 'Probation'); +assert.equal(logged.data.review.reason, 'New cluster'); +assert.equal( + (await call('/api/workspace')).data.jobs.find((j) => j.id === job.id).draft, + '', + 'the job record is untouched while the cluster is on probation', +); + +// The session is the calibration step: a correction becomes a rule. +const opened = await call('/api/drafts', { action: 'open-batch' }); +assert.equal(opened.status, 200, JSON.stringify(opened.data)); +assert.equal(opened.data.reason, 'New cluster'); +const drafts = (await call('/api/drafts')).data.drafts; +assert.equal(drafts[0].batch, opened.data.id); + +const corrected = await call('/api/drafts', { + action: 'correct', + id: drafts[0].id, + corrected: 'I led a team of 6 engineers on ingestion.', +}); +assert.equal(corrected.status, 200, JSON.stringify(corrected.data)); +assert.ok(corrected.data.proposals.length, 'a correction proposes rules'); + +const versionBeforeClose = (await call('/api/profile')).data.version; +const closed = await call('/api/drafts', { + action: 'close-batch', + id: opened.data.id, + rules: [{ rule: 'Open with the employer, not yourself.', scope: 'backend' }], +}); +assert.equal(closed.status, 200, JSON.stringify(closed.data)); +const afterClose = (await call('/api/profile')).data; +assert.equal(afterClose.rules.length, 1); +assert.ok( + afterClose.version > versionBeforeClose, + 'closing with rules advances the profile version', +); +const afterBatch = (await call('/api/drafts')).data; +assert.equal(afterBatch.drafts[0].verdict, 'Corrected'); +assert.equal(afterBatch.trigger, null, 'the queue is clear after review'); + +// Repeated clean reviews graduate the cluster, which then stages unattended. +// Vary the wording without introducing numbers: any digit absent from the +// cited facts is refused, which is the behaviour asserted above. +for (const area of ['ingestion', 'storage', 'billing', 'search', 'indexing']) { + const body = `Your storage work is why I write. I led a team of 6 engineers on the ${area} path.`; + const run = await call('/api/drafts', { + action: 'log', + job_id: job.id, + body, + cited: [teamFact.id], + }); + assert.equal(run.status, 200, JSON.stringify(run.data)); + const queue = (await call('/api/drafts')).data.drafts; + const latest = queue.at(-1); + await call('/api/drafts', { action: 'accept', id: latest.id }); +} +const graduated = (await call('/api/drafts')).data; +assert.equal( + graduated.trust.backend.state, + 'Graduated', + 'five clean reviews graduate the cluster', +); + +const staged = await call('/api/drafts', { + action: 'log', + job_id: job.id, + body: 'Your storage work is why I write. I led a team of 6 engineers here.', + cited: [teamFact.id], +}); +assert.equal(staged.data.staged, true, 'a graduated cluster stages its draft'); +const stagedJob = (await call('/api/workspace')).data.jobs.find( + (j) => j.id === job.id, +); +assert.match(stagedJob.draft, /I led a team of 6 engineers here/); +assert.equal(stagedJob.status, 'Held', 'staging never changes status'); +assert.equal( + stagedJob.accepted_draft, + null, + 'staging never accepts on the human’s behalf', +); + +// Retiring the cited fact must not silently leave graduated autonomy running. +await call('/api/profile', { action: 'retire', id: teamFact.id }); +const blocked = await call('/api/drafts', { + action: 'log', + job_id: job.id, + body: 'I led a team of 6 engineers.', + cited: [teamFact.id], +}); +assert.equal(blocked.status, 400); +assert.match(blocked.data.error, /not verified or has expired/); + +console.log('Calibration API checks passed.'); diff --git a/tests/profile.test.mjs b/tests/profile.test.mjs new file mode 100644 index 0000000..03a33fd --- /dev/null +++ b/tests/profile.test.mjs @@ -0,0 +1,352 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { + clusterOf, + clusterTrust, + editRatio, + factUsage, + isClaim, + limits, + openingGroups, + profileBrief, + proposeRules, + reviewTrigger, + samenessPairs, + unsupportedClaims, + usableFact, + validateDraftLog, + validateFact, + validateRule, +} from '../lib/profile.ts'; +import { parseResume } from '../lib/resume.ts'; +const NOW = '2026-09-05T00:00:00.000Z'; +function fact(id, claim, extra = {}) { + return { + id, + claim, + evidence: '', + tag: 'detail', + status: 'Verified', + verified: NOW, + expires: null, + ...extra, + }; +} +function draft(id, extra = {}) { + return { + id, + job_id: 'j1', + cluster: 'backend', + body: 'A draft body.', + corrected: '', + cited: '', + confidence: 'high', + verdict: 'Logged', + profile_version: 1, + ...extra, + }; +} + +test('claims are recognised by numbers or first-person achievement', () => { + assert.ok(isClaim('I reduced p99 latency by 40%.')); + assert.ok(isClaim('I led the payments team.')); + assert.ok(isClaim('Shipped 3 releases in 2025.')); + assert.ok(!isClaim('I am drawn to your work on distributed systems.')); + assert.ok(!isClaim('Why does your team favour Rust?')); + assert.ok(!isClaim('Your infrastructure post was excellent.')); +}); + +test('a noun that doubles as a verb does not manufacture a claim', () => { + // "work", "design" and "lead" are nouns here, so these need no citation. + assert.ok(!isClaim('I admire the design of your ingestion layer.')); + assert.ok(!isClaim('I read your work on consensus.')); + // The same words as real verbs remain claims and still need one. + assert.ok(isClaim('I own the billing service.')); + assert.ok(isClaim('I design ingestion pipelines.')); +}); + +test('an intransitive verb frames the letter and is not a claim', () => { + // Common cover-letter framing: the verb asserts nothing about the applicant. + assert.ok(!isClaim('Your storage work is why I write.')); + assert.ok(!isClaim('That is the reason I am writing.')); + // The same verb with an object is an assertion and still needs a citation. + assert.ok(isClaim('I write release tooling.')); +}); + +test('an uncited achievement is refused, a cited one passes', () => { + const facts = [fact('f1', 'Led a team of 6 engineers at Northstar')]; + assert.deepEqual( + unsupportedClaims('I led a team of 6 engineers.', facts), + [], + ); + assert.equal(unsupportedClaims('I led a team of 6 engineers.', []).length, 1); +}); + +test('a number absent from every cited fact is unsupported', () => { + const facts = [fact('f1', 'Reduced p99 latency from 400ms to 40ms')]; + assert.deepEqual( + unsupportedClaims('I reduced p99 latency to 40ms.', facts), + [], + ); + // The inflated figure must not pass merely because the sentence looks similar. + assert.equal( + unsupportedClaims('I reduced p99 latency to 4ms.', facts).length, + 1, + ); +}); + +test('non-claim sentences never require a citation', () => { + assert.deepEqual( + unsupportedClaims( + 'Your work on storage engines is why I am writing. I would welcome a conversation.', + [], + ), + [], + ); +}); + +test('logging refuses unverified, expired and uncited facts', () => { + const verified = [fact('f1', 'Shipped 12 releases in 2025')]; + assert.doesNotThrow(() => + validateDraftLog( + { body: 'I shipped 12 releases in 2025.', cited: ['f1'] }, + verified, + NOW, + ), + ); + assert.throws( + () => + validateDraftLog( + { body: 'I shipped 12 releases in 2025.', cited: [] }, + verified, + NOW, + ), + /Unsupported claim/, + ); + assert.throws( + () => + validateDraftLog( + { body: 'Hello.', cited: ['f1'] }, + [fact('f1', 'x', { status: 'Proposed' })], + NOW, + ), + /not verified or has expired/, + ); + assert.throws( + () => + validateDraftLog( + { body: 'Hello.', cited: ['f1'] }, + [fact('f1', 'x', { expires: '2026-01-01T00:00:00.000Z' })], + NOW, + ), + /not verified or has expired/, + ); + assert.throws( + () => validateDraftLog({ body: 'Hello.', cited: ['nope'] }, verified, NOW), + /does not exist/, + ); +}); + +test('expiry governs whether a verified fact is usable', () => { + assert.ok(usableFact(fact('f1', 'x'), NOW)); + assert.ok(!usableFact(fact('f1', 'x', { status: 'Retired' }), NOW)); + assert.ok( + !usableFact(fact('f1', 'x', { expires: '2026-01-01T00:00:00.000Z' }), NOW), + ); + assert.ok( + usableFact(fact('f1', 'x', { expires: '2027-01-01T00:00:00.000Z' }), NOW), + ); +}); + +test('role families cluster distinct jobs and fall back to general', () => { + assert.equal(clusterOf('Northstar Example — Backend Engineer'), 'backend'); + assert.equal(clusterOf('Harbor Example — Platform Engineer'), 'platform'); + assert.equal(clusterOf('Cedar Example — Research Scientist'), 'research'); + assert.equal(clusterOf('Vale Example — Office Coordinator'), 'general'); +}); + +test('edit ratio measures words changed, not characters', () => { + assert.equal(editRatio('one two three', 'one two three'), 0); + assert.equal(editRatio('', ''), 0); + assert.equal(editRatio('one two three four', 'one two three five'), 0.25); + // Case and punctuation are not style edits worth counting. + assert.equal(editRatio('One two, three!', 'one TWO three'), 0); + assert.ok(editRatio('completely different text here', 'nothing alike') > 0.8); +}); + +test('a cluster graduates only after enough near-unchanged reviews', () => { + const clean = Array.from({ length: limits.graduationRuns }, (_, i) => + draft('d' + i, { verdict: 'Reviewed' }), + ); + assert.equal(clusterTrust('backend', clean).state, 'Graduated'); + // One draft short of the threshold is still probation. + assert.equal(clusterTrust('backend', clean.slice(1)).state, 'Probation'); + // Heavy corrections keep it on probation however many there are. + const edited = Array.from({ length: limits.graduationRuns }, (_, i) => + draft('e' + i, { + verdict: 'Corrected', + body: 'one two three four five six', + corrected: 'entirely other words appear here now', + }), + ); + assert.equal(clusterTrust('backend', edited).state, 'Probation'); +}); + +test('an expired fact demotes a graduated cluster', () => { + const clean = Array.from({ length: limits.graduationRuns }, (_, i) => + draft('d' + i, { verdict: 'Reviewed' }), + ); + assert.equal(clusterTrust('backend', clean, true).state, 'Probation'); +}); + +test('an unseen cluster triggers review at the first draft', () => { + const reviewed = Array.from({ length: 3 }, (_, i) => + draft('r' + i, { verdict: 'Reviewed' }), + ); + assert.equal( + reviewTrigger([...reviewed, draft('new', { cluster: 'research' })]).reason, + 'New cluster', + ); +}); + +test('low confidence and batch size trigger in that order', () => { + const seen = Array.from({ length: 3 }, (_, i) => + draft('r' + i, { verdict: 'Reviewed' }), + ); + const lowConf = Array.from({ length: limits.lowConfidence }, (_, i) => + draft('l' + i, { confidence: 'low' }), + ); + assert.equal(reviewTrigger([...seen, ...lowConf]).reason, 'Low confidence'); + const full = Array.from({ length: limits.batchSize }, (_, i) => + draft('p' + i), + ); + assert.equal(reviewTrigger([...seen, ...full]).reason, 'Batch full'); + assert.equal(reviewTrigger([...seen, draft('one')]), null); + assert.equal(reviewTrigger([]), null); +}); + +test('drift in a graduated cluster triggers before the batch fills', () => { + const corpus = Array.from({ length: limits.graduationRuns }, (_, i) => + draft('c' + i, { + verdict: 'Reviewed', + body: 'I write clearly about storage engines and latency work', + }), + ); + const trigger = reviewTrigger([ + ...corpus, + draft('odd', { body: 'Totally unrelated wording with nothing shared' }), + ]); + assert.equal(trigger.reason, 'Style drift'); +}); + +test('a repeated opening groups into one decision', () => { + const batch = [ + draft('a', { body: 'I was excited to see this role posted. More text.' }), + draft('b', { body: 'I was excited to see this role posted. Other text.' }), + draft('c', { body: 'Your storage work is why I write. Different.' }), + ]; + const groups = openingGroups(batch); + assert.equal(groups.length, 1); + assert.deepEqual(groups[0].ids, ['a', 'b']); +}); + +test('near-identical drafts in one batch are flagged', () => { + const batch = [ + draft('a', { body: 'one two three four five six seven eight' }), + draft('b', { body: 'one two three four five six seven eight' }), + draft('c', { body: 'completely unrelated sentence with other words here' }), + ]; + assert.deepEqual(samenessPairs(batch), [['a', 'b']]); +}); + +test('fact usage rolls a repeated claim into one verification', () => { + const facts = [fact('f1', 'Latency work'), fact('f2', 'Team size')]; + const batch = [ + draft('a', { cited: 'f1' }), + draft('b', { cited: 'f1,f2' }), + draft('c', { cited: 'f1' }), + ]; + assert.deepEqual( + factUsage(batch, facts).map((u) => [u.fact.id, u.count]), + [ + ['f1', 3], + ['f2', 1], + ], + ); +}); + +test('a correction generalises into confirmable style rules', () => { + const rules = proposeRules( + 'I was excited to see this role posted at your company.', + 'Your storage work is why I am writing.', + ); + assert.ok(rules.some((r) => r.startsWith('Do not open with'))); + assert.ok(rules.length <= 3); + assert.deepEqual(proposeRules('same text', 'same text'), []); +}); + +test('the brief exposes only usable facts and applicable rules', () => { + const facts = [ + fact('f1', 'Verified claim'), + fact('f2', 'Proposed claim', { status: 'Proposed' }), + fact('f3', 'Expired claim', { expires: '2026-01-01T00:00:00.000Z' }), + ]; + const rules = [ + { id: 'r1', rule: 'Global rule', scope: 'global' }, + { id: 'r2', rule: 'Backend rule', scope: 'backend' }, + { id: 'r3', rule: 'Research rule', scope: 'research' }, + ]; + const brief = profileBrief(facts, rules, 'backend', NOW, 7); + assert.deepEqual( + brief.facts.map((f) => f.id), + ['f1'], + ); + assert.deepEqual(brief.style, ['Global rule', 'Backend rule']); + assert.equal(brief.profile_version, 7); +}); + +test('fact and rule input is bounded', () => { + assert.deepEqual(validateFact({ claim: ' Led a team ', tag: 'role' }), { + claim: 'Led a team', + evidence: '', + tag: 'role', + }); + assert.throws(() => validateFact({ claim: '' }), /needs a claim/); + assert.throws( + () => validateFact({ claim: 'x', tag: 'invented' }), + /known fact tag/, + ); + assert.equal(validateRule({ rule: 'Be brief' }).scope, 'global'); + assert.throws(() => validateRule({ rule: '' }), /needs text/); +}); + +test('resume extraction proposes checkable lines and skips furniture', () => { + const candidates = parseResume( + [ + 'Jane Doe', + 'jane@example.com | +1 555 000 1111', + 'EXPERIENCE', + '• Led a team of 6 engineers at Northstar from 2023 to 2025', + '• Reduced p99 latency by 40% across the ingestion path', + '• Enjoyed the work', + 'EDUCATION', + '• B.S. Computer Science, 2021', + ].join('\n'), + ); + const claims = candidates.map((c) => c.claim); + assert.ok(claims.some((c) => c.startsWith('Led a team of 6'))); + assert.ok(claims.some((c) => c.includes('40%'))); + assert.ok(!claims.some((c) => c.includes('jane@example.com'))); + assert.ok(!claims.includes('Enjoyed the work')); + assert.equal(candidates.find((c) => c.claim.includes('40%')).tag, 'metric'); + assert.equal( + candidates.find((c) => c.claim.startsWith('B.S.')).evidence, + 'Resume · EDUCATION', + ); +}); + +test('resume extraction reports empty and oversized input', () => { + assert.throws(() => parseResume(''), /Paste resume text/); + assert.throws(() => parseResume('Hello there friend'), /No candidate facts/); + assert.throws(() => parseResume('a'.repeat(100001)), /under 100000/); +}); From 79b575b70e2c1146b81a00634f9a431ba807ceee Mon Sep 17 00:00:00 2001 From: sykosyber Date: Sat, 5 Sep 2026 17:21:22 -0700 Subject: [PATCH 02/12] Add profile and review screens for the calibration loop The review screen is built to resist becoming a batched approval queue. Reading twelve letters in sequence is the same work as before, so it leads with repeated openings grouped into one decision, flags near-identical drafts, and rolls up the facts leaned on most. Corrections are generalised into proposed rules and confirmed as "always, or just here?", because most people cannot describe their own voice but recognise a violation instantly. Closing with an empty rule basket says plainly that the next batch will repeat the same habits. The profile screen keeps extraction and verification visibly separate: a pasted resume proposes candidates and writes nothing, and each line stays unusable until verified individually. Docs state what agents may and may not do, and that citation enforcement checks a claim against a verified fact rather than checking that the surrounding wording is true. Co-Authored-By: Claude Opus 5 --- README.md | 29 ++- app/globals.css | 129 +++++++++++++ app/profile/page.tsx | 373 ++++++++++++++++++++++++++++++++++++ app/review/page.tsx | 399 +++++++++++++++++++++++++++++++++++++++ app/workspace.tsx | 11 ++ integrations/GROK_BOT.md | 10 + integrations/README.md | 8 + 7 files changed, 956 insertions(+), 3 deletions(-) create mode 100644 app/profile/page.tsx create mode 100644 app/review/page.tsx diff --git a/README.md b/README.md index 9265bc6..8ae6702 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,11 @@ Relay is a job-search review workspace for people working with AI assistants. Br ## Available in this early release +- Keep a verified fact ledger and a learned style card that direct a writing agent. +- Seed the ledger from pasted resume text, then verify each candidate line before it can be cited. +- Let an agent log drafts unattended; a claim not traceable to a verified fact is refused, not queued. +- Review logged drafts in batches, correcting habits once instead of per letter. +- Earn autonomy per role cluster: a proven cluster stages its own drafts, and still never accepts or sends one. - Consolidate repeated posting URLs and preserve research history. - Edit notes and follow-up drafts on submitted jobs and active interviews without resetting their status. - Accept an exact draft; changing it returns it to review. @@ -22,6 +27,22 @@ The [GrokCell folder](grokcell/README.md) includes First Principles, Product Ide This is a pinned copy of the separately maintained, MIT-licensed [GrokCell project](https://github.com/sdcarlson/grokcell). Propose template improvements upstream, then refresh the copy following [its source record](grokcell/UPSTREAM.md). +## Autonomy and review + +Relay separates what an agent may do freely from what only you may do. The split is enforced in the API, not in a prompt. + +The **fact ledger** (`/profile`) holds claims you have verified. Paste resume text to propose candidates; extraction writes nothing and marks nothing verified. A fact can carry an expiry for anything that goes stale, such as a current title or a headcount. + +The **style card** holds the voice rules learned from your corrections. The agent reads both through `relay_read_profile` and never writes either. + +A writing agent logs drafts through `relay_log_draft` at whatever volume it likes. Each sentence that asserts something checkable must cite a verified fact id, and every number must appear in a cited fact. A draft that fails either rule is refused at the API and is never written, so an invented achievement cannot reach a review queue. An agent short of a fact is expected to log with `confidence: "low"` rather than guess. + +Logged drafts accumulate until review is worth your time (`/review`). A session opens on the first draft in an unseen role cluster, on style drift within a proven one, on enough low-confidence drafts, or on a full batch — in that order. The session groups repeated habits so one decision covers several drafts, flags near-identical letters, and turns your corrections into style rules. Closing a session with no rules changes nothing about the next batch. + +Autonomy is then earned per role cluster. After enough reviewed drafts come back close to unchanged, a cluster graduates and its later drafts are placed into the matching job record unattended. Staging is not acceptance: status is untouched, `accepted_draft` is not set, and accepting an exact draft remains a human action in the workspace. A correction or an expired fact returns the cluster to full review. + +Relay still does not send applications. Nothing here submits, emails or messages anyone. + ## Run locally Node 24 and pnpm are required. From the repository root: @@ -32,6 +53,7 @@ pnpm build pnpm exec wrangler d1 execute DB --local --config dist/server/wrangler.json --persist-to .wrangler/state --file drizzle/0000_sticky_robbie_robertson.sql pnpm exec wrangler d1 execute DB --local --config dist/server/wrangler.json --persist-to .wrangler/state --file drizzle/0001_careless_leader.sql pnpm exec wrangler d1 execute DB --local --config dist/server/wrangler.json --persist-to .wrangler/state --file drizzle/0002_job_key_observations.sql +pnpm exec wrangler d1 execute DB --local --config dist/server/wrangler.json --persist-to .wrangler/state --file drizzle/0003_profile_calibration.sql pnpm dev ``` @@ -39,7 +61,7 @@ Open the local URL printed by the server. Local sign-in is simulated by the Site ## Integration boundaries -The connectors are runnable local commands with file import/export in the app. Notion and Claude require your own credentials. Real Notion records were retrieved read-only through the connected Notion tool; that does not validate the standalone Notion command connector. Claude live access has not been tested. Grok Bot uses a documented command/file adapter, not an assumed proprietary API. A draft was exchanged with the installed Bot, transcribed into a validated file, then loaded in the browser; fully automatic Bot file transfer is not validated. There is no automatic background sync, autonomous hunting, application sending, or LinkedIn messaging. Generated claims still require your review. Production deployment has not been validated. +The connectors are runnable local commands with file import/export in the app. Notion and Claude require your own credentials. Real Notion records were retrieved read-only through the connected Notion tool; that does not validate the standalone Notion command connector. Claude live access has not been tested. Grok Bot uses a documented command/file adapter, not an assumed proprietary API. A draft was exchanged with the installed Bot, transcribed into a validated file, then loaded in the browser; fully automatic Bot file transfer is not validated. There is no automatic background sync, autonomous hunting, application sending, or LinkedIn messaging. Agents may log and, for a graduated cluster, stage drafts; they cannot accept one or change an application status. Citation enforcement checks that a claim traces to a fact you verified, which is not the same as checking that the surrounding wording is true, so generated text still requires your review. Production deployment has not been validated. ## Checks @@ -48,11 +70,12 @@ pnpm test pnpm exec tsc --noEmit pnpm lint node tests/api.test.mjs +node tests/calibration.test.mjs ``` -For an existing local database already on migration 0001, apply only 0002 from the setup commands. It preserves observations and repairs imported Ready records that lack matching accepted text. Imported Ready is research evidence; a new record stays Held until its exact draft is accepted in Relay. +For an existing local database already on migration 0002, apply only 0003 from the setup commands; it only adds the profile, draft and review tables and leaves existing rows untouched. For one still on migration 0001, apply 0002 and then 0003. It preserves observations and repairs imported Ready records that lack matching accepted text. Imported Ready is research evidence; a new record stays Held until its exact draft is accepted in Relay. -The last command needs a running local server and writes only fictional test records. Domain, import, editor and connector tests cover status preservation, duplicate matching, imported acceptance, observation identity, editor version conflicts, draft review rules, provider errors and pagination. Connector tests mock vendor responses; they do not prove live account access. API checks verify database read-back, stale edits, exact acceptance, status-preserving follow-up edits and authentication rejection. Local browser and WebMCP reads/import checks were exercised. Preview works before the first import. Browser file import, save and reload preserved source history. User acceptance and post-acceptance reimport are pending. +The last command needs a running local server and writes only fictional test records. Domain, import, editor and connector tests cover status preservation, duplicate matching, imported acceptance, observation identity, editor version conflicts, draft review rules, provider errors and pagination. Connector tests mock vendor responses; they do not prove live account access. Profile tests cover claim detection, citation support, resume extraction, review triggers and cluster graduation. API checks verify database read-back, stale edits, exact acceptance, status-preserving follow-up edits and authentication rejection. Calibration checks run the whole loop against a local server: extraction writing nothing, an unverified citation and an unsupported claim both refused without a row being written, a correction proposing rules, a closed session advancing the profile version, a cluster graduating into unattended staging that leaves status and acceptance alone, and a retired fact blocking further citation. Local browser and WebMCP reads/import checks were exercised. Preview works before the first import. Browser file import, save and reload preserved source history. User acceptance and post-acceptance reimport are pending. ## Hosting and privacy diff --git a/app/globals.css b/app/globals.css index c53f98f..d76f35e 100644 --- a/app/globals.css +++ b/app/globals.css @@ -704,3 +704,132 @@ footer { max-width: 100%; margin-top: 0.5rem; } + +.backlink { + display: inline-flex; + align-items: center; + gap: 6px; + font-size: 13px; + color: var(--muted); +} +.productpage .import h2 { + display: flex; + align-items: center; + gap: 8px; + margin-top: 0; + font-size: 20px; +} +.checkrow { + display: flex; + gap: 10px; + align-items: flex-start; + padding: 8px 0; + border-top: 1px solid var(--line); + font-size: 14px; +} +.checkrow small { + display: block; + color: var(--muted); + font-size: 12px; + text-transform: uppercase; + letter-spacing: 0.04em; +} +.factrow { + border-top: 1px solid var(--line); + padding: 12px 0; + font-size: 14px; +} +.factrow > b { + margin-right: 8px; +} +.factrow small { + display: block; + color: var(--muted); + font-size: 12px; + margin-top: 3px; +} +.factrow .actions { + margin-top: 8px; + align-items: center; +} +.factrow input[type='date'], +.actions input { + border: 1px solid var(--line); + border-radius: 4px; + padding: 7px 9px; + background: #fff; +} +.actions .grow { + flex: 1; + min-width: 180px; +} +.draftrow { + border-top: 1px solid var(--line); + padding: 16px 0; +} +.drafthead { + display: flex; + align-items: center; + gap: 8px; + margin-bottom: 8px; +} +.drafthead small { + color: var(--muted); + font-size: 12px; +} +.draftrow pre { + white-space: pre-wrap; + background: var(--paper); + border: 1px solid var(--line); + border-radius: 4px; + padding: 12px; + font: + 13px/1.6 Arial, + sans-serif; + max-height: 260px; + overflow: auto; +} +.draftrow textarea { + width: 100%; + min-height: 90px; + border: 1px solid var(--line); + border-radius: 4px; + padding: 10px; + margin-top: 8px; +} +.proposals { + margin-top: 10px; + padding: 12px; + border: 1px dashed var(--line); + border-radius: 4px; + background: var(--paper); + font-size: 14px; +} +.proposals b { + display: flex; + align-items: center; + gap: 6px; + margin-bottom: 8px; +} +.proposals .actions { + align-items: center; + gap: 8px; + margin-bottom: 6px; +} +.proposals .actions span { + flex: 1; +} +.report.warn { + border-color: #b0743a; + background: #fdf6ec; +} +.report.warn b { + display: flex; + align-items: center; + gap: 7px; +} +.badge.warnbadge { + background: #fdf6ec; + border-color: #b0743a; + color: #8a541f; +} diff --git a/app/profile/page.tsx b/app/profile/page.tsx new file mode 100644 index 0000000..ba9654c --- /dev/null +++ b/app/profile/page.tsx @@ -0,0 +1,373 @@ +'use client'; +import { useCallback, useEffect, useState } from 'react'; +import Link from 'next/link'; +import { + ArrowLeft, + BadgeCheck, + CircleAlert, + FileText, + Plus, + Trash2, +} from 'lucide-react'; +type Fact = { + id: string; + claim: string; + evidence: string; + tag: string; + status: string; + verified: string | null; + expires: string | null; +}; +type Rule = { id: string; rule: string; scope: string }; +type Candidate = { claim: string; evidence: string; tag: string }; +export default function Profile() { + const [facts, setFacts] = useState([]), + [rules, setRules] = useState([]), + [version, setVersion] = useState(1), + [usable, setUsable] = useState(0), + [resume, setResume] = useState(''), + [candidates, setCandidates] = useState([]), + [chosen, setChosen] = useState>(new Set()), + [expiry, setExpiry] = useState>({}), + [rule, setRule] = useState(''), + [scope, setScope] = useState('global'), + [busy, setBusy] = useState(false), + [message, setMessage] = useState(''), + [signedOut, setSignedOut] = useState(false); + const refresh = useCallback(async () => { + const r = await fetch('/api/profile'); + const data = (await r.json()) as { + facts: Fact[]; + rules: Rule[]; + version: number; + usable: number; + error?: string; + }; + if (r.status === 401) return setSignedOut(true); + if (!r.ok) throw Error(data.error); + setFacts(data.facts); + setRules(data.rules); + setVersion(data.version); + setUsable(data.usable); + }, []); + useEffect(() => { + void Promise.resolve() + .then(() => refresh()) + .catch((e: Error) => setMessage(e.message)); + }, [refresh]); + async function run(body: Record, note: string) { + setBusy(true); + setMessage(''); + try { + const r = await fetch('/api/profile', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify(body), + }); + const data = (await r.json()) as { error?: string }; + if (!r.ok) throw Error(data.error); + await refresh(); + setMessage(note); + return data; + } catch (e) { + setMessage( + e instanceof Error ? e.message : 'Unable to complete request.', + ); + } finally { + setBusy(false); + } + } + async function extract() { + setBusy(true); + setMessage(''); + try { + const r = await fetch('/api/profile', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ action: 'extract', text: resume }), + }); + const data = (await r.json()) as { + candidates?: Candidate[]; + error?: string; + }; + if (!r.ok) throw Error(data.error); + setCandidates(data.candidates!); + setChosen(new Set(data.candidates!.map((_, i) => i))); + setMessage( + `${data.candidates!.length} candidate facts found. Nothing is saved until you add them, and nothing is usable until you verify it.`, + ); + } catch (e) { + setMessage(e instanceof Error ? e.message : 'Unable to read resume.'); + } finally { + setBusy(false); + } + } + const proposed = facts.filter((f) => f.status === 'Proposed'), + verified = facts.filter((f) => f.status === 'Verified'); + if (signedOut) + return ( +
+

Your profile

+

Sign in to load your fact ledger and style card.

+ {/* oxlint-disable-next-line next/no-html-link-for-pages -- Sites authentication requires top-level navigation. */} + + Sign in with ChatGPT + +
+ ); + return ( +
+ + Workspace + +

PROFILE · WHAT THE AGENT WRITES FROM

+

Facts it may claim. Voice it must use.

+

+ The fact ledger is checked: a draft may only make a claim traceable to a + verified fact here. The style card is learned from your corrections + during review. +

+
+
+ Verified and usable + {usable.toString().padStart(2, '0')} + Citable by the writing agent +
+
+ Awaiting your check + {proposed.length.toString().padStart(2, '0')} + Extracted, not yet usable +
+
+ Style rules + {rules.length.toString().padStart(2, '0')} + Learned from review +
+
+ Profile version + {version.toString().padStart(2, '0')} + Stamped on every draft +
+
+ {message && ( +
+ {message} +
+ )} +
+

+ Seed the ledger from a resume +

+

+ Extraction runs locally and proposes candidate lines only. It never + marks anything verified. +

+