KB-4F4C

20B-P4 — Context-Pack Filesystem Retention Prompt

8 min read Revision 1
vector-hygiene20b-p4filesystemretentioncroncontext-packprompt

20B-P4 — Context-Pack Filesystem Retention

Date: 2026-05-05 | Status: PROMPT — chờ dispatch Controlling: GPT P4 directive + gate structure P1✅→P2✅→P3✅→P4 Scope: Cài wrapper + cron dọn FS staging >7 ngày. Không KB mutation. Không xóa dirs ngay (dry-run trước). Prior: P3 PASS (KB sạch, search restored)


§0. Mission

Gắn van tự động: cài script + cron dọn builds cũ >7 ngày trên filesystem. Giữ current symlink + tối thiểu 3 builds.


§1. Pre-read

Đọc 3 tài liệu từ KB:

  1. knowledge/dev/laws/dieu44-trien-khai/reports/20b-p3-context-pack-kb-purge-execution-report.md
  2. knowledge/dev/laws/dieu44-trien-khai/reviews/opus-review-20b-p3-purge-report-and-p4-proposal-2026-05-05.md
  3. knowledge/dev/laws/dieu44-trien-khai/reviews/gpt-review-20b-p3-report-and-p4-directive-2026-05-05.md

§2. Inspect filesystem

ls -la /opt/incomex/context-pack/ || true
ls -la /opt/incomex/context-pack-staging/ || true

# Current symlink
readlink -f /opt/incomex/context-pack/current || true

# All builds — oldest 20
find /opt/incomex/context-pack-staging -maxdepth 1 -mindepth 1 -type d -printf '%f %TY-%Tm-%Td %TH:%TM\n' | sort | head -20

# Newest 20
find /opt/incomex/context-pack-staging -maxdepth 1 -mindepth 1 -type d -printf '%f %TY-%Tm-%Td %TH:%TM\n' | sort | tail -20

# Total count
find /opt/incomex/context-pack-staging -maxdepth 1 -mindepth 1 -type d | wc -l

# Disk usage
du -sh /opt/incomex/context-pack-staging/

§3. Dry-run candidate list

# Dirs >7 ngày
find /opt/incomex/context-pack-staging -maxdepth 1 -mindepth 1 -type d -mtime +7 -print | sort > /tmp/retention-candidates.txt
wc -l /tmp/retention-candidates.txt
head -5 /tmp/retention-candidates.txt
tail -5 /tmp/retention-candidates.txt

# Current symlink target
CURRENT_TARGET=$(readlink -f /opt/incomex/context-pack/current 2>/dev/null || echo "NO_SYMLINK")
echo "Current target: $CURRENT_TARGET"

# Check current target NOT in candidates
grep -c "$CURRENT_TARGET" /tmp/retention-candidates.txt || echo "Current target safe (not in candidates)"

Gate checks:

  • Nếu current target nằm trong candidate list → STOP. Report. Không tiếp.
  • Nếu total builds - candidate count < 3 → STOP. Report. Không tiếp.

§4. Install wrapper script

Tạo: /opt/incomex/dot/bin/dot-context-pack-retention-cleanup

#!/usr/bin/env bash
set -euo pipefail

# === Config ===
STAGING_DIR="/opt/incomex/context-pack-staging"
RETENTION_DAYS=7
MIN_KEEP=3
LOG_PREFIX="[context-pack-retention]"
MODE="${1:---dry-run}"  # --dry-run (default) hoặc --execute

# === Guards ===
if [ -z "$STAGING_DIR" ] || [ ! -d "$STAGING_DIR" ]; then
  echo "$LOG_PREFIX ERROR: STAGING_DIR='$STAGING_DIR' invalid or missing. Abort." >&2
  exit 1
fi

# Resolve current symlink
CURRENT_LINK="/opt/incomex/context-pack/current"
if [ -L "$CURRENT_LINK" ]; then
  CURRENT_TARGET=$(readlink -f "$CURRENT_LINK")
else
  CURRENT_TARGET=""
  echo "$LOG_PREFIX WARN: No current symlink found at $CURRENT_LINK"
fi

# Build candidate list
mapfile -t ALL_DIRS < <(find "$STAGING_DIR" -maxdepth 1 -mindepth 1 -type d | sort)
mapfile -t CANDIDATES < <(find "$STAGING_DIR" -maxdepth 1 -mindepth 1 -type d -mtime +"$RETENTION_DAYS" | sort)

TOTAL=${#ALL_DIRS[@]}
CAND_COUNT=${#CANDIDATES[@]}
WOULD_REMAIN=$((TOTAL - CAND_COUNT))

echo "$LOG_PREFIX $(date -u +%Y-%m-%dT%H:%M:%SZ) mode=$MODE"
echo "$LOG_PREFIX total_builds=$TOTAL candidates=$CAND_COUNT would_remain=$WOULD_REMAIN min_keep=$MIN_KEEP"

# Guard: minimum retained
if [ "$WOULD_REMAIN" -lt "$MIN_KEEP" ]; then
  echo "$LOG_PREFIX ERROR: Would leave $WOULD_REMAIN builds (min=$MIN_KEEP). Abort." >&2
  exit 1
fi

DELETED=0
SKIPPED=0

for DIR in "${CANDIDATES[@]}"; do
  # Guard: path must be under STAGING_DIR
  case "$DIR" in
    "$STAGING_DIR"/*) ;;
    *) echo "$LOG_PREFIX SKIP (outside staging): $DIR"; SKIPPED=$((SKIPPED+1)); continue ;;
  esac

  # Guard: do not delete current target
  if [ -n "$CURRENT_TARGET" ] && [ "$(readlink -f "$DIR")" = "$CURRENT_TARGET" ]; then
    echo "$LOG_PREFIX SKIP (current target): $DIR"
    SKIPPED=$((SKIPPED+1))
    continue
  fi

  if [ "$MODE" = "--execute" ]; then
    rm -rf "$DIR"
    echo "$LOG_PREFIX DELETED: $DIR"
    DELETED=$((DELETED+1))
  else
    echo "$LOG_PREFIX DRY-RUN would delete: $DIR"
    DELETED=$((DELETED+1))
  fi
done

echo "$LOG_PREFIX Done. deleted=$DELETED skipped=$SKIPPED mode=$MODE"

Sau khi tạo file:

chmod 750 /opt/incomex/dot/bin/dot-context-pack-retention-cleanup
chown root:root /opt/incomex/dot/bin/dot-context-pack-retention-cleanup
bash -n /opt/incomex/dot/bin/dot-context-pack-retention-cleanup
echo "Syntax check exit: $?"

§5. Test wrapper — dry-run

/opt/incomex/dot/bin/dot-context-pack-retention-cleanup --dry-run

Ghi output vào report. Verify:

  • Current target không bị liệt kê
  • Would-remain ≥ 3
  • Tất cả candidates nằm dưới staging dir
  • Count candidates hợp lý (dự kiến ~120+ dirs >7 ngày)

§6. Install cron

Chỉ cài cron sau khi dry-run PASS.

# Tạo log dir
mkdir -p /var/log/incomex

# Cron file
cat > /etc/cron.d/incomex-context-pack-retention <<'EOF'
# Context-pack staging retention — 20B-P4
# Dọn builds >7 ngày, giữ current + min 3 builds
# Installed: 2026-05-05
15 3 * * * root /opt/incomex/dot/bin/dot-context-pack-retention-cleanup --execute >> /var/log/incomex/context-pack-retention.log 2>&1
EOF

chmod 644 /etc/cron.d/incomex-context-pack-retention

# Verify cron nhận
crontab -l 2>/dev/null || true
ls -la /etc/cron.d/incomex-context-pack-retention
cat /etc/cron.d/incomex-context-pack-retention

§7. Optional — execute cleanup ngay

Chỉ làm nếu dry-run PASS và Agent tự tin guards hoạt động đúng.

/opt/incomex/dot/bin/dot-context-pack-retention-cleanup --execute

Verify sau:

find /opt/incomex/context-pack-staging -maxdepth 1 -mindepth 1 -type d | wc -l
du -sh /opt/incomex/context-pack-staging/
readlink -f /opt/incomex/context-pack/current

Nếu không tự tin → chỉ cài wrapper + cron, để cron chạy lần đầu lúc 03:15 đêm nay.


§8. Report

Upload tại: knowledge/dev/laws/dieu44-trien-khai/reports/20b-p4-context-pack-filesystem-retention-report.md

# 20B-P4 — Context-Pack Filesystem Retention Report

## Filesystem inspection
- Total builds on FS: [N]
- Disk usage: [size]
- Current symlink target: [build_id]

## Dry-run
- Candidates >7 days: [N]
- Current target in candidates: no
- Would-remain: [N] (≥3: yes/no)
- Guards: PASS/FAIL

## Wrapper
- Installed: /opt/incomex/dot/bin/dot-context-pack-retention-cleanup
- Syntax check: PASS/FAIL
- Permissions: [mode owner]

## Cron
- Installed: /etc/cron.d/incomex-context-pack-retention
- Schedule: 15 3 * * *
- Mode: --execute

## Execution (if performed)
- Dirs deleted: [N]
- Dirs remaining: [N]
- Current symlink intact: yes/no
- Disk after: [size]

## Verdict
PASS / PARTIAL / FAIL

## Next
- Đ43/20C: kb_mirror_status semantics
- KB Governance Framework

§9. Hard Boundaries

  • ❌ Không KB delete
  • ❌ Không Agent Data mutation
  • ❌ Không search API patch
  • ❌ Không Đ43 patch
  • ❌ Không service restart
  • ❌ Không Pack 2C/IU
  • ❌ Không xóa ngoài /opt/incomex/context-pack-staging/
  • ❌ Không xóa current symlink target

§10. Nôm na

Gắn van hẹn giờ: mỗi đêm 3 giờ sáng, tự dọn giấy cũ >7 ngày trong tủ hồ sơ. Không đụng bản đang dùng, luôn giữ ít nhất 3 bản. Trước khi bật van thật, chạy thử 1 lần để xem van dọn đúng chưa.


20B-P4 Prompt | 2026-05-05 | FS retention. Dry-run first. Chờ dispatch.

Back to Knowledge Hub knowledge/dev/laws/dieu44-trien-khai/prompts/20b-p4-context-pack-filesystem-retention-prompt.md