@cnzgray/dsh-mattpocock-workflow 0.4.0 → 0.5.0

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.
Files changed (3) hide show
  1. package/lib/client.js +238 -55
  2. package/lib/index.js +1099 -436
  3. package/package.json +1 -1
package/lib/client.js CHANGED
@@ -184,6 +184,7 @@ var zh = {
184
184
  "refresh.action": "\u5237\u65B0",
185
185
  "refresh.busy": "\u5237\u65B0\u4E2D\u2026",
186
186
  "refresh.full": "\u5168\u91CF\u5237\u65B0",
187
+ "refresh.title": "\u5237\u65B0\uFF08Shift+\u70B9\u51FB\uFF1A\u5FFD\u7565\u672C\u5730\u7F13\u5B58\u5168\u91CF\u91CD\u62C9\uFF09",
187
188
  "tickets.loaded": "\u5DF2\u52A0\u8F7D {loaded} / \u5171 {total}",
188
189
  "tickets.loadedCapped": "\uFF0C\u5DF2\u8FBE\u4E0A\u9650",
189
190
  "tickets.emptyRepo": "\u8FD9\u4E2A\u4ED3\u5E93\u8FD8\u6CA1\u6709\u5DE5\u5355\u3002",
@@ -303,6 +304,7 @@ var zh = {
303
304
  "error.rpcBadResponse": "\u5BBF\u4E3B\u8FD4\u56DE\u7684\u54CD\u5E94\u683C\u5F0F\u4E0D\u6B63\u786E\u3002",
304
305
  "error.rpcNetworkFailed": "\u7F51\u7EDC\u8BF7\u6C42\u5931\u8D25\uFF1A{detail}",
305
306
  "error.snapshotNoWorkspace": "\u5F53\u524D\u4F1A\u8BDD\u6CA1\u6709\u5DE5\u4F5C\u533A\u76EE\u5F55\uFF0C\u65E0\u6CD5\u8BFB\u53D6\u5DE5\u5355\u3002",
307
+ "error.deltaWithoutBase": "\u5237\u65B0\u56DE\u5305\u7F3A\u5C11\u57FA\u51C6\u5FEB\u7167\uFF0C\u5DF2\u4FDD\u7559\u5F53\u524D\u6570\u636E\u3002",
306
308
  "error.openEnvCheck": "\u67E5\u770B\u73AF\u5883\u68C0\u67E5",
307
309
  "env.summary": "\u73AF\u5883\u68C0\u67E5 {pass}/{total}",
308
310
  "env.recheck": "\u91CD\u65B0\u68C0\u67E5",
@@ -656,6 +658,7 @@ var en = {
656
658
  "refresh.action": "Refresh",
657
659
  "refresh.busy": "Refreshing\u2026",
658
660
  "refresh.full": "Full refresh",
661
+ "refresh.title": "Refresh (Shift+click: refetch all, ignoring the local store)",
659
662
  "tickets.loaded": "Loaded {loaded} of {total}",
660
663
  "tickets.loadedCapped": " (limit reached)",
661
664
  "tickets.emptyRepo": "This repository has no tickets yet.",
@@ -775,6 +778,7 @@ var en = {
775
778
  "error.rpcBadResponse": "The host returned a malformed response.",
776
779
  "error.rpcNetworkFailed": "Network request failed: {detail}",
777
780
  "error.snapshotNoWorkspace": "This session has no workspace directory; cannot read tickets.",
781
+ "error.deltaWithoutBase": "The refresh payload has no base snapshot; kept the current data.",
778
782
  "error.openEnvCheck": "Check environment",
779
783
  "env.summary": "Environment check {pass}/{total}",
780
784
  "env.recheck": "Recheck",
@@ -1326,6 +1330,8 @@ var ERROR_CODES = {
1326
1330
  fetchFailed: "fetch-failed",
1327
1331
  /** 编号不存在或工单已被删除。 */
1328
1332
  ticketNotFound: "ticket-not-found",
1333
+ /** 后端拒绝了请求(限流);携带服务端要求的重试等待时按它退避。 */
1334
+ rateLimited: "rate-limited",
1329
1335
  /** 当前后端不提供所请求的详情或写能力。 */
1330
1336
  capabilityUnsupported: "capability-unsupported",
1331
1337
  invalidParams: "invalid-params",
@@ -1351,8 +1357,12 @@ function createPanelClient(t2, fetchImpl = defaultFetch) {
1351
1357
  get: (workspaceRoot) => call(fetchImpl, t2, RPC_METHODS.snapshotGet, { workspaceRoot }, isSnapshotResult),
1352
1358
  more: (workspaceRoot) => call(fetchImpl, t2, RPC_METHODS.snapshotMore, { workspaceRoot }, isSnapshotResult),
1353
1359
  refresh: (workspaceRoot, options) => {
1354
- const params = options?.full === true ? { workspaceRoot, full: true } : { workspaceRoot };
1355
- return call(fetchImpl, t2, RPC_METHODS.snapshotRefresh, params, isSnapshotResult);
1360
+ const params = {
1361
+ workspaceRoot,
1362
+ ...options?.full === true ? { full: true } : {},
1363
+ ...options?.ifVersion === void 0 ? {} : { ifVersion: options.ifVersion }
1364
+ };
1365
+ return call(fetchImpl, t2, RPC_METHODS.snapshotRefresh, params, isSnapshotRefreshResult);
1356
1366
  },
1357
1367
  cache: (workspaceRoot, options) => {
1358
1368
  const params = options?.clear === true ? { workspaceRoot, clear: true, confirm: SNAPSHOT_CLEAR_CONFIRMATION } : { workspaceRoot };
@@ -1433,6 +1443,23 @@ function isRpcResult(value, isPayload) {
1433
1443
  function isSnapshotResult(value) {
1434
1444
  return isRpcResult(value, isWorkspaceSnapshot);
1435
1445
  }
1446
+ function isSnapshotRefreshResult(value) {
1447
+ return isRpcResult(value, isSnapshotRefreshResultPayload);
1448
+ }
1449
+ function isSnapshotRefreshResultPayload(value) {
1450
+ if (!isObject(value)) return false;
1451
+ if (typeof value.kind !== "string") return false;
1452
+ if (value.kind === "full") {
1453
+ return "snapshot" in value && isWorkspaceSnapshot(value.snapshot);
1454
+ }
1455
+ if (value.kind === "not-modified") {
1456
+ return typeof value.version === "number" && typeof value.checkedAt === "number";
1457
+ }
1458
+ if (value.kind === "changes") {
1459
+ return typeof value.version === "number" && typeof value.fetchedAt === "number" && Array.isArray(value.tickets) && value.tickets.every(hasTicketFilterFields) && isStringArray(value.removed) && typeof value.metrics === "object" && value.metrics !== null && (value.viewer === null || typeof value.viewer === "string") && typeof value.total === "number" && typeof value.loaded === "number" && typeof value.windowLimit === "number" && typeof value.blockersFinalized === "boolean";
1460
+ }
1461
+ return false;
1462
+ }
1436
1463
  function isWorkspaceSnapshot(value) {
1437
1464
  if (typeof value !== "object" || value === null) return false;
1438
1465
  if (!("backend" in value) || typeof value.backend !== "string") return false;
@@ -1448,6 +1475,9 @@ function isWorkspaceSnapshot(value) {
1448
1475
  if (!("hierarchy" in value) || !(value.hierarchy === "native" || value.hierarchy === "convention" || value.hierarchy === "none")) {
1449
1476
  return false;
1450
1477
  }
1478
+ if (!("libraryVersion" in value) || !(value.libraryVersion === null || typeof value.libraryVersion === "number")) {
1479
+ return false;
1480
+ }
1451
1481
  if (!("tickets" in value) || !Array.isArray(value.tickets)) return false;
1452
1482
  if (!value.tickets.every(hasTicketFilterFields)) return false;
1453
1483
  if (!("metrics" in value) || typeof value.metrics !== "object" || value.metrics === null) {
@@ -5951,11 +5981,33 @@ async function loadSnapshot(target, mode) {
5951
5981
  const quiet = mode === "auto" && target.hasData.current;
5952
5982
  if (!quiet) target.setPhase(mode === "get" ? "loading" : "refreshing");
5953
5983
  target.setError(null);
5954
- const result = mode === "get" ? await target.client.get(target.workspaceRoot) : await target.client.refresh(target.workspaceRoot, { full: mode === "refresh-full" });
5984
+ if (mode === "get") {
5985
+ const result2 = await target.client.get(target.workspaceRoot);
5986
+ if (current !== target.sequence.current) return null;
5987
+ if (!result2.ok) return applyFailure(target, mode, quiet, result2.error);
5988
+ const root2 = target.workspaceRoot;
5989
+ return quiet ? applyQuiet(target, root2, current, result2.result) : applyLoaded(target, root2, current, result2.result);
5990
+ }
5991
+ const held = target.latest.current?.libraryVersion;
5992
+ const result = await target.client.refresh(target.workspaceRoot, {
5993
+ ...mode === "refresh-full" ? { full: true } : {},
5994
+ ...held === null || held === void 0 ? {} : { ifVersion: held }
5995
+ });
5955
5996
  if (current !== target.sequence.current) return null;
5956
5997
  if (!result.ok) return applyFailure(target, mode, quiet, result.error);
5957
5998
  const root = target.workspaceRoot;
5958
- return quiet ? applyQuiet(target, root, current, result.result) : applyLoaded(target, root, current, result.result);
5999
+ const payload = result.result;
6000
+ if (payload.kind === "not-modified") {
6001
+ return applyNotModified(target, root, current, quiet, payload.checkedAt);
6002
+ }
6003
+ const snapshot = payload.kind === "full" ? payload.snapshot : mergeChanges(target, payload);
6004
+ if (snapshot === null) {
6005
+ return applyFailure(target, mode, quiet, {
6006
+ code: "fetch-failed",
6007
+ message: target.t("error.deltaWithoutBase")
6008
+ });
6009
+ }
6010
+ return quiet ? applyQuiet(target, root, current, snapshot) : applyLoaded(target, root, current, snapshot);
5959
6011
  }
5960
6012
  async function loadMoreSnapshot(target, running) {
5961
6013
  if (target.workspaceRoot === void 0 || running.current) return null;
@@ -5994,6 +6046,42 @@ function applyFailure(target, mode, quiet, error) {
5994
6046
  if (!quiet) target.setPhase("ready");
5995
6047
  return null;
5996
6048
  }
6049
+ async function applyNotModified(target, root, current, quiet, checkedAt) {
6050
+ const base = target.latest.current;
6051
+ target.setRefreshError(null);
6052
+ if (!quiet) target.setPhase("ready");
6053
+ if (base === null) return null;
6054
+ const verified = {
6055
+ ...base,
6056
+ source: "live",
6057
+ stale: false,
6058
+ refreshError: null,
6059
+ fetchedAt: checkedAt
6060
+ };
6061
+ return applyQuiet(target, root, current, verified);
6062
+ }
6063
+ function mergeChanges(target, changes) {
6064
+ const base = target.latest.current;
6065
+ if (base === null) return null;
6066
+ const byRef = new Map(base.tickets.map((ticket) => [ticket.ref, ticket]));
6067
+ for (const ref of changes.removed) byRef.delete(ref);
6068
+ for (const row of changes.tickets) byRef.set(row.ref, row);
6069
+ return {
6070
+ ...base,
6071
+ tickets: [...byRef.values()],
6072
+ metrics: changes.metrics,
6073
+ viewer: changes.viewer,
6074
+ total: changes.total,
6075
+ loaded: changes.loaded,
6076
+ windowLimit: changes.windowLimit,
6077
+ fetchedAt: changes.fetchedAt,
6078
+ libraryVersion: changes.version,
6079
+ source: "live",
6080
+ stale: false,
6081
+ refreshError: null,
6082
+ blockersFinalized: changes.blockersFinalized
6083
+ };
6084
+ }
5997
6085
  async function applyQuiet(target, root, current, result) {
5998
6086
  let settled = result;
5999
6087
  if (!settled.blockersFinalized) {
@@ -7240,8 +7328,41 @@ async function copyText2(text, t2) {
7240
7328
  }
7241
7329
  }
7242
7330
 
7331
+ // src/client/detail-cache.ts
7332
+ var DETAIL_CACHE_PER_WORKSPACE = 100;
7333
+ var DETAIL_CACHE_WORKSPACES = 5;
7334
+ function detailCacheDecision(hit, currentUpdatedAt) {
7335
+ if (hit !== null && currentUpdatedAt !== void 0 && hit.updatedAt === currentUpdatedAt) {
7336
+ return "hit";
7337
+ }
7338
+ return hit !== null ? "stale" : "miss";
7339
+ }
7340
+ var caches = /* @__PURE__ */ new Map();
7341
+ function readDetailCache(workspaceRoot, ref) {
7342
+ return caches.get(workspaceRoot)?.get(ref) ?? null;
7343
+ }
7344
+ function writeDetailCache(workspaceRoot, detail) {
7345
+ let cache = caches.get(workspaceRoot);
7346
+ if (cache === void 0) {
7347
+ while (caches.size >= DETAIL_CACHE_WORKSPACES) {
7348
+ const oldest = caches.keys().next().value;
7349
+ if (oldest === void 0) break;
7350
+ caches.delete(oldest);
7351
+ }
7352
+ cache = /* @__PURE__ */ new Map();
7353
+ caches.set(workspaceRoot, cache);
7354
+ }
7355
+ cache.delete(detail.ref);
7356
+ cache.set(detail.ref, { detail, updatedAt: detail.updatedAt });
7357
+ while (cache.size > DETAIL_CACHE_PER_WORKSPACE) {
7358
+ const oldest = cache.keys().next().value;
7359
+ if (oldest === void 0) break;
7360
+ cache.delete(oldest);
7361
+ }
7362
+ }
7363
+
7243
7364
  // src/client/ticket-detail-hooks.ts
7244
- function useDetailRequest(client, workspaceRoot, ref, number) {
7365
+ function useDetailRequest(client, workspaceRoot, ref, number, currentUpdatedAt) {
7245
7366
  const [detail, setDetail] = React26.useState(null);
7246
7367
  const [error, setError] = React26.useState(null);
7247
7368
  const [phase, setPhase] = React26.useState("loading");
@@ -7251,29 +7372,22 @@ function useDetailRequest(client, workspaceRoot, ref, number) {
7251
7372
  const readId = React26.useRef(0);
7252
7373
  const readRunning = React26.useRef(false);
7253
7374
  React26.useEffect(() => {
7254
- let active = true;
7255
- readId.current += 1;
7256
- readRunning.current = false;
7257
- setDetail(null);
7258
- setError(null);
7259
- setPhase("loading");
7260
- setRefreshing(false);
7261
- setRefreshError(null);
7262
- void client.getDetail(workspaceRoot, ref, number).then((result) => {
7263
- if (!active) return;
7264
- if (result.ok) {
7265
- setDetail(result.result);
7266
- setPhase("ready");
7267
- return;
7268
- }
7269
- setError(result.error);
7270
- setPhase("error");
7375
+ return startDetailLoad({
7376
+ client,
7377
+ workspaceRoot,
7378
+ ref,
7379
+ number,
7380
+ currentUpdatedAt,
7381
+ attempt,
7382
+ readId,
7383
+ readRunning,
7384
+ setDetail,
7385
+ setError,
7386
+ setPhase,
7387
+ setRefreshing,
7388
+ setRefreshError
7271
7389
  });
7272
- return () => {
7273
- active = false;
7274
- readId.current += 1;
7275
- };
7276
- }, [client, workspaceRoot, ref, number, attempt]);
7390
+ }, [client, workspaceRoot, ref, number, attempt, currentUpdatedAt]);
7277
7391
  const refresh = () => {
7278
7392
  if (readRunning.current) return;
7279
7393
  readRunning.current = true;
@@ -7284,12 +7398,16 @@ function useDetailRequest(client, workspaceRoot, ref, number) {
7284
7398
  if (readId.current !== current) return;
7285
7399
  readRunning.current = false;
7286
7400
  setRefreshing(false);
7287
- if (result.ok) {
7288
- setDetail(result.result);
7289
- setRefreshError(null);
7290
- return;
7291
- }
7292
- setRefreshError(result.error);
7401
+ applyDetailRead({
7402
+ workspaceRoot,
7403
+ result,
7404
+ cached: true,
7405
+ setDetail,
7406
+ setPhase,
7407
+ setError,
7408
+ setRefreshError
7409
+ });
7410
+ if (result.ok) setRefreshError(null);
7293
7411
  });
7294
7412
  };
7295
7413
  return {
@@ -7299,12 +7417,85 @@ function useDetailRequest(client, workspaceRoot, ref, number) {
7299
7417
  reload: () => {
7300
7418
  setAttempt((value) => value + 1);
7301
7419
  },
7302
- replace: setDetail,
7420
+ replace: (next) => {
7421
+ writeDetailCache(workspaceRoot, next);
7422
+ setDetail(next);
7423
+ },
7303
7424
  refresh,
7304
7425
  refreshing,
7305
7426
  refreshError
7306
7427
  };
7307
7428
  }
7429
+ function startDetailLoad(target) {
7430
+ const {
7431
+ client,
7432
+ workspaceRoot,
7433
+ ref,
7434
+ number,
7435
+ currentUpdatedAt,
7436
+ readId,
7437
+ readRunning,
7438
+ setDetail,
7439
+ setError,
7440
+ setPhase,
7441
+ setRefreshing,
7442
+ setRefreshError
7443
+ } = target;
7444
+ let active = true;
7445
+ readId.current += 1;
7446
+ readRunning.current = false;
7447
+ setError(null);
7448
+ setRefreshing(false);
7449
+ setRefreshError(null);
7450
+ const hit = readDetailCache(workspaceRoot, ref);
7451
+ const decision = detailCacheDecision(hit, currentUpdatedAt);
7452
+ if (decision === "hit") {
7453
+ if (hit !== null) setDetail(hit.detail);
7454
+ setPhase("ready");
7455
+ return () => {
7456
+ active = false;
7457
+ readId.current += 1;
7458
+ };
7459
+ }
7460
+ if (decision === "stale") {
7461
+ if (hit !== null) setDetail(hit.detail);
7462
+ setPhase("ready");
7463
+ } else {
7464
+ setDetail(null);
7465
+ setPhase("loading");
7466
+ }
7467
+ void client.getDetail(workspaceRoot, ref, number).then((result) => {
7468
+ if (!active) return;
7469
+ applyDetailRead({
7470
+ workspaceRoot,
7471
+ result,
7472
+ cached: decision !== "miss",
7473
+ setDetail,
7474
+ setPhase,
7475
+ setError,
7476
+ setRefreshError
7477
+ });
7478
+ });
7479
+ return () => {
7480
+ active = false;
7481
+ readId.current += 1;
7482
+ };
7483
+ }
7484
+ function applyDetailRead(options) {
7485
+ const { result } = options;
7486
+ if (result.ok) {
7487
+ writeDetailCache(options.workspaceRoot, result.result);
7488
+ options.setDetail(result.result);
7489
+ options.setPhase("ready");
7490
+ return;
7491
+ }
7492
+ if (options.cached) {
7493
+ options.setRefreshError(result.error);
7494
+ return;
7495
+ }
7496
+ options.setError(result.error);
7497
+ options.setPhase("error");
7498
+ }
7308
7499
  function useTicketMutation(client, options) {
7309
7500
  const [busy, setBusy] = React26.useState(false);
7310
7501
  const [error, setError] = React26.useState(null);
@@ -7330,7 +7521,9 @@ function useTicketMutation(client, options) {
7330
7521
  client.getDetail(options.workspaceRoot, options.ref, options.number),
7331
7522
  client.refresh(options.workspaceRoot)
7332
7523
  ]);
7333
- if (snapshot.ok) options.onSnapshot(snapshot.result);
7524
+ if (snapshot.ok && snapshot.result.kind === "full") {
7525
+ options.onSnapshot(snapshot.result.snapshot);
7526
+ }
7334
7527
  if (!fresh.ok) {
7335
7528
  setError(fresh.error);
7336
7529
  return;
@@ -10844,12 +11037,12 @@ function TicketDetailView(props) {
10844
11037
  function useDetailViewState(props) {
10845
11038
  const { client, workspaceRoot } = props;
10846
11039
  const { ref, number, ticket } = props.target;
10847
- const request = useDetailRequest(client, workspaceRoot, ref, number);
11040
+ const { current: currentTicket, updateFromSnapshot } = useCurrentTicket(ref, ticket);
11041
+ const request = useDetailRequest(client, workspaceRoot, ref, number, currentTicket?.updatedAt);
10848
11042
  const loadAttachment = React29.useMemo(
10849
11043
  () => createAttachmentLoader(client, workspaceRoot),
10850
11044
  [client, workspaceRoot]
10851
11045
  );
10852
- const { current: currentTicket, updateFromSnapshot } = useCurrentTicket(ref, ticket);
10853
11046
  const blockers = blockerItems(currentTicket, props.snapshotTickets);
10854
11047
  const canExpandBlockers = currentTicket !== null && currentTicket.skillActionReason === "blocked" && blockers.length > 0;
10855
11048
  const [expandedRef, setExpandedRef] = React29.useState(null);
@@ -11701,12 +11894,6 @@ function Footer(props) {
11701
11894
 
11702
11895
  // src/client/tickets-header.tsx
11703
11896
  var React36 = __toESM(require("react"), 1);
11704
- var SECONDARY_BUTTON_STYLE = {
11705
- ...BUTTON_STYLE,
11706
- padding: "2px 6px",
11707
- fontSize: 12,
11708
- color: COLORS.secondary
11709
- };
11710
11897
  var REFRESH_ERROR_STYLE2 = {
11711
11898
  color: COLORS.error,
11712
11899
  fontSize: 12,
@@ -11740,22 +11927,18 @@ function TicketsHeader(props) {
11740
11927
  "data-testid": "refresh",
11741
11928
  "data-hover": "soft",
11742
11929
  disabled: props.busy,
11743
- onClick: props.onRefresh,
11930
+ title: t2("refresh.title"),
11931
+ onClick: (event) => {
11932
+ if (event.shiftKey) {
11933
+ props.onRefreshFull();
11934
+ return;
11935
+ }
11936
+ props.onRefresh();
11937
+ },
11744
11938
  style: { ...REFRESH_BUTTON_STYLE, opacity: props.busy ? 0.6 : 1 }
11745
11939
  },
11746
11940
  /* @__PURE__ */ React36.createElement(RefreshIcon, null),
11747
11941
  props.busy ? t2("refresh.busy") : t2("refresh.action")
11748
- ), /* @__PURE__ */ React36.createElement(
11749
- "button",
11750
- {
11751
- type: "button",
11752
- "data-testid": "refresh-full",
11753
- "data-hover": "soft",
11754
- disabled: props.busy,
11755
- onClick: props.onRefreshFull,
11756
- style: { ...SECONDARY_BUTTON_STYLE, opacity: props.busy ? 0.6 : 1 }
11757
- },
11758
- t2("refresh.full")
11759
11942
  )));
11760
11943
  }
11761
11944
  function SourceBadge2(props) {