module.exports=[19078,a=>{"use strict";var b=a.i(66680),c=a.i(80026);function d(a){return(0,c.getDb)().prepare("SELECT * FROM tasks WHERE id = ? OR display_id = ?").get(a,a)??null}a.s(["claimProposedTask",0,function(a){let b=(0,c.getDb)(),e=new Date().toISOString();return 0===b.prepare("UPDATE tasks SET status = 'working', updated_at = ? WHERE id = ? AND status = 'proposed'").run(e,a).changes?null:d(a)},"clearBlockerAndPromote",0,function(a){let b=(0,c.getDb)(),e=new Date().toISOString();return 0===b.prepare("UPDATE tasks SET status = 'proposed', blocked_by_task_id = NULL, updated_at = ? WHERE id = ? AND status = 'blocked'").run(e,a).changes?null:d(a)},"createTask",0,function(a){var e,f;let g,h,i=(0,c.getDb)(),j=new Date().toISOString(),k=a.status??"proposed",l=a.blocked_by_task_id??null;if(l){let a=d(l);a&&"done"!==(f=a.status)&&"failed"!==f&&"cancelled"!==f?k="blocked":l=null}let m={id:(0,b.randomUUID)(),display_id:(e=a.project_slug,g=(0,c.getDb)().prepare(`SELECT COALESCE(MAX(CAST(substr(display_id, ?) AS INTEGER)), 0) AS max_n
       FROM tasks
       WHERE project_slug = ? AND display_id LIKE ?`).get(e.length+2,e,`${e}-%`),h=(g?.max_n??0)+1,`${e}-${h}`),project_slug:a.project_slug,agent_id:a.agent_id,title:a.title??null,brief:a.brief,success_criteria:a.success_criteria??null,deadline_iso:a.deadline_iso??null,status:k,result_json:null,error_message:null,thread_id:null,assigner_agent_id:a.assigner_agent_id??null,blocked_by_task_id:l,created_at:j,updated_at:j};return i.prepare(`INSERT INTO tasks
       (id, display_id, project_slug, agent_id, title, brief, success_criteria, deadline_iso,
        status, result_json, error_message, thread_id, assigner_agent_id, blocked_by_task_id, created_at, updated_at)
     VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, NULL, NULL, NULL, ?, ?, ?, ?)`).run(m.id,m.display_id,m.project_slug,m.agent_id,m.title,m.brief,m.success_criteria,m.deadline_iso,m.status,m.assigner_agent_id,m.blocked_by_task_id,m.created_at,m.updated_at),m},"getTask",0,d,"inFlightCountsByAgent",0,function(a){let b=(0,c.getDb)().prepare(`SELECT agent_id, COUNT(*) as count
       FROM tasks
       WHERE project_slug = ? AND status IN ('proposed', 'approved', 'working', 'blocked')
       GROUP BY agent_id`).all(a),d=new Map;for(let a of b)d.set(a.agent_id,a.count);return d},"listTasks",0,function(a,b){let d=(0,c.getDb)();return b?d.prepare("SELECT * FROM tasks WHERE project_slug = ? AND status = ? ORDER BY created_at DESC").all(a,b):d.prepare("SELECT * FROM tasks WHERE project_slug = ? ORDER BY created_at DESC").all(a)},"listTasksBlockedBy",0,function(a){return(0,c.getDb)().prepare("SELECT * FROM tasks WHERE blocked_by_task_id = ? AND status = 'blocked' ORDER BY created_at ASC").all(a)},"listTasksByAgent",0,function(a,b){let d=(0,c.getDb)();return b?d.prepare("SELECT * FROM tasks WHERE agent_id = ? AND status = ? ORDER BY created_at DESC").all(a,b):d.prepare("SELECT * FROM tasks WHERE agent_id = ? ORDER BY created_at DESC").all(a)},"markTaskBlocked",0,function(a){let b=(0,c.getDb)(),e=new Date().toISOString();return b.prepare("UPDATE tasks SET status = 'blocked', updated_at = ? WHERE id = ? AND status IN ('working','proposed','approved')").run(e,a),d(a)},"setTaskThreadIfMissing",0,function(a,b){let e=(0,c.getDb)(),f=d(a);return f?f.thread_id?f:(e.prepare("UPDATE tasks SET thread_id = ?, updated_at = ? WHERE id = ? AND thread_id IS NULL").run(b,new Date().toISOString(),a),d(a)):null},"unblockTask",0,function(a){let b=(0,c.getDb)(),e=new Date().toISOString();return b.prepare("UPDATE tasks SET status = 'working', updated_at = ? WHERE id = ? AND status = 'blocked'").run(e,a),d(a)},"updateTask",0,function(a,b){let e=(0,c.getDb)(),f=new Date().toISOString(),g=d(a);if(!g)return null;let h=void 0!==b.result?JSON.stringify(b.result):g.result_json,i=void 0!==b.error_message?b.error_message:g.error_message,j=b.status??g.status;return e.prepare("UPDATE tasks SET status = ?, result_json = ?, error_message = ?, updated_at = ? WHERE id = ?").run(j,h,i,f,a),d(a)}])},26341,a=>{"use strict";var b=a.i(66680);a.s(["buildTaskKickoffMessage",0,function(a){let b=["(task assignment)","",`project_slug: ${a.project_slug}`,`agent_id:     ${a.agent_id}`,`task_id:      ${a.id}`,`Title:        ${a.title??"(untitled)"}`,"","Brief:",a.brief,""];return a.success_criteria&&b.push("Success criteria:",a.success_criteria,""),b.push("Acknowledge this task in 1-2 sentences (what you'll do + roughly how","long), then start working. Use your domain tools to actually do the","thing — don't just describe what you'd do. Close the task out when","you're done."),b.join("\n")},"generateTaskThreadId",0,function(){return(0,b.randomUUID)()}])},65454,a=>{"use strict";var b=a.i(22486),c=a.i(48836),d=a.i(12714);a.i(2157);var e=a.i(60526),f=a.i(50227),g=a.i(66680);function h(a,b){return(0,f.join)(process.env.NOTFAIR_CMO_DATA_DIR??(0,f.join)((0,e.homedir)(),".notfair-cmo"),"agents",a,"shadow",`${b}.jsonl`)}async function i(a,b){await (0,d.mkdir)((0,f.dirname)(h(a,b)),{recursive:!0})}async function j(a,b){await i(a,b);let c=h(a,b),e="";async function f(a){await (0,d.appendFile)(c,`${JSON.stringify(a)}
`,"utf8")}async function j(){let a=e.trim();e="",a&&await f({type:"message",id:`shadow-${(0,g.randomUUID)()}`,timestamp:new Date().toISOString(),message:{role:"assistant",content:[{type:"text",text:a}]}})}return{appendDelta(a){e+=a},replaceAssistant(a){e=a},flushAssistant:j,async toolStart(a,b,c){await j(),await f({type:"message",id:`shadow-${(0,g.randomUUID)()}`,timestamp:new Date().toISOString(),message:{role:"assistant",content:[{type:"toolCall",id:a,name:b,...c?{arguments:{__label:c}}:{}}]}})},async toolResult(a,b,c,d){await f({type:"message",id:`shadow-${(0,g.randomUUID)()}`,timestamp:new Date().toISOString(),message:{role:"toolResult",toolCallId:a,toolName:b,isError:!c,content:d?[{type:"text",text:d}]:void 0}})},async close(){await j()}}}async function k(a,b){switch(b.kind){case"delta":a.appendDelta(b.text);return;case"final":a.replaceAssistant(b.text),await a.flushAssistant();return;case"tool":"start"===b.phase?await a.toolStart(b.toolCallId,b.name,b.label):"result"===b.phase&&await a.toolResult(b.toolCallId,b.name,!0,b.label);return;case"lifecycle":case"error":return}}var l=a.i(19078),m=a.i(26341);async function n(a){let d=a;if(!d.thread_id){let b=(0,l.setTaskThreadIfMissing)(a.id,(0,m.generateTaskThreadId)());if(b&&(d=b),!d.thread_id)throw Error(`Failed to assign thread_id for task ${a.id}`)}let e=(0,b.buildPendingSessionKey)(d.agent_id,d.thread_id),f=(0,m.buildTaskKickoffMessage)(d),g=await j(d.agent_id,d.thread_id);console.log(`[run-task] kickoff start task=${d.id} agent=${d.agent_id} thread=${d.thread_id}`);let h=[2e3,4e3,8e3],i=null,n=!1;for(let a=0;a<=h.length;a+=1)try{for await(let a of(0,c.streamChatViaGateway)({sessionKey:e,sessionId:d.thread_id,message:f})){try{await k(g,a)}catch(a){console.error("[run-task] shadow write failed:",a)}if("error"===a.kind)throw Error(a.message)}n=!0;break}catch(c){if(i=c,!(c instanceof Error?c.message:String(c)).includes("no longer exists in configuration")||a>=h.length)break;let b=h[a];console.warn(`[run-task] gateway snapshot stale for ${d.agent_id}; retrying in ${b}ms (attempt ${a+1}/${h.length})`),await new Promise(a=>setTimeout(a,b))}if(n){await g.close(),console.log(`[run-task] kickoff stream done task=${d.id}`);return}await g.close().catch(()=>void 0);let o=i instanceof Error?i.message:String(i);console.error(`[run-task] kickoff failed task=${d.id} agent=${d.agent_id}: ${o}`),(0,l.updateTask)(d.id,{status:"failed",error_message:o})}a.s(["runTaskKickoffServerSide",0,n,"startTaskIfProposed",0,function(a){let b=(0,l.claimProposedTask)(a.id);return b?(n(b).catch(a=>{console.error("[start-task] kickoff failed:",a)}),b):a}],65454)}];

//# sourceMappingURL=src_server_0qz~uwd._.js.map