deepvista-cli 3.0.0__tar.gz → 3.2.0__tar.gz

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.
Files changed (94) hide show
  1. deepvista_cli-3.2.0/.release-please-manifest.json +3 -0
  2. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/CHANGELOG.md +28 -0
  3. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/PKG-INFO +2 -2
  4. deepvista_cli-3.2.0/deepvista_cli/commands/agents.py +537 -0
  5. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/deepvista_cli/commands/card.py +168 -0
  6. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/deepvista_cli/commands/tasks.py +56 -145
  7. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/plugins/claude-code/.claude-plugin/plugin.json +1 -1
  8. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/pyproject.toml +2 -2
  9. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/skills/deepvista/reference/notes.md +5 -2
  10. deepvista_cli-3.2.0/skills/deepvista/reference/openclaw.md +98 -0
  11. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/skills/deepvista/reference/shared.md +5 -4
  12. deepvista_cli-3.2.0/skills/deepvista/reference/tasks.md +54 -0
  13. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/skills/deepvista/reference/vistabase-card.md +14 -1
  14. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/tests/test_agent_id_tagging.py +94 -11
  15. deepvista_cli-3.2.0/tests/test_card_comment_commands.py +117 -0
  16. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/tests/test_tasks_commands.py +94 -54
  17. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/uv.lock +5 -5
  18. deepvista_cli-3.0.0/.release-please-manifest.json +0 -3
  19. deepvista_cli-3.0.0/deepvista_cli/commands/agents.py +0 -366
  20. deepvista_cli-3.0.0/skills/deepvista/reference/openclaw.md +0 -82
  21. deepvista_cli-3.0.0/skills/deepvista/reference/tasks.md +0 -72
  22. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/.claude-plugin/marketplace.json +0 -0
  23. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/.github/workflows/ci.yml +0 -0
  24. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/.github/workflows/publish.yml +0 -0
  25. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/.github/workflows/release-please.yml +0 -0
  26. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/.gitignore +0 -0
  27. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/.pre-commit-config.yaml +0 -0
  28. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/CLAUDE.md +0 -0
  29. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/CONTRIBUTING.md +0 -0
  30. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/LICENSE +0 -0
  31. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/README.md +0 -0
  32. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/deepvista_cli/__init__.py +0 -0
  33. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/deepvista_cli/auth/__init__.py +0 -0
  34. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/deepvista_cli/auth/callback_server.py +0 -0
  35. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/deepvista_cli/auth/login.py +0 -0
  36. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/deepvista_cli/auth/tokens.py +0 -0
  37. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/deepvista_cli/client/__init__.py +0 -0
  38. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/deepvista_cli/client/http.py +0 -0
  39. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/deepvista_cli/client/origin.py +0 -0
  40. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/deepvista_cli/commands/__init__.py +0 -0
  41. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/deepvista_cli/commands/auth.py +0 -0
  42. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/deepvista_cli/commands/chat.py +0 -0
  43. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/deepvista_cli/commands/config.py +0 -0
  44. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/deepvista_cli/commands/notes.py +0 -0
  45. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/deepvista_cli/commands/project.py +0 -0
  46. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/deepvista_cli/commands/schedule.py +0 -0
  47. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/deepvista_cli/commands/session.py +0 -0
  48. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/deepvista_cli/commands/skill.py +0 -0
  49. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/deepvista_cli/commands/upgrade.py +0 -0
  50. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/deepvista_cli/config.py +0 -0
  51. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/deepvista_cli/main.py +0 -0
  52. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/deepvista_cli/output/__init__.py +0 -0
  53. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/deepvista_cli/output/formatter.py +0 -0
  54. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/deepvista_cli/resources/__init__.py +0 -0
  55. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/deepvista_cli/resources/workflow_host_runtime.md +0 -0
  56. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/deepvista_cli/session_note.py +0 -0
  57. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/deepvista_cli/skill_catalog.py +0 -0
  58. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/deepvista_cli/workflow_doc.py +0 -0
  59. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/docs/assets/deepvista-banner.png +0 -0
  60. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/install.ps1 +0 -0
  61. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/install.sh +0 -0
  62. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/plugins/README.md +0 -0
  63. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/plugins/claude-code/README.md +0 -0
  64. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/plugins/claude-code/commands/deepvista.md +0 -0
  65. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/plugins/claude-code/commands/refresh-skills.md +0 -0
  66. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/plugins/claude-code/hooks/hooks.json +0 -0
  67. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/plugins/claude-code/scripts/deepvista-agent-sync.sh +0 -0
  68. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/plugins/claude-code/scripts/deepvista-session-end.sh +0 -0
  69. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/plugins/claude-code/scripts/deepvista-session-start.sh +0 -0
  70. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/plugins/claude-code/scripts/deepvista-session-turn.sh +0 -0
  71. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/plugins/claude-code/scripts/deepvista-skill-url.py +0 -0
  72. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/plugins/claude-code/scripts/deepvista-sync.sh +0 -0
  73. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/plugins/claude-code/skills/.gitignore +0 -0
  74. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/plugins/claude-code/skills/install-deepvista-cli/SKILL.md +0 -0
  75. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/release-please-config.json +0 -0
  76. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/scripts/check_plugin_version.py +0 -0
  77. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/skills/deepvista/SKILL.md +0 -0
  78. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/skills/deepvista/reference/chat.md +0 -0
  79. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/skills/deepvista/reference/project.md +0 -0
  80. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/skills/deepvista/reference/session.md +0 -0
  81. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/skills/deepvista/reference/skill-analyze-notes.md +0 -0
  82. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/skills/deepvista/reference/skill-create-from-note.md +0 -0
  83. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/skills/deepvista/reference/skill-import-files.md +0 -0
  84. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/skills/deepvista/reference/skill-research-to-skill.md +0 -0
  85. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/skills/deepvista/reference/skill.md +0 -0
  86. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/skills/dv-workflow/SKILL.md +0 -0
  87. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/tests/__init__.py +0 -0
  88. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/tests/test_entry_points.py +0 -0
  89. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/tests/test_project_commands.py +0 -0
  90. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/tests/test_session_note_format.py +0 -0
  91. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/tests/test_skill_catalog.py +0 -0
  92. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/tests/test_skill_commands.py +0 -0
  93. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/tests/test_workflow_doc_phases.py +0 -0
  94. {deepvista_cli-3.0.0 → deepvista_cli-3.2.0}/uninstall.sh +0 -0
@@ -0,0 +1,3 @@
1
+ {
2
+ ".": "3.2.0"
3
+ }
@@ -8,6 +8,15 @@ users what's new between the version they have installed and the latest release.
8
8
 
9
9
  ## Unreleased
10
10
 
11
+ ### Changed
12
+ - **Machine identity is project-scoped** — uniqueness is
13
+ `(project_id, machine_fingerprint)`. Local cache:
14
+ `machines/<fingerprint>__<project_id>.json`. Same device in two projects →
15
+ two Machine rows. Project members can see teammate Machines; only the
16
+ registering user syncs/claims. `agent_role` is not sent on register;
17
+ `agent_type` is soft `last_seen_tool`. Adopting `AGENT_ALREADY_REGISTERED`
18
+ requires a matching fingerprint.
19
+
11
20
  ### Added
12
21
  - **Managed agents become Claude Code subagents** (DV-836). New `deepvista
13
22
  agents export` turns each distinct managed-agent role (DV-832 `agent_role`)
@@ -37,6 +46,25 @@ users what's new between the version they have installed and the latest release.
37
46
  adopts a pre-existing server-side row instead of failing when the local
38
47
  file is missing.
39
48
 
49
+ ## [3.2.0](https://github.com/DeepVista-AI/deepvista-cli/compare/v3.1.0...v3.2.0) (2026-07-13)
50
+
51
+
52
+ ### Features
53
+
54
+ * **DV-1496:** expose card comment commands in CLI ([#204](https://github.com/DeepVista-AI/deepvista-cli/issues/204)) ([3551969](https://github.com/DeepVista-AI/deepvista-cli/commit/35519695664c89076fe6785fa1d19c5c6a3846dd))
55
+
56
+
57
+ ### Bug Fixes
58
+
59
+ * **DV-1484:** reconcile OpenClaw auto-capture with explicit-note rule ([#199](https://github.com/DeepVista-AI/deepvista-cli/issues/199)) ([663d2df](https://github.com/DeepVista-AI/deepvista-cli/commit/663d2df18d775ad2e90cf1db23c17621c0db4469))
60
+
61
+ ## [3.1.0](https://github.com/DeepVista-AI/deepvista-cli/compare/v3.0.0...v3.1.0) (2026-07-12)
62
+
63
+
64
+ ### Features
65
+
66
+ * project-scoped Machines (fingerprint + project_id) ([#197](https://github.com/DeepVista-AI/deepvista-cli/issues/197)) ([07da470](https://github.com/DeepVista-AI/deepvista-cli/commit/07da470d324ed2779bbc047b9032775bfa3c8d5b))
67
+
40
68
  ## [3.0.0](https://github.com/DeepVista-AI/deepvista-cli/compare/v2.0.0...v3.0.0) (2026-07-11)
41
69
 
42
70
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: deepvista-cli
3
- Version: 3.0.0
3
+ Version: 3.2.0
4
4
  Summary: CLI for DeepVista — chat, notes, skills, and memory from your terminal.
5
5
  Project-URL: Homepage, https://deepvista.ai
6
6
  Project-URL: Repository, https://github.com/DeepVista-AI/deepvista-cli
@@ -18,7 +18,7 @@ Classifier: Programming Language :: Python :: 3
18
18
  Classifier: Programming Language :: Python :: 3.12
19
19
  Classifier: Topic :: Utilities
20
20
  Requires-Python: >=3.12
21
- Requires-Dist: click>=8.1
21
+ Requires-Dist: click>=8.3.3
22
22
  Requires-Dist: filelock>=3.12
23
23
  Requires-Dist: httpx<1,>=0.27
24
24
  Requires-Dist: psutil>=7.2.2
@@ -0,0 +1,537 @@
1
+ """deepvista agents — Machine identity heartbeat.
2
+
3
+ A Machine is this device (fingerprint = hostname + MAC + OS) **registered to
4
+ a project**. Server uniqueness is ``(project_id, machine_fingerprint)``.
5
+ Project members can see the Machine; only the registering user syncs/claims.
6
+ ``agent_type`` is soft metadata (last-seen tool). ``agent_role`` is unused.
7
+
8
+ Local cache: ``~/.config/deepvista/machines/<fingerprint>__<project_id>.json``.
9
+ Legacy ``agents/<type>__<project>.json`` files are migrated on read.
10
+
11
+ Storage helpers here are shared with ``tasks``, ``session``, ``notes`` and
12
+ origin detection.
13
+ """
14
+
15
+ from __future__ import annotations
16
+
17
+ import json as _json
18
+ import os
19
+ from pathlib import Path
20
+
21
+ import click
22
+
23
+ from deepvista_cli.client.http import DeepVistaClient
24
+ from deepvista_cli.client.origin import build_origin, detect_agent_tool
25
+ from deepvista_cli.config import CONFIG_DIR
26
+ from deepvista_cli.output.formatter import format_output, output_error
27
+
28
+ MACHINES_DIR = CONFIG_DIR / "machines"
29
+ # Legacy path; new writes go to MACHINES_DIR. Reads fall back here.
30
+ AGENTS_DIR = CONFIG_DIR / "agents"
31
+
32
+ ERROR_CODE_AGENT_NOT_FOUND = "AGENT_NOT_FOUND"
33
+ ERROR_CODE_AGENT_ALREADY_REGISTERED = "AGENT_ALREADY_REGISTERED"
34
+
35
+ _AGENT_TYPE_LABELS = {
36
+ "claude-code": "Claude Code",
37
+ "opencode": "OpenCode",
38
+ "openclaw": "OpenClaw",
39
+ "cursor": "Cursor",
40
+ "windsurf": "Windsurf",
41
+ "cline": "Cline",
42
+ "aider": "Aider",
43
+ "github-copilot": "GitHub Copilot",
44
+ "deepvista-cli": "DeepVista CLI",
45
+ }
46
+
47
+
48
+ def _machine_fingerprint() -> str:
49
+ import hashlib
50
+ import platform
51
+ import uuid
52
+
53
+ raw = f"{platform.node()}:{uuid.getnode()}:{platform.system()}"
54
+ return hashlib.sha256(raw.encode()).hexdigest()[:16]
55
+
56
+
57
+ def _machine_path(project_id: str, fingerprint: str | None = None) -> Path:
58
+ fp = fingerprint or _machine_fingerprint()
59
+ return MACHINES_DIR / f"{fp}__{project_id}.json"
60
+
61
+
62
+ def _read_json_file(path: Path) -> dict | None:
63
+ try:
64
+ return _json.loads(path.read_text())
65
+ except (_json.JSONDecodeError, OSError, KeyError):
66
+ return None
67
+
68
+
69
+ def _save_machine_id(
70
+ agent_id: str,
71
+ project_id: str,
72
+ *,
73
+ fingerprint: str | None = None,
74
+ last_seen_tool: str | None = None,
75
+ project_name: str | None = None,
76
+ ) -> None:
77
+ """Persist this Machine's server agent_id for ``(fingerprint, project_id)``."""
78
+ fp = fingerprint or _machine_fingerprint()
79
+ MACHINES_DIR.mkdir(parents=True, exist_ok=True)
80
+ path = _machine_path(project_id, fp)
81
+ data: dict = {
82
+ "agent_id": agent_id,
83
+ "machine_fingerprint": fp,
84
+ "project_id": project_id,
85
+ }
86
+ if last_seen_tool:
87
+ data["last_seen_tool"] = last_seen_tool
88
+ data["agent_type"] = last_seen_tool
89
+ if project_name:
90
+ data["project_name"] = project_name
91
+ path.write_text(_json.dumps(data))
92
+ os.chmod(path, 0o600)
93
+
94
+
95
+ def _load_machine_id(project_id: str, fingerprint: str | None = None) -> str | None:
96
+ """Return cached agent_id for this Machine in ``project_id``."""
97
+ fp = fingerprint or _machine_fingerprint()
98
+ path = _machine_path(project_id, fp)
99
+ if path.exists():
100
+ if data := _read_json_file(path):
101
+ if aid := data.get("agent_id"):
102
+ return str(aid)
103
+ return None
104
+
105
+ # Migrate legacy agents/<type>__<project>.json and fingerprint-only machines/.
106
+ return _migrate_legacy_agent_cache(fp, project_id)
107
+
108
+
109
+ def _migrate_legacy_agent_cache(fingerprint: str, project_id: str) -> str | None:
110
+ """Adopt legacy cache files into ``machines/{fp}__{project}.json``."""
111
+ candidates: list[tuple[float, Path]] = []
112
+
113
+ # Prefer project-scoped legacy agent files.
114
+ if AGENTS_DIR.exists():
115
+ for path in AGENTS_DIR.glob(f"*__{project_id}.json"):
116
+ try:
117
+ candidates.append((path.stat().st_mtime, path))
118
+ except OSError:
119
+ continue
120
+ for path in AGENTS_DIR.glob("*.json"):
121
+ try:
122
+ data = _read_json_file(path)
123
+ except Exception:
124
+ continue
125
+ if data and data.get("project_id") == project_id:
126
+ try:
127
+ candidates.append((path.stat().st_mtime, path))
128
+ except OSError:
129
+ continue
130
+
131
+ # Also adopt older fingerprint-only machine files (user-level experiment).
132
+ legacy_fp_only = MACHINES_DIR / f"{fingerprint}.json"
133
+ if legacy_fp_only.exists():
134
+ try:
135
+ candidates.append((legacy_fp_only.stat().st_mtime, legacy_fp_only))
136
+ except OSError:
137
+ pass
138
+
139
+ for _, path in sorted(candidates, reverse=True):
140
+ data = _read_json_file(path)
141
+ if not data:
142
+ continue
143
+ aid = data.get("agent_id")
144
+ if not aid:
145
+ continue
146
+ # fingerprint-only file without matching project: only adopt if it
147
+ # claims this project_id or has none (best-effort).
148
+ if path.parent == MACHINES_DIR and "__" not in path.stem:
149
+ stored_pid = data.get("project_id")
150
+ if stored_pid and stored_pid != project_id:
151
+ continue
152
+ _save_machine_id(
153
+ str(aid),
154
+ project_id,
155
+ fingerprint=fingerprint,
156
+ last_seen_tool=data.get("agent_type") or data.get("last_seen_tool"),
157
+ project_name=data.get("project_name"),
158
+ )
159
+ return str(aid)
160
+ return None
161
+
162
+
163
+ def _remove_machine_id(project_id: str, fingerprint: str | None = None) -> None:
164
+ _machine_path(project_id, fingerprint).unlink(missing_ok=True)
165
+
166
+
167
+ # ---------------------------------------------------------------------------
168
+ # Backward-compatible wrappers
169
+ # ---------------------------------------------------------------------------
170
+
171
+
172
+ def _agent_id_path(agent_type: str, project_id: str | None = None) -> Path:
173
+ if project_id:
174
+ return AGENTS_DIR / f"{agent_type}__{project_id}.json"
175
+ return AGENTS_DIR / f"{agent_type}.json"
176
+
177
+
178
+ def _save_agent_id(
179
+ agent_type: str,
180
+ agent_id: str,
181
+ project_id: str | None = None,
182
+ project_name: str | None = None,
183
+ ) -> None:
184
+ if not project_id:
185
+ # Global registrations are no longer first-class; require a project.
186
+ return
187
+ _save_machine_id(
188
+ agent_id,
189
+ project_id,
190
+ last_seen_tool=agent_type,
191
+ project_name=project_name,
192
+ )
193
+
194
+
195
+ def _load_agent_id(agent_type: str, project_id: str | None = None) -> str | None:
196
+ if not project_id:
197
+ return None
198
+ _ = agent_type
199
+ return _load_machine_id(project_id)
200
+
201
+
202
+ def load_agent_id_for_active_agent() -> str | None:
203
+ """Return a cached agent UUID for this Machine (any project), or None.
204
+
205
+ Prefers the newest ``machines/<fp>__*.json`` file for this fingerprint.
206
+ """
207
+ fp = _machine_fingerprint()
208
+ if not MACHINES_DIR.exists():
209
+ # Fall back to legacy global / any agent file.
210
+ if not AGENTS_DIR.exists():
211
+ return None
212
+ newest: tuple[float, str] | None = None
213
+ for path in AGENTS_DIR.glob("*.json"):
214
+ try:
215
+ mtime = path.stat().st_mtime
216
+ data = _read_json_file(path)
217
+ except OSError:
218
+ continue
219
+ if data and data.get("agent_id"):
220
+ if newest is None or mtime > newest[0]:
221
+ newest = (mtime, str(data["agent_id"]))
222
+ return newest[1] if newest else None
223
+
224
+ newest: tuple[float, str] | None = None
225
+ for path in MACHINES_DIR.glob(f"{fp}__*.json"):
226
+ try:
227
+ mtime = path.stat().st_mtime
228
+ data = _read_json_file(path)
229
+ except OSError:
230
+ continue
231
+ if data and data.get("agent_id"):
232
+ if newest is None or mtime > newest[0]:
233
+ newest = (mtime, str(data["agent_id"]))
234
+ if newest:
235
+ return newest[1]
236
+ # fingerprint-only legacy
237
+ data = _read_json_file(MACHINES_DIR / f"{fp}.json")
238
+ if data and data.get("agent_id"):
239
+ return str(data["agent_id"])
240
+ return None
241
+
242
+
243
+ def _remove_agent_id(agent_type: str, project_id: str | None = None) -> None:
244
+ _ = agent_type
245
+ if project_id:
246
+ _remove_machine_id(project_id)
247
+
248
+
249
+ def _build_config_snapshot(agent_type: str | None = None) -> dict:
250
+ resolved = agent_type or _resolve_agent_type()
251
+ origin = build_origin()
252
+ config: dict = {
253
+ "machine_fingerprint": _machine_fingerprint(),
254
+ "working_directory": str(Path.cwd()),
255
+ "last_seen_tool": resolved,
256
+ }
257
+ for key in ("machine", "model", "tool_version"):
258
+ if val := origin.get(key):
259
+ config[key] = val
260
+ return config
261
+
262
+
263
+ def _default_agent_name(agent_type: str | None = None) -> str:
264
+ import platform
265
+
266
+ hostname = platform.node() or "unknown-host"
267
+ tool = agent_type or _resolve_agent_type()
268
+ label = _AGENT_TYPE_LABELS.get(tool, tool)
269
+ return f"{hostname} ({label})"
270
+
271
+
272
+ def _find_local_registration_for_project(
273
+ project_id: str,
274
+ agent_type: str | None = None,
275
+ ) -> tuple[str, str] | None:
276
+ """Return ``(agent_id, last_seen_tool)`` for this Machine in ``project_id``."""
277
+ resolved_type = agent_type or _resolve_agent_type()
278
+ if cached := _load_machine_id(project_id):
279
+ return cached, resolved_type
280
+ return None
281
+
282
+
283
+ def _project_headers(project_id: str) -> dict[str, str]:
284
+ return {"X-Project-Id": project_id}
285
+
286
+
287
+ def _resolve_agent_type(agent_type: str | None = None) -> str:
288
+ if agent_type:
289
+ return agent_type
290
+ try:
291
+ detected, _ = detect_agent_tool()
292
+ except Exception:
293
+ detected = None
294
+ return detected or "deepvista-cli"
295
+
296
+
297
+ def _agent_fingerprint(agent: dict | None) -> str | None:
298
+ if not agent:
299
+ return None
300
+ fp = agent.get("machine_fingerprint")
301
+ if isinstance(fp, str) and fp.strip():
302
+ return fp.strip()
303
+ config = agent.get("config") or {}
304
+ nested = config.get("machine_fingerprint")
305
+ if isinstance(nested, str) and nested.strip():
306
+ return nested.strip()
307
+ return None
308
+
309
+
310
+ def _agent_exists_on_server(ctx: click.Context, agent_id: str, project_id: str) -> bool:
311
+ data = _client(ctx).get(f"/agents/{agent_id}", extra_headers=_project_headers(project_id))
312
+ return bool(data.get("agent"))
313
+
314
+
315
+ def _sync_machine_online(
316
+ ctx: click.Context,
317
+ agent_id: str,
318
+ project_id: str,
319
+ config: dict,
320
+ ) -> None:
321
+ _client(ctx).post(
322
+ f"/agents/{agent_id}/sync",
323
+ {"status": "online", "sync_type": "manual", "config_patch": config},
324
+ extra_headers=_project_headers(project_id),
325
+ )
326
+
327
+
328
+ def _register_agent_via_api(
329
+ ctx: click.Context,
330
+ name: str,
331
+ agent_type: str,
332
+ config: dict,
333
+ *,
334
+ project_id: str,
335
+ project_name: str | None = None,
336
+ ) -> tuple[str | None, str | None]:
337
+ """POST /agents. Adopts only when returned Machine matches our fingerprint."""
338
+ local_fp = config.get("machine_fingerprint") or _machine_fingerprint()
339
+ body: dict = {
340
+ "name": name,
341
+ "agent_type": agent_type,
342
+ "config": config,
343
+ }
344
+ data = _client(ctx).post("/agents", body, extra_headers=_project_headers(project_id))
345
+ agent = data.get("agent")
346
+
347
+ def _persist(aid: str) -> str:
348
+ _save_machine_id(
349
+ aid,
350
+ project_id,
351
+ fingerprint=local_fp,
352
+ last_seen_tool=agent_type,
353
+ project_name=project_name,
354
+ )
355
+ return aid
356
+
357
+ if data.get("success") and agent and agent.get("id"):
358
+ return _persist(agent["id"]), None
359
+
360
+ if data.get("error_code") == ERROR_CODE_AGENT_ALREADY_REGISTERED and agent and agent.get("id"):
361
+ remote_fp = _agent_fingerprint(agent)
362
+ if remote_fp and remote_fp != local_fp:
363
+ return None, (
364
+ f"server returned a different machine (fingerprint {remote_fp}, "
365
+ f"local {local_fp}); backend uniqueness may be stale"
366
+ )
367
+ # Same fingerprint already in this project — adopt (possibly another
368
+ # user's prior registration of this device is unusual; fingerprint match
369
+ # means this device).
370
+ return _persist(agent["id"]), None
371
+
372
+ return None, data.get("error", "Registration failed")
373
+
374
+
375
+ def resolve_or_register_machine(
376
+ ctx: click.Context,
377
+ project_id: str,
378
+ *,
379
+ agent_type: str | None = None,
380
+ agent_role: str = "misc", # noqa: ARG001 — deprecated
381
+ project_name: str | None = None,
382
+ quiet: bool = False,
383
+ ) -> str | None:
384
+ """Resolve this Machine for ``project_id``, registering when needed.
385
+
386
+ Identity is ``(project_id, machine_fingerprint)``. Trust-but-verify:
387
+
388
+ 1. Use the local cache when the server still knows that agent id.
389
+ 2. Otherwise register (or adopt same-fingerprint row in this project).
390
+ 3. Never adopt a row whose fingerprint differs from this device.
391
+ """
392
+ _ = agent_role
393
+ resolved_type = _resolve_agent_type(agent_type)
394
+ config = _build_config_snapshot(resolved_type)
395
+ local_fp = config["machine_fingerprint"]
396
+
397
+ cached_id = _load_machine_id(project_id, local_fp)
398
+ if cached_id and _agent_exists_on_server(ctx, cached_id, project_id):
399
+ _sync_machine_online(ctx, cached_id, project_id, config)
400
+ return cached_id
401
+ if cached_id:
402
+ _remove_machine_id(project_id, local_fp)
403
+
404
+ agent_id, error = _register_agent_via_api(
405
+ ctx,
406
+ _default_agent_name(resolved_type),
407
+ resolved_type,
408
+ config,
409
+ project_id=project_id,
410
+ project_name=project_name,
411
+ )
412
+ if not agent_id:
413
+ if not quiet:
414
+ click.echo(
415
+ f" [warn] could not register machine for project {project_id}: {error or 'unknown'}",
416
+ err=True,
417
+ )
418
+ return None
419
+
420
+ _sync_machine_online(ctx, agent_id, project_id, config)
421
+ if not quiet:
422
+ click.echo(
423
+ f" registered machine {agent_id} for project {project_id} (fingerprint {local_fp})",
424
+ err=True,
425
+ )
426
+ return agent_id
427
+
428
+
429
+ def _ensure_agent_registered(ctx: click.Context, agent_type: str) -> str:
430
+ project_id = getattr(getattr(ctx, "obj", None), "project_id", None)
431
+ if project_id:
432
+ if existing := _load_machine_id(project_id):
433
+ return existing
434
+ agent_id = resolve_or_register_machine(ctx, project_id, agent_type=agent_type, quiet=True)
435
+ if agent_id:
436
+ return agent_id
437
+ output_error(1, "Auto-registration failed", f"Could not register for project {project_id}")
438
+ raise SystemExit(1)
439
+
440
+ # No working project — try any cached registration for origin tagging.
441
+ if existing := load_agent_id_for_active_agent():
442
+ return existing
443
+ output_error(
444
+ 3,
445
+ "No working project to register a Machine",
446
+ "Run `deepvista project use <id>` or pass `--project <id>`.",
447
+ )
448
+ raise SystemExit(3)
449
+
450
+
451
+ def _client(ctx: click.Context) -> DeepVistaClient:
452
+ return ctx.obj._client
453
+
454
+
455
+ def _output(ctx: click.Context, data: object, **kwargs: object) -> None:
456
+ format_output(
457
+ data,
458
+ ctx.obj.output_format,
459
+ entity_type="agent",
460
+ base_url=ctx.obj.auth_url,
461
+ project_id=ctx.obj.project_id,
462
+ **kwargs, # type: ignore[arg-type]
463
+ )
464
+
465
+
466
+ @click.group("agents")
467
+ def agents_group() -> None:
468
+ """Machine identity heartbeat for DeepVista."""
469
+
470
+
471
+ @agents_group.command("sync")
472
+ @click.argument("agent_id", required=False, default=None)
473
+ @click.option("--type", "agent_type", default=None, help="Last-seen tool label (soft metadata).")
474
+ @click.option("--status", default=None, type=click.Choice(["online", "offline", "error"]))
475
+ @click.option("--memory", default=None, help="Memory JSON to merge into config.")
476
+ @click.option("--dry-run", is_flag=True, default=False, help="Preview without making changes.")
477
+ @click.pass_context
478
+ def agents_sync(
479
+ ctx: click.Context,
480
+ agent_id: str | None,
481
+ agent_type: str | None,
482
+ status: str | None,
483
+ memory: str | None,
484
+ dry_run: bool,
485
+ ) -> None:
486
+ """Heartbeat + push state. Auto-registers this Machine for the working project.
487
+
488
+ > [!CAUTION] This is a write command — confirm with the user before executing.
489
+ """
490
+ resolved_type = agent_type
491
+ if not resolved_type:
492
+ try:
493
+ resolved_type, _ = detect_agent_tool()
494
+ except Exception:
495
+ resolved_type = None
496
+
497
+ project_id = getattr(ctx.obj, "project_id", None)
498
+
499
+ if agent_id:
500
+ resolved_id = agent_id
501
+ else:
502
+ resolved_id = _ensure_agent_registered(ctx, resolved_type or "deepvista-cli")
503
+
504
+ body: dict = {"sync_type": "manual"}
505
+ if status:
506
+ body["status"] = status
507
+
508
+ config_patch = _build_config_snapshot(resolved_type)
509
+ if memory:
510
+ try:
511
+ config_patch["memory"] = _json.loads(memory)
512
+ except _json.JSONDecodeError:
513
+ output_error(3, "Invalid --memory JSON", f"Got: {memory}")
514
+ return
515
+ if config_patch:
516
+ body["config_patch"] = config_patch
517
+
518
+ if dry_run:
519
+ _output(
520
+ ctx,
521
+ {"dry_run": True, "would": "sync machine state", "agent_id": resolved_id, "payload": body},
522
+ title="Dry Run: Sync Agent",
523
+ )
524
+ return
525
+
526
+ headers = _project_headers(project_id) if project_id else None
527
+ data = _client(ctx).post(f"/agents/{resolved_id}/sync", body, extra_headers=headers)
528
+ if not data.get("success") and data.get("error_code") == ERROR_CODE_AGENT_NOT_FOUND:
529
+ if project_id:
530
+ _remove_machine_id(project_id)
531
+ resolved_id = _ensure_agent_registered(ctx, resolved_type or "deepvista-cli")
532
+ data = _client(ctx).post(f"/agents/{resolved_id}/sync", body, extra_headers=headers)
533
+
534
+ if not data.get("success"):
535
+ output_error(1, "Sync failed", data.get("error", ""))
536
+ return
537
+ _output(ctx, data["agent"], title="Synced Agent")