@cortexkit/opencode-magic-context 0.40.1 → 0.41.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 +1 -1
- package/dist/{announcement-5eg4t64s.js → announcement-3g9xg433.js} +2 -2
- package/dist/config/schema/magic-context.d.ts +15 -0
- package/dist/config/schema/magic-context.d.ts.map +1 -1
- package/dist/{conflict-warning-hook-f406ggbw.js → conflict-warning-hook-rf17b6et.js} +4 -4
- package/dist/{derive-budgets-xk8cy5e6.js → derive-budgets-bb7mrpzs.js} +3 -3
- package/dist/features/builtin-commands/commands.d.ts +35 -2
- package/dist/features/builtin-commands/commands.d.ts.map +1 -1
- package/dist/features/magic-context/compartment-chunk-embedding.d.ts +12 -16
- package/dist/features/magic-context/compartment-chunk-embedding.d.ts.map +1 -1
- package/dist/features/magic-context/context-authority.d.ts +10 -0
- package/dist/features/magic-context/context-authority.d.ts.map +1 -1
- package/dist/features/magic-context/dreamer/classify.d.ts +1 -0
- package/dist/features/magic-context/dreamer/classify.d.ts.map +1 -1
- package/dist/features/magic-context/dreamer/lease.d.ts +12 -27
- package/dist/features/magic-context/dreamer/lease.d.ts.map +1 -1
- package/dist/features/magic-context/dreamer/map-memories-prompt.d.ts +1 -1
- package/dist/features/magic-context/dreamer/map-memories-prompt.d.ts.map +1 -1
- package/dist/features/magic-context/dreamer/map-memories.d.ts +2 -0
- package/dist/features/magic-context/dreamer/map-memories.d.ts.map +1 -1
- package/dist/features/magic-context/dreamer/memory-claim-safety.d.ts +9 -0
- package/dist/features/magic-context/dreamer/memory-claim-safety.d.ts.map +1 -0
- package/dist/features/magic-context/dreamer/task-executor.d.ts.map +1 -1
- package/dist/features/magic-context/dreamer/task-registry.d.ts +2 -0
- package/dist/features/magic-context/dreamer/task-registry.d.ts.map +1 -1
- package/dist/features/magic-context/dreamer/verify-prompt.d.ts +7 -2
- package/dist/features/magic-context/dreamer/verify-prompt.d.ts.map +1 -1
- package/dist/features/magic-context/dreamer/verify.d.ts +13 -10
- package/dist/features/magic-context/dreamer/verify.d.ts.map +1 -1
- package/dist/features/magic-context/memory/embedding-failure.d.ts +1 -1
- package/dist/features/magic-context/memory/embedding-failure.d.ts.map +1 -1
- package/dist/features/magic-context/memory/embedding-local.d.ts +12 -2
- package/dist/features/magic-context/memory/embedding-local.d.ts.map +1 -1
- package/dist/features/magic-context/memory/embedding-synapse.d.ts +10 -1
- package/dist/features/magic-context/memory/embedding-synapse.d.ts.map +1 -1
- package/dist/features/magic-context/memory/embedding.d.ts.map +1 -1
- package/dist/features/magic-context/memory/project-identity.d.ts +4 -0
- package/dist/features/magic-context/memory/project-identity.d.ts.map +1 -1
- package/dist/features/magic-context/memory/storage-memory-verifications.d.ts +5 -1
- package/dist/features/magic-context/memory/storage-memory-verifications.d.ts.map +1 -1
- package/dist/features/magic-context/memory/storage-memory.d.ts.map +1 -1
- package/dist/features/magic-context/memory/transformers-web-entry.d.ts +2 -0
- package/dist/features/magic-context/memory/transformers-web-entry.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/project-embedding-registry.d.ts +3 -0
- package/dist/features/magic-context/project-embedding-registry.d.ts.map +1 -1
- package/dist/features/magic-context/search.d.ts +18 -0
- package/dist/features/magic-context/search.d.ts.map +1 -1
- package/dist/features/magic-context/storage-db.d.ts +1 -1
- package/dist/features/magic-context/storage-db.d.ts.map +1 -1
- package/dist/features/magic-context/storage-identity-merge.d.ts.map +1 -1
- package/dist/features/magic-context/storage-meta-persisted.d.ts +29 -3
- 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.d.ts +1 -1
- package/dist/features/magic-context/storage-meta.d.ts.map +1 -1
- package/dist/features/magic-context/storage-ops.d.ts +2 -0
- package/dist/features/magic-context/storage-ops.d.ts.map +1 -1
- package/dist/features/magic-context/storage-tags.d.ts +7 -0
- package/dist/features/magic-context/storage-tags.d.ts.map +1 -1
- package/dist/features/magic-context/storage.d.ts +2 -2
- package/dist/features/magic-context/storage.d.ts.map +1 -1
- package/dist/hooks/magic-context/boundary-execution.d.ts +3 -0
- package/dist/hooks/magic-context/boundary-execution.d.ts.map +1 -1
- package/dist/hooks/magic-context/channel2-delivery.d.ts +2 -4
- package/dist/hooks/magic-context/channel2-delivery.d.ts.map +1 -1
- package/dist/hooks/magic-context/command-handler.d.ts +7 -0
- 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-runner-incremental.d.ts.map +1 -1
- package/dist/hooks/magic-context/compartment-runner-recomp.d.ts.map +1 -1
- package/dist/hooks/magic-context/compartment-trigger.d.ts +1 -0
- package/dist/hooks/magic-context/compartment-trigger.d.ts.map +1 -1
- package/dist/hooks/magic-context/ctx-reduce-nudge.d.ts +15 -14
- package/dist/hooks/magic-context/ctx-reduce-nudge.d.ts.map +1 -1
- package/dist/hooks/magic-context/emergency-drop.d.ts +6 -8
- package/dist/hooks/magic-context/emergency-drop.d.ts.map +1 -1
- package/dist/hooks/magic-context/event-resolvers.d.ts +2 -2
- package/dist/hooks/magic-context/execute-status.d.ts +4 -1
- package/dist/hooks/magic-context/execute-status.d.ts.map +1 -1
- package/dist/hooks/magic-context/format-embed-failure.d.ts.map +1 -1
- package/dist/hooks/magic-context/heuristic-cleanup.d.ts +6 -0
- package/dist/hooks/magic-context/heuristic-cleanup.d.ts.map +1 -1
- package/dist/hooks/magic-context/hook-handlers.d.ts +21 -8
- package/dist/hooks/magic-context/hook-handlers.d.ts.map +1 -1
- package/dist/hooks/magic-context/hook.d.ts +13 -0
- package/dist/hooks/magic-context/hook.d.ts.map +1 -1
- package/dist/hooks/magic-context/inject-compartments.d.ts +6 -16
- package/dist/hooks/magic-context/inject-compartments.d.ts.map +1 -1
- package/dist/hooks/magic-context/maintenance-authority.d.ts +3 -0
- package/dist/hooks/magic-context/maintenance-authority.d.ts.map +1 -0
- package/dist/hooks/magic-context/module-transport.d.ts.map +1 -1
- package/dist/hooks/magic-context/module-wire.d.ts.map +1 -1
- package/dist/hooks/magic-context/protected-tail-boundary.d.ts.map +1 -1
- package/dist/hooks/magic-context/read-session-true-raw-tokens.d.ts +1 -0
- package/dist/hooks/magic-context/read-session-true-raw-tokens.d.ts.map +1 -1
- package/dist/hooks/magic-context/rust-mode-transform.d.ts +9 -0
- package/dist/hooks/magic-context/rust-mode-transform.d.ts.map +1 -1
- package/dist/hooks/magic-context/strip-content.d.ts +8 -0
- package/dist/hooks/magic-context/strip-content.d.ts.map +1 -1
- package/dist/hooks/magic-context/stripped-command.d.ts +19 -0
- package/dist/hooks/magic-context/stripped-command.d.ts.map +1 -0
- package/dist/hooks/magic-context/supersession-reclaim.d.ts +11 -0
- package/dist/hooks/magic-context/supersession-reclaim.d.ts.map +1 -1
- package/dist/hooks/magic-context/tag-messages.d.ts +6 -0
- package/dist/hooks/magic-context/tag-messages.d.ts.map +1 -1
- package/dist/hooks/magic-context/tail-hygiene-walk.d.ts +11 -0
- package/dist/hooks/magic-context/tail-hygiene-walk.d.ts.map +1 -1
- package/dist/hooks/magic-context/transform-operations.d.ts +1 -1
- package/dist/hooks/magic-context/transform-operations.d.ts.map +1 -1
- package/dist/hooks/magic-context/transform-postprocess-phase.d.ts +8 -1
- package/dist/hooks/magic-context/transform-postprocess-phase.d.ts.map +1 -1
- package/dist/hooks/magic-context/transform.d.ts.map +1 -1
- package/dist/{index-mg1yx1ph.js → index-2d4kf2ve.js} +2 -2
- package/dist/{index-mmrhhk9a.js → index-37yzyeca.js} +36 -8
- package/dist/index-4x691bz5.js +369 -0
- package/dist/{index-3dkzzh0t.js → index-9j3tx8hy.js} +1 -1
- package/dist/{index-sw8gnq34.js → index-b9j85d29.js} +2 -2
- package/dist/{index-ehpzdy0v.js → index-d1mxrd3p.js} +7 -1
- package/dist/{index-x64cmpej.js → index-dh0vfghk.js} +817 -438
- package/dist/{index-qf20mv7e.js → index-dmvy29z0.js} +1 -1
- package/dist/{index-1h9t9xb5.js → index-eq9wr75c.js} +2 -2
- package/dist/{index-pck9vadr.js → index-vnz6jvdc.js} +1 -1
- package/dist/{index-rw0qrkvq.js → index-z893kjyt.js} +17 -4
- package/dist/{index-btj2qhmc.js → index-zwjhba2r.js} +3 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3230 -2422
- package/dist/{model-resolution-r9jbcz86.js → model-resolution-6w6wzw18.js} +3 -1
- package/dist/plugin/dream-timer.d.ts.map +1 -1
- package/dist/plugin/embedding-routing.d.ts.map +1 -1
- package/dist/plugin/hooks/create-session-hooks.d.ts +14 -0
- package/dist/plugin/hooks/create-session-hooks.d.ts.map +1 -1
- package/dist/plugin/rpc-handlers.d.ts +10 -0
- package/dist/plugin/rpc-handlers.d.ts.map +1 -1
- package/dist/{recomp-orchestrator-dtgww0f0.js → recomp-orchestrator-w7xg48e3.js} +7 -7
- package/dist/{safe-notification-target-tepzc2xa.js → safe-notification-target-2a8py95v.js} +3 -3
- package/dist/{send-session-notification-6v5b72b4.js → send-session-notification-bw7eex6w.js} +4 -4
- package/dist/shared/data-path.d.ts +12 -3
- package/dist/shared/data-path.d.ts.map +1 -1
- package/dist/shared/logger.d.ts.map +1 -1
- package/dist/shared/model-resolution.d.ts +1 -0
- package/dist/shared/model-resolution.d.ts.map +1 -1
- package/dist/shared/rpc-types.d.ts +5 -0
- package/dist/shared/rpc-types.d.ts.map +1 -1
- package/dist/shared/rust-mode-status.d.ts +3 -0
- package/dist/shared/rust-mode-status.d.ts.map +1 -0
- package/dist/shared/status-detail-text.d.ts.map +1 -1
- package/dist/shared/subagent-runner.d.ts +4 -0
- package/dist/shared/subagent-runner.d.ts.map +1 -1
- package/dist/shared/test-temp-dir.d.ts +40 -0
- package/dist/shared/test-temp-dir.d.ts.map +1 -0
- package/dist/shared/write-transaction-timing.d.ts +11 -0
- package/dist/shared/write-transaction-timing.d.ts.map +1 -0
- package/dist/{storage-meta-session-yvtbaw7c.js → storage-meta-session-gr4zn7sq.js} +4 -4
- package/dist/{task-config-a1qmj9bx.js → task-config-gv6s04fp.js} +2 -2
- package/dist/tools/ctx-expand/tools.d.ts.map +1 -1
- package/dist/tools/ctx-memory/tools.d.ts.map +1 -1
- package/dist/tools/ctx-search/tools.d.ts.map +1 -1
- package/dist/transformers-web.js +24570 -0
- package/dist/transformers.node-b1wa0hgx.js +28808 -0
- package/dist/tui/data/context-db.d.ts +10 -3
- package/dist/tui/data/context-db.d.ts.map +1 -1
- package/dist/tui-compiled/data/context-db.d.ts +10 -3
- package/dist/tui-compiled/data/context-db.d.ts.map +1 -1
- package/package.json +7 -3
- package/src/config/index.test.ts +47 -4
- package/src/config/schema/magic-context.test.ts +20 -0
- package/src/config/schema/magic-context.ts +8 -0
- package/src/features/magic-context/dreamer/task-registry.ts +2 -0
- package/src/shared/conflict-detector.test.ts +6 -19
- package/src/shared/conflict-fixer.test.ts +5 -8
- package/src/shared/data-path.test.ts +44 -4
- package/src/shared/data-path.ts +54 -11
- package/src/shared/logger.test.ts +145 -35
- package/src/shared/logger.ts +124 -6
- package/src/shared/model-resolution.test.ts +37 -0
- package/src/shared/model-resolution.ts +7 -0
- package/src/shared/redaction.ts +2 -2
- package/src/shared/rpc-types.ts +5 -0
- package/src/shared/rust-mode-status.ts +3 -0
- package/src/shared/status-detail-text.test.ts +13 -0
- package/src/shared/status-detail-text.ts +2 -0
- package/src/shared/subagent-runner.ts +4 -0
- package/src/shared/test-temp-dir.test.ts +57 -0
- package/src/shared/test-temp-dir.ts +165 -0
- package/src/shared/write-transaction-timing.ts +25 -0
- package/src/tui/data/context-db.test.ts +31 -1
- package/src/tui/data/context-db.ts +17 -61
- package/src/tui/index.tsx +36 -5
- package/src/tui-compiled/data/context-db.ts +17 -61
- package/src/tui-compiled/index.tsx +229 -191
- package/dist/index-8pqfcghm.js +0 -81
- /package/dist/{index-rpgg35ms.js → index-8sseka65.js} +0 -0
|
@@ -2,19 +2,21 @@ import {
|
|
|
2
2
|
deriveTriggerBudget,
|
|
3
3
|
modelSupportsVision,
|
|
4
4
|
shouldEnforcePrivateStoragePermissions
|
|
5
|
-
} from "./index-
|
|
5
|
+
} from "./index-2d4kf2ve.js";
|
|
6
6
|
import {
|
|
7
7
|
describeError,
|
|
8
8
|
getErrorMessage,
|
|
9
9
|
sendIgnoredMessage
|
|
10
|
-
} from "./index-
|
|
10
|
+
} from "./index-zwjhba2r.js";
|
|
11
11
|
import {
|
|
12
|
+
SESSION_SCOPED_TABLES,
|
|
12
13
|
clearCachedM0M1,
|
|
13
14
|
deleteSessionScopedRows,
|
|
14
15
|
ensureSessionMetaRow,
|
|
16
|
+
logSlowWriteTransaction,
|
|
15
17
|
persistCachedM0,
|
|
16
18
|
updateSessionMeta
|
|
17
|
-
} from "./index-
|
|
19
|
+
} from "./index-z893kjyt.js";
|
|
18
20
|
import {
|
|
19
21
|
modelRefLookupOrder,
|
|
20
22
|
piModelRefToCanonical
|
|
@@ -27,7 +29,7 @@ import {
|
|
|
27
29
|
openCodeDbExists,
|
|
28
30
|
withPrivilegedWriter,
|
|
29
31
|
withReadOnlySessionDb
|
|
30
|
-
} from "./index-
|
|
32
|
+
} from "./index-b9j85d29.js";
|
|
31
33
|
import {
|
|
32
34
|
detectConfigFile
|
|
33
35
|
} from "./index-8mtsfhyg.js";
|
|
@@ -36,8 +38,9 @@ import {
|
|
|
36
38
|
} from "./index-d62ypazr.js";
|
|
37
39
|
import {
|
|
38
40
|
log,
|
|
41
|
+
sanitizeDiagnosticText,
|
|
39
42
|
sessionLog
|
|
40
|
-
} from "./index-
|
|
43
|
+
} from "./index-4x691bz5.js";
|
|
41
44
|
import {
|
|
42
45
|
ensureCortexKitArtifactGitignore,
|
|
43
46
|
getDataDir,
|
|
@@ -45,7 +48,7 @@ import {
|
|
|
45
48
|
getLegacyOpenCodeMagicContextStorageDir,
|
|
46
49
|
getMagicContextStorageDir,
|
|
47
50
|
getProjectMagicContextHistorianDir
|
|
48
|
-
} from "./index-
|
|
51
|
+
} from "./index-37yzyeca.js";
|
|
49
52
|
import {
|
|
50
53
|
__export,
|
|
51
54
|
__require
|
|
@@ -398,153 +401,6 @@ function buildPrimaryLanguageDirective(language) {
|
|
|
398
401
|
return `Use ${target} for your natural-language replies to the user unless the user explicitly asks for another language. Keep code, identifiers, file paths, commands, logs, and quoted text verbatim.`;
|
|
399
402
|
}
|
|
400
403
|
|
|
401
|
-
// src/shared/redaction.ts
|
|
402
|
-
import { homedir, userInfo } from "node:os";
|
|
403
|
-
function escapeRegex(value) {
|
|
404
|
-
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
405
|
-
}
|
|
406
|
-
var SECRET_WORDS = [
|
|
407
|
-
"key",
|
|
408
|
-
"token",
|
|
409
|
-
"secret",
|
|
410
|
-
"password",
|
|
411
|
-
"auth",
|
|
412
|
-
"authorization",
|
|
413
|
-
"bearer",
|
|
414
|
-
"credential"
|
|
415
|
-
];
|
|
416
|
-
var SECRET_SEGMENT_PATTERN = new RegExp(`^(?:${SECRET_WORDS.map((w) => `${w}s?`).join("|")})$`, "i");
|
|
417
|
-
var TRAILING_DESCRIPTORS = new Set(["id", "ids", "value", "values", "header", "headers"]);
|
|
418
|
-
function redactionTypeForKey(key) {
|
|
419
|
-
const normalized = key.trim().toLowerCase().replace(/[^a-z0-9_.-]+/g, "_");
|
|
420
|
-
const suffix = normalized.split(".").filter(Boolean).at(-1) ?? normalized;
|
|
421
|
-
return suffix || "secret";
|
|
422
|
-
}
|
|
423
|
-
function isNonSecretScalarValue(value) {
|
|
424
|
-
const v = value.trim();
|
|
425
|
-
if (v === "true" || v === "false" || v === "null" || v === "undefined")
|
|
426
|
-
return true;
|
|
427
|
-
return /^[+-]?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?$/.test(v);
|
|
428
|
-
}
|
|
429
|
-
var SECRET_QUALIFIERS = new Set([
|
|
430
|
-
"api",
|
|
431
|
-
"access",
|
|
432
|
-
"private",
|
|
433
|
-
"client",
|
|
434
|
-
"auth",
|
|
435
|
-
"authorization",
|
|
436
|
-
"secret",
|
|
437
|
-
"bearer",
|
|
438
|
-
"session",
|
|
439
|
-
"refresh",
|
|
440
|
-
"service",
|
|
441
|
-
"x",
|
|
442
|
-
"openai",
|
|
443
|
-
"anthropic",
|
|
444
|
-
"google",
|
|
445
|
-
"github",
|
|
446
|
-
"huggingface",
|
|
447
|
-
"aws",
|
|
448
|
-
"azure"
|
|
449
|
-
]);
|
|
450
|
-
function sanitizePathString(value) {
|
|
451
|
-
const home = homedir();
|
|
452
|
-
const username = userInfo().username;
|
|
453
|
-
let sanitized = value;
|
|
454
|
-
if (home) {
|
|
455
|
-
sanitized = sanitized.replace(new RegExp(escapeRegex(home), "g"), "~");
|
|
456
|
-
}
|
|
457
|
-
sanitized = sanitized.replace(/\/Users\/[^/]+\//g, "/Users/<USER>/");
|
|
458
|
-
sanitized = sanitized.replace(/\/home\/[^/]+\//g, "/home/<USER>/");
|
|
459
|
-
sanitized = sanitized.replace(/C:\\Users\\[^\\]+\\/g, "C:\\Users\\<USER>\\");
|
|
460
|
-
if (username) {
|
|
461
|
-
sanitized = sanitized.replace(new RegExp(escapeRegex(username), "g"), "<USER>");
|
|
462
|
-
}
|
|
463
|
-
return sanitized;
|
|
464
|
-
}
|
|
465
|
-
var SECRET_TEXT_PATTERNS = [
|
|
466
|
-
{
|
|
467
|
-
pattern: /\bsk-ant-(?:api03-)?[A-Za-z0-9_-]{32,}/g,
|
|
468
|
-
replacement: "<ANTHROPIC_API_KEY_REDACTED>"
|
|
469
|
-
},
|
|
470
|
-
{
|
|
471
|
-
pattern: /\bsk-(?:proj-)?[A-Za-z0-9_-]{32,}/g,
|
|
472
|
-
replacement: "<OPENAI_API_KEY_REDACTED>"
|
|
473
|
-
},
|
|
474
|
-
{
|
|
475
|
-
pattern: /\bgithub_pat_[A-Za-z0-9_]{20,}/g,
|
|
476
|
-
replacement: "<GITHUB_PAT_REDACTED>"
|
|
477
|
-
},
|
|
478
|
-
{
|
|
479
|
-
pattern: /\b(?:gh[opsu]|ghr)_[A-Za-z0-9]{30,}/g,
|
|
480
|
-
replacement: "<GITHUB_TOKEN_REDACTED>"
|
|
481
|
-
},
|
|
482
|
-
{
|
|
483
|
-
pattern: /\bhf_[A-Za-z0-9]{30,}/g,
|
|
484
|
-
replacement: "<HUGGINGFACE_TOKEN_REDACTED>"
|
|
485
|
-
},
|
|
486
|
-
{
|
|
487
|
-
pattern: /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/g,
|
|
488
|
-
replacement: "<AWS_ACCESS_KEY_ID_REDACTED>"
|
|
489
|
-
},
|
|
490
|
-
{
|
|
491
|
-
pattern: /\bxox[abprsuvc]-[A-Za-z0-9-]{10,}/g,
|
|
492
|
-
replacement: "<SLACK_TOKEN_REDACTED>"
|
|
493
|
-
},
|
|
494
|
-
{
|
|
495
|
-
pattern: /\bAIza[A-Za-z0-9_-]{35}\b/g,
|
|
496
|
-
replacement: "<GOOGLE_API_KEY_REDACTED>"
|
|
497
|
-
},
|
|
498
|
-
{
|
|
499
|
-
pattern: /\b(Authorization\s*:\s*Bearer\s+)([A-Za-z0-9._~+/=-]{8,})/gi,
|
|
500
|
-
replacement: (_full, prefix) => `${prefix}<REDACTED:bearer>`
|
|
501
|
-
},
|
|
502
|
-
{
|
|
503
|
-
pattern: /\beyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+/g,
|
|
504
|
-
replacement: "<JWT_REDACTED>"
|
|
505
|
-
},
|
|
506
|
-
{
|
|
507
|
-
pattern: /(["'])([^"']*(?:key|token|secret|password|auth|bearer|credential)[^"']*)\1(\s*:\s*)(["'])([^"']*)\4/gi,
|
|
508
|
-
replacement: (full, quote, key, separator, valueQuote, value) => isNonSecretScalarValue(value) ? full : `${quote}${key}${quote}${separator}${valueQuote}<REDACTED:${redactionTypeForKey(key)}>${valueQuote}`
|
|
509
|
-
},
|
|
510
|
-
{
|
|
511
|
-
pattern: /\b([A-Za-z0-9_.-]*(?:key|token|secret|password|auth|bearer|credential)[A-Za-z0-9_.-]*)\s*=\s*([^\s'"`]+)/gi,
|
|
512
|
-
replacement: (full, key, value) => isNonSecretScalarValue(value) ? full : `${key}=<REDACTED:${redactionTypeForKey(key)}>`
|
|
513
|
-
}
|
|
514
|
-
];
|
|
515
|
-
function redactSecretText(value) {
|
|
516
|
-
let redacted = value;
|
|
517
|
-
for (const { pattern, replacement } of SECRET_TEXT_PATTERNS) {
|
|
518
|
-
if (typeof replacement === "string") {
|
|
519
|
-
redacted = redacted.replace(pattern, replacement);
|
|
520
|
-
} else {
|
|
521
|
-
redacted = redacted.replace(pattern, replacement);
|
|
522
|
-
}
|
|
523
|
-
}
|
|
524
|
-
return redacted;
|
|
525
|
-
}
|
|
526
|
-
function sanitizeDiagnosticText(value) {
|
|
527
|
-
return redactSecretText(sanitizePathString(value));
|
|
528
|
-
}
|
|
529
|
-
var SHAREABILITY_SENSITIVE_PATTERNS = [
|
|
530
|
-
/\bC:\/Users\/[^/\s]+/i,
|
|
531
|
-
/(?:^|\s)~\/[^\s]+/,
|
|
532
|
-
/\b(?:api[_-]?key|secret|token|password|passwd|pwd|client[_-]?secret|access[_-]?key)\b\s*[:=]\s*\S+/i,
|
|
533
|
-
/\b(?:localhost|127\.0\.0\.1|0\.0\.0\.0|\[::1\])(?::\d+)?\b/i,
|
|
534
|
-
/\b(?:10|127)\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/,
|
|
535
|
-
/\b192\.168\.\d{1,3}\.\d{1,3}\b/,
|
|
536
|
-
/\b172\.(?:1[6-9]|2\d|3[01])\.\d{1,3}\.\d{1,3}\b/
|
|
537
|
-
];
|
|
538
|
-
function hasShareabilitySensitiveText(text) {
|
|
539
|
-
try {
|
|
540
|
-
if (sanitizeDiagnosticText(text) !== text)
|
|
541
|
-
return true;
|
|
542
|
-
return SHAREABILITY_SENSITIVE_PATTERNS.some((pattern) => pattern.test(text));
|
|
543
|
-
} catch {
|
|
544
|
-
return true;
|
|
545
|
-
}
|
|
546
|
-
}
|
|
547
|
-
|
|
548
404
|
// src/features/magic-context/fail-closed-block.ts
|
|
549
405
|
var FAIL_CLOSED_DOCTOR_COMMAND = "npx @cortexkit/magic-context@latest doctor";
|
|
550
406
|
var FAIL_CLOSED_REPROBE_EVERY_N = 5;
|
|
@@ -1145,6 +1001,18 @@ function isValidPort(port) {
|
|
|
1145
1001
|
// src/features/magic-context/context-authority.ts
|
|
1146
1002
|
import { createHash as createHash2, randomUUID } from "node:crypto";
|
|
1147
1003
|
var AUTHORITY_DOMAINS = ["memories", "notes"];
|
|
1004
|
+
var observedAuthorityRoutingByProject = new Map;
|
|
1005
|
+
function observeAuthorityRouting(projectPath, location) {
|
|
1006
|
+
const previous = observedAuthorityRoutingByProject.get(projectPath);
|
|
1007
|
+
if (previous === location)
|
|
1008
|
+
return;
|
|
1009
|
+
observedAuthorityRoutingByProject.set(projectPath, location);
|
|
1010
|
+
if (location === "MODULE") {
|
|
1011
|
+
log(`[magic-context] project ${projectPath} authority → MODULE: host backends → MODULE: ctx_memory, ctx_note; historian: module-side`);
|
|
1012
|
+
} else if (previous === "MODULE") {
|
|
1013
|
+
log(`[magic-context] project ${projectPath} authority → TS: host backends → TypeScript: ctx_memory, ctx_note; historian: host-side`);
|
|
1014
|
+
}
|
|
1015
|
+
}
|
|
1148
1016
|
var moduleNoteEvaluationBridges = new Map;
|
|
1149
1017
|
function registerModuleNoteEvaluationBridge(projectPath, bridge) {
|
|
1150
1018
|
moduleNoteEvaluationBridges.set(projectPath, bridge);
|
|
@@ -1374,7 +1242,7 @@ async function prepareAuthority(args) {
|
|
|
1374
1242
|
if (identities.length > 0) {
|
|
1375
1243
|
args.db.transaction(() => {
|
|
1376
1244
|
for (const identity of identities) {
|
|
1377
|
-
rememberIdentity(args.db, domain, args.projectPath, identity.moduleRowId, identity.sourceRowId);
|
|
1245
|
+
rememberIdentity(args.db, domain, args.projectPath, identity.moduleRowId, identity.sourceRowId, undefined, true);
|
|
1378
1246
|
}
|
|
1379
1247
|
}).immediate();
|
|
1380
1248
|
}
|
|
@@ -1512,6 +1380,9 @@ async function drainAuthority(args) {
|
|
|
1512
1380
|
if (next === cursor)
|
|
1513
1381
|
break;
|
|
1514
1382
|
}
|
|
1383
|
+
if (args.domain === "memories") {
|
|
1384
|
+
resolvePendingMemoryReferencesForDrain(args.db);
|
|
1385
|
+
}
|
|
1515
1386
|
for (const step of [
|
|
1516
1387
|
"seed",
|
|
1517
1388
|
"memories",
|
|
@@ -1684,6 +1555,83 @@ var MEMORY_SNAPSHOT_COLUMNS = [
|
|
|
1684
1555
|
"mural_cue_at",
|
|
1685
1556
|
"mural_cue_rejection_count"
|
|
1686
1557
|
];
|
|
1558
|
+
var IMMUTABLE_MEMORY_SNAPSHOT_COLUMNS = ["project_path", "first_seen_at", "created_at"];
|
|
1559
|
+
var CLASSIFICATION_MEMORY_SNAPSHOT_COLUMNS = [
|
|
1560
|
+
"importance",
|
|
1561
|
+
"scope",
|
|
1562
|
+
"shareable",
|
|
1563
|
+
"source_type",
|
|
1564
|
+
"classified_at"
|
|
1565
|
+
];
|
|
1566
|
+
var VERIFICATION_MEMORY_SNAPSHOT_COLUMNS = [
|
|
1567
|
+
"verification_status",
|
|
1568
|
+
"verified_at",
|
|
1569
|
+
"mapping",
|
|
1570
|
+
"mapping_origin"
|
|
1571
|
+
];
|
|
1572
|
+
var MURAL_MEMORY_SNAPSHOT_COLUMNS = [
|
|
1573
|
+
"mural_cue",
|
|
1574
|
+
"mural_cue_hash",
|
|
1575
|
+
"mural_cue_at",
|
|
1576
|
+
"mural_cue_rejection_count"
|
|
1577
|
+
];
|
|
1578
|
+
var UPDATED_MEMORY_SNAPSHOT_COLUMNS = [
|
|
1579
|
+
"category",
|
|
1580
|
+
"content",
|
|
1581
|
+
"normalized_hash",
|
|
1582
|
+
"source_session_id",
|
|
1583
|
+
"seen_count",
|
|
1584
|
+
"retrieval_count",
|
|
1585
|
+
"updated_at",
|
|
1586
|
+
"last_seen_at",
|
|
1587
|
+
"last_retrieved_at",
|
|
1588
|
+
"status",
|
|
1589
|
+
"expires_at",
|
|
1590
|
+
"superseded_by_memory_id",
|
|
1591
|
+
"merged_from",
|
|
1592
|
+
"metadata_json"
|
|
1593
|
+
];
|
|
1594
|
+
function memorySnapshotTimestamp(row, key) {
|
|
1595
|
+
const value = row[key];
|
|
1596
|
+
return typeof value === "number" && Number.isFinite(value) ? value : 0;
|
|
1597
|
+
}
|
|
1598
|
+
function parsedMemorySnapshot(snapshotJson, fallback) {
|
|
1599
|
+
try {
|
|
1600
|
+
const parsed = JSON.parse(snapshotJson);
|
|
1601
|
+
return parsed && typeof parsed === "object" && !Array.isArray(parsed) ? parsed : fallback;
|
|
1602
|
+
} catch {
|
|
1603
|
+
return fallback;
|
|
1604
|
+
}
|
|
1605
|
+
}
|
|
1606
|
+
function guardMemorySnapshotByRecency(args) {
|
|
1607
|
+
const effectiveRow = { ...args.row };
|
|
1608
|
+
if (!args.existing) {
|
|
1609
|
+
return { effectiveRow, hostUpdatedNewer: false, hostVerificationNewer: false };
|
|
1610
|
+
}
|
|
1611
|
+
const existing = args.existing;
|
|
1612
|
+
const preserve = (columns) => {
|
|
1613
|
+
for (const column of columns) {
|
|
1614
|
+
if (Object.hasOwn(existing, column)) {
|
|
1615
|
+
effectiveRow[column] = existing[column];
|
|
1616
|
+
}
|
|
1617
|
+
}
|
|
1618
|
+
};
|
|
1619
|
+
preserve(IMMUTABLE_MEMORY_SNAPSHOT_COLUMNS);
|
|
1620
|
+
const hostUpdatedNewer = memorySnapshotTimestamp(existing, "updated_at") > memorySnapshotTimestamp(args.snapshot, "updated_at");
|
|
1621
|
+
const hostClassificationNewer = hostUpdatedNewer || memorySnapshotTimestamp(existing, "classified_at") > memorySnapshotTimestamp(args.snapshot, "classified_at");
|
|
1622
|
+
const snapshotCarriesUpdatedAt = hasSnapshotField(args.snapshot, "updated_at");
|
|
1623
|
+
const hostVerificationNewer = hostUpdatedNewer && snapshotCarriesUpdatedAt;
|
|
1624
|
+
const hostMuralNewer = hostUpdatedNewer && snapshotCarriesUpdatedAt;
|
|
1625
|
+
if (hostUpdatedNewer)
|
|
1626
|
+
preserve(UPDATED_MEMORY_SNAPSHOT_COLUMNS);
|
|
1627
|
+
if (hostClassificationNewer)
|
|
1628
|
+
preserve(CLASSIFICATION_MEMORY_SNAPSHOT_COLUMNS);
|
|
1629
|
+
if (hostVerificationNewer)
|
|
1630
|
+
preserve(VERIFICATION_MEMORY_SNAPSHOT_COLUMNS);
|
|
1631
|
+
if (hostMuralNewer)
|
|
1632
|
+
preserve(MURAL_MEMORY_SNAPSHOT_COLUMNS);
|
|
1633
|
+
return { effectiveRow, hostUpdatedNewer, hostVerificationNewer };
|
|
1634
|
+
}
|
|
1687
1635
|
function hasSnapshotField(row, key) {
|
|
1688
1636
|
return Object.prototype.hasOwnProperty.call(row, key);
|
|
1689
1637
|
}
|
|
@@ -1828,11 +1776,11 @@ function prepareMirrorPageStatements(db) {
|
|
|
1828
1776
|
verification_status = ?, verified_at = ?, classified_at = ?, superseded_by_memory_id = ?,
|
|
1829
1777
|
merged_from = ?, metadata_json = ?, mural_cue = ?, mural_cue_hash = ?, mural_cue_at = ?,
|
|
1830
1778
|
mural_cue_rejection_count = ? WHERE id = ?`),
|
|
1831
|
-
updateSuperseded: db.prepare("UPDATE memories SET superseded_by_memory_id = ? WHERE id = ?"),
|
|
1779
|
+
updateSuperseded: db.prepare("UPDATE memories SET superseded_by_memory_id = ?, updated_at = ? WHERE id = ?"),
|
|
1832
1780
|
deletePendingReference: db.prepare("DELETE FROM mirror_pending_references WHERE domain = 'memories' AND module_project = ? AND module_row_id = ?"),
|
|
1833
1781
|
upsertPendingReference: db.prepare("INSERT INTO mirror_pending_references(domain, module_project, module_row_id, target_module_row_id) VALUES ('memories', ?, ?, ?) ON CONFLICT(domain, module_project, module_row_id) DO UPDATE SET target_module_row_id = excluded.target_module_row_id"),
|
|
1834
1782
|
deleteMemoryVerifications: db.prepare("DELETE FROM memory_verifications WHERE memory_id = ?"),
|
|
1835
|
-
insertMemoryVerification: db.prepare("INSERT INTO memory_verifications(memory_id, file_path, verified_at, mapped_at) VALUES (?, ?, ?, ?)"),
|
|
1783
|
+
insertMemoryVerification: db.prepare("INSERT INTO memory_verifications(memory_id, file_path, verified_at, mapped_at, mapping_origin) VALUES (?, ?, ?, ?, ?)"),
|
|
1836
1784
|
noteById: db.prepare("SELECT * FROM notes WHERE id = ?"),
|
|
1837
1785
|
noteIdByStoreId: db.prepare("SELECT id FROM notes WHERE id = ? AND type = 'smart' AND project_path = ?"),
|
|
1838
1786
|
insertNote: db.prepare("INSERT INTO notes (type, status, content, project_path, session_id, created_at, updated_at) VALUES ('smart', 'active', '', ?, ?, 0, 0)"),
|
|
@@ -1860,7 +1808,8 @@ function prepareMirrorPageStatements(db) {
|
|
|
1860
1808
|
AND target.module_row_id = pending.target_module_row_id
|
|
1861
1809
|
WHERE pending.domain = 'memories'
|
|
1862
1810
|
AND source.context_row_id = memories.id
|
|
1863
|
-
)
|
|
1811
|
+
),
|
|
1812
|
+
updated_at = ?
|
|
1864
1813
|
WHERE id IN (
|
|
1865
1814
|
SELECT source.context_row_id
|
|
1866
1815
|
FROM mirror_pending_references pending
|
|
@@ -1890,7 +1839,7 @@ function prepareMirrorPageStatements(db) {
|
|
|
1890
1839
|
repairPending: db.prepare("SELECT dirty FROM mirror_memory_repair_state WHERE id = 1"),
|
|
1891
1840
|
markRepairPending: db.prepare("UPDATE mirror_memory_repair_state SET dirty = 1, updated_at = ? WHERE id = 1"),
|
|
1892
1841
|
clearRepairPending: db.prepare("UPDATE mirror_memory_repair_state SET dirty = 0, updated_at = ? WHERE id = 1"),
|
|
1893
|
-
repairCandidates: db.prepare(`SELECT memory.id, live.full_row_snapshot
|
|
1842
|
+
repairCandidates: db.prepare(`SELECT memory.id, memory.updated_at, memory.classified_at, live.full_row_snapshot
|
|
1894
1843
|
FROM memories memory
|
|
1895
1844
|
JOIN mirror_identity identity
|
|
1896
1845
|
ON identity.domain = 'memories'
|
|
@@ -1904,9 +1853,14 @@ function prepareMirrorPageStatements(db) {
|
|
|
1904
1853
|
WHERE (memory.source_type IS NULL OR memory.importance IS NULL)
|
|
1905
1854
|
AND live.full_row_snapshot IS NOT NULL`),
|
|
1906
1855
|
repairMemory: db.prepare(`UPDATE memories
|
|
1907
|
-
SET source_type = COALESCE(
|
|
1908
|
-
importance = COALESCE(
|
|
1909
|
-
|
|
1856
|
+
SET source_type = COALESCE(source_type, ?),
|
|
1857
|
+
importance = COALESCE(importance, ?),
|
|
1858
|
+
updated_at = ?
|
|
1859
|
+
WHERE id = ?
|
|
1860
|
+
AND COALESCE(updated_at, 0) <= ?
|
|
1861
|
+
AND COALESCE(classified_at, 0) <= ?
|
|
1862
|
+
AND ((source_type IS NULL AND ? IS NOT NULL)
|
|
1863
|
+
OR (importance IS NULL AND ? IS NOT NULL))`),
|
|
1910
1864
|
updateCursor: db.prepare("INSERT INTO mirror_cursors(domain, cursor, updated_at) VALUES (?, ?, ?) ON CONFLICT(domain) DO UPDATE SET cursor = excluded.cursor, updated_at = excluded.updated_at"),
|
|
1911
1865
|
contextStoreUuid: getContextStoreUuid(db)
|
|
1912
1866
|
};
|
|
@@ -1917,20 +1871,26 @@ function mirrorIdentity(db, domain, moduleProject, moduleRowId, statements) {
|
|
|
1917
1871
|
function applyMirroredNoteCompileFields(args) {
|
|
1918
1872
|
return withPrivilegedWriter(args.db, () => {
|
|
1919
1873
|
const result = args.db.prepare(`UPDATE notes
|
|
1920
|
-
SET compiled_provider = ?, compiled_config = ?, compiled_at = ?, compile_status =
|
|
1874
|
+
SET compiled_provider = ?, compiled_config = ?, compiled_at = ?, compile_status = ?,
|
|
1875
|
+
updated_at = ?
|
|
1921
1876
|
WHERE id = (
|
|
1922
1877
|
SELECT context_row_id
|
|
1923
1878
|
FROM mirror_identity
|
|
1924
1879
|
WHERE domain = 'notes' AND module_project = ? AND module_row_id = ?
|
|
1925
|
-
)`).run(args.fields.compiledProvider, args.fields.compiledConfig, args.fields.compiledAt, args.fields.compileStatus, args.moduleProject, args.moduleRowId);
|
|
1880
|
+
)`).run(args.fields.compiledProvider, args.fields.compiledConfig, args.fields.compiledAt, args.fields.compileStatus, Date.now(), args.moduleProject, args.moduleRowId);
|
|
1926
1881
|
return result.changes === 1;
|
|
1927
1882
|
});
|
|
1928
1883
|
}
|
|
1929
|
-
function rememberIdentity(db, domain, moduleProject, moduleRowId, contextRowId, statements) {
|
|
1884
|
+
function rememberIdentity(db, domain, moduleProject, moduleRowId, contextRowId, statements, replaceContextIdentity = domain === "notes") {
|
|
1930
1885
|
const existing = mirrorIdentity(db, domain, moduleProject, moduleRowId, statements);
|
|
1931
|
-
if (existing)
|
|
1886
|
+
if (existing?.context_row_id === contextRowId)
|
|
1932
1887
|
return;
|
|
1933
|
-
if (
|
|
1888
|
+
if (existing) {
|
|
1889
|
+
if (!replaceContextIdentity)
|
|
1890
|
+
return;
|
|
1891
|
+
(statements?.deleteIdentity ?? db.prepare("DELETE FROM mirror_identity WHERE domain = ? AND module_project = ? AND module_row_id = ?")).run(domain, moduleProject, moduleRowId);
|
|
1892
|
+
}
|
|
1893
|
+
if (replaceContextIdentity) {
|
|
1934
1894
|
(statements?.deleteIdentityByContext ?? db.prepare("DELETE FROM mirror_identity WHERE domain = ? AND context_row_id = ?")).run(domain, contextRowId);
|
|
1935
1895
|
}
|
|
1936
1896
|
(statements?.insertIdentity ?? db.prepare("INSERT OR IGNORE INTO mirror_identity(domain, module_project, module_row_id, context_row_id) VALUES (?, ?, ?, ?)")).run(domain, moduleProject, moduleRowId, contextRowId);
|
|
@@ -1945,7 +1905,7 @@ function contextMemoryId(db, domain, moduleProject, row, moduleRowId, statements
|
|
|
1945
1905
|
if (sourceUuid && sourceUuid === localStoreUuid && sourceId >= 0) {
|
|
1946
1906
|
const existing = (statements?.memoryIdByStoreId ?? db.prepare("SELECT id FROM memories WHERE id = ? AND project_path = ?")).get(sourceId, moduleProject);
|
|
1947
1907
|
if (existing?.id !== undefined) {
|
|
1948
|
-
rememberIdentity(db, domain, moduleProject, moduleRowId, existing.id, statements);
|
|
1908
|
+
rememberIdentity(db, domain, moduleProject, moduleRowId, existing.id, statements, true);
|
|
1949
1909
|
return existing.id;
|
|
1950
1910
|
}
|
|
1951
1911
|
}
|
|
@@ -2002,37 +1962,46 @@ function applyMemoryRow(db, feed, statements) {
|
|
|
2002
1962
|
log(`[magic-context] skipping memory mirror update for module ${moduleProject}/${feed.module_row_id}: context row ${contextId} belongs to ${existing.project_path}`);
|
|
2003
1963
|
return;
|
|
2004
1964
|
}
|
|
1965
|
+
const retainedRecencySnapshot = parsedMemorySnapshot(snapshotJson, row);
|
|
1966
|
+
const recencySnapshot = hasSnapshotField(row, "updated_at") ? row : hasSnapshotField(row, "classified_at") ? { ...retainedRecencySnapshot, classified_at: row.classified_at } : retainedRecencySnapshot;
|
|
1967
|
+
const recencyGuard = guardMemorySnapshotByRecency({
|
|
1968
|
+
row,
|
|
1969
|
+
snapshot: recencySnapshot,
|
|
1970
|
+
existing
|
|
1971
|
+
});
|
|
1972
|
+
const projectedRow = recencyGuard.effectiveRow;
|
|
2005
1973
|
const has = (key) => hasSnapshotField(row, key);
|
|
2006
|
-
const nullableNumber = (key, previous) => has(key) ? typeof
|
|
2007
|
-
const nullableString = (key, previous) => has(key) ? rowNullableString(
|
|
2008
|
-
const hasSuperseded = has("superseded_by_memory_id");
|
|
1974
|
+
const nullableNumber = (key, previous) => has(key) ? typeof projectedRow[key] === "number" && Number.isFinite(projectedRow[key]) ? projectedRow[key] : null : previous ?? null;
|
|
1975
|
+
const nullableString = (key, previous) => has(key) ? rowNullableString(projectedRow, key) : previous ?? null;
|
|
1976
|
+
const hasSuperseded = has("superseded_by_memory_id") && !recencyGuard.hostUpdatedNewer;
|
|
2009
1977
|
const previousHash = existing?.normalized_hash;
|
|
2010
|
-
statements.updateMemory.run(has("project_path") ? rowString(
|
|
2011
|
-
if (hasSuperseded && typeof
|
|
2012
|
-
const translated = mirrorIdentity(db, "memories", moduleProject,
|
|
1978
|
+
statements.updateMemory.run(has("project_path") ? rowString(projectedRow, "project_path") : existing?.project_path ?? moduleProject, has("category") ? rowString(projectedRow, "category", "CONSTRAINTS") : existing?.category ?? "CONSTRAINTS", has("content") ? rowString(projectedRow, "content") : existing?.content ?? "", has("normalized_hash") ? rowString(projectedRow, "normalized_hash") : existing?.normalized_hash ?? "", has("importance") ? typeof projectedRow.importance === "number" && Number.isFinite(projectedRow.importance) ? projectedRow.importance : null : existing?.importance ?? null, has("scope") ? rowString(projectedRow, "scope", "project") : existing?.scope ?? "project", has("shareable") ? rowNumber(projectedRow, "shareable") : existing?.shareable ?? 0, nullableString("source_session_id", existing?.source_session_id), nullableString("source_type", existing?.source_type), has("seen_count") ? rowNumber(projectedRow, "seen_count", 1) : existing?.seen_count ?? 1, has("retrieval_count") ? rowNumber(projectedRow, "retrieval_count") : existing?.retrieval_count ?? 0, has("first_seen_at") ? rowNumber(projectedRow, "first_seen_at") : existing?.first_seen_at ?? 0, has("created_at") ? rowNumber(projectedRow, "created_at") : existing?.created_at ?? 0, has("updated_at") ? rowNumber(projectedRow, "updated_at") : existing?.updated_at ?? 0, has("last_seen_at") ? rowNumber(projectedRow, "last_seen_at") : existing?.last_seen_at ?? 0, nullableNumber("last_retrieved_at", existing?.last_retrieved_at), has("status") ? rowString(projectedRow, "status", "active") : existing?.status ?? "active", nullableNumber("expires_at", existing?.expires_at), has("verification_status") ? rowString(projectedRow, "verification_status", "unverified") : existing?.verification_status ?? "unverified", nullableNumber("verified_at", existing?.verified_at), nullableNumber("classified_at", existing?.classified_at), hasSuperseded ? null : existing?.superseded_by_memory_id ?? null, nullableString("merged_from", existing?.merged_from), nullableString("metadata_json", existing?.metadata_json), nullableString("mural_cue", existing?.mural_cue), nullableString("mural_cue_hash", existing?.mural_cue_hash), nullableNumber("mural_cue_at", existing?.mural_cue_at), has("mural_cue_rejection_count") ? rowNumber(projectedRow, "mural_cue_rejection_count") : existing?.mural_cue_rejection_count ?? 0, contextId);
|
|
1979
|
+
if (hasSuperseded && typeof projectedRow.superseded_by_memory_id === "number") {
|
|
1980
|
+
const translated = mirrorIdentity(db, "memories", moduleProject, projectedRow.superseded_by_memory_id, statements);
|
|
2013
1981
|
if (translated) {
|
|
2014
|
-
statements.updateSuperseded.run(translated.context_row_id, contextId);
|
|
1982
|
+
statements.updateSuperseded.run(translated.context_row_id, Date.now(), contextId);
|
|
2015
1983
|
statements.deletePendingReference.run(moduleProject, feed.module_row_id);
|
|
2016
1984
|
} else {
|
|
2017
|
-
statements.upsertPendingReference.run(moduleProject, feed.module_row_id,
|
|
1985
|
+
statements.upsertPendingReference.run(moduleProject, feed.module_row_id, projectedRow.superseded_by_memory_id);
|
|
2018
1986
|
}
|
|
2019
1987
|
} else if (hasSuperseded) {
|
|
2020
1988
|
statements.deletePendingReference.run(moduleProject, feed.module_row_id);
|
|
2021
1989
|
}
|
|
2022
|
-
const appliedHash = has("normalized_hash") ? rowString(
|
|
1990
|
+
const appliedHash = has("normalized_hash") ? rowString(projectedRow, "normalized_hash") : previousHash;
|
|
2023
1991
|
if (previousHash !== appliedHash && appliedHash !== undefined) {
|
|
2024
1992
|
statements.deleteMemoryEmbeddings.run(contextId);
|
|
2025
1993
|
}
|
|
2026
|
-
if (has("mapping")) {
|
|
1994
|
+
if (has("mapping") && !recencyGuard.hostVerificationNewer) {
|
|
2027
1995
|
statements.deleteMemoryVerifications.run(contextId);
|
|
2028
|
-
if (Array.isArray(
|
|
1996
|
+
if (Array.isArray(projectedRow.mapping)) {
|
|
2029
1997
|
const files = [
|
|
2030
|
-
...new Set(
|
|
1998
|
+
...new Set(projectedRow.mapping.filter((file) => typeof file === "string").sort())
|
|
2031
1999
|
];
|
|
2032
|
-
const verifiedAt = rowNumber(
|
|
2033
|
-
const mappedAt = rowNumber(
|
|
2000
|
+
const verifiedAt = rowNumber(projectedRow, "verified_at");
|
|
2001
|
+
const mappedAt = rowNumber(projectedRow, "updated_at", Date.now());
|
|
2002
|
+
const mappingOrigin = projectedRow.mapping_origin === "host_rejected_fallback" ? "host_rejected_fallback" : "mapper";
|
|
2034
2003
|
for (const file of files.length > 0 ? files : [""]) {
|
|
2035
|
-
statements.insertMemoryVerification.run(contextId, file, verifiedAt, mappedAt);
|
|
2004
|
+
statements.insertMemoryVerification.run(contextId, file, verifiedAt, mappedAt, mappingOrigin);
|
|
2036
2005
|
}
|
|
2037
2006
|
}
|
|
2038
2007
|
}
|
|
@@ -2058,7 +2027,11 @@ function repairNullClobberedMemoryRows(statements) {
|
|
|
2058
2027
|
const importance = hasSnapshotField(snapshot, "importance") && typeof snapshot.importance === "number" && Number.isFinite(snapshot.importance) ? snapshot.importance : null;
|
|
2059
2028
|
if (sourceType === null && importance === null)
|
|
2060
2029
|
continue;
|
|
2061
|
-
|
|
2030
|
+
const snapshotVintage = Math.max(memorySnapshotTimestamp(snapshot, "updated_at"), memorySnapshotTimestamp(snapshot, "classified_at"));
|
|
2031
|
+
const hostVintage = Math.max(candidate.updated_at ?? 0, candidate.classified_at ?? 0);
|
|
2032
|
+
if (hostVintage > snapshotVintage)
|
|
2033
|
+
continue;
|
|
2034
|
+
statements.repairMemory.run(sourceType, importance, Date.now(), candidate.id, snapshotVintage, snapshotVintage, sourceType, importance);
|
|
2062
2035
|
}
|
|
2063
2036
|
statements.clearRepairPending.run(Date.now());
|
|
2064
2037
|
}
|
|
@@ -2083,9 +2056,17 @@ function contextNoteId(db, feed, moduleProject, statements) {
|
|
|
2083
2056
|
return contextId;
|
|
2084
2057
|
}
|
|
2085
2058
|
function translateMemoryReferences(statements) {
|
|
2086
|
-
statements.translateMemoryReferences.run();
|
|
2059
|
+
statements.translateMemoryReferences.run(Date.now());
|
|
2087
2060
|
statements.clearTranslatedReferences.run();
|
|
2088
2061
|
}
|
|
2062
|
+
function resolvePendingMemoryReferencesForDrain(db) {
|
|
2063
|
+
withPrivilegedWriter(db, () => {
|
|
2064
|
+
db.transaction(() => {
|
|
2065
|
+
const statements = prepareMirrorPageStatements(db);
|
|
2066
|
+
translateMemoryReferences(statements);
|
|
2067
|
+
}).immediate();
|
|
2068
|
+
});
|
|
2069
|
+
}
|
|
2089
2070
|
function applyNoteRow(db, feed, statements) {
|
|
2090
2071
|
const row = feed.full_row_snapshot;
|
|
2091
2072
|
const moduleProject = rowString(row, "project_path");
|
|
@@ -2153,6 +2134,24 @@ function applyMirrorPage(args) {
|
|
|
2153
2134
|
if (page.domain === "memories" && resnapshotStatus !== "complete" && page.rows.some((feed) => feed.op === "tombstone")) {
|
|
2154
2135
|
throw new Error("memory mirror resnapshot must complete before tombstones");
|
|
2155
2136
|
}
|
|
2137
|
+
if (page.domain === "memories") {
|
|
2138
|
+
for (const feed of page.rows) {
|
|
2139
|
+
if (feed.domain !== "memories" || feed.op === "tombstone" || feed.feed_seq <= durableCursor) {
|
|
2140
|
+
continue;
|
|
2141
|
+
}
|
|
2142
|
+
const row = feed.full_row_snapshot;
|
|
2143
|
+
const moduleProject = rowString(row, "project_path");
|
|
2144
|
+
const sourceUuid = rowNullableString(row, "context_store_uuid");
|
|
2145
|
+
const sourceId = rowNumber(row, "context_row_id", -1);
|
|
2146
|
+
if (!moduleProject || !sourceUuid || sourceUuid !== statements.contextStoreUuid || sourceId < 0) {
|
|
2147
|
+
continue;
|
|
2148
|
+
}
|
|
2149
|
+
const existing = statements.memoryIdByStoreId.get(sourceId, moduleProject);
|
|
2150
|
+
if (existing?.id === undefined)
|
|
2151
|
+
continue;
|
|
2152
|
+
rememberIdentity(db, "memories", moduleProject, feed.module_row_id, existing.id, statements, true);
|
|
2153
|
+
}
|
|
2154
|
+
}
|
|
2156
2155
|
const touchedProjects = new Set;
|
|
2157
2156
|
for (const feed of page.rows) {
|
|
2158
2157
|
if (feed.domain !== page.domain || feed.feed_seq <= nextCursor)
|
|
@@ -2623,7 +2622,7 @@ var CATEGORY_DEFAULT_TTL = {
|
|
|
2623
2622
|
import { execFileSync as execFileSync2 } from "node:child_process";
|
|
2624
2623
|
import { createHash as createHash3 } from "node:crypto";
|
|
2625
2624
|
import { existsSync, realpathSync, statSync } from "node:fs";
|
|
2626
|
-
import { homedir
|
|
2625
|
+
import { homedir } from "node:os";
|
|
2627
2626
|
import path from "node:path";
|
|
2628
2627
|
var GIT_TIMEOUT_MS = 5000;
|
|
2629
2628
|
var TRANSIENT_FAILURE_COOLDOWN_MS = 5 * 60 * 1000;
|
|
@@ -2637,7 +2636,7 @@ var dubiousOwnershipLoggedDirectories = new Set;
|
|
|
2637
2636
|
var dubiousOwnershipWarnedDirectories = new Set;
|
|
2638
2637
|
var transientGitIdentityReuseLoggedDirectories = new Set;
|
|
2639
2638
|
var execFileSyncForIdentity = execFileSync2;
|
|
2640
|
-
var userHomeDirectoryForIdentity = () =>
|
|
2639
|
+
var userHomeDirectoryForIdentity = () => homedir();
|
|
2641
2640
|
var nowMs = () => Date.now();
|
|
2642
2641
|
|
|
2643
2642
|
class ProjectIdentityError extends Error {
|
|
@@ -2952,6 +2951,9 @@ function gitRootDirectory(canonical) {
|
|
|
2952
2951
|
return null;
|
|
2953
2952
|
}
|
|
2954
2953
|
}
|
|
2954
|
+
function directoryHasGitMetadata(directory) {
|
|
2955
|
+
return gitRootDirectory(path.resolve(directory)) !== null;
|
|
2956
|
+
}
|
|
2955
2957
|
function resolveProjectIdentityForSession(directory, allowHomeProject = false) {
|
|
2956
2958
|
const canonicalHome = canonicalUserHomeDirectory();
|
|
2957
2959
|
const canonicalDirectory = (() => {
|
|
@@ -5405,6 +5407,15 @@ var MIGRATIONS = [
|
|
|
5405
5407
|
);
|
|
5406
5408
|
`);
|
|
5407
5409
|
}
|
|
5410
|
+
},
|
|
5411
|
+
{
|
|
5412
|
+
version: 82,
|
|
5413
|
+
description: "record the origin of memory file-independent mappings",
|
|
5414
|
+
up(db) {
|
|
5415
|
+
if (!tableExists2(db, "memory_verifications"))
|
|
5416
|
+
return;
|
|
5417
|
+
ensureColumn(db, "memory_verifications", "mapping_origin", "TEXT NOT NULL DEFAULT 'mapper'");
|
|
5418
|
+
}
|
|
5408
5419
|
}
|
|
5409
5420
|
];
|
|
5410
5421
|
var LATEST_MIGRATION_VERSION = MIGRATIONS.reduce((max, m) => Math.max(max, m.version), 0);
|
|
@@ -5450,6 +5461,7 @@ function runMigrations(db) {
|
|
|
5450
5461
|
let migration;
|
|
5451
5462
|
let currentVersion = 0;
|
|
5452
5463
|
try {
|
|
5464
|
+
const transactionStartedAt = performance.now();
|
|
5453
5465
|
const applied = db.transaction(() => {
|
|
5454
5466
|
currentVersion = getCurrentVersion(db);
|
|
5455
5467
|
migration = MIGRATIONS.find((candidate) => candidate.version > currentVersion && !isMigrationApplied(db, candidate.version));
|
|
@@ -5464,6 +5476,7 @@ function runMigrations(db) {
|
|
|
5464
5476
|
db.prepare("INSERT INTO schema_migrations (version, description, applied_at) VALUES (?, ?, ?)").run(migration.version, migration.description, Date.now());
|
|
5465
5477
|
return true;
|
|
5466
5478
|
}).immediate();
|
|
5479
|
+
logSlowWriteTransaction("migration-runner", transactionStartedAt);
|
|
5467
5480
|
if (!applied || !migration)
|
|
5468
5481
|
break;
|
|
5469
5482
|
if (migration.version <= 61)
|
|
@@ -5488,7 +5501,9 @@ function runMigrations(db) {
|
|
|
5488
5501
|
}
|
|
5489
5502
|
if (touchedLegacyAuthorityBatch) {
|
|
5490
5503
|
try {
|
|
5504
|
+
const transactionStartedAt = performance.now();
|
|
5491
5505
|
db.transaction(() => installLatestAuthorityTriggers(db)).immediate();
|
|
5506
|
+
logSlowWriteTransaction("migration-runner", transactionStartedAt);
|
|
5492
5507
|
} catch (error) {
|
|
5493
5508
|
throw new Error(`Migration authority-trigger postcondition failed: ${error instanceof Error ? error.message : String(error)}. Database may need manual repair.`);
|
|
5494
5509
|
}
|
|
@@ -5524,7 +5539,7 @@ import { createHash as createHash5 } from "node:crypto";
|
|
|
5524
5539
|
// src/hooks/magic-context/read-session-formatting.ts
|
|
5525
5540
|
import { existsSync as existsSync2, readFileSync as readFileSync2, realpathSync as realpathSync2 } from "node:fs";
|
|
5526
5541
|
import { createRequire } from "node:module";
|
|
5527
|
-
import { homedir as
|
|
5542
|
+
import { homedir as homedir2 } from "node:os";
|
|
5528
5543
|
import { dirname, join as join2, resolve } from "node:path";
|
|
5529
5544
|
import { pathToFileURL } from "node:url";
|
|
5530
5545
|
|
|
@@ -5641,7 +5656,7 @@ var tokenizerLoadPromise;
|
|
|
5641
5656
|
var tokenizerWarningSent = false;
|
|
5642
5657
|
function tokenizerPackageRoots() {
|
|
5643
5658
|
const cwd = process.cwd();
|
|
5644
|
-
const openCodeCache = join2(process.env.XDG_CACHE_HOME ?? join2(
|
|
5659
|
+
const openCodeCache = join2(process.env.XDG_CACHE_HOME ?? join2(homedir2(), ".cache"), "opencode");
|
|
5645
5660
|
const roots = [cwd, openCodeCache];
|
|
5646
5661
|
const candidates = [];
|
|
5647
5662
|
for (const root of roots) {
|
|
@@ -6209,7 +6224,7 @@ function getSchemaFenceRejection() {
|
|
|
6209
6224
|
function getMigrationOnOpenRefusal() {
|
|
6210
6225
|
return lastMigrationOnOpenRefusal;
|
|
6211
6226
|
}
|
|
6212
|
-
var LATEST_SUPPORTED_VERSION =
|
|
6227
|
+
var LATEST_SUPPORTED_VERSION = 82;
|
|
6213
6228
|
var PERMISSIONS_ENFORCEABLE = process.platform !== "win32";
|
|
6214
6229
|
var defaultStoragePermissionFs = { chmodSync, mkdirSync };
|
|
6215
6230
|
var storagePermissionFs = defaultStoragePermissionFs;
|
|
@@ -6958,8 +6973,10 @@ function initializeDatabase(db) {
|
|
|
6958
6973
|
-- verified_at=0 means "mapped (files known) but not yet content-verified".
|
|
6959
6974
|
-- map-memories sets mapped_at + verified_at=0; verify sets verified_at=now.
|
|
6960
6975
|
verified_at INTEGER NOT NULL,
|
|
6961
|
-
|
|
6962
|
-
|
|
6976
|
+
mapped_at INTEGER NOT NULL DEFAULT 0,
|
|
6977
|
+
-- Distinguishes mapper-authored independence from a host rejection fallback.
|
|
6978
|
+
mapping_origin TEXT NOT NULL DEFAULT 'mapper',
|
|
6979
|
+
PRIMARY KEY (memory_id, file_path)
|
|
6963
6980
|
);
|
|
6964
6981
|
CREATE INDEX IF NOT EXISTS idx_memory_verifications_memory ON memory_verifications(memory_id);
|
|
6965
6982
|
|
|
@@ -8610,15 +8627,32 @@ function buildToolArcs(messages) {
|
|
|
8610
8627
|
}
|
|
8611
8628
|
return arcs.sort((a, b) => a.invOrdinal - b.invOrdinal || (a.resOrdinal ?? Number.MAX_SAFE_INTEGER) - (b.resOrdinal ?? Number.MAX_SAFE_INTEGER));
|
|
8612
8629
|
}
|
|
8630
|
+
function fenceBoundaryForCompletedToolArcs(candidate, arcs, publicationFloorOrdinal) {
|
|
8631
|
+
const completed = arcs.filter((arc) => arc.resOrdinal !== null).map((arc) => ({ invocation: arc.invOrdinal, result: arc.resOrdinal }));
|
|
8632
|
+
const component = completed.filter((arc) => completedToolArcCrossesBoundary(arc.invocation, arc.result, candidate));
|
|
8633
|
+
if (component.length === 0)
|
|
8634
|
+
return candidate;
|
|
8635
|
+
for (let pass = 0;pass <= completed.length; pass += 1) {
|
|
8636
|
+
const minInvocation2 = Math.min(...component.map((arc) => arc.invocation));
|
|
8637
|
+
const maxResult2 = Math.max(...component.map((arc) => arc.result));
|
|
8638
|
+
const before = component.length;
|
|
8639
|
+
for (const arc of completed) {
|
|
8640
|
+
if (arc.invocation <= maxResult2 && arc.result >= minInvocation2 && !component.includes(arc)) {
|
|
8641
|
+
component.push(arc);
|
|
8642
|
+
}
|
|
8643
|
+
}
|
|
8644
|
+
if (component.length === before)
|
|
8645
|
+
break;
|
|
8646
|
+
}
|
|
8647
|
+
const minInvocation = Math.min(...component.map((arc) => arc.invocation));
|
|
8648
|
+
const maxResult = Math.max(...component.map((arc) => arc.result));
|
|
8649
|
+
return minInvocation < publicationFloorOrdinal ? maxResult + 1 : minInvocation;
|
|
8650
|
+
}
|
|
8613
8651
|
function fenceBoundaryForToolArcs(candidate, arcs, lastCompartmentEndOrdinal, recentOpenArcCutoff) {
|
|
8614
|
-
|
|
8652
|
+
const boundary = fenceBoundaryForCompletedToolArcs(candidate, arcs, lastCompartmentEndOrdinal + 1);
|
|
8615
8653
|
for (const arc of arcs) {
|
|
8616
|
-
if (arc.resOrdinal !== null)
|
|
8617
|
-
if (completedToolArcCrossesBoundary(arc.invOrdinal, arc.resOrdinal, boundary)) {
|
|
8618
|
-
boundary = arc.resOrdinal + 1;
|
|
8619
|
-
}
|
|
8654
|
+
if (arc.resOrdinal !== null)
|
|
8620
8655
|
continue;
|
|
8621
|
-
}
|
|
8622
8656
|
if (arc.invOrdinal < recentOpenArcCutoff)
|
|
8623
8657
|
continue;
|
|
8624
8658
|
if (arc.invOrdinal >= lastCompartmentEndOrdinal + 1 && arc.invOrdinal < boundary) {
|
|
@@ -9975,6 +10009,16 @@ function persistMessageHistoryOrphanSweepState(db, cursor, lastSweptAt) {
|
|
|
9975
10009
|
cursor_session_id = excluded.cursor_session_id,
|
|
9976
10010
|
last_swept_at = excluded.last_swept_at`).run(cursor, lastSweptAt);
|
|
9977
10011
|
}
|
|
10012
|
+
function getOpenCodeSessionScopedCandidateSourceSql() {
|
|
10013
|
+
return SESSION_SCOPED_TABLES.filter((definition) => definition.harnessScoped === true).map((definition) => {
|
|
10014
|
+
const predicates = ["session_id IS NOT NULL", "harness = 'opencode'"];
|
|
10015
|
+
if (definition.extraPredicate)
|
|
10016
|
+
predicates.push(definition.extraPredicate);
|
|
10017
|
+
return `SELECT session_id FROM ${definition.table} WHERE ${predicates.join(" AND ")}`;
|
|
10018
|
+
}).join(`
|
|
10019
|
+
UNION
|
|
10020
|
+
`);
|
|
10021
|
+
}
|
|
9978
10022
|
function sweepOrphanedOpenCodeMessageIndexes(db, openReadableOpenCodeDb, options = {}) {
|
|
9979
10023
|
const now = options.now ?? Date.now();
|
|
9980
10024
|
const batchSize = Math.max(1, Math.floor(options.batchSize ?? MESSAGE_HISTORY_ORPHAN_SWEEP_BATCH_SIZE));
|
|
@@ -9998,13 +10042,19 @@ function sweepOrphanedOpenCodeMessageIndexes(db, openReadableOpenCodeDb, options
|
|
|
9998
10042
|
}
|
|
9999
10043
|
try {
|
|
10000
10044
|
const cutoff = now - safetyAgeMs;
|
|
10045
|
+
const candidateSourceSql = getOpenCodeSessionScopedCandidateSourceSql();
|
|
10001
10046
|
const candidates = db.prepare(`SELECT session_id
|
|
10002
|
-
FROM
|
|
10003
|
-
WHERE
|
|
10004
|
-
AND
|
|
10005
|
-
|
|
10047
|
+
FROM (${candidateSourceSql}) AS session_candidates
|
|
10048
|
+
WHERE session_id > ?
|
|
10049
|
+
AND NOT EXISTS (
|
|
10050
|
+
SELECT 1
|
|
10051
|
+
FROM message_history_index
|
|
10052
|
+
WHERE message_history_index.session_id = session_candidates.session_id
|
|
10053
|
+
AND message_history_index.harness = 'opencode'
|
|
10054
|
+
AND message_history_index.updated_at > ?
|
|
10055
|
+
)
|
|
10006
10056
|
ORDER BY session_id ASC
|
|
10007
|
-
LIMIT ?`).all(
|
|
10057
|
+
LIMIT ?`).all(cursor, cutoff, batchSize);
|
|
10008
10058
|
const sessionExists = openCodeDb.prepare("SELECT 1 FROM session WHERE id = ? LIMIT 1");
|
|
10009
10059
|
const missingSessionIds = candidates.filter((candidate) => !sessionExists.get(candidate.session_id)).map((candidate) => candidate.session_id);
|
|
10010
10060
|
const nextCursor = candidates.length < batchSize ? "" : candidates[candidates.length - 1]?.session_id ?? cursor;
|
|
@@ -10013,7 +10063,17 @@ function sweepOrphanedOpenCodeMessageIndexes(db, openReadableOpenCodeDb, options
|
|
|
10013
10063
|
let committed = false;
|
|
10014
10064
|
let deleted = 0;
|
|
10015
10065
|
try {
|
|
10016
|
-
const stillEligible = db.prepare(
|
|
10066
|
+
const stillEligible = db.prepare(`SELECT 1
|
|
10067
|
+
FROM (${candidateSourceSql}) AS session_candidates
|
|
10068
|
+
WHERE session_id = ?
|
|
10069
|
+
AND NOT EXISTS (
|
|
10070
|
+
SELECT 1
|
|
10071
|
+
FROM message_history_index
|
|
10072
|
+
WHERE message_history_index.session_id = session_candidates.session_id
|
|
10073
|
+
AND message_history_index.harness = 'opencode'
|
|
10074
|
+
AND message_history_index.updated_at > ?
|
|
10075
|
+
)
|
|
10076
|
+
LIMIT 1`);
|
|
10017
10077
|
const eligibleSessionIds = missingSessionIds.filter((sessionId) => stillEligible.get(sessionId, cutoff));
|
|
10018
10078
|
deleteSessionScopedRows(db, eligibleSessionIds, "opencode");
|
|
10019
10079
|
deleted = eligibleSessionIds.length;
|
|
@@ -10448,6 +10508,13 @@ function isProviderOverflowReconfirmed(sessionId) {
|
|
|
10448
10508
|
function getEmergencyRecoveryArmedAt(sessionId) {
|
|
10449
10509
|
return emergencyRecoveryArmedAtBySession.get(sessionId) ?? null;
|
|
10450
10510
|
}
|
|
10511
|
+
function describeProtectedTailDrainBudgetSkip(result) {
|
|
10512
|
+
const state2 = result.budgetState;
|
|
10513
|
+
if (!state2)
|
|
10514
|
+
return "historian skip: internal drain budget spent";
|
|
10515
|
+
const resetMinutes = Math.max(1, Math.ceil(state2.resetInMs / 60000));
|
|
10516
|
+
return `historian skip: internal drain budget spent (${state2.spentTokens}/${state2.limitTokens} tokens; resets in ${resetMinutes}m)`;
|
|
10517
|
+
}
|
|
10451
10518
|
var CAS_RETRY_LIMIT = 5;
|
|
10452
10519
|
var AUTO_SEARCH_NO_HINT_REASONS = new Set([
|
|
10453
10520
|
"below-threshold",
|
|
@@ -10827,19 +10894,28 @@ function reserveProtectedTailDrainTokens(args) {
|
|
|
10827
10894
|
const now = args.now ?? Date.now();
|
|
10828
10895
|
const requested = Math.max(0, Math.floor(args.trueRawTokens));
|
|
10829
10896
|
if (requested === 0) {
|
|
10830
|
-
return {
|
|
10897
|
+
return {
|
|
10898
|
+
ok: true,
|
|
10899
|
+
reservedTokens: 0,
|
|
10900
|
+
overQuotaBypass: false,
|
|
10901
|
+
reservation: null,
|
|
10902
|
+
budgetState: null
|
|
10903
|
+
};
|
|
10831
10904
|
}
|
|
10832
10905
|
let result = {
|
|
10833
10906
|
ok: false,
|
|
10834
10907
|
reservedTokens: 0,
|
|
10835
10908
|
overQuotaBypass: false,
|
|
10836
10909
|
reservation: null,
|
|
10837
|
-
|
|
10910
|
+
budgetState: null,
|
|
10911
|
+
skippedReason: "internal drain budget spent"
|
|
10838
10912
|
};
|
|
10839
10913
|
args.db.transaction(() => {
|
|
10840
10914
|
ensureSessionMetaRow(args.db, args.sessionId);
|
|
10841
10915
|
let meta = loadProtectedTailMeta(args.db, args.sessionId);
|
|
10842
|
-
|
|
10916
|
+
const windowStartedAt = meta.protectedTailDrainWindowStartedAt;
|
|
10917
|
+
const windowExpired = windowStartedAt <= 0 || windowStartedAt > now || now - windowStartedAt >= DRAIN_WINDOW_MS;
|
|
10918
|
+
if (windowExpired) {
|
|
10843
10919
|
args.db.prepare(`UPDATE session_meta
|
|
10844
10920
|
SET protected_tail_drain_window_started_at = ?, protected_tail_drain_tokens = 0
|
|
10845
10921
|
WHERE session_id = ?`).run(now, args.sessionId);
|
|
@@ -10864,22 +10940,35 @@ function reserveProtectedTailDrainTokens(args) {
|
|
|
10864
10940
|
const remaining = Math.max(0, budget - meta.protectedTailDrainTokens);
|
|
10865
10941
|
let reserved = Math.min(requested, args.perRunCap, remaining);
|
|
10866
10942
|
let bypass = false;
|
|
10867
|
-
const inFailureBackoff = meta.historianDrainFailureAt > 0 && now - meta.historianDrainFailureAt < EMERGENCY_DRAIN_FAILURE_BACKOFF_MS;
|
|
10943
|
+
const inFailureBackoff = meta.historianDrainFailureAt > 0 && meta.historianDrainFailureAt <= now && now - meta.historianDrainFailureAt < EMERGENCY_DRAIN_FAILURE_BACKOFF_MS;
|
|
10868
10944
|
if (reserved <= 0 && latchActive && !inFailureBackoff) {
|
|
10869
10945
|
reserved = Math.min(requested, args.perRunCap);
|
|
10870
10946
|
bypass = true;
|
|
10871
10947
|
}
|
|
10872
|
-
|
|
10948
|
+
const activeWindowStartedAt = meta.protectedTailDrainWindowStartedAt;
|
|
10949
|
+
const budgetState = (spentTokens) => ({
|
|
10950
|
+
windowStartedAt: activeWindowStartedAt,
|
|
10951
|
+
resetsAt: activeWindowStartedAt + DRAIN_WINDOW_MS,
|
|
10952
|
+
resetInMs: Math.max(0, activeWindowStartedAt + DRAIN_WINDOW_MS - now),
|
|
10953
|
+
spentTokens,
|
|
10954
|
+
limitTokens: budget
|
|
10955
|
+
});
|
|
10956
|
+
if (reserved <= 0) {
|
|
10957
|
+
result = {
|
|
10958
|
+
...result,
|
|
10959
|
+
budgetState: budgetState(meta.protectedTailDrainTokens)
|
|
10960
|
+
};
|
|
10873
10961
|
return;
|
|
10962
|
+
}
|
|
10874
10963
|
args.db.prepare(`UPDATE session_meta
|
|
10875
|
-
SET
|
|
10876
|
-
|
|
10877
|
-
WHERE session_id = ?`).run(now, reserved, args.sessionId);
|
|
10964
|
+
SET protected_tail_drain_tokens = COALESCE(protected_tail_drain_tokens, 0) + ?
|
|
10965
|
+
WHERE session_id = ?`).run(reserved, args.sessionId);
|
|
10878
10966
|
result = {
|
|
10879
10967
|
ok: true,
|
|
10880
10968
|
reservedTokens: reserved,
|
|
10881
10969
|
overQuotaBypass: bypass,
|
|
10882
|
-
reservation: { sessionId: args.sessionId, runId: args.runId, tokens: reserved }
|
|
10970
|
+
reservation: { sessionId: args.sessionId, runId: args.runId, tokens: reserved },
|
|
10971
|
+
budgetState: budgetState(meta.protectedTailDrainTokens + reserved)
|
|
10883
10972
|
};
|
|
10884
10973
|
})();
|
|
10885
10974
|
return result;
|
|
@@ -10957,19 +11046,38 @@ function parseChannel1NudgeState(raw) {
|
|
|
10957
11046
|
try {
|
|
10958
11047
|
const parsed = JSON.parse(raw);
|
|
10959
11048
|
if (typeof parsed === "object" && parsed !== null && !Array.isArray(parsed)) {
|
|
10960
|
-
|
|
11049
|
+
const state2 = {
|
|
10961
11050
|
level: normalizeLastNudgeLevel(parsed.level),
|
|
10962
11051
|
ordinal: typeof parsed.ordinal === "number" ? Math.max(0, Math.round(parsed.ordinal)) : 0
|
|
10963
11052
|
};
|
|
11053
|
+
if (parsed.postReduceGracePending === true)
|
|
11054
|
+
state2.postReduceGracePending = true;
|
|
11055
|
+
if (typeof parsed.postReduceGraceBaselineU === "number" && Number.isFinite(parsed.postReduceGraceBaselineU)) {
|
|
11056
|
+
state2.postReduceGraceBaselineU = Math.max(0, Math.round(parsed.postReduceGraceBaselineU));
|
|
11057
|
+
}
|
|
11058
|
+
const preLevel = normalizeLastNudgeLevel(parsed.postReduceGracePreLevel);
|
|
11059
|
+
if (preLevel !== "" || parsed.postReduceGracePreLevel === "") {
|
|
11060
|
+
state2.postReduceGracePreLevel = preLevel;
|
|
11061
|
+
}
|
|
11062
|
+
return state2;
|
|
10964
11063
|
}
|
|
10965
11064
|
} catch {}
|
|
10966
11065
|
return { level: normalizeLastNudgeLevel(raw), ordinal: 0 };
|
|
10967
11066
|
}
|
|
10968
11067
|
function serializeChannel1NudgeState(value) {
|
|
10969
|
-
|
|
11068
|
+
const serialized = {
|
|
10970
11069
|
level: normalizeLastNudgeLevel(value.level),
|
|
10971
11070
|
ordinal: Math.max(0, Math.round(value.ordinal))
|
|
10972
|
-
}
|
|
11071
|
+
};
|
|
11072
|
+
if (value.postReduceGracePending === true)
|
|
11073
|
+
serialized.postReduceGracePending = true;
|
|
11074
|
+
if (value.postReduceGraceBaselineU !== undefined) {
|
|
11075
|
+
serialized.postReduceGraceBaselineU = Math.max(0, Math.round(value.postReduceGraceBaselineU));
|
|
11076
|
+
}
|
|
11077
|
+
if (value.postReduceGracePreLevel !== undefined) {
|
|
11078
|
+
serialized.postReduceGracePreLevel = normalizeLastNudgeLevel(value.postReduceGracePreLevel);
|
|
11079
|
+
}
|
|
11080
|
+
return JSON.stringify(serialized);
|
|
10973
11081
|
}
|
|
10974
11082
|
function getLastNudgeUndropped(db, sessionId) {
|
|
10975
11083
|
const result = db.prepare("SELECT last_nudge_undropped FROM session_meta WHERE session_id = ?").get(sessionId);
|
|
@@ -10991,20 +11099,36 @@ function setChannel1NudgeState(db, sessionId, value) {
|
|
|
10991
11099
|
db.prepare("UPDATE session_meta SET last_nudge_level = ? WHERE session_id = ?").run(serializeChannel1NudgeState(value), sessionId);
|
|
10992
11100
|
})();
|
|
10993
11101
|
}
|
|
10994
|
-
function
|
|
10995
|
-
db.transaction(() => {
|
|
11102
|
+
function markChannel1PostReduceGracePending(db, sessionId) {
|
|
11103
|
+
return db.transaction(() => {
|
|
10996
11104
|
ensureSessionMetaRow(db, sessionId);
|
|
10997
|
-
|
|
11105
|
+
const current = getChannel1NudgeState(db, sessionId);
|
|
11106
|
+
const next = {
|
|
11107
|
+
level: current.level,
|
|
11108
|
+
ordinal: current.ordinal,
|
|
11109
|
+
postReduceGracePending: true,
|
|
11110
|
+
postReduceGracePreLevel: current.level
|
|
11111
|
+
};
|
|
11112
|
+
db.prepare("UPDATE session_meta SET last_nudge_level = ? WHERE session_id = ?").run(serializeChannel1NudgeState(next), sessionId);
|
|
11113
|
+
return next;
|
|
10998
11114
|
})();
|
|
10999
11115
|
}
|
|
11000
|
-
function
|
|
11001
|
-
|
|
11002
|
-
db.transaction(() => {
|
|
11116
|
+
function captureChannel1PostReduceGraceBaseline(db, sessionId, measuredUndropped) {
|
|
11117
|
+
return db.transaction(() => {
|
|
11003
11118
|
ensureSessionMetaRow(db, sessionId);
|
|
11004
|
-
const
|
|
11005
|
-
|
|
11119
|
+
const current = getChannel1NudgeState(db, sessionId);
|
|
11120
|
+
if (current.postReduceGracePending !== true)
|
|
11121
|
+
return current;
|
|
11122
|
+
const baselineU = Math.max(0, Math.round(measuredUndropped));
|
|
11123
|
+
const next = {
|
|
11124
|
+
level: current.level,
|
|
11125
|
+
ordinal: current.ordinal,
|
|
11126
|
+
postReduceGraceBaselineU: baselineU,
|
|
11127
|
+
postReduceGracePreLevel: current.postReduceGracePreLevel ?? current.level
|
|
11128
|
+
};
|
|
11129
|
+
db.prepare("UPDATE session_meta SET last_nudge_level = ? WHERE session_id = ?").run(serializeChannel1NudgeState(next), sessionId);
|
|
11130
|
+
return next;
|
|
11006
11131
|
})();
|
|
11007
|
-
return changed;
|
|
11008
11132
|
}
|
|
11009
11133
|
function isChannel2StateRow(row) {
|
|
11010
11134
|
return typeof row === "object" && row !== null && typeof row.channel2_nudge_state === "string";
|
|
@@ -11535,6 +11659,33 @@ function addTrailingBlankDecisions(db, sessionId, additions, options) {
|
|
|
11535
11659
|
sessionLog(sessionId, `trailing_blank_decisions CAS: ${CAS_RETRY_LIMIT} retries exhausted`);
|
|
11536
11660
|
return false;
|
|
11537
11661
|
}
|
|
11662
|
+
function demoteTrailingBlankKeepDecisions(db, sessionId, messageIds) {
|
|
11663
|
+
const ids = new Set(messageIds);
|
|
11664
|
+
if (ids.size === 0)
|
|
11665
|
+
return [];
|
|
11666
|
+
ensureSessionMetaRow(db, sessionId);
|
|
11667
|
+
for (let attempt = 0;attempt < CAS_RETRY_LIMIT; attempt += 1) {
|
|
11668
|
+
const row = db.prepare("SELECT trailing_blank_decisions FROM session_meta WHERE session_id = ?").get(sessionId);
|
|
11669
|
+
const rawStored = row ? row.trailing_blank_decisions ?? null : null;
|
|
11670
|
+
const current = parseTrailingBlankDecisions(rawStored);
|
|
11671
|
+
const demotedIds = [];
|
|
11672
|
+
for (const id of ids) {
|
|
11673
|
+
const decision = current.get(id);
|
|
11674
|
+
if (decision === "keep" || decision?.startsWith("keep:") === true) {
|
|
11675
|
+
current.set(id, "strip");
|
|
11676
|
+
demotedIds.push(id);
|
|
11677
|
+
}
|
|
11678
|
+
}
|
|
11679
|
+
if (demotedIds.length === 0)
|
|
11680
|
+
return [];
|
|
11681
|
+
const nextBlob = JSON.stringify(Object.fromEntries(current));
|
|
11682
|
+
const result = db.prepare("UPDATE session_meta SET trailing_blank_decisions = ? WHERE session_id = ? AND trailing_blank_decisions IS ?").run(nextBlob, sessionId, rawStored);
|
|
11683
|
+
if (result.changes > 0)
|
|
11684
|
+
return demotedIds;
|
|
11685
|
+
}
|
|
11686
|
+
sessionLog(sessionId, `trailing_blank_decisions demotion CAS: ${CAS_RETRY_LIMIT} retries exhausted`);
|
|
11687
|
+
return null;
|
|
11688
|
+
}
|
|
11538
11689
|
function getStaleReduceStrippedIds(db, sessionId) {
|
|
11539
11690
|
const row = db.prepare("SELECT stale_reduce_stripped_ids FROM session_meta WHERE session_id = ?").get(sessionId);
|
|
11540
11691
|
return new Set(parseStrippedBlob(row?.stale_reduce_stripped_ids));
|
|
@@ -11877,6 +12028,7 @@ function markNoteChecked(db, noteId) {
|
|
|
11877
12028
|
// src/features/magic-context/storage-ops.ts
|
|
11878
12029
|
var queuePendingOpStatements = new WeakMap;
|
|
11879
12030
|
var getPendingOpsStatements = new WeakMap;
|
|
12031
|
+
var getPendingOpsCountStatements = new WeakMap;
|
|
11880
12032
|
var clearPendingOpsStatements = new WeakMap;
|
|
11881
12033
|
var removePendingOpStatements = new WeakMap;
|
|
11882
12034
|
function getQueuePendingOpStatement(db) {
|
|
@@ -11937,6 +12089,19 @@ function getPendingOps(db, sessionId) {
|
|
|
11937
12089
|
const rows = getPendingOpsStatement(db).all(sessionId).filter(isPendingOpRow);
|
|
11938
12090
|
return rows.map(toPendingOp).filter((op) => op !== null);
|
|
11939
12091
|
}
|
|
12092
|
+
function getPendingOpsCount(db, sessionId) {
|
|
12093
|
+
try {
|
|
12094
|
+
let statement = getPendingOpsCountStatements.get(db);
|
|
12095
|
+
if (!statement) {
|
|
12096
|
+
statement = db.prepare("SELECT COUNT(*) AS count FROM pending_ops WHERE session_id = ?");
|
|
12097
|
+
getPendingOpsCountStatements.set(db, statement);
|
|
12098
|
+
}
|
|
12099
|
+
const row = statement.get(sessionId);
|
|
12100
|
+
return typeof row?.count === "number" && Number.isFinite(row.count) ? row.count : null;
|
|
12101
|
+
} catch {
|
|
12102
|
+
return null;
|
|
12103
|
+
}
|
|
12104
|
+
}
|
|
11940
12105
|
function clearPendingOps(db, sessionId) {
|
|
11941
12106
|
getClearPendingOpsStatement(db).run(sessionId);
|
|
11942
12107
|
}
|
|
@@ -12976,7 +13141,8 @@ function measureTailHygiene(input) {
|
|
|
12976
13141
|
continue;
|
|
12977
13142
|
}
|
|
12978
13143
|
if (type === "text") {
|
|
12979
|
-
const
|
|
13144
|
+
const rawContent = firstString(part, ["text", "content"]);
|
|
13145
|
+
const content = rawContent === null ? null : stripChannel1ReminderSpans(rawContent);
|
|
12980
13146
|
if (!content || isDropSentinel(content)) {
|
|
12981
13147
|
parts.push(excludedSnapshot(`${key}\x00excluded`, part));
|
|
12982
13148
|
continue;
|
|
@@ -13126,7 +13292,8 @@ function refreshTailHygieneBaseline(input) {
|
|
|
13126
13292
|
evaluable: true,
|
|
13127
13293
|
generationInvalidated: false,
|
|
13128
13294
|
baselineParts: measured.parts,
|
|
13129
|
-
contentSignature: measured.contentSignature
|
|
13295
|
+
contentSignature: measured.contentSignature,
|
|
13296
|
+
channel1PostReduceGrace: input.previous?.channel1PostReduceGrace
|
|
13130
13297
|
};
|
|
13131
13298
|
}
|
|
13132
13299
|
const prefix = sameMeasuredPrefix(input.previous.baselineParts, measured.parts);
|
|
@@ -13188,47 +13355,74 @@ function decideChannel1(input) {
|
|
|
13188
13355
|
const tailTokens = Math.max(0, input.baselineT + input.turnDeltaT);
|
|
13189
13356
|
const undroppedTokens = Math.min(tailTokens, Math.max(0, input.baselineU + input.turnDeltaU));
|
|
13190
13357
|
const severity = Math.min(1, Math.max(0, undroppedTokens / Math.max(tailTokens, 1)));
|
|
13191
|
-
const
|
|
13192
|
-
|
|
13193
|
-
|
|
13194
|
-
|
|
13358
|
+
const previousLevel = input.lastNudgeLevel;
|
|
13359
|
+
let lastNudge = Math.max(0, input.lastNudgeUndropped);
|
|
13360
|
+
let nextLevel = previousLevel;
|
|
13361
|
+
let clearPostReduceGrace = false;
|
|
13362
|
+
const quiet = (level2 = "gentle") => ({
|
|
13195
13363
|
fire: false,
|
|
13196
|
-
|
|
13364
|
+
sticky: false,
|
|
13365
|
+
level: level2,
|
|
13197
13366
|
undroppedTokens,
|
|
13198
13367
|
tailTokens,
|
|
13199
13368
|
severity,
|
|
13200
13369
|
nextLastNudge: lastNudge,
|
|
13201
|
-
nextLastNudgeLevel:
|
|
13370
|
+
nextLastNudgeLevel: nextLevel,
|
|
13371
|
+
clearPostReduceGrace
|
|
13202
13372
|
});
|
|
13203
13373
|
if (input.evaluable === false || input.generationInvalidated === true)
|
|
13204
13374
|
return quiet();
|
|
13205
|
-
if (input.hasRecentReduce)
|
|
13206
|
-
return quiet();
|
|
13207
|
-
if (tailTokens < CHANNEL1_MIN_TOKENS)
|
|
13375
|
+
if (input.hasRecentReduce || input.postReduceGracePending)
|
|
13208
13376
|
return quiet();
|
|
13209
|
-
|
|
13210
|
-
|
|
13211
|
-
|
|
13212
|
-
|
|
13213
|
-
|
|
13214
|
-
|
|
13215
|
-
|
|
13216
|
-
|
|
13217
|
-
|
|
13218
|
-
|
|
13219
|
-
|
|
13220
|
-
const
|
|
13221
|
-
|
|
13222
|
-
|
|
13377
|
+
let level = "";
|
|
13378
|
+
if (tailTokens >= CHANNEL1_MIN_TOKENS && undroppedTokens >= CHANNEL1_FLOOR_TOKENS && severity >= S_GENTLE) {
|
|
13379
|
+
if (severity >= S_URGENT)
|
|
13380
|
+
level = "urgent";
|
|
13381
|
+
else if (severity >= S_FIRM)
|
|
13382
|
+
level = "firm";
|
|
13383
|
+
else
|
|
13384
|
+
level = "gentle";
|
|
13385
|
+
}
|
|
13386
|
+
const previousRank = previousLevel === "" ? 0 : LEVEL_RANK[previousLevel];
|
|
13387
|
+
const currentRank = level === "" ? 0 : LEVEL_RANK[level];
|
|
13388
|
+
const graceBaseline = input.postReduceGraceBaselineU;
|
|
13389
|
+
if (graceBaseline !== undefined) {
|
|
13390
|
+
const preReduceLevel = input.postReduceGracePreLevel ?? previousLevel;
|
|
13391
|
+
const preReduceRank = preReduceLevel === "" ? 0 : LEVEL_RANK[preReduceLevel];
|
|
13392
|
+
const regrowthReached = undroppedTokens - Math.max(0, graceBaseline) >= channel1RefireTokens(tailTokens);
|
|
13393
|
+
const escalatedAbovePreReduceBand = currentRank > preReduceRank;
|
|
13394
|
+
if (!regrowthReached && !escalatedAbovePreReduceBand) {
|
|
13395
|
+
return quiet(level === "" ? "gentle" : level);
|
|
13396
|
+
}
|
|
13397
|
+
clearPostReduceGrace = true;
|
|
13398
|
+
}
|
|
13399
|
+
if (level === "") {
|
|
13400
|
+
nextLevel = "";
|
|
13401
|
+
lastNudge = 0;
|
|
13223
13402
|
return quiet();
|
|
13403
|
+
}
|
|
13404
|
+
if (currentRank < previousRank) {
|
|
13405
|
+
nextLevel = level;
|
|
13406
|
+
lastNudge = undroppedTokens;
|
|
13407
|
+
return quiet(level);
|
|
13408
|
+
}
|
|
13409
|
+
const crossedFromBelow = currentRank > previousRank;
|
|
13410
|
+
const cadenceReached = currentRank === previousRank && undroppedTokens - lastNudge >= channel1RefireTokens(tailTokens);
|
|
13411
|
+
const currentTurn = input.currentRealUserTurnCount;
|
|
13412
|
+
const lastFireTurn = input.lastFireOrdinal;
|
|
13413
|
+
const stickyTurnGapReached = currentTurn === undefined || lastFireTurn === undefined || lastFireTurn > currentTurn || currentTurn - lastFireTurn >= CHANNEL1_STICKY_REAL_USER_TURN_GAP;
|
|
13414
|
+
if (!crossedFromBelow && (!cadenceReached || !stickyTurnGapReached))
|
|
13415
|
+
return quiet(level);
|
|
13224
13416
|
return {
|
|
13225
13417
|
fire: true,
|
|
13418
|
+
sticky: !crossedFromBelow,
|
|
13226
13419
|
level,
|
|
13227
13420
|
undroppedTokens,
|
|
13228
13421
|
tailTokens,
|
|
13229
13422
|
severity,
|
|
13230
13423
|
nextLastNudge: undroppedTokens,
|
|
13231
|
-
nextLastNudgeLevel: level
|
|
13424
|
+
nextLastNudgeLevel: level,
|
|
13425
|
+
clearPostReduceGrace
|
|
13232
13426
|
};
|
|
13233
13427
|
}
|
|
13234
13428
|
var CHANNEL2_SEVERITY_THRESHOLD = 0.75;
|
|
@@ -13265,44 +13459,42 @@ function formatOldestReclaimableHint(hint) {
|
|
|
13265
13459
|
return rendered.length > 0 ? `
|
|
13266
13460
|
oldest reclaimable: ${rendered}.` : "";
|
|
13267
13461
|
}
|
|
13268
|
-
function
|
|
13269
|
-
|
|
13270
|
-
|
|
13462
|
+
function reclaimableToolOutputCount(parts) {
|
|
13463
|
+
return parts.filter((part) => part.kind === "toolOutput" && part.uTokens > 0).length;
|
|
13464
|
+
}
|
|
13465
|
+
function formatReclaimableOutputSummary(count, tokens) {
|
|
13466
|
+
const outputCount = Math.max(0, Math.floor(count));
|
|
13467
|
+
const outputs = outputCount === 0 ? "spent tool outputs" : `${outputCount} spent tool output${outputCount === 1 ? "" : "s"}`;
|
|
13468
|
+
return `${outputs} (~${approxThousands(tokens)} tokens)`;
|
|
13469
|
+
}
|
|
13470
|
+
function buildChannel2Reminder(undroppedTokens, reclaimableToolOutputs, hint) {
|
|
13471
|
+
const summary = formatReclaimableOutputSummary(reclaimableToolOutputs, undroppedTokens);
|
|
13271
13472
|
const hintText = formatOldestReclaimableHint(hint);
|
|
13272
13473
|
return `<system-reminder>
|
|
13273
|
-
` + `Routine housekeeping:
|
|
13474
|
+
` + `Routine housekeeping: ${summary} are reclaimable — make a ctx_reduce pass at a natural stopping point.${hintText}
|
|
13274
13475
|
` + `</system-reminder>`;
|
|
13275
13476
|
}
|
|
13276
|
-
var CHANNEL1_STICKY_REAL_USER_TURN_GAP =
|
|
13277
|
-
function
|
|
13278
|
-
|
|
13279
|
-
return false;
|
|
13280
|
-
if (input.lastOrdinal > input.currentRealUserTurnCount)
|
|
13281
|
-
return false;
|
|
13282
|
-
const gap = input.currentRealUserTurnCount - input.lastOrdinal;
|
|
13283
|
-
return gap >= 0 && gap < CHANNEL1_STICKY_REAL_USER_TURN_GAP;
|
|
13284
|
-
}
|
|
13285
|
-
function buildChannel1Reminder(level, undroppedTokens, usableWindowTokens, hint, sticky = false) {
|
|
13477
|
+
var CHANNEL1_STICKY_REAL_USER_TURN_GAP = 5;
|
|
13478
|
+
function buildChannel1Reminder(level, undroppedTokens, reclaimableToolOutputs, hint, sticky = false) {
|
|
13479
|
+
const summary = formatReclaimableOutputSummary(reclaimableToolOutputs, undroppedTokens);
|
|
13286
13480
|
const hintText = formatOldestReclaimableHint(hint);
|
|
13287
13481
|
if (sticky) {
|
|
13288
13482
|
return `
|
|
13289
13483
|
|
|
13290
13484
|
<system-reminder>
|
|
13291
|
-
Reminder:
|
|
13485
|
+
Reminder: ${summary} are still reclaimable — ctx_reduce them at a natural stopping point.${hintText}
|
|
13292
13486
|
</system-reminder>`;
|
|
13293
13487
|
}
|
|
13294
|
-
const amount = approxThousands(undroppedTokens);
|
|
13295
|
-
const usableWindow = approxThousands(usableWindowTokens);
|
|
13296
13488
|
let body;
|
|
13297
13489
|
switch (level) {
|
|
13298
13490
|
case "gentle":
|
|
13299
|
-
body = `Housekeeping:
|
|
13491
|
+
body = `Housekeeping: ${summary} are reclaimable — drop the ones you have already processed with ctx_reduce at a natural stopping point.`;
|
|
13300
13492
|
break;
|
|
13301
13493
|
case "firm":
|
|
13302
|
-
body = `Housekeeping:
|
|
13494
|
+
body = `Housekeeping: ${summary} are reclaimable — make a ctx_reduce pass at a natural stopping point.`;
|
|
13303
13495
|
break;
|
|
13304
13496
|
case "urgent":
|
|
13305
|
-
body = `Housekeeping backlog:
|
|
13497
|
+
body = `Housekeeping backlog: ${summary} are reclaimable — a ctx_reduce pass is due.`;
|
|
13306
13498
|
break;
|
|
13307
13499
|
}
|
|
13308
13500
|
return `
|
|
@@ -13363,8 +13555,8 @@ function planEmergencyDrop(input) {
|
|
|
13363
13555
|
if (!Number.isFinite(currentTotalInputTokens) || currentTotalInputTokens <= 0) {
|
|
13364
13556
|
return noop("unknown-usage");
|
|
13365
13557
|
}
|
|
13366
|
-
if (hasPriorDrop
|
|
13367
|
-
return noop(
|
|
13558
|
+
if (hasPriorDrop) {
|
|
13559
|
+
return noop(`pressure-episode-latched (prior sample ${priorInputSample}; awaiting exit or independent bust)`);
|
|
13368
13560
|
}
|
|
13369
13561
|
let tailTokens = 0;
|
|
13370
13562
|
for (const tag of floorTags) {
|
|
@@ -13474,12 +13666,50 @@ function getUpdateTagDropModeStatement(db) {
|
|
|
13474
13666
|
var updateTagByteSizeStatements = new WeakMap;
|
|
13475
13667
|
var updateTagInputByteSizeStatements = new WeakMap;
|
|
13476
13668
|
var CONTENT_ID_SUFFIX = /:(?:p|file)\d+$/;
|
|
13669
|
+
var RECENT_OWNER_SCAN_PAGE_SIZE = 128;
|
|
13670
|
+
var recentTagOwnerStatements = new WeakMap;
|
|
13477
13671
|
function ownerMessageIdForTagRow(row) {
|
|
13478
13672
|
if (row.type === "tool") {
|
|
13479
13673
|
return row.tool_owner_message_id ?? row.message_id;
|
|
13480
13674
|
}
|
|
13481
13675
|
return row.message_id.replace(CONTENT_ID_SUFFIX, "");
|
|
13482
13676
|
}
|
|
13677
|
+
function getRecentTagOwnerStatement(db) {
|
|
13678
|
+
let statement = recentTagOwnerStatements.get(db);
|
|
13679
|
+
if (!statement) {
|
|
13680
|
+
statement = db.prepare(`SELECT type, message_id, tool_owner_message_id
|
|
13681
|
+
FROM tags
|
|
13682
|
+
WHERE session_id = ?
|
|
13683
|
+
ORDER BY tag_number DESC, id DESC
|
|
13684
|
+
LIMIT ? OFFSET ?`);
|
|
13685
|
+
recentTagOwnerStatements.set(db, statement);
|
|
13686
|
+
}
|
|
13687
|
+
return statement;
|
|
13688
|
+
}
|
|
13689
|
+
function getRecentTagOwnerMessageIds(db, sessionId, maxOwners) {
|
|
13690
|
+
const recent = new Set;
|
|
13691
|
+
if (!Number.isFinite(maxOwners) || maxOwners <= 0)
|
|
13692
|
+
return recent;
|
|
13693
|
+
const statement = getRecentTagOwnerStatement(db);
|
|
13694
|
+
let offset = 0;
|
|
13695
|
+
while (recent.size < maxOwners) {
|
|
13696
|
+
const rows = statement.all(sessionId, RECENT_OWNER_SCAN_PAGE_SIZE, offset);
|
|
13697
|
+
for (const row of rows) {
|
|
13698
|
+
if (typeof row.type !== "string" || typeof row.message_id !== "string")
|
|
13699
|
+
continue;
|
|
13700
|
+
const ownerId = row.type === "tool" ? typeof row.tool_owner_message_id === "string" ? row.tool_owner_message_id : null : row.message_id.replace(CONTENT_ID_SUFFIX, "");
|
|
13701
|
+
if (!ownerId || recent.has(ownerId))
|
|
13702
|
+
continue;
|
|
13703
|
+
recent.add(ownerId);
|
|
13704
|
+
if (recent.size >= maxOwners)
|
|
13705
|
+
break;
|
|
13706
|
+
}
|
|
13707
|
+
if (rows.length < RECENT_OWNER_SCAN_PAGE_SIZE)
|
|
13708
|
+
break;
|
|
13709
|
+
offset += rows.length;
|
|
13710
|
+
}
|
|
13711
|
+
return recent;
|
|
13712
|
+
}
|
|
13483
13713
|
var RECLAIM_HINT_EXCLUDED_TOOLS = new Set([
|
|
13484
13714
|
"ask",
|
|
13485
13715
|
"bash_kill",
|
|
@@ -14603,15 +14833,15 @@ import {
|
|
|
14603
14833
|
unlinkSync as unlinkSync2,
|
|
14604
14834
|
writeFileSync
|
|
14605
14835
|
} from "node:fs";
|
|
14606
|
-
import { homedir as
|
|
14836
|
+
import { homedir as homedir3 } from "node:os";
|
|
14607
14837
|
import { basename as basename2, dirname as dirname3, isAbsolute, join as join5 } from "node:path";
|
|
14608
14838
|
var CONFIG_FILE_BASENAME = "magic-context";
|
|
14609
14839
|
var MOVED_MARKER_SUFFIX = ".MOVED_READPLEASE";
|
|
14610
14840
|
function homeDir() {
|
|
14611
14841
|
if (process.platform === "win32") {
|
|
14612
|
-
return process.env.USERPROFILE || process.env.HOME ||
|
|
14842
|
+
return process.env.USERPROFILE || process.env.HOME || homedir3();
|
|
14613
14843
|
}
|
|
14614
|
-
return process.env.HOME ||
|
|
14844
|
+
return process.env.HOME || homedir3();
|
|
14615
14845
|
}
|
|
14616
14846
|
function configHome() {
|
|
14617
14847
|
const xdg = process.env.XDG_CONFIG_HOME;
|
|
@@ -15592,6 +15822,7 @@ var memoryImportanceColumnCache = new WeakMap;
|
|
|
15592
15822
|
var memoryScopeColumnCache = new WeakMap;
|
|
15593
15823
|
var memoryShareableColumnCache = new WeakMap;
|
|
15594
15824
|
var memoryClassifiedAtColumnCache = new WeakMap;
|
|
15825
|
+
var memoryVerificationsTableCache = new WeakMap;
|
|
15595
15826
|
function hasMemoryImportanceColumn(db) {
|
|
15596
15827
|
const cached = memoryImportanceColumnCache.get(db);
|
|
15597
15828
|
if (cached !== undefined)
|
|
@@ -17045,39 +17276,76 @@ function mapBackfillCandidateRows(rows) {
|
|
|
17045
17276
|
title: row.title
|
|
17046
17277
|
}));
|
|
17047
17278
|
}
|
|
17048
|
-
|
|
17049
|
-
|
|
17050
|
-
|
|
17051
|
-
|
|
17052
|
-
|
|
17053
|
-
|
|
17054
|
-
|
|
17055
|
-
|
|
17056
|
-
c.title AS title
|
|
17057
|
-
FROM compartments c
|
|
17058
|
-
JOIN session_projects sp
|
|
17059
|
-
ON sp.session_id = c.session_id
|
|
17060
|
-
AND sp.harness = c.harness
|
|
17061
|
-
AND sp.project_path = ?
|
|
17062
|
-
WHERE c.session_id = ?
|
|
17063
|
-
AND c.start_message IS NOT NULL
|
|
17064
|
-
AND c.end_message IS NOT NULL
|
|
17065
|
-
AND c.id NOT IN (${placeholders3})
|
|
17066
|
-
AND NOT EXISTS (
|
|
17067
|
-
SELECT 1
|
|
17068
|
-
FROM compartment_chunk_embeddings current
|
|
17069
|
-
WHERE current.compartment_id = c.id
|
|
17070
|
-
AND current.project_path = ?
|
|
17071
|
-
AND current.model_id = ?
|
|
17072
|
-
)
|
|
17073
|
-
ORDER BY c.start_message ASC, c.id ASC
|
|
17074
|
-
LIMIT ?`);
|
|
17075
|
-
const rows2 = stmt2.all(projectPath, sessionId, ...excludeIds, projectPath, modelId, Math.max(1, limit));
|
|
17076
|
-
return mapBackfillCandidateRows(rows2);
|
|
17279
|
+
function classifyChunkCoverageDefect(db, projectPath, modelId, candidate, maxInputTokens) {
|
|
17280
|
+
const canonicalText = buildCanonicalChunkTextFromFts(db, candidate.sessionId, candidate.startMessage, candidate.endMessage) || buildCompartmentSummaryFallbackText(db, candidate.id);
|
|
17281
|
+
const windows = chunkCanonicalText(canonicalText, candidate.startMessage, candidate.endMessage, maxInputTokens);
|
|
17282
|
+
const existing = getExistingChunkHashes(db, candidate.id, modelId, projectPath);
|
|
17283
|
+
if (windows.some((window) => !existing.has(window.windowIndex)))
|
|
17284
|
+
return "missing";
|
|
17285
|
+
if (existing.size !== windows.length || windows.some((window) => existing.get(window.windowIndex) !== window.chunkHash)) {
|
|
17286
|
+
return "stale";
|
|
17077
17287
|
}
|
|
17078
|
-
|
|
17079
|
-
|
|
17080
|
-
|
|
17288
|
+
return null;
|
|
17289
|
+
}
|
|
17290
|
+
function selectHashIncompleteChunkCandidates(db, projectPath, modelId, candidates, limit, maxInputTokens) {
|
|
17291
|
+
const missing = [];
|
|
17292
|
+
const stale = [];
|
|
17293
|
+
for (const candidate of candidates) {
|
|
17294
|
+
const defect = classifyChunkCoverageDefect(db, projectPath, modelId, candidate, maxInputTokens);
|
|
17295
|
+
if (defect === "missing")
|
|
17296
|
+
missing.push(candidate);
|
|
17297
|
+
else if (defect === "stale")
|
|
17298
|
+
stale.push(candidate);
|
|
17299
|
+
}
|
|
17300
|
+
return [...missing, ...stale].slice(0, limit);
|
|
17301
|
+
}
|
|
17302
|
+
var sessionBackfillCandidateStatements = new WeakMap;
|
|
17303
|
+
function loadUnembeddedSessionChunkCandidates(db, projectPath, sessionId, modelId, limit, excludeIds, maxInputTokens = DEFAULT_COMPARTMENT_CHUNK_MAX_INPUT_TOKENS) {
|
|
17304
|
+
const exclusions = excludeIds && excludeIds.length > 0 ? excludeIds : [];
|
|
17305
|
+
const exclusionSql = exclusions.length > 0 ? `AND c.id NOT IN (${exclusions.map(() => "?").join(", ")})` : "";
|
|
17306
|
+
const stmt = exclusions.length > 0 ? db.prepare(`SELECT c.id AS id,
|
|
17307
|
+
c.session_id AS sessionId,
|
|
17308
|
+
c.start_message AS startMessage,
|
|
17309
|
+
c.end_message AS endMessage,
|
|
17310
|
+
c.title AS title
|
|
17311
|
+
FROM compartments c
|
|
17312
|
+
JOIN session_projects sp
|
|
17313
|
+
ON sp.session_id = c.session_id
|
|
17314
|
+
AND sp.harness = c.harness
|
|
17315
|
+
AND sp.project_path = ?
|
|
17316
|
+
WHERE c.session_id = ?
|
|
17317
|
+
AND c.start_message IS NOT NULL
|
|
17318
|
+
AND c.end_message IS NOT NULL
|
|
17319
|
+
${exclusionSql}
|
|
17320
|
+
ORDER BY c.start_message ASC, c.id ASC`) : (() => {
|
|
17321
|
+
let cached = sessionBackfillCandidateStatements.get(db);
|
|
17322
|
+
if (!cached) {
|
|
17323
|
+
cached = db.prepare(`SELECT c.id AS id,
|
|
17324
|
+
c.session_id AS sessionId,
|
|
17325
|
+
c.start_message AS startMessage,
|
|
17326
|
+
c.end_message AS endMessage,
|
|
17327
|
+
c.title AS title
|
|
17328
|
+
FROM compartments c
|
|
17329
|
+
JOIN session_projects sp
|
|
17330
|
+
ON sp.session_id = c.session_id
|
|
17331
|
+
AND sp.harness = c.harness
|
|
17332
|
+
AND sp.project_path = ?
|
|
17333
|
+
WHERE c.session_id = ?
|
|
17334
|
+
AND c.start_message IS NOT NULL
|
|
17335
|
+
AND c.end_message IS NOT NULL
|
|
17336
|
+
ORDER BY c.start_message ASC, c.id ASC`);
|
|
17337
|
+
sessionBackfillCandidateStatements.set(db, cached);
|
|
17338
|
+
}
|
|
17339
|
+
return cached;
|
|
17340
|
+
})();
|
|
17341
|
+
const rows = stmt.all(projectPath, sessionId, ...exclusions);
|
|
17342
|
+
return selectHashIncompleteChunkCandidates(db, projectPath, modelId, mapBackfillCandidateRows(rows), Math.max(1, limit), maxInputTokens);
|
|
17343
|
+
}
|
|
17344
|
+
function countUnembeddedSessionCompartments(db, projectPath, sessionId, modelId, maxInputTokens = DEFAULT_COMPARTMENT_CHUNK_MAX_INPUT_TOKENS) {
|
|
17345
|
+
return loadUnembeddedSessionChunkCandidates(db, projectPath, sessionId, modelId, Number.MAX_SAFE_INTEGER, undefined, maxInputTokens).length;
|
|
17346
|
+
}
|
|
17347
|
+
function countSessionCompartmentEmbedCoverage(db, projectPath, sessionId, modelId, maxInputTokens = DEFAULT_COMPARTMENT_CHUNK_MAX_INPUT_TOKENS) {
|
|
17348
|
+
const rows = db.prepare(`SELECT c.id AS id,
|
|
17081
17349
|
c.session_id AS sessionId,
|
|
17082
17350
|
c.start_message AS startMessage,
|
|
17083
17351
|
c.end_message AS endMessage,
|
|
@@ -17090,67 +17358,22 @@ function loadUnembeddedSessionChunkCandidates(db, projectPath, sessionId, modelI
|
|
|
17090
17358
|
WHERE c.session_id = ?
|
|
17091
17359
|
AND c.start_message IS NOT NULL
|
|
17092
17360
|
AND c.end_message IS NOT NULL
|
|
17093
|
-
|
|
17094
|
-
|
|
17095
|
-
|
|
17096
|
-
|
|
17097
|
-
|
|
17098
|
-
|
|
17099
|
-
|
|
17100
|
-
ORDER BY c.start_message ASC, c.id ASC
|
|
17101
|
-
LIMIT ?`);
|
|
17102
|
-
sessionBackfillCandidateStatements.set(db, stmt);
|
|
17361
|
+
ORDER BY c.start_message ASC, c.id ASC`).all(projectPath, sessionId);
|
|
17362
|
+
const candidates = mapBackfillCandidateRows(rows);
|
|
17363
|
+
let embedded = 0;
|
|
17364
|
+
for (const candidate of candidates) {
|
|
17365
|
+
if (classifyChunkCoverageDefect(db, projectPath, modelId, candidate, maxInputTokens) === null) {
|
|
17366
|
+
embedded += 1;
|
|
17367
|
+
}
|
|
17103
17368
|
}
|
|
17104
|
-
|
|
17105
|
-
return mapBackfillCandidateRows(rows);
|
|
17106
|
-
}
|
|
17107
|
-
function countUnembeddedSessionCompartments(db, projectPath, sessionId, modelId) {
|
|
17108
|
-
const row = db.prepare(`SELECT COUNT(*) AS n
|
|
17109
|
-
FROM compartments c
|
|
17110
|
-
JOIN session_projects sp
|
|
17111
|
-
ON sp.session_id = c.session_id
|
|
17112
|
-
AND sp.harness = c.harness
|
|
17113
|
-
AND sp.project_path = ?
|
|
17114
|
-
WHERE c.session_id = ?
|
|
17115
|
-
AND c.start_message IS NOT NULL
|
|
17116
|
-
AND c.end_message IS NOT NULL
|
|
17117
|
-
AND NOT EXISTS (
|
|
17118
|
-
SELECT 1
|
|
17119
|
-
FROM compartment_chunk_embeddings current
|
|
17120
|
-
WHERE current.compartment_id = c.id
|
|
17121
|
-
AND current.project_path = ?
|
|
17122
|
-
AND current.model_id = ?
|
|
17123
|
-
)`).get(projectPath, sessionId, projectPath, modelId);
|
|
17124
|
-
return typeof row?.n === "number" ? row.n : 0;
|
|
17125
|
-
}
|
|
17126
|
-
function countSessionCompartmentEmbedCoverage(db, projectPath, sessionId, modelId) {
|
|
17127
|
-
const row = db.prepare(`SELECT
|
|
17128
|
-
COUNT(*) AS total,
|
|
17129
|
-
SUM(CASE WHEN EXISTS (
|
|
17130
|
-
SELECT 1 FROM compartment_chunk_embeddings e
|
|
17131
|
-
WHERE e.compartment_id = c.id
|
|
17132
|
-
AND e.project_path = ?
|
|
17133
|
-
AND e.model_id = ?
|
|
17134
|
-
) THEN 1 ELSE 0 END) AS embedded
|
|
17135
|
-
FROM compartments c
|
|
17136
|
-
JOIN session_projects sp
|
|
17137
|
-
ON sp.session_id = c.session_id
|
|
17138
|
-
AND sp.harness = c.harness
|
|
17139
|
-
AND sp.project_path = ?
|
|
17140
|
-
WHERE c.session_id = ?
|
|
17141
|
-
AND c.start_message IS NOT NULL
|
|
17142
|
-
AND c.end_message IS NOT NULL`).get(projectPath, modelId, projectPath, sessionId);
|
|
17143
|
-
return {
|
|
17144
|
-
total: typeof row?.total === "number" ? row.total : 0,
|
|
17145
|
-
embedded: typeof row?.embedded === "number" ? row.embedded : 0
|
|
17146
|
-
};
|
|
17369
|
+
return { embedded, total: candidates.length };
|
|
17147
17370
|
}
|
|
17148
17371
|
|
|
17149
17372
|
// src/features/magic-context/project-embedding-registry.ts
|
|
17150
17373
|
import { createHash as createHash14, randomUUID as randomUUID2 } from "node:crypto";
|
|
17151
17374
|
|
|
17152
17375
|
// src/config/schema/magic-context.ts
|
|
17153
|
-
import { homedir as
|
|
17376
|
+
import { homedir as homedir4 } from "node:os";
|
|
17154
17377
|
|
|
17155
17378
|
// ../../node_modules/.bun/zod@4.4.3/node_modules/zod/v4/classic/external.js
|
|
17156
17379
|
var exports_external = {};
|
|
@@ -17777,7 +18000,7 @@ __export(exports_util, {
|
|
|
17777
18000
|
createTransparentProxy: () => createTransparentProxy,
|
|
17778
18001
|
defineLazy: () => defineLazy,
|
|
17779
18002
|
esc: () => esc,
|
|
17780
|
-
escapeRegex: () =>
|
|
18003
|
+
escapeRegex: () => escapeRegex,
|
|
17781
18004
|
explicitlyAborted: () => explicitlyAborted,
|
|
17782
18005
|
extend: () => extend,
|
|
17783
18006
|
finalizeIssue: () => finalizeIssue,
|
|
@@ -18054,7 +18277,7 @@ var primitiveTypes = /* @__PURE__ */ new Set([
|
|
|
18054
18277
|
"symbol",
|
|
18055
18278
|
"undefined"
|
|
18056
18279
|
]);
|
|
18057
|
-
function
|
|
18280
|
+
function escapeRegex(str) {
|
|
18058
18281
|
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
18059
18282
|
}
|
|
18060
18283
|
function clone(inst, def, params) {
|
|
@@ -18764,7 +18987,7 @@ function emoji() {
|
|
|
18764
18987
|
var ipv4 = /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/;
|
|
18765
18988
|
var ipv6 = /^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:))$/;
|
|
18766
18989
|
var mac = (delimiter) => {
|
|
18767
|
-
const escapedDelim =
|
|
18990
|
+
const escapedDelim = escapeRegex(delimiter ?? ":");
|
|
18768
18991
|
return new RegExp(`^(?:[0-9A-F]{2}${escapedDelim}){5}[0-9A-F]{2}$|^(?:[0-9a-f]{2}${escapedDelim}){5}[0-9a-f]{2}$`);
|
|
18769
18992
|
};
|
|
18770
18993
|
var cidrv4 = /^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\/([0-9]|[1-2][0-9]|3[0-2])$/;
|
|
@@ -19265,7 +19488,7 @@ var $ZodCheckUpperCase = /* @__PURE__ */ $constructor("$ZodCheckUpperCase", (ins
|
|
|
19265
19488
|
});
|
|
19266
19489
|
var $ZodCheckIncludes = /* @__PURE__ */ $constructor("$ZodCheckIncludes", (inst, def) => {
|
|
19267
19490
|
$ZodCheck.init(inst, def);
|
|
19268
|
-
const escapedRegex =
|
|
19491
|
+
const escapedRegex = escapeRegex(def.includes);
|
|
19269
19492
|
const pattern = new RegExp(typeof def.position === "number" ? `^.{${def.position}}${escapedRegex}` : escapedRegex);
|
|
19270
19493
|
def.pattern = pattern;
|
|
19271
19494
|
inst._zod.onattach.push((inst2) => {
|
|
@@ -19289,7 +19512,7 @@ var $ZodCheckIncludes = /* @__PURE__ */ $constructor("$ZodCheckIncludes", (inst,
|
|
|
19289
19512
|
});
|
|
19290
19513
|
var $ZodCheckStartsWith = /* @__PURE__ */ $constructor("$ZodCheckStartsWith", (inst, def) => {
|
|
19291
19514
|
$ZodCheck.init(inst, def);
|
|
19292
|
-
const pattern = new RegExp(`^${
|
|
19515
|
+
const pattern = new RegExp(`^${escapeRegex(def.prefix)}.*`);
|
|
19293
19516
|
def.pattern ?? (def.pattern = pattern);
|
|
19294
19517
|
inst._zod.onattach.push((inst2) => {
|
|
19295
19518
|
const bag = inst2._zod.bag;
|
|
@@ -19312,7 +19535,7 @@ var $ZodCheckStartsWith = /* @__PURE__ */ $constructor("$ZodCheckStartsWith", (i
|
|
|
19312
19535
|
});
|
|
19313
19536
|
var $ZodCheckEndsWith = /* @__PURE__ */ $constructor("$ZodCheckEndsWith", (inst, def) => {
|
|
19314
19537
|
$ZodCheck.init(inst, def);
|
|
19315
|
-
const pattern = new RegExp(`.*${
|
|
19538
|
+
const pattern = new RegExp(`.*${escapeRegex(def.suffix)}$`);
|
|
19316
19539
|
def.pattern ?? (def.pattern = pattern);
|
|
19317
19540
|
inst._zod.onattach.push((inst2) => {
|
|
19318
19541
|
const bag = inst2._zod.bag;
|
|
@@ -20959,7 +21182,7 @@ var $ZodEnum = /* @__PURE__ */ $constructor("$ZodEnum", (inst, def) => {
|
|
|
20959
21182
|
const values = getEnumValues(def.entries);
|
|
20960
21183
|
const valuesSet = new Set(values);
|
|
20961
21184
|
inst._zod.values = valuesSet;
|
|
20962
|
-
inst._zod.pattern = new RegExp(`^(${values.filter((k) => propertyKeyTypes.has(typeof k)).map((o) => typeof o === "string" ?
|
|
21185
|
+
inst._zod.pattern = new RegExp(`^(${values.filter((k) => propertyKeyTypes.has(typeof k)).map((o) => typeof o === "string" ? escapeRegex(o) : o.toString()).join("|")})$`);
|
|
20963
21186
|
inst._zod.parse = (payload, _ctx) => {
|
|
20964
21187
|
const input = payload.value;
|
|
20965
21188
|
if (valuesSet.has(input)) {
|
|
@@ -20981,7 +21204,7 @@ var $ZodLiteral = /* @__PURE__ */ $constructor("$ZodLiteral", (inst, def) => {
|
|
|
20981
21204
|
}
|
|
20982
21205
|
const values = new Set(def.values);
|
|
20983
21206
|
inst._zod.values = values;
|
|
20984
|
-
inst._zod.pattern = new RegExp(`^(${def.values.map((o) => typeof o === "string" ?
|
|
21207
|
+
inst._zod.pattern = new RegExp(`^(${def.values.map((o) => typeof o === "string" ? escapeRegex(o) : o ? escapeRegex(o.toString()) : String(o)).join("|")})$`);
|
|
20985
21208
|
inst._zod.parse = (payload, _ctx) => {
|
|
20986
21209
|
const input = payload.value;
|
|
20987
21210
|
if (values.has(input)) {
|
|
@@ -21346,7 +21569,7 @@ var $ZodTemplateLiteral = /* @__PURE__ */ $constructor("$ZodTemplateLiteral", (i
|
|
|
21346
21569
|
const end = source.endsWith("$") ? source.length - 1 : source.length;
|
|
21347
21570
|
regexParts.push(source.slice(start, end));
|
|
21348
21571
|
} else if (part === null || primitiveTypes.has(typeof part)) {
|
|
21349
|
-
regexParts.push(
|
|
21572
|
+
regexParts.push(escapeRegex(`${part}`));
|
|
21350
21573
|
} else {
|
|
21351
21574
|
throw new Error(`Invalid template literal part: ${part}`);
|
|
21352
21575
|
}
|
|
@@ -31833,9 +32056,9 @@ var EmbeddingFallbackProviderSchema = exports_external.enum(["local", "openai-co
|
|
|
31833
32056
|
function expandConfigPath(value) {
|
|
31834
32057
|
const trimmed = value.trim();
|
|
31835
32058
|
if (trimmed === "~")
|
|
31836
|
-
return
|
|
32059
|
+
return homedir4();
|
|
31837
32060
|
if (trimmed.startsWith("~/"))
|
|
31838
|
-
return `${
|
|
32061
|
+
return `${homedir4()}/${trimmed.slice(2)}`;
|
|
31839
32062
|
return trimmed;
|
|
31840
32063
|
}
|
|
31841
32064
|
var BaseEmbeddingConfigSchema = exports_external.object({
|
|
@@ -31848,6 +32071,7 @@ var BaseEmbeddingConfigSchema = exports_external.object({
|
|
|
31848
32071
|
query_input_type: exports_external.string().optional().describe("Optional input_type for query (search) embeddings on asymmetric models (e.g. NVIDIA NIM 'query'). When unset, query embeddings use embedding.input_type. Passage/stored content always uses embedding.input_type."),
|
|
31849
32072
|
truncate: exports_external.string().optional().describe("Optional truncate mode sent in the embedding request body (e.g. NVIDIA NIM accepts 'NONE' | 'START' | 'END'). Omitted from the request when unset."),
|
|
31850
32073
|
max_input_tokens: exports_external.number().int().positive().optional().describe("Optional maximum input tokens for chunk embeddings. Defaults conservatively to 512 when omitted."),
|
|
32074
|
+
local_runtime: exports_external.enum(["auto", "native", "wasm"]).default("auto").describe("Local provider only: ONNX runtime selection. 'auto' uses native under Node and uses WASM under Bun versions before 1.4.0, where Bun's NAPI teardown race can panic on quit; native is restored automatically on Bun 1.4.0+. Set 'native' only to prefer speed while accepting that pre-1.4.0 Bun crash risk, or 'wasm' to avoid loading the native addon."),
|
|
31851
32075
|
local_dtype: exports_external.enum([
|
|
31852
32076
|
"auto",
|
|
31853
32077
|
"fp32",
|
|
@@ -31904,6 +32128,7 @@ var EmbeddingConfigSchema = BaseEmbeddingConfigSchema.transform((data) => {
|
|
|
31904
32128
|
return {
|
|
31905
32129
|
provider: "local",
|
|
31906
32130
|
model: data.model?.trim() || DEFAULT_LOCAL_EMBEDDING_MODEL,
|
|
32131
|
+
local_runtime: data.local_runtime,
|
|
31907
32132
|
...data.max_input_tokens ? { max_input_tokens: data.max_input_tokens } : {},
|
|
31908
32133
|
...data.local_dtype ? { local_dtype: data.local_dtype } : {}
|
|
31909
32134
|
};
|
|
@@ -31984,7 +32209,8 @@ var MagicContextConfigSchema = exports_external.object({
|
|
|
31984
32209
|
}).default({ enforce_private_permissions: true }).describe("Storage permission policy. The default keeps session content and memories owner-private. Disabling enforcement is for trusted shared-group storage managed externally; every group member able to read the storage can read all stored session content and memories."),
|
|
31985
32210
|
embedding: EmbeddingConfigSchema.default({
|
|
31986
32211
|
provider: "local",
|
|
31987
|
-
model: DEFAULT_LOCAL_EMBEDDING_MODEL
|
|
32212
|
+
model: DEFAULT_LOCAL_EMBEDDING_MODEL,
|
|
32213
|
+
local_runtime: "auto"
|
|
31988
32214
|
}).describe("Embedding provider configuration"),
|
|
31989
32215
|
subc: exports_external.object({
|
|
31990
32216
|
connection_file: exports_external.string().trim().min(1).transform(expandConfigPath).describe("Path to the owner-only subc connection file.")
|
|
@@ -33726,21 +33952,28 @@ var SYNAPSE_DEFAULT_MODEL = "gte-modernbert-base-f16";
|
|
|
33726
33952
|
var SYNAPSE_MAX_INPUT_TOKENS = 8192;
|
|
33727
33953
|
var SYNAPSE_DEFAULT_QUERY_TIMEOUT_MS = 3000;
|
|
33728
33954
|
var SYNAPSE_DEFAULT_BATCH_TIMEOUT_MS = 120000;
|
|
33955
|
+
var SYNAPSE_CERTIFICATION_REFUSAL_REASONS = new Set([
|
|
33956
|
+
"not_certified",
|
|
33957
|
+
"probe_required",
|
|
33958
|
+
"migration_required"
|
|
33959
|
+
]);
|
|
33729
33960
|
|
|
33730
33961
|
class SynapseEmbeddingError extends Error {
|
|
33731
33962
|
code;
|
|
33732
33963
|
retryAfterMs;
|
|
33733
33964
|
permanent;
|
|
33965
|
+
refusalReason;
|
|
33734
33966
|
constructor(code, message, options) {
|
|
33735
33967
|
super(message, options?.cause === undefined ? undefined : { cause: options.cause });
|
|
33736
33968
|
this.name = "SynapseEmbeddingError";
|
|
33737
33969
|
this.code = code;
|
|
33738
33970
|
this.permanent = options?.permanent ?? isPermanentSynapseCode(code);
|
|
33739
33971
|
this.retryAfterMs = options?.retryAfterMs ?? (this.permanent ? undefined : 100);
|
|
33972
|
+
this.refusalReason = options?.refusalReason;
|
|
33740
33973
|
}
|
|
33741
33974
|
}
|
|
33742
33975
|
function isPermanentSynapseCode(code) {
|
|
33743
|
-
return code === "artifact_invalid" || code === "substitution_rejected" || code === "not_certified" || code === "probe_required" || code === "idempotency_conflict" || code === "schema_violation";
|
|
33976
|
+
return code === "artifact_invalid" || code === "substitution_rejected" || code === "not_certified" || code === "probe_required" || code === "idempotency_conflict" || code === "schema_violation" || code === "certification_refused" || code === "needs_reauth";
|
|
33744
33977
|
}
|
|
33745
33978
|
function asRecord(value) {
|
|
33746
33979
|
return value !== null && typeof value === "object" ? value : null;
|
|
@@ -33766,11 +33999,21 @@ function readErrorCode(value) {
|
|
|
33766
33999
|
}
|
|
33767
34000
|
return;
|
|
33768
34001
|
}
|
|
34002
|
+
function readCertificationRefusalReason(value) {
|
|
34003
|
+
if (!(value instanceof SubcCallError))
|
|
34004
|
+
return;
|
|
34005
|
+
const detail = value.detail;
|
|
34006
|
+
if (typeof detail === "string")
|
|
34007
|
+
return detail;
|
|
34008
|
+
const record2 = asRecord(detail);
|
|
34009
|
+
return typeof record2?.reason === "string" ? record2.reason : undefined;
|
|
34010
|
+
}
|
|
33769
34011
|
function classifyError(value) {
|
|
33770
34012
|
if (value instanceof SynapseEmbeddingError)
|
|
33771
34013
|
return value;
|
|
33772
34014
|
const code = readErrorCode(value) ?? (value instanceof Error ? value.name : "transport");
|
|
33773
34015
|
const normalized = code.toLowerCase();
|
|
34016
|
+
const refusalReason = readCertificationRefusalReason(value);
|
|
33774
34017
|
let mapped = "transport";
|
|
33775
34018
|
if (normalized.includes("queue_full"))
|
|
33776
34019
|
mapped = "queue_full";
|
|
@@ -33782,12 +34025,16 @@ function classifyError(value) {
|
|
|
33782
34025
|
mapped = "artifact_invalid";
|
|
33783
34026
|
else if (normalized.includes("substitution"))
|
|
33784
34027
|
mapped = "substitution_rejected";
|
|
34028
|
+
else if (normalized === "certification_refused")
|
|
34029
|
+
mapped = "certification_refused";
|
|
33785
34030
|
else if (normalized.includes("not_certified"))
|
|
33786
34031
|
mapped = "not_certified";
|
|
33787
34032
|
else if (normalized.includes("probe_required"))
|
|
33788
34033
|
mapped = "probe_required";
|
|
33789
34034
|
else if (normalized.includes("idempotency_conflict"))
|
|
33790
34035
|
mapped = "idempotency_conflict";
|
|
34036
|
+
else if (normalized.includes("needs_reauth"))
|
|
34037
|
+
mapped = "needs_reauth";
|
|
33791
34038
|
else if (normalized.includes("schema"))
|
|
33792
34039
|
mapped = "schema_violation";
|
|
33793
34040
|
else if (normalized.includes("module_restarted") || normalized.includes("module restarted"))
|
|
@@ -33795,9 +34042,41 @@ function classifyError(value) {
|
|
|
33795
34042
|
const message = value instanceof Error ? value.message : String(value);
|
|
33796
34043
|
return new SynapseEmbeddingError(mapped, message, {
|
|
33797
34044
|
retryAfterMs: readRetryAfter(value) ?? (isPermanentSynapseCode(mapped) ? undefined : 100),
|
|
33798
|
-
cause: value
|
|
34045
|
+
cause: value,
|
|
34046
|
+
refusalReason
|
|
33799
34047
|
});
|
|
33800
34048
|
}
|
|
34049
|
+
function embeddingFailureFor(error51) {
|
|
34050
|
+
const typedReason = error51.refusalReason;
|
|
34051
|
+
if (typedReason !== undefined && SYNAPSE_CERTIFICATION_REFUSAL_REASONS.has(typedReason) || error51.code === "not_certified" || error51.code === "probe_required") {
|
|
34052
|
+
return {
|
|
34053
|
+
class: "certification_refusal",
|
|
34054
|
+
reason: `SYNAPSE certification refused embedding: ${typedReason ?? error51.code}`,
|
|
34055
|
+
retryable: false
|
|
34056
|
+
};
|
|
34057
|
+
}
|
|
34058
|
+
if (error51.code === "certification_refused") {
|
|
34059
|
+
return {
|
|
34060
|
+
class: "certification_refusal",
|
|
34061
|
+
reason: `SYNAPSE certification refused embedding: ${typedReason ?? "unknown reason"}`,
|
|
34062
|
+
retryable: false
|
|
34063
|
+
};
|
|
34064
|
+
}
|
|
34065
|
+
if (typedReason === "needs_reauth" || typedReason === "needs_reauth_expired") {
|
|
34066
|
+
return {
|
|
34067
|
+
class: "credential_required",
|
|
34068
|
+
reason: "SYNAPSE requires reauthentication",
|
|
34069
|
+
retryable: false
|
|
34070
|
+
};
|
|
34071
|
+
}
|
|
34072
|
+
if (error51.code === "substitution_rejected") {
|
|
34073
|
+
return { class: "substitution_rejected", reason: error51.message, retryable: false };
|
|
34074
|
+
}
|
|
34075
|
+
if (error51.code === "schema_violation" || error51.code === "artifact_invalid") {
|
|
34076
|
+
return { class: "invalid_envelope", reason: error51.message, retryable: false };
|
|
34077
|
+
}
|
|
34078
|
+
return { class: "transport_error", reason: error51.message, retryable: !error51.permanent };
|
|
34079
|
+
}
|
|
33801
34080
|
function wait(ms) {
|
|
33802
34081
|
return new Promise((resolve4) => setTimeout(resolve4, ms));
|
|
33803
34082
|
}
|
|
@@ -33917,6 +34196,7 @@ class SynapseEmbeddingProvider {
|
|
|
33917
34196
|
initialized = false;
|
|
33918
34197
|
initializing = null;
|
|
33919
34198
|
permanentFailure = false;
|
|
34199
|
+
lastFailureReason = null;
|
|
33920
34200
|
batchLimit = 16;
|
|
33921
34201
|
tokenBudget = null;
|
|
33922
34202
|
constructor(options) {
|
|
@@ -33991,9 +34271,11 @@ class SynapseEmbeddingProvider {
|
|
|
33991
34271
|
this.tokenBudget = metadata.recommended_token_budget ?? this.tokenBudget;
|
|
33992
34272
|
}
|
|
33993
34273
|
this.initialized = true;
|
|
34274
|
+
this.recordSuccess();
|
|
33994
34275
|
return true;
|
|
33995
34276
|
} catch (error51) {
|
|
33996
34277
|
const classified = classifyError(error51);
|
|
34278
|
+
this.recordFailure(classified);
|
|
33997
34279
|
if (classified.permanent) {
|
|
33998
34280
|
this.permanentFailure = true;
|
|
33999
34281
|
log(`[magic-context] Synapse lane disabled: ${classified.code}: ${classified.message}`);
|
|
@@ -34020,6 +34302,7 @@ class SynapseEmbeddingProvider {
|
|
|
34020
34302
|
if (!extracted)
|
|
34021
34303
|
throw new SynapseEmbeddingError("schema_violation", "Synapse query returned no vector");
|
|
34022
34304
|
this.validateResponse(extracted.metadata, extracted.vector.length);
|
|
34305
|
+
this.recordSuccess();
|
|
34023
34306
|
return extracted.vector;
|
|
34024
34307
|
} catch (error51) {
|
|
34025
34308
|
this.logCallFailure(error51, "embed.query");
|
|
@@ -34083,6 +34366,7 @@ class SynapseEmbeddingProvider {
|
|
|
34083
34366
|
const vectorArray = Float32Array.from(vector);
|
|
34084
34367
|
this.validateResponse({ ...batchEnvelope, ...item }, vectorArray.length);
|
|
34085
34368
|
output.set(id, vectorArray);
|
|
34369
|
+
this.recordSuccess();
|
|
34086
34370
|
}
|
|
34087
34371
|
} catch (error51) {
|
|
34088
34372
|
const classified = classifyError(error51);
|
|
@@ -34105,6 +34389,9 @@ class SynapseEmbeddingProvider {
|
|
|
34105
34389
|
isLoaded() {
|
|
34106
34390
|
return this.initialized;
|
|
34107
34391
|
}
|
|
34392
|
+
getLastFailureReason() {
|
|
34393
|
+
return this.lastFailureReason;
|
|
34394
|
+
}
|
|
34108
34395
|
requestConstraints(extra) {
|
|
34109
34396
|
const metadata = this.metadata;
|
|
34110
34397
|
if (!metadata)
|
|
@@ -34222,11 +34509,18 @@ class SynapseEmbeddingProvider {
|
|
|
34222
34509
|
}
|
|
34223
34510
|
logCallFailure(error51, operation) {
|
|
34224
34511
|
const classified = classifyError(error51);
|
|
34512
|
+
this.recordFailure(classified);
|
|
34225
34513
|
if (classified.permanent)
|
|
34226
34514
|
this.permanentFailure = true;
|
|
34227
34515
|
const suffix = classified.retryAfterMs === undefined ? "" : ` retry_after_ms=${classified.retryAfterMs}`;
|
|
34228
34516
|
log(`[magic-context] Synapse ${operation} failed: ${classified.code}${suffix}: ${classified.message}`);
|
|
34229
34517
|
}
|
|
34518
|
+
recordFailure(error51) {
|
|
34519
|
+
this.lastFailureReason = embeddingFailureFor(error51);
|
|
34520
|
+
}
|
|
34521
|
+
recordSuccess() {
|
|
34522
|
+
this.lastFailureReason = null;
|
|
34523
|
+
}
|
|
34230
34524
|
}
|
|
34231
34525
|
|
|
34232
34526
|
// src/features/magic-context/memory/embedding-identity.ts
|
|
@@ -34270,6 +34564,32 @@ import { chmodSync as chmodSync2, mkdirSync as mkdirSync3 } from "node:fs";
|
|
|
34270
34564
|
import { open, stat, unlink, writeFile } from "node:fs/promises";
|
|
34271
34565
|
import { dirname as dirname5, join as join6 } from "node:path";
|
|
34272
34566
|
import { pathToFileURL as pathToFileURL2 } from "node:url";
|
|
34567
|
+
var BUN_NAPI_TEARDOWN_FIX_VERSION = [1, 4, 0];
|
|
34568
|
+
function currentLocalEmbeddingHost() {
|
|
34569
|
+
const bun = globalThis.Bun;
|
|
34570
|
+
const bunVersion = typeof process !== "undefined" && typeof process.versions?.bun === "string" ? process.versions.bun : undefined;
|
|
34571
|
+
return {
|
|
34572
|
+
isElectron: typeof process !== "undefined" && Boolean(process.versions?.electron),
|
|
34573
|
+
isBun: Boolean(bun) || Boolean(bunVersion),
|
|
34574
|
+
bunVersion
|
|
34575
|
+
};
|
|
34576
|
+
}
|
|
34577
|
+
function bunHasNapiTeardownFix(version2) {
|
|
34578
|
+
const match = version2?.match(/^v?(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+))?(?:\+[0-9A-Za-z.-]+)?$/);
|
|
34579
|
+
if (!match || match[4])
|
|
34580
|
+
return false;
|
|
34581
|
+
const parsed = [Number(match[1]), Number(match[2]), Number(match[3])];
|
|
34582
|
+
return parsed.some((part) => !Number.isSafeInteger(part)) ? false : parsed[0] > BUN_NAPI_TEARDOWN_FIX_VERSION[0] || parsed[0] === BUN_NAPI_TEARDOWN_FIX_VERSION[0] && (parsed[1] > BUN_NAPI_TEARDOWN_FIX_VERSION[1] || parsed[1] === BUN_NAPI_TEARDOWN_FIX_VERSION[1] && parsed[2] >= BUN_NAPI_TEARDOWN_FIX_VERSION[2]);
|
|
34583
|
+
}
|
|
34584
|
+
function resolveLocalEmbeddingRuntime(preference = "auto", host = currentLocalEmbeddingHost()) {
|
|
34585
|
+
if (preference === "native" || preference === "wasm")
|
|
34586
|
+
return preference;
|
|
34587
|
+
if (host.isElectron)
|
|
34588
|
+
return "electron";
|
|
34589
|
+
if (host.isBun && !bunHasNapiTeardownFix(host.bunVersion))
|
|
34590
|
+
return "wasm";
|
|
34591
|
+
return "native";
|
|
34592
|
+
}
|
|
34273
34593
|
var LOCK_POLL_MS = 150;
|
|
34274
34594
|
var STALE_LOCK_MS = 3 * 60000;
|
|
34275
34595
|
var MAX_LOCK_WAIT_MS = 5 * 60000;
|
|
@@ -34323,20 +34643,21 @@ function startLockHeartbeat(lockPath) {
|
|
|
34323
34643
|
}
|
|
34324
34644
|
var localEmbeddingRuntimeMode = "native";
|
|
34325
34645
|
var wasmRuntimeInjected = false;
|
|
34326
|
-
var
|
|
34646
|
+
var localEmbeddingHostForRuntime = currentLocalEmbeddingHost;
|
|
34327
34647
|
var importWasmOrtForRuntime = async () => {
|
|
34328
|
-
const
|
|
34329
|
-
|
|
34648
|
+
const { createRequire: createRequireFn } = await import("node:module");
|
|
34649
|
+
const requireFn = createRequireFn(import.meta.url);
|
|
34650
|
+
const ortEntry = requireFn.resolve("onnxruntime-web");
|
|
34651
|
+
return {
|
|
34652
|
+
module: await import("onnxruntime-web"),
|
|
34653
|
+
entryPath: ortEntry
|
|
34654
|
+
};
|
|
34330
34655
|
};
|
|
34331
34656
|
var importTransformersForRuntime = async () => {
|
|
34332
|
-
|
|
34333
|
-
return await import(transformersSpec);
|
|
34657
|
+
return await import("./transformers.node-b1wa0hgx.js");
|
|
34334
34658
|
};
|
|
34335
34659
|
var importTransformersWasmFallbackForRuntime = async () => {
|
|
34336
|
-
const
|
|
34337
|
-
const requireFn = createRequireFn(import.meta.url);
|
|
34338
|
-
const nodeEntry = requireFn.resolve("@huggingface/transformers");
|
|
34339
|
-
const webEntry = pathToFileURL2(join6(dirname5(nodeEntry), "transformers.web.js")).href;
|
|
34660
|
+
const webEntry = new URL("./transformers-web.js", import.meta.url).href;
|
|
34340
34661
|
return await import(webEntry);
|
|
34341
34662
|
};
|
|
34342
34663
|
var modelCacheDirForRuntime = () => join6(getMagicContextStorageDir(), "models");
|
|
@@ -34354,17 +34675,9 @@ async function injectWasmOrt() {
|
|
|
34354
34675
|
if (wasmRuntimeInjected)
|
|
34355
34676
|
return true;
|
|
34356
34677
|
try {
|
|
34357
|
-
const ortWeb = await importWasmOrtForRuntime();
|
|
34358
|
-
|
|
34359
|
-
|
|
34360
|
-
const requireFn = createRequireFn(import.meta.url);
|
|
34361
|
-
const mainEntry = requireFn.resolve("onnxruntime-web");
|
|
34362
|
-
const distDir = dirname5(mainEntry);
|
|
34363
|
-
if (ortWeb.env?.wasm) {
|
|
34364
|
-
ortWeb.env.wasm.wasmPaths = `${pathToFileURL2(distDir).href}/`;
|
|
34365
|
-
}
|
|
34366
|
-
} catch (pathError) {
|
|
34367
|
-
log("[magic-context] could not resolve local onnxruntime-web/dist, falling back to default WASM paths:", pathError instanceof Error ? pathError.message : String(pathError));
|
|
34678
|
+
const { module: ortWeb, entryPath } = await importWasmOrtForRuntime();
|
|
34679
|
+
if (ortWeb.env?.wasm) {
|
|
34680
|
+
ortWeb.env.wasm.wasmPaths = `${pathToFileURL2(dirname5(entryPath)).href}/`;
|
|
34368
34681
|
}
|
|
34369
34682
|
globalThis[Symbol.for("onnxruntime")] = ortWeb;
|
|
34370
34683
|
wasmRuntimeInjected = true;
|
|
@@ -34391,10 +34704,18 @@ function disableLocalEmbeddingsAfterRuntimeFailure(detail) {
|
|
|
34391
34704
|
localEmbeddingRuntimeMode = "disabled";
|
|
34392
34705
|
logForRuntime(`[magic-context] local embeddings are disabled because both the onnxruntime-node native binding and the onnxruntime-web (WASM) fallback failed to load. Native failure: ${detail}. Run \`npx @cortexkit/magic-context@latest doctor\` for repair guidance (use \`doctor --force\` to reinstall cached plugin packages), or configure an \`openai-compatible\` embedding HTTP endpoint. Existing memories are unaffected.`);
|
|
34393
34706
|
}
|
|
34394
|
-
async function loadTransformersForLocalEmbedding() {
|
|
34707
|
+
async function loadTransformersForLocalEmbedding(runtimePreference) {
|
|
34395
34708
|
if (localEmbeddingRuntimeMode === "disabled") {
|
|
34396
34709
|
throw new Error("local embedding runtime is disabled");
|
|
34397
34710
|
}
|
|
34711
|
+
const resolvedRuntime = resolveLocalEmbeddingRuntime(runtimePreference, localEmbeddingHostForRuntime());
|
|
34712
|
+
if (resolvedRuntime === "wasm") {
|
|
34713
|
+
if (!await ensureWasmOrtInjected()) {
|
|
34714
|
+
disableLocalEmbeddingsAfterRuntimeFailure("the selected WASM runtime is unavailable");
|
|
34715
|
+
throw new Error("onnxruntime-web failed to load");
|
|
34716
|
+
}
|
|
34717
|
+
return { module: await importTransformersForRuntime(), usesWasm: true };
|
|
34718
|
+
}
|
|
34398
34719
|
if (localEmbeddingRuntimeMode === "wasm") {
|
|
34399
34720
|
if (!await ensureWasmOrtInjected()) {
|
|
34400
34721
|
disableLocalEmbeddingsAfterRuntimeFailure("the previously selected WASM runtime is unavailable");
|
|
@@ -34407,7 +34728,7 @@ async function loadTransformersForLocalEmbedding() {
|
|
|
34407
34728
|
throw wasmError;
|
|
34408
34729
|
}
|
|
34409
34730
|
}
|
|
34410
|
-
const electron =
|
|
34731
|
+
const electron = resolvedRuntime === "electron";
|
|
34411
34732
|
if (electron) {
|
|
34412
34733
|
const wasInjected = wasmRuntimeInjected;
|
|
34413
34734
|
if (await ensureWasmOrtInjected()) {
|
|
@@ -34463,6 +34784,8 @@ function isNativeRuntimeMissingError(error51) {
|
|
|
34463
34784
|
if (code === "ERR_DLOPEN_FAILED" && lower.includes("onnxruntime")) {
|
|
34464
34785
|
return true;
|
|
34465
34786
|
}
|
|
34787
|
+
if (lower.includes('could not load the "sharp" module'))
|
|
34788
|
+
return true;
|
|
34466
34789
|
const mentionsNativeRuntime = lower.includes("onnxruntime-node") || lower.includes("onnxruntime_binding");
|
|
34467
34790
|
if (!mentionsNativeRuntime)
|
|
34468
34791
|
return false;
|
|
@@ -34515,19 +34838,22 @@ class LocalEmbeddingProvider {
|
|
|
34515
34838
|
maxInputTokens;
|
|
34516
34839
|
model;
|
|
34517
34840
|
dtype;
|
|
34841
|
+
runtimePreference;
|
|
34518
34842
|
pipeline = null;
|
|
34519
34843
|
initPromise = null;
|
|
34520
34844
|
inFlight = 0;
|
|
34521
34845
|
disposing = false;
|
|
34522
34846
|
disposePromise = null;
|
|
34523
34847
|
inFlightWaiters = [];
|
|
34524
|
-
constructor(model = DEFAULT_LOCAL_EMBEDDING_MODEL, maxInputTokens = 512, dtype = DEFAULT_LOCAL_DTYPE) {
|
|
34848
|
+
constructor(model = DEFAULT_LOCAL_EMBEDDING_MODEL, maxInputTokens = 512, dtype = DEFAULT_LOCAL_DTYPE, runtimePreference = "auto") {
|
|
34525
34849
|
this.model = model;
|
|
34526
34850
|
this.maxInputTokens = maxInputTokens;
|
|
34527
34851
|
this.dtype = dtype || DEFAULT_LOCAL_DTYPE;
|
|
34852
|
+
this.runtimePreference = runtimePreference;
|
|
34528
34853
|
this.modelId = getEmbeddingProviderIdentity({
|
|
34529
34854
|
provider: "local",
|
|
34530
34855
|
model,
|
|
34856
|
+
local_runtime: runtimePreference,
|
|
34531
34857
|
...dtype && dtype !== DEFAULT_LOCAL_DTYPE ? { local_dtype: dtype } : {}
|
|
34532
34858
|
});
|
|
34533
34859
|
}
|
|
@@ -34550,7 +34876,7 @@ class LocalEmbeddingProvider {
|
|
|
34550
34876
|
if (this.disposing) {
|
|
34551
34877
|
return;
|
|
34552
34878
|
}
|
|
34553
|
-
const { module: transformersModule, usesWasm } = await loadTransformersForLocalEmbedding();
|
|
34879
|
+
const { module: transformersModule, usesWasm } = await loadTransformersForLocalEmbedding(this.runtimePreference);
|
|
34554
34880
|
const env = transformersModule.env;
|
|
34555
34881
|
const LogLevel = transformersModule.LogLevel;
|
|
34556
34882
|
if (LogLevel && "ERROR" in LogLevel) {
|
|
@@ -34618,7 +34944,7 @@ class LocalEmbeddingProvider {
|
|
|
34618
34944
|
if (!localEmbeddingRuntimeIsDisabled() && isNativeRuntimeMissingError(error51)) {
|
|
34619
34945
|
disableLocalEmbeddingsAfterRuntimeFailure(error51 instanceof Error ? error51.message : String(error51));
|
|
34620
34946
|
} else if (!localEmbeddingRuntimeIsDisabled()) {
|
|
34621
|
-
|
|
34947
|
+
logForRuntime("[magic-context] embedding model failed to load:", error51);
|
|
34622
34948
|
}
|
|
34623
34949
|
this.pipeline = null;
|
|
34624
34950
|
} finally {
|
|
@@ -35210,6 +35536,13 @@ function markProjectLoadUntrusted(projectIdentity) {
|
|
|
35210
35536
|
untrustedLoadProjects.add(projectIdentity);
|
|
35211
35537
|
}
|
|
35212
35538
|
var testProviderFactory = null;
|
|
35539
|
+
|
|
35540
|
+
class TestProviderFactoryRequiredError extends Error {
|
|
35541
|
+
constructor() {
|
|
35542
|
+
super("test constructed a network-capable embedding provider without a test factory — install _setTestProviderFactoryForProject or set embedding.provider off in the fixture");
|
|
35543
|
+
this.name = "TestProviderFactoryRequiredError";
|
|
35544
|
+
}
|
|
35545
|
+
}
|
|
35213
35546
|
function synapseConfigFields(config2) {
|
|
35214
35547
|
const raw = config2;
|
|
35215
35548
|
return {
|
|
@@ -35281,6 +35614,7 @@ function resolveEmbeddingConfig(config2) {
|
|
|
35281
35614
|
return {
|
|
35282
35615
|
provider: "local",
|
|
35283
35616
|
model: config2?.model?.trim() || DEFAULT_LOCAL_EMBEDDING_MODEL,
|
|
35617
|
+
local_runtime: config2?.local_runtime ?? "auto",
|
|
35284
35618
|
...config2?.max_input_tokens ? {
|
|
35285
35619
|
max_input_tokens: normalizeCompartmentChunkMaxInputTokens(config2.max_input_tokens)
|
|
35286
35620
|
} : {},
|
|
@@ -35325,11 +35659,14 @@ function resolveEmbeddingConfig(config2) {
|
|
|
35325
35659
|
throw new Error("Unknown embedding provider");
|
|
35326
35660
|
}
|
|
35327
35661
|
function createProvider(config2, context) {
|
|
35662
|
+
if (config2.provider === "off") {
|
|
35663
|
+
return null;
|
|
35664
|
+
}
|
|
35328
35665
|
if (testProviderFactory) {
|
|
35329
35666
|
return testProviderFactory(config2);
|
|
35330
35667
|
}
|
|
35331
|
-
if (
|
|
35332
|
-
|
|
35668
|
+
if (process.env.MAGIC_CONTEXT_TEST_DATA_DIR?.trim()) {
|
|
35669
|
+
throw new TestProviderFactoryRequiredError;
|
|
35333
35670
|
}
|
|
35334
35671
|
if (config2.provider === "openai-compatible") {
|
|
35335
35672
|
return new OpenAICompatibleEmbeddingProvider({
|
|
@@ -35343,7 +35680,7 @@ function createProvider(config2, context) {
|
|
|
35343
35680
|
});
|
|
35344
35681
|
}
|
|
35345
35682
|
if (config2.provider === "local") {
|
|
35346
|
-
return new LocalEmbeddingProvider(config2.model, config2.max_input_tokens, config2.local_dtype);
|
|
35683
|
+
return new LocalEmbeddingProvider(config2.model, config2.max_input_tokens, config2.local_dtype, config2.local_runtime);
|
|
35347
35684
|
}
|
|
35348
35685
|
if (config2.provider === "synapse") {
|
|
35349
35686
|
const synapse = config2;
|
|
@@ -35830,15 +36167,25 @@ function shadowBackfillMissingBase(scope, primaryModelId, shadowModelId, project
|
|
|
35830
36167
|
};
|
|
35831
36168
|
}
|
|
35832
36169
|
return {
|
|
35833
|
-
sql: `SELECT
|
|
36170
|
+
sql: `SELECT cp.compartment_id AS id,
|
|
36171
|
+
MIN(CASE WHEN NOT EXISTS (
|
|
36172
|
+
SELECT 1 FROM compartment_chunk_embeddings same_key
|
|
36173
|
+
WHERE same_key.compartment_id = cp.compartment_id
|
|
36174
|
+
AND same_key.window_index = cp.window_index
|
|
36175
|
+
AND same_key.model_id = ?
|
|
36176
|
+
) THEN 0 ELSE 1 END) AS defect_order
|
|
35834
36177
|
FROM compartment_chunk_embeddings cp
|
|
35835
36178
|
WHERE cp.project_path = ? AND cp.model_id = ?
|
|
35836
36179
|
AND NOT EXISTS (
|
|
35837
|
-
SELECT 1 FROM compartment_chunk_embeddings
|
|
35838
|
-
WHERE
|
|
35839
|
-
|
|
35840
|
-
|
|
35841
|
-
|
|
36180
|
+
SELECT 1 FROM compartment_chunk_embeddings current
|
|
36181
|
+
WHERE current.compartment_id = cp.compartment_id
|
|
36182
|
+
AND current.window_index = cp.window_index
|
|
36183
|
+
AND current.chunk_hash = cp.chunk_hash
|
|
36184
|
+
AND current.model_id = ?
|
|
36185
|
+
)
|
|
36186
|
+
GROUP BY cp.compartment_id`,
|
|
36187
|
+
params: [shadowModelId, projectIdentity, primaryModelId, shadowModelId],
|
|
36188
|
+
orderBy: " ORDER BY defect_order ASC, cp.compartment_id ASC"
|
|
35842
36189
|
};
|
|
35843
36190
|
}
|
|
35844
36191
|
function shadowBackfillMissingIds(db, projectIdentity, scope, primaryModelId, shadowModelId, limit) {
|
|
@@ -36454,7 +36801,8 @@ async function embedSessionCompartmentChunks(db, projectIdentity, sessionId, opt
|
|
|
36454
36801
|
return { status: "disabled", embedded: 0, total: 0 };
|
|
36455
36802
|
}
|
|
36456
36803
|
recordSessionProjectIdentity(db, sessionId, projectIdentity);
|
|
36457
|
-
const
|
|
36804
|
+
const maxInputTokens = getProjectEmbeddingMaxInputTokens(projectIdentity);
|
|
36805
|
+
const total = countUnembeddedSessionCompartments(db, projectIdentity, sessionId, snapshot2.chunkModelId, maxInputTokens);
|
|
36458
36806
|
if (total === 0)
|
|
36459
36807
|
return { status: "nothing", embedded: 0, total: 0 };
|
|
36460
36808
|
const holderId = `session-embed-${randomUUID2()}`;
|
|
@@ -36492,7 +36840,7 @@ async function embedSessionCompartmentChunks(db, projectIdentity, sessionId, opt
|
|
|
36492
36840
|
aborted2 = true;
|
|
36493
36841
|
break;
|
|
36494
36842
|
}
|
|
36495
|
-
const candidates = loadUnembeddedSessionChunkCandidates(db, projectIdentity, sessionId, snapshot2.chunkModelId, batchSize, [...skipIds, ...failedIds]);
|
|
36843
|
+
const candidates = loadUnembeddedSessionChunkCandidates(db, projectIdentity, sessionId, snapshot2.chunkModelId, batchSize, [...skipIds, ...failedIds], maxInputTokens);
|
|
36496
36844
|
if (candidates.length === 0)
|
|
36497
36845
|
break;
|
|
36498
36846
|
const {
|
|
@@ -36537,7 +36885,7 @@ async function embedSessionCompartmentChunks(db, projectIdentity, sessionId, opt
|
|
|
36537
36885
|
if (aborted2)
|
|
36538
36886
|
return { status: "aborted", embedded, total, failed: failedIds.length };
|
|
36539
36887
|
if (providerDown || failedIds.length > 0) {
|
|
36540
|
-
const remaining = Math.max(0, countUnembeddedSessionCompartments(db, projectIdentity, sessionId, snapshot2.chunkModelId) - skipIds.length);
|
|
36888
|
+
const remaining = Math.max(0, countUnembeddedSessionCompartments(db, projectIdentity, sessionId, snapshot2.chunkModelId, maxInputTokens) - skipIds.length);
|
|
36541
36889
|
if (remaining > 0) {
|
|
36542
36890
|
return {
|
|
36543
36891
|
status: "stalled",
|
|
@@ -36563,7 +36911,7 @@ function getEmbeddingCoverageStatus(db, projectIdentity, sessionId) {
|
|
|
36563
36911
|
commits: { embedded: 0, total: 0, gitEnabled: false }
|
|
36564
36912
|
};
|
|
36565
36913
|
}
|
|
36566
|
-
const session = countSessionCompartmentEmbedCoverage(db, projectIdentity, sessionId, snapshot2.chunkModelId);
|
|
36914
|
+
const session = countSessionCompartmentEmbedCoverage(db, projectIdentity, sessionId, snapshot2.chunkModelId, getProjectEmbeddingMaxInputTokens(projectIdentity));
|
|
36567
36915
|
const memories = getMemoryEmbedCoverage(db, projectIdentity, snapshot2.modelId);
|
|
36568
36916
|
const gitEnabled = snapshot2.gitCommitEnabled;
|
|
36569
36917
|
const commits = gitEnabled ? {
|
|
@@ -36702,7 +37050,8 @@ function cosineSimilarity(a, b) {
|
|
|
36702
37050
|
// src/features/magic-context/memory/embedding.ts
|
|
36703
37051
|
var DEFAULT_EMBEDDING_CONFIG = {
|
|
36704
37052
|
provider: "local",
|
|
36705
|
-
model: DEFAULT_LOCAL_EMBEDDING_MODEL
|
|
37053
|
+
model: DEFAULT_LOCAL_EMBEDDING_MODEL,
|
|
37054
|
+
local_runtime: "auto"
|
|
36706
37055
|
};
|
|
36707
37056
|
var embeddingConfig = DEFAULT_EMBEDDING_CONFIG;
|
|
36708
37057
|
var provider = null;
|
|
@@ -36722,7 +37071,7 @@ function createProvider2(config2) {
|
|
|
36722
37071
|
});
|
|
36723
37072
|
}
|
|
36724
37073
|
if (config2.provider === "local") {
|
|
36725
|
-
return new LocalEmbeddingProvider(config2.model, config2.max_input_tokens, config2.local_dtype);
|
|
37074
|
+
return new LocalEmbeddingProvider(config2.model, config2.max_input_tokens, config2.local_dtype, config2.local_runtime);
|
|
36726
37075
|
}
|
|
36727
37076
|
if (config2.provider === "synapse") {
|
|
36728
37077
|
const synapse = config2;
|
|
@@ -36938,13 +37287,13 @@ function placeholders3(values) {
|
|
|
36938
37287
|
function uniqueSortedFiles(files) {
|
|
36939
37288
|
return Array.from(new Set(files.filter((file2) => file2 !== MEMORY_VERIFICATION_SENTINEL))).sort();
|
|
36940
37289
|
}
|
|
36941
|
-
function recordMemoryMapping(db, memoryId, normalizedFiles, now) {
|
|
37290
|
+
function recordMemoryMapping(db, memoryId, normalizedFiles, now, mappingOrigin = "mapper") {
|
|
36942
37291
|
const realFiles = uniqueSortedFiles(normalizedFiles);
|
|
36943
37292
|
const filesToWrite = realFiles.length > 0 ? realFiles : [MEMORY_VERIFICATION_SENTINEL];
|
|
36944
37293
|
db.prepare("DELETE FROM memory_verifications WHERE memory_id = ?").run(memoryId);
|
|
36945
|
-
const insert = db.prepare("INSERT INTO memory_verifications (memory_id, file_path, verified_at, mapped_at) VALUES (?, ?, 0, ?)");
|
|
37294
|
+
const insert = db.prepare("INSERT INTO memory_verifications (memory_id, file_path, verified_at, mapped_at, mapping_origin) VALUES (?, ?, 0, ?, ?)");
|
|
36946
37295
|
for (const file2 of filesToWrite) {
|
|
36947
|
-
insert.run(memoryId, file2, now);
|
|
37296
|
+
insert.run(memoryId, file2, now, mappingOrigin);
|
|
36948
37297
|
}
|
|
36949
37298
|
return filesToWrite.length;
|
|
36950
37299
|
}
|
|
@@ -36952,7 +37301,7 @@ function recordMemoryVerifications(db, memoryId, normalizedFiles, now) {
|
|
|
36952
37301
|
const realFiles = uniqueSortedFiles(normalizedFiles);
|
|
36953
37302
|
const filesToWrite = realFiles.length > 0 ? realFiles : [MEMORY_VERIFICATION_SENTINEL];
|
|
36954
37303
|
db.prepare("DELETE FROM memory_verifications WHERE memory_id = ?").run(memoryId);
|
|
36955
|
-
const insert = db.prepare("INSERT INTO memory_verifications (memory_id, file_path, verified_at, mapped_at) VALUES (?, ?, ?,
|
|
37304
|
+
const insert = db.prepare("INSERT INTO memory_verifications (memory_id, file_path, verified_at, mapped_at, mapping_origin) VALUES (?, ?, ?, ?, 'mapper')");
|
|
36956
37305
|
for (const file2 of filesToWrite) {
|
|
36957
37306
|
insert.run(memoryId, file2, now, now);
|
|
36958
37307
|
}
|
|
@@ -36974,7 +37323,7 @@ function getMemoryVerifications(db, memoryIds) {
|
|
|
36974
37323
|
const result = new Map;
|
|
36975
37324
|
if (ids.length === 0)
|
|
36976
37325
|
return result;
|
|
36977
|
-
const rows = db.prepare(`SELECT memory_id, file_path, verified_at, mapped_at
|
|
37326
|
+
const rows = db.prepare(`SELECT memory_id, file_path, verified_at, mapped_at, mapping_origin
|
|
36978
37327
|
FROM memory_verifications
|
|
36979
37328
|
WHERE memory_id IN (${placeholders3(ids)})
|
|
36980
37329
|
ORDER BY memory_id, file_path`).all(...ids);
|
|
@@ -36982,6 +37331,7 @@ function getMemoryVerifications(db, memoryIds) {
|
|
|
36982
37331
|
const existing = result.get(row.memory_id) ?? {
|
|
36983
37332
|
files: [],
|
|
36984
37333
|
hasSentinel: false,
|
|
37334
|
+
mappingOrigin: "mapper",
|
|
36985
37335
|
verifiedAt: 0,
|
|
36986
37336
|
mappedAt: 0
|
|
36987
37337
|
};
|
|
@@ -36990,6 +37340,9 @@ function getMemoryVerifications(db, memoryIds) {
|
|
|
36990
37340
|
} else if (!existing.files.includes(row.file_path)) {
|
|
36991
37341
|
existing.files.push(row.file_path);
|
|
36992
37342
|
}
|
|
37343
|
+
if (row.mapping_origin === "host_rejected_fallback") {
|
|
37344
|
+
existing.mappingOrigin = "host_rejected_fallback";
|
|
37345
|
+
}
|
|
36993
37346
|
existing.verifiedAt = Math.max(existing.verifiedAt, row.verified_at);
|
|
36994
37347
|
existing.mappedAt = Math.max(existing.mappedAt, row.mapped_at ?? 0);
|
|
36995
37348
|
result.set(row.memory_id, existing);
|
|
@@ -37709,12 +38062,14 @@ function dropMarkerSummaryTag(db, sessionId, summaryMessageId) {
|
|
|
37709
38062
|
updateTagStatus(db, sessionId, tagNumber, "dropped");
|
|
37710
38063
|
}
|
|
37711
38064
|
function persistMarkerStateAndDropReplacedTag(db, sessionId, state2, replacedSummaryMessageId) {
|
|
38065
|
+
const transactionStartedAt = performance.now();
|
|
37712
38066
|
db.transaction(() => {
|
|
37713
38067
|
setPersistedCompactionMarkerState(db, sessionId, state2);
|
|
37714
38068
|
if (replacedSummaryMessageId !== null) {
|
|
37715
38069
|
dropMarkerSummaryTag(db, sessionId, replacedSummaryMessageId);
|
|
37716
38070
|
}
|
|
37717
38071
|
})();
|
|
38072
|
+
logSlowWriteTransaction("marker-drain", transactionStartedAt);
|
|
37718
38073
|
}
|
|
37719
38074
|
function validatePendingTarget(db, sessionId, pending) {
|
|
37720
38075
|
const ocMessage = getOpenCodeMessageById(sessionId, pending.endMessageId);
|
|
@@ -41366,7 +41721,7 @@ function readCurrentM0SnapshotMarkersUncached(args) {
|
|
|
41366
41721
|
toolSetHash: hard.toolSetHash ?? "",
|
|
41367
41722
|
modelKey: piModelRefToCanonical(hard.modelKey),
|
|
41368
41723
|
projectIdentity: args.projectPath ?? null,
|
|
41369
|
-
muralEnabled: args.muralEnabled === true,
|
|
41724
|
+
muralEnabled: args.memoryEnabled !== false && args.muralEnabled === true,
|
|
41370
41725
|
renderBudgetIdentity: renderBudgetIdentity(args.memoryInjectionBudgetTokens, args.historyBudgetTokens)
|
|
41371
41726
|
}
|
|
41372
41727
|
};
|
|
@@ -41382,7 +41737,7 @@ function refreshVolatileMarkerInputs(markers, args) {
|
|
|
41382
41737
|
toolSetHash: hard.toolSetHash ?? "",
|
|
41383
41738
|
modelKey: hard.modelKey,
|
|
41384
41739
|
projectIdentity: args.projectPath ?? null,
|
|
41385
|
-
muralEnabled: args.muralEnabled === true,
|
|
41740
|
+
muralEnabled: args.memoryEnabled !== false && args.muralEnabled === true,
|
|
41386
41741
|
renderBudgetIdentity: renderBudgetIdentity(args.memoryInjectionBudgetTokens, args.historyBudgetTokens)
|
|
41387
41742
|
};
|
|
41388
41743
|
}
|
|
@@ -41455,6 +41810,10 @@ function snapshotMarkersFromCachedM0(state2) {
|
|
|
41455
41810
|
renderBudgetIdentity: cachedUpgradeIdentity.renderBudgetIdentity
|
|
41456
41811
|
};
|
|
41457
41812
|
}
|
|
41813
|
+
var MEMORY_DERIVED_BLOCK_PATTERN = /<(?:project-memory|user-profile|new-user-profile|memory-updates|memory-mural)(?:>|\s)/;
|
|
41814
|
+
function cachedMemoryDerivedSurfacePresent(state2) {
|
|
41815
|
+
return [state2.cachedM0Bytes, state2.cachedM1Bytes].some((bytes) => bytes !== null && MEMORY_DERIVED_BLOCK_PATTERN.test(bytes.toString("utf8")));
|
|
41816
|
+
}
|
|
41458
41817
|
function mustMaterialize(args) {
|
|
41459
41818
|
if (!args.state.cachedM0Bytes)
|
|
41460
41819
|
return { value: true, reason: "first_render" };
|
|
@@ -41463,6 +41822,9 @@ function mustMaterialize(args) {
|
|
|
41463
41822
|
const hard = args.hardSignals ?? EMPTY_HARD_SIGNALS;
|
|
41464
41823
|
const current = readCurrentM0SnapshotMarkers(args);
|
|
41465
41824
|
const cachedUpgradeIdentity = decodeCachedM0UpgradeIdentity(args.state.cachedM0UpgradeState);
|
|
41825
|
+
if (args.memoryEnabled === false && cachedMemoryDerivedSurfacePresent(args.state)) {
|
|
41826
|
+
return { value: true, reason: "render_config" };
|
|
41827
|
+
}
|
|
41466
41828
|
if (cachedUpgradeIdentity.compartmentRenderEpoch !== current.compartmentRenderEpoch) {
|
|
41467
41829
|
return { value: true, reason: "compartment_render_epoch" };
|
|
41468
41830
|
}
|
|
@@ -41817,7 +42179,7 @@ function historySliceTokens(m0Text) {
|
|
|
41817
42179
|
return slice ? estimateTokens(slice) : 0;
|
|
41818
42180
|
}
|
|
41819
42181
|
function resolveMuralForM0(options, projectPath, modelKey, budgetTokens) {
|
|
41820
|
-
if (!options.muralEnabled)
|
|
42182
|
+
if (options.memoryEnabled === false || !options.muralEnabled)
|
|
41821
42183
|
return;
|
|
41822
42184
|
return resolveMuralWire(options.db, projectPath, modelKey, true, budgetTokens);
|
|
41823
42185
|
}
|
|
@@ -41852,6 +42214,7 @@ function materializeM0(options) {
|
|
|
41852
42214
|
projectPath,
|
|
41853
42215
|
projectDirectory,
|
|
41854
42216
|
injectDocs: options.injectDocs,
|
|
42217
|
+
memoryEnabled: options.memoryEnabled,
|
|
41855
42218
|
muralEnabled: options.muralEnabled,
|
|
41856
42219
|
memoryInjectionBudgetTokens: options.memoryInjectionBudgetTokens,
|
|
41857
42220
|
historyBudgetTokens: options.historyBudgetTokens,
|
|
@@ -41866,7 +42229,7 @@ function materializeM0(options) {
|
|
|
41866
42229
|
compartments = options.compactionOff ? [] : readM0Compartments(options.db, options.sessionId);
|
|
41867
42230
|
facts = [];
|
|
41868
42231
|
memories = projectPath ? workspace.isWorkspaced ? getMemoriesByProjects(options.db, workspace.expandedIdentities, ["active", "permanent"], foldMaterializedAt, workspace.ownIdentities, workspace.shareCategories) : getMemoriesByProject(options.db, projectPath, ["active", "permanent"], foldMaterializedAt) : [];
|
|
41869
|
-
userMemories = safeGetActiveUserMemories(options.db);
|
|
42232
|
+
userMemories = options.memoryEnabled === false ? [] : safeGetActiveUserMemories(options.db);
|
|
41870
42233
|
options.db.exec("COMMIT");
|
|
41871
42234
|
} catch (error51) {
|
|
41872
42235
|
try {
|
|
@@ -41884,7 +42247,7 @@ function materializeM0(options) {
|
|
|
41884
42247
|
})
|
|
41885
42248
|
};
|
|
41886
42249
|
const trimmed = workspace.isWorkspaced ? trimWorkspaceMemoriesToBudgetV2(options.sessionId, memories, memoryBudget, workspace, memoryRenderOptions) : trimMemoriesToBudgetV2(options.sessionId, memories, memoryBudget);
|
|
41887
|
-
const mural = options.mural ?? resolveMuralForM0(options, projectPath, snapshotMarkers.modelKey, memoryBudget);
|
|
42250
|
+
const mural = options.memoryEnabled === false ? undefined : options.mural ?? resolveMuralForM0(options, projectPath, snapshotMarkers.modelKey, memoryBudget);
|
|
41888
42251
|
let decayPressureMultiplier = 1;
|
|
41889
42252
|
let m0Text = renderM0({
|
|
41890
42253
|
projectDocs: docs.renderedBlock,
|
|
@@ -42127,7 +42490,7 @@ ${newCompartments.map((compartment) => renderCompartmentAtTier(compartment, 1)).
|
|
|
42127
42490
|
if (newMemoriesBlock)
|
|
42128
42491
|
blocks.push(newMemoriesBlock);
|
|
42129
42492
|
const currentUserProfileVersion = getGlobalUserProfileVersion(options.db);
|
|
42130
|
-
if (currentUserProfileVersion !== markers.projectUserProfileVersion) {
|
|
42493
|
+
if (options.memoryEnabled !== false && currentUserProfileVersion !== markers.projectUserProfileVersion) {
|
|
42131
42494
|
const profileBlock = renderUserProfileBlock(trimUserMemoriesToBudget(safeGetActiveUserMemories(options.db), Math.max(1, Math.floor((options.userProfileBudgetTokens ?? DEFAULT_USER_PROFILE_BUDGET_TOKENS) * 0.25))), "new-user-profile");
|
|
42132
42495
|
if (profileBlock)
|
|
42133
42496
|
blocks.push(profileBlock);
|
|
@@ -42345,6 +42708,7 @@ function renderFreshM0NonPersisted(options) {
|
|
|
42345
42708
|
projectPath,
|
|
42346
42709
|
projectDirectory,
|
|
42347
42710
|
injectDocs: options.injectDocs,
|
|
42711
|
+
memoryEnabled: options.memoryEnabled,
|
|
42348
42712
|
muralEnabled: options.muralEnabled,
|
|
42349
42713
|
memoryInjectionBudgetTokens: options.memoryInjectionBudgetTokens,
|
|
42350
42714
|
historyBudgetTokens: options.historyBudgetTokens,
|
|
@@ -42359,7 +42723,7 @@ function renderFreshM0NonPersisted(options) {
|
|
|
42359
42723
|
snapshotMarkers.materializedAt = options.state.cachedM0MaterializedAt ?? 0;
|
|
42360
42724
|
const compartments = options.compactionOff ? [] : withCompartmentDates(options.sessionId, readM0Compartments(options.db, options.sessionId), options.temporalAwareness);
|
|
42361
42725
|
const memories = projectPath ? workspace.isWorkspaced ? getMemoriesByProjects(options.db, workspace.expandedIdentities, ["active", "permanent"], snapshotMarkers.materializedAt, workspace.ownIdentities, workspace.shareCategories) : getMemoriesByProject(options.db, projectPath, ["active", "permanent"], snapshotMarkers.materializedAt) : [];
|
|
42362
|
-
const userMemories = safeGetActiveUserMemories(options.db);
|
|
42726
|
+
const userMemories = options.memoryEnabled === false ? [] : safeGetActiveUserMemories(options.db);
|
|
42363
42727
|
const memoryBudget = options.memoryInjectionBudgetTokens ?? DEFAULT_MEMORY_BUDGET_TOKENS;
|
|
42364
42728
|
const memoryRenderOptions = {
|
|
42365
42729
|
sourceNameByMemoryId: sourceNamesForMemories({
|
|
@@ -42370,7 +42734,7 @@ function renderFreshM0NonPersisted(options) {
|
|
|
42370
42734
|
};
|
|
42371
42735
|
const trimmed = workspace.isWorkspaced ? trimWorkspaceMemoriesToBudgetV2(options.sessionId, memories, memoryBudget, workspace, memoryRenderOptions) : trimMemoriesToBudgetV2(options.sessionId, memories, memoryBudget);
|
|
42372
42736
|
const budget = options.historyBudgetTokens ?? DEFAULT_HISTORY_BUDGET_TOKENS;
|
|
42373
|
-
const mural = options.mural ?? resolveMuralForM0(options, projectPath, snapshotMarkers.modelKey, memoryBudget);
|
|
42737
|
+
const mural = options.memoryEnabled === false ? undefined : options.mural ?? resolveMuralForM0(options, projectPath, snapshotMarkers.modelKey, memoryBudget);
|
|
42374
42738
|
let decayPressureMultiplier = 1;
|
|
42375
42739
|
let m0Text = renderM0({
|
|
42376
42740
|
projectDocs: docs.renderedBlock,
|
|
@@ -42446,6 +42810,7 @@ function injectM0M1(options) {
|
|
|
42446
42810
|
hardSignals: options.hardSignals,
|
|
42447
42811
|
workspaceIdentitySet: options.workspaceIdentitySet,
|
|
42448
42812
|
injectDocs: options.injectDocs,
|
|
42813
|
+
memoryEnabled: options.memoryEnabled,
|
|
42449
42814
|
muralEnabled: options.muralEnabled,
|
|
42450
42815
|
memoryInjectionBudgetTokens: options.memoryInjectionBudgetTokens,
|
|
42451
42816
|
historyBudgetTokens: options.historyBudgetTokens
|
|
@@ -42777,23 +43142,33 @@ function fenceWrapupBoundaryForToolArcs(args) {
|
|
|
42777
43142
|
return boundary;
|
|
42778
43143
|
}
|
|
42779
43144
|
function applyHeadCap(args) {
|
|
42780
|
-
const {
|
|
43145
|
+
const {
|
|
43146
|
+
index,
|
|
43147
|
+
protectedTailStart,
|
|
43148
|
+
offset,
|
|
43149
|
+
arcs,
|
|
43150
|
+
lastCompartmentEndOrdinal,
|
|
43151
|
+
capTokens,
|
|
43152
|
+
recentOpenArcCutoff
|
|
43153
|
+
} = args;
|
|
42781
43154
|
if (offset >= protectedTailStart)
|
|
42782
43155
|
return { eligibleEndOrdinal: offset, oversizeAtomicUnit: false };
|
|
42783
43156
|
let end = index.findHeadEndForCap(offset, protectedTailStart, capTokens);
|
|
42784
43157
|
let oversizeAtomicUnit = end === offset + 1 && index.tokenForOrdinal(offset) > capTokens;
|
|
43158
|
+
const completedFence = fenceBoundaryForCompletedToolArcs(end, arcs, lastCompartmentEndOrdinal + 1);
|
|
43159
|
+
if (completedFence > end) {
|
|
43160
|
+
end = Math.min(protectedTailStart, completedFence);
|
|
43161
|
+
if (index.rangeTokens(offset, end) > capTokens)
|
|
43162
|
+
oversizeAtomicUnit = true;
|
|
43163
|
+
} else {
|
|
43164
|
+
end = completedFence;
|
|
43165
|
+
}
|
|
42785
43166
|
for (const arc of arcs) {
|
|
42786
43167
|
const resOrdinal = arc.resOrdinal;
|
|
42787
43168
|
if (resOrdinal === null) {
|
|
42788
43169
|
if (arc.invOrdinal >= recentOpenArcCutoff && arc.invOrdinal >= offset && arc.invOrdinal < end) {
|
|
42789
43170
|
end = Math.min(end, arc.invOrdinal);
|
|
42790
43171
|
}
|
|
42791
|
-
continue;
|
|
42792
|
-
}
|
|
42793
|
-
if (arc.invOrdinal < end && end <= resOrdinal) {
|
|
42794
|
-
end = Math.min(protectedTailStart, resOrdinal + 1);
|
|
42795
|
-
if (index.rangeTokens(Math.max(offset, arc.invOrdinal), end) > capTokens)
|
|
42796
|
-
oversizeAtomicUnit = true;
|
|
42797
43172
|
}
|
|
42798
43173
|
}
|
|
42799
43174
|
if (end <= offset && offset < protectedTailStart) {
|
|
@@ -45200,9 +45575,9 @@ async function runCompartmentAgent(deps) {
|
|
|
45200
45575
|
executeThresholdPercentage: boundarySnapshot.executeThresholdPercentage
|
|
45201
45576
|
});
|
|
45202
45577
|
if (!reserve.ok) {
|
|
45203
|
-
sessionLog(sessionId,
|
|
45578
|
+
sessionLog(sessionId, describeProtectedTailDrainBudgetSkip(reserve));
|
|
45204
45579
|
telemetry.status = "noop";
|
|
45205
|
-
telemetry.failureReason = "protected-tail drain
|
|
45580
|
+
telemetry.failureReason = "internal protected-tail drain budget spent";
|
|
45206
45581
|
return;
|
|
45207
45582
|
}
|
|
45208
45583
|
drainReservation = reserve.reservation;
|
|
@@ -45334,6 +45709,7 @@ ${chunkText}`,
|
|
|
45334
45709
|
return;
|
|
45335
45710
|
}
|
|
45336
45711
|
let published = false;
|
|
45712
|
+
const transactionStartedAt = performance.now();
|
|
45337
45713
|
db.exec("BEGIN IMMEDIATE");
|
|
45338
45714
|
try {
|
|
45339
45715
|
if (!isCompartmentLeaseHeld(db, sessionId, holderId)) {
|
|
@@ -45380,6 +45756,7 @@ ${chunkText}`,
|
|
|
45380
45756
|
}
|
|
45381
45757
|
db.exec("COMMIT");
|
|
45382
45758
|
published = true;
|
|
45759
|
+
logSlowWriteTransaction("historian-publish", transactionStartedAt);
|
|
45383
45760
|
} finally {
|
|
45384
45761
|
if (!published) {
|
|
45385
45762
|
try {
|
|
@@ -45518,6 +45895,7 @@ function insertRecompCompartmentRows(db, sessionId, compartments, now) {
|
|
|
45518
45895
|
}
|
|
45519
45896
|
function promoteRecompStagingWithM0Mutation(db, sessionId, holderId) {
|
|
45520
45897
|
const now = Date.now();
|
|
45898
|
+
const transactionStartedAt = performance.now();
|
|
45521
45899
|
db.exec("BEGIN IMMEDIATE");
|
|
45522
45900
|
let finished = false;
|
|
45523
45901
|
try {
|
|
@@ -45546,6 +45924,7 @@ function promoteRecompStagingWithM0Mutation(db, sessionId, holderId) {
|
|
|
45546
45924
|
clearCachedM0M1(db, sessionId);
|
|
45547
45925
|
db.exec("COMMIT");
|
|
45548
45926
|
finished = true;
|
|
45927
|
+
logSlowWriteTransaction("historian-publish:recomp", transactionStartedAt);
|
|
45549
45928
|
return { compartments: staging.compartments, facts: staging.facts };
|
|
45550
45929
|
} finally {
|
|
45551
45930
|
if (!finished) {
|
|
@@ -46885,4 +47264,4 @@ async function runUpgradeMemoryMigration(ctx, sessionId, migrationDirectory) {
|
|
|
46885
47264
|
}
|
|
46886
47265
|
}
|
|
46887
47266
|
|
|
46888
|
-
export { withContentLanguageDirective, buildPrimaryLanguageDirective, cortexKitUserConfigBasePath, cortexKitProjectConfigBasePath, resolveLegacyConfigSources, resolveLegacyConfigSourcesForHarness, migrateMagicContextConfigLocations, exports_external, DEFAULT_PROTECTED_TAGS, parseCron, nextOccurrence, nextDueAtMs, promptSurfaceConfigIdentity, resolvePromptSurface, resolveModelConfigOrDefault, DEFAULT_EXECUTE_THRESHOLD_PERCENTAGE, DEFAULT_HISTORIAN_TIMEOUT_MS, DEFAULT_LOCAL_EMBEDDING_MODEL, PER_HARNESS_MIGRATION_INVENTORY, ConfigProfilesSchema, MagicContextConfigSchema, getOpenCodeDbPath, removeMcOwnedCompactionMarkers, hasShareabilitySensitiveText, isFailClosedBlockingError, shouldBypassFailClosedBlock, resolveAgentNameFromMessages, createFailClosedController, recordHookInitFailure, clearHookInitFailure, getLastHookInitFailure, ProjectIdentityError, resolveProjectIdentityStrict, takeDubiousOwnershipProjectIdentityWarning, resolveProjectIdentity, resolveProjectIdentityForSession, normalizeStoredProjectPath, storedPathBelongsToIdentity, isLinkedGitWorktree, recordSessionProjectIdentity, beginBootQuietPeriod, bootQuietRemainingMs, scheduleAfterBootQuiet, rpcPortDir, rpcPortFilePath, isPidAlive, parseRpcPortFile, registerModuleNoteEvaluationBridge, getModuleNoteEvaluationBridge, getContextStoreUuid, ensureContextStoreUuid, getAuthorityManagedMarker, reconcileAuthorityProject, checksumAuthoritySeedRows, prepareAuthority, drainAuthority, getMirrorCursor, applyMirroredNoteCompileFields, applyMirrorPage, pullMemoryMirrorOnce, V2_MEMORY_CATEGORIES, CATEGORY_PRIORITY, resolveWorkspaceShareCategories, resolveWorkspaceIdentitySet, expandWorkspaceIdentitySetWithAliases, resolveStoredPathWorkspaceIdentity, storedPathBelongsToWorkspace, sourceNameForMemory, computeWorkspaceEpochFingerprint, textMentionsRecentCommit, hasMeaningfulUserText, extractTexts, extractToolCallSummaries, preloadTokenizer, estimateTokens, normalizeText, getCurrentToolSetHash, recordToolDefinition, getMeasuredToolDefinitionTokens, getSchemaFenceRejection, getMigrationOnOpenRefusal, LATEST_SUPPORTED_VERSION, getDatabasePath, getPersistedSchemaVersion, setSqlitePragmaConfig, runSqliteOptimize, openDatabase, openDatabaseAsync, isDatabasePersisted, getDatabasePersistenceError, COMPARTMENT_LEASE_RENEWAL_MS, acquireCompartmentLease, renewCompartmentLease, releaseCompartmentLease, getCompartments, getLastCompartmentEndMessage, getLastCompartmentEndMessageId, clearRecompStaging, isRawCompactionSummaryInfo, extractInMemoryMessageViews, buildInMemoryTailRawMessages, isRecord, estimateTrueRawMessageTokens, invalidateTrueRawTokenCache, stripPersistedAssistantText, byteSize, stripTagPrefix, prependTag, isThinkingPart, isTextPart, isToolPartWithOutput, isFilePart, buildFileSourceContent, isEditTool, partHasCompletedResult, extractToolCallObservation, ToolMutationBatch, createToolDropTarget, setRawMessageProvider, cleanUserText, withRawSessionMessageCache, readRawSessionMessages, primeTailRawMessageCache, primeInMemoryTailRawMessageCache, readRawSessionMessageOrdinalPage, getRawSessionStoredMessageCount, readRawSessionMessagePartsById, readRawSessionMessageOrdinalById, readRawSessionMessageById, readSessionChunk, getMessageIndexSourceIdentity, isMessageIndexSourceCurrent, getLastIndexedOrdinal, getIndexedMessageCorpusSize, getMessageIndexReconciliationStartOrdinal, isMessageIndexReconciledThrough, deleteIndexedMessage, clearIndexedMessages, indexSingleMessage, indexMessagesAfterOrdinal, sweepOrphanedOpenCodeMessageIndexes, recordEmbeddingMeasurement, getMaxM0MutationId, queueMemoryMutation, getMemoryMutationsForRenderByProjects, getMaxMemoryMutationIdForProjects, MAX_EXECUTE_THRESHOLD, escalationBands, isEmergencyRecoveryArmed, isProviderOverflowReconfirmed, getEmergencyRecoveryArmedAt, loadPersistedUsage, loadProtectedTailMeta, resetProtectedTailNoEligibleHead, getWrapupInProgressState, acquireWrapupInProgress, updateWrapupInProgress, releaseWrapupInProgress, resolveCompactionModeRecord, getCompactionModeRecord, setCompactionModeRecord, getPersistedReasoningWatermark, setPersistedReasoningWatermark, clearPersistedReasoningWatermark, getEmergencyInputSample, setEmergencyDropSample, clearEmergencyDropSample, getLastNudgeUndropped, setLastNudgeUndropped, getChannel1NudgeState, setChannel1NudgeState, resetLastNudgeCycle, resetLastNudgeCycleIfTailShrank, getChannel2NudgeState, getChannel2NudgeClaim, setChannel2NudgeState, casChannel2NudgeState, claimChannel2NudgeState, casChannel2NudgeClaim, getPersistedNoteNudge, getNoteNudgeAnchors, getAutoSearchHintDecisions, appendAutoSearchHintDecision, pruneNoteNudgeAnchors, pruneAutoSearchHintDecisions, removeNoteNudgeAnchorByMessageId, removeAutoSearchHintDecisionByMessageId, getPersistedTodoPermissionDenied, setPersistedTodoPermissionDenied, getPersistedTodoSyntheticAnchor, setPersistedTodoSyntheticAnchor, clearPersistedTodoSyntheticAnchor, setNoteLastReadAt, getHistorianFailureState, clearHistorianFailureState, getOverflowState, recordOverflowDetected, recordDetectedContextLimit, clearEmergencyRecovery, clearDetectedContextLimit, getPersistedCompactionMarkerState, setPersistedCompactionMarkerState, getStrippedPlaceholderIds, applyStrippedPlaceholderDelta, removeStrippedPlaceholderId, getMergedReasoningStrippedIds, addMergedReasoningStrippedIds, getTrailingBlankDecisions, addTrailingBlankDecisions, getStaleReduceStrippedIds, addStaleReduceStrippedIds, getProcessedImageStrippedIds, addProcessedImageStrippedIds, getPendingCompactionMarkerState, clearPendingCompactionMarkerStateIf, peekDeferredExecutePending, setDeferredExecutePendingIfAbsent, clearDeferredExecutePendingIfMatches, getSessionsWithPendingMarker, setSessionWorkMetrics, getNotes, addNote, getSessionNotes, getPendingSmartNotes, getReadySmartNotes, updateNote, dismissNote, markNoteReady, markNoteChecked, queuePendingOp, getPendingOps, clearPendingOps, removePendingOp, PRIMER_CANDIDATE_TTL_MS, PRIMER_CANDIDATE_MAX_AGE_MS, primerOccurrenceKey, primerOccurrenceUtcDay, updatePrimerCandidateEmbedding, getPrimerCandidatesByIds, getPrimerCandidatesForPromotion, countPrimerCandidatesForProject, getActivePrimers, createPrimer, updatePrimerSupport, updatePrimerAnswer, GLOBAL_USER_PROFILE_PROJECT_PATH, getProjectState, bumpProjectMemoryEpoch, bumpProjectUserProfileVersion, saveSourceContent, replaceSourceContent, getSourceContents, CTX_REDUCE_KEEP, estimateImageTokensFromDataUrl, normalizeTodoStateJson, buildSyntheticTodoPart, computeSyntheticCallId, isSyntheticTodoPart, countRealUserMessages, tailHygieneStructuralSignature, sameTailHygieneStructuralSignature, refreshTailHygieneBaseline, effectiveTailHygiene, assertTailHygieneContentUnchanged, CHANNEL1_SENTINEL, CHANNEL1_FLOOR_TOKENS, toolOutputTokens, decideChannel1, evaluateChannel2, buildChannel2Reminder, shouldUseStickyChannel1Reminder, buildChannel1Reminder, estimateEmergencyDropReclaimTokens, planEmergencyDrop, AGE_RECLAIM_MIN_TOKENS, getOldestActiveUnprotectedToolTags, getActiveToolTagsForAgeReclaim, getTriggerTagTokenUpperBound, getActiveTagTokenTotalsByMessage, tagTokenCountIsNull, backfillTagTokenCounts, insertTag, updateTagStatus, updateTagDropMode, updateCavemanDepth, updateTagMessageId, deleteTagsByMessageId, getMaxTagNumberBySession, getTagNumberByMessageId, deriveTagLoadFloor, getTagsBySession, getActiveTagsBySession, getTailHygieneTags, getDroppedTagsBySession, getTagsByNumbers, getMaxDroppedTagNumber, getToolTagNumberByOwner, getNullOwnerToolTag, adoptNullOwnerToolTag, getCandidateToolOwners, pickNearestPriorOwner, createChildSessionWithFence, detectOverflow, resolveFallbackChain, modelBodyField, promptSyncWithValidatedOutputRetry, normalizeSDKResponse, promptSurfaceHashMaterial, createPromptSurfaceRuntime, createPromptSurfaceGuidanceEpochCache, extractLatestAssistantText, hasLengthCappedOutput, setKeepSubagents, shouldKeepSubagents, recordChildInvocation, COMPARTMENT_AGENT_SYSTEM_PROMPT, HISTORIAN_EDITOR_SYSTEM_PROMPT, COMPARTMENT_STRUCTURAL_SYSTEM_PROMPT, Priority, AdmissionClass, StaleRouteHandleError, SocketClosedError, SocketTimeoutError, SubcClient, isConsumerReconnectTransient, connectionFileExists, getCompartmentEvents, loadCompartmentChunkEmbeddingsForSearch, cosineSimilarity, SYNAPSE_DEFAULT_MODEL, SynapseEmbeddingProvider, computeNormalizedHash, saveCommitEmbedding, loadProjectCommitEmbeddings, loadUnembeddedCommits, countEmbeddedCommits, upsertCommits, getLatestIndexedCommitTimeMs, enforceProjectCap, GIT_SWEEP_LEASE_RENEWAL_MS, acquireGitSweepLease, renewGitSweepLease, markGitSweepSuccessAndRelease, parkGitSweepNonIndexable, releaseGitSweepLease, saveEmbeddingIfHashMatches, getProjectEmbeddings, peekProjectEmbeddings, invalidateProject, invalidateMemory, markProjectLoadUntrusted, contentSha256, sweepStaleEmbeddingIdentitiesForProject, registerProjectEmbedding, registerProjectShadowEmbedding, getShadowEmbeddingMeasurementCohort, getPrimaryEmbeddingMeasurementCohort, embedShadowTextForProject, enqueueShadowEmbeddingItems, registerProjectInObservationMode, getProjectEmbeddingSnapshot, embedTextForProject, embedBatchForProject, embedItemsForProject, embedUnembeddedMemoriesForProject, drainCommitBacklogForProject, embedSessionCompartmentChunks, getEmbeddingCoverageStatus, isEmbeddingEnabled, embedText, ensureMemoryEmbeddings, computeCueContentHash, hasMuralCueColumns, getMuralCueState, memoryNeedsCue, setMuralCue, recordMuralCueRejection, hasMemoryShareableColumn, hasMemoryClassifiedAtColumn, getUnclassifiedMemoryIds, ModuleMemoryAuthorityError, insertMemory, insertMemoryIdempotent, getMemoryByHash, getMemoriesByProject, buildWorkspaceMemorySqlFilter, getMemoriesByProjects, getMaxMemoryIdForProjects, readNewMemoriesForM1Union, getMemoryById, getMemoriesByIds, updateMemorySeenCount, updateMemoryRetrievalCount, setMemoryClassification, supersededMemory, mergeMemoryStats, archiveMemory, getMemoryCountsByStatus, sanitizeFtsQuery, searchMemoriesFTS, searchMemoriesFTSUnion, recordMemoryMapping, recordMemoryVerifications, getUnmappedMemoryIds, clearMemoryVerifications, getMemoryVerifications, resolveGitTopLevel, readGitHead, readGitChangedFilesSince, readGitFileChangeTimesSince, verificationFileExists, normalizeVerificationFiles, USER_MEMORY_CANDIDATE_TTL_MS, insertUserMemoryCandidates, getUserMemoryCandidates, deleteUserMemoryCandidates, pruneExpiredUserMemoryCandidates, insertUserMemory, getActiveUserMemories, updateUserMemoryContent, dismissUserMemory, getMural, BoundedSessionMap, registerLkgPersistence, LKG_SNAPSHOT_ARRAY, LKG_SNAPSHOT_OBJECT, LKG_SNAPSHOT_KEY, LKG_SNAPSHOT_STRING, LKG_SNAPSHOT_NUMBER, LKG_SNAPSHOT_BOOLEAN, LKG_SNAPSHOT_NULL, LKG_SNAPSHOT_UNDEFINED, incrementalLkgContentDigests, lkgContentDigest, captureSlot, getSlot, dropSlot, noteEntry, onNoteTrigger, peekNoteNudgeText, markNoteNudgeDelivered, clearNoteNudgeTriggerAndCooldown, resetNoteNudgeCooldownOnly, clearNoteNudgeTriggerOnly, extractM0Block, resolveMuralWire, MARKER_SUMMARY_TEXT, applyDeferredCompactionMarker, removeCompactionMarkerForSession, checkCompactionMarkerConsistency, formatDate, injectTemporalMarkers, clearInjectionCache, getVisibleMemoryIds, prepareCompartmentInjection, renderCompartmentInjection, mustMaterialize, trimMemoriesToBudgetV2, renderMemoryBlockV2, injectM0M1, RECOVERY_NO_HEAD_LIMIT, resolveOpenCodeProtectedTailBoundary, resolveWrapupProtectedTailBoundary, getRawHistoryEligibility, hasRunnableCompartmentWindow, recordHighPressureNoEligibleHead, runCompartmentAgent, snapRangeToCompartments, getActiveCompartmentRun, markActiveCompartmentRunPublished, registerActiveCompartmentRun, startCompartmentAgent, isRecompFailure, isRecompSkip, isRecompComplete, extractRecompReason, contextualizeUpgradeReason, setRecompStarting, setRecompNote, setRecompTerminal, runManagedRecomp, runManagedUpgrade };
|
|
47267
|
+
export { withContentLanguageDirective, buildPrimaryLanguageDirective, cortexKitUserConfigBasePath, cortexKitProjectConfigBasePath, resolveLegacyConfigSources, resolveLegacyConfigSourcesForHarness, migrateMagicContextConfigLocations, exports_external, DEFAULT_PROTECTED_TAGS, parseCron, nextOccurrence, nextDueAtMs, promptSurfaceConfigIdentity, resolvePromptSurface, resolveModelConfigOrDefault, DEFAULT_EXECUTE_THRESHOLD_PERCENTAGE, DEFAULT_HISTORIAN_TIMEOUT_MS, DEFAULT_LOCAL_EMBEDDING_MODEL, PER_HARNESS_MIGRATION_INVENTORY, ConfigProfilesSchema, MagicContextConfigSchema, getOpenCodeDbPath, removeMcOwnedCompactionMarkers, isFailClosedBlockingError, shouldBypassFailClosedBlock, resolveAgentNameFromMessages, createFailClosedController, recordHookInitFailure, clearHookInitFailure, getLastHookInitFailure, ProjectIdentityError, resolveProjectIdentityStrict, takeDubiousOwnershipProjectIdentityWarning, resolveProjectIdentity, directoryHasGitMetadata, resolveProjectIdentityForSession, normalizeStoredProjectPath, storedPathBelongsToIdentity, isLinkedGitWorktree, recordSessionProjectIdentity, beginBootQuietPeriod, bootQuietRemainingMs, scheduleAfterBootQuiet, rpcPortDir, rpcPortFilePath, isPidAlive, parseRpcPortFile, observeAuthorityRouting, registerModuleNoteEvaluationBridge, getModuleNoteEvaluationBridge, getContextStoreUuid, ensureContextStoreUuid, getAuthorityManagedMarker, reconcileAuthorityProject, checksumAuthoritySeedRows, prepareAuthority, drainAuthority, getMirrorCursor, applyMirroredNoteCompileFields, applyMirrorPage, pullMemoryMirrorOnce, V2_MEMORY_CATEGORIES, CATEGORY_PRIORITY, resolveWorkspaceShareCategories, resolveWorkspaceIdentitySet, expandWorkspaceIdentitySetWithAliases, resolveStoredPathWorkspaceIdentity, storedPathBelongsToWorkspace, sourceNameForMemory, computeWorkspaceEpochFingerprint, textMentionsRecentCommit, hasMeaningfulUserText, extractTexts, extractToolCallSummaries, preloadTokenizer, estimateTokens, normalizeText, getCurrentToolSetHash, recordToolDefinition, getMeasuredToolDefinitionTokens, getSchemaFenceRejection, getMigrationOnOpenRefusal, LATEST_SUPPORTED_VERSION, getDatabasePath, getPersistedSchemaVersion, setSqlitePragmaConfig, runSqliteOptimize, openDatabase, openDatabaseAsync, isDatabasePersisted, getDatabasePersistenceError, COMPARTMENT_LEASE_RENEWAL_MS, acquireCompartmentLease, renewCompartmentLease, releaseCompartmentLease, getCompartments, getLastCompartmentEndMessage, getLastCompartmentEndMessageId, clearRecompStaging, isRawCompactionSummaryInfo, extractInMemoryMessageViews, buildInMemoryTailRawMessages, isRecord, estimateTrueRawMessageTokens, invalidateTrueRawTokenCache, stripPersistedAssistantText, byteSize, stripTagPrefix, prependTag, isThinkingPart, isTextPart, isToolPartWithOutput, isFilePart, buildFileSourceContent, isEditTool, partHasCompletedResult, extractToolCallObservation, ToolMutationBatch, createToolDropTarget, setRawMessageProvider, cleanUserText, withRawSessionMessageCache, readRawSessionMessages, primeTailRawMessageCache, primeInMemoryTailRawMessageCache, readRawSessionMessageOrdinalPage, getRawSessionStoredMessageCount, readRawSessionMessagePartsById, readRawSessionMessageOrdinalById, readRawSessionMessageById, readSessionChunk, getMessageIndexSourceIdentity, isMessageIndexSourceCurrent, getLastIndexedOrdinal, getIndexedMessageCorpusSize, getMessageIndexReconciliationStartOrdinal, isMessageIndexReconciledThrough, deleteIndexedMessage, clearIndexedMessages, indexSingleMessage, indexMessagesAfterOrdinal, sweepOrphanedOpenCodeMessageIndexes, recordEmbeddingMeasurement, getMaxM0MutationId, queueMemoryMutation, getMemoryMutationsForRenderByProjects, getMaxMemoryMutationIdForProjects, MAX_EXECUTE_THRESHOLD, escalationBands, isEmergencyRecoveryArmed, isProviderOverflowReconfirmed, getEmergencyRecoveryArmedAt, loadPersistedUsage, loadProtectedTailMeta, resetProtectedTailNoEligibleHead, getWrapupInProgressState, acquireWrapupInProgress, updateWrapupInProgress, releaseWrapupInProgress, resolveCompactionModeRecord, getCompactionModeRecord, setCompactionModeRecord, getPersistedReasoningWatermark, setPersistedReasoningWatermark, clearPersistedReasoningWatermark, getEmergencyInputSample, setEmergencyDropSample, clearEmergencyDropSample, getLastNudgeUndropped, setLastNudgeUndropped, getChannel1NudgeState, setChannel1NudgeState, markChannel1PostReduceGracePending, captureChannel1PostReduceGraceBaseline, getChannel2NudgeState, getChannel2NudgeClaim, setChannel2NudgeState, casChannel2NudgeState, claimChannel2NudgeState, casChannel2NudgeClaim, getPersistedNoteNudge, getNoteNudgeAnchors, getAutoSearchHintDecisions, appendAutoSearchHintDecision, pruneNoteNudgeAnchors, pruneAutoSearchHintDecisions, removeNoteNudgeAnchorByMessageId, removeAutoSearchHintDecisionByMessageId, getPersistedTodoPermissionDenied, setPersistedTodoPermissionDenied, getPersistedTodoSyntheticAnchor, setPersistedTodoSyntheticAnchor, clearPersistedTodoSyntheticAnchor, setNoteLastReadAt, getHistorianFailureState, clearHistorianFailureState, getOverflowState, recordOverflowDetected, recordDetectedContextLimit, clearEmergencyRecovery, clearDetectedContextLimit, getPersistedCompactionMarkerState, setPersistedCompactionMarkerState, getStrippedPlaceholderIds, applyStrippedPlaceholderDelta, removeStrippedPlaceholderId, getMergedReasoningStrippedIds, addMergedReasoningStrippedIds, getTrailingBlankDecisions, addTrailingBlankDecisions, demoteTrailingBlankKeepDecisions, getStaleReduceStrippedIds, addStaleReduceStrippedIds, getProcessedImageStrippedIds, addProcessedImageStrippedIds, getPendingCompactionMarkerState, clearPendingCompactionMarkerStateIf, peekDeferredExecutePending, setDeferredExecutePendingIfAbsent, clearDeferredExecutePendingIfMatches, getSessionsWithPendingMarker, setSessionWorkMetrics, getNotes, addNote, getSessionNotes, getPendingSmartNotes, getReadySmartNotes, updateNote, dismissNote, markNoteReady, markNoteChecked, queuePendingOp, getPendingOps, getPendingOpsCount, clearPendingOps, removePendingOp, PRIMER_CANDIDATE_TTL_MS, PRIMER_CANDIDATE_MAX_AGE_MS, primerOccurrenceKey, primerOccurrenceUtcDay, updatePrimerCandidateEmbedding, getPrimerCandidatesByIds, getPrimerCandidatesForPromotion, countPrimerCandidatesForProject, getActivePrimers, createPrimer, updatePrimerSupport, updatePrimerAnswer, GLOBAL_USER_PROFILE_PROJECT_PATH, getProjectState, bumpProjectMemoryEpoch, bumpProjectUserProfileVersion, saveSourceContent, replaceSourceContent, getSourceContents, CTX_REDUCE_KEEP, estimateImageTokensFromDataUrl, normalizeTodoStateJson, buildSyntheticTodoPart, computeSyntheticCallId, isSyntheticTodoPart, countRealUserMessages, tailHygieneStructuralSignature, sameTailHygieneStructuralSignature, refreshTailHygieneBaseline, effectiveTailHygiene, assertTailHygieneContentUnchanged, CHANNEL1_SENTINEL, CHANNEL1_FLOOR_TOKENS, toolOutputTokens, decideChannel1, evaluateChannel2, reclaimableToolOutputCount, buildChannel2Reminder, buildChannel1Reminder, estimateEmergencyDropReclaimTokens, planEmergencyDrop, getRecentTagOwnerMessageIds, AGE_RECLAIM_MIN_TOKENS, getOldestActiveUnprotectedToolTags, getActiveToolTagsForAgeReclaim, getTriggerTagTokenUpperBound, getActiveTagTokenTotalsByMessage, tagTokenCountIsNull, backfillTagTokenCounts, insertTag, updateTagStatus, updateTagDropMode, updateCavemanDepth, updateTagMessageId, deleteTagsByMessageId, getMaxTagNumberBySession, getTagNumberByMessageId, deriveTagLoadFloor, getTagsBySession, getActiveTagsBySession, getTailHygieneTags, getDroppedTagsBySession, getTagsByNumbers, getMaxDroppedTagNumber, getToolTagNumberByOwner, getNullOwnerToolTag, adoptNullOwnerToolTag, getCandidateToolOwners, pickNearestPriorOwner, createChildSessionWithFence, detectOverflow, resolveFallbackChain, modelBodyField, toModelEntry, promptSyncWithValidatedOutputRetry, normalizeSDKResponse, promptSurfaceHashMaterial, createPromptSurfaceRuntime, createPromptSurfaceGuidanceEpochCache, extractLatestAssistantText, hasLengthCappedOutput, setKeepSubagents, shouldKeepSubagents, recordChildInvocation, COMPARTMENT_AGENT_SYSTEM_PROMPT, HISTORIAN_EDITOR_SYSTEM_PROMPT, COMPARTMENT_STRUCTURAL_SYSTEM_PROMPT, Priority, AdmissionClass, StaleRouteHandleError, SocketClosedError, SocketTimeoutError, SubcClient, isConsumerReconnectTransient, connectionFileExists, getCompartmentEvents, loadCompartmentChunkEmbeddingsForSearch, cosineSimilarity, SYNAPSE_DEFAULT_MODEL, SynapseEmbeddingProvider, computeNormalizedHash, saveCommitEmbedding, loadProjectCommitEmbeddings, loadUnembeddedCommits, countEmbeddedCommits, upsertCommits, getLatestIndexedCommitTimeMs, enforceProjectCap, GIT_SWEEP_LEASE_RENEWAL_MS, acquireGitSweepLease, renewGitSweepLease, markGitSweepSuccessAndRelease, parkGitSweepNonIndexable, releaseGitSweepLease, saveEmbeddingIfHashMatches, getProjectEmbeddings, peekProjectEmbeddings, invalidateProject, invalidateMemory, markProjectLoadUntrusted, contentSha256, sweepStaleEmbeddingIdentitiesForProject, registerProjectEmbedding, registerProjectShadowEmbedding, getShadowEmbeddingMeasurementCohort, getPrimaryEmbeddingMeasurementCohort, embedShadowTextForProject, enqueueShadowEmbeddingItems, registerProjectInObservationMode, getProjectEmbeddingSnapshot, embedTextForProject, embedBatchForProject, embedItemsForProject, embedUnembeddedMemoriesForProject, drainCommitBacklogForProject, embedSessionCompartmentChunks, getEmbeddingCoverageStatus, isEmbeddingEnabled, embedText, ensureMemoryEmbeddings, computeCueContentHash, hasMuralCueColumns, getMuralCueState, memoryNeedsCue, setMuralCue, recordMuralCueRejection, hasMemoryShareableColumn, hasMemoryClassifiedAtColumn, getUnclassifiedMemoryIds, ModuleMemoryAuthorityError, insertMemory, insertMemoryIdempotent, getMemoryByHash, getMemoriesByProject, buildWorkspaceMemorySqlFilter, getMemoriesByProjects, getMaxMemoryIdForProjects, readNewMemoriesForM1Union, getMemoryById, getMemoriesByIds, updateMemorySeenCount, updateMemoryRetrievalCount, setMemoryClassification, supersededMemory, mergeMemoryStats, archiveMemory, getMemoryCountsByStatus, sanitizeFtsQuery, searchMemoriesFTS, searchMemoriesFTSUnion, recordMemoryMapping, recordMemoryVerifications, getUnmappedMemoryIds, clearMemoryVerifications, getMemoryVerifications, resolveGitTopLevel, readGitHead, readGitChangedFilesSince, readGitFileChangeTimesSince, verificationFileExists, normalizeVerificationFiles, USER_MEMORY_CANDIDATE_TTL_MS, insertUserMemoryCandidates, getUserMemoryCandidates, deleteUserMemoryCandidates, pruneExpiredUserMemoryCandidates, insertUserMemory, getActiveUserMemories, updateUserMemoryContent, dismissUserMemory, getMural, BoundedSessionMap, registerLkgPersistence, LKG_SNAPSHOT_ARRAY, LKG_SNAPSHOT_OBJECT, LKG_SNAPSHOT_KEY, LKG_SNAPSHOT_STRING, LKG_SNAPSHOT_NUMBER, LKG_SNAPSHOT_BOOLEAN, LKG_SNAPSHOT_NULL, LKG_SNAPSHOT_UNDEFINED, incrementalLkgContentDigests, lkgContentDigest, captureSlot, getSlot, dropSlot, noteEntry, onNoteTrigger, peekNoteNudgeText, markNoteNudgeDelivered, clearNoteNudgeTriggerAndCooldown, resetNoteNudgeCooldownOnly, clearNoteNudgeTriggerOnly, MARKER_SUMMARY_TEXT, applyDeferredCompactionMarker, removeCompactionMarkerForSession, checkCompactionMarkerConsistency, resolveMuralWire, extractM0Block, formatDate, injectTemporalMarkers, clearInjectionCache, getVisibleMemoryIds, prepareCompartmentInjection, renderCompartmentInjection, mustMaterialize, trimMemoriesToBudgetV2, renderMemoryBlockV2, injectM0M1, RECOVERY_NO_HEAD_LIMIT, resolveOpenCodeProtectedTailBoundary, resolveWrapupProtectedTailBoundary, getRawHistoryEligibility, hasRunnableCompartmentWindow, recordHighPressureNoEligibleHead, runCompartmentAgent, snapRangeToCompartments, getActiveCompartmentRun, markActiveCompartmentRunPublished, registerActiveCompartmentRun, startCompartmentAgent, isRecompFailure, isRecompSkip, isRecompComplete, extractRecompReason, contextualizeUpgradeReason, setRecompStarting, setRecompNote, setRecompTerminal, runManagedRecomp, runManagedUpgrade };
|