forked from NateBJones-Projects/OB1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolicy.test.ts
More file actions
210 lines (196 loc) · 5.79 KB
/
Copy pathpolicy.test.ts
File metadata and controls
210 lines (196 loc) · 5.79 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import { assertEquals } from "jsr:@std/assert@1";
import {
allowedScopeViolation,
buildMemoryThoughtFilter,
type RecallScope,
reviewTransition,
type ScopedMemory,
scopeGuardViolation,
scopeMatches,
} from "./policy.ts";
const baseScope: RecallScope = {
visibility: "project",
project_only: true,
include_unconfirmed: false,
include_stale: false,
};
function memory(overrides: Partial<ScopedMemory> = {}): ScopedMemory {
return {
workspace_id: "workspace-1",
project_id: "project-1",
channel_id: "channel-1",
visibility: "project",
lifecycle_status: "active",
requires_user_confirmation: false,
review_status: "confirmed",
...overrides,
};
}
Deno.test("buildMemoryThoughtFilter returns none when semantic search has no thought ids", () => {
assertEquals(buildMemoryThoughtFilter([]), { mode: "none", thoughtIds: [] });
});
Deno.test("buildMemoryThoughtFilter returns thought ids when semantic search found candidates", () => {
assertEquals(buildMemoryThoughtFilter(["thought-1", "thought-2"]), {
mode: "thought_ids",
thoughtIds: ["thought-1", "thought-2"],
});
});
Deno.test("allowedScopeViolation enforces workspace and project allowlists", () => {
assertEquals(
allowedScopeViolation(
{ workspace_id: "workspace-1", project_id: "project-1" },
{ workspace_id: "workspace-1", project_id: "project-1" },
),
null,
);
assertEquals(
allowedScopeViolation(
{ workspace_id: "workspace-2", project_id: "project-1" },
{ workspace_id: "workspace-1", project_id: "project-1" },
),
"workspace_not_allowed",
);
assertEquals(
allowedScopeViolation(
{ workspace_id: "workspace-1", project_id: "project-2" },
{ workspace_id: "workspace-1", project_id: "project-1" },
),
"project_not_allowed",
);
});
Deno.test("scopeGuardViolation rejects ID read records outside requested scope", () => {
assertEquals(
scopeGuardViolation(
{ workspace_id: "workspace-1", project_id: "project-1" },
{ requested: { workspace_id: "workspace-1", project_id: "project-1" } },
),
null,
);
assertEquals(
scopeGuardViolation(
{ workspace_id: "workspace-2", project_id: "project-1" },
{ requested: { workspace_id: "workspace-1", project_id: "project-1" } },
),
"workspace_mismatch",
);
assertEquals(
scopeGuardViolation(
{ workspace_id: "workspace-1", project_id: "project-2" },
{ requested: { workspace_id: "workspace-1", project_id: "project-1" } },
),
"project_mismatch",
);
});
Deno.test("scopeMatches blocks personal memory unless personal visibility is requested", () => {
assertEquals(
scopeMatches(memory({ visibility: "personal" }), {
workspace_id: "workspace-1",
project_id: "project-1",
channel: {},
scope: baseScope,
}),
false,
);
assertEquals(
scopeMatches(memory({ visibility: "personal" }), {
workspace_id: "workspace-1",
project_id: "project-1",
channel: {},
scope: { ...baseScope, visibility: "personal" },
}),
true,
);
});
Deno.test("scopeMatches respects project_only project filter", () => {
assertEquals(
scopeMatches(memory({ project_id: "other-project" }), {
workspace_id: "workspace-1",
project_id: "project-1",
channel: {},
scope: baseScope,
}),
false,
);
});
Deno.test("scopeMatches blocks channel memory outside the requested channel", () => {
assertEquals(
scopeMatches(memory({ visibility: "channel", channel_id: "channel-2" }), {
workspace_id: "workspace-1",
project_id: "project-1",
channel: { id: "channel-1" },
scope: { ...baseScope, visibility: "channel" },
}),
false,
);
});
Deno.test("scopeMatches allows workspace memory in project recall but blocks organization memory unless requested", () => {
assertEquals(
scopeMatches(memory({ visibility: "workspace", project_id: null }), {
workspace_id: "workspace-1",
project_id: "project-1",
channel: {},
scope: baseScope,
}),
true,
);
assertEquals(
scopeMatches(memory({ visibility: "organization", project_id: null }), {
workspace_id: "workspace-1",
project_id: "project-1",
channel: {},
scope: baseScope,
}),
false,
);
assertEquals(
scopeMatches(memory({ visibility: "organization", project_id: null }), {
workspace_id: "workspace-1",
project_id: "project-1",
channel: {},
scope: { ...baseScope, visibility: "organization" },
}),
true,
);
});
Deno.test("reviewTransition marks a merged memory as merged and non-instructional", () => {
assertEquals(
reviewTransition({ action: "merge", related_memory_id: "target-memory" }),
{
memoryUpdates: {
review_status: "merged",
can_use_as_instruction: false,
requires_user_confirmation: false,
},
relatedMemoryUpdates: {},
relation: {
to_memory_id: "target-memory",
relation: "merged_into",
},
},
);
});
Deno.test("reviewTransition marks the related memory superseded when current memory supersedes it", () => {
assertEquals(
reviewTransition({ action: "supersede", related_memory_id: "old-memory" }),
{
memoryUpdates: {},
relatedMemoryUpdates: {
lifecycle_status: "superseded",
review_status: "stale",
can_use_as_instruction: false,
},
relation: {
to_memory_id: "old-memory",
relation: "supersedes",
},
},
);
});
Deno.test("reviewTransition keeps confirm semantics instruction-grade only after confirmation", () => {
assertEquals(reviewTransition({ action: "confirm" }).memoryUpdates, {
review_status: "confirmed",
provenance_status: "user_confirmed",
can_use_as_instruction: true,
requires_user_confirmation: false,
});
});