MCPcopy Create free account
hub / github.com/agentforce314/clawcodex / _do_bgtask

Method _do_bgtask

src/server/agent_server.py:726–771  ·  view source on GitHub ↗

Background tasks (the original's Ctrl+B runs): bg_run starts a detached shell command, bg_list lists them, bg_kill terminates one.

(self, request_id: object, subtype: str, command: object, tid: object)

Source from the content-addressed store, hash-verified

724 threading.Thread(target=_work, name=f"insights-{self.session_id}", daemon=True).start()
725
726 def _do_bgtask(self, request_id: object, subtype: str, command: object, tid: object) -> None:
727 """Background tasks (the original's Ctrl+B runs): bg_run starts a detached
728 shell command, bg_list lists them, bg_kill terminates one."""
729 try:
730 from src.background import BackgroundTasks
731
732 if self._bgtasks is None:
733 self._bgtasks = BackgroundTasks()
734 if subtype == "bg_run":
735 if not isinstance(command, str) or not command.strip():
736 self._reply(request_id, {"ok": False, "error": "usage: /bg <command>"})
737 return
738 t = self._bgtasks.start(command.strip(), self.cwd, now=time.time())
739 self._reply(request_id, {"ok": True, "id": t.id, "command": t.command})
740 return
741 if subtype == "bg_agent":
742 # Background agent run: a detached `clawcodex -p <prompt>` subprocess
743 # (the §9 async-agent variant) — fully isolated, concurrent, tracked.
744 if not isinstance(command, str) or not command.strip():
745 self._reply(request_id, {"ok": False, "error": "usage: /bg-agent <prompt>"})
746 return
747 import shlex
748
749 cmd = f"clawcodex -p {shlex.quote(command.strip())}"
750 t = self._bgtasks.start(cmd, self.cwd, now=time.time())
751 self._reply(request_id, {"ok": True, "id": t.id, "command": cmd})
752 return
753 if subtype == "bg_kill":
754 ok = self._bgtasks.kill(str(tid or ""))
755 self._reply(request_id, {"ok": ok})
756 return
757 # bg_list
758 tasks = [
759 {
760 "id": t.id,
761 "command": t.command,
762 "status": t.status,
763 "exit_code": t.exit_code,
764 "output": (t.output or "")[-400:],
765 }
766 for t in self._bgtasks.list()
767 ]
768 self._reply(request_id, {"ok": True, "tasks": tasks})
769 except Exception as exc: # noqa: BLE001
770 logger.exception("[agent-server] bgtask failed")
771 self._reply(request_id, {"ok": False, "error": str(exc)})
772
773 def _do_wiki(self, request_id: object, action: object, path: object) -> None:
774 """/wiki: init | status | ingest <path>. File-based project wiki under

Callers 1

Calls 5

_replyMethod · 0.95
BackgroundTasksClass · 0.90
startMethod · 0.45
killMethod · 0.45
listMethod · 0.45

Tested by

no test coverage detected