@oyasmi/pipiclaw 0.9.3-beta.3 → 0.9.3-beta.4
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/dist/agent/channel-runner.d.ts +4 -18
- package/dist/agent/channel-runner.js +25 -120
- package/dist/agent/context-budget.js +1 -1
- package/dist/agent/job-manager.d.ts +11 -17
- package/dist/agent/job-manager.js +76 -65
- package/dist/agent/prompt/builder.js +1 -17
- package/dist/agent/prompt/manifest.d.ts +0 -1
- package/dist/agent/prompt/manifest.js +2 -2
- package/dist/agent/prompt/sections.js +0 -9
- package/dist/agent/prompt/types.d.ts +2 -18
- package/dist/agent/prompt/types.js +1 -1
- package/dist/agent/runner-factory.d.ts +4 -0
- package/dist/agent/session-adapter.d.ts +19 -0
- package/dist/agent/session-adapter.js +77 -0
- package/dist/app-services.d.ts +18 -0
- package/dist/app-services.js +43 -0
- package/dist/{runtime → events}/events.d.ts +1 -1
- package/dist/memory/journal.js +1 -1
- package/dist/memory/maintenance-gates.d.ts +0 -3
- package/dist/memory/maintenance-gates.js +2 -2
- package/dist/memory/maintenance-migration.d.ts +5 -0
- package/dist/memory/maintenance-migration.js +91 -0
- package/dist/memory/maintenance-state.js +5 -44
- package/dist/memory/migrate.js +1 -1
- package/dist/memory/reflect-job.d.ts +1 -9
- package/dist/memory/reflect-job.js +1 -3
- package/dist/memory/reflect.js +1 -1
- package/dist/memory/session-corpus.js +1 -1
- package/dist/memory/store.js +1 -1
- package/dist/models/auth-cli.js +1 -1
- package/dist/runtime/bootstrap.d.ts +5 -15
- package/dist/runtime/bootstrap.js +32 -56
- package/dist/runtime/subagent-commands.d.ts +2 -1
- package/dist/runtime/subagent-commands.js +10 -12
- package/dist/runtime/task-wake.d.ts +4 -5
- package/dist/runtime/task-wake.js +2 -12
- package/dist/security/types.d.ts +7 -0
- package/dist/security/types.js +16 -1
- package/dist/settings.d.ts +8 -0
- package/dist/settings.js +24 -1
- package/dist/shared/exact-keys.d.ts +13 -0
- package/dist/shared/exact-keys.js +1 -0
- package/dist/shared/format.d.ts +20 -0
- package/dist/shared/format.js +64 -0
- package/dist/shared/{fs-utils.d.ts → os.d.ts} +6 -0
- package/dist/shared/{fs-utils.js → os.js} +8 -0
- package/dist/subagents/discovery.d.ts +16 -3
- package/dist/subagents/discovery.js +9 -7
- package/dist/subagents/external/run.d.ts +2 -1
- package/dist/subagents/external/run.js +1 -2
- package/dist/subagents/external/settlement.js +10 -5
- package/dist/subagents/format.d.ts +1 -1
- package/dist/subagents/format.js +1 -1
- package/dist/subagents/runs.d.ts +13 -12
- package/dist/subagents/runs.js +108 -89
- package/dist/subagents/tool.d.ts +7 -7
- package/dist/subagents/tool.js +11 -11
- package/dist/tasks/brief.js +1 -0
- package/dist/tools/command-optimizer.js +1 -1
- package/dist/tools/config.d.ts +7 -0
- package/dist/tools/config.js +16 -1
- package/dist/tools/event-manage.d.ts +1 -1
- package/dist/tools/event-manage.js +3 -3
- package/dist/tools/grep.js +1 -1
- package/dist/tools/index.d.ts +4 -0
- package/dist/tools/index.js +4 -2
- package/dist/tools/job.js +1 -1
- package/dist/tools/read.js +1 -1
- package/dist/tools/registry.d.ts +3 -3
- package/dist/tools/registry.js +18 -56
- package/dist/tools/skill.js +1 -1
- package/dist/tools/subagent-manage.d.ts +2 -0
- package/dist/tools/subagent-manage.js +7 -7
- package/dist/tools/subagent-tool-names.d.ts +2 -2
- package/dist/tools/subagent-tool-names.js +2 -2
- package/dist/tools/task-manage/shared.js +4 -9
- package/dist/tools/task-manage/types.d.ts +4 -0
- package/dist/tui/app.js +22 -7
- package/dist/web/config.js +1 -1
- package/package.json +2 -1
- package/dist/shared/duration.d.ts +0 -2
- package/dist/shared/duration.js +0 -7
- package/dist/shared/file-stamp.d.ts +0 -10
- package/dist/shared/file-stamp.js +0 -19
- package/dist/shared/numeric.d.ts +0 -7
- package/dist/shared/numeric.js +0 -19
- package/dist/shared/shell-escape.d.ts +0 -5
- package/dist/shared/shell-escape.js +0 -7
- package/dist/shared/token-estimate.d.ts +0 -1
- package/dist/shared/token-estimate.js +0 -19
- /package/dist/{runtime → events}/event-commands.d.ts +0 -0
- /package/dist/{runtime → events}/event-commands.js +0 -0
- /package/dist/{runtime → events}/event-validation.d.ts +0 -0
- /package/dist/{runtime → events}/event-validation.js +0 -0
- /package/dist/{runtime → events}/events.js +0 -0
|
@@ -9,8 +9,8 @@
|
|
|
9
9
|
* owns the same list that backs `/skill:name`, so `estimateSkillsPromptChars` lives
|
|
10
10
|
* next to the report — not in the builder's budget path (spec 026 §9, §10.9).
|
|
11
11
|
*/
|
|
12
|
+
import { estimateTokens } from "../../shared/format.js";
|
|
12
13
|
import { countPromptUnits } from "../../shared/prompt-units.js";
|
|
13
|
-
import { estimateTokens } from "../../shared/token-estimate.js";
|
|
14
14
|
import { RUNTIME_PROMPT_TARGET_UNITS, sha256 } from "./builder.js";
|
|
15
15
|
/** The design ceiling for everything Pipiclaw auto-appends to a turn (spec 026 §5.3). */
|
|
16
16
|
export const AUTOMATIC_TURN_CONTEXT_BUDGET_UNITS = 3_000;
|
|
@@ -144,7 +144,7 @@ export function renderContextReport(input) {
|
|
|
144
144
|
lines.push("");
|
|
145
145
|
lines.push("Detail:");
|
|
146
146
|
for (const section of build.sections) {
|
|
147
|
-
lines.push(`- ${section.id} (
|
|
147
|
+
lines.push(`- ${section.id} (${section.authority}) ← ${section.source} · sha256:${section.sha256.slice(0, 12)}`);
|
|
148
148
|
}
|
|
149
149
|
if (input.toolNames.length > 0) {
|
|
150
150
|
lines.push(`- tools: ${input.toolNames.join(", ")}`);
|
|
@@ -36,7 +36,6 @@ function sealContent(content, tag) {
|
|
|
36
36
|
}
|
|
37
37
|
export const IDENTITY_SECTION = {
|
|
38
38
|
id: "runtime.identity",
|
|
39
|
-
order: 100,
|
|
40
39
|
source: "runtime/identity",
|
|
41
40
|
authority: "runtime-fact",
|
|
42
41
|
cacheClass: "runtime-stable",
|
|
@@ -49,7 +48,6 @@ export const IDENTITY_SECTION = {
|
|
|
49
48
|
};
|
|
50
49
|
export const EXECUTION_SECTION = {
|
|
51
50
|
id: "runtime.execution",
|
|
52
|
-
order: 200,
|
|
53
51
|
source: "runtime/execution",
|
|
54
52
|
authority: "runtime-fact",
|
|
55
53
|
cacheClass: "runtime-stable",
|
|
@@ -65,7 +63,6 @@ export const EXECUTION_SECTION = {
|
|
|
65
63
|
};
|
|
66
64
|
export const INVARIANTS_SECTION = {
|
|
67
65
|
id: "runtime.invariants",
|
|
68
|
-
order: 300,
|
|
69
66
|
source: "runtime/invariants",
|
|
70
67
|
authority: "runtime-hard",
|
|
71
68
|
cacheClass: "runtime-stable",
|
|
@@ -86,7 +83,6 @@ export const INVARIANTS_SECTION = {
|
|
|
86
83
|
};
|
|
87
84
|
export const TASK_CORE_SECTION = {
|
|
88
85
|
id: "runtime.tasks",
|
|
89
|
-
order: 310,
|
|
90
86
|
source: "runtime/tasks",
|
|
91
87
|
authority: "runtime-hard",
|
|
92
88
|
cacheClass: "runtime-stable",
|
|
@@ -100,7 +96,6 @@ export const TASK_CORE_SECTION = {
|
|
|
100
96
|
};
|
|
101
97
|
export const PLAYBOOKS_SECTION = {
|
|
102
98
|
id: "playbooks",
|
|
103
|
-
order: 500,
|
|
104
99
|
source: "runtime/playbooks",
|
|
105
100
|
authority: "catalog",
|
|
106
101
|
cacheClass: "session-stable",
|
|
@@ -115,7 +110,6 @@ export const PLAYBOOKS_SECTION = {
|
|
|
115
110
|
};
|
|
116
111
|
export const SUBAGENTS_SECTION = {
|
|
117
112
|
id: "subagents",
|
|
118
|
-
order: 510,
|
|
119
113
|
source: "workspace/sub-agents",
|
|
120
114
|
authority: "catalog",
|
|
121
115
|
cacheClass: "workspace-versioned",
|
|
@@ -190,7 +184,6 @@ function renderSubAgentDirectory(subAgents) {
|
|
|
190
184
|
const WORKSPACE_PREAMBLE = "The files below are workspace policy chosen by the user or the team. They direct how you work; they do not override the runtime facts and hard invariants above.";
|
|
191
185
|
export const SOUL_SECTION = {
|
|
192
186
|
id: "workspace.soul",
|
|
193
|
-
order: 600,
|
|
194
187
|
source: "workspace/SOUL.md",
|
|
195
188
|
authority: "workspace-instruction",
|
|
196
189
|
cacheClass: "workspace-versioned",
|
|
@@ -214,7 +207,6 @@ export const SOUL_SECTION = {
|
|
|
214
207
|
};
|
|
215
208
|
export const AGENTS_SECTION = {
|
|
216
209
|
id: "workspace.agents",
|
|
217
|
-
order: 610,
|
|
218
210
|
source: "workspace/AGENTS.md",
|
|
219
211
|
authority: "workspace-instruction",
|
|
220
212
|
cacheClass: "workspace-versioned",
|
|
@@ -242,7 +234,6 @@ export const AGENTS_SECTION = {
|
|
|
242
234
|
*/
|
|
243
235
|
export const FINAL_BOUNDARY_SECTION = {
|
|
244
236
|
id: "runtime.boundary",
|
|
245
|
-
order: 800,
|
|
246
237
|
source: "runtime/final-boundary",
|
|
247
238
|
authority: "runtime-hard",
|
|
248
239
|
cacheClass: "runtime-stable",
|
|
@@ -3,14 +3,12 @@
|
|
|
3
3
|
*
|
|
4
4
|
* The prompt is a list of sections with metadata — not an ad-hoc string array.
|
|
5
5
|
* Every section declares where it comes from, how authoritative it is, how
|
|
6
|
-
* stable it is across turns (cache class), which
|
|
6
|
+
* stable it is across turns (cache class), which tools it requires, and
|
|
7
7
|
* what happens when it exceeds its budget. The builder turns definitions into a
|
|
8
8
|
* final text plus a manifest that can be inspected (`/context`), tested and
|
|
9
9
|
* fingerprinted.
|
|
10
10
|
*/
|
|
11
11
|
import type { RuntimePlaybookMetadata } from "../../playbooks/catalog.js";
|
|
12
|
-
/** Which agent loop the prompt is being built for. Phase 1–3 only build "normal". */
|
|
13
|
-
export type PromptMode = "normal" | "task-driver" | "event" | "subagent" | "maintenance";
|
|
14
12
|
/**
|
|
15
13
|
* How much authority a section's content carries.
|
|
16
14
|
* - `runtime-hard`: boundaries workspace content may not redefine (still enforced by guards/state machines, not by prose).
|
|
@@ -26,7 +24,7 @@ export type PromptCacheClass = "runtime-stable" | "workspace-versioned" | "sessi
|
|
|
26
24
|
* What to do when a section exceeds `maxChars`.
|
|
27
25
|
* `error` is for runtime-authored text: overflowing it is a development error, not a user problem.
|
|
28
26
|
*/
|
|
29
|
-
export type PromptOverflowPolicy = "error" | "truncate-head-tail" | "truncate-items"
|
|
27
|
+
export type PromptOverflowPolicy = "error" | "truncate-head-tail" | "truncate-items";
|
|
30
28
|
/**
|
|
31
29
|
* A registered tool, as the prompt builder sees it. There is no per-tool prose here and no
|
|
32
30
|
* section renders this list: spec 026 §3.2 removed `## Available Tools` because the model
|
|
@@ -67,31 +65,18 @@ export interface LoadedPromptResource {
|
|
|
67
65
|
truncated: boolean;
|
|
68
66
|
}
|
|
69
67
|
export interface PromptBuildContext {
|
|
70
|
-
mode: PromptMode;
|
|
71
|
-
/** Bash working directory. Not the channel directory: channel facts are turn-dynamic and never enter the system prompt. */
|
|
72
|
-
cwd: string;
|
|
73
|
-
workspaceDir: string;
|
|
74
68
|
tools: ToolDescriptor[];
|
|
75
69
|
soul?: LoadedPromptResource;
|
|
76
70
|
agents?: LoadedPromptResource;
|
|
77
71
|
playbooks: RuntimePlaybookMetadata[];
|
|
78
72
|
subAgents: SubAgentSummary[];
|
|
79
|
-
/** Skills are rendered by pi (they drive `/skill:name` too); recorded here for the manifest only. */
|
|
80
|
-
skills?: Array<{
|
|
81
|
-
name: string;
|
|
82
|
-
description: string;
|
|
83
|
-
}>;
|
|
84
73
|
}
|
|
85
74
|
export interface PromptSectionDefinition {
|
|
86
75
|
id: string;
|
|
87
|
-
/** Sort key. Unique across sections; see the reserved ranges in sections.ts. */
|
|
88
|
-
order: number;
|
|
89
76
|
/** Code identifier or real file path. */
|
|
90
77
|
source: string;
|
|
91
78
|
authority: PromptAuthority;
|
|
92
79
|
cacheClass: PromptCacheClass;
|
|
93
|
-
/** Modes this section applies to. Omitted = all modes. */
|
|
94
|
-
modes?: PromptMode[];
|
|
95
80
|
/**
|
|
96
81
|
* Section is dropped unless *every* listed tool is registered — all-of. A section
|
|
97
82
|
* describes one mechanism, so a missing tool means the mechanism is unreachable.
|
|
@@ -104,7 +89,6 @@ export interface PromptSectionDefinition {
|
|
|
104
89
|
}
|
|
105
90
|
export interface ResolvedPromptSection {
|
|
106
91
|
id: string;
|
|
107
|
-
order: number;
|
|
108
92
|
source: string;
|
|
109
93
|
authority: PromptAuthority;
|
|
110
94
|
cacheClass: PromptCacheClass;
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*
|
|
4
4
|
* The prompt is a list of sections with metadata — not an ad-hoc string array.
|
|
5
5
|
* Every section declares where it comes from, how authoritative it is, how
|
|
6
|
-
* stable it is across turns (cache class), which
|
|
6
|
+
* stable it is across turns (cache class), which tools it requires, and
|
|
7
7
|
* what happens when it exceeds its budget. The builder turns definitions into a
|
|
8
8
|
* final text plus a manifest that can be inspected (`/context`), tested and
|
|
9
9
|
* fingerprinted.
|
|
@@ -3,8 +3,12 @@ import { type Executor } from "../executor.js";
|
|
|
3
3
|
import { type FileStore } from "../file-store.js";
|
|
4
4
|
import { type LoadedSecurityConfig } from "../security/config.js";
|
|
5
5
|
import type { PipiclawSettingsManager } from "../settings.js";
|
|
6
|
+
import type { SubAgentRunManager } from "../subagents/runs.js";
|
|
7
|
+
import type { ChannelJobManager } from "./job-manager.js";
|
|
6
8
|
import type { AgentRunner } from "./types.js";
|
|
7
9
|
export interface RunnerFactoryPaths {
|
|
10
|
+
jobManager?: ChannelJobManager;
|
|
11
|
+
runManager?: SubAgentRunManager;
|
|
8
12
|
appHomeDir: string;
|
|
9
13
|
authConfigPath: string;
|
|
10
14
|
modelsConfigPath: string;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Agent, AgentTool, ThinkingLevel } from "@earendil-works/pi-agent-core";
|
|
2
|
+
import type { Api, Model } from "@earendil-works/pi-ai";
|
|
3
|
+
import { type AgentSession, SettingsManager as SDKSettingsManager, type SessionManager } from "@earendil-works/pi-coding-agent";
|
|
4
|
+
import type { PipiclawSettingsManager } from "../settings.js";
|
|
5
|
+
export declare function asSdkSettingsManager(manager: PipiclawSettingsManager): SDKSettingsManager;
|
|
6
|
+
export declare const DEFAULT_MAIN_THINKING_LEVEL: ThinkingLevel;
|
|
7
|
+
/** Apply the pi 0.83 source-model thinking compatibility branch to one real AgentSession. */
|
|
8
|
+
export declare function setModelWithThinkingPreservation(session: AgentSession, manager: SDKSettingsManager, model: Model<Api>): Promise<void>;
|
|
9
|
+
export declare function setThinkingLevelWithConditionalPersist(session: AgentSession, level: ThinkingLevel): void;
|
|
10
|
+
export declare function cycleThinkingLevelWithConditionalPersist(session: AgentSession): ThinkingLevel | undefined;
|
|
11
|
+
export declare function initializeThinkingLevelCompat(agent: Agent, model: Model<Api>, sessionManager: SessionManager, configuredDefault: ThinkingLevel | undefined): ThinkingLevel;
|
|
12
|
+
/**
|
|
13
|
+
* Overwrite the SDK session's `baseToolsOverride` map so a resource reload swaps in
|
|
14
|
+
* freshly-built tools. The SDK exposes no public setter for this, so we reach into the
|
|
15
|
+
* private `_baseToolsOverride` field. This is the single, isolated point of that coupling:
|
|
16
|
+
* if a future SDK renames or removes the field, the guard below warns loudly instead of
|
|
17
|
+
* silently leaving stale tools in place. Replace with a public setter once upstream adds one.
|
|
18
|
+
*/
|
|
19
|
+
export declare function setSessionBaseToolsOverride(session: AgentSession, tools: AgentTool<any>[], channelId: string): void;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { clampThinkingLevel } from "@earendil-works/pi-ai/compat";
|
|
2
|
+
import { SettingsManager as SDKSettingsManager, } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
import * as log from "../log.js";
|
|
4
|
+
export function asSdkSettingsManager(manager) {
|
|
5
|
+
// The upstream session needs its broad interactive SettingsManager surface, while
|
|
6
|
+
// Pipiclaw deliberately owns a small runtime-specific settings contract. Build a
|
|
7
|
+
// real upstream manager from that contract instead of making PipiclawSettingsManager
|
|
8
|
+
// pretend to implement dozens of unrelated no-op UI preferences.
|
|
9
|
+
return SDKSettingsManager.inMemory({
|
|
10
|
+
defaultProvider: manager.getDefaultProvider(),
|
|
11
|
+
defaultModel: manager.getDefaultModel(),
|
|
12
|
+
defaultThinkingLevel: manager.getDefaultThinkingLevel() ?? DEFAULT_MAIN_THINKING_LEVEL,
|
|
13
|
+
compaction: manager.getCompactionSettings(),
|
|
14
|
+
retry: manager.getRetrySettings(),
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
export const DEFAULT_MAIN_THINKING_LEVEL = "medium";
|
|
18
|
+
/** Apply the pi 0.83 source-model thinking compatibility branch to one real AgentSession. */
|
|
19
|
+
export async function setModelWithThinkingPreservation(session, manager, model) {
|
|
20
|
+
const source = session.model;
|
|
21
|
+
if (!source?.reasoning) {
|
|
22
|
+
await session.setModel(model);
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
const previousDefault = manager.getDefaultThinkingLevel();
|
|
26
|
+
manager.setDefaultThinkingLevel(session.thinkingLevel);
|
|
27
|
+
try {
|
|
28
|
+
await session.setModel(model);
|
|
29
|
+
}
|
|
30
|
+
finally {
|
|
31
|
+
manager.setDefaultThinkingLevel(previousDefault ?? DEFAULT_MAIN_THINKING_LEVEL);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
export function setThinkingLevelWithConditionalPersist(session, level) {
|
|
35
|
+
const before = session.thinkingLevel;
|
|
36
|
+
session.setThinkingLevel(level);
|
|
37
|
+
const effective = session.thinkingLevel;
|
|
38
|
+
if (effective === before)
|
|
39
|
+
return;
|
|
40
|
+
if (!session.model?.reasoning && effective === "off")
|
|
41
|
+
return;
|
|
42
|
+
session.setThinkingLevel(effective, { persist: true });
|
|
43
|
+
}
|
|
44
|
+
export function cycleThinkingLevelWithConditionalPersist(session) {
|
|
45
|
+
const before = session.thinkingLevel;
|
|
46
|
+
const next = session.cycleThinkingLevel();
|
|
47
|
+
const effective = session.thinkingLevel;
|
|
48
|
+
if (next && effective !== before && (session.model?.reasoning || effective !== "off")) {
|
|
49
|
+
session.setThinkingLevel(effective, { persist: true });
|
|
50
|
+
}
|
|
51
|
+
return next;
|
|
52
|
+
}
|
|
53
|
+
export function initializeThinkingLevelCompat(agent, model, sessionManager, configuredDefault) {
|
|
54
|
+
const branch = sessionManager.getBranch();
|
|
55
|
+
const hasThinkingEntry = branch.some((entry) => entry.type === "thinking_level_change");
|
|
56
|
+
const historical = sessionManager.buildSessionContext().thinkingLevel;
|
|
57
|
+
const requested = hasThinkingEntry ? historical : (configuredDefault ?? DEFAULT_MAIN_THINKING_LEVEL);
|
|
58
|
+
const effective = clampThinkingLevel(model, requested);
|
|
59
|
+
agent.state.thinkingLevel = effective;
|
|
60
|
+
if (!hasThinkingEntry)
|
|
61
|
+
sessionManager.appendThinkingLevelChange(effective);
|
|
62
|
+
return effective;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Overwrite the SDK session's `baseToolsOverride` map so a resource reload swaps in
|
|
66
|
+
* freshly-built tools. The SDK exposes no public setter for this, so we reach into the
|
|
67
|
+
* private `_baseToolsOverride` field. This is the single, isolated point of that coupling:
|
|
68
|
+
* if a future SDK renames or removes the field, the guard below warns loudly instead of
|
|
69
|
+
* silently leaving stale tools in place. Replace with a public setter once upstream adds one.
|
|
70
|
+
*/
|
|
71
|
+
export function setSessionBaseToolsOverride(session, tools, channelId) {
|
|
72
|
+
const target = session;
|
|
73
|
+
if (!("_baseToolsOverride" in target)) {
|
|
74
|
+
log.logWarning(`[${channelId}] AgentSession no longer exposes _baseToolsOverride; tool reloads may use stale tools (SDK change?)`);
|
|
75
|
+
}
|
|
76
|
+
target._baseToolsOverride = Object.fromEntries(tools.map((tool) => [tool.name, tool]));
|
|
77
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type BootstrapPaths } from "./runtime/app-home.js";
|
|
2
|
+
import { PipiclawSettingsManager } from "./settings.js";
|
|
3
|
+
/**
|
|
4
|
+
* Transport-neutral app services shared by the DingTalk runtime, the terminal TUI, and the
|
|
5
|
+
* `pipiclaw auth` CLI: loads settings (surfacing load errors) and reports tool/security config
|
|
6
|
+
* diagnostics. Does NOT touch DingTalk config, so non-DingTalk entrypoints can call it without
|
|
7
|
+
* any DingTalk credentials.
|
|
8
|
+
*
|
|
9
|
+
* Lives at the package root rather than in `runtime/` on purpose: `models/auth-cli.ts` needs it
|
|
10
|
+
* before a runner or channel exists, and `runtime/bootstrap.ts` separately depends on `models/`
|
|
11
|
+
* (for the default model reference) — parking this function inside `bootstrap.ts` made `models`
|
|
12
|
+
* and `runtime` depend on each other. Everything this function touches (`settings.ts`,
|
|
13
|
+
* `tools/config.ts`, `security/config.ts`, `runtime/app-home.ts`, `runtime/proxy.ts`) has no
|
|
14
|
+
* import back to `models/`, so this file doesn't reintroduce the cycle it was extracted to break.
|
|
15
|
+
*/
|
|
16
|
+
export declare function prepareAppServices(paths?: BootstrapPaths): {
|
|
17
|
+
settingsManager: PipiclawSettingsManager;
|
|
18
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import * as log from "./log.js";
|
|
2
|
+
import { DEFAULT_BOOTSTRAP_PATHS } from "./runtime/app-home.js";
|
|
3
|
+
import { installLlmProxy } from "./runtime/proxy.js";
|
|
4
|
+
import { loadSecurityConfigWithDiagnostics } from "./security/config.js";
|
|
5
|
+
import { PipiclawSettingsManager } from "./settings.js";
|
|
6
|
+
import { formatConfigDiagnostic } from "./shared/config-diagnostic.js";
|
|
7
|
+
import { loadToolsConfigWithDiagnostics } from "./tools/config.js";
|
|
8
|
+
/**
|
|
9
|
+
* Transport-neutral app services shared by the DingTalk runtime, the terminal TUI, and the
|
|
10
|
+
* `pipiclaw auth` CLI: loads settings (surfacing load errors) and reports tool/security config
|
|
11
|
+
* diagnostics. Does NOT touch DingTalk config, so non-DingTalk entrypoints can call it without
|
|
12
|
+
* any DingTalk credentials.
|
|
13
|
+
*
|
|
14
|
+
* Lives at the package root rather than in `runtime/` on purpose: `models/auth-cli.ts` needs it
|
|
15
|
+
* before a runner or channel exists, and `runtime/bootstrap.ts` separately depends on `models/`
|
|
16
|
+
* (for the default model reference) — parking this function inside `bootstrap.ts` made `models`
|
|
17
|
+
* and `runtime` depend on each other. Everything this function touches (`settings.ts`,
|
|
18
|
+
* `tools/config.ts`, `security/config.ts`, `runtime/app-home.ts`, `runtime/proxy.ts`) has no
|
|
19
|
+
* import back to `models/`, so this file doesn't reintroduce the cycle it was extracted to break.
|
|
20
|
+
*/
|
|
21
|
+
export function prepareAppServices(paths = DEFAULT_BOOTSTRAP_PATHS) {
|
|
22
|
+
// Shared by the DingTalk daemon, the TUI, and the auth CLI (all call prepareAppServices), so
|
|
23
|
+
// this covers every entrypoint that talks to an LLM provider.
|
|
24
|
+
installLlmProxy();
|
|
25
|
+
const settingsManager = new PipiclawSettingsManager(paths.appHomeDir);
|
|
26
|
+
for (const { scope, error } of settingsManager.drainErrors()) {
|
|
27
|
+
log.logWarning(`Failed to load ${scope} settings`, `${error.message}\n${paths.settingsConfigPath}`);
|
|
28
|
+
}
|
|
29
|
+
// Errors already went out above with a richer message; this pass exists for the
|
|
30
|
+
// warnings, chiefly retired settings keys (spec 035 D3).
|
|
31
|
+
for (const diagnostic of settingsManager.getDiagnostics()) {
|
|
32
|
+
if (diagnostic.severity === "error")
|
|
33
|
+
continue;
|
|
34
|
+
log.logWarning(formatConfigDiagnostic(diagnostic), diagnostic.path);
|
|
35
|
+
}
|
|
36
|
+
for (const diagnostic of loadToolsConfigWithDiagnostics(paths.appHomeDir).diagnostics) {
|
|
37
|
+
log.logWarning(formatConfigDiagnostic(diagnostic), diagnostic.path);
|
|
38
|
+
}
|
|
39
|
+
for (const diagnostic of loadSecurityConfigWithDiagnostics(paths.appHomeDir).diagnostics) {
|
|
40
|
+
log.logWarning(formatConfigDiagnostic(diagnostic), diagnostic.path);
|
|
41
|
+
}
|
|
42
|
+
return { settingsManager };
|
|
43
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { ChannelEvent } from "../channel/channel-event.js";
|
|
2
2
|
import type { Executor } from "../executor.js";
|
|
3
|
+
import type { DingTalkBot } from "../runtime/dingtalk.js";
|
|
3
4
|
import type { SecurityConfig } from "../security/types.js";
|
|
4
|
-
import type { DingTalkBot } from "./dingtalk.js";
|
|
5
5
|
export interface EventAction {
|
|
6
6
|
type: "bash";
|
|
7
7
|
command: string;
|
package/dist/memory/journal.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { mkdir, open, readdir } from "node:fs/promises";
|
|
2
2
|
import { join } from "node:path";
|
|
3
3
|
import { writeFileAtomically } from "../shared/atomic-file.js";
|
|
4
|
-
import { isNodeError, readOptionalTextFile } from "../shared/fs-utils.js";
|
|
5
4
|
import { localDayKey } from "../shared/local-time.js";
|
|
5
|
+
import { isNodeError, readOptionalTextFile } from "../shared/os.js";
|
|
6
6
|
/**
|
|
7
7
|
* Spec 050, D5: `journal/YYYY-MM-DD.md`, append-only, one file per local day. Replaces
|
|
8
8
|
* `SESSION.md` (current state) and `HISTORY.md` (folded older history) — a day is never
|
|
@@ -1,11 +1,8 @@
|
|
|
1
1
|
import type { PipiclawMemoryMaintenanceSettings } from "../settings.js";
|
|
2
2
|
import type { MemoryMaintenanceState } from "./maintenance-state.js";
|
|
3
|
-
/** Spec 050, D9: the three v1 jobs collapse to one. */
|
|
4
|
-
export type MaintenanceJobKind = "reflect";
|
|
5
3
|
export interface MaintenanceGateDecision {
|
|
6
4
|
allowed: boolean;
|
|
7
5
|
skipReason?: string;
|
|
8
|
-
jobKind: MaintenanceJobKind;
|
|
9
6
|
}
|
|
10
7
|
export interface ReflectMaterial {
|
|
11
8
|
hasNewEntry: boolean;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { parseLocalTime } from "../shared/local-time.js";
|
|
2
2
|
function deny(skipReason) {
|
|
3
|
-
return { allowed: false,
|
|
3
|
+
return { allowed: false, skipReason };
|
|
4
4
|
}
|
|
5
|
-
const allow = { allowed: true
|
|
5
|
+
const allow = { allowed: true };
|
|
6
6
|
function parseTime(value) {
|
|
7
7
|
if (!value) {
|
|
8
8
|
return null;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/** Upgrade before starting channel readers/writers. Kept through the 0.9.3 upgrade window.
|
|
2
|
+
* Canonical state wins over an obsolete nested copy; old files are removed only after a
|
|
3
|
+
* successful canonical write. Failed I/O aborts startup so no cursor is silently discarded.
|
|
4
|
+
*/
|
|
5
|
+
export declare function migrateMemoryMaintenanceStates(appHomeDir: string): Promise<void>;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { readdir, readFile, rmdir, unlink } from "node:fs/promises";
|
|
2
|
+
import { basename, dirname, join } from "node:path";
|
|
3
|
+
import { isChannelId } from "../channel/channel-paths.js";
|
|
4
|
+
import { writeFileAtomically } from "../shared/atomic-file.js";
|
|
5
|
+
import { isRecord } from "../shared/type-guards.js";
|
|
6
|
+
import { getMemoryMaintenanceStateDir, getMemoryMaintenanceStatePath } from "./maintenance-state.js";
|
|
7
|
+
/** Upgrade before starting channel readers/writers. Kept through the 0.9.3 upgrade window.
|
|
8
|
+
* Canonical state wins over an obsolete nested copy; old files are removed only after a
|
|
9
|
+
* successful canonical write. Failed I/O aborts startup so no cursor is silently discarded.
|
|
10
|
+
*/
|
|
11
|
+
export async function migrateMemoryMaintenanceStates(appHomeDir) {
|
|
12
|
+
const root = getMemoryMaintenanceStateDir(appHomeDir);
|
|
13
|
+
async function files(dir) {
|
|
14
|
+
const entries = await readdir(dir, { withFileTypes: true });
|
|
15
|
+
const result = [];
|
|
16
|
+
for (const entry of entries) {
|
|
17
|
+
const path = join(dir, entry.name);
|
|
18
|
+
if (entry.isDirectory())
|
|
19
|
+
result.push(...(await files(path)));
|
|
20
|
+
else if (entry.isFile() && entry.name.endsWith(".json"))
|
|
21
|
+
result.push(path);
|
|
22
|
+
}
|
|
23
|
+
return result;
|
|
24
|
+
}
|
|
25
|
+
let paths;
|
|
26
|
+
try {
|
|
27
|
+
paths = await files(root);
|
|
28
|
+
}
|
|
29
|
+
catch (error) {
|
|
30
|
+
if (error.code === "ENOENT")
|
|
31
|
+
return;
|
|
32
|
+
throw error;
|
|
33
|
+
}
|
|
34
|
+
for (const source of paths) {
|
|
35
|
+
let raw;
|
|
36
|
+
try {
|
|
37
|
+
raw = JSON.parse(await readFile(source, "utf8"));
|
|
38
|
+
}
|
|
39
|
+
catch (error) {
|
|
40
|
+
if (error instanceof SyntaxError)
|
|
41
|
+
continue; // normal reader diagnoses corrupt state
|
|
42
|
+
throw error;
|
|
43
|
+
}
|
|
44
|
+
if (!isRecord(raw))
|
|
45
|
+
continue;
|
|
46
|
+
const channelId = typeof raw.channelId === "string" ? raw.channelId : basename(source, ".json");
|
|
47
|
+
if (!isChannelId(channelId))
|
|
48
|
+
continue;
|
|
49
|
+
const target = getMemoryMaintenanceStatePath(appHomeDir, channelId);
|
|
50
|
+
if (source !== target && source !== join(root, `${channelId}.json`))
|
|
51
|
+
continue;
|
|
52
|
+
let canonicalExists = false;
|
|
53
|
+
if (source !== target) {
|
|
54
|
+
try {
|
|
55
|
+
const existing = JSON.parse(await readFile(target, "utf8"));
|
|
56
|
+
if (!isRecord(existing) || (existing.channelId !== undefined && existing.channelId !== channelId)) {
|
|
57
|
+
throw new Error(`Invalid memory maintenance state at ${target}; repair it before restarting. Legacy state retained at ${source}.`);
|
|
58
|
+
}
|
|
59
|
+
canonicalExists = true;
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
if (error.code !== "ENOENT")
|
|
63
|
+
throw error;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
const hasOldFields = "lastCheckpointAt" in raw || "lastCheckpointEntryId" in raw;
|
|
67
|
+
if (!canonicalExists && (source !== target || hasOldFields)) {
|
|
68
|
+
for (const [current, old] of [
|
|
69
|
+
["lastReflectAt", "lastCheckpointAt"],
|
|
70
|
+
["lastReflectedEntryId", "lastCheckpointEntryId"],
|
|
71
|
+
]) {
|
|
72
|
+
if (typeof raw[current] !== "string" || !raw[current].trim())
|
|
73
|
+
raw[current] = raw[old];
|
|
74
|
+
delete raw[old];
|
|
75
|
+
}
|
|
76
|
+
await writeFileAtomically(target, `${JSON.stringify({ ...raw, channelId }, null, 2)}\n`);
|
|
77
|
+
}
|
|
78
|
+
if (source !== target) {
|
|
79
|
+
await unlink(source);
|
|
80
|
+
// Empty legacy directories only; never remove unrelated state.
|
|
81
|
+
for (let dir = dirname(source); dir !== root; dir = dirname(dir)) {
|
|
82
|
+
try {
|
|
83
|
+
await rmdir(dir);
|
|
84
|
+
}
|
|
85
|
+
catch {
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { readFile
|
|
2
|
-
import {
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { join } from "node:path";
|
|
3
3
|
import { getChannelDirName } from "../channel/channel-paths.js";
|
|
4
4
|
import * as log from "../log.js";
|
|
5
5
|
import { writeFileAtomically } from "../shared/atomic-file.js";
|
|
@@ -20,19 +20,6 @@ export function getMemoryMaintenanceStateDir(appHomeDir) {
|
|
|
20
20
|
export function getMemoryMaintenanceStatePath(appHomeDir, channelId) {
|
|
21
21
|
return join(getMemoryMaintenanceStateDir(appHomeDir), `${getChannelDirName(channelId)}.json`);
|
|
22
22
|
}
|
|
23
|
-
/**
|
|
24
|
-
* Where a pre-escaping runtime put the same channel's state. Only differs for ids containing
|
|
25
|
-
* `/`, and only matters until the next write moves the state to its canonical path.
|
|
26
|
-
*
|
|
27
|
-
* RETIRE AT v0.9.3, same rationale as the header comment in `../runtime/task-migration.js`: by
|
|
28
|
-
* the stable cut, every still-running group channel with a `/` in its id will have had its state
|
|
29
|
-
* folded onto the canonical path at least once, so this fallback read finds nothing.
|
|
30
|
-
*/
|
|
31
|
-
function legacyStatePath(appHomeDir, channelId) {
|
|
32
|
-
return getChannelDirName(channelId) === channelId
|
|
33
|
-
? undefined
|
|
34
|
-
: join(getMemoryMaintenanceStateDir(appHomeDir), `${channelId}.json`);
|
|
35
|
-
}
|
|
36
23
|
function createDefaultState(channelId) {
|
|
37
24
|
return { channelId, dirty: false, failureBackoffUntil: null };
|
|
38
25
|
}
|
|
@@ -55,11 +42,8 @@ function normalizeState(channelId, value) {
|
|
|
55
42
|
dirty: typeof record.dirty === "boolean" ? record.dirty : false,
|
|
56
43
|
lastActivityAt: normalizeOptionalString(record.lastActivityAt),
|
|
57
44
|
eligibleAfter: normalizeOptionalString(record.eligibleAfter),
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
// history nor lose its due time.
|
|
61
|
-
lastReflectAt: normalizeOptionalString(record.lastReflectAt) ?? normalizeOptionalString(record.lastCheckpointAt),
|
|
62
|
-
lastReflectedEntryId: normalizeOptionalString(record.lastReflectedEntryId) ?? normalizeOptionalString(record.lastCheckpointEntryId),
|
|
45
|
+
lastReflectAt: normalizeOptionalString(record.lastReflectAt),
|
|
46
|
+
lastReflectedEntryId: normalizeOptionalString(record.lastReflectedEntryId),
|
|
63
47
|
failureBackoffUntil: normalizeOptionalNullableString(record.failureBackoffUntil) ?? null,
|
|
64
48
|
};
|
|
65
49
|
}
|
|
@@ -71,42 +55,19 @@ export async function readMemoryMaintenanceState(appHomeDir, channelId) {
|
|
|
71
55
|
}
|
|
72
56
|
catch (error) {
|
|
73
57
|
if (error instanceof Error && "code" in error && error.code === "ENOENT") {
|
|
74
|
-
|
|
75
|
-
return legacy ?? createDefaultState(channelId);
|
|
58
|
+
return createDefaultState(channelId);
|
|
76
59
|
}
|
|
77
60
|
const message = errorMessage(error);
|
|
78
61
|
log.logWarning(`[${channelId}] Failed to read memory maintenance state; rebuilding defaults`, message);
|
|
79
62
|
return createDefaultState(channelId);
|
|
80
63
|
}
|
|
81
64
|
}
|
|
82
|
-
async function readLegacyState(appHomeDir, channelId) {
|
|
83
|
-
const path = legacyStatePath(appHomeDir, channelId);
|
|
84
|
-
if (!path)
|
|
85
|
-
return undefined;
|
|
86
|
-
try {
|
|
87
|
-
return normalizeState(channelId, JSON.parse(await readFile(path, "utf-8")));
|
|
88
|
-
}
|
|
89
|
-
catch {
|
|
90
|
-
return undefined; // absent or unreadable ⇒ the caller falls back to defaults, as before
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
65
|
export async function updateMemoryMaintenanceState(appHomeDir, channelId, update) {
|
|
94
66
|
const path = getMemoryMaintenanceStatePath(appHomeDir, channelId);
|
|
95
67
|
return stateUpdateQueue.run(path, async () => {
|
|
96
68
|
const current = await readMemoryMaintenanceState(appHomeDir, channelId);
|
|
97
69
|
const next = normalizeState(channelId, update(current));
|
|
98
70
|
await writeFileAtomically(path, `${JSON.stringify(next, null, 2)}\n`);
|
|
99
|
-
// The canonical file now holds everything the legacy one did; leaving it would keep a
|
|
100
|
-
// stale copy around forever, since reads only fall back when the canonical file is gone.
|
|
101
|
-
const legacy = legacyStatePath(appHomeDir, channelId);
|
|
102
|
-
if (legacy) {
|
|
103
|
-
await unlink(legacy).catch(() => { });
|
|
104
|
-
// ...along with the directory the raw id created, which `rmdir` leaves alone if
|
|
105
|
-
// another channel's state still lives there. Tidying only; failure changes nothing.
|
|
106
|
-
const strayDir = dirname(legacy);
|
|
107
|
-
if (strayDir !== getMemoryMaintenanceStateDir(appHomeDir))
|
|
108
|
-
await rmdir(strayDir).catch(() => { });
|
|
109
|
-
}
|
|
110
71
|
return next;
|
|
111
72
|
});
|
|
112
73
|
}
|
package/dist/memory/migrate.js
CHANGED
|
@@ -2,8 +2,8 @@ import { createHash } from "node:crypto";
|
|
|
2
2
|
import { existsSync } from "node:fs";
|
|
3
3
|
import { mkdir, readdir, rename, rm, writeFile } from "node:fs/promises";
|
|
4
4
|
import { join } from "node:path";
|
|
5
|
-
import { readOptionalTextFile } from "../shared/fs-utils.js";
|
|
6
5
|
import { localDayKey, parseLocalTime } from "../shared/local-time.js";
|
|
6
|
+
import { readOptionalTextFile } from "../shared/os.js";
|
|
7
7
|
import { dedupeMemoryName, getChannelMemoryDir, getChannelMemoryIndexPath, getMemoryEntryPath, rebuildMemoryIndex, serializeMemoryEntry, slugifyMemoryName, } from "./store.js";
|
|
8
8
|
import { hashMemoryContent } from "./tombstones.js";
|
|
9
9
|
/**
|
|
@@ -3,7 +3,6 @@ import type { Api, Model } from "@earendil-works/pi-ai";
|
|
|
3
3
|
import type { SessionEntry } from "@earendil-works/pi-coding-agent";
|
|
4
4
|
import type { PipiclawMemoryMaintenanceSettings } from "../settings.js";
|
|
5
5
|
import { type ChannelMemoryQueue } from "./channel-maintenance-queue.js";
|
|
6
|
-
import { type MaintenanceJobKind } from "./maintenance-gates.js";
|
|
7
6
|
import { runReflect } from "./reflect.js";
|
|
8
7
|
/**
|
|
9
8
|
* Spec 050, D7/D9: the scheduler's idle-tick entry point into the reflect pass. Boundary
|
|
@@ -28,14 +27,7 @@ export interface ReflectJobInput {
|
|
|
28
27
|
sessionEntries: () => SessionEntry[];
|
|
29
28
|
queue?: ChannelMemoryQueue;
|
|
30
29
|
}
|
|
31
|
-
export
|
|
32
|
-
jobKind: MaintenanceJobKind;
|
|
33
|
-
ran: boolean;
|
|
34
|
-
skipped: boolean;
|
|
35
|
-
skipReason?: string;
|
|
36
|
-
error?: string;
|
|
37
|
-
}
|
|
38
|
-
export declare function runReflectJob(input: ReflectJobInput): Promise<ReflectJobResult>;
|
|
30
|
+
export declare function runReflectJob(input: ReflectJobInput): Promise<void>;
|
|
39
31
|
/** Shared by the idle job and `lifecycle.ts`'s boundary calls, so both write the same shape. */
|
|
40
32
|
export declare function reviewLogEntryFor(result: Awaited<ReturnType<typeof runReflect>>): {
|
|
41
33
|
actions?: unknown[];
|
|
@@ -45,7 +45,7 @@ export async function runReflectJob(input) {
|
|
|
45
45
|
reason: "reflect",
|
|
46
46
|
skipped: [{ target: "reflect", reason: decision.skipReason }],
|
|
47
47
|
});
|
|
48
|
-
return
|
|
48
|
+
return;
|
|
49
49
|
}
|
|
50
50
|
const sourceWindow = loadSourceWindow();
|
|
51
51
|
try {
|
|
@@ -71,7 +71,6 @@ export async function runReflectJob(input) {
|
|
|
71
71
|
correlationId: sourceWindow.windowId,
|
|
72
72
|
...reviewLogEntryFor(result),
|
|
73
73
|
});
|
|
74
|
-
return { jobKind: "reflect", ran: !result.skipped, skipped: result.skipped };
|
|
75
74
|
}
|
|
76
75
|
catch (error) {
|
|
77
76
|
await updateMemoryMaintenanceState(input.appHomeDir, input.channelId, (current) => ({
|
|
@@ -85,7 +84,6 @@ export async function runReflectJob(input) {
|
|
|
85
84
|
reason: "reflect",
|
|
86
85
|
error: message,
|
|
87
86
|
});
|
|
88
|
-
return { jobKind: "reflect", ran: false, skipped: false, error: message };
|
|
89
87
|
}
|
|
90
88
|
});
|
|
91
89
|
}
|
package/dist/memory/reflect.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { serializeConversation } from "@earendil-works/pi-coding-agent";
|
|
2
|
-
import { readOptionalTextFile } from "../shared/fs-utils.js";
|
|
3
2
|
import { parseJsonObject } from "../shared/llm-json.js";
|
|
4
3
|
import { localDayKey } from "../shared/local-time.js";
|
|
4
|
+
import { readOptionalTextFile } from "../shared/os.js";
|
|
5
5
|
import { clipText } from "../shared/text-utils.js";
|
|
6
6
|
import { buildChannelIndexForBootstrap, CHANNEL_INDEX_MAX_UNITS } from "./index-budget.js";
|
|
7
7
|
import { appendJournalEntries } from "./journal.js";
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { readdir, stat } from "node:fs/promises";
|
|
2
2
|
import { basename, join } from "node:path";
|
|
3
|
-
import { isNodeError, readOptionalTextFile } from "../shared/
|
|
3
|
+
import { isNodeError, readOptionalTextFile } from "../shared/os.js";
|
|
4
4
|
import { isRecord } from "../shared/type-guards.js";
|
|
5
5
|
/**
|
|
6
6
|
* Ceiling on how much of one message is kept for *matching*. The old code clipped each message to
|