11import type { ExtensionAPI } from "@earendil-works/pi-coding-agent" ;
22import { StringEnum } from "@earendil-works/pi-ai" ;
33import { Type , type Static } from "typebox" ;
4- import { existsSync } from "node:fs" ;
5- import { dirname , isAbsolute , resolve } from "node:path" ;
4+ import { chmodSync , constants , copyFileSync , existsSync , lstatSync , mkdtempSync , realpathSync , renameSync , rmSync } from "node:fs" ;
5+ import { tmpdir } from "node:os" ;
6+ import { basename , dirname , isAbsolute , join , relative , resolve , sep } from "node:path" ;
67import { fileURLToPath } from "node:url" ;
78
89const scanSchema = Type . Object ( {
@@ -12,7 +13,7 @@ const scanSchema = Type.Object({
1213 description : "SkillSpector output format. Defaults to terminal." ,
1314 } ) ,
1415 ) ,
15- output : Type . Optional ( Type . String ( { description : "Optional report output path." } ) ) ,
16+ output : Type . Optional ( Type . String ( { description : "Optional report output path within the current workspace ." } ) ) ,
1617 noLlm : Type . Optional ( Type . Boolean ( { description : "Skip LLM analysis. Defaults to true." } ) ) ,
1718 provider : Type . Optional (
1819 StringEnum ( [ "openai" , "anthropic" , "anthropic_proxy" , "nv_build" , "nv_inference" ] as const , {
@@ -57,20 +58,57 @@ function packageRoot(): string {
5758}
5859
5960function findSkillSpectorBin ( ) : string {
60- if ( process . env . SKILLSPECTOR_BIN ) return process . env . SKILLSPECTOR_BIN ;
61- const localBin = resolve ( packageRoot ( ) , ".venv/bin/skillspector" ) ;
62- if ( existsSync ( localBin ) ) return localBin ;
63- return "skillspector" ;
61+ const bundledBin = process . platform === "win32" ? ".venv/Scripts/skillspector.exe" : ".venv/bin/skillspector" ;
62+ const bin = process . env . SKILLSPECTOR_BIN ?? resolve ( packageRoot ( ) , bundledBin ) ;
63+ if ( ! isAbsolute ( bin ) || ! existsSync ( bin ) ) {
64+ throw new Error ( "Install SkillSpector in the extension's .venv or set SKILLSPECTOR_BIN to an absolute executable path." ) ;
65+ }
66+ return bin ;
6467}
6568
66- function buildScanArgs ( params : SkillSpectorScanParams , cwd : string ) : string [ ] {
69+ function isWithin ( root : string , path : string ) : boolean {
70+ const rel = relative ( root , path ) ;
71+ return rel !== ".." && ! rel . startsWith ( `..${ sep } ` ) && ! isAbsolute ( rel ) ;
72+ }
73+
74+ function reportOutputPath ( cwd : string , value ?: string ) : string | undefined {
75+ if ( ! value ) return undefined ;
76+ const output = resolve ( cwd , value ) ;
77+ if ( output === resolve ( cwd ) || ! isWithin ( resolve ( cwd ) , output ) ) {
78+ throw new Error ( "Report output must be a file within the current workspace." ) ;
79+ }
80+ const parent = realpathSync ( dirname ( output ) ) ;
81+ if ( ! isWithin ( realpathSync ( cwd ) , parent ) ) {
82+ throw new Error ( "Report output must be a file within the current workspace." ) ;
83+ }
84+ const destination = join ( parent , basename ( output ) ) ;
85+ const existing = lstatSync ( destination , { throwIfNoEntry : false } ) ;
86+ if ( existing && ! existing . isFile ( ) ) {
87+ throw new Error ( "Report output must be a regular file, not a symlink or directory." ) ;
88+ }
89+ return destination ;
90+ }
91+
92+ function publishReport ( source : string , destination : string ) : void {
93+ const staging = mkdtempSync ( join ( dirname ( destination ) , ".skillspector-report-" ) ) ;
94+ try {
95+ const report = join ( staging , "report" ) ;
96+ copyFileSync ( source , report , constants . COPYFILE_EXCL ) ;
97+ chmodSync ( report , 0o600 ) ;
98+ // Replacing the directory entry never follows an existing hard link.
99+ renameSync ( report , destination ) ;
100+ } finally {
101+ rmSync ( staging , { recursive : true , force : true } ) ;
102+ }
103+ }
104+
105+ function buildScanArgs ( params : SkillSpectorScanParams , cwd : string , output ?: string ) : string [ ] {
67106 const args = [ "scan" , resolveMaybePath ( cwd , params . target ) ?? params . target ] ;
68107 args . push ( "--format" , params . format ?? "terminal" ) ;
69108
70109 const noLlm = params . noLlm ?? true ;
71110 if ( noLlm ) args . push ( "--no-llm" ) ;
72111
73- const output = resolveMaybePath ( cwd , params . output ) ;
74112 if ( output ) args . push ( "--output" , output ) ;
75113
76114 const yaraRulesDir = resolveMaybePath ( cwd , params . yaraRulesDir ) ;
@@ -93,50 +131,65 @@ export default function (pi: ExtensionAPI) {
93131 parameters : scanSchema ,
94132 async execute ( _toolCallId , params , signal , onUpdate , ctx ) {
95133 const bin = findSkillSpectorBin ( ) ;
96- const args = buildScanArgs ( params , ctx . cwd ) ;
97- const env : Record < string , string > = { } ;
98-
99- if ( params . provider ) env . SKILLSPECTOR_PROVIDER = params . provider ;
100- if ( params . model ) env . SKILLSPECTOR_MODEL = params . model ;
101-
102- onUpdate ?.( { content : [ { type : "text" , text : `Running ${ bin } ${ args . slice ( 0 , 2 ) . join ( " " ) } ...` } ] } ) ;
103-
104- const result = await pi . exec ( bin , args , {
105- cwd : ctx . cwd ,
106- env,
107- signal,
108- timeout : 120000 ,
109- } ) ;
110-
111- const stdout = truncateText ( redactSecrets ( result . stdout ?? "" ) ) ;
112- const stderr = truncateText ( redactSecrets ( result . stderr ?? "" ) , 6000 ) ;
113-
114- if ( result . code !== 0 ) {
115- throw new Error ( `SkillSpector failed with exit code ${ result . code } .\n${ stderr . text } ` ) ;
134+ const outputPath = reportOutputPath ( ctx . cwd , params . output ) ;
135+ const reportDir = outputPath ? mkdtempSync ( join ( tmpdir ( ) , "skillspector-report-" ) ) : undefined ;
136+ const reportPath = reportDir ? join ( reportDir , "report" ) : undefined ;
137+ try {
138+ const args = buildScanArgs ( params , ctx . cwd , reportPath ) ;
139+ const env : Record < string , string > = { } ;
140+
141+ if ( params . provider ) env . SKILLSPECTOR_PROVIDER = params . provider ;
142+ if ( params . model ) env . SKILLSPECTOR_MODEL = params . model ;
143+
144+ onUpdate ?.( { content : [ { type : "text" , text : `Running ${ bin } ${ args . slice ( 0 , 2 ) . join ( " " ) } ...` } ] } ) ;
145+
146+ const result = await pi . exec ( bin , args , {
147+ cwd : ctx . cwd ,
148+ env,
149+ signal,
150+ timeout : 120000 ,
151+ } ) ;
152+
153+ const stdout = truncateText ( redactSecrets ( result . stdout ?? "" ) ) ;
154+ const stderr = truncateText ( redactSecrets ( result . stderr ?? "" ) , 6000 ) ;
155+
156+ // Exit 2 can follow a diagnostic report; failures before reporting produce no file.
157+ const failureReport = result . code === 2 && reportPath
158+ ? lstatSync ( reportPath , { throwIfNoEntry : false } ) : undefined ;
159+ const hasFailureReport = failureReport ?. isFile ( ) && failureReport . size > 0 ;
160+ if ( ( result . code === 0 || result . code === 1 || hasFailureReport ) && outputPath && reportPath ) {
161+ if ( reportOutputPath ( ctx . cwd , params . output ) !== outputPath ) {
162+ throw new Error ( "Report output directory changed during the scan." ) ;
163+ }
164+ publishReport ( reportPath , outputPath ) ;
165+ }
166+ if ( result . code !== 0 ) {
167+ throw new Error ( `SkillSpector failed with exit code ${ result . code } .\n${ stderr . text } ` ) ;
168+ }
169+ const lines = [
170+ `SkillSpector scan complete: ${ params . target } ` ,
171+ `format: ${ params . format ?? "terminal" } ` ,
172+ `noLlm: ${ params . noLlm ?? true } ` ,
173+ ] ;
174+ if ( outputPath ) lines . push ( `output: ${ outputPath } ` ) ;
175+ if ( stdout . truncated ) lines . push ( "stdout truncated: true" ) ;
176+ if ( stderr . text . trim ( ) ) lines . push ( `stderr:\n${ stderr . text } ` ) ;
177+ if ( stdout . text . trim ( ) ) lines . push ( `stdout:\n${ stdout . text } ` ) ;
178+
179+ return {
180+ content : [ { type : "text" , text : lines . join ( "\n" ) } ] ,
181+ details : {
182+ code : result . code ,
183+ command : bin ,
184+ args,
185+ outputPath,
186+ stdoutTruncated : stdout . truncated ,
187+ stderrTruncated : stderr . truncated ,
188+ } ,
189+ } ;
190+ } finally {
191+ if ( reportDir ) rmSync ( reportDir , { recursive : true , force : true } ) ;
116192 }
117-
118- const outputPath = resolveMaybePath ( ctx . cwd , params . output ) ;
119- const lines = [
120- `SkillSpector scan complete: ${ params . target } ` ,
121- `format: ${ params . format ?? "terminal" } ` ,
122- `noLlm: ${ params . noLlm ?? true } ` ,
123- ] ;
124- if ( outputPath ) lines . push ( `output: ${ outputPath } ` ) ;
125- if ( stdout . truncated ) lines . push ( "stdout truncated: true" ) ;
126- if ( stderr . text . trim ( ) ) lines . push ( `stderr:\n${ stderr . text } ` ) ;
127- if ( stdout . text . trim ( ) ) lines . push ( `stdout:\n${ stdout . text } ` ) ;
128-
129- return {
130- content : [ { type : "text" , text : lines . join ( "\n" ) } ] ,
131- details : {
132- code : result . code ,
133- command : bin ,
134- args,
135- outputPath,
136- stdoutTruncated : stdout . truncated ,
137- stderrTruncated : stderr . truncated ,
138- } ,
139- } ;
140193 } ,
141194 } ) ;
142195}
0 commit comments