| 526 | return json.dumps(payload, ensure_ascii=False) + "\n" |
| 527 | |
| 528 | async def execute(self, task: Task, workspace_path: str) -> TaskResult: |
| 529 | if not await self.is_available(): |
| 530 | return TaskResult(status=TaskStatus.FAILED, content="OpenCode CLI not found") |
| 531 | cmd, metadata = self.build_invocation(task, workspace_path=workspace_path) |
| 532 | |
| 533 | logger.info(f"OpenCode executing: {task.title}") |
| 534 | |
| 535 | try: |
| 536 | stdin_policy = self.stdin_policy_for_process(cmd, metadata) |
| 537 | self._record_stdin_policy_metadata(metadata, stdin_policy) |
| 538 | self._process = await asyncio.create_subprocess_exec( |
| 539 | *cmd, |
| 540 | stdin=self._stdin_target_for_policy(stdin_policy), |
| 541 | stdout=asyncio.subprocess.PIPE, |
| 542 | stderr=asyncio.subprocess.PIPE, |
| 543 | cwd=workspace_path, |
| 544 | **self._subprocess_group_kwargs(), |
| 545 | ) |
| 546 | stdout, stderr = await asyncio.wait_for(self._process.communicate(), timeout=600) |
| 547 | |
| 548 | output = stdout.decode("utf-8", errors="replace") |
| 549 | errors = stderr.decode("utf-8", errors="replace") |
| 550 | |
| 551 | if self._process.returncode == 0: |
| 552 | return TaskResult( |
| 553 | status=TaskStatus.DONE, |
| 554 | content=output, |
| 555 | artifacts={**metadata, "stderr": errors} if errors else metadata, |
| 556 | ) |
| 557 | return TaskResult( |
| 558 | status=TaskStatus.FAILED, |
| 559 | content=f"OpenCode exited with code {self._process.returncode}\n{errors}\n{output}", |
| 560 | artifacts=metadata, |
| 561 | ) |
| 562 | except asyncio.TimeoutError: |
| 563 | if self._process: |
| 564 | self._process.kill() |
| 565 | return TaskResult( |
| 566 | status=TaskStatus.FAILED, |
| 567 | content="OpenCode timed out after 600s", |
| 568 | artifacts=metadata, |
| 569 | ) |
| 570 | except Exception as e: |
| 571 | return TaskResult( |
| 572 | status=TaskStatus.FAILED, |
| 573 | content=f"OpenCode error: {e}", |
| 574 | artifacts=metadata, |
| 575 | ) |
| 576 | finally: |
| 577 | self._process = None |
| 578 | |
| 579 | async def cancel(self, task_id: str) -> bool: |
| 580 | if self._process and self._process.returncode is None: |