@vellumai/assistant 0.4.49 → 0.4.50
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/ARCHITECTURE.md +24 -33
- package/README.md +3 -3
- package/docs/architecture/memory.md +180 -119
- package/package.json +2 -2
- package/src/__tests__/agent-loop.test.ts +3 -1
- package/src/__tests__/anthropic-provider.test.ts +114 -23
- package/src/__tests__/approval-cascade.test.ts +1 -15
- package/src/__tests__/approval-routes-http.test.ts +2 -0
- package/src/__tests__/assistant-feature-flag-guard.test.ts +0 -23
- package/src/__tests__/canonical-guardian-store.test.ts +95 -0
- package/src/__tests__/checker.test.ts +13 -0
- package/src/__tests__/config-schema.test.ts +1 -68
- package/src/__tests__/context-memory-e2e.test.ts +11 -100
- package/src/__tests__/conversation-routes-guardian-reply.test.ts +8 -0
- package/src/__tests__/conversation-routes-slash-commands.test.ts +1 -0
- package/src/__tests__/credential-security-e2e.test.ts +1 -0
- package/src/__tests__/credential-vault-unit.test.ts +4 -0
- package/src/__tests__/credential-vault.test.ts +13 -1
- package/src/__tests__/cu-unified-flow.test.ts +532 -0
- package/src/__tests__/date-context.test.ts +93 -77
- package/src/__tests__/deterministic-verification-control-plane.test.ts +64 -0
- package/src/__tests__/guardian-routing-invariants.test.ts +93 -0
- package/src/__tests__/history-repair.test.ts +245 -0
- package/src/__tests__/host-cu-proxy.test.ts +165 -3
- package/src/__tests__/http-user-message-parity.test.ts +1 -0
- package/src/__tests__/invite-redemption-service.test.ts +65 -1
- package/src/__tests__/keychain-broker-client.test.ts +4 -4
- package/src/__tests__/memory-context-benchmark.benchmark.test.ts +56 -18
- package/src/__tests__/memory-lifecycle-e2e.test.ts +244 -387
- package/src/__tests__/memory-recall-quality.test.ts +244 -407
- package/src/__tests__/memory-regressions.experimental.test.ts +126 -101
- package/src/__tests__/memory-regressions.test.ts +477 -2841
- package/src/__tests__/memory-retrieval.benchmark.test.ts +33 -150
- package/src/__tests__/memory-upsert-concurrency.test.ts +5 -244
- package/src/__tests__/mime-builder.test.ts +28 -0
- package/src/__tests__/native-web-search.test.ts +1 -0
- package/src/__tests__/oauth-cli.test.ts +572 -5
- package/src/__tests__/oauth-store.test.ts +120 -6
- package/src/__tests__/qdrant-collection-migration.test.ts +53 -8
- package/src/__tests__/registry.test.ts +0 -1
- package/src/__tests__/relay-server.test.ts +46 -1
- package/src/__tests__/schedule-tools.test.ts +32 -0
- package/src/__tests__/script-proxy-certs.test.ts +1 -1
- package/src/__tests__/secret-onetime-send.test.ts +1 -0
- package/src/__tests__/secure-keys.test.ts +7 -2
- package/src/__tests__/send-endpoint-busy.test.ts +3 -0
- package/src/__tests__/session-abort-tool-results.test.ts +1 -14
- package/src/__tests__/session-agent-loop-overflow.test.ts +1583 -0
- package/src/__tests__/session-agent-loop.test.ts +19 -15
- package/src/__tests__/session-confirmation-signals.test.ts +1 -15
- package/src/__tests__/session-error.test.ts +124 -2
- package/src/__tests__/session-history-web-search.test.ts +918 -0
- package/src/__tests__/session-pre-run-repair.test.ts +1 -14
- package/src/__tests__/session-provider-retry-repair.test.ts +25 -28
- package/src/__tests__/session-queue.test.ts +37 -27
- package/src/__tests__/session-runtime-assembly.test.ts +54 -0
- package/src/__tests__/session-slash-known.test.ts +1 -15
- package/src/__tests__/session-slash-queue.test.ts +1 -15
- package/src/__tests__/session-slash-unknown.test.ts +1 -15
- package/src/__tests__/session-workspace-cache-state.test.ts +3 -33
- package/src/__tests__/session-workspace-injection.test.ts +3 -37
- package/src/__tests__/session-workspace-tool-tracking.test.ts +3 -37
- package/src/__tests__/skills-install-extract.test.ts +93 -0
- package/src/__tests__/skillssh-registry.test.ts +451 -0
- package/src/__tests__/trust-store.test.ts +15 -0
- package/src/__tests__/voice-invite-redemption.test.ts +32 -1
- package/src/agent/ax-tree-compaction.test.ts +51 -0
- package/src/agent/loop.ts +39 -12
- package/src/approvals/AGENTS.md +1 -1
- package/src/approvals/guardian-request-resolvers.ts +14 -2
- package/src/bundler/compiler-tools.ts +66 -2
- package/src/calls/call-domain.ts +132 -0
- package/src/calls/call-store.ts +6 -0
- package/src/calls/relay-server.ts +43 -5
- package/src/calls/relay-setup-router.ts +17 -1
- package/src/calls/twilio-config.ts +1 -1
- package/src/calls/types.ts +3 -1
- package/src/cli/commands/doctor.ts +4 -3
- package/src/cli/commands/mcp.ts +46 -59
- package/src/cli/commands/memory.ts +16 -165
- package/src/cli/commands/oauth/apps.ts +31 -2
- package/src/cli/commands/oauth/connections.ts +431 -97
- package/src/cli/commands/oauth/providers.ts +15 -1
- package/src/cli/commands/sessions.ts +5 -2
- package/src/cli/commands/skills.ts +173 -1
- package/src/cli/http-client.ts +0 -20
- package/src/cli/main-screen.tsx +2 -2
- package/src/cli/program.ts +5 -6
- package/src/cli.ts +4 -10
- package/src/config/bundled-skills/computer-use/TOOLS.json +1 -1
- package/src/config/bundled-skills/computer-use/tools/computer-use-observe.ts +12 -0
- package/src/config/bundled-tool-registry.ts +2 -5
- package/src/config/schema.ts +1 -12
- package/src/config/schemas/memory-lifecycle.ts +0 -9
- package/src/config/schemas/memory-processing.ts +0 -180
- package/src/config/schemas/memory-retrieval.ts +32 -104
- package/src/config/schemas/memory.ts +0 -10
- package/src/config/types.ts +0 -4
- package/src/context/window-manager.ts +4 -1
- package/src/daemon/config-watcher.ts +61 -3
- package/src/daemon/daemon-control.ts +1 -1
- package/src/daemon/date-context.ts +114 -31
- package/src/daemon/handlers/sessions.ts +18 -13
- package/src/daemon/handlers/skills.ts +20 -1
- package/src/daemon/history-repair.ts +72 -8
- package/src/daemon/host-cu-proxy.ts +55 -26
- package/src/daemon/lifecycle.ts +31 -3
- package/src/daemon/mcp-reload-service.ts +2 -2
- package/src/daemon/message-types/computer-use.ts +1 -12
- package/src/daemon/message-types/memory.ts +4 -16
- package/src/daemon/message-types/messages.ts +1 -0
- package/src/daemon/message-types/sessions.ts +4 -0
- package/src/daemon/server.ts +12 -1
- package/src/daemon/session-agent-loop-handlers.ts +38 -0
- package/src/daemon/session-agent-loop.ts +334 -48
- package/src/daemon/session-error.ts +89 -6
- package/src/daemon/session-history.ts +17 -7
- package/src/daemon/session-media-retry.ts +6 -2
- package/src/daemon/session-memory.ts +69 -149
- package/src/daemon/session-process.ts +10 -1
- package/src/daemon/session-runtime-assembly.ts +49 -19
- package/src/daemon/session-surfaces.ts +4 -1
- package/src/daemon/session-tool-setup.ts +7 -1
- package/src/daemon/session.ts +12 -2
- package/src/instrument.ts +61 -1
- package/src/memory/admin.ts +2 -191
- package/src/memory/canonical-guardian-store.ts +38 -2
- package/src/memory/conversation-crud.ts +0 -33
- package/src/memory/conversation-queries.ts +22 -3
- package/src/memory/db-init.ts +28 -0
- package/src/memory/embedding-backend.ts +84 -8
- package/src/memory/embedding-types.ts +9 -1
- package/src/memory/indexer.ts +7 -46
- package/src/memory/items-extractor.ts +274 -76
- package/src/memory/job-handlers/backfill.ts +2 -127
- package/src/memory/job-handlers/cleanup.ts +2 -16
- package/src/memory/job-handlers/extraction.ts +2 -138
- package/src/memory/job-handlers/index-maintenance.ts +1 -6
- package/src/memory/job-handlers/summarization.ts +3 -148
- package/src/memory/job-utils.ts +21 -59
- package/src/memory/jobs-store.ts +1 -159
- package/src/memory/jobs-worker.ts +9 -52
- package/src/memory/migrations/104-core-indexes.ts +3 -3
- package/src/memory/migrations/149-oauth-tables.ts +2 -0
- package/src/memory/migrations/150-oauth-apps-client-secret-path.ts +98 -0
- package/src/memory/migrations/151-oauth-providers-ping-url.ts +11 -0
- package/src/memory/migrations/152-memory-item-supersession.ts +44 -0
- package/src/memory/migrations/153-drop-entity-tables.ts +15 -0
- package/src/memory/migrations/154-drop-fts.ts +20 -0
- package/src/memory/migrations/155-drop-conflicts.ts +7 -0
- package/src/memory/migrations/156-call-session-invite-metadata.ts +24 -0
- package/src/memory/migrations/index.ts +7 -0
- package/src/memory/qdrant-client.ts +148 -51
- package/src/memory/raw-query.ts +1 -1
- package/src/memory/retriever.test.ts +294 -273
- package/src/memory/retriever.ts +421 -645
- package/src/memory/schema/calls.ts +2 -0
- package/src/memory/schema/memory-core.ts +3 -48
- package/src/memory/schema/oauth.ts +2 -0
- package/src/memory/search/formatting.ts +263 -176
- package/src/memory/search/lexical.ts +1 -254
- package/src/memory/search/ranking.ts +0 -455
- package/src/memory/search/semantic.ts +100 -14
- package/src/memory/search/staleness.ts +47 -0
- package/src/memory/search/tier-classifier.ts +21 -0
- package/src/memory/search/types.ts +15 -77
- package/src/memory/task-memory-cleanup.ts +4 -6
- package/src/messaging/providers/gmail/mime-builder.ts +17 -7
- package/src/oauth/byo-connection.test.ts +8 -1
- package/src/oauth/oauth-store.ts +113 -27
- package/src/oauth/seed-providers.ts +6 -0
- package/src/oauth/token-persistence.ts +11 -3
- package/src/permissions/defaults.ts +1 -0
- package/src/permissions/trust-store.ts +23 -1
- package/src/playbooks/playbook-compiler.ts +1 -1
- package/src/prompts/system-prompt.ts +18 -2
- package/src/providers/anthropic/client.ts +56 -126
- package/src/providers/types.ts +7 -1
- package/src/runtime/AGENTS.md +9 -0
- package/src/runtime/auth/route-policy.ts +6 -3
- package/src/runtime/guardian-reply-router.ts +24 -22
- package/src/runtime/http-server.ts +2 -2
- package/src/runtime/invite-redemption-service.ts +19 -1
- package/src/runtime/invite-service.ts +25 -0
- package/src/runtime/pending-interactions.ts +2 -2
- package/src/runtime/routes/brain-graph-routes.ts +10 -90
- package/src/runtime/routes/conversation-routes.ts +9 -1
- package/src/runtime/routes/inbound-stages/acl-enforcement.ts +21 -12
- package/src/runtime/routes/memory-item-routes.test.ts +754 -0
- package/src/runtime/routes/memory-item-routes.ts +503 -0
- package/src/runtime/routes/session-management-routes.ts +3 -3
- package/src/runtime/routes/settings-routes.ts +2 -2
- package/src/runtime/routes/trust-rules-routes.ts +14 -0
- package/src/runtime/routes/workspace-routes.ts +2 -1
- package/src/security/keychain-broker-client.ts +17 -4
- package/src/security/secure-keys.ts +25 -3
- package/src/security/token-manager.ts +36 -36
- package/src/skills/catalog-install.ts +74 -18
- package/src/skills/skillssh-registry.ts +503 -0
- package/src/tools/assets/search.ts +5 -1
- package/src/tools/computer-use/definitions.ts +0 -10
- package/src/tools/computer-use/registry.ts +1 -1
- package/src/tools/credentials/vault.ts +1 -3
- package/src/tools/memory/definitions.ts +4 -13
- package/src/tools/memory/handlers.test.ts +83 -103
- package/src/tools/memory/handlers.ts +50 -85
- package/src/tools/schedule/create.ts +8 -1
- package/src/tools/schedule/update.ts +8 -1
- package/src/tools/skills/load.ts +25 -2
- package/src/__tests__/clarification-resolver.test.ts +0 -193
- package/src/__tests__/conflict-intent-tokenization.test.ts +0 -160
- package/src/__tests__/conflict-policy.test.ts +0 -269
- package/src/__tests__/conflict-store.test.ts +0 -372
- package/src/__tests__/contradiction-checker.test.ts +0 -361
- package/src/__tests__/entity-extractor.test.ts +0 -211
- package/src/__tests__/entity-search.test.ts +0 -1117
- package/src/__tests__/profile-compiler.test.ts +0 -392
- package/src/__tests__/session-conflict-gate.test.ts +0 -1228
- package/src/__tests__/session-profile-injection.test.ts +0 -557
- package/src/config/bundled-skills/knowledge-graph/SKILL.md +0 -25
- package/src/config/bundled-skills/knowledge-graph/TOOLS.json +0 -66
- package/src/config/bundled-skills/knowledge-graph/tools/graph-query.ts +0 -211
- package/src/daemon/session-conflict-gate.ts +0 -167
- package/src/daemon/session-dynamic-profile.ts +0 -77
- package/src/memory/clarification-resolver.ts +0 -417
- package/src/memory/conflict-intent.ts +0 -205
- package/src/memory/conflict-policy.ts +0 -127
- package/src/memory/conflict-store.ts +0 -410
- package/src/memory/contradiction-checker.ts +0 -508
- package/src/memory/entity-extractor.ts +0 -535
- package/src/memory/format-recall.ts +0 -47
- package/src/memory/fts-reconciler.ts +0 -165
- package/src/memory/job-handlers/conflict.ts +0 -200
- package/src/memory/profile-compiler.ts +0 -195
- package/src/memory/recall-cache.ts +0 -117
- package/src/memory/search/entity.ts +0 -535
- package/src/memory/search/query-expansion.test.ts +0 -70
- package/src/memory/search/query-expansion.ts +0 -118
- package/src/runtime/routes/mcp-routes.ts +0 -20
|
@@ -733,31 +733,60 @@ export function injectChannelTurnContext(
|
|
|
733
733
|
export function buildInboundActorContextBlock(
|
|
734
734
|
ctx: InboundActorContext,
|
|
735
735
|
): string {
|
|
736
|
+
const sanitizeInlineContextValue = (
|
|
737
|
+
value: string | null | undefined,
|
|
738
|
+
): string => {
|
|
739
|
+
if (!value) {
|
|
740
|
+
return "unknown";
|
|
741
|
+
}
|
|
742
|
+
const singleLine = value
|
|
743
|
+
// Replace ASCII and Unicode line/paragraph separators.
|
|
744
|
+
.replace(/[\r\n\u0085\u2028\u2029]+/g, " ")
|
|
745
|
+
// Replace remaining ASCII C0/C1 control characters and DEL.
|
|
746
|
+
.replace(/[\x00-\x1F\x7F-\x9F]/g, " ")
|
|
747
|
+
.trim();
|
|
748
|
+
return singleLine.length > 0 ? singleLine : "unknown";
|
|
749
|
+
};
|
|
750
|
+
|
|
736
751
|
const lines: string[] = ["<inbound_actor_context>"];
|
|
737
|
-
lines.push(`source_channel: ${ctx.sourceChannel}`);
|
|
738
752
|
lines.push(
|
|
739
|
-
`
|
|
753
|
+
`source_channel: ${sanitizeInlineContextValue(ctx.sourceChannel)}`,
|
|
754
|
+
);
|
|
755
|
+
lines.push(
|
|
756
|
+
`canonical_actor_identity: ${sanitizeInlineContextValue(ctx.canonicalActorIdentity)}`,
|
|
757
|
+
);
|
|
758
|
+
lines.push(
|
|
759
|
+
`actor_identifier: ${sanitizeInlineContextValue(ctx.actorIdentifier)}`,
|
|
760
|
+
);
|
|
761
|
+
lines.push(
|
|
762
|
+
`actor_display_name: ${sanitizeInlineContextValue(ctx.actorDisplayName)}`,
|
|
740
763
|
);
|
|
741
|
-
lines.push(`actor_identifier: ${ctx.actorIdentifier ?? "unknown"}`);
|
|
742
|
-
lines.push(`actor_display_name: ${ctx.actorDisplayName ?? "unknown"}`);
|
|
743
764
|
lines.push(
|
|
744
|
-
`actor_sender_display_name: ${ctx.actorSenderDisplayName
|
|
765
|
+
`actor_sender_display_name: ${sanitizeInlineContextValue(ctx.actorSenderDisplayName)}`,
|
|
745
766
|
);
|
|
746
767
|
lines.push(
|
|
747
|
-
`actor_member_display_name: ${ctx.actorMemberDisplayName
|
|
768
|
+
`actor_member_display_name: ${sanitizeInlineContextValue(ctx.actorMemberDisplayName)}`,
|
|
769
|
+
);
|
|
770
|
+
lines.push(`trust_class: ${sanitizeInlineContextValue(ctx.trustClass)}`);
|
|
771
|
+
lines.push(
|
|
772
|
+
`guardian_identity: ${sanitizeInlineContextValue(ctx.guardianIdentity)}`,
|
|
748
773
|
);
|
|
749
|
-
lines.push(`trust_class: ${ctx.trustClass}`);
|
|
750
|
-
lines.push(`guardian_identity: ${ctx.guardianIdentity ?? "unknown"}`);
|
|
751
774
|
if (ctx.memberStatus) {
|
|
752
|
-
lines.push(
|
|
775
|
+
lines.push(
|
|
776
|
+
`member_status: ${sanitizeInlineContextValue(ctx.memberStatus)}`,
|
|
777
|
+
);
|
|
753
778
|
}
|
|
754
779
|
if (ctx.memberPolicy) {
|
|
755
|
-
lines.push(
|
|
780
|
+
lines.push(
|
|
781
|
+
`member_policy: ${sanitizeInlineContextValue(ctx.memberPolicy)}`,
|
|
782
|
+
);
|
|
756
783
|
}
|
|
757
784
|
// Contact metadata — only included when the sender has a contact record
|
|
758
785
|
// with non-default values.
|
|
759
786
|
if (ctx.contactNotes) {
|
|
760
|
-
lines.push(
|
|
787
|
+
lines.push(
|
|
788
|
+
`contact_notes: ${sanitizeInlineContextValue(ctx.contactNotes)}`,
|
|
789
|
+
);
|
|
761
790
|
}
|
|
762
791
|
if (ctx.contactInteractionCount != null && ctx.contactInteractionCount > 0) {
|
|
763
792
|
lines.push(`contact_interaction_count: ${ctx.contactInteractionCount}`);
|
|
@@ -765,7 +794,8 @@ export function buildInboundActorContextBlock(
|
|
|
765
794
|
if (
|
|
766
795
|
ctx.actorMemberDisplayName &&
|
|
767
796
|
ctx.actorSenderDisplayName &&
|
|
768
|
-
ctx.actorMemberDisplayName !==
|
|
797
|
+
sanitizeInlineContextValue(ctx.actorMemberDisplayName) !==
|
|
798
|
+
sanitizeInlineContextValue(ctx.actorSenderDisplayName)
|
|
769
799
|
) {
|
|
770
800
|
lines.push(
|
|
771
801
|
"name_preference_note: actor_member_display_name is the guardian-preferred nickname for this person; actor_sender_display_name is the channel-provided display name.",
|
|
@@ -781,9 +811,12 @@ export function buildInboundActorContextBlock(
|
|
|
781
811
|
lines.push(
|
|
782
812
|
"This is a trusted contact (non-guardian). When the actor makes a reasonable actionable request, attempt to fulfill it normally using the appropriate tool. If the action requires guardian approval, the tool execution layer will automatically deny it and escalate to the guardian for approval — you do not need to pre-screen or decline on behalf of the guardian. Do not self-approve, bypass security gates, or claim to have permissions you do not have. Do not explain the verification system, mention other access methods, or suggest the requester might be the guardian on another device — this leaks system internals and invites social engineering.",
|
|
783
813
|
);
|
|
784
|
-
if (
|
|
814
|
+
if (
|
|
815
|
+
ctx.actorDisplayName &&
|
|
816
|
+
sanitizeInlineContextValue(ctx.actorDisplayName) !== "unknown"
|
|
817
|
+
) {
|
|
785
818
|
lines.push(
|
|
786
|
-
`When this person asks about their name or identity, their name is "${ctx.actorDisplayName}".`,
|
|
819
|
+
`When this person asks about their name or identity, their name is "${sanitizeInlineContextValue(ctx.actorDisplayName)}".`,
|
|
787
820
|
);
|
|
788
821
|
}
|
|
789
822
|
} else if (ctx.trustClass === "unknown") {
|
|
@@ -999,20 +1032,17 @@ const RUNTIME_INJECTION_PREFIXES = [
|
|
|
999
1032
|
*
|
|
1000
1033
|
* Composes:
|
|
1001
1034
|
* 1. `stripMemoryRecallMessages` (caller-supplied, handles its own logic)
|
|
1002
|
-
* 2.
|
|
1003
|
-
* 3. Prefix-based stripping for channel capabilities, workspace top-level,
|
|
1035
|
+
* 2. Prefix-based stripping for channel capabilities, workspace top-level,
|
|
1004
1036
|
* temporal context, and active surface context (single pass).
|
|
1005
1037
|
*/
|
|
1006
1038
|
export function stripInjectedContext(
|
|
1007
1039
|
messages: Message[],
|
|
1008
1040
|
options: {
|
|
1009
1041
|
stripRecall: (msgs: Message[]) => Message[];
|
|
1010
|
-
stripDynamicProfile: (msgs: Message[]) => Message[];
|
|
1011
1042
|
},
|
|
1012
1043
|
): Message[] {
|
|
1013
1044
|
const afterRecall = options.stripRecall(messages);
|
|
1014
|
-
|
|
1015
|
-
return stripUserTextBlocksByPrefix(afterProfile, RUNTIME_INJECTION_PREFIXES);
|
|
1045
|
+
return stripUserTextBlocksByPrefix(afterRecall, RUNTIME_INJECTION_PREFIXES);
|
|
1016
1046
|
}
|
|
1017
1047
|
|
|
1018
1048
|
/**
|
|
@@ -614,6 +614,7 @@ export function handleSurfaceAction(
|
|
|
614
614
|
userMessage: `Something went wrong: ${message}`,
|
|
615
615
|
retryable: false,
|
|
616
616
|
debugDetails: `History-restored relay prompt processing failed: ${message}`,
|
|
617
|
+
errorCategory: "processing_failed",
|
|
617
618
|
}),
|
|
618
619
|
);
|
|
619
620
|
});
|
|
@@ -938,10 +939,11 @@ export async function surfaceProxyResolver(
|
|
|
938
939
|
ctx: SurfaceSessionContext,
|
|
939
940
|
toolName: string,
|
|
940
941
|
input: Record<string, unknown>,
|
|
942
|
+
signal?: AbortSignal,
|
|
941
943
|
): Promise<ToolExecutionResult> {
|
|
942
944
|
// Route CU proxy tools (all computer_use_* action tools)
|
|
943
945
|
if (toolName.startsWith("computer_use_")) {
|
|
944
|
-
if (!ctx.hostCuProxy) {
|
|
946
|
+
if (!ctx.hostCuProxy || !ctx.hostCuProxy.isAvailable()) {
|
|
945
947
|
return {
|
|
946
948
|
content: "Computer use is not available — no desktop client connected.",
|
|
947
949
|
isError: true,
|
|
@@ -973,6 +975,7 @@ export async function surfaceProxyResolver(
|
|
|
973
975
|
ctx.conversationId,
|
|
974
976
|
ctx.hostCuProxy.stepCount,
|
|
975
977
|
reasoning,
|
|
978
|
+
signal,
|
|
976
979
|
);
|
|
977
980
|
}
|
|
978
981
|
|
|
@@ -208,7 +208,13 @@ export function createToolExecutor(
|
|
|
208
208
|
proxyToolResolver: (
|
|
209
209
|
toolName: string,
|
|
210
210
|
proxyInput: Record<string, unknown>,
|
|
211
|
-
) =>
|
|
211
|
+
) =>
|
|
212
|
+
surfaceProxyResolver(
|
|
213
|
+
ctx,
|
|
214
|
+
toolName,
|
|
215
|
+
proxyInput,
|
|
216
|
+
ctx.abortController?.signal,
|
|
217
|
+
),
|
|
212
218
|
proxyApprovalCallback: createProxyApprovalCallback(prompter, ctx),
|
|
213
219
|
requestSecret: async (params) => {
|
|
214
220
|
return secretPrompter.prompt(
|
package/src/daemon/session.ts
CHANGED
|
@@ -65,7 +65,6 @@ import type {
|
|
|
65
65
|
ConfirmationStateChanged,
|
|
66
66
|
} from "./message-types/messages.js";
|
|
67
67
|
import { runAgentLoopImpl } from "./session-agent-loop.js";
|
|
68
|
-
import { ConflictGate } from "./session-conflict-gate.js";
|
|
69
68
|
import type { HistorySessionContext } from "./session-history.js";
|
|
70
69
|
import {
|
|
71
70
|
regenerate as regenerateImpl,
|
|
@@ -159,7 +158,6 @@ export class Session {
|
|
|
159
158
|
/** @internal */ contextCompactedMessageCount = 0;
|
|
160
159
|
/** @internal */ contextCompactedAt: number | null = null;
|
|
161
160
|
/** @internal */ currentRequestId?: string;
|
|
162
|
-
/** @internal */ conflictGate = new ConflictGate();
|
|
163
161
|
/** @internal */ hasNoClient = false;
|
|
164
162
|
/** @internal */ hasAttachments = false;
|
|
165
163
|
/** @internal */ headlessLock = false;
|
|
@@ -822,6 +820,18 @@ export class Session {
|
|
|
822
820
|
this.preactivatedSkillIds = ids;
|
|
823
821
|
}
|
|
824
822
|
|
|
823
|
+
/**
|
|
824
|
+
* Add a skill ID to the preactivated set without replacing existing entries.
|
|
825
|
+
* No-op if the ID is already present.
|
|
826
|
+
*/
|
|
827
|
+
addPreactivatedSkillId(id: string): void {
|
|
828
|
+
if (!this.preactivatedSkillIds) {
|
|
829
|
+
this.preactivatedSkillIds = [id];
|
|
830
|
+
} else if (!this.preactivatedSkillIds.includes(id)) {
|
|
831
|
+
this.preactivatedSkillIds.push(id);
|
|
832
|
+
}
|
|
833
|
+
}
|
|
834
|
+
|
|
825
835
|
setTurnChannelContext(ctx: TurnChannelContext): void {
|
|
826
836
|
this.currentTurnChannelContext = ctx;
|
|
827
837
|
}
|
package/src/instrument.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { arch, platform, release } from "node:os";
|
|
1
|
+
import { arch, hostname, platform, release } from "node:os";
|
|
2
2
|
|
|
3
3
|
import * as Sentry from "@sentry/node";
|
|
4
4
|
|
|
@@ -47,12 +47,14 @@ export function initSentry(): void {
|
|
|
47
47
|
dist: COMMIT_SHA,
|
|
48
48
|
environment: APP_VERSION === "0.0.0-dev" ? "development" : "production",
|
|
49
49
|
sendDefaultPii: false,
|
|
50
|
+
serverName: hostname(),
|
|
50
51
|
initialScope: {
|
|
51
52
|
tags: {
|
|
52
53
|
commit: COMMIT_SHA,
|
|
53
54
|
os_platform: platform(),
|
|
54
55
|
os_release: release(),
|
|
55
56
|
os_arch: arch(),
|
|
57
|
+
server_name: hostname(),
|
|
56
58
|
runtime: "bun",
|
|
57
59
|
runtime_version:
|
|
58
60
|
typeof Bun !== "undefined" ? Bun.version : process.version,
|
|
@@ -90,3 +92,61 @@ export function initSentry(): void {
|
|
|
90
92
|
export async function closeSentry(): Promise<void> {
|
|
91
93
|
await Sentry.close();
|
|
92
94
|
}
|
|
95
|
+
|
|
96
|
+
// ── Dynamic session-scoped Sentry tags ──────────────────────────────
|
|
97
|
+
//
|
|
98
|
+
// These tags change per conversation turn and are set on the current
|
|
99
|
+
// Sentry scope before the agent loop runs. Any `Sentry.captureException`
|
|
100
|
+
// call within that async execution chain (e.g. inside agent/loop.ts)
|
|
101
|
+
// will inherit these tags, enabling filtering by conversation, session,
|
|
102
|
+
// user, or assistant in the Sentry dashboard.
|
|
103
|
+
|
|
104
|
+
/** Tag keys set by {@link setSentrySessionContext}. */
|
|
105
|
+
const SESSION_TAG_KEYS = [
|
|
106
|
+
"assistant_id",
|
|
107
|
+
"conversation_id",
|
|
108
|
+
"session_id",
|
|
109
|
+
"message_count",
|
|
110
|
+
"user_identifier",
|
|
111
|
+
] as const;
|
|
112
|
+
|
|
113
|
+
export interface SentrySessionContext {
|
|
114
|
+
/** Internal assistant ID (daemon uses 'self'). */
|
|
115
|
+
assistantId: string;
|
|
116
|
+
/** Conversation/session identifier. */
|
|
117
|
+
conversationId: string;
|
|
118
|
+
/** Number of messages in the conversation at time of the turn. */
|
|
119
|
+
messageCount: number;
|
|
120
|
+
/** Stable per-user identifier (guardian principal ID or similar). */
|
|
121
|
+
userIdentifier?: string;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Set session-scoped tags on the current Sentry scope.
|
|
126
|
+
*
|
|
127
|
+
* Call at the start of each agent loop turn so that any exceptions
|
|
128
|
+
* captured within the turn include conversation/session context.
|
|
129
|
+
*/
|
|
130
|
+
export function setSentrySessionContext(ctx: SentrySessionContext): void {
|
|
131
|
+
Sentry.setTag("assistant_id", ctx.assistantId);
|
|
132
|
+
Sentry.setTag("conversation_id", ctx.conversationId);
|
|
133
|
+
// session_id mirrors conversation_id — in this codebase they are the
|
|
134
|
+
// same value, but downstream Sentry users may search by either name.
|
|
135
|
+
Sentry.setTag("session_id", ctx.conversationId);
|
|
136
|
+
Sentry.setTag("message_count", String(ctx.messageCount));
|
|
137
|
+
if (ctx.userIdentifier) {
|
|
138
|
+
Sentry.setTag("user_identifier", ctx.userIdentifier);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Clear session-scoped tags from the current Sentry scope.
|
|
144
|
+
*
|
|
145
|
+
* Call in the finally block after the agent loop completes so tags
|
|
146
|
+
* from one conversation do not leak into unrelated error captures.
|
|
147
|
+
*/
|
|
148
|
+
export function clearSentrySessionContext(): void {
|
|
149
|
+
for (const key of SESSION_TAG_KEYS) {
|
|
150
|
+
Sentry.setTag(key, undefined);
|
|
151
|
+
}
|
|
152
|
+
}
|
package/src/memory/admin.ts
CHANGED
|
@@ -1,14 +1,9 @@
|
|
|
1
1
|
import { getConfig } from "../config/loader.js";
|
|
2
2
|
import { getLogger } from "../util/logger.js";
|
|
3
|
-
import {
|
|
4
|
-
listPendingConflictDetails,
|
|
5
|
-
resolveConflict,
|
|
6
|
-
} from "./conflict-store.js";
|
|
7
3
|
import { rawGet } from "./db.js";
|
|
8
4
|
import { getMemoryBackendStatus } from "./embedding-backend.js";
|
|
9
5
|
import { enqueueBackfillJob, enqueueRebuildIndexJob } from "./indexer.js";
|
|
10
6
|
import {
|
|
11
|
-
enqueueCleanupResolvedConflictsJob,
|
|
12
7
|
enqueueCleanupStaleSupersededItemsJob,
|
|
13
8
|
getMemoryJobCounts,
|
|
14
9
|
} from "./jobs-store.js";
|
|
@@ -28,94 +23,18 @@ export interface MemorySystemStatus {
|
|
|
28
23
|
summaries: number;
|
|
29
24
|
embeddings: number;
|
|
30
25
|
};
|
|
31
|
-
conflicts: {
|
|
32
|
-
pending: number;
|
|
33
|
-
resolved: number;
|
|
34
|
-
oldestPendingAgeMs: number | null;
|
|
35
|
-
};
|
|
36
26
|
cleanup: {
|
|
37
|
-
resolvedBacklog: number;
|
|
38
27
|
supersededBacklog: number;
|
|
39
|
-
resolvedCompleted24h: number;
|
|
40
28
|
supersededCompleted24h: number;
|
|
41
29
|
};
|
|
42
30
|
jobs: Record<string, number>;
|
|
43
31
|
}
|
|
44
32
|
|
|
45
|
-
export interface MemoryConflictAndCleanupStats {
|
|
46
|
-
conflicts: MemorySystemStatus["conflicts"];
|
|
47
|
-
cleanup: MemorySystemStatus["cleanup"];
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
interface ConflictStatsRow {
|
|
51
|
-
pending_count: number | null;
|
|
52
|
-
resolved_count: number | null;
|
|
53
|
-
oldest_pending_created_at: number | null;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
33
|
interface CleanupStatsRow {
|
|
57
|
-
resolved_backlog: number | null;
|
|
58
34
|
superseded_backlog: number | null;
|
|
59
|
-
resolved_completed_24h: number | null;
|
|
60
35
|
superseded_completed_24h: number | null;
|
|
61
36
|
}
|
|
62
37
|
|
|
63
|
-
/** Lightweight query for conflict/cleanup metrics only — no table counts or job totals. */
|
|
64
|
-
export function getMemoryConflictAndCleanupStats(): MemoryConflictAndCleanupStats {
|
|
65
|
-
const conflictStats = rawGet<ConflictStatsRow>(`
|
|
66
|
-
SELECT
|
|
67
|
-
SUM(CASE WHEN status = 'pending_clarification' THEN 1 ELSE 0 END) AS pending_count,
|
|
68
|
-
SUM(CASE WHEN status != 'pending_clarification' THEN 1 ELSE 0 END) AS resolved_count,
|
|
69
|
-
MIN(CASE WHEN status = 'pending_clarification' THEN created_at END) AS oldest_pending_created_at
|
|
70
|
-
FROM memory_item_conflicts
|
|
71
|
-
`);
|
|
72
|
-
const pending = conflictStats?.pending_count ?? 0;
|
|
73
|
-
const oldestPendingCreatedAt =
|
|
74
|
-
conflictStats?.oldest_pending_created_at ?? null;
|
|
75
|
-
const oldestPendingAgeMs =
|
|
76
|
-
oldestPendingCreatedAt == null
|
|
77
|
-
? null
|
|
78
|
-
: Math.max(0, Date.now() - oldestPendingCreatedAt);
|
|
79
|
-
const throughputWindowStartMs = Date.now() - 24 * 60 * 60 * 1000;
|
|
80
|
-
const cleanupStats = rawGet<CleanupStatsRow>(
|
|
81
|
-
`
|
|
82
|
-
SELECT
|
|
83
|
-
SUM(CASE
|
|
84
|
-
WHEN type = 'cleanup_resolved_conflicts' AND status IN ('pending', 'running')
|
|
85
|
-
THEN 1 ELSE 0 END
|
|
86
|
-
) AS resolved_backlog,
|
|
87
|
-
SUM(CASE
|
|
88
|
-
WHEN type = 'cleanup_stale_superseded_items' AND status IN ('pending', 'running')
|
|
89
|
-
THEN 1 ELSE 0 END
|
|
90
|
-
) AS superseded_backlog,
|
|
91
|
-
SUM(CASE
|
|
92
|
-
WHEN type = 'cleanup_resolved_conflicts' AND status = 'completed' AND updated_at >= ?
|
|
93
|
-
THEN 1 ELSE 0 END
|
|
94
|
-
) AS resolved_completed_24h,
|
|
95
|
-
SUM(CASE
|
|
96
|
-
WHEN type = 'cleanup_stale_superseded_items' AND status = 'completed' AND updated_at >= ?
|
|
97
|
-
THEN 1 ELSE 0 END
|
|
98
|
-
) AS superseded_completed_24h
|
|
99
|
-
FROM memory_jobs
|
|
100
|
-
`,
|
|
101
|
-
throughputWindowStartMs,
|
|
102
|
-
throughputWindowStartMs,
|
|
103
|
-
);
|
|
104
|
-
return {
|
|
105
|
-
conflicts: {
|
|
106
|
-
pending,
|
|
107
|
-
resolved: conflictStats?.resolved_count ?? 0,
|
|
108
|
-
oldestPendingAgeMs,
|
|
109
|
-
},
|
|
110
|
-
cleanup: {
|
|
111
|
-
resolvedBacklog: cleanupStats?.resolved_backlog ?? 0,
|
|
112
|
-
supersededBacklog: cleanupStats?.superseded_backlog ?? 0,
|
|
113
|
-
resolvedCompleted24h: cleanupStats?.resolved_completed_24h ?? 0,
|
|
114
|
-
supersededCompleted24h: cleanupStats?.superseded_completed_24h ?? 0,
|
|
115
|
-
},
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
|
|
119
38
|
export function getMemorySystemStatus(): MemorySystemStatus {
|
|
120
39
|
const config = getConfig();
|
|
121
40
|
const backend = getMemoryBackendStatus(config);
|
|
@@ -125,36 +44,14 @@ export function getMemorySystemStatus(): MemorySystemStatus {
|
|
|
125
44
|
summaries: countTable("memory_summaries"),
|
|
126
45
|
embeddings: countTable("memory_embeddings"),
|
|
127
46
|
};
|
|
128
|
-
const conflictStats = rawGet<ConflictStatsRow>(`
|
|
129
|
-
SELECT
|
|
130
|
-
SUM(CASE WHEN status = 'pending_clarification' THEN 1 ELSE 0 END) AS pending_count,
|
|
131
|
-
SUM(CASE WHEN status != 'pending_clarification' THEN 1 ELSE 0 END) AS resolved_count,
|
|
132
|
-
MIN(CASE WHEN status = 'pending_clarification' THEN created_at END) AS oldest_pending_created_at
|
|
133
|
-
FROM memory_item_conflicts
|
|
134
|
-
`);
|
|
135
|
-
const pending = conflictStats?.pending_count ?? 0;
|
|
136
|
-
const oldestPendingCreatedAt =
|
|
137
|
-
conflictStats?.oldest_pending_created_at ?? null;
|
|
138
|
-
const oldestPendingAgeMs =
|
|
139
|
-
oldestPendingCreatedAt == null
|
|
140
|
-
? null
|
|
141
|
-
: Math.max(0, Date.now() - oldestPendingCreatedAt);
|
|
142
47
|
const throughputWindowStartMs = Date.now() - 24 * 60 * 60 * 1000;
|
|
143
48
|
const cleanupStats = rawGet<CleanupStatsRow>(
|
|
144
49
|
`
|
|
145
50
|
SELECT
|
|
146
|
-
SUM(CASE
|
|
147
|
-
WHEN type = 'cleanup_resolved_conflicts' AND status IN ('pending', 'running')
|
|
148
|
-
THEN 1 ELSE 0 END
|
|
149
|
-
) AS resolved_backlog,
|
|
150
51
|
SUM(CASE
|
|
151
52
|
WHEN type = 'cleanup_stale_superseded_items' AND status IN ('pending', 'running')
|
|
152
53
|
THEN 1 ELSE 0 END
|
|
153
54
|
) AS superseded_backlog,
|
|
154
|
-
SUM(CASE
|
|
155
|
-
WHEN type = 'cleanup_resolved_conflicts' AND status = 'completed' AND updated_at >= ?
|
|
156
|
-
THEN 1 ELSE 0 END
|
|
157
|
-
) AS resolved_completed_24h,
|
|
158
55
|
SUM(CASE
|
|
159
56
|
WHEN type = 'cleanup_stale_superseded_items' AND status = 'completed' AND updated_at >= ?
|
|
160
57
|
THEN 1 ELSE 0 END
|
|
@@ -162,7 +59,6 @@ export function getMemorySystemStatus(): MemorySystemStatus {
|
|
|
162
59
|
FROM memory_jobs
|
|
163
60
|
`,
|
|
164
61
|
throughputWindowStartMs,
|
|
165
|
-
throughputWindowStartMs,
|
|
166
62
|
);
|
|
167
63
|
return {
|
|
168
64
|
enabled: backend.enabled,
|
|
@@ -171,15 +67,8 @@ export function getMemorySystemStatus(): MemorySystemStatus {
|
|
|
171
67
|
provider: backend.provider,
|
|
172
68
|
model: backend.model,
|
|
173
69
|
counts,
|
|
174
|
-
conflicts: {
|
|
175
|
-
pending,
|
|
176
|
-
resolved: conflictStats?.resolved_count ?? 0,
|
|
177
|
-
oldestPendingAgeMs,
|
|
178
|
-
},
|
|
179
70
|
cleanup: {
|
|
180
|
-
resolvedBacklog: cleanupStats?.resolved_backlog ?? 0,
|
|
181
71
|
supersededBacklog: cleanupStats?.superseded_backlog ?? 0,
|
|
182
|
-
resolvedCompleted24h: cleanupStats?.resolved_completed_24h ?? 0,
|
|
183
72
|
supersededCompleted24h: cleanupStats?.superseded_completed_24h ?? 0,
|
|
184
73
|
},
|
|
185
74
|
jobs: getMemoryJobCounts(),
|
|
@@ -203,95 +92,17 @@ export function requestMemoryRebuildIndex(): string {
|
|
|
203
92
|
}
|
|
204
93
|
|
|
205
94
|
export function requestMemoryCleanup(retentionMs?: number): {
|
|
206
|
-
resolvedConflictsJobId: string;
|
|
207
95
|
staleSupersededItemsJobId: string;
|
|
208
96
|
} {
|
|
209
|
-
const resolvedConflictsJobId =
|
|
210
|
-
enqueueCleanupResolvedConflictsJob(retentionMs);
|
|
211
97
|
const staleSupersededItemsJobId =
|
|
212
98
|
enqueueCleanupStaleSupersededItemsJob(retentionMs);
|
|
213
99
|
log.info(
|
|
214
|
-
{
|
|
100
|
+
{ staleSupersededItemsJobId, retentionMs },
|
|
215
101
|
"Queued memory cleanup jobs",
|
|
216
102
|
);
|
|
217
|
-
return {
|
|
103
|
+
return { staleSupersededItemsJobId };
|
|
218
104
|
}
|
|
219
105
|
|
|
220
106
|
export async function queryMemory(query: string, conversationId: string) {
|
|
221
107
|
return queryMemoryForCli(query, conversationId, getConfig());
|
|
222
108
|
}
|
|
223
|
-
|
|
224
|
-
export interface DismissConflictsResult {
|
|
225
|
-
dismissed: number;
|
|
226
|
-
remaining: number;
|
|
227
|
-
details: Array<{
|
|
228
|
-
id: string;
|
|
229
|
-
existingStatement: string;
|
|
230
|
-
candidateStatement: string;
|
|
231
|
-
}>;
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
/**
|
|
235
|
-
* Dismiss pending memory conflicts. With `all: true`, dismisses every
|
|
236
|
-
* pending conflict. Otherwise, dismisses only conflicts matching the
|
|
237
|
-
* given `pattern` regex against either statement.
|
|
238
|
-
*/
|
|
239
|
-
export function dismissPendingConflicts(options: {
|
|
240
|
-
all?: boolean;
|
|
241
|
-
pattern?: RegExp;
|
|
242
|
-
scopeId?: string;
|
|
243
|
-
}): DismissConflictsResult {
|
|
244
|
-
const scopeId = options.scopeId ?? "default";
|
|
245
|
-
const dismissed: DismissConflictsResult["details"] = [];
|
|
246
|
-
const BATCH_SIZE = 1000;
|
|
247
|
-
|
|
248
|
-
// Cursor-based pagination: track last seen (createdAt, id) to advance past
|
|
249
|
-
// previously inspected rows. This ensures pattern mode scans all pending
|
|
250
|
-
// conflicts even when non-matching rows outnumber the batch size.
|
|
251
|
-
let cursor: { createdAt: number; id: string } | undefined;
|
|
252
|
-
let batch = listPendingConflictDetails(scopeId, BATCH_SIZE);
|
|
253
|
-
while (batch.length > 0) {
|
|
254
|
-
for (const conflict of batch) {
|
|
255
|
-
const matches =
|
|
256
|
-
options.all ||
|
|
257
|
-
(options.pattern &&
|
|
258
|
-
(options.pattern.test(conflict.existingStatement) ||
|
|
259
|
-
options.pattern.test(conflict.candidateStatement)));
|
|
260
|
-
if (!matches) continue;
|
|
261
|
-
|
|
262
|
-
resolveConflict(conflict.id, {
|
|
263
|
-
status: "dismissed",
|
|
264
|
-
resolutionNote: options.all
|
|
265
|
-
? "Bulk dismissed via CLI (dismiss-conflicts --all)."
|
|
266
|
-
: `Dismissed via CLI (pattern: ${options.pattern?.source}).`,
|
|
267
|
-
});
|
|
268
|
-
dismissed.push({
|
|
269
|
-
id: conflict.id,
|
|
270
|
-
existingStatement: conflict.existingStatement,
|
|
271
|
-
candidateStatement: conflict.candidateStatement,
|
|
272
|
-
});
|
|
273
|
-
}
|
|
274
|
-
// No more rows to inspect
|
|
275
|
-
if (batch.length < BATCH_SIZE) break;
|
|
276
|
-
const lastRow = batch[batch.length - 1];
|
|
277
|
-
cursor = { createdAt: lastRow.createdAt, id: lastRow.id };
|
|
278
|
-
batch = listPendingConflictDetails(scopeId, BATCH_SIZE, cursor);
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
// Get true remaining count via SQL to avoid batch-size truncation
|
|
282
|
-
const remaining =
|
|
283
|
-
rawGet<{ c: number }>(
|
|
284
|
-
`SELECT COUNT(*) AS c FROM memory_item_conflicts WHERE scope_id = ? AND status = 'pending_clarification'`,
|
|
285
|
-
scopeId,
|
|
286
|
-
)?.c ?? 0;
|
|
287
|
-
|
|
288
|
-
log.info(
|
|
289
|
-
{ dismissed: dismissed.length, remaining, scopeId },
|
|
290
|
-
"Dismissed pending conflicts",
|
|
291
|
-
);
|
|
292
|
-
return {
|
|
293
|
-
dismissed: dismissed.length,
|
|
294
|
-
remaining,
|
|
295
|
-
details: dismissed,
|
|
296
|
-
};
|
|
297
|
-
}
|
|
@@ -17,6 +17,21 @@ import {
|
|
|
17
17
|
canonicalGuardianRequests,
|
|
18
18
|
} from "./schema.js";
|
|
19
19
|
|
|
20
|
+
// ---------------------------------------------------------------------------
|
|
21
|
+
// Expiry helpers
|
|
22
|
+
// ---------------------------------------------------------------------------
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Returns true when a canonical request has passed its `expiresAt` deadline.
|
|
26
|
+
* Requests without an `expiresAt` are never considered expired by this check.
|
|
27
|
+
*/
|
|
28
|
+
export function isRequestExpired(
|
|
29
|
+
request: Pick<CanonicalGuardianRequest, "expiresAt">,
|
|
30
|
+
): boolean {
|
|
31
|
+
if (!request.expiresAt) return false;
|
|
32
|
+
return new Date(request.expiresAt).getTime() < Date.now();
|
|
33
|
+
}
|
|
34
|
+
|
|
20
35
|
// ---------------------------------------------------------------------------
|
|
21
36
|
// Types
|
|
22
37
|
// ---------------------------------------------------------------------------
|
|
@@ -444,6 +459,27 @@ export function resolveCanonicalGuardianRequest(
|
|
|
444
459
|
return getCanonicalGuardianRequest(id);
|
|
445
460
|
}
|
|
446
461
|
|
|
462
|
+
/**
|
|
463
|
+
* Expire all pending canonical guardian requests in a single bulk update.
|
|
464
|
+
*
|
|
465
|
+
* Called at daemon startup to clean up requests that can never be completed
|
|
466
|
+
* because the in-memory pending-interactions Map (which holds session
|
|
467
|
+
* references needed by resolvers) was wiped on restart.
|
|
468
|
+
*
|
|
469
|
+
* Returns the number of requests transitioned from pending → expired.
|
|
470
|
+
*/
|
|
471
|
+
export function expireAllPendingCanonicalRequests(): number {
|
|
472
|
+
const db = getDb();
|
|
473
|
+
const now = new Date().toISOString();
|
|
474
|
+
|
|
475
|
+
db.update(canonicalGuardianRequests)
|
|
476
|
+
.set({ status: "expired", updatedAt: now })
|
|
477
|
+
.where(eq(canonicalGuardianRequests.status, "pending"))
|
|
478
|
+
.run();
|
|
479
|
+
|
|
480
|
+
return rawChanges();
|
|
481
|
+
}
|
|
482
|
+
|
|
447
483
|
// ---------------------------------------------------------------------------
|
|
448
484
|
// Canonical Guardian Deliveries
|
|
449
485
|
// ---------------------------------------------------------------------------
|
|
@@ -624,14 +660,14 @@ export function listPendingRequestsByConversationScope(
|
|
|
624
660
|
const result: CanonicalGuardianRequest[] = [];
|
|
625
661
|
|
|
626
662
|
for (const req of bySource) {
|
|
627
|
-
if (!seen.has(req.id)) {
|
|
663
|
+
if (!seen.has(req.id) && !isRequestExpired(req)) {
|
|
628
664
|
seen.add(req.id);
|
|
629
665
|
result.push(req);
|
|
630
666
|
}
|
|
631
667
|
}
|
|
632
668
|
|
|
633
669
|
for (const req of byDestination) {
|
|
634
|
-
if (!seen.has(req.id)) {
|
|
670
|
+
if (!seen.has(req.id) && !isRequestExpired(req)) {
|
|
635
671
|
seen.add(req.id);
|
|
636
672
|
result.push(req);
|
|
637
673
|
}
|
|
@@ -19,7 +19,6 @@ import {
|
|
|
19
19
|
conversations,
|
|
20
20
|
llmRequestLogs,
|
|
21
21
|
memoryEmbeddings,
|
|
22
|
-
memoryItemEntities,
|
|
23
22
|
memoryItems,
|
|
24
23
|
memoryItemSources,
|
|
25
24
|
memorySegments,
|
|
@@ -503,19 +502,6 @@ export function clearAll(): { conversations: number; messages: number } {
|
|
|
503
502
|
// triggers so that the subsequent base-table DELETEs don't also fail
|
|
504
503
|
// (SQLite triggers are atomic with the triggering statement, so a
|
|
505
504
|
// corrupted FTS table would roll back every base-table DELETE).
|
|
506
|
-
let segmentFtsCorrupted = false;
|
|
507
|
-
try {
|
|
508
|
-
rawExec("DELETE FROM memory_segment_fts");
|
|
509
|
-
} catch (err) {
|
|
510
|
-
log.warn(
|
|
511
|
-
{ err },
|
|
512
|
-
"clearAll: failed to clear memory_segment_fts — dropping triggers so base-table cleanup can proceed",
|
|
513
|
-
);
|
|
514
|
-
rawExec("DROP TRIGGER IF EXISTS memory_segments_ai");
|
|
515
|
-
rawExec("DROP TRIGGER IF EXISTS memory_segments_ad");
|
|
516
|
-
rawExec("DROP TRIGGER IF EXISTS memory_segments_au");
|
|
517
|
-
segmentFtsCorrupted = true;
|
|
518
|
-
}
|
|
519
505
|
rawExec("DELETE FROM memory_item_sources");
|
|
520
506
|
rawExec("DELETE FROM memory_segments");
|
|
521
507
|
rawExec("DELETE FROM memory_items");
|
|
@@ -548,21 +534,6 @@ export function clearAll(): { conversations: number; messages: number } {
|
|
|
548
534
|
// DELETEs have completed. Dropping the virtual table clears the corruption,
|
|
549
535
|
// and recreating it + triggers means subsequent writes maintain FTS
|
|
550
536
|
// consistency without requiring a daemon restart.
|
|
551
|
-
if (segmentFtsCorrupted) {
|
|
552
|
-
rawExec("DROP TABLE IF EXISTS memory_segment_fts");
|
|
553
|
-
rawExec(
|
|
554
|
-
`CREATE VIRTUAL TABLE IF NOT EXISTS memory_segment_fts USING fts5(segment_id UNINDEXED, text)`,
|
|
555
|
-
);
|
|
556
|
-
rawExec(
|
|
557
|
-
`CREATE TRIGGER IF NOT EXISTS memory_segments_ai AFTER INSERT ON memory_segments BEGIN INSERT INTO memory_segment_fts(segment_id, text) VALUES (new.id, new.text); END`,
|
|
558
|
-
);
|
|
559
|
-
rawExec(
|
|
560
|
-
`CREATE TRIGGER IF NOT EXISTS memory_segments_ad AFTER DELETE ON memory_segments BEGIN DELETE FROM memory_segment_fts WHERE segment_id = old.id; END`,
|
|
561
|
-
);
|
|
562
|
-
rawExec(
|
|
563
|
-
`CREATE TRIGGER IF NOT EXISTS memory_segments_au AFTER UPDATE ON memory_segments BEGIN DELETE FROM memory_segment_fts WHERE segment_id = old.id; INSERT INTO memory_segment_fts(segment_id, text) VALUES (new.id, new.text); END`,
|
|
564
|
-
);
|
|
565
|
-
}
|
|
566
537
|
if (messagesFtsCorrupted) {
|
|
567
538
|
rawExec("DROP TABLE IF EXISTS messages_fts");
|
|
568
539
|
rawExec(
|
|
@@ -787,10 +758,6 @@ export function deleteMessageById(messageId: string): DeletedMemoryIds {
|
|
|
787
758
|
result.orphanedItemIds = orphanedIds;
|
|
788
759
|
|
|
789
760
|
if (orphanedIds.length > 0) {
|
|
790
|
-
// Delete memory_item_entities (no FK cascade on this table).
|
|
791
|
-
tx.delete(memoryItemEntities)
|
|
792
|
-
.where(inArray(memoryItemEntities.memoryItemId, orphanedIds))
|
|
793
|
-
.run();
|
|
794
761
|
// Delete embeddings referencing these items.
|
|
795
762
|
tx.delete(memoryEmbeddings)
|
|
796
763
|
.where(
|