docattest 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +147 -2
- package/dist/agents/claude.json +30 -0
- package/dist/agents/cursor.json +19 -0
- package/dist/agents/gemini.json +27 -0
- package/dist/agents/index.json +3 -0
- package/dist/api/adapter.d.ts +231 -0
- package/dist/api/adapter.js +2 -0
- package/dist/api/attestation.d.ts +76 -0
- package/dist/api/attestation.js +2 -0
- package/dist/api/audit.d.ts +56 -0
- package/dist/api/audit.js +2 -0
- package/dist/api/config.d.ts +44 -0
- package/dist/api/config.js +2 -0
- package/dist/api/constants.d.ts +35 -0
- package/dist/api/constants.js +56 -0
- package/dist/api/errors.d.ts +15 -0
- package/dist/api/errors.js +22 -0
- package/dist/api/impact.d.ts +49 -0
- package/dist/api/impact.js +2 -0
- package/dist/api/project.d.ts +64 -0
- package/dist/api/project.js +2 -0
- package/dist/api/report.d.ts +107 -0
- package/dist/api/report.js +2 -0
- package/dist/client/answers.d.ts +17 -0
- package/dist/client/answers.js +23 -0
- package/dist/client/core.d.ts +18 -0
- package/dist/client/core.js +58 -0
- package/dist/client/place.d.ts +14 -0
- package/dist/client/place.js +37 -0
- package/dist/client/project.d.ts +3 -0
- package/dist/client/project.js +98 -0
- package/dist/client/workspace.d.ts +12 -0
- package/dist/client/workspace.js +72 -0
- package/dist/compare-strings.d.ts +2 -0
- package/dist/compare-strings.js +5 -0
- package/dist/core-binary/locate.js +113 -0
- package/dist/core-binary/package.js +70 -0
- package/dist/core.json +4 -0
- package/dist/files/disk.d.ts +4 -0
- package/dist/files/disk.js +67 -0
- package/dist/files/glob.d.ts +10 -0
- package/dist/files/glob.js +21 -0
- package/dist/files/ignored.d.ts +14 -0
- package/dist/files/ignored.js +14 -0
- package/dist/files/memory.d.ts +5 -0
- package/dist/files/memory.js +26 -0
- package/dist/files/source.d.ts +22 -0
- package/dist/files/source.js +17 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +6 -0
- package/dist/json.d.ts +16 -0
- package/dist/json.js +43 -0
- package/dist/language/cache.js +18 -0
- package/dist/language/citations/listing.js +64 -0
- package/dist/language/citations/match.js +14 -0
- package/dist/language/citations/tests-in-file.js +101 -0
- package/dist/language/compiler.js +73 -0
- package/dist/language/description.js +24 -0
- package/dist/language/impact/affected.js +50 -0
- package/dist/language/impact/fingerprint.js +123 -0
- package/dist/language/impact/graph.js +72 -0
- package/dist/language/impact/lookup.js +100 -0
- package/dist/language/impact/program.js +87 -0
- package/dist/language/impact/renames.js +57 -0
- package/dist/language/impact/revision.js +62 -0
- package/dist/language/impact/units.js +80 -0
- package/dist/language/index.js +10 -0
- package/dist/language/inventory/observables.js +56 -0
- package/dist/language/inventory/plan.js +72 -0
- package/dist/language/inventory/probe.js +150 -0
- package/dist/language/languages.js +43 -0
- package/dist/language/node/dependency.js +34 -0
- package/dist/language/node/exports-map.js +92 -0
- package/dist/language/program/exports.js +29 -0
- package/dist/language/program/file-names.js +13 -0
- package/dist/language/program/globals.js +38 -0
- package/dist/language/program/host.js +91 -0
- package/dist/language/program/options.js +48 -0
- package/dist/language/program/program.js +41 -0
- package/dist/language/run/attempt.js +100 -0
- package/dist/language/run/facts.js +109 -0
- package/dist/language/run/host.js +73 -0
- package/dist/language/run/jobs.js +37 -0
- package/dist/language/run/pool.js +75 -0
- package/dist/language/run/protocol.js +37 -0
- package/dist/language/run/result.js +18 -0
- package/dist/language/run/runner.js +115 -0
- package/dist/language/run/sourcemap.js +43 -0
- package/dist/language/run/spawn.js +40 -0
- package/dist/language/run/transpile.js +50 -0
- package/dist/language/session.js +42 -0
- package/dist/language/surfaces/packages.js +71 -0
- package/dist/language/surfaces/resolve.js +48 -0
- package/dist/language/surfaces/specs.js +32 -0
- package/dist/language/surfaces/stale.js +23 -0
- package/dist/language/symbols.js +95 -0
- package/dist/language/syntax/doc-comments.js +90 -0
- package/dist/language/syntax/doc-prose.js +23 -0
- package/dist/language/syntax/expression.js +52 -0
- package/dist/language/syntax/imports.js +83 -0
- package/dist/language/syntax/lexical.js +41 -0
- package/dist/language/syntax/parse.js +11 -0
- package/dist/language/syntax/prose.js +11 -0
- package/dist/language/syntax/setup-module.js +88 -0
- package/dist/language/ts.cjs +9 -0
- package/dist/language/worker/cases.js +73 -0
- package/dist/language/worker/compile.js +15 -0
- package/dist/language/worker/console.js +10 -0
- package/dist/language/worker/context.js +16 -0
- package/dist/language/worker/describe.js +107 -0
- package/dist/language/worker/example.js +27 -0
- package/dist/language/worker/facts.js +69 -0
- package/dist/language/worker/host-client.js +55 -0
- package/dist/language/worker/main.js +71 -0
- package/dist/language/worker/network.js +35 -0
- package/dist/language/worker/observe.js +39 -0
- package/dist/language/worker/registry.js +78 -0
- package/dist/language/worker/setup-facts.js +50 -0
- package/dist/language/worker/strays.js +37 -0
- package/dist/launcher/main.js +38 -0
- package/dist/platforms.json +40 -0
- package/dist/protocol/context.js +48 -0
- package/dist/protocol/main.js +22 -0
- package/dist/protocol/methods.js +178 -0
- package/dist/protocol/output.js +40 -0
- package/dist/protocol/overlay.js +31 -0
- package/dist/protocol/serve.js +24 -0
- package/dist/protocol/server.js +108 -0
- package/dist/protocol/wire.js +117 -0
- package/dist/schema/agent.json +162 -0
- package/dist/schema/attestations.json +307 -0
- package/dist/schema/kit.json +190 -0
- package/dist/schema/language.json +162 -0
- package/dist/schema/messages.json +68 -0
- package/dist/schema/methods.json +412 -0
- package/dist/schema/project.json +113 -0
- package/dist/schema/results.json +249 -0
- package/package.json +73 -4
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { QUIET_CONSOLE } from './console.js';
|
|
2
|
+
import { ModuleRegistry } from './registry.js';
|
|
3
|
+
/** What every fact expression may call besides the surface, each by its name here. */
|
|
4
|
+
export const FACT_FUNCTIONS = ['snapshot', 'manifest'];
|
|
5
|
+
/** A fresh registry whose modules see a silent console and the given free variables. */
|
|
6
|
+
export function freshRegistry(ctx, globals = {}) {
|
|
7
|
+
return new ModuleRegistry(ctx.host, { console: QUIET_CONSOLE, ...globals });
|
|
8
|
+
}
|
|
9
|
+
/** Own enumerable properties of a module's exports, whatever shape they have. */
|
|
10
|
+
export function exportedEntries(exports) {
|
|
11
|
+
if ((typeof exports === 'object' && exports !== null) || typeof exports === 'function') {
|
|
12
|
+
return Object.entries(exports);
|
|
13
|
+
}
|
|
14
|
+
return [];
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=context.js.map
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import * as util from 'node:util';
|
|
2
|
+
import { isObject } from '../../json.js';
|
|
3
|
+
const OPERATOR_NAMES = {
|
|
4
|
+
'==': 'ok',
|
|
5
|
+
strictEqual: 'equal',
|
|
6
|
+
notStrictEqual: 'notEqual',
|
|
7
|
+
deepStrictEqual: 'deepEqual',
|
|
8
|
+
notDeepStrictEqual: 'notDeepEqual',
|
|
9
|
+
};
|
|
10
|
+
const COMPARISONS = new Set([
|
|
11
|
+
'strictEqual',
|
|
12
|
+
'deepStrictEqual',
|
|
13
|
+
'equal',
|
|
14
|
+
'deepEqual',
|
|
15
|
+
'===',
|
|
16
|
+
'partialDeepStrictEqual',
|
|
17
|
+
'match',
|
|
18
|
+
]);
|
|
19
|
+
const NEGATIONS = new Set([
|
|
20
|
+
'notStrictEqual',
|
|
21
|
+
'notDeepStrictEqual',
|
|
22
|
+
'notEqual',
|
|
23
|
+
'notDeepEqual',
|
|
24
|
+
'!=',
|
|
25
|
+
'!==',
|
|
26
|
+
'doesNotMatch',
|
|
27
|
+
]);
|
|
28
|
+
/** A value for a message: compact, one line, bounded. */
|
|
29
|
+
export function show(value) {
|
|
30
|
+
if (value instanceof Error)
|
|
31
|
+
return `[${value.name}: ${firstLine(value.message)}]`;
|
|
32
|
+
const options = {
|
|
33
|
+
depth: 4,
|
|
34
|
+
breakLength: Infinity,
|
|
35
|
+
compact: true,
|
|
36
|
+
maxArrayLength: 50,
|
|
37
|
+
maxStringLength: 300,
|
|
38
|
+
};
|
|
39
|
+
const text = util.inspect(value, options);
|
|
40
|
+
return text.length > 400 ? `${text.slice(0, 397)}...` : text;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Describe a thrown value. `line` is the block line it passed through, if known; `source`
|
|
44
|
+
* is the block as written, used to name the assert call that failed.
|
|
45
|
+
*/
|
|
46
|
+
export function describeError(err, line, source) {
|
|
47
|
+
const at = (call) => {
|
|
48
|
+
if (line)
|
|
49
|
+
return ` (${call ? `${call}, ` : ''}block line ${line})`;
|
|
50
|
+
return call ? ` (${call})` : '';
|
|
51
|
+
};
|
|
52
|
+
if (isAssertion(err))
|
|
53
|
+
return describeAssertion(err, assertCall(err, line, source), at);
|
|
54
|
+
if (err instanceof Error)
|
|
55
|
+
return { message: `${err.name}: ${firstLine(err.message)}${at()}` };
|
|
56
|
+
return { message: `threw ${show(err)}${at()}` };
|
|
57
|
+
}
|
|
58
|
+
function describeAssertion(e, call, at) {
|
|
59
|
+
const sides = { expected: show(e.expected), actual: show(e.actual) };
|
|
60
|
+
const versus = `expected ${sides.expected}, got ${sides.actual}`;
|
|
61
|
+
const first = firstLine(e.message);
|
|
62
|
+
let text;
|
|
63
|
+
if (!e.generatedMessage)
|
|
64
|
+
text = e.message;
|
|
65
|
+
else if (COMPARISONS.has(e.operator))
|
|
66
|
+
text = versus;
|
|
67
|
+
else if (NEGATIONS.has(e.operator)) {
|
|
68
|
+
text = `expected anything but ${sides.expected}, got ${sides.actual}`;
|
|
69
|
+
}
|
|
70
|
+
else if (first.endsWith(':'))
|
|
71
|
+
text = `${first} ${versus}`;
|
|
72
|
+
else
|
|
73
|
+
return { message: `AssertionError: ${first}${at(call)}` };
|
|
74
|
+
return { message: `AssertionError: ${text}${at(call)}`, ...sides };
|
|
75
|
+
}
|
|
76
|
+
// Name the call as written on the failing line (`assert.match`), else from the operator.
|
|
77
|
+
function assertCall(e, line, source) {
|
|
78
|
+
if (line && source) {
|
|
79
|
+
const written = /\bassert(?:\s*\.\s*(\w+))?\s*\(/.exec(source.split('\n')[line - 1] ?? '');
|
|
80
|
+
return written?.[0].replace(/\s+|\($/g, '');
|
|
81
|
+
}
|
|
82
|
+
return `assert.${OPERATOR_NAMES[e.operator] ?? e.operator}`;
|
|
83
|
+
}
|
|
84
|
+
function isAssertion(err) {
|
|
85
|
+
return (isObject(err) &&
|
|
86
|
+
err.code === 'ERR_ASSERTION' &&
|
|
87
|
+
typeof err.message === 'string' &&
|
|
88
|
+
typeof err.operator === 'string');
|
|
89
|
+
}
|
|
90
|
+
function firstLine(text) {
|
|
91
|
+
return text.split('\n')[0] ?? '';
|
|
92
|
+
}
|
|
93
|
+
/** The source line (1-based) within `file` that an error's stack passed through, if any. */
|
|
94
|
+
export function lineIn(err, file, lines) {
|
|
95
|
+
const stack = err instanceof Error ? (err.stack ?? '') : '';
|
|
96
|
+
const pattern = new RegExp(`${file.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}:(\\d+):\\d+`);
|
|
97
|
+
const match = pattern.exec(stack);
|
|
98
|
+
if (!match)
|
|
99
|
+
return undefined;
|
|
100
|
+
for (let generated = Number(match[1]) - 1; generated >= 0; generated--) {
|
|
101
|
+
const original = lines[generated];
|
|
102
|
+
if (original !== undefined)
|
|
103
|
+
return original + 1;
|
|
104
|
+
}
|
|
105
|
+
return undefined;
|
|
106
|
+
}
|
|
107
|
+
//# sourceMappingURL=describe.js.map
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import assert from 'node:assert/strict';
|
|
2
|
+
import { freshRegistry } from './context.js';
|
|
3
|
+
import { describeError, lineIn } from './describe.js';
|
|
4
|
+
import { offline } from './network.js';
|
|
5
|
+
/** Run one example block in a fresh registry, with `assert` in scope. */
|
|
6
|
+
export async function runExample(job, ctx) {
|
|
7
|
+
const registry = freshRegistry(ctx, { assert });
|
|
8
|
+
const run = () => registry.runBlock(job.file, job.code);
|
|
9
|
+
try {
|
|
10
|
+
const { strays } = await ctx.strays.during(() => (job.offline ? offline(run) : run()));
|
|
11
|
+
if (strays.length) {
|
|
12
|
+
return {
|
|
13
|
+
status: 'fail',
|
|
14
|
+
message: `unhandled error from attested code: ${strays.join('; ')}`,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
const message = job.weak
|
|
18
|
+
? 'ran without throwing, but asserts nothing (weak)'
|
|
19
|
+
: 'all assertions held';
|
|
20
|
+
return { status: 'pass', message };
|
|
21
|
+
}
|
|
22
|
+
catch (err) {
|
|
23
|
+
const failure = describeError(err, lineIn(err, job.file, job.lines), job.source);
|
|
24
|
+
return { status: 'fail', ...failure };
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=example.js.map
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { observe } from './observe.js';
|
|
2
|
+
import { InternalError } from '../../api/errors.js';
|
|
3
|
+
import { compileFunction } from './compile.js';
|
|
4
|
+
import { exportedEntries, FACT_FUNCTIONS, freshRegistry } from './context.js';
|
|
5
|
+
import { describeError } from './describe.js';
|
|
6
|
+
import { offline } from './network.js';
|
|
7
|
+
import { SetupFacts } from './setup-facts.js';
|
|
8
|
+
const IDENTIFIER = /^[A-Za-z_$][\w$]*$/;
|
|
9
|
+
const RESERVED = new Set([
|
|
10
|
+
'break case catch class const continue debugger default delete do else enum export extends',
|
|
11
|
+
'false finally for function if import in instanceof new null return super switch this throw',
|
|
12
|
+
'true try typeof var void while with yield let static implements interface package private',
|
|
13
|
+
'protected public await arguments eval',
|
|
14
|
+
...FACT_FUNCTIONS,
|
|
15
|
+
]
|
|
16
|
+
.join(' ')
|
|
17
|
+
.split(' '));
|
|
18
|
+
/** Load the surface once, then evaluate each fact from `start` against it. */
|
|
19
|
+
export async function runFacts(id, job, start, ctx) {
|
|
20
|
+
const registry = freshRegistry(ctx, { snapshot: ctx.snapshot });
|
|
21
|
+
let surface;
|
|
22
|
+
try {
|
|
23
|
+
surface = new Map(exportedEntries(await offline(() => registry.load(job.file))));
|
|
24
|
+
}
|
|
25
|
+
catch (err) {
|
|
26
|
+
const message = `surface "${job.surface}" failed to load: ${describeError(err).message}`;
|
|
27
|
+
ctx.post({ type: 'failed', id, message });
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
ctx.post({ type: 'ready', id });
|
|
31
|
+
const helpers = new SetupFacts(registry, job.setup, surface);
|
|
32
|
+
for (const fact of job.facts.slice(start)) {
|
|
33
|
+
const evaluation = await evaluate(fact, job.surface, helpers, ctx);
|
|
34
|
+
ctx.post({ type: 'item', id, outcome: { kind: 'evaluation', evaluation } });
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
async function evaluate(fact, surface, helpers, ctx) {
|
|
38
|
+
const label = `fact \`${fact.expression}\``;
|
|
39
|
+
const bindings = await helpers.bindingsFor(fact, surface);
|
|
40
|
+
if (typeof bindings === 'string')
|
|
41
|
+
return { ok: false, message: `${label}: ${bindings}` };
|
|
42
|
+
const names = [...bindings.keys()].filter((n) => IDENTIFIER.test(n) && !RESERVED.has(n));
|
|
43
|
+
try {
|
|
44
|
+
const evaluateFact = factFunction(names, fact.code)(...names.map((n) => bindings.get(n)), ...FACT_FUNCTIONS.map((n) => ctx[n]));
|
|
45
|
+
if (typeof evaluateFact !== 'function') {
|
|
46
|
+
throw new InternalError('the fact module exported no function');
|
|
47
|
+
}
|
|
48
|
+
const value = await offline(() => Reflect.apply(evaluateFact, undefined, []));
|
|
49
|
+
return { ok: true, value: observe(value) };
|
|
50
|
+
}
|
|
51
|
+
catch (err) {
|
|
52
|
+
return { ok: false, message: evaluationError(err, label, surface) };
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
// A free identifier the surface does not export is the commonest mistake: say so.
|
|
56
|
+
function evaluationError(err, label, surface) {
|
|
57
|
+
const missing = err instanceof ReferenceError ? /^(\S+) is not defined$/.exec(err.message) : null;
|
|
58
|
+
if (!missing || !(err instanceof Error))
|
|
59
|
+
return `${label} threw ${describeError(err).message}`;
|
|
60
|
+
const name = missing[1] ?? '';
|
|
61
|
+
const where = `surface "${surface}"`;
|
|
62
|
+
return `${label}: ${name} is not exported by ${where} (ReferenceError: ${err.message})`;
|
|
63
|
+
}
|
|
64
|
+
// The fact module's code is ours: a transpiled `export default async () => (expression)`.
|
|
65
|
+
function factFunction(names, code) {
|
|
66
|
+
const body = `"use strict"; const exports = {}; ${code}; return exports.default;`;
|
|
67
|
+
return compileFunction(`(function (${[...names, ...FACT_FUNCTIONS].join(', ')}) {${body}\n})`, 'fact.js');
|
|
68
|
+
}
|
|
69
|
+
//# sourceMappingURL=facts.js.map
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { receiveMessageOnPort } from 'node:worker_threads';
|
|
2
|
+
import { InternalError } from '../../api/errors.js';
|
|
3
|
+
import { isHostReply, } from '../run/protocol.js';
|
|
4
|
+
/**
|
|
5
|
+
* Asks the session about modules, synchronously: `require` is synchronous, and the session
|
|
6
|
+
* holds the files and the compiler. Answers never change during a check, so they are cached.
|
|
7
|
+
*/
|
|
8
|
+
export class HostClient {
|
|
9
|
+
port;
|
|
10
|
+
signal;
|
|
11
|
+
resolutions = new Map();
|
|
12
|
+
sources = new Map();
|
|
13
|
+
constructor(port, signal) {
|
|
14
|
+
this.port = port;
|
|
15
|
+
this.signal = signal;
|
|
16
|
+
}
|
|
17
|
+
/** How a specifier imported from `from` resolves. */
|
|
18
|
+
resolve(specifier, from) {
|
|
19
|
+
const key = `${from}\0${specifier}`;
|
|
20
|
+
let resolution = this.resolutions.get(key);
|
|
21
|
+
if (!resolution) {
|
|
22
|
+
const reply = this.request({ op: 'resolve', specifier, from });
|
|
23
|
+
if (reply.op !== 'resolve') {
|
|
24
|
+
throw new InternalError('the session answered a resolve with something else');
|
|
25
|
+
}
|
|
26
|
+
resolution = reply.resolution;
|
|
27
|
+
this.resolutions.set(key, resolution);
|
|
28
|
+
}
|
|
29
|
+
return resolution;
|
|
30
|
+
}
|
|
31
|
+
/** A project module's code, or undefined when the file does not exist. */
|
|
32
|
+
source(file) {
|
|
33
|
+
if (!this.sources.has(file)) {
|
|
34
|
+
const reply = this.request({ op: 'source', file });
|
|
35
|
+
if (reply.op !== 'source') {
|
|
36
|
+
throw new InternalError('the session answered a source request with something else');
|
|
37
|
+
}
|
|
38
|
+
this.sources.set(file, reply.source);
|
|
39
|
+
}
|
|
40
|
+
return this.sources.get(file);
|
|
41
|
+
}
|
|
42
|
+
// Post, then block until the session flips the signal; its reply is then on the port.
|
|
43
|
+
request(request) {
|
|
44
|
+
Atomics.store(this.signal, 0, 0);
|
|
45
|
+
this.port.postMessage(request);
|
|
46
|
+
Atomics.wait(this.signal, 0, 0);
|
|
47
|
+
const reply = receiveMessageOnPort(this.port)?.message;
|
|
48
|
+
if (!isHostReply(reply))
|
|
49
|
+
throw new InternalError('the session did not answer a module request');
|
|
50
|
+
if (reply.op === 'error')
|
|
51
|
+
throw new Error(reply.message);
|
|
52
|
+
return reply;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
//# sourceMappingURL=host-client.js.map
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// Worker entry: runs jobs the session sends, one at a time, and reports progress. It never
|
|
2
|
+
// loads TypeScript; modules arrive as CommonJS from the session.
|
|
3
|
+
import { parentPort, workerData } from 'node:worker_threads';
|
|
4
|
+
import { InternalError } from '../../api/errors.js';
|
|
5
|
+
import { isJobRequest, isWorkerData, } from '../run/protocol.js';
|
|
6
|
+
import { runCases } from './cases.js';
|
|
7
|
+
import { runExample } from './example.js';
|
|
8
|
+
import { runFacts } from './facts.js';
|
|
9
|
+
import { HostClient } from './host-client.js';
|
|
10
|
+
import { Strays } from './strays.js';
|
|
11
|
+
if (!parentPort || !isWorkerData(workerData)) {
|
|
12
|
+
throw new InternalError('worker started without its data');
|
|
13
|
+
}
|
|
14
|
+
const port = parentPort;
|
|
15
|
+
const data = workerData;
|
|
16
|
+
// The job running now: a stray error is reported against it. Empty before the first job.
|
|
17
|
+
let current = '';
|
|
18
|
+
const post = (message) => {
|
|
19
|
+
port.postMessage(message);
|
|
20
|
+
};
|
|
21
|
+
const strays = new Strays((message) => {
|
|
22
|
+
post({ type: 'stray', id: current, message });
|
|
23
|
+
});
|
|
24
|
+
const ctx = {
|
|
25
|
+
host: new HostClient(data.port, data.signal),
|
|
26
|
+
strays,
|
|
27
|
+
snapshot,
|
|
28
|
+
manifest,
|
|
29
|
+
post,
|
|
30
|
+
};
|
|
31
|
+
// Escaped errors are attested code's, never the worker's: record them and keep going.
|
|
32
|
+
process.on('unhandledRejection', (reason) => {
|
|
33
|
+
strays.add(reason);
|
|
34
|
+
});
|
|
35
|
+
process.on('uncaughtException', (err) => {
|
|
36
|
+
strays.add(err);
|
|
37
|
+
});
|
|
38
|
+
port.on('message', (raw) => {
|
|
39
|
+
if (isJobRequest(raw))
|
|
40
|
+
void run(raw);
|
|
41
|
+
});
|
|
42
|
+
port.postMessage({ type: 'listening' });
|
|
43
|
+
async function run({ id, job, start }) {
|
|
44
|
+
current = id;
|
|
45
|
+
if (job.type === 'example') {
|
|
46
|
+
post({ type: 'ready', id });
|
|
47
|
+
const verdict = await runExample(job, ctx);
|
|
48
|
+
post({ type: 'item', id, outcome: { kind: 'verdict', verdict } });
|
|
49
|
+
}
|
|
50
|
+
else if (job.type === 'facts')
|
|
51
|
+
await runFacts(id, job, start, ctx);
|
|
52
|
+
else
|
|
53
|
+
await runCases(id, job, start, ctx);
|
|
54
|
+
post({ type: 'done', id });
|
|
55
|
+
}
|
|
56
|
+
function snapshot(name) {
|
|
57
|
+
if (!Object.hasOwn(data.snapshots.data, name)) {
|
|
58
|
+
const dir = data.snapshots.dir ?? '(no snapshots dir configured)';
|
|
59
|
+
throw new Error(`snapshot "${name}" not found in ${dir}`);
|
|
60
|
+
}
|
|
61
|
+
return structuredClone(data.snapshots.data[name]);
|
|
62
|
+
}
|
|
63
|
+
// The core reads a manifest only when a fact writes its path out; one built at run time
|
|
64
|
+
// was never read.
|
|
65
|
+
function manifest(path) {
|
|
66
|
+
if (!Object.hasOwn(data.manifests, path)) {
|
|
67
|
+
throw new Error(`manifest "${path}" was not read: name it in the fact itself, as manifest("${path}")`);
|
|
68
|
+
}
|
|
69
|
+
return structuredClone(data.manifests[path]);
|
|
70
|
+
}
|
|
71
|
+
//# sourceMappingURL=main.js.map
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import * as net from 'node:net';
|
|
2
|
+
const NETWORK_OFF = 'network access is disabled for attestations; tag the block `check network` to allow it';
|
|
3
|
+
const refuse = () => {
|
|
4
|
+
throw new Error(NETWORK_OFF);
|
|
5
|
+
};
|
|
6
|
+
// What the guard replaced, restored when the outermost guarded call ends.
|
|
7
|
+
const saved = {
|
|
8
|
+
depth: 0,
|
|
9
|
+
fetch: globalThis.fetch,
|
|
10
|
+
connect: undefined,
|
|
11
|
+
};
|
|
12
|
+
/** Run with `fetch` and socket connections refused. Nested calls share one guard. */
|
|
13
|
+
export async function offline(run) {
|
|
14
|
+
if (saved.depth++ === 0) {
|
|
15
|
+
saved.fetch = globalThis.fetch;
|
|
16
|
+
saved.connect = Object.getOwnPropertyDescriptor(net.Socket.prototype, 'connect');
|
|
17
|
+
globalThis.fetch = () => Promise.reject(new Error(NETWORK_OFF));
|
|
18
|
+
Object.defineProperty(net.Socket.prototype, 'connect', {
|
|
19
|
+
value: refuse,
|
|
20
|
+
configurable: true,
|
|
21
|
+
writable: true,
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
try {
|
|
25
|
+
return await run();
|
|
26
|
+
}
|
|
27
|
+
finally {
|
|
28
|
+
if (--saved.depth === 0) {
|
|
29
|
+
globalThis.fetch = saved.fetch;
|
|
30
|
+
if (saved.connect)
|
|
31
|
+
Object.defineProperty(net.Socket.prototype, 'connect', saved.connect);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=network.js.map
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/** Observe a runtime value: everything a fact comparison needs, as plain data. */
|
|
2
|
+
export function observe(value) {
|
|
3
|
+
const items = toItems(value);
|
|
4
|
+
const observation = { text: safeString(value), description: describe(value) };
|
|
5
|
+
if (typeof value === 'string')
|
|
6
|
+
observation.kind = 'text';
|
|
7
|
+
return items ? { ...observation, items } : observation;
|
|
8
|
+
}
|
|
9
|
+
// Strings are iterable but are scalars on a page; anything else iterable is a list.
|
|
10
|
+
function toItems(value) {
|
|
11
|
+
if (typeof value === 'string' || value === null || value === undefined)
|
|
12
|
+
return undefined;
|
|
13
|
+
if (!isIterable(value))
|
|
14
|
+
return undefined;
|
|
15
|
+
return Array.from(value, safeString);
|
|
16
|
+
}
|
|
17
|
+
function isIterable(value) {
|
|
18
|
+
if ((typeof value !== 'object' && typeof value !== 'function') || value === null)
|
|
19
|
+
return false;
|
|
20
|
+
return Symbol.iterator in value && typeof value[Symbol.iterator] === 'function';
|
|
21
|
+
}
|
|
22
|
+
function safeString(value) {
|
|
23
|
+
try {
|
|
24
|
+
return String(value);
|
|
25
|
+
}
|
|
26
|
+
catch {
|
|
27
|
+
return Object.prototype.toString.call(value);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
function describe(value) {
|
|
31
|
+
if (value === null)
|
|
32
|
+
return 'null';
|
|
33
|
+
if (Array.isArray(value))
|
|
34
|
+
return 'an array';
|
|
35
|
+
if (typeof value === 'object')
|
|
36
|
+
return 'an object';
|
|
37
|
+
return `${typeof value} ${safeString(value)}`;
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=observe.js.map
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import * as path from 'node:path';
|
|
2
|
+
import { pathToFileURL } from 'node:url';
|
|
3
|
+
import { requireDependency } from '../node/dependency.js';
|
|
4
|
+
import { IMPORT_META } from '../run/protocol.js';
|
|
5
|
+
import { compileFunction } from './compile.js';
|
|
6
|
+
/**
|
|
7
|
+
* A fresh module registry: one per block, per cases table, per page of facts. Project
|
|
8
|
+
* modules run once per registry, so every block imports the surface fresh and no state
|
|
9
|
+
* leaks between blocks. Dependencies load natively and are shared, as in any Node process.
|
|
10
|
+
*/
|
|
11
|
+
export class ModuleRegistry {
|
|
12
|
+
host;
|
|
13
|
+
globals;
|
|
14
|
+
modules = new Map();
|
|
15
|
+
constructor(host,
|
|
16
|
+
/** Free variables visible to every module this registry runs. */
|
|
17
|
+
globals) {
|
|
18
|
+
this.host = host;
|
|
19
|
+
this.globals = globals;
|
|
20
|
+
}
|
|
21
|
+
/** `require` as seen from `from`. */
|
|
22
|
+
require(specifier, from) {
|
|
23
|
+
const resolution = this.host.resolve(specifier, from);
|
|
24
|
+
if (resolution.kind === 'project')
|
|
25
|
+
return this.load(resolution.file);
|
|
26
|
+
if (resolution.kind === 'external')
|
|
27
|
+
return requireDependency(specifier, resolution.base);
|
|
28
|
+
throw new Error(resolution.message);
|
|
29
|
+
}
|
|
30
|
+
/** Load a project module by absolute path. */
|
|
31
|
+
load(file) {
|
|
32
|
+
const cached = this.modules.get(file);
|
|
33
|
+
if (cached)
|
|
34
|
+
return cached.exports;
|
|
35
|
+
const source = this.host.source(file);
|
|
36
|
+
if (!source)
|
|
37
|
+
throw new Error(`Cannot find module ${file}`);
|
|
38
|
+
const record = { exports: {} };
|
|
39
|
+
this.modules.set(file, record);
|
|
40
|
+
if (source.format === 'json')
|
|
41
|
+
record.exports = JSON.parse(source.text);
|
|
42
|
+
else
|
|
43
|
+
this.execute(file, source.code, record, false);
|
|
44
|
+
return record.exports;
|
|
45
|
+
}
|
|
46
|
+
/** Run a block's code as an async module body; settles when the block finishes. */
|
|
47
|
+
async runBlock(file, code) {
|
|
48
|
+
await this.execute(file, code, { exports: {} }, true);
|
|
49
|
+
}
|
|
50
|
+
execute(file, code, record, block) {
|
|
51
|
+
const names = [
|
|
52
|
+
'exports',
|
|
53
|
+
'require',
|
|
54
|
+
'module',
|
|
55
|
+
'__filename',
|
|
56
|
+
'__dirname',
|
|
57
|
+
IMPORT_META,
|
|
58
|
+
...Object.keys(this.globals),
|
|
59
|
+
];
|
|
60
|
+
const wrapper = `(${block ? 'async ' : ''}function (${names.join(', ')}) {${code}\n})`;
|
|
61
|
+
const run = compileFunction(wrapper, file);
|
|
62
|
+
const dirname = path.dirname(file);
|
|
63
|
+
const meta = { url: pathToFileURL(file).href, filename: file, dirname };
|
|
64
|
+
const require = (specifier) => this.require(specifier, file);
|
|
65
|
+
const args = [
|
|
66
|
+
record.exports,
|
|
67
|
+
require,
|
|
68
|
+
record,
|
|
69
|
+
file,
|
|
70
|
+
dirname,
|
|
71
|
+
meta,
|
|
72
|
+
...Object.values(this.globals),
|
|
73
|
+
];
|
|
74
|
+
// CommonJS runs with `this` as the module's exports; a block is a module body.
|
|
75
|
+
return run.apply(block ? undefined : record.exports, args);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
//# sourceMappingURL=registry.js.map
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { ambiguous } from '../run/protocol.js';
|
|
2
|
+
import { exportedEntries, FACT_FUNCTIONS } from './context.js';
|
|
3
|
+
import { describeError } from './describe.js';
|
|
4
|
+
import { offline } from './network.js';
|
|
5
|
+
const FACT_NAMES = new Set(FACT_FUNCTIONS);
|
|
6
|
+
/**
|
|
7
|
+
* Named facts from the setup module, loaded only when an expression needs a name the
|
|
8
|
+
* surface does not export: loading runs the module's top level, and facts must stay pure.
|
|
9
|
+
*/
|
|
10
|
+
export class SetupFacts {
|
|
11
|
+
registry;
|
|
12
|
+
setup;
|
|
13
|
+
surface;
|
|
14
|
+
loaded;
|
|
15
|
+
constructor(registry, setup, surface) {
|
|
16
|
+
this.registry = registry;
|
|
17
|
+
this.setup = setup;
|
|
18
|
+
this.surface = surface;
|
|
19
|
+
}
|
|
20
|
+
/** The names an expression may use, or why it cannot be evaluated. */
|
|
21
|
+
async bindingsFor(fact, surfaceName) {
|
|
22
|
+
const needsHelpers = fact.identifiers.some((n) => !this.surface.has(n) && !FACT_NAMES.has(n) && !(n in globalThis));
|
|
23
|
+
if (!needsHelpers || !this.setup)
|
|
24
|
+
return new Map(this.surface);
|
|
25
|
+
const helpers = await this.load(this.setup);
|
|
26
|
+
if (typeof helpers === 'string')
|
|
27
|
+
return helpers;
|
|
28
|
+
const clash = fact.identifiers.find((n) => this.surface.has(n) && helpers.has(n));
|
|
29
|
+
if (clash !== undefined)
|
|
30
|
+
return ambiguous(clash, surfaceName);
|
|
31
|
+
return new Map([...helpers, ...this.surface]);
|
|
32
|
+
}
|
|
33
|
+
async load(setup) {
|
|
34
|
+
if (this.loaded !== undefined)
|
|
35
|
+
return this.loaded;
|
|
36
|
+
try {
|
|
37
|
+
const exports = await offline(() => this.registry.load(setup.file));
|
|
38
|
+
const facts = new Map(exportedEntries(exports)).get('facts');
|
|
39
|
+
const valid = facts === undefined || (typeof facts === 'object' && facts !== null);
|
|
40
|
+
this.loaded = valid
|
|
41
|
+
? new Map(exportedEntries(facts))
|
|
42
|
+
: `${setup.label} exports \`facts\`, but it is not an object`;
|
|
43
|
+
}
|
|
44
|
+
catch (err) {
|
|
45
|
+
this.loaded = `setup module ${setup.label} failed to load: ${describeError(err).message}`;
|
|
46
|
+
}
|
|
47
|
+
return this.loaded;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
//# sourceMappingURL=setup-facts.js.map
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { messageOf } from '../../api/errors.js';
|
|
2
|
+
/**
|
|
3
|
+
* Errors attested code leaves behind: unhandled rejections and exceptions from timers.
|
|
4
|
+
* While an item runs they belong to it; otherwise they go to whoever is listening.
|
|
5
|
+
*/
|
|
6
|
+
export class Strays {
|
|
7
|
+
outside;
|
|
8
|
+
current;
|
|
9
|
+
constructor(outside) {
|
|
10
|
+
this.outside = outside;
|
|
11
|
+
}
|
|
12
|
+
/** Record an escaped error. */
|
|
13
|
+
add(reason) {
|
|
14
|
+
const message = messageOf(reason);
|
|
15
|
+
if (this.current)
|
|
16
|
+
this.current.push(message);
|
|
17
|
+
else
|
|
18
|
+
this.outside(message);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Run one item, then give its escaped errors a turn of the event loop to surface: a
|
|
22
|
+
* rejection nobody awaited is reported after the microtasks of the block drain.
|
|
23
|
+
*/
|
|
24
|
+
async during(run) {
|
|
25
|
+
const mine = [];
|
|
26
|
+
this.current = mine;
|
|
27
|
+
try {
|
|
28
|
+
const value = await run();
|
|
29
|
+
await new Promise((resolve) => setImmediate(resolve));
|
|
30
|
+
return { value, strays: mine };
|
|
31
|
+
}
|
|
32
|
+
finally {
|
|
33
|
+
this.current = undefined;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=strays.js.map
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// docattest, as the npm package runs it: a launcher for the docattest core.
|
|
3
|
+
//
|
|
4
|
+
// The core is one binary for every language and knows nothing of how it was installed. This
|
|
5
|
+
// launcher belongs to the language package: it finds the core the package brought, tells it
|
|
6
|
+
// how to start this package's adapter and what command runs docattest in a project that
|
|
7
|
+
// installed it from npm, and then gets out of the way: arguments, standard streams and the
|
|
8
|
+
// exit code are the core's (PROTOCOL.md §1, "Launchers").
|
|
9
|
+
//
|
|
10
|
+
// It imports nothing but Node's own modules, so that it costs a process start and no more.
|
|
11
|
+
import { spawnSync } from 'node:child_process';
|
|
12
|
+
import { constants } from 'node:os';
|
|
13
|
+
import { given } from '../core-binary/locate.js';
|
|
14
|
+
import { coreEnvironment, packageCore } from '../core-binary/package.js';
|
|
15
|
+
const found = packageCore();
|
|
16
|
+
if ('error' in found) {
|
|
17
|
+
process.stderr.write(`${found.error}\n`);
|
|
18
|
+
process.exit(1);
|
|
19
|
+
}
|
|
20
|
+
const { core } = found;
|
|
21
|
+
// With a trace asked for (the core's own `DOCATTEST_TRACE`, set to anything but nothing), say
|
|
22
|
+
// which core runs.
|
|
23
|
+
if (given(process.env.DOCATTEST_TRACE) !== undefined) {
|
|
24
|
+
process.stderr.write(`[trace launcher] core ${core}\n`);
|
|
25
|
+
}
|
|
26
|
+
const done = spawnSync(core, process.argv.slice(2), {
|
|
27
|
+
stdio: 'inherit',
|
|
28
|
+
env: coreEnvironment(process.env),
|
|
29
|
+
});
|
|
30
|
+
if (done.error) {
|
|
31
|
+
process.stderr.write(`docattest: cannot run the core (${core}): ${done.error.message}\n`);
|
|
32
|
+
process.exit(1);
|
|
33
|
+
}
|
|
34
|
+
// A core that died of a signal is reported as a shell reports it.
|
|
35
|
+
if (done.signal)
|
|
36
|
+
process.exit(128 + constants.signals[done.signal]);
|
|
37
|
+
process.exit(done.status ?? 1);
|
|
38
|
+
//# sourceMappingURL=main.js.map
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"npmPackage": "@docattest/core-{platform}",
|
|
3
|
+
"platforms": {
|
|
4
|
+
"darwin-arm64": {
|
|
5
|
+
"os": "darwin",
|
|
6
|
+
"cpu": "arm64",
|
|
7
|
+
"rust": "aarch64-apple-darwin",
|
|
8
|
+
"wheel": ["macosx_11_0_arm64"],
|
|
9
|
+
"binary": "docattest"
|
|
10
|
+
},
|
|
11
|
+
"darwin-x64": {
|
|
12
|
+
"os": "darwin",
|
|
13
|
+
"cpu": "x64",
|
|
14
|
+
"rust": "x86_64-apple-darwin",
|
|
15
|
+
"wheel": ["macosx_10_12_x86_64"],
|
|
16
|
+
"binary": "docattest"
|
|
17
|
+
},
|
|
18
|
+
"linux-arm64": {
|
|
19
|
+
"os": "linux",
|
|
20
|
+
"cpu": "arm64",
|
|
21
|
+
"rust": "aarch64-unknown-linux-musl",
|
|
22
|
+
"wheel": ["manylinux_2_17_aarch64", "manylinux2014_aarch64", "musllinux_1_1_aarch64"],
|
|
23
|
+
"binary": "docattest"
|
|
24
|
+
},
|
|
25
|
+
"linux-x64": {
|
|
26
|
+
"os": "linux",
|
|
27
|
+
"cpu": "x64",
|
|
28
|
+
"rust": "x86_64-unknown-linux-musl",
|
|
29
|
+
"wheel": ["manylinux_2_17_x86_64", "manylinux2014_x86_64", "musllinux_1_1_x86_64"],
|
|
30
|
+
"binary": "docattest"
|
|
31
|
+
},
|
|
32
|
+
"windows-x64": {
|
|
33
|
+
"os": "win32",
|
|
34
|
+
"cpu": "x64",
|
|
35
|
+
"rust": "x86_64-pc-windows-msvc",
|
|
36
|
+
"wheel": ["win_amd64"],
|
|
37
|
+
"binary": "docattest.exe"
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|