fix: register embedded profiles in CLI metadata on daemon start (#546)

When HindsightEmbedded(profile="myapp") starts a daemon, the profile
was never written to metadata.json or given a .env file, making it
invisible to `hindsight-embed profile list` and other CLI commands.

Add _register_profile() to DaemonEmbedManager which saves HINDSIGHT_API_*
config to ~/.hindsight/profiles/{name}.env and registers the port in
metadata.json. Called after a successful new daemon start and when the
daemon is already running, so orphaned profiles also get registered on
next use.
This commit is contained in:
Nicolò Boschi 2026-03-12 09:39:47 +01:00 committed by GitHub
parent 0560f6260d
commit 06b0f74a48
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -241,6 +241,9 @@ class DaemonEmbedManager(EmbedManager):
live.update(panel)
live.refresh()
console.print()
# Register profile in metadata so CLI can discover it
if profile:
self._register_profile(profile, port, config)
return True
else:
log_lines.append("")
@ -307,6 +310,18 @@ class DaemonEmbedManager(EmbedManager):
console.print()
return False
def _register_profile(self, profile: str, port: int, config: dict) -> None:
"""Register a named profile in metadata so it's discoverable by the CLI.
Only saves HINDSIGHT_API_* config keys (not internal daemon keys).
Silently ignores errors to avoid blocking daemon startup.
"""
try:
api_config = {k: v for k, v in config.items() if k.startswith("HINDSIGHT_API_")}
self._profile_manager.create_profile(profile, port, api_config)
except Exception as e:
logger.debug(f"Failed to register profile '{profile}' in metadata: {e}")
def ensure_running(self, config: dict, profile: str) -> bool:
"""
Ensure daemon is running, starting it if needed.
@ -320,6 +335,9 @@ class DaemonEmbedManager(EmbedManager):
"""
if self.is_running(profile):
logger.debug(f"Daemon already running for profile '{profile}'")
if profile:
paths = self._profile_manager.resolve_profile_paths(profile)
self._register_profile(profile, paths.port, config)
return True
return self._start_daemon(config, profile)