psyclaw 0.30.22 → 0.31.2

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 (51) hide show
  1. package/dist/src/adapters/pi/extension.js +119 -26
  2. package/dist/src/adapters/pi/extension.js.map +1 -1
  3. package/dist/src/adapters/pi/observation-mask.d.ts +9 -3
  4. package/dist/src/adapters/pi/observation-mask.js +54 -9
  5. package/dist/src/adapters/pi/observation-mask.js.map +1 -1
  6. package/dist/src/analysis/method-rules.d.ts +31 -0
  7. package/dist/src/analysis/method-rules.js +128 -0
  8. package/dist/src/analysis/method-rules.js.map +1 -0
  9. package/dist/src/analysis/runtime-tools.d.ts +2 -1
  10. package/dist/src/analysis/runtime-tools.js +79 -8
  11. package/dist/src/analysis/runtime-tools.js.map +1 -1
  12. package/dist/src/analysis/shell-timeout.d.ts +10 -0
  13. package/dist/src/analysis/shell-timeout.js +26 -0
  14. package/dist/src/analysis/shell-timeout.js.map +1 -0
  15. package/dist/src/analysis/skill-resolution.d.ts +89 -0
  16. package/dist/src/analysis/skill-resolution.js +301 -0
  17. package/dist/src/analysis/skill-resolution.js.map +1 -0
  18. package/dist/src/analysis/stats-router.d.ts +6 -2
  19. package/dist/src/analysis/stats-router.js +9 -4
  20. package/dist/src/analysis/stats-router.js.map +1 -1
  21. package/dist/src/chat.d.ts +1 -1
  22. package/dist/src/chat.js +1 -1
  23. package/dist/src/chat.js.map +1 -1
  24. package/dist/src/headless/run.d.ts +3 -0
  25. package/dist/src/headless/run.js +28 -5
  26. package/dist/src/headless/run.js.map +1 -1
  27. package/dist/src/integrations/mcp-runtime.d.ts +3 -1
  28. package/dist/src/integrations/mcp-runtime.js +27 -3
  29. package/dist/src/integrations/mcp-runtime.js.map +1 -1
  30. package/dist/src/project/workspace.d.ts +3 -0
  31. package/dist/src/project/workspace.js +34 -0
  32. package/dist/src/project/workspace.js.map +1 -1
  33. package/dist/src/safety/data-access.js +3 -1
  34. package/dist/src/safety/data-access.js.map +1 -1
  35. package/dist/src/session/modes.d.ts +2 -0
  36. package/dist/src/session/modes.js +27 -1
  37. package/dist/src/session/modes.js.map +1 -1
  38. package/dist/src/skills/recommended.js +13 -1
  39. package/dist/src/skills/recommended.js.map +1 -1
  40. package/dist/src/skills/user-skills.d.ts +20 -0
  41. package/dist/src/skills/user-skills.js +35 -5
  42. package/dist/src/skills/user-skills.js.map +1 -1
  43. package/package.json +1 -1
  44. package/skills/core/analysis-plan/SKILL.md +23 -7
  45. package/skills/core/manifest.json +20 -2
  46. package/skills/core/method-change-scores/SKILL.md +17 -0
  47. package/skills/core/method-interactions/SKILL.md +21 -0
  48. package/skills/core/method-mediation/SKILL.md +17 -0
  49. package/skills/core/method-multilevel/SKILL.md +19 -0
  50. package/skills/core/method-sem/SKILL.md +17 -0
  51. package/skills/core/report-checklist/SKILL.md +21 -0
@@ -1,6 +1,7 @@
1
1
  import { DefaultPackageManager, getAgentDir, SettingsManager } from "@earendil-works/pi-coding-agent";
2
- import { isObservationEvidence, maskToolObservationsStable, observationSettledLimit } from "./observation-mask.js";
2
+ import { isInstructionResult, isObservationEvidence, maskToolObservationsStable, observationSettledLimit } from "./observation-mask.js";
3
3
  import { outputLogFooter, persistToolOutput } from "../../analysis/output-log.js";
4
+ import { applyShellTimeout, isShellTimeout, SHELL_TIMEOUT_HINT } from "../../analysis/shell-timeout.js";
4
5
  import { registerAnalysisRuntimeTools } from "../../analysis/runtime-tools.js";
5
6
  import { formatDataAccess, loadDataAccess, redactScriptOnlyOutput, scriptOnlyBlock, setDataAccess } from "../../safety/data-access.js";
6
7
  import { Type } from "typebox";
@@ -36,10 +37,12 @@ import { isArsPiActive, isArsPiTurn, psyclawArsPatch, setArsPiSessionActive, } f
36
37
  import { buildArsDoctorReport, ensurePdfEngineForExport } from "../../ars/doctor.js";
37
38
  import { formatAcademicSoftRouteInvocation, rankAcademicSoftRoutes, resolveAcademicSoftRoute, } from "../../ars/academic-router.js";
38
39
  import { advanceAnalysisPlan, createAnalysisPlan, formatAnalysisPlanStatus, readActiveAnalysisPlan, syncHandoffFromAnalysisPlan, writeAnalysisPlan, } from "../../analysis/plan.js";
40
+ import { METHOD_TOPICS, mentionsLoadedSkill, methodTopicsIn } from "../../analysis/method-rules.js";
41
+ import { composeMethodInjection, injectedTopicsFor, readSkillPreferences, referencedDocumentTopics, refreshSkillResolution, SKILL_RESOLUTION_ENTRY, userSkillsOutsideScriptOnly, writeSkillPreferences, } from "../../analysis/skill-resolution.js";
39
42
  import { formatAnalysisPlanTakeover, resolveStatsIntent, } from "../../analysis/stats-router.js";
40
43
  import { arsRoot } from "../../ars/pi-panel-executor.js";
41
44
  import { ArsModeEditor, isArsModeEditorText } from "../../ars/mode-editor.js";
42
- import { MODE_STATUS, detectChatModeMismatch, formatChatModeMismatchNotice, nextSessionMode, parseSessionMode, sessionModePrompt, } from "../../session/modes.js";
45
+ import { MODE_STATUS, detectChatModeMismatch, formatChatModeMismatchNotice, nextSessionMode, parseSessionMode, sessionModePrompt, unattendedAnalysisPrompt, } from "../../session/modes.js";
43
46
  import { continuouslyWorkWarningText, isContinuouslyWorkEnabled, } from "../../session/continuously-work.js";
44
47
  import { assertHumanVerifyGate, formatVerifyChecklist, isNaturalPlanConfirm, isPlanRitualConfirm, PLAN_RITUAL_PHRASE, ensureDefaultVerifyChecklist, loadVerifyChecklist, recordHumanVerification } from "../../verify/checklist.js";
45
48
  import { formatSessionHelp, formatSessionHelpBrief } from "../../session/help.js";
@@ -1191,6 +1194,13 @@ export default function psyclawExtension(pi) {
1191
1194
  let arsModeEditor;
1192
1195
  let arsUiContext;
1193
1196
  let sessionMode = "chat";
1197
+ // Sticky mode as persisted on the session branch. Headless runs have no mode editor, so the input
1198
+ // handler must read it here too; otherwise a restored analysis session is treated as chat.
1199
+ const restoreSessionMode = (sessionManager) => {
1200
+ const modeEntry = [...(sessionManager?.getBranch?.() ?? [])].reverse().find((entry) => entry.type === "custom" && entry.customType === "psyclaw-session-mode");
1201
+ return parseSessionMode(modeEntry?.data?.mode)
1202
+ ?? (isArsPiActive(sessionManager) ? "academic" : sessionMode);
1203
+ };
1194
1204
  let turnStartedAt;
1195
1205
  let llmTurnSpan;
1196
1206
  let capturedTurnGeneration = false;
@@ -1224,17 +1234,21 @@ export default function psyclawExtension(pi) {
1224
1234
  }
1225
1235
  if (!legacyTestApi && typeof pi.on === "function")
1226
1236
  pi.on("resources_discover", async (event) => {
1227
- const enabled = await enabledRecommendedSkillPaths(event.cwd);
1228
- const local = await enabledLocalSkillPaths(event.cwd, {
1229
- excludedNames: [...coreSkillNames(), ...await skillNamesInPaths(enabled.paths)],
1230
- });
1231
- // Return only existing Skill directories. Same-name local/core candidates
1232
- // are resolved before Pi sees them. Discovery warnings belong in /skill;
1233
- // they should not interrupt every startup.
1234
- return {
1235
- skillPaths: [...enabled.paths, ...local.paths],
1236
- promptPaths: await enabledLocalPromptPaths(event.cwd),
1237
- };
1237
+ try {
1238
+ // session_start appends the session record; here only the file is refreshed.
1239
+ const discovered = await refreshSkillResolution(event.cwd);
1240
+ return { skillPaths: [...discovered.recommendedPaths, ...discovered.localPaths], promptPaths: discovered.promptPaths };
1241
+ }
1242
+ catch {
1243
+ const enabled = await enabledRecommendedSkillPaths(event.cwd);
1244
+ const local = await enabledLocalSkillPaths(event.cwd, {
1245
+ excludedNames: [...coreSkillNames(), ...await skillNamesInPaths(enabled.paths)],
1246
+ });
1247
+ return {
1248
+ skillPaths: [...enabled.paths, ...local.paths],
1249
+ promptPaths: await enabledLocalPromptPaths(event.cwd),
1250
+ };
1251
+ }
1238
1252
  });
1239
1253
  if (!legacyTestApi && typeof pi.on === "function")
1240
1254
  pi.on("session_shutdown", () => {
@@ -1297,11 +1311,11 @@ export default function psyclawExtension(pi) {
1297
1311
  });
1298
1312
  if (!legacyTestApi && typeof pi.on === "function")
1299
1313
  pi.on("before_agent_start", (event, ctx) => {
1300
- const modeEntry = [...(ctx?.sessionManager?.getBranch?.() ?? [])].reverse().find((entry) => entry.type === "custom" && entry.customType === "psyclaw-session-mode");
1301
- const restored = parseSessionMode(modeEntry?.data?.mode)
1302
- ?? (isArsPiActive(ctx?.sessionManager) ? "academic" : sessionMode);
1314
+ const restored = restoreSessionMode(ctx?.sessionManager);
1303
1315
  sessionMode = restored;
1304
- const modeBlock = sessionModePrompt(restored);
1316
+ const modeBlock = restored === "analysis" && ctx && !ctx.hasUI
1317
+ ? `${sessionModePrompt(restored)}\n\n${unattendedAnalysisPrompt()}`
1318
+ : sessionModePrompt(restored);
1305
1319
  if (restored === "academic" || isArsPiTurn(event.systemPrompt) || isArsPiActive(ctx?.sessionManager)) {
1306
1320
  return {
1307
1321
  systemPrompt: `${event.systemPrompt}\n\n${modeBlock}\n\n${psyclawArsPatch({
@@ -1313,8 +1327,23 @@ export default function psyclawExtension(pi) {
1313
1327
  return { systemPrompt: `${event.systemPrompt}\n\n${modeBlock}` };
1314
1328
  });
1315
1329
  if (!legacyTestApi && typeof pi.on === "function")
1316
- pi.on("session_start", (_event, ctx) => {
1330
+ pi.on("session_start", async (_event, ctx) => {
1317
1331
  arsUiContext = ctx;
1332
+ if (typeof ctx.cwd === "string") {
1333
+ try {
1334
+ const discovered = await refreshSkillResolution(ctx.cwd);
1335
+ try {
1336
+ pi.appendEntry(SKILL_RESOLUTION_ENTRY, discovered.resolution);
1337
+ }
1338
+ catch { /* session may not be open */ }
1339
+ if (ctx.hasUI) {
1340
+ for (const row of discovered.resolution.skipped.filter((item) => item.reason === "core-name")) {
1341
+ ctx.ui.notify(row.warning ?? `核心 skill「${row.name}」不能被同名 skill 覆盖,已跳过 ${row.path}。请改名后再启用。`, "warning");
1342
+ }
1343
+ }
1344
+ }
1345
+ catch { /* discovery must not block the session */ }
1346
+ }
1318
1347
  if (isContinuouslyWorkEnabled() && ctx.hasUI) {
1319
1348
  ctx.ui.setStatus("cw", "continuously-work");
1320
1349
  ctx.ui.notify(continuouslyWorkWarningText(), "error");
@@ -1366,7 +1395,7 @@ export default function psyclawExtension(pi) {
1366
1395
  }
1367
1396
  return { action: "handled" };
1368
1397
  }
1369
- const mode = arsModeEditor?.getMode() ?? sessionMode;
1398
+ const mode = arsModeEditor?.getMode() ?? restoreSessionMode(ctx.sessionManager);
1370
1399
  const trimmed = event.text.trim();
1371
1400
  // A direct user statement is a valid human-review route when Panel is unavailable.
1372
1401
  // Do not infer approval from assistant output, quoted text, or a question about verification.
@@ -1506,13 +1535,51 @@ export default function psyclawExtension(pi) {
1506
1535
  };
1507
1536
  }
1508
1537
  if (mode === "analysis") {
1509
- const match = resolveStatsIntent(event.text);
1510
- if (!match)
1538
+ const userSkills = await userSkillsOutsideScriptOnly(ctx.cwd).catch(() => []);
1539
+ const userSkillNames = userSkills.map((skill) => skill.name);
1540
+ const match = resolveStatsIntent(event.text, { userSkillNames });
1541
+ const namesUserSkill = mentionsLoadedSkill(event.text, userSkillNames);
1542
+ // The task often lives in a document the message names ("Read BRIEF.md"), not in the message itself.
1543
+ const documents = namesUserSkill
1544
+ ? { documents: [], topics: [] }
1545
+ : await referencedDocumentTopics(ctx.cwd, event.text).catch(() => ({ documents: [], topics: [] }));
1546
+ const messageTopics = match?.topics
1547
+ ?? (!namesUserSkill && methodTopicsIn(event.text).includes("report") ? ["report"] : []);
1548
+ // An unattended run writes its report with no reviewer: add the reporting checklist when it works from a task document.
1549
+ const unattendedReport = ctx.hasUI !== true && documents.documents.length > 0 ? ["report"] : [];
1550
+ const wanted = new Set([...messageTopics, ...documents.topics, ...unattendedReport]);
1551
+ const topics = METHOD_TOPICS.filter((topic) => wanted.has(topic));
1552
+ if (!match && topics.length === 0)
1511
1553
  return;
1512
- return {
1513
- action: "transform",
1514
- text: formatAnalysisPlanTakeover(event.text),
1515
- };
1554
+ let block = "";
1555
+ try {
1556
+ const branch = typeof ctx.sessionManager?.getBranch === "function" ? ctx.sessionManager.getBranch() : [];
1557
+ const select = typeof ctx.ui.select === "function"
1558
+ ? (title, labels) => ctx.ui.select(title, labels, { timeout: 60_000 })
1559
+ : undefined;
1560
+ const composed = await composeMethodInjection({
1561
+ cwd: ctx.cwd,
1562
+ topics,
1563
+ seen: injectedTopicsFor(ctx.cwd, branch),
1564
+ hasUI: ctx.hasUI === true,
1565
+ userSkills,
1566
+ ...(select ? { select } : {}),
1567
+ });
1568
+ block = composed.text;
1569
+ if (composed.resolution) {
1570
+ try {
1571
+ pi.appendEntry(SKILL_RESOLUTION_ENTRY, composed.resolution);
1572
+ }
1573
+ catch { /* session may not be open */ }
1574
+ }
1575
+ }
1576
+ catch { /* the process takeover still applies */ }
1577
+ if (match) {
1578
+ return { action: "transform", text: formatAnalysisPlanTakeover(event.text, block || undefined) };
1579
+ }
1580
+ if (block)
1581
+ return { action: "transform", text: `${event.text.trim()}\n\n${block}` };
1582
+ return;
1516
1583
  }
1517
1584
  });
1518
1585
  if (!legacyTestApi && typeof pi.on === "function")
@@ -1535,6 +1602,7 @@ export default function psyclawExtension(pi) {
1535
1602
  return { block: true, terminate: true, reason };
1536
1603
  }
1537
1604
  }
1605
+ applyShellTimeout(event.toolName, event.input);
1538
1606
  try {
1539
1607
  const access = await loadDataAccess(ctx.cwd);
1540
1608
  if (access.warning && !dataAccessWarningShown) {
@@ -1596,7 +1664,13 @@ export default function psyclawExtension(pi) {
1596
1664
  changed = true;
1597
1665
  return { ...part, text };
1598
1666
  });
1599
- if (!isObservationEvidence(event.toolName)) {
1667
+ const timedOutIndex = content.findIndex((part) => part.type === "text" && isShellTimeout(event.toolName, part.text));
1668
+ const timedOut = content[timedOutIndex];
1669
+ if (timedOut?.type === "text") {
1670
+ content[timedOutIndex] = { ...timedOut, text: `${timedOut.text}\n${SHELL_TIMEOUT_HINT}` };
1671
+ changed = true;
1672
+ }
1673
+ if (!isObservationEvidence(event.toolName) && !isInstructionResult(event.toolName)) {
1600
1674
  const body = content.filter((part) => part.type === "text").map((part) => part.text).join("\n");
1601
1675
  const saved = await persistToolOutput(ctx.cwd, event.toolCallId, body, observationSettledLimit());
1602
1676
  if (saved) {
@@ -1944,6 +2018,25 @@ export default function psyclawExtension(pi) {
1944
2018
  }
1945
2019
  },
1946
2020
  });
2021
+ if (!legacyTestApi)
2022
+ pi.registerCommand("skill-preference", {
2023
+ description: "查看或重置方法主题与用户 skill 的冲突选择",
2024
+ handler: async (args, ctx) => {
2025
+ try {
2026
+ if (args.trim() === "reset") {
2027
+ await writeSkillPreferences(ctx.cwd, { schemaVersion: "psyclaw/skill-preferences/v1", choices: {} });
2028
+ ctx.ui.notify("已重置方法主题的 skill 选择。下次冲突会再问。", "info");
2029
+ return;
2030
+ }
2031
+ const preferences = await readSkillPreferences(ctx.cwd);
2032
+ const lines = Object.entries(preferences.choices);
2033
+ ctx.ui.notify(lines.length === 0 ? "没有已保存的方法 skill 选择。" : lines.map(([key, value]) => `${key}: ${value}`).join("\n"), "info");
2034
+ }
2035
+ catch (error) {
2036
+ await notifyError(ctx, error);
2037
+ }
2038
+ },
2039
+ });
1947
2040
  if (!legacyTestApi)
1948
2041
  pi.registerCommand("biosignal", {
1949
2042
  description: "生理信号分析能力包:选型研究域并安装 MCP/Skill",