omo-ai 5.1.43 → 5.1.44
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/lib/provider-map.json +1 -1
- package/package.json +2 -2
- package/plugin/CHANGELOG.md +4 -0
- package/plugin/extensions/gateway-rules-extension.mjs +1 -1
- package/plugin/extensions/gateway-store-worker.mjs +1 -1
- package/plugin/extensions/memory-run-supervisor.mjs +1 -1
- package/plugin/extensions/omo-claim-receipt.js +1 -1
- package/plugin/extensions/omo-computer-use.js +1 -1
- package/plugin/extensions/omo-feedback.js +1 -1
- package/plugin/extensions/omo-init-deep-advisor.js +1 -1
- package/plugin/extensions/omo-member.js +1 -1
- package/plugin/extensions/omo-memory-doctor.js +1 -1
- package/plugin/extensions/omo-memory-memfs.js +1 -1
- package/plugin/extensions/omo-memory-palace.js +1 -1
- package/plugin/extensions/omo-memory-sync.js +1 -1
- package/plugin/extensions/omo-onboarding-scan.js +2 -2
- package/plugin/extensions/omo-recall-store.js +1 -1
- package/plugin/extensions/omo-session-history.js +1 -1
- package/plugin/extensions/omo-side-panel.js +1 -1
- package/plugin/extensions/omo-task.js +1 -1
- package/plugin/extensions/omo-telemetry-posthog.js +1 -1
- package/plugin/extensions/omo-thread-client.js +1 -1
- package/plugin/extensions/omo.js +3 -3
- package/plugin/package.json +1 -1
- package/plugin/runtime/agent-toolkit-sdk/sdk.js +2 -2
- package/plugin/runtime/ast-grep-mcp/manifest.json +1 -1
- package/plugin/runtime/rollback-migrate.js +1 -1
- package/plugin/runtime/thread-sdk/sdk.js +2 -2
- package/plugin/skills/browser/runtime/omowright/manifest.json +1 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"_comment": "Derived from @code-yeongyu/senpi 2026.10.21-
|
|
2
|
+
"_comment": "Derived from @code-yeongyu/senpi 2026.10.21-3 builtinProviders(). builtinProviderIds and oauthProviderIds are the engine registry verbatim; providers maps a source harness id onto the omo provider that serves the same endpoint; oauthLogins maps a source OAuth id onto the omo provider to run /login for. Re-derive all four whenever the senpi pin changes.",
|
|
3
3
|
"builtinProviderIds": [
|
|
4
4
|
"alibaba-token-plan",
|
|
5
5
|
"amazon-bedrock",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "omo-ai",
|
|
3
|
-
"version": "5.1.
|
|
3
|
+
"version": "5.1.44",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "OmO Native - the standalone omo command with the OMO extension built in: bun add -g omo-ai",
|
|
6
6
|
"bin": {
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"postinstall": "node bin/senpi-patch.mjs"
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@code-yeongyu/senpi": "2026.10.21-
|
|
20
|
+
"@code-yeongyu/senpi": "2026.10.21-3"
|
|
21
21
|
},
|
|
22
22
|
"repository": {
|
|
23
23
|
"type": "git",
|
package/plugin/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [5.1.44] - 2026-10-23
|
|
11
|
+
|
|
12
|
+
**The engine moves to senpi 2026.10.21-3: long 1M-context conversations stop re-writing their cached prefix, Anthropic subscription context is counted once, and "No credential slots available" no longer locks every account out ([#10965](https://github.com/code-yeongyu/oh-my-openagent/pull/10965)).** When context reduction trims a long conversation on a 1M window, the reduced prefix now stays identical and keeps its cache checkpoint, so each step writes only what changed. Anthropic subscription turns with internal SDK re-requests no longer double the context percentage or start compaction early, and a configured compaction model now summarizes while your session provider is healthy, overflow recovery included. The subscription lane is the only layer that picks its accounts, so one revoked or briefly rate-limited account no longer blocks the rest until you log in again. Every turn end says what happens next, a turn-end report names the hold your queued message released, the trailing "Anything else? (optional)" card is gone from clients without native questions, and moving a session between machines runs through one implementation. Forced recovery after a context overflow no longer picks the same compaction cut again when an invisible entry leads the session; an overflow still never moves a task to its next fallback model.
|
|
13
|
+
|
|
10
14
|
## [5.1.43] - 2026-10-22
|
|
11
15
|
|
|
12
16
|
**A session's first prompt no longer freezes a shared host while it reads what changed in memory ([#10728](https://github.com/code-yeongyu/oh-my-openagent/pull/10728)).** A session resumed a few hours after its last turn read the memory commits since then with one `git diff-tree` process per commit, all started at once. On a busy memory repository that was thousands of processes, and starting them blocked every session on the same host for 28 s with one session and 142 s with seven, while the finished ones piled up unreaped. The range is now read with one `git log`; the same notices come out, the longest block with seven sessions fell to 16 s, and the host's memory footprint during prompts fell from a median of 1,258 MB to 813 MB. ([#10726](https://github.com/code-yeongyu/oh-my-openagent/issues/10726))
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
// omo:
|
|
1
|
+
// omo:CxZUq-5XRH6hyoqlp1sIynILZjYN6Duu0jcYWWO6sps:5LLt7853QZ0R275mM__hYa8UeDUj-ySee_PqmOY8R7U
|
|
2
2
|
function e(e){return e.replaceAll("&","&").replaceAll("<","<").replaceAll(">",">")}function r(r,s){let i=[`<operating-rules version="${e(r).replaceAll('"',""")}">`];for(let r of s)i.push(`- ${e(r)}`);return i.push("</operating-rules>"),i.join("\n")}function s(e,r){if(null===e||"object"!=typeof e||Array.isArray(e))throw Error(`${r} must be an object`);return e}function i(e,r){let s=e[r];if("string"!=typeof s||0===s.length)throw Error(`${r} must be a non-empty string`);return s}function o(e){let r=s(e,"a rules target"),o=i(r,"session_durable_id"),n=r.binding_id;if(null!=n&&"string"!=typeof n)throw Error("binding_id must be a string");let t=r.behavioral;if(null===t)return{session_durable_id:o,binding_id:n??void 0,behavioral:null};if(!Array.isArray(t)||!t.every(e=>"string"==typeof e))throw Error("behavioral must be an array of rule texts, or null to clear the session's rules");return{session_durable_id:o,binding_id:n??void 0,behavioral:t}}function n(e,r){let o=i(s(r,"blockForSession args"),"session_durable_id"),n=e.one(["version","block"],"SELECT version, block FROM gateway_rules_blocks WHERE session_durable_id = ?",[o]);return void 0===n?null:{version:String(n.version),block:String(n.block)}}function t(e,r){let o=i(s(r,"sessionsWithRules args"),"scope");return{sessions:e.all(["session_durable_id","version"],"SELECT session_durable_id, version FROM gateway_rules_blocks WHERE scope = ?",[o],"session_durable_id").map(e=>({session_durable_id:String(e.session_durable_id),version:String(e.version)}))}}async function l(n,t){let l=s(t,"rulesCommitted args"),a=i(l,"scope"),d=i(l,"version"),u=l.now??Date.now();if("number"!=typeof u||!Number.isFinite(u))throw Error("now must be a finite timestamp");if(!Array.isArray(l.targets))throw Error("targets must be an array");let _=[];for(let s of l.targets){let i=o(s);if(null===i.behavioral){let e=n.exec("DELETE FROM gateway_rules_blocks WHERE session_durable_id = ?",[i.session_durable_id]);_.push({session_durable_id:i.session_durable_id,outcome:e>0?"removed":"absent"});continue}let t=r(d,i.behavioral),l=n.exec("INSERT INTO gateway_rules_blocks (session_durable_id, scope, version, block, updated_at) VALUES (?, ?, ?, ?, ?)\n ON CONFLICT(session_durable_id) DO UPDATE SET scope = excluded.scope, version = excluded.version, block = excluded.block, updated_at = excluded.updated_at\n WHERE gateway_rules_blocks.version != excluded.version",[i.session_durable_id,a,d,t,u]);if(void 0===i.binding_id){_.push({session_durable_id:i.session_durable_id,outcome:l>0?"updated":"unchanged"});continue}let c=await n.enqueue({binding_id:i.binding_id,event_id:`rules_changed:${a}:${d}:${i.session_durable_id}`,text:[`<operating-rules-changed scope="${e(a).replaceAll('"',""")}" version="${e(d).replaceAll('"',""")}">`,"The operating rules for this gateway scope changed; this session's system prompt carries the new block from its next turn.","</operating-rules-changed>"].join("\n"),mode:"follow_up"});_.push({session_durable_id:i.session_durable_id,outcome:l>0?"updated":"unchanged",delivery:"ok"===c.kind?{kind:"ok",delivery_id:c.delivery_id,deduplicated:!0===c.deduplicated}:{kind:"error",error:c.error}})}return{scope:a,version:d,outcomes:_}}export{n as blockForSession,l as rulesCommitted,t as sessionsWithRules};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
// omo:
|
|
1
|
+
// omo:T1TG4E4G1BqsUkRQfufdvehS2rr5hDbub0U_IZXfFYs:seTg07_ixK2ZBs7D8IU0hmPynnUB4xBhI7gMmDFMUdc
|
|
2
2
|
import{chmodSync as e,closeSync as t,existsSync as n,mkdirSync as i,openSync as r}from"node:fs";import{join as s}from"node:path";import{setTimeout as o}from"node:timers/promises";import{parentPort as a}from"node:worker_threads";function d(e,t,n){return Object.assign(Error(`gateway store lock wait exceeded: ${e} waited ${t} ms for the write lock (limit ${n} ms); another process holds it`),{code:"gateway_lock_wait_exceeded"})}function u(e){return"object"==typeof e&&null!==e&&"gateway_lock_wait_exceeded"===e.code}import{join as _}from"node:path";function l(e){return _(e,"gateway")}function c(e,t){return _(l(e),"inbox",t)}import{execFile as p}from"node:child_process";var g="epoch:",m=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];function b(e){return new Promise(t=>{p("ps",["-o","lstart=","-p",String(e)],{env:{...process.env,LC_ALL:"C",LANG:"C",TZ:"UTC"},windowsHide:!0,timeout:5e3},(e,n)=>{let i=null===e?function(e){let t=/^[A-Z][a-z]{2} ([A-Z][a-z]{2}) +(\d{1,2}) (\d{2}):(\d{2}):(\d{2}) (\d{4})$/.exec(e.trim());if(null===t)return null;let[,n,i,r,s,o,a]=t,d=m.indexOf(n);return d<0?null:Date.UTC(Number(a),d,Number(i),Number(r),Number(s),Number(o))/1e3}(n):null;t(null===i?null:`${g}${i}`)})})}async function E(e){if(!function(e){try{return process.kill(e,0),!0}catch(e){return!(e instanceof Error&&"code"in e&&"ESRCH"===e.code)}}(e.pid))return!0;if(null===e.process_start_time||!e.process_start_time.startsWith(g))return!1;let t=await b(e.pid);return null!==t&&t!==e.process_start_time}function f(e,t){return e.pid===t.pid&&e.process_start_time===t.process_start_time&&e.instance_id===t.instance_id}function*h(e){for(let t=0;t<e.length;){let n,i=t,r=e[t];if(r<=" ")for(n="space";t<e.length&&e[t]<=" ";)t++;else if("-"===r&&"-"===e[t+1])for(n="comment";t<e.length&&"\n"!==e[t];)t++;else if("/"===r&&"*"===e[t+1]){for(n="comment",t+=2;t<e.length&&("*"!==e[t]||"/"!==e[t+1]);)t++;t=Math.min(t+2,e.length)}else if("'"===r||'"'===r||"`"===r||"["===r){n="quoted";let i="["===r?"]":r;for(t++;t<e.length;)if(e[t++]===i){if("["===r||e[t]!==i)break;t++}}else if("?"===r||";"===r)n="?"===r?"parameter":"semicolon",t++;else for(n="text",t++;t<e.length&&e[t]>" "&&!["'",'"',"`","[","?",";","-","/"].includes(e[t]);)t++;yield{kind:n,text:e.slice(i,t)}}}class y{params=[];sinkRows=[];authorizer=null;db;constructor(e){this.db=e,e.function("gw_p",{deterministic:!1},e=>this.params[e]??null),e.function("gw_sink",{varargs:!0,deterministic:!1},(...e)=>(this.sinkRows.push(e),null))}exec(e){this.db.exec(e)}authorized(e,t){let n=this.authorizer;this.authorizer=e,this.db.setAuthorizer(e);try{return t()}finally{this.authorizer=n,this.db.setAuthorizer(n)}}resultColumns(e){return this.authorized(null,()=>{this.db.exec(`CREATE TEMP VIEW gw_result_columns AS ${T(e)}\n`);try{return this.all(["name"],"SELECT cid, name FROM pragma_table_info('gw_result_columns', 'temp')",[],"cid").map(e=>String(e.name))}finally{this.db.exec("DROP VIEW temp.gw_result_columns")}})}run(e,t=[]){this.params=t;try{this.db.exec(T(e))}finally{this.params=[]}return Number(this.all(["n"],"SELECT changes() AS n")[0]?.n??0)}all(e,t,n=[],i){this.params=n,this.sinkRows=[];try{let n=void 0===i?"0":`row_number() OVER (ORDER BY ${i})`;return this.db.exec(`SELECT gw_sink(${n}, ${e.join(", ")}) FROM (${T(t)}\n)${void 0===i?"":` ORDER BY ${i}`}`),(void 0===i?this.sinkRows:this.sinkRows.toSorted((e,t)=>Number(e[0])-Number(t[0]))).map(t=>Object.fromEntries(e.map((e,n)=>[e,t[n+1]??null])))}finally{this.params=[],this.sinkRows=[]}}one(e,t,n=[]){return this.all(e,t,n)[0]}}function T(e){let t=0,n="";for(let i of h(e))n+="parameter"===i.kind?`gw_p(${t++})`:i.text;return n}function v(e){if("object"!=typeof e||null===e)return!1;let t=e;return 5===t.errcode||6===t.errcode||"string"==typeof t.message&&/database is (locked|busy)/i.test(t.message)}import{createHash as w,randomUUID as N}from"node:crypto";import{closeSync as L,constants as k,mkdirSync as R,openSync as x,readdirSync as S,readFileSync as O,rmSync as A,writeSync as q}from"node:fs";import{userInfo as C}from"node:os";import{join as I}from"node:path";function U(e,t,n){return t===n||function(e,t,n){let i=new Map;for(let t of e){let e=i.get(t.from);void 0===e?i.set(t.from,[t.to]):e.push(t.to)}let r=new Set([t]),s=[t];for(;s.length>0;){let e=s.pop();if(e===n)return!0;for(let t of i.get(e)??[])r.has(t)||(r.add(t),s.push(t))}return!1}(e,n,t)}var D=5e3,M=864e5,$=6048e5,H=1048576,W=2592e6,X=2592e6,F=256,P="session_released";import{existsSync as j,readFileSync as B}from"node:fs";import{join as G}from"node:path";function J(e){let t=G(e,"mailbox.jsonl");if(j(t))return function(e){let t=e.slice(0,e.lastIndexOf("\n")+1),n=new Map;for(let e of t.split("\n")){if(0===e.length)continue;let t=JSON.parse(e);if(!z(t)||1!==t.version)throw Error("invalid legacy mailbox event");if("snapshot"===t.kind&&Array.isArray(t.items)){n.clear();for(let e of t.items){let t=Y(e);n.set(t.message_seq,t)}}else if("enqueue"===t.kind){let e=Y(t.item);n.set(e.message_seq,e)}else{if("remove"!==t.kind||"number"!=typeof t.message_seq)throw Error("invalid legacy mailbox event");n.delete(t.message_seq)}}return K([...n.values()])}(B(t,"utf8"));let n=G(e,"mailbox.json");if(!j(n))return null;let i=JSON.parse(B(n,"utf8"));if(!z(i)||!z(i.queues))throw Error(`invalid legacy mailbox state: ${n}`);let r=[];for(let e of Object.values(i.queues)){if(!Array.isArray(e))throw Error(`invalid legacy mailbox queue: ${n}`);for(let t of e)r.push(Y(t))}return K(r)}function K(e){return e.toSorted((e,t)=>e.message_seq-t.message_seq)}function Y(e){if(!z(e)||"string"!=typeof e.target||"string"!=typeof e.message||"number"!=typeof e.message_seq||!Number.isInteger(e.message_seq)||"auto"!==e.delivery&&"steer"!==e.delivery&&"follow_up"!==e.delivery||void 0!==e.expected_turn_id&&"string"!=typeof e.expected_turn_id||"string"!=typeof e.operation_id||"string"!=typeof e.accepted_at)throw Error("invalid legacy mailbox item");return{target:e.target,message:e.message,message_seq:e.message_seq,delivery:e.delivery,expected_turn_id:void 0===e.expected_turn_id?null:V(e.expected_turn_id),operation_id:e.operation_id,accepted_at:e.accepted_at}}function V(e){let t=/^turn-(0|[1-9]\d*)$/.exec(e);if(null===t)return null;let n=Number(t[1]);return Number.isSafeInteger(n)?n:null}function z(e){return"object"==typeof e&&null!==e&&!Array.isArray(e)}import{createHash as Q,createHmac as Z,randomUUID as ee,timingSafeEqual as te}from"node:crypto";var ne=["discord","telegram","slack","notion","feishu","herdr","custom","whatsapp"],ie=["milestone","report","question","completion"],re=["auto","follow_up"],se=31622400,oe=2592e6;function ae(e){return new Date(e).toISOString()}function de(e){return{kind:"refused",code:"invalid_arguments",message:e}}function ue(e,t){return"string"!=typeof t||0===t.trim().length?de(`${e} must be a non-empty string.`):t.length>256?de(`${e} is longer than 256 characters.`):/[\u0000-\u001f\u007f-\u009f\u2028\u2029]/.test(t)?de(`${e} carries a control character or a line separator.`):t}function _e(e,t){return null==t?null:ue(e,t)}function le(e){return"object"==typeof e&&null!==e&&"refused"===e.kind}function ce(e){if(null===e||"object"!=typeof e)return JSON.stringify(e)??"null";if(Array.isArray(e))return`[${e.map(ce).join(",")}]`;let t=e;return`{${Object.keys(t).filter(e=>void 0!==t[e]).sort().map(e=>`${JSON.stringify(e)}:${ce(t[e])}`).join(",")}}`}function pe(e){return Buffer.from(e).toString("base64url")}function ge(e,t){return pe(JSON.stringify({v:1,as_of:e,after:t}))}var me="rt1";function be(e,t){return Z("sha256",e).update(`${me}.${t}`).digest().subarray(0,16).toString("base64url")}var Ee=new WeakMap;function fe(e,t,n){return Ce(e,"DELETE FROM receipts WHERE principal = ? AND operation = ? AND idempotency_key = ? AND expires_at <= ?",[t.principal,t.operation,t.idempotency_key,n])}function he(e,t,n=0){if(t<(Ee.get(e.sql)??Number.NEGATIVE_INFINITY))return null;let i=function(e,t,n=256){let i=F,r=Ce(e,"DELETE FROM receipts WHERE rowid IN (SELECT rowid FROM receipts WHERE expires_at <= ? LIMIT ?)",[t,n]),s=Ce(e,"DELETE FROM deliveries WHERE delivery_id IN (SELECT d.delivery_id FROM deliveries d WHERE d.state IN ('applied', 'refused') AND d.updated_at <= ? AND NOT EXISTS (SELECT 1 FROM receipts r WHERE r.delivery_id = d.delivery_id) LIMIT ?)",[t-X,i]),o=Ce(e,"DELETE FROM causal_edges WHERE rowid IN (SELECT e.rowid FROM causal_edges e JOIN causal_roots r ON r.root_id = e.root_id WHERE r.expires_at <= ? LIMIT ?)",[t,i]),a=Ce(e,"DELETE FROM causal_roots WHERE root_id IN (SELECT r.root_id FROM causal_roots r WHERE r.expires_at <= ? AND NOT EXISTS (SELECT 1 FROM causal_edges e WHERE e.root_id = r.root_id) LIMIT ?)",[t,i]),d=Ce(e,"DELETE FROM rate_buckets WHERE rowid IN (SELECT rowid FROM rate_buckets WHERE updated_at <= ? LIMIT ?)",[t-4e4,i]),u=Ce(e,`DELETE FROM bindings WHERE binding_id IN (SELECT b.binding_id FROM bindings b WHERE b.status != 'active' AND b.updated_at <= ?\n AND NOT EXISTS (SELECT 1 FROM outbox o WHERE o.binding_id = b.binding_id)\n AND NOT EXISTS (SELECT 1 FROM completion_arms a WHERE a.binding_id = b.binding_id)\n AND NOT EXISTS (SELECT 1 FROM deliveries d WHERE d.binding_id = b.binding_id AND d.state IN ${Re}) LIMIT ?)`,[ae(t-oe),i]),_=Ce(e,"DELETE FROM outbox_cursors WHERE binding_id IN (SELECT c.binding_id FROM outbox_cursors c WHERE NOT EXISTS (SELECT 1 FROM bindings b WHERE b.binding_id = c.binding_id) LIMIT ?)",[i]),l=Ce(e,"DELETE FROM session_meta WHERE durable_id IN (SELECT m.durable_id FROM session_meta m WHERE\n m.incarnation IS NULL\n AND NOT EXISTS (SELECT 1 FROM deliveries d WHERE d.target_durable_id = m.durable_id)\n AND NOT EXISTS (SELECT 1 FROM bindings b WHERE b.session_durable_id = m.durable_id)\n AND NOT EXISTS (SELECT 1 FROM outbox o WHERE o.session_durable_id = m.durable_id)\n AND NOT EXISTS (SELECT 1 FROM completion_arms a WHERE a.session_durable_id = m.durable_id) LIMIT ?)",[i]);return{receipts:r,deliveries:s,causal_edges:o,causal_roots:a,rate_buckets:d,bindings:u,outbox_cursors:_,session_meta:l}}(e,t,Math.max(F-n,0)),r=i.receipts+n>=F||Object.values(i).some(e=>e>=F);return Ee.set(e.sql,r?t:t+36e5),i}var ye=new Set(["invalid_arguments","caller_context_missing","not_found","ambiguous_target","scope_denied","name_conflict","not_resumable","orphaned","foreign_live_owner","no_active_turn","turn_conflict","not_steerable","message_too_large","queue_full","cursor_invalid","cursor_stale","idempotency_conflict","idempotency_in_progress","idempotency_uncertain","approval_unavailable","approval_route_locked","partial_commit","unsupported","overloaded","transport_closed","host_unavailable","internal_error","model_not_found","model_ambiguous","thinking_level_unsupported","loop_detected","binding_conflict","binding_mismatch","binding_inactive","stale_revision","stale_token","already_answered","answer_in_progress"]);function Te(e,t,n,i){if("string"!=typeof(r=e)||!ye.has(r))throw Error(`unknown thread error code: ${String(e)}`);var r;return{code:e,message:t,next_action:n,...void 0===i?{}:{details:i}}}var ve={not_steerable:{code:"not_steerable",next:"Send with delivery auto or follow_up; a steer needs a running turn."},turn_conflict:{code:"turn_conflict",next:"Read the target again and retry with its current turn id, or send with delivery auto."},expired:{code:"not_resumable",next:"The target did not take the message within its lifetime; send it again if it still matters."},binding_closed:{code:"invalid_arguments",next:"The binding this message was queued under was detached or rebound; re-read the binding and send again."}};function we(e,t,n,i,r){let s={kind:"ok",thread_id:e.target_durable_id,delivery_id:e.delivery_id,message_seq:e.seq,effective_mode:e.mode_effective??e.mode_requested,endpoint_kind:n,deduplicated:i};if("admitted"===e.state||"applied"===e.state)return"started"===e.admission_kind||"steered"===e.admission_kind?{...s,delivery:{kind:e.admission_kind,turn_id:String(e.turn_epoch??0)}}:{...s,delivery:{kind:"queued",queue_position:Math.max(t,1)}};if("refused"===e.state){let t=ve[e.reason??""]??{code:"internal_error",next:"Call thread_list and check the target."};return{kind:"error",error:Te(t.code,`Delivery ${e.delivery_id} was refused: ${e.reason??"unknown"}.`,t.next,{delivery_id:e.delivery_id})}}return"uncertain"===e.state?{kind:"error",error:Te("idempotency_uncertain",`Delivery ${e.delivery_id} may or may not have reached the target.`,"Read the target transcript before deciding whether to send again.",{delivery_id:e.delivery_id})}:{...s,delivery:{kind:r?"queued_offline":"queued",queue_position:Math.max(t,1)}}}class Ne extends Error{found;supported;code="gateway_schema_too_new";constructor(e,t,n="Gateway"){super(`${n} schema ${e} is newer than this binary's supported version ${t}.`),this.found=e,this.supported=t}}var Le=[["CREATE TABLE deliveries (\n delivery_id TEXT PRIMARY KEY,\n target_durable_id TEXT NOT NULL,\n seq INTEGER NOT NULL,\n sender TEXT NOT NULL,\n sender_turn TEXT,\n envelope TEXT NOT NULL,\n body TEXT NOT NULL,\n bytes INTEGER NOT NULL,\n mode_requested TEXT NOT NULL CHECK (mode_requested IN ('auto', 'steer', 'follow_up')),\n mode_effective TEXT CHECK (mode_effective IS NULL OR mode_effective IN ('steer', 'follow_up')),\n expected_turn_id INTEGER,\n state TEXT NOT NULL CHECK (state IN ('queued', 'admitting', 'admitted', 'applied', 'refused', 'uncertain')),\n reason TEXT,\n admitted_by TEXT,\n claimed_at INTEGER,\n attempt INTEGER NOT NULL DEFAULT 0,\n admission_kind TEXT,\n turn_epoch INTEGER,\n root_id TEXT NOT NULL,\n hop INTEGER NOT NULL,\n created_at INTEGER NOT NULL,\n updated_at INTEGER NOT NULL,\n expires_at INTEGER NOT NULL,\n binding_id TEXT,\n binding_revision INTEGER,\n UNIQUE (target_durable_id, seq)\n )","CREATE INDEX deliveries_target_state_seq ON deliveries (target_durable_id, state, seq)","CREATE INDEX deliveries_root ON deliveries (root_id)","CREATE INDEX deliveries_sender_turn ON deliveries (sender, sender_turn)","CREATE TABLE receipts (\n principal TEXT NOT NULL,\n operation TEXT NOT NULL,\n idempotency_key TEXT NOT NULL,\n args_hash TEXT NOT NULL,\n status TEXT NOT NULL CHECK (status IN ('prepared', 'completed', 'uncertain')),\n delivery_id TEXT,\n owner_instance TEXT NOT NULL,\n result TEXT,\n error_note TEXT,\n created_at INTEGER NOT NULL,\n updated_at INTEGER NOT NULL,\n expires_at INTEGER NOT NULL,\n PRIMARY KEY (principal, operation, idempotency_key)\n )","CREATE INDEX receipts_expiry ON receipts (expires_at)","CREATE TABLE causal_roots (\n root_id TEXT PRIMARY KEY,\n origin_principal TEXT NOT NULL,\n created_at INTEGER NOT NULL,\n expires_at INTEGER NOT NULL\n )","CREATE TABLE causal_edges (\n root_id TEXT NOT NULL,\n from_durable_id TEXT NOT NULL,\n to_durable_id TEXT NOT NULL,\n delivery_id TEXT NOT NULL,\n created_at INTEGER NOT NULL,\n PRIMARY KEY (root_id, from_durable_id, to_durable_id)\n )","CREATE TABLE rate_buckets (\n sender TEXT NOT NULL,\n target_durable_id TEXT NOT NULL,\n tokens REAL NOT NULL,\n updated_at INTEGER NOT NULL,\n PRIMARY KEY (sender, target_durable_id)\n )","CREATE TABLE session_meta (\n durable_id TEXT PRIMARY KEY,\n next_seq INTEGER NOT NULL,\n applied_seq INTEGER NOT NULL DEFAULT 0\n )","CREATE TABLE bindings (\n seq INTEGER PRIMARY KEY AUTOINCREMENT,\n binding_id TEXT NOT NULL UNIQUE,\n schema_version INTEGER NOT NULL DEFAULT 1,\n revision INTEGER NOT NULL CHECK (revision >= 1),\n status TEXT NOT NULL CHECK (status IN ('active', 'detached', 'expired')),\n platform TEXT NOT NULL CHECK (platform IN ('discord', 'telegram', 'slack', 'notion', 'feishu', 'herdr', 'custom', 'whatsapp')),\n account_id TEXT NOT NULL,\n chat_id TEXT NOT NULL,\n thread_id TEXT NOT NULL,\n root_message_id TEXT,\n progress_message_id TEXT,\n session_realm_id TEXT NOT NULL,\n session_durable_id TEXT NOT NULL,\n direction_inbound INTEGER NOT NULL CHECK (direction_inbound IN (0, 1)),\n direction_outbound INTEGER NOT NULL CHECK (direction_outbound IN (0, 1)),\n inbound_mode TEXT NOT NULL CHECK (inbound_mode IN ('auto', 'follow_up')),\n outbound_events TEXT NOT NULL,\n policy_id TEXT NOT NULL,\n created_at TEXT NOT NULL,\n updated_at TEXT NOT NULL,\n lease_started_at TEXT NOT NULL,\n ttl_seconds INTEGER,\n expires_at TEXT,\n CHECK (direction_inbound = 1 OR direction_outbound = 1)\n )","CREATE UNIQUE INDEX bindings_one_active_thread ON bindings (platform, account_id, chat_id, thread_id) WHERE status = 'active'","CREATE INDEX bindings_session ON bindings (session_durable_id, status)","CREATE TABLE outbox (\n cursor INTEGER PRIMARY KEY AUTOINCREMENT,\n binding_id TEXT NOT NULL,\n revision INTEGER NOT NULL,\n event_kind TEXT NOT NULL CHECK (event_kind IN ('milestone', 'report', 'question', 'completion', 'question_closed')),\n payload TEXT NOT NULL,\n state TEXT NOT NULL CHECK (state IN ('pending', 'acked')),\n provider_message_id TEXT,\n created_at INTEGER NOT NULL,\n acked_at INTEGER\n )","CREATE INDEX outbox_binding_cursor ON outbox (binding_id, cursor)","CREATE TABLE gateway_meta (\n key TEXT PRIMARY KEY,\n value TEXT NOT NULL\n )"],["ALTER TABLE session_meta ADD COLUMN incarnation TEXT","ALTER TABLE outbox ADD COLUMN session_durable_id TEXT","ALTER TABLE outbox ADD COLUMN reply_token TEXT","ALTER TABLE outbox ADD COLUMN ui_request_id TEXT","ALTER TABLE outbox ADD COLUMN incarnation TEXT","ALTER TABLE outbox ADD COLUMN question_state TEXT CHECK (question_state IS NULL OR question_state IN ('pending', 'answered', 'expired', 'cancelled'))","ALTER TABLE outbox ADD COLUMN answer TEXT","ALTER TABLE outbox ADD COLUMN answered_at INTEGER","ALTER TABLE outbox ADD COLUMN outcome TEXT CHECK (outcome IS NULL OR outcome IN ('completed', 'failed', 'cancelled'))","CREATE UNIQUE INDEX outbox_reply_token ON outbox (reply_token) WHERE reply_token IS NOT NULL","CREATE TABLE outbox_cursors (\n binding_id TEXT PRIMARY KEY,\n acked_cursor INTEGER NOT NULL,\n updated_at INTEGER NOT NULL\n )","CREATE TABLE completion_arms (\n arm_seq INTEGER PRIMARY KEY AUTOINCREMENT,\n session_durable_id TEXT NOT NULL,\n binding_id TEXT NOT NULL,\n revision INTEGER NOT NULL,\n text TEXT NOT NULL,\n armed_at INTEGER NOT NULL\n )","CREATE INDEX completion_arms_session ON completion_arms (session_durable_id, armed_at)","INSERT OR IGNORE INTO gateway_meta (key, value) VALUES ('realm_id', 'realm-' || lower(hex(randomblob(16))))","INSERT OR IGNORE INTO gateway_meta (key, value) VALUES ('token_secret', lower(hex(randomblob(32))))"],["ALTER TABLE outbox ADD COLUMN ui_request_kind TEXT CHECK (ui_request_kind IS NULL OR ui_request_kind IN ('question', 'select', 'confirm', 'input', 'editor'))","ALTER TABLE outbox ADD COLUMN answer_state TEXT CHECK (answer_state IS NULL OR answer_state IN ('in_flight', 'delivered'))"],["ALTER TABLE outbox ADD COLUMN answered_by TEXT"],["ALTER TABLE session_meta ADD COLUMN endpoint_socket TEXT","ALTER TABLE session_meta ADD COLUMN endpoint_kind TEXT CHECK (endpoint_kind IS NULL OR endpoint_kind IN ('tui', 'rpc_host'))"],["CREATE TABLE extension_schema (name TEXT PRIMARY KEY, version INTEGER NOT NULL, updated_at INTEGER NOT NULL)","ALTER TABLE deliveries ADD COLUMN actor_user_id TEXT","CREATE TABLE extension_objects (type TEXT NOT NULL, name TEXT NOT NULL, owner TEXT, PRIMARY KEY (type, name))","INSERT INTO extension_objects (type, name, owner) SELECT type, name, NULL FROM sqlite_schema"],["CREATE TABLE bindings_v7 (\n seq INTEGER PRIMARY KEY AUTOINCREMENT,\n binding_id TEXT NOT NULL UNIQUE,\n schema_version INTEGER NOT NULL DEFAULT 1,\n revision INTEGER NOT NULL CHECK (revision >= 1),\n status TEXT NOT NULL CHECK (status IN ('active', 'detached', 'expired')),\n platform TEXT NOT NULL CHECK (platform IN ('discord', 'telegram', 'slack', 'notion', 'feishu', 'herdr', 'custom', 'whatsapp')),\n account_id TEXT NOT NULL,\n chat_id TEXT NOT NULL,\n thread_id TEXT NOT NULL,\n root_message_id TEXT,\n progress_message_id TEXT,\n session_realm_id TEXT NOT NULL,\n session_durable_id TEXT NOT NULL,\n direction_inbound INTEGER NOT NULL CHECK (direction_inbound IN (0, 1)),\n direction_outbound INTEGER NOT NULL CHECK (direction_outbound IN (0, 1)),\n inbound_mode TEXT NOT NULL CHECK (inbound_mode IN ('auto', 'follow_up')),\n outbound_events TEXT NOT NULL,\n policy_id TEXT NOT NULL,\n created_at TEXT NOT NULL,\n updated_at TEXT NOT NULL,\n lease_started_at TEXT NOT NULL,\n ttl_seconds INTEGER,\n expires_at TEXT,\n CHECK (direction_inbound = 1 OR direction_outbound = 1)\n )","INSERT INTO bindings_v7 SELECT seq, binding_id, schema_version, revision, status, platform, account_id, chat_id, thread_id, root_message_id, progress_message_id, session_realm_id, session_durable_id, direction_inbound, direction_outbound, inbound_mode, outbound_events, policy_id, created_at, updated_at, lease_started_at, ttl_seconds, expires_at FROM bindings","UPDATE sqlite_sequence SET seq = (SELECT seq FROM sqlite_sequence WHERE name = 'bindings') WHERE name = 'bindings_v7'","DROP TABLE bindings","ALTER TABLE bindings_v7 RENAME TO bindings","CREATE UNIQUE INDEX bindings_one_active_thread ON bindings (platform, account_id, chat_id, thread_id) WHERE status = 'active'","CREATE INDEX bindings_session ON bindings (session_durable_id, status)"]],ke=["delivery_id","target_durable_id","seq","sender","sender_turn","envelope","body","bytes","mode_requested","mode_effective","expected_turn_id","state","reason","admitted_by","claimed_at","attempt","admission_kind","turn_epoch","root_id","hop","created_at","updated_at","expires_at","binding_id","binding_revision","actor_user_id"],Re="('queued', 'admitting', 'admitted')",xe=/^[A-Za-z0-9][A-Za-z0-9._-]{0,199}$/;function Se(e,t,n){return{kind:"refused",code:e,message:t,...void 0===n?{}:{details:n}}}function Oe(e){let t=e=>null==e?null:Number(e);return{delivery_id:String(e.delivery_id),target_durable_id:String(e.target_durable_id),seq:Number(e.seq),sender:String(e.sender),sender_turn:null===e.sender_turn?null:String(e.sender_turn),envelope:JSON.parse(String(e.envelope)),body:String(e.body),bytes:Number(e.bytes),mode_requested:e.mode_requested,mode_effective:e.mode_effective??null,expected_turn_id:t(e.expected_turn_id),state:e.state,reason:null===e.reason?null:String(e.reason),admitted_by:null===e.admitted_by?null:JSON.parse(String(e.admitted_by)),claimed_at:t(e.claimed_at),attempt:Number(e.attempt),admission_kind:null===e.admission_kind?null:String(e.admission_kind),turn_epoch:t(e.turn_epoch),root_id:String(e.root_id),hop:Number(e.hop),created_at:Number(e.created_at),updated_at:Number(e.updated_at),expires_at:Number(e.expires_at),binding_id:null===e.binding_id?null:String(e.binding_id),binding_revision:t(e.binding_revision),actor_user_id:null===e.actor_user_id?null:String(e.actor_user_id)}}function Ae(e,t,n,i){return e.sql.all(ke,`SELECT ${ke.join(", ")} FROM deliveries WHERE ${t}`,n,i).map(Oe)}function qe(e,t){return Ae(e,"delivery_id = ?",[t])[0]}function Ce(e,t,n=[]){let i=e.sql.run(t,n);return e.stats.writes+=i,i}function Ie(e,t){if("queued"!==t.state&&"admitting"!==t.state&&"admitted"!==t.state)return 0;let n=e.sql.one(["n"],`SELECT COUNT(*) AS n FROM deliveries WHERE target_durable_id = ? AND state IN ${Re} AND seq <= ?`,[t.target_durable_id,t.seq]);return Number(n?.n??1)}async function Ue(e,t,n){if(void 0!==e.afterCommit)return e.sql.exec("SAVEPOINT gateway_enqueue"),!0;let i=Date.now();for(;;)try{return e.sql.exec("BEGIN IMMEDIATE"),e.stats.transactions++,!0}catch(r){if(!v(r))throw r;if(e.emit({kind:"busy",op:t}),!n)return!1;let s=Date.now()-i;if(s+2*e.config.busy_timeout_ms>e.config.lock_wait_max_ms)throw e.emit({kind:"lock_wait_exceeded",op:t,waited_ms:s}),d(t,s,e.config.lock_wait_max_ms);await e.delay(e.config.busy_timeout_ms)}}function De(e){try{e.sql.exec(void 0===e.afterCommit?"ROLLBACK":"ROLLBACK TO gateway_enqueue; RELEASE gateway_enqueue")}catch{return}}async function Me(e,t,n){if(void 0!==e.afterCommit)return await n();await Ue(e,t,!0);try{let t=await n();return e.sql.exec("COMMIT"),t}catch(t){throw De(e),t}}function $e(e,t,n,i){let r=c(e.config.agent_dir,t);R(r,{recursive:!0,mode:448});let s,o=I(r,n);try{s=x(o,k.O_CREAT|k.O_EXCL|k.O_WRONLY,384)}catch(e){if(i&&e instanceof Error&&"code"in e&&"EEXIST"===e.code)return o;throw e}try{q(s,JSON.stringify({pid:e.self.pid,process_start_time:e.self.process_start_time}))}finally{L(s)}return e.afterRollback?.push(()=>A(o,{force:!0})),o}function He(e,t,n){if(void 0!==e.afterCommit)return void e.afterCommit.push(()=>He({...e,afterCommit:void 0},t,n));let i=I(c(e.config.agent_dir,t),n);try{A(i),e.stats.marker_unlinks++}catch(e){if(!(e instanceof Error&&"code"in e&&"ENOENT"===e.code))throw e}}function We(e,t){try{return S(c(e.config.agent_dir,t)).filter(e=>!e.startsWith("."))}catch(e){if(e instanceof Error&&"code"in e&&"ENOENT"===e.code)return[];throw e}}function Xe(e,t){let n=e.sql.one(["next_seq"],"SELECT next_seq FROM session_meta WHERE durable_id = ?",[t]),i=Number(void 0===n?e.sql.one(["m"],"SELECT COALESCE(MAX(seq), 0) + 1 AS m FROM deliveries WHERE target_durable_id = ?",[t])?.m??1:n.next_seq);return Ce(e,"INSERT INTO session_meta (durable_id, next_seq) VALUES (?, ?) ON CONFLICT(durable_id) DO UPDATE SET next_seq = excluded.next_seq",[t,i+1]),i}function Fe(e,t){Ce(e,`INSERT INTO deliveries (${ke.join(", ")}) VALUES (${ke.map(()=>"?").join(", ")})`,[t.delivery_id,t.target_durable_id,t.seq,t.sender,t.sender_turn,JSON.stringify(t.envelope),t.body,t.bytes,t.mode_requested,t.mode_effective,t.expected_turn_id,t.state,t.reason,null===t.admitted_by?null:JSON.stringify(t.admitted_by),t.claimed_at,t.attempt,t.admission_kind,t.turn_epoch,t.root_id,t.hop,t.created_at,t.updated_at,t.expires_at,t.binding_id,t.binding_revision,t.actor_user_id])}function Pe(e){return w("sha256").update(JSON.stringify(e)).digest("hex")}function je(e){let t=Number(e.sql.one(["user_version"],"SELECT user_version FROM pragma_user_version()")?.user_version??0);if(t>Le.length)throw new Ne(t,Le.length);return t}class Be extends Error{version;code="gateway_migration_failed";constructor(e,t){super(t),this.version=e,this.name="GatewayMigrationError"}}var Ge=new Set([6]);function Je(e,t,n){let i=e.sql.one(["args_hash","status","delivery_id","owner_instance","result","expires_at"],"SELECT args_hash, status, delivery_id, owner_instance, result, expires_at FROM receipts WHERE principal = ? AND operation = 'deliver' AND idempotency_key = ?",[t,n]);if(void 0!==i)return{args_hash:String(i.args_hash),status:i.status,delivery_id:null===i.delivery_id?null:String(i.delivery_id),owner_instance:String(i.owner_instance),result:null===i.result?null:String(i.result),expires_at:Number(i.expires_at)}}function Ke(e,t,n){if(n.args_hash!==t.receipt.args_hash)return Se("idempotency_conflict","The idempotency key was already used with different arguments.");if("completed"===n.status&&null!==n.result)return{kind:"replay",result:JSON.parse(n.result)};let i=null===n.delivery_id?void 0:qe(e,n.delivery_id),r=void 0!==i&&("admitted"===i.state||"applied"===i.state||"refused"===i.state);if("prepared"===n.status&&!r&&n.owner_instance===e.config.instance_id)return Se("idempotency_in_progress","The same delivery is already in progress.");if("prepared"===n.status&&r){let n={...we(i,Ie(e,i),t.endpoint_kind,!0,!1)};return Ce(e,"UPDATE receipts SET status = 'completed', result = ?, updated_at = ? WHERE principal = ? AND operation = 'deliver' AND idempotency_key = ?",[JSON.stringify(n),t.now,t.sender_principal,t.receipt.idempotency_key]),{kind:"replay",result:n}}return"prepared"===n.status&&Ce(e,"UPDATE receipts SET status = 'uncertain', error_note = ?, updated_at = ? WHERE principal = ? AND operation = 'deliver' AND idempotency_key = ?",["the sender lost the acknowledgement before the target admitted the delivery",t.now,t.sender_principal,t.receipt.idempotency_key]),Se("idempotency_uncertain","The earlier delivery may or may not reach the target; it is never sent twice.",{delivery_id:n.delivery_id,state:i?.state??null})}async function Ye(e,t){if(!xe.test(t.target_durable_id)||!xe.test(t.delivery_id))return Se("invalid_arguments","The target session id is not a durable session id.");if(!await Ue(e,"enqueue",!1))return{kind:"busy"};let n=null,i=!1,r=e.afterCommit?.length??0,s=void 0===e.afterCommit?"COMMIT":"RELEASE gateway_enqueue";try{let r=fe(e,{principal:t.sender_principal,operation:"deliver",idempotency_key:t.receipt.idempotency_key},t.now),o=Je(e,t.sender_principal,t.receipt.idempotency_key);if(void 0!==o){let n=Ke(e,t,o);return e.sql.exec(s),i=!0,n}let a=function(e,t){if(null===t.binding)return{expires_at:null};let n=e.sql.one(["revision","status","session_durable_id","direction_inbound","expires_at"],"SELECT revision, status, session_durable_id, direction_inbound, expires_at FROM bindings WHERE binding_id = ?",[t.binding.binding_id]),i=null==n?.expires_at?null:Date.parse(String(n.expires_at));return void 0===n||"active"!==n.status||Number(n.revision)!==t.binding.revision||String(n.session_durable_id)!==t.target_durable_id||1!==Number(n.direction_inbound)||null!==i&&i<=t.now?Se("invalid_arguments","The binding is not active for this session at this revision.",{binding:t.binding.binding_id}):{expires_at:i}}(e,t);if("kind"in a)return De(e),a;let d=e.sql.one(["n","b"],`SELECT COUNT(*) AS n, COALESCE(SUM(bytes), 0) AS b FROM deliveries WHERE target_durable_id = ? AND state IN ${Re}`,[t.target_durable_id]),u=Buffer.byteLength(t.body);if(Number(d?.n??0)>=128||Number(d?.b??0)+u>H)return De(e),Se("queue_full","The target's delivery backlog is full.",{max_messages:128,max_bytes:H});let _=function(e,t){let n;if(null!==t.cause_delivery_id){let i=qe(e,t.cause_delivery_id);if(void 0===i||i.target_durable_id!==t.sender_node||"admitting"!==i.state&&"admitted"!==i.state&&"applied"!==i.state)return Se("invalid_arguments","The delivery this send continues is not one the sender received.",{guard:"unknown_cause"});n={root_id:i.root_id,hop:i.hop+1,via:[...i.envelope.via,t.sender_node],new_root:!1}}else n={root_id:`root-${N()}`,hop:1,via:[t.sender_node],new_root:!0};if(null!==t.claimed_root_id&&t.claimed_root_id!==n.root_id)return Se("invalid_arguments","The causal root is derived by the gateway and cannot be chosen by the caller.",{guard:"forged_root"});if(!n.new_root){let i=e.sql.one(["expires_at"],"SELECT expires_at FROM causal_roots WHERE root_id = ?",[n.root_id]);if(void 0===i||Number(i.expires_at)<=t.now)return Se("loop_detected","This causal chain is older than its lifetime.",{guard:"root_expired",root_id:n.root_id})}if(t.target_durable_id===t.sender_node)return Se("loop_detected","A session cannot deliver to itself through the gateway.",{guard:"self_send"});if(n.hop>4)return Se("loop_detected",`This delivery would be hop ${n.hop}; at most 4 are allowed.`,{guard:"hop_limit",hop:n.hop});let i=Number(e.sql.one(["n"],"SELECT COUNT(*) AS n FROM deliveries WHERE root_id = ? AND state != 'refused'",[n.root_id])?.n??0);return i>=64?Se("loop_detected",`This causal chain already carried ${i} deliveries.`,{guard:"causal_budget",root_id:n.root_id}):U(e.sql.all(["from_durable_id","to_durable_id"],"SELECT from_durable_id, to_durable_id FROM causal_edges WHERE root_id = ?",[n.root_id]).map(e=>({from:String(e.from_durable_id),to:String(e.to_durable_id)})),t.sender_node,t.target_durable_id)?Se("loop_detected","The target already leads back to the sender in this causal chain.",{guard:"cycle",root_id:n.root_id}):n}(e,t);if("kind"in _)return De(e),_;let l=function(e,t){if(null===t.sender_turn)return;let n=new Set(e.sql.all(["t"],"SELECT DISTINCT target_durable_id AS t FROM deliveries WHERE sender = ? AND sender_turn = ? AND state != 'refused'",[t.sender_principal,t.sender_turn]).map(e=>String(e.t)));return n.has(t.target_durable_id)||n.size<16?void 0:Se("overloaded","One turn may reach at most 16 sessions.",{budget:"fanout"})}(e,t)??function(e,t){let n=t.rate_principal??t.sender_principal,i=e.sql.one(["tokens","updated_at"],"SELECT tokens, updated_at FROM rate_buckets WHERE sender = ? AND target_durable_id = ?",[n,t.target_durable_id]),r=void 0===i?8:Math.min(8,Number(i.tokens)+Math.max(0,t.now-Number(i.updated_at))/D);if(r<1)return Se("overloaded","Too many deliveries to this session; wait before sending again.",{budget:"pair_rate",retry_after_ms:Math.ceil((1-r)*D)});Ce(e,"INSERT INTO rate_buckets (sender, target_durable_id, tokens, updated_at) VALUES (?, ?, ?, ?) ON CONFLICT(sender, target_durable_id) DO UPDATE SET tokens = excluded.tokens, updated_at = excluded.updated_at",[n,t.target_durable_id,r-1,t.now])}(e,t);if(void 0!==l)return De(e),l;_.new_root&&Ce(e,"INSERT INTO causal_roots (root_id, origin_principal, created_at, expires_at) VALUES (?, ?, ?, ?)",[_.root_id,t.sender_principal,t.now,t.now+$]),Ce(e,"INSERT OR IGNORE INTO causal_edges (root_id, from_durable_id, to_durable_id, delivery_id, created_at) VALUES (?, ?, ?, ?, ?)",[_.root_id,t.sender_node,t.target_durable_id,t.delivery_id,t.now]);let c={delivery_id:t.delivery_id,target_durable_id:t.target_durable_id,seq:Xe(e,t.target_durable_id),sender:t.sender_principal,sender_turn:t.sender_turn,envelope:{origin:t.origin,actor:t.actor,via:_.via,root_id:_.root_id,hop:_.hop,delivery_id:t.delivery_id},body:t.body,bytes:u,mode_requested:t.mode,mode_effective:null,expected_turn_id:t.expected_turn_id,state:"queued",reason:null,admitted_by:null,claimed_at:null,attempt:0,admission_kind:null,turn_epoch:null,root_id:_.root_id,hop:_.hop,created_at:t.now,updated_at:t.now,expires_at:Math.min(t.now+M,a.expires_at??Number.POSITIVE_INFINITY),binding_id:t.binding?.binding_id??null,binding_revision:t.binding?.revision??null,actor_user_id:"external"in t.origin?t.origin.external.author?.user_id??null:null};Fe(e,c),Ce(e,"INSERT INTO receipts (principal, operation, idempotency_key, args_hash, status, delivery_id, owner_instance, created_at, updated_at, expires_at) VALUES (?, 'deliver', ?, ?, 'prepared', ?, ?, ?, ?, ?)",[t.sender_principal,t.receipt.idempotency_key,t.receipt.args_hash,c.delivery_id,e.config.instance_id,t.now,t.now,t.now+W]),n=$e(e,c.target_durable_id,c.delivery_id,!1);let p=Ie(e,c);return he(e,t.now,r),void 0===e.afterCommit&&await e.hook("beforeDbCommit"),e.sql.exec(s),i=!0,void 0===e.afterCommit&&await e.hook("afterDbCommit"),{kind:"inserted",row:c,queue_position:p}}catch(t){throw i||(De(e),void 0!==e.afterCommit&&(e.afterCommit.length=r),null!==n&&A(n,{force:!0})),t}}function Ve(e,t){if(null===e)return"";let n=t.get(e);if(void 0===n){try{n=O(e,"utf8")}catch(e){if(!(e instanceof Error&&"code"in e&&"ENOENT"===e.code))throw e;n=""}t.set(e,n)}return n}function ze(e,t,n){if(null===e.runtime_instance||null===t)return!1;let i=n().get(e.runtime_instance);return void 0!==i&&t<=i}function Qe(e,t,n){for(let i of Ve(e,n).split("\n")){if(!i.includes(t))continue;let e;try{e=JSON.parse(i)}catch{continue}if("object"!=typeof e||null===e)continue;let n=e;if("custom_message"===n.type&&"session_control_delivery"===n.customType&&n.details?.delivery_id===t)return!0}return!1}function Ze(e){return`${JSON.stringify(e.admitted_by)}#${e.attempt}`}function et(e,t,n,i,r={}){"queued"!==n?"refused"!==n?(Ce(e,"UPDATE deliveries SET state = ?, updated_at = ? WHERE delivery_id = ?",[n,i,t.delivery_id]),"applied"===n&&Ce(e,"UPDATE session_meta SET applied_seq = MAX(applied_seq, ?) WHERE durable_id = ?",[t.seq,t.target_durable_id])):Ce(e,"UPDATE deliveries SET state = 'refused', reason = ?, updated_at = ? WHERE delivery_id = ?",[r.reason??null,i,t.delivery_id]):Ce(e,"UPDATE deliveries SET state = 'queued', admitted_by = NULL, claimed_at = NULL, mode_effective = NULL, admission_kind = NULL, turn_epoch = NULL, updated_at = ? WHERE delivery_id = ?",[i,t.delivery_id])}async function tt(e,t){let n,i=t.target_durable_id,r=new Map,s=new Map,o=new Map,a=()=>(n??=function(e,t){let n=new Map;for(let i of Ve(e,t).split("\n")){if(!i.includes(P))continue;let e;try{e=JSON.parse(i)}catch{continue}let t=e;if("custom"!==t.type||t.customType!==P)continue;let r=t.data?.host_instance,s="string"==typeof t.data?.released_at?Date.parse(t.data.released_at):Number.NaN;"string"==typeof r&&0!==r.length&&Number.isFinite(s)&&n.set(r,Math.max(s,n.get(r)??s))}return n}(t.session_path,s),n);for(let n of Ae(e,"target_durable_id = ? AND state IN ('admitting', 'admitted')",[i])){if(null===n.admitted_by||f(n.admitted_by,t.self))continue;let e=JSON.stringify(n.admitted_by),i=o.get(e);void 0===i&&(i=await E(n.admitted_by),o.set(e,i));let d=i||ze(n.admitted_by,n.claimed_at,a)?Qe(t.session_path,n.delivery_id,s)?"applied":"queued":"dual";r.set(n.delivery_id,{fingerprint:Ze(n),to:d})}return!0===e.config.test_hooks.announceBarrier&&e.emit({kind:"barrier",op:"reconcile"}),await Me(e,"reconcile",()=>{let n=new Set(t.ledger.pending),s=new Set(t.ledger.emitted),o=[],a=[];for(let d of Ae(e,`target_durable_id = ? AND state IN ${Re}`,[i],"seq")){let i=d.state;if("queued"!==d.state){if(null!==d.admitted_by&&f(d.admitted_by,t.self))i=s.has(d.delivery_id)?"applied":n.has(d.delivery_id)?"admitted":"queued";else if(null===d.admitted_by)i="queued";else{let e=r.get(d.delivery_id);if(void 0===e||e.fingerprint!==Ze(d)||"dual"===e.to){a.push(d.delivery_id);continue}i=e.to}i!==d.state&&(et(e,d,i,t.now),o.push({delivery_id:d.delivery_id,from:d.state,to:i}))}else d.expires_at<=t.now&&(et(e,d,"refused",t.now,{reason:"expired"}),o.push({delivery_id:d.delivery_id,from:d.state,to:"refused"}))}for(let t of We(e,i))"queued"!==e.sql.one(["state"],"SELECT state FROM deliveries WHERE delivery_id = ?",[t])?.state&&He(e,i,t);return{queued:Ae(e,"target_durable_id = ? AND state = 'queued'",[i],"seq"),transitions:o,dual_runtime:a}})}async function nt(e,t){return await Me(e,"complete_receipt",()=>1===Ce(e,"UPDATE receipts SET status = 'completed', result = ?, updated_at = ? WHERE principal = ? AND operation = 'deliver' AND idempotency_key = ? AND status = 'prepared' AND owner_instance = ?",[JSON.stringify(t.result),t.now,t.principal,t.idempotency_key,e.config.instance_id]))}async function it(e,t){return await Me(e,"abandon_receipt",()=>1===Ce(e,"UPDATE receipts SET status = 'uncertain', error_note = ?, updated_at = ? WHERE principal = ? AND operation = 'deliver' AND idempotency_key = ? AND status = 'prepared' AND owner_instance = ?",[t.error_note,t.now,t.principal,t.idempotency_key,e.config.instance_id]))}function rt(e,t){let n=Je(e,t.principal,t.idempotency_key);if(void 0===n||n.expires_at<=t.now||null===n.delivery_id)return null;let i=qe(e,n.delivery_id);if(void 0===i)return null;let r="completed"===n.status&&null!==n.result;return{args_hash:n.args_hash,completed:r,result:r?JSON.parse(n.result):null,target_durable_id:i.target_durable_id,binding_revision:i.binding_revision}}async function st(e,t){return await Me(e,"recover_delivery",()=>{let n=Je(e,t.principal,t.idempotency_key);return void 0===n||n.expires_at<=t.now?null:Ke(e,{now:t.now,sender_principal:t.principal,endpoint_kind:null,receipt:{idempotency_key:t.idempotency_key,args_hash:t.args_hash}},n)})}function ot(e,t){let n=qe(e,t);return void 0===n?null:{row:n,queue_position:Ie(e,n)}}function at(){try{return C().username}catch{return"unknown"}}import{randomUUID as dt}from"node:crypto";import{renameSync as ut,writeFileSync as _t}from"node:fs";var lt=["question","select","confirm","input","editor"];var ct=new Map([["yes",!0],["y",!0],["true",!0],["no",!1],["n",!1],["false",!1]]);function pt(e){return""===e.replace(/[\s\p{Cf}]/gu,"")}function gt(e){let t=ct.get(e.trim().toLowerCase());return void 0===t?{value:e,answers:{},comment:e}:{value:e,answers:{},comment:e,confirmed:t}}function mt(e,t){switch(e){case null:return pt(t)?{ok:!1,reason:"The question declared no request kind, so the answer needs text; this one is blank. An input or editor that takes an empty answer must be reported with its request kind."}:{ok:!0,fields:gt(t)};case"input":case"editor":return{ok:!0,fields:{value:t}};case"select":return pt(t)?{ok:!1,reason:"A select answer names one of its options; the text is blank."}:{ok:!0,fields:{value:t}};case"question":return pt(t)?{ok:!1,reason:"A question answer needs text; this one is blank."}:{ok:!0,fields:{answers:{},comment:t}};case"confirm":{let e=ct.get(t.trim().toLowerCase());return void 0===e?{ok:!1,reason:"A confirm answer is yes or no (also y/n, true/false; any case, surrounding spaces ignored)."}:{ok:!0,fields:{confirmed:e}}}default:return function(e){throw Error(`unknown extension UI request kind: ${String(e)}`)}(e)}}var bt=["binding_id","schema_version","revision","status","platform","account_id","chat_id","thread_id","root_message_id","progress_message_id","session_realm_id","session_durable_id","direction_inbound","direction_outbound","inbound_mode","outbound_events","policy_id","created_at","updated_at","lease_started_at","ttl_seconds","expires_at"],Et=["cursor","binding_id","revision","event_kind","payload","state","provider_message_id","created_at","reply_token","question_state","outcome","answered_by","answer_state"];function ft(e,t,n){return{kind:"refused",code:e,message:t,...void 0===n?{}:{details:n}}}function ht(e){return null==e?null:String(e)}function yt(e){return null==e?null:JSON.stringify(e)}function Tt(e){return null==e?null:JSON.parse(String(e))}function vt(e,t,n,i){if(void 0!==e.afterCommit)return void e.afterCommit.push(()=>vt({...e,afterCommit:void 0},t,n,i));let r=(o=e.config.agent_dir,_(l(o),"outbox.marker")),s=`${r}.${process.pid}.${dt()}.tmp`;var o;_t(s,JSON.stringify({binding_id:t,cursor:n,written_at:ae(i)}),{mode:384}),ut(s,r)}function wt(e){return{schema_version:1,binding_id:String(e.binding_id),revision:Number(e.revision),status:e.status,platform:e.platform,account_id:String(e.account_id),chat_id:String(e.chat_id),thread_id:String(e.thread_id),root_message_id:ht(e.root_message_id),progress_message_id:ht(e.progress_message_id),session_realm_id:String(e.session_realm_id),session_durable_id:String(e.session_durable_id),direction:{inbound:1===Number(e.direction_inbound),outbound:1===Number(e.direction_outbound)},inbound_mode:e.inbound_mode,outbound_events:JSON.parse(String(e.outbound_events)),policy_id:String(e.policy_id),created_at:String(e.created_at),updated_at:String(e.updated_at),lease_started_at:String(e.lease_started_at),ttl_seconds:null===e.ttl_seconds||void 0===e.ttl_seconds?null:Number(e.ttl_seconds),expires_at:ht(e.expires_at)}}function Nt(e,t,n,i){let r=`SELECT ${bt.join(", ")} FROM bindings WHERE ${t}${void 0===i?"":` ORDER BY ${i.orderBy} LIMIT ?`}`;return e.sql.all(bt,r,void 0===i?n:[...n,i.limit],i?.orderBy).map(wt)}function Lt(e,t){return Nt(e,"binding_id = ?",[t])[0]}function kt(e,t){let n=e.sql.one(["value"],"SELECT value FROM gateway_meta WHERE key = ?",[t]);if(void 0===n)throw Error(`gateway_meta ${t} is missing`);return String(n.value)}function Rt(e,t){let n=ae(t);Ce(e,"UPDATE bindings SET status = 'expired', updated_at = ? WHERE status = 'active' AND expires_at IS NOT NULL AND expires_at <= ?",[n,n])}function xt(e,t){Ce(e,"DELETE FROM outbox WHERE state = 'acked' AND acked_at IS NOT NULL AND acked_at <= ?",[t-oe]),Ce(e,"DELETE FROM outbox WHERE state = 'pending' AND binding_id IN (SELECT binding_id FROM bindings WHERE status != 'active' AND updated_at <= ?)",[ae(t-oe)])}function St(e,t){let n=e.sql.one(["args_hash","status","owner_instance","result"],"SELECT args_hash, status, owner_instance, result FROM receipts WHERE principal = ? AND operation = ? AND idempotency_key = ?",[t.principal,t.operation,t.idempotency_key]);if(void 0!==n)return{args_hash:String(n.args_hash),status:String(n.status),owner_instance:String(n.owner_instance),result:ht(n.result)}}async function Ot(e,t,n,i,r){return await Me(e,t,()=>{Rt(e,n);let t=null===i?0:fe(e,i,n),s=function(e,t){if(null===t)return;let n=St(e,t);return void 0!==n?n.args_hash!==t.args_hash?ft("idempotency_conflict","The idempotency key was already used with different arguments."):"completed"===n.status&&null!==n.result?{...JSON.parse(n.result),kind:"ok",deduplicated:!0}:ft("idempotency_uncertain","An earlier call under this key did not record its outcome."):void 0}(e,i);if(void 0!==s)return s;let o=r();return"ok"===o.kind&&function(e,t,n,i){null!==t&&Ce(e,"INSERT INTO receipts (principal, operation, idempotency_key, args_hash, status, delivery_id, owner_instance, result, created_at, updated_at, expires_at) VALUES (?, ?, ?, ?, 'completed', NULL, ?, ?, ?, ?, ?)",[t.principal,t.operation,t.idempotency_key,t.args_hash,e.config.instance_id,JSON.stringify(i),n,n,n+W])}(e,i,n,{...o,deduplicated:!1}),he(e,n,t),"ok"===o.kind?{...o,deduplicated:!1}:o})}async function At(e,t){return await Me(e,"tool_receipt_begin",()=>{let n=fe(e,t,t.now),i=function(e,t){let n=St(e,t);if(void 0===n)return Ce(e,"INSERT INTO receipts (principal, operation, idempotency_key, args_hash, status, delivery_id, owner_instance, created_at, updated_at, expires_at) VALUES (?, ?, ?, ?, 'prepared', NULL, ?, ?, ?, ?)",[t.principal,t.operation,t.idempotency_key,t.args_hash,e.config.instance_id,t.now,t.now,t.now+W]),{kind:"accepted"};if(n.args_hash!==t.args_hash)return{kind:"conflict"};if("completed"===n.status)return{kind:"replay",result:null===n.result?null:JSON.parse(n.result)};if("prepared"===n.status&&n.owner_instance===e.config.instance_id)return{kind:"in_progress"};"prepared"===n.status&&Ce(e,"UPDATE receipts SET status = 'uncertain', error_note = ?, updated_at = ? WHERE principal = ? AND operation = ? AND idempotency_key = ?",["the process that began this call ended before recording its outcome",t.now,t.principal,t.operation,t.idempotency_key]);let i=e.sql.one(["error_note"],"SELECT error_note FROM receipts WHERE principal = ? AND operation = ? AND idempotency_key = ?",[t.principal,t.operation,t.idempotency_key]);return{kind:"uncertain",error_note:ht(i?.error_note)}}(e,t);return he(e,t.now,n),i})}async function qt(e,t){let n=t.binding;return await Ot(e,"bind",t.now,t.receipt,()=>{let i=Nt(e,"platform = ? AND account_id = ? AND chat_id = ? AND thread_id = ? AND status = 'active'",[n.platform,n.account_id,n.chat_id,n.thread_id])[0];if(void 0!==i){return ft("binding_conflict",i.session_durable_id===n.session_durable_id?"This thread is already bound to that session.":"This thread is bound to another session; rebind it explicitly to move it.",{binding_id:i.binding_id,revision:i.revision,session:i.session_durable_id})}let r=ae(t.now),s={schema_version:1,binding_id:`bnd-${ee()}`,revision:1,status:"active",platform:n.platform,account_id:n.account_id,chat_id:n.chat_id,thread_id:n.thread_id,root_message_id:n.root_message_id,progress_message_id:n.progress_message_id,session_realm_id:kt(e,"realm_id"),session_durable_id:n.session_durable_id,direction:n.direction,inbound_mode:n.inbound_mode,outbound_events:n.outbound_events,policy_id:n.policy_id,created_at:r,updated_at:r,lease_started_at:r,ttl_seconds:n.ttl_seconds,expires_at:null===n.ttl_seconds?null:ae(t.now+1e3*n.ttl_seconds)};return Ce(e,`INSERT INTO bindings (${bt.join(", ")}) VALUES (${bt.map(()=>"?").join(", ")})`,[s.binding_id,s.schema_version,s.revision,s.status,s.platform,s.account_id,s.chat_id,s.thread_id,s.root_message_id,s.progress_message_id,s.session_realm_id,s.session_durable_id,s.direction.inbound?1:0,s.direction.outbound?1:0,s.inbound_mode,JSON.stringify(s.outbound_events),s.policy_id,s.created_at,s.updated_at,s.lease_started_at,s.ttl_seconds,s.expires_at]),{kind:"ok",binding:s}})}function Ct(e,t){return e.sql.all(["delivery_id"],`SELECT delivery_id, seq FROM deliveries WHERE binding_id = ? AND state IN ${Re}`,[t],"seq").map(e=>String(e.delivery_id))}async function It(e,t){return await Ot(e,"unbind",t.now,t.receipt,()=>{let n=Lt(e,t.binding_id);if(void 0===n)return ft("not_found","No binding has this id.",{binding_id:t.binding_id});if("active"!==n.status)return{kind:"ok",binding:n,already_closed:!0,in_flight:Ct(e,n.binding_id)};if(n.revision!==t.expected_revision)return ft("stale_revision","The binding changed since that revision.",{binding_id:n.binding_id,revision:n.revision});let i=ae(t.now);return Ce(e,"UPDATE bindings SET status = 'detached', revision = revision + 1, updated_at = ? WHERE binding_id = ? AND revision = ?",[i,n.binding_id,n.revision]),Ce(e,"DELETE FROM completion_arms WHERE binding_id = ?",[n.binding_id]),{kind:"ok",binding:Lt(e,n.binding_id),already_closed:!1,in_flight:Ct(e,n.binding_id)}})}async function Ut(e,t){return await Ot(e,"rebind",t.now,t.receipt,()=>{let n=Lt(e,t.binding_id);if(void 0===n)return ft("not_found","No binding has this id.",{binding_id:t.binding_id});if("active"!==n.status)return ft("binding_inactive",`The binding is ${n.status}.`,{binding_id:n.binding_id,status:n.status});if(n.revision!==t.expected_revision)return ft("stale_revision","The binding changed since that revision.",{binding_id:n.binding_id,revision:n.revision});if(n.session_durable_id===t.session_durable_id)return ft("invalid_arguments","The binding is already attached to that session.",{binding_id:n.binding_id});let i=ae(t.now),r=e.sql.all(["delivery_id","target"],"SELECT delivery_id, target_durable_id AS target, seq FROM deliveries WHERE binding_id = ? AND binding_revision = ? AND state = 'queued'",[n.binding_id,n.revision],"seq").map(e=>({delivery_id:String(e.delivery_id),target:String(e.target)}));for(let n of r)Ce(e,"UPDATE deliveries SET state = 'refused', reason = 'binding_closed', updated_at = ? WHERE delivery_id = ? AND state = 'queued'",[t.now,n.delivery_id]),He(e,n.target,n.delivery_id);return Ce(e,"UPDATE bindings SET session_durable_id = ?, revision = revision + 1, lease_started_at = ?, updated_at = ? WHERE binding_id = ? AND revision = ?",[t.session_durable_id,i,i,n.binding_id,n.revision]),Ce(e,"DELETE FROM completion_arms WHERE binding_id = ?",[n.binding_id]),{kind:"ok",binding:Lt(e,n.binding_id),closed:r.map(e=>e.delivery_id)}})}async function Dt(e,t){let n=void 0===t.cursor?null:function(e){try{let t=JSON.parse(Buffer.from(e,"base64url").toString("utf8"));if(1!==t.v||"number"!=typeof t.as_of||!Number.isInteger(t.as_of))return null;let n=t.after;return null===n||Array.isArray(n)&&2===n.length&&n.every(e=>"string"==typeof e)?{v:1,as_of:t.as_of,after:n}:null}catch{return null}}(t.cursor);if(void 0!==t.cursor&&null===n)return ft("cursor_invalid","The bindings cursor is not one this gateway issued.");let i=Math.min(Math.max(t.limit??50,1),200);return await Me(e,"list_bindings",()=>{Rt(e,t.now);let r=n?.as_of??Number(e.sql.one(["m"],"SELECT COALESCE(MAX(rowid), 0) AS m FROM bindings")?.m??0),s=["rowid <= ?"],o=[r],a=[["session_durable_id","session_durable_id"],["platform","platform"],["account_id","account_id"],["chat_id","chat_id"],["thread_id","thread_id"],["status","status"]];for(let[e,n]of a){let i=t.filter[e];void 0!==i&&(s.push(`${n} = ?`),o.push(i))}null!=n?.after&&(s.push("(created_at > ? OR (created_at = ? AND binding_id > ?))"),o.push(n.after[0],n.after[0],n.after[1]));let d=Nt(e,s.join(" AND "),o,{orderBy:"created_at, binding_id",limit:i+1}),u=d.slice(0,i),_=u.at(-1);return{kind:"ok",bindings:u,next_cursor:d.length>i&&void 0!==_?ge(r,[_.created_at,_.binding_id]):null}})}async function Mt(e,t){return await Me(e,"binding_view",()=>(Rt(e,t.now),Lt(e,t.binding_id)??null))}function $t(e,t){return ht(e.sql.one(["incarnation"],"SELECT incarnation FROM session_meta WHERE durable_id = ?",[t])?.incarnation)}function Ht(e,t){Ce(e,"INSERT INTO outbox (binding_id, revision, event_kind, payload, state, provider_message_id, created_at, acked_at, session_durable_id, reply_token, ui_request_id, ui_request_kind, incarnation, question_state, answer, answered_at, outcome) VALUES (?, ?, ?, ?, 'pending', NULL, ?, NULL, ?, ?, ?, ?, ?, ?, NULL, NULL, ?)",[t.binding.binding_id,t.binding.revision,t.event,JSON.stringify({text:t.text}),t.now,t.binding.session_durable_id,t.reply_token??null,t.ui_request_id??null,t.ui_request_kind??null,t.incarnation??null,void 0===t.reply_token?null:"pending",t.outcome??null]);let n=Number(e.sql.one(["c"],"SELECT last_insert_rowid() AS c")?.c);return vt(e,t.binding.binding_id,n,t.now),n}async function Wt(e,t){return await Ot(e,"report",t.now,t.receipt,()=>{let n=null===t.binding_id?function(e,t,n,i){let r=[...new Set(n.flatMap(n=>{let i=e.sql.one(["binding_id"],"SELECT binding_id FROM deliveries WHERE delivery_id = ? AND target_durable_id = ?",[n,t]),r=ht(i?.binding_id);return null===r?[]:[r]}))].sort(),s=e.sql.all(["binding_id"],"SELECT binding_id FROM bindings WHERE session_durable_id = ? AND status = 'active' AND direction_outbound = 1",[t],"binding_id").map(e=>String(e.binding_id)),[o,...a]=r;if(void 0!==o&&a.length>0)return ft("invalid_arguments",`This session's current run answers messages from ${r.length} bound threads; name binding_id.`,{binding_ids:r});if(void 0!==o&&i&&s.some(e=>e!==o))return ft("invalid_arguments","This session's current run answers a bound thread's message and a user message (typed in its terminal or sent by an extension) together; name binding_id.",{binding_ids:s});if(void 0!==o)return{binding_id:o};let[d,...u]=s;return void 0!==d&&0===u.length?{binding_id:d}:void 0===d?ft("invalid_arguments","This session has no active outbound binding and its current run answers no bound message; name binding_id."):ft("invalid_arguments",`This session has ${s.length} active outbound bindings and its current run answers no bound message; name binding_id.`,{binding_ids:s})}(e,t.session_durable_id,t.origin_delivery_ids,!0===t.origin_local_input):{binding_id:t.binding_id};if("kind"in n)return n;let i=n.binding_id,r=Lt(e,i);if(void 0===r)return ft("not_found","No binding has this id.",{binding_id:i});if(r.session_durable_id!==t.session_durable_id)return ft("scope_denied","The binding is attached to another session.",{binding_id:i});if("active"!==r.status)return ft("binding_inactive",`The binding is ${r.status}.`,{binding_id:i,status:r.status});if(!r.direction.outbound)return ft("unsupported","The binding carries no outbound direction.",{binding_id:i,direction:r.direction});if(!r.outbound_events.includes(t.event))return ft("unsupported",`The binding is not subscribed to ${t.event} events.`,{binding_id:i,outbound_events:r.outbound_events});if(null!==t.ui_request_kind&&"question"!==t.event)return ft("invalid_arguments","Only a question names a request kind.");let s={binding_id:i,revision:r.revision,event:t.event};if("completion"===t.event){Ce(e,"INSERT INTO completion_arms (session_durable_id, binding_id, revision, text, armed_at) VALUES (?, ?, ?, ?, ?)",[t.session_durable_id,i,r.revision,t.text,t.now]);let n=Number(e.sql.one(["s"],"SELECT last_insert_rowid() AS s")?.s);return{kind:"ok",...s,cursor:null,reply_token:null,armed:!0,arm_seq:n}}if("question"===t.event){if(null===t.ui_request_id)return ft("invalid_arguments","A question names the session's pending extension UI request (request_id).");let n=$t(e,t.session_durable_id),o=function(e,t){let n={b:t.binding_id,r:t.revision,s:t.session_durable_id,i:t.incarnation,q:t.ui_request_id,n:ee()},i=pe(JSON.stringify(n));return`${me}.${i}.${be(e,i)}`}(kt(e,"token_secret"),{binding_id:i,revision:r.revision,session_durable_id:t.session_durable_id,incarnation:n,ui_request_id:t.ui_request_id}),a=Ht(e,{binding:r,event:"question",text:t.text,now:t.now,reply_token:o,ui_request_id:t.ui_request_id,...null===t.ui_request_kind?{}:{ui_request_kind:t.ui_request_kind},incarnation:n});return xt(e,t.now),{kind:"ok",...s,cursor:a,reply_token:o,armed:!1,arm_seq:null}}let o=Ht(e,{binding:r,event:t.event,text:t.text,now:t.now});return xt(e,t.now),{kind:"ok",...s,cursor:o,reply_token:null,armed:!1,arm_seq:null}})}async function Xt(e,t){return await Me(e,"emit_completions",()=>{Rt(e,t.now);let n=["arm_seq","binding_id","revision","text"],i=void 0===t.through_arm_seq?e.sql.all(n,"SELECT arm_seq, binding_id, revision, text FROM completion_arms WHERE session_durable_id = ?",[t.session_durable_id],"arm_seq"):e.sql.all(n,"SELECT arm_seq, binding_id, revision, text FROM completion_arms WHERE session_durable_id = ? AND arm_seq <= ?",[t.session_durable_id,t.through_arm_seq],"arm_seq"),r=new Map;for(let t of i)Ce(e,"DELETE FROM completion_arms WHERE arm_seq = ?",[Number(t.arm_seq)]),r.set(String(t.binding_id),t);let s=[];for(let n of[...r.values()].toSorted((e,t)=>String(e.binding_id)<String(t.binding_id)?-1:1)){let i=Lt(e,String(n.binding_id));void 0!==i&&"active"===i.status&&i.revision===Number(n.revision)&&i.session_durable_id===t.session_durable_id&&(i.direction.outbound&&i.outbound_events.includes("completion")&&s.push({binding_id:i.binding_id,cursor:Ht(e,{binding:i,event:"completion",text:String(n.text),now:t.now,outcome:t.outcome})}))}return s}).then(n=>(e.emit({kind:"completions_emitted",session_durable_id:t.session_durable_id,cursors:n.map(e=>e.cursor)}),n))}function Ft(e,t){return Number(e.sql.one(["acked_cursor"],"SELECT acked_cursor FROM outbox_cursors WHERE binding_id = ?",[t])?.acked_cursor??0)}async function Pt(e,t){let n=Math.min(Math.max(t.limit??100,1),500);return await Me(e,"read_outbox",()=>{Rt(e,t.now),xt(e,t.now);let i=Lt(e,t.binding_id);if(void 0===i)return ft("not_found","No binding has this id.",{binding_id:t.binding_id});let r=Ft(e,i.binding_id),s=t.after_cursor??r,o=e.sql.all(Et,`SELECT ${Et.join(", ")} FROM outbox WHERE binding_id = ? AND cursor > ?${t.pendingOnly?" AND state = 'pending'":""} ORDER BY cursor LIMIT ?`,[i.binding_id,s,n],"cursor").map(e=>function(e,t){let n=e.event_kind;return{cursor:Number(e.cursor),binding_id:String(e.binding_id),revision:Number(e.revision),event:n,text:String(JSON.parse(String(e.payload)).text??""),state:e.state,created_at:ae(Number(e.created_at)),edit_message_id:"milestone"===n?t.progress_message_id:null,provider_message_id:ht(e.provider_message_id),reply_token:ht(e.reply_token),question_state:e.question_state??null,outcome:e.outcome??null,answered_by:Tt(e.answered_by),answer_state:e.answer_state??null}}(e,i));return he(e,t.now),{kind:"ok",binding_id:i.binding_id,revision:i.revision,status:i.status,rows:o,next_cursor:o.at(-1)?.cursor??s,acked_cursor:r}})}async function jt(e,t){return await Me(e,"ack_outbox",()=>{Rt(e,t.now);let n=Lt(e,t.binding_id);if(void 0===n)return ft("not_found","No binding has this id.",{binding_id:t.binding_id});let i=Ft(e,n.binding_id);if(t.cursor<=i)return{kind:"ok",binding_id:n.binding_id,acked_cursor:i,changed:!1};if(void 0===e.sql.one(["cursor"],"SELECT cursor FROM outbox WHERE binding_id = ? AND cursor = ?",[n.binding_id,t.cursor])){let i=Number(e.sql.one(["m"],"SELECT COALESCE(MAX(cursor), 0) AS m FROM outbox WHERE binding_id = ?",[n.binding_id])?.m??0);return ft("cursor_invalid",t.cursor>i?"The cursor is past the newest outbox row of this binding.":"The cursor names no outbox row of this binding.",{newest:i})}return Ce(e,"UPDATE outbox SET state = 'acked', acked_at = ? WHERE binding_id = ? AND cursor <= ? AND state = 'pending'",[t.now,n.binding_id,t.cursor]),Ce(e,"INSERT INTO outbox_cursors (binding_id, acked_cursor, updated_at) VALUES (?, ?, ?) ON CONFLICT(binding_id) DO UPDATE SET acked_cursor = excluded.acked_cursor, updated_at = excluded.updated_at",[n.binding_id,t.cursor,t.now]),void 0!==t.provider_message_id&&(Ce(e,"UPDATE outbox SET provider_message_id = ? WHERE binding_id = ? AND cursor = ?",[t.provider_message_id,n.binding_id,t.cursor]),"milestone"===e.sql.one(["event_kind"],"SELECT event_kind FROM outbox WHERE binding_id = ? AND cursor = ?",[n.binding_id,t.cursor])?.event_kind&&null===n.progress_message_id&&Ce(e,"UPDATE bindings SET progress_message_id = ?, updated_at = ? WHERE binding_id = ?",[t.provider_message_id,ae(t.now),n.binding_id])),{kind:"ok",binding_id:n.binding_id,acked_cursor:t.cursor,changed:!0}})}var Bt="The session no longer waits for this question (answered or closed elsewhere)";async function Gt(e,t){return await Me(e,"claim_answer",()=>{Rt(e,t.now);let n=function(e,t){let n=t.split(".");if(3!==n.length||n[0]!==me)return null;let i=Buffer.from(be(e,n[1])),r=Buffer.from(n[2]);if(i.length!==r.length||!te(i,r))return null;try{let e=JSON.parse(Buffer.from(n[1],"base64url").toString("utf8"));return"string"!=typeof e.b||"number"!=typeof e.r||"string"!=typeof e.s||"string"!=typeof e.q||null!==e.i&&"string"!=typeof e.i?null:{binding_id:e.b,revision:e.r,session_durable_id:e.s,incarnation:e.i??null,ui_request_id:e.q}}catch{return null}}(kt(e,"token_secret"),t.reply_token);if(null===n)return ft("invalid_arguments","This is not a reply token this gateway issued.");if(n.binding_id!==t.binding_id)return ft("binding_mismatch","The answer arrived through a different binding than the one that asked the question.",{binding_id:t.binding_id});let i=e.sql.one(["cursor","question_state","answer_state","answer","answered_at","answered_by","ui_request_id","ui_request_kind","session"],"SELECT cursor, question_state, answer_state, answer, answered_at, answered_by, ui_request_id, ui_request_kind, session_durable_id AS session FROM outbox WHERE reply_token = ? AND binding_id = ?",[t.reply_token,n.binding_id]);if(void 0===i)return ft("not_found","The question this token belongs to is no longer in the outbox.");let r="answered"===i.question_state&&"delivered"!==i.answer_state,s=r&&Number(i.answered_at)+12e4<=t.now;if(r&&!s)return ft("answer_in_progress","Another answer to this question is still being handed to the session.",{cursor:Number(i.cursor)});if("answered"===i.question_state&&!s)return ft("already_answered",null===i.answer&&"delivered"===i.answer_state?`${Bt}.`:"This question was already answered.",{cursor:Number(i.cursor)});let o="string"==typeof(d=i.ui_request_kind)&<.includes(d)?i.ui_request_kind:null,a=mt(o,t.answer);var d;if(!a.ok)return ft("invalid_arguments",a.reason,{ui_request_kind:o});let u=Lt(e,n.binding_id);return void 0===u||"active"!==u.status||u.revision!==n.revision||u.session_durable_id!==n.session_durable_id||$t(e,n.session_durable_id)!==n.incarnation||ht(i.ui_request_id)!==n.ui_request_id?ft("stale_token","The binding or the session changed since the question was asked.",{binding_id:n.binding_id}):(Ce(e,"UPDATE outbox SET question_state = 'answered', answer_state = 'in_flight', answer = ?, answered_at = ?, answered_by = ? WHERE reply_token = ?",[t.answer,t.now,yt(t.answered_by),t.reply_token]),{kind:"ok",session_durable_id:n.session_durable_id,ui_request_id:n.ui_request_id,ui_request_kind:o,cursor:Number(i.cursor),claimed_at:t.now,taken_over:s?{answer:ht(i.answer),answered_at:null===i.answered_at||void 0===i.answered_at?null:Number(i.answered_at),answered_by:Tt(i.answered_by)}:null})})}function Jt(e,t,n){let i=e.sql.one(["cursor","binding_id"],"SELECT cursor, binding_id FROM outbox WHERE reply_token = ?",[t]);void 0!==i&&vt(e,String(i.binding_id),Number(i.cursor),n)}async function Kt(e,t){return await Me(e,"release_answer",()=>{let n=1===Ce(e,"UPDATE outbox SET question_state = 'pending', answer_state = NULL, answer = NULL, answered_at = NULL, answered_by = NULL WHERE reply_token = ? AND question_state = 'answered' AND answer_state = 'in_flight' AND answered_at = ?",[t.reply_token,t.claimed_at]);return n&&Jt(e,t.reply_token,t.claimed_at),n})}async function Yt(e,t){return await Me(e,"mark_prior_delivered",()=>{let n=1===Ce(e,"UPDATE outbox SET answer_state = 'delivered', answer = ?, answered_at = ?, answered_by = ? WHERE reply_token = ? AND question_state = 'answered' AND answer_state = 'in_flight' AND answered_at = ?",[t.prior.answer,t.prior.answered_at,yt(t.prior.answered_by),t.reply_token,t.claimed_at]);return n&&Jt(e,t.reply_token,t.claimed_at),n})}async function Vt(e,t){return await Me(e,"confirm_answer",()=>{let n=1===Ce(e,"UPDATE outbox SET question_state = 'answered', answer_state = 'delivered', answer = ?, answered_at = ?, answered_by = ? WHERE reply_token = ? AND (answer_state IS NOT 'delivered' OR answer IS NULL)",[t.answer,t.claimed_at,yt(t.answered_by),t.reply_token]);return n&&Jt(e,t.reply_token,t.claimed_at),n})}import{extname as zt}from"node:path";var Qt=0,Zt=1,en=1,tn=2,nn=9,rn=10,sn=11,on=18,an=20,dn=21,un=23,_n=26,ln=27,cn=31,pn=33;class gn extends Error{code="extension_schema_violation"}var mn=["type","name","tbl_name","sql"],bn=e=>e.replace(/[A-Z]/g,e=>e.toLowerCase()),En=e=>`${String(e.type)}:${bn(String(e.name))}`;function fn(e){return{objects:e.all(mn,"SELECT type, name, tbl_name, sql FROM sqlite_schema",[],"type, name"),owners:new Map(e.all(["type","name","owner"],"SELECT type, name, owner FROM extension_objects").map(e=>[En(e),null===e.owner?null:String(e.owner)]))}}function hn(e,t,n,i){let r=new Map(n.objects.map(e=>[En(e),e])),s=new Map(i.objects.map(e=>[En(e),e])),o=[...new Set([...r.keys(),...s.keys()])].filter(e=>JSON.stringify(r.get(e))!==JSON.stringify(s.get(e)));for(let e of o){let a=r.get(e),d=s.get(e);if(void 0!==a&&n.owners.get(e)!==t)throw new gn(`Extension ${t} changed an object it does not own: ${String(a.name)}.`);if(void 0===d)continue;if("trigger"===d.type||"view"===d.type)throw new gn("Store extensions cannot create triggers or views.");let u=bn(String(d.name));if(("index"!==d.type||null!==d.sql||!u.startsWith(`sqlite_autoindex_${bn(String(d.tbl_name))}_`))&&!u.startsWith(`${t}_`)||n.owners.has(e)&&n.owners.get(e)!==t)throw new gn(`Extension ${t} created an object outside its namespace: ${String(d.name)}.`);if("index"===d.type){let e=`table:${bn(String(d.tbl_name))}`,a=!r.has(e)&&s.has(e)&&o.includes(e);if(n.owners.get(e)!==t&&i.owners.get(e)!==t&&!a)throw new gn(`Extension ${t} indexed a table it does not own: ${String(d.tbl_name)}.`)}}for(let n of o){let i=r.get(n),o=s.get(n);void 0!==i&&e.run("DELETE FROM extension_objects WHERE type = ? AND name = ? COLLATE NOCASE AND owner = ?",[String(i.type),String(i.name),t]),void 0!==o&&e.run("INSERT INTO extension_objects (type, name, owner) VALUES (?, ?, ?) ON CONFLICT(type, name) DO UPDATE SET owner = excluded.owner",[String(o.type),bn(String(o.name)),t])}}function yn(e,t,n){let i,r,s=fn(e),o=new Map(s.owners),a=new Set(s.objects.filter(e=>"trigger"===e.type||"view"===e.type).map(e=>bn(String(e.name)))),d=(e,n)=>null!==n&&o.get(`${e}:${n}`)===t,u=(e,n,i=!1)=>!(null===n||!i&&!n.startsWith(`${t}_`)||o.has(`${e}:${n}`))&&(o.set(`${e}:${n}`,t),!0),_=!1,l=Qt,c=e=>(r=`Extension ${t} does not own ${e??"this SQLite operation"}.`,Zt);try{return e.authorized((e,t,n,r,s)=>{let o=null===t?null:bn(t),p=null===n?null:bn(n),g=null===r?null:bn(r),m=null===s?null:bn(s);if(null!==m&&a.has(m))return c(m);switch(e){case tn:return i="create","main"===g&&u("table",o)?l:c(o);case en:return i="create","main"===g&&d("table",p)&&u("index",o,!0===o?.startsWith(`sqlite_autoindex_${p}_`))?l:c(o);case sn:return i="drop","main"===g&&d("table",o)?l:c(o);case rn:return i="drop","main"===g&&d("index",o)&&d("table",p)?l:c(o);case _n:return i="alter","main"===o&&d("table",p)?l:c(p);case ln:return"main"===g&&d("index",o)?l:c(o);case an:case on:case un:case nn:return"sqlite_master"===o||"sqlite_temp_master"===o?(e===un&&"sql"===p&&(_=!0),e!==an||"alter"===i||"drop"===i||"create"===i&&_&&"rowid"===p?l:c(o)):"alter"!==i||"sqlite_sequence"!==o||"name"!==p||e!==an&&e!==un?("main"===g||null===g&&e===an&&""===p)&&d("table",o)?l:c(o):l;case dn:case pn:return l;case cn:return"load_extension"===p?c(p):l;default:return c(o)}},n)}catch(e){if(void 0!==r)throw new gn(r);throw e}}import{createHash as Tn,randomUUID as vn}from"node:crypto";var wn={loop_detected:"Stop relaying here: a direct reply to the session that messaged you is a cycle. That session reads your answer with thread_read; a bound external thread gets it through thread_report, and its questions come back through thread_answer.",overloaded:"Wait before sending to this session again.",queue_full:"Wait for the target to take its queued messages, then send again.",idempotency_conflict:"Retry with a new idempotency_key.",idempotency_in_progress:"Wait for the earlier call to settle, then retry.",idempotency_uncertain:"Read the target transcript before deciding whether to send again; the gateway never resends.",invalid_arguments:"Fix the arguments and send again."};function Nn(e,t,n){return{kind:"error",error:Te(e,t,wn[e]??"Call thread_list and retry after checking the target.",n)}}function Ln(e){return t={target:e.target,text:e.text,mode:e.mode,expected_turn_id:e.expected_turn_id,binding:e.binding,author:e.author},Tn("sha256").update(JSON.stringify(t)).digest("hex");var t}function kn(e){let t=!1;for(let n of h(e))if("space"!==n.kind&&"comment"!==n.kind){if(t)throw new gn("An extension SQL call must contain one statement.");"semicolon"===n.kind&&(t=!0)}}var Rn=/^[ \t\r\n]*([A-Za-z_][A-Za-z0-9_]*)(?:[ \t\r\n]+COLLATE[ \t\r\n]+(BINARY|NOCASE|RTRIM))?(?:[ \t\r\n]+(ASC|DESC))?(?:[ \t\r\n]+NULLS[ \t\r\n]+(FIRST|LAST))?[ \t\r\n]*$/i,xn=e=>JSON.stringify(e.length>80?`${e.slice(0,80)}…`:e),Sn=e=>e?.toUpperCase();var On=/[\u0000-\u001f\u007f-\u009f\u2028\u2029]/;function An(e){return{kind:"refused",code:"invalid_arguments",message:e}}function qn(e,t,n){return"string"!=typeof t||0===t.trim().length?An(`author.${e} must be a non-empty string.`):t.length>n?An(`author.${e} is longer than ${n} characters.`):On.test(t)?An(`author.${e} carries a control character or line break.`):t.trim()}var Cn={binding_conflict:"Call thread_bindings to find the binding that holds this thread, then thread_rebind it with its revision, or thread_unbind it first.",binding_mismatch:"Answer through the binding that asked the question; the question stays pending.",binding_inactive:"Bind the thread again with thread_bind.",stale_revision:"Call thread_bindings for the current revision and retry with it.",stale_token:"The question was asked under an earlier binding revision or session runtime; wait for the session to ask again.",already_answered:"Nothing to do: the question has its answer.",answer_in_progress:"Retry after a moment: another answer is still being handed to the session, and the question goes back to pending if that fails.",idempotency_conflict:"Retry with a new idempotency_key.",idempotency_uncertain:"Call thread_bindings or thread_outbox to see the current state before retrying with a new key.",scope_denied:"Report through a binding attached to this session; thread_bindings lists them.",unsupported:"Report only the events the binding subscribes to, through a binding with an outbound direction.",not_found:"Call thread_bindings for the binding ids that exist.",cursor_invalid:"Read thread_outbox again and ack a cursor it returned.",host_unavailable:"Retry when the session is running; the question stays pending.",invalid_arguments:"Fix the arguments and call again."},In=new Set(["question_already_resolved","unknown_extension_ui_request","unknown_request"]);function Un(e,t,n,i){return{kind:"error",error:Te(e,t,i??Cn[e]??"Call thread_bindings and retry after checking the binding.",n)}}function Dn(e){return"refused"===e.kind?Un(e.code,e.message,e.details):e}function Mn(e){if(void 0===e)return;let t=function(e){let t=qn("platform_user_id",e.platform_user_id,256);if("string"!=typeof t)return t;let n=qn("display",e.display,256);if("string"!=typeof n)return n;if(void 0===e.user_id)return{platform_user_id:t,display:n};let i=qn("user_id",e.user_id,256);return"string"!=typeof i?i:{platform_user_id:t,display:n,user_id:i}}(e);return"kind"in t?Un("invalid_arguments",t.message):t}function $n(e){let t=Buffer.byteLength(e);return t>32768?Un("message_too_large",`The text is ${t} bytes, above the 32768-byte limit.`):void 0}function Hn(e,t,n){return void 0===e.idempotency_key?null:{principal:e.principal,operation:t,idempotency_key:e.idempotency_key,args_hash:(i=n,Q("sha256").update(ce(i)).digest("hex"))};var i}function Wn(e){let t=e.store,n=e.now??t.now,i=new Set,r=!1;async function s(e){await o(()=>t.releaseAnswer(e))}async function o(e){try{await e()}catch(n){if(!u(n)||r)throw n;let s=function(e,t,n){let i,r=!1,s=()=>{i=setTimeout(()=>{i=void 0,r||e().catch(e=>{r||(u(e)?s():n(e))})},t()),i.unref?.()};return s(),{cancel:()=>{r=!0,clearTimeout(i)}}}(()=>e().then(()=>{i.delete(s)}),()=>t.busyTimeoutMs,()=>{i.delete(s)});i.add(s)}}return{bind:async e=>{let i=function(e){if(!ne.includes(e.platform))return de(`platform must be one of ${ne.join(", ")}.`);let t={account_id:ue("account_id",e.account_id),chat_id:ue("chat_id",e.chat_id),thread_id:ue("thread_id",e.thread_id??"@chat"),root_message_id:_e("root_message_id",e.root_message_id),progress_message_id:_e("progress_message_id",e.progress_message_id),session_durable_id:ue("session",e.session_durable_id),policy_id:ue("policy_id",e.policy_id??"default")};for(let e of Object.values(t))if(le(e))return e;let n=e.direction??{inbound:!0,outbound:!0};if(!0!==n.inbound&&!0!==n.outbound)return de("A binding needs at least one direction: inbound, outbound, or both.");let i=e.inbound_mode??"auto";if(!re.includes(i))return de(`inbound_mode must be one of ${re.join(", ")}.`);let r=e.outbound_events??(n.outbound?ie:[]);if(r.some(e=>!ie.includes(e)))return de(`outbound_events may only name ${ie.join(", ")}.`);if(new Set(r).size!==r.length)return de("outbound_events must not repeat an event.");if(n.outbound&&0===r.length)return de("An outbound binding needs at least one outbound event.");let s=void 0===e.ttl_seconds?604800:e.ttl_seconds;return null!==s&&(!Number.isInteger(s)||s<1||s>se)?de("ttl_seconds must be an integer from 1 to 31622400, or null for a binding that never expires."):{platform:e.platform,account_id:t.account_id,chat_id:t.chat_id,thread_id:t.thread_id,root_message_id:t.root_message_id,progress_message_id:t.progress_message_id,session_durable_id:t.session_durable_id,direction:{inbound:!0===n.inbound,outbound:!0===n.outbound},inbound_mode:i,outbound_events:ie.filter(e=>r.includes(e)),policy_id:t.policy_id,ttl_seconds:s}}(e.binding);return Dn("kind"in i?i:await t.bind({now:n(),receipt:Hn(e,"thread_bind",i),binding:i}))},unbind:async e=>{let i={binding_id:e.binding_id,expected_revision:e.expected_revision};return Dn(await t.unbind({now:n(),receipt:Hn(e,"thread_unbind",i),...i}))},rebind:async e=>{let i={binding_id:e.binding_id,expected_revision:e.expected_revision,session_durable_id:e.session_durable_id};return Dn(await t.rebind({now:n(),receipt:Hn(e,"thread_rebind",i),...i}))},bindings:async e=>Dn(await t.listBindings({now:n(),...e})),report:async e=>{let i=$n(e.text);if(void 0!==i)return i;let r={session_durable_id:e.session_durable_id,binding_id:e.binding_id??null,origin_delivery_ids:e.origin_delivery_ids??[],origin_local_input:!0===e.origin_local_input,event:e.event,text:e.text,ui_request_id:e.request_id??null,ui_request_kind:e.request_kind??null};return Dn(await t.report({now:n(),receipt:Hn(e,"thread_report",r),...r}))},outbox:async e=>Dn(await t.readOutbox({now:n(),...e})),ack:async e=>Dn(await t.ackOutbox({now:n(),...e})),answer:async i=>{let r=$n(i.answer);if(void 0!==r)return r;let a=Mn(i.author);if(void 0!==a&&"kind"in a)return a;let d=a??null,u=await t.claimAnswer({now:n(),binding_id:i.binding_id,reply_token:i.reply_token,answer:i.answer,answered_by:d});if("ok"!==u.kind)return Dn(u);let _={reply_token:i.reply_token,claimed_at:u.claimed_at},l=e.endpoints.respondUi,c=null,p="";if(void 0!==l)try{c=await e.locate(u.session_durable_id)}catch(e){p=`: ${e instanceof Error?e.message:String(e)}`}if(void 0===l||null===c)return await s(_),Un(void 0===l?"unsupported":"host_unavailable",`The session that asked is not reachable to take the answer${p}.`,{session:u.session_durable_id});let g,m=mt(u.ui_request_kind,i.answer);if(!m.ok)return await s(_),Un("invalid_arguments",m.reason,{session:u.session_durable_id,ui_request_kind:u.ui_request_kind});try{g=await l(c,{ui_request_id:u.ui_request_id,fields:m.fields})}catch(e){return await s(_),Un("host_unavailable",`The answer could not be handed to the session: ${e instanceof Error?e.message:String(e)}`,{session:u.session_durable_id})}if(!g.delivered){let e=u.taken_over;if(null!==e&&In.has(g.error))return"question_already_resolved"===g.error?(await o(()=>t.markPriorDelivered({..._,prior:e})),Un("already_answered",`The session already took an earlier answer to this question (${g.error}).`,{session:u.session_durable_id,reason:g.error})):(await o(()=>t.markPriorDelivered({..._,prior:{answer:null,answered_at:_.claimed_at}})),Un("already_answered",`${Bt} (${g.error}).`,{session:u.session_durable_id,reason:g.error},"Nothing to do: the session no longer waits for this question."));await s(_);let n="invalid_response"===g.error||"question_incomplete"===g.error;return Un(n?"invalid_arguments":"stale_token",`The session refused the answer: ${g.error}`,{session:u.session_durable_id,reason:g.error},n?`The session could not read this answer (${g.error}); answer again with text it can take. The question stays pending.`:`The session refused the answer (${g.error}): it no longer waits on this question. Read thread_outbox for a newer question and answer that one.`)}return await async function(e){await o(()=>t.confirmAnswer(e))}({..._,answer:i.answer,answered_by:d}),{kind:"ok",binding_id:i.binding_id,cursor:u.cursor,session_durable_id:u.session_durable_id,answered_by:d}},inbound:async i=>{if(void 0!==i.mode&&!re.includes(i.mode))return Un("invalid_arguments",`A binding message is delivered as ${re.join(" or ")}, never ${String(i.mode)}.`,{mode:i.mode},"Send with mode auto or follow_up, or omit it for the binding's inbound_mode.");let r=Mn(i.author);if(void 0!==r&&"kind"in r)return r;let s=await t.bindingView({now:n(),binding_id:i.binding_id});if(null===s)return Un("not_found","No binding has this id.",{binding_id:i.binding_id});let o={principal:`binding:${s.binding_id}`,idempotency_key:`event:${i.event_id}`},a=await t.deliveryReceipt({now:n(),...o});if(null!==a&&null!==a.binding_revision&&a.args_hash===Ln({target:a.target_durable_id,text:i.text,mode:i.mode??s.inbound_mode,expected_turn_id:null,binding:{binding_id:s.binding_id,revision:a.binding_revision},author:r})){let e=a.completed?{kind:"replay",result:a.result}:await t.recoverDelivery({now:n(),...o,args_hash:a.args_hash});if("replay"===e?.kind){let t=e.result;return"ok"===t.kind?{...t,deduplicated:!0}:t}if("refused"===e?.kind)return Un(e.code,e.message,e.details)}if("active"!==s.status)return Un("binding_inactive",`The binding is ${s.status}.`,{binding_id:s.binding_id,status:s.status});if(!s.direction.inbound)return Un("unsupported","The binding carries no inbound direction.",{binding_id:s.binding_id});let d=i.mode??s.inbound_mode;return"auto"===d&&"follow_up"===s.inbound_mode?Un("invalid_arguments","The binding delivers at most follow_up; a message cannot ask for more than its binding's inbound_mode.",{binding_id:s.binding_id,mode:d,inbound_mode:s.inbound_mode},"Send with mode follow_up (or omit it), or rebind the thread with inbound_mode auto."):await e.engine.deliver({sender:{kind:"external",origin:{platform:s.platform,account_id:s.account_id,chat_id:s.chat_id,thread_id:s.thread_id,message_id:i.event_id,...void 0===r?{}:{author:r}},binding_id:s.binding_id,binding_revision:s.revision},target:s.session_durable_id,text:i.text,mode:d,all_scope:!0,idempotency_key:`event:${i.event_id}`})},settle:e=>t.emitCompletions({now:n(),...e}),dispose:()=>{r=!0;for(let e of i)e.cancel();i.clear()}}}class Xn extends Error{extension;code="extension_operation_failed";constructor(e){super(`The ${e} extension transaction has ended.`),this.extension=e}}class Fn extends Error{extension;code="extension_operation_failed";constructor(e){super(`The ${e} extension ran a statement while a tx helper (enqueue, bind, ...) was still in flight; await the helper first.`),this.extension=e}}function Pn(e,t,n,i){let r,s=!0,o=0,a=!1,d=Promise.resolve();function u(e){if(a)throw new Xn(t);if(void 0!==r)throw r;return e()}function _(e){if(!s)return Promise.reject(new Xn(t));o++;let n=d.then(()=>u(e));return d=n.then(()=>{o--},e=>{o--,r=e}),n}function l(n,i){if((()=>{if(!s)throw new Xn(t);if(void 0!==r)throw r})(),o>0)throw r=new Fn(t),r;let a=fn(e.sql);try{kn(n);let r=yn(e.sql,t,i);return hn(e.sql,t,a,fn(e.sql)),r}catch(e){throw r=e,e}}let c={now:()=>n,busyTimeoutMs:e.config.busy_timeout_ms,enqueue:t=>u(()=>Ye(e,t)),completeReceipt:t=>u(()=>nt(e,t)),abandonReceipt:t=>u(()=>it(e,t)),deliveryView:async t=>u(()=>ot(e,t)),deliveryReceipt:async t=>u(()=>rt(e,t)),recoverDelivery:t=>u(()=>st(e,t)),bind:t=>u(()=>qt(e,t)),unbind:t=>u(()=>It(e,t)),rebind:t=>u(()=>Ut(e,t)),listBindings:t=>u(()=>Dt(e,t)),bindingView:t=>u(()=>Mt(e,t)),report:t=>u(()=>Wt(e,t)),readOutbox:t=>u(()=>Pt(e,{...t,pendingOnly:!0})),ackOutbox:t=>u(()=>jt(e,t)),claimAnswer:t=>u(()=>Gt(e,t)),releaseAnswer:t=>u(()=>Kt(e,t)),confirmAnswer:t=>u(()=>Vt(e,t)),markPriorDelivered:t=>u(()=>Yt(e,t)),emitCompletions:t=>u(()=>Xt(e,t))},p={wake:async()=>{throw Error("An extension enqueue cannot contact a live endpoint.")}},g=function(e){let t=e.store,n=e.now??t.now;return{deliver:async function(i){let r=i.mode??"auto";if("steer"===r&&void 0===i.expected_turn_id)return Nn("invalid_arguments","A steer needs expected_turn_id (the target's current turn_epoch).");let s=Buffer.byteLength(i.text);if(s>H)return Nn("message_too_large",`The message is ${s} bytes, above the 1048576-byte limit.`);let o=await e.resolve(i.target,{all_scope:i.all_scope});if("error"===o.kind)return o;let a=o.target,d="routable"===a.liveness?a.endpoint:null;if(null===d&&"refuse"===function(e,t,n,i){return"offline"===e?"steer"===t?{kind:"refuse",reason:"turn_conflict"}:{kind:"queued_offline"}:"steer"===t?"mid_turn"!==e?{kind:"refuse",reason:"not_steerable"}:null===n||null===i||n!==i?{kind:"refuse",reason:"turn_conflict"}:{kind:"admit",deliverAs:"steer",expected_turn_id:n,expect:"steered"}:{kind:"admit",deliverAs:"followUp",expect:"idle"===e?"started":"queued"}}("offline",r,i.expected_turn_id??null,null).kind)return Nn("turn_conflict","The target has no live turn to steer.",{target:a.durable_id});let u=function(e){if("session"===e.kind)return{principal:`session:${e.durable_id}`,rate_principal:`session:${e.durable_id}`,node:e.durable_id,turn:e.turn_id??null,cause:e.cause_delivery_id??null,actor:e.name??e.durable_id,origin:()=>({session:e.durable_id}),binding:null};if("cli"===e.kind){let t=`cli:${e.uid}`;return{principal:t,rate_principal:t,node:t,turn:null,cause:null,actor:e.user,origin:t=>({external:{platform:"cli",account_id:e.user,chat_id:"@cli",thread_id:"@chat",message_id:t}}),binding:null}}let t=`binding:${e.binding_id}`,n=e.origin.author;return{principal:t,rate_principal:void 0===n?t:`${t}#author:${n.platform_user_id}`,node:t,turn:null,cause:null,actor:e.origin.account_id,origin:()=>({external:e.origin}),binding:{binding_id:e.binding_id,revision:e.binding_revision}}}(i.sender),_=vn(),l=i.idempotency_key??`auto:${_}`,c=await t.enqueue({now:n(),delivery_id:_,target_durable_id:a.durable_id,sender_principal:u.principal,rate_principal:u.rate_principal,sender_node:u.node,sender_turn:u.turn,cause_delivery_id:u.cause,claimed_root_id:i.root_id??null,origin:u.origin(_),actor:u.actor,body:i.text,mode:r,expected_turn_id:i.expected_turn_id??null,binding:u.binding,receipt:{idempotency_key:l,args_hash:Ln({target:a.durable_id,text:i.text,mode:r,expected_turn_id:i.expected_turn_id??null,binding:u.binding,author:"external"===i.sender.kind?i.sender.origin.author:void 0})},endpoint_kind:d?.kind??null});if("busy"===c.kind)return Nn("overloaded","The gateway store is locked by a suspended writer; nothing was sent.",{budget:"store_lock"});if("refused"===c.kind)return Nn(c.code,c.message,c.details);if("replay"===c.kind){let e=c.result;return"ok"===e.kind?{...e,deduplicated:!0}:e}let p=async e=>(await t.completeReceipt({now:n(),principal:u.principal,idempotency_key:l,result:e}),e);try{if(null===d)return await p(we(c.row,c.queue_position,null,!1,!0));let n=!0;try{await e.endpoints.wake(d,[_])}catch{n=!1}let i=await t.deliveryView(_);if(null===i)throw Error(`delivery ${_} vanished after commit`);return await p(we(i.row,i.queue_position,d.kind,!1,!n))}catch(e){throw await t.abandonReceipt({now:n(),principal:u.principal,idempotency_key:l,error_note:e instanceof Error?e.message:String(e)}).catch(()=>!1),e}}}}({store:c,endpoints:p,resolve:async(e,t)=>{let n=await i(e,t);return"error"===n.kind?n:{kind:"ok",target:{...n.target,endpoint:null,liveness:"dead"}}}}),m=Wn({store:c,engine:g,endpoints:p,locate:async()=>null});return{tx:{all:(t,n,i,r)=>l(n,()=>{for(let e of t)kn(e);if(void 0===r)return e.sql.all(t,n,i);let s=function(e){return e.split(",").map(t=>{let n=Rn.exec(t);if(null===n)throw new gn(`orderBy must list the statement's output columns, each optionally with COLLATE BINARY/NOCASE/RTRIM, ASC/DESC or NULLS FIRST/LAST; got ${xn(e)}. Put LIMIT, OFFSET and table-qualified ordering in the statement itself.`);return{column:n[1],collate:Sn(n[2]),direction:Sn(n[3]),nulls:Sn(n[4])}})}(r);return e.sql.all(["0"],`SELECT 0 FROM (${n}\n) WHERE 0`,i),e.sql.all(t,n,i,function(e,t){return e.map(e=>{let n=t.find(t=>bn(t)===bn(e.column));if(void 0===n)throw new gn(`orderBy must list the statement's output columns; ${xn(e.column)} is not one of them (${xn(t.join(", "))}). Select it in the statement or order by a column it returns.`);return[`"${n.replaceAll('"','""')}"`,e.collate&&`COLLATE ${e.collate}`,e.direction,e.nulls&&`NULLS ${e.nulls}`].filter(Boolean).join(" ")}).join(", ")}(s,e.sql.resultColumns(n)))}),one:(t,n,i)=>l(n,()=>{for(let e of t)kn(e);return e.sql.one(t,n,i)}),exec:(t,n)=>l(t,()=>e.sql.run(t,n)),enqueue:e=>_(()=>m.inbound(e)),outboxAck:e=>_(()=>m.ack(e)),bind:e=>_(()=>m.bind(e)),unbind:e=>_(()=>m.unbind(e)),rebind:e=>_(()=>m.rebind(e)),bindingFor:t=>_(()=>async function(e,t){return await Me(e,"binding_for",()=>(Rt(e,t.now),Nt(e,"platform = ? AND account_id = ? AND chat_id = ? AND thread_id = ? AND status = 'active'",[t.platform,t.account_id,t.chat_id,t.thread_id])[0]??null))}(e,{...t,now:n})),outboxPending:e=>_(()=>m.outbox(e))},cancel:()=>{s=!1,a=!0,m.dispose()},finish:async()=>{if(s=!1,await d,m.dispose(),void 0!==r)throw r}}}function jn(e,t){return{kind:"refused",code:e,message:t}}function Bn(e){let t=e instanceof Error?e.message:String(e);return e instanceof gn||e instanceof Ne?jn(e.code,t):u(e)?jn("gateway_lock_wait_exceeded",t):jn("extension_operation_failed",t)}class Gn{ctx;resolveTarget;registered=new Map;constructor(e,t){this.ctx=e,this.resolveTarget=t}async register(e,t){if(!/^[a-z][a-z0-9_]{1,31}$/.test(e.name)||!Array.isArray(e.migrations)||!e.migrations.every(e=>Array.isArray(e)&&e.every(e=>"string"==typeof e)))return jn("invalid_arguments","An extension needs a valid namespace and an array of SQL migration steps.");try{!function(e,t){let n=e.all(["name"],"SELECT name FROM extension_objects WHERE owner IS NULL");if(["gateway","thread","sqlite"].includes(t)||n.some(e=>bn(String(e.name)).startsWith(t)))throw new gn(`Extension name ${t} collides with a core namespace.`)}(this.ctx.sql,e.name)}catch(e){return Bn(e)}let n;try{let t=new URL(e.moduleUrl);if("file:"!==t.protocol||![".js",".mjs",".cjs"].includes(zt(t.pathname)))return jn("extension_import_failed","moduleUrl must name a compiled JavaScript file URL.");n=await(import(e.moduleUrl))}catch(t){return jn("extension_import_failed",`Cannot import extension ${e.name}: ${t instanceof Error?t.message:String(t)}`)}try{let i=await this.ensure(e,t);return this.registered.set(e.name,{descriptor:e,module:n}),{kind:"ok",value:{version:i}}}catch(t){return t instanceof Ne||this.registered.set(e.name,{descriptor:e,module:n}),Bn(t)}}holds(e){return this.registered.get(e.name)?.descriptor===e}async ensure(e,t){let{name:n,migrations:i}=e,r=this.ctx.sql.one(["version"],"SELECT version FROM extension_schema WHERE name = ?",[n]);if(void 0!==r&&Number(r.version)===i.length)return i.length;for(;;){let e=await Me(this.ctx,"extension_migrate",()=>{let e=this.ctx.sql.one(["version"],"SELECT version FROM extension_schema WHERE name = ?",[n]),r=Number(e?.version??0);if(r>i.length)throw new Ne(r,i.length,`Extension ${n}`);let s=fn(this.ctx.sql);if(void 0===e&&s.objects.some(e=>bn(String(e.name)).startsWith(`${n}_`)&&null==s.owners.get(`${String(e.type)}:${bn(String(e.name))}`)))throw new gn(`Namespace ${n} already contains unowned objects.`);if(r===i.length)return void 0===e&&this.ctx.sql.run("INSERT INTO extension_schema (name, version, updated_at) VALUES (?, 0, ?)",[n,t]),{version:r,applied:!1};for(let e of i[r]){kn(e);let t=fn(this.ctx.sql);yn(this.ctx.sql,n,()=>this.ctx.sql.exec(e)),hn(this.ctx.sql,n,t,fn(this.ctx.sql))}return hn(this.ctx.sql,n,s,fn(this.ctx.sql)),this.ctx.sql.run("INSERT INTO extension_schema (name, version, updated_at) VALUES (?, ?, ?) ON CONFLICT(name) DO UPDATE SET version = excluded.version, updated_at = excluded.updated_at",[n,r+1,t]),{version:r+1,applied:!0}});if(!e.applied)return e.version}}async call(e,t,n,i){let r=this.registered.get(e);if(void 0===r)return jn("extension_unknown_name",`No store extension is registered as ${e}.`);let s=Object.hasOwn(r.module,t)?r.module[t]:void 0;if("function"!=typeof s)return jn("extension_unknown_op",`Extension ${e} exports no operation ${t}.`);let o,a=[],d=[];try{let u=await this.ensure(r.descriptor,i);o=await Me(this.ctx,"extension_call",async()=>{let o=this.ctx.sql.one(["version"],"SELECT version FROM extension_schema WHERE name = ?",[e]),_=Number(o?.version??0);if(_!==u)throw new Ne(_,r.descriptor.migrations.length,`Extension ${e}`);let l,c=fn(this.ctx.sql),p=Pn({...this.ctx,afterCommit:a,afterRollback:d},e,i,this.resolveTarget);try{let i=async()=>{try{return await s(p.tx,n)}finally{await p.finish()}},r=new Promise((n,i)=>{l=setTimeout(()=>{p.cancel(),i(Error(`Extension ${e}.${t} exceeded its ${this.ctx.config.lock_wait_max_ms} ms operation budget.`))},this.ctx.config.lock_wait_max_ms)}),o=await Promise.race([i(),r]);return hn(this.ctx.sql,e,c,fn(this.ctx.sql)),structuredClone(o)}finally{clearTimeout(l),p.cancel()}})}catch(t){for(let t of d)try{t()}catch(t){this.ctx.emit({kind:"extension_error",extension:e,phase:"after_rollback",error:t instanceof Error?t.message:String(t)})}return Bn(t)}await this.ctx.hook("afterDbCommit");for(let t of a)try{t()}catch(t){this.ctx.emit({kind:"extension_error",extension:e,phase:"after_commit",error:t instanceof Error?t.message:String(t)})}return{kind:"ok",value:o}}}var Jn=a;if(null===Jn)throw Error("the gateway store worker must run as a worker thread");var Kn,Yn,Vn,zn,Qn=[],Zn=new Map,ei=!1,ti=1,ni=new Map,ii=(e,t)=>new Promise((n,i)=>{let r=ti++;ni.set(r,{resolve:n,reject:i}),Jn.postMessage({type:"resolve",id:r,address:e,request:t})});function ri(e){si(e instanceof Xn?{kind:"extension_error",extension:e.extension,phase:"stale_transaction",error:e.message}:{kind:"extension_error",extension:zn??"worker",phase:"async",error:e instanceof Error?e.message:String(e)})}function si(e){Jn?.postMessage({type:"event",event:e})}async function oi(a,u){if("init"===a)return await async function(a){let{config:u}=a,c=l(u.agent_dir);i(s(c,"inbox"),{recursive:!0,mode:448}),e(c,448);let p=(g=u.agent_dir,_(l(g),"gateway.sqlite"));var g;t(r(p,"a",384)),e(p,384);let m=await(import("node:sqlite"));Yn=new m.DatabaseSync(p,{timeout:u.busy_timeout_ms});let E=new y(Yn);E.exec(`PRAGMA busy_timeout = ${Math.trunc(u.busy_timeout_ms)}`);let f=Date.now();for(;;)try{E.exec("PRAGMA journal_mode = WAL");break}catch(e){if(!v(e))throw e;let t=Date.now()-f;if(t+10>u.lock_wait_max_ms)throw si({kind:"lock_wait_exceeded",op:"open",waited_ms:t}),d("open",t,u.lock_wait_max_ms);await o(10)}E.exec("PRAGMA synchronous = FULL");for(let t of["-wal","-shm"])n(`${p}${t}`)&&e(`${p}${t}`,384);let h={pid:process.pid,process_start_time:await b(process.pid),instance_id:u.instance_id,runtime_instance:u.runtime_instance};Kn={sql:E,config:u,self:h,stats:{writes:0,marker_unlinks:0,transactions:0},emit:si,hook:ai,delay:e=>o(e)};try{await async function(e){if(!(je(e)>=Le.length))for(;;){let t=je(e);if(t>=Le.length)return;let n=Ge.has(t),i=n?Number(e.sql.one(["foreign_keys"],"SELECT foreign_keys FROM pragma_foreign_keys")?.foreign_keys??1):1;n&&e.sql.exec("PRAGMA foreign_keys = OFF");try{await Me(e,"migrate",()=>{let i=je(e);if(i===t){for(let t of Le[i])e.sql.exec(t);if(n&&e.sql.all(["parent"],"SELECT parent FROM pragma_foreign_key_check").filter(e=>"bindings"===e.parent).length>0)throw new Be(i+1,`gateway schema ${i+1} would leave a foreign-key violation referencing bindings; staying at v${i}`);e.sql.exec(`PRAGMA user_version = ${i+1}`)}})}finally{n&&e.sql.exec("PRAGMA foreign_keys = "+(0===i?"OFF":"ON"))}}}(Kn),Vn=new Gn(Kn,ii);let e=await async function(e,t){let n=0;for(let i of e.config.legacy_mailbox_directories){let r,s=`legacy_mailbox:${i}`;if(void 0!==e.sql.one(["value"],"SELECT value FROM gateway_meta WHERE key = ?",[s]))continue;try{r=J(i)}catch(t){e.emit({kind:"legacy_mailbox_invalid",directory:i,error:t instanceof Error?t.message:String(t)});continue}if(null===r)continue;let o=at(),a=r.filter(e=>!xe.test(e.target)).map(e=>({message_seq:e.message_seq,target:e.target,reason:"invalid_target"})),d=r.filter(e=>xe.test(e.target));n+=await Me(e,"migrate_legacy",()=>{let n=0;for(let r of d){let s=`legacy-${Pe([i,r.message_seq]).slice(0,32)}`;if(void 0!==qe(e,s))continue;let a=`root-${N()}`,d=`legacy:${i}`;Ce(e,"INSERT INTO causal_roots (root_id, origin_principal, created_at, expires_at) VALUES (?, ?, ?, ?)",[a,d,t,t+$]);let u=Date.parse(r.accepted_at);Fe(e,{delivery_id:s,target_durable_id:r.target,seq:Xe(e,r.target),sender:d,sender_turn:null,envelope:{origin:{external:{platform:"legacy_mailbox",account_id:o,chat_id:i,thread_id:"@chat",message_id:r.operation_id}},actor:"legacy thread mailbox",via:[d],root_id:a,hop:1,delivery_id:s},body:r.message,bytes:Buffer.byteLength(r.message),mode_requested:r.delivery,mode_effective:null,expected_turn_id:r.expected_turn_id,state:"queued",reason:null,admitted_by:null,claimed_at:null,attempt:0,admission_kind:null,turn_epoch:null,root_id:a,hop:1,created_at:Number.isFinite(u)?u:t,updated_at:t,expires_at:t+M,binding_id:null,binding_revision:null,actor_user_id:null}),$e(e,r.target,s,!0),n++}return Ce(e,"INSERT INTO gateway_meta (key, value) VALUES (?, ?)",[s,JSON.stringify({migrated_at:t,count:n,skipped:a})]),n}),a.length>0&&e.emit({kind:"legacy_mailbox_skipped",directory:i,items:a})}return n}(Kn,a.now);return Kn.stats.writes=0,Kn.stats.transactions=0,Kn.stats.marker_unlinks=0,{self:h,legacy_migrated:e}}catch(e){try{Yn.exec("ROLLBACK")}catch{}try{Yn.close()}finally{Yn=void 0,Kn=void 0,si({kind:"store_closed"})}throw e}}(u);if("close"===a)return Yn?.close(),Yn=void 0,Kn=void 0,Vn=void 0,null;let c=function(){if(void 0===Kn)throw Error("the gateway store is not open");return Kn}();switch(a){case"extension_register":{let e=u;return zn=e.extension.name,{result:await(Vn?.register(e.extension,e.now)),retained:!0===Vn?.holds(e.extension)}}case"extension_call":{let e=u;zn=e.name;try{return await(Vn?.call(e.name,e.op,e.args,e.now))}finally{for(let t of ni.values())t.reject(new Xn(e.name));ni.clear()}}case"enqueue":return await Ye(c,u);case"reconcile":return await tt(c,u);case"claim":return await async function(e,t){return await Me(e,"claim",()=>{let n=qe(e,t.delivery_id);return void 0===n||"queued"!==n.state?{kind:"lost",state:n?.state??null}:n.expires_at<=t.now?(et(e,n,"refused",t.now,{reason:"expired"}),He(e,n.target_durable_id,n.delivery_id),{kind:"lost",state:"refused"}):(Ce(e,"UPDATE deliveries SET state = 'admitting', admitted_by = ?, claimed_at = ?, attempt = attempt + 1, mode_effective = ?, updated_at = ? WHERE delivery_id = ? AND state = 'queued'",[JSON.stringify(t.self),t.now,t.lane,t.now,t.delivery_id]),He(e,n.target_durable_id,n.delivery_id),{kind:"claimed",row:qe(e,t.delivery_id)})})}(c,u);case"record_outcome":return await async function(e,t){return await Me(e,"record_outcome",()=>{let n=qe(e,t.delivery_id);if(void 0===n||"admitting"!==n.state&&"admitted"!==n.state||null===n.admitted_by||!f(n.admitted_by,t.self))return{kind:"lost",state:n?.state??null};let i=t.outcome;return"requeue"===i.kind?et(e,n,"queued",t.now):"refused"===i.kind?et(e,n,"refused",t.now,{reason:i.reason}):(Ce(e,"UPDATE deliveries SET admission_kind = ?, turn_epoch = ?, updated_at = ? WHERE delivery_id = ?",[i.admission_kind,i.turn_epoch,t.now,n.delivery_id]),n.state!==i.kind&&et(e,n,i.kind,t.now)),{kind:"claimed",row:qe(e,t.delivery_id)}})}(c,u);case"refuse_queued":return await async function(e,t){return await Me(e,"refuse",()=>{let n=qe(e,t.delivery_id);return void 0!==n&&"queued"===n.state&&(et(e,n,"refused",t.now,{reason:t.reason}),He(e,n.target_durable_id,n.delivery_id),!0)})}(c,u);case"complete_receipt":return await nt(c,u);case"abandon_receipt":return await it(c,u);case"delivery_view":return ot(c,u);case"delivery_receipt":return rt(c,u);case"recover_delivery":return await st(c,u);case"list":return function(e,t){return void 0!==t.target_durable_id?Ae(e,"target_durable_id = ?",[t.target_durable_id],"seq"):void 0!==t.root_id?Ae(e,"root_id = ?",[t.root_id],"created_at, seq"):Ae(e,"1 = 1",[],"created_at, seq")}(c,u);case"is_referenced":return function(e,t){return Number(e.sql.one(["n"],`SELECT COUNT(*) AS n FROM deliveries WHERE target_durable_id = ? AND state IN ${Re}`,[t])?.n??0)>0||Number(e.sql.one(["n"],"SELECT COUNT(*) AS n FROM bindings WHERE session_durable_id = ? AND status = 'active'",[t])?.n??0)>0||We(e,t).length>0}(c,u);case"journal_mode":return function(e){return String(e.sql.one(["journal_mode"],"SELECT journal_mode FROM pragma_journal_mode()")?.journal_mode??"")}(c);case"stats":return function(e){return{...e.stats}}(c);case"tool_receipt_begin":return await At(c,u);case"tool_receipt_settle":return await async function(e,t){return await Me(e,"tool_receipt_settle",()=>{let n="result"in t;return 1===Ce(e,`UPDATE receipts SET status = ?, ${n?"result":"error_note"} = ?, updated_at = ? WHERE principal = ? AND operation = ? AND idempotency_key = ? AND status = 'prepared' AND owner_instance = ?`,[n?"completed":"uncertain",n?JSON.stringify(t.result):t.error_note,t.now,t.principal,t.operation,t.idempotency_key,e.config.instance_id])})}(c,u);case"bind":return await qt(c,u);case"unbind":return await It(c,u);case"rebind":return await Ut(c,u);case"list_bindings":return await Dt(c,u);case"binding_view":return await Mt(c,u);case"register_incarnation":return await async function(e,t){await Me(e,"register_incarnation",()=>{Ce(e,"INSERT INTO session_meta (durable_id, next_seq, incarnation, endpoint_socket, endpoint_kind)\n VALUES (?, (SELECT COALESCE(MAX(seq), 0) + 1 FROM deliveries WHERE target_durable_id = ?), ?, ?, ?)\n ON CONFLICT(durable_id) DO UPDATE SET incarnation = excluded.incarnation,\n endpoint_socket = excluded.endpoint_socket, endpoint_kind = excluded.endpoint_kind",[t.durable_id,t.durable_id,t.incarnation,t.endpoint?.socket??null,t.endpoint?.kind??null])})}(c,u);case"clear_endpoint":return await async function(e,t){await Me(e,"clear_endpoint",()=>{Ce(e,"UPDATE session_meta SET endpoint_socket = NULL, endpoint_kind = NULL WHERE durable_id = ? AND incarnation = ?",[t.durable_id,t.incarnation])})}(c,u);case"session_owner":return function(e,t){let n=e.sql.one(["incarnation","endpoint_socket","endpoint_kind"],"SELECT incarnation, endpoint_socket, endpoint_kind FROM session_meta WHERE durable_id = ?",[t]);return void 0===n||"string"!=typeof n.incarnation?null:{incarnation:n.incarnation,endpoint:"string"!=typeof n.endpoint_socket||"tui"!==n.endpoint_kind&&"rpc_host"!==n.endpoint_kind?null:{socket:n.endpoint_socket,kind:n.endpoint_kind}}}(c,u);case"report":return await Wt(c,u);case"emit_completions":return await Xt(c,u);case"pending_completion_arms":return function(e,t){return Number(e.sql.one(["n"],"SELECT COUNT(*) AS n FROM completion_arms WHERE session_durable_id = ?",[t])?.n??0)}(c,u);case"latest_completion_arm":return function(e,t){let n=e.sql.one(["s"],"SELECT MAX(arm_seq) AS s FROM completion_arms WHERE session_durable_id = ?",[t])?.s;return null==n?null:Number(n)}(c,u);case"read_outbox":return await Pt(c,u);case"ack_outbox":return await jt(c,u);case"claim_answer":return await Gt(c,u);case"release_answer":return await Kt(c,u);case"confirm_answer":return await Vt(c,u);case"mark_prior_delivered":return await Yt(c,u);case"close_question":return await async function(e,t){return await Me(e,"close_question",()=>{let n=e.sql.all(["cursor","binding_id"],"SELECT cursor, binding_id FROM outbox WHERE event_kind = 'question' AND session_durable_id = ? AND ui_request_id = ? AND (question_state = 'pending' OR (question_state = 'answered' AND answer_state = 'in_flight'))",[t.session_durable_id,t.ui_request_id],"cursor");for(let i of n)Ce(e,"UPDATE outbox SET question_state = 'answered', answer_state = 'delivered', answer = NULL, answered_at = ?, answered_by = NULL WHERE cursor = ? AND (question_state = 'pending' OR (question_state = 'answered' AND answer_state = 'in_flight'))",[t.now,Number(i.cursor)]),vt(e,String(i.binding_id),Number(i.cursor),t.now);return n.length})}(c,u);default:throw Error(`unknown gateway store op: ${a}`)}}async function ai(e){let t=Kn?.config.test_hooks[e];if(void 0===t)return;if("throw"===t)throw Error(`gateway test hook ${e}`);"sigkill"===t&&(process.kill(process.pid,"SIGKILL"),await new Promise(()=>{}));let n=new Promise(t=>Zn.set(e,t));si({kind:"paused",hook:e}),await n}process.on("uncaughtException",ri),process.on("unhandledRejection",ri),Jn.on("message",e=>{if("resolution"===e.type){let t=ni.get(e.id);return ni.delete(e.id),void(e.ok?t?.resolve(e.value):t?.reject(Error(e.error)))}if("resume"===e.type)return Zn.get(e.hook)?.(),void Zn.delete(e.hook);Qn.push(e),async function(){if(ei)return;ei=!0;try{for(let e=Qn.shift();void 0!==e;e=Qn.shift())try{let t=await oi(e.op,e.args);Jn?.postMessage({type:"response",id:e.id,ok:!0,value:t})}catch(t){Jn?.postMessage({type:"response",id:e.id,ok:!1,error:{message:t instanceof Error?t.message:String(t),stack:t instanceof Error?t.stack:void 0,code:"string"==typeof t?.code?t.code:void 0}})}}finally{ei=!1}}()});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
// omo:
|
|
2
|
+
// omo:MMZYSOIyRv5_u38u8BfuLqDdPq5-KPM9q7lt6cXBFVY:8gj0rEwlIh1y6qmmNKU7Yp1gOB-a6gI2ONgPZCrDWb0
|
|
3
3
|
import{spawn as t}from"node:child_process";import*as e from"node:fs";import*as r from"node:fs/promises";function n(t){if(t instanceof Error&&"code"in t)return"string"==typeof t.code?t.code:void 0}async function i(t){for(let e=0;;e+=1)try{return await t()}catch(t){if("EINTR"!==n(t)||e>=128)throw t}}function o(t){for(let e=0;;e+=1)try{return t()}catch(t){if("EINTR"!==n(t)||e>=128)throw t}}var a=new Set(["EPERM","EBUSY","EACCES"]),s=[10,25,50,100,200,400],l=t=>new Promise(e=>setTimeout(e,t));import{Buffer as c}from"node:buffer";import{constants as u}from"node:fs";function d(t){return"number"==typeof t?0!==(t&u.O_EXCL):"string"==typeof t&&t.toLowerCase().includes("x")}async function f(t,e,r,n){let o="string"==typeof e?c.from(e,r??"utf8"):e,a=0;for(;a<o.length;){n?.throwIfAborted();let{bytesWritten:e}=await i(()=>t.write(o,a));if(e<=0)throw Error(`fs write made no progress at offset ${a}`);a+=e}}function m(t){return"function"==typeof t}function p(t){return(...e)=>i(async()=>t(...e))}function h(t){return(...e)=>o(()=>t(...e))}function w(t,e){let r="async"===e?p:h,n=new Map;return new Proxy(t,{get(t,e){if(n.has(e))return n.get(e);let i=Reflect.get(t,e);if(!m(i))return i;let o=r(i),a=Reflect.get(i,"native");return m(a)&&Object.assign(o,{native:r(a)}),n.set(e,o),o}})}var y=new Set(["stat","sync","datasync","chmod","chown","utimes","truncate"]);var v=w(r,"async");function g(t,e){return async function(t,e,r){let i="win32"===r.platform?r.delaysMs??s:[],o=r.sleep??l;for(let s=0;;s+=1)try{return void await r.rename(t,e)}catch(t){let e=i[s],r=n(t);if(void 0===e||void 0===r||!a.has(r))throw t;await o(e)}}(t,e,{rename:v.rename,platform:process.platform})}var b=w(e,"sync"),{access:E,chmod:S,copyFile:_,cp:k,link:x,lstat:O,mkdir:I,mkdtemp:N,readFile:$,readdir:M,readlink:T,realpath:R,rm:A,rmdir:P,stat:D,statfs:C,unlink:L}=v,{accessSync:j,chmodSync:F,fsyncSync:U,linkSync:q,lstatSync:B,mkdirSync:z,readFileSync:J,readdirSync:W,realpathSync:G,statSync:H,unlinkSync:K,writeFileSync:Z,writeSync:Y}=b,{constants:V,watch:X}=e,Q=async(...t)=>{let e=()=>r.open(...t);return function(t){return new Proxy(t,{get(t,e){let r=Reflect.get(t,e);if(!m(r))return r;let o=e=>Reflect.apply(r,t,[...e]);return"close"===e||e===Symbol.asyncDispose?(...t)=>{let e=o(t);return e instanceof Promise?e.catch(t=>{if("EINTR"!==n(t))throw t}):e}:"string"==typeof e&&y.has(e)?(...t)=>{let e=o(t);return e instanceof Promise?e.catch(e=>{if("EINTR"!==n(e))throw e;return i(async()=>o(t))}):e}:(...t)=>o(t)}})}(await(d(t[1]??"r")?e():i(e)))},tt=(t,r,n)=>function(t,e,r,n){return d(r)?t(e,r,n):o(()=>t(e,r,n))}(e.openSync,t,r,n??void 0);function et(t){try{e.closeSync(t)}catch(t){if("EINTR"!==n(t))throw t}}function rt(t){try{return o(()=>e.statSync(t)),!0}catch{return!1}}import{join as nt}from"node:path";import{fileURLToPath as it}from"node:url";import{constants as ot}from"node:os";var at={darwin:{names:["/usr/lib/libSystem.B.dylib"],errnoSymbol:"__error"},linux:{names:["libc.so.6","libc.musl-x86_64.so.1","libc.musl-aarch64.so.1","libc.so"],errnoSymbol:"__errno_location"}},st=null;function lt(){return st??=async function(){let t,e=at[process.platform];if(void 0===e)return null;try{t=await(import("bun:ffi"))}catch{return null}for(let r of e.names)try{let n=t.dlopen(r,{flock:{args:[t.FFIType.i32,t.FFIType.i32],returns:t.FFIType.i32},[e.errnoSymbol]:{args:[],returns:t.FFIType.ptr}}),i=n.symbols[e.errnoSymbol];if(void 0===i)continue;return(e,r)=>{let o=Number(n.symbols.flock(e,r));if(0===o)return{rc:o,errno:0};let a=i();return{rc:o,errno:null===a?-1:t.read.i32(a,0)}}}catch{continue}return null}(),st}async function ct(){return null!==await lt()}ot.errno;import ut from"node:path";import dt from"node:path";import{randomUUID as ft}from"node:crypto";import{hostname as mt}from"node:os";import{execFile as pt}from"node:child_process";var ht=null;function wt(){return ht??=async function(){if("darwin"!==process.platform)return null;try{let{dlopen:t,FFIType:e}=await(import("bun:ffi")),r=t("/usr/lib/libSystem.B.dylib",{proc_pidinfo:{args:[e.i32,e.i32,e.u64,e.ptr,e.i32],returns:e.i32}});return(t,e,n,i,o)=>Number(r.symbols.proc_pidinfo(t,e,n,i,o))}catch{return null}}(),ht}var yt=null;async function vt(t){if("win32"!==process.platform)return null;if(!Number.isSafeInteger(t)||t<=0)return null;yt??=async function(){if("win32"!==process.platform)return null;try{let{dlopen:t,FFIType:e}=await(import("bun:ffi"));return t("kernel32.dll",{OpenProcess:{args:[e.u32,e.i32,e.u32],returns:e.ptr},GetProcessTimes:{args:[e.ptr,e.ptr,e.ptr,e.ptr,e.ptr],returns:e.i32},CloseHandle:{args:[e.ptr],returns:e.i32}}).symbols}catch{return null}}();let e=await yt;if(null===e)return null;let r=e.OpenProcess(4096,0,t);if(null===r||0===r||0n===r)return null;try{let t=new BigUint64Array(1),n=e.GetProcessTimes(r,t,new BigUint64Array(1),new BigUint64Array(1),new BigUint64Array(1)),i=t[0]??0n;return 0===n||0n===i?null:i}finally{e.CloseHandle(r)}}async function gt(t,e){return await new Promise(r=>{pt(t,e,{encoding:"utf8",timeout:2e3,windowsHide:!0},(t,e)=>{if(null!==t)return void r(null);let n=e.trim();r(n.length>0?n:null)})})}var bt=function(t,e){let r;return n=>n!==e?t(n):(r??=t(n).then(t=>(null===t&&(r=void 0),t),t=>{throw r=void 0,t}),r)}(async function(t){if("linux"===process.platform)return await async function(t){try{let e=await $(`/proc/${t}/stat`,"utf8"),r=e.lastIndexOf(")");if(r<0)return null;let n=e.slice(r+2).trim().split(/\s+/)[19];return void 0===n?null:`linux-proc-start-ticks:${n}`}catch{return null}}(t);if("darwin"===process.platform||"freebsd"===process.platform){if("dead"===St(t))return null;let e=await async function(t){if("darwin"!==process.platform)return null;if(!Number.isSafeInteger(t)||t<=0)return null;let e=await wt();if(null===e)return null;let r=new Uint8Array(136),n=0;try{n=e(t,3,0n,r,136)}catch{return null}if(136!==n)return null;let i=new DataView(r.buffer,r.byteOffset,r.byteLength),o=Number(i.getBigUint64(120,!0));return o>0?o:null}(t);if(null!==e)return`proc-start-epoch:${e}`;let r=await gt("/bin/ps",["-o","lstart=","-p",String(t)]);return null===r?null:`ps-lstart:${r.replace(/\s+/g," ")}`}return"win32"===process.platform?await async function(t){if("dead"===St(t))return null;let e=await vt(t);if(null!==e)return`win32-creation-filetime:${e}`;let r=await gt("powershell.exe",["-NoProfile","-Command",`(Get-Process -Id ${t} -ErrorAction SilentlyContinue).StartTime.ToUniversalTime().ToString('o')`]);return null===r?null:`win32-creation-date:${r}`}(t):null},process.pid);function Et(t){let e=t.indexOf(":");return e<=0?null:t.slice(0,e)}function St(t){try{return process.kill(t,0),"alive"}catch(t){let e=function(t){if(t instanceof Error&&"code"in t)return"string"==typeof t.code?t.code:void 0}(t);return"ESRCH"===e?"dead":"EPERM"===e?"alive":"unknown"}}function _t(t){return"string"==typeof t&&t.length>0}function kt(t){let e;try{e=JSON.parse(t)}catch{return null}if("object"!=typeof e||null===e||Array.isArray(e))return null;let r=e;return!Number.isInteger(r.pid)||Number(r.pid)<=0?null:_t(r.process_start)&&_t(r.hostname)&&_t(r.nonce)?!_t(r.created_at)||Number.isNaN(Date.parse(r.created_at))?null:_t(r.purpose)&&(void 0===r.run_id||_t(r.run_id))?{pid:Number(r.pid),process_start:r.process_start,hostname:r.hostname,nonce:r.nonce,created_at:r.created_at,purpose:r.purpose,...void 0===r.run_id?{}:{run_id:r.run_id},...!0===r.git_activity_lock?{git_activity_lock:!0}:{}}:null:null}async function xt(t,e={}){if(0===t.length)throw Error("lock purpose must not be empty");let r=await bt(process.pid);return{pid:process.pid,process_start:r??"unavailable",hostname:mt(),nonce:ft(),created_at:(new Date).toISOString(),purpose:t,...void 0===e.runId?{}:{run_id:e.runId},...await ct()?{git_activity_lock:!0}:{}}}var Ot=new Map;function It(t,e,r){Ot.set(t,{previous:e,takeoverNonce:r})}function Nt(t,e){Ot.get(t)?.takeoverNonce===e&&Ot.delete(t)}function $t(t){if(t instanceof Error&&"code"in t)return"string"==typeof t.code?t.code:void 0}async function Mt(t){try{let e=await $(t,"utf8"),r=await D(t,{bigint:!0});return{raw:e,record:kt(e),dev:r.dev,ino:r.ino,mtimeMs:Number(r.mtimeMs)}}catch(t){let e=$t(t);if("ENOENT"===e)return null;if("\\"===dt.sep&&"EPERM"===e)return{raw:"",record:null,dev:0,ino:0,mtimeMs:Date.now()};throw t}}function Tt(t,e){return t.dev===e.dev&&t.ino===e.ino&&t.mtimeMs===e.mtimeMs&&t.raw===e.raw}async function Rt(t,e){Nt(t,e.nonce);let r=await Mt(t);if(null===r||r.record?.nonce!==e.nonce)return!1;try{return await L(t),!0}catch(t){if("ENOENT"===$t(t))return!1;throw t}}import{randomUUID as At}from"node:crypto";import Pt from"node:path";import Dt from"node:path";var Ct=new Set;function Lt(t){Ct.add(t)}function jt(t){Ct.delete(t)}var Ft=/\.candidate-(?:(\d+)-)?[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;function Ut(t){let e=Ft.exec(t)?.[1];return void 0!==e&&"dead"===St(Number(e))}function qt(t){if(t instanceof Error&&"code"in t)return"string"==typeof t.code?t.code:void 0}function Bt(t,e){try{t.onFailure?.(e)}catch{}}function zt(t){if("win32"!==process.platform)return!1;let e=qt(t);return"EBUSY"===e||"EPERM"===e||"EACCES"===e}var Jt=new Map;function Wt(t){Jt.delete(t)}var Gt=new Set(["EACCES","EPERM","ENOTSUP"]);function Ht(t){if(t instanceof Error&&"code"in t)return"string"==typeof t.code?t.code:void 0}function Kt(t,e){if(void 0!==e)return e(t);if("win32"!==process.platform)return!1;let r=Ht(t);return"EBUSY"===r||"EPERM"===r||"EACCES"===r}var Zt={};function Yt(){return Zt}async function Vt(t){for(let e=0;e<3;e+=1)try{return await(Zt.unlink??L)(t),jt(t),!0}catch(r){if("ENOENT"===Ht(r))return jt(t),!0;if(!Kt(r,Zt.isSharingError))throw Lt(t),Wt(Pt.dirname(t)),r;if(e+1===3)return Lt(t),Wt(Pt.dirname(t)),!1}return!1}async function Xt(t,e){await I(Pt.dirname(t),{recursive:!0,mode:448});let{candidatePath:r,handle:n}=await async function(t){for(let e=0;;e+=1){let r=`${t}.candidate-${process.pid}-${At()}`;try{return{candidatePath:r,handle:await Q(r,"wx",384)}}catch(n){if(await Vt(r)||Wt(Pt.dirname(t)),"EINTR"!==Ht(n)||e>=128)throw n}}}(t);try{try{await f(n,`${JSON.stringify(e)}\n`,"utf8"),await n.sync()}finally{await n.close()}try{return await(Zt.link??x)(r,t),!0}catch(r){if(function(t){let e=Ht(t);return"EEXIST"===e||"ENOENT"===e}(r))return!1;if(Gt.has(Ht(r)??""))return await async function(t,e){let r;try{r=await Q(t,"wx",384)}catch(t){if("EEXIST"===Ht(t))return!1;throw t}try{return await(Zt.writeFallback??f)(r,`${JSON.stringify(e)}\n`,"utf8"),await r.sync(),!0}finally{await r.close()}}(t,e);throw r}}finally{await Vt(r)||Wt(Pt.dirname(t))}}function Qt(t,e,r=Math.random){let n=Math.min(250,e*2**Math.min(t,30));return Math.max(1,Math.round(n/2+r()*n/2))}function te(t,e){return e?.throwIfAborted(),new Promise((r,n)=>{let i=setTimeout(a,t),o=()=>a(e?.reason??new DOMException("The operation was aborted","AbortError"));function a(t){clearTimeout(i),e?.removeEventListener("abort",o),void 0===t?r():n(t)}e?.addEventListener("abort",o,{once:!0})})}import{randomUUID as ee}from"node:crypto";import{hostname as re}from"node:os";function ne(t){if(t instanceof Error&&"code"in t)return"string"==typeof t.code?t.code:void 0}async function ie(t){if(t.hostname!==re())return!1;let e=St(t.pid);if("dead"===e)return!0;if("unknown"===e)return!1;let r=await bt(t.pid);return null!==r&&"unavailable"!==t.process_start&&function(t,e){let r=Et(t);return null!==r&&r===Et(e)&&t!==e}(t.process_start,r)}async function oe(t,e,r){return null!==t.record?ie(t.record):e-t.mtimeMs>r}async function ae(t,e,r){let n=await Mt(t);if(null===n||!await oe(n,e,r))return!1;let i=`${t}.reaping-${ee()}`;try{await g(t,i)}catch(t){if("ENOENT"===ne(t))return!1;if(Kt(t))return!1;throw t}let o=await Mt(i);if(null!==o&&Tt(o,n))return await L(i),!0;try{await x(i,t)}catch(t){if("EEXIST"!==ne(t))throw t}return await L(i),!1}async function se(t,e,r,n,i){if(!await oe(e,n,i))return!1;let o=`${t}.recovery`,a={...r,nonce:ee(),created_at:(new Date).toISOString(),purpose:`${r.purpose}:recovery`};for(let t=0;null!==await Mt(o)||!await Xt(o,a);t+=1)if(t>0||!await ae(o,n,i))return!1;try{let s=await Mt(t);if(null===s)return!0;if(!Tt(s,e))return!1;if(!await oe(s,n,i))return!1;let l=await Mt(o);return null!==l&&l.record?.nonce===a.nonce&&(await L(t),null!==s.record&&It(t,s.record,r.nonce),!0)}finally{await Rt(o,a)}}class le extends Error{lockPath;owner;retriable=!0;constructor(t,e){super(`Lock is held: ${t}`),this.lockPath=t,this.owner=e,this.name="LockContentionError"}}async function ce(t,e,r={}){let n=r.waitTimeoutMs??0,i=r.retryDelayMs??25,o=r.incompleteLockGraceMs??5e3,a=r.now??Date.now;if(n<0||i<=0||o<0)throw Error("lock wait options must be positive");let s=a()+n;try{return await async function(t,e,r,n){let{retryDelayMs:i,incompleteLockGraceMs:o,now:a,deadline:s}=n,l=!1,c=!1;for(let n=0;;n+=1){r.signal?.throwIfAborted();let u=await Mt(t);if(null===u){if(await Xt(t,e))return{sawOtherOwner:l};if(r.signal?.throwIfAborted(),l=!0,c=ue(t,e,c),u=await Mt(t),null===u)continue}if(l=!0,c=ue(t,e,c),await se(t,u,e,a(),o))c=!0;else{if(r.signal?.throwIfAborted(),a()>=s)throw new le(t,u.record);await te(Math.min(Qt(n,i),Math.max(1,s-a())),r.signal)}}}(t,e,r,{retryDelayMs:i,incompleteLockGraceMs:o,now:a,deadline:s})}catch(r){throw Nt(t,e.nonce),r}}function ue(t,e,r){return r&&Nt(t,e.nonce),!1}import{hostname as de}from"node:os";import fe from"node:path";function me(t){if(t instanceof Error&&"code"in t)return"string"==typeof t.code?t.code:void 0}async function pe(t,e){let r=!0===(await O(t).catch(()=>{}))?.isFile()?await async function(t){let e;try{e=await $(t,"utf8")}catch(t){if("ENOENT"===me(t))return null;throw t}let r=kt(e);return null===r||r.hostname!==de()||"dead"!==St(r.pid)?null:await ie(r)?r:null}(t):null;if(null===r)return!1;let n=await xt("dead-owner-sweep");try{await ce(t,n,{waitTimeoutMs:0})}catch(t){if(t instanceof le)return!1;throw t}return await Rt(t,n),e.onReclaimed?.(t,r),!0}async function he(t,e,r={}){let n,i=ut.dirname(t);if(function(t,e){let r=Jt.get(t);return!(void 0!==r&&e-r<6e5||(Jt.set(t,e),0))}(i,(r.now??Date.now)())){let e=Yt();await async function(t,e=Date.now,r={}){let n;try{n=await M(t)}catch(e){if("ENOENT"===qt(e)){for(let e of Ct)Dt.dirname(e)===t&&jt(e);return 0}throw e}let i=0;for(let o of n){if(!Ft.test(o))continue;let n=Dt.join(t,o);try{let t=await D(n);if(!(Ct.has(n)||Ut(o)||e()-t.mtimeMs>36e5))continue;let a=!1;for(let t=0;t<3;t+=1)try{await(r.unlink??L)(n),jt(n),a=!0;break}catch(e){if("ENOENT"===qt(e)){jt(n),a=!0;break}if(!(r.isSharingError?.(e)??zt(e)))throw e;t+1===3&&Bt(r,n)}a&&(i+=1)}catch(t){"ENOENT"===qt(t)&&jt(n),Bt(r,n)}}return i}(i,Date.now,{...void 0===e.unlink?{}:{unlink:e.unlink},...void 0===e.isSharingError?{}:{isSharingError:e.isSharingError},onFailure:()=>Wt(i)}).catch(()=>{Wt(i)}),await async function(t,e={}){let r;try{r=await M(t)}catch(r){return"ENOENT"!==me(r)&&e.onFailure?.(t,r),0}let n=0;for(let i of r){if(!i.endsWith(".lock"))continue;let r=fe.join(t,i);try{await pe(r,e)&&(n+=1)}catch(t){e.onFailure?.(r,t)}}return n}(i,{onReclaimed:(e,r)=>{ut.resolve(e)===ut.resolve(t)&&(n=r)}})}let o=await ce(t,e,r);void 0===n||o.sawOtherOwner||void 0!==function(t,e){let r=Ot.get(t);return r?.takeoverNonce===e?r.previous:void 0}(t,e.nonce)||It(t,n,e.nonce)}async function we(t,e,r,n){await he(t,e,n);try{return await r()}finally{await Rt(t,e)}}Promise.resolve();(function(){let t=process.env.OMO_TEST_GIT_LOCK_SETTLE_MS;if(void 0===t)return;let e=Number.parseInt(t,10);Number.isInteger(e)})(),String.raw`#!/bin/sh
|
|
4
4
|
# Refuse a hand-written claim receipt header in memory notes. Installed by omo memory-core; regenerated on startup.
|
|
5
5
|
grep -q '^Omo-Writer: memory-tool$' "$1" && exit 0
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
// omo:
|
|
1
|
+
// omo:s7lG9buMusyAXjjiazS3XFGbAq-SbdCowdOGeGEAs2M:WOYq8h9oz62-1JU385iD0auqc0bov8GoaApv3ObzqKw
|
|
2
2
|
var e=/<(omo-[a-z0-9-]+-pointer|ultrawork-mode|omo-ultrawork-reminder)>[\s\S]*?<\/\1>/gi,t=/(?<!`)(`+)(?!`)[\s\S]*?(?<!`)\1(?!`)/g,n=/^\s*(?:\[(?:[A-Z][A-Z0-9_-]{2,}|[\w.-]+ -> [\w.-]+)\]|[A-Z][\w.-]{0,30} \([^()\s]{1,40}\)(?:, | to )[\w.-]{1,40}:)/,s=/^ {0,3}>[^\n]*/gm,i=/"[^"\n]*"|\u201C[^\u201D\n]*\u201D/g,r=/(?<![a-z0-9+.-])[a-z][a-z0-9+.-]*:\/\/[^\s<>"'`]+/gi,l=(String.raw`(?<![A-Za-z0-9][-_.]|[/\\])`,String.raw`(?![-_.]?[A-Za-z0-9]*[/\\])`,String.raw`(?![-_.]?[A-Za-z0-9])`);String.raw`(?${l})[A-Za-z0-9]|\.[A-Za-z0-9])`;function o(e){return e.replace(/[^\r\n]/g,"\0")}var a=[{id:"test.zero-fail",kind:"test",pattern:/(?<![0-9])0\s*(?:failed|failures?|fail)\b/i},{id:"test.ko-zero-fail",kind:"test",pattern:/실패\s*:?\s*0(?![0-9])/},{id:"test.n-of-n",kind:"test",pattern:/(?<![\w.])(\d+)\s*\/\s*\1\s*(?:pass(?:ed|ing|es)?|통과)/i},{id:"test.count-passed",kind:"test",pattern:/(?<![\w.])[1-9]\d*\s+pass(?:ed|ing)?\b/i},{id:"test.tests-pass",kind:"test",pattern:/\btests?\b[^.\n\0]{0,30}?\b(?:pass(?:es|ed|ing)?|(?:are|is|stays?) green)\b/i},{id:"test.suite-pass",kind:"test",pattern:/\b(?:suite|ci|checks)\b[^.\n\0]{0,20}?\b(?:pass(?:es|ed)?|green|succeed(?:s|ed)?)\b/i},{id:"test.should-pass",kind:"test",pattern:/\b(?:should|will|would)\s+(?:now\s+|still\s+|all\s+)?pass\b/i},{id:"test.ko-tests",kind:"test",pattern:/테스트[^.\n\0]{0,15}통과/},{id:"test.ko-all",kind:"test",pattern:/(?:모두|전부|다)\s*통과/},{id:"test.ko-passed",kind:"test",pattern:/통과(?:했|합니다|해요|함|됐|된다|될|할)/},{id:"test.ko-count",kind:"test",pattern:/(?<![0-9])[1-9]\d*\s*(?:개|건)?\s*통과/},{id:"typecheck.clean",kind:"typecheck",pattern:/\b(?:typecheck(?:s|ing)?|type ?check(?:s|ing)?|tsc|tsgo|types)\b[^.\n\0]{0,20}?\b(?:pass(?:es|ed)?|clean|green|succeed(?:s|ed)?|ok)\b/i},{id:"typecheck.no-errors",kind:"typecheck",pattern:/\bno (?:type|ts|typescript) errors?\b/i},{id:"typecheck.ko",kind:"typecheck",pattern:/타입\s*(?:체크|검사)[^.\n\0]{0,12}(?:통과|성공|깨끗)|타입\s*(?:에러|오류)\s*(?:없|0)/},{id:"lint.clean",kind:"lint",pattern:/\blint(?:er|ing)?\b[^.\n\0]{0,20}?\b(?:pass(?:es|ed)?|clean|green)\b|린트[^.\n\0]{0,12}(?:통과|성공|깨끗)/i},{id:"build.ok",kind:"build",pattern:/\bbuilds?\b[^.\n\0]{0,15}?\b(?:pass(?:es|ed)?|succeed(?:s|ed)?|green|works|ok)\b/i},{id:"build.ko",kind:"build",pattern:/빌드[^.\n\0]{0,12}(?:성공|통과|됩니다|됐|돼요)/},{id:"run.works",kind:"run",pattern:/\b(?:works|is working|runs) (?:as expected|correctly|end to end|fine)\b|(?:정상(?:적으로)?|잘)\s*(?:동작|작동)/i},{id:"exists.already",kind:"exists",pattern:/\balready\s+(?:exists?|implemented|in place|supported|wired up|present)\b/i},{id:"exists.ko",kind:"exists",pattern:/이미\s*(?:있|존재|구현(?:되어|돼|됐|되었|했|해)|되어|돼|지원(?:되|돼|해|합)|반영(?:되|돼|됐|했)|들어)/},{id:"done.leading",kind:"done",pattern:/^(?:done|completed|finished|fixed|implemented|verified|confirmed|all set)\b/i},{id:"done.ko-leading",kind:"done",pattern:/^(?:완료|끝|구현 완료|수정 완료|확인 완료)/},{id:"done.ko",kind:"done",pattern:/완료(?:했|됐|되었|입니다|됨)|끝났(?:어요|습니다|다)/}],d=/(?<![0-9])[1-9]\d*\s*(?:failed|failures?|fail)\b|실패\s*:?\s*[1-9]|(?<![0-9])[1-9]\d*\s*(?:건|개)?\s*실패/i,u=/\b(?:not|never|cannot|no longer)\b|n't\b/i,c=/\b(?:fail(?:s|ed|ing|ures?)?|broken|red)\b/i,f=/않|못|아니|(?:^|\s)안\s|미(?:통과|완료|구현|검증|반영)|불통과|실패(?:하|했|해|함|입니다|예요|중)|(?:에러|오류)가\s*(?:나|발생)/,p=/\b(?:not (?:yet )?(?:been )?(?:run|ran|verified|tested|checked|executed)|(?:haven't|have not|hasn't|has not|didn't|did not|couldn't|could not)\s+(?:yet\s+)?(?:run|verify|test|check|execute|re-?run)|untested|unverified|without (?:running|testing|verifying))\b/i,h=/아직\s*(?:안|못)|안\s*(?:돌렸|돌려|돌림|해\s*봤|실행)|못\s*(?:돌렸|돌려|돌림|해\s*봤|실행)|돌리지\s*(?:않|못)|실행(?:하지|은)?\s*(?:않|못|안)|미검증|미확인|(?:검증|확인)(?:하지|은|도)?\s*(?:전|안|못)|(?:검증|확인)되지\s*않/,k=/\b(?:should|would|ought to|expected to|likely|probably|presumably|i think|i believe|i assume|might)\b|거예요|겁니다|거야|것\s*같|거\s*같|듯|아마|예상|(?:할|될|일)\s*(?:거|것)/i,b=/^(?:Ask|Now|Next|wanted):/i,m=/\b(?:if|once|when|whenever|whether|until|unless|as soon as|as each)\b|(?:하|되|으|이|라)면(?=[\s,.]|$)|(?:할|될)\s*때/i,g=/(?<![\w.])\d+\s*(?:\/\s*\d+|pass(?:ed|ing)?|tests?|fail(?:ed|ures?)?)\b|(?<![\w.])\d+\s+(?:of\s+\d+|(?:[A-Za-z-]+\s+){1,2}tests?)\b|(?<![\w.])\d+\s*(?:\uAC1C|\uAC74)|\bexit(?:s|ed)?\s*(?:code\s*)?0\b|\b[A-Z_]*EXIT=0\b/i,w=/\u2018[^\u2019\n]*\u2019|[\u300C\u300E][^\u300D\u300F\n]*[\u300D\u300F]/g,v=/^[\s\0]*(?:[-*\u2022]|\d+[.)]|[\u2705\u2714\u2611]\uFE0F?)?\s*/;function y(e){return e.replace(/[^\r\n]/g,"\0")}function x(l){return function(l){if(n.test(l))return o(l);let a=l.replace(e,o),d=/^ {0,3}(`{3,}|~{3,})[^\n]*(?:\n|$)/gm;for(;;){let e=d.exec(a);if(null===e)break;let t=e[1],n=new RegExp(`^ {0,3}${t[0]}{${t.length},}[ \\t]*\\r?$`,"gm");n.lastIndex=d.lastIndex;let s=null===n.exec(a)?a.length:n.lastIndex;a=a.slice(0,e.index)+o(a.slice(e.index,s))+a.slice(s),d.lastIndex=s}return a.replace(t,o).replace(s,o).replace(i,o).replace(r,o)}(l).replace(w,y)}var S=new Set(["test.zero-fail","test.ko-zero-fail","typecheck.no-errors","typecheck.ko"]);function A(e){let t=x(e),n=function(e){let t=[],n=0;for(let s=0;s<=e.length;s+=1){let i=e[s],r=s===e.length,l=void 0!==i&&/[.!?\u3002]/.test(i)&&(s+1===e.length||/\s/.test(e[s+1]??""));if(!r&&"\n"!==i&&!l)continue;let o=l?s+1:s;""!==e.slice(n,o).replace(/[\s\0]/g,"")&&t.push({start:n,end:o}),n=o}return t}(t),s=[];return n.forEach((i,r)=>{let l=t.slice(i.start,i.end),o=function(e){let t=v.exec(e)?.[0].length??0,n=e.slice(t).trimEnd();if(b.test(n))return;let s=d.test(n);for(let e of a){if(s&&"test"===e.kind)continue;let i=e.pattern.exec(n);if(null!==i)return{rule:e,index:t+i.index,length:i[0].length}}}(l);if(void 0===o||m.test(l))return;let w=function(e){return p.test(e)||h.test(e)}(n.slice(Math.max(0,r-1),r+2).map(e=>t.slice(e.start,e.end)).join(" "));!w&&function(e,t,n,s){let i=S.has(t)?(l=n,o=s,(r=e).slice(0,l)+" ".repeat(o-l)+r.slice(o)):e;var r,l,o;return!(!u.test(i)&&!f.test(i))||c.test(i.slice(s))}(l,o.rule.id,o.index,o.index+o.length)||s.push({kind:o.rule.kind,strength:k.test(l)?"hedged":"asserted",admitted:w,reported:g.test(l),sentence:e.slice(i.start,i.end).trim(),rule:o.rule.id})}),s}function _(e){return"object"==typeof e&&null!==e}function $(e,t){let n=function(e){if(!_(e))return[];let t=C(e.files);if(t.length>0)return t;let n=e.result,s=_(n)?C(n.files):[];if(s.length>0)return s;let i=e.metadata;return _(i)?C(i.files):[]}(e).filter(e=>"delete"!==e.type?.toLowerCase()).map(e=>({filePath:e.movePath??e.filePath,before:e.before,after:e.after}));if(n.length>0)return n;let s=void 0===t?void 0:z(t,["patchText","input","patch","command"]);return void 0===s?[]:function(e){let t,n=[],s=()=>{if(void 0!==t){if("add"===t.operation){let e=I(t.newLines);e.length>0&&n.push({filePath:t.filePath,before:"",after:e})}if("update"===t.operation){let e=I(t.newLines);e.length>0&&n.push({filePath:t.movePath??t.filePath,before:I(t.oldLines),after:e})}t=void 0}};for(let n of e.split(/\r?\n/))"*** Begin Patch"!==n&&"*** End Patch"!==n&&(n.startsWith("*** Add File: ")?(s(),t=P("add",n.slice(14).trim())):n.startsWith("*** Update File: ")?(s(),t=P("update",n.slice(17).trim())):n.startsWith("*** Delete File: ")?(s(),t=P("delete",n.slice(17).trim())):n.startsWith("*** Move to: ")?"update"===t?.operation&&(t.movePath=n.slice(13).trim()):void 0===t||n.startsWith("@@")||("add"!==t.operation?"update"===t.operation&&(n.startsWith("-")&&t.oldLines.push(n.slice(1)),n.startsWith("+")&&t.newLines.push(n.slice(1))):n.startsWith("+")&&t.newLines.push(n.slice(1))));return s(),n}(s)}function C(e){if(!Array.isArray(e))return[];let t=[];for(let n of e){if(!_(n))continue;let e=z(n,["filePath","file_path","path"]),s=z(n,["movePath","move_path"]),i=z(n,["before","old","oldString","old_string"]),r=z(n,["after","new","newString","new_string"]),l=z(n,["type","operation"]);void 0!==e&&void 0!==i&&void 0!==r&&t.push({filePath:e,before:i,after:r,...void 0===s?{}:{movePath:s},...void 0===l?{}:{type:l}})}return t}function P(e,t){return{operation:e,filePath:t,oldLines:[],newLines:[]}}function z(e,t){for(let n of t){let t=e[n];if("string"==typeof t)return t}}function I(e){return 0===e.length?"":`${e.join("\n")}\n`}var Z=new Set(["write","edit","apply_patch"]);function E(e){let t=new Set,n=e=>{"string"==typeof e&&e&&t.add(e)},s=e=>{Array.isArray(e)&&e.forEach(n)};n(e.input.path),n(e.input.filePath),s(e.input.paths),s(e.input.filePaths);for(let t of $(e.details,e.input))n(t.filePath);if("string"==typeof e.input.input)for(let t of e.input.input.split(/\r?\n/))for(let e of["*** Add File: ","*** Update File: ","*** Move to: "])t.startsWith(e)&&n(t.slice(e.length).trim());for(let t of["files","changes"]){let s=e.input[t];if(Array.isArray(s))for(let e of s)if("object"==typeof e&&e){let t=e;n(t.path),n(t.filePath),n(t.movePath)}}return[...t]}var F=/(?<!<)<<(?!<)-?\s*(['"]?)([A-Za-z_][A-Za-z0-9_]*)\1/,j=/(?<!<<-?\s*)(?:'[^']*'|"(?:\\.|[^"\\])*")/g,W=new Set(["\n",";","|","&","(",")","`"]);function T(e){let t=null;for(let n=0;n<e.length;n+=1){let s=e[n];if(null!==t)s===t&&(t=null);else if("'"===s||'"'===s)t=s;else if("#"===s&&(0===n||/\s/.test(e[n-1])))return e.slice(0,n)}return e}function L(e){let t=function(e){let t=[],n=null;for(let s of e.split("\n"))null===n?(t.push(T(s)),n=F.exec(T(s).replace(j,""))?.[2]??null):s.trim()===n&&(n=null);return t.join("\n")}(e),n=[],s=[],i="",r=!1,l=null,o=()=>{r&&s.push(i),i="",r=!1},a=()=>{o(),s.length>0&&n.push(s),s=[]};for(let e=0;e<t.length;e+=1){let n=t[e];null===l?"'"!==n&&'"'!==n?"\\"===n&&e+1<t.length?(i+=t[e+1],r=!0,e+=1):"$"===n&&"("===t[e+1]||(W.has(n)?a():" "!==n&&"\t"!==n?(i+=n,r=!0):o()):(l=n,r=!0):n===l?l=null:i+=n}return a(),n}var R=/^[A-Za-z_][A-Za-z0-9_]*=/,M=/(?:^|\/)(?:heavy-slot|early-static-slot|launch-detached)\.sh$/,D=new Set(["bash","sh","zsh"]),N=new Set(["do","then","else","elif","if","while","until","!","{","}"]),O=new Set(["env","time","nohup","command","exec","sudo"]),X={env:new Set(["-u","-C","-P"]),sudo:new Set(["-u","-g","-h","-p","-c","-C","-D","-r","-R","-t","-U","-T"]),time:new Set(["-f","-o"])},q=new Set(["uv","poetry","pdm","pipx","hatch","rye"]),B=/^python(?:3(?:\.\d+)?)?$/,U=new Set(["make","just","task"]),J=/(?:^|\/)scripts\/qa\//,Q=new Set(["bun","npm","pnpm","yarn"]),G=new Set(["bunx","npx","pnpx"]),H=new Set(["rg","grep","egrep","cat","head","tail","ls","find","fd","sg","ast-grep"]),K=new Set("BbcDEeFIiJLlmOoPpQRSWw".split("")),V=/check-runs|check-suites|\/status(?:es)?\b/;function Y(e,t,n){let s=t;for(;s<e.length&&e[s].startsWith("-");)s+=n?.has(e[s])?2:1;return e.slice(s)}var ee=e=>e.flatMap(e=>{let t=function(e){return/^test(?::|$)/.test(e)?"test":/^(?:typecheck|type-check)(?::|$)/.test(e)?"typecheck":/^lint(?::|$)/.test(e)?"lint":/^build(?::|$)/.test(e)?"build":void 0}(e);return t?[t]:[]});function te(e,t){let[n="",s=""]=t;if(("bun"===e||"node"===e||"tsx"===e)&&t.some(e=>J.test(e)))return["run"];if(U.has(e))return ee(Y(t,0)).slice(0,1);if("vitest"===e||"jest"===e||"pytest"===e)return["test"];if("playwright"===e&&"test"===n)return["test"];if("tsc"===e||"tsgo"===e||"mypy"===e||"pyright"===e)return["typecheck"];if("eslint"===e||"oxlint"===e||"ruff"===e)return["lint"];if("biome"===e&&["check","lint","ci"].includes(n))return["lint"];if("cargo"===e){if("test"===n)return["test"];if("check"===n)return["typecheck"];if("clippy"===n)return["lint"];if("build"===n)return["build"]}if("go"===e&&("test"===n||"build"===n))return["test"===n?"test":"build"];if("deno"===e&&"test"===n)return["test"];if(("vite"===e||"next"===e)&&"build"===n)return["build"];if("gh"===e)return function(e){let[t="",n=""]=e;return"pr"===t&&"checks"===n||"pr"===t&&"view"===n&&e.some(e=>e.includes("statusCheckRollup"))?["test"]:"run"!==t||"view"!==n&&"watch"!==n?"api"===t&&e.some(e=>V.test(e))?["test"]:[]:["test"]}(t);if("vp"===e){if("test"===n)return["test"];if("check"===n)return["typecheck","lint"];if("lint"===n)return["lint"];if("run"===n)return ee(t.slice(1))}if(Q.has(e)){if("bun"===e&&"test"===n)return["test"];if("bun"===e&&"build"===n)return["build"];if("run"===n)return ee([s]);if("bun"!==e&&"test"===n)return["test"]}return H.has(e)?["lookup"]:"git"!==e||"grep"!==n&&"show"!==n?[]:["lookup"]}function ne(e,t,n){if("env"===e&&t.includes("-S")){let e=t.indexOf("-S");return n>=3?[]:ie(t.slice(e+1).join(" "),n+1)}if(O.has(e))return se(Y(t,1,X[e]),n);if(q.has(e)&&"run"===t[1])return se(Y(t,2),n);if(B.test(e)){let e=t.indexOf("-m");return-1!==e?se(t.slice(e+1),n):t.some(e=>J.test(e))?["run"]:[]}if("timeout"===e)return se(function(e){let t=1;for(;t<e.length&&e[t].startsWith("-");){let n=e[t];t+="-k"===n||"-s"===n||"--kill-after"===n||"--signal"===n?2:1}return e.slice(t+1)}(t),n);if("nice"===e)return se("-n"===t[1]?t.slice(3):t.slice(1),n);if(M.test(t[0])){let e=t.indexOf("--");return-1===e?[]:se(t.slice(e+1),n)}if("ssh"===e){let e=function(e){let t=1;for(;t<e.length&&e[t].startsWith("-");){let n=e[t];t+=2===n.length&&K.has(n.slice(1))?2:1}let n=e.slice("--"===e[t+1]?t+2:t+1);return 0===n.length?void 0:n.join(" ")}(t);return void 0===e||n>=3?[]:ie(e,n+1)}if(D.has(e)){let e=t.findIndex((e,t)=>t>0&&/^-[a-z]*c$/.test(e));if(-1===e)return Y(t,1).slice(0,1).some(e=>J.test(e))?["run"]:[];let s=t[e+1];return void 0===s||n>=3?[]:ie(s,n+1)}return G.has(e)?se(t.slice(1).filter(e=>!e.startsWith("-")),n):"bun"===e&&"x"===t[1]||"pnpm"===e&&"exec"===t[1]?se(t.slice(2),n):void 0}function se(e,t){let n=e;for(;n.length>0&&(R.test(n[0])||N.has(n[0]));)n=n.slice(1);if(0===n.length)return[];if(J.test(n[0]))return["run"];let s=(i=n[0]).slice(i.lastIndexOf("/")+1);var i;return ne(s,n,t)??te(s,n.slice(1))}function ie(e,t=0){let n=[];for(let s of L(e))for(let e of se(s,t))n.includes(e)||n.push(e);return n}var re=/"""[\s\S]*?"""|'''[\s\S]*?'''|`(?:\\[\s\S]|[^`\\])*`|'(?:\\.|[^'\\\n])*'|"(?:\\.|[^"\\\n])*"/g,le=/\[\s*(?:"""|'''|[`'"])[^\[\]]*\]/g,oe=/^(?:"""|'''|[`'"])|(?:"""|'''|[`'"])$/g,ae=/^(?:f|r|rf|fr|b)?$/i,de=/\$\{[^}]*\}|\{[A-Za-z_][\w.]*\}/g,ue=String.raw`(?:sh|sh\d+|shr|shm|sh_\w+|run|run_\w+|\w+_run|exec|execa|execSync|exec_\w+|\w+_exec|spawn|spawnSync|system|popen|Popen|check_output|check_call|call|shell|bash|zsh|ssh|rsh|rd|q|s|\$)`,ce=new RegExp([String.raw`(?:^|[^\w$])${ue}\s*\(\s*(?:\[\s*)?[A-Za-z]{0,2}$`,String.raw`(?:^|[^\w])(?:\$|sh|run|exec)\s*$`,String.raw`\b(?:commands?|cmds?|scripts?|shell|jobs?|steps?|s)\s*[:=]\s*(?:\[\s*)?[A-Za-z]{0,2}$`,String.raw`\bfor\s*\(?\s*(?:const|let|var)?\s*\w+\s+(?:in|of)\s*$`].join("|")),fe=/(?:\[|\],)\s*$/,pe=(e,t)=>e.slice(Math.max(0,t-80),t),he=(e,t)=>ce.test(pe(e,t)),ke=(e,t)=>he(e,t)||fe.test(pe(e,t)),be=e=>e.replace(oe,"").replace(de,"X"),me=e=>`'${e.replaceAll("'","'\\''")}'`,ge=e=>ie(e).filter(e=>"lookup"!==e);function we(e){let t=function(e){let t=[];for(let n of e.slice(1,-1).split(",")){let e=n.trim();if(""===e)continue;let s=e.match(re)?.[0],i=void 0!==s&&e.endsWith(s)&&ae.test(e.slice(0,-s.length));t.push(i?be(s):"X")}return t}(e),[n="",s=""]=t;if(/\s/.test(n))return t.flatMap(ge);let i=ge(t.map(me).join(" "));return 2===t.length&&/^[\w:.-]+$/.test(n)&&/\s/.test(s)?[...i,...ge(s)]:i}var ve=[/(?<![0-9])[1-9]\d*\s+fail(?:ed|ures?)?\b(?![=:])/i,/\bfail(?:ed|ures?)?=(?:[1-9]|\[[^\]\s])/i,/^\(fail\)/m,/\bFAIL\b/,/\berror TS\d+/,/AssertionError/,/^\s*error(?:\[[^\]]*\])?:/im,/\b[A-Z_]*EXIT[=:]\s*[1-9]\d*\b/,/\bexit(?:ed)?(?: with)?(?: code| status)?\s*[:=]?\s*[1-9]\d*\b/i,/\bTests?:?\s+[1-9]\d* failed/,/\bTest Files\s+[1-9]\d* failed/,/"exitCode":\s*[1-9]/,/\bFound [1-9]\d* errors?\b/,/Files with errors:\s*[1-9]/,/\tfail\t/],ye=[/\t(?:pending|queued|in_progress)\t/],xe=[/(?<![0-9])0\s+fail(?:ed|ures?)?\b/i,/\bfail(?:ed|ures?)?=(?:0\b|\[\])/i,/(?<![0-9])[1-9]\d*\s+pass(?:ed)?\b/i,/\bTests?:?\s+\d+ passed/,/\bTest Files\s+\d+ passed/,/"exitCode":\s*0\b/,/\bAll tests passed\b/i,/\b[A-Z_]*EXIT[=:]\s*0\b/,/\bexit(?:ed)?(?: with)?(?: code| status)?\s*[:=]?\s*0\b/i,/\bFound 0 errors\b/,/\bNo diagnostics found\b/,/Files with errors:\s*0\b/,/\bNo fixes applied\b/,/\tpass\t/],Se=/\bTool \S+ not found\b|\bpermission denied by\b|\b(?:was|were) denied\b|\bblocked by (?:policy|the user|a hook)\b|\brequires approval\b|\b(?:aborted|cancelled) by the user\b/i;function Ae(e,t){return e?Se.test(t)?"unknown":"failed":_e(t)}function _e(e){return ve.some(t=>t.test(e))?"failed":ye.some(t=>t.test(e))?"unknown":xe.some(t=>t.test(e))?"passed":"unknown"}var $e=/(?:^|\/)(?:\.omo|omo-runs|tmp|evidence|notepads?)\/|^\/(?:private\/)?tmp\/|\.(?:md|txt|log|jsonl|csv)$/i;function Ce(e){return!$e.test(e)}var Pe=new Set(["read","grep","glob","find","ls","lsp_goto_definition","lsp_find_references","lsp_symbols","ast_grep"]),ze=e=>{let t=ie(e);return{kinds:t.filter(e=>"lookup"!==e),lookup:t.includes("lookup")}},Ie=e=>"object"==typeof e&&null!==e&&!Array.isArray(e),Ze=e=>Array.isArray(e)?e:[],Ee=e=>"string"==typeof e?e:"";function Fe(e){return"string"==typeof e?e:Ze(e).map(e=>Ie(e)&&"text"===e.type?Ee(e.text):"").join("\n")}var je=/\bID:\s*([\w-]+)/,We=/Detached eval cell (\S+) \(\w+\) (completed|failed)/,Te=/Background terminal session (\S+) finished: (\w+) \(exit code (-?\d+)\)/,Le=/Monitor event\(([^)]*)\)/;class Re{checks=[];edits=[];#e=new Map;#t=new Map;#n=new Map;addCheck(e,t,n,s){for(let i of n.kinds)this.checks.push({at:e,entryId:t,kind:i,outcome:s,command:n.command.slice(0,200)})}addEdit(e,t,n){this.edits.push({at:e,entryId:t,paths:n,relevant:0===n.length||n.some(Ce)})}entry(e,t){if(!Ie(e))return;let n=Ee(e.id);if("custom_message"===e.type)return this.notification(Ee(e.customType),Fe(e.content),e.details,t,n);let s=e.message;if("message"===e.type&&Ie(s))if("assistant"!==s.role)"toolResult"===s.role&&this.toolResult(s,t,n);else for(let e of Ze(s.content))Ie(e)&&"toolCall"===e.type&&this.#e.set(Ee(e.id),{name:Ee(e.name),args:Ie(e.arguments)?e.arguments:{}})}toolResult(e,t,n){let s=Ee(e.toolName),i=Ee(e.toolCallId),r=this.#e.get(i)?.args??{},l=!0===e.isError,o=Fe(e.content);if("eval"===s)return this.evalResult(i,r,e.details,o,l,t,n);if(Z.has(s))l||this.addEdit(t,n,E({toolName:s,input:r,details:e.details}));else{if("lsp_diagnostics"===s)return this.lsp(o,l,t,n);if(Pe.has(s))return this.lookup(l,o,t,n);if("bash"===s||"powershell"===s){let e=Ee(r.command),{kinds:s,lookup:i}=ze(e);s.length>0&&this.addCheck(t,n,{kinds:s,command:e},Ae(l,o)),i&&0===s.length&&this.lookup(l,o,t,n)}}}lookup(e,t,n,s){e||""===t.trim()||this.checks.push({at:n,entryId:s,kind:"lookup",outcome:"passed"})}lsp(e,t,n,s){if(t)return;let i=_e(e);"unknown"!==i&&this.checks.push({at:n,entryId:s,kind:"lsp",outcome:i})}evalResult(e,t,n,s,i,r,l){let o=Ie(n)?n:{},a=Ze(o.toolCalls).filter(Ie);for(let e of a)this.hostCall(e,r+.5,l);if(a.some(e=>"bash"===e.name))return;let d=Ze(o.cells).filter(Ie),u={code:Ee(t.code),output:s,status:i?"error":"complete"};for(let t of d.length>0?d:[u])this.cell(e,Ee(t.code),Ee(t.output),t.status,r,l)}hostCall(e,t,n){let s=Ee(e.name),i=Ie(e.args)?e.args:{},r=Ee(e.resultPreview);if(Z.has(s))return void(!1!==e.ok&&this.addEdit(t,n,E({toolName:s,input:i})));if("lsp_diagnostics"===s)return this.lsp(r,!1===e.ok,t,n);if(Pe.has(s))return this.lookup(!1===e.ok,r,t,n);if("bash"!==s&&"monitor"!==s)return;let l=Ee(i.command),{kinds:o,lookup:a}=ze(l);if(0===o.length)return void(a&&"bash"===s&&this.lookup(!1===e.ok,r,t,n));let d="monitor"===s||!0===i.run_in_background;if(d&&!1===e.ok)return;let u=d?je.exec(r)?.[1]:void 0;if(void 0!==u)return this.#t.set(u,{kinds:o,command:l}),void("monitor"===s&&this.#t.set(`desc:${Ee(i.description)}`,{kinds:o,command:l}));this.addCheck(t,n,{kinds:o,command:l},Ae(!1===e.ok,r))}cell(e,t,n,s,i,r){let{kinds:l,offset:o}=function(e){let t=[],n=-1,s=(e,s)=>{if(0!==s.length){(-1===n||e<n)&&(n=e);for(let e of s)t.includes(e)||t.push(e)}},i=[];for(let t of e.matchAll(le))i.push([t.index,t.index+t[0].length]),ke(e,t.index)&&s(t.index,we(t[0]));for(let t of e.matchAll(re))i.some(([e,n])=>t.index>=e&&t.index<n)||he(e,t.index)&&s(t.index,ge(be(t[0])));return{kinds:t,offset:n}}(t);if(0===l.length)return;let a={kinds:l,command:t},d=i+(o+1)/(t.length+2);return"complete"===s&&"passed"===_e(n)?this.addCheck(d,r,a,"passed"):"complete"===s||"error"===s?this.addCheck(d,r,a,"unknown"):(this.#n.set(e,a),void this.addCheck(d,r,a,"unknown"))}notification(e,t,n,s,i){if("omo-senpi:wake"!==e){if("senpi-codemode:notification"===e){let e=We.exec(t),n=null===e?void 0:this.#n.get(e[1]??"");if(null===e||void 0===n)return;this.#n.delete(e[1]??"");let r="completed"===e[2]&&"passed"===_e(t.slice(e.index+e[0].length));return void this.addCheck(s,i,n,r?"passed":"unknown")}if("senpi-terminal:notification"===e){let e=Te.exec(t),n=null===e?void 0:this.#t.get(e[1]??"");if(null===e||void 0===n)return;if(this.#t.delete(e[1]??""),"killed"===e[2])return;return void this.addCheck(s,i,n,"0"===e[3]?_e(t):"failed")}if("senpi-monitor:notification"===e){if(/\bwatcher (?:killed|timed_out)\b|\bwatcher\b[^\n]*\bkilled\b/.test(t))return;let e=Ze(Ie(n)?n.monitors:void 0).map(e=>Ie(e)?Ee(e.id):""),r=Le.exec(t)?.[1],l=e.map(e=>this.#t.get(e)).find(e=>void 0!==e)??(void 0===r?void 0:this.#t.get(`desc:${r}`)),o=_e(t);void 0!==l&&"unknown"!==o&&this.addCheck(s,i,l,o)}}else for(let e of Ze(n).filter(Ie)){let n=Ze(e.contentRange),[r,l]=[n[0],n[1]],o="number"==typeof r&&"number"==typeof l?t.slice(r,l):t;this.notification(Ee(e.customType),o,e.details,s,i)}}}var Me=new Set(["test","typecheck","lint","build","run","lsp"]),De={test:new Set(["test"]),typecheck:new Set(["typecheck","lsp"]),lint:new Set(["lint"]),build:new Set(["build"]),run:new Set(["run","test","build"]),exists:new Set([...Me,"lookup"]),done:Me},Ne=new Set(["done","run"]),Oe=e=>({entryId:e.entryId,kind:e.kind,outcome:e.outcome}),Xe=e=>e.reduce((e,t)=>void 0===e||t.at>=e.at?t:e,void 0);function qe(e,t,n){if(e.admitted)return{claim:e,status:"admitted"};if(Ne.has(e.kind)&&!n.some(e=>e.relevant))return{claim:e,status:"unknown"};let s=De[e.kind],i=t.filter(e=>s.has(e.kind));if("exists"===e.kind)return function(e,t,n){let s=Xe(t.filter(e=>"passed"===e.outcome));return void 0!==s?{claim:e,status:"supported",receipt:Oe(s)}:e.reported||n>0?{claim:e,status:"unknown"}:{claim:e,status:"unsupported",reason:"no-check"}}(e,i,t.length);let r=Xe(i);if(void 0===r){let n=t.some(e=>"lookup"!==e.kind);return e.reported||n?{claim:e,status:"unknown"}:{claim:e,status:"unsupported",reason:"no-check"}}let l=Xe(i.filter(e=>"unknown"!==e.outcome)),o=e.reported&&"unknown"===r.outcome;if("failed"===l?.outcome&&!o)return{claim:e,status:"unsupported",reason:"last-check-failed",receipt:Oe(l)};if("unknown"===r.outcome)return{claim:e,status:"unknown",receipt:Oe(r)};let a=Xe(n.filter(e=>e.relevant&&e.at>r.at));return void 0!==a?{claim:e,status:"unsupported",reason:"stale-after-edit",receipt:Oe(r),invalidatedBy:{entryId:a.entryId,paths:a.paths}}:{claim:e,status:"supported",receipt:Oe(r)}}var Be=e=>"unsupported"===e.status;function Ue(e,t,n){let s=A(Fe(n));if(0===s.length)return;let i=function(e,t,n){let s=e.checks.filter(e=>e.at<n),i=e.edits.filter(e=>e.at<n);return t.map(e=>qe(e,s,i))}(function(e){let t=new Re;return e.forEach((e,n)=>t.entry(e,n)),{checks:t.checks,edits:t.edits}}(e),s,t);return{verdicts:i,flagged:i.filter(Be)}}export{Ue as evaluateAnswer};
|