taskgraph-ai 0.52.3 → 0.52.4

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.
Files changed (30) hide show
  1. package/README.md +16 -5
  2. package/dist/ai-services-unified-BTTXDpEu.js +1 -0
  3. package/dist/{ai-services-unified-C_rt4w5B.js → ai-services-unified-CL8dbLYJ.js} +1 -1
  4. package/dist/assets/AGENTS.md +12 -31
  5. package/dist/assets/example_prd_rpg.txt +1 -4
  6. package/dist/assets/scripts_README.md +12 -14
  7. package/dist/{commands-REN-S3Y5.js → commands-B49KHl_a.js} +5 -5
  8. package/dist/{config-manager-ze5VncSH.js → config-manager-BTO7V7yI.js} +1 -1
  9. package/dist/{config-manager-DDFU1C2i.js → config-manager-cM9YWtuF.js} +1 -1
  10. package/dist/{dependency-manager-BPBdmQcF.js → dependency-manager-Mm62BpiN.js} +9 -9
  11. package/dist/{init-DE4mO3CM.js → init-C75AhVRQ.js} +3 -3
  12. package/dist/mcp-server.js +2 -2
  13. package/dist/research-ZJOfL6U9.js +1 -0
  14. package/dist/response-language-BiLoLKGt.js +1 -0
  15. package/dist/{response-language-8XZaALbD.js → response-language-CAUwZ_BR.js} +1 -1
  16. package/dist/src-kk3jUMbL.js +207 -0
  17. package/dist/tag-management-upzMTb_q.js +1 -0
  18. package/dist/{task-manager-9jVID7X1.js → task-manager-Bo-fv6V-.js} +1 -1
  19. package/dist/taskgraph.js +1 -1
  20. package/dist/update-subtask-by-id-usEluJxs.js +1 -0
  21. package/dist/update-task-by-id-BDHadCwU.js +1 -0
  22. package/dist/{utils-Drd3leuW.js → utils-D-_waI9k.js} +1 -1
  23. package/package.json +2 -10
  24. package/dist/ai-services-unified-BDcAS7eZ.js +0 -1
  25. package/dist/research-CC0KE46z.js +0 -1
  26. package/dist/response-language-Bo97h8xL.js +0 -1
  27. package/dist/src-DUT6aK7P.js +0 -203
  28. package/dist/tag-management-neogW4lA.js +0 -1
  29. package/dist/update-subtask-by-id-Dv7PlU5v.js +0 -1
  30. package/dist/update-task-by-id-Dzn7ZNbs.js +0 -1
@@ -0,0 +1,207 @@
1
+ import e from"chalk";import t,{join as n,resolve as r}from"node:path";import i,{appendFile as a,mkdir as o,readFile as s}from"node:fs/promises";import c,{join as l}from"path";import u from"fs-extra";import{simpleGit as d}from"simple-git";import{exec as f,execSync as p}from"child_process";import{promisify as m}from"util";import{execFile as h,spawn as g,spawnSync as _}from"node:child_process";import{createHash as v}from"node:crypto";import y,{constants as ee,existsSync as b,readFileSync as te,readdirSync as ne}from"node:fs";import re from"proper-lockfile";import{Writer as ie}from"steno";import ae from"node:os";import{format as oe}from"date-fns";import{z as x}from"zod";var se=class{constructor(e){this.configManager=e}getConfig(){return this.configManager.getConfig()}getStorageConfig(){return this.configManager.getStorageConfig()}getModelConfig(){return this.configManager.getModelConfig()}getResponseLanguage(){return this.configManager.getResponseLanguage()}getProjectRoot(){return this.configManager.getProjectRoot()}getActiveTag(){return this.configManager.getActiveTag()}async setActiveTag(e){return this.configManager.setActiveTag(e)}async updateConfig(e){return this.configManager.updateConfig(e)}async setResponseLanguage(e){return this.configManager.setResponseLanguage(e)}async saveConfig(){return this.configManager.saveConfig()}async reset(){return this.configManager.reset()}getConfigSources(){return this.configManager.getConfigSources()}};const S={MODELS:{MAIN:`claude-sonnet-5-5`,FALLBACK:`claude-sonnet-5-5`},TASKS:{DEFAULT_PRIORITY:`medium`,DEFAULT_COMPLEXITY:`moderate`,MAX_SUBTASKS:20,MAX_CONCURRENT:5,TASK_ID_PREFIX:`TASK-`},TAGS:{DEFAULT_TAG:`master`,MAX_TAGS_PER_TASK:10,NAMING_CONVENTION:`kebab-case`},WORKFLOW:{ENABLE_AUTOPILOT:!0,MAX_PHASE_ATTEMPTS:3,BRANCH_PATTERN:`task-{taskId}`,REQUIRE_CLEAN_WORKING_TREE:!0,AUTO_STAGE_CHANGES:!0,INCLUDE_CO_AUTHOR:!0,CO_AUTHOR_NAME:`TaskGraph`,CO_AUTHOR_EMAIL:`noreply@github.com`,MIN_TESTS:1,MAX_FAILURES_IN_GREEN:0,COMMIT_MESSAGE_TEMPLATE:`{type}({scope}): {description} (Task {taskId}.{subtaskIndex})`,ALLOWED_COMMIT_TYPES:[`feat`,`fix`,`refactor`,`test`,`docs`,`chore`],DEFAULT_COMMIT_TYPE:`feat`,OPERATION_TIMEOUT:6e4,ENABLE_ACTIVITY_LOGGING:!0,ACTIVITY_LOG_PATH:`.taskgraph/logs/workflow-activity.log`,ENABLE_STATE_BACKUP:!0,MAX_STATE_BACKUPS:5,ABORT_ON_MAX_ATTEMPTS:!1},STORAGE:{TYPE:`file`,ENCODING:`utf8`,MAX_BACKUPS:5},RETRY:{ATTEMPTS:3,DELAY:1e3,MAX_DELAY:3e4,BACKOFF_MULTIPLIER:2,TIMEOUT:3e4},LOGGING:{LEVEL:`info`,MAX_FILE_SIZE:10,MAX_FILES:5},SECURITY:{MAX_REQUESTS_PER_MINUTE:60,MAX_PROMPT_LENGTH:1e5,ALLOWED_EXTENSIONS:[`.txt`,`.md`,`.json`]},VERSION:`1.0.0`};let C=function(e){return e[e.SILENT=0]=`SILENT`,e[e.ERROR=1]=`ERROR`,e[e.WARN=2]=`WARN`,e[e.INFO=3]=`INFO`,e[e.DEBUG=4]=`DEBUG`,e}({});var ce=class t{config;static DEFAULT_CONFIG={level:C.SILENT,silent:!1,prefix:``,timestamp:!1,colors:!0,mcpMode:!1,logCallback:void 0};constructor(e={}){let n={};if((process.env.MCP_MODE===`true`||process.env.TASKGRAPH_MCP===`true`)&&(n.mcpMode=!0),(process.env.TASKGRAPH_SILENT===`true`||process.env.TM_SILENT===`true`)&&(n.silent=!0),process.env.TASKGRAPH_LOG_LEVEL||process.env.TM_LOG_LEVEL){let e=(process.env.TASKGRAPH_LOG_LEVEL||process.env.TM_LOG_LEVEL||``).toUpperCase();e in C&&(n.level=C[e])}(process.env.NO_COLOR===`true`||process.env.TASKGRAPH_NO_COLOR===`true`)&&(n.colors=!1),this.config={...t.DEFAULT_CONFIG,...e,...n},this.config.mcpMode&&!this.config.logCallback&&(this.config.silent=!0)}shouldLog(e){return this.config.logCallback?e<=this.config.level:this.config.silent||this.config.mcpMode?!1:e<=this.config.level}formatMessage(t,n,...r){let i=``;if(this.config.timestamp){let t=new Date().toISOString();i+=this.config.colors?e.dim(`[${t}] `):`[${t}] `}if(this.config.prefix&&(i+=this.config.colors?e.cyan(`[${this.config.prefix}] `):`[${this.config.prefix}] `),this.config.colors)switch(t){case C.ERROR:n=e.red(n);break;case C.WARN:n=e.yellow(n);break;case C.INFO:break;case C.DEBUG:n=e.dim(n);break}return i+=n,r.length>0&&(i+=` `+r.map(e=>typeof e==`object`?JSON.stringify(e,null,2):String(e)).join(` `)),i}isLogObject(e){return typeof e==`object`&&!!e&&`info`in e&&`warn`in e&&`error`in e&&`debug`in e}output(e,t,n,...r){let i=this.formatMessage(e,n,...r);if(this.config.logCallback){if(this.isLogObject(this.config.logCallback)){let e=t.toLowerCase();e in this.config.logCallback&&this.config.logCallback[e](i)}else this.config.logCallback(t.toLowerCase(),i);return}switch(e){case C.ERROR:console.error(i);break;case C.WARN:console.warn(i);break;default:console.log(i);break}}error(e,...t){this.shouldLog(C.ERROR)&&this.output(C.ERROR,`ERROR`,e,...t)}warn(e,...t){this.shouldLog(C.WARN)&&this.output(C.WARN,`WARN`,e,...t)}info(e,...t){this.shouldLog(C.INFO)&&this.output(C.INFO,`INFO`,e,...t)}debug(e,...t){this.shouldLog(C.DEBUG)&&this.output(C.DEBUG,`DEBUG`,e,...t)}log(e,...t){if(this.config.logCallback){let n=t.length>0?[e,...t].join(` `):e;this.isLogObject(this.config.logCallback)?this.config.logCallback.info(n):this.config.logCallback(`log`,n);return}this.config.silent||this.config.mcpMode||(t.length>0?console.log(e,...t):console.log(e))}setConfig(e){this.config={...this.config,...e},this.config.mcpMode&&!this.config.logCallback&&(this.config.silent=!0)}getConfig(){return{...this.config}}child(e,n){let r=this.config.prefix?`${this.config.prefix}:${e}`:e;return new t({...this.config,...n,prefix:r})}};let le=null;const w=new Map;function T(e){return new ce(e)}function E(e,t){return e?(w.has(e)||w.set(e,T({prefix:e,...t})),w.get(e)):(le||=T(t),le)}const D={FILE_NOT_FOUND:`FILE_NOT_FOUND`,FILE_READ_ERROR:`FILE_READ_ERROR`,FILE_WRITE_ERROR:`FILE_WRITE_ERROR`,PARSE_ERROR:`PARSE_ERROR`,JSON_PARSE_ERROR:`JSON_PARSE_ERROR`,YAML_PARSE_ERROR:`YAML_PARSE_ERROR`,VALIDATION_ERROR:`VALIDATION_ERROR`,SCHEMA_VALIDATION_ERROR:`SCHEMA_VALIDATION_ERROR`,TYPE_VALIDATION_ERROR:`TYPE_VALIDATION_ERROR`,API_ERROR:`API_ERROR`,NETWORK_ERROR:`NETWORK_ERROR`,AUTHENTICATION_ERROR:`AUTHENTICATION_ERROR`,AUTHORIZATION_ERROR:`AUTHORIZATION_ERROR`,TASK_NOT_FOUND:`TASK_NOT_FOUND`,TASK_DEPENDENCY_ERROR:`TASK_DEPENDENCY_ERROR`,TASK_STATUS_ERROR:`TASK_STATUS_ERROR`,STORAGE_ERROR:`STORAGE_ERROR`,DATABASE_ERROR:`DATABASE_ERROR`,CONFIG_ERROR:`CONFIG_ERROR`,MISSING_CONFIGURATION:`MISSING_CONFIGURATION`,INVALID_CONFIGURATION:`INVALID_CONFIGURATION`,PROVIDER_ERROR:`PROVIDER_ERROR`,PROVIDER_NOT_FOUND:`PROVIDER_NOT_FOUND`,PROVIDER_INITIALIZATION_ERROR:`PROVIDER_INITIALIZATION_ERROR`,INTERNAL_ERROR:`INTERNAL_ERROR`,INVALID_INPUT:`INVALID_INPUT`,NOT_IMPLEMENTED:`NOT_IMPLEMENTED`,UNKNOWN_ERROR:`UNKNOWN_ERROR`,NOT_FOUND:`NOT_FOUND`};var O=class e extends Error{code;context;cause;timestamp;constructor(t,n=D.UNKNOWN_ERROR,r={},i){super(t),this.name=`TaskGraphError`,this.code=n,this.cause=i,this.timestamp=new Date,this.context={timestamp:this.timestamp,...r},Object.setPrototypeOf(this,e.prototype),Error.captureStackTrace&&Error.captureStackTrace(this,e),i?.stack&&(this.stack=`${this.stack}\nCaused by: ${i.stack}`)}getUserMessage(){return this.context.userMessage||this.message}getSanitizedDetails(){let{details:e,resource:t,operation:n}=this.context;return{code:this.code,message:this.getUserMessage(),...t&&{resource:t},...n&&{operation:n},...e&&typeof e==`object`&&!this.containsSensitiveInfo(e)&&{details:e}}}containsSensitiveInfo(e){if(typeof e!=`object`||!e)return!1;let t=[`password`,`token`,`key`,`secret`,`auth`,`credential`],n=JSON.stringify(e).toLowerCase();return t.some(e=>n.includes(e))}toJSON(){let t={name:this.name,message:this.message,code:this.code,context:this.context,stack:this.stack};return this.cause&&(this.cause instanceof e?t.cause=this.cause.toJSON():t.cause={name:this.cause.name,message:this.cause.message,code:D.UNKNOWN_ERROR,context:{},stack:this.cause.stack}),t}toString(){let e=`${this.name}[${this.code}]: ${this.message}`;return this.context.operation&&(e+=` (operation: ${this.context.operation})`),this.context.resource&&(e+=` (resource: ${this.context.resource})`),this.cause&&(e+=`\nCaused by: ${this.cause.toString()}`),e}is(e){return this.code===e}hasCode(t){return this.is(t)?!0:this.cause instanceof e?this.cause.hasCode(t):!1}withContext(t){return new e(this.message,this.code,{...this.context,...t},this.cause)}wrap(t,n=D.INTERNAL_ERROR,r={}){return new e(t,n,r,this)}},ue=class{localConfigPath;globalConfigPath;logger=E(`ConfigLoader`);constructor(e){this.localConfigPath=t.join(e,`.taskgraph`,`config.json`),this.globalConfigPath=t.join(process.env.HOME||``,`.taskgraph`,`config.json`)}getDefaultConfig(){return{models:{main:S.MODELS.MAIN,fallback:S.MODELS.FALLBACK},workflow:{enableAutopilot:S.WORKFLOW.ENABLE_AUTOPILOT,maxPhaseAttempts:S.WORKFLOW.MAX_PHASE_ATTEMPTS,branchPattern:S.WORKFLOW.BRANCH_PATTERN,requireCleanWorkingTree:S.WORKFLOW.REQUIRE_CLEAN_WORKING_TREE,autoStageChanges:S.WORKFLOW.AUTO_STAGE_CHANGES,includeCoAuthor:S.WORKFLOW.INCLUDE_CO_AUTHOR,coAuthorName:S.WORKFLOW.CO_AUTHOR_NAME,coAuthorEmail:S.WORKFLOW.CO_AUTHOR_EMAIL,testThresholds:{minTests:S.WORKFLOW.MIN_TESTS,maxFailuresInGreen:S.WORKFLOW.MAX_FAILURES_IN_GREEN},commitMessageTemplate:S.WORKFLOW.COMMIT_MESSAGE_TEMPLATE,allowedCommitTypes:[...S.WORKFLOW.ALLOWED_COMMIT_TYPES],defaultCommitType:S.WORKFLOW.DEFAULT_COMMIT_TYPE,operationTimeout:S.WORKFLOW.OPERATION_TIMEOUT,enableActivityLogging:S.WORKFLOW.ENABLE_ACTIVITY_LOGGING,activityLogPath:S.WORKFLOW.ACTIVITY_LOG_PATH,enableStateBackup:S.WORKFLOW.ENABLE_STATE_BACKUP,maxStateBackups:S.WORKFLOW.MAX_STATE_BACKUPS,abortOnMaxAttempts:S.WORKFLOW.ABORT_ON_MAX_ATTEMPTS},storage:{type:S.STORAGE.TYPE,encoding:S.STORAGE.ENCODING,enableBackup:!1,maxBackups:S.STORAGE.MAX_BACKUPS,enableCompression:!1,atomicOperations:!0},version:S.VERSION}}async loadLocalConfig(){try{let e=await i.readFile(this.localConfigPath,`utf-8`);return JSON.parse(e)}catch(e){if(e.code===`ENOENT`)return this.logger.debug(`No local config.json found, using defaults`),null;throw new O(`Failed to load local configuration`,D.CONFIG_ERROR,{configPath:this.localConfigPath},e)}}async loadGlobalConfig(){return null}async hasLocalConfig(){try{return await i.access(this.localConfigPath),!0}catch{return!1}}async hasGlobalConfig(){try{return await i.access(this.globalConfigPath),!0}catch{return!1}}};const k={DEFAULTS:0,GLOBAL:1,LOCAL:2,ENVIRONMENT:3};var de=class{configSources=[];addSource(e){this.configSources.push(e)}clearSources(){this.configSources=[]}merge(){let e=[...this.configSources].sort((e,t)=>e.precedence-t.precedence),t={};for(let n of e)t=this.deepMerge(t,n.config);return t}deepMerge(e,t){if(!t)return e;if(!e)return t;let n={...e};for(let e in t)t[e]===null||t[e]===void 0||(typeof t[e]==`object`&&!Array.isArray(t[e])?n[e]=this.deepMerge(n[e]||{},t[e]):n[e]=t[e]);return n}getSources(){return[...this.configSources].sort((e,t)=>t.precedence-e.precedence)}hasSource(e){return this.configSources.some(t=>t.name===e)}removeSource(e){let t=this.configSources.length;return this.configSources=this.configSources.filter(t=>t.name!==e),this.configSources.length<t}},fe=class{localConfigPath;backupDir;logger=E(`ConfigPersistence`);constructor(e){this.localConfigPath=t.join(e,`.taskgraph`,`config.json`),this.backupDir=t.join(e,`.taskgraph`,`backups`)}async saveConfig(e,n={}){let{createBackup:r=!1,atomic:a=!0}=n;try{r&&await this.configExists()&&await this.createBackup();let n=t.dirname(this.localConfigPath);await i.mkdir(n,{recursive:!0});let o=JSON.stringify(e,null,2);if(a){let e=`${this.localConfigPath}.tmp`;await i.writeFile(e,o,`utf-8`),await i.rename(e,this.localConfigPath)}else await i.writeFile(this.localConfigPath,o,`utf-8`)}catch(e){throw new O(`Failed to save configuration`,D.CONFIG_ERROR,{configPath:this.localConfigPath},e)}}async createBackup(){try{await i.mkdir(this.backupDir,{recursive:!0});let e=new Date().toISOString().replace(/[:.]/g,`-`),n=t.join(this.backupDir,`config-${e}.json`),r=await i.readFile(this.localConfigPath,`utf-8`);return await i.writeFile(n,r,`utf-8`),await this.cleanOldBackups(),n}catch(e){throw this.logger.warn(`Failed to create backup:`,e),e}}async cleanOldBackups(e=5){try{let n=(await i.readdir(this.backupDir)).filter(e=>e.startsWith(`config-`)&&e.endsWith(`.json`)).sort().reverse().slice(e);for(let e of n)await i.unlink(t.join(this.backupDir,e))}catch(e){this.logger.warn(`Failed to clean old backups:`,e)}}async configExists(){try{return await i.access(this.localConfigPath),!0}catch{return!1}}async deleteConfig(){try{await i.unlink(this.localConfigPath)}catch(e){if(e.code!==`ENOENT`)throw new O(`Failed to delete configuration`,D.CONFIG_ERROR,{configPath:this.localConfigPath},e)}}async getBackups(){try{return(await i.readdir(this.backupDir)).filter(e=>e.startsWith(`config-`)&&e.endsWith(`.json`)).sort().reverse()}catch{return[]}}async restoreFromBackup(e){let n=t.join(this.backupDir,e);try{let e=await i.readFile(n,`utf-8`);await i.writeFile(this.localConfigPath,e,`utf-8`)}catch(e){throw new O(`Failed to restore from backup`,D.CONFIG_ERROR,{backupPath:n},e)}}},pe=class e{logger=E(`EnvironmentConfigProvider`);static DEFAULT_MAPPINGS=[{env:`TASKGRAPH_MODEL_MAIN`,path:[`models`,`main`]},{env:`TASKGRAPH_MODEL_RESEARCH`,path:[`models`,`research`]},{env:`TASKGRAPH_MODEL_FALLBACK`,path:[`models`,`fallback`]},{env:`TASKGRAPH_RESPONSE_LANGUAGE`,path:[`custom`,`responseLanguage`]}];static RUNTIME_STATE_MAPPINGS=[{env:`TASKGRAPH_TAG`,path:[`activeTag`],isRuntimeState:!0}];mappings;constructor(t){this.mappings=t||[...e.DEFAULT_MAPPINGS,...e.RUNTIME_STATE_MAPPINGS]}loadConfig(){let e={};for(let t of this.mappings){if(t.isRuntimeState)continue;let n=process.env[t.env];if(n){if(t.validate&&!t.validate(n)){this.logger.warn(`Invalid value for ${t.env}: ${n}`);continue}this.setNestedProperty(e,t.path,n)}}return e}getRuntimeState(){let e={};for(let t of this.mappings){if(!t.isRuntimeState)continue;let n=process.env[t.env];if(n){let r=t.path[t.path.length-1];e[r]=n}}return e}setNestedProperty(e,t,n){let r=t[t.length-1],i=t.slice(0,-1),a=e;for(let e of i)a[e]||(a[e]={}),a=a[e];a[r]=n}hasEnvVar(e){return e in process.env&&process.env[e]!==void 0}getAllTaskGraphEnvVars(){let e={};for(let[t,n]of Object.entries(process.env))t.startsWith(`TASKGRAPH_`)&&n!==void 0&&(e[t]=n);return e}addMapping(e){this.mappings.push(e)}getMappings(){return[...this.mappings]}},me=class{stateFilePath;currentState;logger=E(`RuntimeStateManager`);constructor(e){this.stateFilePath=t.join(e,`.taskgraph`,`state.json`),this.currentState={currentTag:S.TAGS.DEFAULT_TAG}}async loadState(){try{let e=await i.readFile(this.stateFilePath,`utf-8`),t=JSON.parse(e),n={currentTag:t.currentTag||t.activeTag||S.TAGS.DEFAULT_TAG,lastUpdated:t.lastUpdated,metadata:t.metadata};return process.env.TASKGRAPH_TAG&&(n.currentTag=process.env.TASKGRAPH_TAG),this.currentState=n,n}catch(e){return e.code===`ENOENT`?(this.logger.debug(`No state.json found, using default state`),process.env.TASKGRAPH_TAG&&(this.currentState.currentTag=process.env.TASKGRAPH_TAG),this.currentState):(this.logger.warn(`Failed to load state file:`,e.message),this.currentState)}}async saveState(){let e=t.dirname(this.stateFilePath);try{await i.mkdir(e,{recursive:!0});let t={...this.currentState,lastUpdated:new Date().toISOString()};await i.writeFile(this.stateFilePath,JSON.stringify(t,null,2),`utf-8`)}catch(e){throw new O(`Failed to save runtime state`,D.CONFIG_ERROR,{statePath:this.stateFilePath},e)}}getCurrentTag(){return this.currentState.currentTag}async setCurrentTag(e){this.currentState.currentTag=e,await this.saveState()}getState(){return{...this.currentState}}async updateMetadata(e){this.currentState.metadata={...this.currentState.metadata,...e},await this.saveState()}async clearState(){try{await i.unlink(this.stateFilePath)}catch(e){if(e.code!==`ENOENT`)throw e}this.currentState={currentTag:S.TAGS.DEFAULT_TAG}}},he=class e{projectRoot;config={};initialized=!1;loader;merger;stateManager;persistence;envProvider;static async create(t){let n=new e(t);return await n.initialize(),n}constructor(e){this.projectRoot=e,this.loader=new ue(e),this.merger=new de,this.stateManager=new me(e),this.persistence=new fe(e),this.envProvider=new pe}async initialize(){if(this.initialized)return;this.merger.clearSources(),this.merger.addSource({name:`defaults`,config:this.loader.getDefaultConfig(),precedence:k.DEFAULTS});let e=await this.loader.loadGlobalConfig();e&&this.merger.addSource({name:`global`,config:e,precedence:k.GLOBAL});let t=await this.loader.loadLocalConfig();t&&this.merger.addSource({name:`local`,config:t,precedence:k.LOCAL});let n=this.envProvider.loadConfig();Object.keys(n).length>0&&this.merger.addSource({name:`environment`,config:n,precedence:k.ENVIRONMENT}),this.config=this.merger.merge(),await this.stateManager.loadState(),this.initialized=!0}getConfig(){return this.config}getStorageConfig(){let e=this.config.storage,t=e?.type;return t&&t!==`file`&&E(`ConfigManager`).warn(`Storage type "${t}" is no longer supported; using local file storage (.taskgraph/tasks/tasks.json).`),{type:`file`,basePath:e?.basePath??this.projectRoot}}getModelConfig(){return this.config.models||{main:S.MODELS.MAIN,fallback:S.MODELS.FALLBACK}}getResponseLanguage(){return this.config.custom?.responseLanguage||`English`}getProjectRoot(){return this.projectRoot}getActiveTag(){return this.stateManager.getCurrentTag()}async setActiveTag(e){await this.stateManager.setCurrentTag(e)}async updateConfig(e){Object.assign(this.config,e),await this.persistence.saveConfig(this.config),this.initialized=!1,await this.initialize()}async setResponseLanguage(e){this.config.custom||(this.config.custom={}),this.config.custom.responseLanguage=e,await this.persistence.saveConfig(this.config)}async saveConfig(){await this.persistence.saveConfig(this.config,{createBackup:!0,atomic:!0})}async reset(){await this.persistence.deleteConfig(),await this.stateManager.clearState(),this.initialized=!1,this.config={},await this.initialize()}getConfigSources(){return this.merger.getSources()}},A=class{projectPath;git;constructor(e){if(!e)throw Error(`Project path is required`);if(!c.isAbsolute(e))throw Error(`Project path must be an absolute path`);this.projectPath=c.normalize(e),this.git=d(this.projectPath)}async isGitRepository(){try{let e=c.join(this.projectPath,`.git`);if(await u.pathExists(e))return!0;try{return await this.git.revparse([`--git-dir`]),!0}catch{return!1}}catch{return!1}}async validateGitInstallation(){try{await this.git.version()}catch(e){let t=e instanceof Error?e.message:String(e);throw Error(`Git is not installed or not accessible: ${t}`)}}async getGitVersion(){let e=await this.git.version();return{major:e.major,minor:e.minor,patch:typeof e.patch==`string`?parseInt(e.patch):e.patch||0,agent:e.agent}}async getRepositoryRoot(){try{let e=await this.git.revparse([`--show-toplevel`]);return c.normalize(e.trim())}catch{throw Error(`not a git repository: ${this.projectPath}`)}}async validateRepository(){if(!await this.isGitRepository())throw Error(`not a git repository: ${this.projectPath}`);try{await this.git.status()}catch(e){let t=e instanceof Error?e.message:String(e);throw Error(`Repository validation failed: ${t}`)}}async ensureGitRepository(){if(!await this.isGitRepository())throw Error(`not a git repository: ${this.projectPath}\nPlease run this command from within a git repository, or initialize one with 'git init'.`)}async isWorkingTreeClean(){return(await this.git.status()).isClean()}async getStatus(){return await this.git.status()}async hasUncommittedChanges(){return!(await this.git.status()).isClean()}async hasStagedChanges(){return(await this.git.status()).staged.length>0}async hasUntrackedFiles(){return(await this.git.status()).not_added.length>0}async getStatusSummary(){let e=await this.git.status(),t=e.staged.length,n=e.modified.length,r=e.deleted.length,i=e.not_added.length,a=t+n+r+i;return{isClean:e.isClean(),staged:t,modified:n,deleted:r,untracked:i,totalChanges:a}}async ensureCleanWorkingTree(){if(!(await this.git.status()).isClean()){let e=await this.getStatusSummary();throw Error(`working tree is not clean: ${this.projectPath}\nStaged: ${e.staged}, Modified: ${e.modified}, Deleted: ${e.deleted}, Untracked: ${e.untracked}\nPlease commit or stash your changes before proceeding.`)}}async getCurrentBranch(){return(await this.git.status()).current||`HEAD`}async listBranches(){let e=await this.git.branchLocal();return Object.keys(e.branches)}async branchExists(e){return(await this.listBranches()).includes(e)}async createBranch(e,t={}){if(await this.branchExists(e))throw Error(`branch already exists: ${e}`);t.checkout&&await this.ensureCleanWorkingTree(),await this.git.branch([e]),t.checkout&&await this.git.checkout(e)}async checkoutBranch(e,t={}){if(!await this.branchExists(e))throw Error(`branch does not exist: ${e}`);t.force||await this.ensureCleanWorkingTree();let n=t.force?[`-f`,e]:[e];await this.git.checkout(n)}async createAndCheckoutBranch(e){if(await this.ensureCleanWorkingTree(),await this.branchExists(e))throw Error(`branch already exists: ${e}`);await this.git.checkoutLocalBranch(e)}async deleteBranch(e,t={}){if(!await this.branchExists(e))throw Error(`branch does not exist: ${e}`);if(await this.getCurrentBranch()===e)throw Error(`cannot delete current branch: ${e}`);let n=t.force?[`-D`,e]:[`-d`,e];await this.git.branch(n)}async stageFiles(e){await this.git.add(e)}async unstageFiles(e){await this.git.reset([`HEAD`,`--`,...e])}async createCommit(e,t={}){if(t.enforceNonDefaultBranch&&!t.force){let e=await this.getCurrentBranch();if([`main`,`master`,`develop`].includes(e))throw Error(`cannot commit to default branch: ${e}\nPlease create a feature branch or use force option.`)}if(!t.allowEmpty&&!await this.hasStagedChanges())throw Error(`no staged changes to commit`);let n=[`commit`];if(n.push(`-m`,e),t.metadata){n.push(`-m`,``);for(let[e,r]of Object.entries(t.metadata))n.push(`-m`,`[${e}:${r}]`)}n.push(`--no-gpg-sign`),t.allowEmpty&&n.push(`--allow-empty`),await this.git.raw(n)}async getCommitLog(e={}){let t={format:{hash:`%H`,date:`%ai`,message:`%B`,author_name:`%an`,author_email:`%ae`}};return e.maxCount&&(t.maxCount=e.maxCount),[...(await this.git.log(t)).all]}async getLastCommit(){return(await this.git.log({maxCount:1,format:{hash:`%H`,date:`%ai`,message:`%B`,author_name:`%an`,author_email:`%ae`}})).latest}async getDefaultBranch(){let e=await this.getCurrentBranch(),t=[`main`,`master`,`develop`];if(t.includes(e))return e;let n=await this.listBranches();for(let e of t)if(n.includes(e))return e;return`main`}async isDefaultBranch(e){return[`main`,`master`,`develop`].includes(e)}async isOnDefaultBranch(){let e=await this.getCurrentBranch();return await this.isDefaultBranch(e)}async ensureNotOnDefaultBranch(){if(await this.isOnDefaultBranch()){let e=await this.getCurrentBranch();throw Error(`currently on default branch: ${e}\nPlease create a feature branch before proceeding.`)}}async hasRemote(){return(await this.git.getRemotes()).length>0}async getRemotes(){return await this.git.getRemotes(!0)}};const ge=[[`**/*.test.*`,`test`],[`**/*.spec.*`,`test`],[`**/test/**`,`test`],[`**/tests/**`,`test`],[`**/__tests__/**`,`test`],[`**/package-lock.json`,`deps`],[`package-lock.json`,`deps`],[`**/pnpm-lock.yaml`,`deps`],[`pnpm-lock.yaml`,`deps`],[`**/yarn.lock`,`deps`],[`yarn.lock`,`deps`],[`**/package.json`,`config`],[`package.json`,`config`],[`**/tsconfig*.json`,`config`],[`tsconfig*.json`,`config`],[`**/.eslintrc*`,`config`],[`.eslintrc*`,`config`],[`**/vite.config.*`,`config`],[`vite.config.*`,`config`],[`**/vitest.config.*`,`config`],[`vitest.config.*`,`config`],[`packages/cli/**`,`cli`],[`packages/tm-core/**`,`core`],[`packages/mcp-server/**`,`mcp`],[`**/workflow/**`,`workflow`],[`**/git/**`,`git`],[`**/storage/**`,`storage`],[`**/auth/**`,`auth`],[`**/config/**`,`config`],[`**/*.md`,`docs`],[`**/docs/**`,`docs`],[`README*`,`docs`],[`CHANGELOG*`,`docs`]],_e={core:100,cli:90,mcp:85,workflow:80,git:75,storage:70,auth:65,config:60,test:50,docs:30,deps:20,repo:10};var ve=class{scopeMappings;scopePriorities;constructor(e,t){this.scopeMappings=[...ge],e&&(this.scopeMappings=[...Object.entries(e),...this.scopeMappings]),this.scopePriorities={..._e,...t}}detectScope(e){if(e.length===0)return`repo`;let t=new Map;for(let n of e){let e=this.getMatchingScope(n);e&&t.set(e,(t.get(e)||0)+1)}if(t.size===0)return`repo`;let n=`repo`,r=0;for(let[e,i]of t){let t=this.getScopePriority(e)*i;t>r&&(r=t,n=e)}return n}getAllMatchingScopes(e){let t=new Set;for(let n of e){let e=this.getMatchingScope(n);e&&t.add(e)}return Array.from(t)}getMatchingScope(e){let t=e.replace(/\\/g,`/`);for(let[e,n]of this.scopeMappings)if(this.matchesPattern(t,e))return n;return null}getScopePriority(e){return this.scopePriorities[e]||0}matchesPattern(e,t){let n=t.replace(/\*\*/g,`§GLOBSTAR§`);return n=n.replace(/[.+^${}()|[\]\\]/g,`\\$&`),n=n.replace(/\*/g,`[^/]*`),n=n.replace(/§GLOBSTAR§/g,`.*`),RegExp(`^${n}$`).test(e)}};const ye={commitMessage:`{{type}}{{#scope}}({{scope}}){{/scope}}{{#breaking}}!{{/breaking}}: {{description}}
2
+
3
+ {{#body}}{{body}}
4
+
5
+ {{/body}}{{#taskId}}Task: {{taskId}}{{/taskId}}{{#phase}}
6
+ Phase: {{phase}}{{/phase}}{{#testsPassing}}
7
+ Tests: {{testsPassing}} passing{{#testsFailing}}, {{testsFailing}} failing{{/testsFailing}}{{/testsPassing}}`};var be=class{templates;preservePlaceholders;constructor(e={}){let t=`customTemplates`in e||`preservePlaceholders`in e?e:{customTemplates:e};this.templates={...ye,...t.customTemplates||{}},this.preservePlaceholders=t.preservePlaceholders??!1}render(e,t,n){let r=n===void 0?this.templates[e]:n;if(r===void 0)throw Error(`Template "${e}" not found`);return this.substituteVariables(r,t)}setTemplate(e,t){this.templates[e]=t}getTemplate(e){return this.templates[e]}hasTemplate(e){return e in this.templates}validateTemplate(e,t){let n=this.extractVariables(e),r=t.filter(e=>!n.includes(e));return{isValid:r.length===0,missingVars:r}}extractVariables(e){let t=e.matchAll(/\{\{\s*([^}#/\s]+)\s*\}\}/g),n=new Set;for(let e of t)n.add(e[1]);return Array.from(n)}substituteVariables(e,t){let n=e;return n=this.processConditionalBlocks(n,t),n=n.replace(/\{\{\s*([^}#/\s]+)\s*\}\}/g,(e,n)=>{let r=t[n];return r==null?this.preservePlaceholders?`{{${n}}}`:``:String(r)}),n}processConditionalBlocks(e,t){let n=e,r=!0;for(;r;){let e=n;n=n.replace(/\{\{#([^}]+)\}\}((?:(?!\{\{#).)*?)\{\{\/\1\}\}/gs,(e,n,r)=>{let i=t[n.trim()];return i!=null&&i!==!1&&i!==``?r:``}),r=n!==e}return n}};const j=[`feat`,`fix`,`docs`,`style`,`refactor`,`perf`,`test`,`build`,`ci`,`chore`,`revert`];var M=class{templateEngine;scopeDetector;constructor(e,t,n){this.templateEngine=new be(e),this.scopeDetector=new ve(t,n)}generateMessage(e){let{type:t,description:n,changedFiles:r,scope:i,body:a,breaking:o=!1,taskId:s,phase:c,tag:l,testsPassing:u,testsFailing:d,coveragePercent:f}=e,p={type:t,scope:i??this.scopeDetector.detectScope(r),breaking:o?`!`:``,description:n,body:a,taskId:s,phase:c,tag:l,testsPassing:u,testsFailing:d,coveragePercent:f};return this.templateEngine.render(`commitMessage`,p)}validateConventionalCommit(e){let t=[],n=e.split(`
8
+ `)[0];if(!n)return t.push(`Missing commit message`),{isValid:!1,errors:t};let r=n.match(/^(\w+)(?:\(([^)]+)\))?(!)?:\s*(.+)$/);if(!r)return t.push(`Invalid conventional commit format. Expected: type(scope): description`),{isValid:!1,errors:t};let[,i,,,a]=r;return j.includes(i)||t.push(`Invalid commit type "${i}". Must be one of: ${j.join(`, `)}`),(!a||a.trim().length===0)&&t.push(`Missing description`),{isValid:t.length===0,errors:t}}parseCommitMessage(e){let t=e.split(`
9
+ `),n=t[0].match(/^(\w+)(?:\(([^)]+)\))?(!)?:\s*(.+)$/);if(!n)throw Error(`Invalid conventional commit format`);let[,r,i,a,o]=n,s=t.findIndex((e,t)=>t>0&&e===``),c=s===-1?void 0:t.slice(s+1).join(`
10
+ `).trim();return{type:r,scope:i,breaking:a===`!`,description:o,body:c}}getScopeDetector(){return this.scopeDetector}getTemplateEngine(){return this.templateEngine}},xe=class{gitAdapter;commitGenerator;constructor(e){this.gitAdapter=new A(e),this.commitGenerator=new M}async isGitRepository(){return this.gitAdapter.isGitRepository()}async ensureGitRepository(){return this.gitAdapter.ensureGitRepository()}async getRepositoryRoot(){return this.gitAdapter.getRepositoryRoot()}async isWorkingTreeClean(){return this.gitAdapter.isWorkingTreeClean()}async getStatus(){return this.gitAdapter.getStatus()}async getStatusSummary(){return this.gitAdapter.getStatusSummary()}async hasUncommittedChanges(){return this.gitAdapter.hasUncommittedChanges()}async hasStagedChanges(){return this.gitAdapter.hasStagedChanges()}async getCurrentBranch(){return this.gitAdapter.getCurrentBranch()}async listBranches(){return this.gitAdapter.listBranches()}async branchExists(e){return this.gitAdapter.branchExists(e)}async createBranch(e,t){return this.gitAdapter.createBranch(e,t)}async checkoutBranch(e,t){return this.gitAdapter.checkoutBranch(e,t)}async createAndCheckoutBranch(e){return this.gitAdapter.createAndCheckoutBranch(e)}async deleteBranch(e,t){return this.gitAdapter.deleteBranch(e,t)}async getDefaultBranch(){return this.gitAdapter.getDefaultBranch()}async isOnDefaultBranch(){return this.gitAdapter.isOnDefaultBranch()}async stageFiles(e){return this.gitAdapter.stageFiles(e)}async unstageFiles(e){return this.gitAdapter.unstageFiles(e)}async createCommit(e,t){return this.gitAdapter.createCommit(e,t)}async getCommitLog(e){return this.gitAdapter.getCommitLog(e)}async getLastCommit(){return this.gitAdapter.getLastCommit()}async hasRemote(){return this.gitAdapter.hasRemote()}async getRemotes(){return this.gitAdapter.getRemotes()}generateCommitMessage(e){return this.commitGenerator.generateMessage(e)}validateCommitMessage(e){return this.commitGenerator.validateConventionalCommit(e)}parseCommitMessage(e){return this.commitGenerator.parseCommitMessage(e)}};const N=m(f);async function Se(e){if(!e)throw Error(`projectRoot is required for isGitRepository`);try{return await N(`git rev-parse --git-dir`,{cwd:e}),!0}catch{return!1}}async function Ce(e){if(!e)throw Error(`projectRoot is required for getLocalBranches`);try{let{stdout:t}=await N(`git branch --format="%(refname:short)"`,{cwd:e,maxBuffer:10*1024*1024});return t.trim().split(`
11
+ `).filter(e=>e.length>0).map(e=>e.trim())}catch{return[]}}async function we(e){if(!e)throw Error(`projectRoot is required for getRemoteBranches`);try{let{stdout:t}=await N(`git branch -r --format="%(refname:short)"`,{cwd:e,maxBuffer:10*1024*1024}),n=t.trim().split(`
12
+ `).filter(e=>e.length>0&&!e.includes(`HEAD`)).map(e=>e.replace(/^[^/]+\//,``).trim());return Array.from(new Set(n))}catch{return[]}}async function P(e){try{return await N(`gh auth status`,e?{cwd:e}:{}),!0}catch{return!1}}async function Te(e){if(!e)throw Error(`projectRoot is required for getGitHubRepoInfo`);try{let{stdout:t}=await N(`gh repo view --json name,owner,defaultBranchRef`,{cwd:e});return JSON.parse(t)}catch{return null}}async function Ee(e){if(!e)throw Error(`projectRoot is required for getDefaultBranch`);try{if(await P(e)){let t=await Te(e);if(t&&t.defaultBranchRef)return t.defaultBranchRef.name}let t=(await N(`git remote`,{cwd:e})).stdout.trim().split(`
13
+ `).filter(Boolean);if(t.length>0){let n=t.includes(`origin`)?`origin`:t[0];try{let{stdout:t}=await N(`git remote show ${n}`,{cwd:e,maxBuffer:10*1024*1024}),r=t.match(/HEAD branch:\s+([^\s]+)/);if(r)return r[1].trim()}catch{}try{let{stdout:t}=await N(`git symbolic-ref refs/remotes/${n}/HEAD`,{cwd:e});return t.replace(`refs/remotes/${n}/`,``).trim()}catch{}}throw Error(`default-branch-not-found`)}catch{let t=[`main`,`master`],n=await Ce(e),r=await we(e);for(let e of t)if(n.includes(e)||r.includes(e))return e;return null}}const De=/^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/,Oe=e=>new Promise(t=>setTimeout(t,e));function F(e){return{number:e.number,url:e.html_url,title:e.title,body:e.body??``,labels:e.labels.map(e=>typeof e==`string`?e:e.name),state:e.state,stateReason:e.state_reason===`completed`||e.state_reason===`not_planned`?e.state_reason:void 0}}function ke(e){let t=e,n=0,r={};for(;t.startsWith(`HTTP/`);){let e=t.search(/\r?\n\r?\n/),[i,...a]=(e===-1?t:t.slice(0,e)).split(/\r?\n/);n=Number(i.split(` `)[1]),r={};for(let e of a){let t=e.indexOf(`:`);t>0&&(r[e.slice(0,t).trim().toLowerCase()]=e.slice(t+1).trim())}t=e===-1?``:t.slice(e).replace(/^\r?\n\r?\n/,``)}let i=null;if(t.trim())try{i=JSON.parse(t)}catch{i=t}return{status:n,headers:r,data:i}}var Ae=class{sleep;constructor(e,t={}){if(this.repo=e,this.options=t,!De.test(e))throw new O(`Invalid repository "${e}", expected owner/name`,D.INVALID_INPUT);this.sleep=t.sleep??Oe}async getRepo(){let{data:e}=await this.request(`GET`,`repos/${this.repo}`);return{fullName:e.full_name,url:e.html_url}}async listIssues(e){let t=[];for(let n=1;;n++){let r=`labels=${encodeURIComponent(e)}&state=all&per_page=100&page=${n}`,{data:i}=await this.request(`GET`,`repos/${this.repo}/issues?${r}`),a=i;if(t.push(...a.filter(e=>!e.pull_request).map(F)),a.length<100)return t}}async createIssue(e){let{data:t}=await this.request(`POST`,`repos/${this.repo}/issues`,e);return F(t)}async updateIssue(e,t){let{stateReason:n,...r}=t,i=n?{...r,state_reason:n}:r,{data:a}=await this.request(`PATCH`,`repos/${this.repo}/issues/${e}`,i);return F(a)}async ensureLabels(e){let t=new Set;for(let e=1;;e++){let{data:n}=await this.request(`GET`,`repos/${this.repo}/labels?per_page=100&page=${e}`);for(let e of n)t.add(e.name.toLowerCase());if(n.length<100)break}for(let n of new Set(e))t.has(n.toLowerCase())||await this.request(`POST`,`repos/${this.repo}/labels`,{name:n,color:`5319e7`})}async request(e,t,n){let r=[`api`,`-i`,`-X`,e,t];n!==void 0&&r.push(`--input`,`-`);let i=n===void 0?void 0:JSON.stringify(n);for(let n=0;;n++){let{stdout:a,stderr:o}=await this.run(r,i),s=ke(a);if(s.status>0&&s.status<400)return s;let c=Number(s.headers[`retry-after`]);if((s.status===403||s.status===429)&&c>0&&n<3){await this.sleep(Math.min(c*1e3,12e4));continue}let l=s.data&&s.data.message||o.trim()||`gh failed`;throw new O(`GitHub request ${e} ${t} failed (${s.status||`no response`}): ${l}`,D.API_ERROR,{operation:`gh-api`,resource:t})}}run(e,t){return new Promise(n=>{h(`gh`,e,{cwd:this.options.cwd,maxBuffer:64*1024*1024},(e,t,r)=>{n({stdout:String(t??``),stderr:String(r??(e?e.message:``))})}).stdin?.end(t)})}};const I=`taskgraph`,je=/<!-- taskgraph:v1 tag=(\S*) id=(\S*) -->/;function Me(e,t){return`<!-- taskgraph:v1 tag=${encodeURIComponent(e)} id=${encodeURIComponent(t)} -->`}function Ne(e){let t=je.exec(e??``);if(!t)return null;try{return{tag:decodeURIComponent(t[1]),id:decodeURIComponent(t[2])}}catch{return null}}function L(e){return`${I}:${e}`.slice(0,50)}const R=e=>e===`done`||e===`completed`;function Pe(e){return R(e)?{state:`closed`,stateReason:`completed`}:e===`cancelled`?{state:`closed`,stateReason:`not_planned`}:{state:`open`}}function Fe(e,t){let n=String(e.id),r=Pe(e.status),i=[I];e.priority&&i.push(L(`priority/${e.priority}`)),r.state===`open`&&i.push(L(`status/${e.status}`)),i.push(L(`tag/${t.tag}`));let a=[Me(t.tag,n)];e.description&&a.push(e.description);let o=(e.dependencies??[]).map(e=>t.issueNumbers?.get(String(e))).filter(e=>e!==void 0).map(e=>`Blocked by #${e}`);if(o.length&&a.push(o.join(`
14
+ `)),e.subtasks?.length){let t=e.subtasks.map(e=>`- [${R(e.status)?`x`:` `}] ${n}.${e.id} ${e.title}`);a.push(`### Subtasks\n${t.join(`
15
+ `)}`)}return t.includeDetails&&(e.details&&a.push(`### Details\n${e.details}`),e.testStrategy&&a.push(`### Test strategy\n${e.testStrategy}`)),{title:`[${n}] ${e.title}`,body:a.join(`
16
+
17
+ `),labels:i.sort(),...r}}function z(e){let t=JSON.stringify([e.title,e.body,[...e.labels].sort(),e.state,e.stateReason??null]);return v(`sha256`).update(t).digest(`hex`)}const B=(e,t)=>`${e}\u0000${t}`;function V(e,t){let n=e.metadata?.github;if(!(!n||typeof n.issueNumber!=`number`))return n.repo===t?n:void 0}function Ie(e){let{repo:t,tasks:n,remote:r}=e,i=new Map;for(let e of r){let t=Ne(e.body);t&&i.set(B(t.tag,t.id),e)}let a=new Map,o=new Set,s=n.map(({tag:e,task:n})=>{let r=String(n.id),s=V(n,t),c=s?void 0:i.get(B(e,r)),l=s?.issueNumber??c?.number;return l!==void 0&&(o.add(l),a.has(e)||a.set(e,new Map),a.get(e)?.set(r,l)),{tag:e,task:n,link:s,recovered:c,number:l}}),c=new Set(s.filter(e=>e.number===void 0).map(e=>B(e.tag,String(e.task.id)))),l=s.map(({tag:t,task:n,link:r,recovered:i,number:o})=>{let s={tag:t,taskId:String(n.id),title:n.title};return o===void 0?{...s,action:`create`}:r?z(Fe(n,{tag:t,includeDetails:e.includeDetails,issueNumbers:a.get(t)}))===r.bodyHash?(n.dependencies??[]).some(e=>c.has(B(t,String(e))))?{...s,action:`update`,issueNumber:o,reason:`dependency issue not created yet`}:{...s,action:`skip`,issueNumber:o}:{...s,action:`update`,issueNumber:o,reason:`content changed`}:{...s,action:`update`,issueNumber:o,recovered:!0,url:i?.url,reason:`link recovered from issue marker`}}),u=e.scopeTags?new Set(e.scopeTags):void 0,d=[];for(let e of r){if(e.state!==`open`||o.has(e.number))continue;let t=Ne(e.body);!t||u&&!u.has(t.tag)||d.push({issueNumber:e.number,url:e.url,title:e.title,tag:t.tag,taskId:t.id})}if(e.closeOrphans)for(let e of d)l.push({action:`close`,tag:e.tag??``,taskId:e.taskId??``,title:e.title,issueNumber:e.issueNumber,reason:`task no longer exists locally`});return{repo:t,steps:l,orphans:d}}const Le=e=>new Promise(t=>setTimeout(t,e)),Re=e=>({...e,state:`open`,stateReason:void 0});async function ze(e,t,n){let{client:r}=n,i=e.repo,a=n.sleep??Le,o=n.now??(()=>new Date),s=n.delayMs??400,c={created:0,updated:0,skipped:0,closed:0},l=new Map(t.map(e=>[`${e.tag}\u0000${e.task.id}`,e])),u=e=>l.get(`${e.tag}\u0000${e.taskId}`),d=new Map,f=(e,t,n)=>{d.has(e)||d.set(e,new Map),d.get(e)?.set(t,n)};for(let{tag:e,task:n}of t){let t=V(n,i);t&&f(e,String(n.id),t.issueNumber)}let p=({tag:e,task:t})=>Fe(t,{tag:e,includeDetails:n.includeDetails,issueNumbers:d.get(e)}),m=async({tag:e,task:t},r,a,s)=>{let c={repo:i,issueNumber:r,url:a,syncedAt:o().toISOString(),lastPushedStatus:t.status,bodyHash:s},l={...t.metadata,github:c};await n.tasks.update(t.id,{metadata:l},e),t.metadata=l},h=!1,g=async()=>{h&&s>0&&await a(s),h=!0},_=e.steps.filter(e=>e.action===`create`||e.action===`update`);for(let t of e.steps)t.action===`skip`&&c.skipped++;for(let e of _.filter(e=>e.recovered)){let t=u(e);!t||e.issueNumber===void 0||(f(e.tag,e.taskId,e.issueNumber),await m(t,e.issueNumber,e.url??``,``))}let v=new Set([I]);for(let e of _){let t=u(e);if(t)for(let e of p(t).labels)v.add(e)}_.length>0&&await r.ensureLabels([...v]);for(let e of _.filter(e=>e.action===`create`)){let t=u(e);if(!t)continue;let n=p(t);await g();let i=await r.createIssue({title:n.title,body:n.body,labels:n.labels});f(e.tag,e.taskId,i.number),await m(t,i.number,i.url,z(Re(n))),c.created++}for(let e of _){let t=u(e);if(!t)continue;let n=V(t.task,i);if(!n)continue;let a=p(t),o=z(a);o!==n.bodyHash&&(await g(),await r.updateIssue(n.issueNumber,{title:a.title,body:a.body,labels:a.labels,state:a.state,stateReason:a.stateReason}),await m(t,n.issueNumber,n.url,o),e.action===`update`&&c.updated++)}for(let t of e.steps.filter(e=>e.action===`close`))t.issueNumber!==void 0&&(await g(),await r.updateIssue(t.issueNumber,{state:`closed`,stateReason:`not_planned`}),c.closed++);return c}var Be=class{tasksDomain;constructor(e,t={}){this.projectRoot=e,this.options=t}setTasksDomain(e){this.tasksDomain=e}async status(e={}){let t=await P(this.projectRoot),n=e.repo??null;!n&&t&&(n=await this.detectRepo());let r=await this.loadTasks(),i=r.filter(e=>e.task.metadata?.github).length;return{ghAvailable:t,repo:n,linkedTasks:i,unlinkedTasks:r.length-i}}async planPush(e={}){let{plan:t}=await this.prepare(e);return t}async push(e={}){let{plan:t,client:n,tasks:r}=await this.prepare(e),i={created:0,updated:0,skipped:0,closed:0};if(e.dryRun){for(let e of t.steps){let t={create:`created`,update:`updated`,skip:`skipped`,close:`closed`}[e.action];i[t]++}return{dryRun:!0,plan:t,...i,orphans:t.orphans}}return{dryRun:!1,plan:t,...await ze(t,r,{client:n,tasks:this.requireTasks(),includeDetails:e.includeDetails,delayMs:e.delayMs,sleep:this.options.sleep,now:this.options.now}),orphans:t.orphans}}async prepare(e){let t=e.repo??await this.detectRepo();if(!t)throw new O("Could not determine the GitHub repository. Pass a repo as owner/name or run `gh auth login` in a GitHub checkout.",D.MISSING_CONFIGURATION);let n=this.options.clientFactory?this.options.clientFactory(t):new Ae(t,{cwd:this.projectRoot}),r=await this.loadTasks(e.tag);return{plan:Ie({repo:t,tasks:r,remote:await n.listIssues(I),includeDetails:e.includeDetails,closeOrphans:e.closeOrphans,scopeTags:e.tag?[e.tag]:void 0}),client:n,tasks:r}}async detectRepo(){let e=await Te(this.projectRoot);return e?`${e.owner.login}/${e.name}`:null}requireTasks(){if(!this.tasksDomain)throw new O(`GitHubDomain is not wired to the tasks domain`,D.INTERNAL_ERROR);return this.tasksDomain}async loadTasks(e){let t=this.requireTasks(),n=e?[e]:(await t.getTagsWithStats()).tags.map(e=>e.name),r=[];for(let e of n){let{tasks:n}=await t.list({tag:e,includeSubtasks:!0});for(let t of n)r.push({tag:e,task:t})}return r}};const H={default:`SETUP: If taskgraph command not found, run: npm i -g taskgraph-ai
18
+
19
+ TASK: Implement ONE task/subtask from the TaskGraph backlog.
20
+
21
+ PROCESS:
22
+ 1. Run taskgraph next (or use MCP) to get the next available task/subtask.
23
+ 2. Read task details with taskgraph show <id>.
24
+ 3. Implement following codebase patterns.
25
+ 4. Write tests alongside implementation.
26
+ 5. Run type check (e.g., \`npm run typecheck\`, \`tsc --noEmit\`).
27
+ 6. Run tests (e.g., \`npm test\`, \`npm run test\`).
28
+ 7. Mark complete: taskgraph set-status --id=<id> --status=done
29
+ 8. Commit with message: feat(<scope>): <what was implemented>
30
+ 9. Append super-concise notes to progress file: task ID, what was done, any learnings.
31
+
32
+ IMPORTANT:
33
+ - Complete ONLY ONE task per iteration.
34
+ - Keep changes small and focused.
35
+ - Do NOT start another task after completing one.
36
+ - If all tasks are done, output <loop-complete>ALL_DONE</loop-complete>.
37
+ - If blocked, output <loop-blocked>REASON</loop-blocked>.
38
+ `,"test-coverage":`# TaskGraph Loop - Test Coverage
39
+
40
+ Find uncovered code and write meaningful tests. ONE test per session.
41
+
42
+ ## Files Available
43
+
44
+ - @.taskgraph/loop-progress.txt - Progress log (coverage %, what was tested)
45
+
46
+ ## What Makes a Great Test
47
+
48
+ A great test covers behavior users depend on. It tests a feature that, if broken,
49
+ would frustrate or block users. It validates real workflows - not implementation details.
50
+
51
+ Do NOT write tests just to increase coverage. Use coverage as a guide to find
52
+ UNTESTED USER-FACING BEHAVIOR. If code is not worth testing (boilerplate, unreachable
53
+ branches, internal plumbing), add ignore comments instead of low-value tests.
54
+
55
+ ## Process
56
+
57
+ 1. Run coverage command (\`pnpm coverage\`, \`npm run coverage\`, etc.)
58
+ 2. Identify the most important USER-FACING FEATURE that lacks tests
59
+ - Prioritize: error handling users hit, CLI commands, API endpoints, file parsing
60
+ - Deprioritize: internal utilities, edge cases users won't encounter, boilerplate
61
+ 3. Write ONE meaningful test that validates the feature works correctly
62
+ 4. Run coverage again - it should increase as a side effect of testing real behavior
63
+ 5. Commit with message: \`test(<file>): <describe the user behavior being tested>\`
64
+ 6. Append to progress file: what you tested, new coverage %, learnings
65
+
66
+ ## Important
67
+
68
+ - Complete ONLY ONE test per session
69
+ - Keep tests focused on user-facing behavior
70
+ - Do NOT start another test after completing one
71
+
72
+ ## Completion Criteria
73
+
74
+ - If coverage reaches target (or 100%), output: <loop-complete>COVERAGE_TARGET</loop-complete>
75
+ `,linting:`# TaskGraph Loop - Linting
76
+
77
+ Fix lint errors and type errors one by one. ONE fix per session.
78
+
79
+ ## Files Available
80
+
81
+ - @.taskgraph/loop-progress.txt - Progress log (errors fixed, remaining count)
82
+
83
+ ## Process
84
+
85
+ 1. Run lint command (\`pnpm lint\`, \`npm run lint\`, \`eslint .\`, etc.)
86
+ 2. Run type check (\`pnpm typecheck\`, \`tsc --noEmit\`, etc.)
87
+ 3. Pick ONE error to fix - prioritize:
88
+ - Type errors (breaks builds)
89
+ - Security-related lint errors
90
+ - Errors in frequently-changed files
91
+ 4. Fix the error with minimal changes - don't refactor surrounding code
92
+ 5. Run lint/typecheck again to verify the fix doesn't introduce new errors
93
+ 6. Commit with message: \`fix(<file>): <describe the lint/type error fixed>\`
94
+ 7. Append to progress file: error fixed, remaining error count
95
+
96
+ ## Important
97
+
98
+ - Complete ONLY ONE fix per session
99
+ - Keep changes minimal and focused
100
+ - Do NOT start another fix after completing one
101
+
102
+ ## Completion Criteria
103
+
104
+ - If zero lint errors and zero type errors, output: <loop-complete>ZERO_ERRORS</loop-complete>
105
+ `,duplication:`# TaskGraph Loop - Duplication
106
+
107
+ Find duplicated code and refactor into shared utilities. ONE refactor per session.
108
+
109
+ ## Files Available
110
+
111
+ - @.taskgraph/loop-progress.txt - Progress log (clones refactored, duplication %)
112
+
113
+ ## Process
114
+
115
+ 1. Run duplication detection (\`npx jscpd .\`, or similar tool)
116
+ 2. Review the report and pick ONE clone to refactor - prioritize:
117
+ - Larger clones (more lines = more maintenance burden)
118
+ - Clones in frequently-changed files
119
+ - Clones with slight variations (consolidate logic)
120
+ 3. Extract the duplicated code into a shared utility/function
121
+ 4. Update all clone locations to use the shared utility
122
+ 5. Run tests to ensure behavior is preserved
123
+ 6. Commit with message: \`refactor(<file>): extract <utility> to reduce duplication\`
124
+ 7. Append to progress file: what was refactored, new duplication %
125
+
126
+ ## Important
127
+
128
+ - Complete ONLY ONE refactor per session
129
+ - Keep changes focused on the specific duplication
130
+ - Do NOT start another refactor after completing one
131
+
132
+ ## Completion Criteria
133
+
134
+ - If duplication below threshold (e.g., <3%), output: <loop-complete>LOW_DUPLICATION</loop-complete>
135
+ `,entropy:`# TaskGraph Loop - Entropy (Code Smells)
136
+
137
+ Find code smells and clean them up. ONE cleanup per session.
138
+
139
+ ## Files Available
140
+
141
+ - @.taskgraph/loop-progress.txt - Progress log (smells fixed, areas cleaned)
142
+
143
+ ## Code Smells to Target
144
+
145
+ - Long functions (>60 lines) - extract into smaller functions
146
+ - Deep nesting (>3 levels) - use early returns, extract conditions
147
+ - Large files (>500 lines) - split into focused modules
148
+ - Magic numbers - extract into named constants
149
+ - Complex conditionals - extract into well-named functions
150
+ - God classes - split responsibilities
151
+
152
+ ## Process
153
+
154
+ 1. Scan the codebase for code smells (use your judgment or tools like \`complexity-report\`)
155
+ 2. Pick ONE smell to fix - prioritize:
156
+ - Smells in frequently-changed files
157
+ - Smells that hurt readability the most
158
+ - Smells in critical paths (authentication, payments, etc.)
159
+ 3. Refactor with minimal changes - don't over-engineer
160
+ 4. Run tests to ensure behavior is preserved
161
+ 5. Commit with message: \`refactor(<file>): <describe the cleanup>\`
162
+ 6. Append to progress file: what was cleaned, smell type
163
+
164
+ ## Important
165
+
166
+ - Complete ONLY ONE cleanup per session
167
+ - Keep refactoring focused and minimal
168
+ - Do NOT start another cleanup after completing one
169
+
170
+ ## Completion Criteria
171
+
172
+ - If no significant smells remain, output: <loop-complete>LOW_ENTROPY</loop-complete>
173
+ `},Ve=Object.keys(H);function He(e){return H[e]}function U(e){return e in H}var W=class{projectRoot;logger=E(`LoopService`);_isRunning=!1;constructor(e){this.projectRoot=e.projectRoot}getProjectRoot(){return this.projectRoot}get isRunning(){return this._isRunning}checkSandboxAuth(){let e=_(`docker`,[`sandbox`,`run`,`claude`,`-p`,`Say OK`],{cwd:this.projectRoot,timeout:3e4,encoding:`utf-8`,stdio:[`inherit`,`pipe`,`pipe`]});return e.error?e.error.code===`ENOENT`?{ready:!1,error:`Docker is not installed. Install Docker Desktop to use --sandbox mode.`}:{ready:!1,error:`Docker error: ${e.error.message}`}:{ready:((e.stdout||``)+(e.stderr||``)).toLowerCase().includes(`ok`)}}runInteractiveAuth(){let e=_(`docker`,[`sandbox`,`run`,`claude`,`You're authenticated! Press Ctrl+C to continue.`],{cwd:this.projectRoot,stdio:`inherit`});return e.error?e.error.code===`ENOENT`?{success:!1,error:`Docker is not installed. Install Docker Desktop to use --sandbox mode.`}:{success:!1,error:`Docker error: ${e.error.message}`}:e.status===null?{success:!1,error:`Docker terminated abnormally (no exit code)`}:e.status===0?{success:!0}:{success:!1,error:`Docker exited with code ${e.status}`}}async run(e){if(e.verbose&&e.sandbox){let t=`Verbose mode is not supported with sandbox mode. Use --verbose without --sandbox, or remove --verbose.`;return this.reportError(e.callbacks,t),{iterations:[],totalIterations:0,tasksCompleted:0,finalStatus:`error`,errorMessage:t}}this._isRunning=!0;let t=[],n=0;await this.initProgressFile(e);for(let r=1;r<=e.iterations&&this._isRunning;r++){e.callbacks?.onIterationStart?.(r,e.iterations);let i=await this.buildPrompt(e,r),a=await this.executeIteration(i,r,e.sandbox??!1,e.includeOutput??!1,e.verbose??!1,e.callbacks);if(t.push(a),e.callbacks?.onIterationEnd?.(a),a.status===`complete`)return this.finalize(e,t,n+1,`all_complete`);if(a.status===`blocked`)return this.finalize(e,t,n,`blocked`);a.status===`success`&&n++,r<e.iterations&&e.sleepSeconds>0&&await new Promise(t=>setTimeout(t,e.sleepSeconds*1e3))}return this.finalize(e,t,n,`max_iterations`)}stop(){this._isRunning=!1}async finalize(e,t,n,r){this._isRunning=!1;let i={iterations:t,totalIterations:t.length,tasksCompleted:n,finalStatus:r};return await this.appendFinalSummary(e.progressFile,i),i}reportError(e,t,n=`error`){e?.onError?e.onError(t,n):n===`warning`?this.logger.warn(t):this.logger.error(t)}async initProgressFile(e){await o(t.dirname(e.progressFile),{recursive:!0});let n=[`# TaskGraph Loop Progress`,`# Started: ${new Date().toISOString()}`,`# Preset: ${e.prompt}`,`# Max Iterations: ${e.iterations}`,...e.tag?[`# Tag: ${e.tag}`]:[],``,`---`,``];await a(e.progressFile,`
174
+ `+n.join(`
175
+ `)+`
176
+ `,`utf-8`)}async appendFinalSummary(e,t){await a(e,`
177
+ ---
178
+ # Loop Complete: ${new Date().toISOString()}
179
+ - Total iterations: ${t.totalIterations}
180
+ - Tasks completed: ${t.tasksCompleted}
181
+ - Final status: ${t.finalStatus}
182
+ `,`utf-8`)}isPreset(e){return U(e)}async resolvePrompt(e){if(this.isPreset(e))return H[e];let t=await s(e,`utf-8`);if(!t.trim())throw Error(`Custom prompt file '${e}' is empty`);return t}buildContextHeader(e,t){let n=e.tag?` (tag: ${e.tag})`:``;return`@${e.progressFile} @CLAUDE.md
183
+
184
+ Loop iteration ${t} of ${e.iterations}${n}`}async buildPrompt(e,t){let n=await this.resolvePrompt(e.prompt);return`${this.buildContextHeader(e,t)}\n\n${n}`}parseCompletion(e,t){let n=e.match(/<loop-complete>([^<]*)<\/loop-complete>/i);if(n)return{status:`complete`,message:n[1].trim()};let r=e.match(/<loop-blocked>([^<]*)<\/loop-blocked>/i);return r?{status:`blocked`,message:r[1].trim()}:t===0?{status:`success`}:{status:`error`,message:`Exit code ${t}`}}async executeIteration(e,t,n,r=!1,i=!1,a){let o=Date.now(),s=n?`docker`:`claude`;if(i)return this.executeVerboseIteration(e,t,s,n,r,o,a);let c=_(s,this.buildCommandArgs(e,n,!1),{cwd:this.projectRoot,encoding:`utf-8`,maxBuffer:50*1024*1024,stdio:[`inherit`,`pipe`,`pipe`]});if(c.error){let e=this.formatCommandError(c.error,s,n);return this.reportError(a,e),this.createErrorIteration(t,o,e)}let l=(c.stdout||``)+(c.stderr||``);if(l&&a?.onOutput?.(l),c.status===null){let e=`Command terminated abnormally (no exit code)`;return this.reportError(a,e),this.createErrorIteration(t,o,e)}let{status:u,message:d}=this.parseCompletion(l,c.status);return{iteration:t,status:u,duration:Date.now()-o,message:d,...r&&{output:l}}}executeVerboseIteration(e,t,n,r,i,a,o){let s=this.buildCommandArgs(e,r,!0);return new Promise(e=>{let c=!1,l=t=>{c||(c=!0,e(t))},u=g(n,s,{cwd:this.projectRoot,stdio:[`inherit`,`pipe`,`pipe`]}),d=!1,f=``,p=``,m=e=>{if(e.startsWith(`{`))try{let t=JSON.parse(e);if(!this.isValidStreamEvent(t))return;this.handleStreamEvent(t,o),t.type===`result`&&(f=typeof t.result==`string`?t.result:``)}catch(t){if(e.trim().startsWith(`{`)){let n=`Failed to parse JSON event: ${t instanceof Error?t.message:`Unknown error`}. Line: ${e.substring(0,100)}...`;this.reportError(o,n,`warning`)}}};if(!u.stdout){l(this.createErrorIteration(t,a,`Failed to capture stdout from child process`));return}u.stdout.on(`data`,e=>{try{let t=this.processBufferedLines(p,e.toString(`utf-8`));p=t.remaining;for(let e of t.complete)m(e)}catch(e){this.reportError(o,`Failed to process stdout data: ${e instanceof Error?e.message:`Unknown error`}`,`warning`)}}),u.stdout.on(`end`,()=>{d=!0,p&&=(m(p),``)}),u.stderr?.on(`data`,e=>{let n=e.toString(`utf-8`);o?.onStderr?.(t,n)}),u.on(`error`,e=>{let i=this.formatCommandError(e,n,r);if(this.reportError(o,i),u.stdout?.removeAllListeners(),u.stderr?.removeAllListeners(),!u.killed)try{u.kill(`SIGTERM`)}catch{}l(this.createErrorIteration(t,a,i))}),u.on(`close`,e=>{if(!d&&p&&m(p),e===null){let e=`Command terminated abnormally (no exit code)`;this.reportError(o,e),l(this.createErrorIteration(t,a,e));return}let{status:n,message:r}=this.parseCompletion(f,e);l({iteration:t,status:n,duration:Date.now()-a,message:r,...i&&{output:f}})})})}isValidStreamEvent(e){if(!e||typeof e!=`object`)return!1;let t=e;if(!(`type`in t)||typeof t.type!=`string`)return!1;if(`message`in t&&t.message!==void 0){if(typeof t.message!=`object`||t.message===null)return!1;let e=t.message;if(`content`in e&&!Array.isArray(e.content))return!1}return!0}buildCommandArgs(e,t,n){if(t)return[`sandbox`,`run`,`claude`,`-p`,e];let r=[`-p`,e,`--dangerously-skip-permissions`];return n&&r.push(`--output-format`,`stream-json`,`--verbose`),r}formatCommandError(e,t,n){return e.code===`ENOENT`?n?`Docker is not installed. Install Docker Desktop to use --sandbox mode.`:`Claude CLI is not installed. Install with: npm install -g @anthropic-ai/claude-code`:e.code===`EACCES`?`Permission denied executing '${t}'`:`Failed to execute '${t}': ${e.message}`}createErrorIteration(e,t,n){return{iteration:e,status:`error`,duration:Date.now()-t,message:n}}handleStreamEvent(e,t){if(!(e.type!==`assistant`||!e.message?.content))for(let n of e.message.content)n.type===`text`&&n.text?t?.onText?.(n.text):n.type===`tool_use`&&n.name&&t?.onToolUse?.(n.name)}processBufferedLines(e,t){let n=(e+t).split(`
185
+ `);return{complete:n.slice(0,-1),remaining:n[n.length-1]}}},Ue=class{logger=E(`LoopDomain`);loopService=null;projectRoot;constructor(e){this.projectRoot=e.getProjectRoot()}checkSandboxAuth(){return new W({projectRoot:this.projectRoot}).checkSandboxAuth()}runInteractiveAuth(){return new W({projectRoot:this.projectRoot}).runInteractiveAuth()}async run(e){if(this.loopService?.isRunning)try{this.loopService.stop()}catch(e){this.logger.warn(`Failed to stop previous loop service:`,e)}let t=this.buildConfig(e);return this.loopService=new W({projectRoot:this.projectRoot}),this.loopService.run(t)}stop(){this.loopService&&=(this.loopService.stop(),null)}getIsRunning(){return this.loopService?.isRunning??!1}isPreset(e){return U(e)}async resolvePrompt(e,t){if(U(e))return He(e);if(!t)throw Error(`Custom prompt file requires readFile callback: ${e}`);return t(e)}getAvailablePresets(){return[...Ve]}resolveIterations(e){let{userIterations:t,preset:n,pendingTaskCount:r}=e;return t===void 0?n===`default`&&r!==void 0&&r>0?r:10:t}buildConfig(e){return{iterations:e.iterations??10,prompt:e.prompt??`default`,progressFile:e.progressFile??t.join(this.projectRoot,`.taskgraph`,`progress.txt`),sleepSeconds:e.sleepSeconds??5,tag:e.tag,sandbox:e.sandbox??!1,includeOutput:e.includeOutput??!1,verbose:e.verbose??!1,callbacks:e.callbacks}}},We=class{detectFormat(e){return!e||typeof e!=`object`?`standard`:Object.keys(e).some(e=>e!==`tasks`&&e!==`metadata`)?`legacy`:`standard`}extractTasks(e,t){return e?this.detectFormat(e)===`legacy`?this.extractTasksFromLegacy(e,t):this.extractTasksFromStandard(e):[]}extractTasksFromLegacy(e,t){if(t in e)return e[t]?.tasks||[];let n=Object.keys(e).filter(e=>e!==`tasks`&&e!==`metadata`);return t===`master`&&n.length>0&&e[n[0]]?.tasks||[]}extractTasksFromStandard(e){return e?.tasks||[]}extractMetadata(e,t){return e?this.detectFormat(e)===`legacy`?this.extractMetadataFromLegacy(e,t):this.extractMetadataFromStandard(e):null}extractMetadataFromLegacy(e,t){if(t in e){let n=e[t];return!n?.metadata&&n?.tasks?this.generateMetadataFromTasks(n.tasks,t):n?.metadata||null}let n=Object.keys(e).filter(e=>e!==`tasks`&&e!==`metadata`);if(t===`master`&&n.length>0){let t=n[0],r=e[t];return!r?.metadata&&r?.tasks?this.generateMetadataFromTasks(r.tasks,t):r?.metadata||null}return null}extractMetadataFromStandard(e){return e?.metadata||null}extractTags(e){return e?this.detectFormat(e)===`legacy`?Object.keys(e).filter(e=>e!==`tasks`&&e!==`metadata`):[`master`]:[]}convertToSaveFormat(e,t,n,r){let i=r||`master`,a=this.normalizeTasks(e);return n&&this.detectFormat(n)===`legacy`?this.convertToLegacyFormat(a,t,i):this.convertToStandardFormat(a,t,r)}convertToLegacyFormat(e,t,n){return{[n]:{tasks:e,metadata:{...t,tags:[n]}}}}convertToStandardFormat(e,t,n){return{tasks:e,metadata:{...t,tags:n?[n]:[]}}}normalizeTasks(e){return e.map(e=>({...e,id:String(e.id),dependencies:e.dependencies?.map(e=>String(e))||[],subtasks:e.subtasks?.map(e=>({...e,id:Number(e.id),parentId:String(e.parentId)}))||[]}))}generateMetadataFromTasks(e,t){return{version:`1.0.0`,lastModified:new Date().toISOString(),taskCount:e.length,completedCount:e.filter(e=>e.status===`done`).length,tags:[t]}}};const Ge={stale:1e4,retries:{retries:5,factor:2,minTimeout:100,maxTimeout:1e3},realpath:!1};var Ke=class{writers=new Map;getWriter(e){let t=this.writers.get(e);return t||(t=new ie(e),this.writers.set(e,t)),t}async readJson(e){try{let t=await i.readFile(e,`utf-8`);return JSON.parse(t)}catch(t){throw t.code===`ENOENT`?t:t instanceof SyntaxError?Error(`Invalid JSON in file ${e}: ${t.message}`):Error(`Failed to read file ${e}: ${t.message}`)}}async writeJson(e,t){await this.ensureFileExists(e);let n=null;try{n=await re.lock(e,Ge);let r=JSON.stringify(t,null,2);await this.getWriter(e).write(r)}finally{if(n)try{await n()}catch(t){(process.env.DEBUG||process.env.TASKGRAPH_DEBUG===`true`)&&console.warn(`[WARN] Lock release warning for ${e}: ${t.message}`)}}}async modifyJson(e,t){await this.ensureFileExists(e);let n=null;try{n=await re.lock(e,Ge);let r;try{let t=await i.readFile(e,`utf-8`);r=JSON.parse(t)}catch(t){if(t.code===`ENOENT`)r={};else if(t instanceof SyntaxError){let n=await i.readFile(e,`utf-8`).catch(()=>``);if(n.trim()===``||n.trim()===`{}`)r={};else throw Error(`Corrupted JSON in ${e}: ${t.message}. File contains: ${n.substring(0,100)}...`)}else throw Error(`Failed to read ${e} for modification: ${t.message}`)}let a=await t(r),o=JSON.stringify(a,null,2);await this.getWriter(e).write(o)}finally{if(n)try{await n()}catch(t){(process.env.DEBUG||process.env.TASKGRAPH_DEBUG===`true`)&&console.warn(`[WARN] Lock release warning for ${e}: ${t.message}`)}}}async ensureFileExists(e){let n=t.dirname(e);await i.mkdir(n,{recursive:!0});try{await i.writeFile(e,`{}`,{flag:`wx`})}catch(e){if(e.code!==`EEXIST`)throw e}}async exists(e){try{return await i.access(e,ee.F_OK),!0}catch{return!1}}async getStats(e){return i.stat(e)}async readDir(e){return i.readdir(e)}async ensureDir(e){try{await i.mkdir(e,{recursive:!0})}catch(t){throw Error(`Failed to create directory ${e}: ${t.message}`)}}async deleteFile(e){try{await i.unlink(e)}catch(t){if(t.code!==`ENOENT`)throw Error(`Failed to delete file ${e}: ${t.message}`)}}async moveFile(e,t){try{await i.rename(e,t)}catch(n){throw Error(`Failed to move file from ${e} to ${t}: ${n.message}`)}}async copyFile(e,t){try{await i.copyFile(e,t)}catch(n){throw Error(`Failed to copy file from ${e} to ${t}: ${n.message}`)}}async cleanup(){this.writers.clear()}},qe=class{basePath;tasksDir;tasksFilePath;constructor(e){this.basePath=t.join(e,`.taskgraph`),this.tasksDir=t.join(this.basePath,`tasks`),this.tasksFilePath=t.join(this.tasksDir,`tasks.json`)}getBasePath(){return this.basePath}getTasksDir(){return this.tasksDir}getTasksPath(){return this.tasksFilePath}};const G=E(`ComplexityReportManager`);var Je=class{projectRoot;reportCache=new Map;constructor(e){this.projectRoot=e}getReportPath(e){let t=c.join(this.projectRoot,`.taskgraph`,`reports`),n=e&&e!==`master`?`_${e}`:``;return c.join(t,`task-complexity-report${n}.json`)}async loadReport(e){let t=e||`master`,n=t;if(this.reportCache.has(n))return this.reportCache.get(n);let r=this.getReportPath(e);try{await i.access(r);let e=await i.readFile(r,`utf-8`),a=JSON.parse(e);return!a.meta||!Array.isArray(a.complexityAnalysis)?(G.warn(`Invalid complexity report structure at ${r}, ignoring`),null):(this.reportCache.set(n,a),G.debug(`Loaded complexity report for tag '${t}' with ${a.complexityAnalysis.length} analyses`),a)}catch(e){return e.code===`ENOENT`?(G.debug(`No complexity report found for tag '${t}'`),null):(G.warn(`Failed to load complexity report for tag '${t}': ${e.message}`),null)}}async getComplexityForTask(e,t){let n=await this.loadReport(t);if(!n)return null;let r=n.complexityAnalysis.find(t=>String(t.taskId)===String(e));return r?{complexityScore:r.complexityScore,recommendedSubtasks:r.recommendedSubtasks,expansionPrompt:r.expansionPrompt,complexityReasoning:r.complexityReasoning}:null}async getComplexityForTasks(e,t){let n=new Map,r=await this.loadReport(t);if(!r)return n;let i=new Map;return r.complexityAnalysis.forEach(e=>{i.set(String(e.taskId),e)}),e.forEach(e=>{let t=i.get(String(e));t&&n.set(String(e),{complexityScore:t.complexityScore,recommendedSubtasks:t.recommendedSubtasks,expansionPrompt:t.expansionPrompt,complexityReasoning:t.complexityReasoning})}),n}clearCache(e){e?this.reportCache.delete(e):this.reportCache.clear()}async hasReport(e){let t=this.getReportPath(e);try{return await i.access(t),!0}catch{return!1}}},Ye=class{formatHandler;fileOps;pathResolver;complexityManager;constructor(e){this.formatHandler=new We,this.fileOps=new Ke,this.pathResolver=new qe(e),this.complexityManager=new Je(e)}async initialize(){await this.fileOps.ensureDir(this.pathResolver.getTasksDir())}async close(){await this.fileOps.cleanup()}async getStats(){let e=this.pathResolver.getTasksPath();try{let t=await this.fileOps.getStats(e),n=await this.fileOps.readJson(e),r=this.formatHandler.extractTags(n),i=0,a=r.map(e=>{let r=this.formatHandler.extractTasks(n,e).length;return i+=r,{tag:e,taskCount:r,lastModified:t.mtime.toISOString()}});return{totalTasks:i,totalTags:r.length,lastModified:t.mtime.toISOString(),storageSize:0,tagStats:a}}catch(e){if(e.code===`ENOENT`)return{totalTasks:0,totalTags:0,lastModified:new Date().toISOString(),storageSize:0,tagStats:[]};throw Error(`Failed to get storage stats: ${e.message}`)}}async loadTasks(e,t){let n=this.pathResolver.getTasksPath(),r=e||`master`;try{let e=await this.fileOps.readJson(n),i=this.formatHandler.extractTasks(e,r);return t&&(t.status&&(i=i.filter(e=>e.status===t.status)),t.excludeSubtasks&&(i=i.map(e=>({...e,subtasks:[]})))),await this.enrichTasksWithComplexity(i,r)}catch(e){if(e.code===`ENOENT`)return[];throw Error(`Failed to load tasks: ${e.message}`)}}async loadTask(e,t){let n=await this.loadTasks(t);if(e.includes(`.`)){let[t,r]=e.split(`.`),i=n.find(e=>String(e.id)===t);if(!i||!i.subtasks)return null;let a=i.subtasks.find(e=>String(e.id)===r);if(!a)return null;let o=e=>{let t=String(e);return t.includes(`.`)?t:`${i.id}.${t}`},s=a.dependencies?.map(e=>o(e))??[];return{...a,id:e,title:a.title||`Subtask ${r}`,description:a.description||``,status:a.status||`pending`,priority:a.priority||i.priority||`medium`,dependencies:s,details:a.details||``,testStrategy:a.testStrategy||``,subtasks:[],tags:i.tags||[],assignee:a.assignee||i.assignee,complexity:a.complexity||i.complexity,createdAt:a.createdAt||i.createdAt,updatedAt:a.updatedAt||i.updatedAt,parentTask:{id:i.id,title:i.title,status:i.status},isSubtask:!0}}return n.find(t=>String(t.id)===String(e))||null}async saveTasks(e,t){let n=this.pathResolver.getTasksPath(),r=t||`master`;await this.fileOps.ensureDir(this.pathResolver.getTasksDir());let i={version:`1.0.0`,lastModified:new Date().toISOString(),taskCount:e.length,completedCount:e.filter(e=>e.status===`done`).length,tags:[r]},a=this.normalizeTaskIds(e);await this.fileOps.modifyJson(n,e=>this.formatHandler.detectFormat(e)===`legacy`||Object.keys(e).some(e=>e!==`tasks`&&e!==`metadata`)?(e[r]={tasks:a,metadata:i},e):r===`master`?{tasks:a,metadata:i}:{master:{tasks:e.tasks||[],metadata:e.metadata||i},[r]:{tasks:a,metadata:i}})}normalizeTaskIds(e){return e.map(e=>({...e,id:String(e.id),dependencies:e.dependencies?.map(e=>String(e))||[],subtasks:e.subtasks?.map(e=>({...e,id:Number(e.id),parentId:String(e.parentId)}))||[]}))}async exists(e){let t=this.pathResolver.getTasksPath();return this.fileOps.exists(t)}async getAllTags(){try{let e=this.pathResolver.getTasksPath(),t=await this.fileOps.readJson(e);return this.formatHandler.extractTags(t)}catch(e){if(e.code===`ENOENT`)return[];throw Error(`Failed to get tags: ${e.message}`)}}async loadMetadata(e){let t=this.pathResolver.getTasksPath(),n=e||`master`;try{let e=await this.fileOps.readJson(t);return this.formatHandler.extractMetadata(e,n)}catch(e){if(e.code===`ENOENT`)return null;throw Error(`Failed to load metadata: ${e.message}`)}}async saveMetadata(e,t){let n=await this.loadTasks(t);await this.saveTasks(n,t)}async appendTasks(e,t){let n=[...await this.loadTasks(t),...e];await this.saveTasks(n,t)}async updateTask(e,t,n){let r=await this.loadTasks(n),i=r.findIndex(t=>String(t.id)===String(e));if(i===-1)throw Error(`Task ${e} not found`);let a=r[i],o=t.subtasks;t.subtasks&&a.subtasks&&(o=t.subtasks.map(e=>{let t=a.subtasks?.find(t=>String(t.id)===String(e.id)||e.title&&t.title===e.title);return t?.metadata||e.metadata?{...e,metadata:{...t?.metadata||{},...e.metadata||{}}}:e})),r[i]={...a,...t,...o&&{subtasks:o},id:String(e)},await this.saveTasks(r,n)}async updateTaskStatus(e,t,n){let r=await this.loadTasks(n);if(e.includes(`.`))return this.updateSubtaskStatusInFile(r,e,t,n);let i=r.findIndex(t=>String(t.id)===String(e));if(i===-1)throw Error(`Task ${e} not found`);let a=r[i].status;return a===t?{success:!0,oldStatus:a,newStatus:t,taskId:String(e)}:(r[i]={...r[i],status:t,updatedAt:new Date().toISOString()},await this.saveTasks(r,n),{success:!0,oldStatus:a,newStatus:t,taskId:String(e)})}async updateSubtaskStatusInFile(e,t,n,r){let i=t.split(`.`);if(i.length!==2)throw Error(`Invalid subtask ID format: ${t}. Expected format: parentId.subtaskId`);let[a,o]=i,s=o.trim();if(!/^\d+$/.test(s))throw Error(`Invalid subtask ID: ${s}. Subtask ID must be a positive integer.`);let c=Number(s),l=e.findIndex(e=>String(e.id)===String(a));if(l===-1)throw Error(`Parent task ${a} not found`);let u=e[l],d=u.subtasks.findIndex(e=>e.id===c||String(e.id)===s);if(d===-1)throw Error(`Subtask ${t} not found in parent task ${a}`);let f=u.subtasks[d].status||`pending`;if(f===n)return{success:!0,oldStatus:f,newStatus:n,taskId:t};let p=new Date().toISOString();u.subtasks[d]={...u.subtasks[d],status:n,updatedAt:p};let m=u.subtasks,h=u.status;if(m.length>0){let e=e=>e.status||`pending`,t=t=>{let n=e(t);return n===`done`||n===`completed`},n=m.every(t),r=m.some(t=>e(t)===`in-progress`),i=m.some(t),a=m.every(t=>e(t)===`pending`);n?h=`done`:r||i?h=`in-progress`:a&&(h=`pending`)}return e[l]={...u,...h===u.status?{}:{status:h},updatedAt:p},await this.saveTasks(e,r),{success:!0,oldStatus:f,newStatus:n,taskId:t}}async deleteTask(e,t){let n=await this.loadTasks(t),r=n.filter(t=>String(t.id)!==String(e));if(r.length===n.length)throw Error(`Task ${e} not found`);await this.saveTasks(r,t)}async createTag(e,t){let n=this.pathResolver.getTasksPath();try{await this.fileOps.modifyJson(n,n=>{if(this.formatHandler.detectFormat(n)===`legacy`){if(e in n)throw new O(`Tag ${e} already exists`,D.VALIDATION_ERROR);let r=[];return t?.copyFrom&&t.copyFrom in n&&n[t.copyFrom].tasks&&(r=JSON.parse(JSON.stringify(n[t.copyFrom].tasks))),n[e]={tasks:r,metadata:{created:new Date().toISOString(),updatedAt:new Date().toISOString(),description:t?.description||`Tag created on ${new Date().toLocaleDateString()}`,tags:[e]}},n}else{let r=n.tasks||[],i=n.metadata||{},a=[];return(t?.copyFrom===`master`||!t?.copyFrom)&&(a=JSON.parse(JSON.stringify(r))),{master:{tasks:r,metadata:{...i,tags:[`master`]}},[e]:{tasks:a,metadata:{created:new Date().toISOString(),updatedAt:new Date().toISOString(),description:t?.description||`Tag created on ${new Date().toLocaleDateString()}`,tags:[e]}}}}})}catch(e){throw e.code===`ENOENT`?Error(`Tasks file not found - initialize project first`):e}}async deleteTag(e){let t=this.pathResolver.getTasksPath();try{let n=!1;await this.fileOps.modifyJson(t,t=>{if(this.formatHandler.detectFormat(t)!==`legacy`&&e===`master`)return n=!0,t;if(this.formatHandler.detectFormat(t)===`legacy`){if(e in t)return delete t[e],t;throw Error(`Tag ${e} not found`)}else throw Error(`Tag ${e} not found in standard format`)}),n&&await this.fileOps.deleteFile(t)}catch(t){throw t.code===`ENOENT`?Error(`Tag ${e} not found - file doesn't exist`):t}}async renameTag(e,t){let n=this.pathResolver.getTasksPath();try{await this.fileOps.modifyJson(n,n=>{if(this.formatHandler.detectFormat(n)===`legacy`){if(e in n)return n[t]=n[e],delete n[e],n[t].metadata&&(n[t].metadata.tags=[t]),n;throw Error(`Tag ${e} not found`)}else if(e===`master`){let e=n.tasks||[],r=n.metadata||{};return{[t]:{tasks:e,metadata:{...r,tags:[t]}}}}else throw Error(`Tag ${e} not found in standard format`)})}catch(t){throw t.code===`ENOENT`?Error(`Tag ${e} not found - file doesn't exist`):t}}async copyTag(e,t){let n=await this.loadTasks(e);if(n.length===0)throw Error(`Source tag ${e} not found or has no tasks`);await this.saveTasks(n,t)}async getTagsWithStats(){let e=await this.getAllTags(),t=await this.getActiveTagFromState(),n=await Promise.all(e.map(async e=>{try{let n=await this.loadTasks(e),r={},i=0,a={totalSubtasks:0,subtasksByStatus:{}};n.forEach(e=>{let t=e.status||`pending`;r[t]=(r[t]||0)+1,t===`done`&&i++,e.subtasks&&e.subtasks.length>0&&(a.totalSubtasks+=e.subtasks.length,e.subtasks.forEach(e=>{let t=e.status||`pending`;a.subtasksByStatus[t]=(a.subtasksByStatus[t]||0)+1}))});let o=await this.loadMetadata(e);return{name:e,isCurrent:e===t,taskCount:n.length,completedTasks:i,statusBreakdown:r,subtaskCounts:a.totalSubtasks>0?a:void 0,created:o?.created,description:o?.description}}catch{return{name:e,isCurrent:e===t,taskCount:0,completedTasks:0,statusBreakdown:{}}}}));return{tags:n,currentTag:t,totalTags:n.length}}async getActiveTagFromState(){try{let e=t.join(this.pathResolver.getBasePath(),`state.json`);return(await this.fileOps.readJson(e))?.currentTag||`master`}catch{return`master`}}async watch(e,t){let n=this.pathResolver.getTasksPath(),r=t?.debounceMs??100;if(!await this.fileOps.exists(n))throw new O(`Tasks file not found. Initialize the project first.`,D.NOT_FOUND,{path:n});let i,a=!1,o=y.watch(n,(t,n)=>{a||n&&t===`change`&&(i&&clearTimeout(i),i=setTimeout(()=>{a||e({type:`change`,timestamp:new Date})},r))});return o.on(`error`,t=>{a||e({type:`error`,timestamp:new Date,error:t})}),{unsubscribe:()=>{a=!0,i&&clearTimeout(i),o.close()}}}async enrichTasksWithComplexity(e,t){let n=e.map(e=>e.id),r=await this.complexityManager.getComplexityForTasks(n,t);return r.size===0?e:e.map(e=>{let t=r.get(String(e.id));return t?{...e,complexity:t.complexityScore,recommendedSubtasks:t.recommendedSubtasks,expansionPrompt:t.expansionPrompt,complexityReasoning:t.complexityReasoning}:e})}},Xe=class{static async createFromStorageConfig(e,t){return new Ye(e.basePath||t)}},Ze=class e{id;title;description;status;priority;dependencies;details;testStrategy;subtasks;createdAt;updatedAt;effort;actualEffort;tags;assignee;complexity;recommendedSubtasks;expansionPrompt;complexityReasoning;metadata;constructor(e){this.validate(e),this.id=String(e.id),this.title=e.title,this.description=e.description,this.status=e.status,this.priority=e.priority,this.dependencies=(e.dependencies||[]).map(e=>String(e)),this.details=e.details,this.testStrategy=e.testStrategy,this.subtasks=(e.subtasks||[]).map(e=>({...e,id:String(e.id),parentId:String(e.parentId)})),this.createdAt=e.createdAt,this.updatedAt=e.updatedAt,this.effort=e.effort,this.actualEffort=e.actualEffort,this.tags=e.tags,this.assignee=e.assignee,this.complexity=e.complexity,this.recommendedSubtasks=e.recommendedSubtasks,this.expansionPrompt=e.expansionPrompt,this.complexityReasoning=e.complexityReasoning,this.metadata=e.metadata}validate(e){if(e.id===void 0||e.id===null||typeof e.id!=`string`&&typeof e.id!=`number`)throw new O(`Task ID is required and must be a string or number`,D.VALIDATION_ERROR);if(!e.title||e.title.trim().length===0)throw new O(`Task title is required`,D.VALIDATION_ERROR);if(!e.description||e.description.trim().length===0)throw new O(`Task description is required`,D.VALIDATION_ERROR);if(!this.isValidStatus(e.status))throw new O(`Invalid task status: ${e.status}`,D.VALIDATION_ERROR);if(!this.isValidPriority(e.priority))throw new O(`Invalid task priority: ${e.priority}`,D.VALIDATION_ERROR)}isValidStatus(e){return[`pending`,`in-progress`,`done`,`deferred`,`cancelled`,`blocked`,`review`].includes(e)}isValidPriority(e){return[`low`,`medium`,`high`,`critical`].includes(e)}canComplete(){return this.status===`done`||this.status===`cancelled`||this.status===`blocked`?!1:this.subtasks.every(e=>e.status===`done`||e.status===`cancelled`)}markAsComplete(){if(!this.canComplete())throw new O(`Task cannot be marked as complete`,D.TASK_STATUS_ERROR,{taskId:this.id,currentStatus:this.status,hasIncompleteSubtasks:this.subtasks.some(e=>e.status!==`done`&&e.status!==`cancelled`)});this.status=`done`,this.updatedAt=new Date().toISOString()}hasDependencies(){return this.dependencies.length>0}hasSubtasks(){return this.subtasks.length>0}addSubtask(e){let t=this.subtasks.length+1;this.subtasks.push({...e,id:t,parentId:this.id}),this.updatedAt=new Date().toISOString()}updateStatus(e){if(!this.isValidStatus(e))throw new O(`Invalid status: ${e}`,D.VALIDATION_ERROR);if(this.status===`done`&&e===`pending`)throw new O(`Cannot move completed task back to pending`,D.TASK_STATUS_ERROR);this.status=e,this.updatedAt=new Date().toISOString()}toJSON(){return{id:this.id,title:this.title,description:this.description,status:this.status,priority:this.priority,dependencies:this.dependencies,details:this.details,testStrategy:this.testStrategy,subtasks:this.subtasks,createdAt:this.createdAt,updatedAt:this.updatedAt,effort:this.effort,actualEffort:this.actualEffort,tags:this.tags,assignee:this.assignee,complexity:this.complexity,recommendedSubtasks:this.recommendedSubtasks,expansionPrompt:this.expansionPrompt,complexityReasoning:this.complexityReasoning,metadata:this.metadata}}static fromObject(t){return new e(t)}static fromArray(t){return t.map(t=>new e(t))}},Qe=class{configManager;storage;initialized=!1;logger=E(`TaskService`);constructor(e){this.configManager=e,this.storage=null}async initialize(){if(this.initialized)return;let e=this.configManager.getStorageConfig(),t=this.configManager.getProjectRoot();this.storage=await Xe.createFromStorageConfig(e,t),await this.storage.initialize(),this.initialized=!0}async getTaskList(e={}){let t=this.configManager.getActiveTag(),n=e.tag||t;try{let t=e.filter?.status&&!e.filter.priority&&!e.filter.tags&&!e.filter.assignee&&!e.filter.search&&e.filter.hasSubtasks===void 0,r={};if(t){let t=Array.isArray(e.filter.status)?e.filter.status:[e.filter.status];t.length===1&&(r.status=t[0])}e.includeSubtasks===!1&&(r.excludeSubtasks=!0);let i=await this.storage.loadTasks(n,r),a={};e.includeSubtasks===!1&&(a.excludeSubtasks=!0);let o=r.status===void 0?i:await this.storage.loadTasks(n,a),s=Ze.fromArray(i),c=s;return(e.filter&&!t||e.filter?.status&&Array.isArray(e.filter.status)&&e.filter.status.length>1)&&(c=this.applyFilters(s,e.filter)),{tasks:c.map(e=>e.toJSON()),total:o.length,filtered:c.length,tag:n}}catch(t){throw t instanceof O?t:(this.logger.error(`Failed to get task list`,t),new O(`Failed to get task list`,D.INTERNAL_ERROR,{operation:`getTaskList`,tag:n,hasFilter:!!e.filter},t))}}async getTask(e,t){let n=t||this.getActiveTag();try{return await this.storage.loadTask(String(e),n)}catch(t){throw t instanceof O?t:new O(`Failed to get task ${e}`,D.STORAGE_ERROR,{operation:`getTask`,resource:`task`,taskId:String(e),tag:n},t)}}async getTasksByStatus(e,t){let n=Array.isArray(e)?e:[e];return(await this.getTaskList({tag:t,filter:{status:n}})).tasks}async getTaskStats(e){let t=await this.getTaskList({tag:e,includeSubtasks:!0}),n={total:t.total,byStatus:{},withSubtasks:0,blocked:0};return[`pending`,`in-progress`,`done`,`deferred`,`cancelled`,`blocked`,`review`].forEach(e=>{n.byStatus[e]=0}),t.tasks.forEach(e=>{n.byStatus[e.status]++,e.subtasks&&e.subtasks.length>0&&n.withSubtasks++,e.status===`blocked`&&n.blocked++}),n}async getNextTask(e){let t=(await this.getTaskList({tag:e,filter:{status:[`pending`,`in-progress`,`done`]}})).tasks,n={critical:4,high:3,medium:2,low:1},r=(e,t)=>typeof t==`string`&&t.includes(`.`)?t:`${e}.${t}`,i=new Set;t.forEach(e=>{e.status===`done`&&i.add(String(e.id)),Array.isArray(e.subtasks)&&e.subtasks.forEach(t=>{t.status===`done`&&i.add(`${e.id}.${t.id}`)})});let a=[];if(t.filter(e=>e.status===`in-progress`&&Array.isArray(e.subtasks)).forEach(e=>{e.subtasks.forEach(t=>{let n=(t.status||`pending`).toLowerCase();if(n!==`pending`&&n!==`in-progress`)return;let o=t.dependencies?.map(t=>r(String(e.id),t))??[];(o.length===0||o.every(e=>i.has(String(e))))&&a.push({id:`${e.id}.${t.id}`,title:t.title||`Subtask ${t.id}`,status:t.status||`pending`,priority:t.priority||e.priority||`medium`,dependencies:o,parentId:String(e.id),description:t.description,details:t.details,testStrategy:t.testStrategy,subtasks:[]})})}),a.length>0)return a.sort((e,t)=>{let r=n[e.priority]??2,i=n[t.priority]??2;if(i!==r)return i-r;if(e.dependencies.length!==t.dependencies.length)return e.dependencies.length-t.dependencies.length;let[a,o]=String(e.id).split(`.`).map(Number),[s,c]=String(t.id).split(`.`).map(Number);return a===s?o-c:a-s}),a[0];let o=t.filter(e=>{let t=(e.status||`pending`).toLowerCase();return t!==`pending`&&t!==`in-progress`?!1:(e.dependencies??[]).every(e=>i.has(String(e)))});return o.length===0?null:o.sort((e,t)=>{let r=n[e.priority]??2,i=n[t.priority]??2;if(i!==r)return i-r;let a=(e.dependencies??[]).length,o=(t.dependencies??[]).length;return a===o?Number(e.id)-Number(t.id):a-o})[0]}applyFilters(e,t){return e.filter(e=>{if(t.status&&!(Array.isArray(t.status)?t.status:[t.status]).includes(e.status)||t.priority&&!(Array.isArray(t.priority)?t.priority:[t.priority]).includes(e.priority)||t.tags&&t.tags.length>0&&(!e.tags||!t.tags.some(t=>e.tags?.includes(t)))||t.assignee&&e.assignee!==t.assignee)return!1;if(t.search){let n=t.search.toLowerCase(),r=e.title.toLowerCase().includes(n),i=e.description.toLowerCase().includes(n),a=e.details.toLowerCase().includes(n);if(!r&&!i&&!a)return!1}return!(t.hasSubtasks!==void 0&&e.subtasks.length>0!==t.hasSubtasks)})}getStorage(){return this.storage}getActiveTag(){return this.configManager.getActiveTag()}async setActiveTag(e){await this.configManager.setActiveTag(e)}async updateTask(e,t,n){if(!this.storage)throw new O(`Storage not initialized`,D.STORAGE_ERROR);this.initialized||await this.initialize();let r=n||this.getActiveTag(),i=String(e);try{await this.storage.updateTask(i,t,r)}catch(t){throw t instanceof O?t:new O(`Failed to update task ${e}`,D.STORAGE_ERROR,{operation:`updateTask`,resource:`task`,taskId:i,tag:r},t)}}async updateTaskStatus(e,t,n){if(!this.storage)throw new O(`Storage not initialized`,D.STORAGE_ERROR);let r=n||this.getActiveTag(),i=String(e);try{return await this.storage.updateTaskStatus(i,t,r)}catch(e){throw e instanceof O?e:new O(`Failed to update task status for ${i}`,D.STORAGE_ERROR,{operation:`updateTaskStatus`,resource:`task`,taskId:i,newStatus:t,tag:r},e)}}async getTagsWithStats(){if(!this.storage)throw new O(`Storage not initialized`,D.STORAGE_ERROR);this.initialized||await this.initialize();try{return await this.storage.getTagsWithStats()}catch(e){throw e instanceof O?e:new O(`Failed to get tags with stats`,D.STORAGE_ERROR,{operation:`getTagsWithStats`,resource:`tags`},e)}}async close(){this.storage&&await this.storage.close(),this.initialized=!1}},$e=class{constructor(e){this.taskService=e}async startTask(e,t={}){try{let{parentId:n,subtaskId:r}=this.parseTaskId(e);if(!t.force){let t=await this.checkInProgressConflicts(e);if(!t.canProceed)return{task:null,found:!1,started:!1,error:`Conflicting tasks in progress: ${t.conflictingTasks.map(e=>`#${e.id}: ${e.title}`).join(`, `)}`}}let i=await this.taskService.getTask(n);if(!i)return{task:null,found:!1,started:!1,error:`Task ${n} not found`};let a;if(r&&i.subtasks&&(a=i.subtasks.find(e=>String(e.id)===r)),t.updateStatus&&!t.dryRun)try{await this.taskService.updateTaskStatus(n,`in-progress`)}catch(e){console.warn(`Could not update task status: ${e instanceof Error?e.message:String(e)}`)}let o=!1,s=`Task ready to execute`,c;return t.dryRun?(o=!0,s=`Dry run - task would be executed`,c=await this.prepareExecutionCommand(i,a)):(c=await this.prepareExecutionCommand(i,a),o=!!c,s=c?`Command prepared: ${c.executable} ${c.args.join(` `)}`:`Failed to prepare execution command`),{task:i,found:!0,started:o,subtaskId:r,subtask:a,executionOutput:s,command:c||void 0}}catch(e){return{task:null,found:!1,started:!1,error:e instanceof Error?e.message:String(e)}}}async checkInProgressConflicts(e){let t=(await this.taskService.getTaskList()).tasks.filter(e=>e.status===`in-progress`);if(t.find(t=>t.id===e))return{canProceed:!0,conflictingTasks:[]};let n=e.includes(`.`);if(n){let n=e.split(`.`)[0];if(t.find(e=>e.id===n))return{canProceed:!0,conflictingTasks:[]}}let r=t.filter(t=>{if(t.id===e)return!1;if(n){let n=e.split(`.`)[0];if(t.id===n)return!1}if(t.id.toString().includes(`.`)){let r=t.id.toString().split(`.`)[0];if(n&&r===e.split(`.`)[0])return!1}return!0});return r.length>0?{canProceed:!1,conflictingTasks:r,reason:`Other tasks are already in progress`}:{canProceed:!0,conflictingTasks:[]}}async getNextAvailableTask(){return(await this.taskService.getNextTask())?.id||null}parseTaskId(e){if(e.includes(`.`)){let[t,n]=e.split(`.`);return{parentId:t,subtaskId:n}}return{parentId:e}}async canStartTask(e,t=!1){return t?!0:(await this.checkInProgressConflicts(e)).canProceed}async prepareExecutionCommand(e,t){try{return{executable:`claude`,args:[this.formatTaskPrompt(e,t)],cwd:process.cwd()}}catch(e){return console.warn(`Failed to prepare execution command: ${e instanceof Error?e.message:String(e)}`),null}}formatTaskPrompt(e,t){let n=t||e,r=t?`Subtask`:`Task`,i=`${r} #${t?`${e.id}.${t.id}`:e.id}: ${n.title}\n\n`;return n.description&&(i+=`Description:\n${n.description}\n\n`),n.details&&(i+=`Implementation Details:\n${n.details}\n\n`),n.testStrategy&&(i+=`Test Strategy:\n${n.testStrategy}\n\n`),e.dependencies&&e.dependencies.length>0&&(i+=`Dependencies: ${e.dependencies.join(`, `)}\n\n`),i+=`Please help me implement this ${r.toLowerCase()}.`,i}async getTaskWithSubtask(e){let{parentId:t,subtaskId:n}=this.parseTaskId(e),r=await this.taskService.getTask(t);return r?n&&r.subtasks?{task:r,subtask:r.subtasks.find(e=>String(e.id)===n),subtaskId:n}:{task:r}:{task:null}}},et=`taskgraph-ai`,tt=`0.52.4`;const nt={CLAUDE_CODE:`claude-code`},rt=Object.values(nt);[...rt];const it=`.taskgraph/tasks/tasks.json`,K=[`.taskgraph`,`.taskgraph/config.json`,it],q=`.git,.svn,.hg,.fossil,.github,.gitlab,.circleci,.travis.yml,.jenkins,.buildkite,.vscode,.idea,.project,.devcontainer,package-lock.json,yarn.lock,pnpm-lock.yaml,bun.lockb,bun.lock,deno.lock,deno.json,deno.jsonc,package.json,lerna.json,nx.json,turbo.json,rush.json,pnpm-workspace.yaml,Cargo.toml,Cargo.lock,go.mod,go.sum,go.work,pyproject.toml,setup.py,setup.cfg,poetry.lock,Pipfile,Pipfile.lock,uv.lock,Gemfile,Gemfile.lock,composer.json,composer.lock,build.gradle,build.gradle.kts,settings.gradle,settings.gradle.kts,pom.xml,build.sbt,project.clj,deps.edn,mix.exs,rebar.config,pubspec.yaml,Package.swift,CMakeLists.txt,Makefile,meson.build,BUILD.bazel,WORKSPACE,flake.nix,shell.nix,default.nix,Dockerfile,docker-compose.yml,docker-compose.yaml,Containerfile,kubernetes.yml,kubernetes.yaml,helm/Chart.yaml`.split(`,`),at=[`tasks.json`,...q,`requirements.txt`];[...K,...at];const J=[`pending`,`in-progress`,`done`,`deferred`,`cancelled`,`blocked`,`review`],ot=[`done`,`completed`,`cancelled`];function Y(e){return ot.includes(e)}const st=[`text`,`json`,`compact`],ct={done:`✓`,completed:`✓`,"in-progress":`►`,blocked:`⭕`,pending:`○`,deferred:`⏸`,cancelled:`✗`,review:`👁`},X=E(`TaskLoader`);var lt=class{taskService;constructor(e){if(!e)throw Error(`taskService is required for TaskLoaderService`);this.taskService=e}async loadAndValidateTask(e){X.info(`Loading task ${e}...`);let t=await this.loadTask(e);if(!t)return{success:!1,errorType:`task_not_found`,errorMessage:`Task with ID "${e}" not found`,suggestion:`Use "taskgraph list" to see available tasks or verify the task ID is correct.`};let n=this.validateTaskStatus(t);if(!n.success)return n;let r=this.validateSubtasksExist(t);if(!r.success)return r;let i=this.validateSubtaskStructure(t);if(!i.success)return i;let a=this.validateDependencies(t);return a.success?(X.info(`Task ${e} validated successfully`),{success:!0,task:t}):a}async loadTask(e){try{return await this.taskService.getTask(e)}catch(t){return X.error(`Failed to load task ${e}:`,t),null}}validateTaskStatus(e){return Y(e.status)?{success:!1,errorType:`task_completed`,errorMessage:`Task "${e.title}" is already ${e.status}`,suggestion:`Autopilot can only execute tasks that are pending or in-progress. Use a different task.`}:{success:!0}}validateSubtasksExist(e){return!e.subtasks||e.subtasks.length===0?{success:!1,errorType:`no_subtasks`,errorMessage:`Task "${e.title}" has no subtasks`,suggestion:this.buildExpansionSuggestion(e)}:{success:!0}}buildExpansionSuggestion(e){let t=[`Autopilot requires tasks to be broken down into subtasks for execution.`];return t.push(`
186
+ Expand this task using:`),t.push(` taskgraph expand --id=${e.id}`),e.complexity||e.recommendedSubtasks?(t.push(`
187
+ This task has complexity analysis available. Consider reviewing it first:`),t.push(` taskgraph show ${e.id}`)):(t.push(`
188
+ Or analyze task complexity first to determine optimal subtask count:`),t.push(` taskgraph analyze-complexity --from=${e.id}`)),t.join(`
189
+ `)}validateSubtaskStructure(e){for(let t of e.subtasks){if(!t.title||!t.description)return{success:!1,errorType:`invalid_structure`,errorMessage:`Subtask ${e.id}.${t.id} is missing required fields`,suggestion:`Subtasks must have title and description. Re-expand the task or manually fix the subtask structure.`};if(t.dependencies&&!Array.isArray(t.dependencies))return{success:!1,errorType:`invalid_structure`,errorMessage:`Subtask ${e.id}.${t.id} has invalid dependencies format`,suggestion:`Dependencies must be an array. Fix the task structure manually.`}}return{success:!0}}validateDependencies(e){let t=[],n=new Set(e.subtasks.map(e=>String(e.id)));for(let r of e.subtasks){let i=`${e.id}.${r.id}`;if(r.dependencies&&r.dependencies.length>0)for(let e of r.dependencies){let r=String(e);n.has(r)||t.push({subtaskId:i,issueType:`missing`,message:`References non-existent subtask ${r}`,dependencyRef:r})}let a=this.detectCircularDependency(r,e.subtasks,new Set);a&&t.push({subtaskId:i,issueType:`circular`,message:`Circular dependency detected: ${a.join(` -> `)}`})}return t.length>0?{success:!1,errorType:t[0].issueType===`circular`?`circular_dependencies`:`missing_dependencies`,errorMessage:`Task "${e.title}" has dependency issues`,suggestion:`Fix dependency issues manually or re-expand the task:
190
+ `+t.map(e=>` - ${e.subtaskId}: ${e.message}`).join(`
191
+ `),dependencyIssues:t}:{success:!0}}detectCircularDependency(e,t,n){let r=String(e.id);if(n.has(r))return[r];if(n.add(r),e.dependencies&&e.dependencies.length>0)for(let i of e.dependencies){let e=String(i),a=t.find(t=>String(t.id)===e);if(a){let e=this.detectCircularDependency(a,t,new Set(n));if(e)return[r,...e]}}return null}getExecutionOrder(e){let t=[],n=new Set;for(;t.length<e.subtasks.length;){let r=!1;for(let i of e.subtasks){let e=String(i.id);if(!n.has(e)&&(!i.dependencies||i.dependencies.length===0||i.dependencies.every(e=>n.has(String(e))))){t.push(i),n.add(e),r=!0;break}}if(!r&&t.length<e.subtasks.length){X.warn(`Could not determine complete execution order for task ${e.id}`);for(let r of e.subtasks)n.has(String(r.id))||t.push(r);break}}return t}async cleanup(){}};const Z=E(`PreflightChecker`);var ut=class{projectRoot;constructor(e){if(!e)throw Error(`projectRoot is required for PreflightChecker`);this.projectRoot=e}async detectTestCommand(){try{let e=te(l(this.projectRoot,`package.json`),`utf-8`),t=JSON.parse(e);if(!t.scripts||!t.scripts.test)return{success:!1,message:`No test script found in package.json. Please add a "test" script.`};let n=t.scripts.test;return{success:!0,value:n,message:`Test command: ${n}`}}catch(e){return e.code===`ENOENT`?{success:!1,message:`package.json not found in project root`}:{success:!1,message:`Failed to read package.json: ${e.message}`}}}async checkGitWorkingTree(){try{return await Se(this.projectRoot)?p(`git status --porcelain`,{cwd:this.projectRoot,encoding:`utf-8`,timeout:5e3}).trim().length>0?{success:!1,value:`dirty`,message:`Working tree has uncommitted or untracked changes. Please commit or stash them.`}:{success:!0,value:`clean`,message:`Working tree is clean`}:{success:!1,message:`Not a git repository. Initialize git first.`}}catch(e){return{success:!1,message:`Git check failed: ${e.message}`}}}detectProjectTypes(){let e=[];return b(l(this.projectRoot,`package.json`))&&e.push(`node`),(b(l(this.projectRoot,`requirements.txt`))||b(l(this.projectRoot,`setup.py`))||b(l(this.projectRoot,`pyproject.toml`)))&&e.push(`python`),(b(l(this.projectRoot,`pom.xml`))||b(l(this.projectRoot,`build.gradle`)))&&e.push(`java`),b(l(this.projectRoot,`go.mod`))&&e.push(`go`),b(l(this.projectRoot,`Cargo.toml`))&&e.push(`rust`),b(l(this.projectRoot,`composer.json`))&&e.push(`php`),b(l(this.projectRoot,`Gemfile`))&&e.push(`ruby`),ne(this.projectRoot).some(e=>e.endsWith(`.csproj`)||e.endsWith(`.sln`))&&e.push(`dotnet`),e}getToolsForProjectType(e){return{node:[{command:`node`,args:[`--version`]},{command:`npm`,args:[`--version`]}],python:[{command:`python3`,args:[`--version`]},{command:`pip3`,args:[`--version`]}],java:[{command:`java`,args:[`--version`]}],go:[{command:`go`,args:[`version`]}],rust:[{command:`cargo`,args:[`--version`]}],php:[{command:`php`,args:[`--version`]},{command:`composer`,args:[`--version`]}],ruby:[{command:`ruby`,args:[`--version`]},{command:`bundle`,args:[`--version`]}],dotnet:[{command:`dotnet`,args:[`--version`]}]}[e]||[]}async validateRequiredTools(){let e=[];e.push(this.checkTool(`git`,[`--version`])),e.push(await this.checkGhCli());let t=this.detectProjectTypes();t.length===0?Z.warn(`No recognized project type detected`):Z.info(`Detected project types: ${t.join(`, `)}`);for(let n of t){let t=this.getToolsForProjectType(n);for(let n of t)e.push(this.checkTool(n.command,n.args))}let n=e.every(e=>e.available),r=e.filter(e=>!e.available).map(e=>e.name);return n?{success:!0,value:e,message:`All required tools are available`}:{success:!1,value:e,message:`Missing required tools: ${r.join(`, `)}`}}checkTool(e,t){try{let n=p(`${e} ${t.join(` `)}`,{cwd:this.projectRoot,encoding:`utf-8`,stdio:`pipe`,timeout:5e3}).trim().split(`
192
+ `)[0];return{name:e,available:!0,version:n,message:`${e} ${n}`}}catch{return{name:e,available:!1,message:`${e} not found`}}}async checkGhCli(){try{return{name:`gh`,available:!0,version:p(`gh --version`,{cwd:this.projectRoot,encoding:`utf-8`,stdio:`pipe`,timeout:5e3}).trim().split(`
193
+ `)[0],message:await P(this.projectRoot)?`GitHub CLI installed (authenticated)`:`GitHub CLI installed (not authenticated)`}}catch{return{name:`gh`,available:!1,message:`GitHub CLI not found`}}}async detectDefaultBranch(){try{let e=await Ee(this.projectRoot);return e?{success:!0,value:e,message:`Default branch: ${e}`}:{success:!1,message:`Could not determine default branch. Make sure remote is configured.`}}catch(e){return{success:!1,message:`Failed to detect default branch: ${e.message}`}}}async runAllChecks(){Z.info(`Running preflight checks...`);let e=await this.detectTestCommand(),t=await this.checkGitWorkingTree(),n=await this.validateRequiredTools(),r=await this.detectDefaultBranch(),i=e.success&&t.success&&n.success&&r.success,a=[],o=[];e.success?a.push(`Test command`):o.push(`Test command`),t.success?a.push(`Git working tree`):o.push(`Git working tree`),n.success?a.push(`Required tools`):o.push(`Required tools`),r.success?a.push(`Default branch`):o.push(`Default branch`);let s=a.length+o.length,c=i?`All preflight checks passed (${a.length}/${s})`:`Preflight checks failed: ${o.join(`, `)} (${a.length}/${s} passed)`;return Z.info(c),{success:i,testCommand:e,gitWorkingTree:t,requiredTools:n,defaultBranch:r,summary:c}}};const dt=[`master`];var ft=class{constructor(e){this.storage=e}validateTagName(e,t=`Tag name`){if(!e||typeof e!=`string`)throw new O(`${t} is required and must be a string`,D.VALIDATION_ERROR);if(e.length>50)throw new O(`${t} must be 50 characters or less`,D.VALIDATION_ERROR,{tagName:e,maxLength:50});if(!/^[a-zA-Z0-9_-]+$/.test(e))throw new O(`${t} can only contain letters, numbers, hyphens, and underscores`,D.VALIDATION_ERROR,{tagName:e});if(dt.includes(e.toLowerCase()))throw new O(`"${e}" is a reserved tag name`,D.VALIDATION_ERROR,{tagName:e,reserved:!0})}async createTag(e,t={}){this.validateTagName(e);let n=await this.storage.getAllTags();if(n.includes(e))throw new O(`Tag "${e}" already exists`,D.VALIDATION_ERROR,{tagName:e});if(t.copyFromTag&&!n.includes(t.copyFromTag))throw new O(`Cannot copy from missing tag "${t.copyFromTag}"`,D.NOT_FOUND,{tagName:t.copyFromTag});let r;return t.copyFromTag?r=t.copyFromTag:t.copyFromCurrent&&(r=(await this.storage.getTagsWithStats()).currentTag||void 0),await this.storage.createTag(e,{copyFrom:r,description:t.description}),{name:e,taskCount:0,completedTasks:0,isCurrent:!1,statusBreakdown:{},description:t.description||`Tag created on ${new Date().toLocaleDateString()}`}}async deleteTag(e,t={}){if(this.validateTagName(e),e===`master`)throw new O(`Cannot delete the "master" tag`,D.VALIDATION_ERROR,{tagName:e,protected:!0});if(!(await this.storage.getAllTags()).includes(e))throw new O(`Tag "${e}" does not exist`,D.NOT_FOUND,{tagName:e});await this.storage.deleteTag(e)}async renameTag(e,t){if(this.validateTagName(e,`Old tag name`),this.validateTagName(t,`New tag name`),e===`master`)throw new O(`Cannot rename the "master" tag`,D.VALIDATION_ERROR,{tagName:e,protected:!0});let n=await this.storage.getAllTags();if(!n.includes(e))throw new O(`Tag "${e}" does not exist`,D.NOT_FOUND,{tagName:e});if(n.includes(t))throw new O(`Tag "${t}" already exists`,D.VALIDATION_ERROR,{tagName:t});await this.storage.renameTag(e,t)}async copyTag(e,t,n={}){this.validateTagName(e,`Source tag name`),this.validateTagName(t,`Target tag name`);let r=await this.storage.getAllTags();if(!r.includes(e))throw new O(`Source tag "${e}" does not exist`,D.NOT_FOUND,{tagName:e});if(r.includes(t))throw new O(`Target tag "${t}" already exists`,D.VALIDATION_ERROR,{tagName:t});await this.storage.copyTag(e,t)}async getTagsWithStats(){return await this.storage.getTagsWithStats()}},pt=class{constructor(e,t,n){this.storage=e,this.projectPath=t,this.configManager=n}async generateTaskFiles(e={}){let n=e.tag||this.configManager.getActiveTag(),r=e.outputDir||t.join(this.projectPath,`.taskgraph`,`tasks`);try{await i.mkdir(r,{recursive:!0});let e=await this.storage.loadTasks(n);if(e.length===0)return{success:!0,count:0,directory:r,orphanedFilesRemoved:0};let a=await this.cleanupOrphanedFiles(r,e,n),o={},s=await Promise.allSettled(e.map(async a=>{let o=this.formatTaskContent(a,e),s=this.getTaskFileName(a.id,n),c=t.join(r,s);return await i.writeFile(c,o,`utf-8`),a.id})),c=0;for(let t=0;t<s.length;t++){let n=s[t];if(n.status===`fulfilled`)c++;else{let r=String(e[t].id);o[r]=n.reason?.message||`Unknown error`}}return{success:Object.keys(o).length===0,count:c,directory:r,orphanedFilesRemoved:a,...Object.keys(o).length>0&&{fileErrors:o}}}catch(e){return{success:!1,count:0,directory:r,orphanedFilesRemoved:0,error:e.message}}}getTaskFileName(e,t){let n=String(e).padStart(3,`0`);return t===`master`?`task_${n}.md`:`task_${n}_${t}.md`}escapeRegExp(e){return e.replace(/[.*+?^${}()|[\]\\]/g,`\\$&`)}async cleanupOrphanedFiles(e,n,r){let a=0;try{let o=await i.readdir(e),s=n.map(e=>String(e.id)),c=/^task_(\d+)\.md$/,l=RegExp(`^task_(\\d+)_${this.escapeRegExp(r)}\\.md$`),u=[];for(let e of o){let t=null,n=null;if(r===`master`?(t=e.match(c),t&&(n=t[1])):(t=e.match(l),t&&(n=t[1])),n!==null){let t=String(parseInt(n,10));s.includes(t)||u.push(e)}}await Promise.all(u.map(async n=>{let r=t.join(e,n);await i.unlink(r)})),a=u.length}catch{}return a}formatTaskContent(e,t){let n=`# Task ID: ${e.id}\n\n`;if(n+=`**Title:** ${e.title}\n\n`,n+=`**Status:** ${e.status||`pending`}\n\n`,e.dependencies&&e.dependencies.length>0){let r=this.formatDependenciesWithStatus(e.dependencies,t);n+=`**Dependencies:** ${r}\n\n`}else n+=`**Dependencies:** None
194
+
195
+ `;if(n+=`**Priority:** ${e.priority||`medium`}\n\n`,n+=`**Description:** ${e.description||``}\n\n`,n+=`**Details:**
196
+
197
+ `,n+=`${e.details||`No details provided.`}\n\n`,n+=`**Test Strategy:**
198
+
199
+ `,n+=`${e.testStrategy||`No test strategy provided.`}\n`,e.subtasks&&e.subtasks.length>0){n+=`
200
+ ## Subtasks
201
+ `;for(let t of e.subtasks)n+=this.formatSubtaskContent(t,e)}return n.replace(/\n{3,}/g,`
202
+
203
+ `)}formatSubtaskContent(e,t){let n=`\n### ${t.id}.${e.id}. ${e.title}\n\n`;if(n+=`**Status:** ${e.status||`pending`} \n`,e.dependencies&&e.dependencies.length>0){let r=e.dependencies.map(e=>{let n=String(e);return n.includes(`.`)?n:`${t.id}.${n}`}).join(`, `);n+=`**Dependencies:** ${r} \n`}else n+=`**Dependencies:** None
204
+ `;return n+=`
205
+ `,e.description&&(n+=`${e.description}\n`),e.details&&(n+=`\n**Details:**\n\n${e.details}\n`),n}formatDependenciesWithStatus(e,t){return e.map(e=>{let n=t.find(t=>String(t.id)===String(e));return n?`${e}${this.getStatusSymbol(n.status)}`:String(e)}).join(`, `)}getStatusSymbol(e){switch(e){case`done`:return` ✓`;case`in-progress`:return` ⧖`;case`blocked`:return` ⛔`;case`cancelled`:return` ✗`;case`deferred`:return` ⏸`;default:return``}}},mt=class{taskService;executionService;loaderService;preflightChecker;tagService;taskFileGenerator;projectRoot;configManager;constructor(e){this.projectRoot=e.getProjectRoot(),this.configManager=e,this.taskService=new Qe(e),this.executionService=new $e(this.taskService),this.loaderService=new lt(this.taskService),this.preflightChecker=new ut(this.projectRoot)}async initialize(){await this.taskService.initialize(),this.tagService=new ft(this.taskService.getStorage()),this.taskFileGenerator=new pt(this.taskService.getStorage(),this.projectRoot,this.configManager)}async list(e){return this.taskService.getTaskList(e)}async get(e,t){let n=e.split(`.`),r=n[0],i=n[1],a=await this.taskService.getTask(r,t);if(!a)return{task:null,isSubtask:!1};if(i&&a.subtasks){let e=a.subtasks.find(e=>String(e.id)===i);return e?{task:e,isSubtask:!0}:{task:null,isSubtask:!0}}return{task:a,isSubtask:!1}}async getByStatus(e,t){return this.taskService.getTasksByStatus(e,t)}async getStats(e){return this.taskService.getTaskStats(e)}async getNext(e){return this.taskService.getNextTask(e)}async getCount(e,t){let n=await this.list({tag:t}),r=0;for(let t of n.tasks)if(t.status===e&&r++,t.subtasks&&t.subtasks.length>0)for(let n of t.subtasks)(n.status===e||e===`pending`&&!n.status)&&r++;return r}async update(e,t,n){return this.taskService.updateTask(e,t,n)}async updateStatus(e,t,n){return this.taskService.updateTaskStatus(e,t,n)}async setActiveTag(e){return this.taskService.setActiveTag(e)}async start(e,t){return this.executionService.startTask(e,t)}async checkInProgressConflicts(e){return this.executionService.checkInProgressConflicts(e)}async getNextAvailable(){return this.executionService.getNextAvailableTask()}async canStart(e,t){return this.executionService.canStartTask(e,t)}async loadAndValidate(e){return this.loaderService.loadAndValidateTask(e)}getExecutionOrder(e){return this.loaderService.getExecutionOrder(e)}async runPreflightChecks(){return this.preflightChecker.runAllChecks()}async detectTestCommand(){return this.preflightChecker.detectTestCommand()}async checkGitWorkingTree(){return this.preflightChecker.checkGitWorkingTree()}async validateRequiredTools(){return this.preflightChecker.validateRequiredTools()}async detectDefaultBranch(){return this.preflightChecker.detectDefaultBranch()}async createTag(e,t){return this.tagService.createTag(e,t)}async deleteTag(e,t){return this.tagService.deleteTag(e,t)}async renameTag(e,t){return this.tagService.renameTag(e,t)}async copyTag(e,t,n){return this.tagService.copyTag(e,t,n)}async getTagsWithStats(){return this.tagService.getTagsWithStats()}async watch(e,t){return this.taskService.getStorage().watch(e,t)}async generateTaskFiles(e){return this.taskFileGenerator.generateTaskFiles(e)}async close(){await this.taskService.close()}},ht=class{projectRoot;statePath;backupDir;sessionDir;maxBackups;logger=E(`WorkflowStateManager`);writer=null;writerInitPromise=null;constructor(e,n=5){this.projectRoot=t.resolve(e),this.maxBackups=n;let r=this.getProjectIdentifier(this.projectRoot),i=ae.homedir(),a=t.join(i,`.taskgraph`,r);this.sessionDir=t.join(a,`sessions`),this.statePath=t.join(this.sessionDir,`workflow-state.json`),this.backupDir=t.join(this.sessionDir,`backups`)}getProjectIdentifier(e){return`-`+t.resolve(e).replace(/^\//,``).replace(/[^a-zA-Z0-9]+/g,`-`).replace(/-+/g,`-`).replace(/-+$/,``)}async ensureWriter(){if(!this.writer){if(this.writerInitPromise){await this.writerInitPromise;return}this.writerInitPromise=(async()=>{await i.mkdir(this.sessionDir,{recursive:!0}),this.writer=new ie(this.statePath)})(),await this.writerInitPromise,this.writerInitPromise=null}}async exists(){try{return await i.access(this.statePath),!0}catch{return!1}}async load(){try{let e=await i.readFile(this.statePath,`utf-8`);return JSON.parse(e)}catch(e){throw e.code===`ENOENT`?Error(`Workflow state file not found at ${this.statePath}`):Error(`Failed to load workflow state: ${e.message}`)}}async save(e){try{await this.ensureWriter();let t=JSON.stringify(e,null,2);try{JSON.parse(t)}catch{throw this.logger.error(`Generated invalid JSON:`,t),Error(`Failed to generate valid JSON from workflow state`)}await this.writer.write(t+`
206
+ `),this.logger.debug(`Saved workflow state (${t.length} bytes)`)}catch(e){throw Error(`Failed to save workflow state: ${e.message}`)}}async createBackup(){try{if(!await this.exists())return;let e=await this.load();await i.mkdir(this.backupDir,{recursive:!0});let n=new Date().toISOString().replace(/[:.]/g,`-`),r=t.join(this.backupDir,`workflow-state-${n}.json`),a={timestamp:new Date().toISOString(),state:e};await i.writeFile(r,JSON.stringify(a,null,2),`utf-8`),await this.pruneBackups()}catch(e){throw Error(`Failed to create backup: ${e.message}`)}}async delete(){try{await i.unlink(this.statePath)}catch(e){if(e.code!==`ENOENT`)throw Error(`Failed to delete workflow state: ${e.message}`)}}async listBackups(){try{return(await i.readdir(this.backupDir)).filter(e=>e.startsWith(`workflow-state-`)&&e.endsWith(`.json`)).sort().reverse()}catch(e){if(e.code===`ENOENT`)return[];throw Error(`Failed to list backups: ${e.message}`)}}async restoreBackup(e){try{let n=t.join(this.backupDir,e),r=await i.readFile(n,`utf-8`),a=JSON.parse(r);await this.save(a.state)}catch(e){throw Error(`Failed to restore backup: ${e.message}`)}}async pruneBackups(){try{let e=await this.listBackups();if(e.length>this.maxBackups){let n=e.slice(this.maxBackups);for(let e of n)await i.unlink(t.join(this.backupDir,e))}}catch(e){this.logger.warn(`Failed to prune backups: ${e.message}`)}}getStatePath(){return this.statePath}getBackupDir(){return this.backupDir}getSessionDir(){return this.sessionDir}getProjectRoot(){return this.projectRoot}getActivityLogPath(){return t.join(this.sessionDir,`activity.jsonl`)}},gt=class{currentPhase;context;transitions;eventListeners;persistCallback;autoPersistEnabled=!1;phaseGuards;aborted=!1;testResultValidator;gitOperationHook;executeHook;constructor(e){this.currentPhase=`PREFLIGHT`,this.context={...e},this.transitions=this.defineTransitions(),this.eventListeners=new Map,this.phaseGuards=new Map}defineTransitions(){return[{from:`PREFLIGHT`,to:`BRANCH_SETUP`,event:`PREFLIGHT_COMPLETE`},{from:`BRANCH_SETUP`,to:`SUBTASK_LOOP`,event:`BRANCH_CREATED`},{from:`SUBTASK_LOOP`,to:`FINALIZE`,event:`ALL_SUBTASKS_COMPLETE`},{from:`FINALIZE`,to:`COMPLETE`,event:`FINALIZE_COMPLETE`}]}getCurrentPhase(){return this.currentPhase}getCurrentTDDPhase(){if(this.currentPhase===`SUBTASK_LOOP`)return this.context.currentTDDPhase||`RED`}getContext(){return{...this.context}}async transition(e){if(this.aborted&&e.type!==`ABORT`)throw Error(`Workflow has been aborted`);if(e.type===`ERROR`){this.handleError(e.error),await this.triggerAutoPersist();return}if(e.type===`ABORT`){this.aborted=!0,await this.triggerAutoPersist();return}if(e.type===`RETRY`){this.handleRetry(),await this.triggerAutoPersist();return}if(this.currentPhase===`SUBTASK_LOOP`){await this.handleTDDPhaseTransition(e),await this.triggerAutoPersist();return}let t=this.transitions.find(t=>t.from===this.currentPhase&&t.event===e.type);if(!t)throw Error(`Invalid transition: ${e.type} from ${this.currentPhase}`);this.executeTransition(t,e),await this.triggerAutoPersist()}async handleTDDPhaseTransition(e){let t=this.context.currentTDDPhase||`RED`;switch(e.type){case`RED_PHASE_COMPLETE`:if(t!==`RED`)throw Error(`Invalid transition: RED_PHASE_COMPLETE from non-RED phase`);if(!e.testResults)throw Error(`Test results required for RED phase transition`);if(this.context.lastTestResults=e.testResults,e.testResults.failed===0){this.emit(`tdd:red:completed`),this.emit(`tdd:feature-already-implemented`,{subtaskId:this.getCurrentSubtaskId(),testResults:e.testResults});let t=this.context.subtasks[this.context.currentSubtaskIndex];t&&(t.status=`completed`),this.emit(`subtask:completed`),this.context.currentSubtaskIndex++;let n=this.getProgress();this.emit(`progress:updated`,{completed:n.completed,total:n.total,percentage:n.percentage}),this.context.currentSubtaskIndex<this.context.subtasks.length?(this.context.currentTDDPhase=`RED`,this.emit(`tdd:red:started`),this.emit(`subtask:started`)):await this.transition({type:`ALL_SUBTASKS_COMPLETE`});break}this.emit(`tdd:red:completed`),this.context.currentTDDPhase=`GREEN`,this.emit(`tdd:green:started`);break;case`GREEN_PHASE_COMPLETE`:if(t!==`GREEN`)throw Error(`Invalid transition: GREEN_PHASE_COMPLETE from non-GREEN phase`);if(!e.testResults)throw Error(`Test results required for GREEN phase transition`);if(e.testResults.failed!==0)throw Error(`GREEN phase must have zero failures`);this.context.lastTestResults=e.testResults,this.emit(`tdd:green:completed`),this.context.currentTDDPhase=`COMMIT`,this.emit(`tdd:commit:started`);break;case`COMMIT_COMPLETE`:if(t!==`COMMIT`)throw Error(`Invalid transition: COMMIT_COMPLETE from non-COMMIT phase`);this.emit(`tdd:commit:completed`);let n=this.context.subtasks[this.context.currentSubtaskIndex];n&&(n.status=`completed`);break;case`SUBTASK_COMPLETE`:this.emit(`subtask:completed`),this.context.currentSubtaskIndex++;let r=this.getProgress();this.emit(`progress:updated`,{completed:r.completed,total:r.total,percentage:r.percentage}),this.context.currentSubtaskIndex<this.context.subtasks.length?(this.context.currentTDDPhase=`RED`,this.emit(`tdd:red:started`),this.emit(`subtask:started`)):await this.transition({type:`ALL_SUBTASKS_COMPLETE`});break;case`ALL_SUBTASKS_COMPLETE`:this.emit(`phase:exited`),this.currentPhase=`FINALIZE`,this.context.currentTDDPhase=void 0,this.emit(`phase:entered`);break;default:throw Error(`Invalid transition: ${e.type} in SUBTASK_LOOP`)}}executeTransition(e,t){if(e.guard&&!e.guard(this.context))throw Error(`Guard condition failed for transition to ${e.to}`);let n=this.phaseGuards.get(e.to);if(n&&!n(this.context))throw Error(`Guard condition failed`);this.emit(`phase:exited`),this.updateContext(t),this.currentPhase=e.to,this.emit(`phase:entered`),this.currentPhase===`SUBTASK_LOOP`&&(this.context.currentTDDPhase=`RED`,this.emit(`tdd:red:started`),this.emit(`subtask:started`))}updateContext(e){switch(e.type){case`BRANCH_CREATED`:this.context.branchName=e.branchName,this.emit(`git:branch:created`,{branchName:e.branchName}),this.gitOperationHook&&this.gitOperationHook(`branch:created`,{branchName:e.branchName});break;case`ERROR`:this.context.errors.push(e.error),this.emit(`error:occurred`,{error:e.error});break}}getState(){return{phase:this.currentPhase,context:{...this.context}}}restoreState(e){this.currentPhase=e.phase,this.context={...e.context},this.emit(`workflow:resumed`,{phase:this.currentPhase,progress:this.getProgress()})}on(e,t){this.eventListeners.has(e)||this.eventListeners.set(e,new Set),this.eventListeners.get(e).add(t)}off(e,t){let n=this.eventListeners.get(e);n&&n.delete(t)}emit(e,t){let n={type:e,timestamp:new Date,phase:this.currentPhase,tddPhase:this.context.currentTDDPhase,subtaskId:this.getCurrentSubtaskId(),data:{...t,adapters:{testValidator:!!this.testResultValidator,gitHook:!!this.gitOperationHook,executeHook:!!this.executeHook}}},r=this.eventListeners.get(e);r&&r.forEach(e=>e(n))}getCurrentSubtaskId(){return this.context.subtasks[this.context.currentSubtaskIndex]?.id}onStatePersist(e){this.persistCallback=e}enableAutoPersist(e){this.persistCallback=e,this.autoPersistEnabled=!0}disableAutoPersist(){this.autoPersistEnabled=!1}async persistState(){this.persistCallback&&await this.persistCallback(this.getState()),this.emit(`state:persisted`)}async triggerAutoPersist(){this.autoPersistEnabled&&this.persistCallback&&await this.persistCallback(this.getState())}addGuard(e,t){this.phaseGuards.set(e,t)}removeGuard(e){this.phaseGuards.delete(e)}getCurrentSubtask(){return this.context.subtasks[this.context.currentSubtaskIndex]}getProgress(){let e=this.context.subtasks.filter(e=>e.status===`completed`).length,t=this.context.subtasks.length;return{completed:e,total:t,current:this.context.currentSubtaskIndex+1,percentage:t>0?Math.round(e/t*100):0}}canProceed(){return this.currentPhase===`SUBTASK_LOOP`?this.getCurrentSubtask()?.status===`completed`:!1}incrementAttempts(){let e=this.getCurrentSubtask();e&&e.attempts++}hasExceededMaxAttempts(){let e=this.getCurrentSubtask();return!e||!e.maxAttempts?!1:e.attempts>e.maxAttempts}handleError(e){this.context.errors.push(e),this.emit(`error:occurred`,{error:e})}handleRetry(){this.currentPhase===`SUBTASK_LOOP`&&(this.context.currentTDDPhase=`RED`,this.emit(`tdd:red:started`))}retryCurrentSubtask(){this.currentPhase===`SUBTASK_LOOP`&&(this.context.currentTDDPhase=`RED`,this.emit(`tdd:red:started`))}handleMaxAttemptsExceeded(){let e=this.getCurrentSubtask();e&&(e.status=`failed`,this.emit(`subtask:failed`,{subtaskId:e.id,attempts:e.attempts,maxAttempts:e.maxAttempts}))}isAborted(){return this.aborted}canResumeFromState(e){return!(![`PREFLIGHT`,`BRANCH_SETUP`,`SUBTASK_LOOP`,`FINALIZE`,`COMPLETE`].includes(e.phase)||!e.context||typeof e.context!=`object`||!e.context.taskId||!Array.isArray(e.context.subtasks)||typeof e.context.currentSubtaskIndex!=`number`||!Array.isArray(e.context.errors))}setTestResultValidator(e){this.testResultValidator=e,this.emit(`adapter:configured`,{adapterType:`test-validator`})}hasTestResultValidator(){return!!this.testResultValidator}removeTestResultValidator(){this.testResultValidator=void 0}onGitOperation(e){this.gitOperationHook=e}onExecute(e){this.executeHook=e}executeCommand(e){this.executeHook&&this.executeHook(e,this.context)}};async function _t(e,t){let n={...t,timestamp:new Date().toISOString()};await u.ensureDir(c.dirname(e));let r=JSON.stringify(n)+`
207
+ `;await u.appendFile(e,r,`utf-8`)}const vt=[`workflow:started`,`workflow:completed`,`workflow:error`,`workflow:resumed`,`phase:entered`,`phase:exited`,`tdd:feature-already-implemented`,`tdd:red:started`,`tdd:red:completed`,`tdd:green:started`,`tdd:green:completed`,`tdd:commit:started`,`tdd:commit:completed`,`subtask:started`,`subtask:completed`,`subtask:failed`,`test:run`,`test:passed`,`test:failed`,`git:branch:created`,`git:commit:created`,`error:occurred`,`state:persisted`,`progress:updated`,`adapter:configured`];var yt=class{activityLogPath;orchestrator;logger=E(`WorkflowActivityLogger`);listenerMap=new Map;isActive=!1;constructor(e,t){this.orchestrator=e,this.activityLogPath=t}start(){if(this.isActive){this.logger.warn(`Activity logger is already active`);return}vt.forEach(e=>{let t=e=>this.logEvent(e);this.listenerMap.set(e,t),this.orchestrator.on(e,t)}),this.isActive=!0,this.logger.debug(`Activity logger started, logging to: ${this.activityLogPath}`)}stop(){this.isActive&&(this.listenerMap.forEach((e,t)=>{this.orchestrator.off(t,e)}),this.listenerMap.clear(),this.isActive=!1,this.logger.debug(`Activity logger stopped and listeners removed`))}async logEvent(e){if(this.isActive)try{let t=e.timestamp instanceof Date?e.timestamp.toISOString():new Date(e.timestamp).toISOString(),n={type:e.type,phase:e.phase,tddPhase:e.tddPhase,subtaskId:e.subtaskId,eventTimestamp:t,...e.data||{}};await _t(this.activityLogPath,n)}catch(t){this.logger.error(`Failed to log activity event ${e.type}: ${t.message}`)}}getActivityLogPath(){return this.activityLogPath}isLogging(){return this.isActive}},bt=class{projectRoot;stateManager;taskStatusUpdater;tag;logger=E(`WorkflowService`);orchestrator;activityLogger;constructor(e){typeof e==`string`?this.projectRoot=e:(this.projectRoot=e.projectRoot,this.taskStatusUpdater=e.taskStatusUpdater,this.tag=e.tag),this.stateManager=new ht(this.projectRoot)}async updateTaskStatus(e,t,n){if(this.taskStatusUpdater)try{await this.taskStatusUpdater.updateStatus(e,t,n??this.tag)}catch(t){let n=t instanceof Error?t.message:String(t);this.logger.warn(`Failed to update task ${e} status: ${n}`)}}async hasWorkflow(){return await this.stateManager.exists()}async startWorkflow(e){let{taskId:t,taskTitle:n,subtasks:r,maxAttempts:i=3,force:a,tag:o}=e;if(await this.hasWorkflow()&&!a)throw Error(`Workflow already exists. Use force=true to override or resume existing workflow.`);let s=new A(this.projectRoot);await s.ensureGitRepository(),await s.ensureCleanWorkingTree();let c=r.map(e=>({id:e.id,title:e.title,status:e.status===`done`?`completed`:`pending`,attempts:0,maxAttempts:e.maxAttempts||i})),l=c.findIndex(e=>e.status!==`completed`);if(l===-1)throw Error(`All subtasks for task ${t} are already completed. Nothing to do.`);this.orchestrator=new gt({taskId:t,subtasks:c,currentSubtaskIndex:l,tag:o,errors:[],metadata:{startedAt:new Date().toISOString(),taskTitle:n,resumedFromSubtask:l>0?c[l].id:void 0}}),this.orchestrator.enableAutoPersist(async e=>{await this.stateManager.save(e)}),this.activityLogger=new yt(this.orchestrator,this.stateManager.getActivityLogPath()),this.activityLogger.start(),await this.orchestrator.transition({type:`PREFLIGHT_COMPLETE`});let u=this.generateBranchName(t,n,o);return await s.getCurrentBranch()!==u&&await s.createAndCheckoutBranch(u),await this.orchestrator.transition({type:`BRANCH_CREATED`,branchName:u}),await this.updateTaskStatus(t,`in-progress`,o),this.getStatus()}async resumeWorkflow(){let e=await this.stateManager.load();if(this.orchestrator=new gt(e.context),!this.orchestrator.canResumeFromState(e))throw Error(`Invalid workflow state. State may be corrupted. Consider starting a new workflow.`);return this.orchestrator.restoreState(e),this.orchestrator.enableAutoPersist(async e=>{await this.stateManager.save(e)}),this.activityLogger=new yt(this.orchestrator,this.stateManager.getActivityLogPath()),this.activityLogger.start(),this.getStatus()}getStatus(){if(!this.orchestrator)throw Error(`No active workflow. Start or resume a workflow first.`);let e=this.orchestrator.getContext(),t=this.orchestrator.getProgress(),n=this.orchestrator.getCurrentSubtask();return{taskId:e.taskId,phase:this.orchestrator.getCurrentPhase(),tddPhase:this.orchestrator.getCurrentTDDPhase(),branchName:e.branchName,currentSubtask:n?{id:n.id,title:n.title,attempts:n.attempts,maxAttempts:n.maxAttempts||3}:void 0,progress:t}}getContext(){if(!this.orchestrator)throw Error(`No active workflow. Start or resume a workflow first.`);return this.orchestrator.getContext()}getNextAction(){if(!this.orchestrator)throw Error(`No active workflow. Start or resume a workflow first.`);let e=this.orchestrator.getCurrentPhase(),t=this.orchestrator.getCurrentTDDPhase(),n=this.orchestrator.getCurrentSubtask();if(e===`COMPLETE`)return{action:`workflow_complete`,description:`All subtasks completed`,nextSteps:`All subtasks completed! Review the entire implementation and merge your branch when ready.`,phase:e};if(e===`FINALIZE`)return{action:`finalize_workflow`,description:`Finalize and complete the workflow`,nextSteps:`All subtasks are complete! Use autopilot_finalize to verify no uncommitted changes remain and mark the workflow as complete.`,phase:e};if(e!==`SUBTASK_LOOP`||!t||!n)return{action:`unknown`,description:`Workflow is not in active state`,nextSteps:`Use autopilot_status to check workflow state.`,phase:e};let r={phase:e,tddPhase:t,subtask:{id:n.id,title:n.title}};switch(t){case`RED`:return{...r,action:`generate_test`,description:`Generate failing test for current subtask`,nextSteps:`Write failing tests for subtask ${n.id}: "${n.title}". Create test file(s) that validate the expected behavior. Run tests and use autopilot_complete_phase with results. Note: If all tests pass (0 failures), the feature is already implemented and the subtask will be auto-completed.`};case`GREEN`:return{...r,action:`implement_code`,description:`Implement feature to make tests pass`,nextSteps:`Implement code to make tests pass for subtask ${n.id}: "${n.title}". Write the minimal code needed to pass all tests (GREEN phase), then use autopilot_complete_phase with test results.`};case`COMMIT`:return{...r,action:`commit_changes`,description:`Commit RED-GREEN cycle changes`,nextSteps:`Review and commit your changes for subtask ${n.id}: "${n.title}". Use autopilot_commit to create the commit and advance to the next subtask.`};default:return{...r,action:`unknown`,description:`Unknown TDD phase`,nextSteps:`Use autopilot_status to check workflow state.`}}}async completePhase(e){if(!this.orchestrator)throw Error(`No active workflow. Start or resume a workflow first.`);let t=this.orchestrator.getCurrentTDDPhase();if(!t)throw Error(`Not in active TDD phase`);switch(t){case`RED`:await this.orchestrator.transition({type:`RED_PHASE_COMPLETE`,testResults:e});break;case`GREEN`:await this.orchestrator.transition({type:`GREEN_PHASE_COMPLETE`,testResults:e});break;case`COMMIT`:throw Error(`Cannot complete COMMIT phase with test results. Use commit() instead.`);default:throw Error(`Unknown TDD phase: ${t}`)}return this.getStatus()}async commit(){if(!this.orchestrator)throw Error(`No active workflow. Start or resume a workflow first.`);let e=this.orchestrator.getCurrentTDDPhase();if(e!==`COMMIT`)throw Error(`Cannot commit in ${e} phase. Complete RED and GREEN phases first.`);let t=this.orchestrator.getCurrentSubtask()?.id;await this.orchestrator.transition({type:`COMMIT_COMPLETE`});let n=this.orchestrator.getProgress();if(n.current<n.total?await this.orchestrator.transition({type:`SUBTASK_COMPLETE`}):await this.orchestrator.transition({type:`ALL_SUBTASKS_COMPLETE`}),t){let e=this.orchestrator.getContext();await this.updateTaskStatus(t,`done`,e.tag)}return this.getStatus()}async finalizeWorkflow(){if(!this.orchestrator)throw Error(`No active workflow. Start or resume a workflow first.`);let e=this.orchestrator.getCurrentPhase();if(e!==`FINALIZE`)throw Error(`Cannot finalize workflow in ${e} phase. Complete all subtasks first.`);let t=await new A(this.projectRoot).getStatusSummary();if(!t.isClean)throw Error(`Cannot finalize workflow: working tree has uncommitted changes.\nStaged: ${t.staged}, Modified: ${t.modified}, Deleted: ${t.deleted}, Untracked: ${t.untracked}\nPlease commit all changes before finalizing the workflow.`);let n=this.orchestrator.getContext(),r=n.taskId;await this.orchestrator.transition({type:`FINALIZE_COMPLETE`});let i=this.getStatus();return await this.updateTaskStatus(r,`done`,n.tag),await this.stateManager.delete(),this.orchestrator=void 0,i}async abortWorkflow(){this.orchestrator&&await this.orchestrator.transition({type:`ABORT`}),await this.stateManager.delete(),this.orchestrator=void 0}generateBranchName(e,t,n){let r=t.toLowerCase().replace(/[^a-z0-9]+/g,`-`).replace(/^-+|-+$/g,``).substring(0,50),i=e.replace(/\./g,`-`);return`${n?`tm/${n}`:`tm`}/task-${i}-${r}`}},xt=class{workflowService=null;projectRoot;configManager;tasksDomain=null;constructor(e){this.configManager=e,this.projectRoot=e.getProjectRoot()}setTasksDomain(e){this.tasksDomain=e}getWorkflowService(){if(!this.workflowService){let e=this.configManager.getActiveTag(),t=this.tasksDomain?{updateStatus:async(e,t,n)=>{await this.tasksDomain.updateStatus(e,t,n)}}:void 0;this.workflowService=new bt({projectRoot:this.projectRoot,taskStatusUpdater:t,tag:e})}return this.workflowService}resetWorkflowService(){this.workflowService=null}async start(e){return this.resetWorkflowService(),this.getWorkflowService().startWorkflow(e)}async resume(){return this.resetWorkflowService(),this.getWorkflowService().resumeWorkflow()}getStatus(){return this.getWorkflowService().getStatus()}getContext(){return this.getWorkflowService().getContext()}getNextAction(){return this.getWorkflowService().getNextAction()}async completePhase(e){return this.getWorkflowService().completePhase(e)}async commit(){return this.getWorkflowService().commit()}async finalize(){let e=await this.getWorkflowService().finalizeWorkflow();return this.resetWorkflowService(),e}async abort(){await this.getWorkflowService().abortWorkflow(),this.resetWorkflowService()}async hasWorkflow(){return this.getWorkflowService().hasWorkflow()}},St=class e{_projectPath;_configManager;_logger;_tasks;_workflow;_git;_config;_loop;_github;get tasks(){return this._tasks}get workflow(){return this._workflow}get git(){return this._git}get config(){return this._config}get loop(){return this._loop}get github(){return this._github}get logger(){return this._logger}static async create(t){let n=new e(t);return await n.initialize(),n}_options;constructor(e){if(!e.projectPath)throw new O(`Project path is required`,D.MISSING_CONFIGURATION);if(!t.isAbsolute(e.projectPath))throw new O(`Project path must be an absolute path, received: "${e.projectPath}"`,D.INVALID_INPUT);this._projectPath=t.resolve(e.projectPath),this._options=e}async initialize(){try{this._logger=T(this._options.loggerConfig),this._configManager=await he.create(this._projectPath),this._options.configuration&&await this._configManager.updateConfig(this._options.configuration),this._tasks=new mt(this._configManager),this._workflow=new xt(this._configManager),this._git=new xe(this._projectPath),this._config=new se(this._configManager),this._loop=new Ue(this._configManager),await this._tasks.initialize(),this._workflow.setTasksDomain(this._tasks),this._github=new Be(this._projectPath),this._github.setTasksDomain(this._tasks),this._logger.info(`TmCore initialized successfully`)}catch(e){throw this._logger&&this._logger.error(`Failed to initialize TmCore:`,e),new O(`Failed to initialize TmCore`,D.INTERNAL_ERROR,{operation:`initialize`},e)}}get projectPath(){return this._projectPath}async close(){this._tasks&&await this._tasks.close()}};async function Ct(e){return St.create(e)}function wt(e,n){try{return y.existsSync(t.join(e,n))}catch{return!1}}function Q(e,t){return t.some(t=>wt(e,t))}function Tt(e=process.cwd()){let n=t.resolve(e),r=t.parse(n).root,i=0,a=null;if(Q(n,K)||Q(n,q))return n;let o=t.dirname(n);for(i=1;i<50;){let e=Q(o,K),n=Q(o,q);if(e&&(n||i===1))return o;if(n&&!e){a=o;break}if(o===r)break;let s=t.dirname(o);if(s===o)break;o=s,i++}for(n=a||t.resolve(e),i=0;i<50;){if(Q(n,at))return n;if(n===r)break;let e=t.dirname(n);if(e===n)break;n=e,i++}return t.resolve(e)}function Et(e){if(!e)return``;let n=String(e).split(t.sep),r=n.findIndex(e=>e===`.taskgraph`);return r===-1?String(e):n.slice(0,r).join(t.sep)||t.sep}function Dt(e){let t=e?r(process.cwd(),e):Tt();return{projectRoot:t,tasksPath:n(t,it)}}function Ot(e){return oe(e,`hh:mm:ss a`)}const kt=/^\d+(\.\d+)*$/;function $(e){return kt.test(e)}x.string().min(1,`Task ID cannot be empty`).refine($,{message:`Invalid task ID format. Expected numeric (e.g., '15') or subtask (e.g., '15.2')`});const At=x.string().min(1,`Task ID(s) cannot be empty`).refine(e=>{let t=e.split(`,`).map(e=>e.trim()).filter(e=>e.length>0);return t.length>0&&t.every($)},{message:`Invalid task ID format. Expected numeric (e.g., '15') or subtask (e.g., '15.2'). Multiple IDs should be comma-separated.`});function jt(e){let t=e.split(`,`).map(e=>e.trim()).filter(e=>e.length>0);if(t.length===0)throw Error(`No valid task IDs provided`);let n=t.filter(e=>!$(e));if(n.length>0)throw Error(`Invalid task ID format: ${n.join(`, `)}. Expected numeric (e.g., '15') or subtask (e.g., '15.2')`);return t}const Mt=/^\d+$/,Nt=/^\d+\.\d+$/;function Pt(e){if(!e)return!1;let t=e.trim();return Mt.test(t)||Nt.test(t)}function Ft(e){return e?Mt.test(e.trim()):!1}const It=x.string().trim().refine(Pt,{message:`Invalid task ID format. Expected numeric ("1") or subtask ("1.2")`}),Lt=x.string().trim().refine(Pt,{message:`Invalid task ID format. Expected numeric ("1")`}).refine(Ft,{message:`Subtask IDs are not allowed. Please provide a main task ID (e.g., "1")`}),Rt=E(`TaskFilters`),zt=[`pending`,`in-progress`,`review`];function Bt(e){let t=new Map(e.map(e=>[String(e.id),[]])),n=[];for(let r of e)for(let e of r.dependencies??[]){let i=String(e),a=t.get(i);a?a.push(String(r.id)):n.push({taskId:String(r.id),depId:i})}return{blocksMap:t,invalidDependencies:n}}function Vt(e){let t=new Set(e.filter(e=>Y(e.status)).map(e=>String(e.id)));return e.filter(e=>(J.includes(e.status)||Rt.warn(`Task ${e.id} has unexpected status "${e.status}". Valid statuses are: ${J.join(`, `)}`),zt.includes(e.status)?(e.dependencies??[]).every(e=>t.has(String(e))):!1))}function Ht(e){return e.filter(e=>e.blocks.length>0)}export{A as C,M as S,nt as _,It as a,tt as b,Ot as c,Et as d,Ct as f,Y as g,J as h,Lt as i,Dt as l,ct as m,Ht as n,jt as o,st as p,Vt as r,At as s,Bt as t,Tt as u,rt as v,Ve as x,et as y};
@@ -0,0 +1 @@
1
+ import"./ai-services-unified-CL8dbLYJ.js";import"./src-kk3jUMbL.js";import"./config-manager-BTO7V7yI.js";import"./git-utils-CrYiSKu0.js";import{$ as e,Q as t,X as n,Z as r,et as i,it as a,nt as o,rt as s,tt as c}from"./dependency-manager-Mm62BpiN.js";import"./response-language-CAUwZ_BR.js";export{n as copyTag,r as createTag,t as createTagFromBranch,e as deleteTag,i as renameTag,c as switchCurrentTag,o as tags,s as updateBranchTagMapping,a as useTag};
@@ -1 +1 @@
1
- import"./ai-services-unified-C_rt4w5B.js";import"./src-DUT6aK7P.js";import{J as e,st as t}from"./config-manager-ze5VncSH.js";import"./git-utils-CrYiSKu0.js";import{A as n,B as r,C as i,D as a,E as o,F as s,H as c,I as l,L as u,M as d,N as f,O as p,R as m,S as h,T as g,U as _,V as v,at as y,j as b,k as x,w as S,x as C,z as w}from"./dependency-manager-BPBdmQcF.js";import{t as T}from"./response-language-8XZaALbD.js";export{_ as addSubtask,c as addTask,v as analyzeTaskComplexity,r as clearSubtasks,m as expandAllTasks,w as expandTask,y as findNextTask,e as findTaskById,u as isTaskDependentOn,l as listTasks,s as moveTask,f as parsePRD,x as performResearch,t as readComplexityReport,d as removeSubtask,n as removeTask,o as scopeDownTask,a as scopeUpTask,T as setResponseLanguage,S as setTaskStatus,b as taskExists,g as updateSingleTaskStatus,i as updateSubtaskById,h as updateTaskById,C as updateTasks,p as validateStrength};
1
+ import"./ai-services-unified-CL8dbLYJ.js";import"./src-kk3jUMbL.js";import{J as e,st as t}from"./config-manager-BTO7V7yI.js";import"./git-utils-CrYiSKu0.js";import{A as n,B as r,C as i,D as a,E as o,F as s,H as c,I as l,L as u,M as d,N as f,O as p,R as m,S as h,T as g,U as _,V as v,at as y,j as b,k as x,w as S,x as C,z as w}from"./dependency-manager-Mm62BpiN.js";import{t as T}from"./response-language-CAUwZ_BR.js";export{_ as addSubtask,c as addTask,v as analyzeTaskComplexity,r as clearSubtasks,m as expandAllTasks,w as expandTask,y as findNextTask,e as findTaskById,u as isTaskDependentOn,l as listTasks,s as moveTask,f as parsePRD,x as performResearch,t as readComplexityReport,d as removeSubtask,n as removeTask,o as scopeDownTask,a as scopeUpTask,T as setResponseLanguage,S as setTaskStatus,b as taskExists,g as updateSingleTaskStatus,i as updateSubtaskById,h as updateTaskById,C as updateTasks,p as validateStrength};
package/dist/taskgraph.js CHANGED
@@ -1,2 +1,2 @@
1
1
  #!/usr/bin/env node
2
- import{u as e}from"./src-DUT6aK7P.js";import t from"chalk";import{join as n}from"node:path";import r from"dotenv";function i(e,n=t){e.NO_COLOR&&e.FORCE_COLOR===void 0&&(e.FORCE_COLOR=`0`,n.level=0)}i(process.env);const a=process.cwd(),o=e();r.config({path:n(o,`.env`)}),process.env.TASKGRAPH_ORIGINAL_CWD=a,process.env.DEBUG===`1`&&console.error(`DEBUG - dev.js received args:`,process.argv.slice(2));const{runCLI:s}=await import(`./commands-REN-S3Y5.js`);s(process.argv);export{};
2
+ import{u as e}from"./src-kk3jUMbL.js";import t from"chalk";import{join as n}from"node:path";import r from"dotenv";function i(e,n=t){e.NO_COLOR&&e.FORCE_COLOR===void 0&&(e.FORCE_COLOR=`0`,n.level=0)}i(process.env);const a=process.cwd(),o=e();r.config({path:n(o,`.env`)}),process.env.TASKGRAPH_ORIGINAL_CWD=a,process.env.DEBUG===`1`&&console.error(`DEBUG - dev.js received args:`,process.argv.slice(2));const{runCLI:s}=await import(`./commands-B49KHl_a.js`);s(process.argv);export{};
@@ -0,0 +1 @@
1
+ import"./ai-services-unified-CL8dbLYJ.js";import"./src-kk3jUMbL.js";import"./config-manager-BTO7V7yI.js";import"./git-utils-CrYiSKu0.js";import{C as e}from"./dependency-manager-Mm62BpiN.js";import"./response-language-CAUwZ_BR.js";export{e as default};
@@ -0,0 +1 @@
1
+ import"./ai-services-unified-CL8dbLYJ.js";import"./src-kk3jUMbL.js";import"./config-manager-BTO7V7yI.js";import"./git-utils-CrYiSKu0.js";import{S as e}from"./dependency-manager-Mm62BpiN.js";import"./response-language-CAUwZ_BR.js";export{e as default};
@@ -1 +1 @@
1
- import"./src-DUT6aK7P.js";import{$ as e,B as t,G as n,H as r,J as i,K as a,Q as o,U as s,V as c,W as l,X as u,Y as d,Z as f,at as p,ct as m,dt as h,et as g,ft as _,ht as v,it as y,lt as b,mt as x,nt as S,ot as C,pt as w,q as T,rt as E,st as D,tt as O,ut as k,z as A}from"./config-manager-ze5VncSH.js";import"./git-utils-CrYiSKu0.js";export{A as LOG_LEVELS,t as addComplexityToTask,c as aggregateTelemetry,r as createStateJson,s as disableSilentMode,l as enableSilentMode,n as ensureTagMetadata,a as findCycles,T as findProjectRoot,i as findTaskById,d as findTaskInComplexityReport,u as flattenTasksWithSubtasks,f as formatTaskId,o as getCurrentTag,e as getTasksForTag,g as isEmpty,O as isSilentMode,S as log,E as markMigrationForNotice,y as migrateConfigJson,p as normalizeTaskIds,C as performCompleteTagMigration,D as readComplexityReport,m as readJSON,b as resolveEnvVariable,k as resolveTag,h as taskExists,_ as traverseDependencies,w as truncate,x as withFileLockSync,v as writeJSON};
1
+ import"./src-kk3jUMbL.js";import{$ as e,B as t,G as n,H as r,J as i,K as a,Q as o,U as s,V as c,W as l,X as u,Y as d,Z as f,at as p,ct as m,dt as h,et as g,ft as _,ht as v,it as y,lt as b,mt as x,nt as S,ot as C,pt as w,q as T,rt as E,st as D,tt as O,ut as k,z as A}from"./config-manager-BTO7V7yI.js";import"./git-utils-CrYiSKu0.js";export{A as LOG_LEVELS,t as addComplexityToTask,c as aggregateTelemetry,r as createStateJson,s as disableSilentMode,l as enableSilentMode,n as ensureTagMetadata,a as findCycles,T as findProjectRoot,i as findTaskById,d as findTaskInComplexityReport,u as flattenTasksWithSubtasks,f as formatTaskId,o as getCurrentTag,e as getTasksForTag,g as isEmpty,O as isSilentMode,S as log,E as markMigrationForNotice,y as migrateConfigJson,p as normalizeTaskIds,C as performCompleteTagMigration,D as readComplexityReport,m as readJSON,b as resolveEnvVariable,k as resolveTag,h as taskExists,_ as traverseDependencies,w as truncate,x as withFileLockSync,v as writeJSON};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "taskgraph-ai",
3
- "version": "0.52.3",
3
+ "version": "0.52.4",
4
4
  "description": "Task management for AI-driven development, built for Claude Code: plan from a PRD, then work tasks one at a time with slash commands, a CLI or an MCP server.",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -26,6 +26,7 @@
26
26
  "turbo:test:integration": "turbo test:integration",
27
27
  "build:build-config": "npm run build -w @tm/build-config",
28
28
  "test": "cross-env NODE_ENV=test node --experimental-vm-modules node_modules/.bin/jest",
29
+ "test:vitest": "cross-env NODE_ENV=test vitest run -c vitest.root.config.ts",
29
30
  "test:sdk-contract": "cross-env NODE_ENV=test node --experimental-vm-modules node_modules/.bin/jest tests/sdk-contract --testPathIgnorePatterns /node_modules/",
30
31
  "test:unit": "node --experimental-vm-modules node_modules/.bin/jest --testPathPattern=unit",
31
32
  "test:integration": "node --experimental-vm-modules node_modules/.bin/jest --testPathPattern=integration",
@@ -73,25 +74,19 @@
73
74
  "cli-progress": "^3.12.0",
74
75
  "cli-table3": "^0.6.5",
75
76
  "commander": "^12.1.0",
76
- "cors": "^2.8.6",
77
77
  "date-fns": "^4.4.0",
78
78
  "dotenv": "^18.0.7",
79
- "express": "^4.21.2",
80
79
  "fastmcp": "^4.22.4",
81
80
  "figlet": "^1.12.0",
82
81
  "fs-extra": "^11.4.1",
83
82
  "fuse.js": "^7.5.0",
84
83
  "gpt-tokens": "^1.3.14",
85
84
  "gradient-string": "^3.0.0",
86
- "helmet": "^8.3.0",
87
85
  "inquirer": "^12.5.0",
88
- "jsonc-parser": "^3.3.1",
89
86
  "jsonrepair": "^3.15.0",
90
- "jsonwebtoken": "^9.0.3",
91
87
  "lru-cache": "^10.2.0",
92
88
  "marked": "^15.0.12",
93
89
  "marked-terminal": "^7.3.0",
94
- "open": "^10.2.0",
95
90
  "ora": "^9.4.1",
96
91
  "proper-lockfile": "^4.1.2",
97
92
  "simple-git": "^4.0.2",
@@ -99,7 +94,6 @@
99
94
  "terminal-link": "^5.0.0",
100
95
  "turndown": "^7.2.4",
101
96
  "undici": "^8.11.2",
102
- "uuid": "^14.0.3",
103
97
  "zod": "^4.1.12"
104
98
  },
105
99
  "optionalDependencies": {
@@ -143,13 +137,11 @@
143
137
  "@vitest/coverage-v8": "^4.1.0",
144
138
  "concurrently": "^10.0.6",
145
139
  "cross-env": "^10.0.0",
146
- "execa": "^8.0.1",
147
140
  "jest": "^30.5.2",
148
141
  "jest-environment-node": "^30.5.2",
149
142
  "js-yaml": "^4.1.1",
150
143
  "mock-fs": "^5.5.0",
151
144
  "prettier": "^3.9.10",
152
- "supertest": "^7.3.1",
153
145
  "ts-jest": "^29.4.14",
154
146
  "tsdown": "^0.15.2",
155
147
  "tsx": "^4.23.15",
@@ -1 +0,0 @@
1
- import{a as e,i as t,n,r,t as i}from"./ai-services-unified-C_rt4w5B.js";import"./src-DUT6aK7P.js";import"./config-manager-ze5VncSH.js";import"./git-utils-CrYiSKu0.js";export{i as generateObjectService,n as generateTextService,r as logAiUsage,t as streamObjectService,e as streamTextService};
@@ -1 +0,0 @@
1
- import"./ai-services-unified-C_rt4w5B.js";import"./src-DUT6aK7P.js";import"./config-manager-ze5VncSH.js";import"./git-utils-CrYiSKu0.js";import{k as e}from"./dependency-manager-BPBdmQcF.js";import"./response-language-8XZaALbD.js";export{e as performResearch};
@@ -1 +0,0 @@
1
- import"./src-DUT6aK7P.js";import"./config-manager-ze5VncSH.js";import"./git-utils-CrYiSKu0.js";import{t as e}from"./response-language-8XZaALbD.js";export{e as default};