"CLI-Anything: Making ALL Software Agent-Native" -- CLI-Hub: https://clianything.cc/
Python
fix: support pid-only DAP attach
yuhao94918967bb38
diff --git a/.gitignore b/.gitignoreindex 47ca0e880..92750c052 100644--- a/.gitignore+++ b/.gitignore@@ -15,6 +15,7 @@!/README.md!/CONTRIBUTING.md!/SECURITY.md+!/FIX_NOTES.md!/assets/!/.pi-extension//.pi-extension/cli-anything/node_modulesdiff --git a/FIX_NOTES.md b/FIX_NOTES.mdnew file mode 100644index 000000000..dab8fe830--- /dev/null+++ b/FIX_NOTES.md@@ -0,0 +1,21 @@+# PR-245 Fix Notes++## DAP Attach Smoke++Result: real LLDB-backed DAP client smoke was not run in this environment because+`lldb` is not installed on PATH. `gcc` is available, but the LLDB Python/runtime+backend required by `cli-anything-lldb-dap` is unavailable.++Checklist for an environment with LLDB installed:++1. Start `cli-anything-lldb-dap` as a stdio DAP server.+2. Send `initialize` and verify a successful response followed by `initialized`.+3. Start a long-running helper process.+4. Send `attach` with only `{"processId": <pid>}` and no `program` or `executable`.+5. Send `configurationDone` and verify the adapter attaches, then emits a stopped+ or continued execution event without an attach validation error.+6. Repeat with `{"processName": "<helper-basename>", "waitFor": false}` and no+ `program` or `executable`.+7. Verify the existing launch path still accepts `{"program": "<helper-path>"}` and+ reaches `configurationDone` without requiring the new empty-target path.+diff --git a/lldb/agent-harness/cli_anything/lldb/core/session.py b/lldb/agent-harness/cli_anything/lldb/core/session.pyindex f0619353c..4a71d9ac1 100644--- a/lldb/agent-harness/cli_anything/lldb/core/session.py+++ b/lldb/agent-harness/cli_anything/lldb/core/session.py@@ -51,6 +51,20 @@ class LLDBSession:"triple": self.target.GetTriple(),}+ def target_create_empty(self, arch: Optional[str] = None) -> Dict[str, Any]:+ """Create an empty target for attach flows without a known executable."""+ if arch:+ self.target = self.debugger.CreateTargetWithFileAndArch("", arch)+ else:+ self.target = self.debugger.CreateTarget("")+ if not self.target or not self.target.IsValid():+ raise RuntimeError("Failed to create empty attach target")+ return {+ "executable": None,+ "arch": arch,+ "triple": self.target.GetTriple(),+ }+def target_info(self) -> Dict[str, Any]:self._require_target()exe = self.target.GetExecutable()diff --git a/lldb/agent-harness/cli_anything/lldb/dap.py b/lldb/agent-harness/cli_anything/lldb/dap.pyindex 93e312682..3864f7f5c 100644--- a/lldb/agent-harness/cli_anything/lldb/dap.py+++ b/lldb/agent-harness/cli_anything/lldb/dap.py@@ -67,6 +67,14 @@ def _raise_protocol_error(message: str):raise DAPProtocolError(message)+def _first_present(mapping: dict[str, Any], *keys: str) -> Any:+ for key in keys:+ value = mapping.get(key)+ if value is not None and not (isinstance(value, str) and value == ""):+ return value+ return None++@dataclass(frozen=True)class StopRule:"""Structured rule used to classify or auto-continue debugger stops."""@@ -293,14 +301,15 @@ class LLDBDebugAdapter:return {}, Nonedef _handle_attach(self, args: dict[str, Any]):- program = args.get("program") or args.get("executable")- if not program:- raise RuntimeError("attach requires 'program' or 'executable' so LLDB can create a target")- self._ensure_session().target_create(str(program), arch=args.get("arch"))- pid = args.get("pid", args.get("processId"))- name = args.get("name", args.get("processName"))+ program = _first_present(args, "program", "executable")+ pid = _first_present(args, "pid", "processId")+ name = _first_present(args, "name", "processName")if pid is None and not name:raise RuntimeError("attach requires pid/processId or name/processName")+ if program:+ self._ensure_session().target_create(str(program), arch=args.get("arch"))+ else:+ self._ensure_session().target_create_empty(arch=args.get("arch"))self._pending_attach = {"pid": int(pid) if pid is not None else None,"name": str(name) if name else None,diff --git a/lldb/agent-harness/cli_anything/lldb/tests/test_core.py b/lldb/agent-harness/cli_anything/lldb/tests/test_core.pyindex d4b3338de..1f3ad2174 100644--- a/lldb/agent-harness/cli_anything/lldb/tests/test_core.py+++ b/lldb/agent-harness/cli_anything/lldb/tests/test_core.py@@ -504,6 +504,77 @@ class TestDAPProtocol:]assert all(item["type"] in {"response", "event"} for item in transcript)+ def test_attach_accepts_process_id_without_program(self):+ from cli_anything.lldb.dap import LLDBDebugAdapter, encode_message, read_message++ fake_session = MagicMock()+ fake_session.target_create_empty.return_value = {"executable": None}+ fake_session.attach_pid.return_value = {}+ fake_session.process_info.return_value = {+ "state": "stopped",+ "selected_thread_id": 77,+ "stop": None,+ "exit_status": 0,+ }+ messages = [+ {"seq": 1, "type": "request", "command": "attach", "arguments": {"processId": 4242}},+ {"seq": 2, "type": "request", "command": "configurationDone", "arguments": {}},+ ]+ out = io.BytesIO()+ adapter = LLDBDebugAdapter(session_factory=lambda: fake_session)+ adapter.run(io.BytesIO(b"".join(encode_message(message) for message in messages)), out)+ out.seek(0)++ attach_response = read_message(out)+ configuration_response = read_message(out)+ stopped_event = read_message(out)++ assert attach_response["success"] is True+ assert configuration_response["success"] is True+ assert stopped_event["event"] == "stopped"+ assert stopped_event["body"]["reason"] == "pause"+ fake_session.target_create.assert_not_called()+ fake_session.target_create_empty.assert_called_once_with(arch=None)+ fake_session.attach_pid.assert_called_once_with(4242)++ def test_attach_accepts_process_name_without_program(self):+ from cli_anything.lldb.dap import LLDBDebugAdapter, encode_message, read_message++ fake_session = MagicMock()+ fake_session.target_create_empty.return_value = {"executable": None}+ fake_session.attach_name.return_value = {}+ fake_session.process_info.return_value = {+ "state": "stopped",+ "selected_thread_id": 78,+ "stop": None,+ "exit_status": 0,+ }+ messages = [+ {+ "seq": 1,+ "type": "request",+ "command": "attach",+ "arguments": {"processName": "sample-app", "waitFor": True},+ },+ {"seq": 2, "type": "request", "command": "configurationDone", "arguments": {}},+ ]+ out = io.BytesIO()+ adapter = LLDBDebugAdapter(session_factory=lambda: fake_session)+ adapter.run(io.BytesIO(b"".join(encode_message(message) for message in messages)), out)+ out.seek(0)++ attach_response = read_message(out)+ configuration_response = read_message(out)+ stopped_event = read_message(out)++ assert attach_response["success"] is True+ assert configuration_response["success"] is True+ assert stopped_event["event"] == "stopped"+ assert stopped_event["body"]["reason"] == "pause"+ fake_session.target_create.assert_not_called()+ fake_session.target_create_empty.assert_called_once_with(arch=None)+ fake_session.attach_name.assert_called_once_with("sample-app", wait_for=True)+def test_modules_response_shape(self):from cli_anything.lldb.dap import LLDBDebugAdapter, read_message@@ -794,6 +865,25 @@ class TestSessionLifecycle:assert status["has_process"] is Trueassert status["process_origin"] == "attached"+ def test_target_create_empty_uses_attach_target(self):+ from cli_anything.lldb.core.session import LLDBSession++ session = self._make_session()+ target = MagicMock()+ target.IsValid.return_value = True+ target.GetTriple.return_value = "x86_64-unknown-linux-gnu"+ session.debugger.CreateTarget.return_value = target++ payload = LLDBSession.target_create_empty(session)++ session.debugger.CreateTarget.assert_called_once_with("")+ assert session.target is target+ assert payload == {+ "executable": None,+ "arch": None,+ "triple": "x86_64-unknown-linux-gnu",+ }+def test_process_info_public_wrapper(self):from cli_anything.lldb.core.session import LLDBSession