@omercnet/paseo-omp 5.4.0-next.418.1 → 5.4.0-next.420.1
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/client/omp-config-surface.tsx +13 -2
- package/client/omp-rules-section.tsx +75 -2
- package/client/rule-notice.tsx +13 -2
- package/index.client.tsx +7 -1
- package/index.server.ts +8 -1
- package/package.json +1 -1
- package/server/omp-rule-firings.ts +211 -0
- package/server/package-version.ts +1 -1
- package/server/provider/session-descriptors.ts +3 -0
- package/shared/omp-rules.ts +23 -0
|
@@ -1157,6 +1157,7 @@ function OmpConfigContent({
|
|
|
1157
1157
|
onStoreChange,
|
|
1158
1158
|
storeInHistory = false,
|
|
1159
1159
|
onComposerPillSettingsChange,
|
|
1160
|
+
focusRule,
|
|
1160
1161
|
}: PluginSurfaceProps & {
|
|
1161
1162
|
cwd?: string;
|
|
1162
1163
|
store?: OmpStore;
|
|
@@ -1164,6 +1165,8 @@ function OmpConfigContent({
|
|
|
1164
1165
|
/** True when each store selection is a separate history entry (0.11 screen params). */
|
|
1165
1166
|
storeInHistory?: boolean;
|
|
1166
1167
|
onComposerPillSettingsChange?: (settings: ComposerPillSettings) => void;
|
|
1168
|
+
/** A rule to open the Rules tab on, from a rule-fired link. */
|
|
1169
|
+
focusRule?: string;
|
|
1167
1170
|
}) {
|
|
1168
1171
|
const loadConfig = useRpc(listOmpConfig);
|
|
1169
1172
|
const loadSettings = useRpc(listOmpSettings);
|
|
@@ -1201,7 +1204,7 @@ function OmpConfigContent({
|
|
|
1201
1204
|
? { error: "Could not list every OMP agent. Typed names remain available." }
|
|
1202
1205
|
: {}),
|
|
1203
1206
|
};
|
|
1204
|
-
const [view, setView] = useState<SurfaceView>("overview");
|
|
1207
|
+
const [view, setView] = useState<SurfaceView>(focusRule ? "rules" : "overview");
|
|
1205
1208
|
const [activeCategory, setActiveCategory] = useState<OmpSettingCategory>("appearance");
|
|
1206
1209
|
const [search, setSearch] = useState("");
|
|
1207
1210
|
const [drafts, setDrafts] = useState<Record<string, SettingDraft>>({});
|
|
@@ -1419,7 +1422,13 @@ function OmpConfigContent({
|
|
|
1419
1422
|
<OmpPluginManagerSection theme={theme} compact={layout.compact} cwd={cwd} store={store} />
|
|
1420
1423
|
) : null}
|
|
1421
1424
|
{view === "rules" ? (
|
|
1422
|
-
<OmpRulesSection
|
|
1425
|
+
<OmpRulesSection
|
|
1426
|
+
theme={theme}
|
|
1427
|
+
styles={styles}
|
|
1428
|
+
cwd={cwd}
|
|
1429
|
+
store={store}
|
|
1430
|
+
focusRule={focusRule}
|
|
1431
|
+
/>
|
|
1423
1432
|
) : null}
|
|
1424
1433
|
{view === "skills" ? (
|
|
1425
1434
|
<OmpSkillsSection theme={theme} styles={styles} cwd={cwd} store={store} />
|
|
@@ -1668,6 +1677,7 @@ function OmpStoreContent(
|
|
|
1668
1677
|
props: PluginSurfaceProps & {
|
|
1669
1678
|
cwd?: string;
|
|
1670
1679
|
onComposerPillSettingsChange?: (settings: ComposerPillSettings) => void;
|
|
1680
|
+
focusRule?: string;
|
|
1671
1681
|
/** Controlled selection (0.11 screen params); omitted, the surface keeps local state. */
|
|
1672
1682
|
store?: OmpStore;
|
|
1673
1683
|
onStoreChange?: (store: OmpStore | undefined) => void;
|
|
@@ -1694,6 +1704,7 @@ export function OmpConfigSurface(
|
|
|
1694
1704
|
onComposerPillSettingsChange: (settings: ComposerPillSettings) => void;
|
|
1695
1705
|
store?: OmpStore;
|
|
1696
1706
|
onStoreChange?: (store: OmpStore | undefined) => void;
|
|
1707
|
+
focusRule?: string;
|
|
1697
1708
|
},
|
|
1698
1709
|
) {
|
|
1699
1710
|
return <OmpStoreContent {...props} />;
|
|
@@ -4,8 +4,10 @@ import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
|
4
4
|
import { useState } from "react";
|
|
5
5
|
import { Pressable, Switch, Text, View } from "react-native";
|
|
6
6
|
import {
|
|
7
|
+
listOmpRuleFirings,
|
|
7
8
|
listOmpRules,
|
|
8
9
|
type OmpRule,
|
|
10
|
+
type OmpRuleFiring,
|
|
9
11
|
type OmpRuleKind,
|
|
10
12
|
setOmpRuleEnabled,
|
|
11
13
|
} from "../shared/omp-rules";
|
|
@@ -25,6 +27,31 @@ const COPY_LABELS: Record<CopyState, string> = {
|
|
|
25
27
|
};
|
|
26
28
|
|
|
27
29
|
const RULES_KEY = ["paseo-omp", "rules"] as const;
|
|
30
|
+
const FIRINGS_KEY = ["paseo-omp", "rule-firings"] as const;
|
|
31
|
+
const MOST_FIRED = 3;
|
|
32
|
+
|
|
33
|
+
function firingText(firing: OmpRuleFiring): string {
|
|
34
|
+
const times = firing.count === 1 ? "1 time" : `${firing.count} times`;
|
|
35
|
+
return firing.lastFiredAt
|
|
36
|
+
? `${times}, last ${new Date(firing.lastFiredAt).toLocaleString()}`
|
|
37
|
+
: times;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function firedSummary(counted: {
|
|
41
|
+
firings: readonly OmpRuleFiring[];
|
|
42
|
+
sessions: number;
|
|
43
|
+
partial: boolean;
|
|
44
|
+
}): string {
|
|
45
|
+
const top = counted.firings
|
|
46
|
+
.slice(0, MOST_FIRED)
|
|
47
|
+
.map((firing) => `${firing.name} (${firing.count})`)
|
|
48
|
+
.join(", ");
|
|
49
|
+
const sessions = `${counted.sessions} ${counted.sessions === 1 ? "session" : "sessions"}`;
|
|
50
|
+
const bound = counted.partial ? ", so some firings are not counted" : "";
|
|
51
|
+
return top
|
|
52
|
+
? `Most fired: ${top}. Counted by rule name from ${sessions}${bound}.`
|
|
53
|
+
: `No rule fired in the ${sessions} counted${bound}.`;
|
|
54
|
+
}
|
|
28
55
|
|
|
29
56
|
const GROUPS: readonly { kind: OmpRuleKind; title: string; empty: string }[] = [
|
|
30
57
|
{ kind: "watch", title: "Watch", empty: "No watch rules" },
|
|
@@ -62,6 +89,8 @@ function Detail({
|
|
|
62
89
|
interface RuleSwitch {
|
|
63
90
|
busy: boolean;
|
|
64
91
|
toggle: (name: string, enabled: boolean) => void;
|
|
92
|
+
/** How often the rule fired in the counted sessions; null until they are counted. */
|
|
93
|
+
firingLabel: (name: string) => string | null;
|
|
65
94
|
}
|
|
66
95
|
|
|
67
96
|
function RuleRow({
|
|
@@ -90,6 +119,8 @@ function RuleRow({
|
|
|
90
119
|
["Files", rule.globs],
|
|
91
120
|
["Agents", rule.agents],
|
|
92
121
|
];
|
|
122
|
+
// Only watch rules fire; always-applied and rulebook rules sit in the prompt.
|
|
123
|
+
const fired = rule.kind === "watch" ? rules.firingLabel(rule.name) : null;
|
|
93
124
|
return (
|
|
94
125
|
<View style={styles.setting}>
|
|
95
126
|
<View style={styles.settingHeader}>
|
|
@@ -105,6 +136,7 @@ function RuleRow({
|
|
|
105
136
|
/>
|
|
106
137
|
</View>
|
|
107
138
|
{rule.description ? <Text style={styles.settingDescription}>{rule.description}</Text> : null}
|
|
139
|
+
{fired ? <Detail styles={styles} label="Fired" value={fired} /> : null}
|
|
108
140
|
{details.map(([label, values]) =>
|
|
109
141
|
values.length > 0 ? (
|
|
110
142
|
<Detail key={label} styles={styles} label={label} value={values.join(", ")} />
|
|
@@ -169,11 +201,14 @@ export function OmpRulesSection({
|
|
|
169
201
|
styles,
|
|
170
202
|
cwd,
|
|
171
203
|
store,
|
|
204
|
+
focusRule,
|
|
172
205
|
}: {
|
|
173
206
|
theme: Theme;
|
|
174
207
|
styles: OmpConfigStyles;
|
|
175
208
|
cwd?: string;
|
|
176
209
|
store?: OmpStore;
|
|
210
|
+
/** The rule a "fired" link opened this tab for, shown first. */
|
|
211
|
+
focusRule?: string;
|
|
177
212
|
}) {
|
|
178
213
|
const loadRules = useRpc(listOmpRules);
|
|
179
214
|
const setEnabled = useRpc(setOmpRuleEnabled);
|
|
@@ -183,6 +218,22 @@ export function OmpRulesSection({
|
|
|
183
218
|
queryKey: [...RULES_KEY, ompStoreKey(store), cwd ?? "global"],
|
|
184
219
|
queryFn: () => loadRules({ store, ...(cwd ? { cwd } : {}) }),
|
|
185
220
|
});
|
|
221
|
+
const loadFirings = useRpc(listOmpRuleFirings);
|
|
222
|
+
const firingsQuery = useQuery({
|
|
223
|
+
queryKey: [...FIRINGS_KEY, ompStoreKey(store), cwd ?? "global"],
|
|
224
|
+
queryFn: () => loadFirings({ store, ...(cwd ? { cwd } : {}) }),
|
|
225
|
+
staleTime: 30_000,
|
|
226
|
+
});
|
|
227
|
+
const counted = firingsQuery.data;
|
|
228
|
+
const firingByName = new Map((counted?.firings ?? []).map((firing) => [firing.name, firing]));
|
|
229
|
+
function firingLabel(name: string): string | null {
|
|
230
|
+
const firing = firingByName.get(name);
|
|
231
|
+
if (firing) return firingText(firing);
|
|
232
|
+
return counted && !counted.partial ? "Not fired in the sessions counted" : null;
|
|
233
|
+
}
|
|
234
|
+
function noisiestFirst(left: OmpRule, right: OmpRule): number {
|
|
235
|
+
return (firingByName.get(right.name)?.count ?? 0) - (firingByName.get(left.name)?.count ?? 0);
|
|
236
|
+
}
|
|
186
237
|
const toggle = useMutation({
|
|
187
238
|
mutationKey: [...RULES_KEY, "toggle"],
|
|
188
239
|
mutationFn: (change: { name: string; enabled: boolean }) =>
|
|
@@ -196,8 +247,15 @@ export function OmpRulesSection({
|
|
|
196
247
|
const rulesSwitch: RuleSwitch = {
|
|
197
248
|
busy: toggle.isPending,
|
|
198
249
|
toggle: (name, enabled) => toggle.mutate({ name, enabled }),
|
|
250
|
+
firingLabel,
|
|
199
251
|
};
|
|
200
252
|
const list = rules.data;
|
|
253
|
+
const focused =
|
|
254
|
+
list?.status === "ready" ? list.rules.find((rule) => rule.name === focusRule) : undefined;
|
|
255
|
+
const focusNote =
|
|
256
|
+
list?.status === "ready" && list.disabled.includes(focusRule ?? "")
|
|
257
|
+
? "This rule is turned off. Switch it on under Turned off below."
|
|
258
|
+
: "No rule with this name is in this scope.";
|
|
201
259
|
return (
|
|
202
260
|
<>
|
|
203
261
|
<View style={styles.sectionHeader}>
|
|
@@ -213,6 +271,21 @@ export function OmpRulesSection({
|
|
|
213
271
|
</Text>
|
|
214
272
|
<Text style={styles.muted}>Rule settings (ttsr.*) are in the Configuration tab.</Text>
|
|
215
273
|
</View>
|
|
274
|
+
{counted ? <Text style={styles.muted}>{firedSummary(counted)}</Text> : null}
|
|
275
|
+
{focusRule && list?.status === "ready" ? (
|
|
276
|
+
<View style={styles.card}>
|
|
277
|
+
<Text style={styles.cardTitle}>Rule named {focusRule}</Text>
|
|
278
|
+
<Text style={styles.muted}>
|
|
279
|
+
Matched by name in this scope. If the rule that fired came from a project's .omp folder,
|
|
280
|
+
look in that workspace's OMP tab.
|
|
281
|
+
</Text>
|
|
282
|
+
{focused ? (
|
|
283
|
+
<RuleRow styles={styles} rule={focused} rules={rulesSwitch} />
|
|
284
|
+
) : (
|
|
285
|
+
<Text style={styles.muted}>{focusNote}</Text>
|
|
286
|
+
)}
|
|
287
|
+
</View>
|
|
288
|
+
) : null}
|
|
216
289
|
<OmpRuleTest theme={theme} styles={styles} cwd={cwd} store={store} />
|
|
217
290
|
{rules.isLoading ? <Text style={styles.muted}>Loading rules...</Text> : null}
|
|
218
291
|
{rules.error ? (
|
|
@@ -229,8 +302,8 @@ export function OmpRulesSection({
|
|
|
229
302
|
<>
|
|
230
303
|
{GROUPS.map((group) => {
|
|
231
304
|
const members = list.rules.filter((rule) => rule.kind === group.kind);
|
|
232
|
-
const own = members.filter((rule) => rule.source !== "builtin");
|
|
233
|
-
const builtin = members.filter((rule) => rule.source === "builtin");
|
|
305
|
+
const own = members.filter((rule) => rule.source !== "builtin").sort(noisiestFirst);
|
|
306
|
+
const builtin = members.filter((rule) => rule.source === "builtin").sort(noisiestFirst);
|
|
234
307
|
return (
|
|
235
308
|
<View key={group.kind} style={styles.card}>
|
|
236
309
|
<Text style={styles.cardTitle}>{group.title}</Text>
|
package/client/rule-notice.tsx
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
import type { PluginTimelineItemProps } from "@getpaseo/plugin/client";
|
|
2
2
|
import { Icon } from "@getpaseo/plugin/client/ui";
|
|
3
|
-
import { Text, View } from "react-native";
|
|
3
|
+
import { Pressable, Text, View } from "react-native";
|
|
4
4
|
import type { OmpRuleNotice } from "../shared/rule-notices";
|
|
5
5
|
|
|
6
|
-
export function OmpRuleNoticeRow({
|
|
6
|
+
export function OmpRuleNoticeRow({
|
|
7
|
+
item,
|
|
8
|
+
theme,
|
|
9
|
+
openRule,
|
|
10
|
+
}: PluginTimelineItemProps<OmpRuleNotice> & { openRule(name: string): void }) {
|
|
7
11
|
const color = theme.colors.foregroundMuted;
|
|
8
12
|
return (
|
|
9
13
|
<View style={{ gap: 2, paddingVertical: 2 }}>
|
|
@@ -15,6 +19,13 @@ export function OmpRuleNoticeRow({ item, theme }: PluginTimelineItemProps<OmpRul
|
|
|
15
19
|
? `OMP rule ${rule.name} fired`
|
|
16
20
|
: `OMP rule ${rule.name} fired on “${rule.matched}”`}
|
|
17
21
|
</Text>
|
|
22
|
+
<Pressable
|
|
23
|
+
accessibilityRole="link"
|
|
24
|
+
accessibilityLabel={`Open rule ${rule.name} in OMP settings`}
|
|
25
|
+
onPress={() => openRule(rule.name)}
|
|
26
|
+
>
|
|
27
|
+
<Text style={{ color: theme.colors.accent, fontSize: 13 }}>View rule</Text>
|
|
28
|
+
</Pressable>
|
|
18
29
|
</View>
|
|
19
30
|
))}
|
|
20
31
|
</View>
|
package/index.client.tsx
CHANGED
|
@@ -132,6 +132,7 @@ export function registerConfig(
|
|
|
132
132
|
return (
|
|
133
133
|
<OmpConfigSurface
|
|
134
134
|
{...props}
|
|
135
|
+
focusRule={props.params.rule}
|
|
135
136
|
onComposerPillSettingsChange={onComposerPillSettingsChange}
|
|
136
137
|
store={current}
|
|
137
138
|
onStoreChange={(store) => {
|
|
@@ -241,7 +242,12 @@ export default function contribute(client: PluginClientContext) {
|
|
|
241
242
|
kind: OMP_RULE_NOTICE_KIND,
|
|
242
243
|
version: 1,
|
|
243
244
|
schema: ompRuleNoticeSchema,
|
|
244
|
-
Component:
|
|
245
|
+
Component: (props) => (
|
|
246
|
+
<OmpRuleNoticeRow
|
|
247
|
+
{...props}
|
|
248
|
+
openRule={(rule) => client.openScreen({ screenId: CONFIG_SCREEN_ID, params: { rule } })}
|
|
249
|
+
/>
|
|
250
|
+
),
|
|
245
251
|
});
|
|
246
252
|
const removeLspDiagnosticsRenderer = client.addTimelineRenderer({
|
|
247
253
|
kind: OMP_LSP_DIAGNOSTICS_KIND,
|
package/index.server.ts
CHANGED
|
@@ -24,6 +24,7 @@ import {
|
|
|
24
24
|
resolveMutateOmpPluginConfig,
|
|
25
25
|
} from "./server/omp-plugins";
|
|
26
26
|
import { readHostAppendPrompt, readOmpPromptFiles } from "./server/omp-prompt-files";
|
|
27
|
+
import { resolveListOmpRuleFirings } from "./server/omp-rule-firings";
|
|
27
28
|
import { resolveSetOmpRuleEnabled } from "./server/omp-rule-switch";
|
|
28
29
|
import { resolveTestOmpRules } from "./server/omp-rule-test";
|
|
29
30
|
import { resolveListOmpRules } from "./server/omp-rules";
|
|
@@ -99,7 +100,12 @@ import {
|
|
|
99
100
|
mutateOmpPluginConfig,
|
|
100
101
|
} from "./shared/omp-plugins";
|
|
101
102
|
import { getOmpPromptFiles } from "./shared/omp-prompt-files";
|
|
102
|
-
import {
|
|
103
|
+
import {
|
|
104
|
+
listOmpRuleFirings,
|
|
105
|
+
listOmpRules,
|
|
106
|
+
setOmpRuleEnabled,
|
|
107
|
+
testOmpRules,
|
|
108
|
+
} from "./shared/omp-rules";
|
|
103
109
|
import { listOmpSecurity } from "./shared/omp-security";
|
|
104
110
|
import { listOmpSettings, updateOmpSettings } from "./shared/omp-settings";
|
|
105
111
|
import { deleteOmpManagedSkill, listOmpSkills, setOmpSkillEnabled } from "./shared/omp-skills";
|
|
@@ -229,6 +235,7 @@ export default function contribute(server: PluginServerContext) {
|
|
|
229
235
|
server.handle(listOmpRules, scoped(resolveListOmpRules));
|
|
230
236
|
server.handle(setOmpRuleEnabled, scoped(resolveSetOmpRuleEnabled));
|
|
231
237
|
server.handle(testOmpRules, scoped(resolveTestOmpRules));
|
|
238
|
+
server.handle(listOmpRuleFirings, scoped(resolveListOmpRuleFirings));
|
|
232
239
|
server.handle(listOmpExtensions, scoped(resolveListOmpExtensions));
|
|
233
240
|
server.handle(setOmpExtensionEnabled, scoped(resolveSetOmpExtensionEnabled));
|
|
234
241
|
server.handle(listOmpSecurity, scoped(resolveListOmpSecurity));
|
package/package.json
CHANGED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import { constants } from "node:fs";
|
|
2
|
+
import { type FileHandle, open, stat } from "node:fs/promises";
|
|
3
|
+
import { createInterface } from "node:readline";
|
|
4
|
+
import type { RpcInput } from "@getpaseo/plugin";
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
import type { listOmpRuleFirings, OmpRuleFiring } from "../shared/omp-rules";
|
|
7
|
+
import { currentOmpEnvironment } from "./paths";
|
|
8
|
+
import { listOmpSessionDescriptors } from "./provider/session-descriptors";
|
|
9
|
+
|
|
10
|
+
export interface FiringLimits {
|
|
11
|
+
/** Newest sessions to count. */
|
|
12
|
+
sessions: number;
|
|
13
|
+
/** Text read from one session file; the rest of a longer file is left uncounted. */
|
|
14
|
+
fileBytes: number;
|
|
15
|
+
/** Text read across all sessions of one request; sessions already counted cost nothing. */
|
|
16
|
+
totalBytes: number;
|
|
17
|
+
ms: number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** `sessions` plus the one extra session asked for must stay within the session listing's cap of 500. */
|
|
21
|
+
export const DEFAULT_FIRING_LIMITS: FiringLimits = {
|
|
22
|
+
sessions: 400,
|
|
23
|
+
fileBytes: 8 * 1024 * 1024,
|
|
24
|
+
totalBytes: 64 * 1024 * 1024,
|
|
25
|
+
ms: 8_000,
|
|
26
|
+
};
|
|
27
|
+
const MAX_RULES = 500;
|
|
28
|
+
const MAX_CACHED_FILES = 2_000;
|
|
29
|
+
const MAX_NAME_CHARS = 256;
|
|
30
|
+
|
|
31
|
+
/** One `ttsr_injection` line of an OMP session file: the rules injected after a match. */
|
|
32
|
+
const InjectionSchema = z.object({
|
|
33
|
+
type: z.literal("ttsr_injection"),
|
|
34
|
+
timestamp: z.string().optional(),
|
|
35
|
+
injectedRules: z.array(z.string()),
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
interface Tally {
|
|
39
|
+
count: number;
|
|
40
|
+
/** Epoch ms of the latest firing, or 0 when no entry had a readable time. */
|
|
41
|
+
last: number;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
interface FileFirings {
|
|
45
|
+
mtimeMs: number;
|
|
46
|
+
size: number;
|
|
47
|
+
/** Bytes the counts cover: a larger bound than this one means the file must be read again. */
|
|
48
|
+
readBytes: number;
|
|
49
|
+
truncated: boolean;
|
|
50
|
+
rules: Map<string, Tally>;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Counts per session file, valid while its modification time and size are unchanged. */
|
|
54
|
+
const cache = new Map<string, FileFirings>();
|
|
55
|
+
|
|
56
|
+
function remember(path: string, firings: FileFirings): void {
|
|
57
|
+
cache.delete(path);
|
|
58
|
+
cache.set(path, firings);
|
|
59
|
+
if (cache.size > MAX_CACHED_FILES) {
|
|
60
|
+
const oldest = cache.keys().next().value;
|
|
61
|
+
if (oldest !== undefined) cache.delete(oldest);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function addTo(rules: Map<string, Tally>, name: string, at: number, count = 1): void {
|
|
66
|
+
const tally = rules.get(name) ?? { count: 0, last: 0 };
|
|
67
|
+
tally.count += count;
|
|
68
|
+
tally.last = Math.max(tally.last, at);
|
|
69
|
+
rules.set(name, tally);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const LINES_PER_TIME_CHECK = 1_000;
|
|
73
|
+
|
|
74
|
+
/** The counts of one file, or null when `expired` ended the read or the file could not be read. */
|
|
75
|
+
async function countFile(
|
|
76
|
+
path: string,
|
|
77
|
+
readBytes: number,
|
|
78
|
+
expired: () => boolean,
|
|
79
|
+
): Promise<Map<string, Tally> | null> {
|
|
80
|
+
const rules = new Map<string, Tally>();
|
|
81
|
+
let lineCount = 0;
|
|
82
|
+
let handle: FileHandle;
|
|
83
|
+
try {
|
|
84
|
+
handle = await open(path, constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0));
|
|
85
|
+
} catch {
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
try {
|
|
89
|
+
const lines = createInterface({
|
|
90
|
+
input: handle.createReadStream({ encoding: "utf8", end: readBytes - 1 }),
|
|
91
|
+
crlfDelay: Number.POSITIVE_INFINITY,
|
|
92
|
+
});
|
|
93
|
+
for await (const line of lines) {
|
|
94
|
+
lineCount += 1;
|
|
95
|
+
if (lineCount % LINES_PER_TIME_CHECK === 0 && expired()) return null;
|
|
96
|
+
if (!line.includes('"ttsr_injection"')) continue;
|
|
97
|
+
let parsed: unknown;
|
|
98
|
+
try {
|
|
99
|
+
parsed = JSON.parse(line);
|
|
100
|
+
} catch {
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
const entry = InjectionSchema.safeParse(parsed);
|
|
104
|
+
if (!entry.success) continue;
|
|
105
|
+
const at = Date.parse(entry.data.timestamp ?? "");
|
|
106
|
+
for (const name of entry.data.injectedRules) {
|
|
107
|
+
if (name && name.length <= MAX_NAME_CHARS) addTo(rules, name, Number.isNaN(at) ? 0 : at);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
} catch {
|
|
111
|
+
return null;
|
|
112
|
+
} finally {
|
|
113
|
+
await handle.close().catch(() => undefined);
|
|
114
|
+
}
|
|
115
|
+
return expired() ? null : rules;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* How often each rule fired, from the `ttsr_injection` entries of OMP's session files: the
|
|
120
|
+
* sessions of `cwd` when given, else every session in the store. Newest sessions first, bounded
|
|
121
|
+
* by `limits`; files already counted are reused while their mtime and size are unchanged.
|
|
122
|
+
*/
|
|
123
|
+
export async function readRuleFirings(
|
|
124
|
+
input: { cwd?: string; sessionDir?: string },
|
|
125
|
+
environment: NodeJS.ProcessEnv = currentOmpEnvironment(),
|
|
126
|
+
limits: FiringLimits = DEFAULT_FIRING_LIMITS,
|
|
127
|
+
now: () => number = Date.now,
|
|
128
|
+
): Promise<{ firings: OmpRuleFiring[]; sessions: number; partial: boolean }> {
|
|
129
|
+
const started = now();
|
|
130
|
+
const expired = () => now() - started > limits.ms;
|
|
131
|
+
let partial = false;
|
|
132
|
+
const signal = AbortSignal.timeout(limits.ms);
|
|
133
|
+
let found: Awaited<ReturnType<typeof listOmpSessionDescriptors>>;
|
|
134
|
+
try {
|
|
135
|
+
found = await listOmpSessionDescriptors(
|
|
136
|
+
{
|
|
137
|
+
// One more than wanted, to learn whether older sessions exist.
|
|
138
|
+
limit: limits.sessions + 1,
|
|
139
|
+
signal,
|
|
140
|
+
onTruncated: () => {
|
|
141
|
+
partial = true;
|
|
142
|
+
},
|
|
143
|
+
...(input.cwd ? { cwd: input.cwd } : {}),
|
|
144
|
+
...(input.sessionDir ? { sessionDir: input.sessionDir } : {}),
|
|
145
|
+
},
|
|
146
|
+
environment,
|
|
147
|
+
);
|
|
148
|
+
} catch (error) {
|
|
149
|
+
if (signal.aborted) return { firings: [], sessions: 0, partial: true };
|
|
150
|
+
throw error;
|
|
151
|
+
}
|
|
152
|
+
if (found.length > limits.sessions) partial = true;
|
|
153
|
+
const sessions = found.slice(0, limits.sessions);
|
|
154
|
+
const totals = new Map<string, Tally>();
|
|
155
|
+
let counted = 0;
|
|
156
|
+
let bytes = 0;
|
|
157
|
+
for (const session of sessions) {
|
|
158
|
+
const file = session.transcriptFile;
|
|
159
|
+
if (!file) continue;
|
|
160
|
+
if (expired()) {
|
|
161
|
+
partial = true;
|
|
162
|
+
break;
|
|
163
|
+
}
|
|
164
|
+
const info = await stat(file).catch(() => null);
|
|
165
|
+
if (!info?.isFile()) continue;
|
|
166
|
+
const readBytes = Math.min(info.size, limits.fileBytes);
|
|
167
|
+
let firings = cache.get(file);
|
|
168
|
+
if (
|
|
169
|
+
firings?.mtimeMs !== info.mtimeMs ||
|
|
170
|
+
firings.size !== info.size ||
|
|
171
|
+
firings.readBytes !== readBytes
|
|
172
|
+
) {
|
|
173
|
+
if (bytes + readBytes > limits.totalBytes) {
|
|
174
|
+
partial = true;
|
|
175
|
+
break;
|
|
176
|
+
}
|
|
177
|
+
bytes += readBytes;
|
|
178
|
+
const rules = await countFile(file, readBytes, expired);
|
|
179
|
+
if (!rules) {
|
|
180
|
+
// Not cached: a file that could not be read completely is read again next time.
|
|
181
|
+
partial = true;
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
firings = {
|
|
185
|
+
mtimeMs: info.mtimeMs,
|
|
186
|
+
size: info.size,
|
|
187
|
+
readBytes,
|
|
188
|
+
truncated: info.size > limits.fileBytes,
|
|
189
|
+
rules,
|
|
190
|
+
};
|
|
191
|
+
remember(file, firings);
|
|
192
|
+
}
|
|
193
|
+
if (firings.truncated) partial = true;
|
|
194
|
+
counted += 1;
|
|
195
|
+
for (const [name, tally] of firings.rules) addTo(totals, name, tally.last, tally.count);
|
|
196
|
+
}
|
|
197
|
+
if (totals.size > MAX_RULES) partial = true;
|
|
198
|
+
const firings = [...totals]
|
|
199
|
+
.map(([name, tally]) => ({
|
|
200
|
+
name,
|
|
201
|
+
count: tally.count,
|
|
202
|
+
lastFiredAt: tally.last > 0 ? new Date(tally.last).toISOString() : null,
|
|
203
|
+
}))
|
|
204
|
+
.sort((left, right) => right.count - left.count || left.name.localeCompare(right.name))
|
|
205
|
+
.slice(0, MAX_RULES);
|
|
206
|
+
return { firings, sessions: counted, partial };
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export function resolveListOmpRuleFirings(input: RpcInput<typeof listOmpRuleFirings>) {
|
|
210
|
+
return readRuleFirings(input);
|
|
211
|
+
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/** Release Please keeps this bundle-safe constant aligned with package.json. */
|
|
2
|
-
export const PASEO_OMP_PACKAGE_VERSION = "5.4.0-next.
|
|
2
|
+
export const PASEO_OMP_PACKAGE_VERSION = "5.4.0-next.420.1"; // x-release-please-version
|
|
@@ -59,6 +59,8 @@ export interface OmpSessionListOptions {
|
|
|
59
59
|
sessionId?: string;
|
|
60
60
|
sessionDir?: string;
|
|
61
61
|
signal?: AbortSignal;
|
|
62
|
+
/** Called when a scan bound ended the listing early, so the result may miss sessions. */
|
|
63
|
+
onTruncated?: () => void;
|
|
62
64
|
}
|
|
63
65
|
|
|
64
66
|
interface ScanBudget {
|
|
@@ -510,6 +512,7 @@ export async function listOmpSessionDescriptors(
|
|
|
510
512
|
options.signal,
|
|
511
513
|
);
|
|
512
514
|
options.signal?.throwIfAborted();
|
|
515
|
+
if (budgetExceeded(budget)) options.onTruncated?.();
|
|
513
516
|
return matches;
|
|
514
517
|
}
|
|
515
518
|
|
package/shared/omp-rules.ts
CHANGED
|
@@ -109,3 +109,26 @@ export const testOmpRules = defineRpc({
|
|
|
109
109
|
z.object({ status: z.literal("error"), message: z.string() }),
|
|
110
110
|
]),
|
|
111
111
|
});
|
|
112
|
+
|
|
113
|
+
export const OmpRuleFiringSchema = z.object({
|
|
114
|
+
name: z.string(),
|
|
115
|
+
count: z.number().int().positive(),
|
|
116
|
+
/** ISO time of the most recent firing. */
|
|
117
|
+
lastFiredAt: z.string().nullable(),
|
|
118
|
+
});
|
|
119
|
+
export type OmpRuleFiring = z.infer<typeof OmpRuleFiringSchema>;
|
|
120
|
+
|
|
121
|
+
/** How often each rule fired, counted from OMP's session files for this workspace or every session. */
|
|
122
|
+
export const listOmpRuleFirings = defineRpc({
|
|
123
|
+
name: "paseo-omp.rule-firings",
|
|
124
|
+
input: z
|
|
125
|
+
.object({ store: OmpStoreSchema.optional(), cwd: OmpWorkspaceCwdSchema.optional() })
|
|
126
|
+
.strict(),
|
|
127
|
+
output: z.object({
|
|
128
|
+
firings: z.array(OmpRuleFiringSchema),
|
|
129
|
+
/** Sessions the counts cover. */
|
|
130
|
+
sessions: z.number().int().nonnegative(),
|
|
131
|
+
/** True when a size or time bound left some sessions or session text uncounted. */
|
|
132
|
+
partial: z.boolean(),
|
|
133
|
+
}),
|
|
134
|
+
});
|