Repository navigation
Expand file tree
/
Copy pathscope.ts
More file actions
131 lines (122 loc) · 4.77 KB
/
Copy pathscope.ts
File metadata and controls
131 lines (122 loc) · 4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
* Memory scope policy — maps an authenticated run to the team-memory pool(s) it
* may read and the single pool it may write, per the memory-scope spec:
*
* team_id = the authenticated run's orgId
* agent_id = the configured useAgent agent id
* org pool → user_id = `org:${orgId}` (every org member shares it)
* personal pool → user_id = the run's authenticated userId (private)
*
* - "org" scope : read org pool only; capture into org pool only.
* - "personal" scope: read personal AND org (personal prioritized); capture
* into personal ONLY. A personal run with NO authenticated user FAILS CLOSED
* — no personal read or write.
*
* Identity is ALWAYS resolved from the persisted authenticated run — never from
* the sandbox, prompt, tool arguments, or a request body. This module is pure
* policy; the Tencent wire calls live in team-memory.ts. Internal canary/test
* origins retain the same team_id but use origin-specific agent/user partitions
* so evaluation traffic cannot read or write product L0-L3 memory.
*/
import { memoryConfig } from "../env";
import { MEMORY_SCOPES, type MemoryScope } from "../db/schema";
import { isInternalRunOrigin } from "../runs/origin";
import type { MemoryIdentity, ScopedPool } from "./team-memory";
export { MEMORY_SCOPES };
export type { MemoryScope, ScopedPool };
/** Runtime guard for the scope enum — the run-creation boundary rejects anything
* else (400) so an invalid scope never reaches a run row. */
export function isMemoryScope(value: unknown): value is MemoryScope {
return typeof value === "string" && (MEMORY_SCOPES as readonly string[]).includes(value);
}
/** The resolved recall/capture plan for one run. `readPools` is priority order
* (personal first); an EMPTY `readPools` + null `writePool` is the fail-closed
* personal run. Carries the tenant scope the retrieval ledger records. */
export interface ScopedMemoryPlan {
readonly scope: MemoryScope;
readonly orgId: string;
readonly agentId: string;
readonly sessionId: string;
/** The authenticated user who triggered the run; null when unauthenticated. */
readonly actorUserId: string | null;
/** Pools to search, personal-first. Empty ⇒ fail closed (no read). */
readonly readPools: readonly ScopedPool[];
/** The single pool captures go to. Null ⇒ fail closed (no write). */
readonly writePool: ScopedPool | null;
}
/**
* Resolve a run's memory plan from its persisted identity + scope. Returns null
* when memory is disabled (`MEMORY_API_URL` unset) so callers gate cleanly.
*
* `run.orgId` is the tenant (Tencent team_id); a legacy/system run with no org
* falls back to the configured default team so it still resolves a stable pool.
*/
export function resolveScopedMemory(run: {
orgId: string | null;
userId: string | null;
threadId: string;
id: string;
memoryScope: MemoryScope;
origin?: string | null;
}): ScopedMemoryPlan | null {
const cfg = memoryConfig();
if (!cfg) return null;
// team_id = the run's org (spec); fall back to the configured default team for
// a legacy/system run with no org so it still resolves a stable partition.
const orgId = run.orgId ?? cfg.teamId;
const internalOrigin = run.origin && isInternalRunOrigin(run.origin)
? run.origin.toLowerCase()
: null;
const agentId = internalOrigin
? `${cfg.agentId}:internal:${internalOrigin}`
: cfg.agentId;
const orgUserId = internalOrigin
? `internal:${internalOrigin}:org:${orgId}`
: `org:${orgId}`;
const base = {
teamId: orgId,
agentId,
sessionId: run.threadId,
runId: run.id,
} as const;
const orgPool: ScopedPool = {
sourceScope: "org",
identity: {
...base,
userId: orgUserId,
// Provenance = the real actor when known, else the shared org partition.
actorUserId: run.userId ?? `org:${orgId}`,
},
};
const common = {
orgId,
agentId,
sessionId: run.threadId,
actorUserId: run.userId,
} as const;
if (run.memoryScope === "org") {
return { ...common, scope: "org", readPools: [orgPool], writePool: orgPool };
}
// personal scope — requires a verified authenticated user, else FAIL CLOSED.
if (!run.userId) {
return { ...common, scope: "personal", actorUserId: null, readPools: [], writePool: null };
}
const personalPool: ScopedPool = {
sourceScope: "personal",
identity: {
...base,
userId: internalOrigin
? `internal:${internalOrigin}:user:${run.userId}`
: run.userId,
actorUserId: run.userId,
},
};
return {
...common,
scope: "personal",
// Personal prioritized; org fills the remaining budget. Capture personal ONLY
// — personal content must never enter the shared org pool.
readPools: [personalPool, orgPool],
writePool: personalPool,
};
}