@dotdotgod/context 0.3.6 → 0.3.8

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 (2) hide show
  1. package/package.json +2 -2
  2. package/src/hooks.mjs +41 -4
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dotdotgod/context",
3
- "version": "0.3.6",
3
+ "version": "0.3.8",
4
4
  "description": "Local-first MCP execution, indexing, and retrieval runtime for dotdotgod adapters.",
5
5
  "type": "module",
6
6
  "license": "Elastic-2.0",
@@ -28,7 +28,7 @@
28
28
  "dependencies": {
29
29
  "@modelcontextprotocol/sdk": "^1.25.2",
30
30
  "zod": "^3.25.76",
31
- "@dotdotgod/cli": "0.3.6"
31
+ "@dotdotgod/cli": "0.3.8"
32
32
  },
33
33
  "engines": {
34
34
  "node": ">=22.5.0"
package/src/hooks.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
2
2
  import { createHash } from 'node:crypto';
3
- import { dirname, join, parse, resolve } from 'node:path';
3
+ import { dirname, isAbsolute, join, parse, relative, resolve } from 'node:path';
4
4
 
5
5
  function inputText() { return readFileSync(0, 'utf8'); }
6
6
  function parseInput() { const text = inputText().trim(); return text ? JSON.parse(text) : {}; }
@@ -37,6 +37,25 @@ function writeState(path, state) {
37
37
  const temp = `${path}.${process.pid}.tmp`; writeFileSync(temp, `${JSON.stringify(state, null, 2)}\n`); renameSync(temp, path);
38
38
  }
39
39
  function fileHash(path) { try { return createHash('sha256').update(readFileSync(path)).digest('hex'); } catch { return 'missing'; } }
40
+ function isInsideRoot(root, path) {
41
+ if (!root || typeof path !== 'string') return false;
42
+ const absolute = resolve(path);
43
+ const rel = relative(root, absolute);
44
+ return rel === '' || (!rel.startsWith('..') && !isAbsolute(rel));
45
+ }
46
+ function normalizeProjectPath(root, path) {
47
+ if (!root || typeof path !== 'string' || !path.trim()) return null;
48
+ const absolute = resolve(root, path);
49
+ return isInsideRoot(root, absolute) ? absolute : null;
50
+ }
51
+ function prunePending(state, root) {
52
+ const next = {};
53
+ for (const [path, fingerprint] of Object.entries(state.pending || {})) {
54
+ const absolute = normalizeProjectPath(root, path);
55
+ if (absolute) next[absolute] = fingerprint;
56
+ }
57
+ state.pending = next;
58
+ }
40
59
  function toolName(input) { return String(input.tool_name || input.toolName || ''); }
41
60
  function toolInput(input) { return input.tool_input || input.toolInput || {}; }
42
61
  function mcpTool(name, suffix) { return name === suffix || name.endsWith(`__${suffix}`) || name.endsWith(`_${suffix}`); }
@@ -50,7 +69,14 @@ function isImpactGate(name, input) {
50
69
  function changedPath(input, root) {
51
70
  const value = toolInput(input);
52
71
  const path = value.file_path || value.path;
53
- return root && typeof path === 'string' ? resolve(root, path) : null;
72
+ return normalizeProjectPath(root, path);
73
+ }
74
+ function hasFailedToolResult(input) {
75
+ const response = input.tool_response || input.toolResponse || input.tool_result || input.toolResult || input.result;
76
+ if (!response || typeof response !== 'object') return false;
77
+ if (response.is_error === true || response.isError === true || response.error) return true;
78
+ if (response.ok === false || response.status === 'error' || response.status === 'failed') return true;
79
+ return false;
54
80
  }
55
81
 
56
82
  export function runHook(event) {
@@ -59,6 +85,10 @@ export function runHook(event) {
59
85
  const context = resolveHookContext(input, event);
60
86
  const state = readState(context?.path);
61
87
  state.pending ||= {};
88
+ if (context) {
89
+ prunePending(state, context.root);
90
+ writeState(context.path, state);
91
+ }
62
92
 
63
93
  if (event === 'sessionstart') {
64
94
  if (!context) return {};
@@ -80,10 +110,17 @@ export function runHook(event) {
80
110
  if (mcpTool(name, 'dotdotgod_project_load')) state.loadRequired = false;
81
111
  else if (mcpTool(name, 'dotdotgod_project_impact')) {
82
112
  const checked = toolInput(input).paths;
113
+ let cleared = 0;
83
114
  if (Array.isArray(checked)) for (const path of checked) {
84
- const absolute = resolve(context.root, path);
85
- if (state.pending[absolute] === fileHash(absolute)) delete state.pending[absolute];
115
+ const absolute = normalizeProjectPath(context.root, path);
116
+ if (absolute && Object.hasOwn(state.pending, absolute)) {
117
+ delete state.pending[absolute];
118
+ cleared += 1;
119
+ }
86
120
  }
121
+ writeState(context.path, state);
122
+ if (cleared > 0 && hasFailedToolResult(input)) return { hookSpecificOutput: { hookEventName: 'PostToolUse', additionalContext: `dotdotgod_project_impact reported a failure, but dotdotgod cleared pending graph impact for ${cleared} requested path(s) to avoid a blocking retry loop. Review the impact error before broad operations.` } };
123
+ return {};
87
124
  } else if (/^(Edit|Write|apply_patch)$/i.test(name)) {
88
125
  const path = changedPath(input, context.root);
89
126
  if (path) state.pending[path] = fileHash(path);