@omercnet/paseo-pr-radar 3.4.0-next.543.1 → 3.4.0-next.545.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.
@@ -14,13 +14,19 @@ import {
14
14
  useToast,
15
15
  } from "@getpaseo/plugin/client/ui";
16
16
  import { useQuery } from "@tanstack/react-query";
17
- import { useMemo, useRef, useState } from "react";
17
+ import { useEffect, useMemo, useRef, useState } from "react";
18
18
  import { Text, View } from "react-native";
19
- import { labelPrefixesProblem } from "../shared/routed-labels";
19
+ import {
20
+ describeFallbackStatus,
21
+ labelPrefixesProblem,
22
+ parseFallbackRuleInput,
23
+ watchFallbackBoundary,
24
+ } from "../shared/routed-labels";
20
25
  import {
21
26
  type AutoLaunchMode,
22
27
  type CwdMode,
23
28
  DEFAULT_PR_RADAR_SETTINGS,
29
+ type PrRadarSettings,
24
30
  PrRadarSettingsSchema,
25
31
  prRadarSettings,
26
32
  } from "../shared/settings";
@@ -32,6 +38,19 @@ import {
32
38
  EFFORT_OPTIONS,
33
39
  useAgentLaunchOptions,
34
40
  } from "./agent-launch-options";
41
+ import {
42
+ createSettingsSaveQueue,
43
+ editFieldDraft,
44
+ fallbackRuleAdder,
45
+ finishFieldSave,
46
+ initFieldState,
47
+ removeFallbackRule,
48
+ resyncFieldToServer,
49
+ type SettingsPatch,
50
+ type SettingsSaveOptions,
51
+ syncFieldFromServer,
52
+ type UncontrolledFieldState,
53
+ } from "./settings-save";
35
54
 
36
55
  type ReadySettings = Extract<SettingsState<typeof prRadarSettings.schema>, { status: "ready" }>;
37
56
 
@@ -68,8 +87,42 @@ function ReadyPrRadarSettings({
68
87
  const toast = useToast();
69
88
  const [dirQueryText, setDirQueryText] = useState(settings.values.sourceCodeDir);
70
89
  const [error, setError] = useState<string | null>(null);
71
- // Remounts the prefix input after a reset: it only reads its initial value once.
72
- const [prefixInputKey, setPrefixInputKey] = useState(0);
90
+ // Tracks the uncontrolled prefix input's mounted/synced base value, local draft and conflict
91
+ // state. Kept in a ref alongside React state so rapid keystrokes always read the latest base.
92
+ const [prefixField, setPrefixField] = useState<UncontrolledFieldState>(() =>
93
+ initFieldState(settings.values.routingLabelPrefixes),
94
+ );
95
+ const prefixFieldRef = useRef(prefixField);
96
+ const prefixInFlightRef = useRef(0);
97
+ const updatePrefixField = (
98
+ updater: (state: UncontrolledFieldState) => UncontrolledFieldState,
99
+ ) => {
100
+ const next = updater(prefixFieldRef.current);
101
+ prefixFieldRef.current = next;
102
+ setPrefixField(next);
103
+ };
104
+ useEffect(() => {
105
+ updatePrefixField((state) =>
106
+ syncFieldFromServer(
107
+ state,
108
+ settings.values.routingLabelPrefixes,
109
+ prefixInFlightRef.current > 0,
110
+ ),
111
+ );
112
+ }, [settings.values.routingLabelPrefixes]);
113
+ const [ruleText, setRuleText] = useState("");
114
+ const [ruleError, setRuleError] = useState<string | null>(null);
115
+ const [ruleInputKey, setRuleInputKey] = useState(0);
116
+ // The status line of each rule reads this clock, so a rule that ends (or comes within the seven
117
+ // day cap) while the screen is open changes its text without a reload.
118
+ const [ruleClock, setRuleClock] = useState(() => Date.now());
119
+ useEffect(
120
+ () =>
121
+ watchFallbackBoundary(settings.values.routingLabelFallbacks, ruleClock, () =>
122
+ setRuleClock(Date.now()),
123
+ ),
124
+ [settings.values.routingLabelFallbacks, ruleClock],
125
+ );
73
126
 
74
127
  const { profileOptions, providerOptions, modelOptions, modeOptions, contextModeOptions } =
75
128
  useAgentLaunchOptions();
@@ -111,49 +164,126 @@ function ReadyPrRadarSettings({
111
164
  values: settings.values,
112
165
  revision: settings.revision,
113
166
  };
114
- const queuedPatchRef = useRef<Partial<typeof settings.values> | null>(null);
115
- const saveChainRef = useRef<Promise<boolean>>(Promise.resolve(true));
116
-
117
- async function savePatch(patch: Partial<typeof settings.values>): Promise<boolean> {
118
- queuedPatchRef.current = { ...(queuedPatchRef.current ?? {}), ...patch };
119
- const run = saveChainRef.current.then(async () => {
120
- const nextPatch = queuedPatchRef.current;
121
- if (!nextPatch) return true;
122
- queuedPatchRef.current = null;
123
- setError(null);
124
- const parsed = PrRadarSettingsSchema.safeParse({
125
- ...latestSettingsRef.current.values,
126
- ...nextPatch,
127
- });
128
- if (!parsed.success) {
129
- setError(parsed.error.issues[0]?.message ?? "Invalid setting value.");
130
- return false;
131
- }
132
- try {
133
- const saved = await settings.save(parsed.data, latestSettingsRef.current.revision);
134
- if (saved === false) {
135
- setError("Settings changed concurrently; please retry.");
136
- return false;
167
+ const settingsRef = useRef(settings);
168
+ settingsRef.current = settings;
169
+ const [saveQueue] = useState(() =>
170
+ createSettingsSaveQueue<PrRadarSettings>({
171
+ latest: () => latestSettingsRef.current,
172
+ remember: (values) => {
173
+ latestSettingsRef.current = { values, revision: latestSettingsRef.current.revision };
174
+ },
175
+ parse: (values) => {
176
+ const parsed = PrRadarSettingsSchema.safeParse(values);
177
+ return parsed.success
178
+ ? { success: true, data: parsed.data }
179
+ : {
180
+ success: false,
181
+ message: parsed.error.issues[0]?.message ?? "Invalid setting value.",
182
+ };
183
+ },
184
+ write: (values, revision) => settingsRef.current.save(values, revision),
185
+ report: (message) => {
186
+ if (message !== null || !prefixFieldRef.current.conflicted) {
187
+ setError(message);
188
+ }
189
+ },
190
+ onConflict: (keys) => {
191
+ if (keys.includes("routingLabelPrefixes")) {
192
+ updatePrefixField((state) => ({
193
+ ...state,
194
+ server: latestSettingsRef.current.values.routingLabelPrefixes,
195
+ conflicted: true,
196
+ }));
137
197
  }
138
- latestSettingsRef.current = {
139
- values: parsed.data,
140
- revision: latestSettingsRef.current.revision,
141
- };
142
- return true;
143
- } catch (updateError) {
144
- setError(updateError instanceof Error ? updateError.message : "Could not save settings.");
145
- return false;
146
- }
147
- });
148
- saveChainRef.current = run.catch(() => false);
149
- return run;
198
+ },
199
+ }),
200
+ );
201
+
202
+ // A plain patch replaces fields; a function patch is applied to the newest saved document, so
203
+ // list edits (fallback rules) never build on a list that an in-flight save already changed.
204
+ function savePatch(
205
+ patch: SettingsPatch<PrRadarSettings>,
206
+ options?: SettingsSaveOptions<PrRadarSettings>,
207
+ ): Promise<boolean> {
208
+ return saveQueue(patch, options);
209
+ }
210
+
211
+ async function savePrefixValue(next: string, base: string) {
212
+ prefixInFlightRef.current += 1;
213
+ try {
214
+ const saved = await savePatch(
215
+ { routingLabelPrefixes: next },
216
+ { base: { routingLabelPrefixes: base } },
217
+ );
218
+ const serverNow = latestSettingsRef.current.values.routingLabelPrefixes;
219
+ const conflicted = !saved && serverNow !== base;
220
+ updatePrefixField((state) =>
221
+ finishFieldSave({ ...state, server: serverNow }, next, saved, conflicted),
222
+ );
223
+ if (saved) setError(null);
224
+ } finally {
225
+ prefixInFlightRef.current -= 1;
226
+ }
227
+ }
228
+
229
+ function handlePrefixChange(next: string) {
230
+ const base = prefixFieldRef.current.base;
231
+ updatePrefixField((state) => editFieldDraft(state, next));
232
+ void savePrefixValue(next, base);
233
+ }
234
+
235
+ function handlePrefixResync() {
236
+ updatePrefixField((state) =>
237
+ resyncFieldToServer({
238
+ ...state,
239
+ server: latestSettingsRef.current.values.routingLabelPrefixes,
240
+ }),
241
+ );
242
+ setError(null);
243
+ }
244
+
245
+ function handlePrefixReapply() {
246
+ // Compare against the server value shown in the conflict notice, not latestSettingsRef, so an
247
+ // unseen third-client edit arriving before the press still conflicts instead of being overwritten.
248
+ const shownServer = prefixFieldRef.current.server;
249
+ const draft = prefixFieldRef.current.draft;
250
+ void savePrefixValue(draft, shownServer);
251
+ }
252
+
253
+ async function handleAddRule() {
254
+ const parsedRule = parseFallbackRuleInput(ruleText, Date.now());
255
+ if ("error" in parsedRule) {
256
+ setRuleError(parsedRule.error);
257
+ return;
258
+ }
259
+ setRuleError(null);
260
+ // Whether the rule is already listed is decided inside the save queue, on the list that holds
261
+ // every edit queued before this one, not on the rendered settings (which lag a pending save).
262
+ const adder = fallbackRuleAdder(parsedRule.rule);
263
+ const saved = await savePatch(adder.patch);
264
+ if (!saved) return;
265
+ if (adder.wasDuplicate()) {
266
+ setRuleError(
267
+ "A rule for this repository and end time already exists. Remove it first to change its note.",
268
+ );
269
+ return;
270
+ }
271
+ setRuleText("");
272
+ setRuleInputKey((key) => key + 1);
150
273
  }
151
274
 
152
275
  async function handleReset() {
153
276
  setDirQueryText(DEFAULT_PR_RADAR_SETTINGS.sourceCodeDir);
154
- const saved = await savePatch({ ...DEFAULT_PR_RADAR_SETTINGS });
277
+ const saved = await savePatch(
278
+ { ...DEFAULT_PR_RADAR_SETTINGS },
279
+ { base: { routingLabelPrefixes: prefixFieldRef.current.base } },
280
+ );
155
281
  if (saved) {
156
- setPrefixInputKey((key) => key + 1);
282
+ updatePrefixField((state) => ({
283
+ ...initFieldState(DEFAULT_PR_RADAR_SETTINGS.routingLabelPrefixes),
284
+ mountKey: state.mountKey + 1,
285
+ }));
286
+ setError(null);
157
287
  toast.show("PR Radar settings reset to defaults.", { variant: "success" });
158
288
  }
159
289
  }
@@ -276,11 +406,62 @@ function ReadyPrRadarSettings({
276
406
  <SettingsInput
277
407
  label="Allowed label prefixes"
278
408
  hint="Routing script labels that one of these (comma or space separated) admits keep their name. A prefix admits itself, keys that continue it with . or / (ade.kind admits ade.kind.x, not ade.kinder), and every key under a prefix that ends in . or / (ade. admits ade.anything). Prefer the narrowest prefix, such as ade.kind. Every other script label is stored as pr-radar.route.<key>. pr-radar, pr-radar.*, paseo and paseo.* are always reserved."
279
- key={prefixInputKey}
280
- error={labelPrefixesProblem(settings.values.routingLabelPrefixes)}
281
- initialValue={settings.values.routingLabelPrefixes}
409
+ key={prefixField.mountKey}
410
+ error={
411
+ prefixField.conflicted
412
+ ? `Another client updated Allowed label prefixes to ${prefixField.server ? `"${prefixField.server}"` : "(empty)"}. Your edit above was not saved.`
413
+ : labelPrefixesProblem(settings.values.routingLabelPrefixes)
414
+ }
415
+ initialValue={prefixField.base}
282
416
  placeholder="None: all script labels go under pr-radar.route."
283
- onChangeText={(next) => void savePatch({ routingLabelPrefixes: next })}
417
+ onChangeText={handlePrefixChange}
418
+ />
419
+ {prefixField.conflicted ? (
420
+ <>
421
+ <SettingsAction
422
+ label="Load updated label prefixes"
423
+ hint={
424
+ prefixField.server
425
+ ? `Replace your unsaved edit with "${prefixField.server}"`
426
+ : "Replace your unsaved edit with the empty server value"
427
+ }
428
+ actionLabel="Use latest"
429
+ onPress={handlePrefixResync}
430
+ />
431
+ <SettingsAction
432
+ label="Apply your label prefixes"
433
+ hint="Save the text above over the updated server value"
434
+ actionLabel="Re-apply edit"
435
+ onPress={handlePrefixReapply}
436
+ />
437
+ </>
438
+ ) : null}
439
+ {settings.values.routingLabelFallbacks.map((rule) => (
440
+ <SettingsAction
441
+ key={`${rule.repository}-${rule.until}-${rule.note ?? ""}`}
442
+ label={`Label fallback: ${rule.repository}`}
443
+ hint={`${describeFallbackStatus(rule, ruleClock)}${rule.note ? ` - ${rule.note}` : ""}`}
444
+ actionLabel="Remove"
445
+ onPress={() =>
446
+ void savePatch((latest) => ({
447
+ routingLabelFallbacks: removeFallbackRule(latest.routingLabelFallbacks, rule),
448
+ }))
449
+ }
450
+ />
451
+ ))}
452
+ <SettingsInput
453
+ key={ruleInputKey}
454
+ label="Add temporary label fallback"
455
+ hint="owner/name (or *), an ISO-8601 end time within 7 days, then an optional note, e.g. acme/api 2026-11-25T18:00Z fixing the script. While a rule is active, invalid routing script labels are skipped with a warning instead of blocking the launch."
456
+ error={ruleError}
457
+ initialValue=""
458
+ placeholder="owner/name 2026-11-25T18:00Z note"
459
+ onChangeText={setRuleText}
460
+ />
461
+ <SettingsAction
462
+ label="Add fallback rule"
463
+ actionLabel="Add rule"
464
+ onPress={() => void handleAddRule()}
284
465
  />
285
466
  <SettingsAction
286
467
  label="Reset settings"
@@ -8,6 +8,7 @@ import {
8
8
  decideRoutedLabels,
9
9
  describeLabel,
10
10
  describeRejection,
11
+ watchFallbackBoundary,
11
12
  } from "../shared/routed-labels";
12
13
  import type { CwdMode, PrRadarSettings } from "../shared/settings";
13
14
  import { listLocalDirs, type RoutingScriptResult, runRoutingScript } from "../shared/viewer-scope";
@@ -91,10 +92,34 @@ export function StartAgentModal({
91
92
  );
92
93
  const [routedLabels, setRoutedLabels] = useState<unknown>(undefined);
93
94
  const [routeReason, setRouteReason] = useState<string | null>(null);
94
- // What the launch will do with the routing script's labels: apply them or refuse to start.
95
+ // What the launch will do with the routing script's labels: apply them, skip them under a
96
+ // fallback rule, or refuse to start.
97
+ const repository = row?.repository ?? "";
98
+ // The clock the fallback decision reads. A rule that ends while the window is open moves it, so
99
+ // the notice and the Start button follow the rule instead of staying on how it looked at render.
100
+ const [labelClock, setLabelClock] = useState(() => Date.now());
101
+ useEffect(
102
+ () =>
103
+ watchFallbackBoundary(settings.routingLabelFallbacks, labelClock, () =>
104
+ setLabelClock(Date.now()),
105
+ ),
106
+ [settings.routingLabelFallbacks, labelClock],
107
+ );
95
108
  const labelDecision = useMemo(
96
- () => decideRoutedLabels({ labels: routedLabels, policy: settings }),
97
- [routedLabels, settings.routingLabelPrefixes],
109
+ () =>
110
+ decideRoutedLabels({
111
+ labels: routedLabels,
112
+ repository,
113
+ policy: settings,
114
+ now: labelClock,
115
+ }),
116
+ [
117
+ routedLabels,
118
+ repository,
119
+ settings.routingLabelPrefixes,
120
+ settings.routingLabelFallbacks,
121
+ labelClock,
122
+ ],
98
123
  );
99
124
  const routedLabelText =
100
125
  labelDecision.status === "ok"
@@ -130,6 +155,7 @@ export function StartAgentModal({
130
155
  setContextMode(settings.defaultContextMode);
131
156
  setRoutedOptions(undefined);
132
157
  setRoutedLabels(undefined);
158
+ setLabelClock(Date.now());
133
159
  setRouteReason(null);
134
160
  setRouteError(null);
135
161
  setRoutingBusy(false);
@@ -590,8 +616,16 @@ export function StartAgentModal({
590
616
  {describeRejection(rejection)}
591
617
  </Text>
592
618
  ))}
619
+ {labelDecision.fallbackRefusal ? (
620
+ <Text style={{ color: theme.colors.statusDanger, fontSize: 12 }}>
621
+ {labelDecision.fallbackRefusal}
622
+ </Text>
623
+ ) : null}
593
624
  </View>
594
625
  ) : null}
626
+ {labelDecision.status === "fallback" ? (
627
+ <Text style={styles.hint}>{labelDecision.warning}</Text>
628
+ ) : null}
595
629
  {routeError ? (
596
630
  <Text style={{ color: theme.colors.statusDanger, fontSize: 12 }}>{routeError}</Text>
597
631
  ) : null}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@omercnet/paseo-pr-radar",
3
- "version": "3.4.0-next.543.1",
3
+ "version": "3.4.0-next.545.1",
4
4
  "type": "module",
5
5
  "description": "A viewer-aware delivery queue for pull requests linked to Paseo workspaces.",
6
6
  "license": "MIT",