@gajae-code/coding-agent 0.16.7 → 0.17.1
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.
- package/CHANGELOG.md +180 -1
- package/dist/types/capability/fs.d.ts +37 -4
- package/dist/types/capability/index.d.ts +19 -0
- package/dist/types/capability/types.d.ts +9 -0
- package/dist/types/cli/doctor/args.d.ts +36 -0
- package/dist/types/cli/doctor/checks.d.ts +18 -0
- package/dist/types/cli/doctor/config-repairs.d.ts +38 -0
- package/dist/types/cli/doctor/context.d.ts +89 -0
- package/dist/types/cli/doctor/files.d.ts +27 -0
- package/dist/types/cli/doctor/ids.d.ts +20 -0
- package/dist/types/cli/doctor/install-repairs.d.ts +94 -0
- package/dist/types/cli/doctor/journal.d.ts +43 -0
- package/dist/types/cli/doctor/managed-link.d.ts +39 -0
- package/dist/types/cli/doctor/native-probe.d.ts +2 -0
- package/dist/types/cli/doctor/permission-repairs.d.ts +25 -0
- package/dist/types/cli/doctor/plugin-quarantine.d.ts +77 -0
- package/dist/types/cli/doctor/plugin-restore.d.ts +40 -0
- package/dist/types/cli/doctor/plugin-targets.d.ts +11 -0
- package/dist/types/cli/doctor/probe-client.d.ts +9 -0
- package/dist/types/cli/doctor/probe-types.d.ts +18 -0
- package/dist/types/cli/doctor/probe.d.ts +3 -0
- package/dist/types/cli/doctor/projection-probe.d.ts +2 -0
- package/dist/types/cli/doctor/repairs.d.ts +28 -0
- package/dist/types/cli/doctor/report.d.ts +27 -0
- package/dist/types/cli/doctor/runner.d.ts +4 -0
- package/dist/types/cli/doctor/safety.d.ts +10 -0
- package/dist/types/cli/doctor/service-repairs.d.ts +8 -0
- package/dist/types/cli/doctor/service-targets.d.ts +41 -0
- package/dist/types/cli/doctor/types.d.ts +145 -0
- package/dist/types/cli/doctor-cli.d.ts +7 -0
- package/dist/types/cli/doctor-supervisor.d.ts +121 -0
- package/dist/types/cli/doctor-worker.d.ts +29 -0
- package/dist/types/cli/install-activation.d.ts +158 -0
- package/dist/types/cli/macos-community-app.d.ts +41 -0
- package/dist/types/cli/mcp-cli.d.ts +0 -11
- package/dist/types/cli/update-cli.d.ts +41 -4
- package/dist/types/cli-main.d.ts +174 -0
- package/dist/types/cli.d.ts +1 -171
- package/dist/types/commands/doctor.d.ts +7 -0
- package/dist/types/commands/sdk.d.ts +17 -8
- package/dist/types/config/autorouting-tier-map.d.ts +19 -0
- package/dist/types/config/file-lock.d.ts +27 -3
- package/dist/types/config/model-preset-registry.d.ts +1 -0
- package/dist/types/config/model-registry.d.ts +12 -5
- package/dist/types/config/model-resolver.d.ts +16 -3
- package/dist/types/config/models-config-schema.d.ts +2 -0
- package/dist/types/config/settings-schema.d.ts +1 -1
- package/dist/types/coordinator-mcp/question-gate-codec.d.ts +1 -1
- package/dist/types/coordinator-mcp/session-reaper.d.ts +15 -5
- package/dist/types/cursor.d.ts +9 -9
- package/dist/types/discovery/agents-md.d.ts +6 -1
- package/dist/types/discovery/helpers.d.ts +36 -7
- package/dist/types/discovery/index.d.ts +1 -1
- package/dist/types/discovery/mcp-json.d.ts +2 -0
- package/dist/types/extensibility/extensions/loader.d.ts +22 -1
- package/dist/types/extensibility/gjc-plugins/lifecycle.d.ts +14 -1
- package/dist/types/extensibility/gjc-plugins/paths.d.ts +2 -1
- package/dist/types/extensibility/gjc-plugins/types.d.ts +25 -0
- package/dist/types/extensibility/plugins/manager.d.ts +33 -4
- package/dist/types/extensibility/plugins/marketplace/cache.d.ts +6 -0
- package/dist/types/extensibility/plugins/marketplace/manager.d.ts +186 -0
- package/dist/types/extensibility/plugins/marketplace/registry.d.ts +27 -0
- package/dist/types/extensibility/plugins/marketplace/source-resolver.d.ts +16 -1
- package/dist/types/extensibility/runtime-skill-discovery.d.ts +16 -3
- package/dist/types/gjc-runtime/autoresearch-runtime.d.ts +6 -0
- package/dist/types/gjc-runtime/launch-worktree.d.ts +53 -0
- package/dist/types/gjc-runtime/session-layout.d.ts +8 -0
- package/dist/types/gjc-runtime/session-state-lock.d.ts +15 -0
- package/dist/types/gjc-runtime/session-state-sidecar.d.ts +13 -0
- package/dist/types/gjc-runtime/ultragoal-runtime.d.ts +6 -0
- package/dist/types/main.d.ts +19 -1
- package/dist/types/modes/acp/acp-agent.d.ts +40 -0
- package/dist/types/modes/components/agent-dashboard.d.ts +1 -0
- package/dist/types/modes/components/model-selector.d.ts +2 -0
- package/dist/types/modes/interactive-mode.d.ts +1 -1
- package/dist/types/runtime-mcp/config-writer.d.ts +47 -0
- package/dist/types/runtime-mcp/policy-json-edit.d.ts +60 -0
- package/dist/types/runtime-mcp/startup-policy.d.ts +4 -0
- package/dist/types/runtime-mcp/types.d.ts +2 -2
- package/dist/types/sdk/acp/adapter.d.ts +5 -1
- package/dist/types/sdk/broker/broker.d.ts +51 -0
- package/dist/types/sdk/broker/daemon-entry.d.ts +39 -0
- package/dist/types/sdk/broker/discovery.d.ts +38 -0
- package/dist/types/sdk/broker/doctor-restart.d.ts +79 -0
- package/dist/types/sdk/broker/endpoint-authority.d.ts +7 -0
- package/dist/types/sdk/broker/ensure.d.ts +14 -0
- package/dist/types/sdk/broker/lifecycle.d.ts +2 -0
- package/dist/types/sdk/broker/process-incarnation.d.ts +50 -0
- package/dist/types/sdk/broker/runtime.d.ts +26 -0
- package/dist/types/sdk/broker/session-index.d.ts +13 -4
- package/dist/types/sdk/broker/worktree-occupancy.d.ts +14 -0
- package/dist/types/sdk/bus/chat-daemon-cli.d.ts +16 -0
- package/dist/types/sdk/bus/chat-daemon-control.d.ts +101 -6
- package/dist/types/sdk/bus/chat-daemon-runtime.d.ts +16 -0
- package/dist/types/sdk/bus/daemon-startup-exclusion.d.ts +43 -0
- package/dist/types/sdk/bus/doctor-daemon-restart-client.d.ts +51 -0
- package/dist/types/sdk/bus/doctor-daemon-restart.d.ts +39 -0
- package/dist/types/sdk/bus/index.d.ts +2 -0
- package/dist/types/sdk/bus/notification-service.d.ts +3 -0
- package/dist/types/sdk/bus/telegram-daemon-cli.d.ts +11 -0
- package/dist/types/sdk/bus/telegram-daemon-contract.d.ts +1 -1
- package/dist/types/sdk/bus/telegram-daemon-control.d.ts +5 -0
- package/dist/types/sdk/bus/telegram-daemon-owner-registry.d.ts +1 -0
- package/dist/types/sdk/bus/telegram-daemon.d.ts +36 -5
- package/dist/types/sdk/cli/session-cli.d.ts +1 -0
- package/dist/types/sdk/host/session-runtime.d.ts +4 -0
- package/dist/types/sdk/host/surface-policy.d.ts +3 -1
- package/dist/types/sdk/lifecycle/service.d.ts +44 -10
- package/dist/types/sdk/prompt-deadline-lease.d.ts +6 -3
- package/dist/types/sdk/prompt-deadline-manager.d.ts +2 -2
- package/dist/types/sdk/prompt-failure.d.ts +47 -0
- package/dist/types/sdk/prompt-status.d.ts +19 -0
- package/dist/types/sdk/router/session-router.d.ts +3 -4
- package/dist/types/sdk/service-artifact-paths.d.ts +16 -0
- package/dist/types/sdk/session-authority.d.ts +9 -0
- package/dist/types/sdk/session.d.ts +3 -1
- package/dist/types/session/agent-session.d.ts +1 -1
- package/dist/types/session/blob-store.d.ts +1 -1
- package/dist/types/session/fallback-chain-controller.d.ts +3 -6
- package/dist/types/session/internal/managed-session-scope.d.ts +44 -0
- package/dist/types/session/primary-control-surface.d.ts +4 -0
- package/dist/types/session/session-manager.d.ts +6 -0
- package/dist/types/system-prompt.d.ts +6 -0
- package/dist/types/tools/ask.d.ts +25 -0
- package/dist/types/tools/image-gen.d.ts +0 -4
- package/dist/types/tools/skill-discovery.d.ts +7 -5
- package/dist/types/tools/ultragoal-ask-guard.d.ts +7 -4
- package/dist/types/utils/changelog.d.ts +0 -9
- package/dist/types/utils/mupdf-wasm.d.ts +1 -0
- package/package.json +9 -8
- package/scripts/compile-args.ts +8 -4
- package/src/capability/fs.ts +395 -17
- package/src/capability/index.ts +273 -10
- package/src/capability/types.ts +9 -0
- package/src/cli/customize-doctor.ts +18 -12
- package/src/cli/doctor/args.ts +236 -0
- package/src/cli/doctor/checks.ts +431 -0
- package/src/cli/doctor/config-repairs.ts +517 -0
- package/src/cli/doctor/context.ts +149 -0
- package/src/cli/doctor/files.ts +149 -0
- package/src/cli/doctor/ids.ts +115 -0
- package/src/cli/doctor/install-repairs.ts +335 -0
- package/src/cli/doctor/journal.ts +423 -0
- package/src/cli/doctor/managed-link.ts +514 -0
- package/src/cli/doctor/native-probe.ts +13 -0
- package/src/cli/doctor/permission-repairs.ts +242 -0
- package/src/cli/doctor/plugin-quarantine.ts +377 -0
- package/src/cli/doctor/plugin-restore.ts +59 -0
- package/src/cli/doctor/plugin-targets.ts +373 -0
- package/src/cli/doctor/probe-client.ts +195 -0
- package/src/cli/doctor/probe-types.ts +21 -0
- package/src/cli/doctor/probe.ts +42 -0
- package/src/cli/doctor/projection-probe.ts +249 -0
- package/src/cli/doctor/repairs.ts +1157 -0
- package/src/cli/doctor/report.ts +176 -0
- package/src/cli/doctor/runner.ts +342 -0
- package/src/cli/doctor/safety.ts +40 -0
- package/src/cli/doctor/service-repairs.ts +452 -0
- package/src/cli/doctor/service-targets.ts +274 -0
- package/src/cli/doctor/types.ts +164 -0
- package/src/cli/doctor-cli.ts +62 -0
- package/src/cli/doctor-supervisor.ts +571 -0
- package/src/cli/doctor-worker.ts +181 -0
- package/src/cli/install-activation.ts +666 -0
- package/src/cli/macos-community-app.ts +1449 -0
- package/src/cli/mcp-cli.ts +11 -21
- package/src/cli/nofile-limit.ts +5 -1
- package/src/cli/plugin-cli.ts +6 -1
- package/src/cli/update-cli.ts +362 -264
- package/src/cli-main.ts +517 -0
- package/src/cli.ts +64 -433
- package/src/commands/autoresearch.ts +11 -5
- package/src/commands/completion.ts +1 -1
- package/src/commands/doctor.ts +13 -0
- package/src/commands/sdk.ts +58 -10
- package/src/commands/ultragoal.ts +11 -5
- package/src/config/autorouting-tier-map.ts +7 -0
- package/src/config/file-lock.ts +736 -71
- package/src/config/mcp-schema.json +3 -3
- package/src/config/model-preset-registry.ts +1 -0
- package/src/config/model-registry.ts +80 -27
- package/src/config/model-resolver.ts +33 -4
- package/src/config/models-config-schema.ts +2 -0
- package/src/config/settings-schema.ts +6 -2
- package/src/coordinator-mcp/question-gate-codec.ts +1 -0
- package/src/coordinator-mcp/server.ts +534 -51
- package/src/coordinator-mcp/session-reaper.ts +64 -8
- package/src/cursor.ts +223 -53
- package/src/discovery/agents-md.ts +74 -8
- package/src/discovery/agents.ts +32 -7
- package/src/discovery/builtin.ts +184 -75
- package/src/discovery/claude-plugins.ts +85 -17
- package/src/discovery/cline.ts +38 -10
- package/src/discovery/cursor.ts +19 -6
- package/src/discovery/gemini.ts +62 -22
- package/src/discovery/github.ts +13 -3
- package/src/discovery/helpers.ts +483 -89
- package/src/discovery/index.ts +1 -0
- package/src/discovery/mcp-json.ts +36 -10
- package/src/discovery/opencode.ts +36 -19
- package/src/discovery/ssh.ts +11 -8
- package/src/discovery/windsurf.ts +12 -3
- package/src/exec/bash-executor.ts +10 -3
- package/src/exec/isolated-shell.ts +15 -2
- package/src/extensibility/extensions/loader.ts +88 -26
- package/src/extensibility/gjc-plugins/lifecycle.ts +189 -0
- package/src/extensibility/gjc-plugins/paths.ts +4 -1
- package/src/extensibility/gjc-plugins/types.ts +27 -0
- package/src/extensibility/plugins/manager.ts +200 -22
- package/src/extensibility/plugins/marketplace/cache.ts +55 -2
- package/src/extensibility/plugins/marketplace/manager.ts +1262 -162
- package/src/extensibility/plugins/marketplace/registry.ts +153 -0
- package/src/extensibility/plugins/marketplace/source-resolver.ts +116 -10
- package/src/extensibility/runtime-skill-discovery.ts +61 -9
- package/src/extensibility/skill-management.ts +7 -1
- package/src/gjc-runtime/autoresearch-runtime.ts +10 -1
- package/src/gjc-runtime/launch-worktree.test.ts +248 -0
- package/src/gjc-runtime/launch-worktree.ts +275 -38
- package/src/gjc-runtime/repository-binding.ts +26 -1
- package/src/gjc-runtime/session-layout.ts +10 -0
- package/src/gjc-runtime/session-state-lock.ts +330 -23
- package/src/gjc-runtime/session-state-sidecar.ts +328 -80
- package/src/gjc-runtime/ultragoal-runtime.ts +10 -1
- package/src/harness-control-plane/session-lease.ts +2 -2
- package/src/internal-urls/docs-index.generated.ts +28 -26
- package/src/main.ts +150 -29
- package/src/memories/index.ts +24 -2
- package/src/modes/acp/acp-agent.ts +522 -68
- package/src/modes/components/agent-dashboard.ts +3 -1
- package/src/modes/components/model-selector.ts +28 -19
- package/src/modes/components/plugin-settings.ts +2 -1
- package/src/modes/controllers/selector-controller.ts +15 -9
- package/src/modes/interactive-mode.ts +21 -3
- package/src/prompts/system/system-prompt.md +5 -1
- package/src/prompts/tools/browser.md +1 -1
- package/src/prompts/tools/move-session.md +4 -0
- package/src/prompts/tools/read.md +2 -1
- package/src/prompts/tools/skill-discovery.md +6 -5
- package/src/prompts/tools/task.md +1 -0
- package/src/runtime-mcp/config-writer.ts +228 -0
- package/src/runtime-mcp/oauth-discovery.ts +114 -14
- package/src/runtime-mcp/policy-json-edit.ts +129 -0
- package/src/runtime-mcp/startup-policy.ts +14 -0
- package/src/runtime-mcp/types.ts +2 -2
- package/src/sdk/acp/adapter.ts +27 -12
- package/src/sdk/broker/broker.ts +273 -44
- package/src/sdk/broker/daemon-entry.ts +119 -0
- package/src/sdk/broker/discovery.ts +173 -20
- package/src/sdk/broker/doctor-restart.ts +206 -0
- package/src/sdk/broker/endpoint-authority.ts +36 -0
- package/src/sdk/broker/ensure.ts +49 -14
- package/src/sdk/broker/lifecycle.ts +174 -42
- package/src/sdk/broker/process-incarnation.ts +86 -2
- package/src/sdk/broker/runtime.ts +145 -21
- package/src/sdk/broker/session-index.ts +136 -22
- package/src/sdk/broker/spawn-substrate.ts +4 -1
- package/src/sdk/broker/transport.ts +5 -0
- package/src/sdk/broker/worktree-occupancy.ts +21 -1
- package/src/sdk/bus/chat-daemon-cli.ts +63 -0
- package/src/sdk/bus/chat-daemon-control.ts +260 -64
- package/src/sdk/bus/chat-daemon-runtime.ts +141 -25
- package/src/sdk/bus/daemon-startup-exclusion.ts +119 -0
- package/src/sdk/bus/doctor-daemon-restart-client.ts +291 -0
- package/src/sdk/bus/doctor-daemon-restart.ts +92 -0
- package/src/sdk/bus/index.ts +252 -51
- package/src/sdk/bus/kind-aware-reconciliation.ts +76 -41
- package/src/sdk/bus/notification-service.ts +21 -3
- package/src/sdk/bus/reconciliation-store.ts +17 -11
- package/src/sdk/bus/telegram-daemon-cli.ts +91 -5
- package/src/sdk/bus/telegram-daemon-contract.ts +1 -1
- package/src/sdk/bus/telegram-daemon-control.ts +47 -0
- package/src/sdk/bus/telegram-daemon-owner-registry.ts +1 -1
- package/src/sdk/bus/telegram-daemon.ts +178 -13
- package/src/sdk/cli/session-cli.ts +100 -12
- package/src/sdk/host/host.ts +2 -7
- package/src/sdk/host/session-runtime.test.ts +329 -8
- package/src/sdk/host/session-runtime.ts +202 -50
- package/src/sdk/host/surface-policy.ts +8 -1
- package/src/sdk/lifecycle/service.ts +199 -22
- package/src/sdk/mcp/server.ts +1 -1
- package/src/sdk/prompt-deadline-lease.ts +12 -4
- package/src/sdk/prompt-deadline-manager.ts +5 -7
- package/src/sdk/prompt-failure.ts +148 -0
- package/src/sdk/prompt-status.ts +30 -1
- package/src/sdk/prompt-watchdog.ts +11 -3
- package/src/sdk/protocol/operation-inventory.generated.json +18 -0
- package/src/sdk/protocol/operation-registry.ts +20 -8
- package/src/sdk/router/session-router.ts +8 -3
- package/src/sdk/service-artifact-paths.ts +26 -0
- package/src/sdk/session-authority.ts +10 -0
- package/src/sdk/session.ts +361 -194
- package/src/session/agent-session.ts +398 -93
- package/src/session/blob-store.ts +9 -9
- package/src/session/fallback-chain-controller.ts +3 -7
- package/src/session/internal/managed-session-scope.ts +445 -29
- package/src/session/internal/managed-session-storage.ts +38 -6
- package/src/session/primary-control-surface.ts +12 -0
- package/src/session/session-manager.ts +11 -3
- package/src/session/session-storage.ts +1 -1
- package/src/session-import/codex.ts +6 -1
- package/src/session-import/redact.ts +7 -0
- package/src/skill-state/workflow-mutation-guard.ts +645 -39
- package/src/slash-commands/builtin-registry.ts +7 -3
- package/src/system-prompt.ts +8 -0
- package/src/task/executor.ts +36 -3
- package/src/task/types.ts +27 -5
- package/src/tools/ask.ts +114 -34
- package/src/tools/browser/actions.ts +2 -2
- package/src/tools/browser/tab-worker.ts +11 -8
- package/src/tools/descriptors.ts +6 -1
- package/src/tools/fetch.ts +7 -2
- package/src/tools/image-gen.ts +33 -34
- package/src/tools/read.ts +78 -32
- package/src/tools/skill-discovery.ts +17 -7
- package/src/tools/tool-catalog.generated.ts +8 -8
- package/src/tools/ultragoal-ask-guard.ts +16 -26
- package/src/utils/changelog.ts +7 -1
- package/src/utils/git.ts +3 -1
- package/src/utils/markit.ts +24 -2
- package/src/utils/mupdf-wasm.ts +26 -0
- package/src/web/search/providers/openai-compatible.ts +191 -14
|
@@ -0,0 +1,452 @@
|
|
|
1
|
+
import { constants } from "node:fs";
|
|
2
|
+
import * as fs from "node:fs/promises";
|
|
3
|
+
import * as path from "node:path";
|
|
4
|
+
import {
|
|
5
|
+
exactRestore,
|
|
6
|
+
exactUnlink,
|
|
7
|
+
type NativeExactFileIdentity,
|
|
8
|
+
verifyOwnerOnlyFdSecurity,
|
|
9
|
+
verifyOwnerOnlyPathSecurityExpected,
|
|
10
|
+
} from "@gajae-code/natives";
|
|
11
|
+
import { withBrokerStartupLock } from "../../sdk/broker/ensure";
|
|
12
|
+
import { isProcessIncarnation, observeProcessIncarnation } from "../../sdk/broker/process-incarnation";
|
|
13
|
+
import { SDK_STATE_VERSION } from "../../sdk/broker/state-version";
|
|
14
|
+
import { agentDirDigest } from "../../sdk/bus/daemon-paths";
|
|
15
|
+
import { withDaemonStartupExclusion } from "../../sdk/bus/daemon-startup-exclusion";
|
|
16
|
+
import { validMarker } from "../../sdk/bus/telegram-daemon-owner-registry";
|
|
17
|
+
import { canonicalServiceRootDigest } from "../../sdk/service-artifact-paths";
|
|
18
|
+
import type { DoctorContext, DoctorServiceTarget } from "./context";
|
|
19
|
+
import { readDoctorFile } from "./files";
|
|
20
|
+
import { DoctorJournal, DoctorJournalCreateError } from "./journal";
|
|
21
|
+
import {
|
|
22
|
+
type DoctorArtifactObservation,
|
|
23
|
+
type DoctorServiceObservation,
|
|
24
|
+
parseServiceRecord,
|
|
25
|
+
readDoctorService,
|
|
26
|
+
serviceArtifactObservation,
|
|
27
|
+
} from "./service-targets";
|
|
28
|
+
import type { DoctorCheck, DoctorRepair } from "./types";
|
|
29
|
+
|
|
30
|
+
interface OwnerProof {
|
|
31
|
+
readonly ownerId: string;
|
|
32
|
+
readonly pid: number;
|
|
33
|
+
readonly incarnation: string;
|
|
34
|
+
}
|
|
35
|
+
export interface DoctorStaleArtifactResult {
|
|
36
|
+
readonly repair: DoctorRepair;
|
|
37
|
+
readonly afterChecks?: readonly DoctorCheck[];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function ownerTuple(value: Record<string, unknown> | undefined): OwnerProof | undefined {
|
|
41
|
+
if (
|
|
42
|
+
!value ||
|
|
43
|
+
typeof value.ownerId !== "string" ||
|
|
44
|
+
value.ownerId.length === 0 ||
|
|
45
|
+
value.ownerId.length > 256 ||
|
|
46
|
+
typeof value.pid !== "number" ||
|
|
47
|
+
!Number.isSafeInteger(value.pid) ||
|
|
48
|
+
value.pid <= 0 ||
|
|
49
|
+
value.pid > 0x7fff_ffff ||
|
|
50
|
+
!isProcessIncarnation(value.incarnation)
|
|
51
|
+
)
|
|
52
|
+
return undefined;
|
|
53
|
+
return { ownerId: value.ownerId, pid: value.pid, incarnation: value.incarnation };
|
|
54
|
+
}
|
|
55
|
+
function sameOwner(left: OwnerProof, right: OwnerProof | undefined): boolean {
|
|
56
|
+
return !!right && left.ownerId === right.ownerId && left.pid === right.pid && left.incarnation === right.incarnation;
|
|
57
|
+
}
|
|
58
|
+
function sameIdentity(a: NativeExactFileIdentity, b: NativeExactFileIdentity): boolean {
|
|
59
|
+
return (
|
|
60
|
+
a.dev === b.dev &&
|
|
61
|
+
a.ino === b.ino &&
|
|
62
|
+
a.nlink === b.nlink &&
|
|
63
|
+
a.size === b.size &&
|
|
64
|
+
a.mtimeNs === b.mtimeNs &&
|
|
65
|
+
a.parentDev === b.parentDev &&
|
|
66
|
+
a.parentIno === b.parentIno &&
|
|
67
|
+
a.sha256 === b.sha256
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
function sameObservation(a: DoctorArtifactObservation | undefined, b: DoctorArtifactObservation | undefined): boolean {
|
|
71
|
+
if (!a || !b) return a === b;
|
|
72
|
+
if (a.status !== b.status) return false;
|
|
73
|
+
if ((a.status === "read" || a.status === "directory") && (b.status === "read" || b.status === "directory"))
|
|
74
|
+
return sameIdentity(a.exactIdentity, b.exactIdentity);
|
|
75
|
+
return a.status === "missing" && b.status === "missing";
|
|
76
|
+
}
|
|
77
|
+
function samePublication(
|
|
78
|
+
a: DoctorServiceObservation,
|
|
79
|
+
b: DoctorServiceObservation,
|
|
80
|
+
detachedSlot?: DoctorServiceTarget["slot"],
|
|
81
|
+
): boolean {
|
|
82
|
+
if (!sameObservation(a.restartIntent, b.restartIntent)) return false;
|
|
83
|
+
if (detachedSlot !== "discovery" && !sameObservation(a.state, b.state)) return false;
|
|
84
|
+
if (
|
|
85
|
+
detachedSlot !== "owner-lock" &&
|
|
86
|
+
(!sameObservation(a.owner, b.owner) || !sameObservation(a.directoryOwner, b.directoryOwner))
|
|
87
|
+
)
|
|
88
|
+
return false;
|
|
89
|
+
if (detachedSlot !== "startup-marker" && !sameObservation(a.startupMarker, b.startupMarker)) return false;
|
|
90
|
+
return (
|
|
91
|
+
a.ownerMarker?.path === b.ownerMarker?.path &&
|
|
92
|
+
sameObservation(a.ownerMarker?.observation, b.ownerMarker?.observation)
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
async function proveOwner(context: DoctorContext, target: DoctorServiceTarget): Promise<OwnerProof | undefined> {
|
|
97
|
+
const observation = target.observation;
|
|
98
|
+
const rootDigest = await canonicalServiceRootDigest(context.agentRoot.locator);
|
|
99
|
+
const state = parseServiceRecord(observation.state);
|
|
100
|
+
const owner = parseServiceRecord(observation.owner);
|
|
101
|
+
if (target.slot === "startup-marker") {
|
|
102
|
+
const marker = observation.startupMarker && parseServiceRecord(observation.startupMarker);
|
|
103
|
+
if (
|
|
104
|
+
target.service !== "telegram" ||
|
|
105
|
+
!marker ||
|
|
106
|
+
marker.version !== 1 ||
|
|
107
|
+
marker.rootDigest !== rootDigest ||
|
|
108
|
+
typeof marker.token !== "string" ||
|
|
109
|
+
!/^[A-Za-z0-9_-]{1,128}$/.test(marker.token)
|
|
110
|
+
)
|
|
111
|
+
return undefined;
|
|
112
|
+
return ownerTuple({ ...marker, ownerId: marker.token });
|
|
113
|
+
}
|
|
114
|
+
if (target.service === "broker") {
|
|
115
|
+
if (observation.owner.status === "missing" && target.slot === "discovery") {
|
|
116
|
+
return state?.version === SDK_STATE_VERSION && state.protocolVersion === 3 && state.rootDigest === rootDigest
|
|
117
|
+
? ownerTuple(state)
|
|
118
|
+
: undefined;
|
|
119
|
+
}
|
|
120
|
+
const proof = ownerTuple(owner);
|
|
121
|
+
if (!proof || owner?.version !== 1 || owner.rootDigest !== rootDigest) return undefined;
|
|
122
|
+
if (
|
|
123
|
+
observation.state.status !== "missing" &&
|
|
124
|
+
(!state ||
|
|
125
|
+
state.version !== SDK_STATE_VERSION ||
|
|
126
|
+
state.protocolVersion !== 3 ||
|
|
127
|
+
!sameOwner(proof, ownerTuple(state)))
|
|
128
|
+
)
|
|
129
|
+
return undefined;
|
|
130
|
+
return proof;
|
|
131
|
+
}
|
|
132
|
+
if (target.service === "telegram") {
|
|
133
|
+
const marker = observation.ownerMarker && parseServiceRecord(observation.ownerMarker.observation);
|
|
134
|
+
if (!validMarker(marker) || marker.agentDirDigest !== agentDirDigest(marker.agentDir)) return undefined;
|
|
135
|
+
const normalized = (value: string): string =>
|
|
136
|
+
process.platform === "win32" ? path.resolve(value).replaceAll("\\", "/").toLowerCase() : path.resolve(value);
|
|
137
|
+
if (normalized(marker.agentDir) !== normalized(context.agentRoot.locator)) return undefined;
|
|
138
|
+
const proof = ownerTuple({ ...marker });
|
|
139
|
+
if (!proof) return undefined;
|
|
140
|
+
for (const [raw, record] of [
|
|
141
|
+
[observation.state, state],
|
|
142
|
+
[observation.owner, owner],
|
|
143
|
+
] as const) {
|
|
144
|
+
if (raw.status === "missing") continue;
|
|
145
|
+
if (!record || !sameOwner(proof, ownerTuple(record)) || record.acquisitionId !== marker.acquisitionId)
|
|
146
|
+
return undefined;
|
|
147
|
+
}
|
|
148
|
+
return proof;
|
|
149
|
+
}
|
|
150
|
+
if (observation.owner.status === "missing" && target.slot === "discovery") {
|
|
151
|
+
return state?.version === 1 && state.kind === target.service && state.rootDigest === rootDigest
|
|
152
|
+
? ownerTuple(state)
|
|
153
|
+
: undefined;
|
|
154
|
+
}
|
|
155
|
+
const proof = ownerTuple(owner);
|
|
156
|
+
if (!proof || owner?.version !== 1 || owner.rootDigest !== rootDigest) return undefined;
|
|
157
|
+
if (
|
|
158
|
+
observation.state.status !== "missing" &&
|
|
159
|
+
(state?.version !== 1 || state.kind !== target.service || !sameOwner(proof, ownerTuple(state)))
|
|
160
|
+
)
|
|
161
|
+
return undefined;
|
|
162
|
+
return proof;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
async function secureObserved(file: string, observation: DoctorArtifactObservation): Promise<boolean> {
|
|
166
|
+
if (observation.status !== "read" && observation.status !== "directory") return false;
|
|
167
|
+
const kind = observation.status === "directory" ? "directory" : "file";
|
|
168
|
+
const expected = observation.exactIdentity;
|
|
169
|
+
if (kind === "file" && expected.nlink !== 1n) return false;
|
|
170
|
+
if (process.platform === "win32")
|
|
171
|
+
return verifyOwnerOnlyPathSecurityExpected(file, kind, expected.dev, expected.ino).ok;
|
|
172
|
+
const handle = await fs.open(
|
|
173
|
+
file,
|
|
174
|
+
constants.O_RDONLY |
|
|
175
|
+
constants.O_NOFOLLOW |
|
|
176
|
+
constants.O_NONBLOCK |
|
|
177
|
+
(kind === "directory" ? constants.O_DIRECTORY : 0),
|
|
178
|
+
);
|
|
179
|
+
try {
|
|
180
|
+
const stat = await handle.stat({ bigint: true });
|
|
181
|
+
return (
|
|
182
|
+
stat.dev === expected.dev && stat.ino === expected.ino && verifyOwnerOnlyFdSecurity(file, kind, handle.fd).ok
|
|
183
|
+
);
|
|
184
|
+
} finally {
|
|
185
|
+
await handle.close();
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function evidence(identity: NativeExactFileIdentity, quarantineName: string): Record<string, unknown> {
|
|
190
|
+
return {
|
|
191
|
+
exists: true,
|
|
192
|
+
dev: identity.dev.toString(),
|
|
193
|
+
ino: identity.ino.toString(),
|
|
194
|
+
nlink: identity.nlink?.toString(),
|
|
195
|
+
size: identity.size.toString(),
|
|
196
|
+
mtimeNs: identity.mtimeNs.toString(),
|
|
197
|
+
parentDev: identity.parentDev?.toString(),
|
|
198
|
+
parentIno: identity.parentIno?.toString(),
|
|
199
|
+
quarantineName,
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** One canonical stale slot, retained in quarantine; never starts or signals a service. */
|
|
204
|
+
export async function applyDoctorStaleArtifact(
|
|
205
|
+
context: DoctorContext,
|
|
206
|
+
runId: string,
|
|
207
|
+
target: DoctorServiceTarget,
|
|
208
|
+
collectAfterChecks: () => Promise<readonly DoctorCheck[]>,
|
|
209
|
+
): Promise<DoctorStaleArtifactResult> {
|
|
210
|
+
const base: DoctorRepair = {
|
|
211
|
+
id: "service.detach-owned-stale-artifact",
|
|
212
|
+
targetId: target.targetId,
|
|
213
|
+
riskClasses: ["artifact-detach"],
|
|
214
|
+
authorization: context.options.allowRisks,
|
|
215
|
+
readiness: [],
|
|
216
|
+
candidates: [],
|
|
217
|
+
preconditions: [
|
|
218
|
+
"canonical slot",
|
|
219
|
+
"original owner and file identity",
|
|
220
|
+
"positive owner death",
|
|
221
|
+
"shared startup exclusion",
|
|
222
|
+
],
|
|
223
|
+
state: "preparing",
|
|
224
|
+
sideEffectStarted: false,
|
|
225
|
+
beforeCheckIds: [],
|
|
226
|
+
afterCheckIds: [],
|
|
227
|
+
restartRequired: false,
|
|
228
|
+
nonrollbackableEffects: [],
|
|
229
|
+
};
|
|
230
|
+
const blocked = (reasonCode: string): DoctorStaleArtifactResult => ({
|
|
231
|
+
repair: { ...base, state: "blocked", reasonCode },
|
|
232
|
+
});
|
|
233
|
+
if (!context.options.allowRisks.includes("artifact-detach")) return blocked("authorization_missing");
|
|
234
|
+
if (
|
|
235
|
+
context.options.mode !== "fix" ||
|
|
236
|
+
target.kind !== "artifact" ||
|
|
237
|
+
!target.slot ||
|
|
238
|
+
target.targetId !== context.options.targetId
|
|
239
|
+
)
|
|
240
|
+
return blocked("invalid_artifact_request");
|
|
241
|
+
const original = serviceArtifactObservation(target.observation, target.slot);
|
|
242
|
+
if (!original) return blocked("unsupported_slot");
|
|
243
|
+
if (target.observation.restartIntent.status !== "missing") return blocked("restart_reservation_present_or_unknown");
|
|
244
|
+
if (original.status !== "read" && original.status !== "directory") return blocked("artifact_authority_unavailable");
|
|
245
|
+
let proof: OwnerProof | undefined;
|
|
246
|
+
try {
|
|
247
|
+
proof = await proveOwner(context, target);
|
|
248
|
+
} catch {
|
|
249
|
+
return blocked("owner_observation_unavailable");
|
|
250
|
+
}
|
|
251
|
+
if (!proof) return blocked("owner_receipt_untrusted");
|
|
252
|
+
if (observeProcessIncarnation(proof.pid).status !== "absent") return blocked("owner_death_unproven");
|
|
253
|
+
const slot = target.slot;
|
|
254
|
+
const file =
|
|
255
|
+
slot === "discovery"
|
|
256
|
+
? target.observation.paths.state
|
|
257
|
+
: slot === "owner-lock"
|
|
258
|
+
? (target.observation.paths.directoryOwner ?? target.observation.paths.owner)
|
|
259
|
+
: target.observation.paths.startupMarker!;
|
|
260
|
+
const quarantineName = `.${path.basename(file)}.doctor-${runId}`;
|
|
261
|
+
const quarantine = path.join(path.dirname(file), quarantineName);
|
|
262
|
+
const expected: NativeExactFileIdentity = { ...original.exactIdentity, detachOnly: true, quarantineName };
|
|
263
|
+
let started = false;
|
|
264
|
+
const operation = async (): Promise<DoctorStaleArtifactResult> => {
|
|
265
|
+
if (context.options.signal?.aborted || performance.now() >= context.deadline)
|
|
266
|
+
return blocked("cancelled_or_expired");
|
|
267
|
+
const current = await readDoctorService(context.agentRoot.locator, target.service);
|
|
268
|
+
if (!samePublication(target.observation, current)) return blocked("publication_changed");
|
|
269
|
+
if (observeProcessIncarnation(proof!.pid).status !== "absent") return blocked("owner_death_unproven");
|
|
270
|
+
if (!(await secureObserved(file, original))) return blocked("artifact_security_unproven");
|
|
271
|
+
for (const [recordPath, record] of [
|
|
272
|
+
[current.paths.owner, current.owner],
|
|
273
|
+
[current.paths.state, current.state],
|
|
274
|
+
[current.ownerMarker?.path, current.ownerMarker?.observation],
|
|
275
|
+
] as const) {
|
|
276
|
+
if (recordPath && record?.status === "read" && !(await secureObserved(recordPath, record)))
|
|
277
|
+
return blocked("owner_receipt_security_unproven");
|
|
278
|
+
}
|
|
279
|
+
if (original.status === "directory") {
|
|
280
|
+
const directory = await fs.opendir(file, { bufferSize: 4 });
|
|
281
|
+
for await (const entry of directory)
|
|
282
|
+
if (entry.name !== "owner.json" || !entry.isFile()) return blocked("unknown_linked_artifact");
|
|
283
|
+
}
|
|
284
|
+
let journal: DoctorJournal;
|
|
285
|
+
try {
|
|
286
|
+
journal = await DoctorJournal.create(context.agentRoot.locator, runId);
|
|
287
|
+
started = true;
|
|
288
|
+
} catch (error) {
|
|
289
|
+
started = error instanceof DoctorJournalCreateError ? error.sideEffectStarted : true;
|
|
290
|
+
return {
|
|
291
|
+
repair: {
|
|
292
|
+
...base,
|
|
293
|
+
state: started ? "uncertain" : "blocked",
|
|
294
|
+
sideEffectStarted: started,
|
|
295
|
+
reasonCode: "journal_unavailable",
|
|
296
|
+
},
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
try {
|
|
300
|
+
await journal.append({
|
|
301
|
+
repairId: base.id,
|
|
302
|
+
targetId: target.targetId,
|
|
303
|
+
phase: "before",
|
|
304
|
+
before: evidence(expected, quarantineName),
|
|
305
|
+
});
|
|
306
|
+
await journal.append({ repairId: base.id, targetId: target.targetId, phase: "applying" });
|
|
307
|
+
if (context.options.signal?.aborted || performance.now() >= context.deadline)
|
|
308
|
+
throw new Error("cancelled_or_expired");
|
|
309
|
+
const detached = exactUnlink(file, expected);
|
|
310
|
+
const retainedCleanup =
|
|
311
|
+
detached.code === "cleanup_pending" &&
|
|
312
|
+
detached.detachedPath === quarantine &&
|
|
313
|
+
!detached.retainedUnknownPath &&
|
|
314
|
+
!detached.retainedSuccessorPath;
|
|
315
|
+
if (!detached.ok && !retainedCleanup)
|
|
316
|
+
throw new Error(
|
|
317
|
+
`detach_${detached.code && /^[a-z_]{1,48}$/.test(detached.code) ? detached.code : "unverified"}`,
|
|
318
|
+
);
|
|
319
|
+
if (detached.detachedPath !== quarantine) throw new Error("quarantine_path_mismatch");
|
|
320
|
+
const after = await readDoctorService(context.agentRoot.locator, target.service);
|
|
321
|
+
if (
|
|
322
|
+
serviceArtifactObservation(after, slot)?.status !== "missing" ||
|
|
323
|
+
!samePublication(target.observation, after, slot)
|
|
324
|
+
)
|
|
325
|
+
throw new Error("publication_changed");
|
|
326
|
+
if (original.status === "read") {
|
|
327
|
+
const retained = await readDoctorFile(quarantine);
|
|
328
|
+
if (retained.status !== "read" || !sameIdentity(original.exactIdentity, retained.exactIdentity))
|
|
329
|
+
throw new Error("quarantine_identity_changed");
|
|
330
|
+
} else {
|
|
331
|
+
const retained = await fs.lstat(quarantine, { bigint: true });
|
|
332
|
+
const parent = await fs.lstat(path.dirname(quarantine), { bigint: true });
|
|
333
|
+
if (
|
|
334
|
+
!retained.isDirectory() ||
|
|
335
|
+
retained.isSymbolicLink() ||
|
|
336
|
+
retained.dev !== expected.dev ||
|
|
337
|
+
retained.ino !== expected.ino ||
|
|
338
|
+
retained.nlink !== expected.nlink ||
|
|
339
|
+
retained.size !== expected.size ||
|
|
340
|
+
retained.mtimeNs !== expected.mtimeNs ||
|
|
341
|
+
parent.isSymbolicLink() ||
|
|
342
|
+
parent.dev !== expected.parentDev ||
|
|
343
|
+
parent.ino !== expected.parentIno
|
|
344
|
+
)
|
|
345
|
+
throw new Error("quarantine_identity_changed");
|
|
346
|
+
}
|
|
347
|
+
if (retainedCleanup) {
|
|
348
|
+
const parent = await fs.open(
|
|
349
|
+
path.dirname(file),
|
|
350
|
+
constants.O_RDONLY | constants.O_DIRECTORY | constants.O_NOFOLLOW | constants.O_NONBLOCK,
|
|
351
|
+
);
|
|
352
|
+
try {
|
|
353
|
+
const identity = await parent.stat({ bigint: true });
|
|
354
|
+
if (identity.dev !== expected.parentDev || identity.ino !== expected.parentIno)
|
|
355
|
+
throw new Error("quarantine_identity_changed");
|
|
356
|
+
await parent.sync();
|
|
357
|
+
} finally {
|
|
358
|
+
await parent.close();
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
const afterChecks = await collectAfterChecks();
|
|
362
|
+
if (
|
|
363
|
+
!afterChecks.some(
|
|
364
|
+
check =>
|
|
365
|
+
check.targetId === target.targetId &&
|
|
366
|
+
check.execution === "completed" &&
|
|
367
|
+
check.evidence.present === false,
|
|
368
|
+
)
|
|
369
|
+
)
|
|
370
|
+
throw new Error("artifact_postcheck_missing");
|
|
371
|
+
await journal.append({
|
|
372
|
+
repairId: base.id,
|
|
373
|
+
targetId: target.targetId,
|
|
374
|
+
phase: "verified",
|
|
375
|
+
after: { exists: false, quarantineName },
|
|
376
|
+
outcome: "verified",
|
|
377
|
+
});
|
|
378
|
+
return {
|
|
379
|
+
repair: {
|
|
380
|
+
...base,
|
|
381
|
+
state: "verified",
|
|
382
|
+
sideEffectStarted: true,
|
|
383
|
+
outcome: { mutationVerified: true },
|
|
384
|
+
nonrollbackableEffects: ["artifact_retained_in_quarantine"],
|
|
385
|
+
},
|
|
386
|
+
afterChecks,
|
|
387
|
+
};
|
|
388
|
+
} catch (error) {
|
|
389
|
+
const known = new Set([
|
|
390
|
+
"cancelled_or_expired",
|
|
391
|
+
"quarantine_path_mismatch",
|
|
392
|
+
"publication_changed",
|
|
393
|
+
"quarantine_identity_changed",
|
|
394
|
+
"artifact_postcheck_missing",
|
|
395
|
+
]);
|
|
396
|
+
const reasonCode =
|
|
397
|
+
error instanceof Error && (known.has(error.message) || /^detach_[a-z_]{1,48}$/.test(error.message))
|
|
398
|
+
? error.message
|
|
399
|
+
: "artifact_detach_unverified";
|
|
400
|
+
let state: DoctorRepair["state"] = "uncertain";
|
|
401
|
+
try {
|
|
402
|
+
const current = await readDoctorService(context.agentRoot.locator, target.service);
|
|
403
|
+
if (
|
|
404
|
+
serviceArtifactObservation(current, slot)?.status === "missing" &&
|
|
405
|
+
samePublication(target.observation, current, slot) &&
|
|
406
|
+
observeProcessIncarnation(proof!.pid).status === "absent"
|
|
407
|
+
) {
|
|
408
|
+
const restored = exactRestore(quarantine, file, {
|
|
409
|
+
...original.exactIdentity,
|
|
410
|
+
detachOnly: false,
|
|
411
|
+
quarantineName: undefined,
|
|
412
|
+
});
|
|
413
|
+
const restoredState = await readDoctorService(context.agentRoot.locator, target.service);
|
|
414
|
+
if (restored.ok && sameObservation(original, serviceArtifactObservation(restoredState, slot)))
|
|
415
|
+
state = "rolled_back";
|
|
416
|
+
} else if (!samePublication(target.observation, current, slot)) state = "rollback_conflict";
|
|
417
|
+
} catch {
|
|
418
|
+
state = "uncertain";
|
|
419
|
+
}
|
|
420
|
+
try {
|
|
421
|
+
await journal.append({
|
|
422
|
+
repairId: base.id,
|
|
423
|
+
targetId: target.targetId,
|
|
424
|
+
phase: state === "rolled_back" ? "failed" : "uncertain",
|
|
425
|
+
outcome: state,
|
|
426
|
+
});
|
|
427
|
+
} catch {
|
|
428
|
+
state = "uncertain";
|
|
429
|
+
}
|
|
430
|
+
return { repair: { ...base, state, sideEffectStarted: true, reasonCode } };
|
|
431
|
+
} finally {
|
|
432
|
+
journal.close();
|
|
433
|
+
}
|
|
434
|
+
};
|
|
435
|
+
try {
|
|
436
|
+
return target.service === "broker"
|
|
437
|
+
? await withBrokerStartupLock(context.agentRoot.locator, operation)
|
|
438
|
+
: await withDaemonStartupExclusion(context.agentRoot.locator, target.service, operation, {
|
|
439
|
+
signal: context.options.signal,
|
|
440
|
+
timeoutMs: Math.max(1, context.deadline - performance.now()),
|
|
441
|
+
});
|
|
442
|
+
} catch {
|
|
443
|
+
return {
|
|
444
|
+
repair: {
|
|
445
|
+
...base,
|
|
446
|
+
state: started ? "uncertain" : "blocked",
|
|
447
|
+
sideEffectStarted: started,
|
|
448
|
+
reasonCode: "startup_exclusion_unavailable",
|
|
449
|
+
},
|
|
450
|
+
};
|
|
451
|
+
}
|
|
452
|
+
}
|