mcoda 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.editorconfig +9 -0
- package/.eslintrc.cjs +12 -0
- package/.github/ISSUE_TEMPLATE/bug_report.md +29 -0
- package/.github/ISSUE_TEMPLATE/config.yml +5 -0
- package/.github/ISSUE_TEMPLATE/feature_request.md +19 -0
- package/.github/workflows/ci.yml +37 -0
- package/.github/workflows/nightly.yml +38 -0
- package/.github/workflows/release-dry-run.yml +40 -0
- package/.github/workflows/release-please.yml +22 -0
- package/.github/workflows/release.yml +149 -0
- package/.prettierrc +5 -0
- package/.release-please-manifest.json +8 -0
- package/CHANGELOG.md +7 -0
- package/CLA.md +42 -0
- package/CONTRIBUTING.md +38 -0
- package/LICENSE +21 -0
- package/README.md +314 -0
- package/docs/oss_publishing_plan.md +41 -0
- package/docs/pdr/.gitkeep +0 -0
- package/docs/quality_gates.md +32 -0
- package/docs/rfp/.gitkeep +0 -0
- package/docs/sds/sds.md +11963 -0
- package/docs/usage.md +72 -0
- package/openapi/gen-openapi.ts +1 -0
- package/openapi/generated/clients/.gitkeep +0 -0
- package/openapi/generated/types/.gitkeep +0 -0
- package/openapi/generated/types/index.ts +118 -0
- package/openapi/mcoda.yaml +2063 -0
- package/pack-mcoda.sh +88 -0
- package/package.json +46 -0
- package/packages/agents/CHANGELOG.md +7 -0
- package/packages/agents/LICENSE +21 -0
- package/packages/agents/README.md +9 -0
- package/packages/agents/package.json +41 -0
- package/packages/agents/src/AgentService/.gitkeep +0 -0
- package/packages/agents/src/AgentService/AgentService.d.ts +21 -0
- package/packages/agents/src/AgentService/AgentService.d.ts.map +1 -0
- package/packages/agents/src/AgentService/AgentService.js +141 -0
- package/packages/agents/src/AgentService/AgentService.ts +308 -0
- package/packages/agents/src/__tests__/AgentService.test.ts +284 -0
- package/packages/agents/src/adapters/AdapterTypes.d.ts +29 -0
- package/packages/agents/src/adapters/AdapterTypes.d.ts.map +1 -0
- package/packages/agents/src/adapters/AdapterTypes.js +1 -0
- package/packages/agents/src/adapters/AdapterTypes.ts +32 -0
- package/packages/agents/src/adapters/codex/.gitkeep +0 -0
- package/packages/agents/src/adapters/codex/CodexAdapter.d.ts +11 -0
- package/packages/agents/src/adapters/codex/CodexAdapter.d.ts.map +1 -0
- package/packages/agents/src/adapters/codex/CodexAdapter.js +43 -0
- package/packages/agents/src/adapters/codex/CodexAdapter.ts +63 -0
- package/packages/agents/src/adapters/codex/CodexCliRunner.ts +154 -0
- package/packages/agents/src/adapters/gemini/.gitkeep +0 -0
- package/packages/agents/src/adapters/gemini/GeminiAdapter.d.ts +11 -0
- package/packages/agents/src/adapters/gemini/GeminiAdapter.d.ts.map +1 -0
- package/packages/agents/src/adapters/gemini/GeminiAdapter.js +42 -0
- package/packages/agents/src/adapters/gemini/GeminiAdapter.ts +58 -0
- package/packages/agents/src/adapters/gemini/GeminiCliRunner.ts +75 -0
- package/packages/agents/src/adapters/local/.gitkeep +0 -0
- package/packages/agents/src/adapters/local/LocalAdapter.d.ts +11 -0
- package/packages/agents/src/adapters/local/LocalAdapter.d.ts.map +1 -0
- package/packages/agents/src/adapters/local/LocalAdapter.js +38 -0
- package/packages/agents/src/adapters/local/LocalAdapter.ts +43 -0
- package/packages/agents/src/adapters/ollama/OllamaCliAdapter.ts +58 -0
- package/packages/agents/src/adapters/ollama/OllamaCliRunner.ts +70 -0
- package/packages/agents/src/adapters/ollama/OllamaRemoteAdapter.ts +205 -0
- package/packages/agents/src/adapters/openai/.gitkeep +0 -0
- package/packages/agents/src/adapters/openai/OpenAiAdapter.d.ts +11 -0
- package/packages/agents/src/adapters/openai/OpenAiAdapter.d.ts.map +1 -0
- package/packages/agents/src/adapters/openai/OpenAiAdapter.js +51 -0
- package/packages/agents/src/adapters/openai/OpenAiAdapter.ts +56 -0
- package/packages/agents/src/adapters/openai/OpenAiCliAdapter.ts +62 -0
- package/packages/agents/src/adapters/qa/.gitkeep +0 -0
- package/packages/agents/src/adapters/qa/QaAdapter.d.ts +11 -0
- package/packages/agents/src/adapters/qa/QaAdapter.d.ts.map +1 -0
- package/packages/agents/src/adapters/qa/QaAdapter.js +37 -0
- package/packages/agents/src/adapters/qa/QaAdapter.ts +42 -0
- package/packages/agents/src/adapters/zhipu/ZhipuApiAdapter.ts +273 -0
- package/packages/agents/src/index.d.ts +8 -0
- package/packages/agents/src/index.d.ts.map +1 -0
- package/packages/agents/src/index.js +7 -0
- package/packages/agents/src/index.ts +11 -0
- package/packages/agents/tsconfig.json +14 -0
- package/packages/cli/CHANGELOG.md +7 -0
- package/packages/cli/LICENSE +21 -0
- package/packages/cli/README.md +23 -0
- package/packages/cli/package.json +61 -0
- package/packages/cli/src/__tests__/AgentsCommands.test.ts +137 -0
- package/packages/cli/src/__tests__/BacklogCommands.test.ts +40 -0
- package/packages/cli/src/__tests__/CodeReviewCommand.test.ts +594 -0
- package/packages/cli/src/__tests__/CreateTasksCommand.test.ts +40 -0
- package/packages/cli/src/__tests__/DocsCommands.test.ts +41 -0
- package/packages/cli/src/__tests__/EstimateCommands.test.ts +54 -0
- package/packages/cli/src/__tests__/JobsCommands.behavior.test.ts +311 -0
- package/packages/cli/src/__tests__/JobsCommands.test.ts +49 -0
- package/packages/cli/src/__tests__/MigrateTasksCommand.test.ts +36 -0
- package/packages/cli/src/__tests__/OpenapiCommands.test.ts +34 -0
- package/packages/cli/src/__tests__/OrderTasksCommand.test.ts +150 -0
- package/packages/cli/src/__tests__/PlanningCommands.test.ts +9 -0
- package/packages/cli/src/__tests__/QaTasksCommand.test.ts +58 -0
- package/packages/cli/src/__tests__/RefineTasksCommand.test.ts +63 -0
- package/packages/cli/src/__tests__/RoutingCommands.test.ts +302 -0
- package/packages/cli/src/__tests__/SetWorkspaceCommand.test.ts +18 -0
- package/packages/cli/src/__tests__/TaskShowCommands.test.ts +130 -0
- package/packages/cli/src/__tests__/TelemetryCommands.test.ts +35 -0
- package/packages/cli/src/__tests__/TestAgentCommand.test.ts +41 -0
- package/packages/cli/src/__tests__/UpdateCommands.test.ts +292 -0
- package/packages/cli/src/__tests__/WorkOnTasksCommand.test.ts +42 -0
- package/packages/cli/src/bin/.gitkeep +0 -0
- package/packages/cli/src/bin/McodaEntrypoint.ts +180 -0
- package/packages/cli/src/commands/agents/.gitkeep +0 -0
- package/packages/cli/src/commands/agents/AgentsCommands.ts +374 -0
- package/packages/cli/src/commands/agents/GatewayAgentCommand.ts +621 -0
- package/packages/cli/src/commands/agents/TestAgentCommand.ts +63 -0
- package/packages/cli/src/commands/backlog/.gitkeep +0 -0
- package/packages/cli/src/commands/backlog/BacklogCommands.ts +286 -0
- package/packages/cli/src/commands/backlog/OrderTasksCommand.ts +237 -0
- package/packages/cli/src/commands/backlog/TaskShowCommands.ts +289 -0
- package/packages/cli/src/commands/docs/.gitkeep +0 -0
- package/packages/cli/src/commands/docs/DocsCommands.ts +413 -0
- package/packages/cli/src/commands/estimate/EstimateCommands.ts +290 -0
- package/packages/cli/src/commands/jobs/.gitkeep +0 -0
- package/packages/cli/src/commands/jobs/JobsCommands.ts +595 -0
- package/packages/cli/src/commands/openapi/OpenapiCommands.ts +167 -0
- package/packages/cli/src/commands/planning/.gitkeep +0 -0
- package/packages/cli/src/commands/planning/CreateTasksCommand.ts +149 -0
- package/packages/cli/src/commands/planning/MigrateTasksCommand.ts +105 -0
- package/packages/cli/src/commands/planning/PlanningCommands.ts +1 -0
- package/packages/cli/src/commands/planning/QaTasksCommand.ts +320 -0
- package/packages/cli/src/commands/planning/RefineTasksCommand.ts +408 -0
- package/packages/cli/src/commands/review/CodeReviewCommand.ts +262 -0
- package/packages/cli/src/commands/routing/.gitkeep +0 -0
- package/packages/cli/src/commands/routing/RoutingCommands.ts +554 -0
- package/packages/cli/src/commands/telemetry/.gitkeep +0 -0
- package/packages/cli/src/commands/telemetry/TelemetryCommands.ts +348 -0
- package/packages/cli/src/commands/update/.gitkeep +0 -0
- package/packages/cli/src/commands/update/UpdateCommands.ts +301 -0
- package/packages/cli/src/commands/work/WorkOnTasksCommand.ts +264 -0
- package/packages/cli/src/commands/workspace/SetWorkspaceCommand.ts +132 -0
- package/packages/cli/src/index.ts +18 -0
- package/packages/cli/test/packaging_guardrails.test.js +75 -0
- package/packages/cli/tsconfig.json +20 -0
- package/packages/core/CHANGELOG.md +7 -0
- package/packages/core/LICENSE +21 -0
- package/packages/core/README.md +9 -0
- package/packages/core/package.json +45 -0
- package/packages/core/src/__tests__/SmokeClasses.test.ts +32 -0
- package/packages/core/src/api/AgentsApi.ts +219 -0
- package/packages/core/src/api/QaTasksApi.ts +38 -0
- package/packages/core/src/api/TasksApi.ts +35 -0
- package/packages/core/src/api/__tests__/AgentsApi.test.ts +203 -0
- package/packages/core/src/api/__tests__/QaTasksApi.test.ts +51 -0
- package/packages/core/src/api/__tests__/TasksApi.test.ts +56 -0
- package/packages/core/src/config/.gitkeep +0 -0
- package/packages/core/src/config/ConfigService.ts +1 -0
- package/packages/core/src/domain/dependencies/.gitkeep +0 -0
- package/packages/core/src/domain/dependencies/Dependency.ts +1 -0
- package/packages/core/src/domain/epics/.gitkeep +0 -0
- package/packages/core/src/domain/epics/Epic.ts +1 -0
- package/packages/core/src/domain/projects/.gitkeep +0 -0
- package/packages/core/src/domain/projects/Project.ts +1 -0
- package/packages/core/src/domain/tasks/.gitkeep +0 -0
- package/packages/core/src/domain/tasks/Task.ts +1 -0
- package/packages/core/src/domain/userStories/.gitkeep +0 -0
- package/packages/core/src/domain/userStories/UserStory.ts +1 -0
- package/packages/core/src/index.ts +27 -0
- package/packages/core/src/prompts/.gitkeep +0 -0
- package/packages/core/src/prompts/PdrPrompts.ts +23 -0
- package/packages/core/src/prompts/PromptLoader.ts +1 -0
- package/packages/core/src/prompts/SdsPrompts.ts +47 -0
- package/packages/core/src/services/agents/.gitkeep +0 -0
- package/packages/core/src/services/agents/AgentManagementService.ts +1 -0
- package/packages/core/src/services/agents/GatewayAgentService.ts +956 -0
- package/packages/core/src/services/agents/RoutingService.ts +461 -0
- package/packages/core/src/services/agents/__tests__/GatewayAgentService.test.ts +72 -0
- package/packages/core/src/services/agents/__tests__/RoutingService.test.ts +267 -0
- package/packages/core/src/services/agents/generated/RoutingApiClient.ts +89 -0
- package/packages/core/src/services/backlog/.gitkeep +0 -0
- package/packages/core/src/services/backlog/BacklogService.ts +580 -0
- package/packages/core/src/services/backlog/TaskOrderingService.ts +868 -0
- package/packages/core/src/services/backlog/__tests__/BacklogService.test.ts +219 -0
- package/packages/core/src/services/backlog/__tests__/TaskOrderingService.test.ts +268 -0
- package/packages/core/src/services/docs/.gitkeep +0 -0
- package/packages/core/src/services/docs/DocsService.ts +1913 -0
- package/packages/core/src/services/docs/__tests__/DocsService.test.ts +350 -0
- package/packages/core/src/services/estimate/EstimateService.ts +111 -0
- package/packages/core/src/services/estimate/VelocityService.ts +272 -0
- package/packages/core/src/services/estimate/__tests__/VelocityAndEstimate.test.ts +209 -0
- package/packages/core/src/services/estimate/types.ts +41 -0
- package/packages/core/src/services/execution/.gitkeep +0 -0
- package/packages/core/src/services/execution/ExecutionService.ts +1 -0
- package/packages/core/src/services/execution/QaFollowupService.ts +289 -0
- package/packages/core/src/services/execution/QaProfileService.ts +160 -0
- package/packages/core/src/services/execution/QaTasksService.ts +1303 -0
- package/packages/core/src/services/execution/TaskSelectionService.ts +362 -0
- package/packages/core/src/services/execution/TaskStateService.ts +64 -0
- package/packages/core/src/services/execution/WorkOnTasksService.ts +2023 -0
- package/packages/core/src/services/execution/__tests__/QaFollowupService.test.ts +58 -0
- package/packages/core/src/services/execution/__tests__/QaProfileService.test.ts +49 -0
- package/packages/core/src/services/execution/__tests__/QaTasksService.test.ts +157 -0
- package/packages/core/src/services/execution/__tests__/TaskSelectionService.test.ts +179 -0
- package/packages/core/src/services/execution/__tests__/TaskStateService.test.ts +51 -0
- package/packages/core/src/services/execution/__tests__/WorkOnTasksService.test.ts +285 -0
- package/packages/core/src/services/jobs/.gitkeep +0 -0
- package/packages/core/src/services/jobs/JobInsightsService.ts +355 -0
- package/packages/core/src/services/jobs/JobResumeService.ts +119 -0
- package/packages/core/src/services/jobs/JobService.ts +648 -0
- package/packages/core/src/services/jobs/JobsApiClient.ts +113 -0
- package/packages/core/src/services/jobs/__tests__/JobInsightsService.test.ts +17 -0
- package/packages/core/src/services/jobs/__tests__/JobResumeService.test.ts +45 -0
- package/packages/core/src/services/jobs/__tests__/JobService.test.ts +44 -0
- package/packages/core/src/services/openapi/OpenApiService.ts +558 -0
- package/packages/core/src/services/openapi/__tests__/OpenApiService.test.ts +57 -0
- package/packages/core/src/services/planning/.gitkeep +0 -0
- package/packages/core/src/services/planning/CreateTasksService.ts +1280 -0
- package/packages/core/src/services/planning/KeyHelpers.ts +80 -0
- package/packages/core/src/services/planning/PlanningService.ts +1 -0
- package/packages/core/src/services/planning/RefineTasksService.ts +1552 -0
- package/packages/core/src/services/planning/__tests__/CreateTasksService.test.ts +288 -0
- package/packages/core/src/services/planning/__tests__/KeyHelpers.test.ts +16 -0
- package/packages/core/src/services/planning/__tests__/RefineTasksService.test.ts +172 -0
- package/packages/core/src/services/review/CodeReviewService.ts +1386 -0
- package/packages/core/src/services/review/__tests__/CodeReviewService.test.ts +89 -0
- package/packages/core/src/services/system/SystemUpdateService.ts +177 -0
- package/packages/core/src/services/system/__tests__/SystemUpdateService.test.ts +40 -0
- package/packages/core/src/services/tasks/TaskApiResolver.ts +37 -0
- package/packages/core/src/services/tasks/TaskDetailService.ts +494 -0
- package/packages/core/src/services/tasks/__tests__/TaskApiResolver.test.ts +41 -0
- package/packages/core/src/services/tasks/__tests__/TaskDetailService.test.ts +178 -0
- package/packages/core/src/services/telemetry/.gitkeep +0 -0
- package/packages/core/src/services/telemetry/TelemetryService.ts +515 -0
- package/packages/core/src/services/telemetry/__tests__/TelemetryService.test.ts +160 -0
- package/packages/core/src/workspace/.gitkeep +0 -0
- package/packages/core/src/workspace/WorkspaceManager.ts +234 -0
- package/packages/core/tsconfig.json +20 -0
- package/packages/db/CHANGELOG.md +7 -0
- package/packages/db/LICENSE +21 -0
- package/packages/db/README.md +9 -0
- package/packages/db/package.json +42 -0
- package/packages/db/src/__tests__/GlobalRepository.test.ts +109 -0
- package/packages/db/src/__tests__/SchemaAlignment.test.ts +80 -0
- package/packages/db/src/__tests__/WorkspaceRepository.test.ts +19 -0
- package/packages/db/src/index.d.ts +6 -0
- package/packages/db/src/index.d.ts.map +1 -0
- package/packages/db/src/index.js +5 -0
- package/packages/db/src/index.ts +6 -0
- package/packages/db/src/migrations/global/.gitkeep +0 -0
- package/packages/db/src/migrations/global/GlobalMigrations.d.ts +9 -0
- package/packages/db/src/migrations/global/GlobalMigrations.d.ts.map +1 -0
- package/packages/db/src/migrations/global/GlobalMigrations.js +68 -0
- package/packages/db/src/migrations/global/GlobalMigrations.ts +336 -0
- package/packages/db/src/migrations/workspace/.gitkeep +0 -0
- package/packages/db/src/migrations/workspace/WorkspaceMigrations.d.ts +9 -0
- package/packages/db/src/migrations/workspace/WorkspaceMigrations.d.ts.map +1 -0
- package/packages/db/src/migrations/workspace/WorkspaceMigrations.js +251 -0
- package/packages/db/src/migrations/workspace/WorkspaceMigrations.ts +248 -0
- package/packages/db/src/repositories/global/.gitkeep +0 -0
- package/packages/db/src/repositories/global/GlobalRepository.d.ts +30 -0
- package/packages/db/src/repositories/global/GlobalRepository.d.ts.map +1 -0
- package/packages/db/src/repositories/global/GlobalRepository.js +209 -0
- package/packages/db/src/repositories/global/GlobalRepository.ts +492 -0
- package/packages/db/src/repositories/workspace/.gitkeep +0 -0
- package/packages/db/src/repositories/workspace/WorkspaceRepository.d.ts +282 -0
- package/packages/db/src/repositories/workspace/WorkspaceRepository.d.ts.map +1 -0
- package/packages/db/src/repositories/workspace/WorkspaceRepository.js +773 -0
- package/packages/db/src/repositories/workspace/WorkspaceRepository.ts +1511 -0
- package/packages/db/src/sqlite/connection.d.ts +11 -0
- package/packages/db/src/sqlite/connection.d.ts.map +1 -0
- package/packages/db/src/sqlite/connection.js +31 -0
- package/packages/db/src/sqlite/connection.ts +35 -0
- package/packages/db/src/sqlite/pragmas.d.ts +5 -0
- package/packages/db/src/sqlite/pragmas.d.ts.map +1 -0
- package/packages/db/src/sqlite/pragmas.js +6 -0
- package/packages/db/src/sqlite/pragmas.ts +10 -0
- package/packages/db/tsconfig.json +13 -0
- package/packages/generators/package.json +21 -0
- package/packages/generators/src/__tests__/Generators.test.ts +19 -0
- package/packages/generators/src/index.ts +1 -0
- package/packages/generators/src/openapi/generateTypes.ts +1 -0
- package/packages/generators/src/openapi/validateSchema.ts +1 -0
- package/packages/generators/src/scaffolding/docs/.gitkeep +0 -0
- package/packages/generators/src/scaffolding/docs/DocsScaffolder.ts +1 -0
- package/packages/generators/src/scaffolding/global/.gitkeep +0 -0
- package/packages/generators/src/scaffolding/global/GlobalScaffolder.ts +1 -0
- package/packages/generators/src/scaffolding/workspace/.gitkeep +0 -0
- package/packages/generators/src/scaffolding/workspace/WorkspaceScaffolder.ts +1 -0
- package/packages/generators/tsconfig.json +10 -0
- package/packages/integrations/CHANGELOG.md +7 -0
- package/packages/integrations/LICENSE +21 -0
- package/packages/integrations/README.md +9 -0
- package/packages/integrations/package.json +47 -0
- package/packages/integrations/src/docdex/.gitkeep +0 -0
- package/packages/integrations/src/docdex/DocdexClient.d.ts +50 -0
- package/packages/integrations/src/docdex/DocdexClient.d.ts.map +1 -0
- package/packages/integrations/src/docdex/DocdexClient.js +216 -0
- package/packages/integrations/src/docdex/DocdexClient.ts +261 -0
- package/packages/integrations/src/docdex/__tests__/DocdexClient.test.ts +29 -0
- package/packages/integrations/src/index.d.ts +2 -0
- package/packages/integrations/src/index.d.ts.map +1 -0
- package/packages/integrations/src/index.js +4 -0
- package/packages/integrations/src/index.ts +5 -0
- package/packages/integrations/src/issues/.gitkeep +0 -0
- package/packages/integrations/src/issues/IssuesClient.ts +1 -0
- package/packages/integrations/src/issues/__tests__/IssuesClient.test.ts +10 -0
- package/packages/integrations/src/qa/.gitkeep +0 -0
- package/packages/integrations/src/qa/ChromiumQaAdapter.ts +89 -0
- package/packages/integrations/src/qa/CliQaAdapter.ts +95 -0
- package/packages/integrations/src/qa/MaestroQaAdapter.ts +91 -0
- package/packages/integrations/src/qa/QaAdapter.ts +7 -0
- package/packages/integrations/src/qa/QaClient.ts +1 -0
- package/packages/integrations/src/qa/QaTypes.ts +26 -0
- package/packages/integrations/src/qa/__tests__/ChromiumQaAdapter.test.ts +30 -0
- package/packages/integrations/src/qa/__tests__/CliQaAdapter.test.ts +33 -0
- package/packages/integrations/src/qa/__tests__/MaestroQaAdapter.test.ts +30 -0
- package/packages/integrations/src/qa/index.ts +5 -0
- package/packages/integrations/src/system/SystemClient.ts +50 -0
- package/packages/integrations/src/system/__tests__/SystemClient.test.ts +40 -0
- package/packages/integrations/src/telemetry/TelemetryClient.ts +139 -0
- package/packages/integrations/src/telemetry/__tests__/TelemetryClient.test.ts +41 -0
- package/packages/integrations/src/vcs/.gitkeep +0 -0
- package/packages/integrations/src/vcs/VcsClient.ts +211 -0
- package/packages/integrations/src/vcs/__tests__/VcsClient.test.ts +26 -0
- package/packages/integrations/tsconfig.json +14 -0
- package/packages/shared/CHANGELOG.md +7 -0
- package/packages/shared/LICENSE +21 -0
- package/packages/shared/README.md +9 -0
- package/packages/shared/package.json +40 -0
- package/packages/shared/src/__tests__/CommandMetadata.test.ts +15 -0
- package/packages/shared/src/__tests__/ServiceShells.test.ts +16 -0
- package/packages/shared/src/crypto/.gitkeep +0 -0
- package/packages/shared/src/crypto/CryptoHelper.d.ts +15 -0
- package/packages/shared/src/crypto/CryptoHelper.d.ts.map +1 -0
- package/packages/shared/src/crypto/CryptoHelper.js +54 -0
- package/packages/shared/src/crypto/CryptoHelper.ts +57 -0
- package/packages/shared/src/errors/.gitkeep +0 -0
- package/packages/shared/src/errors/ErrorFactory.ts +1 -0
- package/packages/shared/src/index.d.ts +6 -0
- package/packages/shared/src/index.d.ts.map +1 -0
- package/packages/shared/src/index.js +4 -0
- package/packages/shared/src/index.ts +35 -0
- package/packages/shared/src/logging/.gitkeep +0 -0
- package/packages/shared/src/logging/Logger.ts +1 -0
- package/packages/shared/src/metadata/CommandMetadata.ts +165 -0
- package/packages/shared/src/openapi/.gitkeep +0 -0
- package/packages/shared/src/openapi/OpenApiTypes.d.ts +216 -0
- package/packages/shared/src/openapi/OpenApiTypes.d.ts.map +1 -0
- package/packages/shared/src/openapi/OpenApiTypes.js +1 -0
- package/packages/shared/src/openapi/OpenApiTypes.ts +312 -0
- package/packages/shared/src/paths/.gitkeep +0 -0
- package/packages/shared/src/paths/PathHelper.d.ts +12 -0
- package/packages/shared/src/paths/PathHelper.d.ts.map +1 -0
- package/packages/shared/src/paths/PathHelper.js +24 -0
- package/packages/shared/src/paths/PathHelper.ts +29 -0
- package/packages/shared/src/qa/QaProfile.ts +14 -0
- package/packages/shared/src/utils/.gitkeep +0 -0
- package/packages/shared/src/utils/UtilityService.ts +1 -0
- package/packages/shared/tsconfig.json +10 -0
- package/packages/testing/package.json +26 -0
- package/packages/testing/src/__tests__/TestingFakes.test.ts +15 -0
- package/packages/testing/src/cli/e2e/.gitkeep +0 -0
- package/packages/testing/src/cli/e2e/E2eSuite.ts +1 -0
- package/packages/testing/src/fakes/agents/.gitkeep +0 -0
- package/packages/testing/src/fakes/agents/FakeAgents.ts +1 -0
- package/packages/testing/src/fakes/docdex/.gitkeep +0 -0
- package/packages/testing/src/fakes/docdex/FakeDocdexClient.ts +1 -0
- package/packages/testing/src/fakes/qa/.gitkeep +0 -0
- package/packages/testing/src/fakes/qa/FakeQaClient.ts +1 -0
- package/packages/testing/src/fakes/vcs/.gitkeep +0 -0
- package/packages/testing/src/fakes/vcs/FakeVcsClient.ts +1 -0
- package/packages/testing/src/fixtures/db/.gitkeep +0 -0
- package/packages/testing/src/fixtures/db/DbFixtures.ts +1 -0
- package/packages/testing/src/fixtures/workspaces/.gitkeep +0 -0
- package/packages/testing/src/fixtures/workspaces/WorkspaceFixtures.ts +1 -0
- package/packages/testing/src/index.ts +1 -0
- package/packages/testing/tsconfig.json +10 -0
- package/pnpm-workspace.yaml +2 -0
- package/prompts/README.md +5 -0
- package/prompts/code-reviewer.md +23 -0
- package/prompts/code-writer.md +35 -0
- package/prompts/gateway-agent.md +27 -0
- package/prompts/qa-agent.md +21 -0
- package/release-please-config.json +39 -0
- package/scripts/build-all.ts +1 -0
- package/scripts/dev.ts +1 -0
- package/scripts/install-local-cli.sh +28 -0
- package/scripts/pack-npm-tarballs.js +63 -0
- package/scripts/release.ts +1 -0
- package/scripts/run-node-tests.js +37 -0
- package/tests/all.js +127 -0
- package/tests/api/openapi_spec.test.js +21 -0
- package/tests/artifacts.md +31 -0
- package/tests/component/cli_version.test.js +38 -0
- package/tests/integration/workspace_resolver.test.js +44 -0
- package/tests/unit/crypto_helper.test.js +36 -0
- package/tests/unit/path_helper.test.js +20 -0
- package/tsconfig.base.json +32 -0
|
@@ -0,0 +1,648 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { promises as fs } from "node:fs";
|
|
4
|
+
import { PathHelper } from "@mcoda/shared";
|
|
5
|
+
import {
|
|
6
|
+
WorkspaceRepository,
|
|
7
|
+
CommandStatus,
|
|
8
|
+
JobStatus,
|
|
9
|
+
TokenUsageInsert,
|
|
10
|
+
} from "@mcoda/db";
|
|
11
|
+
|
|
12
|
+
export type JobState = JobStatus;
|
|
13
|
+
export type CommandRunStatus = CommandStatus;
|
|
14
|
+
|
|
15
|
+
export interface CommandRunRecord {
|
|
16
|
+
id: string;
|
|
17
|
+
commandName: string;
|
|
18
|
+
workspaceId: string;
|
|
19
|
+
projectKey?: string;
|
|
20
|
+
jobId?: string;
|
|
21
|
+
taskIds?: string[];
|
|
22
|
+
gitBranch?: string;
|
|
23
|
+
gitBaseBranch?: string;
|
|
24
|
+
startedAt: string;
|
|
25
|
+
completedAt?: string;
|
|
26
|
+
status: CommandRunStatus;
|
|
27
|
+
errorSummary?: string;
|
|
28
|
+
durationSeconds?: number;
|
|
29
|
+
spProcessed?: number | null;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface JobRecord {
|
|
33
|
+
id: string;
|
|
34
|
+
type: string;
|
|
35
|
+
state: JobState;
|
|
36
|
+
jobState?: JobState;
|
|
37
|
+
jobStateDetail?: string;
|
|
38
|
+
commandRunId?: string;
|
|
39
|
+
commandName?: string;
|
|
40
|
+
workspaceId: string;
|
|
41
|
+
projectKey?: string;
|
|
42
|
+
payload?: Record<string, unknown>;
|
|
43
|
+
totalItems?: number;
|
|
44
|
+
processedItems?: number;
|
|
45
|
+
totalUnits?: number;
|
|
46
|
+
completedUnits?: number;
|
|
47
|
+
lastCheckpoint?: string;
|
|
48
|
+
createdAt: string;
|
|
49
|
+
updatedAt: string;
|
|
50
|
+
completedAt?: string;
|
|
51
|
+
errorSummary?: string;
|
|
52
|
+
durationSeconds?: number;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface TokenUsageRecord extends TokenUsageInsert {
|
|
56
|
+
commandName?: string;
|
|
57
|
+
action?: string;
|
|
58
|
+
promptTokens?: number;
|
|
59
|
+
completionTokens?: number;
|
|
60
|
+
costUsd?: number;
|
|
61
|
+
metadata?: Record<string, unknown>;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export interface JobCheckpoint {
|
|
65
|
+
stage: string;
|
|
66
|
+
timestamp: string;
|
|
67
|
+
details?: Record<string, unknown>;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export interface JobLogRow {
|
|
71
|
+
timestamp: string;
|
|
72
|
+
sequence?: number | null;
|
|
73
|
+
level?: string | null;
|
|
74
|
+
source?: string | null;
|
|
75
|
+
message?: string | null;
|
|
76
|
+
taskId?: string | null;
|
|
77
|
+
taskKey?: string | null;
|
|
78
|
+
phase?: string | null;
|
|
79
|
+
details?: Record<string, unknown> | null;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export interface TaskRunSnapshotRow {
|
|
83
|
+
taskId?: string | null;
|
|
84
|
+
taskKey?: string | null;
|
|
85
|
+
status?: string | null;
|
|
86
|
+
startedAt?: string | null;
|
|
87
|
+
finishedAt?: string | null;
|
|
88
|
+
command?: string | null;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const nowIso = (): string => new Date().toISOString();
|
|
92
|
+
|
|
93
|
+
export class JobService {
|
|
94
|
+
private checkpointCounters = new Map<string, number>();
|
|
95
|
+
private workspaceRepoInit = false;
|
|
96
|
+
private telemetryConfig?: { optOut?: boolean; strict?: boolean };
|
|
97
|
+
private telemetryWarningShown = false;
|
|
98
|
+
private telemetryRemoteWarningShown = false;
|
|
99
|
+
private perRunTelemetryDisabled: boolean;
|
|
100
|
+
private envTelemetryDisabled: boolean;
|
|
101
|
+
private requireRepo: boolean;
|
|
102
|
+
private workspaceRoot: string;
|
|
103
|
+
private workspaceId: string;
|
|
104
|
+
|
|
105
|
+
constructor(
|
|
106
|
+
workspace: string | { workspaceRoot: string; workspaceId?: string },
|
|
107
|
+
private workspaceRepo?: WorkspaceRepository,
|
|
108
|
+
options: { noTelemetry?: boolean; requireRepo?: boolean } = {},
|
|
109
|
+
) {
|
|
110
|
+
const resolvedRoot = typeof workspace === "string" ? workspace : workspace.workspaceRoot;
|
|
111
|
+
this.workspaceRoot = resolvedRoot;
|
|
112
|
+
this.workspaceId = typeof workspace === "string" ? resolvedRoot : workspace.workspaceId ?? resolvedRoot;
|
|
113
|
+
this.perRunTelemetryDisabled = options.noTelemetry ?? false;
|
|
114
|
+
this.envTelemetryDisabled = (process.env.MCODA_TELEMETRY ?? "").toLowerCase() === "off";
|
|
115
|
+
this.requireRepo = options.requireRepo ?? false;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
private get mcodaDir(): string {
|
|
119
|
+
return path.join(this.workspaceRoot, ".mcoda");
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
private get commandRunsPath(): string {
|
|
123
|
+
return path.join(this.mcodaDir, "command_runs.json");
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
private get jobsStorePath(): string {
|
|
127
|
+
return path.join(this.mcodaDir, "jobs.json");
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
private get tokenUsagePath(): string {
|
|
131
|
+
return path.join(this.mcodaDir, "token_usage.json");
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
private jobDir(jobId: string): string {
|
|
135
|
+
return path.join(this.mcodaDir, "jobs", jobId);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
private manifestPath(jobId: string): string {
|
|
139
|
+
return path.join(this.jobDir(jobId), "manifest.json");
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
private checkpointDir(jobId: string): string {
|
|
143
|
+
return path.join(this.jobDir(jobId), "checkpoints");
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
private logsDir(jobId: string): string {
|
|
147
|
+
return path.join(this.jobDir(jobId), "logs");
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
private async ensureMcoda(): Promise<void> {
|
|
151
|
+
await PathHelper.ensureDir(this.mcodaDir);
|
|
152
|
+
if (this.workspaceRepoInit) return;
|
|
153
|
+
this.workspaceRepoInit = true;
|
|
154
|
+
if (process.env.MCODA_DISABLE_DB === "1") {
|
|
155
|
+
if (this.requireRepo) {
|
|
156
|
+
throw new Error("Workspace DB disabled via MCODA_DISABLE_DB; job operations require the workspace DB per SDS.");
|
|
157
|
+
}
|
|
158
|
+
this.workspaceRepo = undefined;
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
try {
|
|
162
|
+
if (!this.workspaceRepo) {
|
|
163
|
+
this.workspaceRepo = await WorkspaceRepository.create(this.workspaceRoot);
|
|
164
|
+
}
|
|
165
|
+
} catch (error) {
|
|
166
|
+
if (this.requireRepo) {
|
|
167
|
+
throw new Error(
|
|
168
|
+
`Workspace DB could not be opened for jobs (${(error as Error).message}); run mcoda init/create-tasks to initialize the workspace.`,
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
// Fall back to JSON stores if sqlite is unavailable or schema mismatches.
|
|
172
|
+
this.workspaceRepo = undefined;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
private async readJsonArray<T>(filePath: string): Promise<T[]> {
|
|
177
|
+
try {
|
|
178
|
+
const raw = await fs.readFile(filePath, "utf8");
|
|
179
|
+
const parsed = JSON.parse(raw) as T[];
|
|
180
|
+
if (Array.isArray(parsed)) return parsed;
|
|
181
|
+
return [];
|
|
182
|
+
} catch {
|
|
183
|
+
return [];
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
private async writeJsonArray<T>(filePath: string, records: T[]): Promise<void> {
|
|
188
|
+
await fs.mkdir(path.dirname(filePath), { recursive: true });
|
|
189
|
+
await fs.writeFile(filePath, JSON.stringify(records, null, 2), "utf8");
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
private async appendJsonArray<T>(filePath: string, record: T): Promise<void> {
|
|
193
|
+
const existing = await this.readJsonArray<T>(filePath);
|
|
194
|
+
existing.push(record);
|
|
195
|
+
await this.writeJsonArray(filePath, existing);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
async listJobs(): Promise<JobRecord[]> {
|
|
199
|
+
await this.ensureMcoda();
|
|
200
|
+
if (this.workspaceRepo && "listJobs" in this.workspaceRepo) {
|
|
201
|
+
const rows = await (this.workspaceRepo as any).listJobs();
|
|
202
|
+
return rows as JobRecord[];
|
|
203
|
+
}
|
|
204
|
+
return this.readJsonArray<JobRecord>(this.jobsStorePath);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
async getJob(jobId: string): Promise<JobRecord | undefined> {
|
|
208
|
+
await this.ensureMcoda();
|
|
209
|
+
if (this.workspaceRepo && "getJob" in this.workspaceRepo) {
|
|
210
|
+
return ((await (this.workspaceRepo as any).getJob(jobId)) as JobRecord | undefined) ?? undefined;
|
|
211
|
+
}
|
|
212
|
+
const jobs = await this.readJsonArray<JobRecord>(this.jobsStorePath);
|
|
213
|
+
return jobs.find((j) => j.id === jobId);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
async readCheckpoints(jobId: string): Promise<JobCheckpoint[]> {
|
|
217
|
+
const dir = this.checkpointDir(jobId);
|
|
218
|
+
try {
|
|
219
|
+
const entries = await fs.readdir(dir);
|
|
220
|
+
const checkpoints: JobCheckpoint[] = [];
|
|
221
|
+
for (const entry of entries.sort()) {
|
|
222
|
+
const raw = await fs.readFile(path.join(dir, entry), "utf8");
|
|
223
|
+
checkpoints.push(JSON.parse(raw) as JobCheckpoint);
|
|
224
|
+
}
|
|
225
|
+
return checkpoints;
|
|
226
|
+
} catch {
|
|
227
|
+
return [];
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
async readLog(jobId: string): Promise<string> {
|
|
232
|
+
const logPath = path.join(this.logsDir(jobId), "stream.log");
|
|
233
|
+
try {
|
|
234
|
+
return await fs.readFile(logPath, "utf8");
|
|
235
|
+
} catch {
|
|
236
|
+
return "";
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
async listJobLogs(
|
|
241
|
+
jobId: string,
|
|
242
|
+
options: { since?: string; after?: { timestamp: string; sequence?: number | null } } = {},
|
|
243
|
+
): Promise<JobLogRow[]> {
|
|
244
|
+
await this.ensureMcoda();
|
|
245
|
+
if (this.workspaceRepo && "getDb" in this.workspaceRepo) {
|
|
246
|
+
const db = (this.workspaceRepo as any).getDb();
|
|
247
|
+
const clauses = ["tr.job_id = ?"];
|
|
248
|
+
const params: any[] = [jobId];
|
|
249
|
+
if (options.since) {
|
|
250
|
+
clauses.push("datetime(tl.timestamp) >= datetime(?)");
|
|
251
|
+
params.push(options.since);
|
|
252
|
+
}
|
|
253
|
+
if (options.after?.timestamp) {
|
|
254
|
+
clauses.push("(datetime(tl.timestamp) > datetime(?) OR (tl.timestamp = ? AND COALESCE(tl.sequence,0) > COALESCE(?,0)))");
|
|
255
|
+
params.push(options.after.timestamp, options.after.timestamp, options.after.sequence ?? 0);
|
|
256
|
+
}
|
|
257
|
+
const where = clauses.length ? `WHERE ${clauses.join(" AND ")}` : "";
|
|
258
|
+
const rows = (await db.all(
|
|
259
|
+
`
|
|
260
|
+
SELECT tl.timestamp, tl.sequence, tl.level, tl.source, tl.message, tl.details_json, tr.task_id, tr.command, t.key as task_key
|
|
261
|
+
FROM task_logs tl
|
|
262
|
+
JOIN task_runs tr ON tr.id = tl.task_run_id
|
|
263
|
+
LEFT JOIN tasks t ON t.id = tr.task_id
|
|
264
|
+
${where}
|
|
265
|
+
ORDER BY datetime(tl.timestamp) ASC, tl.sequence ASC
|
|
266
|
+
LIMIT 500
|
|
267
|
+
`,
|
|
268
|
+
...params
|
|
269
|
+
)) as any[];
|
|
270
|
+
return rows.map((row: any) => {
|
|
271
|
+
const details = row.details_json ? JSON.parse(row.details_json) : null;
|
|
272
|
+
return {
|
|
273
|
+
timestamp: row.timestamp,
|
|
274
|
+
sequence: row.sequence ?? null,
|
|
275
|
+
level: row.level ?? null,
|
|
276
|
+
source: row.source ?? null,
|
|
277
|
+
message: row.message ?? null,
|
|
278
|
+
taskId: row.task_id ?? null,
|
|
279
|
+
taskKey: row.task_key ?? null,
|
|
280
|
+
phase: (details as any)?.phase ?? row.command ?? null,
|
|
281
|
+
details,
|
|
282
|
+
};
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
// Fallback to file-based stream log when DB access is not available.
|
|
286
|
+
const raw = await this.readLog(jobId);
|
|
287
|
+
const lines = raw.split(/\r?\n/).filter(Boolean);
|
|
288
|
+
return lines.map((line, idx) => ({
|
|
289
|
+
timestamp: nowIso(),
|
|
290
|
+
sequence: idx,
|
|
291
|
+
message: line,
|
|
292
|
+
}));
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
async summarizeTaskRuns(jobId: string): Promise<TaskRunSnapshotRow[]> {
|
|
296
|
+
await this.ensureMcoda();
|
|
297
|
+
if (this.workspaceRepo && "getDb" in this.workspaceRepo) {
|
|
298
|
+
const db = (this.workspaceRepo as any).getDb();
|
|
299
|
+
const rows = (await db.all(
|
|
300
|
+
`
|
|
301
|
+
SELECT tr.task_id, tr.status, tr.started_at, tr.finished_at, tr.command, t.key as task_key
|
|
302
|
+
FROM task_runs tr
|
|
303
|
+
LEFT JOIN tasks t ON t.id = tr.task_id
|
|
304
|
+
WHERE tr.job_id = ?
|
|
305
|
+
ORDER BY datetime(tr.started_at) ASC, datetime(tr.finished_at) ASC
|
|
306
|
+
`,
|
|
307
|
+
jobId
|
|
308
|
+
)) as any[];
|
|
309
|
+
return rows.map((row: any) => ({
|
|
310
|
+
taskId: row.task_id ?? null,
|
|
311
|
+
taskKey: row.task_key ?? null,
|
|
312
|
+
status: row.status ?? null,
|
|
313
|
+
startedAt: row.started_at ?? null,
|
|
314
|
+
finishedAt: row.finished_at ?? null,
|
|
315
|
+
command: row.command ?? null,
|
|
316
|
+
}));
|
|
317
|
+
}
|
|
318
|
+
return [];
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
async startCommandRun(
|
|
322
|
+
commandName: string,
|
|
323
|
+
projectKey?: string,
|
|
324
|
+
options?: { gitBranch?: string; gitBaseBranch?: string; taskIds?: string[]; jobId?: string },
|
|
325
|
+
): Promise<CommandRunRecord> {
|
|
326
|
+
await this.ensureMcoda();
|
|
327
|
+
const startedAt = nowIso();
|
|
328
|
+
let record: CommandRunRecord = {
|
|
329
|
+
id: randomUUID(),
|
|
330
|
+
commandName,
|
|
331
|
+
workspaceId: this.workspaceId,
|
|
332
|
+
projectKey,
|
|
333
|
+
jobId: options?.jobId,
|
|
334
|
+
taskIds: options?.taskIds,
|
|
335
|
+
gitBranch: options?.gitBranch,
|
|
336
|
+
gitBaseBranch: options?.gitBaseBranch,
|
|
337
|
+
startedAt,
|
|
338
|
+
status: "running",
|
|
339
|
+
spProcessed: null,
|
|
340
|
+
};
|
|
341
|
+
if (this.workspaceRepo) {
|
|
342
|
+
const row = await this.workspaceRepo.createCommandRun({
|
|
343
|
+
workspaceId: this.workspaceId,
|
|
344
|
+
commandName,
|
|
345
|
+
jobId: record.jobId,
|
|
346
|
+
taskIds: record.taskIds,
|
|
347
|
+
gitBranch: record.gitBranch,
|
|
348
|
+
gitBaseBranch: record.gitBaseBranch,
|
|
349
|
+
startedAt,
|
|
350
|
+
status: "running",
|
|
351
|
+
});
|
|
352
|
+
record = { ...record, id: row.id };
|
|
353
|
+
}
|
|
354
|
+
await this.appendJsonArray(this.commandRunsPath, record);
|
|
355
|
+
return record;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
async finishCommandRun(runId: string, status: CommandRunStatus, errorSummary?: string, spProcessed?: number): Promise<void> {
|
|
359
|
+
const runs = await this.readJsonArray<CommandRunRecord>(this.commandRunsPath);
|
|
360
|
+
const idx = runs.findIndex((r) => r.id === runId);
|
|
361
|
+
if (idx === -1) return;
|
|
362
|
+
const completedAt = nowIso();
|
|
363
|
+
const durationSeconds =
|
|
364
|
+
runs[idx].startedAt ? (Date.parse(completedAt) - Date.parse(runs[idx].startedAt)) / 1000 : undefined;
|
|
365
|
+
runs[idx] = { ...runs[idx], completedAt, status, errorSummary, durationSeconds, spProcessed: spProcessed ?? runs[idx].spProcessed };
|
|
366
|
+
await this.writeJsonArray(this.commandRunsPath, runs);
|
|
367
|
+
if (this.workspaceRepo) {
|
|
368
|
+
await this.workspaceRepo.completeCommandRun(runId, {
|
|
369
|
+
status,
|
|
370
|
+
completedAt,
|
|
371
|
+
errorSummary,
|
|
372
|
+
durationSeconds,
|
|
373
|
+
spProcessed: spProcessed ?? runs[idx].spProcessed ?? null,
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
async startJob(
|
|
379
|
+
type: string,
|
|
380
|
+
commandRunId?: string,
|
|
381
|
+
projectKey?: string,
|
|
382
|
+
options: { payload?: Record<string, unknown>; commandName?: string; totalItems?: number; processedItems?: number } = {},
|
|
383
|
+
): Promise<JobRecord> {
|
|
384
|
+
await this.ensureMcoda();
|
|
385
|
+
const createdAt = nowIso();
|
|
386
|
+
let record: JobRecord = {
|
|
387
|
+
id: randomUUID(),
|
|
388
|
+
type,
|
|
389
|
+
state: "running",
|
|
390
|
+
commandRunId,
|
|
391
|
+
commandName: options.commandName ?? type,
|
|
392
|
+
workspaceId: this.workspaceId,
|
|
393
|
+
projectKey,
|
|
394
|
+
payload: options.payload,
|
|
395
|
+
totalItems: options.totalItems,
|
|
396
|
+
processedItems: options.processedItems,
|
|
397
|
+
totalUnits: options.totalItems,
|
|
398
|
+
completedUnits: options.processedItems,
|
|
399
|
+
createdAt,
|
|
400
|
+
updatedAt: createdAt,
|
|
401
|
+
};
|
|
402
|
+
if (this.workspaceRepo) {
|
|
403
|
+
const row = await this.workspaceRepo.createJob({
|
|
404
|
+
workspaceId: this.workspaceId,
|
|
405
|
+
type,
|
|
406
|
+
state: "running",
|
|
407
|
+
commandName: record.commandName,
|
|
408
|
+
payload: options.payload,
|
|
409
|
+
totalItems: record.totalItems,
|
|
410
|
+
processedItems: record.processedItems,
|
|
411
|
+
});
|
|
412
|
+
record = { ...record, id: row.id, createdAt: row.createdAt, updatedAt: row.updatedAt };
|
|
413
|
+
}
|
|
414
|
+
const jobs = await this.readJsonArray<JobRecord>(this.jobsStorePath);
|
|
415
|
+
jobs.push(record);
|
|
416
|
+
await this.writeJsonArray(this.jobsStorePath, jobs);
|
|
417
|
+
await this.writeManifest(record);
|
|
418
|
+
if (commandRunId) {
|
|
419
|
+
await this.attachCommandRunToJob(commandRunId, record.id);
|
|
420
|
+
}
|
|
421
|
+
return record;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
private async attachCommandRunToJob(commandRunId: string, jobId: string): Promise<void> {
|
|
425
|
+
const runs = await this.readJsonArray<CommandRunRecord>(this.commandRunsPath);
|
|
426
|
+
const idx = runs.findIndex((r) => r.id === commandRunId);
|
|
427
|
+
if (idx !== -1) {
|
|
428
|
+
runs[idx] = { ...runs[idx], jobId };
|
|
429
|
+
await this.writeJsonArray(this.commandRunsPath, runs);
|
|
430
|
+
}
|
|
431
|
+
if (this.workspaceRepo && "setCommandRunJobId" in this.workspaceRepo) {
|
|
432
|
+
try {
|
|
433
|
+
await (this.workspaceRepo as any).setCommandRunJobId(commandRunId, jobId);
|
|
434
|
+
} catch {
|
|
435
|
+
// ignore linking failures
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
async updateJobStatus(
|
|
441
|
+
jobId: string,
|
|
442
|
+
state: JobState,
|
|
443
|
+
metadata?: {
|
|
444
|
+
payload?: Record<string, unknown>;
|
|
445
|
+
totalItems?: number;
|
|
446
|
+
processedItems?: number;
|
|
447
|
+
totalUnits?: number;
|
|
448
|
+
completedUnits?: number;
|
|
449
|
+
lastCheckpoint?: string;
|
|
450
|
+
errorSummary?: string;
|
|
451
|
+
[key: string]: unknown;
|
|
452
|
+
},
|
|
453
|
+
): Promise<void> {
|
|
454
|
+
const jobs = await this.readJsonArray<JobRecord>(this.jobsStorePath);
|
|
455
|
+
const idx = jobs.findIndex((j) => j.id === jobId);
|
|
456
|
+
if (idx === -1) return;
|
|
457
|
+
const completedAt = state !== "running" && state !== "queued" ? nowIso() : jobs[idx].completedAt;
|
|
458
|
+
const durationSeconds =
|
|
459
|
+
completedAt && jobs[idx].createdAt ? (Date.parse(completedAt) - Date.parse(jobs[idx].createdAt)) / 1000 : jobs[idx].durationSeconds;
|
|
460
|
+
const jobStateDetail = (metadata as any)?.job_state_detail ?? (metadata as any)?.jobStateDetail ?? jobs[idx].jobStateDetail;
|
|
461
|
+
const payloadUpdate =
|
|
462
|
+
metadata?.payload ??
|
|
463
|
+
(metadata
|
|
464
|
+
? Object.fromEntries(
|
|
465
|
+
Object.entries(metadata).filter(
|
|
466
|
+
([key]) => !["payload", "totalItems", "processedItems", "lastCheckpoint", "errorSummary", "error"].includes(key),
|
|
467
|
+
),
|
|
468
|
+
)
|
|
469
|
+
: undefined);
|
|
470
|
+
const updated: JobRecord = {
|
|
471
|
+
...jobs[idx],
|
|
472
|
+
state,
|
|
473
|
+
jobState: state,
|
|
474
|
+
jobStateDetail,
|
|
475
|
+
payload: payloadUpdate ? { ...(jobs[idx].payload ?? {}), ...payloadUpdate } : jobs[idx].payload,
|
|
476
|
+
totalItems: metadata?.totalItems ?? jobs[idx].totalItems,
|
|
477
|
+
processedItems: metadata?.processedItems ?? jobs[idx].processedItems,
|
|
478
|
+
totalUnits: metadata?.totalUnits ?? metadata?.totalItems ?? jobs[idx].totalUnits ?? jobs[idx].totalItems,
|
|
479
|
+
completedUnits: metadata?.completedUnits ?? metadata?.processedItems ?? jobs[idx].completedUnits ?? jobs[idx].processedItems,
|
|
480
|
+
lastCheckpoint: metadata?.lastCheckpoint ?? jobs[idx].lastCheckpoint,
|
|
481
|
+
errorSummary: metadata?.errorSummary ?? (metadata as any)?.error ?? jobs[idx].errorSummary,
|
|
482
|
+
updatedAt: nowIso(),
|
|
483
|
+
completedAt,
|
|
484
|
+
durationSeconds,
|
|
485
|
+
};
|
|
486
|
+
jobs[idx] = updated;
|
|
487
|
+
await this.writeJsonArray(this.jobsStorePath, jobs);
|
|
488
|
+
await this.writeManifest(updated);
|
|
489
|
+
if (this.workspaceRepo) {
|
|
490
|
+
await this.workspaceRepo.updateJobState(jobId, {
|
|
491
|
+
state,
|
|
492
|
+
commandName: updated.commandName ?? updated.type,
|
|
493
|
+
totalItems: updated.totalItems,
|
|
494
|
+
processedItems: updated.processedItems,
|
|
495
|
+
lastCheckpoint: updated.lastCheckpoint,
|
|
496
|
+
errorSummary: updated.errorSummary,
|
|
497
|
+
completedAt: updated.completedAt ?? null,
|
|
498
|
+
payload: updated.payload,
|
|
499
|
+
});
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
async writeCheckpoint(jobId: string, checkpoint: JobCheckpoint): Promise<void> {
|
|
504
|
+
const dir = this.checkpointDir(jobId);
|
|
505
|
+
await PathHelper.ensureDir(dir);
|
|
506
|
+
let current = this.checkpointCounters.get(jobId);
|
|
507
|
+
if (current === undefined) {
|
|
508
|
+
try {
|
|
509
|
+
const entries = await fs.readdir(dir);
|
|
510
|
+
const nums = entries
|
|
511
|
+
.map((e) => Number.parseInt(e.replace(/\.ckpt\.json$/, ""), 10))
|
|
512
|
+
.filter((n) => Number.isFinite(n));
|
|
513
|
+
current = nums.length ? Math.max(...nums) : 0;
|
|
514
|
+
} catch {
|
|
515
|
+
current = 0;
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
const next = (current ?? 0) + 1;
|
|
519
|
+
this.checkpointCounters.set(jobId, next);
|
|
520
|
+
const filename = `${String(next).padStart(6, "0")}.ckpt.json`;
|
|
521
|
+
const target = path.join(dir, filename);
|
|
522
|
+
const job = await this.getJob(jobId);
|
|
523
|
+
const createdAt = nowIso();
|
|
524
|
+
const payload = {
|
|
525
|
+
schema_version: 1,
|
|
526
|
+
job_id: jobId,
|
|
527
|
+
checkpoint_seq: next,
|
|
528
|
+
checkpoint_id: randomUUID(),
|
|
529
|
+
created_at: createdAt,
|
|
530
|
+
status: job?.state ?? "running",
|
|
531
|
+
stage: checkpoint.stage,
|
|
532
|
+
timestamp: checkpoint.timestamp ?? createdAt,
|
|
533
|
+
reason: checkpoint.details?.reason,
|
|
534
|
+
progress: {
|
|
535
|
+
total: job?.totalItems ?? (checkpoint.details as any)?.totalItems ?? null,
|
|
536
|
+
completed: job?.processedItems ?? (checkpoint.details as any)?.processedItems ?? null,
|
|
537
|
+
},
|
|
538
|
+
details: checkpoint.details,
|
|
539
|
+
};
|
|
540
|
+
await fs.writeFile(target, JSON.stringify(payload, null, 2), "utf8");
|
|
541
|
+
if (this.workspaceRepo) {
|
|
542
|
+
await this.workspaceRepo.updateJobState(jobId, {
|
|
543
|
+
lastCheckpoint: checkpoint.stage,
|
|
544
|
+
});
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
async appendLog(jobId: string, content: string): Promise<void> {
|
|
549
|
+
const logDir = this.logsDir(jobId);
|
|
550
|
+
await PathHelper.ensureDir(logDir);
|
|
551
|
+
const logPath = path.join(logDir, "stream.log");
|
|
552
|
+
await fs.appendFile(logPath, content, "utf8");
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
private async readTelemetryConfig(): Promise<{ optOut?: boolean; strict?: boolean } | undefined> {
|
|
556
|
+
const configPath = path.join(this.mcodaDir, "config.json");
|
|
557
|
+
try {
|
|
558
|
+
const raw = await fs.readFile(configPath, "utf8");
|
|
559
|
+
const parsed = JSON.parse(raw) as { telemetry?: { optOut?: boolean; strict?: boolean } };
|
|
560
|
+
return parsed.telemetry;
|
|
561
|
+
} catch {
|
|
562
|
+
return undefined;
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
private async shouldRecordTokenUsage(): Promise<boolean> {
|
|
567
|
+
if (this.telemetryConfig === undefined) {
|
|
568
|
+
this.telemetryConfig = await this.readTelemetryConfig();
|
|
569
|
+
}
|
|
570
|
+
if (this.telemetryConfig?.strict) {
|
|
571
|
+
if (!this.telemetryWarningShown) {
|
|
572
|
+
// eslint-disable-next-line no-console
|
|
573
|
+
console.warn("Telemetry strict mode is enabled; token usage will not be recorded locally.");
|
|
574
|
+
this.telemetryWarningShown = true;
|
|
575
|
+
}
|
|
576
|
+
return false;
|
|
577
|
+
}
|
|
578
|
+
if (
|
|
579
|
+
(this.perRunTelemetryDisabled || this.envTelemetryDisabled || this.telemetryConfig?.optOut) &&
|
|
580
|
+
!this.telemetryRemoteWarningShown
|
|
581
|
+
) {
|
|
582
|
+
// eslint-disable-next-line no-console
|
|
583
|
+
console.warn(
|
|
584
|
+
"Remote telemetry export disabled for this run (--no-telemetry/MCODA_TELEMETRY=off or opt-out). Local logging still enabled unless telemetry.strict is set.",
|
|
585
|
+
);
|
|
586
|
+
this.telemetryRemoteWarningShown = true;
|
|
587
|
+
}
|
|
588
|
+
return true;
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
async recordTokenUsage(entry: TokenUsageRecord): Promise<void> {
|
|
592
|
+
const recordTelemetry = await this.shouldRecordTokenUsage();
|
|
593
|
+
if (!recordTelemetry) return;
|
|
594
|
+
const normalized: TokenUsageInsert = {
|
|
595
|
+
workspaceId: entry.workspaceId ?? this.workspaceId,
|
|
596
|
+
agentId: entry.agentId ?? null,
|
|
597
|
+
modelName: entry.modelName ?? null,
|
|
598
|
+
jobId: entry.jobId ?? null,
|
|
599
|
+
commandRunId: entry.commandRunId ?? null,
|
|
600
|
+
taskRunId: entry.taskRunId ?? null,
|
|
601
|
+
taskId: entry.taskId ?? null,
|
|
602
|
+
projectId: entry.projectId ?? null,
|
|
603
|
+
epicId: entry.epicId ?? null,
|
|
604
|
+
userStoryId: entry.userStoryId ?? null,
|
|
605
|
+
tokensPrompt: entry.tokensPrompt ?? entry.promptTokens ?? null,
|
|
606
|
+
tokensCompletion: entry.tokensCompletion ?? entry.completionTokens ?? null,
|
|
607
|
+
tokensTotal: entry.tokensTotal ?? null,
|
|
608
|
+
costEstimate: entry.costEstimate ?? entry.costUsd ?? null,
|
|
609
|
+
durationSeconds: entry.durationSeconds ?? null,
|
|
610
|
+
timestamp: entry.timestamp,
|
|
611
|
+
metadata: {
|
|
612
|
+
...(entry.metadata ?? {}),
|
|
613
|
+
...(entry.commandName ? { commandName: entry.commandName } : {}),
|
|
614
|
+
...(entry.action ? { action: entry.action } : {}),
|
|
615
|
+
},
|
|
616
|
+
};
|
|
617
|
+
const fileRecord = {
|
|
618
|
+
...normalized,
|
|
619
|
+
...(entry.commandName ? { commandName: entry.commandName } : {}),
|
|
620
|
+
...(entry.action ? { action: entry.action } : {}),
|
|
621
|
+
};
|
|
622
|
+
await this.appendJsonArray(this.tokenUsagePath, fileRecord as any);
|
|
623
|
+
if (this.workspaceRepo) {
|
|
624
|
+
await this.workspaceRepo.recordTokenUsage(normalized);
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
async writeManifest(job: JobRecord, extras: Record<string, unknown> = {}): Promise<void> {
|
|
629
|
+
await PathHelper.ensureDir(this.jobDir(job.id));
|
|
630
|
+
const manifestPath = this.manifestPath(job.id);
|
|
631
|
+
const payload = {
|
|
632
|
+
schema_version: 1,
|
|
633
|
+
job_id: job.id,
|
|
634
|
+
updated_at: new Date().toISOString(),
|
|
635
|
+
status: (job as any).status ?? job.state,
|
|
636
|
+
progress: { total: job.totalItems ?? null, completed: job.processedItems ?? null },
|
|
637
|
+
...job,
|
|
638
|
+
...extras,
|
|
639
|
+
};
|
|
640
|
+
await fs.writeFile(manifestPath, JSON.stringify(payload, null, 2), "utf8");
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
async close(): Promise<void> {
|
|
644
|
+
if (this.workspaceRepo) {
|
|
645
|
+
await this.workspaceRepo.close();
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
}
|