pal-code 0.0.20 → 0.0.21

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.
@@ -117,6 +117,10 @@ function askUser(
117
117
  state.permissionResolver = (allow: boolean) => {
118
118
  state.permissionResolver = null;
119
119
 
120
+ // Back to work the moment the prompt is answered: the turn continues, so
121
+ // leaving the terminal showing "needs you" would be a lie.
122
+ state.statusReporter.report('working');
123
+
120
124
  onResult(allow);
121
125
 
122
126
  // If there are queued permissions, show the next one immediately
@@ -133,6 +137,10 @@ function askUser(
133
137
  state.pendingPermission = {toolName, target, summary};
134
138
  state.overlayMode = 'permission';
135
139
 
140
+ // Herdr's most urgent agent state: a pane suffix "needs you" and a badge in
141
+ // the agents list. This is the whole reason OSC 7501 carries `blocked`.
142
+ state.statusReporter.report('blocked', {kind: 'permission', msg: summary});
143
+
136
144
  // Show the centered permission modal (removes editor, adds prompt to messages)
137
145
  state.showPermissionModal();
138
146
 
@@ -161,12 +169,17 @@ function showNextQueuedPermission(): void {
161
169
  } else {
162
170
  state.overlayMode = 'none';
163
171
  state.pendingPermission = null;
172
+ // Queue drained — the turn resumes.
173
+ state.statusReporter.report('working');
164
174
  }
165
175
  };
166
176
 
167
177
  // Swap the pending permission atomically — the mounted PermissionPrompt
168
178
  // component reads this on each render, so it updates in-place.
169
179
  state.pendingPermission = {toolName: next.toolName, target: next.target, summary: next.summary};
180
+ // Still blocked, just on a different request: the `msg` changes so the
181
+ // terminal shows what is actually being asked.
182
+ state.statusReporter.report('blocked', {kind: 'permission', msg: next.summary});
170
183
  state.refreshStatusLine();
171
184
  }
172
185
 
@@ -0,0 +1,125 @@
1
+ /**
2
+ * Emit pal's lifecycle as OSC 7501 program-status reports.
3
+ *
4
+ * `osc-status.ts` owns the bytes; this owns *when* to send them. The split
5
+ * keeps the grammar testable without a terminal and leaves exactly one place
6
+ * that writes to stdout.
7
+ *
8
+ * Design notes:
9
+ *
10
+ * - **No detection round-trip.** Feature detection (`OSC 7501;?`) is optional
11
+ * by spec, and a terminal that does not know the sequence ignores it. pal
12
+ * cannot reliably read the reply anyway (it is rendering a TUI), so it sends
13
+ * reports unconditionally — the spec explicitly allows this.
14
+ * - **Dedupe.** The TUI re-renders constantly and several events map to
15
+ * `working`; sending an identical report per event would be pure noise. The
16
+ * last record is remembered and identical consecutive reports are dropped.
17
+ * - **Never throw.** A status report is a courtesy. It must not fail a turn,
18
+ * end a session, or smear an error across the TUI frame.
19
+ * - **Only a TTY.** When stdout is a pipe (CI, `pal ... | head`), the escape
20
+ * sequences are not a status channel, they are corruption. Skip them.
21
+ */
22
+
23
+ import {
24
+ buildOscStatus,
25
+ buildOscStatusClear,
26
+ PAL_APP_NAME,
27
+ type ProgramStatus,
28
+ type ProgramStatusKind,
29
+ type ProgramStatusState,
30
+ } from './osc-status.js';
31
+
32
+ /** What the reporter was constructed with. */
33
+ export interface StatusReporterOptions {
34
+ /** Where to write. Defaults to `process.stdout`. */
35
+ write?: (chunk: string) => void;
36
+ /** Whether to report at all. Defaults to "stdout is a TTY". */
37
+ enabled?: boolean;
38
+ /** `app` identity on the wire. Defaults to {@link PAL_APP_NAME}. */
39
+ app?: string;
40
+ }
41
+
42
+ /** The narrow surface the CLI/UI layer calls. */
43
+ export interface StatusReporter {
44
+ /** Report a state change. `undefined`/`true` mean "enabled". */
45
+ report(
46
+ state: ProgramStatusState,
47
+ detail?: {kind?: ProgramStatusKind; msg?: string; progress?: number},
48
+ ): void;
49
+ /** Clear pal's records — sent on exit so no stale badge is left behind. */
50
+ clear(): void;
51
+ /** The status last put on the wire, for the status line / `pal doctor`. */
52
+ current(): ProgramStatusState | null;
53
+ }
54
+
55
+ /** A reporter that does nothing. Used when status reporting is switched off. */
56
+ export const nullStatusReporter: StatusReporter = {
57
+ report() {},
58
+ clear() {},
59
+ current() {
60
+ return null;
61
+ },
62
+ };
63
+
64
+ /**
65
+ * Whether pal should emit status reports in this process.
66
+ *
67
+ * `PAL_NO_STATUS=1` opts out (and is what a test harness or a wrapper that
68
+ * wants a silent run sets). Otherwise stdout being a TTY is the whole test:
69
+ * a terminal is the only thing that can consume the sequence.
70
+ */
71
+ export function statusReportingEnabled(
72
+ env: Record<string, string | undefined> = process.env,
73
+ ): boolean {
74
+ if (env.PAL_NO_STATUS === '1') return false;
75
+ return process.stdout.isTTY === true;
76
+ }
77
+
78
+ /** Create the reporter the session uses for its whole lifetime. */
79
+ export function createStatusReporter(options: StatusReporterOptions = {}): StatusReporter {
80
+ const enabled = options.enabled ?? statusReportingEnabled();
81
+ if (!enabled) return nullStatusReporter;
82
+
83
+ const app = options.app ?? PAL_APP_NAME;
84
+ const write = options.write ?? ((chunk: string) => void process.stdout.write(chunk));
85
+
86
+ let last: string | null = null;
87
+ let current: ProgramStatusState | null = null;
88
+
89
+ return {
90
+ report(state, detail) {
91
+ const status: ProgramStatus = {
92
+ state,
93
+ app,
94
+ kind: detail?.kind,
95
+ msg: detail?.msg,
96
+ progress: detail?.progress,
97
+ };
98
+ const sequence = buildOscStatus(status);
99
+ current = state;
100
+ // The dedupe key is the whole sequence, not the state: a `blocked` that
101
+ // gains a different `kind` is a different thing to show.
102
+ if (sequence === last) return;
103
+ last = sequence;
104
+ try {
105
+ write(sequence);
106
+ } catch {
107
+ // A closed or wedged stdout must not take the session down with it.
108
+ }
109
+ },
110
+
111
+ clear() {
112
+ try {
113
+ write(buildOscStatusClear(app));
114
+ last = null;
115
+ current = null;
116
+ } catch {
117
+ // Same reasoning as report().
118
+ }
119
+ },
120
+
121
+ current() {
122
+ return current;
123
+ },
124
+ };
125
+ }