pi-self-complaint 0.1.0 → 0.1.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.
package/README.md CHANGED
@@ -34,7 +34,7 @@ The setting is per project directory. It is stored in `~/.pi/agent/self-complain
34
34
  - 2026-11-16 20:40 · session 01a1e0d4 · #my-project · pnpm gate keeps running after the rules step fails
35
35
  ```
36
36
 
37
- Aborted and errored runs are not asked. Replies that are neither `NO` nor a `COMPLAINT:` line are ignored and logged nothing. The question is asked once per run, so the extension cannot loop.
37
+ Aborted and errored runs are not asked. Runs that only pause while background work is still going are not asked either: with `pi-background-tasks` a task that will wake the agent (`notifyOnCompletion` and `triggerOnCompletion`), and with `pi-subagents` an async subagent run, count as pending until their completion arrives. The question then comes in the run that the result starts. Without those extensions nothing is tracked and each run is asked as before. Replies that are neither `NO` nor a `COMPLAINT:` line are ignored and logged nothing. The question is asked once per run, so the extension cannot loop.
38
38
 
39
39
  ## Privacy
40
40
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-self-complaint",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "After each Pi run, ask the agent what annoyed it and log concrete complaints to the project's docs",
5
5
  "keywords": ["pi-package"],
6
6
  "license": "MIT",
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Event names published by the background-work extensions. They are plain strings on the shared `pi.events` bus,
3
+ * so this package does not depend on them. Without those extensions nothing is ever published and nothing is tracked.
4
+ */
5
+ export const SUBAGENT_ASYNC_STARTED = "subagent:async-started";
6
+ export const SUBAGENT_ASYNC_COMPLETE = "subagent:async-complete";
7
+ export const BACKGROUND_TERMINAL = "pi-background-tasks:terminal:v1";
8
+
9
+ function idOf(value: unknown): string | undefined {
10
+ if (!value || typeof value !== "object") return undefined;
11
+ const id = (value as { id?: unknown }).id;
12
+ return typeof id === "string" && id !== "" ? id : undefined;
13
+ }
14
+
15
+ /** Id of a background subagent run, from `subagent:async-started` or `subagent:async-complete`. */
16
+ export function subagentRunId(payload: unknown): string | undefined {
17
+ return idOf(payload);
18
+ }
19
+
20
+ /** Id of a finished background shell or agent task, from `pi-background-tasks:terminal:v1`. */
21
+ export function terminalTaskId(payload: unknown): string | undefined {
22
+ if (!payload || typeof payload !== "object") return undefined;
23
+ return idOf((payload as { task?: unknown }).task);
24
+ }
25
+
26
+ /**
27
+ * Id of a background task that was just started by a tool, from the tool result details (`details.task`).
28
+ * Only tasks that wake the parent when they finish count: without notify and trigger the parent never
29
+ * gets a new run for them, so waiting for them would silence complaints for good.
30
+ */
31
+ export function startedBackgroundTaskId(details: unknown): string | undefined {
32
+ if (!details || typeof details !== "object") return undefined;
33
+ const task = (details as { task?: unknown }).task;
34
+ if (!task || typeof task !== "object") return undefined;
35
+ const { status, notifyOnCompletion, triggerOnCompletion } = task as Record<string, unknown>;
36
+ if (status !== "running" || notifyOnCompletion !== true || triggerOnCompletion !== true) return undefined;
37
+ return idOf(task);
38
+ }
package/src/index.ts CHANGED
@@ -9,18 +9,57 @@ import {
9
9
  lastAssistantText,
10
10
  parseAnswer,
11
11
  } from "./complaint.ts";
12
+ import {
13
+ BACKGROUND_TERMINAL,
14
+ SUBAGENT_ASYNC_COMPLETE,
15
+ SUBAGENT_ASYNC_STARTED,
16
+ startedBackgroundTaskId,
17
+ subagentRunId,
18
+ terminalTaskId,
19
+ } from "./background.ts";
12
20
  import { defaultStatePath, isEnabled, writeEnabled } from "./state.ts";
13
21
 
14
22
  export default function selfComplaint(pi: ExtensionAPI, statePath = defaultStatePath()): void {
15
23
  // True while the one extra "did anything annoy you?" request of the current run is pending.
16
24
  let asked = false;
17
25
 
26
+ // Background runs that will start a new run when they finish. The parent settles while they are still working,
27
+ // so asking then would ask before the work is done. Ids are removed when the matching completion arrives.
28
+ const pendingBackground = new Set<string>();
29
+ const busUnsubscribes = [
30
+ pi.events?.on(SUBAGENT_ASYNC_STARTED, (payload) => {
31
+ const id = subagentRunId(payload);
32
+ if (id) pendingBackground.add(id);
33
+ }),
34
+ pi.events?.on(SUBAGENT_ASYNC_COMPLETE, (payload) => {
35
+ const id = subagentRunId(payload);
36
+ if (id) pendingBackground.delete(id);
37
+ }),
38
+ pi.events?.on(BACKGROUND_TERMINAL, (payload) => {
39
+ const id = terminalTaskId(payload);
40
+ if (id) pendingBackground.delete(id);
41
+ }),
42
+ ];
43
+
18
44
  pi.on("session_start", () => {
19
45
  asked = false;
20
46
  });
21
47
  pi.on("session_tree", () => {
22
48
  asked = false;
23
49
  });
50
+ pi.on("session_shutdown", () => {
51
+ for (const unsubscribe of busUnsubscribes) unsubscribe?.();
52
+ pendingBackground.clear();
53
+ });
54
+
55
+ // Tools that start a background shell or agent task return its snapshot in details.task. Without the bus there is
56
+ // no completion event to remove the id again, so nothing is tracked.
57
+ if (pi.events) {
58
+ pi.on("tool_result", (event) => {
59
+ const id = startedBackgroundTaskId(event.details);
60
+ if (id) pendingBackground.add(id);
61
+ });
62
+ }
24
63
 
25
64
  pi.registerCommand("complaints", {
26
65
  description: "Turn agent self-complaints on, off, or show status for this project (on | off | status)",
@@ -55,6 +94,10 @@ export default function selfComplaint(pi: ExtensionAPI, statePath = defaultState
55
94
  }
56
95
  if (!enabled) return;
57
96
 
97
+ // Background work is still running, so this settle is only a pause. Its completion starts a new run, and
98
+ // the question is asked there, once the work is actually done.
99
+ if (pendingBackground.size > 0) return;
100
+
58
101
  // First boundary of the run: ask once and request exactly one more model response.
59
102
  asked = true;
60
103
  return {