@cortexkit/opencode-magic-context 0.26.0 → 0.27.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -12
- package/dist/agents/dreamer.d.ts +19 -0
- package/dist/agents/dreamer.d.ts.map +1 -1
- package/dist/agents/hidden-agent-registrations.d.ts +67 -0
- package/dist/agents/hidden-agent-registrations.d.ts.map +1 -0
- package/dist/agents/historian.d.ts +1 -0
- package/dist/agents/historian.d.ts.map +1 -1
- package/dist/agents/permissions.d.ts +15 -44
- package/dist/agents/permissions.d.ts.map +1 -1
- package/dist/agents/smart-note-compiler.d.ts +2 -0
- package/dist/agents/smart-note-compiler.d.ts.map +1 -0
- package/dist/config/index.d.ts +1 -1
- package/dist/config/index.d.ts.map +1 -1
- package/dist/config/migrate-config-location.d.ts +89 -0
- package/dist/config/migrate-config-location.d.ts.map +1 -0
- package/dist/config/migrate-dreamer-v2.d.ts +37 -0
- package/dist/config/migrate-dreamer-v2.d.ts.map +1 -0
- package/dist/config/migrate-experimental.d.ts.map +1 -1
- package/dist/config/project-security.d.ts +3 -0
- package/dist/config/project-security.d.ts.map +1 -1
- package/dist/config/prune-config-leaf.d.ts.map +1 -1
- package/dist/config/schema/magic-context.d.ts +584 -60
- package/dist/config/schema/magic-context.d.ts.map +1 -1
- package/dist/features/magic-context/compaction-marker.d.ts +9 -3
- package/dist/features/magic-context/compaction-marker.d.ts.map +1 -1
- package/dist/features/magic-context/compartment-chunk-embedding.d.ts +1 -1
- package/dist/features/magic-context/compartment-chunk-embedding.d.ts.map +1 -1
- package/dist/features/magic-context/dreamer/classify-prompt.d.ts +50 -0
- package/dist/features/magic-context/dreamer/classify-prompt.d.ts.map +1 -0
- package/dist/features/magic-context/dreamer/classify.d.ts +22 -0
- package/dist/features/magic-context/dreamer/classify.d.ts.map +1 -0
- package/dist/features/magic-context/dreamer/cron.d.ts +72 -0
- package/dist/features/magic-context/dreamer/cron.d.ts.map +1 -0
- package/dist/features/magic-context/dreamer/evaluate-smart-notes.d.ts +30 -0
- package/dist/features/magic-context/dreamer/evaluate-smart-notes.d.ts.map +1 -0
- package/dist/features/magic-context/dreamer/index.d.ts +1 -3
- package/dist/features/magic-context/dreamer/index.d.ts.map +1 -1
- package/dist/features/magic-context/dreamer/lease.d.ts +44 -6
- package/dist/features/magic-context/dreamer/lease.d.ts.map +1 -1
- package/dist/features/magic-context/dreamer/maintain-docs-protected-enforcement.d.ts +13 -0
- package/dist/features/magic-context/dreamer/maintain-docs-protected-enforcement.d.ts.map +1 -0
- package/dist/features/magic-context/dreamer/map-memories-prompt.d.ts +36 -0
- package/dist/features/magic-context/dreamer/map-memories-prompt.d.ts.map +1 -0
- package/dist/features/magic-context/dreamer/map-memories.d.ts +22 -0
- package/dist/features/magic-context/dreamer/map-memories.d.ts.map +1 -0
- package/dist/features/magic-context/dreamer/open-opencode-db.d.ts +7 -0
- package/dist/features/magic-context/dreamer/open-opencode-db.d.ts.map +1 -0
- package/dist/features/magic-context/dreamer/primer-seed.d.ts +25 -0
- package/dist/features/magic-context/dreamer/primer-seed.d.ts.map +1 -0
- package/dist/features/magic-context/dreamer/promote-primers.d.ts +21 -0
- package/dist/features/magic-context/dreamer/promote-primers.d.ts.map +1 -0
- package/dist/features/magic-context/dreamer/protected-regions.d.ts +19 -0
- package/dist/features/magic-context/dreamer/protected-regions.d.ts.map +1 -0
- package/dist/features/magic-context/dreamer/refresh-primers.d.ts +30 -0
- package/dist/features/magic-context/dreamer/refresh-primers.d.ts.map +1 -0
- package/dist/features/magic-context/dreamer/retrospective-learnings.d.ts +47 -0
- package/dist/features/magic-context/dreamer/retrospective-learnings.d.ts.map +1 -0
- package/dist/features/magic-context/dreamer/retrospective-orphan-sweep.d.ts +48 -0
- package/dist/features/magic-context/dreamer/retrospective-orphan-sweep.d.ts.map +1 -0
- package/dist/features/magic-context/dreamer/retrospective-raw-provider.d.ts +81 -0
- package/dist/features/magic-context/dreamer/retrospective-raw-provider.d.ts.map +1 -0
- package/dist/features/magic-context/dreamer/storage-dream-runs.d.ts +8 -0
- package/dist/features/magic-context/dreamer/storage-dream-runs.d.ts.map +1 -1
- package/dist/features/magic-context/dreamer/storage-task-schedule.d.ts +82 -0
- package/dist/features/magic-context/dreamer/storage-task-schedule.d.ts.map +1 -0
- package/dist/features/magic-context/dreamer/task-config.d.ts +28 -0
- package/dist/features/magic-context/dreamer/task-config.d.ts.map +1 -0
- package/dist/features/magic-context/dreamer/task-executor.d.ts +49 -0
- package/dist/features/magic-context/dreamer/task-executor.d.ts.map +1 -0
- package/dist/features/magic-context/dreamer/task-gates.d.ts +29 -0
- package/dist/features/magic-context/dreamer/task-gates.d.ts.map +1 -0
- package/dist/features/magic-context/dreamer/task-prompts.d.ts +37 -6
- package/dist/features/magic-context/dreamer/task-prompts.d.ts.map +1 -1
- package/dist/features/magic-context/dreamer/task-registry.d.ts +48 -0
- package/dist/features/magic-context/dreamer/task-registry.d.ts.map +1 -0
- package/dist/features/magic-context/dreamer/task-scheduler.d.ts +88 -0
- package/dist/features/magic-context/dreamer/task-scheduler.d.ts.map +1 -0
- package/dist/features/magic-context/dreamer/verify-gate.d.ts +43 -0
- package/dist/features/magic-context/dreamer/verify-gate.d.ts.map +1 -0
- package/dist/features/magic-context/dreamer/verify-prompt.d.ts +41 -0
- package/dist/features/magic-context/dreamer/verify-prompt.d.ts.map +1 -0
- package/dist/features/magic-context/dreamer/verify.d.ts +43 -0
- package/dist/features/magic-context/dreamer/verify.d.ts.map +1 -0
- package/dist/features/magic-context/git-commits/search-git-commits.d.ts +2 -0
- package/dist/features/magic-context/git-commits/search-git-commits.d.ts.map +1 -1
- package/dist/features/magic-context/git-commits/storage-git-commit-embeddings.d.ts +4 -4
- package/dist/features/magic-context/git-commits/storage-git-commit-embeddings.d.ts.map +1 -1
- package/dist/features/magic-context/index.d.ts +1 -0
- package/dist/features/magic-context/index.d.ts.map +1 -1
- package/dist/features/magic-context/memory/embedding-cache.d.ts +2 -2
- package/dist/features/magic-context/memory/embedding-cache.d.ts.map +1 -1
- package/dist/features/magic-context/memory/embedding-identity.d.ts.map +1 -1
- package/dist/features/magic-context/memory/embedding-local.d.ts.map +1 -1
- package/dist/features/magic-context/memory/embedding-openai.d.ts +12 -5
- package/dist/features/magic-context/memory/embedding-openai.d.ts.map +1 -1
- package/dist/features/magic-context/memory/embedding.d.ts +2 -2
- package/dist/features/magic-context/memory/embedding.d.ts.map +1 -1
- package/dist/features/magic-context/memory/index.d.ts +4 -1
- package/dist/features/magic-context/memory/index.d.ts.map +1 -1
- package/dist/features/magic-context/memory/memory-migration.d.ts +1 -0
- package/dist/features/magic-context/memory/memory-migration.d.ts.map +1 -1
- package/dist/features/magic-context/memory/promotion.d.ts +16 -4
- package/dist/features/magic-context/memory/promotion.d.ts.map +1 -1
- package/dist/features/magic-context/memory/storage-memory-embeddings.d.ts +2 -2
- package/dist/features/magic-context/memory/storage-memory-embeddings.d.ts.map +1 -1
- package/dist/features/magic-context/memory/storage-memory-verifications.d.ts +31 -0
- package/dist/features/magic-context/memory/storage-memory-verifications.d.ts.map +1 -0
- package/dist/features/magic-context/memory/storage-memory.d.ts +12 -1
- package/dist/features/magic-context/memory/storage-memory.d.ts.map +1 -1
- package/dist/features/magic-context/memory/types.d.ts +4 -0
- package/dist/features/magic-context/memory/types.d.ts.map +1 -1
- package/dist/features/magic-context/memory/verification-paths.d.ts +32 -0
- package/dist/features/magic-context/memory/verification-paths.d.ts.map +1 -0
- package/dist/features/magic-context/message-index.d.ts.map +1 -1
- package/dist/features/magic-context/migrations.d.ts.map +1 -1
- package/dist/features/magic-context/overflow-detection.d.ts.map +1 -1
- package/dist/features/magic-context/primer-clustering.d.ts +29 -0
- package/dist/features/magic-context/primer-clustering.d.ts.map +1 -0
- package/dist/features/magic-context/project-embedding-registry.d.ts +25 -1
- package/dist/features/magic-context/project-embedding-registry.d.ts.map +1 -1
- package/dist/features/magic-context/search.d.ts +12 -2
- package/dist/features/magic-context/search.d.ts.map +1 -1
- package/dist/features/magic-context/sidekick/agent.d.ts.map +1 -1
- package/dist/features/magic-context/smart-notes/capabilities.d.ts +31 -0
- package/dist/features/magic-context/smart-notes/capabilities.d.ts.map +1 -0
- package/dist/features/magic-context/smart-notes/compiler-prompt.d.ts +2 -0
- package/dist/features/magic-context/smart-notes/compiler-prompt.d.ts.map +1 -0
- package/dist/features/magic-context/smart-notes/compiler.d.ts +52 -0
- package/dist/features/magic-context/smart-notes/compiler.d.ts.map +1 -0
- package/dist/features/magic-context/smart-notes/index.d.ts +10 -0
- package/dist/features/magic-context/smart-notes/index.d.ts.map +1 -0
- package/dist/features/magic-context/smart-notes/runner.d.ts +18 -0
- package/dist/features/magic-context/smart-notes/runner.d.ts.map +1 -0
- package/dist/features/magic-context/smart-notes/sandbox-runner.d.ts +22 -0
- package/dist/features/magic-context/smart-notes/sandbox-runner.d.ts.map +1 -0
- package/dist/features/magic-context/smart-notes/schedule.d.ts +9 -0
- package/dist/features/magic-context/smart-notes/schedule.d.ts.map +1 -0
- package/dist/features/magic-context/smart-notes/ssrf-guard.d.ts +49 -0
- package/dist/features/magic-context/smart-notes/ssrf-guard.d.ts.map +1 -0
- package/dist/features/magic-context/smart-notes/storage.d.ts +27 -0
- package/dist/features/magic-context/smart-notes/storage.d.ts.map +1 -0
- package/dist/features/magic-context/smart-notes/types.d.ts +63 -0
- package/dist/features/magic-context/smart-notes/types.d.ts.map +1 -0
- package/dist/features/magic-context/storage-db.d.ts +5 -1
- package/dist/features/magic-context/storage-db.d.ts.map +1 -1
- package/dist/features/magic-context/storage-meta-persisted.d.ts +8 -4
- package/dist/features/magic-context/storage-meta-persisted.d.ts.map +1 -1
- package/dist/features/magic-context/storage-meta-session.d.ts.map +1 -1
- package/dist/features/magic-context/storage-meta-shared.d.ts +3 -1
- package/dist/features/magic-context/storage-meta-shared.d.ts.map +1 -1
- package/dist/features/magic-context/storage-notes.d.ts +15 -0
- package/dist/features/magic-context/storage-notes.d.ts.map +1 -1
- package/dist/features/magic-context/storage-primers.d.ts +85 -0
- package/dist/features/magic-context/storage-primers.d.ts.map +1 -0
- package/dist/features/magic-context/storage-tags.d.ts +20 -0
- package/dist/features/magic-context/storage-tags.d.ts.map +1 -1
- package/dist/features/magic-context/storage.d.ts +2 -1
- package/dist/features/magic-context/storage.d.ts.map +1 -1
- package/dist/features/magic-context/tagger.d.ts +6 -0
- package/dist/features/magic-context/tagger.d.ts.map +1 -1
- package/dist/features/magic-context/tool-owner-backfill.d.ts.map +1 -1
- package/dist/features/magic-context/transform-decision-log.d.ts +10 -0
- package/dist/features/magic-context/transform-decision-log.d.ts.map +1 -1
- package/dist/features/magic-context/types.d.ts +2 -0
- package/dist/features/magic-context/types.d.ts.map +1 -1
- package/dist/features/magic-context/user-memory/review-user-memories.d.ts +5 -0
- package/dist/features/magic-context/user-memory/review-user-memories.d.ts.map +1 -1
- package/dist/features/magic-context/user-memory/storage-user-memory.d.ts +18 -0
- package/dist/features/magic-context/user-memory/storage-user-memory.d.ts.map +1 -1
- package/dist/features/magic-context/v22-deferred-backfill.d.ts.map +1 -1
- package/dist/hooks/auto-update-checker/semver.d.ts +9 -0
- package/dist/hooks/auto-update-checker/semver.d.ts.map +1 -1
- package/dist/hooks/magic-context/auto-search-hint.d.ts.map +1 -1
- package/dist/hooks/magic-context/command-handler.d.ts +8 -15
- package/dist/hooks/magic-context/command-handler.d.ts.map +1 -1
- package/dist/hooks/magic-context/compaction-marker-manager.d.ts.map +1 -1
- package/dist/hooks/magic-context/compartment-parser.d.ts +9 -0
- package/dist/hooks/magic-context/compartment-parser.d.ts.map +1 -1
- package/dist/hooks/magic-context/compartment-prompt.d.ts +4 -1
- package/dist/hooks/magic-context/compartment-prompt.d.ts.map +1 -1
- package/dist/hooks/magic-context/compartment-runner-historian.d.ts +1 -0
- package/dist/hooks/magic-context/compartment-runner-historian.d.ts.map +1 -1
- package/dist/hooks/magic-context/compartment-runner-incremental.d.ts.map +1 -1
- package/dist/hooks/magic-context/compartment-runner-partial-recomp.d.ts.map +1 -1
- package/dist/hooks/magic-context/compartment-runner-recomp.d.ts.map +1 -1
- package/dist/hooks/magic-context/compartment-runner-types.d.ts +8 -0
- package/dist/hooks/magic-context/compartment-runner-types.d.ts.map +1 -1
- package/dist/hooks/magic-context/compartment-runner-validation.d.ts.map +1 -1
- package/dist/hooks/magic-context/compartment-trigger.d.ts.map +1 -1
- package/dist/hooks/magic-context/ctx-reduce-nudge.d.ts.map +1 -1
- package/dist/hooks/magic-context/event-handler.d.ts.map +1 -1
- package/dist/hooks/magic-context/event-resolvers.d.ts.map +1 -1
- package/dist/hooks/magic-context/historian-prompt.generated.d.ts +1 -1
- package/dist/hooks/magic-context/historian-prompt.generated.d.ts.map +1 -1
- package/dist/hooks/magic-context/historian-state-file.d.ts.map +1 -1
- package/dist/hooks/magic-context/hook-handlers.d.ts +2 -1
- package/dist/hooks/magic-context/hook-handlers.d.ts.map +1 -1
- package/dist/hooks/magic-context/hook.d.ts +1 -0
- package/dist/hooks/magic-context/hook.d.ts.map +1 -1
- package/dist/hooks/magic-context/inject-compartments.d.ts +0 -3
- package/dist/hooks/magic-context/inject-compartments.d.ts.map +1 -1
- package/dist/hooks/magic-context/send-session-notification.d.ts +2 -0
- package/dist/hooks/magic-context/send-session-notification.d.ts.map +1 -1
- package/dist/hooks/magic-context/system-prompt-hash.d.ts +17 -0
- package/dist/hooks/magic-context/system-prompt-hash.d.ts.map +1 -1
- package/dist/hooks/magic-context/transform-postprocess-phase.d.ts +8 -5
- package/dist/hooks/magic-context/transform-postprocess-phase.d.ts.map +1 -1
- package/dist/hooks/magic-context/transform.d.ts +0 -2
- package/dist/hooks/magic-context/transform.d.ts.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +17034 -4059
- package/dist/plugin/dream-timer.d.ts +17 -9
- package/dist/plugin/dream-timer.d.ts.map +1 -1
- package/dist/plugin/embedding-bootstrap-helpers.d.ts +1 -1
- package/dist/plugin/embedding-bootstrap-helpers.d.ts.map +1 -1
- package/dist/plugin/embedding-bootstrap.d.ts.map +1 -1
- package/dist/plugin/hooks/create-session-hooks.d.ts +211 -0
- package/dist/plugin/hooks/create-session-hooks.d.ts.map +1 -1
- package/dist/plugin/instance-disposal.d.ts +2 -0
- package/dist/plugin/instance-disposal.d.ts.map +1 -0
- package/dist/plugin/rpc-handlers.d.ts.map +1 -1
- package/dist/shared/announcement.d.ts +1 -1
- package/dist/shared/announcement.d.ts.map +1 -1
- package/dist/shared/data-path.d.ts +26 -7
- package/dist/shared/data-path.d.ts.map +1 -1
- package/dist/shared/model-suggestion-retry.d.ts +48 -2
- package/dist/shared/model-suggestion-retry.d.ts.map +1 -1
- package/dist/shared/redaction.d.ts +7 -0
- package/dist/shared/redaction.d.ts.map +1 -0
- package/dist/shared/resolve-fallbacks.d.ts +12 -0
- package/dist/shared/resolve-fallbacks.d.ts.map +1 -1
- package/dist/shared/rpc-server.d.ts.map +1 -1
- package/dist/shared/rpc-types.d.ts +2 -0
- package/dist/shared/rpc-types.d.ts.map +1 -1
- package/dist/shared/subagent-runner.d.ts +12 -3
- package/dist/shared/subagent-runner.d.ts.map +1 -1
- package/dist/shared/tui-config.d.ts.map +1 -1
- package/dist/tools/ctx-memory/tools.d.ts.map +1 -1
- package/dist/tools/ctx-memory/types.d.ts.map +1 -1
- package/dist/tools/ctx-memory/verification-recording.d.ts +8 -0
- package/dist/tools/ctx-memory/verification-recording.d.ts.map +1 -0
- package/dist/tools/ctx-search/tools.d.ts.map +1 -1
- package/dist/tools/ctx-search/types.d.ts +1 -1
- package/dist/tools/ctx-search/types.d.ts.map +1 -1
- package/dist/tui/data/context-db.d.ts +2 -0
- package/dist/tui/data/context-db.d.ts.map +1 -1
- package/package.json +3 -1
- package/src/shared/announcement.test.ts +20 -0
- package/src/shared/announcement.ts +19 -7
- package/src/shared/data-path.test.ts +70 -6
- package/src/shared/data-path.ts +50 -8
- package/src/shared/model-suggestion-retry.test.ts +79 -2
- package/src/shared/model-suggestion-retry.ts +181 -3
- package/src/shared/redaction.test.ts +84 -0
- package/src/shared/redaction.ts +264 -0
- package/src/shared/resolve-fallbacks.ts +14 -0
- package/src/shared/rpc-server.ts +24 -0
- package/src/shared/rpc-types.ts +2 -0
- package/src/shared/subagent-runner.ts +12 -3
- package/src/shared/tui-config.test.ts +63 -0
- package/src/shared/tui-config.ts +67 -39
- package/src/tui/data/context-db.ts +12 -0
- package/src/tui/index.tsx +87 -17
- package/src/tui/slots/sidebar-content.tsx +4 -0
- package/dist/features/magic-context/dreamer/queue.d.ts +0 -55
- package/dist/features/magic-context/dreamer/queue.d.ts.map +0 -1
- package/dist/features/magic-context/dreamer/runner.d.ts +0 -92
- package/dist/features/magic-context/dreamer/runner.d.ts.map +0 -1
- package/dist/features/magic-context/dreamer/scheduler.d.ts +0 -29
- package/dist/features/magic-context/dreamer/scheduler.d.ts.map +0 -1
- package/dist/features/magic-context/key-files/aft-availability.d.ts +0 -11
- package/dist/features/magic-context/key-files/aft-availability.d.ts.map +0 -1
- package/dist/features/magic-context/key-files/identify-key-files.d.ts +0 -84
- package/dist/features/magic-context/key-files/identify-key-files.d.ts.map +0 -1
- package/dist/features/magic-context/key-files/project-key-files.d.ts +0 -42
- package/dist/features/magic-context/key-files/project-key-files.d.ts.map +0 -1
- package/dist/features/magic-context/key-files/read-history.d.ts +0 -26
- package/dist/features/magic-context/key-files/read-history.d.ts.map +0 -1
- package/dist/features/magic-context/key-files/read-stats.d.ts +0 -18
- package/dist/features/magic-context/key-files/read-stats.d.ts.map +0 -1
- package/dist/features/magic-context/key-files/storage-key-files.d.ts +0 -20
- package/dist/features/magic-context/key-files/storage-key-files.d.ts.map +0 -1
- package/dist/hooks/magic-context/key-files-block.d.ts +0 -27
- package/dist/hooks/magic-context/key-files-block.d.ts.map +0 -1
|
@@ -6,18 +6,36 @@ import { parseProviderModel } from "./resolve-fallbacks";
|
|
|
6
6
|
|
|
7
7
|
type Client = ReturnType<typeof createOpencodeClient>;
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
/** Max time to wait for the best-effort child-session abort HTTP call before
|
|
10
|
+
* giving up on its response (the abort still proceeds server-side). Keeps a
|
|
11
|
+
* wedged abort endpoint from masking the original timeout/abort error. */
|
|
12
|
+
const ABORT_CALL_TIMEOUT_MS = 3000;
|
|
13
|
+
|
|
14
|
+
export type PromptBody = {
|
|
10
15
|
model?: { providerID: string; modelID: string };
|
|
11
16
|
[key: string]: unknown;
|
|
12
17
|
};
|
|
13
18
|
|
|
14
|
-
type PromptArgs = {
|
|
19
|
+
export type PromptArgs = {
|
|
15
20
|
path: { id: string };
|
|
16
21
|
body: PromptBody;
|
|
17
22
|
signal?: AbortSignal;
|
|
18
23
|
[key: string]: unknown;
|
|
19
24
|
};
|
|
20
25
|
|
|
26
|
+
export interface PromptAttemptInfo {
|
|
27
|
+
/** Human-readable model label used in logs ("primary" or "provider/model"). */
|
|
28
|
+
label: string;
|
|
29
|
+
/** Zero-based attempt index: 0 is primary, 1+ are fallback models. */
|
|
30
|
+
attemptIndex: number;
|
|
31
|
+
/** True for configured fallback models, false for the primary attempt. */
|
|
32
|
+
isFallback: boolean;
|
|
33
|
+
/** Total attempted models including the primary and all configured fallbacks. */
|
|
34
|
+
totalAttempts: number;
|
|
35
|
+
/** Explicit model override for this attempt, when one was supplied. */
|
|
36
|
+
model?: { providerID: string; modelID: string };
|
|
37
|
+
}
|
|
38
|
+
|
|
21
39
|
export interface PromptRetryOptions {
|
|
22
40
|
timeoutMs?: number;
|
|
23
41
|
/** External abort signal — cancels the in-flight LLM prompt immediately when aborted */
|
|
@@ -47,6 +65,29 @@ export interface PromptRetryOptions {
|
|
|
47
65
|
callContext?: string;
|
|
48
66
|
}
|
|
49
67
|
|
|
68
|
+
export interface ValidatedPromptRetryOptions<TOutput, TValidated> extends PromptRetryOptions {
|
|
69
|
+
/**
|
|
70
|
+
* Fetch the output produced by the just-completed prompt attempt. This is
|
|
71
|
+
* intentionally caller-owned because OpenCode exposes results via session
|
|
72
|
+
* messages and each caller validates a different shape.
|
|
73
|
+
*/
|
|
74
|
+
fetchOutput: (args: PromptArgs, attempt: PromptAttemptInfo) => Promise<TOutput>;
|
|
75
|
+
/**
|
|
76
|
+
* Validate and optionally transform the fetched output. Throw to reject this
|
|
77
|
+
* model's output and advance to the next configured fallback model.
|
|
78
|
+
*/
|
|
79
|
+
validateOutput: (
|
|
80
|
+
output: TOutput,
|
|
81
|
+
attempt: PromptAttemptInfo,
|
|
82
|
+
) => TValidated | Promise<TValidated>;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export interface ValidatedPromptRetryResult<TOutput, TValidated> {
|
|
86
|
+
output: TOutput;
|
|
87
|
+
validated: TValidated;
|
|
88
|
+
attempt: PromptAttemptInfo;
|
|
89
|
+
}
|
|
90
|
+
|
|
50
91
|
export interface ModelSuggestionInfo {
|
|
51
92
|
providerID: string;
|
|
52
93
|
modelID: string;
|
|
@@ -171,7 +212,15 @@ async function promptWithTimeout(
|
|
|
171
212
|
*/
|
|
172
213
|
async function abortChildRun(client: Client, sessionId: string): Promise<void> {
|
|
173
214
|
try {
|
|
174
|
-
|
|
215
|
+
// Bound the abort call: it's best-effort cleanup, and if the abort
|
|
216
|
+
// endpoint itself stalls (the runner is wedged) an unbounded await here
|
|
217
|
+
// would hang the caller and MASK the original timeout/abort error that we
|
|
218
|
+
// still need to surface. Race against a short timer; the abort keeps
|
|
219
|
+
// running server-side regardless of whether we wait for its response.
|
|
220
|
+
await Promise.race([
|
|
221
|
+
client.session.abort({ path: { id: sessionId } }),
|
|
222
|
+
new Promise<void>((resolve) => setTimeout(resolve, ABORT_CALL_TIMEOUT_MS)),
|
|
223
|
+
]);
|
|
175
224
|
} catch (error) {
|
|
176
225
|
log(`[model-retry] child session abort failed for ${sessionId}: ${String(error)}`);
|
|
177
226
|
}
|
|
@@ -368,3 +417,132 @@ export async function promptSyncWithModelSuggestionRetry(
|
|
|
368
417
|
);
|
|
369
418
|
throw lastError ?? new Error("All fallback models failed");
|
|
370
419
|
}
|
|
420
|
+
|
|
421
|
+
async function attemptAndValidate<TOutput, TValidated>(
|
|
422
|
+
client: Client,
|
|
423
|
+
args: PromptArgs,
|
|
424
|
+
timeoutMs: number,
|
|
425
|
+
signal: AbortSignal | undefined,
|
|
426
|
+
callContext: string,
|
|
427
|
+
attempt: PromptAttemptInfo,
|
|
428
|
+
options: ValidatedPromptRetryOptions<TOutput, TValidated>,
|
|
429
|
+
): Promise<ValidatedPromptRetryResult<TOutput, TValidated>> {
|
|
430
|
+
await attemptOnce(client, args, timeoutMs, signal, callContext, attempt.label);
|
|
431
|
+
const output = await options.fetchOutput(args, attempt);
|
|
432
|
+
const validated = await options.validateOutput(output, attempt);
|
|
433
|
+
return { output, validated, attempt };
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* Run a prompt with model fallback support, but accept an attempt only after the
|
|
438
|
+
* caller validates the model's actual output. This covers "empty success" cases
|
|
439
|
+
* where the provider/OpenCode prompt call completes successfully but the subagent
|
|
440
|
+
* produced no usable assistant text / JSON.
|
|
441
|
+
*
|
|
442
|
+
* The happy path is still one prompt + one caller-owned output fetch: callers
|
|
443
|
+
* should use the returned output instead of fetching messages a second time.
|
|
444
|
+
* Validation failures are retryable across configured fallback models. If every
|
|
445
|
+
* attempt produces invalid output (or otherwise fails retryably), the first
|
|
446
|
+
* failure is re-thrown so callers surface the original failure semantics.
|
|
447
|
+
*/
|
|
448
|
+
export async function promptSyncWithValidatedOutputRetry<TOutput, TValidated = TOutput>(
|
|
449
|
+
client: Client,
|
|
450
|
+
args: PromptArgs,
|
|
451
|
+
options: ValidatedPromptRetryOptions<TOutput, TValidated>,
|
|
452
|
+
): Promise<ValidatedPromptRetryResult<TOutput, TValidated>> {
|
|
453
|
+
const timeoutMs = options.timeoutMs ?? 300_000;
|
|
454
|
+
const callContext = options.callContext ?? "subagent";
|
|
455
|
+
const fallbacks = options.fallbackModels ?? [];
|
|
456
|
+
|
|
457
|
+
const explicitPrimaryLabel =
|
|
458
|
+
args.body.model?.providerID && args.body.model.modelID
|
|
459
|
+
? `${args.body.model.providerID}/${args.body.model.modelID}`
|
|
460
|
+
: "primary";
|
|
461
|
+
const totalAttempts = fallbacks.length + 1;
|
|
462
|
+
|
|
463
|
+
let firstError: unknown = null;
|
|
464
|
+
let lastError: unknown = null;
|
|
465
|
+
|
|
466
|
+
try {
|
|
467
|
+
return await attemptAndValidate(
|
|
468
|
+
client,
|
|
469
|
+
args,
|
|
470
|
+
timeoutMs,
|
|
471
|
+
options.signal,
|
|
472
|
+
callContext,
|
|
473
|
+
{
|
|
474
|
+
label: explicitPrimaryLabel,
|
|
475
|
+
attemptIndex: 0,
|
|
476
|
+
isFallback: false,
|
|
477
|
+
totalAttempts,
|
|
478
|
+
model: args.body.model,
|
|
479
|
+
},
|
|
480
|
+
options,
|
|
481
|
+
);
|
|
482
|
+
} catch (error) {
|
|
483
|
+
firstError = error;
|
|
484
|
+
lastError = error;
|
|
485
|
+
if (isNonRetryable(error, options.signal)) throw error;
|
|
486
|
+
|
|
487
|
+
if (fallbacks.length === 0) {
|
|
488
|
+
throw error;
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
log(
|
|
492
|
+
`[${callContext}] primary (${explicitPrimaryLabel}) failed validation/prompt: ${shortErr(error)}; trying ${fallbacks.length} fallback(s)`,
|
|
493
|
+
);
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
for (let i = 0; i < fallbacks.length; i += 1) {
|
|
497
|
+
const parsed = parseProviderModel(fallbacks[i]);
|
|
498
|
+
if (!parsed) {
|
|
499
|
+
log(`[${callContext}] skipping invalid fallback spec: ${fallbacks[i]}`);
|
|
500
|
+
continue;
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
const label = `${parsed.providerID}/${parsed.modelID}`;
|
|
504
|
+
const attemptArgs: PromptArgs = {
|
|
505
|
+
...args,
|
|
506
|
+
body: { ...args.body, model: parsed },
|
|
507
|
+
};
|
|
508
|
+
const attempt: PromptAttemptInfo = {
|
|
509
|
+
label,
|
|
510
|
+
attemptIndex: i + 1,
|
|
511
|
+
isFallback: true,
|
|
512
|
+
totalAttempts,
|
|
513
|
+
model: parsed,
|
|
514
|
+
};
|
|
515
|
+
|
|
516
|
+
try {
|
|
517
|
+
const result = await attemptAndValidate(
|
|
518
|
+
client,
|
|
519
|
+
attemptArgs,
|
|
520
|
+
timeoutMs,
|
|
521
|
+
options.signal,
|
|
522
|
+
callContext,
|
|
523
|
+
attempt,
|
|
524
|
+
options,
|
|
525
|
+
);
|
|
526
|
+
log(
|
|
527
|
+
`[${callContext}] fallback succeeded with ${label} (attempt ${i + 2}/${fallbacks.length + 1})`,
|
|
528
|
+
);
|
|
529
|
+
return result;
|
|
530
|
+
} catch (error) {
|
|
531
|
+
if (firstError === null) firstError = error;
|
|
532
|
+
lastError = error;
|
|
533
|
+
if (isNonRetryable(error, options.signal)) throw error;
|
|
534
|
+
|
|
535
|
+
const remaining = fallbacks.length - i - 1;
|
|
536
|
+
if (remaining > 0) {
|
|
537
|
+
log(
|
|
538
|
+
`[${callContext}] ${label} failed validation/prompt: ${shortErr(error)}; ${remaining} fallback(s) left`,
|
|
539
|
+
);
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
log(
|
|
545
|
+
`[${callContext}] all models exhausted; tried: ${[explicitPrimaryLabel, ...fallbacks].join(", ")}; original error: ${shortErr(firstError)}; last error: ${shortErr(lastError)}`,
|
|
546
|
+
);
|
|
547
|
+
throw firstError ?? lastError ?? new Error("All fallback models failed validation");
|
|
548
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/// <reference types="bun-types" />
|
|
2
|
+
|
|
3
|
+
import { describe, expect, test } from "bun:test";
|
|
4
|
+
|
|
5
|
+
import { hasShareabilitySensitiveText, redactSecretText } from "./redaction";
|
|
6
|
+
|
|
7
|
+
describe("redactSecretText — token counts and scalar diagnostics stay visible", () => {
|
|
8
|
+
test("keeps numeric/boolean values whose key merely contains a secret word", () => {
|
|
9
|
+
// These log shapes are counts/flags, not secrets, so they must stay readable.
|
|
10
|
+
expect(redactSecretText("tokens.input=45000 cache.read=0 cache.write=0")).toBe(
|
|
11
|
+
"tokens.input=45000 cache.read=0 cache.write=0",
|
|
12
|
+
);
|
|
13
|
+
expect(redactSecretText("hasUsageTokens=true")).toBe("hasUsageTokens=true");
|
|
14
|
+
expect(redactSecretText("totalInputTokens=132000")).toBe("totalInputTokens=132000");
|
|
15
|
+
expect(redactSecretText("max_tokens=4096")).toBe("max_tokens=4096");
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
test("keeps quoted numeric values matched only on the key word", () => {
|
|
19
|
+
expect(redactSecretText('"max_tokens": "4096"')).toBe('"max_tokens": "4096"');
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test("still redacts real secret string values", () => {
|
|
23
|
+
// High-entropy / non-scalar values must always be redacted; only bare
|
|
24
|
+
// numeric/boolean scalars are exempt from the key-based match.
|
|
25
|
+
expect(redactSecretText("api_key=sk-abc123XYZsecretvalue")).toContain("<REDACTED:");
|
|
26
|
+
expect(redactSecretText("api_key=sk-abc123XYZsecretvalue")).not.toContain(
|
|
27
|
+
"sk-abc123XYZsecretvalue",
|
|
28
|
+
);
|
|
29
|
+
expect(redactSecretText('"auth_token": "tok_live_9f8e7d6c5b"')).toContain("<REDACTED:");
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test("value-shaped secret patterns still fire independent of key name", () => {
|
|
33
|
+
// A bearer/JWT value is caught by its own pattern even if its key is bland.
|
|
34
|
+
expect(redactSecretText("Authorization: Bearer abc123def456ghi789")).toContain(
|
|
35
|
+
"<REDACTED:bearer>",
|
|
36
|
+
);
|
|
37
|
+
expect(redactSecretText("blob=eyJhbGciOi.eyJzdWIiOiIx.SflKxwRJSMeKKF2QT4")).toContain(
|
|
38
|
+
"<JWT_REDACTED>",
|
|
39
|
+
);
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
describe("hasShareabilitySensitiveText", () => {
|
|
44
|
+
test("safe project facts are shareable", () => {
|
|
45
|
+
expect(
|
|
46
|
+
hasShareabilitySensitiveText(
|
|
47
|
+
"The historian runs as a hidden subagent and never busts the prompt cache.",
|
|
48
|
+
),
|
|
49
|
+
).toBe(false);
|
|
50
|
+
expect(
|
|
51
|
+
hasShareabilitySensitiveText("Migration v45 adds the retrospective watermark column."),
|
|
52
|
+
).toBe(false);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test("flags inline key:value / key=value secrets the keyed redactor misses in prose", () => {
|
|
56
|
+
expect(hasShareabilitySensitiveText("Set api_key: sk-live-abc123 in the env.")).toBe(true);
|
|
57
|
+
expect(hasShareabilitySensitiveText("password=hunter2 for the staging box")).toBe(true);
|
|
58
|
+
expect(hasShareabilitySensitiveText("client_secret = abcdef in the OAuth app")).toBe(true);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test("flags Windows forward-slash home (sanitizePathString only rewrites backslash form)", () => {
|
|
62
|
+
expect(hasShareabilitySensitiveText("logs are under C:/Users/ufuk/AppData/mc")).toBe(true);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test("flags ~/ rooted personal paths", () => {
|
|
66
|
+
expect(hasShareabilitySensitiveText("config lives at ~/.config/opencode/x.jsonc")).toBe(
|
|
67
|
+
true,
|
|
68
|
+
);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test("flags local / private endpoints", () => {
|
|
72
|
+
expect(hasShareabilitySensitiveText("embed endpoint is http://localhost:1234/v1")).toBe(
|
|
73
|
+
true,
|
|
74
|
+
);
|
|
75
|
+
expect(hasShareabilitySensitiveText("the box answers on 127.0.0.1:8080")).toBe(true);
|
|
76
|
+
expect(hasShareabilitySensitiveText("LAN host 192.168.1.42 runs the model")).toBe(true);
|
|
77
|
+
expect(hasShareabilitySensitiveText("internal 10.0.0.5 endpoint")).toBe(true);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test("a public IP / port alone is not flagged by the private-range rules", () => {
|
|
81
|
+
// 8.8.8.8 is public; no private-range or localhost pattern should match.
|
|
82
|
+
expect(hasShareabilitySensitiveText("DNS resolver at 8.8.8.8")).toBe(false);
|
|
83
|
+
});
|
|
84
|
+
});
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
import { homedir, userInfo } from "node:os";
|
|
2
|
+
|
|
3
|
+
function escapeRegex(value: string): string {
|
|
4
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
// Whole-segment match: the key (or its components when split on common
|
|
8
|
+
// separators) must BE one of these words, not merely contain them as a
|
|
9
|
+
// substring. Bare substring matching wrongly redacts benign fields like
|
|
10
|
+
// `pin_key_files`, `token_budget`, and `injection_budget_tokens`.
|
|
11
|
+
const SECRET_WORDS = [
|
|
12
|
+
"key",
|
|
13
|
+
"token",
|
|
14
|
+
"secret",
|
|
15
|
+
"password",
|
|
16
|
+
"auth",
|
|
17
|
+
"authorization",
|
|
18
|
+
"bearer",
|
|
19
|
+
"credential",
|
|
20
|
+
];
|
|
21
|
+
const SECRET_SEGMENT_PATTERN = new RegExp(
|
|
22
|
+
`^(?:${SECRET_WORDS.map((w) => `${w}s?`).join("|")})$`,
|
|
23
|
+
"i",
|
|
24
|
+
);
|
|
25
|
+
const TRAILING_DESCRIPTORS = new Set(["id", "ids", "value", "values", "header", "headers"]);
|
|
26
|
+
|
|
27
|
+
function redactionTypeForKey(key: string): string {
|
|
28
|
+
const normalized = key
|
|
29
|
+
.trim()
|
|
30
|
+
.toLowerCase()
|
|
31
|
+
.replace(/[^a-z0-9_.-]+/g, "_");
|
|
32
|
+
const suffix = normalized.split(".").filter(Boolean).at(-1) ?? normalized;
|
|
33
|
+
return suffix || "secret";
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// A bare number / boolean / null is never a secret — an API key, bearer token,
|
|
37
|
+
// password, or credential is always a high-entropy string. So when a key-based
|
|
38
|
+
// pattern (the `name=value` / `"name":"value"` forms below) matches purely on
|
|
39
|
+
// the KEY containing a word like "token", but the VALUE is numeric/boolean, it's
|
|
40
|
+
// a count or flag, not a secret. These must stay readable in logs:
|
|
41
|
+
// `tokens.input=45000`, `hasUsageTokens=true`, `max_tokens=4096` are diagnostics,
|
|
42
|
+
// not credentials. (High-entropy secret VALUES are still caught by the
|
|
43
|
+
// value-shaped patterns above — bearer, JWT, AKIA, gh*_, etc. — independent of
|
|
44
|
+
// the key name, so relaxing the key-based match for scalars loses no coverage.)
|
|
45
|
+
function isNonSecretScalarValue(value: string): boolean {
|
|
46
|
+
const v = value.trim();
|
|
47
|
+
if (v === "true" || v === "false" || v === "null" || v === "undefined") return true;
|
|
48
|
+
// Integer or decimal, optional sign/exponent — token counts, ports, sizes.
|
|
49
|
+
return /^[+-]?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?$/.test(v);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const SECRET_QUALIFIERS = new Set([
|
|
53
|
+
"api",
|
|
54
|
+
"access",
|
|
55
|
+
"private",
|
|
56
|
+
"client",
|
|
57
|
+
"auth",
|
|
58
|
+
"authorization",
|
|
59
|
+
"secret",
|
|
60
|
+
"bearer",
|
|
61
|
+
"session",
|
|
62
|
+
"refresh",
|
|
63
|
+
"service",
|
|
64
|
+
"x",
|
|
65
|
+
"openai",
|
|
66
|
+
"anthropic",
|
|
67
|
+
"google",
|
|
68
|
+
"github",
|
|
69
|
+
"huggingface",
|
|
70
|
+
"aws",
|
|
71
|
+
"azure",
|
|
72
|
+
]);
|
|
73
|
+
|
|
74
|
+
export function isSecretKey(key: string): boolean {
|
|
75
|
+
const segments = key
|
|
76
|
+
.replace(/([a-z0-9])([A-Z])/g, "$1_$2")
|
|
77
|
+
.toLowerCase()
|
|
78
|
+
.split(/[._-]+/)
|
|
79
|
+
.filter(Boolean);
|
|
80
|
+
if (segments.length === 0) return false;
|
|
81
|
+
|
|
82
|
+
if (segments.length === 1) {
|
|
83
|
+
const first = segments[0];
|
|
84
|
+
return Boolean(first && SECRET_SEGMENT_PATTERN.test(first));
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
for (let i = 0; i < segments.length; i++) {
|
|
88
|
+
const seg = segments[i];
|
|
89
|
+
if (!seg || !SECRET_SEGMENT_PATTERN.test(seg)) continue;
|
|
90
|
+
|
|
91
|
+
let trailingOk = true;
|
|
92
|
+
for (let j = i + 1; j < segments.length; j++) {
|
|
93
|
+
const tail = segments[j];
|
|
94
|
+
if (!tail) continue;
|
|
95
|
+
if (TRAILING_DESCRIPTORS.has(tail)) continue;
|
|
96
|
+
if (SECRET_SEGMENT_PATTERN.test(tail)) continue;
|
|
97
|
+
trailingOk = false;
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
if (!trailingOk) continue;
|
|
101
|
+
|
|
102
|
+
for (let k = i - 1; k >= 0; k--) {
|
|
103
|
+
const lead = segments[k];
|
|
104
|
+
if (lead && SECRET_QUALIFIERS.has(lead)) return true;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function sanitizePathString(value: string): string {
|
|
111
|
+
const home = homedir();
|
|
112
|
+
const username = userInfo().username;
|
|
113
|
+
let sanitized = value;
|
|
114
|
+
if (home) {
|
|
115
|
+
sanitized = sanitized.replace(new RegExp(escapeRegex(home), "g"), "~");
|
|
116
|
+
}
|
|
117
|
+
sanitized = sanitized.replace(/\/Users\/[^/]+\//g, "/Users/<USER>/");
|
|
118
|
+
sanitized = sanitized.replace(/\/home\/[^/]+\//g, "/home/<USER>/");
|
|
119
|
+
sanitized = sanitized.replace(/C:\\Users\\[^\\]+\\/g, "C:\\Users\\<USER>\\");
|
|
120
|
+
if (username) {
|
|
121
|
+
sanitized = sanitized.replace(new RegExp(escapeRegex(username), "g"), "<USER>");
|
|
122
|
+
}
|
|
123
|
+
return sanitized;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const SECRET_TEXT_PATTERNS: Array<{
|
|
127
|
+
pattern: RegExp;
|
|
128
|
+
replacement: string | ((match: string, ...groups: string[]) => string);
|
|
129
|
+
}> = [
|
|
130
|
+
{
|
|
131
|
+
pattern: /\bsk-ant-(?:api03-)?[A-Za-z0-9_-]{32,}/g,
|
|
132
|
+
replacement: "<ANTHROPIC_API_KEY_REDACTED>",
|
|
133
|
+
},
|
|
134
|
+
{
|
|
135
|
+
pattern: /\bsk-(?:proj-)?[A-Za-z0-9_-]{32,}/g,
|
|
136
|
+
replacement: "<OPENAI_API_KEY_REDACTED>",
|
|
137
|
+
},
|
|
138
|
+
{
|
|
139
|
+
pattern: /\bgithub_pat_[A-Za-z0-9_]{20,}/g,
|
|
140
|
+
replacement: "<GITHUB_PAT_REDACTED>",
|
|
141
|
+
},
|
|
142
|
+
{
|
|
143
|
+
pattern: /\b(?:gh[opsu]|ghr)_[A-Za-z0-9]{30,}/g,
|
|
144
|
+
replacement: "<GITHUB_TOKEN_REDACTED>",
|
|
145
|
+
},
|
|
146
|
+
{
|
|
147
|
+
pattern: /\bhf_[A-Za-z0-9]{30,}/g,
|
|
148
|
+
replacement: "<HUGGINGFACE_TOKEN_REDACTED>",
|
|
149
|
+
},
|
|
150
|
+
{
|
|
151
|
+
pattern: /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/g,
|
|
152
|
+
replacement: "<AWS_ACCESS_KEY_ID_REDACTED>",
|
|
153
|
+
},
|
|
154
|
+
{
|
|
155
|
+
pattern: /\bxox[abprsuvc]-[A-Za-z0-9-]{10,}/g,
|
|
156
|
+
replacement: "<SLACK_TOKEN_REDACTED>",
|
|
157
|
+
},
|
|
158
|
+
{
|
|
159
|
+
pattern: /\bAIza[A-Za-z0-9_-]{35}\b/g,
|
|
160
|
+
replacement: "<GOOGLE_API_KEY_REDACTED>",
|
|
161
|
+
},
|
|
162
|
+
{
|
|
163
|
+
pattern: /\b(Authorization\s*:\s*Bearer\s+)([A-Za-z0-9._~+/=-]{8,})/gi,
|
|
164
|
+
replacement: (_full: string, prefix: string) => `${prefix}<REDACTED:bearer>`,
|
|
165
|
+
},
|
|
166
|
+
{
|
|
167
|
+
pattern: /\beyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+/g,
|
|
168
|
+
replacement: "<JWT_REDACTED>",
|
|
169
|
+
},
|
|
170
|
+
{
|
|
171
|
+
pattern:
|
|
172
|
+
/(["'])([^"']*(?:key|token|secret|password|auth|bearer|credential)[^"']*)\1(\s*:\s*)(["'])([^"']*)\4/gi,
|
|
173
|
+
replacement: (
|
|
174
|
+
full: string,
|
|
175
|
+
quote: string,
|
|
176
|
+
key: string,
|
|
177
|
+
separator: string,
|
|
178
|
+
valueQuote: string,
|
|
179
|
+
value: string,
|
|
180
|
+
) =>
|
|
181
|
+
// A numeric/boolean value matched only because the KEY contains a
|
|
182
|
+
// secret word (e.g. "max_tokens": "4096") is a count, not a secret.
|
|
183
|
+
isNonSecretScalarValue(value)
|
|
184
|
+
? full
|
|
185
|
+
: `${quote}${key}${quote}${separator}${valueQuote}<REDACTED:${redactionTypeForKey(key)}>${valueQuote}`,
|
|
186
|
+
},
|
|
187
|
+
{
|
|
188
|
+
pattern:
|
|
189
|
+
/\b([A-Za-z0-9_.-]*(?:key|token|secret|password|auth|bearer|credential)[A-Za-z0-9_.-]*)\s*=\s*([^\s'"`]+)/gi,
|
|
190
|
+
replacement: (full: string, key: string, value: string) =>
|
|
191
|
+
// tokens.input=45000 / hasUsageTokens=true are diagnostics, not
|
|
192
|
+
// secrets — keep them readable. Real secret values are still caught
|
|
193
|
+
// by the value-shaped patterns above.
|
|
194
|
+
isNonSecretScalarValue(value) ? full : `${key}=<REDACTED:${redactionTypeForKey(key)}>`,
|
|
195
|
+
},
|
|
196
|
+
];
|
|
197
|
+
|
|
198
|
+
export function redactSecretText(value: string): string {
|
|
199
|
+
let redacted = value;
|
|
200
|
+
for (const { pattern, replacement } of SECRET_TEXT_PATTERNS) {
|
|
201
|
+
if (typeof replacement === "string") {
|
|
202
|
+
redacted = redacted.replace(pattern, replacement);
|
|
203
|
+
} else {
|
|
204
|
+
redacted = redacted.replace(
|
|
205
|
+
pattern,
|
|
206
|
+
replacement as (match: string, ...groups: string[]) => string,
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
return redacted;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export function sanitizeDiagnosticText(value: string): string {
|
|
214
|
+
return redactSecretText(sanitizePathString(value));
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// Extra shareability-only signals — patterns that mark text as unsafe to share
|
|
218
|
+
// with teammates but that the diagnostic sanitizer (tuned for secret/path
|
|
219
|
+
// REDACTION, not share-gating) does not rewrite. Kept here, NOT in
|
|
220
|
+
// sanitizeDiagnosticText, so diagnostic redaction output is unchanged.
|
|
221
|
+
const SHAREABILITY_SENSITIVE_PATTERNS: RegExp[] = [
|
|
222
|
+
// Windows user home, forward- OR back-slash (sanitizePathString only rewrites
|
|
223
|
+
// the backslash form).
|
|
224
|
+
/\bC:\/Users\/[^/\s]+/i,
|
|
225
|
+
// A `~`-rooted home path (personal/local).
|
|
226
|
+
/(?:^|\s)~\/[^\s]+/,
|
|
227
|
+
// Inline `key: value` / `key=value` secrets the keyed redactor misses in free
|
|
228
|
+
// text (it keys on config OBJECT keys, not prose).
|
|
229
|
+
/\b(?:api[_-]?key|secret|token|password|passwd|pwd|client[_-]?secret|access[_-]?key)\b\s*[:=]\s*\S+/i,
|
|
230
|
+
// Local / private endpoints — environment-specific, not a shared truth.
|
|
231
|
+
/\b(?:localhost|127\.0\.0\.1|0\.0\.0\.0|\[::1\])(?::\d+)?\b/i,
|
|
232
|
+
/\b(?:10|127)\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/,
|
|
233
|
+
/\b192\.168\.\d{1,3}\.\d{1,3}\b/,
|
|
234
|
+
/\b172\.(?:1[6-9]|2\d|3[01])\.\d{1,3}\.\d{1,3}\b/,
|
|
235
|
+
];
|
|
236
|
+
|
|
237
|
+
export function hasShareabilitySensitiveText(text: string): boolean {
|
|
238
|
+
try {
|
|
239
|
+
if (sanitizeDiagnosticText(text) !== text) return true;
|
|
240
|
+
return SHAREABILITY_SENSITIVE_PATTERNS.some((pattern) => pattern.test(text));
|
|
241
|
+
} catch {
|
|
242
|
+
return true;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
export function sanitizeConfigValue(value: unknown, keyPath: string[] = []): unknown {
|
|
247
|
+
const key = keyPath.at(-1) ?? "";
|
|
248
|
+
if (key && isSecretKey(key)) {
|
|
249
|
+
return `<REDACTED:${redactionTypeForKey(key)}>`;
|
|
250
|
+
}
|
|
251
|
+
if (typeof value === "string") return sanitizeDiagnosticText(value);
|
|
252
|
+
if (Array.isArray(value)) {
|
|
253
|
+
return value.map((entry, index) => sanitizeConfigValue(entry, [...keyPath, String(index)]));
|
|
254
|
+
}
|
|
255
|
+
if (value && typeof value === "object") {
|
|
256
|
+
return Object.fromEntries(
|
|
257
|
+
Object.entries(value).map(([entryKey, entry]) => [
|
|
258
|
+
entryKey,
|
|
259
|
+
sanitizeConfigValue(entry, [...keyPath, entryKey]),
|
|
260
|
+
]),
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
return value;
|
|
264
|
+
}
|
|
@@ -64,3 +64,17 @@ export function parseProviderModel(spec: string): { providerID: string; modelID:
|
|
|
64
64
|
modelID: spec.slice(slash + 1).trim(),
|
|
65
65
|
};
|
|
66
66
|
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Build the `{ model: { providerID, modelID } }` fragment for an OpenCode prompt
|
|
70
|
+
* body from a `provider/model` spec string, or `{}` when the spec is absent or
|
|
71
|
+
* unparseable (the session falls back to its default model). Spread into a
|
|
72
|
+
* `client.session.prompt` body.
|
|
73
|
+
*/
|
|
74
|
+
export function modelBodyField(spec: string | undefined): {
|
|
75
|
+
model?: { providerID: string; modelID: string };
|
|
76
|
+
} {
|
|
77
|
+
if (!spec) return {};
|
|
78
|
+
const parsed = parseProviderModel(spec);
|
|
79
|
+
return parsed ? { model: parsed } : {};
|
|
80
|
+
}
|
package/src/shared/rpc-server.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { randomBytes, timingSafeEqual } from "node:crypto";
|
|
2
2
|
import {
|
|
3
|
+
chmodSync,
|
|
3
4
|
mkdirSync,
|
|
4
5
|
readdirSync,
|
|
5
6
|
readFileSync,
|
|
6
7
|
renameSync,
|
|
8
|
+
rmSync,
|
|
7
9
|
unlinkSync,
|
|
8
10
|
writeFileSync,
|
|
9
11
|
} from "node:fs";
|
|
@@ -85,7 +87,22 @@ export class MagicContextRpcServer {
|
|
|
85
87
|
// file 0o600. renameSync preserves the tmp file's mode, so
|
|
86
88
|
// the 0o600 on the write covers the final file.
|
|
87
89
|
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
90
|
+
// mkdirSync's mode only applies on CREATION — a dir left by an
|
|
91
|
+
// older build (or default 0o755 umask) keeps its loose perms, so
|
|
92
|
+
// chmod it defensively so the bearer token isn't world-readable.
|
|
93
|
+
try {
|
|
94
|
+
chmodSync(dir, 0o700);
|
|
95
|
+
} catch {
|
|
96
|
+
// best-effort
|
|
97
|
+
}
|
|
88
98
|
const tmpPath = `${this.portFilePath}.tmp`;
|
|
99
|
+
// A stale tmp from a crashed write could exist with loose perms;
|
|
100
|
+
// writeFileSync's mode only applies on create, so remove it first.
|
|
101
|
+
try {
|
|
102
|
+
rmSync(tmpPath, { force: true });
|
|
103
|
+
} catch {
|
|
104
|
+
// best-effort
|
|
105
|
+
}
|
|
89
106
|
writeFileSync(
|
|
90
107
|
tmpPath,
|
|
91
108
|
JSON.stringify({
|
|
@@ -97,6 +114,13 @@ export class MagicContextRpcServer {
|
|
|
97
114
|
{ encoding: "utf-8", mode: 0o600 },
|
|
98
115
|
);
|
|
99
116
|
renameSync(tmpPath, this.portFilePath);
|
|
117
|
+
// renameSync preserves the tmp's mode, but chmod the final path
|
|
118
|
+
// defensively in case the token file pre-existed with loose perms.
|
|
119
|
+
try {
|
|
120
|
+
chmodSync(this.portFilePath, 0o600);
|
|
121
|
+
} catch {
|
|
122
|
+
// best-effort
|
|
123
|
+
}
|
|
100
124
|
log(`[rpc] server listening on 127.0.0.1:${this.port}`);
|
|
101
125
|
} catch (err) {
|
|
102
126
|
log(`[rpc] failed to write port file: ${err}`);
|
package/src/shared/rpc-types.ts
CHANGED
|
@@ -121,6 +121,8 @@ export interface StatusDetail extends SidebarSnapshot {
|
|
|
121
121
|
historyBlockTokens: number;
|
|
122
122
|
compressionBudget: number | null;
|
|
123
123
|
compressionUsage: string | null;
|
|
124
|
+
/** Effective configured toast duration in ms after config resolution. */
|
|
125
|
+
toastDurationMs: number;
|
|
124
126
|
}
|
|
125
127
|
|
|
126
128
|
/** Embedding coverage for `/ctx-embed` status (mirrors getEmbeddingCoverageStatus). */
|
|
@@ -155,9 +155,9 @@ export type SubagentProgressEvent =
|
|
|
155
155
|
* Fields:
|
|
156
156
|
* - `ok`: true iff the child produced a final assistant message.
|
|
157
157
|
* - `assistantText`: concatenated text content from the final assistant
|
|
158
|
-
* message, with leading/trailing whitespace trimmed. Empty
|
|
159
|
-
*
|
|
160
|
-
*
|
|
158
|
+
* message, with leading/trailing whitespace trimmed. Empty assistant text is
|
|
159
|
+
* reported as `ok: false, reason: "no_assistant"` so callers can try fallback
|
|
160
|
+
* models instead of accepting an unusable success.
|
|
161
161
|
* - `reason`: failure category, one of:
|
|
162
162
|
* - `"timeout"`: hit `timeoutMs` before the child finished
|
|
163
163
|
* - `"abort"`: caller's `signal` was triggered
|
|
@@ -180,6 +180,15 @@ export type SubagentRunResult =
|
|
|
180
180
|
ok: true;
|
|
181
181
|
assistantText: string;
|
|
182
182
|
durationMs: number;
|
|
183
|
+
/**
|
|
184
|
+
* Number of tool invocations the agent made during the run. Pi reports
|
|
185
|
+
* this so callers that gate on "did the agent actually investigate vs
|
|
186
|
+
* just paraphrase" (refresh-primers' grounding gate) work on Pi, whose
|
|
187
|
+
* facade otherwise surfaces only the final assistant text. OpenCode
|
|
188
|
+
* leaves it undefined — its callers read tool-call parts straight off
|
|
189
|
+
* the real session messages.
|
|
190
|
+
*/
|
|
191
|
+
toolCallCount?: number;
|
|
183
192
|
meta?: Record<string, unknown>;
|
|
184
193
|
}
|
|
185
194
|
| {
|