[{"data":1,"prerenderedAt":860},["ShallowReactive",2],{"docs-\u002Fdocs\u002Fv2\u002Fcognets\u002Ffolder\u002Fmain":3},{"id":4,"title":5,"body":6,"description":853,"extension":854,"meta":855,"navigation":94,"path":856,"seo":857,"stem":858,"__hash__":859},"docs\u002Fdocs\u002Fv2\u002Fcognets\u002Ffolder\u002Fmain.md","src\u002Fmain.ts",{"type":7,"value":8,"toc":845},"minimark",[9,13,22,30,309,312,317,324,352,355,359,365,411,414,426,430,441,522,529,533,544,603,610,648,651,655,708,715,719,726,834,841],[10,11,5],"h1",{"id":12},"srcmaints",[14,15,16,17,21],"p",{},"This file ",[18,19,20],"strong",{},"is"," the brain.",[14,23,24,25,29],{},"Not a module exporting a factory, not a class implementing an interface. A raw script\nthat runs once at load, with the ambient globals live, and declares exactly one ",[26,27,28],"code",{},"loop()",".",[31,32,37],"pre",{"className":33,"code":34,"language":35,"meta":36,"style":36},"language-ts shiki shiki-themes dark-plus","import { render } from \".\u002Frender\"\nimport { state, sync } from \".\u002Fstate\"\n\nloop(async ({ signal, stop }) => {\n    await phase(\"sync\", async () => {\n        sync()\n    })\n\n    const messages = await phase(\"render\", async () => {\n        return render({\n            base: await kernel.base(),\n            scope: kernel.scope(),\n            history: state.entries,\n        })\n    })\n\n    \u002F\u002F ...\n    stop()\n})\n","ts","",[26,38,39,66,89,96,130,155,164,170,175,208,220,240,255,272,278,283,288,295,303],{"__ignoreMap":36},[40,41,44,48,52,56,59,62],"span",{"class":42,"line":43},"line",1,[40,45,47],{"class":46},"sYbnZ","import",[40,49,51],{"class":50},"sTNBD"," { ",[40,53,55],{"class":54},"s3F5K","render",[40,57,58],{"class":50}," } ",[40,60,61],{"class":46},"from",[40,63,65],{"class":64},"sKc5r"," \".\u002Frender\"\n",[40,67,69,71,73,76,79,82,84,86],{"class":42,"line":68},2,[40,70,47],{"class":46},[40,72,51],{"class":50},[40,74,75],{"class":54},"state",[40,77,78],{"class":50},", ",[40,80,81],{"class":54},"sync",[40,83,58],{"class":50},[40,85,61],{"class":46},[40,87,88],{"class":64}," \".\u002Fstate\"\n",[40,90,92],{"class":42,"line":91},3,[40,93,95],{"emptyLinePlaceholder":94},true,"\n",[40,97,99,103,106,110,113,116,118,121,124,127],{"class":42,"line":98},4,[40,100,102],{"class":101},"sCudf","loop",[40,104,105],{"class":50},"(",[40,107,109],{"class":108},"scz_3","async",[40,111,112],{"class":50}," ({ ",[40,114,115],{"class":54},"signal",[40,117,78],{"class":50},[40,119,120],{"class":54},"stop",[40,122,123],{"class":50}," }) ",[40,125,126],{"class":108},"=>",[40,128,129],{"class":50}," {\n",[40,131,133,136,139,141,144,146,148,151,153],{"class":42,"line":132},5,[40,134,135],{"class":46},"    await",[40,137,138],{"class":101}," phase",[40,140,105],{"class":50},[40,142,143],{"class":64},"\"sync\"",[40,145,78],{"class":50},[40,147,109],{"class":108},[40,149,150],{"class":50}," () ",[40,152,126],{"class":108},[40,154,129],{"class":50},[40,156,158,161],{"class":42,"line":157},6,[40,159,160],{"class":101},"        sync",[40,162,163],{"class":50},"()\n",[40,165,167],{"class":42,"line":166},7,[40,168,169],{"class":50},"    })\n",[40,171,173],{"class":42,"line":172},8,[40,174,95],{"emptyLinePlaceholder":94},[40,176,178,181,185,188,191,193,195,198,200,202,204,206],{"class":42,"line":177},9,[40,179,180],{"class":108},"    const",[40,182,184],{"class":183},"s9McN"," messages",[40,186,187],{"class":50}," = ",[40,189,190],{"class":46},"await",[40,192,138],{"class":101},[40,194,105],{"class":50},[40,196,197],{"class":64},"\"render\"",[40,199,78],{"class":50},[40,201,109],{"class":108},[40,203,150],{"class":50},[40,205,126],{"class":108},[40,207,129],{"class":50},[40,209,211,214,217],{"class":42,"line":210},10,[40,212,213],{"class":46},"        return",[40,215,216],{"class":101}," render",[40,218,219],{"class":50},"({\n",[40,221,223,226,229,232,234,237],{"class":42,"line":222},11,[40,224,225],{"class":54},"            base:",[40,227,228],{"class":46}," await",[40,230,231],{"class":54}," kernel",[40,233,29],{"class":50},[40,235,236],{"class":101},"base",[40,238,239],{"class":50},"(),\n",[40,241,243,246,248,250,253],{"class":42,"line":242},12,[40,244,245],{"class":54},"            scope:",[40,247,231],{"class":54},[40,249,29],{"class":50},[40,251,252],{"class":101},"scope",[40,254,239],{"class":50},[40,256,258,261,264,266,269],{"class":42,"line":257},13,[40,259,260],{"class":54},"            history:",[40,262,263],{"class":54}," state",[40,265,29],{"class":50},[40,267,268],{"class":54},"entries",[40,270,271],{"class":50},",\n",[40,273,275],{"class":42,"line":274},14,[40,276,277],{"class":50},"        })\n",[40,279,281],{"class":42,"line":280},15,[40,282,169],{"class":50},[40,284,286],{"class":42,"line":285},16,[40,287,95],{"emptyLinePlaceholder":94},[40,289,291],{"class":42,"line":290},17,[40,292,294],{"class":293},"sOLPB","    \u002F\u002F ...\n",[40,296,298,301],{"class":42,"line":297},18,[40,299,300],{"class":101},"    stop",[40,302,163],{"class":50},[40,304,306],{"class":42,"line":305},19,[40,307,308],{"class":50},"})\n",[14,310,311],{},"Ordinary imports work. Module scope works. The only rule is that the script declares one\nloop, and does it by the time it finishes running.",[313,314,316],"h2",{"id":315},"why-a-script-not-a-module","Why a script, not a module",[14,318,319,320,323],{},"The authoring surface is two clutter-free files, and this is the one with the thinking in\nit. There's no ",[26,321,322],{},"defineCognet({ load, wake, unload })"," boilerplate to write because the\ncompile step generates it:",[325,326,327,335,346],"ol",{},[328,329,330,331,334],"li",{},"It ",[18,332,333],{},"wraps"," this file — hoisting the imports out, deferring the body into a callable.",[328,336,330,337,340,341,345],{},[18,338,339],{},"generates an entry"," that imports the host ",[342,343,344],"em",{},"first",", so the ambient globals are\ninstalled before your code evaluates.",[328,347,330,348,351],{},[18,349,350],{},"composes"," host + config + main into the definition the kernel loads.",[14,353,354],{},"The desugared form is an ordinary definition against the ABI. There is no side channel.",[313,356,358],{"id":357},"module-scope-is-resident-ram","Module scope is resident RAM",[14,360,361,362,29],{},"Everything this file holds in module or closure scope lives for the life of the process:\nalive across wakes, rebuildable from the log, and gone without loss on ",[26,363,364],{},"kill -9",[31,366,368],{"className":33,"code":367,"language":35,"meta":36,"style":36},"import { state } from \".\u002Fstate\"   \u002F\u002F the resident model\n\nconst encoder = buildEncoder()    \u002F\u002F constructed once, reused every wake\n",[26,369,370,388,392],{"__ignoreMap":36},[40,371,372,374,376,378,380,382,385],{"class":42,"line":43},[40,373,47],{"class":46},[40,375,51],{"class":50},[40,377,75],{"class":54},[40,379,58],{"class":50},[40,381,61],{"class":46},[40,383,384],{"class":64}," \".\u002Fstate\"",[40,386,387],{"class":293},"   \u002F\u002F the resident model\n",[40,389,390],{"class":42,"line":68},[40,391,95],{"emptyLinePlaceholder":94},[40,393,394,397,400,402,405,408],{"class":42,"line":91},[40,395,396],{"class":108},"const",[40,398,399],{"class":183}," encoder",[40,401,187],{"class":50},[40,403,404],{"class":101},"buildEncoder",[40,406,407],{"class":50},"()    ",[40,409,410],{"class":293},"\u002F\u002F constructed once, reused every wake\n",[14,412,413],{},"Anything expensive to build belongs at the top level, not inside the loop. It's\nconstructed once and every wake uses it.",[14,415,416,417,420,421,29],{},"The rule that makes this safe: ",[18,418,419],{},"nothing up here is authoritative."," If losing it on a\nrestart would lose information, it belonged in the log. See\n",[422,423,425],"a",{"href":424},"\u002Fdocs\u002Fv2\u002Fcognets\u002Fengine\u002Fmemory","Memory",[313,427,429],{"id":428},"do-work-inside-the-loop","Do work inside the loop",[14,431,432,433,436,437,440],{},"The kernel binds at ",[26,434,435],{},"load()",", and this script runs ",[342,438,439],{},"during"," load. So the globals exist,\nbut reaching for kernel state at module top level is a mistake:",[31,442,444],{"className":33,"code":443,"language":35,"meta":36,"style":36},"\u002F\u002F wrong — runs during load, before any wake exists\nconst scope = kernel.scope()\n\nloop(async ({ stop }) => {\n    \u002F\u002F right — runs inside a wake\n    const scope = kernel.scope()\n    stop()\n})\n",[26,445,446,451,469,473,491,496,512,518],{"__ignoreMap":36},[40,447,448],{"class":42,"line":43},[40,449,450],{"class":293},"\u002F\u002F wrong — runs during load, before any wake exists\n",[40,452,453,455,458,460,463,465,467],{"class":42,"line":68},[40,454,396],{"class":108},[40,456,457],{"class":183}," scope",[40,459,187],{"class":50},[40,461,462],{"class":54},"kernel",[40,464,29],{"class":50},[40,466,252],{"class":101},[40,468,163],{"class":50},[40,470,471],{"class":42,"line":91},[40,472,95],{"emptyLinePlaceholder":94},[40,474,475,477,479,481,483,485,487,489],{"class":42,"line":98},[40,476,102],{"class":101},[40,478,105],{"class":50},[40,480,109],{"class":108},[40,482,112],{"class":50},[40,484,120],{"class":54},[40,486,123],{"class":50},[40,488,126],{"class":108},[40,490,129],{"class":50},[40,492,493],{"class":42,"line":132},[40,494,495],{"class":293},"    \u002F\u002F right — runs inside a wake\n",[40,497,498,500,502,504,506,508,510],{"class":42,"line":157},[40,499,180],{"class":108},[40,501,457],{"class":183},[40,503,187],{"class":50},[40,505,462],{"class":54},[40,507,29],{"class":50},[40,509,252],{"class":101},[40,511,163],{"class":50},[40,513,514,516],{"class":42,"line":166},[40,515,300],{"class":101},[40,517,163],{"class":50},[40,519,520],{"class":42,"line":172},[40,521,308],{"class":50},[14,523,524,525,528],{},"Syscalls that need a running wake throw ",[26,526,527],{},"SYSCALL_OUTSIDE_RUN"," when called outside one.\nConstruct your own objects at the top level; ask the kernel things inside the loop.",[313,530,532],{"id":531},"the-ambient-globals","The ambient globals",[14,534,535,536,539,540,543],{},"Typed by ",[26,537,538],{},".cognet\u002Fglobals.d.ts",", which ",[26,541,542],{},"axon prepare"," regenerates:",[545,546,547,560],"table",{},[548,549,550],"thead",{},[551,552,553,557],"tr",{},[554,555,556],"th",{},"Global",[554,558,559],{},"What it is",[561,562,563,574,583,593],"tbody",{},[551,564,565,571],{},[566,567,568],"td",{},[26,569,570],{},"loop(body)",[566,572,573],{},"Declare the tick pipeline. Exactly once.",[551,575,576,580],{},[566,577,578],{},[26,579,462],{},[566,581,582],{},"The syscall table — the seven verbs.",[551,584,585,590],{},[566,586,587],{},[26,588,589],{},"phase(name, fn)",[566,591,592],{},"A named stage within a tick.",[551,594,595,600],{},[566,596,597],{},[26,598,599],{},"system(name, fn)",[566,601,602],{},"A timed unit of work within a phase.",[14,604,605,606,609],{},"Wake-scoped things are ",[18,607,608],{},"not"," globals. They arrive as arguments to the loop body:",[31,611,613],{"className":33,"code":612,"language":35,"meta":36,"style":36},"loop(async ({ stimuli, signal, stop }) => { \u002F* ... *\u002F })\n",[26,614,615],{"__ignoreMap":36},[40,616,617,619,621,623,625,628,630,632,634,636,638,640,642,645],{"class":42,"line":43},[40,618,102],{"class":101},[40,620,105],{"class":50},[40,622,109],{"class":108},[40,624,112],{"class":50},[40,626,627],{"class":54},"stimuli",[40,629,78],{"class":50},[40,631,115],{"class":54},[40,633,78],{"class":50},[40,635,120],{"class":54},[40,637,123],{"class":50},[40,639,126],{"class":108},[40,641,51],{"class":50},[40,643,644],{"class":293},"\u002F* ... *\u002F",[40,646,647],{"class":50}," })\n",[14,649,650],{},"That split is deliberate. Process-lifetime things are ambient; anything belonging to one\nwake is passed in, so it can never leak into the next one.",[313,652,654],{"id":653},"one-loop-declared-once","One loop, declared once",[31,656,658],{"className":33,"code":657,"language":35,"meta":36,"style":36},"loop(async ({ stop }) => { \u002F* ... *\u002F })\nloop(async ({ stop }) => { \u002F* ... *\u002F })   \u002F\u002F throws COGNET_LOOP_ALREADY_DECLARED\n",[26,659,660,682],{"__ignoreMap":36},[40,661,662,664,666,668,670,672,674,676,678,680],{"class":42,"line":43},[40,663,102],{"class":101},[40,665,105],{"class":50},[40,667,109],{"class":108},[40,669,112],{"class":50},[40,671,120],{"class":54},[40,673,123],{"class":50},[40,675,126],{"class":108},[40,677,51],{"class":50},[40,679,644],{"class":293},[40,681,647],{"class":50},[40,683,684,686,688,690,692,694,696,698,700,702,705],{"class":42,"line":68},[40,685,102],{"class":101},[40,687,105],{"class":50},[40,689,109],{"class":108},[40,691,112],{"class":50},[40,693,120],{"class":54},[40,695,123],{"class":50},[40,697,126],{"class":108},[40,699,51],{"class":50},[40,701,644],{"class":293},[40,703,704],{"class":50}," })   ",[40,706,707],{"class":293},"\u002F\u002F throws COGNET_LOOP_ALREADY_DECLARED\n",[14,709,710,711,714],{},"Running ",[26,712,713],{},"main()"," without declaring a loop throws too. A brain with no loop and a brain\nwith two are both incoherent, and both fail at load rather than at the first wake.",[313,716,718],{"id":717},"structure-it-however-you-like","Structure it however you like",[14,720,721,722,725],{},"The loop body is yours. ",[26,723,724],{},"zero"," uses four phases in a fixed order, but nothing enforces\nthat shape:",[31,727,729],{"className":33,"code":728,"language":35,"meta":36,"style":36},"loop(async ({ stop }) => {\n    await phase(\"sense\", async () => { \u002F* ... *\u002F })\n    await phase(\"think\", async () => { \u002F* ... *\u002F })\n    await phase(\"act\", async () => { \u002F* ... *\u002F })\n    stop()\n})\n",[26,730,731,749,774,799,824,830],{"__ignoreMap":36},[40,732,733,735,737,739,741,743,745,747],{"class":42,"line":43},[40,734,102],{"class":101},[40,736,105],{"class":50},[40,738,109],{"class":108},[40,740,112],{"class":50},[40,742,120],{"class":54},[40,744,123],{"class":50},[40,746,126],{"class":108},[40,748,129],{"class":50},[40,750,751,753,755,757,760,762,764,766,768,770,772],{"class":42,"line":68},[40,752,135],{"class":46},[40,754,138],{"class":101},[40,756,105],{"class":50},[40,758,759],{"class":64},"\"sense\"",[40,761,78],{"class":50},[40,763,109],{"class":108},[40,765,150],{"class":50},[40,767,126],{"class":108},[40,769,51],{"class":50},[40,771,644],{"class":293},[40,773,647],{"class":50},[40,775,776,778,780,782,785,787,789,791,793,795,797],{"class":42,"line":91},[40,777,135],{"class":46},[40,779,138],{"class":101},[40,781,105],{"class":50},[40,783,784],{"class":64},"\"think\"",[40,786,78],{"class":50},[40,788,109],{"class":108},[40,790,150],{"class":50},[40,792,126],{"class":108},[40,794,51],{"class":50},[40,796,644],{"class":293},[40,798,647],{"class":50},[40,800,801,803,805,807,810,812,814,816,818,820,822],{"class":42,"line":98},[40,802,135],{"class":46},[40,804,138],{"class":101},[40,806,105],{"class":50},[40,808,809],{"class":64},"\"act\"",[40,811,78],{"class":50},[40,813,109],{"class":108},[40,815,150],{"class":50},[40,817,126],{"class":108},[40,819,51],{"class":50},[40,821,644],{"class":293},[40,823,647],{"class":50},[40,825,826,828],{"class":42,"line":132},[40,827,300],{"class":101},[40,829,163],{"class":50},[40,831,832],{"class":42,"line":157},[40,833,308],{"class":50},[14,835,836,837,29],{},"Branch, loop, call other functions, split across files. Phases are how you name and time\nthe stages — not a pipeline the engine imposes. See\n",[422,838,840],{"href":839},"\u002Fdocs\u002Fv2\u002Fcognets\u002Fengine\u002Floop","The Loop",[842,843,844],"style",{},"html pre.shiki code .sYbnZ, html code.shiki .sYbnZ{--shiki-default:#C586C0}html pre.shiki code .sTNBD, html code.shiki .sTNBD{--shiki-default:#D4D4D4}html pre.shiki code .s3F5K, html code.shiki .s3F5K{--shiki-default:#9CDCFE}html pre.shiki code .sKc5r, html code.shiki .sKc5r{--shiki-default:#CE9178}html pre.shiki code .sCudf, html code.shiki .sCudf{--shiki-default:#DCDCAA}html pre.shiki code .scz_3, html code.shiki .scz_3{--shiki-default:#569CD6}html pre.shiki code .s9McN, html code.shiki .s9McN{--shiki-default:#4FC1FF}html pre.shiki code .sOLPB, html code.shiki .sOLPB{--shiki-default:#6A9955}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}",{"title":36,"searchDepth":68,"depth":68,"links":846},[847,848,849,850,851,852],{"id":315,"depth":68,"text":316},{"id":357,"depth":68,"text":358},{"id":428,"depth":68,"text":429},{"id":531,"depth":68,"text":532},{"id":653,"depth":68,"text":654},{"id":717,"depth":68,"text":718},"The brain — a raw script that declares one loop.","md",{},"\u002Fdocs\u002Fv2\u002Fcognets\u002Ffolder\u002Fmain",{"title":5,"description":853},"docs\u002Fv2\u002Fcognets\u002Ffolder\u002Fmain","A5emFXvFx7GqoqKy2jg5PH9_wHdIyG-p5hYK71jjZHI",1785156432090]