gentle-pi 4.7.0 → 4.8.0
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/README.md +55 -44
- package/assets/agents/gentle-ai-explore.md +3 -3
- package/assets/agents/gentle-ai-verify.md +20 -6
- package/assets/agents/gentle-ai-worker.md +6 -6
- package/assets/agents/jd-judge-a.md +1 -3
- package/assets/agents/jd-judge-b.md +1 -3
- package/assets/agents/review-readability.md +1 -5
- package/assets/agents/review-reliability.md +1 -5
- package/assets/agents/review-resilience.md +1 -5
- package/assets/agents/review-risk.md +1 -5
- package/assets/design-canvas/canvas-api.js +45 -0
- package/assets/design-canvas/canvas-boards.js +456 -0
- package/assets/design-canvas/canvas-boot.js +217 -0
- package/assets/design-canvas/canvas-checks.js +491 -0
- package/assets/design-canvas/canvas-comments.js +315 -0
- package/assets/design-canvas/canvas-compare.js +159 -0
- package/assets/design-canvas/canvas-events.js +75 -0
- package/assets/design-canvas/canvas-export.js +393 -0
- package/assets/design-canvas/canvas-focus.js +203 -0
- package/assets/design-canvas/canvas-handoffs.js +150 -0
- package/assets/design-canvas/canvas-i18n-es.js +392 -0
- package/assets/design-canvas/canvas-i18n.js +108 -0
- package/assets/design-canvas/canvas-layout.js +526 -0
- package/assets/design-canvas/canvas-menu.js +213 -0
- package/assets/design-canvas/canvas-messages.js +86 -0
- package/assets/design-canvas/canvas-minimap.js +163 -0
- package/assets/design-canvas/canvas-parts.js +401 -0
- package/assets/design-canvas/canvas-popover.js +234 -0
- package/assets/design-canvas/canvas-rail.js +348 -0
- package/assets/design-canvas/canvas-sizes.js +524 -0
- package/assets/design-canvas/canvas-store.js +164 -0
- package/assets/design-canvas/canvas-tokens.js +674 -0
- package/assets/design-canvas/canvas-ui.js +24 -0
- package/assets/design-canvas/canvas-view.js +184 -0
- package/assets/design-canvas/canvas.css +746 -15
- package/assets/design-canvas/canvas.js +2 -1506
- package/assets/design-canvas/index.html +117 -36
- package/assets/design-canvas/inspector.js +546 -16
- package/assets/install-wizard/wizard.js +5 -1
- package/assets/migrations/agent-body-history.json +24 -0
- package/assets/orchestrator-delegation-reference.md +8 -14
- package/assets/orchestrator-delegation.md +4 -4
- package/assets/orchestrator-full-delegation-card.md +4 -5
- package/assets/orchestrator-full-delegation.md +8 -8
- package/assets/orchestrator-memory.md +2 -2
- package/assets/orchestrator-prompts.md +1 -1
- package/assets/orchestrator-quota-recovery.md +5 -0
- package/assets/orchestrator-skills.md +13 -12
- package/assets/orchestrator-tracking.md +10 -14
- package/assets/orchestrator-verification.md +10 -8
- package/assets/orchestrator-writer.md +4 -4
- package/assets/orchestrator.md +18 -22
- package/bin/gentle-shell.mjs +128 -9
- package/contracts/review-provider-contract-mirror/provider-contract.lock.json +8 -9
- package/contracts/review-provider-contract-mirror/{v1.4.0 → v1.5.0}/bundle/manifest.json +3 -3
- package/contracts/review-provider-contract-mirror/{v1.4.0 → v1.5.0}/bundle/orchestration/pi.md +1 -0
- package/contracts/review-provider-contract-mirror/{v1.4.0 → v1.5.0}/bundle/routing/pi.md +6 -6
- package/contracts/review-provider-contract-mirror/{v1.4.0 → v1.5.0}/generated/provider-capabilities.baseline.json +3 -3
- package/contracts/review-provider-contract-mirror/{v1.4.0 → v1.5.0}/generated/provider-roles.baseline.json +1 -1
- package/docs/assets/brand/sponsor-banner.svg +60 -0
- package/docs/bundled-install.md +14 -6
- package/docs/delegated-verification.md +18 -2
- package/docs/desktop-rpc.md +1253 -0
- package/docs/gentle-agents-activity.md +26 -0
- package/docs/gentle-shell.md +39 -16
- package/docs/install-wizard.md +30 -21
- package/docs/odd-preparation-continuity.md +24 -0
- package/docs/permission-modes.md +9 -1
- package/docs/prompt-budget.md +12 -0
- package/docs/rdd-odd-friction-triage-2026-11-07.md +57 -0
- package/docs/readme-reference.md +76 -24
- package/docs/review-coordinator.md +29 -9
- package/docs/review-delivery-preflight.md +22 -0
- package/docs/work-continuity.md +11 -1
- package/extensions/ask-user-question.ts +2 -2
- package/extensions/child-context.ts +21 -7
- package/extensions/gentle-agents.ts +234 -33
- package/extensions/gentle-ai.ts +576 -135
- package/extensions/gentle-design.ts +69 -3
- package/extensions/gentle-desktop-rpc.ts +143 -0
- package/extensions/gentle-guide.ts +3 -0
- package/extensions/gentle-shell.ts +105 -52
- package/extensions/gentle-todo.ts +48 -25
- package/extensions/skill-registry.ts +248 -223
- package/lib/agent-assets.ts +30 -6
- package/lib/agent-tool-overlay.ts +117 -0
- package/lib/agents-config.ts +47 -6
- package/lib/agents-message-delivery.ts +17 -0
- package/lib/agents-messaging.ts +8 -2
- package/lib/agents-protocol.ts +15 -0
- package/lib/agents-report-cap.ts +115 -0
- package/lib/agents-runner.ts +91 -6
- package/lib/agents-session-transport.ts +17 -1
- package/lib/background-jobs-tools.ts +9 -4
- package/lib/bounded-writer-admission.ts +184 -31
- package/lib/child-context-files.ts +21 -6
- package/lib/codemode-renderer.ts +1 -0
- package/lib/delivery-conflict-markers.ts +349 -0
- package/lib/design-canvas-assets.ts +36 -0
- package/lib/design-canvas-server.ts +241 -23
- package/lib/design-comments.ts +94 -4
- package/lib/design-handoffs.ts +2 -1
- package/lib/design-layout.ts +125 -0
- package/lib/design-parts.ts +281 -0
- package/lib/design-system-tokens.ts +111 -0
- package/lib/desktop-agents-service.ts +294 -0
- package/lib/desktop-design-service.ts +233 -0
- package/lib/desktop-jobs-service.ts +263 -0
- package/lib/desktop-rpc-agents.ts +294 -0
- package/lib/desktop-rpc-config.ts +731 -0
- package/lib/desktop-rpc-design.ts +138 -0
- package/lib/desktop-rpc-jobs.ts +181 -0
- package/lib/desktop-rpc-permissions.ts +177 -0
- package/lib/desktop-rpc-profiles.ts +196 -0
- package/lib/desktop-rpc-usage.ts +229 -0
- package/lib/desktop-rpc.ts +225 -0
- package/lib/desktop-usage-service.ts +310 -0
- package/lib/destructive-command-guard.ts +188 -13
- package/lib/gentle-ai-renderer.ts +13 -1
- package/lib/gentle-design.ts +201 -14
- package/lib/gentle-shell-bundled.ts +100 -9
- package/lib/hidden-tools.ts +35 -0
- package/lib/inprocess-reviewer.ts +96 -12
- package/lib/model-fallback-policy.ts +9 -0
- package/lib/model-routing-authority.ts +9 -0
- package/lib/nan-provider.ts +42 -28
- package/lib/native-review-cli.ts +79 -4
- package/lib/orchestrator-discovery.ts +39 -2
- package/lib/reply-language-anchor.ts +27 -2
- package/lib/request-context-placement.ts +53 -0
- package/lib/review-candidate-view.ts +11 -1
- package/lib/review-continuation-registry.ts +17 -0
- package/lib/review-coordinator.ts +252 -8
- package/lib/review-delivery-preflight.ts +85 -0
- package/lib/review-host-relay.ts +5 -1
- package/lib/review-integration-v2.ts +56 -3
- package/lib/reviewer-stream.ts +87 -0
- package/lib/shell-prompt.ts +16 -3
- package/lib/shell-usage.ts +10 -0
- package/lib/vim-editor-adapter.ts +9 -2
- package/lib/writer-batch-admission.ts +55 -0
- package/lib/yolo-session-policy.ts +6 -1
- package/package.json +2 -2
- package/prompts/skill-creation.md +1 -10
- package/runtime/design-canvas-assets.mjs +37 -0
- package/runtime/gentle-shell-bundled.mjs +100 -9
- package/runtime/native-review-cli.mjs +79 -4
- package/runtime/review-integration-v2.mjs +56 -3
- package/scripts/bootstrap.cmd +2 -2
- package/scripts/bootstrap.sh +41 -4
- package/scripts/build-full-delegation-card.mjs +4 -0
- package/scripts/build-runtime-modules.mjs +1 -0
- package/scripts/gentle-ai-installer.mjs +10 -10
- package/scripts/gentle-ai-proxy.mjs +1 -1
- package/scripts/installer-downloads.mjs +34 -8
- package/scripts/installer-preflight.mjs +28 -17
- package/scripts/installer-probes.mjs +25 -5
- package/scripts/installer-server.mjs +38 -9
- package/scripts/installer-windows.mjs +2 -2
- package/scripts/pi-update.mjs +166 -27
- package/scripts/prepare-verifier.mjs +107 -0
- package/scripts/update-agent-body-history.mjs +55 -8
- package/scripts/verify-package-files.mjs +40 -19
- package/skills/branch-pr/SKILL.md +16 -16
- package/skills/comment-writer/SKILL.md +1 -1
- package/skills/design/SKILL.md +6 -6
- package/skills/gentle-ai/SKILL.md +5 -5
- package/skills/judgment-day/SKILL.md +2 -2
- package/skills/judgment-day/references/prompts-and-formats.md +1 -1
- package/skills/rdd-defect-workflow/SKILL.md +6 -6
- package/skills/skill-improver/SKILL.md +2 -2
- package/skills/skill-registry/SKILL.md +11 -13
- package/skills/tui-design/SKILL.md +1 -1
- package/skills/work-unit-commits/SKILL.md +5 -5
- package/tests/agent-prompt-diet-contract.test.ts +101 -0
- package/tests/agent-tool-overlay-acceptance.test.ts +158 -0
- package/tests/agent-tool-overlay.test.ts +89 -0
- package/tests/agents-config.test.ts +10 -1
- package/tests/agents-message-delivery.test.ts +36 -0
- package/tests/agents-queries.test.ts +144 -4
- package/tests/agents-report-cap.test.ts +126 -0
- package/tests/agents-runner.test.ts +37 -1
- package/tests/agents-session-transport.test.ts +34 -0
- package/tests/always-on-prompt-verdicts-contract.test.ts +188 -0
- package/tests/append-system-prompt-route.test.ts +10 -2
- package/tests/approved-text-leftovers-contract.test.ts +136 -0
- package/tests/artifact-language.test.ts +1 -1
- package/tests/ask-user-question.test.ts +2 -1
- package/tests/audit-leftovers-contract.test.ts +51 -0
- package/tests/bounded-stop-contract.test.ts +9 -1
- package/tests/branch-pr-skill.test.ts +10 -1
- package/tests/bundled-install.test.ts +1 -1
- package/tests/child-context-files.test.ts +64 -8
- package/tests/child-skills-catalog.test.ts +56 -0
- package/tests/codemode-rendering.test.ts +4 -1
- package/tests/delegate-for-reason-contract.test.ts +30 -8
- package/tests/delivery-conflict-markers.test.ts +348 -0
- package/tests/design-canvas-all-sizes.test.ts +631 -0
- package/tests/design-canvas-allowlist.test.ts +32 -0
- package/tests/design-canvas-assets.test.ts +11 -878
- package/tests/design-canvas-boards.test.ts +77 -0
- package/tests/design-canvas-checks.test.ts +655 -0
- package/tests/design-canvas-compare.test.ts +79 -0
- package/tests/design-canvas-events.test.ts +59 -0
- package/tests/design-canvas-export.test.ts +1168 -0
- package/tests/design-canvas-focus.test.ts +113 -0
- package/tests/design-canvas-handoffs.test.ts +59 -0
- package/tests/design-canvas-i18n-review.test.ts +513 -0
- package/tests/design-canvas-i18n-views.test.ts +259 -0
- package/tests/design-canvas-i18n.test.ts +526 -0
- package/tests/design-canvas-inspector.test.ts +937 -0
- package/tests/design-canvas-layout.test.ts +1222 -0
- package/tests/design-canvas-minimap.test.ts +413 -0
- package/tests/design-canvas-modules.test.ts +90 -0
- package/tests/design-canvas-parts.test.ts +721 -0
- package/tests/design-canvas-rail.test.ts +799 -0
- package/tests/design-canvas-server.test.ts +855 -17
- package/tests/design-canvas-sizes.test.ts +548 -0
- package/tests/design-canvas-store.test.ts +180 -0
- package/tests/design-canvas-tokens.test.ts +965 -0
- package/tests/design-canvas-view.test.ts +299 -0
- package/tests/design-comments.test.ts +201 -1
- package/tests/design-handoffs.test.ts +20 -0
- package/tests/design-layout.test.ts +100 -0
- package/tests/design-parts.test.ts +260 -0
- package/tests/design-system-tokens.test.ts +215 -0
- package/tests/desktop-rpc-agents.test.ts +893 -0
- package/tests/desktop-rpc-config.test.ts +1452 -0
- package/tests/desktop-rpc-design.test.ts +662 -0
- package/tests/desktop-rpc-jobs.test.ts +796 -0
- package/tests/desktop-rpc-permissions.test.ts +311 -0
- package/tests/desktop-rpc-profiles.test.ts +140 -0
- package/tests/desktop-rpc-safety.test.ts +119 -0
- package/tests/desktop-rpc-usage.test.ts +697 -0
- package/tests/desktop-rpc.test.ts +252 -0
- package/tests/devbinary/pi-host-relay.devtest.ts +149 -80
- package/tests/devbinary/review-session-consent.devtest.ts +84 -0
- package/tests/engram-safety.test.ts +111 -0
- package/tests/fixtures/devbinary/status-v9-escalated-non-blocking.captured.json +92 -0
- package/tests/fixtures/devbinary/status-v9-escalated-non-blocking.provenance.md +3 -0
- package/tests/fixtures/primary-session-prompt/gentleman.golden.md +20 -29
- package/tests/fixtures/refusal-identity.probe.mjs +139 -0
- package/tests/full-delegation-closure.test.ts +29 -0
- package/tests/gentle-agents.test.ts +329 -4
- package/tests/gentle-ai-binary.test.ts +1 -1
- package/tests/gentle-ai-child-guards.test.ts +1 -0
- package/tests/gentle-ai-installer.test.ts +52 -51
- package/tests/gentle-ai-renderer.test.ts +36 -2
- package/tests/gentle-ai.test.ts +121 -6
- package/tests/gentle-design.test.ts +365 -1
- package/tests/gentle-shell-bundled.test.ts +91 -5
- package/tests/gentle-shell-launcher.test.ts +1 -1
- package/tests/gentle-shell.test.ts +118 -87
- package/tests/gentle-todo.test.ts +32 -9
- package/tests/hidden-tools.test.ts +188 -0
- package/tests/independent-recovery-delivery.test.ts +19 -5
- package/tests/inprocess-reviewer.test.ts +73 -0
- package/tests/install-wizard.test.ts +27 -1
- package/tests/installer-downloads.test.ts +30 -1
- package/tests/installer-posix-bootstrap.test.ts +114 -13
- package/tests/installer-preflight.test.ts +49 -1
- package/tests/installer-probes.test.ts +146 -0
- package/tests/installer-server.test.ts +203 -4
- package/tests/installer-windows-bootstrap.test.ts +19 -1
- package/tests/judgment-day-severity-admission.test.ts +46 -0
- package/tests/lazy-module-contradictions-contract.test.ts +158 -0
- package/tests/maintainer/desktop-rpc.maintest.ts +632 -0
- package/tests/maintainer/pi-update-source.maintest.ts +48 -18
- package/tests/model-fallback-policy.test.ts +26 -7
- package/tests/nan-provider-runtime.test.ts +17 -3
- package/tests/nan-provider.test.ts +111 -56
- package/tests/native-review-capability-contract.test.ts +11 -1
- package/tests/native-review-cli-nonblocking-findings.test.ts +165 -0
- package/tests/native-review-parity.test.ts +24 -5
- package/tests/native-review-refusal-identity.test.ts +25 -0
- package/tests/no-self-pause-contract.test.ts +11 -3
- package/tests/odd-integration.test.ts +11 -3
- package/tests/odd-routing-contract.test.ts +80 -28
- package/tests/orchestrator-budget.test.ts +30 -5
- package/tests/orchestrator-consultation-sdk.test.ts +32 -6
- package/tests/orchestrator-delegation-mode.test.ts +2 -1
- package/tests/orchestrator-discovery.test.ts +28 -0
- package/tests/orchestrator-prompt-budget.test.ts +11 -2
- package/tests/orchestrator-prompt-diet-contract.test.ts +221 -0
- package/tests/orchestrator-rdd-ownership.test.ts +2 -1
- package/tests/orchestrator-work-search.test.ts +4 -2
- package/tests/package-files.test.ts +25 -1
- package/tests/package-manifest.test.ts +11 -10
- package/tests/parallel-reason-contract.test.ts +6 -3
- package/tests/permission-allowlist.test.ts +200 -30
- package/tests/persona-neutral-voseo.test.ts +4 -4
- package/tests/persona-reply-language.test.ts +1 -1
- package/tests/persona-single-channel.test.ts +13 -5
- package/tests/pi-update-launcher.test.ts +17 -7
- package/tests/pi-update-source.test.ts +113 -6
- package/tests/pi-update.test.ts +294 -19
- package/tests/pnpm-workspace-policy.test.ts +47 -0
- package/tests/primary-context-files.test.ts +233 -0
- package/tests/primary-context-three-files.test.ts +174 -0
- package/tests/provider-defect-handoff.test.ts +9 -2
- package/tests/rdd-aware-verification-contract.test.ts +1 -1
- package/tests/rdd-status-line.test.ts +94 -1
- package/tests/reply-language-anchor-placement.test.ts +184 -0
- package/tests/reply-language-anchor.test.ts +17 -1
- package/tests/request-context-append-only.test.ts +218 -0
- package/tests/request-context-placement.test.ts +148 -0
- package/tests/review-agent-end-preflight.test.ts +79 -1
- package/tests/review-candidate-view.test.ts +65 -0
- package/tests/review-continuation-registry.test.ts +31 -0
- package/tests/review-contract-prompt.test.ts +1 -1
- package/tests/review-controller.test.ts +2 -1
- package/tests/review-coordinator-correction-gate.test.ts +105 -0
- package/tests/review-coordinator-findings.test.ts +206 -0
- package/tests/review-coordinator-nonblocking-scope.test.ts +169 -0
- package/tests/review-coordinator.test.ts +271 -4
- package/tests/review-delivery-preflight.test.ts +462 -0
- package/tests/review-due-compaction.test.ts +31 -0
- package/tests/review-due-public-boundary.test.ts +26 -0
- package/tests/review-integration-nonblocking-findings.test.ts +108 -0
- package/tests/review-json-arguments.test.ts +41 -2
- package/tests/review-ledger-contract.test.ts +19 -40
- package/tests/review-loadout-rdd-off-sdk.test.ts +30 -1
- package/tests/review-one-call-findings.test.ts +131 -0
- package/tests/review-one-call-followups.test.ts +308 -0
- package/tests/review-one-call-reviewer-requests.test.ts +207 -0
- package/tests/review-one-call.test.ts +452 -0
- package/tests/review-relay-transport-agent.test.ts +28 -1
- package/tests/review-reviewer-evidence.test.ts +41 -1
- package/tests/review-risk-assessment.test.ts +52 -5
- package/tests/review-run-statement-guidance-independent.test.ts +54 -0
- package/tests/review-session-standing-permission-controller.test.ts +32 -2
- package/tests/review-tool-prose-budget.test.ts +1 -0
- package/tests/reviewer-stream.test.ts +149 -0
- package/tests/session-profile-extension.test.ts +2 -2
- package/tests/shell-prompt.test.ts +17 -0
- package/tests/shell-usage.test.ts +59 -10
- package/tests/skill-contradictions-contract.test.ts +122 -0
- package/tests/skill-registry-watcher.test.ts +216 -2
- package/tests/skill-registry.test.ts +929 -306
- package/tests/support/design-canvas-dom.ts +369 -0
- package/tests/support/review-coordinator-runtime.ts +212 -17
- package/tests/task-size-routing-contract.test.ts +42 -11
- package/tests/test-strength-contract.test.ts +2 -1
- package/tests/todo-request-append-only.test.ts +214 -0
- package/tests/tool-batching-contract.test.ts +3 -1
- package/tests/update-agent-body-history.test.ts +138 -0
- package/tests/verification-tiers-contract.test.ts +229 -0
- package/tests/verifier-preparation.test.ts +229 -0
- package/tests/verify-agent-contract.test.ts +20 -2
- package/tests/verify-first-launch-contract.test.ts +87 -11
- package/tests/vim-editor-adapter.test.ts +2 -2
- package/tests/work-continuity-contract.test.ts +54 -11
- package/tests/writer-admission-messages.test.ts +232 -0
- package/tests/writer-batch-admission.test.ts +66 -0
- package/tests/writer-edit-surface-scope.test.ts +52 -20
- package/tests/writer-feature-document-admission.test.ts +184 -0
- package/tests/yolo-mode.test.ts +39 -0
- package/skills/_shared/review-ledger-contract.md +0 -106
- /package/contracts/review-provider-contract-mirror/{v1.4.0 → v1.5.0}/bundle/README.md +0 -0
- /package/contracts/review-provider-contract-mirror/{v1.4.0 → v1.5.0}/bundle/schemas/lens.schema.json +0 -0
- /package/contracts/review-provider-contract-mirror/{v1.4.0 → v1.5.0}/bundle/schemas/refuter.schema.json +0 -0
- /package/contracts/review-provider-contract-mirror/{v1.4.0 → v1.5.0}/bundle/schemas/targeted-validator.schema.json +0 -0
- /package/contracts/review-provider-contract-mirror/{v1.4.0 → v1.5.0}/bundle/vectors/lens.json +0 -0
- /package/contracts/review-provider-contract-mirror/{v1.4.0 → v1.5.0}/bundle/vectors/refuter.json +0 -0
- /package/contracts/review-provider-contract-mirror/{v1.4.0 → v1.5.0}/bundle/vectors/targeted-validator.json +0 -0
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
// Gentle Desktop background-jobs bridge: reaches the existing jobs owner (the
|
|
2
|
+
// single parent-session `JobRegistry` plus the parent `MonitorController`,
|
|
3
|
+
// inside `extensions/gentle-agents.ts`) through a defensive, versioned
|
|
4
|
+
// owner-service event/adapter — the jobs analogue of the usage
|
|
5
|
+
// `discoverDesktopUsageService` seam. It never starts, stops, or polls a job
|
|
6
|
+
// itself and keeps no second registry: every list and every cancel is the
|
|
7
|
+
// owner's own answer, shaped into the Desktop RPC contract by
|
|
8
|
+
// `lib/desktop-rpc-jobs.ts`.
|
|
9
|
+
|
|
10
|
+
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
11
|
+
import {
|
|
12
|
+
DESKTOP_JOBS_EVENT,
|
|
13
|
+
DESKTOP_JOBS_SERVICE_CHANNEL,
|
|
14
|
+
isDesktopJobsServiceShape,
|
|
15
|
+
JOB_NOT_FOUND,
|
|
16
|
+
JOBS_UNAVAILABLE,
|
|
17
|
+
parseCancelJobParams,
|
|
18
|
+
parseDesktopJobEntry,
|
|
19
|
+
parseListJobsParams,
|
|
20
|
+
type DesktopJobEntry,
|
|
21
|
+
type DesktopJobsOwnerRead,
|
|
22
|
+
type DesktopJobsService,
|
|
23
|
+
} from "./desktop-rpc-jobs.ts";
|
|
24
|
+
import type { DesktopRpcOutcome } from "./desktop-rpc.ts";
|
|
25
|
+
|
|
26
|
+
const DISCOVERY_TIMEOUT_MS = 100;
|
|
27
|
+
|
|
28
|
+
/** The `jobs_update` event payload: the active session's projected job list. */
|
|
29
|
+
export interface DesktopJobsUpdateEvent {
|
|
30
|
+
jobs: DesktopJobEntry[];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
interface HostUiJobsRequest { context: ExtensionContext; respond(service: DesktopJobsService): void }
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Discover the live jobs owner exactly like the usage seam: emit a bounded,
|
|
37
|
+
* versioned request and accept only an adapter-shaped responder.
|
|
38
|
+
*/
|
|
39
|
+
export function discoverDesktopJobsService(pi: ExtensionAPI, context: ExtensionContext): Promise<DesktopJobsService | undefined> {
|
|
40
|
+
return new Promise((resolve) => {
|
|
41
|
+
let settled = false;
|
|
42
|
+
const timer = setTimeout(() => { settled = true; resolve(undefined); }, DISCOVERY_TIMEOUT_MS);
|
|
43
|
+
const respond = (value: unknown) => {
|
|
44
|
+
if (!isDesktopJobsServiceShape(value)) return;
|
|
45
|
+
if (settled) { value.dispose(); return; }
|
|
46
|
+
settled = true; clearTimeout(timer); resolve(value);
|
|
47
|
+
};
|
|
48
|
+
try {
|
|
49
|
+
pi.events.emit(DESKTOP_JOBS_SERVICE_CHANNEL, { context, respond } satisfies HostUiJobsRequest);
|
|
50
|
+
} catch {
|
|
51
|
+
settled = true; clearTimeout(timer); resolve(undefined);
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
type Emit = (event: string, data: Record<string, unknown>) => void;
|
|
57
|
+
|
|
58
|
+
interface DesktopJobsBridgeOptions {
|
|
59
|
+
discover?: typeof discoverDesktopJobsService;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* The Desktop domain slice for `list_jobs`, `cancel_job`, and the `jobs_update`
|
|
64
|
+
* event. It is wired to the router's `emit` so agent-driven registry changes
|
|
65
|
+
* become ordered events, and it owns the session-scoped lifecycle: a reset
|
|
66
|
+
* (session replacement or shutdown) disconnects the owner observer and drops
|
|
67
|
+
* stale async outcomes. There is no start/run method here — those remain the
|
|
68
|
+
* `bash_background`/`monitor` tools' job.
|
|
69
|
+
*/
|
|
70
|
+
export class DesktopJobsBridge {
|
|
71
|
+
private readonly pi: ExtensionAPI;
|
|
72
|
+
private readonly discover: typeof discoverDesktopJobsService;
|
|
73
|
+
private host: ExtensionContext | undefined;
|
|
74
|
+
private service: DesktopJobsService | undefined;
|
|
75
|
+
private discovering: Promise<DesktopJobsService | undefined> | undefined;
|
|
76
|
+
private generation = 0;
|
|
77
|
+
private observeDispose: (() => void) | undefined;
|
|
78
|
+
private observeGeneration = 0;
|
|
79
|
+
private emit: Emit | undefined;
|
|
80
|
+
/** Byte-identical projected list already emitted; suppresses observer duplicates. */
|
|
81
|
+
private lastEmittedJobs: string | undefined;
|
|
82
|
+
|
|
83
|
+
constructor(pi: ExtensionAPI, options: DesktopJobsBridgeOptions = {}) {
|
|
84
|
+
this.pi = pi;
|
|
85
|
+
this.discover = options.discover ?? discoverDesktopJobsService;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** The live interactive context of this Desktop session; bound per command. */
|
|
89
|
+
attach(context: ExtensionContext): void {
|
|
90
|
+
this.host = context;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Route agent-driven `jobs_update` events through the router's sequencer. */
|
|
94
|
+
wire(emit: Emit): void {
|
|
95
|
+
this.emit = emit;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Eager discovery/subscription so agent-driven job changes arrive before any request. */
|
|
99
|
+
async start(): Promise<void> {
|
|
100
|
+
const host = this.host;
|
|
101
|
+
if (!host) return;
|
|
102
|
+
await this.owner(host);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Session start/shutdown: the owner adapter of a previous session is invalid. */
|
|
106
|
+
reset(): void {
|
|
107
|
+
this.generation += 1;
|
|
108
|
+
this.observeDispose?.();
|
|
109
|
+
this.observeDispose = undefined;
|
|
110
|
+
const service = this.service;
|
|
111
|
+
this.service = undefined;
|
|
112
|
+
this.discovering = undefined;
|
|
113
|
+
this.host = undefined;
|
|
114
|
+
this.emit = undefined;
|
|
115
|
+
this.lastEmittedJobs = undefined;
|
|
116
|
+
try {
|
|
117
|
+
service?.dispose();
|
|
118
|
+
} catch {
|
|
119
|
+
/* owner teardown */
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
async handle(method: string, params: Record<string, unknown>): Promise<DesktopRpcOutcome | undefined> {
|
|
124
|
+
if (method !== "list_jobs" && method !== "cancel_job") return undefined;
|
|
125
|
+
const host = this.host;
|
|
126
|
+
if (!host) return { ok: false, error: JOBS_UNAVAILABLE };
|
|
127
|
+
// Parse before discovery: a malformed request must never trigger owner work.
|
|
128
|
+
if (method === "list_jobs") {
|
|
129
|
+
const parsed = parseListJobsParams(params);
|
|
130
|
+
if ("error" in parsed) return { ok: false, error: parsed.error };
|
|
131
|
+
} else {
|
|
132
|
+
const parsed = parseCancelJobParams(params);
|
|
133
|
+
if ("error" in parsed) return { ok: false, error: parsed.error };
|
|
134
|
+
}
|
|
135
|
+
const generation = this.generation;
|
|
136
|
+
const service = await this.owner(host);
|
|
137
|
+
if (generation !== this.generation || !service) return { ok: false, error: JOBS_UNAVAILABLE };
|
|
138
|
+
return method === "list_jobs" ? this.list(service) : this.cancel(service, params);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
private async owner(context: ExtensionContext): Promise<DesktopJobsService | undefined> {
|
|
142
|
+
if (this.service) return this.service;
|
|
143
|
+
if (!this.discovering) {
|
|
144
|
+
const generation = this.generation;
|
|
145
|
+
const discovering = Promise.resolve()
|
|
146
|
+
.then(() => this.discover(this.pi, context))
|
|
147
|
+
.then((service) => {
|
|
148
|
+
// A reset/replacement happened while discovery was in flight: the
|
|
149
|
+
// late adapter belongs to a superseded session. Dispose it and never
|
|
150
|
+
// cache or re-subscribe it for the next session.
|
|
151
|
+
if (generation !== this.generation || !service) {
|
|
152
|
+
try { service?.dispose(); } catch { /* owner teardown */ }
|
|
153
|
+
return undefined;
|
|
154
|
+
}
|
|
155
|
+
this.service = service;
|
|
156
|
+
this.subscribe(service);
|
|
157
|
+
return service;
|
|
158
|
+
})
|
|
159
|
+
.catch(() => undefined)
|
|
160
|
+
.finally(() => {
|
|
161
|
+
if (this.discovering === discovering) this.discovering = undefined;
|
|
162
|
+
});
|
|
163
|
+
this.discovering = discovering;
|
|
164
|
+
}
|
|
165
|
+
return this.discovering;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
private subscribe(service: DesktopJobsService): void {
|
|
169
|
+
this.observeGeneration = this.generation;
|
|
170
|
+
try {
|
|
171
|
+
this.observeDispose = service.observe((sessionId) => this.onChanged(service, sessionId));
|
|
172
|
+
} catch {
|
|
173
|
+
this.observeDispose = undefined;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** Agent-driven registry change: forward the active session's projected list. */
|
|
178
|
+
private onChanged(service: DesktopJobsService, sessionId: string): void {
|
|
179
|
+
if (this.observeGeneration !== this.generation) return;
|
|
180
|
+
const host = this.host;
|
|
181
|
+
const emit = this.emit;
|
|
182
|
+
if (!host || !emit) return;
|
|
183
|
+
if (sessionId !== this.activeSessionId(host)) return;
|
|
184
|
+
const read = this.readOwner(service);
|
|
185
|
+
if (!read) return;
|
|
186
|
+
// A single registry change can reach the observer more than once (for
|
|
187
|
+
// example a monitor self-stop flushes its pending lines and then stops).
|
|
188
|
+
// Dedupe byte-identical projected lists so each real change emits once.
|
|
189
|
+
const key = JSON.stringify(read.jobs);
|
|
190
|
+
if (key === this.lastEmittedJobs) return;
|
|
191
|
+
this.lastEmittedJobs = key;
|
|
192
|
+
emit(DESKTOP_JOBS_EVENT, { jobs: read.jobs } satisfies DesktopJobsUpdateEvent);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
private activeSessionId(host: ExtensionContext): string {
|
|
196
|
+
return host.sessionManager?.getSessionId?.() ?? "";
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
private readOwner(service: DesktopJobsService): DesktopJobsOwnerRead | undefined {
|
|
200
|
+
try {
|
|
201
|
+
const read = service.read();
|
|
202
|
+
if (!read || typeof read !== "object") return undefined;
|
|
203
|
+
const jobs = this.parseJobs(read.jobs);
|
|
204
|
+
if (jobs === undefined) return undefined;
|
|
205
|
+
return { sessionId: read.sessionId, jobs };
|
|
206
|
+
} catch {
|
|
207
|
+
// Raw owner failures carry paths and command detail; none of it crosses
|
|
208
|
+
// the contract. An unreadable owner is treated as absent.
|
|
209
|
+
return undefined;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
private parseJobs(value: unknown): DesktopJobEntry[] | undefined {
|
|
214
|
+
if (!Array.isArray(value)) return undefined;
|
|
215
|
+
const jobs: DesktopJobEntry[] = [];
|
|
216
|
+
for (const entry of value) {
|
|
217
|
+
const parsed = parseDesktopJobEntry(entry);
|
|
218
|
+
if (parsed === undefined) return undefined;
|
|
219
|
+
jobs.push(parsed);
|
|
220
|
+
}
|
|
221
|
+
return jobs;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
private list(service: DesktopJobsService): DesktopRpcOutcome {
|
|
225
|
+
const read = this.readOwner(service);
|
|
226
|
+
if (!read) return { ok: false, error: JOBS_UNAVAILABLE };
|
|
227
|
+
return { ok: true, data: { jobs: read.jobs } as unknown as Record<string, unknown> };
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
private cancel(service: DesktopJobsService, params: Record<string, unknown>): DesktopRpcOutcome {
|
|
231
|
+
// Params were already validated before discovery; reparse defensively.
|
|
232
|
+
const parsed = parseCancelJobParams(params);
|
|
233
|
+
if ("error" in parsed) return { ok: false, error: parsed.error };
|
|
234
|
+
let outcome: ReturnType<DesktopJobsService["cancel"]>;
|
|
235
|
+
try {
|
|
236
|
+
outcome = service.cancel(parsed.id);
|
|
237
|
+
} catch {
|
|
238
|
+
// Raw owner failures carry paths and command detail; none of it crosses.
|
|
239
|
+
return { ok: false, error: JOBS_UNAVAILABLE };
|
|
240
|
+
}
|
|
241
|
+
if (!outcome || outcome.kind === "not_found") return { ok: false, error: JOB_NOT_FOUND };
|
|
242
|
+
// The owner's projected job crosses the event bus: re-validate it.
|
|
243
|
+
const job = parseDesktopJobEntry(outcome.job);
|
|
244
|
+
if (job === undefined) return { ok: false, error: JOBS_UNAVAILABLE };
|
|
245
|
+
const data = { job } as unknown as Record<string, unknown>;
|
|
246
|
+
// A no-op (already-terminal) cancellation changes nothing: answer the
|
|
247
|
+
// terminal state and emit no event.
|
|
248
|
+
if (outcome.changed !== true) return { ok: true, data };
|
|
249
|
+
// Response-before-event: carry the event so the router publishes the
|
|
250
|
+
// response line first, then one `jobs_update` with the post-action list.
|
|
251
|
+
const read = this.readOwner(service);
|
|
252
|
+
const jobs = read ? read.jobs : [job];
|
|
253
|
+
this.lastEmittedJobs = JSON.stringify(jobs);
|
|
254
|
+
return {
|
|
255
|
+
ok: true,
|
|
256
|
+
data,
|
|
257
|
+
event: {
|
|
258
|
+
name: DESKTOP_JOBS_EVENT,
|
|
259
|
+
data: { jobs } satisfies DesktopJobsUpdateEvent,
|
|
260
|
+
},
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
}
|
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
// Gentle Desktop subagents slice: the pure contract shared by the Desktop
|
|
2
|
+
// bridge (`lib/desktop-agents-service.ts`) and the owner seam inside
|
|
3
|
+
// `extensions/gentle-agents.ts`.
|
|
4
|
+
//
|
|
5
|
+
// Nothing here starts, stops, or spawns a subagent. It only names the wire
|
|
6
|
+
// shapes, projects the owner's actual `TaskRecord` state into the safe
|
|
7
|
+
// Desktop projection, and defines the bounded active + recent-finished
|
|
8
|
+
// selection the Desktop list uses. The owner (the single parent-session
|
|
9
|
+
// `TaskStore` plus the parent `AgentRunner`, reached through
|
|
10
|
+
// `cancel_subagent`'s exact `runner.cancel` stop path) stays the single
|
|
11
|
+
// source of truth; this module is a projection, not a second store, runner,
|
|
12
|
+
// or process manager.
|
|
13
|
+
|
|
14
|
+
import type { ChildResponseObservation, ChildTokenMeasurement, TaskRecord, TaskStatus, TaskUsage } from "./agents-protocol.ts";
|
|
15
|
+
import { FINISHED_STATUSES, TASK_STATUS } from "./agents-protocol.ts";
|
|
16
|
+
|
|
17
|
+
export const DESKTOP_AGENTS_METHODS = ["list_subagents", "cancel_subagent"] as const;
|
|
18
|
+
export const DESKTOP_AGENTS_EVENT = "agents_update";
|
|
19
|
+
/** Advertised only because the owner seam records the TaskStore transitions this event carries. */
|
|
20
|
+
export const DESKTOP_AGENTS_EVENTS = [DESKTOP_AGENTS_EVENT] as const;
|
|
21
|
+
|
|
22
|
+
// The versioned owner-service discovery seam (the agents analogue of the jobs
|
|
23
|
+
// `gentle:desktop:jobs-service` adapter). The schema string guards the adapter
|
|
24
|
+
// against drift; the bridge ignores any responder whose shape does not match.
|
|
25
|
+
export const DESKTOP_AGENTS_SERVICE_SCHEMA = "gentle-pi.desktop-agents-service/v1";
|
|
26
|
+
export const DESKTOP_AGENTS_SERVICE_CHANNEL = "gentle:desktop:agents-service";
|
|
27
|
+
|
|
28
|
+
/** Upper bound on the number of subagents one list answer carries (active + recent finished). */
|
|
29
|
+
export const DESKTOP_AGENTS_LIST_LIMIT = 50;
|
|
30
|
+
|
|
31
|
+
export type DesktopAgentStatus = TaskStatus;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Per-helper usage: the accumulated total tokens and cost the store already
|
|
35
|
+
* tracks, plus the final-assistant input/output/cache/reasoning breakdown once
|
|
36
|
+
* the runner released it at completion. The breakdown fields are omitted for
|
|
37
|
+
* an in-flight task whose breakdown has not been recorded yet.
|
|
38
|
+
*/
|
|
39
|
+
export interface DesktopAgentUsage {
|
|
40
|
+
/** Accumulated total tokens billed to this helper (`TaskRecord.tokens`). */
|
|
41
|
+
total: number;
|
|
42
|
+
/** Accumulated cost billed to this helper (`TaskRecord.cost`). */
|
|
43
|
+
cost: number;
|
|
44
|
+
input?: number;
|
|
45
|
+
output?: number;
|
|
46
|
+
/** Cache read plus cache write tokens. */
|
|
47
|
+
cache?: number;
|
|
48
|
+
reasoning?: number;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* The closed `lastStep` vocabulary. Raw `TaskRecord.lastStep` text is never
|
|
53
|
+
* trusted: it can carry an error message, a stderr tail, a provider body, or a
|
|
54
|
+
* path. The projection maps status to a fixed phase label, keeping only a
|
|
55
|
+
* hardcoded set of safe active-step labels for running tasks and a generic
|
|
56
|
+
* reason for every terminal status. Nothing else can reach the wire.
|
|
57
|
+
*/
|
|
58
|
+
export const DESKTOP_AGENT_STEPS = [
|
|
59
|
+
"queued", "running", "waiting",
|
|
60
|
+
"starting", "writing", "cleaning up", "generating tool arguments", "question dismissed", "answered", "interrupted",
|
|
61
|
+
"completed", "failed", "cancelled", "timed_out",
|
|
62
|
+
] as const;
|
|
63
|
+
export type DesktopAgentStep = (typeof DESKTOP_AGENT_STEPS)[number];
|
|
64
|
+
|
|
65
|
+
/** Safe active-step labels kept for a running task; anything else maps to "running". */
|
|
66
|
+
const SAFE_ACTIVE_STEPS: ReadonlySet<string> = new Set([
|
|
67
|
+
"starting", "writing", "cleaning up", "generating tool arguments", "question dismissed", "answered", "interrupted",
|
|
68
|
+
]);
|
|
69
|
+
|
|
70
|
+
/** Closed, status-derived step label; never the raw `TaskRecord.lastStep` text. */
|
|
71
|
+
function projectDesktopLastStep(task: TaskRecord): DesktopAgentStep {
|
|
72
|
+
switch (task.status) {
|
|
73
|
+
case TASK_STATUS.COMPLETED: return "completed";
|
|
74
|
+
case TASK_STATUS.FAILED: return "failed";
|
|
75
|
+
case TASK_STATUS.CANCELLED: return "cancelled";
|
|
76
|
+
case TASK_STATUS.TIMED_OUT: return "timed_out";
|
|
77
|
+
case TASK_STATUS.QUEUED: return "queued";
|
|
78
|
+
case TASK_STATUS.WAITING: return "waiting";
|
|
79
|
+
case TASK_STATUS.RUNNING:
|
|
80
|
+
return SAFE_ACTIVE_STEPS.has(task.lastStep) ? (task.lastStep as DesktopAgentStep) : "running";
|
|
81
|
+
default:
|
|
82
|
+
return "running";
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* The safe wire window for one subagent. Only these fields exist on the wire:
|
|
88
|
+
* identity, the agent name, the status word, the routed model plus its
|
|
89
|
+
* launch-layer source and the fallback it moved away from, the thinking level,
|
|
90
|
+
* the closed step label, and the bounded usage window. Never the prompt
|
|
91
|
+
* text, tool args/output, cwd or session path, result, error text, or any
|
|
92
|
+
* session internal.
|
|
93
|
+
*/
|
|
94
|
+
export interface DesktopAgentEntry {
|
|
95
|
+
id: string;
|
|
96
|
+
agent: string;
|
|
97
|
+
status: DesktopAgentStatus;
|
|
98
|
+
model: string;
|
|
99
|
+
modelSource?: string;
|
|
100
|
+
fallbackFrom?: string;
|
|
101
|
+
thinking?: string;
|
|
102
|
+
lastStep: DesktopAgentStep;
|
|
103
|
+
usage: DesktopAgentUsage;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** The owner's list answer for one session: which session, plus its projected agents. */
|
|
107
|
+
export interface DesktopAgentsOwnerRead {
|
|
108
|
+
sessionId: string;
|
|
109
|
+
agents: DesktopAgentEntry[];
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* A `cancel_subagent` owner result. `requested` is `true` only when
|
|
114
|
+
* `runner.cancel` returned `true` (the owner found an active task and accepted
|
|
115
|
+
* the cancellation). `changed` is `true` only when the store actually
|
|
116
|
+
* transitioned synchronously as a result of that call (a queued task becoming
|
|
117
|
+
* `cancelled`). A running task's cancellation is asynchronous: `requested` is
|
|
118
|
+
* `true` but `changed` is `false`, and the eventual store transition arrives
|
|
119
|
+
* through the normal `agents_update` path, never an immediate fabricated one.
|
|
120
|
+
*/
|
|
121
|
+
export type DesktopAgentsCancelOutcome =
|
|
122
|
+
| { kind: "ok"; agent: DesktopAgentEntry; requested: boolean; changed: boolean }
|
|
123
|
+
| { kind: "not_found" };
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* The owner-service adapter the bridge discovers. Every method is defensive:
|
|
127
|
+
* after `dispose()` it answers empty/no-op rather than speaking into a
|
|
128
|
+
* replaced session.
|
|
129
|
+
*/
|
|
130
|
+
export interface DesktopAgentsService {
|
|
131
|
+
schema: typeof DESKTOP_AGENTS_SERVICE_SCHEMA;
|
|
132
|
+
read(): DesktopAgentsOwnerRead;
|
|
133
|
+
cancel(id: string): DesktopAgentsCancelOutcome;
|
|
134
|
+
/** Subscribe to actual TaskStore transitions; the unsubscribe stops delivery. */
|
|
135
|
+
observe(onChange: (sessionId: string) => void): () => void;
|
|
136
|
+
dispose(): void;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export const INVALID_AGENTS_PARAMS = { code: "invalid_request", message: "Invalid subagents parameters" } as const;
|
|
140
|
+
export const AGENTS_UNAVAILABLE = { code: "agents_unavailable", message: "Subagents owner unavailable for this session" } as const;
|
|
141
|
+
export const AGENT_NOT_FOUND = { code: "not_found", message: "No subagent with that id in this session" } as const;
|
|
142
|
+
|
|
143
|
+
/** Freeform name/model bounds; `lastStep` is closed, never truncated raw text. */
|
|
144
|
+
const AGENT_FIELD_LIMIT = 200;
|
|
145
|
+
const MODEL_FIELD_LIMIT = 128;
|
|
146
|
+
const SOURCE_FIELD_LIMIT = 64;
|
|
147
|
+
const THINKING_FIELD_LIMIT = 32;
|
|
148
|
+
|
|
149
|
+
function truncate(text: string, limit: number): string {
|
|
150
|
+
return text.length <= limit ? text : `${text.slice(0, Math.max(0, limit - 1))}…`;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function projectDesktopAgentUsage(task: TaskRecord): DesktopAgentUsage {
|
|
154
|
+
const usage: DesktopAgentUsage = { total: task.tokens, cost: task.cost };
|
|
155
|
+
if (task.usage !== undefined) {
|
|
156
|
+
usage.input = task.usage.input;
|
|
157
|
+
usage.output = task.usage.output;
|
|
158
|
+
usage.cache = task.usage.cacheRead + task.usage.cacheWrite;
|
|
159
|
+
usage.reasoning = task.usage.reasoning;
|
|
160
|
+
}
|
|
161
|
+
return usage;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* The whitelisted projection of one owner `TaskRecord`. Every field is rebuilt
|
|
166
|
+
* from the named members only, so a foreign or stale object can never smuggle
|
|
167
|
+
* a prompt, path, tool argument, result, or secret into the Desktop payload.
|
|
168
|
+
*/
|
|
169
|
+
export function projectDesktopAgent(task: TaskRecord): DesktopAgentEntry {
|
|
170
|
+
const entry: DesktopAgentEntry = {
|
|
171
|
+
id: task.id,
|
|
172
|
+
agent: truncate(task.agent, AGENT_FIELD_LIMIT),
|
|
173
|
+
status: task.status,
|
|
174
|
+
model: truncate(task.model, MODEL_FIELD_LIMIT),
|
|
175
|
+
lastStep: projectDesktopLastStep(task),
|
|
176
|
+
usage: projectDesktopAgentUsage(task),
|
|
177
|
+
};
|
|
178
|
+
if (task.modelSource !== undefined) entry.modelSource = truncate(task.modelSource, SOURCE_FIELD_LIMIT);
|
|
179
|
+
if (task.fallbackFrom !== undefined) entry.fallbackFrom = truncate(task.fallbackFrom, MODEL_FIELD_LIMIT);
|
|
180
|
+
if (task.thinking !== undefined) entry.thinking = truncate(task.thinking, THINKING_FIELD_LIMIT);
|
|
181
|
+
return entry;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* The bounded `list_subagents` selection: active tasks first (recency order,
|
|
186
|
+
* matching `TaskStore#list`), then finished tasks (newest end first), capped
|
|
187
|
+
* so the answer can never grow with the lifetime of the process. The store
|
|
188
|
+
* already evicts finished tasks beyond its own retention, so no separate
|
|
189
|
+
* linger window is needed here.
|
|
190
|
+
*/
|
|
191
|
+
export function selectDesktopAgents(tasks: readonly TaskRecord[]): DesktopAgentEntry[] {
|
|
192
|
+
const active = tasks.filter((task) => !FINISHED_STATUSES.includes(task.status));
|
|
193
|
+
const finished = tasks
|
|
194
|
+
.filter((task) => FINISHED_STATUSES.includes(task.status))
|
|
195
|
+
.sort((left, right) => (right.endedAt ?? 0) - (left.endedAt ?? 0));
|
|
196
|
+
return [...active, ...finished].slice(0, DESKTOP_AGENTS_LIST_LIMIT).map(projectDesktopAgent);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function reportedTokens(measurement: ChildTokenMeasurement): number {
|
|
200
|
+
return measurement.state === "reported" ? measurement.value : 0;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Accumulates the final-assistant token breakdown across the observations the
|
|
205
|
+
* runner delivered at completion. Only `reported` measurements count; an
|
|
206
|
+
* `unavailable` measurement contributes zero rather than being guessed. The
|
|
207
|
+
* accumulated total is intentionally not derived from these observations:
|
|
208
|
+
* `TaskRecord.tokens` is the store's own running total.
|
|
209
|
+
*/
|
|
210
|
+
export function accumulateTaskUsage(responses: readonly ChildResponseObservation[]): TaskUsage {
|
|
211
|
+
const usage: TaskUsage = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, reasoning: 0 };
|
|
212
|
+
for (const response of responses) {
|
|
213
|
+
usage.input += reportedTokens(response.tokens.input);
|
|
214
|
+
usage.output += reportedTokens(response.tokens.output);
|
|
215
|
+
usage.cacheRead += reportedTokens(response.tokens.cacheRead);
|
|
216
|
+
usage.cacheWrite += reportedTokens(response.tokens.cacheWrite);
|
|
217
|
+
usage.reasoning += reportedTokens(response.tokens.reasoning);
|
|
218
|
+
}
|
|
219
|
+
return usage;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function nonNegativeNumber(value: unknown): value is number {
|
|
223
|
+
return typeof value === "number" && Number.isFinite(value) && value >= 0;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function parseDesktopAgentUsage(value: unknown): DesktopAgentUsage | undefined {
|
|
227
|
+
if (!value || typeof value !== "object") return undefined;
|
|
228
|
+
const raw = value as Record<string, unknown>;
|
|
229
|
+
if (!nonNegativeNumber(raw.total) || !nonNegativeNumber(raw.cost)) return undefined;
|
|
230
|
+
const usage: DesktopAgentUsage = { total: raw.total, cost: raw.cost };
|
|
231
|
+
for (const field of ["input", "output", "cache", "reasoning"] as const) {
|
|
232
|
+
const next = raw[field];
|
|
233
|
+
if (next !== undefined) {
|
|
234
|
+
if (!nonNegativeNumber(next)) return undefined;
|
|
235
|
+
usage[field] = next;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
return usage;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Defensive validation of one owner-projected entry. The owner's answer crosses
|
|
243
|
+
* the event bus, so its shape is never trusted beyond the fields the bridge
|
|
244
|
+
* projects; anything else is junk the bridge answers `agents_unavailable` for
|
|
245
|
+
* rather than projecting into a malformed success.
|
|
246
|
+
*/
|
|
247
|
+
export function parseDesktopAgentEntry(value: unknown): DesktopAgentEntry | undefined {
|
|
248
|
+
if (!value || typeof value !== "object") return undefined;
|
|
249
|
+
const raw = value as Record<string, unknown>;
|
|
250
|
+
if (typeof raw.id !== "string" || raw.id.length === 0) return undefined;
|
|
251
|
+
if (typeof raw.agent !== "string" || raw.agent.length > AGENT_FIELD_LIMIT + 1) return undefined;
|
|
252
|
+
if (!Object.values(TASK_STATUS).includes(raw.status as TaskStatus)) return undefined;
|
|
253
|
+
if (typeof raw.model !== "string" || raw.model.length > MODEL_FIELD_LIMIT + 1) return undefined;
|
|
254
|
+
if (!DESKTOP_AGENT_STEPS.includes(raw.lastStep as DesktopAgentStep)) return undefined;
|
|
255
|
+
const usage = parseDesktopAgentUsage(raw.usage);
|
|
256
|
+
if (usage === undefined) return undefined;
|
|
257
|
+
const entry: DesktopAgentEntry = { id: raw.id, agent: raw.agent, status: raw.status as TaskStatus, model: raw.model, lastStep: raw.lastStep as DesktopAgentStep, usage };
|
|
258
|
+
for (const field of ["modelSource", "fallbackFrom", "thinking"] as const) {
|
|
259
|
+
const next = raw[field];
|
|
260
|
+
if (next !== undefined) {
|
|
261
|
+
if (typeof next !== "string") return undefined;
|
|
262
|
+
entry[field] = next;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
return entry;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* `list_subagents` params must be an empty object; anything else is a typed
|
|
270
|
+
* `invalid_request` before any owner work happens.
|
|
271
|
+
*/
|
|
272
|
+
export function parseListSubagentsParams(params: Record<string, unknown>): { ok: true } | { error: { code: string; message: string } } {
|
|
273
|
+
return Object.keys(params).length === 0 ? { ok: true } : { error: INVALID_AGENTS_PARAMS };
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* `cancel_subagent` params are exactly `{ id: non-empty string }`. Anything
|
|
278
|
+
* else is a typed `invalid_request` before any owner work happens.
|
|
279
|
+
*/
|
|
280
|
+
export function parseCancelSubagentParams(params: Record<string, unknown>): { id: string } | { error: { code: string; message: string } } {
|
|
281
|
+
const keys = Object.keys(params);
|
|
282
|
+
if (keys.length !== 1 || keys[0] !== "id" || typeof params.id !== "string" || params.id.length === 0) {
|
|
283
|
+
return { error: INVALID_AGENTS_PARAMS };
|
|
284
|
+
}
|
|
285
|
+
return { id: params.id };
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/** True only for the adapter-shaped object the discovery responder must return. */
|
|
289
|
+
export function isDesktopAgentsServiceShape(value: unknown): value is DesktopAgentsService {
|
|
290
|
+
if (!value || typeof value !== "object") return false;
|
|
291
|
+
const adapter = value as Partial<DesktopAgentsService>;
|
|
292
|
+
if (adapter.schema !== DESKTOP_AGENTS_SERVICE_SCHEMA) return false;
|
|
293
|
+
return [adapter.read, adapter.cancel, adapter.observe, adapter.dispose].every((fn) => typeof fn === "function");
|
|
294
|
+
}
|