package managedobserve import ( "context" "errors" "fmt" "net" "net/http" "os" "strings" "sync" "time" "github.com/kontext-security/kontext/internal/cedarpolicy" "github.com/kontext-security/kontext/internal/claudemanaged" "github.com/kontext-security/kontext/internal/deviceid" "github.com/kontext-security/kontext/internal/diagnostic" "github.com/kontext-security/kontext/internal/endpointconfig" "github.com/kontext-security/kontext/internal/guard/app/server" guardhookruntime "github.com/kontext-security/kontext/internal/guard/hookruntime" "github.com/kontext-security/kontext/internal/guard/stepsafety" "github.com/kontext-security/kontext/internal/guard/store/sqlite" "github.com/kontext-security/kontext/internal/installation" "github.com/kontext-security/kontext/internal/instancelock" "github.com/kontext-security/kontext/internal/ledgerping" "github.com/kontext-security/kontext/internal/managedconfig" "github.com/kontext-security/kontext/internal/managedstream" "github.com/kontext-security/kontext/internal/payloadcapture" "github.com/kontext-security/kontext/internal/privacyfolder" "github.com/kontext-security/kontext/internal/runtimehost" "github.com/kontext-security/kontext/internal/tooloutput" "github.com/kontext-security/kontext/pkg/agentauthority" ) type DaemonOptions struct { streamCaptureConfiguration func() payloadcapture.RuntimeConfiguration SocketPath string DBPath string // NoCodexRoute keeps the daemon from routing Codex through its proxy, // for a daemon run by hand next to a test that routes Codex itself. NoCodexRoute bool IdleTimeout time.Duration StreamStatePath string StreamInterval time.Duration StreamHTTPClient *http.Client CedarPolicyCachePath string EndpointConfigCachePath string // PolicyRefreshInterval and PolicyHTTPClient apply to the Cedar policy // refresh loop (test knobs; zero values use defaults). PolicyRefreshInterval time.Duration PolicyHTTPClient *http.Client EndpointConfigRefreshInterval time.Duration EndpointConfigHTTPClient *http.Client ToolOutputHTTPClient *http.Client AgentInventoryInterval time.Duration Diagnostic diagnostic.Logger // BinaryVersion is this process's linked CLI version, used for startup // logging, daemon status, and the ledger's device.cli_version fact. BinaryVersion string // FallbackDeploymentVersion is reported to the ledger when no MDM // deployment-version marker exists (self-serve brew installs). FallbackDeploymentVersion string HomebrewUpdater func(context.Context, managedconfig.LoadedConfig, diagnostic.Logger) <-chan struct{} BinaryWatchdog func(context.Context, diagnostic.Logger) <-chan struct{} // annotationRewrite and ledgerPrune replace rewriteAnnotationTimes and // pruneExportedLedger in tests. annotationRewrite func(ctx context.Context, dbPath string) (int, error) ledgerPrune func(ctx context.Context, opts DaemonOptions, dbPath string) error // stepSafety replaces the embedded model. stepSafety *stepsafety.Evaluator } var ( managedSettingsDropInPath = claudemanaged.DefaultManagedSettingsDropInPath() managedSettingsFilePath = claudemanaged.DefaultManagedSettingsPath() claudeUserSettingsPath = func() (string, error) { home, err := os.UserHomeDir() if err != nil { return "", err } return claudemanaged.UserSettingsPathNoCreate(home) } ) // instanceLockWait covers a replacement that starts while the daemon it // replaces is still on its way out. var instanceLockWait = 5 * time.Second // holdInstanceLock keeps one daemon on a socket: a live holder of the lock // refuses the start, and so does anything still answering on the socket once the // lock is held. A daemon that was leaving has gone by now, so what answers is // staying, such as a daemon from a release that takes no lock. func holdInstanceLock(socketPath string, log diagnostic.Logger) (func(), error) { release, err := takeInstanceLock(socketPath, log) if err != nil { return nil, err } if socketAnswers(socketPath, log) { release() return nil, fmt.Errorf("another process answers on %s", socketPath) } return release, nil } // socketAnswers reports whether something lives on the socket. A connect that // is accepted says yes, and so does one that waits or finds the queue full, // which is a busy daemon. Other failures read as no: a crash leaves the file // behind with nothing listening, and that must not keep the next daemon out. func socketAnswers(socketPath string, log diagnostic.Logger) bool { conn, err := net.DialTimeout("unix", socketPath, time.Second) if err != nil { log.Printf("managed-observe socket check: %v\n", err) return connectFailureMeansBusy(err) } _ = conn.Close() return true } // connectFailureMeansBusy is true for a full queue as well: Go counts EAGAIN, the // answer on Linux, as a timeout. func connectFailureMeansBusy(err error) bool { var netErr net.Error return errors.As(err, &netErr) && netErr.Timeout() } // takeInstanceLock returns a no-op if the lock can't be taken at all, such as in // a socket folder on a filesystem without file locks. The daemon then runs with // only the socket check, because with no daemon every hook fails open. func takeInstanceLock(socketPath string, log diagnostic.Logger) (func(), error) { lock, err := instancelock.Acquire(instancelock.PathForSocket(socketPath), instanceLockWait) if err == nil { return lock.Release, nil } var held *instancelock.HeldError if errors.As(err, &held) { return nil, fmt.Errorf("another daemon serves %s: %w", socketPath, err) } logAlways(log, "managed-observe daemon running without its instance lock: %v\n", err) return func() {}, nil } func RunDaemon(ctx context.Context, opts DaemonOptions) error { started := time.Now() binaryVersion := opts.BinaryVersion if binaryVersion == "" { binaryVersion = "dev" } opts.BinaryVersion = binaryVersion logAlways(opts.Diagnostic, "managed-observe daemon %s (pid %d) started\n", binaryVersion, os.Getpid()) healLaunchAgentPriority(DefaultLabel(), opts.Diagnostic) loadedConfig, err := managedconfig.Load() if err != nil { if errors.Is(err, managedconfig.ErrNotManaged) { return err } return fmt.Errorf("load managed config: %w", err) } if expected := strings.TrimSpace(os.Getenv(EnvExpectedConfigScope)); expected != "" && expected != string(loadedConfig.Scope) { // An MDM config appeared after this agent was installed (system scope // outranks user scope). Park instead of serving the wrong config — // exiting would just make launchd KeepAlive restart-loop us. fmt.Fprintf(os.Stderr, "managed config scope is %q but this agent was installed for %q — parking; run `kontext setup --uninstall` to remove this agent\n", loadedConfig.Scope, expected) <-ctx.Done() return nil } if err := requireManagedHooksForLegacyCowork(loadedConfig.Config, loadedConfig.Scope); err != nil { return err } // Trimmed as the host trims it before it binds: this path names the lock too. socketPath := strings.TrimSpace(opts.SocketPath) if socketPath == "" { socketPath = DefaultSocketPath() } if err := EnsureSocketDir(socketPath); err != nil { return fmt.Errorf("prepare managed observe socket dir: %w", err) } // Before anything else that changes state: a second daemon must not migrate // the config, open the store or take the socket over. The socket's folder // comes first because the lock lives there, under that folder's owner check. releaseInstance, err := holdInstanceLock(socketPath, opts.Diagnostic) if err != nil { return err } defer releaseInstance() loadedConfig = migrateSelfServeModeToRemote(loadedConfig, opts.Diagnostic) installationState, err := installation.EnsureFile(installationPathForScope(loadedConfig.Scope)) if err != nil { return fmt.Errorf("ensure installation identity: %w", err) } dbPath := opts.DBPath if dbPath == "" { dbPath = DefaultDBPath() } _, err = managedconfig.ResolveInstallToken(ctx, loadedConfig.Config.Credentials.InstallTokenRef) if err != nil { // Leave a breadcrumb: under launchd this exit is otherwise invisible // (doctor would only see "daemon: not running" with no cause). A // locked login keychain at boot is the typical trigger. if breadcrumbErr := WriteStartupError(dbPath, err.Error()); breadcrumbErr != nil { opts.Diagnostic.Printf("write startup-error breadcrumb: %v\n", breadcrumbErr) } return fmt.Errorf("resolve install token: %w", err) } // Token resolved — clear any stale startup breadcrumb from a prior boot. if previous := LoadAuthError(dbPath); previous != nil && previous.Kind == "startup" { if err := ClearAuthError(dbPath); err != nil { opts.Diagnostic.Printf("clear startup-error breadcrumb: %v\n", err) } } if err := cleanupStaleSessions(ctx, dbPath, idleTimeoutOrDefault(opts.IdleTimeout)); err != nil { opts.Diagnostic.Printf("managed observe cleanup: %v\n", err) } // A mode this build cannot name means the install is newer than the running // binary — a downgrade. The config loader already fell back to observe so // the daemon still boots and still reports; this is the only place that // says why, so it logs unconditionally rather than behind a diagnostic // flag. if unsupported := loadedConfig.Config.UnsupportedMode; unsupported != "" { logAlways(opts.Diagnostic, "managed config declares mode %q, which this build (%s) does not implement; running in %q — the installed binary is older than the install that wrote this config\n", unsupported, binaryVersion, loadedConfig.Config.Mode) } // The deployment-level mode from managed.json drives every hook edge: // observe records would-decisions, enforce returns real denies, remote // defers to the fetched policy deployment's rollout mode so the posture // can be flipped from the dashboard without touching the install. mode, err := guardhookruntime.ParseMode(loadedConfig.Config.Mode) if err != nil { return fmt.Errorf("parse managed mode: %w", err) } cedarEnforcement := server.CedarEnforcementOff switch mode { case guardhookruntime.ModeEnforce: cedarEnforcement = server.CedarEnforcementStatic case guardhookruntime.ModeRemote: cedarEnforcement = server.CedarEnforcementRemote } cedarCachePath := opts.CedarPolicyCachePath if cedarCachePath == "" { cedarCachePath = cedarpolicy.DefaultCachePathForDB(dbPath) } cedarCache := cedarpolicy.NewCache(cedarCachePath, 0) if err := cedarCache.Load(); err != nil { cedarCache.MarkInvalid(err) opts.Diagnostic.Printf("cedar policy cache load: %v\n", err) } else if status := cedarCache.Current().Status; status.CatalogMismatch { opts.Diagnostic.Printf("cedar policy cache load: %s\n", status.LastError) } cedarClient, err := cedarpolicy.NewClient(loadedConfig.Config.CloudURL, opts.PolicyHTTPClient) if err != nil { return fmt.Errorf("configure cedar policy client: %w", err) } cedarClient.Diagnostic = opts.Diagnostic endpointConfigCachePath := opts.EndpointConfigCachePath if endpointConfigCachePath == "" { endpointConfigCachePath = endpointconfig.DefaultCachePathForDB(dbPath) } endpointConfigCache := endpointconfig.NewCacheForDB(endpointConfigCachePath, dbPath) if err := endpointConfigCache.Load(); err != nil { endpointConfigCache.MarkInvalid(err) opts.Diagnostic.Printf("endpoint configuration cache load: %v\n", err) } endpointConfigClient, err := endpointconfig.NewClient(loadedConfig.Config.CloudURL, opts.EndpointConfigHTTPClient) if err != nil { return fmt.Errorf("configure endpoint configuration client: %w", err) } installTokenSource := func(refreshCtx context.Context) (string, error) { loaded, err := managedconfig.Load() if err != nil { return "", err } return managedconfig.ResolveInstallToken(refreshCtx, loaded.Config.Credentials.InstallTokenRef) } // Tool output compaction stays off until a confirmed configuration turns // it on: an unreachable, invalid or older configuration reads as off. compactionClient, err := tooloutput.NewClient(loadedConfig.Config.CloudURL, installationState.InstallationID, installTokenSource, opts.ToolOutputHTTPClient) if err != nil { return fmt.Errorf("configure tool output compaction client: %w", err) } codexReportClient, err := newCodexReportClient(loadedConfig.Config.CloudURL, installationState.InstallationID, installTokenSource, nil) if err != nil { return fmt.Errorf("configure codex route reports: %w", err) } compactor := tooloutput.New(tooloutput.Options{ Client: compactionClient, Enabled: func() bool { return endpointConfigCache.Current().Config.ToolOutputCompactionEnabled() }, Dir: tooloutput.DefaultDirForDB(dbPath), Diagnostic: opts.Diagnostic, }) host, err := runtimehost.Start(ctx, runtimehost.Options{ AgentName: managedconfig.Agent, DBPath: dbPath, SocketPath: socketPath, CedarPolicies: cedarCache, CedarEnforcement: cedarEnforcement, Mode: mode, Diagnostic: opts.Diagnostic, SkipInitialSession: true, StepSafety: opts.stepSafety, // Async ingest: non-blocking hooks (PostToolUse, session lifecycle) // are acked immediately and written in the background. Synchronous // writes queue on the store's single SQLite connection, and under a // concurrent subagent burst that queueing blew the hook connection // deadline — Claude Code timed the hook out and the event was lost // (ENG-474). Decision-gating hooks (PreToolUse, UserPromptSubmit) // stay synchronous, and Shutdown drains pending writes. // // Decision recording is deferred the same way: the hook still waits // for the policy verdict (Cedar is in-memory and fast), but not for // classifier inference or the guard.db write. On a long-running // install the first write after idle drags cold pages of a large // database back from disk for seconds, which the hook client reads // as a dead daemon, so the call fails open without a policy check. // Close drains pending writes before the store shuts. AsyncDecisionRecording: true, CompactToolOutput: compactor.Compact, OffersCompaction: compactor.Offers, CodexProxy: codexProxyAnswer(compactor), ObserveEvent: compactor.Observe, HoldFor: enrolmentHold(started, enrolmentWait(opts.Diagnostic), cedarEnforcement, cedarCache), }) if err != nil { return err } stopDeadline := &stopClock{} defer func() { closeCtx, cancel := stopDeadline.context() defer cancel() err := host.Close(closeCtx) if left := host.PendingRecords(); left > 0 { logAlways(opts.Diagnostic, "managed-observe daemon stopping with %d records not written: %v\n", left, err) } else if err != nil { logAlways(opts.Diagnostic, "managed-observe daemon stop: %v\n", err) } }() if err := WriteDaemonStatus(dbPath, binaryVersion); err != nil { opts.Diagnostic.Printf("write daemon-status breadcrumb: %v\n", err) } // Persisted endpoint configuration is conditional state only. Capture stays // at the privacy-safe default until this process receives a confirming 200 // or matching 304 from the independent configuration endpoint. opts.EndpointConfigCachePath = endpointConfigCachePath opts.streamCaptureConfiguration = func() payloadcapture.RuntimeConfiguration { return captureConfiguration(endpointConfigCache.Current()) } host.SetPayloadCaptureConfiguration(captureConfiguration(endpointConfigCache.Current())) policyCtx, stopPolicyRefresh := context.WithCancel(ctx) var background sync.WaitGroup policyReady := make(chan struct{}) authorityAvailable := make(chan struct{}, 1) authorityResend := make(chan struct{}, 1) policyRefreshNow := make(chan struct{}, 1) enrolment := &enrolmentRefresh{cache: cedarCache, now: policyRefreshNow, left: maxEnrolmentRefreshes} cedarRefresher := cedarpolicy.Refresher{ InitialRefreshDone: policyReady, RefreshNow: policyRefreshNow, OnAuthorityScanAvailable: func() { select { case authorityResend <- struct{}{}: default: } select { case authorityAvailable <- struct{}{}: default: } }, Client: cedarClient, Cache: cedarCache, Diagnostic: opts.Diagnostic, TokenSource: installTokenSource, InstallationID: installationState.InstallationID, Interval: opts.PolicyRefreshInterval, } background.Add(1) go func() { defer background.Done() cedarRefresher.Run(policyCtx) }() var codexRouting *codexRouting if !opts.NoCodexRoute { codexRouting = newCodexRouting(dbPath, socketPath, opts.Diagnostic, codexReportClient.send) } endpointConfigRefresher := endpointconfig.Refresher{ Client: endpointConfigClient, Cache: endpointConfigCache, Diagnostic: opts.Diagnostic, TokenSource: installTokenSource, InstallationID: installationState.InstallationID, Interval: opts.EndpointConfigRefreshInterval, OnChanged: func(snapshot endpointconfig.Snapshot) { host.SetPayloadCaptureConfiguration(captureConfiguration(snapshot)) codexRouting.follow(snapshot) }, } background.Add(1) go func() { defer background.Done() endpointConfigRefresher.Run(policyCtx) }() background.Add(1) go func() { defer background.Done() compactor.Run(policyCtx) }() background.Add(1) go func() { defer background.Done() codexRouting.run(policyCtx) }() // A rescan after hook changes refreshes the authority report too, so a // newly hooked agent does not wait for the hourly authority scan. streamStatePath := opts.StreamStatePath if streamStatePath == "" { streamStatePath = managedstream.DefaultStatePathForDB(dbPath) } inventoryStore := managedstream.OpenInventoryStore(managedstream.InventoryStatePath(streamStatePath), installationState.InstallationID) inventoryHolder := &agentInventoryHolder{scope: loadedConfig.Scope, store: inventoryStore, rescan: make(chan struct{}, 1), afterRescan: func() { select { case authorityAvailable <- struct{}{}: default: } }} inventoryReady := make(chan struct{}) background.Add(1) go func() { defer background.Done() inventoryHolder.run(policyCtx, opts, dbPath, inventoryReady) }() authorityHolder := &authorityHolder{available: authorityAvailable, resend: authorityResend, enabled: func() bool { return cedarCache.Current().AuthorityScan }, known: func() bool { return cedarCache.Current().AuthorityScanKnown }} authorityHolder.scanner.BeginRead = authorityIOPolicy(opts.Diagnostic) authorityHolder.discard = func() { if err := inventoryStore.DiscardAuthority(); err != nil { opts.Diagnostic.Printf("authority scan: discard pending results: %v\n", err) } } authorityHolder.record = func(report agentauthority.Report) { if err := inventoryStore.RecordAuthority(report); err != nil { opts.Diagnostic.Printf("authority scan: record scan: %v\n", err) } } server.SetConnectorScan(authorityHolder.Fact) authorityReady := make(chan struct{}) background.Add(1) go func() { defer background.Done() for _, ready := range []chan struct{}{inventoryReady, policyReady} { select { case <-ready: case <-policyCtx.Done(): return } } authorityHolder.run(policyCtx, inventoryHolder, authorityReady) }() streamCtx, stopStream := context.WithCancel(ctx) // The hook socket is serving by now. On the first start after an upgrade // this pass takes seconds to minutes on a large store, more than a hook // waits for the socket, which is why it isn't part of opening the store. annotationsRewritten := make(chan struct{}) background.Add(1) go func() { defer background.Done() if rewriteAnnotationsUntilDone(streamCtx, opts, dbPath, annotationRewriteRetry) { close(annotationsRewritten) } }() streamErr := make(chan error, 1) background.Add(1) go func() { defer background.Done() // Keep the first heartbeat from racing the initial scan, and the first // export from reading annotations the pass hasn't rewritten yet. for _, ready := range []chan struct{}{authorityReady, annotationsRewritten} { select { case <-ready: case <-streamCtx.Done(): return } } streamErr <- runManagedStream(streamCtx, opts, dbPath, installationState.InstallationID, inventoryHolder, authorityHolder, enrolment.afterAcceptedPost) }() defer func() { stopPolicyRefresh() stopStream() stopDeadline.start() // Past this point the queued records need the time more than a shadow // score does. time.AfterFunc(daemonStopBudget/2, host.StopScoring) waitForBackgroundWork(&background, opts.Diagnostic) }() background.Add(1) go func() { defer background.Done() runHookWatch(policyCtx, loadedConfig, opts.Diagnostic, inventoryHolder.requestRescan) }() startUpdater := opts.HomebrewUpdater if startUpdater == nil { startUpdater = startHomebrewUpdater } upgraded := startUpdater(ctx, loadedConfig, opts.Diagnostic) startWatchdog := opts.BinaryWatchdog if startWatchdog == nil { startWatchdog = startBinaryWatchdog } binaryReplaced := startWatchdog(ctx, opts.Diagnostic) idleTimeout := opts.IdleTimeout if idleTimeout == 0 { idleTimeout = DefaultIdleTimeout() } prune := opts.ledgerPrune if prune == nil { prune = pruneExportedLedger } background.Add(1) go func() { defer background.Done() runCleanup(streamCtx, opts, dbPath, idleTimeout, prune, annotationsRewritten) }() for { select { case <-ctx.Done(): return nil case _, ok := <-upgraded: if ok { return nil } upgraded = nil case _, ok := <-binaryReplaced: if ok { return nil } binaryReplaced = nil case err := <-streamErr: if err != nil { logAlways(opts.Diagnostic, "managed stream exited: %v\n", err) return fmt.Errorf("managed stream failed: %w", err) } return nil } } } // runCleanup closes idle sessions and prunes the ledger on a timer. It runs beside // the daemon's main loop: on a busy store a tick can outlast the whole stop budget, // and a stop has to start without waiting for it. func runCleanup(ctx context.Context, opts DaemonOptions, dbPath string, idleTimeout time.Duration, prune func(context.Context, DaemonOptions, string) error, annotationsRewritten <-chan struct{}) { cleanup := time.NewTicker(cleanupInterval(idleTimeout)) defer cleanup.Stop() for { select { case <-ctx.Done(): return case <-cleanup.C: if err := cleanupStaleSessions(ctx, dbPath, idleTimeout); err != nil { opts.Diagnostic.Printf("managed observe cleanup: %v\n", err) } // The prune mirrors the export's reading of annotations, so it waits // for the same pass: a trimmed stamp could read as already uploaded. select { case <-annotationsRewritten: if err := prune(ctx, opts, dbPath); err != nil { opts.Diagnostic.Printf("managed observe ledger prune: %v\n", err) } default: } } } } // annotationRewriteRetry is how long a failed annotation rewrite waits before // it tries again. const annotationRewriteRetry = 30 * time.Second // rewriteAnnotationsUntilDone reports whether the rewrite finished; it returns // false only when ctx ended first. A failure is retried and not skipped: the // export can't read these tables safely before the rewrite has finished, and // whatever stops the rewrite, such as a locked or full disk, would stop the // export's own open as well. func rewriteAnnotationsUntilDone(ctx context.Context, opts DaemonOptions, dbPath string, retry time.Duration) bool { rewrite := opts.annotationRewrite if rewrite == nil { rewrite = rewriteAnnotationTimes } lastErr := "" for { started := time.Now() rewritten, err := rewrite(ctx, dbPath) if rewritten > 0 { logAlways(opts.Diagnostic, "rewrote %d annotation timestamps in %s\n", rewritten, time.Since(started).Round(time.Millisecond)) } if err == nil { return true } if ctx.Err() != nil { return false } if msg := err.Error(); msg != lastErr { logAlways(opts.Diagnostic, "annotation timestamp rewrite: %v; export waits and it tries again\n", err) lastErr = msg } select { case <-time.After(retry): case <-ctx.Done(): return false } } } // rewriteAnnotationTimes uses a store of its own: the hook path's store has one // connection, and pages queued on it would hold up decisions. func rewriteAnnotationTimes(ctx context.Context, dbPath string) (int, error) { store, err := sqlite.OpenStore(dbPath) if err != nil { return 0, err } defer store.Close() return store.RewriteAnnotationCreatedAt(ctx) } func captureConfiguration(snapshot endpointconfig.Snapshot) payloadcapture.RuntimeConfiguration { return payloadcapture.SafeRuntimeConfiguration(payloadcapture.RuntimeConfiguration{ ConfiguredMode: snapshot.Configured.PayloadCaptureMode, EffectiveMode: snapshot.Config.PayloadCaptureMode, ConfigIdentity: snapshot.ConfigIdentity, Confirmed: snapshot.Confirmed, FallbackReason: snapshot.FallbackReason, // Off until a directive is confirmed: SafeRuntimeConfiguration also // clears it for an unconfirmed snapshot. WorkTypeClassification: snapshot.Config.WorkTypeClassificationEnabled(), }) } func requireManagedHooksForLegacyCowork(cfg managedconfig.Config, scope managedconfig.Scope) error { if !cfg.LegacyCoworkEnabled { return nil } foundHooks := false for _, path := range []string{managedSettingsDropInPath, managedSettingsFilePath} { state, err := managedObserveHooksState(path) if err != nil { return fmt.Errorf("check Claude Code managed hooks for cowork_enabled: %w", err) } if state.disabled { return fmt.Errorf("cowork_enabled is set but Claude Code hooks are disabled at %s; remove disableAllHooks before starting managed observe", path) } if state.hasHooks { foundHooks = true } // Older self-serve installs may still carry cowork_enabled and the // original five hooks. Keep those working while the async migration // requests approval for the two new completion observers. Health still // requires the complete current hook set. if scope == managedconfig.ScopeUser && path == managedSettingsDropInPath && !state.hasHooks { data, err := os.ReadFile(path) if err == nil && claudemanaged.IsManagedSettingsDropIn(data) { foundHooks = true } } } if foundHooks { return nil } return fmt.Errorf("cowork_enabled is set but Claude Code managed hooks are missing at %s or %s; run `kontext setup` or install the managed-settings drop-in before starting managed observe", managedSettingsDropInPath, managedSettingsFilePath) } // managedObserveHooksFact reduces the two managed-settings locations to the // device fact reported with every heartbeat batch. Present requires the full // Kontext hook set in at least one location and no disableAllHooks anywhere: // Claude Code merges drop-ins into the managed settings, so a disable in // either file switches off hooks the other file declares. A read error yields // ok=false — the fact is omitted from the batch rather than reported as // missing, so a daemon that cannot read the folder (sandboxing, permissions) // never files a false "hooks deleted" alarm. Self-serve installs keep their // hooks in the user's Claude settings, which count for the user scope only. func managedObserveHooksFact(scope managedconfig.Scope) (managedstream.HooksFact, bool) { user := "" if scope == managedconfig.ScopeUser { path, err := claudeUserSettingsPath() // The daemon lacks the terminal's folder access: reading settings that // resolve into ~/Documents or iCloud would prompt in the background. if err != nil || privacyfolder.Guarded("", path) { return managedstream.HooksFact{}, false } user = path } return claudeHooksFact(user) } // machineClaudeHooksFact covers Claude's machine-wide managed settings, the // only Claude configuration Cowork sessions read. func machineClaudeHooksFact() (managedstream.HooksFact, bool) { return claudeHooksFact("") } // claudeHooksFact merges the events each source covers, as Claude merges hooks // across its settings. disableAllHooks in managed settings, as the last // machine-wide value leaves it, switches off every source; in the user's // settings it switches off only the user's hooks. func claudeHooksFact(userSettings string) (managedstream.HooksFact, bool) { covered := map[string]bool{} var machine [][]byte userDisabled := false paths := append(claudemanaged.MachineSettingsFiles(managedSettingsDropInPath, managedSettingsFilePath), userSettings) for _, path := range paths { if path == "" { continue } // A machine-wide file linked into ~/Documents or iCloud would prompt // for folder access in the background. if privacyfolder.Guarded("", path) { return managedstream.HooksFact{}, false } data, err := os.ReadFile(path) if errors.Is(err, os.ErrNotExist) { continue } if err != nil && (path == managedSettingsDropInPath || path == managedSettingsFilePath || path == userSettings) { return managedstream.HooksFact{}, false } if err != nil { continue // another product's drop-in Claude cannot read either } if path == userSettings { if claudemanaged.DisablesAllHooks(data) { userDisabled = true continue } } else { machine = append(machine, data) } // Hooks that name a binary an upgrade or uninstall removed cannot run. for event := range claudemanaged.RunnableEvents(data) { covered[event] = true } } managedDisabled := claudemanaged.MachineHooksDisabled(machine) present := !managedDisabled && len(covered) == len(claudemanaged.SupportedEvents) return managedstream.HooksFact{Present: present, DisabledAllHooks: managedDisabled || userDisabled && !present}, true } type managedObserveHooksStatus struct { disabled bool hasHooks bool } func managedObserveHooksState(path string) (managedObserveHooksStatus, error) { data, err := os.ReadFile(path) if errors.Is(err, os.ErrNotExist) { return managedObserveHooksStatus{}, nil } if err != nil { return managedObserveHooksStatus{}, err } return managedObserveHooksStatus{ disabled: claudemanaged.DisablesAllHooks(data), hasHooks: claudemanaged.HasManagedObserveHooks(data), }, nil } // deploymentVersionWithFallback resolves the version reported with each // ledger batch: the MDM package marker wins; brew installs have none and // report the CLI's own version instead. Evaluated per flush so a package // update under a running daemon is picked up. func deploymentVersionWithFallback(fallback string) func() string { return func() string { if v := managedconfig.DeploymentVersion(); v != "" { return v } return fallback } } // installationPathForScope ties identity scope to config scope: a system // (MDM) config never reads a user identity and vice versa. The env override // (KONTEXT_INSTALLATION_STATE, honored by PathFromEnv) always wins, and the // enterprise default is byte-identical to the pre-self-serve behavior. func installationPathForScope(scope managedconfig.Scope) string { if strings.TrimSpace(os.Getenv(installation.EnvPath)) != "" { return installation.PathFromEnv() } if scope == managedconfig.ScopeUser { if path := installation.UserPath(); path != "" { return path } } return installation.PathFromEnv() } func loadManagedConfig(ctx context.Context) (managedconfig.LoadedConfig, string, error) { loadedConfig, err := managedconfig.Load() if err != nil { if errors.Is(err, managedconfig.ErrNotManaged) { return managedconfig.LoadedConfig{}, "", err } return managedconfig.LoadedConfig{}, "", fmt.Errorf("load managed config: %w", err) } installToken, err := managedconfig.ResolveInstallToken(ctx, loadedConfig.Config.Credentials.InstallTokenRef) if err != nil { return managedconfig.LoadedConfig{}, "", fmt.Errorf("resolve install token: %w", err) } return loadedConfig, installToken, nil } // envNoDeviceKey switches off device-key reporting entirely — the escape // hatch for a backend that rejects the field before its ingest side deploys. const envNoDeviceKey = "KONTEXT_NO_DEVICE_KEY" // deviceKeyPingTimeout bounds the workspace ping inside a flush. The flush // interval is ~10s; a hung ping must not eat the whole cycle. const deviceKeyPingTimeout = 10 * time.Second // Test seams (repo convention, cf. updater.go's runCommand) so device-key // resolution never touches this machine's ioreg or a hosted backend. var ( platformUUIDFn = deviceid.PlatformUUID workspacePingFn = ledgerping.Ping ) // deviceKeySource resolves the device reconciliation key and caches it per // config identity. Confined to the managed-stream goroutine, so no locking. // // The cache is keyed by (cloudURL, token), not held for the daemon's // lifetime: a system-scope config can be rewritten in place to a DIFFERENT // workspace under a running daemon (MDM re-enrollment does not restart this // agent), and serving that workspace the key derived for the previous one // would hand it the previous workspace's stable pseudonym — precisely the // cross-workspace linkability the per-organization HMAC exists to prevent. // A token rotation within one workspace also invalidates and merely costs // one extra ping; the re-derived key is identical. type deviceKeySource struct { key string cloudURL string token string resolved bool } // resolve returns the key for this flush's config identity, deriving it on // first use or whenever the identity changed. Empty means "no key this // flush" and the batch's device envelope simply omits the field. Every // failure is retried on a later flush — the first flushes after login race // the network coming up, and even the local ioreg read can fail transiently // under resource pressure — so nothing short of the kill switch disables // reporting for the daemon's lifetime. func (s *deviceKeySource) resolve(ctx context.Context, cloudURL, token, installationID string, client *http.Client, diag diagnostic.Logger) string { if strings.TrimSpace(os.Getenv(envNoDeviceKey)) != "" { return "" } if s.resolved && s.cloudURL == cloudURL && s.token == token { return s.key } uuid, err := platformUUIDFn(ctx) if err != nil { diag.Printf("device key: %v\n", err) return "" } pingCtx, cancel := context.WithTimeout(ctx, deviceKeyPingTimeout) defer cancel() ping, err := workspacePingFn(pingCtx, client, cloudURL, token, installationID) if err != nil { diag.Printf("device key: workspace ping: %v\n", err) return "" } key, err := deviceid.Key(uuid, ping.OrganizationID) if err != nil { diag.Printf("device key: %v\n", err) return "" } s.key = key s.cloudURL = cloudURL s.token = token s.resolved = true return s.key } func runManagedStream(ctx context.Context, opts DaemonOptions, dbPath, installationID string, inventoryHolder *agentInventoryHolder, authorityHolder *authorityHolder, afterAcceptedPost func()) error { interval := opts.StreamInterval if interval == 0 { interval = managedstream.DefaultIntervalFromEnv() } deviceKeys := &deviceKeySource{} var consecutiveAuthFailures, consecutiveFlushFailures int flush := func() { err := flushManagedStream(ctx, opts, dbPath, installationID, deviceKeys, inventoryHolder, authorityHolder, afterAcceptedPost) if err == nil { consecutiveAuthFailures = 0 consecutiveFlushFailures = 0 return } opts.Diagnostic.Printf("managed stream flush: %v\n", err) status, ok := managedstream.AuthFailureStatus(err) if ok { consecutiveFlushFailures = 0 consecutiveAuthFailures++ if managedstream.ShouldReportAuthFailure(consecutiveAuthFailures) { writeStreamAuthFailure(opts, dbPath, status) } return } consecutiveAuthFailures = 0 consecutiveFlushFailures++ if managedstream.ShouldReportFailure(consecutiveFlushFailures) { logAlways(opts.Diagnostic, "managed stream flush failing (%d consecutive): %v\n", consecutiveFlushFailures, err) } } flush() ticker := time.NewTicker(interval) defer ticker.Stop() for { select { case <-ctx.Done(): return nil case <-ticker.C: flush() } } } func flushManagedStream(ctx context.Context, opts DaemonOptions, dbPath, installationID string, deviceKeys *deviceKeySource, inventoryHolder *agentInventoryHolder, authorityHolder *authorityHolder, afterAcceptedPost func()) error { loadedConfig, installToken, err := loadManagedConfig(ctx) if err != nil { return fmt.Errorf("managed stream config reload: %w", err) } // Resolved once per flush, not per payload: a Flush drains the backlog in // pages and builds a payload for each, and a hung ping retried per page // would stack its timeout onto every page of the drain. deviceKey := deviceKeys.resolve(ctx, loadedConfig.Config.CloudURL, installToken, installationID, opts.StreamHTTPClient, opts.Diagnostic) accepted := false err = managedstream.Flush(ctx, managedstream.Options{ AllowHistoricalTurnRecovery: loadedConfig.Config.RecoverHistoricalTurnPrompts, EndpointConfigCachePath: opts.EndpointConfigCachePath, CaptureConfiguration: opts.streamCaptureConfiguration, DBPath: dbPath, StatePath: opts.StreamStatePath, CloudURL: loadedConfig.Config.CloudURL, InstallationID: installationID, InstallToken: installToken, DeviceLabel: loadedConfig.Config.Device.Label, UserEmail: loadedConfig.Config.Device.UserEmail, DeploymentVersion: deploymentVersionWithFallback(opts.FallbackDeploymentVersion), CLIVersion: opts.BinaryVersion, HooksFact: func() (managedstream.HooksFact, bool) { return managedObserveHooksFact(loadedConfig.Scope) }, AgentsFact: inventoryHolder.Fact, Inventory: inventoryHolder.store, AuthorityFact: authorityHolder.Fact, AuthorityAvailable: authorityHolder.resend, AuthorityScanKnown: authorityHolder.known, DeviceKey: func() string { return deviceKey }, HTTPClient: opts.StreamHTTPClient, Diagnostic: opts.Diagnostic, OnFlushSuccess: func() { // Once per flush, on the first accepted post: a flush can make several, // and draining a backlog shouldn't hold the policy back. if !accepted { accepted = true afterAcceptedPost() } if err := ClearAuthError(dbPath); err != nil { opts.Diagnostic.Printf("clear auth-error breadcrumb: %v\n", err) } }, }) return err } func writeStreamAuthFailure(opts DaemonOptions, dbPath string, status int) { // Unconditional stderr (Diagnostic is env-gated and would be silent under // launchd) plus a breadcrumb for `kontext doctor`. target := "hosted API" if loadedConfig, err := managedconfig.Load(); err == nil && strings.TrimSpace(loadedConfig.Config.CloudURL) != "" { target = loadedConfig.Config.CloudURL } if status == http.StatusConflict { fmt.Fprintln(os.Stderr, "personal key is bound to another Mac; run kontext setup with a fresh command from Get started") } else { fmt.Fprintf(os.Stderr, "Kontext install token rejected by %s (HTTP %d). It may have been revoked — run `kontext setup` with a new token from the dashboard.\n", target, status) } if err := WriteAuthError(dbPath, status); err != nil { opts.Diagnostic.Printf("write auth-error breadcrumb: %v\n", err) } } func idleTimeoutOrDefault(idleTimeout time.Duration) time.Duration { if idleTimeout == 0 { return DefaultIdleTimeout() } return idleTimeout } func cleanupStaleSessions(ctx context.Context, dbPath string, idleTimeout time.Duration) error { store, err := sqlite.OpenStore(dbPath) if err != nil { return err } defer store.Close() return store.CloseStaleDaemonObservedSessions(ctx, time.Now().UTC().Add(-idleTimeout)) } const ( // ledgerRetention keeps recent history local even after the hosted ledger // accepted it; only older rows are pruned. ledgerRetention = 30 * 24 * time.Hour // ledgerPruneBatch bounds each cleanup tick's delete transaction, so a // large backlog drains over hours instead of holding the write lock. ledgerPruneBatch = 1000 ) // pruneExportedLedger deletes old ledger rows and turns the hosted ledger has // accepted. Without it guard.db only grows. No action is pruned until the // managed stream has persisted an export cursor. func pruneExportedLedger(ctx context.Context, opts DaemonOptions, dbPath string) error { statePath := opts.StreamStatePath if statePath == "" { statePath = managedstream.DefaultStatePathForDB(dbPath) } state, err := managedstream.LoadState(statePath) if err != nil { return err } store, err := sqlite.OpenStore(dbPath) if err != nil { return err } defer store.Close() // Turns carry their own export flag. The usage reader stamps a session's // usage from its turns on every read, so they go only with the session's // usage and after its transcripts' revisit window. if prunedTurns, err := store.PruneExportedTurns(ctx, time.Now().UTC().Add(-sqlite.UsageRevisitWindow), ledgerPruneBatch); err != nil { opts.Diagnostic.Printf("managed observe turn prune: %v\n", err) } else if prunedTurns > 0 { opts.Diagnostic.Printf("managed observe pruned %d exported turns\n", prunedTurns) } if state.UpdatedAfter == nil { return nil } pruned, err := store.PruneExportedLedger(ctx, sqlite.LedgerPruneOptions{ ActionsExportedBefore: *state.UpdatedAfter, RiskTypeCreatedAfter: state.RiskTypeCreatedAfter, RiskTypeAnnotationID: state.RiskTypeAnnotationID, MerlinCreatedAfter: state.MerlinCreatedAfter, MerlinAnnotationID: state.MerlinAnnotationID, VerifiedAfter: state.VerifiedAfter, VerifiedAfterID: state.VerifiedAfterID, OlderThan: time.Now().UTC().Add(-ledgerRetention), MaxRowBytes: managedstream.MaxPayloadBytes / 4, Limit: ledgerPruneBatch, }) if pruned > 0 { opts.Diagnostic.Printf("managed observe pruned %d exported ledger actions\n", pruned) } return err } func cleanupInterval(idleTimeout time.Duration) time.Duration { interval := idleTimeout / 2 if interval <= 0 { return time.Nanosecond } return interval } // migrateSelfServeModeToRemote rewrites a self-serve (user-scope) managed // config still carrying the pre-remote "observe" default to "remote", so the // policy deployment's rollout mode controls the posture without a reinstall. // Only user-scope configs are touched: those were written by `kontext setup` // with the era's default, not by an admin choosing a static pin. MDM // (system-scope) and env-override configs are an explicit posture choice and // are never rewritten. Migration failure is not fatal — the daemon runs with // the config it loaded and retries on next start. func migrateSelfServeModeToRemote(loaded managedconfig.LoadedConfig, log diagnostic.Logger) managedconfig.LoadedConfig { if loaded.Scope != managedconfig.ScopeUser || loaded.Config.Mode != managedconfig.Mode { return loaded } // Observe is not always the era's default: it is also what the loader // substitutes for a mode this build cannot name. Migrating that one would // overwrite the operator's declared posture with this binary's guess, erase // the only on-disk record that the install is newer than the binary, and // silence the downgrade warning on the very next load. A downgraded endpoint // is exactly the case where nothing should be rewritten. if loaded.Config.UnsupportedMode != "" { return loaded } if err := managedconfig.RewriteMode(loaded, managedconfig.ModeRemote); err != nil { log.Printf("migrate self-serve managed mode to remote: %v\n", err) return loaded } logAlways(log, "migrated self-serve managed mode %q -> %q\n", managedconfig.Mode, managedconfig.ModeRemote) reloaded, err := managedconfig.LoadFile(loaded.Path) if err != nil { log.Printf("reload managed config after mode migration: %v\n", err) return loaded } reloaded.Scope = loaded.Scope return reloaded } // maxEnrolmentRefreshes bounds the early policy refreshes per daemon start. At // the default flush interval it covers the minute the refresh timer would have // taken, so an installation that never gets a principal goes back to the timer // and adds at most this many requests per start. const maxEnrolmentRefreshes = 6 // enrolmentRefresh refreshes the policy right after an accepted post while the // cloud can't yet name this installation's principal. The hosted API only knows // an installation once a post is accepted, and until the policy says otherwise // enforcement allows every call, so waiting for the timer leaves a fresh endpoint // ungoverned for up to a minute. Called from the flush goroutine only. type enrolmentRefresh struct { cache *cedarpolicy.Cache now chan<- struct{} left int } func (e *enrolmentRefresh) afterAcceptedPost() { if e.left == 0 { return } // An empty state means no fetch has worked yet. An accepted post shows the // cloud is reachable, so try ahead of the refresher's backoff. if !cedarpolicy.AwaitingEnrolment(e.cache.Current().State) { return } select { case e.now <- struct{}{}: e.left-- default: } }