@gajae-code/coding-agent 0.11.6 → 0.11.8
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 +92 -0
- package/dist/types/capability/index.d.ts +10 -20
- package/dist/types/capability/types.d.ts +3 -0
- package/dist/types/cli/args.d.ts +0 -2
- package/dist/types/commands/daemon.d.ts +2 -2
- package/dist/types/commands/sdk.d.ts +20 -2
- package/dist/types/config/keybindings.d.ts +34 -4
- package/dist/types/config/model-profile-activation.d.ts +4 -3
- package/dist/types/config/model-profile-contract.d.ts +38 -0
- package/dist/types/config/settings-schema.d.ts +70 -7
- package/dist/types/edit/modes/patch.d.ts +13 -0
- package/dist/types/edit/path-mutation-lock.d.ts +15 -0
- package/dist/types/eval/py/executor.d.ts +1 -1
- package/dist/types/eval/py/runner-artifact.d.ts +8 -0
- package/dist/types/eval/py/tool-bridge.d.ts +1 -2
- package/dist/types/gjc-runtime/repository-binding.d.ts +63 -0
- package/dist/types/gjc-runtime/ultragoal-guard.d.ts +6 -5
- package/dist/types/gjc-runtime/ultragoal-receipt-freshness.d.ts +20 -0
- package/dist/types/gjc-runtime/ultragoal-runtime.d.ts +43 -5
- package/dist/types/main.d.ts +32 -1
- package/dist/types/modes/components/btw-panel.d.ts +1 -0
- package/dist/types/modes/components/model-selector.d.ts +2 -0
- package/dist/types/modes/components/runtime-mcp-add-wizard.d.ts +2 -1
- package/dist/types/modes/components/tool-status-header.d.ts +4 -2
- package/dist/types/modes/components/welcome.d.ts +2 -0
- package/dist/types/modes/controllers/btw-controller.d.ts +5 -0
- package/dist/types/modes/controllers/command-controller.d.ts +2 -0
- package/dist/types/modes/controllers/selector-controller.d.ts +2 -1
- package/dist/types/modes/interactive-mode.d.ts +7 -2
- package/dist/types/modes/prompt-action-autocomplete.d.ts +6 -1
- package/dist/types/modes/prompt-suggestion-controller.d.ts +35 -0
- package/dist/types/modes/shared/agent-wire/workflow-gate-broker.d.ts +2 -2
- package/dist/types/modes/types.d.ts +4 -0
- package/dist/types/modes/utils/hotkeys-markdown.d.ts +2 -1
- package/dist/types/runtime-mcp/content-limits.d.ts +7 -0
- package/dist/types/runtime-mcp/manager.d.ts +1 -1
- package/dist/types/runtime-mcp/oauth-flow.d.ts +0 -5
- package/dist/types/runtime-mcp/plugin-network-boundary.d.ts +14 -0
- package/dist/types/sdk/broker/broker.d.ts +2 -0
- package/dist/types/sdk/broker/lifecycle.d.ts +6 -0
- package/dist/types/sdk/bus/chat-daemon-control.d.ts +6 -1
- package/dist/types/sdk/bus/config-commands.d.ts +2 -2
- package/dist/types/sdk/bus/config.d.ts +2 -0
- package/dist/types/sdk/bus/conversation-store.d.ts +1 -0
- package/dist/types/sdk/bus/index.d.ts +26 -1
- package/dist/types/sdk/bus/lifecycle-commands.d.ts +5 -4
- package/dist/types/sdk/bus/notification-orchestration.d.ts +1 -0
- package/dist/types/sdk/bus/notification-service.d.ts +34 -0
- package/dist/types/sdk/bus/prompt-reconciliation.d.ts +105 -0
- package/dist/types/sdk/bus/telegram-daemon-contract.d.ts +18 -3
- package/dist/types/sdk/bus/telegram-daemon-control.d.ts +5 -1
- package/dist/types/sdk/bus/telegram-daemon.d.ts +189 -4
- package/dist/types/sdk/bus/telegram-reference.d.ts +2 -0
- package/dist/types/sdk/bus/topic-registry.d.ts +88 -2
- package/dist/types/sdk/client/discovery.d.ts +2 -0
- package/dist/types/sdk/client/liveness.d.ts +7 -0
- package/dist/types/sdk/host/control/operations.d.ts +1 -1
- package/dist/types/sdk/host/host.d.ts +1 -1
- package/dist/types/sdk/host/query/handlers.d.ts +9 -0
- package/dist/types/sdk/index.d.ts +2 -0
- package/dist/types/sdk/prompt-status.d.ts +88 -0
- package/dist/types/sdk/session-directory.d.ts +1 -1
- package/dist/types/sdk/startup-capability.d.ts +3 -0
- package/dist/types/sdk/transport/auth-preface.d.ts +9 -0
- package/dist/types/sdk/transport/index.d.ts +12 -0
- package/dist/types/sdk/transport/relay.d.ts +43 -0
- package/dist/types/sdk/transport/serve-cli.d.ts +4 -0
- package/dist/types/sdk/transport/socket.d.ts +7 -0
- package/dist/types/sdk/transport/stdio.d.ts +3 -0
- package/dist/types/session/agent-session.d.ts +30 -5
- package/dist/types/session/btw-contract.d.ts +16 -0
- package/dist/types/session/internal/managed-session-scope.d.ts +32 -9
- package/dist/types/session/internal/managed-session-storage.d.ts +27 -10
- package/dist/types/session/internal/native-publish-outcome.d.ts +48 -0
- package/dist/types/session/session-manager.d.ts +25 -4
- package/dist/types/session/session-storage.d.ts +18 -0
- package/dist/types/setup/credential-import.d.ts +5 -1
- package/dist/types/setup/provider-onboarding.d.ts +2 -0
- package/dist/types/ssh/utils.d.ts +1 -0
- package/dist/types/task/discovery.d.ts +3 -1
- package/dist/types/task/index.d.ts +3 -1
- package/dist/types/task/receipt.d.ts +2 -0
- package/dist/types/task/types.d.ts +94 -0
- package/dist/types/tools/ask.d.ts +454 -174
- package/dist/types/tools/fetch.d.ts +5 -8
- package/dist/types/tools/image-gen.d.ts +63 -1
- package/dist/types/tools/index.d.ts +4 -0
- package/dist/types/tools/skill-discovery.d.ts +7 -0
- package/dist/types/tools/sqlite-reader.d.ts +7 -0
- package/dist/types/utils/prompt-suggestion.d.ts +33 -0
- package/dist/types/utils/shell-snapshot.d.ts +12 -0
- package/dist/types/web/insane/bridge.d.ts +1 -0
- package/dist/types/web/insane/url-guard.d.ts +17 -0
- package/package.json +8 -8
- package/scripts/capture-platform-shortcut-labels-showcase.ts +342 -0
- package/scripts/dogfood-repository-binding.ts +209 -0
- package/scripts/generate-sdk-operation-inventory.ts +4 -0
- package/scripts/run-test-manifest.ts +4 -4
- package/src/capability/index.ts +50 -42
- package/src/capability/types.ts +3 -0
- package/src/cli/args.ts +2 -92
- package/src/cli/notify-cli.ts +5 -38
- package/src/cli/setup-cli.ts +10 -4
- package/src/cli.ts +52 -9
- package/src/commands/daemon.ts +22 -5
- package/src/commands/sdk.ts +44 -11
- package/src/config/keybindings.ts +158 -25
- package/src/config/model-profile-activation.ts +7 -18
- package/src/config/model-profile-contract.ts +161 -0
- package/src/config/model-profiles.ts +18 -0
- package/src/config/model-registry.ts +11 -1
- package/src/config/settings-schema.ts +73 -7
- package/src/coordinator-mcp/model-preset.ts +21 -74
- package/src/defaults/gjc/skills/deep-interview/SKILL.md +2 -2
- package/src/defaults/gjc/skills/ralplan/SKILL.md +3 -1
- package/src/defaults/gjc/skills/ultragoal/SKILL.md +42 -1
- package/src/discovery/ssh.ts +12 -1
- package/src/edit/index.ts +3 -6
- package/src/edit/modes/patch.ts +38 -1
- package/src/edit/modes/replace.ts +42 -0
- package/src/edit/path-mutation-lock.ts +112 -0
- package/src/eval/py/executor.ts +30 -11
- package/src/eval/py/kernel.ts +1 -21
- package/src/eval/py/prelude.py +2 -2
- package/src/eval/py/runner-artifact.ts +84 -0
- package/src/eval/py/tool-bridge.ts +34 -18
- package/src/extensibility/gjc-plugins/runtime-adapters.ts +2 -1
- package/src/extensibility/plugins/marketplace/fetcher.ts +191 -7
- package/src/gjc-runtime/launch-tmux.ts +1 -1
- package/src/gjc-runtime/managed-owner-supervisor.ts +83 -4
- package/src/gjc-runtime/ralplan-runtime.ts +75 -3
- package/src/gjc-runtime/repository-binding.ts +267 -0
- package/src/gjc-runtime/session-state-sidecar.ts +25 -9
- package/src/gjc-runtime/tmux-owner-isolation.ts +2 -1
- package/src/gjc-runtime/tmux-sessions.ts +45 -6
- package/src/gjc-runtime/ultragoal-guard.ts +93 -15
- package/src/gjc-runtime/ultragoal-receipt-freshness.ts +103 -0
- package/src/gjc-runtime/ultragoal-runtime.ts +456 -31
- package/src/gjc-runtime/workflow-manifest.generated.json +58 -0
- package/src/gjc-runtime/workflow-manifest.ts +23 -0
- package/src/internal-urls/docs-index.generated.ts +12 -10
- package/src/internal-urls/local-protocol.ts +64 -21
- package/src/internal-urls/mcp-protocol.ts +73 -11
- package/src/main.ts +111 -37
- package/src/modes/DESIGN.md +5 -0
- package/src/modes/acp/acp-agent.ts +50 -16
- package/src/modes/action-registry.ts +1 -1
- package/src/modes/components/btw-panel.ts +82 -16
- package/src/modes/components/model-selector.ts +73 -11
- package/src/modes/components/plan-preview-overlay.ts +1 -1
- package/src/modes/components/runtime-mcp-add-wizard.ts +5 -0
- package/src/modes/components/tool-status-header.ts +25 -4
- package/src/modes/components/welcome.ts +22 -18
- package/src/modes/controllers/btw-controller.ts +119 -36
- package/src/modes/controllers/command-controller.ts +97 -54
- package/src/modes/controllers/event-controller.ts +10 -0
- package/src/modes/controllers/input-controller.ts +38 -2
- package/src/modes/controllers/runtime-mcp-command-controller.ts +29 -6
- package/src/modes/controllers/selector-controller.ts +149 -63
- package/src/modes/interactive-mode.ts +57 -34
- package/src/modes/prompt-action-autocomplete.ts +32 -13
- package/src/modes/prompt-suggestion-controller.ts +96 -0
- package/src/modes/shared/agent-wire/workflow-gate-broker.ts +2 -2
- package/src/modes/types.ts +4 -0
- package/src/modes/utils/hotkeys-markdown.ts +64 -36
- package/src/modes/utils/ui-helpers.ts +5 -3
- package/src/prompts/agents/architect.md +0 -1
- package/src/prompts/agents/critic.md +0 -1
- package/src/prompts/system/btw-user.md +3 -8
- package/src/prompts/system/prompt-suggestion-system.md +32 -0
- package/src/prompts/system/system-prompt.md +6 -4
- package/src/prompts/tools/cron.md +3 -1
- package/src/prompts/tools/skill-discovery.md +1 -0
- package/src/runtime-mcp/client.ts +41 -55
- package/src/runtime-mcp/content-limits.ts +71 -0
- package/src/runtime-mcp/json-rpc.ts +23 -9
- package/src/runtime-mcp/manager.ts +51 -53
- package/src/runtime-mcp/oauth-flow.ts +0 -72
- package/src/runtime-mcp/plugin-network-boundary.ts +101 -0
- package/src/runtime-mcp/transports/http.ts +63 -22
- package/src/sdk/broker/broker.ts +34 -20
- package/src/sdk/broker/lifecycle.ts +185 -36
- package/src/sdk/bus/chat-daemon-control.ts +19 -5
- package/src/sdk/bus/config-commands.ts +2 -2
- package/src/sdk/bus/config.ts +9 -1
- package/src/sdk/bus/conversation-store.ts +40 -6
- package/src/sdk/bus/index.ts +244 -70
- package/src/sdk/bus/lifecycle-commands.ts +63 -10
- package/src/sdk/bus/lifecycle-control-runtime.ts +11 -2
- package/src/sdk/bus/lifecycle-orchestrator.ts +32 -1
- package/src/sdk/bus/notification-orchestration.ts +12 -0
- package/src/sdk/bus/notification-service.ts +68 -20
- package/src/sdk/bus/prompt-reconciliation.ts +239 -0
- package/src/sdk/bus/recent-activity.ts +90 -26
- package/src/sdk/bus/telegram-daemon-contract.ts +18 -3
- package/src/sdk/bus/telegram-daemon-control.ts +147 -35
- package/src/sdk/bus/telegram-daemon.ts +2463 -450
- package/src/sdk/bus/telegram-reference.ts +20 -2
- package/src/sdk/bus/topic-registry.ts +451 -12
- package/src/sdk/cli/session-cli.ts +15 -2
- package/src/sdk/client/discovery.ts +19 -3
- package/src/sdk/client/liveness.ts +49 -0
- package/src/sdk/host/control/dispatch.ts +1 -1
- package/src/sdk/host/control/operations.ts +1 -1
- package/src/sdk/host/host.ts +2 -6
- package/src/sdk/host/query/handlers.ts +70 -5
- package/src/sdk/index.ts +12 -0
- package/src/sdk/prompt-status.ts +97 -0
- package/src/sdk/protocol/operation-inventory.generated.json +58 -0
- package/src/sdk/protocol/operation-registry.ts +18 -4
- package/src/sdk/session-directory.ts +5 -1
- package/src/sdk/session.ts +31 -5
- package/src/sdk/startup-capability.ts +9 -1
- package/src/sdk/transport/auth-preface.ts +82 -0
- package/src/sdk/transport/index.ts +18 -0
- package/src/sdk/transport/relay.ts +252 -0
- package/src/sdk/transport/serve-cli.ts +105 -0
- package/src/sdk/transport/socket.ts +197 -0
- package/src/sdk/transport/stdio.ts +17 -0
- package/src/session/agent-session.ts +292 -154
- package/src/session/blob-store.ts +25 -7
- package/src/session/btw-contract.ts +59 -0
- package/src/session/internal/managed-session-scope.ts +1163 -137
- package/src/session/internal/managed-session-storage.ts +397 -234
- package/src/session/internal/native-publish-outcome.ts +266 -0
- package/src/session/session-manager.ts +620 -403
- package/src/session/session-storage.ts +128 -11
- package/src/setup/credential-import.ts +28 -3
- package/src/setup/provider-onboarding.ts +30 -1
- package/src/setup/provider-presets.json +2 -1
- package/src/slash-commands/builtin-registry.ts +1 -1
- package/src/ssh/utils.ts +14 -0
- package/src/task/discovery.ts +8 -2
- package/src/task/gjc-command.ts +2 -2
- package/src/task/index.ts +121 -15
- package/src/task/receipt.ts +3 -0
- package/src/task/types.ts +34 -3
- package/src/tools/ask.ts +199 -89
- package/src/tools/ast-edit.ts +2 -2
- package/src/tools/bash-pty-selection.ts +2 -2
- package/src/tools/browser/screenshot-format.ts +6 -1
- package/src/tools/cron.ts +1 -1
- package/src/tools/fetch.ts +13 -131
- package/src/tools/image-gen.ts +476 -23
- package/src/tools/index.ts +4 -0
- package/src/tools/read.ts +2 -0
- package/src/tools/render-utils.ts +7 -1
- package/src/tools/skill-discovery.ts +39 -3
- package/src/tools/sqlite-reader.ts +63 -5
- package/src/utils/edit-mode.ts +2 -2
- package/src/utils/pasted-image-path.ts +15 -6
- package/src/utils/prompt-suggestion.ts +265 -0
- package/src/utils/shell-snapshot.ts +173 -63
- package/src/utils/sixel.ts +3 -3
- package/src/web/insane/bridge.ts +5 -1
- package/src/web/insane/url-guard.ts +140 -27
- package/src/web/scrapers/docs-rs.ts +30 -5
- package/src/web/scrapers/types.ts +19 -49
- package/src/web/scrapers/utils.ts +12 -26
package/src/discovery/ssh.ts
CHANGED
|
@@ -5,11 +5,12 @@
|
|
|
5
5
|
* Priority: 5 (low, project/user config discovery)
|
|
6
6
|
*/
|
|
7
7
|
import * as path from "node:path";
|
|
8
|
-
import { getSSHConfigPath, tryParseJson } from "@gajae-code/utils";
|
|
8
|
+
import { getSSHConfigPath, logger, tryParseJson } from "@gajae-code/utils";
|
|
9
9
|
import { registerProvider } from "../capability";
|
|
10
10
|
import { readFile } from "../capability/fs";
|
|
11
11
|
import { type SSHHost, sshCapability } from "../capability/ssh";
|
|
12
12
|
import type { LoadContext, LoadResult, SourceMeta } from "../capability/types";
|
|
13
|
+
import { validateSshDestination } from "../ssh/utils";
|
|
13
14
|
import { expandTilde } from "../tools/path-utils";
|
|
14
15
|
import { createSourceMeta, expandEnvVarsDeep } from "./helpers";
|
|
15
16
|
|
|
@@ -58,6 +59,16 @@ function normalizeHost(
|
|
|
58
59
|
warnings.push(`Missing host for SSH entry: ${name}`);
|
|
59
60
|
return null;
|
|
60
61
|
}
|
|
62
|
+
const destinationError = validateSshDestination(raw.username, raw.host);
|
|
63
|
+
if (destinationError) {
|
|
64
|
+
warnings.push(`Invalid destination for SSH entry ${name}: ${destinationError}`);
|
|
65
|
+
logger.warn("Ignoring SSH entry with invalid destination", {
|
|
66
|
+
name,
|
|
67
|
+
path: source.path,
|
|
68
|
+
reason: destinationError,
|
|
69
|
+
});
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
61
72
|
|
|
62
73
|
const port = parsePort(raw.port);
|
|
63
74
|
if (raw.port !== undefined && port === undefined) {
|
package/src/edit/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { AgentTool, AgentToolContext, AgentToolResult, AgentToolUpdateCallback } from "@gajae-code/agent-core";
|
|
2
|
-
import { prompt } from "@gajae-code/utils";
|
|
2
|
+
import { $pickenv, prompt } from "@gajae-code/utils";
|
|
3
3
|
import type * as z from "zod/v4";
|
|
4
4
|
import {
|
|
5
5
|
executeHashlineSingle,
|
|
@@ -352,11 +352,8 @@ export class EditTool implements AgentTool<TInput> {
|
|
|
352
352
|
readonly #pendingDeferredFetches = new Map<string, AbortController>();
|
|
353
353
|
|
|
354
354
|
constructor(private readonly session: ToolSession) {
|
|
355
|
-
const {
|
|
356
|
-
|
|
357
|
-
PI_EDIT_FUZZY_THRESHOLD: editFuzzyThreshold = "auto",
|
|
358
|
-
PI_EDIT_VARIANT: envEditVariant = "auto",
|
|
359
|
-
} = Bun.env;
|
|
355
|
+
const { PI_EDIT_FUZZY: editFuzzy = "auto", PI_EDIT_FUZZY_THRESHOLD: editFuzzyThreshold = "auto" } = Bun.env;
|
|
356
|
+
const envEditVariant = $pickenv("GJC_EDIT_VARIANT", "PI_EDIT_VARIANT") ?? "auto";
|
|
360
357
|
|
|
361
358
|
this.#editMode = resolveConfiguredEditMode(envEditVariant);
|
|
362
359
|
this.#allowFuzzy = resolveAllowFuzzy(session, editFuzzy);
|
package/src/edit/modes/patch.ts
CHANGED
|
@@ -44,6 +44,7 @@ import {
|
|
|
44
44
|
restoreLineEndings,
|
|
45
45
|
stripBom,
|
|
46
46
|
} from "../normalize";
|
|
47
|
+
import { withEditPathMutation } from "../path-mutation-lock";
|
|
47
48
|
import { readEditFileText, serializeEditFileText } from "../read-file";
|
|
48
49
|
import type { EditToolDetails, LspBatchRequest } from "../renderer";
|
|
49
50
|
import {
|
|
@@ -93,6 +94,15 @@ export interface ApplyPatchOptions {
|
|
|
93
94
|
fuzzyThreshold?: number;
|
|
94
95
|
allowFuzzy?: boolean;
|
|
95
96
|
fs?: FileSystem;
|
|
97
|
+
/**
|
|
98
|
+
* When false, skip durable cross-process file locks (in-process path mutex
|
|
99
|
+
* still serializes). Defaults to true only for the real `defaultFileSystem`
|
|
100
|
+
* (or when `fs` is omitted). Disk-backed adapters that are not
|
|
101
|
+
* `defaultFileSystem` (notably production `LspFileSystem` via
|
|
102
|
+
* `executePatchSingle`) MUST pass `crossProcessLock: true` explicitly —
|
|
103
|
+
* object identity is not a durable-lock capability probe.
|
|
104
|
+
*/
|
|
105
|
+
crossProcessLock?: boolean;
|
|
96
106
|
}
|
|
97
107
|
|
|
98
108
|
// ═══════════════════════════════════════════════════════════════════════════
|
|
@@ -1409,13 +1419,29 @@ function applyHunksToContent(
|
|
|
1409
1419
|
|
|
1410
1420
|
/**
|
|
1411
1421
|
* Apply a patch operation to the filesystem.
|
|
1422
|
+
*
|
|
1423
|
+
* Concurrent mutations of the same absolute path are serialized (in-process always;
|
|
1424
|
+
* cross-process file lock when using the real filesystem) so disjoint concurrent
|
|
1425
|
+
* edits cannot silently overwrite each other (#2900).
|
|
1412
1426
|
*/
|
|
1413
1427
|
export async function applyPatch(input: PatchInput, options: ApplyPatchOptions): Promise<ApplyPatchResult> {
|
|
1414
|
-
|
|
1428
|
+
const resolvePath = (p: string): string => resolveToCwd(p, options.cwd);
|
|
1429
|
+
const absolutePath = resolvePath(input.path);
|
|
1430
|
+
const mutationPaths = [absolutePath];
|
|
1431
|
+
if (input.rename) {
|
|
1432
|
+
const destPath = resolvePath(input.rename);
|
|
1433
|
+
if (destPath !== absolutePath) mutationPaths.push(destPath);
|
|
1434
|
+
}
|
|
1435
|
+
const usingDefaultFs = options.fs === undefined || options.fs === defaultFileSystem;
|
|
1436
|
+
// dryRun/preview must stay read-only: never create durable `<path>.lock` dirs
|
|
1437
|
+
// (would fail on read-only parents and mutate the FS during preview) (#2900 review).
|
|
1438
|
+
const crossProcess = options.dryRun === true ? false : (options.crossProcessLock ?? usingDefaultFs);
|
|
1439
|
+
return withEditPathMutation(mutationPaths, () => applyNormalizedPatch(input, options), { crossProcess });
|
|
1415
1440
|
}
|
|
1416
1441
|
|
|
1417
1442
|
/**
|
|
1418
1443
|
* Apply a normalized patch operation to the filesystem.
|
|
1444
|
+
* Caller must already hold path mutation rights when concurrent writers exist.
|
|
1419
1445
|
* @internal
|
|
1420
1446
|
*/
|
|
1421
1447
|
async function applyNormalizedPatch(input: PatchInput, options: ApplyPatchOptions): Promise<ApplyPatchResult> {
|
|
@@ -1514,6 +1540,12 @@ async function applyNormalizedPatch(input: PatchInput, options: ApplyPatchOption
|
|
|
1514
1540
|
const isMove = Boolean(input.rename) && destPath !== absolutePath;
|
|
1515
1541
|
|
|
1516
1542
|
if (!dryRun) {
|
|
1543
|
+
// Commit-time CAS: reject if another writer mutated the file after our
|
|
1544
|
+
// authoritative read (e.g. a process that did not take the path lock).
|
|
1545
|
+
const commitContent = await readExistingPatchFile(fs, absolutePath, input.path);
|
|
1546
|
+
if (commitContent !== originalContent) {
|
|
1547
|
+
throw new ApplyPatchError(`concurrent edit conflict: file changed since read: ${input.path}`);
|
|
1548
|
+
}
|
|
1517
1549
|
if (isMove) {
|
|
1518
1550
|
const parentDir = path.dirname(destPath);
|
|
1519
1551
|
if (parentDir && parentDir !== ".") {
|
|
@@ -1735,11 +1767,16 @@ export async function executePatchSingle(
|
|
|
1735
1767
|
|
|
1736
1768
|
const input: PatchInput = { path: resolvedPath, op, rename: resolvedRename, diff };
|
|
1737
1769
|
const patchFileSystem = new LspFileSystem(writethrough, signal, batchRequest, beginDeferredDiagnosticsForPath);
|
|
1770
|
+
// Production edit path uses LspFileSystem (disk-backed writethrough), which is
|
|
1771
|
+
// not `defaultFileSystem` by object identity. Force durable cross-process
|
|
1772
|
+
// locking so multi-process agents cannot silently race past the path mutex
|
|
1773
|
+
// (#2900 review: do not infer lock capability from FileSystem identity).
|
|
1738
1774
|
const result = await applyPatch(input, {
|
|
1739
1775
|
cwd: session.cwd,
|
|
1740
1776
|
fs: patchFileSystem,
|
|
1741
1777
|
fuzzyThreshold,
|
|
1742
1778
|
allowFuzzy,
|
|
1779
|
+
crossProcessLock: true,
|
|
1743
1780
|
});
|
|
1744
1781
|
|
|
1745
1782
|
// Post-write verification: only meaningful for in-place updates where the
|
|
@@ -55,6 +55,7 @@ void import("@gajae-code/natives")
|
|
|
55
55
|
// Native unavailable; fuzzy matching uses the TS fallback.
|
|
56
56
|
});
|
|
57
57
|
|
|
58
|
+
import { withEditPathMutation } from "../path-mutation-lock";
|
|
58
59
|
import { readEditFileText, serializeEditFileText } from "../read-file";
|
|
59
60
|
import type { EditToolDetails, LspBatchRequest } from "../renderer";
|
|
60
61
|
|
|
@@ -1087,6 +1088,47 @@ export async function executeReplaceSingle(
|
|
|
1087
1088
|
}
|
|
1088
1089
|
|
|
1089
1090
|
const absolutePath = resolvePlanPath(session, path);
|
|
1091
|
+
return withEditPathMutation([absolutePath], () =>
|
|
1092
|
+
executeReplaceSingleUnderLock({
|
|
1093
|
+
session,
|
|
1094
|
+
path,
|
|
1095
|
+
params,
|
|
1096
|
+
signal,
|
|
1097
|
+
batchRequest,
|
|
1098
|
+
allowFuzzy,
|
|
1099
|
+
fuzzyThreshold,
|
|
1100
|
+
writethrough,
|
|
1101
|
+
beginDeferredDiagnosticsForPath,
|
|
1102
|
+
absolutePath,
|
|
1103
|
+
old_text,
|
|
1104
|
+
new_text,
|
|
1105
|
+
all,
|
|
1106
|
+
}),
|
|
1107
|
+
);
|
|
1108
|
+
}
|
|
1109
|
+
|
|
1110
|
+
async function executeReplaceSingleUnderLock(
|
|
1111
|
+
options: ExecuteReplaceSingleOptions & {
|
|
1112
|
+
absolutePath: string;
|
|
1113
|
+
old_text: string;
|
|
1114
|
+
new_text: string;
|
|
1115
|
+
all: boolean | undefined;
|
|
1116
|
+
},
|
|
1117
|
+
): Promise<AgentToolResult<EditToolDetails, typeof replaceEditEntrySchema>> {
|
|
1118
|
+
const {
|
|
1119
|
+
path,
|
|
1120
|
+
signal,
|
|
1121
|
+
batchRequest,
|
|
1122
|
+
allowFuzzy,
|
|
1123
|
+
fuzzyThreshold,
|
|
1124
|
+
writethrough,
|
|
1125
|
+
beginDeferredDiagnosticsForPath,
|
|
1126
|
+
absolutePath,
|
|
1127
|
+
old_text,
|
|
1128
|
+
new_text,
|
|
1129
|
+
all,
|
|
1130
|
+
} = options;
|
|
1131
|
+
|
|
1090
1132
|
const rawContent = await readEditFileText(absolutePath, path);
|
|
1091
1133
|
const { bom, text: content } = stripBom(rawContent);
|
|
1092
1134
|
const originalEnding = detectLineEnding(content);
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Path-scoped mutation coordinator for edit tools.
|
|
3
|
+
*
|
|
4
|
+
* Serializes concurrent read→compute→write windows against the same absolute
|
|
5
|
+
* path so independent sessions cannot silently overwrite each other's successful
|
|
6
|
+
* disjoint edits (https://github.com/Yeachan-Heo/gajae-code/issues/2900).
|
|
7
|
+
*
|
|
8
|
+
* - Always: in-process async mutex keyed by resolved absolute path.
|
|
9
|
+
* - Optionally: durable cross-process `<path>.lock` via `withFileLock` for real
|
|
10
|
+
* filesystem mutations (subagents / separate processes).
|
|
11
|
+
*
|
|
12
|
+
* Nested acquisition of the same path is not supported; callers that already
|
|
13
|
+
* hold the lock must not re-enter.
|
|
14
|
+
*/
|
|
15
|
+
import * as path from "node:path";
|
|
16
|
+
import { type FileLockOptions, withFileLock } from "../config/file-lock";
|
|
17
|
+
|
|
18
|
+
type AsyncMutex = {
|
|
19
|
+
acquire(): Promise<() => void>;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const pathMutexes = new Map<string, AsyncMutex>();
|
|
23
|
+
|
|
24
|
+
function createAsyncMutex(): AsyncMutex {
|
|
25
|
+
let locked = false;
|
|
26
|
+
const waiters: Array<() => void> = [];
|
|
27
|
+
return {
|
|
28
|
+
async acquire(): Promise<() => void> {
|
|
29
|
+
if (!locked) {
|
|
30
|
+
locked = true;
|
|
31
|
+
return () => release();
|
|
32
|
+
}
|
|
33
|
+
const { promise, resolve } = Promise.withResolvers<void>();
|
|
34
|
+
waiters.push(resolve);
|
|
35
|
+
await promise;
|
|
36
|
+
return () => release();
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
function release(): void {
|
|
41
|
+
const next = waiters.shift();
|
|
42
|
+
if (next) {
|
|
43
|
+
next();
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
locked = false;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function mutexFor(absolutePath: string): AsyncMutex {
|
|
51
|
+
const key = path.resolve(absolutePath);
|
|
52
|
+
let mutex = pathMutexes.get(key);
|
|
53
|
+
if (!mutex) {
|
|
54
|
+
mutex = createAsyncMutex();
|
|
55
|
+
pathMutexes.set(key, mutex);
|
|
56
|
+
}
|
|
57
|
+
return mutex;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async function withInProcessPathLock<T>(absolutePath: string, fn: () => Promise<T>): Promise<T> {
|
|
61
|
+
const release = await mutexFor(absolutePath).acquire();
|
|
62
|
+
try {
|
|
63
|
+
return await fn();
|
|
64
|
+
} finally {
|
|
65
|
+
release();
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Default lock budget: long enough for large writes + formatter writeback. */
|
|
70
|
+
const DEFAULT_CROSS_PROCESS_LOCK: FileLockOptions = {
|
|
71
|
+
staleMs: 60_000,
|
|
72
|
+
retries: 600,
|
|
73
|
+
retryDelayMs: 50,
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
export type EditPathMutationOptions = {
|
|
77
|
+
/**
|
|
78
|
+
* When true (default), also acquire a durable cross-process file lock.
|
|
79
|
+
* Disable for injectible/in-memory filesystem tests that do not touch disk.
|
|
80
|
+
*/
|
|
81
|
+
crossProcess?: boolean;
|
|
82
|
+
fileLock?: FileLockOptions;
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Run `fn` while holding exclusive mutation rights for every absolute path.
|
|
87
|
+
* Paths are locked in lexicographic order to avoid deadlocks on multi-path ops
|
|
88
|
+
* (e.g. rename source + destination).
|
|
89
|
+
*/
|
|
90
|
+
export async function withEditPathMutation<T>(
|
|
91
|
+
absolutePaths: readonly string[],
|
|
92
|
+
fn: () => Promise<T>,
|
|
93
|
+
options: EditPathMutationOptions = {},
|
|
94
|
+
): Promise<T> {
|
|
95
|
+
const unique = [...new Set(absolutePaths.map(entry => path.resolve(entry)))].sort();
|
|
96
|
+
if (unique.length === 0) return fn();
|
|
97
|
+
|
|
98
|
+
const crossProcess = options.crossProcess !== false;
|
|
99
|
+
const fileLock = { ...DEFAULT_CROSS_PROCESS_LOCK, ...options.fileLock };
|
|
100
|
+
|
|
101
|
+
const runAt = async (index: number): Promise<T> => {
|
|
102
|
+
if (index >= unique.length) return fn();
|
|
103
|
+
const target = unique[index];
|
|
104
|
+
const next = () => runAt(index + 1);
|
|
105
|
+
return withInProcessPathLock(target, async () => {
|
|
106
|
+
if (!crossProcess) return next();
|
|
107
|
+
return withFileLock(target, next, fileLock);
|
|
108
|
+
});
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
return runAt(0);
|
|
112
|
+
}
|
package/src/eval/py/executor.ts
CHANGED
|
@@ -61,7 +61,7 @@ export interface PythonExecutorOptions {
|
|
|
61
61
|
/** @internal Bridge session id, set by `executePython` before delegating. */
|
|
62
62
|
bridgeSessionId?: string;
|
|
63
63
|
/** @internal Bridge endpoint info, set by `executePython` before delegating. */
|
|
64
|
-
bridge?: { url: string;
|
|
64
|
+
bridge?: { url: string; capability: string };
|
|
65
65
|
}
|
|
66
66
|
|
|
67
67
|
export interface PythonKernelExecutor {
|
|
@@ -106,6 +106,7 @@ export interface PythonResult {
|
|
|
106
106
|
interface PythonSession {
|
|
107
107
|
sessionId: string;
|
|
108
108
|
kernel: PythonKernel;
|
|
109
|
+
bridgeCapability?: string;
|
|
109
110
|
ownerIds: Set<string>;
|
|
110
111
|
hasFallbackOwner: boolean;
|
|
111
112
|
queue: Promise<void>;
|
|
@@ -261,14 +262,14 @@ function buildKernelEnv(options: {
|
|
|
261
262
|
sessionFile?: string;
|
|
262
263
|
artifactsDir?: string;
|
|
263
264
|
bridgeSessionId?: string;
|
|
264
|
-
bridge?: { url: string;
|
|
265
|
+
bridge?: { url: string; capability: string };
|
|
265
266
|
}): Record<string, string> | undefined {
|
|
266
267
|
const env: Record<string, string> = {};
|
|
267
268
|
if (options.sessionFile) env.PI_SESSION_FILE = options.sessionFile;
|
|
268
269
|
if (options.artifactsDir) env.PI_ARTIFACTS_DIR = options.artifactsDir;
|
|
269
270
|
if (options.bridge && options.bridgeSessionId) {
|
|
270
271
|
env.PI_TOOL_BRIDGE_URL = options.bridge.url;
|
|
271
|
-
env.
|
|
272
|
+
env.PI_TOOL_BRIDGE_CAPABILITY = options.bridge.capability;
|
|
272
273
|
env.PI_TOOL_BRIDGE_SESSION = options.bridgeSessionId;
|
|
273
274
|
}
|
|
274
275
|
return Object.keys(env).length > 0 ? env : undefined;
|
|
@@ -328,6 +329,7 @@ async function acquireSession(sessionId: string, cwd: string, options: PythonExe
|
|
|
328
329
|
const session: PythonSession = {
|
|
329
330
|
sessionId,
|
|
330
331
|
kernel,
|
|
332
|
+
bridgeCapability: options.bridge?.capability,
|
|
331
333
|
ownerIds: new Set(),
|
|
332
334
|
hasFallbackOwner: false,
|
|
333
335
|
queue: Promise.resolve(),
|
|
@@ -361,12 +363,25 @@ async function replaceSessionKernel(
|
|
|
361
363
|
throw new PythonExecutionCancelledError(false);
|
|
362
364
|
}
|
|
363
365
|
requireRemainingTimeoutMs(options.deadlineMs);
|
|
364
|
-
const
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
366
|
+
const bridge = options.bridge;
|
|
367
|
+
const previousCapability = bridge?.capability;
|
|
368
|
+
const nextCapability = bridge ? crypto.randomUUID() : undefined;
|
|
369
|
+
if (bridge && nextCapability) bridge.capability = nextCapability;
|
|
370
|
+
let next: PythonKernel | undefined;
|
|
371
|
+
try {
|
|
372
|
+
next = await startKernel(cwd, options);
|
|
373
|
+
if (sessions.get(session.sessionId) !== session) {
|
|
374
|
+
throw new PythonExecutionCancelledError(false);
|
|
375
|
+
}
|
|
376
|
+
session.kernel = next;
|
|
377
|
+
session.bridgeCapability = nextCapability;
|
|
378
|
+
} catch (err) {
|
|
379
|
+
await next?.shutdown().catch(() => undefined);
|
|
380
|
+
if (bridge && previousCapability && bridge.capability === nextCapability) {
|
|
381
|
+
bridge.capability = previousCapability;
|
|
382
|
+
}
|
|
383
|
+
throw err;
|
|
368
384
|
}
|
|
369
|
-
session.kernel = next;
|
|
370
385
|
}
|
|
371
386
|
|
|
372
387
|
async function resetSession(sessionId: string): Promise<void> {
|
|
@@ -481,8 +496,8 @@ async function executeWithKernel(
|
|
|
481
496
|
displayOutputs.push({ type: "status", event });
|
|
482
497
|
});
|
|
483
498
|
const unregisterBridge =
|
|
484
|
-
options?.toolSession && options?.bridgeSessionId
|
|
485
|
-
? registerPyToolBridge(options.bridgeSessionId, {
|
|
499
|
+
options?.toolSession && options?.bridgeSessionId && options.bridge
|
|
500
|
+
? registerPyToolBridge(options.bridgeSessionId, options.bridge.capability, {
|
|
486
501
|
toolSession: options.toolSession,
|
|
487
502
|
signal: options.signal,
|
|
488
503
|
emitStatus,
|
|
@@ -578,7 +593,8 @@ async function ensureKernelAvailable(cwd: string, options: PythonExecutorOptions
|
|
|
578
593
|
async function ensureToolBridge(options: PythonExecutorOptions): Promise<void> {
|
|
579
594
|
if (!options.toolSession || options.bridge) return;
|
|
580
595
|
try {
|
|
581
|
-
|
|
596
|
+
const bridge = await ensurePyToolBridge();
|
|
597
|
+
options.bridge = { ...bridge, capability: crypto.randomUUID() };
|
|
582
598
|
} catch (err) {
|
|
583
599
|
logger.warn("Failed to start Python tool bridge", {
|
|
584
600
|
error: err instanceof Error ? err.message : String(err),
|
|
@@ -607,6 +623,9 @@ async function executeOnSession(code: string, cwd: string, options: PythonExecut
|
|
|
607
623
|
await resetSession(sessionId);
|
|
608
624
|
}
|
|
609
625
|
const session = await acquireSession(sessionId, cwd, options);
|
|
626
|
+
if (options.bridge && session.bridgeCapability) {
|
|
627
|
+
options.bridge.capability = session.bridgeCapability;
|
|
628
|
+
}
|
|
610
629
|
return await runQueued(session, options, async () => {
|
|
611
630
|
if (options.signal?.aborted) {
|
|
612
631
|
throw new PythonExecutionCancelledError(isTimedOutCancellation(options.signal.reason, options.signal));
|
package/src/eval/py/kernel.ts
CHANGED
|
@@ -8,9 +8,6 @@
|
|
|
8
8
|
* Shutdown writes `{"type":"exit"}` and escalates to SIGTERM/SIGKILL on
|
|
9
9
|
* timeout.
|
|
10
10
|
*/
|
|
11
|
-
import * as fs from "node:fs";
|
|
12
|
-
import * as os from "node:os";
|
|
13
|
-
import * as path from "node:path";
|
|
14
11
|
import { $env, isBunTestRuntime, logger, Snowflake } from "@gajae-code/utils";
|
|
15
12
|
import type { Subprocess } from "bun";
|
|
16
13
|
import { $ } from "bun";
|
|
@@ -18,7 +15,7 @@ import { Settings } from "../../config/settings";
|
|
|
18
15
|
import { type KernelDisplayOutput, renderKernelDisplay } from "./display";
|
|
19
16
|
import { resolvePythonIpcTrace, resolvePythonSkipCheck } from "./env";
|
|
20
17
|
import { PYTHON_PRELUDE } from "./prelude";
|
|
21
|
-
import
|
|
18
|
+
import { ensureRunnerScript } from "./runner-artifact";
|
|
22
19
|
import { ensurePythonRuntime, filterEnv, type PythonRuntimeOptions } from "./runtime";
|
|
23
20
|
|
|
24
21
|
export type { KernelDisplayOutput, PythonStatusEvent } from "./display";
|
|
@@ -27,23 +24,6 @@ export { renderKernelDisplay } from "./display";
|
|
|
27
24
|
// Dual-read: `GJC_PYTHON_IPC_TRACE` is preferred, then legacy `PI_PYTHON_IPC_TRACE`.
|
|
28
25
|
const TRACE_IPC = resolvePythonIpcTrace($env);
|
|
29
26
|
|
|
30
|
-
// Cache the runner script on disk so the subprocess loads it normally. Cached
|
|
31
|
-
// per script hash so installs don't race across versions.
|
|
32
|
-
const RUNNER_CACHE_DIR = path.join(os.tmpdir(), "gjc-python-runner");
|
|
33
|
-
let RUNNER_SCRIPT_PATH: string | null = null;
|
|
34
|
-
|
|
35
|
-
async function ensureRunnerScript(): Promise<string> {
|
|
36
|
-
if (RUNNER_SCRIPT_PATH) return RUNNER_SCRIPT_PATH;
|
|
37
|
-
await fs.promises.mkdir(RUNNER_CACHE_DIR, { recursive: true });
|
|
38
|
-
const hash = Bun.hash(RUNNER_SCRIPT).toString(36);
|
|
39
|
-
const target = path.join(RUNNER_CACHE_DIR, `runner-${hash}.py`);
|
|
40
|
-
if (!fs.existsSync(target)) {
|
|
41
|
-
await Bun.write(target, RUNNER_SCRIPT);
|
|
42
|
-
}
|
|
43
|
-
RUNNER_SCRIPT_PATH = target;
|
|
44
|
-
return target;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
27
|
const SHUTDOWN_GRACE_MS = 1_000;
|
|
48
28
|
const STARTUP_TIMEOUT_MS = 10_000;
|
|
49
29
|
// How long to wait after SIGINT for the runner to emit `done`. If the cell is
|
package/src/eval/py/prelude.py
CHANGED
|
@@ -453,10 +453,10 @@ if "__gjc_prelude_loaded__" not in globals():
|
|
|
453
453
|
|
|
454
454
|
if all(
|
|
455
455
|
_k in os.environ
|
|
456
|
-
for _k in ("PI_TOOL_BRIDGE_URL", "
|
|
456
|
+
for _k in ("PI_TOOL_BRIDGE_URL", "PI_TOOL_BRIDGE_CAPABILITY", "PI_TOOL_BRIDGE_SESSION")
|
|
457
457
|
):
|
|
458
458
|
tool = _ToolProxy(
|
|
459
459
|
os.environ["PI_TOOL_BRIDGE_URL"],
|
|
460
|
-
os.environ["
|
|
460
|
+
os.environ["PI_TOOL_BRIDGE_CAPABILITY"],
|
|
461
461
|
os.environ["PI_TOOL_BRIDGE_SESSION"],
|
|
462
462
|
)
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import * as fs from "node:fs/promises";
|
|
2
|
+
import * as os from "node:os";
|
|
3
|
+
import * as path from "node:path";
|
|
4
|
+
import { postmortem } from "@gajae-code/utils";
|
|
5
|
+
import RUNNER_SCRIPT from "./runner.py" with { type: "text" };
|
|
6
|
+
|
|
7
|
+
const DIRECTORY_MODE = 0o700;
|
|
8
|
+
const FILE_MODE = 0o600;
|
|
9
|
+
const DIRECTORY_PREFIX = "gjc-python-runner-";
|
|
10
|
+
|
|
11
|
+
interface RunnerScriptArtifact {
|
|
12
|
+
directory: string;
|
|
13
|
+
scriptPath: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async function initializeRunnerScript(tempRoot: string): Promise<RunnerScriptArtifact> {
|
|
17
|
+
const directory = await fs.mkdtemp(path.join(tempRoot, DIRECTORY_PREFIX));
|
|
18
|
+
try {
|
|
19
|
+
if (process.platform !== "win32") await fs.chmod(directory, DIRECTORY_MODE);
|
|
20
|
+
const scriptPath = path.join(directory, "runner.py");
|
|
21
|
+
const handle = await fs.open(scriptPath, "wx", FILE_MODE);
|
|
22
|
+
try {
|
|
23
|
+
await handle.writeFile(RUNNER_SCRIPT, { encoding: "utf8" });
|
|
24
|
+
if (process.platform !== "win32") await handle.chmod(FILE_MODE);
|
|
25
|
+
} finally {
|
|
26
|
+
await handle.close();
|
|
27
|
+
}
|
|
28
|
+
return { directory, scriptPath };
|
|
29
|
+
} catch (error) {
|
|
30
|
+
await fs.rm(directory, { recursive: true, force: true }).catch(() => {});
|
|
31
|
+
throw error;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface RunnerScriptCache {
|
|
36
|
+
ensureRunnerScript(): Promise<string>;
|
|
37
|
+
cleanup(): Promise<void>;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function createRunnerScriptCache(tempRoot: string): RunnerScriptCache {
|
|
41
|
+
let initialization: Promise<RunnerScriptArtifact> | null = null;
|
|
42
|
+
let artifact: RunnerScriptArtifact | null = null;
|
|
43
|
+
let cleanupAttempt: Promise<void> | null = null;
|
|
44
|
+
|
|
45
|
+
return {
|
|
46
|
+
async ensureRunnerScript() {
|
|
47
|
+
if (artifact) return artifact.scriptPath;
|
|
48
|
+
if (!initialization) initialization = initializeRunnerScript(tempRoot);
|
|
49
|
+
try {
|
|
50
|
+
artifact = await initialization;
|
|
51
|
+
return artifact.scriptPath;
|
|
52
|
+
} catch (error) {
|
|
53
|
+
if (initialization) initialization = null;
|
|
54
|
+
throw error;
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
async cleanup() {
|
|
58
|
+
if (cleanupAttempt) return await cleanupAttempt;
|
|
59
|
+
cleanupAttempt = (async () => {
|
|
60
|
+
const initialized = await initialization?.catch(() => null);
|
|
61
|
+
const directory = artifact?.directory ?? initialized?.directory;
|
|
62
|
+
artifact = null;
|
|
63
|
+
initialization = null;
|
|
64
|
+
if (directory) await fs.rm(directory, { recursive: true, force: true });
|
|
65
|
+
})();
|
|
66
|
+
try {
|
|
67
|
+
await cleanupAttempt;
|
|
68
|
+
} finally {
|
|
69
|
+
cleanupAttempt = null;
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** @internal Test-only initializer for isolated artifact assertions. */
|
|
76
|
+
export function createRunnerScriptInitializer(tempRoot: string): () => Promise<string> {
|
|
77
|
+
return createRunnerScriptCache(tempRoot).ensureRunnerScript;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const processRunnerScriptCache = createRunnerScriptCache(os.tmpdir());
|
|
81
|
+
|
|
82
|
+
export const ensureRunnerScript = () => processRunnerScriptCache.ensureRunnerScript();
|
|
83
|
+
|
|
84
|
+
postmortem.register("python-runner-artifact", () => processRunnerScriptCache.cleanup());
|
|
@@ -19,7 +19,10 @@ export interface PyToolBridgeEntry {
|
|
|
19
19
|
|
|
20
20
|
export interface PyToolBridgeInfo {
|
|
21
21
|
url: string;
|
|
22
|
-
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
interface PyToolBridgeRegistration extends PyToolBridgeEntry {
|
|
25
|
+
sessionId: string;
|
|
23
26
|
}
|
|
24
27
|
|
|
25
28
|
interface BridgeServer {
|
|
@@ -27,11 +30,20 @@ interface BridgeServer {
|
|
|
27
30
|
stop: () => Promise<void>;
|
|
28
31
|
}
|
|
29
32
|
|
|
30
|
-
const registrations = new Map<string,
|
|
33
|
+
const registrations = new Map<string, PyToolBridgeRegistration>();
|
|
31
34
|
let serverPromise: Promise<BridgeServer> | null = null;
|
|
32
35
|
|
|
36
|
+
function isCanonicalCapability(capability: string): boolean {
|
|
37
|
+
return capability.length > 0 && !/\s/.test(capability);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function parseBearerCapability(authorization: string | null): string | null {
|
|
41
|
+
if (!authorization?.startsWith("Bearer ")) return null;
|
|
42
|
+
const capability = authorization.slice("Bearer ".length);
|
|
43
|
+
return isCanonicalCapability(capability) ? capability : null;
|
|
44
|
+
}
|
|
45
|
+
|
|
33
46
|
async function startServer(): Promise<BridgeServer> {
|
|
34
|
-
const token = crypto.randomUUID();
|
|
35
47
|
const server = Bun.serve({
|
|
36
48
|
hostname: "127.0.0.1",
|
|
37
49
|
port: 0,
|
|
@@ -40,7 +52,12 @@ async function startServer(): Promise<BridgeServer> {
|
|
|
40
52
|
if (req.method !== "POST" || url.pathname !== "/v1/tool") {
|
|
41
53
|
return new Response("Not Found", { status: 404 });
|
|
42
54
|
}
|
|
43
|
-
|
|
55
|
+
const capability = parseBearerCapability(req.headers.get("authorization"));
|
|
56
|
+
if (!capability) {
|
|
57
|
+
return new Response("Forbidden", { status: 403 });
|
|
58
|
+
}
|
|
59
|
+
const registration = registrations.get(capability);
|
|
60
|
+
if (!registration) {
|
|
44
61
|
return new Response("Forbidden", { status: 403 });
|
|
45
62
|
}
|
|
46
63
|
|
|
@@ -55,19 +72,15 @@ async function startServer(): Promise<BridgeServer> {
|
|
|
55
72
|
if (!sessionId || !name) {
|
|
56
73
|
return Response.json({ ok: false, error: "Missing session/name" }, { status: 400 });
|
|
57
74
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
return Response.json(
|
|
61
|
-
{ ok: false, error: `No active Python tool bridge session: ${sessionId}` },
|
|
62
|
-
{ status: 200 },
|
|
63
|
-
);
|
|
75
|
+
if (sessionId !== registration.sessionId) {
|
|
76
|
+
return new Response("Forbidden", { status: 403 });
|
|
64
77
|
}
|
|
65
78
|
|
|
66
79
|
try {
|
|
67
80
|
const value = await callSessionTool(name, body.args, {
|
|
68
|
-
session:
|
|
69
|
-
signal:
|
|
70
|
-
emitStatus:
|
|
81
|
+
session: registration.toolSession,
|
|
82
|
+
signal: registration.signal,
|
|
83
|
+
emitStatus: registration.emitStatus,
|
|
71
84
|
});
|
|
72
85
|
return Response.json({ ok: true, value });
|
|
73
86
|
} catch (err) {
|
|
@@ -81,7 +94,6 @@ async function startServer(): Promise<BridgeServer> {
|
|
|
81
94
|
|
|
82
95
|
const info: PyToolBridgeInfo = {
|
|
83
96
|
url: `http://${server.hostname}:${server.port}`,
|
|
84
|
-
token,
|
|
85
97
|
};
|
|
86
98
|
logger.debug("Python tool bridge listening", { url: info.url });
|
|
87
99
|
|
|
@@ -111,11 +123,15 @@ export async function ensurePyToolBridge(): Promise<PyToolBridgeInfo> {
|
|
|
111
123
|
* Register a tool session for the duration of one execution. The returned
|
|
112
124
|
* function MUST be called to remove the entry once execution finishes.
|
|
113
125
|
*/
|
|
114
|
-
export function registerPyToolBridge(sessionId: string, entry: PyToolBridgeEntry): () => void {
|
|
115
|
-
|
|
126
|
+
export function registerPyToolBridge(sessionId: string, capability: string, entry: PyToolBridgeEntry): () => void {
|
|
127
|
+
if (!isCanonicalCapability(capability)) {
|
|
128
|
+
throw new Error("Python tool bridge capability must be a non-empty canonical bearer token");
|
|
129
|
+
}
|
|
130
|
+
const registration = { ...entry, sessionId };
|
|
131
|
+
registrations.set(capability, registration);
|
|
116
132
|
return () => {
|
|
117
|
-
if (registrations.get(
|
|
118
|
-
registrations.delete(
|
|
133
|
+
if (registrations.get(capability) === registration) {
|
|
134
|
+
registrations.delete(capability);
|
|
119
135
|
}
|
|
120
136
|
};
|
|
121
137
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { createHash } from "node:crypto";
|
|
2
2
|
import * as fs from "node:fs/promises";
|
|
3
3
|
import * as path from "node:path";
|
|
4
|
+
import { bindPluginMcpToPublicNetwork } from "../../runtime-mcp/plugin-network-boundary";
|
|
4
5
|
import { loadCustomTools } from "../custom-tools/loader";
|
|
5
6
|
import type { CustomTool } from "../custom-tools/types";
|
|
6
7
|
import { loadEffectiveGjcPluginRegistry, registryPathForScope } from "./registry";
|
|
@@ -330,7 +331,7 @@ export async function buildPluginMcpConfigs(input: { cwd: string }): Promise<{
|
|
|
330
331
|
// resolution path expands ${env:...}/shell templates, which would let
|
|
331
332
|
// a third-party bundle exfiltrate host secrets. Plugin-bundle MCP
|
|
332
333
|
// servers connect without bundle-declared headers.
|
|
333
|
-
configs[m.name] = { type: cfg.transport, url:
|
|
334
|
+
configs[m.name] = bindPluginMcpToPublicNetwork({ type: cfg.transport, url: url.toString() });
|
|
334
335
|
}
|
|
335
336
|
} catch (error) {
|
|
336
337
|
quarantine.push({
|