ask-please 0.4.8

Agent Skills Kit — download version-specific library docs for AI coding agents (Rust port of @pleaseai/ask).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
//! `ask.json` / `.ask/resolved.json` file I/O + hashing — Rust port of
//! `packages/cli/src/io.ts`.
//!
//! `ask.json` is a root-level file (beside `package.json`); `.ask/` holds
//! materialized, gitignored output (docs + the resolved cache).

use std::collections::BTreeMap;
use std::io::Write as _;
use std::path::{Path, PathBuf};

use serde::Serialize;
use sha2::{Digest, Sha256};
use time::format_description::well_known::Rfc3339;
use time::OffsetDateTime;

use crate::ask_json::{AskJson, LibraryEntry};
use crate::resolved::{ResolvedEntry, ResolvedJson};
use crate::spec::{library_name_from_spec, parse_spec, split_explicit_version, ParsedSpec};

const ASK_DIR: &str = ".ask";
const ASK_JSON_FILE: &str = "ask.json";
const RESOLVED_FILE: &str = "resolved.json";

/// Serialize a value to JSON with **recursively sorted keys**, 2-space indent,
/// and a trailing newline. Two calls with semantically equivalent input always
/// produce the same byte string (port of `sortedJSON`).
///
/// Routing through `serde_json::Value` normalizes key order: serde_json's `Map`
/// is a `BTreeMap` by default (the `preserve_order` feature is off), so
/// `to_value` yields sorted maps at every level and arrays keep their order —
/// exactly the TS `sortKeys` contract.
pub fn sorted_json<T: Serialize>(value: &T) -> String {
    let normalized = serde_json::to_value(value).expect("serializable value");
    let mut out = serde_json::to_string_pretty(&normalized).expect("serializable value");
    out.push('\n');
    out
}

/// A file participating in a content hash. Exactly one of `bytes`/`content` is
/// used (bytes wins); an absent payload hashes as empty, matching the TS.
pub struct HashableFile {
    pub relpath: String,
    pub bytes: Option<Vec<u8>>,
    pub content: Option<String>,
}

impl HashableFile {
    fn payload(&self) -> &[u8] {
        if let Some(b) = &self.bytes {
            b
        } else if let Some(c) = &self.content {
            c.as_bytes()
        } else {
            &[]
        }
    }
}

/// Deterministic content hash over a list of files. Files are sorted by relative
/// path; each contributes `<relpath>\0<bytes>\0` to the stream. The NUL
/// separators prevent `path + content` ambiguity (`"ab"+"cd"` vs `"a"+"bcd"`).
/// Returns `sha256-<hex>` (port of `contentHash`).
pub fn content_hash(files: &[HashableFile]) -> String {
    let mut sorted: Vec<&HashableFile> = files.iter().collect();
    sorted.sort_by(|a, b| a.relpath.cmp(&b.relpath));
    let mut hash = Sha256::new();
    for f in sorted {
        hash.update(f.relpath.as_bytes());
        hash.update([0u8]);
        hash.update(f.payload());
        hash.update([0u8]);
    }
    format!("sha256-{:x}", hash.finalize())
}

/// Crash-safe write: stage the bytes in a sibling temp file, fsync, then rename
/// into place. `std::fs::write` truncates the target first, so a crash/kill/full
/// disk mid-write leaves a truncated file — fatal for `ask.json` (the declarative
/// source of truth, which has no self-heal path). The rename is atomic on the
/// same filesystem, so a reader ever sees either the old file or the complete
/// new one. Same final bytes as `fs::write`, so parity is unaffected.
fn write_atomic(path: &Path, contents: &str) -> anyhow::Result<()> {
    let dir = path.parent().unwrap_or_else(|| Path::new("."));
    std::fs::create_dir_all(dir)?;
    let mut tmp = tempfile::NamedTempFile::new_in(dir)?;
    tmp.write_all(contents.as_bytes())?;
    tmp.as_file().sync_all()?;
    tmp.persist(path).map_err(|e| e.error)?;
    Ok(())
}

pub fn get_ask_dir(project_dir: &Path) -> PathBuf {
    project_dir.join(ASK_DIR)
}

/// `ask.json` sits at the project root, NOT inside `.ask/`.
pub fn get_ask_json_path(project_dir: &Path) -> PathBuf {
    project_dir.join(ASK_JSON_FILE)
}

pub fn get_resolved_json_path(project_dir: &Path) -> PathBuf {
    get_ask_dir(project_dir).join(RESOLVED_FILE)
}

/// Locate a library entry in `ask.json` by user-supplied identifier. Matches in
/// three steps (parity with `findEntry` / `ask remove`):
///
///   1. Exact spec string.
///   2. Library name via `library_name_from_spec(split_explicit_version(...))`
///      — so `next` matches both `npm:next` and `npm:next@14`.
///   3. Raw npm package name — `next` → `npm:next`, `@scope/pkg` → `npm:@scope/pkg`.
///
/// Returns `None` when nothing matches so callers can fall through to defaults.
pub fn find_entry<'a>(ask_json: &'a AskJson, target: &str) -> Option<&'a LibraryEntry> {
    for entry in &ask_json.libraries {
        let spec = entry.spec();
        if spec == target {
            return Some(entry);
        }
        let (body, _version) = split_explicit_version(spec);
        if library_name_from_spec(body) == target {
            return Some(entry);
        }
        if let ParsedSpec::Npm { pkg, .. } = parse_spec(body) {
            if pkg == target {
                return Some(entry);
            }
        }
    }
    None
}

/// Read and validate `ask.json`. Returns `Ok(None)` when the file does not
/// exist (so the install orchestrator can bootstrap an empty file). Errors on
/// invalid JSON or schema violations.
pub fn read_ask_json(project_dir: &Path) -> anyhow::Result<Option<AskJson>> {
    let file = get_ask_json_path(project_dir);
    if !file.exists() {
        return Ok(None);
    }
    let raw = std::fs::read_to_string(&file)?;
    let ask = AskJson::parse(&raw)
        .map_err(|e| anyhow::anyhow!("Failed to parse {}: {e}.", file.display()))?;
    Ok(Some(ask))
}

/// Validate and write `ask.json`. Library entries are NOT reordered (users may
/// care about declaration order; `ask add` appends). Only object *keys* within
/// each entry are sorted, via `sorted_json`.
pub fn write_ask_json(project_dir: &Path, ask_json: &AskJson) -> anyhow::Result<()> {
    ask_json.validate()?;
    let file = get_ask_json_path(project_dir);
    write_atomic(&file, &sorted_json(ask_json))
}

/// Read and validate `.ask/resolved.json`. Returns the default empty cache when
/// the file is MISSING or fails to parse/validate — the cache is rebuilt from
/// scratch in that case, which is what makes `resolved.json` safe to delete by
/// hand. A non-NotFound read error (e.g. permission denied) is NOT masked as an
/// empty cache — it is surfaced, matching TS `readResolvedJson` where the
/// `readFileSync` sits OUTSIDE the parse `try/catch` (only parse failures fall
/// back to empty). Masking it would hide a real disk/permission fault behind a
/// silent full re-resolve.
pub fn read_resolved_json(project_dir: &Path) -> anyhow::Result<ResolvedJson> {
    let file = get_resolved_json_path(project_dir);
    let raw = match std::fs::read_to_string(&file) {
        Ok(raw) => raw,
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(empty_resolved()),
        Err(e) => {
            return Err(anyhow::Error::new(e).context(format!("Failed to read {}", file.display())))
        }
    };
    Ok(ResolvedJson::parse(&raw).unwrap_or_else(|_| empty_resolved()))
}

/// The default empty resolved cache.
pub fn empty_resolved() -> ResolvedJson {
    ResolvedJson {
        schema_version: 1,
        generated_at: "1970-01-01T00:00:00Z".to_string(),
        entries: BTreeMap::new(),
    }
}

/// Validate and write `.ask/resolved.json`.
pub fn write_resolved_json(project_dir: &Path, resolved: &ResolvedJson) -> anyhow::Result<()> {
    resolved.validate()?;
    let file = get_resolved_json_path(project_dir);
    write_atomic(&file, &sorted_json(resolved))
}

/// Upsert a single entry into `.ask/resolved.json`. Skips the rewrite when
/// nothing changed (modulo `fetchedAt`) so file watchers stay quiet on no-op
/// runs (parity with `upsertResolvedEntry`).
pub fn upsert_resolved_entry(
    project_dir: &Path,
    key: &str,
    entry: ResolvedEntry,
) -> anyhow::Result<()> {
    let mut resolved = read_resolved_json(project_dir)?;
    let changed = match resolved.entries.get(key) {
        None => true,
        Some(previous) => !eq_ignoring_fetched_at(previous, &entry),
    };
    if !changed {
        return Ok(());
    }
    resolved.entries.insert(key.to_string(), entry);
    resolved.generated_at = now_iso();
    write_resolved_json(project_dir, &resolved)
}

/// Remove one or more entries from `.ask/resolved.json` by key. No-op if the
/// key set is empty (parity with `removeResolvedEntries`).
pub fn remove_resolved_entries(project_dir: &Path, keys: &[String]) -> anyhow::Result<()> {
    if keys.is_empty() {
        return Ok(());
    }
    let mut resolved = read_resolved_json(project_dir)?;
    for k in keys {
        resolved.entries.remove(k);
    }
    resolved.generated_at = now_iso();
    write_resolved_json(project_dir, &resolved)
}

/// Two entries are equal ignoring `fetched_at` (the TS `stripFetchedAt`
/// comparison, done structurally instead of via JSON).
fn eq_ignoring_fetched_at(a: &ResolvedEntry, b: &ResolvedEntry) -> bool {
    let mut a = a.clone();
    a.fetched_at = b.fetched_at.clone();
    &a == b
}

/// Current UTC time as an RFC3339 string (`new Date().toISOString()` analog).
fn now_iso() -> String {
    OffsetDateTime::now_utc()
        .format(&Rfc3339)
        .expect("Rfc3339 formatting of now_utc is infallible")
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ask_json::entry_from_spec;

    fn tmp() -> tempfile::TempDir {
        tempfile::tempdir().unwrap()
    }

    #[test]
    fn sorted_json_sorts_keys_recursively_with_trailing_newline() {
        let value = serde_json::json!({ "b": 1, "a": { "d": 2, "c": 3 } });
        let out = sorted_json(&value);
        assert_eq!(
            out,
            "{\n  \"a\": {\n    \"c\": 3,\n    \"d\": 2\n  },\n  \"b\": 1\n}\n"
        );
    }

    #[test]
    fn content_hash_is_order_independent_and_nul_separated() {
        let f = |p: &str, c: &str| HashableFile {
            relpath: p.into(),
            bytes: None,
            content: Some(c.into()),
        };
        let a = content_hash(&[f("a.md", "x"), f("b.md", "y")]);
        let b = content_hash(&[f("b.md", "y"), f("a.md", "x")]);
        assert_eq!(a, b, "sorting by relpath makes order irrelevant");
        assert!(a.starts_with("sha256-"));
        assert_eq!(a.len(), "sha256-".len() + 64);
        // NUL separation: ("ab","cd") must differ from ("a","bcd").
        assert_ne!(
            content_hash(&[f("ab", "cd")]),
            content_hash(&[f("a", "bcd")])
        );
    }

    #[test]
    fn content_hash_matches_known_vector() {
        // sha256 of "a.md\0hello\0" — pin the exact stream format.
        let h = content_hash(&[HashableFile {
            relpath: "a.md".into(),
            bytes: None,
            content: Some("hello".into()),
        }]);
        let mut hasher = Sha256::new();
        hasher.update(b"a.md\0hello\0");
        assert_eq!(h, format!("sha256-{:x}", hasher.finalize()));
    }

    #[test]
    fn read_missing_ask_json_is_none() {
        let dir = tmp();
        assert!(read_ask_json(dir.path()).unwrap().is_none());
    }

    #[test]
    fn write_then_read_ask_json_roundtrips() {
        let dir = tmp();
        let ask = AskJson {
            libraries: vec![
                LibraryEntry::Spec("npm:next".into()),
                entry_from_spec("npm:zod", &["docs/API.md".to_string()]),
            ],
        };
        write_ask_json(dir.path(), &ask).unwrap();
        // ask.json is at the root, not under .ask/.
        assert!(dir.path().join("ask.json").exists());
        let back = read_ask_json(dir.path()).unwrap().unwrap();
        assert_eq!(back, ask);
    }

    #[test]
    fn find_entry_three_ways() {
        let ask = AskJson {
            libraries: vec![
                LibraryEntry::Spec("npm:next".into()),
                LibraryEntry::Spec("npm:@mastra/client-js".into()),
                LibraryEntry::Spec("github:vercel/next.js@v14.2.3".into()),
            ],
        };
        // 1. exact spec
        assert_eq!(find_entry(&ask, "npm:next").unwrap().spec(), "npm:next");
        // 2. library name (with version stripped)
        assert_eq!(
            find_entry(&ask, "next.js").unwrap().spec(),
            "github:vercel/next.js@v14.2.3"
        );
        // 3. raw npm package name (scoped)
        assert_eq!(
            find_entry(&ask, "@mastra/client-js").unwrap().spec(),
            "npm:@mastra/client-js"
        );
        assert!(find_entry(&ask, "nonexistent").is_none());
    }

    #[test]
    fn read_missing_resolved_is_empty_default() {
        let dir = tmp();
        let r = read_resolved_json(dir.path()).unwrap();
        assert_eq!(r.schema_version, 1);
        assert!(r.entries.is_empty());
    }

    #[test]
    fn corrupt_resolved_json_falls_back_to_empty() {
        let dir = tmp();
        let file = get_resolved_json_path(dir.path());
        std::fs::create_dir_all(file.parent().unwrap()).unwrap();
        std::fs::write(&file, "{ not valid json").unwrap();
        assert!(read_resolved_json(dir.path()).unwrap().entries.is_empty());
    }

    fn sample_entry(spec: &str) -> ResolvedEntry {
        ResolvedEntry {
            spec: spec.into(),
            resolved_version: "1.0.0".into(),
            content_hash: format!("sha256-{}", "0".repeat(64)),
            fetched_at: "2026-07-04T00:00:00Z".into(),
            file_count: 1,
            format: None,
            store_path: None,
            materialization: None,
            in_place_path: None,
            commit: None,
            store_subpath: None,
        }
    }

    #[test]
    fn upsert_then_remove_resolved_entry() {
        let dir = tmp();
        upsert_resolved_entry(dir.path(), "next", sample_entry("npm:next")).unwrap();
        let r = read_resolved_json(dir.path()).unwrap();
        assert!(r.entries.contains_key("next"));
        // generatedAt was stamped to a real time (not the 1970 default).
        assert_ne!(r.generated_at, "1970-01-01T00:00:00Z");

        remove_resolved_entries(dir.path(), &["next".to_string()]).unwrap();
        assert!(!read_resolved_json(dir.path())
            .unwrap()
            .entries
            .contains_key("next"));
    }

    #[test]
    fn upsert_skips_rewrite_when_only_fetched_at_differs() {
        let dir = tmp();
        upsert_resolved_entry(dir.path(), "next", sample_entry("npm:next")).unwrap();
        let first = read_resolved_json(dir.path()).unwrap().generated_at.clone();

        // Same entry but a different fetched_at → treated as unchanged, no rewrite.
        let mut again = sample_entry("npm:next");
        again.fetched_at = "2099-01-01T00:00:00Z".into();
        upsert_resolved_entry(dir.path(), "next", again).unwrap();
        assert_eq!(read_resolved_json(dir.path()).unwrap().generated_at, first);

        // A material change (version) → rewrite.
        let mut changed = sample_entry("npm:next");
        changed.resolved_version = "2.0.0".into();
        upsert_resolved_entry(dir.path(), "next", changed).unwrap();
        assert_eq!(
            read_resolved_json(dir.path()).unwrap().entries["next"].resolved_version,
            "2.0.0"
        );
    }
}