@aibyzero/cm-workflow 0.16.8 → 0.17.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/.codex-plugin/plugin.json +1 -1
- package/CHANGELOG.md +31 -0
- package/README.md +22 -3
- package/VERSION +1 -1
- package/compat/claude-commands/cm-notify.md +3 -0
- package/docs/cm-control-policy-v2.md +331 -0
- package/docs/execution-optimizations.md +1 -1
- package/docs/external-models.md +3 -3
- package/docs/human-correction.md +5 -5
- package/docs/installation.md +1 -1
- package/docs/js-workflow-control.md +119 -78
- package/docs/next-steps.md +19 -2
- package/docs/untested-branches.md +2 -3
- package/docs/user-guide.md +105 -5
- package/package.json +2 -1
- package/runtime/js/cm-ai/claude-review-stream.mjs +49 -13
- package/runtime/js/cm-ai/cm-ai-conversation-entry.mjs +103 -68
- package/runtime/js/cm-ai/cm-ai-qa-log.mjs +79 -27
- package/runtime/js/cm-ai/code-projects.mjs +4 -1
- package/runtime/js/cm-ai/control-policy.mjs +111 -0
- package/runtime/js/cm-ai/developer-adapter.mjs +7 -0
- package/runtime/js/cm-ai/drive-bootstrap.mjs +11 -8
- package/runtime/js/cm-ai/drive-core.mjs +9 -0
- package/runtime/js/cm-ai/durable-runner-state.mjs +883 -69
- package/runtime/js/cm-ai/effect-abandon-log.mjs +5 -2
- package/runtime/js/cm-ai/effect-contract.mjs +6 -1
- package/runtime/js/cm-ai/external-run-guard.mjs +18 -7
- package/runtime/js/cm-ai/gate-bridge.mjs +4 -4
- package/runtime/js/cm-ai/host-check.mjs +4 -1
- package/runtime/js/cm-ai/host-conversation-execution.mjs +33 -24
- package/runtime/js/cm-ai/host-documentation.mjs +53 -12
- package/runtime/js/cm-ai/host-project-execution.mjs +2 -2
- package/runtime/js/cm-ai/host-qa-executor.mjs +49 -4
- package/runtime/js/cm-ai/host-qa-fix-owner.mjs +53 -9
- package/runtime/js/cm-ai/host-qa-policy.mjs +63 -21
- package/runtime/js/cm-ai/host-review-authority.mjs +2 -2
- package/runtime/js/cm-ai/host-review-file.mjs +1 -1
- package/runtime/js/cm-ai/host-session.mjs +1 -1
- package/runtime/js/cm-ai/host-tool-bridge.mjs +43 -3
- package/runtime/js/cm-ai/host-workflow-capabilities.mjs +21 -10
- package/runtime/js/cm-ai/host.mjs +1 -2
- package/runtime/js/cm-ai/operator-guidance.mjs +89 -3
- package/runtime/js/cm-ai/provider-review-observation.mjs +50 -0
- package/runtime/js/cm-ai/qa-resource-release.mjs +46 -0
- package/runtime/js/cm-ai/qa-round-budget.mjs +13 -0
- package/runtime/js/cm-ai/review-package.mjs +26 -5
- package/runtime/js/cm-ai/reviewed-deliveries.mjs +1 -1
- package/runtime/js/cm-ai/reviewed-evidence-supersede.mjs +1 -1
- package/runtime/js/cm-ai/task-commit.mjs +39 -0
- package/runtime/js/cm-ai/task-runner.mjs +629 -121
- package/runtime/js/cm-ai/worker-claude-developer.mjs +9 -0
- package/runtime/js/cm-ai/worker-claude.mjs +10 -3
- package/runtime/js/cm-ai/worker-process-identity.mjs +43 -0
- package/runtime/js/cm-fix/execution.mjs +176 -48
- package/runtime/js/cm-fix/host.mjs +2 -1
- package/runtime/js/cm-fix/pinned-baseline.mjs +24 -0
- package/runtime/js/cm-fix/progress.mjs +13 -3
- package/runtime/js/cm-fix/protected-edits.mjs +10 -3
- package/runtime/js/cm-fix/repair.mjs +11 -3
- package/runtime/js/cm-fix/test-author.mjs +10 -3
- package/runtime/js/cm-idea/session.mjs +67 -4
- package/runtime/js/cm-init/review-evidence.mjs +3 -1
- package/runtime/js/cm-prd/review-correction.mjs +10 -4
- package/runtime/js/cm-prd/review-host.mjs +1 -1
- package/runtime/js/cm-prd/review-publication.mjs +5 -3
- package/runtime/js/cm-prd/session.mjs +43 -0
- package/runtime/js/cm-refactor/records.mjs +98 -8
- package/runtime/js/cm-refactor/workflow.mjs +80 -10
- package/runtime/js/cm-test/host.mjs +10 -3
- package/runtime/js/cm-test/session.mjs +45 -6
- package/runtime/js/notify-safe-read.mjs +49 -0
- package/runtime/js/notify-send.mjs +313 -0
- package/runtime/js/notify.mjs +164 -37
- package/runtime/js/policy-binding-compat.mjs +55 -0
- package/runtime/review.md +52 -7
- package/runtime/task-gates.md +107 -9
- package/scripts/ci-test-shards.mjs +67 -0
- package/scripts/ci-test-shards.test.mjs +64 -0
- package/scripts/ci-test-weights.json +222 -0
- package/scripts/cm-ai-abandon-effect-position.test.mjs +71 -10
- package/scripts/cm-ai-answer-gap-dispatch.test.mjs +86 -0
- package/scripts/cm-ai-answer-gap-driver.test.mjs +51 -0
- package/scripts/cm-ai-answer-gap-fixture.mjs +89 -0
- package/scripts/cm-ai-answer-gap-recheck.test.mjs +70 -0
- package/scripts/cm-ai-answer-gap-redo.test.mjs +103 -0
- package/scripts/cm-ai-batch-drive.mjs +98 -34
- package/scripts/cm-ai-batch-drive.test.mjs +124 -16
- package/scripts/cm-ai-batch-host.mjs +167 -29
- package/scripts/cm-ai-batch-host.test.mjs +303 -31
- package/scripts/cm-ai-batch-run-fixture.mjs +452 -0
- package/scripts/cm-ai-batch-run-member-stops.test.mjs +26 -0
- package/scripts/cm-ai-batch-run-parallel.test.mjs +7 -0
- package/scripts/cm-ai-batch-run-reschedule.test.mjs +17 -0
- package/scripts/cm-ai-batch-run.mjs +213 -25
- package/scripts/cm-ai-batch-run.test.mjs +21 -341
- package/scripts/cm-ai-bootstrap-flow.test.mjs +1 -7
- package/scripts/cm-ai-bootstrap.test.mjs +2 -3
- package/scripts/cm-ai-control-launch.test.mjs +219 -0
- package/scripts/cm-ai-control-policy.test.mjs +124 -0
- package/scripts/cm-ai-control-v2-drive.test.mjs +270 -0
- package/scripts/cm-ai-develop-answer-invalid.test.mjs +97 -0
- package/scripts/cm-ai-develop-call-timeout.test.mjs +146 -0
- package/scripts/cm-ai-develop-driver-gaps.test.mjs +37 -23
- package/scripts/cm-ai-direct-review.test.mjs +348 -0
- package/scripts/cm-ai-documentation-sync-gap.test.mjs +424 -0
- package/scripts/cm-ai-drive-bootstrap-rules.test.mjs +54 -25
- package/scripts/cm-ai-drive.mjs +142 -58
- package/scripts/cm-ai-drive.test.mjs +70 -84
- package/scripts/cm-ai-fix-integration.test.mjs +3 -5
- package/scripts/cm-ai-fix-later-delivery.test.mjs +3 -6
- package/scripts/cm-ai-fix-run-delivery.test.mjs +3 -6
- package/scripts/cm-ai-host-cli-workflow.test.mjs +267 -0
- package/scripts/cm-ai-host-fixture.mjs +296 -0
- package/scripts/cm-ai-host-protected.test.mjs +253 -0
- package/scripts/cm-ai-host.mjs +122 -100
- package/scripts/cm-ai-host.test.mjs +35 -776
- package/scripts/cm-ai-launch-diagnostics.test.mjs +5 -3
- package/scripts/cm-ai-legacy-history.test.mjs +53 -0
- package/scripts/cm-ai-light-modules.test.mjs +54 -0
- package/scripts/cm-ai-multi-root.test.mjs +4 -4
- package/scripts/cm-ai-nested-execution.test.mjs +16 -19
- package/scripts/cm-ai-operator-guidance.test.mjs +30 -5
- package/scripts/cm-ai-progress.test.mjs +10 -8
- package/scripts/cm-ai-project-qa-gate.test.mjs +37 -10
- package/scripts/cm-ai-protected-scope.test.mjs +8 -5
- package/scripts/cm-ai-qa-answer-gaps.test.mjs +99 -0
- package/scripts/cm-ai-qa-config-revision.test.mjs +1 -9
- package/scripts/cm-ai-qa-fingerprint-config.test.mjs +12 -9
- package/scripts/cm-ai-qa-fix-cross-session.test.mjs +95 -13
- package/scripts/cm-ai-qa-fixed-log.test.mjs +41 -0
- package/scripts/cm-ai-qa-recovery-args.test.mjs +7 -0
- package/scripts/cm-ai-qa-rerun-timeouts.test.mjs +59 -14
- package/scripts/cm-ai-review-drift-precedence.test.mjs +1 -1
- package/scripts/cm-ai-review-failure.test.mjs +137 -32
- package/scripts/cm-ai-review-timeout-config.test.mjs +3 -1
- package/scripts/cm-ai-review-timeout.test.mjs +132 -67
- package/scripts/cm-ai-run.mjs +91 -28
- package/scripts/cm-ai-run.test.mjs +11 -38
- package/scripts/cm-ai-selection-guard.test.mjs +2 -2
- package/scripts/cm-ai-session-chain.test.mjs +18 -15
- package/scripts/cm-ai-spec-rebind.test.mjs +14 -18
- package/scripts/cm-ai-supersede.test.mjs +489 -51
- package/scripts/cm-blocked-reason.test.mjs +2 -3
- package/scripts/cm-capability-gate.test.mjs +33 -1
- package/scripts/cm-check-drive-files.mjs +8 -0
- package/scripts/cm-check-drive.test.mjs +2 -5
- package/scripts/cm-check-runtime.sh +4 -2
- package/scripts/cm-claude-developer-worker.test.mjs +15 -0
- package/scripts/cm-claude-review-stream.test.mjs +21 -16
- package/scripts/cm-claude-review-synthetic.test.mjs +155 -0
- package/scripts/cm-claude-worker.test.mjs +23 -5
- package/scripts/cm-controlled-correction.test.mjs +469 -0
- package/scripts/cm-external-batch-host.test.mjs +30 -11
- package/scripts/cm-external-model-host.test.mjs +1 -1
- package/scripts/cm-external-model-recovery.test.mjs +32 -12
- package/scripts/cm-external-qa-fix.test.mjs +1 -1
- package/scripts/cm-external-review-reconciliation.test.mjs +6 -24
- package/scripts/cm-fix-abandon.test.mjs +163 -10
- package/scripts/cm-fix-drive-unprotected.test.mjs +37 -0
- package/scripts/cm-fix-drive.mjs +25 -7
- package/scripts/cm-fix-host.mjs +8 -7
- package/scripts/cm-fix-review-budget.test.mjs +13 -0
- package/scripts/cm-fix-review-recovery.test.mjs +36 -1
- package/scripts/cm-host-answer-timeout.test.mjs +65 -0
- package/scripts/cm-host-call-kinds.test.mjs +28 -0
- package/scripts/cm-host-documentation.test.mjs +49 -0
- package/scripts/cm-host-qa-executor.test.mjs +23 -12
- package/scripts/cm-host-qa-policy.test.mjs +43 -16
- package/scripts/cm-host-review-authority.test.mjs +0 -31
- package/scripts/cm-idea-answer-gaps.test.mjs +164 -0
- package/scripts/cm-idea-drive.mjs +49 -11
- package/scripts/cm-idea-drive.test.mjs +66 -0
- package/scripts/cm-idea-host.mjs +106 -16
- package/scripts/cm-init-answer-gaps.test.mjs +258 -0
- package/scripts/cm-init-drive.mjs +41 -7
- package/scripts/cm-init-host.mjs +61 -12
- package/scripts/cm-knowledge-closeout.test.mjs +3 -3
- package/scripts/cm-log-event.mjs +21 -10
- package/scripts/cm-notify-command.test.mjs +936 -0
- package/scripts/cm-notify-read-drift.test.mjs +270 -0
- package/scripts/cm-notify.mjs +481 -0
- package/scripts/cm-notify.test.mjs +312 -23
- package/scripts/cm-patrol.test.mjs +3 -2
- package/scripts/cm-prd-answer-gaps.test.mjs +216 -0
- package/scripts/cm-prd-drive.mjs +18 -5
- package/scripts/cm-prd-drive.test.mjs +15 -0
- package/scripts/cm-prd-host.mjs +37 -4
- package/scripts/cm-prd-review-disposition.test.mjs +6 -1
- package/scripts/cm-prd-review-gate.mjs +56 -0
- package/scripts/cm-refactor-answer-gaps.test.mjs +206 -0
- package/scripts/cm-refactor-drive.mjs +52 -8
- package/scripts/cm-refactor-drive.test.mjs +44 -0
- package/scripts/cm-refactor-host.test.mjs +10 -6
- package/scripts/cm-release-smoke.sh +2 -0
- package/scripts/cm-task-gate-correction.test.mjs +69 -16
- package/scripts/cm-task-gate.mjs +368 -34
- package/scripts/cm-task-gate.py +2 -0
- package/scripts/cm-task-gate.test.mjs +252 -4
- package/scripts/cm-test-answer-gaps.test.mjs +151 -0
- package/scripts/cm-test-drive.mjs +21 -5
- package/scripts/fixtures/bootstrap-review-runner.mjs +1 -7
- package/scripts/fixtures/claude-review-process.mjs +1 -1
- package/scripts/fixtures/native-gate-fixture.mjs +18 -0
- package/scripts/policy-binding-compat.test.mjs +103 -0
- package/scripts/scan-public-safety.py +4 -0
- package/scripts/validate-public-repo.py +4 -0
- package/skills/cm-ai/SKILL.md +8 -6
- package/skills/cm-ai/references/N3-execute-task.md +17 -2
- package/skills/cm-ai/references/N4-review.md +11 -21
- package/skills/cm-ai/references/N5-mark-done.md +1 -1
- package/skills/cm-ai/references/N6-qa-eval.md +49 -21
- package/skills/cm-ai/references/js-host-modes.md +35 -14
- package/skills/cm-ai/references/js-host-qa-recovery.md +85 -11
- package/skills/cm-ai/references/js-host-recovery.md +181 -21
- package/skills/cm-ai/references/js-host.md +76 -32
- package/skills/cm-check/SKILL.md +3 -3
- package/skills/cm-fix/SKILL.md +12 -0
- package/skills/cm-fix/references/js-host.md +44 -5
- package/skills/cm-idea/references/js-host.md +27 -2
- package/skills/cm-init/references/js-host.md +17 -4
- package/skills/cm-notify/SKILL.md +34 -0
- package/skills/cm-prd/references/js-change-recovery.md +35 -1
- package/skills/cm-prd/references/js-host.md +1 -1
- package/skills/cm-refactor/SKILL.md +12 -0
- package/skills/cm-refactor/references/js-host.md +17 -0
- package/skills/cm-test/references/js-host.md +14 -1
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
// Two readers enforce the notify.json safe-read boundary: the sender's async
|
|
2
|
+
// readRegularFile (runtime/js/notify-send.mjs, a self-contained file copied
|
|
3
|
+
// alone to ~/.cm-workflow/notify/) and the runtime's synchronous
|
|
4
|
+
// readRegularFileSync (runtime/js/notify-safe-read.mjs, imported by notify.mjs
|
|
5
|
+
// and therefore free of process-exit side effects). The security logic exists
|
|
6
|
+
// twice, so this file runs one fixture matrix through both and fails when
|
|
7
|
+
// either changes behaviour alone.
|
|
8
|
+
import test from 'node:test';
|
|
9
|
+
import assert from 'node:assert/strict';
|
|
10
|
+
import fs from 'node:fs';
|
|
11
|
+
import fsp from 'node:fs/promises';
|
|
12
|
+
import os from 'node:os';
|
|
13
|
+
import path from 'node:path';
|
|
14
|
+
import {spawnSync} from 'node:child_process';
|
|
15
|
+
import {fileURLToPath,pathToFileURL} from 'node:url';
|
|
16
|
+
import {readRegularFile,MAX_FILE_BYTES} from '../runtime/js/notify-send.mjs';
|
|
17
|
+
import {readRegularFileSync,MAX_NOTIFY_FILE_BYTES} from '../runtime/js/notify-safe-read.mjs';
|
|
18
|
+
|
|
19
|
+
const posix=process.platform!=='win32';
|
|
20
|
+
const JSON_TEXT=JSON.stringify({version:1,command:['/bin/true']});
|
|
21
|
+
const sandbox=t=>{const dir=fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(),'cm-read-drift-')));t.after(()=>fs.rmSync(dir,{recursive:true,force:true}));return dir;};
|
|
22
|
+
const mkfifo=file=>assert.equal(spawnSync('mkfifo',['-m','600',file]).status,0);
|
|
23
|
+
const clone=(st,over)=>Object.assign(Object.create(Object.getPrototypeOf(st)),st,over);
|
|
24
|
+
|
|
25
|
+
// One scenario = files to create + optional hooks into the check/open window.
|
|
26
|
+
// Hooks are written once; each reader gets them through its own fileOps shape.
|
|
27
|
+
// lstat(st) -> st seen by the path check open(file,flags) before the open
|
|
28
|
+
// fstat(st) -> st seen on the opened handle openError -> open throws this
|
|
29
|
+
function asyncOps(hooks,calls){
|
|
30
|
+
return {
|
|
31
|
+
lstat:async(file,options)=>(hooks.lstat??(x=>x))(await fsp.lstat(file,options)),
|
|
32
|
+
open:async(file,flags)=>{
|
|
33
|
+
calls.push(flags);hooks.open?.(file,flags);
|
|
34
|
+
if(hooks.openError)throw hooks.openError;
|
|
35
|
+
const handle=await fsp.open(file,flags);
|
|
36
|
+
return {stat:async options=>(hooks.fstat??(x=>x))(await handle.stat(options)),
|
|
37
|
+
read:(...args)=>handle.read(...args),close:()=>handle.close()};
|
|
38
|
+
}};
|
|
39
|
+
}
|
|
40
|
+
function syncOps(hooks,calls){
|
|
41
|
+
return {...fs,
|
|
42
|
+
lstatSync:(file,options)=>(hooks.lstat??(x=>x))(fs.lstatSync(file,options)),
|
|
43
|
+
openSync:(file,flags)=>{
|
|
44
|
+
calls.push(flags);hooks.open?.(file,flags);
|
|
45
|
+
if(hooks.openError)throw hooks.openError;
|
|
46
|
+
return fs.openSync(file,flags);
|
|
47
|
+
},
|
|
48
|
+
fstatSync:(fd,options)=>(hooks.fstat??(x=>x))(fs.fstatSync(fd,options))};
|
|
49
|
+
}
|
|
50
|
+
const eacces=()=>Object.assign(new Error('denied'),{code:'EACCES'});
|
|
51
|
+
// A blocking open of a FIFO would hang the process; the stand-in fails fast.
|
|
52
|
+
const toFifo=(file,flags)=>{fs.unlinkSync(file);mkfifo(file);if(!(flags&fs.constants.O_NONBLOCK))throw new Error('blocking open on a FIFO');};
|
|
53
|
+
|
|
54
|
+
const SCENARIOS=[
|
|
55
|
+
{name:'regular file',make:d=>write(d,JSON_TEXT),ok:{bytes:Buffer.from(JSON_TEXT),text:JSON_TEXT}},
|
|
56
|
+
{name:'BOM file: bytes keep it, text drops it',make:d=>write(d,'\uFEFF'+JSON_TEXT),ok:{bytes:Buffer.from('\uFEFF'+JSON_TEXT),text:JSON_TEXT}},
|
|
57
|
+
{name:'invalid UTF-8: bytes fine, text refused',make:d=>write(d,Buffer.from([0x7b,0xff,0xfe,0x7d])),
|
|
58
|
+
ok:{bytes:Buffer.from([0x7b,0xff,0xfe,0x7d])},textRefused:{state:'invalid',reason:undefined}},
|
|
59
|
+
{name:'exactly 64 KiB',make:d=>write(d,JSON_TEXT+' '.repeat(64*1024-JSON_TEXT.length)),ok:{size:64*1024}},
|
|
60
|
+
{name:'64 KiB + 1',make:d=>write(d,JSON_TEXT+' '.repeat(64*1024+1-JSON_TEXT.length)),refused:{state:'invalid',reason:'too_large'}},
|
|
61
|
+
{name:'missing',make:()=>{},refused:{state:'missing',reason:undefined}},
|
|
62
|
+
{name:'path below a regular file (ENOTDIR)',make:d=>{write(d,'x');return path.join(d,'notify.json','inner');},refused:{state:'invalid',reason:undefined}},
|
|
63
|
+
{name:'symlink to a valid file',make:d=>{fs.writeFileSync(path.join(d,'real.json'),JSON_TEXT);fs.symlinkSync(path.join(d,'real.json'),path.join(d,'notify.json'));},refused:{state:'invalid',reason:'permission'}},
|
|
64
|
+
{name:'dangling symlink',make:d=>fs.symlinkSync(path.join(d,'nowhere.json'),path.join(d,'notify.json')),refused:{state:'invalid',reason:'permission'}},
|
|
65
|
+
{name:'FIFO',posix:true,make:d=>mkfifo(path.join(d,'notify.json')),refused:{state:'invalid',reason:'permission'}},
|
|
66
|
+
{name:'directory',make:d=>fs.mkdirSync(path.join(d,'notify.json')),refused:{state:'invalid',reason:'permission'}},
|
|
67
|
+
// `other.json` exists before the check, so it has its own identity; at the swap
|
|
68
|
+
// it is renamed onto the target. unlink+create could be handed the freed inode
|
|
69
|
+
// back (ext4 does), which would make the identity check legitimately pass.
|
|
70
|
+
{name:'swapped for another regular file between check and open',make:d=>{fs.writeFileSync(path.join(d,'other.json'),'{}');return write(d,JSON_TEXT);},
|
|
71
|
+
hooks:d=>({open:file=>{
|
|
72
|
+
const before=fs.statSync(file,{bigint:true});fs.renameSync(path.join(d,'other.json'),file);
|
|
73
|
+
assert.notEqual(fs.statSync(file,{bigint:true}).ino,before.ino,'fixture invalid: the swapped-in file must have another inode');}}),refused:{state:'invalid',reason:'permission'},flags:true},
|
|
74
|
+
// Identity is dev AND ino: each one alone must be enough to refuse.
|
|
75
|
+
{name:'same ino but another device after the check',make:d=>write(d,JSON_TEXT),hooks:()=>({fstat:st=>clone(st,{dev:st.dev+1n})}),refused:{state:'invalid',reason:'permission'}},
|
|
76
|
+
{name:'same device but another ino after the check',make:d=>write(d,JSON_TEXT),hooks:()=>({fstat:st=>clone(st,{ino:st.ino+1n})}),refused:{state:'invalid',reason:'permission'}},
|
|
77
|
+
{name:'swapped for a FIFO between check and open',posix:true,make:d=>write(d,JSON_TEXT),hooks:()=>({open:toFifo}),refused:{state:'invalid',reason:'permission'},flags:true},
|
|
78
|
+
{name:'grown past the cap after the check',make:d=>write(d,JSON_TEXT),hooks:()=>({fstat:st=>clone(st,{size:BigInt(64*1024+1)})}),refused:{state:'invalid',reason:'too_large'}},
|
|
79
|
+
{name:'handle is not a regular file after the check',make:d=>write(d,JSON_TEXT),hooks:()=>({fstat:st=>clone(st,{isFile:()=>false})}),refused:{state:'invalid',reason:'permission'}},
|
|
80
|
+
{name:'open fails (EACCES)',make:d=>write(d,JSON_TEXT),hooks:()=>({openError:eacces()}),refused:{state:'invalid',reason:undefined},flags:true},
|
|
81
|
+
];
|
|
82
|
+
function write(dir,content){const file=path.join(dir,'notify.json');fs.writeFileSync(file,content);return file;}
|
|
83
|
+
|
|
84
|
+
async function outcome(read,raw){
|
|
85
|
+
try{return {value:await read(raw)};}
|
|
86
|
+
catch(error){const d=error.detail??error;return {state:d.state,reason:d.reason};}
|
|
87
|
+
}
|
|
88
|
+
const normalize=(result,raw)=>result.value===undefined?result:{value:raw?Buffer.from(result.value).toString('hex'):result.value};
|
|
89
|
+
|
|
90
|
+
test('drift guard: the runtime sync reader and the sender async reader take the same decision for every fixture, bytes and text',async t=>{
|
|
91
|
+
assert.equal(MAX_NOTIFY_FILE_BYTES,MAX_FILE_BYTES,'both readers cap at the same size');
|
|
92
|
+
for(const scenario of SCENARIOS){
|
|
93
|
+
if(scenario.posix&&!posix)continue;
|
|
94
|
+
for(const raw of [true,false]){
|
|
95
|
+
const mode=raw?'bytes':'text';
|
|
96
|
+
const results=[];
|
|
97
|
+
for(const kind of ['async','sync']){
|
|
98
|
+
const dir=sandbox(t);
|
|
99
|
+
const target=scenario.make(dir)??path.join(dir,'notify.json');
|
|
100
|
+
const hooks=scenario.hooks?scenario.hooks(dir):{},calls=[];
|
|
101
|
+
const read=kind==='async'
|
|
102
|
+
?()=>readRegularFile(target,'notify.json',{fileOps:asyncOps(hooks,calls),bytes:raw})
|
|
103
|
+
:()=>Promise.resolve().then(()=>readRegularFileSync(target,'notify.json',{fileOps:syncOps(hooks,calls),bytes:raw}));
|
|
104
|
+
const result=await outcome(read,raw);
|
|
105
|
+
// libuv defines O_NOFOLLOW and O_NONBLOCK as 0 on Windows: where a flag is 0
|
|
106
|
+
// the open has no protection of that kind (see docs/user-guide.md).
|
|
107
|
+
if(scenario.flags&&calls.length&&process.platform!=='win32'){
|
|
108
|
+
assert.notEqual(calls[0]&fs.constants.O_NONBLOCK,0,`${scenario.name} (${kind}): open is non-blocking`);
|
|
109
|
+
assert.notEqual(calls[0]&fs.constants.O_NOFOLLOW,0,`${scenario.name} (${kind}): open does not follow links`);
|
|
110
|
+
}
|
|
111
|
+
results.push(normalize(result,raw));
|
|
112
|
+
const label=`${scenario.name} / ${mode} / ${kind}`;
|
|
113
|
+
if(!raw&&scenario.textRefused)assert.deepEqual(result,scenario.textRefused,label);
|
|
114
|
+
else if(scenario.refused)assert.deepEqual(result,scenario.refused,label);
|
|
115
|
+
else{
|
|
116
|
+
assert.equal(result.value!==undefined,true,`${label}: accepted`);
|
|
117
|
+
if(scenario.ok.size!==undefined)assert.equal(result.value.length,scenario.ok.size,label);
|
|
118
|
+
else assert.deepEqual(raw?Buffer.from(result.value):result.value,raw?scenario.ok.bytes:scenario.ok.text,label);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
assert.deepEqual(results[0],results[1],`${scenario.name} / ${mode}: async and sync readers agree`);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
test('runtime sync reader: reads from the handle it checked and always closes it',{skip:!posix},t=>{
|
|
127
|
+
const dir=sandbox(t);const file=write(dir,JSON_TEXT);
|
|
128
|
+
const closed=[];
|
|
129
|
+
const ops={...fs,closeSync:fd=>{closed.push(fd);fs.closeSync(fd);}};
|
|
130
|
+
assert.equal(readRegularFileSync(file,'notify.json',{fileOps:ops}),JSON_TEXT);
|
|
131
|
+
assert.equal(closed.length,1);
|
|
132
|
+
closed.length=0;
|
|
133
|
+
assert.throws(()=>readRegularFileSync(file,'notify.json',{fileOps:{...ops,fstatSync:()=>{throw new Error('boom');}}}),error=>error.state==='invalid');
|
|
134
|
+
assert.equal(closed.length,1,'fd closed when the check after open throws');
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
// notify.mjs is imported by every host; importing it must never end the
|
|
138
|
+
// process. The sender (a CLI entry) may exit on an unsupported Node, so the
|
|
139
|
+
// runtime must not import it, directly or through anything it imports.
|
|
140
|
+
const RUNTIME=new URL('../runtime/js/',import.meta.url);
|
|
141
|
+
// Small scanner, not a parser: static imports/re-exports (also over several
|
|
142
|
+
// lines), side-effect imports and dynamic import() with a literal specifier.
|
|
143
|
+
// A dynamic import() with anything else as its argument is reported as
|
|
144
|
+
// `nonLiteral` so the test fails instead of silently missing a dependency.
|
|
145
|
+
// Lexes the source once: comments are dropped, and every string and regex
|
|
146
|
+
// literal is replaced by a numbered placeholder so that "import x from './y'"
|
|
147
|
+
// inside a string, a comment wedged between two strings, or a regex with quotes
|
|
148
|
+
// in it can neither hide nor fake an import. Template literals keep only their
|
|
149
|
+
// ${...} expressions, lexed the same way, so a dynamic import() inside one still
|
|
150
|
+
// counts. Specifiers are read back from the placeholder table.
|
|
151
|
+
const KEYWORDS=new Set(['return','typeof','instanceof','in','of','new','delete','void','throw','case','do','else','yield','await']);
|
|
152
|
+
function lex(source,start,literals,stopAtBrace){
|
|
153
|
+
let out='',i=start,depth=0;const n=source.length;
|
|
154
|
+
// A `/` starts a regex after an operator, an opener or a keyword; it is a
|
|
155
|
+
// division after a value (identifier, number, `)`, `]`, postfix `++`/`--`).
|
|
156
|
+
// After `}` the two cannot be told apart without parsing (block end vs object
|
|
157
|
+
// literal), so the scan fails loudly instead of guessing.
|
|
158
|
+
const regexAllowed=()=>{
|
|
159
|
+
const head=out.replace(/\s+$/,''),prev=head.slice(-1),word=/(?:^|[^\w$])([A-Za-z_$][\w$]*)$/.exec(head)?.[1];
|
|
160
|
+
if(prev==='}')throw new Error(`import scan: ambiguous "/" after "}" at offset ${i}; rewrite the source so the scanner can tell division from a regex`);
|
|
161
|
+
if(/\+\+$|--$/.test(head))return false;
|
|
162
|
+
return prev===''||/[(,=:\[!&|?{;+\-*%<>~^]/.test(prev)||KEYWORDS.has(word);
|
|
163
|
+
};
|
|
164
|
+
const push=(kind,text)=>{literals.push({kind,text});out+=kind==='string'?`"\u00a7${literals.length-1}\u00a7"`:'/\u00a7R\u00a7/';};
|
|
165
|
+
while(i<n){
|
|
166
|
+
const c=source[i],d=source[i+1];
|
|
167
|
+
if(c==='/'&&d==='/'){while(i<n&&source[i]!=='\n')i++;continue;}
|
|
168
|
+
if(c==='/'&&d==='*'){const close=source.indexOf('*/',i+2);i=close===-1?n:close+2;out+=' ';continue;}
|
|
169
|
+
if(c==="'"||c==='"'){
|
|
170
|
+
let j=i+1;while(j<n&&source[j]!==c&&source[j]!=='\n'){if(source[j]==='\\')j++;j++;}
|
|
171
|
+
push('string',source.slice(i+1,j));i=j+1;continue;
|
|
172
|
+
}
|
|
173
|
+
if(c==='`'){
|
|
174
|
+
out+='`';let j=i+1;
|
|
175
|
+
while(j<n&&source[j]!=='`'){
|
|
176
|
+
if(source[j]==='\\'){j+=2;continue;}
|
|
177
|
+
if(source[j]==='$'&&source[j+1]==='{'){const inner=lex(source,j+2,literals,true);out+='${'+inner.code+'}';j=inner.index+1;continue;}
|
|
178
|
+
j++;
|
|
179
|
+
}
|
|
180
|
+
out+='`';i=j+1;continue;
|
|
181
|
+
}
|
|
182
|
+
if(c==='/'&®exAllowed()){
|
|
183
|
+
let j=i+1,cls=false;
|
|
184
|
+
while(j<n){const ch=source[j];if(ch==='\\'){j+=2;continue;}if(ch==='\n')break;if(cls){if(ch===']')cls=false;}else if(ch==='[')cls=true;else if(ch==='/')break;j++;}
|
|
185
|
+
push('regex',source.slice(i+1,j));i=j+1;continue;
|
|
186
|
+
}
|
|
187
|
+
if(stopAtBrace){if(c==='{')depth++;else if(c==='}'){if(depth===0)return {code:out,index:i};depth--;}}
|
|
188
|
+
out+=c;i++;
|
|
189
|
+
}
|
|
190
|
+
return {code:out,index:i};
|
|
191
|
+
}
|
|
192
|
+
export function lexForImports(source){const literals=[];return {code:lex(source,0,literals,false).code,literals};}
|
|
193
|
+
export function scanImports(source){
|
|
194
|
+
const {code,literals}=lexForImports(source);
|
|
195
|
+
const text=index=>literals[Number(index)].text;
|
|
196
|
+
const specifiers=[];
|
|
197
|
+
for(const m of code.matchAll(/\b(?:import|export)\b[^;"`()]*?\bfrom\s*"\u00a7(\d+)\u00a7"/g))specifiers.push(text(m[1]));
|
|
198
|
+
for(const m of code.matchAll(/\bimport\s*"\u00a7(\d+)\u00a7"/g))specifiers.push(text(m[1]));
|
|
199
|
+
let literal=0;
|
|
200
|
+
for(const m of code.matchAll(/\bimport\s*\(\s*"\u00a7(\d+)\u00a7"\s*\)/g)){specifiers.push(text(m[1]));literal+=1;}
|
|
201
|
+
const nonLiteral=[...code.matchAll(/\bimport\s*\(/g)].length-literal;
|
|
202
|
+
return {specifiers,nonLiteral};
|
|
203
|
+
}
|
|
204
|
+
// Files of the runtime import graph, as paths relative to runtime/js/.
|
|
205
|
+
// Relative specifiers resolve from the importing file's own directory; the
|
|
206
|
+
// only other specifiers allowed are node: built-ins.
|
|
207
|
+
function importGraph(entry,seen=new Set()){
|
|
208
|
+
if(seen.has(entry))return seen;seen.add(entry);
|
|
209
|
+
const base=new URL(entry,RUNTIME);
|
|
210
|
+
const {specifiers,nonLiteral}=scanImports(fs.readFileSync(base,'utf8'));
|
|
211
|
+
assert.equal(nonLiteral,0,`${entry} has a dynamic import() with a non-literal specifier; the import graph cannot be checked`);
|
|
212
|
+
for(const specifier of specifiers){
|
|
213
|
+
if(specifier.startsWith('node:'))continue;
|
|
214
|
+
assert(specifier.startsWith('.'),`${entry} imports ${specifier}: only node: built-ins and relative files are allowed`);
|
|
215
|
+
importGraph(path.relative(fileURLToPath(RUNTIME),fileURLToPath(new URL(specifier,base))),seen);
|
|
216
|
+
}
|
|
217
|
+
return seen;
|
|
218
|
+
}
|
|
219
|
+
test('import scanner: multi-line static imports, dynamic import() with a literal, non-literal dynamic import()',()=>{
|
|
220
|
+
const multi=scanImports(`import {\n a,\n b,\n} from './multi.mjs';\nexport * from "./re.mjs";\nimport './side.mjs';\n// import {x} from './comment.mjs';\n/* import('./block.mjs') */\nconst lazy=await import('./lazy/dyn.mjs');`);
|
|
221
|
+
assert.deepEqual(multi.specifiers.sort(),['./lazy/dyn.mjs','./multi.mjs','./re.mjs','./side.mjs']);
|
|
222
|
+
assert.equal(multi.nonLiteral,0);
|
|
223
|
+
assert.equal(scanImports('const m=await import(name);import(`./${x}.mjs`);import("./ok.mjs");').nonLiteral,2);
|
|
224
|
+
assert.equal(scanImports('console.log(import.meta.url);').nonLiteral,0);
|
|
225
|
+
// Literals cannot hide or fake an import: text inside strings, a block comment
|
|
226
|
+
// wedged between two strings, a line-end comment, a regex with quotes.
|
|
227
|
+
const tricky=scanImports(`const a='x'; /* import './hidden.mjs' */ const b="import z from './fake.mjs'";
|
|
228
|
+
const c=a+'y'; // import './tail.mjs'
|
|
229
|
+
const d=/['"]\\/import/.test(c); import real from './real.mjs';
|
|
230
|
+
const e=\`import './tpl.mjs' \${await import('./dyn.mjs')}\`;`);
|
|
231
|
+
assert.deepEqual(tricky.specifiers.sort(),['./dyn.mjs','./real.mjs']);assert.equal(tricky.nonLiteral,0);
|
|
232
|
+
assert.deepEqual(lexForImports(`'a' /* c */ 'b'`).literals.map(l=>l.text),['a','b']);
|
|
233
|
+
// Division is not a regex: postfix ++/--, a value or a closing paren before "/"
|
|
234
|
+
// must not swallow a later import on the same line (or inside a template).
|
|
235
|
+
for(const source of ['let n=1; n++ / 2; function later(){ return import("./late.mjs"); }',
|
|
236
|
+
'const q=(a+b)/2, r=a/b; import late from "./late.mjs";',
|
|
237
|
+
'const t=`${n-- / 2} ${await import("./late.mjs")}`;']){
|
|
238
|
+
const found=scanImports(source);assert.deepEqual(found.specifiers,['./late.mjs'],source);assert.equal(found.nonLiteral,0,source);
|
|
239
|
+
}
|
|
240
|
+
assert.throws(()=>scanImports('const o={a:1}/2; import("./x.mjs");'),/ambiguous "\/" after "}"/);
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
const importChild=(file,env={})=>spawnSync(process.execPath,['--input-type=module','-e',
|
|
244
|
+
`await import(${JSON.stringify(pathToFileURL(file).href)});console.log('imported');`],{encoding:'utf8',timeout:10000,env:{...process.env,...env}});
|
|
245
|
+
|
|
246
|
+
test('importing runtime/js/notify.mjs has no process-exit side effect, also when import.meta.main is missing',t=>{
|
|
247
|
+
const graph=[...importGraph('notify.mjs')];
|
|
248
|
+
assert(graph.includes('notify-safe-read.mjs'));
|
|
249
|
+
assert(!graph.some(file=>file.includes('notify-send')),`the runtime must not import the sender: ${graph}`);
|
|
250
|
+
for(const file of graph){
|
|
251
|
+
const source=fs.readFileSync(new URL(file,RUNTIME),'utf8');
|
|
252
|
+
assert(!/process\.exit\s*\(/.test(source),`${file} must not call process.exit`);
|
|
253
|
+
}
|
|
254
|
+
// Plain import in a child: exits 0.
|
|
255
|
+
const plain=importChild(fileURLToPath(new URL('notify.mjs',RUNTIME)));
|
|
256
|
+
assert.equal(plain.status,0,plain.stderr);assert.match(plain.stdout,/imported/);
|
|
257
|
+
// Simulated Node without import.meta.main: copy the whole import graph with
|
|
258
|
+
// every `import.meta.main` replaced by `undefined`, import that copy.
|
|
259
|
+
const copy=(files,dir)=>{for(const file of files){
|
|
260
|
+
const target=path.join(dir,file);fs.mkdirSync(path.dirname(target),{recursive:true});
|
|
261
|
+
fs.writeFileSync(target,fs.readFileSync(new URL(file,RUNTIME),'utf8').replaceAll('import.meta.main','undefined'));}};
|
|
262
|
+
const runtimeCopy=sandbox(t);copy(graph.concat(['notify-run.mjs']),runtimeCopy);
|
|
263
|
+
const simulated=importChild(path.join(runtimeCopy,'notify.mjs'));
|
|
264
|
+
assert.equal(simulated.status,0,simulated.stderr);assert.match(simulated.stdout,/imported/);
|
|
265
|
+
// Negative control: the same simulation does kill an import of the sender,
|
|
266
|
+
// so the check above is able to fail.
|
|
267
|
+
const senderCopy=sandbox(t);copy(['notify-send.mjs'],senderCopy);
|
|
268
|
+
const control=importChild(path.join(senderCopy,'notify-send.mjs'));
|
|
269
|
+
assert.equal(control.status,3);assert.doesNotMatch(control.stdout,/imported/);
|
|
270
|
+
});
|