portable-agent-layer 0.113.0 → 0.114.0-canary.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.
@@ -82,6 +82,10 @@
82
82
  "type": "boolean",
83
83
  "description": "Opt-in, off by default until 1.0. Send a turn back when it committed, made a branch, opened a PR or published an artifact without recording it in the work tree (pal cli work). Needs workTree."
84
84
  },
85
+ "workTreeDecisionGate": {
86
+ "type": "boolean",
87
+ "description": "Opt-in, off by default until 1.0. Send a turn back when a small-model check finds it settled a decision that no pal cli work decide recorded. One small-model call at the end of every turn. Needs workTree."
88
+ },
85
89
  "dueReminders": {
86
90
  "type": "boolean",
87
91
  "description": "Opt-in, off by default until 1.0. Due maintenance reminders (learning analysis, relationship reflect) with every prompt until a reply names their command; then quiet until the next day."
@@ -29,6 +29,7 @@
29
29
  "repoState": true,
30
30
  "workTree": false,
31
31
  "workTreeGate": false,
32
+ "workTreeDecisionGate": false,
32
33
  "dueReminders": false,
33
34
  "interactionAwareness": true,
34
35
  "reactionSampling": true,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "portable-agent-layer",
3
- "version": "0.113.0",
3
+ "version": "0.114.0-canary.1",
4
4
  "description": "PAL — Portable Agent Layer: persistent personal context for AI coding assistants",
5
5
  "type": "module",
6
6
  "bin": {
@@ -74,7 +74,9 @@
74
74
  "eval:correction-kind": "bun eval/correction-kind/build-prompt.ts && bun eval/run.ts correction-kind",
75
75
  "eval:correction-kind:test": "bun eval/correction-kind/test-set.ts",
76
76
  "eval:rule-drafter": "bun eval/rule-drafter/build-prompt.ts && bun eval/run.ts rule-drafter",
77
- "eval:rule-drafter:test": "bun eval/rule-drafter/test-set.ts"
77
+ "eval:rule-drafter:test": "bun eval/rule-drafter/test-set.ts",
78
+ "eval:decision": "bun eval/decision/score.ts validation",
79
+ "eval:decision:test": "bun eval/decision/score.ts test"
78
80
  },
79
81
  "lint-staged": {
80
82
  "*.ts": [
@@ -64,7 +64,7 @@ try {
64
64
  logError("StopOrchestrator:adaptation-rules", err);
65
65
  }
66
66
  try {
67
- const sendBack = stopWorkTree(payload);
67
+ const sendBack = await stopWorkTree(payload);
68
68
  if (sendBack) {
69
69
  process.stdout.write(stopBlockResponse(sendBack));
70
70
  process.exit(0);
@@ -137,6 +137,14 @@ function isUserPrompt(entry: TranscriptLine): boolean {
137
137
  return text !== null && !isSystemText(text) && !isCodexInjectedContext(text);
138
138
  }
139
139
 
140
+ export function promptThisTurn(lines: string[]): string | null {
141
+ const prompts = lines
142
+ .map(parsed)
143
+ .filter((e): e is TranscriptLine => e !== null && isUserPrompt(e));
144
+ const last = prompts.at(-1);
145
+ return last ? promptText(last) : null;
146
+ }
147
+
140
148
  const ran = (input: unknown) => JSON.stringify(input ?? {});
141
149
 
142
150
  function callsIn(entry: TranscriptLine, tools: ReadonlySet<string>): string[] {
@@ -0,0 +1,70 @@
1
+ /**
2
+ * Whether a turn settled a decision. Code can see that a turn committed or
3
+ * opened a PR, but a decision is only words, so the work-tree gate asks the
4
+ * small model and sends the turn back when nothing was recorded.
5
+ */
6
+
7
+ export interface DecisionLabel {
8
+ decision: boolean;
9
+ what: string;
10
+ }
11
+
12
+ const DECISION_SYSTEM = `You read one turn of a working session between a user and an AI assistant: the user's message and the end of the assistant's reply. Say whether the turn settled a decision.
13
+
14
+ A decision is a choice between alternatives about the work that holds beyond this turn: a policy, a design, a direction, a scope (what is in, out, parked or dropped), or a standing way of working. Examples: "make them opt in for now", "we only track new posts, not past ones", "we don't need the predictor", "PRs go to canary from now on".
15
+
16
+ A decision is often a few words that answer something open earlier: picking one of the options the assistant offered, keeping something where it is, saying something is not needed or should wait, changing how a thing behaves. Read the reply to see what the user's words settled; a short message can still be a decision.
17
+
18
+ These are not decisions:
19
+ - a go-ahead or approval of a plan or of the assistant's proposals as they stand ("go", "yes pls", "build all of them", "ok to both, build it"), unless it also picks between alternatives or changes the plan;
20
+ - where the work goes (which branch or PR) and other bookkeeping the assistant handles;
21
+ - a plain request for work ("fix CI", "push and open a PR", "plan it", "list our tasks"), including which step comes first;
22
+ - a question, even one about what to decide or what was decided before;
23
+ - a fact, a status report, or an idea floated for discussion;
24
+ - a recommendation or proposal from the assistant that the user has not accepted;
25
+ - small implementation choices the assistant makes while working.
26
+
27
+ Most turns settle no decision. When unsure, answer false.
28
+
29
+ Set what to the decision in under fifteen words, or to an empty string when there is none.`;
30
+
31
+ const DECISION_SCHEMA = {
32
+ type: "object",
33
+ properties: {
34
+ decision: { type: "boolean" },
35
+ what: { type: "string" },
36
+ },
37
+ required: ["decision", "what"],
38
+ additionalProperties: false,
39
+ } as const;
40
+
41
+ const MESSAGE_MAX = 1500;
42
+ const REPLY_END_MAX = 1500;
43
+
44
+ export function decisionUser(message: string, reply: string) {
45
+ return `The user's message:\n<message>\n${message.slice(0, MESSAGE_MAX)}\n</message>\n\nThe end of the assistant's reply:\n<reply_end>\n${reply.slice(-REPLY_END_MAX)}\n</reply_end>`;
46
+ }
47
+
48
+ export function decisionRequest(message: string, reply: string, sessionId?: string) {
49
+ return {
50
+ system: DECISION_SYSTEM,
51
+ user: decisionUser(message, reply),
52
+ maxTokens: 100,
53
+ timeout: 60_000,
54
+ jsonSchema: DECISION_SCHEMA,
55
+ caller: "decision-label",
56
+ sessionId,
57
+ };
58
+ }
59
+
60
+ export function parseDecision(output: string | null | undefined): DecisionLabel | null {
61
+ if (!output) return null;
62
+ try {
63
+ const parsed = JSON.parse(output) as Record<string, unknown>;
64
+ if (typeof parsed.decision !== "boolean") return null;
65
+ const what = typeof parsed.what === "string" ? parsed.what.trim() : "";
66
+ return { decision: parsed.decision, what: parsed.decision ? what : "" };
67
+ } catch {
68
+ return null;
69
+ }
70
+ }
@@ -76,7 +76,11 @@ export function isFixedModelRoute(route: string): route is FixedModelRoute {
76
76
  * caller, whose eval is iterated on 5.5 until it passes. The rest stay on
77
77
  * HAIKU_MODEL until theirs is evaluated.
78
78
  */
79
- const ANTHROPIC_ON_HAIKU_5_5 = new Set(["rule-drafter", "correction-kind"]);
79
+ const ANTHROPIC_ON_HAIKU_5_5 = new Set([
80
+ "rule-drafter",
81
+ "correction-kind",
82
+ "decision-label",
83
+ ]);
80
84
 
81
85
  function evaluatedModel(
82
86
  models: RouteModels,
@@ -4,8 +4,11 @@
4
4
  * nothing in the tree is sent back once to record it.
5
5
  */
6
6
 
7
- import { callsThisTurn, commandsThisTurn } from "./claim-check";
7
+ import { callsThisTurn, commandsThisTurn, promptThisTurn } from "./claim-check";
8
8
  import { alreadySentBack, transcriptLines } from "./claim-log";
9
+ import { type DecisionLabel, decisionRequest, parseDecision } from "./decision-label";
10
+ import { hookFinalReply } from "./hook-turn";
11
+ import { inference } from "./inference";
9
12
  import { sessionDir } from "./session-dir";
10
13
  import { isOptedIn } from "./settings";
11
14
  import type { StopTurnPayload } from "./stop";
@@ -14,6 +17,7 @@ import { attachGitProgress } from "./work-tree-git";
14
17
  import { renderPromptLine } from "./work-tree-render";
15
18
 
16
19
  const RECORDS_WORK_RE = /\bpal\s+cli\s+work\b/;
20
+ const RECORDS_DECISION_RE = /\bpal\s+cli\s+work\s+decide\b/;
17
21
 
18
22
  const PRODUCERS: { what: string; re: RegExp }[] = [
19
23
  { what: "committed", re: /\bgit\s+commit\b/ },
@@ -34,8 +38,12 @@ function producedThisTurn(commands: string[], lines: string[]): string[] {
34
38
  return produced;
35
39
  }
36
40
 
41
+ function whereWorkStands(scope: string): string {
42
+ return renderPromptLine(readTree(scope), false) ?? "Work: nothing recorded yet";
43
+ }
44
+
37
45
  function sendBackReason(produced: string[], scope: string): string {
38
- const where = renderPromptLine(readTree(scope), false) ?? "Work: nothing recorded yet";
46
+ const where = whereWorkStands(scope);
39
47
  return [
40
48
  `This turn ${produced.join(", ")} but recorded nothing in the work tree (${where}).`,
41
49
  'Record where the work stands with `pal cli work stopped "…"`, plus `decide` or `attach` where they fit;',
@@ -56,11 +64,47 @@ export function workTreeSendBack(
56
64
  return produced.length > 0 ? sendBackReason(produced, scopeOf(cwd)) : null;
57
65
  }
58
66
 
67
+ type LabelDecision = (message: string, reply: string) => Promise<DecisionLabel | null>;
68
+
69
+ async function labelWithInference(message: string, reply: string) {
70
+ const result = await inference(decisionRequest(message, reply));
71
+ return result.success ? parseDecision(result.output) : null;
72
+ }
73
+
74
+ function decisionReason(what: string, scope: string): string {
75
+ return [
76
+ `This turn settled a decision (${what}) but recorded none in the work tree (${whereWorkStands(scope)}).`,
77
+ 'Record it with `pal cli work decide "…"`; if no node fits, open one first with `pal cli work open "…"`.',
78
+ ].join(" ");
79
+ }
80
+
81
+ export async function decisionSendBack(
82
+ payload: StopTurnPayload,
83
+ lines: string[],
84
+ cwd: string,
85
+ label: LabelDecision = labelWithInference
86
+ ): Promise<string | null> {
87
+ if (!isOptedIn("workTree") || !isOptedIn("workTreeDecisionGate")) return null;
88
+ if (alreadySentBack(payload)) return null;
89
+ const commands = commandsThisTurn(lines);
90
+ if (commands === null || commands.some((c) => RECORDS_DECISION_RE.test(c))) return null;
91
+ const message = promptThisTurn(lines);
92
+ const reply = hookFinalReply(payload);
93
+ if (!message || !reply) return null;
94
+ const decided = await label(message, reply);
95
+ return decided?.decision ? decisionReason(decided.what, scopeOf(cwd)) : null;
96
+ }
97
+
59
98
  /** The reason to send the turn back with, or null after attaching what git moved. */
60
- export function stopWorkTree(payload: StopTurnPayload | null): string | null {
99
+ export async function stopWorkTree(
100
+ payload: StopTurnPayload | null
101
+ ): Promise<string | null> {
61
102
  if (!payload || !isOptedIn("workTree")) return null;
62
103
  const cwd = sessionDir();
63
- const sendBack = workTreeSendBack(payload, transcriptLines(payload), cwd);
104
+ const lines = transcriptLines(payload);
105
+ const sendBack =
106
+ workTreeSendBack(payload, lines, cwd) ??
107
+ (await decisionSendBack(payload, lines, cwd));
64
108
  if (sendBack) return sendBack;
65
109
  attachGitProgress(cwd);
66
110
  return null;