kankaku 1.4.1 → 1.4.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.
package/dist/cli.js CHANGED
File without changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kankaku",
3
- "version": "1.4.1",
3
+ "version": "1.4.2",
4
4
  "description": "Terminal app that shows today's kankaku work across every project on disk",
5
5
  "keywords": [
6
6
  "pi-package",
@@ -38,9 +38,9 @@
38
38
  },
39
39
  "dependencies": {
40
40
  "ink": "^7.1.1",
41
- "kankaku-claude": "^1.4.1",
42
- "kankaku-hub": "^0.3.0",
43
- "kankaku-pi": "^1.4.1",
41
+ "kankaku-claude": "^1.4.2",
42
+ "kankaku-hub": "^0.5.1",
43
+ "kankaku-pi": "^1.4.2",
44
44
  "react": "^19.3.0"
45
45
  },
46
46
  "devDependencies": {
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kankaku-pi",
3
- "version": "1.4.1",
3
+ "version": "1.4.2",
4
4
  "description": "pi extension that records agent work time per prompt, excluding waits for the user, with subagent linkage and task/session views",
5
5
  "license": "MIT",
6
6
  "author": "soyunninja",
@@ -32,6 +32,16 @@ function isCatalogSnapshot(value: unknown): value is CatalogSnapshot {
32
32
  );
33
33
  }
34
34
 
35
+ export class CatalogAdoptionFailure extends Error {
36
+ readonly reason: "snapshot-unavailable" | "persistence";
37
+
38
+ constructor(reason: "snapshot-unavailable" | "persistence") {
39
+ super(reason === "persistence" ? "Catalog cache persistence failed" : "Catalog snapshot unavailable");
40
+ this.name = "CatalogAdoptionFailure";
41
+ this.reason = reason;
42
+ }
43
+ }
44
+
35
45
  export interface CachedCatalogDeps {
36
46
  /** Absolute path to the cache file, e.g. `~/.kankaku/catalog.json`. */
37
47
  filePath: string;
@@ -78,11 +88,18 @@ export class CachedCatalog implements Catalog {
78
88
  /** Adopt a POST response without relying on an eventually-consistent list refresh. */
79
89
  adoptTask(task: HubTask): void {
80
90
  const snapshot = this.read();
81
- if (!snapshot) throw new Error("Cannot adopt a task without a catalog snapshot");
91
+ if (!snapshot) throw new CatalogAdoptionFailure("snapshot-unavailable");
82
92
  const adopted = { ...snapshot, tasks: [...(snapshot.tasks ?? []).filter((item) => item.id !== task.id), task] };
83
- this.writeDisk(adopted, true);
93
+ // The parsed POST response is the freshest known task even when atomic
94
+ // persistence fails. Publish it to this process and in-flight refreshes
95
+ // before writing so session target resolution can still validate the link.
84
96
  this.memo = adopted;
85
97
  for (const pending of this.pendingAdoptions) pending.set(task.id, task);
98
+ try {
99
+ this.writeDisk(adopted, true);
100
+ } catch {
101
+ throw new CatalogAdoptionFailure("persistence");
102
+ }
86
103
  }
87
104
 
88
105
  isStale(): boolean {
@@ -31,12 +31,13 @@ export interface CreateHubTaskInput {
31
31
 
32
32
  /** Explicit user-driven creation; never called from hook paths. */
33
33
  export async function createHubTask(client: PocketBaseClient, input: CreateHubTaskInput, signal?: AbortSignal): Promise<HubTask> {
34
- const record = await client.request<TaskRecord>("POST", "/api/collections/tasks/records", {
34
+ const record: unknown = await client.request("POST", "/api/collections/tasks/records", {
35
35
  title: input.title,
36
36
  project: input.projectId,
37
37
  status: "open",
38
38
  ...(input.externalRef !== undefined ? { external_ref: input.externalRef } : {}),
39
39
  }, signal);
40
+ assertCreatedTaskRecord(record, input.projectId);
40
41
  return mapTask(record);
41
42
  }
42
43
 
@@ -64,6 +65,21 @@ function mapProject(record: ProjectRecord): Project {
64
65
  }
65
66
 
66
67
  /** `open` on any unrecognised or missing status, rather than dropping the task. */
68
+ function assertCreatedTaskRecord(record: unknown, projectId: string): asserts record is TaskRecord {
69
+ if (!record || typeof record !== "object" || Array.isArray(record)) {
70
+ throw new Error("Hub returned an invalid task creation response");
71
+ }
72
+ const candidate = record as Record<string, unknown>;
73
+ if (
74
+ typeof candidate["id"] !== "string" || candidate["id"].trim() === "" ||
75
+ typeof candidate["title"] !== "string" || candidate["title"].trim() === "" ||
76
+ candidate["project"] !== projectId ||
77
+ typeof candidate["status"] !== "string" || !TASK_STATUSES.has(candidate["status"])
78
+ ) {
79
+ throw new Error("Hub returned an invalid task creation response");
80
+ }
81
+ }
82
+
67
83
  function mapTaskStatus(status: string | undefined): HubTask["status"] {
68
84
  return status !== undefined && TASK_STATUSES.has(status) ? (status as HubTask["status"]) : "open";
69
85
  }
@@ -4,6 +4,7 @@ import type { WorkTarget, WorkTargetCandidate, WorkTargetSessionOverride, WorkTa
4
4
  import type { WorkRole } from "../domain/work-record.ts";
5
5
  import type { Catalog, CatalogSnapshot } from "../ports/catalog.ts";
6
6
  import type { TaskCreator } from "./target-picker.ts";
7
+ import { formatTaskCreationFailure, TaskCreationFailure } from "./task-creation.ts";
7
8
  import { pickHubTask, pickTarget } from "./target-picker.ts";
8
9
 
9
10
  /** Persisted as a `kankaku-target` custom session entry so the session-level target survives a reload. */
@@ -420,9 +421,17 @@ export function createSessionTarget(deps: SessionTargetDeps): SessionTarget {
420
421
  return;
421
422
  }
422
423
  if (!deps.createTask) throw new Error("task creation is not configured");
423
- const task = await deps.createTask(target.projectId, title, externalRef);
424
- setTask(pi, task.id);
425
- ctx.ui.notify(`kankaku: created and linked: ${task.title}`);
424
+ try {
425
+ const task = await deps.createTask(target.projectId, title, externalRef);
426
+ setTask(pi, task.id);
427
+ ctx.ui.notify(`kankaku: created and linked: ${task.title}`);
428
+ } catch (error) {
429
+ if (error instanceof TaskCreationFailure && error.kind === "cache-persistence" && error.task) {
430
+ setTask(pi, error.task.id);
431
+ }
432
+ const warning = error instanceof TaskCreationFailure && (error.kind === "cache-persistence" || error.kind === "catalog-unavailable");
433
+ ctx.ui.notify(formatTaskCreationFailure(error), warning ? "warning" : "error");
434
+ }
426
435
  }
427
436
 
428
437
  function clearTask(pi: ExtensionAPI): void {
@@ -1,5 +1,6 @@
1
1
  import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
2
2
  import type { Client, HubTask, Project, WorkTarget } from "../domain/work-target.ts";
3
+ import { formatTaskCreationFailure, TaskCreationFailure } from "./task-creation.ts";
3
4
 
4
5
  const SKIP_OPTION = "— skip —";
5
6
  const NO_PROJECT_OPTION = "(no project)";
@@ -142,8 +143,12 @@ export async function pickHubTask(ctx: ExtensionContext, tasks: HubTask[], proje
142
143
  const externalRef = (await ctx.ui.input("kankaku — external reference (optional)"))?.trim();
143
144
  try {
144
145
  return { kind: "picked", task: await createTask(projectId, title, externalRef || undefined) };
145
- } catch {
146
- ctx.ui.notify("kankaku: task creation failed; no task linked", "error");
146
+ } catch (error) {
147
+ if (error instanceof TaskCreationFailure && error.kind === "cache-persistence" && error.task) {
148
+ ctx.ui.notify(formatTaskCreationFailure(error), "warning");
149
+ return { kind: "picked", task: error.task };
150
+ }
151
+ ctx.ui.notify(formatTaskCreationFailure(error), error instanceof TaskCreationFailure && error.kind === "catalog-unavailable" ? "warning" : "error");
147
152
  return { kind: "skipped" };
148
153
  }
149
154
  }
@@ -0,0 +1,68 @@
1
+ import type { HubTask } from "../domain/work-target.ts";
2
+ import type { TaskCreator } from "./target-picker.ts";
3
+ import { CatalogAdoptionFailure } from "./cached-catalog.ts";
4
+ import { PocketBaseError } from "./pocketbase-client.ts";
5
+
6
+ export class TaskCreationFailure extends Error {
7
+ readonly kind: "request" | "cache-persistence" | "catalog-unavailable";
8
+ readonly task: HubTask | undefined;
9
+ readonly source: unknown;
10
+
11
+ constructor(kind: "request" | "cache-persistence" | "catalog-unavailable", task?: HubTask, source?: unknown) {
12
+ super(kind === "request" ? "Task creation request failed" : "Task was created but could not be adopted locally");
13
+ this.name = "TaskCreationFailure";
14
+ this.kind = kind;
15
+ this.task = task;
16
+ this.source = source;
17
+ }
18
+ }
19
+
20
+ /** Compose the remote POST and local cache adoption without hiding known remote success. */
21
+ export function createTaskCreator(createRemote: TaskCreator, adopt: (task: HubTask) => void): TaskCreator {
22
+ return async (projectId, title, externalRef) => {
23
+ let task: HubTask;
24
+ try {
25
+ task = await createRemote(projectId, title, externalRef);
26
+ } catch (error) {
27
+ throw new TaskCreationFailure("request", undefined, error);
28
+ }
29
+ try {
30
+ adopt(task);
31
+ } catch (error) {
32
+ const kind = error instanceof CatalogAdoptionFailure && error.reason === "persistence" ? "cache-persistence" : "catalog-unavailable";
33
+ throw new TaskCreationFailure(kind, task);
34
+ }
35
+ return task;
36
+ };
37
+ }
38
+
39
+ /** A bounded user-facing diagnostic; arbitrary exception text is never included. */
40
+ export function formatTaskCreationFailure(error: unknown): string {
41
+ if (error instanceof TaskCreationFailure && error.kind === "cache-persistence") {
42
+ return "kankaku: task created in the hub, but local catalog cache persistence failed; the task is linked to this session. The in-memory catalog remains available until restart.";
43
+ }
44
+ if (error instanceof TaskCreationFailure && error.kind === "catalog-unavailable") {
45
+ return "kankaku: task created in the hub, but no local catalog snapshot was available to validate a session link. Refresh the catalog, then link the existing task; do not create it again.";
46
+ }
47
+
48
+ const source = error instanceof TaskCreationFailure ? error.source : error;
49
+ let kind: string | undefined;
50
+ let status: number | undefined;
51
+ if (source instanceof PocketBaseError) {
52
+ try {
53
+ const candidateKind: unknown = source.kind;
54
+ const candidateStatus: unknown = source.status;
55
+ if (candidateKind === "network" || candidateKind === "timeout" || candidateKind === "http" || candidateKind === "auth") {
56
+ kind = candidateKind;
57
+ }
58
+ if (typeof candidateStatus === "number" && Number.isInteger(candidateStatus) && candidateStatus >= 100 && candidateStatus <= 599) {
59
+ status = candidateStatus;
60
+ }
61
+ } catch {
62
+ // Malformed typed errors fall back to a generic diagnostic.
63
+ }
64
+ }
65
+ const detail = [kind, status !== undefined ? String(status) : undefined].filter(Boolean).join(" ");
66
+ const uncertain = kind === undefined || kind === "network" || kind === "timeout";
67
+ return `kankaku: task creation was not confirmed${detail ? ` (${detail})` : ""}; ${uncertain ? "the outcome is uncertain; " : ""}check the hub before retrying.`;
68
+ }
@@ -13,6 +13,7 @@ import { resolveHubCredentials, safeHomeDir } from "./adapters/hub-credentials.t
13
13
  import { PocketBaseClient } from "./adapters/pocketbase-client.ts";
14
14
  import { createHubTask, createPocketBaseCatalogFetcher } from "./adapters/pocketbase-catalog.ts";
15
15
  import { CachedCatalog } from "./adapters/cached-catalog.ts";
16
+ import { createTaskCreator } from "./adapters/task-creation.ts";
16
17
  import { createSessionTarget } from "./adapters/session-target.ts";
17
18
  import { resolveKankakuDir, resolveWritableTarget } from "./adapters/kankaku-dir.ts";
18
19
  import { SyncStateStore } from "./adapters/sync-state-store.ts";
@@ -226,11 +227,10 @@ export default function kankaku(pi: ExtensionAPI): void {
226
227
  sessionTarget = createSessionTarget({
227
228
  role,
228
229
  catalog,
229
- createTask: async (projectId, title, externalRef) => {
230
- const task = await createHubTask(client, { projectId, title, ...(externalRef ? { externalRef } : {}) });
231
- cachedCatalog.adoptTask(task);
232
- return task;
233
- },
230
+ createTask: createTaskCreator(
231
+ (projectId, title, externalRef) => createHubTask(client, { projectId, title, ...(externalRef ? { externalRef } : {}) }),
232
+ (task) => cachedCatalog.adoptTask(task),
233
+ ),
234
234
  resolveProjectConfigIds: () => projectTarget.read(),
235
235
  persistProjectConfig: (ids) => projectTarget.write(ids),
236
236
  });