{"version":3,"file":"working-context.d.ts","sourceRoot":"","sources":["../../src/core/working-context.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,eAAe,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAC;AAChG,OAAO,KAAK,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AAOpD;;;GAGG;AACH,MAAM,WAAW,2BAA2B;IAC3C,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,EAAE,MAAM,CAAC;IACnB,UAAU,EAAE,MAAM,EAAE,CAAC;IACrB,gEAAgE;IAChE,WAAW,EAAE,IAAI,CAAC;IAClB,mDAAmD;IACnD,KAAK,EAAE,8BAA8B,CAAC;CACtC;AAED;;;GAGG;AACH,MAAM,WAAW,yBAAyB;IACzC,KAAK,EAAE,MAAM,CAAC;IACd,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,8DAA8D;IAC9D,WAAW,EAAE,IAAI,CAAC;IAClB,mDAAmD;IACnD,KAAK,EAAE,8BAA8B,CAAC;CACtC;AAED;;;GAGG;AACH,MAAM,WAAW,qCAAqC;IACrD,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,aAAa,CAAC;IACpB,MAAM,EAAE,OAAO,CAAC,eAAe,EAAE,OAAO,GAAG,SAAS,CAAC,CAAC;IACtD,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,YAAY,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,MAAM,WAAW,kCAAkC;IAClD,WAAW,EAAE,MAAM,CAAC;IACpB,cAAc,EAAE,MAAM,CAAC;IACvB,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,MAAM,EAAE,CAAC;IACvB,cAAc,EAAE,qCAA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B,CAkBhG;AAED;;GAEG;AACH,wBAAgB,mBAAmB,CAAC,OAAO,EAAE;IAC5C,WAAW,EAAE,SAAS,UAAU,EAAE,CAAC;IACnC,KAAK,EAAE,aAAa,CAAC;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,UAAU,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IAC9E,KAAK,EAAE,aAAa,CAAC,IAAI,CAAC,CAAC;IAC3B,oBAAoB,EAAE,oBAAoB,CAAC;CAC3C,GAAG,cAAc,CASjB","sourcesContent":["/**\n * Shared Working Context Contract.\n *\n * Provides a bounded, serializable surface for the operator-visible working context:\n * - Memory: persisted session state (snapshot-based, current-branch)\n * - Todo: persisted session state (current branch)\n * - Delegated work: ephemeral current-process state (explicitly not persisted)\n *\n * This contract is the single source of truth for building working-context data\n * shared between interactive panel, /session output, and RPC get_working_context.\n */\n\nimport type { DelegatedMode, DelegatedStatus, DelegatedWorkSummary } from \"./delegated-work.js\";\nimport type { MemoryItem, Task } from \"./memory.js\";\nimport { reviewMemoryItems } from \"./memory-review.js\";\n\n// ============================================================================\n// Structured Summary Types\n// ============================================================================\n\n/**\n * Memory summary with explicit provenance marker.\n * Memory is persisted in session snapshots on the current branch.\n */\nexport interface WorkingContextMemorySummary {\n\titemCount: number;\n\tstaleCount: number;\n\tkeyPreview: string[];\n\t/** Explicit marker: memory is persisted in session snapshots */\n\tisPersisted: true;\n\t/** Explicit scope: current branch session state */\n\tscope: \"current_branch_session_state\";\n}\n\n/**\n * Todo summary with explicit provenance marker.\n * Todos are persisted in session entries on the current branch.\n */\nexport interface WorkingContextTodoSummary {\n\ttotal: number;\n\tcompleted: number;\n\tinProgress?: string;\n\t/** Explicit marker: todos are persisted in session entries */\n\tisPersisted: true;\n\t/** Explicit scope: current branch session state */\n\tscope: \"current_branch_session_state\";\n}\n\n/**\n * Delegated work summary with explicit provenance marker.\n * Delegated work is live current-process runtime state, not persisted.\n */\nexport interface WorkingContextDelegatedFailurePreview {\n\tagent: string;\n\ttask: string;\n\tmode: DelegatedMode;\n\tstatus: Extract<DelegatedStatus, \"error\" | \"blocked\">;\n\tchildIndex?: number;\n\tstep?: number;\n\texitCode?: number;\n\terrorMessage?: string;\n}\n\nexport interface WorkingContextDelegatedWorkSummary {\n\tactiveCount: number;\n\tcompletedCount: number;\n\tfailedCount: number;\n\tactiveAgents: string[];\n\tfailurePreview: WorkingContextDelegatedFailurePreview[];\n\t/** Explicit marker: delegated work is ephemeral current-process state */\n\tisPersisted: false;\n\t/** Explicit scope: current process runtime state */\n\tscope: \"current_process_runtime_state\";\n\tnote: \"live current-process state only; not persisted and resets on session switch/resume\";\n}\n\n/**\n * Task summary with explicit provenance marker.\n * Tasks are persisted in session entries on the current branch.\n */\nexport interface WorkingContextTaskSummary {\n\ttotal: number;\n\tpending: number;\n\tinProgress: number;\n\tcompleted: number;\n\t/** The task currently marked in_progress, if any */\n\tinProgressTask?: { id: string; subject: string; activeForm?: string };\n\t/** Explicit marker: tasks are persisted in session entries */\n\tisPersisted: true;\n\t/** Explicit scope: current branch session state */\n\tscope: \"current_branch_session_state\";\n}\n\n/**\n * Aggregated working context surface.\n * This is always a current-state summary, not a history surface.\n */\nexport interface WorkingContext {\n\t/** Memory summary persisted in current-branch session snapshots. */\n\tmemory: WorkingContextMemorySummary;\n\t/** Todo summary persisted in current-branch session entries. */\n\ttodo: WorkingContextTodoSummary;\n\t/** Task summary persisted in current-branch session entries. */\n\ttasks: WorkingContextTaskSummary;\n\t/** Delegated work summary from live current-process runtime state. */\n\tdelegatedWork: WorkingContextDelegatedWorkSummary;\n}\n\n// ============================================================================\n// Builder Functions\n// ============================================================================\n\n/**\n * Build a memory summary from memory items.\n */\nexport function buildWorkingContextMemorySummary(items: readonly MemoryItem[]): WorkingContextMemorySummary {\n\tconst staleCount = reviewMemoryItems([...items]).filter((entry) => entry.reviewRecommended).length;\n\n\treturn {\n\t\titemCount: items.length,\n\t\tstaleCount,\n\t\tkeyPreview: items.slice(0, 4).map((item) => item.key),\n\t\tisPersisted: true,\n\t\tscope: \"current_branch_session_state\",\n\t};\n}\n\n/**\n * Build a todo summary from todo items.\n */\nexport function buildWorkingContextTodoSummary(\n\titems: ReadonlyArray<{ content: string; activeForm: string; status: string }>,\n): WorkingContextTodoSummary {\n\treturn {\n\t\ttotal: items.length,\n\t\tcompleted: items.filter((todo) => todo.status === \"completed\").length,\n\t\tinProgress: items.find((todo) => todo.status === \"in_progress\")?.activeForm,\n\t\tisPersisted: true,\n\t\tscope: \"current_branch_session_state\",\n\t};\n}\n\nfunction summarizeFailureMessage(message: string | undefined): string | undefined {\n\tif (!message) {\n\t\treturn undefined;\n\t}\n\n\tconst normalized = message.replace(/\\s+/gu, \" \").trim();\n\tif (normalized.length === 0) {\n\t\treturn undefined;\n\t}\n\n\treturn normalized.length > 160 ? `${normalized.slice(0, 160)}...` : normalized;\n}\n\n/**\n * Build a delegated work summary from the session's delegated work state.\n */\nexport function buildWorkingContextDelegatedWorkSummary(\n\tsummary: DelegatedWorkSummary,\n): WorkingContextDelegatedWorkSummary {\n\tconst failurePreview = [...summary.failed]\n\t\t.sort((left, right) => right.timestamp - left.timestamp)\n\t\t.slice(0, 3)\n\t\t.map((task) => ({\n\t\t\tagent: task.agent,\n\t\t\ttask: task.task,\n\t\t\tmode: task.mode,\n\t\t\tstatus: (task.status === \"blocked\" ? \"blocked\" : \"error\") as Extract<DelegatedStatus, \"error\" | \"blocked\">,\n\t\t\tchildIndex: task.childIndex,\n\t\t\tstep: task.step,\n\t\t\texitCode: task.exitCode,\n\t\t\terrorMessage: summarizeFailureMessage(task.errorMessage),\n\t\t}));\n\n\treturn {\n\t\tactiveCount: summary.active.length,\n\t\tcompletedCount: summary.completed.length,\n\t\tfailedCount: summary.failed.length,\n\t\tactiveAgents: Array.from(new Set(summary.active.map((task) => task.agent))),\n\t\tfailurePreview,\n\t\tisPersisted: false,\n\t\tscope: \"current_process_runtime_state\",\n\t\tnote: \"live current-process state only; not persisted and resets on session switch/resume\",\n\t};\n}\n\n/**\n * Build a task summary from task items.\n */\nexport function buildWorkingContextTaskSummary(tasks: readonly Task[]): WorkingContextTaskSummary {\n\tconst pending = tasks.filter((t) => t.status === \"pending\");\n\tconst inProgress = tasks.filter((t) => t.status === \"in_progress\");\n\tconst completed = tasks.filter((t) => t.status === \"completed\");\n\tconst inProgressTask = inProgress[0];\n\n\treturn {\n\t\ttotal: tasks.length,\n\t\tpending: pending.length,\n\t\tinProgress: inProgress.length,\n\t\tcompleted: completed.length,\n\t\tinProgressTask:\n\t\t\tinProgressTask !== undefined\n\t\t\t\t? { id: inProgressTask.id, subject: inProgressTask.subject, activeForm: inProgressTask.activeForm }\n\t\t\t\t: undefined,\n\t\tisPersisted: true,\n\t\tscope: \"current_branch_session_state\",\n\t};\n}\n\n/**\n * Build the complete working context from session state.\n */\nexport function buildWorkingContext(options: {\n\tmemoryItems: readonly MemoryItem[];\n\ttodos: ReadonlyArray<{ content: string; activeForm: string; status: string }>;\n\ttasks: ReadonlyArray<Task>;\n\tdelegatedWorkSummary: DelegatedWorkSummary;\n}): WorkingContext {\n\tconst { memoryItems, todos, tasks, delegatedWorkSummary } = options;\n\n\treturn {\n\t\tmemory: buildWorkingContextMemorySummary(memoryItems),\n\t\ttodo: buildWorkingContextTodoSummary(todos),\n\t\ttasks: buildWorkingContextTaskSummary(tasks),\n\t\tdelegatedWork: buildWorkingContextDelegatedWorkSummary(delegatedWorkSummary),\n\t};\n}\n"]}