forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjitTriggerRuntime.test.ts
More file actions
255 lines (244 loc) · 8.06 KB
/
Copy pathjitTriggerRuntime.test.ts
File metadata and controls
255 lines (244 loc) · 8.06 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import { describe, expect, it } from 'vitest'
import {
compileTriggerSnapshotRow,
evaluateJitTrigger,
evaluateJitWatchlist,
JIT_RUNTIME_DEFAULT_AUTHORITY,
JitTriggerCompileError,
type JitTriggerSnapshotRow
} from './jitTriggerRuntime'
const row = (
condition: Record<string, unknown>,
overrides: Partial<JitTriggerSnapshotRow> = {}
): JitTriggerSnapshotRow => ({
memoryId: 'trigger-1',
itemRevision: 1,
updatedAt: '2026-08-24T12:00:00.000Z',
triggerConditionJson: JSON.stringify({
schema_version: 'jit_trigger.v1',
match_mode: 'all',
action: { type: 'agent_prompt', prompt: 'Follow up on the current work.' },
...condition
}),
action: { type: 'agent_prompt', prompt: 'Follow up on the current work.' },
wakeupBudgetPerDay: 1,
...overrides
})
describe('Windows JIT trigger contract', () => {
it('matches deterministic app and time conditions and respects the per-trigger budget', () => {
const compiled = compileTriggerSnapshotRow(
row({
apps: ['Visual Studio Code'],
time: { weekdays: [0], start: '08:00', end: '18:00', timezone: 'UTC' }
})
)
const observation = {
appName: 'visual studio code',
occurredAt: new Date('2026-08-24T12:00:00Z')
}
const first = evaluateJitTrigger(compiled, observation, '2026-08-24')
expect(first.status).toBe('match')
expect(first.wakeupsUsed).toBe(1)
expect(evaluateJitTrigger(compiled, observation, '2026-08-24', 1).reason).toBe(
'wakeup_budget_exhausted'
)
})
it('fails closed when the daily wakeup budget is missing', () => {
const compiled = compileTriggerSnapshotRow(
row({ apps: ['Visual Studio Code'] }, { wakeupBudgetPerDay: null })
)
const result = evaluateJitTrigger(compiled, { appName: 'Visual Studio Code' }, '2026-08-24')
expect(result.status).toBe('no_match')
expect(result.reason).toBe('wakeup_budget_missing')
})
it('honors the authoritative timezone-aware trigger snooze and resumes at expiry', () => {
const compiled = compileTriggerSnapshotRow(
row({ apps: ['Visual Studio Code'] }, { snoozedUntil: '2026-08-24T13:00:00+01:00' })
)
expect(
evaluateJitTrigger(
compiled,
{ appName: 'Visual Studio Code', occurredAt: new Date('2026-08-24T11:59:59Z') },
'2026-08-24'
).reason
).toBe('trigger_snoozed')
expect(
evaluateJitTrigger(
compiled,
{ appName: 'Visual Studio Code', occurredAt: new Date('2026-08-24T12:00:00Z') },
'2026-08-24'
).status
).toBe('match')
})
it('rejects a trigger snooze without an explicit timezone', () => {
expect(() =>
compileTriggerSnapshotRow(row({ apps: ['Code'] }, { snoozedUntil: '2026-08-24T13:00:00' }))
).toThrow('snooze malformed')
})
it('rejects impossible calendar dates instead of trusting Date.parse normalization', () => {
expect(() =>
compileTriggerSnapshotRow(row({ apps: ['Code'] }, { snoozedUntil: '2026-02-30T13:00:00Z' }))
).toThrow('snooze malformed')
})
it('does not guess when calendar or embedding evidence is absent/unattested', () => {
const compiled = compileTriggerSnapshotRow(
row({
calendar: { event_keywords: ['planning'], event_types: [] },
embedding: {
prototype_id: 'proto',
prototype_revision: 'r1',
model_id: 'local',
model_version: '1',
language: 'en',
min_similarity: 0.82
}
})
)
const result = evaluateJitTrigger(compiled, {}, '2026-08-24')
expect(result.status).toBe('no_match')
const attested = evaluateJitTrigger(
compiled,
{
calendarEvents: [{ title: 'Planning', eventType: 'meeting' }],
calendarAuthorized: true,
embeddingScores: {
proto: {
score: 0.9,
modelId: 'local',
modelVersion: '1',
language: 'en',
prototypeRevision: 'r1'
}
}
},
'2026-08-24',
0,
{ modelId: 'local', modelVersion: '1', language: 'en', prototypeRevision: 'r1' }
)
expect(attested.status).toBe('match')
})
it('uses the ratified .74-.82 embedding band for bounded triage', () => {
const compiled = compileTriggerSnapshotRow(
row({
embedding: {
prototype_id: 'proto',
prototype_revision: 'r1',
model_id: 'local',
model_version: '1',
language: 'en',
min_similarity: 0.82
}
})
)
const contract = {
modelId: 'local',
modelVersion: '1',
language: 'en',
prototypeRevision: 'r1'
}
const score = (value: number) => ({
embeddingScores: {
proto: {
score: value,
modelId: 'local',
modelVersion: '1',
language: 'en',
prototypeRevision: 'r1'
}
}
})
expect(evaluateJitTrigger(compiled, score(0.75), '2026-08-24', 0, contract).status).toBe(
'ambiguous'
)
expect(evaluateJitTrigger(compiled, score(0.73), '2026-08-24', 0, contract).status).toBe(
'no_match'
)
})
it('rejects unknown and duplicate authority keys', () => {
expect(() => compileTriggerSnapshotRow(row({ nope: true }))).toThrow(JitTriggerCompileError)
expect(() =>
compileTriggerSnapshotRow({
...row({}),
triggerConditionJson:
'{"schema_version":"jit_trigger.v1","schema_version":"jit_trigger.v1","match_mode":"all","action":{"type":"agent_prompt","prompt":"Follow up on the current work."}}'
})
).toThrow(/duplicate|malformed/i)
})
it('hands a complete empty watchlist to the ambient fallback instead of going silent', () => {
// Parity register item 5 (macOS KnowledgeLedgerTriggerWatchlistRuntime): an
// account with no standing trigger is an ambient candidate, never `none`.
const enabled = {
mode: 'enabled' as const,
killSwitchEnabled: false,
ownerId: 'u',
accountGeneration: 2,
snapshotOwnerId: 'u',
snapshotAccountGeneration: 2,
snapshotIsAuthoritative: true,
authorizationIsCurrent: true
}
const empty = evaluateJitWatchlist(enabled, [], { appName: 'Code' }, '2026-08-24')
expect(empty).toEqual({
status: 'evaluated',
nextLane: 'ambient_fallback',
matches: [],
ambiguous: []
})
// A non-empty watchlist with no match still reaches the same lane, so the
// two cases are indistinguishable downstream (same nano/full-turn budget).
const miss = evaluateJitWatchlist(
enabled,
[compileTriggerSnapshotRow(row({ apps: ['Xcode'] }))],
{ appName: 'Code' },
'2026-08-24'
)
expect(miss.nextLane).toBe('ambient_fallback')
// Inactive authority is unchanged: no lane, no ambient spend.
expect(
evaluateJitWatchlist(JIT_RUNTIME_DEFAULT_AUTHORITY, [], { appName: 'Code' }, '2026-08-24')
.nextLane
).toBe('none')
})
it('keeps the runtime inactive unless the complete backend authority is current', () => {
const compiled = compileTriggerSnapshotRow(row({ apps: ['Code'] }))
const observation = { appName: 'Code' }
expect(
evaluateJitWatchlist(JIT_RUNTIME_DEFAULT_AUTHORITY, [compiled], observation, '2026-08-24')
.status
).toBe('inactive')
expect(
evaluateJitWatchlist(
{
mode: 'enabled',
killSwitchEnabled: false,
ownerId: 'u',
accountGeneration: 2,
snapshotOwnerId: 'u',
snapshotAccountGeneration: 2,
snapshotIsAuthoritative: true,
authorizationIsCurrent: true
},
[compiled],
observation,
'2026-08-24'
).nextLane
).toBe('planned_trigger')
expect(
evaluateJitWatchlist(
{
mode: 'enabled',
killSwitchEnabled: false,
ownerId: 'u',
accountGeneration: 2,
snapshotOwnerId: 'u',
snapshotAccountGeneration: 2,
snapshotIsAuthoritative: true,
authorizationIsCurrent: true
},
[],
observation,
'2026-08-24'
).nextLane
).toBe('ambient_fallback')
})
})