icoa-cli 2.19.390 → 2.19.391

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.
@@ -1 +1 @@
1
- import{existsSync as e,mkdirSync as o,readFileSync as n,writeFileSync as l}from"node:fs";import{homedir as t}from"node:os";import{Interface as r,createInterface as a}from"node:readline";import{basename as s,isAbsolute as c,join as i,resolve as y}from"node:path";import chalk from"chalk";import{aienvPaths as g}from"../lib/aienv.js";import{editTextInEditor as d}from"../lib/editor-spawn.js";import{buildSshArgs as u,leaseConnectProblems as p,parseLeaseResponse as h,queueLine as m}from"../lib/gpu.js";import{shouldExecuteCell as w}from"../lib/ipynb-input.js";import{looksLikeMarkdown as f,renderMarkdown as v}from"../lib/md-term.js";import{NotebookKernel as k,buildRemoteBridgeCommand as b}from"../lib/kernel.js";import{getMainRl as $}from"../lib/main-rl.js";import{cellPreview as x,parseIpynb as P,parsePercentPy as E,parseRunRange as G,serializeIpynb as C,statusChar as S}from"../lib/notebook-doc.js";import{openFile as U}from"../lib/open-file.js";const N=new RegExp(`${String.fromCharCode(27)}\\[[0-9;]*m`,"g");export function registerIpynbCommand(h){h.command("ipynb").argument("[file]","notebook (.ipynb) or script (.py, split on # %%) to open").description("Open an in-terminal Python notebook (AI/ML — needs `aienv setup`)").action(async h=>{await async function(h){const N=g(t()),L=i(t(),".icoa","ipynb-out");if(A(),!e(N.python))return console.log(chalk.yellow(" The AI/ML environment is not set up yet.")),console.log(chalk.gray(" Run ")+chalk.bold.cyan("aienv setup")+chalk.gray(" first (one-time, ~300 MB).")),void console.log();const K=h?function(o){const l=c(o)?o:y(process.cwd(),o);if(!e(l))return{doc:{cells:[],path:l,dirty:!1},note:chalk.gray(" new notebook — will be created on ")+chalk.cyan("save")};try{const e=n(l,"utf8"),o=l.endsWith(".ipynb")?P(e):E(e);return{doc:{cells:o,path:l,dirty:!1},note:chalk.gray(" loaded ")+chalk.cyan(l)+chalk.gray(` — ${o.length} cell${1===o.length?"":"s"}. Type `)+chalk.cyan("cells")+chalk.gray(" to list them.")}}catch(e){return{doc:{cells:[],path:null,dirty:!1},note:chalk.yellow(` could not open ${l}: ${e instanceof Error?e.message:String(e)}`)+chalk.gray(" — starting an empty notebook.")}}}(h):null,W=K?K.doc:{cells:[],path:null,dirty:!1};let V=new k({venvPython:N.python,venvRoot:N.root}),Y=null,z=_(W.path);process.stdout.write(chalk.gray(" Starting Python kernel…"));try{await V.start()}catch{return console.log(chalk.red(" failed.")),console.log(chalk.gray(" Check the environment: ")+chalk.cyan("aienv status")),void console.log()}console.log(chalk.green(" ready.")),K&&console.log(K.note),O();const B=$(),F=null!==B,J=F?B.listeners("line").slice():[];F&&B.removeAllListeners("line");const H=F?B:a({input:process.stdin,output:process.stdout,terminal:!0}),Q=F?H.prompt:null;F&&(H.prompt=r.prototype.prompt);const X=[];let Z=!1,ee=!1,oe=!1;const ne=()=>{const e=Y?"icoa ipynb ⚡gpu":"icoa ipynb";H.setPrompt(chalk.bold.cyan(X.length?`${e} ...> `:`${e}> `)),H.prompt()};let le=()=>{};const te=new Promise(e=>{le=e}),re=async()=>{if(!Y)return void console.log(chalk.gray(" not on a GPU kernel."));const e=q();await V.shutdown().catch(()=>{}),e&&await T("release",e),Y=null,console.log(chalk.green(" ⚡ GPU lease released")+chalk.gray(" — billing stopped.")),await(async()=>{V=new k({venvPython:N.python,venvRoot:N.root}),process.stdout.write(chalk.gray(" Starting local kernel…"));try{await V.start(),console.log(chalk.green(" ready.")+chalk.gray(" (local CPU — variables were reset)"))}catch{console.log(chalk.red(" failed.")+chalk.gray(" Check ")+chalk.cyan("aienv status"))}})()},ae=async e=>{const[n,...r]=e.trim().split(/\s+/),a=(n??"").toLowerCase();"on"===a||"connect"===a?await(async()=>{if(Y)return void console.log(chalk.gray(" already on the GPU kernel — ")+chalk.cyan("gpu status")+chalk.gray(" for time left."));const e=q();if(!e)return console.log(chalk.yellow(" No GPU access key found.")),void console.log(chalk.gray(" Save yours once with ")+chalk.cyan("gpu key <your-key>")+chalk.gray(" (issued by your coach/ICOA)."));z||(z=_(W.path)),process.stdout.write(chalk.gray(" Requesting a GPU box…"));let n=await T("lease",e,z??void 0),r=0;for(;"queued"===n.state&&r<24;)0===r&&console.log(),process.stdout.write(`\r ${chalk.yellow(m(n))} `),await new Promise(e=>setTimeout(e,5e3)),n=await T("status",e),r++;if("queued"===n.state)return console.log(),console.log(chalk.yellow(" Still queued — your place is held server-side.")),void console.log(chalk.gray(" Keep working locally; ")+chalk.cyan("gpu on")+chalk.gray(" again in a few minutes."));if("ready"!==n.state)return void console.log(chalk.red(" failed.")+chalk.gray(` ${n.message??"no lease granted"}`));const a=p(n);if("local"!==n.transport||process.env.ICOA_GPU_SERVER||a.push("local transport from a non-dev broker"),a.length>0)return console.log(chalk.red(" rejected: ")+chalk.gray(a.join(", "))),void await T("release",e);const s=z?`${n.dataRoot}/${z}`:n.dataRoot,c=b(n.python,{ICOA_KAGGLE_DATA:s,ICOA_KAGGLE_OUT:n.outDir},n.outDir);let y;if("local"===n.transport)y={command:"/bin/sh",args:["-c",c]};else{const r=i(t(),".icoa","gpu","lease-key");try{o(i(t(),".icoa","gpu"),{recursive:!0}),l(r,n.keyPem,{mode:384})}catch{return console.log(chalk.red(" could not write the ssh key locally.")),void await T("release",e)}y={command:"ssh",args:u(n,r,c)}}const g=new k({spawnSpec:y,readyTimeoutMs:9e4});process.stdout.write(chalk.gray(" connecting…"));try{await g.start()}catch(o){return console.log(chalk.red(" connect failed.")),console.log(chalk.gray(` ${o instanceof Error?o.message:String(o)}`)),void await T("release",e)}await V.shutdown().catch(()=>{}),V=g,Y=n;const d=Math.round((n.timeLeftSec??0)/60);console.log(chalk.bold.green(" ⚡ GPU connected.")),console.log(chalk.gray(` Cells now run on the GPU box · ${d}m left on this lease.`)),console.log(chalk.gray(" Variables were reset — ")+chalk.cyan("run all")+chalk.gray(" to retrain there.")),z&&console.log(chalk.gray(` Dataset is already on the box (${z}) — same cells, no edits.`)),console.log(chalk.gray(" When done: ")+chalk.cyan("download")+chalk.gray(" your submission, then ")+chalk.cyan("gpu off")+chalk.gray("."))})():"off"===a||"disconnect"===a?await re():"status"===a||""===a?await(async()=>{const e=q();if(!e)return void console.log(chalk.gray(" no GPU key saved — ")+chalk.cyan("gpu key <your-key>"));const o=await T("status",e);if("ready"===o.state){const e=Math.round((o.timeLeftSec??0)/60);console.log(chalk.green(` ⚡ GPU lease active · ${e}m left`)+chalk.gray(Y?" (this notebook is connected)":" (not connected here — gpu on)"))}else"queued"===o.state?console.log(chalk.yellow(` ${m(o)}`)):"none"===o.state?console.log(chalk.gray(" no GPU lease — ")+chalk.cyan("gpu on")+chalk.gray(" to request one.")):console.log(chalk.yellow(` ${o.message??"GPU broker not reachable"}`))})():"key"===a?(e=>{const n=e.trim();if(n)try{o(i(t(),".icoa"),{recursive:!0}),l(R(),`${n}\n`,{mode:384}),console.log(chalk.green(" ✓ GPU key saved")+chalk.gray(" — ")+chalk.cyan("gpu on")+chalk.gray(" when you need the box."))}catch{console.log(chalk.red(" could not save the key")+chalk.gray(` (${R()})`))}else console.log(chalk.yellow(" usage: ")+chalk.cyan("gpu key <your-key>"))})(r.join(" ")):console.log(chalk.yellow(" usage: ")+chalk.cyan("gpu on")+chalk.yellow(" · ")+chalk.cyan("gpu off")+chalk.yellow(" · ")+chalk.cyan("gpu status")+chalk.yellow(" · ")+chalk.cyan("gpu key <k>"))},se=e=>{const o=Number(e.trim());return!Number.isInteger(o)||o<1||o>W.cells.length?(console.log(0===W.cells.length?chalk.yellow(" no cells yet — type Python or use ")+chalk.cyan("add"):chalk.yellow(` no cell ${e.trim()||"?"} — pick 1-${W.cells.length} (see `)+chalk.cyan("cells")+chalk.yellow(")")),null):{cell:W.cells[o-1],idx:o}},ce=(e,o)=>!(!e.result||e.result.ok)&&!(!e.result.outputs.some(e=>"error"===e.kind&&"SyntaxError"===e.ename)||!f(e.source)||(console.log(chalk.gray(" ✎ looks like text/markdown, not Python — ")+chalk.cyan(`md ${o}`)+chalk.gray(" converts this cell to markdown (new note: ")+chalk.cyan("add md")+chalk.gray(").")),0)),ie=async e=>{if(ee)return void(e.trim()&&console.log(chalk.gray(" (busy — wait for the current cell to finish, then retype)")));let n=e.trim();if(0===X.length&&/^icoa\s+[a-z/]/i.test(n)&&(n=n.replace(/^icoa\s+/i,"")),0===X.length){const e=n.split(/\s+/),t=(e[0]??"").toLowerCase(),r=n.slice(e[0]?.length??0).trim();if("quit"!==t&&"exit"!==t&&"q"!==t&&(oe=!1),"quit"===t||"exit"===t||"back"===t||"q"===t)return W.dirty&&!oe?(oe=!0,console.log(chalk.yellow(" unsaved changes — ")+chalk.cyan("save")+chalk.yellow(" first, or type ")+chalk.cyan("quit")+chalk.yellow(" again to discard.")),void ne()):void await(async()=>{if(H.removeAllListeners("line"),console.log(),Y){const e=q();e&&await T("release",e),Y=null,console.log(chalk.green(" ⚡ GPU lease released")+chalk.gray(" — billing stopped."))}if(console.log(chalk.gray(" Notebook closed. Kernel stopped.")),console.log(),await V.shutdown().catch(()=>{}),F){Q&&(H.prompt=Q);for(const e of J)H.on("line",e);H.prompt(),le()}else le(),H.close()})();if("clear"===t||"cls"===t)return console.clear(),A(),void ne();if("restart"===t)return await(async()=>{process.stdout.write(chalk.gray(" Restarting kernel…")),await V.shutdown().catch(()=>{});try{await V.start(),console.log(chalk.green(" fresh kernel ready.")+chalk.gray(" (all variables cleared)"))}catch{console.log(chalk.red(" restart failed.")+chalk.gray(" Leave and re-enter the notebook."))}})(),void ne();if("help"===t||"?"===t)return console.log(),console.log(chalk.bold.white(" Notebook commands")),console.log(chalk.cyan(" cells")+chalk.gray(" list cells (○ not run · ✓ ok · ✗ error)")),console.log(chalk.cyan(" show N")+chalk.gray(" print cell N source + outputs")),console.log(chalk.cyan(" edit N")+chalk.gray(" edit cell N in your editor ($EDITOR, default vi)")),console.log(chalk.cyan(" add")+chalk.gray(" write a new Python cell in your editor")),console.log(chalk.cyan(" add md")+chalk.gray(" write a markdown/text cell (notes, headings)")),console.log(chalk.cyan(" md N / py N")+chalk.gray(" convert cell N to markdown / Python")),console.log(chalk.cyan(" del N")+chalk.gray(" delete cell N")),console.log(chalk.cyan(" run N")+chalk.gray(" run cell N — markdown cells render (also: run 2-5, run all)")),console.log(chalk.cyan(" save [file]")+chalk.gray(" save as a real .ipynb")),console.log(chalk.cyan(" restart")+chalk.gray(" restart the kernel (wipes all variables)")),console.log(chalk.cyan(" gpu on/off")+chalk.gray(" run cells on a leased GPU box (dataset pre-mounted)")),console.log(chalk.cyan(" gpu status")+chalk.gray(" lease/queue state · ")+chalk.cyan("gpu key <k>")+chalk.gray(" save your access key")),console.log(chalk.cyan(" download")+chalk.gray(" pull submission.csv from the GPU box to this machine")),console.log(chalk.cyan(" open")+chalk.gray(" reopen the last figure in your image viewer")),console.log(chalk.cyan(" clear")+chalk.gray(" clear the screen · ")+chalk.cyan("quit")+chalk.gray(" leave")),console.log(chalk.gray(" Everything else runs as Python in the persistent kernel (and is appended as a cell).")),console.log(),void ne();if("guide"===t||"/guide"===t)return O(),void ne();if("python"===t||"python3"===t)return console.log(chalk.gray(" You're already inside the notebook kernel — no need to run python.")),console.log(chalk.gray(" Type ")+chalk.bold.cyan("run all")+chalk.gray(" to run every cell, or ")+chalk.cyan("guide")+chalk.gray(" for the basics.")),void ne();if("open"===t)return M&&U(M)?console.log(chalk.gray(" reopening ")+chalk.cyan(M)):console.log(chalk.gray(" no figure yet — plot something (e.g. matplotlib) first.")),void ne();if("cells"===t||"ls"===t)return(()=>{if(0===W.cells.length)return void console.log(chalk.gray(" no cells yet — type Python, or ")+chalk.cyan("add")+chalk.gray(" to write one in your editor."));console.log();const e=Math.max(20,(process.stdout.columns??80)-12);W.cells.forEach((o,n)=>{const l=S(o),t="✓"===l?chalk.green(l.padEnd(2)):"✗"===l?chalk.red(l.padEnd(2)):"md"===l?chalk.magenta(l.padEnd(2)):chalk.gray(l.padEnd(2));console.log(`${chalk.cyan(String(n+1).padStart(4))} ${t} ${chalk.white(x(o,e))}`)}),console.log(),console.log(chalk.gray(" ")+chalk.cyan("show N")+chalk.gray(" · ")+chalk.cyan("edit N")+chalk.gray(" · ")+chalk.cyan("run N")+chalk.gray(" · ")+chalk.cyan("run all"))})(),void ne();if("show"===t)return(e=>{const o=se(e);if(o){if(console.log(),console.log(chalk.gray(` ── cell ${o.idx} ${"markdown"===o.cell.kind?"(markdown) ":""}──`)),"markdown"===o.cell.kind){for(const e of v(o.cell.source))console.log(e);console.log(chalk.gray(" (rendered — ")+chalk.cyan(`edit ${o.idx}`)+chalk.gray(" for the raw source)"))}else for(const e of o.cell.source.split("\n"))console.log(chalk.white(` ${e}`));o.cell.result&&(console.log(chalk.gray(" ── output ──")),D(o.cell.result,L)),console.log()}})(r),void ne();if("edit"===t)return(e=>{const o=se(e);if(!o)return;H.pause();const n=d(o.cell.source,"markdown"===o.cell.kind?".md":".py");if(H.resume(),null===n)return void console.log(chalk.yellow(" editor cancelled or failed — cell unchanged.")+chalk.gray(" (set $EDITOR to choose your editor)"));const l=n.replace(/\s+$/,"");l!==o.cell.source?(o.cell.source=l,o.cell.result=null,o.cell.rawOutputs=void 0,o.cell.rawExecCount=void 0,W.dirty=!0,console.log(chalk.green(` ✓ cell ${o.idx} updated`)+chalk.gray(" — ")+chalk.cyan(`run ${o.idx}`)+chalk.gray(" to execute."))):console.log(chalk.gray(" no changes."))})(r),void ne();if("add"===t)return(e=>{const o=e.trim().toLowerCase(),n="md"===o||"markdown"===o||"text"===o||"txt"===o;if(o&&!n&&"py"!==o&&"code"!==o&&"python"!==o)return void console.log(chalk.yellow(" usage: ")+chalk.cyan("add")+chalk.yellow(" (Python cell) · ")+chalk.cyan("add md")+chalk.yellow(" (markdown/text cell)"));H.pause();const l=d(n?"\x3c!-- markdown cell — write notes/headings, save & exit --\x3e\n":"",n?".md":".py");H.resume();const t=null===l?"":l.replace(/^<!-- markdown cell[^\n]*-->\n?/,"").replace(/\s+$/,"");t?(W.cells.push({kind:n?"markdown":"code",source:t,result:null}),W.dirty=!0,console.log(chalk.green(` ✓ added ${n?"markdown ":""}cell ${W.cells.length}`)+chalk.gray(" — ")+chalk.cyan(`run ${W.cells.length}`)+chalk.gray(n?" to render it.":" to execute."))):console.log(chalk.gray(" nothing added."))})(r),void ne();if("del"===t||"delete"===t)return(e=>{const o=se(e);o&&(W.cells.splice(o.idx-1,1),W.dirty=!0,console.log(chalk.gray(` deleted cell ${o.idx} — ${W.cells.length} left.`)))})(r),void ne();if(("md"===t||"py"===t)&&/^\d+$/.test(r))return((e,o)=>{const n=se(e);if(!n)return;const l="markdown"===o?"markdown":"Python";n.cell.kind!==o?(n.cell.kind=o,n.cell.result=null,n.cell.rawOutputs=void 0,n.cell.rawExecCount=void 0,W.dirty=!0,console.log(chalk.green(` ✓ cell ${n.idx} → ${l}`)+chalk.gray(" — ")+chalk.cyan(`run ${n.idx}`)+chalk.gray("markdown"===o?" to render it.":" to execute."))):console.log(chalk.gray(` cell ${n.idx} is already ${l}.`))})(r,"md"===t?"markdown":"code"),void ne();if("gpu"===t)return ee=!0,await ae(r),ee=!1,void ne();if("download"===t||"dl"===t)return ee=!0,await(async e=>{if(!Y)return void console.log(chalk.gray(" local kernel — your files are already on this machine (no download needed)."));const n=e.trim()||"submission.csv",t=n.startsWith("/")?n:`${Y.outDir}/${n}`;let r;try{r=await V.fetchFile(t)}catch(e){return console.log(chalk.yellow(` could not fetch ${t}: `)+chalk.gray(e instanceof Error?e.message:String(e))),void console.log(chalk.gray(" (run the submission cell first — it writes into the GPU workspace)"))}const a=z?i(process.cwd(),"icoa-kaggle",z):process.cwd(),c=i(a,s(n));try{o(a,{recursive:!0}),l(c,r)}catch{return void console.log(chalk.yellow(" could not write the file here — check directory permissions."))}console.log(chalk.green(" ✓ downloaded ")+chalk.white(c)+chalk.gray(` (${r.length} bytes)`)),z&&"submission.csv"===s(n)&&console.log(chalk.gray(" verify it: ")+chalk.cyan(`kaggle check ${z}`)+chalk.gray(" — then upload on Kaggle yourself."))})(r),ee=!1,void ne();if("run"===t)return ee=!0,await(async e=>{const o=G(e,W.cells.length);if(o)for(const e of o){const n=W.cells[e-1];if("markdown"!==n.kind){console.log(chalk.gray(` ── run cell ${e}: `)+chalk.white(x(n,50))+chalk.gray(" ──"));try{n.result=await V.execute(n.source)}catch(e){return console.log(chalk.red(" Kernel error: ")+chalk.gray(e instanceof Error?e.message:String(e))),void j()}if(W.dirty=!0,D(n.result,L),ce(n,e),!n.result.ok){const n=e!==o[o.length-1];return console.log(chalk.yellow(` ✗ cell ${e} raised${n?" — stopping here.":"."}`)+chalk.gray(" Fix it (")+chalk.cyan(`edit ${e}`)+chalk.gray(") and re-run.")),void j()}}else{console.log(chalk.gray(` ── cell ${e} (markdown) ──`));for(const e of v(n.source))console.log(e);console.log()}}else console.log(0===W.cells.length?chalk.yellow(" no cells yet — type Python or use ")+chalk.cyan("add"):chalk.yellow(" usage: ")+chalk.cyan("run 3")+chalk.yellow(" · ")+chalk.cyan("run 1-5")+chalk.yellow(" · ")+chalk.cyan("run all"))})(r),ee=!1,void ne();if("save"===t)return(e=>{if(0===W.cells.length)return void console.log(chalk.yellow(" nothing to save — the notebook is empty."));let o;o=e.trim()?c(e.trim())?e.trim():y(process.cwd(),e.trim()):W.path?W.path.endsWith(".ipynb")?W.path:`${W.path.replace(/\.[^./]+$/,"")}.ipynb`:y(process.cwd(),"notebook.ipynb"),o.endsWith(".ipynb")||(o+=".ipynb");try{l(o,C(W.cells)),W.path=o,W.dirty=!1,console.log(chalk.green(" ✓ saved ")+chalk.cyan(o)+chalk.gray(` (${W.cells.length} cells)`))}catch(e){console.log(chalk.red(" save failed: ")+chalk.gray(e instanceof Error?e.message:String(e)))}})(r),void ne();if(""===n)return void ne()}if(X.length>0&&I.has(n.toLowerCase()))return console.log(chalk.gray(" (⏸ you are inside a multi-line block — press Enter on an ")+chalk.bold.white("empty line")+chalk.gray(" to run it; then ")+chalk.cyan(n.toLowerCase().replace(/^\//,""))+chalk.gray(" will work. This line was not added to the block.)")),void ne();if(X.push(e),!w(X))return 1!==X.length||Z||(Z=!0,console.log(chalk.gray(" (multi-line block started — keep typing; an empty line runs it)"))),void ne();const t=X.join("\n").replace(/\s+$/,"");if(X.length=0,""!==t.trim()){ee=!0;try{const e=await V.execute(t);W.cells.push({kind:"code",source:t,result:e}),W.dirty=!0,D(e,L);const o=ce(W.cells[W.cells.length-1],W.cells.length);e.ok||o||j()}catch(e){console.log(chalk.red(" Kernel error: ")+chalk.gray(e instanceof Error?e.message:String(e))),j()}ee=!1,ne()}else ne()};H.on("line",e=>{ie(e)}),F||H.on("close",async()=>{await V.shutdown().catch(()=>{}),process.exit(0)}),ne(),await te}(h)})}function A(){console.log(),console.log(chalk.bold.white(" ICOA Notebook ")+chalk.gray("· in-terminal Python · AI/ML notebook arena")),console.log(chalk.gray(" ─────────────────────────────────────────────"))}function O(){console.log(chalk.bold.white(" ▶ Start here: ")+chalk.bold.cyan("run all")+chalk.gray(" — runs every cell (trains the baseline, writes submission.csv).")),console.log(chalk.gray(" You are already inside the notebook — do ")+chalk.bold.red("not")+chalk.gray(" type ")+chalk.cyan("python file.ipynb")+chalk.gray(". Just ")+chalk.bold.cyan("run all")+chalk.gray(".")),console.log(chalk.gray(" Then: ")+chalk.cyan("cells")+chalk.gray(" list · ")+chalk.cyan("edit N")+chalk.gray(" edit · ")+chalk.cyan("save")+chalk.gray(" · ")+chalk.cyan("help")+chalk.gray(" full · ")+chalk.cyan("quit")),console.log(chalk.gray(" Or just type Python and press Enter (blocks continue until a blank line).")),console.log(chalk.bold.yellow(" 🧭 Lost at any point? Type ")+chalk.bold.cyan("guide")+chalk.bold.yellow(" — these instructions reprint right here.")),console.log()}const I=new Set(["guide","/guide","help","?","quit","exit","back","menu","cells","run all"]);function j(){console.log(chalk.gray(" 🧭 stuck? type ")+chalk.cyan("guide")+chalk.gray(" for the basics · ")+chalk.cyan("help")+chalk.gray(" for every command."))}const L="https://practice.icoa2026.au";function R(){return i(t(),".icoa","gpu-key")}function q(){if(process.env.ICOA_GPU_KEY)return process.env.ICOA_GPU_KEY.trim();try{return n(R(),"utf-8").trim()||null}catch{return null}}async function T(e,o,n){try{const l=await fetch(`${process.env.ICOA_GPU_SERVER?process.env.ICOA_GPU_SERVER.replace(/\/$/,""):L}/api/icoa/gpu/${e}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(n?{key:o,task:n}:{key:o}),signal:AbortSignal.timeout(2e4)});return h(await l.json())}catch{return{state:"error",message:"GPU broker not reachable"}}}function _(e){const o=e?.match(/icoa-kaggle[/\\]([a-z0-9-]+)[/\\]/);return o?o[1]:null}let K=0,M=null;function D(e,o){for(const n of e.outputs)if("stream"===n.kind){const e=n.text.replace(/\n$/,"");if(0===e.length)continue;console.log("stderr"===n.name?chalk.yellow(e):chalk.white(e))}else if("result"===n.kind)console.log(chalk.cyan(` Out[${e.execCount??"*"}]: `)+chalk.white(n.text));else if("error"===n.kind){console.log(chalk.bold.red(` ${n.ename}: ${n.evalue}`));for(const e of n.traceback)console.log(chalk.gray(` ${e.replace(N,"")}`))}else"display"===n.kind&&W(n.data,o)}function W(e,n){if(e["image/png"])try{o(n,{recursive:!0}),K+=1;const t=i(n,`figure-${K}.png`);return l(t,Buffer.from(e["image/png"],"base64")),M=t,console.log(chalk.magenta(" 🖼 figure saved → ")+chalk.cyan(t)),void(U(t)?console.log(chalk.gray(" opening in your image viewer… (type ")+chalk.cyan("open")+chalk.gray(" to reopen)")):console.log(chalk.gray(" open it with your file browser (inline images need a graphical viewer).")))}catch{}e["text/plain"]&&console.log(chalk.white(` ${e["text/plain"]}`))}
1
+ import{existsSync as e,mkdirSync as o,readFileSync as n,writeFileSync as l}from"node:fs";import{homedir as t}from"node:os";import{Interface as r,createInterface as a}from"node:readline";import{basename as s,isAbsolute as c,join as i,resolve as y}from"node:path";import chalk from"chalk";import{aienvPaths as g}from"../lib/aienv.js";import{editTextInEditor as d}from"../lib/editor-spawn.js";import{buildSshArgs as u,leaseConnectProblems as p,parseLeaseResponse as h,queueLine as m}from"../lib/gpu.js";import{shouldExecuteCell as w}from"../lib/ipynb-input.js";import{looksLikeMarkdown as f,renderMarkdown as v}from"../lib/md-term.js";import{NotebookKernel as k,buildRemoteBridgeCommand as b}from"../lib/kernel.js";import{getMainRl as $}from"../lib/main-rl.js";import{logCommand as x}from"../lib/logger.js";import{startLogSync as P,stopLogSync as E}from"../lib/log-sync.js";import{cellPreview as G,parseIpynb as C,parsePercentPy as S,parseRunRange as U,serializeIpynb as N,statusChar as A}from"../lib/notebook-doc.js";import{openFile as O}from"../lib/open-file.js";const j=new RegExp(`${String.fromCharCode(27)}\\[[0-9;]*m`,"g");export function registerIpynbCommand(h){h.command("ipynb").argument("[file]","notebook (.ipynb) or script (.py, split on # %%) to open").description("Open an in-terminal Python notebook (AI/ML — needs `aienv setup`)").action(async h=>{await async function(h){const j=g(t()),T=i(t(),".icoa","ipynb-out");if(I(),!e(j.python))return console.log(chalk.yellow(" The AI/ML environment is not set up yet.")),console.log(chalk.gray(" Run ")+chalk.bold.cyan("aienv setup")+chalk.gray(" first (one-time, ~300 MB).")),void console.log();const W=h?function(o){const l=c(o)?o:y(process.cwd(),o);if(!e(l))return{doc:{cells:[],path:l,dirty:!1},note:chalk.gray(" new notebook — will be created on ")+chalk.cyan("save")};try{const e=n(l,"utf8"),o=l.endsWith(".ipynb")?C(e):S(e);return{doc:{cells:o,path:l,dirty:!1},note:chalk.gray(" loaded ")+chalk.cyan(l)+chalk.gray(` — ${o.length} cell${1===o.length?"":"s"}. Type `)+chalk.cyan("cells")+chalk.gray(" to list them.")}}catch(e){return{doc:{cells:[],path:null,dirty:!1},note:chalk.yellow(` could not open ${l}: ${e instanceof Error?e.message:String(e)}`)+chalk.gray(" — starting an empty notebook.")}}}(h):null,z=W?W.doc:{cells:[],path:null,dirty:!1};let B=new k({venvPython:j.python,venvRoot:j.root}),F=null,J=D(z.path);process.stdout.write(chalk.gray(" Starting Python kernel…"));try{await B.start()}catch{return console.log(chalk.red(" failed.")),console.log(chalk.gray(" Check the environment: ")+chalk.cyan("aienv status")),void console.log()}console.log(chalk.green(" ready.")),W&&console.log(W.note),L();const H=$(),Q=null!==H,X=Q?H.listeners("line").slice():[];Q&&H.removeAllListeners("line");const Z=Q?H:a({input:process.stdin,output:process.stdout,terminal:!0}),ee=Q?Z.prompt:null;Q&&(Z.prompt=r.prototype.prompt),Q||P();const oe=[];let ne=!1,le=!1,te=!1;const re=()=>{const e=F?"icoa ipynb ⚡gpu":"icoa ipynb";Z.setPrompt(chalk.bold.cyan(oe.length?`${e} ...> `:`${e}> `)),Z.prompt()};let ae=()=>{};const se=new Promise(e=>{ae=e}),ce=async()=>{if(!F)return void console.log(chalk.gray(" not on a GPU kernel."));const e=K();await B.shutdown().catch(()=>{}),e&&await M("release",e),F=null,console.log(chalk.green(" ⚡ GPU lease released")+chalk.gray(" — billing stopped.")),await(async()=>{B=new k({venvPython:j.python,venvRoot:j.root}),process.stdout.write(chalk.gray(" Starting local kernel…"));try{await B.start(),console.log(chalk.green(" ready.")+chalk.gray(" (local CPU — variables were reset)"))}catch{console.log(chalk.red(" failed.")+chalk.gray(" Check ")+chalk.cyan("aienv status"))}})()},ie=async e=>{const[n,...r]=e.trim().split(/\s+/),a=(n??"").toLowerCase();"on"===a||"connect"===a?await(async()=>{if(F)return void console.log(chalk.gray(" already on the GPU kernel — ")+chalk.cyan("gpu status")+chalk.gray(" for time left."));const e=K();if(!e)return console.log(chalk.yellow(" No GPU access key found.")),void console.log(chalk.gray(" Save yours once with ")+chalk.cyan("gpu key <your-key>")+chalk.gray(" (issued by your coach/ICOA)."));J||(J=D(z.path)),process.stdout.write(chalk.gray(" Requesting a GPU box…"));let n=await M("lease",e,J??void 0),r=0;for(;"queued"===n.state&&r<24;)0===r&&console.log(),process.stdout.write(`\r ${chalk.yellow(m(n))} `),await new Promise(e=>setTimeout(e,5e3)),n=await M("status",e),r++;if("queued"===n.state)return console.log(),console.log(chalk.yellow(" Still queued — your place is held server-side.")),void console.log(chalk.gray(" Keep working locally; ")+chalk.cyan("gpu on")+chalk.gray(" again in a few minutes."));if("ready"!==n.state)return void console.log(chalk.red(" failed.")+chalk.gray(` ${n.message??"no lease granted"}`));const a=p(n);if("local"!==n.transport||process.env.ICOA_GPU_SERVER||a.push("local transport from a non-dev broker"),a.length>0)return console.log(chalk.red(" rejected: ")+chalk.gray(a.join(", "))),void await M("release",e);const s=J?`${n.dataRoot}/${J}`:n.dataRoot,c=b(n.python,{ICOA_KAGGLE_DATA:s,ICOA_KAGGLE_OUT:n.outDir},n.outDir);let y;if("local"===n.transport)y={command:"/bin/sh",args:["-c",c]};else{const r=i(t(),".icoa","gpu","lease-key");try{o(i(t(),".icoa","gpu"),{recursive:!0}),l(r,n.keyPem,{mode:384})}catch{return console.log(chalk.red(" could not write the ssh key locally.")),void await M("release",e)}y={command:"ssh",args:u(n,r,c)}}const g=new k({spawnSpec:y,readyTimeoutMs:9e4});process.stdout.write(chalk.gray(" connecting…"));try{await g.start()}catch(o){return console.log(chalk.red(" connect failed.")),console.log(chalk.gray(` ${o instanceof Error?o.message:String(o)}`)),void await M("release",e)}await B.shutdown().catch(()=>{}),B=g,F=n;const d=Math.round((n.timeLeftSec??0)/60);console.log(chalk.bold.green(" ⚡ GPU connected.")),console.log(chalk.gray(` Cells now run on the GPU box · ${d}m left on this lease.`)),console.log(chalk.gray(" Variables were reset — ")+chalk.cyan("run all")+chalk.gray(" to retrain there.")),J&&console.log(chalk.gray(` Dataset is already on the box (${J}) — same cells, no edits.`)),console.log(chalk.gray(" When done: ")+chalk.cyan("download")+chalk.gray(" your submission, then ")+chalk.cyan("gpu off")+chalk.gray("."))})():"off"===a||"disconnect"===a?await ce():"status"===a||""===a?await(async()=>{const e=K();if(!e)return void console.log(chalk.gray(" no GPU key saved — ")+chalk.cyan("gpu key <your-key>"));const o=await M("status",e);if("ready"===o.state){const e=Math.round((o.timeLeftSec??0)/60);console.log(chalk.green(` ⚡ GPU lease active · ${e}m left`)+chalk.gray(F?" (this notebook is connected)":" (not connected here — gpu on)"))}else"queued"===o.state?console.log(chalk.yellow(` ${m(o)}`)):"none"===o.state?console.log(chalk.gray(" no GPU lease — ")+chalk.cyan("gpu on")+chalk.gray(" to request one.")):console.log(chalk.yellow(` ${o.message??"GPU broker not reachable"}`))})():"key"===a?(e=>{const n=e.trim();if(n)try{o(i(t(),".icoa"),{recursive:!0}),l(_(),`${n}\n`,{mode:384}),console.log(chalk.green(" ✓ GPU key saved")+chalk.gray(" — ")+chalk.cyan("gpu on")+chalk.gray(" when you need the box."))}catch{console.log(chalk.red(" could not save the key")+chalk.gray(` (${_()})`))}else console.log(chalk.yellow(" usage: ")+chalk.cyan("gpu key <your-key>"))})(r.join(" ")):console.log(chalk.yellow(" usage: ")+chalk.cyan("gpu on")+chalk.yellow(" · ")+chalk.cyan("gpu off")+chalk.yellow(" · ")+chalk.cyan("gpu status")+chalk.yellow(" · ")+chalk.cyan("gpu key <k>"))},ye=e=>{const o=Number(e.trim());return!Number.isInteger(o)||o<1||o>z.cells.length?(console.log(0===z.cells.length?chalk.yellow(" no cells yet — type Python or use ")+chalk.cyan("add"):chalk.yellow(` no cell ${e.trim()||"?"} — pick 1-${z.cells.length} (see `)+chalk.cyan("cells")+chalk.yellow(")")),null):{cell:z.cells[o-1],idx:o}},ge=(e,o)=>!(!e.result||e.result.ok)&&!(!e.result.outputs.some(e=>"error"===e.kind&&"SyntaxError"===e.ename)||!f(e.source)||(console.log(chalk.gray(" ✎ looks like text/markdown, not Python — ")+chalk.cyan(`md ${o}`)+chalk.gray(" converts this cell to markdown (new note: ")+chalk.cyan("add md")+chalk.gray(").")),0)),de=async e=>{if(e.trim()&&x(`ipynb: ${e.trim()}`),le)return void(e.trim()&&console.log(chalk.gray(" (busy — wait for the current cell to finish, then retype)")));let n=e.trim();if(0===oe.length&&/^icoa\s+[a-z/]/i.test(n)&&(n=n.replace(/^icoa\s+/i,"")),0===oe.length){const e=n.split(/\s+/),t=(e[0]??"").toLowerCase(),r=n.slice(e[0]?.length??0).trim();if("quit"!==t&&"exit"!==t&&"q"!==t&&(te=!1),"quit"===t||"exit"===t||"back"===t||"q"===t)return z.dirty&&!te?(te=!0,console.log(chalk.yellow(" unsaved changes — ")+chalk.cyan("save")+chalk.yellow(" first, or type ")+chalk.cyan("quit")+chalk.yellow(" again to discard.")),void re()):void await(async()=>{if(Z.removeAllListeners("line"),Q||E(),console.log(),F){const e=K();e&&await M("release",e),F=null,console.log(chalk.green(" ⚡ GPU lease released")+chalk.gray(" — billing stopped."))}if(console.log(chalk.gray(" Notebook closed. Kernel stopped.")),console.log(),await B.shutdown().catch(()=>{}),Q){ee&&(Z.prompt=ee);for(const e of X)Z.on("line",e);Z.prompt(),ae()}else ae(),Z.close()})();if("clear"===t||"cls"===t)return console.clear(),I(),void re();if("restart"===t)return await(async()=>{process.stdout.write(chalk.gray(" Restarting kernel…")),await B.shutdown().catch(()=>{});try{await B.start(),console.log(chalk.green(" fresh kernel ready.")+chalk.gray(" (all variables cleared)"))}catch{console.log(chalk.red(" restart failed.")+chalk.gray(" Leave and re-enter the notebook."))}})(),void re();if("help"===t||"?"===t)return console.log(),console.log(chalk.bold.white(" Notebook commands")),console.log(chalk.cyan(" cells")+chalk.gray(" list cells (○ not run · ✓ ok · ✗ error)")),console.log(chalk.cyan(" show N")+chalk.gray(" print cell N source + outputs")),console.log(chalk.cyan(" edit N")+chalk.gray(" edit cell N in your editor ($EDITOR, default vi)")),console.log(chalk.cyan(" add")+chalk.gray(" write a new Python cell in your editor")),console.log(chalk.cyan(" add md")+chalk.gray(" write a markdown/text cell (notes, headings)")),console.log(chalk.cyan(" md N / py N")+chalk.gray(" convert cell N to markdown / Python")),console.log(chalk.cyan(" del N")+chalk.gray(" delete cell N")),console.log(chalk.cyan(" run N")+chalk.gray(" run cell N — markdown cells render (also: run 2-5, run all)")),console.log(chalk.cyan(" save [file]")+chalk.gray(" save as a real .ipynb")),console.log(chalk.cyan(" restart")+chalk.gray(" restart the kernel (wipes all variables)")),console.log(chalk.cyan(" gpu on/off")+chalk.gray(" run cells on a leased GPU box (dataset pre-mounted)")),console.log(chalk.cyan(" gpu status")+chalk.gray(" lease/queue state · ")+chalk.cyan("gpu key <k>")+chalk.gray(" save your access key")),console.log(chalk.cyan(" download")+chalk.gray(" pull submission.csv from the GPU box to this machine")),console.log(chalk.cyan(" open")+chalk.gray(" reopen the last figure in your image viewer")),console.log(chalk.cyan(" clear")+chalk.gray(" clear the screen · ")+chalk.cyan("quit")+chalk.gray(" leave")),console.log(chalk.gray(" Everything else runs as Python in the persistent kernel (and is appended as a cell).")),console.log(),void re();if("guide"===t||"/guide"===t)return L(),void re();if("python"===t||"python3"===t)return console.log(chalk.gray(" You're already inside the notebook kernel — no need to run python.")),console.log(chalk.gray(" Type ")+chalk.bold.cyan("run all")+chalk.gray(" to run every cell, or ")+chalk.cyan("guide")+chalk.gray(" for the basics.")),void re();if("open"===t)return V&&O(V)?console.log(chalk.gray(" reopening ")+chalk.cyan(V)):console.log(chalk.gray(" no figure yet — plot something (e.g. matplotlib) first.")),void re();if("cells"===t||"ls"===t)return(()=>{if(0===z.cells.length)return void console.log(chalk.gray(" no cells yet — type Python, or ")+chalk.cyan("add")+chalk.gray(" to write one in your editor."));console.log();const e=Math.max(20,(process.stdout.columns??80)-12);z.cells.forEach((o,n)=>{const l=A(o),t="✓"===l?chalk.green(l.padEnd(2)):"✗"===l?chalk.red(l.padEnd(2)):"md"===l?chalk.magenta(l.padEnd(2)):chalk.gray(l.padEnd(2));console.log(`${chalk.cyan(String(n+1).padStart(4))} ${t} ${chalk.white(G(o,e))}`)}),console.log(),console.log(chalk.gray(" ")+chalk.cyan("show N")+chalk.gray(" · ")+chalk.cyan("edit N")+chalk.gray(" · ")+chalk.cyan("run N")+chalk.gray(" · ")+chalk.cyan("run all"))})(),void re();if("show"===t)return(e=>{const o=ye(e);if(o){if(console.log(),console.log(chalk.gray(` ── cell ${o.idx} ${"markdown"===o.cell.kind?"(markdown) ":""}──`)),"markdown"===o.cell.kind){for(const e of v(o.cell.source))console.log(e);console.log(chalk.gray(" (rendered — ")+chalk.cyan(`edit ${o.idx}`)+chalk.gray(" for the raw source)"))}else for(const e of o.cell.source.split("\n"))console.log(chalk.white(` ${e}`));o.cell.result&&(console.log(chalk.gray(" ── output ──")),Y(o.cell.result,T)),console.log()}})(r),void re();if("edit"===t)return(e=>{const o=ye(e);if(!o)return;Z.pause();const n=d(o.cell.source,"markdown"===o.cell.kind?".md":".py");if(Z.resume(),null===n)return void console.log(chalk.yellow(" editor cancelled or failed — cell unchanged.")+chalk.gray(" (set $EDITOR to choose your editor)"));const l=n.replace(/\s+$/,"");l!==o.cell.source?(o.cell.source=l,o.cell.result=null,o.cell.rawOutputs=void 0,o.cell.rawExecCount=void 0,z.dirty=!0,console.log(chalk.green(` ✓ cell ${o.idx} updated`)+chalk.gray(" — ")+chalk.cyan(`run ${o.idx}`)+chalk.gray(" to execute."))):console.log(chalk.gray(" no changes."))})(r),void re();if("add"===t)return(e=>{const o=e.trim().toLowerCase(),n="md"===o||"markdown"===o||"text"===o||"txt"===o;if(o&&!n&&"py"!==o&&"code"!==o&&"python"!==o)return void console.log(chalk.yellow(" usage: ")+chalk.cyan("add")+chalk.yellow(" (Python cell) · ")+chalk.cyan("add md")+chalk.yellow(" (markdown/text cell)"));Z.pause();const l=d(n?"\x3c!-- markdown cell — write notes/headings, save & exit --\x3e\n":"",n?".md":".py");Z.resume();const t=null===l?"":l.replace(/^<!-- markdown cell[^\n]*-->\n?/,"").replace(/\s+$/,"");t?(z.cells.push({kind:n?"markdown":"code",source:t,result:null}),z.dirty=!0,console.log(chalk.green(` ✓ added ${n?"markdown ":""}cell ${z.cells.length}`)+chalk.gray(" — ")+chalk.cyan(`run ${z.cells.length}`)+chalk.gray(n?" to render it.":" to execute."))):console.log(chalk.gray(" nothing added."))})(r),void re();if("del"===t||"delete"===t)return(e=>{const o=ye(e);o&&(z.cells.splice(o.idx-1,1),z.dirty=!0,console.log(chalk.gray(` deleted cell ${o.idx} — ${z.cells.length} left.`)))})(r),void re();if(("md"===t||"py"===t)&&/^\d+$/.test(r))return((e,o)=>{const n=ye(e);if(!n)return;const l="markdown"===o?"markdown":"Python";n.cell.kind!==o?(n.cell.kind=o,n.cell.result=null,n.cell.rawOutputs=void 0,n.cell.rawExecCount=void 0,z.dirty=!0,console.log(chalk.green(` ✓ cell ${n.idx} → ${l}`)+chalk.gray(" — ")+chalk.cyan(`run ${n.idx}`)+chalk.gray("markdown"===o?" to render it.":" to execute."))):console.log(chalk.gray(` cell ${n.idx} is already ${l}.`))})(r,"md"===t?"markdown":"code"),void re();if("gpu"===t)return le=!0,await ie(r),le=!1,void re();if("download"===t||"dl"===t)return le=!0,await(async e=>{if(!F)return void console.log(chalk.gray(" local kernel — your files are already on this machine (no download needed)."));const n=e.trim()||"submission.csv",t=n.startsWith("/")?n:`${F.outDir}/${n}`;let r;try{r=await B.fetchFile(t)}catch(e){return console.log(chalk.yellow(` could not fetch ${t}: `)+chalk.gray(e instanceof Error?e.message:String(e))),void console.log(chalk.gray(" (run the submission cell first — it writes into the GPU workspace)"))}const a=J?i(process.cwd(),"icoa-kaggle",J):process.cwd(),c=i(a,s(n));try{o(a,{recursive:!0}),l(c,r)}catch{return void console.log(chalk.yellow(" could not write the file here — check directory permissions."))}console.log(chalk.green(" ✓ downloaded ")+chalk.white(c)+chalk.gray(` (${r.length} bytes)`)),J&&"submission.csv"===s(n)&&console.log(chalk.gray(" verify it: ")+chalk.cyan(`kaggle check ${J}`)+chalk.gray(" — then upload on Kaggle yourself."))})(r),le=!1,void re();if("run"===t)return le=!0,await(async e=>{const o=U(e,z.cells.length);if(o)for(const e of o){const n=z.cells[e-1];if("markdown"!==n.kind){console.log(chalk.gray(` ── run cell ${e}: `)+chalk.white(G(n,50))+chalk.gray(" ──"));try{n.result=await B.execute(n.source)}catch(e){return console.log(chalk.red(" Kernel error: ")+chalk.gray(e instanceof Error?e.message:String(e))),void q()}if(z.dirty=!0,Y(n.result,T),ge(n,e),!n.result.ok){const n=e!==o[o.length-1];return console.log(chalk.yellow(` ✗ cell ${e} raised${n?" — stopping here.":"."}`)+chalk.gray(" Fix it (")+chalk.cyan(`edit ${e}`)+chalk.gray(") and re-run.")),void q()}}else{console.log(chalk.gray(` ── cell ${e} (markdown) ──`));for(const e of v(n.source))console.log(e);console.log()}}else console.log(0===z.cells.length?chalk.yellow(" no cells yet — type Python or use ")+chalk.cyan("add"):chalk.yellow(" usage: ")+chalk.cyan("run 3")+chalk.yellow(" · ")+chalk.cyan("run 1-5")+chalk.yellow(" · ")+chalk.cyan("run all"))})(r),le=!1,void re();if("save"===t)return(e=>{if(0===z.cells.length)return void console.log(chalk.yellow(" nothing to save — the notebook is empty."));let o;o=e.trim()?c(e.trim())?e.trim():y(process.cwd(),e.trim()):z.path?z.path.endsWith(".ipynb")?z.path:`${z.path.replace(/\.[^./]+$/,"")}.ipynb`:y(process.cwd(),"notebook.ipynb"),o.endsWith(".ipynb")||(o+=".ipynb");try{l(o,N(z.cells)),z.path=o,z.dirty=!1,console.log(chalk.green(" ✓ saved ")+chalk.cyan(o)+chalk.gray(` (${z.cells.length} cells)`))}catch(e){console.log(chalk.red(" save failed: ")+chalk.gray(e instanceof Error?e.message:String(e)))}})(r),void re();if(""===n)return void re()}if(oe.length>0&&R.has(n.toLowerCase()))return console.log(chalk.gray(" (⏸ you are inside a multi-line block — press Enter on an ")+chalk.bold.white("empty line")+chalk.gray(" to run it; then ")+chalk.cyan(n.toLowerCase().replace(/^\//,""))+chalk.gray(" will work. This line was not added to the block.)")),void re();if(oe.push(e),!w(oe))return 1!==oe.length||ne||(ne=!0,console.log(chalk.gray(" (multi-line block started — keep typing; an empty line runs it)"))),void re();const t=oe.join("\n").replace(/\s+$/,"");if(oe.length=0,""!==t.trim()){le=!0;try{const e=await B.execute(t);z.cells.push({kind:"code",source:t,result:e}),z.dirty=!0,Y(e,T);const o=ge(z.cells[z.cells.length-1],z.cells.length);e.ok||o||q()}catch(e){console.log(chalk.red(" Kernel error: ")+chalk.gray(e instanceof Error?e.message:String(e))),q()}le=!1,re()}else re()};Z.on("line",e=>{de(e)}),Q||Z.on("close",async()=>{await B.shutdown().catch(()=>{}),process.exit(0)}),re(),await se}(h)})}function I(){console.log(),console.log(chalk.bold.white(" ICOA Notebook ")+chalk.gray("· in-terminal Python · AI/ML notebook arena")),console.log(chalk.gray(" ─────────────────────────────────────────────"))}function L(){console.log(chalk.bold.white(" ▶ Start here: ")+chalk.bold.cyan("run all")+chalk.gray(" — runs every cell (trains the baseline, writes submission.csv).")),console.log(chalk.gray(" You are already inside the notebook — do ")+chalk.bold.red("not")+chalk.gray(" type ")+chalk.cyan("python file.ipynb")+chalk.gray(". Just ")+chalk.bold.cyan("run all")+chalk.gray(".")),console.log(chalk.gray(" Then: ")+chalk.cyan("cells")+chalk.gray(" list · ")+chalk.cyan("edit N")+chalk.gray(" edit · ")+chalk.cyan("save")+chalk.gray(" · ")+chalk.cyan("help")+chalk.gray(" full · ")+chalk.cyan("quit")),console.log(chalk.gray(" Or just type Python and press Enter (blocks continue until a blank line).")),console.log(chalk.bold.yellow(" 🧭 Lost at any point? Type ")+chalk.bold.cyan("guide")+chalk.bold.yellow(" — these instructions reprint right here.")),console.log()}const R=new Set(["guide","/guide","help","?","quit","exit","back","menu","cells","run all"]);function q(){console.log(chalk.gray(" 🧭 stuck? type ")+chalk.cyan("guide")+chalk.gray(" for the basics · ")+chalk.cyan("help")+chalk.gray(" for every command."))}const T="https://practice.icoa2026.au";function _(){return i(t(),".icoa","gpu-key")}function K(){if(process.env.ICOA_GPU_KEY)return process.env.ICOA_GPU_KEY.trim();try{return n(_(),"utf-8").trim()||null}catch{return null}}async function M(e,o,n){try{const l=await fetch(`${process.env.ICOA_GPU_SERVER?process.env.ICOA_GPU_SERVER.replace(/\/$/,""):T}/api/icoa/gpu/${e}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(n?{key:o,task:n}:{key:o}),signal:AbortSignal.timeout(2e4)});return h(await l.json())}catch{return{state:"error",message:"GPU broker not reachable"}}}function D(e){const o=e?.match(/icoa-kaggle[/\\]([a-z0-9-]+)[/\\]/);return o?o[1]:null}let W=0,V=null;function Y(e,o){for(const n of e.outputs)if("stream"===n.kind){const e=n.text.replace(/\n$/,"");if(0===e.length)continue;console.log("stderr"===n.name?chalk.yellow(e):chalk.white(e))}else if("result"===n.kind)console.log(chalk.cyan(` Out[${e.execCount??"*"}]: `)+chalk.white(n.text));else if("error"===n.kind){console.log(chalk.bold.red(` ${n.ename}: ${n.evalue}`));for(const e of n.traceback)console.log(chalk.gray(` ${e.replace(j,"")}`))}else"display"===n.kind&&z(n.data,o)}function z(e,n){if(e["image/png"])try{o(n,{recursive:!0}),W+=1;const t=i(n,`figure-${W}.png`);return l(t,Buffer.from(e["image/png"],"base64")),V=t,console.log(chalk.magenta(" 🖼 figure saved → ")+chalk.cyan(t)),void(O(t)?console.log(chalk.gray(" opening in your image viewer… (type ")+chalk.cyan("open")+chalk.gray(" to reopen)")):console.log(chalk.gray(" open it with your file browser (inline images need a graphical viewer).")))}catch{}e["text/plain"]&&console.log(chalk.white(` ${e["text/plain"]}`))}
@@ -1 +1 @@
1
- import{existsSync as o,mkdirSync as e,readFileSync as n,readdirSync as l,writeFileSync as t}from"node:fs";import{join as s}from"node:path";import chalk from"chalk";import{makeReplAsker as i}from"../lib/repl-asker.js";import{openFile as r}from"../lib/open-file.js";import{serializeIpynb as a}from"../lib/notebook-doc.js";import{loadLearnState as c}from"../lib/learn-state.js";import{buildStarterCells as g,dataFileUrls as u,parseKaggleTaskList as d,parseSubmitResponse as y,validateSubmissionText as m}from"../lib/kaggle.js";function b(){return process.env.ICOA_KAGGLE_SERVER?process.env.ICOA_KAGGLE_SERVER.replace(/\/$/,""):"https://practice.icoa2026.au"}async function h(o){try{const e=await fetch(b()+o,{signal:AbortSignal.timeout(3e4)});return await e.json()}catch{return null}}async function p(o){try{const e=await fetch(o,{signal:AbortSignal.timeout(6e4)});return e.ok?await e.text():null}catch{return null}}function f(){console.log(),console.log(chalk.yellow(" The practice server is not reachable right now.")),console.log(chalk.gray(" Check your connection, or try again later.")),console.log()}function w(o){return s(process.cwd(),"icoa-kaggle",o)}function k(o){if("icoa-holdout"===o?.judge)return console.log(chalk.bold.white(" Submit to ICOA (scored on the server):")),console.log(chalk.gray(" ")+chalk.bold.cyan(`kaggle submit ${o.comp_id}`)),void console.log(chalk.gray(" scores vs the secret test labels → live rank on ")+chalk.cyan("board"));console.log(chalk.bold.white(" Submit on Kaggle (manual, your own account):")),o?.kaggle_url?(console.log(chalk.gray(" 1. open ")+chalk.cyan(o.kaggle_url)),console.log(chalk.gray(" 2. Submit Predictions → upload ")+chalk.white("submission.csv")),console.log(chalk.gray(" 3. read your score, improve the MODEL cell, re-run, repeat."))):(console.log(chalk.gray(" (placeholder dataset — no live Kaggle page yet; when the real")),console.log(chalk.gray(" competition dataset lands, its submission link will appear here.)")))}export async function runKaggle(n){const l=i(),r=async o=>await l.ask(o)??"";try{console.log(),console.log(chalk.bold.cyan(" 📊 KAGGLE PRACTICE")+chalk.gray(" · train locally · submit on Kaggle yourself")),console.log(chalk.gray(" Small real-format datasets, a runnable starter notebook, your own CPU.")),console.log();const l=await h("/api/icoa/kaggle/tasks");if(!l)return f();const i=d(l);if(0===i.length)return console.log(chalk.yellow(" No practice competitions are live yet — check back soon.")),void console.log();let c=n?i.find(o=>o.comp_id===n):void 0;if(n&&!c&&console.log(chalk.yellow(` No competition called "${n}".`)),!c){console.log(chalk.white(" Live practice competitions:")),i.forEach((o,e)=>{console.log(chalk.gray(` ${e+1}. `)+chalk.cyan(o.comp_id)+chalk.gray(` — ${o.title} · ${o.metric}`))});for(let o=0;o<3&&!c;o++){const o=(await r(chalk.cyan(" Pick one (number or id): "))).trim();if(!o)return;const e=Number(o);c=!Number.isNaN(e)&&i[e-1]||i.find(e=>e.comp_id===o.toLowerCase())||void 0,c||console.log(chalk.gray(` Try a number 1-${i.length} or an id like "${i[0].comp_id}".`))}if(!c)return}const y=c.comp_id;if(console.log(),console.log(chalk.white(` ${c.title}`)+chalk.gray(` · ${c.metric}`)),c.description){console.log();for(const o of function(o){const e=[];let n="";for(const l of o.split(/\s+/))""!==n&&n.length+1+l.length>74?(e.push(n),n=l):n=""===n?l:`${n} ${l}`;return""!==n&&e.push(n),e}(c.description))console.log(chalk.white(` ${o}`))}console.log(),console.log(chalk.gray(" Downloading data…"));let m=null;const b=u(c);if(b){const[o,e,n]=await Promise.all([p(b.train),p(b.test),p(b.sample_submission)]);o&&e&&n&&(m={train:o,test:e,sample_submission:n})}if(!m){const o=await h(`/api/icoa/kaggle/${y}/data`);m=o?.data??o}if(!m?.train||!m?.test||!m?.sample_submission)return f();const v=w(y),$={...c,...m.meta??{}};try{e(v,{recursive:!0}),t(s(v,"train.csv"),m.train),t(s(v,"test.csv"),m.test),t(s(v,"sample_submission.csv"),m.sample_submission),t(s(v,"meta.json"),`${JSON.stringify($,null,2)}\n`)}catch{return void console.log(chalk.yellow(" Could not write the data files here — check directory permissions."))}const _=s(v,"starter.ipynb");let S=!1;o(_)?S=!0:t(_,a(g(v,$)));const j="number"==typeof $.n_train?`${$.n_train} rows`:"labelled",K="number"==typeof $.n_test?`${$.n_test} rows`:"predict these";console.log(chalk.green(" ✓ train: ")+chalk.white(s(v,"train.csv"))+chalk.gray(` (${j})`)),console.log(chalk.green(" ✓ test: ")+chalk.white(s(v,"test.csv"))+chalk.gray(` (${K})`)),console.log(chalk.green(" ✓ format: ")+chalk.white(s(v,"sample_submission.csv"))),console.log(chalk.green(" ✓ notebook: ")+chalk.white(_)+chalk.gray(S?" (kept your existing one)":" (runnable baseline inside)")),($.licence||$.attribution)&&console.log(chalk.gray(` licence: ${$.licence??"—"} · ${$.attribution??""}`)),console.log(),console.log(chalk.bold.white(" Your move — 3 steps:")),console.log(chalk.gray(" 1. ")+chalk.bold.cyan(`icoa ipynb ${_}`)+chalk.gray(" → run all (baseline trains)")),console.log(chalk.gray(" 2. it writes ")+chalk.white("submission.csv")+chalk.gray(" → verify with ")+chalk.cyan(`kaggle check ${y}`)),"icoa-holdout"===$.judge?console.log(chalk.gray(" 3. ")+chalk.bold.cyan(`kaggle submit ${y}`)+chalk.gray(" → scored + ranked live.")):console.log(chalk.gray(" 3. upload it on Kaggle yourself — then beat the baseline.")),console.log(),console.log(chalk.bold.white(" 🤝 Want AI help? Set up your cockpit:")),console.log(chalk.gray(" 1. split the screen: ")+chalk.bold.cyan("Ctrl-b %")+chalk.gray(" (lesson/work left · AI right)")),console.log(chalk.gray(" 2. in the right pane: ")+chalk.bold.cyan("icoa")+chalk.gray(" then type ")+chalk.bold.cyan("ai4ioai")+chalk.gray(" → live Gemma 3")),console.log(chalk.gray(" 3. ask it for feature ideas or bug fixes — then ")+chalk.bold.white("VERIFY")+chalk.gray(" with your val score (a tip that lowers it, drop it).")),console.log(),k($),console.log()}finally{l.close()}}function v(e,n){if(e)return e;const t=s(process.cwd(),"icoa-kaggle"),i=o(t)?l(t,{withFileTypes:!0}).filter(e=>e.isDirectory()&&o(s(t,e.name,"sample_submission.csv"))).map(o=>o.name):[];if(1===i.length)return i[0];if(0===i.length)return console.log(chalk.yellow(" No downloaded competition found here — run ")+chalk.cyan("kaggle")+chalk.yellow(" first.")),console.log(),null;console.log(chalk.white(" Several competitions downloaded — pick one:"));for(const o of i)console.log(chalk.gray(` kaggle ${n} `)+chalk.cyan(o));return console.log(),null}export async function runKaggleCheck(e){console.log();const l=v(e,"check");if(!l)return;const t=w(l),i=s(t,"submission.csv"),a=s(t,"sample_submission.csv");if(!o(a))return console.log(chalk.yellow(` No data for "${l}" here — run `)+chalk.cyan(`kaggle ${l}`)+chalk.yellow(" first.")),void console.log();if(!o(i))return console.log(chalk.yellow(" No submission.csv yet — open the notebook and run all cells:")),console.log(chalk.gray(" ")+chalk.cyan(`icoa ipynb ${s(t,"starter.ipynb")}`)),void console.log();const c=m(n(i,"utf-8"),n(a,"utf-8"));let g=null;try{g=JSON.parse(n(s(t,"meta.json"),"utf-8"))}catch{g=null}if(c.ok)console.log(chalk.bold.green(" ✓ submission.csv is Kaggle-ready")+chalk.gray(" (matches sample_submission.csv)")),console.log(chalk.gray(" ")+chalk.white(i)),console.log(),k(g),r(t);else{console.log(chalk.bold.red(" ✗ submission.csv would be rejected:"));for(const o of c.problems)console.log(chalk.yellow(` · ${o}`));console.log(),console.log(chalk.gray(" Fix the submission cell in the notebook and re-run it."))}console.log()}export async function runKaggleSubmit(e){console.log();const l=v(e,"submit");if(!l)return;const t=w(l),r=s(t,"submission.csv"),a=s(t,"sample_submission.csv");let g=null;try{g=JSON.parse(n(s(t,"meta.json"),"utf-8"))}catch{g=null}if(!g||!o(a))return console.log(chalk.yellow(` No data for "${l}" here — run `)+chalk.cyan(`kaggle ${l}`)+chalk.yellow(" first.")),void console.log();if("icoa-holdout"!==g.judge)return console.log(chalk.yellow(" This competition is judged on kaggle.com — nothing to submit here.")),console.log(),k(g),void console.log();if(!o(r))return console.log(chalk.yellow(" No submission.csv yet — open the notebook and run all cells:")),console.log(chalk.gray(" ")+chalk.cyan(`icoa ipynb ${s(t,"starter.ipynb")}`)),void console.log();const u=n(r,"utf-8"),d=m(u,n(a,"utf-8"));if(!d.ok){console.log(chalk.bold.red(" ✗ submission.csv would be rejected:"));for(const o of d.problems)console.log(chalk.yellow(` · ${o}`));return console.log(),console.log(chalk.gray(" Fix the submission cell in the notebook and re-run it.")),void console.log()}let h=c()?.token??"";if(!h){const o=i();try{h=(await o.ask(chalk.cyan(" Your learn token: "))??"").trim()}finally{o.close()}if(!h)return console.log(chalk.gray(" A learn token identifies you on the board — ask your coach for one.")),void console.log()}console.log(chalk.gray(" Scoring on the server…"));const p=await async function(o,e){try{const n=await fetch(b()+o,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e),signal:AbortSignal.timeout(6e4)});return await n.json()}catch{return null}}(`/api/icoa/kaggle/${l}/submit`,{token:h,submission_csv:u}),f=y(p);if(console.log(),f.ok){const o=`${f.metric}: ${Number(f.score).toFixed(4).replace(/\.?0+$/,"")}`;f.deduped?(console.log(chalk.bold.white(` ✓ already scored — ${o}`)),console.log(chalk.gray(" These exact predictions were scored before. Improve the MODEL cell and re-run."))):(console.log(chalk.bold.green(` ✓ scored — ${o}`)),"number"==typeof f.rank&&"number"==typeof f.fieldSize&&console.log(chalk.bold.white(` 🏁 board rank #${f.rank}`)+chalk.gray(` of ${f.fieldSize}`))),f.boardEvent&&console.log(chalk.gray(" standings: ")+chalk.cyan(`board ${f.boardEvent} ${l}`))}else switch(f.reason){case"bad_token":console.log(chalk.bold.red(" ✗ that learn token was not accepted")+chalk.gray(" (invalid or expired)")),console.log(chalk.gray(" Check the token with your coach, then try again."));break;case"format":console.log(chalk.bold.red(" ✗ the server rejected submission.csv:"));for(const o of f.problems??[])console.log(chalk.yellow(` · ${o}`));break;case"event_closed":case"outside_window":console.log(chalk.bold.yellow(" ⏱ the submission window for this competition is closed."));break;case"cap_reached":console.log(chalk.bold.yellow(" ⏱ you have used all your submissions for this competition."));break;case"kaggle_judged":console.log(chalk.yellow(" This competition is judged on kaggle.com — nothing to submit here."));break;default:console.log(chalk.yellow(" The scoring server is not reachable right now — try again in a minute."))}console.log()}export function registerKaggleCommand(o){o.command("kaggle").argument("[action]",'competition id, or "check" / "submit"').argument("[comp]",'competition id (with "check" / "submit")').description("Kaggle-style ML practice — dataset + starter notebook, check & submit").action(async(o,e)=>{"check"===o?await runKaggleCheck(e):"submit"===o?await runKaggleSubmit(e):await runKaggle(o)})}
1
+ import{existsSync as o,mkdirSync as e,readFileSync as l,readdirSync as n,writeFileSync as t}from"node:fs";import{join as s}from"node:path";import chalk from"chalk";import{makeReplAsker as r}from"../lib/repl-asker.js";import{logCommand as i}from"../lib/logger.js";import{openFile as a}from"../lib/open-file.js";import{serializeIpynb as c}from"../lib/notebook-doc.js";import{loadLearnState as g}from"../lib/learn-state.js";import{buildStarterCells as u,dataFileUrls as d,parseKaggleTaskList as y,parseSubmitResponse as m,validateSubmissionText as b}from"../lib/kaggle.js";function h(){return process.env.ICOA_KAGGLE_SERVER?process.env.ICOA_KAGGLE_SERVER.replace(/\/$/,""):"https://practice.icoa2026.au"}async function p(o){try{const e=await fetch(h()+o,{signal:AbortSignal.timeout(3e4)});return await e.json()}catch{return null}}async function f(o){try{const e=await fetch(o,{signal:AbortSignal.timeout(6e4)});return e.ok?await e.text():null}catch{return null}}function w(){console.log(),console.log(chalk.yellow(" The practice server is not reachable right now.")),console.log(chalk.gray(" Check your connection, or try again later.")),console.log()}function k(o){return s(process.cwd(),"icoa-kaggle",o)}function v(o){if("icoa-holdout"===o?.judge)return console.log(chalk.bold.white(" Submit to ICOA (scored on the server):")),console.log(chalk.gray(" ")+chalk.bold.cyan(`kaggle submit ${o.comp_id}`)),void console.log(chalk.gray(" scores vs the secret test labels → live rank on ")+chalk.cyan("board"));console.log(chalk.bold.white(" Submit on Kaggle (manual, your own account):")),o?.kaggle_url?(console.log(chalk.gray(" 1. open ")+chalk.cyan(o.kaggle_url)),console.log(chalk.gray(" 2. Submit Predictions → upload ")+chalk.white("submission.csv")),console.log(chalk.gray(" 3. read your score, improve the MODEL cell, re-run, repeat."))):(console.log(chalk.gray(" (placeholder dataset — no live Kaggle page yet; when the real")),console.log(chalk.gray(" competition dataset lands, its submission link will appear here.)")))}export async function runKaggle(l){const n=r(),a=async o=>await n.ask(o)??"";try{console.log(),console.log(chalk.bold.cyan(" 📊 KAGGLE PRACTICE")+chalk.gray(" · train locally · submit on Kaggle yourself")),console.log(chalk.gray(" Small real-format datasets, a runnable starter notebook, your own CPU.")),console.log();const n=await p("/api/icoa/kaggle/tasks");if(!n)return w();const r=y(n);if(0===r.length)return console.log(chalk.yellow(" No practice competitions are live yet — check back soon.")),void console.log();let g=l?r.find(o=>o.comp_id===l):void 0;if(l&&!g&&console.log(chalk.yellow(` No competition called "${l}".`)),!g){console.log(chalk.white(" Live practice competitions:")),r.forEach((o,e)=>{console.log(chalk.gray(` ${e+1}. `)+chalk.cyan(o.comp_id)+chalk.gray(` — ${o.title} · ${o.metric}`))});for(let o=0;o<3&&!g;o++){const o=(await a(chalk.cyan(" Pick one (number or id): "))).trim();if(!o)return;const e=Number(o);g=!Number.isNaN(e)&&r[e-1]||r.find(e=>e.comp_id===o.toLowerCase())||void 0,g||console.log(chalk.gray(` Try a number 1-${r.length} or an id like "${r[0].comp_id}".`))}if(!g)return}const m=g.comp_id;if(i(`kaggle: download ${m}`),console.log(),console.log(chalk.white(` ${g.title}`)+chalk.gray(` · ${g.metric}`)),g.description){console.log();for(const o of function(o){const e=[];let l="";for(const n of o.split(/\s+/))""!==l&&l.length+1+n.length>74?(e.push(l),l=n):l=""===l?n:`${l} ${n}`;return""!==l&&e.push(l),e}(g.description))console.log(chalk.white(` ${o}`))}console.log(),console.log(chalk.gray(" Downloading data…"));let b=null;const h=d(g);if(h){const[o,e,l]=await Promise.all([f(h.train),f(h.test),f(h.sample_submission)]);o&&e&&l&&(b={train:o,test:e,sample_submission:l})}if(!b){const o=await p(`/api/icoa/kaggle/${m}/data`);b=o?.data??o}if(!b?.train||!b?.test||!b?.sample_submission)return w();const $=k(m),_={...g,...b.meta??{}};try{e($,{recursive:!0}),t(s($,"train.csv"),b.train),t(s($,"test.csv"),b.test),t(s($,"sample_submission.csv"),b.sample_submission),t(s($,"meta.json"),`${JSON.stringify(_,null,2)}\n`)}catch{return void console.log(chalk.yellow(" Could not write the data files here — check directory permissions."))}const j=s($,"starter.ipynb");let S=!1;o(j)?S=!0:t(j,c(u($,_)));const K="number"==typeof _.n_train?`${_.n_train} rows`:"labelled",N="number"==typeof _.n_test?`${_.n_test} rows`:"predict these";console.log(chalk.green(" ✓ train: ")+chalk.white(s($,"train.csv"))+chalk.gray(` (${K})`)),console.log(chalk.green(" ✓ test: ")+chalk.white(s($,"test.csv"))+chalk.gray(` (${N})`)),console.log(chalk.green(" ✓ format: ")+chalk.white(s($,"sample_submission.csv"))),console.log(chalk.green(" ✓ notebook: ")+chalk.white(j)+chalk.gray(S?" (kept your existing one)":" (runnable baseline inside)")),(_.licence||_.attribution)&&console.log(chalk.gray(` licence: ${_.licence??"—"} · ${_.attribution??""}`)),console.log(),console.log(chalk.bold.white(" Your move — 3 steps:")),console.log(chalk.gray(" 1. ")+chalk.bold.cyan(`icoa ipynb ${j}`)+chalk.gray(" → run all (baseline trains)")),console.log(chalk.gray(" 2. it writes ")+chalk.white("submission.csv")+chalk.gray(" → verify with ")+chalk.cyan(`kaggle check ${m}`)),"icoa-holdout"===_.judge?console.log(chalk.gray(" 3. ")+chalk.bold.cyan(`kaggle submit ${m}`)+chalk.gray(" → scored + ranked live.")):console.log(chalk.gray(" 3. upload it on Kaggle yourself — then beat the baseline.")),console.log(),console.log(chalk.bold.white(" 🤝 Want AI help? Set up your cockpit:")),console.log(chalk.gray(" 1. split the screen: ")+chalk.bold.cyan("Ctrl-b %")+chalk.gray(" (lesson/work left · AI right)")),console.log(chalk.gray(" 2. in the right pane: ")+chalk.bold.cyan("icoa")+chalk.gray(" then type ")+chalk.bold.cyan("ai4ioai")+chalk.gray(" → live Gemma 3")),console.log(chalk.gray(" 3. ask it for feature ideas or bug fixes — then ")+chalk.bold.white("VERIFY")+chalk.gray(" with your val score (a tip that lowers it, drop it).")),console.log(),v(_),console.log()}finally{n.close()}}function $(e,l){if(e)return e;const t=s(process.cwd(),"icoa-kaggle"),r=o(t)?n(t,{withFileTypes:!0}).filter(e=>e.isDirectory()&&o(s(t,e.name,"sample_submission.csv"))).map(o=>o.name):[];if(1===r.length)return r[0];if(0===r.length)return console.log(chalk.yellow(" No downloaded competition found here — run ")+chalk.cyan("kaggle")+chalk.yellow(" first.")),console.log(),null;console.log(chalk.white(" Several competitions downloaded — pick one:"));for(const o of r)console.log(chalk.gray(` kaggle ${l} `)+chalk.cyan(o));return console.log(),null}export async function runKaggleCheck(e){console.log();const n=$(e,"check");if(!n)return;const t=k(n),r=s(t,"submission.csv"),c=s(t,"sample_submission.csv");if(!o(c))return console.log(chalk.yellow(` No data for "${n}" here — run `)+chalk.cyan(`kaggle ${n}`)+chalk.yellow(" first.")),void console.log();if(!o(r))return console.log(chalk.yellow(" No submission.csv yet — open the notebook and run all cells:")),console.log(chalk.gray(" ")+chalk.cyan(`icoa ipynb ${s(t,"starter.ipynb")}`)),void console.log();const g=b(l(r,"utf-8"),l(c,"utf-8"));i(`kaggle: check ${n} → ${g.ok?"ok":`rejected: ${g.problems[0]??"format"}`}`);let u=null;try{u=JSON.parse(l(s(t,"meta.json"),"utf-8"))}catch{u=null}if(g.ok)console.log(chalk.bold.green(" ✓ submission.csv is Kaggle-ready")+chalk.gray(" (matches sample_submission.csv)")),console.log(chalk.gray(" ")+chalk.white(r)),console.log(),v(u),a(t);else{console.log(chalk.bold.red(" ✗ submission.csv would be rejected:"));for(const o of g.problems)console.log(chalk.yellow(` · ${o}`));console.log(),console.log(chalk.gray(" Fix the submission cell in the notebook and re-run it."))}console.log()}export async function runKaggleSubmit(e){console.log();const n=$(e,"submit");if(!n)return;const t=k(n),a=s(t,"submission.csv"),c=s(t,"sample_submission.csv");let u=null;try{u=JSON.parse(l(s(t,"meta.json"),"utf-8"))}catch{u=null}if(!u||!o(c))return console.log(chalk.yellow(` No data for "${n}" here — run `)+chalk.cyan(`kaggle ${n}`)+chalk.yellow(" first.")),void console.log();if("icoa-holdout"!==u.judge)return console.log(chalk.yellow(" This competition is judged on kaggle.com — nothing to submit here.")),console.log(),v(u),void console.log();if(!o(a))return console.log(chalk.yellow(" No submission.csv yet — open the notebook and run all cells:")),console.log(chalk.gray(" ")+chalk.cyan(`icoa ipynb ${s(t,"starter.ipynb")}`)),void console.log();const d=l(a,"utf-8"),y=b(d,l(c,"utf-8"));if(!y.ok){i(`kaggle: submit ${n} → local-reject: ${y.problems[0]??"format"}`),console.log(chalk.bold.red(" ✗ submission.csv would be rejected:"));for(const o of y.problems)console.log(chalk.yellow(` · ${o}`));return console.log(),console.log(chalk.gray(" Fix the submission cell in the notebook and re-run it.")),void console.log()}let p=g()?.token??"";if(!p){const o=r();try{p=(await o.ask(chalk.cyan(" Your learn token: "))??"").trim()}finally{o.close()}if(!p)return console.log(chalk.gray(" A learn token identifies you on the board — ask your coach for one.")),void console.log()}console.log(chalk.gray(" Scoring on the server…"));const f=await async function(o,e){try{const l=await fetch(h()+o,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e),signal:AbortSignal.timeout(6e4)});return await l.json()}catch{return null}}(`/api/icoa/kaggle/${n}/submit`,{token:p,submission_csv:d}),w=m(f);if(i(`kaggle: submit ${n} → ${w.ok?`${w.metric}=${w.score}${w.deduped?" (deduped)":""} rank=${w.rank??"-"}`:w.reason??"error"}`),console.log(),w.ok){const o=`${w.metric}: ${Number(w.score).toFixed(4).replace(/\.?0+$/,"")}`;w.deduped?(console.log(chalk.bold.white(` ✓ already scored — ${o}`)),console.log(chalk.gray(" These exact predictions were scored before. Improve the MODEL cell and re-run."))):(console.log(chalk.bold.green(` ✓ scored — ${o}`)),"number"==typeof w.rank&&"number"==typeof w.fieldSize&&console.log(chalk.bold.white(` 🏁 board rank #${w.rank}`)+chalk.gray(` of ${w.fieldSize}`))),w.boardEvent&&console.log(chalk.gray(" standings: ")+chalk.cyan(`board ${w.boardEvent} ${n}`))}else switch(w.reason){case"bad_token":console.log(chalk.bold.red(" ✗ that learn token was not accepted")+chalk.gray(" (invalid or expired)")),console.log(chalk.gray(" Check the token with your coach, then try again."));break;case"format":console.log(chalk.bold.red(" ✗ the server rejected submission.csv:"));for(const o of w.problems??[])console.log(chalk.yellow(` · ${o}`));break;case"event_closed":case"outside_window":console.log(chalk.bold.yellow(" ⏱ the submission window for this competition is closed."));break;case"cap_reached":console.log(chalk.bold.yellow(" ⏱ you have used all your submissions for this competition."));break;case"kaggle_judged":console.log(chalk.yellow(" This competition is judged on kaggle.com — nothing to submit here."));break;default:console.log(chalk.yellow(" The scoring server is not reachable right now — try again in a minute."))}console.log()}export function registerKaggleCommand(o){o.command("kaggle").argument("[action]",'competition id, or "check" / "submit"').argument("[comp]",'competition id (with "check" / "submit")').description("Kaggle-style ML practice — dataset + starter notebook, check & submit").action(async(o,e)=>{"check"===o?await runKaggleCheck(e):"submit"===o?await runKaggleSubmit(e):await runKaggle(o)})}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "icoa-cli",
3
- "version": "2.19.390",
3
+ "version": "2.19.391",
4
4
  "description": "ICOA CLI — The world's first CLI-native cyber & AI security olympiad terminal: AI4CTF (Day 1), CTF4AI (Day 2), VLA4CTF (Pioneer Round — embodied AI)",
5
5
  "type": "module",
6
6
  "bin": {