@aiwayds/dsh-opentui 0.26.1 → 0.26.3

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.
@@ -36,6 +36,14 @@ function resolveApprovalGateConfig(config) {
36
36
  }
37
37
  return result;
38
38
  }
39
+ function sessionIdOf(session) {
40
+ if (session !== null && typeof session === "object") {
41
+ const id = session.id;
42
+ if (typeof id === "string" && id !== "")
43
+ return id;
44
+ }
45
+ return "(unknown)";
46
+ }
39
47
  function decideAsk(exec, config) {
40
48
  if (!config.enabled)
41
49
  return;
@@ -50,11 +58,16 @@ function decideAsk(exec, config) {
50
58
  }
51
59
  return decision;
52
60
  }
53
- function createPreExecuteListener(config) {
61
+ function createPreExecuteListener(config, lookup, onBypass) {
54
62
  return (exec, next) => {
55
63
  const decision = decideAsk(exec, config);
56
- if (decision !== undefined)
64
+ if (decision !== undefined) {
65
+ if (lookup?.(exec) === "never") {
66
+ onBypass?.(exec);
67
+ return next();
68
+ }
57
69
  return Promise.resolve(decision);
70
+ }
58
71
  return next();
59
72
  };
60
73
  }
@@ -68,7 +81,29 @@ function apply(ctx, config) {
68
81
  ctx.logger?.warn("[approval-gate] ctx.on unavailable — gate not registered (all tools pass)");
69
82
  return () => {};
70
83
  }
71
- const listener = createPreExecuteListener(policy);
84
+ const lookup = (exec) => {
85
+ const session = exec.agent?.session;
86
+ if (session === undefined || session === null)
87
+ return;
88
+ let approval;
89
+ try {
90
+ approval = typeof ctx.get === "function" ? ctx.get("approval") : undefined;
91
+ } catch {
92
+ return;
93
+ }
94
+ if (typeof approval?.effectivePolicy !== "function")
95
+ return;
96
+ try {
97
+ return approval.effectivePolicy(session);
98
+ } catch {
99
+ return;
100
+ }
101
+ };
102
+ const onBypass = (exec) => {
103
+ const toolName = typeof exec.name === "string" && exec.name !== "" ? exec.name : "(unknown)";
104
+ ctx.logger?.debug?.("[approval-gate] policy bypass: %s passes without ask (session %s effective policy never)", toolName, sessionIdOf(exec.agent?.session));
105
+ };
106
+ const listener = createPreExecuteListener(policy, lookup, onBypass);
72
107
  const unsub = ctx.on("tools/pre-execute", listener);
73
108
  return () => {
74
109
  try {
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "@aiwayds/dsh-opentui-bridge",
3
3
  "version": "0.1.0",
4
- "sha256": "bb8e8e439911492ee14d55e85aeb72964548f51c0b09da503a32cff23050e68b"
4
+ "sha256": "1401f93e4e33ff6fd2c416de9ece67b945b05df0fdaa4411c34db2111e6341f5"
5
5
  }
@@ -8400,12 +8400,9 @@ function apply(ctx, config) {
8400
8400
  logger.warn("[bridge:stat] turn=%s deltas=%d tokenFrames=%d textChars=%d reasonChars=%d", state.turnId, state.deltaCount, state.tokenFrameCount, state.textAcc.length, state.reasoningAcc.length);
8401
8401
  }
8402
8402
  }
8403
- const repinnedChildren = new Set;
8404
- function repinChildApprovalPolicy(childId, parentSessionId) {
8403
+ function syncChildApprovalPolicy(childId, parentSessionId, desiredOverride) {
8405
8404
  if (binding === undefined || parentSessionId !== binding.sessionId)
8406
8405
  return;
8407
- if (repinnedChildren.has(childId))
8408
- return;
8409
8406
  const get = ctx.get;
8410
8407
  if (typeof get !== "function")
8411
8408
  return;
@@ -8414,11 +8411,14 @@ function apply(ctx, config) {
8414
8411
  try {
8415
8412
  sessions = get.call(ctx, "sessions");
8416
8413
  approval = get.call(ctx, "approval");
8417
- } catch {
8414
+ } catch (err) {
8415
+ logger.warn("[dsh-opentui-bridge] child approval sync skipped: service lookup failed child=%s parent=%s: %s", childId, parentSessionId, String(err));
8418
8416
  return;
8419
8417
  }
8420
- if (typeof sessions?.get !== "function")
8418
+ if (typeof sessions?.get !== "function") {
8419
+ logger.warn("[dsh-opentui-bridge] child approval sync skipped: sessions.get unavailable child=%s parent=%s", childId, parentSessionId);
8421
8420
  return;
8421
+ }
8422
8422
  let childSession;
8423
8423
  try {
8424
8424
  childSession = sessions.get(childId);
@@ -8427,19 +8427,61 @@ function apply(ctx, config) {
8427
8427
  }
8428
8428
  if (childSession === undefined || typeof childSession.append !== "function")
8429
8429
  return;
8430
- if (approval !== undefined && typeof approval.overrideOf === "function") {
8431
- const current = approval.overrideOf(childSession);
8432
- if (current !== undefined && current !== "never") {
8433
- repinnedChildren.add(childId);
8430
+ let parentSession;
8431
+ try {
8432
+ parentSession = sessions.get(parentSessionId);
8433
+ } catch (err) {
8434
+ logger.warn("[dsh-opentui-bridge] child approval sync skipped: parent session lookup failed child=%s parent=%s: %s", childId, parentSessionId, String(err));
8435
+ return;
8436
+ }
8437
+ if (parentSession === undefined) {
8438
+ logger.warn("[dsh-opentui-bridge] child approval sync skipped: parent session not found child=%s parent=%s", childId, parentSessionId);
8439
+ return;
8440
+ }
8441
+ if (approval === undefined || typeof approval.effectivePolicy !== "function" || typeof approval.overrideOf !== "function") {
8442
+ logger.warn("[dsh-opentui-bridge] child approval sync skipped: approval service/effectivePolicy/overrideOf unavailable child=%s parent=%s", childId, parentSessionId);
8443
+ return;
8444
+ }
8445
+ let desired;
8446
+ if (desiredOverride === "ask" || desiredOverride === "never") {
8447
+ desired = desiredOverride;
8448
+ } else {
8449
+ try {
8450
+ desired = approval.effectivePolicy(parentSession);
8451
+ } catch (err) {
8452
+ logger.warn("[dsh-opentui-bridge] child approval sync skipped: effectivePolicy threw child=%s parent=%s: %s", childId, parentSessionId, String(err));
8434
8453
  return;
8435
8454
  }
8436
8455
  }
8456
+ if (desired !== "ask" && desired !== "never") {
8457
+ logger.warn("[dsh-opentui-bridge] child approval sync skipped: unresolved desired policy=%s child=%s parent=%s", String(desired), childId, parentSessionId);
8458
+ return;
8459
+ }
8460
+ let current;
8437
8461
  try {
8438
- childSession.append("approval/policy", { policy: "ask" });
8439
- repinnedChildren.add(childId);
8440
- logger.info("[dsh-opentui-bridge] child approval re-pinned to ask child=%s parent=%s (delegation never pin overridden)", childId, parentSessionId);
8462
+ current = approval.overrideOf(childSession);
8441
8463
  } catch (err) {
8442
- logger.warn("[dsh-opentui-bridge] child approval re-pin failed child=%s: %s", childId, String(err));
8464
+ logger.warn("[dsh-opentui-bridge] child approval sync skipped: overrideOf threw child=%s parent=%s: %s", childId, parentSessionId, String(err));
8465
+ return;
8466
+ }
8467
+ if (current === desired)
8468
+ return;
8469
+ try {
8470
+ childSession.append("approval/policy", { policy: desired });
8471
+ if (current === "never" && desired === "ask") {
8472
+ logger.warn("[dsh-opentui-bridge] child approval self-healed never→ask child=%s parent=%s (policy mirror recovered a skipped sync)", childId, parentSessionId);
8473
+ } else {
8474
+ logger.info("[dsh-opentui-bridge] child approval mirrored to %s child=%s parent=%s (was %s)", desired, childId, parentSessionId, String(current));
8475
+ }
8476
+ } catch (err) {
8477
+ logger.warn("[dsh-opentui-bridge] child approval sync append failed child=%s parent=%s: %s", childId, parentSessionId, String(err));
8478
+ }
8479
+ }
8480
+ function syncKnownChildrenOfBinding(bindingSessionId) {
8481
+ for (const [childId, row] of getSubagentIndex(ctx).rows) {
8482
+ if (row.parentSessionId !== bindingSessionId)
8483
+ continue;
8484
+ syncChildApprovalPolicy(childId, bindingSessionId);
8443
8485
  }
8444
8486
  }
8445
8487
  function onSessionEvent(session, event) {
@@ -8449,6 +8491,39 @@ function apply(ctx, config) {
8449
8491
  const e = event;
8450
8492
  const type = typeof e?.type === "string" ? e.type : "";
8451
8493
  const data = e?.data !== null && typeof e?.data === "object" ? e.data : {};
8494
+ if (binding !== undefined) {
8495
+ if (type === "approval/policy" && sessionId === binding.sessionId) {
8496
+ for (const [childId, row] of getSubagentIndex(ctx).rows) {
8497
+ if (row.parentSessionId !== sessionId)
8498
+ continue;
8499
+ syncChildApprovalPolicy(childId, sessionId, data.policy);
8500
+ }
8501
+ }
8502
+ if (type === "turn/start" && sessionId === binding.sessionId) {
8503
+ syncKnownChildrenOfBinding(sessionId);
8504
+ }
8505
+ if (sessionId !== binding.sessionId) {
8506
+ const index = getSubagentIndex(ctx);
8507
+ if (index.has(sessionId)) {
8508
+ if (type === "turn/start") {
8509
+ const row = index.get(sessionId);
8510
+ if (row !== undefined && row.parentSessionId === binding.sessionId) {
8511
+ const childId = sessionId;
8512
+ const parentId = binding.sessionId;
8513
+ queueMicrotask(() => syncChildApprovalPolicy(childId, parentId));
8514
+ }
8515
+ }
8516
+ } else {
8517
+ const header = s.header;
8518
+ if (header?.origin === "subagent" && header?.parentSession === binding.sessionId) {
8519
+ index.noteCatalog(sessionId, binding.sessionId);
8520
+ const childId = sessionId;
8521
+ const parentId = binding.sessionId;
8522
+ queueMicrotask(() => syncChildApprovalPolicy(childId, parentId));
8523
+ }
8524
+ }
8525
+ }
8526
+ }
8452
8527
  if (type === "subagent/catalog") {
8453
8528
  const boundId = binding?.sessionId;
8454
8529
  if (sessionId !== boundId)
@@ -8457,7 +8532,7 @@ function apply(ctx, config) {
8457
8532
  if (!childId)
8458
8533
  return;
8459
8534
  getSubagentIndex(ctx).noteCatalog(childId, sessionId, typeof data.childCreatedAt === "number" ? data.childCreatedAt : undefined);
8460
- repinChildApprovalPolicy(childId, sessionId);
8535
+ syncChildApprovalPolicy(childId, sessionId);
8461
8536
  const turnId = state !== undefined && !state.ended ? state.turnId : inFlightTurnId ?? "";
8462
8537
  const label = typeof data.label === "string" && data.label !== "" ? data.label : typeof data.name === "string" ? data.name : "";
8463
8538
  const name = typeof data.name === "string" && data.name !== "" ? data.name : "";
@@ -8842,6 +8917,11 @@ function apply(ctx, config) {
8842
8917
  startPushRelay(binding.sessionId);
8843
8918
  startAccountRelay(binding.sessionId);
8844
8919
  restoreSessionGrantsFromJournal(binding.sessionId);
8920
+ try {
8921
+ syncKnownChildrenOfBinding(binding.sessionId);
8922
+ } catch (err) {
8923
+ logger.warn("[dsh-opentui-bridge] resume child approval sync failed session=%s: %s", binding.sessionId, String(err));
8924
+ }
8845
8925
  return;
8846
8926
  }
8847
8927
  if (!replace) {
@@ -8908,6 +8988,11 @@ function apply(ctx, config) {
8908
8988
  pushResumePendingSnapshot(sessionId);
8909
8989
  restoreSessionGrantsFromJournal(sessionId);
8910
8990
  }
8991
+ try {
8992
+ syncKnownChildrenOfBinding(sessionId);
8993
+ } catch (err) {
8994
+ logger.warn("[dsh-opentui-bridge] bind-time child approval sync failed session=%s: %s", sessionId, String(err));
8995
+ }
8911
8996
  } catch (err) {
8912
8997
  logger.error("[dsh-opentui-bridge] session.open failed: %s", err);
8913
8998
  const mapped = mapHostError(err);
@@ -9119,6 +9204,8 @@ function apply(ctx, config) {
9119
9204
  if (raw !== null && typeof raw === "object" && !Array.isArray(raw) && Array.isArray(raw.items)) {
9120
9205
  const count = getSubagentIndex(ctx).syncFromList(raw.items);
9121
9206
  logger.info("[dsh-opentui-bridge] subagent index reconciled: subagents=%d rows=%d", count, getSubagentIndex(ctx).rows.size);
9207
+ if (binding !== undefined)
9208
+ syncKnownChildrenOfBinding(binding.sessionId);
9122
9209
  } else {
9123
9210
  logger.warn("[dsh-opentui-bridge] subagent index reconcile skipped: malformed session.list reply");
9124
9211
  }