@josephyulei/summon-foundation 0.1.1 → 0.1.3
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/README.md +10 -4
- package/bin/summon.mjs +65 -29
- package/lib/acquire.mjs +44 -11
- package/lib/cli-options.mjs +19 -0
- package/lib/operation-result.mjs +36 -0
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -6,13 +6,19 @@ Foundation 的薄获取入口,不包含完整运行包,没有 npm 安装生
|
|
|
6
6
|
可用性以 npm registry 的实际版本和公共 GitHub Release 为准;本目录或说明存在不是发布回执。只使用本项目已核验的包名,缺包时不换同名来源。
|
|
7
7
|
|
|
8
8
|
```text
|
|
9
|
-
npx --yes --package @josephyulei/summon-foundation@0.1.
|
|
9
|
+
npx --yes --package @josephyulei/summon-foundation@0.1.3 summon foundation
|
|
10
10
|
```
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
0.1.3 入口配合 Foundation 0.2.8 或后续支持目录选择的正式发行使用。默认命令实际查询、固定版本、披露缓存、下载并校验,然后保持运行等待安装页目录选择。选择目录后还要亲自确认精确安装计划;不会注册 Skill 或接入项目。实际获取仍须通过 registry 与 Release 校验,本文不替代发布证明。
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
Codex 需要打开返回的内置浏览器网址,并分段等待同次操作结果。npm 不能自行操控或唤醒 Codex,对话主动告知不是程序退出码保证;新对话接续仍待真人验收。系统浏览器只是明确告知后的备用,不自动打开。
|
|
15
|
+
|
|
16
|
+
0.1.3 的程序收尾:联网前建立同次操作记录,输出阶段、大文件实际字节/总量、尝试次数与最近进展;不能保存早期记录则不开始联网。大文件每次最多 10 分钟,连接 15 秒、低速 60 秒有界停止,仅暂时网络错误可重试一次。失败保留 curl 退出码和脱敏类别,校验失败与网络失败分开。`--status <返回的结果文件>` 只读恢复,不获取、安装或重试;中间记录不证明进程仍在运行。新运行版安装后程序核验稳定入口,启动唯一工作台至就绪便返回结果,不等常驻工作台退出;Codex 打开返回的实际网址并解释结果。旧 0.1.2 不含上述修复。工具返回 running 不是任务完成,运行中的 Codex 应继续观察原句柄;程序无法强制宿主不提前结束,新对话无追问反馈仍待验证。
|
|
17
|
+
|
|
18
|
+
显式 `--inspect` 才只查询,不下载运行文件。已有选择时可用 `--prepare --version <固定版本> --destination <绝对目录>`;`--acquire --version <固定版本>` 只准备更新输入,不执行更新。更新使用已安装稳定入口。旧运行版不支持页面选择时拒绝自动采用默认目录。没有 npm 时先从 GitHub 安装说明检查环境,不自动安装全局依赖。
|
|
19
|
+
|
|
20
|
+
需要 macOS arm64、Node.js 22.9+ 与 npm。无 TTY 不会卡在终端提问;目录选择在页面进行,关闭页面不算确认或取消。
|
|
15
21
|
固定版本准备阶段匿名校验 GitHub 仓库身份、不可变 Release、签名证明、时间戳、源码提交和资产字节,随后使用随包运行时进入现有 Foundation 确认页面。
|
|
16
22
|
不关闭 Gatekeeper、不清除隔离属性,不冒充 Apple 签名或公证。遇到权限问题安全停止。
|
|
17
23
|
|
|
18
|
-
尚无 Foundation / Skill 的缓存说明:[公开清理指南](https://github.com/y15801172513-lang/foundation/blob/main/docs/cache-cleanup.md)
|
|
24
|
+
尚无 Foundation / Skill 的缓存说明:[公开清理指南](https://github.com/y15801172513-lang/foundation/blob/main/docs/cache-cleanup.md)。当前 README 不是安装完成回执。
|
package/bin/summon.mjs
CHANGED
|
@@ -1,34 +1,25 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import fs from 'node:fs';
|
|
3
3
|
import path from 'node:path';
|
|
4
|
-
import {spawnSync} from 'node:child_process';
|
|
4
|
+
import {spawn,spawnSync} from 'node:child_process';
|
|
5
5
|
import {inspectRelease,acquireRelease,plainPath} from '../lib/acquire.mjs';
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
import {parseSummonArgs} from '../lib/cli-options.mjs';
|
|
8
|
+
import {createOperation,saveOperation,readOperation,runtimeObservation} from '../lib/operation-result.mjs';
|
|
9
|
+
|
|
10
|
+
const help=`summon foundation [--version x.y.z] [--destination /absolute/folder]
|
|
11
|
+
summon foundation --inspect [--version x.y.z]
|
|
8
12
|
summon foundation --prepare --version x.y.z --destination /absolute/folder
|
|
9
13
|
summon foundation --acquire --version x.y.z
|
|
10
|
-
|
|
14
|
+
summon foundation --status /absolute/acquisition/operation-result.json
|
|
15
|
+
默认命令下载并验证正式发行,进入安装准备;未给目录时在安装页选择。不会静默安装或注册 Skill。
|
|
16
|
+
--inspect 才只读查询发行,不下载运行归档。
|
|
11
17
|
--prepare 下载并核验固定发行,打开 Foundation 本人确认流程;不会代替确认。
|
|
12
18
|
--acquire 仅下载并核验更新材料,返回候选与回执;不启动安装或执行更新。
|
|
19
|
+
--status 只读本次结果记录;中间记录不证明进程仍在运行,不重试或重放确认。
|
|
13
20
|
仅 macOS arm64。npm 入口需要 Node/npm;已安装 Foundation 使用随包运行时。
|
|
14
21
|
普通终端不能控制 Codex 对话或内置浏览器。无需 npm 的引导见公共仓库安装说明。`;
|
|
15
22
|
function fail(message){throw Error(message);}
|
|
16
|
-
function parse(args){
|
|
17
|
-
if(args.length===0||args.length===1&&['--help','-h'].includes(args[0]))return {help:true};
|
|
18
|
-
if(args.shift()!=='foundation')fail('仅支持 summon foundation');
|
|
19
|
-
const result={prepare:false};const seen=new Set();
|
|
20
|
-
while(args.length){const flag=args.shift();if(seen.has(flag))fail('重复参数');seen.add(flag);
|
|
21
|
-
if(flag==='--prepare')result.prepare=true;
|
|
22
|
-
else if(flag==='--acquire')result.acquire=true;
|
|
23
|
-
else if(flag==='--inspect')result.inspect=true;
|
|
24
|
-
else if(flag==='--version'||flag==='--destination'){if(!args.length||args[0].startsWith('--'))fail('参数缺值');result[flag.slice(2)]=args.shift();}
|
|
25
|
-
else fail('未知参数;请查看 --help');
|
|
26
|
-
}
|
|
27
|
-
if((result.prepare||result.acquire)&&result.inspect||result.prepare&&result.acquire||result.destination&&!result.prepare)fail('参数组合不支持');
|
|
28
|
-
if(result.prepare&&(!result.version||!result.destination))fail('准备前须由对话明确固定版本和意向目录');
|
|
29
|
-
if(result.acquire&&!result.version)fail('获取更新材料前须固定版本');
|
|
30
|
-
return result;
|
|
31
|
-
}
|
|
32
23
|
function sourceGuard(){
|
|
33
24
|
for(const start of [fs.realpathSync(process.cwd()),fs.realpathSync(import.meta.dirname)]){
|
|
34
25
|
for(let p=start;p!==path.dirname(p);p=path.dirname(p)){
|
|
@@ -46,24 +37,69 @@ function stageDirectory(){
|
|
|
46
37
|
const stat=fs.lstatSync(cache);if(stat.uid!==process.getuid()||(stat.mode&0o777)!==0o700)fail('获取缓存必须由当前用户独占 0700;不会自动 chmod 已有目录');
|
|
47
38
|
return fs.mkdtempSync(path.join(cache,'acquisition.'));
|
|
48
39
|
}
|
|
40
|
+
let operation=null,stage=null;
|
|
41
|
+
function updateOperation(patch){if(!operation)return;Object.assign(operation,patch,{updatedAt:new Date().toISOString()});const file=saveOperation(operation,stage);console.log(JSON.stringify({status:'FOUNDATION_INSTALL_OPERATION',...operation,resultFile:file,recordSaved:!!file}));}
|
|
42
|
+
async function openInstalledWorkbench(root,env){
|
|
43
|
+
let child;
|
|
44
|
+
try{
|
|
45
|
+
plainPath(root);const launcher=path.join(root,'bin/foundation-kit');plainPath(launcher);
|
|
46
|
+
const stdout=path.join(stage,'workbench.stdout.log'),stderr=path.join(stage,'workbench.stderr.log');
|
|
47
|
+
const out=fs.openSync(stdout,'wx',0o600);let err;
|
|
48
|
+
try{err=fs.openSync(stderr,'wx',0o600);child=spawn(launcher,['workbench','open','--root',root],{detached:true,stdio:['ignore',out,err],env});}finally{fs.closeSync(out);if(err!==undefined)fs.closeSync(err);}
|
|
49
|
+
await new Promise((resolve,reject)=>{child.once('spawn',resolve);child.once('error',reject);});child.unref();
|
|
50
|
+
for(let n=0;n<100;n++){
|
|
51
|
+
if(child.exitCode!==null)throw Error('工作台进程提前退出');
|
|
52
|
+
let ready;try{ready=JSON.parse(fs.readFileSync(stdout,'utf8'));}catch{}
|
|
53
|
+
if(ready?.url){const url=new URL(ready.url);if(url.protocol!=='http:'||url.hostname!=='127.0.0.1'||url.username||url.password)throw Error('工作台网址无效');const response=await fetch(url,{signal:AbortSignal.timeout(5000),redirect:'error'});if(!response.ok)throw Error('工作台未就绪');return{state:'ready',url:url.href,pid:child.pid,requiredHostAction:'open-returned-loopback-url-in-codex-browser'};}
|
|
54
|
+
await new Promise(r=>setTimeout(r,100));
|
|
55
|
+
}
|
|
56
|
+
throw Error('工作台启动超时');
|
|
57
|
+
}catch{if(child&&child.exitCode===null)child.kill('SIGTERM');return{state:'not-ready',reason:'安装结果保留;工作台未能就绪,可从已安装入口重新打开'};}
|
|
58
|
+
}
|
|
49
59
|
try{
|
|
50
|
-
const args=
|
|
60
|
+
const args=parseSummonArgs(process.argv.slice(2));
|
|
51
61
|
if(args.help){console.log(help);process.exit(0);}
|
|
62
|
+
if(args.status){console.log(JSON.stringify(readOperation(args.status),null,2));process.exit(0);}
|
|
63
|
+
const [major,minor]=process.versions.node.split('.').map(Number);
|
|
64
|
+
if(major<22||(major===22&&minor<9))fail('获取入口需要 Node.js 22.9 或以上;尚未安装,不自动安装依赖');
|
|
52
65
|
if(process.platform!=='darwin'||process.arch!=='arm64')fail('当前仅支持 macOS arm64;尚未安装');
|
|
66
|
+
if(args.prepare||args.acquire)sourceGuard();
|
|
67
|
+
if(args.prepare||args.acquire){operation=createOperation();stage=stageDirectory();updateOperation({});if(!saveOperation(operation,stage))fail('无法在已披露的获取目录保存操作记录;尚未联网或安装');}
|
|
68
|
+
if(args.prepare)console.log('正在准备 Foundation 安装:将查询并固定本次正式版本,说明缓存位置后下载核验。未选目录会在安装页选择;之后仍需本人确认精确计划。请保持当前 Codex 任务等待并打开返回的内置浏览器网址;不会自动改用系统浏览器。');
|
|
53
69
|
const context=inspectRelease(args.version);
|
|
54
70
|
const guide=`https://github.com/${context.repository.full_name}/blob/${context.documentationCommit}/docs/install-with-codex.md`;
|
|
55
71
|
if(!args.prepare&&!args.acquire){
|
|
56
72
|
console.log(JSON.stringify({schemaVersion:'1.0.0',status:'RELEASE_DISCOVERED_NOT_ACQUIRED',product:'Foundation',version:context.version,sourceCommit:context.sourceCommit,documentationCommit:context.documentationCommit,repository:context.repository.full_name,guide,installationPerformed:false,skillRegistered:false,
|
|
57
|
-
conversationNextStep:'
|
|
73
|
+
conversationNextStep:'本次 --inspect 仅查询;未下载、未安装,不自动接续写入。需要安装时由用户发起默认安装入口,并核对当前任务权限。',
|
|
58
74
|
terminalBoundary:'普通终端无法创建或唤醒 Codex 对话;请在有相应权限的 Codex 任务发起安装。',
|
|
59
|
-
acquisition:'
|
|
75
|
+
acquisition:'尚未下载或验证运行归档。默认入口或 --prepare 执行匿名 GitHub 证明与完整性核验。'},null,2));
|
|
60
76
|
}else{
|
|
61
|
-
|
|
62
|
-
const receipt=await acquireRelease(context,stage);console.log(JSON.stringify({status:'GITHUB_ACQUISITION_VERIFIED',...receipt,destinationIntent:args.destination}));
|
|
63
|
-
|
|
77
|
+
if(args.destination)plainPath(args.destination);console.log(`本次独占获取目录:${stage}`);
|
|
78
|
+
const receipt=await acquireRelease(context,stage,{operationId:operation.operationId,onProgress:download=>updateOperation({download}),onPhase:phase=>updateOperation({phase,version:context.version,sourceCommit:context.sourceCommit})});console.log(JSON.stringify({status:'GITHUB_ACQUISITION_VERIFIED',...receipt,destinationIntent:args.destination}));
|
|
79
|
+
updateOperation({phase:'acquired',version:context.version,sourceCommit:context.sourceCommit});
|
|
80
|
+
if(args.acquire){updateOperation({state:'update-input-ready',terminal:true});console.log(JSON.stringify({status:'UPDATE_INPUT_READY_NOT_APPLIED',candidate:path.dirname(receipt.launcher),receipt:path.join(stage,'acquisition.json'),next:'使用当前健康安装的稳定入口生成独立更新计划;本人确认后才更新。获取回执不是删除授权。'}));process.exit(0);}
|
|
64
81
|
const env={...process.env};for(const key of ['NODE_OPTIONS','NODE_PATH','NODE_V8_COVERAGE','NODE_REDIRECT_WARNINGS','NODE_COMPILE_CACHE','NODE_COMPILE_CACHE_PORTABLE','NODE_PRESERVE_SYMLINKS'])delete env[key];
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
82
|
+
if(!args.destination){
|
|
83
|
+
const supported=spawnSync(receipt.launcher,['--help'],{encoding:'utf8',env,timeout:15000});
|
|
84
|
+
if(supported.status!==0||!supported.stdout.includes('--choose-destination'))fail('本次发行尚不支持页面选择目录;未启动安装。请由当前对话取得目录后使用明确的 --destination,或等待新版发行,不静默采用默认位置');
|
|
85
|
+
}
|
|
86
|
+
updateOperation({phase:'starting-confirmation-service',installationWrites:'unknown'});
|
|
87
|
+
const child=spawn(receipt.launcher,['install',...(args.destination?['--destination',args.destination]:['--choose-destination']),'--browser','codex'],{stdio:['inherit','pipe','inherit'],env});
|
|
88
|
+
child.stdout.setEncoding('utf8');
|
|
89
|
+
let pending='',json='';
|
|
90
|
+
child.stdout.on('data',bytes=>{process.stdout.write(bytes);pending+=bytes.toString();if(pending.length>1_000_000){pending='';json='';return;}let end;while((end=pending.indexOf('\n'))>=0){const line=pending.slice(0,end);pending=pending.slice(end+1);if(!json&&!line.trim().startsWith('{'))continue;json+=line+'\n';if(json.length>1_000_000){json='';continue;}let e;try{e=JSON.parse(json)}catch{continue}json='';
|
|
91
|
+
const observed=runtimeObservation(e);if(observed)updateOperation(observed);
|
|
92
|
+
}});
|
|
93
|
+
const interrupt=signal=>{child.kill(signal);};
|
|
94
|
+
const int=()=>interrupt('SIGINT'),term=()=>interrupt('SIGTERM');
|
|
95
|
+
process.once('SIGINT',int);process.once('SIGTERM',term);
|
|
96
|
+
const ended=await new Promise((resolve,reject)=>{child.once('error',reject);child.once('close',(code,signal)=>resolve({code,signal}));}).finally(()=>{process.off('SIGINT',int);process.off('SIGTERM',term);});
|
|
97
|
+
if(ended.signal||ended.code===null)fail(`确认服务未正常返回;结果待核实,保留 ${stage},不要自动重试安装`);
|
|
98
|
+
if(!operation.terminal)updateOperation({state:'verification-required',terminal:true,childExitCode:ended.code,next:'服务退出不等于成功或取消;只读核验原 session 与 installed 状态,不自动重试'});
|
|
99
|
+
if(operation.state==='completed'&&operation.installationRoot){
|
|
100
|
+
const workbench=await openInstalledWorkbench(operation.installationRoot,env);
|
|
101
|
+
updateOperation({phase:'finished',workbench,next:workbench.state==='ready'?'在 Codex 内置浏览器打开已就绪工作台;Skill 与项目仍未接入,按用户偏好另行询问':'安装已完成,但工作台未就绪;保留安装结果并只读检查'});
|
|
102
|
+
}
|
|
103
|
+
process.exitCode=ended.code;
|
|
68
104
|
}
|
|
69
|
-
}catch(e){console.error(`错误:${
|
|
105
|
+
}catch(e){updateOperation({state:operation&&operation.installationWrites!=='none'?'verification-required':'failed',terminal:true,errorCode:['ACQUISITION_TRANSPORT_FAILED','ACQUISITION_VALIDATION_FAILED'].includes(e.code)?e.code:'INSTALL_ENTRY_FAILED',diagnostic:e.diagnostic||null,next:'先核验旧工具进程已结束及同次结果;保留材料,不自动重试或重放安装确认'});const message=String(e.message).replace(/https?:\/\/\S+/g,'[已脱敏网址]').replace(/(?:github_pat_|ghp_|npm_)[A-Za-z0-9_]+/g,'[已脱敏凭据]');console.error(`错误:${message}`);process.exitCode=1;}
|
package/lib/acquire.mjs
CHANGED
|
@@ -6,26 +6,54 @@ import {Updater} from 'tuf-js';
|
|
|
6
6
|
import snappy from 'snappyjs';
|
|
7
7
|
import {verifyReleaseProof} from './release-proof.mjs';
|
|
8
8
|
import policy from './public-policy.json' with {type:'json'};
|
|
9
|
+
import {readOperation} from './operation-result.mjs';
|
|
9
10
|
|
|
10
11
|
const hash = bytes => crypto.createHash('sha256').update(bytes).digest('hex');
|
|
11
|
-
const fail = message => { throw new Error(`错误:${message}`); };
|
|
12
|
+
const fail = message => { throw Object.assign(new Error(`错误:${message}`),{code:'ACQUISITION_VALIDATION_FAILED'}); };
|
|
12
13
|
const semver = /^\d+\.\d+\.\d+$/;
|
|
13
14
|
const json = bytes => JSON.parse(bytes.toString('utf8'));
|
|
14
15
|
const canonical = value => Array.isArray(value) ? `[${value.map(canonical).join(',')}]` : value && typeof value==='object' ? `{${Object.keys(value).sort().map(k=>`${JSON.stringify(k)}:${canonical(value[k])}`).join(',')}}` : JSON.stringify(value);
|
|
16
|
+
export function downloadFailure(result,hostname) {
|
|
17
|
+
const code=Number.isInteger(result.status)?result.status:null;
|
|
18
|
+
const category=({5:'proxy-dns',6:'dns',7:'connection',22:'http',28:'timeout-or-low-speed',35:'tls',60:'tls-certificate'})[code] || (result.signal?'terminated':'transport');
|
|
19
|
+
// Do not retain stderr: redirects, proxy credentials and signed query URLs may occur there.
|
|
20
|
+
const error=Error(`匿名获取失败(${hostname});类别 ${category},curl 退出码 ${code ?? '未知'};未执行安装`);
|
|
21
|
+
error.code='ACQUISITION_TRANSPORT_FAILED';error.diagnostic={category,curlExitCode:code,signal:['SIGINT','SIGTERM','SIGKILL'].includes(result.signal)?result.signal:null,host:hostname};return error;
|
|
22
|
+
}
|
|
15
23
|
// Public transport never reads gh credentials or curl configuration. Proxy
|
|
16
24
|
// environment remains optional; neither Mono nor an author path is packaged.
|
|
17
|
-
function download(url, limit=10_000_000
|
|
25
|
+
function download(url, limit=10_000_000) {
|
|
18
26
|
const u = new URL(url);
|
|
19
27
|
if (u.protocol !== 'https:' || u.username || u.password) fail('只接受无凭证 HTTPS 地址');
|
|
20
28
|
const env = {PATH:'/usr/bin:/bin'};
|
|
21
29
|
for (const key of ['HTTPS_PROXY','HTTP_PROXY','ALL_PROXY','NO_PROXY','https_proxy','http_proxy','all_proxy','no_proxy','TMPDIR']) if (process.env[key]) env[key]=process.env[key];
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
const r=spawnSync('/usr/bin/curl',['-q',...(largeAsset?['--http1.1','--speed-limit','1024','--speed-time','60']:[]),'--fail','--silent','--show-error','--location','--max-redirs','4','--proto','=https','--proto-redir','=https','--connect-timeout','15','--max-time',largeAsset?'1200':'120',url],{env,maxBuffer:limit});
|
|
25
|
-
if(r.status!==0 || r.error) fail(`匿名获取失败(${u.hostname});保留本次材料,不自动重试安装`);
|
|
30
|
+
const r=spawnSync('/usr/bin/curl',['-q','--fail','--silent','--show-error','--location','--max-redirs','4','--proto','=https','--proto-redir','=https','--connect-timeout','15','--max-time','120',url],{env,maxBuffer:limit});
|
|
31
|
+
if(r.status!==0 || r.error) throw downloadFailure(r,u.hostname);
|
|
26
32
|
return r.stdout;
|
|
27
33
|
}
|
|
28
34
|
function api(endpoint) { return json(download(`https://api.github.com/repos/${policy.repository}/${endpoint}`)); }
|
|
35
|
+
async function downloadLarge(url,total,onProgress) {
|
|
36
|
+
const {spawn}=await import('node:child_process');
|
|
37
|
+
const env={PATH:'/usr/bin:/bin'};for(const k of ['HTTPS_PROXY','HTTP_PROXY','ALL_PROXY','NO_PROXY','https_proxy','http_proxy','all_proxy','no_proxy','TMPDIR'])if(process.env[k])env[k]=process.env[k];
|
|
38
|
+
// Retry acquisition only, never an installation. Each attempt has independent
|
|
39
|
+
// byte accounting and a hard bound; incomplete bytes are never executed.
|
|
40
|
+
for(let attempt=1;attempt<=2;attempt++){
|
|
41
|
+
let bytes=0,chunks=[],last=0,overflow=false;
|
|
42
|
+
onProgress({attempt,downloadedBytes:0,totalBytes:total,lastProgressAt:null});
|
|
43
|
+
const child=spawn('/usr/bin/curl',['-q','--http1.1','--speed-limit','1024','--speed-time','60','--fail','--silent','--show-error','--location','--max-redirs','4','--proto','=https','--proto-redir','=https','--connect-timeout','15','--max-time','600',url],{env,stdio:['ignore','pipe','pipe']});
|
|
44
|
+
const stop=signal=>child.kill(signal),int=()=>stop('SIGINT'),term=()=>stop('SIGTERM');process.once('SIGINT',int);process.once('SIGTERM',term);
|
|
45
|
+
let interrupted=null;const markInt=()=>interrupted='SIGINT',markTerm=()=>interrupted='SIGTERM';process.once('SIGINT',markInt);process.once('SIGTERM',markTerm);
|
|
46
|
+
child.stderr.resume();
|
|
47
|
+
child.stdout.on('data',b=>{bytes+=b.length;if(bytes>total){overflow=true;child.kill('SIGTERM');return;}chunks.push(b);const now=Date.now();if(now-last>=1000){last=now;onProgress({attempt,downloadedBytes:bytes,totalBytes:total,lastProgressAt:new Date(now).toISOString()});}});
|
|
48
|
+
const ended=await new Promise(resolve=>{child.once('error',error=>resolve({status:null,error}));child.once('close',(status,signal)=>resolve({status,signal}));});
|
|
49
|
+
process.off('SIGINT',int);process.off('SIGTERM',term);process.off('SIGINT',markInt);process.off('SIGTERM',markTerm);
|
|
50
|
+
onProgress({attempt,downloadedBytes:bytes,totalBytes:total,lastProgressAt:last?new Date(last).toISOString():null});
|
|
51
|
+
if(overflow)fail('下载超出已绑定资产长度;拒绝执行');
|
|
52
|
+
if(ended.status===0&&!interrupted)return Buffer.concat(chunks);
|
|
53
|
+
const error=downloadFailure({...ended,signal:interrupted||ended.signal},new URL(url).hostname);
|
|
54
|
+
if(attempt===2||interrupted||![5,6,7,28,52,56].includes(ended.status))throw error;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
29
57
|
function regular(file) { const s=fs.lstatSync(file); if(!s.isFile()||s.isSymbolicLink())fail('不是普通文件');return s; }
|
|
30
58
|
export function plainPath(file) {
|
|
31
59
|
if(!path.isAbsolute(file)||path.normalize(file)!==file)fail('路径必须是规范化绝对路径');
|
|
@@ -63,9 +91,9 @@ async function trustRoot(stage) {
|
|
|
63
91
|
await updater.refresh();const info=await updater.getTargetInfo('trusted_root.json');if(!info)fail('GitHub 信任目标缺失');
|
|
64
92
|
return json(fs.readFileSync(await updater.downloadTarget(info)));
|
|
65
93
|
}
|
|
66
|
-
async function verifiedAsset(context,asset,stage,trust) {
|
|
94
|
+
async function verifiedAsset(context,asset,stage,trust,onProgress) {
|
|
67
95
|
if(!asset||!Number.isSafeInteger(asset.size)||asset.size<=0||asset.size>250_000_000||!/^[A-Za-z0-9][A-Za-z0-9._-]*$/.test(asset.name)||!/^sha256:[a-f0-9]{64}$/.test(asset.digest)||asset.browser_download_url!==`https://github.com/${policy.repository}/releases/download/${context.release.tag_name}/${asset.name}`)fail('资产元数据无效');
|
|
68
|
-
const bytes=
|
|
96
|
+
const bytes=asset.size>10_000_000?await downloadLarge(asset.browser_download_url,asset.size,onProgress):download(asset.browser_download_url,asset.size+1);
|
|
69
97
|
if(bytes.length!==asset.size||`sha256:${hash(bytes)}`!==asset.digest)fail('下载长度或摘要不匹配');
|
|
70
98
|
const proofs=api(`attestations/${asset.digest}?predicate_type=release&per_page=100`).attestations;
|
|
71
99
|
if(!Array.isArray(proofs)||proofs.length>=100)fail('证明列表不完整或分页待核实');
|
|
@@ -107,20 +135,25 @@ function validateCandidate(root,release,sourceCommit) {
|
|
|
107
135
|
// stage is always created by the CLI from the OS account in production.
|
|
108
136
|
// Keeping this module callable allows contained engineering validation without
|
|
109
137
|
// adding an install/cache override to the user-facing executable.
|
|
110
|
-
export async function acquireRelease(context,stage) {
|
|
111
|
-
plainPath(stage);const s=fs.lstatSync(stage);if(!s.isDirectory()||(s.mode&0o777)!==0o700||s.uid!==process.getuid()
|
|
138
|
+
export async function acquireRelease(context,stage,{onPhase=()=>{},onProgress=()=>{},operationId=null}={}) {
|
|
139
|
+
plainPath(stage);const s=fs.lstatSync(stage),entries=fs.readdirSync(stage);if(!s.isDirectory()||(s.mode&0o777)!==0o700||s.uid!==process.getuid())fail('获取目录必须是当前用户的新建独占空目录');
|
|
140
|
+
if(entries.length){if(entries.length!==1||entries[0]!=='operation-result.json'||!operationId)fail('获取目录包含未知内容');const r=readOperation(path.join(stage,entries[0]));if(r.operationId!==operationId||r.terminal||r.phase!=='discovering')fail('获取目录不是本次早期记录');}
|
|
112
141
|
const beforeTmp=process.env.TMPDIR;process.env.TMPDIR=stage;
|
|
113
142
|
try{
|
|
143
|
+
onPhase('verifying-trust');
|
|
114
144
|
const trust=await trustRoot(stage);
|
|
115
145
|
const name=`foundation-release-${context.version}-macos-arm64.json`;
|
|
116
146
|
const catalogs=context.release.assets.filter(x=>x.name===name);if(catalogs.length!==1)fail('发行清单缺失或重复');
|
|
147
|
+
onPhase('fetching-and-verifying-catalog');
|
|
117
148
|
const catalog=json((await verifiedAsset(context,catalogs[0],stage,trust)).bytes);
|
|
118
149
|
const releases=catalog.releases?.filter(x=>x.version===context.version&&x.platform==='darwin'&&x.arch==='arm64');
|
|
119
150
|
if(catalog.schemaVersion!=='1.0.0'||catalog.product!=='ai-product-foundation-kit'||catalog.keyId!==null||!Number.isSafeInteger(catalog.issuedAt)||catalog.issuedAt>Date.now()||!Number.isSafeInteger(catalog.expiresAt)||catalog.expiresAt<=Date.now()||catalog.expiresAt<=catalog.issuedAt||releases?.length!==1)fail('发行清单身份、有效期或架构不匹配');
|
|
120
151
|
const release=releases[0];const assetName=`foundation-${release.sha256}.tar.gz`;
|
|
121
152
|
if(release.repositoryId!==policy.repositoryId||release.sourceCommit!==context.sourceCommit||!/^[a-f0-9]{64}$/.test(release.candidateHash)||release.url!==`https://github.com/${policy.repository}/releases/download/${context.release.tag_name}/${assetName}`||!/^\d+\.\d+\.\d+$/.test(release.runtime?.version)||release.runtime.url!==`https://nodejs.org/dist/v${release.runtime.version}/node-v${release.runtime.version}-darwin-arm64.tar.gz`)fail('清单不能更换仓库、提交或官方运行时来源');
|
|
122
153
|
const assets=context.release.assets.filter(x=>x.name===assetName&&x.size===release.bytes&&x.digest===`sha256:${release.sha256}`);if(assets.length!==1)fail('归档身份不匹配');
|
|
123
|
-
|
|
154
|
+
onPhase('fetching-and-verifying-runtime');
|
|
155
|
+
const archive=await verifiedAsset(context,assets[0],stage,trust,onProgress);
|
|
156
|
+
onPhase('extracting-and-validating');
|
|
124
157
|
const listing=spawnSync('/usr/bin/tar',['-tzf',archive.file],{encoding:'utf8',maxBuffer:10_000_000});
|
|
125
158
|
const types=spawnSync('/usr/bin/tar',['-tvzf',archive.file],{encoding:'utf8',maxBuffer:10_000_000});
|
|
126
159
|
if(listing.status!==0||types.status!==0||listing.stdout.trim().split('\n').some(p=>p.startsWith('/')||p.includes('\\')||p.split('/').includes('..'))||types.stdout.trim().split('\n').some(p=>!['d','-'].includes(p[0])||/[sStT]/.test(p.slice(1,10))))fail('归档包含不安全路径、链接或特殊权限');
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
function fail(message){throw Error(message);}
|
|
2
|
+
export function parseSummonArgs(args){
|
|
3
|
+
args=[...args];
|
|
4
|
+
if(args.length===0||args.length===1&&['--help','-h'].includes(args[0]))return {help:true};
|
|
5
|
+
if(args.shift()!=='foundation')fail('仅支持 summon foundation');
|
|
6
|
+
const result={prepare:false};const seen=new Set();
|
|
7
|
+
while(args.length){const flag=args.shift();if(seen.has(flag))fail('重复参数');seen.add(flag);
|
|
8
|
+
if(flag==='--prepare')result.prepare=true;
|
|
9
|
+
else if(flag==='--acquire')result.acquire=true;
|
|
10
|
+
else if(flag==='--inspect')result.inspect=true;
|
|
11
|
+
else if(flag==='--version'||flag==='--destination'||flag==='--status'){if(!args.length||args[0].startsWith('--'))fail('参数缺值');result[flag.slice(2)]=args.shift();}
|
|
12
|
+
else fail('未知参数;请查看 --help');
|
|
13
|
+
}
|
|
14
|
+
if((result.prepare||result.acquire||result.destination)&&result.inspect||result.prepare&&result.acquire||result.destination&&result.acquire)fail('参数组合不支持');
|
|
15
|
+
if(seen.has('--status')){if(!result.status||seen.size!==1)fail('结果查询需要非空路径且不能与其他操作组合');return result;}
|
|
16
|
+
if(!result.inspect&&!result.acquire)result.prepare=true;
|
|
17
|
+
if(result.acquire&&!result.version)fail('获取更新材料前须固定版本');
|
|
18
|
+
return result;
|
|
19
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import crypto from 'node:crypto';
|
|
4
|
+
|
|
5
|
+
export function createOperation() {
|
|
6
|
+
return {schemaVersion:'1.0.0',operationId:crypto.randomUUID(),pid:process.pid,startedAt:new Date().toISOString(),phase:'discovering',state:'running',terminal:false,installationWrites:'none',skillRegistered:false};
|
|
7
|
+
}
|
|
8
|
+
// Observations from the verified runtime, never confirmation or execution authority.
|
|
9
|
+
export function runtimeObservation(event) {
|
|
10
|
+
if(event.status==='AWAITING_FOUNDATION_DIRECTORY_SELECTION')return {phase:'selecting-directory',selectionId:event.selectionId,installationWrites:'none'};
|
|
11
|
+
if(['FOUNDATION_SELECTION_BOUND','AWAITING_FOUNDATION_UI_CONFIRMATION'].includes(event.status))return {phase:'awaiting-confirmation',sessionId:event.sessionId,planHash:event.planHash,installationWrites:'none'};
|
|
12
|
+
if(event.status==='FOUNDATION_OPERATION_STATE'&&['executing','consumed'].includes(event.state))return {phase:'executing',sessionId:event.sessionId,installationWrites:'possible'};
|
|
13
|
+
if(event.status==='BOOTSTRAP_OPERATION_ENDED'){
|
|
14
|
+
const state=['completed','failed','cancelled','expired'].includes(event.state)?event.state:'verification-required';
|
|
15
|
+
return {phase:'runtime-ended',state,terminal:state!=='verification-required',sessionId:event.sessionId,installationRoot:event.installationRoot,installationWrites:state==='completed'?'runtime-reported-installed':'unknown',runtimeHealth:event.result?.executableHealth||'unknown',next:'只读核验稳定 installed launcher 或同次 session 结果,不重放确认'};
|
|
16
|
+
}
|
|
17
|
+
if(['cancelled-no-install','expired-no-install','shutdown-no-install'].includes(event.state))return {phase:'directory-selection-ended',state:event.state,terminal:true,installationWrites:'none'};
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
export function saveOperation(record,stage) {
|
|
21
|
+
if(!stage)return null;
|
|
22
|
+
try{
|
|
23
|
+
for(let p=stage;p!==path.dirname(p);p=path.dirname(p)){if(fs.lstatSync(p).isSymbolicLink())throw Error('symlink');}
|
|
24
|
+
const s=fs.lstatSync(stage);if(!s.isDirectory()||s.uid!==process.getuid()||(s.mode&511)!==448)throw Error('private stage required');
|
|
25
|
+
const file=path.join(stage,'operation-result.json');
|
|
26
|
+
if(fs.existsSync(file)){const old=fs.lstatSync(file);if(!old.isFile()||old.isSymbolicLink()||old.nlink!==1||old.uid!==process.getuid()||(old.mode&511)!==384)throw Error('unknown record');const previous=JSON.parse(fs.readFileSync(file));if(previous.operationId!==record.operationId)throw Error('different operation');}
|
|
27
|
+
const tmp=path.join(stage,`.operation-${crypto.randomUUID()}.json`);fs.writeFileSync(tmp,JSON.stringify(record,null,2)+'\n',{flag:'wx',mode:0o600});fs.renameSync(tmp,file);return file;
|
|
28
|
+
}catch{return null;}
|
|
29
|
+
}
|
|
30
|
+
export function readOperation(file) {
|
|
31
|
+
if(!path.isAbsolute(file)||path.normalize(file)!==file)throw Error('结果路径必须为规范化绝对路径');
|
|
32
|
+
for(let p=file;p!==path.dirname(p);p=path.dirname(p)){if(fs.lstatSync(p).isSymbolicLink())throw Error('拒绝链接结果路径');}
|
|
33
|
+
const s=fs.lstatSync(file);if(!s.isFile()||s.size>1_000_000||s.uid!==process.getuid()||(s.mode&511)!==384)throw Error('结果文件类型、大小或归属无效');
|
|
34
|
+
const r=JSON.parse(fs.readFileSync(file));if(r.schemaVersion!=='1.0.0'||typeof r.operationId!=='string'||typeof r.terminal!=='boolean')throw Error('结果记录无效');
|
|
35
|
+
return {...r,...(!r.terminal?{state:'verification-required',note:'这是最后一次记录,不证明原进程仍活着。先查原工具句柄和同次记录;不自动重试。'}:{}),readOnly:true,executionAuthority:false};
|
|
36
|
+
}
|
package/npm-shrinkwrap.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@josephyulei/summon-foundation",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"lockfileVersion": 3,
|
|
5
5
|
"requires": true,
|
|
6
6
|
"packages": {
|
|
7
7
|
"": {
|
|
8
8
|
"name": "@josephyulei/summon-foundation",
|
|
9
|
-
"version": "0.1.
|
|
9
|
+
"version": "0.1.3",
|
|
10
10
|
"license": "MIT",
|
|
11
11
|
"dependencies": {
|
|
12
12
|
"@sigstore/bundle": "4.0.0",
|
package/package.json
CHANGED