@prplkane/roundtable 0.6.6 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +61 -0
- package/README.md +83 -91
- package/SECURITY.md +25 -19
- package/docs/workflow.md +112 -63
- package/package.json +2 -2
- package/scripts/agent.mjs +2 -2
- package/scripts/mcp.mjs +5 -2
- package/skills/roundtable/SKILL.md +6 -0
- package/src/adapters/acp.js +247 -67
- package/src/adapters/catalogue.js +37 -18
- package/src/adapters/common.js +49 -7
- package/src/adapters/config.js +13 -9
- package/src/adapters/endpoint.js +2 -1
- package/src/adapters/models.js +32 -19
- package/src/adapters/spawn.js +3 -1
- package/src/agent-client.js +27 -14
- package/src/cli.js +21 -3
- package/src/diagnostics.js +1 -0
- package/src/engine.js +73 -28
- package/src/estimate.js +41 -6
- package/src/export.js +12 -6
- package/src/history.js +11 -3
- package/src/local-http.js +16 -6
- package/src/local-server.js +1 -1
- package/src/preflight.js +26 -6
- package/src/progress.js +18 -0
- package/src/prompts.js +33 -30
- package/src/research.js +46 -7
- package/src/review-view.js +11 -0
- package/src/seats.js +101 -20
- package/src/server.js +127 -40
- package/src/snapshot.js +4 -3
- package/src/source-check.js +173 -0
- package/src/store.js +2 -2
- package/src/ui/app.js +307 -128
- package/src/ui/design/app.css +3 -2
- package/src/ui/help.js +2 -1
- package/src/ui/index.html +13 -6
- package/src/validate.js +15 -3
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
// A research URL is provider output. Fetch only public HTTPS pages, pin the checked DNS
|
|
2
|
+
// answer on each connection, and examine a bounded text body without executing the page.
|
|
3
|
+
import https from 'node:https';
|
|
4
|
+
import { publicSourceUrl } from './research.js';
|
|
5
|
+
import { lookup } from 'node:dns';
|
|
6
|
+
import { BlockList, isIP } from 'node:net';
|
|
7
|
+
|
|
8
|
+
const blocked = new BlockList();
|
|
9
|
+
for (const [network, bits] of [
|
|
10
|
+
['0.0.0.0', 8], ['10.0.0.0', 8], ['100.64.0.0', 10], ['127.0.0.0', 8], ['169.254.0.0', 16],
|
|
11
|
+
['172.16.0.0', 12], ['192.0.0.0', 24], ['192.0.2.0', 24], ['192.88.99.0', 24], ['192.168.0.0', 16],
|
|
12
|
+
['198.18.0.0', 15], ['198.51.100.0', 24], ['203.0.113.0', 24], ['224.0.0.0', 3],
|
|
13
|
+
]) blocked.addSubnet(network, bits, 'ipv4');
|
|
14
|
+
for (const [network, bits] of [['2001::', 23], ['2001:db8::', 32], ['2002::', 16], ['3fff::', 20]]) blocked.addSubnet(network, bits, 'ipv6');
|
|
15
|
+
const unicast = new BlockList();
|
|
16
|
+
unicast.addSubnet('2000::', 3, 'ipv6');
|
|
17
|
+
export function publicAddress(address) {
|
|
18
|
+
const family = isIP(address);
|
|
19
|
+
return family === 4 ? !blocked.check(address, 'ipv4') : family === 6 && unicast.check(address, 'ipv6') && !blocked.check(address, 'ipv6');
|
|
20
|
+
}
|
|
21
|
+
function publicLookup(host, options, done) {
|
|
22
|
+
lookup(host, { all: true }, (error, records) => {
|
|
23
|
+
if (error) return done(error);
|
|
24
|
+
if (!records.length || !records.every(r => publicAddress(r.address))) return done(new Error('Non-public source address.'));
|
|
25
|
+
if (options.all) return done(null, records);
|
|
26
|
+
done(null, records[0].address, records[0].family);
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
function inScope(url, scopes) {
|
|
30
|
+
if (!scopes?.length) return true;
|
|
31
|
+
return scopes.some(raw => {
|
|
32
|
+
const scope = new URL(raw);
|
|
33
|
+
return scope.origin === url.origin && (scope.pathname === url.pathname || url.pathname.startsWith(scope.pathname.replace(/\/?$/, '/')));
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function readPage(url, signal) {
|
|
38
|
+
return new Promise((resolve, reject) => {
|
|
39
|
+
const req = https.get(url, { agent: false, lookup: publicLookup, signal,
|
|
40
|
+
headers: { accept: 'text/html, text/plain, application/xhtml+xml', 'accept-encoding': 'identity', 'user-agent': 'Roundtable citation check' } }, res => {
|
|
41
|
+
res.on('error', reject);
|
|
42
|
+
const redirect = [301, 302, 303, 307, 308].includes(res.statusCode) && res.headers.location;
|
|
43
|
+
if (redirect) { resolve({ redirect }); res.destroy(); return; }
|
|
44
|
+
if (res.statusCode !== 200 || !/^(?:text\/(?:html|plain)|application\/xhtml\+xml)(?:;|$)/i.test(res.headers['content-type'] || '') ||
|
|
45
|
+
(res.headers['content-encoding'] && res.headers['content-encoding'] !== 'identity')) {
|
|
46
|
+
reject(new Error('No readable source page.')); res.destroy(); return;
|
|
47
|
+
}
|
|
48
|
+
const chunks = [];
|
|
49
|
+
let bytes = 0;
|
|
50
|
+
res.on('data', chunk => {
|
|
51
|
+
bytes += chunk.length;
|
|
52
|
+
if (bytes > 2 * 1024 * 1024) { res.destroy(new Error('Source page exceeds the byte limit.')); return; }
|
|
53
|
+
chunks.push(chunk);
|
|
54
|
+
});
|
|
55
|
+
res.on('end', () => resolve({ body: Buffer.concat(chunks).toString('utf8') }));
|
|
56
|
+
});
|
|
57
|
+
req.on('error', reject);
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Treat page text as evidence only. Discard provider paraphrases, guessed dates and titles.
|
|
62
|
+
// Separate structural boundaries before decoding entities. Skip hidden sections in one pass.
|
|
63
|
+
function visibleText(html) {
|
|
64
|
+
const tags = /<!--[\s\S]*?(?:-->|$)|<\/?([a-z][\w:-]*)\b[^<>]{0,2048}>/gi;
|
|
65
|
+
const hidden = new Set(['script', 'style', 'template', 'noscript', 'head', 'nav', 'footer']);
|
|
66
|
+
const blocks = new Set(['p', 'li', 'br', 'div', 'td', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6']);
|
|
67
|
+
const parts = [];
|
|
68
|
+
let from = 0, skip = null;
|
|
69
|
+
for (const match of html.matchAll(tags)) {
|
|
70
|
+
if (!skip) parts.push(html.slice(from, match.index));
|
|
71
|
+
const tag = match[1]?.toLowerCase();
|
|
72
|
+
const closing = match[0][1] === '/';
|
|
73
|
+
if (!skip && hidden.has(tag) && !closing) skip = tag;
|
|
74
|
+
else if (skip === tag && closing) skip = null;
|
|
75
|
+
if (!skip && blocks.has(tag)) parts.push('\n');
|
|
76
|
+
from = match.index + match[0].length;
|
|
77
|
+
}
|
|
78
|
+
if (!skip) parts.push(html.slice(from));
|
|
79
|
+
return parts.join('');
|
|
80
|
+
}
|
|
81
|
+
function plain(html) {
|
|
82
|
+
const entities = { amp: '&', lt: '<', gt: '>', quot: '"', apos: "'", nbsp: ' ', ndash: '–', mdash: '—', rsquo: '’', lsquo: '‘' };
|
|
83
|
+
return visibleText(html)
|
|
84
|
+
.replace(/&(?:#(x[\da-f]+|\d+)|([a-z]+));/gi, (s, num, name) => {
|
|
85
|
+
if (!num) return entities[name.toLowerCase()] ?? s;
|
|
86
|
+
const n = num[0].toLowerCase() === 'x' ? parseInt(num.slice(1), 16) : Number(num);
|
|
87
|
+
return n > 0 && n <= 0x10ffff ? String.fromCodePoint(n) : ' ';
|
|
88
|
+
}).replace(/[ \t\r]+/g, ' ').trim();
|
|
89
|
+
}
|
|
90
|
+
const norm = text => text.normalize('NFKC').toLowerCase().replace(/[‘’]/g, "'").replace(/[“”]/g, '"').replace(/\s+/g, ' ').trim();
|
|
91
|
+
const stopWords = new Set('about after also before between calls could described different does each example first following from have into more other query return search should source specifies that their them these they this those using what when where which will with would'.split(' '));
|
|
92
|
+
const keywords = s => new Set(norm(s).match(/[\p{L}\p{N}]+/gu)?.filter(w => w.length >= 3 && !stopWords.has(w)));
|
|
93
|
+
|
|
94
|
+
function publicationDate(body, proposed) {
|
|
95
|
+
if (!proposed) return null;
|
|
96
|
+
// Accept only explicit publication metadata, and only the date the searcher proposed.
|
|
97
|
+
for (const [tag] of body.matchAll(/<(?:meta|time)\b[^<>]{0,2048}>/gi)) {
|
|
98
|
+
const attributes = Object.fromEntries([...tag.matchAll(/\s([\w:-]+)\s*=\s*(["'])(.*?)\2/g)].map(m => [m[1].toLowerCase(), m[3]]));
|
|
99
|
+
if (attributes.property !== 'article:published_time' && attributes.itemprop !== 'datePublished') continue;
|
|
100
|
+
const date = /^(\d{4}-\d{2}-\d{2})(?:T|$)/.exec(attributes.content || attributes.datetime || '')?.[1];
|
|
101
|
+
if (date === proposed) return date;
|
|
102
|
+
}
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function pageEvidence(body, source, query, url) {
|
|
107
|
+
const text = plain(body);
|
|
108
|
+
const quote = source.excerpt.trim().replace(/^["“]|["”]$/g, '');
|
|
109
|
+
const titleStart = /<title\b[^<>]{0,500}>/i.exec(body);
|
|
110
|
+
const title = titleStart && plain(body.slice(titleStart.index + titleStart[0].length).split('<', 1)[0]).slice(0, 200) || url.hostname;
|
|
111
|
+
const wanted = keywords(query);
|
|
112
|
+
const claim = keywords(quote);
|
|
113
|
+
const passages = text.split(/\n+|(?<=[.!?])\s+(?=[A-Z])/u);
|
|
114
|
+
const shares = (words, set) => [...words].filter(w => set.has(w)).length;
|
|
115
|
+
const exact = quote.length >= 18 && quote.length <= 450 && claim.size >= 3 && norm(text).includes(norm(quote));
|
|
116
|
+
if (exact && shares(claim, wanted) >= 1) return { title, excerpt: quote };
|
|
117
|
+
// If search returned a paraphrase, send only an actual, relevant passage. The seat receives
|
|
118
|
+
// the passage itself, never the original inference. Home pages need an exact quote.
|
|
119
|
+
if (url.pathname === '/') return null;
|
|
120
|
+
let best = null, score = 0;
|
|
121
|
+
for (const passage of passages) {
|
|
122
|
+
const line = passage.trim();
|
|
123
|
+
if (line.length < 30 || line.length > 450) continue;
|
|
124
|
+
const words = keywords(line), support = shares(words, claim), topic = shares(words, wanted);
|
|
125
|
+
if (support < 3 || topic < 2 || support / Math.max(1, Math.min(claim.size, words.size)) < 0.35) continue;
|
|
126
|
+
if (support + topic > score) { best = line; score = support + topic; }
|
|
127
|
+
}
|
|
128
|
+
return best ? { title, excerpt: best } : null;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export async function verifyResearchSources(sources, settings, signal, read = readPage) {
|
|
132
|
+
const verification = { checked: 0, unavailable: 0, outsideScope: 0, unsupported: 0, duplicates: 0 };
|
|
133
|
+
const kept = [], seen = new Set(), sameEvidence = new Set();
|
|
134
|
+
const budget = AbortSignal.timeout(20_000);
|
|
135
|
+
async function check(source) {
|
|
136
|
+
signal.throwIfAborted();
|
|
137
|
+
let url = new URL(source.url), issue = 'unavailable';
|
|
138
|
+
verification.checked++;
|
|
139
|
+
url.hash = '';
|
|
140
|
+
try {
|
|
141
|
+
const deadline = AbortSignal.any([signal, budget, AbortSignal.timeout(6000)]);
|
|
142
|
+
deadline.throwIfAborted();
|
|
143
|
+
for (let hop = 0; hop <= 4 && publicSourceUrl(url); hop++) {
|
|
144
|
+
const grounding = url.origin === 'https://vertexaisearch.cloud.google.com' && url.pathname.startsWith('/grounding-api-redirect/');
|
|
145
|
+
if (!grounding && !inScope(url, settings.sourceScopes)) { issue = 'outsideScope'; break; }
|
|
146
|
+
if (seen.has(url.href)) { issue = 'duplicates'; break; }
|
|
147
|
+
const page = await read(url, deadline);
|
|
148
|
+
if (page.redirect) { url = new URL(page.redirect, url); url.hash = ''; continue; }
|
|
149
|
+
if (grounding) break;
|
|
150
|
+
const evidence = pageEvidence(page.body, source, settings.query, url);
|
|
151
|
+
if (!evidence) { issue = 'unsupported'; break; }
|
|
152
|
+
seen.add(url.href);
|
|
153
|
+
const key = JSON.stringify([url.hostname, evidence.title, norm(evidence.excerpt)]);
|
|
154
|
+
if (sameEvidence.has(key)) { issue = 'duplicates'; break; }
|
|
155
|
+
sameEvidence.add(key);
|
|
156
|
+
kept.push({ ...source, id: 'S' + (kept.length + 1), url: url.href, ...evidence, publishedAt: publicationDate(page.body, source.publishedAt) });
|
|
157
|
+
issue = null;
|
|
158
|
+
break;
|
|
159
|
+
}
|
|
160
|
+
} catch { signal.throwIfAborted(); }
|
|
161
|
+
if (issue) verification[issue]++;
|
|
162
|
+
}
|
|
163
|
+
for (const source of sources) await check(source);
|
|
164
|
+
if (!kept.length) {
|
|
165
|
+
// A user-supplied document can supply a passage even if search returned a mirror.
|
|
166
|
+
// Hosts and directory prefixes constrain search and are never crawled.
|
|
167
|
+
for (const url of settings.sourceScopes || []) {
|
|
168
|
+
if (new URL(url).pathname.endsWith('/') || sources.some(s => s.url === url) || budget.aborted) continue;
|
|
169
|
+
await check({ url, excerpt: settings.query, gatheredAt: sources[0]?.gatheredAt });
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return { sources: kept, verification };
|
|
173
|
+
}
|
package/src/store.js
CHANGED
|
@@ -6,7 +6,7 @@ import { positions, ledgerEntries, synthesisOf } from './engine.js';
|
|
|
6
6
|
import { ADAPTERS } from './adapters/config.js';
|
|
7
7
|
import { optionId, humanQuestion } from './contract.js';
|
|
8
8
|
import { noteRecord, storedNote } from './notes.js';
|
|
9
|
-
import { researchSources } from './research.js';
|
|
9
|
+
import { researchSources, checkedCounts } from './research.js';
|
|
10
10
|
|
|
11
11
|
// Usage rows written before usageVersion 3 carry the vendor's own input figure. Bring them to
|
|
12
12
|
// the one meaning (input = uncached) by the vendor's cache semantics. Idempotent: a row already
|
|
@@ -188,7 +188,7 @@ export function rebuildDebate(rows) {
|
|
|
188
188
|
try {
|
|
189
189
|
const gatheredAt = new Date(research.gatheredAt).toISOString();
|
|
190
190
|
if (!Number.isInteger(research.searches) || research.searches < 1 || research.searches > 8) throw new Error('invalid count');
|
|
191
|
-
research = { status: 'done', gatheredAt, searches: research.searches, attemptId: research.attemptId,
|
|
191
|
+
research = { status: 'done', gatheredAt, searches: research.searches, attemptId: research.attemptId, ...checkedCounts(research.verification),
|
|
192
192
|
sources: researchSources(JSON.stringify({ sources: research.sources }), gatheredAt) };
|
|
193
193
|
fieldNotes.research = research.sources;
|
|
194
194
|
} catch { research = { status: 'failed', errorKind: 'format', error: 'The saved research bundle is invalid. It was not refetched.' }; }
|