@ainova-systems/intelligence 0.17.0 → 0.17.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.
@@ -118,6 +118,8 @@ adapter_remove() {
118
118
  read -r answer
119
119
  case "$answer" in y|Y|yes|YES) ;; *) echo "removal cancelled"; exit 0 ;; esac
120
120
  fi
121
+ # A render sources project adapters: never remove one under it.
122
+ project_lock_hold "$IP_ROOT"
121
123
  rm -f "$file"
122
124
  echo "removed: $content_dir/adapters/$name.sh"
123
125
  echo " generated output was kept"
@@ -136,6 +138,8 @@ case "$action" in
136
138
  [ -n "$name" ] && [ $# -eq 2 ] || die "usage: intelligence adapter <enable|disable> <name>"
137
139
  assert_valid_target_name "$name"
138
140
  require_cli_project
141
+ # Held through the final render: target-state and sync join it.
142
+ project_lock_hold "$IP_ROOT"
139
143
  ensure_project_current "$IP_ROOT"
140
144
  if [ "$action" = "enable" ]; then
141
145
  state_stage="$(mktemp -d -t intelligence-adapter-state-XXXXXX)"
@@ -1,9 +1,15 @@
1
1
  #!/bin/bash
2
2
  # intelligence sync [target] [--compact] [--force] - render intelligence to enabled tools.
3
3
  set -euo pipefail
4
- source "$CLI_DIR/lib/cli-common.sh"
5
4
 
5
+ # Print before library loading and lifecycle checks so a content verification
6
+ # never looks idle. Compact output remains a buffered machine-readable report.
6
7
  compact=0
8
+ for argument in "$@"; do
9
+ [ "$argument" != --compact ] || compact=1
10
+ done
11
+ [ "$compact" -ne 0 ] || echo 'Checking project...'
12
+ source "$CLI_DIR/lib/cli-common.sh"
7
13
  force=0
8
14
  target=""
9
15
  while [ $# -gt 0 ]; do
@@ -23,6 +29,9 @@ run_sync() {
23
29
  detect_project || return $?
24
30
  case "$IP_MODE" in
25
31
  cli)
32
+ # One writer per project at a time: the store restore and the
33
+ # render below both rewrite shared state (sync-lock.sh).
34
+ project_lock_hold "$IP_ROOT"
26
35
  # `sync` is the normal fresh-clone command. A newer globally installed
27
36
  # CLI aligns the current Intelligence project first (except in CI, where a
28
37
  # tracked migration must be reviewed and committed locally), and a
@@ -105,6 +105,9 @@ if [ "$mode" = "ask" ]; then
105
105
  esac
106
106
  fi
107
107
 
108
+ # Held from alignment through the final render: the package pass and the sync
109
+ # it execs join this lock instead of taking their own.
110
+ project_lock_hold "$IP_ROOT"
108
111
  ensure_project_current "$IP_ROOT"
109
112
  apply_args=(--no-sync)
110
113
  [ -z "$only" ] || apply_args+=("$only")
package/cli/intelligence CHANGED
@@ -7,15 +7,21 @@
7
7
 
8
8
  set -euo pipefail
9
9
 
10
- CLI_DIR="$(cd "$(dirname "$0")" && pwd)"
10
+ case "$0" in
11
+ */*) CLI_DIR="${0%/*}"; [ -n "$CLI_DIR" ] || CLI_DIR="/" ;;
12
+ *) CLI_DIR="." ;;
13
+ esac
14
+ CLI_DIR="$(cd "$CLI_DIR" && pwd)"
11
15
  export CLI_DIR
12
16
 
13
17
  # Engine discovery, by role (sync.sh + VERSION). The repo and the npm package
14
18
  # ship one layout — <root>/{cli,engine,packages/sync} — so there is a single
15
- # candidate rather than a per-distribution list.
19
+ # candidate rather than a per-distribution list. CLI_DIR is already the logical
20
+ # `pwd` spelling, so its textual parent is what `cd "$CLI_DIR/.."` would print.
21
+ CLI_ROOT="${CLI_DIR%/*}"
16
22
  IS_ENGINE_DIR=""
17
23
  if [ -f "$CLI_DIR/../engine/sync.sh" ] && [ -f "$CLI_DIR/../engine/VERSION" ]; then
18
- IS_ENGINE_DIR="$(cd "$CLI_DIR/../engine" && pwd)"
24
+ IS_ENGINE_DIR="$CLI_ROOT/engine"
19
25
  fi
20
26
  if [ -z "$IS_ENGINE_DIR" ]; then
21
27
  echo "ERROR: bundled sync engine not found next to the CLI ($CLI_DIR)." >&2
@@ -29,7 +35,7 @@ export IS_ENGINE_DIR
29
35
  # engine dir: the store must receive rules/agents/skills, never engine scripts.
30
36
  IS_BUNDLED_PKG_DIR=""
31
37
  if [ -d "$CLI_DIR/../packages/sync" ]; then
32
- IS_BUNDLED_PKG_DIR="$(cd "$CLI_DIR/../packages/sync" && pwd)"
38
+ IS_BUNDLED_PKG_DIR="$CLI_ROOT/packages/sync"
33
39
  fi
34
40
  export IS_BUNDLED_PKG_DIR
35
41
 
@@ -15,6 +15,7 @@ while [ $# -gt 0 ]; do
15
15
  done
16
16
 
17
17
  require_cli_project
18
+ project_lock_hold "$IP_ROOT"
18
19
  manifest="$IP_ROOT/intelligence.yaml"
19
20
 
20
21
  stamp="$(read_schema_version "$manifest")"
@@ -227,7 +227,38 @@ rewrite_source_value() {
227
227
  esac
228
228
  printf '%s' "$v"
229
229
  }
230
- in_packs=0 in_sources=0
230
+ # The project's own content stays where the legacy layout kept it. When that
231
+ # directory is not `intelligence/`, the manifest names it: the content directory
232
+ # is where project adapters live, what no output may overwrite, and the source of
233
+ # truth AGENTS.md points readers at. Left at the default, all three name a
234
+ # directory that only a case-insensitive filesystem confuses with the real one.
235
+ legacy_content_dir=""
236
+ case "$umbrella_rel" in
237
+ "$umbrella"|intelligence|*/*|.intelligence*|*[\"\'\\]*|*[[:cntrl:]]*) ;;
238
+ *)
239
+ # Only content that stays counts: the vendored module and pack mirrors
240
+ # move into the store and leave, so a config-only umbrella is not one.
241
+ if [ -z "$(get_yaml_field "$config" "project" "intelligence_dir")" ]; then
242
+ for section in rules agents skills; do
243
+ while IFS= read -r src; do
244
+ case "$src" in ""|git+*|@*) continue ;; esac
245
+ src_canon="$(normalize_path "$root/$src")"
246
+ case "$src_canon" in "$root"/*) src_rel="${src_canon#"$root"/}" ;; *) continue ;; esac
247
+ case "$src_rel" in "$module_rel"|"$module_rel"/*) continue ;; esac
248
+ leaving=0
249
+ while IFS="$LOCK_SEP" read -r _ _ _ _ mirror; do
250
+ [ -n "$mirror" ] || continue
251
+ mirror="$(normalize_path "$root/$mirror")"
252
+ case "$src_canon" in "$mirror"|"$mirror"/*) leaving=1 ;; esac
253
+ done < "$pack_rows"
254
+ [ "$leaving" -eq 0 ] || continue
255
+ case "$src_rel" in "$umbrella_rel"|"$umbrella_rel"/*) legacy_content_dir="$umbrella_rel" ;; esac
256
+ done < <(read_yaml_list "$config" "$section")
257
+ done
258
+ fi
259
+ ;;
260
+ esac
261
+ in_packs=0 in_sources=0 project_key=0 content_dir_written=0
231
262
  while IFS= read -r line || [ -n "$line" ]; do
232
263
  line="${line%$'\r'}"
233
264
  case "$line" in
@@ -236,6 +267,18 @@ while IFS= read -r line || [ -n "$line" ]; do
236
267
  sources:*) in_sources=1; in_packs=0; echo "$line"; continue ;;
237
268
  [!\ \#]*) in_packs=0; in_sources=0 ;;
238
269
  esac
270
+ case "$line" in project:*) project_key=1 ;; esac
271
+ if [ -n "$legacy_content_dir" ] && [ "$content_dir_written" -eq 0 ]; then
272
+ # Only the block form takes a child line; `project: {...}` does not.
273
+ key_part="${line%%#*}"
274
+ key_part="${key_part%"${key_part##*[![:space:]]}"}"
275
+ if [ "$key_part" = "project:" ]; then
276
+ echo "$line"
277
+ echo " intelligence_dir: \"$legacy_content_dir\""
278
+ content_dir_written=1
279
+ continue
280
+ fi
281
+ fi
239
282
  [ "$in_packs" -eq 1 ] && continue
240
283
  if [ "$in_sources" -eq 1 ]; then
241
284
  case "$line" in
@@ -261,6 +304,9 @@ while IFS= read -r line || [ -n "$line" ]; do
261
304
  fi
262
305
  echo "$line"
263
306
  done < "$config" > "$manifest_stage"
307
+ if [ -n "$legacy_content_dir" ] && [ "$content_dir_written" -eq 0 ] && [ "$project_key" -eq 0 ]; then
308
+ printf 'project:\n intelligence_dir: "%s"\n' "$legacy_content_dir" >> "$manifest_stage"
309
+ fi
264
310
  stamp_schema_version "$manifest_stage" "$eng"
265
311
 
266
312
  # packages: requested pins only; resolved source details live in the lock.
@@ -24,6 +24,7 @@ while [ $# -gt 0 ]; do
24
24
  done
25
25
 
26
26
  require_cli_project
27
+ project_lock_hold "$IP_ROOT"
27
28
  manifest="$IP_ROOT/intelligence.yaml"
28
29
  lock="$IP_ROOT/intelligence.lock"
29
30
 
@@ -16,6 +16,7 @@ for a in "$@"; do
16
16
  done
17
17
 
18
18
  require_cli_project
19
+ project_lock_hold "$IP_ROOT"
19
20
 
20
21
  # Removing the engine-content package guts the outputs (meta-skills, the
21
22
  # authoring rule, both engine agents disappear) while everything still
@@ -25,6 +25,7 @@ done
25
25
  || die "--latest needs the package to move: intelligence update @scope/name --latest"
26
26
 
27
27
  require_cli_project
28
+ [ "$preview" -eq 1 ] || project_lock_hold "$IP_ROOT"
28
29
  manifest="$IP_ROOT/intelligence.yaml"
29
30
  lock="$IP_ROOT/intelligence.lock"
30
31
 
@@ -19,6 +19,7 @@ while [ $# -gt 0 ]; do
19
19
  done
20
20
 
21
21
  require_cli_project
22
+ project_lock_hold "$IP_ROOT"
22
23
  manifest="$IP_ROOT/intelligence.yaml"
23
24
  lock="$IP_ROOT/intelligence.lock"
24
25
  validate_project_lock "$IP_ROOT" --restore
@@ -15,6 +15,7 @@ case "$action" in enable|disable) ;; *) die "internal target state: expected <en
15
15
  assert_valid_target_name "$name"
16
16
 
17
17
  require_cli_project
18
+ project_lock_hold "$IP_ROOT"
18
19
  manifest="$IP_ROOT/intelligence.yaml"
19
20
  content_dir="$(manifest_intelligence_dir "$manifest")"
20
21
 
@@ -87,6 +87,7 @@ source "$CLI_DIR/lib/adapter-lifecycle.sh"
87
87
  source "$CLI_DIR/lib/gitignore.sh"
88
88
  source "$CLI_DIR/lib/onboarding.sh"
89
89
  source "$CLI_DIR/lib/sync-cache.sh"
90
+ source "$CLI_DIR/lib/sync-lock.sh"
90
91
 
91
92
  # The engine-content package: OPTIONAL but auto-selected at init. Package by
92
93
  # UX (manifest entry, lockfile row, list/search/remove), bundle by mechanics —
@@ -97,12 +98,21 @@ source "$CLI_DIR/lib/sync-cache.sh"
97
98
  # Its identity is DATA shipped with the distribution (cli/engine-package.yaml),
98
99
  # never a name compiled into cli code — a fork edits the file.
99
100
  _EPKG="$CLI_DIR/engine-package.yaml"
100
- SYNC_PKG_NAME="$(top_scalar "$_EPKG" "name")"
101
- SYNC_PKG_URL="$(top_scalar "$_EPKG" "url")"
102
- SYNC_PKG_PATH="$(top_scalar "$_EPKG" "path")"
101
+ SYNC_PKG_NAME="" SYNC_PKG_URL="" SYNC_PKG_PATH=""
103
102
  # Read by commands (init seeds it) — per-file shellcheck cannot see that.
104
103
  # shellcheck disable=SC2034
105
- DEFAULT_REGISTRY_URL="$(top_scalar "$_EPKG" "default_registry")"
104
+ DEFAULT_REGISTRY_URL=""
105
+ # Every command loads this file first: read the four fields in one pass.
106
+ # shellcheck disable=SC2034 # DEFAULT_REGISTRY_URL is read by commands, as above
107
+ while IFS="$LOCK_SEP" read -r _epkg_key _epkg_value; do
108
+ case "$_epkg_key" in
109
+ name) SYNC_PKG_NAME="$_epkg_value" ;;
110
+ url) SYNC_PKG_URL="$_epkg_value" ;;
111
+ path) SYNC_PKG_PATH="$_epkg_value" ;;
112
+ default_registry) DEFAULT_REGISTRY_URL="$_epkg_value" ;;
113
+ esac
114
+ done <<< "$(_qmap_read tops "$_EPKG" '' 'name url path default_registry')"
115
+ unset _epkg_key _epkg_value
106
116
  [ -n "$SYNC_PKG_NAME" ] || die "corrupt CLI installation: $_EPKG is missing or has no 'name'"
107
117
  SYNC_PKG_STORE=".intelligence/packages/$SYNC_PKG_NAME"
108
118
 
@@ -116,15 +126,18 @@ SYNC_PKG_STORE=".intelligence/packages/$SYNC_PKG_NAME"
116
126
  # by role — scripts/sync.sh + scripts/VERSION — never by name.
117
127
  detect_project() {
118
128
  IP_MODE="none"; IP_ROOT=""; IP_UMBRELLA=""; IP_MODULE_DIR=""
119
- local dir="$PWD"
129
+ # PWD is already the logical `pwd` spelling, and so is each textual parent.
130
+ local dir="$PWD" parent
120
131
  while :; do
121
132
  if [ -f "$dir/intelligence.yaml" ]; then
122
133
  IP_MODE="cli"
123
- IP_ROOT="$(cd "$dir" && pwd)"
134
+ IP_ROOT="$dir"
124
135
  return 0
125
136
  fi
126
- [ "$dir" = "$(dirname "$dir")" ] && break
127
- dir="$(dirname "$dir")"
137
+ parent="${dir%/*}"
138
+ [ -n "$parent" ] || parent="/"
139
+ [ "$dir" = "$parent" ] && break
140
+ dir="$parent"
128
141
  done
129
142
  local root
130
143
  root="$(git rev-parse --show-toplevel 2>/dev/null || true)"
@@ -257,10 +270,23 @@ default_target_output() {
257
270
  esac
258
271
  }
259
272
 
273
+ # bundled_engine_version_var sets IS_BUNDLED_ENGINE_VERSION, reading VERSION
274
+ # once per process; lifecycle preflight compares against it several times.
275
+ bundled_engine_version_var() {
276
+ [ -z "${IS_BUNDLED_ENGINE_VERSION_READ:-}" ] || return 0
277
+ local version=""
278
+ [ -r "$IS_ENGINE_DIR/VERSION" ] || {
279
+ echo "ERROR: cannot read $IS_ENGINE_DIR/VERSION" >&2
280
+ return 1
281
+ }
282
+ IFS= read -r -d '' version < "$IS_ENGINE_DIR/VERSION" || true
283
+ IS_BUNDLED_ENGINE_VERSION="${version//[$' \t\r\n']/}"
284
+ IS_BUNDLED_ENGINE_VERSION_READ=1
285
+ }
286
+
260
287
  bundled_engine_version() {
261
- local version
262
- version="$(< "$IS_ENGINE_DIR/VERSION")"
263
- printf '%s' "${version//[$' \t\r\n']/}"
288
+ bundled_engine_version_var
289
+ printf '%s' "$IS_BUNDLED_ENGINE_VERSION"
264
290
  }
265
291
 
266
292
  # --- The sync package's manifest/lock plumbing ---------------------------
@@ -345,15 +371,19 @@ assert_safe_content_dir() {
345
371
  # the admitted minor/patch gap; the predicate itself does not re-judge the gap.
346
372
  project_stamped_ahead() {
347
373
  local stamp
348
- stamp="$(read_schema_version "$1/intelligence.yaml")"
349
- [ -n "$stamp" ] && _ver_gt "$stamp" "$(bundled_engine_version)"
374
+ read_schema_version_var "$1/intelligence.yaml"
375
+ stamp="$IS_SCHEMA_VERSION"
376
+ bundled_engine_version_var
377
+ [ -n "$stamp" ] && _ver_gt "$stamp" "$IS_BUNDLED_ENGINE_VERSION"
350
378
  }
351
379
 
352
380
  project_needs_upgrade() {
353
- local root="$1" manifest="$1/intelligence.yaml" stamp eng pinned locked name
381
+ local root="$1" manifest="$1/intelligence.yaml" stamp eng pinned locked name url path rows
354
382
  [ -f "$manifest" ] || return 1
355
- stamp="$(read_schema_version "$manifest")"
356
- eng="$(bundled_engine_version)"
383
+ read_schema_version_var "$manifest"
384
+ stamp="$IS_SCHEMA_VERSION"
385
+ bundled_engine_version_var
386
+ eng="$IS_BUNDLED_ENGINE_VERSION"
357
387
  [ -z "$stamp" ] && return 0
358
388
  # A project stamped ahead belongs to a newer CLI. Aligning it here would
359
389
  # restamp the schema and re-pin the engine content DOWNWARD, and the next
@@ -363,14 +393,15 @@ project_needs_upgrade() {
363
393
  _ver_gt "$eng" "$stamp" && return 0
364
394
  [ -d "$root/.intelligence/engine" ] && return 0
365
395
 
366
- while IFS= read -r name; do
396
+ # One manifest pass for every package's fields, not a reader per field.
397
+ rows="$(_qmap_read fieldrows "$manifest" packages '' 'url path version')"
398
+ while IFS="$LOCK_SEP" read -r name url path pinned; do
367
399
  [ -n "$name" ] || continue
368
400
  # Early RC manifests mixed requested intent with resolved source
369
401
  # details. Source URL/path now live only in the required lockfile.
370
- [ -n "$(qmap_field "$manifest" "packages" "$name" "url")" ] && return 0
371
- [ -n "$(qmap_field "$manifest" "packages" "$name" "path")" ] && return 0
402
+ [ -n "$url" ] && return 0
403
+ [ -n "$path" ] && return 0
372
404
  if [ "$name" = "$SYNC_PKG_NAME" ]; then
373
- pinned="$(qmap_field "$manifest" "packages" "$name" "version")"
374
405
  locked="$(qmap_field "$root/intelligence.lock" "packages" "$name" "resolved")"
375
406
  [ "$pinned" = "$eng" ] || return 0
376
407
  if [ -f "$root/intelligence.lock" ]; then
@@ -378,7 +409,7 @@ project_needs_upgrade() {
378
409
  [ "${locked#v}" = "$eng" ] || return 0
379
410
  fi
380
411
  fi
381
- done < <(qmap_keys "$manifest" "packages")
412
+ done <<< "$rows"
382
413
  return 1
383
414
  }
384
415
 
@@ -402,8 +433,10 @@ ensure_project_current() {
402
433
  die "manifest declares packages but intelligence.lock is absent — restore the committed lock before running project lifecycle commands"
403
434
  fi
404
435
  project_needs_upgrade "$root" || return 0
405
- stamp="$(read_schema_version "$manifest")"
406
- eng="$(bundled_engine_version)"
436
+ read_schema_version_var "$manifest"
437
+ stamp="$IS_SCHEMA_VERSION"
438
+ bundled_engine_version_var
439
+ eng="$IS_BUNDLED_ENGINE_VERSION"
407
440
  if is_ci_environment && [ "$explicit" != "--explicit" ]; then
408
441
  die "project lifecycle requires alignment (stamp ${stamp:-unstamped}, engine $eng) — run 'intelligence init --apply' locally, review and commit the diff"
409
442
  fi
@@ -24,7 +24,8 @@ lock_validate() (
24
24
  # The same tokenizer supplies ordinary lock rows and strict metadata. Buffer
25
25
  # before consumption: a failed parser must never yield a partial package set.
26
26
  rows="$(_qmap_read lock "$lock" packages '' '' "$LOCKFILE_VERSION")" || return $?
27
- current="$(bundled_engine_version)"
27
+ bundled_engine_version_var
28
+ current="$IS_BUNDLED_ENGINE_VERSION"
28
29
  while IFS="$LOCK_SEP" read -r kind name requested url path resolved sha line; do
29
30
  case "$kind" in
30
31
  V)
package/cli/lib/qmap.awk CHANGED
@@ -177,7 +177,17 @@ END {
177
177
  exit 1
178
178
  }
179
179
  if (mode == "lock") print "V" sep top_value["lockfile_version"] sep top_value["engine_version"]
180
- if (mode == "rows" || mode == "lock" || mode == "keys") {
180
+ if (mode == "fieldrows") {
181
+ # One row per package key, as `keys` lists them: the name, then each
182
+ # field QMAP_FIELD names (space separated), as qmap_field decodes it.
183
+ count = split(wanted_field, fields, " ")
184
+ for (i = 1; i <= row_count; i++) {
185
+ name = row_name[i]
186
+ row = name
187
+ for (f = 1; f <= count; f++) row = row sep field_value[name SUBSEP fields[f]]
188
+ print row
189
+ }
190
+ } else if (mode == "rows" || mode == "lock" || mode == "keys") {
181
191
  for (i = 1; i <= row_count; i++) {
182
192
  name = row_name[i]
183
193
  if (mode == "keys") print name
@@ -187,5 +197,10 @@ END {
187
197
  else print row
188
198
  }
189
199
  }
200
+ } else if (mode == "tops") {
201
+ # Several top-level scalars in one pass: QMAP_KEY lists them, space
202
+ # separated; each present one prints as key<US>value.
203
+ count = split(wanted_key, tops, " ")
204
+ for (i = 1; i <= count; i++) if (tops[i] in top_value) print tops[i] sep top_value[tops[i]]
190
205
  } else if (found) print result
191
206
  }
@@ -16,6 +16,8 @@ sync_cache_safe_path() {
16
16
  # One checked enumeration and one batch hash, including names, directory/file
17
17
  # types and executable bits. Timestamps cannot prove content is unchanged.
18
18
  # Unsupported paths/types are a cache miss, never an incomplete fingerprint.
19
+ # Keep list descriptors open across the walk; reopening each line is expensive
20
+ # on Windows and provides no additional content verification.
19
21
  sync_cache_fingerprint() (
20
22
  local work="$1" path kind executable seen=$'\n'
21
23
  local -a existing
@@ -48,14 +50,14 @@ sync_cache_fingerprint() (
48
50
  if [ -f "$path" ]; then
49
51
  kind='file'
50
52
  [ ! -x "$path" ] || executable=1
51
- printf '%s\n' "$path" >> "$work/files" || return 1
53
+ printf '%s\n' "$path" >&4 || return 1
52
54
  elif [ -d "$path" ]; then
53
55
  kind=directory
54
56
  else
55
57
  return 1
56
58
  fi
57
- printf '%s\t%s\t%s\n' "$kind" "$executable" "$path" >> "$work/entries" || return 1
58
- done < "$work/list"
59
+ printf '%s\t%s\t%s\n' "$kind" "$executable" "$path" >&3 || return 1
60
+ done < "$work/list" 3>> "$work/entries" 4> "$work/files" || return 1
59
61
  # find order need not be stable. Sort both streams; preserve path-to-hash
60
62
  # association by feeding the sorted file list into a single git process.
61
63
  LC_ALL=C sort -u "$work/entries" > "$work/sorted" || return 1
@@ -109,14 +111,17 @@ sync_cache_dependencies() {
109
111
  }
110
112
 
111
113
  sync_cache_input_hash() {
112
- local fingerprint
114
+ local fingerprint payload
113
115
  fingerprint="$(sync_cache_fingerprint "$1" "${SC_INPUTS[@]}")" || return 1
116
+ [ -n "${SC_UMASK:-}" ] || SC_UMASK="$(umask)"
114
117
  # Explicit environment values that affect rendering, discovery or parsing.
115
118
  # No secrets or inherited environment dump are persisted.
116
- printf '%s\n' "$fingerprint" "$2" "$REPO_ROOT" "$CONFIG_FILE" \
119
+ printf -v payload '%s\n' "$fingerprint" "$2" "$REPO_ROOT" "$CONFIG_FILE" \
117
120
  "$IS_CONTENT_REL" "$IS_MODULE_REL" "$IS_SYNC_CMD" "$IS_MANIFEST_NAME" \
118
121
  "$IS_PROTECTED_DIRS" "$BASH_VERSION" "${OSTYPE:-}" "${LANG:-}" \
119
- "${LC_ALL:-}" "${LC_CTYPE:-}" "${LC_COLLATE:-}" "${PATH:-}" "$(umask)" | git hash-object --stdin
122
+ "${LC_ALL:-}" "${LC_CTYPE:-}" "${LC_COLLATE:-}" "${PATH:-}" "$SC_UMASK"
123
+ # A here-string appends the final newline the payload already ends with.
124
+ git hash-object --stdin <<< "${payload%$'\n'}"
120
125
  }
121
126
 
122
127
  sync_cache_directory() {
@@ -161,15 +166,21 @@ sync_with_cache() (
161
166
  local target="$1" force="$2" work before after outputs report_hash rc=0 cacheable=0
162
167
  work="$(mktemp -d -t intelligence-cache-XXXXXX)" || return 1
163
168
  trap 'rm -rf "$work"' EXIT
169
+ echo "Checking source files..."
170
+ # Read once here so both input hashes, one in a background job, reuse it.
171
+ SC_UMASK="$(umask)"
164
172
  if sync_cache_dependencies && sync_cache_directory; then
165
173
  before="$(sync_cache_input_hash "$work" "$target")" || before=""
166
174
  [ -z "$before" ] || cacheable=1
167
175
  fi
168
176
  if [ "$cacheable" = 1 ] && [ "$force" = 0 ]; then
177
+ echo "Checking generated files..."
169
178
  outputs="$(sync_cache_fingerprint "$work" "${SC_OUTPUTS[@]}")" || outputs=""
170
179
  if [ -n "$outputs" ] && sync_cache_read "$SC_DIRECTORY/state" "$before" "$outputs" "$work"; then
171
- echo ' Unchanged: verified sources and generated output; skipped rendering (use --force to refresh discovery).'
180
+ echo 'Unchanged: generated files are up to date.'
181
+ printf '\n'
172
182
  cat "$work/report"
183
+ printf '\n=== Unchanged: no files needed updating ===\n'
173
184
  return 0
174
185
  fi
175
186
  fi
@@ -177,26 +188,45 @@ sync_with_cache() (
177
188
  if [ "$cacheable" = 1 ]; then
178
189
  rm -f "$SC_DIRECTORY/state" || cacheable=0
179
190
  fi
180
- # Buffer once so success diagnostics can be replayed without asking the
181
- # engine to know about persistent CLI state. Preserve the engine exit code.
182
- bash "$IS_ENGINE_DIR/sync.sh" "$target" > "$work/log" 2>&1 || rc=$?
183
- cat "$work/log"
184
- [ "$rc" = 0 ] || return "$rc"
185
- [ "$cacheable" = 1 ] || return 0
186
- after="$(sync_cache_input_hash "$work" "$target")" || return 0
187
- [ "$before" = "$after" ] || return 0
188
- outputs="$(sync_cache_fingerprint "$work" "${SC_OUTPUTS[@]}")" || return 0
189
- sync_cache_report "$work/log" > "$work/report" || return 0
190
- report_hash="$(git hash-object --stdin < "$work/report")" || return 0
191
- # Recheck containment before publishing. mktemp+rename avoids following a
192
- # pre-existing state-file link, and readers see only complete records.
193
- sync_cache_directory create || return 0
194
- local staged
195
- staged="$(mktemp "$SC_DIRECTORY/state-XXXXXX")" || return 0
196
- if { printf 'intelligence-sync-cache-v1 %s %s %s\n' "$after" "$outputs" "$report_hash"; cat "$work/report"; } > "$staged"; then
197
- mv -f "$staged" "$SC_DIRECTORY/state" || rm -f "$staged"
191
+ # Stream progress while retaining diagnostics for the next cache hit. Check
192
+ # both processes: a failed capture must never publish a success record.
193
+ local -a pipeline_status
194
+ if bash "$IS_ENGINE_DIR/sync.sh" "$target" 2>&1 | tee "$work/log"; then
195
+ pipeline_status=("${PIPESTATUS[@]}")
198
196
  else
199
- rm -f "$staged"
197
+ pipeline_status=("${PIPESTATUS[@]}")
198
+ fi
199
+ rc="${pipeline_status[0]}"
200
+ [ "$rc" = 0 ] || return "$rc"
201
+ [ "${pipeline_status[1]}" = 0 ] || return "${pipeline_status[1]}"
202
+ if [ "$cacheable" = 1 ]; then
203
+ (
204
+ echo "Checking sync results..."
205
+ # The input recheck and the output fingerprint only read files, so
206
+ # they run side by side, each in its own scratch directory.
207
+ local after_pid after_rc=0
208
+ mkdir "$work/after" "$work/outputs" || return 0
209
+ sync_cache_input_hash "$work/after" "$target" > "$work/after/hash" &
210
+ after_pid=$!
211
+ outputs="$(sync_cache_fingerprint "$work/outputs" "${SC_OUTPUTS[@]}")" || outputs=""
212
+ sync_cache_report "$work/log" > "$work/report" || outputs=""
213
+ report_hash="$(git hash-object --stdin < "$work/report")" || outputs=""
214
+ wait "$after_pid" || after_rc=$?
215
+ [ "$after_rc" = 0 ] && [ -n "$outputs" ] || return 0
216
+ IFS= read -r after < "$work/after/hash" || return 0
217
+ [ "$before" = "$after" ] || return 0
218
+ # Recheck containment before publishing. mktemp+rename avoids following a
219
+ # pre-existing state-file link, and readers see only complete records.
220
+ sync_cache_directory create || return 0
221
+ local staged
222
+ staged="$(mktemp "$SC_DIRECTORY/state-XXXXXX")" || return 0
223
+ if { printf 'intelligence-sync-cache-v1 %s %s %s\n' "$after" "$outputs" "$report_hash"; cat "$work/report"; } > "$staged"; then
224
+ mv -f "$staged" "$SC_DIRECTORY/state" || rm -f "$staged"
225
+ else
226
+ rm -f "$staged"
227
+ fi
228
+ )
200
229
  fi
230
+ echo "=== Sync complete ==="
201
231
  return 0
202
232
  )