-
Notifications
You must be signed in to change notification settings - Fork 28
feat(memory): Add storage-backed plugin package #623
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
dcramer
wants to merge
4
commits into
main
Choose a base branch
from
feat/memory-plugin-storage
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3ed4402
feat(memory): Add storage-backed plugin package
dcramer 5f9cfc2
feat(memory): Tighten memory provenance columns
dcramer c2bc19b
feat(memory): Simplify memory storage row
dcramer 6191dbf
fix(memory): Tighten storage lifecycle semantics
dcramer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import { defineConfig } from "drizzle-kit"; | ||
|
|
||
| export default defineConfig({ | ||
| dialect: "postgresql", | ||
| out: "./migrations", | ||
| schema: "./src/db/schema.ts", | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| CREATE TABLE IF NOT EXISTS junior_memory_memories ( | ||
| id TEXT PRIMARY KEY, | ||
| scope TEXT NOT NULL, | ||
| scope_key TEXT NOT NULL, | ||
| type TEXT NOT NULL, | ||
| sensitivity TEXT NOT NULL, | ||
| content TEXT NOT NULL, | ||
| content_hash TEXT NOT NULL, | ||
| source_platform TEXT NOT NULL, | ||
| source_key TEXT NOT NULL, | ||
| idempotency_key TEXT, | ||
| observed_at_ms BIGINT NOT NULL, | ||
| created_at_ms BIGINT NOT NULL, | ||
| expires_at_ms BIGINT, | ||
| superseded_at_ms BIGINT, | ||
| superseded_by_id TEXT, | ||
| archived_at_ms BIGINT, | ||
| archive_reason TEXT | ||
| ); | ||
|
|
||
| CREATE INDEX IF NOT EXISTS junior_memory_memories_visible_idx | ||
| ON junior_memory_memories (scope, scope_key, created_at_ms DESC, id) | ||
| WHERE archived_at_ms IS NULL AND superseded_at_ms IS NULL AND superseded_by_id IS NULL; | ||
|
|
||
| CREATE INDEX IF NOT EXISTS junior_memory_memories_expiration_idx | ||
| ON junior_memory_memories (expires_at_ms) | ||
| WHERE archived_at_ms IS NULL AND expires_at_ms IS NOT NULL; | ||
|
|
||
| CREATE UNIQUE INDEX IF NOT EXISTS junior_memory_memories_active_hash_idx | ||
| ON junior_memory_memories (scope, scope_key, content_hash) | ||
| WHERE archived_at_ms IS NULL AND superseded_at_ms IS NULL AND superseded_by_id IS NULL; | ||
|
|
||
| CREATE UNIQUE INDEX IF NOT EXISTS junior_memory_memories_idempotency_idx | ||
| ON junior_memory_memories (scope, scope_key, idempotency_key) | ||
| WHERE idempotency_key IS NOT NULL; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| { | ||
| "name": "@sentry/junior-memory", | ||
| "version": "0.75.0", | ||
| "private": false, | ||
| "publishConfig": { | ||
| "access": "public" | ||
| }, | ||
| "type": "module", | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+https://github.com/getsentry/junior.git", | ||
| "directory": "packages/junior-memory" | ||
| }, | ||
| "exports": { | ||
| ".": { | ||
| "types": "./src/index.ts", | ||
| "default": "./dist/index.js" | ||
| } | ||
| }, | ||
| "files": [ | ||
| "dist", | ||
| "migrations", | ||
| "src" | ||
| ], | ||
| "scripts": { | ||
| "build": "tsup && tsc -p tsconfig.build.json --emitDeclarationOnly", | ||
| "db:generate": "pnpm dlx drizzle-kit@0.31.10 generate --config drizzle.config.ts", | ||
| "prepare": "pnpm run build", | ||
| "prepack": "pnpm run build", | ||
| "typecheck": "tsc --noEmit" | ||
| }, | ||
| "dependencies": { | ||
| "@sentry/junior-plugin-api": "workspace:*", | ||
| "drizzle-orm": "catalog:", | ||
| "zod": "catalog:" | ||
| }, | ||
| "devDependencies": { | ||
| "@types/node": "^25.9.1", | ||
| "tsup": "^8.5.1", | ||
| "typescript": "^6.0.3" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import { sql } from "drizzle-orm"; | ||
| import { bigint, index, pgTable, text, uniqueIndex } from "drizzle-orm/pg-core"; | ||
|
|
||
| export const juniorMemoryMemories = pgTable( | ||
| "junior_memory_memories", | ||
| { | ||
| id: text("id").primaryKey(), | ||
| scope: text("scope").notNull(), | ||
| scopeKey: text("scope_key").notNull(), | ||
| type: text("type").notNull(), | ||
| sensitivity: text("sensitivity").notNull(), | ||
| content: text("content").notNull(), | ||
| contentHash: text("content_hash").notNull(), | ||
| sourcePlatform: text("source_platform").notNull(), | ||
| sourceKey: text("source_key").notNull(), | ||
| idempotencyKey: text("idempotency_key"), | ||
| observedAtMs: bigint("observed_at_ms", { mode: "number" }).notNull(), | ||
| createdAtMs: bigint("created_at_ms", { mode: "number" }).notNull(), | ||
| expiresAtMs: bigint("expires_at_ms", { mode: "number" }), | ||
| supersededAtMs: bigint("superseded_at_ms", { mode: "number" }), | ||
| supersededById: text("superseded_by_id"), | ||
| archivedAtMs: bigint("archived_at_ms", { mode: "number" }), | ||
| archiveReason: text("archive_reason"), | ||
| }, | ||
| (table) => [ | ||
| index("junior_memory_memories_visible_idx") | ||
| .on(table.scope, table.scopeKey, table.createdAtMs.desc(), table.id) | ||
| .where( | ||
| sql`${table.archivedAtMs} IS NULL AND ${table.supersededAtMs} IS NULL AND ${table.supersededById} IS NULL`, | ||
| ), | ||
| index("junior_memory_memories_expiration_idx") | ||
| .on(table.expiresAtMs) | ||
| .where( | ||
| sql`${table.archivedAtMs} IS NULL AND ${table.expiresAtMs} IS NOT NULL`, | ||
| ), | ||
| uniqueIndex("junior_memory_memories_active_hash_idx") | ||
| .on(table.scope, table.scopeKey, table.contentHash) | ||
| .where( | ||
| sql`${table.archivedAtMs} IS NULL AND ${table.supersededAtMs} IS NULL AND ${table.supersededById} IS NULL`, | ||
| ), | ||
| uniqueIndex("junior_memory_memories_idempotency_idx") | ||
| .on(table.scope, table.scopeKey, table.idempotencyKey) | ||
| .where(sql`${table.idempotencyKey} IS NOT NULL`), | ||
| ], | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| export { createMemoryPlugin, memoryPlugin } from "./plugin"; | ||
| export { createMemoryStore } from "./store"; | ||
| export type { | ||
| ArchiveMemoryInput, | ||
| CreateMemoryInput, | ||
| CreateMemoryResult, | ||
| ListMemoriesInput, | ||
| MemoryStore, | ||
| SearchMemoriesInput, | ||
| } from "./store"; | ||
| export type { | ||
| MemoryRecord, | ||
| MemoryRuntimeContext, | ||
| MemoryScope, | ||
| MemorySensitivity, | ||
| MemoryType, | ||
| } from "./types"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { defineJuniorPlugin } from "@sentry/junior-plugin-api"; | ||
|
|
||
| /** Create Junior's trusted long-term memory plugin registration. */ | ||
| export function createMemoryPlugin() { | ||
| return defineJuniorPlugin({ | ||
| database: {}, | ||
| manifest: { | ||
| name: "memory", | ||
| displayName: "Memory", | ||
| description: "Long-term Junior memory storage and recall", | ||
| }, | ||
| packageName: "@sentry/junior-memory", | ||
| }); | ||
| } | ||
|
|
||
| export const memoryPlugin = createMemoryPlugin(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import type { MemoryScope, MemorySensitivity } from "./types"; | ||
|
|
||
| const SECRET_PATTERNS = [ | ||
| /\b(?:api[_-]?key|secret|token|password|passwd|private[_-]?key)\b/i, | ||
| /\b(?:xox[baprs]-|gh[pousr]_|sk-[A-Za-z0-9_-]{12,})/, | ||
| /-----BEGIN [A-Z ]*PRIVATE KEY-----/, | ||
| ]; | ||
|
|
||
| /** Return whether content matches the plugin's deterministic secret rejection. */ | ||
| export function containsMemorySecret(content: string): boolean { | ||
| return SECRET_PATTERNS.some((pattern) => pattern.test(content)); | ||
| } | ||
|
|
||
| /** Validate deterministic write policy before memory content reaches storage. */ | ||
| export function validateMemoryWritePolicy(args: { | ||
| content: string; | ||
| scope: MemoryScope; | ||
| sensitivity: MemorySensitivity; | ||
| }): { ok: true } | { ok: false; reason: string } { | ||
| if (!args.content.trim()) { | ||
| return { ok: false, reason: "Memory content is required." }; | ||
| } | ||
| if (containsMemorySecret(args.content)) { | ||
| return { | ||
| ok: false, | ||
| reason: "Memory content appears to contain a secret.", | ||
| }; | ||
| } | ||
| if (args.scope === "conversation" && args.sensitivity === "sensitive") { | ||
| return { | ||
| ok: false, | ||
| reason: "Sensitive memories can only be stored personally.", | ||
| }; | ||
| } | ||
| return { ok: true }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import type { MemoryRuntimeContext, MemoryScope } from "./types"; | ||
|
|
||
| export interface ResolvedMemoryScope { | ||
| scope: MemoryScope; | ||
| scopeKey: string; | ||
| } | ||
|
|
||
| function sourceConversationKey(ctx: MemoryRuntimeContext): string | undefined { | ||
| if (ctx.source.platform === "local") { | ||
| return ctx.source.conversationId; | ||
| } | ||
| const threadKey = ctx.source.threadTs ?? ctx.source.messageTs; | ||
| if (!threadKey) { | ||
| return undefined; | ||
| } | ||
| return `slack:${ctx.source.teamId}:${ctx.source.channelId}:${threadKey}`; | ||
| } | ||
|
|
||
| function requesterScopeKey(ctx: MemoryRuntimeContext): string | undefined { | ||
| const requester = ctx.requester; | ||
| if (!requester?.userId) { | ||
| return undefined; | ||
| } | ||
| if (requester.platform === "slack") { | ||
| return `slack:${requester.teamId}:${requester.userId}`; | ||
| } | ||
| return `local:${requester.userId}`; | ||
| } | ||
|
|
||
| /** Derive the authority-bearing key for a requested memory scope. */ | ||
| export function deriveMemoryScope( | ||
| ctx: MemoryRuntimeContext, | ||
| scope: MemoryScope, | ||
| ): ResolvedMemoryScope { | ||
| if (scope === "personal") { | ||
| const scopeKey = requesterScopeKey(ctx); | ||
| if (!scopeKey) { | ||
| throw new Error("Personal memory requires requester context."); | ||
| } | ||
| return { scope, scopeKey }; | ||
| } | ||
|
|
||
| const scopeKey = sourceConversationKey(ctx); | ||
| if (!scopeKey) { | ||
| throw new Error("Conversation memory requires conversation context."); | ||
| } | ||
| return { scope, scopeKey }; | ||
| } | ||
|
|
||
| /** Return every visible scope for memory retrieval in the current context. */ | ||
| export function deriveVisibleMemoryScopes( | ||
| ctx: MemoryRuntimeContext, | ||
| ): ResolvedMemoryScope[] { | ||
| const scopes: ResolvedMemoryScope[] = []; | ||
| try { | ||
| scopes.push(deriveMemoryScope(ctx, "personal")); | ||
| } catch { | ||
| // Personal memory is optional when a runtime surface has no requester. | ||
| } | ||
| try { | ||
| scopes.push(deriveMemoryScope(ctx, "conversation")); | ||
| } catch { | ||
| // Conversation memory is optional for synthetic invocations. | ||
| } | ||
| return scopes; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.