pi-harness-runtime 1.1.507 → 1.1.509

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 (86) hide show
  1. package/CHANGELOG.md +171 -0
  2. package/harness/auto-resume-marker.ts +219 -0
  3. package/harness/scope-expansion-runtime.ts +464 -0
  4. package/harness/scope-expansion.ts +212 -23
  5. package/index.ts +502 -25
  6. package/package.json +1 -1
  7. package/packages/agent-policy/package.json +1 -1
  8. package/packages/auth/package.json +1 -1
  9. package/packages/autonomous-runtime/package.json +1 -1
  10. package/packages/checkpoint/package.json +1 -1
  11. package/packages/clipboard/package.json +1 -1
  12. package/packages/codex-watcher/package.json +1 -1
  13. package/packages/config-capture/package.json +1 -1
  14. package/packages/contracts/package.json +1 -1
  15. package/packages/cookie-sanitizer/package.json +1 -1
  16. package/packages/evaluation-engine/package.json +1 -1
  17. package/packages/event-bus/package.json +1 -1
  18. package/packages/event-store/package.json +1 -1
  19. package/packages/experience-replay/package.json +1 -1
  20. package/packages/file-copy-helper/package.json +1 -1
  21. package/packages/intent-analyzer/package.json +1 -1
  22. package/packages/jev-judge/package.json +1 -1
  23. package/packages/knowledge-retrieval/package.json +1 -1
  24. package/packages/learning-engine/package.json +1 -1
  25. package/packages/logging/package.json +1 -1
  26. package/packages/memory-engine/package.json +1 -1
  27. package/packages/notification/dist/adapters/detail-format.d.ts +1 -1
  28. package/packages/notification/dist/adapters/detail-format.d.ts.map +1 -1
  29. package/packages/notification/dist/adapters/detail-format.js +12 -0
  30. package/packages/notification/dist/adapters/detail-format.js.map +1 -1
  31. package/packages/notification/dist/adapters/email-adapter.d.ts.map +1 -1
  32. package/packages/notification/dist/adapters/email-adapter.js +2 -0
  33. package/packages/notification/dist/adapters/email-adapter.js.map +1 -1
  34. package/packages/notification/dist/adapters/line-adapter.d.ts.map +1 -1
  35. package/packages/notification/dist/adapters/line-adapter.js +2 -0
  36. package/packages/notification/dist/adapters/line-adapter.js.map +1 -1
  37. package/packages/notification/dist/adapters/ntfy-adapter.d.ts.map +1 -1
  38. package/packages/notification/dist/adapters/ntfy-adapter.js +2 -0
  39. package/packages/notification/dist/adapters/ntfy-adapter.js.map +1 -1
  40. package/packages/notification/dist/adapters/telegram-adapter.d.ts +0 -1
  41. package/packages/notification/dist/adapters/telegram-adapter.d.ts.map +1 -1
  42. package/packages/notification/dist/adapters/telegram-adapter.js +2 -3
  43. package/packages/notification/dist/adapters/telegram-adapter.js.map +1 -1
  44. package/packages/notification/dist/notification-center.d.ts +8 -4
  45. package/packages/notification/dist/notification-center.d.ts.map +1 -1
  46. package/packages/notification/dist/notification-center.js +32 -9
  47. package/packages/notification/dist/notification-center.js.map +1 -1
  48. package/packages/notification/dist/notification-policy.d.ts.map +1 -1
  49. package/packages/notification/dist/notification-policy.js +6 -0
  50. package/packages/notification/dist/notification-policy.js.map +1 -1
  51. package/packages/notification/dist/pending-question.d.ts +20 -1
  52. package/packages/notification/dist/pending-question.d.ts.map +1 -1
  53. package/packages/notification/dist/pending-question.js +9 -3
  54. package/packages/notification/dist/pending-question.js.map +1 -1
  55. package/packages/notification/dist/types.d.ts +10 -1
  56. package/packages/notification/dist/types.d.ts.map +1 -1
  57. package/packages/notification/package.json +1 -1
  58. package/packages/notification/src/adapters/detail-format.ts +17 -1
  59. package/packages/notification/src/adapters/email-adapter.ts +2 -0
  60. package/packages/notification/src/adapters/line-adapter.ts +2 -0
  61. package/packages/notification/src/adapters/ntfy-adapter.ts +2 -0
  62. package/packages/notification/src/adapters/telegram-adapter.ts +2 -4
  63. package/packages/notification/src/notification-center.ts +32 -9
  64. package/packages/notification/src/notification-policy.ts +6 -0
  65. package/packages/notification/src/pending-question.ts +28 -2
  66. package/packages/notification/src/types.ts +11 -0
  67. package/packages/paperclip-integration/package.json +1 -1
  68. package/packages/performance-optimizer/package.json +1 -1
  69. package/packages/privilege-broker/package.json +1 -1
  70. package/packages/prompt-compiler/package.json +1 -1
  71. package/packages/providers/package.json +1 -1
  72. package/packages/qdrant-skills/package.json +1 -1
  73. package/packages/quota-manager/package.json +1 -1
  74. package/packages/relay/package.json +1 -1
  75. package/packages/skill-mcp-client/package.json +1 -1
  76. package/packages/skills/package.json +1 -1
  77. package/packages/todo-bd-sync/package.json +1 -1
  78. package/packages/token-estimation/package.json +1 -1
  79. package/packages/tooling-preference/package.json +1 -1
  80. package/packages/trajectory/package.json +1 -1
  81. package/packages/tui/package.json +1 -1
  82. package/packages/two-phase-review/package.json +1 -1
  83. package/packages/types/package.json +1 -1
  84. package/packages/webmcp-tester/package.json +1 -1
  85. package/packages/wiki/package.json +1 -1
  86. package/packages/write-review/package.json +1 -1
@@ -105,6 +105,8 @@ export interface CreateScopeProposalInput {
105
105
  scope: string;
106
106
  validation: string;
107
107
  risks?: string[];
108
+ /** The choices the human has. */
109
+ choices?: string[];
108
110
  now: number;
109
111
  }
110
112
 
@@ -113,16 +115,22 @@ export interface CreateScopeProposalInput {
113
115
  *
114
116
  * The proposal names a bounded subset of the candidates: a candidate that
115
117
  * is not named is not proposed, and an id that is not a candidate is
116
- * refused. The deadline is absolute, so every reader measures the same
117
- * moment.
118
+ * refused. The bound applies to the proposed ids, and an id is checked
119
+ * against the whole list of the candidates: a bound that drops a known id
120
+ * would refuse a proposal of ids that the caller knew.
121
+ *
122
+ * The deadline is absolute, so every reader measures the same moment.
118
123
  */
119
124
  export function createScopeProposal(input: CreateScopeProposalInput): ScopeProposal {
120
- const candidates = input.candidates.slice(0, MAX_SCOPE_CANDIDATES);
121
- const known = new Set(candidates.map((candidate) => candidate.id));
122
- const proposedIds = input.proposedIds.filter((id) => known.has(id));
125
+ const known = new Set(input.candidates.map((candidate) => candidate.id));
126
+ const proposedIds = input.proposedIds
127
+ .filter((id) => known.has(id))
128
+ .slice(0, MAX_SCOPE_CANDIDATES);
123
129
  if (proposedIds.length === 0) {
124
130
  throw new Error("a scope proposal needs at least one candidate id");
125
131
  }
132
+ const proposed = new Set(proposedIds);
133
+ const candidates = input.candidates.filter((candidate) => proposed.has(candidate.id));
126
134
  return {
127
135
  id: input.id,
128
136
  sessionId: input.sessionId,
@@ -134,7 +142,10 @@ export function createScopeProposal(input: CreateScopeProposalInput): ScopePropo
134
142
  scope: input.scope,
135
143
  validation: input.validation,
136
144
  risks: input.risks ?? [],
137
- choices: ["approve the proposed subset", "decline", "revise the proposal"],
145
+ choices:
146
+ input.choices && input.choices.length > 0
147
+ ? input.choices
148
+ : ["approve the proposed subset", "decline", "revise the proposal"],
138
149
  createdAt: input.now,
139
150
  deadlineAt: input.now + SCOPE_EXPANSION_WINDOW_MS,
140
151
  state: "proposed",
@@ -162,7 +173,7 @@ export function scopeProposalMessage(proposal: ScopeProposal): string {
162
173
  lines.push(
163
174
  `Choices: ${proposal.choices.join(" / ")}.`,
164
175
  `Deadline: ${new Date(proposal.deadlineAt).toISOString()}.`,
165
- `If unanswered for 5 minutes, Jev may decide this proposal under your policy.`,
176
+ `If unanswered for ${Math.round(SCOPE_EXPANSION_WINDOW_MS / 60_000)} minutes, Jev may decide this proposal under your policy.`,
166
177
  );
167
178
  return lines.join("\n");
168
179
  }
@@ -170,20 +181,25 @@ export function scopeProposalMessage(proposal: ScopeProposal): string {
170
181
  /**
171
182
  * The message of the decision.
172
183
  *
173
- * The decision names what was decided, what it means, and whether the work
174
- * starts or the session stays stopped.
184
+ * The decision names the proposal, what was decided, the scope that is
185
+ * really authorized, and whether the work starts or the session stays
186
+ * stopped.
175
187
  */
176
188
  export function scopeDecisionMessage(
177
189
  proposal: ScopeProposal,
178
190
  decision: ScopeDecision,
179
191
  action: "starting the selected work" | "remaining stopped",
192
+ authorizedIds?: readonly string[],
180
193
  ): string {
181
- const who = decision.source === "jev" ? "Jev decision after 5 minutes" : "Your decision";
194
+ const who = decision.source === "jev" ? "Jev decision after the window" : "Your decision";
195
+ const ids = authorizedIds ?? decision.selectedIds;
182
196
  return [
183
197
  `${who}: ${decision.outcome}.`,
184
- `Scope: ${decision.selectedIds.length > 0 ? decision.selectedIds.join(", ") : "none"}.`,
198
+ `Proposal: ${proposal.id}.`,
199
+ `Scope: ${ids.length > 0 ? ids.join(", ") : "none"}.`,
185
200
  `Reason: ${decision.reasoning}`,
186
201
  `Action: ${action}.`,
202
+ `Proposed: ${proposal.scope}`,
187
203
  ].join("\n");
188
204
  }
189
205
 
@@ -219,7 +235,10 @@ export interface ScopeExpansionDeps {
219
235
  * The result says whether at least one route accepted the message. An
220
236
  * acknowledgement is a submission, not a reading.
221
237
  */
222
- deliver(text: string, meta: { id: string; kind: "proposal" | "decision" }): Promise<{
238
+ deliver(
239
+ text: string,
240
+ meta: { id: string; kind: "proposal" | "decision"; questionId?: string },
241
+ ): Promise<{
223
242
  delivered: boolean;
224
243
  failures: string[];
225
244
  }>;
@@ -227,6 +246,14 @@ export interface ScopeExpansionDeps {
227
246
  judge(proposal: ScopeProposal): Promise<ScopeDecision>;
228
247
  /** Start the authorized work. */
229
248
  dispatch(proposal: ScopeProposal, ids: string[]): void;
249
+ /**
250
+ * The latest human decision of a proposal.
251
+ *
252
+ * It is read after the delivery of the decision and before the
253
+ * dispatch: a rejection that arrived while the decision was on its way
254
+ * still prevents the work.
255
+ */
256
+ latestHumanDecision?(proposal: ScopeProposal): "approve" | "reject" | undefined;
230
257
  /** The log of the controller. */
231
258
  log?(message: string): void;
232
259
  }
@@ -244,7 +271,16 @@ export class ScopeExpansionController {
244
271
  private generationCounter = 0;
245
272
  private answered = new Set<string>();
246
273
  private evaluated = new Set<string>();
247
- private closedGeneration: number | null = null;
274
+ /** The proposals whose decision is being applied right now. */
275
+ private applying = new Set<string>();
276
+ /**
277
+ * The generations that are closed.
278
+ *
279
+ * Every closed generation is remembered: the approval of one group is
280
+ * not the approval of the next one, and that holds for every group
281
+ * that finished, not only for the last one.
282
+ */
283
+ private closedGenerations = new Set<number>();
248
284
 
249
285
  constructor(deps: ScopeExpansionDeps) {
250
286
  this.deps = deps;
@@ -275,7 +311,7 @@ export class ScopeExpansionController {
275
311
  * approval of one group is not the approval of the next one.
276
312
  */
277
313
  register(proposal: ScopeProposal): boolean {
278
- if (this.closedGeneration === proposal.generation) {
314
+ if (this.closedGenerations.has(proposal.generation)) {
279
315
  this.deps.log?.(
280
316
  `The scope proposal ${proposal.id} is refused: the generation ${proposal.generation} is closed.`,
281
317
  );
@@ -349,6 +385,20 @@ export class ScopeExpansionController {
349
385
  failed: true,
350
386
  };
351
387
  }
388
+ // A failed evaluation is not a decision: it authorizes nothing, and
389
+ // it is a wait that ends the attempts for this proposal.
390
+ if (decision.failed === true) {
391
+ decision = {
392
+ outcome: "wait",
393
+ selectedIds: [],
394
+ reasoning:
395
+ decision.reasoning.length > 0
396
+ ? decision.reasoning
397
+ : "the evaluation failed",
398
+ source: "jev",
399
+ failed: true,
400
+ };
401
+ }
352
402
  // The human answered while the evaluation was running: the answer
353
403
  // wins.
354
404
  if (this.answered.has(id)) {
@@ -369,6 +419,32 @@ export class ScopeExpansionController {
369
419
  * that the human was informed and start anyway.
370
420
  */
371
421
  async applyDecision(id: string): Promise<{ started: boolean; reason?: string }> {
422
+ // The decision of a proposal is applied once. The answer of the
423
+ // human that arrives while the decision is on its way starts a
424
+ // second application of the same proposal, and the group must not
425
+ // start twice.
426
+ if (this.applying.has(id)) {
427
+ return {
428
+ started: false,
429
+ reason: "the decision of the proposal is being applied",
430
+ };
431
+ }
432
+ this.applying.add(id);
433
+ try {
434
+ return await this.applyDecisionOnce(id);
435
+ } finally {
436
+ this.applying.delete(id);
437
+ }
438
+ }
439
+
440
+ /**
441
+ * Apply the decision of a proposal.
442
+ *
443
+ * The decision is applied once: the caller holds the lock.
444
+ */
445
+ private async applyDecisionOnce(
446
+ id: string,
447
+ ): Promise<{ started: boolean; reason?: string }> {
372
448
  const proposal = this.proposals.get(id);
373
449
  if (!proposal) return { started: false, reason: "unknown proposal" };
374
450
  const decision = proposal.decision;
@@ -376,13 +452,43 @@ export class ScopeExpansionController {
376
452
  if (proposal.state === "dispatched" || proposal.state === "finished") {
377
453
  return { started: false, reason: "the proposal was already executed" };
378
454
  }
379
- const scope = authorizedScope(proposal, decision);
455
+ // The human decides. The answer is read before the delivery, so the
456
+ // delivered message and the executed work agree.
457
+ let effective = decision;
458
+ const recorded = this.humanDecision(proposal);
459
+ const latestBefore = this.deps.latestHumanDecision?.(proposal);
460
+ if (recorded) {
461
+ // The first answer of the human wins: the answer that the
462
+ // controller recorded is stronger than a store that reports the
463
+ // last click.
464
+ effective = recorded;
465
+ } else if (latestBefore === "reject") {
466
+ effective = {
467
+ outcome: "reject",
468
+ selectedIds: [],
469
+ reasoning: "the human rejected the proposal",
470
+ source: "human",
471
+ };
472
+ proposal.decision = effective;
473
+ } else if (latestBefore === "approve" && decision.source !== "human") {
474
+ effective = {
475
+ ...decision,
476
+ outcome: "approve",
477
+ selectedIds: [...proposal.proposedIds],
478
+ source: "human",
479
+ };
480
+ proposal.decision = effective;
481
+ }
482
+ const scope = authorizedScope(proposal, effective);
380
483
  const action: "starting the selected work" | "remaining stopped" =
381
484
  scope.ids.length > 0 ? "starting the selected work" : "remaining stopped";
382
- const delivery = await this.deps.deliver(scopeDecisionMessage(proposal, decision, action), {
383
- id: proposal.id,
384
- kind: "decision",
385
- });
485
+ const delivery = await this.deps.deliver(
486
+ scopeDecisionMessage(proposal, effective, action, scope.ids),
487
+ {
488
+ id: proposal.id,
489
+ kind: "decision",
490
+ },
491
+ );
386
492
  if (!delivery.delivered) {
387
493
  proposal.state = "stopped";
388
494
  proposal.stopReason = `the decision was not delivered: ${delivery.failures.join(", ")}`;
@@ -390,16 +496,99 @@ export class ScopeExpansionController {
390
496
  return { started: false, reason: proposal.stopReason };
391
497
  }
392
498
  proposal.state = "decided";
393
- if (scope.ids.length === 0) {
499
+ // The answer is read again after the delivery: a rejection that
500
+ // arrived while the decision was on its way still prevents the work,
501
+ // and the answer of the controller is stronger than the store.
502
+ const after = this.humanDecision(proposal);
503
+ const latestAfter = this.deps.latestHumanDecision?.(proposal);
504
+ let finalDecision = effective;
505
+ if (after) {
506
+ finalDecision = after;
507
+ } else if (latestAfter === "reject") {
508
+ finalDecision = {
509
+ outcome: "reject",
510
+ selectedIds: [],
511
+ reasoning: "the human rejected the proposal",
512
+ source: "human",
513
+ };
514
+ } else if (latestAfter === "approve" && effective.source !== "human") {
515
+ finalDecision = {
516
+ ...effective,
517
+ outcome: "approve",
518
+ selectedIds: [...proposal.proposedIds],
519
+ source: "human",
520
+ };
521
+ }
522
+ if (finalDecision.outcome === "reject" && effective.outcome !== "reject") {
394
523
  proposal.state = "stopped";
395
- proposal.stopReason = scope.reason ?? "the decision authorizes no work";
524
+ proposal.stopReason =
525
+ "the human rejected the proposal after the decision was delivered";
526
+ // The delivered message said something else: the human has to
527
+ // know that the work stays stopped. A correction that reaches
528
+ // nobody does not change the outcome: the work stays stopped
529
+ // either way.
530
+ const correction = await this.deps.deliver(
531
+ scopeDecisionMessage(proposal, finalDecision, "remaining stopped", []),
532
+ { id: proposal.id, kind: "decision" },
533
+ );
534
+ if (!correction.delivered) {
535
+ proposal.stopReason = `${proposal.stopReason}; the correction was not delivered: ${correction.failures.join(", ")}`;
536
+ }
537
+ this.deps.log?.(
538
+ `The scope proposal ${proposal.id} stays unexecuted: ${proposal.stopReason}`,
539
+ );
396
540
  return { started: false, reason: proposal.stopReason };
397
541
  }
398
- this.deps.dispatch(proposal, scope.ids);
542
+ let finalScope = authorizedScope(proposal, finalDecision);
543
+ if (finalDecision !== effective) {
544
+ proposal.decision = finalDecision;
545
+ // The delivered message said something else: the human has to
546
+ // know what starts. A correction that reaches nobody leaves
547
+ // the work stopped.
548
+ const correction = await this.deps.deliver(
549
+ scopeDecisionMessage(
550
+ proposal,
551
+ finalDecision,
552
+ finalScope.ids.length > 0 ? "starting the selected work" : "remaining stopped",
553
+ finalScope.ids,
554
+ ),
555
+ { id: proposal.id, kind: "decision" },
556
+ );
557
+ if (!correction.delivered) {
558
+ proposal.state = "stopped";
559
+ proposal.stopReason = `the correction was not delivered: ${correction.failures.join(", ")}`;
560
+ this.deps.log?.(
561
+ `The scope proposal ${proposal.id} stays unexecuted: ${proposal.stopReason}`,
562
+ );
563
+ return { started: false, reason: proposal.stopReason };
564
+ }
565
+ }
566
+ if (finalScope.ids.length === 0) {
567
+ proposal.state = "stopped";
568
+ proposal.stopReason =
569
+ finalDecision.outcome === "reject"
570
+ ? finalDecision.reasoning
571
+ : (finalScope.reason ?? "the decision authorizes no work");
572
+ return { started: false, reason: proposal.stopReason };
573
+ }
574
+ this.deps.dispatch(proposal, finalScope.ids);
399
575
  proposal.state = "dispatched";
400
576
  return { started: true };
401
577
  }
402
578
 
579
+ /**
580
+ * The answer of the human, when the controller recorded one.
581
+ *
582
+ * The first answer wins: a second answer - a stale button of a
583
+ * notification that was already delivered - does not replace it.
584
+ */
585
+ private humanDecision(proposal: ScopeProposal): ScopeDecision | undefined {
586
+ if (!this.answered.has(proposal.id)) return undefined;
587
+ const decision = proposal.decision;
588
+ if (!decision || decision.source !== "human") return undefined;
589
+ return decision;
590
+ }
591
+
403
592
  /**
404
593
  * The authorized group finished.
405
594
  *
@@ -410,7 +599,7 @@ export class ScopeExpansionController {
410
599
  const proposal = this.proposals.get(id);
411
600
  if (!proposal) return;
412
601
  proposal.state = "finished";
413
- this.closedGeneration = proposal.generation;
602
+ this.closedGenerations.add(proposal.generation);
414
603
  this.deps.log?.(
415
604
  `The scope proposal ${proposal.id} is finished: the session stops again.`,
416
605
  );